]>
Commit | Line | Data |
---|---|---|
835d8321 | 1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
923aa825 LR |
2 | /* |
3 | * res.c RDMA tool | |
923aa825 LR |
4 | * Authors: Leon Romanovsky <leonro@mellanox.com> |
5 | */ | |
6 | ||
cc613127 | 7 | #include "res.h" |
923aa825 LR |
8 | #include <inttypes.h> |
9 | ||
10 | static int res_help(struct rd *rd) | |
11 | { | |
12 | pr_out("Usage: %s resource\n", rd->filename); | |
13 | pr_out(" resource show [DEV]\n"); | |
4ee770ee | 14 | pr_out(" resource show [qp|cm_id|pd|mr|cq]\n"); |
8ecac46a LR |
15 | pr_out(" resource show qp link [DEV/PORT]\n"); |
16 | pr_out(" resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n"); | |
9a362cc7 SW |
17 | pr_out(" resource show cm_id link [DEV/PORT]\n"); |
18 | pr_out(" resource show cm_id link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n"); | |
b0b8e32c SW |
19 | pr_out(" resource show cq link [DEV/PORT]\n"); |
20 | pr_out(" resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n"); | |
4ee770ee LR |
21 | pr_out(" resource show pd dev [DEV]\n"); |
22 | pr_out(" resource show pd dev [DEV] [FILTER-NAME FILTER-VALUE]\n"); | |
23 | pr_out(" resource show mr dev [DEV]\n"); | |
24 | pr_out(" resource show mr dev [DEV] [FILTER-NAME FILTER-VALUE]\n"); | |
923aa825 LR |
25 | return 0; |
26 | } | |
27 | ||
28 | static int res_print_summary(struct rd *rd, struct nlattr **tb) | |
29 | { | |
30 | struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY]; | |
31 | struct nlattr *nla_entry; | |
32 | const char *name; | |
33 | uint64_t curr; | |
34 | int err; | |
35 | ||
36 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
37 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
923aa825 LR |
38 | |
39 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
40 | if (err != MNL_CB_OK) | |
41 | return -EINVAL; | |
42 | ||
43 | if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] || | |
44 | !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) { | |
45 | return -EINVAL; | |
46 | } | |
47 | ||
48 | name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]); | |
49 | curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]); | |
127ff956 LR |
50 | res_print_uint( |
51 | rd, name, curr, | |
52 | nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]); | |
923aa825 LR |
53 | } |
54 | return 0; | |
55 | } | |
56 | ||
5a823593 LR |
57 | static int res_no_args_idx_parse_cb(const struct nlmsghdr *nlh, void *data) |
58 | { | |
59 | return MNL_CB_OK; | |
60 | } | |
61 | ||
923aa825 LR |
62 | static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data) |
63 | { | |
64 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
65 | struct rd *rd = data; | |
66 | const char *name; | |
67 | uint32_t idx; | |
68 | ||
69 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
70 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || | |
71 | !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
72 | !tb[RDMA_NLDEV_ATTR_RES_SUMMARY]) | |
73 | return MNL_CB_ERROR; | |
74 | ||
75 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
76 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
77 | if (rd->json_output) { | |
78 | jsonw_uint_field(rd->jw, "ifindex", idx); | |
79 | jsonw_string_field(rd->jw, "ifname", name); | |
80 | } else { | |
81 | pr_out("%u: %s: ", idx, name); | |
82 | } | |
83 | ||
84 | res_print_summary(rd, tb); | |
85 | ||
86 | if (!rd->json_output) | |
87 | pr_out("\n"); | |
88 | return MNL_CB_OK; | |
89 | } | |
90 | ||
5a823593 LR |
91 | int _res_send_idx_msg(struct rd *rd, uint32_t command, mnl_cb_t callback, |
92 | uint32_t idx, uint32_t id) | |
93 | { | |
94 | uint32_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
95 | uint32_t seq; | |
96 | int ret; | |
97 | ||
98 | rd_prepare_msg(rd, command, &seq, flags); | |
99 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
100 | if (rd->port_idx) | |
101 | mnl_attr_put_u32(rd->nlh, | |
102 | RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
103 | ||
104 | mnl_attr_put_u32(rd->nlh, id, idx); | |
105 | ||
33552ade EA |
106 | if (command == RDMA_NLDEV_CMD_STAT_GET) |
107 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, | |
108 | RDMA_NLDEV_ATTR_RES_MR); | |
109 | ||
5a823593 LR |
110 | ret = rd_send_msg(rd); |
111 | if (ret) | |
112 | return ret; | |
113 | ||
114 | if (rd->json_output) | |
115 | jsonw_start_object(rd->jw); | |
116 | ret = rd_recv_msg(rd, callback, rd, seq); | |
117 | if (rd->json_output) | |
118 | jsonw_end_object(rd->jw); | |
119 | return ret; | |
120 | } | |
121 | ||
cc613127 | 122 | int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback) |
923aa825 LR |
123 | { |
124 | uint32_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
125 | uint32_t seq; | |
126 | int ret; | |
127 | ||
128 | if (command != RDMA_NLDEV_CMD_RES_GET) | |
129 | flags |= NLM_F_DUMP; | |
130 | ||
131 | rd_prepare_msg(rd, command, &seq, flags); | |
132 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
133 | if (rd->port_idx) | |
134 | mnl_attr_put_u32(rd->nlh, | |
135 | RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
136 | ||
33552ade EA |
137 | if (command == RDMA_NLDEV_CMD_STAT_GET) |
138 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, | |
139 | RDMA_NLDEV_ATTR_RES_MR); | |
140 | ||
923aa825 LR |
141 | ret = rd_send_msg(rd); |
142 | if (ret) | |
143 | return ret; | |
144 | ||
145 | if (rd->json_output) | |
146 | jsonw_start_object(rd->jw); | |
147 | ret = rd_recv_msg(rd, callback, rd, seq); | |
148 | if (rd->json_output) | |
149 | jsonw_end_object(rd->jw); | |
150 | return ret; | |
151 | } | |
152 | ||
438fac3a | 153 | const char *qp_types_to_str(uint8_t idx) |
8ecac46a LR |
154 | { |
155 | static const char * const qp_types_str[] = { "SMI", "GSI", "RC", | |
156 | "UC", "UD", "RAW_IPV6", | |
157 | "RAW_ETHERTYPE", | |
158 | "UNKNOWN", "RAW_PACKET", | |
39307384 GP |
159 | "XRC_INI", "XRC_TGT", |
160 | [0xFF] = "DRIVER", | |
161 | }; | |
8ecac46a | 162 | |
39307384 | 163 | if (idx < ARRAY_SIZE(qp_types_str) && qp_types_str[idx]) |
8ecac46a LR |
164 | return qp_types_str[idx]; |
165 | return "UNKNOWN"; | |
166 | } | |
167 | ||
cc613127 | 168 | void print_comm(struct rd *rd, const char *str, struct nlattr **nla_line) |
8ecac46a LR |
169 | { |
170 | char tmp[18]; | |
171 | ||
4e2d9fc4 MZ |
172 | if (!str) |
173 | return; | |
174 | ||
8ecac46a LR |
175 | if (rd->json_output) { |
176 | /* Don't beatify output in JSON format */ | |
177 | jsonw_string_field(rd->jw, "comm", str); | |
178 | return; | |
179 | } | |
180 | ||
181 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) | |
182 | snprintf(tmp, sizeof(tmp), "%s", str); | |
183 | else | |
184 | snprintf(tmp, sizeof(tmp), "[%s]", str); | |
185 | ||
186 | pr_out("comm %s ", tmp); | |
187 | } | |
188 | ||
cc613127 | 189 | void print_dev(struct rd *rd, uint32_t idx, const char *name) |
b0b8e32c SW |
190 | { |
191 | if (rd->json_output) { | |
192 | jsonw_uint_field(rd->jw, "ifindex", idx); | |
193 | jsonw_string_field(rd->jw, "ifname", name); | |
194 | } else { | |
195 | pr_out("dev %s ", name); | |
196 | } | |
197 | } | |
198 | ||
438fac3a LR |
199 | void print_link(struct rd *rd, uint32_t idx, const char *name, uint32_t port, |
200 | struct nlattr **nla_line) | |
8ecac46a LR |
201 | { |
202 | if (rd->json_output) { | |
203 | jsonw_uint_field(rd->jw, "ifindex", idx); | |
204 | ||
205 | if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) | |
206 | jsonw_uint_field(rd->jw, "port", port); | |
207 | ||
208 | jsonw_string_field(rd->jw, "ifname", name); | |
209 | } else { | |
210 | if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) | |
211 | pr_out("link %s/%u ", name, port); | |
212 | else | |
213 | pr_out("link %s/- ", name); | |
214 | } | |
215 | } | |
216 | ||
cc613127 | 217 | char *get_task_name(uint32_t pid) |
8ecac46a LR |
218 | { |
219 | char *comm; | |
220 | FILE *f; | |
221 | ||
f9313484 LR |
222 | if (!pid) |
223 | return NULL; | |
224 | ||
8ecac46a LR |
225 | if (asprintf(&comm, "/proc/%d/comm", pid) < 0) |
226 | return NULL; | |
227 | ||
228 | f = fopen(comm, "r"); | |
229 | free(comm); | |
230 | if (!f) | |
231 | return NULL; | |
232 | ||
233 | if (fscanf(f, "%ms\n", &comm) != 1) | |
234 | comm = NULL; | |
235 | ||
236 | fclose(f); | |
237 | ||
238 | return comm; | |
239 | } | |
240 | ||
127ff956 LR |
241 | void print_key(struct rd *rd, const char *name, uint64_t val, |
242 | struct nlattr *nlattr) | |
96f59e7f | 243 | { |
127ff956 LR |
244 | if (!nlattr) |
245 | return; | |
246 | ||
96f59e7f LR |
247 | if (rd->json_output) |
248 | jsonw_xint_field(rd->jw, name, val); | |
249 | else | |
250 | pr_out("%s 0x%" PRIx64 " ", name, val); | |
251 | } | |
252 | ||
127ff956 LR |
253 | void res_print_uint(struct rd *rd, const char *name, uint64_t val, |
254 | struct nlattr *nlattr) | |
96f59e7f | 255 | { |
127ff956 LR |
256 | if (!nlattr) |
257 | return; | |
258 | ||
96f59e7f | 259 | if (rd->json_output) |
05846c9c | 260 | jsonw_u64_field(rd->jw, name, val); |
96f59e7f LR |
261 | else |
262 | pr_out("%s %" PRIu64 " ", name, val); | |
263 | } | |
264 | ||
5a823593 | 265 | RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true, 0); |
923aa825 LR |
266 | |
267 | static int res_show(struct rd *rd) | |
268 | { | |
269 | const struct rd_cmd cmds[] = { | |
270 | { NULL, res_no_args }, | |
8ecac46a | 271 | { "qp", res_qp }, |
9a362cc7 | 272 | { "cm_id", res_cm_id }, |
b0b8e32c | 273 | { "cq", res_cq }, |
8958a15c | 274 | { "mr", res_mr }, |
4060e4c0 | 275 | { "pd", res_pd }, |
923aa825 LR |
276 | { 0 } |
277 | }; | |
278 | ||
279 | /* | |
280 | * Special case to support "rdma res show DEV_NAME" | |
281 | */ | |
282 | if (rd_argc(rd) == 1 && dev_map_lookup(rd, false)) | |
283 | return rd_exec_dev(rd, _res_no_args); | |
284 | ||
285 | return rd_exec_cmd(rd, cmds, "parameter"); | |
286 | } | |
287 | ||
288 | int cmd_res(struct rd *rd) | |
289 | { | |
290 | const struct rd_cmd cmds[] = { | |
291 | { NULL, res_show }, | |
292 | { "show", res_show }, | |
293 | { "list", res_show }, | |
294 | { "help", res_help }, | |
295 | { 0 } | |
296 | }; | |
297 | ||
298 | return rd_exec_cmd(rd, cmds, "resource command"); | |
299 | } |