xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision 2579e2d7)
1ccdc3305SLuigi Rizzo /*
2*2579e2d7SLuigi Rizzo  * Copyright (C) 2012-2013 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved.
3ccdc3305SLuigi Rizzo  *
4ccdc3305SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
5ccdc3305SLuigi Rizzo  * modification, are permitted provided that the following conditions
6ccdc3305SLuigi Rizzo  * are met:
7ccdc3305SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
8ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
9ccdc3305SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
10ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
11ccdc3305SLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
12ccdc3305SLuigi Rizzo  *
13ccdc3305SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ccdc3305SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ccdc3305SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ccdc3305SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ccdc3305SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ccdc3305SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ccdc3305SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ccdc3305SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ccdc3305SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ccdc3305SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ccdc3305SLuigi Rizzo  * SUCH DAMAGE.
24ccdc3305SLuigi Rizzo  */
25ccdc3305SLuigi Rizzo 
26ccdc3305SLuigi Rizzo /*
27ccdc3305SLuigi Rizzo  * $FreeBSD$
288241616dSLuigi Rizzo  * $Id: netmap_mem2.c 11881 2012-10-18 23:24:15Z luigi $
29ccdc3305SLuigi Rizzo  *
308241616dSLuigi Rizzo  * (New) memory allocator for netmap
31ccdc3305SLuigi Rizzo  */
32ccdc3305SLuigi Rizzo 
33ccdc3305SLuigi Rizzo /*
34*2579e2d7SLuigi Rizzo  * This allocator creates three memory pools:
35ccdc3305SLuigi Rizzo  *	nm_if_pool	for the struct netmap_if
36ccdc3305SLuigi Rizzo  *	nm_ring_pool	for the struct netmap_ring
37ccdc3305SLuigi Rizzo  *	nm_buf_pool	for the packet buffers.
38ccdc3305SLuigi Rizzo  *
39*2579e2d7SLuigi Rizzo  * that contain netmap objects. Each pool is made of a number of clusters,
40*2579e2d7SLuigi Rizzo  * multiple of a page size, each containing an integer number of objects.
41*2579e2d7SLuigi Rizzo  * The clusters are contiguous in user space but not in the kernel.
42*2579e2d7SLuigi Rizzo  * Only nm_buf_pool needs to be dma-able,
43ccdc3305SLuigi Rizzo  * but for convenience use the same type of allocator for all.
44ccdc3305SLuigi Rizzo  *
45*2579e2d7SLuigi Rizzo  * Once mapped, the three pools are exported to userspace
46ccdc3305SLuigi Rizzo  * as a contiguous block, starting from nm_if_pool. Each
47ccdc3305SLuigi Rizzo  * cluster (and pool) is an integral number of pages.
48ccdc3305SLuigi Rizzo  *   [ . . . ][ . . . . . .][ . . . . . . . . . .]
49ccdc3305SLuigi Rizzo  *    nm_if     nm_ring            nm_buf
50ccdc3305SLuigi Rizzo  *
51ccdc3305SLuigi Rizzo  * The userspace areas contain offsets of the objects in userspace.
52ccdc3305SLuigi Rizzo  * When (at init time) we write these offsets, we find out the index
53ccdc3305SLuigi Rizzo  * of the object, and from there locate the offset from the beginning
54ccdc3305SLuigi Rizzo  * of the region.
55ccdc3305SLuigi Rizzo  *
568241616dSLuigi Rizzo  * The invididual allocators manage a pool of memory for objects of
578241616dSLuigi Rizzo  * the same size.
58ccdc3305SLuigi Rizzo  * The pool is split into smaller clusters, whose size is a
59ccdc3305SLuigi Rizzo  * multiple of the page size. The cluster size is chosen
60ccdc3305SLuigi Rizzo  * to minimize the waste for a given max cluster size
61*2579e2d7SLuigi Rizzo  * (we do it by brute force, as we have relatively few objects
62ccdc3305SLuigi Rizzo  * per cluster).
63ccdc3305SLuigi Rizzo  *
648241616dSLuigi Rizzo  * Objects are aligned to the cache line (64 bytes) rounding up object
658241616dSLuigi Rizzo  * sizes when needed. A bitmap contains the state of each object.
668241616dSLuigi Rizzo  * Allocation scans the bitmap; this is done only on attach, so we are not
67ccdc3305SLuigi Rizzo  * too worried about performance
68ccdc3305SLuigi Rizzo  *
698241616dSLuigi Rizzo  * For each allocator we can define (thorugh sysctl) the size and
708241616dSLuigi Rizzo  * number of each object. Memory is allocated at the first use of a
718241616dSLuigi Rizzo  * netmap file descriptor, and can be freed when all such descriptors
728241616dSLuigi Rizzo  * have been released (including unmapping the memory).
738241616dSLuigi Rizzo  * If memory is scarce, the system tries to get as much as possible
748241616dSLuigi Rizzo  * and the sysctl values reflect the actual allocation.
758241616dSLuigi Rizzo  * Together with desired values, the sysctl export also absolute
768241616dSLuigi Rizzo  * min and maximum values that cannot be overridden.
77ccdc3305SLuigi Rizzo  *
788241616dSLuigi Rizzo  * struct netmap_if:
798241616dSLuigi Rizzo  *	variable size, max 16 bytes per ring pair plus some fixed amount.
808241616dSLuigi Rizzo  *	1024 bytes should be large enough in practice.
818241616dSLuigi Rizzo  *
828241616dSLuigi Rizzo  *	In the worst case we have one netmap_if per ring in the system.
838241616dSLuigi Rizzo  *
848241616dSLuigi Rizzo  * struct netmap_ring
85*2579e2d7SLuigi Rizzo  *	variable size, 8 byte per slot plus some fixed amount.
868241616dSLuigi Rizzo  *	Rings can be large (e.g. 4k slots, or >32Kbytes).
878241616dSLuigi Rizzo  *	We default to 36 KB (9 pages), and a few hundred rings.
888241616dSLuigi Rizzo  *
898241616dSLuigi Rizzo  * struct netmap_buffer
908241616dSLuigi Rizzo  *	The more the better, both because fast interfaces tend to have
918241616dSLuigi Rizzo  *	many slots, and because we may want to use buffers to store
928241616dSLuigi Rizzo  *	packets in userspace avoiding copies.
938241616dSLuigi Rizzo  *	Must contain a full frame (eg 1518, or more for vlans, jumbo
948241616dSLuigi Rizzo  *	frames etc.) plus be nicely aligned, plus some NICs restrict
958241616dSLuigi Rizzo  *	the size to multiple of 1K or so. Default to 2K
96ccdc3305SLuigi Rizzo  */
97ccdc3305SLuigi Rizzo 
988241616dSLuigi Rizzo #define NETMAP_BUF_MAX_NUM	20*4096*2	/* large machine */
99ccdc3305SLuigi Rizzo 
1008241616dSLuigi Rizzo #ifdef linux
1018241616dSLuigi Rizzo #define NMA_LOCK_T		struct semaphore
1028241616dSLuigi Rizzo #define NMA_LOCK_INIT()		sema_init(&nm_mem.nm_mtx, 1)
1038241616dSLuigi Rizzo #define NMA_LOCK_DESTROY()
1048241616dSLuigi Rizzo #define NMA_LOCK()		down(&nm_mem.nm_mtx)
1058241616dSLuigi Rizzo #define NMA_UNLOCK()		up(&nm_mem.nm_mtx)
1068241616dSLuigi Rizzo #else /* !linux */
1078241616dSLuigi Rizzo #define NMA_LOCK_T		struct mtx
1088241616dSLuigi Rizzo #define NMA_LOCK_INIT()		mtx_init(&nm_mem.nm_mtx, "netmap memory allocator lock", NULL, MTX_DEF)
1098241616dSLuigi Rizzo #define NMA_LOCK_DESTROY()	mtx_destroy(&nm_mem.nm_mtx)
1108241616dSLuigi Rizzo #define NMA_LOCK()		mtx_lock(&nm_mem.nm_mtx)
1118241616dSLuigi Rizzo #define NMA_UNLOCK()		mtx_unlock(&nm_mem.nm_mtx)
1128241616dSLuigi Rizzo #endif /* linux */
1138241616dSLuigi Rizzo 
1148241616dSLuigi Rizzo enum {
1158241616dSLuigi Rizzo 	NETMAP_IF_POOL   = 0,
1168241616dSLuigi Rizzo 	NETMAP_RING_POOL,
1178241616dSLuigi Rizzo 	NETMAP_BUF_POOL,
1188241616dSLuigi Rizzo 	NETMAP_POOLS_NR
1198241616dSLuigi Rizzo };
1208241616dSLuigi Rizzo 
1218241616dSLuigi Rizzo 
1228241616dSLuigi Rizzo struct netmap_obj_params {
1238241616dSLuigi Rizzo 	u_int size;
1248241616dSLuigi Rizzo 	u_int num;
1258241616dSLuigi Rizzo };
1268241616dSLuigi Rizzo 
1278241616dSLuigi Rizzo 
1288241616dSLuigi Rizzo struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = {
1298241616dSLuigi Rizzo 	[NETMAP_IF_POOL] = {
1308241616dSLuigi Rizzo 		.size = 1024,
1318241616dSLuigi Rizzo 		.num  = 100,
1328241616dSLuigi Rizzo 	},
1338241616dSLuigi Rizzo 	[NETMAP_RING_POOL] = {
1348241616dSLuigi Rizzo 		.size = 9*PAGE_SIZE,
1358241616dSLuigi Rizzo 		.num  = 200,
1368241616dSLuigi Rizzo 	},
1378241616dSLuigi Rizzo 	[NETMAP_BUF_POOL] = {
1388241616dSLuigi Rizzo 		.size = 2048,
1398241616dSLuigi Rizzo 		.num  = NETMAP_BUF_MAX_NUM,
1408241616dSLuigi Rizzo 	},
1418241616dSLuigi Rizzo };
1428241616dSLuigi Rizzo 
143ccdc3305SLuigi Rizzo 
144ccdc3305SLuigi Rizzo struct netmap_obj_pool {
145ccdc3305SLuigi Rizzo 	char name[16];		/* name of the allocator */
146ccdc3305SLuigi Rizzo 	u_int objtotal;         /* actual total number of objects. */
147ccdc3305SLuigi Rizzo 	u_int objfree;          /* number of free objects. */
148ccdc3305SLuigi Rizzo 	u_int clustentries;	/* actual objects per cluster */
149ccdc3305SLuigi Rizzo 
1508241616dSLuigi Rizzo 	/* limits */
1518241616dSLuigi Rizzo 	u_int objminsize;	/* minimum object size */
1528241616dSLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
1538241616dSLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
1548241616dSLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
1558241616dSLuigi Rizzo 
156ccdc3305SLuigi Rizzo 	/* the total memory space is _numclusters*_clustsize */
157ccdc3305SLuigi Rizzo 	u_int _numclusters;	/* how many clusters */
158ccdc3305SLuigi Rizzo 	u_int _clustsize;        /* cluster size */
159ccdc3305SLuigi Rizzo 	u_int _objsize;		/* actual object size */
160ccdc3305SLuigi Rizzo 
161ccdc3305SLuigi Rizzo 	u_int _memtotal;	/* _numclusters*_clustsize */
162ccdc3305SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
163ccdc3305SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
1648241616dSLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
165ccdc3305SLuigi Rizzo };
166ccdc3305SLuigi Rizzo 
1678241616dSLuigi Rizzo 
168ccdc3305SLuigi Rizzo struct netmap_mem_d {
1698241616dSLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
170ccdc3305SLuigi Rizzo 	u_int nm_totalsize; /* shorthand */
171ccdc3305SLuigi Rizzo 
1728241616dSLuigi Rizzo 	int finalized;		/* !=0 iff preallocation done */
1738241616dSLuigi Rizzo 	int lasterr;		/* last error for curr config */
1748241616dSLuigi Rizzo 	int refcount;		/* existing priv structures */
1758241616dSLuigi Rizzo 	/* the three allocators */
1768241616dSLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
1778241616dSLuigi Rizzo };
1788241616dSLuigi Rizzo 
179*2579e2d7SLuigi Rizzo /*
180*2579e2d7SLuigi Rizzo  * nm_mem is the memory allocator used for all physical interfaces
181*2579e2d7SLuigi Rizzo  * running in netmap mode.
182*2579e2d7SLuigi Rizzo  * Virtual (VALE) ports will have each its own allocator.
183*2579e2d7SLuigi Rizzo  */
1848241616dSLuigi Rizzo static struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
1858241616dSLuigi Rizzo 	.pools = {
1868241616dSLuigi Rizzo 		[NETMAP_IF_POOL] = {
1878241616dSLuigi Rizzo 			.name 	= "netmap_if",
1888241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
1898241616dSLuigi Rizzo 			.objmaxsize = 4096,
1908241616dSLuigi Rizzo 			.nummin     = 10,	/* don't be stingy */
1918241616dSLuigi Rizzo 			.nummax	    = 10000,	/* XXX very large */
1928241616dSLuigi Rizzo 		},
1938241616dSLuigi Rizzo 		[NETMAP_RING_POOL] = {
1948241616dSLuigi Rizzo 			.name 	= "netmap_ring",
1958241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
1968241616dSLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
1978241616dSLuigi Rizzo 			.nummin     = 2,
1988241616dSLuigi Rizzo 			.nummax	    = 1024,
1998241616dSLuigi Rizzo 		},
2008241616dSLuigi Rizzo 		[NETMAP_BUF_POOL] = {
2018241616dSLuigi Rizzo 			.name	= "netmap_buf",
2028241616dSLuigi Rizzo 			.objminsize = 64,
2038241616dSLuigi Rizzo 			.objmaxsize = 65536,
2048241616dSLuigi Rizzo 			.nummin     = 4,
2058241616dSLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
2068241616dSLuigi Rizzo 		},
2078241616dSLuigi Rizzo 	},
208ccdc3305SLuigi Rizzo };
209ccdc3305SLuigi Rizzo 
210*2579e2d7SLuigi Rizzo // XXX logically belongs to nm_mem
211ccdc3305SLuigi Rizzo struct lut_entry *netmap_buffer_lut;	/* exported */
212ccdc3305SLuigi Rizzo 
2138241616dSLuigi Rizzo /* memory allocator related sysctls */
2148241616dSLuigi Rizzo 
2158241616dSLuigi Rizzo #define STRINGIFY(x) #x
2168241616dSLuigi Rizzo 
2178241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \
2188241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
2198241616dSLuigi Rizzo 	    CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
2208241616dSLuigi Rizzo         SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
2218241616dSLuigi Rizzo             CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
2228241616dSLuigi Rizzo         SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
2238241616dSLuigi Rizzo             CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
2248241616dSLuigi Rizzo         SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
2258241616dSLuigi Rizzo             CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s")
2268241616dSLuigi Rizzo 
2278241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
2288241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
2298241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
230ccdc3305SLuigi Rizzo 
231ccdc3305SLuigi Rizzo /*
232*2579e2d7SLuigi Rizzo  * Convert a userspace offset to a physical address.
233*2579e2d7SLuigi Rizzo  * XXX only called in the FreeBSD's netmap_mmap()
234*2579e2d7SLuigi Rizzo  * because in linux we map everything at once.
235ccdc3305SLuigi Rizzo  *
236*2579e2d7SLuigi Rizzo  * First, find the allocator that contains the requested offset,
237*2579e2d7SLuigi Rizzo  * then locate the cluster through a lookup table.
238ccdc3305SLuigi Rizzo  */
239ccdc3305SLuigi Rizzo static inline vm_paddr_t
240ccdc3305SLuigi Rizzo netmap_ofstophys(vm_offset_t offset)
241ccdc3305SLuigi Rizzo {
242ccdc3305SLuigi Rizzo 	int i;
243ccdc3305SLuigi Rizzo 	vm_offset_t o = offset;
2448241616dSLuigi Rizzo 	struct netmap_obj_pool *p = nm_mem.pools;
245ccdc3305SLuigi Rizzo 
2468241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i]._memtotal, i++) {
2478241616dSLuigi Rizzo 		if (offset >= p[i]._memtotal)
248ccdc3305SLuigi Rizzo 			continue;
249*2579e2d7SLuigi Rizzo 		// now lookup the cluster's address
2508241616dSLuigi Rizzo 		return p[i].lut[offset / p[i]._objsize].paddr +
2518241616dSLuigi Rizzo 			offset % p[i]._objsize;
252ccdc3305SLuigi Rizzo 	}
2538241616dSLuigi Rizzo 	/* this is only in case of errors */
254b1123b01SLuigi Rizzo 	D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
2558241616dSLuigi Rizzo 		p[NETMAP_IF_POOL]._memtotal,
2568241616dSLuigi Rizzo 		p[NETMAP_IF_POOL]._memtotal
2578241616dSLuigi Rizzo 			+ p[NETMAP_RING_POOL]._memtotal,
2588241616dSLuigi Rizzo 		p[NETMAP_IF_POOL]._memtotal
2598241616dSLuigi Rizzo 			+ p[NETMAP_RING_POOL]._memtotal
2608241616dSLuigi Rizzo 			+ p[NETMAP_BUF_POOL]._memtotal);
261ccdc3305SLuigi Rizzo 	return 0;	// XXX bad address
262ccdc3305SLuigi Rizzo }
263ccdc3305SLuigi Rizzo 
264ccdc3305SLuigi Rizzo /*
265ccdc3305SLuigi Rizzo  * we store objects by kernel address, need to find the offset
266ccdc3305SLuigi Rizzo  * within the pool to export the value to userspace.
267ccdc3305SLuigi Rizzo  * Algorithm: scan until we find the cluster, then add the
268ccdc3305SLuigi Rizzo  * actual offset in the cluster
269ccdc3305SLuigi Rizzo  */
270ce2cb792SLuigi Rizzo static ssize_t
271ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
272ccdc3305SLuigi Rizzo {
273ccdc3305SLuigi Rizzo 	int i, k = p->clustentries, n = p->objtotal;
274ccdc3305SLuigi Rizzo 	ssize_t ofs = 0;
275ccdc3305SLuigi Rizzo 
276ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
277ccdc3305SLuigi Rizzo 		const char *base = p->lut[i].vaddr;
278ccdc3305SLuigi Rizzo 		ssize_t relofs = (const char *) vaddr - base;
279ccdc3305SLuigi Rizzo 
280aa76317cSLuigi Rizzo 		if (relofs < 0 || relofs >= p->_clustsize)
281ccdc3305SLuigi Rizzo 			continue;
282ccdc3305SLuigi Rizzo 
283ccdc3305SLuigi Rizzo 		ofs = ofs + relofs;
284ccdc3305SLuigi Rizzo 		ND("%s: return offset %d (cluster %d) for pointer %p",
285ccdc3305SLuigi Rizzo 		    p->name, ofs, i, vaddr);
286ccdc3305SLuigi Rizzo 		return ofs;
287ccdc3305SLuigi Rizzo 	}
288ccdc3305SLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
289ccdc3305SLuigi Rizzo 	    vaddr, p->name);
290ccdc3305SLuigi Rizzo 	return 0; /* An error occurred */
291ccdc3305SLuigi Rizzo }
292ccdc3305SLuigi Rizzo 
293ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */
294ccdc3305SLuigi Rizzo #define netmap_if_offset(v)					\
2958241616dSLuigi Rizzo 	netmap_obj_offset(&nm_mem.pools[NETMAP_IF_POOL], (v))
296ccdc3305SLuigi Rizzo 
297ccdc3305SLuigi Rizzo #define netmap_ring_offset(v)					\
2988241616dSLuigi Rizzo     (nm_mem.pools[NETMAP_IF_POOL]._memtotal + 			\
2998241616dSLuigi Rizzo 	netmap_obj_offset(&nm_mem.pools[NETMAP_RING_POOL], (v)))
300ccdc3305SLuigi Rizzo 
301ccdc3305SLuigi Rizzo #define netmap_buf_offset(v)					\
3028241616dSLuigi Rizzo     (nm_mem.pools[NETMAP_IF_POOL]._memtotal +			\
3038241616dSLuigi Rizzo 	nm_mem.pools[NETMAP_RING_POOL]._memtotal +		\
3048241616dSLuigi Rizzo 	netmap_obj_offset(&nm_mem.pools[NETMAP_BUF_POOL], (v)))
305ccdc3305SLuigi Rizzo 
306ccdc3305SLuigi Rizzo 
3078241616dSLuigi Rizzo /*
3088241616dSLuigi Rizzo  * report the index, and use start position as a hint,
3098241616dSLuigi Rizzo  * otherwise buffer allocation becomes terribly expensive.
3108241616dSLuigi Rizzo  */
311ccdc3305SLuigi Rizzo static void *
3128241616dSLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, int len, uint32_t *start, uint32_t *index)
313ccdc3305SLuigi Rizzo {
314ccdc3305SLuigi Rizzo 	uint32_t i = 0;			/* index in the bitmap */
315ccdc3305SLuigi Rizzo 	uint32_t mask, j;		/* slot counter */
316ccdc3305SLuigi Rizzo 	void *vaddr = NULL;
317ccdc3305SLuigi Rizzo 
318ccdc3305SLuigi Rizzo 	if (len > p->_objsize) {
319ccdc3305SLuigi Rizzo 		D("%s request size %d too large", p->name, len);
320ccdc3305SLuigi Rizzo 		// XXX cannot reduce the size
321ccdc3305SLuigi Rizzo 		return NULL;
322ccdc3305SLuigi Rizzo 	}
323ccdc3305SLuigi Rizzo 
324ccdc3305SLuigi Rizzo 	if (p->objfree == 0) {
325ccdc3305SLuigi Rizzo 		D("%s allocator: run out of memory", p->name);
326ccdc3305SLuigi Rizzo 		return NULL;
327ccdc3305SLuigi Rizzo 	}
3288241616dSLuigi Rizzo 	if (start)
3298241616dSLuigi Rizzo 		i = *start;
330ccdc3305SLuigi Rizzo 
3318241616dSLuigi Rizzo 	/* termination is guaranteed by p->free, but better check bounds on i */
3328241616dSLuigi Rizzo 	while (vaddr == NULL && i < p->bitmap_slots)  {
333ccdc3305SLuigi Rizzo 		uint32_t cur = p->bitmap[i];
334ccdc3305SLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
335ccdc3305SLuigi Rizzo 			i++;
336ccdc3305SLuigi Rizzo 			continue;
337ccdc3305SLuigi Rizzo 		}
338ccdc3305SLuigi Rizzo 		/* locate a slot */
339ccdc3305SLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
340ccdc3305SLuigi Rizzo 			;
341ccdc3305SLuigi Rizzo 
342ccdc3305SLuigi Rizzo 		p->bitmap[i] &= ~mask; /* mark object as in use */
343ccdc3305SLuigi Rizzo 		p->objfree--;
344ccdc3305SLuigi Rizzo 
345ccdc3305SLuigi Rizzo 		vaddr = p->lut[i * 32 + j].vaddr;
3468241616dSLuigi Rizzo 		if (index)
3478241616dSLuigi Rizzo 			*index = i * 32 + j;
348ccdc3305SLuigi Rizzo 	}
349ccdc3305SLuigi Rizzo 	ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr);
350ccdc3305SLuigi Rizzo 
3518241616dSLuigi Rizzo 	if (start)
3528241616dSLuigi Rizzo 		*start = i;
353ccdc3305SLuigi Rizzo 	return vaddr;
354ccdc3305SLuigi Rizzo }
355ccdc3305SLuigi Rizzo 
356ccdc3305SLuigi Rizzo 
357ccdc3305SLuigi Rizzo /*
358*2579e2d7SLuigi Rizzo  * free by index, not by address. This is slow, but is only used
359*2579e2d7SLuigi Rizzo  * for a small number of objects (rings, nifp)
360ccdc3305SLuigi Rizzo  */
361ccdc3305SLuigi Rizzo static void
362ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
363ccdc3305SLuigi Rizzo {
364ccdc3305SLuigi Rizzo 	if (j >= p->objtotal) {
365ccdc3305SLuigi Rizzo 		D("invalid index %u, max %u", j, p->objtotal);
366ccdc3305SLuigi Rizzo 		return;
367ccdc3305SLuigi Rizzo 	}
368ccdc3305SLuigi Rizzo 	p->bitmap[j / 32] |= (1 << (j % 32));
369ccdc3305SLuigi Rizzo 	p->objfree++;
370ccdc3305SLuigi Rizzo 	return;
371ccdc3305SLuigi Rizzo }
372ccdc3305SLuigi Rizzo 
373ccdc3305SLuigi Rizzo static void
374ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
375ccdc3305SLuigi Rizzo {
376ccdc3305SLuigi Rizzo 	int i, j, n = p->_memtotal / p->_clustsize;
377ccdc3305SLuigi Rizzo 
378ccdc3305SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->clustentries) {
379ccdc3305SLuigi Rizzo 		void *base = p->lut[i * p->clustentries].vaddr;
380ccdc3305SLuigi Rizzo 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
381ccdc3305SLuigi Rizzo 
382ccdc3305SLuigi Rizzo 		/* Given address, is out of the scope of the current cluster.*/
383ccdc3305SLuigi Rizzo 		if (vaddr < base || relofs > p->_clustsize)
384ccdc3305SLuigi Rizzo 			continue;
385ccdc3305SLuigi Rizzo 
386ccdc3305SLuigi Rizzo 		j = j + relofs / p->_objsize;
387ccdc3305SLuigi Rizzo 		KASSERT(j != 0, ("Cannot free object 0"));
388ccdc3305SLuigi Rizzo 		netmap_obj_free(p, j);
389ccdc3305SLuigi Rizzo 		return;
390ccdc3305SLuigi Rizzo 	}
391ae10d1afSLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
392ccdc3305SLuigi Rizzo 	    vaddr, p->name);
393ccdc3305SLuigi Rizzo }
394ccdc3305SLuigi Rizzo 
3958241616dSLuigi Rizzo #define netmap_if_malloc(len)	netmap_obj_malloc(&nm_mem.pools[NETMAP_IF_POOL], len, NULL, NULL)
3968241616dSLuigi Rizzo #define netmap_if_free(v)	netmap_obj_free_va(&nm_mem.pools[NETMAP_IF_POOL], (v))
3978241616dSLuigi Rizzo #define netmap_ring_malloc(len)	netmap_obj_malloc(&nm_mem.pools[NETMAP_RING_POOL], len, NULL, NULL)
3988241616dSLuigi Rizzo #define netmap_ring_free(v)	netmap_obj_free_va(&nm_mem.pools[NETMAP_RING_POOL], (v))
3998241616dSLuigi Rizzo #define netmap_buf_malloc(_pos, _index)			\
4008241616dSLuigi Rizzo 	netmap_obj_malloc(&nm_mem.pools[NETMAP_BUF_POOL], NETMAP_BUF_SIZE, _pos, _index)
401ccdc3305SLuigi Rizzo 
402ccdc3305SLuigi Rizzo 
403ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */
404ccdc3305SLuigi Rizzo #define netmap_buf_index(v)						\
4058241616dSLuigi Rizzo     (netmap_obj_offset(&nm_mem.pools[NETMAP_BUF_POOL], (v)) / nm_mem.pools[NETMAP_BUF_POOL]._objsize)
406ccdc3305SLuigi Rizzo 
407ccdc3305SLuigi Rizzo 
4088241616dSLuigi Rizzo /* Return nonzero on error */
4098241616dSLuigi Rizzo static int
4100b8ed8e0SLuigi Rizzo netmap_new_bufs(struct netmap_if *nifp,
411ccdc3305SLuigi Rizzo                 struct netmap_slot *slot, u_int n)
412ccdc3305SLuigi Rizzo {
4138241616dSLuigi Rizzo 	struct netmap_obj_pool *p = &nm_mem.pools[NETMAP_BUF_POOL];
4148241616dSLuigi Rizzo 	int i = 0;	/* slot counter */
4158241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
4168241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
417ccdc3305SLuigi Rizzo 
4180b8ed8e0SLuigi Rizzo 	(void)nifp;	/* UNUSED */
419ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
4208241616dSLuigi Rizzo 		void *vaddr = netmap_buf_malloc(&pos, &index);
421ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
422ccdc3305SLuigi Rizzo 			D("unable to locate empty packet buffer");
423ccdc3305SLuigi Rizzo 			goto cleanup;
424ccdc3305SLuigi Rizzo 		}
4258241616dSLuigi Rizzo 		slot[i].buf_idx = index;
426ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
4278241616dSLuigi Rizzo 		/* XXX setting flags=NS_BUF_CHANGED forces a pointer reload
4288241616dSLuigi Rizzo 		 * in the NIC ring. This is a hack that hides missing
4298241616dSLuigi Rizzo 		 * initializations in the drivers, and should go away.
4308241616dSLuigi Rizzo 		 */
431*2579e2d7SLuigi Rizzo 		// slot[i].flags = NS_BUF_CHANGED;
432ccdc3305SLuigi Rizzo 	}
433ccdc3305SLuigi Rizzo 
4348241616dSLuigi Rizzo 	ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
4358241616dSLuigi Rizzo 	return (0);
436ccdc3305SLuigi Rizzo 
437ccdc3305SLuigi Rizzo cleanup:
4384cf8455fSEd Maste 	while (i > 0) {
4394cf8455fSEd Maste 		i--;
4408241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
441ccdc3305SLuigi Rizzo 	}
4428241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
4438241616dSLuigi Rizzo 	return (ENOMEM);
444ccdc3305SLuigi Rizzo }
445ccdc3305SLuigi Rizzo 
446ccdc3305SLuigi Rizzo 
447ccdc3305SLuigi Rizzo static void
448ccdc3305SLuigi Rizzo netmap_free_buf(struct netmap_if *nifp, uint32_t i)
449ccdc3305SLuigi Rizzo {
4508241616dSLuigi Rizzo 	struct netmap_obj_pool *p = &nm_mem.pools[NETMAP_BUF_POOL];
4518241616dSLuigi Rizzo 
452ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
453ccdc3305SLuigi Rizzo 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
454ccdc3305SLuigi Rizzo 		return;
455ccdc3305SLuigi Rizzo 	}
4568241616dSLuigi Rizzo 	netmap_obj_free(p, i);
457ccdc3305SLuigi Rizzo }
458ccdc3305SLuigi Rizzo 
4598241616dSLuigi Rizzo static void
4608241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
4618241616dSLuigi Rizzo {
4628241616dSLuigi Rizzo 	if (p == NULL)
4638241616dSLuigi Rizzo 		return;
4648241616dSLuigi Rizzo 	if (p->bitmap)
4658241616dSLuigi Rizzo 		free(p->bitmap, M_NETMAP);
4668241616dSLuigi Rizzo 	p->bitmap = NULL;
4678241616dSLuigi Rizzo 	if (p->lut) {
4688241616dSLuigi Rizzo 		int i;
4698241616dSLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->clustentries) {
4708241616dSLuigi Rizzo 			if (p->lut[i].vaddr)
4718241616dSLuigi Rizzo 				contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
4728241616dSLuigi Rizzo 		}
4738241616dSLuigi Rizzo 		bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
4748241616dSLuigi Rizzo #ifdef linux
4758241616dSLuigi Rizzo 		vfree(p->lut);
4768241616dSLuigi Rizzo #else
4778241616dSLuigi Rizzo 		free(p->lut, M_NETMAP);
4788241616dSLuigi Rizzo #endif
4798241616dSLuigi Rizzo 	}
4808241616dSLuigi Rizzo 	p->lut = NULL;
4818241616dSLuigi Rizzo }
482ccdc3305SLuigi Rizzo 
483ccdc3305SLuigi Rizzo /*
484ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
485ccdc3305SLuigi Rizzo  */
486ccdc3305SLuigi Rizzo static void
487ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
488ccdc3305SLuigi Rizzo {
489ccdc3305SLuigi Rizzo 	if (p == NULL)
490ccdc3305SLuigi Rizzo 		return;
4918241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
492ccdc3305SLuigi Rizzo }
493ccdc3305SLuigi Rizzo 
494ccdc3305SLuigi Rizzo /*
495ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
496ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
497ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
498ccdc3305SLuigi Rizzo  * In the allocator we don't need to store the objsize,
499ccdc3305SLuigi Rizzo  * but we do need to keep track of objtotal' and clustentries,
500ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
501ccdc3305SLuigi Rizzo  *
502ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
503ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
504ccdc3305SLuigi Rizzo  */
5058241616dSLuigi Rizzo 
5068241616dSLuigi Rizzo 
5078241616dSLuigi Rizzo /* call with NMA_LOCK held */
5088241616dSLuigi Rizzo static int
5098241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
510ccdc3305SLuigi Rizzo {
511ccdc3305SLuigi Rizzo 	int i, n;
512ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
513ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
514ccdc3305SLuigi Rizzo 
515ccdc3305SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<17)
516ccdc3305SLuigi Rizzo #define LINE_ROUND	64
517ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
518ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
519ccdc3305SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
5208241616dSLuigi Rizzo 		goto error;
521ccdc3305SLuigi Rizzo 	}
522ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
523ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
524ccdc3305SLuigi Rizzo 	if (i) {
525ccdc3305SLuigi Rizzo 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
526ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
527ccdc3305SLuigi Rizzo 	}
5288241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
5298241616dSLuigi Rizzo 		D("requested objsize %d out of range [%d, %d]",
5308241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
5318241616dSLuigi Rizzo 		goto error;
5328241616dSLuigi Rizzo 	}
5338241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
5348241616dSLuigi Rizzo 		D("requested objtotal %d out of range [%d, %d]",
5358241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
5368241616dSLuigi Rizzo 		goto error;
5378241616dSLuigi Rizzo 	}
538ccdc3305SLuigi Rizzo 	/*
539ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
540ccdc3305SLuigi Rizzo 	 * given a max cluster size,
541ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
542ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
543ccdc3305SLuigi Rizzo 	 */
544ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
545ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
546ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
547ccdc3305SLuigi Rizzo 			break;
548ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
549ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
550ccdc3305SLuigi Rizzo 			clustentries = i;
551ccdc3305SLuigi Rizzo 			break;
552ccdc3305SLuigi Rizzo 		}
553ccdc3305SLuigi Rizzo 		if (delta > ( (clustentries*objsize) % PAGE_SIZE) )
554ccdc3305SLuigi Rizzo 			clustentries = i;
555ccdc3305SLuigi Rizzo 	}
556ccdc3305SLuigi Rizzo 	// D("XXX --- ouch, delta %d (bad for buffers)", delta);
557ccdc3305SLuigi Rizzo 	/* compute clustsize and round to the next page */
558ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
559ccdc3305SLuigi Rizzo 	i =  (clustsize & (PAGE_SIZE - 1));
560ccdc3305SLuigi Rizzo 	if (i)
561ccdc3305SLuigi Rizzo 		clustsize += PAGE_SIZE - i;
562ae10d1afSLuigi Rizzo 	if (netmap_verbose)
563ccdc3305SLuigi Rizzo 		D("objsize %d clustsize %d objects %d",
564ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
565ccdc3305SLuigi Rizzo 
566ccdc3305SLuigi Rizzo 	/*
567ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
568ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
569ccdc3305SLuigi Rizzo 	 */
570ccdc3305SLuigi Rizzo 	p->clustentries = clustentries;
571ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
572ccdc3305SLuigi Rizzo 	n = (objtotal + clustentries - 1) / clustentries;
573ccdc3305SLuigi Rizzo 	p->_numclusters = n;
574ccdc3305SLuigi Rizzo 	p->objtotal = n * clustentries;
575ccdc3305SLuigi Rizzo 	p->objfree = p->objtotal - 2; /* obj 0 and 1 are reserved */
576ccdc3305SLuigi Rizzo 	p->_memtotal = p->_numclusters * p->_clustsize;
5778241616dSLuigi Rizzo 	p->_objsize = objsize;
578ccdc3305SLuigi Rizzo 
5798241616dSLuigi Rizzo 	return 0;
5808241616dSLuigi Rizzo 
5818241616dSLuigi Rizzo error:
5828241616dSLuigi Rizzo 	p->_objsize = objsize;
5838241616dSLuigi Rizzo 	p->objtotal = objtotal;
5848241616dSLuigi Rizzo 
5858241616dSLuigi Rizzo 	return EINVAL;
5868241616dSLuigi Rizzo }
5878241616dSLuigi Rizzo 
5888241616dSLuigi Rizzo 
5898241616dSLuigi Rizzo /* call with NMA_LOCK held */
5908241616dSLuigi Rizzo static int
5918241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
5928241616dSLuigi Rizzo {
5938241616dSLuigi Rizzo 	int i, n;
5948241616dSLuigi Rizzo 
5958241616dSLuigi Rizzo 	n = sizeof(struct lut_entry) * p->objtotal;
5968241616dSLuigi Rizzo #ifdef linux
5978241616dSLuigi Rizzo 	p->lut = vmalloc(n);
5988241616dSLuigi Rizzo #else
599d2b91851SEd Maste 	p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
6008241616dSLuigi Rizzo #endif
601ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
6028241616dSLuigi Rizzo 		D("Unable to create lookup table (%d bytes) for '%s'", n, p->name);
603ccdc3305SLuigi Rizzo 		goto clean;
604ccdc3305SLuigi Rizzo 	}
605ccdc3305SLuigi Rizzo 
606ccdc3305SLuigi Rizzo 	/* Allocate the bitmap */
607ccdc3305SLuigi Rizzo 	n = (p->objtotal + 31) / 32;
608d2b91851SEd Maste 	p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
609ccdc3305SLuigi Rizzo 	if (p->bitmap == NULL) {
610ccdc3305SLuigi Rizzo 		D("Unable to create bitmap (%d entries) for allocator '%s'", n,
6118241616dSLuigi Rizzo 		    p->name);
612ccdc3305SLuigi Rizzo 		goto clean;
613ccdc3305SLuigi Rizzo 	}
6148241616dSLuigi Rizzo 	p->bitmap_slots = n;
615ccdc3305SLuigi Rizzo 
616ccdc3305SLuigi Rizzo 	/*
617ccdc3305SLuigi Rizzo 	 * Allocate clusters, init pointers and bitmap
618ccdc3305SLuigi Rizzo 	 */
619ccdc3305SLuigi Rizzo 	for (i = 0; i < p->objtotal;) {
6208241616dSLuigi Rizzo 		int lim = i + p->clustentries;
621ccdc3305SLuigi Rizzo 		char *clust;
622ccdc3305SLuigi Rizzo 
6238241616dSLuigi Rizzo 		clust = contigmalloc(p->_clustsize, M_NETMAP, M_NOWAIT | M_ZERO,
624ccdc3305SLuigi Rizzo 		    0, -1UL, PAGE_SIZE, 0);
625ccdc3305SLuigi Rizzo 		if (clust == NULL) {
626ccdc3305SLuigi Rizzo 			/*
627ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
628ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
6298241616dSLuigi Rizzo 			 * XXX check boundaries
630ccdc3305SLuigi Rizzo 			 */
631ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
6328241616dSLuigi Rizzo 			    i, p->name);
633ccdc3305SLuigi Rizzo 			lim = i / 2;
6348241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
635ccdc3305SLuigi Rizzo 				p->bitmap[ (i>>5) ] &=  ~( 1 << (i & 31) );
6368241616dSLuigi Rizzo 				if (i % p->clustentries == 0 && p->lut[i].vaddr)
637ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
638ccdc3305SLuigi Rizzo 						p->_clustsize, M_NETMAP);
639ccdc3305SLuigi Rizzo 			}
640ccdc3305SLuigi Rizzo 			p->objtotal = i;
641ccdc3305SLuigi Rizzo 			p->objfree = p->objtotal - 2;
6428241616dSLuigi Rizzo 			p->_numclusters = i / p->clustentries;
643ccdc3305SLuigi Rizzo 			p->_memtotal = p->_numclusters * p->_clustsize;
644ccdc3305SLuigi Rizzo 			break;
645ccdc3305SLuigi Rizzo 		}
6468241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
647ccdc3305SLuigi Rizzo 			p->bitmap[ (i>>5) ] |=  ( 1 << (i & 31) );
648ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
649ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
650ccdc3305SLuigi Rizzo 		}
651ccdc3305SLuigi Rizzo 	}
652ccdc3305SLuigi Rizzo 	p->bitmap[0] = ~3; /* objs 0 and 1 is always busy */
653ae10d1afSLuigi Rizzo 	if (netmap_verbose)
654ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
655ccdc3305SLuigi Rizzo 		    p->_numclusters, p->_clustsize >> 10,
6568241616dSLuigi Rizzo 		    p->_memtotal >> 10, p->name);
657ccdc3305SLuigi Rizzo 
6588241616dSLuigi Rizzo 	return 0;
659ccdc3305SLuigi Rizzo 
660ccdc3305SLuigi Rizzo clean:
6618241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
6628241616dSLuigi Rizzo 	return ENOMEM;
6638241616dSLuigi Rizzo }
6648241616dSLuigi Rizzo 
6658241616dSLuigi Rizzo /* call with lock held */
6668241616dSLuigi Rizzo static int
6678241616dSLuigi Rizzo netmap_memory_config_changed(void)
6688241616dSLuigi Rizzo {
6698241616dSLuigi Rizzo 	int i;
6708241616dSLuigi Rizzo 
6718241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
6728241616dSLuigi Rizzo 		if (nm_mem.pools[i]._objsize != netmap_params[i].size ||
6738241616dSLuigi Rizzo 		    nm_mem.pools[i].objtotal != netmap_params[i].num)
6748241616dSLuigi Rizzo 		    return 1;
6758241616dSLuigi Rizzo 	}
6768241616dSLuigi Rizzo 	return 0;
6778241616dSLuigi Rizzo }
6788241616dSLuigi Rizzo 
6798241616dSLuigi Rizzo 
6808241616dSLuigi Rizzo /* call with lock held */
6818241616dSLuigi Rizzo static int
6828241616dSLuigi Rizzo netmap_memory_config(void)
6838241616dSLuigi Rizzo {
6848241616dSLuigi Rizzo 	int i;
6858241616dSLuigi Rizzo 
6868241616dSLuigi Rizzo 	if (!netmap_memory_config_changed())
6878241616dSLuigi Rizzo 		goto out;
6888241616dSLuigi Rizzo 
6898241616dSLuigi Rizzo 	D("reconfiguring");
6908241616dSLuigi Rizzo 
6918241616dSLuigi Rizzo 	if (nm_mem.finalized) {
6928241616dSLuigi Rizzo 		/* reset previous allocation */
6938241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
6948241616dSLuigi Rizzo 			netmap_reset_obj_allocator(&nm_mem.pools[i]);
6958241616dSLuigi Rizzo 		}
6968241616dSLuigi Rizzo 		nm_mem.finalized = 0;
6978241616dSLuigi Rizzo         }
6988241616dSLuigi Rizzo 
6998241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
7008241616dSLuigi Rizzo 		nm_mem.lasterr = netmap_config_obj_allocator(&nm_mem.pools[i],
7018241616dSLuigi Rizzo 				netmap_params[i].num, netmap_params[i].size);
7028241616dSLuigi Rizzo 		if (nm_mem.lasterr)
7038241616dSLuigi Rizzo 			goto out;
7048241616dSLuigi Rizzo 	}
7058241616dSLuigi Rizzo 
7068241616dSLuigi Rizzo 	D("Have %d KB for interfaces, %d KB for rings and %d MB for buffers",
7078241616dSLuigi Rizzo 	    nm_mem.pools[NETMAP_IF_POOL]._memtotal >> 10,
7088241616dSLuigi Rizzo 	    nm_mem.pools[NETMAP_RING_POOL]._memtotal >> 10,
7098241616dSLuigi Rizzo 	    nm_mem.pools[NETMAP_BUF_POOL]._memtotal >> 20);
7108241616dSLuigi Rizzo 
7118241616dSLuigi Rizzo out:
7128241616dSLuigi Rizzo 
7138241616dSLuigi Rizzo 	return nm_mem.lasterr;
7148241616dSLuigi Rizzo }
7158241616dSLuigi Rizzo 
7168241616dSLuigi Rizzo /* call with lock held */
7178241616dSLuigi Rizzo static int
7188241616dSLuigi Rizzo netmap_memory_finalize(void)
7198241616dSLuigi Rizzo {
7208241616dSLuigi Rizzo 	int i;
7218241616dSLuigi Rizzo 	u_int totalsize = 0;
7228241616dSLuigi Rizzo 
7238241616dSLuigi Rizzo 	nm_mem.refcount++;
7248241616dSLuigi Rizzo 	if (nm_mem.refcount > 1) {
725ae10d1afSLuigi Rizzo 		ND("busy (refcount %d)", nm_mem.refcount);
7268241616dSLuigi Rizzo 		goto out;
7278241616dSLuigi Rizzo 	}
7288241616dSLuigi Rizzo 
7298241616dSLuigi Rizzo 	/* update configuration if changed */
7308241616dSLuigi Rizzo 	if (netmap_memory_config())
7318241616dSLuigi Rizzo 		goto out;
7328241616dSLuigi Rizzo 
7338241616dSLuigi Rizzo 	if (nm_mem.finalized) {
7348241616dSLuigi Rizzo 		/* may happen if config is not changed */
7358241616dSLuigi Rizzo 		ND("nothing to do");
7368241616dSLuigi Rizzo 		goto out;
7378241616dSLuigi Rizzo 	}
7388241616dSLuigi Rizzo 
7398241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
7408241616dSLuigi Rizzo 		nm_mem.lasterr = netmap_finalize_obj_allocator(&nm_mem.pools[i]);
7418241616dSLuigi Rizzo 		if (nm_mem.lasterr)
7428241616dSLuigi Rizzo 			goto cleanup;
7438241616dSLuigi Rizzo 		totalsize += nm_mem.pools[i]._memtotal;
7448241616dSLuigi Rizzo 	}
7458241616dSLuigi Rizzo 	nm_mem.nm_totalsize = totalsize;
7468241616dSLuigi Rizzo 
7478241616dSLuigi Rizzo 	/* backward compatibility */
7488241616dSLuigi Rizzo 	netmap_buf_size = nm_mem.pools[NETMAP_BUF_POOL]._objsize;
7498241616dSLuigi Rizzo 	netmap_total_buffers = nm_mem.pools[NETMAP_BUF_POOL].objtotal;
7508241616dSLuigi Rizzo 
7518241616dSLuigi Rizzo 	netmap_buffer_lut = nm_mem.pools[NETMAP_BUF_POOL].lut;
7528241616dSLuigi Rizzo 	netmap_buffer_base = nm_mem.pools[NETMAP_BUF_POOL].lut[0].vaddr;
7538241616dSLuigi Rizzo 
7548241616dSLuigi Rizzo 	nm_mem.finalized = 1;
7558241616dSLuigi Rizzo 	nm_mem.lasterr = 0;
7568241616dSLuigi Rizzo 
7578241616dSLuigi Rizzo 	/* make sysctl values match actual values in the pools */
7588241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
7598241616dSLuigi Rizzo 		netmap_params[i].size = nm_mem.pools[i]._objsize;
7608241616dSLuigi Rizzo 		netmap_params[i].num  = nm_mem.pools[i].objtotal;
7618241616dSLuigi Rizzo 	}
7628241616dSLuigi Rizzo 
7638241616dSLuigi Rizzo out:
7648241616dSLuigi Rizzo 	if (nm_mem.lasterr)
7658241616dSLuigi Rizzo 		nm_mem.refcount--;
7668241616dSLuigi Rizzo 
7678241616dSLuigi Rizzo 	return nm_mem.lasterr;
7688241616dSLuigi Rizzo 
7698241616dSLuigi Rizzo cleanup:
7708241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
7718241616dSLuigi Rizzo 		netmap_reset_obj_allocator(&nm_mem.pools[i]);
7728241616dSLuigi Rizzo 	}
7738241616dSLuigi Rizzo 	nm_mem.refcount--;
7748241616dSLuigi Rizzo 
7758241616dSLuigi Rizzo 	return nm_mem.lasterr;
776ccdc3305SLuigi Rizzo }
777ccdc3305SLuigi Rizzo 
778ccdc3305SLuigi Rizzo static int
779ccdc3305SLuigi Rizzo netmap_memory_init(void)
780ccdc3305SLuigi Rizzo {
7818241616dSLuigi Rizzo 	NMA_LOCK_INIT();
7828241616dSLuigi Rizzo 	return (0);
783ccdc3305SLuigi Rizzo }
784ccdc3305SLuigi Rizzo 
785ccdc3305SLuigi Rizzo static void
786ccdc3305SLuigi Rizzo netmap_memory_fini(void)
787ccdc3305SLuigi Rizzo {
7888241616dSLuigi Rizzo 	int i;
7898241616dSLuigi Rizzo 
7908241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
7918241616dSLuigi Rizzo 	    netmap_destroy_obj_allocator(&nm_mem.pools[i]);
7928241616dSLuigi Rizzo 	}
7938241616dSLuigi Rizzo 	NMA_LOCK_DESTROY();
7948241616dSLuigi Rizzo }
7958241616dSLuigi Rizzo 
7968241616dSLuigi Rizzo static void
7978241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
7988241616dSLuigi Rizzo {
7998241616dSLuigi Rizzo 	int i;
800ae10d1afSLuigi Rizzo 	if (!na->tx_rings)
801ae10d1afSLuigi Rizzo 		return;
8028241616dSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
8038241616dSLuigi Rizzo 		netmap_ring_free(na->tx_rings[i].ring);
8048241616dSLuigi Rizzo 		na->tx_rings[i].ring = NULL;
8058241616dSLuigi Rizzo 	}
8068241616dSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
8078241616dSLuigi Rizzo 		netmap_ring_free(na->rx_rings[i].ring);
8088241616dSLuigi Rizzo 		na->rx_rings[i].ring = NULL;
8098241616dSLuigi Rizzo 	}
810ae10d1afSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
811ae10d1afSLuigi Rizzo 	na->tx_rings = na->rx_rings = NULL;
812ccdc3305SLuigi Rizzo }
813ccdc3305SLuigi Rizzo 
814ccdc3305SLuigi Rizzo 
815ccdc3305SLuigi Rizzo 
8168241616dSLuigi Rizzo /* call with NMA_LOCK held */
817ae10d1afSLuigi Rizzo /*
818ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
819ae10d1afSLuigi Rizzo  * If this is the first instance, also allocate the krings, rings etc.
820ae10d1afSLuigi Rizzo  */
821ccdc3305SLuigi Rizzo static void *
822ccdc3305SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
823ccdc3305SLuigi Rizzo {
824ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
825ccdc3305SLuigi Rizzo 	struct netmap_ring *ring;
826ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
827ae10d1afSLuigi Rizzo 	u_int i, len, ndesc, ntx, nrx;
828ccdc3305SLuigi Rizzo 	struct netmap_kring *kring;
829ccdc3305SLuigi Rizzo 
830ae10d1afSLuigi Rizzo 	if (netmap_update_config(na)) {
831ae10d1afSLuigi Rizzo 		/* configuration mismatch, report and fail */
832ae10d1afSLuigi Rizzo 		return NULL;
833ae10d1afSLuigi Rizzo 	}
834ae10d1afSLuigi Rizzo 	ntx = na->num_tx_rings + 1; /* shorthand, include stack ring */
835ae10d1afSLuigi Rizzo 	nrx = na->num_rx_rings + 1; /* shorthand, include stack ring */
836ccdc3305SLuigi Rizzo 	/*
837ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
838ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
839ccdc3305SLuigi Rizzo 	 */
840ccdc3305SLuigi Rizzo 	len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
841ccdc3305SLuigi Rizzo 	nifp = netmap_if_malloc(len);
842ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
843ccdc3305SLuigi Rizzo 		return NULL;
844ccdc3305SLuigi Rizzo 	}
845ccdc3305SLuigi Rizzo 
846ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
847ccdc3305SLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
848ccdc3305SLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
849ccdc3305SLuigi Rizzo 	strncpy(nifp->ni_name, ifname, IFNAMSIZ);
850ccdc3305SLuigi Rizzo 
851ccdc3305SLuigi Rizzo 	(na->refcount)++;	/* XXX atomic ? we are under lock */
852ccdc3305SLuigi Rizzo 	if (na->refcount > 1) { /* already setup, we are done */
853ccdc3305SLuigi Rizzo 		goto final;
854ccdc3305SLuigi Rizzo 	}
855ccdc3305SLuigi Rizzo 
856ae10d1afSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring);
857ae10d1afSLuigi Rizzo 	na->tx_rings = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
858ae10d1afSLuigi Rizzo 	if (na->tx_rings == NULL) {
859ae10d1afSLuigi Rizzo 		D("Cannot allocate krings for %s", ifname);
860ae10d1afSLuigi Rizzo 		goto cleanup;
861ae10d1afSLuigi Rizzo 	}
862ae10d1afSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
863ae10d1afSLuigi Rizzo 
864ccdc3305SLuigi Rizzo 	/*
865ccdc3305SLuigi Rizzo 	 * First instance, allocate netmap rings and buffers for this card
866ccdc3305SLuigi Rizzo 	 * The rings are contiguous, but have variable size.
867ccdc3305SLuigi Rizzo 	 */
868ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
869ccdc3305SLuigi Rizzo 		kring = &na->tx_rings[i];
870ccdc3305SLuigi Rizzo 		ndesc = na->num_tx_desc;
871ccdc3305SLuigi Rizzo 		bzero(kring, sizeof(*kring));
872ccdc3305SLuigi Rizzo 		len = sizeof(struct netmap_ring) +
873ccdc3305SLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
874ccdc3305SLuigi Rizzo 		ring = netmap_ring_malloc(len);
875ccdc3305SLuigi Rizzo 		if (ring == NULL) {
876ccdc3305SLuigi Rizzo 			D("Cannot allocate tx_ring[%d] for %s", i, ifname);
877ccdc3305SLuigi Rizzo 			goto cleanup;
878ccdc3305SLuigi Rizzo 		}
879ccdc3305SLuigi Rizzo 		ND("txring[%d] at %p ofs %d", i, ring);
880ccdc3305SLuigi Rizzo 		kring->na = na;
881ccdc3305SLuigi Rizzo 		kring->ring = ring;
882ccdc3305SLuigi Rizzo 		*(int *)(uintptr_t)&ring->num_slots = kring->nkr_num_slots = ndesc;
883ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
8848241616dSLuigi Rizzo 		    (nm_mem.pools[NETMAP_IF_POOL]._memtotal +
8858241616dSLuigi Rizzo 			nm_mem.pools[NETMAP_RING_POOL]._memtotal) -
886ccdc3305SLuigi Rizzo 			netmap_ring_offset(ring);
887ccdc3305SLuigi Rizzo 
888ccdc3305SLuigi Rizzo 		/*
889ccdc3305SLuigi Rizzo 		 * IMPORTANT:
890ccdc3305SLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
891ccdc3305SLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
892ccdc3305SLuigi Rizzo 		 * the same only if there are no new transmissions).
893ccdc3305SLuigi Rizzo 		 */
894ccdc3305SLuigi Rizzo 		ring->avail = kring->nr_hwavail = ndesc - 1;
895ccdc3305SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
896ccdc3305SLuigi Rizzo 		*(int *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
897ccdc3305SLuigi Rizzo 		ND("initializing slots for txring[%d]", i);
8988241616dSLuigi Rizzo 		if (netmap_new_bufs(nifp, ring->slot, ndesc)) {
8998241616dSLuigi Rizzo 			D("Cannot allocate buffers for tx_ring[%d] for %s", i, ifname);
9008241616dSLuigi Rizzo 			goto cleanup;
9018241616dSLuigi Rizzo 		}
902ccdc3305SLuigi Rizzo 	}
903ccdc3305SLuigi Rizzo 
904ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
905ccdc3305SLuigi Rizzo 		kring = &na->rx_rings[i];
906ccdc3305SLuigi Rizzo 		ndesc = na->num_rx_desc;
907ccdc3305SLuigi Rizzo 		bzero(kring, sizeof(*kring));
908ccdc3305SLuigi Rizzo 		len = sizeof(struct netmap_ring) +
909ccdc3305SLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
910ccdc3305SLuigi Rizzo 		ring = netmap_ring_malloc(len);
911ccdc3305SLuigi Rizzo 		if (ring == NULL) {
912ccdc3305SLuigi Rizzo 			D("Cannot allocate rx_ring[%d] for %s", i, ifname);
913ccdc3305SLuigi Rizzo 			goto cleanup;
914ccdc3305SLuigi Rizzo 		}
915ccdc3305SLuigi Rizzo 		ND("rxring[%d] at %p ofs %d", i, ring);
916ccdc3305SLuigi Rizzo 
917ccdc3305SLuigi Rizzo 		kring->na = na;
918ccdc3305SLuigi Rizzo 		kring->ring = ring;
919ccdc3305SLuigi Rizzo 		*(int *)(uintptr_t)&ring->num_slots = kring->nkr_num_slots = ndesc;
920ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
9218241616dSLuigi Rizzo 		    (nm_mem.pools[NETMAP_IF_POOL]._memtotal +
9228241616dSLuigi Rizzo 		        nm_mem.pools[NETMAP_RING_POOL]._memtotal) -
923ccdc3305SLuigi Rizzo 			netmap_ring_offset(ring);
924ccdc3305SLuigi Rizzo 
925ccdc3305SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
926ccdc3305SLuigi Rizzo 		ring->avail = kring->nr_hwavail = 0; /* empty */
927ccdc3305SLuigi Rizzo 		*(int *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
928ccdc3305SLuigi Rizzo 		ND("initializing slots for rxring[%d]", i);
9298241616dSLuigi Rizzo 		if (netmap_new_bufs(nifp, ring->slot, ndesc)) {
9308241616dSLuigi Rizzo 			D("Cannot allocate buffers for rx_ring[%d] for %s", i, ifname);
9318241616dSLuigi Rizzo 			goto cleanup;
932ccdc3305SLuigi Rizzo 		}
9338241616dSLuigi Rizzo 	}
934ccdc3305SLuigi Rizzo #ifdef linux
935ccdc3305SLuigi Rizzo 	// XXX initialize the selrecord structs.
936ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++)
937ccdc3305SLuigi Rizzo 		init_waitqueue_head(&na->tx_rings[i].si);
938f196ce38SLuigi Rizzo 	for (i = 0; i < nrx; i++)
939f196ce38SLuigi Rizzo 		init_waitqueue_head(&na->rx_rings[i].si);
940ccdc3305SLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
941f196ce38SLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
942ccdc3305SLuigi Rizzo #endif
943ccdc3305SLuigi Rizzo final:
944ccdc3305SLuigi Rizzo 	/*
945ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
946ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
947ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
948ccdc3305SLuigi Rizzo 	 */
949ccdc3305SLuigi Rizzo 	base = netmap_if_offset(nifp);
950ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
951ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
952ccdc3305SLuigi Rizzo 			netmap_ring_offset(na->tx_rings[i].ring) - base;
953ccdc3305SLuigi Rizzo 	}
954ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
955ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
956ccdc3305SLuigi Rizzo 			netmap_ring_offset(na->rx_rings[i].ring) - base;
957ccdc3305SLuigi Rizzo 	}
958ccdc3305SLuigi Rizzo 	return (nifp);
959ccdc3305SLuigi Rizzo cleanup:
9608241616dSLuigi Rizzo 	netmap_free_rings(na);
9618241616dSLuigi Rizzo 	netmap_if_free(nifp);
9628241616dSLuigi Rizzo 	(na->refcount)--;
963ccdc3305SLuigi Rizzo 	return NULL;
964ccdc3305SLuigi Rizzo }
965ccdc3305SLuigi Rizzo 
9668241616dSLuigi Rizzo /* call with NMA_LOCK held */
967ccdc3305SLuigi Rizzo static void
9688241616dSLuigi Rizzo netmap_memory_deref(void)
969ccdc3305SLuigi Rizzo {
9708241616dSLuigi Rizzo 	nm_mem.refcount--;
971ae10d1afSLuigi Rizzo 	if (netmap_verbose)
9728241616dSLuigi Rizzo 		D("refcount = %d", nm_mem.refcount);
973ccdc3305SLuigi Rizzo }
974