1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
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";
9 #include <linux/if_link.h>
22 #include <bpf/libbpf.h>
24 #include "xdp_sample_user.h"
25 #include "xdp_redirect_map.skel.h"
27 static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_MAP_CNT |
28 SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
30 DEFINE_SAMPLE_INIT(xdp_redirect_map);
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' },
43 static int verbose = 0;
45 int main(int argc, char **argv)
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;
64 while ((opt = getopt_long(argc, argv, "hSFXi:vs",
65 long_options, NULL)) != -1) {
69 /* devmap_xmit tracepoint not available */
70 mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
71 SAMPLE_DEVMAP_XMIT_CNT_MULTI);
77 xdp_devmap_attached = true;
80 interval = strtoul(optarg, NULL, 0);
87 mask |= SAMPLE_REDIRECT_MAP_CNT;
92 sample_usage(argv, long_options, __doc__, mask, error);
97 if (argc <= optind + 1) {
98 sample_usage(argv, long_options, __doc__, mask, true);
102 ifindex_in = if_nametoindex(argv[optind]);
104 ifindex_in = strtoul(argv[optind], NULL, 0);
106 ifindex_out = if_nametoindex(argv[optind + 1]);
108 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
110 if (!ifindex_in || !ifindex_out) {
111 fprintf(stderr, "Bad interface index or name\n");
112 sample_usage(argv, long_options, __doc__, mask, true);
116 skel = xdp_redirect_map__open();
118 fprintf(stderr, "Failed to xdp_redirect_map__open: %s\n",
124 ret = sample_init_pre_load(skel);
126 fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
131 /* Load 2nd xdp prog on egress. */
132 if (xdp_devmap_attached) {
133 ret = get_mac_addr(ifindex_out, skel->rodata->tx_mac_addr);
135 fprintf(stderr, "Failed to get interface %d mac address: %s\n",
136 ifindex_out, strerror(-ret));
141 printf("Egress ifindex:%d using src MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
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]);
148 skel->rodata->from_match[0] = ifindex_in;
149 skel->rodata->to_match[0] = ifindex_out;
151 ret = xdp_redirect_map__load(skel);
153 fprintf(stderr, "Failed to xdp_redirect_map__load: %s\n",
159 ret = sample_init(skel, mask);
161 fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
166 prog = skel->progs.xdp_redirect_map_native;
167 tx_port_map = skel->maps.tx_port_native;
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.
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;
185 /* Loading dummy XDP prog on out-device */
186 sample_install_xdp(skel->progs.xdp_redirect_dummy_prog, ifindex_out, generic, force);
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);
193 fprintf(stderr, "Failed to update devmap value: %s\n",
200 if (!if_indextoname(ifindex_in, ifname_in)) {
201 fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_in,
206 if (!if_indextoname(ifindex_out, ifname_out)) {
207 fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_out,
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);
217 ret = sample_run(interval, NULL, NULL);
219 fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
225 xdp_redirect_map__destroy(skel);