xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision 19ab58bfe3daf34d714195dcbd0f91e480924f7e)
1 /*
2  * Copyright (C) 2011-2012 Matteo Landi, Luigi Rizzo. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *   1. Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *   2. Redistributions in binary form must reproduce the above copyright
10  *      notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * $FreeBSD$
28  * $Id: netmap_kern.h 9795 2011-12-02 11:39:08Z luigi $
29  *
30  * The header contains the definitions of constants and function
31  * prototypes used only in kernelspace.
32  */
33 
34 #ifndef _NET_NETMAP_KERN_H_
35 #define _NET_NETMAP_KERN_H_
36 
37 #if defined(__FreeBSD__)
38 #define	NM_LOCK_T	struct mtx
39 #define	NM_SELINFO_T	struct selinfo
40 #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
41 #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
42 #elif defined (__linux__)
43 #define	NM_LOCK_T	spinlock_t
44 #define	NM_SELINFO_T	wait_queue_head_t
45 #define	MBUF_LEN(m)	((m)->len)
46 #define	NM_SEND_UP(ifp, m)	netif_rx(m)
47 #else
48 #error unsupported platform
49 #endif
50 
51 #ifdef MALLOC_DECLARE
52 MALLOC_DECLARE(M_NETMAP);
53 #endif
54 
55 #define ND(format, ...)
56 #define D(format, ...)						\
57 	do {							\
58 		struct timeval __xxts;				\
59 		microtime(&__xxts);				\
60 		printf("%03d.%06d %s [%d] " format "\n",	\
61 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
62 		__FUNCTION__, __LINE__, ##__VA_ARGS__);		\
63 	} while (0)
64 
65 struct netmap_adapter;
66 
67 /*
68  * private, kernel view of a ring.
69  *
70  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
71  * This is so that, on a reset, buffers owned by userspace are not
72  * modified by the kernel. In particular:
73  * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with
74  * 	the next empty buffer as known by the hardware (next_to_check or so).
75  * TX rings: hwcur + hwofs coincides with next_to_send
76  */
77 struct netmap_kring {
78 	struct netmap_ring *ring;
79 	u_int nr_hwcur;
80 	int nr_hwavail;
81 	u_int nr_kflags;	/* private driver flags */
82 #define NKR_PENDINTR   0x1     // Pending interrupt.
83 	u_int nkr_num_slots;
84 
85 	int	nkr_hwofs;	/* offset between NIC and netmap ring */
86 	struct netmap_adapter *na;
87 	NM_SELINFO_T si;	/* poll/select wait queue */
88 	NM_LOCK_T q_lock;	/* used if no device lock available */
89 } __attribute__((__aligned__(64)));
90 
91 /*
92  * This struct extends the 'struct adapter' (or
93  * equivalent) device descriptor. It contains all fields needed to
94  * support netmap operation.
95  */
96 struct netmap_adapter {
97 	int refcount; /* number of user-space descriptors using this
98 			 interface, which is equal to the number of
99 			 struct netmap_if objs in the mapped region. */
100 
101 	int separate_locks; /* set if the interface suports different
102 			       locks for rx, tx and core. */
103 
104 	u_int num_queues; /* number of tx/rx queue pairs: this is
105 			   a duplicate field needed to simplify the
106 			   signature of ``netmap_detach``. */
107 
108 	u_int num_tx_desc; /* number of descriptor in each queue */
109 	u_int num_rx_desc;
110 	u_int buff_size;
111 
112 	//u_int	flags;	// XXX unused
113 	/* tx_rings and rx_rings are private but allocated
114 	 * as a contiguous chunk of memory. Each array has
115 	 * N+1 entries, for the adapter queues and for the host queue.
116 	 */
117 	struct netmap_kring *tx_rings; /* array of TX rings. */
118 	struct netmap_kring *rx_rings; /* array of RX rings. */
119 
120 	/* copy of if_qflush and if_transmit pointers, to intercept
121 	 * packets from the network stack when netmap is active.
122 	 * XXX probably if_qflush is not necessary.
123 	 */
124 	//void    (*if_qflush)(struct ifnet *);	// XXX unused
125 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
126 
127 	/* references to the ifnet and device routines, used by
128 	 * the generic netmap functions.
129 	 */
130 	struct ifnet *ifp; /* adapter is ifp->if_softc */
131 
132 	NM_LOCK_T core_lock;	/* used if no device lock available */
133 
134 	int (*nm_register)(struct ifnet *, int onoff);
135 	void (*nm_lock)(struct ifnet *, int what, u_int ringid);
136 	int (*nm_txsync)(struct ifnet *, u_int ring, int lock);
137 	int (*nm_rxsync)(struct ifnet *, u_int ring, int lock);
138 };
139 
140 /*
141  * The combination of "enable" (ifp->if_capabilities &IFCAP_NETMAP)
142  * and refcount gives the status of the interface, namely:
143  *
144  *	enable	refcount	Status
145  *
146  *	FALSE	0		normal operation
147  *	FALSE	!= 0		-- (impossible)
148  *	TRUE	1		netmap mode
149  *	TRUE	0		being deleted.
150  */
151 
152 #define NETMAP_DELETING(_na)  (  ((_na)->refcount == 0) &&	\
153 	( (_na)->ifp->if_capenable & IFCAP_NETMAP) )
154 
155 /*
156  * parameters for (*nm_lock)(adapter, what, index)
157  */
158 enum {
159 	NETMAP_NO_LOCK = 0,
160 	NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK,
161 	NETMAP_TX_LOCK, NETMAP_TX_UNLOCK,
162 	NETMAP_RX_LOCK, NETMAP_RX_UNLOCK,
163 #ifdef __FreeBSD__
164 #define	NETMAP_REG_LOCK		NETMAP_CORE_LOCK
165 #define	NETMAP_REG_UNLOCK	NETMAP_CORE_UNLOCK
166 #else
167 	NETMAP_REG_LOCK, NETMAP_REG_UNLOCK
168 #endif
169 };
170 
171 /*
172  * The following are support routines used by individual drivers to
173  * support netmap operation.
174  *
175  * netmap_attach() initializes a struct netmap_adapter, allocating the
176  * 	struct netmap_ring's and the struct selinfo.
177  *
178  * netmap_detach() frees the memory allocated by netmap_attach().
179  *
180  * netmap_start() replaces the if_transmit routine of the interface,
181  *	and is used to intercept packets coming from the stack.
182  *
183  * netmap_load_map/netmap_reload_map are helper routines to set/reset
184  *	the dmamap for a packet buffer
185  *
186  * netmap_reset() is a helper routine to be called in the driver
187  *	when reinitializing a ring.
188  */
189 int netmap_attach(struct netmap_adapter *, int);
190 void netmap_detach(struct ifnet *);
191 int netmap_start(struct ifnet *, struct mbuf *);
192 enum txrx { NR_RX = 0, NR_TX = 1 };
193 struct netmap_slot *netmap_reset(struct netmap_adapter *na,
194 	enum txrx tx, int n, u_int new_cur);
195 int netmap_ring_reinit(struct netmap_kring *);
196 
197 extern int netmap_buf_size;
198 #define NETMAP_BUF_SIZE netmap_buf_size
199 extern int netmap_mitigate;
200 extern int netmap_no_pendintr;
201 extern u_int netmap_total_buffers;
202 extern char *netmap_buffer_base;
203 extern int netmap_verbose;	// XXX debugging
204 enum {                                  /* verbose flags */
205 	NM_VERB_ON = 1,                 /* generic verbose */
206 	NM_VERB_HOST = 0x2,             /* verbose host stack */
207 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
208 	NM_VERB_TXSYNC = 0x20,
209 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
210 	NM_VERB_TXINTR = 0x200,
211 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
212 	NM_VERB_NIC_TXSYNC = 0x2000,
213 };
214 
215 /*
216  * NA returns a pointer to the struct netmap adapter from the ifp,
217  * WNA is used to write it.
218  */
219 #ifndef WNA
220 #define	WNA(_ifp)	(_ifp)->if_pspare[0]
221 #endif
222 #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
223 
224 
225 /* Callback invoked by the dma machinery after a successfull dmamap_load */
226 static void netmap_dmamap_cb(__unused void *arg,
227     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
228 {
229 }
230 
231 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
232  * XXX can we do it without a callback ?
233  */
234 static inline void
235 netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
236 {
237 	if (map)
238 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
239 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
240 }
241 
242 /* update the map when a buffer changes. */
243 static inline void
244 netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
245 {
246 	if (map) {
247 		bus_dmamap_unload(tag, map);
248 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
249 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
250 	}
251 }
252 
253 /*
254  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
255  */
256 static inline int
257 netmap_ridx_n2k(struct netmap_adapter *na, int ring, int nic_idx)
258 {
259 	int kring_idx = nic_idx + na->rx_rings[ring].nkr_hwofs;
260 	if (kring_idx < 0)
261 		return kring_idx + na->num_rx_desc;
262 	else if (kring_idx < na->num_rx_desc)
263 		return kring_idx;
264 	else
265 		return kring_idx - na->num_rx_desc;
266 }
267 
268 static inline int
269 netmap_tidx_n2k(struct netmap_adapter *na, int ring, int nic_idx)
270 {
271 	int kring_idx = nic_idx + na->tx_rings[ring].nkr_hwofs;
272 	if (kring_idx < 0)
273 		return kring_idx + na->num_tx_desc;
274 	else if (kring_idx < na->num_tx_desc)
275 		return kring_idx;
276 	else
277 		return kring_idx - na->num_tx_desc;
278 }
279 
280 
281 static inline int
282 netmap_ridx_k2n(struct netmap_adapter *na, int ring, int kring_idx)
283 {
284 	int nic_idx = kring_idx - na->rx_rings[ring].nkr_hwofs;
285 	if (nic_idx < 0)
286 		return nic_idx + na->num_rx_desc;
287 	else if (nic_idx < na->num_rx_desc)
288 		return nic_idx;
289 	else
290 		return nic_idx - na->num_rx_desc;
291 }
292 
293 
294 static inline int
295 netmap_tidx_k2n(struct netmap_adapter *na, int ring, int kring_idx)
296 {
297 	int nic_idx = kring_idx - na->tx_rings[ring].nkr_hwofs;
298 	if (nic_idx < 0)
299 		return nic_idx + na->num_tx_desc;
300 	else if (nic_idx < na->num_tx_desc)
301 		return nic_idx;
302 	else
303 		return nic_idx - na->num_tx_desc;
304 }
305 
306 /*
307  * NMB return the virtual address of a buffer (buffer 0 on bad index)
308  * PNMB also fills the physical address
309  */
310 static inline void *
311 NMB(struct netmap_slot *slot)
312 {
313 	uint32_t i = slot->buf_idx;
314 	return (i >= netmap_total_buffers) ? netmap_buffer_base :
315 		netmap_buffer_base + (i *NETMAP_BUF_SIZE);
316 }
317 
318 static inline void *
319 PNMB(struct netmap_slot *slot, uint64_t *pp)
320 {
321 	uint32_t i = slot->buf_idx;
322 	void *ret = (i >= netmap_total_buffers) ? netmap_buffer_base :
323 		netmap_buffer_base + (i *NETMAP_BUF_SIZE);
324 	*pp = vtophys(ret);
325 	return ret;
326 }
327 
328 /* default functions to handle rx/tx interrupts */
329 int netmap_rx_irq(struct ifnet *, int, int *);
330 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
331 #ifdef __linux__
332 #define bus_dmamap_sync(_a, _b, _c) // wmb() or rmb() ?
333 netdev_tx_t netmap_start_linux(struct sk_buff *skb, struct net_device *dev);
334 #endif
335 #endif /* _NET_NETMAP_KERN_H_ */
336