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_xdp_attach(idx
, prog_fd
, xdp_flags
, NULL
);
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 ifindex
, const char *ifname
, const char *app_name
)
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
;
59 if (bpf_xdp_query_id(ifindex
, xdp_flags
, &curr_prog_id
)) {
60 printf("ERROR: bpf_xdp_query_id failed (%s)\n",
66 printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
71 info_len
= sizeof(prog_info
);
72 prog_fd
= bpf_prog_get_fd_by_id(curr_prog_id
);
74 printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
79 err
= bpf_prog_get_info_by_fd(prog_fd
, &prog_info
, &info_len
);
81 printf("ERROR: bpf_prog_get_info_by_fd failed (%s)\n",
85 snprintf(prog_name
, sizeof(prog_name
), "%s_prog", app_name
);
86 prog_name
[BPF_OBJ_NAME_LEN
- 1] = '\0';
88 if (strcmp(prog_info
.name
, prog_name
)) {
89 printf("ERROR: %s isn't attached to %s\n", app_name
, ifname
);
94 opts
.old_prog_fd
= prog_fd
;
95 err
= bpf_xdp_detach(ifindex
, xdp_flags
, &opts
);
97 printf("ERROR: failed to detach program from %s (%s)\n",
98 ifname
, strerror(errno
));
99 /* TODO: Remember to cleanup map, when adding use of shared map
100 * bpf_map_delete_elem((map_fd, &idx);
107 static void usage(const char *prog
)
110 "usage: %s [OPTS] interface-list\n"
112 " -d detach program\n"
114 " -F force loading prog\n"
115 " -D direct table lookups (skip fib rules)\n",
119 int main(int argc
, char **argv
)
121 const char *prog_name
= "xdp_fwd";
122 struct bpf_program
*prog
= NULL
;
123 struct bpf_program
*pos
;
124 const char *sec_name
;
125 int prog_fd
= -1, map_fd
= -1;
126 char filename
[PATH_MAX
];
127 struct bpf_object
*obj
;
128 int opt
, i
, idx
, err
;
132 while ((opt
= getopt(argc
, argv
, ":dDSF")) != -1) {
138 xdp_flags
|= XDP_FLAGS_SKB_MODE
;
141 xdp_flags
&= ~XDP_FLAGS_UPDATE_IF_NOEXIST
;
144 prog_name
= "xdp_fwd_direct";
147 usage(basename(argv
[0]));
152 if (!(xdp_flags
& XDP_FLAGS_SKB_MODE
))
153 xdp_flags
|= XDP_FLAGS_DRV_MODE
;
155 if (optind
== argc
) {
156 usage(basename(argv
[0]));
161 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
163 if (access(filename
, O_RDONLY
) < 0) {
164 printf("error accessing file %s: %s\n",
165 filename
, strerror(errno
));
169 obj
= bpf_object__open_file(filename
, NULL
);
170 if (libbpf_get_error(obj
))
173 prog
= bpf_object__next_program(obj
, NULL
);
174 bpf_program__set_type(prog
, BPF_PROG_TYPE_XDP
);
176 err
= bpf_object__load(obj
);
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"
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
)) {
192 prog_fd
= bpf_program__fd(prog
);
194 printf("program not found: %s\n", strerror(prog_fd
));
197 map_fd
= bpf_map__fd(bpf_object__find_map_by_name(obj
,
200 printf("map not found: %s\n", strerror(map_fd
));
205 for (i
= optind
; i
< argc
; ++i
) {
206 idx
= if_nametoindex(argv
[i
]);
208 idx
= strtoul(argv
[i
], NULL
, 0);
211 fprintf(stderr
, "Invalid arg\n");
215 err
= do_detach(idx
, argv
[i
], prog_name
);
219 err
= do_attach(idx
, prog_fd
, map_fd
, argv
[i
]);