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

tools lib bpf: Add bpf_prog_{attach,detach}

Commit d8c5b17f2bc0 ("samples: bpf: add userspace example for attaching
eBPF programs to cgroups") added these functions to samples/libbpf, but
during this merge all of the samples libbpf functionality is shifting to
tools/lib/bpf. Shift these functions there.

Committer notes:

Use bzero + attr.FIELD = value instead of 'attr = { .FIELD = value, just
like the other wrapper calls to sys_bpf with bpf_attr to make this build
in older toolchais, such as the ones in CentOS 5 and 6.

Signed-off-by: Joe Stringer <joe@ovn.org>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-au2zvtsh55vqeo3v3uw7jr4c@git.kernel.org
Link: https://github.com/joestringer/linux/commit/353e6f298c3d0a92fa8bfa61ff898c5050261a12.patch
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Joe Stringer and committed by
Arnaldo Carvalho de Melo
5dc880de 43371c83

+26 -24
-21
samples/bpf/libbpf.c
··· 11 11 #include <arpa/inet.h> 12 12 #include "libbpf.h" 13 13 14 - int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) 15 - { 16 - union bpf_attr attr = { 17 - .target_fd = target_fd, 18 - .attach_bpf_fd = prog_fd, 19 - .attach_type = type, 20 - }; 21 - 22 - return syscall(__NR_bpf, BPF_PROG_ATTACH, &attr, sizeof(attr)); 23 - } 24 - 25 - int bpf_prog_detach(int target_fd, enum bpf_attach_type type) 26 - { 27 - union bpf_attr attr = { 28 - .target_fd = target_fd, 29 - .attach_type = type, 30 - }; 31 - 32 - return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr)); 33 - } 34 - 35 14 int open_raw_sock(const char *name) 36 15 { 37 16 struct sockaddr_ll sll;
-3
samples/bpf/libbpf.h
··· 6 6 7 7 struct bpf_insn; 8 8 9 - int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type); 10 - int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 11 - 12 9 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ 13 10 14 11 #define BPF_ALU64_REG(OP, DST, SRC) \
+23
tools/lib/bpf/bpf.c
··· 167 167 168 168 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr)); 169 169 } 170 + 171 + int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) 172 + { 173 + union bpf_attr attr; 174 + 175 + bzero(&attr, sizeof(attr)); 176 + attr.target_fd = target_fd; 177 + attr.attach_bpf_fd = prog_fd; 178 + attr.attach_type = type; 179 + 180 + return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)); 181 + } 182 + 183 + int bpf_prog_detach(int target_fd, enum bpf_attach_type type) 184 + { 185 + union bpf_attr attr; 186 + 187 + bzero(&attr, sizeof(attr)); 188 + attr.target_fd = target_fd; 189 + attr.attach_type = type; 190 + 191 + return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr)); 192 + }
+3
tools/lib/bpf/bpf.h
··· 41 41 int bpf_map_get_next_key(int fd, void *key, void *next_key); 42 42 int bpf_obj_pin(int fd, const char *pathname); 43 43 int bpf_obj_get(const char *pathname); 44 + int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type); 45 + int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); 46 + 44 47 45 48 #endif