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