Device arguments naming a NUMA socket are common, and every driver that
takes one open codes the same range check against RTE_MAX_NUMA_NODES.
They do not agree on what the range is: dlb2 accepts RTE_MAX_NUMA_NODES
itself, and turbo_sw and bbdev_null check only the upper bound, so a
negative socket id is taken as valid.

Add a handler for these: rte_kvargs_handle_socket_id()
It stores through an int and accepts -1,
which is SOCKET_ID_ANY, through RTE_MAX_NUMA_NODES - 1.

SOCKET_ID_ANY is spelled as a literal -1 rather than included from
rte_memory.h, since kvargs sits below EAL.

Signed-off-by: Stephen Hemminger <[email protected]>
Acked-by: Chengwen Feng <[email protected]>
---
 app/test/test_kvargs.c                 | 31 ++++++++++++++++++++++++++
 doc/guides/prog_guide/devargs.rst      |  5 +++++
 doc/guides/rel_notes/release_27_03.rst |  1 +
 lib/kvargs/rte_kvargs.c                | 18 +++++++++++++++
 lib/kvargs/rte_kvargs.h                | 27 ++++++++++++++++++++++
 5 files changed, 82 insertions(+)

diff --git a/app/test/test_kvargs.c b/app/test/test_kvargs.c
index b74dfd6acb..f05c7918e9 100644
--- a/app/test/test_kvargs.c
+++ b/app/test/test_kvargs.c
@@ -515,6 +515,36 @@ test_handle_bool(void)
        return TEST_SUCCESS;
 }
 
+static int
+test_handle_socket_id(void)
+{
+       char buf[32];
+
+       /* SOCKET_ID_ANY is a valid socket id, anything below it is not. */
+       CHECK_GOOD(rte_kvargs_handle_socket_id, int, "-1", -1);
+       CHECK_GOOD(rte_kvargs_handle_socket_id, int, "0", 0);
+       CHECK_BAD(rte_kvargs_handle_socket_id, int, "-2");
+       CHECK_BAD(rte_kvargs_handle_socket_id, int, "");
+       CHECK_BAD(rte_kvargs_handle_socket_id, int, "1x");
+
+       /* The last valid value, and the first one past it. */
+       {
+               int socket = 0x5a;
+
+               snprintf(buf, sizeof(buf), "%d", RTE_MAX_NUMA_NODES - 1);
+               TEST_ASSERT_SUCCESS(handle_one(rte_kvargs_handle_socket_id, buf,
+                                              &socket), "socket id %s 
rejected", buf);
+               TEST_ASSERT_EQUAL(socket, RTE_MAX_NUMA_NODES - 1, "wrong socket 
id");
+
+               snprintf(buf, sizeof(buf), "%d", RTE_MAX_NUMA_NODES);
+               TEST_ASSERT_FAIL(handle_one(rte_kvargs_handle_socket_id, buf,
+                                           &socket), "socket id %s accepted", 
buf);
+               TEST_ASSERT_EQUAL(socket, RTE_MAX_NUMA_NODES - 1, "target 
clobbered");
+       }
+
+       return TEST_SUCCESS;
+}
+
 static int
 test_kvargs_to_range(void)
 {
@@ -574,6 +604,7 @@ static struct unit_test_suite kvargs_test_suite  = {
                TEST_CASE(test_handle_unsigned),
                TEST_CASE(test_handle_signed),
                TEST_CASE(test_handle_bool),
+               TEST_CASE(test_handle_socket_id),
                TEST_CASE(test_kvargs_to_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 b73ad20cf6..50ef501b99 100644
--- a/doc/guides/prog_guide/devargs.rst
+++ b/doc/guides/prog_guide/devargs.rst
@@ -255,9 +255,11 @@ conversion handlers provided by kvargs, which do the range 
checking:
 
    uint16_t queues = 1;
    bool scalar = false;
+   int socket_id = SOCKET_ID_ANY;
 
    rte_kvargs_process(kvlist, "queues", rte_kvargs_handle_u16, &queues);
    rte_kvargs_process_opt(kvlist, "scalar", rte_kvargs_handle_bool, &scalar);
+   rte_kvargs_process(kvlist, "socket_id", rte_kvargs_handle_socket_id, 
&socket_id);
 
 Boolean arguments accept ``1``, ``y``, ``yes``, ``on`` and ``true``,
 case insensitively, and their negative counterparts. A bare ``scalar``
@@ -266,6 +268,9 @@ rte_kvargs_process_opt(); rte_kvargs_process() rejects a 
missing value
 first. An empty ``scalar=`` is rejected, since that is what an unset
 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``.
+
 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 f98efba364..31b843f089 100644
--- a/doc/guides/rel_notes/release_27_03.rst
+++ b/doc/guides/rel_notes/release_27_03.rst
@@ -63,6 +63,7 @@ New Features
   See ``rte_kvargs.h`` for the full list.
 
   * Added handlers storing into the integer types and ``bool``.
+  * Added a handler for a NUMA socket id.
 
 
 Removed Items
diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c
index 46241ba944..e8d35bb869 100644
--- a/lib/kvargs/rte_kvargs.c
+++ b/lib/kvargs/rte_kvargs.c
@@ -750,3 +750,21 @@ rte_kvargs_handle_bool(const char *key, const char *value, 
void *opaque)
 
        return -EINVAL;
 }
+
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_kvargs_handle_socket_id, 27.03)
+int
+rte_kvargs_handle_socket_id(const char *key, const char *value, void *opaque)
+{
+       int64_t val;
+       int ret;
+
+       /* SOCKET_ID_ANY, which is -1, is a valid socket id. It is spelled
+        * out here rather than included from EAL, which kvargs sits below.
+        */
+       ret = kvargs_store_int(key, value, opaque, -1,
+                              RTE_MAX_NUMA_NODES - 1, &val);
+       if (ret == 0)
+               *(int *)opaque = (int)val;
+
+       return ret;
+}
diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h
index 118cf3c79b..acc15607bc 100644
--- a/lib/kvargs/rte_kvargs.h
+++ b/lib/kvargs/rte_kvargs.h
@@ -359,6 +359,33 @@ int rte_kvargs_handle_size(const char *key, const char 
*value, void *opaque);
 __rte_experimental
 int rte_kvargs_handle_bool(const char *key, const char *value, void *opaque);
 
+/**
+ * @warning
+ * @b EXPERIMENTAL: this API may change without prior notice.
+ *
+ * Convert a key/value pair to a NUMA socket id.
+ *
+ * Accepts -1, which is SOCKET_ID_ANY, through RTE_MAX_NUMA_NODES - 1.
+ * The bound is the compile time maximum rather than the set of sockets
+ * present on the running system, matching what drivers checked before
+ * this helper existed.
+ *
+ * @param key
+ *   The key, used for error reporting only. May be NULL.
+ * @param value
+ *   The value to convert.
+ * @param opaque
+ *   Pointer to an ``int`` to store the result into. On error it is left
+ *   unmodified.
+ *
+ * @return
+ *   - 0 on success.
+ *   - -EINVAL if the value is malformed, or if @p opaque is NULL.
+ *   - -ERANGE if the value is not a valid socket id.
+ */
+__rte_experimental
+int rte_kvargs_handle_socket_id(const char *key, const char *value, void 
*opaque);
+
 /**
  * @warning
  * @b EXPERIMENTAL: this API may change without prior notice.
-- 
2.53.0

Reply via email to