]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/res.c
rdma: Move resource QP logic to separate file
[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 "res.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 pr_out(" resource show [qp|cm_id|pd|mr|cq]\n");
20 pr_out(" resource show qp link [DEV/PORT]\n");
21 pr_out(" resource show qp link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
22 pr_out(" resource show cm_id link [DEV/PORT]\n");
23 pr_out(" resource show cm_id link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
24 pr_out(" resource show cq link [DEV/PORT]\n");
25 pr_out(" resource show cq link [DEV/PORT] [FILTER-NAME FILTER-VALUE]\n");
26 pr_out(" resource show pd dev [DEV]\n");
27 pr_out(" resource show pd dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
28 pr_out(" resource show mr dev [DEV]\n");
29 pr_out(" resource show mr dev [DEV] [FILTER-NAME FILTER-VALUE]\n");
30 return 0;
31 }
32
33 static int res_print_summary(struct rd *rd, struct nlattr **tb)
34 {
35 struct nlattr *nla_table = tb[RDMA_NLDEV_ATTR_RES_SUMMARY];
36 struct nlattr *nla_entry;
37 const char *name;
38 uint64_t curr;
39 int err;
40
41 mnl_attr_for_each_nested(nla_entry, nla_table) {
42 struct nlattr *nla_line[RDMA_NLDEV_ATTR_MAX] = {};
43 char json_name[32];
44
45 err = mnl_attr_parse_nested(nla_entry, rd_attr_cb, nla_line);
46 if (err != MNL_CB_OK)
47 return -EINVAL;
48
49 if (!nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] ||
50 !nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]) {
51 return -EINVAL;
52 }
53
54 name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]);
55 curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]);
56 if (rd->json_output) {
57 snprintf(json_name, 32, "%s", name);
58 jsonw_lluint_field(rd->jw, json_name, curr);
59 } else {
60 pr_out("%s %"PRId64 " ", name, curr);
61 }
62 }
63 return 0;
64 }
65
66 static int res_no_args_parse_cb(const struct nlmsghdr *nlh, void *data)
67 {
68 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
69 struct rd *rd = data;
70 const char *name;
71 uint32_t idx;
72
73 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
74 if (!tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
75 !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
76 !tb[RDMA_NLDEV_ATTR_RES_SUMMARY])
77 return MNL_CB_ERROR;
78
79 idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
80 name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
81 if (rd->json_output) {
82 jsonw_uint_field(rd->jw, "ifindex", idx);
83 jsonw_string_field(rd->jw, "ifname", name);
84 } else {
85 pr_out("%u: %s: ", idx, name);
86 }
87
88 res_print_summary(rd, tb);
89
90 if (!rd->json_output)
91 pr_out("\n");
92 return MNL_CB_OK;
93 }
94
95 int _res_send_msg(struct rd *rd, uint32_t command, mnl_cb_t callback)
96 {
97 uint32_t flags = NLM_F_REQUEST | NLM_F_ACK;
98 uint32_t seq;
99 int ret;
100
101 if (command != RDMA_NLDEV_CMD_RES_GET)
102 flags |= NLM_F_DUMP;
103
104 rd_prepare_msg(rd, command, &seq, flags);
105 mnl_attr_put_u32(rd->nlh, RDMA_NLDEV_ATTR_DEV_INDEX, rd->dev_idx);
106 if (rd->port_idx)
107 mnl_attr_put_u32(rd->nlh,
108 RDMA_NLDEV_ATTR_PORT_INDEX, rd->port_idx);
109
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
122 const char *qp_types_to_str(uint8_t idx)
123 {
124 static const char * const qp_types_str[] = { "SMI", "GSI", "RC",
125 "UC", "UD", "RAW_IPV6",
126 "RAW_ETHERTYPE",
127 "UNKNOWN", "RAW_PACKET",
128 "XRC_INI", "XRC_TGT" };
129
130 if (idx < ARRAY_SIZE(qp_types_str))
131 return qp_types_str[idx];
132 return "UNKNOWN";
133 }
134
135 void print_lqpn(struct rd *rd, uint32_t val)
136 {
137 if (rd->json_output)
138 jsonw_uint_field(rd->jw, "lqpn", val);
139 else
140 pr_out("lqpn %u ", val);
141 }
142
143 void print_pid(struct rd *rd, uint32_t val)
144 {
145 if (rd->json_output)
146 jsonw_uint_field(rd->jw, "pid", val);
147 else
148 pr_out("pid %u ", val);
149 }
150
151 void print_comm(struct rd *rd, const char *str, struct nlattr **nla_line)
152 {
153 char tmp[18];
154
155 if (rd->json_output) {
156 /* Don't beatify output in JSON format */
157 jsonw_string_field(rd->jw, "comm", str);
158 return;
159 }
160
161 if (nla_line[RDMA_NLDEV_ATTR_RES_PID])
162 snprintf(tmp, sizeof(tmp), "%s", str);
163 else
164 snprintf(tmp, sizeof(tmp), "[%s]", str);
165
166 pr_out("comm %s ", tmp);
167 }
168
169 void print_dev(struct rd *rd, uint32_t idx, const char *name)
170 {
171 if (rd->json_output) {
172 jsonw_uint_field(rd->jw, "ifindex", idx);
173 jsonw_string_field(rd->jw, "ifname", name);
174 } else {
175 pr_out("dev %s ", name);
176 }
177 }
178
179 void print_link(struct rd *rd, uint32_t idx, const char *name, uint32_t port,
180 struct nlattr **nla_line)
181 {
182 if (rd->json_output) {
183 jsonw_uint_field(rd->jw, "ifindex", idx);
184
185 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
186 jsonw_uint_field(rd->jw, "port", port);
187
188 jsonw_string_field(rd->jw, "ifname", name);
189 } else {
190 if (nla_line[RDMA_NLDEV_ATTR_PORT_INDEX])
191 pr_out("link %s/%u ", name, port);
192 else
193 pr_out("link %s/- ", name);
194 }
195 }
196
197 char *get_task_name(uint32_t pid)
198 {
199 char *comm;
200 FILE *f;
201
202 if (asprintf(&comm, "/proc/%d/comm", pid) < 0)
203 return NULL;
204
205 f = fopen(comm, "r");
206 free(comm);
207 if (!f)
208 return NULL;
209
210 if (fscanf(f, "%ms\n", &comm) != 1)
211 comm = NULL;
212
213 fclose(f);
214
215 return comm;
216 }
217
218 void print_key(struct rd *rd, const char *name, uint64_t val)
219 {
220 if (rd->json_output)
221 jsonw_xint_field(rd->jw, name, val);
222 else
223 pr_out("%s 0x%" PRIx64 " ", name, val);
224 }
225
226 void res_print_uint(struct rd *rd, const char *name, uint64_t val)
227 {
228 if (rd->json_output)
229 jsonw_uint_field(rd->jw, name, val);
230 else
231 pr_out("%s %" PRIu64 " ", name, val);
232 }
233
234 void print_users(struct rd *rd, uint64_t val)
235 {
236 if (rd->json_output)
237 jsonw_uint_field(rd->jw, "users", val);
238 else
239 pr_out("users %" PRIu64 " ", val);
240 }
241
242 RES_FUNC(res_no_args, RDMA_NLDEV_CMD_RES_GET, NULL, true);
243
244 static int res_show(struct rd *rd)
245 {
246 const struct rd_cmd cmds[] = {
247 { NULL, res_no_args },
248 { "qp", res_qp },
249 { "cm_id", res_cm_id },
250 { "cq", res_cq },
251 { "mr", res_mr },
252 { "pd", res_pd },
253 { 0 }
254 };
255
256 /*
257 * Special case to support "rdma res show DEV_NAME"
258 */
259 if (rd_argc(rd) == 1 && dev_map_lookup(rd, false))
260 return rd_exec_dev(rd, _res_no_args);
261
262 return rd_exec_cmd(rd, cmds, "parameter");
263 }
264
265 int cmd_res(struct rd *rd)
266 {
267 const struct rd_cmd cmds[] = {
268 { NULL, res_show },
269 { "show", res_show },
270 { "list", res_show },
271 { "help", res_help },
272 { 0 }
273 };
274
275 return rd_exec_cmd(rd, cmds, "resource command");
276 }