Skip to content

Commit

Permalink
selftests/bpf: Add a test case for loading BPF_SK_SKB_VERDICT
Browse files Browse the repository at this point in the history
This adds a test case to ensure BPF_SK_SKB_VERDICT and
BPF_SK_STREAM_VERDICT will never be attached at the same time.

Signed-off-by: Cong Wang <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
Cong Wang authored and Alexei Starovoitov committed Apr 1, 2021
1 parent d6378af commit 8d7cb74
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "test_skmsg_load_helpers.skel.h"
#include "test_sockmap_update.skel.h"
#include "test_sockmap_invalid_update.skel.h"
#include "test_sockmap_skb_verdict_attach.skel.h"
#include "bpf_iter_sockmap.skel.h"

#define TCP_REPAIR 19 /* TCP sock is under repair right now */
Expand Down Expand Up @@ -281,6 +282,39 @@ static void test_sockmap_copy(enum bpf_map_type map_type)
bpf_iter_sockmap__destroy(skel);
}

static void test_sockmap_skb_verdict_attach(enum bpf_attach_type first,
enum bpf_attach_type second)
{
struct test_sockmap_skb_verdict_attach *skel;
int err, map, verdict;

skel = test_sockmap_skb_verdict_attach__open_and_load();
if (CHECK_FAIL(!skel)) {
perror("test_sockmap_skb_verdict_attach__open_and_load");
return;
}

verdict = bpf_program__fd(skel->progs.prog_skb_verdict);
map = bpf_map__fd(skel->maps.sock_map);

err = bpf_prog_attach(verdict, map, first, 0);
if (CHECK_FAIL(err)) {
perror("bpf_prog_attach");
goto out;
}

err = bpf_prog_attach(verdict, map, second, 0);
assert(err == -1 && errno == EBUSY);

err = bpf_prog_detach2(verdict, map, first);
if (CHECK_FAIL(err)) {
perror("bpf_prog_detach2");
goto out;
}
out:
test_sockmap_skb_verdict_attach__destroy(skel);
}

void test_sockmap_basic(void)
{
if (test__start_subtest("sockmap create_update_free"))
Expand All @@ -301,4 +335,10 @@ void test_sockmap_basic(void)
test_sockmap_copy(BPF_MAP_TYPE_SOCKMAP);
if (test__start_subtest("sockhash copy"))
test_sockmap_copy(BPF_MAP_TYPE_SOCKHASH);
if (test__start_subtest("sockmap skb_verdict attach")) {
test_sockmap_skb_verdict_attach(BPF_SK_SKB_VERDICT,
BPF_SK_SKB_STREAM_VERDICT);
test_sockmap_skb_verdict_attach(BPF_SK_SKB_STREAM_VERDICT,
BPF_SK_SKB_VERDICT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>

struct {
__uint(type, BPF_MAP_TYPE_SOCKMAP);
__uint(max_entries, 2);
__type(key, __u32);
__type(value, __u64);
} sock_map SEC(".maps");

SEC("sk_skb/skb_verdict")
int prog_skb_verdict(struct __sk_buff *skb)
{
return SK_DROP;
}

char _license[] SEC("license") = "GPL";

0 comments on commit 8d7cb74

Please sign in to comment.