Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Daniel T. Lee and committed by
Daniel Borkmann
bc1a8597 63841bc0

+103 -31
+2 -2
samples/bpf/Makefile
··· 63 63 fds_example-objs := fds_example.o 64 64 sockex1-objs := sockex1_user.o 65 65 sockex2-objs := sockex2_user.o 66 - sockex3-objs := bpf_load.o sockex3_user.o 66 + sockex3-objs := sockex3_user.o 67 67 tracex1-objs := tracex1_user.o $(TRACE_HELPERS) 68 68 tracex2-objs := tracex2_user.o 69 69 tracex3-objs := tracex3_user.o 70 70 tracex4-objs := tracex4_user.o 71 - tracex5-objs := bpf_load.o tracex5_user.o $(TRACE_HELPERS) 71 + tracex5-objs := tracex5_user.o $(TRACE_HELPERS) 72 72 tracex6-objs := tracex6_user.o 73 73 tracex7-objs := tracex7_user.o 74 74 test_probe_write_user-objs := bpf_load.o test_probe_write_user_user.o
+43 -21
samples/bpf/sockex3_user.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include <stdio.h> 3 3 #include <assert.h> 4 - #include <linux/bpf.h> 5 4 #include <bpf/bpf.h> 6 - #include "bpf_load.h" 5 + #include <bpf/libbpf.h> 7 6 #include "sock_example.h" 8 7 #include <unistd.h> 9 8 #include <arpa/inet.h> 10 9 #include <sys/resource.h> 11 - 12 - #define PARSE_IP 3 13 - #define PARSE_IP_PROG_FD (prog_fd[0]) 14 - #define PROG_ARRAY_FD (map_fd[0]) 15 10 16 11 struct flow_key_record { 17 12 __be32 src; ··· 25 30 26 31 int main(int argc, char **argv) 27 32 { 33 + int i, sock, key, fd, main_prog_fd, jmp_table_fd, hash_map_fd; 28 34 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 35 + struct bpf_program *prog; 36 + struct bpf_object *obj; 29 37 char filename[256]; 38 + const char *title; 30 39 FILE *f; 31 - int i, sock, err, id, key = PARSE_IP; 32 - struct bpf_prog_info info = {}; 33 - uint32_t info_len = sizeof(info); 34 40 35 41 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 36 42 setrlimit(RLIMIT_MEMLOCK, &r); 37 43 38 - if (load_bpf_file(filename)) { 39 - printf("%s", bpf_log_buf); 40 - return 1; 44 + obj = bpf_object__open_file(filename, NULL); 45 + if (libbpf_get_error(obj)) { 46 + fprintf(stderr, "ERROR: opening BPF object file failed\n"); 47 + return 0; 41 48 } 42 49 43 - /* Test fd array lookup which returns the id of the bpf_prog */ 44 - err = bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD, &info, &info_len); 45 - assert(!err); 46 - err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id); 47 - assert(!err); 48 - assert(id == info.id); 50 + /* load BPF program */ 51 + if (bpf_object__load(obj)) { 52 + fprintf(stderr, "ERROR: loading BPF object file failed\n"); 53 + goto cleanup; 54 + } 55 + 56 + jmp_table_fd = bpf_object__find_map_fd_by_name(obj, "jmp_table"); 57 + hash_map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map"); 58 + if (jmp_table_fd < 0 || hash_map_fd < 0) { 59 + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); 60 + goto cleanup; 61 + } 62 + 63 + bpf_object__for_each_program(prog, obj) { 64 + fd = bpf_program__fd(prog); 65 + 66 + title = bpf_program__title(prog, false); 67 + if (sscanf(title, "socket/%d", &key) != 1) { 68 + fprintf(stderr, "ERROR: finding prog failed\n"); 69 + goto cleanup; 70 + } 71 + 72 + if (key == 0) 73 + main_prog_fd = fd; 74 + else 75 + bpf_map_update_elem(jmp_table_fd, &key, &fd, BPF_ANY); 76 + } 49 77 50 78 sock = open_raw_sock("lo"); 51 79 52 - assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4], 80 + /* attach BPF program to socket */ 81 + assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &main_prog_fd, 53 82 sizeof(__u32)) == 0); 54 83 55 84 if (argc > 1) ··· 88 69 89 70 sleep(1); 90 71 printf("IP src.port -> dst.port bytes packets\n"); 91 - while (bpf_map_get_next_key(map_fd[2], &key, &next_key) == 0) { 92 - bpf_map_lookup_elem(map_fd[2], &next_key, &value); 72 + while (bpf_map_get_next_key(hash_map_fd, &key, &next_key) == 0) { 73 + bpf_map_lookup_elem(hash_map_fd, &next_key, &value); 93 74 printf("%s.%05d -> %s.%05d %12lld %12lld\n", 94 75 inet_ntoa((struct in_addr){htonl(next_key.src)}), 95 76 next_key.port16[0], ··· 99 80 key = next_key; 100 81 } 101 82 } 83 + 84 + cleanup: 85 + bpf_object__close(obj); 102 86 return 0; 103 87 }
+58 -8
samples/bpf/tracex5_user.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include <stdio.h> 3 - #include <linux/bpf.h> 3 + #include <stdlib.h> 4 4 #include <unistd.h> 5 5 #include <linux/filter.h> 6 6 #include <linux/seccomp.h> 7 7 #include <sys/prctl.h> 8 8 #include <bpf/bpf.h> 9 - #include "bpf_load.h" 9 + #include <bpf/libbpf.h> 10 10 #include <sys/resource.h> 11 11 #include "trace_helpers.h" 12 + 13 + #ifdef __mips__ 14 + #define MAX_ENTRIES 6000 /* MIPS n64 syscalls start at 5000 */ 15 + #else 16 + #define MAX_ENTRIES 1024 17 + #endif 12 18 13 19 /* install fake seccomp program to enable seccomp code path inside the kernel, 14 20 * so that our kprobe attached to seccomp_phase1() can be triggered ··· 34 28 35 29 int main(int ac, char **argv) 36 30 { 37 - FILE *f; 38 - char filename[256]; 39 31 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 32 + struct bpf_link *link = NULL; 33 + struct bpf_program *prog; 34 + struct bpf_object *obj; 35 + int key, fd, progs_fd; 36 + char filename[256]; 37 + const char *title; 38 + FILE *f; 40 39 41 - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 42 40 setrlimit(RLIMIT_MEMLOCK, &r); 43 41 44 - if (load_bpf_file(filename)) { 45 - printf("%s", bpf_log_buf); 46 - return 1; 42 + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 43 + obj = bpf_object__open_file(filename, NULL); 44 + if (libbpf_get_error(obj)) { 45 + fprintf(stderr, "ERROR: opening BPF object file failed\n"); 46 + return 0; 47 + } 48 + 49 + prog = bpf_object__find_program_by_name(obj, "bpf_prog1"); 50 + if (!prog) { 51 + printf("finding a prog in obj file failed\n"); 52 + goto cleanup; 53 + } 54 + 55 + /* load BPF program */ 56 + if (bpf_object__load(obj)) { 57 + fprintf(stderr, "ERROR: loading BPF object file failed\n"); 58 + goto cleanup; 59 + } 60 + 61 + link = bpf_program__attach(prog); 62 + if (libbpf_get_error(link)) { 63 + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); 64 + link = NULL; 65 + goto cleanup; 66 + } 67 + 68 + progs_fd = bpf_object__find_map_fd_by_name(obj, "progs"); 69 + if (progs_fd < 0) { 70 + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); 71 + goto cleanup; 72 + } 73 + 74 + bpf_object__for_each_program(prog, obj) { 75 + title = bpf_program__title(prog, false); 76 + /* register only syscalls to PROG_ARRAY */ 77 + if (sscanf(title, "kprobe/%d", &key) != 1) 78 + continue; 79 + 80 + fd = bpf_program__fd(prog); 81 + bpf_map_update_elem(progs_fd, &key, &fd, BPF_ANY); 47 82 } 48 83 49 84 install_accept_all_seccomp(); ··· 94 47 95 48 read_trace_pipe(); 96 49 50 + cleanup: 51 + bpf_link__destroy(link); 52 + bpf_object__close(obj); 97 53 return 0; 98 54 }