]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a8085782 AS |
2 | #include <stdio.h> |
3 | #include <assert.h> | |
4 | #include <linux/bpf.h> | |
2bf3e2ef | 5 | #include <bpf/bpf.h> |
4d18f6de | 6 | #include "libbpf.h" |
9899694a | 7 | #include "sock_example.h" |
a8085782 AS |
8 | #include <unistd.h> |
9 | #include <arpa/inet.h> | |
10 | ||
11 | int main(int ac, char **argv) | |
12 | { | |
1a9b268c JK |
13 | struct bpf_object *obj; |
14 | int map_fd, prog_fd; | |
a8085782 | 15 | char filename[256]; |
a8085782 | 16 | int i, sock; |
1a9b268c | 17 | FILE *f; |
a8085782 AS |
18 | |
19 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | |
20 | ||
1a9b268c JK |
21 | if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER, |
22 | &obj, &prog_fd)) | |
a8085782 | 23 | return 1; |
1a9b268c JK |
24 | |
25 | map_fd = bpf_object__find_map_fd_by_name(obj, "my_map"); | |
a8085782 AS |
26 | |
27 | sock = open_raw_sock("lo"); | |
28 | ||
1a9b268c JK |
29 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, |
30 | sizeof(prog_fd)) == 0); | |
a8085782 | 31 | |
5c3cf87d | 32 | f = popen("ping -4 -c5 localhost", "r"); |
a8085782 AS |
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; | |
1a9b268c | 40 | assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0); |
a8085782 AS |
41 | |
42 | key = IPPROTO_UDP; | |
1a9b268c | 43 | assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0); |
a8085782 AS |
44 | |
45 | key = IPPROTO_ICMP; | |
1a9b268c | 46 | assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0); |
a8085782 | 47 | |
614cd3bd | 48 | printf("TCP %lld UDP %lld ICMP %lld bytes\n", |
a8085782 AS |
49 | tcp_cnt, udp_cnt, icmp_cnt); |
50 | sleep(1); | |
51 | } | |
52 | ||
53 | return 0; | |
54 | } |