]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res.c
rdma: Provide unique indexes for all visible objects
[mirror_iproute2.git] / rdma / res.c
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
15 static int res_help(struct rd *rd)
16 {
17 pr_out("Usage: %s resource\n", rd->filename);
18 pr_out(" resource show [DEV]\n");
19 pr_out(" resource show [qp|cm_id|pd|mr|cq]\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");
22 pr_out(" resource show cm_id link [DEV/PORT]\n");
23 pr_out(" resource show cm_id link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
24 pr_out(" resource show cq link [DEV/PORT]\n");
25 pr_out(" resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
26 pr_out(" resource show pd dev [DEV]\n");
27 pr_out(" resource show pd dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
28 pr_out(" resource show mr dev [DEV]\n");
29 pr_out(" resource show mr dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
30 return 0;
31 }
32
33 static int res_print_summary(struct rd *rd, struct nlattr **tb)
34 {
35 struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY];
36 struct nlattr *nla_entry;
37 const char *name;
38 uint64_t curr;
39 int err;
40
41 mnl_attr_for_each_nested(nla_entry, nla_table) {
42 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
43 char json_name[32];
44
45 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
46 if (err != MNL_CB_OK)
47 return -EINVAL;
48
49 if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] ||
50 !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) {
51 return -EINVAL;
52 }
53
54 name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
55 curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
56 if (rd->json_output) {
57 snprintf(json_name, 32, "%s", name);
58 jsonw_lluint_field(rd->jw, json_name, curr);
59 } else {
60 pr_out("%s %"PRId64 " ", name, curr);
61 }
62 }
63 return 0;
64 }
65
66 static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
67 {
68 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
69 struct rd *rd = data;
70 const char *name;
71 uint32_t idx;
72
73 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
74 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
75 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
76 !tb[RDMA_NLDEV_ATTR_RES_SUMMARY])
77 return MNL_CB_ERROR;
78
79 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
80 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
81 if (rd->json_output) {
82 jsonw_uint_field(rd->jw, "ifindex", idx);
83 jsonw_string_field(rd->jw, "ifname", name);
84 } else {
85 pr_out("%u: %s: ", idx, name);
86 }
87
88 res_print_summary(rd, tb);
89
90 if (!rd->json_output)
91 pr_out("\n");
92 return MNL_CB_OK;
93 }
94
95 static int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
96 {
97 uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
98 uint32_t seq;
99 int ret;
100
101 if (command != RDMA_NLDEV_CMD_RES_GET)
102 flags |= NLM_F_DUMP;
103
104 rd_prepare_msg(rd, command, &seq, flags);
105 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
106 if (rd->port_idx)
107 mnl_attr_put_u32(rd->nlh,
108 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
109
110 ret = rd_send_msg(rd);
111 if (ret)
112 return ret;
113
114 if (rd->json_output)
115 jsonw_start_object(rd->jw);
116 ret = rd_recv_msg(rd, callback, rd, seq);
117 if (rd->json_output)
118 jsonw_end_object(rd->jw);
119 return ret;
120 }
121
122 #define RES_FUNC(name, command, valid_filters, strict_port) \
123 static int _##name(struct rd *rd)\
124 { \
125 return _res_send_msg(rd, command, name##_parse_cb); \
126 } \
127 static int name(struct rd *rd) \
128 {\
129 int ret = rd_build_filter(rd, valid_filters); \
130 if (ret) \
131 return ret; \
132 if ((uintptr_t)valid_filters != (uintptr_t)NULL) { \
133 ret = rd_set_arg_to_devname(rd); \
134 if (ret) \
135 return ret;\
136 } \
137 if (strict_port) \
138 return rd_exec_dev(rd, _##name); \
139 else \
140 return rd_exec_link(rd, _##name, strict_port); \
141 }
142
143 static const char *path_mig_to_str(uint8_t idx)
144 {
145 static const char * const path_mig_str[] = { "MIGRATED",
146 "REARM", "ARMED" };
147
148 if (idx < ARRAY_SIZE(path_mig_str))
149 return path_mig_str[idx];
150 return "UNKNOWN";
151 }
152
153 static const char *qp_states_to_str(uint8_t idx)
154 {
155 static const char * const qp_states_str[] = { "RESET", "INIT",
156 "RTR", "RTS", "SQD",
157 "SQE", "ERR" };
158
159 if (idx < ARRAY_SIZE(qp_states_str))
160 return qp_states_str[idx];
161 return "UNKNOWN";
162 }
163
164 static const char *qp_types_to_str(uint8_t idx)
165 {
166 static const char * const qp_types_str[] = { "SMI", "GSI", "RC",
167 "UC", "UD", "RAW_IPV6",
168 "RAW_ETHERTYPE",
169 "UNKNOWN", "RAW_PACKET",
170 "XRC_INI", "XRC_TGT" };
171
172 if (idx < ARRAY_SIZE(qp_types_str))
173 return qp_types_str[idx];
174 return "UNKNOWN";
175 }
176
177 static void print_lqpn(struct rd *rd, uint32_t val)
178 {
179 if (rd->json_output)
180 jsonw_uint_field(rd->jw, "lqpn", val);
181 else
182 pr_out("lqpn %u ", val);
183 }
184
185 static void print_rqpn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
186 {
187 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQPN])
188 return;
189
190 if (rd->json_output)
191 jsonw_uint_field(rd->jw, "rqpn", val);
192 else
193 pr_out("rqpn %u ", val);
194 }
195
196 static void print_type(struct rd *rd, uint32_t val)
197 {
198 if (rd->json_output)
199 jsonw_string_field(rd->jw, "type",
200 qp_types_to_str(val));
201 else
202 pr_out("type %s ", qp_types_to_str(val));
203 }
204
205 static void print_state(struct rd *rd, uint32_t val)
206 {
207 if (rd->json_output)
208 jsonw_string_field(rd->jw, "state",
209 qp_states_to_str(val));
210 else
211 pr_out("state %s ", qp_states_to_str(val));
212 }
213
214 static void print_rqpsn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
215 {
216 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN])
217 return;
218
219 if (rd->json_output)
220 jsonw_uint_field(rd->jw, "rq-psn", val);
221 else
222 pr_out("rq-psn %u ", val);
223 }
224
225 static void print_sqpsn(struct rd *rd, uint32_t val)
226 {
227 if (rd->json_output)
228 jsonw_uint_field(rd->jw, "sq-psn", val);
229 else
230 pr_out("sq-psn %u ", val);
231 }
232
233 static void print_pathmig(struct rd *rd, uint32_t val,
234 struct nlattr **nla_line)
235 {
236 if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE])
237 return;
238
239 if (rd->json_output)
240 jsonw_string_field(rd->jw,
241 "path-mig-state",
242 path_mig_to_str(val));
243 else
244 pr_out("path-mig-state %s ", path_mig_to_str(val));
245 }
246
247 static void print_pid(struct rd *rd, uint32_t val)
248 {
249 if (rd->json_output)
250 jsonw_uint_field(rd->jw, "pid", val);
251 else
252 pr_out("pid %u ", val);
253 }
254
255 static void print_comm(struct rd *rd, const char *str,
256 struct nlattr **nla_line)
257 {
258 char tmp[18];
259
260 if (rd->json_output) {
261 /* Don't beatify output in JSON format */
262 jsonw_string_field(rd->jw, "comm", str);
263 return;
264 }
265
266 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
267 snprintf(tmp, sizeof(tmp), "%s", str);
268 else
269 snprintf(tmp, sizeof(tmp), "[%s]", str);
270
271 pr_out("comm %s ", tmp);
272 }
273
274 static void print_dev(struct rd *rd, uint32_t idx, const char *name)
275 {
276 if (rd->json_output) {
277 jsonw_uint_field(rd->jw, "ifindex", idx);
278 jsonw_string_field(rd->jw, "ifname", name);
279 } else {
280 pr_out("dev %s ", name);
281 }
282 }
283
284 static void print_link(struct rd *rd, uint32_t idx, const char *name,
285 uint32_t port, struct nlattr **nla_line)
286 {
287 if (rd->json_output) {
288 jsonw_uint_field(rd->jw, "ifindex", idx);
289
290 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
291 jsonw_uint_field(rd->jw, "port", port);
292
293 jsonw_string_field(rd->jw, "ifname", name);
294 } else {
295 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
296 pr_out("link %s/%u ", name, port);
297 else
298 pr_out("link %s/- ", name);
299 }
300 }
301
302 static char *get_task_name(uint32_t pid)
303 {
304 char *comm;
305 FILE *f;
306
307 if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
308 return NULL;
309
310 f = fopen(comm, "r");
311 free(comm);
312 if (!f)
313 return NULL;
314
315 if (fscanf(f, "%ms\n", &comm) != 1)
316 comm = NULL;
317
318 fclose(f);
319
320 return comm;
321 }
322
323 static int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data)
324 {
325 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
326 struct nlattr *nla_table, *nla_entry;
327 struct rd *rd = data;
328 const char *name;
329 uint32_t idx;
330
331 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
332 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
333 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
334 !tb[RDMA_NLDEV_ATTR_RES_QP])
335 return MNL_CB_ERROR;
336
337 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
338 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
339 nla_table = tb[RDMA_NLDEV_ATTR_RES_QP];
340
341 mnl_attr_for_each_nested(nla_entry, nla_table) {
342 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
343 uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn;
344 uint8_t type, state, path_mig_state = 0;
345 uint32_t port = 0, pid = 0;
346 char *comm = NULL;
347 int err;
348
349 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
350 if (err != MNL_CB_OK)
351 return MNL_CB_ERROR;
352
353 if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] ||
354 !nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] ||
355 !nla_line[RDMA_NLDEV_ATTR_RES_TYPE] ||
356 !nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
357 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
358 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
359 return MNL_CB_ERROR;
360 }
361
362 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
363 port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
364
365 if (port != rd->port_idx)
366 continue;
367
368 lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
369 if (rd_check_is_filtered(rd, "lqpn", lqpn))
370 continue;
371
372 if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) {
373 rqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQPN]);
374 if (rd_check_is_filtered(rd, "rqpn", rqpn))
375 continue;
376 } else {
377 if (rd_check_is_key_exist(rd, "rqpn"))
378 continue;
379 }
380
381 if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) {
382 rq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]);
383 if (rd_check_is_filtered(rd, "rq-psn", rq_psn))
384 continue;
385 } else {
386 if (rd_check_is_key_exist(rd, "rq-psn"))
387 continue;
388 }
389
390 sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]);
391 if (rd_check_is_filtered(rd, "sq-psn", sq_psn))
392 continue;
393
394 if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) {
395 path_mig_state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]);
396 if (rd_check_is_string_filtered(rd, "path-mig-state", path_mig_to_str(path_mig_state)))
397 continue;
398 } else {
399 if (rd_check_is_key_exist(rd, "path-mig-state"))
400 continue;
401 }
402
403 type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
404 if (rd_check_is_string_filtered(rd, "type", qp_types_to_str(type)))
405 continue;
406
407 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
408 if (rd_check_is_string_filtered(rd, "state", qp_states_to_str(state)))
409 continue;
410
411 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
412 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
413 comm = get_task_name(pid);
414 }
415
416 if (rd_check_is_filtered(rd, "pid", pid)) {
417 free(comm);
418 continue;
419 }
420
421 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
422 /* discard const from mnl_attr_get_str */
423 comm = (char *)mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
424
425 if (rd->json_output)
426 jsonw_start_array(rd->jw);
427
428 print_link(rd, idx, name, port, nla_line);
429
430 print_lqpn(rd, lqpn);
431 print_rqpn(rd, rqpn, nla_line);
432
433 print_type(rd, type);
434 print_state(rd, state);
435
436 print_rqpsn(rd, rq_psn, nla_line);
437 print_sqpsn(rd, sq_psn);
438
439 print_pathmig(rd, path_mig_state, nla_line);
440 print_pid(rd, pid);
441 print_comm(rd, comm, nla_line);
442
443 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
444 free(comm);
445
446 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
447 newline(rd);
448 }
449 return MNL_CB_OK;
450 }
451
452 static void print_qp_type(struct rd *rd, uint32_t val)
453 {
454 if (rd->json_output)
455 jsonw_string_field(rd->jw, "qp-type",
456 qp_types_to_str(val));
457 else
458 pr_out("qp-type %s ", qp_types_to_str(val));
459 }
460
461 static const char *cm_id_state_to_str(uint8_t idx)
462 {
463 static const char * const cm_id_states_str[] = {
464 "IDLE", "ADDR_QUERY", "ADDR_RESOLVED", "ROUTE_QUERY",
465 "ROUTE_RESOLVED", "CONNECT", "DISCONNECT", "ADDR_BOUND",
466 "LISTEN", "DEVICE_REMOVAL", "DESTROYING" };
467
468 if (idx < ARRAY_SIZE(cm_id_states_str))
469 return cm_id_states_str[idx];
470 return "UNKNOWN";
471 }
472
473 static const char *cm_id_ps_to_str(uint32_t ps)
474 {
475 switch (ps) {
476 case RDMA_PS_IPOIB:
477 return "IPoIB";
478 case RDMA_PS_IB:
479 return "IPoIB";
480 case RDMA_PS_TCP:
481 return "TCP";
482 case RDMA_PS_UDP:
483 return "UDP";
484 default:
485 return "---";
486 }
487 }
488
489 static void print_cm_id_state(struct rd *rd, uint8_t state)
490 {
491 if (rd->json_output) {
492 jsonw_string_field(rd->jw, "state", cm_id_state_to_str(state));
493 return;
494 }
495 pr_out("state %s ", cm_id_state_to_str(state));
496 }
497
498 static void print_ps(struct rd *rd, uint32_t ps)
499 {
500 if (rd->json_output) {
501 jsonw_string_field(rd->jw, "ps", cm_id_ps_to_str(ps));
502 return;
503 }
504 pr_out("ps %s ", cm_id_ps_to_str(ps));
505 }
506
507 static void print_ipaddr(struct rd *rd, const char *key, char *addrstr,
508 uint16_t port)
509 {
510 if (rd->json_output) {
511 int name_size = INET6_ADDRSTRLEN+strlen(":65535");
512 char json_name[name_size];
513
514 snprintf(json_name, name_size, "%s:%u", addrstr, port);
515 jsonw_string_field(rd->jw, key, json_name);
516 return;
517 }
518 pr_out("%s %s:%u ", key, addrstr, port);
519 }
520
521 static int ss_ntop(struct nlattr *nla_line, char *addr_str, uint16_t *port)
522 {
523 struct __kernel_sockaddr_storage *addr;
524
525 addr = (struct __kernel_sockaddr_storage *)
526 mnl_attr_get_payload(nla_line);
527 switch (addr->ss_family) {
528 case AF_INET: {
529 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
530
531 if (!inet_ntop(AF_INET, (const void *)&sin->sin_addr, addr_str,
532 INET6_ADDRSTRLEN))
533 return -EINVAL;
534 *port = ntohs(sin->sin_port);
535 break;
536 }
537 case AF_INET6: {
538 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
539
540 if (!inet_ntop(AF_INET6, (const void *)&sin6->sin6_addr,
541 addr_str, INET6_ADDRSTRLEN))
542 return -EINVAL;
543 *port = ntohs(sin6->sin6_port);
544 break;
545 }
546 default:
547 return -EINVAL;
548 }
549 return 0;
550 }
551
552 static void print_key(struct rd *rd, const char *name, uint64_t val)
553 {
554 if (rd->json_output)
555 jsonw_xint_field(rd->jw, name, val);
556 else
557 pr_out("%s 0x%" PRIx64 " ", name, val);
558 }
559
560 static void res_print_uint(struct rd *rd, const char *name, uint64_t val)
561 {
562 if (rd->json_output)
563 jsonw_uint_field(rd->jw, name, val);
564 else
565 pr_out("%s %" PRIu64 " ", name, val);
566 }
567
568 static int res_cm_id_parse_cb(const struct nlmsghdr *nlh, void *data)
569 {
570 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
571 struct nlattr *nla_table, *nla_entry;
572 struct rd *rd = data;
573 const char *name;
574 int idx;
575
576 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
577 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
578 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
579 !tb[RDMA_NLDEV_ATTR_RES_CM_ID])
580 return MNL_CB_ERROR;
581
582 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
583 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
584 nla_table = tb[RDMA_NLDEV_ATTR_RES_CM_ID];
585 mnl_attr_for_each_nested(nla_entry, nla_table) {
586 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
587 char src_addr_str[INET6_ADDRSTRLEN];
588 char dst_addr_str[INET6_ADDRSTRLEN];
589 uint16_t src_port, dst_port;
590 uint32_t port = 0, pid = 0;
591 uint8_t type = 0, state;
592 uint32_t lqpn = 0, ps;
593 uint32_t cm_idn = 0;
594 char *comm = NULL;
595 int err;
596
597 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
598 if (err != MNL_CB_OK)
599 return -EINVAL;
600
601 if (!nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
602 !nla_line[RDMA_NLDEV_ATTR_RES_PS] ||
603 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
604 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
605 return MNL_CB_ERROR;
606 }
607
608 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
609 port = mnl_attr_get_u32(
610 nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
611
612 if (port && port != rd->port_idx)
613 continue;
614
615 if (nla_line[RDMA_NLDEV_ATTR_RES_LQPN]) {
616 lqpn = mnl_attr_get_u32(
617 nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
618 if (rd_check_is_filtered(rd, "lqpn", lqpn))
619 continue;
620 }
621 if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE]) {
622 type = mnl_attr_get_u8(
623 nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
624 if (rd_check_is_string_filtered(rd, "qp-type",
625 qp_types_to_str(type)))
626 continue;
627 }
628
629 ps = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PS]);
630 if (rd_check_is_string_filtered(rd, "ps", cm_id_ps_to_str(ps)))
631 continue;
632
633 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
634 if (rd_check_is_string_filtered(rd, "state",
635 cm_id_state_to_str(state)))
636 continue;
637
638 if (nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR]) {
639 if (ss_ntop(nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR],
640 src_addr_str, &src_port))
641 continue;
642 if (rd_check_is_string_filtered(rd, "src-addr",
643 src_addr_str))
644 continue;
645 if (rd_check_is_filtered(rd, "src-port", src_port))
646 continue;
647 }
648
649 if (nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR]) {
650 if (ss_ntop(nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR],
651 dst_addr_str, &dst_port))
652 continue;
653 if (rd_check_is_string_filtered(rd, "dst-addr",
654 dst_addr_str))
655 continue;
656 if (rd_check_is_filtered(rd, "dst-port", dst_port))
657 continue;
658 }
659
660 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
661 pid = mnl_attr_get_u32(
662 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
663 comm = get_task_name(pid);
664 }
665
666 if (rd_check_is_filtered(rd, "pid", pid)) {
667 free(comm);
668 continue;
669 }
670
671 if (nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN])
672 cm_idn = mnl_attr_get_u32(
673 nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN]);
674 if (rd_check_is_filtered(rd, "cm-idn", cm_idn))
675 continue;
676
677 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
678 /* discard const from mnl_attr_get_str */
679 comm = (char *)mnl_attr_get_str(
680 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
681 }
682
683 if (rd->json_output)
684 jsonw_start_array(rd->jw);
685
686 print_link(rd, idx, name, port, nla_line);
687 if (nla_line[RDMA_NLDEV_ATTR_RES_LQPN])
688 print_lqpn(rd, lqpn);
689 if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE])
690 print_qp_type(rd, type);
691 print_cm_id_state(rd, state);
692 print_ps(rd, ps);
693 print_pid(rd, pid);
694 print_comm(rd, comm, nla_line);
695 if (nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN])
696 res_print_uint(rd, "cm-idn", cm_idn);
697
698 if (nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR])
699 print_ipaddr(rd, "src-addr", src_addr_str, src_port);
700 if (nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR])
701 print_ipaddr(rd, "dst-addr", dst_addr_str, dst_port);
702
703 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
704 free(comm);
705
706 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
707 newline(rd);
708 }
709 return MNL_CB_OK;
710 }
711
712 static void print_cqe(struct rd *rd, uint32_t val)
713 {
714 if (rd->json_output)
715 jsonw_uint_field(rd->jw, "cqe", val);
716 else
717 pr_out("cqe %u ", val);
718 }
719
720 static void print_users(struct rd *rd, uint64_t val)
721 {
722 if (rd->json_output)
723 jsonw_uint_field(rd->jw, "users", val);
724 else
725 pr_out("users %" PRIu64 " ", val);
726 }
727
728 static const char *poll_ctx_to_str(uint8_t idx)
729 {
730 static const char * const cm_id_states_str[] = {
731 "DIRECT", "SOFTIRQ", "WORKQUEUE", "UNBOUND_WORKQUEUE"};
732
733 if (idx < ARRAY_SIZE(cm_id_states_str))
734 return cm_id_states_str[idx];
735 return "UNKNOWN";
736 }
737
738 static void print_poll_ctx(struct rd *rd, uint8_t poll_ctx)
739 {
740 if (rd->json_output) {
741 jsonw_string_field(rd->jw, "poll-ctx",
742 poll_ctx_to_str(poll_ctx));
743 return;
744 }
745 pr_out("poll-ctx %s ", poll_ctx_to_str(poll_ctx));
746 }
747
748 static int res_cq_parse_cb(const struct nlmsghdr *nlh, void *data)
749 {
750 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
751 struct nlattr *nla_table, *nla_entry;
752 struct rd *rd = data;
753 const char *name;
754 uint32_t idx;
755
756 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
757 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
758 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
759 !tb[RDMA_NLDEV_ATTR_RES_CQ])
760 return MNL_CB_ERROR;
761
762 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
763 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
764 nla_table = tb[RDMA_NLDEV_ATTR_RES_CQ];
765
766 mnl_attr_for_each_nested(nla_entry, nla_table) {
767 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
768 char *comm = NULL;
769 uint32_t pid = 0;
770 uint8_t poll_ctx = 0;
771 uint32_t cqn = 0;
772 uint64_t users;
773 uint32_t cqe;
774 int err;
775
776 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
777 if (err != MNL_CB_OK)
778 return MNL_CB_ERROR;
779
780 if (!nla_line[RDMA_NLDEV_ATTR_RES_CQE] ||
781 !nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
782 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
783 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
784 return MNL_CB_ERROR;
785 }
786
787 cqe = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CQE]);
788
789 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
790 if (rd_check_is_filtered(rd, "users", users))
791 continue;
792
793 if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]) {
794 poll_ctx = mnl_attr_get_u8(
795 nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]);
796 if (rd_check_is_string_filtered(rd, "poll-ctx",
797 poll_ctx_to_str(poll_ctx)))
798 continue;
799 }
800
801 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
802 pid = mnl_attr_get_u32(
803 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
804 comm = get_task_name(pid);
805 }
806
807 if (rd_check_is_filtered(rd, "pid", pid)) {
808 free(comm);
809 continue;
810 }
811
812 if (nla_line[RDMA_NLDEV_ATTR_RES_CQN])
813 cqn = mnl_attr_get_u32(
814 nla_line[RDMA_NLDEV_ATTR_RES_CQN]);
815 if (rd_check_is_filtered(rd, "cqn", cqn))
816 continue;
817
818 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
819 /* discard const from mnl_attr_get_str */
820 comm = (char *)mnl_attr_get_str(
821 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
822
823 if (rd->json_output)
824 jsonw_start_array(rd->jw);
825
826 print_dev(rd, idx, name);
827 print_cqe(rd, cqe);
828 print_users(rd, users);
829 if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX])
830 print_poll_ctx(rd, poll_ctx);
831 print_pid(rd, pid);
832 print_comm(rd, comm, nla_line);
833
834 if (nla_line[RDMA_NLDEV_ATTR_RES_CQN])
835 res_print_uint(rd, "cqn", cqn);
836
837 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
838 free(comm);
839
840 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
841 newline(rd);
842 }
843 return MNL_CB_OK;
844 }
845
846 static int res_mr_parse_cb(const struct nlmsghdr *nlh, void *data)
847 {
848 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
849 struct nlattr *nla_table, *nla_entry;
850 struct rd *rd = data;
851 const char *name;
852 uint32_t idx;
853
854 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
855 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
856 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
857 !tb[RDMA_NLDEV_ATTR_RES_MR])
858 return MNL_CB_ERROR;
859
860 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
861 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
862 nla_table = tb[RDMA_NLDEV_ATTR_RES_MR];
863
864 mnl_attr_for_each_nested(nla_entry, nla_table) {
865 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
866 uint32_t rkey = 0, lkey = 0;
867 uint64_t iova = 0, mrlen;
868 char *comm = NULL;
869 uint32_t mrn = 0;
870 uint32_t pid = 0;
871 int err;
872
873 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
874 if (err != MNL_CB_OK)
875 return MNL_CB_ERROR;
876
877 if (!nla_line[RDMA_NLDEV_ATTR_RES_MRLEN] ||
878 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
879 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
880 return MNL_CB_ERROR;
881 }
882
883 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
884 rkey = mnl_attr_get_u32(
885 nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
886 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
887 lkey = mnl_attr_get_u32(
888 nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
889 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
890 iova = mnl_attr_get_u64(
891 nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
892
893 mrlen = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
894 if (rd_check_is_filtered(rd, "mrlen", mrlen))
895 continue;
896
897 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
898 pid = mnl_attr_get_u32(
899 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
900 comm = get_task_name(pid);
901 }
902
903 if (rd_check_is_filtered(rd, "pid", pid)) {
904 free(comm);
905 continue;
906 }
907
908 if (nla_line[RDMA_NLDEV_ATTR_RES_MRN])
909 mrn = mnl_attr_get_u32(
910 nla_line[RDMA_NLDEV_ATTR_RES_MRN]);
911 if (rd_check_is_filtered(rd, "mrn", mrn))
912 continue;
913
914 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
915 /* discard const from mnl_attr_get_str */
916 comm = (char *)mnl_attr_get_str(
917 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
918
919 if (rd->json_output)
920 jsonw_start_array(rd->jw);
921
922 print_dev(rd, idx, name);
923 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
924 print_key(rd, "rkey", rkey);
925 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
926 print_key(rd, "lkey", lkey);
927 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
928 print_key(rd, "iova", iova);
929 res_print_uint(rd, "mrlen", mrlen);
930 print_pid(rd, pid);
931 print_comm(rd, comm, nla_line);
932
933 if (nla_line[RDMA_NLDEV_ATTR_RES_MRN])
934 res_print_uint(rd, "mrn", mrn);
935
936 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
937 free(comm);
938
939 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
940 newline(rd);
941 }
942 return MNL_CB_OK;
943 }
944
945 static int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data)
946 {
947 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
948 struct nlattr *nla_table, *nla_entry;
949 struct rd *rd = data;
950 const char *name;
951 uint32_t idx;
952
953 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
954 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
955 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
956 !tb[RDMA_NLDEV_ATTR_RES_PD])
957 return MNL_CB_ERROR;
958
959 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
960 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
961 nla_table = tb[RDMA_NLDEV_ATTR_RES_PD];
962
963 mnl_attr_for_each_nested(nla_entry, nla_table) {
964 uint32_t local_dma_lkey = 0, unsafe_global_rkey = 0;
965 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
966 char *comm = NULL;
967 uint32_t pid = 0;
968 uint32_t pdn = 0;
969 uint64_t users;
970 int err;
971
972 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
973 if (err != MNL_CB_OK)
974 return MNL_CB_ERROR;
975
976 if (!nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
977 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
978 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
979 return MNL_CB_ERROR;
980 }
981
982 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
983 local_dma_lkey = mnl_attr_get_u32(
984 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
985
986 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
987 if (rd_check_is_filtered(rd, "users", users))
988 continue;
989
990 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
991 unsafe_global_rkey = mnl_attr_get_u32(
992 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
993
994 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
995 pid = mnl_attr_get_u32(
996 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
997 comm = get_task_name(pid);
998 }
999
1000 if (rd_check_is_filtered(rd, "pid", pid))
1001 continue;
1002
1003 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
1004 pdn = mnl_attr_get_u32(
1005 nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
1006 if (rd_check_is_filtered(rd, "pdn", pdn))
1007 continue;
1008
1009 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
1010 /* discard const from mnl_attr_get_str */
1011 comm = (char *)mnl_attr_get_str(
1012 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
1013
1014 if (rd->json_output)
1015 jsonw_start_array(rd->jw);
1016
1017 print_dev(rd, idx, name);
1018 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
1019 print_key(rd, "local_dma_lkey", local_dma_lkey);
1020 print_users(rd, users);
1021 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
1022 print_key(rd, "unsafe_global_rkey", unsafe_global_rkey);
1023 print_pid(rd, pid);
1024 print_comm(rd, comm, nla_line);
1025
1026 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
1027 res_print_uint(rd, "pdn", pdn);
1028
1029 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
1030 free(comm);
1031
1032 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
1033 newline(rd);
1034 }
1035 return MNL_CB_OK;
1036 }
1037
1038 RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true);
1039
1040 static const struct
1041 filters qp_valid_filters[MAX_NUMBER_OF_FILTERS] = {{ .name = "link",
1042 .is_number = false },
1043 { .name = "lqpn",
1044 .is_number = true },
1045 { .name = "rqpn",
1046 .is_number = true },
1047 { .name = "pid",
1048 .is_number = true },
1049 { .name = "sq-psn",
1050 .is_number = true },
1051 { .name = "rq-psn",
1052 .is_number = true },
1053 { .name = "type",
1054 .is_number = false },
1055 { .name = "path-mig-state",
1056 .is_number = false },
1057 { .name = "state",
1058 .is_number = false } };
1059
1060 RES_FUNC(res_qp, RDMA_NLDEV_CMD_RES_QP_GET, qp_valid_filters, false);
1061
1062 static const
1063 struct filters cm_id_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1064 { .name = "link", .is_number = false },
1065 { .name = "lqpn", .is_number = true },
1066 { .name = "qp-type", .is_number = false },
1067 { .name = "state", .is_number = false },
1068 { .name = "ps", .is_number = false },
1069 { .name = "dev-type", .is_number = false },
1070 { .name = "transport-type", .is_number = false },
1071 { .name = "pid", .is_number = true },
1072 { .name = "src-addr", .is_number = false },
1073 { .name = "src-port", .is_number = true },
1074 { .name = "dst-addr", .is_number = false },
1075 { .name = "dst-port", .is_number = true },
1076 { .name = "cm-idn", .is_number = true }
1077 };
1078
1079 RES_FUNC(res_cm_id, RDMA_NLDEV_CMD_RES_CM_ID_GET, cm_id_valid_filters, false);
1080
1081 static const
1082 struct filters cq_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1083 { .name = "dev", .is_number = false },
1084 { .name = "users", .is_number = true },
1085 { .name = "poll-ctx", .is_number = false },
1086 { .name = "pid", .is_number = true },
1087 { .name = "cqn", .is_number = true }
1088 };
1089
1090 RES_FUNC(res_cq, RDMA_NLDEV_CMD_RES_CQ_GET, cq_valid_filters, true);
1091
1092 static const
1093 struct filters mr_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1094 { .name = "dev", .is_number = false },
1095 { .name = "rkey", .is_number = true },
1096 { .name = "lkey", .is_number = true },
1097 { .name = "mrlen", .is_number = true },
1098 { .name = "pid", .is_number = true },
1099 { .name = "mrn", .is_number = true }
1100 };
1101
1102 RES_FUNC(res_mr, RDMA_NLDEV_CMD_RES_MR_GET, mr_valid_filters, true);
1103
1104 static const
1105 struct filters pd_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1106 { .name = "dev", .is_number = false },
1107 { .name = "users", .is_number = true },
1108 { .name = "pid", .is_number = true },
1109 { .name = "pdn", .is_number = true }
1110 };
1111
1112 RES_FUNC(res_pd, RDMA_NLDEV_CMD_RES_PD_GET, pd_valid_filters, true);
1113
1114 static int res_show(struct rd *rd)
1115 {
1116 const struct rd_cmd cmds[] = {
1117 { NULL, res_no_args },
1118 { "qp", res_qp },
1119 { "cm_id", res_cm_id },
1120 { "cq", res_cq },
1121 { "mr", res_mr },
1122 { "pd", res_pd },
1123 { 0 }
1124 };
1125
1126 /*
1127 * Special case to support "rdma res show DEV_NAME"
1128 */
1129 if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
1130 return rd_exec_dev(rd, _res_no_args);
1131
1132 return rd_exec_cmd(rd, cmds, "parameter");
1133 }
1134
1135 int cmd_res(struct rd *rd)
1136 {
1137 const struct rd_cmd cmds[] = {
1138 { NULL, res_show },
1139 { "show", res_show },
1140 { "list", res_show },
1141 { "help", res_help },
1142 { 0 }
1143 };
1144
1145 return rd_exec_cmd(rd, cmds, "resource command");
1146 }