Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 1 | /* eBPF example program: |
| 2 | * - creates arraymap in kernel with key 4 bytes and value 8 bytes |
| 3 | * |
| 4 | * - loads eBPF program: |
| 5 | * r0 = skb->data[ETH_HLEN + offsetof(struct iphdr, protocol)]; |
| 6 | * *(u32*)(fp - 4) = r0; |
| 7 | * // assuming packet is IPv4, lookup ip->proto in a map |
| 8 | * value = bpf_map_lookup_elem(map_fd, fp - 4); |
| 9 | * if (value) |
| 10 | * (*(u64*)value) += 1; |
| 11 | * |
Wang Sheng-Hui | 97c3361 | 2018-04-17 10:25:20 +0800 | [diff] [blame] | 12 | * - attaches this program to loopback interface "lo" raw socket |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 13 | * |
| 14 | * - every second user space reads map[tcp], map[udp], map[icmp] to see |
Wang Sheng-Hui | 97c3361 | 2018-04-17 10:25:20 +0800 | [diff] [blame] | 15 | * how many packets of given protocol were seen on "lo" |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 16 | */ |
| 17 | #include <stdio.h> |
| 18 | #include <unistd.h> |
| 19 | #include <assert.h> |
| 20 | #include <linux/bpf.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <errno.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <arpa/inet.h> |
| 26 | #include <linux/if_ether.h> |
| 27 | #include <linux/ip.h> |
| 28 | #include <stddef.h> |
Jakub Kicinski | 8d93045 | 2018-05-14 22:35:03 -0700 | [diff] [blame] | 29 | #include <bpf/bpf.h> |
| 30 | #include "bpf_insn.h" |
Joe Stringer | 9899694 | 2016-12-08 18:46:20 -0800 | [diff] [blame] | 31 | #include "sock_example.h" |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 32 | |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 33 | char bpf_log_buf[BPF_LOG_BUF_SIZE]; |
| 34 | |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 35 | static int test_sock(void) |
| 36 | { |
| 37 | int sock = -1, map_fd, prog_fd, i, key; |
| 38 | long long value = 0, tcp_cnt, udp_cnt, icmp_cnt; |
| 39 | |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 40 | map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value), |
| 41 | 256, NULL); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 42 | if (map_fd < 0) { |
| 43 | printf("failed to create map '%s'\n", strerror(errno)); |
| 44 | goto cleanup; |
| 45 | } |
| 46 | |
| 47 | struct bpf_insn prog[] = { |
| 48 | BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), |
| 49 | BPF_LD_ABS(BPF_B, ETH_HLEN + offsetof(struct iphdr, protocol) /* R0 = ip->proto */), |
| 50 | BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */ |
| 51 | BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), |
| 52 | BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */ |
| 53 | BPF_LD_MAP_FD(BPF_REG_1, map_fd), |
| 54 | BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 55 | BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), |
| 56 | BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */ |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 57 | BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_0, BPF_REG_1, 0), |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 58 | BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */ |
| 59 | BPF_EXIT_INSN(), |
| 60 | }; |
Joe Stringer | 43371c8 | 2016-12-14 14:43:39 -0800 | [diff] [blame] | 61 | size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 62 | LIBBPF_OPTS(bpf_prog_load_opts, opts, |
| 63 | .log_buf = bpf_log_buf, |
| 64 | .log_size = BPF_LOG_BUF_SIZE, |
| 65 | ); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 66 | |
Andrii Nakryiko | c58f981 | 2021-12-01 15:28:23 -0800 | [diff] [blame] | 67 | prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", |
| 68 | prog, insns_cnt, &opts); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 69 | if (prog_fd < 0) { |
| 70 | printf("failed to load prog '%s'\n", strerror(errno)); |
| 71 | goto cleanup; |
| 72 | } |
| 73 | |
| 74 | sock = open_raw_sock("lo"); |
| 75 | |
| 76 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, |
| 77 | sizeof(prog_fd)) < 0) { |
| 78 | printf("setsockopt %s\n", strerror(errno)); |
| 79 | goto cleanup; |
| 80 | } |
| 81 | |
| 82 | for (i = 0; i < 10; i++) { |
| 83 | key = IPPROTO_TCP; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 84 | assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 85 | |
| 86 | key = IPPROTO_UDP; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 87 | assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 88 | |
| 89 | key = IPPROTO_ICMP; |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 90 | assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 91 | |
| 92 | printf("TCP %lld UDP %lld ICMP %lld packets\n", |
| 93 | tcp_cnt, udp_cnt, icmp_cnt); |
| 94 | sleep(1); |
| 95 | } |
| 96 | |
| 97 | cleanup: |
| 98 | /* maps, programs, raw sockets will auto cleanup on process exit */ |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int main(void) |
| 103 | { |
| 104 | FILE *f; |
| 105 | |
Jakub Kicinski | 5c3cf87 | 2019-02-27 19:04:10 -0800 | [diff] [blame] | 106 | f = popen("ping -4 -c5 localhost", "r"); |
Alexei Starovoitov | 03f4723 | 2014-12-01 15:06:36 -0800 | [diff] [blame] | 107 | (void)f; |
| 108 | |
| 109 | return test_sock(); |
| 110 | } |