Merge tag 'drm-misc-next-fixes-2019-03-06' of git://anongit.freedesktop.org/drm/drm...
[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 #include <sys/resource.h>
18 #include <net/if.h>
19
20 #include "bpf_util.h"
21 #include "bpf/bpf.h"
22 #include "bpf/libbpf.h"
23
24 static int ifindex;
25 static __u32 xdp_flags;
26
27 static void int_exit(int sig)
28 {
29         bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
30         exit(0);
31 }
32
33 /* simple per-protocol drop counter
34  */
35 static void poll_stats(int map_fd, int interval)
36 {
37         unsigned int nr_cpus = bpf_num_possible_cpus();
38         __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
39         int i;
40
41         while (1) {
42                 __u32 key = UINT32_MAX;
43
44                 sleep(interval);
45
46                 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
47                         __u64 sum = 0;
48
49                         assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
50                         for (i = 0; i < nr_cpus; i++)
51                                 sum += values[i];
52                         if (sum > prev[key])
53                                 printf("proto %u: %10llu pkt/s\n",
54                                        key, (sum - prev[key]) / interval);
55                         prev[key] = sum;
56                 }
57         }
58 }
59
60 static void usage(const char *prog)
61 {
62         fprintf(stderr,
63                 "usage: %s [OPTS] IFACE\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         struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
73         struct bpf_prog_load_attr prog_load_attr = {
74                 .prog_type      = BPF_PROG_TYPE_XDP,
75         };
76         const char *optstr = "SN";
77         int prog_fd, map_fd, opt;
78         struct bpf_object *obj;
79         struct bpf_map *map;
80         char filename[256];
81
82         while ((opt = getopt(argc, argv, optstr)) != -1) {
83                 switch (opt) {
84                 case 'S':
85                         xdp_flags |= XDP_FLAGS_SKB_MODE;
86                         break;
87                 case 'N':
88                         xdp_flags |= XDP_FLAGS_DRV_MODE;
89                         break;
90                 default:
91                         usage(basename(argv[0]));
92                         return 1;
93                 }
94         }
95
96         if (optind == argc) {
97                 usage(basename(argv[0]));
98                 return 1;
99         }
100
101         if (setrlimit(RLIMIT_MEMLOCK, &r)) {
102                 perror("setrlimit(RLIMIT_MEMLOCK)");
103                 return 1;
104         }
105
106         ifindex = if_nametoindex(argv[optind]);
107         if (!ifindex) {
108                 perror("if_nametoindex");
109                 return 1;
110         }
111
112         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
113         prog_load_attr.file = filename;
114
115         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
116                 return 1;
117
118         map = bpf_map__next(NULL, obj);
119         if (!map) {
120                 printf("finding a map in obj file failed\n");
121                 return 1;
122         }
123         map_fd = bpf_map__fd(map);
124
125         if (!prog_fd) {
126                 printf("load_bpf_file: %s\n", strerror(errno));
127                 return 1;
128         }
129
130         signal(SIGINT, int_exit);
131         signal(SIGTERM, int_exit);
132
133         if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
134                 printf("link set xdp fd failed\n");
135                 return 1;
136         }
137
138         poll_stats(map_fd, 2);
139
140         return 0;
141 }