]> git.proxmox.com Git - mirror_iproute2.git/blame - rdma/stat.c
Merge branch 'master' into next
[mirror_iproute2.git] / rdma / stat.c
CommitLineData
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
12static 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
43struct counter_param {
44 char *name;
45 uint32_t attr;
46};
47
48static struct counter_param auto_params[] = {
49 { "type", RDMA_COUNTER_MASK_QP_TYPE, },
50 { NULL },
51};
52
53static 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
85static 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
118 if (rd->json_output) {
119 jsonw_uint_field(rd->jw, "ifindex", idx);
120 jsonw_uint_field(rd->jw, "port", port);
121 jsonw_string_field(rd->jw, "mode", output);
122 } else {
123 pr_out("%u/%u: %s/%u: %s\n", idx, port, name, port, output);
124 }
125
126 return MNL_CB_OK;
127}
128
129static int stat_one_qp_link_get_mode(struct rd *rd)
130{
131 uint32_t seq;
132 int ret;
133
134 if (!rd->port_idx)
135 return 0;
136
137 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET,
138 &seq, (NLM_F_REQUEST | NLM_F_ACK));
139
140 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
141 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
142 /* Make RDMA_NLDEV_ATTR_STAT_MODE valid so that kernel knows
143 * return only mode instead of all counters
144 */
145 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE,
146 RDMA_COUNTER_MODE_MANUAL);
147 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP);
148 ret = rd_send_msg(rd);
149 if (ret)
150 return ret;
151
152 if (rd->json_output)
153 jsonw_start_object(rd->jw);
154 ret = rd_recv_msg(rd, qp_link_get_mode_parse_cb, rd, seq);
155 if (rd->json_output)
156 jsonw_end_object(rd->jw);
157
158 return ret;
159}
160
161static int stat_qp_link_get_mode(struct rd *rd)
162{
163 return rd_exec_link(rd, stat_one_qp_link_get_mode, false);
164}
165
166static int stat_qp_get_mode(struct rd *rd)
167{
168 const struct rd_cmd cmds[] = {
169 { NULL, stat_qp_link_get_mode },
170 { "link", stat_qp_link_get_mode },
171 { "help", stat_help },
172 { 0 }
173 };
174
175 return rd_exec_cmd(rd, cmds, "parameter");
176}
177
33552ade 178int res_get_hwcounters(struct rd *rd, struct nlattr *hwc_table, bool print)
5937552b
MZ
179{
180 struct nlattr *nla_entry;
181 const char *nm;
182 uint64_t v;
183 int err;
184
185 mnl_attr_for_each_nested(nla_entry, hwc_table) {
186 struct nlattr *hw_line[RDMA_NLDEV_ATTR_MAX] = {};
187
188 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, hw_line);
189 if (err != MNL_CB_OK)
190 return -EINVAL;
191
192 if (!hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] ||
193 !hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE]) {
194 return -EINVAL;
195 }
196
197 if (!print)
198 continue;
199
200 nm = mnl_attr_get_str(hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME]);
201 v = mnl_attr_get_u64(hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE]);
202 if (rd->pretty_output && !rd->json_output)
203 newline_indent(rd);
204 res_print_uint(rd, nm, v, hw_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME]);
205 }
206
207 return MNL_CB_OK;
208}
209
210static int res_counter_line(struct rd *rd, const char *name, int index,
211 struct nlattr **nla_line)
212{
213 uint32_t cntn, port = 0, pid = 0, qpn;
214 struct nlattr *hwc_table, *qp_table;
215 struct nlattr *nla_entry;
216 const char *comm = NULL;
217 bool isfirst;
218 int err;
219
220 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
221 port = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_PORT_INDEX]);
222
223 hwc_table = nla_line[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS];
224 qp_table = nla_line[RDMA_NLDEV_ATTR_RES_QP];
225 if (!hwc_table || !qp_table ||
226 !nla_line[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
227 return MNL_CB_ERROR;
228
229 cntn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
230 if (rd_is_filtered_attr(rd, "cntn", cntn,
231 nla_line[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]))
232 return MNL_CB_OK;
233
234 if (nla_line[RDMA_NLDEV_ATTR_RES_PID]) {
235 pid = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_PID]);
236 comm = get_task_name(pid);
237 }
238 if (rd_is_filtered_attr(rd, "pid", pid,
239 nla_line[RDMA_NLDEV_ATTR_RES_PID]))
240 return MNL_CB_OK;
241
242 if (nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME])
243 comm = (char *)mnl_attr_get_str(
244 nla_line[RDMA_NLDEV_ATTR_RES_KERN_NAME]);
245
246 mnl_attr_for_each_nested(nla_entry, qp_table) {
247 struct nlattr *qp_line[RDMA_NLDEV_ATTR_MAX] = {};
248
249 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, qp_line);
250 if (err != MNL_CB_OK)
251 return -EINVAL;
252
253 if (!qp_line[RDMA_NLDEV_ATTR_RES_LQPN])
254 return -EINVAL;
255
256 qpn = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_RES_LQPN]);
257 if (rd_is_filtered_attr(rd, "lqpn", qpn,
258 qp_line[RDMA_NLDEV_ATTR_RES_LQPN]))
259 return MNL_CB_OK;
260 }
261
262 err = res_get_hwcounters(rd, hwc_table, false);
263 if (err != MNL_CB_OK)
264 return err;
265
266 if (rd->json_output) {
267 jsonw_string_field(rd->jw, "ifname", name);
268 if (port)
269 jsonw_uint_field(rd->jw, "port", port);
270 jsonw_uint_field(rd->jw, "cntn", cntn);
271 } else {
272 if (port)
273 pr_out("link %s/%u cntn %u ", name, port, cntn);
274 else
275 pr_out("dev %s cntn %u ", name, cntn);
276 }
277
278 res_print_uint(rd, "pid", pid, nla_line[RDMA_NLDEV_ATTR_RES_PID]);
279 print_comm(rd, comm, nla_line);
280
281 res_get_hwcounters(rd, hwc_table, true);
282
283 isfirst = true;
284 mnl_attr_for_each_nested(nla_entry, qp_table) {
285 struct nlattr *qp_line[RDMA_NLDEV_ATTR_MAX] = {};
286
287 if (isfirst && !rd->json_output)
288 pr_out("\n LQPN: <");
289
290 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, qp_line);
291 if (err != MNL_CB_OK)
292 return -EINVAL;
293
294 if (!qp_line[RDMA_NLDEV_ATTR_RES_LQPN])
295 return -EINVAL;
296
297 qpn = mnl_attr_get_u32(qp_line[RDMA_NLDEV_ATTR_RES_LQPN]);
298 if (rd->json_output) {
299 jsonw_uint_field(rd->jw, "lqpn", qpn);
300 } else {
301 if (isfirst)
302 pr_out("%d", qpn);
303 else
304 pr_out(", %d", qpn);
305 }
306 isfirst = false;
307 }
308
309 if (!rd->json_output)
310 pr_out(">\n");
311 return MNL_CB_OK;
312}
313
314static int stat_qp_show_parse_cb(const struct nlmsghdr *nlh, void *data)
315{
316 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
317 struct nlattr *nla_table, *nla_entry;
318 struct rd *rd = data;
319 const char *name;
320 uint32_t idx;
321 int ret;
322
323 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
324 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
325 !tb[RDMA_NLDEV_ATTR_STAT_COUNTER])
326 return MNL_CB_ERROR;
327
328 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
329 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
330 nla_table = tb[RDMA_NLDEV_ATTR_STAT_COUNTER];
331
332 mnl_attr_for_each_nested(nla_entry, nla_table) {
333 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
334
335 ret = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
336 if (ret != MNL_CB_OK)
337 break;
338
339 ret = res_counter_line(rd, name, idx, nla_line);
340 if (ret != MNL_CB_OK)
341 break;
342 }
343
344 return ret;
345}
346
347static const struct filters stat_valid_filters[MAX_NUMBER_OF_FILTERS] = {
348 { .name = "cntn", .is_number = true },
349 { .name = "lqpn", .is_number = true },
350 { .name = "pid", .is_number = true },
351};
352
353static int stat_qp_show_one_link(struct rd *rd)
354{
355 int flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP;
356 uint32_t seq;
357 int ret;
358
359 if (!rd->port_idx)
360 return 0;
361
362 ret = rd_build_filter(rd, stat_valid_filters);
363 if (ret)
364 return ret;
365
366 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, &seq, flags);
367 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
368 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
369 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP);
370 ret = rd_send_msg(rd);
371 if (ret)
372 return ret;
373
374 if (rd->json_output)
375 jsonw_start_object(rd->jw);
376 ret = rd_recv_msg(rd, stat_qp_show_parse_cb, rd, seq);
377 if (rd->json_output)
378 jsonw_end_object(rd->jw);
379
380 return ret;
381}
382
383static int stat_qp_show_link(struct rd *rd)
384{
385 return rd_exec_link(rd, stat_qp_show_one_link, false);
386}
387
388static int stat_qp_show(struct rd *rd)
389{
390 const struct rd_cmd cmds[] = {
391 { NULL, stat_qp_show_link },
392 { "link", stat_qp_show_link },
393 { "help", stat_help },
394 { 0 }
395 };
396
397 return rd_exec_cmd(rd, cmds, "parameter");
398}
399
887fc739
MZ
400static int stat_qp_set_link_auto_sendmsg(struct rd *rd, uint32_t mask)
401{
402 uint32_t seq;
403
404 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_SET,
405 &seq, (NLM_F_REQUEST | NLM_F_ACK));
406
407 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
408 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
409 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP);
410 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE,
411 RDMA_COUNTER_MODE_AUTO);
412 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK, mask);
413
414 return rd_sendrecv_msg(rd, seq);
415}
416
417static int stat_one_qp_set_link_auto_off(struct rd *rd)
418{
419 return stat_qp_set_link_auto_sendmsg(rd, 0);
420}
421
422static int stat_one_qp_set_auto_type_on(struct rd *rd)
423{
424 return stat_qp_set_link_auto_sendmsg(rd, RDMA_COUNTER_MASK_QP_TYPE);
425}
426
427static int stat_one_qp_set_link_auto_type(struct rd *rd)
428{
429 const struct rd_cmd cmds[] = {
430 { NULL, stat_help },
431 { "on", stat_one_qp_set_auto_type_on },
432 { 0 }
433 };
434
435 return rd_exec_cmd(rd, cmds, "parameter");
436}
437
438static int stat_one_qp_set_link_auto(struct rd *rd)
439{
440 const struct rd_cmd cmds[] = {
441 { NULL, stat_one_qp_link_get_mode },
442 { "off", stat_one_qp_set_link_auto_off },
443 { "type", stat_one_qp_set_link_auto_type },
444 { 0 }
445 };
446
447 return rd_exec_cmd(rd, cmds, "parameter");
448}
449
450static int stat_one_qp_set_link(struct rd *rd)
451{
452 const struct rd_cmd cmds[] = {
453 { NULL, stat_one_qp_link_get_mode },
454 { "auto", stat_one_qp_set_link_auto },
455 { 0 }
456 };
457
458 if (!rd->port_idx)
459 return 0;
460
461 return rd_exec_cmd(rd, cmds, "parameter");
462}
463
464static int stat_qp_set_link(struct rd *rd)
465{
466 return rd_exec_link(rd, stat_one_qp_set_link, false);
467}
468
469static int stat_qp_set(struct rd *rd)
470{
471 const struct rd_cmd cmds[] = {
472 { NULL, stat_help },
473 { "link", stat_qp_set_link },
474 { "help", stat_help },
475 { 0 }
476 };
477
478 return rd_exec_cmd(rd, cmds, "parameter");
479}
480
a6d0773e
MZ
481static int stat_get_arg(struct rd *rd, const char *arg)
482{
483 int value = 0;
484 char *endp;
485
486 if (strcmpx(rd_argv(rd), arg) != 0)
487 return -EINVAL;
488
489 rd_arg_inc(rd);
490 value = strtol(rd_argv(rd), &endp, 10);
491 rd_arg_inc(rd);
492
493 return value;
494}
495
496static int stat_one_qp_bind(struct rd *rd)
497{
498 int lqpn = 0, cntn = 0, ret;
499 uint32_t seq;
500
501 if (rd_no_arg(rd)) {
502 stat_help(rd);
503 return -EINVAL;
504 }
505
506 ret = rd_build_filter(rd, stat_valid_filters);
507 if (ret)
508 return ret;
509
510 lqpn = stat_get_arg(rd, "lqpn");
511
512 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_SET,
513 &seq, (NLM_F_REQUEST | NLM_F_ACK));
514
515 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE,
516 RDMA_COUNTER_MODE_MANUAL);
517
518 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP);
519 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
520 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
521 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_RES_LQPN, lqpn);
522
523 if (rd_argc(rd)) {
524 cntn = stat_get_arg(rd, "cntn");
525 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_COUNTER_ID,
526 cntn);
527 }
528
529 return rd_sendrecv_msg(rd, seq);
530}
531
532static int do_stat_qp_unbind_lqpn(struct rd *rd, uint32_t cntn, uint32_t lqpn)
533{
534 uint32_t seq;
535
536 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_DEL,
537 &seq, (NLM_F_REQUEST | NLM_F_ACK));
538
539 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_MODE,
540 RDMA_COUNTER_MODE_MANUAL);
541 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP);
542 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
543 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
544 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn);
545 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_RES_LQPN, lqpn);
546
547 return rd_sendrecv_msg(rd, seq);
548}
549
550static int stat_get_counter_parse_cb(const struct nlmsghdr *nlh, void *data)
551{
552 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
553 struct nlattr *nla_table, *nla_entry;
554 struct rd *rd = data;
555 uint32_t lqpn, cntn;
556 int err;
557
558 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
559
560 if (!tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID])
561 return MNL_CB_ERROR;
562 cntn = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_STAT_COUNTER_ID]);
563
564 nla_table = tb[RDMA_NLDEV_ATTR_RES_QP];
565 if (!nla_table)
566 return MNL_CB_ERROR;
567
568 mnl_attr_for_each_nested(nla_entry, nla_table) {
569 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
570
571 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
572 if (err != MNL_CB_OK)
573 return -EINVAL;
574
575 if (!nla_line[RDMA_NLDEV_ATTR_RES_LQPN])
576 return -EINVAL;
577
578 lqpn = mnl_attr_get_u32(nla_line[RDMA_NLDEV_ATTR_RES_LQPN]);
579 err = do_stat_qp_unbind_lqpn(rd, cntn, lqpn);
580 if (err)
581 return MNL_CB_ERROR;
582 }
583
584 return MNL_CB_OK;
585}
586
587static int stat_one_qp_unbind(struct rd *rd)
588{
589 int flags = NLM_F_REQUEST | NLM_F_ACK, ret;
590 char buf[MNL_SOCKET_BUFFER_SIZE];
591 int lqpn = 0, cntn = 0;
592 unsigned int portid;
593 uint32_t seq;
594
595 ret = rd_build_filter(rd, stat_valid_filters);
596 if (ret)
597 return ret;
598
599 cntn = stat_get_arg(rd, "cntn");
600 if (rd_argc(rd)) {
601 lqpn = stat_get_arg(rd, "lqpn");
602 return do_stat_qp_unbind_lqpn(rd, cntn, lqpn);
603 }
604
605 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, &seq, flags);
606 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
607 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
608 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_RES, RDMA_NLDEV_ATTR_RES_QP);
609 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_STAT_COUNTER_ID, cntn);
610 ret = rd_send_msg(rd);
611 if (ret)
612 return ret;
613
614
615 /* Can't use rd_recv_msg() since the callback also calls it (recursively),
616 * then rd_recv_msg() always return -1 here
617 */
618 portid = mnl_socket_get_portid(rd->nl);
619 ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf));
620 if (ret <= 0)
621 return ret;
622
623 ret = mnl_cb_run(buf, ret, seq, portid, stat_get_counter_parse_cb, rd);
624 mnl_socket_close(rd->nl);
625 if (ret != MNL_CB_OK)
626 return ret;
627
628 return 0;
629}
630
631static int stat_qp_bind_link(struct rd *rd)
632{
633 return rd_exec_link(rd, stat_one_qp_bind, true);
634}
635
636static int stat_qp_bind(struct rd *rd)
637{
638 const struct rd_cmd cmds[] = {
639 { NULL, stat_help },
640 { "link", stat_qp_bind_link },
641 { "help", stat_help },
642 { 0 },
643 };
644
645 return rd_exec_cmd(rd, cmds, "parameter");
646}
647
648static int stat_qp_unbind_link(struct rd *rd)
649{
650 return rd_exec_link(rd, stat_one_qp_unbind, true);
651}
652
653static int stat_qp_unbind(struct rd *rd)
654{
655 const struct rd_cmd cmds[] = {
656 { NULL, stat_help },
657 { "link", stat_qp_unbind_link },
658 { "help", stat_help },
659 { 0 },
660 };
661
662 return rd_exec_cmd(rd, cmds, "parameter");
663}
664
5937552b
MZ
665static int stat_qp(struct rd *rd)
666{
667 const struct rd_cmd cmds[] = {
668 { NULL, stat_qp_show },
669 { "show", stat_qp_show },
670 { "list", stat_qp_show },
1b2ca7ad 671 { "mode", stat_qp_get_mode },
887fc739 672 { "set", stat_qp_set },
a6d0773e
MZ
673 { "bind", stat_qp_bind },
674 { "unbind", stat_qp_unbind },
5937552b
MZ
675 { "help", stat_help },
676 { 0 }
677 };
678
679 return rd_exec_cmd(rd, cmds, "parameter");
680}
681
a7137e51
MZ
682static int stat_show_parse_cb(const struct nlmsghdr *nlh, void *data)
683{
684 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
685 struct rd *rd = data;
686 const char *name;
687 uint32_t port;
688 int ret;
689
690 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
691 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
692 !tb[RDMA_NLDEV_ATTR_PORT_INDEX] ||
693 !tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS])
694 return MNL_CB_ERROR;
695
696 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
697 port = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
698 if (rd->json_output) {
699 jsonw_string_field(rd->jw, "ifname", name);
700 jsonw_uint_field(rd->jw, "port", port);
701 } else {
702 pr_out("link %s/%u ", name, port);
703 }
704
705 ret = res_get_hwcounters(rd, tb[RDMA_NLDEV_ATTR_STAT_HWCOUNTERS], true);
706
707 if (!rd->json_output)
708 pr_out("\n");
709 return ret;
710}
711
712static int stat_show_one_link(struct rd *rd)
713{
714 int flags = NLM_F_REQUEST | NLM_F_ACK;
715 uint32_t seq;
716 int ret;
717
718 if (!rd->port_idx)
719 return 0;
720
721 rd_prepare_msg(rd, RDMA_NLDEV_CMD_STAT_GET, &seq, flags);
722 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
723 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
724 ret = rd_send_msg(rd);
725 if (ret)
726 return ret;
727
728 return rd_recv_msg(rd, stat_show_parse_cb, rd, seq);
729}
730
731static int stat_show_link(struct rd *rd)
732{
733 return rd_exec_link(rd, stat_show_one_link, false);
734}
735
736static int stat_show(struct rd *rd)
737{
738 const struct rd_cmd cmds[] = {
739 { NULL, stat_show_link },
740 { "link", stat_show_link },
33552ade 741 { "mr", stat_mr },
a7137e51
MZ
742 { "help", stat_help },
743 { 0 }
744 };
745
746 return rd_exec_cmd(rd, cmds, "parameter");
747}
748
5937552b
MZ
749int cmd_stat(struct rd *rd)
750{
751 const struct rd_cmd cmds[] = {
a7137e51
MZ
752 { NULL, stat_show },
753 { "show", stat_show },
754 { "list", stat_show },
5937552b
MZ
755 { "help", stat_help },
756 { "qp", stat_qp },
33552ade 757 { "mr", stat_mr },
5937552b
MZ
758 { 0 }
759 };
760
761 return rd_exec_cmd(rd, cmds, "statistic command");
762}