]> git.proxmox.com Git - mirror_iproute2.git/blame - rdma/res-cq.c
rdma: Simplify code to reuse existing functions
[mirror_iproute2.git] / rdma / res-cq.c
CommitLineData
fcdd2e0c
LR
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/*
3 * res-cq.c RDMA tool
4 * Authors: Leon Romanovsky <leonro@mellanox.com>
5 */
6
7#include "res.h"
8#include <inttypes.h>
9
fcdd2e0c
LR
10static const char *poll_ctx_to_str(uint8_t idx)
11{
12 static const char * const cm_id_states_str[] = {
13 "DIRECT", "SOFTIRQ", "WORKQUEUE", "UNBOUND_WORKQUEUE"};
14
15 if (idx < ARRAY_SIZE(cm_id_states_str))
16 return cm_id_states_str[idx];
17 return "UNKNOWN";
18}
19
20static void print_poll_ctx(struct rd *rd, uint8_t poll_ctx)
21{
22 if (rd->json_output) {
23 jsonw_string_field(rd->jw, "poll-ctx",
24 poll_ctx_to_str(poll_ctx));
25 return;
26 }
27 pr_out("poll-ctx %s ", poll_ctx_to_str(poll_ctx));
28}
29
30int res_cq_parse_cb(const struct nlmsghdr *nlh, void *data)
31{
32 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
33 struct nlattr *nla_table, *nla_entry;
34 struct rd *rd = data;
35 const char *name;
36 uint32_t idx;
37
38 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
39 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
40 !tb[RDMA_NLDEV_ATTR_RES_CQ])
41 return MNL_CB_ERROR;
42
43 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
44 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
45 nla_table = tb[RDMA_NLDEV_ATTR_RES_CQ];
46
47 mnl_attr_for_each_nested(nla_entry, nla_table) {
48 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
49 char *comm = NULL;
50 uint32_t pid = 0;
51 uint8_t poll_ctx = 0;
52 uint32_t ctxn = 0;
53 uint32_t cqn = 0;
54 uint64_t users;
55 uint32_t cqe;
56 int err;
57
58 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
59 if (err != MNL_CB_OK)
60 return MNL_CB_ERROR;
61
62 if (!nla_line[RDMA_NLDEV_ATTR_RES_CQE] ||
63 !nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
64 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
65 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
66 return MNL_CB_ERROR;
67 }
68
69 cqe = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CQE]);
70
71 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
72 if (rd_check_is_filtered(rd, "users", users))
73 continue;
74
75 if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]) {
76 poll_ctx = mnl_attr_get_u8(
77 nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]);
78 if (rd_check_is_string_filtered(
79 rd, "poll-ctx", poll_ctx_to_str(poll_ctx)))
80 continue;
81 }
82
83 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
84 pid = mnl_attr_get_u32(
85 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
86 comm = get_task_name(pid);
87 }
88
89 if (rd_check_is_filtered(rd, "pid", pid)) {
90 free(comm);
91 continue;
92 }
93
94 if (nla_line[RDMA_NLDEV_ATTR_RES_CQN])
95 cqn = mnl_attr_get_u32(
96 nla_line[RDMA_NLDEV_ATTR_RES_CQN]);
97 if (rd_check_is_filtered(rd, "cqn", cqn))
98 continue;
99
100 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
101 ctxn = mnl_attr_get_u32(
102 nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
103 if (rd_check_is_filtered(rd, "ctxn", ctxn))
104 continue;
105
106 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
107 /* discard const from mnl_attr_get_str */
108 comm = (char *)mnl_attr_get_str(
109 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
110
111 if (rd->json_output)
112 jsonw_start_array(rd->jw);
113
114 print_dev(rd, idx, name);
05846c9c
LR
115 res_print_uint(rd, "cqe", cqe);
116 res_print_uint(rd, "users", users);
fcdd2e0c
LR
117 if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX])
118 print_poll_ctx(rd, poll_ctx);
05846c9c 119 res_print_uint(rd, "pid", pid);
fcdd2e0c
LR
120 print_comm(rd, comm, nla_line);
121
122 if (nla_line[RDMA_NLDEV_ATTR_RES_CQN])
123 res_print_uint(rd, "cqn", cqn);
124 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
125 res_print_uint(rd, "ctxn", ctxn);
126
127 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
128 free(comm);
129
130 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
131 newline(rd);
132 }
133 return MNL_CB_OK;
134}
135