1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2530b2c86SAlexei Starovoitov #include <stdio.h> 3530b2c86SAlexei Starovoitov #include <assert.h> 4530b2c86SAlexei Starovoitov #include <linux/bpf.h> 52bf3e2efSJakub Kicinski #include <bpf/bpf.h> 6530b2c86SAlexei Starovoitov #include "bpf_load.h" 79899694aSJoe Stringer #include "sock_example.h" 8530b2c86SAlexei Starovoitov #include <unistd.h> 9530b2c86SAlexei Starovoitov #include <arpa/inet.h> 10eb88d585SWilliam Tu #include <sys/resource.h> 11530b2c86SAlexei Starovoitov 12a8744f25SMartin KaFai Lau #define PARSE_IP 3 13a8744f25SMartin KaFai Lau #define PARSE_IP_PROG_FD (prog_fd[0]) 14a8744f25SMartin KaFai Lau #define PROG_ARRAY_FD (map_fd[0]) 15a8744f25SMartin KaFai Lau 162b064fffSNaveen N. Rao struct bpf_flow_keys { 17530b2c86SAlexei Starovoitov __be32 src; 18530b2c86SAlexei Starovoitov __be32 dst; 19530b2c86SAlexei Starovoitov union { 20530b2c86SAlexei Starovoitov __be32 ports; 21530b2c86SAlexei Starovoitov __be16 port16[2]; 22530b2c86SAlexei Starovoitov }; 23530b2c86SAlexei Starovoitov __u32 ip_proto; 24530b2c86SAlexei Starovoitov }; 25530b2c86SAlexei Starovoitov 26530b2c86SAlexei Starovoitov struct pair { 27530b2c86SAlexei Starovoitov __u64 packets; 28530b2c86SAlexei Starovoitov __u64 bytes; 29530b2c86SAlexei Starovoitov }; 30530b2c86SAlexei Starovoitov 31530b2c86SAlexei Starovoitov int main(int argc, char **argv) 32530b2c86SAlexei Starovoitov { 33eb88d585SWilliam Tu struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; 34530b2c86SAlexei Starovoitov char filename[256]; 35530b2c86SAlexei Starovoitov FILE *f; 36a8744f25SMartin KaFai Lau int i, sock, err, id, key = PARSE_IP; 37a8744f25SMartin KaFai Lau struct bpf_prog_info info = {}; 38a8744f25SMartin KaFai Lau uint32_t info_len = sizeof(info); 39530b2c86SAlexei Starovoitov 40530b2c86SAlexei Starovoitov snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); 41eb88d585SWilliam Tu setrlimit(RLIMIT_MEMLOCK, &r); 42530b2c86SAlexei Starovoitov 43530b2c86SAlexei Starovoitov if (load_bpf_file(filename)) { 44530b2c86SAlexei Starovoitov printf("%s", bpf_log_buf); 45530b2c86SAlexei Starovoitov return 1; 46530b2c86SAlexei Starovoitov } 47530b2c86SAlexei Starovoitov 48a8744f25SMartin KaFai Lau /* Test fd array lookup which returns the id of the bpf_prog */ 49a8744f25SMartin KaFai Lau err = bpf_obj_get_info_by_fd(PARSE_IP_PROG_FD, &info, &info_len); 50a8744f25SMartin KaFai Lau assert(!err); 51a8744f25SMartin KaFai Lau err = bpf_map_lookup_elem(PROG_ARRAY_FD, &key, &id); 52a8744f25SMartin KaFai Lau assert(!err); 53a8744f25SMartin KaFai Lau assert(id == info.id); 54a8744f25SMartin KaFai Lau 55530b2c86SAlexei Starovoitov sock = open_raw_sock("lo"); 56530b2c86SAlexei Starovoitov 57530b2c86SAlexei Starovoitov assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd[4], 58530b2c86SAlexei Starovoitov sizeof(__u32)) == 0); 59530b2c86SAlexei Starovoitov 60530b2c86SAlexei Starovoitov if (argc > 1) 61530b2c86SAlexei Starovoitov f = popen("ping -c5 localhost", "r"); 62530b2c86SAlexei Starovoitov else 63530b2c86SAlexei Starovoitov f = popen("netperf -l 4 localhost", "r"); 64530b2c86SAlexei Starovoitov (void) f; 65530b2c86SAlexei Starovoitov 66530b2c86SAlexei Starovoitov for (i = 0; i < 5; i++) { 672b064fffSNaveen N. Rao struct bpf_flow_keys key = {}, next_key; 68530b2c86SAlexei Starovoitov struct pair value; 69530b2c86SAlexei Starovoitov 70530b2c86SAlexei Starovoitov sleep(1); 71530b2c86SAlexei Starovoitov printf("IP src.port -> dst.port bytes packets\n"); 72d40fc181SJoe Stringer while (bpf_map_get_next_key(map_fd[2], &key, &next_key) == 0) { 73d40fc181SJoe Stringer bpf_map_lookup_elem(map_fd[2], &next_key, &value); 74530b2c86SAlexei Starovoitov printf("%s.%05d -> %s.%05d %12lld %12lld\n", 75530b2c86SAlexei Starovoitov inet_ntoa((struct in_addr){htonl(next_key.src)}), 76530b2c86SAlexei Starovoitov next_key.port16[0], 77530b2c86SAlexei Starovoitov inet_ntoa((struct in_addr){htonl(next_key.dst)}), 78530b2c86SAlexei Starovoitov next_key.port16[1], 79530b2c86SAlexei Starovoitov value.bytes, value.packets); 80530b2c86SAlexei Starovoitov key = next_key; 81530b2c86SAlexei Starovoitov } 82530b2c86SAlexei Starovoitov } 83530b2c86SAlexei Starovoitov return 0; 84530b2c86SAlexei Starovoitov } 85