0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/bpf.h>
0014 #include <signal.h>
0015 #include <stdio.h>
0016 #include <stdlib.h>
0017 #include <string.h>
0018 #include <unistd.h>
0019 #include <sys/types.h>
0020 #include <limits.h>
0021
0022 #include <getopt.h>
0023 #include <net/if.h>
0024
0025 #include <bpf/bpf.h>
0026 #include "bpf_util.h"
0027 #include <bpf/libbpf.h>
0028
0029 static struct bpf_link *tp_links[3];
0030 static struct bpf_object *obj;
0031 static int map_fd[2];
0032 static int tp_cnt;
0033
0034 static void dump_counts(int fd)
0035 {
0036 __u32 key;
0037 __u64 value;
0038
0039 for (key = 0; key < 256; key++) {
0040 if (bpf_map_lookup_elem(fd, &key, &value)) {
0041 printf("failed to read key %u\n", key);
0042 continue;
0043 }
0044 if (value)
0045 printf("0x%02x : %llu\n", key, value);
0046 }
0047 }
0048
0049 static void dump_all_counts(void)
0050 {
0051 printf("Read 'Class : count'\n");
0052 dump_counts(map_fd[0]);
0053 printf("Write 'Class : count'\n");
0054 dump_counts(map_fd[1]);
0055 }
0056
0057 static void dump_exit(int sig)
0058 {
0059 dump_all_counts();
0060
0061 while (tp_cnt)
0062 bpf_link__destroy(tp_links[--tp_cnt]);
0063
0064 bpf_object__close(obj);
0065 exit(0);
0066 }
0067
0068 static const struct option long_options[] = {
0069 {"help", no_argument, NULL, 'h'},
0070 {"delay", required_argument, NULL, 'd'},
0071 };
0072
0073 static void usage(char *cmd)
0074 {
0075 printf("eBPF test program to count packets from various IP addresses\n"
0076 "Usage: %s <options>\n"
0077 " --help, -h this menu\n"
0078 " --delay, -d <delay> wait <delay> sec between prints [1 - 1000000]\n"
0079 , cmd
0080 );
0081 }
0082
0083 int main(int argc, char **argv)
0084 {
0085 struct bpf_program *prog;
0086 unsigned long delay = 5;
0087 char filename[256];
0088 int longindex = 0;
0089 int opt, err = -1;
0090
0091 while ((opt = getopt_long(argc, argv, "hd:rSw",
0092 long_options, &longindex)) != -1) {
0093 switch (opt) {
0094 case 'd':
0095 delay = strtoul(optarg, NULL, 0);
0096 if (delay == ULONG_MAX || delay < 0 ||
0097 delay > 1000000) {
0098 fprintf(stderr, "ERROR: invalid delay : %s\n",
0099 optarg);
0100 usage(argv[0]);
0101 return 1;
0102 }
0103 break;
0104 default:
0105 case 'h':
0106 usage(argv[0]);
0107 return 1;
0108 }
0109 }
0110
0111
0112 signal(SIGINT, dump_exit);
0113 signal(SIGTERM, dump_exit);
0114
0115 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0116 obj = bpf_object__open_file(filename, NULL);
0117 if (libbpf_get_error(obj)) {
0118 fprintf(stderr, "ERROR: opening BPF object file failed\n");
0119 return err;
0120 }
0121
0122
0123 if (bpf_object__load(obj)) {
0124 fprintf(stderr, "ERROR: loading BPF object file failed\n");
0125 goto cleanup;
0126 }
0127
0128 map_fd[0] = bpf_object__find_map_fd_by_name(obj, "read_count");
0129 map_fd[1] = bpf_object__find_map_fd_by_name(obj, "write_count");
0130 if (map_fd[0] < 0 || map_fd[1] < 0) {
0131 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
0132 goto cleanup;
0133 }
0134
0135 bpf_object__for_each_program(prog, obj) {
0136 tp_links[tp_cnt] = bpf_program__attach(prog);
0137 if (libbpf_get_error(tp_links[tp_cnt])) {
0138 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
0139 tp_links[tp_cnt] = NULL;
0140 goto cleanup;
0141 }
0142 tp_cnt++;
0143 }
0144
0145 while (1) {
0146 sleep(delay);
0147 dump_all_counts();
0148 }
0149 err = 0;
0150
0151 cleanup:
0152
0153 while (tp_cnt)
0154 bpf_link__destroy(tp_links[--tp_cnt]);
0155
0156 bpf_object__close(obj);
0157 return err;
0158 }