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