Skip to content

Commit

Permalink
samples/bpf: Fix xdp_redirect_map egress devmap prog
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Andrii Nakryiko <[email protected]>
Link: https://lore.kernel.org/bpf/165754826292.575614.5636444052787717159.stgit@firesoul
  • Loading branch information
netoptimizer authored and anakryiko committed Jul 12, 2022
1 parent efc9909 commit 49705c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions samples/bpf/xdp_redirect_map.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -73,14 +73,16 @@ 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;

nh_off = sizeof(*eth);
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;
}
Expand Down
9 changes: 9 additions & 0 deletions samples/bpf/xdp_redirect_map_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 49705c4

Please sign in to comment.