Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2017 6WIND S.A.
3 : : * Copyright 2017 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <fcntl.h>
7 : : #include <stdio.h>
8 : : #include <stdlib.h>
9 : : #include <string.h>
10 : : #include <unistd.h>
11 : : #include <errno.h>
12 : :
13 : : #include <rte_debug.h>
14 : : #include <rte_devargs.h>
15 : : #include <rte_malloc.h>
16 : : #include <rte_kvargs.h>
17 : : #include <rte_string_fns.h>
18 : :
19 : : #include "failsafe_private.h"
20 : :
21 : : /* Callback used when a new device is found in devargs */
22 : : typedef int (parse_cb)(struct rte_eth_dev *dev, const char *params,
23 : : uint8_t head);
24 : :
25 : : uint64_t failsafe_hotplug_poll = FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS;
26 : : int failsafe_mac_from_arg;
27 : :
28 : : static const char * const pmd_failsafe_init_parameters[] = {
29 : : PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
30 : : PMD_FAILSAFE_MAC_KVARG,
31 : : NULL,
32 : : };
33 : :
34 : : /*
35 : : * input: text.
36 : : * output: 0: if text[0] != '(',
37 : : * 0: if there are no corresponding ')'
38 : : * n: distance to corresponding ')' otherwise
39 : : */
40 : : static size_t
41 : : closing_paren(const char *text)
42 : : {
43 : : int nb_open = 0;
44 : : size_t i = 0;
45 : :
46 [ # # # # : 0 : while (text[i] != '\0') {
# # ]
47 [ # # # # : 0 : if (text[i] == '(')
# # ]
48 : 0 : nb_open++;
49 [ # # # # : 0 : if (text[i] == ')')
# # ]
50 : 0 : nb_open--;
51 [ # # # # : 0 : if (nb_open == 0)
# # ]
52 : : return i;
53 : 0 : i++;
54 : : }
55 : : return 0;
56 : : }
57 : :
58 : : static int
59 : 0 : fs_parse_device(struct sub_device *sdev, char *args)
60 : : {
61 : : struct rte_devargs *d;
62 : : int ret;
63 : :
64 : 0 : d = &sdev->devargs;
65 : 0 : DEBUG("%s", args);
66 : 0 : ret = rte_devargs_parse(d, args);
67 [ # # ]: 0 : if (ret) {
68 : 0 : DEBUG("devargs parsing failed with code %d", ret);
69 : 0 : return ret;
70 : : }
71 : 0 : sdev->bus = d->bus;
72 : 0 : sdev->state = DEV_PARSED;
73 : 0 : return 0;
74 : : }
75 : :
76 : : static void
77 : : fs_sanitize_cmdline(char *args)
78 : : {
79 : : char *nl;
80 : :
81 : 0 : nl = strrchr(args, '\n');
82 [ # # # # ]: 0 : if (nl)
83 : 0 : nl[0] = '\0';
84 : : }
85 : :
86 : : static int
87 : 0 : fs_execute_cmd(struct sub_device *sdev, char *cmdline)
88 : : {
89 : : FILE *fp;
90 : : /* store possible newline as well */
91 : : char output[DEVARGS_MAXLEN + 1];
92 : : size_t len;
93 : : int ret;
94 : :
95 : : RTE_ASSERT(cmdline != NULL || sdev->cmdline != NULL);
96 [ # # ]: 0 : if (sdev->cmdline == NULL) {
97 : : size_t i;
98 : :
99 : 0 : len = strlen(cmdline) + 1;
100 : 0 : sdev->cmdline = calloc(1, len);
101 [ # # ]: 0 : if (sdev->cmdline == NULL) {
102 : 0 : ERROR("Command line allocation failed");
103 : 0 : return -ENOMEM;
104 : : }
105 : : strlcpy(sdev->cmdline, cmdline, len);
106 : : /* Replace all commas in the command line by spaces */
107 [ # # ]: 0 : for (i = 0; i < len; i++)
108 [ # # ]: 0 : if (sdev->cmdline[i] == ',')
109 : 0 : sdev->cmdline[i] = ' ';
110 : : }
111 : 0 : DEBUG("'%s'", sdev->cmdline);
112 : 0 : fp = popen(sdev->cmdline, "r");
113 [ # # ]: 0 : if (fp == NULL) {
114 : 0 : ret = -errno;
115 : 0 : ERROR("popen: %s", strerror(errno));
116 : 0 : return ret;
117 : : }
118 : : /* We only read one line */
119 [ # # ]: 0 : if (fgets(output, sizeof(output) - 1, fp) == NULL) {
120 : 0 : DEBUG("Could not read command output");
121 : : ret = -ENODEV;
122 : 0 : goto ret_pclose;
123 : : }
124 : : fs_sanitize_cmdline(output);
125 [ # # ]: 0 : if (output[0] == '\0') {
126 : : ret = -ENODEV;
127 : 0 : goto ret_pclose;
128 : : }
129 : 0 : ret = fs_parse_device(sdev, output);
130 [ # # ]: 0 : if (ret)
131 : 0 : ERROR("Parsing device '%s' failed", output);
132 : 0 : ret_pclose:
133 [ # # ]: 0 : if (pclose(fp) == -1)
134 : 0 : ERROR("pclose: %s", strerror(errno));
135 : : return ret;
136 : : }
137 : :
138 : : static int
139 : 0 : fs_read_fd(struct sub_device *sdev, char *fd_str)
140 : : {
141 : : FILE *fp = NULL;
142 : : int fd = -1;
143 : : /* store possible newline as well */
144 : : char output[DEVARGS_MAXLEN + 1];
145 : : int err = -ENODEV;
146 : : int oflags;
147 : : int lcount;
148 : :
149 : : RTE_ASSERT(fd_str != NULL || sdev->fd_str != NULL);
150 [ # # ]: 0 : if (sdev->fd_str == NULL) {
151 : 0 : sdev->fd_str = strdup(fd_str);
152 [ # # ]: 0 : if (sdev->fd_str == NULL) {
153 : 0 : ERROR("Command line allocation failed");
154 : 0 : return -ENOMEM;
155 : : }
156 : : }
157 : 0 : errno = 0;
158 : 0 : fd = strtol(fd_str, &fd_str, 0);
159 [ # # # # : 0 : if (errno || *fd_str || fd < 0) {
# # ]
160 : 0 : ERROR("Parsing FD number failed");
161 : 0 : goto error;
162 : : }
163 : : /* Fiddle with copy of file descriptor */
164 : 0 : fd = dup(fd);
165 [ # # ]: 0 : if (fd == -1)
166 : 0 : goto error;
167 : 0 : oflags = fcntl(fd, F_GETFL);
168 [ # # ]: 0 : if (oflags == -1)
169 : 0 : goto error;
170 [ # # ]: 0 : if (fcntl(fd, F_SETFL, oflags | O_NONBLOCK) == -1)
171 : 0 : goto error;
172 : 0 : fp = fdopen(fd, "r");
173 [ # # ]: 0 : if (fp == NULL)
174 : 0 : goto error;
175 : : fd = -1;
176 : : /* Only take the last line into account */
177 : : lcount = 0;
178 [ # # ]: 0 : while (fgets(output, sizeof(output), fp))
179 : 0 : ++lcount;
180 [ # # ]: 0 : if (lcount == 0)
181 : 0 : goto error;
182 [ # # # # ]: 0 : else if (ferror(fp) && errno != EAGAIN)
183 : 0 : goto error;
184 : : /* Line must end with a newline character */
185 : : fs_sanitize_cmdline(output);
186 [ # # ]: 0 : if (output[0] == '\0')
187 : 0 : goto error;
188 : 0 : err = fs_parse_device(sdev, output);
189 [ # # ]: 0 : if (err)
190 : 0 : ERROR("Parsing device '%s' failed", output);
191 : 0 : error:
192 [ # # ]: 0 : if (fp)
193 : 0 : fclose(fp);
194 [ # # ]: 0 : if (fd != -1)
195 : 0 : close(fd);
196 : : return err;
197 : : }
198 : :
199 : : static int
200 : 0 : fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
201 : : uint8_t head)
202 : : {
203 : : struct fs_priv *priv;
204 : : struct sub_device *sdev;
205 : : char *args = NULL;
206 : : size_t a, b;
207 : : int ret;
208 : :
209 : 0 : priv = PRIV(dev);
210 : : a = 0;
211 : : b = 0;
212 : : ret = 0;
213 [ # # ]: 0 : while (param[b] != '(' &&
214 : : param[b] != '\0')
215 : 0 : b++;
216 : : a = b;
217 : 0 : b += closing_paren(¶m[b]);
218 [ # # ]: 0 : if (a == b) {
219 : 0 : ERROR("Dangling parenthesis");
220 : 0 : return -EINVAL;
221 : : }
222 : 0 : a += 1;
223 : 0 : args = strndup(¶m[a], b - a);
224 [ # # ]: 0 : if (args == NULL) {
225 : 0 : ERROR("Not enough memory for parameter parsing");
226 : 0 : return -ENOMEM;
227 : : }
228 : 0 : sdev = &priv->subs[head];
229 [ # # ]: 0 : if (strncmp(param, "dev", 3) == 0) {
230 : 0 : ret = fs_parse_device(sdev, args);
231 [ # # ]: 0 : if (ret)
232 : 0 : goto free_args;
233 [ # # ]: 0 : } else if (strncmp(param, "exec", 4) == 0) {
234 : 0 : ret = fs_execute_cmd(sdev, args);
235 [ # # ]: 0 : if (ret == -ENODEV) {
236 : 0 : DEBUG("Reading device info from command line failed");
237 : : ret = 0;
238 : : }
239 [ # # ]: 0 : if (ret)
240 : 0 : goto free_args;
241 [ # # ]: 0 : } else if (strncmp(param, "fd(", 3) == 0) {
242 : 0 : ret = fs_read_fd(sdev, args);
243 [ # # ]: 0 : if (ret == -ENODEV) {
244 : 0 : DEBUG("Reading device info from FD failed");
245 : : ret = 0;
246 : : }
247 [ # # ]: 0 : if (ret)
248 : 0 : goto free_args;
249 : : } else {
250 : 0 : ERROR("Unrecognized device type: %.*s", (int)b, param);
251 : : ret = -EINVAL;
252 : : }
253 : 0 : free_args:
254 : 0 : free(args);
255 : 0 : return ret;
256 : : }
257 : :
258 : : static int
259 : 0 : fs_parse_sub_devices(parse_cb *cb,
260 : : struct rte_eth_dev *dev, const char *params)
261 : : {
262 : : size_t a, b;
263 : : uint8_t head;
264 : : int ret;
265 : :
266 : : a = 0;
267 : : head = 0;
268 : : ret = 0;
269 [ # # ]: 0 : while (params[a] != '\0') {
270 : : b = a;
271 : 0 : while (params[b] != '(' &&
272 [ # # # # ]: 0 : params[b] != ',' &&
273 : : params[b] != '\0')
274 : 0 : b++;
275 [ # # ]: 0 : if (b == a) {
276 : 0 : ERROR("Invalid parameter");
277 : 0 : return -EINVAL;
278 : : }
279 [ # # ]: 0 : if (params[b] == ',') {
280 : 0 : a = b + 1;
281 : 0 : continue;
282 : : }
283 [ # # ]: 0 : if (params[b] == '(') {
284 : : size_t start = b;
285 : :
286 : 0 : b += closing_paren(¶ms[b]);
287 [ # # ]: 0 : if (b == start) {
288 : 0 : ERROR("Dangling parenthesis");
289 : 0 : return -EINVAL;
290 : : }
291 : 0 : ret = (*cb)(dev, ¶ms[a], head);
292 [ # # ]: 0 : if (ret)
293 : 0 : return ret;
294 : 0 : head += 1;
295 : 0 : b += 1;
296 [ # # ]: 0 : if (params[b] == '\0')
297 : : return 0;
298 : : }
299 : 0 : a = b + 1;
300 : : }
301 : : return 0;
302 : : }
303 : :
304 : : static int
305 : 0 : fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
306 : : {
307 : 0 : char buffer[DEVARGS_MAXLEN] = {0};
308 : : size_t a, b;
309 : : int i;
310 : :
311 : : a = 0;
312 : : i = 0;
313 [ # # ]: 0 : while (params[a] != '\0') {
314 : : b = a;
315 : 0 : while (params[b] != '(' &&
316 [ # # # # ]: 0 : params[b] != ',' &&
317 : : params[b] != '\0')
318 : 0 : b++;
319 [ # # ]: 0 : if (b == a) {
320 : 0 : ERROR("Invalid parameter");
321 : 0 : return -EINVAL;
322 : : }
323 [ # # ]: 0 : if (params[b] == ',' || params[b] == '\0') {
324 : 0 : size_t len = b - a;
325 : :
326 [ # # ]: 0 : if (i > 0)
327 : 0 : len += 1;
328 [ # # ]: 0 : snprintf(&buffer[i], len + 1, "%s%s",
329 : : i ? "," : "", ¶ms[a]);
330 : 0 : i += len;
331 : : } else if (params[b] == '(') {
332 : : size_t start = b;
333 : :
334 : 0 : b += closing_paren(¶ms[b]);
335 [ # # ]: 0 : if (b == start)
336 : : return -EINVAL;
337 : 0 : b += 1;
338 [ # # ]: 0 : if (params[b] == '\0')
339 : 0 : goto out;
340 : : }
341 : 0 : a = b + 1;
342 : : }
343 : 0 : out:
344 : : strlcpy(params, buffer, DEVARGS_MAXLEN);
345 : 0 : return 0;
346 : : }
347 : :
348 : : static int
349 : 0 : fs_get_u64_arg(const char *key __rte_unused,
350 : : const char *value, void *out)
351 : : {
352 : : uint64_t *u64 = out;
353 : 0 : char *endptr = NULL;
354 : :
355 [ # # ]: 0 : if ((value == NULL) || (out == NULL))
356 : : return -EINVAL;
357 : 0 : errno = 0;
358 : 0 : *u64 = strtoull(value, &endptr, 0);
359 [ # # ]: 0 : if (errno != 0)
360 : 0 : return -errno;
361 [ # # ]: 0 : if (endptr == value)
362 : 0 : return -1;
363 : : return 0;
364 : : }
365 : :
366 : : static int
367 : 0 : fs_get_mac_addr_arg(const char *key __rte_unused,
368 : : const char *value, void *out)
369 : : {
370 : : struct rte_ether_addr *ea = out;
371 : :
372 [ # # ]: 0 : if ((value == NULL) || (out == NULL))
373 : : return -EINVAL;
374 : :
375 : 0 : return rte_ether_unformat_addr(value, ea);
376 : : }
377 : :
378 : : int
379 : 0 : failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
380 : : {
381 : : struct fs_priv *priv;
382 : 0 : char mut_params[DEVARGS_MAXLEN] = "";
383 : : struct rte_kvargs *kvlist = NULL;
384 : : unsigned int arg_count;
385 : : size_t n;
386 : : int ret;
387 : :
388 : 0 : priv = PRIV(dev);
389 : : ret = 0;
390 [ # # ]: 0 : priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
391 : : /* default parameters */
392 : : n = strlcpy(mut_params, params, sizeof(mut_params));
393 [ # # ]: 0 : if (n >= sizeof(mut_params)) {
394 : 0 : ERROR("Parameter string too long (>=%zu)",
395 : : sizeof(mut_params));
396 : 0 : return -ENOMEM;
397 : : }
398 : 0 : ret = fs_parse_sub_devices(fs_parse_device_param,
399 : : dev, params);
400 [ # # ]: 0 : if (ret < 0)
401 : : return ret;
402 : 0 : ret = fs_remove_sub_devices_definition(mut_params);
403 [ # # ]: 0 : if (ret < 0)
404 : : return ret;
405 [ # # ]: 0 : if (strnlen(mut_params, sizeof(mut_params)) > 0) {
406 : 0 : kvlist = rte_kvargs_parse(mut_params,
407 : : pmd_failsafe_init_parameters);
408 [ # # ]: 0 : if (kvlist == NULL) {
409 : 0 : ERROR("Error parsing parameters, usage:"
410 : : PMD_FAILSAFE_PARAM_STRING);
411 : 0 : return -1;
412 : : }
413 : : /* PLUG_IN event poll timer */
414 : 0 : arg_count = rte_kvargs_count(kvlist,
415 : : PMD_FAILSAFE_HOTPLUG_POLL_KVARG);
416 [ # # ]: 0 : if (arg_count == 1) {
417 : 0 : ret = rte_kvargs_process(kvlist,
418 : : PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
419 : : &fs_get_u64_arg, &failsafe_hotplug_poll);
420 [ # # ]: 0 : if (ret < 0)
421 : 0 : goto free_kvlist;
422 : : }
423 : : /* MAC addr */
424 : 0 : arg_count = rte_kvargs_count(kvlist,
425 : : PMD_FAILSAFE_MAC_KVARG);
426 [ # # ]: 0 : if (arg_count > 0) {
427 : 0 : ret = rte_kvargs_process(kvlist,
428 : : PMD_FAILSAFE_MAC_KVARG,
429 : : &fs_get_mac_addr_arg,
430 : 0 : &dev->data->mac_addrs[0]);
431 [ # # ]: 0 : if (ret < 0)
432 : 0 : goto free_kvlist;
433 : :
434 : 0 : failsafe_mac_from_arg = 1;
435 : : }
436 : : }
437 : 0 : PRIV(dev)->state = DEV_PARSED;
438 : 0 : free_kvlist:
439 : 0 : rte_kvargs_free(kvlist);
440 : 0 : return ret;
441 : : }
442 : :
443 : : void
444 [ # # ]: 0 : failsafe_args_free(struct rte_eth_dev *dev)
445 : : {
446 : : struct sub_device *sdev;
447 : : uint8_t i;
448 : :
449 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, dev) {
450 : 0 : free(sdev->cmdline);
451 : 0 : sdev->cmdline = NULL;
452 : 0 : free(sdev->fd_str);
453 : 0 : sdev->fd_str = NULL;
454 : 0 : rte_devargs_reset(&sdev->devargs);
455 : : }
456 : 0 : }
457 : :
458 : : static int
459 : 0 : fs_count_device(struct rte_eth_dev *dev, const char *param,
460 : : uint8_t head __rte_unused)
461 : : {
462 : : size_t b = 0;
463 : :
464 [ # # ]: 0 : while (param[b] != '(' &&
465 : : param[b] != '\0')
466 : 0 : b++;
467 [ # # ]: 0 : if (strncmp(param, "dev", b) != 0 &&
468 [ # # ]: 0 : strncmp(param, "exec", b) != 0 &&
469 [ # # ]: 0 : strncmp(param, "fd(", b) != 0) {
470 : 0 : ERROR("Unrecognized device type: %.*s", (int)b, param);
471 : 0 : return -EINVAL;
472 : : }
473 : 0 : PRIV(dev)->subs_tail += 1;
474 : 0 : return 0;
475 : : }
476 : :
477 : : int
478 : 0 : failsafe_args_count_subdevice(struct rte_eth_dev *dev,
479 : : const char *params)
480 : : {
481 : 0 : return fs_parse_sub_devices(fs_count_device,
482 : : dev, params);
483 : : }
484 : :
485 : : static int
486 : 0 : fs_parse_sub_device(struct sub_device *sdev)
487 : : {
488 : : struct rte_devargs *da;
489 : 0 : char devstr[DEVARGS_MAXLEN] = "";
490 : :
491 : : da = &sdev->devargs;
492 : 0 : snprintf(devstr, sizeof(devstr), "%s,%s", da->name, da->args);
493 : 0 : return fs_parse_device(sdev, devstr);
494 : : }
495 : :
496 : : int
497 [ # # ]: 0 : failsafe_args_parse_subs(struct rte_eth_dev *dev)
498 : : {
499 : : struct sub_device *sdev;
500 : : uint8_t i;
501 : : int ret = 0;
502 : :
503 [ # # # # ]: 0 : FOREACH_SUBDEV(sdev, i, dev) {
504 [ # # ]: 0 : if (sdev->state >= DEV_PARSED)
505 : 0 : continue;
506 [ # # ]: 0 : if (sdev->cmdline)
507 : 0 : ret = fs_execute_cmd(sdev, sdev->cmdline);
508 [ # # ]: 0 : else if (sdev->fd_str)
509 : 0 : ret = fs_read_fd(sdev, sdev->fd_str);
510 : : else
511 : 0 : ret = fs_parse_sub_device(sdev);
512 [ # # ]: 0 : if (ret == 0)
513 : 0 : sdev->state = DEV_PARSED;
514 : : }
515 : 0 : return 0;
516 : : }
|