1321936Shselasky/*
2321936Shselasky * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3321936Shselasky *
4321936Shselasky * This software is available to you under a choice of one of two
5321936Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
6321936Shselasky * General Public License (GPL) Version 2, available from the file
7321936Shselasky * COPYING in the main directory of this source tree, or the
8321936Shselasky * OpenIB.org BSD license below:
9321936Shselasky *
10321936Shselasky *     Redistribution and use in source and binary forms, with or
11321936Shselasky *     without modification, are permitted provided that the following
12321936Shselasky *     conditions are met:
13321936Shselasky *
14321936Shselasky *      - Redistributions of source code must retain the above
15321936Shselasky *        copyright notice, this list of conditions and the following
16321936Shselasky *        disclaimer.
17321936Shselasky *
18321936Shselasky *      - Redistributions in binary form must reproduce the above
19321936Shselasky *        copyright notice, this list of conditions and the following
20321936Shselasky *        disclaimer in the documentation and/or other materials
21321936Shselasky *        provided with the distribution.
22321936Shselasky *
23321936Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24321936Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25321936Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26321936Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27321936Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28321936Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29321936Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30321936Shselasky * SOFTWARE.
31321936Shselasky *
32321936Shselasky */
33321936Shselasky
34321936Shselasky#if HAVE_CONFIG_H
35321936Shselasky#  include <config.h>
36321936Shselasky#endif				/* HAVE_CONFIG_H */
37321936Shselasky
38321936Shselasky#include <stdio.h>
39321936Shselasky#include <stdlib.h>
40321936Shselasky#include <string.h>
41321936Shselasky#include <arpa/inet.h>
42321936Shselasky
43321936Shselasky#include <infiniband/mad.h>
44321936Shselasky
45321936Shselasky#undef DEBUG
46321936Shselasky#define DEBUG	if (ibdebug)	IBWARN
47321936Shselasky
48321936Shselaskyint portid2portnum(ib_portid_t * portid)
49321936Shselasky{
50321936Shselasky	if (portid->lid > 0)
51321936Shselasky		return -1;
52321936Shselasky
53321936Shselasky	if (portid->drpath.cnt == 0)
54321936Shselasky		return 0;
55321936Shselasky
56321936Shselasky	return portid->drpath.p[(portid->drpath.cnt - 1)];
57321936Shselasky}
58321936Shselasky
59321936Shselaskychar *portid2str(ib_portid_t * portid)
60321936Shselasky{
61321936Shselasky	static char buf[1024] = "local";
62321936Shselasky	int n = 0;
63321936Shselasky
64321936Shselasky	if (portid->lid > 0) {
65321936Shselasky		n += sprintf(buf + n, "Lid %d", portid->lid);
66321936Shselasky		if (portid->grh_present) {
67321936Shselasky			char gid[sizeof
68321936Shselasky				 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
69321936Shselasky			if (inet_ntop(AF_INET6, portid->gid, gid, sizeof(gid)))
70321936Shselasky				n += sprintf(buf + n, " Gid %s", gid);
71321936Shselasky		}
72321936Shselasky		if (portid->drpath.cnt)
73321936Shselasky			n += sprintf(buf + n, " ");
74321936Shselasky		else
75321936Shselasky			return buf;
76321936Shselasky	}
77321936Shselasky	n += sprintf(buf + n, "DR path ");
78321936Shselasky	drpath2str(&(portid->drpath), buf + n, sizeof(buf) - n);
79321936Shselasky
80321936Shselasky	return buf;
81321936Shselasky}
82321936Shselasky
83321936Shselaskyint str2drpath(ib_dr_path_t * path, char *routepath, int drslid, int drdlid)
84321936Shselasky{
85321936Shselasky	char *s, *str;
86321936Shselasky	char *tmp;
87321936Shselasky
88321936Shselasky	path->cnt = -1;
89321936Shselasky
90321936Shselasky	if (!routepath || !(tmp = strdup(routepath)))
91321936Shselasky		goto Exit;
92321936Shselasky
93321936Shselasky	DEBUG("DR str: %s", routepath);
94321936Shselasky
95321936Shselasky	str = tmp;
96321936Shselasky
97321936Shselasky	while (str && *str) {
98321936Shselasky		if ((s = strchr(str, ',')))
99321936Shselasky			*s = 0;
100321936Shselasky		path->p[++path->cnt] = (uint8_t) atoi(str);
101321936Shselasky		if (!s)
102321936Shselasky			break;
103321936Shselasky		str = s + 1;
104321936Shselasky	}
105321936Shselasky	free(tmp);
106321936Shselasky
107321936ShselaskyExit:
108321936Shselasky	path->drdlid = drdlid ? drdlid : 0xffff;
109321936Shselasky	path->drslid = drslid ? drslid : 0xffff;
110321936Shselasky
111321936Shselasky	return path->cnt;
112321936Shselasky}
113321936Shselasky
114321936Shselaskychar *drpath2str(ib_dr_path_t * path, char *dstr, size_t dstr_size)
115321936Shselasky{
116321936Shselasky	int i = 0;
117321936Shselasky	int rc = snprintf(dstr, dstr_size, "slid %u; dlid %u; %d",
118321936Shselasky			  path->drslid, path->drdlid, path->p[0]);
119321936Shselasky	if (rc >= (int)dstr_size)
120321936Shselasky		return dstr;
121321936Shselasky	for (i = 1; i <= path->cnt; i++) {
122321936Shselasky		rc += snprintf(dstr + rc, dstr_size - rc, ",%d", path->p[i]);
123321936Shselasky		if (rc >= (int)dstr_size)
124321936Shselasky			break;
125321936Shselasky	}
126321936Shselasky	return (dstr);
127321936Shselasky}
128