Merge tag 'thermal-v5.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/therma...
[linux-2.6-microblaze.git] / samples / bpf / xdp_redirect_map_user.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
3  */
4 #include <linux/bpf.h>
5 #include <linux/if_link.h>
6 #include <assert.h>
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 <net/if.h>
14 #include <unistd.h>
15 #include <libgen.h>
16 #include <sys/resource.h>
17
18 #include "bpf_util.h"
19 #include <bpf/bpf.h>
20 #include <bpf/libbpf.h>
21
22 static int ifindex_in;
23 static int ifindex_out;
24 static bool ifindex_out_xdp_dummy_attached = true;
25 static __u32 prog_id;
26 static __u32 dummy_prog_id;
27
28 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
29 static int rxcnt_map_fd;
30
31 static void int_exit(int sig)
32 {
33         __u32 curr_prog_id = 0;
34
35         if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
36                 printf("bpf_get_link_xdp_id failed\n");
37                 exit(1);
38         }
39         if (prog_id == curr_prog_id)
40                 bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
41         else if (!curr_prog_id)
42                 printf("couldn't find a prog id on iface IN\n");
43         else
44                 printf("program on iface IN changed, not removing\n");
45
46         if (ifindex_out_xdp_dummy_attached) {
47                 curr_prog_id = 0;
48                 if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
49                                         xdp_flags)) {
50                         printf("bpf_get_link_xdp_id failed\n");
51                         exit(1);
52                 }
53                 if (dummy_prog_id == curr_prog_id)
54                         bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
55                 else if (!curr_prog_id)
56                         printf("couldn't find a prog id on iface OUT\n");
57                 else
58                         printf("program on iface OUT changed, not removing\n");
59         }
60         exit(0);
61 }
62
63 static void poll_stats(int interval, int ifindex)
64 {
65         unsigned int nr_cpus = bpf_num_possible_cpus();
66         __u64 values[nr_cpus], prev[nr_cpus];
67
68         memset(prev, 0, sizeof(prev));
69
70         while (1) {
71                 __u64 sum = 0;
72                 __u32 key = 0;
73                 int i;
74
75                 sleep(interval);
76                 assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
77                 for (i = 0; i < nr_cpus; i++)
78                         sum += (values[i] - prev[i]);
79                 if (sum)
80                         printf("ifindex %i: %10llu pkt/s\n",
81                                ifindex, sum / interval);
82                 memcpy(prev, values, sizeof(values));
83         }
84 }
85
86 static void usage(const char *prog)
87 {
88         fprintf(stderr,
89                 "usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
90                 "OPTS:\n"
91                 "    -S    use skb-mode\n"
92                 "    -N    enforce native mode\n"
93                 "    -F    force loading prog\n",
94                 prog);
95 }
96
97 int main(int argc, char **argv)
98 {
99         struct bpf_prog_load_attr prog_load_attr = {
100                 .prog_type      = BPF_PROG_TYPE_XDP,
101         };
102         struct bpf_program *prog, *dummy_prog;
103         struct bpf_prog_info info = {};
104         __u32 info_len = sizeof(info);
105         int prog_fd, dummy_prog_fd;
106         const char *optstr = "FSN";
107         struct bpf_object *obj;
108         int ret, opt, key = 0;
109         char filename[256];
110         int tx_port_map_fd;
111
112         while ((opt = getopt(argc, argv, optstr)) != -1) {
113                 switch (opt) {
114                 case 'S':
115                         xdp_flags |= XDP_FLAGS_SKB_MODE;
116                         break;
117                 case 'N':
118                         /* default, set below */
119                         break;
120                 case 'F':
121                         xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
122                         break;
123                 default:
124                         usage(basename(argv[0]));
125                         return 1;
126                 }
127         }
128
129         if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
130                 xdp_flags |= XDP_FLAGS_DRV_MODE;
131
132         if (optind == argc) {
133                 printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
134                 return 1;
135         }
136
137         ifindex_in = if_nametoindex(argv[optind]);
138         if (!ifindex_in)
139                 ifindex_in = strtoul(argv[optind], NULL, 0);
140
141         ifindex_out = if_nametoindex(argv[optind + 1]);
142         if (!ifindex_out)
143                 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
144
145         printf("input: %d output: %d\n", ifindex_in, ifindex_out);
146
147         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
148         prog_load_attr.file = filename;
149
150         if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
151                 return 1;
152
153         prog = bpf_program__next(NULL, obj);
154         dummy_prog = bpf_program__next(prog, obj);
155         if (!prog || !dummy_prog) {
156                 printf("finding a prog in obj file failed\n");
157                 return 1;
158         }
159         /* bpf_prog_load_xattr gives us the pointer to first prog's fd,
160          * so we're missing only the fd for dummy prog
161          */
162         dummy_prog_fd = bpf_program__fd(dummy_prog);
163         if (prog_fd < 0 || dummy_prog_fd < 0) {
164                 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
165                 return 1;
166         }
167
168         tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
169         rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
170         if (tx_port_map_fd < 0 || rxcnt_map_fd < 0) {
171                 printf("bpf_object__find_map_fd_by_name failed\n");
172                 return 1;
173         }
174
175         if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
176                 printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
177                 return 1;
178         }
179
180         ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
181         if (ret) {
182                 printf("can't get prog info - %s\n", strerror(errno));
183                 return ret;
184         }
185         prog_id = info.id;
186
187         /* Loading dummy XDP prog on out-device */
188         if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
189                             (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
190                 printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
191                 ifindex_out_xdp_dummy_attached = false;
192         }
193
194         memset(&info, 0, sizeof(info));
195         ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
196         if (ret) {
197                 printf("can't get prog info - %s\n", strerror(errno));
198                 return ret;
199         }
200         dummy_prog_id = info.id;
201
202         signal(SIGINT, int_exit);
203         signal(SIGTERM, int_exit);
204
205         /* populate virtual to physical port map */
206         ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
207         if (ret) {
208                 perror("bpf_update_elem");
209                 goto out;
210         }
211
212         poll_stats(2, ifindex_out);
213
214 out:
215         return 0;
216 }