Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <stdio.h>
0003 #include <assert.h>
0004 #include <bpf/bpf.h>
0005 #include <bpf/libbpf.h>
0006 #include "sock_example.h"
0007 #include <unistd.h>
0008 #include <arpa/inet.h>
0009 
0010 struct flow_key_record {
0011     __be32 src;
0012     __be32 dst;
0013     union {
0014         __be32 ports;
0015         __be16 port16[2];
0016     };
0017     __u32 ip_proto;
0018 };
0019 
0020 struct pair {
0021     __u64 packets;
0022     __u64 bytes;
0023 };
0024 
0025 int main(int argc, char **argv)
0026 {
0027     int i, sock, key, fd, main_prog_fd, jmp_table_fd, hash_map_fd;
0028     struct bpf_program *prog;
0029     struct bpf_object *obj;
0030     const char *section;
0031     char filename[256];
0032     FILE *f;
0033 
0034     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0035 
0036     obj = bpf_object__open_file(filename, NULL);
0037     if (libbpf_get_error(obj)) {
0038         fprintf(stderr, "ERROR: opening BPF object file failed\n");
0039         return 0;
0040     }
0041 
0042     /* load BPF program */
0043     if (bpf_object__load(obj)) {
0044         fprintf(stderr, "ERROR: loading BPF object file failed\n");
0045         goto cleanup;
0046     }
0047 
0048     jmp_table_fd = bpf_object__find_map_fd_by_name(obj, "jmp_table");
0049     hash_map_fd = bpf_object__find_map_fd_by_name(obj, "hash_map");
0050     if (jmp_table_fd < 0 || hash_map_fd < 0) {
0051         fprintf(stderr, "ERROR: finding a map in obj file failed\n");
0052         goto cleanup;
0053     }
0054 
0055     bpf_object__for_each_program(prog, obj) {
0056         fd = bpf_program__fd(prog);
0057 
0058         section = bpf_program__section_name(prog);
0059         if (sscanf(section, "socket/%d", &key) != 1) {
0060             fprintf(stderr, "ERROR: finding prog failed\n");
0061             goto cleanup;
0062         }
0063 
0064         if (key == 0)
0065             main_prog_fd = fd;
0066         else
0067             bpf_map_update_elem(jmp_table_fd, &key, &fd, BPF_ANY);
0068     }
0069 
0070     sock = open_raw_sock("lo");
0071 
0072     /* attach BPF program to socket */
0073     assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &main_prog_fd,
0074               sizeof(__u32)) == 0);
0075 
0076     if (argc > 1)
0077         f = popen("ping -4 -c5 localhost", "r");
0078     else
0079         f = popen("netperf -l 4 localhost", "r");
0080     (void) f;
0081 
0082     for (i = 0; i < 5; i++) {
0083         struct flow_key_record key = {}, next_key;
0084         struct pair value;
0085 
0086         sleep(1);
0087         printf("IP     src.port -> dst.port               bytes      packets\n");
0088         while (bpf_map_get_next_key(hash_map_fd, &key, &next_key) == 0) {
0089             bpf_map_lookup_elem(hash_map_fd, &next_key, &value);
0090             printf("%s.%05d -> %s.%05d %12lld %12lld\n",
0091                    inet_ntoa((struct in_addr){htonl(next_key.src)}),
0092                    next_key.port16[0],
0093                    inet_ntoa((struct in_addr){htonl(next_key.dst)}),
0094                    next_key.port16[1],
0095                    value.bytes, value.packets);
0096             key = next_key;
0097         }
0098     }
0099 
0100 cleanup:
0101     bpf_object__close(obj);
0102     return 0;
0103 }