Skip to content

Commit 63841bc

Browse files
DanielTimLeeborkmann
authored andcommitted
samples, bpf: Refactor kprobe tracing user progs with libbpf
Currently, the kprobe BPF program attachment method for bpf_load is quite old. The implementation of bpf_load "directly" controls and manages(create, delete) the kprobe events of DEBUGFS. On the other hand, using using the libbpf automatically manages the kprobe event. (under bpf_link interface) By calling bpf_program__attach(_kprobe) in libbpf, the corresponding kprobe is created and the BPF program will be attached to this kprobe. To remove this, by simply invoking bpf_link__destroy will clean up the event. This commit refactors kprobe tracing programs (tracex{1~7}_user.c) with libbpf using bpf_link interface and bpf_program__attach. tracex2_kern.c, which tracks system calls (sys_*), has been modified to append prefix depending on architecture. Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200516040608.1377876-3-danieltimlee@gmail.com
1 parent 0efdcef commit 63841bc

9 files changed

Lines changed: 252 additions & 64 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ fds_example-objs := fds_example.o
6464
sockex1-objs := sockex1_user.o
6565
sockex2-objs := sockex2_user.o
6666
sockex3-objs := bpf_load.o sockex3_user.o
67-
tracex1-objs := bpf_load.o tracex1_user.o $(TRACE_HELPERS)
68-
tracex2-objs := bpf_load.o tracex2_user.o
69-
tracex3-objs := bpf_load.o tracex3_user.o
70-
tracex4-objs := bpf_load.o tracex4_user.o
67+
tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
68+
tracex2-objs := tracex2_user.o
69+
tracex3-objs := tracex3_user.o
70+
tracex4-objs := tracex4_user.o
7171
tracex5-objs := bpf_load.o tracex5_user.o $(TRACE_HELPERS)
72-
tracex6-objs := bpf_load.o tracex6_user.o
73-
tracex7-objs := bpf_load.o tracex7_user.o
72+
tracex6-objs := tracex6_user.o
73+
tracex7-objs := tracex7_user.o
7474
test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o
7575
trace_output-objs := bpf_load.o trace_output_user.o $(TRACE_HELPERS)
7676
lathist-objs := bpf_load.o lathist_user.o

‎samples/bpf/trace_common.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#ifndef __TRACE_COMMON_H
3+
#define __TRACE_COMMON_H
4+
5+
#ifdef __x86_64__
6+
#define SYSCALL(SYS) "__x64_" __stringify(SYS)
7+
#elif defined(__s390x__)
8+
#define SYSCALL(SYS) "__s390x_" __stringify(SYS)
9+
#else
10+
#define SYSCALL(SYS) __stringify(SYS)
11+
#endif
12+
13+
#endif

‎samples/bpf/tracex1_user.c‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,50 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <stdio.h>
3-
#include <linux/bpf.h>
43
#include <unistd.h>
5-
#include <bpf/bpf.h>
6-
#include "bpf_load.h"
4+
#include <bpf/libbpf.h>
75
#include "trace_helpers.h"
86

97
int main(int ac, char **argv)
108
{
11-
FILE *f;
9+
struct bpf_link *link = NULL;
10+
struct bpf_program *prog;
11+
struct bpf_object *obj;
1212
char filename[256];
13+
FILE *f;
1314

1415
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
16+
obj = bpf_object__open_file(filename, NULL);
17+
if (libbpf_get_error(obj)) {
18+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
19+
return 0;
20+
}
21+
22+
prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
23+
if (!prog) {
24+
fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
25+
goto cleanup;
26+
}
27+
28+
/* load BPF program */
29+
if (bpf_object__load(obj)) {
30+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
31+
goto cleanup;
32+
}
1533

16-
if (load_bpf_file(filename)) {
17-
printf("%s", bpf_log_buf);
18-
return 1;
34+
link = bpf_program__attach(prog);
35+
if (libbpf_get_error(link)) {
36+
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
37+
link = NULL;
38+
goto cleanup;
1939
}
2040

2141
f = popen("taskset 1 ping -c5 localhost", "r");
2242
(void) f;
2343

2444
read_trace_pipe();
2545

46+
cleanup:
47+
bpf_link__destroy(link);
48+
bpf_object__close(obj);
2649
return 0;
2750
}

‎samples/bpf/tracex2_kern.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uapi/linux/bpf.h>
1111
#include <bpf/bpf_helpers.h>
1212
#include <bpf/bpf_tracing.h>
13+
#include "trace_common.h"
1314

1415
struct bpf_map_def SEC("maps") my_map = {
1516
.type = BPF_MAP_TYPE_HASH,
@@ -77,7 +78,7 @@ struct bpf_map_def SEC("maps") my_hist_map = {
7778
.max_entries = 1024,
7879
};
7980

80-
SEC("kprobe/sys_write")
81+
SEC("kprobe/" SYSCALL(sys_write))
8182
int bpf_prog3(struct pt_regs *ctx)
8283
{
8384
long write_size = PT_REGS_PARM3(ctx);

‎samples/bpf/tracex2_user.c‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
#include <unistd.h>
44
#include <stdlib.h>
55
#include <signal.h>
6-
#include <linux/bpf.h>
76
#include <string.h>
87
#include <sys/resource.h>
98

109
#include <bpf/bpf.h>
11-
#include "bpf_load.h"
10+
#include <bpf/libbpf.h>
1211
#include "bpf_util.h"
1312

1413
#define MAX_INDEX 64
1514
#define MAX_STARS 38
1615

16+
/* my_map, my_hist_map */
17+
static int map_fd[2];
18+
1719
static void stars(char *str, long val, long max, int width)
1820
{
1921
int i;
@@ -115,18 +117,39 @@ static void int_exit(int sig)
115117
int main(int ac, char **argv)
116118
{
117119
struct rlimit r = {1024*1024, RLIM_INFINITY};
118-
char filename[256];
119120
long key, next_key, value;
121+
struct bpf_link *links[2];
122+
struct bpf_program *prog;
123+
struct bpf_object *obj;
124+
char filename[256];
125+
int i, j = 0;
120126
FILE *f;
121-
int i;
122-
123-
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
124127

125128
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
126129
perror("setrlimit(RLIMIT_MEMLOCK)");
127130
return 1;
128131
}
129132

133+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
134+
obj = bpf_object__open_file(filename, NULL);
135+
if (libbpf_get_error(obj)) {
136+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
137+
return 0;
138+
}
139+
140+
/* load BPF program */
141+
if (bpf_object__load(obj)) {
142+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
143+
goto cleanup;
144+
}
145+
146+
map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map");
147+
map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map");
148+
if (map_fd[0] < 0 || map_fd[1] < 0) {
149+
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
150+
goto cleanup;
151+
}
152+
130153
signal(SIGINT, int_exit);
131154
signal(SIGTERM, int_exit);
132155

@@ -138,9 +161,14 @@ int main(int ac, char **argv)
138161
f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
139162
(void) f;
140163

141-
if (load_bpf_file(filename)) {
142-
printf("%s", bpf_log_buf);
143-
return 1;
164+
bpf_object__for_each_program(prog, obj) {
165+
links[j] = bpf_program__attach(prog);
166+
if (libbpf_get_error(links[j])) {
167+
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
168+
links[j] = NULL;
169+
goto cleanup;
170+
}
171+
j++;
144172
}
145173

146174
for (i = 0; i < 5; i++) {
@@ -156,5 +184,10 @@ int main(int ac, char **argv)
156184
}
157185
print_hist(map_fd[1]);
158186

187+
cleanup:
188+
for (j--; j >= 0; j--)
189+
bpf_link__destroy(links[j]);
190+
191+
bpf_object__close(obj);
159192
return 0;
160193
}

‎samples/bpf/tracex3_user.c‎

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
#include <unistd.h>
88
#include <stdbool.h>
99
#include <string.h>
10-
#include <linux/bpf.h>
1110
#include <sys/resource.h>
1211

1312
#include <bpf/bpf.h>
14-
#include "bpf_load.h"
13+
#include <bpf/libbpf.h>
1514
#include "bpf_util.h"
1615

1716
#define SLOTS 100
@@ -109,20 +108,11 @@ static void print_hist(int fd)
109108
int main(int ac, char **argv)
110109
{
111110
struct rlimit r = {1024*1024, RLIM_INFINITY};
111+
struct bpf_link *links[2];
112+
struct bpf_program *prog;
113+
struct bpf_object *obj;
112114
char filename[256];
113-
int i;
114-
115-
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
116-
117-
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
118-
perror("setrlimit(RLIMIT_MEMLOCK)");
119-
return 1;
120-
}
121-
122-
if (load_bpf_file(filename)) {
123-
printf("%s", bpf_log_buf);
124-
return 1;
125-
}
115+
int map_fd, i, j = 0;
126116

127117
for (i = 1; i < ac; i++) {
128118
if (strcmp(argv[i], "-a") == 0) {
@@ -137,6 +127,40 @@ int main(int ac, char **argv)
137127
}
138128
}
139129

130+
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
131+
perror("setrlimit(RLIMIT_MEMLOCK)");
132+
return 1;
133+
}
134+
135+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
136+
obj = bpf_object__open_file(filename, NULL);
137+
if (libbpf_get_error(obj)) {
138+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
139+
return 0;
140+
}
141+
142+
/* load BPF program */
143+
if (bpf_object__load(obj)) {
144+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
145+
goto cleanup;
146+
}
147+
148+
map_fd = bpf_object__find_map_fd_by_name(obj, "lat_map");
149+
if (map_fd < 0) {
150+
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
151+
goto cleanup;
152+
}
153+
154+
bpf_object__for_each_program(prog, obj) {
155+
links[j] = bpf_program__attach(prog);
156+
if (libbpf_get_error(links[j])) {
157+
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
158+
links[j] = NULL;
159+
goto cleanup;
160+
}
161+
j++;
162+
}
163+
140164
printf(" heatmap of IO latency\n");
141165
if (text_only)
142166
printf(" %s", sym[num_colors - 1]);
@@ -153,9 +177,14 @@ int main(int ac, char **argv)
153177
for (i = 0; ; i++) {
154178
if (i % 20 == 0)
155179
print_banner();
156-
print_hist(map_fd[1]);
180+
print_hist(map_fd);
157181
sleep(2);
158182
}
159183

184+
cleanup:
185+
for (j--; j >= 0; j--)
186+
bpf_link__destroy(links[j]);
187+
188+
bpf_object__close(obj);
160189
return 0;
161190
}

‎samples/bpf/tracex4_user.c‎

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
#include <stdbool.h>
99
#include <string.h>
1010
#include <time.h>
11-
#include <linux/bpf.h>
1211
#include <sys/resource.h>
1312

1413
#include <bpf/bpf.h>
15-
#include "bpf_load.h"
14+
#include <bpf/libbpf.h>
1615

1716
struct pair {
1817
long long val;
@@ -36,8 +35,8 @@ static void print_old_objects(int fd)
3635
key = write(1, "\e[1;1H\e[2J", 12); /* clear screen */
3736

3837
key = -1;
39-
while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) {
40-
bpf_map_lookup_elem(map_fd[0], &next_key, &v);
38+
while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
39+
bpf_map_lookup_elem(fd, &next_key, &v);
4140
key = next_key;
4241
if (val - v.val < 1000000000ll)
4342
/* object was allocated more then 1 sec ago */
@@ -50,25 +49,55 @@ static void print_old_objects(int fd)
5049
int main(int ac, char **argv)
5150
{
5251
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
52+
struct bpf_link *links[2];
53+
struct bpf_program *prog;
54+
struct bpf_object *obj;
5355
char filename[256];
54-
int i;
55-
56-
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
56+
int map_fd, i, j = 0;
5757

5858
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
5959
perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
6060
return 1;
6161
}
6262

63-
if (load_bpf_file(filename)) {
64-
printf("%s", bpf_log_buf);
65-
return 1;
63+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
64+
obj = bpf_object__open_file(filename, NULL);
65+
if (libbpf_get_error(obj)) {
66+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
67+
return 0;
68+
}
69+
70+
/* load BPF program */
71+
if (bpf_object__load(obj)) {
72+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
73+
goto cleanup;
74+
}
75+
76+
map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
77+
if (map_fd < 0) {
78+
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
79+
goto cleanup;
80+
}
81+
82+
bpf_object__for_each_program(prog, obj) {
83+
links[j] = bpf_program__attach(prog);
84+
if (libbpf_get_error(links[j])) {
85+
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
86+
links[j] = NULL;
87+
goto cleanup;
88+
}
89+
j++;
6690
}
6791

6892
for (i = 0; ; i++) {
69-
print_old_objects(map_fd[1]);
93+
print_old_objects(map_fd);
7094
sleep(1);
7195
}
7296

97+
cleanup:
98+
for (j--; j >= 0; j--)
99+
bpf_link__destroy(links[j]);
100+
101+
bpf_object__close(obj);
73102
return 0;
74103
}

0 commit comments

Comments
 (0)