Skip to content

Commit 435f90a

Browse files
sowminivborkmann
authored andcommitted
selftests/bpf: add a test case for sock_ops perf-event notification
This patch provides a tcp_bpf based eBPF sample. The test - ncat(1) as the TCP client program to connect() to a port with the intention of triggerring SYN retransmissions: we first install an iptables DROP rule to make sure ncat SYNs are resent (instead of aborting instantly after a TCP RST) - has a bpf kernel module that sends a perf-event notification for each TCP retransmit, and also tracks the number of such notifications sent in the global_map The test passes when the number of event notifications intercepted in user-space matches the value in the global_map. Signed-off-by: Sowmini Varadhan <sowmini.varadhan@oracle.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent a5a3a82 commit 435f90a

File tree

4 files changed

+303
-1
lines changed

4 files changed

+303
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
2424
test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
2525
test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user \
2626
test_socket_cookie test_cgroup_storage test_select_reuseport test_section_names \
27-
test_netcnt
27+
test_netcnt test_tcpnotify_user
2828

2929
TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \
3030
test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \
3131
sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
3232
test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
33+
test_tcpnotify_kern.o \
3334
sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
3435
sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \
3536
test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \
@@ -74,6 +75,7 @@ $(OUTPUT)/test_sock_addr: cgroup_helpers.c
7475
$(OUTPUT)/test_socket_cookie: cgroup_helpers.c
7576
$(OUTPUT)/test_sockmap: cgroup_helpers.c
7677
$(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
78+
$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
7779
$(OUTPUT)/test_progs: trace_helpers.c
7880
$(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
7981
$(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#ifndef _TEST_TCPBPF_H
4+
#define _TEST_TCPBPF_H
5+
6+
struct tcpnotify_globals {
7+
__u32 total_retrans;
8+
__u32 ncalls;
9+
};
10+
11+
struct tcp_notifier {
12+
__u8 type;
13+
__u8 subtype;
14+
__u8 source;
15+
__u8 hash;
16+
};
17+
18+
#define TESTPORT 12877
19+
#endif
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stddef.h>
3+
#include <string.h>
4+
#include <linux/bpf.h>
5+
#include <linux/if_ether.h>
6+
#include <linux/if_packet.h>
7+
#include <linux/ip.h>
8+
#include <linux/ipv6.h>
9+
#include <linux/types.h>
10+
#include <linux/socket.h>
11+
#include <linux/tcp.h>
12+
#include <netinet/in.h>
13+
#include "bpf_helpers.h"
14+
#include "bpf_endian.h"
15+
#include "test_tcpnotify.h"
16+
17+
struct bpf_map_def SEC("maps") global_map = {
18+
.type = BPF_MAP_TYPE_ARRAY,
19+
.key_size = sizeof(__u32),
20+
.value_size = sizeof(struct tcpnotify_globals),
21+
.max_entries = 4,
22+
};
23+
24+
struct bpf_map_def SEC("maps") perf_event_map = {
25+
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
26+
.key_size = sizeof(int),
27+
.value_size = sizeof(__u32),
28+
.max_entries = 2,
29+
};
30+
31+
int _version SEC("version") = 1;
32+
33+
SEC("sockops")
34+
int bpf_testcb(struct bpf_sock_ops *skops)
35+
{
36+
int rv = -1;
37+
int op;
38+
39+
op = (int) skops->op;
40+
41+
if (bpf_ntohl(skops->remote_port) != TESTPORT) {
42+
skops->reply = -1;
43+
return 0;
44+
}
45+
46+
switch (op) {
47+
case BPF_SOCK_OPS_TIMEOUT_INIT:
48+
case BPF_SOCK_OPS_RWND_INIT:
49+
case BPF_SOCK_OPS_NEEDS_ECN:
50+
case BPF_SOCK_OPS_BASE_RTT:
51+
case BPF_SOCK_OPS_RTO_CB:
52+
rv = 1;
53+
break;
54+
55+
case BPF_SOCK_OPS_TCP_CONNECT_CB:
56+
case BPF_SOCK_OPS_TCP_LISTEN_CB:
57+
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
58+
case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
59+
bpf_sock_ops_cb_flags_set(skops, (BPF_SOCK_OPS_RETRANS_CB_FLAG|
60+
BPF_SOCK_OPS_RTO_CB_FLAG));
61+
rv = 1;
62+
break;
63+
case BPF_SOCK_OPS_RETRANS_CB: {
64+
__u32 key = 0;
65+
struct tcpnotify_globals g, *gp;
66+
struct tcp_notifier msg = {
67+
.type = 0xde,
68+
.subtype = 0xad,
69+
.source = 0xbe,
70+
.hash = 0xef,
71+
};
72+
73+
rv = 1;
74+
75+
/* Update results */
76+
gp = bpf_map_lookup_elem(&global_map, &key);
77+
if (!gp)
78+
break;
79+
g = *gp;
80+
g.total_retrans = skops->total_retrans;
81+
g.ncalls++;
82+
bpf_map_update_elem(&global_map, &key, &g,
83+
BPF_ANY);
84+
bpf_perf_event_output(skops, &perf_event_map,
85+
BPF_F_CURRENT_CPU,
86+
&msg, sizeof(msg));
87+
}
88+
break;
89+
default:
90+
rv = -1;
91+
}
92+
skops->reply = rv;
93+
return 1;
94+
}
95+
char _license[] SEC("license") = "GPL";
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#define _GNU_SOURCE
3+
#include <pthread.h>
4+
#include <inttypes.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <unistd.h>
8+
#include <asm/types.h>
9+
#include <sys/syscall.h>
10+
#include <errno.h>
11+
#include <string.h>
12+
#include <linux/bpf.h>
13+
#include <sys/socket.h>
14+
#include <bpf/bpf.h>
15+
#include <bpf/libbpf.h>
16+
#include <sys/ioctl.h>
17+
#include <linux/rtnetlink.h>
18+
#include <signal.h>
19+
#include <linux/perf_event.h>
20+
21+
#include "bpf_rlimit.h"
22+
#include "bpf_util.h"
23+
#include "cgroup_helpers.h"
24+
25+
#include "test_tcpnotify.h"
26+
#include "trace_helpers.h"
27+
28+
#define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)
29+
30+
pthread_t tid;
31+
int rx_callbacks;
32+
33+
static int dummyfn(void *data, int size)
34+
{
35+
struct tcp_notifier *t = data;
36+
37+
if (t->type != 0xde || t->subtype != 0xad ||
38+
t->source != 0xbe || t->hash != 0xef)
39+
return 1;
40+
rx_callbacks++;
41+
return 0;
42+
}
43+
44+
void tcp_notifier_poller(int fd)
45+
{
46+
while (1)
47+
perf_event_poller(fd, dummyfn);
48+
}
49+
50+
static void *poller_thread(void *arg)
51+
{
52+
int fd = *(int *)arg;
53+
54+
tcp_notifier_poller(fd);
55+
return arg;
56+
}
57+
58+
int verify_result(const struct tcpnotify_globals *result)
59+
{
60+
return (result->ncalls > 0 && result->ncalls == rx_callbacks ? 0 : 1);
61+
}
62+
63+
static int bpf_find_map(const char *test, struct bpf_object *obj,
64+
const char *name)
65+
{
66+
struct bpf_map *map;
67+
68+
map = bpf_object__find_map_by_name(obj, name);
69+
if (!map) {
70+
printf("%s:FAIL:map '%s' not found\n", test, name);
71+
return -1;
72+
}
73+
return bpf_map__fd(map);
74+
}
75+
76+
static int setup_bpf_perf_event(int mapfd)
77+
{
78+
struct perf_event_attr attr = {
79+
.sample_type = PERF_SAMPLE_RAW,
80+
.type = PERF_TYPE_SOFTWARE,
81+
.config = PERF_COUNT_SW_BPF_OUTPUT,
82+
};
83+
int key = 0;
84+
int pmu_fd;
85+
86+
pmu_fd = syscall(__NR_perf_event_open, &attr, -1, 0, -1, 0);
87+
if (pmu_fd < 0)
88+
return pmu_fd;
89+
bpf_map_update_elem(mapfd, &key, &pmu_fd, BPF_ANY);
90+
91+
ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
92+
return pmu_fd;
93+
}
94+
95+
int main(int argc, char **argv)
96+
{
97+
const char *file = "test_tcpnotify_kern.o";
98+
int prog_fd, map_fd, perf_event_fd;
99+
struct tcpnotify_globals g = {0};
100+
const char *cg_path = "/foo";
101+
int error = EXIT_FAILURE;
102+
struct bpf_object *obj;
103+
int cg_fd = -1;
104+
__u32 key = 0;
105+
int rv;
106+
char test_script[80];
107+
int pmu_fd;
108+
cpu_set_t cpuset;
109+
110+
CPU_ZERO(&cpuset);
111+
CPU_SET(0, &cpuset);
112+
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
113+
114+
if (setup_cgroup_environment())
115+
goto err;
116+
117+
cg_fd = create_and_get_cgroup(cg_path);
118+
if (!cg_fd)
119+
goto err;
120+
121+
if (join_cgroup(cg_path))
122+
goto err;
123+
124+
if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
125+
printf("FAILED: load_bpf_file failed for: %s\n", file);
126+
goto err;
127+
}
128+
129+
rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
130+
if (rv) {
131+
printf("FAILED: bpf_prog_attach: %d (%s)\n",
132+
error, strerror(errno));
133+
goto err;
134+
}
135+
136+
perf_event_fd = bpf_find_map(__func__, obj, "perf_event_map");
137+
if (perf_event_fd < 0)
138+
goto err;
139+
140+
map_fd = bpf_find_map(__func__, obj, "global_map");
141+
if (map_fd < 0)
142+
goto err;
143+
144+
pmu_fd = setup_bpf_perf_event(perf_event_fd);
145+
if (pmu_fd < 0 || perf_event_mmap(pmu_fd) < 0)
146+
goto err;
147+
148+
pthread_create(&tid, NULL, poller_thread, (void *)&pmu_fd);
149+
150+
sprintf(test_script,
151+
"/usr/sbin/iptables -A INPUT -p tcp --dport %d -j DROP",
152+
TESTPORT);
153+
system(test_script);
154+
155+
sprintf(test_script,
156+
"/usr/bin/nc 127.0.0.1 %d < /etc/passwd > /dev/null 2>&1 ",
157+
TESTPORT);
158+
system(test_script);
159+
160+
sprintf(test_script,
161+
"/usr/sbin/iptables -D INPUT -p tcp --dport %d -j DROP",
162+
TESTPORT);
163+
system(test_script);
164+
165+
rv = bpf_map_lookup_elem(map_fd, &key, &g);
166+
if (rv != 0) {
167+
printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
168+
goto err;
169+
}
170+
171+
sleep(10);
172+
173+
if (verify_result(&g)) {
174+
printf("FAILED: Wrong stats Expected %d calls, got %d\n",
175+
g.ncalls, rx_callbacks);
176+
goto err;
177+
}
178+
179+
printf("PASSED!\n");
180+
error = 0;
181+
err:
182+
bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
183+
close(cg_fd);
184+
cleanup_cgroup_environment();
185+
return error;
186+
}

0 commit comments

Comments
 (0)