Merge tag 'for-linus' of git://git.armlinux.org.uk/~rmk/linux-arm
[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 = XDP_FLAGS_UPDATE_IF_NOEXIST;
26 static __u32 prog_id;
27
28 static void int_exit(int sig)
29 {
30         __u32 curr_prog_id = 0;
31
32         if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
33                 printf("bpf_get_link_xdp_id failed\n");
34                 exit(1);
35         }
36         if (prog_id == curr_prog_id)
37                 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
38         else if (!curr_prog_id)
39                 printf("couldn't find a prog id on a given interface\n");
40         else
41                 printf("program on interface changed, not removing\n");
42         exit(0);
43 }
44
45 /* simple per-protocol drop counter
46  */
47 static void poll_stats(int map_fd, int interval)
48 {
49         unsigned int nr_cpus = bpf_num_possible_cpus();
50         __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
51         int i;
52
53         while (1) {
54                 __u32 key = UINT32_MAX;
55
56                 sleep(interval);
57
58                 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
59                         __u64 sum = 0;
60
61                         assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
62                         for (i = 0; i < nr_cpus; i++)
63                                 sum += values[i];
64                         if (sum > prev[key])
65                                 printf("proto %u: %10llu pkt/s\n",
66                                        key, (sum - prev[key]) / interval);
67                         prev[key] = sum;
68                 }
69         }
70 }
71
72 static void usage(const char *prog)
73 {
74         fprintf(stderr,
75                 "usage: %s [OPTS] IFACE\n\n"
76                 "OPTS:\n"
77                 "    -S    use skb-mode\n"
78                 "    -N    enforce native mode\n"
79                 "    -F    force loading prog\n",
80                 prog);
81 }
82
83 int main(int argc, char **argv)
84 {
85         struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
86         struct bpf_prog_load_attr prog_load_attr = {
87                 .prog_type      = BPF_PROG_TYPE_XDP,
88         };
89         struct bpf_prog_info info = {};
90         __u32 info_len = sizeof(info);
91         const char *optstr = "FSN";
92         int prog_fd, map_fd, opt;
93         struct bpf_object *obj;
94         struct bpf_map *map;
95         char filename[256];
96         int err;
97
98         while ((opt = getopt(argc, argv, optstr)) != -1) {
99                 switch (opt) {
100                 case 'S':
101                         xdp_flags |= XDP_FLAGS_SKB_MODE;
102                         break;
103                 case 'N':
104                         xdp_flags |= XDP_FLAGS_DRV_MODE;
105                         break;
106                 case 'F':
107                         xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
108                         break;
109                 default:
110                         usage(basename(argv[0]));
111                         return 1;
112                 }
113         }
114
115         if (optind == argc) {
116                 usage(basename(argv[0]));
117                 return 1;
118         }
119
120         if (setrlimit(RLIMIT_MEMLOCK, &r)) {
121                 perror("setrlimit(RLIMIT_MEMLOCK)");
122                 return 1;
123         }
124
125         ifindex = if_nametoindex(argv[optind]);
126         if (!ifindex) {
127                 perror("if_nametoindex");
128                 return 1;
129         }
130
131         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
132         prog_load_attr.file = filename;
133
134         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
135                 return 1;
136
137         map = bpf_map__next(NULL, obj);
138         if (!map) {
139                 printf("finding a map in obj file failed\n");
140                 return 1;
141         }
142         map_fd = bpf_map__fd(map);
143
144         if (!prog_fd) {
145                 printf("load_bpf_file: %s\n", strerror(errno));
146                 return 1;
147         }
148
149         signal(SIGINT, int_exit);
150         signal(SIGTERM, int_exit);
151
152         if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
153                 printf("link set xdp fd failed\n");
154                 return 1;
155         }
156
157         err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
158         if (err) {
159                 printf("can't get prog info - %s\n", strerror(errno));
160                 return err;
161         }
162         prog_id = info.id;
163
164         poll_stats(map_fd, 2);
165
166         return 0;
167 }