103f4723eSAlexei Starovoitov /* eBPF example program:
203f4723eSAlexei Starovoitov * - creates arraymap in kernel with key 4 bytes and value 8 bytes
303f4723eSAlexei Starovoitov *
403f4723eSAlexei Starovoitov * - loads eBPF program:
503f4723eSAlexei Starovoitov * r0 = skb->data[ETH_HLEN + offsetof(struct iphdr, protocol)];
603f4723eSAlexei Starovoitov * *(u32*)(fp - 4) = r0;
703f4723eSAlexei Starovoitov * // assuming packet is IPv4, lookup ip->proto in a map
803f4723eSAlexei Starovoitov * value = bpf_map_lookup_elem(map_fd, fp - 4);
903f4723eSAlexei Starovoitov * if (value)
1003f4723eSAlexei Starovoitov * (*(u64*)value) += 1;
1103f4723eSAlexei Starovoitov *
1297c33610SWang Sheng-Hui * - attaches this program to loopback interface "lo" raw socket
1303f4723eSAlexei Starovoitov *
1403f4723eSAlexei Starovoitov * - every second user space reads map[tcp], map[udp], map[icmp] to see
1597c33610SWang Sheng-Hui * how many packets of given protocol were seen on "lo"
1603f4723eSAlexei Starovoitov */
1703f4723eSAlexei Starovoitov #include <stdio.h>
1803f4723eSAlexei Starovoitov #include <unistd.h>
1903f4723eSAlexei Starovoitov #include <assert.h>
2003f4723eSAlexei Starovoitov #include <linux/bpf.h>
2103f4723eSAlexei Starovoitov #include <string.h>
2203f4723eSAlexei Starovoitov #include <stdlib.h>
2303f4723eSAlexei Starovoitov #include <errno.h>
2403f4723eSAlexei Starovoitov #include <sys/socket.h>
2503f4723eSAlexei Starovoitov #include <arpa/inet.h>
2603f4723eSAlexei Starovoitov #include <linux/if_ether.h>
2703f4723eSAlexei Starovoitov #include <linux/ip.h>
2803f4723eSAlexei Starovoitov #include <stddef.h>
298d930450SJakub Kicinski #include <bpf/bpf.h>
308d930450SJakub Kicinski #include "bpf_insn.h"
319899694aSJoe Stringer #include "sock_example.h"
32*b1fc28b3SLinkui Xiao #include "bpf_util.h"
3303f4723eSAlexei Starovoitov
34d40fc181SJoe Stringer char bpf_log_buf[BPF_LOG_BUF_SIZE];
35d40fc181SJoe Stringer
test_sock(void)3603f4723eSAlexei Starovoitov static int test_sock(void)
3703f4723eSAlexei Starovoitov {
3803f4723eSAlexei Starovoitov int sock = -1, map_fd, prog_fd, i, key;
3903f4723eSAlexei Starovoitov long long value = 0, tcp_cnt, udp_cnt, icmp_cnt;
4003f4723eSAlexei Starovoitov
41c58f9815SAndrii Nakryiko map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value),
42c58f9815SAndrii Nakryiko 256, NULL);
4303f4723eSAlexei Starovoitov if (map_fd < 0) {
4403f4723eSAlexei Starovoitov printf("failed to create map '%s'\n", strerror(errno));
4503f4723eSAlexei Starovoitov goto cleanup;
4603f4723eSAlexei Starovoitov }
4703f4723eSAlexei Starovoitov
4803f4723eSAlexei Starovoitov struct bpf_insn prog[] = {
4903f4723eSAlexei Starovoitov BPF_MOV64_REG(BPF_REG_6, BPF_REG_1),
5003f4723eSAlexei Starovoitov BPF_LD_ABS(BPF_B, ETH_HLEN + offsetof(struct iphdr, protocol) /* R0 = ip->proto */),
5103f4723eSAlexei Starovoitov BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
5203f4723eSAlexei Starovoitov BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
5303f4723eSAlexei Starovoitov BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
5403f4723eSAlexei Starovoitov BPF_LD_MAP_FD(BPF_REG_1, map_fd),
5503f4723eSAlexei Starovoitov BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
5603f4723eSAlexei Starovoitov BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
5703f4723eSAlexei Starovoitov BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */
5891c960b0SBrendan Jackman BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_0, BPF_REG_1, 0),
5903f4723eSAlexei Starovoitov BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
6003f4723eSAlexei Starovoitov BPF_EXIT_INSN(),
6103f4723eSAlexei Starovoitov };
62*b1fc28b3SLinkui Xiao size_t insns_cnt = ARRAY_SIZE(prog);
63c58f9815SAndrii Nakryiko LIBBPF_OPTS(bpf_prog_load_opts, opts,
64c58f9815SAndrii Nakryiko .log_buf = bpf_log_buf,
65c58f9815SAndrii Nakryiko .log_size = BPF_LOG_BUF_SIZE,
66c58f9815SAndrii Nakryiko );
6703f4723eSAlexei Starovoitov
68c58f9815SAndrii Nakryiko prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
69c58f9815SAndrii Nakryiko prog, insns_cnt, &opts);
7003f4723eSAlexei Starovoitov if (prog_fd < 0) {
7103f4723eSAlexei Starovoitov printf("failed to load prog '%s'\n", strerror(errno));
7203f4723eSAlexei Starovoitov goto cleanup;
7303f4723eSAlexei Starovoitov }
7403f4723eSAlexei Starovoitov
7503f4723eSAlexei Starovoitov sock = open_raw_sock("lo");
7603f4723eSAlexei Starovoitov
7703f4723eSAlexei Starovoitov if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
7803f4723eSAlexei Starovoitov sizeof(prog_fd)) < 0) {
7903f4723eSAlexei Starovoitov printf("setsockopt %s\n", strerror(errno));
8003f4723eSAlexei Starovoitov goto cleanup;
8103f4723eSAlexei Starovoitov }
8203f4723eSAlexei Starovoitov
8303f4723eSAlexei Starovoitov for (i = 0; i < 10; i++) {
8403f4723eSAlexei Starovoitov key = IPPROTO_TCP;
85d40fc181SJoe Stringer assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
8603f4723eSAlexei Starovoitov
8703f4723eSAlexei Starovoitov key = IPPROTO_UDP;
88d40fc181SJoe Stringer assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
8903f4723eSAlexei Starovoitov
9003f4723eSAlexei Starovoitov key = IPPROTO_ICMP;
91d40fc181SJoe Stringer assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
9203f4723eSAlexei Starovoitov
9303f4723eSAlexei Starovoitov printf("TCP %lld UDP %lld ICMP %lld packets\n",
9403f4723eSAlexei Starovoitov tcp_cnt, udp_cnt, icmp_cnt);
9503f4723eSAlexei Starovoitov sleep(1);
9603f4723eSAlexei Starovoitov }
9703f4723eSAlexei Starovoitov
9803f4723eSAlexei Starovoitov cleanup:
9903f4723eSAlexei Starovoitov /* maps, programs, raw sockets will auto cleanup on process exit */
10003f4723eSAlexei Starovoitov return 0;
10103f4723eSAlexei Starovoitov }
10203f4723eSAlexei Starovoitov
main(void)10303f4723eSAlexei Starovoitov int main(void)
10403f4723eSAlexei Starovoitov {
10503f4723eSAlexei Starovoitov FILE *f;
10603f4723eSAlexei Starovoitov
1075c3cf87dSJakub Kicinski f = popen("ping -4 -c5 localhost", "r");
10803f4723eSAlexei Starovoitov (void)f;
10903f4723eSAlexei Starovoitov
11003f4723eSAlexei Starovoitov return test_sock();
11103f4723eSAlexei Starovoitov }
112