Merge git://www.linux-watchdog.org/linux-watchdog
[sfrench/cifs-2.6.git] / samples / bpf / xdp1_user.c
1 /* Copyright (c) 2016 PLUMgrid
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  */
7 #include <linux/bpf.h>
8 #include <linux/if_link.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <libgen.h>
17
18 #include "bpf_load.h"
19 #include "bpf_util.h"
20 #include "libbpf.h"
21
22 static int ifindex;
23 static __u32 xdp_flags;
24
25 static void int_exit(int sig)
26 {
27         set_link_xdp_fd(ifindex, -1, xdp_flags);
28         exit(0);
29 }
30
31 /* simple per-protocol drop counter
32  */
33 static void poll_stats(int interval)
34 {
35         unsigned int nr_cpus = bpf_num_possible_cpus();
36         const unsigned int nr_keys = 256;
37         __u64 values[nr_cpus], prev[nr_keys][nr_cpus];
38         __u32 key;
39         int i;
40
41         memset(prev, 0, sizeof(prev));
42
43         while (1) {
44                 sleep(interval);
45
46                 for (key = 0; key < nr_keys; key++) {
47                         __u64 sum = 0;
48
49                         assert(bpf_map_lookup_elem(map_fd[0], &key, values) == 0);
50                         for (i = 0; i < nr_cpus; i++)
51                                 sum += (values[i] - prev[key][i]);
52                         if (sum)
53                                 printf("proto %u: %10llu pkt/s\n",
54                                        key, sum / interval);
55                         memcpy(prev[key], values, sizeof(values));
56                 }
57         }
58 }
59
60 static void usage(const char *prog)
61 {
62         fprintf(stderr,
63                 "usage: %s [OPTS] IFINDEX\n\n"
64                 "OPTS:\n"
65                 "    -S    use skb-mode\n"
66                 "    -N    enforce native mode\n",
67                 prog);
68 }
69
70 int main(int argc, char **argv)
71 {
72         const char *optstr = "SN";
73         char filename[256];
74         int opt;
75
76         while ((opt = getopt(argc, argv, optstr)) != -1) {
77                 switch (opt) {
78                 case 'S':
79                         xdp_flags |= XDP_FLAGS_SKB_MODE;
80                         break;
81                 case 'N':
82                         xdp_flags |= XDP_FLAGS_DRV_MODE;
83                         break;
84                 default:
85                         usage(basename(argv[0]));
86                         return 1;
87                 }
88         }
89
90         if (optind == argc) {
91                 usage(basename(argv[0]));
92                 return 1;
93         }
94         ifindex = strtoul(argv[optind], NULL, 0);
95
96         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
97
98         if (load_bpf_file(filename)) {
99                 printf("%s", bpf_log_buf);
100                 return 1;
101         }
102
103         if (!prog_fd[0]) {
104                 printf("load_bpf_file: %s\n", strerror(errno));
105                 return 1;
106         }
107
108         signal(SIGINT, int_exit);
109         signal(SIGTERM, int_exit);
110
111         if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
112                 printf("link set xdp fd failed\n");
113                 return 1;
114         }
115
116         poll_stats(2);
117
118         return 0;
119 }