1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Interface for implementing AF_XDP zero-copy support in drivers.
3  * Copyright(c) 2020 Intel Corporation.
4  */
5 
6 #ifndef _LINUX_XDP_SOCK_DRV_H
7 #define _LINUX_XDP_SOCK_DRV_H
8 
9 #include <net/xdp_sock.h>
10 #include <net/xsk_buff_pool.h>
11 
12 #define XDP_UMEM_MIN_CHUNK_SHIFT 11
13 #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
14 
15 #ifdef CONFIG_XDP_SOCKETS
16 
17 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
18 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
19 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
20 void xsk_tx_release(struct xsk_buff_pool *pool);
21 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
22 					    u16 queue_id);
23 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
24 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
25 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
26 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
27 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
28 
xsk_pool_get_headroom(struct xsk_buff_pool * pool)29 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
30 {
31 	return XDP_PACKET_HEADROOM + pool->headroom;
32 }
33 
xsk_pool_get_chunk_size(struct xsk_buff_pool * pool)34 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
35 {
36 	return pool->chunk_size;
37 }
38 
xsk_pool_get_rx_frame_size(struct xsk_buff_pool * pool)39 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
40 {
41 	return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
42 }
43 
xsk_pool_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)44 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
45 					 struct xdp_rxq_info *rxq)
46 {
47 	xp_set_rxq_info(pool, rxq);
48 }
49 
xsk_pool_get_napi_id(struct xsk_buff_pool * pool)50 static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
51 {
52 #ifdef CONFIG_NET_RX_BUSY_POLL
53 	return pool->heads[0].xdp.rxq->napi_id;
54 #else
55 	return 0;
56 #endif
57 }
58 
xsk_pool_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)59 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
60 				      unsigned long attrs)
61 {
62 	xp_dma_unmap(pool, attrs);
63 }
64 
xsk_pool_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs)65 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
66 				   struct device *dev, unsigned long attrs)
67 {
68 	struct xdp_umem *umem = pool->umem;
69 
70 	return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
71 }
72 
xsk_buff_xdp_get_dma(struct xdp_buff * xdp)73 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
74 {
75 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
76 
77 	return xp_get_dma(xskb);
78 }
79 
xsk_buff_xdp_get_frame_dma(struct xdp_buff * xdp)80 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
81 {
82 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
83 
84 	return xp_get_frame_dma(xskb);
85 }
86 
xsk_buff_alloc(struct xsk_buff_pool * pool)87 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
88 {
89 	return xp_alloc(pool);
90 }
91 
xsk_is_eop_desc(struct xdp_desc * desc)92 static inline bool xsk_is_eop_desc(struct xdp_desc *desc)
93 {
94 	return !xp_mb_desc(desc);
95 }
96 
97 /* Returns as many entries as possible up to max. 0 <= N <= max. */
xsk_buff_alloc_batch(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 max)98 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
99 {
100 	return xp_alloc_batch(pool, xdp, max);
101 }
102 
xsk_buff_can_alloc(struct xsk_buff_pool * pool,u32 count)103 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
104 {
105 	return xp_can_alloc(pool, count);
106 }
107 
xsk_buff_free(struct xdp_buff * xdp)108 static inline void xsk_buff_free(struct xdp_buff *xdp)
109 {
110 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
111 	struct list_head *xskb_list = &xskb->pool->xskb_list;
112 	struct xdp_buff_xsk *pos, *tmp;
113 
114 	if (likely(!xdp_buff_has_frags(xdp)))
115 		goto out;
116 
117 	list_for_each_entry_safe(pos, tmp, xskb_list, xskb_list_node) {
118 		list_del(&pos->xskb_list_node);
119 		xp_free(pos);
120 	}
121 
122 	xdp_get_shared_info_from_buff(xdp)->nr_frags = 0;
123 out:
124 	xp_free(xskb);
125 }
126 
xsk_buff_add_frag(struct xdp_buff * xdp)127 static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
128 {
129 	struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp);
130 
131 	list_add_tail(&frag->xskb_list_node, &frag->pool->xskb_list);
132 }
133 
xsk_buff_get_frag(struct xdp_buff * first)134 static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
135 {
136 	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
137 	struct xdp_buff *ret = NULL;
138 	struct xdp_buff_xsk *frag;
139 
140 	frag = list_first_entry_or_null(&xskb->pool->xskb_list,
141 					struct xdp_buff_xsk, xskb_list_node);
142 	if (frag) {
143 		list_del(&frag->xskb_list_node);
144 		ret = &frag->xdp;
145 	}
146 
147 	return ret;
148 }
149 
xsk_buff_del_tail(struct xdp_buff * tail)150 static inline void xsk_buff_del_tail(struct xdp_buff *tail)
151 {
152 	struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp);
153 
154 	list_del(&xskb->xskb_list_node);
155 }
156 
xsk_buff_get_tail(struct xdp_buff * first)157 static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
158 {
159 	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
160 	struct xdp_buff_xsk *frag;
161 
162 	frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
163 			       xskb_list_node);
164 	return &frag->xdp;
165 }
166 
xsk_buff_set_size(struct xdp_buff * xdp,u32 size)167 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
168 {
169 	xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
170 	xdp->data_meta = xdp->data;
171 	xdp->data_end = xdp->data + size;
172 	xdp->flags = 0;
173 }
174 
xsk_buff_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)175 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
176 					      u64 addr)
177 {
178 	return xp_raw_get_dma(pool, addr);
179 }
180 
xsk_buff_raw_get_data(struct xsk_buff_pool * pool,u64 addr)181 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
182 {
183 	return xp_raw_get_data(pool, addr);
184 }
185 
xsk_buff_dma_sync_for_cpu(struct xdp_buff * xdp,struct xsk_buff_pool * pool)186 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
187 {
188 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
189 
190 	if (!pool->dma_need_sync)
191 		return;
192 
193 	xp_dma_sync_for_cpu(xskb);
194 }
195 
xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)196 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
197 						    dma_addr_t dma,
198 						    size_t size)
199 {
200 	xp_dma_sync_for_device(pool, dma, size);
201 }
202 
203 #else
204 
xsk_tx_completed(struct xsk_buff_pool * pool,u32 nb_entries)205 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
206 {
207 }
208 
xsk_tx_peek_desc(struct xsk_buff_pool * pool,struct xdp_desc * desc)209 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
210 				    struct xdp_desc *desc)
211 {
212 	return false;
213 }
214 
xsk_tx_peek_release_desc_batch(struct xsk_buff_pool * pool,u32 max)215 static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
216 {
217 	return 0;
218 }
219 
xsk_tx_release(struct xsk_buff_pool * pool)220 static inline void xsk_tx_release(struct xsk_buff_pool *pool)
221 {
222 }
223 
224 static inline struct xsk_buff_pool *
xsk_get_pool_from_qid(struct net_device * dev,u16 queue_id)225 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
226 {
227 	return NULL;
228 }
229 
xsk_set_rx_need_wakeup(struct xsk_buff_pool * pool)230 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
231 {
232 }
233 
xsk_set_tx_need_wakeup(struct xsk_buff_pool * pool)234 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
235 {
236 }
237 
xsk_clear_rx_need_wakeup(struct xsk_buff_pool * pool)238 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
239 {
240 }
241 
xsk_clear_tx_need_wakeup(struct xsk_buff_pool * pool)242 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
243 {
244 }
245 
xsk_uses_need_wakeup(struct xsk_buff_pool * pool)246 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
247 {
248 	return false;
249 }
250 
xsk_pool_get_headroom(struct xsk_buff_pool * pool)251 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
252 {
253 	return 0;
254 }
255 
xsk_pool_get_chunk_size(struct xsk_buff_pool * pool)256 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
257 {
258 	return 0;
259 }
260 
xsk_pool_get_rx_frame_size(struct xsk_buff_pool * pool)261 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
262 {
263 	return 0;
264 }
265 
xsk_pool_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)266 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
267 					 struct xdp_rxq_info *rxq)
268 {
269 }
270 
xsk_pool_get_napi_id(struct xsk_buff_pool * pool)271 static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
272 {
273 	return 0;
274 }
275 
xsk_pool_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)276 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
277 				      unsigned long attrs)
278 {
279 }
280 
xsk_pool_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs)281 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
282 				   struct device *dev, unsigned long attrs)
283 {
284 	return 0;
285 }
286 
xsk_buff_xdp_get_dma(struct xdp_buff * xdp)287 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
288 {
289 	return 0;
290 }
291 
xsk_buff_xdp_get_frame_dma(struct xdp_buff * xdp)292 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
293 {
294 	return 0;
295 }
296 
xsk_buff_alloc(struct xsk_buff_pool * pool)297 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
298 {
299 	return NULL;
300 }
301 
xsk_is_eop_desc(struct xdp_desc * desc)302 static inline bool xsk_is_eop_desc(struct xdp_desc *desc)
303 {
304 	return false;
305 }
306 
xsk_buff_alloc_batch(struct xsk_buff_pool * pool,struct xdp_buff ** xdp,u32 max)307 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
308 {
309 	return 0;
310 }
311 
xsk_buff_can_alloc(struct xsk_buff_pool * pool,u32 count)312 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
313 {
314 	return false;
315 }
316 
xsk_buff_free(struct xdp_buff * xdp)317 static inline void xsk_buff_free(struct xdp_buff *xdp)
318 {
319 }
320 
xsk_buff_add_frag(struct xdp_buff * xdp)321 static inline void xsk_buff_add_frag(struct xdp_buff *xdp)
322 {
323 }
324 
xsk_buff_get_frag(struct xdp_buff * first)325 static inline struct xdp_buff *xsk_buff_get_frag(struct xdp_buff *first)
326 {
327 	return NULL;
328 }
329 
xsk_buff_del_tail(struct xdp_buff * tail)330 static inline void xsk_buff_del_tail(struct xdp_buff *tail)
331 {
332 }
333 
xsk_buff_get_tail(struct xdp_buff * first)334 static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
335 {
336 	return NULL;
337 }
338 
xsk_buff_set_size(struct xdp_buff * xdp,u32 size)339 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
340 {
341 }
342 
xsk_buff_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)343 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
344 					      u64 addr)
345 {
346 	return 0;
347 }
348 
xsk_buff_raw_get_data(struct xsk_buff_pool * pool,u64 addr)349 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
350 {
351 	return NULL;
352 }
353 
xsk_buff_dma_sync_for_cpu(struct xdp_buff * xdp,struct xsk_buff_pool * pool)354 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
355 {
356 }
357 
xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)358 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
359 						    dma_addr_t dma,
360 						    size_t size)
361 {
362 }
363 
364 #endif /* CONFIG_XDP_SOCKETS */
365 
366 #endif /* _LINUX_XDP_SOCK_DRV_H */
367