aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorSargun Dhillon <sargun@sargun.me>2016-08-12 11:57:04 -0400
committerDavid S. Miller <davem@davemloft.net>2016-08-13 00:49:42 -0400
commit9e6e60ecbd7323d4ac3f98dcdc1bd2c527a736ef (patch)
tree249b8cc8c37b46570ca05305aaa0c5ab79c28cee /samples
parent60d20f9195b260bdf0ac10c275ae9f6016f9c069 (diff)
samples/bpf: Add test_current_task_under_cgroup test
This test has a BPF program which writes the last known pid to call the sync syscall within a given cgroup to a map. The user mode program creates its own mount namespace, and mounts the cgroupsv2 hierarchy in there, as on all current test systems (Ubuntu 16.04, Debian), the cgroupsv2 vfs is unmounted by default. Once it does this, it proceeds to test. The test checks for positive and negative condition. It ensures that when it's part of a given cgroup, its pid is captured in the map, and that when it leaves the cgroup, this doesn't happen. It populate a cgroups arraymap prior to execution in userspace. This means that the program must be run in the same cgroups namespace as the programs that are being traced. Signed-off-by: Sargun Dhillon <sargun@sargun.me> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Tejun Heo <tj@kernel.org> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples')
-rw-r--r--samples/bpf/Makefile5
-rw-r--r--samples/bpf/bpf_helpers.h2
-rw-r--r--samples/bpf/test_current_task_under_cgroup_kern.c43
-rw-r--r--samples/bpf/test_current_task_under_cgroup_user.c145
4 files changed, 195 insertions, 0 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index 90ebf7d35c07..eb582c6264c3 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -24,6 +24,7 @@ hostprogs-y += test_overhead
24hostprogs-y += test_cgrp2_array_pin 24hostprogs-y += test_cgrp2_array_pin
25hostprogs-y += xdp1 25hostprogs-y += xdp1
26hostprogs-y += xdp2 26hostprogs-y += xdp2
27hostprogs-y += test_current_task_under_cgroup
27 28
28test_verifier-objs := test_verifier.o libbpf.o 29test_verifier-objs := test_verifier.o libbpf.o
29test_maps-objs := test_maps.o libbpf.o 30test_maps-objs := test_maps.o libbpf.o
@@ -49,6 +50,8 @@ test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o
49xdp1-objs := bpf_load.o libbpf.o xdp1_user.o 50xdp1-objs := bpf_load.o libbpf.o xdp1_user.o
50# reuse xdp1 source intentionally 51# reuse xdp1 source intentionally
51xdp2-objs := bpf_load.o libbpf.o xdp1_user.o 52xdp2-objs := bpf_load.o libbpf.o xdp1_user.o
53test_current_task_under_cgroup-objs := bpf_load.o libbpf.o \
54 test_current_task_under_cgroup_user.o
52 55
53# Tell kbuild to always build the programs 56# Tell kbuild to always build the programs
54always := $(hostprogs-y) 57always := $(hostprogs-y)
@@ -74,6 +77,7 @@ always += parse_varlen.o parse_simple.o parse_ldabs.o
74always += test_cgrp2_tc_kern.o 77always += test_cgrp2_tc_kern.o
75always += xdp1_kern.o 78always += xdp1_kern.o
76always += xdp2_kern.o 79always += xdp2_kern.o
80always += test_current_task_under_cgroup_kern.o
77 81
78HOSTCFLAGS += -I$(objtree)/usr/include 82HOSTCFLAGS += -I$(objtree)/usr/include
79 83
@@ -97,6 +101,7 @@ HOSTLOADLIBES_map_perf_test += -lelf -lrt
97HOSTLOADLIBES_test_overhead += -lelf -lrt 101HOSTLOADLIBES_test_overhead += -lelf -lrt
98HOSTLOADLIBES_xdp1 += -lelf 102HOSTLOADLIBES_xdp1 += -lelf
99HOSTLOADLIBES_xdp2 += -lelf 103HOSTLOADLIBES_xdp2 += -lelf
104HOSTLOADLIBES_test_current_task_under_cgroup += -lelf
100 105
101# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: 106# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
102# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang 107# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
diff --git a/samples/bpf/bpf_helpers.h b/samples/bpf/bpf_helpers.h
index cbc52df165b4..5e4c41e256b8 100644
--- a/samples/bpf/bpf_helpers.h
+++ b/samples/bpf/bpf_helpers.h
@@ -45,6 +45,8 @@ static int (*bpf_get_stackid)(void *ctx, void *map, int flags) =
45 (void *) BPF_FUNC_get_stackid; 45 (void *) BPF_FUNC_get_stackid;
46static int (*bpf_probe_write_user)(void *dst, void *src, int size) = 46static int (*bpf_probe_write_user)(void *dst, void *src, int size) =
47 (void *) BPF_FUNC_probe_write_user; 47 (void *) BPF_FUNC_probe_write_user;
48static int (*bpf_current_task_under_cgroup)(void *map, int index) =
49 (void *) BPF_FUNC_current_task_under_cgroup;
48 50
49/* llvm builtin functions that eBPF C program may use to 51/* llvm builtin functions that eBPF C program may use to
50 * emit BPF_LD_ABS and BPF_LD_IND instructions 52 * emit BPF_LD_ABS and BPF_LD_IND instructions
diff --git a/samples/bpf/test_current_task_under_cgroup_kern.c b/samples/bpf/test_current_task_under_cgroup_kern.c
new file mode 100644
index 000000000000..86b28d7d6c99
--- /dev/null
+++ b/samples/bpf/test_current_task_under_cgroup_kern.c
@@ -0,0 +1,43 @@
1/* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
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
8#include <linux/ptrace.h>
9#include <uapi/linux/bpf.h>
10#include <linux/version.h>
11#include "bpf_helpers.h"
12#include <uapi/linux/utsname.h>
13
14struct bpf_map_def SEC("maps") cgroup_map = {
15 .type = BPF_MAP_TYPE_CGROUP_ARRAY,
16 .key_size = sizeof(u32),
17 .value_size = sizeof(u32),
18 .max_entries = 1,
19};
20
21struct bpf_map_def SEC("maps") perf_map = {
22 .type = BPF_MAP_TYPE_ARRAY,
23 .key_size = sizeof(u32),
24 .value_size = sizeof(u64),
25 .max_entries = 1,
26};
27
28/* Writes the last PID that called sync to a map at index 0 */
29SEC("kprobe/sys_sync")
30int bpf_prog1(struct pt_regs *ctx)
31{
32 u64 pid = bpf_get_current_pid_tgid();
33 int idx = 0;
34
35 if (!bpf_current_task_under_cgroup(&cgroup_map, 0))
36 return 0;
37
38 bpf_map_update_elem(&perf_map, &idx, &pid, BPF_ANY);
39 return 0;
40}
41
42char _license[] SEC("license") = "GPL";
43u32 _version SEC("version") = LINUX_VERSION_CODE;
diff --git a/samples/bpf/test_current_task_under_cgroup_user.c b/samples/bpf/test_current_task_under_cgroup_user.c
new file mode 100644
index 000000000000..30b0bce884f9
--- /dev/null
+++ b/samples/bpf/test_current_task_under_cgroup_user.c
@@ -0,0 +1,145 @@
1/* Copyright (c) 2016 Sargun Dhillon <sargun@sargun.me>
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
8#define _GNU_SOURCE
9#include <stdio.h>
10#include <linux/bpf.h>
11#include <unistd.h>
12#include "libbpf.h"
13#include "bpf_load.h"
14#include <string.h>
15#include <fcntl.h>
16#include <errno.h>
17#include <linux/bpf.h>
18#include <sched.h>
19#include <sys/mount.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <linux/limits.h>
23
24#define CGROUP_MOUNT_PATH "/mnt"
25#define CGROUP_PATH "/mnt/my-cgroup"
26
27#define clean_errno() (errno == 0 ? "None" : strerror(errno))
28#define log_err(MSG, ...) fprintf(stderr, "(%s:%d: errno: %s) " MSG "\n", \
29 __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
30
31static int join_cgroup(char *path)
32{
33 int fd, rc = 0;
34 pid_t pid = getpid();
35 char cgroup_path[PATH_MAX + 1];
36
37 snprintf(cgroup_path, sizeof(cgroup_path), "%s/cgroup.procs", path);
38
39 fd = open(cgroup_path, O_WRONLY);
40 if (fd < 0) {
41 log_err("Opening Cgroup");
42 return 1;
43 }
44
45 if (dprintf(fd, "%d\n", pid) < 0) {
46 log_err("Joining Cgroup");
47 rc = 1;
48 }
49 close(fd);
50 return rc;
51}
52
53int main(int argc, char **argv)
54{
55 char filename[256];
56 int cg2, idx = 0;
57 pid_t remote_pid, local_pid = getpid();
58
59 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
60 if (load_bpf_file(filename)) {
61 printf("%s", bpf_log_buf);
62 return 1;
63 }
64
65 /*
66 * This is to avoid interfering with existing cgroups. Unfortunately,
67 * most people don't have cgroupv2 enabled at this point in time.
68 * It's easier to create our own mount namespace and manage it
69 * ourselves.
70 */
71 if (unshare(CLONE_NEWNS)) {
72 log_err("unshare");
73 return 1;
74 }
75
76 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
77 log_err("mount fakeroot");
78 return 1;
79 }
80
81 if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL)) {
82 log_err("mount cgroup2");
83 return 1;
84 }
85
86 if (mkdir(CGROUP_PATH, 0777) && errno != EEXIST) {
87 log_err("mkdir cgroup");
88 return 1;
89 }
90
91 cg2 = open(CGROUP_PATH, O_RDONLY);
92 if (cg2 < 0) {
93 log_err("opening target cgroup");
94 goto cleanup_cgroup_err;
95 }
96
97 if (bpf_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
98 log_err("Adding target cgroup to map");
99 goto cleanup_cgroup_err;
100 }
101 if (join_cgroup("/mnt/my-cgroup")) {
102 log_err("Leaving target cgroup");
103 goto cleanup_cgroup_err;
104 }
105
106 /*
107 * The installed helper program catched the sync call, and should
108 * write it to the map.
109 */
110
111 sync();
112 bpf_lookup_elem(map_fd[1], &idx, &remote_pid);
113
114 if (local_pid != remote_pid) {
115 fprintf(stderr,
116 "BPF Helper didn't write correct PID to map, but: %d\n",
117 remote_pid);
118 goto leave_cgroup_err;
119 }
120
121 /* Verify the negative scenario; leave the cgroup */
122 if (join_cgroup(CGROUP_MOUNT_PATH))
123 goto leave_cgroup_err;
124
125 remote_pid = 0;
126 bpf_update_elem(map_fd[1], &idx, &remote_pid, BPF_ANY);
127
128 sync();
129 bpf_lookup_elem(map_fd[1], &idx, &remote_pid);
130
131 if (local_pid == remote_pid) {
132 fprintf(stderr, "BPF cgroup negative test did not work\n");
133 goto cleanup_cgroup_err;
134 }
135
136 rmdir(CGROUP_PATH);
137 return 0;
138
139 /* Error condition, cleanup */
140leave_cgroup_err:
141 join_cgroup(CGROUP_MOUNT_PATH);
142cleanup_cgroup_err:
143 rmdir(CGROUP_PATH);
144 return 1;
145}