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];
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);
137 fp = popen(sdev->cmdline, "r");
140 ERROR("popen: %s", strerror(errno));
143 /* We only read one line */
144 if (fgets(output, sizeof(output) - 1, fp) == NULL) {
145 DEBUG("Could not read command output");
149 fs_sanitize_cmdline(output);
150 if (output[0] == '\0') {
154 ret = fs_parse_device(sdev, output);
156 ERROR("Parsing device '%s' failed", output);
158 if (pclose(fp) == -1)
159 ERROR("pclose: %s", strerror(errno));
164 fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
167 struct fs_priv *priv;
168 struct sub_device *sdev;
177 while (param[b] != '(' &&
181 b += closing_paren(¶m[b]);
183 ERROR("Dangling parenthesis");
187 args = strndup(¶m[a], b - a);
189 ERROR("Not enough memory for parameter parsing");
192 sdev = &priv->subs[head];
193 if (strncmp(param, "dev", 3) == 0) {
194 ret = fs_parse_device(sdev, args);
197 } else if (strncmp(param, "exec", 4) == 0) {
198 ret = fs_execute_cmd(sdev, args);
199 if (ret == -ENODEV) {
200 DEBUG("Reading device info from command line failed");
206 ERROR("Unrecognized device type: %.*s", (int)b, param);
215 fs_parse_sub_devices(parse_cb *cb,
216 struct rte_eth_dev *dev, const char *params)
225 while (params[a] != '\0') {
227 while (params[b] != '(' &&
232 ERROR("Invalid parameter");
235 if (params[b] == ',') {
239 if (params[b] == '(') {
242 b += closing_paren(¶ms[b]);
244 ERROR("Dangling parenthesis");
247 ret = (*cb)(dev, ¶ms[a], head);
252 if (params[b] == '\0')
261 fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
263 char buffer[DEVARGS_MAXLEN] = {0};
269 while (params[a] != '\0') {
271 while (params[b] != '(' &&
276 ERROR("Invalid parameter");
279 if (params[b] == ',' || params[b] == '\0') {
284 snprintf(&buffer[i], len + 1, "%s%s",
285 i ? "," : "", ¶ms[a]);
287 } else if (params[b] == '(') {
290 b += closing_paren(¶ms[b]);
294 if (params[b] == '\0')
300 snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
305 fs_get_u64_arg(const char *key __rte_unused,
306 const char *value, void *out)
311 if ((value == NULL) || (out == NULL))
314 *u64 = strtoull(value, &endptr, 0);
323 fs_get_mac_addr_arg(const char *key __rte_unused,
324 const char *value, void *out)
326 struct ether_addr *ea = out;
329 if ((value == NULL) || (out == NULL))
331 ret = sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
332 &ea->addr_bytes[0], &ea->addr_bytes[1],
333 &ea->addr_bytes[2], &ea->addr_bytes[3],
334 &ea->addr_bytes[4], &ea->addr_bytes[5]);
335 return ret != ETHER_ADDR_LEN;
339 failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
341 struct fs_priv *priv;
342 char mut_params[DEVARGS_MAXLEN] = "";
343 struct rte_kvargs *kvlist = NULL;
344 unsigned int arg_count;
350 priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
351 /* default parameters */
352 n = snprintf(mut_params, sizeof(mut_params), "%s", params);
353 if (n >= sizeof(mut_params)) {
354 ERROR("Parameter string too long (>=%zu)",
358 ret = fs_parse_sub_devices(fs_parse_device_param,
362 ret = fs_remove_sub_devices_definition(mut_params);
365 if (strnlen(mut_params, sizeof(mut_params)) > 0) {
366 kvlist = rte_kvargs_parse(mut_params,
367 pmd_failsafe_init_parameters);
368 if (kvlist == NULL) {
369 ERROR("Error parsing parameters, usage:\n"
370 PMD_FAILSAFE_PARAM_STRING);
373 /* PLUG_IN event poll timer */
374 arg_count = rte_kvargs_count(kvlist,
375 PMD_FAILSAFE_HOTPLUG_POLL_KVARG);
376 if (arg_count == 1) {
377 ret = rte_kvargs_process(kvlist,
378 PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
379 &fs_get_u64_arg, &hotplug_poll);
384 arg_count = rte_kvargs_count(kvlist,
385 PMD_FAILSAFE_MAC_KVARG);
387 ret = rte_kvargs_process(kvlist,
388 PMD_FAILSAFE_MAC_KVARG,
389 &fs_get_mac_addr_arg,
390 &dev->data->mac_addrs[0]);
397 PRIV(dev)->state = DEV_PARSED;
399 rte_kvargs_free(kvlist);
404 failsafe_args_free(struct rte_eth_dev *dev)
406 struct sub_device *sdev;
409 FOREACH_SUBDEV(sdev, i, dev) {
410 rte_free(sdev->cmdline);
411 sdev->cmdline = NULL;
412 free(sdev->devargs.args);
413 sdev->devargs.args = NULL;
418 fs_count_device(struct rte_eth_dev *dev, const char *param,
419 uint8_t head __rte_unused)
423 while (param[b] != '(' &&
426 if (strncmp(param, "dev", b) != 0 &&
427 strncmp(param, "exec", b) != 0) {
428 ERROR("Unrecognized device type: %.*s", (int)b, param);
431 PRIV(dev)->subs_tail += 1;
436 failsafe_args_count_subdevice(struct rte_eth_dev *dev,
439 return fs_parse_sub_devices(fs_count_device,
444 fs_parse_sub_device(struct sub_device *sdev)
446 struct rte_devargs *da;
447 char devstr[DEVARGS_MAXLEN] = "";
450 snprintf(devstr, sizeof(devstr), "%s,%s", da->name, da->args);
451 return fs_parse_device(sdev, devstr);
455 failsafe_args_parse_subs(struct rte_eth_dev *dev)
457 struct sub_device *sdev;
461 FOREACH_SUBDEV(sdev, i, dev) {
462 if (sdev->state >= DEV_PARSED)
465 ret = fs_execute_cmd(sdev, sdev->cmdline);
467 ret = fs_parse_sub_device(sdev);
469 sdev->state = DEV_PARSED;