]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res-pd.c
rdma: Add support for the netlink extack
[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 open_json_object(NULL);
64 print_dev(rd, idx, name);
65 res_print_uint(rd, "pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
66 print_key(rd, "local_dma_lkey", local_dma_lkey,
67 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
68 res_print_uint(rd, "users", users,
69 nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
70 print_key(rd, "unsafe_global_rkey", unsafe_global_rkey,
71 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
72 res_print_uint(rd, "ctxn", ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
73 res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
74 print_comm(rd, comm, nla_line);
75
76 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
77 newline(rd);
78
79 out: if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
80 free(comm);
81 return MNL_CB_OK;
82 }
83
84 int res_pd_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
85 {
86 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
87 struct rd *rd = data;
88 const char *name;
89 uint32_t idx;
90
91 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
92 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
93 return MNL_CB_ERROR;
94
95 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
96 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
97
98 return res_pd_line(rd, name, idx, tb);
99 }
100
101 int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data)
102 {
103 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
104 struct nlattr *nla_table, *nla_entry;
105 struct rd *rd = data;
106 int ret = MNL_CB_OK;
107 const char *name;
108 uint32_t idx;
109
110 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
111 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
112 !tb[RDMA_NLDEV_ATTR_RES_PD])
113 return MNL_CB_ERROR;
114
115 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
116 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
117 nla_table = tb[RDMA_NLDEV_ATTR_RES_PD];
118
119 mnl_attr_for_each_nested(nla_entry, nla_table) {
120 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
121
122 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
123 if (ret != MNL_CB_OK)
124 break;
125
126 ret = res_pd_line(rd, name, idx, nla_line);
127 if (ret != MNL_CB_OK)
128 break;
129 }
130 return ret;
131 }