| .. | .. |
|---|
| 3 | 3 | #include <unistd.h> |
|---|
| 4 | 4 | #include <stdlib.h> |
|---|
| 5 | 5 | #include <signal.h> |
|---|
| 6 | | -#include <linux/bpf.h> |
|---|
| 7 | 6 | #include <string.h> |
|---|
| 8 | 7 | #include <sys/resource.h> |
|---|
| 9 | 8 | |
|---|
| 10 | 9 | #include <bpf/bpf.h> |
|---|
| 11 | | -#include "bpf_load.h" |
|---|
| 10 | +#include <bpf/libbpf.h> |
|---|
| 12 | 11 | #include "bpf_util.h" |
|---|
| 13 | 12 | |
|---|
| 14 | 13 | #define MAX_INDEX 64 |
|---|
| 15 | 14 | #define MAX_STARS 38 |
|---|
| 15 | + |
|---|
| 16 | +/* my_map, my_hist_map */ |
|---|
| 17 | +static int map_fd[2]; |
|---|
| 16 | 18 | |
|---|
| 17 | 19 | static void stars(char *str, long val, long max, int width) |
|---|
| 18 | 20 | { |
|---|
| .. | .. |
|---|
| 114 | 116 | |
|---|
| 115 | 117 | int main(int ac, char **argv) |
|---|
| 116 | 118 | { |
|---|
| 117 | | - struct rlimit r = {1024*1024, RLIM_INFINITY}; |
|---|
| 118 | | - char filename[256]; |
|---|
| 119 | + struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
|---|
| 119 | 120 | long key, next_key, value; |
|---|
| 121 | + struct bpf_link *links[2]; |
|---|
| 122 | + struct bpf_program *prog; |
|---|
| 123 | + struct bpf_object *obj; |
|---|
| 124 | + char filename[256]; |
|---|
| 125 | + int i, j = 0; |
|---|
| 120 | 126 | FILE *f; |
|---|
| 121 | | - int i; |
|---|
| 122 | | - |
|---|
| 123 | | - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 124 | 127 | |
|---|
| 125 | 128 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
|---|
| 126 | 129 | perror("setrlimit(RLIMIT_MEMLOCK)"); |
|---|
| 127 | 130 | return 1; |
|---|
| 128 | 131 | } |
|---|
| 129 | 132 | |
|---|
| 133 | + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
|---|
| 134 | + obj = bpf_object__open_file(filename, NULL); |
|---|
| 135 | + if (libbpf_get_error(obj)) { |
|---|
| 136 | + fprintf(stderr, "ERROR: opening BPF object file failed\n"); |
|---|
| 137 | + return 0; |
|---|
| 138 | + } |
|---|
| 139 | + |
|---|
| 140 | + /* load BPF program */ |
|---|
| 141 | + if (bpf_object__load(obj)) { |
|---|
| 142 | + fprintf(stderr, "ERROR: loading BPF object file failed\n"); |
|---|
| 143 | + goto cleanup; |
|---|
| 144 | + } |
|---|
| 145 | + |
|---|
| 146 | + map_fd[0] = bpf_object__find_map_fd_by_name(obj, "my_map"); |
|---|
| 147 | + map_fd[1] = bpf_object__find_map_fd_by_name(obj, "my_hist_map"); |
|---|
| 148 | + if (map_fd[0] < 0 || map_fd[1] < 0) { |
|---|
| 149 | + fprintf(stderr, "ERROR: finding a map in obj file failed\n"); |
|---|
| 150 | + goto cleanup; |
|---|
| 151 | + } |
|---|
| 152 | + |
|---|
| 130 | 153 | signal(SIGINT, int_exit); |
|---|
| 131 | 154 | signal(SIGTERM, int_exit); |
|---|
| 132 | 155 | |
|---|
| 133 | 156 | /* start 'ping' in the background to have some kfree_skb events */ |
|---|
| 134 | | - f = popen("ping -c5 localhost", "r"); |
|---|
| 157 | + f = popen("ping -4 -c5 localhost", "r"); |
|---|
| 135 | 158 | (void) f; |
|---|
| 136 | 159 | |
|---|
| 137 | 160 | /* start 'dd' in the background to have plenty of 'write' syscalls */ |
|---|
| 138 | 161 | f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r"); |
|---|
| 139 | 162 | (void) f; |
|---|
| 140 | 163 | |
|---|
| 141 | | - if (load_bpf_file(filename)) { |
|---|
| 142 | | - printf("%s", bpf_log_buf); |
|---|
| 143 | | - return 1; |
|---|
| 164 | + bpf_object__for_each_program(prog, obj) { |
|---|
| 165 | + links[j] = bpf_program__attach(prog); |
|---|
| 166 | + if (libbpf_get_error(links[j])) { |
|---|
| 167 | + fprintf(stderr, "ERROR: bpf_program__attach failed\n"); |
|---|
| 168 | + links[j] = NULL; |
|---|
| 169 | + goto cleanup; |
|---|
| 170 | + } |
|---|
| 171 | + j++; |
|---|
| 144 | 172 | } |
|---|
| 145 | 173 | |
|---|
| 146 | 174 | for (i = 0; i < 5; i++) { |
|---|
| .. | .. |
|---|
| 156 | 184 | } |
|---|
| 157 | 185 | print_hist(map_fd[1]); |
|---|
| 158 | 186 | |
|---|
| 187 | +cleanup: |
|---|
| 188 | + for (j--; j >= 0; j--) |
|---|
| 189 | + bpf_link__destroy(links[j]); |
|---|
| 190 | + |
|---|
| 191 | + bpf_object__close(obj); |
|---|
| 159 | 192 | return 0; |
|---|
| 160 | 193 | } |
|---|