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 nl = strrchr(args, '\n');
112 fs_execute_cmd(struct sub_device *sdev, char *cmdline)
115 /* store possible newline as well */
116 char output[DEVARGS_MAXLEN + 1];
121 RTE_ASSERT(cmdline != NULL || sdev->cmdline != NULL);
122 if (sdev->cmdline == NULL) {
125 len = strlen(cmdline) + 1;
126 sdev->cmdline = calloc(1, len);
127 if (sdev->cmdline == NULL) {
128 ERROR("Command line allocation failed");
131 snprintf(sdev->cmdline, len, "%s", cmdline);
132 /* Replace all commas in the command line by spaces */
133 for (i = 0; i < len; i++)
134 if (sdev->cmdline[i] == ',')
135 sdev->cmdline[i] = ' ';
137 DEBUG("'%s'", sdev->cmdline);
139 fp = popen(sdev->cmdline, "r");
142 ERROR("popen: %s", strerror(errno));
146 /* We only read one line */
147 if (fgets(output, sizeof(output) - 1, fp) == NULL) {
148 DEBUG("Could not read command output");
152 fs_sanitize_cmdline(output);
153 if (output[0] == '\0') {
157 ret = fs_parse_device(sdev, output);
159 ERROR("Parsing device '%s' failed", output);
163 pclose_ret = pclose(fp);
166 ERROR("pclose: %s", strerror(errno));
174 fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
177 struct fs_priv *priv;
178 struct sub_device *sdev;
187 while (param[b] != '(' &&
191 b += closing_paren(¶m[b]);
193 ERROR("Dangling parenthesis");
197 args = strndup(¶m[a], b - a);
199 ERROR("Not enough memory for parameter parsing");
202 sdev = &priv->subs[head];
203 if (strncmp(param, "dev", 3) == 0) {
204 ret = fs_parse_device(sdev, args);
207 } else if (strncmp(param, "exec", 4) == 0) {
208 ret = fs_execute_cmd(sdev, args);
209 if (ret == -ENODEV) {
210 DEBUG("Reading device info from command line failed");
216 ERROR("Unrecognized device type: %.*s", (int)b, param);
225 fs_parse_sub_devices(parse_cb *cb,
226 struct rte_eth_dev *dev, const char *params)
235 while (params[a] != '\0') {
237 while (params[b] != '(' &&
242 ERROR("Invalid parameter");
245 if (params[b] == ',') {
249 if (params[b] == '(') {
252 b += closing_paren(¶ms[b]);
254 ERROR("Dangling parenthesis");
257 ret = (*cb)(dev, ¶ms[a], head);
262 if (params[b] == '\0')
271 fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
273 char buffer[DEVARGS_MAXLEN] = {0};
279 while (params[a] != '\0') {
281 while (params[b] != '(' &&
286 ERROR("Invalid parameter");
289 if (params[b] == ',' || params[b] == '\0')
290 i += snprintf(&buffer[i], b - a + 1, "%s", ¶ms[a]);
291 if (params[b] == '(') {
293 b += closing_paren(¶ms[b]);
297 if (params[b] == '\0')
303 snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
308 fs_get_u64_arg(const char *key __rte_unused,
309 const char *value, void *out)
314 if ((value == NULL) || (out == NULL))
317 *u64 = strtoull(value, &endptr, 0);
326 fs_get_mac_addr_arg(const char *key __rte_unused,
327 const char *value, void *out)
329 struct ether_addr *ea = out;
332 if ((value == NULL) || (out == NULL))
334 ret = sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
335 &ea->addr_bytes[0], &ea->addr_bytes[1],
336 &ea->addr_bytes[2], &ea->addr_bytes[3],
337 &ea->addr_bytes[4], &ea->addr_bytes[5]);
338 return ret != ETHER_ADDR_LEN;
342 failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
344 struct fs_priv *priv;
345 char mut_params[DEVARGS_MAXLEN] = "";
346 struct rte_kvargs *kvlist = NULL;
347 unsigned int arg_count;
353 priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
354 /* default parameters */
355 n = snprintf(mut_params, sizeof(mut_params), "%s", params);
356 if (n >= sizeof(mut_params)) {
357 ERROR("Parameter string too long (>=%zu)",
361 ret = fs_parse_sub_devices(fs_parse_device_param,
365 ret = fs_remove_sub_devices_definition(mut_params);
368 if (strnlen(mut_params, sizeof(mut_params)) > 0) {
369 kvlist = rte_kvargs_parse(mut_params,
370 pmd_failsafe_init_parameters);
371 if (kvlist == NULL) {
372 ERROR("Error parsing parameters, usage:\n"
373 PMD_FAILSAFE_PARAM_STRING);
376 /* PLUG_IN event poll timer */
377 arg_count = rte_kvargs_count(kvlist,
378 PMD_FAILSAFE_HOTPLUG_POLL_KVARG);
379 if (arg_count == 1) {
380 ret = rte_kvargs_process(kvlist,
381 PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
382 &fs_get_u64_arg, &hotplug_poll);
387 arg_count = rte_kvargs_count(kvlist,
388 PMD_FAILSAFE_MAC_KVARG);
390 ret = rte_kvargs_process(kvlist,
391 PMD_FAILSAFE_MAC_KVARG,
392 &fs_get_mac_addr_arg,
393 &dev->data->mac_addrs[0]);
399 PRIV(dev)->state = DEV_PARSED;
401 rte_kvargs_free(kvlist);
406 failsafe_args_free(struct rte_eth_dev *dev)
408 struct sub_device *sdev;
411 FOREACH_SUBDEV(sdev, i, dev) {
412 rte_free(sdev->cmdline);
413 sdev->cmdline = NULL;
414 free(sdev->devargs.args);
415 sdev->devargs.args = NULL;
420 fs_count_device(struct rte_eth_dev *dev, const char *param,
421 uint8_t head __rte_unused)
425 while (param[b] != '(' &&
428 if (strncmp(param, "dev", b) != 0 &&
429 strncmp(param, "exec", b) != 0) {
430 ERROR("Unrecognized device type: %.*s", (int)b, param);
433 PRIV(dev)->subs_tail += 1;
438 failsafe_args_count_subdevice(struct rte_eth_dev *dev,
441 return fs_parse_sub_devices(fs_count_device,
446 fs_parse_sub_device(struct sub_device *sdev)
448 struct rte_devargs *da;
449 char devstr[DEVARGS_MAXLEN] = "";
452 snprintf(devstr, sizeof(devstr), "%s,%s", da->name, da->args);
453 return fs_parse_device(sdev, devstr);
457 failsafe_args_parse_subs(struct rte_eth_dev *dev)
459 struct sub_device *sdev;
463 FOREACH_SUBDEV(sdev, i, dev) {
464 if (sdev->state >= DEV_PARSED)
467 ret = fs_execute_cmd(sdev, sdev->cmdline);
469 ret = fs_parse_sub_device(sdev);
471 sdev->state = DEV_PARSED;