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
|
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/*
* dev.c RDMA tool
* Authors: Leon Romanovsky <leonro@mellanox.com>
*/
#include <fcntl.h>
#include "rdma.h"
static int dev_help(struct rd *rd)
{
pr_out("Usage: %s dev show [DEV]\n", rd->filename);
pr_out(" %s dev set [DEV] name DEVNAME\n", rd->filename);
pr_out(" %s dev set [DEV] netns NSNAME\n", rd->filename);
pr_out(" %s dev set [DEV] adaptive-moderation [on|off]\n", rd->filename);
return 0;
}
static const char *dev_caps_to_str(uint32_t idx)
{
#define RDMA_DEV_FLAGS_LOW(x) \
x(RESIZE_MAX_WR, 0) \
x(BAD_PKEY_CNTR, 1) \
x(BAD_QKEY_CNTR, 2) \
x(RAW_MULTI, 3) \
x(AUTO_PATH_MIG, 4) \
x(CHANGE_PHY_PORT, 5) \
x(UD_AV_PORT_ENFORCE_PORT_ENFORCE, 6) \
x(CURR_QP_STATE_MOD, 7) \
x(SHUTDOWN_PORT, 8) \
x(INIT_TYPE, 9) \
x(PORT_ACTIVE_EVENT, 10) \
x(SYS_IMAGE_GUID, 11) \
x(RC_RNR_NAK_GEN, 12) \
x(SRQ_RESIZE, 13) \
x(N_NOTIFY_CQ, 14) \
x(LOCAL_DMA_LKEY, 15) \
x(MEM_WINDOW, 17) \
x(UD_IP_CSUM, 18) \
x(UD_TSO, 19) \
x(XRC, 20) \
x(MEM_MGT_EXTENSIONS, 21) \
x(BLOCK_MULTICAST_LOOPBACK, 22) \
x(MEM_WINDOW_TYPE_2A, 23) \
x(MEM_WINDOW_TYPE_2B, 24) \
x(RC_IP_CSUM, 25) \
x(RAW_IP_CSUM, 26) \
x(CROSS_CHANNEL, 27) \
x(MANAGED_FLOW_STEERING, 29) \
x(SIGNATURE_HANDOVER, 30) \
x(ON_DEMAND_PAGING, 31)
#define RDMA_DEV_FLAGS_HIGH(x) \
x(SG_GAPS_REG, 0) \
x(VIRTUAL_FUNCTION, 1) \
x(RAW_SCATTER_FCS, 2) \
x(RDMA_NETDEV_OPA_VNIC, 3) \
x(PCI_WRITE_END_PADDING, 4)
/*
* 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_DEV_FLAGS_LOW(RDMA_BITMAP_ENUM) };
enum { RDMA_DEV_FLAGS_HIGH(RDMA_BITMAP_ENUM) };
static const char * const
rdma_dev_names_low[] = { RDMA_DEV_FLAGS_LOW(RDMA_BITMAP_NAMES) };
static const char * const
rdma_dev_names_high[] = { RDMA_DEV_FLAGS_HIGH(RDMA_BITMAP_NAMES) };
uint32_t high_idx;
#undef RDMA_DEV_FLAGS_LOW
#undef RDMA_DEV_FLAGS_HIGH
if (idx < ARRAY_SIZE(rdma_dev_names_low) && rdma_dev_names_low[idx])
return rdma_dev_names_low[idx];
high_idx = idx - ARRAY_SIZE(rdma_dev_names_low);
if (high_idx < ARRAY_SIZE(rdma_dev_names_high) &&
rdma_dev_names_high[high_idx])
return rdma_dev_names_high[high_idx];
return "UNKNOWN";
}
static void dev_print_caps(struct rd *rd, 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_color_string(PRINT_FP, COLOR_NONE, NULL, "\n caps: <", NULL);
open_json_array(PRINT_JSON, "caps");
for (idx = 0; caps; idx++) {
if (caps & 0x1)
print_color_string(PRINT_ANY, COLOR_NONE, NULL,
caps >> 0x1 ? "%s, " : "%s",
dev_caps_to_str(idx));
caps >>= 0x1;
}
close_json_array(PRINT_ANY, ">");
}
static void dev_print_fw(struct rd *rd, struct nlattr **tb)
{
const char *str;
if (!tb[RDMA_NLDEV_ATTR_FW_VERSION])
return;
str = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_FW_VERSION]);
print_color_string(PRINT_ANY, COLOR_NONE, "fw", "fw %s ", str);
}
static void dev_print_node_guid(struct rd *rd, struct nlattr **tb)
{
uint64_t node_guid;
uint16_t vp[4];
char str[32];
if (!tb[RDMA_NLDEV_ATTR_NODE_GUID])
return;
node_guid = mnl_attr_get_u64(tb[RDMA_NLDEV_ATTR_NODE_GUID]);
memcpy(vp, &node_guid, sizeof(uint64_t));
snprintf(str, 32, "%04x:%04x:%04x:%04x", vp[3], vp[2], vp[1], vp[0]);
print_color_string(PRINT_ANY, COLOR_NONE, "node_guid", "node_guid %s ",
str);
}
static void dev_print_sys_image_guid(struct rd *rd, struct nlattr **tb)
{
uint64_t sys_image_guid;
uint16_t vp[4];
char str[32];
if (!tb[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID])
return;
sys_image_guid = mnl_attr_get_u64(tb[RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]);
memcpy(vp, &sys_image_guid, sizeof(uint64_t));
snprintf(str, 32, "%04x:%04x:%04x:%04x", vp[3], vp[2], vp[1], vp[0]);
print_color_string(PRINT_ANY, COLOR_NONE, "sys_image_guid",
"sys_image_guid %s ", str);
}
static void dev_print_dim_setting(struct rd *rd, struct nlattr **tb)
{
uint8_t dim_setting;
if (!tb[RDMA_NLDEV_ATTR_DEV_DIM])
return;
dim_setting = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_DEV_DIM]);
if (dim_setting > 1)
return;
print_on_off(PRINT_ANY, "adaptive-moderation", "adaptive-moderation %s ", dim_setting);
}
static const char *node_type_to_str(uint8_t node_type)
{
static const char * const node_type_str[] = { "unknown", "ca",
"switch", "router",
"rnic", "usnic",
"usnic_udp",
"unspecified" };
if (node_type < ARRAY_SIZE(node_type_str))
return node_type_str[node_type];
return "unknown";
}
static void dev_print_node_type(struct rd *rd, struct nlattr **tb)
{
const char *node_str;
uint8_t node_type;
if (!tb[RDMA_NLDEV_ATTR_DEV_NODE_TYPE])
return;
node_type = mnl_attr_get_u8(tb[RDMA_NLDEV_ATTR_DEV_NODE_TYPE]);
node_str = node_type_to_str(node_type);
print_color_string(PRINT_ANY, COLOR_NONE, "node_type", "node_type %s ",
node_str);
}
static int dev_parse_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
struct rd *rd = data;
const char *name;
uint32_t idx;
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;
open_json_object(NULL);
idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
print_color_uint(PRINT_ANY, COLOR_NONE, "ifindex", "%u: ", idx);
print_color_string(PRINT_ANY, COLOR_NONE, "ifname", "%s: ", name);
dev_print_node_type(rd, tb);
dev_print_fw(rd, tb);
dev_print_node_guid(rd, tb);
dev_print_sys_image_guid(rd, tb);
if (rd->show_details) {
dev_print_dim_setting(rd, tb);
dev_print_caps(rd, tb);
}
newline(rd);
return MNL_CB_OK;
}
static int dev_no_args(struct rd *rd)
{
uint32_t seq;
int ret;
rd_prepare_msg(rd, RDMA_NLDEV_CMD_GET,
&seq, (NLM_F_REQUEST | NLM_F_ACK));
mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
ret = rd_send_msg(rd);
if (ret)
return ret;
ret = rd_recv_msg(rd, dev_parse_cb, rd, seq);
return ret;
}
static int dev_one_show(struct rd *rd)
{
const struct rd_cmd cmds[] = {
{ NULL, dev_no_args},
{ 0 }
};
return rd_exec_cmd(rd, cmds, "parameter");
}
static int dev_set_name(struct rd *rd)
{
uint32_t seq;
if (rd_no_arg(rd)) {
pr_err("Please provide device new name.\n");
return -EINVAL;
}
rd_prepare_msg(rd, RDMA_NLDEV_CMD_SET,
&seq, (NLM_F_REQUEST | NLM_F_ACK));
mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
mnl_attr_put_strz(rd->nlh, RDMA_NLDEV_ATTR_DEV_NAME, rd_argv(rd));
return rd_sendrecv_msg(rd, seq);
}
static int dev_set_netns(struct rd *rd)
{
char *netns_path;
uint32_t seq;
int netns;
int ret;
if (rd_no_arg(rd)) {
pr_err("Please provide device name.\n");
return -EINVAL;
}
if (asprintf(&netns_path, "%s/%s", NETNS_RUN_DIR, rd_argv(rd)) < 0)
return -ENOMEM;
netns = open(netns_path, O_RDONLY | O_CLOEXEC);
if (netns < 0) {
fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
rd_argv(rd), strerror(errno));
ret = -EINVAL;
goto done;
}
rd_prepare_msg(rd, RDMA_NLDEV_CMD_SET,
&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_NET_NS_FD, netns);
ret = rd_sendrecv_msg(rd, seq);
close(netns);
done:
free(netns_path);
return ret;
}
static int dev_set_dim_sendmsg(struct rd *rd, uint8_t dim_setting)
{
uint32_t seq;
rd_prepare_msg(rd, RDMA_NLDEV_CMD_SET, &seq,
(NLM_F_REQUEST | NLM_F_ACK));
mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
mnl_attr_put_u8(rd->nlh, RDMA_NLDEV_ATTR_DEV_DIM, dim_setting);
return rd_sendrecv_msg(rd, seq);
}
static int dev_set_dim_off(struct rd *rd)
{
return dev_set_dim_sendmsg(rd, 0);
}
static int dev_set_dim_on(struct rd *rd)
{
return dev_set_dim_sendmsg(rd, 1);
}
static int dev_set_dim(struct rd *rd)
{
const struct rd_cmd cmds[] = {
{ NULL, dev_help},
{ "on", dev_set_dim_on},
{ "off", dev_set_dim_off},
{ 0 }
};
return rd_exec_cmd(rd, cmds, "parameter");
}
static int dev_one_set(struct rd *rd)
{
const struct rd_cmd cmds[] = {
{ NULL, dev_help},
{ "name", dev_set_name},
{ "netns", dev_set_netns},
{ "adaptive-moderation", dev_set_dim},
{ 0 }
};
return rd_exec_cmd(rd, cmds, "parameter");
}
static int dev_show(struct rd *rd)
{
return rd_exec_dev(rd, dev_one_show);
}
static int dev_set(struct rd *rd)
{
return rd_exec_require_dev(rd, dev_one_set);
}
int cmd_dev(struct rd *rd)
{
const struct rd_cmd cmds[] = {
{ NULL, dev_show },
{ "show", dev_show },
{ "list", dev_show },
{ "set", dev_set },
{ "help", dev_help },
{ 0 }
};
return rd_exec_cmd(rd, cmds, "dev command");
}
|