]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res.c
rdma: update uapi headers
[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 int res_cm_id_parse_cb(const struct nlmsghdr *nlh, void *data)
553 {
554 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
555 struct nlattr *nla_table, *nla_entry;
556 struct rd *rd = data;
557 const char *name;
558 int idx;
559
560 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
561 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
562 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
563 !tb[RDMA_NLDEV_ATTR_RES_CM_ID])
564 return MNL_CB_ERROR;
565
566 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
567 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
568 nla_table = tb[RDMA_NLDEV_ATTR_RES_CM_ID];
569 mnl_attr_for_each_nested(nla_entry, nla_table) {
570 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
571 char src_addr_str[INET6_ADDRSTRLEN];
572 char dst_addr_str[INET6_ADDRSTRLEN];
573 uint16_t src_port, dst_port;
574 uint32_t port = 0, pid = 0;
575 uint8_t type = 0, state;
576 uint32_t lqpn = 0, ps;
577 char *comm = NULL;
578 int err;
579
580 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
581 if (err != MNL_CB_OK)
582 return -EINVAL;
583
584 if (!nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
585 !nla_line[RDMA_NLDEV_ATTR_RES_PS] ||
586 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
587 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
588 return MNL_CB_ERROR;
589 }
590
591 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
592 port = mnl_attr_get_u32(
593 nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
594
595 if (port && port != rd->port_idx)
596 continue;
597
598 if (nla_line[RDMA_NLDEV_ATTR_RES_LQPN]) {
599 lqpn = mnl_attr_get_u32(
600 nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
601 if (rd_check_is_filtered(rd, "lqpn", lqpn))
602 continue;
603 }
604 if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE]) {
605 type = mnl_attr_get_u8(
606 nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
607 if (rd_check_is_string_filtered(rd, "qp-type",
608 qp_types_to_str(type)))
609 continue;
610 }
611
612 ps = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PS]);
613 if (rd_check_is_string_filtered(rd, "ps", cm_id_ps_to_str(ps)))
614 continue;
615
616 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
617 if (rd_check_is_string_filtered(rd, "state",
618 cm_id_state_to_str(state)))
619 continue;
620
621 if (nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR]) {
622 if (ss_ntop(nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR],
623 src_addr_str, &src_port))
624 continue;
625 if (rd_check_is_string_filtered(rd, "src-addr",
626 src_addr_str))
627 continue;
628 if (rd_check_is_filtered(rd, "src-port", src_port))
629 continue;
630 }
631
632 if (nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR]) {
633 if (ss_ntop(nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR],
634 dst_addr_str, &dst_port))
635 continue;
636 if (rd_check_is_string_filtered(rd, "dst-addr",
637 dst_addr_str))
638 continue;
639 if (rd_check_is_filtered(rd, "dst-port", dst_port))
640 continue;
641 }
642
643 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
644 pid = mnl_attr_get_u32(
645 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
646 comm = get_task_name(pid);
647 }
648
649 if (rd_check_is_filtered(rd, "pid", pid)) {
650 free(comm);
651 continue;
652 }
653
654 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
655 /* discard const from mnl_attr_get_str */
656 comm = (char *)mnl_attr_get_str(
657 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
658 }
659
660 if (rd->json_output)
661 jsonw_start_array(rd->jw);
662
663 print_link(rd, idx, name, port, nla_line);
664 if (nla_line[RDMA_NLDEV_ATTR_RES_LQPN])
665 print_lqpn(rd, lqpn);
666 if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE])
667 print_qp_type(rd, type);
668 print_cm_id_state(rd, state);
669 print_ps(rd, ps);
670 print_pid(rd, pid);
671 print_comm(rd, comm, nla_line);
672
673 if (nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR])
674 print_ipaddr(rd, "src-addr", src_addr_str, src_port);
675 if (nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR])
676 print_ipaddr(rd, "dst-addr", dst_addr_str, dst_port);
677
678 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
679 free(comm);
680
681 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
682 newline(rd);
683 }
684 return MNL_CB_OK;
685 }
686
687 static void print_cqe(struct rd *rd, uint32_t val)
688 {
689 if (rd->json_output)
690 jsonw_uint_field(rd->jw, "cqe", val);
691 else
692 pr_out("cqe %u ", val);
693 }
694
695 static void print_users(struct rd *rd, uint64_t val)
696 {
697 if (rd->json_output)
698 jsonw_uint_field(rd->jw, "users", val);
699 else
700 pr_out("users %" PRIu64 " ", val);
701 }
702
703 static const char *poll_ctx_to_str(uint8_t idx)
704 {
705 static const char * const cm_id_states_str[] = {
706 "DIRECT", "SOFTIRQ", "WORKQUEUE", "UNBOUND_WORKQUEUE"};
707
708 if (idx < ARRAY_SIZE(cm_id_states_str))
709 return cm_id_states_str[idx];
710 return "UNKNOWN";
711 }
712
713 static void print_poll_ctx(struct rd *rd, uint8_t poll_ctx)
714 {
715 if (rd->json_output) {
716 jsonw_string_field(rd->jw, "poll-ctx",
717 poll_ctx_to_str(poll_ctx));
718 return;
719 }
720 pr_out("poll-ctx %s ", poll_ctx_to_str(poll_ctx));
721 }
722
723 static int res_cq_parse_cb(const struct nlmsghdr *nlh, void *data)
724 {
725 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
726 struct nlattr *nla_table, *nla_entry;
727 struct rd *rd = data;
728 const char *name;
729 uint32_t idx;
730
731 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
732 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
733 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
734 !tb[RDMA_NLDEV_ATTR_RES_CQ])
735 return MNL_CB_ERROR;
736
737 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
738 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
739 nla_table = tb[RDMA_NLDEV_ATTR_RES_CQ];
740
741 mnl_attr_for_each_nested(nla_entry, nla_table) {
742 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
743 char *comm = NULL;
744 uint32_t pid = 0;
745 uint8_t poll_ctx = 0;
746 uint64_t users;
747 uint32_t cqe;
748 int err;
749
750 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
751 if (err != MNL_CB_OK)
752 return MNL_CB_ERROR;
753
754 if (!nla_line[RDMA_NLDEV_ATTR_RES_CQE] ||
755 !nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
756 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
757 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
758 return MNL_CB_ERROR;
759 }
760
761 cqe = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CQE]);
762
763 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
764 if (rd_check_is_filtered(rd, "users", users))
765 continue;
766
767 if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]) {
768 poll_ctx = mnl_attr_get_u8(
769 nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]);
770 if (rd_check_is_string_filtered(rd, "poll-ctx",
771 poll_ctx_to_str(poll_ctx)))
772 continue;
773 }
774
775 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
776 pid = mnl_attr_get_u32(
777 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
778 comm = get_task_name(pid);
779 }
780
781 if (rd_check_is_filtered(rd, "pid", pid)) {
782 free(comm);
783 continue;
784 }
785
786 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
787 /* discard const from mnl_attr_get_str */
788 comm = (char *)mnl_attr_get_str(
789 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
790
791 if (rd->json_output)
792 jsonw_start_array(rd->jw);
793
794 print_dev(rd, idx, name);
795 print_cqe(rd, cqe);
796 print_users(rd, users);
797 if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX])
798 print_poll_ctx(rd, poll_ctx);
799 print_pid(rd, pid);
800 print_comm(rd, comm, nla_line);
801
802 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
803 free(comm);
804
805 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
806 newline(rd);
807 }
808 return MNL_CB_OK;
809 }
810
811 static void print_key(struct rd *rd, const char *name, uint32_t val)
812 {
813 if (rd->json_output)
814 jsonw_xint_field(rd->jw, name, val);
815 else
816 pr_out("%s 0x%x ", name, val);
817 }
818
819 static void print_iova(struct rd *rd, uint64_t val)
820 {
821 if (rd->json_output)
822 jsonw_xint_field(rd->jw, "iova", val);
823 else
824 pr_out("iova 0x%" PRIx64 " ", val);
825 }
826
827 static void print_mrlen(struct rd *rd, uint64_t val)
828 {
829 if (rd->json_output)
830 jsonw_uint_field(rd->jw, "mrlen", val);
831 else
832 pr_out("mrlen %" PRIu64 " ", val);
833 }
834
835 static int res_mr_parse_cb(const struct nlmsghdr *nlh, void *data)
836 {
837 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
838 struct nlattr *nla_table, *nla_entry;
839 struct rd *rd = data;
840 const char *name;
841 uint32_t idx;
842
843 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
844 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
845 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
846 !tb[RDMA_NLDEV_ATTR_RES_MR])
847 return MNL_CB_ERROR;
848
849 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
850 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
851 nla_table = tb[RDMA_NLDEV_ATTR_RES_MR];
852
853 mnl_attr_for_each_nested(nla_entry, nla_table) {
854 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
855 uint32_t rkey = 0, lkey = 0;
856 uint64_t iova = 0, mrlen;
857 char *comm = NULL;
858 uint32_t pid = 0;
859 int err;
860
861 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
862 if (err != MNL_CB_OK)
863 return MNL_CB_ERROR;
864
865 if (!nla_line[RDMA_NLDEV_ATTR_RES_MRLEN] ||
866 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
867 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
868 return MNL_CB_ERROR;
869 }
870
871 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
872 rkey = mnl_attr_get_u32(
873 nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
874 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
875 lkey = mnl_attr_get_u32(
876 nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
877 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
878 iova = mnl_attr_get_u64(
879 nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
880
881 mrlen = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
882 if (rd_check_is_filtered(rd, "mrlen", mrlen))
883 continue;
884
885 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
886 pid = mnl_attr_get_u32(
887 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
888 comm = get_task_name(pid);
889 }
890
891 if (rd_check_is_filtered(rd, "pid", pid)) {
892 free(comm);
893 continue;
894 }
895
896 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
897 /* discard const from mnl_attr_get_str */
898 comm = (char *)mnl_attr_get_str(
899 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
900
901 if (rd->json_output)
902 jsonw_start_array(rd->jw);
903
904 print_dev(rd, idx, name);
905 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
906 print_key(rd, "rkey", rkey);
907 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
908 print_key(rd, "lkey", lkey);
909 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
910 print_iova(rd, iova);
911 print_mrlen(rd, mrlen);
912 print_pid(rd, pid);
913 print_comm(rd, comm, nla_line);
914
915 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
916 free(comm);
917
918 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
919 newline(rd);
920 }
921 return MNL_CB_OK;
922 }
923
924 static int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data)
925 {
926 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
927 struct nlattr *nla_table, *nla_entry;
928 struct rd *rd = data;
929 const char *name;
930 uint32_t idx;
931
932 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
933 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
934 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
935 !tb[RDMA_NLDEV_ATTR_RES_PD])
936 return MNL_CB_ERROR;
937
938 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
939 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
940 nla_table = tb[RDMA_NLDEV_ATTR_RES_PD];
941
942 mnl_attr_for_each_nested(nla_entry, nla_table) {
943 uint32_t local_dma_lkey = 0, unsafe_global_rkey = 0;
944 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
945 char *comm = NULL;
946 uint32_t pid = 0;
947 uint64_t users;
948 int err;
949
950 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
951 if (err != MNL_CB_OK)
952 return MNL_CB_ERROR;
953
954 if (!nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
955 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
956 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
957 return MNL_CB_ERROR;
958 }
959
960 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
961 local_dma_lkey = mnl_attr_get_u32(
962 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
963
964 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
965 if (rd_check_is_filtered(rd, "users", users))
966 continue;
967
968 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
969 unsafe_global_rkey = mnl_attr_get_u32(
970 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
971
972 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
973 pid = mnl_attr_get_u32(
974 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
975 comm = get_task_name(pid);
976 }
977
978 if (rd_check_is_filtered(rd, "pid", pid))
979 continue;
980
981 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
982 /* discard const from mnl_attr_get_str */
983 comm = (char *)mnl_attr_get_str(
984 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
985
986 if (rd->json_output)
987 jsonw_start_array(rd->jw);
988
989 print_dev(rd, idx, name);
990 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
991 print_key(rd, "local_dma_lkey", local_dma_lkey);
992 print_users(rd, users);
993 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
994 print_key(rd, "unsafe_global_rkey", unsafe_global_rkey);
995 print_pid(rd, pid);
996 print_comm(rd, comm, nla_line);
997
998 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
999 free(comm);
1000
1001 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
1002 newline(rd);
1003 }
1004 return MNL_CB_OK;
1005 }
1006
1007 RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true);
1008
1009 static const struct
1010 filters qp_valid_filters[MAX_NUMBER_OF_FILTERS] = {{ .name = "link",
1011 .is_number = false },
1012 { .name = "lqpn",
1013 .is_number = true },
1014 { .name = "rqpn",
1015 .is_number = true },
1016 { .name = "pid",
1017 .is_number = true },
1018 { .name = "sq-psn",
1019 .is_number = true },
1020 { .name = "rq-psn",
1021 .is_number = true },
1022 { .name = "type",
1023 .is_number = false },
1024 { .name = "path-mig-state",
1025 .is_number = false },
1026 { .name = "state",
1027 .is_number = false } };
1028
1029 RES_FUNC(res_qp, RDMA_NLDEV_CMD_RES_QP_GET, qp_valid_filters, false);
1030
1031 static const
1032 struct filters cm_id_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1033 { .name = "link", .is_number = false },
1034 { .name = "lqpn", .is_number = true },
1035 { .name = "qp-type", .is_number = false },
1036 { .name = "state", .is_number = false },
1037 { .name = "ps", .is_number = false },
1038 { .name = "dev-type", .is_number = false },
1039 { .name = "transport-type", .is_number = false },
1040 { .name = "pid", .is_number = true },
1041 { .name = "src-addr", .is_number = false },
1042 { .name = "src-port", .is_number = true },
1043 { .name = "dst-addr", .is_number = false },
1044 { .name = "dst-port", .is_number = true }
1045 };
1046
1047 RES_FUNC(res_cm_id, RDMA_NLDEV_CMD_RES_CM_ID_GET, cm_id_valid_filters, false);
1048
1049 static const
1050 struct filters cq_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1051 { .name = "dev", .is_number = false },
1052 { .name = "users", .is_number = true },
1053 { .name = "poll-ctx", .is_number = false },
1054 { .name = "pid", .is_number = true }
1055 };
1056
1057 RES_FUNC(res_cq, RDMA_NLDEV_CMD_RES_CQ_GET, cq_valid_filters, true);
1058
1059 static const
1060 struct filters mr_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1061 { .name = "dev", .is_number = false },
1062 { .name = "rkey", .is_number = true },
1063 { .name = "lkey", .is_number = true },
1064 { .name = "mrlen", .is_number = true },
1065 { .name = "pid", .is_number = true }
1066 };
1067
1068 RES_FUNC(res_mr, RDMA_NLDEV_CMD_RES_MR_GET, mr_valid_filters, true);
1069
1070 static const
1071 struct filters pd_valid_filters[MAX_NUMBER_OF_FILTERS] = {
1072 { .name = "dev", .is_number = false },
1073 { .name = "users", .is_number = true },
1074 { .name = "pid", .is_number = true }
1075 };
1076
1077 RES_FUNC(res_pd, RDMA_NLDEV_CMD_RES_PD_GET, pd_valid_filters, true);
1078
1079 static int res_show(struct rd *rd)
1080 {
1081 const struct rd_cmd cmds[] = {
1082 { NULL, res_no_args },
1083 { "qp", res_qp },
1084 { "cm_id", res_cm_id },
1085 { "cq", res_cq },
1086 { "mr", res_mr },
1087 { "pd", res_pd },
1088 { 0 }
1089 };
1090
1091 /*
1092 * Special case to support "rdma res show DEV_NAME"
1093 */
1094 if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
1095 return rd_exec_dev(rd, _res_no_args);
1096
1097 return rd_exec_cmd(rd, cmds, "parameter");
1098 }
1099
1100 int cmd_res(struct rd *rd)
1101 {
1102 const struct rd_cmd cmds[] = {
1103 { NULL, res_show },
1104 { "show", res_show },
1105 { "list", res_show },
1106 { "help", res_help },
1107 { 0 }
1108 };
1109
1110 return rd_exec_cmd(rd, cmds, "resource command");
1111 }