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