xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision 9a41df2a0e6408e9b329bbd8b9e37c2b44461a1b)
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 11343 2012-07-03 09:08:38Z 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 #define NETMAP_MEM2    // use the new memory allocator
38 
39 #if defined(__FreeBSD__)
40 #define likely(x)	__builtin_expect(!!(x), 1)
41 #define unlikely(x)	__builtin_expect(!!(x), 0)
42 
43 #define	NM_LOCK_T	struct mtx
44 #define	NM_SELINFO_T	struct selinfo
45 #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
46 #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
47 #elif defined (linux)
48 #define	NM_LOCK_T	spinlock_t
49 #define	NM_SELINFO_T	wait_queue_head_t
50 #define	MBUF_LEN(m)	((m)->len)
51 #define	NM_SEND_UP(ifp, m)	netif_rx(m)
52 
53 #ifndef DEV_NETMAP
54 #define DEV_NETMAP
55 #endif
56 
57 /*
58  * IFCAP_NETMAP goes into net_device's flags (if_capabilities)
59  * and priv_flags (if_capenable). The latter used to be 16 bits
60  * up to linux 2.6.36, so we need to use a 16 bit value on older
61  * platforms and tolerate the clash with IFF_DYNAMIC and IFF_BRIDGE_PORT.
62  * For the 32-bit value, 0x100000 (bit 20) has no clashes up to 3.3.1
63  */
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
65 #define IFCAP_NETMAP	0x8000
66 #else
67 #define IFCAP_NETMAP	0x100000
68 #endif
69 
70 #elif defined (__APPLE__)
71 #warning apple support is experimental
72 #define likely(x)	__builtin_expect(!!(x), 1)
73 #define unlikely(x)	__builtin_expect(!!(x), 0)
74 #define	NM_LOCK_T	IOLock *
75 #define	NM_SELINFO_T	struct selinfo
76 #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
77 #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
78 
79 #else
80 #error unsupported platform
81 #endif
82 
83 #define ND(format, ...)
84 #define D(format, ...)						\
85 	do {							\
86 		struct timeval __xxts;				\
87 		microtime(&__xxts);				\
88 		printf("%03d.%06d %s [%d] " format "\n",	\
89 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
90 		__FUNCTION__, __LINE__, ##__VA_ARGS__);		\
91 	} while (0)
92 
93 struct netmap_adapter;
94 
95 /*
96  * private, kernel view of a ring. Keeps track of the status of
97  * a ring across system calls.
98  *
99  *	nr_hwcur	index of the next buffer to refill.
100  *			It corresponds to ring->cur - ring->reserved
101  *
102  *	nr_hwavail	the number of slots "owned" by userspace.
103  *			nr_hwavail =:= ring->avail + ring->reserved
104  *
105  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
106  * This is so that, on a reset, buffers owned by userspace are not
107  * modified by the kernel. In particular:
108  * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with
109  * 	the next empty buffer as known by the hardware (next_to_check or so).
110  * TX rings: hwcur + hwofs coincides with next_to_send
111  */
112 struct netmap_kring {
113 	struct netmap_ring *ring;
114 	u_int nr_hwcur;
115 	int nr_hwavail;
116 	u_int nr_kflags;	/* private driver flags */
117 #define NKR_PENDINTR	0x1	// Pending interrupt.
118 	u_int nkr_num_slots;
119 
120 	int	nkr_hwofs;	/* offset between NIC and netmap ring */
121 	struct netmap_adapter *na;
122 	NM_SELINFO_T si;	/* poll/select wait queue */
123 	NM_LOCK_T q_lock;	/* used if no device lock available */
124 } __attribute__((__aligned__(64)));
125 
126 /*
127  * This struct extends the 'struct adapter' (or
128  * equivalent) device descriptor. It contains all fields needed to
129  * support netmap operation.
130  */
131 struct netmap_adapter {
132 	int refcount; /* number of user-space descriptors using this
133 			 interface, which is equal to the number of
134 			 struct netmap_if objs in the mapped region. */
135 	/*
136 	 * The selwakeup in the interrupt thread can use per-ring
137 	 * and/or global wait queues. We track how many clients
138 	 * of each type we have so we can optimize the drivers,
139 	 * and especially avoid huge contention on the locks.
140 	 */
141 	int na_single;	/* threads attached to a single hw queue */
142 	int na_multi;	/* threads attached to multiple hw queues */
143 
144 	int separate_locks; /* set if the interface suports different
145 			       locks for rx, tx and core. */
146 
147 	u_int num_rx_rings; /* number of adapter receive rings */
148 	u_int num_tx_rings; /* number of adapter transmit rings */
149 
150 	u_int num_tx_desc; /* number of descriptor in each queue */
151 	u_int num_rx_desc;
152 	//u_int buff_size;	// XXX deprecate, use NETMAP_BUF_SIZE
153 
154 	/* tx_rings and rx_rings are private but allocated
155 	 * as a contiguous chunk of memory. Each array has
156 	 * N+1 entries, for the adapter queues and for the host queue.
157 	 */
158 	struct netmap_kring *tx_rings; /* array of TX rings. */
159 	struct netmap_kring *rx_rings; /* array of RX rings. */
160 
161 	NM_SELINFO_T tx_si, rx_si;	/* global wait queues */
162 
163 	/* copy of if_qflush and if_transmit pointers, to intercept
164 	 * packets from the network stack when netmap is active.
165 	 */
166 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
167 
168 	/* references to the ifnet and device routines, used by
169 	 * the generic netmap functions.
170 	 */
171 	struct ifnet *ifp; /* adapter is ifp->if_softc */
172 
173 	NM_LOCK_T core_lock;	/* used if no device lock available */
174 
175 	int (*nm_register)(struct ifnet *, int onoff);
176 	void (*nm_lock)(struct ifnet *, int what, u_int ringid);
177 	int (*nm_txsync)(struct ifnet *, u_int ring, int lock);
178 	int (*nm_rxsync)(struct ifnet *, u_int ring, int lock);
179 
180 	int bdg_port;
181 #ifdef linux
182 	struct net_device_ops nm_ndo;
183 	int if_refcount;	// XXX additions for bridge
184 #endif /* linux */
185 };
186 
187 /*
188  * The combination of "enable" (ifp->if_capabilities &IFCAP_NETMAP)
189  * and refcount gives the status of the interface, namely:
190  *
191  *	enable	refcount	Status
192  *
193  *	FALSE	0		normal operation
194  *	FALSE	!= 0		-- (impossible)
195  *	TRUE	1		netmap mode
196  *	TRUE	0		being deleted.
197  */
198 
199 #define NETMAP_DELETING(_na)  (  ((_na)->refcount == 0) &&	\
200 	( (_na)->ifp->if_capenable & IFCAP_NETMAP) )
201 
202 /*
203  * parameters for (*nm_lock)(adapter, what, index)
204  */
205 enum {
206 	NETMAP_NO_LOCK = 0,
207 	NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK,
208 	NETMAP_TX_LOCK, NETMAP_TX_UNLOCK,
209 	NETMAP_RX_LOCK, NETMAP_RX_UNLOCK,
210 #ifdef __FreeBSD__
211 #define	NETMAP_REG_LOCK		NETMAP_CORE_LOCK
212 #define	NETMAP_REG_UNLOCK	NETMAP_CORE_UNLOCK
213 #else
214 	NETMAP_REG_LOCK, NETMAP_REG_UNLOCK
215 #endif
216 };
217 
218 /*
219  * The following are support routines used by individual drivers to
220  * support netmap operation.
221  *
222  * netmap_attach() initializes a struct netmap_adapter, allocating the
223  * 	struct netmap_ring's and the struct selinfo.
224  *
225  * netmap_detach() frees the memory allocated by netmap_attach().
226  *
227  * netmap_start() replaces the if_transmit routine of the interface,
228  *	and is used to intercept packets coming from the stack.
229  *
230  * netmap_load_map/netmap_reload_map are helper routines to set/reset
231  *	the dmamap for a packet buffer
232  *
233  * netmap_reset() is a helper routine to be called in the driver
234  *	when reinitializing a ring.
235  */
236 int netmap_attach(struct netmap_adapter *, int);
237 void netmap_detach(struct ifnet *);
238 int netmap_start(struct ifnet *, struct mbuf *);
239 enum txrx { NR_RX = 0, NR_TX = 1 };
240 struct netmap_slot *netmap_reset(struct netmap_adapter *na,
241 	enum txrx tx, int n, u_int new_cur);
242 int netmap_ring_reinit(struct netmap_kring *);
243 
244 extern u_int netmap_buf_size;
245 #define NETMAP_BUF_SIZE	netmap_buf_size
246 extern int netmap_mitigate;
247 extern int netmap_no_pendintr;
248 extern u_int netmap_total_buffers;
249 extern char *netmap_buffer_base;
250 extern int netmap_verbose;	// XXX debugging
251 enum {                                  /* verbose flags */
252 	NM_VERB_ON = 1,                 /* generic verbose */
253 	NM_VERB_HOST = 0x2,             /* verbose host stack */
254 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
255 	NM_VERB_TXSYNC = 0x20,
256 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
257 	NM_VERB_TXINTR = 0x200,
258 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
259 	NM_VERB_NIC_TXSYNC = 0x2000,
260 };
261 
262 /*
263  * NA returns a pointer to the struct netmap adapter from the ifp,
264  * WNA is used to write it.
265  */
266 #ifndef WNA
267 #define	WNA(_ifp)	(_ifp)->if_pspare[0]
268 #endif
269 #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
270 
271 
272 #ifdef __FreeBSD__
273 /* Callback invoked by the dma machinery after a successfull dmamap_load */
274 static void netmap_dmamap_cb(__unused void *arg,
275     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
276 {
277 }
278 
279 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
280  * XXX can we do it without a callback ?
281  */
282 static inline void
283 netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
284 {
285 	if (map)
286 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
287 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
288 }
289 
290 /* update the map when a buffer changes. */
291 static inline void
292 netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
293 {
294 	if (map) {
295 		bus_dmamap_unload(tag, map);
296 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
297 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
298 	}
299 }
300 #else /* linux */
301 
302 /*
303  * XXX How do we redefine these functions:
304  *
305  * on linux we need
306  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
307  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
308  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
309  * unfortunately the direction is not, so we need to change
310  * something to have a cross API
311  */
312 #define netmap_load_map(_t, _m, _b)
313 #define netmap_reload_map(_t, _m, _b)
314 #if 0
315 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
316 	/* set time_stamp *before* dma to help avoid a possible race */
317 	buffer_info->time_stamp = jiffies;
318 	buffer_info->mapped_as_page = false;
319 	buffer_info->length = len;
320 	//buffer_info->next_to_watch = l;
321 	/* reload dma map */
322 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
323 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
324 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
325 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
326 
327 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
328 		D("dma mapping error");
329 		/* goto dma_error; See e1000_put_txbuf() */
330 		/* XXX reset */
331 	}
332 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
333 
334 #endif
335 
336 /*
337  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
338  */
339 #define bus_dmamap_sync(_a, _b, _c)
340 
341 #endif /* linux */
342 
343 /*
344  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
345  */
346 static inline int
347 netmap_idx_n2k(struct netmap_kring *kr, int idx)
348 {
349 	int n = kr->nkr_num_slots;
350 	idx += kr->nkr_hwofs;
351 	if (idx < 0)
352 		return idx + n;
353 	else if (idx < n)
354 		return idx;
355 	else
356 		return idx - n;
357 }
358 
359 
360 static inline int
361 netmap_idx_k2n(struct netmap_kring *kr, int idx)
362 {
363 	int n = kr->nkr_num_slots;
364 	idx -= kr->nkr_hwofs;
365 	if (idx < 0)
366 		return idx + n;
367 	else if (idx < n)
368 		return idx;
369 	else
370 		return idx - n;
371 }
372 
373 
374 #ifdef NETMAP_MEM2
375 /* Entries of the look-up table. */
376 struct lut_entry {
377 	void *vaddr;		/* virtual address. */
378 	vm_paddr_t paddr;	/* phisical address. */
379 };
380 
381 struct netmap_obj_pool;
382 extern struct lut_entry *netmap_buffer_lut;
383 #define NMB_VA(i)	(netmap_buffer_lut[i].vaddr)
384 #define NMB_PA(i)	(netmap_buffer_lut[i].paddr)
385 #else /* NETMAP_MEM1 */
386 #define NMB_VA(i)	(netmap_buffer_base + (i * NETMAP_BUF_SIZE) )
387 #endif /* NETMAP_MEM2 */
388 
389 /*
390  * NMB return the virtual address of a buffer (buffer 0 on bad index)
391  * PNMB also fills the physical address
392  */
393 static inline void *
394 NMB(struct netmap_slot *slot)
395 {
396 	uint32_t i = slot->buf_idx;
397 	return (unlikely(i >= netmap_total_buffers)) ?  NMB_VA(0) : NMB_VA(i);
398 }
399 
400 static inline void *
401 PNMB(struct netmap_slot *slot, uint64_t *pp)
402 {
403 	uint32_t i = slot->buf_idx;
404 	void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i);
405 #ifdef NETMAP_MEM2
406 	*pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i);
407 #else
408 	*pp = vtophys(ret);
409 #endif
410 	return ret;
411 }
412 
413 /* default functions to handle rx/tx interrupts */
414 int netmap_rx_irq(struct ifnet *, int, int *);
415 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
416 
417 extern int netmap_copy;
418 #endif /* _NET_NETMAP_KERN_H_ */
419