1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
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.
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.
14 #include <linux/bpf.h>
15 #include <linux/if_link.h>
16 #include <linux/limits.h>
27 #include <bpf/libbpf.h>
30 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
32 static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
36 err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
38 printf("ERROR: failed to attach program to %s\n", name);
42 /* Adding ifindex as a possible egress TX port */
43 err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
45 printf("ERROR: failed using device %s as TX-port\n", name);
50 static int do_detach(int idx, const char *name)
54 err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
56 printf("ERROR: failed to detach program from %s\n", name);
58 /* TODO: Remember to cleanup map, when adding use of shared map
59 * bpf_map_delete_elem((map_fd, &idx);
64 static void usage(const char *prog)
67 "usage: %s [OPTS] interface-list\n"
69 " -d detach program\n"
71 " -F force loading prog\n"
72 " -D direct table lookups (skip fib rules)\n",
76 int main(int argc, char **argv)
78 struct bpf_prog_load_attr prog_load_attr = {
79 .prog_type = BPF_PROG_TYPE_XDP,
81 const char *prog_name = "xdp_fwd";
82 struct bpf_program *prog = NULL;
83 struct bpf_program *pos;
85 int prog_fd, map_fd = -1;
86 char filename[PATH_MAX];
87 struct bpf_object *obj;
92 while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
98 xdp_flags |= XDP_FLAGS_SKB_MODE;
101 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
104 prog_name = "xdp_fwd_direct";
107 usage(basename(argv[0]));
112 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
113 xdp_flags |= XDP_FLAGS_DRV_MODE;
115 if (optind == argc) {
116 usage(basename(argv[0]));
121 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
122 prog_load_attr.file = filename;
124 if (access(filename, O_RDONLY) < 0) {
125 printf("error accessing file %s: %s\n",
126 filename, strerror(errno));
130 err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
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"
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)) {
146 prog_fd = bpf_program__fd(prog);
148 printf("program not found: %s\n", strerror(prog_fd));
151 map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
154 printf("map not found: %s\n", strerror(map_fd));
159 for (i = optind; i < argc; ++i) {
160 idx = if_nametoindex(argv[i]);
162 idx = strtoul(argv[i], NULL, 0);
165 fprintf(stderr, "Invalid arg\n");
169 err = do_detach(idx, argv[i]);
173 err = do_attach(idx, prog_fd, map_fd, argv[i]);