^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "sock_example.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <arpa/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct pair {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) __u64 packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) __u64 bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int main(int ac, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int map_fd, prog_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int i, sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) setrlimit(RLIMIT_MEMLOCK, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) &obj, &prog_fd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) sock = open_raw_sock("lo");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) sizeof(prog_fd)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) f = popen("ping -4 -c5 localhost", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) (void) f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) for (i = 0; i < 5; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int key = 0, next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct pair value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) while (bpf_map_get_next_key(map_fd, &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) bpf_map_lookup_elem(map_fd, &next_key, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) printf("ip %s bytes %lld packets %lld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) inet_ntoa((struct in_addr){htonl(next_key)}),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) value.bytes, value.packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) sleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }