2 * Copyright (C) 2012-2014 Matteo Landi
3 * Copyright (C) 2012-2016 Luigi Rizzo
4 * Copyright (C) 2012-2016 Giuseppe Lettieri
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #endif /* __APPLE__ */
38 #include <sys/cdefs.h> /* prerequisite */
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h> /* MALLOC_DEFINE */
45 #include <vm/vm.h> /* vtophys */
46 #include <vm/pmap.h> /* vtophys */
47 #include <sys/socket.h> /* sockaddrs */
48 #include <sys/selinfo.h>
49 #include <sys/sysctl.h>
51 #include <net/if_var.h>
53 #include <machine/bus.h> /* bus_dmamap_* */
55 /* M_NETMAP only used in here */
56 MALLOC_DECLARE(M_NETMAP);
57 MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
59 #endif /* __FreeBSD__ */
65 #include <net/netmap.h>
66 #include <dev/netmap/netmap_kern.h>
67 #include <net/netmap_virt.h>
68 #include "netmap_mem2.h"
70 #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY
71 #define NETMAP_BUF_MAX_NUM 8*4096 /* if too big takes too much time to allocate */
73 #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */
76 #define NETMAP_POOL_MAX_NAMSZ 32
87 struct netmap_obj_params {
92 struct netmap_obj_pool {
93 char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */
95 /* ---------------------------------------------------*/
96 /* these are only meaningful if the pool is finalized */
97 /* (see 'finalized' field in netmap_mem_d) */
98 u_int objtotal; /* actual total number of objects. */
99 u_int memtotal; /* actual total memory space */
100 u_int numclusters; /* actual number of clusters */
102 u_int objfree; /* number of free objects. */
104 struct lut_entry *lut; /* virt,phys addresses, objtotal entries */
105 uint32_t *bitmap; /* one bit per buffer, 1 means free */
106 uint32_t bitmap_slots; /* number of uint32 entries in bitmap */
107 /* ---------------------------------------------------*/
110 u_int objminsize; /* minimum object size */
111 u_int objmaxsize; /* maximum object size */
112 u_int nummin; /* minimum number of objects */
113 u_int nummax; /* maximum number of objects */
115 /* these are changed only by config */
116 u_int _objtotal; /* total number of objects */
117 u_int _objsize; /* object size */
118 u_int _clustsize; /* cluster size */
119 u_int _clustentries; /* objects per cluster */
120 u_int _numclusters; /* number of clusters */
122 /* requested values */
127 #define NMA_LOCK_T NM_MTX_T
130 struct netmap_mem_ops {
131 int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
132 int (*nmd_get_info)(struct netmap_mem_d *, u_int *size,
133 u_int *memflags, uint16_t *id);
135 vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
136 int (*nmd_config)(struct netmap_mem_d *);
137 int (*nmd_finalize)(struct netmap_mem_d *);
138 void (*nmd_deref)(struct netmap_mem_d *);
139 ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
140 void (*nmd_delete)(struct netmap_mem_d *);
142 struct netmap_if * (*nmd_if_new)(struct netmap_adapter *);
143 void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
144 int (*nmd_rings_create)(struct netmap_adapter *);
145 void (*nmd_rings_delete)(struct netmap_adapter *);
148 typedef uint16_t nm_memid_t;
150 struct netmap_mem_d {
151 NMA_LOCK_T nm_mtx; /* protect the allocator */
152 u_int nm_totalsize; /* shorthand */
155 #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */
156 int lasterr; /* last error for curr config */
157 int active; /* active users */
159 /* the three allocators */
160 struct netmap_obj_pool pools[NETMAP_POOLS_NR];
162 nm_memid_t nm_id; /* allocator identifier */
163 int nm_grp; /* iommu groupd id */
165 /* list of all existing allocators, sorted by nm_id */
166 struct netmap_mem_d *prev, *next;
168 struct netmap_mem_ops *ops;
172 * XXX need to fix the case of t0 == void
174 #define NMD_DEFCB(t0, name) \
176 netmap_mem_##name(struct netmap_mem_d *nmd) \
178 return nmd->ops->nmd_##name(nmd); \
181 #define NMD_DEFCB1(t0, name, t1) \
183 netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1) \
185 return nmd->ops->nmd_##name(nmd, a1); \
188 #define NMD_DEFCB3(t0, name, t1, t2, t3) \
190 netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1, t2 a2, t3 a3) \
192 return nmd->ops->nmd_##name(nmd, a1, a2, a3); \
195 #define NMD_DEFNACB(t0, name) \
197 netmap_mem_##name(struct netmap_adapter *na) \
199 return na->nm_mem->ops->nmd_##name(na); \
202 #define NMD_DEFNACB1(t0, name, t1) \
204 netmap_mem_##name(struct netmap_adapter *na, t1 a1) \
206 return na->nm_mem->ops->nmd_##name(na, a1); \
209 NMD_DEFCB1(int, get_lut, struct netmap_lut *);
210 NMD_DEFCB3(int, get_info, u_int *, u_int *, uint16_t *);
211 NMD_DEFCB1(vm_paddr_t, ofstophys, vm_ooffset_t);
212 static int netmap_mem_config(struct netmap_mem_d *);
213 NMD_DEFCB(int, config);
214 NMD_DEFCB1(ssize_t, if_offset, const void *);
215 NMD_DEFCB(void, delete);
217 NMD_DEFNACB(struct netmap_if *, if_new);
218 NMD_DEFNACB1(void, if_delete, struct netmap_if *);
219 NMD_DEFNACB(int, rings_create);
220 NMD_DEFNACB(void, rings_delete);
222 static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
223 static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
224 static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
226 #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx)
227 #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx)
228 #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx)
229 #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx)
231 #ifdef NM_DEBUG_MEM_PUTGET
232 #define NM_DBG_REFC(nmd, func, line) \
233 printf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
235 #define NM_DBG_REFC(nmd, func, line)
238 #ifdef NM_DEBUG_MEM_PUTGET
239 void __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
241 void netmap_mem_get(struct netmap_mem_d *nmd)
246 NM_DBG_REFC(nmd, func, line);
250 #ifdef NM_DEBUG_MEM_PUTGET
251 void __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
253 void netmap_mem_put(struct netmap_mem_d *nmd)
258 last = (--nmd->refcount == 0);
259 NM_DBG_REFC(nmd, func, line);
262 netmap_mem_delete(nmd);
266 netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
268 if (nm_mem_assign_group(nmd, na->pdev) < 0) {
272 nmd->lasterr = nmd->ops->nmd_finalize(nmd);
276 if (!nmd->lasterr && na->pdev)
277 netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
283 netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
286 netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
287 if (nmd->active == 1) {
291 * Reset the allocator when it falls out of use so that any
292 * pool resources leaked by unclean application exits are
295 for (i = 0; i < NETMAP_POOLS_NR; i++) {
296 struct netmap_obj_pool *p;
300 p->objfree = p->objtotal;
302 * Reproduce the net effect of the M_ZERO malloc()
303 * and marking of free entries in the bitmap that
304 * occur in finalize_obj_allocator()
308 sizeof(uint32_t) * ((p->objtotal + 31) / 32));
311 * Set all the bits in the bitmap that have
312 * corresponding buffers to 1 to indicate they are
315 for (j = 0; j < p->objtotal; j++) {
316 if (p->lut[j].vaddr != NULL) {
317 p->bitmap[ (j>>5) ] |= ( 1 << (j & 31) );
323 * Per netmap_mem_finalize_all(),
324 * buffers 0 and 1 are reserved
326 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
327 if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
328 /* XXX This check is a workaround that prevents a
329 * NULL pointer crash which currently happens only
330 * with ptnetmap guests.
331 * Removed shared-info --> is the bug still there? */
332 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
335 nmd->ops->nmd_deref(nmd);
341 /* accessor functions */
343 netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
345 lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
346 lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
347 lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
352 static struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = {
357 [NETMAP_RING_POOL] = {
361 [NETMAP_BUF_POOL] = {
363 .num = NETMAP_BUF_MAX_NUM,
367 static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
372 [NETMAP_RING_POOL] = {
376 [NETMAP_BUF_POOL] = {
384 * nm_mem is the memory allocator used for all physical interfaces
385 * running in netmap mode.
386 * Virtual (VALE) ports will have each its own allocator.
388 extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
389 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
393 .objminsize = sizeof(struct netmap_if),
395 .nummin = 10, /* don't be stingy */
396 .nummax = 10000, /* XXX very large */
398 [NETMAP_RING_POOL] = {
399 .name = "netmap_ring",
400 .objminsize = sizeof(struct netmap_ring),
401 .objmaxsize = 32*PAGE_SIZE,
405 [NETMAP_BUF_POOL] = {
406 .name = "netmap_buf",
410 .nummax = 1000000, /* one million! */
420 .ops = &netmap_mem_global_ops
424 static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
426 /* blueprint for the private memory allocators */
427 extern struct netmap_mem_ops netmap_mem_private_ops; /* forward */
428 /* XXX clang is not happy about using name as a print format */
429 static const struct netmap_mem_d nm_blueprint = {
433 .objminsize = sizeof(struct netmap_if),
438 [NETMAP_RING_POOL] = {
440 .objminsize = sizeof(struct netmap_ring),
441 .objmaxsize = 32*PAGE_SIZE,
445 [NETMAP_BUF_POOL] = {
450 .nummax = 1000000, /* one million! */
454 .flags = NETMAP_MEM_PRIVATE,
456 .ops = &netmap_mem_private_ops
459 /* memory allocator related sysctls */
461 #define STRINGIFY(x) #x
464 #define DECLARE_SYSCTLS(id, name) \
465 SYSBEGIN(mem2_ ## name); \
466 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
467 CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
468 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
469 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
470 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
471 CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
472 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
473 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
474 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
475 CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
476 "Default size of private netmap " STRINGIFY(name) "s"); \
477 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
478 CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
479 "Default number of private netmap " STRINGIFY(name) "s"); \
482 SYSCTL_DECL(_dev_netmap);
483 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
484 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
485 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
487 /* call with NMA_LOCK(&nm_mem) held */
489 nm_mem_assign_id_locked(struct netmap_mem_d *nmd)
492 struct netmap_mem_d *scan = netmap_last_mem_d;
496 /* we rely on unsigned wrap around */
497 id = scan->nm_id + 1;
498 if (id == 0) /* reserve 0 as error value */
501 if (id != scan->nm_id) {
503 nmd->prev = scan->prev;
505 scan->prev->next = nmd;
507 netmap_last_mem_d = nmd;
511 } while (scan != netmap_last_mem_d);
516 /* call with NMA_LOCK(&nm_mem) *not* held */
518 nm_mem_assign_id(struct netmap_mem_d *nmd)
523 ret = nm_mem_assign_id_locked(nmd);
530 nm_mem_release_id(struct netmap_mem_d *nmd)
534 nmd->prev->next = nmd->next;
535 nmd->next->prev = nmd->prev;
537 if (netmap_last_mem_d == nmd)
538 netmap_last_mem_d = nmd->prev;
540 nmd->prev = nmd->next = NULL;
546 nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
549 id = nm_iommu_group_id(dev);
551 D("iommu_group %d", id);
558 if (nmd->nm_grp != id)
559 nmd->lasterr = err = ENOMEM;
566 * First, find the allocator that contains the requested offset,
567 * then locate the cluster through a lookup table.
570 netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
573 vm_ooffset_t o = offset;
575 struct netmap_obj_pool *p;
580 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
581 if (offset >= p[i].memtotal)
583 // now lookup the cluster's address
585 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
586 offset % p[i]._objsize;
588 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
589 pa.QuadPart += offset % p[i]._objsize;
594 /* this is only in case of errors */
595 D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
596 p[NETMAP_IF_POOL].memtotal,
597 p[NETMAP_IF_POOL].memtotal
598 + p[NETMAP_RING_POOL].memtotal,
599 p[NETMAP_IF_POOL].memtotal
600 + p[NETMAP_RING_POOL].memtotal
601 + p[NETMAP_BUF_POOL].memtotal);
604 return 0; // XXX bad address
615 * win32_build_virtual_memory_for_userspace
617 * This function get all the object making part of the pools and maps
618 * a contiguous virtual memory space for the userspace
620 * 1 - allocate a Memory Descriptor List wide as the sum
621 * of the memory needed for the pools
622 * 2 - cycle all the objects in every pool and for every object do
624 * 2a - cycle all the objects in every pool, get the list
625 * of the physical address descriptors
626 * 2b - calculate the offset in the array of pages desciptor in the
628 * 2c - copy the descriptors of the object in the main MDL
630 * 3 - return the resulting MDL that needs to be mapped in userland
632 * In this way we will have an MDL that describes all the memory for the
633 * objects in a single object
637 win32_build_user_vm_map(struct netmap_mem_d* nmd)
640 u_int memsize, memflags, ofs = 0;
641 PMDL mainMdl, tempMdl;
643 if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
644 D("memory not finalised yet");
648 mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
649 if (mainMdl == NULL) {
650 D("failed to allocate mdl");
655 for (i = 0; i < NETMAP_POOLS_NR; i++) {
656 struct netmap_obj_pool *p = &nmd->pools[i];
657 int clsz = p->_clustsize;
658 int clobjs = p->_clustentries; /* objects per cluster */
659 int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
660 PPFN_NUMBER pSrc, pDst;
662 /* each pool has a different cluster size so we need to reallocate */
663 tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
664 if (tempMdl == NULL) {
666 D("fail to allocate tempMdl");
670 pSrc = MmGetMdlPfnArray(tempMdl);
671 /* create one entry per cluster, the lut[] has one entry per object */
672 for (j = 0; j < p->numclusters; j++, ofs += clsz) {
673 pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
674 MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
675 MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
676 RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
677 mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
688 * helper function for OS-specific mmap routines (currently only windows).
689 * Given an nmd and a pool index, returns the cluster size and number of clusters.
690 * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
691 * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
695 netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
697 if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
698 return 1; /* invalid arguments */
699 // NMA_LOCK_ASSERT(nmd);
700 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
701 *clustsize = *numclusters = 0;
702 return 1; /* not ready yet */
704 *clustsize = nmd->pools[pool]._clustsize;
705 *numclusters = nmd->pools[pool].numclusters;
706 return 0; /* success */
710 netmap_mem2_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags,
715 error = netmap_mem_config(nmd);
719 if (nmd->flags & NETMAP_MEM_FINALIZED) {
720 *size = nmd->nm_totalsize;
724 for (i = 0; i < NETMAP_POOLS_NR; i++) {
725 struct netmap_obj_pool *p = nmd->pools + i;
726 *size += (p->_numclusters * p->_clustsize);
731 *memflags = nmd->flags;
740 * we store objects by kernel address, need to find the offset
741 * within the pool to export the value to userspace.
742 * Algorithm: scan until we find the cluster, then add the
743 * actual offset in the cluster
746 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
748 int i, k = p->_clustentries, n = p->objtotal;
751 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
752 const char *base = p->lut[i].vaddr;
753 ssize_t relofs = (const char *) vaddr - base;
755 if (relofs < 0 || relofs >= p->_clustsize)
759 ND("%s: return offset %d (cluster %d) for pointer %p",
760 p->name, ofs, i, vaddr);
763 D("address %p is not contained inside any cluster (%s)",
765 return 0; /* An error occurred */
768 /* Helper functions which convert virtual addresses to offsets */
769 #define netmap_if_offset(n, v) \
770 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
772 #define netmap_ring_offset(n, v) \
773 ((n)->pools[NETMAP_IF_POOL].memtotal + \
774 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
777 netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
781 v = netmap_if_offset(nmd, addr);
787 * report the index, and use start position as a hint,
788 * otherwise buffer allocation becomes terribly expensive.
791 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
793 uint32_t i = 0; /* index in the bitmap */
794 uint32_t mask, j = 0; /* slot counter */
797 if (len > p->_objsize) {
798 D("%s request size %d too large", p->name, len);
799 // XXX cannot reduce the size
803 if (p->objfree == 0) {
804 D("no more %s objects", p->name);
810 /* termination is guaranteed by p->free, but better check bounds on i */
811 while (vaddr == NULL && i < p->bitmap_slots) {
812 uint32_t cur = p->bitmap[i];
813 if (cur == 0) { /* bitmask is fully used */
818 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
821 p->bitmap[i] &= ~mask; /* mark object as in use */
824 vaddr = p->lut[i * 32 + j].vaddr;
828 ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
837 * free by index, not by address.
838 * XXX should we also cleanup the content ?
841 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
845 if (j >= p->objtotal) {
846 D("invalid index %u, max %u", j, p->objtotal);
849 ptr = &p->bitmap[j / 32];
850 mask = (1 << (j % 32));
852 D("ouch, double free on buffer %d", j);
862 * free by address. This is slow but is only used for a few
863 * objects (rings, nifp)
866 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
868 u_int i, j, n = p->numclusters;
870 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
871 void *base = p->lut[i * p->_clustentries].vaddr;
872 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
874 /* Given address, is out of the scope of the current cluster.*/
875 if (vaddr < base || relofs >= p->_clustsize)
878 j = j + relofs / p->_objsize;
879 /* KASSERT(j != 0, ("Cannot free object 0")); */
880 netmap_obj_free(p, j);
883 D("address %p is not contained inside any cluster (%s)",
887 #define netmap_mem_bufsize(n) \
888 ((n)->pools[NETMAP_BUF_POOL]._objsize)
890 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
891 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
892 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
893 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
894 #define netmap_buf_malloc(n, _pos, _index) \
895 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
899 /* Return the index associated to the given packet buffer */
900 #define netmap_buf_index(n, v) \
901 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
905 * allocate extra buffers in a linked list.
906 * returns the actual number.
909 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
911 struct netmap_mem_d *nmd = na->nm_mem;
912 uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
916 *head = 0; /* default, 'null' index ie empty list */
917 for (i = 0 ; i < n; i++) {
918 uint32_t cur = *head; /* save current head */
919 uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
921 D("no more buffers after %d of %d", i, n);
922 *head = cur; /* restore */
925 ND(5, "allocate buffer %d -> %d", *head, cur);
926 *p = cur; /* link to previous head */
935 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
937 struct lut_entry *lut = na->na_lut.lut;
938 struct netmap_mem_d *nmd = na->nm_mem;
939 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
940 uint32_t i, cur, *buf;
942 ND("freeing the extra list");
943 for (i = 0; head >=2 && head < p->objtotal; i++) {
945 buf = lut[head].vaddr;
948 if (netmap_obj_free(p, cur))
952 D("breaking with head %d", head);
954 D("freed %d buffers", i);
958 /* Return nonzero on error */
960 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
962 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
963 u_int i = 0; /* slot counter */
964 uint32_t pos = 0; /* slot in p->bitmap */
965 uint32_t index = 0; /* buffer index */
967 for (i = 0; i < n; i++) {
968 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
970 D("no more buffers after %d of %d", i, n);
973 slot[i].buf_idx = index;
974 slot[i].len = p->_objsize;
978 ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
984 netmap_obj_free(p, slot[i].buf_idx);
986 bzero(slot, n * sizeof(slot[0]));
991 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
993 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
996 for (i = 0; i < n; i++) {
997 slot[i].buf_idx = index;
998 slot[i].len = p->_objsize;
1005 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1007 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1009 if (i < 2 || i >= p->objtotal) {
1010 D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1013 netmap_obj_free(p, i);
1018 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1022 for (i = 0; i < n; i++) {
1023 if (slot[i].buf_idx > 2)
1024 netmap_free_buf(nmd, slot[i].buf_idx);
1029 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
1035 free(p->bitmap, M_NETMAP);
1041 * Free each cluster allocated in
1042 * netmap_finalize_obj_allocator(). The cluster start
1043 * addresses are stored at multiples of p->_clusterentries
1046 for (i = 0; i < p->objtotal; i += p->_clustentries) {
1047 if (p->lut[i].vaddr)
1048 contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
1050 bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
1054 free(p->lut, M_NETMAP);
1065 * Free all resources related to an allocator.
1068 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1072 netmap_reset_obj_allocator(p);
1076 * We receive a request for objtotal objects, of size objsize each.
1077 * Internally we may round up both numbers, as we allocate objects
1078 * in small clusters multiple of the page size.
1079 * We need to keep track of objtotal and clustentries,
1080 * as they are needed when freeing memory.
1082 * XXX note -- userspace needs the buffers to be contiguous,
1083 * so we cannot afford gaps at the end of a cluster.
1087 /* call with NMA_LOCK held */
1089 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1092 u_int clustsize; /* the cluster size, multiple of page size */
1093 u_int clustentries; /* how many objects per entry */
1095 /* we store the current request, so we can
1096 * detect configuration changes later */
1097 p->r_objtotal = objtotal;
1098 p->r_objsize = objsize;
1100 #define MAX_CLUSTSIZE (1<<22) // 4 MB
1101 #define LINE_ROUND NM_CACHE_ALIGN // 64
1102 if (objsize >= MAX_CLUSTSIZE) {
1103 /* we could do it but there is no point */
1104 D("unsupported allocation for %d bytes", objsize);
1107 /* make sure objsize is a multiple of LINE_ROUND */
1108 i = (objsize & (LINE_ROUND - 1));
1110 D("XXX aligning object by %d bytes", LINE_ROUND - i);
1111 objsize += LINE_ROUND - i;
1113 if (objsize < p->objminsize || objsize > p->objmaxsize) {
1114 D("requested objsize %d out of range [%d, %d]",
1115 objsize, p->objminsize, p->objmaxsize);
1118 if (objtotal < p->nummin || objtotal > p->nummax) {
1119 D("requested objtotal %d out of range [%d, %d]",
1120 objtotal, p->nummin, p->nummax);
1124 * Compute number of objects using a brute-force approach:
1125 * given a max cluster size,
1126 * we try to fill it with objects keeping track of the
1127 * wasted space to the next page boundary.
1129 for (clustentries = 0, i = 1;; i++) {
1130 u_int delta, used = i * objsize;
1131 if (used > MAX_CLUSTSIZE)
1133 delta = used % PAGE_SIZE;
1134 if (delta == 0) { // exact solution
1139 /* exact solution not found */
1140 if (clustentries == 0) {
1141 D("unsupported allocation for %d bytes", objsize);
1144 /* compute clustsize */
1145 clustsize = clustentries * objsize;
1147 D("objsize %d clustsize %d objects %d",
1148 objsize, clustsize, clustentries);
1151 * The number of clusters is n = ceil(objtotal/clustentries)
1152 * objtotal' = n * clustentries
1154 p->_clustentries = clustentries;
1155 p->_clustsize = clustsize;
1156 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1158 /* actual values (may be larger than requested) */
1159 p->_objsize = objsize;
1160 p->_objtotal = p->_numclusters * clustentries;
1165 static struct lut_entry *
1166 nm_alloc_lut(u_int nobj)
1168 size_t n = sizeof(struct lut_entry) * nobj;
1169 struct lut_entry *lut;
1173 lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
1178 /* call with NMA_LOCK held */
1180 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
1182 int i; /* must be signed */
1185 /* optimistically assume we have enough memory */
1186 p->numclusters = p->_numclusters;
1187 p->objtotal = p->_objtotal;
1189 p->lut = nm_alloc_lut(p->objtotal);
1190 if (p->lut == NULL) {
1191 D("Unable to create lookup table for '%s'", p->name);
1195 /* Allocate the bitmap */
1196 n = (p->objtotal + 31) / 32;
1197 p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
1198 if (p->bitmap == NULL) {
1199 D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
1203 p->bitmap_slots = n;
1206 * Allocate clusters, init pointers and bitmap
1210 for (i = 0; i < (int)p->objtotal;) {
1211 int lim = i + p->_clustentries;
1215 * XXX Note, we only need contigmalloc() for buffers attached
1216 * to native interfaces. In all other cases (nifp, netmap rings
1217 * and even buffers for VALE ports or emulated interfaces) we
1218 * can live with standard malloc, because the hardware will not
1219 * access the pages directly.
1221 clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1222 (size_t)0, -1UL, PAGE_SIZE, 0);
1223 if (clust == NULL) {
1225 * If we get here, there is a severe memory shortage,
1226 * so halve the allocated memory to reclaim some.
1228 D("Unable to create cluster at %d for '%s' allocator",
1230 if (i < 2) /* nothing to halve */
1233 for (i--; i >= lim; i--) {
1234 p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) );
1235 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1236 contigfree(p->lut[i].vaddr,
1238 p->lut[i].vaddr = NULL;
1242 /* we may have stopped in the middle of a cluster */
1243 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1247 * Set bitmap and lut state for all buffers in the current
1250 * [i, lim) is the set of buffer indexes that cover the
1253 * 'clust' is really the address of the current buffer in
1254 * the current cluster as we index through it with a stride
1257 for (; i < lim; i++, clust += p->_objsize) {
1258 p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) );
1259 p->lut[i].vaddr = clust;
1260 p->lut[i].paddr = vtophys(clust);
1263 p->objfree = p->objtotal;
1264 p->memtotal = p->numclusters * p->_clustsize;
1265 if (p->objfree == 0)
1268 D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1269 p->numclusters, p->_clustsize >> 10,
1270 p->memtotal >> 10, p->name);
1275 netmap_reset_obj_allocator(p);
1279 /* call with lock held */
1281 netmap_memory_config_changed(struct netmap_mem_d *nmd)
1285 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1286 if (nmd->pools[i].r_objsize != netmap_params[i].size ||
1287 nmd->pools[i].r_objtotal != netmap_params[i].num)
1294 netmap_mem_reset_all(struct netmap_mem_d *nmd)
1299 D("resetting %p", nmd);
1300 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1301 netmap_reset_obj_allocator(&nmd->pools[i]);
1303 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1307 netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
1309 int i, lim = p->_objtotal;
1311 if (na->pdev == NULL)
1314 #if defined(__FreeBSD__)
1317 D("unsupported on FreeBSD");
1319 #elif defined(_WIN32)
1322 D("unsupported on Windows"); //XXX_ale, really?
1324 for (i = 2; i < lim; i++) {
1325 netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr);
1333 netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1335 #if defined(__FreeBSD__)
1336 D("unsupported on FreeBSD");
1337 #elif defined(_WIN32)
1338 D("unsupported on Windows"); //XXX_ale, really?
1340 int i, lim = p->_objtotal;
1342 if (na->pdev == NULL)
1345 for (i = 2; i < lim; i++) {
1346 netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr,
1355 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1358 if (nmd->flags & NETMAP_MEM_FINALIZED)
1361 nmd->nm_totalsize = 0;
1362 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1363 nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1366 nmd->nm_totalsize += nmd->pools[i].memtotal;
1368 /* buffers 0 and 1 are reserved */
1369 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1370 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1371 nmd->flags |= NETMAP_MEM_FINALIZED;
1374 D("interfaces %d KB, rings %d KB, buffers %d MB",
1375 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1376 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1377 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1380 D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1385 netmap_mem_reset_all(nmd);
1386 return nmd->lasterr;
1392 netmap_mem_private_delete(struct netmap_mem_d *nmd)
1397 D("deleting %p", nmd);
1398 if (nmd->active > 0)
1399 D("bug: deleting mem allocator with active=%d!", nmd->active);
1400 nm_mem_release_id(nmd);
1402 D("done deleting %p", nmd);
1403 NMA_LOCK_DESTROY(nmd);
1404 free(nmd, M_DEVBUF);
1408 netmap_mem_private_config(struct netmap_mem_d *nmd)
1410 /* nothing to do, we are configured on creation
1411 * and configuration never changes thereafter
1417 netmap_mem_private_finalize(struct netmap_mem_d *nmd)
1420 err = netmap_mem_finalize_all(nmd);
1428 netmap_mem_private_deref(struct netmap_mem_d *nmd)
1430 if (--nmd->active <= 0)
1431 netmap_mem_reset_all(nmd);
1436 * allocator for private memory
1438 struct netmap_mem_d *
1439 netmap_mem_private_new(const char *name, u_int txr, u_int txd,
1440 u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr)
1442 struct netmap_mem_d *d = NULL;
1443 struct netmap_obj_params p[NETMAP_POOLS_NR];
1447 d = malloc(sizeof(struct netmap_mem_d),
1448 M_DEVBUF, M_NOWAIT | M_ZERO);
1456 err = nm_mem_assign_id(d);
1460 /* account for the fake host rings */
1464 /* copy the min values */
1465 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1466 p[i] = netmap_min_priv_params[i];
1469 /* possibly increase them to fit user request */
1470 v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1471 if (p[NETMAP_IF_POOL].size < v)
1472 p[NETMAP_IF_POOL].size = v;
1474 if (p[NETMAP_IF_POOL].num < v)
1475 p[NETMAP_IF_POOL].num = v;
1476 maxd = (txd > rxd) ? txd : rxd;
1477 v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1478 if (p[NETMAP_RING_POOL].size < v)
1479 p[NETMAP_RING_POOL].size = v;
1480 /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1481 * and two rx rings (again, 1 normal and 1 fake host)
1483 v = txr + rxr + 8 * npipes;
1484 if (p[NETMAP_RING_POOL].num < v)
1485 p[NETMAP_RING_POOL].num = v;
1486 /* for each pipe we only need the buffers for the 4 "real" rings.
1487 * On the other end, the pipe ring dimension may be different from
1488 * the parent port ring dimension. As a compromise, we allocate twice the
1489 * space actually needed if the pipe rings were the same size as the parent rings
1491 v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1492 /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1493 if (p[NETMAP_BUF_POOL].num < v)
1494 p[NETMAP_BUF_POOL].num = v;
1497 D("req if %d*%d ring %d*%d buf %d*%d",
1498 p[NETMAP_IF_POOL].num,
1499 p[NETMAP_IF_POOL].size,
1500 p[NETMAP_RING_POOL].num,
1501 p[NETMAP_RING_POOL].size,
1502 p[NETMAP_BUF_POOL].num,
1503 p[NETMAP_BUF_POOL].size);
1505 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1506 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1507 nm_blueprint.pools[i].name,
1509 err = netmap_config_obj_allocator(&d->pools[i],
1510 p[i].num, p[i].size);
1515 d->flags &= ~NETMAP_MEM_FINALIZED;
1521 netmap_mem_private_delete(d);
1528 /* call with lock held */
1530 netmap_mem_global_config(struct netmap_mem_d *nmd)
1535 /* already in use, we cannot change the configuration */
1538 if (!netmap_memory_config_changed(nmd))
1541 ND("reconfiguring");
1543 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1544 /* reset previous allocation */
1545 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1546 netmap_reset_obj_allocator(&nmd->pools[i]);
1548 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1551 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1552 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1553 netmap_params[i].num, netmap_params[i].size);
1560 return nmd->lasterr;
1564 netmap_mem_global_finalize(struct netmap_mem_d *nmd)
1568 /* update configuration if changed */
1569 if (netmap_mem_global_config(nmd))
1570 return nmd->lasterr;
1574 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1575 /* may happen if config is not changed */
1576 ND("nothing to do");
1580 if (netmap_mem_finalize_all(nmd))
1595 netmap_mem_global_delete(struct netmap_mem_d *nmd)
1599 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1600 netmap_destroy_obj_allocator(&nm_mem.pools[i]);
1603 NMA_LOCK_DESTROY(&nm_mem);
1607 netmap_mem_init(void)
1609 NMA_LOCK_INIT(&nm_mem);
1610 netmap_mem_get(&nm_mem);
1615 netmap_mem_fini(void)
1617 netmap_mem_put(&nm_mem);
1621 netmap_free_rings(struct netmap_adapter *na)
1627 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
1628 struct netmap_kring *kring = &NMR(na, t)[i];
1629 struct netmap_ring *ring = kring->ring;
1631 if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) {
1632 ND("skipping ring %s (ring %p, users %d)",
1633 kring->name, ring, kring->users);
1636 if (i != nma_get_nrings(na, t) || na->na_flags & NAF_HOST_RINGS)
1637 netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1638 netmap_ring_free(na->nm_mem, ring);
1644 /* call with NMA_LOCK held *
1646 * Allocate netmap rings and buffers for this card
1647 * The rings are contiguous, but have variable size.
1648 * The kring array must follow the layout described
1649 * in netmap_krings_create().
1652 netmap_mem2_rings_create(struct netmap_adapter *na)
1656 NMA_LOCK(na->nm_mem);
1661 for (i = 0; i <= nma_get_nrings(na, t); i++) {
1662 struct netmap_kring *kring = &NMR(na, t)[i];
1663 struct netmap_ring *ring = kring->ring;
1666 if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) {
1667 /* uneeded, or already created by somebody else */
1668 ND("skipping ring %s", kring->name);
1671 ndesc = kring->nkr_num_slots;
1672 len = sizeof(struct netmap_ring) +
1673 ndesc * sizeof(struct netmap_slot);
1674 ring = netmap_ring_malloc(na->nm_mem, len);
1676 D("Cannot allocate %s_ring", nm_txrx2str(t));
1679 ND("txring at %p", ring);
1681 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1682 *(int64_t *)(uintptr_t)&ring->buf_ofs =
1683 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1684 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1685 netmap_ring_offset(na->nm_mem, ring);
1687 /* copy values from kring */
1688 ring->head = kring->rhead;
1689 ring->cur = kring->rcur;
1690 ring->tail = kring->rtail;
1691 *(uint16_t *)(uintptr_t)&ring->nr_buf_size =
1692 netmap_mem_bufsize(na->nm_mem);
1693 ND("%s h %d c %d t %d", kring->name,
1694 ring->head, ring->cur, ring->tail);
1695 ND("initializing slots for %s_ring", nm_txrx2str(txrx));
1696 if (i != nma_get_nrings(na, t) || (na->na_flags & NAF_HOST_RINGS)) {
1697 /* this is a real ring */
1698 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1699 D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1703 /* this is a fake ring, set all indices to 0 */
1704 netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1707 *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1708 *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1712 NMA_UNLOCK(na->nm_mem);
1717 netmap_free_rings(na);
1719 NMA_UNLOCK(na->nm_mem);
1725 netmap_mem2_rings_delete(struct netmap_adapter *na)
1727 /* last instance, release bufs and rings */
1728 NMA_LOCK(na->nm_mem);
1730 netmap_free_rings(na);
1732 NMA_UNLOCK(na->nm_mem);
1736 /* call with NMA_LOCK held */
1738 * Allocate the per-fd structure netmap_if.
1740 * We assume that the configuration stored in na
1741 * (number of tx/rx rings and descs) does not change while
1742 * the interface is in netmap mode.
1744 static struct netmap_if *
1745 netmap_mem2_if_new(struct netmap_adapter *na)
1747 struct netmap_if *nifp;
1748 ssize_t base; /* handy for relative offsets between rings and nifp */
1749 u_int i, len, n[NR_TXRX], ntot;
1754 /* account for the (eventually fake) host rings */
1755 n[t] = nma_get_nrings(na, t) + 1;
1759 * the descriptor is followed inline by an array of offsets
1760 * to the tx and rx rings in the shared memory region.
1763 NMA_LOCK(na->nm_mem);
1765 len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1766 nifp = netmap_if_malloc(na->nm_mem, len);
1768 NMA_UNLOCK(na->nm_mem);
1772 /* initialize base fields -- override const */
1773 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1774 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1775 strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1778 * fill the slots for the rx and tx rings. They contain the offset
1779 * between the ring and nifp, so the information is usable in
1780 * userspace to reach the ring from the nifp.
1782 base = netmap_if_offset(na->nm_mem, nifp);
1783 for (i = 0; i < n[NR_TX]; i++) {
1784 if (na->tx_rings[i].ring == NULL) {
1785 // XXX maybe use the offset of an error ring,
1786 // like we do for buffers?
1787 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = 0;
1790 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1791 netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1793 for (i = 0; i < n[NR_RX]; i++) {
1794 if (na->rx_rings[i].ring == NULL) {
1795 // XXX maybe use the offset of an error ring,
1796 // like we do for buffers?
1797 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = 0;
1800 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] =
1801 netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1804 NMA_UNLOCK(na->nm_mem);
1810 netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1815 NMA_LOCK(na->nm_mem);
1816 if (nifp->ni_bufs_head)
1817 netmap_extra_free(na, nifp->ni_bufs_head);
1818 netmap_if_free(na->nm_mem, nifp);
1820 NMA_UNLOCK(na->nm_mem);
1824 netmap_mem_global_deref(struct netmap_mem_d *nmd)
1831 D("active = %d", nmd->active);
1835 struct netmap_mem_ops netmap_mem_global_ops = {
1836 .nmd_get_lut = netmap_mem2_get_lut,
1837 .nmd_get_info = netmap_mem2_get_info,
1838 .nmd_ofstophys = netmap_mem2_ofstophys,
1839 .nmd_config = netmap_mem_global_config,
1840 .nmd_finalize = netmap_mem_global_finalize,
1841 .nmd_deref = netmap_mem_global_deref,
1842 .nmd_delete = netmap_mem_global_delete,
1843 .nmd_if_offset = netmap_mem2_if_offset,
1844 .nmd_if_new = netmap_mem2_if_new,
1845 .nmd_if_delete = netmap_mem2_if_delete,
1846 .nmd_rings_create = netmap_mem2_rings_create,
1847 .nmd_rings_delete = netmap_mem2_rings_delete
1849 struct netmap_mem_ops netmap_mem_private_ops = {
1850 .nmd_get_lut = netmap_mem2_get_lut,
1851 .nmd_get_info = netmap_mem2_get_info,
1852 .nmd_ofstophys = netmap_mem2_ofstophys,
1853 .nmd_config = netmap_mem_private_config,
1854 .nmd_finalize = netmap_mem_private_finalize,
1855 .nmd_deref = netmap_mem_private_deref,
1856 .nmd_if_offset = netmap_mem2_if_offset,
1857 .nmd_delete = netmap_mem_private_delete,
1858 .nmd_if_new = netmap_mem2_if_new,
1859 .nmd_if_delete = netmap_mem2_if_delete,
1860 .nmd_rings_create = netmap_mem2_rings_create,
1861 .nmd_rings_delete = netmap_mem2_rings_delete
1865 netmap_mem_pools_info_get(struct nmreq *nmr, struct netmap_adapter *na)
1867 uintptr_t *pp = (uintptr_t *)&nmr->nr_arg1;
1868 struct netmap_pools_info *upi = (struct netmap_pools_info *)(*pp);
1869 struct netmap_mem_d *nmd = na->nm_mem;
1870 struct netmap_pools_info pi;
1871 unsigned int memsize;
1879 ret = netmap_mem_get_info(nmd, &memsize, NULL, &memid);
1884 pi.memsize = memsize;
1886 pi.if_pool_offset = 0;
1887 pi.if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
1888 pi.if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
1890 pi.ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
1891 pi.ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
1892 pi.ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
1894 pi.buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
1895 nmd->pools[NETMAP_RING_POOL].memtotal;
1896 pi.buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
1897 pi.buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
1899 ret = copyout(&pi, upi, sizeof(pi));
1907 #ifdef WITH_PTNETMAP_GUEST
1909 struct mem_pt_if *next;
1911 unsigned int nifp_offset;
1914 /* Netmap allocator for ptnetmap guests. */
1915 struct netmap_mem_ptg {
1916 struct netmap_mem_d up;
1918 vm_paddr_t nm_paddr; /* physical address in the guest */
1919 void *nm_addr; /* virtual address in the guest */
1920 struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */
1921 nm_memid_t host_mem_id; /* allocator identifier in the host */
1922 struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
1923 struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */
1926 /* Link a passthrough interface to a passthrough netmap allocator. */
1928 netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp,
1929 unsigned int nifp_offset)
1931 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1932 struct mem_pt_if *ptif = malloc(sizeof(*ptif), M_NETMAP,
1942 ptif->nifp_offset = nifp_offset;
1944 if (ptnmd->pt_ifs) {
1945 ptif->next = ptnmd->pt_ifs;
1947 ptnmd->pt_ifs = ptif;
1951 D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset);
1956 /* Called with NMA_LOCK(nmd) held. */
1957 static struct mem_pt_if *
1958 netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp)
1960 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1961 struct mem_pt_if *curr;
1963 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
1964 if (curr->ifp == ifp) {
1972 /* Unlink a passthrough interface from a passthrough netmap allocator. */
1974 netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp)
1976 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1977 struct mem_pt_if *prev = NULL;
1978 struct mem_pt_if *curr;
1983 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
1984 if (curr->ifp == ifp) {
1986 prev->next = curr->next;
1988 ptnmd->pt_ifs = curr->next;
1990 D("removed (ifp=%p,nifp_offset=%u)",
1991 curr->ifp, curr->nifp_offset);
1992 free(curr, M_NETMAP);
2005 netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
2007 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2009 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
2013 *lut = ptnmd->buf_lut;
2018 netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, u_int *size,
2019 u_int *memflags, uint16_t *id)
2025 error = nmd->ops->nmd_config(nmd);
2030 *size = nmd->nm_totalsize;
2032 *memflags = nmd->flags;
2043 netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
2045 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2047 /* if the offset is valid, just return csb->base_addr + off */
2048 paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
2049 ND("off %lx padr %lx", off, (unsigned long)paddr);
2054 netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
2056 /* nothing to do, we are configured on creation
2057 * and configuration never changes thereafter
2063 netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd)
2065 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2077 if (nmd->flags & NETMAP_MEM_FINALIZED)
2080 if (ptnmd->ptn_dev == NULL) {
2081 D("ptnetmap memdev not attached");
2085 /* Map memory through ptnetmap-memdev BAR. */
2086 error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2087 &ptnmd->nm_addr, &mem_size);
2091 /* Initialize the lut using the information contained in the
2092 * ptnetmap memory device. */
2093 bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2094 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2095 nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2096 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2098 /* allocate the lut */
2099 if (ptnmd->buf_lut.lut == NULL) {
2100 D("allocating lut");
2101 ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2102 if (ptnmd->buf_lut.lut == NULL) {
2103 D("lut allocation failed");
2108 /* we have physically contiguous memory mapped through PCI BAR */
2109 poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2110 PTNET_MDEV_IO_BUF_POOL_OFS);
2111 vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2112 paddr = ptnmd->nm_paddr + poolofs;
2114 for (i = 0; i < nbuffers; i++) {
2115 ptnmd->buf_lut.lut[i].vaddr = vaddr;
2116 ptnmd->buf_lut.lut[i].paddr = paddr;
2121 ptnmd->buf_lut.objtotal = nbuffers;
2122 ptnmd->buf_lut.objsize = bufsize;
2123 nmd->nm_totalsize = (unsigned int)mem_size;
2125 nmd->flags |= NETMAP_MEM_FINALIZED;
2134 netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd)
2136 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2139 if (nmd->active <= 0 &&
2140 (nmd->flags & NETMAP_MEM_FINALIZED)) {
2141 nmd->flags &= ~NETMAP_MEM_FINALIZED;
2142 /* unmap ptnetmap-memdev memory */
2143 if (ptnmd->ptn_dev) {
2144 nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
2146 ptnmd->nm_addr = NULL;
2147 ptnmd->nm_paddr = 0;
2152 netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
2154 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2156 return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
2160 netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
2165 D("deleting %p", nmd);
2166 if (nmd->active > 0)
2167 D("bug: deleting mem allocator with active=%d!", nmd->active);
2168 nm_mem_release_id(nmd);
2170 D("done deleting %p", nmd);
2171 NMA_LOCK_DESTROY(nmd);
2172 free(nmd, M_DEVBUF);
2175 static struct netmap_if *
2176 netmap_mem_pt_guest_if_new(struct netmap_adapter *na)
2178 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2179 struct mem_pt_if *ptif;
2180 struct netmap_if *nifp = NULL;
2182 NMA_LOCK(na->nm_mem);
2184 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2186 D("Error: interface %p is not in passthrough", na->ifp);
2190 nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
2192 NMA_UNLOCK(na->nm_mem);
2198 netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2200 struct mem_pt_if *ptif;
2202 NMA_LOCK(na->nm_mem);
2203 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2205 D("Error: interface %p is not in passthrough", na->ifp);
2207 NMA_UNLOCK(na->nm_mem);
2211 netmap_mem_pt_guest_rings_create(struct netmap_adapter *na)
2213 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2214 struct mem_pt_if *ptif;
2215 struct netmap_if *nifp;
2218 NMA_LOCK(na->nm_mem);
2220 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2222 D("Error: interface %p is not in passthrough", na->ifp);
2227 /* point each kring to the corresponding backend ring */
2228 nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
2229 for (i = 0; i <= na->num_tx_rings; i++) {
2230 struct netmap_kring *kring = na->tx_rings + i;
2233 kring->ring = (struct netmap_ring *)
2234 ((char *)nifp + nifp->ring_ofs[i]);
2236 for (i = 0; i <= na->num_rx_rings; i++) {
2237 struct netmap_kring *kring = na->rx_rings + i;
2240 kring->ring = (struct netmap_ring *)
2242 nifp->ring_ofs[i + na->num_tx_rings + 1]);
2247 NMA_UNLOCK(na->nm_mem);
2253 netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na)
2255 /* TODO: remove?? */
2257 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2258 struct mem_pt_if *ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem,
2263 static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
2264 .nmd_get_lut = netmap_mem_pt_guest_get_lut,
2265 .nmd_get_info = netmap_mem_pt_guest_get_info,
2266 .nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
2267 .nmd_config = netmap_mem_pt_guest_config,
2268 .nmd_finalize = netmap_mem_pt_guest_finalize,
2269 .nmd_deref = netmap_mem_pt_guest_deref,
2270 .nmd_if_offset = netmap_mem_pt_guest_if_offset,
2271 .nmd_delete = netmap_mem_pt_guest_delete,
2272 .nmd_if_new = netmap_mem_pt_guest_if_new,
2273 .nmd_if_delete = netmap_mem_pt_guest_if_delete,
2274 .nmd_rings_create = netmap_mem_pt_guest_rings_create,
2275 .nmd_rings_delete = netmap_mem_pt_guest_rings_delete
2278 /* Called with NMA_LOCK(&nm_mem) held. */
2279 static struct netmap_mem_d *
2280 netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
2282 struct netmap_mem_d *mem = NULL;
2283 struct netmap_mem_d *scan = netmap_last_mem_d;
2286 /* find ptnetmap allocator through host ID */
2287 if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2288 ((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
2293 } while (scan != netmap_last_mem_d);
2298 /* Called with NMA_LOCK(&nm_mem) held. */
2299 static struct netmap_mem_d *
2300 netmap_mem_pt_guest_create(nm_memid_t mem_id)
2302 struct netmap_mem_ptg *ptnmd;
2305 ptnmd = malloc(sizeof(struct netmap_mem_ptg),
2306 M_DEVBUF, M_NOWAIT | M_ZERO);
2307 if (ptnmd == NULL) {
2312 ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2313 ptnmd->host_mem_id = mem_id;
2314 ptnmd->pt_ifs = NULL;
2316 /* Assign new id in the guest (We have the lock) */
2317 err = nm_mem_assign_id_locked(&ptnmd->up);
2321 ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
2322 ptnmd->up.flags |= NETMAP_MEM_IO;
2324 NMA_LOCK_INIT(&ptnmd->up);
2328 netmap_mem_pt_guest_delete(&ptnmd->up);
2333 * find host id in guest allocators and create guest allocator
2334 * if it is not there
2336 static struct netmap_mem_d *
2337 netmap_mem_pt_guest_get(nm_memid_t mem_id)
2339 struct netmap_mem_d *nmd;
2342 nmd = netmap_mem_pt_guest_find_memid(mem_id);
2344 nmd = netmap_mem_pt_guest_create(mem_id);
2346 NMA_UNLOCK(&nm_mem);
2352 * The guest allocator can be created by ptnetmap_memdev (during the device
2353 * attach) or by ptnetmap device (ptnet), during the netmap_attach.
2355 * The order is not important (we have different order in LINUX and FreeBSD).
2356 * The first one, creates the device, and the second one simply attaches it.
2359 /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
2361 struct netmap_mem_d *
2362 netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
2364 struct netmap_mem_d *nmd;
2365 struct netmap_mem_ptg *ptnmd;
2367 nmd = netmap_mem_pt_guest_get(mem_id);
2369 /* assign this device to the guest allocator */
2371 ptnmd = (struct netmap_mem_ptg *)nmd;
2372 ptnmd->ptn_dev = ptn_dev;
2378 /* Called when ptnet device is attaching */
2379 struct netmap_mem_d *
2380 netmap_mem_pt_guest_new(struct ifnet *ifp,
2381 unsigned int nifp_offset,
2384 struct netmap_mem_d *nmd;
2390 nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
2393 netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
2399 #endif /* WITH_PTNETMAP_GUEST */