]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res-pd.c
rdma: Simplify CM_ID print code
[mirror_iproute2.git] / rdma / res-pd.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * res-pd.c RDMA tool
4 * Authors: Leon Romanovsky <leonro@mellanox.com>
5 */
6
7 #include "res.h"
8 #include <inttypes.h>
9
10 int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data)
11 {
12 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
13 struct nlattr *nla_table, *nla_entry;
14 struct rd *rd = data;
15 const char *name;
16 uint32_t idx;
17
18 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
19 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
20 !tb[RDMA_NLDEV_ATTR_RES_PD])
21 return MNL_CB_ERROR;
22
23 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
24 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
25 nla_table = tb[RDMA_NLDEV_ATTR_RES_PD];
26
27 mnl_attr_for_each_nested(nla_entry, nla_table) {
28 uint32_t local_dma_lkey = 0, unsafe_global_rkey = 0;
29 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
30 char *comm = NULL;
31 uint32_t ctxn = 0;
32 uint32_t pid = 0;
33 uint32_t pdn = 0;
34 uint64_t users;
35 int err;
36
37 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
38 if (err != MNL_CB_OK)
39 return MNL_CB_ERROR;
40
41 if (!nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
42 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
43 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
44 return MNL_CB_ERROR;
45 }
46
47 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
48 local_dma_lkey = mnl_attr_get_u32(
49 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
50
51 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
52 if (rd_check_is_filtered(rd, "users", users))
53 continue;
54
55 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
56 unsafe_global_rkey = mnl_attr_get_u32(
57 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
58
59 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
60 pid = mnl_attr_get_u32(
61 nla_line[RDMA_NLDEV_ATTR_RES_PID]);
62 comm = get_task_name(pid);
63 }
64
65 if (rd_check_is_filtered(rd, "pid", pid))
66 continue;
67
68 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
69 ctxn = mnl_attr_get_u32(
70 nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
71
72 if (rd_check_is_filtered(rd, "ctxn", ctxn))
73 continue;
74
75 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
76 pdn = mnl_attr_get_u32(
77 nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
78 if (rd_check_is_filtered(rd, "pdn", pdn))
79 continue;
80
81 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
82 /* discard const from mnl_attr_get_str */
83 comm = (char *)mnl_attr_get_str(
84 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
85
86 if (rd->json_output)
87 jsonw_start_array(rd->jw);
88
89 print_dev(rd, idx, name);
90 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
91 print_key(rd, "local_dma_lkey", local_dma_lkey);
92 res_print_uint(rd, "users", users);
93 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
94 print_key(rd, "unsafe_global_rkey", unsafe_global_rkey);
95 res_print_uint(rd, "pid", pid);
96 print_comm(rd, comm, nla_line);
97 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
98 res_print_uint(rd, "ctxn", ctxn);
99
100 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
101 res_print_uint(rd, "pdn", pdn);
102
103 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
104 free(comm);
105
106 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
107 newline(rd);
108 }
109 return MNL_CB_OK;
110 }