Skip to content

Commit 1da236b

Browse files
yonghong-songdavem330
authored andcommitted
bpf: add a test case for syscalls/sys_{enter|exit}_* tracepoints
Signed-off-by: Yonghong Song <yhs@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cf5f5ce commit 1da236b

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

samples/bpf/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ hostprogs-y += per_socket_stats_example
3939
hostprogs-y += load_sock_ops
4040
hostprogs-y += xdp_redirect
4141
hostprogs-y += xdp_redirect_map
42+
hostprogs-y += syscall_tp
4243

4344
# Libbpf dependencies
4445
LIBBPF := ../../tools/lib/bpf/bpf.o
@@ -82,6 +83,7 @@ test_map_in_map-objs := bpf_load.o $(LIBBPF) test_map_in_map_user.o
8283
per_socket_stats_example-objs := $(LIBBPF) cookie_uid_helper_example.o
8384
xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o
8485
xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o
86+
syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
8587

8688
# Tell kbuild to always build the programs
8789
always := $(hostprogs-y)
@@ -125,6 +127,7 @@ always += tcp_iw_kern.o
125127
always += tcp_clamp_kern.o
126128
always += xdp_redirect_kern.o
127129
always += xdp_redirect_map_kern.o
130+
always += syscall_tp_kern.o
128131

129132
HOSTCFLAGS += -I$(objtree)/usr/include
130133
HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -163,6 +166,7 @@ HOSTLOADLIBES_xdp_tx_iptunnel += -lelf
163166
HOSTLOADLIBES_test_map_in_map += -lelf
164167
HOSTLOADLIBES_xdp_redirect += -lelf
165168
HOSTLOADLIBES_xdp_redirect_map += -lelf
169+
HOSTLOADLIBES_syscall_tp += -lelf
166170

167171
# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
168172
# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang

samples/bpf/syscall_tp_kern.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* Copyright (c) 2017 Facebook
2+
*
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of version 2 of the GNU General Public
5+
* License as published by the Free Software Foundation.
6+
*/
7+
#include <uapi/linux/bpf.h>
8+
#include "bpf_helpers.h"
9+
10+
struct syscalls_enter_open_args {
11+
unsigned long long unused;
12+
long syscall_nr;
13+
long filename_ptr;
14+
long flags;
15+
long mode;
16+
};
17+
18+
struct syscalls_exit_open_args {
19+
unsigned long long unused;
20+
long syscall_nr;
21+
long ret;
22+
};
23+
24+
struct bpf_map_def SEC("maps") enter_open_map = {
25+
.type = BPF_MAP_TYPE_ARRAY,
26+
.key_size = sizeof(u32),
27+
.value_size = sizeof(u32),
28+
.max_entries = 1,
29+
};
30+
31+
struct bpf_map_def SEC("maps") exit_open_map = {
32+
.type = BPF_MAP_TYPE_ARRAY,
33+
.key_size = sizeof(u32),
34+
.value_size = sizeof(u32),
35+
.max_entries = 1,
36+
};
37+
38+
static __always_inline void count(void *map)
39+
{
40+
u32 key = 0;
41+
u32 *value, init_val = 1;
42+
43+
value = bpf_map_lookup_elem(map, &key);
44+
if (value)
45+
*value += 1;
46+
else
47+
bpf_map_update_elem(map, &key, &init_val, BPF_NOEXIST);
48+
}
49+
50+
SEC("tracepoint/syscalls/sys_enter_open")
51+
int trace_enter_open(struct syscalls_enter_open_args *ctx)
52+
{
53+
count((void *)&enter_open_map);
54+
return 0;
55+
}
56+
57+
SEC("tracepoint/syscalls/sys_exit_open")
58+
int trace_enter_exit(struct syscalls_exit_open_args *ctx)
59+
{
60+
count((void *)&exit_open_map);
61+
return 0;
62+
}

samples/bpf/syscall_tp_user.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (c) 2017 Facebook
2+
*
3+
* This program is free software; you can redistribute it and/or
4+
* modify it under the terms of version 2 of the GNU General Public
5+
* License as published by the Free Software Foundation.
6+
*/
7+
#include <stdio.h>
8+
#include <unistd.h>
9+
#include <fcntl.h>
10+
#include <stdlib.h>
11+
#include <signal.h>
12+
#include <linux/bpf.h>
13+
#include <string.h>
14+
#include <linux/perf_event.h>
15+
#include <errno.h>
16+
#include <assert.h>
17+
#include <stdbool.h>
18+
#include <sys/resource.h>
19+
#include "libbpf.h"
20+
#include "bpf_load.h"
21+
22+
/* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
23+
* This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
24+
*/
25+
26+
static void verify_map(int map_id)
27+
{
28+
__u32 key = 0;
29+
__u32 val;
30+
31+
if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
32+
fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
33+
return;
34+
}
35+
if (val == 0)
36+
fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
37+
}
38+
39+
int main(int argc, char **argv)
40+
{
41+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
42+
char filename[256];
43+
int fd;
44+
45+
setrlimit(RLIMIT_MEMLOCK, &r);
46+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
47+
48+
if (load_bpf_file(filename)) {
49+
fprintf(stderr, "%s", bpf_log_buf);
50+
return 1;
51+
}
52+
53+
/* current load_bpf_file has perf_event_open default pid = -1
54+
* and cpu = 0, which permits attached bpf execution on
55+
* all cpus for all pid's. bpf program execution ignores
56+
* cpu affinity.
57+
*/
58+
/* trigger some "open" operations */
59+
fd = open(filename, O_RDONLY);
60+
if (fd < 0) {
61+
fprintf(stderr, "open failed: %s\n", strerror(errno));
62+
return 1;
63+
}
64+
close(fd);
65+
66+
/* verify the map */
67+
verify_map(map_fd[0]);
68+
verify_map(map_fd[1]);
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)