blob: b6f5ebae43a1cb7ff5b3bb44c0e35d628fd688a3 [file] [log] [blame]
Björn Töpeldac091492018-05-18 14:00:21 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/* AF_XDP internal functions
Björn Töpelc0c77d82018-05-02 13:01:23 +02003 * Copyright(c) 2018 Intel Corporation.
Björn Töpelc0c77d82018-05-02 13:01:23 +02004 */
5
6#ifndef _LINUX_XDP_SOCK_H
7#define _LINUX_XDP_SOCK_H
8
Björn Töpele61e62b92018-06-04 14:05:51 +02009#include <linux/workqueue.h>
10#include <linux/if_xdp.h>
Björn Töpelc0c77d82018-05-02 13:01:23 +020011#include <linux/mutex.h>
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020012#include <linux/spinlock.h>
Björn Töpele61e62b92018-06-04 14:05:51 +020013#include <linux/mm.h>
Björn Töpelc0c77d82018-05-02 13:01:23 +020014#include <net/sock.h>
15
Björn Töpelb9b6b682018-05-02 13:01:25 +020016struct net_device;
17struct xsk_queue;
Björn Töpele61e62b92018-06-04 14:05:51 +020018
Björn Töpel8aef7342018-06-04 14:05:52 +020019struct xdp_umem_page {
20 void *addr;
Björn Töpel173d3ad2018-06-04 14:05:55 +020021 dma_addr_t dma;
Björn Töpel8aef7342018-06-04 14:05:52 +020022};
23
Jakub Kicinskif5bd9132018-09-07 10:18:46 +020024struct xdp_umem_fq_reuse {
25 u32 nentries;
26 u32 length;
27 u64 handles[];
28};
29
Björn Töpele61e62b92018-06-04 14:05:51 +020030struct xdp_umem {
31 struct xsk_queue *fq;
32 struct xsk_queue *cq;
Björn Töpel8aef7342018-06-04 14:05:52 +020033 struct xdp_umem_page *pages;
Magnus Karlsson93ee30f2018-08-31 13:40:02 +020034 u64 chunk_mask;
35 u64 size;
Björn Töpele61e62b92018-06-04 14:05:51 +020036 u32 headroom;
37 u32 chunk_size_nohr;
38 struct user_struct *user;
Björn Töpele61e62b92018-06-04 14:05:51 +020039 unsigned long address;
40 refcount_t users;
41 struct work_struct work;
Björn Töpel8aef7342018-06-04 14:05:52 +020042 struct page **pgs;
Björn Töpele61e62b92018-06-04 14:05:51 +020043 u32 npgs;
Björn Töpel50e74c02019-01-24 19:59:38 +010044 int id;
Björn Töpel173d3ad2018-06-04 14:05:55 +020045 struct net_device *dev;
Jakub Kicinskif5bd9132018-09-07 10:18:46 +020046 struct xdp_umem_fq_reuse *fq_reuse;
Björn Töpel173d3ad2018-06-04 14:05:55 +020047 u16 queue_id;
48 bool zc;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020049 spinlock_t xsk_list_lock;
50 struct list_head xsk_list;
Björn Töpele61e62b92018-06-04 14:05:51 +020051};
Björn Töpelc0c77d82018-05-02 13:01:23 +020052
53struct xdp_sock {
54 /* struct sock must be the first member of struct xdp_sock */
55 struct sock sk;
Björn Töpelb9b6b682018-05-02 13:01:25 +020056 struct xsk_queue *rx;
57 struct net_device *dev;
Björn Töpelc0c77d82018-05-02 13:01:23 +020058 struct xdp_umem *umem;
Björn Töpelfbfc504a2018-05-02 13:01:28 +020059 struct list_head flush_node;
Magnus Karlsson965a9902018-05-02 13:01:26 +020060 u16 queue_id;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020061 bool zc;
Björn Töpelc0c77d82018-05-02 13:01:23 +020062 /* Protects multiple processes in the control path */
63 struct mutex mutex;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -070064 struct xsk_queue *tx ____cacheline_aligned_in_smp;
65 struct list_head list;
Magnus Karlssona9744f72018-06-29 09:48:20 +020066 /* Mutual exclusion of NAPI TX thread and sendmsg error paths
67 * in the SKB destructor callback.
68 */
69 spinlock_t tx_completion_lock;
Björn Töpelc4971762018-05-02 13:01:27 +020070 u64 rx_dropped;
Björn Töpelc0c77d82018-05-02 13:01:23 +020071};
72
Björn Töpelc4971762018-05-02 13:01:27 +020073struct xdp_buff;
74#ifdef CONFIG_XDP_SOCKETS
75int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
76int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
77void xsk_flush(struct xdp_sock *xs);
Björn Töpelfbfc504a2018-05-02 13:01:28 +020078bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020079/* Used from netdev driver */
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +030080bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt);
Björn Töpel173d3ad2018-06-04 14:05:55 +020081u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr);
82void xsk_umem_discard_addr(struct xdp_umem *umem);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020083void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries);
84bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len);
85void xsk_umem_consume_tx_done(struct xdp_umem *umem);
Jakub Kicinskif5bd9132018-09-07 10:18:46 +020086struct xdp_umem_fq_reuse *xsk_reuseq_prepare(u32 nentries);
87struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,
88 struct xdp_umem_fq_reuse *newq);
89void xsk_reuseq_free(struct xdp_umem_fq_reuse *rq);
Jakub Kicinski1661d342018-10-01 14:51:36 +020090struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, u16 queue_id);
Björn Töpel90254032018-08-28 14:44:27 +020091
92static inline char *xdp_umem_get_data(struct xdp_umem *umem, u64 addr)
93{
94 return umem->pages[addr >> PAGE_SHIFT].addr + (addr & (PAGE_SIZE - 1));
95}
96
97static inline dma_addr_t xdp_umem_get_dma(struct xdp_umem *umem, u64 addr)
98{
99 return umem->pages[addr >> PAGE_SHIFT].dma + (addr & (PAGE_SIZE - 1));
100}
Jakub Kicinskif5bd9132018-09-07 10:18:46 +0200101
102/* Reuse-queue aware version of FILL queue helpers */
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +0300103static inline bool xsk_umem_has_addrs_rq(struct xdp_umem *umem, u32 cnt)
104{
105 struct xdp_umem_fq_reuse *rq = umem->fq_reuse;
106
107 if (rq->length >= cnt)
108 return true;
109
110 return xsk_umem_has_addrs(umem, cnt - rq->length);
111}
112
Jakub Kicinskif5bd9132018-09-07 10:18:46 +0200113static inline u64 *xsk_umem_peek_addr_rq(struct xdp_umem *umem, u64 *addr)
114{
115 struct xdp_umem_fq_reuse *rq = umem->fq_reuse;
116
117 if (!rq->length)
118 return xsk_umem_peek_addr(umem, addr);
119
120 *addr = rq->handles[rq->length - 1];
121 return addr;
122}
123
124static inline void xsk_umem_discard_addr_rq(struct xdp_umem *umem)
125{
126 struct xdp_umem_fq_reuse *rq = umem->fq_reuse;
127
128 if (!rq->length)
129 xsk_umem_discard_addr(umem);
130 else
131 rq->length--;
132}
133
134static inline void xsk_umem_fq_reuse(struct xdp_umem *umem, u64 addr)
135{
136 struct xdp_umem_fq_reuse *rq = umem->fq_reuse;
137
138 rq->handles[rq->length++] = addr;
139}
Björn Töpelc4971762018-05-02 13:01:27 +0200140#else
141static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
142{
143 return -ENOTSUPP;
144}
145
146static inline int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
147{
148 return -ENOTSUPP;
149}
150
151static inline void xsk_flush(struct xdp_sock *xs)
152{
153}
Björn Töpelfbfc504a2018-05-02 13:01:28 +0200154
155static inline bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
156{
157 return false;
158}
Björn Töpel90254032018-08-28 14:44:27 +0200159
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +0300160static inline bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt)
161{
162 return false;
163}
164
Björn Töpel90254032018-08-28 14:44:27 +0200165static inline u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
166{
167 return NULL;
168}
169
170static inline void xsk_umem_discard_addr(struct xdp_umem *umem)
171{
172}
173
174static inline void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
175{
176}
177
178static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma,
179 u32 *len)
180{
181 return false;
182}
183
184static inline void xsk_umem_consume_tx_done(struct xdp_umem *umem)
185{
186}
187
Jakub Kicinskif5bd9132018-09-07 10:18:46 +0200188static inline struct xdp_umem_fq_reuse *xsk_reuseq_prepare(u32 nentries)
189{
190 return NULL;
191}
192
193static inline struct xdp_umem_fq_reuse *xsk_reuseq_swap(
194 struct xdp_umem *umem,
195 struct xdp_umem_fq_reuse *newq)
196{
197 return NULL;
198}
199static inline void xsk_reuseq_free(struct xdp_umem_fq_reuse *rq)
200{
201}
202
Jakub Kicinski1661d342018-10-01 14:51:36 +0200203static inline struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev,
204 u16 queue_id)
205{
206 return NULL;
207}
208
Björn Töpel90254032018-08-28 14:44:27 +0200209static inline char *xdp_umem_get_data(struct xdp_umem *umem, u64 addr)
210{
211 return NULL;
212}
213
214static inline dma_addr_t xdp_umem_get_dma(struct xdp_umem *umem, u64 addr)
215{
216 return 0;
217}
Jakub Kicinskif5bd9132018-09-07 10:18:46 +0200218
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +0300219static inline bool xsk_umem_has_addrs_rq(struct xdp_umem *umem, u32 cnt)
220{
221 return false;
222}
223
Jakub Kicinskif5bd9132018-09-07 10:18:46 +0200224static inline u64 *xsk_umem_peek_addr_rq(struct xdp_umem *umem, u64 *addr)
225{
226 return NULL;
227}
228
229static inline void xsk_umem_discard_addr_rq(struct xdp_umem *umem)
230{
231}
232
233static inline void xsk_umem_fq_reuse(struct xdp_umem *umem, u64 addr)
234{
235}
236
Björn Töpelc4971762018-05-02 13:01:27 +0200237#endif /* CONFIG_XDP_SOCKETS */
238
Björn Töpelc0c77d82018-05-02 13:01:23 +0200239#endif /* _LINUX_XDP_SOCK_H */