xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision d76bf4ff)
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  * This module supports memory mapped access to network devices,
2868b8534bSLuigi Rizzo  * see netmap(4).
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3168b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3268b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3368b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3468b8534bSLuigi Rizzo  *
3568b8534bSLuigi Rizzo  * Access to the network card works like this:
3668b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
3768b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
3868b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
3968b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4068b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4168b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4268b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4368b8534bSLuigi Rizzo  *    the shared memory region.
4468b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4568b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4668b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
4768b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
4868b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
4968b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5068b8534bSLuigi Rizzo  *    packets on the output interface.
5168b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5268b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
5368b8534bSLuigi Rizzo  */
5468b8534bSLuigi Rizzo 
5568b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
5668b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
5768b8534bSLuigi Rizzo 
5868b8534bSLuigi Rizzo #include <sys/types.h>
5968b8534bSLuigi Rizzo #include <sys/module.h>
6068b8534bSLuigi Rizzo #include <sys/errno.h>
6168b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
62506cc70cSLuigi Rizzo #include <sys/jail.h>
6368b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
6468b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
6568b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
6668b8534bSLuigi Rizzo #include <sys/sockio.h>
6768b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
6868b8534bSLuigi Rizzo #include <sys/malloc.h>
6968b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
7068b8534bSLuigi Rizzo #include <sys/poll.h>
71506cc70cSLuigi Rizzo #include <sys/proc.h>
7268b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
7368b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
7468b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
7568b8534bSLuigi Rizzo #include <machine/bus.h>
7668b8534bSLuigi Rizzo #include <sys/selinfo.h>
7768b8534bSLuigi Rizzo #include <sys/sysctl.h>
7868b8534bSLuigi Rizzo #include <net/if.h>
7968b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
80506cc70cSLuigi Rizzo #include <net/vnet.h>
8168b8534bSLuigi Rizzo #include <net/netmap.h>
8268b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
8368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
8468b8534bSLuigi Rizzo 
8568b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
8668b8534bSLuigi Rizzo 
8768b8534bSLuigi Rizzo /*
8868b8534bSLuigi Rizzo  * lock and unlock for the netmap memory allocator
8968b8534bSLuigi Rizzo  */
9064ae02c3SLuigi Rizzo #define NMA_LOCK()	mtx_lock(&nm_mem->nm_mtx);
9164ae02c3SLuigi Rizzo #define NMA_UNLOCK()	mtx_unlock(&nm_mem->nm_mtx);
925819da83SLuigi Rizzo struct netmap_mem_d;
9364ae02c3SLuigi Rizzo static struct netmap_mem_d *nm_mem;	/* Our memory allocator. */
945819da83SLuigi Rizzo 
955819da83SLuigi Rizzo u_int netmap_total_buffers;
965819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
975819da83SLuigi Rizzo 
985819da83SLuigi Rizzo /* user-controlled variables */
995819da83SLuigi Rizzo int netmap_verbose;
1005819da83SLuigi Rizzo 
1015819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1025819da83SLuigi Rizzo 
1035819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1045819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1055819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1065819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1075819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1085819da83SLuigi Rizzo int netmap_buf_size = 2048;
1095819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size);
1105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
1115819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
1125819da83SLuigi Rizzo int netmap_mitigate = 1;
1135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
114c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1165819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1175819da83SLuigi Rizzo 
1185819da83SLuigi Rizzo 
1193c0caf6cSLuigi Rizzo /*------------- memory allocator -----------------*/
1203c0caf6cSLuigi Rizzo #ifdef NETMAP_MEM2
1213c0caf6cSLuigi Rizzo #include "netmap_mem2.c"
1223c0caf6cSLuigi Rizzo #else /* !NETMAP_MEM2 */
1233c0caf6cSLuigi Rizzo #include "netmap_mem1.c"
1243c0caf6cSLuigi Rizzo #endif /* !NETMAP_MEM2 */
1253c0caf6cSLuigi Rizzo /*------------ end of memory allocator ----------*/
12668b8534bSLuigi Rizzo 
12768b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
12868b8534bSLuigi Rizzo struct netmap_priv_d {
12968b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
13068b8534bSLuigi Rizzo 
13168b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
13268b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
13368b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
13468b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
13568b8534bSLuigi Rizzo };
13668b8534bSLuigi Rizzo 
13768b8534bSLuigi Rizzo 
13868b8534bSLuigi Rizzo /*
13968b8534bSLuigi Rizzo  * File descriptor's private data destructor.
14068b8534bSLuigi Rizzo  *
14168b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
14268b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
14368b8534bSLuigi Rizzo  */
14468b8534bSLuigi Rizzo static void
1455819da83SLuigi Rizzo netmap_dtor_locked(void *data)
14668b8534bSLuigi Rizzo {
14768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
14868b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
14968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
15068b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
15168b8534bSLuigi Rizzo 
15268b8534bSLuigi Rizzo 	na->refcount--;
15368b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
15464ae02c3SLuigi Rizzo 		u_int i, j, lim;
15568b8534bSLuigi Rizzo 
15668b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
15768b8534bSLuigi Rizzo 		/*
15868b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
1591a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
16068b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
16168b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
16268b8534bSLuigi Rizzo 		 * device is dying.
16368b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
16468b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
16568b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
16668b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
16768b8534bSLuigi Rizzo 		 * they cannot run.
16868b8534bSLuigi Rizzo 		 */
1691a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
17068b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
1711a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
17268b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
17368b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
17468b8534bSLuigi Rizzo 		 * then return POLLERR
17568b8534bSLuigi Rizzo 		 */
176*d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++)
17768b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
178*d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++)
17968b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
18064ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
18164ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
18268b8534bSLuigi Rizzo 		/* release all buffers */
18368b8534bSLuigi Rizzo 		NMA_LOCK();
184*d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
18564ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
18668b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
18768b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
188446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
18964ae02c3SLuigi Rizzo 		}
190*d76bf4ffSLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
19164ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
19268b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
19368b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
194446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
19568b8534bSLuigi Rizzo 		}
19668b8534bSLuigi Rizzo 		NMA_UNLOCK();
1976e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
19868b8534bSLuigi Rizzo 		wakeup(na);
19968b8534bSLuigi Rizzo 	}
2006e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
2015819da83SLuigi Rizzo }
20268b8534bSLuigi Rizzo 
2035819da83SLuigi Rizzo 
2045819da83SLuigi Rizzo static void
2055819da83SLuigi Rizzo netmap_dtor(void *data)
2065819da83SLuigi Rizzo {
2075819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
2085819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
2095819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2105819da83SLuigi Rizzo 
2111a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
2125819da83SLuigi Rizzo 	netmap_dtor_locked(data);
2131a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
21468b8534bSLuigi Rizzo 
21568b8534bSLuigi Rizzo 	if_rele(ifp);
21668b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
21768b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
21868b8534bSLuigi Rizzo }
21968b8534bSLuigi Rizzo 
22068b8534bSLuigi Rizzo 
22168b8534bSLuigi Rizzo /*
22268b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
22368b8534bSLuigi Rizzo  *
22468b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
22568b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
22668b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
22768b8534bSLuigi Rizzo  *
22868b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
22968b8534bSLuigi Rizzo  */
230babc7c12SLuigi Rizzo 
23168b8534bSLuigi Rizzo static int
232babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
23368b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
234babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
23568b8534bSLuigi Rizzo #else
236babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
237babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
23868b8534bSLuigi Rizzo #endif
239babc7c12SLuigi Rizzo 	)
24068b8534bSLuigi Rizzo {
24168b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
24268b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
243446ee301SLuigi Rizzo 
24468b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
245446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
24668b8534bSLuigi Rizzo 
24768b8534bSLuigi Rizzo 	return (0);
24868b8534bSLuigi Rizzo }
24968b8534bSLuigi Rizzo 
25068b8534bSLuigi Rizzo 
25168b8534bSLuigi Rizzo /*
25202ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
25302ad4083SLuigi Rizzo  *
25402ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
25502ad4083SLuigi Rizzo  * system call in user process context, and the only contention
25602ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
25702ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
25802ad4083SLuigi Rizzo  * need to lock.
25968b8534bSLuigi Rizzo  */
26068b8534bSLuigi Rizzo static void
26168b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
26268b8534bSLuigi Rizzo {
263*d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
26468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
26568b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
26602ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
26768b8534bSLuigi Rizzo 
26802ad4083SLuigi Rizzo 	k = ring->cur;
26902ad4083SLuigi Rizzo 	if (k > lim) {
27002ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
27102ad4083SLuigi Rizzo 		return;
27202ad4083SLuigi Rizzo 	}
2731a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
27468b8534bSLuigi Rizzo 
27568b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
27668b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
27768b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
27868b8534bSLuigi Rizzo 	 */
27902ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
28068b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
28168b8534bSLuigi Rizzo 
28268b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
28368b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
28468b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
28568b8534bSLuigi Rizzo 			continue;
28668b8534bSLuigi Rizzo 		}
28768b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
28868b8534bSLuigi Rizzo 
28968b8534bSLuigi Rizzo 		if (m == NULL)
29068b8534bSLuigi Rizzo 			break;
29168b8534bSLuigi Rizzo 		if (tail)
29268b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
29368b8534bSLuigi Rizzo 		else
29468b8534bSLuigi Rizzo 			head = m;
29568b8534bSLuigi Rizzo 		tail = m;
29668b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
29768b8534bSLuigi Rizzo 	}
29802ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
29968b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
3001a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
30168b8534bSLuigi Rizzo 
30268b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
30368b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
30468b8534bSLuigi Rizzo 		head = head->m_nextpkt;
30568b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
30668b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
3071a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
3081a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
30968b8534bSLuigi Rizzo 	}
31068b8534bSLuigi Rizzo }
31168b8534bSLuigi Rizzo 
31268b8534bSLuigi Rizzo /*
31302ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
31402ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
31502ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
31602ad4083SLuigi Rizzo  *
31768b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
31868b8534bSLuigi Rizzo  * (we know because td != NULL).
31968b8534bSLuigi Rizzo  */
32068b8534bSLuigi Rizzo static void
32168b8534bSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td)
32268b8534bSLuigi Rizzo {
323*d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
32468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
32564ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
32664ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
32768b8534bSLuigi Rizzo 
3281a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
32964ae02c3SLuigi Rizzo 	if (k >= lim) {
33064ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
33164ae02c3SLuigi Rizzo 		return;
33264ae02c3SLuigi Rizzo 	}
33364ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
33464ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
33564ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
33664ae02c3SLuigi Rizzo 	if (resvd > 0) {
33764ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
33864ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
33964ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
34064ae02c3SLuigi Rizzo 		}
34164ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
34264ae02c3SLuigi Rizzo         }
34364ae02c3SLuigi Rizzo 	if (j != k) {
34464ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
34564ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
34602ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
34764ae02c3SLuigi Rizzo 	}
34864ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
34902ad4083SLuigi Rizzo 	if (k == 0 && td)
35068b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
35102ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
35202ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
3531a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
35468b8534bSLuigi Rizzo }
35568b8534bSLuigi Rizzo 
35668b8534bSLuigi Rizzo 
35768b8534bSLuigi Rizzo /*
35868b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
35968b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
36068b8534bSLuigi Rizzo  * is not supported by the interface.
36168b8534bSLuigi Rizzo  * If successful, hold a reference.
36268b8534bSLuigi Rizzo  */
36368b8534bSLuigi Rizzo static int
36468b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
36568b8534bSLuigi Rizzo {
36668b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
36768b8534bSLuigi Rizzo 	if (*ifp == NULL)
36868b8534bSLuigi Rizzo 		return (ENXIO);
36968b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
37068b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
37168b8534bSLuigi Rizzo 	 */
37268b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
37368b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
37468b8534bSLuigi Rizzo 	if_rele(*ifp);
37568b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
37668b8534bSLuigi Rizzo }
37768b8534bSLuigi Rizzo 
37868b8534bSLuigi Rizzo 
37968b8534bSLuigi Rizzo /*
38068b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
38168b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
38268b8534bSLuigi Rizzo  * Return 1 on reinit.
383506cc70cSLuigi Rizzo  *
384506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
385506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
386506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
387506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
388506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
389506cc70cSLuigi Rizzo  * it under lock.
39068b8534bSLuigi Rizzo  */
39168b8534bSLuigi Rizzo int
39268b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
39368b8534bSLuigi Rizzo {
39468b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
39568b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
39668b8534bSLuigi Rizzo 	int errors = 0;
39768b8534bSLuigi Rizzo 
39868b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
39968b8534bSLuigi Rizzo 	if (ring->cur > lim)
40068b8534bSLuigi Rizzo 		errors++;
40168b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
40268b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
40368b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
40468b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
40568b8534bSLuigi Rizzo 			if (!errors++)
40668b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
40768b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
40868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
40968b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
41068b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
41168b8534bSLuigi Rizzo 			if (!errors++)
41268b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
41368b8534bSLuigi Rizzo 					len, i, idx);
41468b8534bSLuigi Rizzo 		}
41568b8534bSLuigi Rizzo 	}
41668b8534bSLuigi Rizzo 	if (errors) {
41768b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
418*d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
41968b8534bSLuigi Rizzo 
42068b8534bSLuigi Rizzo 		D("total %d errors", errors);
42168b8534bSLuigi Rizzo 		errors++;
42268b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
42368b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
42468b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
42568b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
42668b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
42768b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
42868b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
42968b8534bSLuigi Rizzo 	}
43068b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
43168b8534bSLuigi Rizzo }
43268b8534bSLuigi Rizzo 
43368b8534bSLuigi Rizzo 
43468b8534bSLuigi Rizzo /*
43568b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
43668b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
43768b8534bSLuigi Rizzo  */
43868b8534bSLuigi Rizzo static int
43968b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
44068b8534bSLuigi Rizzo {
44168b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
44268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
44368b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
44464ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
44568b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
446*d76bf4ffSLuigi Rizzo 	int lim = na->num_rx_rings;
44768b8534bSLuigi Rizzo 
448*d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
449*d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
45064ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
45168b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
45268b8534bSLuigi Rizzo 		return (EINVAL);
45368b8534bSLuigi Rizzo 	}
45468b8534bSLuigi Rizzo 	if (need_lock)
4551a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
45668b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
45768b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
45864ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
45964ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
46068b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
46168b8534bSLuigi Rizzo 		priv->np_qfirst = i;
46268b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
46368b8534bSLuigi Rizzo 	} else {
46468b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
46564ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
46668b8534bSLuigi Rizzo 	}
46768b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
46868b8534bSLuigi Rizzo 	if (need_lock)
4691a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
47068b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
47168b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
47268b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
47368b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
47468b8534bSLuigi Rizzo 			priv->np_qfirst);
47568b8534bSLuigi Rizzo 	else
47664ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
47768b8534bSLuigi Rizzo 	return 0;
47868b8534bSLuigi Rizzo }
47968b8534bSLuigi Rizzo 
48068b8534bSLuigi Rizzo /*
48168b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
48268b8534bSLuigi Rizzo  *
48368b8534bSLuigi Rizzo  * Following a list of accepted commands:
48468b8534bSLuigi Rizzo  * - NIOCGINFO
48568b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
48668b8534bSLuigi Rizzo  * - NIOCREGIF
48768b8534bSLuigi Rizzo  * - NIOCUNREGIF
48868b8534bSLuigi Rizzo  * - NIOCTXSYNC
48968b8534bSLuigi Rizzo  * - NIOCRXSYNC
49068b8534bSLuigi Rizzo  *
49168b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
49268b8534bSLuigi Rizzo  */
49368b8534bSLuigi Rizzo static int
49468b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data,
495506cc70cSLuigi Rizzo 	__unused int fflag, struct thread *td)
49668b8534bSLuigi Rizzo {
49768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
49868b8534bSLuigi Rizzo 	struct ifnet *ifp;
49968b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
50068b8534bSLuigi Rizzo 	struct netmap_adapter *na;
50168b8534bSLuigi Rizzo 	int error;
50264ae02c3SLuigi Rizzo 	u_int i, lim;
50368b8534bSLuigi Rizzo 	struct netmap_if *nifp;
50468b8534bSLuigi Rizzo 
505506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
506506cc70cSLuigi Rizzo 
50768b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
508506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
509506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
51068b8534bSLuigi Rizzo 		return (error);
511506cc70cSLuigi Rizzo 	}
51268b8534bSLuigi Rizzo 
51368b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
51468b8534bSLuigi Rizzo 	switch (cmd) {
51568b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
51668b8534bSLuigi Rizzo 		/* memsize is always valid */
51764ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
51868b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
51964ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
52064ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
52164ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
52264ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
52364ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
52464ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
52564ae02c3SLuigi Rizzo 			error = EINVAL;
52664ae02c3SLuigi Rizzo 			break;
52764ae02c3SLuigi Rizzo 		}
52868b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
52968b8534bSLuigi Rizzo 			break;
53068b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
53168b8534bSLuigi Rizzo 		if (error)
53268b8534bSLuigi Rizzo 			break;
53368b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
534*d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
535*d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
53664ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
53764ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
53868b8534bSLuigi Rizzo 		if_rele(ifp);	/* return the refcount */
53968b8534bSLuigi Rizzo 		break;
54068b8534bSLuigi Rizzo 
54168b8534bSLuigi Rizzo 	case NIOCREGIF:
54264ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
54364ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
54464ae02c3SLuigi Rizzo 			error = EINVAL;
54564ae02c3SLuigi Rizzo 			break;
54664ae02c3SLuigi Rizzo 		}
547506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
548506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
549506cc70cSLuigi Rizzo 			break;
550506cc70cSLuigi Rizzo 		}
55168b8534bSLuigi Rizzo 		/* find the interface and a reference */
55268b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
55368b8534bSLuigi Rizzo 		if (error)
55468b8534bSLuigi Rizzo 			break;
55568b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
55668b8534bSLuigi Rizzo 		/*
55768b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
55868b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
55968b8534bSLuigi Rizzo 		 */
56068b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
56168b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
56268b8534bSLuigi Rizzo 		if (priv == NULL) {
56368b8534bSLuigi Rizzo 			error = ENOMEM;
56468b8534bSLuigi Rizzo 			if_rele(ifp);   /* return the refcount */
56568b8534bSLuigi Rizzo 			break;
56668b8534bSLuigi Rizzo 		}
56768b8534bSLuigi Rizzo 
56868b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
5691a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
57068b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
57168b8534bSLuigi Rizzo 				break;
5721a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
57368b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
57468b8534bSLuigi Rizzo 		}
57568b8534bSLuigi Rizzo 		if (i == 0) {
57668b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
57768b8534bSLuigi Rizzo 			error = EINVAL;
57868b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
57968b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
58068b8534bSLuigi Rizzo 			break;
58168b8534bSLuigi Rizzo 		}
58268b8534bSLuigi Rizzo 
58368b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
58468b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
58568b8534bSLuigi Rizzo 		if (error)
58668b8534bSLuigi Rizzo 			goto error;
58768b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
58868b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
58968b8534bSLuigi Rizzo 			error = ENOMEM;
59068b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
59168b8534bSLuigi Rizzo 			/* was already set */
59268b8534bSLuigi Rizzo 		} else {
59368b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
59468b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
59568b8534bSLuigi Rizzo 			 */
59668b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
5975819da83SLuigi Rizzo 			if (error)
5985819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
59968b8534bSLuigi Rizzo 		}
60068b8534bSLuigi Rizzo 
60168b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
60268b8534bSLuigi Rizzo error:
6031a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
60468b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
6055819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
6065819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
60768b8534bSLuigi Rizzo 			break;
60868b8534bSLuigi Rizzo 		}
60968b8534bSLuigi Rizzo 
6101a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
61168b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
61268b8534bSLuigi Rizzo 
61368b8534bSLuigi Rizzo 		if (error != 0) {
61468b8534bSLuigi Rizzo 			/* could not assign the private storage for the
61568b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
61668b8534bSLuigi Rizzo 			 */
61768b8534bSLuigi Rizzo 			netmap_dtor(priv);
61868b8534bSLuigi Rizzo 			break;
61968b8534bSLuigi Rizzo 		}
62068b8534bSLuigi Rizzo 
62168b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
622*d76bf4ffSLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_rings;
623*d76bf4ffSLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_rings;
62464ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
62564ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
62664ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
627446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
62868b8534bSLuigi Rizzo 		break;
62968b8534bSLuigi Rizzo 
63068b8534bSLuigi Rizzo 	case NIOCUNREGIF:
631506cc70cSLuigi Rizzo 		if (priv == NULL) {
632506cc70cSLuigi Rizzo 			error = ENXIO;
633506cc70cSLuigi Rizzo 			break;
634506cc70cSLuigi Rizzo 		}
63568b8534bSLuigi Rizzo 
63668b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
63768b8534bSLuigi Rizzo 		   destructor of the private data. */
63868b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
63968b8534bSLuigi Rizzo 		break;
64068b8534bSLuigi Rizzo 
64168b8534bSLuigi Rizzo 	case NIOCTXSYNC:
64268b8534bSLuigi Rizzo         case NIOCRXSYNC:
643506cc70cSLuigi Rizzo 		if (priv == NULL) {
644506cc70cSLuigi Rizzo 			error = ENXIO;
645506cc70cSLuigi Rizzo 			break;
646506cc70cSLuigi Rizzo 		}
64768b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
64868b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
64964ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
65068b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
65168b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
65268b8534bSLuigi Rizzo 			else
65368b8534bSLuigi Rizzo 				netmap_sync_from_host(na, NULL);
654506cc70cSLuigi Rizzo 			break;
65568b8534bSLuigi Rizzo 		}
65664ae02c3SLuigi Rizzo 		/* find the last ring to scan */
65764ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
65864ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
6593c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
660*d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
66168b8534bSLuigi Rizzo 
66264ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
66368b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
66468b8534bSLuigi Rizzo 				struct netmap_kring *kring = &na->tx_rings[i];
66568b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
6663c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
66768b8534bSLuigi Rizzo 					    i, kring->ring->cur,
66868b8534bSLuigi Rizzo 					    kring->nr_hwcur);
6691a26580eSLuigi Rizzo 				na->nm_txsync(ifp, i, 1 /* do lock */);
67068b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
6713c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
67268b8534bSLuigi Rizzo 					    i, kring->ring->cur,
67368b8534bSLuigi Rizzo 					    kring->nr_hwcur);
67468b8534bSLuigi Rizzo 			} else {
6751a26580eSLuigi Rizzo 				na->nm_rxsync(ifp, i, 1 /* do lock */);
67668b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
67768b8534bSLuigi Rizzo 			}
67868b8534bSLuigi Rizzo 		}
67968b8534bSLuigi Rizzo 
68068b8534bSLuigi Rizzo 		break;
68168b8534bSLuigi Rizzo 
68268b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
68368b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
68468b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
68568b8534bSLuigi Rizzo 	case BIOCSSEESENT:
68668b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
68768b8534bSLuigi Rizzo 		break;
68868b8534bSLuigi Rizzo 
689babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
69068b8534bSLuigi Rizzo 	    {
69168b8534bSLuigi Rizzo 		struct socket so;
69268b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
69368b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
69468b8534bSLuigi Rizzo 		if (error)
69568b8534bSLuigi Rizzo 			break;
69668b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
69768b8534bSLuigi Rizzo 		// so->so_proto not null.
69868b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
69968b8534bSLuigi Rizzo 		if_rele(ifp);
700babc7c12SLuigi Rizzo 		break;
70168b8534bSLuigi Rizzo 	    }
70268b8534bSLuigi Rizzo 	}
70368b8534bSLuigi Rizzo 
704506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
70568b8534bSLuigi Rizzo 	return (error);
70668b8534bSLuigi Rizzo }
70768b8534bSLuigi Rizzo 
70868b8534bSLuigi Rizzo 
70968b8534bSLuigi Rizzo /*
71068b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
71168b8534bSLuigi Rizzo  *
71268b8534bSLuigi Rizzo  * Can be called for one or more queues.
71368b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
71468b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
71568b8534bSLuigi Rizzo  * selfd or on the global one.
71668b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
71768b8534bSLuigi Rizzo  * are done through callbacks.
71868b8534bSLuigi Rizzo  */
71968b8534bSLuigi Rizzo static int
72068b8534bSLuigi Rizzo netmap_poll(__unused struct cdev *dev, int events, struct thread *td)
72168b8534bSLuigi Rizzo {
72268b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
72368b8534bSLuigi Rizzo 	struct netmap_adapter *na;
72468b8534bSLuigi Rizzo 	struct ifnet *ifp;
72568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
726d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
72764ae02c3SLuigi Rizzo 	u_int lim_tx, lim_rx;
728bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
72968b8534bSLuigi Rizzo 
73068b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
73168b8534bSLuigi Rizzo 		return POLLERR;
73268b8534bSLuigi Rizzo 
73368b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
73468b8534bSLuigi Rizzo 	// XXX check for deleting() ?
73568b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
73668b8534bSLuigi Rizzo 		return POLLERR;
73768b8534bSLuigi Rizzo 
73868b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
73968b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
74068b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
74168b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
74268b8534bSLuigi Rizzo 
74368b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
74468b8534bSLuigi Rizzo 
745*d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
746*d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
74768b8534bSLuigi Rizzo 	/* how many queues we are scanning */
74864ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
74968b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
75068b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
75164ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
75268b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
75368b8534bSLuigi Rizzo 			revents |= want_tx;
75468b8534bSLuigi Rizzo 		}
75568b8534bSLuigi Rizzo 		if (want_rx) {
75664ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
75768b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
75868b8534bSLuigi Rizzo 				netmap_sync_from_host(na, td);
75968b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
76068b8534bSLuigi Rizzo 				revents |= want_rx;
76168b8534bSLuigi Rizzo 			}
76268b8534bSLuigi Rizzo 		}
76368b8534bSLuigi Rizzo 		return (revents);
76468b8534bSLuigi Rizzo 	}
76568b8534bSLuigi Rizzo 
76668b8534bSLuigi Rizzo 	/*
76768b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
76868b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
76968b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
77068b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
77168b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
77268b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
77368b8534bSLuigi Rizzo 	 * has more than one ring.
77468b8534bSLuigi Rizzo 	 *
77568b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
77668b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
77768b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
77868b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
77968b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
78068b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
78168b8534bSLuigi Rizzo 	 *
78268b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
78368b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
78468b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
78568b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
78668b8534bSLuigi Rizzo 	 */
78764ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
78868b8534bSLuigi Rizzo 
78968b8534bSLuigi Rizzo 	/*
79068b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
79168b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
79268b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
79368b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
79468b8534bSLuigi Rizzo 	 *
79568b8534bSLuigi Rizzo 	 * It has three possible states:
79668b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
79768b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
79868b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
79968b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
80068b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
80168b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
80268b8534bSLuigi Rizzo 	 */
803d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
80464ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
80564ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
80664ae02c3SLuigi Rizzo 	}
80764ae02c3SLuigi Rizzo 
80868b8534bSLuigi Rizzo 	/*
80968b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
81068b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
81168b8534bSLuigi Rizzo 	 * routines.
81268b8534bSLuigi Rizzo 	 */
81364ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
81468b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
81568b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
81668b8534bSLuigi Rizzo 			revents |= want_rx;
81768b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
81868b8534bSLuigi Rizzo 		}
81968b8534bSLuigi Rizzo 	}
82064ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
82168b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
82268b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
82368b8534bSLuigi Rizzo 			revents |= want_tx;
82468b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
82568b8534bSLuigi Rizzo 		}
82668b8534bSLuigi Rizzo 	}
82768b8534bSLuigi Rizzo 
82868b8534bSLuigi Rizzo 	/*
82968b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
83068b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
83168b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
83268b8534bSLuigi Rizzo 	 */
83368b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
83464ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
83568b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
8365819da83SLuigi Rizzo 			/*
8375819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
8385819da83SLuigi Rizzo 			 * (we have already done a successful sync on
8395819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
8405819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
8415819da83SLuigi Rizzo 			 */
84268b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
84368b8534bSLuigi Rizzo 				continue;
84468b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
8451a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
84668b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
84768b8534bSLuigi Rizzo 			}
84868b8534bSLuigi Rizzo 			if (na->separate_locks)
8491a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
85068b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
85168b8534bSLuigi Rizzo 				D("send %d on %s %d",
85268b8534bSLuigi Rizzo 					kring->ring->cur,
85368b8534bSLuigi Rizzo 					ifp->if_xname, i);
8541a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
85568b8534bSLuigi Rizzo 				revents |= POLLERR;
85668b8534bSLuigi Rizzo 
8575819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
85868b8534bSLuigi Rizzo 			if (want_tx) {
85968b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
86068b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
86168b8534bSLuigi Rizzo 					 * starvation.
86268b8534bSLuigi Rizzo 					 */
86368b8534bSLuigi Rizzo 					revents |= want_tx;
86468b8534bSLuigi Rizzo 					want_tx = 0;
86568b8534bSLuigi Rizzo 				} else if (!check_all)
86668b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
86768b8534bSLuigi Rizzo 			}
86868b8534bSLuigi Rizzo 			if (na->separate_locks)
8691a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
87068b8534bSLuigi Rizzo 		}
87168b8534bSLuigi Rizzo 	}
87268b8534bSLuigi Rizzo 
87368b8534bSLuigi Rizzo 	/*
87468b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
87568b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
87668b8534bSLuigi Rizzo 	 */
87768b8534bSLuigi Rizzo 	if (want_rx) {
87864ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
87968b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
88068b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
8811a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
88268b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
88368b8534bSLuigi Rizzo 			}
88468b8534bSLuigi Rizzo 			if (na->separate_locks)
8851a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
88668b8534bSLuigi Rizzo 
8871a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
88868b8534bSLuigi Rizzo 				revents |= POLLERR;
8895819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
8905819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
89168b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
8925819da83SLuigi Rizzo 			}
89368b8534bSLuigi Rizzo 
89468b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
89568b8534bSLuigi Rizzo 				revents |= want_rx;
89668b8534bSLuigi Rizzo 			else if (!check_all)
89768b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
89868b8534bSLuigi Rizzo 			if (na->separate_locks)
8991a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
90068b8534bSLuigi Rizzo 		}
90168b8534bSLuigi Rizzo 	}
90264ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
90368b8534bSLuigi Rizzo 		if (want_tx)
90464ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
90568b8534bSLuigi Rizzo 		if (want_rx)
90664ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
90768b8534bSLuigi Rizzo 	}
90868b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
9091a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
91068b8534bSLuigi Rizzo 
91168b8534bSLuigi Rizzo 	return (revents);
91268b8534bSLuigi Rizzo }
91368b8534bSLuigi Rizzo 
91468b8534bSLuigi Rizzo /*------- driver support routines ------*/
91568b8534bSLuigi Rizzo 
91668b8534bSLuigi Rizzo /*
917babc7c12SLuigi Rizzo  * default lock wrapper.
9181a26580eSLuigi Rizzo  */
9191a26580eSLuigi Rizzo static void
920babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
9211a26580eSLuigi Rizzo {
922babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
9231a26580eSLuigi Rizzo 
9241a26580eSLuigi Rizzo 	switch (what) {
925babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
9261a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
9271a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
9281a26580eSLuigi Rizzo 		break;
929babc7c12SLuigi Rizzo #endif /* linux */
9301a26580eSLuigi Rizzo 
9311a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
9321a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
9331a26580eSLuigi Rizzo 		break;
9341a26580eSLuigi Rizzo 
9351a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
9361a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
9371a26580eSLuigi Rizzo 		break;
9381a26580eSLuigi Rizzo 
9391a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
9401a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
9411a26580eSLuigi Rizzo 		break;
9421a26580eSLuigi Rizzo 
9431a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
9441a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
9451a26580eSLuigi Rizzo 		break;
9461a26580eSLuigi Rizzo 
9471a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
9481a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
9491a26580eSLuigi Rizzo 		break;
9501a26580eSLuigi Rizzo 
9511a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
9521a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
9531a26580eSLuigi Rizzo 		break;
9541a26580eSLuigi Rizzo 	}
9551a26580eSLuigi Rizzo }
9561a26580eSLuigi Rizzo 
9571a26580eSLuigi Rizzo 
9581a26580eSLuigi Rizzo /*
95968b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
96068b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
96168b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
96268b8534bSLuigi Rizzo  * of hardware rings):
96368b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
96468b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
96568b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
96668b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
96764ae02c3SLuigi Rizzo  *
968*d76bf4ffSLuigi Rizzo  * na->num_tx_rings can be set for cards with different tx/rx setups
96968b8534bSLuigi Rizzo  */
97068b8534bSLuigi Rizzo int
97168b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
97268b8534bSLuigi Rizzo {
97364ae02c3SLuigi Rizzo 	int i, n, size;
97468b8534bSLuigi Rizzo 	void *buf;
97568b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
97668b8534bSLuigi Rizzo 
97768b8534bSLuigi Rizzo 	if (ifp == NULL) {
97868b8534bSLuigi Rizzo 		D("ifp not set, giving up");
97968b8534bSLuigi Rizzo 		return EINVAL;
98068b8534bSLuigi Rizzo 	}
98164ae02c3SLuigi Rizzo 	/* clear other fields ? */
98268b8534bSLuigi Rizzo 	na->refcount = 0;
983*d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings == 0)
984*d76bf4ffSLuigi Rizzo 		na->num_tx_rings = num_queues;
985*d76bf4ffSLuigi Rizzo 	na->num_rx_rings = num_queues;
98664ae02c3SLuigi Rizzo 	/* on each direction we have N+1 resources
98764ae02c3SLuigi Rizzo 	 * 0..n-1	are the hardware rings
98864ae02c3SLuigi Rizzo 	 * n		is the ring attached to the stack.
98964ae02c3SLuigi Rizzo 	 */
990*d76bf4ffSLuigi Rizzo 	n = na->num_rx_rings + na->num_tx_rings + 2;
99164ae02c3SLuigi Rizzo 	size = sizeof(*na) + n * sizeof(struct netmap_kring);
99268b8534bSLuigi Rizzo 
99368b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
99468b8534bSLuigi Rizzo 	if (buf) {
995d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
99668b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
997*d76bf4ffSLuigi Rizzo 		na->rx_rings = na->tx_rings + na->num_tx_rings + 1;
99868b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
99968b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
10001a26580eSLuigi Rizzo 
10011a26580eSLuigi Rizzo 		na = buf;
10021a26580eSLuigi Rizzo 		if (na->nm_lock == NULL)
10031a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
10041a26580eSLuigi Rizzo 		mtx_init(&na->core_lock, "netmap core lock", NULL, MTX_DEF);
1005*d76bf4ffSLuigi Rizzo 		for (i = 0 ; i < na->num_tx_rings + 1; i++)
10061a26580eSLuigi Rizzo 			mtx_init(&na->tx_rings[i].q_lock, "netmap txq lock", NULL, MTX_DEF);
1007*d76bf4ffSLuigi Rizzo 		for (i = 0 ; i < na->num_rx_rings + 1; i++)
10081a26580eSLuigi Rizzo 			mtx_init(&na->rx_rings[i].q_lock, "netmap rxq lock", NULL, MTX_DEF);
100968b8534bSLuigi Rizzo 	}
101064ae02c3SLuigi Rizzo #ifdef linux
101164ae02c3SLuigi Rizzo 	D("netdev_ops %p", ifp->netdev_ops);
101264ae02c3SLuigi Rizzo 	/* prepare a clone of the netdev ops */
101364ae02c3SLuigi Rizzo 	na->nm_ndo = *ifp->netdev_ops;
101464ae02c3SLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = netmap_start_linux;
101564ae02c3SLuigi Rizzo #endif
101668b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
101768b8534bSLuigi Rizzo 
101868b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
101968b8534bSLuigi Rizzo }
102068b8534bSLuigi Rizzo 
102168b8534bSLuigi Rizzo 
102268b8534bSLuigi Rizzo /*
102368b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
102468b8534bSLuigi Rizzo  * object.
102568b8534bSLuigi Rizzo  */
102668b8534bSLuigi Rizzo void
102768b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
102868b8534bSLuigi Rizzo {
102968b8534bSLuigi Rizzo 	u_int i;
103068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
103168b8534bSLuigi Rizzo 
103268b8534bSLuigi Rizzo 	if (!na)
103368b8534bSLuigi Rizzo 		return;
103468b8534bSLuigi Rizzo 
1035*d76bf4ffSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
103668b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
103764ae02c3SLuigi Rizzo 		mtx_destroy(&na->tx_rings[i].q_lock);
103868b8534bSLuigi Rizzo 	}
1039*d76bf4ffSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
104064ae02c3SLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
104164ae02c3SLuigi Rizzo 		mtx_destroy(&na->rx_rings[i].q_lock);
104264ae02c3SLuigi Rizzo 	}
104364ae02c3SLuigi Rizzo 	knlist_destroy(&na->tx_si.si_note);
104464ae02c3SLuigi Rizzo 	knlist_destroy(&na->rx_si.si_note);
104568b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1046d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
104768b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
104868b8534bSLuigi Rizzo }
104968b8534bSLuigi Rizzo 
105068b8534bSLuigi Rizzo 
105168b8534bSLuigi Rizzo /*
105202ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
105302ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
105468b8534bSLuigi Rizzo  * We are not locked when called.
105568b8534bSLuigi Rizzo  */
105668b8534bSLuigi Rizzo int
105768b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
105868b8534bSLuigi Rizzo {
105968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
1060*d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
10611a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
106202ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
106368b8534bSLuigi Rizzo 	struct netmap_slot *slot;
106468b8534bSLuigi Rizzo 
106568b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
106668b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
106768b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
10681a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
106902ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
107068b8534bSLuigi Rizzo 		D("stack ring %s full\n", ifp->if_xname);
107168b8534bSLuigi Rizzo 		goto done;	/* no space */
107268b8534bSLuigi Rizzo 	}
107364ae02c3SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) {
107464ae02c3SLuigi Rizzo 		D("drop packet size %d > %d", len, NETMAP_BUF_SIZE);
107568b8534bSLuigi Rizzo 		goto done;	/* too long for us */
107668b8534bSLuigi Rizzo 	}
107768b8534bSLuigi Rizzo 
107868b8534bSLuigi Rizzo 	/* compute the insert position */
107968b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
108002ad4083SLuigi Rizzo 	if (i > lim)
108102ad4083SLuigi Rizzo 		i -= lim + 1;
108268b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
108368b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
108468b8534bSLuigi Rizzo 	slot->len = len;
108568b8534bSLuigi Rizzo 	kring->nr_hwavail++;
108668b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
1087*d76bf4ffSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_rings);
108868b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
108968b8534bSLuigi Rizzo 	error = 0;
109068b8534bSLuigi Rizzo done:
10911a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
109268b8534bSLuigi Rizzo 
109368b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
109468b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
109568b8534bSLuigi Rizzo 	 * only when really necessary.
109668b8534bSLuigi Rizzo 	 */
109768b8534bSLuigi Rizzo 	m_freem(m);
109868b8534bSLuigi Rizzo 
109968b8534bSLuigi Rizzo 	return (error);
110068b8534bSLuigi Rizzo }
110168b8534bSLuigi Rizzo 
110268b8534bSLuigi Rizzo 
110368b8534bSLuigi Rizzo /*
110468b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
110568b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
110668b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
110768b8534bSLuigi Rizzo  */
110868b8534bSLuigi Rizzo struct netmap_slot *
110968b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
111068b8534bSLuigi Rizzo 	u_int new_cur)
111168b8534bSLuigi Rizzo {
111268b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1113506cc70cSLuigi Rizzo 	int new_hwofs, lim;
111468b8534bSLuigi Rizzo 
111568b8534bSLuigi Rizzo 	if (na == NULL)
111668b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
111768b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
111868b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
111968b8534bSLuigi Rizzo 
112064ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
112164ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1122506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
112364ae02c3SLuigi Rizzo 	} else {
112464ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1125506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
112664ae02c3SLuigi Rizzo 	}
112764ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1128506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1129506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1130506cc70cSLuigi Rizzo 
1131506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1132506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1133506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1134506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1135506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1136506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1137506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1138506cc70cSLuigi Rizzo 
113968b8534bSLuigi Rizzo 	/*
114064ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1141506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1142506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
114368b8534bSLuigi Rizzo 	 */
114468b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
114564ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
114668b8534bSLuigi Rizzo 	return kring->ring->slot;
114768b8534bSLuigi Rizzo }
114868b8534bSLuigi Rizzo 
114968b8534bSLuigi Rizzo 
115068b8534bSLuigi Rizzo /*
11511a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
11521a26580eSLuigi Rizzo  * we have 4 cases:
11531a26580eSLuigi Rizzo  * 1 ring, single lock:
11541a26580eSLuigi Rizzo  *	lock(core); wake(i=0); unlock(core)
11551a26580eSLuigi Rizzo  * N rings, single lock:
11561a26580eSLuigi Rizzo  *	lock(core); wake(i); wake(N+1) unlock(core)
11571a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
11581a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i)
11591a26580eSLuigi Rizzo  * N rings, separate locks:
11601a26580eSLuigi Rizzo  *	lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
116164ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
11621a26580eSLuigi Rizzo  */
1163babc7c12SLuigi Rizzo int
1164babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
11651a26580eSLuigi Rizzo {
11661a26580eSLuigi Rizzo 	struct netmap_adapter *na;
11671a26580eSLuigi Rizzo 	struct netmap_kring *r;
116864ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
11691a26580eSLuigi Rizzo 
11701a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
11711a26580eSLuigi Rizzo 		return 0;
11721a26580eSLuigi Rizzo 	na = NA(ifp);
117364ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
117464ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
117564ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
1176*d76bf4ffSLuigi Rizzo 		main_wq = (na->num_rx_rings > 1) ? &na->rx_si : NULL;
117764ae02c3SLuigi Rizzo 	} else { /* tx path */
117864ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
1179*d76bf4ffSLuigi Rizzo 		main_wq = (na->num_tx_rings > 1) ? &na->tx_si : NULL;
118064ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
118164ae02c3SLuigi Rizzo 	}
11821a26580eSLuigi Rizzo 	if (na->separate_locks) {
118364ae02c3SLuigi Rizzo 		mtx_lock(&r->q_lock);
118464ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
118564ae02c3SLuigi Rizzo 		mtx_unlock(&r->q_lock);
118664ae02c3SLuigi Rizzo 		if (main_wq) {
11871a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
118864ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
11891a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
11901a26580eSLuigi Rizzo 		}
11911a26580eSLuigi Rizzo 	} else {
11921a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
119364ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
119464ae02c3SLuigi Rizzo 		if (main_wq)
119564ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
11961a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
11971a26580eSLuigi Rizzo 	}
11981a26580eSLuigi Rizzo 	*work_done = 1; /* do not fire napi again */
11991a26580eSLuigi Rizzo 	return 1;
12001a26580eSLuigi Rizzo }
12011a26580eSLuigi Rizzo 
120264ae02c3SLuigi Rizzo 
1203babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
1204babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
1205babc7c12SLuigi Rizzo 	.d_name = "netmap",
1206babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
1207babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
1208babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
1209babc7c12SLuigi Rizzo };
1210babc7c12SLuigi Rizzo 
1211babc7c12SLuigi Rizzo 
1212babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
1213babc7c12SLuigi Rizzo 
1214babc7c12SLuigi Rizzo 
12151a26580eSLuigi Rizzo /*
121668b8534bSLuigi Rizzo  * Module loader.
121768b8534bSLuigi Rizzo  *
121868b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
121968b8534bSLuigi Rizzo  * variables.
122068b8534bSLuigi Rizzo  *
122168b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
122268b8534bSLuigi Rizzo  */
122368b8534bSLuigi Rizzo static int
122468b8534bSLuigi Rizzo netmap_init(void)
122568b8534bSLuigi Rizzo {
122668b8534bSLuigi Rizzo 	int error;
122768b8534bSLuigi Rizzo 
122868b8534bSLuigi Rizzo 	error = netmap_memory_init();
122968b8534bSLuigi Rizzo 	if (error != 0) {
123068b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
123168b8534bSLuigi Rizzo 		return (error);
123268b8534bSLuigi Rizzo 	}
123368b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
123464ae02c3SLuigi Rizzo 		(int)(nm_mem->nm_totalsize >> 20));
123568b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
123668b8534bSLuigi Rizzo 			      "netmap");
1237babc7c12SLuigi Rizzo 	return (error);
123868b8534bSLuigi Rizzo }
123968b8534bSLuigi Rizzo 
124068b8534bSLuigi Rizzo 
124168b8534bSLuigi Rizzo /*
124268b8534bSLuigi Rizzo  * Module unloader.
124368b8534bSLuigi Rizzo  *
124468b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
124568b8534bSLuigi Rizzo  */
124668b8534bSLuigi Rizzo static void
124768b8534bSLuigi Rizzo netmap_fini(void)
124868b8534bSLuigi Rizzo {
124968b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
125068b8534bSLuigi Rizzo 	netmap_memory_fini();
125168b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
125268b8534bSLuigi Rizzo }
125368b8534bSLuigi Rizzo 
125468b8534bSLuigi Rizzo 
125568b8534bSLuigi Rizzo /*
125668b8534bSLuigi Rizzo  * Kernel entry point.
125768b8534bSLuigi Rizzo  *
125868b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
125968b8534bSLuigi Rizzo  *
126068b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
126168b8534bSLuigi Rizzo  */
126268b8534bSLuigi Rizzo static int
126368b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
126468b8534bSLuigi Rizzo {
126568b8534bSLuigi Rizzo 	int error = 0;
126668b8534bSLuigi Rizzo 
126768b8534bSLuigi Rizzo 	switch (event) {
126868b8534bSLuigi Rizzo 	case MOD_LOAD:
126968b8534bSLuigi Rizzo 		error = netmap_init();
127068b8534bSLuigi Rizzo 		break;
127168b8534bSLuigi Rizzo 
127268b8534bSLuigi Rizzo 	case MOD_UNLOAD:
127368b8534bSLuigi Rizzo 		netmap_fini();
127468b8534bSLuigi Rizzo 		break;
127568b8534bSLuigi Rizzo 
127668b8534bSLuigi Rizzo 	default:
127768b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
127868b8534bSLuigi Rizzo 		break;
127968b8534bSLuigi Rizzo 	}
128068b8534bSLuigi Rizzo 
128168b8534bSLuigi Rizzo 	return (error);
128268b8534bSLuigi Rizzo }
128368b8534bSLuigi Rizzo 
128468b8534bSLuigi Rizzo 
128568b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
1286