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
7 #include <linux/if_xdp.h>
8 #include <linux/types.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/bpf.h>
11 #include <net/xdp.h>
12
13 struct xsk_buff_pool;
14 struct xdp_rxq_info;
15 struct xsk_cb_desc;
16 struct xsk_queue;
17 struct xdp_desc;
18 struct xdp_umem;
19 struct xdp_sock;
20 struct device;
21 struct page;
22
23 #define XSK_PRIV_MAX 24
24
25 struct xdp_buff_xsk {
26 struct xdp_buff xdp;
27 u8 cb[XSK_PRIV_MAX];
28 dma_addr_t dma;
29 dma_addr_t frame_dma;
30 struct xsk_buff_pool *pool;
31 struct list_head list_node;
32 } __aligned_largest;
33
34 #define XSK_CHECK_PRIV_TYPE(t) BUILD_BUG_ON(sizeof(t) > offsetofend(struct xdp_buff_xsk, cb))
35 #define XSK_TX_COMPL_FITS(t) BUILD_BUG_ON(sizeof(struct xsk_tx_metadata_compl) > sizeof(t))
36
37 struct 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;
44 };
45
46 struct xsk_buff_pool {
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;
56 /* Protects generic receive in shared and non-shared umem mode. */
57 spinlock_t rx_lock;
58 struct list_head free_list;
59 struct list_head xskb_list;
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;
66 /* For performance reasons, each buff pool has its own array of dma_pages
67 * even when they are identical.
68 */
69 dma_addr_t *dma_pages;
70 struct xdp_buff_xsk *heads;
71 struct xdp_desc *tx_descs;
72 u64 chunk_mask;
73 u64 addrs_cnt;
74 u32 free_list_cnt;
75 u32 dma_pages_cnt;
76 u32 free_heads_cnt;
77 u32 headroom;
78 u32 chunk_size;
79 u32 chunk_shift;
80 u32 frame_len;
81 u32 tx_descs_nentries;
82 u32 reclaim_descs;
83 u32 tx_zc_pending_descs;
84 u32 xdp_zc_max_segs;
85 u8 tx_metadata_len; /* inherited from umem */
86 u8 cached_need_wakeup;
87 bool uses_need_wakeup;
88 bool unaligned;
89 bool tx_sw_csum;
90 void *addrs;
91 /* Mutual exclusion of the completion ring in the SKB mode.
92 * Protect: NAPI TX thread and sendmsg error paths in the SKB
93 * destructor callback.
94 */
95 spinlock_t cq_prod_lock;
96 struct xdp_buff_xsk *free_heads[];
97 };
98
99 /* Masks for xdp_umem_page flags.
100 * The low 12-bits of the addr will be 0 since this is the page address, so we
101 * can use them for flags.
102 */
103 #define XSK_NEXT_PG_CONTIG_SHIFT 0
104 #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
105
106 /* AF_XDP core. */
107 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
108 struct xdp_umem *umem,
109 u32 max_segs);
110 int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
111 u16 queue_id, u16 flags);
112 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
113 struct net_device *dev, u16 queue_id);
114 int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs,
115 u32 max_segs);
116 void xp_destroy(struct xsk_buff_pool *pool);
117 void xp_get_pool(struct xsk_buff_pool *pool);
118 bool xp_put_pool(struct xsk_buff_pool *pool);
119 void xp_clear_dev(struct xsk_buff_pool *pool);
120 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
121 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
122
123 /* AF_XDP, and XDP core. */
124 void xp_free(struct xdp_buff_xsk *xskb);
125
xp_init_xskb_addr(struct xdp_buff_xsk * xskb,struct xsk_buff_pool * pool,u64 addr)126 static inline void xp_init_xskb_addr(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
127 u64 addr)
128 {
129 xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom;
130 }
131
xp_init_xskb_dma(struct xdp_buff_xsk * xskb,struct xsk_buff_pool * pool,dma_addr_t * dma_pages,u64 addr)132 static inline void xp_init_xskb_dma(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
133 dma_addr_t *dma_pages, u64 addr)
134 {
135 xskb->frame_dma = (dma_pages[addr >> PAGE_SHIFT] & ~XSK_NEXT_PG_CONTIG_MASK) +
136 (addr & ~PAGE_MASK);
137 xskb->dma = xskb->frame_dma + pool->headroom + XDP_PACKET_HEADROOM;
138 }
139
140 /* AF_XDP ZC drivers, via xdp_sock_buff.h */
141 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
142 void xp_fill_cb(struct xsk_buff_pool *pool, struct xsk_cb_desc *desc);
143 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
144 unsigned long attrs, struct page **pages, u32 nr_pages);
145 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
146 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
147 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max);
148 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
149 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
150 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
151
152 struct xdp_desc_ctx {
153 dma_addr_t dma;
154 struct xsk_tx_metadata *meta;
155 };
156
157 struct xdp_desc_ctx xp_raw_get_ctx(const struct xsk_buff_pool *pool, u64 addr,
158 u32 options);
159
xp_get_dma(struct xdp_buff_xsk * xskb)160 static inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
161 {
162 return xskb->dma;
163 }
164
xp_get_frame_dma(struct xdp_buff_xsk * xskb)165 static inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
166 {
167 return xskb->frame_dma;
168 }
169
xp_dma_sync_for_cpu(struct xdp_buff_xsk * xskb)170 static inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
171 {
172 dma_sync_single_for_cpu(xskb->pool->dev, xskb->dma,
173 xskb->pool->frame_len,
174 DMA_BIDIRECTIONAL);
175 }
176
xp_dma_sync_for_device(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)177 static inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
178 dma_addr_t dma, size_t size)
179 {
180 dma_sync_single_for_device(pool->dev, dma, size, DMA_BIDIRECTIONAL);
181 }
182
xp_desc_crosses_non_contig_pg(struct xsk_buff_pool * pool,u64 addr,u32 len)183 static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
184 u64 addr, u32 len)
185 {
186 bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
187
188 if (likely(!cross_pg))
189 return false;
190
191 return pool->dma_pages &&
192 !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);
193 }
194
xp_mb_desc(const struct xdp_desc * desc)195 static inline bool xp_mb_desc(const struct xdp_desc *desc)
196 {
197 return desc->options & XDP_PKT_CONTD;
198 }
199
xp_aligned_extract_addr(struct xsk_buff_pool * pool,u64 addr)200 static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
201 {
202 return addr & pool->chunk_mask;
203 }
204
xp_unaligned_extract_addr(u64 addr)205 static inline u64 xp_unaligned_extract_addr(u64 addr)
206 {
207 return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
208 }
209
xp_unaligned_extract_offset(u64 addr)210 static inline u64 xp_unaligned_extract_offset(u64 addr)
211 {
212 return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
213 }
214
xp_unaligned_add_offset_to_addr(u64 addr)215 static inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
216 {
217 return xp_unaligned_extract_addr(addr) +
218 xp_unaligned_extract_offset(addr);
219 }
220
xp_aligned_extract_idx(struct xsk_buff_pool * pool,u64 addr)221 static inline u32 xp_aligned_extract_idx(struct xsk_buff_pool *pool, u64 addr)
222 {
223 return xp_aligned_extract_addr(pool, addr) >> pool->chunk_shift;
224 }
225
xp_release(struct xdp_buff_xsk * xskb)226 static inline void xp_release(struct xdp_buff_xsk *xskb)
227 {
228 if (xskb->pool->unaligned)
229 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
230 }
231
xp_get_handle(struct xdp_buff_xsk * xskb,struct xsk_buff_pool * pool)232 static inline u64 xp_get_handle(struct xdp_buff_xsk *xskb,
233 struct xsk_buff_pool *pool)
234 {
235 u64 orig_addr = xskb->xdp.data - pool->addrs;
236 u64 offset;
237
238 if (!pool->unaligned)
239 return orig_addr;
240
241 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
242 offset += pool->headroom;
243 orig_addr -= offset;
244 return orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
245 }
246
xp_tx_metadata_enabled(const struct xsk_buff_pool * pool)247 static inline bool xp_tx_metadata_enabled(const struct xsk_buff_pool *pool)
248 {
249 return pool->tx_metadata_len > 0;
250 }
251
252 #endif /* XSK_BUFF_POOL_H_ */
253