From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f50.google.com (mail-wm0-f50.google.com [74.125.82.50]) by dpdk.org (Postfix) with ESMTP id 351021BEFC for ; Thu, 5 Jul 2018 13:49:14 +0200 (CEST) Received: by mail-wm0-f50.google.com with SMTP id i139-v6so10576760wmf.4 for ; Thu, 05 Jul 2018 04:49:14 -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; bh=lW3jHlnRhfN2aBmSCSE+TxwoKO+DJP6lU7B9ktdGrlg=; b=hK/nQ82gYGma+p5mJ1kBRWQK/pzuGFdsq8eLT+xo7ffU/F0qfwbo9MqoZt+ogMpeRC xUHr1oUsqKVUQMVQmzbIOtz45B0/ggpgN6wK1XHukMo1r0sDUPpqm8PlebtOH6t3lh+V 32O4BRB6oWLhpxrA5+8jJL9v4k6aX1gJGaj5akRXAVzn8KLRbURHH2r5JTlkss6qto79 KmbB2zf+MfAtig+hDggiOHpIvOcpLqC1vBiVRH8Rte7bULoQ9EiDkayjClGqfKlM445i 0sIuBpQMG2QnWARAIeWWkCde0CXyBUCSMTuTzICSVub7GRaKMBiqlh/4Nv7y8+FNH+hb kfDg== 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; bh=lW3jHlnRhfN2aBmSCSE+TxwoKO+DJP6lU7B9ktdGrlg=; b=lqlaCygqaphkEy5FUHu3DbOxlrm9akzplVEi/xdeabMj+G0zor4hiapr9hwIirpLpD Kb4IjUeZAWLzjzW1GyVWeuUp1tJDihlDqPG3f7nNl9m3booYcGSmadhUZKRs2t9LUtE9 XpqAYe5So1snoiCmP2aA0YGouhJ0PyQrsbbvIZfNzwq1NjydtWYeRinOb9POT0a0o0LT r51repxCtcumIzbQlCM5EEGu4QGTH6LiaI2Z7DfgiB6PtweNdtD3pkJ4UMPVmAie8bLJ W5H511l5dEDeYu/0NSZP9b/hIj5yFwZmlAc9a2Cph7WeIEcJHdNRJ6OpcqmdMWVyvEX8 1xhw== X-Gm-Message-State: APt69E0E2MUMr4JZlHeAYK8Rrw7+96kOXlGo3YeqOPwC1Ioam4UMPGN6 M6eaACWuB0OEf6OrpAydRq74qqSr X-Google-Smtp-Source: AAOMgpewfUiwkuk64vVqW32CKCCQx7OlgtyKb4fpPnQMufzYADo3VqGGSOxGdcPY+ixjLAa2Fd+3hA== X-Received: by 2002:a1c:9c0b:: with SMTP id f11-v6mr3881376wme.148.1530791353490; Thu, 05 Jul 2018 04:49:13 -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 o42-v6sm15809183wrc.50.2018.07.05.04.49.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Jul 2018 04:49:12 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Thu, 5 Jul 2018 13:48:20 +0200 Message-Id: <379a32c4447eed32b982d5d04b0cc0be2cf38ce0.1530791217.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: Subject: [dpdk-dev] [PATCH v10 13/27] 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: Thu, 05 Jul 2018 11:49:14 -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 5b7956d17..3d486a841 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -45,6 +45,28 @@ static struct dev_event_cb_list dev_event_cbs; /* spinlock for device callbacks */ static rte_spinlock_t dev_event_lock = RTE_SPINLOCK_INITIALIZER; +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) { @@ -398,3 +420,149 @@ rte_dev_iterator_init(struct rte_dev_iterator *it, get_out: 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 7b7bd83bc..1553c0970 100644 --- a/lib/librte_eal/common/include/rte_dev.h +++ b/lib/librte_eal/common/include/rte_dev.h @@ -356,6 +356,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 ac04120d6..4cd5ab3df 100644 --- a/lib/librte_eal/rte_eal_version.map +++ b/lib/librte_eal/rte_eal_version.map @@ -252,6 +252,7 @@ EXPERIMENTAL { rte_dev_event_monitor_start; rte_dev_event_monitor_stop; rte_dev_iterator_init; + rte_dev_iterator_next; rte_devargs_add; rte_devargs_dump; rte_devargs_insert; -- 2.18.0