Skip to content

Commit 743e568

Browse files
Maciej Fijalkowskiborkmann
authored andcommitted
samples/bpf: Add a "force" flag to XDP samples
Make xdp samples consistent with iproute2 behavior and set the XDP_FLAGS_UPDATE_IF_NOEXIST by default when setting the xdp program on interface. Provide an option for user to force the program loading, which as a result will not include the mentioned flag in bpf_set_link_xdp_fd call. Signed-off-by: Maciej Fijalkowski <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Acked-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 01dde20 commit 743e568

10 files changed

+119
-40
lines changed

samples/bpf/xdp1_user.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "bpf/libbpf.h"
2323

2424
static int ifindex;
25-
static __u32 xdp_flags;
25+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
2626

2727
static void int_exit(int sig)
2828
{
@@ -63,7 +63,8 @@ static void usage(const char *prog)
6363
"usage: %s [OPTS] IFACE\n\n"
6464
"OPTS:\n"
6565
" -S use skb-mode\n"
66-
" -N enforce native mode\n",
66+
" -N enforce native mode\n"
67+
" -F force loading prog\n",
6768
prog);
6869
}
6970

@@ -73,7 +74,7 @@ int main(int argc, char **argv)
7374
struct bpf_prog_load_attr prog_load_attr = {
7475
.prog_type = BPF_PROG_TYPE_XDP,
7576
};
76-
const char *optstr = "SN";
77+
const char *optstr = "FSN";
7778
int prog_fd, map_fd, opt;
7879
struct bpf_object *obj;
7980
struct bpf_map *map;
@@ -87,6 +88,9 @@ int main(int argc, char **argv)
8788
case 'N':
8889
xdp_flags |= XDP_FLAGS_DRV_MODE;
8990
break;
91+
case 'F':
92+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
93+
break;
9094
default:
9195
usage(basename(argv[0]));
9296
return 1;

samples/bpf/xdp_adjust_tail_user.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define STATS_INTERVAL_S 2U
2525

2626
static int ifindex = -1;
27-
static __u32 xdp_flags;
27+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
2828

2929
static void int_exit(int sig)
3030
{
@@ -60,6 +60,7 @@ static void usage(const char *cmd)
6060
printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
6161
printf(" -S use skb-mode\n");
6262
printf(" -N enforce native mode\n");
63+
printf(" -F force loading prog\n");
6364
printf(" -h Display this help\n");
6465
}
6566

@@ -70,8 +71,8 @@ int main(int argc, char **argv)
7071
.prog_type = BPF_PROG_TYPE_XDP,
7172
};
7273
unsigned char opt_flags[256] = {};
74+
const char *optstr = "i:T:SNFh";
7375
unsigned int kill_after_s = 0;
74-
const char *optstr = "i:T:SNh";
7576
int i, prog_fd, map_fd, opt;
7677
struct bpf_object *obj;
7778
struct bpf_map *map;
@@ -96,6 +97,9 @@ int main(int argc, char **argv)
9697
case 'N':
9798
xdp_flags |= XDP_FLAGS_DRV_MODE;
9899
break;
100+
case 'F':
101+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
102+
break;
99103
default:
100104
usage(argv[0]);
101105
return 1;

samples/bpf/xdp_redirect_cpu_user.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int ifindex = -1;
3333
static char ifname_buf[IF_NAMESIZE];
3434
static char *ifname;
3535

36-
static __u32 xdp_flags;
36+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3737
static int cpu_map_fd;
3838
static int rx_cnt_map_fd;
3939
static int redirect_err_cnt_map_fd;
@@ -62,6 +62,7 @@ static const struct option long_options[] = {
6262
{"cpu", required_argument, NULL, 'c' },
6363
{"stress-mode", no_argument, NULL, 'x' },
6464
{"no-separators", no_argument, NULL, 'z' },
65+
{"force", no_argument, NULL, 'F' },
6566
{0, 0, NULL, 0 }
6667
};
6768

@@ -651,7 +652,7 @@ int main(int argc, char **argv)
651652
mark_cpus_unavailable();
652653

653654
/* Parse commands line args */
654-
while ((opt = getopt_long(argc, argv, "hSd:",
655+
while ((opt = getopt_long(argc, argv, "hSd:s:p:q:c:xzF",
655656
long_options, &longindex)) != -1) {
656657
switch (opt) {
657658
case 'd':
@@ -700,6 +701,9 @@ int main(int argc, char **argv)
700701
case 'q':
701702
qsize = atoi(optarg);
702703
break;
704+
case 'F':
705+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
706+
break;
703707
case 'h':
704708
error:
705709
default:

samples/bpf/xdp_redirect_map_user.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int ifindex_in;
3030
static int ifindex_out;
3131
static bool ifindex_out_xdp_dummy_attached = true;
3232

33-
static __u32 xdp_flags;
33+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3434
static int rxcnt_map_fd;
3535

3636
static void int_exit(int sig)
@@ -70,7 +70,8 @@ static void usage(const char *prog)
7070
"usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
7171
"OPTS:\n"
7272
" -S use skb-mode\n"
73-
" -N enforce native mode\n",
73+
" -N enforce native mode\n"
74+
" -F force loading prog\n",
7475
prog);
7576
}
7677

@@ -82,7 +83,7 @@ int main(int argc, char **argv)
8283
};
8384
struct bpf_program *prog, *dummy_prog;
8485
int prog_fd, dummy_prog_fd;
85-
const char *optstr = "SN";
86+
const char *optstr = "FSN";
8687
struct bpf_object *obj;
8788
int ret, opt, key = 0;
8889
char filename[256];
@@ -96,6 +97,9 @@ int main(int argc, char **argv)
9697
case 'N':
9798
xdp_flags |= XDP_FLAGS_DRV_MODE;
9899
break;
100+
case 'F':
101+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
102+
break;
99103
default:
100104
usage(basename(argv[0]));
101105
return 1;

samples/bpf/xdp_redirect_user.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int ifindex_in;
3030
static int ifindex_out;
3131
static bool ifindex_out_xdp_dummy_attached = true;
3232

33-
static __u32 xdp_flags;
33+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3434
static int rxcnt_map_fd;
3535

3636
static void int_exit(int sig)
@@ -70,7 +70,8 @@ static void usage(const char *prog)
7070
"usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
7171
"OPTS:\n"
7272
" -S use skb-mode\n"
73-
" -N enforce native mode\n",
73+
" -N enforce native mode\n"
74+
" -F force loading prog\n",
7475
prog);
7576
}
7677

@@ -83,7 +84,7 @@ int main(int argc, char **argv)
8384
};
8485
struct bpf_program *prog, *dummy_prog;
8586
int prog_fd, tx_port_map_fd, opt;
86-
const char *optstr = "SN";
87+
const char *optstr = "FSN";
8788
struct bpf_object *obj;
8889
char filename[256];
8990
int dummy_prog_fd;
@@ -97,6 +98,9 @@ int main(int argc, char **argv)
9798
case 'N':
9899
xdp_flags |= XDP_FLAGS_DRV_MODE;
99100
break;
101+
case 'F':
102+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
103+
break;
100104
default:
101105
usage(basename(argv[0]));
102106
return 1;

samples/bpf/xdp_router_ipv4_user.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
#include "bpf_util.h"
2727
#include "bpf/libbpf.h"
2828
#include <sys/resource.h>
29+
#include <libgen.h>
2930

30-
int sock, sock_arp, flags = 0;
31+
int sock, sock_arp, flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3132
static int total_ifindex;
3233
int *ifindex_list;
3334
char buf[8192];
@@ -608,33 +609,56 @@ static int monitor_route(void)
608609
return ret;
609610
}
610611

612+
static void usage(const char *prog)
613+
{
614+
fprintf(stderr,
615+
"%s: %s [OPTS] interface name list\n\n"
616+
"OPTS:\n"
617+
" -S use skb-mode\n"
618+
" -F force loading prog\n",
619+
__func__, prog);
620+
}
621+
611622
int main(int ac, char **argv)
612623
{
613624
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
614625
struct bpf_prog_load_attr prog_load_attr = {
615626
.prog_type = BPF_PROG_TYPE_XDP,
616627
};
628+
const char *optstr = "SF";
617629
struct bpf_object *obj;
618630
char filename[256];
619631
char **ifname_list;
620-
int prog_fd;
632+
int prog_fd, opt;
621633
int i = 1;
622634

623635
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
624636
prog_load_attr.file = filename;
625637

626-
if (ac < 2) {
627-
printf("usage: %s [-S] Interface name list\n", argv[0]);
628-
return 1;
638+
total_ifindex = ac - 1;
639+
ifname_list = (argv + 1);
640+
641+
while ((opt = getopt(ac, argv, optstr)) != -1) {
642+
switch (opt) {
643+
case 'S':
644+
flags |= XDP_FLAGS_SKB_MODE;
645+
total_ifindex--;
646+
ifname_list++;
647+
break;
648+
case 'F':
649+
flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
650+
total_ifindex--;
651+
ifname_list++;
652+
break;
653+
default:
654+
usage(basename(argv[0]));
655+
return 1;
656+
}
629657
}
630-
if (!strcmp(argv[1], "-S")) {
631-
flags = XDP_FLAGS_SKB_MODE;
632-
total_ifindex = ac - 2;
633-
ifname_list = (argv + 2);
634-
} else {
635-
flags = 0;
636-
total_ifindex = ac - 1;
637-
ifname_list = (argv + 1);
658+
659+
if (optind == ac) {
660+
usage(basename(argv[0]));
661+
return 1;
638662
}
639663

640664
if (setrlimit(RLIMIT_MEMLOCK, &r)) {

samples/bpf/xdp_rxq_info_user.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static int ifindex = -1;
3030
static char ifname_buf[IF_NAMESIZE];
3131
static char *ifname;
3232

33-
static __u32 xdp_flags;
33+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3434

3535
static struct bpf_map *stats_global_map;
3636
static struct bpf_map *rx_queue_index_map;
@@ -52,6 +52,7 @@ static const struct option long_options[] = {
5252
{"action", required_argument, NULL, 'a' },
5353
{"readmem", no_argument, NULL, 'r' },
5454
{"swapmac", no_argument, NULL, 'm' },
55+
{"force", no_argument, NULL, 'F' },
5556
{0, 0, NULL, 0 }
5657
};
5758

@@ -487,7 +488,7 @@ int main(int argc, char **argv)
487488
}
488489

489490
/* Parse commands line args */
490-
while ((opt = getopt_long(argc, argv, "hSd:",
491+
while ((opt = getopt_long(argc, argv, "FhSrmzd:s:a:",
491492
long_options, &longindex)) != -1) {
492493
switch (opt) {
493494
case 'd':
@@ -524,6 +525,9 @@ int main(int argc, char **argv)
524525
case 'm':
525526
cfg_options |= SWAP_MAC;
526527
break;
528+
case 'F':
529+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
530+
break;
527531
case 'h':
528532
error:
529533
default:

samples/bpf/xdp_sample_pkts_user.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <libbpf.h>
1414
#include <bpf/bpf.h>
1515
#include <sys/resource.h>
16+
#include <libgen.h>
17+
#include <linux/if_link.h>
1618

1719
#include "perf-sys.h"
1820
#include "trace_helpers.h"
@@ -21,12 +23,13 @@
2123
static int pmu_fds[MAX_CPUS], if_idx;
2224
static struct perf_event_mmap_page *headers[MAX_CPUS];
2325
static char *if_name;
26+
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
2427

2528
static int do_attach(int idx, int fd, const char *name)
2629
{
2730
int err;
2831

29-
err = bpf_set_link_xdp_fd(idx, fd, 0);
32+
err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
3033
if (err < 0)
3134
printf("ERROR: failed to attach program to %s\n", name);
3235

@@ -98,21 +101,42 @@ static void sig_handler(int signo)
98101
exit(0);
99102
}
100103

104+
static void usage(const char *prog)
105+
{
106+
fprintf(stderr,
107+
"%s: %s [OPTS] <ifname|ifindex>\n\n"
108+
"OPTS:\n"
109+
" -F force loading prog\n",
110+
__func__, prog);
111+
}
112+
101113
int main(int argc, char **argv)
102114
{
103115
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
104116
struct bpf_prog_load_attr prog_load_attr = {
105117
.prog_type = BPF_PROG_TYPE_XDP,
106118
};
119+
const char *optstr = "F";
120+
int prog_fd, map_fd, opt;
107121
struct bpf_object *obj;
108122
struct bpf_map *map;
109-
int prog_fd, map_fd;
110123
char filename[256];
111124
int ret, err, i;
112125
int numcpus;
113126

114-
if (argc < 2) {
115-
printf("Usage: %s <ifname>\n", argv[0]);
127+
while ((opt = getopt(argc, argv, optstr)) != -1) {
128+
switch (opt) {
129+
case 'F':
130+
xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
131+
break;
132+
default:
133+
usage(basename(argv[0]));
134+
return 1;
135+
}
136+
}
137+
138+
if (optind == argc) {
139+
usage(basename(argv[0]));
116140
return 1;
117141
}
118142

@@ -143,16 +167,16 @@ int main(int argc, char **argv)
143167
}
144168
map_fd = bpf_map__fd(map);
145169

146-
if_idx = if_nametoindex(argv[1]);
170+
if_idx = if_nametoindex(argv[optind]);
147171
if (!if_idx)
148-
if_idx = strtoul(argv[1], NULL, 0);
172+
if_idx = strtoul(argv[optind], NULL, 0);
149173

150174
if (!if_idx) {
151175
fprintf(stderr, "Invalid ifname\n");
152176
return 1;
153177
}
154-
if_name = argv[1];
155-
err = do_attach(if_idx, prog_fd, argv[1]);
178+
if_name = argv[optind];
179+
err = do_attach(if_idx, prog_fd, if_name);
156180
if (err)
157181
return err;
158182

0 commit comments

Comments
 (0)