Merge tag 'probes-fixes-v6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / include / net / xsk_buff_pool.h
CommitLineData
2b43470a
BT
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright(c) 2020 Intel Corporation. */
3
4#ifndef XSK_BUFF_POOL_H_
5#define XSK_BUFF_POOL_H_
6
26062b18 7#include <linux/if_xdp.h>
2b43470a
BT
8#include <linux/types.h>
9#include <linux/dma-mapping.h>
94033cd8 10#include <linux/bpf.h>
2b43470a
BT
11#include <net/xdp.h>
12
13struct xsk_buff_pool;
14struct xdp_rxq_info;
b4e352ff 15struct xsk_cb_desc;
2b43470a
BT
16struct xsk_queue;
17struct xdp_desc;
1742b3d5 18struct xdp_umem;
1c1efc2a 19struct xdp_sock;
2b43470a
BT
20struct device;
21struct page;
22
94ecc5ca
THJ
23#define XSK_PRIV_MAX 24
24
2b43470a
BT
25struct xdp_buff_xsk {
26 struct xdp_buff xdp;
94ecc5ca 27 u8 cb[XSK_PRIV_MAX];
2b43470a
BT
28 dma_addr_t dma;
29 dma_addr_t frame_dma;
30 struct xsk_buff_pool *pool;
30ec2c1b 31 struct list_head list_node;
ca5c9494 32} __aligned_largest;
2b43470a 33
94ecc5ca 34#define XSK_CHECK_PRIV_TYPE(t) BUILD_BUG_ON(sizeof(t) > offsetofend(struct xdp_buff_xsk, cb))
48eb03dd 35#define XSK_TX_COMPL_FITS(t) BUILD_BUG_ON(sizeof(struct xsk_tx_metadata_compl) > sizeof(t))
94ecc5ca 36
921b6869
MK
37struct xsk_dma_map {
38 dma_addr_t *dma_pages;
39 struct device *dev;
40 struct net_device *netdev;
41 refcount_t users;
42 struct list_head list; /* Protected by the RTNL_LOCK */
43 u32 dma_pages_cnt;
921b6869
MK
44};
45
26062b18 46struct xsk_buff_pool {
8ef4e27e
MK
47 /* Members only used in the control path first. */
48 struct device *dev;
49 struct net_device *netdev;
50 struct list_head xsk_tx_list;
51 /* Protects modifications to the xsk_tx_list */
52 spinlock_t xsk_tx_list_lock;
53 refcount_t users;
54 struct xdp_umem *umem;
55 struct work_struct work;
a1356ac7 56 /* Protects generic receive in shared and non-shared umem mode. */
57 spinlock_t rx_lock;
26062b18 58 struct list_head free_list;
24ea5012 59 struct list_head xskb_list;
8ef4e27e
MK
60 u32 heads_cnt;
61 u16 queue_id;
62
63 /* Data path members as close to free_heads at the end as possible. */
64 struct xsk_queue *fq ____cacheline_aligned_in_smp;
65 struct xsk_queue *cq;
921b6869
MK
66 /* For performance reasons, each buff pool has its own array of dma_pages
67 * even when they are identical.
68 */
26062b18
BT
69 dma_addr_t *dma_pages;
70 struct xdp_buff_xsk *heads;
d1bc532e 71 struct xdp_desc *tx_descs;
26062b18
BT
72 u64 chunk_mask;
73 u64 addrs_cnt;
74 u32 free_list_cnt;
75 u32 dma_pages_cnt;
26062b18
BT
76 u32 free_heads_cnt;
77 u32 headroom;
78 u32 chunk_size;
94033cd8 79 u32 chunk_shift;
26062b18 80 u32 frame_len;
6e126872 81 u32 xdp_zc_max_segs;
341ac980 82 u8 tx_metadata_len; /* inherited from umem */
c2d3d6a4
MK
83 u8 cached_need_wakeup;
84 bool uses_need_wakeup;
26062b18 85 bool unaligned;
11614723 86 bool tx_sw_csum;
26062b18 87 void *addrs;
f09ced40
MK
88 /* Mutual exclusion of the completion ring in the SKB mode. Two cases to protect:
89 * NAPI TX thread and sendmsg error paths in the SKB destructor callback and when
90 * sockets share a single cq when the same netdev and queue id is shared.
91 */
92 spinlock_t cq_lock;
26062b18
BT
93 struct xdp_buff_xsk *free_heads[];
94};
95
94033cd8
MK
96/* Masks for xdp_umem_page flags.
97 * The low 12-bits of the addr will be 0 since this is the page address, so we
98 * can use them for flags.
99 */
100#define XSK_NEXT_PG_CONTIG_SHIFT 0
101#define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
102
2b43470a 103/* AF_XDP core. */
1c1efc2a
MK
104struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
105 struct xdp_umem *umem);
106int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
107 u16 queue_id, u16 flags);
60240bc2 108int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
b5aea28d 109 struct net_device *dev, u16 queue_id);
ba3beec2 110int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs);
2b43470a 111void xp_destroy(struct xsk_buff_pool *pool);
1c1efc2a 112void xp_get_pool(struct xsk_buff_pool *pool);
e5e1a4bc 113bool xp_put_pool(struct xsk_buff_pool *pool);
1c1efc2a 114void xp_clear_dev(struct xsk_buff_pool *pool);
a5aa8e52
MK
115void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
116void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
2b43470a
BT
117
118/* AF_XDP, and XDP core. */
119void xp_free(struct xdp_buff_xsk *xskb);
120
94033cd8
MK
121static inline void xp_init_xskb_addr(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
122 u64 addr)
123{
94033cd8
MK
124 xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom;
125}
126
127static inline void xp_init_xskb_dma(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
128 dma_addr_t *dma_pages, u64 addr)
129{
130 xskb->frame_dma = (dma_pages[addr >> PAGE_SHIFT] & ~XSK_NEXT_PG_CONTIG_MASK) +
131 (addr & ~PAGE_MASK);
132 xskb->dma = xskb->frame_dma + pool->headroom + XDP_PACKET_HEADROOM;
133}
134
2b43470a
BT
135/* AF_XDP ZC drivers, via xdp_sock_buff.h */
136void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
b4e352ff 137void xp_fill_cb(struct xsk_buff_pool *pool, struct xsk_cb_desc *desc);
2b43470a
BT
138int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
139 unsigned long attrs, struct page **pages, u32 nr_pages);
140void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
141struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
47e4075d 142u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max);
2b43470a
BT
143bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
144void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
145dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
23d9324a
AL
146
147struct xdp_desc_ctx {
148 dma_addr_t dma;
149 struct xsk_tx_metadata *meta;
150};
151
152struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr);
153
26062b18
BT
154static inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
155{
156 return xskb->dma;
157}
158
159static inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
160{
161 return xskb->frame_dma;
162}
163
26062b18
BT
164static inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
165{
163943ac
AL
166 dma_sync_single_for_cpu(xskb->pool->dev, xskb->dma,
167 xskb->pool->frame_len,
168 DMA_BIDIRECTIONAL);
26062b18
BT
169}
170
26062b18
BT
171static inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
172 dma_addr_t dma, size_t size)
173{
163943ac 174 dma_sync_single_for_device(pool->dev, dma, size, DMA_BIDIRECTIONAL);
26062b18
BT
175}
176
177/* Masks for xdp_umem_page flags.
178 * The low 12-bits of the addr will be 0 since this is the page address, so we
179 * can use them for flags.
180 */
181#define XSK_NEXT_PG_CONTIG_SHIFT 0
182#define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
183
184static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
185 u64 addr, u32 len)
186{
187 bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
188
2f996198
MK
189 if (likely(!cross_pg))
190 return false;
191
6ec7be9a 192 return pool->dma_pages &&
d769ccaf 193 !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);
26062b18
BT
194}
195
dcf3827c 196static inline bool xp_mb_desc(const struct xdp_desc *desc)
1b725b0c
MF
197{
198 return desc->options & XDP_PKT_CONTD;
199}
200
26062b18
BT
201static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
202{
203 return addr & pool->chunk_mask;
204}
205
206static inline u64 xp_unaligned_extract_addr(u64 addr)
207{
208 return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
209}
210
211static inline u64 xp_unaligned_extract_offset(u64 addr)
212{
213 return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
214}
215
216static inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
217{
218 return xp_unaligned_extract_addr(addr) +
219 xp_unaligned_extract_offset(addr);
220}
2b43470a 221
94033cd8
MK
222static inline u32 xp_aligned_extract_idx(struct xsk_buff_pool *pool, u64 addr)
223{
224 return xp_aligned_extract_addr(pool, addr) >> pool->chunk_shift;
225}
226
227static inline void xp_release(struct xdp_buff_xsk *xskb)
228{
229 if (xskb->pool->unaligned)
230 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
231}
232
bea14124
MF
233static inline u64 xp_get_handle(struct xdp_buff_xsk *xskb,
234 struct xsk_buff_pool *pool)
94033cd8 235{
bea14124
MF
236 u64 orig_addr = xskb->xdp.data - pool->addrs;
237 u64 offset;
94033cd8 238
bea14124
MF
239 if (!pool->unaligned)
240 return orig_addr;
241
242 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
bea14124 243 offset += pool->headroom;
bf20af07 244 orig_addr -= offset;
bea14124 245 return orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
94033cd8
MK
246}
247
48eb03dd
SF
248static inline bool xp_tx_metadata_enabled(const struct xsk_buff_pool *pool)
249{
250 return pool->tx_metadata_len > 0;
251}
252
2b43470a 253#endif /* XSK_BUFF_POOL_H_ */