samples/bpf: fixup some tools to be able to support xdp multibuffer
authorAndy Gospodarek <andrew.gospodarek@broadcom.com>
Tue, 21 Jun 2022 17:54:02 +0000 (17:54 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 22 Jun 2022 01:55:53 +0000 (18:55 -0700)
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>
samples/bpf/xdp1_kern.c
samples/bpf/xdp2_kern.c
samples/bpf/xdp_tx_iptunnel_kern.c

index f0c5d95084dec92ea84ac9cef71b57a61f1b3483..0a5c704badd00c6078169ecee5f43f029fb412fa 100644 (file)
@@ -39,11 +39,13 @@ static int parse_ipv6(void *data, u64 nh_off, void *data_end)
        return ip6h->nexthdr;
 }
 
-SEC("xdp1")
+#define XDPBUFSIZE     64
+SEC("xdp.frags")
 int xdp_prog1(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;
        struct ethhdr *eth = data;
        int rc = XDP_DROP;
        long *value;
@@ -51,6 +53,9 @@ int xdp_prog1(struct xdp_md *ctx)
        u64 nh_off;
        u32 ipproto;
 
+       if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
+               return rc;
+
        nh_off = sizeof(*eth);
        if (data + nh_off > data_end)
                return rc;
index d8a64ab077b0045d515dfa9e547479944ba1b76b..3332ba6bb95fb14d04507bde56cf0a514bd2ac92 100644 (file)
@@ -55,11 +55,13 @@ static int parse_ipv6(void *data, u64 nh_off, void *data_end)
        return ip6h->nexthdr;
 }
 
-SEC("xdp1")
+#define XDPBUFSIZE     64
+SEC("xdp.frags")
 int xdp_prog1(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;
        struct ethhdr *eth = data;
        int rc = XDP_DROP;
        long *value;
@@ -67,6 +69,9 @@ int xdp_prog1(struct xdp_md *ctx)
        u64 nh_off;
        u32 ipproto;
 
+       if (bpf_xdp_load_bytes(ctx, 0, pkt, sizeof(pkt)))
+               return rc;
+
        nh_off = sizeof(*eth);
        if (data + nh_off > data_end)
                return rc;
index 575d57e4b8d6333ad374a0ad01919df634ad0223..0e2bca3a3fff7de71af5c4c195e074405f3e22ff 100644 (file)
@@ -212,7 +212,7 @@ static __always_inline int handle_ipv6(struct xdp_md *xdp)
        return XDP_TX;
 }
 
-SEC("xdp_tx_iptunnel")
+SEC("xdp.frags")
 int _xdp_tx_iptunnel(struct xdp_md *xdp)
 {
        void *data_end = (void *)(long)xdp->data_end;