Skip to content

Commit d50ecc4

Browse files
tohojoAlexei Starovoitov
authored andcommitted
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
1 parent 4502789 commit d50ecc4

11 files changed

Lines changed: 58 additions & 12 deletions

‎samples/bpf/xdp1_user.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main(int argc, char **argv)
9898
xdp_flags |= XDP_FLAGS_SKB_MODE;
9999
break;
100100
case 'N':
101-
xdp_flags |= XDP_FLAGS_DRV_MODE;
101+
/* default, set below */
102102
break;
103103
case 'F':
104104
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@@ -109,6 +109,9 @@ int main(int argc, char **argv)
109109
}
110110
}
111111

112+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
113+
xdp_flags |= XDP_FLAGS_DRV_MODE;
114+
112115
if (optind == argc) {
113116
usage(basename(argv[0]));
114117
return 1;

‎samples/bpf/xdp_adjust_tail_user.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int main(int argc, char **argv)
120120
xdp_flags |= XDP_FLAGS_SKB_MODE;
121121
break;
122122
case 'N':
123-
xdp_flags |= XDP_FLAGS_DRV_MODE;
123+
/* default, set below */
124124
break;
125125
case 'F':
126126
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@@ -132,6 +132,9 @@ int main(int argc, char **argv)
132132
opt_flags[opt] = 0;
133133
}
134134

135+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
136+
xdp_flags |= XDP_FLAGS_DRV_MODE;
137+
135138
for (i = 0; i < strlen(optstr); i++) {
136139
if (opt_flags[(unsigned int)optstr[i]]) {
137140
fprintf(stderr, "Missing argument -%c\n", optstr[i]);

‎samples/bpf/xdp_fwd_user.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
#include "libbpf.h"
2828
#include <bpf/bpf.h>
2929

30+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
31+
3032
static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
3133
{
3234
int err;
3335

34-
err = bpf_set_link_xdp_fd(idx, prog_fd, 0);
36+
err = bpf_set_link_xdp_fd(idx, prog_fd, xdp_flags);
3537
if (err < 0) {
3638
printf("ERROR: failed to attach program to %s\n", name);
3739
return err;
@@ -49,7 +51,7 @@ static int do_detach(int idx, const char *name)
4951
{
5052
int err;
5153

52-
err = bpf_set_link_xdp_fd(idx, -1, 0);
54+
err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
5355
if (err < 0)
5456
printf("ERROR: failed to detach program from %s\n", name);
5557

@@ -83,11 +85,17 @@ int main(int argc, char **argv)
8385
int attach = 1;
8486
int ret = 0;
8587

86-
while ((opt = getopt(argc, argv, ":dD")) != -1) {
88+
while ((opt = getopt(argc, argv, ":dDSF")) != -1) {
8789
switch (opt) {
8890
case 'd':
8991
attach = 0;
9092
break;
93+
case 'S':
94+
xdp_flags |= XDP_FLAGS_SKB_MODE;
95+
break;
96+
case 'F':
97+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
98+
break;
9199
case 'D':
92100
prog_name = "xdp_fwd_direct";
93101
break;
@@ -97,6 +105,9 @@ int main(int argc, char **argv)
97105
}
98106
}
99107

108+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
109+
xdp_flags |= XDP_FLAGS_DRV_MODE;
110+
100111
if (optind == argc) {
101112
usage(basename(argv[0]));
102113
return 1;

‎samples/bpf/xdp_redirect_cpu_user.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,10 @@ int main(int argc, char **argv)
728728
return EXIT_FAIL_OPTION;
729729
}
730730
}
731+
732+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
733+
xdp_flags |= XDP_FLAGS_DRV_MODE;
734+
731735
/* Required option */
732736
if (ifindex == -1) {
733737
fprintf(stderr, "ERR: required option --dev missing\n");

‎samples/bpf/xdp_redirect_map_user.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int main(int argc, char **argv)
116116
xdp_flags |= XDP_FLAGS_SKB_MODE;
117117
break;
118118
case 'N':
119-
xdp_flags |= XDP_FLAGS_DRV_MODE;
119+
/* default, set below */
120120
break;
121121
case 'F':
122122
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@@ -127,6 +127,9 @@ int main(int argc, char **argv)
127127
}
128128
}
129129

130+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
131+
xdp_flags |= XDP_FLAGS_DRV_MODE;
132+
130133
if (optind == argc) {
131134
printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
132135
return 1;

‎samples/bpf/xdp_redirect_user.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int main(int argc, char **argv)
117117
xdp_flags |= XDP_FLAGS_SKB_MODE;
118118
break;
119119
case 'N':
120-
xdp_flags |= XDP_FLAGS_DRV_MODE;
120+
/* default, set below */
121121
break;
122122
case 'F':
123123
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@@ -128,6 +128,9 @@ int main(int argc, char **argv)
128128
}
129129
}
130130

131+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
132+
xdp_flags |= XDP_FLAGS_DRV_MODE;
133+
131134
if (optind == argc) {
132135
printf("usage: %s <IFNAME|IFINDEX>_IN <IFNAME|IFINDEX>_OUT\n", argv[0]);
133136
return 1;

‎samples/bpf/xdp_router_ipv4_user.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,9 @@ int main(int ac, char **argv)
662662
}
663663
}
664664

665+
if (!(flags & XDP_FLAGS_SKB_MODE))
666+
flags |= XDP_FLAGS_DRV_MODE;
667+
665668
if (optind == ac) {
666669
usage(basename(argv[0]));
667670
return 1;

‎samples/bpf/xdp_rxq_info_user.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ int main(int argc, char **argv)
551551
return EXIT_FAIL_OPTION;
552552
}
553553
}
554+
555+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
556+
xdp_flags |= XDP_FLAGS_DRV_MODE;
557+
554558
/* Required option */
555559
if (ifindex == -1) {
556560
fprintf(stderr, "ERR: required option --dev missing\n");

‎samples/bpf/xdp_sample_pkts_user.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ static int do_detach(int idx, const char *name)
5252
__u32 curr_prog_id = 0;
5353
int err = 0;
5454

55-
err = bpf_get_link_xdp_id(idx, &curr_prog_id, 0);
55+
err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
5656
if (err) {
5757
printf("bpf_get_link_xdp_id failed\n");
5858
return err;
5959
}
6060
if (prog_id == curr_prog_id) {
61-
err = bpf_set_link_xdp_fd(idx, -1, 0);
61+
err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
6262
if (err < 0)
6363
printf("ERROR: failed to detach prog from %s\n", name);
6464
} else if (!curr_prog_id) {
@@ -115,7 +115,7 @@ int main(int argc, char **argv)
115115
.prog_type = BPF_PROG_TYPE_XDP,
116116
};
117117
struct perf_buffer_opts pb_opts = {};
118-
const char *optstr = "F";
118+
const char *optstr = "FS";
119119
int prog_fd, map_fd, opt;
120120
struct bpf_object *obj;
121121
struct bpf_map *map;
@@ -127,12 +127,18 @@ int main(int argc, char **argv)
127127
case 'F':
128128
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
129129
break;
130+
case 'S':
131+
xdp_flags |= XDP_FLAGS_SKB_MODE;
132+
break;
130133
default:
131134
usage(basename(argv[0]));
132135
return 1;
133136
}
134137
}
135138

139+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
140+
xdp_flags |= XDP_FLAGS_DRV_MODE;
141+
136142
if (optind == argc) {
137143
usage(basename(argv[0]));
138144
return 1;

‎samples/bpf/xdp_tx_iptunnel_user.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ int main(int argc, char **argv)
231231
xdp_flags |= XDP_FLAGS_SKB_MODE;
232232
break;
233233
case 'N':
234-
xdp_flags |= XDP_FLAGS_DRV_MODE;
234+
/* default, set below */
235235
break;
236236
case 'F':
237237
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
@@ -243,6 +243,9 @@ int main(int argc, char **argv)
243243
opt_flags[opt] = 0;
244244
}
245245

246+
if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
247+
xdp_flags |= XDP_FLAGS_DRV_MODE;
248+
246249
for (i = 0; i < strlen(optstr); i++) {
247250
if (opt_flags[(unsigned int)optstr[i]]) {
248251
fprintf(stderr, "Missing argument -%c\n", optstr[i]);

0 commit comments

Comments
 (0)