1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01: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
This commit is contained in:
Toke Høiland-Jørgensen 2019-12-16 12:07:42 +01:00 committed by Alexei Starovoitov
parent 450278977a
commit d50ecc46d1
11 changed files with 58 additions and 12 deletions

View file

@ -98,7 +98,7 @@ int main(int argc, char **argv)
xdp_flags |= XDP_FLAGS_SKB_MODE; xdp_flags |= XDP_FLAGS_SKB_MODE;
break; break;
case 'N': case 'N':
xdp_flags |= XDP_FLAGS_DRV_MODE; /* default, set below */
break; break;
case 'F': case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@ -109,6 +109,9 @@ int main(int argc, char **argv)
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
if (optind == argc) { if (optind == argc) {
usage(basename(argv[0])); usage(basename(argv[0]));
return 1; return 1;

View file

@ -120,7 +120,7 @@ int main(int argc, char **argv)
xdp_flags |= XDP_FLAGS_SKB_MODE; xdp_flags |= XDP_FLAGS_SKB_MODE;
break; break;
case 'N': case 'N':
xdp_flags |= XDP_FLAGS_DRV_MODE; /* default, set below */
break; break;
case 'F': case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@ -132,6 +132,9 @@ int main(int argc, char **argv)
opt_flags[opt] = 0; opt_flags[opt] = 0;
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
for (i = 0; i < strlen(optstr); i++) { for (i = 0; i < strlen(optstr); i++) {
if (opt_flags[(unsigned int)optstr[i]]) { if (opt_flags[(unsigned int)optstr[i]]) {
fprintf(stderr, "Missing argument -%c\n", optstr[i]); fprintf(stderr, "Missing argument -%c\n", optstr[i]);

View file

@ -27,11 +27,13 @@
#include "libbpf.h" #include "libbpf.h"
#include <bpf/bpf.h> #include <bpf/bpf.h>
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
static int do_attach(int idx, int prog_fd, int map_fd, const char *name) static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
{ {
int err; int err;
err = bpf_set_link_xdp_fd(idx, prog_fd, 0); err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
if (err < 0) { if (err < 0) {
printf("ERROR: failed to attach program to %s\n", name); printf("ERROR: failed to attach program to %s\n", name);
return err; return err;
@ -49,7 +51,7 @@ static int do_detach(int idx, const char *name)
{ {
int err; int err;
err = bpf_set_link_xdp_fd(idx, -1, 0); err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
if (err < 0) if (err < 0)
printf("ERROR: failed to detach program from %s\n", name); printf("ERROR: failed to detach program from %s\n", name);
@ -83,11 +85,17 @@ int main(int argc, char **argv)
int attach = 1; int attach = 1;
int ret = 0; int ret = 0;
while ((opt = getopt(argc, argv, ":dD")) != -1) { while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
attach = 0; attach = 0;
break; break;
case 'S':
xdp_flags |= XDP_FLAGS_SKB_MODE;
break;
case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
break;
case 'D': case 'D':
prog_name = "xdp_fwd_direct"; prog_name = "xdp_fwd_direct";
break; break;
@ -97,6 +105,9 @@ int main(int argc, char **argv)
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
if (optind == argc) { if (optind == argc) {
usage(basename(argv[0])); usage(basename(argv[0]));
return 1; return 1;

View file

@ -728,6 +728,10 @@ int main(int argc, char **argv)
return EXIT_FAIL_OPTION; return EXIT_FAIL_OPTION;
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
/* Required option */ /* Required option */
if (ifindex == -1) { if (ifindex == -1) {
fprintf(stderr, "ERR: required option --dev missing\n"); fprintf(stderr, "ERR: required option --dev missing\n");

View file

@ -116,7 +116,7 @@ int main(int argc, char **argv)
xdp_flags |= XDP_FLAGS_SKB_MODE; xdp_flags |= XDP_FLAGS_SKB_MODE;
break; break;
case 'N': case 'N':
xdp_flags |= XDP_FLAGS_DRV_MODE; /* default, set below */
break; break;
case 'F': case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@ -127,6 +127,9 @@ int main(int argc, char **argv)
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
if (optind == argc) { if (optind == argc) {
printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]); printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
return 1; return 1;

View file

@ -117,7 +117,7 @@ int main(int argc, char **argv)
xdp_flags |= XDP_FLAGS_SKB_MODE; xdp_flags |= XDP_FLAGS_SKB_MODE;
break; break;
case 'N': case 'N':
xdp_flags |= XDP_FLAGS_DRV_MODE; /* default, set below */
break; break;
case 'F': case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@ -128,6 +128,9 @@ int main(int argc, char **argv)
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
if (optind == argc) { if (optind == argc) {
printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]); printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
return 1; return 1;

View file

@ -662,6 +662,9 @@ int main(int ac, char **argv)
} }
} }
if (!(flags & XDP_FLAGS_SKB_MODE))
flags |= XDP_FLAGS_DRV_MODE;
if (optind == ac) { if (optind == ac) {
usage(basename(argv[0])); usage(basename(argv[0]));
return 1; return 1;

View file

@ -551,6 +551,10 @@ int main(int argc, char **argv)
return EXIT_FAIL_OPTION; return EXIT_FAIL_OPTION;
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
/* Required option */ /* Required option */
if (ifindex == -1) { if (ifindex == -1) {
fprintf(stderr, "ERR: required option --dev missing\n"); fprintf(stderr, "ERR: required option --dev missing\n");

View file

@ -52,13 +52,13 @@ static int do_detach(int idx, const char *name)
__u32 curr_prog_id = 0; __u32 curr_prog_id = 0;
int err = 0; int err = 0;
err = bpf_get_link_xdp_id(idx, &curr_prog_id, 0); err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
if (err) { if (err) {
printf("bpf_get_link_xdp_id failed\n"); printf("bpf_get_link_xdp_id failed\n");
return err; return err;
} }
if (prog_id == curr_prog_id) { if (prog_id == curr_prog_id) {
err = bpf_set_link_xdp_fd(idx, -1, 0); err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
if (err < 0) if (err < 0)
printf("ERROR: failed to detach prog from %s\n", name); printf("ERROR: failed to detach prog from %s\n", name);
} else if (!curr_prog_id) { } else if (!curr_prog_id) {
@ -115,7 +115,7 @@ int main(int argc, char **argv)
.prog_type = BPF_PROG_TYPE_XDP, .prog_type = BPF_PROG_TYPE_XDP,
}; };
struct perf_buffer_opts pb_opts = {}; struct perf_buffer_opts pb_opts = {};
const char *optstr = "F"; const char *optstr = "FS";
int prog_fd, map_fd, opt; int prog_fd, map_fd, opt;
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_map *map; struct bpf_map *map;
@ -127,12 +127,18 @@ int main(int argc, char **argv)
case 'F': case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
break; break;
case 'S':
xdp_flags |= XDP_FLAGS_SKB_MODE;
break;
default: default:
usage(basename(argv[0])); usage(basename(argv[0]));
return 1; return 1;
} }
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
if (optind == argc) { if (optind == argc) {
usage(basename(argv[0])); usage(basename(argv[0]));
return 1; return 1;

View file

@ -231,7 +231,7 @@ int main(int argc, char **argv)
xdp_flags |= XDP_FLAGS_SKB_MODE; xdp_flags |= XDP_FLAGS_SKB_MODE;
break; break;
case 'N': case 'N':
xdp_flags |= XDP_FLAGS_DRV_MODE; /* default, set below */
break; break;
case 'F': case 'F':
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@ -243,6 +243,9 @@ int main(int argc, char **argv)
opt_flags[opt] = 0; opt_flags[opt] = 0;
} }
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
xdp_flags |= XDP_FLAGS_DRV_MODE;
for (i = 0; i < strlen(optstr); i++) { for (i = 0; i < strlen(optstr); i++) {
if (opt_flags[(unsigned int)optstr[i]]) { if (opt_flags[(unsigned int)optstr[i]]) {
fprintf(stderr, "Missing argument -%c\n", optstr[i]); fprintf(stderr, "Missing argument -%c\n", optstr[i]);

View file

@ -440,7 +440,7 @@ static void parse_command_line(int argc, char **argv)
opt_xdp_bind_flags |= XDP_COPY; opt_xdp_bind_flags |= XDP_COPY;
break; break;
case 'N': case 'N':
opt_xdp_flags |= XDP_FLAGS_DRV_MODE; /* default, set below */
break; break;
case 'n': case 'n':
opt_interval = atoi(optarg); opt_interval = atoi(optarg);
@ -474,6 +474,9 @@ static void parse_command_line(int argc, char **argv)
} }
} }
if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
opt_ifindex = if_nametoindex(opt_if); opt_ifindex = if_nametoindex(opt_if);
if (!opt_ifindex) { if (!opt_ifindex) {
fprintf(stderr, "ERROR: interface \"%s\" does not exist\n", fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",