xdp_helper only binds an XDP socket and waits. Tests in selftests/net have no way to send a frame through the AF_XDP TX path. Add a -t option that takes one Ethernet frame in hex. With -t, the helper also creates a TX ring, sends the frame and exits. It returns 1 if sendto() fails. Without -t, nothing changes. The test passes the whole frame in hex, so xdp_helper does not need to know about IP or UDP. -t binds the socket in copy mode. Copy mode sends the frame within sendto(), so the helper can exit right after it. In zerocopy mode, sendto() only wakes up the driver, which may send the frame after the helper has exited and closed the socket. Also, the extra headroom is added when copy mode allocates an skb for the frame in xsk_build_skb(). send_frame() follows tools/testing/selftests/net/lib/xsk/xsk.c and tools/testing/selftests/bpf/prog_tests/xdp_metadata.c. XDP_PGOFF_TX_RING (0x80000000) does not fit in a 32-bit off_t. xsk.c also passes it to mmap() without _FILE_OFFSET_BITS, so xdp_helper does not define it either. Signed-off-by: Tianyi Gao --- v3: - Bind in copy mode with -t (Sashiko). v2: - Exit after sendto() and do not wait for the TX completion, as xdp_metadata.c does. - Drop the _FILE_OFFSET_BITS define, as xsk.c does. --- tools/testing/selftests/net/lib/xdp_helper.c | 83 ++++++++++++++++++-- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/net/lib/xdp_helper.c b/tools/testing/selftests/net/lib/xdp_helper.c index eb025a9f35b1..e17768816032 100644 --- a/tools/testing/selftests/net/lib/xdp_helper.c +++ b/tools/testing/selftests/net/lib/xdp_helper.c @@ -19,15 +19,65 @@ static void print_usage(const char *bin) { - fprintf(stderr, "Usage: %s ifindex queue_id [-z]\n\n" - "where:\n\t-z: force zerocopy mode", bin); + fprintf(stderr, "Usage: %s ifindex queue_id [-z] [-t frame_hex]\n\n" + "where:\n\t-z: force zerocopy mode\n" + "\t-t: send one frame, given in hex, and exit\n", bin); +} + +static int parse_frame(unsigned char *buf, const char *hex) +{ + size_t len = strlen(hex) / 2; + size_t i; + + if (!len || len > 2048 || strlen(hex) % 2) + return -1; + for (i = 0; i < len; i++) + if (sscanf(hex + 2 * i, "%2hhx", &buf[i]) != 1) + return -1; + return len; +} + +static int send_frame(int sock_fd, __u32 len) +{ + struct xdp_mmap_offsets off; + socklen_t optlen = sizeof(off); + struct xdp_desc *desc; + size_t tx_sz; + int ret = 0; + char *tx; + + if (getsockopt(sock_fd, SOL_XDP, XDP_MMAP_OFFSETS, &off, &optlen)) { + perror("getsockopt failed"); + return 1; + } + + tx_sz = off.tx.desc + NUM_DESC * sizeof(*desc); + tx = mmap(NULL, tx_sz, PROT_READ | PROT_WRITE, MAP_SHARED, sock_fd, + XDP_PGOFF_TX_RING); + if (tx == MAP_FAILED) { + perror("TX ring mmap failed"); + return 1; + } + + desc = (struct xdp_desc *)(tx + off.tx.desc); + desc->addr = 0; + desc->len = len; + __atomic_store_n((__u32 *)(tx + off.tx.producer), 1, __ATOMIC_RELEASE); + + if (sendto(sock_fd, NULL, 0, MSG_DONTWAIT, NULL, 0) < 0) { + perror("sendto failed"); + ret = 1; + } + + munmap(tx, tx_sz); + return ret; } /* this is a simple helper program that creates an XDP socket and does the * minimum necessary to get bind() to succeed. * - * this test program is not intended to actually process packets, but could be - * extended in the future if that is actually needed. + * without -t it does not process packets. with -t it sends one frame + * through the TX ring and exits. * * it is used by queues.py to ensure the xsk netlinux attribute is set * correctly. @@ -37,13 +87,15 @@ int main(int argc, char **argv) struct xdp_umem_reg umem_reg = { 0 }; struct sockaddr_xdp sxdp = { 0 }; int num_desc = NUM_DESC; + int frame_len = 0; void *umem_area; int retry = 0; int ifindex; int sock_fd; int queue; + int i; - if (argc != 3 && argc != 4) { + if (argc < 3) { print_usage(argv[0]); return 1; } @@ -94,9 +146,21 @@ int main(int argc, char **argv) sxdp.sxdp_queue_id = queue; sxdp.sxdp_flags = 0; - if (argc > 3) { - if (!strcmp(argv[3], "-z")) { - sxdp.sxdp_flags = XDP_ZEROCOPY; + for (i = 3; i < argc; i++) { + if (!strcmp(argv[i], "-z")) { + sxdp.sxdp_flags |= XDP_ZEROCOPY; + } else if (!strcmp(argv[i], "-t") && i + 1 < argc) { + sxdp.sxdp_flags |= XDP_COPY; + frame_len = parse_frame(umem_area, argv[++i]); + if (frame_len < 0) { + print_usage(argv[0]); + return 1; + } + if (setsockopt(sock_fd, SOL_XDP, XDP_TX_RING, &num_desc, + sizeof(num_desc))) { + perror("setsockopt XDP_TX_RING failed"); + return 1; + } } else { print_usage(argv[0]); return 1; @@ -119,6 +183,9 @@ int main(int argc, char **argv) } } + if (frame_len) + return send_frame(sock_fd, frame_len); + ksft_ready(); ksft_wait(); -- 2.56.0