Merge tag 'net-next-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev...
[sfrench/cifs-2.6.git] / samples / bpf / xdp_redirect_user.c
index 93854e135134cb5dd1de7e3eab22fb9cf7dc3135..7af5b07a7523044346015c8962629378b8e80a57 100644 (file)
@@ -1,6 +1,10 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
  */
+static const char *__doc__ =
+"XDP redirect tool, using bpf_redirect helper\n"
+"Usage: xdp_redirect <IFINDEX|IFNAME>_IN <IFINDEX|IFNAME>_OUT\n";
+
 #include <linux/bpf.h>
 #include <linux/if_link.h>
 #include <assert.h>
 #include <net/if.h>
 #include <unistd.h>
 #include <libgen.h>
+#include <getopt.h>
 #include <sys/resource.h>
-
-#include "bpf_util.h"
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
+#include "bpf_util.h"
+#include "xdp_sample_user.h"
+#include "xdp_redirect.skel.h"
 
-static int ifindex_in;
-static int ifindex_out;
-static bool ifindex_out_xdp_dummy_attached = true;
-static __u32 prog_id;
-static __u32 dummy_prog_id;
-
-static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
-static int rxcnt_map_fd;
-
-static void int_exit(int sig)
-{
-       __u32 curr_prog_id = 0;
-
-       if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
-               printf("bpf_get_link_xdp_id failed\n");
-               exit(1);
-       }
-       if (prog_id == curr_prog_id)
-               bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
-       else if (!curr_prog_id)
-               printf("couldn't find a prog id on iface IN\n");
-       else
-               printf("program on iface IN changed, not removing\n");
-
-       if (ifindex_out_xdp_dummy_attached) {
-               curr_prog_id = 0;
-               if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
-                                       xdp_flags)) {
-                       printf("bpf_get_link_xdp_id failed\n");
-                       exit(1);
-               }
-               if (dummy_prog_id == curr_prog_id)
-                       bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
-               else if (!curr_prog_id)
-                       printf("couldn't find a prog id on iface OUT\n");
-               else
-                       printf("program on iface OUT changed, not removing\n");
-       }
-       exit(0);
-}
-
-static void poll_stats(int interval, int ifindex)
-{
-       unsigned int nr_cpus = bpf_num_possible_cpus();
-       __u64 values[nr_cpus], prev[nr_cpus];
-
-       memset(prev, 0, sizeof(prev));
-
-       while (1) {
-               __u64 sum = 0;
-               __u32 key = 0;
-               int i;
-
-               sleep(interval);
-               assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
-               for (i = 0; i < nr_cpus; i++)
-                       sum += (values[i] - prev[i]);
-               if (sum)
-                       printf("ifindex %i: %10llu pkt/s\n",
-                              ifindex, sum / interval);
-               memcpy(prev, values, sizeof(values));
-       }
-}
+static int mask = SAMPLE_RX_CNT | SAMPLE_REDIRECT_ERR_CNT |
+                 SAMPLE_EXCEPTION_CNT | SAMPLE_DEVMAP_XMIT_CNT_MULTI;
 
-static void usage(const char *prog)
-{
-       fprintf(stderr,
-               "usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
-               "OPTS:\n"
-               "    -S    use skb-mode\n"
-               "    -N    enforce native mode\n"
-               "    -F    force loading prog\n",
-               prog);
-}
+DEFINE_SAMPLE_INIT(xdp_redirect);
 
+static const struct option long_options[] = {
+       {"help",        no_argument,            NULL, 'h' },
+       {"skb-mode",    no_argument,            NULL, 'S' },
+       {"force",       no_argument,            NULL, 'F' },
+       {"stats",       no_argument,            NULL, 's' },
+       {"interval",    required_argument,      NULL, 'i' },
+       {"verbose",     no_argument,            NULL, 'v' },
+       {}
+};
 
 int main(int argc, char **argv)
 {
-       struct bpf_prog_load_attr prog_load_attr = {
-               .prog_type      = BPF_PROG_TYPE_XDP,
-       };
-       struct bpf_program *prog, *dummy_prog;
-       int prog_fd, tx_port_map_fd, opt;
-       struct bpf_prog_info info = {};
-       __u32 info_len = sizeof(info);
-       const char *optstr = "FSN";
-       struct bpf_object *obj;
-       char filename[256];
-       int dummy_prog_fd;
-       int ret, key = 0;
-
-       while ((opt = getopt(argc, argv, optstr)) != -1) {
+       int ifindex_in, ifindex_out, opt;
+       char str[2 * IF_NAMESIZE + 1];
+       char ifname_out[IF_NAMESIZE];
+       char ifname_in[IF_NAMESIZE];
+       int ret = EXIT_FAIL_OPTION;
+       unsigned long interval = 2;
+       struct xdp_redirect *skel;
+       bool generic = false;
+       bool force = false;
+       bool error = true;
+
+       while ((opt = getopt_long(argc, argv, "hSFi:vs",
+                                 long_options, NULL)) != -1) {
                switch (opt) {
                case 'S':
-                       xdp_flags |= XDP_FLAGS_SKB_MODE;
-                       break;
-               case 'N':
-                       /* default, set below */
+                       generic = true;
+                       mask &= ~(SAMPLE_DEVMAP_XMIT_CNT |
+                                 SAMPLE_DEVMAP_XMIT_CNT_MULTI);
                        break;
                case 'F':
-                       xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
+                       force = true;
+                       break;
+               case 'i':
+                       interval = strtoul(optarg, NULL, 0);
+                       break;
+               case 'v':
+                       sample_switch_mode();
+                       break;
+               case 's':
+                       mask |= SAMPLE_REDIRECT_CNT;
                        break;
+               case 'h':
+                       error = false;
                default:
-                       usage(basename(argv[0]));
-                       return 1;
+                       sample_usage(argv, long_options, __doc__, mask, error);
+                       return ret;
                }
        }
 
-       if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
-               xdp_flags |= XDP_FLAGS_DRV_MODE;
-
-       if (optind + 2 != argc) {
-               printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
-               return 1;
+       if (argc <= optind + 1) {
+               sample_usage(argv, long_options, __doc__, mask, true);
+               return ret;
        }
 
        ifindex_in = if_nametoindex(argv[optind]);
@@ -143,75 +94,80 @@ int main(int argc, char **argv)
        if (!ifindex_out)
                ifindex_out = strtoul(argv[optind + 1], NULL, 0);
 
-       printf("input: %d output: %d\n", ifindex_in, ifindex_out);
-
-       snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
-       prog_load_attr.file = filename;
-
-       if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
-               return 1;
-
-       prog = bpf_program__next(NULL, obj);
-       dummy_prog = bpf_program__next(prog, obj);
-       if (!prog || !dummy_prog) {
-               printf("finding a prog in obj file failed\n");
-               return 1;
+       if (!ifindex_in || !ifindex_out) {
+               fprintf(stderr, "Bad interface index or name\n");
+               sample_usage(argv, long_options, __doc__, mask, true);
+               goto end;
        }
-       /* bpf_prog_load_xattr gives us the pointer to first prog's fd,
-        * so we're missing only the fd for dummy prog
-        */
-       dummy_prog_fd = bpf_program__fd(dummy_prog);
-       if (prog_fd < 0 || dummy_prog_fd < 0) {
-               printf("bpf_prog_load_xattr: %s\n", strerror(errno));
-               return 1;
+
+       skel = xdp_redirect__open();
+       if (!skel) {
+               fprintf(stderr, "Failed to xdp_redirect__open: %s\n", strerror(errno));
+               ret = EXIT_FAIL_BPF;
+               goto end;
        }
 
-       tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
-       rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
-       if (tx_port_map_fd < 0 || rxcnt_map_fd < 0) {
-               printf("bpf_object__find_map_fd_by_name failed\n");
-               return 1;
+       ret = sample_init_pre_load(skel);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to sample_init_pre_load: %s\n", strerror(-ret));
+               ret = EXIT_FAIL_BPF;
+               goto end_destroy;
        }
 
-       if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
-               printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
-               return 1;
+       skel->rodata->from_match[0] = ifindex_in;
+       skel->rodata->to_match[0] = ifindex_out;
+       skel->rodata->ifindex_out = ifindex_out;
+
+       ret = xdp_redirect__load(skel);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to xdp_redirect__load: %s\n", strerror(errno));
+               ret = EXIT_FAIL_BPF;
+               goto end_destroy;
        }
 
-       ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
-       if (ret) {
-               printf("can't get prog info - %s\n", strerror(errno));
-               return ret;
+       ret = sample_init(skel, mask);
+       if (ret < 0) {
+               fprintf(stderr, "Failed to initialize sample: %s\n", strerror(-ret));
+               ret = EXIT_FAIL;
+               goto end_destroy;
        }
-       prog_id = info.id;
+
+       ret = EXIT_FAIL_XDP;
+       if (sample_install_xdp(skel->progs.xdp_redirect_prog, ifindex_in,
+                              generic, force) < 0)
+               goto end_destroy;
 
        /* Loading dummy XDP prog on out-device */
-       if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
-                           (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
-               printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
-               ifindex_out_xdp_dummy_attached = false;
+       sample_install_xdp(skel->progs.xdp_redirect_dummy_prog, ifindex_out,
+                          generic, force);
+
+       ret = EXIT_FAIL;
+       if (!if_indextoname(ifindex_in, ifname_in)) {
+               fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_in,
+                       strerror(errno));
+               goto end_destroy;
        }
 
-       memset(&info, 0, sizeof(info));
-       ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
-       if (ret) {
-               printf("can't get prog info - %s\n", strerror(errno));
-               return ret;
+       if (!if_indextoname(ifindex_out, ifname_out)) {
+               fprintf(stderr, "Failed to if_indextoname for %d: %s\n", ifindex_out,
+                       strerror(errno));
+               goto end_destroy;
        }
-       dummy_prog_id = info.id;
 
-       signal(SIGINT, int_exit);
-       signal(SIGTERM, int_exit);
+       safe_strncpy(str, get_driver_name(ifindex_in), sizeof(str));
+       printf("Redirecting from %s (ifindex %d; driver %s) to %s (ifindex %d; driver %s)\n",
+              ifname_in, ifindex_in, str, ifname_out, ifindex_out, get_driver_name(ifindex_out));
+       snprintf(str, sizeof(str), "%s->%s", ifname_in, ifname_out);
 
-       /* bpf redirect port */
-       ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
-       if (ret) {
-               perror("bpf_update_elem");
-               goto out;
+       ret = sample_run(interval, NULL, NULL);
+       if (ret < 0) {
+               fprintf(stderr, "Failed during sample run: %s\n", strerror(-ret));
+               ret = EXIT_FAIL;
+               goto end_destroy;
        }
-
-       poll_stats(2, ifindex_out);
-
-out:
-       return ret;
+       ret = EXIT_OK;
+end_destroy:
+       xdp_redirect__destroy(skel);
+end:
+       sample_exit(ret);
 }