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 33FBD606A322 for ; Sat, 5 Dec 2026 20:54:06 +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=bEaldxD6 Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id B0288410D2; Sat, 5 Dec 2026 21:52:42 +0100 (CET) Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by mails.dpdk.org (Postfix) with ESMTPS id B518940EAB for ; Sat, 5 Dec 2026 21:52:39 +0100 (CET) Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-3b53db3d0a5so995686a91.0 for ; Sat, 05 Dec 2026 12:52:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20260707.gappssmtp.com; s=20260707; t=1796503958; x=1797108758; 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=bEaldxD6WApjVYD9b6WF2oIbHWwpFzq97c1uQy+ST5RvuVj3SjpcAnfkcY++cLRcZ/ SBtYlonfCEY6Mdi8R2BShJYr9g1rrCj/6Wd2W2RPLmjw62YtRou+372qI+l5oCHjQYiD D0oRyOj49UT8SMetbHl99gekkOY/RZoX7Uv9aZUVfQ5qKWfvG9kxgL6VdScl5TUhaOKD H+FpTyqIOJGp7s9cz8vMHVI0y3j2RMcANm1uS0z1/kdPcR6NtXsQOLhkOuTbyy+dQ8VT YSYxQnFd0Xi+OYthcSLD24AEu+eWK2gwViQCysD9fH0M0w9PtSqIenZExwybKCymBFh9 YxTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796503958; x=1797108758; 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=OLQ0qBURyNc6ftMffrRKQHLyHgV3sZaFeARZdlwNjEGEPwignl68VwJjJpbXednZ2Y G7JRbr3/1AymDFjvOOpun49O7eHJfVfJ5kIu2wqhUnl8MIqvNwq5mP2OSkEIqh72NtxM 9snJLXtwxE2XkOWAFtv1FslbUv1PQFKkibumCb1V59h008+uVTKClH0ZIA2xznI7O2SH fh+CzT1XRYRwN4TOIIsScrZTPn82HHKHvVM7aeD+sqWFrVPnBvuU429xmr5FskeToXUA lkqSCj/2QNvbi1XszqcO1TxYrVqlvt+jygrAn6VnbMEH6phfgN7lKNrQcICNybVqa9Er q45A== X-Gm-Message-State: AB4q+uFOARDxl9+byKzqkuLRXSI0GF1/ghjyXHICNw2fjZcEHqOffFDc BDxk+KK/FSfsAxs3Gbl1iP4YMB4UM8YSnn0pE5/Sdht58wD8h3XP/NZ0MfSeP4AFXeR9ZfkML9E BCrWuvZ4= X-Gm-Gg: AZFdTZdFgDKKszBjKiKc3dEbBKqnX0/yI8c6zK0qsEXmoDZm2EY3Qpsl8OKYGSTccBy IwFpEVpLzVbR+d4f3HrGDciGoC7CqXL8Bv8CAXp/Ar3NGyltPNrKrEKym93xbiHM8amVclAQFnd z7APc8u8jrkatofEg6Az2ClkT7iqgO/ILFvzRCodAu0CffO2UCxVVUpJF6beZWbeMkqlgjrEHla KDB1o/yBGXoZx+uapokBk49XBz01uXVAnm4ZGgj7tkpyKBvfWj5ilLPVBCVxYyOKSkOYPTWsCsK P9+wqSgN8CVxOQvN+vpG0EixnG1CrsWR8U2bkqRrN0awrrK51Vl7VEXBv7qbo6fwQQB3JhiGHKV kdWpsUrHsdMriTb1t0UwBvnBmHjgvWTgGG8YpXEpi2XXchxs5xOhOr2OUwRY6xTQ5rTpVLz2BzU hbNOJcBl32bXzIiV6g5Em4HGrUJJWa8ctCGdPBmmNF8xGkzV9kbIUFsODqTkSxefzrVEKKBOEkL vHRdpLpGhX1JFixf7hA+UkkuETBKr56fO/ZtkRHuejvJqoIZBpzW5QWlzwxN4nC0bfeAk9Yeymf pHAQr7yS3pni0RRpp9gsBOPKiq6k3TdD+NsXSvUyOQ/+2QlFwebADhk3PlIvbMAG8Hr5UcxYoJ9 9hMubQct0IHJ3Sg== X-Received: by 2002:a17:90b:2804:b0:3b4:1877:8d35 with SMTP id 98e67ed59e1d1-3b5e063d909mr5814179a91.52.1796503958111; Sat, 05 Dec 2026 12:52:38 -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.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 12:52:37 -0800 (PST) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Chengwen Feng Subject: [PATCH v5 11/68] kvargs: add hexadecimal conversion helpers Date: Sat, 5 Dec 2026 12:49:42 -0800 Message-ID: <20261205205221.1131118-12-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 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