• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
xsk_pool_get_headroom(struct xsk_buff_pool * pool)26 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
27 {
28 	return XDP_PACKET_HEADROOM + pool->headroom;
29 }
30 
xsk_pool_get_chunk_size(struct xsk_buff_pool * pool)31 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
32 {
33 	return pool->chunk_size;
34 }
35 
xsk_pool_get_rx_frame_size(struct xsk_buff_pool * pool)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 
xsk_pool_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)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 
xsk_pool_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)47 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
48 				      unsigned long attrs)
49 {
50 	xp_dma_unmap(pool, attrs);
51 }
52 
xsk_pool_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs)53 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
54 				   struct device *dev, unsigned long attrs)
55 {
56 	struct xdp_umem *umem = pool->umem;
57 
58 	return xp_dma_map(pool, dev, attrs, umem->pgs, umem->npgs);
59 }
60 
xsk_buff_xdp_get_dma(struct xdp_buff * xdp)61 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
62 {
63 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
64 
65 	return xp_get_dma(xskb);
66 }
67 
xsk_buff_xdp_get_frame_dma(struct xdp_buff * xdp)68 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
69 {
70 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
71 
72 	return xp_get_frame_dma(xskb);
73 }
74 
xsk_buff_alloc(struct xsk_buff_pool * pool)75 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
76 {
77 	return xp_alloc(pool);
78 }
79 
xsk_buff_can_alloc(struct xsk_buff_pool * pool,u32 count)80 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
81 {
82 	return xp_can_alloc(pool, count);
83 }
84 
xsk_buff_free(struct xdp_buff * xdp)85 static inline void xsk_buff_free(struct xdp_buff *xdp)
86 {
87 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
88 
89 	xp_free(xskb);
90 }
91 
xsk_buff_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)92 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
93 					      u64 addr)
94 {
95 	return xp_raw_get_dma(pool, addr);
96 }
97 
xsk_buff_raw_get_data(struct xsk_buff_pool * pool,u64 addr)98 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
99 {
100 	return xp_raw_get_data(pool, addr);
101 }
102 
xsk_buff_dma_sync_for_cpu(struct xdp_buff * xdp,struct xsk_buff_pool * pool)103 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
104 {
105 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
106 
107 	if (!pool->dma_need_sync)
108 		return;
109 
110 	xp_dma_sync_for_cpu(xskb);
111 }
112 
xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)113 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
114 						    dma_addr_t dma,
115 						    size_t size)
116 {
117 	xp_dma_sync_for_device(pool, dma, size);
118 }
119 
120 #else
121 
xsk_tx_completed(struct xsk_buff_pool * pool,u32 nb_entries)122 static inline void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
123 {
124 }
125 
xsk_tx_peek_desc(struct xsk_buff_pool * pool,struct xdp_desc * desc)126 static inline bool xsk_tx_peek_desc(struct xsk_buff_pool *pool,
127 				    struct xdp_desc *desc)
128 {
129 	return false;
130 }
131 
xsk_tx_peek_release_desc_batch(struct xsk_buff_pool * pool,u32 max)132 static inline u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max)
133 {
134 	return 0;
135 }
136 
xsk_tx_release(struct xsk_buff_pool * pool)137 static inline void xsk_tx_release(struct xsk_buff_pool *pool)
138 {
139 }
140 
141 static inline struct xsk_buff_pool *
xsk_get_pool_from_qid(struct net_device * dev,u16 queue_id)142 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
143 {
144 	return NULL;
145 }
146 
xsk_set_rx_need_wakeup(struct xsk_buff_pool * pool)147 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
148 {
149 }
150 
xsk_set_tx_need_wakeup(struct xsk_buff_pool * pool)151 static inline void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
152 {
153 }
154 
xsk_clear_rx_need_wakeup(struct xsk_buff_pool * pool)155 static inline void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
156 {
157 }
158 
xsk_clear_tx_need_wakeup(struct xsk_buff_pool * pool)159 static inline void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
160 {
161 }
162 
xsk_uses_need_wakeup(struct xsk_buff_pool * pool)163 static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
164 {
165 	return false;
166 }
167 
xsk_pool_get_headroom(struct xsk_buff_pool * pool)168 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
169 {
170 	return 0;
171 }
172 
xsk_pool_get_chunk_size(struct xsk_buff_pool * pool)173 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
174 {
175 	return 0;
176 }
177 
xsk_pool_get_rx_frame_size(struct xsk_buff_pool * pool)178 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
179 {
180 	return 0;
181 }
182 
xsk_pool_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)183 static inline void xsk_pool_set_rxq_info(struct xsk_buff_pool *pool,
184 					 struct xdp_rxq_info *rxq)
185 {
186 }
187 
xsk_pool_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)188 static inline void xsk_pool_dma_unmap(struct xsk_buff_pool *pool,
189 				      unsigned long attrs)
190 {
191 }
192 
xsk_pool_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs)193 static inline int xsk_pool_dma_map(struct xsk_buff_pool *pool,
194 				   struct device *dev, unsigned long attrs)
195 {
196 	return 0;
197 }
198 
xsk_buff_xdp_get_dma(struct xdp_buff * xdp)199 static inline dma_addr_t xsk_buff_xdp_get_dma(struct xdp_buff *xdp)
200 {
201 	return 0;
202 }
203 
xsk_buff_xdp_get_frame_dma(struct xdp_buff * xdp)204 static inline dma_addr_t xsk_buff_xdp_get_frame_dma(struct xdp_buff *xdp)
205 {
206 	return 0;
207 }
208 
xsk_buff_alloc(struct xsk_buff_pool * pool)209 static inline struct xdp_buff *xsk_buff_alloc(struct xsk_buff_pool *pool)
210 {
211 	return NULL;
212 }
213 
xsk_buff_can_alloc(struct xsk_buff_pool * pool,u32 count)214 static inline bool xsk_buff_can_alloc(struct xsk_buff_pool *pool, u32 count)
215 {
216 	return false;
217 }
218 
xsk_buff_free(struct xdp_buff * xdp)219 static inline void xsk_buff_free(struct xdp_buff *xdp)
220 {
221 }
222 
xsk_buff_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)223 static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
224 					      u64 addr)
225 {
226 	return 0;
227 }
228 
xsk_buff_raw_get_data(struct xsk_buff_pool * pool,u64 addr)229 static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
230 {
231 	return NULL;
232 }
233 
xsk_buff_dma_sync_for_cpu(struct xdp_buff * xdp,struct xsk_buff_pool * pool)234 static inline void xsk_buff_dma_sync_for_cpu(struct xdp_buff *xdp, struct xsk_buff_pool *pool)
235 {
236 }
237 
xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)238 static inline void xsk_buff_raw_dma_sync_for_device(struct xsk_buff_pool *pool,
239 						    dma_addr_t dma,
240 						    size_t size)
241 {
242 }
243 
244 #endif /* CONFIG_XDP_SOCKETS */
245 
246 #endif /* _LINUX_XDP_SOCK_DRV_H */
247