]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/net/xdp_sock.h
xsk: Fix race in SKB mode transmit with shared cq
[mirror_ubuntu-hirsute-kernel.git] / include / net / xdp_sock.h
CommitLineData
dac09149
BT
1/* SPDX-License-Identifier: GPL-2.0 */
2/* AF_XDP internal functions
c0c77d8f 3 * Copyright(c) 2018 Intel Corporation.
c0c77d8f
BT
4 */
5
6#ifndef _LINUX_XDP_SOCK_H
7#define _LINUX_XDP_SOCK_H
8
e61e62b9
BT
9#include <linux/workqueue.h>
10#include <linux/if_xdp.h>
c0c77d8f 11#include <linux/mutex.h>
ac98d8aa 12#include <linux/spinlock.h>
e61e62b9 13#include <linux/mm.h>
c0c77d8f
BT
14#include <net/sock.h>
15
b9b6b68e
BT
16struct net_device;
17struct xsk_queue;
a71506a4 18struct xdp_buff;
e61e62b9 19
e61e62b9 20struct xdp_umem {
7f7ffa4e 21 void *addrs;
93ee30f3 22 u64 size;
e61e62b9 23 u32 headroom;
2b43470a 24 u32 chunk_size;
1c1efc2a 25 u32 chunks;
8ef4e27e 26 u32 npgs;
e61e62b9 27 struct user_struct *user;
e61e62b9 28 refcount_t users;
77cd0d7b 29 u8 flags;
173d3adb 30 bool zc;
8ef4e27e
MK
31 struct page **pgs;
32 int id;
921b6869 33 struct list_head xsk_dma_list;
537cf4e3 34 struct work_struct work;
e61e62b9 35};
c0c77d8f 36
d817991c
BT
37struct xsk_map {
38 struct bpf_map map;
d817991c
BT
39 spinlock_t lock; /* Synchronize map updates */
40 struct xdp_sock *xsk_map[];
41};
42
c0c77d8f
BT
43struct xdp_sock {
44 /* struct sock must be the first member of struct xdp_sock */
45 struct sock sk;
8ef4e27e 46 struct xsk_queue *rx ____cacheline_aligned_in_smp;
b9b6b68e 47 struct net_device *dev;
c0c77d8f 48 struct xdp_umem *umem;
fbfc504a 49 struct list_head flush_node;
c4655761 50 struct xsk_buff_pool *pool;
965a9909 51 u16 queue_id;
ac98d8aa 52 bool zc;
455302d1
IM
53 enum {
54 XSK_READY = 0,
55 XSK_BOUND,
56 XSK_UNBOUND,
57 } state;
8ef4e27e 58
fada7fdc 59 struct xsk_queue *tx ____cacheline_aligned_in_smp;
a5aa8e52 60 struct list_head tx_list;
bf0bdd13
IM
61 /* Protects generic receive. */
62 spinlock_t rx_lock;
8aa5a335
CL
63
64 /* Statistics */
c497176c 65 u64 rx_dropped;
8aa5a335
CL
66 u64 rx_queue_full;
67
0402acd6
BT
68 struct list_head map_list;
69 /* Protects map_list */
70 spinlock_t map_list_lock;
8ef4e27e
MK
71 /* Protects multiple processes in the control path */
72 struct mutex mutex;
7361f9c3
MK
73 struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */
74 struct xsk_queue *cq_tmp; /* Only as tmp storage before bind */
c0c77d8f
BT
75};
76
c497176c 77#ifdef CONFIG_XDP_SOCKETS
90254034 78
a71506a4 79int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
e312b9e7
BT
80int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
81void __xsk_map_flush(void);
d817991c
BT
82
83static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
84 u32 key)
85{
86 struct xsk_map *m = container_of(map, struct xsk_map, map);
87 struct xdp_sock *xs;
88
89 if (key >= map->max_entries)
90 return NULL;
91
92 xs = READ_ONCE(m->xsk_map[key]);
93 return xs;
94}
0402acd6 95
c497176c 96#else
a71506a4 97
c497176c
BT
98static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
99{
100 return -ENOTSUPP;
101}
102
a71506a4 103static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
f5bd9138 104{
a71506a4 105 return -EOPNOTSUPP;
f5bd9138
JK
106}
107
a71506a4 108static inline void __xsk_map_flush(void)
f5bd9138
JK
109{
110}
111
a71506a4
MK
112static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map,
113 u32 key)
1661d346
JK
114{
115 return NULL;
116}
117
c497176c
BT
118#endif /* CONFIG_XDP_SOCKETS */
119
c0c77d8f 120#endif /* _LINUX_XDP_SOCK_H */