Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples/bpf: fixup some tools to be able to support xdp multibuffer

This changes the section name for the bpf program embedded in these
files to "xdp.frags" to allow the programs to be loaded on drivers that
are using an MTU greater than PAGE_SIZE. Rather than directly accessing
the buffers, the packet data is now accessed via xdp helper functions to
provide an example for those who may need to write more complex
programs.

v2: remove new unnecessary variable

Signed-off-by: Andy Gospodarek <gospo@broadcom.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Lorenzo Bianconi <lorenzo@kernel.org>
Link: https://lore.kernel.org/r/20220621175402.35327-1-gospo@broadcom.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Andy Gospodarek and committed by
Alexei Starovoitov
77225174 d4609a5d

+17 -7
+8 -3
samples/bpf/xdp1_kern.c
··· 39 39 return ip6h->nexthdr; 40 40 } 41 41 42 - SEC("xdp1") 42 + #define XDPBUFSIZE 64 43 + SEC("xdp.frags") 43 44 int xdp_prog1(struct xdp_md *ctx) 44 45 { 45 - void *data_end = (void *)(long)ctx->data_end; 46 - void *data = (void *)(long)ctx->data; 46 + __u8 pkt[XDPBUFSIZE] = {}; 47 + void *data_end = &pkt[XDPBUFSIZE-1]; 48 + void *data = pkt; 47 49 struct ethhdr *eth = data; 48 50 int rc = XDP_DROP; 49 51 long *value; 50 52 u16 h_proto; 51 53 u64 nh_off; 52 54 u32 ipproto; 55 + 56 + if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt))) 57 + return rc; 53 58 54 59 nh_off = sizeof(*eth); 55 60 if (data + nh_off > data_end)
+8 -3
samples/bpf/xdp2_kern.c
··· 55 55 return ip6h->nexthdr; 56 56 } 57 57 58 - SEC("xdp1") 58 + #define XDPBUFSIZE 64 59 + SEC("xdp.frags") 59 60 int xdp_prog1(struct xdp_md *ctx) 60 61 { 61 - void *data_end = (void *)(long)ctx->data_end; 62 - void *data = (void *)(long)ctx->data; 62 + __u8 pkt[XDPBUFSIZE] = {}; 63 + void *data_end = &pkt[XDPBUFSIZE-1]; 64 + void *data = pkt; 63 65 struct ethhdr *eth = data; 64 66 int rc = XDP_DROP; 65 67 long *value; 66 68 u16 h_proto; 67 69 u64 nh_off; 68 70 u32 ipproto; 71 + 72 + if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt))) 73 + return rc; 69 74 70 75 nh_off = sizeof(*eth); 71 76 if (data + nh_off > data_end)
+1 -1
samples/bpf/xdp_tx_iptunnel_kern.c
··· 212 212 return XDP_TX; 213 213 } 214 214 215 - SEC("xdp_tx_iptunnel") 215 + SEC("xdp.frags") 216 216 int _xdp_tx_iptunnel(struct xdp_md *xdp) 217 217 { 218 218 void *data_end = (void *)(long)xdp->data_end;