From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id BEB9346003;
	Mon,  6 Jan 2025 16:35:34 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 5B66D406A2;
	Mon,  6 Jan 2025 16:35:34 +0100 (CET)
Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com
 [209.85.218.41]) by mails.dpdk.org (Postfix) with ESMTP id 57BE840687
 for <dev@dpdk.org>; Mon,  6 Jan 2025 16:35:31 +0100 (CET)
Received: by mail-ej1-f41.google.com with SMTP id
 a640c23a62f3a-aa66c1345caso495057866b.3
 for <dev@dpdk.org>; Mon, 06 Jan 2025 07:35:31 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1736177731; x=1736782531; 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;
 bh=Q/RSGhH7r3s+VKSjo4dK7onZjCF7WXvddHnzT3/xvqE=;
 b=a7Yu2s4knQw+1t+WFXSJZlzgALkMaRIjBuqdM4siw7aoyLdC+UN94LfxLvin/usK4Z
 CZXLduiuITH1gKejyut6h6CWqoMANcazY5tVU3rLQHMPlicJWkgfxef9J2AwzcGxorER
 OJuzdVqfHH+U2qM1cUvKGGmWR563B6gEJ0ACiU+vbzzFhMsUPPq4encFwjDIag+XIRIK
 N4mEtUcKSqtvcFgNRpRM6eV0zmdUcBM0rd/gbVQ/wknM5pfmmoR4WBhN1SfwfTOGVX5r
 fcfh5Pi4wlVpo22OeE/RWiqpOlLSUK+/y9gsux7eGzZpIBTEIfIy1aHjJ1TC4Oi++up6
 jiGQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1736177731; x=1736782531;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=Q/RSGhH7r3s+VKSjo4dK7onZjCF7WXvddHnzT3/xvqE=;
 b=ffEi0vheZf4cH9bzrgS4mDlMqdzoDy4wjCX+RdzVpMd0kym6+VFJqbrRvaAVt7Tkgv
 Qt78z8Re7l/fw4xMlEOqW6dc6qu1nrnA2n5lf21kPbpfDC6CIKUNmfrXPBIcesIp7VKf
 o0SOP8l/VgjZ+J1k8lc8LK6cGSq0dCv+oBp/1EWQ2H2plHtJfGvtqgAwj5Nt5hlOeA9O
 7VrsZF8ZbRWKOkunSOuEE7VZJrzCPFjJfG/e1PomvsfhRLWtIxpP5aFKV8WzZBbdUDLU
 2BInz/Yc7u8F/a3Po0RsPF/ZXaOPmBe0jFrzVsMA9M5Ii2k6mxGdolilDvbnsuuc94A5
 /vMg==
X-Forwarded-Encrypted: i=1;
 AJvYcCXtanFm6l/w1iQdRQ4xKNPV5S+1kKXjceVd54g2Zx00gfMzRLfP6KLBSGyYAL/q9l5HbUQ=@dpdk.org
X-Gm-Message-State: AOJu0YxyOZJZjwBgf42CIhVdwe+Z1VEPvuPaSZ6anccFS0pyFbJDf1dk
 alXYJrxdJqJ5rnrgZKHZPsndeifeunIAjjrhWURVdkzwJGR4+FwQ
X-Gm-Gg: ASbGncsHR2vWl71v9yz/4rpu6e9BnEmyzIzN/uRGMEKRVkIlM7wKwzYCC06JWcEuVwA
 A0PdLfpMq62kqTU7jP3ZEAS4QnRbp49Z6uic1Hsy+W4FMf7KLA6J77x3IMGzI91A1J0r3J8Y190
 K1Dj3CFmq4EEP2tGWwxvEzcI4czEkkcWjJz75BxLlgyZyMGRCpxHBGkBp1LJ94Y9Q8gTz7q0SIW
 fo+VFWMQl1QskSjbbnf28AN4MEb0BFDzYHktZbsBzMls/6bFg1IhZHDnvg/fOl+xmuA4PyhK9Yo
 4zypSccpgS/LJdJJp0lA7dZ6eb1//6QjQvEsTA==
X-Google-Smtp-Source: AGHT+IFOi6ZLdugUsIpKBf3sWxdTS0wxwOLgJR30NqfMQlkGyPgqpy34dcgRI9tGDlnnnzheFZJBAw==
X-Received: by 2002:a17:906:c10f:b0:aa6:af4b:7c87 with SMTP id
 a640c23a62f3a-aac33442793mr5847770666b.21.1736177730741; 
 Mon, 06 Jan 2025 07:35:30 -0800 (PST)
Received: from tucornea-pc-virtualbox.buh.is.keysight.com ([213.249.122.236])
 by smtp.gmail.com with ESMTPSA id
 a640c23a62f3a-aaf379e34e4sm1229332966b.121.2025.01.06.07.35.29
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Mon, 06 Jan 2025 07:35:30 -0800 (PST)
From: Tudor Cornea <tudor.cornea@gmail.com>
To: stephen@networkplumber.org
Cc: linville@tuxdriver.com, ferruh.yigit@amd.com,
 andrew.rybchenko@oktetlabs.ru, dev@dpdk.org,
 Tudor Cornea <tudor.cornea@gmail.com>
Subject: [PATCH v2] net/af_packet: allow changing fanout mode
Date: Mon,  6 Jan 2025 17:35:08 +0200
Message-Id: <20250106153508.11262-1-tudor.cornea@gmail.com>
X-Mailer: git-send-email 2.34.1
In-Reply-To: <20241212080442.1628366-1-tudor.cornea@gmail.com>
References: <20241212080442.1628366-1-tudor.cornea@gmail.com>
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 <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

This allows us to control the algorithm used to spread traffic between
sockets, adding more fine grained control. If the user does not
specify a fanout mode, the PMD driver will default to
PACKET_FANOUT_HASH.

Signed-off-by: Tudor Cornea <tudor.cornea@gmail.com>

---
v2:
* Renamed the patch
* Replaced packet_fanout argument with fanout_mode, which allows
more fine grained control
---
 doc/guides/nics/af_packet.rst             |  4 +-
 drivers/net/af_packet/rte_eth_af_packet.c | 92 ++++++++++++++++++++---
 2 files changed, 83 insertions(+), 13 deletions(-)

diff --git a/doc/guides/nics/af_packet.rst b/doc/guides/nics/af_packet.rst
index a343d3a961..3443f95004 100644
--- a/doc/guides/nics/af_packet.rst
+++ b/doc/guides/nics/af_packet.rst
@@ -25,6 +25,8 @@ Some of these, in turn, will be used to configure the PACKET_MMAP settings.
 *   ``qpairs`` - number of Rx and Tx queues (optional, default 1);
 *   ``qdisc_bypass`` - set PACKET_QDISC_BYPASS option in AF_PACKET (optional,
     disabled by default);
+*   ``fanout_mode`` - set fanout algorithm. Possible choices: hash,lb,cpu,rollover,rnd,qm (optional,
+    default hash);
 *   ``blocksz`` - PACKET_MMAP block size (optional, default 4096);
 *   ``framesz`` - PACKET_MMAP frame size (optional, default 2048B; Note: multiple
     of 16B);
@@ -64,7 +66,7 @@ framecnt=512):
 
 .. code-block:: console
 
-    --vdev=eth_af_packet0,iface=tap0,blocksz=4096,framesz=2048,framecnt=512,qpairs=1,qdisc_bypass=0
+    --vdev=eth_af_packet0,iface=tap0,blocksz=4096,framesz=2048,framecnt=512,qpairs=1,qdisc_bypass=0,fanout_mode=hash
 
 Features and Limitations
 ------------------------
diff --git a/drivers/net/af_packet/rte_eth_af_packet.c b/drivers/net/af_packet/rte_eth_af_packet.c
index ceb8d9356a..8449975384 100644
--- a/drivers/net/af_packet/rte_eth_af_packet.c
+++ b/drivers/net/af_packet/rte_eth_af_packet.c
@@ -36,6 +36,7 @@
 #define ETH_AF_PACKET_FRAMESIZE_ARG	"framesz"
 #define ETH_AF_PACKET_FRAMECOUNT_ARG	"framecnt"
 #define ETH_AF_PACKET_QDISC_BYPASS_ARG	"qdisc_bypass"
+#define ETH_AF_PACKET_FANOUT_MODE_ARG	"fanout_mode"
 
 #define DFLT_FRAME_SIZE		(1 << 11)
 #define DFLT_FRAME_COUNT	(1 << 9)
@@ -96,6 +97,7 @@ static const char *valid_arguments[] = {
 	ETH_AF_PACKET_FRAMESIZE_ARG,
 	ETH_AF_PACKET_FRAMECOUNT_ARG,
 	ETH_AF_PACKET_QDISC_BYPASS_ARG,
+	ETH_AF_PACKET_FANOUT_MODE_ARG,
 	NULL
 };
 
@@ -700,6 +702,61 @@ open_packet_iface(const char *key __rte_unused,
 	return 0;
 }
 
+#if defined(PACKET_FANOUT)
+#define PACKET_FANOUT_INVALID -1
+
+static int
+get_fanout_group_id(int if_index)
+{
+	return (getpid() ^ if_index) & 0xffff;
+}
+
+static int
+get_fanout_mode(const char *fanout_mode)
+{
+	int mode = PACKET_FANOUT_FLAG_DEFRAG;
+
+#if defined(PACKET_FANOUT_FLAG_ROLLOVER)
+	mode |= PACKET_FANOUT_FLAG_ROLLOVER;
+#endif
+
+	if (!fanout_mode) {
+		/* Default */
+		mode |= PACKET_FANOUT_HASH;
+	} else if (!strcmp(fanout_mode, "hash")) {
+		mode |= PACKET_FANOUT_HASH;
+	} else if (!strcmp(fanout_mode, "lb")) {
+		mode |= PACKET_FANOUT_LB;
+	} else if (!strcmp(fanout_mode, "cpu")) {
+		mode |= PACKET_FANOUT_CPU;
+	} else if (!strcmp(fanout_mode, "rollover")) {
+		mode |= PACKET_FANOUT_ROLLOVER;
+	} else if (!strcmp(fanout_mode, "rnd")) {
+		mode |= PACKET_FANOUT_RND;
+	} else if (!strcmp(fanout_mode, "qm")) {
+		mode |= PACKET_FANOUT_QM;
+	} else {
+		/* Invalid Fanout Mode */
+		mode = PACKET_FANOUT_INVALID;
+	}
+
+	return mode;
+}
+
+static int
+get_fanout(const char *fanout_mode, int if_index)
+{
+	int group_id = get_fanout_group_id(if_index);
+	int mode = get_fanout_mode(fanout_mode);
+	int fanout = PACKET_FANOUT_INVALID;
+
+	if (mode != PACKET_FANOUT_INVALID)
+		fanout = group_id | (mode << 16);
+
+	return fanout;
+}
+#endif
+
 static int
 rte_pmd_init_internals(struct rte_vdev_device *dev,
                        const int sockfd,
@@ -709,6 +766,7 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
                        unsigned int framesize,
                        unsigned int framecnt,
 		       unsigned int qdisc_bypass,
+		       const char *fanout_mode,
                        struct pmd_internals **internals,
                        struct rte_eth_dev **eth_dev,
                        struct rte_kvargs *kvlist)
@@ -810,11 +868,12 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
 	sockaddr.sll_ifindex = (*internals)->if_index;
 
 #if defined(PACKET_FANOUT)
-	fanout_arg = (getpid() ^ (*internals)->if_index) & 0xffff;
-	fanout_arg |= (PACKET_FANOUT_HASH | PACKET_FANOUT_FLAG_DEFRAG) << 16;
-#if defined(PACKET_FANOUT_FLAG_ROLLOVER)
-	fanout_arg |= PACKET_FANOUT_FLAG_ROLLOVER << 16;
-#endif
+	fanout_arg = get_fanout(fanout_mode, (*internals)->if_index);
+
+	if (fanout_arg == PACKET_FANOUT_INVALID) {
+		PMD_LOG(ERR, "Invalid fanout mode: %s", fanout_mode);
+		goto error;
+	}
 #endif
 
 	for (q = 0; q < nb_queues; q++) {
@@ -927,13 +986,16 @@ rte_pmd_init_internals(struct rte_vdev_device *dev,
 		}
 
 #if defined(PACKET_FANOUT)
-		rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
-				&fanout_arg, sizeof(fanout_arg));
-		if (rc == -1) {
-			PMD_LOG_ERRNO(ERR,
-				"%s: could not set PACKET_FANOUT on AF_PACKET socket for %s",
-				name, pair->value);
-			goto error;
+		if (nb_queues > 1) {
+			rc = setsockopt(qsockfd, SOL_PACKET, PACKET_FANOUT,
+					&fanout_arg, sizeof(fanout_arg));
+			if (rc == -1) {
+				PMD_LOG_ERRNO(ERR,
+					"%s: could not set PACKET_FANOUT "
+					"on AF_PACKET socket for %s",
+					name, pair->value);
+				goto error;
+			}
 		}
 #endif
 	}
@@ -1003,6 +1065,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
 	unsigned int framecount = DFLT_FRAME_COUNT;
 	unsigned int qpairs = 1;
 	unsigned int qdisc_bypass = 1;
+	const char *fanout_mode = NULL;
 
 	/* do some parameter checking */
 	if (*sockfd < 0)
@@ -1065,6 +1128,10 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
 			}
 			continue;
 		}
+		if (strstr(pair->key, ETH_AF_PACKET_FANOUT_MODE_ARG) != NULL) {
+			fanout_mode = pair->value;
+			continue;
+		}
 	}
 
 	if (framesize > blocksize) {
@@ -1091,6 +1158,7 @@ rte_eth_from_packet(struct rte_vdev_device *dev,
 				   blocksize, blockcount,
 				   framesize, framecount,
 				   qdisc_bypass,
+				   fanout_mode,
 				   &internals, &eth_dev,
 				   kvlist) < 0)
 		return -1;
-- 
2.34.1