mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-24 13:13:57 +08:00
d50ecc46d1
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
168 lines
3.4 KiB
C
168 lines
3.4 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/* Copyright (c) 2016 PLUMgrid
|
|
*/
|
|
#include <linux/bpf.h>
|
|
#include <linux/if_link.h>
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <libgen.h>
|
|
#include <sys/resource.h>
|
|
#include <net/if.h>
|
|
|
|
#include "bpf_util.h"
|
|
#include "bpf.h"
|
|
#include "libbpf.h"
|
|
|
|
static int ifindex;
|
|
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
static __u32 prog_id;
|
|
|
|
static void int_exit(int sig)
|
|
{
|
|
__u32 curr_prog_id = 0;
|
|
|
|
if (bpf_get_link_xdp_id(ifindex, &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, -1, xdp_flags);
|
|
else if (!curr_prog_id)
|
|
printf("couldn't find a prog id on a given interface\n");
|
|
else
|
|
printf("program on interface changed, not removing\n");
|
|
exit(0);
|
|
}
|
|
|
|
/* simple per-protocol drop counter
|
|
*/
|
|
static void poll_stats(int map_fd, int interval)
|
|
{
|
|
unsigned int nr_cpus = bpf_num_possible_cpus();
|
|
__u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
|
|
int i;
|
|
|
|
while (1) {
|
|
__u32 key = UINT32_MAX;
|
|
|
|
sleep(interval);
|
|
|
|
while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
|
|
__u64 sum = 0;
|
|
|
|
assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
|
|
for (i = 0; i < nr_cpus; i++)
|
|
sum += values[i];
|
|
if (sum > prev[key])
|
|
printf("proto %u: %10llu pkt/s\n",
|
|
key, (sum - prev[key]) / interval);
|
|
prev[key] = sum;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void usage(const char *prog)
|
|
{
|
|
fprintf(stderr,
|
|
"usage: %s [OPTS] IFACE\n\n"
|
|
"OPTS:\n"
|
|
" -S use skb-mode\n"
|
|
" -N enforce native mode\n"
|
|
" -F force loading prog\n",
|
|
prog);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
|
|
struct bpf_prog_load_attr prog_load_attr = {
|
|
.prog_type = BPF_PROG_TYPE_XDP,
|
|
};
|
|
struct bpf_prog_info info = {};
|
|
__u32 info_len = sizeof(info);
|
|
const char *optstr = "FSN";
|
|
int prog_fd, map_fd, opt;
|
|
struct bpf_object *obj;
|
|
struct bpf_map *map;
|
|
char filename[256];
|
|
int err;
|
|
|
|
while ((opt = getopt(argc, argv, optstr)) != -1) {
|
|
switch (opt) {
|
|
case 'S':
|
|
xdp_flags |= XDP_FLAGS_SKB_MODE;
|
|
break;
|
|
case 'N':
|
|
/* default, set below */
|
|
break;
|
|
case 'F':
|
|
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
|
|
break;
|
|
default:
|
|
usage(basename(argv[0]));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
|
|
xdp_flags |= XDP_FLAGS_DRV_MODE;
|
|
|
|
if (optind == argc) {
|
|
usage(basename(argv[0]));
|
|
return 1;
|
|
}
|
|
|
|
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
|
|
perror("setrlimit(RLIMIT_MEMLOCK)");
|
|
return 1;
|
|
}
|
|
|
|
ifindex = if_nametoindex(argv[optind]);
|
|
if (!ifindex) {
|
|
perror("if_nametoindex");
|
|
return 1;
|
|
}
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
if (!prog_fd) {
|
|
printf("bpf_prog_load_xattr: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
signal(SIGINT, int_exit);
|
|
signal(SIGTERM, int_exit);
|
|
|
|
if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
|
|
printf("link set xdp fd failed\n");
|
|
return 1;
|
|
}
|
|
|
|
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
|
|
if (err) {
|
|
printf("can't get prog info - %s\n", strerror(errno));
|
|
return err;
|
|
}
|
|
prog_id = info.id;
|
|
|
|
poll_stats(map_fd, 2);
|
|
|
|
return 0;
|
|
}
|