zero-copy RX for io_uring
Subsystems (3)
v2 cover zero-copy RX for io_uring
This series is a RFC for io_uring/zctap. This is an evolution of the earlier zctap work, re-targeted to use io_uring as the userspace API. The current code is intended to provide a zero-copy RX path for upper-level networking protocols (aka TCP and UDP). The current draft focuses on host-provided memory (not GPU memory).
This RFC contains the upper-level core code required for operation, with the intent of soliciting feedback on the general API. This does not contain the network driver side changes required for complete operation. Also please note that as an RFC, there are some things which are incomplete or in need of rework.
The intent is to use a network driver which provides header/data splitting, so the frame header (which is processed by the networking stack) does not reside in user memory.
The code is successfully receiving a zero-copy TCP stream from a remote sender. An RFC, the intent is to solicit feedback on the API and overall design. The current code will also work with system pages, copying the data out to the application - this is intended as a fallback/testing path.
Performance numbers are coming soon!
High level description:
The application allocates a frame backing store, and provides this to the kernel for use. An interface queue is requested from the networking device, and incoming frames are deposited into the provided memory region.
Responsibility for correctly steering incoming frames to the queue is outside the scope of this work - it is assumed that the user has set steering rules up separately.
Incoming frames are sent up the stack as skb's and eventually land in the application's socket receive queue. This differs from AF_XDP, which receives raw frames directly to userspace, without protocol processing.
The RECV_ZC opcode then returns an iov[] style vector which points to the data in userspace memory. When the application has completed processing of the data, the buffer is returned back to the kernel through a fill ring for reuse.
Changelog:
v1: initial version
v2: Remove separate PROVIDE_REGION opcode, fold this functionality
into REGISTER_IFQ. Remove page_pool hooks, as it appears the page pool is currently incompatible with user-mapped memory. Add io_zctap_buffers and network driver API.
.
Jonathan Lemon (13):
io_uring: add zctap ifq definition
netdevice: add SETUP_ZCTAP to the netdev_bpf structure
io_uring: add register ifq opcode
io_uring: create a zctap region for a mapped buffer
io_uring: create page freelist for the ifq region
io_uring: Provide driver API for zctap packet buffers.
io_uring: Allocate the zctap buffers for the device
io_uring: Add zctap buffer get/put functions and refcounting.
skbuff: Introduce SKBFL_FIXED_FRAG and skb_fixed()
io_uring: Allocate a uarg for use by the ifq RX
io_uring: Define the zctap iov[] returned to the user.
io_uring: add OP_RECV_ZC command.
io_uring: Make remove_ifq_region a delayed work call
include/linux/io_uring.h | 35 ++ include/linux/io_uring_types.h | 11 + include/linux/netdevice.h | 6 + include/linux/skbuff.h | 10 +- include/uapi/linux/io_uring.h | 24 + io_uring/Makefile | 3 +- io_uring/io_uring.c | 8 + io_uring/kbuf.c | 13 + io_uring/kbuf.h | 2 + io_uring/net.c | 123 +++++ io_uring/opdef.c | 15 + io_uring/zctap.c | 842 +++++++++++++++++++++++++++++++++ io_uring/zctap.h | 16 + 13 files changed, 1106 insertions(+), 2 deletions(-) create mode 100644 io_uring/zctap.c create mode 100644 io_uring/zctap.h
Signature · 1 line
2.30.2
RFC PATCH v2 1/13 io_uring: add zctap ifq definition
Add structure definition for io_zctap_ifq for use by lower level networking hooks.
--- include/linux/io_uring_types.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/io_uring_types.h +11 -0
@@ -327,6 +327,7 @@ struct io_ring_ctx {struct io_mapped_ubuf *dummy_ubuf;struct io_rsrc_data *file_data;struct io_rsrc_data *buf_data; +struct io_zctap_ifq *zctap_ifq;struct delayed_work rsrc_put_work;struct llist_head rsrc_put_llist;
@@ -582,4 +583,14 @@ struct io_overflow_cqe { struct io_uring_cqe cqe; }; +struct io_zctap_ifq { +struct net_device *dev; +struct io_ring_ctx *ctx; +void *region; +struct ubuf_info *uarg; +u16 queue_id; +u16 id; +u16 fill_bgid; +}; +#endif
Signature · 1 line
2.30.2
RFC PATCH v2 2/13 netdevice: add SETUP_ZCTAP to the netdev_bpf structure
In reply to Jonathan LemonThis command requests the networking device setup or teardown a new interface queue, backed by a region of user supplied memory.
The queue will be managed by io-uring.
--- include/linux/netdevice.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/netdevice.h +6 -0
@@ -980,6 +980,7 @@ enum bpf_netdev_command {BPF_OFFLOAD_MAP_ALLOC ,BPF_OFFLOAD_MAP_FREE ,XDP_SETUP_XSK_POOL , + XDP_SETUP_ZCTAP, };struct bpf_prog_offload_ops ;
@@ -1018,6 +1019,11 @@ struct netdev_bpf {struct xsk_buff_pool *pool;u16 queue_id; } xsk; +/* XDP_SETUP_ZCTAP */ +struct { +struct io_zctap_ifq *ifq; +u16 queue_id; + } zct; }; };
Signature · 1 line
2.30.2
RFC PATCH v2 3/13 io_uring: add register ifq opcode
In reply to Jonathan LemonAdd initial support for support for hooking in zero-copy interface queues to io_uring. This command requests a user-managed queue from the specified network device.
This only includes the register opcode, unregistration is currently done implicitly when the ring is removed.
--- include/uapi/linux/io_uring.h | 15 ++++ io_uring/Makefile | 3 +- io_uring/io_uring.c | 8 +++ io_uring/zctap.c | 131 ++++++++++++++++++++++++++++++++++ io_uring/zctap.h | 9 +++ 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 io_uring/zctap.c create mode 100644 io_uring/zctap.h
include/uapi/linux/io_uring.h +15 -0
@@ -490,6 +490,9 @@ enum {/* register a range of fixed file slots for automatic slot allocation */ IORING_REGISTER_FILE_ALLOC_RANGE =25 , +/* register a network ifq for zerocopy RX */ +IORING_REGISTER_IFQ =26 , +/* this goes last */ IORING_REGISTER_LAST };
@@ -666,6 +669,18 @@ struct io_uring_recvmsg_out {__u32 flags; }; +/* +* Argument for IORING_REGISTER_IFQ +*/ +struct io_uring_ifq_req { +__u32 ifindex; +__u16 queue_id; +__u16 ifq_id; +__u16 region_id; +__u16 fill_bgid; +__u16 __pad[2 ]; +}; + #ifdef __cplusplus }#endif
io_uring/Makefile +2 -1
@@ -7,5 +7,6 @@ obj-$(CONFIG_IO_URING) += io_uring.o xattr.o nop.o fs.o splice.o \ openclose.o uring_cmd.o epoll.o \ statx.o net.o msg_ring.o timeout.o \ sqpoll.o fdinfo.o tctx.o poll.o \ - cancel.o kbuf.o rsrc.o rw.o opdef.o notif.o + cancel.o kbuf.o rsrc.o rw.o opdef.o \ + notif.o zctap.o obj-$(CONFIG_IO_WQ) += io-wq.o
io_uring/io_uring.c +8 -0
@@ -91,6 +91,7 @@#include "cancel.h" #include "net.h" #include "notif.h" +#include "zctap.h" #include "timeout.h" #include "poll.h"
@@ -2809,6 +2810,7 @@ static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)__io_cqring_overflow_flush (ctx, true);xa_for_each (&ctx->personalities, index, creds)io_unregister_personality (ctx, index); +io_unregister_zctap_all (ctx);if (ctx->rings)io_poll_remove_all (ctx,NULL , true);mutex_unlock (&ctx->uring_lock);
@@ -4033,6 +4035,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,break ; ret =io_register_file_alloc_range (ctx, arg);break ; +case IORING_REGISTER_IFQ : + ret = -EINVAL ; +if (!arg || nr_args !=1 ) +break ; + ret =io_register_ifq (ctx, arg); +break ;default : ret = -EINVAL ;break ;
io_uring/zctap.c new file +131 -0
131 lines not on this page · raw message
io_uring/zctap.h new file +9 -0
@@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef IOU_ZCTAP_H +#define IOU_ZCTAP_H + +int io_register_ifq (struct io_ring_ctx *ctx, +struct io_uring_ifq_req __user *arg); +void io_unregister_zctap_all (struct io_ring_ctx *ctx); + +#endif
Signature · 1 line
2.30.2
RFC PATCH v2 4/13 io_uring: create a zctap region for a mapped buffer
In reply to Jonathan LemonThis function takes all of a memory region that was previously registered with io_uring, and assigns it as the backing store for the specified ifq, binding the pages to a specific device.
The entire region is registered instead of providing individual bufferrs, as this allows the hardware to select the optimal buffer size for incoming packets.
The region is registered as part of the register_ifq opcode, instead of separately, since the ifq ring requires memory when it is created.
--- io_uring/zctap.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++- io_uring/zctap.h | 2 ++ 2 files changed, 64 insertions(+), 1 deletion(-)
io_uring/zctap.c +62 -1
@@ -6,16 +6,73 @@#include <linux/mm.h> #include <linux/io_uring.h> #include <linux/netdevice.h> +#include <linux/nospec.h> #include <uapi/linux/io_uring.h> #include "io_uring.h" #include "zctap.h" +#include "rsrc.h" +#include "kbuf.h" #define NR_ZCTAP_IFQS 1 +struct ifq_region { +struct io_mapped_ubuf *imu; +int count; +int nr_pages; +u16 id; +struct page *freelist[]; +}; +typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf); +static void io_remove_ifq_region (struct ifq_region *ifr) +{ +kvfree (ifr); +} + +int io_provide_ifq_region (struct io_zctap_ifq *ifq,u16 id) +{ +struct io_ring_ctx *ctx = ifq->ctx; +struct io_mapped_ubuf *imu; +struct ifq_region *ifr; +int i, nr_pages; +struct page *page; + +/* XXX for now, only allow one region per ifq. */ +if (ifq->region) +return -EFAULT ; + +if (unlikely (id >= ctx->nr_user_bufs)) +return -EFAULT ; + id =array_index_nospec (id, ctx->nr_user_bufs); + imu = ctx->user_bufs[id]; + +/* XXX check region is page aligned */ +if (imu->ubuf & ~PAGE_MASK || imu->ubuf_end & ~PAGE_MASK ) +return -EFAULT ; + + nr_pages = imu->nr_bvecs; + ifr =kvmalloc (struct_size (ifr, freelist, nr_pages),GFP_KERNEL ); +if (!ifr) +return -ENOMEM ; + + ifr->nr_pages = nr_pages; + ifr->imu = imu; + ifr->count = nr_pages; + ifr->id = id; + +for (i =0 ; i < nr_pages; i++) { + page = imu->bvec[i].bv_page; + ifr->freelist[i] = page; + } + + ifq->region = ifr; + +return 0 ; +} +static int __io_queue_mgmt (struct net_device *dev,struct io_zctap_ifq *ifq,u16 queue_id) {
@@ -59,6 +116,8 @@ static void io_zctap_ifq_free(struct io_zctap_ifq *ifq) {if (ifq->queue_id !=-1 )io_close_zctap_ifq (ifq, ifq->queue_id); +if (ifq->region) +io_remove_ifq_region (ifq->region);if (ifq->dev)dev_put (ifq->dev);kfree (ifq);
@@ -92,7 +151,9 @@ int io_register_ifq(struct io_ring_ctx *ctx,if (!ifq->dev) goto out; -/* region attachment TBD */ + err =io_provide_ifq_region (ifq, req.region_id); +if (err) + goto out; err =io_open_zctap_ifq (ifq, req.queue_id);if (err)
io_uring/zctap.h +2 -0
@@ -6,4 +6,6 @@ int io_register_ifq(struct io_ring_ctx *ctx, struct io_uring_ifq_req __user *arg);void io_unregister_zctap_all (struct io_ring_ctx *ctx); +int io_provide_ifq_region (struct io_zctap_ifq *ifq,u16 id); +#endif
Signature · 1 line
2.30.2
RFC PATCH v2 5/13 io_uring: create page freelist for the ifq region
In reply to Jonathan LemonCreate a freelist where the driver can obtain pages for the packet backing store. Use the page's page_private field to record lookup information.
--- io_uring/zctap.c | 61 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 5 deletions(-)
io_uring/zctap.c +56 -5
@@ -27,18 +27,68 @@ struct ifq_region {typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf); +static void zctap_set_page_info (struct page *page,u64 info) +{ +set_page_private (page, info); +} + +static u64 zctap_mk_page_info (u16 region_id,u16 pgid) +{ +return (u64 )0xface <<48 | (u64 )region_id <<16 | (u64 )pgid; +} +static void io_remove_ifq_region (struct ifq_region *ifr) { +struct io_mapped_ubuf *imu; +struct page *page; +int i; + + imu = ifr->imu; +for (i =0 ; i < ifr->nr_pages; i++) { + page = imu->bvec[i].bv_page; + +ClearPagePrivate (page); +set_page_private (page,0 ); + } +kvfree (ifr); } +static int io_zctap_map_region (struct ifq_region *ifr) +{ +struct io_mapped_ubuf *imu; +struct page *page; +u64 info; +int i; + + imu = ifr->imu; +for (i =0 ; i < ifr->nr_pages; i++) { + page = imu->bvec[i].bv_page; +if (PagePrivate (page)) + goto out; +SetPagePrivate (page); + info =zctap_mk_page_info (ifr->id, i); +zctap_set_page_info (page, info); + ifr->freelist[i] = page; + } +return 0 ; + +out: +while (i--) { + page = imu->bvec[i].bv_page; +ClearPagePrivate (page); +set_page_private (page,0 ); + } +return -EEXIST ; +} +int io_provide_ifq_region (struct io_zctap_ifq *ifq,u16 id) {struct io_ring_ctx *ctx = ifq->ctx;struct io_mapped_ubuf *imu;struct ifq_region *ifr; -int i, nr_pages; -struct page *page; +int nr_pages; +int err;/* XXX for now, only allow one region per ifq. */ if (ifq->region)
@@ -63,9 +113,10 @@ int io_provide_ifq_region(struct io_zctap_ifq *ifq, u16 id) ifr->count = nr_pages; ifr->id = id; -for (i =0 ; i < nr_pages; i++) { - page = imu->bvec[i].bv_page; - ifr->freelist[i] = page; + err =io_zctap_map_region (ifr); +if (err) { +kvfree (ifr); +return err; } ifq->region = ifr;
Signature · 1 line
2.30.2
RFC PATCH v2 6/13 io_uring: Provide driver API for zctap packet buffers.
In reply to Jonathan LemonIntroduce 'struct io_zctap_buf', representing a buffer used by the network drivers, and a pair of get/put functions which are used to obtain the buffers.
The code for these will be fleshed out in the next patch.
--- include/linux/io_uring.h | 35 +++++++++++++++++++++++++++++++++++ io_uring/zctap.c | 11 +++++++++++ 2 files changed, 46 insertions(+)
include/linux/io_uring.h +35 -0
@@ -32,6 +32,13 @@ struct io_uring_cmd {u8 pdu[32 ];/* available inline for free use */ }; +struct io_zctap_buf { +dma_addr_t dma; +struct page *page; +atomic_t refcount; +u8 _pad[4 ]; +}; +#if defined(CONFIG_IO_URING )int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd);
@@ -44,6 +51,18 @@ void __io_uring_free(struct task_struct *tsk);void io_uring_unreg_ringfd (void );const char *io_uring_get_opcode (u8 opcode); +struct io_zctap_ifq ; +struct io_zctap_buf *io_zctap_get_buf (struct io_zctap_ifq *ifq); +void io_zctap_put_buf (struct io_zctap_ifq *ifq,struct io_zctap_buf *buf); + +static inline dma_addr_t io_zctap_buf_dma (struct io_zctap_buf *buf) +{ +return buf->dma; +} +static inline struct page *io_zctap_buf_page (struct io_zctap_buf *buf) +{ +return buf->page; +}static inline void io_uring_files_cancel (void ) {if (current->io_uring) {
@@ -92,6 +111,22 @@ static inline const char *io_uring_get_opcode(u8 opcode) {return "" ; } +static inline dma_addr_t io_zctap_buf_dma (struct io_zctap_buf *buf) +{ +return 0 ; +} +static inline struct page *io_zctap_buf_page (struct io_zctap_buf *buf) +{ +return NULL ; +} +static inline struct io_zctap_buf *io_zctap_get_buf (struct io_zctap_ifq *ifq) +{ +return NULL ; +} +void io_zctap_put_buf (struct io_zctap_ifq *ifq,struct io_zctap_buf *buf) +{ +} +#endif #endif
io_uring/zctap.c +11 -0
@@ -37,6 +37,17 @@ static u64 zctap_mk_page_info(u16 region_id, u16 pgid)return (u64 )0xface <<48 | (u64 )region_id <<16 | (u64 )pgid; } +struct io_zctap_buf *io_zctap_get_buf (struct io_zctap_ifq *ifq) +{ +return NULL ; +} +EXPORT_SYMBOL (io_zctap_get_buf); + +void io_zctap_put_buf (struct io_zctap_ifq *ifq,struct io_zctap_buf *buf) +{ +} +EXPORT_SYMBOL (io_zctap_put_buf); +static void io_remove_ifq_region (struct ifq_region *ifr) {struct io_mapped_ubuf *imu;
Signature · 1 line
2.30.2
RFC PATCH v2 7/13 io_uring: Allocate the zctap buffers for the device
In reply to Jonathan LemonThe idea is to register a memory region with the device, and later specify the desired packet buffer size. The code currently assumes a page size.
Create the desired number of zctap buffers and DMA map them to the target device, recording the dma address for later use.
--- io_uring/zctap.c | 55 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-)
io_uring/zctap.c +47 -8
@@ -22,7 +22,9 @@ struct ifq_region {int count;int nr_pages;u16 id; -struct page *freelist[]; + +struct io_zctap_buf *buf; +struct io_zctap_buf *freelist[]; };typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf);
@@ -62,35 +64,65 @@ static void io_remove_ifq_region(struct ifq_region *ifr)set_page_private (page,0 ); } +kvfree (ifr->buf);kvfree (ifr); } -static int io_zctap_map_region (struct ifq_region *ifr) +static inline struct device * +netdev2device (struct net_device *dev) +{ +return dev->dev.parent;/* from SET_NETDEV_DEV() */ +} + +static int io_zctap_map_region (struct ifq_region *ifr,struct device *device) {struct io_mapped_ubuf *imu; +struct io_zctap_buf *buf;struct page *page; +dma_addr_t addr; +int i, err;u64 info; -int i; imu = ifr->imu;for (i =0 ; i < ifr->nr_pages; i++) { page = imu->bvec[i].bv_page; -if (PagePrivate (page)) + +if (PagePrivate (page)) { + err = -EEXIST ; goto out; + } +SetPagePrivate (page); info =zctap_mk_page_info (ifr->id, i);zctap_set_page_info (page, info); - ifr->freelist[i] = page; + + buf = &ifr->buf[i]; + addr =dma_map_page_attrs (device, page,0 ,PAGE_SIZE , +DMA_BIDIRECTIONAL , +DMA_ATTR_SKIP_CPU_SYNC ); +if (dma_mapping_error (device, addr)) { + err = -ENOMEM ; + goto out; + } + buf->dma = addr; + buf->page = page; +atomic_set (&buf->refcount,0 ); + + ifr->freelist[i] = buf; }return 0 ; out:while (i--) { page = imu->bvec[i].bv_page; -ClearPagePrivate (page);set_page_private (page,0 ); +ClearPagePrivate (page); + buf = &ifr->buf[i]; +dma_unmap_page_attrs (device, buf->dma,PAGE_SIZE , +DMA_BIDIRECTIONAL , +DMA_ATTR_SKIP_CPU_SYNC ); } -return -EEXIST ; +return err; }int io_provide_ifq_region (struct io_zctap_ifq *ifq,u16 id)
@@ -119,13 +151,20 @@ int io_provide_ifq_region(struct io_zctap_ifq *ifq, u16 id)if (!ifr)return -ENOMEM ; + ifr->buf =kvmalloc_array (nr_pages,sizeof (*ifr->buf),GFP_KERNEL ); +if (!ifr->buf) { +kvfree (ifr); +return -ENOMEM ; + } + ifr->nr_pages = nr_pages; ifr->imu = imu; ifr->count = nr_pages; ifr->id = id; - err =io_zctap_map_region (ifr); + err =io_zctap_map_region (ifr,netdev2device (ifq->dev));if (err) { +kvfree (ifr->buf);kvfree (ifr);return err; }
Signature · 1 line
2.30.2
RFC PATCH v2 8/13 io_uring: Add zctap buffer get/put functions and refcounting.
In reply to Jonathan LemonFlesh out the driver API functions introduced earlier.
The driver should get a buffer reference, and get a reference.
The refcount is incremented as skb fragments go up the stack, and the driver releases its ref when finished with the buffer.
When ownership of the fragment is transferred to the user, a user refcount is incremented, and correspondingly decremented when returned. When all refcounts are released, the buffer is safe to reuse. The user/kernel split is needed to differentiate between "safe to reuse the buffer" and "still in use by the kernel".
The locking here is non-optimal, and likely can be improved.
--- io_uring/kbuf.c | 13 ++++++ io_uring/kbuf.h | 2 + io_uring/zctap.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 121 insertions(+), 1 deletion(-)
io_uring/kbuf.c +13 -0
@@ -188,6 +188,19 @@ void __user *io_buffer_select(struct io_kiocb *req, size_t *len,return ret; } +/* XXX May called from the driver, in napi context. */ +u64 io_zctap_buffer (struct io_kiocb *req,size_t *len) +{ +struct io_ring_ctx *ctx = req->ctx; +struct io_buffer_list *bl; +void __user *ret =NULL ; + + bl =io_buffer_get_list (ctx, req->buf_index); +if (likely (bl)) + ret =io_ring_buffer_select (req, len, bl,IO_URING_F_UNLOCKED ); +return (u64 )ret; +} +static __cold int io_init_bl_list (struct io_ring_ctx *ctx) {int i;
io_uring/kbuf.h +2 -0
@@ -50,6 +50,8 @@ unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags);void io_kbuf_recycle_legacy (struct io_kiocb *req,unsigned issue_flags); +u64 io_zctap_buffer (struct io_kiocb *req,size_t *len); +static inline void io_kbuf_recycle_ring (struct io_kiocb *req) {/*
io_uring/zctap.c +106 -1
132 lines not on this page · raw message
Signature · 1 line
2.30.2
RFC PATCH v2 9/13 skbuff: Introduce SKBFL_FIXED_FRAG and skb_fixed()
In reply to Jonathan LemonWhen a skb marked as zerocopy goes up the network stack, during RX, it calls skb_orphan_frags_rx. This is designed to catch TX zerocopy data being redirected back up the stack, not new zerocopy fragments coming up from the driver.
Currently, since the skb is marked as zerocopy, skb_copy_ubufs() is called, defeating the point of zerocopy-RX.
Have the driver mark the fragments as fixed, so they are not copied.
--- include/linux/skbuff.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
include/linux/skbuff.h +9 -1
@@ -516,6 +516,9 @@ enum {* use frags only up until ubuf_info is released */ SKBFL_MANAGED_FRAG_REFS =BIT (4 ), + +/* don't move or copy the fragment */ + SKBFL_FIXED_FRAG = BIT(5), };#define SKBFL_ZEROCOPY_FRAG (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG)
@@ -1651,6 +1654,11 @@ static inline bool skb_zcopy_managed(const struct sk_buff *skb)return skb_shinfo (skb)->flags &SKBFL_MANAGED_FRAG_REFS ; } +static inline bool skb_fixed (const struct sk_buff *skb) +{ +return skb_shinfo (skb)->flags &SKBFL_FIXED_FRAG ; +} +static inline bool skb_pure_zcopy_same (const struct sk_buff *skb1,const struct sk_buff *skb2) {
@@ -3087,7 +3095,7 @@ static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)/* Frags must be orphaned, even if refcounted, if skb might loop to rx path */ static inline int skb_orphan_frags_rx (struct sk_buff *skb,gfp_t gfp_mask) { -if (likely (!skb_zcopy (skb))) +if (likely (!skb_zcopy (skb) ||skb_fixed (skb)))return 0 ;return skb_copy_ubufs (skb, gfp_mask); }
Signature · 1 line
2.30.2
RFC PATCH v2 10/13 io_uring: Allocate a uarg for use by the ifq RX
In reply to Jonathan LemonCreate a static uarg which is attached to zerocopy RX buffers, and add a callback to handle freeing the skb.
As the skb is marked as zerocopy, it bypasses the default network skb fragment destructor and calls our version. This handles our refcounts, and releasing the ZC buffer back to the freelist.
--- io_uring/zctap.c | 71 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-)
io_uring/zctap.c +66 -5
@@ -41,6 +41,26 @@ static u64 zctap_mk_page_info(u16 region_id, u16 pgid)return (u64 )0xface <<48 | (u64 )region_id <<16 | (u64 )pgid; } +static u64 zctap_page_info (const struct page *page) +{ +return page_private (page); +} + +static u16 zctap_page_id (const struct page *page) +{ +return zctap_page_info (page) &0xffff ; +} + +static bool zctap_page_magic (const struct page *page) +{ +return (zctap_page_info (page) >>48 ) ==0xface ; +} + +static bool zctap_page_ours (struct page *page) +{ +return PagePrivate (page) &&zctap_page_magic (page); +} +/* driver bias cannot be larger than this */ #define IO_ZCTAP_UREF 0x1000#define IO_ZCTAP_KREF_MASK (IO_ZCTAP_UREF - 1)
@@ -154,6 +174,17 @@ void io_zctap_put_buf(struct io_zctap_ifq *ifq, struct io_zctap_buf *buf) }EXPORT_SYMBOL (io_zctap_put_buf); +/* could be called by the stack as it drops/recycles the skbs */ +static void io_zctap_put_page (struct io_zctap_ifq *ifq,struct page *page) +{ +struct ifq_region *ifr; +u16 pgid; + + ifr = ifq->region;/* only one */ + pgid =zctap_page_id (page); +io_zctap_put_buf (ifq, &ifr->buf[pgid]); +} +static void io_remove_ifq_region (struct ifq_region *ifr) {struct io_mapped_ubuf *imu;
@@ -306,16 +337,44 @@ static int io_close_zctap_ifq(struct io_zctap_ifq *ifq, u16 queue_id)return __io_queue_mgmt (ifq->dev,NULL , queue_id); } +/* XXX get around not having "struct ubuf_info" defined in io_uring_types.h */ +struct io_zctap_ifq_priv { +struct io_zctap_ifq ifq; +struct ubuf_info uarg; +}; + +static void io_zctap_ifq_callback (struct sk_buff *skb,struct ubuf_info *uarg, +bool success) +{ +struct skb_shared_info *shinfo =skb_shinfo (skb); +struct io_zctap_ifq_priv *priv; +struct page *page; +int i; + + priv = container_of(uarg, struct io_zctap_ifq_priv, uarg); + +for (i =0 ; i < shinfo->nr_frags; i++) { + page =skb_frag_page (&shinfo->frags[i]); +if (zctap_page_ours (page)) +io_zctap_put_page (&priv->ifq, page); +#if 0 + else +put_page (page ); +#endif + } +} +static struct io_zctap_ifq *io_zctap_ifq_alloc (void ) { -struct io_zctap_ifq *ifq; +struct io_zctap_ifq_priv *priv; - ifq =kzalloc (sizeof (*ifq),GFP_KERNEL ); -if (!ifq) + priv =kzalloc (sizeof (*priv),GFP_KERNEL ); +if (!priv)return NULL ; - ifq->queue_id =-1 ; -return ifq; + priv->ifq.queue_id =-1 ; + priv->ifq.uarg = &priv->uarg; +return &priv->ifq; }static void io_zctap_ifq_free (struct io_zctap_ifq *ifq)
@@ -351,6 +410,8 @@ int io_register_ifq(struct io_ring_ctx *ctx, ifq->ctx = ctx; ifq->fill_bgid = req.fill_bgid; + ifq->uarg->callback = io_zctap_ifq_callback; + ifq->uarg->flags =SKBFL_ALL_ZEROCOPY |SKBFL_FIXED_FRAG ; err = -ENODEV ; ifq->dev =dev_get_by_index (&init_net, req.ifindex);
Signature · 1 line
2.30.2
RFC PATCH v2 11/13 io_uring: Define the zctap iov[] returned to the user.
In reply to Jonathan LemonWhen performing a ZC receive, instead of returning data directly to the user, an iov[] structure is returned referencing the data in user space.
The application locates the base address of the data by performing address computations on bgid:bid. The off/len applies to the base address, resulting in the data segment.
The bgid:bid identifying the buffer should later be placed in the ifq's fill ring, which returns the buffer back to the kernel.
--- include/uapi/linux/io_uring.h | 8 ++++++++ 1 file changed, 8 insertions(+)
include/uapi/linux/io_uring.h +8 -0
@@ -681,6 +681,14 @@ struct io_uring_ifq_req {__u16 __pad[2 ]; }; +struct io_uring_zctap_iov { +__u32 off; +__u32 len; +__u16 bgid; +__u16 bid; +__u16 resv[2 ]; +}; + #ifdef __cplusplus }#endif
Signature · 1 line
2.30.2
RFC PATCH v2 12/13 io_uring: add OP_RECV_ZC command.
In reply to Jonathan LemonThis is still a WIP. The current code (temporarily) uses addr3 as a hack in order to leverage code in io_recvmsg_prep.
The recvzc opcode uses a metadata buffer either supplied directly with buf/len, or indirectly from the buffer group. The expectation is that this buffer is then filled with an array of io_uring_zctap_iov structures, which point to the data in user-memory.
addr3 = (readlen << 32) | (copy_bgid << 16) | ctx->ifq_id;
The amount of returned data is limited by the number of iovs that the metadata area can hold, and also the readlen parameter.
As a fallback (and for testing purposes), if the skb data is not present in user memory (perhaps due to system misconfiguration), then a seprate buffer is obtained from the copy_bgid and the data is copied into user-memory.
--- include/uapi/linux/io_uring.h | 1 + io_uring/net.c | 123 ++++++++++++ io_uring/opdef.c | 15 ++ io_uring/zctap.c | 366 ++++++++++++++++++++++++++++++++++ io_uring/zctap.h | 5 + 5 files changed, 510 insertions(+)
include/uapi/linux/io_uring.h +1 -0
@@ -215,6 +215,7 @@ enum io_uring_op {IORING_OP_URING_CMD ,IORING_OP_SEND_ZC ,IORING_OP_SENDMSG_ZC , +IORING_OP_RECV_ZC ,/* this goes last, obviously */ IORING_OP_LAST,
io_uring/net.c +123 -0
141 lines not on this page · raw message
io_uring/opdef.c +15 -0
@@ -33,6 +33,7 @@#include "poll.h" #include "cancel.h" #include "rw.h" +#include "zctap.h" static int io_no_issue (struct io_kiocb *req,unsigned int issue_flags) {
@@ -522,6 +523,20 @@ const struct io_op_def io_op_defs[] = { .fail = io_sendrecv_fail, #else .prep = io_eopnotsupp_prep, +#endif + }, + [IORING_OP_RECV_ZC ] = { + .name ="RECV_ZC" , + .needs_file =1 , + .unbound_nonreg_file =1 , + .pollin =1 , + .buffer_select =1 , + .ioprio =1 , +#if defined(CONFIG_NET) + .prep = io_recvzc_prep, + .issue = io_recvzc, +#else + .prep = io_eopnotsupp_prep, #endif }, };
io_uring/zctap.c +366 -0
393 lines not on this page · raw message
io_uring/zctap.h +5 -0
@@ -8,4 +8,9 @@ void io_unregister_zctap_all(struct io_ring_ctx *ctx);int io_provide_ifq_region (struct io_zctap_ifq *ifq,u16 id); +int io_recvzc (struct io_kiocb *req,unsigned int issue_flags); +int io_recvzc_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); +int io_zctap_recv (struct io_zctap_ifq *ifq,struct socket *sock, +struct msghdr *msg,int flags,u32 datalen,u16 copy_bgid); +#endif
Signature · 1 line
2.30.2
RFC PATCH v2 13/13 io_uring: Make remove_ifq_region a delayed work call
In reply to Jonathan LemonVery much a WIP!
The page backing store should not be removed until all ouststanding packets are returned. The packets may be inflight, owned by the driver or sitting in a socket buffer.
This shows how the cleanup routine should check that there are no pending packets in flight, before cleaning up the buffers.
--- io_uring/zctap.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-)
io_uring/zctap.c +25 -8
@@ -25,6 +25,7 @@ struct ifq_region {u16 id;spinlock_t freelist_lock; +struct delayed_work release_work;struct io_zctap_buf *buf;struct io_zctap_buf *freelist[];
@@ -199,24 +200,38 @@ static void io_zctap_put_page(struct io_zctap_ifq *ifq, struct page *page)io_zctap_put_buf (ifq, &ifr->buf[pgid]); } -static void io_remove_ifq_region (struct ifq_region *ifr) +static void io_remove_ifq_region_work (struct work_struct *work) { -struct io_mapped_ubuf *imu; -struct page *page; -int i; +struct ifq_region *ifr =container_of ( + to_delayed_work(work), struct ifq_region, release_work); +struct io_zctap_buf *buf; +int i, refs; - imu = ifr->imu;for (i =0 ; i < ifr->nr_pages; i++) { - page = imu->bvec[i].bv_page; + buf = &ifr->buf[i]; + refs =atomic_read (&buf->refcount) &IO_ZCTAP_KREF_MASK ; +if (refs) { +schedule_delayed_work (&ifr->release_work,HZ ); +return ; + } + } -ClearPagePrivate (page); -set_page_private (page,0 ); +for (i =0 ; i < ifr->nr_pages; i++) { + buf = &ifr->buf[i]; +set_page_private (buf->page,0 ); +ClearPagePrivate (buf->page); }kvfree (ifr->buf);kvfree (ifr); } +static void io_remove_ifq_region (struct ifq_region *ifr) +{ +INIT_DELAYED_WORK (&ifr->release_work, io_remove_ifq_region_work); +schedule_delayed_work (&ifr->release_work,0 ); +} +static inline struct device *netdev2device (struct net_device *dev) {
@@ -403,6 +418,8 @@ static struct io_zctap_ifq *io_zctap_ifq_alloc(void)return &priv->ifq; } +/* XXX this seems to be called too late - MM is already torn down? */ +/* need to tear down sockets, then io_uring, then MM */ static void io_zctap_ifq_free (struct io_zctap_ifq *ifq) {if (ifq->queue_id !=-1 )
Signature · 1 line
2.30.2
Quoted 68 lines · Jonathan Lemon
This series is a RFC for
io_uring/zctap. This is an evolution of the earlier zctap work, re-targeted to useio_uringas the userspace API. The current code is intended to provide a zero-copy RX path for upper-level networking protocols (aka TCP and UDP). The current draft focuses on host-provided memory (not GPU memory).This RFC contains the upper-level core code required for operation, with the intent of soliciting feedback on the general API. This does not contain the network driver side changes required for complete operation. Also please note that as an RFC, there are some things which are incomplete or in need of rework.
The intent is to use a network driver which provides header/data splitting, so the frame header (which is processed by the networking stack) does not reside in user memory.
The code is successfully receiving a zero-copy TCP stream from a remote sender. An RFC, the intent is to solicit feedback on the API and overall design. The current code will also work with system pages, copying the data out to the application - this is intended as a fallback/testing path.
Performance numbers are coming soon!
High level description:
The application allocates a frame backing store, and provides this to the kernel for use. An interface queue is requested from the networking device, and incoming frames are deposited into the provided memory region.
Responsibility for correctly steering incoming frames to the queue is outside the scope of this work - it is assumed that the user has set steering rules up separately.
Incoming frames are sent up the stack as skb's and eventually land in the application's socket receive queue. This differs from
AF_XDP, which receives raw frames directly to userspace, without protocol processing.The
RECV_ZCopcode then returns an iov[] style vector which points to the data in userspace memory. When the application has completed processing of the data, the buffer is returned back to the kernel through a fill ring for reuse.Changelog:
v1: initial version
v2: Remove separatePROVIDE_REGIONopcode, fold this functionality
intoREGISTER_IFQ. Removepage_poolhooks, as it appears the page pool is currently incompatible with user-mapped memory. Addio_zctap_buffersand network driver API.
.Jonathan Lemon (13):
io_uring: add zctap ifq definition
netdevice: addSETUP_ZCTAPto thenetdev_bpfstructure
io_uring: add register ifq opcode
io_uring: create a zctap region for a mapped buffer
io_uring: create page freelist for the ifq region
io_uring: Provide driver API for zctap packet buffers.
io_uring: Allocate the zctap buffers for the device
io_uring: Add zctap buffer get/put functions and refcounting.
skbuff: IntroduceSKBFL_FIXED_FRAGandskb_fixed()
io_uring: Allocate a uarg for use by the ifq RX
io_uring: Define the zctap iov[] returned to the user.
io_uring: addOP_RECV_ZCcommand.
io_uring: Makeremove_ifq_regiona delayed work call
Quoted 17 lines
include/linux/io_uring.h | 35 ++ include/linux/io_uring_types.h | 11 + include/linux/netdevice.h | 6 + include/linux/skbuff.h | 10 +- include/uapi/linux/io_uring.h | 24 + io_uring/Makefile | 3 +- io_uring/io_uring.c | 8 + io_uring/kbuf.c | 13 + io_uring/kbuf.h | 2 + io_uring/net.c | 123 +++++ io_uring/opdef.c | 15 + io_uring/zctap.c | 842 +++++++++++++++++++++++++++++++++ io_uring/zctap.h | 16 + 13 files changed, 1106 insertions(+), 2 deletions(-) create mode 100644 io_uring/zctap.c create mode 100644 io_uring/zctap.h
Hi, Jonathan
We are interested in your work, too. I think the API is better than V1. I have a question: Is this patchset still incomplete? We'd like to know how to split msg header and body by XDP with io_uring ZC_RECV. Or could you please share one runnable demo which could run with your previous liburing patch.
Regards,
Zhang.
Quoted 6 lines · Ziyang Zhang
Hi, Jonathan
We are interested in your work, too. I think the API is better than V1. I have a question: Is this patchset still incomplete? We'd like to know how to split msg header and body by XDP with
io_uringZC_RECV. Or could you please share one runnable demo which could run with your previous liburing patch.
It seems my earlier reply to this didn't go to the list, so retrying.
I just posted RFC v3, which is just about complete. The cover letter also has the location of a github repo which has a simple example showing the operation. I'm in the process of updating iperf3 as another sample application.
The packet splitting is done by the NIC hardware. There are Broadcom and nVidia NICs which have these feature sets. One of my testbeds is set up and working with Broadcom.
Signature · 1 line
Jonathan