From 4969f5914c9e696c73d0aff2a3adc2f1ade606d1 Mon Sep 17 00:00:00 2001 From: Gaetan Rivet Date: Fri, 7 Jul 2017 02:04:33 +0200 Subject: [PATCH] devargs: introduce new parsing helper Introduce a more versatile helper to parse device strings. This helper expects a generic rte_devargs structure as storage in order not to require API changes in the future, should this structure be updated. The old equivalent function is thus being deprecated, as its API does not allow to accompany rte_devargs evolutions. A deprecation notice is issued. This new helper will parse bus information as well as device name and device parameters. It does not allocate an rte_devargs structure and expects one to be given as input. Signed-off-by: Gaetan Rivet --- doc/guides/rel_notes/deprecation.rst | 5 ++ lib/librte_eal/bsdapp/eal/rte_eal_version.map | 1 + lib/librte_eal/common/eal_common_devargs.c | 78 ++++++++++++------- lib/librte_eal/common/include/rte_devargs.h | 20 +++++ .../linuxapp/eal/rte_eal_version.map | 1 + 5 files changed, 79 insertions(+), 26 deletions(-) diff --git a/doc/guides/rel_notes/deprecation.rst b/doc/guides/rel_notes/deprecation.rst index 68a1ccfd11..257dcba323 100644 --- a/doc/guides/rel_notes/deprecation.rst +++ b/doc/guides/rel_notes/deprecation.rst @@ -59,3 +59,8 @@ Deprecation Notices The non-"do-sig" versions of the hash tables will be removed (including the ``signature_offset`` parameter) and the "do-sig" versions renamed accordingly. + +* eal: the following function is deprecated starting from 17.08 and will + be removed in 17.11: + + - ``rte_eal_parse_devargs_str``, replaced by ``rte_eal_devargs_parse`` diff --git a/lib/librte_eal/bsdapp/eal/rte_eal_version.map b/lib/librte_eal/bsdapp/eal/rte_eal_version.map index 0295ea948a..381f895cd1 100644 --- a/lib/librte_eal/bsdapp/eal/rte_eal_version.map +++ b/lib/librte_eal/bsdapp/eal/rte_eal_version.map @@ -206,6 +206,7 @@ DPDK_17.08 { EXPERIMENTAL { global: + rte_eal_devargs_parse; rte_eal_hotplug_add; rte_eal_hotplug_remove; diff --git a/lib/librte_eal/common/eal_common_devargs.c b/lib/librte_eal/common/eal_common_devargs.c index f0c3291fbf..49d43a3285 100644 --- a/lib/librte_eal/common/eal_common_devargs.c +++ b/lib/librte_eal/common/eal_common_devargs.c @@ -83,49 +83,77 @@ bus_name_cmp(const struct rte_bus *bus, const void *name) return strncmp(bus->name, name, strlen(bus->name)); } -/* store a whitelist parameter for later parsing */ int -rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str) +rte_eal_devargs_parse(const char *dev, struct rte_devargs *da) { - struct rte_devargs *devargs = NULL; struct rte_bus *bus = NULL; - char *dev = NULL; const char *devname; - int ret; - - /* use malloc instead of rte_malloc as it's called early at init */ - devargs = malloc(sizeof(*devargs)); - if (devargs == NULL) - goto fail; - - memset(devargs, 0, sizeof(*devargs)); - devargs->type = devtype; + const size_t maxlen = sizeof(da->name); + size_t i; - if (rte_eal_parse_devargs_str(devargs_str, &dev, &devargs->args)) - goto fail; - devname = dev; + if (dev == NULL || da == NULL) + return -EINVAL; + /* Retrieve eventual bus info */ do { + devname = dev; bus = rte_bus_find(bus, bus_name_cmp, dev); if (bus == NULL) break; devname = dev + strlen(bus->name) + 1; if (rte_bus_find_by_device_name(devname) == bus) break; - devname = dev; } while (1); + /* Store device name */ + i = 0; + while (devname[i] != '\0' && devname[i] != ',') { + da->name[i] = devname[i]; + i++; + if (i == maxlen) { + fprintf(stderr, "WARNING: Parsing \"%s\": device name should be shorter than %zu\n", + dev, maxlen); + da->name[i - 1] = '\0'; + return -EINVAL; + } + } + da->name[i] = '\0'; if (bus == NULL) { - bus = rte_bus_find_by_device_name(devname); + bus = rte_bus_find_by_device_name(da->name); if (bus == NULL) { - fprintf(stderr, "ERROR: failed to parse bus info from device declaration\n"); - goto fail; + fprintf(stderr, "ERROR: failed to parse device \"%s\"\n", + da->name); + return -EFAULT; } } - devargs->bus = bus; + da->bus = bus; + /* Parse eventual device arguments */ + if (devname[i] == ',') + da->args = strdup(&devname[i + 1]); + else + da->args = strdup(""); + if (da->args == NULL) { + fprintf(stderr, "ERROR: not enough memory to parse arguments\n"); + return -ENOMEM; + } + return 0; +} + +/* store a whitelist parameter for later parsing */ +int +rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str) +{ + struct rte_devargs *devargs = NULL; + struct rte_bus *bus = NULL; + const char *dev = devargs_str; - /* save device name. */ - ret = snprintf(devargs->name, sizeof(devargs->name), "%s", devname); - if (ret < 0 || ret >= (int)sizeof(devargs->name)) + /* use calloc instead of rte_zmalloc as it's called early at init */ + devargs = calloc(1, sizeof(*devargs)); + if (devargs == NULL) goto fail; + + if (rte_eal_devargs_parse(dev, devargs)) + goto fail; + devargs->type = devtype; + bus = devargs->bus; if (devargs->type == RTE_DEVTYPE_WHITELISTED) { if (bus->conf.scan_mode == RTE_BUS_SCAN_UNDEFINED) { bus->conf.scan_mode = RTE_BUS_SCAN_WHITELIST; @@ -142,12 +170,10 @@ rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str) } } - free(dev); TAILQ_INSERT_TAIL(&devargs_list, devargs, next); return 0; fail: - free(dev); if (devargs) { free(devargs->args); free(devargs); diff --git a/lib/librte_eal/common/include/rte_devargs.h b/lib/librte_eal/common/include/rte_devargs.h index a5c4a4ae56..a0427cd8a7 100644 --- a/lib/librte_eal/common/include/rte_devargs.h +++ b/lib/librte_eal/common/include/rte_devargs.h @@ -118,6 +118,26 @@ extern struct rte_devargs_list devargs_list; int rte_eal_parse_devargs_str(const char *devargs_str, char **drvname, char **drvargs); +/** + * Parse a device string. + * + * Verify that a bus is capable of handling the device passed + * in argument. Store which bus will handle the device, its name + * and the eventual device parameters. + * + * @param dev + * The device declaration string. + * @param da + * The devargs structure holding the device information. + * + * @return + * - 0 on success. + * - Negative errno on error. + */ +int +rte_eal_devargs_parse(const char *dev, + struct rte_devargs *da); + /** * Add a device to the user device list * diff --git a/lib/librte_eal/linuxapp/eal/rte_eal_version.map b/lib/librte_eal/linuxapp/eal/rte_eal_version.map index a118fb1600..0f9e009b60 100644 --- a/lib/librte_eal/linuxapp/eal/rte_eal_version.map +++ b/lib/librte_eal/linuxapp/eal/rte_eal_version.map @@ -211,6 +211,7 @@ DPDK_17.08 { EXPERIMENTAL { global: + rte_eal_devargs_parse; rte_eal_hotplug_add; rte_eal_hotplug_remove; -- 2.20.1