2 * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2009 HNR Consulting. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #endif /* HAVE_CONFIG_H */
42 #include <arpa/inet.h>
45 #include <infiniband/umad.h>
46 #include <infiniband/mad.h>
47 #include "mad_internal.h"
50 #define DEBUG if (ibdebug) IBWARN
52 int ib_resolve_smlid_via(ib_portid_t * sm_id, int timeout,
53 const struct ibmad_port *srcport)
58 memset(sm_id, 0, sizeof(*sm_id));
60 ret = umad_get_port(srcport->ca_name, srcport->portnum, &port);
64 if (!IB_LID_VALID(port.sm_lid)) {
68 sm_id->sl = port.sm_sl;
70 ret = ib_portid_set(sm_id, port.sm_lid, 0, 0);
71 umad_release_port(&port);
75 int ib_resolve_smlid(ib_portid_t * sm_id, int timeout)
77 return ib_resolve_smlid_via(sm_id, timeout, ibmp);
80 int ib_resolve_gid_via(ib_portid_t * portid, ibmad_gid_t gid,
81 ib_portid_t * sm_id, int timeout,
82 const struct ibmad_port *srcport)
84 ib_portid_t sm_portid = { 0 };
85 char buf[IB_SA_DATA_SIZE] = { 0 };
90 if (!IB_LID_VALID(sm_id->lid)) {
91 if (ib_resolve_smlid_via(sm_id, timeout, srcport) < 0)
96 ib_path_query_via(srcport, gid, gid, sm_id, buf)) < 0)
102 int ib_resolve_guid_via(ib_portid_t * portid, uint64_t * guid,
103 ib_portid_t * sm_id, int timeout,
104 const struct ibmad_port *srcport)
106 ib_portid_t sm_portid = { 0 };
107 uint8_t buf[IB_SA_DATA_SIZE] = { 0 };
115 if (!IB_LID_VALID(sm_id->lid)) {
116 if (ib_resolve_smlid_via(sm_id, timeout, srcport) < 0)
120 if (umad_get_port(srcport->ca_name, srcport->portnum, &port))
123 mad_set_field64(selfgid, 0, IB_GID_PREFIX_F, ntohll(port.gid_prefix));
124 mad_set_field64(selfgid, 0, IB_GID_GUID_F, ntohll(port.port_guid));
125 umad_release_port(&port);
127 memcpy(&prefix, portid->gid, sizeof(prefix));
129 mad_set_field64(portid->gid, 0, IB_GID_PREFIX_F,
130 IB_DEFAULT_SUBN_PREFIX);
132 mad_set_field64(portid->gid, 0, IB_GID_GUID_F, *guid);
135 ib_path_query_via(srcport, selfgid, portid->gid, sm_id, buf)) < 0)
138 mad_decode_field(buf, IB_SA_PR_SL_F, &portid->sl);
142 int ib_resolve_portid_str_via(ib_portid_t * portid, char *addr_str,
143 enum MAD_DEST dest_type, ib_portid_t * sm_id,
144 const struct ibmad_port *srcport)
150 ib_portid_t selfportid = { 0 };
153 memset(portid, 0, sizeof *portid);
157 lid = strtol(addr_str, 0, 0);
158 if (!IB_LID_VALID(lid)) {
162 return ib_portid_set(portid, lid, 0, 0);
165 if (str2drpath(&portid->drpath, addr_str, 0, 0) < 0) {
172 if (!(guid = strtoull(addr_str, 0, 0))) {
177 /* keep guid in portid? */
178 return ib_resolve_guid_via(portid, &guid, sm_id, 0, srcport);
181 lid = strtol(addr_str, &routepath, 0);
183 if (!IB_LID_VALID(lid)) {
187 ib_portid_set(portid, lid, 0, 0);
189 /* handle DR parsing and set DrSLID to local lid */
190 if (ib_resolve_self_via(&selfportid, &selfport, 0, srcport) < 0)
192 if (str2drpath(&portid->drpath, routepath, selfportid.lid, 0) <
200 if (inet_pton(AF_INET6, addr_str, &gid) <= 0)
202 return ib_resolve_gid_via(portid, gid, sm_id, 0, srcport);
204 IBWARN("bad dest_type %d", dest_type);
211 int ib_resolve_portid_str(ib_portid_t * portid, char *addr_str,
212 enum MAD_DEST dest_type, ib_portid_t * sm_id)
214 return ib_resolve_portid_str_via(portid, addr_str, dest_type,
218 int ib_resolve_self_via(ib_portid_t * portid, int *portnum, ibmad_gid_t * gid,
219 const struct ibmad_port *srcport)
221 ib_portid_t self = { 0 };
222 uint8_t portinfo[64];
223 uint8_t nodeinfo[64];
224 uint64_t guid, prefix;
226 if (!smp_query_via(nodeinfo, &self, IB_ATTR_NODE_INFO, 0, 0, srcport))
229 if (!smp_query_via(portinfo, &self, IB_ATTR_PORT_INFO, 0, 0, srcport))
232 mad_decode_field(portinfo, IB_PORT_LID_F, &portid->lid);
233 mad_decode_field(portinfo, IB_PORT_SMSL_F, &portid->sl);
234 mad_decode_field(portinfo, IB_PORT_GID_PREFIX_F, &prefix);
235 mad_decode_field(nodeinfo, IB_NODE_PORT_GUID_F, &guid);
238 mad_decode_field(nodeinfo, IB_NODE_LOCAL_PORT_F, portnum);
240 mad_encode_field(*gid, IB_GID_PREFIX_F, &prefix);
241 mad_encode_field(*gid, IB_GID_GUID_F, &guid);
246 int ib_resolve_self(ib_portid_t * portid, int *portnum, ibmad_gid_t * gid)
248 return ib_resolve_self_via(portid, portnum, gid, ibmp);