Skip to content

Commit 77cd0d7

Browse files
magnus-karlssonborkmann
authored andcommitted
xsk: add support for need_wakeup flag in AF_XDP rings
This commit adds support for a new flag called need_wakeup in the AF_XDP Tx and fill rings. When this flag is set, it means that the application has to explicitly wake up the kernel Rx (for the bit in the fill ring) or kernel Tx (for bit in the Tx ring) processing by issuing a syscall. Poll() can wake up both depending on the flags submitted and sendto() will wake up tx processing only. The main reason for introducing this new flag is to be able to efficiently support the case when application and driver is executing on the same core. Previously, the driver was just busy-spinning on the fill ring if it ran out of buffers in the HW and there were none on the fill ring. This approach works when the application is running on another core as it can replenish the fill ring while the driver is busy-spinning. Though, this is a lousy approach if both of them are running on the same core as the probability of the fill ring getting more entries when the driver is busy-spinning is zero. With this new feature the driver now sets the need_wakeup flag and returns to the application. The application can then replenish the fill queue and then explicitly wake up the Rx processing in the kernel using the syscall poll(). For Tx, the flag is only set to one if the driver has no outstanding Tx completion interrupts. If it has some, the flag is zero as it will be woken up by a completion interrupt anyway. As a nice side effect, this new flag also improves the performance of the case where application and driver are running on two different cores as it reduces the number of syscalls to the kernel. The kernel tells user space if it needs to be woken up by a syscall, and this eliminates many of the syscalls. This flag needs some simple driver support. If the driver does not support this, the Rx flag is always zero and the Tx flag is always one. This makes any application relying on this feature default to the old behaviour of not requiring any syscalls in the Rx path and always having to call sendto() in the Tx path. For backwards compatibility reasons, this feature has to be explicitly turned on using a new bind flag (XDP_USE_NEED_WAKEUP). I recommend that you always turn it on as it so far always have had a positive performance impact. The name and inspiration of the flag has been taken from io_uring by Jens Axboe. Details about this feature in io_uring can be found in http://kernel.dk/io_uring.pdf, section 8.3. Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> Acked-by: Jonathan Lemon <jonathan.lemon@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 9116e5e commit 77cd0d7

6 files changed

Lines changed: 195 additions & 20 deletions

File tree

‎include/net/xdp_sock.h‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ struct xdp_umem_fq_reuse {
2727
u64 handles[];
2828
};
2929

30+
/* Flags for the umem flags field. */
31+
#define XDP_UMEM_USES_NEED_WAKEUP (1 << 0)
32+
3033
struct xdp_umem {
3134
struct xsk_queue *fq;
3235
struct xsk_queue *cq;
@@ -41,10 +44,12 @@ struct xdp_umem {
4144
struct work_struct work;
4245
struct page **pgs;
4346
u32 npgs;
47+
u16 queue_id;
48+
u8 need_wakeup;
49+
u8 flags;
4450
int id;
4551
struct net_device *dev;
4652
struct xdp_umem_fq_reuse *fq_reuse;
47-
u16 queue_id;
4853
bool zc;
4954
spinlock_t xsk_list_lock;
5055
struct list_head xsk_list;
@@ -95,6 +100,11 @@ struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,
95100
struct xdp_umem_fq_reuse *newq);
96101
void xsk_reuseq_free(struct xdp_umem_fq_reuse *rq);
97102
struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, u16 queue_id);
103+
void xsk_set_rx_need_wakeup(struct xdp_umem *umem);
104+
void xsk_set_tx_need_wakeup(struct xdp_umem *umem);
105+
void xsk_clear_rx_need_wakeup(struct xdp_umem *umem);
106+
void xsk_clear_tx_need_wakeup(struct xdp_umem *umem);
107+
bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem);
98108

99109
static inline char *xdp_umem_get_data(struct xdp_umem *umem, u64 addr)
100110
{
@@ -241,6 +251,27 @@ static inline void xsk_umem_fq_reuse(struct xdp_umem *umem, u64 addr)
241251
{
242252
}
243253

254+
static inline void xsk_set_rx_need_wakeup(struct xdp_umem *umem)
255+
{
256+
}
257+
258+
static inline void xsk_set_tx_need_wakeup(struct xdp_umem *umem)
259+
{
260+
}
261+
262+
static inline void xsk_clear_rx_need_wakeup(struct xdp_umem *umem)
263+
{
264+
}
265+
266+
static inline void xsk_clear_tx_need_wakeup(struct xdp_umem *umem)
267+
{
268+
}
269+
270+
static inline bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem)
271+
{
272+
return false;
273+
}
274+
244275
#endif /* CONFIG_XDP_SOCKETS */
245276

246277
#endif /* _LINUX_XDP_SOCK_H */

‎include/uapi/linux/if_xdp.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
#define XDP_SHARED_UMEM (1 << 0)
1717
#define XDP_COPY (1 << 1) /* Force copy-mode */
1818
#define XDP_ZEROCOPY (1 << 2) /* Force zero-copy mode */
19+
/* If this option is set, the driver might go sleep and in that case
20+
* the XDP_RING_NEED_WAKEUP flag in the fill and/or Tx rings will be
21+
* set. If it is set, the application need to explicitly wake up the
22+
* driver with a poll() (Rx and Tx) or sendto() (Tx only). If you are
23+
* running the driver and the application on the same core, you should
24+
* use this option so that the kernel will yield to the user space
25+
* application.
26+
*/
27+
#define XDP_USE_NEED_WAKEUP (1 << 3)
1928

2029
struct sockaddr_xdp {
2130
__u16 sxdp_family;
@@ -25,10 +34,14 @@ struct sockaddr_xdp {
2534
__u32 sxdp_shared_umem_fd;
2635
};
2736

37+
/* XDP_RING flags */
38+
#define XDP_RING_NEED_WAKEUP (1 << 0)
39+
2840
struct xdp_ring_offset {
2941
__u64 producer;
3042
__u64 consumer;
3143
__u64 desc;
44+
__u64 flags;
3245
};
3346

3447
struct xdp_mmap_offsets {

‎net/xdp/xdp_umem.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
106106
umem->dev = dev;
107107
umem->queue_id = queue_id;
108108

109+
if (flags & XDP_USE_NEED_WAKEUP) {
110+
umem->flags |= XDP_UMEM_USES_NEED_WAKEUP;
111+
/* Tx needs to be explicitly woken up the first time.
112+
* Also for supporting drivers that do not implement this
113+
* feature. They will always have to call sendto().
114+
*/
115+
xsk_set_tx_need_wakeup(umem);
116+
}
117+
109118
dev_hold(dev);
110119

111120
if (force_copy)

‎net/xdp/xsk.c‎

Lines changed: 127 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,66 @@ void xsk_umem_discard_addr(struct xdp_umem *umem)
5555
}
5656
EXPORT_SYMBOL(xsk_umem_discard_addr);
5757

58+
void xsk_set_rx_need_wakeup(struct xdp_umem *umem)
59+
{
60+
if (umem->need_wakeup & XDP_WAKEUP_RX)
61+
return;
62+
63+
umem->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
64+
umem->need_wakeup |= XDP_WAKEUP_RX;
65+
}
66+
EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
67+
68+
void xsk_set_tx_need_wakeup(struct xdp_umem *umem)
69+
{
70+
struct xdp_sock *xs;
71+
72+
if (umem->need_wakeup & XDP_WAKEUP_TX)
73+
return;
74+
75+
rcu_read_lock();
76+
list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
77+
xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
78+
}
79+
rcu_read_unlock();
80+
81+
umem->need_wakeup |= XDP_WAKEUP_TX;
82+
}
83+
EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
84+
85+
void xsk_clear_rx_need_wakeup(struct xdp_umem *umem)
86+
{
87+
if (!(umem->need_wakeup & XDP_WAKEUP_RX))
88+
return;
89+
90+
umem->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
91+
umem->need_wakeup &= ~XDP_WAKEUP_RX;
92+
}
93+
EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
94+
95+
void xsk_clear_tx_need_wakeup(struct xdp_umem *umem)
96+
{
97+
struct xdp_sock *xs;
98+
99+
if (!(umem->need_wakeup & XDP_WAKEUP_TX))
100+
return;
101+
102+
rcu_read_lock();
103+
list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
104+
xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
105+
}
106+
rcu_read_unlock();
107+
108+
umem->need_wakeup &= ~XDP_WAKEUP_TX;
109+
}
110+
EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
111+
112+
bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem)
113+
{
114+
return umem->flags & XDP_UMEM_USES_NEED_WAKEUP;
115+
}
116+
EXPORT_SYMBOL(xsk_umem_uses_need_wakeup);
117+
58118
static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
59119
{
60120
void *to_buf, *from_buf;
@@ -320,6 +380,12 @@ static unsigned int xsk_poll(struct file *file, struct socket *sock,
320380
unsigned int mask = datagram_poll(file, sock, wait);
321381
struct sock *sk = sock->sk;
322382
struct xdp_sock *xs = xdp_sk(sk);
383+
struct net_device *dev = xs->dev;
384+
struct xdp_umem *umem = xs->umem;
385+
386+
if (umem->need_wakeup)
387+
dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id,
388+
umem->need_wakeup);
323389

324390
if (xs->rx && !xskq_empty_desc(xs->rx))
325391
mask |= POLLIN | POLLRDNORM;
@@ -428,7 +494,8 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
428494
return -EINVAL;
429495

430496
flags = sxdp->sxdp_flags;
431-
if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY))
497+
if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
498+
XDP_USE_NEED_WAKEUP))
432499
return -EINVAL;
433500

434501
rtnl_lock();
@@ -455,7 +522,8 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
455522
struct xdp_sock *umem_xs;
456523
struct socket *sock;
457524

458-
if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY)) {
525+
if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
526+
(flags & XDP_USE_NEED_WAKEUP)) {
459527
/* Cannot specify flags for shared sockets. */
460528
err = -EINVAL;
461529
goto out_unlock;
@@ -550,6 +618,9 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
550618
}
551619
q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
552620
err = xsk_init_queue(entries, q, false);
621+
if (!err && optname == XDP_TX_RING)
622+
/* Tx needs to be explicitly woken up the first time */
623+
xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
553624
mutex_unlock(&xs->mutex);
554625
return err;
555626
}
@@ -611,6 +682,20 @@ static int xsk_setsockopt(struct socket *sock, int level, int optname,
611682
return -ENOPROTOOPT;
612683
}
613684

685+
static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
686+
{
687+
ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
688+
ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
689+
ring->desc = offsetof(struct xdp_rxtx_ring, desc);
690+
}
691+
692+
static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
693+
{
694+
ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
695+
ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
696+
ring->desc = offsetof(struct xdp_umem_ring, desc);
697+
}
698+
614699
static int xsk_getsockopt(struct socket *sock, int level, int optname,
615700
char __user *optval, int __user *optlen)
616701
{
@@ -650,26 +735,49 @@ static int xsk_getsockopt(struct socket *sock, int level, int optname,
650735
case XDP_MMAP_OFFSETS:
651736
{
652737
struct xdp_mmap_offsets off;
738+
struct xdp_mmap_offsets_v1 off_v1;
739+
bool flags_supported = true;
740+
void *to_copy;
653741

654-
if (len < sizeof(off))
742+
if (len < sizeof(off_v1))
655743
return -EINVAL;
744+
else if (len < sizeof(off))
745+
flags_supported = false;
746+
747+
if (flags_supported) {
748+
/* xdp_ring_offset is identical to xdp_ring_offset_v1
749+
* except for the flags field added to the end.
750+
*/
751+
xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
752+
&off.rx);
753+
xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
754+
&off.tx);
755+
xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
756+
&off.fr);
757+
xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
758+
&off.cr);
759+
off.rx.flags = offsetof(struct xdp_rxtx_ring,
760+
ptrs.flags);
761+
off.tx.flags = offsetof(struct xdp_rxtx_ring,
762+
ptrs.flags);
763+
off.fr.flags = offsetof(struct xdp_umem_ring,
764+
ptrs.flags);
765+
off.cr.flags = offsetof(struct xdp_umem_ring,
766+
ptrs.flags);
767+
768+
len = sizeof(off);
769+
to_copy = &off;
770+
} else {
771+
xsk_enter_rxtx_offsets(&off_v1.rx);
772+
xsk_enter_rxtx_offsets(&off_v1.tx);
773+
xsk_enter_umem_offsets(&off_v1.fr);
774+
xsk_enter_umem_offsets(&off_v1.cr);
775+
776+
len = sizeof(off_v1);
777+
to_copy = &off_v1;
778+
}
656779

657-
off.rx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
658-
off.rx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
659-
off.rx.desc = offsetof(struct xdp_rxtx_ring, desc);
660-
off.tx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
661-
off.tx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
662-
off.tx.desc = offsetof(struct xdp_rxtx_ring, desc);
663-
664-
off.fr.producer = offsetof(struct xdp_umem_ring, ptrs.producer);
665-
off.fr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
666-
off.fr.desc = offsetof(struct xdp_umem_ring, desc);
667-
off.cr.producer = offsetof(struct xdp_umem_ring, ptrs.producer);
668-
off.cr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
669-
off.cr.desc = offsetof(struct xdp_umem_ring, desc);
670-
671-
len = sizeof(off);
672-
if (copy_to_user(optval, &off, len))
780+
if (copy_to_user(optval, to_copy, len))
673781
return -EFAULT;
674782
if (put_user(len, optlen))
675783
return -EFAULT;

‎net/xdp/xsk.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
#ifndef XSK_H_
55
#define XSK_H_
66

7+
struct xdp_ring_offset_v1 {
8+
__u64 producer;
9+
__u64 consumer;
10+
__u64 desc;
11+
};
12+
13+
struct xdp_mmap_offsets_v1 {
14+
struct xdp_ring_offset_v1 rx;
15+
struct xdp_ring_offset_v1 tx;
16+
struct xdp_ring_offset_v1 fr;
17+
struct xdp_ring_offset_v1 cr;
18+
};
19+
720
static inline struct xdp_sock *xdp_sk(struct sock *sk)
821
{
922
return (struct xdp_sock *)sk;

‎net/xdp/xsk_queue.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
struct xdp_ring {
1717
u32 producer ____cacheline_aligned_in_smp;
1818
u32 consumer ____cacheline_aligned_in_smp;
19+
u32 flags;
1920
};
2021

2122
/* Used for the RX and TX queues for packets */

0 commit comments

Comments
 (0)