Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-buffer support for XDP_REDIRECT samples #5130

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions samples/bpf/xdp_redirect.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@

const volatile int ifindex_out;

SEC("xdp")
#define XDPBUFSIZE 64
SEC("xdp.frags")
int xdp_redirect_prog(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u8 pkt[XDPBUFSIZE] = {};
void *data_end = &pkt[XDPBUFSIZE-1];
void *data = pkt;
u32 key = bpf_get_smp_processor_id();
struct ethhdr *eth = data;
struct datarec *rec;
u64 nh_off;

if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
return XDP_DROP;

nh_off = sizeof(*eth);
if (data + nh_off > data_end)
return XDP_DROP;
Expand All @@ -36,11 +41,14 @@ int xdp_redirect_prog(struct xdp_md *ctx)
NO_TEAR_INC(rec->processed);

swap_src_dst_mac(data);
if (bpf_xdp_store_bytes(ctx, 0, pkt, sizeof(pkt)))
return XDP_DROP;

return bpf_redirect(ifindex_out, 0);
}

/* Redirect require an XDP bpf_prog loaded on the TX device */
SEC("xdp")
SEC("xdp.frags")
int xdp_redirect_dummy_prog(struct xdp_md *ctx)
{
return XDP_PASS;
Expand Down
31 changes: 23 additions & 8 deletions samples/bpf/xdp_redirect_map.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ struct {
/* store egress interface mac address */
const volatile __u8 tx_mac_addr[ETH_ALEN];

#define XDPBUFSIZE 64
static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_map)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u8 pkt[XDPBUFSIZE] = {};
void *data_end = &pkt[XDPBUFSIZE-1];
void *data = pkt;
u32 key = bpf_get_smp_processor_id();
struct ethhdr *eth = data;
struct datarec *rec;
u64 nh_off;

if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
return XDP_DROP;

nh_off = sizeof(*eth);
if (data + nh_off > data_end)
return XDP_DROP;
Expand All @@ -53,42 +58,52 @@ static __always_inline int xdp_redirect_map(struct xdp_md *ctx, void *redirect_m
return XDP_PASS;
NO_TEAR_INC(rec->processed);
swap_src_dst_mac(data);
if (bpf_xdp_store_bytes(ctx, 0, pkt, sizeof(pkt)))
return XDP_DROP;

return bpf_redirect_map(redirect_map, 0, 0);
}

SEC("xdp")
SEC("xdp.frags")
int xdp_redirect_map_general(struct xdp_md *ctx)
{
return xdp_redirect_map(ctx, &tx_port_general);
}

SEC("xdp")
SEC("xdp.frags")
int xdp_redirect_map_native(struct xdp_md *ctx)
{
return xdp_redirect_map(ctx, &tx_port_native);
}

SEC("xdp/devmap")
SEC("xdp.frags/devmap")
int xdp_redirect_map_egress(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u8 pkt[XDPBUFSIZE] = {};
void *data_end = &pkt[XDPBUFSIZE-1];
void *data = pkt;
u8 *mac_addr = (u8 *) tx_mac_addr;
struct ethhdr *eth = data;
u64 nh_off;

if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
return XDP_DROP;

nh_off = sizeof(*eth);
if (data + nh_off > data_end)
return XDP_DROP;

barrier_var(mac_addr); /* prevent optimizing out memcpy */
__builtin_memcpy(eth->h_source, mac_addr, ETH_ALEN);

if (bpf_xdp_store_bytes(ctx, 0, pkt, sizeof(pkt)))
return XDP_DROP;

return XDP_PASS;
}

/* Redirect require an XDP bpf_prog loaded on the TX device */
SEC("xdp")
SEC("xdp.frags")
int xdp_redirect_dummy_prog(struct xdp_md *ctx)
{
return XDP_PASS;
Expand Down
Loading