1
2
3
4
5
6
7#include "res.h"
8#include <inttypes.h>
9
10static 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 SPRINT_BUF(b);
20
21 if (!nla_line[RDMA_NLDEV_ATTR_RES_USECNT])
22 return MNL_CB_ERROR;
23
24 if (nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY])
25 local_dma_lkey = mnl_attr_get_u32(
26 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
27
28 users = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
29 if (rd_is_filtered_attr(rd, "users", users,
30 nla_line[RDMA_NLDEV_ATTR_RES_USECNT]))
31 goto out;
32
33 if (nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY])
34 unsafe_global_rkey = mnl_attr_get_u32(
35 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
36
37 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
38 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
39 if (!get_task_name(pid, b, sizeof(b)))
40 comm = b;
41 } else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
42
43 comm = (char *)mnl_attr_get_str(
44 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
45 }
46
47 if (rd_is_filtered_attr(rd, "pid", pid,
48 nla_line[RDMA_NLDEV_ATTR_RES_PID]))
49 goto out;
50
51 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
52 ctxn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
53
54 if (rd_is_filtered_attr(rd, "ctxn", ctxn,
55 nla_line[RDMA_NLDEV_ATTR_RES_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_is_filtered_attr(rd, "pdn", pdn,
61 nla_line[RDMA_NLDEV_ATTR_RES_PDN]))
62 goto out;
63
64 open_json_object(NULL);
65 print_dev(idx, name);
66 res_print_u32("pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
67 print_key("local_dma_lkey", local_dma_lkey,
68 nla_line[RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]);
69 res_print_u64("users", users, nla_line[RDMA_NLDEV_ATTR_RES_USECNT]);
70 print_key("unsafe_global_rkey", unsafe_global_rkey,
71 nla_line[RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]);
72 res_print_u32("ctxn", ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
73 res_print_u32("pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
74 print_comm(comm, nla_line);
75
76 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
77 close_json_object();
78 newline();
79out:
80 return MNL_CB_OK;
81}
82
83int res_pd_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
84{
85 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
86 struct rd *rd = data;
87 const char *name;
88 uint32_t idx;
89
90 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
91 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
92 return MNL_CB_ERROR;
93
94 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
95 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
96
97 return res_pd_line(rd, name, idx, tb);
98}
99
100int res_pd_parse_cb(const struct nlmsghdr *nlh, void *data)
101{
102 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
103 struct nlattr *nla_table, *nla_entry;
104 struct rd *rd = data;
105 int ret = MNL_CB_OK;
106 const char *name;
107 uint32_t idx;
108
109 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
110 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
111 !tb[RDMA_NLDEV_ATTR_RES_PD])
112 return MNL_CB_ERROR;
113
114 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
115 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
116 nla_table = tb[RDMA_NLDEV_ATTR_RES_PD];
117
118 mnl_attr_for_each_nested(nla_entry, nla_table) {
119 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
120
121 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
122 if (ret != MNL_CB_OK)
123 break;
124
125 ret = res_pd_line(rd, name, idx, nla_line);
126 if (ret != MNL_CB_OK)
127 break;
128 }
129 return ret;
130}
131