]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ofed/management/libibmad/src/resolve.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ofed / management / libibmad / src / resolve.c
1 /*
2  * Copyright (c) 2004-2006 Voltaire Inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #if HAVE_CONFIG_H
35 #  include <config.h>
36 #endif /* HAVE_CONFIG_H */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <pthread.h>
42 #include <string.h>
43 #include <sys/time.h>
44
45 #include <infiniband/common.h>
46 #include <infiniband/umad.h>
47 #include <mad.h>
48
49 #undef DEBUG
50 #define DEBUG   if (ibdebug)    IBWARN
51
52 int
53 ib_resolve_smlid_via(ib_portid_t *sm_id, int timeout, const void *srcport)
54 {
55         ib_portid_t self = {0};
56         uint8_t portinfo[64];
57         int lid;
58
59         memset(sm_id, 0, sizeof(*sm_id));
60
61         if (!smp_query_via(portinfo, &self, IB_ATTR_PORT_INFO,
62                            0, 0, srcport))
63                 return -1;
64
65         mad_decode_field(portinfo, IB_PORT_SMLID_F, &lid);
66
67         return ib_portid_set(sm_id, lid, 0, 0);
68 }
69
70 int
71 ib_resolve_smlid(ib_portid_t *sm_id, int timeout)
72 {
73         return ib_resolve_smlid_via(sm_id, timeout, NULL);
74 }
75
76 int
77 ib_resolve_guid_via(ib_portid_t *portid, uint64_t *guid, ib_portid_t *sm_id, int timeout, const void *srcport)
78 {
79         ib_portid_t sm_portid;
80         char buf[IB_SA_DATA_SIZE] = {0};
81
82         if (!sm_id) {
83                 sm_id = &sm_portid;
84                 if (ib_resolve_smlid_via(sm_id, timeout, srcport) < 0)
85                         return -1;
86         }
87         if (*(uint64_t*)&portid->gid == 0)
88                 mad_set_field64(portid->gid, 0, IB_GID_PREFIX_F, IB_DEFAULT_SUBN_PREFIX);
89         if (guid)
90                 mad_set_field64(portid->gid, 0, IB_GID_GUID_F, *guid);
91
92         if ((portid->lid = ib_path_query_via(srcport, portid->gid, portid->gid, sm_id, buf)) < 0)
93                 return -1;
94
95         return 0;
96 }
97
98 int
99 ib_resolve_portid_str_via(ib_portid_t *portid, char *addr_str, int dest_type, ib_portid_t *sm_id, const void *srcport)
100 {
101         uint64_t guid;
102         int lid;
103         char *routepath;
104         ib_portid_t selfportid = {0};
105         int selfport = 0;
106
107         switch (dest_type) {
108         case IB_DEST_LID:
109                 lid = strtol(addr_str, 0, 0);
110                 if (!IB_LID_VALID(lid))
111                         return -1;
112                 return ib_portid_set(portid, lid, 0, 0);
113
114         case IB_DEST_DRPATH:
115                 if (str2drpath(&portid->drpath, addr_str, 0, 0) < 0)
116                         return -1;
117                 return 0;
118
119         case IB_DEST_GUID:
120                 if (!(guid = strtoull(addr_str, 0, 0)))
121                         return -1;
122
123                 /* keep guid in portid? */
124                 return ib_resolve_guid_via(portid, &guid, sm_id, 0, srcport);
125
126         case IB_DEST_DRSLID:
127                 lid = strtol(addr_str, &routepath, 0);
128                 routepath++;
129                 if (!IB_LID_VALID(lid))
130                         return -1;
131                 ib_portid_set(portid, lid, 0, 0);
132
133                 /* handle DR parsing and set DrSLID to local lid */
134                 if (ib_resolve_self_via(&selfportid, &selfport, 0, srcport) < 0)
135                         return -1;
136                 if (str2drpath(&portid->drpath, routepath, selfportid.lid, 0) < 0)
137                         return -1;
138                 return 0;
139
140         default:
141                 IBWARN("bad dest_type %d", dest_type);
142         }
143
144         return -1;
145 }
146
147 int
148 ib_resolve_portid_str(ib_portid_t *portid, char *addr_str, int dest_type, ib_portid_t *sm_id)
149 {
150         return ib_resolve_portid_str_via(portid, addr_str, dest_type,
151                                          sm_id, NULL);
152 }
153
154 int
155 ib_resolve_self_via(ib_portid_t *portid, int *portnum, ibmad_gid_t *gid,
156                     const void *srcport)
157 {
158         ib_portid_t self = {0};
159         uint8_t portinfo[64];
160         uint8_t nodeinfo[64];
161         uint64_t guid, prefix;
162
163         if (!smp_query_via(nodeinfo, &self, IB_ATTR_NODE_INFO, 0, 0, srcport))
164                 return -1;
165
166         if (!smp_query_via(portinfo, &self, IB_ATTR_PORT_INFO, 0, 0, srcport))
167                 return -1;
168
169         mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
170         mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
171         mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
172
173         if (portnum)
174                 mad_decode_field(nodeinfo, IB_NODE_LOCAL_PORT_F, portnum);
175         if (gid) {
176                 mad_encode_field(*gid, IB_GID_PREFIX_F, &prefix);
177                 mad_encode_field(*gid, IB_GID_GUID_F, &guid);
178         }
179         return 0;
180 }
181
182 int
183 ib_resolve_self(ib_portid_t *portid, int *portnum, ibmad_gid_t *gid)
184 {
185         return ib_resolve_self_via (portid, portnum, gid, NULL);
186 }