mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-12 05:48:40 +00:00
selftests/bpf: Add tests for program extensions
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
This commit is contained in:
parent
2db6eab18b
commit
7805fe8439
3 changed files with 83 additions and 2 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue