]>
Commit | Line | Data |
---|---|---|
923aa825 LR |
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"); | |
4ee770ee | 19 | pr_out(" resource show [qp|cm_id|pd|mr|cq]\n"); |
8ecac46a LR |
20 | pr_out(" resource show qp link [DEV/PORT]\n"); |
21 | pr_out(" resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n"); | |
9a362cc7 SW |
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"); | |
b0b8e32c SW |
24 | pr_out(" resource show cq link [DEV/PORT]\n"); |
25 | pr_out(" resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n"); | |
4ee770ee LR |
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"); | |
923aa825 LR |
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 | ||
8ecac46a LR |
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 | ||
b0b8e32c SW |
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 | ||
8ecac46a LR |
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 | ||
96f59e7f LR |
323 | static void print_key(struct rd *rd, const char *name, uint64_t val) |
324 | { | |
325 | if (rd->json_output) | |
326 | jsonw_xint_field(rd->jw, name, val); | |
327 | else | |
328 | pr_out("%s 0x%" PRIx64 " ", name, val); | |
329 | } | |
330 | ||
331 | static void res_print_uint(struct rd *rd, const char *name, uint64_t val) | |
332 | { | |
333 | if (rd->json_output) | |
334 | jsonw_uint_field(rd->jw, name, val); | |
335 | else | |
336 | pr_out("%s %" PRIu64 " ", name, val); | |
337 | } | |
338 | ||
8ecac46a LR |
339 | static int res_qp_parse_cb(const struct nlmsghdr *nlh, void *data) |
340 | { | |
341 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
342 | struct nlattr *nla_table, *nla_entry; | |
343 | struct rd *rd = data; | |
344 | const char *name; | |
345 | uint32_t idx; | |
346 | ||
347 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
348 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || | |
349 | !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
350 | !tb[RDMA_NLDEV_ATTR_RES_QP]) | |
351 | return MNL_CB_ERROR; | |
352 | ||
353 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
354 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
355 | nla_table = tb[RDMA_NLDEV_ATTR_RES_QP]; | |
356 | ||
357 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
358 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
359 | uint32_t lqpn, rqpn = 0, rq_psn = 0, sq_psn; | |
360 | uint8_t type, state, path_mig_state = 0; | |
361 | uint32_t port = 0, pid = 0; | |
96f59e7f | 362 | uint32_t pdn = 0; |
8ecac46a LR |
363 | char *comm = NULL; |
364 | int err; | |
365 | ||
366 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
367 | if (err != MNL_CB_OK) | |
368 | return MNL_CB_ERROR; | |
369 | ||
370 | if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN] || | |
371 | !nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN] || | |
372 | !nla_line[RDMA_NLDEV_ATTR_RES_TYPE] || | |
373 | !nla_line[RDMA_NLDEV_ATTR_RES_STATE] || | |
374 | (!nla_line[RDMA_NLDEV_ATTR_RES_PID] && | |
375 | !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) { | |
376 | return MNL_CB_ERROR; | |
377 | } | |
378 | ||
379 | if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) | |
380 | port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
381 | ||
382 | if (port != rd->port_idx) | |
383 | continue; | |
384 | ||
385 | lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]); | |
386 | if (rd_check_is_filtered(rd, "lqpn", lqpn)) | |
387 | continue; | |
388 | ||
96f59e7f LR |
389 | if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
390 | pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]); | |
391 | if (rd_check_is_filtered(rd, "pdn", pdn)) | |
392 | continue; | |
393 | ||
8ecac46a LR |
394 | if (nla_line[RDMA_NLDEV_ATTR_RES_RQPN]) { |
395 | rqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQPN]); | |
396 | if (rd_check_is_filtered(rd, "rqpn", rqpn)) | |
397 | continue; | |
398 | } else { | |
399 | if (rd_check_is_key_exist(rd, "rqpn")) | |
400 | continue; | |
401 | } | |
402 | ||
403 | if (nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]) { | |
404 | rq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RQ_PSN]); | |
405 | if (rd_check_is_filtered(rd, "rq-psn", rq_psn)) | |
406 | continue; | |
407 | } else { | |
408 | if (rd_check_is_key_exist(rd, "rq-psn")) | |
409 | continue; | |
410 | } | |
411 | ||
412 | sq_psn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_SQ_PSN]); | |
413 | if (rd_check_is_filtered(rd, "sq-psn", sq_psn)) | |
414 | continue; | |
415 | ||
416 | if (nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]) { | |
417 | path_mig_state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]); | |
418 | if (rd_check_is_string_filtered(rd, "path-mig-state", path_mig_to_str(path_mig_state))) | |
419 | continue; | |
420 | } else { | |
421 | if (rd_check_is_key_exist(rd, "path-mig-state")) | |
422 | continue; | |
423 | } | |
424 | ||
425 | type = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_TYPE]); | |
426 | if (rd_check_is_string_filtered(rd, "type", qp_types_to_str(type))) | |
427 | continue; | |
428 | ||
429 | state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]); | |
430 | if (rd_check_is_string_filtered(rd, "state", qp_states_to_str(state))) | |
431 | continue; | |
432 | ||
433 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { | |
434 | pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]); | |
435 | comm = get_task_name(pid); | |
436 | } | |
437 | ||
4ac152d0 LR |
438 | if (rd_check_is_filtered(rd, "pid", pid)) { |
439 | free(comm); | |
8ecac46a | 440 | continue; |
4ac152d0 | 441 | } |
8ecac46a LR |
442 | |
443 | if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) | |
444 | /* discard const from mnl_attr_get_str */ | |
445 | comm = (char *)mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); | |
446 | ||
447 | if (rd->json_output) | |
448 | jsonw_start_array(rd->jw); | |
449 | ||
450 | print_link(rd, idx, name, port, nla_line); | |
451 | ||
452 | print_lqpn(rd, lqpn); | |
96f59e7f LR |
453 | if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
454 | res_print_uint(rd, "pdn", pdn); | |
8ecac46a LR |
455 | print_rqpn(rd, rqpn, nla_line); |
456 | ||
457 | print_type(rd, type); | |
458 | print_state(rd, state); | |
459 | ||
460 | print_rqpsn(rd, rq_psn, nla_line); | |
461 | print_sqpsn(rd, sq_psn); | |
462 | ||
463 | print_pathmig(rd, path_mig_state, nla_line); | |
464 | print_pid(rd, pid); | |
465 | print_comm(rd, comm, nla_line); | |
466 | ||
467 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) | |
468 | free(comm); | |
469 | ||
33115275 SW |
470 | print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
471 | newline(rd); | |
8ecac46a LR |
472 | } |
473 | return MNL_CB_OK; | |
474 | } | |
475 | ||
9a362cc7 SW |
476 | static void print_qp_type(struct rd *rd, uint32_t val) |
477 | { | |
478 | if (rd->json_output) | |
479 | jsonw_string_field(rd->jw, "qp-type", | |
480 | qp_types_to_str(val)); | |
481 | else | |
482 | pr_out("qp-type %s ", qp_types_to_str(val)); | |
483 | } | |
484 | ||
485 | static const char *cm_id_state_to_str(uint8_t idx) | |
486 | { | |
487 | static const char * const cm_id_states_str[] = { | |
488 | "IDLE", "ADDR_QUERY", "ADDR_RESOLVED", "ROUTE_QUERY", | |
489 | "ROUTE_RESOLVED", "CONNECT", "DISCONNECT", "ADDR_BOUND", | |
490 | "LISTEN", "DEVICE_REMOVAL", "DESTROYING" }; | |
491 | ||
492 | if (idx < ARRAY_SIZE(cm_id_states_str)) | |
493 | return cm_id_states_str[idx]; | |
494 | return "UNKNOWN"; | |
495 | } | |
496 | ||
497 | static const char *cm_id_ps_to_str(uint32_t ps) | |
498 | { | |
499 | switch (ps) { | |
500 | case RDMA_PS_IPOIB: | |
501 | return "IPoIB"; | |
502 | case RDMA_PS_IB: | |
503 | return "IPoIB"; | |
504 | case RDMA_PS_TCP: | |
505 | return "TCP"; | |
506 | case RDMA_PS_UDP: | |
507 | return "UDP"; | |
508 | default: | |
509 | return "---"; | |
510 | } | |
511 | } | |
512 | ||
513 | static void print_cm_id_state(struct rd *rd, uint8_t state) | |
514 | { | |
515 | if (rd->json_output) { | |
516 | jsonw_string_field(rd->jw, "state", cm_id_state_to_str(state)); | |
517 | return; | |
518 | } | |
519 | pr_out("state %s ", cm_id_state_to_str(state)); | |
520 | } | |
521 | ||
522 | static void print_ps(struct rd *rd, uint32_t ps) | |
523 | { | |
524 | if (rd->json_output) { | |
525 | jsonw_string_field(rd->jw, "ps", cm_id_ps_to_str(ps)); | |
526 | return; | |
527 | } | |
528 | pr_out("ps %s ", cm_id_ps_to_str(ps)); | |
529 | } | |
530 | ||
531 | static void print_ipaddr(struct rd *rd, const char *key, char *addrstr, | |
532 | uint16_t port) | |
533 | { | |
534 | if (rd->json_output) { | |
535 | int name_size = INET6_ADDRSTRLEN+strlen(":65535"); | |
536 | char json_name[name_size]; | |
537 | ||
538 | snprintf(json_name, name_size, "%s:%u", addrstr, port); | |
539 | jsonw_string_field(rd->jw, key, json_name); | |
540 | return; | |
541 | } | |
542 | pr_out("%s %s:%u ", key, addrstr, port); | |
543 | } | |
544 | ||
545 | static int ss_ntop(struct nlattr *nla_line, char *addr_str, uint16_t *port) | |
546 | { | |
547 | struct __kernel_sockaddr_storage *addr; | |
548 | ||
549 | addr = (struct __kernel_sockaddr_storage *) | |
550 | mnl_attr_get_payload(nla_line); | |
551 | switch (addr->ss_family) { | |
552 | case AF_INET: { | |
553 | struct sockaddr_in *sin = (struct sockaddr_in *)addr; | |
554 | ||
555 | if (!inet_ntop(AF_INET, (const void *)&sin->sin_addr, addr_str, | |
556 | INET6_ADDRSTRLEN)) | |
557 | return -EINVAL; | |
558 | *port = ntohs(sin->sin_port); | |
559 | break; | |
560 | } | |
561 | case AF_INET6: { | |
562 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; | |
563 | ||
564 | if (!inet_ntop(AF_INET6, (const void *)&sin6->sin6_addr, | |
565 | addr_str, INET6_ADDRSTRLEN)) | |
566 | return -EINVAL; | |
567 | *port = ntohs(sin6->sin6_port); | |
568 | break; | |
569 | } | |
570 | default: | |
571 | return -EINVAL; | |
572 | } | |
573 | return 0; | |
574 | } | |
575 | ||
576 | static int res_cm_id_parse_cb(const struct nlmsghdr *nlh, void *data) | |
577 | { | |
578 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
579 | struct nlattr *nla_table, *nla_entry; | |
580 | struct rd *rd = data; | |
581 | const char *name; | |
582 | int idx; | |
583 | ||
584 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
585 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || | |
586 | !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
587 | !tb[RDMA_NLDEV_ATTR_RES_CM_ID]) | |
588 | return MNL_CB_ERROR; | |
589 | ||
590 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
591 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
592 | nla_table = tb[RDMA_NLDEV_ATTR_RES_CM_ID]; | |
593 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
594 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
595 | char src_addr_str[INET6_ADDRSTRLEN]; | |
596 | char dst_addr_str[INET6_ADDRSTRLEN]; | |
597 | uint16_t src_port, dst_port; | |
598 | uint32_t port = 0, pid = 0; | |
599 | uint8_t type = 0, state; | |
600 | uint32_t lqpn = 0, ps; | |
1dc03586 | 601 | uint32_t cm_idn = 0; |
9a362cc7 SW |
602 | char *comm = NULL; |
603 | int err; | |
604 | ||
605 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
606 | if (err != MNL_CB_OK) | |
607 | return -EINVAL; | |
608 | ||
609 | if (!nla_line[RDMA_NLDEV_ATTR_RES_STATE] || | |
610 | !nla_line[RDMA_NLDEV_ATTR_RES_PS] || | |
611 | (!nla_line[RDMA_NLDEV_ATTR_RES_PID] && | |
612 | !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) { | |
613 | return MNL_CB_ERROR; | |
614 | } | |
615 | ||
616 | if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) | |
617 | port = mnl_attr_get_u32( | |
618 | nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
619 | ||
620 | if (port && port != rd->port_idx) | |
621 | continue; | |
622 | ||
623 | if (nla_line[RDMA_NLDEV_ATTR_RES_LQPN]) { | |
624 | lqpn = mnl_attr_get_u32( | |
625 | nla_line[RDMA_NLDEV_ATTR_RES_LQPN]); | |
626 | if (rd_check_is_filtered(rd, "lqpn", lqpn)) | |
627 | continue; | |
628 | } | |
629 | if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE]) { | |
630 | type = mnl_attr_get_u8( | |
631 | nla_line[RDMA_NLDEV_ATTR_RES_TYPE]); | |
632 | if (rd_check_is_string_filtered(rd, "qp-type", | |
633 | qp_types_to_str(type))) | |
634 | continue; | |
635 | } | |
636 | ||
637 | ps = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PS]); | |
638 | if (rd_check_is_string_filtered(rd, "ps", cm_id_ps_to_str(ps))) | |
639 | continue; | |
640 | ||
641 | state = mnl_attr_get_u8(nla_line[RDMA_NLDEV_ATTR_RES_STATE]); | |
642 | if (rd_check_is_string_filtered(rd, "state", | |
643 | cm_id_state_to_str(state))) | |
644 | continue; | |
645 | ||
646 | if (nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR]) { | |
647 | if (ss_ntop(nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR], | |
648 | src_addr_str, &src_port)) | |
649 | continue; | |
650 | if (rd_check_is_string_filtered(rd, "src-addr", | |
651 | src_addr_str)) | |
652 | continue; | |
cdefe1d8 PS |
653 | if (rd_check_is_filtered(rd, "src-port", src_port)) |
654 | continue; | |
9a362cc7 SW |
655 | } |
656 | ||
657 | if (nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR]) { | |
658 | if (ss_ntop(nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR], | |
659 | dst_addr_str, &dst_port)) | |
660 | continue; | |
661 | if (rd_check_is_string_filtered(rd, "dst-addr", | |
662 | dst_addr_str)) | |
663 | continue; | |
cdefe1d8 PS |
664 | if (rd_check_is_filtered(rd, "dst-port", dst_port)) |
665 | continue; | |
9a362cc7 SW |
666 | } |
667 | ||
9a362cc7 SW |
668 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { |
669 | pid = mnl_attr_get_u32( | |
670 | nla_line[RDMA_NLDEV_ATTR_RES_PID]); | |
671 | comm = get_task_name(pid); | |
672 | } | |
673 | ||
674 | if (rd_check_is_filtered(rd, "pid", pid)) { | |
675 | free(comm); | |
676 | continue; | |
677 | } | |
678 | ||
1dc03586 LR |
679 | if (nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN]) |
680 | cm_idn = mnl_attr_get_u32( | |
681 | nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN]); | |
682 | if (rd_check_is_filtered(rd, "cm-idn", cm_idn)) | |
683 | continue; | |
684 | ||
9a362cc7 SW |
685 | if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) { |
686 | /* discard const from mnl_attr_get_str */ | |
687 | comm = (char *)mnl_attr_get_str( | |
688 | nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); | |
689 | } | |
690 | ||
691 | if (rd->json_output) | |
692 | jsonw_start_array(rd->jw); | |
693 | ||
694 | print_link(rd, idx, name, port, nla_line); | |
695 | if (nla_line[RDMA_NLDEV_ATTR_RES_LQPN]) | |
696 | print_lqpn(rd, lqpn); | |
697 | if (nla_line[RDMA_NLDEV_ATTR_RES_TYPE]) | |
698 | print_qp_type(rd, type); | |
699 | print_cm_id_state(rd, state); | |
700 | print_ps(rd, ps); | |
701 | print_pid(rd, pid); | |
702 | print_comm(rd, comm, nla_line); | |
1dc03586 LR |
703 | if (nla_line[RDMA_NLDEV_ATTR_RES_CM_IDN]) |
704 | res_print_uint(rd, "cm-idn", cm_idn); | |
9a362cc7 SW |
705 | |
706 | if (nla_line[RDMA_NLDEV_ATTR_RES_SRC_ADDR]) | |
707 | print_ipaddr(rd, "src-addr", src_addr_str, src_port); | |
708 | if (nla_line[RDMA_NLDEV_ATTR_RES_DST_ADDR]) | |
709 | print_ipaddr(rd, "dst-addr", dst_addr_str, dst_port); | |
710 | ||
711 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) | |
712 | free(comm); | |
713 | ||
33115275 SW |
714 | print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
715 | newline(rd); | |
9a362cc7 SW |
716 | } |
717 | return MNL_CB_OK; | |
718 | } | |
719 | ||
b0b8e32c SW |
720 | static void print_cqe(struct rd *rd, uint32_t val) |
721 | { | |
722 | if (rd->json_output) | |
723 | jsonw_uint_field(rd->jw, "cqe", val); | |
724 | else | |
725 | pr_out("cqe %u ", val); | |
726 | } | |
727 | ||
728 | static void print_users(struct rd *rd, uint64_t val) | |
729 | { | |
730 | if (rd->json_output) | |
731 | jsonw_uint_field(rd->jw, "users", val); | |
732 | else | |
733 | pr_out("users %" PRIu64 " ", val); | |
734 | } | |
735 | ||
736 | static const char *poll_ctx_to_str(uint8_t idx) | |
737 | { | |
738 | static const char * const cm_id_states_str[] = { | |
b058f969 | 739 | "DIRECT", "SOFTIRQ", "WORKQUEUE", "UNBOUND_WORKQUEUE"}; |
b0b8e32c SW |
740 | |
741 | if (idx < ARRAY_SIZE(cm_id_states_str)) | |
742 | return cm_id_states_str[idx]; | |
743 | return "UNKNOWN"; | |
744 | } | |
745 | ||
746 | static void print_poll_ctx(struct rd *rd, uint8_t poll_ctx) | |
747 | { | |
748 | if (rd->json_output) { | |
749 | jsonw_string_field(rd->jw, "poll-ctx", | |
750 | poll_ctx_to_str(poll_ctx)); | |
751 | return; | |
752 | } | |
753 | pr_out("poll-ctx %s ", poll_ctx_to_str(poll_ctx)); | |
754 | } | |
755 | ||
756 | static int res_cq_parse_cb(const struct nlmsghdr *nlh, void *data) | |
757 | { | |
758 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
759 | struct nlattr *nla_table, *nla_entry; | |
760 | struct rd *rd = data; | |
761 | const char *name; | |
762 | uint32_t idx; | |
763 | ||
764 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
765 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || | |
766 | !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
767 | !tb[RDMA_NLDEV_ATTR_RES_CQ]) | |
768 | return MNL_CB_ERROR; | |
769 | ||
770 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
771 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
772 | nla_table = tb[RDMA_NLDEV_ATTR_RES_CQ]; | |
773 | ||
774 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
775 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
776 | char *comm = NULL; | |
777 | uint32_t pid = 0; | |
778 | uint8_t poll_ctx = 0; | |
96f59e7f | 779 | uint32_t ctxn = 0; |
1dc03586 | 780 | uint32_t cqn = 0; |
b0b8e32c SW |
781 | uint64_t users; |
782 | uint32_t cqe; | |
783 | int err; | |
784 | ||
785 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
786 | if (err != MNL_CB_OK) | |
787 | return MNL_CB_ERROR; | |
788 | ||
789 | if (!nla_line[RDMA_NLDEV_ATTR_RES_CQE] || | |
790 | !nla_line[RDMA_NLDEV_ATTR_RES_USECNT] || | |
791 | (!nla_line[RDMA_NLDEV_ATTR_RES_PID] && | |
792 | !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) { | |
793 | return MNL_CB_ERROR; | |
794 | } | |
795 | ||
796 | cqe = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CQE]); | |
797 | ||
798 | users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]); | |
799 | if (rd_check_is_filtered(rd, "users", users)) | |
800 | continue; | |
801 | ||
802 | if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]) { | |
803 | poll_ctx = mnl_attr_get_u8( | |
804 | nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]); | |
805 | if (rd_check_is_string_filtered(rd, "poll-ctx", | |
806 | poll_ctx_to_str(poll_ctx))) | |
807 | continue; | |
808 | } | |
809 | ||
810 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { | |
811 | pid = mnl_attr_get_u32( | |
812 | nla_line[RDMA_NLDEV_ATTR_RES_PID]); | |
813 | comm = get_task_name(pid); | |
814 | } | |
815 | ||
816 | if (rd_check_is_filtered(rd, "pid", pid)) { | |
817 | free(comm); | |
818 | continue; | |
819 | } | |
820 | ||
1dc03586 LR |
821 | if (nla_line[RDMA_NLDEV_ATTR_RES_CQN]) |
822 | cqn = mnl_attr_get_u32( | |
823 | nla_line[RDMA_NLDEV_ATTR_RES_CQN]); | |
824 | if (rd_check_is_filtered(rd, "cqn", cqn)) | |
825 | continue; | |
826 | ||
96f59e7f LR |
827 | if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN]) |
828 | ctxn = mnl_attr_get_u32( | |
829 | nla_line[RDMA_NLDEV_ATTR_RES_CTXN]); | |
830 | if (rd_check_is_filtered(rd, "ctxn", ctxn)) | |
831 | continue; | |
832 | ||
b0b8e32c SW |
833 | if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) |
834 | /* discard const from mnl_attr_get_str */ | |
835 | comm = (char *)mnl_attr_get_str( | |
836 | nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); | |
837 | ||
838 | if (rd->json_output) | |
839 | jsonw_start_array(rd->jw); | |
840 | ||
841 | print_dev(rd, idx, name); | |
842 | print_cqe(rd, cqe); | |
843 | print_users(rd, users); | |
844 | if (nla_line[RDMA_NLDEV_ATTR_RES_POLL_CTX]) | |
845 | print_poll_ctx(rd, poll_ctx); | |
846 | print_pid(rd, pid); | |
847 | print_comm(rd, comm, nla_line); | |
848 | ||
1dc03586 LR |
849 | if (nla_line[RDMA_NLDEV_ATTR_RES_CQN]) |
850 | res_print_uint(rd, "cqn", cqn); | |
96f59e7f LR |
851 | if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN]) |
852 | res_print_uint(rd, "ctxn", ctxn); | |
1dc03586 | 853 | |
b0b8e32c SW |
854 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) |
855 | free(comm); | |
856 | ||
33115275 SW |
857 | print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
858 | newline(rd); | |
b0b8e32c SW |
859 | } |
860 | return MNL_CB_OK; | |
861 | } | |
862 | ||
8958a15c SW |
863 | static int res_mr_parse_cb(const struct nlmsghdr *nlh, void *data) |
864 | { | |
865 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
866 | struct nlattr *nla_table, *nla_entry; | |
867 | struct rd *rd = data; | |
868 | const char *name; | |
869 | uint32_t idx; | |
870 | ||
871 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
872 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || | |
873 | !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
874 | !tb[RDMA_NLDEV_ATTR_RES_MR]) | |
875 | return MNL_CB_ERROR; | |
876 | ||
877 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
878 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
879 | nla_table = tb[RDMA_NLDEV_ATTR_RES_MR]; | |
880 | ||
881 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
882 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
883 | uint32_t rkey = 0, lkey = 0; | |
884 | uint64_t iova = 0, mrlen; | |
885 | char *comm = NULL; | |
96f59e7f | 886 | uint32_t pdn = 0; |
1dc03586 | 887 | uint32_t mrn = 0; |
8958a15c SW |
888 | uint32_t pid = 0; |
889 | int err; | |
890 | ||
891 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
892 | if (err != MNL_CB_OK) | |
893 | return MNL_CB_ERROR; | |
894 | ||
895 | if (!nla_line[RDMA_NLDEV_ATTR_RES_MRLEN] || | |
896 | (!nla_line[RDMA_NLDEV_ATTR_RES_PID] && | |
897 | !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) { | |
898 | return MNL_CB_ERROR; | |
899 | } | |
900 | ||
901 | if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY]) | |
902 | rkey = mnl_attr_get_u32( | |
903 | nla_line[RDMA_NLDEV_ATTR_RES_RKEY]); | |
904 | if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY]) | |
905 | lkey = mnl_attr_get_u32( | |
906 | nla_line[RDMA_NLDEV_ATTR_RES_LKEY]); | |
907 | if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA]) | |
908 | iova = mnl_attr_get_u64( | |
909 | nla_line[RDMA_NLDEV_ATTR_RES_IOVA]); | |
910 | ||
911 | mrlen = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]); | |
912 | if (rd_check_is_filtered(rd, "mrlen", mrlen)) | |
913 | continue; | |
914 | ||
915 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { | |
916 | pid = mnl_attr_get_u32( | |
917 | nla_line[RDMA_NLDEV_ATTR_RES_PID]); | |
918 | comm = get_task_name(pid); | |
919 | } | |
920 | ||
921 | if (rd_check_is_filtered(rd, "pid", pid)) { | |
922 | free(comm); | |
923 | continue; | |
924 | } | |
925 | ||
1dc03586 LR |
926 | if (nla_line[RDMA_NLDEV_ATTR_RES_MRN]) |
927 | mrn = mnl_attr_get_u32( | |
928 | nla_line[RDMA_NLDEV_ATTR_RES_MRN]); | |
929 | if (rd_check_is_filtered(rd, "mrn", mrn)) | |
930 | continue; | |
931 | ||
96f59e7f LR |
932 | if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
933 | pdn = mnl_attr_get_u32( | |
934 | nla_line[RDMA_NLDEV_ATTR_RES_PDN]); | |
935 | if (rd_check_is_filtered(rd, "pdn", pdn)) | |
936 | continue; | |
937 | ||
8958a15c SW |
938 | if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) |
939 | /* discard const from mnl_attr_get_str */ | |
940 | comm = (char *)mnl_attr_get_str( | |
941 | nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); | |
942 | ||
943 | if (rd->json_output) | |
944 | jsonw_start_array(rd->jw); | |
945 | ||
946 | print_dev(rd, idx, name); | |
947 | if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY]) | |
948 | print_key(rd, "rkey", rkey); | |
949 | if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY]) | |
950 | print_key(rd, "lkey", lkey); | |
951 | if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA]) | |
beac6a39 LR |
952 | print_key(rd, "iova", iova); |
953 | res_print_uint(rd, "mrlen", mrlen); | |
8958a15c SW |
954 | print_pid(rd, pid); |
955 | print_comm(rd, comm, nla_line); | |
956 | ||
1dc03586 LR |
957 | if (nla_line[RDMA_NLDEV_ATTR_RES_MRN]) |
958 | res_print_uint(rd, "mrn", mrn); | |
959 | ||
96f59e7f LR |
960 | if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
961 | res_print_uint(rd, "pdn", pdn); | |
962 | ||
8958a15c SW |
963 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) |
964 | free(comm); | |
965 | ||
33115275 SW |
966 | print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
967 | newline(rd); | |
8958a15c SW |
968 | } |
969 | return MNL_CB_OK; | |
970 | } | |
971 | ||
4060e4c0 SW |
972 | static int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data) |
973 | { | |
974 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
975 | struct nlattr *nla_table, *nla_entry; | |
976 | struct rd *rd = data; | |
977 | const char *name; | |
978 | uint32_t idx; | |
979 | ||
980 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
981 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || | |
982 | !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
983 | !tb[RDMA_NLDEV_ATTR_RES_PD]) | |
984 | return MNL_CB_ERROR; | |
985 | ||
986 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
987 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
988 | nla_table = tb[RDMA_NLDEV_ATTR_RES_PD]; | |
989 | ||
990 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
991 | uint32_t local_dma_lkey = 0, unsafe_global_rkey = 0; | |
992 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
993 | char *comm = NULL; | |
96f59e7f | 994 | uint32_t ctxn = 0; |
4060e4c0 | 995 | uint32_t pid = 0; |
1dc03586 | 996 | uint32_t pdn = 0; |
4060e4c0 SW |
997 | uint64_t users; |
998 | int err; | |
999 | ||
1000 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
1001 | if (err != MNL_CB_OK) | |
1002 | return MNL_CB_ERROR; | |
1003 | ||
1004 | if (!nla_line[RDMA_NLDEV_ATTR_RES_USECNT] || | |
1005 | (!nla_line[RDMA_NLDEV_ATTR_RES_PID] && | |
1006 | !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) { | |
1007 | return MNL_CB_ERROR; | |
1008 | } | |
1009 | ||
1010 | if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]) | |
1011 | local_dma_lkey = mnl_attr_get_u32( | |
1012 | nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]); | |
1013 | ||
1014 | users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]); | |
1015 | if (rd_check_is_filtered(rd, "users", users)) | |
1016 | continue; | |
1017 | ||
1018 | if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]) | |
1019 | unsafe_global_rkey = mnl_attr_get_u32( | |
1020 | nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]); | |
1021 | ||
1022 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { | |
1023 | pid = mnl_attr_get_u32( | |
1024 | nla_line[RDMA_NLDEV_ATTR_RES_PID]); | |
1025 | comm = get_task_name(pid); | |
1026 | } | |
1027 | ||
96f59e7f LR |
1028 | if (rd_check_is_filtered(rd, "pid", pid)) |
1029 | continue; | |
1030 | ||
1031 | if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN]) | |
1032 | ctxn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CTXN]); | |
1033 | ||
1034 | if (rd_check_is_filtered(rd, "ctxn", ctxn)) | |
4060e4c0 SW |
1035 | continue; |
1036 | ||
1dc03586 LR |
1037 | if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
1038 | pdn = mnl_attr_get_u32( | |
1039 | nla_line[RDMA_NLDEV_ATTR_RES_PDN]); | |
1040 | if (rd_check_is_filtered(rd, "pdn", pdn)) | |
1041 | continue; | |
1042 | ||
4060e4c0 SW |
1043 | if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) |
1044 | /* discard const from mnl_attr_get_str */ | |
1045 | comm = (char *)mnl_attr_get_str( | |
1046 | nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); | |
1047 | ||
1048 | if (rd->json_output) | |
1049 | jsonw_start_array(rd->jw); | |
1050 | ||
1051 | print_dev(rd, idx, name); | |
1052 | if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]) | |
1053 | print_key(rd, "local_dma_lkey", local_dma_lkey); | |
1054 | print_users(rd, users); | |
1055 | if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]) | |
1056 | print_key(rd, "unsafe_global_rkey", unsafe_global_rkey); | |
1057 | print_pid(rd, pid); | |
1058 | print_comm(rd, comm, nla_line); | |
96f59e7f LR |
1059 | if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN]) |
1060 | res_print_uint(rd, "ctxn", ctxn); | |
4060e4c0 | 1061 | |
1dc03586 LR |
1062 | if (nla_line[RDMA_NLDEV_ATTR_RES_PDN]) |
1063 | res_print_uint(rd, "pdn", pdn); | |
1064 | ||
4060e4c0 SW |
1065 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) |
1066 | free(comm); | |
1067 | ||
33115275 SW |
1068 | print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]); |
1069 | newline(rd); | |
4060e4c0 SW |
1070 | } |
1071 | return MNL_CB_OK; | |
1072 | } | |
1073 | ||
923aa825 LR |
1074 | RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true); |
1075 | ||
8ecac46a | 1076 | static const struct |
96f59e7f LR |
1077 | filters qp_valid_filters[MAX_NUMBER_OF_FILTERS] = { |
1078 | { .name = "link", .is_number = false }, | |
1079 | { .name = "lqpn", .is_number = true }, | |
1080 | { .name = "rqpn", .is_number = true }, | |
1081 | { .name = "pid", .is_number = true }, | |
1082 | { .name = "sq-psn", .is_number = true }, | |
1083 | { .name = "rq-psn", .is_number = true }, | |
1084 | { .name = "type", .is_number = false }, | |
1085 | { .name = "path-mig-state", .is_number = false }, | |
1086 | { .name = "state", .is_number = false }, | |
1087 | { .name = "pdn", .is_number = true }, | |
1088 | }; | |
8ecac46a LR |
1089 | |
1090 | RES_FUNC(res_qp, RDMA_NLDEV_CMD_RES_QP_GET, qp_valid_filters, false); | |
1091 | ||
9a362cc7 SW |
1092 | static const |
1093 | struct filters cm_id_valid_filters[MAX_NUMBER_OF_FILTERS] = { | |
1094 | { .name = "link", .is_number = false }, | |
1095 | { .name = "lqpn", .is_number = true }, | |
1096 | { .name = "qp-type", .is_number = false }, | |
1097 | { .name = "state", .is_number = false }, | |
1098 | { .name = "ps", .is_number = false }, | |
1099 | { .name = "dev-type", .is_number = false }, | |
1100 | { .name = "transport-type", .is_number = false }, | |
1101 | { .name = "pid", .is_number = true }, | |
1102 | { .name = "src-addr", .is_number = false }, | |
1103 | { .name = "src-port", .is_number = true }, | |
1104 | { .name = "dst-addr", .is_number = false }, | |
1dc03586 LR |
1105 | { .name = "dst-port", .is_number = true }, |
1106 | { .name = "cm-idn", .is_number = true } | |
9a362cc7 SW |
1107 | }; |
1108 | ||
1109 | RES_FUNC(res_cm_id, RDMA_NLDEV_CMD_RES_CM_ID_GET, cm_id_valid_filters, false); | |
1110 | ||
b0b8e32c SW |
1111 | static const |
1112 | struct filters cq_valid_filters[MAX_NUMBER_OF_FILTERS] = { | |
1113 | { .name = "dev", .is_number = false }, | |
1114 | { .name = "users", .is_number = true }, | |
1115 | { .name = "poll-ctx", .is_number = false }, | |
1dc03586 | 1116 | { .name = "pid", .is_number = true }, |
96f59e7f LR |
1117 | { .name = "cqn", .is_number = true }, |
1118 | { .name = "ctxn", .is_number = true } | |
b0b8e32c SW |
1119 | }; |
1120 | ||
1121 | RES_FUNC(res_cq, RDMA_NLDEV_CMD_RES_CQ_GET, cq_valid_filters, true); | |
1122 | ||
8958a15c SW |
1123 | static const |
1124 | struct filters mr_valid_filters[MAX_NUMBER_OF_FILTERS] = { | |
1125 | { .name = "dev", .is_number = false }, | |
1126 | { .name = "rkey", .is_number = true }, | |
1127 | { .name = "lkey", .is_number = true }, | |
1128 | { .name = "mrlen", .is_number = true }, | |
1dc03586 | 1129 | { .name = "pid", .is_number = true }, |
96f59e7f LR |
1130 | { .name = "mrn", .is_number = true }, |
1131 | { .name = "pdn", .is_number = true } | |
8958a15c SW |
1132 | }; |
1133 | ||
1134 | RES_FUNC(res_mr, RDMA_NLDEV_CMD_RES_MR_GET, mr_valid_filters, true); | |
1135 | ||
4060e4c0 SW |
1136 | static const |
1137 | struct filters pd_valid_filters[MAX_NUMBER_OF_FILTERS] = { | |
1138 | { .name = "dev", .is_number = false }, | |
1139 | { .name = "users", .is_number = true }, | |
1dc03586 | 1140 | { .name = "pid", .is_number = true }, |
96f59e7f LR |
1141 | { .name = "ctxn", .is_number = true }, |
1142 | { .name = "pdn", .is_number = true }, | |
1143 | { .name = "ctxn", .is_number = true } | |
4060e4c0 SW |
1144 | }; |
1145 | ||
1146 | RES_FUNC(res_pd, RDMA_NLDEV_CMD_RES_PD_GET, pd_valid_filters, true); | |
1147 | ||
923aa825 LR |
1148 | static int res_show(struct rd *rd) |
1149 | { | |
1150 | const struct rd_cmd cmds[] = { | |
1151 | { NULL, res_no_args }, | |
8ecac46a | 1152 | { "qp", res_qp }, |
9a362cc7 | 1153 | { "cm_id", res_cm_id }, |
b0b8e32c | 1154 | { "cq", res_cq }, |
8958a15c | 1155 | { "mr", res_mr }, |
4060e4c0 | 1156 | { "pd", res_pd }, |
923aa825 LR |
1157 | { 0 } |
1158 | }; | |
1159 | ||
1160 | /* | |
1161 | * Special case to support "rdma res show DEV_NAME" | |
1162 | */ | |
1163 | if (rd_argc(rd) == 1 && dev_map_lookup(rd, false)) | |
1164 | return rd_exec_dev(rd, _res_no_args); | |
1165 | ||
1166 | return rd_exec_cmd(rd, cmds, "parameter"); | |
1167 | } | |
1168 | ||
1169 | int cmd_res(struct rd *rd) | |
1170 | { | |
1171 | const struct rd_cmd cmds[] = { | |
1172 | { NULL, res_show }, | |
1173 | { "show", res_show }, | |
1174 | { "list", res_show }, | |
1175 | { "help", res_help }, | |
1176 | { 0 } | |
1177 | }; | |
1178 | ||
1179 | return rd_exec_cmd(rd, cmds, "resource command"); | |
1180 | } |