6 #include "sock_example.h"
9 #include <sys/resource.h>
12 #define PARSE_IP_PROG_FD (prog_fd[0])
13 #define PROG_ARRAY_FD (map_fd[0])
15 struct bpf_flow_keys
{
30 int main(int argc
, char **argv
)
32 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
35 int i
, sock
, err
, id
, key
= PARSE_IP
;
36 struct bpf_prog_info info
= {};
37 uint32_t info_len
= sizeof(info
);
39 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
40 setrlimit(RLIMIT_MEMLOCK
, &r
);
42 if (load_bpf_file(filename
)) {
43 printf("%s", bpf_log_buf
);
47 /* Test fd array lookup which returns the id of the bpf_prog */
48 err
= bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD
, &info
, &info_len
);
50 err
= bpf_map_lookup_elem(PROG_ARRAY_FD
, &key
, &id
);
52 assert(id
== info
.id
);
54 sock
= open_raw_sock("lo");
56 assert(setsockopt(sock
, SOL_SOCKET
, SO_ATTACH_BPF
, &prog_fd
[4],
60 f
= popen("ping -c5 localhost", "r");
62 f
= popen("netperf -l 4 localhost", "r");
65 for (i
= 0; i
< 5; i
++) {
66 struct bpf_flow_keys key
= {}, next_key
;
70 printf("IP src.port -> dst.port bytes packets\n");
71 while (bpf_map_get_next_key(map_fd
[2], &key
, &next_key
) == 0) {
72 bpf_map_lookup_elem(map_fd
[2], &next_key
, &value
);
73 printf("%s.%05d -> %s.%05d %12lld %12lld\n",
74 inet_ntoa((struct in_addr
){htonl(next_key
.src
)}),
76 inet_ntoa((struct in_addr
){htonl(next_key
.dst
)}),
78 value
.bytes
, value
.packets
);