xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision 1a26580e)
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.c 9795 2011-12-02 11:39:08Z luigi $
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3168b8534bSLuigi Rizzo  * see netmap(4).
3268b8534bSLuigi Rizzo  *
3368b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3468b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3568b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3668b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3768b8534bSLuigi Rizzo  *
3868b8534bSLuigi Rizzo  * Access to the network card works like this:
3968b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
4068b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4168b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4268b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4368b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4468b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4568b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4668b8534bSLuigi Rizzo  *    the shared memory region.
4768b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4868b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4968b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
5068b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5168b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5268b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5368b8534bSLuigi Rizzo  *    packets on the output interface.
5468b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5568b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
5668b8534bSLuigi Rizzo  */
5768b8534bSLuigi Rizzo 
5868b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
5968b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
6068b8534bSLuigi Rizzo 
6168b8534bSLuigi Rizzo #include <sys/types.h>
6268b8534bSLuigi Rizzo #include <sys/module.h>
6368b8534bSLuigi Rizzo #include <sys/errno.h>
6468b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
65506cc70cSLuigi Rizzo #include <sys/jail.h>
6668b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
6768b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
6868b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
6968b8534bSLuigi Rizzo #include <sys/sockio.h>
7068b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
7168b8534bSLuigi Rizzo #include <sys/malloc.h>
7268b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
7368b8534bSLuigi Rizzo #include <sys/poll.h>
74506cc70cSLuigi Rizzo #include <sys/proc.h>
7568b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
7668b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
7768b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
7868b8534bSLuigi Rizzo #include <machine/bus.h>
7968b8534bSLuigi Rizzo #include <sys/selinfo.h>
8068b8534bSLuigi Rizzo #include <sys/sysctl.h>
8168b8534bSLuigi Rizzo #include <net/if.h>
8268b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
83506cc70cSLuigi Rizzo #include <net/vnet.h>
8468b8534bSLuigi Rizzo #include <net/netmap.h>
8568b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
8668b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
8768b8534bSLuigi Rizzo 
8868b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
8968b8534bSLuigi Rizzo 
9068b8534bSLuigi Rizzo /*
9168b8534bSLuigi Rizzo  * lock and unlock for the netmap memory allocator
9268b8534bSLuigi Rizzo  */
9368b8534bSLuigi Rizzo #define NMA_LOCK()	mtx_lock(&netmap_mem_d->nm_mtx);
9468b8534bSLuigi Rizzo #define NMA_UNLOCK()	mtx_unlock(&netmap_mem_d->nm_mtx);
955819da83SLuigi Rizzo struct netmap_mem_d;
965819da83SLuigi Rizzo static struct netmap_mem_d *netmap_mem_d;	/* Our memory allocator. */
975819da83SLuigi Rizzo 
985819da83SLuigi Rizzo u_int netmap_total_buffers;
995819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
1005819da83SLuigi Rizzo 
1015819da83SLuigi Rizzo /* user-controlled variables */
1025819da83SLuigi Rizzo int netmap_verbose;
1035819da83SLuigi Rizzo 
1045819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1055819da83SLuigi Rizzo 
1065819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1075819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1085819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1095819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1105819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1115819da83SLuigi Rizzo int netmap_buf_size = 2048;
1125819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size);
1135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
1145819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
1155819da83SLuigi Rizzo int netmap_mitigate = 1;
1165819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
1175819da83SLuigi Rizzo int netmap_no_pendintr;
1185819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1195819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1205819da83SLuigi Rizzo 
1215819da83SLuigi Rizzo 
1225819da83SLuigi Rizzo 
1235819da83SLuigi Rizzo /*----- memory allocator -----------------*/
1245819da83SLuigi Rizzo /*
1255819da83SLuigi Rizzo  * Here we have the low level routines for memory allocator
1265819da83SLuigi Rizzo  * and its primary users.
1275819da83SLuigi Rizzo  */
12868b8534bSLuigi Rizzo 
12968b8534bSLuigi Rizzo /*
13068b8534bSLuigi Rizzo  * Default amount of memory pre-allocated by the module.
13168b8534bSLuigi Rizzo  * We start with a large size and then shrink our demand
13268b8534bSLuigi Rizzo  * according to what is avalable when the module is loaded.
13368b8534bSLuigi Rizzo  * At the moment the block is contiguous, but we can easily
13468b8534bSLuigi Rizzo  * restrict our demand to smaller units (16..64k)
13568b8534bSLuigi Rizzo  */
13668b8534bSLuigi Rizzo #define NETMAP_MEMORY_SIZE (64 * 1024 * PAGE_SIZE)
13768b8534bSLuigi Rizzo static void * netmap_malloc(size_t size, const char *msg);
13868b8534bSLuigi Rizzo static void netmap_free(void *addr, const char *msg);
13968b8534bSLuigi Rizzo 
1406e10c8b8SLuigi Rizzo #define netmap_if_malloc(len)   netmap_malloc(len, "nifp")
1416e10c8b8SLuigi Rizzo #define netmap_if_free(v)	netmap_free((v), "nifp")
1426e10c8b8SLuigi Rizzo 
1436e10c8b8SLuigi Rizzo #define netmap_ring_malloc(len) netmap_malloc(len, "ring")
1446e10c8b8SLuigi Rizzo #define netmap_free_rings(na)		\
1456e10c8b8SLuigi Rizzo 	netmap_free((na)->tx_rings[0].ring, "shadow rings");
1466e10c8b8SLuigi Rizzo 
14768b8534bSLuigi Rizzo /*
14868b8534bSLuigi Rizzo  * Allocator for a pool of packet buffers. For each buffer we have
14968b8534bSLuigi Rizzo  * one entry in the bitmap to signal the state. Allocation scans
15068b8534bSLuigi Rizzo  * the bitmap, but since this is done only on attach, we are not
15168b8534bSLuigi Rizzo  * too worried about performance
15268b8534bSLuigi Rizzo  * XXX if we need to allocate small blocks, a translation
15368b8534bSLuigi Rizzo  * table is used both for kernel virtual address and physical
15468b8534bSLuigi Rizzo  * addresses.
15568b8534bSLuigi Rizzo  */
15668b8534bSLuigi Rizzo struct netmap_buf_pool {
15768b8534bSLuigi Rizzo 	u_int total_buffers;	/* total buffers. */
15868b8534bSLuigi Rizzo 	u_int free;
15968b8534bSLuigi Rizzo 	u_int bufsize;
16068b8534bSLuigi Rizzo 	char *base;		/* buffer base address */
16168b8534bSLuigi Rizzo 	uint32_t *bitmap;	/* one bit per buffer, 1 means free */
16268b8534bSLuigi Rizzo };
16368b8534bSLuigi Rizzo struct netmap_buf_pool nm_buf_pool;
16468b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, total_buffers,
16568b8534bSLuigi Rizzo     CTLFLAG_RD, &nm_buf_pool.total_buffers, 0, "total_buffers");
16668b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, free_buffers,
16768b8534bSLuigi Rizzo     CTLFLAG_RD, &nm_buf_pool.free, 0, "free_buffers");
1685819da83SLuigi Rizzo 
1695819da83SLuigi Rizzo 
1705819da83SLuigi Rizzo 
17168b8534bSLuigi Rizzo 
17268b8534bSLuigi Rizzo /*
17368b8534bSLuigi Rizzo  * Allocate n buffers from the ring, and fill the slot.
17468b8534bSLuigi Rizzo  * Buffer 0 is the 'junk' buffer.
17568b8534bSLuigi Rizzo  */
17668b8534bSLuigi Rizzo static void
177446ee301SLuigi Rizzo netmap_new_bufs(struct netmap_if *nifp __unused,
178446ee301SLuigi Rizzo 		struct netmap_slot *slot, u_int n)
17968b8534bSLuigi Rizzo {
180446ee301SLuigi Rizzo 	struct netmap_buf_pool *p = &nm_buf_pool;
18168b8534bSLuigi Rizzo 	uint32_t bi = 0;		/* index in the bitmap */
18268b8534bSLuigi Rizzo 	uint32_t mask, j, i = 0;	/* slot counter */
18368b8534bSLuigi Rizzo 
18468b8534bSLuigi Rizzo 	if (n > p->free) {
18568b8534bSLuigi Rizzo 		D("only %d out of %d buffers available", i, n);
18668b8534bSLuigi Rizzo 		return;
18768b8534bSLuigi Rizzo 	}
18868b8534bSLuigi Rizzo 	/* termination is guaranteed by p->free */
18968b8534bSLuigi Rizzo 	while (i < n && p->free > 0) {
19068b8534bSLuigi Rizzo 		uint32_t cur = p->bitmap[bi];
19168b8534bSLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
19268b8534bSLuigi Rizzo 			bi++;
19368b8534bSLuigi Rizzo 			continue;
19468b8534bSLuigi Rizzo 		}
19568b8534bSLuigi Rizzo 		/* locate a slot */
19668b8534bSLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) ;
19768b8534bSLuigi Rizzo 		p->bitmap[bi] &= ~mask;		/* slot in use */
19868b8534bSLuigi Rizzo 		p->free--;
19968b8534bSLuigi Rizzo 		slot[i].buf_idx = bi*32+j;
20068b8534bSLuigi Rizzo 		slot[i].len = p->bufsize;
20168b8534bSLuigi Rizzo 		slot[i].flags = NS_BUF_CHANGED;
20268b8534bSLuigi Rizzo 		i++;
20368b8534bSLuigi Rizzo 	}
20468b8534bSLuigi Rizzo 	ND("allocated %d buffers, %d available", n, p->free);
20568b8534bSLuigi Rizzo }
20668b8534bSLuigi Rizzo 
20768b8534bSLuigi Rizzo 
20868b8534bSLuigi Rizzo static void
209446ee301SLuigi Rizzo netmap_free_buf(struct netmap_if *nifp __unused, uint32_t i)
21068b8534bSLuigi Rizzo {
211446ee301SLuigi Rizzo 	struct netmap_buf_pool *p = &nm_buf_pool;
212446ee301SLuigi Rizzo 
21368b8534bSLuigi Rizzo 	uint32_t pos, mask;
21468b8534bSLuigi Rizzo 	if (i >= p->total_buffers) {
21568b8534bSLuigi Rizzo 		D("invalid free index %d", i);
21668b8534bSLuigi Rizzo 		return;
21768b8534bSLuigi Rizzo 	}
21868b8534bSLuigi Rizzo 	pos = i / 32;
21968b8534bSLuigi Rizzo 	mask = 1 << (i % 32);
22068b8534bSLuigi Rizzo 	if (p->bitmap[pos] & mask) {
22168b8534bSLuigi Rizzo 		D("slot %d already free", i);
22268b8534bSLuigi Rizzo 		return;
22368b8534bSLuigi Rizzo 	}
22468b8534bSLuigi Rizzo 	p->bitmap[pos] |= mask;
22568b8534bSLuigi Rizzo 	p->free++;
22668b8534bSLuigi Rizzo }
22768b8534bSLuigi Rizzo 
22868b8534bSLuigi Rizzo 
22968b8534bSLuigi Rizzo /* Descriptor of the memory objects handled by our memory allocator. */
23068b8534bSLuigi Rizzo struct netmap_mem_obj {
23168b8534bSLuigi Rizzo 	TAILQ_ENTRY(netmap_mem_obj) nmo_next; /* next object in the
23268b8534bSLuigi Rizzo 						 chain. */
23368b8534bSLuigi Rizzo 	int nmo_used; /* flag set on used memory objects. */
23468b8534bSLuigi Rizzo 	size_t nmo_size; /* size of the memory area reserved for the
23568b8534bSLuigi Rizzo 			    object. */
23668b8534bSLuigi Rizzo 	void *nmo_data; /* pointer to the memory area. */
23768b8534bSLuigi Rizzo };
23868b8534bSLuigi Rizzo 
23968b8534bSLuigi Rizzo /* Wrap our memory objects to make them ``chainable``. */
24068b8534bSLuigi Rizzo TAILQ_HEAD(netmap_mem_obj_h, netmap_mem_obj);
24168b8534bSLuigi Rizzo 
24268b8534bSLuigi Rizzo 
24368b8534bSLuigi Rizzo /* Descriptor of our custom memory allocator. */
24468b8534bSLuigi Rizzo struct netmap_mem_d {
24568b8534bSLuigi Rizzo 	struct mtx nm_mtx; /* lock used to handle the chain of memory
24668b8534bSLuigi Rizzo 			      objects. */
24768b8534bSLuigi Rizzo 	struct netmap_mem_obj_h nm_molist; /* list of memory objects */
24868b8534bSLuigi Rizzo 	size_t nm_size; /* total amount of memory used for rings etc. */
24968b8534bSLuigi Rizzo 	size_t nm_totalsize; /* total amount of allocated memory
25068b8534bSLuigi Rizzo 		(the difference is used for buffers) */
25168b8534bSLuigi Rizzo 	size_t nm_buf_start; /* offset of packet buffers.
25268b8534bSLuigi Rizzo 			This is page-aligned. */
25368b8534bSLuigi Rizzo 	size_t nm_buf_len; /* total memory for buffers */
25468b8534bSLuigi Rizzo 	void *nm_buffer; /* pointer to the whole pre-allocated memory
25568b8534bSLuigi Rizzo 			    area. */
25668b8534bSLuigi Rizzo };
25768b8534bSLuigi Rizzo 
2585819da83SLuigi Rizzo /* Shorthand to compute a netmap interface offset. */
2595819da83SLuigi Rizzo #define netmap_if_offset(v)                                     \
2605819da83SLuigi Rizzo     ((char *) (v) - (char *) netmap_mem_d->nm_buffer)
2615819da83SLuigi Rizzo /* .. and get a physical address given a memory offset */
2625819da83SLuigi Rizzo #define netmap_ofstophys(o)                                     \
2635819da83SLuigi Rizzo     (vtophys(netmap_mem_d->nm_buffer) + (o))
2645819da83SLuigi Rizzo 
2655819da83SLuigi Rizzo 
2665819da83SLuigi Rizzo /*------ netmap memory allocator -------*/
2675819da83SLuigi Rizzo /*
2685819da83SLuigi Rizzo  * Request for a chunk of memory.
2695819da83SLuigi Rizzo  *
2705819da83SLuigi Rizzo  * Memory objects are arranged into a list, hence we need to walk this
2715819da83SLuigi Rizzo  * list until we find an object with the needed amount of data free.
2725819da83SLuigi Rizzo  * This sounds like a completely inefficient implementation, but given
2735819da83SLuigi Rizzo  * the fact that data allocation is done once, we can handle it
2745819da83SLuigi Rizzo  * flawlessly.
2755819da83SLuigi Rizzo  *
2765819da83SLuigi Rizzo  * Return NULL on failure.
2775819da83SLuigi Rizzo  */
2785819da83SLuigi Rizzo static void *
2795819da83SLuigi Rizzo netmap_malloc(size_t size, __unused const char *msg)
2805819da83SLuigi Rizzo {
2815819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj, *new_mem_obj;
2825819da83SLuigi Rizzo 	void *ret = NULL;
2835819da83SLuigi Rizzo 
2845819da83SLuigi Rizzo 	NMA_LOCK();
2855819da83SLuigi Rizzo 	TAILQ_FOREACH(mem_obj, &netmap_mem_d->nm_molist, nmo_next) {
2865819da83SLuigi Rizzo 		if (mem_obj->nmo_used != 0 || mem_obj->nmo_size < size)
2875819da83SLuigi Rizzo 			continue;
2885819da83SLuigi Rizzo 
2895819da83SLuigi Rizzo 		new_mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
2905819da83SLuigi Rizzo 				     M_WAITOK | M_ZERO);
2915819da83SLuigi Rizzo 		TAILQ_INSERT_BEFORE(mem_obj, new_mem_obj, nmo_next);
2925819da83SLuigi Rizzo 
2935819da83SLuigi Rizzo 		new_mem_obj->nmo_used = 1;
2945819da83SLuigi Rizzo 		new_mem_obj->nmo_size = size;
2955819da83SLuigi Rizzo 		new_mem_obj->nmo_data = mem_obj->nmo_data;
2965819da83SLuigi Rizzo 		memset(new_mem_obj->nmo_data, 0, new_mem_obj->nmo_size);
2975819da83SLuigi Rizzo 
2985819da83SLuigi Rizzo 		mem_obj->nmo_size -= size;
2995819da83SLuigi Rizzo 		mem_obj->nmo_data = (char *) mem_obj->nmo_data + size;
3005819da83SLuigi Rizzo 		if (mem_obj->nmo_size == 0) {
3015819da83SLuigi Rizzo 			TAILQ_REMOVE(&netmap_mem_d->nm_molist, mem_obj,
3025819da83SLuigi Rizzo 				     nmo_next);
3035819da83SLuigi Rizzo 			free(mem_obj, M_NETMAP);
3045819da83SLuigi Rizzo 		}
3055819da83SLuigi Rizzo 
3065819da83SLuigi Rizzo 		ret = new_mem_obj->nmo_data;
3075819da83SLuigi Rizzo 
3085819da83SLuigi Rizzo 		break;
3095819da83SLuigi Rizzo 	}
3105819da83SLuigi Rizzo 	NMA_UNLOCK();
3115819da83SLuigi Rizzo 	ND("%s: %d bytes at %p", msg, size, ret);
3125819da83SLuigi Rizzo 
3135819da83SLuigi Rizzo 	return (ret);
3145819da83SLuigi Rizzo }
3155819da83SLuigi Rizzo 
3165819da83SLuigi Rizzo /*
3175819da83SLuigi Rizzo  * Return the memory to the allocator.
3185819da83SLuigi Rizzo  *
3195819da83SLuigi Rizzo  * While freeing a memory object, we try to merge adjacent chunks in
3205819da83SLuigi Rizzo  * order to reduce memory fragmentation.
3215819da83SLuigi Rizzo  */
3225819da83SLuigi Rizzo static void
3235819da83SLuigi Rizzo netmap_free(void *addr, const char *msg)
3245819da83SLuigi Rizzo {
3255819da83SLuigi Rizzo 	size_t size;
3265819da83SLuigi Rizzo 	struct netmap_mem_obj *cur, *prev, *next;
3275819da83SLuigi Rizzo 
3285819da83SLuigi Rizzo 	if (addr == NULL) {
3295819da83SLuigi Rizzo 		D("NULL addr for %s", msg);
3305819da83SLuigi Rizzo 		return;
3315819da83SLuigi Rizzo 	}
3325819da83SLuigi Rizzo 
3335819da83SLuigi Rizzo 	NMA_LOCK();
3345819da83SLuigi Rizzo 	TAILQ_FOREACH(cur, &netmap_mem_d->nm_molist, nmo_next) {
3355819da83SLuigi Rizzo 		if (cur->nmo_data == addr && cur->nmo_used)
3365819da83SLuigi Rizzo 			break;
3375819da83SLuigi Rizzo 	}
3385819da83SLuigi Rizzo 	if (cur == NULL) {
3395819da83SLuigi Rizzo 		NMA_UNLOCK();
3405819da83SLuigi Rizzo 		D("invalid addr %s %p", msg, addr);
3415819da83SLuigi Rizzo 		return;
3425819da83SLuigi Rizzo 	}
3435819da83SLuigi Rizzo 
3445819da83SLuigi Rizzo 	size = cur->nmo_size;
3455819da83SLuigi Rizzo 	cur->nmo_used = 0;
3465819da83SLuigi Rizzo 
3475819da83SLuigi Rizzo 	/* merge current chunk of memory with the previous one,
3485819da83SLuigi Rizzo 	   if present. */
3495819da83SLuigi Rizzo 	prev = TAILQ_PREV(cur, netmap_mem_obj_h, nmo_next);
3505819da83SLuigi Rizzo 	if (prev && prev->nmo_used == 0) {
3515819da83SLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, cur, nmo_next);
3525819da83SLuigi Rizzo 		prev->nmo_size += cur->nmo_size;
3535819da83SLuigi Rizzo 		free(cur, M_NETMAP);
3545819da83SLuigi Rizzo 		cur = prev;
3555819da83SLuigi Rizzo 	}
3565819da83SLuigi Rizzo 
3575819da83SLuigi Rizzo 	/* merge with the next one */
3585819da83SLuigi Rizzo 	next = TAILQ_NEXT(cur, nmo_next);
3595819da83SLuigi Rizzo 	if (next && next->nmo_used == 0) {
3605819da83SLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, next, nmo_next);
3615819da83SLuigi Rizzo 		cur->nmo_size += next->nmo_size;
3625819da83SLuigi Rizzo 		free(next, M_NETMAP);
3635819da83SLuigi Rizzo 	}
3645819da83SLuigi Rizzo 	NMA_UNLOCK();
3655819da83SLuigi Rizzo 	ND("freed %s %d bytes at %p", msg, size, addr);
3665819da83SLuigi Rizzo }
3675819da83SLuigi Rizzo 
3685819da83SLuigi Rizzo 
3695819da83SLuigi Rizzo /*
3705819da83SLuigi Rizzo  * Create and return a new ``netmap_if`` object, and possibly also
3715819da83SLuigi Rizzo  * rings and packet buffors.
3725819da83SLuigi Rizzo  *
3735819da83SLuigi Rizzo  * Return NULL on failure.
3745819da83SLuigi Rizzo  */
3755819da83SLuigi Rizzo static void *
3765819da83SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
3775819da83SLuigi Rizzo {
3785819da83SLuigi Rizzo 	struct netmap_if *nifp;
3795819da83SLuigi Rizzo 	struct netmap_ring *ring;
3805819da83SLuigi Rizzo 	char *buff;
3815819da83SLuigi Rizzo 	u_int i, len, ofs;
3825819da83SLuigi Rizzo 	u_int n = na->num_queues + 1; /* shorthand, include stack queue */
3835819da83SLuigi Rizzo 
3845819da83SLuigi Rizzo 	/*
3855819da83SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
3865819da83SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
3875819da83SLuigi Rizzo 	 */
3885819da83SLuigi Rizzo 	len = sizeof(struct netmap_if) + 2 * n * sizeof(ssize_t);
3895819da83SLuigi Rizzo 	nifp = netmap_if_malloc(len);
3905819da83SLuigi Rizzo 	if (nifp == NULL)
3915819da83SLuigi Rizzo 		return (NULL);
3925819da83SLuigi Rizzo 
3935819da83SLuigi Rizzo 	/* initialize base fields */
3945819da83SLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_num_queues = na->num_queues;
3955819da83SLuigi Rizzo 	strncpy(nifp->ni_name, ifname, IFNAMSIZ);
3965819da83SLuigi Rizzo 
3975819da83SLuigi Rizzo 	(na->refcount)++;	/* XXX atomic ? we are under lock */
3985819da83SLuigi Rizzo 	if (na->refcount > 1)
3995819da83SLuigi Rizzo 		goto final;
4005819da83SLuigi Rizzo 
4015819da83SLuigi Rizzo 	/*
4025819da83SLuigi Rizzo 	 * If this is the first instance, allocate the shadow rings and
4035819da83SLuigi Rizzo 	 * buffers for this card (one for each hw queue, one for the host).
4045819da83SLuigi Rizzo 	 * The rings are contiguous, but have variable size.
4055819da83SLuigi Rizzo 	 * The entire block is reachable at
4065819da83SLuigi Rizzo 	 *	na->tx_rings[0].ring
4075819da83SLuigi Rizzo 	 */
4085819da83SLuigi Rizzo 
4095819da83SLuigi Rizzo 	len = n * (2 * sizeof(struct netmap_ring) +
4105819da83SLuigi Rizzo 		  (na->num_tx_desc + na->num_rx_desc) *
4115819da83SLuigi Rizzo 		   sizeof(struct netmap_slot) );
4125819da83SLuigi Rizzo 	buff = netmap_ring_malloc(len);
4135819da83SLuigi Rizzo 	if (buff == NULL) {
4145819da83SLuigi Rizzo 		D("failed to allocate %d bytes for %s shadow ring",
4155819da83SLuigi Rizzo 			len, ifname);
4165819da83SLuigi Rizzo error:
4175819da83SLuigi Rizzo 		(na->refcount)--;
4185819da83SLuigi Rizzo 		netmap_if_free(nifp);
4195819da83SLuigi Rizzo 		return (NULL);
4205819da83SLuigi Rizzo 	}
4215819da83SLuigi Rizzo 	/* do we have the bufers ? we are in need of num_tx_desc buffers for
4225819da83SLuigi Rizzo 	 * each tx ring and num_tx_desc buffers for each rx ring. */
4235819da83SLuigi Rizzo 	len = n * (na->num_tx_desc + na->num_rx_desc);
4245819da83SLuigi Rizzo 	NMA_LOCK();
4255819da83SLuigi Rizzo 	if (nm_buf_pool.free < len) {
4265819da83SLuigi Rizzo 		NMA_UNLOCK();
4275819da83SLuigi Rizzo 		netmap_free(buff, "not enough bufs");
4285819da83SLuigi Rizzo 		goto error;
4295819da83SLuigi Rizzo 	}
4305819da83SLuigi Rizzo 	/*
4315819da83SLuigi Rizzo 	 * in the kring, store the pointers to the shared rings
4325819da83SLuigi Rizzo 	 * and initialize the rings. We are under NMA_LOCK().
4335819da83SLuigi Rizzo 	 */
4345819da83SLuigi Rizzo 	ofs = 0;
4355819da83SLuigi Rizzo 	for (i = 0; i < n; i++) {
4365819da83SLuigi Rizzo 		struct netmap_kring *kring;
4375819da83SLuigi Rizzo 		int numdesc;
4385819da83SLuigi Rizzo 
4395819da83SLuigi Rizzo 		/* Transmit rings */
4405819da83SLuigi Rizzo 		kring = &na->tx_rings[i];
4415819da83SLuigi Rizzo 		numdesc = na->num_tx_desc;
4425819da83SLuigi Rizzo 		bzero(kring, sizeof(*kring));
4435819da83SLuigi Rizzo 		kring->na = na;
4445819da83SLuigi Rizzo 
4455819da83SLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
4465819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
4475819da83SLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
4485819da83SLuigi Rizzo 		ND("txring[%d] at %p ofs %d", i, ring, ring->buf_ofs);
4495819da83SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots =
4505819da83SLuigi Rizzo 			kring->nkr_num_slots = numdesc;
4515819da83SLuigi Rizzo 
4525819da83SLuigi Rizzo 		/*
4535819da83SLuigi Rizzo 		 * IMPORTANT:
4545819da83SLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
4555819da83SLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
4565819da83SLuigi Rizzo 		 * the same only if there are no new transmissions).
4575819da83SLuigi Rizzo 		 */
4585819da83SLuigi Rizzo 		ring->avail = kring->nr_hwavail = numdesc - 1;
4595819da83SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
4605819da83SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
4615819da83SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
4625819da83SLuigi Rizzo 
4635819da83SLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
4645819da83SLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
4655819da83SLuigi Rizzo 
4665819da83SLuigi Rizzo 		/* Receive rings */
4675819da83SLuigi Rizzo 		kring = &na->rx_rings[i];
4685819da83SLuigi Rizzo 		numdesc = na->num_rx_desc;
4695819da83SLuigi Rizzo 		bzero(kring, sizeof(*kring));
4705819da83SLuigi Rizzo 		kring->na = na;
4715819da83SLuigi Rizzo 
4725819da83SLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
4735819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
4745819da83SLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
4755819da83SLuigi Rizzo 		ND("rxring[%d] at %p offset %d", i, ring, ring->buf_ofs);
4765819da83SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots =
4775819da83SLuigi Rizzo 			kring->nkr_num_slots = numdesc;
4785819da83SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
4795819da83SLuigi Rizzo 		ring->avail = kring->nr_hwavail = 0; /* empty */
4805819da83SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
4815819da83SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
4825819da83SLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
4835819da83SLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
4845819da83SLuigi Rizzo 	}
4855819da83SLuigi Rizzo 	NMA_UNLOCK();
4865819da83SLuigi Rizzo 	for (i = 0; i < n+1; i++) {
4875819da83SLuigi Rizzo 		// XXX initialize the selrecord structs.
4885819da83SLuigi Rizzo 	}
4895819da83SLuigi Rizzo final:
4905819da83SLuigi Rizzo 	/*
4915819da83SLuigi Rizzo 	 * fill the slots for the rx and tx queues. They contain the offset
4925819da83SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
4935819da83SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
4945819da83SLuigi Rizzo 	 */
4955819da83SLuigi Rizzo 	for (i = 0; i < n; i++) {
4965819da83SLuigi Rizzo 		char *base = (char *)nifp;
4975819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
4985819da83SLuigi Rizzo 			(char *)na->tx_rings[i].ring - base;
4995819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n] =
5005819da83SLuigi Rizzo 			(char *)na->rx_rings[i].ring - base;
5015819da83SLuigi Rizzo 	}
5025819da83SLuigi Rizzo 	return (nifp);
5035819da83SLuigi Rizzo }
5045819da83SLuigi Rizzo 
5055819da83SLuigi Rizzo /*
5065819da83SLuigi Rizzo  * Initialize the memory allocator.
5075819da83SLuigi Rizzo  *
5085819da83SLuigi Rizzo  * Create the descriptor for the memory , allocate the pool of memory
5095819da83SLuigi Rizzo  * and initialize the list of memory objects with a single chunk
5105819da83SLuigi Rizzo  * containing the whole pre-allocated memory marked as free.
5115819da83SLuigi Rizzo  *
5125819da83SLuigi Rizzo  * Start with a large size, then halve as needed if we fail to
5135819da83SLuigi Rizzo  * allocate the block. While halving, always add one extra page
5145819da83SLuigi Rizzo  * because buffers 0 and 1 are used for special purposes.
5155819da83SLuigi Rizzo  * Return 0 on success, errno otherwise.
5165819da83SLuigi Rizzo  */
5175819da83SLuigi Rizzo static int
5185819da83SLuigi Rizzo netmap_memory_init(void)
5195819da83SLuigi Rizzo {
5205819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
5215819da83SLuigi Rizzo 	void *buf = NULL;
5225819da83SLuigi Rizzo 	int i, n, sz = NETMAP_MEMORY_SIZE;
5235819da83SLuigi Rizzo 	int extra_sz = 0; // space for rings and two spare buffers
5245819da83SLuigi Rizzo 
5255819da83SLuigi Rizzo 	for (; sz >= 1<<20; sz >>=1) {
5265819da83SLuigi Rizzo 		extra_sz = sz/200;
5275819da83SLuigi Rizzo 		extra_sz = (extra_sz + 2*PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
5285819da83SLuigi Rizzo 	        buf = contigmalloc(sz + extra_sz,
5295819da83SLuigi Rizzo 			     M_NETMAP,
5305819da83SLuigi Rizzo 			     M_WAITOK | M_ZERO,
5315819da83SLuigi Rizzo 			     0, /* low address */
5325819da83SLuigi Rizzo 			     -1UL, /* high address */
5335819da83SLuigi Rizzo 			     PAGE_SIZE, /* alignment */
5345819da83SLuigi Rizzo 			     0 /* boundary */
5355819da83SLuigi Rizzo 			    );
5365819da83SLuigi Rizzo 		if (buf)
5375819da83SLuigi Rizzo 			break;
5385819da83SLuigi Rizzo 	}
5395819da83SLuigi Rizzo 	if (buf == NULL)
5405819da83SLuigi Rizzo 		return (ENOMEM);
5415819da83SLuigi Rizzo 	sz += extra_sz;
5425819da83SLuigi Rizzo 	netmap_mem_d = malloc(sizeof(struct netmap_mem_d), M_NETMAP,
5435819da83SLuigi Rizzo 			      M_WAITOK | M_ZERO);
5445819da83SLuigi Rizzo 	mtx_init(&netmap_mem_d->nm_mtx, "netmap memory allocator lock", NULL,
5455819da83SLuigi Rizzo 		 MTX_DEF);
5465819da83SLuigi Rizzo 	TAILQ_INIT(&netmap_mem_d->nm_molist);
5475819da83SLuigi Rizzo 	netmap_mem_d->nm_buffer = buf;
5485819da83SLuigi Rizzo 	netmap_mem_d->nm_totalsize = sz;
5495819da83SLuigi Rizzo 
5505819da83SLuigi Rizzo 	/*
5515819da83SLuigi Rizzo 	 * A buffer takes 2k, a slot takes 8 bytes + ring overhead,
5525819da83SLuigi Rizzo 	 * so the ratio is 200:1. In other words, we can use 1/200 of
5535819da83SLuigi Rizzo 	 * the memory for the rings, and the rest for the buffers,
5545819da83SLuigi Rizzo 	 * and be sure we never run out.
5555819da83SLuigi Rizzo 	 */
5565819da83SLuigi Rizzo 	netmap_mem_d->nm_size = sz/200;
5575819da83SLuigi Rizzo 	netmap_mem_d->nm_buf_start =
5585819da83SLuigi Rizzo 		(netmap_mem_d->nm_size + PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
5595819da83SLuigi Rizzo 	netmap_mem_d->nm_buf_len = sz - netmap_mem_d->nm_buf_start;
5605819da83SLuigi Rizzo 
5615819da83SLuigi Rizzo 	nm_buf_pool.base = netmap_mem_d->nm_buffer;
5625819da83SLuigi Rizzo 	nm_buf_pool.base += netmap_mem_d->nm_buf_start;
5635819da83SLuigi Rizzo 	netmap_buffer_base = nm_buf_pool.base;
5645819da83SLuigi Rizzo 	D("netmap_buffer_base %p (offset %d)",
5655819da83SLuigi Rizzo 		netmap_buffer_base, (int)netmap_mem_d->nm_buf_start);
5665819da83SLuigi Rizzo 	/* number of buffers, they all start as free */
5675819da83SLuigi Rizzo 
5685819da83SLuigi Rizzo 	netmap_total_buffers = nm_buf_pool.total_buffers =
5695819da83SLuigi Rizzo 		netmap_mem_d->nm_buf_len / NETMAP_BUF_SIZE;
5705819da83SLuigi Rizzo 	nm_buf_pool.bufsize = NETMAP_BUF_SIZE;
5715819da83SLuigi Rizzo 
5725819da83SLuigi Rizzo 	D("Have %d MB, use %dKB for rings, %d buffers at %p",
5735819da83SLuigi Rizzo 		(sz >> 20), (int)(netmap_mem_d->nm_size >> 10),
5745819da83SLuigi Rizzo 		nm_buf_pool.total_buffers, nm_buf_pool.base);
5755819da83SLuigi Rizzo 
5765819da83SLuigi Rizzo 	/* allocate and initialize the bitmap. Entry 0 is considered
5775819da83SLuigi Rizzo 	 * always busy (used as default when there are no buffers left).
5785819da83SLuigi Rizzo 	 */
5795819da83SLuigi Rizzo 	n = (nm_buf_pool.total_buffers + 31) / 32;
5805819da83SLuigi Rizzo 	nm_buf_pool.bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP,
5815819da83SLuigi Rizzo 			 M_WAITOK | M_ZERO);
5825819da83SLuigi Rizzo 	nm_buf_pool.bitmap[0] = ~3; /* slot 0 and 1 always busy */
5835819da83SLuigi Rizzo 	for (i = 1; i < n; i++)
5845819da83SLuigi Rizzo 		nm_buf_pool.bitmap[i] = ~0;
5855819da83SLuigi Rizzo 	nm_buf_pool.free = nm_buf_pool.total_buffers - 2;
5865819da83SLuigi Rizzo 
5875819da83SLuigi Rizzo 	mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
5885819da83SLuigi Rizzo 			 M_WAITOK | M_ZERO);
5895819da83SLuigi Rizzo 	TAILQ_INSERT_HEAD(&netmap_mem_d->nm_molist, mem_obj, nmo_next);
5905819da83SLuigi Rizzo 	mem_obj->nmo_used = 0;
5915819da83SLuigi Rizzo 	mem_obj->nmo_size = netmap_mem_d->nm_size;
5925819da83SLuigi Rizzo 	mem_obj->nmo_data = netmap_mem_d->nm_buffer;
5935819da83SLuigi Rizzo 
5945819da83SLuigi Rizzo 	return (0);
5955819da83SLuigi Rizzo }
5965819da83SLuigi Rizzo 
5975819da83SLuigi Rizzo 
5985819da83SLuigi Rizzo /*
5995819da83SLuigi Rizzo  * Finalize the memory allocator.
6005819da83SLuigi Rizzo  *
6015819da83SLuigi Rizzo  * Free all the memory objects contained inside the list, and deallocate
6025819da83SLuigi Rizzo  * the pool of memory; finally free the memory allocator descriptor.
6035819da83SLuigi Rizzo  */
6045819da83SLuigi Rizzo static void
6055819da83SLuigi Rizzo netmap_memory_fini(void)
6065819da83SLuigi Rizzo {
6075819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
6085819da83SLuigi Rizzo 
6095819da83SLuigi Rizzo 	while (!TAILQ_EMPTY(&netmap_mem_d->nm_molist)) {
6105819da83SLuigi Rizzo 		mem_obj = TAILQ_FIRST(&netmap_mem_d->nm_molist);
6115819da83SLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, mem_obj, nmo_next);
6125819da83SLuigi Rizzo 		if (mem_obj->nmo_used == 1) {
6135819da83SLuigi Rizzo 			printf("netmap: leaked %d bytes at %p\n",
6145819da83SLuigi Rizzo 			       (int)mem_obj->nmo_size,
6155819da83SLuigi Rizzo 			       mem_obj->nmo_data);
6165819da83SLuigi Rizzo 		}
6175819da83SLuigi Rizzo 		free(mem_obj, M_NETMAP);
6185819da83SLuigi Rizzo 	}
6195819da83SLuigi Rizzo 	contigfree(netmap_mem_d->nm_buffer, netmap_mem_d->nm_totalsize, M_NETMAP);
6205819da83SLuigi Rizzo 	// XXX mutex_destroy(nm_mtx);
6215819da83SLuigi Rizzo 	free(netmap_mem_d, M_NETMAP);
6225819da83SLuigi Rizzo }
6235819da83SLuigi Rizzo /*------------- end of memory allocator -----------------*/
6245819da83SLuigi Rizzo 
62568b8534bSLuigi Rizzo 
62668b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
62768b8534bSLuigi Rizzo struct netmap_priv_d {
62868b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
62968b8534bSLuigi Rizzo 
63068b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
63168b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
63268b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
63368b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
63468b8534bSLuigi Rizzo };
63568b8534bSLuigi Rizzo 
63668b8534bSLuigi Rizzo 
63768b8534bSLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
63868b8534bSLuigi Rizzo 
63968b8534bSLuigi Rizzo 
64068b8534bSLuigi Rizzo static d_mmap_t netmap_mmap;
64168b8534bSLuigi Rizzo static d_ioctl_t netmap_ioctl;
64268b8534bSLuigi Rizzo static d_poll_t netmap_poll;
64368b8534bSLuigi Rizzo 
64468b8534bSLuigi Rizzo #ifdef NETMAP_KEVENT
64568b8534bSLuigi Rizzo static d_kqfilter_t netmap_kqfilter;
64668b8534bSLuigi Rizzo #endif
64768b8534bSLuigi Rizzo 
64868b8534bSLuigi Rizzo static struct cdevsw netmap_cdevsw = {
64968b8534bSLuigi Rizzo 	.d_version = D_VERSION,
65068b8534bSLuigi Rizzo 	.d_name = "netmap",
65168b8534bSLuigi Rizzo 	.d_mmap = netmap_mmap,
65268b8534bSLuigi Rizzo 	.d_ioctl = netmap_ioctl,
65368b8534bSLuigi Rizzo 	.d_poll = netmap_poll,
65468b8534bSLuigi Rizzo #ifdef NETMAP_KEVENT
65568b8534bSLuigi Rizzo 	.d_kqfilter = netmap_kqfilter,
65668b8534bSLuigi Rizzo #endif
65768b8534bSLuigi Rizzo };
65868b8534bSLuigi Rizzo 
65968b8534bSLuigi Rizzo #ifdef NETMAP_KEVENT
66068b8534bSLuigi Rizzo static int              netmap_kqread(struct knote *, long);
66168b8534bSLuigi Rizzo static int              netmap_kqwrite(struct knote *, long);
66268b8534bSLuigi Rizzo static void             netmap_kqdetach(struct knote *);
66368b8534bSLuigi Rizzo 
66468b8534bSLuigi Rizzo static struct filterops netmap_read_filterops = {
66568b8534bSLuigi Rizzo 	.f_isfd =       1,
66668b8534bSLuigi Rizzo 	.f_attach =     NULL,
66768b8534bSLuigi Rizzo 	.f_detach =     netmap_kqdetach,
66868b8534bSLuigi Rizzo 	.f_event =      netmap_kqread,
66968b8534bSLuigi Rizzo };
67068b8534bSLuigi Rizzo 
67168b8534bSLuigi Rizzo static struct filterops netmap_write_filterops = {
67268b8534bSLuigi Rizzo 	.f_isfd =       1,
67368b8534bSLuigi Rizzo 	.f_attach =     NULL,
67468b8534bSLuigi Rizzo 	.f_detach =     netmap_kqdetach,
67568b8534bSLuigi Rizzo 	.f_event =      netmap_kqwrite,
67668b8534bSLuigi Rizzo };
67768b8534bSLuigi Rizzo 
67868b8534bSLuigi Rizzo /*
67968b8534bSLuigi Rizzo  * support for the kevent() system call.
68068b8534bSLuigi Rizzo  *
68168b8534bSLuigi Rizzo  * This is the kevent filter, and is executed each time a new event
68268b8534bSLuigi Rizzo  * is triggered on the device. This function execute some operation
68368b8534bSLuigi Rizzo  * depending on the received filter.
68468b8534bSLuigi Rizzo  *
68568b8534bSLuigi Rizzo  * The implementation should test the filters and should implement
68668b8534bSLuigi Rizzo  * filter operations we are interested on (a full list in /sys/event.h).
68768b8534bSLuigi Rizzo  *
68868b8534bSLuigi Rizzo  * On a match we should:
68968b8534bSLuigi Rizzo  * - set kn->kn_fop
69068b8534bSLuigi Rizzo  * - set kn->kn_hook
69168b8534bSLuigi Rizzo  * - call knlist_add() to deliver the event to the application.
69268b8534bSLuigi Rizzo  *
69368b8534bSLuigi Rizzo  * Return 0 if the event should be delivered to the application.
69468b8534bSLuigi Rizzo  */
69568b8534bSLuigi Rizzo static int
69668b8534bSLuigi Rizzo netmap_kqfilter(struct cdev *dev, struct knote *kn)
69768b8534bSLuigi Rizzo {
69868b8534bSLuigi Rizzo 	/* declare variables needed to read/write */
69968b8534bSLuigi Rizzo 
70068b8534bSLuigi Rizzo 	switch(kn->kn_filter) {
70168b8534bSLuigi Rizzo 	case EVFILT_READ:
70268b8534bSLuigi Rizzo 		if (netmap_verbose)
70368b8534bSLuigi Rizzo 			D("%s kqfilter: EVFILT_READ" ifp->if_xname);
70468b8534bSLuigi Rizzo 
70568b8534bSLuigi Rizzo 		/* read operations */
70668b8534bSLuigi Rizzo 		kn->kn_fop = &netmap_read_filterops;
70768b8534bSLuigi Rizzo 		break;
70868b8534bSLuigi Rizzo 
70968b8534bSLuigi Rizzo 	case EVFILT_WRITE:
71068b8534bSLuigi Rizzo 		if (netmap_verbose)
71168b8534bSLuigi Rizzo 			D("%s kqfilter: EVFILT_WRITE" ifp->if_xname);
71268b8534bSLuigi Rizzo 
71368b8534bSLuigi Rizzo 		/* write operations */
71468b8534bSLuigi Rizzo 		kn->kn_fop = &netmap_write_filterops;
71568b8534bSLuigi Rizzo 		break;
71668b8534bSLuigi Rizzo 
71768b8534bSLuigi Rizzo 	default:
71868b8534bSLuigi Rizzo 		if (netmap_verbose)
71968b8534bSLuigi Rizzo 			D("%s kqfilter: invalid filter" ifp->if_xname);
72068b8534bSLuigi Rizzo 		return(EINVAL);
72168b8534bSLuigi Rizzo 	}
72268b8534bSLuigi Rizzo 
72368b8534bSLuigi Rizzo 	kn->kn_hook = 0;//
72468b8534bSLuigi Rizzo 	knlist_add(&netmap_sc->tun_rsel.si_note, kn, 0);
72568b8534bSLuigi Rizzo 
72668b8534bSLuigi Rizzo 	return (0);
72768b8534bSLuigi Rizzo }
72868b8534bSLuigi Rizzo #endif /* NETMAP_KEVENT */
72968b8534bSLuigi Rizzo 
7305819da83SLuigi Rizzo 
73168b8534bSLuigi Rizzo /*
73268b8534bSLuigi Rizzo  * File descriptor's private data destructor.
73368b8534bSLuigi Rizzo  *
73468b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
73568b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
73668b8534bSLuigi Rizzo  */
73768b8534bSLuigi Rizzo static void
7385819da83SLuigi Rizzo netmap_dtor_locked(void *data)
73968b8534bSLuigi Rizzo {
74068b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
74168b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
74268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
74368b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
74468b8534bSLuigi Rizzo 
74568b8534bSLuigi Rizzo 	na->refcount--;
74668b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
74768b8534bSLuigi Rizzo 		u_int i;
74868b8534bSLuigi Rizzo 
74968b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
75068b8534bSLuigi Rizzo 		/*
75168b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
752*1a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
75368b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
75468b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
75568b8534bSLuigi Rizzo 		 * device is dying.
75668b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
75768b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
75868b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
75968b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
76068b8534bSLuigi Rizzo 		 * they cannot run.
76168b8534bSLuigi Rizzo 		 */
762*1a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
76368b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
764*1a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
76568b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
76668b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
76768b8534bSLuigi Rizzo 		 * then return POLLERR
76868b8534bSLuigi Rizzo 		 */
76968b8534bSLuigi Rizzo 		for (i = 0; i < na->num_queues + 2; i++) {
77068b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
77168b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
77268b8534bSLuigi Rizzo 		}
77368b8534bSLuigi Rizzo 		/* release all buffers */
77468b8534bSLuigi Rizzo 		NMA_LOCK();
77568b8534bSLuigi Rizzo 		for (i = 0; i < na->num_queues + 1; i++) {
77668b8534bSLuigi Rizzo 			int j, lim;
77768b8534bSLuigi Rizzo 			struct netmap_ring *ring;
77868b8534bSLuigi Rizzo 
77968b8534bSLuigi Rizzo 			ND("tx queue %d", i);
78068b8534bSLuigi Rizzo 			ring = na->tx_rings[i].ring;
78168b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
78268b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
783446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
78468b8534bSLuigi Rizzo 
78568b8534bSLuigi Rizzo 			ND("rx queue %d", i);
78668b8534bSLuigi Rizzo 			ring = na->rx_rings[i].ring;
78768b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
78868b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
789446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
79068b8534bSLuigi Rizzo 		}
79168b8534bSLuigi Rizzo 		NMA_UNLOCK();
7926e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
79368b8534bSLuigi Rizzo 		wakeup(na);
79468b8534bSLuigi Rizzo 	}
7956e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
7965819da83SLuigi Rizzo }
79768b8534bSLuigi Rizzo 
7985819da83SLuigi Rizzo 
7995819da83SLuigi Rizzo static void
8005819da83SLuigi Rizzo netmap_dtor(void *data)
8015819da83SLuigi Rizzo {
8025819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
8035819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
8045819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
8055819da83SLuigi Rizzo 
806*1a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
8075819da83SLuigi Rizzo 	netmap_dtor_locked(data);
808*1a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
80968b8534bSLuigi Rizzo 
81068b8534bSLuigi Rizzo 	if_rele(ifp);
81168b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
81268b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
81368b8534bSLuigi Rizzo }
81468b8534bSLuigi Rizzo 
81568b8534bSLuigi Rizzo 
81668b8534bSLuigi Rizzo /*
81768b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
81868b8534bSLuigi Rizzo  *
81968b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
82068b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
82168b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
82268b8534bSLuigi Rizzo  *
82368b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
82468b8534bSLuigi Rizzo  */
82568b8534bSLuigi Rizzo static int
82668b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
82768b8534bSLuigi Rizzo netmap_mmap(__unused struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr,
82868b8534bSLuigi Rizzo 	    int nprot)
82968b8534bSLuigi Rizzo #else
83068b8534bSLuigi Rizzo netmap_mmap(__unused struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
83168b8534bSLuigi Rizzo 	    int nprot, __unused vm_memattr_t *memattr)
83268b8534bSLuigi Rizzo #endif
83368b8534bSLuigi Rizzo {
83468b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
83568b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
836446ee301SLuigi Rizzo 
83768b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
838446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
83968b8534bSLuigi Rizzo 
84068b8534bSLuigi Rizzo 	return (0);
84168b8534bSLuigi Rizzo }
84268b8534bSLuigi Rizzo 
84368b8534bSLuigi Rizzo 
84468b8534bSLuigi Rizzo /*
84502ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
84602ad4083SLuigi Rizzo  *
84702ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
84802ad4083SLuigi Rizzo  * system call in user process context, and the only contention
84902ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
85002ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
85102ad4083SLuigi Rizzo  * need to lock.
85268b8534bSLuigi Rizzo  */
85368b8534bSLuigi Rizzo static void
85468b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
85568b8534bSLuigi Rizzo {
85668b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_queues];
85768b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
85868b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
85902ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
86068b8534bSLuigi Rizzo 
86102ad4083SLuigi Rizzo 	k = ring->cur;
86202ad4083SLuigi Rizzo 	if (k > lim) {
86302ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
86402ad4083SLuigi Rizzo 		return;
86502ad4083SLuigi Rizzo 	}
866*1a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
86768b8534bSLuigi Rizzo 
86868b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
86968b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
87068b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
87168b8534bSLuigi Rizzo 	 */
87202ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
87368b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
87468b8534bSLuigi Rizzo 
87568b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
87668b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
87768b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
87868b8534bSLuigi Rizzo 			continue;
87968b8534bSLuigi Rizzo 		}
88068b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
88168b8534bSLuigi Rizzo 
88268b8534bSLuigi Rizzo 		if (m == NULL)
88368b8534bSLuigi Rizzo 			break;
88468b8534bSLuigi Rizzo 		if (tail)
88568b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
88668b8534bSLuigi Rizzo 		else
88768b8534bSLuigi Rizzo 			head = m;
88868b8534bSLuigi Rizzo 		tail = m;
88968b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
89068b8534bSLuigi Rizzo 	}
89102ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
89268b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
893*1a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
89468b8534bSLuigi Rizzo 
89568b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
89668b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
89768b8534bSLuigi Rizzo 		head = head->m_nextpkt;
89868b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
89968b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
900*1a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
901*1a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
90268b8534bSLuigi Rizzo 	}
90368b8534bSLuigi Rizzo }
90468b8534bSLuigi Rizzo 
90568b8534bSLuigi Rizzo /*
90602ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
90702ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
90802ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
90902ad4083SLuigi Rizzo  *
91068b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
91168b8534bSLuigi Rizzo  * (we know because td != NULL).
91268b8534bSLuigi Rizzo  */
91368b8534bSLuigi Rizzo static void
91468b8534bSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td)
91568b8534bSLuigi Rizzo {
91668b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_queues];
91768b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
91802ad4083SLuigi Rizzo 	int error = 1, delta;
91902ad4083SLuigi Rizzo 	u_int k = ring->cur, lim = kring->nkr_num_slots;
92068b8534bSLuigi Rizzo 
921*1a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
92202ad4083SLuigi Rizzo 	if (k >= lim) /* bad value */
92302ad4083SLuigi Rizzo 		goto done;
92402ad4083SLuigi Rizzo 	delta = k - kring->nr_hwcur;
92568b8534bSLuigi Rizzo 	if (delta < 0)
92602ad4083SLuigi Rizzo 		delta += lim;
92768b8534bSLuigi Rizzo 	kring->nr_hwavail -= delta;
92802ad4083SLuigi Rizzo 	if (kring->nr_hwavail < 0)	/* error */
92902ad4083SLuigi Rizzo 		goto done;
93002ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
93102ad4083SLuigi Rizzo 	error = 0;
93202ad4083SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail;
93302ad4083SLuigi Rizzo 	if (k == 0 && td)
93468b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
93502ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
93602ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
93702ad4083SLuigi Rizzo done:
938*1a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
93902ad4083SLuigi Rizzo 	if (error)
94002ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
94168b8534bSLuigi Rizzo }
94268b8534bSLuigi Rizzo 
94368b8534bSLuigi Rizzo 
94468b8534bSLuigi Rizzo /*
94568b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
94668b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
94768b8534bSLuigi Rizzo  * is not supported by the interface.
94868b8534bSLuigi Rizzo  * If successful, hold a reference.
94968b8534bSLuigi Rizzo  */
95068b8534bSLuigi Rizzo static int
95168b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
95268b8534bSLuigi Rizzo {
95368b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
95468b8534bSLuigi Rizzo 	if (*ifp == NULL)
95568b8534bSLuigi Rizzo 		return (ENXIO);
95668b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
95768b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
95868b8534bSLuigi Rizzo 	 */
95968b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
96068b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
96168b8534bSLuigi Rizzo 	if_rele(*ifp);
96268b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
96368b8534bSLuigi Rizzo }
96468b8534bSLuigi Rizzo 
96568b8534bSLuigi Rizzo 
96668b8534bSLuigi Rizzo /*
96768b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
96868b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
96968b8534bSLuigi Rizzo  * Return 1 on reinit.
970506cc70cSLuigi Rizzo  *
971506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
972506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
973506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
974506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
975506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
976506cc70cSLuigi Rizzo  * it under lock.
97768b8534bSLuigi Rizzo  */
97868b8534bSLuigi Rizzo int
97968b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
98068b8534bSLuigi Rizzo {
98168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
98268b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
98368b8534bSLuigi Rizzo 	int errors = 0;
98468b8534bSLuigi Rizzo 
98568b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
98668b8534bSLuigi Rizzo 	if (ring->cur > lim)
98768b8534bSLuigi Rizzo 		errors++;
98868b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
98968b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
99068b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
99168b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
99268b8534bSLuigi Rizzo 			if (!errors++)
99368b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
99468b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
99568b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
99668b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
99768b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
99868b8534bSLuigi Rizzo 			if (!errors++)
99968b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
100068b8534bSLuigi Rizzo 					len, i, idx);
100168b8534bSLuigi Rizzo 		}
100268b8534bSLuigi Rizzo 	}
100368b8534bSLuigi Rizzo 	if (errors) {
100468b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
100568b8534bSLuigi Rizzo 		int n = kring->na->num_queues + 2;
100668b8534bSLuigi Rizzo 
100768b8534bSLuigi Rizzo 		D("total %d errors", errors);
100868b8534bSLuigi Rizzo 		errors++;
100968b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
101068b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
101168b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
101268b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
101368b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
101468b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
101568b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
101668b8534bSLuigi Rizzo 	}
101768b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
101868b8534bSLuigi Rizzo }
101968b8534bSLuigi Rizzo 
102068b8534bSLuigi Rizzo 
102168b8534bSLuigi Rizzo /*
102268b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
102368b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
102468b8534bSLuigi Rizzo  */
102568b8534bSLuigi Rizzo static int
102668b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
102768b8534bSLuigi Rizzo {
102868b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
102968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
103068b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
103168b8534bSLuigi Rizzo 	/* first time we don't lock */
103268b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
103368b8534bSLuigi Rizzo 
103468b8534bSLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= na->num_queues) {
103568b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
103668b8534bSLuigi Rizzo 		return (EINVAL);
103768b8534bSLuigi Rizzo 	}
103868b8534bSLuigi Rizzo 	if (need_lock)
1039*1a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
104068b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
104168b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
104268b8534bSLuigi Rizzo 		priv->np_qfirst = na->num_queues;
104368b8534bSLuigi Rizzo 		priv->np_qlast = na->num_queues + 1;
104468b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
104568b8534bSLuigi Rizzo 		priv->np_qfirst = i;
104668b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
104768b8534bSLuigi Rizzo 	} else {
104868b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
104968b8534bSLuigi Rizzo 		priv->np_qlast = na->num_queues;
105068b8534bSLuigi Rizzo 	}
105168b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
105268b8534bSLuigi Rizzo 	if (need_lock)
1053*1a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
105468b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
105568b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
105668b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
105768b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
105868b8534bSLuigi Rizzo 			priv->np_qfirst);
105968b8534bSLuigi Rizzo 	else
106068b8534bSLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname,
106168b8534bSLuigi Rizzo 			priv->np_qlast);
106268b8534bSLuigi Rizzo 	return 0;
106368b8534bSLuigi Rizzo }
106468b8534bSLuigi Rizzo 
106568b8534bSLuigi Rizzo /*
106668b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
106768b8534bSLuigi Rizzo  *
106868b8534bSLuigi Rizzo  * Following a list of accepted commands:
106968b8534bSLuigi Rizzo  * - NIOCGINFO
107068b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
107168b8534bSLuigi Rizzo  * - NIOCREGIF
107268b8534bSLuigi Rizzo  * - NIOCUNREGIF
107368b8534bSLuigi Rizzo  * - NIOCTXSYNC
107468b8534bSLuigi Rizzo  * - NIOCRXSYNC
107568b8534bSLuigi Rizzo  *
107668b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
107768b8534bSLuigi Rizzo  */
107868b8534bSLuigi Rizzo static int
107968b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data,
1080506cc70cSLuigi Rizzo 	__unused int fflag, struct thread *td)
108168b8534bSLuigi Rizzo {
108268b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
108368b8534bSLuigi Rizzo 	struct ifnet *ifp;
108468b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
108568b8534bSLuigi Rizzo 	struct netmap_adapter *na;
108668b8534bSLuigi Rizzo 	int error;
108768b8534bSLuigi Rizzo 	u_int i;
108868b8534bSLuigi Rizzo 	struct netmap_if *nifp;
108968b8534bSLuigi Rizzo 
1090506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1091506cc70cSLuigi Rizzo 
109268b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
1093506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
1094506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
109568b8534bSLuigi Rizzo 		return (error);
1096506cc70cSLuigi Rizzo 	}
109768b8534bSLuigi Rizzo 
109868b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
109968b8534bSLuigi Rizzo 	switch (cmd) {
110068b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
110168b8534bSLuigi Rizzo 		/* memsize is always valid */
110268b8534bSLuigi Rizzo 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
110368b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
110468b8534bSLuigi Rizzo 		nmr->nr_numrings = 0;
110568b8534bSLuigi Rizzo 		nmr->nr_numslots = 0;
110668b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
110768b8534bSLuigi Rizzo 			break;
110868b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
110968b8534bSLuigi Rizzo 		if (error)
111068b8534bSLuigi Rizzo 			break;
111168b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
111268b8534bSLuigi Rizzo 		nmr->nr_numrings = na->num_queues;
111368b8534bSLuigi Rizzo 		nmr->nr_numslots = na->num_tx_desc;
111468b8534bSLuigi Rizzo 		if_rele(ifp);	/* return the refcount */
111568b8534bSLuigi Rizzo 		break;
111668b8534bSLuigi Rizzo 
111768b8534bSLuigi Rizzo 	case NIOCREGIF:
1118506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
1119506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
1120506cc70cSLuigi Rizzo 			break;
1121506cc70cSLuigi Rizzo 		}
112268b8534bSLuigi Rizzo 		/* find the interface and a reference */
112368b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
112468b8534bSLuigi Rizzo 		if (error)
112568b8534bSLuigi Rizzo 			break;
112668b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
112768b8534bSLuigi Rizzo 		/*
112868b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
112968b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
113068b8534bSLuigi Rizzo 		 */
113168b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
113268b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
113368b8534bSLuigi Rizzo 		if (priv == NULL) {
113468b8534bSLuigi Rizzo 			error = ENOMEM;
113568b8534bSLuigi Rizzo 			if_rele(ifp);   /* return the refcount */
113668b8534bSLuigi Rizzo 			break;
113768b8534bSLuigi Rizzo 		}
113868b8534bSLuigi Rizzo 
113968b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
1140*1a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
114168b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
114268b8534bSLuigi Rizzo 				break;
1143*1a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
114468b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
114568b8534bSLuigi Rizzo 		}
114668b8534bSLuigi Rizzo 		if (i == 0) {
114768b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
114868b8534bSLuigi Rizzo 			error = EINVAL;
114968b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
115068b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
115168b8534bSLuigi Rizzo 			break;
115268b8534bSLuigi Rizzo 		}
115368b8534bSLuigi Rizzo 
115468b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
115568b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
115668b8534bSLuigi Rizzo 		if (error)
115768b8534bSLuigi Rizzo 			goto error;
115868b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
115968b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
116068b8534bSLuigi Rizzo 			error = ENOMEM;
116168b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
116268b8534bSLuigi Rizzo 			/* was already set */
116368b8534bSLuigi Rizzo 		} else {
116468b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
116568b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
116668b8534bSLuigi Rizzo 			 */
116768b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
11685819da83SLuigi Rizzo 			if (error)
11695819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
117068b8534bSLuigi Rizzo 		}
117168b8534bSLuigi Rizzo 
117268b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
117368b8534bSLuigi Rizzo error:
1174*1a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
117568b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
11765819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
11775819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
117868b8534bSLuigi Rizzo 			break;
117968b8534bSLuigi Rizzo 		}
118068b8534bSLuigi Rizzo 
1181*1a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
118268b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
118368b8534bSLuigi Rizzo 
118468b8534bSLuigi Rizzo 		if (error != 0) {
118568b8534bSLuigi Rizzo 			/* could not assign the private storage for the
118668b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
118768b8534bSLuigi Rizzo 			 */
118868b8534bSLuigi Rizzo 			netmap_dtor(priv);
118968b8534bSLuigi Rizzo 			break;
119068b8534bSLuigi Rizzo 		}
119168b8534bSLuigi Rizzo 
119268b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
119368b8534bSLuigi Rizzo 		nmr->nr_numrings = na->num_queues;
119468b8534bSLuigi Rizzo 		nmr->nr_numslots = na->num_tx_desc;
119568b8534bSLuigi Rizzo 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
1196446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
119768b8534bSLuigi Rizzo 		break;
119868b8534bSLuigi Rizzo 
119968b8534bSLuigi Rizzo 	case NIOCUNREGIF:
1200506cc70cSLuigi Rizzo 		if (priv == NULL) {
1201506cc70cSLuigi Rizzo 			error = ENXIO;
1202506cc70cSLuigi Rizzo 			break;
1203506cc70cSLuigi Rizzo 		}
120468b8534bSLuigi Rizzo 
120568b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
120668b8534bSLuigi Rizzo 		   destructor of the private data. */
120768b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
120868b8534bSLuigi Rizzo 		break;
120968b8534bSLuigi Rizzo 
121068b8534bSLuigi Rizzo 	case NIOCTXSYNC:
121168b8534bSLuigi Rizzo         case NIOCRXSYNC:
1212506cc70cSLuigi Rizzo 		if (priv == NULL) {
1213506cc70cSLuigi Rizzo 			error = ENXIO;
1214506cc70cSLuigi Rizzo 			break;
1215506cc70cSLuigi Rizzo 		}
121668b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
121768b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
121868b8534bSLuigi Rizzo 
121968b8534bSLuigi Rizzo 		if (priv->np_qfirst == na->num_queues) {
122068b8534bSLuigi Rizzo 			/* queues to/from host */
122168b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
122268b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
122368b8534bSLuigi Rizzo 			else
122468b8534bSLuigi Rizzo 				netmap_sync_from_host(na, NULL);
1225506cc70cSLuigi Rizzo 			break;
122668b8534bSLuigi Rizzo 		}
122768b8534bSLuigi Rizzo 
122868b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
122968b8534bSLuigi Rizzo 		    if (cmd == NIOCTXSYNC) {
123068b8534bSLuigi Rizzo 			struct netmap_kring *kring = &na->tx_rings[i];
123168b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
123268b8534bSLuigi Rizzo 				D("sync tx ring %d cur %d hwcur %d",
123368b8534bSLuigi Rizzo 					i, kring->ring->cur,
123468b8534bSLuigi Rizzo 					kring->nr_hwcur);
1235*1a26580eSLuigi Rizzo                         na->nm_txsync(ifp, i, 1 /* do lock */);
123668b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
123768b8534bSLuigi Rizzo 				D("after sync tx ring %d cur %d hwcur %d",
123868b8534bSLuigi Rizzo 					i, kring->ring->cur,
123968b8534bSLuigi Rizzo 					kring->nr_hwcur);
124068b8534bSLuigi Rizzo 		    } else {
1241*1a26580eSLuigi Rizzo 			na->nm_rxsync(ifp, i, 1 /* do lock */);
124268b8534bSLuigi Rizzo 			microtime(&na->rx_rings[i].ring->ts);
124368b8534bSLuigi Rizzo 		    }
124468b8534bSLuigi Rizzo 		}
124568b8534bSLuigi Rizzo 
124668b8534bSLuigi Rizzo                 break;
124768b8534bSLuigi Rizzo 
124868b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
124968b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
125068b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
125168b8534bSLuigi Rizzo 	case BIOCSSEESENT:
125268b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
125368b8534bSLuigi Rizzo 		break;
125468b8534bSLuigi Rizzo 
125568b8534bSLuigi Rizzo 	default:
125668b8534bSLuigi Rizzo 	    {
125768b8534bSLuigi Rizzo 		/*
125868b8534bSLuigi Rizzo 		 * allow device calls
125968b8534bSLuigi Rizzo 		 */
126068b8534bSLuigi Rizzo 		struct socket so;
126168b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
126268b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
126368b8534bSLuigi Rizzo 		if (error)
126468b8534bSLuigi Rizzo 			break;
126568b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
126668b8534bSLuigi Rizzo 		// so->so_proto not null.
126768b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
126868b8534bSLuigi Rizzo 		if_rele(ifp);
126968b8534bSLuigi Rizzo 	    }
127068b8534bSLuigi Rizzo 	}
127168b8534bSLuigi Rizzo 
1272506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
127368b8534bSLuigi Rizzo 	return (error);
127468b8534bSLuigi Rizzo }
127568b8534bSLuigi Rizzo 
127668b8534bSLuigi Rizzo 
127768b8534bSLuigi Rizzo /*
127868b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
127968b8534bSLuigi Rizzo  *
128068b8534bSLuigi Rizzo  * Can be called for one or more queues.
128168b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
128268b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
128368b8534bSLuigi Rizzo  * selfd or on the global one.
128468b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
128568b8534bSLuigi Rizzo  * are done through callbacks.
128668b8534bSLuigi Rizzo  */
128768b8534bSLuigi Rizzo static int
128868b8534bSLuigi Rizzo netmap_poll(__unused struct cdev *dev, int events, struct thread *td)
128968b8534bSLuigi Rizzo {
129068b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
129168b8534bSLuigi Rizzo 	struct netmap_adapter *na;
129268b8534bSLuigi Rizzo 	struct ifnet *ifp;
129368b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1294d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
1295bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
129668b8534bSLuigi Rizzo 
129768b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
129868b8534bSLuigi Rizzo 		return POLLERR;
129968b8534bSLuigi Rizzo 
130068b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
130168b8534bSLuigi Rizzo 	// XXX check for deleting() ?
130268b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
130368b8534bSLuigi Rizzo 		return POLLERR;
130468b8534bSLuigi Rizzo 
130568b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
130668b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
130768b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
130868b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
130968b8534bSLuigi Rizzo 
131068b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
131168b8534bSLuigi Rizzo 
131268b8534bSLuigi Rizzo 	/* how many queues we are scanning */
131368b8534bSLuigi Rizzo 	i = priv->np_qfirst;
131468b8534bSLuigi Rizzo 	if (i == na->num_queues) { /* from/to host */
131568b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
131668b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
131768b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
131868b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
131968b8534bSLuigi Rizzo 			revents |= want_tx;
132068b8534bSLuigi Rizzo 		}
132168b8534bSLuigi Rizzo 		if (want_rx) {
132268b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
132368b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
132468b8534bSLuigi Rizzo 				netmap_sync_from_host(na, td);
132568b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
132668b8534bSLuigi Rizzo 				revents |= want_rx;
132768b8534bSLuigi Rizzo 			}
132868b8534bSLuigi Rizzo 		}
132968b8534bSLuigi Rizzo 		return (revents);
133068b8534bSLuigi Rizzo 	}
133168b8534bSLuigi Rizzo 
133268b8534bSLuigi Rizzo 	/*
133368b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
133468b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
133568b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
133668b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
133768b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
133868b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
133968b8534bSLuigi Rizzo 	 * has more than one ring.
134068b8534bSLuigi Rizzo 	 *
134168b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
134268b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
134368b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
134468b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
134568b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
134668b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
134768b8534bSLuigi Rizzo 	 *
134868b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
134968b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
135068b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
135168b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
135268b8534bSLuigi Rizzo 	 */
135368b8534bSLuigi Rizzo 	check_all = (i + 1 != priv->np_qlast);
135468b8534bSLuigi Rizzo 
135568b8534bSLuigi Rizzo 	/*
135668b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
135768b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
135868b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
135968b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
136068b8534bSLuigi Rizzo 	 *
136168b8534bSLuigi Rizzo 	 * It has three possible states:
136268b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
136368b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
136468b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
136568b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
136668b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
136768b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
136868b8534bSLuigi Rizzo 	 */
1369d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
137068b8534bSLuigi Rizzo 	/*
137168b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
137268b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
137368b8534bSLuigi Rizzo 	 * routines.
137468b8534bSLuigi Rizzo 	 */
137568b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; want_rx && i < priv->np_qlast; i++) {
137668b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
137768b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
137868b8534bSLuigi Rizzo 				revents |= want_rx;
137968b8534bSLuigi Rizzo 				want_rx = 0;	/* also breaks the loop */
138068b8534bSLuigi Rizzo 			}
138168b8534bSLuigi Rizzo 		}
138268b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; want_tx && i < priv->np_qlast; i++) {
138368b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
138468b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
138568b8534bSLuigi Rizzo 				revents |= want_tx;
138668b8534bSLuigi Rizzo 				want_tx = 0;	/* also breaks the loop */
138768b8534bSLuigi Rizzo 			}
138868b8534bSLuigi Rizzo 		}
138968b8534bSLuigi Rizzo 
139068b8534bSLuigi Rizzo 	/*
139168b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
139268b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
139368b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
139468b8534bSLuigi Rizzo 	 */
139568b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
139668b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
139768b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
13985819da83SLuigi Rizzo 			/*
13995819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
14005819da83SLuigi Rizzo 			 * (we have already done a successful sync on
14015819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
14025819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
14035819da83SLuigi Rizzo 			 */
140468b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
140568b8534bSLuigi Rizzo 				continue;
140668b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
1407*1a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
140868b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
140968b8534bSLuigi Rizzo 			}
141068b8534bSLuigi Rizzo 			if (na->separate_locks)
1411*1a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
141268b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
141368b8534bSLuigi Rizzo 				D("send %d on %s %d",
141468b8534bSLuigi Rizzo 					kring->ring->cur,
141568b8534bSLuigi Rizzo 					ifp->if_xname, i);
1416*1a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
141768b8534bSLuigi Rizzo 				revents |= POLLERR;
141868b8534bSLuigi Rizzo 
14195819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
142068b8534bSLuigi Rizzo 			if (want_tx) {
142168b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
142268b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
142368b8534bSLuigi Rizzo 					 * starvation.
142468b8534bSLuigi Rizzo 					 */
142568b8534bSLuigi Rizzo 					revents |= want_tx;
142668b8534bSLuigi Rizzo 					want_tx = 0;
142768b8534bSLuigi Rizzo 				} else if (!check_all)
142868b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
142968b8534bSLuigi Rizzo 			}
143068b8534bSLuigi Rizzo 			if (na->separate_locks)
1431*1a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
143268b8534bSLuigi Rizzo 		}
143368b8534bSLuigi Rizzo 	}
143468b8534bSLuigi Rizzo 
143568b8534bSLuigi Rizzo 	/*
143668b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
143768b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
143868b8534bSLuigi Rizzo 	 */
143968b8534bSLuigi Rizzo 	if (want_rx) {
144068b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
144168b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
144268b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
1443*1a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
144468b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
144568b8534bSLuigi Rizzo 			}
144668b8534bSLuigi Rizzo 			if (na->separate_locks)
1447*1a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
144868b8534bSLuigi Rizzo 
1449*1a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
145068b8534bSLuigi Rizzo 				revents |= POLLERR;
14515819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
14525819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
145368b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
14545819da83SLuigi Rizzo 			}
145568b8534bSLuigi Rizzo 
145668b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
145768b8534bSLuigi Rizzo 				revents |= want_rx;
145868b8534bSLuigi Rizzo 			else if (!check_all)
145968b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
146068b8534bSLuigi Rizzo 			if (na->separate_locks)
1461*1a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
146268b8534bSLuigi Rizzo 		}
146368b8534bSLuigi Rizzo 	}
146468b8534bSLuigi Rizzo 	if (check_all && revents == 0) {
146568b8534bSLuigi Rizzo 		i = na->num_queues + 1; /* the global queue */
146668b8534bSLuigi Rizzo 		if (want_tx)
146768b8534bSLuigi Rizzo 			selrecord(td, &na->tx_rings[i].si);
146868b8534bSLuigi Rizzo 		if (want_rx)
146968b8534bSLuigi Rizzo 			selrecord(td, &na->rx_rings[i].si);
147068b8534bSLuigi Rizzo 	}
147168b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
1472*1a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
147368b8534bSLuigi Rizzo 
147468b8534bSLuigi Rizzo 	return (revents);
147568b8534bSLuigi Rizzo }
147668b8534bSLuigi Rizzo 
147768b8534bSLuigi Rizzo /*------- driver support routines ------*/
147868b8534bSLuigi Rizzo 
147968b8534bSLuigi Rizzo /*
1480*1a26580eSLuigi Rizzo  * default lock wrapper. On linux we use mostly netmap-specific locks.
1481*1a26580eSLuigi Rizzo  */
1482*1a26580eSLuigi Rizzo static void
1483*1a26580eSLuigi Rizzo netmap_lock_wrapper(struct ifnet *_a, int what, u_int queueid)
1484*1a26580eSLuigi Rizzo {
1485*1a26580eSLuigi Rizzo 	struct netmap_adapter *na = NA(_a);
1486*1a26580eSLuigi Rizzo 
1487*1a26580eSLuigi Rizzo 	switch (what) {
1488*1a26580eSLuigi Rizzo #ifndef __FreeBSD__	/* some system do not need lock on register */
1489*1a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
1490*1a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
1491*1a26580eSLuigi Rizzo 		break;
1492*1a26580eSLuigi Rizzo #endif
1493*1a26580eSLuigi Rizzo 
1494*1a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
1495*1a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
1496*1a26580eSLuigi Rizzo 		break;
1497*1a26580eSLuigi Rizzo 
1498*1a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
1499*1a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
1500*1a26580eSLuigi Rizzo 		break;
1501*1a26580eSLuigi Rizzo 
1502*1a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
1503*1a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
1504*1a26580eSLuigi Rizzo 		break;
1505*1a26580eSLuigi Rizzo 
1506*1a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
1507*1a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
1508*1a26580eSLuigi Rizzo 		break;
1509*1a26580eSLuigi Rizzo 
1510*1a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
1511*1a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
1512*1a26580eSLuigi Rizzo 		break;
1513*1a26580eSLuigi Rizzo 
1514*1a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
1515*1a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
1516*1a26580eSLuigi Rizzo 		break;
1517*1a26580eSLuigi Rizzo 	}
1518*1a26580eSLuigi Rizzo }
1519*1a26580eSLuigi Rizzo 
1520*1a26580eSLuigi Rizzo 
1521*1a26580eSLuigi Rizzo /*
152268b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
152368b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
152468b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
152568b8534bSLuigi Rizzo  * of hardware rings):
152668b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
152768b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
152868b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
152968b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
153068b8534bSLuigi Rizzo  */
153168b8534bSLuigi Rizzo int
153268b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
153368b8534bSLuigi Rizzo {
153468b8534bSLuigi Rizzo 	int n = num_queues + 2;
153568b8534bSLuigi Rizzo 	int size = sizeof(*na) + 2 * n * sizeof(struct netmap_kring);
153668b8534bSLuigi Rizzo 	void *buf;
153768b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1538*1a26580eSLuigi Rizzo 	int i;
153968b8534bSLuigi Rizzo 
154068b8534bSLuigi Rizzo 	if (ifp == NULL) {
154168b8534bSLuigi Rizzo 		D("ifp not set, giving up");
154268b8534bSLuigi Rizzo 		return EINVAL;
154368b8534bSLuigi Rizzo 	}
154468b8534bSLuigi Rizzo 	na->refcount = 0;
154568b8534bSLuigi Rizzo 	na->num_queues = num_queues;
154668b8534bSLuigi Rizzo 
154768b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
154868b8534bSLuigi Rizzo 	if (buf) {
1549d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
155068b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
155168b8534bSLuigi Rizzo 		na->rx_rings = na->tx_rings + n;
15525819da83SLuigi Rizzo 		na->buff_size = NETMAP_BUF_SIZE;
155368b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
155468b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
1555*1a26580eSLuigi Rizzo 
1556*1a26580eSLuigi Rizzo 		na = buf;
1557*1a26580eSLuigi Rizzo 		if (na->nm_lock == NULL)
1558*1a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
1559*1a26580eSLuigi Rizzo 		mtx_init(&na->core_lock, "netmap core lock", NULL, MTX_DEF);
1560*1a26580eSLuigi Rizzo 		for (i = 0 ; i < num_queues; i++)
1561*1a26580eSLuigi Rizzo 			mtx_init(&na->tx_rings[i].q_lock, "netmap txq lock", NULL, MTX_DEF);
1562*1a26580eSLuigi Rizzo 		for (i = 0 ; i < num_queues; i++)
1563*1a26580eSLuigi Rizzo 			mtx_init(&na->rx_rings[i].q_lock, "netmap rxq lock", NULL, MTX_DEF);
156468b8534bSLuigi Rizzo 	}
156568b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
156668b8534bSLuigi Rizzo 
156768b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
156868b8534bSLuigi Rizzo }
156968b8534bSLuigi Rizzo 
157068b8534bSLuigi Rizzo 
157168b8534bSLuigi Rizzo /*
157268b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
157368b8534bSLuigi Rizzo  * object.
157468b8534bSLuigi Rizzo  */
157568b8534bSLuigi Rizzo void
157668b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
157768b8534bSLuigi Rizzo {
157868b8534bSLuigi Rizzo 	u_int i;
157968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
158068b8534bSLuigi Rizzo 
158168b8534bSLuigi Rizzo 	if (!na)
158268b8534bSLuigi Rizzo 		return;
158368b8534bSLuigi Rizzo 
158468b8534bSLuigi Rizzo 	for (i = 0; i < na->num_queues + 2; i++) {
158568b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
158668b8534bSLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
158768b8534bSLuigi Rizzo 	}
158868b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1589d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
159068b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
159168b8534bSLuigi Rizzo }
159268b8534bSLuigi Rizzo 
159368b8534bSLuigi Rizzo 
159468b8534bSLuigi Rizzo /*
159502ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
159602ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
159768b8534bSLuigi Rizzo  * We are not locked when called.
159868b8534bSLuigi Rizzo  */
159968b8534bSLuigi Rizzo int
160068b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
160168b8534bSLuigi Rizzo {
160268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
160302ad4083SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_queues];
1604*1a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
160502ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
160668b8534bSLuigi Rizzo 	struct netmap_slot *slot;
160768b8534bSLuigi Rizzo 
160868b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
160968b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
161068b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
1611*1a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
161202ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
161368b8534bSLuigi Rizzo 		D("stack ring %s full\n", ifp->if_xname);
161468b8534bSLuigi Rizzo 		goto done;	/* no space */
161568b8534bSLuigi Rizzo 	}
161668b8534bSLuigi Rizzo 	if (len > na->buff_size) {
161768b8534bSLuigi Rizzo 		D("drop packet size %d > %d", len, na->buff_size);
161868b8534bSLuigi Rizzo 		goto done;	/* too long for us */
161968b8534bSLuigi Rizzo 	}
162068b8534bSLuigi Rizzo 
162168b8534bSLuigi Rizzo 	/* compute the insert position */
162268b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
162302ad4083SLuigi Rizzo 	if (i > lim)
162402ad4083SLuigi Rizzo 		i -= lim + 1;
162568b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
162668b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
162768b8534bSLuigi Rizzo 	slot->len = len;
162868b8534bSLuigi Rizzo 	kring->nr_hwavail++;
162968b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
163068b8534bSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_queues);
163168b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
163268b8534bSLuigi Rizzo 	error = 0;
163368b8534bSLuigi Rizzo done:
1634*1a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
163568b8534bSLuigi Rizzo 
163668b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
163768b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
163868b8534bSLuigi Rizzo 	 * only when really necessary.
163968b8534bSLuigi Rizzo 	 */
164068b8534bSLuigi Rizzo 	m_freem(m);
164168b8534bSLuigi Rizzo 
164268b8534bSLuigi Rizzo 	return (error);
164368b8534bSLuigi Rizzo }
164468b8534bSLuigi Rizzo 
164568b8534bSLuigi Rizzo 
164668b8534bSLuigi Rizzo /*
164768b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
164868b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
164968b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
165068b8534bSLuigi Rizzo  */
165168b8534bSLuigi Rizzo struct netmap_slot *
165268b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
165368b8534bSLuigi Rizzo 	u_int new_cur)
165468b8534bSLuigi Rizzo {
165568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
165668b8534bSLuigi Rizzo 	struct netmap_ring *ring;
1657506cc70cSLuigi Rizzo 	int new_hwofs, lim;
165868b8534bSLuigi Rizzo 
165968b8534bSLuigi Rizzo 	if (na == NULL)
166068b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
166168b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
166268b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
166368b8534bSLuigi Rizzo 	kring = tx == NR_TX ?  na->tx_rings + n : na->rx_rings + n;
166468b8534bSLuigi Rizzo 	ring = kring->ring;
1665506cc70cSLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
166668b8534bSLuigi Rizzo 
1667506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1668506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
1669506cc70cSLuigi Rizzo 	else
1670506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
1671506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1672506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1673506cc70cSLuigi Rizzo 
1674506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1675506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1676506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1677506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1678506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1679506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1680506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1681506cc70cSLuigi Rizzo 
168268b8534bSLuigi Rizzo 	/*
1683506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1684506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
168568b8534bSLuigi Rizzo 	 */
168668b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
168768b8534bSLuigi Rizzo 	selwakeuppri(&kring[na->num_queues + 1 - n].si, PI_NET);
168868b8534bSLuigi Rizzo 	return kring->ring->slot;
168968b8534bSLuigi Rizzo }
169068b8534bSLuigi Rizzo 
169168b8534bSLuigi Rizzo 
169268b8534bSLuigi Rizzo /*
1693*1a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
1694*1a26580eSLuigi Rizzo  * we have 4 cases:
1695*1a26580eSLuigi Rizzo  * 1 ring, single lock:
1696*1a26580eSLuigi Rizzo  *     lock(core); wake(i=0); unlock(core)
1697*1a26580eSLuigi Rizzo  * N rings, single lock:
1698*1a26580eSLuigi Rizzo  *     lock(core); wake(i); wake(N+1) unlock(core)
1699*1a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
1700*1a26580eSLuigi Rizzo  *     lock(i); wake(i); unlock(i)
1701*1a26580eSLuigi Rizzo  * N rings, separate locks:
1702*1a26580eSLuigi Rizzo  *     lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
1703*1a26580eSLuigi Rizzo  */
1704*1a26580eSLuigi Rizzo int netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
1705*1a26580eSLuigi Rizzo {
1706*1a26580eSLuigi Rizzo 	struct netmap_adapter *na;
1707*1a26580eSLuigi Rizzo 	struct netmap_kring *r;
1708*1a26580eSLuigi Rizzo 
1709*1a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
1710*1a26580eSLuigi Rizzo 		return 0;
1711*1a26580eSLuigi Rizzo 	na = NA(ifp);
1712*1a26580eSLuigi Rizzo 	r = work_done ? na->rx_rings : na->tx_rings;
1713*1a26580eSLuigi Rizzo 	if (na->separate_locks) {
1714*1a26580eSLuigi Rizzo 		mtx_lock(&r[q].q_lock);
1715*1a26580eSLuigi Rizzo 		selwakeuppri(&r[q].si, PI_NET);
1716*1a26580eSLuigi Rizzo 		mtx_unlock(&r[q].q_lock);
1717*1a26580eSLuigi Rizzo 		if (na->num_queues > 1) {
1718*1a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
1719*1a26580eSLuigi Rizzo 			selwakeuppri(&r[na->num_queues + 1].si, PI_NET);
1720*1a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
1721*1a26580eSLuigi Rizzo 		}
1722*1a26580eSLuigi Rizzo 	} else {
1723*1a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
1724*1a26580eSLuigi Rizzo 		selwakeuppri(&r[q].si, PI_NET);
1725*1a26580eSLuigi Rizzo 		if (na->num_queues > 1)
1726*1a26580eSLuigi Rizzo 			selwakeuppri(&r[na->num_queues + 1].si, PI_NET);
1727*1a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
1728*1a26580eSLuigi Rizzo 	}
1729*1a26580eSLuigi Rizzo 	if (work_done)
1730*1a26580eSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
1731*1a26580eSLuigi Rizzo 	return 1;
1732*1a26580eSLuigi Rizzo }
1733*1a26580eSLuigi Rizzo 
1734*1a26580eSLuigi Rizzo /*
173568b8534bSLuigi Rizzo  * Module loader.
173668b8534bSLuigi Rizzo  *
173768b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
173868b8534bSLuigi Rizzo  * variables.
173968b8534bSLuigi Rizzo  *
174068b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
174168b8534bSLuigi Rizzo  */
174268b8534bSLuigi Rizzo static int
174368b8534bSLuigi Rizzo netmap_init(void)
174468b8534bSLuigi Rizzo {
174568b8534bSLuigi Rizzo 	int error;
174668b8534bSLuigi Rizzo 
174768b8534bSLuigi Rizzo 
174868b8534bSLuigi Rizzo 	error = netmap_memory_init();
174968b8534bSLuigi Rizzo 	if (error != 0) {
175068b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
175168b8534bSLuigi Rizzo 		return (error);
175268b8534bSLuigi Rizzo 	}
175368b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
175485df3791SLuigi Rizzo 		(int)(netmap_mem_d->nm_totalsize >> 20));
175568b8534bSLuigi Rizzo 
175668b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
175768b8534bSLuigi Rizzo 			      "netmap");
175868b8534bSLuigi Rizzo 
175968b8534bSLuigi Rizzo 	return (0);
176068b8534bSLuigi Rizzo }
176168b8534bSLuigi Rizzo 
176268b8534bSLuigi Rizzo 
176368b8534bSLuigi Rizzo /*
176468b8534bSLuigi Rizzo  * Module unloader.
176568b8534bSLuigi Rizzo  *
176668b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
176768b8534bSLuigi Rizzo  */
176868b8534bSLuigi Rizzo static void
176968b8534bSLuigi Rizzo netmap_fini(void)
177068b8534bSLuigi Rizzo {
177168b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
177268b8534bSLuigi Rizzo 
177368b8534bSLuigi Rizzo 	netmap_memory_fini();
177468b8534bSLuigi Rizzo 
177568b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
177668b8534bSLuigi Rizzo }
177768b8534bSLuigi Rizzo 
177868b8534bSLuigi Rizzo 
177968b8534bSLuigi Rizzo /*
178068b8534bSLuigi Rizzo  * Kernel entry point.
178168b8534bSLuigi Rizzo  *
178268b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
178368b8534bSLuigi Rizzo  *
178468b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
178568b8534bSLuigi Rizzo  */
178668b8534bSLuigi Rizzo static int
178768b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
178868b8534bSLuigi Rizzo {
178968b8534bSLuigi Rizzo 	int error = 0;
179068b8534bSLuigi Rizzo 
179168b8534bSLuigi Rizzo 	switch (event) {
179268b8534bSLuigi Rizzo 	case MOD_LOAD:
179368b8534bSLuigi Rizzo 		error = netmap_init();
179468b8534bSLuigi Rizzo 		break;
179568b8534bSLuigi Rizzo 
179668b8534bSLuigi Rizzo 	case MOD_UNLOAD:
179768b8534bSLuigi Rizzo 		netmap_fini();
179868b8534bSLuigi Rizzo 		break;
179968b8534bSLuigi Rizzo 
180068b8534bSLuigi Rizzo 	default:
180168b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
180268b8534bSLuigi Rizzo 		break;
180368b8534bSLuigi Rizzo 	}
180468b8534bSLuigi Rizzo 
180568b8534bSLuigi Rizzo 	return (error);
180668b8534bSLuigi Rizzo }
180768b8534bSLuigi Rizzo 
180868b8534bSLuigi Rizzo 
180968b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
1810