xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision f6c2a31f)
1ccdc3305SLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2012-2014 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
27ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
288241616dSLuigi Rizzo #endif /* linux */
298241616dSLuigi Rizzo 
30ce3ee1e7SLuigi Rizzo #ifdef __APPLE__
31ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
32ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */
338241616dSLuigi Rizzo 
34ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
35ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
36ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$");
378241616dSLuigi Rizzo 
38ce3ee1e7SLuigi Rizzo #include <sys/types.h>
39ce3ee1e7SLuigi Rizzo #include <sys/malloc.h>
40ce3ee1e7SLuigi Rizzo #include <sys/proc.h>
41ce3ee1e7SLuigi Rizzo #include <vm/vm.h>	/* vtophys */
42ce3ee1e7SLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
43ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h>
45ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h>
46ce3ee1e7SLuigi Rizzo #include <net/if.h>
47ce3ee1e7SLuigi Rizzo #include <net/if_var.h>
48ce3ee1e7SLuigi Rizzo #include <net/vnet.h>
49ce3ee1e7SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
50ce3ee1e7SLuigi Rizzo 
51ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
52ce3ee1e7SLuigi Rizzo 
53ce3ee1e7SLuigi Rizzo #include <net/netmap.h>
54ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
55ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h"
56ce3ee1e7SLuigi Rizzo 
57ce3ee1e7SLuigi Rizzo #ifdef linux
58ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n)	sema_init(&(n)->nm_mtx, 1)
59ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n)
60ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n)		down(&(n)->nm_mtx)
61ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n)		up(&(n)->nm_mtx)
62ce3ee1e7SLuigi Rizzo #else /* !linux */
63ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n)	mtx_init(&(n)->nm_mtx, "netmap memory allocator lock", NULL, MTX_DEF)
64ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n)	mtx_destroy(&(n)->nm_mtx)
65ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n)		mtx_lock(&(n)->nm_mtx)
66ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n)		mtx_unlock(&(n)->nm_mtx)
67ce3ee1e7SLuigi 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  */
91ce3ee1e7SLuigi Rizzo static int netmap_mem_global_config(struct netmap_mem_d *nmd);
92ce3ee1e7SLuigi Rizzo static int netmap_mem_global_finalize(struct netmap_mem_d *nmd);
93ce3ee1e7SLuigi Rizzo static void netmap_mem_global_deref(struct netmap_mem_d *nmd);
94ce3ee1e7SLuigi 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 	},
118ce3ee1e7SLuigi Rizzo 	.config   = netmap_mem_global_config,
119ce3ee1e7SLuigi Rizzo 	.finalize = netmap_mem_global_finalize,
120ce3ee1e7SLuigi Rizzo 	.deref    = netmap_mem_global_deref,
121ccdc3305SLuigi Rizzo };
122ccdc3305SLuigi Rizzo 
123ce3ee1e7SLuigi Rizzo 
1242579e2d7SLuigi Rizzo // XXX logically belongs to nm_mem
125ccdc3305SLuigi Rizzo struct lut_entry *netmap_buffer_lut;	/* exported */
126ccdc3305SLuigi Rizzo 
127ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */
128ce3ee1e7SLuigi Rizzo static int netmap_mem_private_config(struct netmap_mem_d *nmd);
129ce3ee1e7SLuigi Rizzo static int netmap_mem_private_finalize(struct netmap_mem_d *nmd);
130ce3ee1e7SLuigi Rizzo static void netmap_mem_private_deref(struct netmap_mem_d *nmd);
131ce3ee1e7SLuigi Rizzo const struct netmap_mem_d nm_blueprint = {
132ce3ee1e7SLuigi Rizzo 	.pools = {
133ce3ee1e7SLuigi Rizzo 		[NETMAP_IF_POOL] = {
134ce3ee1e7SLuigi Rizzo 			.name 	= "%s_if",
135ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
136ce3ee1e7SLuigi Rizzo 			.objmaxsize = 4096,
137ce3ee1e7SLuigi Rizzo 			.nummin     = 1,
138ce3ee1e7SLuigi Rizzo 			.nummax	    = 10,
139ce3ee1e7SLuigi Rizzo 		},
140ce3ee1e7SLuigi Rizzo 		[NETMAP_RING_POOL] = {
141ce3ee1e7SLuigi Rizzo 			.name 	= "%s_ring",
142ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
143ce3ee1e7SLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
144ce3ee1e7SLuigi Rizzo 			.nummin     = 2,
145ce3ee1e7SLuigi Rizzo 			.nummax	    = 1024,
146ce3ee1e7SLuigi Rizzo 		},
147ce3ee1e7SLuigi Rizzo 		[NETMAP_BUF_POOL] = {
148ce3ee1e7SLuigi Rizzo 			.name	= "%s_buf",
149ce3ee1e7SLuigi Rizzo 			.objminsize = 64,
150ce3ee1e7SLuigi Rizzo 			.objmaxsize = 65536,
151ce3ee1e7SLuigi Rizzo 			.nummin     = 4,
152ce3ee1e7SLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
153ce3ee1e7SLuigi Rizzo 		},
154ce3ee1e7SLuigi Rizzo 	},
155ce3ee1e7SLuigi Rizzo 	.config   = netmap_mem_private_config,
156ce3ee1e7SLuigi Rizzo 	.finalize = netmap_mem_private_finalize,
157ce3ee1e7SLuigi Rizzo 	.deref    = netmap_mem_private_deref,
158ce3ee1e7SLuigi Rizzo 
159ce3ee1e7SLuigi Rizzo 	.flags = NETMAP_MEM_PRIVATE,
160ce3ee1e7SLuigi Rizzo };
161ce3ee1e7SLuigi Rizzo 
1628241616dSLuigi Rizzo /* memory allocator related sysctls */
1638241616dSLuigi Rizzo 
1648241616dSLuigi Rizzo #define STRINGIFY(x) #x
1658241616dSLuigi Rizzo 
166ce3ee1e7SLuigi 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 
177ce3ee1e7SLuigi 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  */
186ce3ee1e7SLuigi Rizzo vm_paddr_t
187ce3ee1e7SLuigi Rizzo netmap_mem_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
188ccdc3305SLuigi Rizzo {
189ccdc3305SLuigi Rizzo 	int i;
190ce3ee1e7SLuigi Rizzo 	vm_ooffset_t o = offset;
191ce3ee1e7SLuigi Rizzo 	vm_paddr_t pa;
192ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p;
193ccdc3305SLuigi Rizzo 
194ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
195ce3ee1e7SLuigi Rizzo 	p = nmd->pools;
196ce3ee1e7SLuigi Rizzo 
197ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
198ce3ee1e7SLuigi Rizzo 		if (offset >= p[i].memtotal)
199ccdc3305SLuigi Rizzo 			continue;
2002579e2d7SLuigi Rizzo 		// now lookup the cluster's address
201ce3ee1e7SLuigi Rizzo 		pa = p[i].lut[offset / p[i]._objsize].paddr +
2028241616dSLuigi Rizzo 			offset % p[i]._objsize;
203ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(nmd);
204ce3ee1e7SLuigi 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,
208ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal,
209ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
210ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal,
211ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
212ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal
213ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_BUF_POOL].memtotal);
214ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
215ccdc3305SLuigi Rizzo 	return 0;	// XXX bad address
216ccdc3305SLuigi Rizzo }
217ccdc3305SLuigi Rizzo 
218ce3ee1e7SLuigi Rizzo int
219ce3ee1e7SLuigi Rizzo netmap_mem_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags)
220ce3ee1e7SLuigi Rizzo {
221ce3ee1e7SLuigi Rizzo 	int error = 0;
222ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
223ce3ee1e7SLuigi Rizzo 	error = nmd->config(nmd);
224ce3ee1e7SLuigi Rizzo 	if (error)
225ce3ee1e7SLuigi Rizzo 		goto out;
226ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
227ce3ee1e7SLuigi Rizzo 		*size = nmd->nm_totalsize;
228ce3ee1e7SLuigi Rizzo 	} else {
229ce3ee1e7SLuigi Rizzo 		int i;
230ce3ee1e7SLuigi Rizzo 		*size = 0;
231ce3ee1e7SLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
232ce3ee1e7SLuigi Rizzo 			struct netmap_obj_pool *p = nmd->pools + i;
233ce3ee1e7SLuigi Rizzo 			*size += (p->_numclusters * p->_clustsize);
234ce3ee1e7SLuigi Rizzo 		}
235ce3ee1e7SLuigi Rizzo 	}
236ce3ee1e7SLuigi Rizzo 	*memflags = nmd->flags;
237ce3ee1e7SLuigi Rizzo out:
238ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
239ce3ee1e7SLuigi Rizzo 	return error;
240ce3ee1e7SLuigi Rizzo }
241ce3ee1e7SLuigi 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 {
251ce3ee1e7SLuigi 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 */
272ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v)					\
273ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
274ccdc3305SLuigi Rizzo 
275ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v)				\
276ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
277ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
278ccdc3305SLuigi Rizzo 
279ce3ee1e7SLuigi Rizzo #define netmap_buf_offset(n, v)					\
280ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal +			\
281ce3ee1e7SLuigi Rizzo 	(n)->pools[NETMAP_RING_POOL].memtotal +		\
282ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)))
283ccdc3305SLuigi Rizzo 
284ccdc3305SLuigi Rizzo 
285ce3ee1e7SLuigi Rizzo ssize_t
286ce3ee1e7SLuigi Rizzo netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *addr)
287ce3ee1e7SLuigi Rizzo {
288ce3ee1e7SLuigi Rizzo 	ssize_t v;
289ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
290ce3ee1e7SLuigi Rizzo 	v = netmap_if_offset(nmd, addr);
291ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
292ce3ee1e7SLuigi Rizzo 	return v;
293ce3ee1e7SLuigi Rizzo }
294ce3ee1e7SLuigi 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 *
300ce3ee1e7SLuigi 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) {
313f9790aebSLuigi Rizzo 		D("no more %s objects", 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 {
364ce3ee1e7SLuigi Rizzo 	u_int i, j, n = p->numclusters;
365ccdc3305SLuigi Rizzo 
366ce3ee1e7SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
367ce3ee1e7SLuigi 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;
375ce3ee1e7SLuigi 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 
383ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
384ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
385ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
386ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
387ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index)			\
388ce3ee1e7SLuigi 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 */
392ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v)						\
393ce3ee1e7SLuigi 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
398f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
399ccdc3305SLuigi Rizzo {
400ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
401ce3ee1e7SLuigi Rizzo 	u_int i = 0;	/* slot counter */
4028241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
4038241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
404ccdc3305SLuigi Rizzo 
405ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
406ce3ee1e7SLuigi Rizzo 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
407ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
408f9790aebSLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
409ccdc3305SLuigi Rizzo 			goto cleanup;
410ccdc3305SLuigi Rizzo 		}
4118241616dSLuigi Rizzo 		slot[i].buf_idx = index;
412ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
413f9790aebSLuigi Rizzo 		slot[i].flags = 0;
414ccdc3305SLuigi Rizzo 	}
415ccdc3305SLuigi Rizzo 
4168241616dSLuigi Rizzo 	ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
4178241616dSLuigi Rizzo 	return (0);
418ccdc3305SLuigi Rizzo 
419ccdc3305SLuigi Rizzo cleanup:
4204cf8455fSEd Maste 	while (i > 0) {
4214cf8455fSEd Maste 		i--;
4228241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
423ccdc3305SLuigi Rizzo 	}
4248241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
4258241616dSLuigi Rizzo 	return (ENOMEM);
426ccdc3305SLuigi Rizzo }
427ccdc3305SLuigi Rizzo 
428ccdc3305SLuigi Rizzo 
429ccdc3305SLuigi Rizzo static void
430f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
431ccdc3305SLuigi Rizzo {
432ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
4338241616dSLuigi Rizzo 
434ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
435ccdc3305SLuigi Rizzo 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
436ccdc3305SLuigi Rizzo 		return;
437ccdc3305SLuigi Rizzo 	}
4388241616dSLuigi Rizzo 	netmap_obj_free(p, i);
439ccdc3305SLuigi Rizzo }
440ccdc3305SLuigi Rizzo 
4418241616dSLuigi Rizzo static void
4428241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
4438241616dSLuigi Rizzo {
444ce3ee1e7SLuigi Rizzo 
4458241616dSLuigi Rizzo 	if (p == NULL)
4468241616dSLuigi Rizzo 		return;
4478241616dSLuigi Rizzo 	if (p->bitmap)
4488241616dSLuigi Rizzo 		free(p->bitmap, M_NETMAP);
4498241616dSLuigi Rizzo 	p->bitmap = NULL;
4508241616dSLuigi Rizzo 	if (p->lut) {
451ce3ee1e7SLuigi Rizzo 		u_int i;
452ce3ee1e7SLuigi Rizzo 		size_t sz = p->_clustsize;
453ce3ee1e7SLuigi Rizzo 
454ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
4558241616dSLuigi Rizzo 			if (p->lut[i].vaddr)
456ce3ee1e7SLuigi Rizzo 				contigfree(p->lut[i].vaddr, sz, M_NETMAP);
4578241616dSLuigi Rizzo 		}
4588241616dSLuigi Rizzo 		bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
4598241616dSLuigi Rizzo #ifdef linux
4608241616dSLuigi Rizzo 		vfree(p->lut);
4618241616dSLuigi Rizzo #else
4628241616dSLuigi Rizzo 		free(p->lut, M_NETMAP);
4638241616dSLuigi Rizzo #endif
4648241616dSLuigi Rizzo 	}
4658241616dSLuigi Rizzo 	p->lut = NULL;
466ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
467ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
468ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
469ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
4708241616dSLuigi Rizzo }
471ccdc3305SLuigi Rizzo 
472ccdc3305SLuigi Rizzo /*
473ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
474ccdc3305SLuigi Rizzo  */
475ccdc3305SLuigi Rizzo static void
476ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
477ccdc3305SLuigi Rizzo {
478ccdc3305SLuigi Rizzo 	if (p == NULL)
479ccdc3305SLuigi Rizzo 		return;
4808241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
481ccdc3305SLuigi Rizzo }
482ccdc3305SLuigi Rizzo 
483ccdc3305SLuigi Rizzo /*
484ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
485ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
486ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
487ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
488ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
489ccdc3305SLuigi Rizzo  *
490ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
491ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
492ccdc3305SLuigi Rizzo  */
4938241616dSLuigi Rizzo 
4948241616dSLuigi Rizzo 
4958241616dSLuigi Rizzo /* call with NMA_LOCK held */
4968241616dSLuigi Rizzo static int
4978241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
498ccdc3305SLuigi Rizzo {
499ce3ee1e7SLuigi Rizzo 	int i;
500ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
501ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
502ccdc3305SLuigi Rizzo 
503ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
504ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
505ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
506ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
507ce3ee1e7SLuigi Rizzo 
508ccdc3305SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<17)
50917885a7bSLuigi Rizzo #define LINE_ROUND	NM_CACHE_ALIGN	// 64
510ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
511ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
512ccdc3305SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
513ce3ee1e7SLuigi Rizzo 		return EINVAL;
514ccdc3305SLuigi Rizzo 	}
515ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
516ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
517ccdc3305SLuigi Rizzo 	if (i) {
518ccdc3305SLuigi Rizzo 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
519ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
520ccdc3305SLuigi Rizzo 	}
5218241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
5228241616dSLuigi Rizzo 		D("requested objsize %d out of range [%d, %d]",
5238241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
524ce3ee1e7SLuigi Rizzo 		return EINVAL;
5258241616dSLuigi Rizzo 	}
5268241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
5278241616dSLuigi Rizzo 		D("requested objtotal %d out of range [%d, %d]",
5288241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
529ce3ee1e7SLuigi Rizzo 		return EINVAL;
5308241616dSLuigi Rizzo 	}
531ccdc3305SLuigi Rizzo 	/*
532ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
533ccdc3305SLuigi Rizzo 	 * given a max cluster size,
534ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
535ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
536ccdc3305SLuigi Rizzo 	 */
537ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
538ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
539ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
540ccdc3305SLuigi Rizzo 			break;
541ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
542ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
543ccdc3305SLuigi Rizzo 			clustentries = i;
544ccdc3305SLuigi Rizzo 			break;
545ccdc3305SLuigi Rizzo 		}
546ccdc3305SLuigi Rizzo 		if (delta > ( (clustentries*objsize) % PAGE_SIZE) )
547ccdc3305SLuigi Rizzo 			clustentries = i;
548ccdc3305SLuigi Rizzo 	}
549ccdc3305SLuigi Rizzo 	// D("XXX --- ouch, delta %d (bad for buffers)", delta);
550ccdc3305SLuigi Rizzo 	/* compute clustsize and round to the next page */
551ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
552ccdc3305SLuigi Rizzo 	i =  (clustsize & (PAGE_SIZE - 1));
553ccdc3305SLuigi Rizzo 	if (i)
554ccdc3305SLuigi Rizzo 		clustsize += PAGE_SIZE - i;
555ae10d1afSLuigi Rizzo 	if (netmap_verbose)
556ccdc3305SLuigi Rizzo 		D("objsize %d clustsize %d objects %d",
557ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
558ccdc3305SLuigi Rizzo 
559ccdc3305SLuigi Rizzo 	/*
560ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
561ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
562ccdc3305SLuigi Rizzo 	 */
563ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
564ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
565ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
566ce3ee1e7SLuigi Rizzo 
567ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
5688241616dSLuigi Rizzo 	p->_objsize = objsize;
569ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
570ccdc3305SLuigi Rizzo 
5718241616dSLuigi Rizzo 	return 0;
5728241616dSLuigi Rizzo }
5738241616dSLuigi Rizzo 
5748241616dSLuigi Rizzo 
5758241616dSLuigi Rizzo /* call with NMA_LOCK held */
5768241616dSLuigi Rizzo static int
5778241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
5788241616dSLuigi Rizzo {
579ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
580ce3ee1e7SLuigi Rizzo 	size_t n;
581ce3ee1e7SLuigi Rizzo 
582ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
583ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
584ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
5858241616dSLuigi Rizzo 
5868241616dSLuigi Rizzo 	n = sizeof(struct lut_entry) * p->objtotal;
5878241616dSLuigi Rizzo #ifdef linux
5888241616dSLuigi Rizzo 	p->lut = vmalloc(n);
5898241616dSLuigi Rizzo #else
590d2b91851SEd Maste 	p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
5918241616dSLuigi Rizzo #endif
592ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
593ce3ee1e7SLuigi Rizzo 		D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
594ccdc3305SLuigi Rizzo 		goto clean;
595ccdc3305SLuigi Rizzo 	}
596ccdc3305SLuigi Rizzo 
597ccdc3305SLuigi Rizzo 	/* Allocate the bitmap */
598ccdc3305SLuigi Rizzo 	n = (p->objtotal + 31) / 32;
599d2b91851SEd Maste 	p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
600ccdc3305SLuigi Rizzo 	if (p->bitmap == NULL) {
601ce3ee1e7SLuigi Rizzo 		D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
6028241616dSLuigi Rizzo 		    p->name);
603ccdc3305SLuigi Rizzo 		goto clean;
604ccdc3305SLuigi Rizzo 	}
6058241616dSLuigi Rizzo 	p->bitmap_slots = n;
606ccdc3305SLuigi Rizzo 
607ccdc3305SLuigi Rizzo 	/*
608ccdc3305SLuigi Rizzo 	 * Allocate clusters, init pointers and bitmap
609ccdc3305SLuigi Rizzo 	 */
610ce3ee1e7SLuigi Rizzo 
611ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
612ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
613ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
614ccdc3305SLuigi Rizzo 		char *clust;
615ccdc3305SLuigi Rizzo 
616ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
617ce3ee1e7SLuigi Rizzo 		    (size_t)0, -1UL, PAGE_SIZE, 0);
618ccdc3305SLuigi Rizzo 		if (clust == NULL) {
619ccdc3305SLuigi Rizzo 			/*
620ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
621ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
622ccdc3305SLuigi Rizzo 			 */
623ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
6248241616dSLuigi Rizzo 			    i, p->name);
625ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
626ce3ee1e7SLuigi Rizzo 				goto out;
627ccdc3305SLuigi Rizzo 			lim = i / 2;
6288241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
629ccdc3305SLuigi Rizzo 				p->bitmap[ (i>>5) ] &=  ~( 1 << (i & 31) );
630ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
631ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
632ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
633ccdc3305SLuigi Rizzo 			}
634ce3ee1e7SLuigi Rizzo 		out:
635ccdc3305SLuigi Rizzo 			p->objtotal = i;
636ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
637ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
638ccdc3305SLuigi Rizzo 			break;
639ccdc3305SLuigi Rizzo 		}
6408241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
641ccdc3305SLuigi Rizzo 			p->bitmap[ (i>>5) ] |=  ( 1 << (i & 31) );
642ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
643ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
644ccdc3305SLuigi Rizzo 		}
645ccdc3305SLuigi Rizzo 	}
646ce3ee1e7SLuigi Rizzo 	p->objfree = p->objtotal;
647ce3ee1e7SLuigi Rizzo 	p->memtotal = p->numclusters * p->_clustsize;
648ce3ee1e7SLuigi Rizzo 	if (p->objfree == 0)
649ce3ee1e7SLuigi Rizzo 		goto clean;
650ae10d1afSLuigi Rizzo 	if (netmap_verbose)
651ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
652ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
653ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
654ccdc3305SLuigi Rizzo 
6558241616dSLuigi Rizzo 	return 0;
656ccdc3305SLuigi Rizzo 
657ccdc3305SLuigi Rizzo clean:
6588241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
6598241616dSLuigi Rizzo 	return ENOMEM;
6608241616dSLuigi Rizzo }
6618241616dSLuigi Rizzo 
6628241616dSLuigi Rizzo /* call with lock held */
6638241616dSLuigi Rizzo static int
664ce3ee1e7SLuigi Rizzo netmap_memory_config_changed(struct netmap_mem_d *nmd)
6658241616dSLuigi Rizzo {
6668241616dSLuigi Rizzo 	int i;
6678241616dSLuigi Rizzo 
6688241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
669ce3ee1e7SLuigi Rizzo 		if (nmd->pools[i].r_objsize != netmap_params[i].size ||
670ce3ee1e7SLuigi Rizzo 		    nmd->pools[i].r_objtotal != netmap_params[i].num)
6718241616dSLuigi Rizzo 		    return 1;
6728241616dSLuigi Rizzo 	}
6738241616dSLuigi Rizzo 	return 0;
6748241616dSLuigi Rizzo }
6758241616dSLuigi Rizzo 
676ce3ee1e7SLuigi Rizzo static void
677ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
678ce3ee1e7SLuigi Rizzo {
679ce3ee1e7SLuigi Rizzo 	int i;
680ce3ee1e7SLuigi Rizzo 	D("resetting %p", nmd);
681ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
682ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
683ce3ee1e7SLuigi Rizzo 	}
684ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
685ce3ee1e7SLuigi Rizzo }
686ce3ee1e7SLuigi Rizzo 
687ce3ee1e7SLuigi Rizzo static int
688ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
689ce3ee1e7SLuigi Rizzo {
690ce3ee1e7SLuigi Rizzo 	int i;
691ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
692ce3ee1e7SLuigi Rizzo 		return 0;
693ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
694ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
695ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
696ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
697ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
698ce3ee1e7SLuigi Rizzo 			goto error;
699ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
700ce3ee1e7SLuigi Rizzo 	}
701ce3ee1e7SLuigi Rizzo 	/* buffers 0 and 1 are reserved */
702ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
703ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
704ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
705ce3ee1e7SLuigi Rizzo 
706ce3ee1e7SLuigi Rizzo 	D("Have %d KB for interfaces, %d KB for rings and %d MB for buffers",
707ce3ee1e7SLuigi Rizzo 	    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
708ce3ee1e7SLuigi Rizzo 	    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
709ce3ee1e7SLuigi Rizzo 	    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
710ce3ee1e7SLuigi Rizzo 
711ce3ee1e7SLuigi Rizzo 	D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
712ce3ee1e7SLuigi Rizzo 
713ce3ee1e7SLuigi Rizzo 
714ce3ee1e7SLuigi Rizzo 	return 0;
715ce3ee1e7SLuigi Rizzo error:
716ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
717ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
718ce3ee1e7SLuigi Rizzo }
719ce3ee1e7SLuigi Rizzo 
720ce3ee1e7SLuigi Rizzo 
721ce3ee1e7SLuigi Rizzo 
722ce3ee1e7SLuigi Rizzo void
723ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(struct netmap_mem_d *nmd)
724ce3ee1e7SLuigi Rizzo {
725ce3ee1e7SLuigi Rizzo 	if (nmd == NULL)
726ce3ee1e7SLuigi Rizzo 		return;
727ce3ee1e7SLuigi Rizzo 	D("deleting %p", nmd);
728ce3ee1e7SLuigi Rizzo 	if (nmd->refcount > 0)
729ce3ee1e7SLuigi Rizzo 		D("bug: deleting mem allocator with refcount=%d!", nmd->refcount);
730ce3ee1e7SLuigi Rizzo 	D("done deleting %p", nmd);
731ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
732ce3ee1e7SLuigi Rizzo 	free(nmd, M_DEVBUF);
733ce3ee1e7SLuigi Rizzo }
734ce3ee1e7SLuigi Rizzo 
735ce3ee1e7SLuigi Rizzo static int
736ce3ee1e7SLuigi Rizzo netmap_mem_private_config(struct netmap_mem_d *nmd)
737ce3ee1e7SLuigi Rizzo {
738ce3ee1e7SLuigi Rizzo 	/* nothing to do, we are configured on creation
739ce3ee1e7SLuigi Rizzo  	 * and configuration never changes thereafter
740ce3ee1e7SLuigi Rizzo  	 */
741ce3ee1e7SLuigi Rizzo 	return 0;
742ce3ee1e7SLuigi Rizzo }
743ce3ee1e7SLuigi Rizzo 
744ce3ee1e7SLuigi Rizzo static int
745ce3ee1e7SLuigi Rizzo netmap_mem_private_finalize(struct netmap_mem_d *nmd)
746ce3ee1e7SLuigi Rizzo {
747ce3ee1e7SLuigi Rizzo 	int err;
748ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
749ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
750ce3ee1e7SLuigi Rizzo 	err = netmap_mem_finalize_all(nmd);
751ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
752ce3ee1e7SLuigi Rizzo 	return err;
753ce3ee1e7SLuigi Rizzo 
754ce3ee1e7SLuigi Rizzo }
755ce3ee1e7SLuigi Rizzo 
756f9790aebSLuigi Rizzo static void
757f9790aebSLuigi Rizzo netmap_mem_private_deref(struct netmap_mem_d *nmd)
758ce3ee1e7SLuigi Rizzo {
759ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
760ce3ee1e7SLuigi Rizzo 	if (--nmd->refcount <= 0)
761ce3ee1e7SLuigi Rizzo 		netmap_mem_reset_all(nmd);
762ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
763ce3ee1e7SLuigi Rizzo }
764ce3ee1e7SLuigi Rizzo 
765ce3ee1e7SLuigi Rizzo struct netmap_mem_d *
766ce3ee1e7SLuigi Rizzo netmap_mem_private_new(const char *name, u_int txr, u_int txd, u_int rxr, u_int rxd)
767ce3ee1e7SLuigi Rizzo {
768ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
769ce3ee1e7SLuigi Rizzo 	struct netmap_obj_params p[NETMAP_POOLS_NR];
770ce3ee1e7SLuigi Rizzo 	int i;
771ce3ee1e7SLuigi Rizzo 	u_int maxd;
772ce3ee1e7SLuigi Rizzo 
773ce3ee1e7SLuigi Rizzo 	d = malloc(sizeof(struct netmap_mem_d),
774ce3ee1e7SLuigi Rizzo 			M_DEVBUF, M_NOWAIT | M_ZERO);
775ce3ee1e7SLuigi Rizzo 	if (d == NULL)
776ce3ee1e7SLuigi Rizzo 		return NULL;
777ce3ee1e7SLuigi Rizzo 
778ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
779ce3ee1e7SLuigi Rizzo 
780ce3ee1e7SLuigi Rizzo 	/* XXX the rest of the code assumes the stack rings are alwasy present */
781ce3ee1e7SLuigi Rizzo 	txr++;
782ce3ee1e7SLuigi Rizzo 	rxr++;
783ce3ee1e7SLuigi Rizzo 	p[NETMAP_IF_POOL].size = sizeof(struct netmap_if) +
784ce3ee1e7SLuigi Rizzo 		sizeof(ssize_t) * (txr + rxr);
785ce3ee1e7SLuigi Rizzo 	p[NETMAP_IF_POOL].num = 2;
786ce3ee1e7SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
787ce3ee1e7SLuigi Rizzo 	p[NETMAP_RING_POOL].size = sizeof(struct netmap_ring) +
788ce3ee1e7SLuigi Rizzo 		sizeof(struct netmap_slot) * maxd;
789ce3ee1e7SLuigi Rizzo 	p[NETMAP_RING_POOL].num = txr + rxr;
790ce3ee1e7SLuigi Rizzo 	p[NETMAP_BUF_POOL].size = 2048; /* XXX find a way to let the user choose this */
791ce3ee1e7SLuigi Rizzo 	p[NETMAP_BUF_POOL].num = rxr * (rxd + 2) + txr * (txd + 2);
792ce3ee1e7SLuigi Rizzo 
793ce3ee1e7SLuigi Rizzo 	D("req if %d*%d ring %d*%d buf %d*%d",
794ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
795ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
796ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
797ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
798ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
799ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
800ce3ee1e7SLuigi Rizzo 
801ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
802ce3ee1e7SLuigi Rizzo 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
803ce3ee1e7SLuigi Rizzo 				nm_blueprint.pools[i].name,
804ce3ee1e7SLuigi Rizzo 				name);
805ce3ee1e7SLuigi Rizzo 		if (netmap_config_obj_allocator(&d->pools[i],
806ce3ee1e7SLuigi Rizzo 				p[i].num, p[i].size))
807ce3ee1e7SLuigi Rizzo 			goto error;
808ce3ee1e7SLuigi Rizzo 	}
809ce3ee1e7SLuigi Rizzo 
810ce3ee1e7SLuigi Rizzo 	d->flags &= ~NETMAP_MEM_FINALIZED;
811ce3ee1e7SLuigi Rizzo 
812ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(d);
813ce3ee1e7SLuigi Rizzo 
814ce3ee1e7SLuigi Rizzo 	return d;
815ce3ee1e7SLuigi Rizzo error:
816ce3ee1e7SLuigi Rizzo 	netmap_mem_private_delete(d);
817ce3ee1e7SLuigi Rizzo 	return NULL;
818ce3ee1e7SLuigi Rizzo }
819ce3ee1e7SLuigi Rizzo 
8208241616dSLuigi Rizzo 
8218241616dSLuigi Rizzo /* call with lock held */
8228241616dSLuigi Rizzo static int
823ce3ee1e7SLuigi Rizzo netmap_mem_global_config(struct netmap_mem_d *nmd)
8248241616dSLuigi Rizzo {
8258241616dSLuigi Rizzo 	int i;
8268241616dSLuigi Rizzo 
827ce3ee1e7SLuigi Rizzo 	if (nmd->refcount)
828ce3ee1e7SLuigi Rizzo 		/* already in use, we cannot change the configuration */
829ce3ee1e7SLuigi Rizzo 		goto out;
830ce3ee1e7SLuigi Rizzo 
831ce3ee1e7SLuigi Rizzo 	if (!netmap_memory_config_changed(nmd))
8328241616dSLuigi Rizzo 		goto out;
8338241616dSLuigi Rizzo 
8348241616dSLuigi Rizzo 	D("reconfiguring");
8358241616dSLuigi Rizzo 
836ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
8378241616dSLuigi Rizzo 		/* reset previous allocation */
8388241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
839ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
8408241616dSLuigi Rizzo 		}
841ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
8428241616dSLuigi Rizzo 	}
8438241616dSLuigi Rizzo 
8448241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
845ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
8468241616dSLuigi Rizzo 				netmap_params[i].num, netmap_params[i].size);
847ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
8488241616dSLuigi Rizzo 			goto out;
8498241616dSLuigi Rizzo 	}
8508241616dSLuigi Rizzo 
8518241616dSLuigi Rizzo out:
8528241616dSLuigi Rizzo 
853ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
8548241616dSLuigi Rizzo }
8558241616dSLuigi Rizzo 
8568241616dSLuigi Rizzo static int
857ce3ee1e7SLuigi Rizzo netmap_mem_global_finalize(struct netmap_mem_d *nmd)
8588241616dSLuigi Rizzo {
859ce3ee1e7SLuigi Rizzo 	int err;
8608241616dSLuigi Rizzo 
861ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
862ce3ee1e7SLuigi Rizzo 
8638241616dSLuigi Rizzo 
8648241616dSLuigi Rizzo 	/* update configuration if changed */
865ce3ee1e7SLuigi Rizzo 	if (netmap_mem_global_config(nmd))
8668241616dSLuigi Rizzo 		goto out;
8678241616dSLuigi Rizzo 
868ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
869ce3ee1e7SLuigi Rizzo 
870ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
8718241616dSLuigi Rizzo 		/* may happen if config is not changed */
8728241616dSLuigi Rizzo 		ND("nothing to do");
8738241616dSLuigi Rizzo 		goto out;
8748241616dSLuigi Rizzo 	}
8758241616dSLuigi Rizzo 
876ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
877ce3ee1e7SLuigi Rizzo 		goto out;
8788241616dSLuigi Rizzo 
8798241616dSLuigi Rizzo 	/* backward compatibility */
880ce3ee1e7SLuigi Rizzo 	netmap_buf_size = nmd->pools[NETMAP_BUF_POOL]._objsize;
881ce3ee1e7SLuigi Rizzo 	netmap_total_buffers = nmd->pools[NETMAP_BUF_POOL].objtotal;
8828241616dSLuigi Rizzo 
883ce3ee1e7SLuigi Rizzo 	netmap_buffer_lut = nmd->pools[NETMAP_BUF_POOL].lut;
884ce3ee1e7SLuigi Rizzo 	netmap_buffer_base = nmd->pools[NETMAP_BUF_POOL].lut[0].vaddr;
8858241616dSLuigi Rizzo 
886ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
8878241616dSLuigi Rizzo 
8888241616dSLuigi Rizzo out:
889ce3ee1e7SLuigi Rizzo 	if (nmd->lasterr)
890ce3ee1e7SLuigi Rizzo 		nmd->refcount--;
891ce3ee1e7SLuigi Rizzo 	err = nmd->lasterr;
8928241616dSLuigi Rizzo 
893ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
8948241616dSLuigi Rizzo 
895ce3ee1e7SLuigi Rizzo 	return err;
8968241616dSLuigi Rizzo 
897ccdc3305SLuigi Rizzo }
898ccdc3305SLuigi Rizzo 
899ce3ee1e7SLuigi Rizzo int
900ce3ee1e7SLuigi Rizzo netmap_mem_init(void)
901ccdc3305SLuigi Rizzo {
902ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
9038241616dSLuigi Rizzo 	return (0);
904ccdc3305SLuigi Rizzo }
905ccdc3305SLuigi Rizzo 
906ce3ee1e7SLuigi Rizzo void
907ce3ee1e7SLuigi Rizzo netmap_mem_fini(void)
908ccdc3305SLuigi Rizzo {
9098241616dSLuigi Rizzo 	int i;
9108241616dSLuigi Rizzo 
9118241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
9128241616dSLuigi Rizzo 	    netmap_destroy_obj_allocator(&nm_mem.pools[i]);
9138241616dSLuigi Rizzo 	}
914ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(&nm_mem);
9158241616dSLuigi Rizzo }
9168241616dSLuigi Rizzo 
9178241616dSLuigi Rizzo static void
9188241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
9198241616dSLuigi Rizzo {
920ce3ee1e7SLuigi Rizzo 	u_int i;
921ae10d1afSLuigi Rizzo 	if (!na->tx_rings)
922ae10d1afSLuigi Rizzo 		return;
9238241616dSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
924ce3ee1e7SLuigi Rizzo 		if (na->tx_rings[i].ring) {
925ce3ee1e7SLuigi Rizzo 			netmap_ring_free(na->nm_mem, na->tx_rings[i].ring);
9268241616dSLuigi Rizzo 			na->tx_rings[i].ring = NULL;
9278241616dSLuigi Rizzo 		}
928ce3ee1e7SLuigi Rizzo 	}
9298241616dSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
930ce3ee1e7SLuigi Rizzo 		if (na->rx_rings[i].ring) {
931ce3ee1e7SLuigi Rizzo 			netmap_ring_free(na->nm_mem, na->rx_rings[i].ring);
9328241616dSLuigi Rizzo 			na->rx_rings[i].ring = NULL;
9338241616dSLuigi Rizzo 		}
934ce3ee1e7SLuigi Rizzo 	}
935ccdc3305SLuigi Rizzo }
936ccdc3305SLuigi Rizzo 
937f9790aebSLuigi Rizzo /* call with NMA_LOCK held *
938f9790aebSLuigi Rizzo  *
939f9790aebSLuigi Rizzo  * Allocate netmap rings and buffers for this card
940f9790aebSLuigi Rizzo  * The rings are contiguous, but have variable size.
941f9790aebSLuigi Rizzo  */
942f9790aebSLuigi Rizzo int
943f9790aebSLuigi Rizzo netmap_mem_rings_create(struct netmap_adapter *na)
944f9790aebSLuigi Rizzo {
945f9790aebSLuigi Rizzo 	struct netmap_ring *ring;
946f9790aebSLuigi Rizzo 	u_int len, ndesc;
947f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
948f9790aebSLuigi Rizzo 
949f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
950f9790aebSLuigi Rizzo 
951f9790aebSLuigi Rizzo 	for (kring = na->tx_rings; kring != na->rx_rings; kring++) { /* Transmit rings */
952f9790aebSLuigi Rizzo 		ndesc = kring->nkr_num_slots;
953f9790aebSLuigi Rizzo 		len = sizeof(struct netmap_ring) +
954f9790aebSLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
955f9790aebSLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
956f9790aebSLuigi Rizzo 		if (ring == NULL) {
957f9790aebSLuigi Rizzo 			D("Cannot allocate tx_ring");
958f9790aebSLuigi Rizzo 			goto cleanup;
959f9790aebSLuigi Rizzo 		}
960*f6c2a31fSLuigi Rizzo 		ND("txring at %p", ring);
961f9790aebSLuigi Rizzo 		kring->ring = ring;
962f9790aebSLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
96317885a7bSLuigi Rizzo 		*(int64_t *)(uintptr_t)&ring->buf_ofs =
964f9790aebSLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
965f9790aebSLuigi Rizzo 			na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
966f9790aebSLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
967f9790aebSLuigi Rizzo 
96817885a7bSLuigi Rizzo 		/* copy values from kring */
96917885a7bSLuigi Rizzo 		ring->head = kring->rhead;
97017885a7bSLuigi Rizzo 		ring->cur = kring->rcur;
97117885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
972f9790aebSLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size =
973f9790aebSLuigi Rizzo 			NETMAP_BDG_BUF_SIZE(na->nm_mem);
974f9790aebSLuigi Rizzo 		ND("initializing slots for txring");
975f9790aebSLuigi Rizzo 		if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
976f9790aebSLuigi Rizzo 			D("Cannot allocate buffers for tx_ring");
977f9790aebSLuigi Rizzo 			goto cleanup;
978f9790aebSLuigi Rizzo 		}
979f9790aebSLuigi Rizzo 	}
980f9790aebSLuigi Rizzo 
981f9790aebSLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) { /* Receive rings */
982f9790aebSLuigi Rizzo 		ndesc = kring->nkr_num_slots;
983f9790aebSLuigi Rizzo 		len = sizeof(struct netmap_ring) +
984f9790aebSLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
985f9790aebSLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
986f9790aebSLuigi Rizzo 		if (ring == NULL) {
987f9790aebSLuigi Rizzo 			D("Cannot allocate rx_ring");
988f9790aebSLuigi Rizzo 			goto cleanup;
989f9790aebSLuigi Rizzo 		}
990*f6c2a31fSLuigi Rizzo 		ND("rxring at %p", ring);
991f9790aebSLuigi Rizzo 		kring->ring = ring;
992f9790aebSLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
99317885a7bSLuigi Rizzo 		*(int64_t *)(uintptr_t)&ring->buf_ofs =
994f9790aebSLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
995f9790aebSLuigi Rizzo 		        na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
996f9790aebSLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
997f9790aebSLuigi Rizzo 
99817885a7bSLuigi Rizzo 		/* copy values from kring */
99917885a7bSLuigi Rizzo 		ring->head = kring->rhead;
100017885a7bSLuigi Rizzo 		ring->cur = kring->rcur;
100117885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
1002f9790aebSLuigi Rizzo 		*(int *)(uintptr_t)&ring->nr_buf_size =
1003f9790aebSLuigi Rizzo 			NETMAP_BDG_BUF_SIZE(na->nm_mem);
1004*f6c2a31fSLuigi Rizzo 		ND("initializing slots for rxring %p", ring);
1005f9790aebSLuigi Rizzo 		if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1006f9790aebSLuigi Rizzo 			D("Cannot allocate buffers for rx_ring");
1007f9790aebSLuigi Rizzo 			goto cleanup;
1008f9790aebSLuigi Rizzo 		}
1009f9790aebSLuigi Rizzo 	}
1010f9790aebSLuigi Rizzo 
1011f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1012f9790aebSLuigi Rizzo 
1013f9790aebSLuigi Rizzo 	return 0;
1014f9790aebSLuigi Rizzo 
1015f9790aebSLuigi Rizzo cleanup:
1016f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1017f9790aebSLuigi Rizzo 
1018f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1019f9790aebSLuigi Rizzo 
1020f9790aebSLuigi Rizzo 	return ENOMEM;
1021f9790aebSLuigi Rizzo }
1022f9790aebSLuigi Rizzo 
1023f9790aebSLuigi Rizzo void
1024f9790aebSLuigi Rizzo netmap_mem_rings_delete(struct netmap_adapter *na)
1025f9790aebSLuigi Rizzo {
1026f9790aebSLuigi Rizzo 	/* last instance, release bufs and rings */
1027f9790aebSLuigi Rizzo 	u_int i, lim;
1028f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
1029f9790aebSLuigi Rizzo 	struct netmap_ring *ring;
1030f9790aebSLuigi Rizzo 
1031f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1032f9790aebSLuigi Rizzo 
1033f9790aebSLuigi Rizzo 	for (kring = na->tx_rings; kring != na->tailroom; kring++) {
1034f9790aebSLuigi Rizzo 		ring = kring->ring;
1035f9790aebSLuigi Rizzo 		if (ring == NULL)
1036f9790aebSLuigi Rizzo 			continue;
1037f9790aebSLuigi Rizzo 		lim = kring->nkr_num_slots;
1038f9790aebSLuigi Rizzo 		for (i = 0; i < lim; i++)
1039f9790aebSLuigi Rizzo 			netmap_free_buf(na->nm_mem, ring->slot[i].buf_idx);
1040f9790aebSLuigi Rizzo 	}
1041f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1042f9790aebSLuigi Rizzo 
1043f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1044f9790aebSLuigi Rizzo }
1045ccdc3305SLuigi Rizzo 
1046ccdc3305SLuigi Rizzo 
10478241616dSLuigi Rizzo /* call with NMA_LOCK held */
1048ae10d1afSLuigi Rizzo /*
1049ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
1050ce3ee1e7SLuigi Rizzo  *
1051ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
1052ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
1053ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
1054ae10d1afSLuigi Rizzo  */
1055ce3ee1e7SLuigi Rizzo struct netmap_if *
1056ce3ee1e7SLuigi Rizzo netmap_mem_if_new(const char *ifname, struct netmap_adapter *na)
1057ccdc3305SLuigi Rizzo {
1058ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
1059ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
1060f9790aebSLuigi Rizzo 	u_int i, len, ntx, nrx;
1061ccdc3305SLuigi Rizzo 
1062ce3ee1e7SLuigi Rizzo 	/*
1063ce3ee1e7SLuigi Rizzo 	 * verify whether virtual port need the stack ring
1064ce3ee1e7SLuigi Rizzo 	 */
1065ae10d1afSLuigi Rizzo 	ntx = na->num_tx_rings + 1; /* shorthand, include stack ring */
1066ae10d1afSLuigi Rizzo 	nrx = na->num_rx_rings + 1; /* shorthand, include stack ring */
1067ccdc3305SLuigi Rizzo 	/*
1068ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
1069ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
1070ce3ee1e7SLuigi Rizzo 	 * For virtual rx rings we also allocate an array of
1071ce3ee1e7SLuigi Rizzo 	 * pointers to assign to nkr_leases.
1072ccdc3305SLuigi Rizzo 	 */
1073ce3ee1e7SLuigi Rizzo 
1074ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1075ce3ee1e7SLuigi Rizzo 
1076ccdc3305SLuigi Rizzo 	len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
1077ce3ee1e7SLuigi Rizzo 	nifp = netmap_if_malloc(na->nm_mem, len);
1078ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
1079ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(na->nm_mem);
1080ccdc3305SLuigi Rizzo 		return NULL;
1081ccdc3305SLuigi Rizzo 	}
1082ccdc3305SLuigi Rizzo 
1083ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
1084ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1085ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1086ce3ee1e7SLuigi Rizzo 	strncpy(nifp->ni_name, ifname, (size_t)IFNAMSIZ);
1087ccdc3305SLuigi Rizzo 
1088ccdc3305SLuigi Rizzo 	/*
1089ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
1090ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
1091ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
1092ccdc3305SLuigi Rizzo 	 */
1093ce3ee1e7SLuigi Rizzo 	base = netmap_if_offset(na->nm_mem, nifp);
1094ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
1095ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1096ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1097ccdc3305SLuigi Rizzo 	}
1098ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
1099ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
1100ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1101ccdc3305SLuigi Rizzo 	}
1102ce3ee1e7SLuigi Rizzo 
1103ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1104ce3ee1e7SLuigi Rizzo 
1105ccdc3305SLuigi Rizzo 	return (nifp);
1106ccdc3305SLuigi Rizzo }
1107ccdc3305SLuigi Rizzo 
1108ce3ee1e7SLuigi Rizzo void
1109ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1110ccdc3305SLuigi Rizzo {
1111ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
1112ce3ee1e7SLuigi Rizzo 		/* nothing to do */
1113ce3ee1e7SLuigi Rizzo 		return;
1114ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1115ce3ee1e7SLuigi Rizzo 
1116ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
1117ce3ee1e7SLuigi Rizzo 
1118ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1119ce3ee1e7SLuigi Rizzo }
1120ce3ee1e7SLuigi Rizzo 
1121ce3ee1e7SLuigi Rizzo static void
1122ce3ee1e7SLuigi Rizzo netmap_mem_global_deref(struct netmap_mem_d *nmd)
1123ce3ee1e7SLuigi Rizzo {
1124ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
1125ce3ee1e7SLuigi Rizzo 
1126ce3ee1e7SLuigi Rizzo 	nmd->refcount--;
1127ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1128ce3ee1e7SLuigi Rizzo 		D("refcount = %d", nmd->refcount);
1129ce3ee1e7SLuigi Rizzo 
1130ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
1131ce3ee1e7SLuigi Rizzo }
1132ce3ee1e7SLuigi Rizzo 
1133f9790aebSLuigi Rizzo int
1134f9790aebSLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd)
1135ce3ee1e7SLuigi Rizzo {
1136ce3ee1e7SLuigi Rizzo 	return nmd->finalize(nmd);
1137ce3ee1e7SLuigi Rizzo }
1138ce3ee1e7SLuigi Rizzo 
1139f9790aebSLuigi Rizzo void
1140f9790aebSLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd)
1141ce3ee1e7SLuigi Rizzo {
1142ce3ee1e7SLuigi Rizzo 	return nmd->deref(nmd);
1143ccdc3305SLuigi Rizzo }
1144