2019-05-29 14:18:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-07-17 16:30:25 +00:00
|
|
|
/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
|
|
|
|
*/
|
|
|
|
#include <linux/bpf.h>
|
2017-07-17 20:14:19 +00:00
|
|
|
#include <linux/if_link.h>
|
2017-07-17 16:30:25 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-08-29 14:38:06 +00:00
|
|
|
#include <stdbool.h>
|
2017-07-17 16:30:25 +00:00
|
|
|
#include <string.h>
|
2019-06-25 00:55:36 +00:00
|
|
|
#include <net/if.h>
|
2017-07-17 16:30:25 +00:00
|
|
|
#include <unistd.h>
|
2017-07-17 20:14:19 +00:00
|
|
|
#include <libgen.h>
|
2017-10-28 00:28:22 +00:00
|
|
|
#include <sys/resource.h>
|
2017-07-17 16:30:25 +00:00
|
|
|
|
|
|
|
#include "bpf_util.h"
|
2018-05-15 05:35:02 +00:00
|
|
|
#include <bpf/bpf.h>
|
2020-01-20 13:06:49 +00:00
|
|
|
#include <bpf/libbpf.h>
|
2017-07-17 16:30:25 +00:00
|
|
|
|
|
|
|
static int ifindex_in;
|
|
|
|
static int ifindex_out;
|
2017-08-29 14:38:06 +00:00
|
|
|
static bool ifindex_out_xdp_dummy_attached = true;
|
2019-02-01 21:42:30 +00:00
|
|
|
static __u32 prog_id;
|
|
|
|
static __u32 dummy_prog_id;
|
2017-07-17 16:30:25 +00:00
|
|
|
|
2019-02-01 21:42:28 +00:00
|
|
|
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
2019-02-01 21:42:25 +00:00
|
|
|
static int rxcnt_map_fd;
|
2017-07-17 20:14:19 +00:00
|
|
|
|
2017-07-17 16:30:25 +00:00
|
|
|
static void int_exit(int sig)
|
|
|
|
{
|
2019-02-01 21:42:30 +00:00
|
|
|
__u32 curr_prog_id = 0;
|
|
|
|
|
|
|
|
if (bpf_get_link_xdp_id(ifindex_in, &curr_prog_id, xdp_flags)) {
|
|
|
|
printf("bpf_get_link_xdp_id failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (prog_id == curr_prog_id)
|
|
|
|
bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
|
|
|
|
else if (!curr_prog_id)
|
|
|
|
printf("couldn't find a prog id on iface IN\n");
|
|
|
|
else
|
|
|
|
printf("program on iface IN changed, not removing\n");
|
|
|
|
|
|
|
|
if (ifindex_out_xdp_dummy_attached) {
|
|
|
|
curr_prog_id = 0;
|
|
|
|
if (bpf_get_link_xdp_id(ifindex_out, &curr_prog_id,
|
|
|
|
xdp_flags)) {
|
|
|
|
printf("bpf_get_link_xdp_id failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-02-21 16:05:39 +00:00
|
|
|
if (dummy_prog_id == curr_prog_id)
|
2019-02-01 21:42:30 +00:00
|
|
|
bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
|
|
|
|
else if (!curr_prog_id)
|
|
|
|
printf("couldn't find a prog id on iface OUT\n");
|
|
|
|
else
|
|
|
|
printf("program on iface OUT changed, not removing\n");
|
|
|
|
}
|
2017-07-17 16:30:25 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void poll_stats(int interval, int ifindex)
|
|
|
|
{
|
|
|
|
unsigned int nr_cpus = bpf_num_possible_cpus();
|
|
|
|
__u64 values[nr_cpus], prev[nr_cpus];
|
|
|
|
|
|
|
|
memset(prev, 0, sizeof(prev));
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
__u64 sum = 0;
|
|
|
|
__u32 key = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sleep(interval);
|
2019-02-01 21:42:25 +00:00
|
|
|
assert(bpf_map_lookup_elem(rxcnt_map_fd, &key, values) == 0);
|
2017-07-17 16:30:25 +00:00
|
|
|
for (i = 0; i < nr_cpus; i++)
|
|
|
|
sum += (values[i] - prev[i]);
|
|
|
|
if (sum)
|
|
|
|
printf("ifindex %i: %10llu pkt/s\n",
|
|
|
|
ifindex, sum / interval);
|
|
|
|
memcpy(prev, values, sizeof(values));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:14:19 +00:00
|
|
|
static void usage(const char *prog)
|
2017-07-17 16:30:25 +00:00
|
|
|
{
|
2017-07-17 20:14:19 +00:00
|
|
|
fprintf(stderr,
|
2019-06-25 00:55:36 +00:00
|
|
|
"usage: %s [OPTS] <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n\n"
|
2017-07-17 20:14:19 +00:00
|
|
|
"OPTS:\n"
|
|
|
|
" -S use skb-mode\n"
|
2019-02-01 21:42:28 +00:00
|
|
|
" -N enforce native mode\n"
|
|
|
|
" -F force loading prog\n",
|
2017-07-17 20:14:19 +00:00
|
|
|
prog);
|
|
|
|
}
|
2017-07-17 16:30:25 +00:00
|
|
|
|
2017-07-17 20:14:19 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-02-01 21:42:25 +00:00
|
|
|
struct bpf_prog_load_attr prog_load_attr = {
|
|
|
|
.prog_type = BPF_PROG_TYPE_XDP,
|
|
|
|
};
|
|
|
|
struct bpf_program *prog, *dummy_prog;
|
2019-02-01 21:42:30 +00:00
|
|
|
struct bpf_prog_info info = {};
|
|
|
|
__u32 info_len = sizeof(info);
|
2019-02-01 21:42:25 +00:00
|
|
|
int prog_fd, dummy_prog_fd;
|
2019-02-01 21:42:28 +00:00
|
|
|
const char *optstr = "FSN";
|
2019-02-01 21:42:25 +00:00
|
|
|
struct bpf_object *obj;
|
2017-07-17 20:14:19 +00:00
|
|
|
int ret, opt, key = 0;
|
2019-02-01 21:42:25 +00:00
|
|
|
char filename[256];
|
|
|
|
int tx_port_map_fd;
|
2017-07-17 20:14:19 +00:00
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, optstr)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'S':
|
|
|
|
xdp_flags |= XDP_FLAGS_SKB_MODE;
|
|
|
|
break;
|
|
|
|
case 'N':
|
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 11:07:42 +00:00
|
|
|
/* default, set below */
|
2017-07-17 20:14:19 +00:00
|
|
|
break;
|
2019-02-01 21:42:28 +00:00
|
|
|
case 'F':
|
|
|
|
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
|
|
break;
|
2017-07-17 20:14:19 +00: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 11:07:42 +00:00
|
|
|
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
|
|
|
|
xdp_flags |= XDP_FLAGS_DRV_MODE;
|
|
|
|
|
2017-07-17 20:14:19 +00:00
|
|
|
if (optind == argc) {
|
2019-06-25 00:55:36 +00:00
|
|
|
printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
|
2017-07-17 16:30:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-25 00:55:36 +00:00
|
|
|
ifindex_in = if_nametoindex(argv[optind]);
|
|
|
|
if (!ifindex_in)
|
|
|
|
ifindex_in = strtoul(argv[optind], NULL, 0);
|
|
|
|
|
|
|
|
ifindex_out = if_nametoindex(argv[optind + 1]);
|
|
|
|
if (!ifindex_out)
|
|
|
|
ifindex_out = strtoul(argv[optind + 1], NULL, 0);
|
|
|
|
|
2017-07-17 20:14:19 +00:00
|
|
|
printf("input: %d output: %d\n", ifindex_in, ifindex_out);
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
2019-02-01 21:42:25 +00:00
|
|
|
prog_load_attr.file = filename;
|
|
|
|
|
|
|
|
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
|
|
|
|
return 1;
|
2017-07-17 16:30:25 +00:00
|
|
|
|
2019-02-01 21:42:25 +00:00
|
|
|
prog = bpf_program__next(NULL, obj);
|
|
|
|
dummy_prog = bpf_program__next(prog, obj);
|
|
|
|
if (!prog || !dummy_prog) {
|
|
|
|
printf("finding a prog in obj file failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* bpf_prog_load_xattr gives us the pointer to first prog's fd,
|
|
|
|
* so we're missing only the fd for dummy prog
|
|
|
|
*/
|
|
|
|
dummy_prog_fd = bpf_program__fd(dummy_prog);
|
|
|
|
if (prog_fd < 0 || dummy_prog_fd < 0) {
|
|
|
|
printf("bpf_prog_load_xattr: %s\n", strerror(errno));
|
2017-07-17 16:30:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-01 21:42:25 +00:00
|
|
|
tx_port_map_fd = bpf_object__find_map_fd_by_name(obj, "tx_port");
|
|
|
|
rxcnt_map_fd = bpf_object__find_map_fd_by_name(obj, "rxcnt");
|
|
|
|
if (tx_port_map_fd < 0 || rxcnt_map_fd < 0) {
|
|
|
|
printf("bpf_object__find_map_fd_by_name failed\n");
|
2017-07-17 16:30:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-01 21:42:25 +00:00
|
|
|
if (bpf_set_link_xdp_fd(ifindex_in, prog_fd, xdp_flags) < 0) {
|
2017-08-29 14:38:06 +00:00
|
|
|
printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
|
2017-07-17 16:30:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-02-01 21:42:30 +00:00
|
|
|
ret = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
|
|
|
|
if (ret) {
|
|
|
|
printf("can't get prog info - %s\n", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
prog_id = info.id;
|
|
|
|
|
2017-08-29 14:38:06 +00:00
|
|
|
/* Loading dummy XDP prog on out-device */
|
2019-02-01 21:42:25 +00:00
|
|
|
if (bpf_set_link_xdp_fd(ifindex_out, dummy_prog_fd,
|
2017-08-29 14:38:06 +00:00
|
|
|
(xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
|
|
|
|
printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
|
|
|
|
ifindex_out_xdp_dummy_attached = false;
|
|
|
|
}
|
|
|
|
|
2019-02-01 21:42:30 +00:00
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
ret = bpf_obj_get_info_by_fd(dummy_prog_fd, &info, &info_len);
|
|
|
|
if (ret) {
|
|
|
|
printf("can't get prog info - %s\n", strerror(errno));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
dummy_prog_id = info.id;
|
|
|
|
|
2017-08-29 14:38:06 +00:00
|
|
|
signal(SIGINT, int_exit);
|
|
|
|
signal(SIGTERM, int_exit);
|
|
|
|
|
2017-07-17 16:30:25 +00:00
|
|
|
/* populate virtual to physical port map */
|
2019-02-01 21:42:25 +00:00
|
|
|
ret = bpf_map_update_elem(tx_port_map_fd, &key, &ifindex_out, 0);
|
2017-07-17 16:30:25 +00:00
|
|
|
if (ret) {
|
|
|
|
perror("bpf_update_elem");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
poll_stats(2, ifindex_out);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return 0;
|
|
|
|
}
|