Merge tag 'char-misc-6.0-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[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 static const char *__doc__ =
5 "XDP redirect tool, using BPF_MAP_TYPE_DEVMAP\n"
6 "Usage: xdp_redirect_map <IFINDEX|IFNAME>_IN <IFINDEX|IFNAME>_OUT\n";
7
8 #include <linux/bpf.h>
9 #include <linux/if_link.h>
10 #include <assert.h>
11 #include <errno.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <net/if.h>
18 #include <unistd.h>
19 #include <libgen.h>
20 #include <getopt.h>
21 #include <bpf/bpf.h>
22 #include <bpf/libbpf.h>
23 #include "bpf_util.h"
24 #include "xdp_sample_user.h"
25 #include "xdp_redirect_map.skel.h"
26
27 static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_MAP_CNT |
28                   SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
29
30 DEFINE_SAMPLE_INIT(xdp_redirect_map);
31
32 static const struct option long_options[] = {
33         { "help", no_argument, NULL, 'h' },
34         { "skb-mode", no_argument, NULL, 'S' },
35         { "force", no_argument, NULL, 'F' },
36         { "load-egress", no_argument, NULL, 'X' },
37         { "stats", no_argument, NULL, 's' },
38         { "interval", required_argument, NULL, 'i' },
39         { "verbose", no_argument, NULL, 'v' },
40         {}
41 };
42
43 static int verbose = 0;
44
45 int main(int argc, char **argv)
46 {
47         struct bpf_devmap_val devmap_val = {};
48         bool xdp_devmap_attached = false;
49         struct xdp_redirect_map *skel;
50         char str[2 * IF_NAMESIZE + 1];
51         char ifname_out[IF_NAMESIZE];
52         struct bpf_map *tx_port_map;
53         char ifname_in[IF_NAMESIZE];
54         int ifindex_in, ifindex_out;
55         unsigned long interval = 2;
56         int ret = EXIT_FAIL_OPTION;
57         struct bpf_program *prog;
58         bool generic = false;
59         bool force = false;
60         bool tried = false;
61         bool error = true;
62         int opt, key = 0;
63
64         while ((opt = getopt_long(argc, argv, "hSFXi:vs",
65                                   long_options, NULL)) != -1) {
66                 switch (opt) {
67                 case 'S':
68                         generic = true;
69                         /* devmap_xmit tracepoint not available */
70                         mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
71                                   SAMPLE_DEVMAP_XMIT_CNT_MULTI);
72                         break;
73                 case 'F':
74                         force = true;
75                         break;
76                 case 'X':
77                         xdp_devmap_attached = true;
78                         break;
79                 case 'i':
80                         interval = strtoul(optarg, NULL, 0);
81                         break;
82                 case 'v':
83                         sample_switch_mode();
84                         verbose = 1;
85                         break;
86                 case 's':
87                         mask |= SAMPLE_REDIRECT_MAP_CNT;
88                         break;
89                 case 'h':
90                         error = false;
91                 default:
92                         sample_usage(argv, long_options, __doc__, mask, error);
93                         return ret;
94                 }
95         }
96
97         if (argc <= optind + 1) {
98                 sample_usage(argv, long_options, __doc__, mask, true);
99                 goto end;
100         }
101
102         ifindex_in = if_nametoindex(argv[optind]);
103         if (!ifindex_in)
104                 ifindex_in = strtoul(argv[optind], NULL, 0);
105
106         ifindex_out = if_nametoindex(argv[optind + 1]);
107         if (!ifindex_out)
108                 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
109
110         if (!ifindex_in || !ifindex_out) {
111                 fprintf(stderr, "Bad interface index or name\n");
112                 sample_usage(argv, long_options, __doc__, mask, true);
113                 goto end;
114         }
115
116         skel = xdp_redirect_map__open();
117         if (!skel) {
118                 fprintf(stderr, "Failed to xdp_redirect_map__open: %s\n",
119                         strerror(errno));
120                 ret = EXIT_FAIL_BPF;
121                 goto end;
122         }
123
124         ret = sample_init_pre_load(skel);
125         if (ret < 0) {
126                 fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
127                 ret = EXIT_FAIL_BPF;
128                 goto end_destroy;
129         }
130
131         /* Load 2nd xdp prog on egress. */
132         if (xdp_devmap_attached) {
133                 ret = get_mac_addr(ifindex_out, skel->rodata->tx_mac_addr);
134                 if (ret < 0) {
135                         fprintf(stderr, "Failed to get interface %d mac address: %s\n",
136                                 ifindex_out, strerror(-ret));
137                         ret = EXIT_FAIL;
138                         goto end_destroy;
139                 }
140                 if (verbose)
141                         printf("Egress ifindex:%d using src MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
142                                ifindex_out,
143                                skel->rodata->tx_mac_addr[0], skel->rodata->tx_mac_addr[1],
144                                skel->rodata->tx_mac_addr[2], skel->rodata->tx_mac_addr[3],
145                                skel->rodata->tx_mac_addr[4], skel->rodata->tx_mac_addr[5]);
146         }
147
148         skel->rodata->from_match[0] = ifindex_in;
149         skel->rodata->to_match[0] = ifindex_out;
150
151         ret = xdp_redirect_map__load(skel);
152         if (ret < 0) {
153                 fprintf(stderr, "Failed to xdp_redirect_map__load: %s\n",
154                         strerror(errno));
155                 ret = EXIT_FAIL_BPF;
156                 goto end_destroy;
157         }
158
159         ret = sample_init(skel, mask);
160         if (ret < 0) {
161                 fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
162                 ret = EXIT_FAIL;
163                 goto end_destroy;
164         }
165
166         prog = skel->progs.xdp_redirect_map_native;
167         tx_port_map = skel->maps.tx_port_native;
168 restart:
169         if (sample_install_xdp(prog, ifindex_in, generic, force) < 0) {
170                 /* First try with struct bpf_devmap_val as value for generic
171                  * mode, then fallback to sizeof(int) for older kernels.
172                  */
173                 fprintf(stderr,
174                         "Trying fallback to sizeof(int) as value_size for devmap in generic mode\n");
175                 if (generic && !tried) {
176                         prog = skel->progs.xdp_redirect_map_general;
177                         tx_port_map = skel->maps.tx_port_general;
178                         tried = true;
179                         goto restart;
180                 }
181                 ret = EXIT_FAIL_XDP;
182                 goto end_destroy;
183         }
184
185         /* Loading dummy XDP prog on out-device */
186         sample_install_xdp(skel->progs.xdp_redirect_dummy_prog, ifindex_out, generic, force);
187
188         devmap_val.ifindex = ifindex_out;
189         if (xdp_devmap_attached)
190                 devmap_val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_redirect_map_egress);
191         ret = bpf_map_update_elem(bpf_map__fd(tx_port_map), &key, &devmap_val, 0);
192         if (ret < 0) {
193                 fprintf(stderr, "Failed to update devmap value: %s\n",
194                         strerror(errno));
195                 ret = EXIT_FAIL_BPF;
196                 goto end_destroy;
197         }
198
199         ret = EXIT_FAIL;
200         if (!if_indextoname(ifindex_in, ifname_in)) {
201                 fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_in,
202                         strerror(errno));
203                 goto end_destroy;
204         }
205
206         if (!if_indextoname(ifindex_out, ifname_out)) {
207                 fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_out,
208                         strerror(errno));
209                 goto end_destroy;
210         }
211
212         safe_strncpy(str, get_driver_name(ifindex_in), sizeof(str));
213         printf("Redirecting from %s (ifindex %d; driver %s) to %s (ifindex %d; driver %s)\n",
214                ifname_in, ifindex_in, str, ifname_out, ifindex_out, get_driver_name(ifindex_out));
215         snprintf(str, sizeof(str), "%s->%s", ifname_in, ifname_out);
216
217         ret = sample_run(interval, NULL, NULL);
218         if (ret < 0) {
219                 fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
220                 ret = EXIT_FAIL;
221                 goto end_destroy;
222         }
223         ret = EXIT_OK;
224 end_destroy:
225         xdp_redirect_map__destroy(skel);
226 end:
227         sample_exit(ret);
228 }