1 /* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc.
3 static const char *__doc__=
4 "XDP monitor tool, based on tracepoints\n"
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"
31 static int verbose = 1;
32 static bool debug = false;
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' },
42 static void usage(char *argv[])
45 printf("\nDOCUMENTATION:\n%s\n", __doc__);
47 printf(" Usage: %s (options-see-below)\n",
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);
56 printf("(internal short-option: -%c)",
63 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
69 res = clock_gettime(CLOCK_MONOTONIC, &t);
71 fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
74 return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
81 #define REDIR_RES_MAX 2
82 static const char *redir_names[REDIR_RES_MAX] = {
83 [REDIR_SUCCESS] = "Success",
84 [REDIR_ERROR] = "Error",
86 static const char *err2str(int err)
88 if (err < REDIR_RES_MAX)
89 return redir_names[err];
99 struct record xdp_redir[REDIR_RES_MAX];
102 static void stats_print_headers(bool err_only)
105 printf("\n%s\n", __doc_err_only__);
107 printf("%-14s %-10s %-18s %-9s\n",
108 "XDP_REDIRECT", "pps ", "pps-human-readable", "measure-period");
111 static void stats_print(struct stats_record *rec,
112 struct stats_record *prev,
120 for (; i < REDIR_RES_MAX; i++) {
121 struct record *r = &rec->xdp_redir[i];
122 struct record *p = &prev->xdp_redir[i];
129 packets = r->counter - p->counter;
130 period = r->timestamp - p->timestamp;
132 period_ = ((double) period / NANOSEC_PER_SEC);
133 pps = packets / period_;
137 printf("%-14s %-10.0f %'-18.0f %f\n",
138 err2str(i), pps, pps, period_);
142 static __u64 get_key32_value64_percpu(int fd, __u32 key)
144 /* For percpu maps, userspace gets a value per possible CPU */
145 unsigned int nr_cpus = bpf_num_possible_cpus();
146 __u64 values[nr_cpus];
150 if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
152 "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
156 /* Sum values from each CPU */
157 for (i = 0; i < nr_cpus; i++) {
163 static bool stats_collect(int fd, struct stats_record *rec)
167 /* TODO: Detect if someone unloaded the perf event_fd's, as
168 * this can happen by someone running perf-record -e
171 for (i = 0; i < REDIR_RES_MAX; i++) {
172 rec->xdp_redir[i].timestamp = gettime();
173 rec->xdp_redir[i].counter = get_key32_value64_percpu(fd, i);
178 static void stats_poll(int interval, bool err_only)
180 struct stats_record rec, prev;
183 memset(&rec, 0, sizeof(rec));
185 /* Trick to pretty printf with thousands separators use %' */
186 setlocale(LC_NUMERIC, "en_US");
190 printf("\n%s", __doc__);
192 /* TODO Need more advanced stats on error types */
194 printf(" - Stats map: %s\n", map_data[0].name);
195 map_fd = map_data[0].fd;
197 stats_print_headers(err_only);
201 memcpy(&prev, &rec, sizeof(rec));
202 stats_collect(map_fd, &rec);
203 stats_print(&rec, &prev, err_only);
209 void print_bpf_prog_info(void)
214 printf("Loaded BPF prog have %d bpf program(s)\n", prog_cnt);
215 for (i = 0; i < prog_cnt; i++) {
216 printf(" - prog_fd[%d] = fd(%d)\n", i, prog_fd[i]);
220 printf("Loaded BPF prog have %d map(s)\n", map_data_count);
221 for (i = 0; i < map_data_count; i++) {
222 char *name = map_data[i].name;
223 int fd = map_data[i].fd;
225 printf(" - map_data[%d] = fd(%d) name:%s\n", i, fd, name);
229 printf("Searching for (max:%d) event file descriptor(s)\n", prog_cnt);
230 for (i = 0; i < prog_cnt; i++) {
231 if (event_fd[i] != -1)
232 printf(" - event_fd[%d] = fd(%d)\n", i, event_fd[i]);
236 int main(int argc, char **argv)
238 int longindex = 0, opt;
239 int ret = EXIT_SUCCESS;
240 char bpf_obj_file[256];
242 /* Default settings: */
243 bool errors_only = true;
246 snprintf(bpf_obj_file, sizeof(bpf_obj_file), "%s_kern.o", argv[0]);
248 /* Parse commands line args */
249 while ((opt = getopt_long(argc, argv, "h",
250 long_options, &longindex)) != -1) {
259 interval = atoi(optarg);
268 if (load_bpf_file(bpf_obj_file)) {
269 printf("ERROR - bpf_log_buf: %s", bpf_log_buf);
273 printf("ERROR - load_bpf_file: %s\n", strerror(errno));
278 print_bpf_prog_info();
281 /* Unload/stop tracepoint event by closing fd's */
283 /* The prog_fd[i] and event_fd[i] depend on the
284 * order the functions was defined in _kern.c
286 close(event_fd[2]); /* tracepoint/xdp/xdp_redirect */
287 close(prog_fd[2]); /* func: trace_xdp_redirect */
288 close(event_fd[3]); /* tracepoint/xdp/xdp_redirect_map */
289 close(prog_fd[3]); /* func: trace_xdp_redirect_map */
292 stats_poll(interval, errors_only);