]> git.proxmox.com Git - mirror_iproute2.git/blob - rdma/utils.c
rdma: Set pointer to device name position
[mirror_iproute2.git] / rdma / utils.c
1 /*
2 * utils.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 <ctype.h>
14
15 int rd_argc(struct rd *rd)
16 {
17 return rd->argc;
18 }
19
20 char *rd_argv(struct rd *rd)
21 {
22 if (!rd_argc(rd))
23 return NULL;
24 return *rd->argv;
25 }
26
27 static int strcmpx(const char *str1, const char *str2)
28 {
29 if (strlen(str1) > strlen(str2))
30 return -1;
31 return strncmp(str1, str2, strlen(str1));
32 }
33
34 static bool rd_argv_match(struct rd *rd, const char *pattern)
35 {
36 if (!rd_argc(rd))
37 return false;
38 return strcmpx(rd_argv(rd), pattern) == 0;
39 }
40
41 void rd_arg_inc(struct rd *rd)
42 {
43 if (!rd_argc(rd))
44 return;
45 rd->argc--;
46 rd->argv++;
47 }
48
49 bool rd_no_arg(struct rd *rd)
50 {
51 return rd_argc(rd) == 0;
52 }
53
54 /*
55 * Possible input:output
56 * dev/port | first port | is_dump_all
57 * mlx5_1 | 0 | true
58 * mlx5_1/ | 0 | true
59 * mlx5_1/0 | 0 | false
60 * mlx5_1/1 | 1 | false
61 * mlx5_1/- | 0 | false
62 *
63 * In strict mode, /- will return error.
64 */
65 static int get_port_from_argv(struct rd *rd, uint32_t *port,
66 bool *is_dump_all, bool strict_port)
67 {
68 char *slash;
69
70 *port = 0;
71 *is_dump_all = true;
72
73 slash = strchr(rd_argv(rd), '/');
74 /* if no port found, return 0 */
75 if (slash++) {
76 if (*slash == '-') {
77 if (strict_port)
78 return -EINVAL;
79 *is_dump_all = false;
80 return 0;
81 }
82
83 if (isdigit(*slash)) {
84 *is_dump_all = false;
85 *port = atoi(slash);
86 }
87 if (!*port && strlen(slash))
88 return -EINVAL;
89 }
90 return 0;
91 }
92
93 static struct dev_map *dev_map_alloc(const char *dev_name)
94 {
95 struct dev_map *dev_map;
96
97 dev_map = calloc(1, sizeof(*dev_map));
98 if (!dev_map)
99 return NULL;
100 dev_map->dev_name = strdup(dev_name);
101
102 return dev_map;
103 }
104
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);
112 free(dev_map->dev_name);
113 free(dev_map);
114 }
115 }
116
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;
124 int ret;
125
126 fe = calloc(1, sizeof(*fe));
127 if (!fe)
128 return -ENOMEM;
129
130 while (idx < MAX_NUMBER_OF_FILTERS && valid_filters[idx].name) {
131 if (!strcmpx(key, valid_filters[idx].name)) {
132 key_found = true;
133 break;
134 }
135 idx++;
136 }
137 if (!key_found) {
138 pr_err("Unsupported filter option: %s\n", key);
139 ret = -EINVAL;
140 goto err;
141 }
142
143 /*
144 * Check the filter validity, not optimal, but works
145 *
146 * Actually, there are three types of filters
147 * numeric - for example PID or QPN
148 * string - for example states
149 * link - user requested to filter on specific link
150 * e.g. mlx5_1/1, mlx5_1/-, mlx5_1 ...
151 */
152 if (valid_filters[idx].is_number &&
153 strspn(value, cset) != strlen(value)) {
154 pr_err("%s filter accepts \"%s\" characters only\n", key, cset);
155 ret = -EINVAL;
156 goto err;
157 }
158
159 fe->key = strdup(key);
160 fe->value = strdup(value);
161 if (!fe->key || !fe->value) {
162 ret = -ENOMEM;
163 goto err_alloc;
164 }
165
166 for (idx = 0; idx < strlen(fe->value); idx++)
167 fe->value[idx] = tolower(fe->value[idx]);
168
169 list_add_tail(&fe->list, &rd->filter_list);
170 return 0;
171
172 err_alloc:
173 free(fe->value);
174 free(fe->key);
175 err:
176 free(fe);
177 return ret;
178 }
179
180 int rd_build_filter(struct rd *rd, const struct filters valid_filters[])
181 {
182 int ret = 0;
183 int idx = 0;
184
185 if (!valid_filters || !rd_argc(rd))
186 goto out;
187
188 if (rd_argc(rd) == 1) {
189 pr_err("No filter data was supplied to filter option %s\n", rd_argv(rd));
190 ret = -EINVAL;
191 goto out;
192 }
193
194 if (rd_argc(rd) % 2) {
195 pr_err("There is filter option without data\n");
196 ret = -EINVAL;
197 goto out;
198 }
199
200 while (idx != rd_argc(rd)) {
201 /*
202 * We can do micro-optimization and skip "dev"
203 * and "link" filters, but it is not worth of it.
204 */
205 ret = add_filter(rd, *(rd->argv + idx),
206 *(rd->argv + idx + 1), valid_filters);
207 if (ret)
208 goto out;
209 idx += 2;
210 }
211
212 out:
213 return ret;
214 }
215
216 bool rd_check_is_key_exist(struct rd *rd, const char *key)
217 {
218 struct filter_entry *fe;
219
220 list_for_each_entry(fe, &rd->filter_list, list) {
221 if (!strcmpx(fe->key, key))
222 return true;
223 }
224
225 return false;
226 }
227
228 /*
229 * Check if string entry is filtered:
230 * * key doesn't exist -> user didn't request -> not filtered
231 */
232 bool rd_check_is_string_filtered(struct rd *rd,
233 const char *key, const char *val)
234 {
235 bool key_is_filtered = false;
236 struct filter_entry *fe;
237 char *p = NULL;
238 char *str;
239
240 list_for_each_entry(fe, &rd->filter_list, list) {
241 if (!strcmpx(fe->key, key)) {
242 /* We found the key */
243 p = strdup(fe->value);
244 key_is_filtered = true;
245 if (!p) {
246 /*
247 * Something extremely wrong if we fail
248 * to allocate small amount of bytes.
249 */
250 pr_err("Found key, but failed to allocate memory to store value\n");
251 return key_is_filtered;
252 }
253
254 /*
255 * Need to check if value in range
256 * It can come in the following formats
257 * and their permutations:
258 * str
259 * str1,str2
260 */
261 str = strtok(p, ",");
262 while (str) {
263 if (strlen(str) == strlen(val) &&
264 !strcasecmp(str, val)) {
265 key_is_filtered = false;
266 goto out;
267 }
268 str = strtok(NULL, ",");
269 }
270 goto out;
271 }
272 }
273
274 out:
275 free(p);
276 return key_is_filtered;
277 }
278
279 /*
280 * Check if key is filtered:
281 * key doesn't exist -> user didn't request -> not filtered
282 */
283 bool rd_check_is_filtered(struct rd *rd, const char *key, uint32_t val)
284 {
285 bool key_is_filtered = false;
286 struct filter_entry *fe;
287
288 list_for_each_entry(fe, &rd->filter_list, list) {
289 uint32_t left_val = 0, fe_value = 0;
290 bool range_check = false;
291 char *p = fe->value;
292
293 if (!strcmpx(fe->key, key)) {
294 /* We found the key */
295 key_is_filtered = true;
296 /*
297 * Need to check if value in range
298 * It can come in the following formats
299 * (and their permutations):
300 * numb
301 * numb1,numb2
302 * ,numb1,numb2
303 * numb1-numb2
304 * numb1,numb2-numb3,numb4-numb5
305 */
306 while (*p) {
307 if (isdigit(*p)) {
308 fe_value = strtol(p, &p, 10);
309 if (fe_value == val ||
310 (range_check && left_val < val &&
311 val < fe_value)) {
312 key_is_filtered = false;
313 goto out;
314 }
315 range_check = false;
316 } else {
317 if (*p == '-') {
318 left_val = fe_value;
319 range_check = true;
320 }
321 p++;
322 }
323 }
324 goto out;
325 }
326 }
327
328 out:
329 return key_is_filtered;
330 }
331
332 static void filters_cleanup(struct rd *rd)
333 {
334 struct filter_entry *fe, *tmp;
335
336 list_for_each_entry_safe(fe, tmp,
337 &rd->filter_list, list) {
338 list_del(&fe->list);
339 free(fe->key);
340 free(fe->value);
341 free(fe);
342 }
343 }
344
345 static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
346 [RDMA_NLDEV_ATTR_DEV_INDEX] = MNL_TYPE_U32,
347 [RDMA_NLDEV_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING,
348 [RDMA_NLDEV_ATTR_PORT_INDEX] = MNL_TYPE_U32,
349 [RDMA_NLDEV_ATTR_CAP_FLAGS] = MNL_TYPE_U64,
350 [RDMA_NLDEV_ATTR_FW_VERSION] = MNL_TYPE_NUL_STRING,
351 [RDMA_NLDEV_ATTR_NODE_GUID] = MNL_TYPE_U64,
352 [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID] = MNL_TYPE_U64,
353 [RDMA_NLDEV_ATTR_LID] = MNL_TYPE_U32,
354 [RDMA_NLDEV_ATTR_SM_LID] = MNL_TYPE_U32,
355 [RDMA_NLDEV_ATTR_LMC] = MNL_TYPE_U8,
356 [RDMA_NLDEV_ATTR_PORT_STATE] = MNL_TYPE_U8,
357 [RDMA_NLDEV_ATTR_PORT_PHYS_STATE] = MNL_TYPE_U8,
358 [RDMA_NLDEV_ATTR_DEV_NODE_TYPE] = MNL_TYPE_U8,
359 };
360
361 int rd_attr_cb(const struct nlattr *attr, void *data)
362 {
363 const struct nlattr **tb = data;
364 int type;
365
366 if (mnl_attr_type_valid(attr, RDMA_NLDEV_ATTR_MAX) < 0)
367 return MNL_CB_ERROR;
368
369 type = mnl_attr_get_type(attr);
370
371 if (mnl_attr_validate(attr, nldev_policy[type]) < 0)
372 return MNL_CB_ERROR;
373
374 tb[type] = attr;
375 return MNL_CB_OK;
376 }
377
378 int rd_dev_init_cb(const struct nlmsghdr *nlh, void *data)
379 {
380 struct nlattr *tb[RDMA_NLDEV_ATTR_MAX] = {};
381 struct dev_map *dev_map;
382 struct rd *rd = data;
383 const char *dev_name;
384
385 mnl_attr_parse(nlh, 0, rd_attr_cb, tb);
386 if (!tb[RDMA_NLDEV_ATTR_DEV_NAME] || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
387 return MNL_CB_ERROR;
388 if (!tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
389 pr_err("This tool doesn't support switches yet\n");
390 return MNL_CB_ERROR;
391 }
392
393 dev_name = mnl_attr_get_str(tb[RDMA_NLDEV_ATTR_DEV_NAME]);
394
395 dev_map = dev_map_alloc(dev_name);
396 if (!dev_map)
397 /* The main function will cleanup the allocations */
398 return MNL_CB_ERROR;
399 list_add_tail(&dev_map->list, &rd->dev_map_list);
400
401 dev_map->num_ports = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
402 dev_map->idx = mnl_attr_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
403 return MNL_CB_OK;
404 }
405
406 void rd_free(struct rd *rd)
407 {
408 if (!rd)
409 return;
410 free(rd->buff);
411 dev_map_cleanup(rd);
412 filters_cleanup(rd);
413 }
414
415 int rd_set_arg_to_devname(struct rd *rd)
416 {
417 int ret = 0;
418
419 while (!rd_no_arg(rd)) {
420 if (rd_argv_match(rd, "dev") || rd_argv_match(rd, "link")) {
421 rd_arg_inc(rd);
422 if (rd_no_arg(rd)) {
423 pr_err("No device name was supplied\n");
424 ret = -EINVAL;
425 }
426 goto out;
427 }
428 rd_arg_inc(rd);
429 }
430 out:
431 return ret;
432 }
433
434 int rd_exec_link(struct rd *rd, int (*cb)(struct rd *rd), bool strict_port)
435 {
436 struct dev_map *dev_map;
437 uint32_t port;
438 int ret = 0;
439
440 if (rd->json_output)
441 jsonw_start_array(rd->jw);
442 if (rd_no_arg(rd)) {
443 list_for_each_entry(dev_map, &rd->dev_map_list, list) {
444 rd->dev_idx = dev_map->idx;
445 port = (strict_port) ? 1 : 0;
446 for (; port < dev_map->num_ports + 1; port++) {
447 rd->port_idx = port;
448 ret = cb(rd);
449 if (ret)
450 goto out;
451 }
452 }
453
454 } else {
455 bool is_dump_all;
456
457 dev_map = dev_map_lookup(rd, true);
458 ret = get_port_from_argv(rd, &port, &is_dump_all, strict_port);
459 if (!dev_map || port > dev_map->num_ports || (!port && ret)) {
460 pr_err("Wrong device name\n");
461 ret = -ENOENT;
462 goto out;
463 }
464 rd_arg_inc(rd);
465 rd->dev_idx = dev_map->idx;
466 rd->port_idx = port;
467 for (; rd->port_idx < dev_map->num_ports + 1; rd->port_idx++) {
468 ret = cb(rd);
469 if (ret)
470 goto out;
471 if (!is_dump_all)
472 /*
473 * We got request to show link for devname
474 * with port index.
475 */
476 break;
477 }
478 }
479
480 out:
481 if (rd->json_output)
482 jsonw_end_array(rd->jw);
483 return ret;
484 }
485
486 int rd_exec_dev(struct rd *rd, int (*cb)(struct rd *rd))
487 {
488 struct dev_map *dev_map;
489 int ret = 0;
490
491 if (rd->json_output)
492 jsonw_start_array(rd->jw);
493 if (rd_no_arg(rd)) {
494 list_for_each_entry(dev_map, &rd->dev_map_list, list) {
495 rd->dev_idx = dev_map->idx;
496 ret = cb(rd);
497 if (ret)
498 goto out;
499 }
500 } else {
501 dev_map = dev_map_lookup(rd, false);
502 if (!dev_map) {
503 pr_err("Wrong device name - %s\n", rd_argv(rd));
504 ret = -ENOENT;
505 goto out;
506 }
507 rd_arg_inc(rd);
508 rd->dev_idx = dev_map->idx;
509 ret = cb(rd);
510 }
511 out:
512 if (rd->json_output)
513 jsonw_end_array(rd->jw);
514 return ret;
515 }
516
517 int rd_exec_cmd(struct rd *rd, const struct rd_cmd *cmds, const char *str)
518 {
519 const struct rd_cmd *c;
520
521 /* First argument in objs table is default variant */
522 if (rd_no_arg(rd))
523 return cmds->func(rd);
524
525 for (c = cmds + 1; c->cmd; ++c) {
526 if (rd_argv_match(rd, c->cmd)) {
527 /* Move to next argument */
528 rd_arg_inc(rd);
529 return c->func(rd);
530 }
531 }
532
533 pr_err("Unknown %s '%s'.\n", str, rd_argv(rd));
534 return 0;
535 }
536
537 void rd_prepare_msg(struct rd *rd, uint32_t cmd, uint32_t *seq, uint16_t flags)
538 {
539 *seq = time(NULL);
540
541 rd->nlh = mnl_nlmsg_put_header(rd->buff);
542 rd->nlh->nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, cmd);
543 rd->nlh->nlmsg_seq = *seq;
544 rd->nlh->nlmsg_flags = flags;
545 }
546
547 int rd_send_msg(struct rd *rd)
548 {
549 int ret;
550
551 rd->nl = mnl_socket_open(NETLINK_RDMA);
552 if (!rd->nl) {
553 pr_err("Failed to open NETLINK_RDMA socket\n");
554 return -ENODEV;
555 }
556
557 ret = mnl_socket_bind(rd->nl, 0, MNL_SOCKET_AUTOPID);
558 if (ret < 0) {
559 pr_err("Failed to bind socket with err %d\n", ret);
560 goto err;
561 }
562
563 ret = mnl_socket_sendto(rd->nl, rd->nlh, rd->nlh->nlmsg_len);
564 if (ret < 0) {
565 pr_err("Failed to send to socket with err %d\n", ret);
566 goto err;
567 }
568 return 0;
569
570 err:
571 mnl_socket_close(rd->nl);
572 return ret;
573 }
574
575 int rd_recv_msg(struct rd *rd, mnl_cb_t callback, void *data, unsigned int seq)
576 {
577 int ret;
578 unsigned int portid;
579 char buf[MNL_SOCKET_BUFFER_SIZE];
580
581 portid = mnl_socket_get_portid(rd->nl);
582 do {
583 ret = mnl_socket_recvfrom(rd->nl, buf, sizeof(buf));
584 if (ret <= 0)
585 break;
586
587 ret = mnl_cb_run(buf, ret, seq, portid, callback, data);
588 } while (ret > 0);
589
590 mnl_socket_close(rd->nl);
591 return ret;
592 }
593
594 static struct dev_map *_dev_map_lookup(struct rd *rd, const char *dev_name)
595 {
596 struct dev_map *dev_map;
597
598 list_for_each_entry(dev_map, &rd->dev_map_list, list)
599 if (strcmp(dev_name, dev_map->dev_name) == 0)
600 return dev_map;
601
602 return NULL;
603 }
604
605 struct dev_map *dev_map_lookup(struct rd *rd, bool allow_port_index)
606 {
607 struct dev_map *dev_map;
608 char *dev_name;
609 char *slash;
610
611 if (rd_no_arg(rd))
612 return NULL;
613
614 dev_name = strdup(rd_argv(rd));
615 if (allow_port_index) {
616 slash = strrchr(dev_name, '/');
617 if (slash)
618 *slash = '\0';
619 }
620
621 dev_map = _dev_map_lookup(rd, dev_name);
622 free(dev_name);
623 return dev_map;
624 }