Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <assert.h> |
| 4 | #include <linux/bpf.h> |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 5 | #include <bpf/bpf.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 6 | #include <bpf/libbpf.h> |
Joe Stringer | 9899694 | 2016-12-08 18:46:20 -0800 | [diff] [blame] | 7 | #include "sock_example.h" |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | #include <arpa/inet.h> |
| 10 | |
| 11 | int main(int ac, char **argv) |
| 12 | { |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 13 | struct bpf_object *obj; |
| 14 | int map_fd, prog_fd; |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 15 | char filename[256]; |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 16 | int i, sock; |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 17 | FILE *f; |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 18 | |
| 19 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 20 | |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 21 | if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER, |
| 22 | &obj, &prog_fd)) |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 23 | return 1; |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 24 | |
| 25 | map_fd = bpf_object__find_map_fd_by_name(obj, "my_map"); |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 26 | |
| 27 | sock = open_raw_sock("lo"); |
| 28 | |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 29 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, |
| 30 | sizeof(prog_fd)) == 0); |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 31 | |
Jakub Kicinski | 5c3cf87 | 2019-02-27 19:04:10 -0800 | [diff] [blame] | 32 | f = popen("ping -4 -c5 localhost", "r"); |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 33 | (void) f; |
| 34 | |
| 35 | for (i = 0; i < 5; i++) { |
| 36 | long long tcp_cnt, udp_cnt, icmp_cnt; |
| 37 | int key; |
| 38 | |
| 39 | key = IPPROTO_TCP; |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 40 | assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0); |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 41 | |
| 42 | key = IPPROTO_UDP; |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 43 | assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0); |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 44 | |
| 45 | key = IPPROTO_ICMP; |
Jakub Kicinski | 1a9b268 | 2019-02-27 19:04:13 -0800 | [diff] [blame] | 46 | assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0); |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 47 | |
Alexei Starovoitov | 614cd3b | 2015-03-13 11:57:43 -0700 | [diff] [blame] | 48 | printf("TCP %lld UDP %lld ICMP %lld bytes\n", |
Alexei Starovoitov | a808578 | 2014-12-01 15:06:38 -0800 | [diff] [blame] | 49 | tcp_cnt, udp_cnt, icmp_cnt); |
| 50 | sleep(1); |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |