blob: 74a4583d0d866435dd85979ed2893fc2576abd9e [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
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010036 err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
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
50static int do_detach(int idx, const char *name)
51{
52 int err;
53
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010054 err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
David Ahernfe616052018-05-09 20:34:27 -070055 if (err < 0)
56 printf("ERROR: failed to detach program from %s\n", name);
57
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +020058 /* TODO: Remember to cleanup map, when adding use of shared map
59 * bpf_map_delete_elem((map_fd, &idx);
60 */
David Ahernfe616052018-05-09 20:34:27 -070061 return err;
62}
63
64static void usage(const char *prog)
65{
66 fprintf(stderr,
67 "usage: %s [OPTS] interface-list\n"
68 "\nOPTS:\n"
69 " -d detach program\n"
70 " -D direct table lookups (skip fib rules)\n",
71 prog);
72}
73
74int main(int argc, char **argv)
75{
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070076 struct bpf_prog_load_attr prog_load_attr = {
77 .prog_type = BPF_PROG_TYPE_XDP,
78 };
79 const char *prog_name = "xdp_fwd";
80 struct bpf_program *prog;
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +020081 int prog_fd, map_fd = -1;
David Ahernfe616052018-05-09 20:34:27 -070082 char filename[PATH_MAX];
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -070083 struct bpf_object *obj;
David Ahernfe616052018-05-09 20:34:27 -070084 int opt, i, idx, err;
David Ahernfe616052018-05-09 20:34:27 -070085 int attach = 1;
86 int ret = 0;
87
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010088 while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
David Ahernfe616052018-05-09 20:34:27 -070089 switch (opt) {
90 case 'd':
91 attach = 0;
92 break;
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +010093 case 'S':
94 xdp_flags |= XDP_FLAGS_SKB_MODE;
95 break;
96 case 'F':
97 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
98 break;
David Ahernfe616052018-05-09 20:34:27 -070099 case 'D':
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700100 prog_name = "xdp_fwd_direct";
David Ahernfe616052018-05-09 20:34:27 -0700101 break;
102 default:
103 usage(basename(argv[0]));
104 return 1;
105 }
106 }
107
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100108 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
109 xdp_flags |= XDP_FLAGS_DRV_MODE;
110
David Ahernfe616052018-05-09 20:34:27 -0700111 if (optind == argc) {
112 usage(basename(argv[0]));
113 return 1;
114 }
115
116 if (attach) {
117 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700118 prog_load_attr.file = filename;
David Ahernfe616052018-05-09 20:34:27 -0700119
120 if (access(filename, O_RDONLY) < 0) {
121 printf("error accessing file %s: %s\n",
122 filename, strerror(errno));
123 return 1;
124 }
125
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +0200126 err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
127 if (err) {
128 printf("Does kernel support devmap lookup?\n");
129 /* If not, the error message will be:
130 * "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
131 */
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700132 return 1;
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +0200133 }
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700134
135 prog = bpf_object__find_program_by_title(obj, prog_name);
136 prog_fd = bpf_program__fd(prog);
137 if (prog_fd < 0) {
138 printf("program not found: %s\n", strerror(prog_fd));
David Ahernfe616052018-05-09 20:34:27 -0700139 return 1;
140 }
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700141 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
Jesper Dangaard Brouer3783d432019-08-08 18:17:37 +0200142 "xdp_tx_ports"));
Jakub Kicinskie1a40ef2018-07-26 14:32:20 -0700143 if (map_fd < 0) {
144 printf("map not found: %s\n", strerror(map_fd));
David Ahernfe616052018-05-09 20:34:27 -0700145 return 1;
146 }
147 }
David Ahernfe616052018-05-09 20:34:27 -0700148
149 for (i = optind; i < argc; ++i) {
150 idx = if_nametoindex(argv[i]);
151 if (!idx)
152 idx = strtoul(argv[i], NULL, 0);
153
154 if (!idx) {
155 fprintf(stderr, "Invalid arg\n");
156 return 1;
157 }
158 if (!attach) {
159 err = do_detach(idx, argv[i]);
160 if (err)
161 ret = err;
162 } else {
Jesper Dangaard Brouera32a32c2019-08-08 18:17:42 +0200163 err = do_attach(idx, prog_fd, map_fd, argv[i]);
David Ahernfe616052018-05-09 20:34:27 -0700164 if (err)
165 ret = err;
166 }
167 }
168
169 return ret;
170}