]> git.proxmox.com Git - mirror_iproute2.git/blame - rdma/res.c
rdma: initialize the rd struct
[mirror_iproute2.git] / rdma / res.c
CommitLineData
923aa825
LR
1/*
2 * res.c RDMA tool
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Leon Romanovsky <leonro@mellanox.com>
10 */
11
12#include "rdma.h"
13#include <inttypes.h>
14
15static int res_help(struct rd *rd)
16{
17 pr_out("Usage: %s resource\n", rd->filename);
18 pr_out(" resource show [DEV]\n");
8ecac46a
LR
19 pr_out(" resource show [qp]\n");
20 pr_out(" resource show qp link [DEV/PORT]\n");
21 pr_out(" resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
923aa825
LR
22 return 0;
23}
24
25static int res_print_summary(struct rd *rd, struct nlattr **tb)
26{
27 struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY];
28 struct nlattr *nla_entry;
29 const char *name;
30 uint64_t curr;
31 int err;
32
33 mnl_attr_for_each_nested(nla_entry, nla_table) {
34 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
35 char json_name[32];
36
37 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
38 if (err != MNL_CB_OK)
39 return -EINVAL;
40
41 if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] ||
42 !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) {
43 return -EINVAL;
44 }
45
46 name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
47 curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
48 if (rd->json_output) {
49 snprintf(json_name, 32, "%s", name);
50 jsonw_lluint_field(rd->jw, json_name, curr);
51 } else {
52 pr_out("%s %"PRId64 " ", name, curr);
53 }
54 }
55 return 0;
56}
57
58static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
59{
60 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
61 struct rd *rd = data;
62 const char *name;
63 uint32_t idx;
64
65 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
66 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
67 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
68 !tb[RDMA_NLDEV_ATTR_RES_SUMMARY])
69 return MNL_CB_ERROR;
70
71 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
72 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
73 if (rd->json_output) {
74 jsonw_uint_field(rd->jw, "ifindex", idx);
75 jsonw_string_field(rd->jw, "ifname", name);
76 } else {
77 pr_out("%u: %s: ", idx, name);
78 }
79
80 res_print_summary(rd, tb);
81
82 if (!rd->json_output)
83 pr_out("\n");
84 return MNL_CB_OK;
85}
86
87static int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
88{
89 uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
90 uint32_t seq;
91 int ret;
92
93 if (command != RDMA_NLDEV_CMD_RES_GET)
94 flags |= NLM_F_DUMP;
95
96 rd_prepare_msg(rd, command, &seq, flags);
97 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
98 if (rd->port_idx)
99 mnl_attr_put_u32(rd->nlh,
100 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
101
102 ret = rd_send_msg(rd);
103 if (ret)
104 return ret;
105
106 if (rd->json_output)
107 jsonw_start_object(rd->jw);
108 ret = rd_recv_msg(rd, callback, rd, seq);
109 if (rd->json_output)
110 jsonw_end_object(rd->jw);
111 return ret;
112}
113
114#define RES_FUNC(name, command, valid_filters, strict_port) \
115 static int _##name(struct rd *rd)\
116 { \
117 return _res_send_msg(rd, command, name##_parse_cb); \
118 } \
119 static int name(struct rd *rd) \
120 {\
121 int ret = rd_build_filter(rd, valid_filters); \
122 if (ret) \
123 return ret; \
124 if ((uintptr_t)valid_filters != (uintptr_t)NULL) { \
125 ret = rd_set_arg_to_devname(rd); \
126 if (ret) \
127 return ret;\
128 } \
129 if (strict_port) \
130 return rd_exec_dev(rd, _##name); \
131 else \
132 return rd_exec_link(rd, _##name, strict_port); \
133 }
134
8ecac46a
LR
135static const char *path_mig_to_str(uint8_t idx)
136{
137 static const char * const path_mig_str[] = { "MIGRATED",
138 "REARM", "ARMED" };
139
140 if (idx < ARRAY_SIZE(path_mig_str))
141 return path_mig_str[idx];
142 return "UNKNOWN";
143}
144
145static const char *qp_states_to_str(uint8_t idx)
146{
147 static const char * const qp_states_str[] = { "RESET", "INIT",
148 "RTR", "RTS", "SQD",
149 "SQE", "ERR" };
150
151 if (idx < ARRAY_SIZE(qp_states_str))
152 return qp_states_str[idx];
153 return "UNKNOWN";
154}
155
156static const char *qp_types_to_str(uint8_t idx)
157{
158 static const char * const qp_types_str[] = { "SMI", "GSI", "RC",
159 "UC", "UD", "RAW_IPV6",
160 "RAW_ETHERTYPE",
161 "UNKNOWN", "RAW_PACKET",
162 "XRC_INI", "XRC_TGT" };
163
164 if (idx < ARRAY_SIZE(qp_types_str))
165 return qp_types_str[idx];
166 return "UNKNOWN";
167}
168
169static void print_lqpn(struct rd *rd, uint32_t val)
170{
171 if (rd->json_output)
172 jsonw_uint_field(rd->jw, "lqpn", val);
173 else
174 pr_out("lqpn %u ", val);
175}
176
177static void print_rqpn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
178{
179 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQPN])
180 return;
181
182 if (rd->json_output)
183 jsonw_uint_field(rd->jw, "rqpn", val);
184 else
185 pr_out("rqpn %u ", val);
186}
187
188static void print_type(struct rd *rd, uint32_t val)
189{
190 if (rd->json_output)
191 jsonw_string_field(rd->jw, "type",
192 qp_types_to_str(val));
193 else
194 pr_out("type %s ", qp_types_to_str(val));
195}
196
197static void print_state(struct rd *rd, uint32_t val)
198{
199 if (rd->json_output)
200 jsonw_string_field(rd->jw, "state",
201 qp_states_to_str(val));
202 else
203 pr_out("state %s ", qp_states_to_str(val));
204}
205
206static void print_rqpsn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
207{
208 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN])
209 return;
210
211 if (rd->json_output)
212 jsonw_uint_field(rd->jw, "rq-psn", val);
213 else
214 pr_out("rq-psn %u ", val);
215}
216
217static void print_sqpsn(struct rd *rd, uint32_t val)
218{
219 if (rd->json_output)
220 jsonw_uint_field(rd->jw, "sq-psn", val);
221 else
222 pr_out("sq-psn %u ", val);
223}
224
225static void print_pathmig(struct rd *rd, uint32_t val,
226 struct nlattr **nla_line)
227{
228 if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE])
229 return;
230
231 if (rd->json_output)
232 jsonw_string_field(rd->jw,
233 "path-mig-state",
234 path_mig_to_str(val));
235 else
236 pr_out("path-mig-state %s ", path_mig_to_str(val));
237}
238
239static void print_pid(struct rd *rd, uint32_t val)
240{
241 if (rd->json_output)
242 jsonw_uint_field(rd->jw, "pid", val);
243 else
244 pr_out("pid %u ", val);
245}
246
247static void print_comm(struct rd *rd, const char *str,
248 struct nlattr **nla_line)
249{
250 char tmp[18];
251
252 if (rd->json_output) {
253 /* Don't beatify output in JSON format */
254 jsonw_string_field(rd->jw, "comm", str);
255 return;
256 }
257
258 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
259 snprintf(tmp, sizeof(tmp), "%s", str);
260 else
261 snprintf(tmp, sizeof(tmp), "[%s]", str);
262
263 pr_out("comm %s ", tmp);
264}
265
266static void print_link(struct rd *rd, uint32_t idx, const char *name,
267 uint32_t port, struct nlattr **nla_line)
268{
269 if (rd->json_output) {
270 jsonw_uint_field(rd->jw, "ifindex", idx);
271
272 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
273 jsonw_uint_field(rd->jw, "port", port);
274
275 jsonw_string_field(rd->jw, "ifname", name);
276 } else {
277 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
278 pr_out("link %s/%u ", name, port);
279 else
280 pr_out("link %s/- ", name);
281 }
282}
283
284static char *get_task_name(uint32_t pid)
285{
286 char *comm;
287 FILE *f;
288
289 if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
290 return NULL;
291
292 f = fopen(comm, "r");
293 free(comm);
294 if (!f)
295 return NULL;
296
297 if (fscanf(f, "%ms\n", &comm) != 1)
298 comm = NULL;
299
300 fclose(f);
301
302 return comm;
303}
304
305static int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data)
306{
307 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
308 struct nlattr *nla_table, *nla_entry;
309 struct rd *rd = data;
310 const char *name;
311 uint32_t idx;
312
313 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
314 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
315 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
316 !tb[RDMA_NLDEV_ATTR_RES_QP])
317 return MNL_CB_ERROR;
318
319 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
320 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
321 nla_table = tb[RDMA_NLDEV_ATTR_RES_QP];
322
323 mnl_attr_for_each_nested(nla_entry, nla_table) {
324 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
325 uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn;
326 uint8_t type, state, path_mig_state = 0;
327 uint32_t port = 0, pid = 0;
328 char *comm = NULL;
329 int err;
330
331 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
332 if (err != MNL_CB_OK)
333 return MNL_CB_ERROR;
334
335 if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] ||
336 !nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] ||
337 !nla_line[RDMA_NLDEV_ATTR_RES_TYPE] ||
338 !nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
339 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
340 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
341 return MNL_CB_ERROR;
342 }
343
344 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
345 port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
346
347 if (port != rd->port_idx)
348 continue;
349
350 lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
351 if (rd_check_is_filtered(rd, "lqpn", lqpn))
352 continue;
353
354 if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) {
355 rqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQPN]);
356 if (rd_check_is_filtered(rd, "rqpn", rqpn))
357 continue;
358 } else {
359 if (rd_check_is_key_exist(rd, "rqpn"))
360 continue;
361 }
362
363 if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) {
364 rq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]);
365 if (rd_check_is_filtered(rd, "rq-psn", rq_psn))
366 continue;
367 } else {
368 if (rd_check_is_key_exist(rd, "rq-psn"))
369 continue;
370 }
371
372 sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]);
373 if (rd_check_is_filtered(rd, "sq-psn", sq_psn))
374 continue;
375
376 if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) {
377 path_mig_state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]);
378 if (rd_check_is_string_filtered(rd, "path-mig-state", path_mig_to_str(path_mig_state)))
379 continue;
380 } else {
381 if (rd_check_is_key_exist(rd, "path-mig-state"))
382 continue;
383 }
384
385 type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
386 if (rd_check_is_string_filtered(rd, "type", qp_types_to_str(type)))
387 continue;
388
389 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
390 if (rd_check_is_string_filtered(rd, "state", qp_states_to_str(state)))
391 continue;
392
393 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
394 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
395 comm = get_task_name(pid);
396 }
397
4ac152d0
LR
398 if (rd_check_is_filtered(rd, "pid", pid)) {
399 free(comm);
8ecac46a 400 continue;
4ac152d0 401 }
8ecac46a
LR
402
403 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
404 /* discard const from mnl_attr_get_str */
405 comm = (char *)mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
406
407 if (rd->json_output)
408 jsonw_start_array(rd->jw);
409
410 print_link(rd, idx, name, port, nla_line);
411
412 print_lqpn(rd, lqpn);
413 print_rqpn(rd, rqpn, nla_line);
414
415 print_type(rd, type);
416 print_state(rd, state);
417
418 print_rqpsn(rd, rq_psn, nla_line);
419 print_sqpsn(rd, sq_psn);
420
421 print_pathmig(rd, path_mig_state, nla_line);
422 print_pid(rd, pid);
423 print_comm(rd, comm, nla_line);
424
425 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
426 free(comm);
427
428 if (rd->json_output)
429 jsonw_end_array(rd->jw);
430 else
431 pr_out("\n");
432 }
433 return MNL_CB_OK;
434}
435
923aa825
LR
436RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true);
437
8ecac46a
LR
438static const struct
439filters qp_valid_filters[MAX_NUMBER_OF_FILTERS] = {{ .name = "link",
440 .is_number = false },
441 { .name = "lqpn",
442 .is_number = true },
443 { .name = "rqpn",
444 .is_number = true },
445 { .name = "pid",
446 .is_number = true },
447 { .name = "sq-psn",
448 .is_number = true },
449 { .name = "rq-psn",
450 .is_number = true },
451 { .name = "type",
452 .is_number = false },
453 { .name = "path-mig-state",
454 .is_number = false },
455 { .name = "state",
456 .is_number = false } };
457
458RES_FUNC(res_qp, RDMA_NLDEV_CMD_RES_QP_GET, qp_valid_filters, false);
459
923aa825
LR
460static int res_show(struct rd *rd)
461{
462 const struct rd_cmd cmds[] = {
463 { NULL, res_no_args },
8ecac46a 464 { "qp", res_qp },
923aa825
LR
465 { 0 }
466 };
467
468 /*
469 * Special case to support "rdma res show DEV_NAME"
470 */
471 if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
472 return rd_exec_dev(rd, _res_no_args);
473
474 return rd_exec_cmd(rd, cmds, "parameter");
475}
476
477int cmd_res(struct rd *rd)
478{
479 const struct rd_cmd cmds[] = {
480 { NULL, res_show },
481 { "show", res_show },
482 { "list", res_show },
483 { "help", res_help },
484 { 0 }
485 };
486
487 return rd_exec_cmd(rd, cmds, "resource command");
488}