]>
Commit | Line | Data |
---|---|---|
835d8321 | 1 | // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
74bd75c2 LR |
2 | /* |
3 | * utils.c RDMA tool | |
74bd75c2 LR |
4 | * Authors: Leon Romanovsky <leonro@mellanox.com> |
5 | */ | |
6 | ||
7 | #include "rdma.h" | |
6416d1a0 | 8 | #include <ctype.h> |
33115275 | 9 | #include <inttypes.h> |
74bd75c2 | 10 | |
5f4892e2 | 11 | int rd_argc(struct rd *rd) |
74bd75c2 LR |
12 | { |
13 | return rd->argc; | |
14 | } | |
15 | ||
6ae54b13 | 16 | char *rd_argv(struct rd *rd) |
74bd75c2 LR |
17 | { |
18 | if (!rd_argc(rd)) | |
19 | return NULL; | |
20 | return *rd->argv; | |
21 | } | |
22 | ||
6ae54b13 | 23 | int strcmpx(const char *str1, const char *str2) |
74bd75c2 LR |
24 | { |
25 | if (strlen(str1) > strlen(str2)) | |
26 | return -1; | |
27 | return strncmp(str1, str2, strlen(str1)); | |
28 | } | |
29 | ||
30 | static bool rd_argv_match(struct rd *rd, const char *pattern) | |
31 | { | |
32 | if (!rd_argc(rd)) | |
33 | return false; | |
34 | return strcmpx(rd_argv(rd), pattern) == 0; | |
35 | } | |
36 | ||
6ae54b13 | 37 | void rd_arg_inc(struct rd *rd) |
74bd75c2 LR |
38 | { |
39 | if (!rd_argc(rd)) | |
40 | return; | |
41 | rd->argc--; | |
42 | rd->argv++; | |
43 | } | |
44 | ||
6ae54b13 | 45 | bool rd_no_arg(struct rd *rd) |
74bd75c2 LR |
46 | { |
47 | return rd_argc(rd) == 0; | |
48 | } | |
49 | ||
6416d1a0 LR |
50 | /* |
51 | * Possible input:output | |
52 | * dev/port | first port | is_dump_all | |
53 | * mlx5_1 | 0 | true | |
54 | * mlx5_1/ | 0 | true | |
55 | * mlx5_1/0 | 0 | false | |
56 | * mlx5_1/1 | 1 | false | |
57 | * mlx5_1/- | 0 | false | |
58 | * | |
59 | * In strict mode, /- will return error. | |
60 | */ | |
61 | static int get_port_from_argv(struct rd *rd, uint32_t *port, | |
62 | bool *is_dump_all, bool strict_port) | |
40df8263 LR |
63 | { |
64 | char *slash; | |
65 | ||
6416d1a0 LR |
66 | *port = 0; |
67 | *is_dump_all = true; | |
68 | ||
40df8263 LR |
69 | slash = strchr(rd_argv(rd), '/'); |
70 | /* if no port found, return 0 */ | |
6416d1a0 LR |
71 | if (slash++) { |
72 | if (*slash == '-') { | |
73 | if (strict_port) | |
74 | return -EINVAL; | |
75 | *is_dump_all = false; | |
76 | return 0; | |
77 | } | |
78 | ||
79 | if (isdigit(*slash)) { | |
80 | *is_dump_all = false; | |
81 | *port = atoi(slash); | |
82 | } | |
83 | if (!*port && strlen(slash)) | |
84 | return -EINVAL; | |
85 | } | |
86 | return 0; | |
40df8263 LR |
87 | } |
88 | ||
74bd75c2 LR |
89 | static struct dev_map *dev_map_alloc(const char *dev_name) |
90 | { | |
91 | struct dev_map *dev_map; | |
92 | ||
93 | dev_map = calloc(1, sizeof(*dev_map)); | |
94 | if (!dev_map) | |
95 | return NULL; | |
96 | dev_map->dev_name = strdup(dev_name); | |
5f826553 LR |
97 | if (!dev_map->dev_name) { |
98 | free(dev_map); | |
99 | return NULL; | |
100 | } | |
74bd75c2 LR |
101 | |
102 | return dev_map; | |
103 | } | |
104 | ||
74bd75c2 LR |
105 | static void dev_map_cleanup(struct rd *rd) |
106 | { | |
107 | struct dev_map *dev_map, *tmp; | |
108 | ||
109 | list_for_each_entry_safe(dev_map, tmp, | |
110 | &rd->dev_map_list, list) { | |
111 | list_del(&dev_map->list); | |
b1e6bc43 LR |
112 | free(dev_map->dev_name); |
113 | free(dev_map); | |
74bd75c2 LR |
114 | } |
115 | } | |
116 | ||
1174be72 LR |
117 | static int add_filter(struct rd *rd, char *key, char *value, |
118 | const struct filters valid_filters[]) | |
119 | { | |
120 | char cset[] = "1234567890,-"; | |
121 | struct filter_entry *fe; | |
122 | bool key_found = false; | |
123 | int idx = 0; | |
5a823593 | 124 | char *endp; |
1174be72 LR |
125 | int ret; |
126 | ||
127 | fe = calloc(1, sizeof(*fe)); | |
128 | if (!fe) | |
129 | return -ENOMEM; | |
130 | ||
131 | while (idx < MAX_NUMBER_OF_FILTERS && valid_filters[idx].name) { | |
132 | if (!strcmpx(key, valid_filters[idx].name)) { | |
133 | key_found = true; | |
134 | break; | |
135 | } | |
136 | idx++; | |
137 | } | |
138 | if (!key_found) { | |
139 | pr_err("Unsupported filter option: %s\n", key); | |
140 | ret = -EINVAL; | |
141 | goto err; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Check the filter validity, not optimal, but works | |
146 | * | |
147 | * Actually, there are three types of filters | |
148 | * numeric - for example PID or QPN | |
149 | * string - for example states | |
150 | * link - user requested to filter on specific link | |
151 | * e.g. mlx5_1/1, mlx5_1/-, mlx5_1 ... | |
152 | */ | |
153 | if (valid_filters[idx].is_number && | |
154 | strspn(value, cset) != strlen(value)) { | |
155 | pr_err("%s filter accepts \"%s\" characters only\n", key, cset); | |
156 | ret = -EINVAL; | |
157 | goto err; | |
158 | } | |
159 | ||
160 | fe->key = strdup(key); | |
161 | fe->value = strdup(value); | |
162 | if (!fe->key || !fe->value) { | |
163 | ret = -ENOMEM; | |
164 | goto err_alloc; | |
165 | } | |
166 | ||
5a823593 LR |
167 | errno = 0; |
168 | strtol(fe->value, &endp, 10); | |
169 | if (valid_filters[idx].is_doit && !errno && *endp == '\0') | |
170 | fe->is_doit = true; | |
171 | ||
1174be72 LR |
172 | for (idx = 0; idx < strlen(fe->value); idx++) |
173 | fe->value[idx] = tolower(fe->value[idx]); | |
174 | ||
175 | list_add_tail(&fe->list, &rd->filter_list); | |
176 | return 0; | |
177 | ||
178 | err_alloc: | |
179 | free(fe->value); | |
180 | free(fe->key); | |
181 | err: | |
182 | free(fe); | |
183 | return ret; | |
184 | } | |
185 | ||
5a823593 LR |
186 | bool rd_doit_index(struct rd *rd, uint32_t *idx) |
187 | { | |
188 | struct filter_entry *fe; | |
189 | ||
190 | list_for_each_entry(fe, &rd->filter_list, list) { | |
191 | if (fe->is_doit) { | |
192 | *idx = atoi(fe->value); | |
193 | return true; | |
194 | } | |
195 | } | |
196 | ||
197 | return false; | |
198 | } | |
199 | ||
1174be72 LR |
200 | int rd_build_filter(struct rd *rd, const struct filters valid_filters[]) |
201 | { | |
202 | int ret = 0; | |
203 | int idx = 0; | |
204 | ||
205 | if (!valid_filters || !rd_argc(rd)) | |
206 | goto out; | |
207 | ||
208 | if (rd_argc(rd) == 1) { | |
209 | pr_err("No filter data was supplied to filter option %s\n", rd_argv(rd)); | |
210 | ret = -EINVAL; | |
211 | goto out; | |
212 | } | |
213 | ||
214 | if (rd_argc(rd) % 2) { | |
215 | pr_err("There is filter option without data\n"); | |
216 | ret = -EINVAL; | |
217 | goto out; | |
218 | } | |
219 | ||
220 | while (idx != rd_argc(rd)) { | |
221 | /* | |
222 | * We can do micro-optimization and skip "dev" | |
223 | * and "link" filters, but it is not worth of it. | |
224 | */ | |
225 | ret = add_filter(rd, *(rd->argv + idx), | |
226 | *(rd->argv + idx + 1), valid_filters); | |
227 | if (ret) | |
228 | goto out; | |
229 | idx += 2; | |
230 | } | |
231 | ||
232 | out: | |
233 | return ret; | |
234 | } | |
235 | ||
78728b7e | 236 | static bool rd_check_is_key_exist(struct rd *rd, const char *key) |
1174be72 LR |
237 | { |
238 | struct filter_entry *fe; | |
239 | ||
240 | list_for_each_entry(fe, &rd->filter_list, list) { | |
241 | if (!strcmpx(fe->key, key)) | |
242 | return true; | |
243 | } | |
244 | ||
245 | return false; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Check if string entry is filtered: | |
250 | * * key doesn't exist -> user didn't request -> not filtered | |
251 | */ | |
78728b7e LR |
252 | static bool rd_check_is_string_filtered(struct rd *rd, const char *key, |
253 | const char *val) | |
1174be72 LR |
254 | { |
255 | bool key_is_filtered = false; | |
256 | struct filter_entry *fe; | |
257 | char *p = NULL; | |
258 | char *str; | |
259 | ||
260 | list_for_each_entry(fe, &rd->filter_list, list) { | |
261 | if (!strcmpx(fe->key, key)) { | |
262 | /* We found the key */ | |
263 | p = strdup(fe->value); | |
264 | key_is_filtered = true; | |
265 | if (!p) { | |
266 | /* | |
267 | * Something extremely wrong if we fail | |
268 | * to allocate small amount of bytes. | |
269 | */ | |
270 | pr_err("Found key, but failed to allocate memory to store value\n"); | |
271 | return key_is_filtered; | |
272 | } | |
273 | ||
274 | /* | |
275 | * Need to check if value in range | |
276 | * It can come in the following formats | |
277 | * and their permutations: | |
278 | * str | |
279 | * str1,str2 | |
280 | */ | |
281 | str = strtok(p, ","); | |
282 | while (str) { | |
283 | if (strlen(str) == strlen(val) && | |
284 | !strcasecmp(str, val)) { | |
285 | key_is_filtered = false; | |
286 | goto out; | |
287 | } | |
288 | str = strtok(NULL, ","); | |
289 | } | |
290 | goto out; | |
291 | } | |
292 | } | |
293 | ||
294 | out: | |
295 | free(p); | |
296 | return key_is_filtered; | |
297 | } | |
298 | ||
299 | /* | |
300 | * Check if key is filtered: | |
301 | * key doesn't exist -> user didn't request -> not filtered | |
302 | */ | |
78728b7e | 303 | static bool rd_check_is_filtered(struct rd *rd, const char *key, uint32_t val) |
1174be72 LR |
304 | { |
305 | bool key_is_filtered = false; | |
306 | struct filter_entry *fe; | |
307 | ||
308 | list_for_each_entry(fe, &rd->filter_list, list) { | |
309 | uint32_t left_val = 0, fe_value = 0; | |
310 | bool range_check = false; | |
311 | char *p = fe->value; | |
312 | ||
313 | if (!strcmpx(fe->key, key)) { | |
314 | /* We found the key */ | |
315 | key_is_filtered = true; | |
316 | /* | |
317 | * Need to check if value in range | |
318 | * It can come in the following formats | |
319 | * (and their permutations): | |
320 | * numb | |
321 | * numb1,numb2 | |
322 | * ,numb1,numb2 | |
323 | * numb1-numb2 | |
324 | * numb1,numb2-numb3,numb4-numb5 | |
325 | */ | |
326 | while (*p) { | |
327 | if (isdigit(*p)) { | |
328 | fe_value = strtol(p, &p, 10); | |
329 | if (fe_value == val || | |
330 | (range_check && left_val < val && | |
331 | val < fe_value)) { | |
332 | key_is_filtered = false; | |
333 | goto out; | |
334 | } | |
335 | range_check = false; | |
336 | } else { | |
337 | if (*p == '-') { | |
338 | left_val = fe_value; | |
339 | range_check = true; | |
340 | } | |
341 | p++; | |
342 | } | |
343 | } | |
344 | goto out; | |
345 | } | |
346 | } | |
347 | ||
348 | out: | |
349 | return key_is_filtered; | |
350 | } | |
351 | ||
78728b7e LR |
352 | bool rd_is_filtered_attr(struct rd *rd, const char *key, uint32_t val, |
353 | struct nlattr *attr) | |
354 | { | |
355 | if (!attr) | |
356 | return rd_check_is_key_exist(rd, key); | |
357 | ||
358 | return rd_check_is_filtered(rd, key, val); | |
359 | } | |
360 | ||
361 | bool rd_is_string_filtered_attr(struct rd *rd, const char *key, const char *val, | |
362 | struct nlattr *attr) | |
363 | { | |
364 | if (!attr) | |
365 | rd_check_is_key_exist(rd, key); | |
366 | ||
367 | return rd_check_is_string_filtered(rd, key, val); | |
368 | } | |
369 | ||
1174be72 LR |
370 | static void filters_cleanup(struct rd *rd) |
371 | { | |
372 | struct filter_entry *fe, *tmp; | |
373 | ||
374 | list_for_each_entry_safe(fe, tmp, | |
375 | &rd->filter_list, list) { | |
376 | list_del(&fe->list); | |
377 | free(fe->key); | |
378 | free(fe->value); | |
379 | free(fe); | |
380 | } | |
381 | } | |
382 | ||
74bd75c2 | 383 | static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = { |
40df8263 | 384 | [RDMA_NLDEV_ATTR_DEV_INDEX] = MNL_TYPE_U32, |
74bd75c2 LR |
385 | [RDMA_NLDEV_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING, |
386 | [RDMA_NLDEV_ATTR_PORT_INDEX] = MNL_TYPE_U32, | |
40df8263 LR |
387 | [RDMA_NLDEV_ATTR_CAP_FLAGS] = MNL_TYPE_U64, |
388 | [RDMA_NLDEV_ATTR_FW_VERSION] = MNL_TYPE_NUL_STRING, | |
389 | [RDMA_NLDEV_ATTR_NODE_GUID] = MNL_TYPE_U64, | |
390 | [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = MNL_TYPE_U64, | |
da990ab4 LR |
391 | [RDMA_NLDEV_ATTR_LID] = MNL_TYPE_U32, |
392 | [RDMA_NLDEV_ATTR_SM_LID] = MNL_TYPE_U32, | |
393 | [RDMA_NLDEV_ATTR_LMC] = MNL_TYPE_U8, | |
394 | [RDMA_NLDEV_ATTR_PORT_STATE] = MNL_TYPE_U8, | |
395 | [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = MNL_TYPE_U8, | |
40df8263 | 396 | [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = MNL_TYPE_U8, |
923aa825 LR |
397 | [RDMA_NLDEV_ATTR_RES_SUMMARY] = MNL_TYPE_NESTED, |
398 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY] = MNL_TYPE_NESTED, | |
399 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME] = MNL_TYPE_NUL_STRING, | |
400 | [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR] = MNL_TYPE_U64, | |
8ecac46a LR |
401 | [RDMA_NLDEV_ATTR_RES_QP] = MNL_TYPE_NESTED, |
402 | [RDMA_NLDEV_ATTR_RES_QP_ENTRY] = MNL_TYPE_NESTED, | |
403 | [RDMA_NLDEV_ATTR_RES_LQPN] = MNL_TYPE_U32, | |
404 | [RDMA_NLDEV_ATTR_RES_RQPN] = MNL_TYPE_U32, | |
405 | [RDMA_NLDEV_ATTR_RES_RQ_PSN] = MNL_TYPE_U32, | |
406 | [RDMA_NLDEV_ATTR_RES_SQ_PSN] = MNL_TYPE_U32, | |
407 | [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE] = MNL_TYPE_U8, | |
408 | [RDMA_NLDEV_ATTR_RES_TYPE] = MNL_TYPE_U8, | |
409 | [RDMA_NLDEV_ATTR_RES_STATE] = MNL_TYPE_U8, | |
410 | [RDMA_NLDEV_ATTR_RES_PID] = MNL_TYPE_U32, | |
411 | [RDMA_NLDEV_ATTR_RES_KERN_NAME] = MNL_TYPE_NUL_STRING, | |
9a362cc7 SW |
412 | [RDMA_NLDEV_ATTR_RES_CM_ID] = MNL_TYPE_NESTED, |
413 | [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY] = MNL_TYPE_NESTED, | |
414 | [RDMA_NLDEV_ATTR_RES_PS] = MNL_TYPE_U32, | |
415 | [RDMA_NLDEV_ATTR_RES_SRC_ADDR] = MNL_TYPE_UNSPEC, | |
416 | [RDMA_NLDEV_ATTR_RES_DST_ADDR] = MNL_TYPE_UNSPEC, | |
b0b8e32c SW |
417 | [RDMA_NLDEV_ATTR_RES_CQ] = MNL_TYPE_NESTED, |
418 | [RDMA_NLDEV_ATTR_RES_CQ_ENTRY] = MNL_TYPE_NESTED, | |
419 | [RDMA_NLDEV_ATTR_RES_CQE] = MNL_TYPE_U32, | |
420 | [RDMA_NLDEV_ATTR_RES_USECNT] = MNL_TYPE_U64, | |
421 | [RDMA_NLDEV_ATTR_RES_POLL_CTX] = MNL_TYPE_U8, | |
8958a15c SW |
422 | [RDMA_NLDEV_ATTR_RES_MR] = MNL_TYPE_NESTED, |
423 | [RDMA_NLDEV_ATTR_RES_MR_ENTRY] = MNL_TYPE_NESTED, | |
424 | [RDMA_NLDEV_ATTR_RES_RKEY] = MNL_TYPE_U32, | |
425 | [RDMA_NLDEV_ATTR_RES_LKEY] = MNL_TYPE_U32, | |
426 | [RDMA_NLDEV_ATTR_RES_IOVA] = MNL_TYPE_U64, | |
427 | [RDMA_NLDEV_ATTR_RES_MRLEN] = MNL_TYPE_U64, | |
15259427 LR |
428 | [RDMA_NLDEV_ATTR_NDEV_INDEX] = MNL_TYPE_U32, |
429 | [RDMA_NLDEV_ATTR_NDEV_NAME] = MNL_TYPE_NUL_STRING, | |
33115275 SW |
430 | [RDMA_NLDEV_ATTR_DRIVER] = MNL_TYPE_NESTED, |
431 | [RDMA_NLDEV_ATTR_DRIVER_ENTRY] = MNL_TYPE_NESTED, | |
432 | [RDMA_NLDEV_ATTR_DRIVER_STRING] = MNL_TYPE_NUL_STRING, | |
433 | [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE] = MNL_TYPE_U8, | |
434 | [RDMA_NLDEV_ATTR_DRIVER_S32] = MNL_TYPE_U32, | |
435 | [RDMA_NLDEV_ATTR_DRIVER_U32] = MNL_TYPE_U32, | |
436 | [RDMA_NLDEV_ATTR_DRIVER_S64] = MNL_TYPE_U64, | |
437 | [RDMA_NLDEV_ATTR_DRIVER_U64] = MNL_TYPE_U64, | |
c4572a46 | 438 | [RDMA_NLDEV_SYS_ATTR_NETNS_MODE] = MNL_TYPE_U8, |
5937552b MZ |
439 | [RDMA_NLDEV_ATTR_STAT_COUNTER] = MNL_TYPE_NESTED, |
440 | [RDMA_NLDEV_ATTR_STAT_COUNTER_ENTRY] = MNL_TYPE_NESTED, | |
441 | [RDMA_NLDEV_ATTR_STAT_COUNTER_ID] = MNL_TYPE_U32, | |
442 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTERS] = MNL_TYPE_NESTED, | |
443 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY] = MNL_TYPE_NESTED, | |
444 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_NAME] = MNL_TYPE_NUL_STRING, | |
445 | [RDMA_NLDEV_ATTR_STAT_HWCOUNTER_ENTRY_VALUE] = MNL_TYPE_U64, | |
1b2ca7ad MZ |
446 | [RDMA_NLDEV_ATTR_STAT_MODE] = MNL_TYPE_U32, |
447 | [RDMA_NLDEV_ATTR_STAT_RES] = MNL_TYPE_U32, | |
887fc739 | 448 | [RDMA_NLDEV_ATTR_STAT_AUTO_MODE_MASK] = MNL_TYPE_U32, |
74bd75c2 LR |
449 | }; |
450 | ||
6ae54b13 | 451 | int rd_attr_check(const struct nlattr *attr, int *typep) |
33115275 SW |
452 | { |
453 | int type; | |
454 | ||
455 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX) < 0) | |
456 | return MNL_CB_ERROR; | |
457 | ||
458 | type = mnl_attr_get_type(attr); | |
459 | ||
460 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
461 | return MNL_CB_ERROR; | |
462 | ||
463 | *typep = nldev_policy[type]; | |
464 | return MNL_CB_OK; | |
465 | } | |
466 | ||
74bd75c2 LR |
467 | int rd_attr_cb(const struct nlattr *attr, void *data) |
468 | { | |
469 | const struct nlattr **tb = data; | |
470 | int type; | |
471 | ||
f0cabaca LR |
472 | if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX - 1) < 0) |
473 | /* We received unknown attribute */ | |
fda0a61d | 474 | return MNL_CB_OK; |
74bd75c2 LR |
475 | |
476 | type = mnl_attr_get_type(attr); | |
477 | ||
478 | if (mnl_attr_validate(attr, nldev_policy[type]) < 0) | |
479 | return MNL_CB_ERROR; | |
480 | ||
481 | tb[type] = attr; | |
482 | return MNL_CB_OK; | |
483 | } | |
484 | ||
485 | int rd_dev_init_cb(const struct nlmsghdr *nlh, void *data) | |
486 | { | |
487 | struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {}; | |
488 | struct dev_map *dev_map; | |
489 | struct rd *rd = data; | |
490 | const char *dev_name; | |
491 | ||
492 | mnl_attr_parse(nlh, 0, rd_attr_cb, tb); | |
493 | if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX]) | |
494 | return MNL_CB_ERROR; | |
495 | if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) { | |
496 | pr_err("This tool doesn't support switches yet\n"); | |
497 | return MNL_CB_ERROR; | |
498 | } | |
499 | ||
500 | dev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]); | |
501 | ||
502 | dev_map = dev_map_alloc(dev_name); | |
503 | if (!dev_map) | |
504 | /* The main function will cleanup the allocations */ | |
505 | return MNL_CB_ERROR; | |
506 | list_add_tail(&dev_map->list, &rd->dev_map_list); | |
507 | ||
508 | dev_map->num_ports = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]); | |
509 | dev_map->idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]); | |
510 | return MNL_CB_OK; | |
511 | } | |
512 | ||
5fc17280 | 513 | void rd_free(struct rd *rd) |
74bd75c2 LR |
514 | { |
515 | if (!rd) | |
516 | return; | |
5fc17280 | 517 | free(rd->buff); |
74bd75c2 | 518 | dev_map_cleanup(rd); |
1174be72 | 519 | filters_cleanup(rd); |
74bd75c2 LR |
520 | } |
521 | ||
6e0de6e8 LR |
522 | int rd_set_arg_to_devname(struct rd *rd) |
523 | { | |
524 | int ret = 0; | |
525 | ||
526 | while (!rd_no_arg(rd)) { | |
527 | if (rd_argv_match(rd, "dev") || rd_argv_match(rd, "link")) { | |
528 | rd_arg_inc(rd); | |
529 | if (rd_no_arg(rd)) { | |
530 | pr_err("No device name was supplied\n"); | |
531 | ret = -EINVAL; | |
532 | } | |
533 | goto out; | |
534 | } | |
535 | rd_arg_inc(rd); | |
536 | } | |
537 | out: | |
538 | return ret; | |
539 | } | |
540 | ||
6416d1a0 | 541 | int rd_exec_link(struct rd *rd, int (*cb)(struct rd *rd), bool strict_port) |
874c734c LR |
542 | { |
543 | struct dev_map *dev_map; | |
544 | uint32_t port; | |
545 | int ret = 0; | |
546 | ||
547 | if (rd->json_output) | |
548 | jsonw_start_array(rd->jw); | |
549 | if (rd_no_arg(rd)) { | |
550 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
551 | rd->dev_idx = dev_map->idx; | |
6416d1a0 LR |
552 | port = (strict_port) ? 1 : 0; |
553 | for (; port < dev_map->num_ports + 1; port++) { | |
874c734c LR |
554 | rd->port_idx = port; |
555 | ret = cb(rd); | |
556 | if (ret) | |
557 | goto out; | |
558 | } | |
559 | } | |
560 | ||
561 | } else { | |
6416d1a0 LR |
562 | bool is_dump_all; |
563 | ||
874c734c | 564 | dev_map = dev_map_lookup(rd, true); |
6416d1a0 LR |
565 | ret = get_port_from_argv(rd, &port, &is_dump_all, strict_port); |
566 | if (!dev_map || port > dev_map->num_ports || (!port && ret)) { | |
874c734c LR |
567 | pr_err("Wrong device name\n"); |
568 | ret = -ENOENT; | |
569 | goto out; | |
570 | } | |
571 | rd_arg_inc(rd); | |
572 | rd->dev_idx = dev_map->idx; | |
6416d1a0 | 573 | rd->port_idx = port; |
874c734c LR |
574 | for (; rd->port_idx < dev_map->num_ports + 1; rd->port_idx++) { |
575 | ret = cb(rd); | |
576 | if (ret) | |
577 | goto out; | |
6416d1a0 | 578 | if (!is_dump_all) |
874c734c LR |
579 | /* |
580 | * We got request to show link for devname | |
581 | * with port index. | |
582 | */ | |
583 | break; | |
584 | } | |
585 | } | |
586 | ||
587 | out: | |
588 | if (rd->json_output) | |
589 | jsonw_end_array(rd->jw); | |
590 | return ret; | |
591 | } | |
592 | ||
8b92a2c9 LR |
593 | int rd_exec_dev(struct rd *rd, int (*cb)(struct rd *rd)) |
594 | { | |
595 | struct dev_map *dev_map; | |
596 | int ret = 0; | |
597 | ||
598 | if (rd->json_output) | |
599 | jsonw_start_array(rd->jw); | |
600 | if (rd_no_arg(rd)) { | |
601 | list_for_each_entry(dev_map, &rd->dev_map_list, list) { | |
602 | rd->dev_idx = dev_map->idx; | |
603 | ret = cb(rd); | |
604 | if (ret) | |
605 | goto out; | |
606 | } | |
607 | } else { | |
608 | dev_map = dev_map_lookup(rd, false); | |
609 | if (!dev_map) { | |
b5564441 | 610 | pr_err("Wrong device name - %s\n", rd_argv(rd)); |
8b92a2c9 LR |
611 | ret = -ENOENT; |
612 | goto out; | |
613 | } | |
614 | rd_arg_inc(rd); | |
615 | rd->dev_idx = dev_map->idx; | |
616 | ret = cb(rd); | |
617 | } | |
618 | out: | |
619 | if (rd->json_output) | |
620 | jsonw_end_array(rd->jw); | |
621 | return ret; | |
622 | } | |
623 | ||
a14ceed3 LR |
624 | int rd_exec_require_dev(struct rd *rd, int (*cb)(struct rd *rd)) |
625 | { | |
626 | if (rd_no_arg(rd)) { | |
627 | pr_err("Please provide device name.\n"); | |
628 | return -EINVAL; | |
629 | } | |
630 | ||
631 | return rd_exec_dev(rd, cb); | |
632 | } | |
633 | ||
74bd75c2 LR |
634 | int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str) |
635 | { | |
636 | const struct rd_cmd *c; | |
637 | ||
638 | /* First argument in objs table is default variant */ | |
639 | if (rd_no_arg(rd)) | |
640 | return cmds->func(rd); | |
641 | ||
642 | for (c = cmds + 1; c->cmd; ++c) { | |
643 | if (rd_argv_match(rd, c->cmd)) { | |
644 | /* Move to next argument */ | |
645 | rd_arg_inc(rd); | |
646 | return c->func(rd); | |
647 | } | |
648 | } | |
649 | ||
650 | pr_err("Unknown %s '%s'.\n", str, rd_argv(rd)); | |
651 | return 0; | |
652 | } | |
653 | ||
654 | void rd_prepare_msg(struct rd *rd, uint32_t cmd, uint32_t *seq, uint16_t flags) | |
655 | { | |
656 | *seq = time(NULL); | |
657 | ||
658 | rd->nlh = mnl_nlmsg_put_header(rd->buff); | |
659 | rd->nlh->nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, cmd); | |
660 | rd->nlh->nlmsg_seq = *seq; | |
661 | rd->nlh->nlmsg_flags = flags; | |
662 | } | |
663 | ||
664 | int rd_send_msg(struct rd *rd) | |
665 | { | |
666 | int ret; | |
667 | ||
668 | rd->nl = mnl_socket_open(NETLINK_RDMA); | |
669 | if (!rd->nl) { | |
670 | pr_err("Failed to open NETLINK_RDMA socket\n"); | |
671 | return -ENODEV; | |
672 | } | |
673 | ||
674 | ret = mnl_socket_bind(rd->nl, 0, MNL_SOCKET_AUTOPID); | |
675 | if (ret < 0) { | |
676 | pr_err("Failed to bind socket with err %d\n", ret); | |
677 | goto err; | |
678 | } | |
679 | ||
680 | ret = mnl_socket_sendto(rd->nl, rd->nlh, rd->nlh->nlmsg_len); | |
681 | if (ret < 0) { | |
682 | pr_err("Failed to send to socket with err %d\n", ret); | |
683 | goto err; | |
684 | } | |
685 | return 0; | |
686 | ||
687 | err: | |
688 | mnl_socket_close(rd->nl); | |
689 | return ret; | |
690 | } | |
691 | ||
692 | int rd_recv_msg(struct rd *rd, mnl_cb_t callback, void *data, unsigned int seq) | |
693 | { | |
694 | int ret; | |
695 | unsigned int portid; | |
696 | char buf[MNL_SOCKET_BUFFER_SIZE]; | |
697 | ||
698 | portid = mnl_socket_get_portid(rd->nl); | |
699 | do { | |
700 | ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf)); | |
701 | if (ret <= 0) | |
702 | break; | |
703 | ||
704 | ret = mnl_cb_run(buf, ret, seq, portid, callback, data); | |
705 | } while (ret > 0); | |
706 | ||
8f5cfd23 SW |
707 | if (ret < 0 && !rd->suppress_errors) |
708 | perror("error"); | |
709 | ||
74bd75c2 LR |
710 | mnl_socket_close(rd->nl); |
711 | return ret; | |
712 | } | |
40df8263 | 713 | |
8f5cfd23 SW |
714 | static int null_cb(const struct nlmsghdr *nlh, void *data) |
715 | { | |
716 | return MNL_CB_OK; | |
717 | } | |
718 | ||
719 | int rd_sendrecv_msg(struct rd *rd, unsigned int seq) | |
720 | { | |
721 | int ret; | |
722 | ||
723 | ret = rd_send_msg(rd); | |
724 | if (!ret) | |
725 | ret = rd_recv_msg(rd, null_cb, rd, seq); | |
726 | return ret; | |
727 | } | |
728 | ||
0fc8c30b | 729 | static struct dev_map *_dev_map_lookup(struct rd *rd, const char *dev_name) |
40df8263 LR |
730 | { |
731 | struct dev_map *dev_map; | |
732 | ||
733 | list_for_each_entry(dev_map, &rd->dev_map_list, list) | |
734 | if (strcmp(dev_name, dev_map->dev_name) == 0) | |
735 | return dev_map; | |
736 | ||
737 | return NULL; | |
738 | } | |
739 | ||
740 | struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index) | |
741 | { | |
742 | struct dev_map *dev_map; | |
743 | char *dev_name; | |
744 | char *slash; | |
745 | ||
99da9032 LR |
746 | if (rd_no_arg(rd)) |
747 | return NULL; | |
748 | ||
40df8263 LR |
749 | dev_name = strdup(rd_argv(rd)); |
750 | if (allow_port_index) { | |
751 | slash = strrchr(dev_name, '/'); | |
752 | if (slash) | |
753 | *slash = '\0'; | |
754 | } | |
755 | ||
756 | dev_map = _dev_map_lookup(rd, dev_name); | |
757 | free(dev_name); | |
758 | return dev_map; | |
759 | } | |
33115275 SW |
760 | |
761 | #define nla_type(attr) ((attr)->nla_type & NLA_TYPE_MASK) | |
762 | ||
763 | void newline(struct rd *rd) | |
764 | { | |
765 | if (rd->json_output) | |
766 | jsonw_end_array(rd->jw); | |
767 | else | |
768 | pr_out("\n"); | |
769 | } | |
770 | ||
6ae54b13 | 771 | void newline_indent(struct rd *rd) |
33115275 SW |
772 | { |
773 | newline(rd); | |
774 | if (!rd->json_output) | |
775 | pr_out(" "); | |
776 | } | |
777 | ||
778 | static int print_driver_string(struct rd *rd, const char *key_str, | |
779 | const char *val_str) | |
780 | { | |
781 | if (rd->json_output) { | |
782 | jsonw_string_field(rd->jw, key_str, val_str); | |
783 | return 0; | |
784 | } else { | |
785 | return pr_out("%s %s ", key_str, val_str); | |
786 | } | |
787 | } | |
788 | ||
789 | static int print_driver_s32(struct rd *rd, const char *key_str, int32_t val, | |
790 | enum rdma_nldev_print_type print_type) | |
791 | { | |
792 | if (rd->json_output) { | |
793 | jsonw_int_field(rd->jw, key_str, val); | |
794 | return 0; | |
795 | } | |
796 | switch (print_type) { | |
797 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
798 | return pr_out("%s %d ", key_str, val); | |
799 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
800 | return pr_out("%s 0x%x ", key_str, val); | |
801 | default: | |
802 | return -EINVAL; | |
803 | } | |
804 | } | |
805 | ||
806 | static int print_driver_u32(struct rd *rd, const char *key_str, uint32_t val, | |
807 | enum rdma_nldev_print_type print_type) | |
808 | { | |
809 | if (rd->json_output) { | |
810 | jsonw_int_field(rd->jw, key_str, val); | |
811 | return 0; | |
812 | } | |
813 | switch (print_type) { | |
814 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
815 | return pr_out("%s %u ", key_str, val); | |
816 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
817 | return pr_out("%s 0x%x ", key_str, val); | |
818 | default: | |
819 | return -EINVAL; | |
820 | } | |
821 | } | |
822 | ||
823 | static int print_driver_s64(struct rd *rd, const char *key_str, int64_t val, | |
824 | enum rdma_nldev_print_type print_type) | |
825 | { | |
826 | if (rd->json_output) { | |
827 | jsonw_int_field(rd->jw, key_str, val); | |
828 | return 0; | |
829 | } | |
830 | switch (print_type) { | |
831 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
832 | return pr_out("%s %" PRId64 " ", key_str, val); | |
833 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
834 | return pr_out("%s 0x%" PRIx64 " ", key_str, val); | |
835 | default: | |
836 | return -EINVAL; | |
837 | } | |
838 | } | |
839 | ||
840 | static int print_driver_u64(struct rd *rd, const char *key_str, uint64_t val, | |
841 | enum rdma_nldev_print_type print_type) | |
842 | { | |
843 | if (rd->json_output) { | |
844 | jsonw_int_field(rd->jw, key_str, val); | |
845 | return 0; | |
846 | } | |
847 | switch (print_type) { | |
848 | case RDMA_NLDEV_PRINT_TYPE_UNSPEC: | |
849 | return pr_out("%s %" PRIu64 " ", key_str, val); | |
850 | case RDMA_NLDEV_PRINT_TYPE_HEX: | |
851 | return pr_out("%s 0x%" PRIx64 " ", key_str, val); | |
852 | default: | |
853 | return -EINVAL; | |
854 | } | |
855 | } | |
856 | ||
857 | static int print_driver_entry(struct rd *rd, struct nlattr *key_attr, | |
858 | struct nlattr *val_attr, | |
859 | enum rdma_nldev_print_type print_type) | |
860 | { | |
33115275 | 861 | int attr_type = nla_type(val_attr); |
359f69f7 LR |
862 | int ret = -EINVAL; |
863 | char *key_str; | |
864 | ||
865 | if (asprintf(&key_str, "drv_%s", mnl_attr_get_str(key_attr)) == -1) | |
866 | return -ENOMEM; | |
33115275 SW |
867 | |
868 | switch (attr_type) { | |
869 | case RDMA_NLDEV_ATTR_DRIVER_STRING: | |
359f69f7 LR |
870 | ret = print_driver_string(rd, key_str, |
871 | mnl_attr_get_str(val_attr)); | |
872 | break; | |
33115275 | 873 | case RDMA_NLDEV_ATTR_DRIVER_S32: |
359f69f7 LR |
874 | ret = print_driver_s32(rd, key_str, mnl_attr_get_u32(val_attr), |
875 | print_type); | |
876 | break; | |
33115275 | 877 | case RDMA_NLDEV_ATTR_DRIVER_U32: |
359f69f7 LR |
878 | ret = print_driver_u32(rd, key_str, mnl_attr_get_u32(val_attr), |
879 | print_type); | |
880 | break; | |
33115275 | 881 | case RDMA_NLDEV_ATTR_DRIVER_S64: |
359f69f7 LR |
882 | ret = print_driver_s64(rd, key_str, mnl_attr_get_u64(val_attr), |
883 | print_type); | |
884 | break; | |
33115275 | 885 | case RDMA_NLDEV_ATTR_DRIVER_U64: |
359f69f7 LR |
886 | ret = print_driver_u64(rd, key_str, mnl_attr_get_u64(val_attr), |
887 | print_type); | |
888 | break; | |
33115275 | 889 | } |
359f69f7 LR |
890 | free(key_str); |
891 | return ret; | |
33115275 SW |
892 | } |
893 | ||
894 | void print_driver_table(struct rd *rd, struct nlattr *tb) | |
895 | { | |
896 | int print_type = RDMA_NLDEV_PRINT_TYPE_UNSPEC; | |
897 | struct nlattr *tb_entry, *key = NULL, *val; | |
898 | int type, cc = 0; | |
899 | int ret; | |
900 | ||
901 | if (!rd->show_driver_details || !tb) | |
902 | return; | |
903 | ||
904 | if (rd->pretty_output) | |
905 | newline_indent(rd); | |
906 | ||
907 | /* | |
908 | * Driver attrs are tuples of {key, [print-type], value}. | |
909 | * The key must be a string. If print-type is present, it | |
910 | * defines an alternate printf format type vs the native format | |
911 | * for the attribute. And the value can be any available | |
912 | * driver type. | |
913 | */ | |
914 | mnl_attr_for_each_nested(tb_entry, tb) { | |
915 | ||
916 | if (cc > MAX_LINE_LENGTH) { | |
917 | if (rd->pretty_output) | |
918 | newline_indent(rd); | |
919 | cc = 0; | |
920 | } | |
921 | if (rd_attr_check(tb_entry, &type) != MNL_CB_OK) | |
922 | return; | |
923 | if (!key) { | |
924 | if (type != MNL_TYPE_NUL_STRING) | |
925 | return; | |
926 | key = tb_entry; | |
927 | } else if (type == MNL_TYPE_U8) { | |
928 | print_type = mnl_attr_get_u8(tb_entry); | |
929 | } else { | |
930 | val = tb_entry; | |
931 | ret = print_driver_entry(rd, key, val, print_type); | |
932 | if (ret < 0) | |
933 | return; | |
934 | cc += ret; | |
935 | print_type = RDMA_NLDEV_PRINT_TYPE_UNSPEC; | |
936 | key = NULL; | |
937 | } | |
938 | } | |
939 | return; | |
940 | } |