xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision ce3ee1e7)
1ccdc3305SLuigi Rizzo /*
22579e2d7SLuigi 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 
268241616dSLuigi Rizzo #ifdef linux
27*ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
288241616dSLuigi Rizzo #endif /* linux */
298241616dSLuigi Rizzo 
30*ce3ee1e7SLuigi Rizzo #ifdef __APPLE__
31*ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
32*ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */
338241616dSLuigi Rizzo 
34*ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
35*ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
36*ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$");
378241616dSLuigi Rizzo 
38*ce3ee1e7SLuigi Rizzo #include <sys/types.h>
39*ce3ee1e7SLuigi Rizzo #include <sys/malloc.h>
40*ce3ee1e7SLuigi Rizzo #include <sys/proc.h>
41*ce3ee1e7SLuigi Rizzo #include <vm/vm.h>	/* vtophys */
42*ce3ee1e7SLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
43*ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44*ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h>
45*ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h>
46*ce3ee1e7SLuigi Rizzo #include <net/if.h>
47*ce3ee1e7SLuigi Rizzo #include <net/if_var.h>
48*ce3ee1e7SLuigi Rizzo #include <net/vnet.h>
49*ce3ee1e7SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
50*ce3ee1e7SLuigi Rizzo 
51*ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
52*ce3ee1e7SLuigi Rizzo 
53*ce3ee1e7SLuigi Rizzo #include <net/netmap.h>
54*ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
55*ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h"
56*ce3ee1e7SLuigi Rizzo 
57*ce3ee1e7SLuigi Rizzo #ifdef linux
58*ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n)	sema_init(&(n)->nm_mtx, 1)
59*ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n)
60*ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n)		down(&(n)->nm_mtx)
61*ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n)		up(&(n)->nm_mtx)
62*ce3ee1e7SLuigi Rizzo #else /* !linux */
63*ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n)	mtx_init(&(n)->nm_mtx, "netmap memory allocator lock", NULL, MTX_DEF)
64*ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n)	mtx_destroy(&(n)->nm_mtx)
65*ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n)		mtx_lock(&(n)->nm_mtx)
66*ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n)		mtx_unlock(&(n)->nm_mtx)
67*ce3ee1e7SLuigi Rizzo #endif /* linux */
688241616dSLuigi Rizzo 
698241616dSLuigi Rizzo 
708241616dSLuigi Rizzo struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = {
718241616dSLuigi Rizzo 	[NETMAP_IF_POOL] = {
728241616dSLuigi Rizzo 		.size = 1024,
738241616dSLuigi Rizzo 		.num  = 100,
748241616dSLuigi Rizzo 	},
758241616dSLuigi Rizzo 	[NETMAP_RING_POOL] = {
768241616dSLuigi Rizzo 		.size = 9*PAGE_SIZE,
778241616dSLuigi Rizzo 		.num  = 200,
788241616dSLuigi Rizzo 	},
798241616dSLuigi Rizzo 	[NETMAP_BUF_POOL] = {
808241616dSLuigi Rizzo 		.size = 2048,
818241616dSLuigi Rizzo 		.num  = NETMAP_BUF_MAX_NUM,
828241616dSLuigi Rizzo 	},
838241616dSLuigi Rizzo };
848241616dSLuigi Rizzo 
85ccdc3305SLuigi Rizzo 
862579e2d7SLuigi Rizzo /*
872579e2d7SLuigi Rizzo  * nm_mem is the memory allocator used for all physical interfaces
882579e2d7SLuigi Rizzo  * running in netmap mode.
892579e2d7SLuigi Rizzo  * Virtual (VALE) ports will have each its own allocator.
902579e2d7SLuigi Rizzo  */
91*ce3ee1e7SLuigi Rizzo static int netmap_mem_global_config(struct netmap_mem_d *nmd);
92*ce3ee1e7SLuigi Rizzo static int netmap_mem_global_finalize(struct netmap_mem_d *nmd);
93*ce3ee1e7SLuigi Rizzo static void netmap_mem_global_deref(struct netmap_mem_d *nmd);
94*ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
958241616dSLuigi Rizzo 	.pools = {
968241616dSLuigi Rizzo 		[NETMAP_IF_POOL] = {
978241616dSLuigi Rizzo 			.name 	= "netmap_if",
988241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
998241616dSLuigi Rizzo 			.objmaxsize = 4096,
1008241616dSLuigi Rizzo 			.nummin     = 10,	/* don't be stingy */
1018241616dSLuigi Rizzo 			.nummax	    = 10000,	/* XXX very large */
1028241616dSLuigi Rizzo 		},
1038241616dSLuigi Rizzo 		[NETMAP_RING_POOL] = {
1048241616dSLuigi Rizzo 			.name 	= "netmap_ring",
1058241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
1068241616dSLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
1078241616dSLuigi Rizzo 			.nummin     = 2,
1088241616dSLuigi Rizzo 			.nummax	    = 1024,
1098241616dSLuigi Rizzo 		},
1108241616dSLuigi Rizzo 		[NETMAP_BUF_POOL] = {
1118241616dSLuigi Rizzo 			.name	= "netmap_buf",
1128241616dSLuigi Rizzo 			.objminsize = 64,
1138241616dSLuigi Rizzo 			.objmaxsize = 65536,
1148241616dSLuigi Rizzo 			.nummin     = 4,
1158241616dSLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
1168241616dSLuigi Rizzo 		},
1178241616dSLuigi Rizzo 	},
118*ce3ee1e7SLuigi Rizzo 	.config   = netmap_mem_global_config,
119*ce3ee1e7SLuigi Rizzo 	.finalize = netmap_mem_global_finalize,
120*ce3ee1e7SLuigi Rizzo 	.deref    = netmap_mem_global_deref,
121ccdc3305SLuigi Rizzo };
122ccdc3305SLuigi Rizzo 
123*ce3ee1e7SLuigi Rizzo 
1242579e2d7SLuigi Rizzo // XXX logically belongs to nm_mem
125ccdc3305SLuigi Rizzo struct lut_entry *netmap_buffer_lut;	/* exported */
126ccdc3305SLuigi Rizzo 
127*ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */
128*ce3ee1e7SLuigi Rizzo static int netmap_mem_private_config(struct netmap_mem_d *nmd);
129*ce3ee1e7SLuigi Rizzo static int netmap_mem_private_finalize(struct netmap_mem_d *nmd);
130*ce3ee1e7SLuigi Rizzo static void netmap_mem_private_deref(struct netmap_mem_d *nmd);
131*ce3ee1e7SLuigi Rizzo const struct netmap_mem_d nm_blueprint = {
132*ce3ee1e7SLuigi Rizzo 	.pools = {
133*ce3ee1e7SLuigi Rizzo 		[NETMAP_IF_POOL] = {
134*ce3ee1e7SLuigi Rizzo 			.name 	= "%s_if",
135*ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
136*ce3ee1e7SLuigi Rizzo 			.objmaxsize = 4096,
137*ce3ee1e7SLuigi Rizzo 			.nummin     = 1,
138*ce3ee1e7SLuigi Rizzo 			.nummax	    = 10,
139*ce3ee1e7SLuigi Rizzo 		},
140*ce3ee1e7SLuigi Rizzo 		[NETMAP_RING_POOL] = {
141*ce3ee1e7SLuigi Rizzo 			.name 	= "%s_ring",
142*ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
143*ce3ee1e7SLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
144*ce3ee1e7SLuigi Rizzo 			.nummin     = 2,
145*ce3ee1e7SLuigi Rizzo 			.nummax	    = 1024,
146*ce3ee1e7SLuigi Rizzo 		},
147*ce3ee1e7SLuigi Rizzo 		[NETMAP_BUF_POOL] = {
148*ce3ee1e7SLuigi Rizzo 			.name	= "%s_buf",
149*ce3ee1e7SLuigi Rizzo 			.objminsize = 64,
150*ce3ee1e7SLuigi Rizzo 			.objmaxsize = 65536,
151*ce3ee1e7SLuigi Rizzo 			.nummin     = 4,
152*ce3ee1e7SLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
153*ce3ee1e7SLuigi Rizzo 		},
154*ce3ee1e7SLuigi Rizzo 	},
155*ce3ee1e7SLuigi Rizzo 	.config   = netmap_mem_private_config,
156*ce3ee1e7SLuigi Rizzo 	.finalize = netmap_mem_private_finalize,
157*ce3ee1e7SLuigi Rizzo 	.deref    = netmap_mem_private_deref,
158*ce3ee1e7SLuigi Rizzo 
159*ce3ee1e7SLuigi Rizzo 	.flags = NETMAP_MEM_PRIVATE,
160*ce3ee1e7SLuigi Rizzo };
161*ce3ee1e7SLuigi Rizzo 
1628241616dSLuigi Rizzo /* memory allocator related sysctls */
1638241616dSLuigi Rizzo 
1648241616dSLuigi Rizzo #define STRINGIFY(x) #x
1658241616dSLuigi Rizzo 
166*ce3ee1e7SLuigi Rizzo 
1678241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \
1688241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
1698241616dSLuigi Rizzo 	    CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
1708241616dSLuigi Rizzo         SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
1718241616dSLuigi Rizzo             CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
1728241616dSLuigi Rizzo         SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
1738241616dSLuigi Rizzo             CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
1748241616dSLuigi Rizzo         SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
1758241616dSLuigi Rizzo             CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s")
1768241616dSLuigi Rizzo 
177*ce3ee1e7SLuigi Rizzo SYSCTL_DECL(_dev_netmap);
1788241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
1798241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
1808241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
181ccdc3305SLuigi Rizzo 
182ccdc3305SLuigi Rizzo /*
1832579e2d7SLuigi Rizzo  * First, find the allocator that contains the requested offset,
1842579e2d7SLuigi Rizzo  * then locate the cluster through a lookup table.
185ccdc3305SLuigi Rizzo  */
186*ce3ee1e7SLuigi Rizzo vm_paddr_t
187*ce3ee1e7SLuigi Rizzo netmap_mem_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
188ccdc3305SLuigi Rizzo {
189ccdc3305SLuigi Rizzo 	int i;
190*ce3ee1e7SLuigi Rizzo 	vm_ooffset_t o = offset;
191*ce3ee1e7SLuigi Rizzo 	vm_paddr_t pa;
192*ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p;
193ccdc3305SLuigi Rizzo 
194*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
195*ce3ee1e7SLuigi Rizzo 	p = nmd->pools;
196*ce3ee1e7SLuigi Rizzo 
197*ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
198*ce3ee1e7SLuigi Rizzo 		if (offset >= p[i].memtotal)
199ccdc3305SLuigi Rizzo 			continue;
2002579e2d7SLuigi Rizzo 		// now lookup the cluster's address
201*ce3ee1e7SLuigi Rizzo 		pa = p[i].lut[offset / p[i]._objsize].paddr +
2028241616dSLuigi Rizzo 			offset % p[i]._objsize;
203*ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(nmd);
204*ce3ee1e7SLuigi Rizzo 		return pa;
205ccdc3305SLuigi Rizzo 	}
2068241616dSLuigi Rizzo 	/* this is only in case of errors */
207b1123b01SLuigi Rizzo 	D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
208*ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal,
209*ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
210*ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal,
211*ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
212*ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal
213*ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_BUF_POOL].memtotal);
214*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
215ccdc3305SLuigi Rizzo 	return 0;	// XXX bad address
216ccdc3305SLuigi Rizzo }
217ccdc3305SLuigi Rizzo 
218*ce3ee1e7SLuigi Rizzo int
219*ce3ee1e7SLuigi Rizzo netmap_mem_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags)
220*ce3ee1e7SLuigi Rizzo {
221*ce3ee1e7SLuigi Rizzo 	int error = 0;
222*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
223*ce3ee1e7SLuigi Rizzo 	error = nmd->config(nmd);
224*ce3ee1e7SLuigi Rizzo 	if (error)
225*ce3ee1e7SLuigi Rizzo 		goto out;
226*ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
227*ce3ee1e7SLuigi Rizzo 		*size = nmd->nm_totalsize;
228*ce3ee1e7SLuigi Rizzo 	} else {
229*ce3ee1e7SLuigi Rizzo 		int i;
230*ce3ee1e7SLuigi Rizzo 		*size = 0;
231*ce3ee1e7SLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
232*ce3ee1e7SLuigi Rizzo 			struct netmap_obj_pool *p = nmd->pools + i;
233*ce3ee1e7SLuigi Rizzo 			*size += (p->_numclusters * p->_clustsize);
234*ce3ee1e7SLuigi Rizzo 		}
235*ce3ee1e7SLuigi Rizzo 	}
236*ce3ee1e7SLuigi Rizzo 	*memflags = nmd->flags;
237*ce3ee1e7SLuigi Rizzo out:
238*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
239*ce3ee1e7SLuigi Rizzo 	return error;
240*ce3ee1e7SLuigi Rizzo }
241*ce3ee1e7SLuigi Rizzo 
242ccdc3305SLuigi Rizzo /*
243ccdc3305SLuigi Rizzo  * we store objects by kernel address, need to find the offset
244ccdc3305SLuigi Rizzo  * within the pool to export the value to userspace.
245ccdc3305SLuigi Rizzo  * Algorithm: scan until we find the cluster, then add the
246ccdc3305SLuigi Rizzo  * actual offset in the cluster
247ccdc3305SLuigi Rizzo  */
248ce2cb792SLuigi Rizzo static ssize_t
249ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
250ccdc3305SLuigi Rizzo {
251*ce3ee1e7SLuigi Rizzo 	int i, k = p->_clustentries, n = p->objtotal;
252ccdc3305SLuigi Rizzo 	ssize_t ofs = 0;
253ccdc3305SLuigi Rizzo 
254ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
255ccdc3305SLuigi Rizzo 		const char *base = p->lut[i].vaddr;
256ccdc3305SLuigi Rizzo 		ssize_t relofs = (const char *) vaddr - base;
257ccdc3305SLuigi Rizzo 
258aa76317cSLuigi Rizzo 		if (relofs < 0 || relofs >= p->_clustsize)
259ccdc3305SLuigi Rizzo 			continue;
260ccdc3305SLuigi Rizzo 
261ccdc3305SLuigi Rizzo 		ofs = ofs + relofs;
262ccdc3305SLuigi Rizzo 		ND("%s: return offset %d (cluster %d) for pointer %p",
263ccdc3305SLuigi Rizzo 		    p->name, ofs, i, vaddr);
264ccdc3305SLuigi Rizzo 		return ofs;
265ccdc3305SLuigi Rizzo 	}
266ccdc3305SLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
267ccdc3305SLuigi Rizzo 	    vaddr, p->name);
268ccdc3305SLuigi Rizzo 	return 0; /* An error occurred */
269ccdc3305SLuigi Rizzo }
270ccdc3305SLuigi Rizzo 
271ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */
272*ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v)					\
273*ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
274ccdc3305SLuigi Rizzo 
275*ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v)				\
276*ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
277*ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
278ccdc3305SLuigi Rizzo 
279*ce3ee1e7SLuigi Rizzo #define netmap_buf_offset(n, v)					\
280*ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal +			\
281*ce3ee1e7SLuigi Rizzo 	(n)->pools[NETMAP_RING_POOL].memtotal +		\
282*ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)))
283ccdc3305SLuigi Rizzo 
284ccdc3305SLuigi Rizzo 
285*ce3ee1e7SLuigi Rizzo ssize_t
286*ce3ee1e7SLuigi Rizzo netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *addr)
287*ce3ee1e7SLuigi Rizzo {
288*ce3ee1e7SLuigi Rizzo 	ssize_t v;
289*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
290*ce3ee1e7SLuigi Rizzo 	v = netmap_if_offset(nmd, addr);
291*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
292*ce3ee1e7SLuigi Rizzo 	return v;
293*ce3ee1e7SLuigi Rizzo }
294*ce3ee1e7SLuigi Rizzo 
2958241616dSLuigi Rizzo /*
2968241616dSLuigi Rizzo  * report the index, and use start position as a hint,
2978241616dSLuigi Rizzo  * otherwise buffer allocation becomes terribly expensive.
2988241616dSLuigi Rizzo  */
299ccdc3305SLuigi Rizzo static void *
300*ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
301ccdc3305SLuigi Rizzo {
302ccdc3305SLuigi Rizzo 	uint32_t i = 0;			/* index in the bitmap */
303ccdc3305SLuigi Rizzo 	uint32_t mask, j;		/* slot counter */
304ccdc3305SLuigi Rizzo 	void *vaddr = NULL;
305ccdc3305SLuigi Rizzo 
306ccdc3305SLuigi Rizzo 	if (len > p->_objsize) {
307ccdc3305SLuigi Rizzo 		D("%s request size %d too large", p->name, len);
308ccdc3305SLuigi Rizzo 		// XXX cannot reduce the size
309ccdc3305SLuigi Rizzo 		return NULL;
310ccdc3305SLuigi Rizzo 	}
311ccdc3305SLuigi Rizzo 
312ccdc3305SLuigi Rizzo 	if (p->objfree == 0) {
313ccdc3305SLuigi Rizzo 		D("%s allocator: run out of memory", p->name);
314ccdc3305SLuigi Rizzo 		return NULL;
315ccdc3305SLuigi Rizzo 	}
3168241616dSLuigi Rizzo 	if (start)
3178241616dSLuigi Rizzo 		i = *start;
318ccdc3305SLuigi Rizzo 
3198241616dSLuigi Rizzo 	/* termination is guaranteed by p->free, but better check bounds on i */
3208241616dSLuigi Rizzo 	while (vaddr == NULL && i < p->bitmap_slots)  {
321ccdc3305SLuigi Rizzo 		uint32_t cur = p->bitmap[i];
322ccdc3305SLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
323ccdc3305SLuigi Rizzo 			i++;
324ccdc3305SLuigi Rizzo 			continue;
325ccdc3305SLuigi Rizzo 		}
326ccdc3305SLuigi Rizzo 		/* locate a slot */
327ccdc3305SLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
328ccdc3305SLuigi Rizzo 			;
329ccdc3305SLuigi Rizzo 
330ccdc3305SLuigi Rizzo 		p->bitmap[i] &= ~mask; /* mark object as in use */
331ccdc3305SLuigi Rizzo 		p->objfree--;
332ccdc3305SLuigi Rizzo 
333ccdc3305SLuigi Rizzo 		vaddr = p->lut[i * 32 + j].vaddr;
3348241616dSLuigi Rizzo 		if (index)
3358241616dSLuigi Rizzo 			*index = i * 32 + j;
336ccdc3305SLuigi Rizzo 	}
337ccdc3305SLuigi Rizzo 	ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr);
338ccdc3305SLuigi Rizzo 
3398241616dSLuigi Rizzo 	if (start)
3408241616dSLuigi Rizzo 		*start = i;
341ccdc3305SLuigi Rizzo 	return vaddr;
342ccdc3305SLuigi Rizzo }
343ccdc3305SLuigi Rizzo 
344ccdc3305SLuigi Rizzo 
345ccdc3305SLuigi Rizzo /*
3462579e2d7SLuigi Rizzo  * free by index, not by address. This is slow, but is only used
3472579e2d7SLuigi Rizzo  * for a small number of objects (rings, nifp)
348ccdc3305SLuigi Rizzo  */
349ccdc3305SLuigi Rizzo static void
350ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
351ccdc3305SLuigi Rizzo {
352ccdc3305SLuigi Rizzo 	if (j >= p->objtotal) {
353ccdc3305SLuigi Rizzo 		D("invalid index %u, max %u", j, p->objtotal);
354ccdc3305SLuigi Rizzo 		return;
355ccdc3305SLuigi Rizzo 	}
356ccdc3305SLuigi Rizzo 	p->bitmap[j / 32] |= (1 << (j % 32));
357ccdc3305SLuigi Rizzo 	p->objfree++;
358ccdc3305SLuigi Rizzo 	return;
359ccdc3305SLuigi Rizzo }
360ccdc3305SLuigi Rizzo 
361ccdc3305SLuigi Rizzo static void
362ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
363ccdc3305SLuigi Rizzo {
364*ce3ee1e7SLuigi Rizzo 	u_int i, j, n = p->numclusters;
365ccdc3305SLuigi Rizzo 
366*ce3ee1e7SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
367*ce3ee1e7SLuigi Rizzo 		void *base = p->lut[i * p->_clustentries].vaddr;
368ccdc3305SLuigi Rizzo 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
369ccdc3305SLuigi Rizzo 
370ccdc3305SLuigi Rizzo 		/* Given address, is out of the scope of the current cluster.*/
371ede69cffSLuigi Rizzo 		if (vaddr < base || relofs >= p->_clustsize)
372ccdc3305SLuigi Rizzo 			continue;
373ccdc3305SLuigi Rizzo 
374ccdc3305SLuigi Rizzo 		j = j + relofs / p->_objsize;
375*ce3ee1e7SLuigi Rizzo 		/* KASSERT(j != 0, ("Cannot free object 0")); */
376ccdc3305SLuigi Rizzo 		netmap_obj_free(p, j);
377ccdc3305SLuigi Rizzo 		return;
378ccdc3305SLuigi Rizzo 	}
379ae10d1afSLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
380ccdc3305SLuigi Rizzo 	    vaddr, p->name);
381ccdc3305SLuigi Rizzo }
382ccdc3305SLuigi Rizzo 
383*ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
384*ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
385*ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
386*ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
387*ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index)			\
388*ce3ee1e7SLuigi Rizzo 	netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], NETMAP_BDG_BUF_SIZE(n), _pos, _index)
389ccdc3305SLuigi Rizzo 
390ccdc3305SLuigi Rizzo 
391ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */
392*ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v)						\
393*ce3ee1e7SLuigi Rizzo     (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
394ccdc3305SLuigi Rizzo 
395ccdc3305SLuigi Rizzo 
3968241616dSLuigi Rizzo /* Return nonzero on error */
3978241616dSLuigi Rizzo static int
398*ce3ee1e7SLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_if *nifp,
399ccdc3305SLuigi Rizzo                 struct netmap_slot *slot, u_int n)
400ccdc3305SLuigi Rizzo {
401*ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
402*ce3ee1e7SLuigi Rizzo 	u_int i = 0;	/* slot counter */
4038241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
4048241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
405ccdc3305SLuigi Rizzo 
4060b8ed8e0SLuigi Rizzo 	(void)nifp;	/* UNUSED */
407ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
408*ce3ee1e7SLuigi Rizzo 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
409ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
410ccdc3305SLuigi Rizzo 			D("unable to locate empty packet buffer");
411ccdc3305SLuigi Rizzo 			goto cleanup;
412ccdc3305SLuigi Rizzo 		}
4138241616dSLuigi Rizzo 		slot[i].buf_idx = index;
414ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
4158241616dSLuigi Rizzo 		/* XXX setting flags=NS_BUF_CHANGED forces a pointer reload
4168241616dSLuigi Rizzo 		 * in the NIC ring. This is a hack that hides missing
4178241616dSLuigi Rizzo 		 * initializations in the drivers, and should go away.
4188241616dSLuigi Rizzo 		 */
4192579e2d7SLuigi Rizzo 		// slot[i].flags = NS_BUF_CHANGED;
420ccdc3305SLuigi Rizzo 	}
421ccdc3305SLuigi Rizzo 
4228241616dSLuigi Rizzo 	ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
4238241616dSLuigi Rizzo 	return (0);
424ccdc3305SLuigi Rizzo 
425ccdc3305SLuigi Rizzo cleanup:
4264cf8455fSEd Maste 	while (i > 0) {
4274cf8455fSEd Maste 		i--;
4288241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
429ccdc3305SLuigi Rizzo 	}
4308241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
4318241616dSLuigi Rizzo 	return (ENOMEM);
432ccdc3305SLuigi Rizzo }
433ccdc3305SLuigi Rizzo 
434ccdc3305SLuigi Rizzo 
435ccdc3305SLuigi Rizzo static void
436*ce3ee1e7SLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, struct netmap_if *nifp, uint32_t i)
437ccdc3305SLuigi Rizzo {
438*ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
4398241616dSLuigi Rizzo 
440*ce3ee1e7SLuigi Rizzo 	(void)nifp;
441ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
442ccdc3305SLuigi Rizzo 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
443ccdc3305SLuigi Rizzo 		return;
444ccdc3305SLuigi Rizzo 	}
4458241616dSLuigi Rizzo 	netmap_obj_free(p, i);
446ccdc3305SLuigi Rizzo }
447ccdc3305SLuigi Rizzo 
4488241616dSLuigi Rizzo static void
4498241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
4508241616dSLuigi Rizzo {
451*ce3ee1e7SLuigi Rizzo 
4528241616dSLuigi Rizzo 	if (p == NULL)
4538241616dSLuigi Rizzo 		return;
4548241616dSLuigi Rizzo 	if (p->bitmap)
4558241616dSLuigi Rizzo 		free(p->bitmap, M_NETMAP);
4568241616dSLuigi Rizzo 	p->bitmap = NULL;
4578241616dSLuigi Rizzo 	if (p->lut) {
458*ce3ee1e7SLuigi Rizzo 		u_int i;
459*ce3ee1e7SLuigi Rizzo 		size_t sz = p->_clustsize;
460*ce3ee1e7SLuigi Rizzo 
461*ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
4628241616dSLuigi Rizzo 			if (p->lut[i].vaddr)
463*ce3ee1e7SLuigi Rizzo 				contigfree(p->lut[i].vaddr, sz, M_NETMAP);
4648241616dSLuigi Rizzo 		}
4658241616dSLuigi Rizzo 		bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
4668241616dSLuigi Rizzo #ifdef linux
4678241616dSLuigi Rizzo 		vfree(p->lut);
4688241616dSLuigi Rizzo #else
4698241616dSLuigi Rizzo 		free(p->lut, M_NETMAP);
4708241616dSLuigi Rizzo #endif
4718241616dSLuigi Rizzo 	}
4728241616dSLuigi Rizzo 	p->lut = NULL;
473*ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
474*ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
475*ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
476*ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
4778241616dSLuigi Rizzo }
478ccdc3305SLuigi Rizzo 
479ccdc3305SLuigi Rizzo /*
480ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
481ccdc3305SLuigi Rizzo  */
482ccdc3305SLuigi Rizzo static void
483ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
484ccdc3305SLuigi Rizzo {
485ccdc3305SLuigi Rizzo 	if (p == NULL)
486ccdc3305SLuigi Rizzo 		return;
4878241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
488ccdc3305SLuigi Rizzo }
489ccdc3305SLuigi Rizzo 
490ccdc3305SLuigi Rizzo /*
491ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
492ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
493ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
494*ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
495ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
496ccdc3305SLuigi Rizzo  *
497ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
498ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
499ccdc3305SLuigi Rizzo  */
5008241616dSLuigi Rizzo 
5018241616dSLuigi Rizzo 
5028241616dSLuigi Rizzo /* call with NMA_LOCK held */
5038241616dSLuigi Rizzo static int
5048241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
505ccdc3305SLuigi Rizzo {
506*ce3ee1e7SLuigi Rizzo 	int i;
507ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
508ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
509ccdc3305SLuigi Rizzo 
510*ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
511*ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
512*ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
513*ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
514*ce3ee1e7SLuigi 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);
520*ce3ee1e7SLuigi Rizzo 		return EINVAL;
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);
531*ce3ee1e7SLuigi Rizzo 		return EINVAL;
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);
536*ce3ee1e7SLuigi Rizzo 		return EINVAL;
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 	 */
570*ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
571ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
572*ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
573*ce3ee1e7SLuigi Rizzo 
574*ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
5758241616dSLuigi Rizzo 	p->_objsize = objsize;
576*ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
577ccdc3305SLuigi Rizzo 
5788241616dSLuigi Rizzo 	return 0;
5798241616dSLuigi Rizzo }
5808241616dSLuigi Rizzo 
5818241616dSLuigi Rizzo 
5828241616dSLuigi Rizzo /* call with NMA_LOCK held */
5838241616dSLuigi Rizzo static int
5848241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
5858241616dSLuigi Rizzo {
586*ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
587*ce3ee1e7SLuigi Rizzo 	size_t n;
588*ce3ee1e7SLuigi Rizzo 
589*ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
590*ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
591*ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
5928241616dSLuigi Rizzo 
5938241616dSLuigi Rizzo 	n = sizeof(struct lut_entry) * p->objtotal;
5948241616dSLuigi Rizzo #ifdef linux
5958241616dSLuigi Rizzo 	p->lut = vmalloc(n);
5968241616dSLuigi Rizzo #else
597d2b91851SEd Maste 	p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
5988241616dSLuigi Rizzo #endif
599ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
600*ce3ee1e7SLuigi Rizzo 		D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
601ccdc3305SLuigi Rizzo 		goto clean;
602ccdc3305SLuigi Rizzo 	}
603ccdc3305SLuigi Rizzo 
604ccdc3305SLuigi Rizzo 	/* Allocate the bitmap */
605ccdc3305SLuigi Rizzo 	n = (p->objtotal + 31) / 32;
606d2b91851SEd Maste 	p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
607ccdc3305SLuigi Rizzo 	if (p->bitmap == NULL) {
608*ce3ee1e7SLuigi Rizzo 		D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
6098241616dSLuigi Rizzo 		    p->name);
610ccdc3305SLuigi Rizzo 		goto clean;
611ccdc3305SLuigi Rizzo 	}
6128241616dSLuigi Rizzo 	p->bitmap_slots = n;
613ccdc3305SLuigi Rizzo 
614ccdc3305SLuigi Rizzo 	/*
615ccdc3305SLuigi Rizzo 	 * Allocate clusters, init pointers and bitmap
616ccdc3305SLuigi Rizzo 	 */
617*ce3ee1e7SLuigi Rizzo 
618*ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
619*ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
620*ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
621ccdc3305SLuigi Rizzo 		char *clust;
622ccdc3305SLuigi Rizzo 
623*ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
624*ce3ee1e7SLuigi Rizzo 		    (size_t)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.
629ccdc3305SLuigi Rizzo 			 */
630ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
6318241616dSLuigi Rizzo 			    i, p->name);
632*ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
633*ce3ee1e7SLuigi Rizzo 				goto out;
634ccdc3305SLuigi Rizzo 			lim = i / 2;
6358241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
636ccdc3305SLuigi Rizzo 				p->bitmap[ (i>>5) ] &=  ~( 1 << (i & 31) );
637*ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
638ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
639*ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
640ccdc3305SLuigi Rizzo 			}
641*ce3ee1e7SLuigi Rizzo 		out:
642ccdc3305SLuigi Rizzo 			p->objtotal = i;
643*ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
644*ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
645ccdc3305SLuigi Rizzo 			break;
646ccdc3305SLuigi Rizzo 		}
6478241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
648ccdc3305SLuigi Rizzo 			p->bitmap[ (i>>5) ] |=  ( 1 << (i & 31) );
649ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
650ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
651ccdc3305SLuigi Rizzo 		}
652ccdc3305SLuigi Rizzo 	}
653*ce3ee1e7SLuigi Rizzo 	p->objfree = p->objtotal;
654*ce3ee1e7SLuigi Rizzo 	p->memtotal = p->numclusters * p->_clustsize;
655*ce3ee1e7SLuigi Rizzo 	if (p->objfree == 0)
656*ce3ee1e7SLuigi Rizzo 		goto clean;
657ae10d1afSLuigi Rizzo 	if (netmap_verbose)
658ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
659*ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
660*ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
661ccdc3305SLuigi Rizzo 
6628241616dSLuigi Rizzo 	return 0;
663ccdc3305SLuigi Rizzo 
664ccdc3305SLuigi Rizzo clean:
6658241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
6668241616dSLuigi Rizzo 	return ENOMEM;
6678241616dSLuigi Rizzo }
6688241616dSLuigi Rizzo 
6698241616dSLuigi Rizzo /* call with lock held */
6708241616dSLuigi Rizzo static int
671*ce3ee1e7SLuigi Rizzo netmap_memory_config_changed(struct netmap_mem_d *nmd)
6728241616dSLuigi Rizzo {
6738241616dSLuigi Rizzo 	int i;
6748241616dSLuigi Rizzo 
6758241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
676*ce3ee1e7SLuigi Rizzo 		if (nmd->pools[i].r_objsize != netmap_params[i].size ||
677*ce3ee1e7SLuigi Rizzo 		    nmd->pools[i].r_objtotal != netmap_params[i].num)
6788241616dSLuigi Rizzo 		    return 1;
6798241616dSLuigi Rizzo 	}
6808241616dSLuigi Rizzo 	return 0;
6818241616dSLuigi Rizzo }
6828241616dSLuigi Rizzo 
683*ce3ee1e7SLuigi Rizzo static void
684*ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
685*ce3ee1e7SLuigi Rizzo {
686*ce3ee1e7SLuigi Rizzo 	int i;
687*ce3ee1e7SLuigi Rizzo 	D("resetting %p", nmd);
688*ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
689*ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
690*ce3ee1e7SLuigi Rizzo 	}
691*ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
692*ce3ee1e7SLuigi Rizzo }
693*ce3ee1e7SLuigi Rizzo 
694*ce3ee1e7SLuigi Rizzo static int
695*ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
696*ce3ee1e7SLuigi Rizzo {
697*ce3ee1e7SLuigi Rizzo 	int i;
698*ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
699*ce3ee1e7SLuigi Rizzo 		return 0;
700*ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
701*ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
702*ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
703*ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
704*ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
705*ce3ee1e7SLuigi Rizzo 			goto error;
706*ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
707*ce3ee1e7SLuigi Rizzo 	}
708*ce3ee1e7SLuigi Rizzo 	/* buffers 0 and 1 are reserved */
709*ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
710*ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
711*ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
712*ce3ee1e7SLuigi Rizzo 
713*ce3ee1e7SLuigi Rizzo 	D("Have %d KB for interfaces, %d KB for rings and %d MB for buffers",
714*ce3ee1e7SLuigi Rizzo 	    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
715*ce3ee1e7SLuigi Rizzo 	    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
716*ce3ee1e7SLuigi Rizzo 	    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
717*ce3ee1e7SLuigi Rizzo 
718*ce3ee1e7SLuigi Rizzo 	D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
719*ce3ee1e7SLuigi Rizzo 
720*ce3ee1e7SLuigi Rizzo 
721*ce3ee1e7SLuigi Rizzo 	return 0;
722*ce3ee1e7SLuigi Rizzo error:
723*ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
724*ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
725*ce3ee1e7SLuigi Rizzo }
726*ce3ee1e7SLuigi Rizzo 
727*ce3ee1e7SLuigi Rizzo 
728*ce3ee1e7SLuigi Rizzo 
729*ce3ee1e7SLuigi Rizzo void
730*ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(struct netmap_mem_d *nmd)
731*ce3ee1e7SLuigi Rizzo {
732*ce3ee1e7SLuigi Rizzo 	if (nmd == NULL)
733*ce3ee1e7SLuigi Rizzo 		return;
734*ce3ee1e7SLuigi Rizzo 	D("deleting %p", nmd);
735*ce3ee1e7SLuigi Rizzo 	if (nmd->refcount > 0)
736*ce3ee1e7SLuigi Rizzo 		D("bug: deleting mem allocator with refcount=%d!", nmd->refcount);
737*ce3ee1e7SLuigi Rizzo 	D("done deleting %p", nmd);
738*ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
739*ce3ee1e7SLuigi Rizzo 	free(nmd, M_DEVBUF);
740*ce3ee1e7SLuigi Rizzo }
741*ce3ee1e7SLuigi Rizzo 
742*ce3ee1e7SLuigi Rizzo static int
743*ce3ee1e7SLuigi Rizzo netmap_mem_private_config(struct netmap_mem_d *nmd)
744*ce3ee1e7SLuigi Rizzo {
745*ce3ee1e7SLuigi Rizzo 	/* nothing to do, we are configured on creation
746*ce3ee1e7SLuigi Rizzo  	 * and configuration never changes thereafter
747*ce3ee1e7SLuigi Rizzo  	 */
748*ce3ee1e7SLuigi Rizzo 	return 0;
749*ce3ee1e7SLuigi Rizzo }
750*ce3ee1e7SLuigi Rizzo 
751*ce3ee1e7SLuigi Rizzo static int
752*ce3ee1e7SLuigi Rizzo netmap_mem_private_finalize(struct netmap_mem_d *nmd)
753*ce3ee1e7SLuigi Rizzo {
754*ce3ee1e7SLuigi Rizzo 	int err;
755*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
756*ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
757*ce3ee1e7SLuigi Rizzo 	err = netmap_mem_finalize_all(nmd);
758*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
759*ce3ee1e7SLuigi Rizzo 	return err;
760*ce3ee1e7SLuigi Rizzo 
761*ce3ee1e7SLuigi Rizzo }
762*ce3ee1e7SLuigi Rizzo 
763*ce3ee1e7SLuigi Rizzo static void netmap_mem_private_deref(struct netmap_mem_d *nmd)
764*ce3ee1e7SLuigi Rizzo {
765*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
766*ce3ee1e7SLuigi Rizzo 	if (--nmd->refcount <= 0)
767*ce3ee1e7SLuigi Rizzo 		netmap_mem_reset_all(nmd);
768*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
769*ce3ee1e7SLuigi Rizzo }
770*ce3ee1e7SLuigi Rizzo 
771*ce3ee1e7SLuigi Rizzo struct netmap_mem_d *
772*ce3ee1e7SLuigi Rizzo netmap_mem_private_new(const char *name, u_int txr, u_int txd, u_int rxr, u_int rxd)
773*ce3ee1e7SLuigi Rizzo {
774*ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
775*ce3ee1e7SLuigi Rizzo 	struct netmap_obj_params p[NETMAP_POOLS_NR];
776*ce3ee1e7SLuigi Rizzo 	int i;
777*ce3ee1e7SLuigi Rizzo 	u_int maxd;
778*ce3ee1e7SLuigi Rizzo 
779*ce3ee1e7SLuigi Rizzo 	d = malloc(sizeof(struct netmap_mem_d),
780*ce3ee1e7SLuigi Rizzo 			M_DEVBUF, M_NOWAIT | M_ZERO);
781*ce3ee1e7SLuigi Rizzo 	if (d == NULL)
782*ce3ee1e7SLuigi Rizzo 		return NULL;
783*ce3ee1e7SLuigi Rizzo 
784*ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
785*ce3ee1e7SLuigi Rizzo 
786*ce3ee1e7SLuigi Rizzo 	/* XXX the rest of the code assumes the stack rings are alwasy present */
787*ce3ee1e7SLuigi Rizzo 	txr++;
788*ce3ee1e7SLuigi Rizzo 	rxr++;
789*ce3ee1e7SLuigi Rizzo 	p[NETMAP_IF_POOL].size = sizeof(struct netmap_if) +
790*ce3ee1e7SLuigi Rizzo 		sizeof(ssize_t) * (txr + rxr);
791*ce3ee1e7SLuigi Rizzo 	p[NETMAP_IF_POOL].num = 2;
792*ce3ee1e7SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
793*ce3ee1e7SLuigi Rizzo 	p[NETMAP_RING_POOL].size = sizeof(struct netmap_ring) +
794*ce3ee1e7SLuigi Rizzo 		sizeof(struct netmap_slot) * maxd;
795*ce3ee1e7SLuigi Rizzo 	p[NETMAP_RING_POOL].num = txr + rxr;
796*ce3ee1e7SLuigi Rizzo 	p[NETMAP_BUF_POOL].size = 2048; /* XXX find a way to let the user choose this */
797*ce3ee1e7SLuigi Rizzo 	p[NETMAP_BUF_POOL].num = rxr * (rxd + 2) + txr * (txd + 2);
798*ce3ee1e7SLuigi Rizzo 
799*ce3ee1e7SLuigi Rizzo 	D("req if %d*%d ring %d*%d buf %d*%d",
800*ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
801*ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
802*ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
803*ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
804*ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
805*ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
806*ce3ee1e7SLuigi Rizzo 
807*ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
808*ce3ee1e7SLuigi Rizzo 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
809*ce3ee1e7SLuigi Rizzo 				nm_blueprint.pools[i].name,
810*ce3ee1e7SLuigi Rizzo 				name);
811*ce3ee1e7SLuigi Rizzo 		if (netmap_config_obj_allocator(&d->pools[i],
812*ce3ee1e7SLuigi Rizzo 				p[i].num, p[i].size))
813*ce3ee1e7SLuigi Rizzo 			goto error;
814*ce3ee1e7SLuigi Rizzo 	}
815*ce3ee1e7SLuigi Rizzo 
816*ce3ee1e7SLuigi Rizzo 	d->flags &= ~NETMAP_MEM_FINALIZED;
817*ce3ee1e7SLuigi Rizzo 
818*ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(d);
819*ce3ee1e7SLuigi Rizzo 
820*ce3ee1e7SLuigi Rizzo 	return d;
821*ce3ee1e7SLuigi Rizzo error:
822*ce3ee1e7SLuigi Rizzo 	netmap_mem_private_delete(d);
823*ce3ee1e7SLuigi Rizzo 	return NULL;
824*ce3ee1e7SLuigi Rizzo }
825*ce3ee1e7SLuigi Rizzo 
8268241616dSLuigi Rizzo 
8278241616dSLuigi Rizzo /* call with lock held */
8288241616dSLuigi Rizzo static int
829*ce3ee1e7SLuigi Rizzo netmap_mem_global_config(struct netmap_mem_d *nmd)
8308241616dSLuigi Rizzo {
8318241616dSLuigi Rizzo 	int i;
8328241616dSLuigi Rizzo 
833*ce3ee1e7SLuigi Rizzo 	if (nmd->refcount)
834*ce3ee1e7SLuigi Rizzo 		/* already in use, we cannot change the configuration */
835*ce3ee1e7SLuigi Rizzo 		goto out;
836*ce3ee1e7SLuigi Rizzo 
837*ce3ee1e7SLuigi Rizzo 	if (!netmap_memory_config_changed(nmd))
8388241616dSLuigi Rizzo 		goto out;
8398241616dSLuigi Rizzo 
8408241616dSLuigi Rizzo 	D("reconfiguring");
8418241616dSLuigi Rizzo 
842*ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
8438241616dSLuigi Rizzo 		/* reset previous allocation */
8448241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
845*ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
8468241616dSLuigi Rizzo 		}
847*ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
8488241616dSLuigi Rizzo         }
8498241616dSLuigi Rizzo 
8508241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
851*ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
8528241616dSLuigi Rizzo 				netmap_params[i].num, netmap_params[i].size);
853*ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
8548241616dSLuigi Rizzo 			goto out;
8558241616dSLuigi Rizzo 	}
8568241616dSLuigi Rizzo 
8578241616dSLuigi Rizzo out:
8588241616dSLuigi Rizzo 
859*ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
8608241616dSLuigi Rizzo }
8618241616dSLuigi Rizzo 
8628241616dSLuigi Rizzo static int
863*ce3ee1e7SLuigi Rizzo netmap_mem_global_finalize(struct netmap_mem_d *nmd)
8648241616dSLuigi Rizzo {
865*ce3ee1e7SLuigi Rizzo 	int err;
8668241616dSLuigi Rizzo 
867*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
868*ce3ee1e7SLuigi Rizzo 
8698241616dSLuigi Rizzo 
8708241616dSLuigi Rizzo 	/* update configuration if changed */
871*ce3ee1e7SLuigi Rizzo 	if (netmap_mem_global_config(nmd))
8728241616dSLuigi Rizzo 		goto out;
8738241616dSLuigi Rizzo 
874*ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
875*ce3ee1e7SLuigi Rizzo 
876*ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
8778241616dSLuigi Rizzo 		/* may happen if config is not changed */
8788241616dSLuigi Rizzo 		ND("nothing to do");
8798241616dSLuigi Rizzo 		goto out;
8808241616dSLuigi Rizzo 	}
8818241616dSLuigi Rizzo 
882*ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
883*ce3ee1e7SLuigi Rizzo 		goto out;
8848241616dSLuigi Rizzo 
8858241616dSLuigi Rizzo 	/* backward compatibility */
886*ce3ee1e7SLuigi Rizzo 	netmap_buf_size = nmd->pools[NETMAP_BUF_POOL]._objsize;
887*ce3ee1e7SLuigi Rizzo 	netmap_total_buffers = nmd->pools[NETMAP_BUF_POOL].objtotal;
8888241616dSLuigi Rizzo 
889*ce3ee1e7SLuigi Rizzo 	netmap_buffer_lut = nmd->pools[NETMAP_BUF_POOL].lut;
890*ce3ee1e7SLuigi Rizzo 	netmap_buffer_base = nmd->pools[NETMAP_BUF_POOL].lut[0].vaddr;
8918241616dSLuigi Rizzo 
892*ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
8938241616dSLuigi Rizzo 
8948241616dSLuigi Rizzo out:
895*ce3ee1e7SLuigi Rizzo 	if (nmd->lasterr)
896*ce3ee1e7SLuigi Rizzo 		nmd->refcount--;
897*ce3ee1e7SLuigi Rizzo 	err = nmd->lasterr;
8988241616dSLuigi Rizzo 
899*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
9008241616dSLuigi Rizzo 
901*ce3ee1e7SLuigi Rizzo 	return err;
9028241616dSLuigi Rizzo 
903ccdc3305SLuigi Rizzo }
904ccdc3305SLuigi Rizzo 
905*ce3ee1e7SLuigi Rizzo int
906*ce3ee1e7SLuigi Rizzo netmap_mem_init(void)
907ccdc3305SLuigi Rizzo {
908*ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
9098241616dSLuigi Rizzo 	return (0);
910ccdc3305SLuigi Rizzo }
911ccdc3305SLuigi Rizzo 
912*ce3ee1e7SLuigi Rizzo void
913*ce3ee1e7SLuigi Rizzo netmap_mem_fini(void)
914ccdc3305SLuigi Rizzo {
9158241616dSLuigi Rizzo 	int i;
9168241616dSLuigi Rizzo 
9178241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
9188241616dSLuigi Rizzo 	    netmap_destroy_obj_allocator(&nm_mem.pools[i]);
9198241616dSLuigi Rizzo 	}
920*ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(&nm_mem);
9218241616dSLuigi Rizzo }
9228241616dSLuigi Rizzo 
9238241616dSLuigi Rizzo static void
9248241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
9258241616dSLuigi Rizzo {
926*ce3ee1e7SLuigi Rizzo 	u_int i;
927ae10d1afSLuigi Rizzo 	if (!na->tx_rings)
928ae10d1afSLuigi Rizzo 		return;
9298241616dSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
930*ce3ee1e7SLuigi Rizzo 		if (na->tx_rings[i].ring) {
931*ce3ee1e7SLuigi Rizzo 			netmap_ring_free(na->nm_mem, na->tx_rings[i].ring);
9328241616dSLuigi Rizzo 			na->tx_rings[i].ring = NULL;
9338241616dSLuigi Rizzo 		}
934*ce3ee1e7SLuigi Rizzo 	}
9358241616dSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
936*ce3ee1e7SLuigi Rizzo 		if (na->rx_rings[i].ring) {
937*ce3ee1e7SLuigi Rizzo 			netmap_ring_free(na->nm_mem, na->rx_rings[i].ring);
9388241616dSLuigi Rizzo 			na->rx_rings[i].ring = NULL;
9398241616dSLuigi Rizzo 		}
940*ce3ee1e7SLuigi Rizzo 	}
941ae10d1afSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
942ae10d1afSLuigi Rizzo 	na->tx_rings = na->rx_rings = NULL;
943ccdc3305SLuigi Rizzo }
944ccdc3305SLuigi Rizzo 
945ccdc3305SLuigi Rizzo 
946ccdc3305SLuigi Rizzo 
9478241616dSLuigi Rizzo /* call with NMA_LOCK held */
948ae10d1afSLuigi Rizzo /*
949ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
950ae10d1afSLuigi Rizzo  * If this is the first instance, also allocate the krings, rings etc.
951*ce3ee1e7SLuigi Rizzo  *
952*ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
953*ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
954*ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
955ae10d1afSLuigi Rizzo  */
956*ce3ee1e7SLuigi Rizzo extern int nma_is_vp(struct netmap_adapter *na);
957*ce3ee1e7SLuigi Rizzo struct netmap_if *
958*ce3ee1e7SLuigi Rizzo netmap_mem_if_new(const char *ifname, struct netmap_adapter *na)
959ccdc3305SLuigi Rizzo {
960ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
961ccdc3305SLuigi Rizzo 	struct netmap_ring *ring;
962ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
963ae10d1afSLuigi Rizzo 	u_int i, len, ndesc, ntx, nrx;
964ccdc3305SLuigi Rizzo 	struct netmap_kring *kring;
965*ce3ee1e7SLuigi Rizzo 	uint32_t *tx_leases = NULL, *rx_leases = NULL;
966ccdc3305SLuigi Rizzo 
967*ce3ee1e7SLuigi Rizzo 	/*
968*ce3ee1e7SLuigi Rizzo 	 * verify whether virtual port need the stack ring
969*ce3ee1e7SLuigi Rizzo 	 */
970ae10d1afSLuigi Rizzo 	ntx = na->num_tx_rings + 1; /* shorthand, include stack ring */
971ae10d1afSLuigi Rizzo 	nrx = na->num_rx_rings + 1; /* shorthand, include stack ring */
972ccdc3305SLuigi Rizzo 	/*
973ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
974ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
975*ce3ee1e7SLuigi Rizzo 	 * For virtual rx rings we also allocate an array of
976*ce3ee1e7SLuigi Rizzo 	 * pointers to assign to nkr_leases.
977ccdc3305SLuigi Rizzo 	 */
978*ce3ee1e7SLuigi Rizzo 
979*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
980*ce3ee1e7SLuigi Rizzo 
981ccdc3305SLuigi Rizzo 	len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
982*ce3ee1e7SLuigi Rizzo 	nifp = netmap_if_malloc(na->nm_mem, len);
983ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
984*ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(na->nm_mem);
985ccdc3305SLuigi Rizzo 		return NULL;
986ccdc3305SLuigi Rizzo 	}
987ccdc3305SLuigi Rizzo 
988ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
989*ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
990*ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
991*ce3ee1e7SLuigi Rizzo 	strncpy(nifp->ni_name, ifname, (size_t)IFNAMSIZ);
992ccdc3305SLuigi Rizzo 
993*ce3ee1e7SLuigi Rizzo 	if (na->refcount) { /* already setup, we are done */
994ccdc3305SLuigi Rizzo 		goto final;
995ccdc3305SLuigi Rizzo 	}
996ccdc3305SLuigi Rizzo 
997ae10d1afSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring);
998*ce3ee1e7SLuigi Rizzo 	/*
999*ce3ee1e7SLuigi Rizzo 	 * Leases are attached to TX rings on NIC/host ports,
1000*ce3ee1e7SLuigi Rizzo 	 * and to RX rings on VALE ports.
1001*ce3ee1e7SLuigi Rizzo 	 */
1002*ce3ee1e7SLuigi Rizzo 	if (nma_is_vp(na)) {
1003*ce3ee1e7SLuigi Rizzo 		len += sizeof(uint32_t) * na->num_rx_desc * na->num_rx_rings;
1004*ce3ee1e7SLuigi Rizzo 	} else {
1005*ce3ee1e7SLuigi Rizzo 		len += sizeof(uint32_t) * na->num_tx_desc * ntx;
1006*ce3ee1e7SLuigi Rizzo 	}
1007*ce3ee1e7SLuigi Rizzo 
1008*ce3ee1e7SLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
1009ae10d1afSLuigi Rizzo 	if (na->tx_rings == NULL) {
1010ae10d1afSLuigi Rizzo 		D("Cannot allocate krings for %s", ifname);
1011ae10d1afSLuigi Rizzo 		goto cleanup;
1012ae10d1afSLuigi Rizzo 	}
1013ae10d1afSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
1014ae10d1afSLuigi Rizzo 
1015*ce3ee1e7SLuigi Rizzo 	if (nma_is_vp(na)) {
1016*ce3ee1e7SLuigi Rizzo 		rx_leases = (uint32_t *)(na->rx_rings + nrx);
1017*ce3ee1e7SLuigi Rizzo 	} else {
1018*ce3ee1e7SLuigi Rizzo 		tx_leases = (uint32_t *)(na->rx_rings + nrx);
1019*ce3ee1e7SLuigi Rizzo 	}
1020*ce3ee1e7SLuigi Rizzo 
1021ccdc3305SLuigi Rizzo 	/*
1022ccdc3305SLuigi Rizzo 	 * First instance, allocate netmap rings and buffers for this card
1023ccdc3305SLuigi Rizzo 	 * The rings are contiguous, but have variable size.
1024ccdc3305SLuigi Rizzo 	 */
1025ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
1026ccdc3305SLuigi Rizzo 		kring = &na->tx_rings[i];
1027ccdc3305SLuigi Rizzo 		ndesc = na->num_tx_desc;
1028ccdc3305SLuigi Rizzo 		bzero(kring, sizeof(*kring));
1029ccdc3305SLuigi Rizzo 		len = sizeof(struct netmap_ring) +
1030ccdc3305SLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
1031*ce3ee1e7SLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
1032ccdc3305SLuigi Rizzo 		if (ring == NULL) {
1033ccdc3305SLuigi Rizzo 			D("Cannot allocate tx_ring[%d] for %s", i, ifname);
1034ccdc3305SLuigi Rizzo 			goto cleanup;
1035ccdc3305SLuigi Rizzo 		}
1036ccdc3305SLuigi Rizzo 		ND("txring[%d] at %p ofs %d", i, ring);
1037ccdc3305SLuigi Rizzo 		kring->na = na;
1038ccdc3305SLuigi Rizzo 		kring->ring = ring;
1039*ce3ee1e7SLuigi Rizzo 		if (tx_leases) {
1040*ce3ee1e7SLuigi Rizzo 			kring->nkr_leases = tx_leases;
1041*ce3ee1e7SLuigi Rizzo 			tx_leases += ndesc;
1042*ce3ee1e7SLuigi Rizzo 		}
1043*ce3ee1e7SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = kring->nkr_num_slots = ndesc;
1044ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
1045*ce3ee1e7SLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1046*ce3ee1e7SLuigi Rizzo 			na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1047*ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
1048ccdc3305SLuigi Rizzo 
1049ccdc3305SLuigi Rizzo 		/*
1050ccdc3305SLuigi Rizzo 		 * IMPORTANT:
1051ccdc3305SLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
1052ccdc3305SLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
1053ccdc3305SLuigi Rizzo 		 * the same only if there are no new transmissions).
1054ccdc3305SLuigi Rizzo 		 */
1055ccdc3305SLuigi Rizzo 		ring->avail = kring->nr_hwavail = ndesc - 1;
1056ccdc3305SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
1057*ce3ee1e7SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size =
1058*ce3ee1e7SLuigi Rizzo 			NETMAP_BDG_BUF_SIZE(na->nm_mem);
1059ccdc3305SLuigi Rizzo 		ND("initializing slots for txring[%d]", i);
1060*ce3ee1e7SLuigi Rizzo 		if (netmap_new_bufs(na->nm_mem, nifp, ring->slot, ndesc)) {
10618241616dSLuigi Rizzo 			D("Cannot allocate buffers for tx_ring[%d] for %s", i, ifname);
10628241616dSLuigi Rizzo 			goto cleanup;
10638241616dSLuigi Rizzo 		}
1064ccdc3305SLuigi Rizzo 	}
1065ccdc3305SLuigi Rizzo 
1066ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
1067ccdc3305SLuigi Rizzo 		kring = &na->rx_rings[i];
1068ccdc3305SLuigi Rizzo 		ndesc = na->num_rx_desc;
1069ccdc3305SLuigi Rizzo 		bzero(kring, sizeof(*kring));
1070ccdc3305SLuigi Rizzo 		len = sizeof(struct netmap_ring) +
1071ccdc3305SLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
1072*ce3ee1e7SLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
1073ccdc3305SLuigi Rizzo 		if (ring == NULL) {
1074ccdc3305SLuigi Rizzo 			D("Cannot allocate rx_ring[%d] for %s", i, ifname);
1075ccdc3305SLuigi Rizzo 			goto cleanup;
1076ccdc3305SLuigi Rizzo 		}
1077ccdc3305SLuigi Rizzo 		ND("rxring[%d] at %p ofs %d", i, ring);
1078ccdc3305SLuigi Rizzo 
1079ccdc3305SLuigi Rizzo 		kring->na = na;
1080ccdc3305SLuigi Rizzo 		kring->ring = ring;
1081*ce3ee1e7SLuigi Rizzo 		if (rx_leases && i < na->num_rx_rings) {
1082*ce3ee1e7SLuigi Rizzo 			kring->nkr_leases = rx_leases;
1083*ce3ee1e7SLuigi Rizzo 			rx_leases += ndesc;
1084*ce3ee1e7SLuigi Rizzo 		}
1085*ce3ee1e7SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = kring->nkr_num_slots = ndesc;
1086ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
1087*ce3ee1e7SLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1088*ce3ee1e7SLuigi Rizzo 		        na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1089*ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
1090ccdc3305SLuigi Rizzo 
1091ccdc3305SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
1092ccdc3305SLuigi Rizzo 		ring->avail = kring->nr_hwavail = 0; /* empty */
1093*ce3ee1e7SLuigi Rizzo 		*(int *)(uintptr_t)&ring->nr_buf_size =
1094*ce3ee1e7SLuigi Rizzo 			NETMAP_BDG_BUF_SIZE(na->nm_mem);
1095ccdc3305SLuigi Rizzo 		ND("initializing slots for rxring[%d]", i);
1096*ce3ee1e7SLuigi Rizzo 		if (netmap_new_bufs(na->nm_mem, nifp, ring->slot, ndesc)) {
10978241616dSLuigi Rizzo 			D("Cannot allocate buffers for rx_ring[%d] for %s", i, ifname);
10988241616dSLuigi Rizzo 			goto cleanup;
1099ccdc3305SLuigi Rizzo 		}
11008241616dSLuigi Rizzo 	}
1101ccdc3305SLuigi Rizzo #ifdef linux
1102ccdc3305SLuigi Rizzo 	// XXX initialize the selrecord structs.
1103ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++)
1104ccdc3305SLuigi Rizzo 		init_waitqueue_head(&na->tx_rings[i].si);
1105f196ce38SLuigi Rizzo 	for (i = 0; i < nrx; i++)
1106f196ce38SLuigi Rizzo 		init_waitqueue_head(&na->rx_rings[i].si);
1107ccdc3305SLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
1108f196ce38SLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
1109ccdc3305SLuigi Rizzo #endif
1110ccdc3305SLuigi Rizzo final:
1111ccdc3305SLuigi Rizzo 	/*
1112ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
1113ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
1114ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
1115ccdc3305SLuigi Rizzo 	 */
1116*ce3ee1e7SLuigi Rizzo 	base = netmap_if_offset(na->nm_mem, nifp);
1117ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
1118ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1119*ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1120ccdc3305SLuigi Rizzo 	}
1121ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
1122ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
1123*ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1124ccdc3305SLuigi Rizzo 	}
1125*ce3ee1e7SLuigi Rizzo 
1126*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1127*ce3ee1e7SLuigi Rizzo 
1128ccdc3305SLuigi Rizzo 	return (nifp);
1129ccdc3305SLuigi Rizzo cleanup:
11308241616dSLuigi Rizzo 	netmap_free_rings(na);
1131*ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
1132*ce3ee1e7SLuigi Rizzo 
1133*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1134*ce3ee1e7SLuigi Rizzo 
1135ccdc3305SLuigi Rizzo 	return NULL;
1136ccdc3305SLuigi Rizzo }
1137ccdc3305SLuigi Rizzo 
1138*ce3ee1e7SLuigi Rizzo void
1139*ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1140ccdc3305SLuigi Rizzo {
1141*ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
1142*ce3ee1e7SLuigi Rizzo 		/* nothing to do */
1143*ce3ee1e7SLuigi Rizzo 		return;
1144*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1145*ce3ee1e7SLuigi Rizzo 
1146*ce3ee1e7SLuigi Rizzo 	if (na->refcount <= 0) {
1147*ce3ee1e7SLuigi Rizzo 		/* last instance, release bufs and rings */
1148*ce3ee1e7SLuigi Rizzo 		u_int i, j, lim;
1149*ce3ee1e7SLuigi Rizzo 		struct netmap_ring *ring;
1150*ce3ee1e7SLuigi Rizzo 
1151*ce3ee1e7SLuigi Rizzo 		for (i = 0; i < na->num_tx_rings + 1; i++) {
1152*ce3ee1e7SLuigi Rizzo 			ring = na->tx_rings[i].ring;
1153*ce3ee1e7SLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
1154*ce3ee1e7SLuigi Rizzo 			for (j = 0; j < lim; j++)
1155*ce3ee1e7SLuigi Rizzo 				netmap_free_buf(na->nm_mem, nifp, ring->slot[j].buf_idx);
1156*ce3ee1e7SLuigi Rizzo 		}
1157*ce3ee1e7SLuigi Rizzo 		for (i = 0; i < na->num_rx_rings + 1; i++) {
1158*ce3ee1e7SLuigi Rizzo 			ring = na->rx_rings[i].ring;
1159*ce3ee1e7SLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
1160*ce3ee1e7SLuigi Rizzo 			for (j = 0; j < lim; j++)
1161*ce3ee1e7SLuigi Rizzo 				netmap_free_buf(na->nm_mem, nifp, ring->slot[j].buf_idx);
1162*ce3ee1e7SLuigi Rizzo 		}
1163*ce3ee1e7SLuigi Rizzo 		netmap_free_rings(na);
1164*ce3ee1e7SLuigi Rizzo 	}
1165*ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
1166*ce3ee1e7SLuigi Rizzo 
1167*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1168*ce3ee1e7SLuigi Rizzo }
1169*ce3ee1e7SLuigi Rizzo 
1170*ce3ee1e7SLuigi Rizzo static void
1171*ce3ee1e7SLuigi Rizzo netmap_mem_global_deref(struct netmap_mem_d *nmd)
1172*ce3ee1e7SLuigi Rizzo {
1173*ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
1174*ce3ee1e7SLuigi Rizzo 
1175*ce3ee1e7SLuigi Rizzo 	nmd->refcount--;
1176ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1177*ce3ee1e7SLuigi Rizzo 		D("refcount = %d", nmd->refcount);
1178*ce3ee1e7SLuigi Rizzo 
1179*ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
1180*ce3ee1e7SLuigi Rizzo }
1181*ce3ee1e7SLuigi Rizzo 
1182*ce3ee1e7SLuigi Rizzo int netmap_mem_finalize(struct netmap_mem_d *nmd)
1183*ce3ee1e7SLuigi Rizzo {
1184*ce3ee1e7SLuigi Rizzo 	return nmd->finalize(nmd);
1185*ce3ee1e7SLuigi Rizzo }
1186*ce3ee1e7SLuigi Rizzo 
1187*ce3ee1e7SLuigi Rizzo void netmap_mem_deref(struct netmap_mem_d *nmd)
1188*ce3ee1e7SLuigi Rizzo {
1189*ce3ee1e7SLuigi Rizzo 	return nmd->deref(nmd);
1190ccdc3305SLuigi Rizzo }
1191