diff mbox series

[bpf-next] samples/bpf: Fix xdp_redirect_map egress devmap prog

Message ID 165754826292.575614.5636444052787717159.stgit@firesoul (mailing list archive)
State Accepted
Commit 49705c4ab324654a7038fc843255140730477e04
Delegated to: BPF
Headers show
Series [bpf-next] samples/bpf: Fix xdp_redirect_map egress devmap prog | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 18 maintainers not CCed: haoluo@google.com trix@redhat.com song@kernel.org ast@kernel.org hawk@kernel.org kuba@kernel.org yhs@fb.com davem@davemloft.net martin.lau@linux.dev ndesaulniers@google.com jolsa@kernel.org andrii@kernel.org kpsingh@kernel.org daniel@iogearbox.net nathan@kernel.org john.fastabend@gmail.com llvm@lists.linux.dev sdf@google.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch fail CHECK: No space is necessary after a cast ERROR: do not initialise statics to 0 WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst WARNING: line length of 90 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Jesper Dangaard Brouer July 11, 2022, 2:04 p.m. UTC
LLVM compiler optimized out the memcpy in xdp_redirect_map_egress,
which caused the Ethernet source MAC-addr to always be zero
when enabling the devmap egress prog via cmdline --load-egress.

Issue observed with LLVM version 14.0.0
 - Shipped with Fedora 36 on target: x86_64-redhat-linux-gnu.

In verbose mode print the source MAC-addr in case xdp_devmap_attached
mode is used.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
---
 samples/bpf/xdp_redirect_map.bpf.c  |    6 ++++--
 samples/bpf/xdp_redirect_map_user.c |    9 +++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

Comments

patchwork-bot+netdevbpf@kernel.org July 12, 2022, 4:20 a.m. UTC | #1
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Mon, 11 Jul 2022 16:04:22 +0200 you wrote:
> LLVM compiler optimized out the memcpy in xdp_redirect_map_egress,
> which caused the Ethernet source MAC-addr to always be zero
> when enabling the devmap egress prog via cmdline --load-egress.
> 
> Issue observed with LLVM version 14.0.0
>  - Shipped with Fedora 36 on target: x86_64-redhat-linux-gnu.
> 
> [...]

Here is the summary with links:
  - [bpf-next] samples/bpf: Fix xdp_redirect_map egress devmap prog
    https://git.kernel.org/bpf/bpf-next/c/49705c4ab324

You are awesome, thank you!
diff mbox series

Patch

diff --git a/samples/bpf/xdp_redirect_map.bpf.c b/samples/bpf/xdp_redirect_map.bpf.c
index 415bac1758e3..8557c278df77 100644
--- a/samples/bpf/xdp_redirect_map.bpf.c
+++ b/samples/bpf/xdp_redirect_map.bpf.c
@@ -33,7 +33,7 @@  struct {
 } tx_port_native SEC(".maps");
 
 /* store egress interface mac address */
-const volatile char tx_mac_addr[ETH_ALEN];
+const volatile __u8 tx_mac_addr[ETH_ALEN];
 
 static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_map)
 {
@@ -73,6 +73,7 @@  int xdp_redirect_map_egress(struct xdp_md *ctx)
 {
 	void *data_end = (void *)(long)ctx->data_end;
 	void *data = (void *)(long)ctx->data;
+	u8 *mac_addr = (u8 *) tx_mac_addr;
 	struct ethhdr *eth = data;
 	u64 nh_off;
 
@@ -80,7 +81,8 @@  int xdp_redirect_map_egress(struct xdp_md *ctx)
 	if (data + nh_off > data_end)
 		return XDP_DROP;
 
-	__builtin_memcpy(eth->h_source, (const char *)tx_mac_addr, ETH_ALEN);
+	barrier_var(mac_addr); /* prevent optimizing out memcpy */
+	__builtin_memcpy(eth->h_source, mac_addr, ETH_ALEN);
 
 	return XDP_PASS;
 }
diff --git a/samples/bpf/xdp_redirect_map_user.c b/samples/bpf/xdp_redirect_map_user.c
index b6e4fc849577..c889a1394dc1 100644
--- a/samples/bpf/xdp_redirect_map_user.c
+++ b/samples/bpf/xdp_redirect_map_user.c
@@ -40,6 +40,8 @@  static const struct option long_options[] = {
 	{}
 };
 
+static int verbose = 0;
+
 int main(int argc, char **argv)
 {
 	struct bpf_devmap_val devmap_val = {};
@@ -79,6 +81,7 @@  int main(int argc, char **argv)
 			break;
 		case 'v':
 			sample_switch_mode();
+			verbose = 1;
 			break;
 		case 's':
 			mask |= SAMPLE_REDIRECT_MAP_CNT;
@@ -134,6 +137,12 @@  int main(int argc, char **argv)
 			ret = EXIT_FAIL;
 			goto end_destroy;
 		}
+		if (verbose)
+			printf("Egress ifindex:%d using src MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
+			       ifindex_out,
+			       skel->rodata->tx_mac_addr[0], skel->rodata->tx_mac_addr[1],
+			       skel->rodata->tx_mac_addr[2], skel->rodata->tx_mac_addr[3],
+			       skel->rodata->tx_mac_addr[4], skel->rodata->tx_mac_addr[5]);
 	}
 
 	skel->rodata->from_match[0] = ifindex_in;