xref: /freebsd/contrib/ofed/libibmad/portid.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*
2  * Copyright (c) 2004-2009 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 <string.h>
41 #include <arpa/inet.h>
42 
43 #include <infiniband/mad.h>
44 
45 #undef DEBUG
46 #define DEBUG	if (ibdebug)	IBWARN
47 
48 int portid2portnum(ib_portid_t * portid)
49 {
50 	if (portid->lid > 0)
51 		return -1;
52 
53 	if (portid->drpath.cnt == 0)
54 		return 0;
55 
56 	return portid->drpath.p[(portid->drpath.cnt - 1)];
57 }
58 
59 char *portid2str(ib_portid_t * portid)
60 {
61 	static char buf[1024] = "local";
62 	int n = 0;
63 
64 	if (portid->lid > 0) {
65 		n += sprintf(buf + n, "Lid %d", portid->lid);
66 		if (portid->grh_present) {
67 			char gid[sizeof
68 				 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
69 			if (inet_ntop(AF_INET6, portid->gid, gid, sizeof(gid)))
70 				n += sprintf(buf + n, " Gid %s", gid);
71 		}
72 		if (portid->drpath.cnt)
73 			n += sprintf(buf + n, " ");
74 		else
75 			return buf;
76 	}
77 	n += sprintf(buf + n, "DR path ");
78 	drpath2str(&(portid->drpath), buf + n, sizeof(buf) - n);
79 
80 	return buf;
81 }
82 
83 int str2drpath(ib_dr_path_t * path, char *routepath, int drslid, int drdlid)
84 {
85 	char *s, *str;
86 	char *tmp;
87 
88 	path->cnt = -1;
89 
90 	if (!routepath || !(tmp = strdup(routepath)))
91 		goto Exit;
92 
93 	DEBUG("DR str: %s", routepath);
94 
95 	str = tmp;
96 
97 	while (str && *str) {
98 		if ((s = strchr(str, ',')))
99 			*s = 0;
100 		path->p[++path->cnt] = (uint8_t) atoi(str);
101 		if (!s)
102 			break;
103 		str = s + 1;
104 	}
105 	free(tmp);
106 
107 Exit:
108 	path->drdlid = drdlid ? drdlid : 0xffff;
109 	path->drslid = drslid ? drslid : 0xffff;
110 
111 	return path->cnt;
112 }
113 
114 char *drpath2str(ib_dr_path_t * path, char *dstr, size_t dstr_size)
115 {
116 	int i = 0;
117 	int rc = snprintf(dstr, dstr_size, "slid %u; dlid %u; %d",
118 			  path->drslid, path->drdlid, path->p[0]);
119 	if (rc >= (int)dstr_size)
120 		return dstr;
121 	for (i = 1; i <= path->cnt; i++) {
122 		rc += snprintf(dstr + rc, dstr_size - rc, ",%d", path->p[i]);
123 		if (rc >= (int)dstr_size)
124 			break;
125 	}
126 	return (dstr);
127 }
128