2018-05-10 11:34:27 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/* Copyright (c) 2017-18 David Ahern <dsahern@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of version 2 of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <linux/if_link.h>
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
|
2020-01-20 21:06:49 +08:00
|
|
|
#include <bpf/libbpf.h>
|
2018-05-15 13:35:02 +08:00
|
|
|
#include <bpf/bpf.h>
|
2018-05-10 11:34:27 +08:00
|
|
|
|
samples/bpf: Attach XDP programs in driver mode by default
When attaching XDP programs, userspace can set flags to request the attach
mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
are requested, the kernel will attempt to attach in driver mode, and then
silently fall back to SKB mode if this fails.
The silent fallback is a major source of user confusion, as users will try
to load a program on a device without XDP support, and instead of an error
they will get the silent fallback behaviour, not notice, and then wonder
why performance is not what they were expecting.
In an attempt to combat this, let's switch all the samples to default to
explicitly requesting driver-mode attach. As part of this, ensure that all
the userspace utilities have a switch to enable SKB mode. For those that
have a switch to request driver mode, keep it but turn it into a no-op.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: David Ahern <dsahern@gmail.com>
Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
2019-12-16 19:07:42 +08:00
|
|
|
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
|
2019-08-09 00:17:42 +08:00
|
|
|
static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
|
2018-05-10 11:34:27 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-01-20 14:14:22 +08:00
|
|
|
err = bpf_xdp_attach(idx, prog_fd, xdp_flags, NULL);
|
2019-08-09 00:17:42 +08:00
|
|
|
if (err < 0) {
|
2018-05-10 11:34:27 +08:00
|
|
|
printf("ERROR: failed to attach program to %s\n", name);
|
2019-08-09 00:17:42 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adding ifindex as a possible egress TX port */
|
|
|
|
err = bpf_map_update_elem(map_fd, &idx, &idx, 0);
|
|
|
|
if (err)
|
|
|
|
printf("ERROR: failed using device %s as TX-port\n", name);
|
2018-05-10 11:34:27 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-06-06 08:54:25 +08:00
|
|
|
static int do_detach(int ifindex, const char *ifname, const char *app_name)
|
2018-05-10 11:34:27 +08:00
|
|
|
{
|
2022-06-06 08:54:25 +08:00
|
|
|
LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
|
|
|
|
struct bpf_prog_info prog_info = {};
|
|
|
|
char prog_name[BPF_OBJ_NAME_LEN];
|
|
|
|
__u32 info_len, curr_prog_id;
|
|
|
|
int prog_fd;
|
|
|
|
int err = 1;
|
|
|
|
|
|
|
|
if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
|
|
|
|
printf("ERROR: bpf_xdp_query_id failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
return err;
|
|
|
|
}
|
2018-05-10 11:34:27 +08:00
|
|
|
|
2022-06-06 08:54:25 +08:00
|
|
|
if (!curr_prog_id) {
|
|
|
|
printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
|
|
|
|
xdp_flags, ifname);
|
|
|
|
return err;
|
|
|
|
}
|
2018-05-10 11:34:27 +08:00
|
|
|
|
2022-06-06 08:54:25 +08:00
|
|
|
info_len = sizeof(prog_info);
|
|
|
|
prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
|
|
|
|
if (prog_fd < 0) {
|
|
|
|
printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
return prog_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
|
|
|
|
if (err) {
|
|
|
|
printf("ERROR: bpf_obj_get_info_by_fd failed (%s)\n",
|
|
|
|
strerror(errno));
|
|
|
|
goto close_out;
|
|
|
|
}
|
|
|
|
snprintf(prog_name, sizeof(prog_name), "%s_prog", app_name);
|
|
|
|
prog_name[BPF_OBJ_NAME_LEN - 1] = '\0';
|
|
|
|
|
|
|
|
if (strcmp(prog_info.name, prog_name)) {
|
|
|
|
printf("ERROR: %s isn't attached to %s\n", app_name, ifname);
|
|
|
|
err = 1;
|
|
|
|
goto close_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.old_prog_fd = prog_fd;
|
|
|
|
err = bpf_xdp_detach(ifindex, xdp_flags, &opts);
|
|
|
|
if (err < 0)
|
|
|
|
printf("ERROR: failed to detach program from %s (%s)\n",
|
|
|
|
ifname, strerror(errno));
|
2019-08-09 00:17:42 +08:00
|
|
|
/* TODO: Remember to cleanup map, when adding use of shared map
|
|
|
|
* bpf_map_delete_elem((map_fd, &idx);
|
|
|
|
*/
|
2022-06-06 08:54:25 +08:00
|
|
|
close_out:
|
|
|
|
close(prog_fd);
|
2018-05-10 11:34:27 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char *prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [OPTS] interface-list\n"
|
|
|
|
"\nOPTS:\n"
|
|
|
|
" -d detach program\n"
|
2021-06-15 21:55:54 +08:00
|
|
|
" -S use skb-mode\n"
|
|
|
|
" -F force loading prog\n"
|
2018-05-10 11:34:27 +08:00
|
|
|
" -D direct table lookups (skip fib rules)\n",
|
|
|
|
prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-07-27 05:32:20 +08:00
|
|
|
const char *prog_name = "xdp_fwd";
|
2021-12-14 11:59:29 +08:00
|
|
|
struct bpf_program *prog = NULL;
|
|
|
|
struct bpf_program *pos;
|
|
|
|
const char *sec_name;
|
2022-02-03 06:59:16 +08:00
|
|
|
int prog_fd = -1, map_fd = -1;
|
2018-05-10 11:34:27 +08:00
|
|
|
char filename[PATH_MAX];
|
2018-07-27 05:32:20 +08:00
|
|
|
struct bpf_object *obj;
|
2018-05-10 11:34:27 +08:00
|
|
|
int opt, i, idx, err;
|
|
|
|
int attach = 1;
|
|
|
|
int ret = 0;
|
|
|
|
|
samples/bpf: Attach XDP programs in driver mode by default
When attaching XDP programs, userspace can set flags to request the attach
mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
are requested, the kernel will attempt to attach in driver mode, and then
silently fall back to SKB mode if this fails.
The silent fallback is a major source of user confusion, as users will try
to load a program on a device without XDP support, and instead of an error
they will get the silent fallback behaviour, not notice, and then wonder
why performance is not what they were expecting.
In an attempt to combat this, let's switch all the samples to default to
explicitly requesting driver-mode attach. As part of this, ensure that all
the userspace utilities have a switch to enable SKB mode. For those that
have a switch to request driver mode, keep it but turn it into a no-op.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: David Ahern <dsahern@gmail.com>
Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
2019-12-16 19:07:42 +08:00
|
|
|
while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
|
2018-05-10 11:34:27 +08:00
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
|
|
|
attach = 0;
|
|
|
|
break;
|
samples/bpf: Attach XDP programs in driver mode by default
When attaching XDP programs, userspace can set flags to request the attach
mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
are requested, the kernel will attempt to attach in driver mode, and then
silently fall back to SKB mode if this fails.
The silent fallback is a major source of user confusion, as users will try
to load a program on a device without XDP support, and instead of an error
they will get the silent fallback behaviour, not notice, and then wonder
why performance is not what they were expecting.
In an attempt to combat this, let's switch all the samples to default to
explicitly requesting driver-mode attach. As part of this, ensure that all
the userspace utilities have a switch to enable SKB mode. For those that
have a switch to request driver mode, keep it but turn it into a no-op.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: David Ahern <dsahern@gmail.com>
Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
2019-12-16 19:07:42 +08:00
|
|
|
case 'S':
|
|
|
|
xdp_flags |= XDP_FLAGS_SKB_MODE;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
break;
|
2018-05-10 11:34:27 +08:00
|
|
|
case 'D':
|
2018-07-27 05:32:20 +08:00
|
|
|
prog_name = "xdp_fwd_direct";
|
2018-05-10 11:34:27 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
samples/bpf: Attach XDP programs in driver mode by default
When attaching XDP programs, userspace can set flags to request the attach
mode (generic/SKB mode, driver mode or hw offloaded mode). If no such flags
are requested, the kernel will attempt to attach in driver mode, and then
silently fall back to SKB mode if this fails.
The silent fallback is a major source of user confusion, as users will try
to load a program on a device without XDP support, and instead of an error
they will get the silent fallback behaviour, not notice, and then wonder
why performance is not what they were expecting.
In an attempt to combat this, let's switch all the samples to default to
explicitly requesting driver-mode attach. As part of this, ensure that all
the userspace utilities have a switch to enable SKB mode. For those that
have a switch to request driver mode, keep it but turn it into a no-op.
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Jesper Dangaard Brouer <brouer@redhat.com>
Acked-by: David Ahern <dsahern@gmail.com>
Link: https://lore.kernel.org/bpf/20191216110742.364456-1-toke@redhat.com
2019-12-16 19:07:42 +08:00
|
|
|
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
|
|
|
|
xdp_flags |= XDP_FLAGS_DRV_MODE;
|
|
|
|
|
2018-05-10 11:34:27 +08:00
|
|
|
if (optind == argc) {
|
|
|
|
usage(basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attach) {
|
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
|
|
|
|
|
|
|
if (access(filename, O_RDONLY) < 0) {
|
|
|
|
printf("error accessing file %s: %s\n",
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:59:16 +08:00
|
|
|
obj = bpf_object__open_file(filename, NULL);
|
|
|
|
if (libbpf_get_error(obj))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
prog = bpf_object__next_program(obj, NULL);
|
|
|
|
bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
|
|
|
|
|
|
|
|
err = bpf_object__load(obj);
|
2019-08-09 00:17:42 +08:00
|
|
|
if (err) {
|
|
|
|
printf("Does kernel support devmap lookup?\n");
|
|
|
|
/* If not, the error message will be:
|
|
|
|
* "cannot pass map_type 14 into func bpf_map_lookup_elem#1"
|
|
|
|
*/
|
2018-05-10 11:34:27 +08:00
|
|
|
return 1;
|
2019-08-09 00:17:42 +08:00
|
|
|
}
|
2018-05-10 11:34:27 +08:00
|
|
|
|
2021-12-14 11:59:29 +08:00
|
|
|
bpf_object__for_each_program(pos, obj) {
|
|
|
|
sec_name = bpf_program__section_name(pos);
|
|
|
|
if (sec_name && !strcmp(sec_name, prog_name)) {
|
|
|
|
prog = pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 05:32:20 +08:00
|
|
|
prog_fd = bpf_program__fd(prog);
|
|
|
|
if (prog_fd < 0) {
|
|
|
|
printf("program not found: %s\n", strerror(prog_fd));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
map_fd = bpf_map__fd(bpf_object__find_map_by_name(obj,
|
2019-08-09 00:17:37 +08:00
|
|
|
"xdp_tx_ports"));
|
2018-07-27 05:32:20 +08:00
|
|
|
if (map_fd < 0) {
|
|
|
|
printf("map not found: %s\n", strerror(map_fd));
|
2018-05-10 11:34:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = optind; i < argc; ++i) {
|
|
|
|
idx = if_nametoindex(argv[i]);
|
|
|
|
if (!idx)
|
|
|
|
idx = strtoul(argv[i], NULL, 0);
|
|
|
|
|
|
|
|
if (!idx) {
|
|
|
|
fprintf(stderr, "Invalid arg\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!attach) {
|
2022-06-06 08:54:25 +08:00
|
|
|
err = do_detach(idx, argv[i], prog_name);
|
2018-05-10 11:34:27 +08:00
|
|
|
if (err)
|
|
|
|
ret = err;
|
|
|
|
} else {
|
2019-08-09 00:17:42 +08:00
|
|
|
err = do_attach(idx, prog_fd, map_fd, argv[i]);
|
2018-05-10 11:34:27 +08:00
|
|
|
if (err)
|
|
|
|
ret = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|