diff options
author | Jakub Kicinski <jakub.kicinski@netronome.com> | 2019-02-27 22:04:13 -0500 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2019-02-28 18:53:45 -0500 |
commit | 1a9b268c90286cae99051353cb7dfb53ffd82676 (patch) | |
tree | 13672d85bbaeeab4e52a7b0d47199f53f43cc1b7 /samples | |
parent | f74a53d9a567f6bc6f6d8460e84c76bd2a45d016 (diff) |
samples: bpf: use libbpf where easy
Some samples don't really need the magic of bpf_load,
switch them to libbpf.
v2: - specify program types.
Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/bpf/Makefile | 6 | ||||
-rw-r--r-- | samples/bpf/fds_example.c | 10 | ||||
-rw-r--r-- | samples/bpf/sockex1_user.c | 23 | ||||
-rw-r--r-- | samples/bpf/sockex2_user.c | 21 |
4 files changed, 35 insertions, 25 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 4dd98100678e..0c62ac39c697 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile | |||
@@ -59,9 +59,9 @@ LIBBPF = $(TOOLS_PATH)/lib/bpf/libbpf.a | |||
59 | CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o | 59 | CGROUP_HELPERS := ../../tools/testing/selftests/bpf/cgroup_helpers.o |
60 | TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o | 60 | TRACE_HELPERS := ../../tools/testing/selftests/bpf/trace_helpers.o |
61 | 61 | ||
62 | fds_example-objs := bpf_load.o fds_example.o | 62 | fds_example-objs := fds_example.o |
63 | sockex1-objs := bpf_load.o sockex1_user.o | 63 | sockex1-objs := sockex1_user.o |
64 | sockex2-objs := bpf_load.o sockex2_user.o | 64 | sockex2-objs := sockex2_user.o |
65 | sockex3-objs := bpf_load.o sockex3_user.o | 65 | sockex3-objs := bpf_load.o sockex3_user.o |
66 | tracex1-objs := bpf_load.o tracex1_user.o | 66 | tracex1-objs := bpf_load.o tracex1_user.o |
67 | tracex2-objs := bpf_load.o tracex2_user.o | 67 | tracex2-objs := bpf_load.o tracex2_user.o |
diff --git a/samples/bpf/fds_example.c b/samples/bpf/fds_example.c index 9854854f05d1..e51eb060244e 100644 --- a/samples/bpf/fds_example.c +++ b/samples/bpf/fds_example.c | |||
@@ -14,8 +14,8 @@ | |||
14 | 14 | ||
15 | #include <bpf/bpf.h> | 15 | #include <bpf/bpf.h> |
16 | 16 | ||
17 | #include "bpf/libbpf.h" | ||
17 | #include "bpf_insn.h" | 18 | #include "bpf_insn.h" |
18 | #include "bpf_load.h" | ||
19 | #include "sock_example.h" | 19 | #include "sock_example.h" |
20 | 20 | ||
21 | #define BPF_F_PIN (1 << 0) | 21 | #define BPF_F_PIN (1 << 0) |
@@ -57,10 +57,14 @@ static int bpf_prog_create(const char *object) | |||
57 | BPF_EXIT_INSN(), | 57 | BPF_EXIT_INSN(), |
58 | }; | 58 | }; |
59 | size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn); | 59 | size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn); |
60 | char bpf_log_buf[BPF_LOG_BUF_SIZE]; | ||
61 | struct bpf_object *obj; | ||
62 | int prog_fd; | ||
60 | 63 | ||
61 | if (object) { | 64 | if (object) { |
62 | assert(!load_bpf_file((char *)object)); | 65 | assert(!bpf_prog_load(object, BPF_PROG_TYPE_UNSPEC, |
63 | return prog_fd[0]; | 66 | &obj, &prog_fd)); |
67 | return prog_fd; | ||
64 | } else { | 68 | } else { |
65 | return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, | 69 | return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, |
66 | insns, insns_cnt, "GPL", 0, | 70 | insns, insns_cnt, "GPL", 0, |
diff --git a/samples/bpf/sockex1_user.c b/samples/bpf/sockex1_user.c index be8ba5686924..7f90796ae15a 100644 --- a/samples/bpf/sockex1_user.c +++ b/samples/bpf/sockex1_user.c | |||
@@ -3,28 +3,31 @@ | |||
3 | #include <assert.h> | 3 | #include <assert.h> |
4 | #include <linux/bpf.h> | 4 | #include <linux/bpf.h> |
5 | #include <bpf/bpf.h> | 5 | #include <bpf/bpf.h> |
6 | #include "bpf_load.h" | 6 | #include "bpf/libbpf.h" |
7 | #include "sock_example.h" | 7 | #include "sock_example.h" |
8 | #include <unistd.h> | 8 | #include <unistd.h> |
9 | #include <arpa/inet.h> | 9 | #include <arpa/inet.h> |
10 | 10 | ||
11 | int main(int ac, char **argv) | 11 | int main(int ac, char **argv) |
12 | { | 12 | { |
13 | struct bpf_object *obj; | ||
14 | int map_fd, prog_fd; | ||
13 | char filename[256]; | 15 | char filename[256]; |
14 | FILE *f; | ||
15 | int i, sock; | 16 | int i, sock; |
17 | FILE *f; | ||
16 | 18 | ||
17 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | 19 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
18 | 20 | ||
19 | if (load_bpf_file(filename)) { | 21 | if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER, |
20 | printf("%s", bpf_log_buf); | 22 | &obj, &prog_fd)) |
21 | return 1; | 23 | return 1; |
22 | } | 24 | |
25 | map_fd = bpf_object__find_map_fd_by_name(obj, "my_map"); | ||
23 | 26 | ||
24 | sock = open_raw_sock("lo"); | 27 | sock = open_raw_sock("lo"); |
25 | 28 | ||
26 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, | 29 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, |
27 | sizeof(prog_fd[0])) == 0); | 30 | sizeof(prog_fd)) == 0); |
28 | 31 | ||
29 | f = popen("ping -4 -c5 localhost", "r"); | 32 | f = popen("ping -4 -c5 localhost", "r"); |
30 | (void) f; | 33 | (void) f; |
@@ -34,13 +37,13 @@ int main(int ac, char **argv) | |||
34 | int key; | 37 | int key; |
35 | 38 | ||
36 | key = IPPROTO_TCP; | 39 | key = IPPROTO_TCP; |
37 | assert(bpf_map_lookup_elem(map_fd[0], &key, &tcp_cnt) == 0); | 40 | assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0); |
38 | 41 | ||
39 | key = IPPROTO_UDP; | 42 | key = IPPROTO_UDP; |
40 | assert(bpf_map_lookup_elem(map_fd[0], &key, &udp_cnt) == 0); | 43 | assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0); |
41 | 44 | ||
42 | key = IPPROTO_ICMP; | 45 | key = IPPROTO_ICMP; |
43 | assert(bpf_map_lookup_elem(map_fd[0], &key, &icmp_cnt) == 0); | 46 | assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0); |
44 | 47 | ||
45 | printf("TCP %lld UDP %lld ICMP %lld bytes\n", | 48 | printf("TCP %lld UDP %lld ICMP %lld bytes\n", |
46 | tcp_cnt, udp_cnt, icmp_cnt); | 49 | tcp_cnt, udp_cnt, icmp_cnt); |
diff --git a/samples/bpf/sockex2_user.c b/samples/bpf/sockex2_user.c index 125ee6efc913..bc257333ad92 100644 --- a/samples/bpf/sockex2_user.c +++ b/samples/bpf/sockex2_user.c | |||
@@ -3,7 +3,7 @@ | |||
3 | #include <assert.h> | 3 | #include <assert.h> |
4 | #include <linux/bpf.h> | 4 | #include <linux/bpf.h> |
5 | #include <bpf/bpf.h> | 5 | #include <bpf/bpf.h> |
6 | #include "bpf_load.h" | 6 | #include "bpf/libbpf.h" |
7 | #include "sock_example.h" | 7 | #include "sock_example.h" |
8 | #include <unistd.h> | 8 | #include <unistd.h> |
9 | #include <arpa/inet.h> | 9 | #include <arpa/inet.h> |
@@ -17,22 +17,25 @@ struct pair { | |||
17 | int main(int ac, char **argv) | 17 | int main(int ac, char **argv) |
18 | { | 18 | { |
19 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; | 19 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
20 | struct bpf_object *obj; | ||
21 | int map_fd, prog_fd; | ||
20 | char filename[256]; | 22 | char filename[256]; |
21 | FILE *f; | ||
22 | int i, sock; | 23 | int i, sock; |
24 | FILE *f; | ||
23 | 25 | ||
24 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | 26 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
25 | setrlimit(RLIMIT_MEMLOCK, &r); | 27 | setrlimit(RLIMIT_MEMLOCK, &r); |
26 | 28 | ||
27 | if (load_bpf_file(filename)) { | 29 | if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER, |
28 | printf("%s", bpf_log_buf); | 30 | &obj, &prog_fd)) |
29 | return 1; | 31 | return 1; |
30 | } | 32 | |
33 | map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map"); | ||
31 | 34 | ||
32 | sock = open_raw_sock("lo"); | 35 | sock = open_raw_sock("lo"); |
33 | 36 | ||
34 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, | 37 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, |
35 | sizeof(prog_fd[0])) == 0); | 38 | sizeof(prog_fd)) == 0); |
36 | 39 | ||
37 | f = popen("ping -4 -c5 localhost", "r"); | 40 | f = popen("ping -4 -c5 localhost", "r"); |
38 | (void) f; | 41 | (void) f; |
@@ -41,8 +44,8 @@ int main(int ac, char **argv) | |||
41 | int key = 0, next_key; | 44 | int key = 0, next_key; |
42 | struct pair value; | 45 | struct pair value; |
43 | 46 | ||
44 | while (bpf_map_get_next_key(map_fd[0], &key, &next_key) == 0) { | 47 | while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) { |
45 | bpf_map_lookup_elem(map_fd[0], &next_key, &value); | 48 | bpf_map_lookup_elem(map_fd, &next_key, &value); |
46 | printf("ip %s bytes %lld packets %lld\n", | 49 | printf("ip %s bytes %lld packets %lld\n", |
47 | inet_ntoa((struct in_addr){htonl(next_key)}), | 50 | inet_ntoa((struct in_addr){htonl(next_key)}), |
48 | value.bytes, value.packets); | 51 | value.bytes, value.packets); |