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 {
95 struct netmap_obj_pool {
96 char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */
98 /* ---------------------------------------------------*/
99 /* these are only meaningful if the pool is finalized */
100 /* (see 'finalized' field in netmap_mem_d) */
101 u_int objtotal; /* actual total number of objects. */
102 u_int memtotal; /* actual total memory space */
103 u_int numclusters; /* actual number of clusters */
105 u_int objfree; /* number of free objects. */
107 struct lut_entry *lut; /* virt,phys addresses, objtotal entries */
108 uint32_t *bitmap; /* one bit per buffer, 1 means free */
109 uint32_t bitmap_slots; /* number of uint32 entries in bitmap */
110 /* ---------------------------------------------------*/
113 u_int objminsize; /* minimum object size */
114 u_int objmaxsize; /* maximum object size */
115 u_int nummin; /* minimum number of objects */
116 u_int nummax; /* maximum number of objects */
118 /* these are changed only by config */
119 u_int _objtotal; /* total number of objects */
120 u_int _objsize; /* object size */
121 u_int _clustsize; /* cluster size */
122 u_int _clustentries; /* objects per cluster */
123 u_int _numclusters; /* number of clusters */
125 /* requested values */
130 #define NMA_LOCK_T NM_MTX_T
133 struct netmap_mem_ops {
134 int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
135 int (*nmd_get_info)(struct netmap_mem_d *, u_int *size,
136 u_int *memflags, uint16_t *id);
138 vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
139 int (*nmd_config)(struct netmap_mem_d *);
140 int (*nmd_finalize)(struct netmap_mem_d *);
141 void (*nmd_deref)(struct netmap_mem_d *);
142 ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
143 void (*nmd_delete)(struct netmap_mem_d *);
145 struct netmap_if * (*nmd_if_new)(struct netmap_adapter *,
146 struct netmap_priv_d *);
147 void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
148 int (*nmd_rings_create)(struct netmap_adapter *);
149 void (*nmd_rings_delete)(struct netmap_adapter *);
152 struct netmap_mem_d {
153 NMA_LOCK_T nm_mtx; /* protect the allocator */
154 u_int nm_totalsize; /* shorthand */
157 #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */
158 #define NETMAP_MEM_HIDDEN 0x8 /* beeing prepared */
159 int lasterr; /* last error for curr config */
160 int active; /* active users */
162 /* the three allocators */
163 struct netmap_obj_pool pools[NETMAP_POOLS_NR];
165 nm_memid_t nm_id; /* allocator identifier */
166 int nm_grp; /* iommu groupd id */
168 /* list of all existing allocators, sorted by nm_id */
169 struct netmap_mem_d *prev, *next;
171 struct netmap_mem_ops *ops;
173 struct netmap_obj_params params[NETMAP_POOLS_NR];
175 #define NM_MEM_NAMESZ 16
176 char name[NM_MEM_NAMESZ];
180 * XXX need to fix the case of t0 == void
182 #define NMD_DEFCB(t0, name) \
184 netmap_mem_##name(struct netmap_mem_d *nmd) \
186 return nmd->ops->nmd_##name(nmd); \
189 #define NMD_DEFCB1(t0, name, t1) \
191 netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1) \
193 return nmd->ops->nmd_##name(nmd, a1); \
196 #define NMD_DEFCB3(t0, name, t1, t2, t3) \
198 netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1, t2 a2, t3 a3) \
200 return nmd->ops->nmd_##name(nmd, a1, a2, a3); \
203 #define NMD_DEFNACB(t0, name) \
205 netmap_mem_##name(struct netmap_adapter *na) \
207 return na->nm_mem->ops->nmd_##name(na); \
210 #define NMD_DEFNACB1(t0, name, t1) \
212 netmap_mem_##name(struct netmap_adapter *na, t1 a1) \
214 return na->nm_mem->ops->nmd_##name(na, a1); \
217 NMD_DEFCB1(int, get_lut, struct netmap_lut *);
218 NMD_DEFCB3(int, get_info, u_int *, u_int *, uint16_t *);
219 NMD_DEFCB1(vm_paddr_t, ofstophys, vm_ooffset_t);
220 static int netmap_mem_config(struct netmap_mem_d *);
221 NMD_DEFCB(int, config);
222 NMD_DEFCB1(ssize_t, if_offset, const void *);
223 NMD_DEFCB(void, delete);
225 NMD_DEFNACB1(struct netmap_if *, if_new, struct netmap_priv_d *);
226 NMD_DEFNACB1(void, if_delete, struct netmap_if *);
227 NMD_DEFNACB(int, rings_create);
228 NMD_DEFNACB(void, rings_delete);
230 static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
231 static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
232 static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
233 static void nm_mem_release_id(struct netmap_mem_d *);
236 netmap_mem_get_id(struct netmap_mem_d *nmd)
241 #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx)
242 #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx)
243 #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx)
244 #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx)
246 #ifdef NM_DEBUG_MEM_PUTGET
247 #define NM_DBG_REFC(nmd, func, line) \
248 nm_prinf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
250 #define NM_DBG_REFC(nmd, func, line)
253 /* circular list of all existing allocators */
254 static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
255 NM_MTX_T nm_mem_list_lock;
257 struct netmap_mem_d *
258 __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
260 NM_MTX_LOCK(nm_mem_list_lock);
262 NM_DBG_REFC(nmd, func, line);
263 NM_MTX_UNLOCK(nm_mem_list_lock);
268 __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
271 NM_MTX_LOCK(nm_mem_list_lock);
272 last = (--nmd->refcount == 0);
274 nm_mem_release_id(nmd);
275 NM_DBG_REFC(nmd, func, line);
276 NM_MTX_UNLOCK(nm_mem_list_lock);
278 netmap_mem_delete(nmd);
282 netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
284 if (nm_mem_assign_group(nmd, na->pdev) < 0) {
288 nmd->lasterr = nmd->ops->nmd_finalize(nmd);
292 if (!nmd->lasterr && na->pdev)
293 netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
299 netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
302 netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
303 if (nmd->active == 1) {
307 * Reset the allocator when it falls out of use so that any
308 * pool resources leaked by unclean application exits are
311 for (i = 0; i < NETMAP_POOLS_NR; i++) {
312 struct netmap_obj_pool *p;
316 p->objfree = p->objtotal;
318 * Reproduce the net effect of the M_ZERO malloc()
319 * and marking of free entries in the bitmap that
320 * occur in finalize_obj_allocator()
324 sizeof(uint32_t) * ((p->objtotal + 31) / 32));
327 * Set all the bits in the bitmap that have
328 * corresponding buffers to 1 to indicate they are
331 for (j = 0; j < p->objtotal; j++) {
332 if (p->lut[j].vaddr != NULL) {
333 p->bitmap[ (j>>5) ] |= ( 1 << (j & 31) );
339 * Per netmap_mem_finalize_all(),
340 * buffers 0 and 1 are reserved
342 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
343 if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
344 /* XXX This check is a workaround that prevents a
345 * NULL pointer crash which currently happens only
346 * with ptnetmap guests.
347 * Removed shared-info --> is the bug still there? */
348 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
351 nmd->ops->nmd_deref(nmd);
357 /* accessor functions */
359 netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
361 lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
362 lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
363 lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
368 static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
373 [NETMAP_RING_POOL] = {
377 [NETMAP_BUF_POOL] = {
385 * nm_mem is the memory allocator used for all physical interfaces
386 * running in netmap mode.
387 * Virtual (VALE) ports will have each its own allocator.
389 extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
390 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
394 .objminsize = sizeof(struct netmap_if),
396 .nummin = 10, /* don't be stingy */
397 .nummax = 10000, /* XXX very large */
399 [NETMAP_RING_POOL] = {
400 .name = "netmap_ring",
401 .objminsize = sizeof(struct netmap_ring),
402 .objmaxsize = 32*PAGE_SIZE,
406 [NETMAP_BUF_POOL] = {
407 .name = "netmap_buf",
411 .nummax = 1000000, /* one million! */
420 [NETMAP_RING_POOL] = {
424 [NETMAP_BUF_POOL] = {
426 .num = NETMAP_BUF_MAX_NUM,
436 .ops = &netmap_mem_global_ops,
442 /* blueprint for the private memory allocators */
443 extern struct netmap_mem_ops netmap_mem_private_ops; /* forward */
444 /* XXX clang is not happy about using name as a print format */
445 static const struct netmap_mem_d nm_blueprint = {
449 .objminsize = sizeof(struct netmap_if),
454 [NETMAP_RING_POOL] = {
456 .objminsize = sizeof(struct netmap_ring),
457 .objmaxsize = 32*PAGE_SIZE,
461 [NETMAP_BUF_POOL] = {
466 .nummax = 1000000, /* one million! */
472 .flags = NETMAP_MEM_PRIVATE,
474 .ops = &netmap_mem_global_ops,
477 /* memory allocator related sysctls */
479 #define STRINGIFY(x) #x
482 #define DECLARE_SYSCTLS(id, name) \
483 SYSBEGIN(mem2_ ## name); \
484 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
485 CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
486 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
487 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
488 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
489 CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
490 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
491 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
492 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
493 CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
494 "Default size of private netmap " STRINGIFY(name) "s"); \
495 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
496 CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
497 "Default number of private netmap " STRINGIFY(name) "s"); \
500 SYSCTL_DECL(_dev_netmap);
501 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
502 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
503 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
505 /* call with nm_mem_list_lock held */
507 nm_mem_assign_id_locked(struct netmap_mem_d *nmd)
510 struct netmap_mem_d *scan = netmap_last_mem_d;
514 /* we rely on unsigned wrap around */
515 id = scan->nm_id + 1;
516 if (id == 0) /* reserve 0 as error value */
519 if (id != scan->nm_id) {
521 nmd->prev = scan->prev;
523 scan->prev->next = nmd;
525 netmap_last_mem_d = nmd;
527 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
531 } while (scan != netmap_last_mem_d);
536 /* call with nm_mem_list_lock *not* held */
538 nm_mem_assign_id(struct netmap_mem_d *nmd)
542 NM_MTX_LOCK(nm_mem_list_lock);
543 ret = nm_mem_assign_id_locked(nmd);
544 NM_MTX_UNLOCK(nm_mem_list_lock);
549 /* call with nm_mem_list_lock held */
551 nm_mem_release_id(struct netmap_mem_d *nmd)
553 nmd->prev->next = nmd->next;
554 nmd->next->prev = nmd->prev;
556 if (netmap_last_mem_d == nmd)
557 netmap_last_mem_d = nmd->prev;
559 nmd->prev = nmd->next = NULL;
562 struct netmap_mem_d *
563 netmap_mem_find(nm_memid_t id)
565 struct netmap_mem_d *nmd;
567 NM_MTX_LOCK(nm_mem_list_lock);
568 nmd = netmap_last_mem_d;
570 if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
572 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
573 NM_MTX_UNLOCK(nm_mem_list_lock);
577 } while (nmd != netmap_last_mem_d);
578 NM_MTX_UNLOCK(nm_mem_list_lock);
583 nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
586 id = nm_iommu_group_id(dev);
588 D("iommu_group %d", id);
595 if (nmd->nm_grp != id)
596 nmd->lasterr = err = ENOMEM;
603 * First, find the allocator that contains the requested offset,
604 * then locate the cluster through a lookup table.
607 netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
610 vm_ooffset_t o = offset;
612 struct netmap_obj_pool *p;
617 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
618 if (offset >= p[i].memtotal)
620 // now lookup the cluster's address
622 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
623 offset % p[i]._objsize;
625 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
626 pa.QuadPart += offset % p[i]._objsize;
631 /* this is only in case of errors */
632 D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
633 p[NETMAP_IF_POOL].memtotal,
634 p[NETMAP_IF_POOL].memtotal
635 + p[NETMAP_RING_POOL].memtotal,
636 p[NETMAP_IF_POOL].memtotal
637 + p[NETMAP_RING_POOL].memtotal
638 + p[NETMAP_BUF_POOL].memtotal);
641 return 0; // XXX bad address
652 * win32_build_virtual_memory_for_userspace
654 * This function get all the object making part of the pools and maps
655 * a contiguous virtual memory space for the userspace
657 * 1 - allocate a Memory Descriptor List wide as the sum
658 * of the memory needed for the pools
659 * 2 - cycle all the objects in every pool and for every object do
661 * 2a - cycle all the objects in every pool, get the list
662 * of the physical address descriptors
663 * 2b - calculate the offset in the array of pages desciptor in the
665 * 2c - copy the descriptors of the object in the main MDL
667 * 3 - return the resulting MDL that needs to be mapped in userland
669 * In this way we will have an MDL that describes all the memory for the
670 * objects in a single object
674 win32_build_user_vm_map(struct netmap_mem_d* nmd)
677 u_int memsize, memflags, ofs = 0;
678 PMDL mainMdl, tempMdl;
680 if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
681 D("memory not finalised yet");
685 mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
686 if (mainMdl == NULL) {
687 D("failed to allocate mdl");
692 for (i = 0; i < NETMAP_POOLS_NR; i++) {
693 struct netmap_obj_pool *p = &nmd->pools[i];
694 int clsz = p->_clustsize;
695 int clobjs = p->_clustentries; /* objects per cluster */
696 int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
697 PPFN_NUMBER pSrc, pDst;
699 /* each pool has a different cluster size so we need to reallocate */
700 tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
701 if (tempMdl == NULL) {
703 D("fail to allocate tempMdl");
707 pSrc = MmGetMdlPfnArray(tempMdl);
708 /* create one entry per cluster, the lut[] has one entry per object */
709 for (j = 0; j < p->numclusters; j++, ofs += clsz) {
710 pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
711 MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
712 MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
713 RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
714 mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
725 * helper function for OS-specific mmap routines (currently only windows).
726 * Given an nmd and a pool index, returns the cluster size and number of clusters.
727 * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
728 * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
732 netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
734 if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
735 return 1; /* invalid arguments */
736 // NMA_LOCK_ASSERT(nmd);
737 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
738 *clustsize = *numclusters = 0;
739 return 1; /* not ready yet */
741 *clustsize = nmd->pools[pool]._clustsize;
742 *numclusters = nmd->pools[pool].numclusters;
743 return 0; /* success */
747 netmap_mem2_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags,
752 error = netmap_mem_config(nmd);
756 if (nmd->flags & NETMAP_MEM_FINALIZED) {
757 *size = nmd->nm_totalsize;
761 for (i = 0; i < NETMAP_POOLS_NR; i++) {
762 struct netmap_obj_pool *p = nmd->pools + i;
763 *size += (p->_numclusters * p->_clustsize);
768 *memflags = nmd->flags;
777 * we store objects by kernel address, need to find the offset
778 * within the pool to export the value to userspace.
779 * Algorithm: scan until we find the cluster, then add the
780 * actual offset in the cluster
783 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
785 int i, k = p->_clustentries, n = p->objtotal;
788 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
789 const char *base = p->lut[i].vaddr;
790 ssize_t relofs = (const char *) vaddr - base;
792 if (relofs < 0 || relofs >= p->_clustsize)
796 ND("%s: return offset %d (cluster %d) for pointer %p",
797 p->name, ofs, i, vaddr);
800 D("address %p is not contained inside any cluster (%s)",
802 return 0; /* An error occurred */
805 /* Helper functions which convert virtual addresses to offsets */
806 #define netmap_if_offset(n, v) \
807 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
809 #define netmap_ring_offset(n, v) \
810 ((n)->pools[NETMAP_IF_POOL].memtotal + \
811 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
814 netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
818 v = netmap_if_offset(nmd, addr);
824 * report the index, and use start position as a hint,
825 * otherwise buffer allocation becomes terribly expensive.
828 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
830 uint32_t i = 0; /* index in the bitmap */
831 uint32_t mask, j = 0; /* slot counter */
834 if (len > p->_objsize) {
835 D("%s request size %d too large", p->name, len);
836 // XXX cannot reduce the size
840 if (p->objfree == 0) {
841 D("no more %s objects", p->name);
847 /* termination is guaranteed by p->free, but better check bounds on i */
848 while (vaddr == NULL && i < p->bitmap_slots) {
849 uint32_t cur = p->bitmap[i];
850 if (cur == 0) { /* bitmask is fully used */
855 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
858 p->bitmap[i] &= ~mask; /* mark object as in use */
861 vaddr = p->lut[i * 32 + j].vaddr;
865 ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
874 * free by index, not by address.
875 * XXX should we also cleanup the content ?
878 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
882 if (j >= p->objtotal) {
883 D("invalid index %u, max %u", j, p->objtotal);
886 ptr = &p->bitmap[j / 32];
887 mask = (1 << (j % 32));
889 D("ouch, double free on buffer %d", j);
899 * free by address. This is slow but is only used for a few
900 * objects (rings, nifp)
903 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
905 u_int i, j, n = p->numclusters;
907 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
908 void *base = p->lut[i * p->_clustentries].vaddr;
909 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
911 /* Given address, is out of the scope of the current cluster.*/
912 if (vaddr < base || relofs >= p->_clustsize)
915 j = j + relofs / p->_objsize;
916 /* KASSERT(j != 0, ("Cannot free object 0")); */
917 netmap_obj_free(p, j);
920 D("address %p is not contained inside any cluster (%s)",
924 #define netmap_mem_bufsize(n) \
925 ((n)->pools[NETMAP_BUF_POOL]._objsize)
927 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
928 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
929 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
930 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
931 #define netmap_buf_malloc(n, _pos, _index) \
932 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
936 /* Return the index associated to the given packet buffer */
937 #define netmap_buf_index(n, v) \
938 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
942 * allocate extra buffers in a linked list.
943 * returns the actual number.
946 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
948 struct netmap_mem_d *nmd = na->nm_mem;
949 uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
953 *head = 0; /* default, 'null' index ie empty list */
954 for (i = 0 ; i < n; i++) {
955 uint32_t cur = *head; /* save current head */
956 uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
958 D("no more buffers after %d of %d", i, n);
959 *head = cur; /* restore */
962 ND(5, "allocate buffer %d -> %d", *head, cur);
963 *p = cur; /* link to previous head */
972 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
974 struct lut_entry *lut = na->na_lut.lut;
975 struct netmap_mem_d *nmd = na->nm_mem;
976 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
977 uint32_t i, cur, *buf;
979 ND("freeing the extra list");
980 for (i = 0; head >=2 && head < p->objtotal; i++) {
982 buf = lut[head].vaddr;
985 if (netmap_obj_free(p, cur))
989 D("breaking with head %d", head);
991 D("freed %d buffers", i);
995 /* Return nonzero on error */
997 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
999 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1000 u_int i = 0; /* slot counter */
1001 uint32_t pos = 0; /* slot in p->bitmap */
1002 uint32_t index = 0; /* buffer index */
1004 for (i = 0; i < n; i++) {
1005 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1006 if (vaddr == NULL) {
1007 D("no more buffers after %d of %d", i, n);
1010 slot[i].buf_idx = index;
1011 slot[i].len = p->_objsize;
1015 ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
1021 netmap_obj_free(p, slot[i].buf_idx);
1023 bzero(slot, n * sizeof(slot[0]));
1028 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1030 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1033 for (i = 0; i < n; i++) {
1034 slot[i].buf_idx = index;
1035 slot[i].len = p->_objsize;
1042 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1044 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1046 if (i < 2 || i >= p->objtotal) {
1047 D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1050 netmap_obj_free(p, i);
1055 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1059 for (i = 0; i < n; i++) {
1060 if (slot[i].buf_idx > 2)
1061 netmap_free_buf(nmd, slot[i].buf_idx);
1066 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
1072 nm_os_free(p->bitmap);
1078 * Free each cluster allocated in
1079 * netmap_finalize_obj_allocator(). The cluster start
1080 * addresses are stored at multiples of p->_clusterentries
1083 for (i = 0; i < p->objtotal; i += p->_clustentries) {
1084 if (p->lut[i].vaddr)
1085 contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
1087 bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
1102 * Free all resources related to an allocator.
1105 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1109 netmap_reset_obj_allocator(p);
1113 * We receive a request for objtotal objects, of size objsize each.
1114 * Internally we may round up both numbers, as we allocate objects
1115 * in small clusters multiple of the page size.
1116 * We need to keep track of objtotal and clustentries,
1117 * as they are needed when freeing memory.
1119 * XXX note -- userspace needs the buffers to be contiguous,
1120 * so we cannot afford gaps at the end of a cluster.
1124 /* call with NMA_LOCK held */
1126 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1129 u_int clustsize; /* the cluster size, multiple of page size */
1130 u_int clustentries; /* how many objects per entry */
1132 /* we store the current request, so we can
1133 * detect configuration changes later */
1134 p->r_objtotal = objtotal;
1135 p->r_objsize = objsize;
1137 #define MAX_CLUSTSIZE (1<<22) // 4 MB
1138 #define LINE_ROUND NM_CACHE_ALIGN // 64
1139 if (objsize >= MAX_CLUSTSIZE) {
1140 /* we could do it but there is no point */
1141 D("unsupported allocation for %d bytes", objsize);
1144 /* make sure objsize is a multiple of LINE_ROUND */
1145 i = (objsize & (LINE_ROUND - 1));
1147 D("XXX aligning object by %d bytes", LINE_ROUND - i);
1148 objsize += LINE_ROUND - i;
1150 if (objsize < p->objminsize || objsize > p->objmaxsize) {
1151 D("requested objsize %d out of range [%d, %d]",
1152 objsize, p->objminsize, p->objmaxsize);
1155 if (objtotal < p->nummin || objtotal > p->nummax) {
1156 D("requested objtotal %d out of range [%d, %d]",
1157 objtotal, p->nummin, p->nummax);
1161 * Compute number of objects using a brute-force approach:
1162 * given a max cluster size,
1163 * we try to fill it with objects keeping track of the
1164 * wasted space to the next page boundary.
1166 for (clustentries = 0, i = 1;; i++) {
1167 u_int delta, used = i * objsize;
1168 if (used > MAX_CLUSTSIZE)
1170 delta = used % PAGE_SIZE;
1171 if (delta == 0) { // exact solution
1176 /* exact solution not found */
1177 if (clustentries == 0) {
1178 D("unsupported allocation for %d bytes", objsize);
1181 /* compute clustsize */
1182 clustsize = clustentries * objsize;
1184 D("objsize %d clustsize %d objects %d",
1185 objsize, clustsize, clustentries);
1188 * The number of clusters is n = ceil(objtotal/clustentries)
1189 * objtotal' = n * clustentries
1191 p->_clustentries = clustentries;
1192 p->_clustsize = clustsize;
1193 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1195 /* actual values (may be larger than requested) */
1196 p->_objsize = objsize;
1197 p->_objtotal = p->_numclusters * clustentries;
1202 static struct lut_entry *
1203 nm_alloc_lut(u_int nobj)
1205 size_t n = sizeof(struct lut_entry) * nobj;
1206 struct lut_entry *lut;
1210 lut = nm_os_malloc(n);
1215 /* call with NMA_LOCK held */
1217 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
1219 int i; /* must be signed */
1222 /* optimistically assume we have enough memory */
1223 p->numclusters = p->_numclusters;
1224 p->objtotal = p->_objtotal;
1226 p->lut = nm_alloc_lut(p->objtotal);
1227 if (p->lut == NULL) {
1228 D("Unable to create lookup table for '%s'", p->name);
1232 /* Allocate the bitmap */
1233 n = (p->objtotal + 31) / 32;
1234 p->bitmap = nm_os_malloc(sizeof(uint32_t) * n);
1235 if (p->bitmap == NULL) {
1236 D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
1240 p->bitmap_slots = n;
1243 * Allocate clusters, init pointers and bitmap
1247 for (i = 0; i < (int)p->objtotal;) {
1248 int lim = i + p->_clustentries;
1252 * XXX Note, we only need contigmalloc() for buffers attached
1253 * to native interfaces. In all other cases (nifp, netmap rings
1254 * and even buffers for VALE ports or emulated interfaces) we
1255 * can live with standard malloc, because the hardware will not
1256 * access the pages directly.
1258 clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1259 (size_t)0, -1UL, PAGE_SIZE, 0);
1260 if (clust == NULL) {
1262 * If we get here, there is a severe memory shortage,
1263 * so halve the allocated memory to reclaim some.
1265 D("Unable to create cluster at %d for '%s' allocator",
1267 if (i < 2) /* nothing to halve */
1270 for (i--; i >= lim; i--) {
1271 p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) );
1272 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1273 contigfree(p->lut[i].vaddr,
1275 p->lut[i].vaddr = NULL;
1279 /* we may have stopped in the middle of a cluster */
1280 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1284 * Set bitmap and lut state for all buffers in the current
1287 * [i, lim) is the set of buffer indexes that cover the
1290 * 'clust' is really the address of the current buffer in
1291 * the current cluster as we index through it with a stride
1294 for (; i < lim; i++, clust += p->_objsize) {
1295 p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) );
1296 p->lut[i].vaddr = clust;
1297 p->lut[i].paddr = vtophys(clust);
1300 p->objfree = p->objtotal;
1301 p->memtotal = p->numclusters * p->_clustsize;
1302 if (p->objfree == 0)
1305 D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1306 p->numclusters, p->_clustsize >> 10,
1307 p->memtotal >> 10, p->name);
1312 netmap_reset_obj_allocator(p);
1316 /* call with lock held */
1318 netmap_mem_params_changed(struct netmap_obj_params* p)
1322 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1323 if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1324 p[i].last_size = p[i].size;
1325 p[i].last_num = p[i].num;
1333 netmap_mem_reset_all(struct netmap_mem_d *nmd)
1338 D("resetting %p", nmd);
1339 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1340 netmap_reset_obj_allocator(&nmd->pools[i]);
1342 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1346 netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
1348 int i, lim = p->_objtotal;
1350 if (na == NULL || na->pdev == NULL)
1353 #if defined(__FreeBSD__)
1356 D("unsupported on FreeBSD");
1358 #elif defined(_WIN32)
1361 D("unsupported on Windows"); //XXX_ale, really?
1363 for (i = 2; i < lim; i++) {
1364 netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr);
1372 netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1374 #if defined(__FreeBSD__)
1375 D("unsupported on FreeBSD");
1376 #elif defined(_WIN32)
1377 D("unsupported on Windows"); //XXX_ale, really?
1379 int i, lim = p->_objtotal;
1381 if (na->pdev == NULL)
1384 for (i = 2; i < lim; i++) {
1385 netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr,
1394 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1397 if (nmd->flags & NETMAP_MEM_FINALIZED)
1400 nmd->nm_totalsize = 0;
1401 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1402 nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1405 nmd->nm_totalsize += nmd->pools[i].memtotal;
1407 /* buffers 0 and 1 are reserved */
1408 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1409 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1410 nmd->flags |= NETMAP_MEM_FINALIZED;
1413 D("interfaces %d KB, rings %d KB, buffers %d MB",
1414 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1415 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1416 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1419 D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1424 netmap_mem_reset_all(nmd);
1425 return nmd->lasterr;
1429 * allocator for private memory
1431 static struct netmap_mem_d *
1432 _netmap_mem_private_new(struct netmap_obj_params *p, int *perr)
1434 struct netmap_mem_d *d = NULL;
1437 d = nm_os_malloc(sizeof(struct netmap_mem_d));
1445 err = nm_mem_assign_id(d);
1448 snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1450 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1451 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1452 nm_blueprint.pools[i].name,
1454 d->params[i].num = p[i].num;
1455 d->params[i].size = p[i].size;
1460 err = netmap_mem_config(d);
1464 d->flags &= ~NETMAP_MEM_FINALIZED;
1469 netmap_mem_delete(d);
1475 struct netmap_mem_d *
1476 netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1477 u_int extra_bufs, u_int npipes, int *perr)
1479 struct netmap_mem_d *d = NULL;
1480 struct netmap_obj_params p[NETMAP_POOLS_NR];
1483 /* account for the fake host rings */
1487 /* copy the min values */
1488 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1489 p[i] = netmap_min_priv_params[i];
1492 /* possibly increase them to fit user request */
1493 v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1494 if (p[NETMAP_IF_POOL].size < v)
1495 p[NETMAP_IF_POOL].size = v;
1497 if (p[NETMAP_IF_POOL].num < v)
1498 p[NETMAP_IF_POOL].num = v;
1499 maxd = (txd > rxd) ? txd : rxd;
1500 v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1501 if (p[NETMAP_RING_POOL].size < v)
1502 p[NETMAP_RING_POOL].size = v;
1503 /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1504 * and two rx rings (again, 1 normal and 1 fake host)
1506 v = txr + rxr + 8 * npipes;
1507 if (p[NETMAP_RING_POOL].num < v)
1508 p[NETMAP_RING_POOL].num = v;
1509 /* for each pipe we only need the buffers for the 4 "real" rings.
1510 * On the other end, the pipe ring dimension may be different from
1511 * the parent port ring dimension. As a compromise, we allocate twice the
1512 * space actually needed if the pipe rings were the same size as the parent rings
1514 v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1515 /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1516 if (p[NETMAP_BUF_POOL].num < v)
1517 p[NETMAP_BUF_POOL].num = v;
1520 D("req if %d*%d ring %d*%d buf %d*%d",
1521 p[NETMAP_IF_POOL].num,
1522 p[NETMAP_IF_POOL].size,
1523 p[NETMAP_RING_POOL].num,
1524 p[NETMAP_RING_POOL].size,
1525 p[NETMAP_BUF_POOL].num,
1526 p[NETMAP_BUF_POOL].size);
1528 d = _netmap_mem_private_new(p, perr);
1534 netmap_mem_delete(d);
1541 /* call with lock held */
1543 netmap_mem2_config(struct netmap_mem_d *nmd)
1548 /* already in use, we cannot change the configuration */
1551 if (!netmap_mem_params_changed(nmd->params))
1554 ND("reconfiguring");
1556 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1557 /* reset previous allocation */
1558 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1559 netmap_reset_obj_allocator(&nmd->pools[i]);
1561 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1564 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1565 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1566 nmd->params[i].num, nmd->params[i].size);
1573 return nmd->lasterr;
1577 netmap_mem2_finalize(struct netmap_mem_d *nmd)
1581 /* update configuration if changed */
1582 if (netmap_mem2_config(nmd))
1587 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1588 /* may happen if config is not changed */
1589 ND("nothing to do");
1593 if (netmap_mem_finalize_all(nmd))
1609 netmap_mem2_delete(struct netmap_mem_d *nmd)
1613 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1614 netmap_destroy_obj_allocator(&nmd->pools[i]);
1617 NMA_LOCK_DESTROY(nmd);
1623 netmap_mem_init(void)
1625 NM_MTX_INIT(nm_mem_list_lock);
1626 NMA_LOCK_INIT(&nm_mem);
1627 netmap_mem_get(&nm_mem);
1632 netmap_mem_fini(void)
1634 netmap_mem_put(&nm_mem);
1638 netmap_free_rings(struct netmap_adapter *na)
1644 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
1645 struct netmap_kring *kring = &NMR(na, t)[i];
1646 struct netmap_ring *ring = kring->ring;
1648 if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) {
1649 ND("skipping ring %s (ring %p, users %d)",
1650 kring->name, ring, kring->users);
1653 if (i != nma_get_nrings(na, t) || na->na_flags & NAF_HOST_RINGS)
1654 netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1655 netmap_ring_free(na->nm_mem, ring);
1661 /* call with NMA_LOCK held *
1663 * Allocate netmap rings and buffers for this card
1664 * The rings are contiguous, but have variable size.
1665 * The kring array must follow the layout described
1666 * in netmap_krings_create().
1669 netmap_mem2_rings_create(struct netmap_adapter *na)
1673 NMA_LOCK(na->nm_mem);
1678 for (i = 0; i <= nma_get_nrings(na, t); i++) {
1679 struct netmap_kring *kring = &NMR(na, t)[i];
1680 struct netmap_ring *ring = kring->ring;
1683 if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) {
1684 /* uneeded, or already created by somebody else */
1685 ND("skipping ring %s", kring->name);
1688 ndesc = kring->nkr_num_slots;
1689 len = sizeof(struct netmap_ring) +
1690 ndesc * sizeof(struct netmap_slot);
1691 ring = netmap_ring_malloc(na->nm_mem, len);
1693 D("Cannot allocate %s_ring", nm_txrx2str(t));
1696 ND("txring at %p", ring);
1698 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1699 *(int64_t *)(uintptr_t)&ring->buf_ofs =
1700 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1701 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1702 netmap_ring_offset(na->nm_mem, ring);
1704 /* copy values from kring */
1705 ring->head = kring->rhead;
1706 ring->cur = kring->rcur;
1707 ring->tail = kring->rtail;
1708 *(uint16_t *)(uintptr_t)&ring->nr_buf_size =
1709 netmap_mem_bufsize(na->nm_mem);
1710 ND("%s h %d c %d t %d", kring->name,
1711 ring->head, ring->cur, ring->tail);
1712 ND("initializing slots for %s_ring", nm_txrx2str(txrx));
1713 if (i != nma_get_nrings(na, t) || (na->na_flags & NAF_HOST_RINGS)) {
1714 /* this is a real ring */
1715 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1716 D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1720 /* this is a fake ring, set all indices to 0 */
1721 netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1724 *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1725 *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1729 NMA_UNLOCK(na->nm_mem);
1734 netmap_free_rings(na);
1736 NMA_UNLOCK(na->nm_mem);
1742 netmap_mem2_rings_delete(struct netmap_adapter *na)
1744 /* last instance, release bufs and rings */
1745 NMA_LOCK(na->nm_mem);
1747 netmap_free_rings(na);
1749 NMA_UNLOCK(na->nm_mem);
1753 /* call with NMA_LOCK held */
1755 * Allocate the per-fd structure netmap_if.
1757 * We assume that the configuration stored in na
1758 * (number of tx/rx rings and descs) does not change while
1759 * the interface is in netmap mode.
1761 static struct netmap_if *
1762 netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
1764 struct netmap_if *nifp;
1765 ssize_t base; /* handy for relative offsets between rings and nifp */
1766 u_int i, len, n[NR_TXRX], ntot;
1771 /* account for the (eventually fake) host rings */
1772 n[t] = nma_get_nrings(na, t) + 1;
1776 * the descriptor is followed inline by an array of offsets
1777 * to the tx and rx rings in the shared memory region.
1780 NMA_LOCK(na->nm_mem);
1782 len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1783 nifp = netmap_if_malloc(na->nm_mem, len);
1785 NMA_UNLOCK(na->nm_mem);
1789 /* initialize base fields -- override const */
1790 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1791 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1792 strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1795 * fill the slots for the rx and tx rings. They contain the offset
1796 * between the ring and nifp, so the information is usable in
1797 * userspace to reach the ring from the nifp.
1799 base = netmap_if_offset(na->nm_mem, nifp);
1800 for (i = 0; i < n[NR_TX]; i++) {
1801 /* XXX instead of ofs == 0 maybe use the offset of an error
1802 * ring, like we do for buffers? */
1805 if (na->tx_rings[i].ring != NULL && i >= priv->np_qfirst[NR_TX]
1806 && i < priv->np_qlast[NR_TX]) {
1807 ofs = netmap_ring_offset(na->nm_mem,
1808 na->tx_rings[i].ring) - base;
1810 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
1812 for (i = 0; i < n[NR_RX]; i++) {
1813 /* XXX instead of ofs == 0 maybe use the offset of an error
1814 * ring, like we do for buffers? */
1817 if (na->rx_rings[i].ring != NULL && i >= priv->np_qfirst[NR_RX]
1818 && i < priv->np_qlast[NR_RX]) {
1819 ofs = netmap_ring_offset(na->nm_mem,
1820 na->rx_rings[i].ring) - base;
1822 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
1825 NMA_UNLOCK(na->nm_mem);
1831 netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1836 NMA_LOCK(na->nm_mem);
1837 if (nifp->ni_bufs_head)
1838 netmap_extra_free(na, nifp->ni_bufs_head);
1839 netmap_if_free(na->nm_mem, nifp);
1841 NMA_UNLOCK(na->nm_mem);
1845 netmap_mem2_deref(struct netmap_mem_d *nmd)
1852 D("active = %d", nmd->active);
1856 struct netmap_mem_ops netmap_mem_global_ops = {
1857 .nmd_get_lut = netmap_mem2_get_lut,
1858 .nmd_get_info = netmap_mem2_get_info,
1859 .nmd_ofstophys = netmap_mem2_ofstophys,
1860 .nmd_config = netmap_mem2_config,
1861 .nmd_finalize = netmap_mem2_finalize,
1862 .nmd_deref = netmap_mem2_deref,
1863 .nmd_delete = netmap_mem2_delete,
1864 .nmd_if_offset = netmap_mem2_if_offset,
1865 .nmd_if_new = netmap_mem2_if_new,
1866 .nmd_if_delete = netmap_mem2_if_delete,
1867 .nmd_rings_create = netmap_mem2_rings_create,
1868 .nmd_rings_delete = netmap_mem2_rings_delete
1872 netmap_mem_pools_info_get(struct nmreq *nmr, struct netmap_mem_d *nmd)
1874 uintptr_t *pp = (uintptr_t *)&nmr->nr_arg1;
1875 struct netmap_pools_info *upi = (struct netmap_pools_info *)(*pp);
1876 struct netmap_pools_info pi;
1877 unsigned int memsize;
1881 ret = netmap_mem_get_info(nmd, &memsize, NULL, &memid);
1886 pi.memsize = memsize;
1889 pi.if_pool_offset = 0;
1890 pi.if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
1891 pi.if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
1893 pi.ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
1894 pi.ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
1895 pi.ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
1897 pi.buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
1898 nmd->pools[NETMAP_RING_POOL].memtotal;
1899 pi.buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
1900 pi.buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
1903 ret = copyout(&pi, upi, sizeof(pi));
1911 #ifdef WITH_PTNETMAP_GUEST
1913 struct mem_pt_if *next;
1915 unsigned int nifp_offset;
1918 /* Netmap allocator for ptnetmap guests. */
1919 struct netmap_mem_ptg {
1920 struct netmap_mem_d up;
1922 vm_paddr_t nm_paddr; /* physical address in the guest */
1923 void *nm_addr; /* virtual address in the guest */
1924 struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */
1925 nm_memid_t host_mem_id; /* allocator identifier in the host */
1926 struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
1927 struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */
1930 /* Link a passthrough interface to a passthrough netmap allocator. */
1932 netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp,
1933 unsigned int nifp_offset)
1935 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1936 struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
1945 ptif->nifp_offset = nifp_offset;
1947 if (ptnmd->pt_ifs) {
1948 ptif->next = ptnmd->pt_ifs;
1950 ptnmd->pt_ifs = ptif;
1954 D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset);
1959 /* Called with NMA_LOCK(nmd) held. */
1960 static struct mem_pt_if *
1961 netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp)
1963 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1964 struct mem_pt_if *curr;
1966 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
1967 if (curr->ifp == ifp) {
1975 /* Unlink a passthrough interface from a passthrough netmap allocator. */
1977 netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp)
1979 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1980 struct mem_pt_if *prev = NULL;
1981 struct mem_pt_if *curr;
1986 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
1987 if (curr->ifp == ifp) {
1989 prev->next = curr->next;
1991 ptnmd->pt_ifs = curr->next;
1993 D("removed (ifp=%p,nifp_offset=%u)",
1994 curr->ifp, curr->nifp_offset);
2008 netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
2010 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2012 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
2016 *lut = ptnmd->buf_lut;
2021 netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, u_int *size,
2022 u_int *memflags, uint16_t *id)
2028 error = nmd->ops->nmd_config(nmd);
2033 *size = nmd->nm_totalsize;
2035 *memflags = nmd->flags;
2046 netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
2048 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2050 /* if the offset is valid, just return csb->base_addr + off */
2051 paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
2052 ND("off %lx padr %lx", off, (unsigned long)paddr);
2057 netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
2059 /* nothing to do, we are configured on creation
2060 * and configuration never changes thereafter
2066 netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd)
2068 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2080 if (nmd->flags & NETMAP_MEM_FINALIZED)
2083 if (ptnmd->ptn_dev == NULL) {
2084 D("ptnetmap memdev not attached");
2088 /* Map memory through ptnetmap-memdev BAR. */
2089 error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2090 &ptnmd->nm_addr, &mem_size);
2094 /* Initialize the lut using the information contained in the
2095 * ptnetmap memory device. */
2096 bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2097 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2098 nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2099 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2101 /* allocate the lut */
2102 if (ptnmd->buf_lut.lut == NULL) {
2103 D("allocating lut");
2104 ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2105 if (ptnmd->buf_lut.lut == NULL) {
2106 D("lut allocation failed");
2111 /* we have physically contiguous memory mapped through PCI BAR */
2112 poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2113 PTNET_MDEV_IO_BUF_POOL_OFS);
2114 vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2115 paddr = ptnmd->nm_paddr + poolofs;
2117 for (i = 0; i < nbuffers; i++) {
2118 ptnmd->buf_lut.lut[i].vaddr = vaddr;
2119 ptnmd->buf_lut.lut[i].paddr = paddr;
2124 ptnmd->buf_lut.objtotal = nbuffers;
2125 ptnmd->buf_lut.objsize = bufsize;
2126 nmd->nm_totalsize = (unsigned int)mem_size;
2128 nmd->flags |= NETMAP_MEM_FINALIZED;
2137 netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd)
2139 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2142 if (nmd->active <= 0 &&
2143 (nmd->flags & NETMAP_MEM_FINALIZED)) {
2144 nmd->flags &= ~NETMAP_MEM_FINALIZED;
2145 /* unmap ptnetmap-memdev memory */
2146 if (ptnmd->ptn_dev) {
2147 nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
2149 ptnmd->nm_addr = NULL;
2150 ptnmd->nm_paddr = 0;
2155 netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
2157 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2159 return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
2163 netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
2168 D("deleting %p", nmd);
2169 if (nmd->active > 0)
2170 D("bug: deleting mem allocator with active=%d!", nmd->active);
2172 D("done deleting %p", nmd);
2173 NMA_LOCK_DESTROY(nmd);
2177 static struct netmap_if *
2178 netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
2180 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2181 struct mem_pt_if *ptif;
2182 struct netmap_if *nifp = NULL;
2184 NMA_LOCK(na->nm_mem);
2186 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2188 D("Error: interface %p is not in passthrough", na->ifp);
2192 nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
2194 NMA_UNLOCK(na->nm_mem);
2200 netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2202 struct mem_pt_if *ptif;
2204 NMA_LOCK(na->nm_mem);
2205 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2207 D("Error: interface %p is not in passthrough", na->ifp);
2209 NMA_UNLOCK(na->nm_mem);
2213 netmap_mem_pt_guest_rings_create(struct netmap_adapter *na)
2215 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2216 struct mem_pt_if *ptif;
2217 struct netmap_if *nifp;
2220 NMA_LOCK(na->nm_mem);
2222 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2224 D("Error: interface %p is not in passthrough", na->ifp);
2229 /* point each kring to the corresponding backend ring */
2230 nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
2231 for (i = 0; i <= na->num_tx_rings; i++) {
2232 struct netmap_kring *kring = na->tx_rings + i;
2235 kring->ring = (struct netmap_ring *)
2236 ((char *)nifp + nifp->ring_ofs[i]);
2238 for (i = 0; i <= na->num_rx_rings; i++) {
2239 struct netmap_kring *kring = na->rx_rings + i;
2242 kring->ring = (struct netmap_ring *)
2244 nifp->ring_ofs[i + na->num_tx_rings + 1]);
2249 NMA_UNLOCK(na->nm_mem);
2255 netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na)
2257 /* TODO: remove?? */
2259 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2260 struct mem_pt_if *ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem,
2265 static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
2266 .nmd_get_lut = netmap_mem_pt_guest_get_lut,
2267 .nmd_get_info = netmap_mem_pt_guest_get_info,
2268 .nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
2269 .nmd_config = netmap_mem_pt_guest_config,
2270 .nmd_finalize = netmap_mem_pt_guest_finalize,
2271 .nmd_deref = netmap_mem_pt_guest_deref,
2272 .nmd_if_offset = netmap_mem_pt_guest_if_offset,
2273 .nmd_delete = netmap_mem_pt_guest_delete,
2274 .nmd_if_new = netmap_mem_pt_guest_if_new,
2275 .nmd_if_delete = netmap_mem_pt_guest_if_delete,
2276 .nmd_rings_create = netmap_mem_pt_guest_rings_create,
2277 .nmd_rings_delete = netmap_mem_pt_guest_rings_delete
2280 /* Called with nm_mem_list_lock held. */
2281 static struct netmap_mem_d *
2282 netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
2284 struct netmap_mem_d *mem = NULL;
2285 struct netmap_mem_d *scan = netmap_last_mem_d;
2288 /* find ptnetmap allocator through host ID */
2289 if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2290 ((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
2293 NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
2297 } while (scan != netmap_last_mem_d);
2302 /* Called with nm_mem_list_lock held. */
2303 static struct netmap_mem_d *
2304 netmap_mem_pt_guest_create(nm_memid_t mem_id)
2306 struct netmap_mem_ptg *ptnmd;
2309 ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
2310 if (ptnmd == NULL) {
2315 ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2316 ptnmd->host_mem_id = mem_id;
2317 ptnmd->pt_ifs = NULL;
2319 /* Assign new id in the guest (We have the lock) */
2320 err = nm_mem_assign_id_locked(&ptnmd->up);
2324 ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
2325 ptnmd->up.flags |= NETMAP_MEM_IO;
2327 NMA_LOCK_INIT(&ptnmd->up);
2329 snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2334 netmap_mem_pt_guest_delete(&ptnmd->up);
2339 * find host id in guest allocators and create guest allocator
2340 * if it is not there
2342 static struct netmap_mem_d *
2343 netmap_mem_pt_guest_get(nm_memid_t mem_id)
2345 struct netmap_mem_d *nmd;
2347 NM_MTX_LOCK(nm_mem_list_lock);
2348 nmd = netmap_mem_pt_guest_find_memid(mem_id);
2350 nmd = netmap_mem_pt_guest_create(mem_id);
2352 NM_MTX_UNLOCK(nm_mem_list_lock);
2358 * The guest allocator can be created by ptnetmap_memdev (during the device
2359 * attach) or by ptnetmap device (ptnet), during the netmap_attach.
2361 * The order is not important (we have different order in LINUX and FreeBSD).
2362 * The first one, creates the device, and the second one simply attaches it.
2365 /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
2367 struct netmap_mem_d *
2368 netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
2370 struct netmap_mem_d *nmd;
2371 struct netmap_mem_ptg *ptnmd;
2373 nmd = netmap_mem_pt_guest_get(mem_id);
2375 /* assign this device to the guest allocator */
2377 ptnmd = (struct netmap_mem_ptg *)nmd;
2378 ptnmd->ptn_dev = ptn_dev;
2384 /* Called when ptnet device is attaching */
2385 struct netmap_mem_d *
2386 netmap_mem_pt_guest_new(struct ifnet *ifp,
2387 unsigned int nifp_offset,
2390 struct netmap_mem_d *nmd;
2396 nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
2399 netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
2405 #endif /* WITH_PTNETMAP_GUEST */