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