GNU Linux-libre 4.14.295-gnu1
[releases.git] / samples / bpf / xdp_monitor_user.c
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 */
64 __u64 gettime(void)
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 }
92
93 struct record {
94         __u64 counter;
95         __u64 timestamp;
96 };
97
98 struct stats_record {
99         struct record xdp_redir[REDIR_RES_MAX];
100 };
101
102 static void stats_print_headers(bool err_only)
103 {
104         if (err_only)
105                 printf("\n%s\n", __doc_err_only__);
106
107         printf("%-14s %-10s %-18s %-9s\n",
108                "XDP_REDIRECT", "pps ", "pps-human-readable", "measure-period");
109 }
110
111 static void stats_print(struct stats_record *rec,
112                         struct stats_record *prev,
113                         bool err_only)
114 {
115         int i = 0;
116
117         if (err_only)
118                 i = REDIR_ERROR;
119
120         for (; i < REDIR_RES_MAX; i++) {
121                 struct record *r = &rec->xdp_redir[i];
122                 struct record *p = &prev->xdp_redir[i];
123                 __u64 period  = 0;
124                 __u64 packets = 0;
125                 double pps = 0;
126                 double period_ = 0;
127
128                 if (p->timestamp) {
129                         packets = r->counter - p->counter;
130                         period  = r->timestamp - p->timestamp;
131                         if (period > 0) {
132                                 period_ = ((double) period / NANOSEC_PER_SEC);
133                                 pps = packets / period_;
134                         }
135                 }
136
137                 printf("%-14s %-10.0f %'-18.0f %f\n",
138                        err2str(i), pps, pps, period_);
139         }
140 }
141
142 static __u64 get_key32_value64_percpu(int fd, __u32 key)
143 {
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];
147         __u64 sum = 0;
148         int i;
149
150         if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
151                 fprintf(stderr,
152                         "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
153                 return 0;
154         }
155
156         /* Sum values from each CPU */
157         for (i = 0; i < nr_cpus; i++) {
158                 sum += values[i];
159         }
160         return sum;
161 }
162
163 static bool stats_collect(int fd, struct stats_record *rec)
164 {
165         int i;
166
167         /* TODO: Detect if someone unloaded the perf event_fd's, as
168          * this can happen by someone running perf-record -e
169          */
170
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);
174         }
175         return true;
176 }
177
178 static void stats_poll(int interval, bool err_only)
179 {
180         struct stats_record rec, prev;
181         int map_fd;
182
183         memset(&rec, 0, sizeof(rec));
184
185         /* Trick to pretty printf with thousands separators use %' */
186         setlocale(LC_NUMERIC, "en_US");
187
188         /* Header */
189         if (verbose)
190                 printf("\n%s", __doc__);
191
192         /* TODO Need more advanced stats on error types */
193         if (verbose)
194                 printf(" - Stats map: %s\n", map_data[0].name);
195         map_fd = map_data[0].fd;
196
197         stats_print_headers(err_only);
198         fflush(stdout);
199
200         while (1) {
201                 memcpy(&prev, &rec, sizeof(rec));
202                 stats_collect(map_fd, &rec);
203                 stats_print(&rec, &prev, err_only);
204                 fflush(stdout);
205                 sleep(interval);
206         }
207 }
208
209 void print_bpf_prog_info(void)
210 {
211         int i;
212
213         /* Prog info */
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]);
217         }
218
219         /* Maps info */
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;
224
225                 printf(" - map_data[%d] = fd(%d) name:%s\n", i, fd, name);
226         }
227
228         /* Event info */
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]);
233         }
234 }
235
236 int main(int argc, char **argv)
237 {
238         int longindex = 0, opt;
239         int ret = EXIT_SUCCESS;
240         char bpf_obj_file[256];
241
242         /* Default settings: */
243         bool errors_only = true;
244         int interval = 2;
245
246         snprintf(bpf_obj_file, sizeof(bpf_obj_file), "%s_kern.o", argv[0]);
247
248         /* Parse commands line args */
249         while ((opt = getopt_long(argc, argv, "h",
250                                   long_options, &longindex)) != -1) {
251                 switch (opt) {
252                 case 'D':
253                         debug = true;
254                         break;
255                 case 'S':
256                         errors_only = false;
257                         break;
258                 case 's':
259                         interval = atoi(optarg);
260                         break;
261                 case 'h':
262                 default:
263                         usage(argv);
264                         return EXIT_FAILURE;
265                 }
266         }
267
268         if (load_bpf_file(bpf_obj_file)) {
269                 printf("ERROR - bpf_log_buf: %s", bpf_log_buf);
270                 return 1;
271         }
272         if (!prog_fd[0]) {
273                 printf("ERROR - load_bpf_file: %s\n", strerror(errno));
274                 return 1;
275         }
276
277         if (debug) {
278                 print_bpf_prog_info();
279         }
280
281         /* Unload/stop tracepoint event by closing fd's */
282         if (errors_only) {
283                 /* The prog_fd[i] and event_fd[i] depend on the
284                  * order the functions was defined in _kern.c
285                  */
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 */
290         }
291
292         stats_poll(interval, errors_only);
293
294         return ret;
295 }