From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by dpdk.org (Postfix) with ESMTP id 62E3C7CF1 for ; Wed, 21 Mar 2018 18:16:11 +0100 (CET) Received: by mail-wm0-f67.google.com with SMTP id f19so11170953wmc.0 for ; Wed, 21 Mar 2018 10:16:11 -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=+cg5qZtDxeHzMjGxNw0pmqibmZbpaoK+lJYMwanYeNI=; b=MZ06jddG5vu5+JpgBoUDdKsWVL0u/XbirxW2m6wB8qV7fVz9fWW/NuRSpIT1P6ETA9 M24SioHSFvVARcNQATImQvSAvw9dTb9px4/fdVVHi+KkwRcImvh6nYXLvrqFuLCwkukR k9kDWbTbdFXm34vDNL8I7B7Ppb9EYGtc7FAblp53h0eJZoDaS/PeU+4oIM40bxBKh4BQ Sk3O/FHOCpDhrvZXibEMCLEasG+LnLCsySYiqvg31ShU+ZoVbWCAPIRpm0SNxneLHmBa mzg7qo3GgOKUCPj4x2Z8xISOxoBOGniFZ6YCoD66GAuxEoxMqjiDQpnP6WzSUlL7jY7A lEOQ== 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=+cg5qZtDxeHzMjGxNw0pmqibmZbpaoK+lJYMwanYeNI=; b=DBHXNceoJkRyNMHmPOsabS6iWaxBJEPZrP2wy7FxmaFEmhktYik9cYehVVpiX3xL4x dBscOIYXYkoAPQvg3gtKis2NFjl9gjUDRII5tW8611KKUxaKG5OCqqpWLccmeurhcHcv luV/UerXhvZMLxc+Z/ghEb5kvHt7Uff6DLkrioy2idNb76mBhMCRHcNDdIZ9pl3xUAws dviDwYhAWk+11vsZ+n3949IhiTp6SYfsZ2xPIW9A3dYTFmPFN1tKtuxgPZCKJihY0tIG bIrGn9vPk/P+0OYf+xoYscM+76nq6aI9F5Gi2iqiwaDu+AR23gjUKdhpsY9dohOm5cb1 seCQ== X-Gm-Message-State: AElRT7F0X4D9uaHoh1BPqNSEeiUsp3zVTfJvxFR68D83I/rErC13Bmdt LLaQfvLrl8/hlTn16vpmVihEBvNs X-Google-Smtp-Source: AG47ELsneBy6oY188dW3HgWHrRJnfBtJdC1nxIrY5TYiXniJI//yIbxu7tCHkiUVb2atEpNRP41f1A== X-Received: by 10.28.35.76 with SMTP id j73mr3026708wmj.103.1521652570269; Wed, 21 Mar 2018 10:16:10 -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 l10sm4224101wrf.37.2018.03.21.10.16.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Mar 2018 10:16:09 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Wed, 21 Mar 2018 18:15:30 +0100 Message-Id: <390e98e1afddd5c80ba243efc44b8a047ea68710.1521652453.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 v2 09/18] 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, 21 Mar 2018 17:16:11 -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 | 159 ++++++++++++++++++++++++++++++++ lib/librte_eal/common/include/rte_dev.h | 25 +++++ lib/librte_eal/rte_eal_version.map | 1 + 3 files changed, 185 insertions(+) diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c index bdab1423a..4efc27462 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -19,6 +19,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 + /* EAL-private function. */ int rte_parse_kv(const char *str, struct rte_kvarg *kv) @@ -303,3 +325,140 @@ rte_dev_iterator_init(struct rte_dev_iterator *it, const char *str) it->cls = cls; return 0; } + +static char * +dev_str_sane_cpy(const char *str) +{ + struct rte_kvarg kv; + char *end; + char *cpy; + + if (rte_parse_kv(str, &kv)) { + rte_errno = EINVAL; + return NULL; + } + /* copying '\0' is valid. */ + if (kv.next != NULL) + cpy = strdup(kv.next); + else + cpy = strdup(""); + if (cpy == NULL) { + rte_errno = ENOMEM; + return NULL; + } + end = strchr(cpy, '/'); + if (end != NULL) + end[0] = '\0'; + return cpy; +} + +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; + 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 (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_cpy(it->busstr); + if (busstr == NULL) + goto out; + } + if (it->clsstr != NULL) { + clsstr = dev_str_sane_cpy(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 7ce13e068..dfb5d696f 100644 --- a/lib/librte_eal/common/include/rte_dev.h +++ b/lib/librte_eal/common/include/rte_dev.h @@ -333,6 +333,31 @@ typedef void *(*rte_dev_iterate_t)(const void *start, int __rte_experimental 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). + */ +struct rte_device * __rte_experimental +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 921da3075..925efcb6d 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -229,6 +229,7 @@ EXPERIMENTAL { rte_mp_request; rte_mp_reply; rte_dev_iterator_init; + rte_dev_iterator_next; rte_service_attr_get; rte_service_attr_reset_all; rte_service_component_register; -- 2.11.0