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