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