4 * Copyright 2017 6WIND S.A.
5 * Copyright 2017 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_debug.h>
38 #include <rte_devargs.h>
39 #include <rte_malloc.h>
40 #include <rte_kvargs.h>
42 #include "failsafe_private.h"
44 #define DEVARGS_MAXLEN 4096
46 /* Callback used when a new device is found in devargs */
47 typedef int (parse_cb)(struct rte_eth_dev *dev, const char *params,
50 uint64_t hotplug_poll = FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS;
53 const char *pmd_failsafe_init_parameters[] = {
54 PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
55 PMD_FAILSAFE_MAC_KVARG,
61 * output: 0: if text[0] != '(',
62 * 0: if there are no corresponding ')'
63 * n: distance to corresponding ')' otherwise
66 closing_paren(const char *text)
71 while (text[i] != '\0') {
84 fs_parse_device(struct sub_device *sdev, char *args)
86 struct rte_devargs *d;
91 ret = rte_eal_devargs_parse(args, d);
93 DEBUG("devargs parsing failed with code %d", ret);
97 sdev->state = DEV_PARSED;
102 fs_sanitize_cmdline(char *args)
106 len = strnlen(args, DEVARGS_MAXLEN);
107 args[len - 1] = '\0';
111 fs_execute_cmd(struct sub_device *sdev, char *cmdline)
114 /* store possible newline as well */
115 char output[DEVARGS_MAXLEN + 1];
120 RTE_ASSERT(cmdline != NULL || sdev->cmdline != NULL);
121 if (sdev->cmdline == NULL) {
124 len = strlen(cmdline) + 1;
125 sdev->cmdline = calloc(1, len);
126 if (sdev->cmdline == NULL) {
127 ERROR("Command line allocation failed");
130 snprintf(sdev->cmdline, len, "%s", cmdline);
131 /* Replace all commas in the command line by spaces */
132 for (i = 0; i < len; i++)
133 if (sdev->cmdline[i] == ',')
134 sdev->cmdline[i] = ' ';
136 DEBUG("'%s'", sdev->cmdline);
138 fp = popen(sdev->cmdline, "r");
141 ERROR("popen: %s", strerror(errno));
145 /* We only read one line */
146 if (fgets(output, sizeof(output) - 1, fp) == NULL) {
147 DEBUG("Could not read command output");
151 fs_sanitize_cmdline(output);
152 ret = fs_parse_device(sdev, output);
154 ERROR("Parsing device '%s' failed", output);
158 pclose_ret = pclose(fp);
161 ERROR("pclose: %s", strerror(errno));
169 fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
172 struct fs_priv *priv;
173 struct sub_device *sdev;
182 while (param[b] != '(' &&
186 b += closing_paren(¶m[b]);
188 ERROR("Dangling parenthesis");
192 args = strndup(¶m[a], b - a);
194 ERROR("Not enough memory for parameter parsing");
197 sdev = &priv->subs[head];
198 if (strncmp(param, "dev", 3) == 0) {
199 ret = fs_parse_device(sdev, args);
202 } else if (strncmp(param, "exec", 4) == 0) {
203 ret = fs_execute_cmd(sdev, args);
204 if (ret == -ENODEV) {
205 DEBUG("Reading device info from command line failed");
211 ERROR("Unrecognized device type: %.*s", (int)b, param);
220 fs_parse_sub_devices(parse_cb *cb,
221 struct rte_eth_dev *dev, const char *params)
230 while (params[a] != '\0') {
232 while (params[b] != '(' &&
237 ERROR("Invalid parameter");
240 if (params[b] == ',') {
244 if (params[b] == '(') {
247 b += closing_paren(¶ms[b]);
249 ERROR("Dangling parenthesis");
252 ret = (*cb)(dev, ¶ms[a], head);
257 if (params[b] == '\0')
266 fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
268 char buffer[DEVARGS_MAXLEN] = {0};
274 while (params[a] != '\0') {
276 while (params[b] != '(' &&
281 ERROR("Invalid parameter");
284 if (params[b] == ',' || params[b] == '\0')
285 i += snprintf(&buffer[i], b - a + 1, "%s", ¶ms[a]);
286 if (params[b] == '(') {
288 b += closing_paren(¶ms[b]);
292 if (params[b] == '\0')
298 snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
303 fs_get_u64_arg(const char *key __rte_unused,
304 const char *value, void *out)
309 if ((value == NULL) || (out == NULL))
312 *u64 = strtoull(value, &endptr, 0);
321 fs_get_mac_addr_arg(const char *key __rte_unused,
322 const char *value, void *out)
324 struct ether_addr *ea = out;
327 if ((value == NULL) || (out == NULL))
329 ret = sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
330 &ea->addr_bytes[0], &ea->addr_bytes[1],
331 &ea->addr_bytes[2], &ea->addr_bytes[3],
332 &ea->addr_bytes[4], &ea->addr_bytes[5]);
333 return ret != ETHER_ADDR_LEN;
337 failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
339 struct fs_priv *priv;
340 char mut_params[DEVARGS_MAXLEN] = "";
341 struct rte_kvargs *kvlist = NULL;
342 unsigned int arg_count;
348 priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
349 /* default parameters */
350 n = snprintf(mut_params, sizeof(mut_params), "%s", params);
351 if (n >= sizeof(mut_params)) {
352 ERROR("Parameter string too long (>=%zu)",
356 ret = fs_parse_sub_devices(fs_parse_device_param,
360 ret = fs_remove_sub_devices_definition(mut_params);
363 if (strnlen(mut_params, sizeof(mut_params)) > 0) {
364 kvlist = rte_kvargs_parse(mut_params,
365 pmd_failsafe_init_parameters);
366 if (kvlist == NULL) {
367 ERROR("Error parsing parameters, usage:\n"
368 PMD_FAILSAFE_PARAM_STRING);
371 /* PLUG_IN event poll timer */
372 arg_count = rte_kvargs_count(kvlist,
373 PMD_FAILSAFE_HOTPLUG_POLL_KVARG);
374 if (arg_count == 1) {
375 ret = rte_kvargs_process(kvlist,
376 PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
377 &fs_get_u64_arg, &hotplug_poll);
382 arg_count = rte_kvargs_count(kvlist,
383 PMD_FAILSAFE_MAC_KVARG);
385 ret = rte_kvargs_process(kvlist,
386 PMD_FAILSAFE_MAC_KVARG,
387 &fs_get_mac_addr_arg,
388 &dev->data->mac_addrs[0]);
394 PRIV(dev)->state = DEV_PARSED;
396 rte_kvargs_free(kvlist);
401 failsafe_args_free(struct rte_eth_dev *dev)
403 struct sub_device *sdev;
406 FOREACH_SUBDEV(sdev, i, dev) {
407 rte_free(sdev->cmdline);
408 sdev->cmdline = NULL;
409 free(sdev->devargs.args);
410 sdev->devargs.args = NULL;
415 fs_count_device(struct rte_eth_dev *dev, const char *param,
416 uint8_t head __rte_unused)
420 while (param[b] != '(' &&
423 if (strncmp(param, "dev", b) != 0 &&
424 strncmp(param, "exec", b) != 0) {
425 ERROR("Unrecognized device type: %.*s", (int)b, param);
428 PRIV(dev)->subs_tail += 1;
433 failsafe_args_count_subdevice(struct rte_eth_dev *dev,
436 return fs_parse_sub_devices(fs_count_device,
441 fs_parse_sub_device(struct sub_device *sdev)
443 struct rte_devargs *da;
444 char devstr[DEVARGS_MAXLEN] = "";
447 snprintf(devstr, sizeof(devstr), "%s,%s", da->name, da->args);
448 return fs_parse_device(sdev, devstr);
452 failsafe_args_parse_subs(struct rte_eth_dev *dev)
454 struct sub_device *sdev;
458 FOREACH_SUBDEV(sdev, i, dev) {
459 if (sdev->state >= DEV_PARSED)
462 ret = fs_execute_cmd(sdev, sdev->cmdline);
464 ret = fs_parse_sub_device(sdev);
466 sdev->state = DEV_PARSED;