diff options
author | Alexei Starovoitov <ast@plumgrid.com> | 2014-12-01 18:06:38 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2014-12-06 00:47:33 -0500 |
commit | a80857822b0c2ed608c93504bd3687b78f20c619 (patch) | |
tree | 8fb218a13a13a16d5083c7b831398f967137f152 /samples/bpf | |
parent | 249b812d8005ec38e351ee763ceb85d56b155064 (diff) |
samples: bpf: trivial eBPF program in C
this example does the same task as previous socket example
in assembler, but this one does it in C.
eBPF program in kernel does:
/* assume that packet is IPv4, load one byte of IP->proto */
int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol));
long *value;
value = bpf_map_lookup_elem(&my_map, &index);
if (value)
__sync_fetch_and_add(value, 1);
Corresponding user space reads map[tcp], map[udp], map[icmp]
and prints protocol stats every second
Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'samples/bpf')
-rw-r--r-- | samples/bpf/Makefile | 14 | ||||
-rw-r--r-- | samples/bpf/libbpf.h | 2 | ||||
-rw-r--r-- | samples/bpf/sockex1_kern.c | 25 | ||||
-rw-r--r-- | samples/bpf/sockex1_user.c | 49 |
4 files changed, 89 insertions, 1 deletions
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index f46d3492d032..770d145186c3 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile | |||
@@ -4,12 +4,26 @@ obj- := dummy.o | |||
4 | # List of programs to build | 4 | # List of programs to build |
5 | hostprogs-y := test_verifier test_maps | 5 | hostprogs-y := test_verifier test_maps |
6 | hostprogs-y += sock_example | 6 | hostprogs-y += sock_example |
7 | hostprogs-y += sockex1 | ||
7 | 8 | ||
8 | test_verifier-objs := test_verifier.o libbpf.o | 9 | test_verifier-objs := test_verifier.o libbpf.o |
9 | test_maps-objs := test_maps.o libbpf.o | 10 | test_maps-objs := test_maps.o libbpf.o |
10 | sock_example-objs := sock_example.o libbpf.o | 11 | sock_example-objs := sock_example.o libbpf.o |
12 | sockex1-objs := bpf_load.o libbpf.o sockex1_user.o | ||
11 | 13 | ||
12 | # Tell kbuild to always build the programs | 14 | # Tell kbuild to always build the programs |
13 | always := $(hostprogs-y) | 15 | always := $(hostprogs-y) |
16 | always += sockex1_kern.o | ||
14 | 17 | ||
15 | HOSTCFLAGS += -I$(objtree)/usr/include | 18 | HOSTCFLAGS += -I$(objtree)/usr/include |
19 | |||
20 | HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable | ||
21 | HOSTLOADLIBES_sockex1 += -lelf | ||
22 | |||
23 | # point this to your LLVM backend with bpf support | ||
24 | LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc | ||
25 | |||
26 | %.o: %.c | ||
27 | clang $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) \ | ||
28 | -D__KERNEL__ -Wno-unused-value -Wno-pointer-sign \ | ||
29 | -O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf -filetype=obj -o $@ | ||
diff --git a/samples/bpf/libbpf.h b/samples/bpf/libbpf.h index cc62ad4d95de..58c5fe1bdba1 100644 --- a/samples/bpf/libbpf.h +++ b/samples/bpf/libbpf.h | |||
@@ -15,7 +15,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, | |||
15 | const struct bpf_insn *insns, int insn_len, | 15 | const struct bpf_insn *insns, int insn_len, |
16 | const char *license); | 16 | const char *license); |
17 | 17 | ||
18 | #define LOG_BUF_SIZE 8192 | 18 | #define LOG_BUF_SIZE 65536 |
19 | extern char bpf_log_buf[LOG_BUF_SIZE]; | 19 | extern char bpf_log_buf[LOG_BUF_SIZE]; |
20 | 20 | ||
21 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ | 21 | /* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ |
diff --git a/samples/bpf/sockex1_kern.c b/samples/bpf/sockex1_kern.c new file mode 100644 index 000000000000..066892662915 --- /dev/null +++ b/samples/bpf/sockex1_kern.c | |||
@@ -0,0 +1,25 @@ | |||
1 | #include <uapi/linux/bpf.h> | ||
2 | #include <uapi/linux/if_ether.h> | ||
3 | #include <uapi/linux/ip.h> | ||
4 | #include "bpf_helpers.h" | ||
5 | |||
6 | struct bpf_map_def SEC("maps") my_map = { | ||
7 | .type = BPF_MAP_TYPE_ARRAY, | ||
8 | .key_size = sizeof(u32), | ||
9 | .value_size = sizeof(long), | ||
10 | .max_entries = 256, | ||
11 | }; | ||
12 | |||
13 | SEC("socket1") | ||
14 | int bpf_prog1(struct sk_buff *skb) | ||
15 | { | ||
16 | int index = load_byte(skb, ETH_HLEN + offsetof(struct iphdr, protocol)); | ||
17 | long *value; | ||
18 | |||
19 | value = bpf_map_lookup_elem(&my_map, &index); | ||
20 | if (value) | ||
21 | __sync_fetch_and_add(value, 1); | ||
22 | |||
23 | return 0; | ||
24 | } | ||
25 | char _license[] SEC("license") = "GPL"; | ||
diff --git a/samples/bpf/sockex1_user.c b/samples/bpf/sockex1_user.c new file mode 100644 index 000000000000..34a443ff3831 --- /dev/null +++ b/samples/bpf/sockex1_user.c | |||
@@ -0,0 +1,49 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <assert.h> | ||
3 | #include <linux/bpf.h> | ||
4 | #include "libbpf.h" | ||
5 | #include "bpf_load.h" | ||
6 | #include <unistd.h> | ||
7 | #include <arpa/inet.h> | ||
8 | |||
9 | int main(int ac, char **argv) | ||
10 | { | ||
11 | char filename[256]; | ||
12 | FILE *f; | ||
13 | int i, sock; | ||
14 | |||
15 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | ||
16 | |||
17 | if (load_bpf_file(filename)) { | ||
18 | printf("%s", bpf_log_buf); | ||
19 | return 1; | ||
20 | } | ||
21 | |||
22 | sock = open_raw_sock("lo"); | ||
23 | |||
24 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, | ||
25 | sizeof(prog_fd[0])) == 0); | ||
26 | |||
27 | f = popen("ping -c5 localhost", "r"); | ||
28 | (void) f; | ||
29 | |||
30 | for (i = 0; i < 5; i++) { | ||
31 | long long tcp_cnt, udp_cnt, icmp_cnt; | ||
32 | int key; | ||
33 | |||
34 | key = IPPROTO_TCP; | ||
35 | assert(bpf_lookup_elem(map_fd[0], &key, &tcp_cnt) == 0); | ||
36 | |||
37 | key = IPPROTO_UDP; | ||
38 | assert(bpf_lookup_elem(map_fd[0], &key, &udp_cnt) == 0); | ||
39 | |||
40 | key = IPPROTO_ICMP; | ||
41 | assert(bpf_lookup_elem(map_fd[0], &key, &icmp_cnt) == 0); | ||
42 | |||
43 | printf("TCP %lld UDP %lld ICMP %lld packets\n", | ||
44 | tcp_cnt, udp_cnt, icmp_cnt); | ||
45 | sleep(1); | ||
46 | } | ||
47 | |||
48 | return 0; | ||
49 | } | ||