xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision 446ee301)
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);
9568b8534bSLuigi Rizzo 
9668b8534bSLuigi Rizzo /*
9768b8534bSLuigi Rizzo  * Default amount of memory pre-allocated by the module.
9868b8534bSLuigi Rizzo  * We start with a large size and then shrink our demand
9968b8534bSLuigi Rizzo  * according to what is avalable when the module is loaded.
10068b8534bSLuigi Rizzo  * At the moment the block is contiguous, but we can easily
10168b8534bSLuigi Rizzo  * restrict our demand to smaller units (16..64k)
10268b8534bSLuigi Rizzo  */
10368b8534bSLuigi Rizzo #define NETMAP_MEMORY_SIZE (64 * 1024 * PAGE_SIZE)
10468b8534bSLuigi Rizzo static void * netmap_malloc(size_t size, const char *msg);
10568b8534bSLuigi Rizzo static void netmap_free(void *addr, const char *msg);
10668b8534bSLuigi Rizzo 
1076e10c8b8SLuigi Rizzo #define netmap_if_malloc(len)   netmap_malloc(len, "nifp")
1086e10c8b8SLuigi Rizzo #define netmap_if_free(v)	netmap_free((v), "nifp")
1096e10c8b8SLuigi Rizzo 
1106e10c8b8SLuigi Rizzo #define netmap_ring_malloc(len) netmap_malloc(len, "ring")
1116e10c8b8SLuigi Rizzo #define netmap_free_rings(na)		\
1126e10c8b8SLuigi Rizzo 	netmap_free((na)->tx_rings[0].ring, "shadow rings");
1136e10c8b8SLuigi Rizzo 
11468b8534bSLuigi Rizzo /*
11568b8534bSLuigi Rizzo  * Allocator for a pool of packet buffers. For each buffer we have
11668b8534bSLuigi Rizzo  * one entry in the bitmap to signal the state. Allocation scans
11768b8534bSLuigi Rizzo  * the bitmap, but since this is done only on attach, we are not
11868b8534bSLuigi Rizzo  * too worried about performance
11968b8534bSLuigi Rizzo  * XXX if we need to allocate small blocks, a translation
12068b8534bSLuigi Rizzo  * table is used both for kernel virtual address and physical
12168b8534bSLuigi Rizzo  * addresses.
12268b8534bSLuigi Rizzo  */
12368b8534bSLuigi Rizzo struct netmap_buf_pool {
12468b8534bSLuigi Rizzo 	u_int total_buffers;	/* total buffers. */
12568b8534bSLuigi Rizzo 	u_int free;
12668b8534bSLuigi Rizzo 	u_int bufsize;
12768b8534bSLuigi Rizzo 	char *base;		/* buffer base address */
12868b8534bSLuigi Rizzo 	uint32_t *bitmap;	/* one bit per buffer, 1 means free */
12968b8534bSLuigi Rizzo };
13068b8534bSLuigi Rizzo struct netmap_buf_pool nm_buf_pool;
13168b8534bSLuigi Rizzo /* XXX move these two vars back into netmap_buf_pool */
13268b8534bSLuigi Rizzo u_int netmap_total_buffers;
1336e10c8b8SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
13468b8534bSLuigi Rizzo 
13568b8534bSLuigi Rizzo /* user-controlled variables */
13668b8534bSLuigi Rizzo int netmap_verbose;
13768b8534bSLuigi Rizzo 
13868b8534bSLuigi Rizzo static int no_timestamp; /* don't timestamp on rxsync */
13968b8534bSLuigi Rizzo 
14068b8534bSLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
14168b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
14268b8534bSLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
14368b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
14468b8534bSLuigi Rizzo     CTLFLAG_RW, &no_timestamp, 0, "no_timestamp");
14568b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, total_buffers,
14668b8534bSLuigi Rizzo     CTLFLAG_RD, &nm_buf_pool.total_buffers, 0, "total_buffers");
14768b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, free_buffers,
14868b8534bSLuigi Rizzo     CTLFLAG_RD, &nm_buf_pool.free, 0, "free_buffers");
14968b8534bSLuigi Rizzo 
15068b8534bSLuigi Rizzo /*
15168b8534bSLuigi Rizzo  * Allocate n buffers from the ring, and fill the slot.
15268b8534bSLuigi Rizzo  * Buffer 0 is the 'junk' buffer.
15368b8534bSLuigi Rizzo  */
15468b8534bSLuigi Rizzo static void
155*446ee301SLuigi Rizzo netmap_new_bufs(struct netmap_if *nifp __unused,
156*446ee301SLuigi Rizzo 		struct netmap_slot *slot, u_int n)
15768b8534bSLuigi Rizzo {
158*446ee301SLuigi Rizzo 	struct netmap_buf_pool *p = &nm_buf_pool;
15968b8534bSLuigi Rizzo 	uint32_t bi = 0;		/* index in the bitmap */
16068b8534bSLuigi Rizzo 	uint32_t mask, j, i = 0;	/* slot counter */
16168b8534bSLuigi Rizzo 
16268b8534bSLuigi Rizzo 	if (n > p->free) {
16368b8534bSLuigi Rizzo 		D("only %d out of %d buffers available", i, n);
16468b8534bSLuigi Rizzo 		return;
16568b8534bSLuigi Rizzo 	}
16668b8534bSLuigi Rizzo 	/* termination is guaranteed by p->free */
16768b8534bSLuigi Rizzo 	while (i < n && p->free > 0) {
16868b8534bSLuigi Rizzo 		uint32_t cur = p->bitmap[bi];
16968b8534bSLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
17068b8534bSLuigi Rizzo 			bi++;
17168b8534bSLuigi Rizzo 			continue;
17268b8534bSLuigi Rizzo 		}
17368b8534bSLuigi Rizzo 		/* locate a slot */
17468b8534bSLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) ;
17568b8534bSLuigi Rizzo 		p->bitmap[bi] &= ~mask;		/* slot in use */
17668b8534bSLuigi Rizzo 		p->free--;
17768b8534bSLuigi Rizzo 		slot[i].buf_idx = bi*32+j;
17868b8534bSLuigi Rizzo 		slot[i].len = p->bufsize;
17968b8534bSLuigi Rizzo 		slot[i].flags = NS_BUF_CHANGED;
18068b8534bSLuigi Rizzo 		i++;
18168b8534bSLuigi Rizzo 	}
18268b8534bSLuigi Rizzo 	ND("allocated %d buffers, %d available", n, p->free);
18368b8534bSLuigi Rizzo }
18468b8534bSLuigi Rizzo 
18568b8534bSLuigi Rizzo 
18668b8534bSLuigi Rizzo static void
187*446ee301SLuigi Rizzo netmap_free_buf(struct netmap_if *nifp __unused, uint32_t i)
18868b8534bSLuigi Rizzo {
189*446ee301SLuigi Rizzo 	struct netmap_buf_pool *p = &nm_buf_pool;
190*446ee301SLuigi Rizzo 
19168b8534bSLuigi Rizzo 	uint32_t pos, mask;
19268b8534bSLuigi Rizzo 	if (i >= p->total_buffers) {
19368b8534bSLuigi Rizzo 		D("invalid free index %d", i);
19468b8534bSLuigi Rizzo 		return;
19568b8534bSLuigi Rizzo 	}
19668b8534bSLuigi Rizzo 	pos = i / 32;
19768b8534bSLuigi Rizzo 	mask = 1 << (i % 32);
19868b8534bSLuigi Rizzo 	if (p->bitmap[pos] & mask) {
19968b8534bSLuigi Rizzo 		D("slot %d already free", i);
20068b8534bSLuigi Rizzo 		return;
20168b8534bSLuigi Rizzo 	}
20268b8534bSLuigi Rizzo 	p->bitmap[pos] |= mask;
20368b8534bSLuigi Rizzo 	p->free++;
20468b8534bSLuigi Rizzo }
20568b8534bSLuigi Rizzo 
20668b8534bSLuigi Rizzo 
20768b8534bSLuigi Rizzo /* Descriptor of the memory objects handled by our memory allocator. */
20868b8534bSLuigi Rizzo struct netmap_mem_obj {
20968b8534bSLuigi Rizzo 	TAILQ_ENTRY(netmap_mem_obj) nmo_next; /* next object in the
21068b8534bSLuigi Rizzo 						 chain. */
21168b8534bSLuigi Rizzo 	int nmo_used; /* flag set on used memory objects. */
21268b8534bSLuigi Rizzo 	size_t nmo_size; /* size of the memory area reserved for the
21368b8534bSLuigi Rizzo 			    object. */
21468b8534bSLuigi Rizzo 	void *nmo_data; /* pointer to the memory area. */
21568b8534bSLuigi Rizzo };
21668b8534bSLuigi Rizzo 
21768b8534bSLuigi Rizzo /* Wrap our memory objects to make them ``chainable``. */
21868b8534bSLuigi Rizzo TAILQ_HEAD(netmap_mem_obj_h, netmap_mem_obj);
21968b8534bSLuigi Rizzo 
22068b8534bSLuigi Rizzo 
22168b8534bSLuigi Rizzo /* Descriptor of our custom memory allocator. */
22268b8534bSLuigi Rizzo struct netmap_mem_d {
22368b8534bSLuigi Rizzo 	struct mtx nm_mtx; /* lock used to handle the chain of memory
22468b8534bSLuigi Rizzo 			      objects. */
22568b8534bSLuigi Rizzo 	struct netmap_mem_obj_h nm_molist; /* list of memory objects */
22668b8534bSLuigi Rizzo 	size_t nm_size; /* total amount of memory used for rings etc. */
22768b8534bSLuigi Rizzo 	size_t nm_totalsize; /* total amount of allocated memory
22868b8534bSLuigi Rizzo 		(the difference is used for buffers) */
22968b8534bSLuigi Rizzo 	size_t nm_buf_start; /* offset of packet buffers.
23068b8534bSLuigi Rizzo 			This is page-aligned. */
23168b8534bSLuigi Rizzo 	size_t nm_buf_len; /* total memory for buffers */
23268b8534bSLuigi Rizzo 	void *nm_buffer; /* pointer to the whole pre-allocated memory
23368b8534bSLuigi Rizzo 			    area. */
23468b8534bSLuigi Rizzo };
23568b8534bSLuigi Rizzo 
23668b8534bSLuigi Rizzo 
23768b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
23868b8534bSLuigi Rizzo struct netmap_priv_d {
23968b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
24068b8534bSLuigi Rizzo 
24168b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
24268b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
24368b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
24468b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
24568b8534bSLuigi Rizzo };
24668b8534bSLuigi Rizzo 
2476e10c8b8SLuigi Rizzo /* Shorthand to compute a netmap interface offset. */
2486e10c8b8SLuigi Rizzo #define netmap_if_offset(v)                                     \
2496e10c8b8SLuigi Rizzo     ((char *) (v) - (char *) netmap_mem_d->nm_buffer)
2506e10c8b8SLuigi Rizzo /* .. and get a physical address given a memory offset */
2516e10c8b8SLuigi Rizzo #define netmap_ofstophys(o)                                     \
2526e10c8b8SLuigi Rizzo     (vtophys(netmap_mem_d->nm_buffer) + (o))
25368b8534bSLuigi Rizzo 
25468b8534bSLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
25568b8534bSLuigi Rizzo static struct netmap_mem_d *netmap_mem_d; /* Our memory allocator. */
25668b8534bSLuigi Rizzo 
25768b8534bSLuigi Rizzo 
25868b8534bSLuigi Rizzo static d_mmap_t netmap_mmap;
25968b8534bSLuigi Rizzo static d_ioctl_t netmap_ioctl;
26068b8534bSLuigi Rizzo static d_poll_t netmap_poll;
26168b8534bSLuigi Rizzo 
26268b8534bSLuigi Rizzo #ifdef NETMAP_KEVENT
26368b8534bSLuigi Rizzo static d_kqfilter_t netmap_kqfilter;
26468b8534bSLuigi Rizzo #endif
26568b8534bSLuigi Rizzo 
26668b8534bSLuigi Rizzo static struct cdevsw netmap_cdevsw = {
26768b8534bSLuigi Rizzo 	.d_version = D_VERSION,
26868b8534bSLuigi Rizzo 	.d_name = "netmap",
26968b8534bSLuigi Rizzo 	.d_mmap = netmap_mmap,
27068b8534bSLuigi Rizzo 	.d_ioctl = netmap_ioctl,
27168b8534bSLuigi Rizzo 	.d_poll = netmap_poll,
27268b8534bSLuigi Rizzo #ifdef NETMAP_KEVENT
27368b8534bSLuigi Rizzo 	.d_kqfilter = netmap_kqfilter,
27468b8534bSLuigi Rizzo #endif
27568b8534bSLuigi Rizzo };
27668b8534bSLuigi Rizzo 
27768b8534bSLuigi Rizzo #ifdef NETMAP_KEVENT
27868b8534bSLuigi Rizzo static int              netmap_kqread(struct knote *, long);
27968b8534bSLuigi Rizzo static int              netmap_kqwrite(struct knote *, long);
28068b8534bSLuigi Rizzo static void             netmap_kqdetach(struct knote *);
28168b8534bSLuigi Rizzo 
28268b8534bSLuigi Rizzo static struct filterops netmap_read_filterops = {
28368b8534bSLuigi Rizzo 	.f_isfd =       1,
28468b8534bSLuigi Rizzo 	.f_attach =     NULL,
28568b8534bSLuigi Rizzo 	.f_detach =     netmap_kqdetach,
28668b8534bSLuigi Rizzo 	.f_event =      netmap_kqread,
28768b8534bSLuigi Rizzo };
28868b8534bSLuigi Rizzo 
28968b8534bSLuigi Rizzo static struct filterops netmap_write_filterops = {
29068b8534bSLuigi Rizzo 	.f_isfd =       1,
29168b8534bSLuigi Rizzo 	.f_attach =     NULL,
29268b8534bSLuigi Rizzo 	.f_detach =     netmap_kqdetach,
29368b8534bSLuigi Rizzo 	.f_event =      netmap_kqwrite,
29468b8534bSLuigi Rizzo };
29568b8534bSLuigi Rizzo 
29668b8534bSLuigi Rizzo /*
29768b8534bSLuigi Rizzo  * support for the kevent() system call.
29868b8534bSLuigi Rizzo  *
29968b8534bSLuigi Rizzo  * This is the kevent filter, and is executed each time a new event
30068b8534bSLuigi Rizzo  * is triggered on the device. This function execute some operation
30168b8534bSLuigi Rizzo  * depending on the received filter.
30268b8534bSLuigi Rizzo  *
30368b8534bSLuigi Rizzo  * The implementation should test the filters and should implement
30468b8534bSLuigi Rizzo  * filter operations we are interested on (a full list in /sys/event.h).
30568b8534bSLuigi Rizzo  *
30668b8534bSLuigi Rizzo  * On a match we should:
30768b8534bSLuigi Rizzo  * - set kn->kn_fop
30868b8534bSLuigi Rizzo  * - set kn->kn_hook
30968b8534bSLuigi Rizzo  * - call knlist_add() to deliver the event to the application.
31068b8534bSLuigi Rizzo  *
31168b8534bSLuigi Rizzo  * Return 0 if the event should be delivered to the application.
31268b8534bSLuigi Rizzo  */
31368b8534bSLuigi Rizzo static int
31468b8534bSLuigi Rizzo netmap_kqfilter(struct cdev *dev, struct knote *kn)
31568b8534bSLuigi Rizzo {
31668b8534bSLuigi Rizzo 	/* declare variables needed to read/write */
31768b8534bSLuigi Rizzo 
31868b8534bSLuigi Rizzo 	switch(kn->kn_filter) {
31968b8534bSLuigi Rizzo 	case EVFILT_READ:
32068b8534bSLuigi Rizzo 		if (netmap_verbose)
32168b8534bSLuigi Rizzo 			D("%s kqfilter: EVFILT_READ" ifp->if_xname);
32268b8534bSLuigi Rizzo 
32368b8534bSLuigi Rizzo 		/* read operations */
32468b8534bSLuigi Rizzo 		kn->kn_fop = &netmap_read_filterops;
32568b8534bSLuigi Rizzo 		break;
32668b8534bSLuigi Rizzo 
32768b8534bSLuigi Rizzo 	case EVFILT_WRITE:
32868b8534bSLuigi Rizzo 		if (netmap_verbose)
32968b8534bSLuigi Rizzo 			D("%s kqfilter: EVFILT_WRITE" ifp->if_xname);
33068b8534bSLuigi Rizzo 
33168b8534bSLuigi Rizzo 		/* write operations */
33268b8534bSLuigi Rizzo 		kn->kn_fop = &netmap_write_filterops;
33368b8534bSLuigi Rizzo 		break;
33468b8534bSLuigi Rizzo 
33568b8534bSLuigi Rizzo 	default:
33668b8534bSLuigi Rizzo 		if (netmap_verbose)
33768b8534bSLuigi Rizzo 			D("%s kqfilter: invalid filter" ifp->if_xname);
33868b8534bSLuigi Rizzo 		return(EINVAL);
33968b8534bSLuigi Rizzo 	}
34068b8534bSLuigi Rizzo 
34168b8534bSLuigi Rizzo 	kn->kn_hook = 0;//
34268b8534bSLuigi Rizzo 	knlist_add(&netmap_sc->tun_rsel.si_note, kn, 0);
34368b8534bSLuigi Rizzo 
34468b8534bSLuigi Rizzo 	return (0);
34568b8534bSLuigi Rizzo }
34668b8534bSLuigi Rizzo #endif /* NETMAP_KEVENT */
34768b8534bSLuigi Rizzo 
34868b8534bSLuigi Rizzo /*
34968b8534bSLuigi Rizzo  * File descriptor's private data destructor.
35068b8534bSLuigi Rizzo  *
35168b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
35268b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
35368b8534bSLuigi Rizzo  */
35468b8534bSLuigi Rizzo static void
35568b8534bSLuigi Rizzo netmap_dtor(void *data)
35668b8534bSLuigi Rizzo {
35768b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
35868b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
35968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
36068b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
36168b8534bSLuigi Rizzo 
36268b8534bSLuigi Rizzo 	if (0)
36368b8534bSLuigi Rizzo 	    printf("%s starting for %p ifp %p\n", __FUNCTION__, priv,
36468b8534bSLuigi Rizzo 		priv ? priv->np_ifp : NULL);
36568b8534bSLuigi Rizzo 
36668b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_LOCK, 0);
36768b8534bSLuigi Rizzo 
36868b8534bSLuigi Rizzo 	na->refcount--;
36968b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
37068b8534bSLuigi Rizzo 		u_int i;
37168b8534bSLuigi Rizzo 
37268b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
37368b8534bSLuigi Rizzo 		/*
37468b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
37568b8534bSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_CORE_LOCK.
37668b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
37768b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
37868b8534bSLuigi Rizzo 		 * device is dying.
37968b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
38068b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
38168b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
38268b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
38368b8534bSLuigi Rizzo 		 * they cannot run.
38468b8534bSLuigi Rizzo 		 */
38568b8534bSLuigi Rizzo 		na->nm_lock(ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
38668b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
38768b8534bSLuigi Rizzo 		na->nm_lock(ifp->if_softc, NETMAP_CORE_LOCK, 0);
38868b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
38968b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
39068b8534bSLuigi Rizzo 		 * then return POLLERR
39168b8534bSLuigi Rizzo 		 */
39268b8534bSLuigi Rizzo 		for (i = 0; i < na->num_queues + 2; i++) {
39368b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
39468b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
39568b8534bSLuigi Rizzo 		}
39668b8534bSLuigi Rizzo 		/* release all buffers */
39768b8534bSLuigi Rizzo 		NMA_LOCK();
39868b8534bSLuigi Rizzo 		for (i = 0; i < na->num_queues + 1; i++) {
39968b8534bSLuigi Rizzo 			int j, lim;
40068b8534bSLuigi Rizzo 			struct netmap_ring *ring;
40168b8534bSLuigi Rizzo 
40268b8534bSLuigi Rizzo 			ND("tx queue %d", i);
40368b8534bSLuigi Rizzo 			ring = na->tx_rings[i].ring;
40468b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
40568b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
406*446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
40768b8534bSLuigi Rizzo 
40868b8534bSLuigi Rizzo 			ND("rx queue %d", i);
40968b8534bSLuigi Rizzo 			ring = na->rx_rings[i].ring;
41068b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
41168b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
412*446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
41368b8534bSLuigi Rizzo 		}
41468b8534bSLuigi Rizzo 		NMA_UNLOCK();
4156e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
41668b8534bSLuigi Rizzo 		wakeup(na);
41768b8534bSLuigi Rizzo 	}
4186e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
41968b8534bSLuigi Rizzo 
42068b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
42168b8534bSLuigi Rizzo 
42268b8534bSLuigi Rizzo 	if_rele(ifp);
42368b8534bSLuigi Rizzo 
42468b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
42568b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
42668b8534bSLuigi Rizzo }
42768b8534bSLuigi Rizzo 
42868b8534bSLuigi Rizzo 
42968b8534bSLuigi Rizzo 
43068b8534bSLuigi Rizzo /*
43168b8534bSLuigi Rizzo  * Create and return a new ``netmap_if`` object, and possibly also
43268b8534bSLuigi Rizzo  * rings and packet buffors.
43368b8534bSLuigi Rizzo  *
43468b8534bSLuigi Rizzo  * Return NULL on failure.
43568b8534bSLuigi Rizzo  */
43668b8534bSLuigi Rizzo static void *
43768b8534bSLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
43868b8534bSLuigi Rizzo {
43968b8534bSLuigi Rizzo 	struct netmap_if *nifp;
44068b8534bSLuigi Rizzo 	struct netmap_ring *ring;
44168b8534bSLuigi Rizzo 	char *buff;
44268b8534bSLuigi Rizzo 	u_int i, len, ofs;
44368b8534bSLuigi Rizzo 	u_int n = na->num_queues + 1; /* shorthand, include stack queue */
44468b8534bSLuigi Rizzo 
44568b8534bSLuigi Rizzo 	/*
44668b8534bSLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
44768b8534bSLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
44868b8534bSLuigi Rizzo 	 */
44968b8534bSLuigi Rizzo 	len = sizeof(struct netmap_if) + 2 * n * sizeof(ssize_t);
4506e10c8b8SLuigi Rizzo 	nifp = netmap_if_malloc(len);
45168b8534bSLuigi Rizzo 	if (nifp == NULL)
45268b8534bSLuigi Rizzo 		return (NULL);
45368b8534bSLuigi Rizzo 
45468b8534bSLuigi Rizzo 	/* initialize base fields */
45568b8534bSLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_num_queues = na->num_queues;
45668b8534bSLuigi Rizzo 	strncpy(nifp->ni_name, ifname, IFNAMSIZ);
45768b8534bSLuigi Rizzo 
45868b8534bSLuigi Rizzo 	(na->refcount)++;	/* XXX atomic ? we are under lock */
45968b8534bSLuigi Rizzo 	if (na->refcount > 1)
46068b8534bSLuigi Rizzo 		goto final;
46168b8534bSLuigi Rizzo 
46268b8534bSLuigi Rizzo 	/*
46368b8534bSLuigi Rizzo 	 * If this is the first instance, allocate the shadow rings and
46468b8534bSLuigi Rizzo 	 * buffers for this card (one for each hw queue, one for the host).
46568b8534bSLuigi Rizzo 	 * The rings are contiguous, but have variable size.
46668b8534bSLuigi Rizzo 	 * The entire block is reachable at
46768b8534bSLuigi Rizzo 	 *	na->tx_rings[0].ring
46868b8534bSLuigi Rizzo 	 */
46968b8534bSLuigi Rizzo 
47068b8534bSLuigi Rizzo 	len = n * (2 * sizeof(struct netmap_ring) +
47168b8534bSLuigi Rizzo 		  (na->num_tx_desc + na->num_rx_desc) *
47268b8534bSLuigi Rizzo 		   sizeof(struct netmap_slot) );
4736e10c8b8SLuigi Rizzo 	buff = netmap_ring_malloc(len);
47468b8534bSLuigi Rizzo 	if (buff == NULL) {
47568b8534bSLuigi Rizzo 		D("failed to allocate %d bytes for %s shadow ring",
47668b8534bSLuigi Rizzo 			len, ifname);
47768b8534bSLuigi Rizzo error:
47868b8534bSLuigi Rizzo 		(na->refcount)--;
4796e10c8b8SLuigi Rizzo 		netmap_if_free(nifp);
48068b8534bSLuigi Rizzo 		return (NULL);
48168b8534bSLuigi Rizzo 	}
48268b8534bSLuigi Rizzo 	/* do we have the bufers ? we are in need of num_tx_desc buffers for
48368b8534bSLuigi Rizzo 	 * each tx ring and num_tx_desc buffers for each rx ring. */
48468b8534bSLuigi Rizzo 	len = n * (na->num_tx_desc + na->num_rx_desc);
48568b8534bSLuigi Rizzo 	NMA_LOCK();
48668b8534bSLuigi Rizzo 	if (nm_buf_pool.free < len) {
48768b8534bSLuigi Rizzo 		NMA_UNLOCK();
48868b8534bSLuigi Rizzo 		netmap_free(buff, "not enough bufs");
48968b8534bSLuigi Rizzo 		goto error;
49068b8534bSLuigi Rizzo 	}
49168b8534bSLuigi Rizzo 	/*
49268b8534bSLuigi Rizzo 	 * in the kring, store the pointers to the shared rings
49368b8534bSLuigi Rizzo 	 * and initialize the rings. We are under NMA_LOCK().
49468b8534bSLuigi Rizzo 	 */
49568b8534bSLuigi Rizzo 	ofs = 0;
49668b8534bSLuigi Rizzo 	for (i = 0; i < n; i++) {
49768b8534bSLuigi Rizzo 		struct netmap_kring *kring;
49868b8534bSLuigi Rizzo 		int numdesc;
49968b8534bSLuigi Rizzo 
50068b8534bSLuigi Rizzo 		/* Transmit rings */
50168b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
50268b8534bSLuigi Rizzo 		numdesc = na->num_tx_desc;
50368b8534bSLuigi Rizzo 		bzero(kring, sizeof(*kring));
50468b8534bSLuigi Rizzo 		kring->na = na;
50568b8534bSLuigi Rizzo 
50668b8534bSLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
50768b8534bSLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
50868b8534bSLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
50968b8534bSLuigi Rizzo 		ND("txring[%d] at %p ofs %d", i, ring, ring->buf_ofs);
51068b8534bSLuigi Rizzo 		*(int *)(int *)(uintptr_t)&ring->num_slots =
51168b8534bSLuigi Rizzo 			kring->nkr_num_slots = numdesc;
51268b8534bSLuigi Rizzo 
51368b8534bSLuigi Rizzo 		/*
51468b8534bSLuigi Rizzo 		 * IMPORTANT:
51568b8534bSLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
51668b8534bSLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
51768b8534bSLuigi Rizzo 		 * the same only if there are no new transmissions).
51868b8534bSLuigi Rizzo 		 */
51968b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail = numdesc - 1;
52068b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
521*446ee301SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
52268b8534bSLuigi Rizzo 
52368b8534bSLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
52468b8534bSLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
52568b8534bSLuigi Rizzo 
52668b8534bSLuigi Rizzo 		/* Receive rings */
52768b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
52868b8534bSLuigi Rizzo 		numdesc = na->num_rx_desc;
52968b8534bSLuigi Rizzo 		bzero(kring, sizeof(*kring));
53068b8534bSLuigi Rizzo 		kring->na = na;
53168b8534bSLuigi Rizzo 
53268b8534bSLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
53368b8534bSLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
53468b8534bSLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
53568b8534bSLuigi Rizzo 		ND("rxring[%d] at %p offset %d", i, ring, ring->buf_ofs);
53668b8534bSLuigi Rizzo 		*(int *)(int *)(uintptr_t)&ring->num_slots =
53768b8534bSLuigi Rizzo 			kring->nkr_num_slots = numdesc;
53868b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
53968b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail = 0; /* empty */
540*446ee301SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
54168b8534bSLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
54268b8534bSLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
54368b8534bSLuigi Rizzo 	}
54468b8534bSLuigi Rizzo 	NMA_UNLOCK();
54568b8534bSLuigi Rizzo 	for (i = 0; i < n+1; i++) {
54668b8534bSLuigi Rizzo 		// XXX initialize the selrecord structs.
54768b8534bSLuigi Rizzo 	}
54868b8534bSLuigi Rizzo final:
54968b8534bSLuigi Rizzo 	/*
55068b8534bSLuigi Rizzo 	 * fill the slots for the rx and tx queues. They contain the offset
55168b8534bSLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
55268b8534bSLuigi Rizzo 	 * userspace to reach the ring from the nifp.
55368b8534bSLuigi Rizzo 	 */
55468b8534bSLuigi Rizzo 	for (i = 0; i < n; i++) {
55568b8534bSLuigi Rizzo 		char *base = (char *)nifp;
55668b8534bSLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
55768b8534bSLuigi Rizzo 			(char *)na->tx_rings[i].ring - base;
55868b8534bSLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n] =
55968b8534bSLuigi Rizzo 			(char *)na->rx_rings[i].ring - base;
56068b8534bSLuigi Rizzo 	}
56168b8534bSLuigi Rizzo 	return (nifp);
56268b8534bSLuigi Rizzo }
56368b8534bSLuigi Rizzo 
56468b8534bSLuigi Rizzo 
56568b8534bSLuigi Rizzo /*
56668b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
56768b8534bSLuigi Rizzo  *
56868b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
56968b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
57068b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
57168b8534bSLuigi Rizzo  *
57268b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
57368b8534bSLuigi Rizzo  */
57468b8534bSLuigi Rizzo static int
57568b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
57668b8534bSLuigi Rizzo netmap_mmap(__unused struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr,
57768b8534bSLuigi Rizzo 	    int nprot)
57868b8534bSLuigi Rizzo #else
57968b8534bSLuigi Rizzo netmap_mmap(__unused struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
58068b8534bSLuigi Rizzo 	    int nprot, __unused vm_memattr_t *memattr)
58168b8534bSLuigi Rizzo #endif
58268b8534bSLuigi Rizzo {
58368b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
58468b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
585*446ee301SLuigi Rizzo 
58668b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
587*446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
58868b8534bSLuigi Rizzo 
58968b8534bSLuigi Rizzo 	return (0);
59068b8534bSLuigi Rizzo }
59168b8534bSLuigi Rizzo 
59268b8534bSLuigi Rizzo 
59368b8534bSLuigi Rizzo /*
59402ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
59502ad4083SLuigi Rizzo  *
59602ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
59702ad4083SLuigi Rizzo  * system call in user process context, and the only contention
59802ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
59902ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
60002ad4083SLuigi Rizzo  * need to lock.
60168b8534bSLuigi Rizzo  */
60268b8534bSLuigi Rizzo static void
60368b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
60468b8534bSLuigi Rizzo {
60568b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_queues];
60668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
60768b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
60802ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
60968b8534bSLuigi Rizzo 
61002ad4083SLuigi Rizzo 	k = ring->cur;
61102ad4083SLuigi Rizzo 	if (k > lim) {
61202ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
61302ad4083SLuigi Rizzo 		return;
61402ad4083SLuigi Rizzo 	}
61502ad4083SLuigi Rizzo 	// na->nm_lock(na->ifp->if_softc, NETMAP_CORE_LOCK, 0);
61668b8534bSLuigi Rizzo 
61768b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
61868b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
61968b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
62068b8534bSLuigi Rizzo 	 */
62102ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
62268b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
62368b8534bSLuigi Rizzo 
62468b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
62568b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
62668b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
62768b8534bSLuigi Rizzo 			continue;
62868b8534bSLuigi Rizzo 		}
62968b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
63068b8534bSLuigi Rizzo 
63168b8534bSLuigi Rizzo 		if (m == NULL)
63268b8534bSLuigi Rizzo 			break;
63368b8534bSLuigi Rizzo 		if (tail)
63468b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
63568b8534bSLuigi Rizzo 		else
63668b8534bSLuigi Rizzo 			head = m;
63768b8534bSLuigi Rizzo 		tail = m;
63868b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
63968b8534bSLuigi Rizzo 	}
64002ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
64168b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
64202ad4083SLuigi Rizzo 	// na->nm_lock(na->ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
64368b8534bSLuigi Rizzo 
64468b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
64568b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
64668b8534bSLuigi Rizzo 		head = head->m_nextpkt;
64768b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
64868b8534bSLuigi Rizzo 		m->m_pkthdr.rcvif = na->ifp;
64968b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
65068b8534bSLuigi Rizzo 			D("sending up pkt %p size %d", m, m->m_pkthdr.len);
65168b8534bSLuigi Rizzo 		(na->ifp->if_input)(na->ifp, m);
65268b8534bSLuigi Rizzo 	}
65368b8534bSLuigi Rizzo }
65468b8534bSLuigi Rizzo 
65568b8534bSLuigi Rizzo /*
65602ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
65702ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
65802ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
65902ad4083SLuigi Rizzo  *
66068b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
66168b8534bSLuigi Rizzo  * (we know because td != NULL).
66268b8534bSLuigi Rizzo  */
66368b8534bSLuigi Rizzo static void
66468b8534bSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td)
66568b8534bSLuigi Rizzo {
66668b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_queues];
66768b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
66802ad4083SLuigi Rizzo 	int error = 1, delta;
66902ad4083SLuigi Rizzo 	u_int k = ring->cur, lim = kring->nkr_num_slots;
67068b8534bSLuigi Rizzo 
67168b8534bSLuigi Rizzo 	na->nm_lock(na->ifp->if_softc, NETMAP_CORE_LOCK, 0);
67202ad4083SLuigi Rizzo 	if (k >= lim) /* bad value */
67302ad4083SLuigi Rizzo 		goto done;
67402ad4083SLuigi Rizzo 	delta = k - kring->nr_hwcur;
67568b8534bSLuigi Rizzo 	if (delta < 0)
67602ad4083SLuigi Rizzo 		delta += lim;
67768b8534bSLuigi Rizzo 	kring->nr_hwavail -= delta;
67802ad4083SLuigi Rizzo 	if (kring->nr_hwavail < 0)	/* error */
67902ad4083SLuigi Rizzo 		goto done;
68002ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
68102ad4083SLuigi Rizzo 	error = 0;
68202ad4083SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail;
68302ad4083SLuigi Rizzo 	if (k == 0 && td)
68468b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
68502ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
68602ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
68702ad4083SLuigi Rizzo done:
68868b8534bSLuigi Rizzo 	na->nm_lock(na->ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
68902ad4083SLuigi Rizzo 	if (error)
69002ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
69168b8534bSLuigi Rizzo }
69268b8534bSLuigi Rizzo 
69368b8534bSLuigi Rizzo 
69468b8534bSLuigi Rizzo /*
69568b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
69668b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
69768b8534bSLuigi Rizzo  * is not supported by the interface.
69868b8534bSLuigi Rizzo  * If successful, hold a reference.
69968b8534bSLuigi Rizzo  */
70068b8534bSLuigi Rizzo static int
70168b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
70268b8534bSLuigi Rizzo {
70368b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
70468b8534bSLuigi Rizzo 	if (*ifp == NULL)
70568b8534bSLuigi Rizzo 		return (ENXIO);
70668b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
70768b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
70868b8534bSLuigi Rizzo 	 */
70968b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
71068b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
71168b8534bSLuigi Rizzo 	if_rele(*ifp);
71268b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
71368b8534bSLuigi Rizzo }
71468b8534bSLuigi Rizzo 
71568b8534bSLuigi Rizzo 
71668b8534bSLuigi Rizzo /*
71768b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
71868b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
71968b8534bSLuigi Rizzo  * Return 1 on reinit.
720506cc70cSLuigi Rizzo  *
721506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
722506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
723506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
724506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
725506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
726506cc70cSLuigi Rizzo  * it under lock.
72768b8534bSLuigi Rizzo  */
72868b8534bSLuigi Rizzo int
72968b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
73068b8534bSLuigi Rizzo {
73168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
73268b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
73368b8534bSLuigi Rizzo 	int errors = 0;
73468b8534bSLuigi Rizzo 
73568b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
73668b8534bSLuigi Rizzo 	if (ring->cur > lim)
73768b8534bSLuigi Rizzo 		errors++;
73868b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
73968b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
74068b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
74168b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
74268b8534bSLuigi Rizzo 			if (!errors++)
74368b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
74468b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
74568b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
74668b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
74768b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
74868b8534bSLuigi Rizzo 			if (!errors++)
74968b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
75068b8534bSLuigi Rizzo 					len, i, idx);
75168b8534bSLuigi Rizzo 		}
75268b8534bSLuigi Rizzo 	}
75368b8534bSLuigi Rizzo 	if (errors) {
75468b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
75568b8534bSLuigi Rizzo 		int n = kring->na->num_queues + 2;
75668b8534bSLuigi Rizzo 
75768b8534bSLuigi Rizzo 		D("total %d errors", errors);
75868b8534bSLuigi Rizzo 		errors++;
75968b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
76068b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
76168b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
76268b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
76368b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
76468b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
76568b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
76668b8534bSLuigi Rizzo 	}
76768b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
76868b8534bSLuigi Rizzo }
76968b8534bSLuigi Rizzo 
77068b8534bSLuigi Rizzo 
77168b8534bSLuigi Rizzo /*
77268b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
77368b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
77468b8534bSLuigi Rizzo  */
77568b8534bSLuigi Rizzo static int
77668b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
77768b8534bSLuigi Rizzo {
77868b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
77968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
78068b8534bSLuigi Rizzo 	void *adapter = na->ifp->if_softc;	/* shorthand */
78168b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
78268b8534bSLuigi Rizzo 	/* first time we don't lock */
78368b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
78468b8534bSLuigi Rizzo 
78568b8534bSLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= na->num_queues) {
78668b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
78768b8534bSLuigi Rizzo 		return (EINVAL);
78868b8534bSLuigi Rizzo 	}
78968b8534bSLuigi Rizzo 	if (need_lock)
79068b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
79168b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
79268b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
79368b8534bSLuigi Rizzo 		priv->np_qfirst = na->num_queues;
79468b8534bSLuigi Rizzo 		priv->np_qlast = na->num_queues + 1;
79568b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
79668b8534bSLuigi Rizzo 		priv->np_qfirst = i;
79768b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
79868b8534bSLuigi Rizzo 	} else {
79968b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
80068b8534bSLuigi Rizzo 		priv->np_qlast = na->num_queues;
80168b8534bSLuigi Rizzo 	}
80268b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
80368b8534bSLuigi Rizzo 	if (need_lock)
80468b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
80568b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
80668b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
80768b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
80868b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
80968b8534bSLuigi Rizzo 			priv->np_qfirst);
81068b8534bSLuigi Rizzo 	else
81168b8534bSLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname,
81268b8534bSLuigi Rizzo 			priv->np_qlast);
81368b8534bSLuigi Rizzo 	return 0;
81468b8534bSLuigi Rizzo }
81568b8534bSLuigi Rizzo 
81668b8534bSLuigi Rizzo /*
81768b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
81868b8534bSLuigi Rizzo  *
81968b8534bSLuigi Rizzo  * Following a list of accepted commands:
82068b8534bSLuigi Rizzo  * - NIOCGINFO
82168b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
82268b8534bSLuigi Rizzo  * - NIOCREGIF
82368b8534bSLuigi Rizzo  * - NIOCUNREGIF
82468b8534bSLuigi Rizzo  * - NIOCTXSYNC
82568b8534bSLuigi Rizzo  * - NIOCRXSYNC
82668b8534bSLuigi Rizzo  *
82768b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
82868b8534bSLuigi Rizzo  */
82968b8534bSLuigi Rizzo static int
83068b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data,
831506cc70cSLuigi Rizzo 	__unused int fflag, struct thread *td)
83268b8534bSLuigi Rizzo {
83368b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
83468b8534bSLuigi Rizzo 	struct ifnet *ifp;
83568b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
83668b8534bSLuigi Rizzo 	struct netmap_adapter *na;
83768b8534bSLuigi Rizzo 	void *adapter;
83868b8534bSLuigi Rizzo 	int error;
83968b8534bSLuigi Rizzo 	u_int i;
84068b8534bSLuigi Rizzo 	struct netmap_if *nifp;
84168b8534bSLuigi Rizzo 
842506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
843506cc70cSLuigi Rizzo 
84468b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
845506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
846506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
84768b8534bSLuigi Rizzo 		return (error);
848506cc70cSLuigi Rizzo 	}
84968b8534bSLuigi Rizzo 
85068b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
85168b8534bSLuigi Rizzo 	switch (cmd) {
85268b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
85368b8534bSLuigi Rizzo 		/* memsize is always valid */
85468b8534bSLuigi Rizzo 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
85568b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
85668b8534bSLuigi Rizzo 		nmr->nr_numrings = 0;
85768b8534bSLuigi Rizzo 		nmr->nr_numslots = 0;
85868b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
85968b8534bSLuigi Rizzo 			break;
86068b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
86168b8534bSLuigi Rizzo 		if (error)
86268b8534bSLuigi Rizzo 			break;
86368b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
86468b8534bSLuigi Rizzo 		nmr->nr_numrings = na->num_queues;
86568b8534bSLuigi Rizzo 		nmr->nr_numslots = na->num_tx_desc;
86668b8534bSLuigi Rizzo 		if_rele(ifp);	/* return the refcount */
86768b8534bSLuigi Rizzo 		break;
86868b8534bSLuigi Rizzo 
86968b8534bSLuigi Rizzo 	case NIOCREGIF:
870506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
871506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
872506cc70cSLuigi Rizzo 			break;
873506cc70cSLuigi Rizzo 		}
87468b8534bSLuigi Rizzo 		/* find the interface and a reference */
87568b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
87668b8534bSLuigi Rizzo 		if (error)
87768b8534bSLuigi Rizzo 			break;
87868b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
87968b8534bSLuigi Rizzo 		adapter = na->ifp->if_softc;	/* shorthand */
88068b8534bSLuigi Rizzo 		/*
88168b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
88268b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
88368b8534bSLuigi Rizzo 		 */
88468b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
88568b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
88668b8534bSLuigi Rizzo 		if (priv == NULL) {
88768b8534bSLuigi Rizzo 			error = ENOMEM;
88868b8534bSLuigi Rizzo 			if_rele(ifp);   /* return the refcount */
88968b8534bSLuigi Rizzo 			break;
89068b8534bSLuigi Rizzo 		}
89168b8534bSLuigi Rizzo 
89268b8534bSLuigi Rizzo 
89368b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
89468b8534bSLuigi Rizzo 			na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
89568b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
89668b8534bSLuigi Rizzo 				break;
89768b8534bSLuigi Rizzo 			na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
89868b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
89968b8534bSLuigi Rizzo 		}
90068b8534bSLuigi Rizzo 		if (i == 0) {
90168b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
90268b8534bSLuigi Rizzo 			error = EINVAL;
90368b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
90468b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
90568b8534bSLuigi Rizzo 			break;
90668b8534bSLuigi Rizzo 		}
90768b8534bSLuigi Rizzo 
90868b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
90968b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
91068b8534bSLuigi Rizzo 		if (error)
91168b8534bSLuigi Rizzo 			goto error;
91268b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
91368b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
91468b8534bSLuigi Rizzo 			error = ENOMEM;
91568b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
91668b8534bSLuigi Rizzo 			/* was already set */
91768b8534bSLuigi Rizzo 		} else {
91868b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
91968b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
92068b8534bSLuigi Rizzo 			 */
92168b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
92268b8534bSLuigi Rizzo 			if (error) {
92368b8534bSLuigi Rizzo 				/*
92468b8534bSLuigi Rizzo 				 * do something similar to netmap_dtor().
92568b8534bSLuigi Rizzo 				 */
9266e10c8b8SLuigi Rizzo 				netmap_free_rings(na);
9276e10c8b8SLuigi Rizzo 				// XXX tx_rings is inline, must not be freed.
9286e10c8b8SLuigi Rizzo 				// free(na->tx_rings, M_DEVBUF); // XXX wrong ?
92968b8534bSLuigi Rizzo 				na->tx_rings = na->rx_rings = NULL;
93068b8534bSLuigi Rizzo 				na->refcount--;
9316e10c8b8SLuigi Rizzo 				netmap_if_free(nifp);
93268b8534bSLuigi Rizzo 				nifp = NULL;
93368b8534bSLuigi Rizzo 			}
93468b8534bSLuigi Rizzo 		}
93568b8534bSLuigi Rizzo 
93668b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
93768b8534bSLuigi Rizzo error:
93868b8534bSLuigi Rizzo 			na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
93968b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
94068b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
94168b8534bSLuigi Rizzo 			break;
94268b8534bSLuigi Rizzo 		}
94368b8534bSLuigi Rizzo 
94468b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
94568b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
94668b8534bSLuigi Rizzo 
94768b8534bSLuigi Rizzo 		if (error != 0) {
94868b8534bSLuigi Rizzo 			/* could not assign the private storage for the
94968b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
95068b8534bSLuigi Rizzo 			 */
95168b8534bSLuigi Rizzo 			netmap_dtor(priv);
95268b8534bSLuigi Rizzo 			break;
95368b8534bSLuigi Rizzo 		}
95468b8534bSLuigi Rizzo 
95568b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
95668b8534bSLuigi Rizzo 		nmr->nr_numrings = na->num_queues;
95768b8534bSLuigi Rizzo 		nmr->nr_numslots = na->num_tx_desc;
95868b8534bSLuigi Rizzo 		nmr->nr_memsize = netmap_mem_d->nm_totalsize;
959*446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
96068b8534bSLuigi Rizzo 		break;
96168b8534bSLuigi Rizzo 
96268b8534bSLuigi Rizzo 	case NIOCUNREGIF:
963506cc70cSLuigi Rizzo 		if (priv == NULL) {
964506cc70cSLuigi Rizzo 			error = ENXIO;
965506cc70cSLuigi Rizzo 			break;
966506cc70cSLuigi Rizzo 		}
96768b8534bSLuigi Rizzo 
96868b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
96968b8534bSLuigi Rizzo 		   destructor of the private data. */
97068b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
97168b8534bSLuigi Rizzo 		break;
97268b8534bSLuigi Rizzo 
97368b8534bSLuigi Rizzo 	case NIOCTXSYNC:
97468b8534bSLuigi Rizzo         case NIOCRXSYNC:
975506cc70cSLuigi Rizzo 		if (priv == NULL) {
976506cc70cSLuigi Rizzo 			error = ENXIO;
977506cc70cSLuigi Rizzo 			break;
978506cc70cSLuigi Rizzo 		}
97968b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
98068b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
98168b8534bSLuigi Rizzo 		adapter = ifp->if_softc;	/* shorthand */
98268b8534bSLuigi Rizzo 
98368b8534bSLuigi Rizzo 		if (priv->np_qfirst == na->num_queues) {
98468b8534bSLuigi Rizzo 			/* queues to/from host */
98568b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
98668b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
98768b8534bSLuigi Rizzo 			else
98868b8534bSLuigi Rizzo 				netmap_sync_from_host(na, NULL);
989506cc70cSLuigi Rizzo 			break;
99068b8534bSLuigi Rizzo 		}
99168b8534bSLuigi Rizzo 
99268b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
99368b8534bSLuigi Rizzo 		    if (cmd == NIOCTXSYNC) {
99468b8534bSLuigi Rizzo 			struct netmap_kring *kring = &na->tx_rings[i];
99568b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
99668b8534bSLuigi Rizzo 				D("sync tx ring %d cur %d hwcur %d",
99768b8534bSLuigi Rizzo 					i, kring->ring->cur,
99868b8534bSLuigi Rizzo 					kring->nr_hwcur);
99968b8534bSLuigi Rizzo                         na->nm_txsync(adapter, i, 1 /* do lock */);
100068b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
100168b8534bSLuigi Rizzo 				D("after sync tx ring %d cur %d hwcur %d",
100268b8534bSLuigi Rizzo 					i, kring->ring->cur,
100368b8534bSLuigi Rizzo 					kring->nr_hwcur);
100468b8534bSLuigi Rizzo 		    } else {
100568b8534bSLuigi Rizzo 			na->nm_rxsync(adapter, i, 1 /* do lock */);
100668b8534bSLuigi Rizzo 			microtime(&na->rx_rings[i].ring->ts);
100768b8534bSLuigi Rizzo 		    }
100868b8534bSLuigi Rizzo 		}
100968b8534bSLuigi Rizzo 
101068b8534bSLuigi Rizzo                 break;
101168b8534bSLuigi Rizzo 
101268b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
101368b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
101468b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
101568b8534bSLuigi Rizzo 	case BIOCSSEESENT:
101668b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
101768b8534bSLuigi Rizzo 		break;
101868b8534bSLuigi Rizzo 
101968b8534bSLuigi Rizzo 	default:
102068b8534bSLuigi Rizzo 	    {
102168b8534bSLuigi Rizzo 		/*
102268b8534bSLuigi Rizzo 		 * allow device calls
102368b8534bSLuigi Rizzo 		 */
102468b8534bSLuigi Rizzo 		struct socket so;
102568b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
102668b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
102768b8534bSLuigi Rizzo 		if (error)
102868b8534bSLuigi Rizzo 			break;
102968b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
103068b8534bSLuigi Rizzo 		// so->so_proto not null.
103168b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
103268b8534bSLuigi Rizzo 		if_rele(ifp);
103368b8534bSLuigi Rizzo 	    }
103468b8534bSLuigi Rizzo 	}
103568b8534bSLuigi Rizzo 
1036506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
103768b8534bSLuigi Rizzo 	return (error);
103868b8534bSLuigi Rizzo }
103968b8534bSLuigi Rizzo 
104068b8534bSLuigi Rizzo 
104168b8534bSLuigi Rizzo /*
104268b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
104368b8534bSLuigi Rizzo  *
104468b8534bSLuigi Rizzo  * Can be called for one or more queues.
104568b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
104668b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
104768b8534bSLuigi Rizzo  * selfd or on the global one.
104868b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
104968b8534bSLuigi Rizzo  * are done through callbacks.
105068b8534bSLuigi Rizzo  */
105168b8534bSLuigi Rizzo static int
105268b8534bSLuigi Rizzo netmap_poll(__unused struct cdev *dev, int events, struct thread *td)
105368b8534bSLuigi Rizzo {
105468b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
105568b8534bSLuigi Rizzo 	struct netmap_adapter *na;
105668b8534bSLuigi Rizzo 	struct ifnet *ifp;
105768b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1058d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
105968b8534bSLuigi Rizzo 	void *adapter;
106068b8534bSLuigi Rizzo 
106168b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
106268b8534bSLuigi Rizzo 		return POLLERR;
106368b8534bSLuigi Rizzo 
106468b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
106568b8534bSLuigi Rizzo 	// XXX check for deleting() ?
106668b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
106768b8534bSLuigi Rizzo 		return POLLERR;
106868b8534bSLuigi Rizzo 
106968b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
107068b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
107168b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
107268b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
107368b8534bSLuigi Rizzo 
107468b8534bSLuigi Rizzo 	adapter = ifp->if_softc;
107568b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
107668b8534bSLuigi Rizzo 
107768b8534bSLuigi Rizzo 	/* how many queues we are scanning */
107868b8534bSLuigi Rizzo 	i = priv->np_qfirst;
107968b8534bSLuigi Rizzo 	if (i == na->num_queues) { /* from/to host */
108068b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
108168b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
108268b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
108368b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
108468b8534bSLuigi Rizzo 			revents |= want_tx;
108568b8534bSLuigi Rizzo 		}
108668b8534bSLuigi Rizzo 		if (want_rx) {
108768b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
108868b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
108968b8534bSLuigi Rizzo 				netmap_sync_from_host(na, td);
109068b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
109168b8534bSLuigi Rizzo 				revents |= want_rx;
109268b8534bSLuigi Rizzo 			}
109368b8534bSLuigi Rizzo 		}
109468b8534bSLuigi Rizzo 		return (revents);
109568b8534bSLuigi Rizzo 	}
109668b8534bSLuigi Rizzo 
109768b8534bSLuigi Rizzo 	/*
109868b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
109968b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
110068b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
110168b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
110268b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
110368b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
110468b8534bSLuigi Rizzo 	 * has more than one ring.
110568b8534bSLuigi Rizzo 	 *
110668b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
110768b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
110868b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
110968b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
111068b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
111168b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
111268b8534bSLuigi Rizzo 	 *
111368b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
111468b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
111568b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
111668b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
111768b8534bSLuigi Rizzo 	 */
111868b8534bSLuigi Rizzo 	check_all = (i + 1 != priv->np_qlast);
111968b8534bSLuigi Rizzo 
112068b8534bSLuigi Rizzo 	/*
112168b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
112268b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
112368b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
112468b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
112568b8534bSLuigi Rizzo 	 *
112668b8534bSLuigi Rizzo 	 * It has three possible states:
112768b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
112868b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
112968b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
113068b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
113168b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
113268b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
113368b8534bSLuigi Rizzo 	 */
113468b8534bSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL };
1135d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ?  NEED_CL:NO_CL;
113668b8534bSLuigi Rizzo 	/*
113768b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
113868b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
113968b8534bSLuigi Rizzo 	 * routines.
114068b8534bSLuigi Rizzo 	 */
114168b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; want_rx && i < priv->np_qlast; i++) {
114268b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
114368b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
114468b8534bSLuigi Rizzo 				revents |= want_rx;
114568b8534bSLuigi Rizzo 				want_rx = 0;	/* also breaks the loop */
114668b8534bSLuigi Rizzo 			}
114768b8534bSLuigi Rizzo 		}
114868b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; want_tx && i < priv->np_qlast; i++) {
114968b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
115068b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
115168b8534bSLuigi Rizzo 				revents |= want_tx;
115268b8534bSLuigi Rizzo 				want_tx = 0;	/* also breaks the loop */
115368b8534bSLuigi Rizzo 			}
115468b8534bSLuigi Rizzo 		}
115568b8534bSLuigi Rizzo 
115668b8534bSLuigi Rizzo 	/*
115768b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
115868b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
115968b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
116068b8534bSLuigi Rizzo 	 */
116168b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
116268b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
116368b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
116468b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
116568b8534bSLuigi Rizzo 				continue;
116668b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
116768b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
116868b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
116968b8534bSLuigi Rizzo 			}
117068b8534bSLuigi Rizzo 			if (na->separate_locks)
117168b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_TX_LOCK, i);
117268b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
117368b8534bSLuigi Rizzo 				D("send %d on %s %d",
117468b8534bSLuigi Rizzo 					kring->ring->cur,
117568b8534bSLuigi Rizzo 					ifp->if_xname, i);
117668b8534bSLuigi Rizzo 			if (na->nm_txsync(adapter, i, 0 /* no lock */))
117768b8534bSLuigi Rizzo 				revents |= POLLERR;
117868b8534bSLuigi Rizzo 
117968b8534bSLuigi Rizzo 			if (want_tx) {
118068b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
118168b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
118268b8534bSLuigi Rizzo 					 * starvation.
118368b8534bSLuigi Rizzo 					 */
118468b8534bSLuigi Rizzo 					revents |= want_tx;
118568b8534bSLuigi Rizzo 					want_tx = 0;
118668b8534bSLuigi Rizzo 				} else if (!check_all)
118768b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
118868b8534bSLuigi Rizzo 			}
118968b8534bSLuigi Rizzo 			if (na->separate_locks)
119068b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_TX_UNLOCK, i);
119168b8534bSLuigi Rizzo 		}
119268b8534bSLuigi Rizzo 	}
119368b8534bSLuigi Rizzo 
119468b8534bSLuigi Rizzo 	/*
119568b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
119668b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
119768b8534bSLuigi Rizzo 	 */
119868b8534bSLuigi Rizzo 	if (want_rx) {
119968b8534bSLuigi Rizzo 		for (i = priv->np_qfirst; i < priv->np_qlast; i++) {
120068b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
120168b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
120268b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_CORE_LOCK, 0);
120368b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
120468b8534bSLuigi Rizzo 			}
120568b8534bSLuigi Rizzo 			if (na->separate_locks)
120668b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_RX_LOCK, i);
120768b8534bSLuigi Rizzo 
120868b8534bSLuigi Rizzo 			if (na->nm_rxsync(adapter, i, 0 /* no lock */))
120968b8534bSLuigi Rizzo 				revents |= POLLERR;
121068b8534bSLuigi Rizzo 			if (no_timestamp == 0 ||
121168b8534bSLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP)
121268b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
121368b8534bSLuigi Rizzo 
121468b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
121568b8534bSLuigi Rizzo 				revents |= want_rx;
121668b8534bSLuigi Rizzo 			else if (!check_all)
121768b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
121868b8534bSLuigi Rizzo 			if (na->separate_locks)
121968b8534bSLuigi Rizzo 				na->nm_lock(adapter, NETMAP_RX_UNLOCK, i);
122068b8534bSLuigi Rizzo 		}
122168b8534bSLuigi Rizzo 	}
122268b8534bSLuigi Rizzo 	if (check_all && revents == 0) {
122368b8534bSLuigi Rizzo 		i = na->num_queues + 1; /* the global queue */
122468b8534bSLuigi Rizzo 		if (want_tx)
122568b8534bSLuigi Rizzo 			selrecord(td, &na->tx_rings[i].si);
122668b8534bSLuigi Rizzo 		if (want_rx)
122768b8534bSLuigi Rizzo 			selrecord(td, &na->rx_rings[i].si);
122868b8534bSLuigi Rizzo 	}
122968b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
123068b8534bSLuigi Rizzo 		na->nm_lock(adapter, NETMAP_CORE_UNLOCK, 0);
123168b8534bSLuigi Rizzo 
123268b8534bSLuigi Rizzo 	return (revents);
123368b8534bSLuigi Rizzo }
123468b8534bSLuigi Rizzo 
123568b8534bSLuigi Rizzo /*------- driver support routines ------*/
123668b8534bSLuigi Rizzo 
123768b8534bSLuigi Rizzo /*
123868b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
123968b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
124068b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
124168b8534bSLuigi Rizzo  * of hardware rings):
124268b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
124368b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
124468b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
124568b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
124668b8534bSLuigi Rizzo  */
124768b8534bSLuigi Rizzo int
124868b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
124968b8534bSLuigi Rizzo {
125068b8534bSLuigi Rizzo 	int n = num_queues + 2;
125168b8534bSLuigi Rizzo 	int size = sizeof(*na) + 2 * n * sizeof(struct netmap_kring);
125268b8534bSLuigi Rizzo 	void *buf;
125368b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
125468b8534bSLuigi Rizzo 
125568b8534bSLuigi Rizzo 	if (ifp == NULL) {
125668b8534bSLuigi Rizzo 		D("ifp not set, giving up");
125768b8534bSLuigi Rizzo 		return EINVAL;
125868b8534bSLuigi Rizzo 	}
125968b8534bSLuigi Rizzo 	na->refcount = 0;
126068b8534bSLuigi Rizzo 	na->num_queues = num_queues;
126168b8534bSLuigi Rizzo 
126268b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
126368b8534bSLuigi Rizzo 	if (buf) {
1264d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
126568b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
126668b8534bSLuigi Rizzo 		na->rx_rings = na->tx_rings + n;
126768b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
126868b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
126968b8534bSLuigi Rizzo 	}
127068b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
127168b8534bSLuigi Rizzo 
127268b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
127368b8534bSLuigi Rizzo }
127468b8534bSLuigi Rizzo 
127568b8534bSLuigi Rizzo 
127668b8534bSLuigi Rizzo /*
127768b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
127868b8534bSLuigi Rizzo  * object.
127968b8534bSLuigi Rizzo  */
128068b8534bSLuigi Rizzo void
128168b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
128268b8534bSLuigi Rizzo {
128368b8534bSLuigi Rizzo 	u_int i;
128468b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
128568b8534bSLuigi Rizzo 
128668b8534bSLuigi Rizzo 	if (!na)
128768b8534bSLuigi Rizzo 		return;
128868b8534bSLuigi Rizzo 
128968b8534bSLuigi Rizzo 	for (i = 0; i < na->num_queues + 2; i++) {
129068b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
129168b8534bSLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
129268b8534bSLuigi Rizzo 	}
129368b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1294d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
129568b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
129668b8534bSLuigi Rizzo }
129768b8534bSLuigi Rizzo 
129868b8534bSLuigi Rizzo 
129968b8534bSLuigi Rizzo /*
130002ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
130102ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
130268b8534bSLuigi Rizzo  * We are not locked when called.
130368b8534bSLuigi Rizzo  */
130468b8534bSLuigi Rizzo int
130568b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
130668b8534bSLuigi Rizzo {
130768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
130802ad4083SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_queues];
130902ad4083SLuigi Rizzo 	u_int i, len = m->m_pkthdr.len;
131002ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
131168b8534bSLuigi Rizzo 	struct netmap_slot *slot;
131268b8534bSLuigi Rizzo 
131368b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
131468b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
131568b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
131668b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_LOCK, 0);
131702ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
131868b8534bSLuigi Rizzo 		D("stack ring %s full\n", ifp->if_xname);
131968b8534bSLuigi Rizzo 		goto done;	/* no space */
132068b8534bSLuigi Rizzo 	}
132168b8534bSLuigi Rizzo 	if (len > na->buff_size) {
132268b8534bSLuigi Rizzo 		D("drop packet size %d > %d", len, na->buff_size);
132368b8534bSLuigi Rizzo 		goto done;	/* too long for us */
132468b8534bSLuigi Rizzo 	}
132568b8534bSLuigi Rizzo 
132668b8534bSLuigi Rizzo 	/* compute the insert position */
132768b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
132802ad4083SLuigi Rizzo 	if (i > lim)
132902ad4083SLuigi Rizzo 		i -= lim + 1;
133068b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
133168b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
133268b8534bSLuigi Rizzo 	slot->len = len;
133368b8534bSLuigi Rizzo 	kring->nr_hwavail++;
133468b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
133568b8534bSLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_queues);
133668b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
133768b8534bSLuigi Rizzo 	error = 0;
133868b8534bSLuigi Rizzo done:
133968b8534bSLuigi Rizzo 	na->nm_lock(ifp->if_softc, NETMAP_CORE_UNLOCK, 0);
134068b8534bSLuigi Rizzo 
134168b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
134268b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
134368b8534bSLuigi Rizzo 	 * only when really necessary.
134468b8534bSLuigi Rizzo 	 */
134568b8534bSLuigi Rizzo 	m_freem(m);
134668b8534bSLuigi Rizzo 
134768b8534bSLuigi Rizzo 	return (error);
134868b8534bSLuigi Rizzo }
134968b8534bSLuigi Rizzo 
135068b8534bSLuigi Rizzo 
135168b8534bSLuigi Rizzo /*
135268b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
135368b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
135468b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
135568b8534bSLuigi Rizzo  */
135668b8534bSLuigi Rizzo struct netmap_slot *
135768b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
135868b8534bSLuigi Rizzo 	u_int new_cur)
135968b8534bSLuigi Rizzo {
136068b8534bSLuigi Rizzo 	struct netmap_kring *kring;
136168b8534bSLuigi Rizzo 	struct netmap_ring *ring;
1362506cc70cSLuigi Rizzo 	int new_hwofs, lim;
136368b8534bSLuigi Rizzo 
136468b8534bSLuigi Rizzo 	if (na == NULL)
136568b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
136668b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
136768b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
136868b8534bSLuigi Rizzo 	kring = tx == NR_TX ?  na->tx_rings + n : na->rx_rings + n;
136968b8534bSLuigi Rizzo 	ring = kring->ring;
1370506cc70cSLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
137168b8534bSLuigi Rizzo 
1372506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1373506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
1374506cc70cSLuigi Rizzo 	else
1375506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
1376506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1377506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1378506cc70cSLuigi Rizzo 
1379506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1380506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1381506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1382506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1383506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1384506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1385506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1386506cc70cSLuigi Rizzo 
138768b8534bSLuigi Rizzo 	/*
1388506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1389506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
139068b8534bSLuigi Rizzo 	 */
139168b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
139268b8534bSLuigi Rizzo 	selwakeuppri(&kring[na->num_queues + 1 - n].si, PI_NET);
139368b8534bSLuigi Rizzo 	return kring->ring->slot;
139468b8534bSLuigi Rizzo }
139568b8534bSLuigi Rizzo 
139668b8534bSLuigi Rizzo static void
139768b8534bSLuigi Rizzo ns_dmamap_cb(__unused void *arg, __unused bus_dma_segment_t * segs,
139868b8534bSLuigi Rizzo 	__unused int nseg, __unused int error)
139968b8534bSLuigi Rizzo {
140068b8534bSLuigi Rizzo }
140168b8534bSLuigi Rizzo 
140268b8534bSLuigi Rizzo /* unload a bus_dmamap and create a new one. Used when the
140368b8534bSLuigi Rizzo  * buffer in the slot is changed.
140468b8534bSLuigi Rizzo  * XXX buflen is probably not needed, buffers have constant size.
140568b8534bSLuigi Rizzo  */
140668b8534bSLuigi Rizzo void
14076e10c8b8SLuigi Rizzo netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
140868b8534bSLuigi Rizzo {
140968b8534bSLuigi Rizzo 	bus_dmamap_unload(tag, map);
14106e10c8b8SLuigi Rizzo 	bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, ns_dmamap_cb,
14116e10c8b8SLuigi Rizzo 		NULL, BUS_DMA_NOWAIT);
141268b8534bSLuigi Rizzo }
141368b8534bSLuigi Rizzo 
141468b8534bSLuigi Rizzo void
14156e10c8b8SLuigi Rizzo netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
141668b8534bSLuigi Rizzo {
14176e10c8b8SLuigi Rizzo 	bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE, ns_dmamap_cb,
14186e10c8b8SLuigi Rizzo 		NULL, BUS_DMA_NOWAIT);
141968b8534bSLuigi Rizzo }
142068b8534bSLuigi Rizzo 
142168b8534bSLuigi Rizzo /*------ netmap memory allocator -------*/
142268b8534bSLuigi Rizzo /*
142368b8534bSLuigi Rizzo  * Request for a chunk of memory.
142468b8534bSLuigi Rizzo  *
142568b8534bSLuigi Rizzo  * Memory objects are arranged into a list, hence we need to walk this
142668b8534bSLuigi Rizzo  * list until we find an object with the needed amount of data free.
142768b8534bSLuigi Rizzo  * This sounds like a completely inefficient implementation, but given
142868b8534bSLuigi Rizzo  * the fact that data allocation is done once, we can handle it
142968b8534bSLuigi Rizzo  * flawlessly.
143068b8534bSLuigi Rizzo  *
143168b8534bSLuigi Rizzo  * Return NULL on failure.
143268b8534bSLuigi Rizzo  */
143368b8534bSLuigi Rizzo static void *
143468b8534bSLuigi Rizzo netmap_malloc(size_t size, __unused const char *msg)
143568b8534bSLuigi Rizzo {
143668b8534bSLuigi Rizzo 	struct netmap_mem_obj *mem_obj, *new_mem_obj;
143768b8534bSLuigi Rizzo 	void *ret = NULL;
143868b8534bSLuigi Rizzo 
143968b8534bSLuigi Rizzo 	NMA_LOCK();
144068b8534bSLuigi Rizzo 	TAILQ_FOREACH(mem_obj, &netmap_mem_d->nm_molist, nmo_next) {
144168b8534bSLuigi Rizzo 		if (mem_obj->nmo_used != 0 || mem_obj->nmo_size < size)
144268b8534bSLuigi Rizzo 			continue;
144368b8534bSLuigi Rizzo 
144468b8534bSLuigi Rizzo 		new_mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
144568b8534bSLuigi Rizzo 				     M_WAITOK | M_ZERO);
144668b8534bSLuigi Rizzo 		TAILQ_INSERT_BEFORE(mem_obj, new_mem_obj, nmo_next);
144768b8534bSLuigi Rizzo 
144868b8534bSLuigi Rizzo 		new_mem_obj->nmo_used = 1;
144968b8534bSLuigi Rizzo 		new_mem_obj->nmo_size = size;
145068b8534bSLuigi Rizzo 		new_mem_obj->nmo_data = mem_obj->nmo_data;
145168b8534bSLuigi Rizzo 		memset(new_mem_obj->nmo_data, 0, new_mem_obj->nmo_size);
145268b8534bSLuigi Rizzo 
145368b8534bSLuigi Rizzo 		mem_obj->nmo_size -= size;
145468b8534bSLuigi Rizzo 		mem_obj->nmo_data = (char *) mem_obj->nmo_data + size;
145568b8534bSLuigi Rizzo 		if (mem_obj->nmo_size == 0) {
145668b8534bSLuigi Rizzo 			TAILQ_REMOVE(&netmap_mem_d->nm_molist, mem_obj,
145768b8534bSLuigi Rizzo 				     nmo_next);
145868b8534bSLuigi Rizzo 			free(mem_obj, M_NETMAP);
145968b8534bSLuigi Rizzo 		}
146068b8534bSLuigi Rizzo 
146168b8534bSLuigi Rizzo 		ret = new_mem_obj->nmo_data;
146268b8534bSLuigi Rizzo 
146368b8534bSLuigi Rizzo 		break;
146468b8534bSLuigi Rizzo 	}
146568b8534bSLuigi Rizzo 	NMA_UNLOCK();
146668b8534bSLuigi Rizzo 	ND("%s: %d bytes at %p", msg, size, ret);
146768b8534bSLuigi Rizzo 
146868b8534bSLuigi Rizzo 	return (ret);
146968b8534bSLuigi Rizzo }
147068b8534bSLuigi Rizzo 
147168b8534bSLuigi Rizzo /*
147268b8534bSLuigi Rizzo  * Return the memory to the allocator.
147368b8534bSLuigi Rizzo  *
147468b8534bSLuigi Rizzo  * While freeing a memory object, we try to merge adjacent chunks in
147568b8534bSLuigi Rizzo  * order to reduce memory fragmentation.
147668b8534bSLuigi Rizzo  */
147768b8534bSLuigi Rizzo static void
147868b8534bSLuigi Rizzo netmap_free(void *addr, const char *msg)
147968b8534bSLuigi Rizzo {
148068b8534bSLuigi Rizzo 	size_t size;
148168b8534bSLuigi Rizzo 	struct netmap_mem_obj *cur, *prev, *next;
148268b8534bSLuigi Rizzo 
148368b8534bSLuigi Rizzo 	if (addr == NULL) {
148468b8534bSLuigi Rizzo 		D("NULL addr for %s", msg);
148568b8534bSLuigi Rizzo 		return;
148668b8534bSLuigi Rizzo 	}
148768b8534bSLuigi Rizzo 
148868b8534bSLuigi Rizzo 	NMA_LOCK();
148968b8534bSLuigi Rizzo 	TAILQ_FOREACH(cur, &netmap_mem_d->nm_molist, nmo_next) {
149068b8534bSLuigi Rizzo 		if (cur->nmo_data == addr && cur->nmo_used)
149168b8534bSLuigi Rizzo 			break;
149268b8534bSLuigi Rizzo 	}
149368b8534bSLuigi Rizzo 	if (cur == NULL) {
149468b8534bSLuigi Rizzo 		NMA_UNLOCK();
149568b8534bSLuigi Rizzo 		D("invalid addr %s %p", msg, addr);
149668b8534bSLuigi Rizzo 		return;
149768b8534bSLuigi Rizzo 	}
149868b8534bSLuigi Rizzo 
149968b8534bSLuigi Rizzo 	size = cur->nmo_size;
150068b8534bSLuigi Rizzo 	cur->nmo_used = 0;
150168b8534bSLuigi Rizzo 
150268b8534bSLuigi Rizzo 	/* merge current chunk of memory with the previous one,
150368b8534bSLuigi Rizzo 	   if present. */
150468b8534bSLuigi Rizzo 	prev = TAILQ_PREV(cur, netmap_mem_obj_h, nmo_next);
150568b8534bSLuigi Rizzo 	if (prev && prev->nmo_used == 0) {
150668b8534bSLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, cur, nmo_next);
150768b8534bSLuigi Rizzo 		prev->nmo_size += cur->nmo_size;
150868b8534bSLuigi Rizzo 		free(cur, M_NETMAP);
150968b8534bSLuigi Rizzo 		cur = prev;
151068b8534bSLuigi Rizzo 	}
151168b8534bSLuigi Rizzo 
151268b8534bSLuigi Rizzo 	/* merge with the next one */
151368b8534bSLuigi Rizzo 	next = TAILQ_NEXT(cur, nmo_next);
151468b8534bSLuigi Rizzo 	if (next && next->nmo_used == 0) {
151568b8534bSLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, next, nmo_next);
151668b8534bSLuigi Rizzo 		cur->nmo_size += next->nmo_size;
151768b8534bSLuigi Rizzo 		free(next, M_NETMAP);
151868b8534bSLuigi Rizzo 	}
151968b8534bSLuigi Rizzo 	NMA_UNLOCK();
152068b8534bSLuigi Rizzo 	ND("freed %s %d bytes at %p", msg, size, addr);
152168b8534bSLuigi Rizzo }
152268b8534bSLuigi Rizzo 
152368b8534bSLuigi Rizzo 
152468b8534bSLuigi Rizzo /*
152568b8534bSLuigi Rizzo  * Initialize the memory allocator.
152668b8534bSLuigi Rizzo  *
152768b8534bSLuigi Rizzo  * Create the descriptor for the memory , allocate the pool of memory
152868b8534bSLuigi Rizzo  * and initialize the list of memory objects with a single chunk
152968b8534bSLuigi Rizzo  * containing the whole pre-allocated memory marked as free.
153068b8534bSLuigi Rizzo  *
153168b8534bSLuigi Rizzo  * Start with a large size, then halve as needed if we fail to
153268b8534bSLuigi Rizzo  * allocate the block. While halving, always add one extra page
153368b8534bSLuigi Rizzo  * because buffers 0 and 1 are used for special purposes.
153468b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
153568b8534bSLuigi Rizzo  */
153668b8534bSLuigi Rizzo static int
153768b8534bSLuigi Rizzo netmap_memory_init(void)
153868b8534bSLuigi Rizzo {
153968b8534bSLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
154068b8534bSLuigi Rizzo 	void *buf = NULL;
154168b8534bSLuigi Rizzo 	int i, n, sz = NETMAP_MEMORY_SIZE;
154268b8534bSLuigi Rizzo 	int extra_sz = 0; // space for rings and two spare buffers
154368b8534bSLuigi Rizzo 
154468b8534bSLuigi Rizzo 	for (; !buf && sz >= 1<<20; sz >>=1) {
154568b8534bSLuigi Rizzo 		extra_sz = sz/200;
154668b8534bSLuigi Rizzo 		extra_sz = (extra_sz + 2*PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
154768b8534bSLuigi Rizzo 	        buf = contigmalloc(sz + extra_sz,
154868b8534bSLuigi Rizzo 			     M_NETMAP,
154968b8534bSLuigi Rizzo 			     M_WAITOK | M_ZERO,
155068b8534bSLuigi Rizzo 			     0, /* low address */
155168b8534bSLuigi Rizzo 			     -1UL, /* high address */
155268b8534bSLuigi Rizzo 			     PAGE_SIZE, /* alignment */
155368b8534bSLuigi Rizzo 			     0 /* boundary */
155468b8534bSLuigi Rizzo 			    );
155568b8534bSLuigi Rizzo 	}
155668b8534bSLuigi Rizzo 	if (buf == NULL)
155768b8534bSLuigi Rizzo 		return (ENOMEM);
155868b8534bSLuigi Rizzo 	sz += extra_sz;
155968b8534bSLuigi Rizzo 	netmap_mem_d = malloc(sizeof(struct netmap_mem_d), M_NETMAP,
156068b8534bSLuigi Rizzo 			      M_WAITOK | M_ZERO);
156168b8534bSLuigi Rizzo 	mtx_init(&netmap_mem_d->nm_mtx, "netmap memory allocator lock", NULL,
156268b8534bSLuigi Rizzo 		 MTX_DEF);
156368b8534bSLuigi Rizzo 	TAILQ_INIT(&netmap_mem_d->nm_molist);
156468b8534bSLuigi Rizzo 	netmap_mem_d->nm_buffer = buf;
156568b8534bSLuigi Rizzo 	netmap_mem_d->nm_totalsize = sz;
156668b8534bSLuigi Rizzo 
156768b8534bSLuigi Rizzo 	/*
156868b8534bSLuigi Rizzo 	 * A buffer takes 2k, a slot takes 8 bytes + ring overhead,
156968b8534bSLuigi Rizzo 	 * so the ratio is 200:1. In other words, we can use 1/200 of
157068b8534bSLuigi Rizzo 	 * the memory for the rings, and the rest for the buffers,
157168b8534bSLuigi Rizzo 	 * and be sure we never run out.
157268b8534bSLuigi Rizzo 	 */
157368b8534bSLuigi Rizzo 	netmap_mem_d->nm_size = sz/200;
157468b8534bSLuigi Rizzo 	netmap_mem_d->nm_buf_start =
157568b8534bSLuigi Rizzo 		(netmap_mem_d->nm_size + PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
157668b8534bSLuigi Rizzo 	netmap_mem_d->nm_buf_len = sz - netmap_mem_d->nm_buf_start;
157768b8534bSLuigi Rizzo 
157868b8534bSLuigi Rizzo 	nm_buf_pool.base = netmap_mem_d->nm_buffer;
157968b8534bSLuigi Rizzo 	nm_buf_pool.base += netmap_mem_d->nm_buf_start;
158068b8534bSLuigi Rizzo 	netmap_buffer_base = nm_buf_pool.base;
158168b8534bSLuigi Rizzo 	D("netmap_buffer_base %p (offset %d)",
158285df3791SLuigi Rizzo 		netmap_buffer_base, (int)netmap_mem_d->nm_buf_start);
158368b8534bSLuigi Rizzo 	/* number of buffers, they all start as free */
158468b8534bSLuigi Rizzo 
158568b8534bSLuigi Rizzo 	netmap_total_buffers = nm_buf_pool.total_buffers =
158668b8534bSLuigi Rizzo 		netmap_mem_d->nm_buf_len / NETMAP_BUF_SIZE;
158768b8534bSLuigi Rizzo 	nm_buf_pool.bufsize = NETMAP_BUF_SIZE;
158868b8534bSLuigi Rizzo 
158968b8534bSLuigi Rizzo 	D("Have %d MB, use %dKB for rings, %d buffers at %p",
159085df3791SLuigi Rizzo 		(sz >> 20), (int)(netmap_mem_d->nm_size >> 10),
159168b8534bSLuigi Rizzo 		nm_buf_pool.total_buffers, nm_buf_pool.base);
159268b8534bSLuigi Rizzo 
159368b8534bSLuigi Rizzo 	/* allocate and initialize the bitmap. Entry 0 is considered
159468b8534bSLuigi Rizzo 	 * always busy (used as default when there are no buffers left).
159568b8534bSLuigi Rizzo 	 */
159668b8534bSLuigi Rizzo 	n = (nm_buf_pool.total_buffers + 31) / 32;
159768b8534bSLuigi Rizzo 	nm_buf_pool.bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP,
159868b8534bSLuigi Rizzo 			 M_WAITOK | M_ZERO);
159968b8534bSLuigi Rizzo 	nm_buf_pool.bitmap[0] = ~3; /* slot 0 and 1 always busy */
160068b8534bSLuigi Rizzo 	for (i = 1; i < n; i++)
160168b8534bSLuigi Rizzo 		nm_buf_pool.bitmap[i] = ~0;
160268b8534bSLuigi Rizzo 	nm_buf_pool.free = nm_buf_pool.total_buffers - 2;
160368b8534bSLuigi Rizzo 
160468b8534bSLuigi Rizzo 	mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
160568b8534bSLuigi Rizzo 			 M_WAITOK | M_ZERO);
160668b8534bSLuigi Rizzo 	TAILQ_INSERT_HEAD(&netmap_mem_d->nm_molist, mem_obj, nmo_next);
160768b8534bSLuigi Rizzo 	mem_obj->nmo_used = 0;
160868b8534bSLuigi Rizzo 	mem_obj->nmo_size = netmap_mem_d->nm_size;
160968b8534bSLuigi Rizzo 	mem_obj->nmo_data = netmap_mem_d->nm_buffer;
161068b8534bSLuigi Rizzo 
161168b8534bSLuigi Rizzo 	return (0);
161268b8534bSLuigi Rizzo }
161368b8534bSLuigi Rizzo 
161468b8534bSLuigi Rizzo 
161568b8534bSLuigi Rizzo /*
161668b8534bSLuigi Rizzo  * Finalize the memory allocator.
161768b8534bSLuigi Rizzo  *
161868b8534bSLuigi Rizzo  * Free all the memory objects contained inside the list, and deallocate
161968b8534bSLuigi Rizzo  * the pool of memory; finally free the memory allocator descriptor.
162068b8534bSLuigi Rizzo  */
162168b8534bSLuigi Rizzo static void
162268b8534bSLuigi Rizzo netmap_memory_fini(void)
162368b8534bSLuigi Rizzo {
162468b8534bSLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
162568b8534bSLuigi Rizzo 
162668b8534bSLuigi Rizzo 	while (!TAILQ_EMPTY(&netmap_mem_d->nm_molist)) {
162768b8534bSLuigi Rizzo 		mem_obj = TAILQ_FIRST(&netmap_mem_d->nm_molist);
162868b8534bSLuigi Rizzo 		TAILQ_REMOVE(&netmap_mem_d->nm_molist, mem_obj, nmo_next);
162968b8534bSLuigi Rizzo 		if (mem_obj->nmo_used == 1) {
163068b8534bSLuigi Rizzo 			printf("netmap: leaked %d bytes at %p\n",
163185df3791SLuigi Rizzo 			       (int)mem_obj->nmo_size,
163268b8534bSLuigi Rizzo 			       mem_obj->nmo_data);
163368b8534bSLuigi Rizzo 		}
163468b8534bSLuigi Rizzo 		free(mem_obj, M_NETMAP);
163568b8534bSLuigi Rizzo 	}
163668b8534bSLuigi Rizzo 	contigfree(netmap_mem_d->nm_buffer, netmap_mem_d->nm_totalsize, M_NETMAP);
163768b8534bSLuigi Rizzo 	// XXX mutex_destroy(nm_mtx);
163868b8534bSLuigi Rizzo 	free(netmap_mem_d, M_NETMAP);
163968b8534bSLuigi Rizzo }
164068b8534bSLuigi Rizzo 
164168b8534bSLuigi Rizzo 
164268b8534bSLuigi Rizzo /*
164368b8534bSLuigi Rizzo  * Module loader.
164468b8534bSLuigi Rizzo  *
164568b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
164668b8534bSLuigi Rizzo  * variables.
164768b8534bSLuigi Rizzo  *
164868b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
164968b8534bSLuigi Rizzo  */
165068b8534bSLuigi Rizzo static int
165168b8534bSLuigi Rizzo netmap_init(void)
165268b8534bSLuigi Rizzo {
165368b8534bSLuigi Rizzo 	int error;
165468b8534bSLuigi Rizzo 
165568b8534bSLuigi Rizzo 
165668b8534bSLuigi Rizzo 	error = netmap_memory_init();
165768b8534bSLuigi Rizzo 	if (error != 0) {
165868b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
165968b8534bSLuigi Rizzo 		return (error);
166068b8534bSLuigi Rizzo 	}
166168b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
166285df3791SLuigi Rizzo 		(int)(netmap_mem_d->nm_totalsize >> 20));
166368b8534bSLuigi Rizzo 
166468b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
166568b8534bSLuigi Rizzo 			      "netmap");
166668b8534bSLuigi Rizzo 
166768b8534bSLuigi Rizzo 	return (0);
166868b8534bSLuigi Rizzo }
166968b8534bSLuigi Rizzo 
167068b8534bSLuigi Rizzo 
167168b8534bSLuigi Rizzo /*
167268b8534bSLuigi Rizzo  * Module unloader.
167368b8534bSLuigi Rizzo  *
167468b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
167568b8534bSLuigi Rizzo  */
167668b8534bSLuigi Rizzo static void
167768b8534bSLuigi Rizzo netmap_fini(void)
167868b8534bSLuigi Rizzo {
167968b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
168068b8534bSLuigi Rizzo 
168168b8534bSLuigi Rizzo 	netmap_memory_fini();
168268b8534bSLuigi Rizzo 
168368b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
168468b8534bSLuigi Rizzo }
168568b8534bSLuigi Rizzo 
168668b8534bSLuigi Rizzo 
168768b8534bSLuigi Rizzo /*
168868b8534bSLuigi Rizzo  * Kernel entry point.
168968b8534bSLuigi Rizzo  *
169068b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
169168b8534bSLuigi Rizzo  *
169268b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
169368b8534bSLuigi Rizzo  */
169468b8534bSLuigi Rizzo static int
169568b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
169668b8534bSLuigi Rizzo {
169768b8534bSLuigi Rizzo 	int error = 0;
169868b8534bSLuigi Rizzo 
169968b8534bSLuigi Rizzo 	switch (event) {
170068b8534bSLuigi Rizzo 	case MOD_LOAD:
170168b8534bSLuigi Rizzo 		error = netmap_init();
170268b8534bSLuigi Rizzo 		break;
170368b8534bSLuigi Rizzo 
170468b8534bSLuigi Rizzo 	case MOD_UNLOAD:
170568b8534bSLuigi Rizzo 		netmap_fini();
170668b8534bSLuigi Rizzo 		break;
170768b8534bSLuigi Rizzo 
170868b8534bSLuigi Rizzo 	default:
170968b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
171068b8534bSLuigi Rizzo 		break;
171168b8534bSLuigi Rizzo 	}
171268b8534bSLuigi Rizzo 
171368b8534bSLuigi Rizzo 	return (error);
171468b8534bSLuigi Rizzo }
171568b8534bSLuigi Rizzo 
171668b8534bSLuigi Rizzo 
171768b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
1718