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