blob: 84f57f1209ce5ca9b8f5f2b52c11c99527bbff57 [file] [log] [blame]
David Ahernfe616052018-05-09 20:34:27 -07001// 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ørgensen7cf245a2020-01-20 14:06:49 +010027#include <bpf/libbpf.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070028#include <bpf/bpf.h>
David Ahernfe616052018-05-09 20:34:27 -070029
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010030static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
31
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +020032static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
David Ahernfe616052018-05-09 20:34:27 -070033{
34 int err;
35
Andrii Nakryikod4e34bf2022-01-19 22:14:22 -080036 err = bpf_xdp_attach(idx, prog_fd, xdp_flags, NULL);
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +020037 if (err < 0) {
David Ahernfe616052018-05-09 20:34:27 -070038 printf("ERROR: failed to attach program to %s\n", name);
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +020039 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 Ahernfe616052018-05-09 20:34:27 -070046
47 return err;
48}
49
Zhengchao Shaode5bb432022-06-06 08:54:25 +080050static int do_detach(int ifindex, const char *ifname, const char *app_name)
David Ahernfe616052018-05-09 20:34:27 -070051{
Zhengchao Shaode5bb432022-06-06 08:54:25 +080052 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 Ahernfe616052018-05-09 20:34:27 -070058
Zhengchao Shaode5bb432022-06-06 08:54:25 +080059 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 Ahernfe616052018-05-09 20:34:27 -070096 if (err < 0)
Zhengchao Shaode5bb432022-06-06 08:54:25 +080097 printf("ERROR: failed to detach program from %s (%s)\n",
98 ifname, strerror(errno));
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +020099 /* TODO: Remember to cleanup map, when adding use of shared map
100 * bpf_map_delete_elem((map_fd, &idx);
101 */
Zhengchao Shaode5bb432022-06-06 08:54:25 +0800102close_out:
103 close(prog_fd);
David Ahernfe616052018-05-09 20:34:27 -0700104 return err;
105}
106
107static 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 Haibf067f12021-06-15 21:55:54 +0800113 " -S use skb-mode\n"
114 " -F force loading prog\n"
David Ahernfe616052018-05-09 20:34:27 -0700115 " -D direct table lookups (skip fib rules)\n",
116 prog);
117}
118
119int main(int argc, char **argv)
120{
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700121 const char *prog_name = "xdp_fwd";
Kui-Feng Lee7490d592021-12-13 19:59:29 -0800122 struct bpf_program *prog = NULL;
123 struct bpf_program *pos;
124 const char *sec_name;
Andrii Nakryiko1e4edb62022-02-02 14:59:16 -0800125 int prog_fd = -1, map_fd = -1;
David Ahernfe616052018-05-09 20:34:27 -0700126 char filename[PATH_MAX];
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700127 struct bpf_object *obj;
David Ahernfe616052018-05-09 20:34:27 -0700128 int opt, i, idx, err;
David Ahernfe616052018-05-09 20:34:27 -0700129 int attach = 1;
130 int ret = 0;
131
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100132 while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
David Ahernfe616052018-05-09 20:34:27 -0700133 switch (opt) {
134 case 'd':
135 attach = 0;
136 break;
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100137 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 Ahernfe616052018-05-09 20:34:27 -0700143 case 'D':
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700144 prog_name = "xdp_fwd_direct";
David Ahernfe616052018-05-09 20:34:27 -0700145 break;
146 default:
147 usage(basename(argv[0]));
148 return 1;
149 }
150 }
151
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100152 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
153 xdp_flags |= XDP_FLAGS_DRV_MODE;
154
David Ahernfe616052018-05-09 20:34:27 -0700155 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 Nakryiko1e4edb62022-02-02 14:59:16 -0800169 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 Brouera32a32c2019-08-08 18:17:42 +0200177 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 Kicinskie1a40ef2018-07-26 14:32:20 -0700182 return 1;
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +0200183 }
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700184
Kui-Feng Lee7490d592021-12-13 19:59:29 -0800185 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 Kicinskie1a40ef2018-07-26 14:32:20 -0700192 prog_fd = bpf_program__fd(prog);
193 if (prog_fd < 0) {
194 printf("program not found: %s\n", strerror(prog_fd));
David Ahernfe616052018-05-09 20:34:27 -0700195 return 1;
196 }
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700197 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
Jesper Dangaard Brouer3783d432019-08-08 18:17:37 +0200198 "xdp_tx_ports"));
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700199 if (map_fd < 0) {
200 printf("map not found: %s\n", strerror(map_fd));
David Ahernfe616052018-05-09 20:34:27 -0700201 return 1;
202 }
203 }
David Ahernfe616052018-05-09 20:34:27 -0700204
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 Shaode5bb432022-06-06 08:54:25 +0800215 err = do_detach(idx, argv[i], prog_name);
David Ahernfe616052018-05-09 20:34:27 -0700216 if (err)
217 ret = err;
218 } else {
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +0200219 err = do_attach(idx, prog_fd, map_fd, argv[i]);
David Ahernfe616052018-05-09 20:34:27 -0700220 if (err)
221 ret = err;
222 }
223 }
224
225 return ret;
226}