8 #include <sys/resource.h>
10 struct bpf_flow_keys {
25 int main(int argc, char **argv)
27 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
32 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
33 setrlimit(RLIMIT_MEMLOCK, &r);
35 if (load_bpf_file(filename)) {
36 printf("%s", bpf_log_buf);
40 sock = open_raw_sock("lo");
42 assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4],
46 f = popen("ping -c5 localhost", "r");
48 f = popen("netperf -l 4 localhost", "r");
51 for (i = 0; i < 5; i++) {
52 struct bpf_flow_keys key = {}, next_key;
56 printf("IP src.port -> dst.port bytes packets\n");
57 while (bpf_map_get_next_key(map_fd[2], &key, &next_key) == 0) {
58 bpf_map_lookup_elem(map_fd[2], &next_key, &value);
59 printf("%s.%05d -> %s.%05d %12lld %12lld\n",
60 inet_ntoa((struct in_addr){htonl(next_key.src)}),
62 inet_ntoa((struct in_addr){htonl(next_key.dst)}),
64 value.bytes, value.packets);