xref: /linux/include/net/xdp_sock_drv.h (revision 6ecf206d602fafd077811b6033c183deb0c0a9c8)
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 #ifdef CONFIG_XDP_SOCKETS
13 
14 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
15 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
16 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
17 void xsk_tx_release(struct xsk_buff_pool *pool);
18 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
19 					    u16 queue_id);
20 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
21 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
22 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
23 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
24 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
25 
26 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
27 {
28 	return XDP_PACKET_HEADROOM + pool->headroom;
29 }
30 
31 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
32 {
33 	return pool->chunk_size;
34 }
35 
36 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
37 {
38 	return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
39 }
40 
41 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
42 					 struct xdp_rxq_info *rxq)
43 {
44 	xp_set_rxq_info(pool, rxq);
45 }
46 
47 static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
48 {
49 #ifdef CONFIG_NET_RX_BUSY_POLL
50 	return pool->heads[0].xdp.rxq->napi_id;
51 #else
52 	return 0;
53 #endif
54 }
55 
56 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
57 				      unsigned long attrs)
58 {
59 	xp_dma_unmap(pool, attrs);
60 }
61 
62 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
63 				   struct device *dev, unsigned long attrs)
64 {
65 	struct xdp_umem *umem = pool->umem;
66 
67 	return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
68 }
69 
70 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
71 {
72 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
73 
74 	return xp_get_dma(xskb);
75 }
76 
77 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
78 {
79 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
80 
81 	return xp_get_frame_dma(xskb);
82 }
83 
84 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
85 {
86 	return xp_alloc(pool);
87 }
88 
89 /* Returns as many entries as possible up to max. 0 <= N <= max. */
90 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
91 {
92 	return xp_alloc_batch(pool, xdp, max);
93 }
94 
95 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
96 {
97 	return xp_can_alloc(pool, count);
98 }
99 
100 static inline void xsk_buff_free(struct xdp_buff *xdp)
101 {
102 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
103 
104 	xp_free(xskb);
105 }
106 
107 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
108 {
109 	xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM;
110 	xdp->data_meta = xdp->data;
111 	xdp->data_end = xdp->data + size;
112 }
113 
114 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
115 					      u64 addr)
116 {
117 	return xp_raw_get_dma(pool, addr);
118 }
119 
120 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
121 {
122 	return xp_raw_get_data(pool, addr);
123 }
124 
125 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
126 {
127 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
128 
129 	if (!pool->dma_need_sync)
130 		return;
131 
132 	xp_dma_sync_for_cpu(xskb);
133 }
134 
135 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
136 						    dma_addr_t dma,
137 						    size_t size)
138 {
139 	xp_dma_sync_for_device(pool, dma, size);
140 }
141 
142 #else
143 
144 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
145 {
146 }
147 
148 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
149 				    struct xdp_desc *desc)
150 {
151 	return false;
152 }
153 
154 static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
155 {
156 	return 0;
157 }
158 
159 static inline void xsk_tx_release(struct xsk_buff_pool *pool)
160 {
161 }
162 
163 static inline struct xsk_buff_pool *
164 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
165 {
166 	return NULL;
167 }
168 
169 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
170 {
171 }
172 
173 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
174 {
175 }
176 
177 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
178 {
179 }
180 
181 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
182 {
183 }
184 
185 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
186 {
187 	return false;
188 }
189 
190 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
191 {
192 	return 0;
193 }
194 
195 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
196 {
197 	return 0;
198 }
199 
200 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
201 {
202 	return 0;
203 }
204 
205 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
206 					 struct xdp_rxq_info *rxq)
207 {
208 }
209 
210 static inline unsigned int xsk_pool_get_napi_id(struct xsk_buff_pool *pool)
211 {
212 	return 0;
213 }
214 
215 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
216 				      unsigned long attrs)
217 {
218 }
219 
220 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
221 				   struct device *dev, unsigned long attrs)
222 {
223 	return 0;
224 }
225 
226 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
227 {
228 	return 0;
229 }
230 
231 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
232 {
233 	return 0;
234 }
235 
236 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
237 {
238 	return NULL;
239 }
240 
241 static inline u32 xsk_buff_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
242 {
243 	return 0;
244 }
245 
246 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
247 {
248 	return false;
249 }
250 
251 static inline void xsk_buff_free(struct xdp_buff *xdp)
252 {
253 }
254 
255 static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size)
256 {
257 }
258 
259 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
260 					      u64 addr)
261 {
262 	return 0;
263 }
264 
265 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
266 {
267 	return NULL;
268 }
269 
270 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
271 {
272 }
273 
274 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
275 						    dma_addr_t dma,
276 						    size_t size)
277 {
278 }
279 
280 #endif /* CONFIG_XDP_SOCKETS */
281 
282 #endif /* _LINUX_XDP_SOCK_DRV_H */
283