Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 1 | /* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc. |
| 2 | */ |
| 3 | static const char *__doc__= |
| 4 | "XDP monitor tool, based on tracepoints\n" |
| 5 | ; |
| 6 | |
| 7 | static const char *__doc_err_only__= |
| 8 | " NOTICE: Only tracking XDP redirect errors\n" |
| 9 | " Enable TX success stats via '--stats'\n" |
| 10 | " (which comes with a per packet processing overhead)\n" |
| 11 | ; |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdint.h> |
| 18 | #include <string.h> |
| 19 | #include <ctype.h> |
| 20 | #include <unistd.h> |
| 21 | #include <locale.h> |
| 22 | |
| 23 | #include <getopt.h> |
| 24 | #include <net/if.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include "libbpf.h" |
| 28 | #include "bpf_load.h" |
| 29 | #include "bpf_util.h" |
| 30 | |
| 31 | static int verbose = 1; |
| 32 | static bool debug = false; |
| 33 | |
| 34 | static const struct option long_options[] = { |
| 35 | {"help", no_argument, NULL, 'h' }, |
| 36 | {"debug", no_argument, NULL, 'D' }, |
| 37 | {"stats", no_argument, NULL, 'S' }, |
| 38 | {"sec", required_argument, NULL, 's' }, |
| 39 | {0, 0, NULL, 0 } |
| 40 | }; |
| 41 | |
| 42 | static void usage(char *argv[]) |
| 43 | { |
| 44 | int i; |
| 45 | printf("\nDOCUMENTATION:\n%s\n", __doc__); |
| 46 | printf("\n"); |
| 47 | printf(" Usage: %s (options-see-below)\n", |
| 48 | argv[0]); |
| 49 | printf(" Listing options:\n"); |
| 50 | for (i = 0; long_options[i].name != 0; i++) { |
| 51 | printf(" --%-15s", long_options[i].name); |
| 52 | if (long_options[i].flag != NULL) |
| 53 | printf(" flag (internal value:%d)", |
| 54 | *long_options[i].flag); |
| 55 | else |
| 56 | printf("(internal short-option: -%c)", |
| 57 | long_options[i].val); |
| 58 | printf("\n"); |
| 59 | } |
| 60 | printf("\n"); |
| 61 | } |
| 62 | |
| 63 | #define NANOSEC_PER_SEC 1000000000 /* 10^9 */ |
Stephen Hemminger | 0929567 | 2017-10-01 14:07:34 -0700 | [diff] [blame] | 64 | static __u64 gettime(void) |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 65 | { |
| 66 | struct timespec t; |
| 67 | int res; |
| 68 | |
| 69 | res = clock_gettime(CLOCK_MONOTONIC, &t); |
| 70 | if (res < 0) { |
| 71 | fprintf(stderr, "Error with gettimeofday! (%i)\n", res); |
| 72 | exit(EXIT_FAILURE); |
| 73 | } |
| 74 | return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec; |
| 75 | } |
| 76 | |
| 77 | enum { |
| 78 | REDIR_SUCCESS = 0, |
| 79 | REDIR_ERROR = 1, |
| 80 | }; |
| 81 | #define REDIR_RES_MAX 2 |
| 82 | static const char *redir_names[REDIR_RES_MAX] = { |
| 83 | [REDIR_SUCCESS] = "Success", |
| 84 | [REDIR_ERROR] = "Error", |
| 85 | }; |
| 86 | static const char *err2str(int err) |
| 87 | { |
| 88 | if (err < REDIR_RES_MAX) |
| 89 | return redir_names[err]; |
| 90 | return NULL; |
| 91 | } |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 92 | /* enum xdp_action */ |
| 93 | #define XDP_UNKNOWN XDP_REDIRECT + 1 |
| 94 | #define XDP_ACTION_MAX (XDP_UNKNOWN + 1) |
| 95 | static const char *xdp_action_names[XDP_ACTION_MAX] = { |
| 96 | [XDP_ABORTED] = "XDP_ABORTED", |
| 97 | [XDP_DROP] = "XDP_DROP", |
| 98 | [XDP_PASS] = "XDP_PASS", |
| 99 | [XDP_TX] = "XDP_TX", |
| 100 | [XDP_REDIRECT] = "XDP_REDIRECT", |
| 101 | [XDP_UNKNOWN] = "XDP_UNKNOWN", |
| 102 | }; |
| 103 | static const char *action2str(int action) |
| 104 | { |
| 105 | if (action < XDP_ACTION_MAX) |
| 106 | return xdp_action_names[action]; |
| 107 | return NULL; |
| 108 | } |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 109 | |
| 110 | struct record { |
| 111 | __u64 counter; |
| 112 | __u64 timestamp; |
| 113 | }; |
| 114 | |
| 115 | struct stats_record { |
| 116 | struct record xdp_redir[REDIR_RES_MAX]; |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 117 | struct record xdp_exception[XDP_ACTION_MAX]; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static void stats_print_headers(bool err_only) |
| 121 | { |
| 122 | if (err_only) |
| 123 | printf("\n%s\n", __doc_err_only__); |
| 124 | |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 125 | printf("%-14s %-11s %-10s %-18s %-9s\n", |
| 126 | "ACTION", "result", "pps ", "pps-human-readable", "measure-period"); |
| 127 | } |
| 128 | |
| 129 | static double calc_period(struct record *r, struct record *p) |
| 130 | { |
| 131 | double period_ = 0; |
| 132 | __u64 period = 0; |
| 133 | |
| 134 | period = r->timestamp - p->timestamp; |
| 135 | if (period > 0) |
| 136 | period_ = ((double) period / NANOSEC_PER_SEC); |
| 137 | |
| 138 | return period_; |
| 139 | } |
| 140 | |
| 141 | static double calc_pps(struct record *r, struct record *p, double period) |
| 142 | { |
| 143 | __u64 packets = 0; |
| 144 | double pps = 0; |
| 145 | |
| 146 | if (period > 0) { |
| 147 | packets = r->counter - p->counter; |
| 148 | pps = packets / period; |
| 149 | } |
| 150 | return pps; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void stats_print(struct stats_record *rec, |
| 154 | struct stats_record *prev, |
| 155 | bool err_only) |
| 156 | { |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 157 | double period = 0, pps = 0; |
| 158 | struct record *r, *p; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 159 | int i = 0; |
| 160 | |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 161 | char *fmt = "%-14s %-11s %-10.0f %'-18.0f %f\n"; |
| 162 | |
| 163 | /* tracepoint: xdp:xdp_redirect_* */ |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 164 | if (err_only) |
| 165 | i = REDIR_ERROR; |
| 166 | |
| 167 | for (; i < REDIR_RES_MAX; i++) { |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 168 | r = &rec->xdp_redir[i]; |
| 169 | p = &prev->xdp_redir[i]; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 170 | |
| 171 | if (p->timestamp) { |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 172 | period = calc_period(r, p); |
| 173 | pps = calc_pps(r, p, period); |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 174 | } |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 175 | printf(fmt, "XDP_REDIRECT", err2str(i), pps, pps, period); |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 176 | } |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 177 | |
| 178 | /* tracepoint: xdp:xdp_exception */ |
| 179 | for (i = 0; i < XDP_ACTION_MAX; i++) { |
| 180 | r = &rec->xdp_exception[i]; |
| 181 | p = &prev->xdp_exception[i]; |
| 182 | if (p->timestamp) { |
| 183 | period = calc_period(r, p); |
| 184 | pps = calc_pps(r, p, period); |
| 185 | } |
| 186 | if (pps > 0) |
| 187 | printf(fmt, action2str(i), "Exception", |
| 188 | pps, pps, period); |
| 189 | } |
| 190 | printf("\n"); |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static __u64 get_key32_value64_percpu(int fd, __u32 key) |
| 194 | { |
| 195 | /* For percpu maps, userspace gets a value per possible CPU */ |
| 196 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
| 197 | __u64 values[nr_cpus]; |
| 198 | __u64 sum = 0; |
| 199 | int i; |
| 200 | |
| 201 | if ((bpf_map_lookup_elem(fd, &key, values)) != 0) { |
| 202 | fprintf(stderr, |
| 203 | "ERR: bpf_map_lookup_elem failed key:0x%X\n", key); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | /* Sum values from each CPU */ |
| 208 | for (i = 0; i < nr_cpus; i++) { |
| 209 | sum += values[i]; |
| 210 | } |
| 211 | return sum; |
| 212 | } |
| 213 | |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 214 | static bool stats_collect(struct stats_record *rec) |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 215 | { |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 216 | int fd; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 217 | int i; |
| 218 | |
| 219 | /* TODO: Detect if someone unloaded the perf event_fd's, as |
| 220 | * this can happen by someone running perf-record -e |
| 221 | */ |
| 222 | |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 223 | fd = map_data[0].fd; /* map0: redirect_err_cnt */ |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 224 | for (i = 0; i < REDIR_RES_MAX; i++) { |
| 225 | rec->xdp_redir[i].timestamp = gettime(); |
| 226 | rec->xdp_redir[i].counter = get_key32_value64_percpu(fd, i); |
| 227 | } |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 228 | |
| 229 | fd = map_data[1].fd; /* map1: exception_cnt */ |
| 230 | for (i = 0; i < XDP_ACTION_MAX; i++) { |
| 231 | rec->xdp_exception[i].timestamp = gettime(); |
| 232 | rec->xdp_exception[i].counter = get_key32_value64_percpu(fd, i); |
| 233 | } |
| 234 | |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
| 238 | static void stats_poll(int interval, bool err_only) |
| 239 | { |
| 240 | struct stats_record rec, prev; |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 241 | |
| 242 | memset(&rec, 0, sizeof(rec)); |
| 243 | |
| 244 | /* Trick to pretty printf with thousands separators use %' */ |
| 245 | setlocale(LC_NUMERIC, "en_US"); |
| 246 | |
| 247 | /* Header */ |
| 248 | if (verbose) |
| 249 | printf("\n%s", __doc__); |
| 250 | |
| 251 | /* TODO Need more advanced stats on error types */ |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 252 | if (verbose) { |
| 253 | printf(" - Stats map0: %s\n", map_data[0].name); |
| 254 | printf(" - Stats map1: %s\n", map_data[1].name); |
| 255 | printf("\n"); |
| 256 | } |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 257 | fflush(stdout); |
| 258 | |
| 259 | while (1) { |
| 260 | memcpy(&prev, &rec, sizeof(rec)); |
Jesper Dangaard Brouer | 280b058 | 2017-10-06 10:41:46 +0200 | [diff] [blame^] | 261 | stats_collect(&rec); |
| 262 | stats_print_headers(err_only); |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 263 | stats_print(&rec, &prev, err_only); |
| 264 | fflush(stdout); |
| 265 | sleep(interval); |
| 266 | } |
| 267 | } |
| 268 | |
Stephen Hemminger | 0929567 | 2017-10-01 14:07:34 -0700 | [diff] [blame] | 269 | static void print_bpf_prog_info(void) |
Jesper Dangaard Brouer | 3ffab54 | 2017-08-29 16:38:11 +0200 | [diff] [blame] | 270 | { |
| 271 | int i; |
| 272 | |
| 273 | /* Prog info */ |
| 274 | printf("Loaded BPF prog have %d bpf program(s)\n", prog_cnt); |
| 275 | for (i = 0; i < prog_cnt; i++) { |
| 276 | printf(" - prog_fd[%d] = fd(%d)\n", i, prog_fd[i]); |
| 277 | } |
| 278 | |
| 279 | /* Maps info */ |
| 280 | printf("Loaded BPF prog have %d map(s)\n", map_data_count); |
| 281 | for (i = 0; i < map_data_count; i++) { |
| 282 | char *name = map_data[i].name; |
| 283 | int fd = map_data[i].fd; |
| 284 | |
| 285 | printf(" - map_data[%d] = fd(%d) name:%s\n", i, fd, name); |
| 286 | } |
| 287 | |
| 288 | /* Event info */ |
| 289 | printf("Searching for (max:%d) event file descriptor(s)\n", prog_cnt); |
| 290 | for (i = 0; i < prog_cnt; i++) { |
| 291 | if (event_fd[i] != -1) |
| 292 | printf(" - event_fd[%d] = fd(%d)\n", i, event_fd[i]); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | int main(int argc, char **argv) |
| 297 | { |
| 298 | int longindex = 0, opt; |
| 299 | int ret = EXIT_SUCCESS; |
| 300 | char bpf_obj_file[256]; |
| 301 | |
| 302 | /* Default settings: */ |
| 303 | bool errors_only = true; |
| 304 | int interval = 2; |
| 305 | |
| 306 | snprintf(bpf_obj_file, sizeof(bpf_obj_file), "%s_kern.o", argv[0]); |
| 307 | |
| 308 | /* Parse commands line args */ |
| 309 | while ((opt = getopt_long(argc, argv, "h", |
| 310 | long_options, &longindex)) != -1) { |
| 311 | switch (opt) { |
| 312 | case 'D': |
| 313 | debug = true; |
| 314 | break; |
| 315 | case 'S': |
| 316 | errors_only = false; |
| 317 | break; |
| 318 | case 's': |
| 319 | interval = atoi(optarg); |
| 320 | break; |
| 321 | case 'h': |
| 322 | default: |
| 323 | usage(argv); |
| 324 | return EXIT_FAILURE; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (load_bpf_file(bpf_obj_file)) { |
| 329 | printf("ERROR - bpf_log_buf: %s", bpf_log_buf); |
| 330 | return 1; |
| 331 | } |
| 332 | if (!prog_fd[0]) { |
| 333 | printf("ERROR - load_bpf_file: %s\n", strerror(errno)); |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | if (debug) { |
| 338 | print_bpf_prog_info(); |
| 339 | } |
| 340 | |
| 341 | /* Unload/stop tracepoint event by closing fd's */ |
| 342 | if (errors_only) { |
| 343 | /* The prog_fd[i] and event_fd[i] depend on the |
| 344 | * order the functions was defined in _kern.c |
| 345 | */ |
| 346 | close(event_fd[2]); /* tracepoint/xdp/xdp_redirect */ |
| 347 | close(prog_fd[2]); /* func: trace_xdp_redirect */ |
| 348 | close(event_fd[3]); /* tracepoint/xdp/xdp_redirect_map */ |
| 349 | close(prog_fd[3]); /* func: trace_xdp_redirect_map */ |
| 350 | } |
| 351 | |
| 352 | stats_poll(interval, errors_only); |
| 353 | |
| 354 | return ret; |
| 355 | } |