xref: /linux-6.15/include/net/xsk_buff_pool.h (revision 10a5e009)
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 <net/xdp.h>
11 
12 struct xsk_buff_pool;
13 struct xdp_rxq_info;
14 struct xsk_queue;
15 struct xdp_desc;
16 struct xdp_umem;
17 struct xdp_sock;
18 struct device;
19 struct page;
20 
21 struct xdp_buff_xsk {
22 	struct xdp_buff xdp;
23 	dma_addr_t dma;
24 	dma_addr_t frame_dma;
25 	struct xsk_buff_pool *pool;
26 	u64 orig_addr;
27 	struct list_head free_list_node;
28 };
29 
30 struct xsk_dma_map {
31 	dma_addr_t *dma_pages;
32 	struct device *dev;
33 	struct net_device *netdev;
34 	refcount_t users;
35 	struct list_head list; /* Protected by the RTNL_LOCK */
36 	u32 dma_pages_cnt;
37 	bool dma_need_sync;
38 };
39 
40 struct xsk_buff_pool {
41 	/* Members only used in the control path first. */
42 	struct device *dev;
43 	struct net_device *netdev;
44 	struct list_head xsk_tx_list;
45 	/* Protects modifications to the xsk_tx_list */
46 	spinlock_t xsk_tx_list_lock;
47 	refcount_t users;
48 	struct xdp_umem *umem;
49 	struct work_struct work;
50 	struct list_head free_list;
51 	u32 heads_cnt;
52 	u16 queue_id;
53 
54 	/* Data path members as close to free_heads at the end as possible. */
55 	struct xsk_queue *fq ____cacheline_aligned_in_smp;
56 	struct xsk_queue *cq;
57 	/* For performance reasons, each buff pool has its own array of dma_pages
58 	 * even when they are identical.
59 	 */
60 	dma_addr_t *dma_pages;
61 	struct xdp_buff_xsk *heads;
62 	u64 chunk_mask;
63 	u64 addrs_cnt;
64 	u32 free_list_cnt;
65 	u32 dma_pages_cnt;
66 	u32 free_heads_cnt;
67 	u32 headroom;
68 	u32 chunk_size;
69 	u32 frame_len;
70 	u8 cached_need_wakeup;
71 	bool uses_need_wakeup;
72 	bool dma_need_sync;
73 	bool unaligned;
74 	void *addrs;
75 	/* Mutual exclusion of the completion ring in the SKB mode. Two cases to protect:
76 	 * NAPI TX thread and sendmsg error paths in the SKB destructor callback and when
77 	 * sockets share a single cq when the same netdev and queue id is shared.
78 	 */
79 	spinlock_t cq_lock;
80 	struct xdp_buff_xsk *free_heads[];
81 };
82 
83 /* AF_XDP core. */
84 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
85 						struct xdp_umem *umem);
86 int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev,
87 		  u16 queue_id, u16 flags);
88 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
89 			 struct net_device *dev, u16 queue_id);
90 void xp_destroy(struct xsk_buff_pool *pool);
91 void xp_release(struct xdp_buff_xsk *xskb);
92 void xp_get_pool(struct xsk_buff_pool *pool);
93 bool xp_put_pool(struct xsk_buff_pool *pool);
94 void xp_clear_dev(struct xsk_buff_pool *pool);
95 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
96 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
97 
98 /* AF_XDP, and XDP core. */
99 void xp_free(struct xdp_buff_xsk *xskb);
100 
101 /* AF_XDP ZC drivers, via xdp_sock_buff.h */
102 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq);
103 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
104 	       unsigned long attrs, struct page **pages, u32 nr_pages);
105 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs);
106 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool);
107 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count);
108 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr);
109 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr);
110 static inline dma_addr_t xp_get_dma(struct xdp_buff_xsk *xskb)
111 {
112 	return xskb->dma;
113 }
114 
115 static inline dma_addr_t xp_get_frame_dma(struct xdp_buff_xsk *xskb)
116 {
117 	return xskb->frame_dma;
118 }
119 
120 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb);
121 static inline void xp_dma_sync_for_cpu(struct xdp_buff_xsk *xskb)
122 {
123 	xp_dma_sync_for_cpu_slow(xskb);
124 }
125 
126 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
127 				 size_t size);
128 static inline void xp_dma_sync_for_device(struct xsk_buff_pool *pool,
129 					  dma_addr_t dma, size_t size)
130 {
131 	if (!pool->dma_need_sync)
132 		return;
133 
134 	xp_dma_sync_for_device_slow(pool, dma, size);
135 }
136 
137 /* Masks for xdp_umem_page flags.
138  * The low 12-bits of the addr will be 0 since this is the page address, so we
139  * can use them for flags.
140  */
141 #define XSK_NEXT_PG_CONTIG_SHIFT 0
142 #define XSK_NEXT_PG_CONTIG_MASK BIT_ULL(XSK_NEXT_PG_CONTIG_SHIFT)
143 
144 static inline bool xp_desc_crosses_non_contig_pg(struct xsk_buff_pool *pool,
145 						 u64 addr, u32 len)
146 {
147 	bool cross_pg = (addr & (PAGE_SIZE - 1)) + len > PAGE_SIZE;
148 
149 	if (likely(!cross_pg))
150 		return false;
151 
152 	if (pool->dma_pages_cnt) {
153 		return !(pool->dma_pages[addr >> PAGE_SHIFT] &
154 			 XSK_NEXT_PG_CONTIG_MASK);
155 	}
156 
157 	/* skb path */
158 	return addr + len > pool->addrs_cnt;
159 }
160 
161 static inline u64 xp_aligned_extract_addr(struct xsk_buff_pool *pool, u64 addr)
162 {
163 	return addr & pool->chunk_mask;
164 }
165 
166 static inline u64 xp_unaligned_extract_addr(u64 addr)
167 {
168 	return addr & XSK_UNALIGNED_BUF_ADDR_MASK;
169 }
170 
171 static inline u64 xp_unaligned_extract_offset(u64 addr)
172 {
173 	return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT;
174 }
175 
176 static inline u64 xp_unaligned_add_offset_to_addr(u64 addr)
177 {
178 	return xp_unaligned_extract_addr(addr) +
179 		xp_unaligned_extract_offset(addr);
180 }
181 
182 #endif /* XSK_BUFF_POOL_H_ */
183