1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fbe33108SAlexei Starovoitov #include <stdio.h>
3fbe33108SAlexei Starovoitov #include <assert.h>
4fbe33108SAlexei Starovoitov #include <linux/bpf.h>
52bf3e2efSJakub Kicinski #include <bpf/bpf.h>
67cf245a3SToke Høiland-Jørgensen #include <bpf/libbpf.h>
79899694aSJoe Stringer #include "sock_example.h"
8fbe33108SAlexei Starovoitov #include <unistd.h>
9fbe33108SAlexei Starovoitov #include <arpa/inet.h>
10fbe33108SAlexei Starovoitov
11614cd3bdSAlexei Starovoitov struct pair {
12614cd3bdSAlexei Starovoitov __u64 packets;
13614cd3bdSAlexei Starovoitov __u64 bytes;
14614cd3bdSAlexei Starovoitov };
15614cd3bdSAlexei Starovoitov
main(int ac,char ** argv)16fbe33108SAlexei Starovoitov int main(int ac, char **argv)
17fbe33108SAlexei Starovoitov {
18*c58f9815SAndrii Nakryiko struct bpf_program *prog;
191a9b268cSJakub Kicinski struct bpf_object *obj;
201a9b268cSJakub Kicinski int map_fd, prog_fd;
21fbe33108SAlexei Starovoitov char filename[256];
22*c58f9815SAndrii Nakryiko int i, sock, err;
231a9b268cSJakub Kicinski FILE *f;
24fbe33108SAlexei Starovoitov
25fbe33108SAlexei Starovoitov snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
26*c58f9815SAndrii Nakryiko obj = bpf_object__open_file(filename, NULL);
27*c58f9815SAndrii Nakryiko if (libbpf_get_error(obj))
28fbe33108SAlexei Starovoitov return 1;
291a9b268cSJakub Kicinski
30*c58f9815SAndrii Nakryiko prog = bpf_object__next_program(obj, NULL);
31*c58f9815SAndrii Nakryiko bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
32*c58f9815SAndrii Nakryiko
33*c58f9815SAndrii Nakryiko err = bpf_object__load(obj);
34*c58f9815SAndrii Nakryiko if (err)
35*c58f9815SAndrii Nakryiko return 1;
36*c58f9815SAndrii Nakryiko
37*c58f9815SAndrii Nakryiko prog_fd = bpf_program__fd(prog);
381a9b268cSJakub Kicinski map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map");
39fbe33108SAlexei Starovoitov
40fbe33108SAlexei Starovoitov sock = open_raw_sock("lo");
41fbe33108SAlexei Starovoitov
421a9b268cSJakub Kicinski assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
431a9b268cSJakub Kicinski sizeof(prog_fd)) == 0);
44fbe33108SAlexei Starovoitov
455c3cf87dSJakub Kicinski f = popen("ping -4 -c5 localhost", "r");
46fbe33108SAlexei Starovoitov (void) f;
47fbe33108SAlexei Starovoitov
48fbe33108SAlexei Starovoitov for (i = 0; i < 5; i++) {
49fbe33108SAlexei Starovoitov int key = 0, next_key;
50614cd3bdSAlexei Starovoitov struct pair value;
51fbe33108SAlexei Starovoitov
521a9b268cSJakub Kicinski while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
531a9b268cSJakub Kicinski bpf_map_lookup_elem(map_fd, &next_key, &value);
54614cd3bdSAlexei Starovoitov printf("ip %s bytes %lld packets %lld\n",
55fbe33108SAlexei Starovoitov inet_ntoa((struct in_addr){htonl(next_key)}),
56614cd3bdSAlexei Starovoitov value.bytes, value.packets);
57fbe33108SAlexei Starovoitov key = next_key;
58fbe33108SAlexei Starovoitov }
59fbe33108SAlexei Starovoitov sleep(1);
60fbe33108SAlexei Starovoitov }
61fbe33108SAlexei Starovoitov return 0;
62fbe33108SAlexei Starovoitov }
63