2018-05-10 10:34:27 +07: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 20:06:49 +07:00
|
|
|
#include <bpf/libbpf.h>
|
2018-05-15 12:35:02 +07:00
|
|
|
#include <bpf/bpf.h>
|
2018-05-10 10:34:27 +07: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 18:07:42 +07:00
|
|
|
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
|
2019-08-08 23:17:42 +07:00
|
|
|
static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
|
2018-05-10 10:34:27 +07:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
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 18:07:42 +07:00
|
|
|
err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
|
2019-08-08 23:17:42 +07:00
|
|
|
if (err < 0) {
|
2018-05-10 10:34:27 +07:00
|
|
|
printf("ERROR: failed to attach program to %s\n", name);
|
2019-08-08 23:17:42 +07: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 10:34:27 +07:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_detach(int idx, const char *name)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
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 18:07:42 +07:00
|
|
|
err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
|
2018-05-10 10:34:27 +07:00
|
|
|
if (err < 0)
|
|
|
|
printf("ERROR: failed to detach program from %s\n", name);
|
|
|
|
|
2019-08-08 23:17:42 +07:00
|
|
|
/* TODO: Remember to cleanup map, when adding use of shared map
|
|
|
|
* bpf_map_delete_elem((map_fd, &idx);
|
|
|
|
*/
|
2018-05-10 10:34:27 +07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char *prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [OPTS] interface-list\n"
|
|
|
|
"\nOPTS:\n"
|
|
|
|
" -d detach program\n"
|
|
|
|
" -D direct table lookups (skip fib rules)\n",
|
|
|
|
prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-07-27 04:32:20 +07:00
|
|
|
struct bpf_prog_load_attr prog_load_attr = {
|
|
|
|
.prog_type = BPF_PROG_TYPE_XDP,
|
|
|
|
};
|
|
|
|
const char *prog_name = "xdp_fwd";
|
|
|
|
struct bpf_program *prog;
|
2019-08-08 23:17:42 +07:00
|
|
|
int prog_fd, map_fd = -1;
|
2018-05-10 10:34:27 +07:00
|
|
|
char filename[PATH_MAX];
|
2018-07-27 04:32:20 +07:00
|
|
|
struct bpf_object *obj;
|
2018-05-10 10:34:27 +07: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 18:07:42 +07:00
|
|
|
while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
|
2018-05-10 10:34:27 +07: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 18:07:42 +07:00
|
|
|
case 'S':
|
|
|
|
xdp_flags |= XDP_FLAGS_SKB_MODE;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
break;
|
2018-05-10 10:34:27 +07:00
|
|
|
case 'D':
|
2018-07-27 04:32:20 +07:00
|
|
|
prog_name = "xdp_fwd_direct";
|
2018-05-10 10:34:27 +07: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 18:07:42 +07:00
|
|
|
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
|
|
|
|
xdp_flags |= XDP_FLAGS_DRV_MODE;
|
|
|
|
|
2018-05-10 10:34:27 +07:00
|
|
|
if (optind == argc) {
|
|
|
|
usage(basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attach) {
|
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
2018-07-27 04:32:20 +07:00
|
|
|
prog_load_attr.file = filename;
|
2018-05-10 10:34:27 +07:00
|
|
|
|
|
|
|
if (access(filename, O_RDONLY) < 0) {
|
|
|
|
printf("error accessing file %s: %s\n",
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-08 23:17:42 +07:00
|
|
|
err = bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd);
|
|
|
|
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 10:34:27 +07:00
|
|
|
return 1;
|
2019-08-08 23:17:42 +07:00
|
|
|
}
|
2018-05-10 10:34:27 +07:00
|
|
|
|
2018-07-27 04:32:20 +07:00
|
|
|
prog = bpf_object__find_program_by_title(obj, prog_name);
|
|
|
|
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-08 23:17:37 +07:00
|
|
|
"xdp_tx_ports"));
|
2018-07-27 04:32:20 +07:00
|
|
|
if (map_fd < 0) {
|
|
|
|
printf("map not found: %s\n", strerror(map_fd));
|
2018-05-10 10:34:27 +07: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) {
|
|
|
|
err = do_detach(idx, argv[i]);
|
|
|
|
if (err)
|
|
|
|
ret = err;
|
|
|
|
} else {
|
2019-08-08 23:17:42 +07:00
|
|
|
err = do_attach(idx, prog_fd, map_fd, argv[i]);
|
2018-05-10 10:34:27 +07:00
|
|
|
if (err)
|
|
|
|
ret = err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|