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_devargs.h>
38 #include <rte_malloc.h>
39 #include <rte_kvargs.h>
41 #include "failsafe_private.h"
43 #define DEVARGS_MAXLEN 4096
45 /* Callback used when a new device is found in devargs */
46 typedef int (parse_cb)(struct rte_eth_dev *dev, const char *params,
49 uint64_t hotplug_poll = FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS;
52 const char *pmd_failsafe_init_parameters[] = {
53 PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
54 PMD_FAILSAFE_MAC_KVARG,
60 * output: 0: if text[0] != '(',
61 * 0: if there are no corresponding ')'
62 * n: distance to corresponding ')' otherwise
65 closing_paren(const char *text)
70 while (text[i] != '\0') {
83 fs_parse_device(struct sub_device *sdev, char *args)
85 struct rte_devargs *d;
90 ret = rte_eal_devargs_parse(args, d);
92 DEBUG("devargs parsing failed with code %d", ret);
96 sdev->state = DEV_PARSED;
101 fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
104 struct fs_priv *priv;
105 struct sub_device *sdev;
114 while (param[b] != '(' &&
118 b += closing_paren(¶m[b]);
120 ERROR("Dangling parenthesis");
124 args = strndup(¶m[a], b - a);
126 ERROR("Not enough memory for parameter parsing");
129 sdev = &priv->subs[head];
130 if (strncmp(param, "dev", 3) == 0) {
131 ret = fs_parse_device(sdev, args);
135 ERROR("Unrecognized device type: %.*s", (int)b, param);
144 fs_parse_sub_devices(parse_cb *cb,
145 struct rte_eth_dev *dev, const char *params)
154 while (params[a] != '\0') {
156 while (params[b] != '(' &&
161 ERROR("Invalid parameter");
164 if (params[b] == ',') {
168 if (params[b] == '(') {
171 b += closing_paren(¶ms[b]);
173 ERROR("Dangling parenthesis");
176 ret = (*cb)(dev, ¶ms[a], head);
181 if (params[b] == '\0')
190 fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
192 char buffer[DEVARGS_MAXLEN] = {0};
198 while (params[a] != '\0') {
200 while (params[b] != '(' &&
205 ERROR("Invalid parameter");
208 if (params[b] == ',' || params[b] == '\0')
209 i += snprintf(&buffer[i], b - a + 1, "%s", ¶ms[a]);
210 if (params[b] == '(') {
212 b += closing_paren(¶ms[b]);
216 if (params[b] == '\0')
222 snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
227 fs_get_u64_arg(const char *key __rte_unused,
228 const char *value, void *out)
233 if ((value == NULL) || (out == NULL))
236 *u64 = strtoull(value, &endptr, 0);
245 fs_get_mac_addr_arg(const char *key __rte_unused,
246 const char *value, void *out)
248 struct ether_addr *ea = out;
251 if ((value == NULL) || (out == NULL))
253 ret = sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
254 &ea->addr_bytes[0], &ea->addr_bytes[1],
255 &ea->addr_bytes[2], &ea->addr_bytes[3],
256 &ea->addr_bytes[4], &ea->addr_bytes[5]);
257 return ret != ETHER_ADDR_LEN;
261 failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
263 struct fs_priv *priv;
264 char mut_params[DEVARGS_MAXLEN] = "";
265 struct rte_kvargs *kvlist = NULL;
266 unsigned int arg_count;
272 priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
273 /* default parameters */
274 n = snprintf(mut_params, sizeof(mut_params), "%s", params);
275 if (n >= sizeof(mut_params)) {
276 ERROR("Parameter string too long (>=%zu)",
280 ret = fs_parse_sub_devices(fs_parse_device_param,
284 ret = fs_remove_sub_devices_definition(mut_params);
287 if (strnlen(mut_params, sizeof(mut_params)) > 0) {
288 kvlist = rte_kvargs_parse(mut_params,
289 pmd_failsafe_init_parameters);
290 if (kvlist == NULL) {
291 ERROR("Error parsing parameters, usage:\n"
292 PMD_FAILSAFE_PARAM_STRING);
295 /* PLUG_IN event poll timer */
296 arg_count = rte_kvargs_count(kvlist,
297 PMD_FAILSAFE_HOTPLUG_POLL_KVARG);
298 if (arg_count == 1) {
299 ret = rte_kvargs_process(kvlist,
300 PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
301 &fs_get_u64_arg, &hotplug_poll);
306 arg_count = rte_kvargs_count(kvlist,
307 PMD_FAILSAFE_MAC_KVARG);
309 ret = rte_kvargs_process(kvlist,
310 PMD_FAILSAFE_MAC_KVARG,
311 &fs_get_mac_addr_arg,
312 &dev->data->mac_addrs[0]);
318 PRIV(dev)->state = DEV_PARSED;
320 rte_kvargs_free(kvlist);
325 failsafe_args_free(struct rte_eth_dev *dev)
327 struct sub_device *sdev;
330 FOREACH_SUBDEV(sdev, i, dev) {
331 free(sdev->devargs.args);
332 sdev->devargs.args = NULL;
337 fs_count_device(struct rte_eth_dev *dev, const char *param,
338 uint8_t head __rte_unused)
342 while (param[b] != '(' &&
345 if (strncmp(param, "dev", b) != 0) {
346 ERROR("Unrecognized device type: %.*s", (int)b, param);
349 PRIV(dev)->subs_tail += 1;
354 failsafe_args_count_subdevice(struct rte_eth_dev *dev,
357 return fs_parse_sub_devices(fs_count_device,