]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res-pd.c
956d4d9fbf7e3f258bf83537be66e243a8397ebe
[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 static int res_pd_line(struct rd *rd, const char *name, int idx,
11 struct nlattr **nla_line)
12 {
13 uint32_t local_dma_lkey = 0, unsafe_global_rkey = 0;
14 char *comm = NULL;
15 uint32_t ctxn = 0;
16 uint32_t pid = 0;
17 uint32_t pdn = 0;
18 uint64_t users;
19
20 if (!nla_line[RDMA_NLDEV_ATTR_RES_USECNT] ||
21 (!nla_line[RDMA_NLDEV_ATTR_RES_PID] &&
22 !nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])) {
23 return MNL_CB_ERROR;
24 }
25
26 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
27 local_dma_lkey = mnl_attr_get_u32(
28 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
29
30 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
31 if (rd_check_is_filtered(rd, "users", users))
32 goto out;
33
34 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
35 unsafe_global_rkey = mnl_attr_get_u32(
36 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
37
38 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
39 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
40 comm = get_task_name(pid);
41 }
42
43 if (rd_check_is_filtered(rd, "pid", pid))
44 goto out;
45
46 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
47 ctxn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
48
49 if (rd_check_is_filtered(rd, "ctxn", ctxn))
50 goto out;
51
52 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
53 pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
54 if (rd_check_is_filtered(rd, "pdn", pdn))
55 goto out;
56
57 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
58 /* discard const from mnl_attr_get_str */
59 comm = (char *)mnl_attr_get_str(
60 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
61
62 if (rd->json_output)
63 jsonw_start_array(rd->jw);
64
65 print_dev(rd, idx, name);
66 res_print_uint(rd, "pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
67 print_key(rd, "local_dma_lkey", local_dma_lkey,
68 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
69 res_print_uint(rd, "users", users,
70 nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
71 print_key(rd, "unsafe_global_rkey", unsafe_global_rkey,
72 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
73 res_print_uint(rd, "ctxn", ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
74 res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
75 print_comm(rd, comm, nla_line);
76
77 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
78 newline(rd);
79
80 out: if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
81 free(comm);
82 return MNL_CB_OK;
83 }
84
85 int res_pd_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
86 {
87 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
88 struct rd *rd = data;
89 const char *name;
90 uint32_t idx;
91
92 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
93 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
94 return MNL_CB_ERROR;
95
96 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
97 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
98
99 return res_pd_line(rd, name, idx, tb);
100 }
101
102 int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data)
103 {
104 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
105 struct nlattr *nla_table, *nla_entry;
106 struct rd *rd = data;
107 int ret = MNL_CB_OK;
108 const char *name;
109 uint32_t idx;
110
111 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
112 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
113 !tb[RDMA_NLDEV_ATTR_RES_PD])
114 return MNL_CB_ERROR;
115
116 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
117 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
118 nla_table = tb[RDMA_NLDEV_ATTR_RES_PD];
119
120 mnl_attr_for_each_nested(nla_entry, nla_table) {
121 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
122
123 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
124 if (ret != MNL_CB_OK)
125 break;
126
127 ret = res_pd_line(rd, name, idx, nla_line);
128 if (ret != MNL_CB_OK)
129 break;
130 }
131 return ret;
132 }