2018-06-25 20:25:02 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <linux/perf_event.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <signal.h>
|
2020-01-20 21:06:49 +08:00
|
|
|
#include <bpf/libbpf.h>
|
2018-06-25 20:25:02 +08:00
|
|
|
#include <bpf/bpf.h>
|
2019-02-02 05:42:26 +08:00
|
|
|
#include <sys/resource.h>
|
2019-02-02 05:42:28 +08:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <linux/if_link.h>
|
2018-06-25 20:25:02 +08:00
|
|
|
|
|
|
|
#include "perf-sys.h"
|
|
|
|
|
|
|
|
#define MAX_CPUS 128
|
2019-07-24 05:34:43 +08:00
|
|
|
static int if_idx;
|
2018-06-25 20:25:02 +08:00
|
|
|
static char *if_name;
|
2019-02-02 05:42:28 +08:00
|
|
|
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
2019-02-02 05:42:30 +08:00
|
|
|
static __u32 prog_id;
|
2019-07-24 05:34:43 +08:00
|
|
|
static struct perf_buffer *pb = NULL;
|
2018-06-25 20:25:02 +08:00
|
|
|
|
|
|
|
static int do_attach(int idx, int fd, const char *name)
|
|
|
|
{
|
2019-02-02 05:42:30 +08:00
|
|
|
struct bpf_prog_info info = {};
|
|
|
|
__u32 info_len = sizeof(info);
|
2018-06-25 20:25:02 +08:00
|
|
|
int err;
|
|
|
|
|
2019-02-02 05:42:28 +08:00
|
|
|
err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
|
2019-02-02 05:42:30 +08:00
|
|
|
if (err < 0) {
|
2018-06-25 20:25:02 +08:00
|
|
|
printf("ERROR: failed to attach program to %s\n", name);
|
2019-02-02 05:42:30 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
|
|
|
|
if (err) {
|
|
|
|
printf("can't get prog info - %s\n", strerror(errno));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
prog_id = info.id;
|
2018-06-25 20:25:02 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_detach(int idx, const char *name)
|
|
|
|
{
|
2019-02-02 05:42:30 +08:00
|
|
|
__u32 curr_prog_id = 0;
|
|
|
|
int err = 0;
|
2018-06-25 20:25:02 +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
|
|
|
err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
|
2019-02-02 05:42:30 +08:00
|
|
|
if (err) {
|
|
|
|
printf("bpf_get_link_xdp_id failed\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (prog_id == curr_prog_id) {
|
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
|
|
|
err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
|
2019-02-02 05:42:30 +08:00
|
|
|
if (err < 0)
|
|
|
|
printf("ERROR: failed to detach prog from %s\n", name);
|
|
|
|
} else if (!curr_prog_id) {
|
|
|
|
printf("couldn't find a prog id on a %s\n", name);
|
|
|
|
} else {
|
|
|
|
printf("program on interface changed, not removing\n");
|
|
|
|
}
|
2018-06-25 20:25:02 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SAMPLE_SIZE 64
|
|
|
|
|
2019-07-24 05:34:43 +08:00
|
|
|
static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
|
2018-06-25 20:25:02 +08:00
|
|
|
{
|
|
|
|
struct {
|
|
|
|
__u16 cookie;
|
|
|
|
__u16 pkt_len;
|
|
|
|
__u8 pkt_data[SAMPLE_SIZE];
|
|
|
|
} __packed *e = data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (e->cookie != 0xdead) {
|
2019-07-24 05:34:43 +08:00
|
|
|
printf("BUG cookie %x sized %d\n", e->cookie, size);
|
|
|
|
return;
|
2018-06-25 20:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
|
|
|
|
for (i = 0; i < 14 && i < e->pkt_len; i++)
|
|
|
|
printf("%02x ", e->pkt_data[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_handler(int signo)
|
|
|
|
{
|
|
|
|
do_detach(if_idx, if_name);
|
2019-07-24 05:34:43 +08:00
|
|
|
perf_buffer__free(pb);
|
2018-06-25 20:25:02 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2019-02-02 05:42:28 +08:00
|
|
|
static void usage(const char *prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %s [OPTS] <ifname|ifindex>\n\n"
|
|
|
|
"OPTS:\n"
|
|
|
|
" -F force loading prog\n",
|
|
|
|
__func__, prog);
|
|
|
|
}
|
|
|
|
|
2018-06-25 20:25:02 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-02-02 05:42:26 +08:00
|
|
|
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
|
2018-06-25 20:25:02 +08:00
|
|
|
struct bpf_prog_load_attr prog_load_attr = {
|
|
|
|
.prog_type = BPF_PROG_TYPE_XDP,
|
|
|
|
};
|
2019-07-24 05:34:43 +08:00
|
|
|
struct perf_buffer_opts pb_opts = {};
|
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
|
|
|
const char *optstr = "FS";
|
2019-02-02 05:42:28 +08:00
|
|
|
int prog_fd, map_fd, opt;
|
2018-06-25 20:25:02 +08:00
|
|
|
struct bpf_object *obj;
|
|
|
|
struct bpf_map *map;
|
|
|
|
char filename[256];
|
2019-07-24 05:34:43 +08:00
|
|
|
int ret, err;
|
2018-06-25 20:25:02 +08:00
|
|
|
|
2019-02-02 05:42:28 +08:00
|
|
|
while ((opt = getopt(argc, argv, optstr)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'F':
|
|
|
|
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
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;
|
2019-02-02 05:42:28 +08:00
|
|
|
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;
|
|
|
|
|
2019-02-02 05:42:28 +08:00
|
|
|
if (optind == argc) {
|
|
|
|
usage(basename(argv[0]));
|
2018-06-25 20:25:02 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-02 05:42:26 +08:00
|
|
|
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
|
|
|
|
perror("setrlimit(RLIMIT_MEMLOCK)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-25 20:25:02 +08:00
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
|
|
|
prog_load_attr.file = filename;
|
|
|
|
|
|
|
|
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!prog_fd) {
|
2019-11-07 08:51:52 +08:00
|
|
|
printf("bpf_prog_load_xattr: %s\n", strerror(errno));
|
2018-06-25 20:25:02 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
map = bpf_map__next(NULL, obj);
|
|
|
|
if (!map) {
|
|
|
|
printf("finding a map in obj file failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
map_fd = bpf_map__fd(map);
|
|
|
|
|
2019-02-02 05:42:28 +08:00
|
|
|
if_idx = if_nametoindex(argv[optind]);
|
2018-06-25 20:25:02 +08:00
|
|
|
if (!if_idx)
|
2019-02-02 05:42:28 +08:00
|
|
|
if_idx = strtoul(argv[optind], NULL, 0);
|
2018-06-25 20:25:02 +08:00
|
|
|
|
|
|
|
if (!if_idx) {
|
|
|
|
fprintf(stderr, "Invalid ifname\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-02-02 05:42:28 +08:00
|
|
|
if_name = argv[optind];
|
|
|
|
err = do_attach(if_idx, prog_fd, if_name);
|
2018-06-25 20:25:02 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (signal(SIGINT, sig_handler) ||
|
|
|
|
signal(SIGHUP, sig_handler) ||
|
|
|
|
signal(SIGTERM, sig_handler)) {
|
|
|
|
perror("signal");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-24 05:34:43 +08:00
|
|
|
pb_opts.sample_cb = print_bpf_output;
|
|
|
|
pb = perf_buffer__new(map_fd, 8, &pb_opts);
|
|
|
|
err = libbpf_get_error(pb);
|
|
|
|
if (err) {
|
|
|
|
perror("perf_buffer setup failed");
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-25 20:25:02 +08:00
|
|
|
|
2019-07-24 05:34:43 +08:00
|
|
|
while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {
|
|
|
|
}
|
2018-06-25 20:25:02 +08:00
|
|
|
|
|
|
|
kill(0, SIGINT);
|
|
|
|
return ret;
|
|
|
|
}
|