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.
9 * Authors: Leon Romanovsky <leonro@mellanox.com>
15 static int res_help(struct rd
*rd
)
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");
33 static int res_print_summary(struct rd
*rd
, struct nlattr
**tb
)
35 struct nlattr
*nla_table
= tb
[RDMA_NLDEV_ATTR_RES_SUMMARY
];
36 struct nlattr
*nla_entry
;
41 mnl_attr_for_each_nested(nla_entry
, nla_table
) {
42 struct nlattr
*nla_line
[RDMA_NLDEV_ATTR_MAX
] = {};
45 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
49 if (!nla_line
[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME
] ||
50 !nla_line
[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR
]) {
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
);
60 pr_out("%s %"PRId64
" ", name
, curr
);
66 static int res_no_args_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
68 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
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
])
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
);
85 pr_out("%u: %s: ", idx
, name
);
88 res_print_summary(rd
, tb
);
95 static int _res_send_msg(struct rd
*rd
, uint32_t command
, mnl_cb_t callback
)
97 uint32_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
101 if (command
!= RDMA_NLDEV_CMD_RES_GET
)
104 rd_prepare_msg(rd
, command
, &seq
, flags
);
105 mnl_attr_put_u32(rd
->nlh
, RDMA_NLDEV_ATTR_DEV_INDEX
, rd
->dev_idx
);
107 mnl_attr_put_u32(rd
->nlh
,
108 RDMA_NLDEV_ATTR_PORT_INDEX
, rd
->port_idx
);
110 ret
= rd_send_msg(rd
);
115 jsonw_start_object(rd
->jw
);
116 ret
= rd_recv_msg(rd
, callback
, rd
, seq
);
118 jsonw_end_object(rd
->jw
);
122 #define RES_FUNC(name, command, valid_filters, strict_port) \
123 static int _##name(struct rd *rd)\
125 return _res_send_msg(rd, command, name##_parse_cb); \
127 static int name(struct rd *rd) \
129 int ret = rd_build_filter(rd, valid_filters); \
132 if ((uintptr_t)valid_filters != (uintptr_t)NULL) { \
133 ret = rd_set_arg_to_devname(rd); \
138 return rd_exec_dev(rd, _##name); \
140 return rd_exec_link(rd, _##name, strict_port); \
143 static const char *path_mig_to_str(uint8_t idx
)
145 static const char * const path_mig_str
[] = { "MIGRATED",
148 if (idx
< ARRAY_SIZE(path_mig_str
))
149 return path_mig_str
[idx
];
153 static const char *qp_states_to_str(uint8_t idx
)
155 static const char * const qp_states_str
[] = { "RESET", "INIT",
159 if (idx
< ARRAY_SIZE(qp_states_str
))
160 return qp_states_str
[idx
];
164 static const char *qp_types_to_str(uint8_t idx
)
166 static const char * const qp_types_str
[] = { "SMI", "GSI", "RC",
167 "UC", "UD", "RAW_IPV6",
169 "UNKNOWN", "RAW_PACKET",
170 "XRC_INI", "XRC_TGT" };
172 if (idx
< ARRAY_SIZE(qp_types_str
))
173 return qp_types_str
[idx
];
177 static void print_lqpn(struct rd
*rd
, uint32_t val
)
180 jsonw_uint_field(rd
->jw
, "lqpn", val
);
182 pr_out("lqpn %u ", val
);
185 static void print_rqpn(struct rd
*rd
, uint32_t val
, struct nlattr
**nla_line
)
187 if (!nla_line
[RDMA_NLDEV_ATTR_RES_RQPN
])
191 jsonw_uint_field(rd
->jw
, "rqpn", val
);
193 pr_out("rqpn %u ", val
);
196 static void print_type(struct rd
*rd
, uint32_t val
)
199 jsonw_string_field(rd
->jw
, "type",
200 qp_types_to_str(val
));
202 pr_out("type %s ", qp_types_to_str(val
));
205 static void print_state(struct rd
*rd
, uint32_t val
)
208 jsonw_string_field(rd
->jw
, "state",
209 qp_states_to_str(val
));
211 pr_out("state %s ", qp_states_to_str(val
));
214 static void print_rqpsn(struct rd
*rd
, uint32_t val
, struct nlattr
**nla_line
)
216 if (!nla_line
[RDMA_NLDEV_ATTR_RES_RQ_PSN
])
220 jsonw_uint_field(rd
->jw
, "rq-psn", val
);
222 pr_out("rq-psn %u ", val
);
225 static void print_sqpsn(struct rd
*rd
, uint32_t val
)
228 jsonw_uint_field(rd
->jw
, "sq-psn", val
);
230 pr_out("sq-psn %u ", val
);
233 static void print_pathmig(struct rd
*rd
, uint32_t val
,
234 struct nlattr
**nla_line
)
236 if (!nla_line
[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE
])
240 jsonw_string_field(rd
->jw
,
242 path_mig_to_str(val
));
244 pr_out("path-mig-state %s ", path_mig_to_str(val
));
247 static void print_pid(struct rd
*rd
, uint32_t val
)
250 jsonw_uint_field(rd
->jw
, "pid", val
);
252 pr_out("pid %u ", val
);
255 static void print_comm(struct rd
*rd
, const char *str
,
256 struct nlattr
**nla_line
)
260 if (rd
->json_output
) {
261 /* Don't beatify output in JSON format */
262 jsonw_string_field(rd
->jw
, "comm", str
);
266 if (nla_line
[RDMA_NLDEV_ATTR_RES_PID
])
267 snprintf(tmp
, sizeof(tmp
), "%s", str
);
269 snprintf(tmp
, sizeof(tmp
), "[%s]", str
);
271 pr_out("comm %s ", tmp
);
274 static void print_dev(struct rd
*rd
, uint32_t idx
, const char *name
)
276 if (rd
->json_output
) {
277 jsonw_uint_field(rd
->jw
, "ifindex", idx
);
278 jsonw_string_field(rd
->jw
, "ifname", name
);
280 pr_out("dev %s ", name
);
284 static void print_link(struct rd
*rd
, uint32_t idx
, const char *name
,
285 uint32_t port
, struct nlattr
**nla_line
)
287 if (rd
->json_output
) {
288 jsonw_uint_field(rd
->jw
, "ifindex", idx
);
290 if (nla_line
[RDMA_NLDEV_ATTR_PORT_INDEX
])
291 jsonw_uint_field(rd
->jw
, "port", port
);
293 jsonw_string_field(rd
->jw
, "ifname", name
);
295 if (nla_line
[RDMA_NLDEV_ATTR_PORT_INDEX
])
296 pr_out("link %s/%u ", name
, port
);
298 pr_out("link %s/- ", name
);
302 static char *get_task_name(uint32_t pid
)
307 if (asprintf(&comm
, "/proc/%d/comm", pid
) < 0)
310 f
= fopen(comm
, "r");
315 if (fscanf(f
, "%ms\n", &comm
) != 1)
323 static int res_qp_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
325 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
326 struct nlattr
*nla_table
, *nla_entry
;
327 struct rd
*rd
= data
;
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
])
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
];
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;
349 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
350 if (err
!= MNL_CB_OK
)
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
])) {
362 if (nla_line
[RDMA_NLDEV_ATTR_PORT_INDEX
])
363 port
= mnl_attr_get_u32(nla_line
[RDMA_NLDEV_ATTR_PORT_INDEX
]);
365 if (port
!= rd
->port_idx
)
368 lqpn
= mnl_attr_get_u32(nla_line
[RDMA_NLDEV_ATTR_RES_LQPN
]);
369 if (rd_check_is_filtered(rd
, "lqpn", lqpn
))
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
))
377 if (rd_check_is_key_exist(rd
, "rqpn"))
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
))
386 if (rd_check_is_key_exist(rd
, "rq-psn"))
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
))
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
)))
399 if (rd_check_is_key_exist(rd
, "path-mig-state"))
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
)))
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
)))
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
);
416 if (rd_check_is_filtered(rd
, "pid", pid
)) {
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
]);
426 jsonw_start_array(rd
->jw
);
428 print_link(rd
, idx
, name
, port
, nla_line
);
430 print_lqpn(rd
, lqpn
);
431 print_rqpn(rd
, rqpn
, nla_line
);
433 print_type(rd
, type
);
434 print_state(rd
, state
);
436 print_rqpsn(rd
, rq_psn
, nla_line
);
437 print_sqpsn(rd
, sq_psn
);
439 print_pathmig(rd
, path_mig_state
, nla_line
);
441 print_comm(rd
, comm
, nla_line
);
443 if (nla_line
[RDMA_NLDEV_ATTR_RES_PID
])
446 print_driver_table(rd
, nla_line
[RDMA_NLDEV_ATTR_DRIVER
]);
452 static void print_qp_type(struct rd
*rd
, uint32_t val
)
455 jsonw_string_field(rd
->jw
, "qp-type",
456 qp_types_to_str(val
));
458 pr_out("qp-type %s ", qp_types_to_str(val
));
461 static const char *cm_id_state_to_str(uint8_t idx
)
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" };
468 if (idx
< ARRAY_SIZE(cm_id_states_str
))
469 return cm_id_states_str
[idx
];
473 static const char *cm_id_ps_to_str(uint32_t ps
)
489 static void print_cm_id_state(struct rd
*rd
, uint8_t state
)
491 if (rd
->json_output
) {
492 jsonw_string_field(rd
->jw
, "state", cm_id_state_to_str(state
));
495 pr_out("state %s ", cm_id_state_to_str(state
));
498 static void print_ps(struct rd
*rd
, uint32_t ps
)
500 if (rd
->json_output
) {
501 jsonw_string_field(rd
->jw
, "ps", cm_id_ps_to_str(ps
));
504 pr_out("ps %s ", cm_id_ps_to_str(ps
));
507 static void print_ipaddr(struct rd
*rd
, const char *key
, char *addrstr
,
510 if (rd
->json_output
) {
511 int name_size
= INET6_ADDRSTRLEN
+strlen(":65535");
512 char json_name
[name_size
];
514 snprintf(json_name
, name_size
, "%s:%u", addrstr
, port
);
515 jsonw_string_field(rd
->jw
, key
, json_name
);
518 pr_out("%s %s:%u ", key
, addrstr
, port
);
521 static int ss_ntop(struct nlattr
*nla_line
, char *addr_str
, uint16_t *port
)
523 struct __kernel_sockaddr_storage
*addr
;
525 addr
= (struct __kernel_sockaddr_storage
*)
526 mnl_attr_get_payload(nla_line
);
527 switch (addr
->ss_family
) {
529 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
531 if (!inet_ntop(AF_INET
, (const void *)&sin
->sin_addr
, addr_str
,
534 *port
= ntohs(sin
->sin_port
);
538 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
540 if (!inet_ntop(AF_INET6
, (const void *)&sin6
->sin6_addr
,
541 addr_str
, INET6_ADDRSTRLEN
))
543 *port
= ntohs(sin6
->sin6_port
);
552 static int res_cm_id_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
554 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
555 struct nlattr
*nla_table
, *nla_entry
;
556 struct rd
*rd
= data
;
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
])
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
;
580 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
581 if (err
!= MNL_CB_OK
)
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
])) {
591 if (nla_line
[RDMA_NLDEV_ATTR_PORT_INDEX
])
592 port
= mnl_attr_get_u32(
593 nla_line
[RDMA_NLDEV_ATTR_PORT_INDEX
]);
595 if (port
&& port
!= rd
->port_idx
)
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
))
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
)))
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
)))
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
)))
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
))
625 if (rd_check_is_string_filtered(rd
, "src-addr",
628 if (rd_check_is_filtered(rd
, "src-port", src_port
))
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
))
636 if (rd_check_is_string_filtered(rd
, "dst-addr",
639 if (rd_check_is_filtered(rd
, "dst-port", dst_port
))
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
);
649 if (rd_check_is_filtered(rd
, "pid", pid
)) {
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
]);
661 jsonw_start_array(rd
->jw
);
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
);
671 print_comm(rd
, comm
, nla_line
);
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
);
678 if (nla_line
[RDMA_NLDEV_ATTR_RES_PID
])
681 print_driver_table(rd
, nla_line
[RDMA_NLDEV_ATTR_DRIVER
]);
687 static void print_cqe(struct rd
*rd
, uint32_t val
)
690 jsonw_uint_field(rd
->jw
, "cqe", val
);
692 pr_out("cqe %u ", val
);
695 static void print_users(struct rd
*rd
, uint64_t val
)
698 jsonw_uint_field(rd
->jw
, "users", val
);
700 pr_out("users %" PRIu64
" ", val
);
703 static const char *poll_ctx_to_str(uint8_t idx
)
705 static const char * const cm_id_states_str
[] = {
706 "DIRECT", "SOFTIRQ", "WORKQUEUE"};
708 if (idx
< ARRAY_SIZE(cm_id_states_str
))
709 return cm_id_states_str
[idx
];
713 static void print_poll_ctx(struct rd
*rd
, uint8_t poll_ctx
)
715 if (rd
->json_output
) {
716 jsonw_string_field(rd
->jw
, "poll-ctx",
717 poll_ctx_to_str(poll_ctx
));
720 pr_out("poll-ctx %s ", poll_ctx_to_str(poll_ctx
));
723 static int res_cq_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
725 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
726 struct nlattr
*nla_table
, *nla_entry
;
727 struct rd
*rd
= data
;
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
])
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
];
741 mnl_attr_for_each_nested(nla_entry
, nla_table
) {
742 struct nlattr
*nla_line
[RDMA_NLDEV_ATTR_MAX
] = {};
745 uint8_t poll_ctx
= 0;
750 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
751 if (err
!= MNL_CB_OK
)
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
])) {
761 cqe
= mnl_attr_get_u32(nla_line
[RDMA_NLDEV_ATTR_RES_CQE
]);
763 users
= mnl_attr_get_u64(nla_line
[RDMA_NLDEV_ATTR_RES_USECNT
]);
764 if (rd_check_is_filtered(rd
, "users", users
))
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
)))
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
);
781 if (rd_check_is_filtered(rd
, "pid", pid
)) {
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
]);
792 jsonw_start_array(rd
->jw
);
794 print_dev(rd
, idx
, name
);
796 print_users(rd
, users
);
797 if (nla_line
[RDMA_NLDEV_ATTR_RES_POLL_CTX
])
798 print_poll_ctx(rd
, poll_ctx
);
800 print_comm(rd
, comm
, nla_line
);
802 if (nla_line
[RDMA_NLDEV_ATTR_RES_PID
])
805 print_driver_table(rd
, nla_line
[RDMA_NLDEV_ATTR_DRIVER
]);
811 static void print_key(struct rd
*rd
, const char *name
, uint32_t val
)
814 jsonw_xint_field(rd
->jw
, name
, val
);
816 pr_out("%s 0x%x ", name
, val
);
819 static void print_iova(struct rd
*rd
, uint64_t val
)
822 jsonw_xint_field(rd
->jw
, "iova", val
);
824 pr_out("iova 0x%" PRIx64
" ", val
);
827 static void print_mrlen(struct rd
*rd
, uint64_t val
)
830 jsonw_uint_field(rd
->jw
, "mrlen", val
);
832 pr_out("mrlen %" PRIu64
" ", val
);
835 static int res_mr_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
837 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
838 struct nlattr
*nla_table
, *nla_entry
;
839 struct rd
*rd
= data
;
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
])
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
];
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
;
861 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
862 if (err
!= MNL_CB_OK
)
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
])) {
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
]);
881 mrlen
= mnl_attr_get_u64(nla_line
[RDMA_NLDEV_ATTR_RES_MRLEN
]);
882 if (rd_check_is_filtered(rd
, "mrlen", mrlen
))
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
);
891 if (rd_check_is_filtered(rd
, "pid", pid
)) {
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
]);
902 jsonw_start_array(rd
->jw
);
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
);
913 print_comm(rd
, comm
, nla_line
);
915 if (nla_line
[RDMA_NLDEV_ATTR_RES_PID
])
918 print_driver_table(rd
, nla_line
[RDMA_NLDEV_ATTR_DRIVER
]);
924 static int res_pd_parse_cb(const struct nlmsghdr
*nlh
, void *data
)
926 struct nlattr
*tb
[RDMA_NLDEV_ATTR_MAX
] = {};
927 struct nlattr
*nla_table
, *nla_entry
;
928 struct rd
*rd
= data
;
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
])
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
];
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
] = {};
950 err
= mnl_attr_parse_nested(nla_entry
, rd_attr_cb
, nla_line
);
951 if (err
!= MNL_CB_OK
)
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
])) {
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
]);
964 users
= mnl_attr_get_u64(nla_line
[RDMA_NLDEV_ATTR_RES_USECNT
]);
965 if (rd_check_is_filtered(rd
, "users", users
))
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
]);
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
);
978 if (rd_check_is_filtered(rd
, "pid", pid
))
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
]);
987 jsonw_start_array(rd
->jw
);
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
);
996 print_comm(rd
, comm
, nla_line
);
998 if (nla_line
[RDMA_NLDEV_ATTR_RES_PID
])
1001 print_driver_table(rd
, nla_line
[RDMA_NLDEV_ATTR_DRIVER
]);
1007 RES_FUNC(res_no_args
, RDMA_NLDEV_CMD_RES_GET
, NULL
, true);
1010 filters qp_valid_filters
[MAX_NUMBER_OF_FILTERS
] = {{ .name
= "link",
1011 .is_number
= false },
1013 .is_number
= true },
1015 .is_number
= true },
1017 .is_number
= true },
1019 .is_number
= true },
1021 .is_number
= true },
1023 .is_number
= false },
1024 { .name
= "path-mig-state",
1025 .is_number
= false },
1027 .is_number
= false } };
1029 RES_FUNC(res_qp
, RDMA_NLDEV_CMD_RES_QP_GET
, qp_valid_filters
, false);
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 }
1047 RES_FUNC(res_cm_id
, RDMA_NLDEV_CMD_RES_CM_ID_GET
, cm_id_valid_filters
, false);
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 }
1057 RES_FUNC(res_cq
, RDMA_NLDEV_CMD_RES_CQ_GET
, cq_valid_filters
, true);
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 }
1068 RES_FUNC(res_mr
, RDMA_NLDEV_CMD_RES_MR_GET
, mr_valid_filters
, true);
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 }
1077 RES_FUNC(res_pd
, RDMA_NLDEV_CMD_RES_PD_GET
, pd_valid_filters
, true);
1079 static int res_show(struct rd
*rd
)
1081 const struct rd_cmd cmds
[] = {
1082 { NULL
, res_no_args
},
1084 { "cm_id", res_cm_id
},
1092 * Special case to support "rdma res show DEV_NAME"
1094 if (rd_argc(rd
) == 1 && dev_map_lookup(rd
, false))
1095 return rd_exec_dev(rd
, _res_no_args
);
1097 return rd_exec_cmd(rd
, cmds
, "parameter");
1100 int cmd_res(struct rd
*rd
)
1102 const struct rd_cmd cmds
[] = {
1104 { "show", res_show
},
1105 { "list", res_show
},
1106 { "help", res_help
},
1110 return rd_exec_cmd(rd
, cmds
, "resource command");