1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a8085782SAlexei Starovoitov #include <stdio.h>
3a8085782SAlexei Starovoitov #include <assert.h>
4a8085782SAlexei 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"
8a8085782SAlexei Starovoitov #include <unistd.h>
9a8085782SAlexei Starovoitov #include <arpa/inet.h>
10a8085782SAlexei Starovoitov
main(int ac,char ** argv)11a8085782SAlexei Starovoitov int main(int ac, char **argv)
12a8085782SAlexei Starovoitov {
131a9b268cSJakub Kicinski struct bpf_object *obj;
14*c58f9815SAndrii Nakryiko struct bpf_program *prog;
151a9b268cSJakub Kicinski int map_fd, prog_fd;
16a8085782SAlexei Starovoitov char filename[256];
17*c58f9815SAndrii Nakryiko int i, sock, err;
181a9b268cSJakub Kicinski FILE *f;
19a8085782SAlexei Starovoitov
20a8085782SAlexei Starovoitov snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
21a8085782SAlexei Starovoitov
22*c58f9815SAndrii Nakryiko obj = bpf_object__open_file(filename, NULL);
23*c58f9815SAndrii Nakryiko if (libbpf_get_error(obj))
24a8085782SAlexei Starovoitov return 1;
251a9b268cSJakub Kicinski
26*c58f9815SAndrii Nakryiko prog = bpf_object__next_program(obj, NULL);
27*c58f9815SAndrii Nakryiko bpf_program__set_type(prog, BPF_PROG_TYPE_SOCKET_FILTER);
28*c58f9815SAndrii Nakryiko
29*c58f9815SAndrii Nakryiko err = bpf_object__load(obj);
30*c58f9815SAndrii Nakryiko if (err)
31*c58f9815SAndrii Nakryiko return 1;
32*c58f9815SAndrii Nakryiko
33*c58f9815SAndrii Nakryiko prog_fd = bpf_program__fd(prog);
341a9b268cSJakub Kicinski map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
35a8085782SAlexei Starovoitov
36a8085782SAlexei Starovoitov sock = open_raw_sock("lo");
37a8085782SAlexei Starovoitov
381a9b268cSJakub Kicinski assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
391a9b268cSJakub Kicinski sizeof(prog_fd)) == 0);
40a8085782SAlexei Starovoitov
415c3cf87dSJakub Kicinski f = popen("ping -4 -c5 localhost", "r");
42a8085782SAlexei Starovoitov (void) f;
43a8085782SAlexei Starovoitov
44a8085782SAlexei Starovoitov for (i = 0; i < 5; i++) {
45a8085782SAlexei Starovoitov long long tcp_cnt, udp_cnt, icmp_cnt;
46a8085782SAlexei Starovoitov int key;
47a8085782SAlexei Starovoitov
48a8085782SAlexei Starovoitov key = IPPROTO_TCP;
491a9b268cSJakub Kicinski assert(bpf_map_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
50a8085782SAlexei Starovoitov
51a8085782SAlexei Starovoitov key = IPPROTO_UDP;
521a9b268cSJakub Kicinski assert(bpf_map_lookup_elem(map_fd, &key, &udp_cnt) == 0);
53a8085782SAlexei Starovoitov
54a8085782SAlexei Starovoitov key = IPPROTO_ICMP;
551a9b268cSJakub Kicinski assert(bpf_map_lookup_elem(map_fd, &key, &icmp_cnt) == 0);
56a8085782SAlexei Starovoitov
57614cd3bdSAlexei Starovoitov printf("TCP %lld UDP %lld ICMP %lld bytes\n",
58a8085782SAlexei Starovoitov tcp_cnt, udp_cnt, icmp_cnt);
59a8085782SAlexei Starovoitov sleep(1);
60a8085782SAlexei Starovoitov }
61a8085782SAlexei Starovoitov
62a8085782SAlexei Starovoitov return 0;
63a8085782SAlexei Starovoitov }
64