On Tue, Dec 06, 2022 at 10:08:23AM +0100, Magnus Karlsson wrote: > From: Magnus Karlsson <magnus.karlsson@xxxxxxxxx> > > Add a new test where some of the packets are not passed to the AF_XDP > socket and instead get a XDP_DROP verdict. This is important as it > tests the recycling mechanism of the buffer pool. If a packet is not > sent to the AF_XDP socket, the buffer the packet resides in is instead > recycled so it can be used again without the round-trip to user > space. The test introduces a new XDP program that drops every other > packet. > > Signed-off-by: Magnus Karlsson <magnus.karlsson@xxxxxxxxx> > --- > tools/testing/selftests/bpf/Makefile | 2 +- > .../selftests/bpf/progs/xsk_xdp_drop.c | 25 ++++++++++ > tools/testing/selftests/bpf/xskxceiver.c | 48 +++++++++++++++++-- > tools/testing/selftests/bpf/xskxceiver.h | 3 ++ > 4 files changed, 72 insertions(+), 6 deletions(-) > create mode 100644 tools/testing/selftests/bpf/progs/xsk_xdp_drop.c > > diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile > index 42e15b5a34a7..77ef8a8e6db4 100644 > --- a/tools/testing/selftests/bpf/Makefile > +++ b/tools/testing/selftests/bpf/Makefile > @@ -240,7 +240,7 @@ $(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS) > $(OUTPUT)/test_maps: $(TESTING_HELPERS) > $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) > $(OUTPUT)/xsk.o: $(BPFOBJ) > -$(OUTPUT)/xskxceiver: $(OUTPUT)/xsk.o $(OUTPUT)/xsk_def_prog.skel.h > +$(OUTPUT)/xskxceiver: $(OUTPUT)/xsk.o $(OUTPUT)/xsk_def_prog.skel.h $(OUTPUT)/xsk_xdp_drop.skel.h > > BPFTOOL ?= $(DEFAULT_BPFTOOL) > $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \ > diff --git a/tools/testing/selftests/bpf/progs/xsk_xdp_drop.c b/tools/testing/selftests/bpf/progs/xsk_xdp_drop.c > new file mode 100644 > index 000000000000..12a12b0d9fc1 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/xsk_xdp_drop.c > @@ -0,0 +1,25 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2022 Intel */ > + > +#include <linux/bpf.h> > +#include <bpf/bpf_helpers.h> > + > +struct { > + __uint(type, BPF_MAP_TYPE_XSKMAP); > + __uint(max_entries, 1); > + __uint(key_size, sizeof(int)); > + __uint(value_size, sizeof(int)); > +} xsk SEC(".maps"); > + > +static unsigned int idx; > + > +SEC("xdp") int xsk_xdp_drop(struct xdp_md *xdp) > +{ > + /* Drop every other packet */ > + if (idx++ % 2) > + return XDP_DROP; > + > + return bpf_redirect_map(&xsk, 0, XDP_DROP); > +} > + > +char _license[] SEC("license") = "GPL"; > diff --git a/tools/testing/selftests/bpf/xskxceiver.c b/tools/testing/selftests/bpf/xskxceiver.c > index 0cda4e3f1871..522dc1d69c17 100644 > --- a/tools/testing/selftests/bpf/xskxceiver.c > +++ b/tools/testing/selftests/bpf/xskxceiver.c > @@ -1654,18 +1654,53 @@ static void testapp_invalid_desc(struct test_spec *test) > pkt_stream_restore_default(test); > } > > -static int xsk_load_xdp_program(struct ifobject *ifobj) > +static void testapp_xdp_drop(struct test_spec *test) > +{ > + struct ifobject *ifobj = test->ifobj_rx; > + int err; > + > + test_spec_set_name(test, "XDP_CONSUMES_SOME_PACKETS"); XDP_DROP_ODD_PACKETS ? > + xsk_detach_xdp_program(ifobj->ifindex, ifobj->xdp_flags); > + err = xsk_attach_xdp_program(ifobj->xdp_drop->progs.xsk_xdp_drop, ifobj->ifindex, > + ifobj->xdp_flags); > + if (err) { > + printf("Error attaching XDP_DROP program\n"); > + test->fail = true; > + return; > + } > + ifobj->xskmap = ifobj->xdp_drop->maps.xsk; > + > + pkt_stream_receive_half(test); > + testapp_validate_traffic(test); > + > + pkt_stream_restore_default(test); > + xsk_detach_xdp_program(ifobj->ifindex, ifobj->xdp_flags); > + err = xsk_attach_xdp_program(ifobj->def_prog->progs.xsk_def_prog, ifobj->ifindex, > + ifobj->xdp_flags); > + if (err) { > + printf("Error restoring default XDP program\n"); > + exit_with_error(-err); > + } > + ifobj->xskmap = ifobj->def_prog->maps.xsk; > +} > + > +static int xsk_load_xdp_programs(struct ifobject *ifobj) > { > ifobj->def_prog = xsk_def_prog__open_and_load(); > if (libbpf_get_error(ifobj->def_prog)) > return libbpf_get_error(ifobj->def_prog); > > + ifobj->xdp_drop = xsk_xdp_drop__open_and_load(); > + if (libbpf_get_error(ifobj->xdp_drop)) > + return libbpf_get_error(ifobj->xdp_drop); > + > return 0; > } > > -static void xsk_unload_xdp_program(struct ifobject *ifobj) > +static void xsk_unload_xdp_programs(struct ifobject *ifobj) > { > xsk_def_prog__destroy(ifobj->def_prog); > + xsk_xdp_drop__destroy(ifobj->xdp_drop); > } > > static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac, > @@ -1692,7 +1727,7 @@ static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char * > if (!load_xdp) > return; > > - err = xsk_load_xdp_program(ifobj); > + err = xsk_load_xdp_programs(ifobj); > if (err) { > printf("Error loading XDP program\n"); > exit_with_error(err); > @@ -1804,6 +1839,9 @@ static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_ > case TEST_TYPE_HEADROOM: > testapp_headroom(test); > break; > + case TEST_TYPE_XDP_CONSUMES_PACKETS: > + testapp_xdp_drop(test); > + break; > default: > break; > } > @@ -1971,8 +2009,8 @@ int main(int argc, char **argv) > > pkt_stream_delete(tx_pkt_stream_default); > pkt_stream_delete(rx_pkt_stream_default); > - xsk_unload_xdp_program(ifobj_tx); > - xsk_unload_xdp_program(ifobj_rx); > + xsk_unload_xdp_programs(ifobj_tx); > + xsk_unload_xdp_programs(ifobj_rx); > ifobject_delete(ifobj_tx); > ifobject_delete(ifobj_rx); > > diff --git a/tools/testing/selftests/bpf/xskxceiver.h b/tools/testing/selftests/bpf/xskxceiver.h > index eb6355bcc143..3483ac240b2e 100644 > --- a/tools/testing/selftests/bpf/xskxceiver.h > +++ b/tools/testing/selftests/bpf/xskxceiver.h > @@ -6,6 +6,7 @@ > #define XSKXCEIVER_H_ > > #include "xsk_def_prog.skel.h" > +#include "xsk_xdp_drop.skel.h" > > #ifndef SOL_XDP > #define SOL_XDP 283 > @@ -87,6 +88,7 @@ enum test_type { > TEST_TYPE_STATS_RX_FULL, > TEST_TYPE_STATS_FILL_EMPTY, > TEST_TYPE_BPF_RES, > + TEST_TYPE_XDP_CONSUMES_PACKETS, > TEST_TYPE_MAX > }; > > @@ -141,6 +143,7 @@ struct ifobject { > validation_func_t validation_func; > struct pkt_stream *pkt_stream; > struct xsk_def_prog *def_prog; > + struct xsk_xdp_drop *xdp_drop; Is this going to scale if we add plenty of XDP progs for testing? > struct bpf_map *xskmap; > int ifindex; > u32 dst_ip; > -- > 2.34.1 >