From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by dpdk.org (Postfix) with ESMTP id 6C7AE1BAA3 for ; Wed, 11 Apr 2018 02:04:54 +0200 (CEST) Received: by mail-wm0-f66.google.com with SMTP id r131so556738wmb.2 for ; Tue, 10 Apr 2018 17:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=zInE0O+CAUbP0S8yhN3lfS7+gcMdiy2Qa5s7XNaVXA8=; b=eXiYQY2mL0uwD18WdavfxvmaNwlvOFWTSaHbBMQWJ118WCnwzc2KfUYPNNv+zCnzTe tiB+EMtmcR13nt44NfimtQkO1O3Cr0Ar2VCN0u/MDTlff2peutKueLrPHHPh88K0ZasX vC0KO1t0ayrljlb9CMNrFnHd/TUMifsHGhc0kFj4FfKgIIxOBT5/0TDXghY5CWzUnizm nYj/hqr9i6wMJ6+5LbRAMeuvMCRDK+yqryPnMy1UyjHKyzwbFxfk0pkrwgBNtTr4T/F5 0ZJEJRdZ/y7mjzHo8T4+PK6QddVVP0qQTrtvpJ0SUgwt86hcHhcwPzHwQc7RSVrZtUY5 1KoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=zInE0O+CAUbP0S8yhN3lfS7+gcMdiy2Qa5s7XNaVXA8=; b=tMviItEP2Hry1+KtKrDaS2l7yWP9IBMK9vOW9RyIEjYkONOIj4E2XQh0Y3vYclCfFJ b8ZuXeK+C6BKQhM+aNMxUM3/qOu+FXUAuzY+rPNs/8gaEHAmpMpLcOq5rtWTDECTlqEw TsVnFJJboAQdMTzTtKmF37ktQR9r4izb6NWEk6h4E6FRJzfBUEXGLVaoytcP9oeLSiik q+c2Rbw8dpLZWFuDGVnTSRlsD80g+XtjMiLcBRouwLEO9auyId/692HipxUXNpyP2/nT LTRruWPoW44CB1I/U41/RTPNsUQooHu9nqE4xRSPfLzslDgvcMDXLppsIsqwUg+yoIIx x/lQ== X-Gm-Message-State: ALQs6tD5YIrkQCBx3tbxFImV6mrmnIDK+W6sYDURU908BfAUNT7xvlOZ JxGp+qzTJvnRZoWZmwO6s5H/ARRz X-Google-Smtp-Source: AIpwx48xOYB7q5xAj0hLGyfmPkT2nloG2atWutkPdKHaaR/yXzyHABJd5lUT5QJH165AyPTE8jzHNg== X-Received: by 10.28.44.68 with SMTP id s65mr888540wms.80.1523405093788; Tue, 10 Apr 2018 17:04:53 -0700 (PDT) Received: from bidouze.dev.6wind.com. (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id 39sm7626563wry.89.2018.04.10.17.04.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 10 Apr 2018 17:04:52 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Wed, 11 Apr 2018 02:04:11 +0200 Message-Id: <81d673726db79c1699ecaea8facbd44368efaf9e.1523404469.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH v5 10/21] eal/dev: implement device iteration 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: Wed, 11 Apr 2018 00:04:54 -0000 Use the iteration hooks in the abstraction layers to perform the requested filtering on the internal device lists. Signed-off-by: Gaetan Rivet --- lib/librte_eal/common/eal_common_dev.c | 168 ++++++++++++++++++++++++++++++++ lib/librte_eal/common/include/rte_dev.h | 26 +++++ lib/librte_eal/rte_eal_version.map | 1 + 3 files changed, 195 insertions(+) diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c index 1a671758a..645cb2ce8 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -20,6 +20,28 @@ #include "eal_private.h" +struct dev_next_ctx { + struct rte_dev_iterator *it; + const char *busstr; + const char *clsstr; +}; + +#define CTX(it, busstr, clsstr) \ + (&(const struct dev_next_ctx){ \ + .it = it, \ + .busstr = busstr, \ + .clsstr = clsstr, \ + }) + +#define ITCTX(ptr) \ + (((struct dev_next_ctx *)(intptr_t)ptr)->it) + +#define BUSCTX(ptr) \ + (((struct dev_next_ctx *)(intptr_t)ptr)->busstr) + +#define CLSCTX(ptr) \ + (((struct dev_next_ctx *)(intptr_t)ptr)->clsstr) + static int cmp_detached_dev_name(const struct rte_device *dev, const void *_name) { @@ -355,3 +377,149 @@ rte_dev_iterator_init(struct rte_dev_iterator *it, } return -rte_errno; } + +static char * +dev_str_sane_copy(const char *str) +{ + size_t end; + char *copy; + + end = strcspn(str, ",/"); + if (str[end] == ',') { + copy = strdup(&str[end + 1]); + } else { + /* '/' or '\0' */ + copy = strdup(""); + } + if (copy == NULL) { + rte_errno = ENOMEM; + } else { + char *slash; + + slash = strchr(copy, '/'); + if (slash != NULL) + slash[0] = '\0'; + } + return copy; +} + +static int +class_next_dev_cmp(const struct rte_class *cls, + const void *ctx) +{ + struct rte_dev_iterator *it; + const char *clsstr = NULL; + void *dev; + + if (cls->dev_iterate == NULL) + return 1; + it = ITCTX(ctx); + clsstr = CLSCTX(ctx); + dev = it->class_device; + /* it->clsstr != NULL means a class + * was specified in the devstr. + */ + if (it->clsstr != NULL && cls != it->cls) + return 1; + /* If an error occurred previously, + * no need to test further. + */ + if (rte_errno != 0) + return -1; + dev = cls->dev_iterate(dev, clsstr, it); + it->class_device = dev; + return dev == NULL; +} + +static int +bus_next_dev_cmp(const struct rte_bus *bus, + const void *ctx) +{ + struct rte_device *dev = NULL; + struct rte_class *cls = NULL; + struct rte_dev_iterator *it; + const char *busstr = NULL; + + if (bus->dev_iterate == NULL) + return 1; + it = ITCTX(ctx); + busstr = BUSCTX(ctx); + dev = it->device; + /* it->busstr != NULL means a bus + * was specified in the devstr. + */ + if (it->busstr != NULL && bus != it->bus) + return 1; + /* If an error occurred previously, + * no need to test further. + */ + if (rte_errno != 0) + return -1; + if (it->clsstr == NULL) { + dev = bus->dev_iterate(dev, busstr, it); + goto end; + } + /* clsstr != NULL */ + if (dev == NULL) { +next_dev_on_bus: + dev = bus->dev_iterate(dev, busstr, it); + it->device = dev; + } + if (dev == NULL) + return 1; + if (it->cls != NULL) + cls = TAILQ_PREV(it->cls, rte_class_list, next); + cls = rte_class_find(cls, class_next_dev_cmp, ctx); + if (cls != NULL) { + it->cls = cls; + goto end; + } + goto next_dev_on_bus; +end: + it->device = dev; + return dev == NULL; +} +__rte_experimental +struct rte_device * +rte_dev_iterator_next(struct rte_dev_iterator *it) +{ + struct rte_bus *bus = NULL; + int old_errno = rte_errno; + char *busstr = NULL; + char *clsstr = NULL; + + rte_errno = 0; + if (it->busstr == NULL && it->clsstr == NULL) { + /* Invalid iterator. */ + rte_errno = EINVAL; + return NULL; + } + if (it->bus != NULL) + bus = TAILQ_PREV(it->bus, rte_bus_list, next); + if (it->busstr != NULL) { + busstr = dev_str_sane_copy(it->busstr); + if (busstr == NULL) + goto out; + } + if (it->clsstr != NULL) { + clsstr = dev_str_sane_copy(it->clsstr); + if (clsstr == NULL) + goto out; + } + while ((bus = rte_bus_find(bus, bus_next_dev_cmp, + CTX(it, busstr, clsstr)))) { + if (it->device != NULL) { + it->bus = bus; + goto out; + } + if (it->busstr != NULL || + rte_errno != 0) + break; + } + if (rte_errno == 0) + rte_errno = old_errno; +out: + free(busstr); + free(clsstr); + return it->device; +} diff --git a/lib/librte_eal/common/include/rte_dev.h b/lib/librte_eal/common/include/rte_dev.h index f84128e5c..b35fa3cd9 100644 --- a/lib/librte_eal/common/include/rte_dev.h +++ b/lib/librte_eal/common/include/rte_dev.h @@ -334,6 +334,32 @@ __rte_experimental int rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str); +/** + * Iterates on a device iterator. + * + * Generates a new rte_device handle corresponding to the next element + * in the list described in comprehension by the iterator. + * + * The next object is returned, and the iterator is updated. + * + * @param it + * Device iterator handle. + * + * @return + * An rte_device handle if found. + * NULL if an error occurred (rte_errno is set). + * NULL if no device could be found (rte_errno is not set). + */ +__rte_experimental +struct rte_device * +rte_dev_iterator_next(struct rte_dev_iterator *it); + +#define RTE_DEV_FOREACH(dev, devstr, it) \ + for (rte_dev_iterator_init(it, devstr), \ + dev = rte_dev_iterator_next(it); \ + dev != NULL; \ + dev = rte_dev_iterator_next(it)) + #ifdef __cplusplus } #endif diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map index cad24376f..f75aa7245 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -221,6 +221,7 @@ EXPERIMENTAL { rte_class_register; rte_class_unregister; rte_dev_iterator_init; + rte_dev_iterator_next; rte_eal_hotplug_add; rte_eal_hotplug_remove; rte_eal_mbuf_user_pool_ops; -- 2.11.0