From patchwork Thu Jan 16 23:17:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13942464 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14CE2246A09 for ; Thu, 16 Jan 2025 23:17:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737069452; cv=none; b=TsBaclTa+hZmnfoqLnRWj5EM2HcQNszxY5fkBWUZlDgFVyJkTFSi2QzS3qWqZyNMVpt+p15eDitIbwoqE/C4aHUZ8m5n8rV3M++aDMrTTce+iAiePqpCyUpMICC6eVhDonW7KIdeniAq07wZ/tnUMjwdMdWMFCPLMRah1QODUNo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737069452; c=relaxed/simple; bh=aPsgcI6sv2YIZcrbbnlWPzhKE0O2/vrVZpMxYf0vPWw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UM24+7uEnETKr/UvdhwzthcBXEEe4ggQiXZud0NVX6W2ukEqaPcDcskagucNeswOfDGtKIvWZ5N5ypB9Ra5Q+B1zF1ICAmV3o6IZaQVSG+DLc/ECULKrJTFh+FX3GzfhEzdu+BC0EDgDVfsNRc9IdxRhcn/hBAFe+hcfXsRZYgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk; spf=none smtp.mailfrom=davidwei.uk; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b=n7rakZHI; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b="n7rakZHI" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-216728b1836so25928205ad.0 for ; Thu, 16 Jan 2025 15:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1737069449; x=1737674249; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EFrx4dtkhpWaD6dvhicsjhhig5cONS03Ll2dOXxfwMI=; b=n7rakZHITxkr4P++NlkiVyKNz8+7y3YrB5WBlOnwjpCE2C6kZIcXdoj+SfRjpCEJu5 33/wuFLBSRfh/vf5snhIdR+wvYK+e+HGrcMTJDbPsWz24xK7GxR6Q1isQoeaHs/FfKjY VYG31SY0EKCDkQn3hPz0V3w2HU6KfEIqvS9qeBjLQx/nCkcx5a12ydSReJuVgy3QEGYF s/524YBGVjsGnVgvabQ/lcjPPeeCjaazPR8fq8C6JVi1m7eUSKvX5PCRNsJ21xa9v43U 0gVGHCVnM/LQ6uls0XLG7sumnL6ytn8pFX8Bb6ThVKdP8Kkzkd7kRw0Uq3A5jhBDQa6F miOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737069449; x=1737674249; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EFrx4dtkhpWaD6dvhicsjhhig5cONS03Ll2dOXxfwMI=; b=Bea9IkEFoTNzGvzkl2aSRCyBsBiS5K14pNDDMbaBQdFKClGISedkmzWbE+ix/+Pv9b RdZt0BOVkxrLuTGkj8ltEVEABVieei7iSwYe1XuUE44CsaZ+dz5TpjCQIbDd5l6/ZRgJ BzWW4seXL6jKjApfuZHN2qM3KvneVpsYy6p7wwQnBrPYIpAymjqd/wYYVUt36/Dpomp+ VfdJxAzj/a5EgzeLq8Ur55ilSGgxGlUTg/XRx6W3Eu4wTJqrjfllje65XVmsarkPChdM jivakeStiQr8EM2spIA10rUp0728td+HpZRQZHShulmUBc6nfqnKHxx4fvqxySMtJd3T NGHw== X-Forwarded-Encrypted: i=1; AJvYcCXIJu0o015TVWoE1rFYSBBQKU/N/9gkSs/s0DbInU99KddS2Wjy5+uySzxa99oXfOTJ13Iw9qA=@vger.kernel.org X-Gm-Message-State: AOJu0YzyyULYxgcXq8VY23MLLY3LJXj37Cf/qezNJINbdBStwRfQlWX9 UIj8RwHGivmyPbjBP0jVA0b3utPmGLnJp/BzjYMS8CZesvhANV0x/alWWz3uEGE= X-Gm-Gg: ASbGncsR6AJ+ofchjEhzg/dPW1h2x35fjU7VUW6j8IK+VuU1kClEFPjsgKv/d2dG5ll FQECHmoJa2ITKAYE2cYJeS6g8+C1UM0TY7IcHGRg/ud8nQf17qDrcViiErR52sSlesFHhyHbRR5 tLrYEvGpHFl8NqczObPG+KWCvQ05CgI41WvXnyQhzAXARq9R7aeNPjCtJmwO3Y2BRSx1t0QmutA x9W3NcTyoeLj/6MS6rmV38jWtPVuTwciqVCoPhzKQ== X-Google-Smtp-Source: AGHT+IEJ/gqghOJxd2TDDQDXM4bFQnd4Rz3G5xYAmJzRD5lljlGIKa3GdLohUK0dl0NVpi4ehbGUEA== X-Received: by 2002:a05:6a20:3d89:b0:1e0:c6c0:1e1f with SMTP id adf61e73a8af0-1eb215e1995mr668470637.36.1737069449357; Thu, 16 Jan 2025 15:17:29 -0800 (PST) Received: from localhost ([2a03:2880:ff:1a::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72dab81754csm565197b3a.63.2025.01.16.15.17.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jan 2025 15:17:29 -0800 (PST) From: David Wei To: io-uring@vger.kernel.org, netdev@vger.kernel.org Cc: Jens Axboe , Pavel Begunkov , Jakub Kicinski , Paolo Abeni , "David S. Miller" , Eric Dumazet , Jesper Dangaard Brouer , David Ahern , Mina Almasry , Stanislav Fomichev , Joe Damato , Pedro Tammela Subject: [PATCH net-next v11 20/21] net: add documentation for io_uring zcrx Date: Thu, 16 Jan 2025 15:17:02 -0800 Message-ID: <20250116231704.2402455-21-dw@davidwei.uk> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20250116231704.2402455-1-dw@davidwei.uk> References: <20250116231704.2402455-1-dw@davidwei.uk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add documentation for io_uring zero copy Rx that explains requirements and the user API. Signed-off-by: David Wei --- Documentation/networking/index.rst | 1 + Documentation/networking/iou-zcrx.rst | 201 ++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 Documentation/networking/iou-zcrx.rst diff --git a/Documentation/networking/index.rst b/Documentation/networking/index.rst index 058193ed2eeb..c64133d309bf 100644 --- a/Documentation/networking/index.rst +++ b/Documentation/networking/index.rst @@ -63,6 +63,7 @@ Contents: gtp ila ioam6-sysctl + iou-zcrx ip_dynaddr ipsec ip-sysctl diff --git a/Documentation/networking/iou-zcrx.rst b/Documentation/networking/iou-zcrx.rst new file mode 100644 index 000000000000..7f6b7c072b59 --- /dev/null +++ b/Documentation/networking/iou-zcrx.rst @@ -0,0 +1,201 @@ +.. SPDX-License-Identifier: GPL-2.0 + +===================== +io_uring zero copy Rx +===================== + +Introduction +============ + +io_uring zero copy Rx (ZC Rx) is a feature that removes kernel-to-user copy on +the network receive path, allowing packet data to be received directly into +userspace memory. This feature is different to TCP_ZEROCOPY_RECEIVE in that +there are no strict alignment requirements and no need to mmap()/munmap(). +Compared to kernel bypass solutions such as e.g. DPDK, the packet headers are +processed by the kernel TCP stack as normal. + +NIC HW Requirements +=================== + +Several NIC HW features are required for io_uring ZC Rx to work. For now the +kernel API does not configure the NIC and it must be done by the user. + +Header/data split +----------------- + +Required to split packets at the L4 boundary into a header and a payload. +Headers are received into kernel memory as normal and processed by the TCP +stack as normal. Payloads are received into userspace memory directly. + +Flow steering +------------- + +Specific HW Rx queues are configured for this feature, but modern NICs +typically distribute flows across all HW Rx queues. Flow steering is required +to ensure that only desired flows are directed towards HW queues that are +configured for io_uring ZC Rx. + +RSS +--- + +In addition to flow steering above, RSS is required to steer all other non-zero +copy flows away from queues that are configured for io_uring ZC Rx. + +Usage +===== + +Setup NIC +--------- + +Must be done out of band for now. + +Ensure there are at least two queues:: + + ethtool -L eth0 combined 2 + +Enable header/data split:: + + ethtool -G eth0 tcp-data-split on + +Carve out half of the HW Rx queues for zero copy using RSS:: + + ethtool -X eth0 equal 1 + +Set up flow steering, bearing in mind that queues are 0-indexed:: + + ethtool -N eth0 flow-type tcp6 ... action 1 + +Setup io_uring +-------------- + +This section describes the low level io_uring kernel API. Please refer to +liburing documentation for how to use the higher level API. + +Create an io_uring instance with the following required setup flags:: + + IORING_SETUP_SINGLE_ISSUER + IORING_SETUP_DEFER_TASKRUN + IORING_SETUP_CQE32 + +Create memory area +------------------ + +Allocate userspace memory area for receiving zero copy data:: + + void *area_ptr = mmap(NULL, area_size, + PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, + 0, 0); + +Create refill ring +------------------ + +Allocate memory for a shared ringbuf used for returning consumed buffers:: + + void *ring_ptr = mmap(NULL, ring_size, + PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, + 0, 0); + +This refill ring consists of some space for the header, followed by an array of +``struct io_uring_zcrx_rqe``:: + + size_t rq_entries = 4096; + size_t ring_size = rq_entries * sizeof(struct io_uring_zcrx_rqe) + PAGE_SIZE; + /* align to page size */ + ring_size = (ring_size + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1); + +Register ZC Rx +-------------- + +Fill in registration structs:: + + struct io_uring_zcrx_area_reg area_reg = { + .addr = (__u64)(unsigned long)area_ptr, + .len = area_size, + .flags = 0, + }; + + struct io_uring_region_desc region_reg = { + .user_addr = (__u64)(unsigned long)ring_ptr, + .size = ring_size, + .flags = IORING_MEM_REGION_TYPE_USER, + }; + + struct io_uring_zcrx_ifq_reg reg = { + .if_idx = if_nametoindex("eth0"), + /* this is the HW queue with desired flow steered into it */ + .if_rxq = 1, + .rq_entries = rq_entries, + .area_ptr = (__u64)(unsigned long)&area_reg, + .region_ptr = (__u64)(unsigned long)®ion_reg, + }; + +Register with kernel:: + + io_uring_register_ifq(ring, ®); + +Map refill ring +--------------- + +The kernel fills in fields for the refill ring in the registration ``struct +io_uring_zcrx_ifq_reg``. Map it into userspace:: + + struct io_uring_zcrx_rq refill_ring; + + refill_ring.khead = (unsigned *)((char *)ring_ptr + reg.offsets.head); + refill_ring.khead = (unsigned *)((char *)ring_ptr + reg.offsets.tail); + refill_ring.rqes = + (struct io_uring_zcrx_rqe *)((char *)ring_ptr + reg.offsets.rqes); + refill_ring.rq_tail = 0; + refill_ring.ring_ptr = ring_ptr; + +Receiving data +-------------- + +Prepare a zero copy recv request:: + + struct io_uring_sqe *sqe; + + sqe = io_uring_get_sqe(ring); + io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, fd, NULL, 0, 0); + sqe->ioprio |= IORING_RECV_MULTISHOT; + +Now, submit and wait:: + + io_uring_submit_and_wait(ring, 1); + +Finally, process completions:: + + struct io_uring_cqe *cqe; + unsigned int count = 0; + unsigned int head; + + io_uring_for_each_cqe(ring, head, cqe) { + struct io_uring_zcrx_cqe *rcqe = (struct io_uring_zcrx_cqe *)(cqe + 1); + + unsigned char *data = area_ptr + (rcqe->off & IORING_ZCRX_AREA_MASK); + /* do something with the data */ + + count++; + } + io_uring_cq_advance(ring, count); + +Recycling buffers +----------------- + +Return buffers back to the kernel to be used again:: + + struct io_uring_zcrx_rqe *rqe; + unsigned mask = refill_ring.ring_entries - 1; + rqe = &refill_ring.rqes[refill_ring.rq_tail & mask]; + + area_offset = rcqe->off & IORING_ZCRX_AREA_MASK; + rqe->off = area_offset | area_reg.rq_area_token; + rqe->len = cqe->res; + IO_URING_WRITE_ONCE(*refill_ring.ktail, ++refill_ring.rq_tail); + +Testing +======= + +See ``tools/testing/selftests/drivers/net/hw/iou-zcrx.c``