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