1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
4 static const char *__doc__ =
5 "XDP redirect tool, using bpf_redirect helper\n"
6 "Usage: xdp_redirect <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.skel.h"
27 static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_CNT |
28 SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
30 DEFINE_SAMPLE_INIT(xdp_redirect);
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 {"stats", no_argument, NULL, 's' },
37 {"interval", required_argument, NULL, 'i' },
38 {"verbose", no_argument, NULL, 'v' },
42 int main(int argc, char **argv)
44 int ifindex_in, ifindex_out, opt;
45 char str[2 * IF_NAMESIZE + 1];
46 char ifname_out[IF_NAMESIZE];
47 char ifname_in[IF_NAMESIZE];
48 int ret = EXIT_FAIL_OPTION;
49 unsigned long interval = 2;
50 struct xdp_redirect *skel;
55 while ((opt = getopt_long(argc, argv, "hSFi:vs",
56 long_options, NULL)) != -1) {
60 mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
61 SAMPLE_DEVMAP_XMIT_CNT_MULTI);
67 interval = strtoul(optarg, NULL, 0);
73 mask |= SAMPLE_REDIRECT_CNT;
78 sample_usage(argv, long_options, __doc__, mask, error);
83 if (argc <= optind + 1) {
84 sample_usage(argv, long_options, __doc__, mask, true);
88 ifindex_in = if_nametoindex(argv[optind]);
90 ifindex_in = strtoul(argv[optind], NULL, 0);
92 ifindex_out = if_nametoindex(argv[optind + 1]);
94 ifindex_out = strtoul(argv[optind + 1], NULL, 0);
96 if (!ifindex_in || !ifindex_out) {
97 fprintf(stderr, "Bad interface index or name\n");
98 sample_usage(argv, long_options, __doc__, mask, true);
102 skel = xdp_redirect__open();
104 fprintf(stderr, "Failed to xdp_redirect__open: %s\n", strerror(errno));
109 ret = sample_init_pre_load(skel);
111 fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
116 skel->rodata->from_match[0] = ifindex_in;
117 skel->rodata->to_match[0] = ifindex_out;
118 skel->rodata->ifindex_out = ifindex_out;
120 ret = xdp_redirect__load(skel);
122 fprintf(stderr, "Failed to xdp_redirect__load: %s\n", strerror(errno));
127 ret = sample_init(skel, mask);
129 fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
135 if (sample_install_xdp(skel->progs.xdp_redirect_prog, ifindex_in,
139 /* Loading dummy XDP prog on out-device */
140 sample_install_xdp(skel->progs.xdp_redirect_dummy_prog, ifindex_out,
144 if (!if_indextoname(ifindex_in, ifname_in)) {
145 fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_in,
150 if (!if_indextoname(ifindex_out, ifname_out)) {
151 fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_out,
156 safe_strncpy(str, get_driver_name(ifindex_in), sizeof(str));
157 printf("Redirecting from %s (ifindex %d; driver %s) to %s (ifindex %d; driver %s)\n",
158 ifname_in, ifindex_in, str, ifname_out, ifindex_out, get_driver_name(ifindex_out));
159 snprintf(str, sizeof(str), "%s->%s", ifname_in, ifname_out);
161 ret = sample_run(interval, NULL, NULL);
163 fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
169 xdp_redirect__destroy(skel);