2018-05-18 12:00:21 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
/* AF_XDP internal functions
|
2018-05-02 11:01:23 +00:00
|
|
|
* Copyright(c) 2018 Intel Corporation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINUX_XDP_SOCK_H
|
|
|
|
#define _LINUX_XDP_SOCK_H
|
|
|
|
|
2018-06-04 12:05:51 +00:00
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/if_xdp.h>
|
2018-05-02 11:01:23 +00:00
|
|
|
#include <linux/mutex.h>
|
2018-06-04 12:05:57 +00:00
|
|
|
#include <linux/spinlock.h>
|
2018-06-04 12:05:51 +00:00
|
|
|
#include <linux/mm.h>
|
2018-05-02 11:01:23 +00:00
|
|
|
#include <net/sock.h>
|
|
|
|
|
2018-05-02 11:01:25 +00:00
|
|
|
struct net_device;
|
|
|
|
struct xsk_queue;
|
2020-05-20 19:20:51 +00:00
|
|
|
struct xdp_buff;
|
2018-06-04 12:05:51 +00:00
|
|
|
|
|
|
|
struct xdp_umem {
|
|
|
|
struct xsk_queue *fq;
|
|
|
|
struct xsk_queue *cq;
|
2020-05-20 19:20:53 +00:00
|
|
|
struct xsk_buff_pool *pool;
|
2018-08-31 11:40:02 +00:00
|
|
|
u64 size;
|
2018-06-04 12:05:51 +00:00
|
|
|
u32 headroom;
|
2020-05-20 19:20:53 +00:00
|
|
|
u32 chunk_size;
|
2018-06-04 12:05:51 +00:00
|
|
|
struct user_struct *user;
|
|
|
|
refcount_t users;
|
|
|
|
struct work_struct work;
|
2018-06-04 12:05:52 +00:00
|
|
|
struct page **pgs;
|
2018-06-04 12:05:51 +00:00
|
|
|
u32 npgs;
|
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>
2019-08-14 07:27:17 +00:00
|
|
|
u16 queue_id;
|
|
|
|
u8 need_wakeup;
|
|
|
|
u8 flags;
|
2019-01-24 18:59:38 +00:00
|
|
|
int id;
|
2018-06-04 12:05:55 +00:00
|
|
|
struct net_device *dev;
|
|
|
|
bool zc;
|
2020-05-04 13:33:51 +00:00
|
|
|
spinlock_t xsk_tx_list_lock;
|
|
|
|
struct list_head xsk_tx_list;
|
2018-06-04 12:05:51 +00:00
|
|
|
};
|
2018-05-02 11:01:23 +00:00
|
|
|
|
2019-11-01 11:03:46 +00:00
|
|
|
struct xsk_map {
|
|
|
|
struct bpf_map map;
|
|
|
|
spinlock_t lock; /* Synchronize map updates */
|
|
|
|
struct xdp_sock *xsk_map[];
|
|
|
|
};
|
|
|
|
|
2018-05-02 11:01:23 +00:00
|
|
|
struct xdp_sock {
|
|
|
|
/* struct sock must be the first member of struct xdp_sock */
|
|
|
|
struct sock sk;
|
2018-05-02 11:01:25 +00:00
|
|
|
struct xsk_queue *rx;
|
|
|
|
struct net_device *dev;
|
2018-05-02 11:01:23 +00:00
|
|
|
struct xdp_umem *umem;
|
2018-05-02 11:01:28 +00:00
|
|
|
struct list_head flush_node;
|
2018-05-02 11:01:26 +00:00
|
|
|
u16 queue_id;
|
2018-06-04 12:05:57 +00:00
|
|
|
bool zc;
|
2019-06-28 08:04:07 +00:00
|
|
|
enum {
|
|
|
|
XSK_READY = 0,
|
|
|
|
XSK_BOUND,
|
|
|
|
XSK_UNBOUND,
|
|
|
|
} state;
|
2018-05-02 11:01:23 +00:00
|
|
|
/* Protects multiple processes in the control path */
|
|
|
|
struct mutex mutex;
|
2019-06-06 20:59:40 +00:00
|
|
|
struct xsk_queue *tx ____cacheline_aligned_in_smp;
|
|
|
|
struct list_head list;
|
2018-06-29 07:48:20 +00:00
|
|
|
/* Mutual exclusion of NAPI TX thread and sendmsg error paths
|
|
|
|
* in the SKB destructor callback.
|
|
|
|
*/
|
|
|
|
spinlock_t tx_completion_lock;
|
2019-07-03 12:09:16 +00:00
|
|
|
/* Protects generic receive. */
|
|
|
|
spinlock_t rx_lock;
|
2018-05-02 11:01:27 +00:00
|
|
|
u64 rx_dropped;
|
2019-08-15 09:30:13 +00:00
|
|
|
struct list_head map_list;
|
|
|
|
/* Protects map_list */
|
|
|
|
spinlock_t map_list_lock;
|
2018-05-02 11:01:23 +00:00
|
|
|
};
|
|
|
|
|
2018-05-02 11:01:27 +00:00
|
|
|
#ifdef CONFIG_XDP_SOCKETS
|
2018-08-28 12:44:27 +00:00
|
|
|
|
2020-05-20 19:20:51 +00:00
|
|
|
int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
|
2019-12-19 06:10:02 +00:00
|
|
|
int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
|
|
|
|
void __xsk_map_flush(void);
|
2019-11-01 11:03:46 +00:00
|
|
|
|
|
|
|
static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
|
|
|
|
u32 key)
|
|
|
|
{
|
|
|
|
struct xsk_map *m = container_of(map, struct xsk_map, map);
|
|
|
|
struct xdp_sock *xs;
|
|
|
|
|
|
|
|
if (key >= map->max_entries)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
xs = READ_ONCE(m->xsk_map[key]);
|
|
|
|
return xs;
|
|
|
|
}
|
2019-08-15 09:30:13 +00:00
|
|
|
|
2018-05-02 11:01:27 +00:00
|
|
|
#else
|
2020-05-20 19:20:51 +00:00
|
|
|
|
2018-05-02 11:01:27 +00:00
|
|
|
static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
|
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:20:51 +00:00
|
|
|
static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
|
2018-09-07 08:18:46 +00:00
|
|
|
{
|
2020-05-20 19:20:51 +00:00
|
|
|
return -EOPNOTSUPP;
|
2018-09-07 08:18:46 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 19:20:51 +00:00
|
|
|
static inline void __xsk_map_flush(void)
|
2018-09-07 08:18:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:20:51 +00:00
|
|
|
static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
|
|
|
|
u32 key)
|
2018-10-01 12:51:36 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-02 11:01:27 +00:00
|
|
|
#endif /* CONFIG_XDP_SOCKETS */
|
|
|
|
|
2018-05-02 11:01:23 +00:00
|
|
|
#endif /* _LINUX_XDP_SOCK_H */
|