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 <stdarg.h>
42#include <inttypes.h>
43#include <getopt.h>
44
45#include <infiniband/common.h>
46#include <infiniband/umad.h>
47#include <infiniband/mad.h>
48
49#include "ibdiag_common.h"
50
51static uint8_t sminfo[1024];
52
53char *argv0 = "sminfo";
54
55static void
56usage(void)
57{
58	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -s state -p prio -a activity -D(irect) -G(uid) -V(ersion) -C ca_name -P ca_port "
59			"-t(imeout) timeout_ms] <sm_lid|sm_dr_path> [modifier]\n",
60			argv0);
61	exit(-1);
62}
63
64int strdata, xdata=1, bindata;
65enum {
66	SMINFO_NOTACT,
67	SMINFO_DISCOVER,
68	SMINFO_STANDBY,
69	SMINFO_MASTER,
70
71	SMINFO_STATE_LAST,
72};
73
74char *statestr[] = {
75	[SMINFO_NOTACT] "SMINFO_NOTACT",
76	[SMINFO_DISCOVER] "SMINFO_DISCOVER",
77	[SMINFO_STANDBY] "SMINFO_STANDBY",
78	[SMINFO_MASTER] "SMINFO_MASTER",
79};
80
81#define STATESTR(s)	(((unsigned)(s)) < SMINFO_STATE_LAST ? statestr[s] : "???")
82
83int
84main(int argc, char **argv)
85{
86	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
87	int mod = 0;
88	ib_portid_t portid = {0};
89	int timeout = 0;	/* use default */
90	uint8_t *p;
91	unsigned act = 0;
92	int prio = 0, state = SMINFO_STANDBY;
93	uint64_t guid = 0, key = 0;
94	extern int ibdebug;
95	int dest_type = IB_DEST_LID;
96	int udebug = 0;
97	char *ca = 0;
98	int ca_port = 0;
99
100	static char const str_opts[] = "C:P:t:s:p:a:deDGVhu";
101	static const struct option long_opts[] = {
102		{ "C", 1, 0, 'C'},
103		{ "P", 1, 0, 'P'},
104		{ "debug", 0, 0, 'd'},
105		{ "err_show", 0, 0, 'e'},
106		{ "s", 1, 0, 's'},
107		{ "p", 1, 0, 'p'},
108		{ "a", 1, 0, 'a'},
109		{ "Direct", 0, 0, 'D'},
110		{ "Guid", 0, 0, 'G'},
111		{ "Version", 0, 0, 'V'},
112		{ "timeout", 1, 0, 't'},
113		{ "help", 0, 0, 'h'},
114		{ "usage", 0, 0, 'u'},
115		{ }
116	};
117
118	argv0 = argv[0];
119
120	while (1) {
121		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
122		if ( ch == -1 )
123			break;
124		switch(ch) {
125		case 'C':
126			ca = optarg;
127			break;
128		case 'P':
129			ca_port = strtoul(optarg, 0, 0);
130			break;
131		case 'd':
132			ibdebug++;
133			madrpc_show_errors(1);
134			umad_debug(udebug);
135			udebug++;
136			break;
137		case 'e':
138			madrpc_show_errors(1);
139			break;
140		case 'D':
141			dest_type = IB_DEST_DRPATH;
142			break;
143		case 'G':
144			dest_type = IB_DEST_GUID;
145			break;
146		case 't':
147			timeout = strtoul(optarg, 0, 0);
148			madrpc_set_timeout(timeout);
149			break;
150		case 'a':
151			act = strtoul(optarg, 0, 0);
152			break;
153		case 's':
154			state = strtoul(optarg, 0, 0);
155			break;
156		case 'p':
157			prio = strtoul(optarg, 0, 0);
158			break;
159		case 'V':
160			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
161			exit(-1);
162		default:
163			usage();
164			break;
165		}
166	}
167	argc -= optind;
168	argv += optind;
169
170	if (argc > 1)
171		mod = atoi(argv[1]);
172
173	madrpc_init(ca, ca_port, mgmt_classes, 3);
174
175	if (argc) {
176		if (ib_resolve_portid_str(&portid, argv[0], dest_type, 0) < 0)
177			IBERROR("can't resolve destination port %s", argv[0]);
178	} else {
179		if (ib_resolve_smlid(&portid, timeout) < 0)
180			IBERROR("can't resolve sm port %s", argv[0]);
181	}
182
183	mad_encode_field(sminfo, IB_SMINFO_GUID_F, &guid);
184	mad_encode_field(sminfo, IB_SMINFO_ACT_F, &act);
185	mad_encode_field(sminfo, IB_SMINFO_KEY_F, &key);
186	mad_encode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
187	mad_encode_field(sminfo, IB_SMINFO_STATE_F, &state);
188
189	if (mod) {
190		if (!(p = smp_set(sminfo, &portid, IB_ATTR_SMINFO, mod, timeout)))
191			IBERROR("query");
192	} else
193		if (!(p = smp_query(sminfo, &portid, IB_ATTR_SMINFO, 0, timeout)))
194			IBERROR("query");
195
196	mad_decode_field(sminfo, IB_SMINFO_GUID_F, &guid);
197	mad_decode_field(sminfo, IB_SMINFO_ACT_F, &act);
198	mad_decode_field(sminfo, IB_SMINFO_KEY_F, &key);
199	mad_decode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
200	mad_decode_field(sminfo, IB_SMINFO_STATE_F, &state);
201
202	printf("sminfo: sm lid %d sm guid 0x%" PRIx64 ", activity count %u priority %d state %d %s\n",
203		portid.lid, guid, act, prio, state, STATESTR(state));
204
205	exit(0);
206}
207