1
2
3
4
5
6
7#include "res.h"
8#include <inttypes.h>
9
10static int res_ctx_line(struct rd *rd, const char *name, int idx,
11 struct nlattr **nla_line)
12{
13 char *comm = NULL;
14 uint32_t ctxn = 0;
15 uint32_t pid = 0;
16
17 if (!nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
18 return MNL_CB_ERROR;
19
20 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
21 SPRINT_BUF(b);
22
23 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
24 if (!get_task_name(pid, b, sizeof(b)))
25 comm = b;
26 } else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
27
28 comm = (char *)mnl_attr_get_str(
29 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
30 }
31
32 if (rd_is_filtered_attr(rd, "pid", pid,
33 nla_line[RDMA_NLDEV_ATTR_RES_PID]))
34 goto out;
35
36 if (nla_line[RDMA_NLDEV_ATTR_RES_CTXN])
37 ctxn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
38
39 if (rd_is_filtered_attr(rd, "ctxn", ctxn,
40 nla_line[RDMA_NLDEV_ATTR_RES_CTXN]))
41 goto out;
42
43 open_json_object(NULL);
44 print_dev(rd, idx, name);
45 res_print_u32(rd, "ctxn", ctxn, nla_line[RDMA_NLDEV_ATTR_RES_CTXN]);
46 res_print_u32(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
47 print_comm(rd, comm, nla_line);
48
49 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
50 newline(rd);
51
52out:
53 return MNL_CB_OK;
54}
55
56int res_ctx_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
57{
58 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
59 struct rd *rd = data;
60 const char *name;
61 uint32_t idx;
62
63 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
64 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
65 return MNL_CB_ERROR;
66
67 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
68 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
69
70 return res_ctx_line(rd, name, idx, tb);
71}
72
73int res_ctx_parse_cb(const struct nlmsghdr *nlh, void *data)
74{
75 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
76 struct nlattr *nla_table, *nla_entry;
77 struct rd *rd = data;
78 int ret = MNL_CB_OK;
79 const char *name;
80 uint32_t idx;
81
82 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
83 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
84 !tb[RDMA_NLDEV_ATTR_RES_CTX])
85 return MNL_CB_ERROR;
86
87 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
88 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
89 nla_table = tb[RDMA_NLDEV_ATTR_RES_CTX];
90
91 mnl_attr_for_each_nested(nla_entry, nla_table) {
92 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
93
94 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
95 if (ret != MNL_CB_OK)
96 break;
97
98 ret = res_ctx_line(rd, name, idx, nla_line);
99 if (ret != MNL_CB_OK)
100 break;
101 }
102 return ret;
103}
104