From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9A854A0032; Wed, 29 Sep 2021 23:40:22 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 51EDD410F0; Wed, 29 Sep 2021 23:40:11 +0200 (CEST) Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by mails.dpdk.org (Postfix) with ESMTP id 12021410F2 for ; Wed, 29 Sep 2021 23:40:06 +0200 (CEST) Received: by mail-wm1-f41.google.com with SMTP id j27so2966627wms.0 for ; Wed, 29 Sep 2021 14:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qXKA7hBgJPdrYfOCcAdOOtanDoOf3WmmEVZubd/adjY=; b=G2olN1tIGofVXNBs9A5spKEmj0BD/HWI9aesJhhe8KesxiHm6SCXppriIQuYfWSd1x mGWjMYUIYuPvP31pkUmHXIPuIMqZlyaHmSzh63uOIobUDe2rfZ5lr4hCuQiUdilkLh0j 9OF0vpBKfEo+Qjza+ZyNQGdoZtr8N4MKsG2whFtFpOG48miRiOaGNNDPzpHEGy1Z1xEe OSBQ44O66gWmtXpiMaMlFSe0SURkMxB5sJMjSg1boW/fE1nveUM6Qls+5VQTB0Z5JqSf e0Tgr1J52v9twR43eVTrWSAwEwbdfc57JJ9QYs3RjQCk86nxfQHVdkoAFJA46xXFthMP xLjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qXKA7hBgJPdrYfOCcAdOOtanDoOf3WmmEVZubd/adjY=; b=q0yZCO+zGQBF4F2fUZkZmLF3vSBWFVxKZOAt8+aglKOGTASNYYDC0Z4glFbJLwrvrL B3KK7aCDvdQxDnUdi85Gedk3I2YHClKFHBVuYkFp9/Z3aE8Qicvqn+aMD1RacFPg2HE2 0dqFYkv0Hdj4KUGgR9XKVCcWRmhFw7tbm+dA7QjZklcH/oGNlQzZg5be+xCWaxLGoE7u Yw9R4tmxOdzWvtR64N1FXB73f2iC5JQqa6A0f4BRx7YUwVWUhANH9zVUAh7y8/pgIIFO 4yiDm4uy//izWCC7/zauLeSiEoZg7NKcgTkkNwCB46qe7eqE+SonWvcVQC5Xn/Ov/aYv adxw== X-Gm-Message-State: AOAM530WczJNdyjdVyrfcDPPu2E26w58XEQftB70DDRkX9xQefmU/++z 8lauCccyCg4OCz6eEPQK/bHUMSeSdAuSEQ== X-Google-Smtp-Source: ABdhPJzcEIpVOyzt2Qo/MUiEK44oLnmly2DVfSIhLESwcmzoE6zqSGGPdNJR8WvWqigm04DNlmGR4A== X-Received: by 2002:a05:600c:4fc7:: with SMTP id o7mr12839356wmq.91.1632951605793; Wed, 29 Sep 2021 14:40:05 -0700 (PDT) Received: from gojira.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id o19sm1079743wrg.60.2021.09.29.14.40.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Sep 2021 14:40:05 -0700 (PDT) From: Olivier Matz To: dev@dpdk.org Cc: Olivier Matz , Bruce Richardson , Xueming Li , Gaetan Rivet , Ray Kinsella Date: Wed, 29 Sep 2021 23:39:41 +0200 Message-Id: <20210929213943.17817-4-olivier.matz@6wind.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210929213943.17817-1-olivier.matz@6wind.com> References: <20210924155740.20510-1-olivier.matz@6wind.com> <20210929213943.17817-1-olivier.matz@6wind.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v2 3/5] kvargs: new function to get from key and value X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" A quite common scenario with kvargs is to lookup for a = in a kvlist. For instance, check if name=foo is present in name=toto,name=foo,name=bar. This is currently done in drivers/bus with rte_kvargs_process() + the rte_kvargs_strcmp() handler. This approach is not straightforward, and can be replaced by this new function. Signed-off-by: Olivier Matz Reviewed-by: Xueming Li --- drivers/bus/auxiliary/auxiliary_params.c | 9 ++++---- drivers/bus/vdev/vdev_params.c | 13 ++--------- lib/kvargs/rte_kvargs.c | 23 +++++++++++++++---- lib/kvargs/rte_kvargs.h | 29 +++++++++++++++++++++++- lib/kvargs/version.map | 2 ++ 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/drivers/bus/auxiliary/auxiliary_params.c b/drivers/bus/auxiliary/auxiliary_params.c index cd3fa56cb4..a9c7853ed1 100644 --- a/drivers/bus/auxiliary/auxiliary_params.c +++ b/drivers/bus/auxiliary/auxiliary_params.c @@ -25,13 +25,12 @@ auxiliary_dev_match(const struct rte_device *dev, const void *_kvlist) { const struct rte_kvargs *kvlist = _kvlist; - int ret; + const char *key = auxiliary_params_keys[RTE_AUXILIARY_PARAM_NAME]; - ret = rte_kvargs_process(kvlist, - auxiliary_params_keys[RTE_AUXILIARY_PARAM_NAME], - rte_kvargs_strcmp, (void *)(uintptr_t)dev->name); + if (rte_kvargs_get_with_value(kvlist, key, dev->name) == NULL) + return -1; - return ret != 0 ? -1 : 0; + return 0; } void * diff --git a/drivers/bus/vdev/vdev_params.c b/drivers/bus/vdev/vdev_params.c index 6f74704d1c..37d95395e7 100644 --- a/drivers/bus/vdev/vdev_params.c +++ b/drivers/bus/vdev/vdev_params.c @@ -26,19 +26,10 @@ static int vdev_dev_match(const struct rte_device *dev, const void *_kvlist) { - int ret; const struct rte_kvargs *kvlist = _kvlist; - char *name; + const char *key = vdev_params_keys[RTE_VDEV_PARAM_NAME]; - /* cannot pass const dev->name to rte_kvargs_process() */ - name = strdup(dev->name); - if (name == NULL) - return -1; - ret = rte_kvargs_process(kvlist, - vdev_params_keys[RTE_VDEV_PARAM_NAME], - rte_kvargs_strcmp, name); - free(name); - if (ret != 0) + if (rte_kvargs_get_with_value(kvlist, key, dev->name) == NULL) return -1; return 0; diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c index 38e9d5c1ca..20abb23183 100644 --- a/lib/kvargs/rte_kvargs.c +++ b/lib/kvargs/rte_kvargs.c @@ -204,21 +204,34 @@ rte_kvargs_free(struct rte_kvargs *kvlist) free(kvlist); } -/* Lookup a value in an rte_kvargs list by its key. */ +/* Lookup a value in an rte_kvargs list by its key and value. */ const char * -rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key) +rte_kvargs_get_with_value(const struct rte_kvargs *kvlist, const char *key, + const char *value) { unsigned int i; - if (kvlist == NULL || key == NULL) + if (kvlist == NULL) return NULL; for (i = 0; i < kvlist->count; ++i) { - if (strcmp(kvlist->pairs[i].key, key) == 0) - return kvlist->pairs[i].value; + if (key != NULL && strcmp(kvlist->pairs[i].key, key) != 0) + continue; + if (value != NULL && strcmp(kvlist->pairs[i].value, value) != 0) + continue; + return kvlist->pairs[i].value; } return NULL; } +/* Lookup a value in an rte_kvargs list by its key. */ +const char * +rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key) +{ + if (kvlist == NULL || key == NULL) + return NULL; + return rte_kvargs_get_with_value(kvlist, key, NULL); +} + /* * Parse the arguments "key=value,key=value,..." string and return * an allocated structure that contains a key/value list. Also diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h index 328f0d3cc6..e40df1bd2a 100644 --- a/lib/kvargs/rte_kvargs.h +++ b/lib/kvargs/rte_kvargs.h @@ -116,7 +116,7 @@ void rte_kvargs_free(struct rte_kvargs *kvlist); /** * Get the value associated with a given key. * - * If multiple key matches, the value of the first one is returned. + * If multiple keys match, the value of the first one is returned. * * The memory returned is allocated as part of the rte_kvargs structure, * it must never be modified. @@ -132,6 +132,33 @@ void rte_kvargs_free(struct rte_kvargs *kvlist); */ const char *rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice + * + * Get the value associated with a given key and value. + * + * Find the first entry in the kvlist whose key and value match the + * ones passed as argument. + * + * The memory returned is allocated as part of the rte_kvargs structure, + * it must never be modified. + * + * @param kvlist + * A list of rte_kvargs pair of 'key=value'. + * @param key + * The matching key. If NULL, any key will match. + * @param value + * The matching value. If NULL, any value will match. + * + * @return + * NULL if no key matches the input, + * a value associated with a matching key otherwise. + */ +__rte_experimental +const char *rte_kvargs_get_with_value(const struct rte_kvargs *kvlist, + const char *key, const char *value); + /** * Call a handler function for each key/value matching the key * diff --git a/lib/kvargs/version.map b/lib/kvargs/version.map index 236f35c02b..82879b7140 100644 --- a/lib/kvargs/version.map +++ b/lib/kvargs/version.map @@ -16,4 +16,6 @@ EXPERIMENTAL { rte_kvargs_strcmp; + # added in 21.11 + rte_kvargs_get_with_value; }; -- 2.30.2