net/failsafe: fix missing pclose after popen
[dpdk.git] / drivers / net / failsafe / failsafe_args.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <string.h>
35 #include <errno.h>
36
37 #include <rte_debug.h>
38 #include <rte_devargs.h>
39 #include <rte_malloc.h>
40 #include <rte_kvargs.h>
41
42 #include "failsafe_private.h"
43
44 #define DEVARGS_MAXLEN 4096
45
46 /* Callback used when a new device is found in devargs */
47 typedef int (parse_cb)(struct rte_eth_dev *dev, const char *params,
48                 uint8_t head);
49
50 uint64_t hotplug_poll = FAILSAFE_HOTPLUG_DEFAULT_TIMEOUT_MS;
51 int mac_from_arg = 0;
52
53 const char *pmd_failsafe_init_parameters[] = {
54         PMD_FAILSAFE_HOTPLUG_POLL_KVARG,
55         PMD_FAILSAFE_MAC_KVARG,
56         NULL,
57 };
58
59 /*
60  * input: text.
61  * output: 0: if text[0] != '(',
62  *         0: if there are no corresponding ')'
63  *         n: distance to corresponding ')' otherwise
64  */
65 static size_t
66 closing_paren(const char *text)
67 {
68         int nb_open = 0;
69         size_t i = 0;
70
71         while (text[i] != '\0') {
72                 if (text[i] == '(')
73                         nb_open++;
74                 if (text[i] == ')')
75                         nb_open--;
76                 if (nb_open == 0)
77                         return i;
78                 i++;
79         }
80         return 0;
81 }
82
83 static int
84 fs_parse_device(struct sub_device *sdev, char *args)
85 {
86         struct rte_devargs *d;
87         int ret;
88
89         d = &sdev->devargs;
90         DEBUG("%s", args);
91         ret = rte_eal_devargs_parse(args, d);
92         if (ret) {
93                 DEBUG("devargs parsing failed with code %d", ret);
94                 return ret;
95         }
96         sdev->bus = d->bus;
97         sdev->state = DEV_PARSED;
98         return 0;
99 }
100
101 static void
102 fs_sanitize_cmdline(char *args)
103 {
104         size_t len;
105
106         len = strnlen(args, DEVARGS_MAXLEN);
107         args[len - 1] = '\0';
108 }
109
110 static int
111 fs_execute_cmd(struct sub_device *sdev, char *cmdline)
112 {
113         FILE *fp;
114         /* store possible newline as well */
115         char output[DEVARGS_MAXLEN + 1];
116         size_t len;
117         int old_err;
118         int ret, pclose_ret;
119
120         RTE_ASSERT(cmdline != NULL || sdev->cmdline != NULL);
121         if (sdev->cmdline == NULL) {
122                 size_t i;
123
124                 len = strlen(cmdline) + 1;
125                 sdev->cmdline = calloc(1, len);
126                 if (sdev->cmdline == NULL) {
127                         ERROR("Command line allocation failed");
128                         return -ENOMEM;
129                 }
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] = ' ';
135         }
136         DEBUG("'%s'", sdev->cmdline);
137         old_err = errno;
138         fp = popen(sdev->cmdline, "r");
139         if (fp == NULL) {
140                 ret = errno;
141                 ERROR("popen: %s", strerror(errno));
142                 errno = old_err;
143                 return ret;
144         }
145         /* We only read one line */
146         if (fgets(output, sizeof(output) - 1, fp) == NULL) {
147                 DEBUG("Could not read command output");
148                 ret = -ENODEV;
149                 goto ret_pclose;
150         }
151         fs_sanitize_cmdline(output);
152         ret = fs_parse_device(sdev, output);
153         if (ret) {
154                 ERROR("Parsing device '%s' failed", output);
155                 goto ret_pclose;
156         }
157 ret_pclose:
158         pclose_ret = pclose(fp);
159         if (pclose_ret) {
160                 pclose_ret = errno;
161                 ERROR("pclose: %s", strerror(errno));
162                 errno = old_err;
163                 return pclose_ret;
164         }
165         return ret;
166 }
167
168 static int
169 fs_parse_device_param(struct rte_eth_dev *dev, const char *param,
170                 uint8_t head)
171 {
172         struct fs_priv *priv;
173         struct sub_device *sdev;
174         char *args = NULL;
175         size_t a, b;
176         int ret;
177
178         priv = PRIV(dev);
179         a = 0;
180         b = 0;
181         ret = 0;
182         while  (param[b] != '(' &&
183                 param[b] != '\0')
184                 b++;
185         a = b;
186         b += closing_paren(&param[b]);
187         if (a == b) {
188                 ERROR("Dangling parenthesis");
189                 return -EINVAL;
190         }
191         a += 1;
192         args = strndup(&param[a], b - a);
193         if (args == NULL) {
194                 ERROR("Not enough memory for parameter parsing");
195                 return -ENOMEM;
196         }
197         sdev = &priv->subs[head];
198         if (strncmp(param, "dev", 3) == 0) {
199                 ret = fs_parse_device(sdev, args);
200                 if (ret)
201                         goto free_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");
206                         ret = 0;
207                 }
208                 if (ret)
209                         goto free_args;
210         } else {
211                 ERROR("Unrecognized device type: %.*s", (int)b, param);
212                 return -EINVAL;
213         }
214 free_args:
215         free(args);
216         return ret;
217 }
218
219 static int
220 fs_parse_sub_devices(parse_cb *cb,
221                 struct rte_eth_dev *dev, const char *params)
222 {
223         size_t a, b;
224         uint8_t head;
225         int ret;
226
227         a = 0;
228         head = 0;
229         ret = 0;
230         while (params[a] != '\0') {
231                 b = a;
232                 while (params[b] != '(' &&
233                        params[b] != ',' &&
234                        params[b] != '\0')
235                         b++;
236                 if (b == a) {
237                         ERROR("Invalid parameter");
238                         return -EINVAL;
239                 }
240                 if (params[b] == ',') {
241                         a = b + 1;
242                         continue;
243                 }
244                 if (params[b] == '(') {
245                         size_t start = b;
246
247                         b += closing_paren(&params[b]);
248                         if (b == start) {
249                                 ERROR("Dangling parenthesis");
250                                 return -EINVAL;
251                         }
252                         ret = (*cb)(dev, &params[a], head);
253                         if (ret)
254                                 return ret;
255                         head += 1;
256                         b += 1;
257                         if (params[b] == '\0')
258                                 return 0;
259                 }
260                 a = b + 1;
261         }
262         return 0;
263 }
264
265 static int
266 fs_remove_sub_devices_definition(char params[DEVARGS_MAXLEN])
267 {
268         char buffer[DEVARGS_MAXLEN] = {0};
269         size_t a, b;
270         int i;
271
272         a = 0;
273         i = 0;
274         while (params[a] != '\0') {
275                 b = a;
276                 while (params[b] != '(' &&
277                        params[b] != ',' &&
278                        params[b] != '\0')
279                         b++;
280                 if (b == a) {
281                         ERROR("Invalid parameter");
282                         return -EINVAL;
283                 }
284                 if (params[b] == ',' || params[b] == '\0')
285                         i += snprintf(&buffer[i], b - a + 1, "%s", &params[a]);
286                 if (params[b] == '(') {
287                         size_t start = b;
288                         b += closing_paren(&params[b]);
289                         if (b == start)
290                                 return -EINVAL;
291                         b += 1;
292                         if (params[b] == '\0')
293                                 goto out;
294                 }
295                 a = b + 1;
296         }
297 out:
298         snprintf(params, DEVARGS_MAXLEN, "%s", buffer);
299         return 0;
300 }
301
302 static int
303 fs_get_u64_arg(const char *key __rte_unused,
304                 const char *value, void *out)
305 {
306         uint64_t *u64 = out;
307         char *endptr = NULL;
308
309         if ((value == NULL) || (out == NULL))
310                 return -EINVAL;
311         errno = 0;
312         *u64 = strtoull(value, &endptr, 0);
313         if (errno != 0)
314                 return -errno;
315         if (endptr == value)
316                 return -1;
317         return 0;
318 }
319
320 static int
321 fs_get_mac_addr_arg(const char *key __rte_unused,
322                 const char *value, void *out)
323 {
324         struct ether_addr *ea = out;
325         int ret;
326
327         if ((value == NULL) || (out == NULL))
328                 return -EINVAL;
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;
334 }
335
336 int
337 failsafe_args_parse(struct rte_eth_dev *dev, const char *params)
338 {
339         struct fs_priv *priv;
340         char mut_params[DEVARGS_MAXLEN] = "";
341         struct rte_kvargs *kvlist = NULL;
342         unsigned int arg_count;
343         size_t n;
344         int ret;
345
346         priv = PRIV(dev);
347         ret = 0;
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)",
353                                 sizeof(mut_params));
354                 return -ENOMEM;
355         }
356         ret = fs_parse_sub_devices(fs_parse_device_param,
357                                    dev, params);
358         if (ret < 0)
359                 return ret;
360         ret = fs_remove_sub_devices_definition(mut_params);
361         if (ret < 0)
362                 return ret;
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);
369                         return -1;
370                 }
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);
378                         if (ret < 0)
379                                 goto free_kvlist;
380                 }
381                 /* MAC addr */
382                 arg_count = rte_kvargs_count(kvlist,
383                                 PMD_FAILSAFE_MAC_KVARG);
384                 if (arg_count > 0) {
385                         ret = rte_kvargs_process(kvlist,
386                                         PMD_FAILSAFE_MAC_KVARG,
387                                         &fs_get_mac_addr_arg,
388                                         &dev->data->mac_addrs[0]);
389                         if (ret < 0)
390                                 goto free_kvlist;
391                         mac_from_arg = 1;
392                 }
393         }
394         PRIV(dev)->state = DEV_PARSED;
395 free_kvlist:
396         rte_kvargs_free(kvlist);
397         return ret;
398 }
399
400 void
401 failsafe_args_free(struct rte_eth_dev *dev)
402 {
403         struct sub_device *sdev;
404         uint8_t i;
405
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;
411         }
412 }
413
414 static int
415 fs_count_device(struct rte_eth_dev *dev, const char *param,
416                 uint8_t head __rte_unused)
417 {
418         size_t b = 0;
419
420         while  (param[b] != '(' &&
421                 param[b] != '\0')
422                 b++;
423         if (strncmp(param, "dev", b) != 0 &&
424             strncmp(param, "exec", b) != 0) {
425                 ERROR("Unrecognized device type: %.*s", (int)b, param);
426                 return -EINVAL;
427         }
428         PRIV(dev)->subs_tail += 1;
429         return 0;
430 }
431
432 int
433 failsafe_args_count_subdevice(struct rte_eth_dev *dev,
434                         const char *params)
435 {
436         return fs_parse_sub_devices(fs_count_device,
437                                     dev, params);
438 }
439
440 static int
441 fs_parse_sub_device(struct sub_device *sdev)
442 {
443         struct rte_devargs *da;
444         char devstr[DEVARGS_MAXLEN] = "";
445
446         da = &sdev->devargs;
447         snprintf(devstr, sizeof(devstr), "%s,%s", da->name, da->args);
448         return fs_parse_device(sdev, devstr);
449 }
450
451 int
452 failsafe_args_parse_subs(struct rte_eth_dev *dev)
453 {
454         struct sub_device *sdev;
455         uint8_t i;
456         int ret = 0;
457
458         FOREACH_SUBDEV(sdev, i, dev) {
459                 if (sdev->state >= DEV_PARSED)
460                         continue;
461                 if (sdev->cmdline)
462                         ret = fs_execute_cmd(sdev, sdev->cmdline);
463                 else
464                         ret = fs_parse_sub_device(sdev);
465                 if (ret == 0)
466                         sdev->state = DEV_PARSED;
467         }
468         return 0;
469 }