Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <assert.h> |
| 3 | #include <linux/bpf.h> |
| 4 | #include "libbpf.h" |
| 5 | #include "bpf_load.h" |
Joe Stringer | 9899694 | 2016-12-08 18:46:20 -0800 | [diff] [blame] | 6 | #include "sock_example.h" |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
| 8 | #include <arpa/inet.h> |
William Tu | eb88d58 | 2016-06-21 21:05:58 -0700 | [diff] [blame] | 9 | #include <sys/resource.h> |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 10 | |
Martin KaFai Lau | a8744f2 | 2017-06-27 23:08:35 -0700 | [diff] [blame] | 11 | #define PARSE_IP 3 |
| 12 | #define PARSE_IP_PROG_FD (prog_fd[0]) |
| 13 | #define PROG_ARRAY_FD (map_fd[0]) |
| 14 | |
Naveen N. Rao | 2b064ff | 2016-09-24 02:10:04 +0530 | [diff] [blame] | 15 | struct bpf_flow_keys { |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 16 | __be32 src; |
| 17 | __be32 dst; |
| 18 | union { |
| 19 | __be32 ports; |
| 20 | __be16 port16[2]; |
| 21 | }; |
| 22 | __u32 ip_proto; |
| 23 | }; |
| 24 | |
| 25 | struct pair { |
| 26 | __u64 packets; |
| 27 | __u64 bytes; |
| 28 | }; |
| 29 | |
| 30 | int main(int argc, char **argv) |
| 31 | { |
William Tu | eb88d58 | 2016-06-21 21:05:58 -0700 | [diff] [blame] | 32 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 33 | char filename[256]; |
| 34 | FILE *f; |
Martin KaFai Lau | a8744f2 | 2017-06-27 23:08:35 -0700 | [diff] [blame] | 35 | int i, sock, err, id, key = PARSE_IP; |
| 36 | struct bpf_prog_info info = {}; |
| 37 | uint32_t info_len = sizeof(info); |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 38 | |
| 39 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
William Tu | eb88d58 | 2016-06-21 21:05:58 -0700 | [diff] [blame] | 40 | setrlimit(RLIMIT_MEMLOCK, &r); |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 41 | |
| 42 | if (load_bpf_file(filename)) { |
| 43 | printf("%s", bpf_log_buf); |
| 44 | return 1; |
| 45 | } |
| 46 | |
Martin KaFai Lau | a8744f2 | 2017-06-27 23:08:35 -0700 | [diff] [blame] | 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); |
| 49 | assert(!err); |
| 50 | err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id); |
| 51 | assert(!err); |
| 52 | assert(id == info.id); |
| 53 | |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 54 | sock = open_raw_sock("lo"); |
| 55 | |
| 56 | assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4], |
| 57 | sizeof(__u32)) == 0); |
| 58 | |
| 59 | if (argc > 1) |
| 60 | f = popen("ping -c5 localhost", "r"); |
| 61 | else |
| 62 | f = popen("netperf -l 4 localhost", "r"); |
| 63 | (void) f; |
| 64 | |
| 65 | for (i = 0; i < 5; i++) { |
Naveen N. Rao | 2b064ff | 2016-09-24 02:10:04 +0530 | [diff] [blame] | 66 | struct bpf_flow_keys key = {}, next_key; |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 67 | struct pair value; |
| 68 | |
| 69 | sleep(1); |
| 70 | printf("IP src.port -> dst.port bytes packets\n"); |
Joe Stringer | d40fc18 | 2016-12-14 14:43:38 -0800 | [diff] [blame] | 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); |
Alexei Starovoitov | 530b2c8 | 2015-05-19 16:59:06 -0700 | [diff] [blame] | 73 | printf("%s.%05d -> %s.%05d %12lld %12lld\n", |
| 74 | inet_ntoa((struct in_addr){htonl(next_key.src)}), |
| 75 | next_key.port16[0], |
| 76 | inet_ntoa((struct in_addr){htonl(next_key.dst)}), |
| 77 | next_key.port16[1], |
| 78 | value.bytes, value.packets); |
| 79 | key = next_key; |
| 80 | } |
| 81 | } |
| 82 | return 0; |
| 83 | } |