summaryrefslogtreecommitdiffstats
path: root/rdma/link.c
blob: 853181733f00b06c77124f72def57b7dc74c1e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/*
 * link.c	RDMA tool
 * Authors:     Leon Romanovsky <leonro@mellanox.com>
 */

#include "rdma.h"

static int link_help(struct rd *rd)
{
	pr_out("Usage: %s link show [DEV/PORT_INDEX]\n", rd->filename);
	pr_out("Usage: %s link add NAME type TYPE netdev NETDEV\n",
	       rd->filename);
	pr_out("Usage: %s link delete NAME\n", rd->filename);
	return 0;
}

static const char *caps_to_str(uint32_t idx)
{
#define RDMA_PORT_FLAGS_LOW(x) \
	x(RESERVED, 0) \
	x(SM, 1) \
	x(NOTICE, 2) \
	x(TRAP, 3) \
	x(OPT_IPD, 4) \
	x(AUTO_MIGR, 5) \
	x(SL_MAP, 6) \
	x(MKEY_NVRAM, 7) \
	x(PKEY_NVRAM, 8) \
	x(LED_INFO, 9) \
	x(SM_DISABLED, 10) \
	x(SYS_IMAGE_GUID, 11) \
	x(PKEY_SW_EXT_PORT_TRAP, 12) \
	x(CABLE_INFO, 13) \
	x(EXTENDED_SPEEDS, 14) \
	x(CAP_MASK2, 15) \
	x(CM, 16) \
	x(SNMP_TUNNEL, 17) \
	x(REINIT, 18) \
	x(DEVICE_MGMT, 19) \
	x(VENDOR_CLASS, 20) \
	x(DR_NOTICE, 21) \
	x(CAP_MASK_NOTICE, 22) \
	x(BOOT_MGMT, 23) \
	x(LINK_LATENCY, 24) \
	x(CLIENT_REG, 25) \
	x(OTHER_LOCAL_CHANGES, 26) \
	x(LINK_SPPED_WIDTH, 27) \
	x(VENDOR_SPECIFIC_MADS, 28) \
	x(MULT_PKER_TRAP, 29) \
	x(MULT_FDB, 30) \
	x(HIERARCHY_INFO, 31)

#define RDMA_PORT_FLAGS_HIGH(x) \
	x(SET_NODE_DESC, 0) \
	x(EXT_INFO, 1) \
	x(VIRT, 2) \
	x(SWITCH_POR_STATE_TABLE, 3) \
	x(LINK_WIDTH_2X, 4) \
	x(LINK_SPEED_HDR, 5)

	/*
	 * Separation below is needed to allow compilation of rdmatool
	 * on 32bits systems. On such systems, C-enum is limited to be
	 * int and can't hold more than 32 bits.
	 */
	enum { RDMA_PORT_FLAGS_LOW(RDMA_BITMAP_ENUM) };
	enum { RDMA_PORT_FLAGS_HIGH(RDMA_BITMAP_ENUM) };

	static const char * const
		rdma_port_names_low[] = { RDMA_PORT_FLAGS_LOW(RDMA_BITMAP_NAMES) };
	static const char * const
		rdma_port_names_high[] = { RDMA_PORT_FLAGS_HIGH(RDMA_BITMAP_NAMES) };
	uint32_t high_idx;
	#undef RDMA_PORT_FLAGS_LOW
	#undef RDMA_PORT_FLAGS_HIGH

	if (idx < ARRAY_SIZE(rdma_port_names_low) && rdma_port_names_low[idx])
		return rdma_port_names_low[idx];

	high_idx = idx - ARRAY_SIZE(rdma_port_names_low);
	if (high_idx < ARRAY_SIZE(rdma_port_names_high) &&
	    rdma_port_names_high[high_idx])
		return rdma_port_names_high[high_idx];

	return "UNKNOWN";
}

static void link_print_caps(struct nlattr **tb)
{
	uint64_t caps;
	uint32_t idx;

	if (!tb[RDMA_NLDEV_ATTR_CAP_FLAGS])
		return;

	caps = mnl_attr_get_u64(tb[RDMA_NLDEV_ATTR_CAP_FLAGS]);

	print_string(PRINT_FP, NULL, "%s    caps: <", _SL_);
	open_json_array(PRINT_JSON, "caps");
	for (idx = 0; caps; idx++) {
		if (caps & 0x1)
			print_string(PRINT_ANY, NULL,
					   caps >> 0x1 ? "%s, " : "%s",
					   caps_to_str(idx));
		caps >>= 0x1;
	}
	close_json_array(PRINT_ANY, ">");
}

static void link_print_subnet_prefix(struct nlattr **tb)
{
	uint64_t subnet_prefix;
	uint16_t vp[4];
	char str[32];

	if (!tb[RDMA_NLDEV_ATTR_SUBNET_PREFIX])
		return;

	subnet_prefix = mnl_attr_get_u64(tb[RDMA_NLDEV_ATTR_SUBNET_PREFIX]);
	memcpy(vp, &subnet_prefix, sizeof(uint64_t));
	snprintf(str, 32, "%04x:%04x:%04x:%04x", vp[3], vp[2], vp[1], vp[0]);
	print_string(PRINT_ANY, "subnet_prefix", "subnet_prefix %s ", str);
}

static void link_print_lid(struct nlattr **tb)
{
	uint32_t lid;

	if (!tb[RDMA_NLDEV_ATTR_LID])
		return;

	lid = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_LID]);
	print_uint(PRINT_ANY, "lid", "lid %u ", lid);
}

static void link_print_sm_lid(struct nlattr **tb)
{
	uint32_t sm_lid;

	if (!tb[RDMA_NLDEV_ATTR_SM_LID])
		return;

	sm_lid = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_SM_LID]);
	print_uint(PRINT_ANY, "sm_lid", "sm_lid %u ", sm_lid);
}

static void link_print_lmc(struct nlattr **tb)
{
	uint8_t lmc;

	if (!tb[RDMA_NLDEV_ATTR_LMC])
		return;

	lmc = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_LMC]);
	print_uint(PRINT_ANY, "lmc", "lmc %u ", lmc);
}

static const char *link_state_to_str(uint8_t link_state)
{
	static const char * const link_state_str[] = { "NOP", "DOWN",
						       "INIT", "ARMED",
						       "ACTIVE",
						       "ACTIVE_DEFER" };
	if (link_state < ARRAY_SIZE(link_state_str))
		return link_state_str[link_state];
	return "UNKNOWN";
}

static void link_print_state(struct nlattr **tb)
{
	uint8_t state;

	if (!tb[RDMA_NLDEV_ATTR_PORT_STATE])
		return;

	state = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_PORT_STATE]);
	print_string(PRINT_ANY, "state", "state %s ", link_state_to_str(state));
}

static const char *phys_state_to_str(uint8_t phys_state)
{
	static const char * const phys_state_str[] = { "NOP", "SLEEP",
						       "POLLING", "DISABLED",
						       "ARMED", "LINK_UP",
						       "LINK_ERROR_RECOVER",
						       "PHY_TEST", "UNKNOWN",
						       "OPA_OFFLINE",
						       "UNKNOWN", "OPA_TEST" };
	if (phys_state < ARRAY_SIZE(phys_state_str))
		return phys_state_str[phys_state];
	return "UNKNOWN";
};

static void link_print_phys_state(struct nlattr **tb)
{
	uint8_t phys_state;

	if (!tb[RDMA_NLDEV_ATTR_PORT_PHYS_STATE])
		return;

	phys_state = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_PORT_PHYS_STATE]);
	print_string(PRINT_ANY, "physical_state", "physical_state %s ",
		     phys_state_to_str(phys_state));
}

static void link_print_netdev(const struct rd * rd, struct nlattr **tb)
{
	const char *netdev_name;
	uint32_t idx;

	if (!tb[RDMA_NLDEV_ATTR_NDEV_NAME] || !tb[RDMA_NLDEV_ATTR_NDEV_INDEX])
		return;

	netdev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_NDEV_NAME]);
	idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_NDEV_INDEX]);
	print_string(PRINT_ANY, "netdev", "netdev %s ", netdev_name);
	print_uint(PRINT_ANY, "netdev_index", rd->show_details ? "netdev_index %u " : "", idx);
}

static int link_parse_cb(const struct nlmsghdr *nlh, void *data)
{
	struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
	struct rd *rd = data;
	uint32_t port, idx;
	const char *name;

	mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
	if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
		return MNL_CB_ERROR;

	if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
		pr_err("This tool doesn't support switches yet\n");
		return MNL_CB_ERROR;
	}

	idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
	port = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
	name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);

	open_json_object(NULL);
	print_uint(PRINT_JSON, "ifindex", NULL, idx);
	print_string(PRINT_ANY, "ifname", "link %s/", name);
	print_uint(PRINT_ANY, "port", "%u ", port);
	link_print_subnet_prefix(tb);
	link_print_lid(tb);
	link_print_sm_lid(tb);
	link_print_lmc(tb);
	link_print_state(tb);
	link_print_phys_state(tb);
	link_print_netdev(rd, tb);
	if (rd->show_details)
		link_print_caps(tb);

	close_json_object();
	newline();

	return MNL_CB_OK;
}

static int link_no_args(struct rd *rd)
{
	uint32_t seq;
	int ret;

	rd_prepare_msg(rd, RDMA_NLDEV_CMD_PORT_GET, &seq,
		       (NLM_F_REQUEST | NLM_F_ACK));
	mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
	mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
	ret = rd_send_msg(rd);
	if (ret)
		return ret;

	ret = rd_recv_msg(rd, link_parse_cb, rd, seq);
	return ret;
}

static int link_one_show(struct rd *rd)
{
	const struct rd_cmd cmds[] = {
		{ NULL,		link_no_args},
		{ 0 }
	};

	if (!rd->port_idx)
		return 0;

	return rd_exec_cmd(rd, cmds, "parameter");
}

static int link_show(struct rd *rd)
{
	return rd_exec_link(rd, link_one_show, true);
}

static int link_add_netdev(struct rd *rd)
{
	char *link_netdev;
	uint32_t seq;

	if (rd_no_arg(rd)) {
		pr_err("Please provide a net device name.\n");
		return -EINVAL;
	}

	link_netdev = rd_argv(rd);
	rd_prepare_msg(rd, RDMA_NLDEV_CMD_NEWLINK, &seq,
		       (NLM_F_REQUEST | NLM_F_ACK));
	mnl_attr_put_strz(rd->nlh, RDMA_NLDEV_ATTR_DEV_NAME, rd->link_name);
	mnl_attr_put_strz(rd->nlh, RDMA_NLDEV_ATTR_LINK_TYPE, rd->link_type);
	mnl_attr_put_strz(rd->nlh, RDMA_NLDEV_ATTR_NDEV_NAME, link_netdev);
	return rd_sendrecv_msg(rd, seq);
}

static int link_add_type(struct rd *rd)
{
	const struct rd_cmd cmds[] = {
		{ NULL,		link_help},
		{ "netdev",	link_add_netdev},
		{ 0 }
	};

	if (rd_no_arg(rd)) {
		pr_err("Please provide a link type name.\n");
		return -EINVAL;
	}
	rd->link_type = rd_argv(rd);
	rd_arg_inc(rd);
	return rd_exec_cmd(rd, cmds, "parameter");
}

static int link_add(struct rd *rd)
{
	const struct rd_cmd cmds[] = {
		{ NULL,		link_help},
		{ "type",	link_add_type},
		{ 0 }
	};

	if (rd_no_arg(rd)) {
		pr_err("Please provide a link name to add.\n");
		return -EINVAL;
	}
	rd->link_name = rd_argv(rd);
	rd_arg_inc(rd);

	return rd_exec_cmd(rd, cmds, "parameter");
}

static int _link_del(struct rd *rd)
{
	uint32_t seq;

	if (!rd_no_arg(rd)) {
		pr_err("Unknown parameter %s\n", rd_argv(rd));
		return -EINVAL;
	}
	rd_prepare_msg(rd, RDMA_NLDEV_CMD_DELLINK, &seq,
		       (NLM_F_REQUEST | NLM_F_ACK));
	mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
	return rd_sendrecv_msg(rd, seq);
}

static int link_del(struct rd *rd)
{
	return rd_exec_require_dev(rd, _link_del);
}

int cmd_link(struct rd *rd)
{
	const struct rd_cmd cmds[] = {
		{ NULL,		link_show },
		{ "add",	link_add },
		{ "delete",	link_del },
		{ "show",	link_show },
		{ "list",	link_show },
		{ "help",	link_help },
		{ 0 }
	};

	return rd_exec_cmd(rd, cmds, "link command");
}