1 // SPDX-License-Identifier: GPL-2.0
7 #include "sock_example.h"
11 int main(int ac, char **argv)
13 struct bpf_object *obj;
19 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
21 if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
25 map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
27 sock = open_raw_sock("lo");
29 assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
30 sizeof(prog_fd)) == 0);
32 f = popen("ping -4 -c5 localhost", "r");
35 for (i = 0; i < 5; i++) {
36 long long tcp_cnt, udp_cnt, icmp_cnt;
40 assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
43 assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
46 assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
48 printf("TCP %lld UDP %lld ICMP %lld bytes\n",
49 tcp_cnt, udp_cnt, icmp_cnt);