xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision 5819da83)
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);
95*5819da83SLuigi Rizzo struct netmap_mem_d;
96*5819da83SLuigi Rizzo static struct netmap_mem_d *netmap_mem_d;	/* Our memory allocator. */
97*5819da83SLuigi Rizzo 
98*5819da83SLuigi Rizzo u_int netmap_total_buffers;
99*5819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
100*5819da83SLuigi Rizzo 
101*5819da83SLuigi Rizzo /* user-controlled variables */
102*5819da83SLuigi Rizzo int netmap_verbose;
103*5819da83SLuigi Rizzo 
104*5819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
105*5819da83SLuigi Rizzo 
106*5819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
107*5819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
108*5819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
109*5819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
110*5819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
111*5819da83SLuigi Rizzo int netmap_buf_size = 2048;
112*5819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size);
113*5819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
114*5819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
115*5819da83SLuigi Rizzo int netmap_mitigate = 1;
116*5819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
117*5819da83SLuigi Rizzo int netmap_no_pendintr;
118*5819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
119*5819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
120*5819da83SLuigi Rizzo 
121*5819da83SLuigi Rizzo 
122*5819da83SLuigi Rizzo 
123*5819da83SLuigi Rizzo /*----- memory allocator -----------------*/
124*5819da83SLuigi Rizzo /*
125*5819da83SLuigi Rizzo  * Here we have the low level routines for memory allocator
126*5819da83SLuigi Rizzo  * and its primary users.
127*5819da83SLuigi 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");
168*5819da83SLuigi Rizzo 
169*5819da83SLuigi Rizzo 
170*5819da83SLuigi 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 
258*5819da83SLuigi Rizzo /* Shorthand to compute a netmap interface offset. */
259*5819da83SLuigi Rizzo #define netmap_if_offset(v)                                     \
260*5819da83SLuigi Rizzo     ((char *) (v) - (char *) netmap_mem_d->nm_buffer)
261*5819da83SLuigi Rizzo /* .. and get a physical address given a memory offset */
262*5819da83SLuigi Rizzo #define netmap_ofstophys(o)                                     \
263*5819da83SLuigi Rizzo     (vtophys(netmap_mem_d->nm_buffer) + (o))
264*5819da83SLuigi Rizzo 
265*5819da83SLuigi Rizzo 
266*5819da83SLuigi Rizzo /*------ netmap memory allocator -------*/
267*5819da83SLuigi Rizzo /*
268*5819da83SLuigi Rizzo  * Request for a chunk of memory.
269*5819da83SLuigi Rizzo  *
270*5819da83SLuigi Rizzo  * Memory objects are arranged into a list, hence we need to walk this
271*5819da83SLuigi Rizzo  * list until we find an object with the needed amount of data free.
272*5819da83SLuigi Rizzo  * This sounds like a completely inefficient implementation, but given
273*5819da83SLuigi Rizzo  * the fact that data allocation is done once, we can handle it
274*5819da83SLuigi Rizzo  * flawlessly.
275*5819da83SLuigi Rizzo  *
276*5819da83SLuigi Rizzo  * Return NULL on failure.
277*5819da83SLuigi Rizzo  */
278*5819da83SLuigi Rizzo static void *
279*5819da83SLuigi Rizzo netmap_malloc(size_t size, __unused const char *msg)
280*5819da83SLuigi Rizzo {
281*5819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj, *new_mem_obj;
282*5819da83SLuigi Rizzo 	void *ret = NULL;
283*5819da83SLuigi Rizzo 
284*5819da83SLuigi Rizzo 	NMA_LOCK();
285*5819da83SLuigi Rizzo 	TAILQ_FOREACH(mem_obj, &netmap_mem_d->nm_molist, nmo_next) {
286*5819da83SLuigi Rizzo 		if (mem_obj->nmo_used != 0 || mem_obj->nmo_size < size)
287*5819da83SLuigi Rizzo 			continue;
288*5819da83SLuigi Rizzo 
289*5819da83SLuigi Rizzo 		new_mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
290*5819da83SLuigi Rizzo 				     M_WAITOK | M_ZERO);
291*5819da83SLuigi Rizzo 		TAILQ_INSERT_BEFORE(mem_obj, new_mem_obj, nmo_next);
292*5819da83SLuigi Rizzo 
293*5819da83SLuigi Rizzo 		new_mem_obj->nmo_used = 1;
294*5819da83SLuigi Rizzo 		new_mem_obj->nmo_size = size;
295*5819da83SLuigi Rizzo 		new_mem_obj->nmo_data = mem_obj->nmo_data;
296*5819da83SLuigi Rizzo 		memset(new_mem_obj->nmo_data, 0, new_mem_obj->nmo_size);
297*5819da83SLuigi Rizzo 
298*5819da83SLuigi Rizzo 		mem_obj->nmo_size -= size;
299*5819da83SLuigi Rizzo 		mem_obj->nmo_data = (char *) mem_obj->nmo_data + size;
300*5819da83SLuigi Rizzo 		if (mem_obj->nmo_size == 0) {
301*5819da83SLuigi Rizzo 			TAILQ_REMOVE(&netmap_mem_d->nm_molist, mem_obj,
302*5819da83SLuigi Rizzo 				     nmo_next);
303*5819da83SLuigi Rizzo 			free(mem_obj, M_NETMAP);
304*5819da83SLuigi Rizzo 		}
305*5819da83SLuigi Rizzo 
306*5819da83SLuigi Rizzo 		ret = new_mem_obj->nmo_data;
307*5819da83SLuigi Rizzo 
308*5819da83SLuigi Rizzo 		break;
309*5819da83SLuigi Rizzo 	}
310*5819da83SLuigi Rizzo 	NMA_UNLOCK();
311*5819da83SLuigi Rizzo 	ND("%s: %d bytes at %p", msg, size, ret);
312*5819da83SLuigi Rizzo 
313*5819da83SLuigi Rizzo 	return (ret);
314*5819da83SLuigi Rizzo }
315*5819da83SLuigi Rizzo 
316*5819da83SLuigi Rizzo /*
317*5819da83SLuigi Rizzo  * Return the memory to the allocator.
318*5819da83SLuigi Rizzo  *
319*5819da83SLuigi Rizzo  * While freeing a memory object, we try to merge adjacent chunks in
320*5819da83SLuigi Rizzo  * order to reduce memory fragmentation.
321*5819da83SLuigi Rizzo  */
322*5819da83SLuigi Rizzo static void
323*5819da83SLuigi Rizzo netmap_free(void *addr, const char *msg)
324*5819da83SLuigi Rizzo {
325*5819da83SLuigi Rizzo 	size_t size;
326*5819da83SLuigi Rizzo 	struct netmap_mem_obj *cur, *prev, *next;
327*5819da83SLuigi Rizzo 
328*5819da83SLuigi Rizzo 	if (addr == NULL) {
329*5819da83SLuigi Rizzo 		D("NULL addr for %s", msg);
330*5819da83SLuigi Rizzo 		return;
331*5819da83SLuigi Rizzo 	}
332*5819da83SLuigi Rizzo 
333*5819da83SLuigi Rizzo 	NMA_LOCK();
334*5819da83SLuigi Rizzo 	TAILQ_FOREACH(cur, &netmap_mem_d->nm_molist, nmo_next) {
335*5819da83SLuigi Rizzo 		if (cur->nmo_data == addr && cur->nmo_used)
336*5819da83SLuigi Rizzo 			break;
337*5819da83SLuigi Rizzo 	}
338*5819da83SLuigi Rizzo 	if (cur == NULL) {
339*5819da83SLuigi Rizzo 		NMA_UNLOCK();
340*5819da83SLuigi Rizzo 		D("invalid addr %s %p", msg, addr);
341*5819da83SLuigi Rizzo 		return;
342*5819da83SLuigi Rizzo 	}
343*5819da83SLuigi Rizzo 
344*5819da83SLuigi Rizzo 	size = cur->nmo_size;
345*5819da83SLuigi Rizzo 	cur->nmo_used = 0;
346*5819da83SLuigi Rizzo 
347*5819da83SLuigi Rizzo 	/* merge current chunk of memory with the previous one,
348*5819da83SLuigi Rizzo 	   if present. */
349*5819da83SLuigi Rizzo 	prev = TAILQ_PREV(cur, netmap_mem_obj_h, nmo_next);
350*5819da83SLuigi Rizzo 	if (prev && prev->nmo_used == 0) {
351*5819da83SLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, cur, nmo_next);
352*5819da83SLuigi Rizzo 		prev->nmo_size += cur->nmo_size;
353*5819da83SLuigi Rizzo 		free(cur, M_NETMAP);
354*5819da83SLuigi Rizzo 		cur = prev;
355*5819da83SLuigi Rizzo 	}
356*5819da83SLuigi Rizzo 
357*5819da83SLuigi Rizzo 	/* merge with the next one */
358*5819da83SLuigi Rizzo 	next = TAILQ_NEXT(cur, nmo_next);
359*5819da83SLuigi Rizzo 	if (next && next->nmo_used == 0) {
360*5819da83SLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, next, nmo_next);
361*5819da83SLuigi Rizzo 		cur->nmo_size += next->nmo_size;
362*5819da83SLuigi Rizzo 		free(next, M_NETMAP);
363*5819da83SLuigi Rizzo 	}
364*5819da83SLuigi Rizzo 	NMA_UNLOCK();
365*5819da83SLuigi Rizzo 	ND("freed %s %d bytes at %p", msg, size, addr);
366*5819da83SLuigi Rizzo }
367*5819da83SLuigi Rizzo 
368*5819da83SLuigi Rizzo 
369*5819da83SLuigi Rizzo /*
370*5819da83SLuigi Rizzo  * Create and return a new ``netmap_if`` object, and possibly also
371*5819da83SLuigi Rizzo  * rings and packet buffors.
372*5819da83SLuigi Rizzo  *
373*5819da83SLuigi Rizzo  * Return NULL on failure.
374*5819da83SLuigi Rizzo  */
375*5819da83SLuigi Rizzo static void *
376*5819da83SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
377*5819da83SLuigi Rizzo {
378*5819da83SLuigi Rizzo 	struct netmap_if *nifp;
379*5819da83SLuigi Rizzo 	struct netmap_ring *ring;
380*5819da83SLuigi Rizzo 	char *buff;
381*5819da83SLuigi Rizzo 	u_int i, len, ofs;
382*5819da83SLuigi Rizzo 	u_int n = na->num_queues + 1; /* shorthand, include stack queue */
383*5819da83SLuigi Rizzo 
384*5819da83SLuigi Rizzo 	/*
385*5819da83SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
386*5819da83SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
387*5819da83SLuigi Rizzo 	 */
388*5819da83SLuigi Rizzo 	len = sizeof(struct netmap_if) + 2 * n * sizeof(ssize_t);
389*5819da83SLuigi Rizzo 	nifp = netmap_if_malloc(len);
390*5819da83SLuigi Rizzo 	if (nifp == NULL)
391*5819da83SLuigi Rizzo 		return (NULL);
392*5819da83SLuigi Rizzo 
393*5819da83SLuigi Rizzo 	/* initialize base fields */
394*5819da83SLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_num_queues = na->num_queues;
395*5819da83SLuigi Rizzo 	strncpy(nifp->ni_name, ifname, IFNAMSIZ);
396*5819da83SLuigi Rizzo 
397*5819da83SLuigi Rizzo 	(na->refcount)++;	/* XXX atomic ? we are under lock */
398*5819da83SLuigi Rizzo 	if (na->refcount > 1)
399*5819da83SLuigi Rizzo 		goto final;
400*5819da83SLuigi Rizzo 
401*5819da83SLuigi Rizzo 	/*
402*5819da83SLuigi Rizzo 	 * If this is the first instance, allocate the shadow rings and
403*5819da83SLuigi Rizzo 	 * buffers for this card (one for each hw queue, one for the host).
404*5819da83SLuigi Rizzo 	 * The rings are contiguous, but have variable size.
405*5819da83SLuigi Rizzo 	 * The entire block is reachable at
406*5819da83SLuigi Rizzo 	 *	na->tx_rings[0].ring
407*5819da83SLuigi Rizzo 	 */
408*5819da83SLuigi Rizzo 
409*5819da83SLuigi Rizzo 	len = n * (2 * sizeof(struct netmap_ring) +
410*5819da83SLuigi Rizzo 		  (na->num_tx_desc + na->num_rx_desc) *
411*5819da83SLuigi Rizzo 		   sizeof(struct netmap_slot) );
412*5819da83SLuigi Rizzo 	buff = netmap_ring_malloc(len);
413*5819da83SLuigi Rizzo 	if (buff == NULL) {
414*5819da83SLuigi Rizzo 		D("failed to allocate %d bytes for %s shadow ring",
415*5819da83SLuigi Rizzo 			len, ifname);
416*5819da83SLuigi Rizzo error:
417*5819da83SLuigi Rizzo 		(na->refcount)--;
418*5819da83SLuigi Rizzo 		netmap_if_free(nifp);
419*5819da83SLuigi Rizzo 		return (NULL);
420*5819da83SLuigi Rizzo 	}
421*5819da83SLuigi Rizzo 	/* do we have the bufers ? we are in need of num_tx_desc buffers for
422*5819da83SLuigi Rizzo 	 * each tx ring and num_tx_desc buffers for each rx ring. */
423*5819da83SLuigi Rizzo 	len = n * (na->num_tx_desc + na->num_rx_desc);
424*5819da83SLuigi Rizzo 	NMA_LOCK();
425*5819da83SLuigi Rizzo 	if (nm_buf_pool.free < len) {
426*5819da83SLuigi Rizzo 		NMA_UNLOCK();
427*5819da83SLuigi Rizzo 		netmap_free(buff, "not enough bufs");
428*5819da83SLuigi Rizzo 		goto error;
429*5819da83SLuigi Rizzo 	}
430*5819da83SLuigi Rizzo 	/*
431*5819da83SLuigi Rizzo 	 * in the kring, store the pointers to the shared rings
432*5819da83SLuigi Rizzo 	 * and initialize the rings. We are under NMA_LOCK().
433*5819da83SLuigi Rizzo 	 */
434*5819da83SLuigi Rizzo 	ofs = 0;
435*5819da83SLuigi Rizzo 	for (i = 0; i < n; i++) {
436*5819da83SLuigi Rizzo 		struct netmap_kring *kring;
437*5819da83SLuigi Rizzo 		int numdesc;
438*5819da83SLuigi Rizzo 
439*5819da83SLuigi Rizzo 		/* Transmit rings */
440*5819da83SLuigi Rizzo 		kring = &na->tx_rings[i];
441*5819da83SLuigi Rizzo 		numdesc = na->num_tx_desc;
442*5819da83SLuigi Rizzo 		bzero(kring, sizeof(*kring));
443*5819da83SLuigi Rizzo 		kring->na = na;
444*5819da83SLuigi Rizzo 
445*5819da83SLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
446*5819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
447*5819da83SLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
448*5819da83SLuigi Rizzo 		ND("txring[%d] at %p ofs %d", i, ring, ring->buf_ofs);
449*5819da83SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots =
450*5819da83SLuigi Rizzo 			kring->nkr_num_slots = numdesc;
451*5819da83SLuigi Rizzo 
452*5819da83SLuigi Rizzo 		/*
453*5819da83SLuigi Rizzo 		 * IMPORTANT:
454*5819da83SLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
455*5819da83SLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
456*5819da83SLuigi Rizzo 		 * the same only if there are no new transmissions).
457*5819da83SLuigi Rizzo 		 */
458*5819da83SLuigi Rizzo 		ring->avail = kring->nr_hwavail = numdesc - 1;
459*5819da83SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
460*5819da83SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
461*5819da83SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
462*5819da83SLuigi Rizzo 
463*5819da83SLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
464*5819da83SLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
465*5819da83SLuigi Rizzo 
466*5819da83SLuigi Rizzo 		/* Receive rings */
467*5819da83SLuigi Rizzo 		kring = &na->rx_rings[i];
468*5819da83SLuigi Rizzo 		numdesc = na->num_rx_desc;
469*5819da83SLuigi Rizzo 		bzero(kring, sizeof(*kring));
470*5819da83SLuigi Rizzo 		kring->na = na;
471*5819da83SLuigi Rizzo 
472*5819da83SLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
473*5819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
474*5819da83SLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
475*5819da83SLuigi Rizzo 		ND("rxring[%d] at %p offset %d", i, ring, ring->buf_ofs);
476*5819da83SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots =
477*5819da83SLuigi Rizzo 			kring->nkr_num_slots = numdesc;
478*5819da83SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
479*5819da83SLuigi Rizzo 		ring->avail = kring->nr_hwavail = 0; /* empty */
480*5819da83SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
481*5819da83SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
482*5819da83SLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
483*5819da83SLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
484*5819da83SLuigi Rizzo 	}
485*5819da83SLuigi Rizzo 	NMA_UNLOCK();
486*5819da83SLuigi Rizzo 	for (i = 0; i < n+1; i++) {
487*5819da83SLuigi Rizzo 		// XXX initialize the selrecord structs.
488*5819da83SLuigi Rizzo 	}
489*5819da83SLuigi Rizzo final:
490*5819da83SLuigi Rizzo 	/*
491*5819da83SLuigi Rizzo 	 * fill the slots for the rx and tx queues. They contain the offset
492*5819da83SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
493*5819da83SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
494*5819da83SLuigi Rizzo 	 */
495*5819da83SLuigi Rizzo 	for (i = 0; i < n; i++) {
496*5819da83SLuigi Rizzo 		char *base = (char *)nifp;
497*5819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
498*5819da83SLuigi Rizzo 			(char *)na->tx_rings[i].ring - base;
499*5819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n] =
500*5819da83SLuigi Rizzo 			(char *)na->rx_rings[i].ring - base;
501*5819da83SLuigi Rizzo 	}
502*5819da83SLuigi Rizzo 	return (nifp);
503*5819da83SLuigi Rizzo }
504*5819da83SLuigi Rizzo 
505*5819da83SLuigi Rizzo /*
506*5819da83SLuigi Rizzo  * Initialize the memory allocator.
507*5819da83SLuigi Rizzo  *
508*5819da83SLuigi Rizzo  * Create the descriptor for the memory , allocate the pool of memory
509*5819da83SLuigi Rizzo  * and initialize the list of memory objects with a single chunk
510*5819da83SLuigi Rizzo  * containing the whole pre-allocated memory marked as free.
511*5819da83SLuigi Rizzo  *
512*5819da83SLuigi Rizzo  * Start with a large size, then halve as needed if we fail to
513*5819da83SLuigi Rizzo  * allocate the block. While halving, always add one extra page
514*5819da83SLuigi Rizzo  * because buffers 0 and 1 are used for special purposes.
515*5819da83SLuigi Rizzo  * Return 0 on success, errno otherwise.
516*5819da83SLuigi Rizzo  */
517*5819da83SLuigi Rizzo static int
518*5819da83SLuigi Rizzo netmap_memory_init(void)
519*5819da83SLuigi Rizzo {
520*5819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
521*5819da83SLuigi Rizzo 	void *buf = NULL;
522*5819da83SLuigi Rizzo 	int i, n, sz = NETMAP_MEMORY_SIZE;
523*5819da83SLuigi Rizzo 	int extra_sz = 0; // space for rings and two spare buffers
524*5819da83SLuigi Rizzo 
525*5819da83SLuigi Rizzo 	for (; sz >= 1<<20; sz >>=1) {
526*5819da83SLuigi Rizzo 		extra_sz = sz/200;
527*5819da83SLuigi Rizzo 		extra_sz = (extra_sz + 2*PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
528*5819da83SLuigi Rizzo 	        buf = contigmalloc(sz + extra_sz,
529*5819da83SLuigi Rizzo 			     M_NETMAP,
530*5819da83SLuigi Rizzo 			     M_WAITOK | M_ZERO,
531*5819da83SLuigi Rizzo 			     0, /* low address */
532*5819da83SLuigi Rizzo 			     -1UL, /* high address */
533*5819da83SLuigi Rizzo 			     PAGE_SIZE, /* alignment */
534*5819da83SLuigi Rizzo 			     0 /* boundary */
535*5819da83SLuigi Rizzo 			    );
536*5819da83SLuigi Rizzo 		if (buf)
537*5819da83SLuigi Rizzo 			break;
538*5819da83SLuigi Rizzo 	}
539*5819da83SLuigi Rizzo 	if (buf == NULL)
540*5819da83SLuigi Rizzo 		return (ENOMEM);
541*5819da83SLuigi Rizzo 	sz += extra_sz;
542*5819da83SLuigi Rizzo 	netmap_mem_d = malloc(sizeof(struct netmap_mem_d), M_NETMAP,
543*5819da83SLuigi Rizzo 			      M_WAITOK | M_ZERO);
544*5819da83SLuigi Rizzo 	mtx_init(&netmap_mem_d->nm_mtx, "netmap memory allocator lock", NULL,
545*5819da83SLuigi Rizzo 		 MTX_DEF);
546*5819da83SLuigi Rizzo 	TAILQ_INIT(&netmap_mem_d->nm_molist);
547*5819da83SLuigi Rizzo 	netmap_mem_d->nm_buffer = buf;
548*5819da83SLuigi Rizzo 	netmap_mem_d->nm_totalsize = sz;
549*5819da83SLuigi Rizzo 
550*5819da83SLuigi Rizzo 	/*
551*5819da83SLuigi Rizzo 	 * A buffer takes 2k, a slot takes 8 bytes + ring overhead,
552*5819da83SLuigi Rizzo 	 * so the ratio is 200:1. In other words, we can use 1/200 of
553*5819da83SLuigi Rizzo 	 * the memory for the rings, and the rest for the buffers,
554*5819da83SLuigi Rizzo 	 * and be sure we never run out.
555*5819da83SLuigi Rizzo 	 */
556*5819da83SLuigi Rizzo 	netmap_mem_d->nm_size = sz/200;
557*5819da83SLuigi Rizzo 	netmap_mem_d->nm_buf_start =
558*5819da83SLuigi Rizzo 		(netmap_mem_d->nm_size + PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
559*5819da83SLuigi Rizzo 	netmap_mem_d->nm_buf_len = sz - netmap_mem_d->nm_buf_start;
560*5819da83SLuigi Rizzo 
561*5819da83SLuigi Rizzo 	nm_buf_pool.base = netmap_mem_d->nm_buffer;
562*5819da83SLuigi Rizzo 	nm_buf_pool.base += netmap_mem_d->nm_buf_start;
563*5819da83SLuigi Rizzo 	netmap_buffer_base = nm_buf_pool.base;
564*5819da83SLuigi Rizzo 	D("netmap_buffer_base %p (offset %d)",
565*5819da83SLuigi Rizzo 		netmap_buffer_base, (int)netmap_mem_d->nm_buf_start);
566*5819da83SLuigi Rizzo 	/* number of buffers, they all start as free */
567*5819da83SLuigi Rizzo 
568*5819da83SLuigi Rizzo 	netmap_total_buffers = nm_buf_pool.total_buffers =
569*5819da83SLuigi Rizzo 		netmap_mem_d->nm_buf_len / NETMAP_BUF_SIZE;
570*5819da83SLuigi Rizzo 	nm_buf_pool.bufsize = NETMAP_BUF_SIZE;
571*5819da83SLuigi Rizzo 
572*5819da83SLuigi Rizzo 	D("Have %d MB, use %dKB for rings, %d buffers at %p",
573*5819da83SLuigi Rizzo 		(sz >> 20), (int)(netmap_mem_d->nm_size >> 10),
574*5819da83SLuigi Rizzo 		nm_buf_pool.total_buffers, nm_buf_pool.base);
575*5819da83SLuigi Rizzo 
576*5819da83SLuigi Rizzo 	/* allocate and initialize the bitmap. Entry 0 is considered
577*5819da83SLuigi Rizzo 	 * always busy (used as default when there are no buffers left).
578*5819da83SLuigi Rizzo 	 */
579*5819da83SLuigi Rizzo 	n = (nm_buf_pool.total_buffers + 31) / 32;
580*5819da83SLuigi Rizzo 	nm_buf_pool.bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP,
581*5819da83SLuigi Rizzo 			 M_WAITOK | M_ZERO);
582*5819da83SLuigi Rizzo 	nm_buf_pool.bitmap[0] = ~3; /* slot 0 and 1 always busy */
583*5819da83SLuigi Rizzo 	for (i = 1; i < n; i++)
584*5819da83SLuigi Rizzo 		nm_buf_pool.bitmap[i] = ~0;
585*5819da83SLuigi Rizzo 	nm_buf_pool.free = nm_buf_pool.total_buffers - 2;
586*5819da83SLuigi Rizzo 
587*5819da83SLuigi Rizzo 	mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
588*5819da83SLuigi Rizzo 			 M_WAITOK | M_ZERO);
589*5819da83SLuigi Rizzo 	TAILQ_INSERT_HEAD(&netmap_mem_d->nm_molist, mem_obj, nmo_next);
590*5819da83SLuigi Rizzo 	mem_obj->nmo_used = 0;
591*5819da83SLuigi Rizzo 	mem_obj->nmo_size = netmap_mem_d->nm_size;
592*5819da83SLuigi Rizzo 	mem_obj->nmo_data = netmap_mem_d->nm_buffer;
593*5819da83SLuigi Rizzo 
594*5819da83SLuigi Rizzo 	return (0);
595*5819da83SLuigi Rizzo }
596*5819da83SLuigi Rizzo 
597*5819da83SLuigi Rizzo 
598*5819da83SLuigi Rizzo /*
599*5819da83SLuigi Rizzo  * Finalize the memory allocator.
600*5819da83SLuigi Rizzo  *
601*5819da83SLuigi Rizzo  * Free all the memory objects contained inside the list, and deallocate
602*5819da83SLuigi Rizzo  * the pool of memory; finally free the memory allocator descriptor.
603*5819da83SLuigi Rizzo  */
604*5819da83SLuigi Rizzo static void
605*5819da83SLuigi Rizzo netmap_memory_fini(void)
606*5819da83SLuigi Rizzo {
607*5819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
608*5819da83SLuigi Rizzo 
609*5819da83SLuigi Rizzo 	while (!TAILQ_EMPTY(&netmap_mem_d->nm_molist)) {
610*5819da83SLuigi Rizzo 		mem_obj = TAILQ_FIRST(&netmap_mem_d->nm_molist);
611*5819da83SLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, mem_obj, nmo_next);
612*5819da83SLuigi Rizzo 		if (mem_obj->nmo_used == 1) {
613*5819da83SLuigi Rizzo 			printf("netmap: leaked %d bytes at %p\n",
614*5819da83SLuigi Rizzo 			       (int)mem_obj->nmo_size,
615*5819da83SLuigi Rizzo 			       mem_obj->nmo_data);
616*5819da83SLuigi Rizzo 		}
617*5819da83SLuigi Rizzo 		free(mem_obj, M_NETMAP);
618*5819da83SLuigi Rizzo 	}
619*5819da83SLuigi Rizzo 	contigfree(netmap_mem_d->nm_buffer, netmap_mem_d->nm_totalsize, M_NETMAP);
620*5819da83SLuigi Rizzo 	// XXX mutex_destroy(nm_mtx);
621*5819da83SLuigi Rizzo 	free(netmap_mem_d, M_NETMAP);
622*5819da83SLuigi Rizzo }
623*5819da83SLuigi Rizzo /*------------- end of memory allocator -----------------*/
624*5819da83SLuigi 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 
730*5819da83SLuigi 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
738*5819da83SLuigi 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
75268b8534bSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_CORE_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 		 */
76268b8534bSLuigi Rizzo 		na->nm_lock(ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
76368b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
76468b8534bSLuigi Rizzo 		na->nm_lock(ifp->if_softc, NETMAP_CORE_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);
796*5819da83SLuigi Rizzo }
79768b8534bSLuigi Rizzo 
798*5819da83SLuigi Rizzo 
799*5819da83SLuigi Rizzo static void
800*5819da83SLuigi Rizzo netmap_dtor(void *data)
801*5819da83SLuigi Rizzo {
802*5819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
803*5819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
804*5819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
805*5819da83SLuigi Rizzo 
806*5819da83SLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_LOCK, 0);
807*5819da83SLuigi Rizzo 	netmap_dtor_locked(data);
80868b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_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 	}
86602ad4083SLuigi Rizzo 	// na->nm_lock(na->ifp->if_softc, 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;
89302ad4083SLuigi Rizzo 	// na->nm_lock(na->ifp->if_softc, 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 		m->m_pkthdr.rcvif = na->ifp;
90068b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
90168b8534bSLuigi Rizzo 			D("sending up pkt %p size %d", m, m->m_pkthdr.len);
90268b8534bSLuigi Rizzo 		(na->ifp->if_input)(na->ifp, m);
90368b8534bSLuigi Rizzo 	}
90468b8534bSLuigi Rizzo }
90568b8534bSLuigi Rizzo 
90668b8534bSLuigi Rizzo /*
90702ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
90802ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
90902ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
91002ad4083SLuigi Rizzo  *
91168b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
91268b8534bSLuigi Rizzo  * (we know because td != NULL).
91368b8534bSLuigi Rizzo  */
91468b8534bSLuigi Rizzo static void
91568b8534bSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td)
91668b8534bSLuigi Rizzo {
91768b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_queues];
91868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
91902ad4083SLuigi Rizzo 	int error = 1, delta;
92002ad4083SLuigi Rizzo 	u_int k = ring->cur, lim = kring->nkr_num_slots;
92168b8534bSLuigi Rizzo 
92268b8534bSLuigi Rizzo 	na->nm_lock(na->ifp->if_softc, NETMAP_CORE_LOCK, 0);
92302ad4083SLuigi Rizzo 	if (k >= lim) /* bad value */
92402ad4083SLuigi Rizzo 		goto done;
92502ad4083SLuigi Rizzo 	delta = k - kring->nr_hwcur;
92668b8534bSLuigi Rizzo 	if (delta < 0)
92702ad4083SLuigi Rizzo 		delta += lim;
92868b8534bSLuigi Rizzo 	kring->nr_hwavail -= delta;
92902ad4083SLuigi Rizzo 	if (kring->nr_hwavail < 0)	/* error */
93002ad4083SLuigi Rizzo 		goto done;
93102ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
93202ad4083SLuigi Rizzo 	error = 0;
93302ad4083SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail;
93402ad4083SLuigi Rizzo 	if (k == 0 && td)
93568b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
93602ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
93702ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
93802ad4083SLuigi Rizzo done:
93968b8534bSLuigi Rizzo 	na->nm_lock(na->ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
94002ad4083SLuigi Rizzo 	if (error)
94102ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
94268b8534bSLuigi Rizzo }
94368b8534bSLuigi Rizzo 
94468b8534bSLuigi Rizzo 
94568b8534bSLuigi Rizzo /*
94668b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
94768b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
94868b8534bSLuigi Rizzo  * is not supported by the interface.
94968b8534bSLuigi Rizzo  * If successful, hold a reference.
95068b8534bSLuigi Rizzo  */
95168b8534bSLuigi Rizzo static int
95268b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
95368b8534bSLuigi Rizzo {
95468b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
95568b8534bSLuigi Rizzo 	if (*ifp == NULL)
95668b8534bSLuigi Rizzo 		return (ENXIO);
95768b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
95868b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
95968b8534bSLuigi Rizzo 	 */
96068b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
96168b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
96268b8534bSLuigi Rizzo 	if_rele(*ifp);
96368b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
96468b8534bSLuigi Rizzo }
96568b8534bSLuigi Rizzo 
96668b8534bSLuigi Rizzo 
96768b8534bSLuigi Rizzo /*
96868b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
96968b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
97068b8534bSLuigi Rizzo  * Return 1 on reinit.
971506cc70cSLuigi Rizzo  *
972506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
973506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
974506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
975506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
976506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
977506cc70cSLuigi Rizzo  * it under lock.
97868b8534bSLuigi Rizzo  */
97968b8534bSLuigi Rizzo int
98068b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
98168b8534bSLuigi Rizzo {
98268b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
98368b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
98468b8534bSLuigi Rizzo 	int errors = 0;
98568b8534bSLuigi Rizzo 
98668b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
98768b8534bSLuigi Rizzo 	if (ring->cur > lim)
98868b8534bSLuigi Rizzo 		errors++;
98968b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
99068b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
99168b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
99268b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
99368b8534bSLuigi Rizzo 			if (!errors++)
99468b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
99568b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
99668b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
99768b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
99868b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
99968b8534bSLuigi Rizzo 			if (!errors++)
100068b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
100168b8534bSLuigi Rizzo 					len, i, idx);
100268b8534bSLuigi Rizzo 		}
100368b8534bSLuigi Rizzo 	}
100468b8534bSLuigi Rizzo 	if (errors) {
100568b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
100668b8534bSLuigi Rizzo 		int n = kring->na->num_queues + 2;
100768b8534bSLuigi Rizzo 
100868b8534bSLuigi Rizzo 		D("total %d errors", errors);
100968b8534bSLuigi Rizzo 		errors++;
101068b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
101168b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
101268b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
101368b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
101468b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
101568b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
101668b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
101768b8534bSLuigi Rizzo 	}
101868b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
101968b8534bSLuigi Rizzo }
102068b8534bSLuigi Rizzo 
102168b8534bSLuigi Rizzo 
102268b8534bSLuigi Rizzo /*
102368b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
102468b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
102568b8534bSLuigi Rizzo  */
102668b8534bSLuigi Rizzo static int
102768b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
102868b8534bSLuigi Rizzo {
102968b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
103068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
103168b8534bSLuigi Rizzo 	void *adapter = na->ifp->if_softc;	/* shorthand */
103268b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
103368b8534bSLuigi Rizzo 	/* first time we don't lock */
103468b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
103568b8534bSLuigi Rizzo 
103668b8534bSLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= na->num_queues) {
103768b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
103868b8534bSLuigi Rizzo 		return (EINVAL);
103968b8534bSLuigi Rizzo 	}
104068b8534bSLuigi Rizzo 	if (need_lock)
104168b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
104268b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
104368b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
104468b8534bSLuigi Rizzo 		priv->np_qfirst = na->num_queues;
104568b8534bSLuigi Rizzo 		priv->np_qlast = na->num_queues + 1;
104668b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
104768b8534bSLuigi Rizzo 		priv->np_qfirst = i;
104868b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
104968b8534bSLuigi Rizzo 	} else {
105068b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
105168b8534bSLuigi Rizzo 		priv->np_qlast = na->num_queues;
105268b8534bSLuigi Rizzo 	}
105368b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
105468b8534bSLuigi Rizzo 	if (need_lock)
105568b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
105668b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
105768b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
105868b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
105968b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
106068b8534bSLuigi Rizzo 			priv->np_qfirst);
106168b8534bSLuigi Rizzo 	else
106268b8534bSLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname,
106368b8534bSLuigi Rizzo 			priv->np_qlast);
106468b8534bSLuigi Rizzo 	return 0;
106568b8534bSLuigi Rizzo }
106668b8534bSLuigi Rizzo 
106768b8534bSLuigi Rizzo /*
106868b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
106968b8534bSLuigi Rizzo  *
107068b8534bSLuigi Rizzo  * Following a list of accepted commands:
107168b8534bSLuigi Rizzo  * - NIOCGINFO
107268b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
107368b8534bSLuigi Rizzo  * - NIOCREGIF
107468b8534bSLuigi Rizzo  * - NIOCUNREGIF
107568b8534bSLuigi Rizzo  * - NIOCTXSYNC
107668b8534bSLuigi Rizzo  * - NIOCRXSYNC
107768b8534bSLuigi Rizzo  *
107868b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
107968b8534bSLuigi Rizzo  */
108068b8534bSLuigi Rizzo static int
108168b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data,
1082506cc70cSLuigi Rizzo 	__unused int fflag, struct thread *td)
108368b8534bSLuigi Rizzo {
108468b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
108568b8534bSLuigi Rizzo 	struct ifnet *ifp;
108668b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
108768b8534bSLuigi Rizzo 	struct netmap_adapter *na;
108868b8534bSLuigi Rizzo 	void *adapter;
108968b8534bSLuigi Rizzo 	int error;
109068b8534bSLuigi Rizzo 	u_int i;
109168b8534bSLuigi Rizzo 	struct netmap_if *nifp;
109268b8534bSLuigi Rizzo 
1093506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1094506cc70cSLuigi Rizzo 
109568b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
1096506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
1097506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
109868b8534bSLuigi Rizzo 		return (error);
1099506cc70cSLuigi Rizzo 	}
110068b8534bSLuigi Rizzo 
110168b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
110268b8534bSLuigi Rizzo 	switch (cmd) {
110368b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
110468b8534bSLuigi Rizzo 		/* memsize is always valid */
110568b8534bSLuigi Rizzo 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
110668b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
110768b8534bSLuigi Rizzo 		nmr->nr_numrings = 0;
110868b8534bSLuigi Rizzo 		nmr->nr_numslots = 0;
110968b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
111068b8534bSLuigi Rizzo 			break;
111168b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
111268b8534bSLuigi Rizzo 		if (error)
111368b8534bSLuigi Rizzo 			break;
111468b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
111568b8534bSLuigi Rizzo 		nmr->nr_numrings = na->num_queues;
111668b8534bSLuigi Rizzo 		nmr->nr_numslots = na->num_tx_desc;
111768b8534bSLuigi Rizzo 		if_rele(ifp);	/* return the refcount */
111868b8534bSLuigi Rizzo 		break;
111968b8534bSLuigi Rizzo 
112068b8534bSLuigi Rizzo 	case NIOCREGIF:
1121506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
1122506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
1123506cc70cSLuigi Rizzo 			break;
1124506cc70cSLuigi Rizzo 		}
112568b8534bSLuigi Rizzo 		/* find the interface and a reference */
112668b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
112768b8534bSLuigi Rizzo 		if (error)
112868b8534bSLuigi Rizzo 			break;
112968b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
113068b8534bSLuigi Rizzo 		adapter = na->ifp->if_softc;	/* shorthand */
113168b8534bSLuigi Rizzo 		/*
113268b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
113368b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
113468b8534bSLuigi Rizzo 		 */
113568b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
113668b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
113768b8534bSLuigi Rizzo 		if (priv == NULL) {
113868b8534bSLuigi Rizzo 			error = ENOMEM;
113968b8534bSLuigi Rizzo 			if_rele(ifp);   /* return the refcount */
114068b8534bSLuigi Rizzo 			break;
114168b8534bSLuigi Rizzo 		}
114268b8534bSLuigi Rizzo 
114368b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
114468b8534bSLuigi Rizzo 			na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
114568b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
114668b8534bSLuigi Rizzo 				break;
114768b8534bSLuigi Rizzo 			na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
114868b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
114968b8534bSLuigi Rizzo 		}
115068b8534bSLuigi Rizzo 		if (i == 0) {
115168b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
115268b8534bSLuigi Rizzo 			error = EINVAL;
115368b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
115468b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
115568b8534bSLuigi Rizzo 			break;
115668b8534bSLuigi Rizzo 		}
115768b8534bSLuigi Rizzo 
115868b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
115968b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
116068b8534bSLuigi Rizzo 		if (error)
116168b8534bSLuigi Rizzo 			goto error;
116268b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
116368b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
116468b8534bSLuigi Rizzo 			error = ENOMEM;
116568b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
116668b8534bSLuigi Rizzo 			/* was already set */
116768b8534bSLuigi Rizzo 		} else {
116868b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
116968b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
117068b8534bSLuigi Rizzo 			 */
117168b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
1172*5819da83SLuigi Rizzo 			if (error)
1173*5819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
117468b8534bSLuigi Rizzo 		}
117568b8534bSLuigi Rizzo 
117668b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
117768b8534bSLuigi Rizzo error:
117868b8534bSLuigi Rizzo 			na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
117968b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
1180*5819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
1181*5819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
118268b8534bSLuigi Rizzo 			break;
118368b8534bSLuigi Rizzo 		}
118468b8534bSLuigi Rizzo 
118568b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
118668b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
118768b8534bSLuigi Rizzo 
118868b8534bSLuigi Rizzo 		if (error != 0) {
118968b8534bSLuigi Rizzo 			/* could not assign the private storage for the
119068b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
119168b8534bSLuigi Rizzo 			 */
119268b8534bSLuigi Rizzo 			netmap_dtor(priv);
119368b8534bSLuigi Rizzo 			break;
119468b8534bSLuigi Rizzo 		}
119568b8534bSLuigi Rizzo 
119668b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
119768b8534bSLuigi Rizzo 		nmr->nr_numrings = na->num_queues;
119868b8534bSLuigi Rizzo 		nmr->nr_numslots = na->num_tx_desc;
119968b8534bSLuigi Rizzo 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
1200446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
120168b8534bSLuigi Rizzo 		break;
120268b8534bSLuigi Rizzo 
120368b8534bSLuigi Rizzo 	case NIOCUNREGIF:
1204506cc70cSLuigi Rizzo 		if (priv == NULL) {
1205506cc70cSLuigi Rizzo 			error = ENXIO;
1206506cc70cSLuigi Rizzo 			break;
1207506cc70cSLuigi Rizzo 		}
120868b8534bSLuigi Rizzo 
120968b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
121068b8534bSLuigi Rizzo 		   destructor of the private data. */
121168b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
121268b8534bSLuigi Rizzo 		break;
121368b8534bSLuigi Rizzo 
121468b8534bSLuigi Rizzo 	case NIOCTXSYNC:
121568b8534bSLuigi Rizzo         case NIOCRXSYNC:
1216506cc70cSLuigi Rizzo 		if (priv == NULL) {
1217506cc70cSLuigi Rizzo 			error = ENXIO;
1218506cc70cSLuigi Rizzo 			break;
1219506cc70cSLuigi Rizzo 		}
122068b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
122168b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
122268b8534bSLuigi Rizzo 		adapter = ifp->if_softc;	/* shorthand */
122368b8534bSLuigi Rizzo 
122468b8534bSLuigi Rizzo 		if (priv->np_qfirst == na->num_queues) {
122568b8534bSLuigi Rizzo 			/* queues to/from host */
122668b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
122768b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
122868b8534bSLuigi Rizzo 			else
122968b8534bSLuigi Rizzo 				netmap_sync_from_host(na, NULL);
1230506cc70cSLuigi Rizzo 			break;
123168b8534bSLuigi Rizzo 		}
123268b8534bSLuigi Rizzo 
123368b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
123468b8534bSLuigi Rizzo 		    if (cmd == NIOCTXSYNC) {
123568b8534bSLuigi Rizzo 			struct netmap_kring *kring = &na->tx_rings[i];
123668b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
123768b8534bSLuigi Rizzo 				D("sync tx ring %d cur %d hwcur %d",
123868b8534bSLuigi Rizzo 					i, kring->ring->cur,
123968b8534bSLuigi Rizzo 					kring->nr_hwcur);
124068b8534bSLuigi Rizzo                         na->nm_txsync(adapter, i, 1 /* do lock */);
124168b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
124268b8534bSLuigi Rizzo 				D("after sync tx ring %d cur %d hwcur %d",
124368b8534bSLuigi Rizzo 					i, kring->ring->cur,
124468b8534bSLuigi Rizzo 					kring->nr_hwcur);
124568b8534bSLuigi Rizzo 		    } else {
124668b8534bSLuigi Rizzo 			na->nm_rxsync(adapter, i, 1 /* do lock */);
124768b8534bSLuigi Rizzo 			microtime(&na->rx_rings[i].ring->ts);
124868b8534bSLuigi Rizzo 		    }
124968b8534bSLuigi Rizzo 		}
125068b8534bSLuigi Rizzo 
125168b8534bSLuigi Rizzo                 break;
125268b8534bSLuigi Rizzo 
125368b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
125468b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
125568b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
125668b8534bSLuigi Rizzo 	case BIOCSSEESENT:
125768b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
125868b8534bSLuigi Rizzo 		break;
125968b8534bSLuigi Rizzo 
126068b8534bSLuigi Rizzo 	default:
126168b8534bSLuigi Rizzo 	    {
126268b8534bSLuigi Rizzo 		/*
126368b8534bSLuigi Rizzo 		 * allow device calls
126468b8534bSLuigi Rizzo 		 */
126568b8534bSLuigi Rizzo 		struct socket so;
126668b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
126768b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
126868b8534bSLuigi Rizzo 		if (error)
126968b8534bSLuigi Rizzo 			break;
127068b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
127168b8534bSLuigi Rizzo 		// so->so_proto not null.
127268b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
127368b8534bSLuigi Rizzo 		if_rele(ifp);
127468b8534bSLuigi Rizzo 	    }
127568b8534bSLuigi Rizzo 	}
127668b8534bSLuigi Rizzo 
1277506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
127868b8534bSLuigi Rizzo 	return (error);
127968b8534bSLuigi Rizzo }
128068b8534bSLuigi Rizzo 
128168b8534bSLuigi Rizzo 
128268b8534bSLuigi Rizzo /*
128368b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
128468b8534bSLuigi Rizzo  *
128568b8534bSLuigi Rizzo  * Can be called for one or more queues.
128668b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
128768b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
128868b8534bSLuigi Rizzo  * selfd or on the global one.
128968b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
129068b8534bSLuigi Rizzo  * are done through callbacks.
129168b8534bSLuigi Rizzo  */
129268b8534bSLuigi Rizzo static int
129368b8534bSLuigi Rizzo netmap_poll(__unused struct cdev *dev, int events, struct thread *td)
129468b8534bSLuigi Rizzo {
129568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
129668b8534bSLuigi Rizzo 	struct netmap_adapter *na;
129768b8534bSLuigi Rizzo 	struct ifnet *ifp;
129868b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1299d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
130068b8534bSLuigi Rizzo 	void *adapter;
1301bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
130268b8534bSLuigi Rizzo 
130368b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
130468b8534bSLuigi Rizzo 		return POLLERR;
130568b8534bSLuigi Rizzo 
130668b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
130768b8534bSLuigi Rizzo 	// XXX check for deleting() ?
130868b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
130968b8534bSLuigi Rizzo 		return POLLERR;
131068b8534bSLuigi Rizzo 
131168b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
131268b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
131368b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
131468b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
131568b8534bSLuigi Rizzo 
131668b8534bSLuigi Rizzo 	adapter = ifp->if_softc;
131768b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
131868b8534bSLuigi Rizzo 
131968b8534bSLuigi Rizzo 	/* how many queues we are scanning */
132068b8534bSLuigi Rizzo 	i = priv->np_qfirst;
132168b8534bSLuigi Rizzo 	if (i == na->num_queues) { /* from/to host */
132268b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
132368b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
132468b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
132568b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
132668b8534bSLuigi Rizzo 			revents |= want_tx;
132768b8534bSLuigi Rizzo 		}
132868b8534bSLuigi Rizzo 		if (want_rx) {
132968b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
133068b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
133168b8534bSLuigi Rizzo 				netmap_sync_from_host(na, td);
133268b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
133368b8534bSLuigi Rizzo 				revents |= want_rx;
133468b8534bSLuigi Rizzo 			}
133568b8534bSLuigi Rizzo 		}
133668b8534bSLuigi Rizzo 		return (revents);
133768b8534bSLuigi Rizzo 	}
133868b8534bSLuigi Rizzo 
133968b8534bSLuigi Rizzo 	/*
134068b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
134168b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
134268b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
134368b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
134468b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
134568b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
134668b8534bSLuigi Rizzo 	 * has more than one ring.
134768b8534bSLuigi Rizzo 	 *
134868b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
134968b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
135068b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
135168b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
135268b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
135368b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
135468b8534bSLuigi Rizzo 	 *
135568b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
135668b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
135768b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
135868b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
135968b8534bSLuigi Rizzo 	 */
136068b8534bSLuigi Rizzo 	check_all = (i + 1 != priv->np_qlast);
136168b8534bSLuigi Rizzo 
136268b8534bSLuigi Rizzo 	/*
136368b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
136468b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
136568b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
136668b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
136768b8534bSLuigi Rizzo 	 *
136868b8534bSLuigi Rizzo 	 * It has three possible states:
136968b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
137068b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
137168b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
137268b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
137368b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
137468b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
137568b8534bSLuigi Rizzo 	 */
1376d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
137768b8534bSLuigi Rizzo 	/*
137868b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
137968b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
138068b8534bSLuigi Rizzo 	 * routines.
138168b8534bSLuigi Rizzo 	 */
138268b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; want_rx && i < priv->np_qlast; i++) {
138368b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
138468b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
138568b8534bSLuigi Rizzo 				revents |= want_rx;
138668b8534bSLuigi Rizzo 				want_rx = 0;	/* also breaks the loop */
138768b8534bSLuigi Rizzo 			}
138868b8534bSLuigi Rizzo 		}
138968b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; want_tx && i < priv->np_qlast; i++) {
139068b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
139168b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
139268b8534bSLuigi Rizzo 				revents |= want_tx;
139368b8534bSLuigi Rizzo 				want_tx = 0;	/* also breaks the loop */
139468b8534bSLuigi Rizzo 			}
139568b8534bSLuigi Rizzo 		}
139668b8534bSLuigi Rizzo 
139768b8534bSLuigi Rizzo 	/*
139868b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
139968b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
140068b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
140168b8534bSLuigi Rizzo 	 */
140268b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
140368b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
140468b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
1405*5819da83SLuigi Rizzo 			/*
1406*5819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
1407*5819da83SLuigi Rizzo 			 * (we have already done a successful sync on
1408*5819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
1409*5819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
1410*5819da83SLuigi Rizzo 			 */
141168b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
141268b8534bSLuigi Rizzo 				continue;
141368b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
141468b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
141568b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
141668b8534bSLuigi Rizzo 			}
141768b8534bSLuigi Rizzo 			if (na->separate_locks)
141868b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_TX_LOCK, i);
141968b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
142068b8534bSLuigi Rizzo 				D("send %d on %s %d",
142168b8534bSLuigi Rizzo 					kring->ring->cur,
142268b8534bSLuigi Rizzo 					ifp->if_xname, i);
142368b8534bSLuigi Rizzo 			if (na->nm_txsync(adapter, i, 0 /* no lock */))
142468b8534bSLuigi Rizzo 				revents |= POLLERR;
142568b8534bSLuigi Rizzo 
1426*5819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
142768b8534bSLuigi Rizzo 			if (want_tx) {
142868b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
142968b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
143068b8534bSLuigi Rizzo 					 * starvation.
143168b8534bSLuigi Rizzo 					 */
143268b8534bSLuigi Rizzo 					revents |= want_tx;
143368b8534bSLuigi Rizzo 					want_tx = 0;
143468b8534bSLuigi Rizzo 				} else if (!check_all)
143568b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
143668b8534bSLuigi Rizzo 			}
143768b8534bSLuigi Rizzo 			if (na->separate_locks)
143868b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_TX_UNLOCK, i);
143968b8534bSLuigi Rizzo 		}
144068b8534bSLuigi Rizzo 	}
144168b8534bSLuigi Rizzo 
144268b8534bSLuigi Rizzo 	/*
144368b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
144468b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
144568b8534bSLuigi Rizzo 	 */
144668b8534bSLuigi Rizzo 	if (want_rx) {
144768b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
144868b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
144968b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
145068b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
145168b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
145268b8534bSLuigi Rizzo 			}
145368b8534bSLuigi Rizzo 			if (na->separate_locks)
145468b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_RX_LOCK, i);
145568b8534bSLuigi Rizzo 
145668b8534bSLuigi Rizzo 			if (na->nm_rxsync(adapter, i, 0 /* no lock */))
145768b8534bSLuigi Rizzo 				revents |= POLLERR;
1458*5819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
1459*5819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
146068b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
1461*5819da83SLuigi Rizzo 			}
146268b8534bSLuigi Rizzo 
146368b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
146468b8534bSLuigi Rizzo 				revents |= want_rx;
146568b8534bSLuigi Rizzo 			else if (!check_all)
146668b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
146768b8534bSLuigi Rizzo 			if (na->separate_locks)
146868b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_RX_UNLOCK, i);
146968b8534bSLuigi Rizzo 		}
147068b8534bSLuigi Rizzo 	}
147168b8534bSLuigi Rizzo 	if (check_all && revents == 0) {
147268b8534bSLuigi Rizzo 		i = na->num_queues + 1; /* the global queue */
147368b8534bSLuigi Rizzo 		if (want_tx)
147468b8534bSLuigi Rizzo 			selrecord(td, &na->tx_rings[i].si);
147568b8534bSLuigi Rizzo 		if (want_rx)
147668b8534bSLuigi Rizzo 			selrecord(td, &na->rx_rings[i].si);
147768b8534bSLuigi Rizzo 	}
147868b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
147968b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
148068b8534bSLuigi Rizzo 
148168b8534bSLuigi Rizzo 	return (revents);
148268b8534bSLuigi Rizzo }
148368b8534bSLuigi Rizzo 
148468b8534bSLuigi Rizzo /*------- driver support routines ------*/
148568b8534bSLuigi Rizzo 
148668b8534bSLuigi Rizzo /*
148768b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
148868b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
148968b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
149068b8534bSLuigi Rizzo  * of hardware rings):
149168b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
149268b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
149368b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
149468b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
149568b8534bSLuigi Rizzo  */
149668b8534bSLuigi Rizzo int
149768b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
149868b8534bSLuigi Rizzo {
149968b8534bSLuigi Rizzo 	int n = num_queues + 2;
150068b8534bSLuigi Rizzo 	int size = sizeof(*na) + 2 * n * sizeof(struct netmap_kring);
150168b8534bSLuigi Rizzo 	void *buf;
150268b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
150368b8534bSLuigi Rizzo 
150468b8534bSLuigi Rizzo 	if (ifp == NULL) {
150568b8534bSLuigi Rizzo 		D("ifp not set, giving up");
150668b8534bSLuigi Rizzo 		return EINVAL;
150768b8534bSLuigi Rizzo 	}
150868b8534bSLuigi Rizzo 	na->refcount = 0;
150968b8534bSLuigi Rizzo 	na->num_queues = num_queues;
151068b8534bSLuigi Rizzo 
151168b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
151268b8534bSLuigi Rizzo 	if (buf) {
1513d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
151468b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
151568b8534bSLuigi Rizzo 		na->rx_rings = na->tx_rings + n;
1516*5819da83SLuigi Rizzo 		na->buff_size = NETMAP_BUF_SIZE;
151768b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
151868b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
151968b8534bSLuigi Rizzo 	}
152068b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
152168b8534bSLuigi Rizzo 
152268b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
152368b8534bSLuigi Rizzo }
152468b8534bSLuigi Rizzo 
152568b8534bSLuigi Rizzo 
152668b8534bSLuigi Rizzo /*
152768b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
152868b8534bSLuigi Rizzo  * object.
152968b8534bSLuigi Rizzo  */
153068b8534bSLuigi Rizzo void
153168b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
153268b8534bSLuigi Rizzo {
153368b8534bSLuigi Rizzo 	u_int i;
153468b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
153568b8534bSLuigi Rizzo 
153668b8534bSLuigi Rizzo 	if (!na)
153768b8534bSLuigi Rizzo 		return;
153868b8534bSLuigi Rizzo 
153968b8534bSLuigi Rizzo 	for (i = 0; i < na->num_queues + 2; i++) {
154068b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
154168b8534bSLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
154268b8534bSLuigi Rizzo 	}
154368b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1544d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
154568b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
154668b8534bSLuigi Rizzo }
154768b8534bSLuigi Rizzo 
154868b8534bSLuigi Rizzo 
154968b8534bSLuigi Rizzo /*
155002ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
155102ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
155268b8534bSLuigi Rizzo  * We are not locked when called.
155368b8534bSLuigi Rizzo  */
155468b8534bSLuigi Rizzo int
155568b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
155668b8534bSLuigi Rizzo {
155768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
155802ad4083SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_queues];
155902ad4083SLuigi Rizzo 	u_int i, len = m->m_pkthdr.len;
156002ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
156168b8534bSLuigi Rizzo 	struct netmap_slot *slot;
156268b8534bSLuigi Rizzo 
156368b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
156468b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
156568b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
156668b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_LOCK, 0);
156702ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
156868b8534bSLuigi Rizzo 		D("stack ring %s full\n", ifp->if_xname);
156968b8534bSLuigi Rizzo 		goto done;	/* no space */
157068b8534bSLuigi Rizzo 	}
157168b8534bSLuigi Rizzo 	if (len > na->buff_size) {
157268b8534bSLuigi Rizzo 		D("drop packet size %d > %d", len, na->buff_size);
157368b8534bSLuigi Rizzo 		goto done;	/* too long for us */
157468b8534bSLuigi Rizzo 	}
157568b8534bSLuigi Rizzo 
157668b8534bSLuigi Rizzo 	/* compute the insert position */
157768b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
157802ad4083SLuigi Rizzo 	if (i > lim)
157902ad4083SLuigi Rizzo 		i -= lim + 1;
158068b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
158168b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
158268b8534bSLuigi Rizzo 	slot->len = len;
158368b8534bSLuigi Rizzo 	kring->nr_hwavail++;
158468b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
158568b8534bSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_queues);
158668b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
158768b8534bSLuigi Rizzo 	error = 0;
158868b8534bSLuigi Rizzo done:
158968b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
159068b8534bSLuigi Rizzo 
159168b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
159268b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
159368b8534bSLuigi Rizzo 	 * only when really necessary.
159468b8534bSLuigi Rizzo 	 */
159568b8534bSLuigi Rizzo 	m_freem(m);
159668b8534bSLuigi Rizzo 
159768b8534bSLuigi Rizzo 	return (error);
159868b8534bSLuigi Rizzo }
159968b8534bSLuigi Rizzo 
160068b8534bSLuigi Rizzo 
160168b8534bSLuigi Rizzo /*
160268b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
160368b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
160468b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
160568b8534bSLuigi Rizzo  */
160668b8534bSLuigi Rizzo struct netmap_slot *
160768b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
160868b8534bSLuigi Rizzo 	u_int new_cur)
160968b8534bSLuigi Rizzo {
161068b8534bSLuigi Rizzo 	struct netmap_kring *kring;
161168b8534bSLuigi Rizzo 	struct netmap_ring *ring;
1612506cc70cSLuigi Rizzo 	int new_hwofs, lim;
161368b8534bSLuigi Rizzo 
161468b8534bSLuigi Rizzo 	if (na == NULL)
161568b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
161668b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
161768b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
161868b8534bSLuigi Rizzo 	kring = tx == NR_TX ?  na->tx_rings + n : na->rx_rings + n;
161968b8534bSLuigi Rizzo 	ring = kring->ring;
1620506cc70cSLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
162168b8534bSLuigi Rizzo 
1622506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1623506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
1624506cc70cSLuigi Rizzo 	else
1625506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
1626506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1627506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1628506cc70cSLuigi Rizzo 
1629506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1630506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1631506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1632506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1633506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1634506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1635506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1636506cc70cSLuigi Rizzo 
163768b8534bSLuigi Rizzo 	/*
1638506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1639506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
164068b8534bSLuigi Rizzo 	 */
164168b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
164268b8534bSLuigi Rizzo 	selwakeuppri(&kring[na->num_queues + 1 - n].si, PI_NET);
164368b8534bSLuigi Rizzo 	return kring->ring->slot;
164468b8534bSLuigi Rizzo }
164568b8534bSLuigi Rizzo 
164668b8534bSLuigi Rizzo 
164768b8534bSLuigi Rizzo /*
164868b8534bSLuigi Rizzo  * Module loader.
164968b8534bSLuigi Rizzo  *
165068b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
165168b8534bSLuigi Rizzo  * variables.
165268b8534bSLuigi Rizzo  *
165368b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
165468b8534bSLuigi Rizzo  */
165568b8534bSLuigi Rizzo static int
165668b8534bSLuigi Rizzo netmap_init(void)
165768b8534bSLuigi Rizzo {
165868b8534bSLuigi Rizzo 	int error;
165968b8534bSLuigi Rizzo 
166068b8534bSLuigi Rizzo 
166168b8534bSLuigi Rizzo 	error = netmap_memory_init();
166268b8534bSLuigi Rizzo 	if (error != 0) {
166368b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
166468b8534bSLuigi Rizzo 		return (error);
166568b8534bSLuigi Rizzo 	}
166668b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
166785df3791SLuigi Rizzo 		(int)(netmap_mem_d->nm_totalsize >> 20));
166868b8534bSLuigi Rizzo 
166968b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
167068b8534bSLuigi Rizzo 			      "netmap");
167168b8534bSLuigi Rizzo 
167268b8534bSLuigi Rizzo 	return (0);
167368b8534bSLuigi Rizzo }
167468b8534bSLuigi Rizzo 
167568b8534bSLuigi Rizzo 
167668b8534bSLuigi Rizzo /*
167768b8534bSLuigi Rizzo  * Module unloader.
167868b8534bSLuigi Rizzo  *
167968b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
168068b8534bSLuigi Rizzo  */
168168b8534bSLuigi Rizzo static void
168268b8534bSLuigi Rizzo netmap_fini(void)
168368b8534bSLuigi Rizzo {
168468b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
168568b8534bSLuigi Rizzo 
168668b8534bSLuigi Rizzo 	netmap_memory_fini();
168768b8534bSLuigi Rizzo 
168868b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
168968b8534bSLuigi Rizzo }
169068b8534bSLuigi Rizzo 
169168b8534bSLuigi Rizzo 
169268b8534bSLuigi Rizzo /*
169368b8534bSLuigi Rizzo  * Kernel entry point.
169468b8534bSLuigi Rizzo  *
169568b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
169668b8534bSLuigi Rizzo  *
169768b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
169868b8534bSLuigi Rizzo  */
169968b8534bSLuigi Rizzo static int
170068b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
170168b8534bSLuigi Rizzo {
170268b8534bSLuigi Rizzo 	int error = 0;
170368b8534bSLuigi Rizzo 
170468b8534bSLuigi Rizzo 	switch (event) {
170568b8534bSLuigi Rizzo 	case MOD_LOAD:
170668b8534bSLuigi Rizzo 		error = netmap_init();
170768b8534bSLuigi Rizzo 		break;
170868b8534bSLuigi Rizzo 
170968b8534bSLuigi Rizzo 	case MOD_UNLOAD:
171068b8534bSLuigi Rizzo 		netmap_fini();
171168b8534bSLuigi Rizzo 		break;
171268b8534bSLuigi Rizzo 
171368b8534bSLuigi Rizzo 	default:
171468b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
171568b8534bSLuigi Rizzo 		break;
171668b8534bSLuigi Rizzo 	}
171768b8534bSLuigi Rizzo 
171868b8534bSLuigi Rizzo 	return (error);
171968b8534bSLuigi Rizzo }
172068b8534bSLuigi Rizzo 
172168b8534bSLuigi Rizzo 
172268b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
1723