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.
36 #include <rte_devargs.h>
37 #include <rte_malloc.h>
38 #include <rte_kvargs.h>
40 #include "failsafe_private.h"
42 #define DEVARGS_MAXLEN 4096
44 /* Callback used when a new device is found in devargs */
45 typedef int (parse_cb)(struct rte_eth_dev *dev, const char *params,
50 const char *pmd_failsafe_init_parameters[] = {
51 PMD_FAILSAFE_MAC_KVARG,
57 * output: 0: if text[0] != '(',
58 * 0: if there are no corresponding ')'
59 * n: distance to corresponding ')' otherwise
62 closing_paren(const char *text)
67 while (text[i] != '\0') {
80 fs_parse_device(struct sub_device *sdev, char *args)
82 struct rte_devargs *d;
87 ret = rte_eal_devargs_parse(args, d);
89 DEBUG("devargs parsing failed with code %d", ret);
93 sdev->state = DEV_PARSED;
98 fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
101 struct fs_priv *priv;
102 struct sub_device *sdev;
111 while (param[b] != '(' &&
115 b += closing_paren(¶m[b]);
117 ERROR("Dangling parenthesis");
121 args = strndup(¶m[a], b - a);
123 ERROR("Not enough memory for parameter parsing");
126 sdev = &priv->subs[head];
127 if (strncmp(param, "dev", 3) == 0) {
128 ret = fs_parse_device(sdev, args);
132 ERROR("Unrecognized device type: %.*s", (int)b, param);
141 fs_parse_sub_devices(parse_cb *cb,
142 struct rte_eth_dev *dev, const char *params)
151 while (params[a] != '\0') {
153 while (params[b] != '(' &&
158 ERROR("Invalid parameter");
161 if (params[b] == ',') {
165 if (params[b] == '(') {
168 b += closing_paren(¶ms[b]);
170 ERROR("Dangling parenthesis");
173 ret = (*cb)(dev, ¶ms[a], head);
178 if (params[b] == '\0')
187 fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
189 char buffer[DEVARGS_MAXLEN] = {0};
195 while (params[a] != '\0') {
197 while (params[b] != '(' &&
202 ERROR("Invalid parameter");
205 if (params[b] == ',' || params[b] == '\0')
206 i += snprintf(&buffer[i], b - a + 1, "%s", ¶ms[a]);
207 if (params[b] == '(') {
209 b += closing_paren(¶ms[b]);
213 if (params[b] == '\0')
219 snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
224 fs_get_mac_addr_arg(const char *key __rte_unused,
225 const char *value, void *out)
227 struct ether_addr *ea = out;
230 if ((value == NULL) || (out == NULL))
232 ret = sscanf(value, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
233 &ea->addr_bytes[0], &ea->addr_bytes[1],
234 &ea->addr_bytes[2], &ea->addr_bytes[3],
235 &ea->addr_bytes[4], &ea->addr_bytes[5]);
236 return ret != ETHER_ADDR_LEN;
240 failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
242 struct fs_priv *priv;
243 char mut_params[DEVARGS_MAXLEN] = "";
244 struct rte_kvargs *kvlist = NULL;
245 unsigned int arg_count;
251 priv->subs_tx = FAILSAFE_MAX_ETHPORTS;
252 /* default parameters */
253 n = snprintf(mut_params, sizeof(mut_params), "%s", params);
254 if (n >= sizeof(mut_params)) {
255 ERROR("Parameter string too long (>=%zu)",
259 ret = fs_parse_sub_devices(fs_parse_device_param,
263 ret = fs_remove_sub_devices_definition(mut_params);
266 if (strnlen(mut_params, sizeof(mut_params)) > 0) {
267 kvlist = rte_kvargs_parse(mut_params,
268 pmd_failsafe_init_parameters);
269 if (kvlist == NULL) {
270 ERROR("Error parsing parameters, usage:\n"
271 PMD_FAILSAFE_PARAM_STRING);
275 arg_count = rte_kvargs_count(kvlist,
276 PMD_FAILSAFE_MAC_KVARG);
278 ret = rte_kvargs_process(kvlist,
279 PMD_FAILSAFE_MAC_KVARG,
280 &fs_get_mac_addr_arg,
281 &dev->data->mac_addrs[0]);
288 rte_kvargs_free(kvlist);
293 failsafe_args_free(struct rte_eth_dev *dev)
295 struct sub_device *sdev;
298 FOREACH_SUBDEV(sdev, i, dev) {
299 free(sdev->devargs.args);
300 sdev->devargs.args = NULL;
305 fs_count_device(struct rte_eth_dev *dev, const char *param,
306 uint8_t head __rte_unused)
310 while (param[b] != '(' &&
313 if (strncmp(param, "dev", b) != 0) {
314 ERROR("Unrecognized device type: %.*s", (int)b, param);
317 PRIV(dev)->subs_tail += 1;
322 failsafe_args_count_subdevice(struct rte_eth_dev *dev,
325 return fs_parse_sub_devices(fs_count_device,