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 CEEEE606A322 for ; Sat, 5 Dec 2026 06:27:31 +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=c3ZrjNaB Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E2AE340E4F; Sat, 5 Dec 2026 07:26:07 +0100 (CET) Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by mails.dpdk.org (Postfix) with ESMTPS id 8A33340E38 for ; Sat, 5 Dec 2026 07:26:04 +0100 (CET) Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2f01f296937so12714455ad.3 for ; Fri, 04 Dec 2026 22:26:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796451963; x=1797056763; 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=BLDu6biiMqS19Fs3qjSZ0s43OPYG/pq/9f4TCfb9uLc=; b=c3ZrjNaB9HXAKyOih2PMafv/lRw84IblS3SWFIyIutDl0+UhKWBA+Sucq+0QvYykQc +aEdZ08AO8tSu+yHBp0Fq/+oGZ70QOYp6qgSCs9YlE+7ztJrCIv4/2G607ydnDYpfJn5 1a81y6tebW54+ffEMSDvW5h52qQJZ/asyWgwF7QVxaBvQ42D3GXokPhCM+zw1TND/RyS r1hbJJq0GzgD+GELzaT3rwg6KAqWnPp6rAFP3L+yHkNlnkfiK+3UoOLVen7XoW5GXLJs wODucVBgqc7Pec0HKrYnwCrCqhWZDTNV2EFd1Wj+YcuPquvInyuG1wIoZBpyw1cAzmjv ei3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796451963; x=1797056763; 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=BLDu6biiMqS19Fs3qjSZ0s43OPYG/pq/9f4TCfb9uLc=; b=XNQgdZLT4LFyExjxkcVPi7tYfUr6Jb0qWWOG7MsXEnT2NcGiyu4XoPMoIfj5mQvYhc DRH3i4l8mLhgILw1UFfSvbtRmkSnkHa9dwluFksR3+9jUf/bjpIvhWPUkLSBrGwzhVRZ tpVD9wQNKP8x6suJXvISIS6eAeb+ljlLWM4pmxA3kDfiPn4/kx1jxadPOqb7/g0IUPaQ Df9m+Boy9Kc05Buzgx5qNxzXoWMNxWZGB9I8XiiSzBG+KrS8vMDsE6hCHUJyJtnDYiO6 QK//LCQJSBGnyWuepqUCD++X4j3Q/OZxqK76tp9idgKyrVFPW84wHWHpvPxK6uBWN0Tl 1kug== X-Gm-Message-State: AB4q+uG2D2y5eQra0ZjKr2ps9Lk1KgFQ7aQ3kJlVM/spFkDy4tNTNTLY sU6G8IbyTv707et5rRejzHBwWdaqfKwoNLjqIL/kHrOzYWPrAS8NipqHKOEVpW3s247fzrGBL0s xeyusGME= X-Gm-Gg: AZFdTZce/mVNdapdbiAtPVTbhL4p6ddts6eBCL6CF7RaECiIIRzS3LAmMtYvqwR42KV QnOpBF4KuSCCYuqm1yxH2S9VnnQ9ygi2U/Utg+3+F3VLTwXmLruqjuxyrd47OTkKYIAmQaqRZlz IvxhcxO/PAZkR9WMhOJ4kI+X88LJq7KrAgsmVpt262SPavjNtUgM+cbD47TZi5qukEibMzvx91D J2Od9f3vhzbFA2b/LXKg2YTU0H4Ah+KaiRC61iIfSxRveTOESYnPsFSE/VDAUmKYiDoG/16f7Xk R/gzsc2q0Jq/glsTCFKbWwJDzV32MIwr6lrYj5fW0X2ibRGBHEbYZaiF6Z8YGXVszlkw5zTN0da tuThh8jtI6Vne6JlTIV6NRgpEMY0aU7DOEpKsC99oz2YEhbUYzYb/8n9KDmx9UspZRaCmTCTxks JgCcWXozCTwCXQL6WrFGVr1CxrWwPKOs8jF8MeqXH9KFOqwEBtb30A9HyxN4iXEmW0/QY7bEJLt 8wUJcExr3s0TnR5hhXSob3sya8/Ze12dAbGeTFAzgk6C98QjHV1kGyTIb49ZRw0DbDexwHoHCpB sndoVU/lPvT/z0f0u1JEYSllxUvVJhuiQjPbteoXX9Qd2fha3E6NlDCTqlCqydbIlKZG X-Received: by 2002:a17:90b:1b12:b0:3b5:728d:2d27 with SMTP id 98e67ed59e1d1-3b5e06844bcmr3997391a91.38.1796451962795; Fri, 04 Dec 2026 22:26:02 -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.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 22:26:02 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Chengwen Feng Subject: [PATCH v4 11/68] kvargs: add hexadecimal conversion helpers Date: Fri, 4 Dec 2026 22:19:48 -0800 Message-ID: <20261205062551.19553-12-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 Several device arguments are documented as a bare hexadecimal mask, with no 0x prefix. Each place the driver open codes strtoull() with base 16, and each gets the validation wrong in a one way or another. Introduce a new helper to handle these type of args. Add rte_kvargs_handle_hex32() and rte_kvargs_handle_hex64(). A sign is rejected, as it is for the unsigned handlers, so a mask cannot be given as -1. Only the 32 and 64 bit widths are added, since those are the only ones any caller needs. Signed-off-by: Stephen Hemminger Acked-by: Chengwen Feng --- app/test/test_kvargs.c | 45 +++++++++ doc/guides/rel_notes/release_27_03.rst | 1 + lib/kvargs/rte_kvargs.c | 131 ++++++++++++++++++++++--- lib/kvargs/rte_kvargs.h | 44 +++++++++ 4 files changed, 208 insertions(+), 13 deletions(-) diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c index f05c7918e9..6e84ebf35c 100644 --- a/app/test/test_kvargs.c +++ b/app/test/test_kvargs.c @@ -475,6 +475,50 @@ test_handle_signed(void) return TEST_SUCCESS; } +static int +test_handle_hex(void) +{ + uint64_t u = 0x5a; + + /* The 0x prefix is optional, and a bare value is still hexadecimal. */ + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "0xff", 0xff); + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "0XFF", 0xff); + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "ff", 0xff); + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "FF", 0xff); + /* "10" is sixteen here, not ten. */ + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "10", 0x10); + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "0", 0); + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "00110F10", 0x00110f10); + CHECK_GOOD(rte_kvargs_handle_hex32, uint32_t, "ffffffff", 0xffffffff); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "100000000"); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, ""); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "-1"); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "0x"); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "0x0x10"); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "1g"); + CHECK_BAD(rte_kvargs_handle_hex32, uint32_t, "0x1p"); + + CHECK_GOOD(rte_kvargs_handle_hex64, uint64_t, "0xF", 0xf); + CHECK_GOOD(rte_kvargs_handle_hex64, uint64_t, "F", 0xf); + CHECK_GOOD(rte_kvargs_handle_hex64, uint64_t, "ffffffffffffffff", + UINT64_MAX); + CHECK_BAD(rte_kvargs_handle_hex64, uint64_t, "10000000000000000"); + CHECK_BAD(rte_kvargs_handle_hex64, uint64_t, "-1"); + + /* The underlying conversion, with a mask narrower than the type. */ + TEST_ASSERT_SUCCESS(rte_kvargs_to_hex("fff", 0xfff, &u), "fff in 0..fff"); + TEST_ASSERT_EQUAL(u, 0xfffU, "wrong value"); + TEST_ASSERT_EQUAL(rte_kvargs_to_hex("1000", 0xfff, &u), -ERANGE, + "1000 should be out of 0..fff"); + TEST_ASSERT_EQUAL(u, 0xfffU, "target clobbered on range error"); + TEST_ASSERT_EQUAL(rte_kvargs_to_hex(NULL, 0xfff, &u), -EINVAL, + "NULL should be rejected"); + TEST_ASSERT_EQUAL(rte_kvargs_to_hex("f", 0xfff, NULL), -EINVAL, + "a NULL result should be rejected"); + + return TEST_SUCCESS; +} + static int test_handle_bool(void) { @@ -603,6 +647,7 @@ static struct unit_test_suite kvargs_test_suite = { TEST_CASE(test_invalid_kvargs), TEST_CASE(test_handle_unsigned), TEST_CASE(test_handle_signed), + TEST_CASE(test_handle_hex), TEST_CASE(test_handle_bool), TEST_CASE(test_handle_socket_id), TEST_CASE(test_kvargs_to_range), diff --git a/doc/guides/rel_notes/release_27_03.rst b/doc/guides/rel_notes/release_27_03.rst index 31b843f089..de24e12334 100644 --- a/doc/guides/rel_notes/release_27_03.rst +++ b/doc/guides/rel_notes/release_27_03.rst @@ -64,6 +64,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``. Removed Items diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c index e8d35bb869..150fc5f76f 100644 --- a/lib/kvargs/rte_kvargs.c +++ b/lib/kvargs/rte_kvargs.c @@ -323,6 +323,35 @@ rte_kvargs_parse_delim(const char *args, const char * const valid_keys[], return kvlist; } +/* + * Skip over a "0x" prefix if there is one. + * + * Returns true if a prefix was consumed, and advances *str past it. Only one + * prefix is ever consumed: a second one is left in place so that the caller + * rejects it, since strtoull() would otherwise strip it itself and read + * "0x0x10" as sixteen rather than as the garbage it is. + */ +static bool +kvargs_skip_hex_prefix(const char **str) +{ + const char *s = *str; + + if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && + isxdigit((unsigned char)s[2])) { + *str = s + 2; + return true; + } + + return false; +} + +/* Tell whether a "0x" prefix is present, without consuming it. */ +static bool +kvargs_has_hex_prefix(const char *str) +{ + return str[0] == '0' && (str[1] == 'x' || str[1] == 'X'); +} + /* * Determine the base of a numeric value and skip over its prefix. * @@ -331,24 +360,15 @@ rte_kvargs_parse_delim(const char *args, const char * const valid_keys[], * has been a recurring source of surprise. * * Returns the base, and advances *str past the "0x" prefix if there is one. - * Returns 0 if what follows the prefix is a second one: strtoull() would - * strip that itself, making "0x0x10" sixteen rather than the garbage it is. + * Returns 0 if what follows the prefix is a second one, which is not a number. */ static int kvargs_get_base(const char **str) { - const char *s = *str; - - if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && - isxdigit((unsigned char)s[2])) { - s += 2; - if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) - return 0; - *str = s; - return 16; - } + if (!kvargs_skip_hex_prefix(str)) + return 10; - return 10; + return kvargs_has_hex_prefix(*str) ? 0 : 16; } /* Skip trailing white space, and tell whether anything else is left. */ @@ -487,6 +507,46 @@ rte_kvargs_to_int(const char *value, int64_t min, int64_t max, int64_t *result) return 0; } +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_to_hex, 27.03) +int +rte_kvargs_to_hex(const char *value, uint64_t max, uint64_t *result) +{ + const char *str = value; + unsigned long long val; + char *endptr; + + if (str == NULL || result == NULL) + return -EINVAL; + + /* A mask has no sign; "-1" must not wrap around to UINT64_MAX. */ + if (kvargs_get_sign(&str)) + return -EINVAL; + + /* The 0x prefix is optional here, but still accepted. */ + if (kvargs_skip_hex_prefix(&str) && kvargs_has_hex_prefix(str)) + return -EINVAL; /* doubled 0x prefix */ + + if (!isxdigit((unsigned char)*str)) + return -EINVAL; + + errno = 0; + val = strtoull(str, &endptr, 16); + if (endptr == str) + return -EINVAL; + if (errno == ERANGE) + return -ERANGE; + if (errno != 0) + return -EINVAL; + if (!kvargs_at_end(endptr)) + return -EINVAL; /* trailing garbage */ + + if (val > max) + return -ERANGE; + + *result = val; + return 0; +} + /* * The typed handlers below share this shape: convert with a range matching * the target type, then store. The target is written only on success, so a @@ -710,6 +770,51 @@ rte_kvargs_handle_ulong(const char *key, const char *value, void *opaque) return ret; } +static int +kvargs_store_hex(const char *key, const char *value, void *opaque, + uint64_t max, uint64_t *val) +{ + int ret; + + if (opaque == NULL) + return -EINVAL; + + ret = rte_kvargs_to_hex(value, max, val); + if (ret < 0) + KVARGS_LOG(ERR, "invalid value \"%s\" for key \"%s\", expected 0..%" PRIx64 " in hex", + value != NULL ? value : "", key != NULL ? key : "", max); + + return ret; +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_handle_hex32, 27.03) +int +rte_kvargs_handle_hex32(const char *key, const char *value, void *opaque) +{ + uint64_t val; + int ret; + + ret = kvargs_store_hex(key, value, opaque, UINT32_MAX, &val); + if (ret == 0) + *(uint32_t *)opaque = (uint32_t)val; + + return ret; +} + +RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_handle_hex64, 27.03) +int +rte_kvargs_handle_hex64(const char *key, const char *value, void *opaque) +{ + uint64_t val; + int ret; + + ret = kvargs_store_hex(key, value, opaque, UINT64_MAX, &val); + if (ret == 0) + *(uint64_t *)opaque = val; + + return ret; +} + static const char * const kvargs_true[] = { "1", "y", "yes", "on", "true" }; static const char * const kvargs_false[] = { "0", "n", "no", "off", "false" }; diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h index acc15607bc..fe18841709 100644 --- a/lib/kvargs/rte_kvargs.h +++ b/lib/kvargs/rte_kvargs.h @@ -330,6 +330,24 @@ int rte_kvargs_handle_ulong(const char *key, const char *value, void *opaque); __rte_experimental int rte_kvargs_handle_size(const char *key, const char *value, void *opaque); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Convert a bit mask to uint32_t. + * + * As rte_kvargs_handle_u32(), except that the value is always read as + * hexadecimal, with or without a ``0x`` prefix, so ``10`` is sixteen. This + * is for arguments documented as a bare hexadecimal mask; use + * rte_kvargs_handle_u32() for a count or a size. + */ +__rte_experimental +int rte_kvargs_handle_hex32(const char *key, const char *value, void *opaque); + +/** Convert a hexadecimal value to uint64_t. See rte_kvargs_handle_hex32(). */ +__rte_experimental +int rte_kvargs_handle_hex64(const char *key, const char *value, void *opaque); + /** * @warning * @b EXPERIMENTAL: this API may change without prior notice. @@ -443,6 +461,32 @@ __rte_experimental int rte_kvargs_to_int(const char *value, int64_t min, int64_t max, int64_t *result); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Convert a hexadecimal string to an unsigned integer, checking it against + * a maximum. + * + * This is the conversion underlying rte_kvargs_handle_hex32(), and is the + * hexadecimal counterpart of rte_kvargs_to_uint(). The minimum is always + * zero, since a negative value is rejected rather than wrapped around. + * + * @param value + * The string to convert. Must be non-NULL and non-empty. + * @param max + * Largest acceptable value, inclusive. + * @param result + * Where to store the converted value. Left unmodified on error. + * + * @return + * - 0 on success. + * - -EINVAL if the value is missing or malformed, or if @p result is NULL. + * - -ERANGE if the value is greater than @p max. + */ +__rte_experimental +int rte_kvargs_to_hex(const char *value, uint64_t max, uint64_t *result); + #ifdef __cplusplus } #endif -- 2.53.0