1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff *
32219820Sjeff */
33219820Sjeff
34219820Sjeff#if HAVE_CONFIG_H
35219820Sjeff#  include <config.h>
36219820Sjeff#endif /* HAVE_CONFIG_H */
37219820Sjeff
38219820Sjeff#include <stdio.h>
39219820Sjeff#include <stdlib.h>
40219820Sjeff#include <unistd.h>
41219820Sjeff#include <pthread.h>
42219820Sjeff#include <sys/time.h>
43219820Sjeff#include <string.h>
44219820Sjeff#include <inttypes.h>
45219820Sjeff#include <arpa/inet.h>
46219820Sjeff
47219820Sjeff#include <mad.h>
48219820Sjeff#include <infiniband/common.h>
49219820Sjeff
50219820Sjeff#undef DEBUG
51219820Sjeff#define DEBUG	if (ibdebug)	IBWARN
52219820Sjeff
53219820Sjeffint
54219820Sjeffportid2portnum(ib_portid_t *portid)
55219820Sjeff{
56219820Sjeff	if (portid->lid > 0)
57219820Sjeff		return -1;
58219820Sjeff
59219820Sjeff	if (portid->drpath.cnt == 0)
60219820Sjeff		return 0;
61219820Sjeff
62219820Sjeff	return portid->drpath.p[(portid->drpath.cnt-1)];
63219820Sjeff}
64219820Sjeff
65219820Sjeffchar *
66219820Sjeffportid2str(ib_portid_t *portid)
67219820Sjeff{
68219820Sjeff	static char buf[1024] = "local";
69219820Sjeff	int n = 0;
70219820Sjeff
71219820Sjeff	if (portid->lid > 0) {
72219820Sjeff		n += sprintf(buf + n, "Lid %d", portid->lid);
73219820Sjeff		if (portid->grh_present) {
74219820Sjeff			char gid[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
75219820Sjeff			if (inet_ntop(AF_INET6, portid->gid, gid, sizeof(gid)))
76219820Sjeff				n += sprintf(buf + n, " Gid %s", gid);
77219820Sjeff		}
78219820Sjeff		if (portid->drpath.cnt)
79219820Sjeff			n += sprintf(buf + n, " ");
80219820Sjeff		else
81219820Sjeff			return buf;
82219820Sjeff	}
83219820Sjeff	n += sprintf(buf + n, "DR path ");
84219820Sjeff	drpath2str(&(portid->drpath), buf + n, sizeof(buf) - n);
85219820Sjeff
86219820Sjeff	return buf;
87219820Sjeff}
88219820Sjeff
89219820Sjeffint
90219820Sjeffstr2drpath(ib_dr_path_t *path, char *routepath, int drslid, int drdlid)
91219820Sjeff{
92219820Sjeff	char *s, *str = routepath;
93219820Sjeff
94219820Sjeff	path->cnt = -1;
95219820Sjeff
96219820Sjeff	DEBUG("DR str: %s", routepath);
97219820Sjeff	while (str && *str) {
98219820Sjeff		if ((s = strchr(str, ',')))
99219820Sjeff			*s = 0;
100219820Sjeff		path->p[++path->cnt] = atoi(str);
101219820Sjeff		if (!s)
102219820Sjeff			break;
103219820Sjeff		str = s+1;
104219820Sjeff	}
105219820Sjeff
106219820Sjeff	path->drdlid = drdlid ? drdlid : 0xffff;
107219820Sjeff	path->drslid = drslid ? drslid : 0xffff;
108219820Sjeff
109219820Sjeff	return path->cnt;
110219820Sjeff}
111219820Sjeff
112219820Sjeffchar *
113219820Sjeffdrpath2str(ib_dr_path_t *path, char *dstr, size_t dstr_size)
114219820Sjeff{
115219820Sjeff	int i = 0;
116219820Sjeff	int rc = snprintf(dstr, dstr_size, "slid %d; dlid %d; %d",
117219820Sjeff		path->drslid, path->drdlid, path->p[0]);
118219820Sjeff	if (rc >= dstr_size)
119219820Sjeff		return dstr;
120219820Sjeff	for (i = 1; i <= path->cnt; i++) {
121219820Sjeff		rc += snprintf(dstr+rc, dstr_size-rc, ",%d", path->p[i]);
122219820Sjeff		if (rc >= dstr_size)
123219820Sjeff			break;
124219820Sjeff	}
125219820Sjeff	return (dstr);
126219820Sjeff}
127