diff options
author | Daniel Borkmann <daniel@iogearbox.net> | 2020-10-11 02:40:06 +0300 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2020-10-11 20:21:04 +0300 |
commit | 9f4c53ca23a28c891d2bd3ff4738f7d95ba0303b (patch) | |
tree | c6559345e3b01dbd0ab5a39bad0e73ec1bb5b263 /tools/testing/selftests/bpf/progs/test_tc_peer.c | |
parent | 57a73fe7c1988ae726de23ee7c3c49ca82221751 (diff) | |
download | linux-9f4c53ca23a28c891d2bd3ff4738f7d95ba0303b.tar.xz |
bpf, selftests: Add redirect_peer selftest
Extend the test_tc_redirect test and add a small test that exercises the new
redirect_peer() helper for the IPv4 and IPv6 case.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20201010234006.7075-7-daniel@iogearbox.net
Diffstat (limited to 'tools/testing/selftests/bpf/progs/test_tc_peer.c')
-rw-r--r-- | tools/testing/selftests/bpf/progs/test_tc_peer.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/progs/test_tc_peer.c b/tools/testing/selftests/bpf/progs/test_tc_peer.c new file mode 100644 index 000000000000..fc84a7685aa2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_tc_peer.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <stdint.h> +#include <stdbool.h> + +#include <linux/bpf.h> +#include <linux/stddef.h> +#include <linux/pkt_cls.h> + +#include <bpf/bpf_helpers.h> + +enum { + dev_src, + dev_dst, +}; + +struct bpf_map_def SEC("maps") ifindex_map = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(int), + .value_size = sizeof(int), + .max_entries = 2, +}; + +static __always_inline int get_dev_ifindex(int which) +{ + int *ifindex = bpf_map_lookup_elem(&ifindex_map, &which); + + return ifindex ? *ifindex : 0; +} + +SEC("chk_egress") int tc_chk(struct __sk_buff *skb) +{ + return TC_ACT_SHOT; +} + +SEC("dst_ingress") int tc_dst(struct __sk_buff *skb) +{ + return bpf_redirect_peer(get_dev_ifindex(dev_src), 0); +} + +SEC("src_ingress") int tc_src(struct __sk_buff *skb) +{ + return bpf_redirect_peer(get_dev_ifindex(dev_dst), 0); +} + +char __license[] SEC("license") = "GPL"; |