From a31eaf3090f26f73fa3996487d9bde36418dbcd9 Mon Sep 17 00:00:00 2001
From: Huisong Li <lihuisong@huawei.com>
Date: Tue, 15 Nov 2022 12:06:09 +0800
Subject: app/testpmd: fix MAC header in csum forward engine

[ upstream commit 008834b91ac9a9e4ea982e5d2a4526d1b90a8d18 ]

MLX5 SR-IOV Tx engine will not transmit Ethernet frame
if destination MAC address matched local port address. The frame ether
looped-back to Rx or dropped, depending on the port configuration.

Application running over MLX5 SR-IOV port cannot transmit packet
polled from Rx queue as is. The packet Ethernet destination address
must be changed.

Add new run-time configuration parameter to the `csum` forwarding
engine to control MAC addresses configuration:

testpmd> csum mac-swap on|off <port_id>

`mac-swap on`  replace MAC addresses.
`mac-swap off` keep Ethernet header unchanged.

Fixes: 9b4ea7ae77fa ("app/testpmd: revert MAC update in checksum forwarding")

Signed-off-by: Gregory Etelson <getelson@nvidia.com>
Acked-by: Huisong Li <lihuisong@huawei.com>
---
 app/test-pmd/cmdline.c  | 50 +++++++++++++++++++++++++++++++++++++++++
 app/test-pmd/csumonly.c |  6 +++++
 app/test-pmd/testpmd.c  |  5 +++--
 app/test-pmd/testpmd.h  |  3 ++-
 4 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c
index 8d4a88bb85..9e0e725913 100644
--- a/app/test-pmd/cmdline.c
+++ b/app/test-pmd/cmdline.c
@@ -4836,6 +4836,55 @@ cmdline_parse_inst_t cmd_csum_tunnel = {
 	},
 };
 
+struct cmd_csum_mac_swap_result {
+	cmdline_fixed_string_t csum;
+	cmdline_fixed_string_t parse;
+	cmdline_fixed_string_t onoff;
+	portid_t port_id;
+};
+
+static void
+cmd_csum_mac_swap_parsed(void *parsed_result,
+		       __rte_unused struct cmdline *cl,
+		       __rte_unused void *data)
+{
+	struct cmd_csum_mac_swap_result *res = parsed_result;
+
+	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
+		return;
+	if (strcmp(res->onoff, "on") == 0)
+		ports[res->port_id].fwd_mac_swap = 1;
+	else
+		ports[res->port_id].fwd_mac_swap = 0;
+}
+
+static cmdline_parse_token_string_t cmd_csum_mac_swap_csum =
+	TOKEN_STRING_INITIALIZER(struct cmd_csum_mac_swap_result,
+				 csum, "csum");
+static cmdline_parse_token_string_t cmd_csum_mac_swap_parse =
+	TOKEN_STRING_INITIALIZER(struct cmd_csum_mac_swap_result,
+				 parse, "mac-swap");
+static cmdline_parse_token_string_t cmd_csum_mac_swap_onoff =
+	TOKEN_STRING_INITIALIZER(struct cmd_csum_mac_swap_result,
+				 onoff, "on#off");
+static cmdline_parse_token_num_t cmd_csum_mac_swap_portid =
+	TOKEN_NUM_INITIALIZER(struct cmd_csum_mac_swap_result,
+			      port_id, RTE_UINT16);
+
+static cmdline_parse_inst_t cmd_csum_mac_swap = {
+	.f = cmd_csum_mac_swap_parsed,
+	.data = NULL,
+	.help_str = "csum mac-swap on|off <port_id>: "
+		    "Enable/Disable forward mac address swap",
+	.tokens = {
+		(void *)&cmd_csum_mac_swap_csum,
+		(void *)&cmd_csum_mac_swap_parse,
+		(void *)&cmd_csum_mac_swap_onoff,
+		(void *)&cmd_csum_mac_swap_portid,
+		NULL,
+	},
+};
+
 /* *** ENABLE HARDWARE SEGMENTATION IN TX NON-TUNNELED PACKETS *** */
 struct cmd_tso_set_result {
 	cmdline_fixed_string_t tso;
@@ -17699,6 +17748,7 @@ cmdline_parse_ctx_t main_ctx[] = {
 	(cmdline_parse_inst_t *)&cmd_csum_set,
 	(cmdline_parse_inst_t *)&cmd_csum_show,
 	(cmdline_parse_inst_t *)&cmd_csum_tunnel,
+	(cmdline_parse_inst_t *)&cmd_csum_mac_swap,
 	(cmdline_parse_inst_t *)&cmd_tso_set,
 	(cmdline_parse_inst_t *)&cmd_tso_show,
 	(cmdline_parse_inst_t *)&cmd_tunnel_tso_set,
diff --git a/app/test-pmd/csumonly.c b/app/test-pmd/csumonly.c
index 206968d37a..d8cb8c89aa 100644
--- a/app/test-pmd/csumonly.c
+++ b/app/test-pmd/csumonly.c
@@ -887,6 +887,12 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
 		 * and inner headers */
 
 		eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
+		if (ports[fs->tx_port].fwd_mac_swap) {
+			rte_ether_addr_copy(&peer_eth_addrs[fs->peer_addr],
+					    &eth_hdr->dst_addr);
+			rte_ether_addr_copy(&ports[fs->tx_port].eth_addr,
+					    &eth_hdr->src_addr);
+		}
 		parse_ethernet(eth_hdr, &info);
 		l3_hdr = (char *)eth_hdr + info.l2_len;
 
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 2be92af9f8..ff9eabbcb7 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -4160,10 +4160,11 @@ init_port(void)
 				"rte_zmalloc(%d struct rte_port) failed\n",
 				RTE_MAX_ETHPORTS);
 	}
-	for (i = 0; i < RTE_MAX_ETHPORTS; i++)
+	for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
+		ports[i].fwd_mac_swap = 1;
 		ports[i].xstats_info.allocated = false;
-	for (i = 0; i < RTE_MAX_ETHPORTS; i++)
 		LIST_INIT(&ports[i].flow_tunnel_list);
+	}
 	/* Initialize ports NUMA structures */
 	memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
 	memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index ab6642585e..442f97ce3d 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -247,7 +247,8 @@ struct rte_port {
 	struct rte_ether_addr   *mc_addr_pool; /**< pool of multicast addrs */
 	uint32_t                mc_addr_nb; /**< nb. of addr. in mc_addr_pool */
 	uint8_t                 slave_flag : 1, /**< bonding slave port */
-				bond_flag : 1; /**< port is bond device */
+				bond_flag : 1, /**< port is bond device */
+				fwd_mac_swap : 1; /**< swap packet MAC before forward */
 	struct port_flow        *flow_list; /**< Associated flows. */
 	struct port_indirect_action *actions_list;
 	/**< Associated indirect actions. */
-- 
2.23.0

