From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796401786; b=Xj3vAURbVOrHdJmwQe4yjMoTY6/IWQFaAO835nGq2io8JYE7AsnWGQokq5ribVoOsgQM ft98uNvRa2NVqeAKO9XQceF5KpBYus2IgDQ25sJk4LfcyyPoL/YPBr4qM2uMunbLS780h LksN26CGGPeVeBZh7hy76V8ofXnLhpxTDS6sw+mITpDNg8S84uj/pTwp09Y/0L+QxRiK9 qBrs0PwQXmAcsIVncHtOe8yyNXRQ0eYCFDX+QKeErh8+ybCUTC2OEZPCcW/4GgeiYL/yM +AUEyJ86RZIAsjIKMQK6n7AJ78+w8je5dE26I2AwKhN4ETl1xzR1tnsWsUWDuvI12sA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401786; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=0geHnZu0sVMZCqAE7MB+lL2GyXaqJTrlRbUHytT5wjw=; b=lTwTS/MCrGxEOaFmIvfvROFBJCYgXLThupJRs+rQXzKdD621xzAXmI4ho+xhtrsCbbjr 6AqxnWa4GEugD3vS/ckP1RjGMeBC1KzMfNo6vMxrnI0LF/voIP90G7DbvKJRRM36x7veU yg+4CgN8mJBeL+q6seLkFCTm1JC1SRg1LB5px+5cK9zoD0JhC70vguu0LjbGIAqerDOjX rhgtmZj90GCqLVbqqPQByNwSeVbTWullAACcIsgm2grzv0CqipK2A5GsjceoPxpyfMU6D FwtNY6FqA7jDlzPVLU6Gnj5GI1TNdWGJ3nA/PGxq5P3rvmkUUOKYJtZbkTvl061xaoA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FLjFqqbx; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B63F606A322; Fri, 4 Dec 2026 16:29:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=FLjFqqbx Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2FC041F00936; Fri, 4 Dec 2026 16:29:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401785; bh=0geHnZu0sVMZCqAE7MB+lL2GyXaqJTrlRbUHytT5wjw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FLjFqqbx2TaeYwJENqGeIi55w7q0eUKIQ7fIDG/+gpIMXlZXgceazypLRQCuZDYYn biK0lqWvQmrvUy8KwVIE3ajVB+KTXKvSkKWv0COZad9Nv77D8Bnz2n/CtNhDbxE+J8 gxuRtxy6V3pqElY988e5Fi3bFXnVAUTo06ET2c/7bPHcJvt1RntWx47HM6C2dMa/32 hmP9HZG4UcBC4wjUAj87pPj7+45tkQIWaXwZABwgxrU+l6WvE0Q37no15MWVR3ZK2s o7h2pP3izYIh4HTQvHbMufwMuNwOcbkfkrfHNtAvk/niqoO/NWQtFolnb/rLC1QD3l 90S+YctpEVH5A== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Nikolay Aleksandrov , Andrew Lunn , Ilias Apalodimas , Magnus Karlsson , Maciej Fijalkowski , Donald Hunter , Alexander Duyck , Saeed Mahameed , Tariq Toukan , Mark Bloch , Leon Romanovsky , Shuah Khan , Jonathan Corbet , netdev@vger.kernel.org, bpf@vger.kernel.org Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Simon Horman , Stanislav Fomichev , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Wei , Tao Cui , linux-kernel@vger.kernel.org, kernel-team@meta.com, "Mike Marciniszyn (Meta)" , linux-rdma@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan , Randy Dunlap , linux-doc@vger.kernel.org Subject: [PATCH net-next v2 14/15] selftests: net: xsk: Test explicit chunk return Date: Fri, 4 Dec 2026 17:27:19 +0100 Message-ID: <20261204162723.755897-15-bjorn@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org> References: <20261204162723.755897-1-bjorn@kernel.org> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit xskxceiver cannot test explicit chunk return yet, as it expects one chunk per packet at the UMEM headroom. Add -D (--explicit-return). The RX endpoint, in drv mode, registers its UMEM with XDP_UMEM_EXPLICIT_RETURN, checks bytes by packet offset, and refills a chunk only on XDP_RX_CHUNK_RETURNED. The TX endpoint takes -D too, as it keeps fewer packets in flight to such a receiver. -H BYTES (--headroom) fails a run in which a packet starts less than BYTES into its chunk. Cases that cannot run skip and say why. Run STAT_RX_FULL under -D too, and add RX_RING_FULL_RETURNS. With NETIF, xsk.py runs each case that tests the rx role once more with explicit return, e.g. test_xsk.er_SEND_RECEIVE. Also stop the cleanup of a UMEM that failed to register from unmapping address zero. Signed-off-by: Björn Töpel --- Notes (v2changes): Changes in v2: - Rename from "selftests: net: xsk: Test the device's RX buffer layout". - Rename --drv-layout (-D) to --explicit-return, and add -H BYTES (--headroom), a headroom check that needs -D. - Port to the reworked xsk selftests in net-next: one case per invocation, with -D on both endpoints, as the sender paces for an explicit-return receiver. drivers/net/xsk.py runs the rx-role cases with explicit return on NETIF (er_ variants), in place of the test_xsk.sh options. RX_RING_FULL_RETURNS lists the rx role. - Reserve FILL slots only for chunks that came back, so that the receive loop cannot spin (Sashiko). - Register only the receiving UMEM with the flag, with page-sized chunks also on 16K and 64K pages, and bind with XDP_USE_SG only where the device takes multi-buffer XDP (Sashiko). - Skip what the mode cannot run, and say why, among them STAT_FILL_EMPTY and HW_SW_MIN_RING_SIZE (Sashiko). A kernel or device without the mode, or a UMEM with too few chunks for the device, skips instead of failing or hanging. - Check empty heads with metadata, and count a packet only once it completes (Sashiko). - Check the sequence number of the last payload word unless the tail grew (Sashiko). - Clean up a UMEM that failed to register without unmapping address zero. - Size the too-large packets of the invalid-descriptor tests by the chunk, and let INVALID_DESC straddle a chunk on pages larger than 4K (Sashiko). - Skip -D on a device without native XDP (Sashiko). - Run STAT_RX_FULL under -D: the RX ring fills while the reader waits, and each return must come outside a packet, after data of its chunk. - Add RX_RING_FULL_RETURNS, a slow reader on a small RX ring. tools/testing/selftests/drivers/net/xsk.py | 28 +- .../testing/selftests/net/lib/xsk/test_xsk.c | 556 +++++++++++++++++- .../testing/selftests/net/lib/xsk/test_xsk.h | 9 + .../selftests/net/lib/xsk/xskxceiver.c | 57 +- 4 files changed, 617 insertions(+), 33 deletions(-) diff --git a/tools/testing/selftests/drivers/net/xsk.py b/tools/testing/selftests/drivers/net/xsk.py index ebde81fa5140..6c296e6bd257 100755 --- a/tools/testing/selftests/drivers/net/xsk.py +++ b/tools/testing/selftests/drivers/net/xsk.py @@ -34,6 +34,12 @@ them for IPv6, the case runs over IPv4. NETIF thus needs RSS, and both hosts ntuple filters. The cases in RING_SIZES set the rings that they test with ethtool -G. +With NETIF, each case that tests the rx role also runs with the local +endpoint receiving in drv mode with explicit chunk return +(XDP_UMEM_EXPLICIT_RETURN), e.g. test_xsk.er_SEND_RECEIVE. Both endpoints +take -D, as the sender paces for it. They skip the case if NETIF cannot back +the queue with an XDP-only memory provider. + The UNALIGNED cases need 32 huge pages of 2 MiB on both hosts, and are expected failures without them. """ @@ -74,8 +80,10 @@ HUGEPAGES = "/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages" MAX_SKB_FRAGS = "/proc/sys/net/core/max_skb_frags" # What xskxceiver takes when it cannot read MAX_SKB_FRAGS. DEFAULT_SKB_FRAGS = 17 -# The ip link option that detaches the XDP program of an endpoint mode. -XDP_OFF = {"skb": "xdpgeneric off", "drv": "xdpdrv off", "zc": "xdpdrv off"} +# The ip link option that detaches the XDP program of an endpoint mode, er +# being drv mode with explicit chunk return. +XDP_OFF = {"skb": "xdpgeneric off", "drv": "xdpdrv off", "zc": "xdpdrv off", + "er": "xdpdrv off"} # Why a case cannot pass over veth, by the mode, None for any, and the name. VETH_XFAIL = { # Generic XDP leaves no room to grow the tail, so the RX endpoint skips. @@ -134,6 +142,9 @@ def _get_cases(cfg): for direction in roles.split(): case = Case("zc", direction, int(test_id), name) ret.append(KsftNamedVariant(f"{direction}_{name}", case)) + if "rx" in roles.split(): + case = Case("er", "rx", int(test_id), name) + ret.append(KsftNamedVariant(f"er_{name}", case)) return ret @@ -228,12 +239,14 @@ def _steer(cfg, queue, udp_port, ctrl_port): raise KsftSkipEx("the hosts reject the ntuple rules of the test") -def _setup_nic(cfg, udp_port, ctrl_port): +def _setup_nic(cfg, case, udp_port, ctrl_port): """Take the last queue with both an RX and a TX ring out of RSS and steer the flows of a case until it ends. Return the queue and the frags per packet that the DUT takes in zero-copy mode.""" dev = cfg.netnl.dev_get({"ifindex": cfg.ifindex}) - if "xsk-zerocopy" not in dev["xdp-features"]: + # Explicit return needs no zero-copy support; xskxceiver skips if the + # bind fails. + if case.mode == "zc" and "xsk-zerocopy" not in dev["xdp-features"]: raise KsftSkipEx(f"{cfg.ifname} does not support AF_XDP zero-copy") req = {"header": {"dev-index": cfg.ifindex}} @@ -361,7 +374,7 @@ def _test(cfg, case, busy_poll): queue, max_frags = 0, _setup_veth(case) else: timeout, xsk_timeout = NIC_CASE_TIMEOUT, NIC_XSK_TIMEOUT - queue, max_frags = _setup_nic(cfg, udp_port, ctrl_port) + queue, max_frags = _setup_nic(cfg, case, udp_port, ctrl_port) if busy_poll: # So that SO_PREFER_BUSY_POLL of xskxceiver -b keeps the IRQs masked. @@ -392,12 +405,13 @@ def _test(cfg, case, busy_poll): "-t", str(case.test_id), "--udp-port", str(udp_port), "--max-frags", str(max_frags), - "--timeout", str(xsk_timeout)] + "--timeout", str(xsk_timeout), + *(["-D"] if case.mode == "er" else [])] local = ["timeout", "-k", "5", str(timeout), str(cfg.bin_local), "-i", cfg.ifname, "--queue", str(queue), "--listen", - "-m", case.mode, + "-m", "drv" if case.mode == "er" else case.mode, "-e", case.direction, *(["-b"] if busy_poll else []), "--src-ip", cfg.addr, diff --git a/tools/testing/selftests/net/lib/xsk/test_xsk.c b/tools/testing/selftests/net/lib/xsk/test_xsk.c index 2aadb95c61fa..8da7de41edf9 100644 --- a/tools/testing/selftests/net/lib/xsk/test_xsk.c +++ b/tools/testing/selftests/net/lib/xsk/test_xsk.c @@ -24,6 +24,8 @@ #include "xsk_xdp_progs.skel.h" #define DEFAULT_BATCH_SIZE 64 +/* Descriptors of an RX batch that count_drv_shared() compares */ +#define DRV_SHARE_WINDOW 64 /* Payload of the smallest packet, which is 64 bytes over IPv4 */ #define MIN_PAYLOAD_SIZE 20 #define MAX_ETH_JUMBO_SIZE 9000 @@ -53,6 +55,14 @@ static u32 thread_tmout = THREAD_TMOUT; bool opt_verbose; int min_pkt_size; +bool opt_explicit_return; +/* RX descriptors, and those whose chunk an earlier one of the batch holds */ +u64 drv_rx_descs, drv_rx_shared, drv_rx_empty_heads; +u64 drv_rx_empty_heads_freed; +/* Least headroom seen in front of a packet, and what the device leaves */ +u32 drv_rx_headroom = UINT_MAX, opt_headroom; +/* An explicit-return bind of a receiving socket worked in this run */ +static bool explicit_return_bound; int pkts_in_flight; static struct xsk_peer *ctrl_peer; @@ -262,6 +272,9 @@ int xsk_configure_umem(struct ifobject *ifobj, struct xsk_umem_info *umem, void if (umem->unaligned_mode) cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG; + /* Only the receiving UMEM has explicit return */ + if (opt_explicit_return && ifobj->rx_on) + cfg.flags |= XDP_UMEM_EXPLICIT_RETURN; ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq, &cfg); @@ -325,7 +338,11 @@ int xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem cfg.bind_flags = ifobject->bind_flags; if (shared) cfg.bind_flags |= XDP_SHARED_UMEM; - if (ifobject->mtu > MAX_ETH_PKT_SIZE) + /* A device with multi-buffer XDP may split any frame, see + * XDP_UMEM_EXPLICIT_RETURN + */ + if (ifobject->mtu > MAX_ETH_PKT_SIZE || + (opt_explicit_return && ifobj_has_cap(ifobject, XSK_CAP_MBUF))) cfg.bind_flags |= XDP_USE_SG; if (umem->comp_size) cfg.tx_size = umem->comp_size; @@ -385,6 +402,9 @@ static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx, ifobj->xsk->umem->num_frames = DEFAULT_UMEM_BUFFERS; ifobj->xsk->umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE; + /* Explicit return takes page-sized chunks */ + if (opt_explicit_return) + ifobj->xsk->umem->frame_size = getpagesize(); } test->ifobj_tx = ifobj_tx; @@ -394,6 +414,7 @@ static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx, test->nb_sockets = 1; test->adjust_tail = false; test->adjust_tail_support = false; + test->adjust_grow = 0; test->mtu = ETH_DATA_LEN; test->xdp_prog_rx = ifobj_rx->xdp_progs->progs.xsk_def_prog; test->xskmap_rx = ifobj_rx->xdp_progs->maps.xsk; @@ -419,9 +440,12 @@ void test_init(struct test_spec *test, struct ifobject *ifobj_tx, struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx; ifobj->bind_flags = XDP_USE_NEED_WAKEUP; + /* Explicit return is neither of the classic modes, and only the + * receiving socket has it + */ if (mode == TEST_MODE_ZC) ifobj->bind_flags |= XDP_ZEROCOPY; - else + else if (!opt_explicit_return || ifobj == ifobj_tx) ifobj->bind_flags |= XDP_COPY; } @@ -605,6 +629,15 @@ static u32 pkt_total_len(struct pkt_stream *pkt_stream, struct pkt *pkt, u32 nb_ return len; } +/* A packet that no chunk of @umem holds, and that is longer than any MTU */ +static u32 invalid_pkt_len(struct xsk_umem_info *umem) +{ + u32 len = umem->frame_size + 1; + + return len > XSK_UMEM__INVALID_FRAME_SIZE ? + len : XSK_UMEM__INVALID_FRAME_SIZE; +} + static bool set_pkt_valid(int offset, u32 len) { return len <= MAX_ETH_JUMBO_SIZE; @@ -1028,6 +1061,82 @@ static bool is_frag_valid(struct xsk_umem_info *umem, u64 addr, u32 len, u32 exp return false; } +/* Explicit return: a buffer may start and end anywhere in the packet, and + * anywhere in its chunk. Check its bytes against the generated pattern by + * packet offset. XDP_ADJUST_TAIL_GROW zeroes the last @grow bytes and + * writes the sequence number of the last word, so that word only needs the + * right sequence number, and the bytes before it may be zero. + */ +static bool is_frag_valid_drv(struct xsk_umem_info *umem, u64 addr, u32 len, + u32 expected_pkt_nb, u32 bytes_processed, + u32 pkt_len, u32 grow) +{ + u32 payload_end = PKT_HDR_SIZE + ((pkt_len - PKT_HDR_SIZE) & ~3U); + u8 *bytes = xsk_umem__get_data(umem->buffer, addr); + u64 umem_sz = umem_size(umem); + u32 i, off, word; + bool last; + + if (addr >= umem_sz || addr + len > umem_sz || + addr % umem->frame_size + len > umem->frame_size) { + ksft_print_msg("Frag invalid addr: %llx len: %u\n", + (unsigned long long)addr, len); + return false; + } + + for (i = 0; i < len; i++) { + off = bytes_processed + i; + if (off < PKT_HDR_SIZE || off >= payload_end) + continue; + + /* The last word lies in the grown tail, but must carry its + * sequence number, so it takes no zero exemption + */ + last = grow && (off - PKT_HDR_SIZE) / 4 == + (payload_end - PKT_HDR_SIZE) / 4 - 1; + if (!last && off >= pkt_len - grow && !bytes[i]) + continue; + + off -= PKT_HDR_SIZE; + word = htonl(expected_pkt_nb << 16 | (off / 4)); + if (last && off % 4 < 2) + continue; + if (bytes[i] == ((u8 *)&word)[off % 4]) + continue; + + ksft_print_msg("[%s] pkt %u byte %u (off %u): %#x, not %#x\n", + __func__, expected_pkt_nb, i, + (u32)(off + PKT_HDR_SIZE), bytes[i], + ((u8 *)&word)[off % 4]); + return false; + } + + return true; +} + +/* Explicit return: count descriptors whose chunk an earlier descriptor of the + * same RX batch holds, that is, chunks the device split. Only the first + * DRV_SHARE_WINDOW descriptors of a batch are compared. + */ +static void count_drv_shared(struct xsk_umem_info *umem, u64 *chunks, u32 n, + u64 addr) +{ + u64 chunk = addr / umem->frame_size; + u32 i; + + drv_rx_descs++; + if (n >= DRV_SHARE_WINDOW) + return; + + for (i = 0; i < n; i++) { + if (chunks[i] != chunk) + continue; + drv_rx_shared++; + break; + } + chunks[n] = chunk; +} + static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 frag_len, u32 len) { if (pkt->len != len) { @@ -1130,16 +1239,36 @@ static int complete_pkts(struct xsk_socket_info *xsk, int batch_size) return TEST_PASS; } +/* Explicit return: the descriptors of an RX batch that give a chunk back */ +static u32 count_chunk_free(struct xsk_socket_info *xsk, u32 idx, u32 rcvd) +{ + u32 i, n = 0; + + for (i = 0; i < rcvd; i++) + if (xsk_ring_cons__rx_desc(&xsk->rx, idx + i)->options & + XDP_RX_CHUNK_RETURNED) + n++; + return n; +} + static int __receive_pkts(struct test_spec *test, struct xsk_socket_info *xsk) { u32 frags_processed = 0, nb_frags = 0, pkt_len = 0, first_len = 0; u32 idx_rx = 0, idx_fq = 0, rcvd, pkts_sent = 0; + /* Explicit return: the statistics as they were before the packet */ + u64 descs = 0, shared = 0, heads = 0, heads_freed = 0; + /* Explicit return: FILL entries written, those of a partial packet */ + u32 fills = 0, pkt_fills = 0; struct pkt_stream *pkt_stream = xsk->pkt_stream; struct ifobject *ifobj = test->ifobj_rx; struct xsk_umem_info *umem = xsk->umem; struct pollfd fds = { }; + u64 batch_chunks[DRV_SHARE_WINDOW]; + /* Entries of batch_chunks[]: data descriptors only */ + u32 batch_n = 0; struct pkt *pkt; u64 first_addr = 0; + u32 nb_fq; int ret; fds.fd = xsk_socket__fd(xsk->xsk); @@ -1173,15 +1302,22 @@ static int __receive_pkts(struct test_spec *test, struct xsk_socket_info *xsk) if (!rcvd) return TEST_CONTINUE; + /* Explicit return: only a descriptor with XDP_RX_CHUNK_RETURNED gives a + * chunk back, and a chunk may hold more buffers than FILL has free + * slots + */ + nb_fq = rcvd; + if (opt_explicit_return) + nb_fq = count_chunk_free(xsk, idx_rx, rcvd); if (ifobj->use_fill_ring) { - ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq); - while (ret != rcvd) { + ret = xsk_ring_prod__reserve(&umem->fq, nb_fq, &idx_fq); + while (ret != nb_fq) { if (xsk_ring_prod__needs_wakeup(&umem->fq)) { ret = poll(&fds, 1, POLL_TMOUT); if (ret < 0) return TEST_FAILURE; } - ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq); + ret = xsk_ring_prod__reserve(&umem->fq, nb_fq, &idx_fq); } } @@ -1192,6 +1328,47 @@ static int __receive_pkts(struct test_spec *test, struct xsk_socket_info *xsk) orig = xsk_umem__extract_addr(addr); addr = xsk_umem__add_offset_to_addr(addr); + if (!nb_frags) { + descs = drv_rx_descs; + shared = drv_rx_shared; + heads = drv_rx_empty_heads; + heads_freed = drv_rx_empty_heads_freed; + } + + /* Explicit return: a descriptor without data gives a chunk + * back, and one with XDP_PKT_CONTD is the head of a packet + * whose data follows, which carries the metadata. + */ + if (opt_explicit_return && !desc->len && + (desc->options & XDP_PKT_CONTD)) { + if (nb_frags) { + ksft_print_msg("[%s] nested empty head %llx\n", + __func__, + (unsigned long long)addr); + return TEST_FAILURE; + } + drv_rx_empty_heads++; + /* The chunk of an empty head may come back with it */ + if (desc->options & XDP_RX_CHUNK_RETURNED) + drv_rx_empty_heads_freed++; + } else if (opt_explicit_return && !desc->len) { + if (nb_frags || + !(desc->options & XDP_RX_CHUNK_RETURNED)) { + ksft_print_msg("[%s] empty %llx, options %u\n", + __func__, + (unsigned long long)addr, + desc->options); + return TEST_FAILURE; + } + if (ifobj->use_fill_ring) { + *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = + orig - orig % umem->frame_size; + fills++; + } + frags_processed++; + continue; + } + if (!nb_frags) { pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &pkts_sent); if (!pkt) { @@ -1204,48 +1381,108 @@ static int __receive_pkts(struct test_spec *test, struct xsk_socket_info *xsk) print_verbose("Rx: addr: %lx len: %u options: %u pkt_nb: %u valid: %u\n", addr, desc->len, desc->options, pkt->pkt_nb, pkt->valid); - if (!is_frag_valid(umem, addr, desc->len, pkt->pkt_nb, pkt_len) || - !is_offset_correct(umem, pkt, addr) || (ifobj->use_metadata && - !is_metadata_correct(pkt, umem->buffer, addr))) + if (opt_explicit_return) { + /* The device picks offsets; metadata precedes the + * head + */ + if (!is_frag_valid_drv(umem, addr, desc->len, + pkt->pkt_nb, pkt_len, + pkt->len, test->adjust_grow) || + (ifobj->use_metadata && !nb_frags && + !is_metadata_correct(pkt, umem->buffer, addr))) + return TEST_FAILURE; + /* Empty heads are counted on their own */ + if (desc->len) + count_drv_shared(umem, batch_chunks, batch_n++, + addr); + } else if (!is_frag_valid(umem, addr, desc->len, pkt->pkt_nb, + pkt_len) || + !is_offset_correct(umem, pkt, addr) || + (ifobj->use_metadata && + !is_metadata_correct(pkt, umem->buffer, addr))) { return TEST_FAILURE; + } - if (!nb_frags++) { + if (!nb_frags) { + u32 headroom = addr % umem->frame_size; + + /* The first buffer keeps the headroom of the + * device, as native XDP has it. Its offset in the + * chunk bounds that headroom from above: userspace + * cannot see where the device starts a buffer. + */ + if (opt_explicit_return) { + if (headroom < drv_rx_headroom) + drv_rx_headroom = headroom; + if (headroom < opt_headroom) { + ksft_print_msg("[%s] headroom %u < %u at %llx\n", + __func__, headroom, + opt_headroom, + (unsigned long long)addr); + return TEST_FAILURE; + } + } first_addr = addr; first_len = desc->len; } + nb_frags++; frags_processed++; pkt_len += desc->len; - if (ifobj->use_fill_ring) + if (ifobj->use_fill_ring && !opt_explicit_return) { *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = orig; + } else if (ifobj->use_fill_ring && + desc->options & XDP_RX_CHUNK_RETURNED) { + /* Only a returned chunk goes back to FILL */ + *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = + orig - orig % umem->frame_size; + fills++; + pkt_fills++; + } if (pkt_continues(desc->options)) continue; - /* The complete packet has been received */ + /* The complete packet has been received. With explicit + * return, the first buffer may hold only part of the header. + */ + if (opt_explicit_return && first_len < PKT_HDR_SIZE) + first_len = PKT_HDR_SIZE; if (!is_pkt_valid(pkt, umem->buffer, first_addr, first_len, pkt_len) || - !is_offset_correct(umem, pkt, addr)) + (!opt_explicit_return && + !is_offset_correct(umem, pkt, addr))) return TEST_FAILURE; pkt_stream->nb_rx_pkts++; nb_frags = 0; pkt_len = 0; + pkt_fills = 0; } if (nb_frags) { /* In the middle of a packet. Start over from beginning of packet. */ idx_rx -= nb_frags; xsk_ring_cons__cancel(&xsk->rx, nb_frags); - if (ifobj->use_fill_ring) { + if (ifobj->use_fill_ring && !opt_explicit_return) { idx_fq -= nb_frags; xsk_ring_prod__cancel(&umem->fq, nb_frags); } frags_processed -= nb_frags; + fills -= pkt_fills; + /* Its descriptors count once it completes */ + drv_rx_descs = descs; + drv_rx_shared = shared; + drv_rx_empty_heads = heads; + drv_rx_empty_heads_freed = heads_freed; pkt_stream_cancel(pkt_stream); pkts_sent--; } - if (ifobj->use_fill_ring) + if (ifobj->use_fill_ring && opt_explicit_return) { + xsk_ring_prod__cancel(&umem->fq, nb_fq - fills); + xsk_ring_prod__submit(&umem->fq, fills); + } else if (ifobj->use_fill_ring) { xsk_ring_prod__submit(&umem->fq, frags_processed); + } if (ifobj->release_rx) xsk_ring_cons__release(&xsk->rx, frags_processed); @@ -1316,22 +1553,32 @@ static int receive_pkts(struct test_spec *test) } static int __send_pkts(struct ifobject *ifobject, struct xsk_socket_info *xsk, - bool test_timeout) + bool test_timeout, bool drv_rx) { u32 i, idx = 0, valid_pkts = 0, valid_frags = 0, buffer_len; struct xsk_umem_info *umem = ifobject->xsk_arr[0].umem; struct pkt_stream *pkt_stream = xsk->pkt_stream; bool use_poll = ifobject->use_poll; struct pollfd fds = { }; - int ret; + int ret, max_in_flight; buffer_len = pkt_get_buffer_len(umem, pkt_stream->max_pkt_len); if (pacing_tx_sync()) return TEST_FAILURE; + max_in_flight = (umem_size(umem) - xsk->batch_size * buffer_len) / + buffer_len; + /* A receiver with explicit return needs more: its queue + * also holds chunks without packets, posted on rings that the + * traffic does not fill, such as a header ring, and in the page + * pool cache. And a packet takes an RX descriptor per buffer, and + * empty ones that give chunks back. Keep a quarter of the frames in + * flight to such a receiver. + */ + if (drv_rx) + max_in_flight /= 4; /* pkts_in_flight might be negative if many invalid packets are sent */ - if (pkts_in_flight >= (int)((umem_size(umem) - xsk->batch_size * buffer_len) / - buffer_len) && !test_timeout) { + if (pkts_in_flight >= max_in_flight && !test_timeout) { /* Only RX progress reports can lower pkts_in_flight. */ if (xsk_peer_skipped(ctrl_peer)) { ksft_print_msg("RX skipped the case with %d packets in flight\n", @@ -1484,6 +1731,7 @@ bool all_packets_sent(struct test_spec *test, unsigned long *bitmap) static int send_pkts(struct test_spec *test, struct ifobject *ifobject) { + bool drv_rx = opt_explicit_return && test->ifobj_rx->use_fill_ring; DECLARE_BITMAP(bitmap, test->nb_sockets); u32 i, ret; @@ -1498,7 +1746,8 @@ static int send_pkts(struct test_spec *test, struct ifobject *ifobject) __set_bit(i, bitmap); continue; } - ret = __send_pkts(ifobject, &ifobject->xsk_arr[i], test->poll_tmout); + ret = __send_pkts(ifobject, &ifobject->xsk_arr[i], + test->poll_tmout, drv_rx); if (ret != TEST_CONTINUE) return ret; @@ -1611,6 +1860,124 @@ static int validate_rx_full(struct ifobject *ifobject) return wait_rx_stat(ifobject, false); } +/* Explicit return: packets that find the RX ring full are dropped, never a + * return, as the ring keeps room for them. Waits @rx_full_wait_us, and then + * drains one batch every @rx_full_step_us until the ring stays empty. Each + * return must come outside a packet and after a data descriptor of its + * chunk, and every chunk with data must come back, but for the few that the + * device still fills. Those come back only once the device lets go of them, + * or without a descriptor once the queue leaves the UMEM, which the test + * cannot see. + */ +static u32 rx_full_wait_us, rx_full_step_us; +#define RX_FULL_IDLE_POLLS 100 +#define RX_FULL_SLACK 16 + +static int validate_rx_full_returns(struct ifobject *ifobject) +{ + u32 i, idx, idx_fq = 0, rcvd, nb_fq, idle = 0, chunk, fills, tries; + struct xsk_socket_info *xsk = ifobject->xsk; + struct xsk_umem_info *umem = xsk->umem; + u32 returns = 0, outstanding = 0, nr; + bool in_pkt = false, ret_only; + const struct xdp_desc *desc; + struct xdp_statistics stats; + int ret = TEST_FAILURE; + struct pollfd fds = { + .fd = xsk_socket__fd(xsk->xsk), + .events = POLLIN, + }; + __u64 *fill; + u64 addr; + u8 *seen; + + nr = umem->num_frames; + seen = calloc(nr, sizeof(*seen)); + if (!seen) + return TEST_FAILURE; + + usleep(rx_full_wait_us); + while (idle < RX_FULL_IDLE_POLLS) { + if (kick_rx(xsk)) + goto out; + rcvd = xsk_ring_cons__peek(&xsk->rx, xsk->batch_size, &idx); + if (!rcvd) { + idle++; + usleep(1000); + continue; + } + idle = 0; + + nb_fq = count_chunk_free(xsk, idx, rcvd); + tries = 0; + while (xsk_ring_prod__reserve(&umem->fq, nb_fq, &idx_fq) != + nb_fq) { + if (++tries > RX_FULL_IDLE_POLLS) { + ksft_print_msg("[%s] FILL stays full\n", + __func__); + goto out; + } + if (xsk_ring_prod__needs_wakeup(&umem->fq) && + poll(&fds, 1, POLL_TMOUT) < 0) + goto out; + usleep(1000); + } + + fills = 0; + for (i = 0; i < rcvd; i++) { + desc = xsk_ring_cons__rx_desc(&xsk->rx, idx + i); + addr = xsk_umem__extract_addr(desc->addr); + if (addr >= umem_size(umem)) { + ksft_print_msg("[%s] %llx outside the UMEM\n", + __func__, desc->addr); + goto out; + } + chunk = addr / umem->frame_size; + ret_only = !desc->len && + !(desc->options & XDP_PKT_CONTD); + if (ret_only && (in_pkt || !seen[chunk])) { + ksft_print_msg("[%s] misplaced return of %u\n", + __func__, chunk); + goto out; + } + returns += ret_only; + if (!ret_only) + seen[chunk] = 1; + if (desc->options & XDP_RX_CHUNK_RETURNED) { + seen[chunk] = 0; + fill = xsk_ring_prod__fill_addr(&umem->fq, + idx_fq++); + *fill = addr - addr % umem->frame_size; + fills++; + } + in_pkt = desc->options & XDP_PKT_CONTD; + } + xsk_ring_prod__submit(&umem->fq, fills); + xsk_ring_cons__release(&xsk->rx, rcvd); + usleep(rx_full_step_us); + } + + if (get_xsk_stats(xsk->xsk, &stats)) + goto out; + for (i = 0; i < nr; i++) + outstanding += seen[i]; + print_verbose("rx_ring_full %llu returns %u outstanding %u\n", + stats.rx_ring_full, returns, outstanding); + if (!stats.rx_ring_full) { + ksft_print_msg("[%s] the RX ring never filled\n", __func__); + goto out; + } + if (outstanding > RX_FULL_SLACK) { + ksft_print_msg("[%s] %u chunks with data did not come back\n", + __func__, outstanding); + goto out; + } + ret = TEST_PASS; +out: + free(seen); + return ret; +} + static int validate_fill_empty(struct ifobject *ifobject) { return wait_rx_stat(ifobject, true); @@ -1754,8 +2121,16 @@ static int thread_common_ops(struct test_spec *test, struct ifobject *ifobject) umem->mmap_size = mmap_sz; ret = xsk_configure_umem(ifobject, umem, bufs, umem_sz); - if (ret) + if (ret) { + munmap(bufs, mmap_sz); + /* A kernel without explicit return refuses the UMEM flag */ + if (ret == -EINVAL && opt_explicit_return && + ifobject->rx_on && !explicit_return_bound) { + ksft_print_msg("No explicit return in the kernel?\n"); + return TEST_SKIP; + } return ret; + } ifobject->xsk = &ifobject->xsk_arr[0]; @@ -1771,8 +2146,24 @@ static int thread_common_ops(struct test_spec *test, struct ifobject *ifobject) } ret = xsk_configure(test, ifobject, umem); + /* The queue's page pool may need more chunks than the UMEM has */ + if (ret == -ENOSPC && opt_explicit_return && ifobject->rx_on) { + ksft_print_msg("%llu chunks are too few for the device\n", + (unsigned long long)umem_sz / umem->frame_size); + return TEST_SKIP; + } + /* Without CAP_NET_ADMIN, or on a device that cannot take it. Once a + * receiving bind worked, the device can, and a refusal is a failure. + */ + if ((ret == -EOPNOTSUPP || ret == -EPERM) && opt_explicit_return && + ifobject->rx_on && !explicit_return_bound) { + ksft_print_msg("Bind failed: %s\n", strerror(-ret)); + return TEST_SKIP; + } if (ret) return ret; + if (opt_explicit_return && ifobject->rx_on) + explicit_return_bound = true; if (!ifobject->rx_on) return 0; @@ -1915,6 +2306,11 @@ int worker_testapp_validate_rx(struct test_spec *test) int err; err = testapp_prepare_rx_endpoint(test, ifobject); + /* A bind the device cannot take is a skip, which main() tells the + * peer of + */ + if (err == TEST_SKIP) + return err; if (err) { ksft_print_msg("RX endpoint setup failed at step %u: %d\n", test->current_step, err); @@ -1940,7 +2336,13 @@ static void testapp_clean_xsk_umem(struct ifobject *ifobj) struct xsk_umem_info *umem = ifobj->xsk->umem; xsk_umem__delete(umem->umem); - munmap(umem->buffer, umem->mmap_size); + umem->umem = NULL; + /* No buffer if registration failed: munmap(NULL) would unmap the + * program + */ + if (umem->buffer) + munmap(umem->buffer, umem->mmap_size); + umem->buffer = NULL; } static bool xdp_prog_changed_rx(struct test_spec *test) @@ -2038,12 +2440,40 @@ static void clean_umem(struct test_spec *test, struct ifobject *ifobj1, struct i testapp_clean_xsk_umem(ifobj2); } +/* Explicit return takes one receiving socket per queue */ +static bool skip_second_receiver(void) +{ + if (!opt_explicit_return) + return false; + ksft_print_msg("One socket receives per queue with explicit return\n"); + return true; +} + +/* UMEM with explicit return: page chunks, no headroom */ +static bool explicit_return_umem_ok(struct ifobject *ifobj) +{ + struct xsk_umem_info *umem = ifobj->xsk->umem; + + /* A TX UMEM of its own has no explicit return */ + if (!ifobj->rx_on) + return true; + return umem->frame_size == getpagesize() && !umem->frame_headroom && + !umem->unaligned_mode; +} + static int __testapp_validate_traffic(struct test_spec *test, struct ifobject *ifobj1, struct ifobject *ifobj2) { u32 mbuf_cap; int err; + if (opt_explicit_return && + (!explicit_return_umem_ok(ifobj1) || + (ifobj2 && !explicit_return_umem_ok(ifobj2)))) { + ksft_print_msg("-D needs page chunks without headroom\n"); + return TEST_SKIP; + } + if (test->mtu <= MAX_ETH_PKT_SIZE) goto skip_mbuf_check; @@ -2147,6 +2577,14 @@ int testapp_bidirectional(struct test_spec *test) { int res; + /* The sending side binds in copy mode, and on one netdev it would + * also receive on the queue of the other + */ + if (opt_explicit_return) { + ksft_print_msg("-D: no copy mode, one receiver per queue\n"); + return TEST_SKIP; + } + test->ifobj_tx->rx_on = true; test->ifobj_rx->tx_on = true; test->total_steps = 2; @@ -2187,6 +2625,9 @@ int testapp_xdp_prog_cleanup(struct test_spec *test) { int ret; + if (skip_second_receiver()) + return TEST_SKIP; + test->total_steps = 2; test->nb_sockets = 2; ret = testapp_validate_traffic(test); @@ -2231,16 +2672,50 @@ int testapp_stats_rx_dropped(struct test_spec *test) int testapp_stats_tx_invalid_descs(struct test_spec *test) { - if (pkt_stream_replace_half(test, XSK_UMEM__INVALID_FRAME_SIZE, 0)) + u32 len = invalid_pkt_len(test->ifobj_tx->xsk->umem); + + if (pkt_stream_replace_half(test, len, 0)) return TEST_FAILURE; test->ifobj_tx->validation_func = validate_tx_invalid_descs; return testapp_validate_traffic(test); } +/* With explicit return, returns take RX slots too, so the number of + * packets that fit is not known: the receive loop takes none, and the + * validation drains the ring, see validate_rx_full_returns(). The sender + * keeps fewer packets in flight than it may. + */ +static int testapp_rx_full_returns(struct test_spec *test, u32 rxqsize, + u32 wait_us, u32 step_us) +{ + struct pkt_stream *tmp; + + tmp = pkt_stream_generate(DEFAULT_UMEM_BUFFERS / 8, MIN_PKT_SIZE); + if (!tmp) + return TEST_FAILURE; + test->ifobj_tx->xsk->pkt_stream = tmp; + + tmp = pkt_stream_generate(DEFAULT_UMEM_BUFFERS / 8, MIN_PKT_SIZE); + if (!tmp) + return TEST_FAILURE; + tmp->nb_valid_entries = 0; + test->ifobj_rx->xsk->pkt_stream = tmp; + + rx_full_wait_us = wait_us; + rx_full_step_us = step_us; + test->ifobj_rx->xsk->rxqsize = rxqsize; + test->ifobj_rx->validation_func = validate_rx_full_returns; + return testapp_validate_traffic(test); +} + int testapp_stats_rx_full(struct test_spec *test) { struct pkt_stream *tmp; + if (opt_explicit_return) + return testapp_rx_full_returns(test, DEFAULT_BATCH_SIZE, + 200 * 1000, 0); + tmp = pkt_stream_generate(DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE); if (!tmp) return TEST_FAILURE; @@ -2257,10 +2732,29 @@ int testapp_stats_rx_full(struct test_spec *test) return testapp_validate_traffic(test); } +/* A small RX ring that a slow reader drains one batch at a time */ +int testapp_rx_ring_full_returns(struct test_spec *test) +{ + if (!opt_explicit_return) { + ksft_print_msg("Returns take RX slots only with -D\n"); + return TEST_SKIP; + } + + test->ifobj_rx->xsk->batch_size = DEFAULT_BATCH_SIZE / 4; + return testapp_rx_full_returns(test, DEFAULT_BATCH_SIZE / 4, + 50 * 1000, 2000); +} + int testapp_stats_fill_empty(struct test_spec *test) { struct pkt_stream *tmp; + /* Several packets may share a chunk, so FILL runs dry later */ + if (opt_explicit_return) { + ksft_print_msg("FILL entries are not packets with -D\n"); + return TEST_SKIP; + } + tmp = pkt_stream_generate(DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE); if (!tmp) return TEST_FAILURE; @@ -2350,6 +2844,8 @@ int testapp_invalid_desc_mb(struct test_spec *test) pkts[15].valid = true; pkts[16].valid = true; } + /* Too large for a chunk, also for chunks larger than an MTU */ + pkts[8].len = invalid_pkt_len(umem); test->mtu = MAX_ETH_JUMBO_SIZE; if (pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts))) @@ -2391,6 +2887,12 @@ int testapp_invalid_desc(struct test_spec *test) /* Crossing a 2K frame size boundary not allowed */ pkts[8].valid = false; } + if (!umem->unaligned_mode && umem->frame_size > 0x1000) { + /* Page-sized chunks larger than 4K: straddle a chunk instead */ + pkts[7].offset = umem->frame_size - MIN_PKT_SIZE / 2; + } + /* Too large for a chunk, also for chunks larger than an MTU */ + pkts[3].len = invalid_pkt_len(umem); if (pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts))) return TEST_FAILURE; @@ -2431,6 +2933,9 @@ int testapp_xdp_shared_umem(struct test_spec *test) struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; int ret; + if (skip_second_receiver()) + return TEST_SKIP; + test->total_steps = 1; test->nb_sockets = 2; @@ -2701,6 +3206,11 @@ int testapp_hw_sw_min_ring_size(struct test_spec *test) ksft_print_msg("HW ring sizes not supported.\n"); return TEST_SKIP; } + /* A batch of one never completes a packet of several descriptors */ + if (opt_explicit_return) { + ksft_print_msg("A packet may span descriptors with -D\n"); + return TEST_SKIP; + } test->total_steps = 2; test->ifobj_tx->xsk->batch_size = 1; @@ -2789,6 +3299,8 @@ static int testapp_adjust_tail(struct test_spec *test, u32 value, u32 pkt_len) test->adjust_tail_support = true; test->adjust_tail = true; + /* A negative value shrinks */ + test->adjust_grow = (int)value > 0 ? value : 0; test->total_steps = 1; ret = pkt_stream_replace_ifobject(test->ifobj_tx, DEFAULT_BATCH_SIZE, pkt_len); diff --git a/tools/testing/selftests/net/lib/xsk/test_xsk.h b/tools/testing/selftests/net/lib/xsk/test_xsk.h index 872e15206b48..7d73df25e819 100644 --- a/tools/testing/selftests/net/lib/xsk/test_xsk.h +++ b/tools/testing/selftests/net/lib/xsk/test_xsk.h @@ -36,6 +36,11 @@ extern bool opt_verbose; /* The smallest test packet, set by xsk_set_flow() */ extern int min_pkt_size; +/* XDP_UMEM_EXPLICIT_RETURN: the device lays out RX buffers in UMEM chunks */ +extern bool opt_explicit_return; +extern u64 drv_rx_descs, drv_rx_shared, drv_rx_empty_heads; +extern u64 drv_rx_empty_heads_freed; +extern u32 drv_rx_headroom, opt_headroom; #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0) @@ -234,6 +239,7 @@ struct test_spec { struct bpf_map *xskmap_tx; test_func_t test_func; int mtu; + u32 adjust_grow; u16 total_steps; u16 current_step; u16 nb_sockets; @@ -288,6 +294,7 @@ int testapp_stats_fill_empty(struct test_spec *test); int testapp_stats_rx_dropped(struct test_spec *test); int testapp_stats_tx_invalid_descs(struct test_spec *test); int testapp_stats_rx_full(struct test_spec *test); +int testapp_rx_ring_full_returns(struct test_spec *test); int testapp_teardown(struct test_spec *test); int testapp_too_many_frags(struct test_spec *test); int testapp_tx_queue_consumer(struct test_spec *test); @@ -336,6 +343,8 @@ static const struct test_spec hw_rx_tests[] = { {.name = "XDP_ADJUST_TAIL_SHRINK_MULTI_BUFF", .test_func = testapp_adjust_tail_shrink_mb}, {.name = "XDP_ADJUST_TAIL_GROW", .test_func = testapp_adjust_tail_grow}, {.name = "XDP_ADJUST_TAIL_GROW_MULTI_BUFF", .test_func = testapp_adjust_tail_grow_mb}, + {.name = "RX_RING_FULL_RETURNS", + .test_func = testapp_rx_ring_full_returns}, }; static const struct test_spec hw_tx_tests[] = { diff --git a/tools/testing/selftests/net/lib/xsk/xskxceiver.c b/tools/testing/selftests/net/lib/xsk/xskxceiver.c index bc92de5e8323..db58df137b64 100644 --- a/tools/testing/selftests/net/lib/xsk/xskxceiver.c +++ b/tools/testing/selftests/net/lib/xsk/xskxceiver.c @@ -158,6 +158,8 @@ static struct option long_options[] = { {"queue", required_argument, 0, OPT_QUEUE}, {"max-frags", required_argument, 0, OPT_MAX_FRAGS}, {"timeout", required_argument, 0, OPT_TIMEOUT}, + {"explicit-return", no_argument, 0, 'D'}, + {"headroom", required_argument, 0, 'H'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; @@ -186,6 +188,10 @@ static void print_usage(char **argv) " --queue N AF_XDP queue to bind (default 0)\n" " --max-frags N Fragment limit for TOO_MANY_FRAGS (default by mode)\n" " --timeout SEC Traffic, link and bind timeout (default 3)\n" + " -D, --explicit-return The RX endpoint returns chunks explicitly (drv\n" + " mode); both endpoints take it\n" + " -H, --headroom BYTES Least headroom the device leaves in front of a\n" + " packet, with -D\n" " -h, --help Display this help and exit\n"; ksft_print_msg(str, basename(argv[0])); @@ -247,6 +253,7 @@ static const struct test_spec *find_test(u32 id) static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc, char **argv) { + bool headroom = false; int option_index, c; unsigned long val; char *end; @@ -254,7 +261,8 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj opterr = 0; for (;;) { - c = getopt_long(argc, argv, "i:vbm:lt:e:p:P:", long_options, &option_index); + c = getopt_long(argc, argv, "i:vbm:lt:e:p:P:DH:", long_options, + &option_index); if (c == -1) break; @@ -347,6 +355,19 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj print_usage(argv); opt_timeout = val; break; + case 'D': + opt_explicit_return = true; + break; + case 'H': + errno = 0; + val = strtoul(optarg, &end, 0); + /* Headroom lies within a page-sized chunk */ + if (errno || end == optarg || *end || + val >= (unsigned long)getpagesize()) + print_usage(argv); + opt_headroom = val; + headroom = true; + break; case 'h': default: print_usage(argv); @@ -358,6 +379,16 @@ static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj opt_mode == TEST_MODE_ALL || !opt_src_ip || !opt_dst_ip || !opt_dst_mac || !opt_udp_port) print_usage(argv); + + /* Only explicit chunk return leaves the device the headroom */ + if (headroom && !opt_explicit_return) + print_usage(argv); + /* Explicit return is a drv mode feature of the receiving endpoint. + * The sending endpoint takes -D to send as the case needs it. + */ + if (opt_explicit_return && opt_endpoint_role == XSK_ENDPOINT_RX && + opt_mode != TEST_MODE_DRV) + print_usage(argv); } static void xsk_unload_xdp_programs(struct ifobject *ifobj) @@ -370,6 +401,14 @@ static int run_pkt_test(struct test_spec *test) int ret; ret = test->test_func(test); + if (opt_explicit_return && opt_endpoint_role == XSK_ENDPOINT_RX) + ksft_print_msg("explicit return: rx descs %llu, in a chunk shared within an RX batch %llu, empty heads %llu (%llu freed), least headroom %u\n", + (unsigned long long)drv_rx_descs, + (unsigned long long)drv_rx_shared, + (unsigned long long)drv_rx_empty_heads, + (unsigned long long)drv_rx_empty_heads_freed, + drv_rx_headroom == UINT_MAX ? 0 : + drv_rx_headroom); pkt_stream_restore_default(test); return ret; } @@ -468,9 +507,19 @@ static int cleanup_iface(struct ifobject *ifobj) if (!ifobj_is_local(ifobj)) goto unload; - if (ifobj->xdp_prog) - xsk_detach_xdp_program(ifobj->ifindex, - mode_to_xdp_flags(ifobj->mode)); + /* With explicit return, the queue keeps its program (EBUSY) until the + * kernel has released the pool of the closed socket from a work item + */ + if (ifobj->xdp_prog) { + int wait_ms = opt_timeout * 1000; + + while (bpf_xdp_detach(ifobj->ifindex, + mode_to_xdp_flags(ifobj->mode), + NULL) == -EBUSY && wait_ms > 0) { + usleep(10 * 1000); + wait_ms -= 10; + } + } /* After the detach, as an attached program can cap the MTU. A driver * can also refuse it while it holds the pool of a closed socket, which * the kernel releases from a work item. -- 2.56.1