xref: /freebsd-14.2/sys/dev/netmap/netmap_kern.h (revision d0c7b075)
168b8534bSLuigi Rizzo /*
268b8534bSLuigi Rizzo  * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
2668b8534bSLuigi Rizzo /*
2768b8534bSLuigi Rizzo  * $FreeBSD$
28506cc70cSLuigi Rizzo  * $Id: netmap_kern.h 9795 2011-12-02 11:39:08Z luigi $
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * The header contains the definitions of constants and function
3168b8534bSLuigi Rizzo  * prototypes used only in kernelspace.
3268b8534bSLuigi Rizzo  */
3368b8534bSLuigi Rizzo 
3468b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_
3568b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_
3668b8534bSLuigi Rizzo 
3768b8534bSLuigi Rizzo #ifdef MALLOC_DECLARE
3868b8534bSLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
3968b8534bSLuigi Rizzo #endif
4068b8534bSLuigi Rizzo 
4168b8534bSLuigi Rizzo #define ND(format, ...)
4268b8534bSLuigi Rizzo #define D(format, ...)					\
4368b8534bSLuigi Rizzo 	do {						\
4468b8534bSLuigi Rizzo 		struct timeval __xxts;			\
4568b8534bSLuigi Rizzo 		microtime(&__xxts);				\
4668b8534bSLuigi Rizzo 		printf("%03d.%06d %s [%d] " format "\n",\
4768b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,		\
4868b8534bSLuigi Rizzo 		__FUNCTION__, __LINE__, ##__VA_ARGS__);	\
4968b8534bSLuigi Rizzo 	} while (0)
5068b8534bSLuigi Rizzo 
5168b8534bSLuigi Rizzo struct netmap_adapter;
5268b8534bSLuigi Rizzo 
5368b8534bSLuigi Rizzo /*
5468b8534bSLuigi Rizzo  * private, kernel view of a ring.
5568b8534bSLuigi Rizzo  *
5668b8534bSLuigi Rizzo  * XXX 20110627-todo
5768b8534bSLuigi Rizzo  * The index in the NIC and netmap ring is offset by nkr_hwofs slots.
5868b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
5968b8534bSLuigi Rizzo  * modified by the kernel. In particular:
6068b8534bSLuigi Rizzo  * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides
6168b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
6268b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
6368b8534bSLuigi Rizzo  */
6468b8534bSLuigi Rizzo struct netmap_kring {
6568b8534bSLuigi Rizzo 	struct netmap_ring *ring;
6668b8534bSLuigi Rizzo 	u_int nr_hwcur;
6768b8534bSLuigi Rizzo 	int nr_hwavail;
6868b8534bSLuigi Rizzo 	u_int nr_kflags;
6968b8534bSLuigi Rizzo 	u_int nkr_num_slots;
7068b8534bSLuigi Rizzo 
71506cc70cSLuigi Rizzo 	int	nkr_hwofs;	/* offset between NIC and netmap ring */
7268b8534bSLuigi Rizzo 	struct netmap_adapter *na;	 // debugging
7368b8534bSLuigi Rizzo 	struct selinfo si; /* poll/select wait queue */
7468b8534bSLuigi Rizzo };
7568b8534bSLuigi Rizzo 
7668b8534bSLuigi Rizzo /*
7768b8534bSLuigi Rizzo  * This struct is part of and extends the 'struct adapter' (or
7868b8534bSLuigi Rizzo  * equivalent) device descriptor. It contains all fields needed to
7968b8534bSLuigi Rizzo  * support netmap operation.
8068b8534bSLuigi Rizzo  */
8168b8534bSLuigi Rizzo struct netmap_adapter {
8268b8534bSLuigi Rizzo 	int refcount; /* number of user-space descriptors using this
8368b8534bSLuigi Rizzo 			 interface, which is equal to the number of
8468b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
8568b8534bSLuigi Rizzo 
8668b8534bSLuigi Rizzo 	int separate_locks; /* set if the interface suports different
8768b8534bSLuigi Rizzo 			       locks for rx, tx and core. */
8868b8534bSLuigi Rizzo 
8968b8534bSLuigi Rizzo 	u_int num_queues; /* number of tx/rx queue pairs: this is
9068b8534bSLuigi Rizzo 			   a duplicate field needed to simplify the
9168b8534bSLuigi Rizzo 			   signature of ``netmap_detach``. */
9268b8534bSLuigi Rizzo 
9368b8534bSLuigi Rizzo 	u_int num_tx_desc; /* number of descriptor in each queue */
9468b8534bSLuigi Rizzo 	u_int num_rx_desc;
9568b8534bSLuigi Rizzo 	u_int buff_size;
9668b8534bSLuigi Rizzo 
97506cc70cSLuigi Rizzo 	u_int	flags;
9868b8534bSLuigi Rizzo 	/* tx_rings and rx_rings are private but allocated
9968b8534bSLuigi Rizzo 	 * as a contiguous chunk of memory. Each array has
10068b8534bSLuigi Rizzo 	 * N+1 entries, for the adapter queues and for the host queue.
10168b8534bSLuigi Rizzo 	 */
10268b8534bSLuigi Rizzo 	struct netmap_kring *tx_rings; /* array of TX rings. */
10368b8534bSLuigi Rizzo 	struct netmap_kring *rx_rings; /* array of RX rings. */
10468b8534bSLuigi Rizzo 
10568b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
10668b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
10768b8534bSLuigi Rizzo 	 * XXX probably if_qflush is not necessary.
10868b8534bSLuigi Rizzo 	 */
10968b8534bSLuigi Rizzo 	void    (*if_qflush)(struct ifnet *);
11068b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
11168b8534bSLuigi Rizzo 
11268b8534bSLuigi Rizzo 	/* references to the ifnet and device routines, used by
11368b8534bSLuigi Rizzo 	 * the generic netmap functions.
11468b8534bSLuigi Rizzo 	 */
11568b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
11668b8534bSLuigi Rizzo 
11768b8534bSLuigi Rizzo 	int (*nm_register)(struct ifnet *, int onoff);
11868b8534bSLuigi Rizzo 	void (*nm_lock)(void *, int what, u_int ringid);
11968b8534bSLuigi Rizzo 	int (*nm_txsync)(void *, u_int ring, int lock);
12068b8534bSLuigi Rizzo 	int (*nm_rxsync)(void *, u_int ring, int lock);
12168b8534bSLuigi Rizzo };
12268b8534bSLuigi Rizzo 
12368b8534bSLuigi Rizzo /*
12468b8534bSLuigi Rizzo  * The combination of "enable" (ifp->if_capabilities &IFCAP_NETMAP)
12568b8534bSLuigi Rizzo  * and refcount gives the status of the interface, namely:
12668b8534bSLuigi Rizzo  *
12768b8534bSLuigi Rizzo  *	enable	refcount	Status
12868b8534bSLuigi Rizzo  *
12968b8534bSLuigi Rizzo  *	FALSE	0		normal operation
13068b8534bSLuigi Rizzo  *	FALSE	!= 0		-- (impossible)
13168b8534bSLuigi Rizzo  *	TRUE	1		netmap mode
13268b8534bSLuigi Rizzo  *	TRUE	0		being deleted.
13368b8534bSLuigi Rizzo  */
13468b8534bSLuigi Rizzo 
13568b8534bSLuigi Rizzo #define NETMAP_DELETING(_na)  (  ((_na)->refcount == 0) &&	\
13668b8534bSLuigi Rizzo 	( (_na)->ifp->if_capenable & IFCAP_NETMAP) )
13768b8534bSLuigi Rizzo 
13868b8534bSLuigi Rizzo /*
13968b8534bSLuigi Rizzo  * parameters for (*nm_lock)(adapter, what, index)
14068b8534bSLuigi Rizzo  */
14168b8534bSLuigi Rizzo enum {
14268b8534bSLuigi Rizzo 	NETMAP_NO_LOCK = 0,
14368b8534bSLuigi Rizzo 	NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK,
14468b8534bSLuigi Rizzo 	NETMAP_TX_LOCK, NETMAP_TX_UNLOCK,
14568b8534bSLuigi Rizzo 	NETMAP_RX_LOCK, NETMAP_RX_UNLOCK,
14668b8534bSLuigi Rizzo };
14768b8534bSLuigi Rizzo 
14868b8534bSLuigi Rizzo /*
14968b8534bSLuigi Rizzo  * The following are support routines used by individual drivers to
15068b8534bSLuigi Rizzo  * support netmap operation.
15168b8534bSLuigi Rizzo  *
15268b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
15368b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
15468b8534bSLuigi Rizzo  *
15568b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
15668b8534bSLuigi Rizzo  *
15768b8534bSLuigi Rizzo  * netmap_start() replaces the if_transmit routine of the interface,
15868b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
15968b8534bSLuigi Rizzo  *
16068b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
16168b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
16268b8534bSLuigi Rizzo  *
16368b8534bSLuigi Rizzo  * netmap_reset() is a helper routine to be called in the driver
16468b8534bSLuigi Rizzo  *	when reinitializing a ring.
16568b8534bSLuigi Rizzo  */
16668b8534bSLuigi Rizzo int netmap_attach(struct netmap_adapter *, int);
16768b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
16868b8534bSLuigi Rizzo int netmap_start(struct ifnet *, struct mbuf *);
16968b8534bSLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1 };
17068b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
17168b8534bSLuigi Rizzo 	enum txrx tx, int n, u_int new_cur);
17268b8534bSLuigi Rizzo void netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map,
17368b8534bSLuigi Rizzo         void *buf, bus_size_t buflen);
17468b8534bSLuigi Rizzo void netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map,
17568b8534bSLuigi Rizzo         void *buf, bus_size_t buflen);
17668b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
17768b8534bSLuigi Rizzo 
17868b8534bSLuigi Rizzo /*
17968b8534bSLuigi Rizzo  * XXX eventually, get rid of netmap_total_buffers and netmap_buffer_base
18068b8534bSLuigi Rizzo  * in favour of the structure
18168b8534bSLuigi Rizzo  */
18268b8534bSLuigi Rizzo // struct netmap_buf_pool;
18368b8534bSLuigi Rizzo // extern struct netmap_buf_pool nm_buf_pool;
18468b8534bSLuigi Rizzo extern u_int netmap_total_buffers;
18568b8534bSLuigi Rizzo extern char *netmap_buffer_base;
18668b8534bSLuigi Rizzo extern int netmap_verbose;	// XXX debugging
18768b8534bSLuigi Rizzo enum {                                  /* verbose flags */
18868b8534bSLuigi Rizzo 	NM_VERB_ON = 1,                 /* generic verbose */
18968b8534bSLuigi Rizzo 	NM_VERB_HOST = 0x2,             /* verbose host stack */
19068b8534bSLuigi Rizzo 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
19168b8534bSLuigi Rizzo 	NM_VERB_TXSYNC = 0x20,
19268b8534bSLuigi Rizzo 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
19368b8534bSLuigi Rizzo 	NM_VERB_TXINTR = 0x200,
19468b8534bSLuigi Rizzo 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
19568b8534bSLuigi Rizzo 	NM_VERB_NIC_TXSYNC = 0x2000,
19668b8534bSLuigi Rizzo };
19768b8534bSLuigi Rizzo 
19868b8534bSLuigi Rizzo /*
199*d0c7b075SLuigi Rizzo  * NA returns a pointer to the struct netmap adapter from the ifp,
200*d0c7b075SLuigi Rizzo  * WNA is used to write it.
20168b8534bSLuigi Rizzo  */
202*d0c7b075SLuigi Rizzo #ifndef WNA
203*d0c7b075SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
204*d0c7b075SLuigi Rizzo #endif
205*d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
20668b8534bSLuigi Rizzo 
20768b8534bSLuigi Rizzo 
20868b8534bSLuigi Rizzo /*
20968b8534bSLuigi Rizzo  * return the address of a buffer.
21068b8534bSLuigi Rizzo  * XXX this is a special version with hardwired 2k bufs
21168b8534bSLuigi Rizzo  * On error return netmap_buffer_base which is detected as a bad pointer.
21268b8534bSLuigi Rizzo  */
21368b8534bSLuigi Rizzo static inline char *
21468b8534bSLuigi Rizzo NMB(struct netmap_slot *slot)
21568b8534bSLuigi Rizzo {
21668b8534bSLuigi Rizzo 	uint32_t i = slot->buf_idx;
21768b8534bSLuigi Rizzo 	return (i >= netmap_total_buffers) ? netmap_buffer_base :
21868b8534bSLuigi Rizzo #if NETMAP_BUF_SIZE == 2048
21968b8534bSLuigi Rizzo 		netmap_buffer_base + (i << 11);
22068b8534bSLuigi Rizzo #else
22168b8534bSLuigi Rizzo 		netmap_buffer_base + (i *NETMAP_BUF_SIZE);
22268b8534bSLuigi Rizzo #endif
22368b8534bSLuigi Rizzo }
22468b8534bSLuigi Rizzo 
22568b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
226