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