selftests/xsk: speed-ups, fixes, and new XDP programs
Subsystems (7)
v3 cover selftests/xsk: speed-ups, fixes, and new XDP programs
This is a patch set of various performance improvements, fixes, and the introduction of more than one XDP program to the xsk selftests framework so we can test more things in the future such as upcoming multi-buffer and metadata support for AF_XDP. The new programs just reuse the framework that all the other eBPF selftests use. The new feature is used to implement one new test that does XDP_DROP on every other packet. More tests using this will be added in future commits.
Contents:
* The run-time of the test suite is cut by 10x when executing the tests on a real NIC, by only attaching the XDP program once per mode tested, instead of once per test program.
* Over 700 lines of code have been removed. The xsk.c control file was moved straight over from libbpf when the xsk support was deprecated there. As it is now not used as library code that has to work with all kinds of versions of Linux, a lot of code could be dropped or simplified.
* Add a new command line option "-d" that can be used when a test fails and you want to debug it with gdb or some other debugger. The option creates the two veth netdevs and prints them to the screen without deleting them afterwards. This way these veth netdevs can be used when running xskxceiver in a debugger.
* Implemented the possibility to load external XDP programs so we can have more than the default one. This feature is used to implement a test where every other packet is dropped. Good exercise for the recycling mechanism of the xsk buffer pool used in zero-copy mode.
* Various clean-ups and small fixes in patches 1 to 5. None of these fixes has any impact on the correct execution of the tests when they pass, though they can be irritating when a test fails. IMHO, they do not need to go to bpf as they will not fix anything there. The first version of patches 1, 2, and 4 where previously sent to bpf, but has now been included here.
v2 -> v3:
* Fixed compilation error for llvm [David]
* Made the function xsk_is_in_drv_mode(ifobj) more generic by changing it to xsk_is_in_mode(ifobj, xdp_mode) [Maciej]
* Added Maciej's acks to all the patches
v1 -> v2:
* Fixed spelling error in commit message of patch #6 [Björn]
* Added explanation on why it is safe to use C11 atomics in patch #7 [Daniel]
* Put all XDP programs in the same file so that adding more XDP programs to xskxceiver.c becomes more scalable in patches #11 and #12 [Maciej]
* Removed more dead code in patch #8 [Maciej]
* Removed stale %s specifier in error print, patch #9 [Maciej]
* Changed name of XDP_CONSUMES_SOME_PACKETS to XDP_DROP_HALF to hopefully make it clearer [Maciej]
* ifobj_rx and ifobj_tx name changes in patch #13 [Maciej]
* Simplified XDP attachment code in patch #15 [Maciej]
Patches:
1-5: Small fixes and clean-ups 6: New convenient debug option when using a debugger such as gdb 7-8: Removal of unnecessary code 9: Add the ability to load external XDP programs
10-11: Removal of more unnecessary code
12: Implement a new test where every other packet is XDP_DROP:ed
13: Unify the thread dispatching code
14-15: Simplify the way tests are written when using custom packet_streams
or custom XDP programs
Thanks: Magnus
Magnus Karlsson (15):
selftests/xsk: print correct payload for packet dump selftests/xsk: do not close unused file descriptors selftests/xsk: submit correct number of frames in populate_fill_ring selftests/xsk: print correct error codes when exiting selftests/xsk: remove unused variable outstanding_tx selftests/xsk: add debug option for creating netdevs selftests/xsk: replace asm acquire/release implementations selftests/xsk: remove namespaces
selftests/xsk: load and attach XDP program only once per mode selftests/xsk: remove unnecessary code in control path selftests/xsk: get rid of built-in XDP program selftests/xsk: add test when some packets are XDP_DROPed selftests/xsk: merge dual and single thread dispatchers selftests/xsk: automatically restore packet stream selftests/xsk: automatically switch XDP programs
tools/testing/selftests/bpf/Makefile | 6 +- .../selftests/bpf/progs/xsk_xdp_progs.c | 30 + tools/testing/selftests/bpf/test_xsk.sh | 42 +- tools/testing/selftests/bpf/xsk.c | 677 +----------------- tools/testing/selftests/bpf/xsk.h | 97 +-- tools/testing/selftests/bpf/xsk_prereqs.sh | 12 +- tools/testing/selftests/bpf/xskxceiver.c | 382 +++++----- tools/testing/selftests/bpf/xskxceiver.h | 17 +- 8 files changed, 316 insertions(+), 947 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/xsk_xdp_progs.c
base-commit: 6920b08661e3ad829206078b5c9879b24aea8dfc --
2.34.1
PATCH v3 1/15 selftests/xsk: print correct payload for packet dump
From: Magnus Karlsson <[email protected]>
Print the correct payload when the packet dump option is selected. The network to host conversion was forgotten and the payload was erronously declared to be an int instead of an unsigned int.
--- tools/testing/selftests/bpf/xskxceiver.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +2 -2
@@ -767,7 +767,7 @@ static void pkt_dump(void *pkt, u32 len)struct ethhdr *ethhdr;struct udphdr *udphdr;struct iphdr *iphdr; -int payload, i; +u32 payload, i; ethhdr = pkt; iphdr = pkt +sizeof (*ethhdr);
@@ -792,7 +792,7 @@ static void pkt_dump(void *pkt, u32 len)fprintf (stdout,"DEBUG>> L4: udp_hdr->src: %d\n" ,ntohs (udphdr->source));fprintf (stdout,"DEBUG>> L4: udp_hdr->dst: %d\n" ,ntohs (udphdr->dest));/*extract L5 frame */ - payload = *((uint32_t *)(pkt +PKT_HDR_SIZE )); + payload =ntohl (*((u32 *)(pkt +PKT_HDR_SIZE )));fprintf (stdout,"DEBUG>> L5: payload: %d\n" , payload);fprintf (stdout,"---------------------------------------\n" );
Signature · 1 line
2.34.1
PATCH v3 2/15 selftests/xsk: do not close unused file descriptors
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Do not close descriptors that have never been used. File descriptor fields that are not in use are erroneously marked with the number 0, which is a valid fd. Mark unused fds with -1 instead and do not close these when deleting the socket.
--- tools/testing/selftests/bpf/xsk.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/xsk.c +11 -2
@@ -35,6 +35,8 @@#include "xsk.h" #include "bpf_util.h" +#define FD_NOT_USED (-1) +#ifndef SOL_XDP #define SOL_XDP 283#endif
@@ -583,6 +585,9 @@ static void xsk_delete_bpf_maps(struct xsk_socket *xsk) {struct xsk_ctx *ctx = xsk->ctx; +if (ctx->xsks_map_fd ==FD_NOT_USED ) +return ; +bpf_map_delete_elem (ctx->xsks_map_fd, &ctx->queue_id);close (ctx->xsks_map_fd); }
@@ -941,6 +946,9 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk, ctx->umem = umem; ctx->queue_id = queue_id;bpf_strlcpy (ctx->ifname, ifname,IFNAMSIZ ); + ctx->prog_fd =FD_NOT_USED ; + ctx->link_fd =FD_NOT_USED ; + ctx->xsks_map_fd =FD_NOT_USED ; ctx->fill = fill; ctx->comp = comp;
@@ -1221,8 +1229,9 @@ void xsk_socket__delete(struct xsk_socket *xsk)if (ctx->refcount ==1 ) {xsk_delete_bpf_maps (xsk); -close (ctx->prog_fd); -if (ctx->has_bpf_link) +if (ctx->prog_fd !=FD_NOT_USED ) +close (ctx->prog_fd); +if (ctx->has_bpf_link && ctx->link_fd !=FD_NOT_USED )close (ctx->link_fd); }
Signature · 1 line
2.34.1
PATCH v3 3/15 selftests/xsk: submit correct number of frames in populate_fill_ring
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Submit the correct number of frames in the function xsk_populate_fill_ring(). For the tests that set the flag use_addr_for_fill, uninitialized buffers were sent to the fill ring following the correct ones. This has no impact on the tests, since they only use the ones that were initialized. But for correctness, this should be fixed.
--- tools/testing/selftests/bpf/xskxceiver.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/xskxceiver.c +1 -1
@@ -1272,7 +1272,7 @@ static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *xsk_ring_prod__fill_addr (&umem->fq, idx++) = addr; } -xsk_ring_prod__submit (&umem->fq, buffers_to_fill); +xsk_ring_prod__submit (&umem->fq, i); }static void thread_common_ops (struct test_spec *test,struct ifobject *ifobject)
Signature · 1 line
2.34.1
PATCH v3 4/15 selftests/xsk: print correct error codes when exiting
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Print the correct error codes when exiting the test suite due to some terminal error. Some of these had a switched sign and some of them printed zero instead of errno.
--- tools/testing/selftests/bpf/xskxceiver.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +9 -9
@@ -350,7 +350,7 @@ static bool ifobj_zc_avail(struct ifobject *ifobject) umem =calloc (1 ,sizeof (struct xsk_umem_info ));if (!umem) {munmap (bufs, umem_sz); -exit_with_error (-ENOMEM ); +exit_with_error (ENOMEM ); } umem->frame_size =XSK_UMEM__DEFAULT_FRAME_SIZE ; ret =xsk_configure_umem (umem, bufs, umem_sz);
@@ -936,7 +936,7 @@ static int receive_pkts(struct test_spec *test, struct pollfd *fds)if (ifobj->use_poll) { ret =poll (fds,1 ,POLL_TMOUT );if (ret <0 ) -exit_with_error (-ret); +exit_with_error (errno);if (!ret) {if (!is_umem_valid (test->ifobj_tx))
@@ -963,7 +963,7 @@ static int receive_pkts(struct test_spec *test, struct pollfd *fds)if (xsk_ring_prod__needs_wakeup (&umem->fq)) { ret =poll (fds,1 ,POLL_TMOUT );if (ret <0 ) -exit_with_error (-ret); +exit_with_error (errno); } ret =xsk_ring_prod__reserve (&umem->fq, rcvd, &idx_fq); }
@@ -1015,7 +1015,7 @@ static int __send_pkts(struct ifobject *ifobject, u32 *pkt_nb, struct pollfd *fdif (timeout) {if (ret <0 ) {ksft_print_msg ("ERROR: [%s] Poll error %d\n" , - __func__, ret); + __func__, errno);return TEST_FAILURE ; }if (ret ==0 )
@@ -1024,7 +1024,7 @@ static int __send_pkts(struct ifobject *ifobject, u32 *pkt_nb, struct pollfd *fd }if (ret <=0 ) {ksft_print_msg ("ERROR: [%s] Poll error %d\n" , - __func__, ret); + __func__, errno);return TEST_FAILURE ; } }
@@ -1323,18 +1323,18 @@ static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)if (ifobject->xdp_flags &XDP_FLAGS_SKB_MODE ) {if (opts.attach_mode !=XDP_ATTACHED_SKB ) {ksft_print_msg ("ERROR: [%s] XDP prog not in SKB mode\n" ); -exit_with_error (-EINVAL ); +exit_with_error (EINVAL ); } }else if (ifobject->xdp_flags &XDP_FLAGS_DRV_MODE ) {if (opts.attach_mode !=XDP_ATTACHED_DRV ) {ksft_print_msg ("ERROR: [%s] XDP prog not in DRV mode\n" ); -exit_with_error (-EINVAL ); +exit_with_error (EINVAL ); } } ret =xsk_socket__update_xskmap (ifobject->xsk->xsk, ifobject->xsk_map_fd);if (ret) -exit_with_error (-ret); +exit_with_error (errno); }static void *worker_testapp_validate_tx (void *arg)
@@ -1541,7 +1541,7 @@ static void swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj ret =xsk_socket__update_xskmap (ifobj_rx->xsk->xsk, ifobj_rx->xsk_map_fd);if (ret) -exit_with_error (-ret); +exit_with_error (errno); }static void testapp_bpf_res (struct test_spec *test)
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 04/15] selftests/xsk: print correct error codes when exiting
Quoted 2 lines · Magnus Karlsson
(quoted diff) +1
-1- exit_with_error(-ret); + exit_with_error(errno);
...
tools/testing/selftests/bpf/xskxceiver.c static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject) · line 1326
Quoted 6 lines
tools/testing/selftests/bpf/xskxceiver.c +1
-1@@ -1323,18 +1323,18 @@ static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)if (ifobject->xdp_flags &XDP_FLAGS_SKB_MODE ) {if (opts.attach_mode !=XDP_ATTACHED_SKB ) {ksft_print_msg ("ERROR: [%s] XDP prog not in SKB mode\n" ); -exit_with_error (-EINVAL ); +exit_with_error (EINVAL );
My understanding is that you want exit_with_error() to always see a positive error, right? Have you considered doing something like:
#define exit_with_error(error) ({\
if (__builtin_constant_p(error) && error < 0) // build error; __exit_with_error(error, __FILE__, __func__, __LINE__);
})
would it help to catch some of these issues?
Re: [PATCH bpf-next v3 04/15] selftests/xsk: print correct error codes when exiting
Quoted 5 lines · Alexei Starovoitov
Quoted 2 lines · Magnus Karlsson
(quoted diff) +1
-1- exit_with_error(-ret); + exit_with_error(errno);...
Quoted 14 lines
tools/testing/selftests/bpf/xskxceiver.c static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject) · line 1326
Quoted 6 lines
tools/testing/selftests/bpf/xskxceiver.c +1
-1@@ -1323,18 +1323,18 @@ static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)if (ifobject->xdp_flags &XDP_FLAGS_SKB_MODE ) {if (opts.attach_mode !=XDP_ATTACHED_SKB ) {ksft_print_msg ("ERROR: [%s] XDP prog not in SKB mode\n" ); -exit_with_error (-EINVAL ); +exit_with_error (EINVAL );My understanding is that you want
exit_with_error()to always see a positive error, right? Have you considered doing something like:
#defineexit_with_error(error)({\
if (__builtin_constant_p(error)&& error < 0) // build error;__exit_with_error(error, __FILE__, __func__, __LINE__);
})
would it help to catch some of these issues?
Yes it would. Will add this to the next series. Thanks!
PATCH v3 5/15 selftests/xsk: remove unused variable outstanding_tx
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Remove the unused variable outstanding_tx.
--- tools/testing/selftests/bpf/xsk.c | 2 -- 1 file changed, 2 deletions(-)
tools/testing/selftests/bpf/xsk.c +0 -2
@@ -86,7 +86,6 @@ struct xsk_ctx {struct xsk_socket {struct xsk_ring_cons *rx;struct xsk_ring_prod *tx; -__u64 outstanding_tx;struct xsk_ctx *ctx;struct xsk_socket_config config;int fd;
@@ -1021,7 +1020,6 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,if (err) goto out_xsk_alloc; - xsk->outstanding_tx =0 ; ifindex =if_nametoindex (ifname);if (!ifindex) { err = -errno;
Signature · 1 line
2.34.1
PATCH v3 6/15 selftests/xsk: add debug option for creating netdevs
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Add a new option to the test_xsk.sh script that only creates the two veth netdevs and the extra namespace, then exits without running any tests. The failed test can then be executed in the debugger without having to create the netdevs and namespace manually. For ease-of-use, the veth netdevs to use are printed so they can be copied into the debugger.
Here is an example how to use it:
Quoted 1 line
sudo ./test_xsk.sh -d
veth10 veth11
Quoted 1 line
gdb xskxceiver
In gdb:
run -i veth10 -i veth11
And now the test cases can be debugged with gdb.
If you want to debug the test suite on a real NIC in loopback mode, there is no need to use this feature as you already know the netdev of your NIC.
--- tools/testing/selftests/bpf/test_xsk.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/test_xsk.sh +10 -1
@@ -74,6 +74,9 @@ # Run and dump packet contents: # sudo ./test_xsk.sh -D # +# Set up veth interfaces and leave them up so xskxceiver can be launched in a debugger: +# sudo ./test_xsk.sh -d +# # Run test suite for physical device in loopback mode # sudo ./test_xsk.sh -i IFACE
@@ -81,11 +84,12 @@ ETH="" -while getopts "vDi:" flag +while getopts "vDi:d" flag do case "${flag}" in v) verbose=1;; D) dump_pkts=1;; + d) debug=1;; i) ETH=${OPTARG};; esac done
@@ -174,6 +178,11 @@ statusList=() TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ" +if [[ $debug -eq 1 ]]; then + echo "-i" ${VETH0} "-i" ${VETH1},${NS1} + exit +fi + exec_xskxceiver if [ -z $ETH ]; then
Signature · 1 line
2.34.1
PATCH v3 7/15 selftests/xsk: replace asm acquire/release implementations
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Replace our own homegrown assembly store/release and load/acquire implementations with the HW agnositic atomic APIs C11 offers. This to make the code more portable, easier to read, and reduce the maintenance burden.
The original code used load-acquire and store-release barriers hand-coded in assembly. Since C11, these kind of operations are offered as built-ins in gcc and llvm. The load-acquire operation prevents hoisting of non-atomic memory operations to before this operation and it corresponds to the __ATOMIC_ACQUIRE operation in the built-in atomics. The store-release operation prevents hoisting of non-atomic memory operations to after this operation and it corresponds to the __ATOMIC_RELEASE operation in the built-in atomics.
--- tools/testing/selftests/bpf/xsk.h | 80 ++----------------------------- 1 file changed, 4 insertions(+), 76 deletions(-)
tools/testing/selftests/bpf/xsk.h +4 -76
@@ -23,77 +23,6 @@ extern "C" { #endif -/* This whole API has been deprecated and moved to libxdp that can be found at - * https://github.com/xdp-project/xdp-tools. The APIs are exactly the same so - * it should just be linking with libxdp instead of libbpf for this set of - * functionality. If not, please submit a bug report on the aforementioned page. - */ - -/* Load-Acquire Store-Release barriers used by the XDP socket - * library. The following macros should *NOT* be considered part of - * the xsk.h API, and is subject to change anytime. - * - * LIBRARY INTERNAL - */ - -#define __XSK_READ_ONCE(x) (*(volatile typeof(x) *)&x) -#define __XSK_WRITE_ONCE(x, v) (*(volatile typeof(x) *)&x) = (v) - -#if defined(__i386__) || defined(__x86_64__) -# define libbpf_smp_store_release(p, v) \ - do { \ - asm volatile("" : : : "memory"); \ - __XSK_WRITE_ONCE(*p, v); \ - } while (0) -# define libbpf_smp_load_acquire(p) \ - ({ \ - typeof(*p) ___p1 = __XSK_READ_ONCE(*p); \ - asm volatile("" : : : "memory"); \ - ___p1; \ - }) -#elif defined(__aarch64__) -# define libbpf_smp_store_release(p, v) \ - asm volatile ("stlr %w1, %0" : "=Q" (*p) : "r" (v) : "memory") -# define libbpf_smp_load_acquire(p) \ - ({ \ - typeof(*p) ___p1; \ - asm volatile ("ldar %w0, %1" \ - : "=r" (___p1) : "Q" (*p) : "memory"); \ - ___p1; \ - }) -#elif defined(__riscv) -# define libbpf_smp_store_release(p, v) \ - do { \ - asm volatile ("fence rw,w" : : : "memory"); \ - __XSK_WRITE_ONCE(*p, v); \ - } while (0) -# define libbpf_smp_load_acquire(p) \ - ({ \ - typeof(*p) ___p1 = __XSK_READ_ONCE(*p); \ - asm volatile ("fence r,rw" : : : "memory"); \ - ___p1; \ - }) -#endif - -#ifndef libbpf_smp_store_release -#define libbpf_smp_store_release(p, v) \ - do { \ - __sync_synchronize(); \ - __XSK_WRITE_ONCE(*p, v); \ - } while (0) -#endif - -#ifndef libbpf_smp_load_acquire -#define libbpf_smp_load_acquire(p) \ - ({ \ - typeof(*p) ___p1 = __XSK_READ_ONCE(*p); \ - __sync_synchronize(); \ - ___p1; \ - }) -#endif - -/* LIBRARY INTERNAL -- END */ - /* Do not access these members directly. Use the functions below. */ #define DEFINE_XSK_RING(name) \ struct name { \
@@ -168,7 +97,7 @@ static inline __u32 xsk_prod_nb_free(struct xsk_ring_prod *r, __u32 nb)* this function. Without this optimization it whould have been * free_entries = r->cached_prod - r->cached_cons + r->size. */ - r->cached_cons =libbpf_smp_load_acquire (r->consumer); + r->cached_cons =__atomic_load_n (r->consumer, __ATOMIC_ACQUIRE); r->cached_cons += r->size;return r->cached_cons - r->cached_prod;
@@ -179,7 +108,7 @@ static inline __u32 xsk_cons_nb_avail(struct xsk_ring_cons *r, __u32 nb)__u32 entries = r->cached_prod - r->cached_cons;if (entries ==0 ) { - r->cached_prod =libbpf_smp_load_acquire (r->producer); + r->cached_prod =__atomic_load_n (r->producer, __ATOMIC_ACQUIRE); entries = r->cached_prod - r->cached_cons; }
@@ -202,7 +131,7 @@ static inline void xsk_ring_prod__submit(struct xsk_ring_prod *prod, __u32 nb)/* Make sure everything has been written to the ring before indicating * this to the kernel by writing the producer pointer. */ -libbpf_smp_store_release (prod->producer, *prod->producer + nb); +__atomic_store_n (prod->producer, *prod->producer + nb, __ATOMIC_RELEASE); }static inline __u32 xsk_ring_cons__peek (struct xsk_ring_cons *cons,__u32 nb,__u32 *idx)
@@ -227,8 +156,7 @@ static inline void xsk_ring_cons__release(struct xsk_ring_cons *cons, __u32 nb)/* Make sure data has been read before indicating we are done * with the entries by updating the consumer pointer. */ -libbpf_smp_store_release (cons->consumer, *cons->consumer + nb); - +__atomic_store_n (cons->consumer, *cons->consumer + nb, __ATOMIC_RELEASE); }static inline void *xsk_umem__get_data (void *umem_area,__u64 addr)
Signature · 1 line
2.34.1
PATCH v3 8/15 selftests/xsk: remove namespaces
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Remove the namespaces used as they fill no function. This will simplify the code for speeding up the tests in the following commits.
--- tools/testing/selftests/bpf/test_xsk.sh | 33 ++++++---------- tools/testing/selftests/bpf/xsk_prereqs.sh | 12 +----- tools/testing/selftests/bpf/xskxceiver.c | 46 +++------------------- tools/testing/selftests/bpf/xskxceiver.h | 3 -- 4 files changed, 20 insertions(+), 74 deletions(-)
tools/testing/selftests/bpf/test_xsk.sh +12 -21
@@ -24,8 +24,6 @@ # ----------- | ---------- # | vethX | --------- | vethY | # ----------- peer ---------- -# | | | -# namespaceX | namespaceY # # AF_XDP is an address family optimized for high performance packet processing, # it is XDP’s user-space interface.
@@ -39,10 +37,9 @@ # Prerequisites setup by script: # # Set up veth interfaces as per the topology shown ^^: -# * setup two veth interfaces and one namespace -# ** veth<xxxx> in root namespace -# ** veth<yyyy> in af_xdp<xxxx> namespace -# ** namespace af_xdp<xxxx> +# * setup two veth interfaces +# ** veth<xxxx> +# ** veth<yyyy> # *** xxxx and yyyy are randomly generated 4 digit numbers used to avoid # conflict with any existing interface # * tests the veth and xsk layers of the topology
@@ -103,28 +100,25 @@ VETH0_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head - VETH0=ve${VETH0_POSTFIX} VETH1_POSTFIX=$(cat ${URANDOM} | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 4) VETH1=ve${VETH1_POSTFIX} -NS0=root -NS1=af_xdp${VETH1_POSTFIX} MTU=1500 trap ctrl_c INT function ctrl_c() { - cleanup_exit ${VETH0} ${VETH1} ${NS1} + cleanup_exit ${VETH0} ${VETH1} exit 1 } setup_vethPairs() { if [[ $verbose -eq 1 ]]; then - echo "setting up ${VETH0}: namespace: ${NS0}" + echo "setting up ${VETH0}" fi - ip netns add ${NS1} ip link add ${VETH0} numtxqueues 4 numrxqueues 4 type veth peer name ${VETH1} numtxqueues 4 numrxqueues 4 if [ -f /proc/net/if_inet6 ]; then echo 1 > /proc/sys/net/ipv6/conf/${VETH0}/disable_ipv6 fi if [[ $verbose -eq 1 ]]; then - echo "setting up ${VETH1}: namespace: ${NS1}" + echo "setting up ${VETH1}" fi if [[ $busy_poll -eq 1 ]]; then
@@ -134,18 +128,15 @@ setup_vethPairs() { echo 200000 > /sys/class/net/${VETH1}/gro_flush_timeout fi - ip link set ${VETH1} netns ${NS1} - ip netns exec ${NS1} ip link set ${VETH1} mtu ${MTU} + ip link set ${VETH1} mtu ${MTU} ip link set ${VETH0} mtu ${MTU} - ip netns exec ${NS1} ip link set ${VETH1} up - ip netns exec ${NS1} ip link set dev lo up + ip link set ${VETH1} up ip link set ${VETH0} up } if [ ! -z $ETH ]; then VETH0=${ETH} VETH1=${ETH} - NS1="" else validate_root_exec validate_veth_support ${VETH0}
@@ -155,7 +146,7 @@ else retval=$? if [ $retval -ne 0 ]; then test_status $retval "${TEST_NAME}" - cleanup_exit ${VETH0} ${VETH1} ${NS1} + cleanup_exit ${VETH0} ${VETH1} exit $retval fi fi
@@ -179,14 +170,14 @@ statusList=() TEST_NAME="XSK_SELFTESTS_${VETH0}_SOFTIRQ" if [[ $debug -eq 1 ]]; then - echo "-i" ${VETH0} "-i" ${VETH1},${NS1} + echo "-i" ${VETH0} "-i" ${VETH1} exit fi exec_xskxceiver if [ -z $ETH ]; then - cleanup_exit ${VETH0} ${VETH1} ${NS1} + cleanup_exit ${VETH0} ${VETH1} fi TEST_NAME="XSK_SELFTESTS_${VETH0}_BUSY_POLL" busy_poll=1
@@ -199,7 +190,7 @@ exec_xskxceiver ## END TESTS if [ -z $ETH ]; then - cleanup_exit ${VETH0} ${VETH1} ${NS1} + cleanup_exit ${VETH0} ${VETH1} fi failures=0
tools/testing/selftests/bpf/xsk_prereqs.sh +2 -10
@@ -55,21 +55,13 @@ test_exit() clear_configs() { - if [ $(ip netns show | grep $3 &>/dev/null; echo $?;) == 0 ]; then - [ $(ip netns exec $3 ip link show $2 &>/dev/null; echo $?;) == 0 ] && - { ip netns exec $3 ip link del $2; } - ip netns del $3 - fi - #Once we delete a veth pair node, the entire veth pair is removed, - #this is just to be cautious just incase the NS does not exist then - #veth node inside NS won't get removed so we explicitly remove it [ $(ip link show $1 &>/dev/null; echo $?;) == 0 ] && { ip link del $1; } } cleanup_exit() { - clear_configs $1 $2 $3 + clear_configs $1 $2 } validate_ip_utility()
@@ -83,7 +75,7 @@ exec_xskxceiver() ARGS+="-b " fi - ./${XSKOBJ} -i ${VETH0} -i ${VETH1},${NS1} ${ARGS} + ./${XSKOBJ} -i ${VETH0} -i ${VETH1} ${ARGS} retval=$? test_status $retval "${TEST_NAME}"
tools/testing/selftests/bpf/xskxceiver.c +6 -40
89 lines not on this page · raw message
tools/testing/selftests/bpf/xskxceiver.h +0 -3
@@ -30,7 +30,6 @@#define TEST_CONTINUE 1#define MAX_INTERFACES 2#define MAX_INTERFACE_NAME_CHARS 16 -#define MAX_INTERFACES_NAMESPACE_CHARS 16#define MAX_SOCKETS 2#define MAX_TEST_NAME_SIZE 32#define MAX_TEARDOWN_ITER 10
@@ -133,14 +132,12 @@ typedef void *(*thread_func_t)(void *arg);struct ifobject {char ifname[MAX_INTERFACE_NAME_CHARS ]; -char nsname[MAX_INTERFACES_NAMESPACE_CHARS ];struct xsk_socket_info *xsk;struct xsk_socket_info *xsk_arr;struct xsk_umem_info *umem;thread_func_t func_ptr;validation_func_t validation_func;struct pkt_stream *pkt_stream; -int ns_fd;int xsk_map_fd;u32 dst_ip;u32 src_ip;
Signature · 1 line
2.34.1
PATCH v3 9/15 selftests/xsk: load and attach XDP program only once per mode
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Load and attach the XDP program only once per XDP mode that is being executed. Today, the XDP program is loaded and attached for every test, then unloaded, which takes a long time on real NICs, since they have to reconfigure their HW, in contrast to veth. The test suite now completes in 21 seconds, instead of 207 seconds previously on my machine. This is a speed-up of around 10x.
This is accomplished by moving the XDP loading from the worker threads to the main thread and replacing the XDP loading interfaces of xsk.c that was taken from the xsk support in libbpf, with something more explicit that is more useful for these tests. Instead, the relevant file descriptors and ifindexes are just passed down to the new functions.
--- tools/testing/selftests/bpf/xsk.c | 92 +++++++++----- tools/testing/selftests/bpf/xsk.h | 7 +- tools/testing/selftests/bpf/xskxceiver.c | 147 ++++++++++++++--------- tools/testing/selftests/bpf/xskxceiver.h | 3 + 4 files changed, 162 insertions(+), 87 deletions(-)
tools/testing/selftests/bpf/xsk.c +61 -31
@@ -51,6 +51,8 @@#define pr_warn (fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__) +#define XSKMAP_SIZE 1 +enum xsk_prog {XSK_PROG_FALLBACK ,XSK_PROG_REDIRECT_FLAGS ,
@@ -387,10 +389,9 @@ static enum xsk_prog get_xsk_prog(void)return detected; } -static int xsk_load_xdp_prog (struct xsk_socket *xsk) +static int __xsk_load_xdp_prog (int xsk_map_fd) {static const int log_buf_size =16 *1024 ; -struct xsk_ctx *ctx = xsk->ctx;char log_buf[log_buf_size];int prog_fd;
@@ -418,7 +419,7 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk)/* *(u32 *)(r10 - 4) = r2 */ BPF_STX_MEM (BPF_W ,BPF_REG_10 ,BPF_REG_2 ,-4 ),/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , ctx->xsks_map_fd), +BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd),/* r3 = XDP_PASS */ BPF_MOV64_IMM(BPF_REG_3, 2),/* call bpf_redirect_map */
@@ -430,7 +431,7 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk)/* r2 += -4 */ BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-4 ),/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , ctx->xsks_map_fd), +BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd),/* call bpf_map_lookup_elem */ BPF_EMIT_CALL(BPF_FUNC_map_lookup_elem),/* r1 = r0 */
@@ -442,7 +443,7 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk)/* r2 = *(u32 *)(r10 - 4) */ BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_10 ,-4 ),/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , ctx->xsks_map_fd), +BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd),/* r3 = 0 */ BPF_MOV64_IMM(BPF_REG_3, 0),/* call bpf_redirect_map */
@@ -461,7 +462,7 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk)/* r2 = *(u32 *)(r1 + 16) */ BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 ,16 ),/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , ctx->xsks_map_fd), +BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd),/* r3 = XDP_PASS */ BPF_MOV64_IMM(BPF_REG_3, 2),/* call bpf_redirect_map */
@@ -480,13 +481,40 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk) prog_fd =bpf_prog_load (BPF_PROG_TYPE_XDP ,NULL ,"LGPL-2.1 or BSD-2-Clause" , progs[option], insns_cnt[option], &opts); -if (prog_fd <0 ) { +if (prog_fd <0 )pr_warn ("BPF log buffer:\n%s" , log_buf); -return prog_fd; + +return prog_fd; +} + +int xsk_attach_xdp_program (int ifindex,int prog_fd,u32 xdp_flags) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +__u32 prog_id =0 ; +int link_fd; +int err; + + err =bpf_xdp_query_id (ifindex, xdp_flags, &prog_id); +if (err) { +pr_warn ("getting XDP prog id failed\n" ); +return err; } - ctx->prog_fd = prog_fd; -return 0 ; +/* If there's a netlink-based XDP prog loaded on interface, bail out +* and ask user to do the removal by himself +*/ +if (prog_id) { +pr_warn ("Netlink-based XDP prog detected, please unload it in order to launch AF_XDP prog\n" ); +return -EINVAL ; + } + + opts.flags = xdp_flags & ~(XDP_FLAGS_UPDATE_IF_NOEXIST |XDP_FLAGS_REPLACE ); + + link_fd =bpf_link_create (prog_fd, ifindex,BPF_XDP , &opts); +if (link_fd <0 ) +pr_warn ("bpf_link_create failed: %s\n" ,strerror (errno)); + +return link_fd; }static int xsk_create_bpf_link (struct xsk_socket *xsk)
@@ -775,7 +803,7 @@ static int xsk_init_xdp_res(struct xsk_socket *xsk,if (err)return err; - err =xsk_load_xdp_prog (xsk); + err =__xsk_load_xdp_prog (*xsks_map_fd);if (err) goto err_load_xdp_prog;
@@ -871,6 +899,22 @@ int xsk_setup_xdp_prog_xsk(struct xsk_socket *xsk, int *xsks_map_fd)return __xsk_setup_xdp_prog (xsk, xsks_map_fd); } +int xsk_load_xdp_program (int *xsk_map_fd,int *prog_fd) +{ + *xsk_map_fd =bpf_map_create (BPF_MAP_TYPE_XSKMAP ,"xsks_map" ,sizeof (int ),sizeof (int ), +XSKMAP_SIZE ,NULL ); +if (*xsk_map_fd <0 ) +return *xsk_map_fd; + + *prog_fd =__xsk_load_xdp_prog (*xsk_map_fd); +if (*prog_fd <0 ) { +close (*xsk_map_fd); +return *prog_fd; + } + +return 0 ; +} +static struct xsk_ctx *xsk_get_ctx (struct xsk_umem *umem,int ifindex,__u32 queue_id) {
@@ -917,7 +961,7 @@ static void xsk_put_ctx(struct xsk_ctx *ctx, bool unmap)static struct xsk_ctx *xsk_create_ctx (struct xsk_socket *xsk,struct xsk_umem *umem,int ifindex, -const char *ifname,__u32 queue_id, +__u32 queue_id,struct xsk_ring_prod *fill,struct xsk_ring_cons *comp) {
@@ -944,7 +988,6 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk, ctx->refcount =1 ; ctx->umem = umem; ctx->queue_id = queue_id; -bpf_strlcpy (ctx->ifname, ifname,IFNAMSIZ ); ctx->prog_fd =FD_NOT_USED ; ctx->link_fd =FD_NOT_USED ; ctx->xsks_map_fd =FD_NOT_USED ;
@@ -991,7 +1034,7 @@ int xsk_setup_xdp_prog(int ifindex, int *xsks_map_fd) } int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, - const char *ifname, + int ifindex, __u32 queue_id, struct xsk_umem *umem, struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
@@ -1005,7 +1048,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,struct xdp_mmap_offsets off;struct xsk_socket *xsk;struct xsk_ctx *ctx; -int err, ifindex; +int err;if (!umem || !xsk_ptr || !(rx || tx))return -EFAULT ;
@@ -1020,12 +1063,6 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,if (err) goto out_xsk_alloc; - ifindex =if_nametoindex (ifname); -if (!ifindex) { - err = -errno; - goto out_xsk_alloc; - } -if (umem->refcount++ >0 ) { xsk->fd =socket (AF_XDP ,SOCK_RAW |SOCK_CLOEXEC ,0 );if (xsk->fd <0 ) {
@@ -1045,8 +1082,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, goto out_socket; } - ctx =xsk_create_ctx (xsk, umem, ifindex, ifname, queue_id, - fill, comp); + ctx =xsk_create_ctx (xsk, umem, ifindex, queue_id, fill, comp);if (!ctx) { err = -ENOMEM ; goto out_socket;
@@ -1144,12 +1180,6 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, goto out_mmap_tx; } -if (!(xsk->config.libbpf_flags &XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD )) { - err =__xsk_setup_xdp_prog (xsk,NULL ); -if (err) - goto out_mmap_tx; - } - *xsk_ptr = xsk; umem->fill_save =NULL ; umem->comp_save =NULL ;
@@ -1173,7 +1203,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,return err; } -int xsk_socket__create (struct xsk_socket **xsk_ptr,const char *ifname, +int xsk_socket__create (struct xsk_socket **xsk_ptr,int ifindex,__u32 queue_id,struct xsk_umem *umem,struct xsk_ring_cons *rx,struct xsk_ring_prod *tx,const struct xsk_socket_config *usr_config)
@@ -1181,7 +1211,7 @@ int xsk_socket__create(struct xsk_socket **xsk_ptr, const char *ifname,if (!umem)return -EFAULT ; -return xsk_socket__create_shared (xsk_ptr, ifname, queue_id, umem, +return xsk_socket__create_shared (xsk_ptr, ifindex, queue_id, umem, rx, tx, umem->fill_save, umem->comp_save, usr_config); }
tools/testing/selftests/bpf/xsk.h +5 -2
@@ -204,6 +204,9 @@ int xsk_socket__update_xskmap(struct xsk_socket *xsk, int xsks_map_fd);/* Flags for the libbpf_flags field. */ #define XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD (1 << 0) +int xsk_load_xdp_program (int *xsk_map_fd,int *prog_fd); +int xsk_attach_xdp_program (int ifindex,int prog_fd,u32 xdp_flags); +struct xsk_socket_config {__u32 rx_size;__u32 tx_size;
@@ -219,13 +222,13 @@ int xsk_umem__create(struct xsk_umem **umem,struct xsk_ring_cons *comp, const struct xsk_umem_config *config);int xsk_socket__create (struct xsk_socket **xsk, -const char *ifname,__u32 queue_id, +int ifindex,__u32 queue_id,struct xsk_umem *umem,struct xsk_ring_cons *rx,struct xsk_ring_prod *tx,const struct xsk_socket_config *config); int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, - const char *ifname, + int ifindex, __u32 queue_id, struct xsk_umem *umem, struct xsk_ring_cons *rx, struct xsk_ring_prod *tx,
tools/testing/selftests/bpf/xskxceiver.c +93 -54
292 lines not on this page · raw message
tools/testing/selftests/bpf/xskxceiver.h +3 -0
@@ -139,6 +139,9 @@ struct ifobject {validation_func_t validation_func;struct pkt_stream *pkt_stream;int xsk_map_fd; +int prog_fd; +int link_fd; +int ifindex;u32 dst_ip;u32 src_ip;u32 xdp_flags;
Signature · 1 line
2.34.1
PATCH v3 10/15 selftests/xsk: remove unnecessary code in control path
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Remove unnecessary code in the control path. This is located in the file xsk.c that was moved from libbpf when the xsk support there was deprecated. Some of the code there is not needed anymore as the selftests are only guaranteed to run on the kernel it is shipped with. Therefore, all the code that has to deal with compatibility of older kernels can be dropped and also any other function that is not of any use for the tests.
--- tools/testing/selftests/bpf/xsk.c | 617 +---------------------- tools/testing/selftests/bpf/xsk.h | 9 - tools/testing/selftests/bpf/xskxceiver.c | 8 - 3 files changed, 3 insertions(+), 631 deletions(-)
tools/testing/selftests/bpf/xsk.c +3 -614
@@ -35,8 +35,6 @@#include "xsk.h" #include "bpf_util.h" -#define FD_NOT_USED (-1) -#ifndef SOL_XDP #define SOL_XDP 283#endif
@@ -53,11 +51,6 @@#define XSKMAP_SIZE 1 -enum xsk_prog { -XSK_PROG_FALLBACK , -XSK_PROG_REDIRECT_FLAGS , -}; -struct xsk_umem {struct xsk_ring_prod *fill_save;struct xsk_ring_cons *comp_save;
@@ -78,11 +71,6 @@ struct xsk_ctx {int refcount;int ifindex;struct list_head list; -int prog_fd; -int link_fd; -int xsks_map_fd; -char ifname[IFNAMSIZ ]; -bool has_bpf_link; };struct xsk_socket {
@@ -93,27 +81,6 @@ struct xsk_socket {int fd; }; -struct xsk_nl_info { -bool xdp_prog_attached; -int ifindex; -int fd; -}; - -/* Up until and including Linux 5.3 */ -struct xdp_ring_offset_v1 { -__u64 producer; -__u64 consumer; -__u64 desc; -}; - -/* Up until and including Linux 5.3 */ -struct xdp_mmap_offsets_v1 { -struct xdp_ring_offset_v1 rx; -struct xdp_ring_offset_v1 tx; -struct xdp_ring_offset_v1 fr; -struct xdp_ring_offset_v1 cr; -}; -int xsk_umem__fd (const struct xsk_umem *umem) {return umem ? umem->fd : -EINVAL ;
@@ -156,55 +123,17 @@ static int xsk_set_xdp_socket_config(struct xsk_socket_config *cfg,if (!usr_cfg) { cfg->rx_size =XSK_RING_CONS__DEFAULT_NUM_DESCS ; cfg->tx_size =XSK_RING_PROD__DEFAULT_NUM_DESCS ; - cfg->libbpf_flags =0 ; - cfg->xdp_flags =0 ; cfg->bind_flags =0 ;return 0 ; } -if (usr_cfg->libbpf_flags & ~XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD ) -return -EINVAL ; - cfg->rx_size = usr_cfg->rx_size; cfg->tx_size = usr_cfg->tx_size; - cfg->libbpf_flags = usr_cfg->libbpf_flags; - cfg->xdp_flags = usr_cfg->xdp_flags; cfg->bind_flags = usr_cfg->bind_flags;return 0 ; } -static void xsk_mmap_offsets_v1 (struct xdp_mmap_offsets *off) -{ -struct xdp_mmap_offsets_v1 off_v1; - -/* getsockopt on a kernel <= 5.3 has no flags fields. -* Copy over the offsets to the correct places in the >=5.4 format -* and put the flags where they would have been on that kernel. -*/ -memcpy (&off_v1, off,sizeof (off_v1)); - - off->rx.producer = off_v1.rx.producer; - off->rx.consumer = off_v1.rx.consumer; - off->rx.desc = off_v1.rx.desc; - off->rx.flags = off_v1.rx.consumer +sizeof (__u32); - - off->tx.producer = off_v1.tx.producer; - off->tx.consumer = off_v1.tx.consumer; - off->tx.desc = off_v1.tx.desc; - off->tx.flags = off_v1.tx.consumer +sizeof (__u32); - - off->fr.producer = off_v1.fr.producer; - off->fr.consumer = off_v1.fr.consumer; - off->fr.desc = off_v1.fr.desc; - off->fr.flags = off_v1.fr.consumer +sizeof (__u32); - - off->cr.producer = off_v1.cr.producer; - off->cr.consumer = off_v1.cr.consumer; - off->cr.desc = off_v1.cr.desc; - off->cr.flags = off_v1.cr.consumer +sizeof (__u32); -} -static int xsk_get_mmap_offsets (int fd,struct xdp_mmap_offsets *off) {socklen_t optlen;
@@ -218,11 +147,6 @@ static int xsk_get_mmap_offsets(int fd, struct xdp_mmap_offsets *off)if (optlen ==sizeof (*off))return 0 ; -if (optlen ==sizeof (struct xdp_mmap_offsets_v1 )) { -xsk_mmap_offsets_v1 (off); -return 0 ; - } -return -EINVAL ; }
@@ -343,122 +267,19 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area,return err; } -struct xsk_umem_config_v1 { -__u32 fill_size; -__u32 comp_size; -__u32 frame_size; -__u32 frame_headroom; -}; - -static enum xsk_prog get_xsk_prog (void ) -{ -enum xsk_prog detected =XSK_PROG_FALLBACK ; -char data_in =0 , data_out; -struct bpf_insn insns[] = { -BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_MOV64_IMM (BPF_REG_2 ,0 ), -BPF_MOV64_IMM (BPF_REG_3 ,XDP_PASS ), -BPF_EMIT_CALL (BPF_FUNC_redirect_map), -BPF_EXIT_INSN (), - }; -LIBBPF_OPTS (bpf_test_run_opts, opts, - .data_in = &data_in, - .data_size_in = 1, - .data_out = &data_out, - ); - -int prog_fd, map_fd, ret, insn_cnt =ARRAY_SIZE (insns); - - map_fd =bpf_map_create (BPF_MAP_TYPE_XSKMAP ,NULL ,sizeof (int ),sizeof (int ),1 ,NULL ); -if (map_fd <0 ) -return detected; - - insns[0 ].imm = map_fd; - - prog_fd =bpf_prog_load (BPF_PROG_TYPE_XDP ,NULL ,"GPL" , insns, insn_cnt,NULL ); -if (prog_fd <0 ) { -close (map_fd); -return detected; - } - - ret =bpf_prog_test_run_opts (prog_fd, &opts); -if (!ret && opts.retval ==XDP_PASS ) - detected =XSK_PROG_REDIRECT_FLAGS ; -close (prog_fd); -close (map_fd); -return detected; -} -static int __xsk_load_xdp_prog (int xsk_map_fd) {static const int log_buf_size =16 *1024 ;char log_buf[log_buf_size];int prog_fd; -/* This is the fallback C-program: -* SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx) -* { -* int ret, index = ctx->rx_queue_index; -* -* // A set entry here means that the correspnding queue_id -* // has an active AF_XDP socket bound to it. -* ret = bpf_redirect_map(&xsks_map, index, XDP_PASS); -* if (ret > 0) -* return ret; -* -* // Fallback for pre-5.3 kernels, not supporting default -* // action in the flags parameter. -* if (bpf_map_lookup_elem(&xsks_map, &index)) -* return bpf_redirect_map(&xsks_map, index, 0); -* return XDP_PASS; -* } -*/ -struct bpf_insn prog[] = { -/* r2 = *(u32 *)(r1 + 16) */ -BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 ,16 ), -/* *(u32 *)(r10 - 4) = r2 */ -BPF_STX_MEM (BPF_W ,BPF_REG_10 ,BPF_REG_2 ,-4 ), -/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd), -/* r3 = XDP_PASS */ -BPF_MOV64_IMM (BPF_REG_3 ,2 ), -/* call bpf_redirect_map */ -BPF_EMIT_CALL (BPF_FUNC_redirect_map), -/* if w0 != 0 goto pc+13 */ -BPF_JMP32_IMM (BPF_JSGT ,BPF_REG_0 ,0 ,13 ), -/* r2 = r10 */ -BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), -/* r2 += -4 */ -BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-4 ), -/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd), -/* call bpf_map_lookup_elem */ -BPF_EMIT_CALL (BPF_FUNC_map_lookup_elem), -/* r1 = r0 */ -BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_0 ), -/* r0 = XDP_PASS */ -BPF_MOV64_IMM (BPF_REG_0 ,2 ), -/* if r1 == 0 goto pc+5 */ -BPF_JMP_IMM (BPF_JEQ ,BPF_REG_1 ,0 ,5 ), -/* r2 = *(u32 *)(r10 - 4) */ -BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_10 ,-4 ), -/* r1 = xskmap[] */ -BPF_LD_MAP_FD (BPF_REG_1 , xsk_map_fd), -/* r3 = 0 */ -BPF_MOV64_IMM (BPF_REG_3 ,0 ), -/* call bpf_redirect_map */ -BPF_EMIT_CALL (BPF_FUNC_redirect_map), -/* The jumps are to this instruction */ -BPF_EXIT_INSN (), - }; -/* This is the post-5.3 kernel C-program: * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx) * { * return bpf_redirect_map(&xsks_map, ctx->rx_queue_index, XDP_PASS); * } */ -struct bpf_insn prog_redirect_flags[] = { +struct bpf_insn prog[] = {/* r2 = *(u32 *)(r1 + 16) */ BPF_LDX_MEM (BPF_W ,BPF_REG_2 ,BPF_REG_1 ,16 ),/* r1 = xskmap[] */
@@ -469,18 +290,14 @@ static int __xsk_load_xdp_prog(int xsk_map_fd)BPF_EMIT_CALL (BPF_FUNC_redirect_map), BPF_EXIT_INSN(), }; -size_t insns_cnt[] = {ARRAY_SIZE (prog), -ARRAY_SIZE (prog_redirect_flags), - }; -struct bpf_insn *progs[] = {prog, prog_redirect_flags}; -enum xsk_prog option =get_xsk_prog (); +size_t insns_cnt =ARRAY_SIZE (prog);LIBBPF_OPTS (bpf_prog_load_opts, opts, .log_buf = log_buf, .log_size = log_buf_size, ); prog_fd =bpf_prog_load (BPF_PROG_TYPE_XDP ,NULL ,"LGPL-2.1 or BSD-2-Clause" , - progs[option], insns_cnt[option], &opts); + prog, insns_cnt, &opts);if (prog_fd <0 )pr_warn ("BPF log buffer:\n%s" , log_buf);
@@ -517,388 +334,6 @@ int xsk_attach_xdp_program(int ifindex, int prog_fd, u32 xdp_flags)return link_fd; } -static int xsk_create_bpf_link (struct xsk_socket *xsk) -{ -DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); -struct xsk_ctx *ctx = xsk->ctx; -__u32 prog_id =0 ; -int link_fd; -int err; - - err =bpf_xdp_query_id (ctx->ifindex, xsk->config.xdp_flags, &prog_id); -if (err) { -pr_warn ("getting XDP prog id failed\n" ); -return err; - } - -/* if there's a netlink-based XDP prog loaded on interface, bail out -* and ask user to do the removal by himself -*/ -if (prog_id) { -pr_warn ("Netlink-based XDP prog detected, please unload it in order to launch AF_XDP prog\n" ); -return -EINVAL ; - } - - opts.flags = xsk->config.xdp_flags & ~(XDP_FLAGS_UPDATE_IF_NOEXIST |XDP_FLAGS_REPLACE ); - - link_fd =bpf_link_create (ctx->prog_fd, ctx->ifindex,BPF_XDP , &opts); -if (link_fd <0 ) { -pr_warn ("bpf_link_create failed: %s\n" ,strerror (errno)); -return link_fd; - } - - ctx->link_fd = link_fd; -return 0 ; -} - -static int xsk_get_max_queues (struct xsk_socket *xsk) -{ -struct ethtool_channels channels = { .cmd =ETHTOOL_GCHANNELS }; -struct xsk_ctx *ctx = xsk->ctx; -struct ifreq ifr = {}; -int fd, err, ret; - - fd =socket (AF_LOCAL ,SOCK_DGRAM |SOCK_CLOEXEC ,0 ); -if (fd <0 ) -return -errno; - - ifr.ifr_data = (void *)&channels; -bpf_strlcpy (ifr.ifr_name, ctx->ifname,IFNAMSIZ ); - err =ioctl (fd,SIOCETHTOOL , &ifr); -if (err && errno !=EOPNOTSUPP ) { - ret = -errno; - goto out; - } - -if (err) { -/* If the device says it has no channels, then all traffic -* is sent to a single stream, so max queues = 1. -*/ - ret =1 ; - }else { -/* Take the max of rx, tx, combined. Drivers return -* the number of channels in different ways. -*/ - ret =max (channels.max_rx, channels.max_tx); - ret =max (ret, (int )channels.max_combined); - } - -out: -close (fd); -return ret; -} - -static int xsk_create_bpf_maps (struct xsk_socket *xsk) -{ -struct xsk_ctx *ctx = xsk->ctx; -int max_queues; -int fd; - - max_queues =xsk_get_max_queues (xsk); -if (max_queues <0 ) -return max_queues; - - fd =bpf_map_create (BPF_MAP_TYPE_XSKMAP ,"xsks_map" , -sizeof (int ),sizeof (int ), max_queues,NULL ); -if (fd <0 ) -return fd; - - ctx->xsks_map_fd = fd; - -return 0 ; -} - -static void xsk_delete_bpf_maps (struct xsk_socket *xsk) -{ -struct xsk_ctx *ctx = xsk->ctx; - -if (ctx->xsks_map_fd ==FD_NOT_USED ) -return ; - -bpf_map_delete_elem (ctx->xsks_map_fd, &ctx->queue_id); -close (ctx->xsks_map_fd); -} - -static int xsk_lookup_bpf_maps (struct xsk_socket *xsk) -{ -__u32 i, *map_ids, num_maps, prog_len =sizeof (struct bpf_prog_info ); -__u32 map_len =sizeof (struct bpf_map_info ); -struct bpf_prog_info prog_info = {}; -struct xsk_ctx *ctx = xsk->ctx; -struct bpf_map_info map_info; -int fd, err; - - err =bpf_obj_get_info_by_fd (ctx->prog_fd, &prog_info, &prog_len); -if (err) -return err; - - num_maps = prog_info.nr_map_ids; - - map_ids =calloc (prog_info.nr_map_ids,sizeof (*map_ids)); -if (!map_ids) -return -ENOMEM ; - -memset (&prog_info,0 , prog_len); - prog_info.nr_map_ids = num_maps; - prog_info.map_ids = (__u64 )(unsigned long )map_ids; - - err =bpf_obj_get_info_by_fd (ctx->prog_fd, &prog_info, &prog_len); -if (err) - goto out_map_ids; - - ctx->xsks_map_fd =-1 ; - -for (i =0 ; i < prog_info.nr_map_ids; i++) { - fd =bpf_map_get_fd_by_id (map_ids[i]); -if (fd <0 ) -continue ; - -memset (&map_info,0 , map_len); - err =bpf_obj_get_info_by_fd (fd, &map_info, &map_len); -if (err) { -close (fd); -continue ; - } - -if (!strncmp (map_info.name,"xsks_map" ,sizeof (map_info.name))) { - ctx->xsks_map_fd = fd; -break ; - } - -close (fd); - } - -if (ctx->xsks_map_fd ==-1 ) - err = -ENOENT ; - -out_map_ids: -free (map_ids); -return err; -} - -static int xsk_set_bpf_maps (struct xsk_socket *xsk) -{ -struct xsk_ctx *ctx = xsk->ctx; - -return bpf_map_update_elem (ctx->xsks_map_fd, &ctx->queue_id, - &xsk->fd,0 ); -} - -static int xsk_link_lookup (int ifindex,__u32 *prog_id,int *link_fd) -{ -struct bpf_link_info link_info; -__u32 link_len; -__u32 id =0 ; -int err; -int fd; - -while (true) { - err =bpf_link_get_next_id (id, &id); -if (err) { -if (errno ==ENOENT ) { - err =0 ; -break ; - } -pr_warn ("can't get next link: %s\n" ,strerror (errno)); -break ; - } - - fd =bpf_link_get_fd_by_id (id); -if (fd <0 ) { -if (errno ==ENOENT ) -continue ; -pr_warn ("can't get link by id (%u): %s\n" , id,strerror (errno)); - err = -errno; -break ; - } - - link_len =sizeof (struct bpf_link_info ); -memset (&link_info,0 , link_len); - err =bpf_obj_get_info_by_fd (fd, &link_info, &link_len); -if (err) { -pr_warn ("can't get link info: %s\n" ,strerror (errno)); -close (fd); -break ; - } -if (link_info.type ==BPF_LINK_TYPE_XDP ) { -if (link_info.xdp.ifindex == ifindex) { - *link_fd = fd; -if (prog_id) - *prog_id = link_info.prog_id; -break ; - } - } -close (fd); - } - -return err; -} - -static bool xsk_probe_bpf_link (void ) -{ - LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = XDP_FLAGS_SKB_MODE); -struct bpf_insn insns[2 ] = { -BPF_MOV64_IMM (BPF_REG_0 ,XDP_PASS ), -BPF_EXIT_INSN () - }; -int prog_fd, link_fd =-1 , insn_cnt =ARRAY_SIZE (insns); -int ifindex_lo =1 ; -bool ret = false; -int err; - - err =xsk_link_lookup (ifindex_lo,NULL , &link_fd); -if (err) -return ret; - -if (link_fd >=0 ) -return true; - - prog_fd =bpf_prog_load (BPF_PROG_TYPE_XDP ,NULL ,"GPL" , insns, insn_cnt,NULL ); -if (prog_fd <0 ) -return ret; - - link_fd =bpf_link_create (prog_fd, ifindex_lo,BPF_XDP , &opts); -close (prog_fd); - -if (link_fd >=0 ) { - ret = true; -close (link_fd); - } - -return ret; -} - -static int xsk_create_xsk_struct (int ifindex,struct xsk_socket *xsk) -{ -char ifname[IFNAMSIZ ]; -struct xsk_ctx *ctx; -char *interface; - - ctx =calloc (1 ,sizeof (*ctx)); -if (!ctx) -return -ENOMEM ; - - interface =if_indextoname (ifindex, &ifname[0 ]); -if (!interface) { -free (ctx); -return -errno; - } - - ctx->ifindex = ifindex; -bpf_strlcpy (ctx->ifname, ifname,IFNAMSIZ ); - - xsk->ctx = ctx; - xsk->ctx->has_bpf_link =xsk_probe_bpf_link (); - -return 0 ; -} - -static int xsk_init_xdp_res (struct xsk_socket *xsk, -int *xsks_map_fd) -{ -struct xsk_ctx *ctx = xsk->ctx; -int err; - - err =xsk_create_bpf_maps (xsk); -if (err) -return err; - - err =__xsk_load_xdp_prog (*xsks_map_fd); -if (err) - goto err_load_xdp_prog; - -if (ctx->has_bpf_link) - err =xsk_create_bpf_link (xsk); -else - err =bpf_xdp_attach (xsk->ctx->ifindex, ctx->prog_fd, - xsk->config.xdp_flags,NULL ); - -if (err) - goto err_attach_xdp_prog; - -if (!xsk->rx) -return err; - - err =xsk_set_bpf_maps (xsk); -if (err) - goto err_set_bpf_maps; - -return err; - -err_set_bpf_maps: -if (ctx->has_bpf_link) -close (ctx->link_fd); -else -bpf_xdp_detach (ctx->ifindex,0 ,NULL ); -err_attach_xdp_prog: -close (ctx->prog_fd); -err_load_xdp_prog: -xsk_delete_bpf_maps (xsk); -return err; -} - -static int xsk_lookup_xdp_res (struct xsk_socket *xsk,int *xsks_map_fd,int prog_id) -{ -struct xsk_ctx *ctx = xsk->ctx; -int err; - - ctx->prog_fd =bpf_prog_get_fd_by_id (prog_id); -if (ctx->prog_fd <0 ) { - err = -errno; - goto err_prog_fd; - } - err =xsk_lookup_bpf_maps (xsk); -if (err) - goto err_lookup_maps; - -if (!xsk->rx) -return err; - - err =xsk_set_bpf_maps (xsk); -if (err) - goto err_set_maps; - -return err; - -err_set_maps: -close (ctx->xsks_map_fd); -err_lookup_maps: -close (ctx->prog_fd); -err_prog_fd: -if (ctx->has_bpf_link) -close (ctx->link_fd); -return err; -} - -static int __xsk_setup_xdp_prog (struct xsk_socket *_xdp,int *xsks_map_fd) -{ -struct xsk_socket *xsk = _xdp; -struct xsk_ctx *ctx = xsk->ctx; -__u32 prog_id =0 ; -int err; - -if (ctx->has_bpf_link) - err =xsk_link_lookup (ctx->ifindex, &prog_id, &ctx->link_fd); -else - err =bpf_xdp_query_id (ctx->ifindex, xsk->config.xdp_flags, &prog_id); - -if (err) -return err; - - err = !prog_id ?xsk_init_xdp_res (xsk, xsks_map_fd) : -xsk_lookup_xdp_res (xsk, xsks_map_fd, prog_id); - -if (!err && xsks_map_fd) - *xsks_map_fd = ctx->xsks_map_fd; - -return err; -} - -int xsk_setup_xdp_prog_xsk (struct xsk_socket *xsk,int *xsks_map_fd) -{ -return __xsk_setup_xdp_prog (xsk, xsks_map_fd); -} -int xsk_load_xdp_program (int *xsk_map_fd,int *prog_fd) { *xsk_map_fd = bpf_map_create(BPF_MAP_TYPE_XSKMAP, "xsks_map", sizeof(int), sizeof(int),
@@ -988,51 +423,13 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk, ctx->refcount =1 ; ctx->umem = umem; ctx->queue_id = queue_id; - ctx->prog_fd =FD_NOT_USED ; - ctx->link_fd =FD_NOT_USED ; - ctx->xsks_map_fd =FD_NOT_USED ; ctx->fill = fill; ctx->comp = comp;list_add (&ctx->list, &umem->ctx_list); - ctx->has_bpf_link =xsk_probe_bpf_link ();return ctx; } -static void xsk_destroy_xsk_struct (struct xsk_socket *xsk) -{ -free (xsk->ctx); -free (xsk); -} - -int xsk_socket__update_xskmap (struct xsk_socket *xsk,int fd) -{ - xsk->ctx->xsks_map_fd = fd; -return xsk_set_bpf_maps (xsk); -} - -int xsk_setup_xdp_prog (int ifindex,int *xsks_map_fd) -{ -struct xsk_socket *xsk; -int res; - - xsk =calloc (1 ,sizeof (*xsk)); -if (!xsk) -return -ENOMEM ; - - res =xsk_create_xsk_struct (ifindex, xsk); -if (res) { -free (xsk); -return -EINVAL ; - } - - res =__xsk_setup_xdp_prog (xsk, xsks_map_fd); - -xsk_destroy_xsk_struct (xsk); - -return res; -} - int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, int ifindex, __u32 queue_id, struct xsk_umem *umem,
@@ -1255,14 +652,6 @@ void xsk_socket__delete(struct xsk_socket *xsk) ctx = xsk->ctx; umem = ctx->umem; -if (ctx->refcount ==1 ) { -xsk_delete_bpf_maps (xsk); -if (ctx->prog_fd !=FD_NOT_USED ) -close (ctx->prog_fd); -if (ctx->has_bpf_link && ctx->link_fd !=FD_NOT_USED ) -close (ctx->link_fd); - } -xsk_put_ctx (ctx, true); err =xsk_get_mmap_offsets (xsk->fd, &off);
tools/testing/selftests/bpf/xsk.h +0 -9
@@ -197,21 +197,12 @@ struct xsk_umem_config {__u32 flags; }; -int xsk_setup_xdp_prog_xsk (struct xsk_socket *xsk,int *xsks_map_fd); -int xsk_setup_xdp_prog (int ifindex,int *xsks_map_fd); -int xsk_socket__update_xskmap (struct xsk_socket *xsk,int xsks_map_fd); - -/* Flags for the libbpf_flags field. */ -#define XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD (1 << 0) -int xsk_load_xdp_program (int *xsk_map_fd,int *prog_fd);int xsk_attach_xdp_program (int ifindex,int prog_fd,u32 xdp_flags);struct xsk_socket_config {__u32 rx_size;__u32 tx_size; -__u32 libbpf_flags; -__u32 xdp_flags;__u16 bind_flags; };
tools/testing/selftests/bpf/xskxceiver.c +0 -8
@@ -102,12 +102,6 @@#include <linux/filter.h> #include "../kselftest.h" -/* AF_XDP APIs were moved into libxdp and marked as deprecated in libbpf. -* Until xskxceiver is either moved or re-writed into libxdp, suppress -* deprecation warnings in this file -*/ -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -static const char *MAC1 ="\x00\x0A\x56\x9E\xEE\x62" ;static const char *MAC2 ="\x00\x0A\x56\x9E\xEE\x61" ;static const char *IP1 ="192.168.100.162" ;
@@ -326,8 +320,6 @@ static int __xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_i xsk->umem = umem; cfg.rx_size = xsk->rxqsize; cfg.tx_size =XSK_RING_PROD__DEFAULT_NUM_DESCS ; - cfg.libbpf_flags =XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD ; - cfg.xdp_flags = ifobject->xdp_flags; cfg.bind_flags = ifobject->bind_flags;if (shared) cfg.bind_flags |=XDP_SHARED_UMEM ;
Signature · 1 line
2.34.1
PATCH v3 11/15 selftests/xsk: get rid of built-in XDP program
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Get rid of the built-in XDP program that was part of the old libbpf code in xsk.c and replace it with an eBPF program build using the framework by all the other bpf selftests. This will form the base for adding more programs in later commits.
--- tools/testing/selftests/bpf/Makefile | 6 +- .../selftests/bpf/progs/xsk_xdp_progs.c | 19 ++++ tools/testing/selftests/bpf/xsk.c | 88 ++++--------------- tools/testing/selftests/bpf/xsk.h | 6 +- tools/testing/selftests/bpf/xskxceiver.c | 72 ++++++++------- tools/testing/selftests/bpf/xskxceiver.h | 7 +- 6 files changed, 92 insertions(+), 106 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/xsk_xdp_progs.c
tools/testing/selftests/bpf/Makefile +5 -1
@@ -240,7 +240,6 @@ $(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 BPFTOOL ?= $(DEFAULT_BPFTOOL) $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile) \
@@ -383,6 +382,7 @@ linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o +xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
@@ -576,6 +576,10 @@ $(OUTPUT)/test_verifier: test_verifier.c verifier/tests.h $(BPFOBJ) | $(OUTPUT) $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ +$(OUTPUT)/xskxceiver: xskxceiver.c $(OUTPUT)/xsk.o $(OUTPUT)/xsk_xdp_progs.skel.h $(BPFOBJ) | $(OUTPUT) + $(call msg,BINARY,,$@) + $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ + # Make sure we are able to include and link libbpf against c++. $(OUTPUT)/test_cpp: test_cpp.cpp $(OUTPUT)/test_core_extern.skel.h $(BPFOBJ) $(call msg,CXX,,$@)
tools/testing/selftests/bpf/progs/xsk_xdp_progs.c new file +19 -0
@@ -0,0 +1,19 @@ +// 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"); + +SEC("xdp") int xsk_def_prog(struct xdp_md *xdp) +{ +return bpf_redirect_map (&xsk,0 ,XDP_DROP ); +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/xsk.c +19 -69
106 lines not on this page · raw message
tools/testing/selftests/bpf/xsk.h +4 -2
@@ -197,8 +197,10 @@ struct xsk_umem_config {__u32 flags; }; -int xsk_load_xdp_program (int *xsk_map_fd,int *prog_fd); -int xsk_attach_xdp_program (int ifindex,int prog_fd,u32 xdp_flags); +int xsk_attach_xdp_program (struct bpf_program *prog,int ifindex,u32 xdp_flags); +void xsk_detach_xdp_program (int ifindex,u32 xdp_flags); +int xsk_update_xskmap (struct bpf_map *map,struct xsk_socket *xsk); +void xsk_clear_xskmap (struct bpf_map *map);struct xsk_socket_config {__u32 rx_size;
tools/testing/selftests/bpf/xskxceiver.c +41 -31
167 lines not on this page · raw message
tools/testing/selftests/bpf/xskxceiver.h +4 -3
@@ -5,6 +5,8 @@#ifndef XSKXCEIVER_H_ #define XSKXCEIVER_H_ +#include "xsk_xdp_progs.skel.h" +#ifndef SOL_XDP #define SOL_XDP 283#endif
@@ -138,9 +140,8 @@ struct ifobject {thread_func_t func_ptr;validation_func_t validation_func;struct pkt_stream *pkt_stream; -int xsk_map_fd; -int prog_fd; -int link_fd; +struct xsk_xdp_progs *xdp_progs; +struct bpf_map *xskmap;int ifindex;u32 dst_ip;u32 src_ip;
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 11/15] selftests/xsk: get rid of built-in XDP program
Quoted 1 line · Magnus Karlsson
+xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
...
Quoted 23 lines
tools/testing/selftests/bpf/progs/xsk_xdp_progs.c new file +17
-0@@ -0,0 +1,19 @@ +// 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"); + +SEC("xdp") int xsk_def_prog(struct xdp_md *xdp) +{ +return bpf_redirect_map (&xsk,0 ,XDP_DROP ); +}
...
Quoted 1 line
+#include "
xsk_xdp_progs.skel.h"
Nice. Glad you found it useful. Clearly helps the next patch ;)
PATCH v3 12/15 selftests/xsk: add test when some packets are XDP_DROPed
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
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.
--- .../selftests/bpf/progs/xsk_xdp_progs.c | 11 +++++++ tools/testing/selftests/bpf/xskxceiver.c | 31 +++++++++++++++++++ tools/testing/selftests/bpf/xskxceiver.h | 1 + 3 files changed, 43 insertions(+)
tools/testing/selftests/bpf/progs/xsk_xdp_progs.c +11 -0
@@ -11,9 +11,20 @@ struct {__uint (value_size,sizeof (int )); } xsk SEC(".maps"); +static unsigned int idx; + SEC("xdp") int xsk_def_prog(struct xdp_md *xdp) {return bpf_redirect_map (&xsk,0 ,XDP_DROP ); } +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";
tools/testing/selftests/bpf/xskxceiver.c +31 -0
@@ -1646,6 +1646,34 @@ static void testapp_invalid_desc(struct test_spec *test)pkt_stream_restore_default (test); } +static void testapp_xdp_drop (struct test_spec *test) +{ +struct ifobject *ifobj = test->ifobj_rx; +int err; + +test_spec_set_name (test,"XDP_DROP_HALF" ); +xsk_detach_xdp_program (ifobj->ifindex, ifobj->xdp_flags); + err =xsk_attach_xdp_program (ifobj->xdp_progs->progs.xsk_xdp_drop, ifobj->ifindex, + ifobj->xdp_flags); +if (err) { +printf ("Error attaching XDP_DROP program\n" ); + test->fail = true; +return ; + } + +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->xdp_progs->progs.xsk_def_prog, ifobj->ifindex, + ifobj->xdp_flags); +if (err) { +printf ("Error restoring default XDP program\n" ); +exit_with_error (-err); + } +} +static int xsk_load_xdp_programs (struct ifobject *ifobj) { ifobj->xdp_progs =xsk_xdp_progs__open_and_load ();
@@ -1796,6 +1824,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_DROP_HALF : +testapp_xdp_drop (test); +break ;default :break ; }
tools/testing/selftests/bpf/xskxceiver.h +1 -0
@@ -87,6 +87,7 @@ enum test_type {TEST_TYPE_STATS_RX_FULL ,TEST_TYPE_STATS_FILL_EMPTY ,TEST_TYPE_BPF_RES , +TEST_TYPE_XDP_DROP_HALF , TEST_TYPE_MAX };
Signature · 1 line
2.34.1
PATCH v3 13/15 selftests/xsk: merge dual and single thread dispatchers
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Make the thread dispatching code common by unifying the dual and single thread dispatcher code. This so we do not have to add code in two places in upcoming commits.
--- tools/testing/selftests/bpf/xskxceiver.c | 120 ++++++++++------------- 1 file changed, 54 insertions(+), 66 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +54 -66
@@ -1356,83 +1356,59 @@ static void handler(int signum)pthread_exit (NULL ); } -static int testapp_validate_traffic_single_thread (struct test_spec *test,struct ifobject *ifobj, -enum test_type type) +static int __testapp_validate_traffic (struct test_spec *test,struct ifobject *ifobj1, +struct ifobject *ifobj2) { -bool old_shared_umem = ifobj->shared_umem; -pthread_t t0; +pthread_t t0, t1; -if (pthread_barrier_init (&barr,NULL ,2 )) -exit_with_error (errno); +if (ifobj2) +if (pthread_barrier_init (&barr,NULL ,2 )) +exit_with_error (errno); test->current_step++; -if (type ==TEST_TYPE_POLL_RXQ_TMOUT ) -pkt_stream_reset (ifobj->pkt_stream); +pkt_stream_reset (ifobj1->pkt_stream); pkts_in_flight =0 ; - test->ifobj_rx->shared_umem = false; - test->ifobj_tx->shared_umem = false; -signal (SIGUSR1 , handler); -/* Spawn thread */ -pthread_create (&t0,NULL , ifobj->func_ptr, test); +/*Spawn RX thread */ +pthread_create (&t0,NULL , ifobj1->func_ptr, test); -if (type !=TEST_TYPE_POLL_TXQ_TMOUT ) +if (ifobj2) {pthread_barrier_wait (&barr); +if (pthread_barrier_destroy (&barr)) +exit_with_error (errno); -if (pthread_barrier_destroy (&barr)) -exit_with_error (errno); +/*Spawn TX thread */ +pthread_create (&t1,NULL , ifobj2->func_ptr, test); -pthread_kill (t0,SIGUSR1 ); -pthread_join (t0,NULL ); +pthread_join (t1,NULL ); + } + +if (!ifobj2) +pthread_kill (t0,SIGUSR1 ); +else +pthread_join (t0,NULL );if (test->total_steps == test->current_step || test->fail) { -xsk_socket__delete (ifobj->xsk->xsk); -xsk_clear_xskmap (ifobj->xskmap); -testapp_clean_xsk_umem (ifobj); +if (ifobj2) +xsk_socket__delete (ifobj2->xsk->xsk); +xsk_socket__delete (ifobj1->xsk->xsk); +testapp_clean_xsk_umem (ifobj1); +if (ifobj2 && !ifobj2->shared_umem) +testapp_clean_xsk_umem (ifobj2); } - test->ifobj_rx->shared_umem = old_shared_umem; - test->ifobj_tx->shared_umem = old_shared_umem; -return !!test->fail; }static int testapp_validate_traffic (struct test_spec *test) { -struct ifobject *ifobj_tx = test->ifobj_tx; -struct ifobject *ifobj_rx = test->ifobj_rx; -pthread_t t0, t1; - -if (pthread_barrier_init (&barr,NULL ,2 )) -exit_with_error (errno); - - test->current_step++; -pkt_stream_reset (ifobj_rx->pkt_stream); - pkts_in_flight =0 ; - -/*Spawn RX thread */ -pthread_create (&t0,NULL , ifobj_rx->func_ptr, test); - -pthread_barrier_wait (&barr); -if (pthread_barrier_destroy (&barr)) -exit_with_error (errno); - -/*Spawn TX thread */ -pthread_create (&t1,NULL , ifobj_tx->func_ptr, test); - -pthread_join (t1,NULL ); -pthread_join (t0,NULL ); - -if (test->total_steps == test->current_step || test->fail) { -xsk_socket__delete (ifobj_tx->xsk->xsk); -xsk_socket__delete (ifobj_rx->xsk->xsk); -testapp_clean_xsk_umem (ifobj_rx); -if (!ifobj_tx->shared_umem) -testapp_clean_xsk_umem (ifobj_tx); - } +return __testapp_validate_traffic (test, test->ifobj_rx, test->ifobj_tx); +} -return !!test->fail; +static int testapp_validate_traffic_single_thread (struct test_spec *test,struct ifobject *ifobj) +{ +return __testapp_validate_traffic (test, ifobj,NULL ); }static void testapp_teardown (struct test_spec *test)
@@ -1674,6 +1650,26 @@ static void testapp_xdp_drop(struct test_spec *test) } } +static void testapp_poll_txq_tmout (struct test_spec *test) +{ +test_spec_set_name (test,"POLL_TXQ_FULL" ); + + test->ifobj_tx->use_poll = true; +/* create invalid frame by set umem frame_size and pkt length equal to 2048 */ + test->ifobj_tx->umem->frame_size =2048 ; +pkt_stream_replace (test,2 *DEFAULT_PKT_CNT ,2048 ); +testapp_validate_traffic_single_thread (test, test->ifobj_tx); + +pkt_stream_restore_default (test); +} + +static void testapp_poll_rxq_tmout (struct test_spec *test) +{ +test_spec_set_name (test,"POLL_RXQ_EMPTY" ); + test->ifobj_rx->use_poll = true; +testapp_validate_traffic_single_thread (test, test->ifobj_rx); +} +static int xsk_load_xdp_programs (struct ifobject *ifobj) { ifobj->xdp_progs =xsk_xdp_progs__open_and_load ();
@@ -1784,18 +1780,10 @@ static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_testapp_validate_traffic (test);break ;case TEST_TYPE_POLL_TXQ_TMOUT : -test_spec_set_name (test,"POLL_TXQ_FULL" ); - test->ifobj_tx->use_poll = true; -/* create invalid frame by set umem frame_size and pkt length equal to 2048 */ - test->ifobj_tx->umem->frame_size =2048 ; -pkt_stream_replace (test,2 *DEFAULT_PKT_CNT ,2048 ); -testapp_validate_traffic_single_thread (test, test->ifobj_tx, type); -pkt_stream_restore_default (test); +testapp_poll_txq_tmout (test);break ;case TEST_TYPE_POLL_RXQ_TMOUT : -test_spec_set_name (test,"POLL_RXQ_EMPTY" ); - test->ifobj_rx->use_poll = true; -testapp_validate_traffic_single_thread (test, test->ifobj_rx, type); +testapp_poll_rxq_tmout (test);break ;case TEST_TYPE_ALIGNED_INV_DESC :test_spec_set_name (test,"ALIGNED_INV_DESC" );
Signature · 1 line
2.34.1
PATCH v3 14/15 selftests/xsk: automatically restore packet stream
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Automatically restore the default packet stream if needed at the end of each test. This so that test writers do not forget to do this.
--- tools/testing/selftests/bpf/xskxceiver.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
tools/testing/selftests/bpf/xskxceiver.c +1 -14
@@ -1501,8 +1501,6 @@ static void testapp_stats_tx_invalid_descs(struct test_spec *test)pkt_stream_replace_half (test,XSK_UMEM__INVALID_FRAME_SIZE ,0 ); test->ifobj_tx->validation_func = validate_tx_invalid_descs;testapp_validate_traffic (test); - -pkt_stream_restore_default (test); }static void testapp_stats_rx_full (struct test_spec *test)
@@ -1518,8 +1516,6 @@ static void testapp_stats_rx_full(struct test_spec *test) test->ifobj_rx->release_rx = false; test->ifobj_rx->validation_func = validate_rx_full;testapp_validate_traffic (test); - -pkt_stream_restore_default (test); }static void testapp_stats_fill_empty (struct test_spec *test)
@@ -1534,8 +1530,6 @@ static void testapp_stats_fill_empty(struct test_spec *test) test->ifobj_rx->use_fill_ring = false; test->ifobj_rx->validation_func = validate_fill_empty;testapp_validate_traffic (test); - -pkt_stream_restore_default (test); }/* Simple test */
@@ -1568,7 +1562,6 @@ static bool testapp_unaligned(struct test_spec *test) test->ifobj_rx->pkt_stream->use_addr_for_fill = true;testapp_validate_traffic (test); -pkt_stream_restore_default (test);return true; }
@@ -1578,7 +1571,6 @@ static void testapp_single_pkt(struct test_spec *test)pkt_stream_generate_custom (test, pkts,ARRAY_SIZE (pkts));testapp_validate_traffic (test); -pkt_stream_restore_default (test); }static void testapp_invalid_desc (struct test_spec *test)
@@ -1619,7 +1611,6 @@ static void testapp_invalid_desc(struct test_spec *test)pkt_stream_generate_custom (test, pkts,ARRAY_SIZE (pkts));testapp_validate_traffic (test); -pkt_stream_restore_default (test); }static void testapp_xdp_drop (struct test_spec *test)
@@ -1640,7 +1631,6 @@ static void testapp_xdp_drop(struct test_spec *test)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->xdp_progs->progs.xsk_def_prog, ifobj->ifindex, ifobj->xdp_flags);
@@ -1659,8 +1649,6 @@ static void testapp_poll_txq_tmout(struct test_spec *test) test->ifobj_tx->umem->frame_size =2048 ;pkt_stream_replace (test,2 *DEFAULT_PKT_CNT ,2048 );testapp_validate_traffic_single_thread (test, test->ifobj_tx); - -pkt_stream_restore_default (test); }static void testapp_poll_rxq_tmout (struct test_spec *test)
@@ -1766,8 +1754,6 @@ static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_ test->ifobj_rx->umem->frame_size =2048 ;pkt_stream_replace (test,DEFAULT_PKT_CNT ,PKT_SIZE );testapp_validate_traffic (test); - -pkt_stream_restore_default (test);break ;case TEST_TYPE_RX_POLL : test->ifobj_rx->use_poll = true;
@@ -1822,6 +1808,7 @@ static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_if (!test->fail)ksft_test_result_pass ("PASS: %s %s%s\n" ,mode_string (test),busy_poll_string (test), test->name); +pkt_stream_restore_default (test); }static struct ifobject *ifobject_create (void )
Signature · 1 line
2.34.1
PATCH v3 15/15 selftests/xsk: automatically switch XDP programs
In reply to Magnus KarlssonFrom: Magnus Karlsson <[email protected]>
Implement automatic switching of XDP programs and execution modes if needed by a test. This makes it much simpler to write a test as it only has to say what XDP program it needs if it is not the default one. This also makes it possible to remove the initial explicit attachment of the XDP program as well as the explicit mode switch in the code. These are now handled by the same code that just checks if a switch is necessary, so no special cases are needed.
The default XDP program for all tests is one that sends all packets to the AF_XDP socket. If you need another one, please use the new function test_spec_set_xdp_prog() to specify what XDP programs and maps to use for this test.
--- tools/testing/selftests/bpf/xsk.c | 19 ++++ tools/testing/selftests/bpf/xsk.h | 1 + tools/testing/selftests/bpf/xskxceiver.c | 137 ++++++++++++----------- tools/testing/selftests/bpf/xskxceiver.h | 7 +- 4 files changed, 96 insertions(+), 68 deletions(-)
tools/testing/selftests/bpf/xsk.c +19 -0
@@ -267,6 +267,25 @@ int xsk_umem__create(struct xsk_umem **umem_ptr, void *umem_area,return err; } +bool xsk_is_in_mode (u32 ifindex,int mode) +{ +LIBBPF_OPTS (bpf_xdp_query_opts, opts); +int ret; + + ret =bpf_xdp_query (ifindex, mode, &opts); +if (ret) { +printf ("XDP mode query returned error %s\n" ,strerror (errno)); +return false; + } + +if (mode ==XDP_FLAGS_DRV_MODE ) +return opts.attach_mode ==XDP_ATTACHED_DRV ; +else if (mode ==XDP_FLAGS_SKB_MODE ) +return opts.attach_mode ==XDP_ATTACHED_SKB ; + +return false; +} +int xsk_attach_xdp_program (struct bpf_program *prog,int ifindex,u32 xdp_flags) {int prog_fd;
tools/testing/selftests/bpf/xsk.h +1 -0
@@ -201,6 +201,7 @@ int xsk_attach_xdp_program(struct bpf_program *prog, int ifindex, u32 xdp_flags)void xsk_detach_xdp_program (int ifindex,u32 xdp_flags);int xsk_update_xskmap (struct bpf_map *map,struct xsk_socket *xsk);void xsk_clear_xskmap (struct bpf_map *map); +bool xsk_is_in_mode (u32 ifindex,int mode);struct xsk_socket_config {__u32 rx_size;
tools/testing/selftests/bpf/xskxceiver.c +70 -67
226 lines not on this page · raw message
tools/testing/selftests/bpf/xskxceiver.h +6 -1
@@ -143,10 +143,11 @@ struct ifobject {struct pkt_stream *pkt_stream;struct xsk_xdp_progs *xdp_progs;struct bpf_map *xskmap; +struct bpf_program *xdp_prog; +enum test_mode mode;int ifindex;u32 dst_ip;u32 src_ip; -u32 xdp_flags;u32 bind_flags;u16 src_port;u16 dst_port;
@@ -166,6 +167,10 @@ struct test_spec {struct ifobject *ifobj_rx;struct pkt_stream *tx_pkt_stream_default;struct pkt_stream *rx_pkt_stream_default; +struct bpf_program *xdp_prog_rx; +struct bpf_program *xdp_prog_tx; +struct bpf_map *xskmap_rx; +struct bpf_map *xskmap_tx;u16 total_steps;u16 current_step;u16 nb_sockets;
Signature · 1 line
2.34.1
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Wed, 11 Jan 2023 10:35:11 +0100 you wrote:
This is a patch set of various performance improvements, fixes, and the introduction of more than one XDP program to the xsk selftests framework so we can test more things in the future such as upcoming multi-buffer and metadata support for
AF_XDP. The new programs just reuse the framework that all the other eBPF selftests use. The new feature is used to implement one new test that doesXDP_DROPon every other packet. More tests using this will be added in future commits.[...]
Here is the summary with links:
- [bpf-next,v3,01/15] selftests/xsk: print correct payload for packet dump
https://git.kernel.org/bpf/bpf-next/c/2d0b2ae2871a - [bpf-next,v3,02/15] selftests/xsk: do not close unused file descriptors https://git.kernel.org/bpf/bpf-next/c/5adaf52776a4 - [bpf-next,v3,03/15] selftests/xsk: submit correct number of frames in populate_fill_ring https://git.kernel.org/bpf/bpf-next/c/1e04f23bccf9 - [bpf-next,v3,04/15] selftests/xsk: print correct error codes when exiting https://git.kernel.org/bpf/bpf-next/c/085dcccfb7d3 - [bpf-next,v3,05/15] selftests/xsk: remove unused variable outstanding_tx https://git.kernel.org/bpf/bpf-next/c/a4ca62277b6a - [bpf-next,v3,06/15] selftests/xsk: add debug option for creating netdevs https://git.kernel.org/bpf/bpf-next/c/703bfd371013 - [bpf-next,v3,07/15] selftests/xsk: replace asm acquire/release implementations https://git.kernel.org/bpf/bpf-next/c/efe620e5ba03 - [bpf-next,v3,08/15] selftests/xsk: remove namespaces https://git.kernel.org/bpf/bpf-next/c/64aef77d750e - [bpf-next,v3,09/15] selftests/xsk: load and attach XDP program only once per mode https://git.kernel.org/bpf/bpf-next/c/aa61d81f397c - [bpf-next,v3,10/15] selftests/xsk: remove unnecessary code in control path https://git.kernel.org/bpf/bpf-next/c/6b3c0821caa4 - [bpf-next,v3,11/15] selftests/xsk: get rid of built-in XDP program https://git.kernel.org/bpf/bpf-next/c/f0a249df1b07 - [bpf-next,v3,12/15] selftests/xsk: add test when some packets are XDP_DROPed https://git.kernel.org/bpf/bpf-next/c/80bea9acabb7 - [bpf-next,v3,13/15] selftests/xsk: merge dual and single thread dispatchers https://git.kernel.org/bpf/bpf-next/c/7f881984073a - [bpf-next,v3,14/15] selftests/xsk: automatically restore packet stream https://git.kernel.org/bpf/bpf-next/c/e67b2554f301 - [bpf-next,v3,15/15] selftests/xsk: automatically switch XDP programs https://git.kernel.org/bpf/bpf-next/c/7d8319a7cc66
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html