From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f196.google.com (mail-wr0-f196.google.com [209.85.128.196]) by dpdk.org (Postfix) with ESMTP id 6DACD7CAF for ; Wed, 12 Jul 2017 01:25:44 +0200 (CEST) Received: by mail-wr0-f196.google.com with SMTP id k67so1741468wrc.1 for ; Tue, 11 Jul 2017 16:25:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=4VTrQTBmn4wt3z2+NjQstX3JF+JZA6Ci16pIiSLkFEg=; b=tnTn4v3nD7DlSWRb+a+MFBcgO6xWPWOo4U8UNmkYeeQJ+2iJjuUmt2bkCc2nVAXw1p nXzUSG55A6cwy3/BdIgqNeBX8xrZx1RNk1MCGuHDCkXyCa39y7xQl/e4L3qcd6ixF2ph owiYbtKnPJL+3e36p9oDS4cjxzwy5R8ixReZwhA9XueTL6sjVy8xvj0qpddaDEIHcjr8 FXCVoBBaF2CWEcsLa5Rx/2cdSz6SsMJm+9zNI6/Od8F3A45rbLkaFeRqBqfAcVOquunJ qtgD6svsvRPAkmq4vhWVi+r7qS6fZU7EVWYc8gd6O036Iaw83PgVqKJZUjKZLfVmULAS DriA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=4VTrQTBmn4wt3z2+NjQstX3JF+JZA6Ci16pIiSLkFEg=; b=AG0FaKlT004m/IE+gSVtedDCcRDn/jFV+kT0QBlXMaSsw3fzoksvk2VO8NOaNU4IHz EZSMwDDThUUCJRcx66Hx1kRfWZS6Ot/uPK+nwNK/oNqPpIGAAh6oROK+2fJgJJayldtf Ha+yMgdt46s9cab70oX26z+xGIMPEyaA24cGSasln61KE8tKve02SJ5lPk4YUmPP2/H1 G5q55wtj2hvMEBnAznen8fxTSmjjaoMSJ8BNcCjWWa7ynBu7EExEzg2qtMxRp3HLh/TI KiILiDLEDiM5keiOBZbA/Bgzb/N1gA33oE6sQnQsl+Ey+nFdoKp/GuSEE1JAadHTXyv/ OOTg== X-Gm-Message-State: AIVw113YrwzdJ2C8aiBqlMujknLjldiiHmSwhFFwm+EHxRCBcRM+OQba ef7uNHchbF7yYQjA X-Received: by 10.80.165.229 with SMTP id b34mr4221512edc.29.1499815543812; Tue, 11 Jul 2017 16:25:43 -0700 (PDT) Received: from localhost.localdomain ([37.228.147.16]) by smtp.gmail.com with ESMTPSA id k45sm311516ede.16.2017.07.11.16.25.42 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 11 Jul 2017 16:25:42 -0700 (PDT) Sender: Jan Blunck From: Jan Blunck To: dev@dpdk.org Date: Tue, 11 Jul 2017 19:25:08 -0400 Message-Id: <20170711232512.54641-10-jblunck@infradead.org> X-Mailer: git-send-email 2.13.2 In-Reply-To: <20170711232512.54641-1-jblunck@infradead.org> References: <20170711232512.54641-1-jblunck@infradead.org> Subject: [dpdk-dev] [PATCH 09/13] devargs: parse "bus=" argument X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Jul 2017 23:25:45 -0000 Let the rte_eal_devargs_parse() function parse the "bus=" argument. Signed-off-by: Jan Blunck --- lib/librte_eal/common/eal_common_devargs.c | 131 ++++++++++++++++++----------- test/test/test_devargs.c | 19 +++++ 2 files changed, 102 insertions(+), 48 deletions(-) diff --git a/lib/librte_eal/common/eal_common_devargs.c b/lib/librte_eal/common/eal_common_devargs.c index 2bdee9a30..a0d0e6e91 100644 --- a/lib/librte_eal/common/eal_common_devargs.c +++ b/lib/librte_eal/common/eal_common_devargs.c @@ -77,6 +77,40 @@ rte_eal_parse_devargs_str(const char *devargs_str, return 0; } +/* + * Parse "bus=" argument without adding a dependency on rte_kvargs.h + */ +static char *parse_bus_arg(const char *args) +{ + const char *c; + char *str = NULL; + + if (!args || args[0] == '\0') + return NULL; + + c = args; + + do { + if (strncmp(c, "bus=", 4) == 0) { + c += 4; + break; + } + + c = strchr(c, ','); + if (c) + c++; + } while (c); + + if (c) { + str = strdup(c); + c = strchr(str, ','); + if (c) + c = '\0'; + } + + return str; +} + static int devargs_add(const char *busname, const char *name, const char *args) { @@ -113,64 +147,65 @@ devargs_add(const char *busname, const char *name, const char *args) return -1; } -static int -bus_name_cmp(const struct rte_bus *bus, const void *name) -{ - return strncmp(bus->name, name, strlen(bus->name)); -} - int rte_eal_devargs_parse(const char *dev, struct rte_devargs *da) { - struct rte_bus *bus = NULL; - const char *devname; - const size_t maxlen = sizeof(da->name); - size_t i; + char *busname; + char *devname; + char *drvargs; + int ret; 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; - } 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(da->name); + + ret = rte_eal_parse_devargs_str(dev, &devname, &drvargs); + if (ret != 0) + return ret; + + RTE_LOG(DEBUG, EAL, "%s: adding devargs for device [%s]: %s\n", + __FUNCTION__, devname, drvargs); + + /* Retrieve eventual bus info (...,bus=...)*/ + busname = parse_bus_arg(drvargs); + if (busname == NULL) { + struct rte_bus *bus; + + bus = rte_bus_find_by_device_name(devname); if (bus == NULL) { - fprintf(stderr, "ERROR: failed to parse device \"%s\"\n", - da->name); - return -EFAULT; + RTE_LOG(ERR, EAL, "failed to parse device [%s]\n", + devname); + ret = -EINVAL; + goto fail; } + + busname = strdup(bus->name); } - 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; + + ret = devargs_add(busname, devname, drvargs); + if (ret != 0) { + RTE_LOG(ERR, EAL, "devargs_add failed for device [%s]\n", + devname); + goto fail; } - return 0; + + ret = snprintf(da->busname, sizeof(da->busname), "%s", busname); + if (ret < 0 || ret >= (int)sizeof(da->busname)) + goto fail; + + ret = snprintf(da->name, sizeof(da->name), "%s", devname); + if (ret < 0 || ret >= (int)sizeof(da->name)) + goto fail; + + da->args = strdup(drvargs); + if (da->args == NULL) + ret = -ENOMEM; + ret = 0; + +fail: + free(busname); + free(devname); + free(drvargs); + return ret; } static const struct rte_bus_conf BUS_CONF_WHITELIST = { diff --git a/test/test/test_devargs.c b/test/test/test_devargs.c index 048b3d00b..3a1033ca3 100644 --- a/test/test/test_devargs.c +++ b/test/test/test_devargs.c @@ -58,6 +58,7 @@ test_devargs(void) { struct rte_devargs_list save_devargs_list; struct rte_devargs *devargs; + struct rte_devargs devargs_stack; /* save the real devargs_list, it is restored at the end of the test */ save_devargs_list = devargs_list; @@ -109,6 +110,24 @@ test_devargs(void) goto fail; free_devargs_list(); + if (rte_eal_devargs_parse("", &devargs_stack) == 0) { + printf("Error in rte_eal_devargs_parse\n"); + goto fail; + } + + if (rte_eal_devargs_parse("08:00.1,foo=bar,bus=pci", &devargs_stack) != 0) { + printf("Error in rte_eal_devargs_parse 08:00.1,bus=pci\n"); + goto fail; + } + devargs = TAILQ_FIRST(&devargs_list); + if (strcmp(devargs->name, "08:00.1") != 0) + goto fail; + if (strcmp(devargs->busname, "pci") != 0) + goto fail; + if (!devargs->args || strcmp(devargs->args, "foo=bar,bus=pci") != 0) + goto fail; + + free_devargs_list(); devargs_list = save_devargs_list; return 0; -- 2.13.2