From mboxrd@z Thu Jan 1 00:00:00 1970 From: Joe Stringer Subject: [RFC bpf-next 10/11] selftests/bpf: Add C tests for reference tracking Date: Wed, 9 May 2018 14:07:08 -0700 Message-ID: <20180509210709.7201-11-joe@wand.net.nz> References: <20180509210709.7201-1-joe@wand.net.nz> Cc: netdev@vger.kernel.org, ast@kernel.org, john.fastabend@gmail.com, kafai@fb.com To: daniel@iogearbox.net Return-path: Received: from mail-ot0-f193.google.com ([74.125.82.193]:36331 "EHLO mail-ot0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965454AbeEIVHf (ORCPT ); Wed, 9 May 2018 17:07:35 -0400 Received: by mail-ot0-f193.google.com with SMTP id m11-v6so35600991otf.3 for ; Wed, 09 May 2018 14:07:34 -0700 (PDT) In-Reply-To: <20180509210709.7201-1-joe@wand.net.nz> Sender: netdev-owner@vger.kernel.org List-ID: Signed-off-by: Joe Stringer --- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/test_progs.c | 38 +++++++ tools/testing/selftests/bpf/test_sk_lookup_kern.c | 127 ++++++++++++++++++++++ 3 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_sk_lookup_kern.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 9d762184b805..cf71baa9d51d 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -33,7 +33,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \ sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \ test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \ - test_get_stack_rawtp.o + test_get_stack_rawtp.o test_sk_lookup_kern.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \ diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index ed197eef1cfc..6d868a031b00 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -1409,6 +1409,43 @@ static void test_get_stack_raw_tp(void) bpf_object__close(obj); } +static void test_reference_tracking() +{ + const char *file = "./test_sk_lookup_kern.o"; + struct bpf_object *obj; + struct bpf_program *prog; + __u32 duration; + int err = 0; + + obj = bpf_object__open(file); + if (IS_ERR(obj)) { + error_cnt++; + return; + } + + bpf_object__for_each_program(prog, obj) { + const char *title; + + /* Ignore .text sections */ + title = bpf_program__title(prog, false); + if (strstr(title, ".text") != NULL) + continue; + + bpf_program__set_type(prog, BPF_PROG_TYPE_SCHED_CLS); + + /* Expect verifier failure if test name has 'fail' */ + if (strstr(title, "fail") != NULL) { + libbpf_set_print(NULL, NULL, NULL); + err = !bpf_program__load(prog, "GPL", 0); + libbpf_set_print(printf, printf, NULL); + } else { + err = bpf_program__load(prog, "GPL", 0); + } + CHECK(err, title, "\n"); + } + bpf_object__close(obj); +} + int main(void) { jit_enabled = is_jit_enabled(); @@ -1427,6 +1464,7 @@ int main(void) test_stacktrace_build_id(); test_stacktrace_map_raw_tp(); test_get_stack_raw_tp(); + test_reference_tracking(); printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS; diff --git a/tools/testing/selftests/bpf/test_sk_lookup_kern.c b/tools/testing/selftests/bpf/test_sk_lookup_kern.c new file mode 100644 index 000000000000..4f7383a31916 --- /dev/null +++ b/tools/testing/selftests/bpf/test_sk_lookup_kern.c @@ -0,0 +1,127 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +// Copyright (c) 2018 Covalent IO, Inc. http://covalent.io + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "bpf_helpers.h" +#include "bpf_endian.h" + +int _version SEC("version") = 1; +char _license[] SEC("license") = "GPL"; + +/* Fill 'tuple' with L3 info, and attempt to find L4. On fail, return NULL. */ +static void *fill_ip(struct bpf_sock_tuple *tuple, void *data, __u64 nh_off, + void *data_end, __u16 eth_proto) +{ + __u64 ihl_len; + + if (eth_proto == bpf_htons(ETH_P_IP)) { + struct iphdr *iph = (struct iphdr *)(data + nh_off); + + if (iph + 1 > data_end) + return NULL; + ihl_len = iph->ihl * 4; + + tuple->family = AF_INET; + tuple->proto = iph->protocol; + tuple->saddr.ipv4 = iph->saddr; + tuple->daddr.ipv4 = iph->daddr; + } else if (eth_proto == bpf_htons(ETH_P_IPV6)) { + struct ipv6hdr *ip6h = (struct ipv6hdr *)(data + nh_off); + + if (ip6h + 1 > data_end) + return NULL; + ihl_len = sizeof(*ip6h); + + tuple->family = AF_INET6; + tuple->proto = ip6h->nexthdr; + *((struct in6_addr *)&tuple->saddr.ipv6) = ip6h->saddr; + *((struct in6_addr *)&tuple->daddr.ipv6) = ip6h->daddr; + } + + if (tuple->proto != IPPROTO_TCP) + return NULL; + + return data + nh_off + ihl_len; +} + +SEC("sk_lookup_success") +int bpf_sk_lookup_test0(struct __sk_buff *skb) +{ + void *data_end = (void *)(long)skb->data_end; + void *data = (void *)(long)skb->data; + struct ethhdr *eth = (struct ethhdr *)(data); + struct bpf_sock_tuple tuple = {}; + struct tcphdr *tcp; + struct bpf_sock *sk; + void *l4; + + if (eth + 1 > data_end) + return TC_ACT_SHOT; + + l4 = fill_ip(&tuple, data, sizeof(*eth), data_end, eth->h_proto); + if (!l4 || l4 + sizeof *tcp > data_end) + return TC_ACT_SHOT; + + tcp = l4; + tuple.sport = tcp->source; + tuple.dport = tcp->dest; + + sk = bpf_sk_lookup(skb, &tuple, sizeof(tuple), 0, 0); + if (sk) + bpf_sk_release(sk, 0); + return sk ? TC_ACT_OK : TC_ACT_UNSPEC; +} + +SEC("fail_no_release") +int bpf_sk_lookup_test1(struct __sk_buff *skb) +{ + struct bpf_sock_tuple tuple = {}; + + bpf_sk_lookup(skb, &tuple, sizeof(tuple), 0, 0); + return 0; +} + +SEC("fail_release_twice") +int bpf_sk_lookup_test2(struct __sk_buff *skb) +{ + struct bpf_sock_tuple tuple = {}; + struct bpf_sock *sk; + + sk = bpf_sk_lookup(skb, &tuple, sizeof(tuple), 0, 0); + bpf_sk_release(sk, 0); + bpf_sk_release(sk, 0); + return 0; +} + +SEC("fail_release_unchecked") +int bpf_sk_lookup_test3(struct __sk_buff *skb) +{ + struct bpf_sock_tuple tuple = {}; + struct bpf_sock *sk; + + sk = bpf_sk_lookup(skb, &tuple, sizeof(tuple), 0, 0); + bpf_sk_release(sk, 0); + return 0; +} + +void lookup_no_release(struct __sk_buff *skb) +{ + struct bpf_sock_tuple tuple = {}; + bpf_sk_lookup(skb, &tuple, sizeof(tuple), 0, 0); +} + +SEC("fail_no_release_subcall") +int bpf_sk_lookup_test4(struct __sk_buff *skb) +{ + lookup_no_release(skb); + return 0; +} -- 2.14.1