1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2020 Intel Corporation. */
4 #ifndef XSK_BUFF_POOL_H_
5 #define XSK_BUFF_POOL_H_
7 #include <linux/if_xdp.h>
8 #include <linux/types.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/bpf.h>
26 struct xsk_buff_pool *pool;
28 struct list_head free_list_node;
32 dma_addr_t *dma_pages;
34 struct net_device *netdev;
36 struct list_head list; /* Protected by the RTNL_LOCK */
41 struct xsk_buff_pool {
42 /* Members only used in the control path first. */
44 struct net_device *netdev;
45 struct list_head xsk_tx_list;
46 /* Protects modifications to the xsk_tx_list */
47 spinlock_t xsk_tx_list_lock;
49 struct xdp_umem *umem;
50 struct work_struct work;
51 struct list_head free_list;
55 /* Data path members as close to free_heads at the end as possible. */
56 struct xsk_queue *fq ____cacheline_aligned_in_smp;
58 /* For performance reasons, each buff pool has its own array of dma_pages
59 * even when they are identical.
61 dma_addr_t *dma_pages;
62 struct xdp_buff_xsk *heads;
63 struct xdp_desc *tx_descs;
73 u8 cached_need_wakeup;
74 bool uses_need_wakeup;
78 /* Mutual exclusion of the completion ring in the SKB mode. Two cases to protect:
79 * NAPI TX thread and sendmsg error paths in the SKB destructor callback and when
80 * sockets share a single cq when the same netdev and queue id is shared.
83 struct xdp_buff_xsk *free_heads[];
86 /* Masks for xdp_umem_page flags.
87 * The low 12-bits of the addr will be 0 since this is the page address, so we
88 * can use them for flags.
90 #define XSK_NEXT_PG_CONTIG_SHIFT 0
91 #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
94 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
95 struct xdp_umem *umem);
96 int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
97 u16 queue_id, u16 flags);
98 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
99 struct net_device *dev, u16 queue_id);
100 int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs);
101 void xp_destroy(struct xsk_buff_pool *pool);
102 void xp_get_pool(struct xsk_buff_pool *pool);
103 bool xp_put_pool(struct xsk_buff_pool *pool);
104 void xp_clear_dev(struct xsk_buff_pool *pool);
105 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
106 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
108 /* AF_XDP, and XDP core. */
109 void xp_free(struct xdp_buff_xsk *xskb);
111 static inline void xp_init_xskb_addr(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
114 xskb->orig_addr = addr;
115 xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom;
118 static inline void xp_init_xskb_dma(struct xdp_buff_xsk *xskb, struct xsk_buff_pool *pool,
119 dma_addr_t *dma_pages, u64 addr)
121 xskb->frame_dma = (dma_pages[addr >> PAGE_SHIFT] & ~XSK_NEXT_PG_CONTIG_MASK) +
123 xskb->dma = xskb->frame_dma + pool->headroom + XDP_PACKET_HEADROOM;
126 /* AF_XDP ZC drivers, via xdp_sock_buff.h */
127 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
128 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
129 unsigned long attrs, struct page **pages, u32 nr_pages);
130 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
131 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
132 u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max);
133 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
134 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
135 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
136 static inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
141 static inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
143 return xskb->frame_dma;
146 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb);
147 static inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
149 xp_dma_sync_for_cpu_slow(xskb);
152 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
154 static inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
155 dma_addr_t dma, size_t size)
157 if (!pool->dma_need_sync)
160 xp_dma_sync_for_device_slow(pool, dma, size);
163 /* Masks for xdp_umem_page flags.
164 * The low 12-bits of the addr will be 0 since this is the page address, so we
165 * can use them for flags.
167 #define XSK_NEXT_PG_CONTIG_SHIFT 0
168 #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
170 static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
173 bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
175 if (likely(!cross_pg))
178 if (pool->dma_pages_cnt) {
179 return !(pool->dma_pages[addr >> PAGE_SHIFT] &
180 XSK_NEXT_PG_CONTIG_MASK);
184 return addr + len > pool->addrs_cnt;
187 static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
189 return addr & pool->chunk_mask;
192 static inline u64 xp_unaligned_extract_addr(u64 addr)
194 return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
197 static inline u64 xp_unaligned_extract_offset(u64 addr)
199 return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
202 static inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
204 return xp_unaligned_extract_addr(addr) +
205 xp_unaligned_extract_offset(addr);
208 static inline u32 xp_aligned_extract_idx(struct xsk_buff_pool *pool, u64 addr)
210 return xp_aligned_extract_addr(pool, addr) >> pool->chunk_shift;
213 static inline void xp_release(struct xdp_buff_xsk *xskb)
215 if (xskb->pool->unaligned)
216 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
219 static inline u64 xp_get_handle(struct xdp_buff_xsk *xskb)
221 u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
223 offset += xskb->pool->headroom;
224 if (!xskb->pool->unaligned)
225 return xskb->orig_addr + offset;
226 return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
229 #endif /* XSK_BUFF_POOL_H_ */