[PATCH 2/4] app/testpmd: consolidate port socket ID computation

Stephen Hemminger stephen at networkplumber.org
Thu Jul 2 18:10:12 CEST 2026


The socket id of a port was computed in three places with
diverging copies of the same policy:

- init_config() and init_fwd_streams() duplicated the full policy
  (--port-numa-config, device socket id, first available socket,
  --socket-num);
- setup_attached_port() used only the device socket id and ignored
  --port-numa-config, --no-numa and --socket-num, so a hot-attached
  port could be keyed to a socket where no mbuf pool exists.

Move the policy into a single helper, port_socket_id(), and use it
in all three places.

Signed-off-by: Stephen Hemminger <stephen at networkplumber.org>
---
 app/test-pmd/testpmd.c | 69 +++++++++++++++++-------------------------
 1 file changed, 28 insertions(+), 41 deletions(-)

diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index b5dca03047..04f0e15bee 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -1740,6 +1740,31 @@ uma_socket_id(void)
 	       (unsigned int)SOCKET_ID_ANY : socket_num;
 }
 
+/*
+ * Socket id to use for a port: the socket its mbuf pools are created
+ * on and where its queue resources are placed.
+ */
+static unsigned int
+port_socket_id(portid_t pid)
+{
+	unsigned int socket_id;
+
+	if (!numa_support)
+		return uma_socket_id();
+
+	socket_id = port_numa[pid];
+	if (socket_id == NUMA_NO_CONFIG) {
+		socket_id = rte_eth_dev_socket_id(pid);
+		/*
+		 * If socket_id is invalid,
+		 * set to the first available socket.
+		 */
+		if (check_socket_id(socket_id) < 0)
+			socket_id = socket_ids[0];
+	}
+	return socket_id;
+}
+
 static void
 init_config(void)
 {
@@ -1774,25 +1799,8 @@ init_config(void)
 	}
 
 	RTE_ETH_FOREACH_DEV(pid) {
-		uint32_t socket_id;
-
-		if (numa_support) {
-			socket_id = port_numa[pid];
-			if (port_numa[pid] == NUMA_NO_CONFIG) {
-				socket_id = rte_eth_dev_socket_id(pid);
-
-				/*
-				 * if socket_id is invalid,
-				 * set to the first available socket.
-				 */
-				if (check_socket_id(socket_id) < 0)
-					socket_id = socket_ids[0];
-			}
-		} else {
-			socket_id = uma_socket_id();
-		}
 		/* Apply default TxRx configuration for all ports */
-		init_config_port_offloads(pid, socket_id);
+		init_config_port_offloads(pid, port_socket_id(pid));
 	}
 	/*
 	 * Create pools of mbuf.
@@ -1916,23 +1924,7 @@ init_fwd_streams(void)
 				nb_txq, port->dev_info.max_tx_queues);
 			return -1;
 		}
-		if (numa_support) {
-			if (port_numa[pid] != NUMA_NO_CONFIG)
-				port->socket_id = port_numa[pid];
-			else {
-				port->socket_id = rte_eth_dev_socket_id(pid);
-
-				/*
-				 * if socket_id is invalid,
-				 * set to the first available socket.
-				 */
-				if (check_socket_id(port->socket_id) < 0)
-					port->socket_id = socket_ids[0];
-			}
-		}
-		else {
-			port->socket_id = uma_socket_id();
-		}
+		port->socket_id = port_socket_id(pid);
 	}
 
 	q = RTE_MAX(nb_rxq, nb_txq);
@@ -3604,14 +3596,9 @@ attach_port(char *identifier)
 static void
 setup_attached_port(portid_t pi)
 {
-	unsigned int socket_id;
 	int ret;
 
-	socket_id = (unsigned)rte_eth_dev_socket_id(pi);
-	/* if socket_id is invalid, set to the first available socket. */
-	if (check_socket_id(socket_id) < 0)
-		socket_id = socket_ids[0];
-	reconfig(pi, socket_id);
+	reconfig(pi, port_socket_id(pi));
 	ret = rte_eth_promiscuous_enable(pi);
 	if (ret != 0)
 		fprintf(stderr,
-- 
2.53.0



More information about the dev mailing list