]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/commitdiff
tools lib bpf: Add bpf_prog_{attach,detach}
authorJoe Stringer <joe@ovn.org>
Wed, 14 Dec 2016 22:05:26 +0000 (14:05 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 20 Dec 2016 15:00:39 +0000 (12:00 -0300)
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>
samples/bpf/libbpf.c
samples/bpf/libbpf.h
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h

index 3391225ad7e9467c4433ce7a2bbede63f257ab8d..d9af876b4a2cbd1d02c4d8207b3c29840090c8ab 100644 (file)
 #include <arpa/inet.h>
 #include "libbpf.h"
 
-int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
-{
-       union bpf_attr attr = {
-               .target_fd = target_fd,
-               .attach_bpf_fd = prog_fd,
-               .attach_type = type,
-       };
-
-       return syscall(__NR_bpf, BPF_PROG_ATTACH, &attr, sizeof(attr));
-}
-
-int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
-{
-       union bpf_attr attr = {
-               .target_fd = target_fd,
-               .attach_type = type,
-       };
-
-       return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr));
-}
-
 int open_raw_sock(const char *name)
 {
        struct sockaddr_ll sll;
index cf7d2386d1f9bf0b9f38eb9b62f3f735c0c2fa42..cc815624aacf4e24df28d1399e36772971a60e8a 100644 (file)
@@ -6,9 +6,6 @@
 
 struct bpf_insn;
 
-int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
-int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
-
 /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
 
 #define BPF_ALU64_REG(OP, DST, SRC)                            \
index d0afb26c2e0fb72fa83d05eca3eadff2aa6f9c9b..3ddb58a36d3c2534ce6207581a4fee1730c6e6a0 100644 (file)
@@ -167,3 +167,26 @@ int bpf_obj_get(const char *pathname)
 
        return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
 }
+
+int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.target_fd     = target_fd;
+       attr.attach_bpf_fd = prog_fd;
+       attr.attach_type   = type;
+
+       return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
+}
+
+int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
+{
+       union bpf_attr attr;
+
+       bzero(&attr, sizeof(attr));
+       attr.target_fd   = target_fd;
+       attr.attach_type = type;
+
+       return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
+}
index 7fcdce16fd62b6358d4a989eb59cd89182104f52..a2f9853dd88259d810e6506ec5ac48d863bc074b 100644 (file)
@@ -41,5 +41,8 @@ int bpf_map_delete_elem(int fd, void *key);
 int bpf_map_get_next_key(int fd, void *key, void *next_key);
 int bpf_obj_pin(int fd, const char *pathname);
 int bpf_obj_get(const char *pathname);
+int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
+int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
+
 
 #endif