diff options
author | Joe Stringer <joe@ovn.org> | 2016-12-14 17:43:39 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-12-20 10:00:38 -0500 |
commit | 43371c83f382bd495a2294e91a32f30763cfdbef (patch) | |
tree | 2b8dbd0bcce4525143acef07afaef33b8071fb6e | |
parent | ed6c166cc7dc329736cace3affd2df984fb22ec8 (diff) |
samples/bpf: Switch over to libbpf
Now that libbpf under tools/lib/bpf/* is synced with the version from
samples/bpf, we can get rid most of the libbpf library here.
Committer notes:
Built it in a docker fedora rawhide container and ran it in the f25 host, seems
to work just like it did before this patch, i.e. the switch to tools/lib/bpf/
doesn't seem to have introduced problems and Joe said he tested it with
all the entries in samples/bpf/ and other code he found:
[root@f5065a7d6272 linux]# make -j4 O=/tmp/build/linux headers_install
<SNIP>
[root@f5065a7d6272 linux]# rm -rf /tmp/build/linux/samples/bpf/
[root@f5065a7d6272 linux]# make -j4 O=/tmp/build/linux samples/bpf/
make[1]: Entering directory '/tmp/build/linux'
CHK include/config/kernel.release
HOSTCC scripts/basic/fixdep
GEN ./Makefile
CHK include/generated/uapi/linux/version.h
Using /git/linux as source for kernel
CHK include/generated/utsrelease.h
HOSTCC scripts/basic/bin2c
HOSTCC arch/x86/tools/relocs_32.o
HOSTCC arch/x86/tools/relocs_64.o
LD samples/bpf/built-in.o
<SNIP>
HOSTCC samples/bpf/fds_example.o
HOSTCC samples/bpf/sockex1_user.o
/git/linux/samples/bpf/fds_example.c: In function 'bpf_prog_create':
/git/linux/samples/bpf/fds_example.c:63:6: warning: passing argument 2 of 'bpf_load_program' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
insns, insns_cnt, "GPL", 0,
^~~~~
In file included from /git/linux/samples/bpf/libbpf.h:5:0,
from /git/linux/samples/bpf/bpf_load.h:4,
from /git/linux/samples/bpf/fds_example.c:15:
/git/linux/tools/lib/bpf/bpf.h:31:5: note: expected 'struct bpf_insn *' but argument is of type 'const struct bpf_insn *'
int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
^~~~~~~~~~~~~~~~
HOSTCC samples/bpf/sockex2_user.o
<SNIP>
HOSTCC samples/bpf/xdp_tx_iptunnel_user.o
clang -nostdinc -isystem /usr/lib/gcc/x86_64-redhat-linux/6.2.1/include -I/git/linux/arch/x86/include -I./arch/x86/include/generated/uapi -I./arch/x86/include/generated -I/git/linux/include -I./include -I/git/linux/arch/x86/include/uapi -I/git/linux/include/uapi -I./include/generated/uapi -include /git/linux/include/linux/kconfig.h \
-D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-address-of-packed-member -Wno-tautological-compare \
-O2 -emit-llvm -c /git/linux/samples/bpf/sockex1_kern.c -o -| llc -march=bpf -filetype=obj -o samples/bpf/sockex1_kern.o
HOSTLD samples/bpf/tc_l2_redirect
<SNIP>
HOSTLD samples/bpf/lwt_len_hist
HOSTLD samples/bpf/xdp_tx_iptunnel
make[1]: Leaving directory '/tmp/build/linux'
[root@f5065a7d6272 linux]#
And then, in the host:
[root@jouet bpf]# mount | grep "docker.*devicemapper\/"
/dev/mapper/docker-253:0-1705076-9bd8aa1e0af33adce89ff42090847868ca676932878942be53941a06ec5923f9 on /var/lib/docker/devicemapper/mnt/9bd8aa1e0af33adce89ff42090847868ca676932878942be53941a06ec5923f9 type xfs (rw,relatime,context="system_u:object_r:container_file_t:s0:c73,c276",nouuid,attr2,inode64,sunit=1024,swidth=1024,noquota)
[root@jouet bpf]# cd /var/lib/docker/devicemapper/mnt/9bd8aa1e0af33adce89ff42090847868ca676932878942be53941a06ec5923f9/rootfs/tmp/build/linux/samples/bpf/
[root@jouet bpf]# file offwaketime
offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=f423d171e0487b2f802b6a792657f0f3c8f6d155, not stripped
[root@jouet bpf]# readelf -SW offwaketime
offwaketime offwaketime_kern.o offwaketime_user.o
[root@jouet bpf]# readelf -SW offwaketime_kern.o
There are 11 section headers, starting at offset 0x700:
Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[ 0] NULL 0000000000000000 000000 000000 00 0 0 0
[ 1] .strtab STRTAB 0000000000000000 000658 0000a8 00 0 0 1
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8
[ 4] .relkprobe/try_to_wake_up REL 0000000000000000 0005a8 000020 10 10 3 8
[ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8
[ 6] .reltracepoint/sched/sched_switch REL 0000000000000000 0005c8 000090 10 10 5 8
[ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4
[ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1
[ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4
[10] .symtab SYMTAB 0000000000000000 000488 000120 18 1 4 8
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
[root@jouet bpf]# ./offwaketime | head -3
qemu-system-x86;entry_SYSCALL_64_fastpath;sys_ppoll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;hrtimer_wakeup;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter_state;cpuidle_enter;call_cpuidle;cpu_startup_entry;rest_init;start_kernel;x86_64_start_reservations;x86_64_start_kernel;start_cpu;;swapper/0 4
firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 1
swapper/2;start_cpu;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 61
[root@jouet bpf]#
Signed-off-by: Joe Stringer <joe@ovn.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: netdev@vger.kernel.org
Link: https://github.com/joestringer/linux/commit/5c40f54a52b1f437123c81e21873f4b4b1f9bd55.patch
Link: http://lkml.kernel.org/n/tip-xr8twtx7sjh5821g8qw47yxk@git.kernel.org
[ Use -I$(srctree)/tools/lib/ to support out of source code tree builds, as noticed by Wang Nan ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r-- | samples/bpf/Makefile | 68 | ||||
-rw-r--r-- | samples/bpf/README.rst | 4 | ||||
-rw-r--r-- | samples/bpf/bpf_load.c | 3 | ||||
-rw-r--r-- | samples/bpf/fds_example.c | 3 | ||||
-rw-r--r-- | samples/bpf/libbpf.c | 111 | ||||
-rw-r--r-- | samples/bpf/libbpf.h | 19 | ||||
-rw-r--r-- | samples/bpf/sock_example.c | 3 | ||||
-rw-r--r-- | samples/bpf/test_cgrp2_attach.c | 3 | ||||
-rw-r--r-- | samples/bpf/test_cgrp2_attach2.c | 3 | ||||
-rw-r--r-- | samples/bpf/test_cgrp2_sock.c | 3 |
10 files changed, 52 insertions, 168 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index f2219c1489e5..81b0ef2f7994 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile | |||
@@ -35,40 +35,43 @@ hostprogs-y += tc_l2_redirect | |||
35 | hostprogs-y += lwt_len_hist | 35 | hostprogs-y += lwt_len_hist |
36 | hostprogs-y += xdp_tx_iptunnel | 36 | hostprogs-y += xdp_tx_iptunnel |
37 | 37 | ||
38 | test_lru_dist-objs := test_lru_dist.o libbpf.o | 38 | # Libbpf dependencies |
39 | sock_example-objs := sock_example.o libbpf.o | 39 | LIBBPF := libbpf.o ../../tools/lib/bpf/bpf.o |
40 | fds_example-objs := bpf_load.o libbpf.o fds_example.o | 40 | |
41 | sockex1-objs := bpf_load.o libbpf.o sockex1_user.o | 41 | test_lru_dist-objs := test_lru_dist.o $(LIBBPF) |
42 | sockex2-objs := bpf_load.o libbpf.o sockex2_user.o | 42 | sock_example-objs := sock_example.o $(LIBBPF) |
43 | sockex3-objs := bpf_load.o libbpf.o sockex3_user.o | 43 | fds_example-objs := bpf_load.o $(LIBBPF) fds_example.o |
44 | tracex1-objs := bpf_load.o libbpf.o tracex1_user.o | 44 | sockex1-objs := bpf_load.o $(LIBBPF) sockex1_user.o |
45 | tracex2-objs := bpf_load.o libbpf.o tracex2_user.o | 45 | sockex2-objs := bpf_load.o $(LIBBPF) sockex2_user.o |
46 | tracex3-objs := bpf_load.o libbpf.o tracex3_user.o | 46 | sockex3-objs := bpf_load.o $(LIBBPF) sockex3_user.o |
47 | tracex4-objs := bpf_load.o libbpf.o tracex4_user.o | 47 | tracex1-objs := bpf_load.o $(LIBBPF) tracex1_user.o |
48 | tracex5-objs := bpf_load.o libbpf.o tracex5_user.o | 48 | tracex2-objs := bpf_load.o $(LIBBPF) tracex2_user.o |
49 | tracex6-objs := bpf_load.o libbpf.o tracex6_user.o | 49 | tracex3-objs := bpf_load.o $(LIBBPF) tracex3_user.o |
50 | test_probe_write_user-objs := bpf_load.o libbpf.o test_probe_write_user_user.o | 50 | tracex4-objs := bpf_load.o $(LIBBPF) tracex4_user.o |
51 | trace_output-objs := bpf_load.o libbpf.o trace_output_user.o | 51 | tracex5-objs := bpf_load.o $(LIBBPF) tracex5_user.o |
52 | lathist-objs := bpf_load.o libbpf.o lathist_user.o | 52 | tracex6-objs := bpf_load.o $(LIBBPF) tracex6_user.o |
53 | offwaketime-objs := bpf_load.o libbpf.o offwaketime_user.o | 53 | test_probe_write_user-objs := bpf_load.o $(LIBBPF) test_probe_write_user_user.o |
54 | spintest-objs := bpf_load.o libbpf.o spintest_user.o | 54 | trace_output-objs := bpf_load.o $(LIBBPF) trace_output_user.o |
55 | map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o | 55 | lathist-objs := bpf_load.o $(LIBBPF) lathist_user.o |
56 | test_overhead-objs := bpf_load.o libbpf.o test_overhead_user.o | 56 | offwaketime-objs := bpf_load.o $(LIBBPF) offwaketime_user.o |
57 | test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o | 57 | spintest-objs := bpf_load.o $(LIBBPF) spintest_user.o |
58 | test_cgrp2_attach-objs := libbpf.o test_cgrp2_attach.o | 58 | map_perf_test-objs := bpf_load.o $(LIBBPF) map_perf_test_user.o |
59 | test_cgrp2_attach2-objs := libbpf.o test_cgrp2_attach2.o cgroup_helpers.o | 59 | test_overhead-objs := bpf_load.o $(LIBBPF) test_overhead_user.o |
60 | test_cgrp2_sock-objs := libbpf.o test_cgrp2_sock.o | 60 | test_cgrp2_array_pin-objs := $(LIBBPF) test_cgrp2_array_pin.o |
61 | test_cgrp2_sock2-objs := bpf_load.o libbpf.o test_cgrp2_sock2.o | 61 | test_cgrp2_attach-objs := $(LIBBPF) test_cgrp2_attach.o |
62 | xdp1-objs := bpf_load.o libbpf.o xdp1_user.o | 62 | test_cgrp2_attach2-objs := $(LIBBPF) test_cgrp2_attach2.o cgroup_helpers.o |
63 | test_cgrp2_sock-objs := $(LIBBPF) test_cgrp2_sock.o | ||
64 | test_cgrp2_sock2-objs := bpf_load.o $(LIBBPF) test_cgrp2_sock2.o | ||
65 | xdp1-objs := bpf_load.o $(LIBBPF) xdp1_user.o | ||
63 | # reuse xdp1 source intentionally | 66 | # reuse xdp1 source intentionally |
64 | xdp2-objs := bpf_load.o libbpf.o xdp1_user.o | 67 | xdp2-objs := bpf_load.o $(LIBBPF) xdp1_user.o |
65 | test_current_task_under_cgroup-objs := bpf_load.o libbpf.o cgroup_helpers.o \ | 68 | test_current_task_under_cgroup-objs := bpf_load.o $(LIBBPF) cgroup_helpers.o \ |
66 | test_current_task_under_cgroup_user.o | 69 | test_current_task_under_cgroup_user.o |
67 | trace_event-objs := bpf_load.o libbpf.o trace_event_user.o | 70 | trace_event-objs := bpf_load.o $(LIBBPF) trace_event_user.o |
68 | sampleip-objs := bpf_load.o libbpf.o sampleip_user.o | 71 | sampleip-objs := bpf_load.o $(LIBBPF) sampleip_user.o |
69 | tc_l2_redirect-objs := bpf_load.o libbpf.o tc_l2_redirect_user.o | 72 | tc_l2_redirect-objs := bpf_load.o $(LIBBPF) tc_l2_redirect_user.o |
70 | lwt_len_hist-objs := bpf_load.o libbpf.o lwt_len_hist_user.o | 73 | lwt_len_hist-objs := bpf_load.o $(LIBBPF) lwt_len_hist_user.o |
71 | xdp_tx_iptunnel-objs := bpf_load.o libbpf.o xdp_tx_iptunnel_user.o | 74 | xdp_tx_iptunnel-objs := bpf_load.o $(LIBBPF) xdp_tx_iptunnel_user.o |
72 | 75 | ||
73 | # Tell kbuild to always build the programs | 76 | # Tell kbuild to always build the programs |
74 | always := $(hostprogs-y) | 77 | always := $(hostprogs-y) |
@@ -104,6 +107,7 @@ always += lwt_len_hist_kern.o | |||
104 | always += xdp_tx_iptunnel_kern.o | 107 | always += xdp_tx_iptunnel_kern.o |
105 | 108 | ||
106 | HOSTCFLAGS += -I$(objtree)/usr/include | 109 | HOSTCFLAGS += -I$(objtree)/usr/include |
110 | HOSTCFLAGS += -I$(srctree)/tools/lib/ | ||
107 | HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ | 111 | HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ |
108 | 112 | ||
109 | HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable | 113 | HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable |
diff --git a/samples/bpf/README.rst b/samples/bpf/README.rst index a43eae3f0551..79f9a58f1872 100644 --- a/samples/bpf/README.rst +++ b/samples/bpf/README.rst | |||
@@ -1,8 +1,8 @@ | |||
1 | eBPF sample programs | 1 | eBPF sample programs |
2 | ==================== | 2 | ==================== |
3 | 3 | ||
4 | This directory contains a mini eBPF library, test stubs, verifier | 4 | This directory contains a test stubs, verifier test-suite and examples |
5 | test-suite and examples for using eBPF. | 5 | for using eBPF. The examples use libbpf from tools/lib/bpf. |
6 | 6 | ||
7 | Build dependencies | 7 | Build dependencies |
8 | ================== | 8 | ================== |
diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c index f5b186c46b7c..1bfb43394013 100644 --- a/samples/bpf/bpf_load.c +++ b/samples/bpf/bpf_load.c | |||
@@ -66,6 +66,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) | |||
66 | bool is_perf_event = strncmp(event, "perf_event", 10) == 0; | 66 | bool is_perf_event = strncmp(event, "perf_event", 10) == 0; |
67 | bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0; | 67 | bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0; |
68 | bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0; | 68 | bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0; |
69 | size_t insns_cnt = size / sizeof(struct bpf_insn); | ||
69 | enum bpf_prog_type prog_type; | 70 | enum bpf_prog_type prog_type; |
70 | char buf[256]; | 71 | char buf[256]; |
71 | int fd, efd, err, id; | 72 | int fd, efd, err, id; |
@@ -95,7 +96,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) | |||
95 | return -1; | 96 | return -1; |
96 | } | 97 | } |
97 | 98 | ||
98 | fd = bpf_load_program(prog_type, prog, size, license, kern_version, | 99 | fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version, |
99 | bpf_log_buf, BPF_LOG_BUF_SIZE); | 100 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
100 | if (fd < 0) { | 101 | if (fd < 0) { |
101 | printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf); | 102 | printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf); |
diff --git a/samples/bpf/fds_example.c b/samples/bpf/fds_example.c index 8a4fc4ef3993..6245062844d1 100644 --- a/samples/bpf/fds_example.c +++ b/samples/bpf/fds_example.c | |||
@@ -53,13 +53,14 @@ static int bpf_prog_create(const char *object) | |||
53 | BPF_MOV64_IMM(BPF_REG_0, 1), | 53 | BPF_MOV64_IMM(BPF_REG_0, 1), |
54 | BPF_EXIT_INSN(), | 54 | BPF_EXIT_INSN(), |
55 | }; | 55 | }; |
56 | size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn); | ||
56 | 57 | ||
57 | if (object) { | 58 | if (object) { |
58 | assert(!load_bpf_file((char *)object)); | 59 | assert(!load_bpf_file((char *)object)); |
59 | return prog_fd[0]; | 60 | return prog_fd[0]; |
60 | } else { | 61 | } else { |
61 | return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, | 62 | return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, |
62 | insns, sizeof(insns), "GPL", 0, | 63 | insns, insns_cnt, "GPL", 0, |
63 | bpf_log_buf, BPF_LOG_BUF_SIZE); | 64 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
64 | } | 65 | } |
65 | } | 66 | } |
diff --git a/samples/bpf/libbpf.c b/samples/bpf/libbpf.c index 6f076abdca35..3391225ad7e9 100644 --- a/samples/bpf/libbpf.c +++ b/samples/bpf/libbpf.c | |||
@@ -4,8 +4,6 @@ | |||
4 | #include <linux/unistd.h> | 4 | #include <linux/unistd.h> |
5 | #include <unistd.h> | 5 | #include <unistd.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <linux/netlink.h> | ||
8 | #include <linux/bpf.h> | ||
9 | #include <errno.h> | 7 | #include <errno.h> |
10 | #include <net/ethernet.h> | 8 | #include <net/ethernet.h> |
11 | #include <net/if.h> | 9 | #include <net/if.h> |
@@ -13,96 +11,6 @@ | |||
13 | #include <arpa/inet.h> | 11 | #include <arpa/inet.h> |
14 | #include "libbpf.h" | 12 | #include "libbpf.h" |
15 | 13 | ||
16 | static __u64 ptr_to_u64(void *ptr) | ||
17 | { | ||
18 | return (__u64) (unsigned long) ptr; | ||
19 | } | ||
20 | |||
21 | int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, | ||
22 | int max_entries, int map_flags) | ||
23 | { | ||
24 | union bpf_attr attr = { | ||
25 | .map_type = map_type, | ||
26 | .key_size = key_size, | ||
27 | .value_size = value_size, | ||
28 | .max_entries = max_entries, | ||
29 | .map_flags = map_flags, | ||
30 | }; | ||
31 | |||
32 | return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); | ||
33 | } | ||
34 | |||
35 | int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags) | ||
36 | { | ||
37 | union bpf_attr attr = { | ||
38 | .map_fd = fd, | ||
39 | .key = ptr_to_u64(key), | ||
40 | .value = ptr_to_u64(value), | ||
41 | .flags = flags, | ||
42 | }; | ||
43 | |||
44 | return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); | ||
45 | } | ||
46 | |||
47 | int bpf_map_lookup_elem(int fd, void *key, void *value) | ||
48 | { | ||
49 | union bpf_attr attr = { | ||
50 | .map_fd = fd, | ||
51 | .key = ptr_to_u64(key), | ||
52 | .value = ptr_to_u64(value), | ||
53 | }; | ||
54 | |||
55 | return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); | ||
56 | } | ||
57 | |||
58 | int bpf_map_delete_elem(int fd, void *key) | ||
59 | { | ||
60 | union bpf_attr attr = { | ||
61 | .map_fd = fd, | ||
62 | .key = ptr_to_u64(key), | ||
63 | }; | ||
64 | |||
65 | return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); | ||
66 | } | ||
67 | |||
68 | int bpf_map_get_next_key(int fd, void *key, void *next_key) | ||
69 | { | ||
70 | union bpf_attr attr = { | ||
71 | .map_fd = fd, | ||
72 | .key = ptr_to_u64(key), | ||
73 | .next_key = ptr_to_u64(next_key), | ||
74 | }; | ||
75 | |||
76 | return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); | ||
77 | } | ||
78 | |||
79 | #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u)) | ||
80 | |||
81 | int bpf_load_program(enum bpf_prog_type prog_type, | ||
82 | const struct bpf_insn *insns, int prog_len, | ||
83 | const char *license, int kern_version, | ||
84 | char *log_buf, size_t log_buf_sz) | ||
85 | { | ||
86 | union bpf_attr attr = { | ||
87 | .prog_type = prog_type, | ||
88 | .insns = ptr_to_u64((void *) insns), | ||
89 | .insn_cnt = prog_len / sizeof(struct bpf_insn), | ||
90 | .license = ptr_to_u64((void *) license), | ||
91 | .log_buf = ptr_to_u64(log_buf), | ||
92 | .log_size = log_buf_sz, | ||
93 | .log_level = 1, | ||
94 | }; | ||
95 | |||
96 | /* assign one field outside of struct init to make sure any | ||
97 | * padding is zero initialized | ||
98 | */ | ||
99 | attr.kern_version = kern_version; | ||
100 | |||
101 | log_buf[0] = 0; | ||
102 | |||
103 | return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); | ||
104 | } | ||
105 | |||
106 | int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) | 14 | int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type) |
107 | { | 15 | { |
108 | union bpf_attr attr = { | 16 | union bpf_attr attr = { |
@@ -124,25 +32,6 @@ int bpf_prog_detach(int target_fd, enum bpf_attach_type type) | |||
124 | return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr)); | 32 | return syscall(__NR_bpf, BPF_PROG_DETACH, &attr, sizeof(attr)); |
125 | } | 33 | } |
126 | 34 | ||
127 | int bpf_obj_pin(int fd, const char *pathname) | ||
128 | { | ||
129 | union bpf_attr attr = { | ||
130 | .pathname = ptr_to_u64((void *)pathname), | ||
131 | .bpf_fd = fd, | ||
132 | }; | ||
133 | |||
134 | return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr)); | ||
135 | } | ||
136 | |||
137 | int bpf_obj_get(const char *pathname) | ||
138 | { | ||
139 | union bpf_attr attr = { | ||
140 | .pathname = ptr_to_u64((void *)pathname), | ||
141 | }; | ||
142 | |||
143 | return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr)); | ||
144 | } | ||
145 | |||
146 | int open_raw_sock(const char *name) | 35 | int open_raw_sock(const char *name) |
147 | { | 36 | { |
148 | struct sockaddr_ll sll; | 37 | struct sockaddr_ll sll; |
diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h index 20e3457857ca..cf7d2386d1f9 100644 --- a/samples/bpf/libbpf.h +++ b/samples/bpf/libbpf.h | |||
@@ -2,28 +2,13 @@ | |||
2 | #ifndef __LIBBPF_H | 2 | #ifndef __LIBBPF_H |
3 | #define __LIBBPF_H | 3 | #define __LIBBPF_H |
4 | 4 | ||
5 | struct bpf_insn; | 5 | #include <bpf/bpf.h> |
6 | |||
7 | int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, | ||
8 | int max_entries, int map_flags); | ||
9 | int bpf_map_update_elem(int fd, void *key, void *value, unsigned long long flags); | ||
10 | int bpf_map_lookup_elem(int fd, void *key, void *value); | ||
11 | int bpf_map_delete_elem(int fd, void *key); | ||
12 | int bpf_map_get_next_key(int fd, void *key, void *next_key); | ||
13 | 6 | ||
14 | int bpf_load_program(enum bpf_prog_type prog_type, | 7 | struct bpf_insn; |
15 | const struct bpf_insn *insns, int insn_len, | ||
16 | const char *license, int kern_version, | ||
17 | char *log_buf, size_t log_buf_sz); | ||
18 | 8 | ||
19 | int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type); | 9 | int bpf_prog_attach(int prog_fd, int attachable_fd, enum bpf_attach_type type); |
20 | int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); | 10 | int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type); |
21 | 11 | ||
22 | int bpf_obj_pin(int fd, const char *pathname); | ||
23 | int bpf_obj_get(const char *pathname); | ||
24 | |||
25 | #define BPF_LOG_BUF_SIZE (256 * 1024) | ||
26 | |||
27 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ | 12 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ |
28 | 13 | ||
29 | #define BPF_ALU64_REG(OP, DST, SRC) \ | 14 | #define BPF_ALU64_REG(OP, DST, SRC) \ |
diff --git a/samples/bpf/sock_example.c b/samples/bpf/sock_example.c index d6b91e9a38ad..5546f8aac37e 100644 --- a/samples/bpf/sock_example.c +++ b/samples/bpf/sock_example.c | |||
@@ -56,8 +56,9 @@ static int test_sock(void) | |||
56 | BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */ | 56 | BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */ |
57 | BPF_EXIT_INSN(), | 57 | BPF_EXIT_INSN(), |
58 | }; | 58 | }; |
59 | size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); | ||
59 | 60 | ||
60 | prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, sizeof(prog), | 61 | prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog, insns_cnt, |
61 | "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE); | 62 | "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE); |
62 | if (prog_fd < 0) { | 63 | if (prog_fd < 0) { |
63 | printf("failed to load prog '%s'\n", strerror(errno)); | 64 | printf("failed to load prog '%s'\n", strerror(errno)); |
diff --git a/samples/bpf/test_cgrp2_attach.c b/samples/bpf/test_cgrp2_attach.c index 8283ef86d392..504058631ffc 100644 --- a/samples/bpf/test_cgrp2_attach.c +++ b/samples/bpf/test_cgrp2_attach.c | |||
@@ -68,9 +68,10 @@ static int prog_load(int map_fd, int verdict) | |||
68 | BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ | 68 | BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ |
69 | BPF_EXIT_INSN(), | 69 | BPF_EXIT_INSN(), |
70 | }; | 70 | }; |
71 | size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); | ||
71 | 72 | ||
72 | return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, | 73 | return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, |
73 | prog, sizeof(prog), "GPL", 0, | 74 | prog, insns_cnt, "GPL", 0, |
74 | bpf_log_buf, BPF_LOG_BUF_SIZE); | 75 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
75 | } | 76 | } |
76 | 77 | ||
diff --git a/samples/bpf/test_cgrp2_attach2.c b/samples/bpf/test_cgrp2_attach2.c index fc6092fdc3b0..6e69be37f87f 100644 --- a/samples/bpf/test_cgrp2_attach2.c +++ b/samples/bpf/test_cgrp2_attach2.c | |||
@@ -41,9 +41,10 @@ static int prog_load(int verdict) | |||
41 | BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ | 41 | BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ |
42 | BPF_EXIT_INSN(), | 42 | BPF_EXIT_INSN(), |
43 | }; | 43 | }; |
44 | size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); | ||
44 | 45 | ||
45 | ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, | 46 | ret = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB, |
46 | prog, sizeof(prog), "GPL", 0, | 47 | prog, insns_cnt, "GPL", 0, |
47 | bpf_log_buf, BPF_LOG_BUF_SIZE); | 48 | bpf_log_buf, BPF_LOG_BUF_SIZE); |
48 | 49 | ||
49 | if (ret < 0) { | 50 | if (ret < 0) { |
diff --git a/samples/bpf/test_cgrp2_sock.c b/samples/bpf/test_cgrp2_sock.c index 43b4bde5d05c..0791b949cbe4 100644 --- a/samples/bpf/test_cgrp2_sock.c +++ b/samples/bpf/test_cgrp2_sock.c | |||
@@ -35,8 +35,9 @@ static int prog_load(int idx) | |||
35 | BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = verdict */ | 35 | BPF_MOV64_IMM(BPF_REG_0, 1), /* r0 = verdict */ |
36 | BPF_EXIT_INSN(), | 36 | BPF_EXIT_INSN(), |
37 | }; | 37 | }; |
38 | size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); | ||
38 | 39 | ||
39 | return bpf_load_program(BPF_PROG_TYPE_CGROUP_SOCK, prog, sizeof(prog), | 40 | return bpf_load_program(BPF_PROG_TYPE_CGROUP_SOCK, prog, insns_cnt, |
40 | "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE); | 41 | "GPL", 0, bpf_log_buf, BPF_LOG_BUF_SIZE); |
41 | } | 42 | } |
42 | 43 | ||