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 {
|
2020-08-28 16:26:21 +08:00
|
|
|
void *addrs;
|
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;
|
2020-08-28 16:26:23 +08:00
|
|
|
u32 npgs;
|
2018-06-04 20:05:51 +08:00
|
|
|
struct user_struct *user;
|
|
|
|
refcount_t users;
|
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;
|
2018-06-04 20:05:55 +08:00
|
|
|
bool zc;
|
2020-08-28 16:26:23 +08:00
|
|
|
struct page **pgs;
|
|
|
|
int id;
|
2020-08-28 16:26:22 +08:00
|
|
|
struct list_head xsk_dma_list;
|
xsk: Fix umem cleanup bug at socket destruct
Fix a bug that is triggered when a partially setup socket is
destroyed. For a fully setup socket, a socket that has been bound to a
device, the cleanup of the umem is performed at the end of the buffer
pool's cleanup work queue item. This has to be performed in a work
queue, and not in RCU cleanup, as it is doing a vunmap that cannot
execute in interrupt context. However, when a socket has only been
partially set up so that a umem has been created but the buffer pool
has not, the code erroneously directly calls the umem cleanup function
instead of using a work queue, and this leads to a BUG_ON() in
vunmap().
As there in this case is no buffer pool, we cannot use its work queue,
so we need to introduce a work queue for the umem and schedule this for
the cleanup. So in the case there is no pool, we are going to use the
umem's own work queue to schedule the cleanup. But if there is a
pool, the cleanup of the umem is still being performed by the pool's
work queue, as it is important that the umem is cleaned up after the
pool.
Fixes: e5e1a4bc916d ("xsk: Fix possible memory leak at socket close")
Reported-by: Marek Majtyka <marekx.majtyka@intel.com>
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Marek Majtyka <marekx.majtyka@intel.com>
Link: https://lore.kernel.org/bpf/1605873219-21629-1-git-send-email-magnus.karlsson@gmail.com
2020-11-20 19:53:39 +08:00
|
|
|
struct work_struct work;
|
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;
|
2020-08-28 16:26:23 +08:00
|
|
|
struct xsk_queue *rx ____cacheline_aligned_in_smp;
|
2018-05-02 19:01:25 +08:00
|
|
|
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;
|
2020-08-28 16:26:23 +08:00
|
|
|
|
2019-06-07 04:59:40 +08:00
|
|
|
struct xsk_queue *tx ____cacheline_aligned_in_smp;
|
2020-08-28 16:26:20 +08:00
|
|
|
struct list_head tx_list;
|
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:23 +08:00
|
|
|
/* Protects multiple processes in the control path */
|
|
|
|
struct mutex mutex;
|
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 */
|