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. veth has no AF_XDP zerocopy support, so the socket runs in copy mode. In copy mode the kernel allocates an skb for the frame in xsk_build_skb(), which uses the extra headroom. As a test helper, -t takes one well-formed frame and does not wait for TX completion, because copy mode sends the frame within sendto(). A driver with zerocopy support may send it later, so -t is meant for copy mode. 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 --- 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 | 80 ++++++++++++++++++-- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/net/lib/xdp_helper.c b/tools/testing/selftests/net/lib/xdp_helper.c index eb025a9f35b1..1111631efd73 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,20 @@ int main(int argc, char **argv) sxdp.sxdp_queue_id = queue; sxdp.sxdp_flags = 0; - if (argc > 3) { - if (!strcmp(argv[3], "-z")) { + 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) { + 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 +182,9 @@ int main(int argc, char **argv) } } + if (frame_len) + return send_frame(sock_fd, frame_len); + ksft_ready(); ksft_wait(); -- 2.56.0