]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res.c
rdma: Add resource tracking summary
[mirror_iproute2.git] / rdma / res.c
1 /*
2 * res.c RDMA tool
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Leon Romanovsky <leonro@mellanox.com>
10 */
11
12 #include "rdma.h"
13 #include <inttypes.h>
14
15 static int res_help(struct rd *rd)
16 {
17 pr_out("Usage: %s resource\n", rd->filename);
18 pr_out(" resource show [DEV]\n");
19 return 0;
20 }
21
22 static int res_print_summary(struct rd *rd, struct nlattr **tb)
23 {
24 struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY];
25 struct nlattr *nla_entry;
26 const char *name;
27 uint64_t curr;
28 int err;
29
30 mnl_attr_for_each_nested(nla_entry, nla_table) {
31 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
32 char json_name[32];
33
34 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
35 if (err != MNL_CB_OK)
36 return -EINVAL;
37
38 if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] ||
39 !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) {
40 return -EINVAL;
41 }
42
43 name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
44 curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
45 if (rd->json_output) {
46 snprintf(json_name, 32, "%s", name);
47 jsonw_lluint_field(rd->jw, json_name, curr);
48 } else {
49 pr_out("%s %"PRId64 " ", name, curr);
50 }
51 }
52 return 0;
53 }
54
55 static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
56 {
57 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
58 struct rd *rd = data;
59 const char *name;
60 uint32_t idx;
61
62 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
63 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
64 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
65 !tb[RDMA_NLDEV_ATTR_RES_SUMMARY])
66 return MNL_CB_ERROR;
67
68 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
69 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
70 if (rd->json_output) {
71 jsonw_uint_field(rd->jw, "ifindex", idx);
72 jsonw_string_field(rd->jw, "ifname", name);
73 } else {
74 pr_out("%u: %s: ", idx, name);
75 }
76
77 res_print_summary(rd, tb);
78
79 if (!rd->json_output)
80 pr_out("\n");
81 return MNL_CB_OK;
82 }
83
84 static int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
85 {
86 uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
87 uint32_t seq;
88 int ret;
89
90 if (command != RDMA_NLDEV_CMD_RES_GET)
91 flags |= NLM_F_DUMP;
92
93 rd_prepare_msg(rd, command, &seq, flags);
94 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
95 if (rd->port_idx)
96 mnl_attr_put_u32(rd->nlh,
97 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
98
99 ret = rd_send_msg(rd);
100 if (ret)
101 return ret;
102
103 if (rd->json_output)
104 jsonw_start_object(rd->jw);
105 ret = rd_recv_msg(rd, callback, rd, seq);
106 if (rd->json_output)
107 jsonw_end_object(rd->jw);
108 return ret;
109 }
110
111 #define RES_FUNC(name, command, valid_filters, strict_port) \
112 static int _##name(struct rd *rd)\
113 { \
114 return _res_send_msg(rd, command, name##_parse_cb); \
115 } \
116 static int name(struct rd *rd) \
117 {\
118 int ret = rd_build_filter(rd, valid_filters); \
119 if (ret) \
120 return ret; \
121 if ((uintptr_t)valid_filters != (uintptr_t)NULL) { \
122 ret = rd_set_arg_to_devname(rd); \
123 if (ret) \
124 return ret;\
125 } \
126 if (strict_port) \
127 return rd_exec_dev(rd, _##name); \
128 else \
129 return rd_exec_link(rd, _##name, strict_port); \
130 }
131
132 RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true);
133
134 static int res_show(struct rd *rd)
135 {
136 const struct rd_cmd cmds[] = {
137 { NULL, res_no_args },
138 { 0 }
139 };
140
141 /*
142 * Special case to support "rdma res show DEV_NAME"
143 */
144 if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
145 return rd_exec_dev(rd, _res_no_args);
146
147 return rd_exec_cmd(rd, cmds, "parameter");
148 }
149
150 int cmd_res(struct rd *rd)
151 {
152 const struct rd_cmd cmds[] = {
153 { NULL, res_show },
154 { "show", res_show },
155 { "list", res_show },
156 { "help", res_help },
157 { 0 }
158 };
159
160 return rd_exec_cmd(rd, cmds, "resource command");
161 }