aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe Stringer <joe@ovn.org>2016-12-14 17:05:26 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-12-20 10:00:39 -0500
commit5dc880de6e7c8566fbc8bc5dfc3a922d2d1c5ee3 (patch)
treeb2d6edd5122a4da1c87e5760adbeea14d2323e0d
parent43371c83f382bd495a2294e91a32f30763cfdbef (diff)
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>
-rw-r--r--samples/bpf/libbpf.c21
-rw-r--r--samples/bpf/libbpf.h3
-rw-r--r--tools/lib/bpf/bpf.c23
-rw-r--r--tools/lib/bpf/bpf.h3
4 files changed, 26 insertions, 24 deletions
diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c
index 3391225ad7e9..d9af876b4a2c 100644
--- a/samples/bpf/libbpf.c
+++ b/samples/bpf/libbpf.c
@@ -11,27 +11,6 @@
11#include <arpa/inet.h> 11#include <arpa/inet.h>
12#include "libbpf.h" 12#include "libbpf.h"
13 13
14int 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
25int 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
35int open_raw_sock(const char *name) 14int open_raw_sock(const char *name)
36{ 15{
37 struct sockaddr_ll sll; 16 struct sockaddr_ll sll;
diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h
index cf7d2386d1f9..cc815624aacf 100644
--- a/samples/bpf/libbpf.h
+++ b/samples/bpf/libbpf.h
@@ -6,9 +6,6 @@
6 6
7struct bpf_insn; 7struct bpf_insn;
8 8
9int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
10int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
11
12/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ 9/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */
13 10
14#define BPF_ALU64_REG(OP, DST, SRC) \ 11#define BPF_ALU64_REG(OP, DST, SRC) \
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index d0afb26c2e0f..3ddb58a36d3c 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -167,3 +167,26 @@ int bpf_obj_get(const char *pathname)
167 167
168 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr)); 168 return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
169} 169}
170
171int 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
183int 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}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 7fcdce16fd62..a2f9853dd882 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -41,5 +41,8 @@ int bpf_map_delete_elem(int fd, void *key);
41int bpf_map_get_next_key(int fd, void *key, void *next_key); 41int bpf_map_get_next_key(int fd, void *key, void *next_key);
42int bpf_obj_pin(int fd, const char *pathname); 42int bpf_obj_pin(int fd, const char *pathname);
43int bpf_obj_get(const char *pathname); 43int bpf_obj_get(const char *pathname);
44int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type);
45int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
46
44 47
45#endif 48#endif