1 // SPDX-License-Identifier: GPL-2.0
6 #include <bpf/libbpf.h>
7 #include "sock_example.h"
11 int main(int ac, char **argv)
13 struct bpf_object *obj;
14 struct bpf_program *prog;
20 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
22 obj = bpf_object__open_file(filename, NULL);
23 if (libbpf_get_error(obj))
26 prog = bpf_object__next_program(obj, NULL);
27 bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
29 err = bpf_object__load(obj);
33 prog_fd = bpf_program__fd(prog);
34 map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
36 sock = open_raw_sock("lo");
38 assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
39 sizeof(prog_fd)) == 0);
41 f = popen("ping -4 -c5 localhost", "r");
44 for (i = 0; i < 5; i++) {
45 long long tcp_cnt, udp_cnt, icmp_cnt;
49 assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
52 assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
55 assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
57 printf("TCP %lld UDP %lld ICMP %lld bytes\n",
58 tcp_cnt, udp_cnt, icmp_cnt);