Skip to content

Commit b5aea28

Browse files
magnus-karlssonborkmann
authored andcommitted
xsk: Add shared umem support between queue ids
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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 9647c57 commit b5aea28

File tree

3 files changed

+56
-16
lines changed

3 files changed

+56
-16
lines changed

include/net/xsk_buff_pool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
8181
struct xdp_umem *umem);
8282
int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
8383
u16 queue_id, u16 flags);
84+
int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
85+
struct net_device *dev, u16 queue_id);
8486
void xp_destroy(struct xsk_buff_pool *pool);
8587
void xp_release(struct xdp_buff_xsk *xskb);
8688
void xp_get_pool(struct xsk_buff_pool *pool);

net/xdp/xsk.c

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -689,12 +689,6 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
689689
goto out_unlock;
690690
}
691691

692-
if (xs->fq_tmp || xs->cq_tmp) {
693-
/* Do not allow setting your own fq or cq. */
694-
err = -EINVAL;
695-
goto out_unlock;
696-
}
697-
698692
sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
699693
if (IS_ERR(sock)) {
700694
err = PTR_ERR(sock);
@@ -707,15 +701,41 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
707701
sockfd_put(sock);
708702
goto out_unlock;
709703
}
710-
if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
704+
if (umem_xs->dev != dev) {
711705
err = -EINVAL;
712706
sockfd_put(sock);
713707
goto out_unlock;
714708
}
715709

716-
/* Share the buffer pool with the other socket. */
717-
xp_get_pool(umem_xs->pool);
718-
xs->pool = umem_xs->pool;
710+
if (umem_xs->queue_id != qid) {
711+
/* Share the umem with another socket on another qid */
712+
xs->pool = xp_create_and_assign_umem(xs,
713+
umem_xs->umem);
714+
if (!xs->pool) {
715+
sockfd_put(sock);
716+
goto out_unlock;
717+
}
718+
719+
err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
720+
dev, qid);
721+
if (err) {
722+
xp_destroy(xs->pool);
723+
sockfd_put(sock);
724+
goto out_unlock;
725+
}
726+
} else {
727+
/* Share the buffer pool with the other socket. */
728+
if (xs->fq_tmp || xs->cq_tmp) {
729+
/* Do not allow setting your own fq or cq. */
730+
err = -EINVAL;
731+
sockfd_put(sock);
732+
goto out_unlock;
733+
}
734+
735+
xp_get_pool(umem_xs->pool);
736+
xs->pool = umem_xs->pool;
737+
}
738+
719739
xdp_get_umem(umem_xs->umem);
720740
WRITE_ONCE(xs->umem, umem_xs->umem);
721741
sockfd_put(sock);
@@ -847,10 +867,6 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
847867
mutex_unlock(&xs->mutex);
848868
return -EBUSY;
849869
}
850-
if (!xs->umem) {
851-
mutex_unlock(&xs->mutex);
852-
return -EINVAL;
853-
}
854870

855871
q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
856872
&xs->cq_tmp;

net/xdp/xsk_buff_pool.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
123123
}
124124
}
125125

126-
int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *netdev,
127-
u16 queue_id, u16 flags)
126+
static int __xp_assign_dev(struct xsk_buff_pool *pool,
127+
struct net_device *netdev, u16 queue_id, u16 flags)
128128
{
129129
bool force_zc, force_copy;
130130
struct netdev_bpf bpf;
@@ -193,6 +193,28 @@ int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *netdev,
193193
return err;
194194
}
195195

196+
int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
197+
u16 queue_id, u16 flags)
198+
{
199+
return __xp_assign_dev(pool, dev, queue_id, flags);
200+
}
201+
202+
int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
203+
struct net_device *dev, u16 queue_id)
204+
{
205+
u16 flags;
206+
207+
/* One fill and completion ring required for each queue id. */
208+
if (!pool->fq || !pool->cq)
209+
return -EINVAL;
210+
211+
flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
212+
if (pool->uses_need_wakeup)
213+
flags |= XDP_USE_NEED_WAKEUP;
214+
215+
return __xp_assign_dev(pool, dev, queue_id, flags);
216+
}
217+
196218
void xp_clear_dev(struct xsk_buff_pool *pool)
197219
{
198220
if (!pool->netdev)

0 commit comments

Comments
 (0)