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 | |
Andrii Nakryiko | d4e34bf | 2022-01-19 22:14:22 -0800 | [diff] [blame] | 36 | err = bpf_xdp_attach(idx, prog_fd, xdp_flags, NULL); |
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 | |
Zhengchao Shao | de5bb43 | 2022-06-06 08:54:25 +0800 | [diff] [blame] | 50 | static int do_detach(int ifindex, const char *ifname, const char *app_name) |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 51 | { |
Zhengchao Shao | de5bb43 | 2022-06-06 08:54:25 +0800 | [diff] [blame] | 52 | LIBBPF_OPTS(bpf_xdp_attach_opts, opts); |
| 53 | struct bpf_prog_info prog_info = {}; |
| 54 | char prog_name[BPF_OBJ_NAME_LEN]; |
| 55 | __u32 info_len, curr_prog_id; |
| 56 | int prog_fd; |
| 57 | int err = 1; |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 58 | |
Zhengchao Shao | de5bb43 | 2022-06-06 08:54:25 +0800 | [diff] [blame] | 59 | if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) { |
| 60 | printf("ERROR: bpf_xdp_query_id failed (%s)\n", |
| 61 | strerror(errno)); |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | if (!curr_prog_id) { |
| 66 | printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n", |
| 67 | xdp_flags, ifname); |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | info_len = sizeof(prog_info); |
| 72 | prog_fd = bpf_prog_get_fd_by_id(curr_prog_id); |
| 73 | if (prog_fd < 0) { |
| 74 | printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n", |
| 75 | strerror(errno)); |
| 76 | return prog_fd; |
| 77 | } |
| 78 | |
| 79 | err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len); |
| 80 | if (err) { |
| 81 | printf("ERROR: bpf_obj_get_info_by_fd failed (%s)\n", |
| 82 | strerror(errno)); |
| 83 | goto close_out; |
| 84 | } |
| 85 | snprintf(prog_name, sizeof(prog_name), "%s_prog", app_name); |
| 86 | prog_name[BPF_OBJ_NAME_LEN - 1] = '\0'; |
| 87 | |
| 88 | if (strcmp(prog_info.name, prog_name)) { |
| 89 | printf("ERROR: %s isn't attached to %s\n", app_name, ifname); |
| 90 | err = 1; |
| 91 | goto close_out; |
| 92 | } |
| 93 | |
| 94 | opts.old_prog_fd = prog_fd; |
| 95 | err = bpf_xdp_detach(ifindex, xdp_flags, &opts); |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 96 | if (err < 0) |
Zhengchao Shao | de5bb43 | 2022-06-06 08:54:25 +0800 | [diff] [blame] | 97 | printf("ERROR: failed to detach program from %s (%s)\n", |
| 98 | ifname, strerror(errno)); |
Jesper Dangaard Brouer | a32a32c | 2019-08-08 18:17:42 +0200 | [diff] [blame] | 99 | /* TODO: Remember to cleanup map, when adding use of shared map |
| 100 | * bpf_map_delete_elem((map_fd, &idx); |
| 101 | */ |
Zhengchao Shao | de5bb43 | 2022-06-06 08:54:25 +0800 | [diff] [blame] | 102 | close_out: |
| 103 | close(prog_fd); |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 104 | return err; |
| 105 | } |
| 106 | |
| 107 | static void usage(const char *prog) |
| 108 | { |
| 109 | fprintf(stderr, |
| 110 | "usage: %s [OPTS] interface-list\n" |
| 111 | "\nOPTS:\n" |
| 112 | " -d detach program\n" |
Wang Hai | bf067f1 | 2021-06-15 21:55:54 +0800 | [diff] [blame] | 113 | " -S use skb-mode\n" |
| 114 | " -F force loading prog\n" |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 115 | " -D direct table lookups (skip fib rules)\n", |
| 116 | prog); |
| 117 | } |
| 118 | |
| 119 | int main(int argc, char **argv) |
| 120 | { |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 121 | const char *prog_name = "xdp_fwd"; |
Kui-Feng Lee | 7490d59 | 2021-12-13 19:59:29 -0800 | [diff] [blame] | 122 | struct bpf_program *prog = NULL; |
| 123 | struct bpf_program *pos; |
| 124 | const char *sec_name; |
Andrii Nakryiko | 1e4edb6 | 2022-02-02 14:59:16 -0800 | [diff] [blame] | 125 | int prog_fd = -1, map_fd = -1; |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 126 | char filename[PATH_MAX]; |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 127 | struct bpf_object *obj; |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 128 | int opt, i, idx, err; |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 129 | int attach = 1; |
| 130 | int ret = 0; |
| 131 | |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 132 | while ((opt = getopt(argc, argv, ":dDSF")) != -1) { |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 133 | switch (opt) { |
| 134 | case 'd': |
| 135 | attach = 0; |
| 136 | break; |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 137 | case 'S': |
| 138 | xdp_flags |= XDP_FLAGS_SKB_MODE; |
| 139 | break; |
| 140 | case 'F': |
| 141 | xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 142 | break; |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 143 | case 'D': |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 144 | prog_name = "xdp_fwd_direct"; |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 145 | break; |
| 146 | default: |
| 147 | usage(basename(argv[0])); |
| 148 | return 1; |
| 149 | } |
| 150 | } |
| 151 | |
Toke Høiland-Jørgensen | d50ecc4 | 2019-12-16 12:07:42 +0100 | [diff] [blame] | 152 | if (!(xdp_flags & XDP_FLAGS_SKB_MODE)) |
| 153 | xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 154 | |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 155 | if (optind == argc) { |
| 156 | usage(basename(argv[0])); |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | if (attach) { |
| 161 | snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); |
| 162 | |
| 163 | if (access(filename, O_RDONLY) < 0) { |
| 164 | printf("error accessing file %s: %s\n", |
| 165 | filename, strerror(errno)); |
| 166 | return 1; |
| 167 | } |
| 168 | |
Andrii Nakryiko | 1e4edb6 | 2022-02-02 14:59:16 -0800 | [diff] [blame] | 169 | obj = bpf_object__open_file(filename, NULL); |
| 170 | if (libbpf_get_error(obj)) |
| 171 | return 1; |
| 172 | |
| 173 | prog = bpf_object__next_program(obj, NULL); |
| 174 | bpf_program__set_type(prog, BPF_PROG_TYPE_XDP); |
| 175 | |
| 176 | err = bpf_object__load(obj); |
Jesper Dangaard Brouer | a32a32c | 2019-08-08 18:17:42 +0200 | [diff] [blame] | 177 | if (err) { |
| 178 | printf("Does kernel support devmap lookup?\n"); |
| 179 | /* If not, the error message will be: |
| 180 | * "cannot pass map_type 14 into func bpf_map_lookup_elem#1" |
| 181 | */ |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 182 | return 1; |
Jesper Dangaard Brouer | a32a32c | 2019-08-08 18:17:42 +0200 | [diff] [blame] | 183 | } |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 184 | |
Kui-Feng Lee | 7490d59 | 2021-12-13 19:59:29 -0800 | [diff] [blame] | 185 | bpf_object__for_each_program(pos, obj) { |
| 186 | sec_name = bpf_program__section_name(pos); |
| 187 | if (sec_name && !strcmp(sec_name, prog_name)) { |
| 188 | prog = pos; |
| 189 | break; |
| 190 | } |
| 191 | } |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 192 | prog_fd = bpf_program__fd(prog); |
| 193 | if (prog_fd < 0) { |
| 194 | printf("program not found: %s\n", strerror(prog_fd)); |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 195 | return 1; |
| 196 | } |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 197 | 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] | 198 | "xdp_tx_ports")); |
Jakub Kicinski | e1a40ef | 2018-07-26 14:32:20 -0700 | [diff] [blame] | 199 | if (map_fd < 0) { |
| 200 | printf("map not found: %s\n", strerror(map_fd)); |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 201 | return 1; |
| 202 | } |
| 203 | } |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 204 | |
| 205 | for (i = optind; i < argc; ++i) { |
| 206 | idx = if_nametoindex(argv[i]); |
| 207 | if (!idx) |
| 208 | idx = strtoul(argv[i], NULL, 0); |
| 209 | |
| 210 | if (!idx) { |
| 211 | fprintf(stderr, "Invalid arg\n"); |
| 212 | return 1; |
| 213 | } |
| 214 | if (!attach) { |
Zhengchao Shao | de5bb43 | 2022-06-06 08:54:25 +0800 | [diff] [blame] | 215 | err = do_detach(idx, argv[i], prog_name); |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 216 | if (err) |
| 217 | ret = err; |
| 218 | } else { |
Jesper Dangaard Brouer | a32a32c | 2019-08-08 18:17:42 +0200 | [diff] [blame] | 219 | err = do_attach(idx, prog_fd, map_fd, argv[i]); |
David Ahern | fe61605 | 2018-05-09 20:34:27 -0700 | [diff] [blame] | 220 | if (err) |
| 221 | ret = err; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return ret; |
| 226 | } |