Skip to content

Commit bc1a859

Browse files
DanielTimLeeborkmann
authored andcommitted
samples, bpf: Refactor tail call user progs with libbpf
BPF tail call uses the BPF_MAP_TYPE_PROG_ARRAY type map for calling into other BPF programs and this PROG_ARRAY should be filled prior to use. Currently, samples with the PROG_ARRAY type MAP fill this program array with bpf_load. For bpf_load to fill this map, kernel BPF program must specify the section with specific format of <prog_type>/<array_idx> (e.g. SEC("socket/0")) But by using libbpf instead of bpf_load, user program can specify which programs should be added to PROG_ARRAY. The advantage of this approach is that you can selectively add only the programs you want, rather than adding all of them to PROG_ARRAY, and it's much more intuitive than the traditional approach. This commit refactors user programs with the PROG_ARRAY type MAP with libbpf instead of using bpf_load. 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-4-danieltimlee@gmail.com
1 parent 63841bc commit bc1a859

3 files changed

Lines changed: 103 additions & 31 deletions

File tree

‎samples/bpf/Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o
6363
fds_example-objs := fds_example.o
6464
sockex1-objs := sockex1_user.o
6565
sockex2-objs := sockex2_user.o
66-
sockex3-objs := bpf_load.o sockex3_user.o
66+
sockex3-objs := sockex3_user.o
6767
tracex1-objs := tracex1_user.o $(TRACE_HELPERS)
6868
tracex2-objs := tracex2_user.o
6969
tracex3-objs := tracex3_user.o
7070
tracex4-objs := tracex4_user.o
71-
tracex5-objs := bpf_load.o tracex5_user.o $(TRACE_HELPERS)
71+
tracex5-objs := tracex5_user.o $(TRACE_HELPERS)
7272
tracex6-objs := tracex6_user.o
7373
tracex7-objs := tracex7_user.o
7474
test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o

‎samples/bpf/sockex3_user.c‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <stdio.h>
33
#include <assert.h>
4-
#include <linux/bpf.h>
54
#include <bpf/bpf.h>
6-
#include "bpf_load.h"
5+
#include <bpf/libbpf.h>
76
#include "sock_example.h"
87
#include <unistd.h>
98
#include <arpa/inet.h>
109
#include <sys/resource.h>
1110

12-
#define PARSE_IP 3
13-
#define PARSE_IP_PROG_FD (prog_fd[0])
14-
#define PROG_ARRAY_FD (map_fd[0])
15-
1611
struct flow_key_record {
1712
__be32 src;
1813
__be32 dst;
@@ -30,31 +25,55 @@ struct pair {
3025

3126
int main(int argc, char **argv)
3227
{
28+
int i, sock, key, fd, main_prog_fd, jmp_table_fd, hash_map_fd;
3329
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
30+
struct bpf_program *prog;
31+
struct bpf_object *obj;
3432
char filename[256];
33+
const char *title;
3534
FILE *f;
36-
int i, sock, err, id, key = PARSE_IP;
37-
struct bpf_prog_info info = {};
38-
uint32_t info_len = sizeof(info);
3935

4036
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
4137
setrlimit(RLIMIT_MEMLOCK, &r);
4238

43-
if (load_bpf_file(filename)) {
44-
printf("%s", bpf_log_buf);
45-
return 1;
39+
obj = bpf_object__open_file(filename, NULL);
40+
if (libbpf_get_error(obj)) {
41+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
42+
return 0;
43+
}
44+
45+
/* load BPF program */
46+
if (bpf_object__load(obj)) {
47+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
48+
goto cleanup;
49+
}
50+
51+
jmp_table_fd = bpf_object__find_map_fd_by_name(obj, "jmp_table");
52+
hash_map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map");
53+
if (jmp_table_fd < 0 || hash_map_fd < 0) {
54+
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
55+
goto cleanup;
4656
}
4757

48-
/* Test fd array lookup which returns the id of the bpf_prog */
49-
err = bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD, &info, &info_len);
50-
assert(!err);
51-
err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id);
52-
assert(!err);
53-
assert(id == info.id);
58+
bpf_object__for_each_program(prog, obj) {
59+
fd = bpf_program__fd(prog);
60+
61+
title = bpf_program__title(prog, false);
62+
if (sscanf(title, "socket/%d", &key) != 1) {
63+
fprintf(stderr, "ERROR: finding prog failed\n");
64+
goto cleanup;
65+
}
66+
67+
if (key == 0)
68+
main_prog_fd = fd;
69+
else
70+
bpf_map_update_elem(jmp_table_fd, &key, &fd, BPF_ANY);
71+
}
5472

5573
sock = open_raw_sock("lo");
5674

57-
assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4],
75+
/* attach BPF program to socket */
76+
assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &main_prog_fd,
5877
sizeof(__u32)) == 0);
5978

6079
if (argc > 1)
@@ -69,8 +88,8 @@ int main(int argc, char **argv)
6988

7089
sleep(1);
7190
printf("IP src.port -> dst.port bytes packets\n");
72-
while (bpf_map_get_next_key(map_fd[2], &key, &next_key) == 0) {
73-
bpf_map_lookup_elem(map_fd[2], &next_key, &value);
91+
while (bpf_map_get_next_key(hash_map_fd, &key, &next_key) == 0) {
92+
bpf_map_lookup_elem(hash_map_fd, &next_key, &value);
7493
printf("%s.%05d -> %s.%05d %12lld %12lld\n",
7594
inet_ntoa((struct in_addr){htonl(next_key.src)}),
7695
next_key.port16[0],
@@ -80,5 +99,8 @@ int main(int argc, char **argv)
8099
key = next_key;
81100
}
82101
}
102+
103+
cleanup:
104+
bpf_object__close(obj);
83105
return 0;
84106
}

‎samples/bpf/tracex5_user.c‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <stdio.h>
3-
#include <linux/bpf.h>
3+
#include <stdlib.h>
44
#include <unistd.h>
55
#include <linux/filter.h>
66
#include <linux/seccomp.h>
77
#include <sys/prctl.h>
88
#include <bpf/bpf.h>
9-
#include "bpf_load.h"
9+
#include <bpf/libbpf.h>
1010
#include <sys/resource.h>
1111
#include "trace_helpers.h"
1212

13+
#ifdef __mips__
14+
#define MAX_ENTRIES 6000 /* MIPS n64 syscalls start at 5000 */
15+
#else
16+
#define MAX_ENTRIES 1024
17+
#endif
18+
1319
/* install fake seccomp program to enable seccomp code path inside the kernel,
1420
* so that our kprobe attached to seccomp_phase1() can be triggered
1521
*/
@@ -28,16 +34,57 @@ static void install_accept_all_seccomp(void)
2834

2935
int main(int ac, char **argv)
3036
{
31-
FILE *f;
32-
char filename[256];
3337
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
38+
struct bpf_link *link = NULL;
39+
struct bpf_program *prog;
40+
struct bpf_object *obj;
41+
int key, fd, progs_fd;
42+
char filename[256];
43+
const char *title;
44+
FILE *f;
3445

35-
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
3646
setrlimit(RLIMIT_MEMLOCK, &r);
3747

38-
if (load_bpf_file(filename)) {
39-
printf("%s", bpf_log_buf);
40-
return 1;
48+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
49+
obj = bpf_object__open_file(filename, NULL);
50+
if (libbpf_get_error(obj)) {
51+
fprintf(stderr, "ERROR: opening BPF object file failed\n");
52+
return 0;
53+
}
54+
55+
prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
56+
if (!prog) {
57+
printf("finding a prog in obj file failed\n");
58+
goto cleanup;
59+
}
60+
61+
/* load BPF program */
62+
if (bpf_object__load(obj)) {
63+
fprintf(stderr, "ERROR: loading BPF object file failed\n");
64+
goto cleanup;
65+
}
66+
67+
link = bpf_program__attach(prog);
68+
if (libbpf_get_error(link)) {
69+
fprintf(stderr, "ERROR: bpf_program__attach failed\n");
70+
link = NULL;
71+
goto cleanup;
72+
}
73+
74+
progs_fd = bpf_object__find_map_fd_by_name(obj, "progs");
75+
if (progs_fd < 0) {
76+
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
77+
goto cleanup;
78+
}
79+
80+
bpf_object__for_each_program(prog, obj) {
81+
title = bpf_program__title(prog, false);
82+
/* register only syscalls to PROG_ARRAY */
83+
if (sscanf(title, "kprobe/%d", &key) != 1)
84+
continue;
85+
86+
fd = bpf_program__fd(prog);
87+
bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY);
4188
}
4289

4390
install_accept_all_seccomp();
@@ -47,5 +94,8 @@ int main(int ac, char **argv)
4794

4895
read_trace_pipe();
4996

97+
cleanup:
98+
bpf_link__destroy(link);
99+
bpf_object__close(obj);
50100
return 0;
51101
}

0 commit comments

Comments
 (0)