1/*
2 * Copyright (c) 2008 Lawrence Livermore National Security
3 *
4 * Produced at Lawrence Livermore National Laboratory.
5 * Written by Ira Weiny <weiny2@llnl.gov>.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses.  You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 *     Redistribution and use in source and binary forms, with or
14 *     without modification, are permitted provided that the following
15 *     conditions are met:
16 *
17 *      - Redistributions of source code must retain the above
18 *        copyright notice, this list of conditions and the following
19 *        disclaimer.
20 *
21 *      - Redistributions in binary form must reproduce the above
22 *        copyright notice, this list of conditions and the following
23 *        disclaimer in the documentation and/or other materials
24 *        provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 *
35 */
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <unistd.h>
40#include <string.h>
41
42#define _GNU_SOURCE
43#include <getopt.h>
44
45#include <infiniband/mad.h>
46#include <infiniband/iba/ib_types.h>
47
48#include "ibdiag_common.h"
49
50char *argv0 = "";
51
52static int send_144_node_desc_update(void)
53{
54	ib_portid_t sm_port;
55	ib_portid_t selfportid;
56	int selfport;
57	ib_rpc_t trap_rpc;
58	ib_mad_notice_attr_t notice;
59
60	if (ib_resolve_self(&selfportid, &selfport, NULL))
61		IBERROR("can't resolve self");
62
63	if (ib_resolve_smlid(&sm_port, 0))
64		IBERROR("can't resolve SM destination port");
65
66	memset(&trap_rpc, 0, sizeof(trap_rpc));
67	trap_rpc.mgtclass = IB_SMI_CLASS;
68	trap_rpc.method = IB_MAD_METHOD_TRAP;
69	trap_rpc.trid = mad_trid();
70	trap_rpc.attr.id = NOTICE;
71	trap_rpc.datasz = IB_SMP_DATA_SIZE;
72	trap_rpc.dataoffs = IB_SMP_DATA_OFFS;
73
74	memset(&notice, 0, sizeof(notice));
75	notice.generic_type = 0x80 | IB_NOTICE_TYPE_INFO;
76	notice.g_or_v.generic.prod_type_lsb = cl_hton16(IB_NODE_TYPE_CA);
77	notice.g_or_v.generic.trap_num = cl_hton16(144);
78	notice.issuer_lid = cl_hton16(selfportid.lid);
79	notice.data_details.ntc_144.lid = cl_hton16(selfportid.lid);
80	notice.data_details.ntc_144.local_changes =
81	    TRAP_144_MASK_OTHER_LOCAL_CHANGES;
82	notice.data_details.ntc_144.change_flgs =
83	    TRAP_144_MASK_NODE_DESCRIPTION_CHANGE;
84
85	return (mad_send(&trap_rpc, &sm_port, NULL, &notice));
86}
87
88typedef struct _trap_def {
89	char *trap_name;
90	int (*send_func) (void);
91} trap_def_t;
92
93trap_def_t traps[2] = {
94	{"node_desc_change", send_144_node_desc_update},
95	{NULL, NULL}
96};
97
98static void usage(void)
99{
100	int i;
101
102	fprintf(stderr, "Usage: %s [-hV]"
103		" [-C <ca_name>] [-P <ca_port>] [<trap_name>]\n", argv0);
104	fprintf(stderr, "   -V print version\n");
105	fprintf(stderr, "   <trap_name> can be one of the following\n");
106	for (i = 0; traps[i].trap_name; i++) {
107		fprintf(stderr, "      %s\n", traps[i].trap_name);
108	}
109	fprintf(stderr, "   default behavior is to send \"%s\"\n",
110		traps[0].trap_name);
111
112	exit(-1);
113}
114
115int send_trap(char *trap_name)
116{
117	int i;
118
119	for (i = 0; traps[i].trap_name; i++) {
120		if (strcmp(traps[i].trap_name, trap_name) == 0) {
121			return (traps[i].send_func());
122		}
123	}
124	usage();
125	exit(1);
126}
127
128int main(int argc, char **argv)
129{
130	int mgmt_classes[2] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS };
131	int ch = 0;
132	char *trap_name = NULL;
133	char *ca = NULL;
134	int ca_port = 0;
135
136	static char const str_opts[] = "hVP:C:";
137	static const struct option long_opts[] = {
138		{"Version", 0, 0, 'V'},
139		{"P", 1, 0, 'P'},
140		{"C", 1, 0, 'C'},
141		{"help", 0, 0, 'h'},
142		{}
143	};
144
145	argv0 = argv[0];
146
147	while ((ch = getopt_long(argc, argv, str_opts, long_opts, NULL)) != -1) {
148		switch (ch) {
149		case 'V':
150			fprintf(stderr, "%s %s\n", argv0, get_build_version());
151			exit(-1);
152		case 'C':
153			ca = optarg;
154			break;
155		case 'P':
156			ca_port = strtoul(optarg, NULL, 0);
157			break;
158		case 'h':
159		default:
160			usage();
161		}
162	}
163	argc -= optind;
164	argv += optind;
165
166	if (!argv[0]) {
167		trap_name = traps[0].trap_name;
168	} else {
169		trap_name = argv[0];
170	}
171
172	madrpc_show_errors(1);
173	madrpc_init(ca, ca_port, mgmt_classes, 2);
174
175	return (send_trap(trap_name));
176}
177