xref: /linux/include/net/xdp_sock_drv.h (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 #define NETDEV_XDP_ACT_XSK	(NETDEV_XDP_ACT_BASIC |		\
16 				 NETDEV_XDP_ACT_REDIRECT |	\
17 				 NETDEV_XDP_ACT_XSK_ZEROCOPY)
18 
19 struct xsk_cb_desc {
20 	void *src;
21 	u8 off;
22 	u8 bytes;
23 };
24 
25 #ifdef CONFIG_XDP_SOCKETS
26 
27 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
28 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
29 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
30 void xsk_tx_release(struct xsk_buff_pool *pool);
31 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
32 					    u16 queue_id);
33 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
34 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
35 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
36 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
37 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
38 
39 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
40 {
41 	return XDP_PACKET_HEADROOM + pool->headroom;
42 }
43 
44 static inline u32 xsk_pool_get_tailroom(bool mbuf)
45 {
46 	return mbuf ? SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) : 0;
47 }
48 
49 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
50 {
51 	return pool->chunk_size;
52 }
53 
54 static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
55 {
56 	return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
57 }
58 
59 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
60 {
61 	u32 frame_size =  __xsk_pool_get_rx_frame_size(pool);
62 	struct xdp_umem *umem = pool->umem;
63 	bool mbuf;
64 
65 	/* Reserve tailroom only for zero-copy pools that opted into
66 	 * multi-buffer. The reserved area is used for skb_shared_info,
67 	 * matching the XDP core's xdp_data_hard_end() layout.
68 	 */
69 	mbuf = pool->dev && (umem->flags & XDP_UMEM_SG_FLAG);
70 	frame_size -= xsk_pool_get_tailroom(mbuf);
71 
72 	return ALIGN_DOWN(frame_size, 128);
73 }
74 
75 static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool)
76 {
77 	return pool->unaligned ? 0 : xsk_pool_get_chunk_size(pool);
78 }
79 
80 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
81 					 struct xdp_rxq_info *rxq)
82 {
83 	xp_set_rxq_info(pool, rxq);
84 }
85 
86 static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
87 				    struct xsk_cb_desc *desc)
88 {
89 	xp_fill_cb(pool, desc);
90 }
91 
92 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
93 				      unsigned long attrs)
94 {
95 	xp_dma_unmap(pool, attrs);
96 }
97 
98 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
99 				   struct device *dev, unsigned long attrs)
100 {
101 	struct xdp_umem *umem = pool->umem;
102 
103 	return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
104 }
105 
106 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
107 {
108 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
109 
110 	return xp_get_dma(xskb);
111 }
112 
113 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
114 {
115 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
116 
117 	return xp_get_frame_dma(xskb);
118 }
119 
120 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
121 {
122 	return xp_alloc(pool);
123 }
124 
125 static inline bool xsk_is_eop_desc(const struct xdp_desc *desc)
126 {
127 	return !xp_mb_desc(desc);
128 }
129 
130 /* Returns as many entries as possible up to max. 0 <= N <= max. */
131 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
132 {
133 	return xp_alloc_batch(pool, xdp, max);
134 }
135 
136 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
137 {
138 	return xp_can_alloc(pool, count);
139 }
140 
141 static inline void xsk_buff_free(struct xdp_buff *xdp)
142 {
143 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
144 	struct list_head *xskb_list = &xskb->pool->xskb_list;
145 	struct xdp_buff_xsk *pos, *tmp;
146 
147 	if (likely(!xdp_buff_has_frags(xdp)))
148 		goto out;
149 
150 	list_for_each_entry_safe(pos, tmp, xskb_list, list_node) {
151 		list_del_init(&pos->list_node);
152 		xp_free(pos);
153 	}
154 
155 	xdp_get_shared_info_from_buff(xdp)->nr_frags = 0;
156 	xdp_buff_clear_frags_flag(xdp);
157 out:
158 	xp_free(xskb);
159 }
160 
161 static inline bool xsk_buff_add_frag(struct xdp_buff *head,
162 				     struct xdp_buff *xdp)
163 {
164 	struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp);
165 	const void *data = xdp->data;
166 
167 	/* Duplicate fill ring entries map to the same xskb in aligned mode */
168 	if (unlikely(xdp == head || !list_empty(&frag->list_node)))
169 		return false;
170 
171 	if (!__xdp_buff_add_frag(head, virt_to_netmem(data),
172 				 offset_in_page(data), xdp->data_end - data,
173 				 xdp->frame_sz, false))
174 		return false;
175 
176 	list_add_tail(&frag->list_node, &frag->pool->xskb_list);
177 
178 	return true;
179 }
180 
181 static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
182 {
183 	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
184 	struct xdp_buff *ret = NULL;
185 	struct xdp_buff_xsk *frag;
186 
187 	frag = list_first_entry_or_null(&xskb->pool->xskb_list,
188 					struct xdp_buff_xsk, list_node);
189 	if (frag) {
190 		list_del_init(&frag->list_node);
191 		ret = &frag->xdp;
192 	}
193 
194 	return ret;
195 }
196 
197 static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
198 {
199 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
200 
201 	list_del_init(&xskb->list_node);
202 }
203 
204 static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
205 {
206 	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
207 	struct xdp_buff_xsk *frag;
208 
209 	frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
210 				list_node);
211 	return &frag->xdp;
212 }
213 
214 static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
215 {
216 	struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
217 	struct xdp_buff_xsk *frag;
218 
219 	frag = list_last_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk,
220 			       list_node);
221 	return &frag->xdp;
222 }
223 
224 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
225 {
226 	xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
227 	xdp->data_meta = xdp->data;
228 	xdp->data_end = xdp->data + size;
229 	/* Frags flag is cleared when the frags of a head are released */
230 	xdp->flags &= XDP_FLAGS_HAS_FRAGS;
231 }
232 
233 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
234 					      u64 addr)
235 {
236 	return xp_raw_get_dma(pool, addr);
237 }
238 
239 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
240 {
241 	return xp_raw_get_data(pool, addr);
242 }
243 
244 /**
245  * xsk_buff_raw_get_ctx - get &xdp_desc context
246  * @pool: XSk buff pool desc address belongs to
247  * @addr: desc address (from userspace)
248  * @options: desc options (from userspace)
249  *
250  * Wrapper for xp_raw_get_ctx() to be used in drivers, see its kdoc for
251  * details.
252  *
253  * Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata
254  * pointer, if it is present (initialized to %NULL otherwise).
255  */
256 static inline struct xdp_desc_ctx
257 xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr, u32 options)
258 {
259 	return xp_raw_get_ctx(pool, addr, options);
260 }
261 
262 #define XDP_TXMD_FLAGS_VALID ( \
263 		XDP_TXMD_FLAGS_TIMESTAMP | \
264 		XDP_TXMD_FLAGS_CHECKSUM | \
265 		XDP_TXMD_FLAGS_LAUNCH_TIME | \
266 	0)
267 
268 static inline bool
269 xsk_buff_valid_tx_metadata(const struct xsk_buff_pool *pool,
270 			   const struct xsk_tx_metadata *meta, u64 *flags)
271 {
272 	*flags = READ_ONCE(meta->flags);
273 	if (*flags & XDP_TXMD_FLAGS_LAUNCH_TIME)
274 		if (pool->tx_metadata_len <
275 		    offsetofend(struct xsk_tx_metadata, request.launch_time))
276 			return false;
277 	return !(*flags & ~XDP_TXMD_FLAGS_VALID);
278 }
279 
280 /**
281  *  xsk_tx_metadata_request - Evaluate AF_XDP TX metadata at submission
282  *  and call appropriate xsk_tx_metadata_ops operation.
283  *  @pool: pointer to AF_XDP buffer pool, used to validate the metadata
284  *  @pmeta: pointer to pointer to AF_XDP metadata area
285  *  @ops: pointer to struct xsk_tx_metadata_ops
286  *  @priv: pointer to driver-private area
287  *
288  *  This function should be called by the networking device when
289  *  it prepares AF_XDP egress packet.
290  */
291 static inline void
292 xsk_tx_metadata_request(const struct xsk_buff_pool *pool,
293 			struct xsk_tx_metadata **pmeta,
294 			const struct xsk_tx_metadata_ops *ops, void *priv)
295 {
296 	const struct xsk_tx_metadata *meta = *pmeta;
297 	u64 flags;
298 
299 	if (!meta)
300 		return;
301 
302 	if (unlikely(!xsk_buff_valid_tx_metadata(pool, meta, &flags))) {
303 		*pmeta = NULL;
304 		return; /* no way to signal the error to the user */
305 	}
306 
307 	if (ops->tmo_request_launch_time)
308 		if (flags & XDP_TXMD_FLAGS_LAUNCH_TIME)
309 			ops->tmo_request_launch_time(
310 				READ_ONCE(meta->request.launch_time), priv);
311 
312 	if (ops->tmo_request_timestamp)
313 		if (flags & XDP_TXMD_FLAGS_TIMESTAMP)
314 			ops->tmo_request_timestamp(priv);
315 
316 	if (ops->tmo_request_checksum)
317 		if (flags & XDP_TXMD_FLAGS_CHECKSUM)
318 			ops->tmo_request_checksum(
319 				READ_ONCE(meta->request.csum_start),
320 				READ_ONCE(meta->request.csum_offset), priv);
321 
322 	if (!(flags & XDP_TXMD_FLAGS_TIMESTAMP))
323 		*pmeta = NULL;
324 }
325 
326 static inline struct xsk_tx_metadata *
327 __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data,
328 			unsigned int options)
329 {
330 	if (!pool->tx_metadata_len || !(options & XDP_TX_METADATA))
331 		return NULL;
332 
333 	return data - pool->tx_metadata_len;
334 }
335 
336 static inline struct xsk_tx_metadata *
337 xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr, u32 options)
338 {
339 	return __xsk_buff_get_metadata(pool, xp_raw_get_data(pool, addr),
340 				       options);
341 }
342 
343 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
344 {
345 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
346 
347 	xp_dma_sync_for_cpu(xskb);
348 }
349 
350 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
351 						    dma_addr_t dma,
352 						    size_t size)
353 {
354 	xp_dma_sync_for_device(pool, dma, size);
355 }
356 
357 #else
358 
359 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
360 {
361 }
362 
363 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
364 				    struct xdp_desc *desc)
365 {
366 	return false;
367 }
368 
369 static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
370 {
371 	return 0;
372 }
373 
374 static inline void xsk_tx_release(struct xsk_buff_pool *pool)
375 {
376 }
377 
378 static inline struct xsk_buff_pool *
379 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
380 {
381 	return NULL;
382 }
383 
384 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
385 {
386 }
387 
388 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
389 {
390 }
391 
392 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
393 {
394 }
395 
396 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
397 {
398 }
399 
400 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
401 {
402 	return false;
403 }
404 
405 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
406 {
407 	return 0;
408 }
409 
410 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
411 {
412 	return 0;
413 }
414 
415 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
416 {
417 	return 0;
418 }
419 
420 static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool)
421 {
422 	return 0;
423 }
424 
425 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
426 					 struct xdp_rxq_info *rxq)
427 {
428 }
429 
430 static inline void xsk_pool_fill_cb(struct xsk_buff_pool *pool,
431 				    struct xsk_cb_desc *desc)
432 {
433 }
434 
435 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
436 				      unsigned long attrs)
437 {
438 }
439 
440 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
441 				   struct device *dev, unsigned long attrs)
442 {
443 	return 0;
444 }
445 
446 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
447 {
448 	return 0;
449 }
450 
451 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
452 {
453 	return 0;
454 }
455 
456 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
457 {
458 	return NULL;
459 }
460 
461 static inline bool xsk_is_eop_desc(const struct xdp_desc *desc)
462 {
463 	return false;
464 }
465 
466 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
467 {
468 	return 0;
469 }
470 
471 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
472 {
473 	return false;
474 }
475 
476 static inline void xsk_buff_free(struct xdp_buff *xdp)
477 {
478 }
479 
480 static inline bool xsk_buff_add_frag(struct xdp_buff *head,
481 				     struct xdp_buff *xdp)
482 {
483 	return false;
484 }
485 
486 static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)
487 {
488 	return NULL;
489 }
490 
491 static inline void xsk_buff_del_frag(struct xdp_buff *xdp)
492 {
493 }
494 
495 static inline struct xdp_buff *xsk_buff_get_head(struct xdp_buff *first)
496 {
497 	return NULL;
498 }
499 
500 static inline struct xdp_buff *xsk_buff_get_tail(struct xdp_buff *first)
501 {
502 	return NULL;
503 }
504 
505 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
506 {
507 }
508 
509 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
510 					      u64 addr)
511 {
512 	return 0;
513 }
514 
515 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
516 {
517 	return NULL;
518 }
519 
520 static inline struct xdp_desc_ctx
521 xsk_buff_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr, u32 options)
522 {
523 	return (struct xdp_desc_ctx){ };
524 }
525 
526 static inline bool
527 xsk_buff_valid_tx_metadata(const struct xsk_buff_pool *pool,
528 			   const struct xsk_tx_metadata *meta, u64 *flags)
529 {
530 	return false;
531 }
532 
533 static inline void
534 xsk_tx_metadata_request(const struct xsk_buff_pool *pool,
535 			struct xsk_tx_metadata **pmeta,
536 			const struct xsk_tx_metadata_ops *ops, void *priv)
537 {
538 }
539 
540 static inline struct xsk_tx_metadata *
541 __xsk_buff_get_metadata(const struct xsk_buff_pool *pool, void *data,
542 			unsigned int options)
543 {
544 	return NULL;
545 }
546 
547 static inline struct xsk_tx_metadata *
548 xsk_buff_get_metadata(struct xsk_buff_pool *pool, u64 addr, u32 options)
549 {
550 	return NULL;
551 }
552 
553 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp)
554 {
555 }
556 
557 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
558 						    dma_addr_t dma,
559 						    size_t size)
560 {
561 }
562 
563 #endif /* CONFIG_XDP_SOCKETS */
564 
565 #endif /* _LINUX_XDP_SOCK_DRV_H */
566