Merge tag 'arm-newsoc-5.9' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-2.6-microblaze.git] / samples / bpf / xdp_redirect_cpu_user.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2017 Jesper Dangaard Brouer, Red Hat, Inc.
3  */
4 static const char *__doc__ =
5         " XDP redirect with a CPU-map type \"BPF_MAP_TYPE_CPUMAP\"";
6
7 #include <errno.h>
8 #include <signal.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <locale.h>
15 #include <sys/resource.h>
16 #include <sys/sysinfo.h>
17 #include <getopt.h>
18 #include <net/if.h>
19 #include <time.h>
20 #include <linux/limits.h>
21
22 #include <arpa/inet.h>
23 #include <linux/if_link.h>
24
25 /* How many xdp_progs are defined in _kern.c */
26 #define MAX_PROG 6
27
28 #include <bpf/bpf.h>
29 #include <bpf/libbpf.h>
30
31 #include "bpf_util.h"
32
33 static int ifindex = -1;
34 static char ifname_buf[IF_NAMESIZE];
35 static char *ifname;
36 static __u32 prog_id;
37
38 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
39 static int n_cpus;
40 static int cpu_map_fd;
41 static int rx_cnt_map_fd;
42 static int redirect_err_cnt_map_fd;
43 static int cpumap_enqueue_cnt_map_fd;
44 static int cpumap_kthread_cnt_map_fd;
45 static int cpus_available_map_fd;
46 static int cpus_count_map_fd;
47 static int cpus_iterator_map_fd;
48 static int exception_cnt_map_fd;
49
50 #define NUM_TP 5
51 struct bpf_link *tp_links[NUM_TP] = { 0 };
52 static int tp_cnt = 0;
53
54 /* Exit return codes */
55 #define EXIT_OK         0
56 #define EXIT_FAIL               1
57 #define EXIT_FAIL_OPTION        2
58 #define EXIT_FAIL_XDP           3
59 #define EXIT_FAIL_BPF           4
60 #define EXIT_FAIL_MEM           5
61
62 static const struct option long_options[] = {
63         {"help",        no_argument,            NULL, 'h' },
64         {"dev",         required_argument,      NULL, 'd' },
65         {"skb-mode",    no_argument,            NULL, 'S' },
66         {"sec",         required_argument,      NULL, 's' },
67         {"progname",    required_argument,      NULL, 'p' },
68         {"qsize",       required_argument,      NULL, 'q' },
69         {"cpu",         required_argument,      NULL, 'c' },
70         {"stress-mode", no_argument,            NULL, 'x' },
71         {"no-separators", no_argument,          NULL, 'z' },
72         {"force",       no_argument,            NULL, 'F' },
73         {0, 0, NULL,  0 }
74 };
75
76 static void int_exit(int sig)
77 {
78         __u32 curr_prog_id = 0;
79
80         if (ifindex > -1) {
81                 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
82                         printf("bpf_get_link_xdp_id failed\n");
83                         exit(EXIT_FAIL);
84                 }
85                 if (prog_id == curr_prog_id) {
86                         fprintf(stderr,
87                                 "Interrupted: Removing XDP program on ifindex:%d device:%s\n",
88                                 ifindex, ifname);
89                         bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
90                 } else if (!curr_prog_id) {
91                         printf("couldn't find a prog id on a given iface\n");
92                 } else {
93                         printf("program on interface changed, not removing\n");
94                 }
95         }
96         /* Detach tracepoints */
97         while (tp_cnt)
98                 bpf_link__destroy(tp_links[--tp_cnt]);
99
100         exit(EXIT_OK);
101 }
102
103 static void print_avail_progs(struct bpf_object *obj)
104 {
105         struct bpf_program *pos;
106
107         bpf_object__for_each_program(pos, obj) {
108                 if (bpf_program__is_xdp(pos))
109                         printf(" %s\n", bpf_program__title(pos, false));
110         }
111 }
112
113 static void usage(char *argv[], struct bpf_object *obj)
114 {
115         int i;
116
117         printf("\nDOCUMENTATION:\n%s\n", __doc__);
118         printf("\n");
119         printf(" Usage: %s (options-see-below)\n", argv[0]);
120         printf(" Listing options:\n");
121         for (i = 0; long_options[i].name != 0; i++) {
122                 printf(" --%-12s", long_options[i].name);
123                 if (long_options[i].flag != NULL)
124                         printf(" flag (internal value:%d)",
125                                 *long_options[i].flag);
126                 else
127                         printf(" short-option: -%c",
128                                 long_options[i].val);
129                 printf("\n");
130         }
131         printf("\n Programs to be used for --progname:\n");
132         print_avail_progs(obj);
133         printf("\n");
134 }
135
136 /* gettime returns the current time of day in nanoseconds.
137  * Cost: clock_gettime (ns) => 26ns (CLOCK_MONOTONIC)
138  *       clock_gettime (ns) =>  9ns (CLOCK_MONOTONIC_COARSE)
139  */
140 #define NANOSEC_PER_SEC 1000000000 /* 10^9 */
141 static __u64 gettime(void)
142 {
143         struct timespec t;
144         int res;
145
146         res = clock_gettime(CLOCK_MONOTONIC, &t);
147         if (res < 0) {
148                 fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
149                 exit(EXIT_FAIL);
150         }
151         return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
152 }
153
154 /* Common stats data record shared with _kern.c */
155 struct datarec {
156         __u64 processed;
157         __u64 dropped;
158         __u64 issue;
159 };
160 struct record {
161         __u64 timestamp;
162         struct datarec total;
163         struct datarec *cpu;
164 };
165 struct stats_record {
166         struct record rx_cnt;
167         struct record redir_err;
168         struct record kthread;
169         struct record exception;
170         struct record enq[];
171 };
172
173 static bool map_collect_percpu(int fd, __u32 key, struct record *rec)
174 {
175         /* For percpu maps, userspace gets a value per possible CPU */
176         unsigned int nr_cpus = bpf_num_possible_cpus();
177         struct datarec values[nr_cpus];
178         __u64 sum_processed = 0;
179         __u64 sum_dropped = 0;
180         __u64 sum_issue = 0;
181         int i;
182
183         if ((bpf_map_lookup_elem(fd, &key, values)) != 0) {
184                 fprintf(stderr,
185                         "ERR: bpf_map_lookup_elem failed key:0x%X\n", key);
186                 return false;
187         }
188         /* Get time as close as possible to reading map contents */
189         rec->timestamp = gettime();
190
191         /* Record and sum values from each CPU */
192         for (i = 0; i < nr_cpus; i++) {
193                 rec->cpu[i].processed = values[i].processed;
194                 sum_processed        += values[i].processed;
195                 rec->cpu[i].dropped = values[i].dropped;
196                 sum_dropped        += values[i].dropped;
197                 rec->cpu[i].issue = values[i].issue;
198                 sum_issue        += values[i].issue;
199         }
200         rec->total.processed = sum_processed;
201         rec->total.dropped   = sum_dropped;
202         rec->total.issue     = sum_issue;
203         return true;
204 }
205
206 static struct datarec *alloc_record_per_cpu(void)
207 {
208         unsigned int nr_cpus = bpf_num_possible_cpus();
209         struct datarec *array;
210
211         array = calloc(nr_cpus, sizeof(struct datarec));
212         if (!array) {
213                 fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
214                 exit(EXIT_FAIL_MEM);
215         }
216         return array;
217 }
218
219 static struct stats_record *alloc_stats_record(void)
220 {
221         struct stats_record *rec;
222         int i, size;
223
224         size = sizeof(*rec) + n_cpus * sizeof(struct record);
225         rec = malloc(size);
226         if (!rec) {
227                 fprintf(stderr, "Mem alloc error\n");
228                 exit(EXIT_FAIL_MEM);
229         }
230         memset(rec, 0, size);
231         rec->rx_cnt.cpu    = alloc_record_per_cpu();
232         rec->redir_err.cpu = alloc_record_per_cpu();
233         rec->kthread.cpu   = alloc_record_per_cpu();
234         rec->exception.cpu = alloc_record_per_cpu();
235         for (i = 0; i < n_cpus; i++)
236                 rec->enq[i].cpu = alloc_record_per_cpu();
237
238         return rec;
239 }
240
241 static void free_stats_record(struct stats_record *r)
242 {
243         int i;
244
245         for (i = 0; i < n_cpus; i++)
246                 free(r->enq[i].cpu);
247         free(r->exception.cpu);
248         free(r->kthread.cpu);
249         free(r->redir_err.cpu);
250         free(r->rx_cnt.cpu);
251         free(r);
252 }
253
254 static double calc_period(struct record *r, struct record *p)
255 {
256         double period_ = 0;
257         __u64 period = 0;
258
259         period = r->timestamp - p->timestamp;
260         if (period > 0)
261                 period_ = ((double) period / NANOSEC_PER_SEC);
262
263         return period_;
264 }
265
266 static __u64 calc_pps(struct datarec *r, struct datarec *p, double period_)
267 {
268         __u64 packets = 0;
269         __u64 pps = 0;
270
271         if (period_ > 0) {
272                 packets = r->processed - p->processed;
273                 pps = packets / period_;
274         }
275         return pps;
276 }
277
278 static __u64 calc_drop_pps(struct datarec *r, struct datarec *p, double period_)
279 {
280         __u64 packets = 0;
281         __u64 pps = 0;
282
283         if (period_ > 0) {
284                 packets = r->dropped - p->dropped;
285                 pps = packets / period_;
286         }
287         return pps;
288 }
289
290 static __u64 calc_errs_pps(struct datarec *r,
291                             struct datarec *p, double period_)
292 {
293         __u64 packets = 0;
294         __u64 pps = 0;
295
296         if (period_ > 0) {
297                 packets = r->issue - p->issue;
298                 pps = packets / period_;
299         }
300         return pps;
301 }
302
303 static void stats_print(struct stats_record *stats_rec,
304                         struct stats_record *stats_prev,
305                         char *prog_name)
306 {
307         unsigned int nr_cpus = bpf_num_possible_cpus();
308         double pps = 0, drop = 0, err = 0;
309         struct record *rec, *prev;
310         int to_cpu;
311         double t;
312         int i;
313
314         /* Header */
315         printf("Running XDP/eBPF prog_name:%s\n", prog_name);
316         printf("%-15s %-7s %-14s %-11s %-9s\n",
317                "XDP-cpumap", "CPU:to", "pps", "drop-pps", "extra-info");
318
319         /* XDP rx_cnt */
320         {
321                 char *fmt_rx = "%-15s %-7d %'-14.0f %'-11.0f %'-10.0f %s\n";
322                 char *fm2_rx = "%-15s %-7s %'-14.0f %'-11.0f\n";
323                 char *errstr = "";
324
325                 rec  = &stats_rec->rx_cnt;
326                 prev = &stats_prev->rx_cnt;
327                 t = calc_period(rec, prev);
328                 for (i = 0; i < nr_cpus; i++) {
329                         struct datarec *r = &rec->cpu[i];
330                         struct datarec *p = &prev->cpu[i];
331
332                         pps = calc_pps(r, p, t);
333                         drop = calc_drop_pps(r, p, t);
334                         err  = calc_errs_pps(r, p, t);
335                         if (err > 0)
336                                 errstr = "cpu-dest/err";
337                         if (pps > 0)
338                                 printf(fmt_rx, "XDP-RX",
339                                         i, pps, drop, err, errstr);
340                 }
341                 pps  = calc_pps(&rec->total, &prev->total, t);
342                 drop = calc_drop_pps(&rec->total, &prev->total, t);
343                 err  = calc_errs_pps(&rec->total, &prev->total, t);
344                 printf(fm2_rx, "XDP-RX", "total", pps, drop);
345         }
346
347         /* cpumap enqueue stats */
348         for (to_cpu = 0; to_cpu < n_cpus; to_cpu++) {
349                 char *fmt = "%-15s %3d:%-3d %'-14.0f %'-11.0f %'-10.2f %s\n";
350                 char *fm2 = "%-15s %3s:%-3d %'-14.0f %'-11.0f %'-10.2f %s\n";
351                 char *errstr = "";
352
353                 rec  =  &stats_rec->enq[to_cpu];
354                 prev = &stats_prev->enq[to_cpu];
355                 t = calc_period(rec, prev);
356                 for (i = 0; i < nr_cpus; i++) {
357                         struct datarec *r = &rec->cpu[i];
358                         struct datarec *p = &prev->cpu[i];
359
360                         pps  = calc_pps(r, p, t);
361                         drop = calc_drop_pps(r, p, t);
362                         err  = calc_errs_pps(r, p, t);
363                         if (err > 0) {
364                                 errstr = "bulk-average";
365                                 err = pps / err; /* calc average bulk size */
366                         }
367                         if (pps > 0)
368                                 printf(fmt, "cpumap-enqueue",
369                                        i, to_cpu, pps, drop, err, errstr);
370                 }
371                 pps = calc_pps(&rec->total, &prev->total, t);
372                 if (pps > 0) {
373                         drop = calc_drop_pps(&rec->total, &prev->total, t);
374                         err  = calc_errs_pps(&rec->total, &prev->total, t);
375                         if (err > 0) {
376                                 errstr = "bulk-average";
377                                 err = pps / err; /* calc average bulk size */
378                         }
379                         printf(fm2, "cpumap-enqueue",
380                                "sum", to_cpu, pps, drop, err, errstr);
381                 }
382         }
383
384         /* cpumap kthread stats */
385         {
386                 char *fmt_k = "%-15s %-7d %'-14.0f %'-11.0f %'-10.0f %s\n";
387                 char *fm2_k = "%-15s %-7s %'-14.0f %'-11.0f %'-10.0f %s\n";
388                 char *e_str = "";
389
390                 rec  = &stats_rec->kthread;
391                 prev = &stats_prev->kthread;
392                 t = calc_period(rec, prev);
393                 for (i = 0; i < nr_cpus; i++) {
394                         struct datarec *r = &rec->cpu[i];
395                         struct datarec *p = &prev->cpu[i];
396
397                         pps  = calc_pps(r, p, t);
398                         drop = calc_drop_pps(r, p, t);
399                         err  = calc_errs_pps(r, p, t);
400                         if (err > 0)
401                                 e_str = "sched";
402                         if (pps > 0)
403                                 printf(fmt_k, "cpumap_kthread",
404                                        i, pps, drop, err, e_str);
405                 }
406                 pps = calc_pps(&rec->total, &prev->total, t);
407                 drop = calc_drop_pps(&rec->total, &prev->total, t);
408                 err  = calc_errs_pps(&rec->total, &prev->total, t);
409                 if (err > 0)
410                         e_str = "sched-sum";
411                 printf(fm2_k, "cpumap_kthread", "total", pps, drop, err, e_str);
412         }
413
414         /* XDP redirect err tracepoints (very unlikely) */
415         {
416                 char *fmt_err = "%-15s %-7d %'-14.0f %'-11.0f\n";
417                 char *fm2_err = "%-15s %-7s %'-14.0f %'-11.0f\n";
418
419                 rec  = &stats_rec->redir_err;
420                 prev = &stats_prev->redir_err;
421                 t = calc_period(rec, prev);
422                 for (i = 0; i < nr_cpus; i++) {
423                         struct datarec *r = &rec->cpu[i];
424                         struct datarec *p = &prev->cpu[i];
425
426                         pps  = calc_pps(r, p, t);
427                         drop = calc_drop_pps(r, p, t);
428                         if (pps > 0)
429                                 printf(fmt_err, "redirect_err", i, pps, drop);
430                 }
431                 pps = calc_pps(&rec->total, &prev->total, t);
432                 drop = calc_drop_pps(&rec->total, &prev->total, t);
433                 printf(fm2_err, "redirect_err", "total", pps, drop);
434         }
435
436         /* XDP general exception tracepoints */
437         {
438                 char *fmt_err = "%-15s %-7d %'-14.0f %'-11.0f\n";
439                 char *fm2_err = "%-15s %-7s %'-14.0f %'-11.0f\n";
440
441                 rec  = &stats_rec->exception;
442                 prev = &stats_prev->exception;
443                 t = calc_period(rec, prev);
444                 for (i = 0; i < nr_cpus; i++) {
445                         struct datarec *r = &rec->cpu[i];
446                         struct datarec *p = &prev->cpu[i];
447
448                         pps  = calc_pps(r, p, t);
449                         drop = calc_drop_pps(r, p, t);
450                         if (pps > 0)
451                                 printf(fmt_err, "xdp_exception", i, pps, drop);
452                 }
453                 pps = calc_pps(&rec->total, &prev->total, t);
454                 drop = calc_drop_pps(&rec->total, &prev->total, t);
455                 printf(fm2_err, "xdp_exception", "total", pps, drop);
456         }
457
458         printf("\n");
459         fflush(stdout);
460 }
461
462 static void stats_collect(struct stats_record *rec)
463 {
464         int fd, i;
465
466         fd = rx_cnt_map_fd;
467         map_collect_percpu(fd, 0, &rec->rx_cnt);
468
469         fd = redirect_err_cnt_map_fd;
470         map_collect_percpu(fd, 1, &rec->redir_err);
471
472         fd = cpumap_enqueue_cnt_map_fd;
473         for (i = 0; i < n_cpus; i++)
474                 map_collect_percpu(fd, i, &rec->enq[i]);
475
476         fd = cpumap_kthread_cnt_map_fd;
477         map_collect_percpu(fd, 0, &rec->kthread);
478
479         fd = exception_cnt_map_fd;
480         map_collect_percpu(fd, 0, &rec->exception);
481 }
482
483
484 /* Pointer swap trick */
485 static inline void swap(struct stats_record **a, struct stats_record **b)
486 {
487         struct stats_record *tmp;
488
489         tmp = *a;
490         *a = *b;
491         *b = tmp;
492 }
493
494 static int create_cpu_entry(__u32 cpu, __u32 queue_size,
495                             __u32 avail_idx, bool new)
496 {
497         __u32 curr_cpus_count = 0;
498         __u32 key = 0;
499         int ret;
500
501         /* Add a CPU entry to cpumap, as this allocate a cpu entry in
502          * the kernel for the cpu.
503          */
504         ret = bpf_map_update_elem(cpu_map_fd, &cpu, &queue_size, 0);
505         if (ret) {
506                 fprintf(stderr, "Create CPU entry failed (err:%d)\n", ret);
507                 exit(EXIT_FAIL_BPF);
508         }
509
510         /* Inform bpf_prog's that a new CPU is available to select
511          * from via some control maps.
512          */
513         ret = bpf_map_update_elem(cpus_available_map_fd, &avail_idx, &cpu, 0);
514         if (ret) {
515                 fprintf(stderr, "Add to avail CPUs failed\n");
516                 exit(EXIT_FAIL_BPF);
517         }
518
519         /* When not replacing/updating existing entry, bump the count */
520         ret = bpf_map_lookup_elem(cpus_count_map_fd, &key, &curr_cpus_count);
521         if (ret) {
522                 fprintf(stderr, "Failed reading curr cpus_count\n");
523                 exit(EXIT_FAIL_BPF);
524         }
525         if (new) {
526                 curr_cpus_count++;
527                 ret = bpf_map_update_elem(cpus_count_map_fd, &key,
528                                           &curr_cpus_count, 0);
529                 if (ret) {
530                         fprintf(stderr, "Failed write curr cpus_count\n");
531                         exit(EXIT_FAIL_BPF);
532                 }
533         }
534         /* map_fd[7] = cpus_iterator */
535         printf("%s CPU:%u as idx:%u queue_size:%d (total cpus_count:%u)\n",
536                new ? "Add-new":"Replace", cpu, avail_idx,
537                queue_size, curr_cpus_count);
538
539         return 0;
540 }
541
542 /* CPUs are zero-indexed. Thus, add a special sentinel default value
543  * in map cpus_available to mark CPU index'es not configured
544  */
545 static void mark_cpus_unavailable(void)
546 {
547         __u32 invalid_cpu = n_cpus;
548         int ret, i;
549
550         for (i = 0; i < n_cpus; i++) {
551                 ret = bpf_map_update_elem(cpus_available_map_fd, &i,
552                                           &invalid_cpu, 0);
553                 if (ret) {
554                         fprintf(stderr, "Failed marking CPU unavailable\n");
555                         exit(EXIT_FAIL_BPF);
556                 }
557         }
558 }
559
560 /* Stress cpumap management code by concurrently changing underlying cpumap */
561 static void stress_cpumap(void)
562 {
563         /* Changing qsize will cause kernel to free and alloc a new
564          * bpf_cpu_map_entry, with an associated/complicated tear-down
565          * procedure.
566          */
567         create_cpu_entry(1,  1024, 0, false);
568         create_cpu_entry(1,     8, 0, false);
569         create_cpu_entry(1, 16000, 0, false);
570 }
571
572 static void stats_poll(int interval, bool use_separators, char *prog_name,
573                        bool stress_mode)
574 {
575         struct stats_record *record, *prev;
576
577         record = alloc_stats_record();
578         prev   = alloc_stats_record();
579         stats_collect(record);
580
581         /* Trick to pretty printf with thousands separators use %' */
582         if (use_separators)
583                 setlocale(LC_NUMERIC, "en_US");
584
585         while (1) {
586                 swap(&prev, &record);
587                 stats_collect(record);
588                 stats_print(record, prev, prog_name);
589                 sleep(interval);
590                 if (stress_mode)
591                         stress_cpumap();
592         }
593
594         free_stats_record(record);
595         free_stats_record(prev);
596 }
597
598 static struct bpf_link * attach_tp(struct bpf_object *obj,
599                                    const char *tp_category,
600                                    const char* tp_name)
601 {
602         struct bpf_program *prog;
603         struct bpf_link *link;
604         char sec_name[PATH_MAX];
605         int len;
606
607         len = snprintf(sec_name, PATH_MAX, "tracepoint/%s/%s",
608                        tp_category, tp_name);
609         if (len < 0)
610                 exit(EXIT_FAIL);
611
612         prog = bpf_object__find_program_by_title(obj, sec_name);
613         if (!prog) {
614                 fprintf(stderr, "ERR: finding progsec: %s\n", sec_name);
615                 exit(EXIT_FAIL_BPF);
616         }
617
618         link = bpf_program__attach_tracepoint(prog, tp_category, tp_name);
619         if (libbpf_get_error(link))
620                 exit(EXIT_FAIL_BPF);
621
622         return link;
623 }
624
625 static void init_tracepoints(struct bpf_object *obj) {
626         tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_redirect_err");
627         tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_redirect_map_err");
628         tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_exception");
629         tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_cpumap_enqueue");
630         tp_links[tp_cnt++] = attach_tp(obj, "xdp", "xdp_cpumap_kthread");
631 }
632
633 static int init_map_fds(struct bpf_object *obj)
634 {
635         /* Maps updated by tracepoints */
636         redirect_err_cnt_map_fd =
637                 bpf_object__find_map_fd_by_name(obj, "redirect_err_cnt");
638         exception_cnt_map_fd =
639                 bpf_object__find_map_fd_by_name(obj, "exception_cnt");
640         cpumap_enqueue_cnt_map_fd =
641                 bpf_object__find_map_fd_by_name(obj, "cpumap_enqueue_cnt");
642         cpumap_kthread_cnt_map_fd =
643                 bpf_object__find_map_fd_by_name(obj, "cpumap_kthread_cnt");
644
645         /* Maps used by XDP */
646         rx_cnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rx_cnt");
647         cpu_map_fd = bpf_object__find_map_fd_by_name(obj, "cpu_map");
648         cpus_available_map_fd =
649                 bpf_object__find_map_fd_by_name(obj, "cpus_available");
650         cpus_count_map_fd = bpf_object__find_map_fd_by_name(obj, "cpus_count");
651         cpus_iterator_map_fd =
652                 bpf_object__find_map_fd_by_name(obj, "cpus_iterator");
653
654         if (cpu_map_fd < 0 || rx_cnt_map_fd < 0 ||
655             redirect_err_cnt_map_fd < 0 || cpumap_enqueue_cnt_map_fd < 0 ||
656             cpumap_kthread_cnt_map_fd < 0 || cpus_available_map_fd < 0 ||
657             cpus_count_map_fd < 0 || cpus_iterator_map_fd < 0 ||
658             exception_cnt_map_fd < 0)
659                 return -ENOENT;
660
661         return 0;
662 }
663
664 int main(int argc, char **argv)
665 {
666         struct rlimit r = {10 * 1024 * 1024, RLIM_INFINITY};
667         char *prog_name = "xdp_cpu_map5_lb_hash_ip_pairs";
668         struct bpf_prog_load_attr prog_load_attr = {
669                 .prog_type      = BPF_PROG_TYPE_UNSPEC,
670         };
671         struct bpf_prog_info info = {};
672         __u32 info_len = sizeof(info);
673         bool use_separators = true;
674         bool stress_mode = false;
675         struct bpf_program *prog;
676         struct bpf_object *obj;
677         char filename[256];
678         int added_cpus = 0;
679         int longindex = 0;
680         int interval = 2;
681         int add_cpu = -1;
682         int opt, err;
683         int prog_fd;
684         __u32 qsize;
685
686         n_cpus = get_nprocs_conf();
687
688         /* Notice: choosing he queue size is very important with the
689          * ixgbe driver, because it's driver page recycling trick is
690          * dependend on pages being returned quickly.  The number of
691          * out-standing packets in the system must be less-than 2x
692          * RX-ring size.
693          */
694         qsize = 128+64;
695
696         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
697         prog_load_attr.file = filename;
698
699         if (setrlimit(RLIMIT_MEMLOCK, &r)) {
700                 perror("setrlimit(RLIMIT_MEMLOCK)");
701                 return 1;
702         }
703
704         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
705                 return EXIT_FAIL;
706
707         if (prog_fd < 0) {
708                 fprintf(stderr, "ERR: bpf_prog_load_xattr: %s\n",
709                         strerror(errno));
710                 return EXIT_FAIL;
711         }
712         init_tracepoints(obj);
713         if (init_map_fds(obj) < 0) {
714                 fprintf(stderr, "bpf_object__find_map_fd_by_name failed\n");
715                 return EXIT_FAIL;
716         }
717         mark_cpus_unavailable();
718
719         /* Parse commands line args */
720         while ((opt = getopt_long(argc, argv, "hSd:s:p:q:c:xzF",
721                                   long_options, &longindex)) != -1) {
722                 switch (opt) {
723                 case 'd':
724                         if (strlen(optarg) >= IF_NAMESIZE) {
725                                 fprintf(stderr, "ERR: --dev name too long\n");
726                                 goto error;
727                         }
728                         ifname = (char *)&ifname_buf;
729                         strncpy(ifname, optarg, IF_NAMESIZE);
730                         ifindex = if_nametoindex(ifname);
731                         if (ifindex == 0) {
732                                 fprintf(stderr,
733                                         "ERR: --dev name unknown err(%d):%s\n",
734                                         errno, strerror(errno));
735                                 goto error;
736                         }
737                         break;
738                 case 's':
739                         interval = atoi(optarg);
740                         break;
741                 case 'S':
742                         xdp_flags |= XDP_FLAGS_SKB_MODE;
743                         break;
744                 case 'x':
745                         stress_mode = true;
746                         break;
747                 case 'z':
748                         use_separators = false;
749                         break;
750                 case 'p':
751                         /* Selecting eBPF prog to load */
752                         prog_name = optarg;
753                         break;
754                 case 'c':
755                         /* Add multiple CPUs */
756                         add_cpu = strtoul(optarg, NULL, 0);
757                         if (add_cpu >= n_cpus) {
758                                 fprintf(stderr,
759                                 "--cpu nr too large for cpumap err(%d):%s\n",
760                                         errno, strerror(errno));
761                                 goto error;
762                         }
763                         create_cpu_entry(add_cpu, qsize, added_cpus, true);
764                         added_cpus++;
765                         break;
766                 case 'q':
767                         qsize = atoi(optarg);
768                         break;
769                 case 'F':
770                         xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
771                         break;
772                 case 'h':
773                 error:
774                 default:
775                         usage(argv, obj);
776                         return EXIT_FAIL_OPTION;
777                 }
778         }
779
780         if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
781                 xdp_flags |= XDP_FLAGS_DRV_MODE;
782
783         /* Required option */
784         if (ifindex == -1) {
785                 fprintf(stderr, "ERR: required option --dev missing\n");
786                 usage(argv, obj);
787                 return EXIT_FAIL_OPTION;
788         }
789         /* Required option */
790         if (add_cpu == -1) {
791                 fprintf(stderr, "ERR: required option --cpu missing\n");
792                 fprintf(stderr, " Specify multiple --cpu option to add more\n");
793                 usage(argv, obj);
794                 return EXIT_FAIL_OPTION;
795         }
796
797         /* Remove XDP program when program is interrupted or killed */
798         signal(SIGINT, int_exit);
799         signal(SIGTERM, int_exit);
800
801         prog = bpf_object__find_program_by_title(obj, prog_name);
802         if (!prog) {
803                 fprintf(stderr, "bpf_object__find_program_by_title failed\n");
804                 return EXIT_FAIL;
805         }
806
807         prog_fd = bpf_program__fd(prog);
808         if (prog_fd < 0) {
809                 fprintf(stderr, "bpf_program__fd failed\n");
810                 return EXIT_FAIL;
811         }
812
813         if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
814                 fprintf(stderr, "link set xdp fd failed\n");
815                 return EXIT_FAIL_XDP;
816         }
817
818         err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
819         if (err) {
820                 printf("can't get prog info - %s\n", strerror(errno));
821                 return err;
822         }
823         prog_id = info.id;
824
825         stats_poll(interval, use_separators, prog_name, stress_mode);
826         return EXIT_OK;
827 }