summaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2017-08-04 19:00:10 -0400
committerDavid S. Miller <davem@davemloft.net>2017-08-07 17:09:48 -0400
commit1da236b6be9632255ab034f22aca5b78d7c3c007 (patch)
tree172104216444020892b0fbbd0f4de8a72e8686a9 /samples
parentcf5f5cea270655dd49370760576c64b228583b79 (diff)
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>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/Makefile4
-rw-r--r--samples/bpf/syscall_tp_kern.c62
-rw-r--r--samples/bpf/syscall_tp_user.c71
3 files changed, 137 insertions, 0 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 770d46cdf9f4..f1010fe759fe 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -39,6 +39,7 @@ hostprogs-y += per_socket_stats_example
39hostprogs-y += load_sock_ops 39hostprogs-y += load_sock_ops
40hostprogs-y += xdp_redirect 40hostprogs-y += xdp_redirect
41hostprogs-y += xdp_redirect_map 41hostprogs-y += xdp_redirect_map
42hostprogs-y += syscall_tp
42 43
43# Libbpf dependencies 44# Libbpf dependencies
44LIBBPF := ../../tools/lib/bpf/bpf.o 45LIBBPF := ../../tools/lib/bpf/bpf.o
@@ -82,6 +83,7 @@ test_map_in_map-objs := bpf_load.o $(LIBBPF) test_map_in_map_user.o
82per_socket_stats_example-objs := $(LIBBPF) cookie_uid_helper_example.o 83per_socket_stats_example-objs := $(LIBBPF) cookie_uid_helper_example.o
83xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o 84xdp_redirect-objs := bpf_load.o $(LIBBPF) xdp_redirect_user.o
84xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o 85xdp_redirect_map-objs := bpf_load.o $(LIBBPF) xdp_redirect_map_user.o
86syscall_tp-objs := bpf_load.o $(LIBBPF) syscall_tp_user.o
85 87
86# Tell kbuild to always build the programs 88# Tell kbuild to always build the programs
87always := $(hostprogs-y) 89always := $(hostprogs-y)
@@ -125,6 +127,7 @@ always += tcp_iw_kern.o
125always += tcp_clamp_kern.o 127always += tcp_clamp_kern.o
126always += xdp_redirect_kern.o 128always += xdp_redirect_kern.o
127always += xdp_redirect_map_kern.o 129always += xdp_redirect_map_kern.o
130always += syscall_tp_kern.o
128 131
129HOSTCFLAGS += -I$(objtree)/usr/include 132HOSTCFLAGS += -I$(objtree)/usr/include
130HOSTCFLAGS += -I$(srctree)/tools/lib/ 133HOSTCFLAGS += -I$(srctree)/tools/lib/
@@ -163,6 +166,7 @@ HOSTLOADLIBES_xdp_tx_iptunnel += -lelf
163HOSTLOADLIBES_test_map_in_map += -lelf 166HOSTLOADLIBES_test_map_in_map += -lelf
164HOSTLOADLIBES_xdp_redirect += -lelf 167HOSTLOADLIBES_xdp_redirect += -lelf
165HOSTLOADLIBES_xdp_redirect_map += -lelf 168HOSTLOADLIBES_xdp_redirect_map += -lelf
169HOSTLOADLIBES_syscall_tp += -lelf
166 170
167# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: 171# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
168# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang 172# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
diff --git a/samples/bpf/syscall_tp_kern.c b/samples/bpf/syscall_tp_kern.c
new file mode 100644
index 000000000000..9149c524d279
--- /dev/null
+++ b/samples/bpf/syscall_tp_kern.c
@@ -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
10struct 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
18struct syscalls_exit_open_args {
19 unsigned long long unused;
20 long syscall_nr;
21 long ret;
22};
23
24struct 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
31struct 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
38static __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
50SEC("tracepoint/syscalls/sys_enter_open")
51int trace_enter_open(struct syscalls_enter_open_args *ctx)
52{
53 count((void *)&enter_open_map);
54 return 0;
55}
56
57SEC("tracepoint/syscalls/sys_exit_open")
58int trace_enter_exit(struct syscalls_exit_open_args *ctx)
59{
60 count((void *)&exit_open_map);
61 return 0;
62}
diff --git a/samples/bpf/syscall_tp_user.c b/samples/bpf/syscall_tp_user.c
new file mode 100644
index 000000000000..a3cb91ebf4e7
--- /dev/null
+++ b/samples/bpf/syscall_tp_user.c
@@ -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
26static 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
39int 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}