]>
Commit | Line | Data |
---|---|---|
5937552b MZ |
1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
2 | /* | |
3 | * rdma.c RDMA tool | |
4 | * Authors: Mark Zhang <markz@mellanox.com> | |
5 | */ | |
6 | ||
7 | #include "rdma.h" | |
8 | #include "res.h" | |
33552ade | 9 | #include "stat.h" |
5937552b MZ |
10 | #include <inttypes.h> |
11 | ||
12 | static int stat_help(struct rd *rd) | |
13 | { | |
14 | pr_out("Usage: %s [ OPTIONS ] statistic { COMMAND | help }\n", rd->filename); | |
15 | pr_out(" %s statistic OBJECT show\n", rd->filename); | |
16 | pr_out(" %s statistic OBJECT show link [ DEV/PORT_INDEX ] [ FILTER-NAME FILTER-VALUE ]\n", rd->filename); | |
1b2ca7ad | 17 | pr_out(" %s statistic OBJECT mode\n", rd->filename); |
887fc739 | 18 | pr_out(" %s statistic OBJECT set COUNTER_SCOPE [DEV/PORT_INDEX] auto {CRITERIA | off}\n", rd->filename); |
a6d0773e MZ |
19 | pr_out(" %s statistic OBJECT bind COUNTER_SCOPE [DEV/PORT_INDEX] [OBJECT-ID] [COUNTER-ID]\n", rd->filename); |
20 | pr_out(" %s statistic OBJECT unbind COUNTER_SCOPE [DEV/PORT_INDEX] [COUNTER-ID]\n", rd->filename); | |
a7137e51 MZ |
21 | pr_out(" %s statistic show\n", rd->filename); |
22 | pr_out(" %s statistic show link [ DEV/PORT_INDEX ]\n", rd->filename); | |
1b2ca7ad | 23 | pr_out("where OBJECT: = { qp }\n"); |
887fc739 MZ |
24 | pr_out(" CRITERIA : = { type }\n"); |
25 | pr_out(" COUNTER_SCOPE: = { link | dev }\n"); | |
5937552b MZ |
26 | pr_out("Examples:\n"); |
27 | pr_out(" %s statistic qp show\n", rd->filename); | |
28 | pr_out(" %s statistic qp show link mlx5_2/1\n", rd->filename); | |
1b2ca7ad MZ |
29 | pr_out(" %s statistic qp mode\n", rd->filename); |
30 | pr_out(" %s statistic qp mode link mlx5_0\n", rd->filename); | |
887fc739 MZ |
31 | pr_out(" %s statistic qp set link mlx5_2/1 auto type on\n", rd->filename); |
32 | pr_out(" %s statistic qp set link mlx5_2/1 auto off\n", rd->filename); | |
a6d0773e MZ |
33 | pr_out(" %s statistic qp bind link mlx5_2/1 lqpn 178\n", rd->filename); |
34 | pr_out(" %s statistic qp bind link mlx5_2/1 lqpn 178 cntn 4\n", rd->filename); | |
35 | pr_out(" %s statistic qp unbind link mlx5_2/1 cntn 4\n", rd->filename); | |
36 | pr_out(" %s statistic qp unbind link mlx5_2/1 cntn 4 lqpn 178\n", rd->filename); | |
a7137e51 MZ |
37 | pr_out(" %s statistic show\n", rd->filename); |
38 | pr_out(" %s statistic show link mlx5_2/1\n", rd->filename); | |
5937552b MZ |
39 | |
40 | return 0; | |
41 | } | |
42 | ||
1b2ca7ad MZ |
43 | struct counter_param { |
44 | char *name; | |
45 | uint32_t attr; | |
46 | }; | |
47 | ||
48 | static struct counter_param auto_params[] = { | |
49 | { "type", RDMA_COUNTER_MASK_QP_TYPE, }, | |
50 | { NULL }, | |
51 | }; | |
52 | ||
53 | static int prepare_auto_mode_str(struct nlattr **tb, uint32_t mask, | |
54 | char *output, int len) | |
55 | { | |
56 | char s[] = "qp auto"; | |
57 | int i, outlen = strlen(s); | |
58 | ||
59 | memset(output, 0, len); | |
60 | snprintf(output, len, "%s", s); | |
61 | ||
62 | if (mask) { | |
63 | for (i = 0; auto_params[i].name != NULL; i++) { | |
64 | if (mask & auto_params[i].attr) { | |
65 | outlen += strlen(auto_params[i].name) + 1; | |
66 | if (outlen >= len) | |
67 | return -EINVAL; | |
68 | strcat(output, " "); | |
69 | strcat(output, auto_params[i].name); | |
70 | } | |
71 | } | |
72 | ||
73 | if (outlen + strlen(" on") >= len) | |
74 | return -EINVAL; | |
75 | strcat(output, " on"); | |
76 | } else { | |
77 | if (outlen + strlen(" off") >= len) | |
78 | return -EINVAL; | |
79 | strcat(output, " off"); | |
80 | } | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
85 | static int qp_link_get_mode_parse_cb(const struct nlmsghdr *nlh, void *data) | |
86 | { | |
87 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
88 | uint32_t mode = 0, mask = 0; | |
89 | char output[128] = {}; | |
90 | struct rd *rd = data; | |
91 | uint32_t idx, port; | |
92 | const char *name; | |
93 | ||
94 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
95 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME]) | |
96 | return MNL_CB_ERROR; | |
97 | ||
98 | if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { | |
99 | pr_err("This tool doesn't support switches yet\n"); | |
100 | return MNL_CB_ERROR; | |
101 | } | |
102 | ||
103 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
104 | port = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
105 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
106 | if (tb[RDMA_NLDEV_ATTR_STAT_MODE]) | |
107 | mode = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_STAT_MODE]); | |
108 | ||
109 | if (mode == RDMA_COUNTER_MODE_AUTO) { | |
110 | if (!tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]) | |
111 | return MNL_CB_ERROR; | |
112 | mask = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK]); | |
113 | prepare_auto_mode_str(tb, mask, output, sizeof(output)); | |
114 | } else { | |
115 | snprintf(output, sizeof(output), "qp auto off"); | |
116 | } | |
117 | ||
b0a688a5 IK |
118 | open_json_object(NULL); |
119 | print_link(rd, idx, name, port, tb); | |
120 | print_color_string(PRINT_ANY, COLOR_NONE, "mode", "mode %s ", output); | |
121 | newline(rd); | |
1b2ca7ad MZ |
122 | return MNL_CB_OK; |
123 | } | |
124 | ||
125 | static int stat_one_qp_link_get_mode(struct rd *rd) | |
126 | { | |
127 | uint32_t seq; | |
128 | int ret; | |
129 | ||
130 | if (!rd->port_idx) | |
131 | return 0; | |
132 | ||
133 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, | |
134 | &seq, (NLM_F_REQUEST | NLM_F_ACK)); | |
135 | ||
136 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
137 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
138 | /* Make RDMA_NLDEV_ATTR_STAT_MODE valid so that kernel knows | |
139 | * return only mode instead of all counters | |
140 | */ | |
141 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE, | |
142 | RDMA_COUNTER_MODE_MANUAL); | |
143 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP); | |
144 | ret = rd_send_msg(rd); | |
145 | if (ret) | |
146 | return ret; | |
147 | ||
1b2ca7ad | 148 | ret = rd_recv_msg(rd, qp_link_get_mode_parse_cb, rd, seq); |
1b2ca7ad MZ |
149 | return ret; |
150 | } | |
151 | ||
152 | static int stat_qp_link_get_mode(struct rd *rd) | |
153 | { | |
154 | return rd_exec_link(rd, stat_one_qp_link_get_mode, false); | |
155 | } | |
156 | ||
157 | static int stat_qp_get_mode(struct rd *rd) | |
158 | { | |
159 | const struct rd_cmd cmds[] = { | |
160 | { NULL, stat_qp_link_get_mode }, | |
161 | { "link", stat_qp_link_get_mode }, | |
162 | { "help", stat_help }, | |
163 | { 0 } | |
164 | }; | |
165 | ||
166 | return rd_exec_cmd(rd, cmds, "parameter"); | |
167 | } | |
168 | ||
33552ade | 169 | int res_get_hwcounters(struct rd *rd, struct nlattr *hwc_table, bool print) |
5937552b MZ |
170 | { |
171 | struct nlattr *nla_entry; | |
172 | const char *nm; | |
173 | uint64_t v; | |
174 | int err; | |
175 | ||
176 | mnl_attr_for_each_nested(nla_entry, hwc_table) { | |
177 | struct nlattr *hw_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
178 | ||
179 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, hw_line); | |
180 | if (err != MNL_CB_OK) | |
181 | return -EINVAL; | |
182 | ||
183 | if (!hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] || | |
184 | !hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE]) { | |
185 | return -EINVAL; | |
186 | } | |
187 | ||
188 | if (!print) | |
189 | continue; | |
190 | ||
191 | nm = mnl_attr_get_str(hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME]); | |
192 | v = mnl_attr_get_u64(hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE]); | |
193 | if (rd->pretty_output && !rd->json_output) | |
194 | newline_indent(rd); | |
195 | res_print_uint(rd, nm, v, hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME]); | |
196 | } | |
197 | ||
198 | return MNL_CB_OK; | |
199 | } | |
200 | ||
201 | static int res_counter_line(struct rd *rd, const char *name, int index, | |
202 | struct nlattr **nla_line) | |
203 | { | |
204 | uint32_t cntn, port = 0, pid = 0, qpn; | |
205 | struct nlattr *hwc_table, *qp_table; | |
206 | struct nlattr *nla_entry; | |
207 | const char *comm = NULL; | |
208 | bool isfirst; | |
209 | int err; | |
210 | ||
211 | if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]) | |
212 | port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
213 | ||
214 | hwc_table = nla_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]; | |
215 | qp_table = nla_line[RDMA_NLDEV_ATTR_RES_QP]; | |
216 | if (!hwc_table || !qp_table || | |
217 | !nla_line[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) | |
218 | return MNL_CB_ERROR; | |
219 | ||
220 | cntn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]); | |
221 | if (rd_is_filtered_attr(rd, "cntn", cntn, | |
222 | nla_line[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])) | |
223 | return MNL_CB_OK; | |
224 | ||
225 | if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) { | |
226 | pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]); | |
227 | comm = get_task_name(pid); | |
228 | } | |
229 | if (rd_is_filtered_attr(rd, "pid", pid, | |
230 | nla_line[RDMA_NLDEV_ATTR_RES_PID])) | |
231 | return MNL_CB_OK; | |
232 | ||
233 | if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]) | |
234 | comm = (char *)mnl_attr_get_str( | |
235 | nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]); | |
236 | ||
237 | mnl_attr_for_each_nested(nla_entry, qp_table) { | |
238 | struct nlattr *qp_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
239 | ||
240 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, qp_line); | |
241 | if (err != MNL_CB_OK) | |
242 | return -EINVAL; | |
243 | ||
244 | if (!qp_line[RDMA_NLDEV_ATTR_RES_LQPN]) | |
245 | return -EINVAL; | |
246 | ||
247 | qpn = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_RES_LQPN]); | |
248 | if (rd_is_filtered_attr(rd, "lqpn", qpn, | |
249 | qp_line[RDMA_NLDEV_ATTR_RES_LQPN])) | |
250 | return MNL_CB_OK; | |
251 | } | |
252 | ||
253 | err = res_get_hwcounters(rd, hwc_table, false); | |
254 | if (err != MNL_CB_OK) | |
255 | return err; | |
b0a688a5 IK |
256 | open_json_object(NULL); |
257 | print_link(rd, index, name, port, nla_line); | |
258 | print_color_uint(PRINT_ANY, COLOR_NONE, "cntn", "cntn %u ", cntn); | |
5937552b MZ |
259 | res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]); |
260 | print_comm(rd, comm, nla_line); | |
5937552b | 261 | res_get_hwcounters(rd, hwc_table, true); |
5937552b | 262 | isfirst = true; |
b0a688a5 IK |
263 | open_json_array(PRINT_JSON, "lqpn"); |
264 | print_color_string(PRINT_FP, COLOR_NONE, NULL, "\n LQPN: <", NULL); | |
5937552b MZ |
265 | mnl_attr_for_each_nested(nla_entry, qp_table) { |
266 | struct nlattr *qp_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
5937552b MZ |
267 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, qp_line); |
268 | if (err != MNL_CB_OK) | |
269 | return -EINVAL; | |
270 | ||
271 | if (!qp_line[RDMA_NLDEV_ATTR_RES_LQPN]) | |
272 | return -EINVAL; | |
273 | ||
274 | qpn = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_RES_LQPN]); | |
b0a688a5 IK |
275 | if (!isfirst) |
276 | print_color_string(PRINT_FP, COLOR_NONE, NULL, ",", | |
277 | NULL); | |
278 | print_color_uint(PRINT_ANY, COLOR_NONE, NULL, "%d", qpn); | |
5937552b MZ |
279 | isfirst = false; |
280 | } | |
b0a688a5 IK |
281 | close_json_array(PRINT_ANY, ">"); |
282 | newline(rd); | |
5937552b MZ |
283 | return MNL_CB_OK; |
284 | } | |
285 | ||
286 | static int stat_qp_show_parse_cb(const struct nlmsghdr *nlh, void *data) | |
287 | { | |
288 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
289 | struct nlattr *nla_table, *nla_entry; | |
290 | struct rd *rd = data; | |
291 | const char *name; | |
292 | uint32_t idx; | |
293 | int ret; | |
294 | ||
295 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
296 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
297 | !tb[RDMA_NLDEV_ATTR_STAT_COUNTER]) | |
298 | return MNL_CB_ERROR; | |
299 | ||
300 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
301 | idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
302 | nla_table = tb[RDMA_NLDEV_ATTR_STAT_COUNTER]; | |
303 | ||
304 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
305 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
306 | ||
307 | ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
308 | if (ret != MNL_CB_OK) | |
309 | break; | |
310 | ||
311 | ret = res_counter_line(rd, name, idx, nla_line); | |
312 | if (ret != MNL_CB_OK) | |
313 | break; | |
314 | } | |
315 | ||
316 | return ret; | |
317 | } | |
318 | ||
319 | static const struct filters stat_valid_filters[MAX_NUMBER_OF_FILTERS] = { | |
320 | { .name = "cntn", .is_number = true }, | |
321 | { .name = "lqpn", .is_number = true }, | |
322 | { .name = "pid", .is_number = true }, | |
323 | }; | |
324 | ||
325 | static int stat_qp_show_one_link(struct rd *rd) | |
326 | { | |
327 | int flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP; | |
328 | uint32_t seq; | |
329 | int ret; | |
330 | ||
331 | if (!rd->port_idx) | |
332 | return 0; | |
333 | ||
334 | ret = rd_build_filter(rd, stat_valid_filters); | |
335 | if (ret) | |
336 | return ret; | |
337 | ||
338 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, &seq, flags); | |
339 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
340 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
341 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP); | |
342 | ret = rd_send_msg(rd); | |
343 | if (ret) | |
344 | return ret; | |
345 | ||
5937552b | 346 | ret = rd_recv_msg(rd, stat_qp_show_parse_cb, rd, seq); |
5937552b MZ |
347 | return ret; |
348 | } | |
349 | ||
350 | static int stat_qp_show_link(struct rd *rd) | |
351 | { | |
352 | return rd_exec_link(rd, stat_qp_show_one_link, false); | |
353 | } | |
354 | ||
355 | static int stat_qp_show(struct rd *rd) | |
356 | { | |
357 | const struct rd_cmd cmds[] = { | |
358 | { NULL, stat_qp_show_link }, | |
359 | { "link", stat_qp_show_link }, | |
360 | { "help", stat_help }, | |
361 | { 0 } | |
362 | }; | |
363 | ||
364 | return rd_exec_cmd(rd, cmds, "parameter"); | |
365 | } | |
366 | ||
887fc739 MZ |
367 | static int stat_qp_set_link_auto_sendmsg(struct rd *rd, uint32_t mask) |
368 | { | |
369 | uint32_t seq; | |
370 | ||
371 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_SET, | |
372 | &seq, (NLM_F_REQUEST | NLM_F_ACK)); | |
373 | ||
374 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
375 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
376 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP); | |
377 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE, | |
378 | RDMA_COUNTER_MODE_AUTO); | |
379 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask); | |
380 | ||
381 | return rd_sendrecv_msg(rd, seq); | |
382 | } | |
383 | ||
384 | static int stat_one_qp_set_link_auto_off(struct rd *rd) | |
385 | { | |
386 | return stat_qp_set_link_auto_sendmsg(rd, 0); | |
387 | } | |
388 | ||
389 | static int stat_one_qp_set_auto_type_on(struct rd *rd) | |
390 | { | |
391 | return stat_qp_set_link_auto_sendmsg(rd, RDMA_COUNTER_MASK_QP_TYPE); | |
392 | } | |
393 | ||
394 | static int stat_one_qp_set_link_auto_type(struct rd *rd) | |
395 | { | |
396 | const struct rd_cmd cmds[] = { | |
397 | { NULL, stat_help }, | |
398 | { "on", stat_one_qp_set_auto_type_on }, | |
399 | { 0 } | |
400 | }; | |
401 | ||
402 | return rd_exec_cmd(rd, cmds, "parameter"); | |
403 | } | |
404 | ||
405 | static int stat_one_qp_set_link_auto(struct rd *rd) | |
406 | { | |
407 | const struct rd_cmd cmds[] = { | |
408 | { NULL, stat_one_qp_link_get_mode }, | |
409 | { "off", stat_one_qp_set_link_auto_off }, | |
410 | { "type", stat_one_qp_set_link_auto_type }, | |
411 | { 0 } | |
412 | }; | |
413 | ||
414 | return rd_exec_cmd(rd, cmds, "parameter"); | |
415 | } | |
416 | ||
417 | static int stat_one_qp_set_link(struct rd *rd) | |
418 | { | |
419 | const struct rd_cmd cmds[] = { | |
420 | { NULL, stat_one_qp_link_get_mode }, | |
421 | { "auto", stat_one_qp_set_link_auto }, | |
422 | { 0 } | |
423 | }; | |
424 | ||
425 | if (!rd->port_idx) | |
426 | return 0; | |
427 | ||
428 | return rd_exec_cmd(rd, cmds, "parameter"); | |
429 | } | |
430 | ||
431 | static int stat_qp_set_link(struct rd *rd) | |
432 | { | |
433 | return rd_exec_link(rd, stat_one_qp_set_link, false); | |
434 | } | |
435 | ||
436 | static int stat_qp_set(struct rd *rd) | |
437 | { | |
438 | const struct rd_cmd cmds[] = { | |
439 | { NULL, stat_help }, | |
440 | { "link", stat_qp_set_link }, | |
441 | { "help", stat_help }, | |
442 | { 0 } | |
443 | }; | |
444 | ||
445 | return rd_exec_cmd(rd, cmds, "parameter"); | |
446 | } | |
447 | ||
a6d0773e MZ |
448 | static int stat_get_arg(struct rd *rd, const char *arg) |
449 | { | |
450 | int value = 0; | |
451 | char *endp; | |
452 | ||
453 | if (strcmpx(rd_argv(rd), arg) != 0) | |
454 | return -EINVAL; | |
455 | ||
456 | rd_arg_inc(rd); | |
457 | value = strtol(rd_argv(rd), &endp, 10); | |
458 | rd_arg_inc(rd); | |
459 | ||
460 | return value; | |
461 | } | |
462 | ||
463 | static int stat_one_qp_bind(struct rd *rd) | |
464 | { | |
465 | int lqpn = 0, cntn = 0, ret; | |
466 | uint32_t seq; | |
467 | ||
468 | if (rd_no_arg(rd)) { | |
469 | stat_help(rd); | |
470 | return -EINVAL; | |
471 | } | |
472 | ||
473 | ret = rd_build_filter(rd, stat_valid_filters); | |
474 | if (ret) | |
475 | return ret; | |
476 | ||
477 | lqpn = stat_get_arg(rd, "lqpn"); | |
478 | ||
479 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_SET, | |
480 | &seq, (NLM_F_REQUEST | NLM_F_ACK)); | |
481 | ||
482 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE, | |
483 | RDMA_COUNTER_MODE_MANUAL); | |
484 | ||
485 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP); | |
486 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
487 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
488 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_RES_LQPN, lqpn); | |
489 | ||
490 | if (rd_argc(rd)) { | |
491 | cntn = stat_get_arg(rd, "cntn"); | |
492 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, | |
493 | cntn); | |
494 | } | |
495 | ||
496 | return rd_sendrecv_msg(rd, seq); | |
497 | } | |
498 | ||
499 | static int do_stat_qp_unbind_lqpn(struct rd *rd, uint32_t cntn, uint32_t lqpn) | |
500 | { | |
501 | uint32_t seq; | |
502 | ||
503 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_DEL, | |
504 | &seq, (NLM_F_REQUEST | NLM_F_ACK)); | |
505 | ||
506 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE, | |
507 | RDMA_COUNTER_MODE_MANUAL); | |
508 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP); | |
509 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
510 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
511 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn); | |
512 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_RES_LQPN, lqpn); | |
513 | ||
514 | return rd_sendrecv_msg(rd, seq); | |
515 | } | |
516 | ||
517 | static int stat_get_counter_parse_cb(const struct nlmsghdr *nlh, void *data) | |
518 | { | |
519 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
520 | struct nlattr *nla_table, *nla_entry; | |
521 | struct rd *rd = data; | |
522 | uint32_t lqpn, cntn; | |
523 | int err; | |
524 | ||
525 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
526 | ||
527 | if (!tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]) | |
528 | return MNL_CB_ERROR; | |
529 | cntn = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]); | |
530 | ||
531 | nla_table = tb[RDMA_NLDEV_ATTR_RES_QP]; | |
532 | if (!nla_table) | |
533 | return MNL_CB_ERROR; | |
534 | ||
535 | mnl_attr_for_each_nested(nla_entry, nla_table) { | |
536 | struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {}; | |
537 | ||
538 | err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line); | |
539 | if (err != MNL_CB_OK) | |
540 | return -EINVAL; | |
541 | ||
542 | if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN]) | |
543 | return -EINVAL; | |
544 | ||
545 | lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]); | |
546 | err = do_stat_qp_unbind_lqpn(rd, cntn, lqpn); | |
547 | if (err) | |
548 | return MNL_CB_ERROR; | |
549 | } | |
550 | ||
551 | return MNL_CB_OK; | |
552 | } | |
553 | ||
554 | static int stat_one_qp_unbind(struct rd *rd) | |
555 | { | |
556 | int flags = NLM_F_REQUEST | NLM_F_ACK, ret; | |
557 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
558 | int lqpn = 0, cntn = 0; | |
559 | unsigned int portid; | |
560 | uint32_t seq; | |
561 | ||
562 | ret = rd_build_filter(rd, stat_valid_filters); | |
563 | if (ret) | |
564 | return ret; | |
565 | ||
566 | cntn = stat_get_arg(rd, "cntn"); | |
567 | if (rd_argc(rd)) { | |
568 | lqpn = stat_get_arg(rd, "lqpn"); | |
569 | return do_stat_qp_unbind_lqpn(rd, cntn, lqpn); | |
570 | } | |
571 | ||
572 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, &seq, flags); | |
573 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
574 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
575 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP); | |
576 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn); | |
577 | ret = rd_send_msg(rd); | |
578 | if (ret) | |
579 | return ret; | |
580 | ||
581 | ||
582 | /* Can't use rd_recv_msg() since the callback also calls it (recursively), | |
583 | * then rd_recv_msg() always return -1 here | |
584 | */ | |
585 | portid = mnl_socket_get_portid(rd->nl); | |
586 | ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf)); | |
587 | if (ret <= 0) | |
588 | return ret; | |
589 | ||
590 | ret = mnl_cb_run(buf, ret, seq, portid, stat_get_counter_parse_cb, rd); | |
591 | mnl_socket_close(rd->nl); | |
592 | if (ret != MNL_CB_OK) | |
593 | return ret; | |
594 | ||
595 | return 0; | |
596 | } | |
597 | ||
598 | static int stat_qp_bind_link(struct rd *rd) | |
599 | { | |
600 | return rd_exec_link(rd, stat_one_qp_bind, true); | |
601 | } | |
602 | ||
603 | static int stat_qp_bind(struct rd *rd) | |
604 | { | |
605 | const struct rd_cmd cmds[] = { | |
606 | { NULL, stat_help }, | |
607 | { "link", stat_qp_bind_link }, | |
608 | { "help", stat_help }, | |
609 | { 0 }, | |
610 | }; | |
611 | ||
612 | return rd_exec_cmd(rd, cmds, "parameter"); | |
613 | } | |
614 | ||
615 | static int stat_qp_unbind_link(struct rd *rd) | |
616 | { | |
617 | return rd_exec_link(rd, stat_one_qp_unbind, true); | |
618 | } | |
619 | ||
620 | static int stat_qp_unbind(struct rd *rd) | |
621 | { | |
622 | const struct rd_cmd cmds[] = { | |
623 | { NULL, stat_help }, | |
624 | { "link", stat_qp_unbind_link }, | |
625 | { "help", stat_help }, | |
626 | { 0 }, | |
627 | }; | |
628 | ||
629 | return rd_exec_cmd(rd, cmds, "parameter"); | |
630 | } | |
631 | ||
5937552b MZ |
632 | static int stat_qp(struct rd *rd) |
633 | { | |
634 | const struct rd_cmd cmds[] = { | |
635 | { NULL, stat_qp_show }, | |
636 | { "show", stat_qp_show }, | |
637 | { "list", stat_qp_show }, | |
1b2ca7ad | 638 | { "mode", stat_qp_get_mode }, |
887fc739 | 639 | { "set", stat_qp_set }, |
a6d0773e MZ |
640 | { "bind", stat_qp_bind }, |
641 | { "unbind", stat_qp_unbind }, | |
5937552b MZ |
642 | { "help", stat_help }, |
643 | { 0 } | |
644 | }; | |
645 | ||
646 | return rd_exec_cmd(rd, cmds, "parameter"); | |
647 | } | |
648 | ||
a7137e51 MZ |
649 | static int stat_show_parse_cb(const struct nlmsghdr *nlh, void *data) |
650 | { | |
651 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
652 | struct rd *rd = data; | |
653 | const char *name; | |
654 | uint32_t port; | |
655 | int ret; | |
656 | ||
657 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
658 | if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] || | |
659 | !tb[RDMA_NLDEV_ATTR_PORT_INDEX] || | |
660 | !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS]) | |
661 | return MNL_CB_ERROR; | |
662 | ||
663 | name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
664 | port = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
b0a688a5 IK |
665 | open_json_object(NULL); |
666 | print_color_string(PRINT_ANY, COLOR_NONE, "ifname", "link %s/", name); | |
667 | print_color_uint(PRINT_ANY, COLOR_NONE, "port", "%u ", port); | |
a7137e51 MZ |
668 | ret = res_get_hwcounters(rd, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS], true); |
669 | ||
b0a688a5 | 670 | newline(rd); |
a7137e51 MZ |
671 | return ret; |
672 | } | |
673 | ||
674 | static int stat_show_one_link(struct rd *rd) | |
675 | { | |
676 | int flags = NLM_F_REQUEST | NLM_F_ACK; | |
677 | uint32_t seq; | |
678 | int ret; | |
679 | ||
680 | if (!rd->port_idx) | |
681 | return 0; | |
682 | ||
683 | rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, &seq, flags); | |
684 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx); | |
685 | mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx); | |
686 | ret = rd_send_msg(rd); | |
687 | if (ret) | |
688 | return ret; | |
689 | ||
690 | return rd_recv_msg(rd, stat_show_parse_cb, rd, seq); | |
691 | } | |
692 | ||
693 | static int stat_show_link(struct rd *rd) | |
694 | { | |
695 | return rd_exec_link(rd, stat_show_one_link, false); | |
696 | } | |
697 | ||
698 | static int stat_show(struct rd *rd) | |
699 | { | |
700 | const struct rd_cmd cmds[] = { | |
701 | { NULL, stat_show_link }, | |
702 | { "link", stat_show_link }, | |
33552ade | 703 | { "mr", stat_mr }, |
a7137e51 MZ |
704 | { "help", stat_help }, |
705 | { 0 } | |
706 | }; | |
707 | ||
708 | return rd_exec_cmd(rd, cmds, "parameter"); | |
709 | } | |
710 | ||
5937552b MZ |
711 | int cmd_stat(struct rd *rd) |
712 | { | |
713 | const struct rd_cmd cmds[] = { | |
a7137e51 MZ |
714 | { NULL, stat_show }, |
715 | { "show", stat_show }, | |
716 | { "list", stat_show }, | |
5937552b MZ |
717 | { "help", stat_help }, |
718 | { "qp", stat_qp }, | |
33552ade | 719 | { "mr", stat_mr }, |
5937552b MZ |
720 | { 0 } |
721 | }; | |
722 | ||
723 | return rd_exec_cmd(rd, cmds, "statistic command"); | |
724 | } |