^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This program is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/if_link.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <net/if.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <libgen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) printf("ERROR: failed to attach program to %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Adding ifindex as a possible egress TX port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) printf("ERROR: failed using device %s as TX-port\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int do_detach(int idx, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) printf("ERROR: failed to detach program from %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* TODO: Remember to cleanup map, when adding use of shared map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * bpf_map_delete_elem((map_fd, &idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static void usage(const char *prog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) "usage: %s [OPTS] interface-list\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) "\nOPTS:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) " -d detach program\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) " -D direct table lookups (skip fib rules)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct bpf_prog_load_attr prog_load_attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .prog_type = BPF_PROG_TYPE_XDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) const char *prog_name = "xdp_fwd";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int prog_fd, map_fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) char filename[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct bpf_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int opt, i, idx, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int attach = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) attach = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case 'S':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) xdp_flags |= XDP_FLAGS_SKB_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case 'F':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case 'D':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) prog_name = "xdp_fwd_direct";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) usage(basename(argv[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) xdp_flags |= XDP_FLAGS_DRV_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (optind == argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) usage(basename(argv[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (attach) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) prog_load_attr.file = filename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (access(filename, O_RDONLY) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) printf("error accessing file %s: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) filename, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) printf("Does kernel support devmap lookup?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* If not, the error message will be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) prog = bpf_object__find_program_by_title(obj, prog_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) prog_fd = bpf_program__fd(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (prog_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) printf("program not found: %s\n", strerror(prog_fd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) "xdp_tx_ports"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (map_fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) printf("map not found: %s\n", strerror(map_fd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (i = optind; i < argc; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) idx = if_nametoindex(argv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (!idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) idx = strtoul(argv[i], NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) fprintf(stderr, "Invalid arg\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!attach) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) err = do_detach(idx, argv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) err = do_attach(idx, prog_fd, map_fd, argv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }