Skip to content

Commit 1d9c037

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf, selftests: Add sk_msg helpers load and attach test
The test itself is not particularly useful but it encodes a common pattern we have. Namely do a sk storage lookup then depending on data here decide if we need to do more work or alternatively allow packet to PASS. Then if we need to do more work consult task_struct for more information about the running task. Finally based on this additional information drop or pass the data. In this case the suspicious check is not so realisitic but it encodes the general pattern and uses the helpers so we test the workflow. This is a load test to ensure verifier correctly handles this case. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/159033909665.12355.6166415847337547879.stgit@john-Precision-5820-Tower Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 13d70f5 commit 1d9c037

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

tools/testing/selftests/bpf/prog_tests/sockmap_basic.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2020 Cloudflare
3+
#include <error.h>
34

45
#include "test_progs.h"
6+
#include "test_skmsg_load_helpers.skel.h"
57

68
#define TCP_REPAIR 19 /* TCP sock is under repair right now */
79

@@ -70,10 +72,43 @@ static void test_sockmap_create_update_free(enum bpf_map_type map_type)
7072
close(s);
7173
}
7274

75+
static void test_skmsg_helpers(enum bpf_map_type map_type)
76+
{
77+
struct test_skmsg_load_helpers *skel;
78+
int err, map, verdict;
79+
80+
skel = test_skmsg_load_helpers__open_and_load();
81+
if (CHECK_FAIL(!skel)) {
82+
perror("test_skmsg_load_helpers__open_and_load");
83+
return;
84+
}
85+
86+
verdict = bpf_program__fd(skel->progs.prog_msg_verdict);
87+
map = bpf_map__fd(skel->maps.sock_map);
88+
89+
err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0);
90+
if (CHECK_FAIL(err)) {
91+
perror("bpf_prog_attach");
92+
goto out;
93+
}
94+
95+
err = bpf_prog_detach2(verdict, map, BPF_SK_MSG_VERDICT);
96+
if (CHECK_FAIL(err)) {
97+
perror("bpf_prog_detach2");
98+
goto out;
99+
}
100+
out:
101+
test_skmsg_load_helpers__destroy(skel);
102+
}
103+
73104
void test_sockmap_basic(void)
74105
{
75106
if (test__start_subtest("sockmap create_update_free"))
76107
test_sockmap_create_update_free(BPF_MAP_TYPE_SOCKMAP);
77108
if (test__start_subtest("sockhash create_update_free"))
78109
test_sockmap_create_update_free(BPF_MAP_TYPE_SOCKHASH);
110+
if (test__start_subtest("sockmap sk_msg load helpers"))
111+
test_skmsg_helpers(BPF_MAP_TYPE_SOCKMAP);
112+
if (test__start_subtest("sockhash sk_msg load helpers"))
113+
test_skmsg_helpers(BPF_MAP_TYPE_SOCKHASH);
79114
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2020 Isovalent, Inc.
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
struct {
7+
__uint(type, BPF_MAP_TYPE_SOCKMAP);
8+
__uint(max_entries, 2);
9+
__type(key, __u32);
10+
__type(value, __u64);
11+
} sock_map SEC(".maps");
12+
13+
struct {
14+
__uint(type, BPF_MAP_TYPE_SOCKHASH);
15+
__uint(max_entries, 2);
16+
__type(key, __u32);
17+
__type(value, __u64);
18+
} sock_hash SEC(".maps");
19+
20+
struct {
21+
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
22+
__uint(map_flags, BPF_F_NO_PREALLOC);
23+
__type(key, __u32);
24+
__type(value, __u64);
25+
} socket_storage SEC(".maps");
26+
27+
SEC("sk_msg")
28+
int prog_msg_verdict(struct sk_msg_md *msg)
29+
{
30+
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
31+
int verdict = SK_PASS;
32+
__u32 pid, tpid;
33+
__u64 *sk_stg;
34+
35+
pid = bpf_get_current_pid_tgid() >> 32;
36+
sk_stg = bpf_sk_storage_get(&socket_storage, msg->sk, 0, BPF_SK_STORAGE_GET_F_CREATE);
37+
if (!sk_stg)
38+
return SK_DROP;
39+
*sk_stg = pid;
40+
bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
41+
if (pid != tpid)
42+
verdict = SK_DROP;
43+
bpf_sk_storage_delete(&socket_storage, (void *)msg->sk);
44+
return verdict;
45+
}
46+
47+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)