Debian Codegraph @

rdma/res-mr.c

raw
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * res-mr.c RDMA tool
4 * Authors: Leon Romanovsky <[email protected]>
5 */
6
7 #include "res.h"
8 #include <inttypes.h>
9
10 static int res_mr_line_raw(struct rd *rd, const char *name, int idx,
11 struct nlattr **nla_line)
12 {
13 if (!nla_line[RDMA_NLDEV_ATTR_RES_RAW])
14 return MNL_CB_ERROR;
15
16 open_json_object(NULL);
17 print_dev(idx, name);
18 print_raw_data(rd, nla_line);
19 close_json_object();
20 newline();
21
22 return MNL_CB_OK;
23 }
24
25 static int res_mr_line(struct rd *rd, const char *name, int idx,
26 struct nlattr **nla_line)
27 {
28 uint32_t rkey = 0, lkey = 0;
29 uint64_t iova = 0, mrlen;
30 char *comm = NULL;
31 uint32_t pdn = 0;
32 uint32_t mrn = 0;
33 uint32_t pid = 0;
34 SPRINT_BUF(b);
35
36 if (!nla_line[RDMA_NLDEV_ATTR_RES_MRLEN])
37 return MNL_CB_ERROR;
38
39 if (nla_line[RDMA_NLDEV_ATTR_RES_RKEY])
40 rkey = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
41 if (nla_line[RDMA_NLDEV_ATTR_RES_LKEY])
42 lkey = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
43 if (nla_line[RDMA_NLDEV_ATTR_RES_IOVA])
44 iova = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
45
46 mrlen = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
47 if (rd_is_filtered_attr(rd, "mrlen", mrlen,
48 nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]))
49 goto out;
50
51 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
52 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
53 if (!get_task_name(pid, b, sizeof(b)))
54 comm = b;
55 } else if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) {
56 /* discard const from mnl_attr_get_str */
57 comm = (char *)mnl_attr_get_str(
58 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
59 }
60
61 if (rd_is_filtered_attr(rd, "pid", pid,
62 nla_line[RDMA_NLDEV_ATTR_RES_PID]))
63 goto out;
64
65 if (nla_line[RDMA_NLDEV_ATTR_RES_MRN])
66 mrn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_MRN]);
67 if (rd_is_filtered_attr(rd, "mrn", mrn,
68 nla_line[RDMA_NLDEV_ATTR_RES_MRN]))
69 goto out;
70
71 if (nla_line[RDMA_NLDEV_ATTR_RES_PDN])
72 pdn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
73 if (rd_is_filtered_attr(rd, "pdn", pdn,
74 nla_line[RDMA_NLDEV_ATTR_RES_PDN]))
75 goto out;
76
77 open_json_object(NULL);
78 print_dev(idx, name);
79 res_print_u32("mrn", mrn, nla_line[RDMA_NLDEV_ATTR_RES_MRN]);
80 print_key("rkey", rkey, nla_line[RDMA_NLDEV_ATTR_RES_RKEY]);
81 print_key("lkey", lkey, nla_line[RDMA_NLDEV_ATTR_RES_LKEY]);
82 print_key("iova", iova, nla_line[RDMA_NLDEV_ATTR_RES_IOVA]);
83 res_print_u64("mrlen", mrlen, nla_line[RDMA_NLDEV_ATTR_RES_MRLEN]);
84 res_print_u32("pdn", pdn, nla_line[RDMA_NLDEV_ATTR_RES_PDN]);
85 res_print_u32("pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
86 print_comm(comm, nla_line);
87
88 print_driver_table(rd, nla_line[RDMA_NLDEV_ATTR_DRIVER]);
89 print_raw_data(rd, nla_line);
90 close_json_object();
91 newline();
92
93 out:
94 return MNL_CB_OK;
95 }
96
97 int res_mr_idx_parse_cb(const struct nlmsghdr *nlh, void *data)
98 {
99 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
100 struct rd *rd = data;
101 const char *name;
102 uint32_t idx;
103
104 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
105 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME])
106 return MNL_CB_ERROR;
107
108 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
109 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
110
111 return (rd->show_raw) ? res_mr_line_raw(rd, name, idx, tb) :
112 res_mr_line(rd, name, idx, tb);
113 }
114
115 int res_mr_parse_cb(const struct nlmsghdr *nlh, void *data)
116 {
117 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
118 struct nlattr *nla_table, *nla_entry;
119 struct rd *rd = data;
120 int ret = MNL_CB_OK;
121 const char *name;
122 uint32_t idx;
123
124 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
125 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
126 !tb[RDMA_NLDEV_ATTR_RES_MR])
127 return MNL_CB_ERROR;
128
129 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
130 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
131 nla_table = tb[RDMA_NLDEV_ATTR_RES_MR];
132
133 mnl_attr_for_each_nested(nla_entry, nla_table) {
134 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
135
136 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
137 if (ret != MNL_CB_OK)
138 break;
139
140 ret = (rd->show_raw) ? res_mr_line_raw(rd, name, idx, nla_line) :
141 res_mr_line(rd, name, idx, nla_line);
142 if (ret != MNL_CB_OK)
143 break;
144 }
145 return ret;
146 }