]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res-qp.c
rdma: Simplify code to reuse existing functions
[mirror_iproute2.git] / rdma / res-qp.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * res-qp.c RDMA tool
4 * Authors: Leon Romanovsky <leonro@mellanox.com>
5 */
6
7 #include "res.h"
8 #include <inttypes.h>
9
10 static const char *path_mig_to_str(uint8_t idx)
11 {
12 static const char *const path_mig_str[] = { "MIGRATED", "REARM",
13 "ARMED" };
14
15 if (idx < ARRAY_SIZE(path_mig_str))
16 return path_mig_str[idx];
17 return "UNKNOWN";
18 }
19
20 static const char *qp_states_to_str(uint8_t idx)
21 {
22 static const char *const qp_states_str[] = { "RESET", "INIT", "RTR",
23 "RTS", "SQD", "SQE",
24 "ERR" };
25
26 if (idx < ARRAY_SIZE(qp_states_str))
27 return qp_states_str[idx];
28 return "UNKNOWN";
29 }
30
31 static void print_rqpn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
32 {
33 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQPN])
34 return;
35
36 if (rd->json_output)
37 jsonw_uint_field(rd->jw, "rqpn", val);
38 else
39 pr_out("rqpn %u ", val);
40 }
41
42 static void print_type(struct rd *rd, uint32_t val)
43 {
44 if (rd->json_output)
45 jsonw_string_field(rd->jw, "type", qp_types_to_str(val));
46 else
47 pr_out("type %s ", qp_types_to_str(val));
48 }
49
50 static void print_state(struct rd *rd, uint32_t val)
51 {
52 if (rd->json_output)
53 jsonw_string_field(rd->jw, "state", qp_states_to_str(val));
54 else
55 pr_out("state %s ", qp_states_to_str(val));
56 }
57
58 static void print_rqpsn(struct rd *rd, uint32_t val, struct nlattr **nla_line)
59 {
60 if (!nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN])
61 return;
62
63 if (rd->json_output)
64 jsonw_uint_field(rd->jw, "rq-psn", val);
65 else
66 pr_out("rq-psn %u ", val);
67 }
68
69 static void print_pathmig(struct rd *rd, uint32_t val, struct nlattr **nla_line)
70 {
71 if (!nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE])
72 return;
73
74 if (rd->json_output)
75 jsonw_string_field(rd->jw, "path-mig-state",
76 path_mig_to_str(val));
77 else
78 pr_out("path-mig-state %s ", path_mig_to_str(val));
79 }
80
81 int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data)
82 {
83 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
84 struct nlattr *nla_table, *nla_entry;
85 struct rd *rd = data;
86 const char *name;
87 uint32_t idx;
88
89 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
90 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
91 !tb[RDMA_NLDEV_ATTR_RES_QP])
92 return MNL_CB_ERROR;
93
94 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
95 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
96 nla_table = tb[RDMA_NLDEV_ATTR_RES_QP];
97
98 mnl_attr_for_each_nested(nla_entry, nla_table) {
99 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
100 uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn;
101 uint8_t type, state, path_mig_state = 0;
102 uint32_t port = 0, pid = 0;
103 uint32_t pdn = 0;
104 char *comm = NULL;
105 int err;
106
107 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
108 if (err != MNL_CB_OK)
109 return MNL_CB_ERROR;
110
111 if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] ||
112 !nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] ||
113 !nla_line[RDMA_NLDEV_ATTR_RES_TYPE] ||
114 !nla_line[RDMA_NLDEV_ATTR_RES_STATE] ||
115 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
116 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
117 return MNL_CB_ERROR;
118 }
119
120 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
121 port = mnl_attr_get_u32(
122 nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
123
124 if (port != rd->port_idx)
125 continue;
126
127 lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
128 if (rd_check_is_filtered(rd, "lqpn", lqpn))
129 continue;
130
131 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
132 pdn = mnl_attr_get_u32(
133 nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
134 if (rd_check_is_filtered(rd, "pdn", pdn))
135 continue;
136
137 if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) {
138 rqpn = mnl_attr_get_u32(
139 nla_line[RDMA_NLDEV_ATTR_RES_RQPN]);
140 if (rd_check_is_filtered(rd, "rqpn", rqpn))
141 continue;
142 } else {
143 if (rd_check_is_key_exist(rd, "rqpn"))
144 continue;
145 }
146
147 if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) {
148 rq_psn = mnl_attr_get_u32(
149 nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]);
150 if (rd_check_is_filtered(rd, "rq-psn", rq_psn))
151 continue;
152 } else {
153 if (rd_check_is_key_exist(rd, "rq-psn"))
154 continue;
155 }
156
157 sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]);
158 if (rd_check_is_filtered(rd, "sq-psn", sq_psn))
159 continue;
160
161 if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) {
162 path_mig_state = mnl_attr_get_u8(
163 nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]);
164 if (rd_check_is_string_filtered(
165 rd, "path-mig-state",
166 path_mig_to_str(path_mig_state)))
167 continue;
168 } else {
169 if (rd_check_is_key_exist(rd, "path-mig-state"))
170 continue;
171 }
172
173 type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]);
174 if (rd_check_is_string_filtered(rd, "type",
175 qp_types_to_str(type)))
176 continue;
177
178 state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]);
179 if (rd_check_is_string_filtered(rd, "state",
180 qp_states_to_str(state)))
181 continue;
182
183 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
184 pid = mnl_attr_get_u32(
185 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
186 comm = get_task_name(pid);
187 }
188
189 if (rd_check_is_filtered(rd, "pid", pid)) {
190 free(comm);
191 continue;
192 }
193
194 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
195 /* discard const from mnl_attr_get_str */
196 comm = (char *)mnl_attr_get_str(
197 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
198
199 if (rd->json_output)
200 jsonw_start_array(rd->jw);
201
202 print_link(rd, idx, name, port, nla_line);
203
204 res_print_uint(rd, "lqpn", lqpn);
205 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
206 res_print_uint(rd, "pdn", pdn);
207 print_rqpn(rd, rqpn, nla_line);
208
209 print_type(rd, type);
210 print_state(rd, state);
211
212 print_rqpsn(rd, rq_psn, nla_line);
213 res_print_uint(rd, "sq-psn", sq_psn);
214
215 print_pathmig(rd, path_mig_state, nla_line);
216 res_print_uint(rd, "pid", pid);
217 print_comm(rd, comm, nla_line);
218
219 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
220 free(comm);
221
222 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
223 newline(rd);
224 }
225 return MNL_CB_OK;
226 }