From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37489606A31F for ; Sat, 5 Dec 2026 21:01:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=networkplumber-org.20260707.gappssmtp.com header.i=@networkplumber-org.20260707.gappssmtp.com header.a=rsa-sha256 header.s=20260707 header.b=MCdDRt8k Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id DBA0442E7F; Sat, 5 Dec 2026 21:53:43 +0100 (CET) Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by mails.dpdk.org (Postfix) with ESMTPS id 3045A410E3 for ; Sat, 5 Dec 2026 21:53:42 +0100 (CET) Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3b4a93d0814so1012743a91.1 for ; Sat, 05 Dec 2026 12:53:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796504021; x=1797108821; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=iCpVMs4ke1h4kU08QRQjApWFVBDv61ZSLdt8TjLYEjc=; b=MCdDRt8kxAyw3qSRE4ZG+WAGkKTtFk7PaVTmD1+WuVvYbFbS8fSwkdx5QZ23e0bXXT uyAmfC1uyj9I1NfyP/kRJ86mYO/VYCAwCSTliAv9VK/pl5LPpL3S5c+V5EpwVD7zJCtY 3fzyXOuNSNAeiHgx7cHP7oqrx3TkdX0w0LxBTRn/DRYRYufPK9buRb/x0JpTyAkG/2Oj Sq9ySI6o4KSrH683WrL5DYNZjJtKDLFJ4hXx2NsKU0+BAZlOvnEMgqYDR5KLq+1n1clQ arG0npkXYT57a9wm9SgL1EvBJ32ymPsQbcPIozd/6b985YjXoXQRyLhgljUjbqEr1CZB Z0Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796504021; x=1797108821; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=iCpVMs4ke1h4kU08QRQjApWFVBDv61ZSLdt8TjLYEjc=; b=DI5hlrTwUBzTRERCbP0A7Q/+gVFB/U5FAcmGi5aosBPNQ0tKEncoS5UgOMwZs3ILV1 lV5pQIbpSkNGu+0514ckWQ0NM+pldtC+Y9nFYRYbp6XL/9BNqtcyAIWddZoTAiMc7Ywe i7rHVnJ2lS3VmuKFWeU8VHOXH2QuSxPbsuGeYFdUNsZ9VXFk5L6ELKRvIp0n9Dfhp8AB aENLg2OXWrFTuwHzMLpep+N2NXCD1YAhNBwzw5AdVmg5cNiFD5QT0kPOOArVZLf62mtD 9EhaO15TLj3sGQdSZd0jtQ+Gad9PUqeAZfIYwfrs8yyKjnMTRmjoy5d/oeKafY5duq2C oP2A== X-Gm-Message-State: AB4q+uEGl44A3xU8KYHiLWmMHkkBlAnbgs7CtEkC6mAB/5lrf0YOlsZV /PyOtA5/QVhZ8lR+1ZF6evWJZpN0ukBiSzLekWEPVs5rWe1IQdXJofkf11JLkK6g40pomDHSvMu arRxUZ4Y= X-Gm-Gg: AZFdTZeTlLHdnn+6QEGeHaYCSeztADMP0V20YOTmNK5zUJvLit6D717XTua83QKoZbP p741ujFmJwETDu308QxYeXr9yl40tRyt+vUj5zTXCD8iygdSqFDXNiJTpxoLixd1eO1/MlrwE/a gd6BESZ4upcqkRw21yOSaH+X1ldXWlOvW8iNi7Du+AvA9hSZMee0zcdM8+jLEFxdVKwGibVx2Hm AiYhUR4iAaCsF9D1fOWRDfx5PIltJ8iTUUXx5AP/Oz+MrNIeavUoRKCB9gJaMwT5/xp5DRHQgNC Rtxbh4jryyw2jFYuYjFCazD57u+zr/ETHmtD2DYr+YzHmsU2/O8VnfkeRVhqY2Kyq+jXpilMYsu hRfjlf7EzYW/7d4h+Tg6u5hO1RlwJGTrMcv1C+6LroOu3/DqUilfOat0I+dBVf1dz7SrFl1X3z2 TT8ZkzB++EiJNJu+jp2I66LK2W6BhDIa+B1nwD6Z+1xl0c+Qeh/3tY3UV9QUTjvajIoza+gioYG hXhIytczhyPDZTBcySz0kmZD8duBmggdFdW6nFVUkpwLh5DMMHDvDx0xtlj4NgvY1i1qQBrILkk IDN3+sa37wzCeO8i09xMDkVI07r1TlXhNf4kcSij2T5RCekpFeJ4yPbGSCffheq42Ft2G0dwN5j bMkij5y71f+wLYA== X-Received: by 2002:a17:90b:4c46:b0:3b0:accf:5c66 with SMTP id 98e67ed59e1d1-3b5e0696092mr5545714a91.49.1796504020589; Sat, 05 Dec 2026 12:53:40 -0800 (PST) Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b6166c7c72sm2965998a91.3.2026.12.05.12.53.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 12:53:39 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v5 66/68] kvargs: add range conversion handlers Date: Sat, 5 Dec 2026 12:50:37 -0800 Message-ID: <20261205205221.1131118-67-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261205205221.1131118-1-stephen@networkplumber.org> References: <20260914054912.755403-1-stephen@networkplumber.org> <20261205205221.1131118-1-stephen@networkplumber.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 An argument whose valid range is narrower than the type it is stored in cannot use the typed handlers, so drivers write a handler of their own which calls rte_kvargs_to_uint() and stores the result. There are about 25 of these. They do not report the same way. Of the call sites, 22 reject a bad value with no message at all, so nothing tells the user what the range was. Those which do log one each word it differently: net/af_xdp: "Invalid busy_budget, must be >= 0 and <= %u" dma/skeleton: "Invalid %s, must be 0..%u" net/bnxt: "Invalid parameter passed to rep_q_r2f devargs." Add handlers which take the bounds from the caller: rte_kvargs_handle_urange rte_kvargs_handle_irange A handler has only one opaque pointer, so the bounds are passed and the result returned through a structure. As the typed handlers, the result is written only on success, so seeding it with the default leaves it alone when the key is absent and when the value is rejected. A value outside the range is now reported as one which does not fit a whole type already is, naming the key and the bounds. kvargs_store_uint() grows a minimum to go with the maximum it already had; the typed handlers pass zero. Signed-off-by: Stephen Hemminger --- app/test/test_kvargs.c | 43 ++++++++++++++++++ doc/guides/prog_guide/devargs.rst | 13 ++++++ doc/guides/rel_notes/release_27_03.rst | 1 + lib/kvargs/rte_kvargs.c | 62 +++++++++++++++++++++----- lib/kvargs/rte_kvargs.h | 57 +++++++++++++++++++++++ 5 files changed, 165 insertions(+), 11 deletions(-) diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c index 6e84ebf35c..12e1376e86 100644 --- a/app/test/test_kvargs.c +++ b/app/test/test_kvargs.c @@ -619,6 +619,48 @@ test_kvargs_to_range(void) return TEST_SUCCESS; } +static int +test_handle_range(void) +{ + struct rte_kvargs_urange u = { .min = 1, .max = 10, .val = 7 }; + struct rte_kvargs_irange s = { .min = -10, .max = 10, .val = -7 }; + struct rte_kvargs *kvlist; + + TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_urange, "10", &u), + "10 rejected in [1,10]"); + TEST_ASSERT_EQUAL(u.val, 10U, "wrong value"); + + /* Out of range and malformed values leave the result alone. */ + TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_urange, "11", &u), + "11 accepted in [1,10]"); + TEST_ASSERT_EQUAL(u.val, 10U, "target clobbered above the range"); + TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_urange, "0", &u), + "0 accepted in [1,10]"); + TEST_ASSERT_EQUAL(u.val, 10U, "target clobbered below the range"); + TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_urange, "x", &u), + "\"x\" accepted"); + TEST_ASSERT_EQUAL(u.val, 10U, "target clobbered on a bad value"); + + TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_irange, "-10", &s), + "-10 rejected in [-10,10]"); + TEST_ASSERT_EQUAL(s.val, -10, "wrong value"); + TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_irange, "-11", &s), + "-11 accepted in [-10,10]"); + TEST_ASSERT_EQUAL(s.val, -10, "target clobbered below the range"); + + /* A key which is not present leaves the seeded default alone. */ + u.val = 7; + kvlist = rte_kvargs_parse("other=1", NULL); + TEST_ASSERT_NOT_NULL(kvlist, "parse failed"); + TEST_ASSERT_SUCCESS(rte_kvargs_process(kvlist, "k", + rte_kvargs_handle_urange, &u), + "processing an absent key failed"); + rte_kvargs_free(kvlist); + TEST_ASSERT_EQUAL(u.val, 7U, "default lost when the key is absent"); + + return TEST_SUCCESS; +} + static struct unit_test_suite kvargs_test_suite = { .suite_name = "Kvargs Unit Test Suite", .setup = NULL, @@ -651,6 +693,7 @@ static struct unit_test_suite kvargs_test_suite = { TEST_CASE(test_handle_bool), TEST_CASE(test_handle_socket_id), TEST_CASE(test_kvargs_to_range), + TEST_CASE(test_handle_range), TEST_CASES_END() /**< NULL terminate unit test array */ } }; diff --git a/doc/guides/prog_guide/devargs.rst b/doc/guides/prog_guide/devargs.rst index 50ef501b99..d3cf3356b8 100644 --- a/doc/guides/prog_guide/devargs.rst +++ b/doc/guides/prog_guide/devargs.rst @@ -271,6 +271,19 @@ shell variable expands to. An argument naming a NUMA socket uses ``rte_kvargs_handle_socket_id()``, which accepts ``SOCKET_ID_ANY`` through ``RTE_MAX_NUMA_NODES - 1``. +When the valid range is narrower than the type the value is stored in, +``rte_kvargs_handle_urange()`` and ``rte_kvargs_handle_irange()`` take the +bounds from the caller, passed in and returned through a structure: + +.. code-block:: c + + struct rte_kvargs_urange budget = { .min = 0, .max = 512, .val = 64 }; + + rte_kvargs_process(kvlist, "busy_budget", rte_kvargs_handle_urange, &budget); + +All of these handlers write the target only on success, so a default set +before the call survives both a missing key and a rejected value. + For Ethernet devices, use ``rte_eth_devargs_parse()`` to parse standard Ethernet arguments like representors: diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index de24e12334..43b175d0a0 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -65,6 +65,7 @@ New Features * Added handlers storing into the integer types and ``bool``. * Added a handler for a NUMA socket id. * Added handlers for a hexadecimal mask, with an optional ``0x``. + * Added handlers taking the valid range from the caller. Removed Items diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c index 150fc5f76f..ae9717e275 100644 --- a/lib/kvargs/rte_kvargs.c +++ b/lib/kvargs/rte_kvargs.c @@ -554,17 +554,19 @@ rte_kvargs_to_hex(const char *value, uint64_t max, uint64_t *result) */ static int kvargs_store_uint(const char *key, const char *value, void *opaque, - uint64_t max, uint64_t *val) + uint64_t min, uint64_t max, uint64_t *val) { int ret; if (opaque == NULL) return -EINVAL; - ret = rte_kvargs_to_uint(value, 0, max, val); + ret = rte_kvargs_to_uint(value, min, max, val); if (ret < 0) - KVARGS_LOG(ERR, "invalid value \"%s\" for key \"%s\", expected 0..%" PRIu64, - value != NULL ? value : "", key != NULL ? key : "", max); + KVARGS_LOG(ERR, + "invalid value \"%s\" for key \"%s\", expected %" PRIu64 "..%" PRIu64, + value != NULL ? value : "", key != NULL ? key : "", + min, max); return ret; } @@ -595,7 +597,7 @@ rte_kvargs_handle_u8(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, UINT8_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, UINT8_MAX, &val); if (ret == 0) *(uint8_t *)opaque = (uint8_t)val; @@ -609,7 +611,7 @@ rte_kvargs_handle_u16(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, UINT16_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, UINT16_MAX, &val); if (ret == 0) *(uint16_t *)opaque = (uint16_t)val; @@ -623,7 +625,7 @@ rte_kvargs_handle_u32(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, UINT32_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, UINT32_MAX, &val); if (ret == 0) *(uint32_t *)opaque = (uint32_t)val; @@ -637,7 +639,7 @@ rte_kvargs_handle_u64(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, UINT64_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, UINT64_MAX, &val); if (ret == 0) *(uint64_t *)opaque = (uint64_t)val; @@ -651,7 +653,7 @@ rte_kvargs_handle_uint(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, UINT_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, UINT_MAX, &val); if (ret == 0) *(unsigned int *)opaque = (unsigned int)val; @@ -665,7 +667,7 @@ rte_kvargs_handle_size(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, SIZE_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, SIZE_MAX, &val); if (ret == 0) *(size_t *)opaque = (size_t)val; @@ -763,7 +765,7 @@ rte_kvargs_handle_ulong(const char *key, const char *value, void *opaque) uint64_t val; int ret; - ret = kvargs_store_uint(key, value, opaque, ULONG_MAX, &val); + ret = kvargs_store_uint(key, value, opaque, 0, ULONG_MAX, &val); if (ret == 0) *(unsigned long *)opaque = (unsigned long)val; @@ -873,3 +875,41 @@ rte_kvargs_handle_socket_id(const char *key, const char *value, void *opaque) return ret; } + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_handle_urange, 27.03) +int +rte_kvargs_handle_urange(const char *key, const char *value, void *opaque) +{ + struct rte_kvargs_urange *range = opaque; + uint64_t val; + int ret; + + if (range == NULL) + return -EINVAL; + + ret = kvargs_store_uint(key, value, opaque, range->min, range->max, + &val); + if (ret == 0) + range->val = val; + + return ret; +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_handle_irange, 27.03) +int +rte_kvargs_handle_irange(const char *key, const char *value, void *opaque) +{ + struct rte_kvargs_irange *range = opaque; + int64_t val; + int ret; + + if (range == NULL) + return -EINVAL; + + ret = kvargs_store_int(key, value, opaque, range->min, range->max, + &val); + if (ret == 0) + range->val = val; + + return ret; +} diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h index fe18841709..ac79640085 100644 --- a/lib/kvargs/rte_kvargs.h +++ b/lib/kvargs/rte_kvargs.h @@ -487,6 +487,63 @@ int rte_kvargs_to_int(const char *value, int64_t min, int64_t max, __rte_experimental int rte_kvargs_to_hex(const char *value, uint64_t max, uint64_t *result); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Range and result for rte_kvargs_handle_urange(). + */ +struct rte_kvargs_urange { + uint64_t min; /**< Smallest acceptable value, inclusive. */ + uint64_t max; /**< Largest acceptable value, inclusive. */ + uint64_t val; /**< The result, written only on success. */ +}; + +/** Range and result for rte_kvargs_handle_irange(). */ +struct rte_kvargs_irange { + int64_t min; /**< Smallest acceptable value, inclusive. */ + int64_t max; /**< Largest acceptable value, inclusive. */ + int64_t val; /**< The result, written only on success. */ +}; + +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Convert a key/value pair to an unsigned integer in a range. + * + * As rte_kvargs_handle_u8(), except that the bounds are given by the + * caller rather than by the target type. This is for an argument whose + * valid range is narrower than the type it is stored in. + * + * The bounds are passed and the result returned through the same + * structure, since a handler has only one opaque pointer. Seed ``val`` + * with the default: it is left alone when the key is absent and when + * the value is rejected. + * + * @param key + * The key, used for error reporting only. May be NULL. + * @param value + * The value to convert. + * @param opaque + * Pointer to a ``struct rte_kvargs_urange`` holding the range. On + * success its ``val`` is set, on error it is left unmodified. + * + * @return + * - 0 on success. + * - -EINVAL if the value is missing or malformed, or if @p opaque is NULL. + * - -ERANGE if the value is outside the range. + */ +__rte_experimental +int rte_kvargs_handle_urange(const char *key, const char *value, void *opaque); + +/** + * Convert a value to a signed integer in a range, taking a + * ``struct rte_kvargs_irange``. See rte_kvargs_handle_urange(). + */ +__rte_experimental +int rte_kvargs_handle_irange(const char *key, const char *value, void *opaque); + #ifdef __cplusplus } #endif -- 2.53.0