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 3A8BA606A333 for ; Sat, 5 Dec 2026 06:33:51 +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=k1szC3wi Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 412F2427B7; Sat, 5 Dec 2026 07:26:55 +0100 (CET) Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) by mails.dpdk.org (Postfix) with ESMTPS id 4716142DD4 for ; Sat, 5 Dec 2026 07:26:49 +0100 (CET) Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-3b54096b2d0so1167535a91.1 for ; Fri, 04 Dec 2026 22:26:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796452008; x=1797056808; 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=k1szC3wi++LfZwJQ/qr8M6cvfigy5HZbDlp9+vXri25FX00gZN1neWwWzhgw3iBBSN lKV1FWeKU78cUwNe+3nUru4yosznW9gqoefkmFoDz5mjr0l7zCumLDrZ0zf4RWrTa+uh F+Hkivs9+FA5GDo2itmd4TXE9uTT2IkwOMNZIv63J6wLr79UFYJRaXN4fHdoEaEohnSu eVbGULVtHmJFeanC2IYhXCS+tv/1IQyqL0V23nn9As+KgHkgWgrxI3/JWlynDKi3JyU+ VVVXhqGk9duxA5SyZqUewrQT9o0cTaKztmi7CUmp1LxarS3fWZEqhWFfBwG2tsYXCx98 JcHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796452008; x=1797056808; 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=TKcGN7dX0dIXJyQnoosBc41vzSzNJhKhV6awpw20OTCT9c+GWblSo/ZQYXQXy5Dw1x wEHuIH3BCGCS+YQPSYVoemgtDxiZI7+cDDFRD2L41qD5gSaNk0LiPuZizud4mlAUtsqQ Dtedcf3zYKb9pPiOsqnkfPDSFspLPDHsz2JJmfKzGHMHkXZA38McdD9ZxVZ8Nuxb7PlT 2Ru0TFesv1fthfREtp74p7OWpErJp6+9RIFy6kmtRLGI8zv5hA3wvvlZu9qEuEH9lXMS 921NYPlmhh1RF3ruq91TEZkxRgSK4r/UvtEUXRF61fvLcBbsYkQUiDK2N2GemeTB0fJS q1TA== X-Gm-Message-State: AB4q+uGdBobwP3tIDDFFPJf5Sa3jzq/3oclyBTJDhRgBwtMAE02LK+Ob TmAT3j/Kfy14eS5sgMdE44iEo1z3RipfcFej/3+idgXSPt8IGBjw4kLj6q0L1Twhq8cMz+4+6x3 lyU4ohUo= X-Gm-Gg: AZFdTZeLdq+exssU7z4cr9wJe6MHBC6oaefL64/zekB/YQCHE0AiuZm0kAQY2Cuxrnk AXvfbrrE1FhFQiS4XGQD3qKjPQCHrrYrHWazs2pFnuqsaHlyy8pqWE4J3gEvvHwfC500lUqntN5 BGXgvNVy4mRcwC7GAWpsPFTn+dL17kIw++n/6y7APoI+zrnXBprKZ0u9NgydaD/ShOQYlt1TYTp 3rLZWCGFm0RiUHuuZNnGdXl63JUnCQg7Wz5Tw6m8JoK80NLXPSfTRPWms6g5T0GjG9D92Jfs5I0 YnTkEEEQqGrUYiKnBH7NVVzrFoa44cl7FlYXVITdv7wjWt0sjFkgAdc1BY0kxXZSmKfpz/b0Qb9 YI4DGiq2B8BWj4lB3EzSFJdd3hTEbpjyS8iYU4qxMo9W2Ih08/iOxcr5Rp0ANSJZBUbGbgwvLDT J/mYmioeEa4t4ccUOAOv+Two7SvIQo2JCcBC9V+xARKDBQfsLjkYWnRBUtxGLBezYGKyreXiLtZ 1nNlp23svoqCuDNXLJAv1MdrlKBHBetbiFAzoPhTglgetGbSSBlS/RC5CXnL/uoFNNu67WJBwC7 9ng3uwkl3kBt2vqWCDnSW/0D1oRq83qMrsnmDXQsl9UlrfivZ/cU1rx0RzQQvicRzyzkcw== X-Received: by 2002:a17:90b:4a92:b0:3b5:9d6a:ff85 with SMTP id 98e67ed59e1d1-3b5e06aaa98mr3885007a91.60.1796452007673; Fri, 04 Dec 2026 22:26:47 -0800 (PST) Received: from phoenix.lan (204-195-112-43.wavecable.com. [204.195.112.43]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b6168d3e2fsm1162797a91.2.2026.12.04.22.26.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 22:26:47 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v4 66/68] kvargs: add range conversion handlers Date: Fri, 4 Dec 2026 22:20:43 -0800 Message-ID: <20261205062551.19553-67-stephen@networkplumber.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261205062551.19553-1-stephen@networkplumber.org> References: <20260914054912.755403-1-stephen@networkplumber.org> <20261205062551.19553-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