]> git.baikalelectronics.ru Git - kernel.git/commitdiff
selftests/bpf: Add tests for program extensions
authorAlexei Starovoitov <ast@kernel.org>
Tue, 21 Jan 2020 00:53:48 +0000 (16:53 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 22 Jan 2020 22:04:53 +0000 (23:04 +0100)
Add program extension tests that build on top of fexit_bpf2bpf tests.
Replace three global functions in previously loaded test_pkt_access.c program
with three new implementations:
int get_skb_len(struct __sk_buff *skb);
int get_constant(long val);
int get_skb_ifindex(int val, struct __sk_buff *skb, int var);
New function return the same results as original only if arguments match.

new_get_skb_ifindex() demonstrates that 'skb' argument doesn't have to be first
and only argument of BPF program. All normal skb based accesses are available.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20200121005348.2769920-4-ast@kernel.org
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c
tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c
tools/testing/selftests/bpf/progs/test_pkt_access.c

index 7d3740d38965d49b11fbb60bfc74633229d7d2e2..db5c74d2ce6d0245e6a7446e4369817c7cf67cfe 100644 (file)
@@ -26,7 +26,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
 
        link = calloc(sizeof(struct bpf_link *), prog_cnt);
        prog = calloc(sizeof(struct bpf_program *), prog_cnt);
-       result = malloc(prog_cnt * sizeof(u64));
+       result = malloc((prog_cnt + 32 /* spare */) * sizeof(u64));
        if (CHECK(!link || !prog || !result, "alloc_memory",
                  "failed to alloc memory"))
                goto close_prog;
@@ -106,8 +106,26 @@ static void test_target_yes_callees(void)
                                  prog_name);
 }
 
+static void test_func_replace(void)
+{
+       const char *prog_name[] = {
+               "fexit/test_pkt_access",
+               "fexit/test_pkt_access_subprog1",
+               "fexit/test_pkt_access_subprog2",
+               "fexit/test_pkt_access_subprog3",
+               "freplace/get_skb_len",
+               "freplace/get_skb_ifindex",
+               "freplace/get_constant",
+       };
+       test_fexit_bpf2bpf_common("./fexit_bpf2bpf.o",
+                                 "./test_pkt_access.o",
+                                 ARRAY_SIZE(prog_name),
+                                 prog_name);
+}
+
 void test_fexit_bpf2bpf(void)
 {
        test_target_no_callees();
        test_target_yes_callees();
+       test_func_replace();
 }
index ec5710767d1358aa0ff9ae8812ab59916ad59849..c329fccf9842c24a4a028d1ac88bdabed21a009e 100644 (file)
@@ -1,7 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2019 Facebook */
+#include <linux/stddef.h>
+#include <linux/ipv6.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_endian.h>
 #include "bpf_trace_helpers.h"
 
 struct sk_buff {
@@ -94,4 +97,58 @@ int BPF_PROG(test_subprog3, int val, struct sk_buff *skb, int ret)
        test_result_subprog3 = 1;
        return 0;
 }
+
+__u64 test_get_skb_len = 0;
+SEC("freplace/get_skb_len")
+int new_get_skb_len(struct __sk_buff *skb)
+{
+       int len = skb->len;
+
+       if (len != 74)
+               return 0;
+       test_get_skb_len = 1;
+       return 74; /* original get_skb_len() returns skb->len */
+}
+
+__u64 test_get_skb_ifindex = 0;
+SEC("freplace/get_skb_ifindex")
+int new_get_skb_ifindex(int val, struct __sk_buff *skb, int var)
+{
+       void *data_end = (void *)(long)skb->data_end;
+       void *data = (void *)(long)skb->data;
+       struct ipv6hdr ip6, *ip6p;
+       int ifindex = skb->ifindex;
+       __u32 eth_proto;
+       __u32 nh_off;
+
+       /* check that BPF extension can read packet via direct packet access */
+       if (data + 14 + sizeof(ip6) > data_end)
+               return 0;
+       ip6p = data + 14;
+
+       if (ip6p->nexthdr != 6 || ip6p->payload_len != __bpf_constant_htons(123))
+               return 0;
+
+       /* check that legacy packet access helper works too */
+       if (bpf_skb_load_bytes(skb, 14, &ip6, sizeof(ip6)) < 0)
+               return 0;
+       ip6p = &ip6;
+       if (ip6p->nexthdr != 6 || ip6p->payload_len != __bpf_constant_htons(123))
+               return 0;
+
+       if (ifindex != 1 || val != 3 || var != 1)
+               return 0;
+       test_get_skb_ifindex = 1;
+       return 3; /* original get_skb_ifindex() returns val * ifindex * var */
+}
+
+volatile __u64 test_get_constant = 0;
+SEC("freplace/get_constant")
+int new_get_constant(long val)
+{
+       if (val != 123)
+               return 0;
+       test_get_constant = 1;
+       return test_get_constant; /* original get_constant() returns val - 122 */
+}
 char _license[] SEC("license") = "GPL";
index 7c9fcfd2b463eb5e2ac9af1f159b54e34f20cc5e..e72eba4a93d21fc44481e364f236c585380fdefe 100644 (file)
@@ -57,12 +57,18 @@ int get_skb_len(struct __sk_buff *skb)
        return skb->len;
 }
 
+__attribute__ ((noinline))
+int get_constant(long val)
+{
+       return val - 122;
+}
+
 int get_skb_ifindex(int, struct __sk_buff *skb, int);
 
 __attribute__ ((noinline))
 int test_pkt_access_subprog3(int val, struct __sk_buff *skb)
 {
-       return get_skb_len(skb) * get_skb_ifindex(val, skb, 1);
+       return get_skb_len(skb) * get_skb_ifindex(val, skb, get_constant(123));
 }
 
 __attribute__ ((noinline))