Add support to share a umem between queue ids on the same device. This mode can be invoked with the XDP_SHARED_UMEM bind flag. Previously, sharing was only supported within the same queue id and device, and you shared one set of fill and completion rings. However, note that when sharing a umem between queue ids, you need to create a fill ring and a completion ring and tie them to the socket before you do the bind with the XDP_SHARED_UMEM flag. This so that the single-producer single-consumer semantics can be upheld. Signed-off-by: Magnus Karlsson <magnus.karlsson@xxxxxxxxx> --- include/net/xsk_buff_pool.h | 3 +++ net/xdp/xsk.c | 51 +++++++++++++++++++++++++++++---------------- net/xdp/xsk_buff_pool.c | 27 ++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/include/net/xsk_buff_pool.h b/include/net/xsk_buff_pool.h index 7513a17..844901c 100644 --- a/include/net/xsk_buff_pool.h +++ b/include/net/xsk_buff_pool.h @@ -76,6 +76,9 @@ struct xsk_buff_pool *xp_assign_umem(struct xsk_buff_pool *pool, struct xdp_umem *umem); int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs, struct net_device *dev, u16 queue_id, u16 flags); +int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *xs, + struct xdp_umem *umem, struct net_device *dev, + u16 queue_id); void xp_destroy(struct xsk_buff_pool *pool); void xp_release(struct xdp_buff_xsk *xskb); void xp_get_pool(struct xsk_buff_pool *pool); diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 4d0028c..1abc222 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -627,6 +627,7 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr; struct sock *sk = sock->sk; struct xdp_sock *xs = xdp_sk(sk); + struct xsk_buff_pool *new_pool; struct net_device *dev; u32 flags, qid; int err = 0; @@ -679,12 +680,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) goto out_unlock; } - if (xs->pool->fq || xs->pool->cq) { - /* Do not allow setting your own fq or cq. */ - err = -EINVAL; - goto out_unlock; - } - sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd); if (IS_ERR(sock)) { err = PTR_ERR(sock); @@ -697,17 +692,43 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) sockfd_put(sock); goto out_unlock; } - if (umem_xs->dev != dev || umem_xs->queue_id != qid) { + if (umem_xs->dev != dev) { err = -EINVAL; sockfd_put(sock); goto out_unlock; } - /* Share the buffer pool with the other socket. */ - xp_get_pool(umem_xs->pool); - curr_pool = xs->pool; - xs->pool = umem_xs->pool; - xp_destroy(curr_pool); + if (umem_xs->queue_id != qid) { + /* Share the umem with another socket on another qid */ + new_pool = xp_assign_umem(xs->pool, umem_xs->umem); + if (!new_pool) { + sockfd_put(sock); + goto out_unlock; + } + + err = xp_assign_dev_shared(new_pool, xs, umem_xs->umem, + dev, qid); + if (err) { + xp_destroy(new_pool); + sockfd_put(sock); + goto out_unlock; + } + xs->pool = new_pool; + } else { + /* Share the buffer pool with the other socket. */ + if (xs->pool->fq || xs->pool->cq) { + /* Do not allow setting your own fq or cq. */ + err = -EINVAL; + sockfd_put(sock); + goto out_unlock; + } + + xp_get_pool(umem_xs->pool); + curr_pool = xs->pool; + xs->pool = umem_xs->pool; + xp_destroy(curr_pool); + } + xdp_get_umem(umem_xs->umem); WRITE_ONCE(xs->umem, umem_xs->umem); sockfd_put(sock); @@ -715,8 +736,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) err = -EINVAL; goto out_unlock; } else { - struct xsk_buff_pool *new_pool; - /* This xsk has its own umem. */ new_pool = xp_assign_umem(xs->pool, xs->umem); if (!new_pool) { @@ -841,10 +860,6 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname, mutex_unlock(&xs->mutex); return -EBUSY; } - if (!xs->umem) { - mutex_unlock(&xs->mutex); - return -EINVAL; - } q = (optname == XDP_UMEM_FILL_RING) ? &xs->pool->fq : &xs->pool->cq; diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c index 3c58d76..7987c17 100644 --- a/net/xdp/xsk_buff_pool.c +++ b/net/xdp/xsk_buff_pool.c @@ -126,8 +126,8 @@ static void xp_disable_drv_zc(struct xsk_buff_pool *pool) } } -int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs, - struct net_device *netdev, u16 queue_id, u16 flags) +static int __xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs, + struct net_device *netdev, u16 queue_id, u16 flags) { bool force_zc, force_copy; struct netdev_bpf bpf; @@ -196,6 +196,29 @@ int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs, return err; } +int xp_assign_dev(struct xsk_buff_pool *pool, struct xdp_sock *xs, + struct net_device *dev, u16 queue_id, u16 flags) +{ + return __xp_assign_dev(pool, xs, dev, queue_id, flags); +} + +int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *xs, + struct xdp_umem *umem, struct net_device *dev, + u16 queue_id) +{ + u16 flags; + + /* One fill and completion ring required for each queue id. */ + if (!pool->fq || !pool->cq) + return -EINVAL; + + flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY; + if (pool->uses_need_wakeup) + flags |= XDP_USE_NEED_WAKEUP; + + return __xp_assign_dev(pool, xs, dev, queue_id, flags); +} + void xp_clear_dev(struct xsk_buff_pool *pool) { if (!pool->netdev) -- 2.7.4