Skip to content

Commit 898ca68

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
selftests/bpf: switch test_tcpnotify to perf_buffer API
Switch test_tcpnotify test to use libbpf's perf_buffer API instead of re-implementing portion of it. Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 58b8081 commit 898ca68

File tree

1 file changed

+36
-54
lines changed

1 file changed

+36
-54
lines changed

tools/testing/selftests/bpf/test_tcpnotify_user.c

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/rtnetlink.h>
1818
#include <signal.h>
1919
#include <linux/perf_event.h>
20+
#include <linux/err.h>
2021

2122
#include "bpf_rlimit.h"
2223
#include "bpf_util.h"
@@ -30,28 +31,34 @@
3031
pthread_t tid;
3132
int rx_callbacks;
3233

33-
static int dummyfn(void *data, int size)
34+
static void dummyfn(void *ctx, int cpu, void *data, __u32 size)
3435
{
3536
struct tcp_notifier *t = data;
3637

3738
if (t->type != 0xde || t->subtype != 0xad ||
3839
t->source != 0xbe || t->hash != 0xef)
39-
return 1;
40+
return;
4041
rx_callbacks++;
41-
return 0;
4242
}
4343

44-
void tcp_notifier_poller(int fd)
44+
void tcp_notifier_poller(struct perf_buffer *pb)
4545
{
46-
while (1)
47-
perf_event_poller(fd, dummyfn);
46+
int err;
47+
48+
while (1) {
49+
err = perf_buffer__poll(pb, 100);
50+
if (err < 0 && err != -EINTR) {
51+
printf("failed perf_buffer__poll: %d\n", err);
52+
return;
53+
}
54+
}
4855
}
4956

5057
static void *poller_thread(void *arg)
5158
{
52-
int fd = *(int *)arg;
59+
struct perf_buffer *pb = arg;
5360

54-
tcp_notifier_poller(fd);
61+
tcp_notifier_poller(pb);
5562
return arg;
5663
}
5764

@@ -60,52 +67,20 @@ int verify_result(const struct tcpnotify_globals *result)
6067
return (result->ncalls > 0 && result->ncalls == rx_callbacks ? 0 : 1);
6168
}
6269

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-
9570
int main(int argc, char **argv)
9671
{
9772
const char *file = "test_tcpnotify_kern.o";
98-
int prog_fd, map_fd, perf_event_fd;
73+
struct bpf_map *perf_map, *global_map;
74+
struct perf_buffer_opts pb_opts = {};
9975
struct tcpnotify_globals g = {0};
76+
struct perf_buffer *pb = NULL;
10077
const char *cg_path = "/foo";
78+
int prog_fd, rv, cg_fd = -1;
10179
int error = EXIT_FAILURE;
10280
struct bpf_object *obj;
103-
int cg_fd = -1;
104-
__u32 key = 0;
105-
int rv;
10681
char test_script[80];
107-
int pmu_fd;
10882
cpu_set_t cpuset;
83+
__u32 key = 0;
10984

11085
CPU_ZERO(&cpuset);
11186
CPU_SET(0, &cpuset);
@@ -133,19 +108,24 @@ int main(int argc, char **argv)
133108
goto err;
134109
}
135110

136-
perf_event_fd = bpf_find_map(__func__, obj, "perf_event_map");
137-
if (perf_event_fd < 0)
111+
perf_map = bpf_object__find_map_by_name(obj, "perf_event_map");
112+
if (!perf_map) {
113+
printf("FAIL:map '%s' not found\n", "perf_event_map");
138114
goto err;
115+
}
139116

140-
map_fd = bpf_find_map(__func__, obj, "global_map");
141-
if (map_fd < 0)
142-
goto err;
117+
global_map = bpf_object__find_map_by_name(obj, "global_map");
118+
if (!global_map) {
119+
printf("FAIL:map '%s' not found\n", "global_map");
120+
return -1;
121+
}
143122

144-
pmu_fd = setup_bpf_perf_event(perf_event_fd);
145-
if (pmu_fd < 0 || perf_event_mmap(pmu_fd) < 0)
123+
pb_opts.sample_cb = dummyfn;
124+
pb = perf_buffer__new(bpf_map__fd(perf_map), 8, &pb_opts);
125+
if (IS_ERR(pb))
146126
goto err;
147127

148-
pthread_create(&tid, NULL, poller_thread, (void *)&pmu_fd);
128+
pthread_create(&tid, NULL, poller_thread, pb);
149129

150130
sprintf(test_script,
151131
"iptables -A INPUT -p tcp --dport %d -j DROP",
@@ -162,7 +142,7 @@ int main(int argc, char **argv)
162142
TESTPORT);
163143
system(test_script);
164144

165-
rv = bpf_map_lookup_elem(map_fd, &key, &g);
145+
rv = bpf_map_lookup_elem(bpf_map__fd(global_map), &key, &g);
166146
if (rv != 0) {
167147
printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
168148
goto err;
@@ -182,5 +162,7 @@ int main(int argc, char **argv)
182162
bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
183163
close(cg_fd);
184164
cleanup_cgroup_environment();
165+
if (!IS_ERR_OR_NULL(pb))
166+
perf_buffer__free(pb);
185167
return error;
186168
}

0 commit comments

Comments
 (0)