2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012-2014 Matteo Landi
5 * Copyright (C) 2012-2016 Luigi Rizzo
6 * Copyright (C) 2012-2016 Giuseppe Lettieri
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #endif /* __APPLE__ */
40 #include <sys/cdefs.h> /* prerequisite */
41 __FBSDID("$FreeBSD$");
43 #include <sys/types.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h> /* MALLOC_DEFINE */
47 #include <vm/vm.h> /* vtophys */
48 #include <vm/pmap.h> /* vtophys */
49 #include <sys/socket.h> /* sockaddrs */
50 #include <sys/selinfo.h>
51 #include <sys/sysctl.h>
53 #include <net/if_var.h>
55 #include <machine/bus.h> /* bus_dmamap_* */
57 /* M_NETMAP only used in here */
58 MALLOC_DECLARE(M_NETMAP);
59 MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
61 #endif /* __FreeBSD__ */
67 #include <net/netmap.h>
68 #include <dev/netmap/netmap_kern.h>
69 #include <net/netmap_virt.h>
70 #include "netmap_mem2.h"
72 #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY
73 #define NETMAP_BUF_MAX_NUM 8*4096 /* if too big takes too much time to allocate */
75 #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */
78 #define NETMAP_POOL_MAX_NAMSZ 32
89 struct netmap_obj_params {
97 struct netmap_obj_pool {
98 char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */
100 /* ---------------------------------------------------*/
101 /* these are only meaningful if the pool is finalized */
102 /* (see 'finalized' field in netmap_mem_d) */
103 u_int objtotal; /* actual total number of objects. */
104 u_int memtotal; /* actual total memory space */
105 u_int numclusters; /* actual number of clusters */
107 u_int objfree; /* number of free objects. */
109 struct lut_entry *lut; /* virt,phys addresses, objtotal entries */
110 uint32_t *bitmap; /* one bit per buffer, 1 means free */
111 uint32_t *invalid_bitmap;/* one bit per buffer, 1 means invalid */
112 uint32_t bitmap_slots; /* number of uint32 entries in bitmap */
113 int alloc_done; /* we have allocated the memory */
114 /* ---------------------------------------------------*/
117 u_int objminsize; /* minimum object size */
118 u_int objmaxsize; /* maximum object size */
119 u_int nummin; /* minimum number of objects */
120 u_int nummax; /* maximum number of objects */
122 /* these are changed only by config */
123 u_int _objtotal; /* total number of objects */
124 u_int _objsize; /* object size */
125 u_int _clustsize; /* cluster size */
126 u_int _clustentries; /* objects per cluster */
127 u_int _numclusters; /* number of clusters */
129 /* requested values */
134 #define NMA_LOCK_T NM_MTX_T
135 #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx)
136 #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx)
137 #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx)
138 #define NMA_SPINLOCK(n) NM_MTX_SPINLOCK((n)->nm_mtx)
139 #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx)
141 struct netmap_mem_ops {
142 int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
143 int (*nmd_get_info)(struct netmap_mem_d *, uint64_t *size,
144 u_int *memflags, uint16_t *id);
146 vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
147 int (*nmd_config)(struct netmap_mem_d *);
148 int (*nmd_finalize)(struct netmap_mem_d *);
149 void (*nmd_deref)(struct netmap_mem_d *);
150 ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
151 void (*nmd_delete)(struct netmap_mem_d *);
153 struct netmap_if * (*nmd_if_new)(struct netmap_adapter *,
154 struct netmap_priv_d *);
155 void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
156 int (*nmd_rings_create)(struct netmap_adapter *);
157 void (*nmd_rings_delete)(struct netmap_adapter *);
160 struct netmap_mem_d {
161 NMA_LOCK_T nm_mtx; /* protect the allocator */
162 u_int nm_totalsize; /* shorthand */
165 #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */
166 #define NETMAP_MEM_HIDDEN 0x8 /* beeing prepared */
167 int lasterr; /* last error for curr config */
168 int active; /* active users */
170 /* the three allocators */
171 struct netmap_obj_pool pools[NETMAP_POOLS_NR];
173 nm_memid_t nm_id; /* allocator identifier */
174 int nm_grp; /* iommu groupd id */
176 /* list of all existing allocators, sorted by nm_id */
177 struct netmap_mem_d *prev, *next;
179 struct netmap_mem_ops *ops;
181 struct netmap_obj_params params[NETMAP_POOLS_NR];
183 #define NM_MEM_NAMESZ 16
184 char name[NM_MEM_NAMESZ];
188 netmap_mem_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
193 rv = nmd->ops->nmd_get_lut(nmd, lut);
200 netmap_mem_get_info(struct netmap_mem_d *nmd, uint64_t *size,
201 u_int *memflags, nm_memid_t *memid)
206 rv = nmd->ops->nmd_get_info(nmd, size, memflags, memid);
213 netmap_mem_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
217 #if defined(__FreeBSD__)
218 /* This function is called by netmap_dev_pager_fault(), which holds a
219 * non-sleepable lock since FreeBSD 12. Since we cannot sleep, we
220 * spin on the trylock. */
225 pa = nmd->ops->nmd_ofstophys(nmd, off);
232 netmap_mem_config(struct netmap_mem_d *nmd)
235 /* already in use. Not fatal, but we
236 * cannot change the configuration
241 return nmd->ops->nmd_config(nmd);
245 netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *off)
250 rv = nmd->ops->nmd_if_offset(nmd, off);
257 netmap_mem_delete(struct netmap_mem_d *nmd)
259 nmd->ops->nmd_delete(nmd);
263 netmap_mem_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
265 struct netmap_if *nifp;
266 struct netmap_mem_d *nmd = na->nm_mem;
269 nifp = nmd->ops->nmd_if_new(na, priv);
276 netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nif)
278 struct netmap_mem_d *nmd = na->nm_mem;
281 nmd->ops->nmd_if_delete(na, nif);
286 netmap_mem_rings_create(struct netmap_adapter *na)
289 struct netmap_mem_d *nmd = na->nm_mem;
292 rv = nmd->ops->nmd_rings_create(na);
299 netmap_mem_rings_delete(struct netmap_adapter *na)
301 struct netmap_mem_d *nmd = na->nm_mem;
304 nmd->ops->nmd_rings_delete(na);
308 static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
309 static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
310 static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
311 static void nm_mem_release_id(struct netmap_mem_d *);
314 netmap_mem_get_id(struct netmap_mem_d *nmd)
319 #ifdef NM_DEBUG_MEM_PUTGET
320 #define NM_DBG_REFC(nmd, func, line) \
321 nm_prinf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
323 #define NM_DBG_REFC(nmd, func, line)
326 /* circular list of all existing allocators */
327 static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
328 NM_MTX_T nm_mem_list_lock;
330 struct netmap_mem_d *
331 __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
333 NM_MTX_LOCK(nm_mem_list_lock);
335 NM_DBG_REFC(nmd, func, line);
336 NM_MTX_UNLOCK(nm_mem_list_lock);
341 __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
344 NM_MTX_LOCK(nm_mem_list_lock);
345 last = (--nmd->refcount == 0);
347 nm_mem_release_id(nmd);
348 NM_DBG_REFC(nmd, func, line);
349 NM_MTX_UNLOCK(nm_mem_list_lock);
351 netmap_mem_delete(nmd);
355 netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
358 if (nm_mem_assign_group(nmd, na->pdev) < 0) {
364 if (netmap_mem_config(nmd))
369 nmd->lasterr = nmd->ops->nmd_finalize(nmd);
371 if (!nmd->lasterr && na->pdev) {
372 nmd->lasterr = netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
376 lasterr = nmd->lasterr;
380 netmap_mem_deref(nmd, na);
386 nm_isset(uint32_t *bitmap, u_int i)
388 return bitmap[ (i>>5) ] & ( 1U << (i & 31U) );
393 netmap_init_obj_allocator_bitmap(struct netmap_obj_pool *p)
397 if (p->bitmap == NULL) {
398 /* Allocate the bitmap */
399 n = (p->objtotal + 31) / 32;
400 p->bitmap = nm_os_malloc(sizeof(uint32_t) * n);
401 if (p->bitmap == NULL) {
402 D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
408 memset(p->bitmap, 0, p->bitmap_slots);
413 * Set all the bits in the bitmap that have
414 * corresponding buffers to 1 to indicate they are
417 for (j = 0; j < p->objtotal; j++) {
418 if (p->invalid_bitmap && nm_isset(p->invalid_bitmap, j)) {
419 D("skipping %s %d", p->name, j);
422 p->bitmap[ (j>>5) ] |= ( 1U << (j & 31U) );
426 ND("%s free %u", p->name, p->objfree);
434 netmap_mem_init_bitmaps(struct netmap_mem_d *nmd)
438 for (i = 0; i < NETMAP_POOLS_NR; i++) {
439 struct netmap_obj_pool *p = &nmd->pools[i];
441 error = netmap_init_obj_allocator_bitmap(p);
447 * buffers 0 and 1 are reserved
449 if (nmd->pools[NETMAP_BUF_POOL].objfree < 2) {
453 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
454 if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
455 /* XXX This check is a workaround that prevents a
456 * NULL pointer crash which currently happens only
457 * with ptnetmap guests.
458 * Removed shared-info --> is the bug still there? */
459 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3U;
465 netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
469 if (na->active_fds <= 0)
470 netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
471 if (nmd->active == 1) {
474 * Reset the allocator when it falls out of use so that any
475 * pool resources leaked by unclean application exits are
478 netmap_mem_init_bitmaps(nmd);
480 nmd->ops->nmd_deref(nmd);
491 /* accessor functions */
493 netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
495 lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
497 lut->plut = lut->lut;
499 lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
500 lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
505 static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
510 [NETMAP_RING_POOL] = {
514 [NETMAP_BUF_POOL] = {
522 * nm_mem is the memory allocator used for all physical interfaces
523 * running in netmap mode.
524 * Virtual (VALE) ports will have each its own allocator.
526 extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
527 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
531 .objminsize = sizeof(struct netmap_if),
533 .nummin = 10, /* don't be stingy */
534 .nummax = 10000, /* XXX very large */
536 [NETMAP_RING_POOL] = {
537 .name = "netmap_ring",
538 .objminsize = sizeof(struct netmap_ring),
539 .objmaxsize = 32*PAGE_SIZE,
543 [NETMAP_BUF_POOL] = {
544 .name = "netmap_buf",
548 .nummax = 1000000, /* one million! */
557 [NETMAP_RING_POOL] = {
561 [NETMAP_BUF_POOL] = {
563 .num = NETMAP_BUF_MAX_NUM,
573 .ops = &netmap_mem_global_ops,
579 /* blueprint for the private memory allocators */
580 /* XXX clang is not happy about using name as a print format */
581 static const struct netmap_mem_d nm_blueprint = {
585 .objminsize = sizeof(struct netmap_if),
590 [NETMAP_RING_POOL] = {
592 .objminsize = sizeof(struct netmap_ring),
593 .objmaxsize = 32*PAGE_SIZE,
597 [NETMAP_BUF_POOL] = {
602 .nummax = 1000000, /* one million! */
608 .flags = NETMAP_MEM_PRIVATE,
610 .ops = &netmap_mem_global_ops,
613 /* memory allocator related sysctls */
615 #define STRINGIFY(x) #x
618 #define DECLARE_SYSCTLS(id, name) \
619 SYSBEGIN(mem2_ ## name); \
620 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
621 CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
622 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
623 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
624 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
625 CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
626 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
627 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
628 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
629 CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
630 "Default size of private netmap " STRINGIFY(name) "s"); \
631 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
632 CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
633 "Default number of private netmap " STRINGIFY(name) "s"); \
636 SYSCTL_DECL(_dev_netmap);
637 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
638 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
639 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
641 /* call with nm_mem_list_lock held */
643 nm_mem_assign_id_locked(struct netmap_mem_d *nmd)
646 struct netmap_mem_d *scan = netmap_last_mem_d;
650 /* we rely on unsigned wrap around */
651 id = scan->nm_id + 1;
652 if (id == 0) /* reserve 0 as error value */
655 if (id != scan->nm_id) {
657 nmd->prev = scan->prev;
659 scan->prev->next = nmd;
661 netmap_last_mem_d = nmd;
663 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
667 } while (scan != netmap_last_mem_d);
672 /* call with nm_mem_list_lock *not* held */
674 nm_mem_assign_id(struct netmap_mem_d *nmd)
678 NM_MTX_LOCK(nm_mem_list_lock);
679 ret = nm_mem_assign_id_locked(nmd);
680 NM_MTX_UNLOCK(nm_mem_list_lock);
685 /* call with nm_mem_list_lock held */
687 nm_mem_release_id(struct netmap_mem_d *nmd)
689 nmd->prev->next = nmd->next;
690 nmd->next->prev = nmd->prev;
692 if (netmap_last_mem_d == nmd)
693 netmap_last_mem_d = nmd->prev;
695 nmd->prev = nmd->next = NULL;
698 struct netmap_mem_d *
699 netmap_mem_find(nm_memid_t id)
701 struct netmap_mem_d *nmd;
703 NM_MTX_LOCK(nm_mem_list_lock);
704 nmd = netmap_last_mem_d;
706 if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
708 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
709 NM_MTX_UNLOCK(nm_mem_list_lock);
713 } while (nmd != netmap_last_mem_d);
714 NM_MTX_UNLOCK(nm_mem_list_lock);
719 nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
722 id = nm_iommu_group_id(dev);
724 D("iommu_group %d", id);
731 if (nmd->nm_grp != id)
732 nmd->lasterr = err = ENOMEM;
738 static struct lut_entry *
739 nm_alloc_lut(u_int nobj)
741 size_t n = sizeof(struct lut_entry) * nobj;
742 struct lut_entry *lut;
746 lut = nm_os_malloc(n);
752 nm_free_lut(struct lut_entry *lut, u_int objtotal)
754 bzero(lut, sizeof(struct lut_entry) * objtotal);
762 #if defined(linux) || defined(_WIN32)
763 static struct plut_entry *
764 nm_alloc_plut(u_int nobj)
766 size_t n = sizeof(struct plut_entry) * nobj;
767 struct plut_entry *lut;
773 nm_free_plut(struct plut_entry * lut)
777 #endif /* linux or _WIN32 */
781 * First, find the allocator that contains the requested offset,
782 * then locate the cluster through a lookup table.
785 netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
788 vm_ooffset_t o = offset;
790 struct netmap_obj_pool *p;
794 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
795 if (offset >= p[i].memtotal)
797 // now lookup the cluster's address
799 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
800 offset % p[i]._objsize;
802 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
803 pa.QuadPart += offset % p[i]._objsize;
807 /* this is only in case of errors */
808 D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
809 p[NETMAP_IF_POOL].memtotal,
810 p[NETMAP_IF_POOL].memtotal
811 + p[NETMAP_RING_POOL].memtotal,
812 p[NETMAP_IF_POOL].memtotal
813 + p[NETMAP_RING_POOL].memtotal
814 + p[NETMAP_BUF_POOL].memtotal);
816 return 0; /* bad address */
827 * win32_build_virtual_memory_for_userspace
829 * This function get all the object making part of the pools and maps
830 * a contiguous virtual memory space for the userspace
832 * 1 - allocate a Memory Descriptor List wide as the sum
833 * of the memory needed for the pools
834 * 2 - cycle all the objects in every pool and for every object do
836 * 2a - cycle all the objects in every pool, get the list
837 * of the physical address descriptors
838 * 2b - calculate the offset in the array of pages desciptor in the
840 * 2c - copy the descriptors of the object in the main MDL
842 * 3 - return the resulting MDL that needs to be mapped in userland
844 * In this way we will have an MDL that describes all the memory for the
845 * objects in a single object
849 win32_build_user_vm_map(struct netmap_mem_d* nmd)
851 u_int memflags, ofs = 0;
852 PMDL mainMdl, tempMdl;
856 if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
857 D("memory not finalised yet");
861 mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
862 if (mainMdl == NULL) {
863 D("failed to allocate mdl");
868 for (i = 0; i < NETMAP_POOLS_NR; i++) {
869 struct netmap_obj_pool *p = &nmd->pools[i];
870 int clsz = p->_clustsize;
871 int clobjs = p->_clustentries; /* objects per cluster */
872 int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
873 PPFN_NUMBER pSrc, pDst;
875 /* each pool has a different cluster size so we need to reallocate */
876 tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
877 if (tempMdl == NULL) {
879 D("fail to allocate tempMdl");
883 pSrc = MmGetMdlPfnArray(tempMdl);
884 /* create one entry per cluster, the lut[] has one entry per object */
885 for (j = 0; j < p->numclusters; j++, ofs += clsz) {
886 pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
887 MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
888 MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
889 RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
890 mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
901 * helper function for OS-specific mmap routines (currently only windows).
902 * Given an nmd and a pool index, returns the cluster size and number of clusters.
903 * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
904 * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
908 netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
910 if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
911 return 1; /* invalid arguments */
912 // NMA_LOCK_ASSERT(nmd);
913 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
914 *clustsize = *numclusters = 0;
915 return 1; /* not ready yet */
917 *clustsize = nmd->pools[pool]._clustsize;
918 *numclusters = nmd->pools[pool].numclusters;
919 return 0; /* success */
923 netmap_mem2_get_info(struct netmap_mem_d* nmd, uint64_t* size,
924 u_int *memflags, nm_memid_t *id)
927 error = netmap_mem_config(nmd);
931 if (nmd->flags & NETMAP_MEM_FINALIZED) {
932 *size = nmd->nm_totalsize;
936 for (i = 0; i < NETMAP_POOLS_NR; i++) {
937 struct netmap_obj_pool *p = nmd->pools + i;
938 *size += (p->_numclusters * p->_clustsize);
943 *memflags = nmd->flags;
951 * we store objects by kernel address, need to find the offset
952 * within the pool to export the value to userspace.
953 * Algorithm: scan until we find the cluster, then add the
954 * actual offset in the cluster
957 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
959 int i, k = p->_clustentries, n = p->objtotal;
962 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
963 const char *base = p->lut[i].vaddr;
964 ssize_t relofs = (const char *) vaddr - base;
966 if (relofs < 0 || relofs >= p->_clustsize)
970 ND("%s: return offset %d (cluster %d) for pointer %p",
971 p->name, ofs, i, vaddr);
974 D("address %p is not contained inside any cluster (%s)",
976 return 0; /* An error occurred */
979 /* Helper functions which convert virtual addresses to offsets */
980 #define netmap_if_offset(n, v) \
981 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
983 #define netmap_ring_offset(n, v) \
984 ((n)->pools[NETMAP_IF_POOL].memtotal + \
985 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
988 netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
990 return netmap_if_offset(nmd, addr);
994 * report the index, and use start position as a hint,
995 * otherwise buffer allocation becomes terribly expensive.
998 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
1000 uint32_t i = 0; /* index in the bitmap */
1001 uint32_t mask, j = 0; /* slot counter */
1004 if (len > p->_objsize) {
1005 D("%s request size %d too large", p->name, len);
1009 if (p->objfree == 0) {
1010 D("no more %s objects", p->name);
1016 /* termination is guaranteed by p->free, but better check bounds on i */
1017 while (vaddr == NULL && i < p->bitmap_slots) {
1018 uint32_t cur = p->bitmap[i];
1019 if (cur == 0) { /* bitmask is fully used */
1024 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
1027 p->bitmap[i] &= ~mask; /* mark object as in use */
1030 vaddr = p->lut[i * 32 + j].vaddr;
1032 *index = i * 32 + j;
1034 ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
1043 * free by index, not by address.
1044 * XXX should we also cleanup the content ?
1047 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
1049 uint32_t *ptr, mask;
1051 if (j >= p->objtotal) {
1052 D("invalid index %u, max %u", j, p->objtotal);
1055 ptr = &p->bitmap[j / 32];
1056 mask = (1 << (j % 32));
1058 D("ouch, double free on buffer %d", j);
1068 * free by address. This is slow but is only used for a few
1069 * objects (rings, nifp)
1072 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
1074 u_int i, j, n = p->numclusters;
1076 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
1077 void *base = p->lut[i * p->_clustentries].vaddr;
1078 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
1080 /* Given address, is out of the scope of the current cluster.*/
1081 if (base == NULL || vaddr < base || relofs >= p->_clustsize)
1084 j = j + relofs / p->_objsize;
1085 /* KASSERT(j != 0, ("Cannot free object 0")); */
1086 netmap_obj_free(p, j);
1089 D("address %p is not contained inside any cluster (%s)",
1094 netmap_mem_bufsize(struct netmap_mem_d *nmd)
1096 return nmd->pools[NETMAP_BUF_POOL]._objsize;
1099 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
1100 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
1101 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
1102 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
1103 #define netmap_buf_malloc(n, _pos, _index) \
1104 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
1107 #if 0 /* currently unused */
1108 /* Return the index associated to the given packet buffer */
1109 #define netmap_buf_index(n, v) \
1110 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
1114 * allocate extra buffers in a linked list.
1115 * returns the actual number.
1118 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
1120 struct netmap_mem_d *nmd = na->nm_mem;
1121 uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
1125 *head = 0; /* default, 'null' index ie empty list */
1126 for (i = 0 ; i < n; i++) {
1127 uint32_t cur = *head; /* save current head */
1128 uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
1130 D("no more buffers after %d of %d", i, n);
1131 *head = cur; /* restore */
1134 ND(5, "allocate buffer %d -> %d", *head, cur);
1135 *p = cur; /* link to previous head */
1144 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
1146 struct lut_entry *lut = na->na_lut.lut;
1147 struct netmap_mem_d *nmd = na->nm_mem;
1148 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1149 uint32_t i, cur, *buf;
1151 ND("freeing the extra list");
1152 for (i = 0; head >=2 && head < p->objtotal; i++) {
1154 buf = lut[head].vaddr;
1157 if (netmap_obj_free(p, cur))
1161 D("breaking with head %d", head);
1163 D("freed %d buffers", i);
1167 /* Return nonzero on error */
1169 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1171 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1172 u_int i = 0; /* slot counter */
1173 uint32_t pos = 0; /* slot in p->bitmap */
1174 uint32_t index = 0; /* buffer index */
1176 for (i = 0; i < n; i++) {
1177 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1178 if (vaddr == NULL) {
1179 D("no more buffers after %d of %d", i, n);
1182 slot[i].buf_idx = index;
1183 slot[i].len = p->_objsize;
1188 ND("%s: allocated %d buffers, %d available, first at %d", p->name, n, p->objfree, pos);
1194 netmap_obj_free(p, slot[i].buf_idx);
1196 bzero(slot, n * sizeof(slot[0]));
1201 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1203 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1206 for (i = 0; i < n; i++) {
1207 slot[i].buf_idx = index;
1208 slot[i].len = p->_objsize;
1215 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1217 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1219 if (i < 2 || i >= p->objtotal) {
1220 D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1223 netmap_obj_free(p, i);
1228 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1232 for (i = 0; i < n; i++) {
1233 if (slot[i].buf_idx > 1)
1234 netmap_free_buf(nmd, slot[i].buf_idx);
1236 ND("%s: released some buffers, available: %u",
1237 p->name, p->objfree);
1241 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
1247 nm_os_free(p->bitmap);
1249 if (p->invalid_bitmap)
1250 nm_os_free(p->invalid_bitmap);
1251 p->invalid_bitmap = NULL;
1252 if (!p->alloc_done) {
1253 /* allocation was done by somebody else.
1254 * Let them clean up after themselves.
1262 * Free each cluster allocated in
1263 * netmap_finalize_obj_allocator(). The cluster start
1264 * addresses are stored at multiples of p->_clusterentries
1267 for (i = 0; i < p->objtotal; i += p->_clustentries) {
1268 contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
1270 nm_free_lut(p->lut, p->objtotal);
1281 * Free all resources related to an allocator.
1284 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1288 netmap_reset_obj_allocator(p);
1292 * We receive a request for objtotal objects, of size objsize each.
1293 * Internally we may round up both numbers, as we allocate objects
1294 * in small clusters multiple of the page size.
1295 * We need to keep track of objtotal and clustentries,
1296 * as they are needed when freeing memory.
1298 * XXX note -- userspace needs the buffers to be contiguous,
1299 * so we cannot afford gaps at the end of a cluster.
1303 /* call with NMA_LOCK held */
1305 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1308 u_int clustsize; /* the cluster size, multiple of page size */
1309 u_int clustentries; /* how many objects per entry */
1311 /* we store the current request, so we can
1312 * detect configuration changes later */
1313 p->r_objtotal = objtotal;
1314 p->r_objsize = objsize;
1316 #define MAX_CLUSTSIZE (1<<22) // 4 MB
1317 #define LINE_ROUND NM_CACHE_ALIGN // 64
1318 if (objsize >= MAX_CLUSTSIZE) {
1319 /* we could do it but there is no point */
1320 D("unsupported allocation for %d bytes", objsize);
1323 /* make sure objsize is a multiple of LINE_ROUND */
1324 i = (objsize & (LINE_ROUND - 1));
1326 D("XXX aligning object by %d bytes", LINE_ROUND - i);
1327 objsize += LINE_ROUND - i;
1329 if (objsize < p->objminsize || objsize > p->objmaxsize) {
1330 D("requested objsize %d out of range [%d, %d]",
1331 objsize, p->objminsize, p->objmaxsize);
1334 if (objtotal < p->nummin || objtotal > p->nummax) {
1335 D("requested objtotal %d out of range [%d, %d]",
1336 objtotal, p->nummin, p->nummax);
1340 * Compute number of objects using a brute-force approach:
1341 * given a max cluster size,
1342 * we try to fill it with objects keeping track of the
1343 * wasted space to the next page boundary.
1345 for (clustentries = 0, i = 1;; i++) {
1346 u_int delta, used = i * objsize;
1347 if (used > MAX_CLUSTSIZE)
1349 delta = used % PAGE_SIZE;
1350 if (delta == 0) { // exact solution
1355 /* exact solution not found */
1356 if (clustentries == 0) {
1357 D("unsupported allocation for %d bytes", objsize);
1360 /* compute clustsize */
1361 clustsize = clustentries * objsize;
1363 D("objsize %d clustsize %d objects %d",
1364 objsize, clustsize, clustentries);
1367 * The number of clusters is n = ceil(objtotal/clustentries)
1368 * objtotal' = n * clustentries
1370 p->_clustentries = clustentries;
1371 p->_clustsize = clustsize;
1372 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1374 /* actual values (may be larger than requested) */
1375 p->_objsize = objsize;
1376 p->_objtotal = p->_numclusters * clustentries;
1381 /* call with NMA_LOCK held */
1383 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
1385 int i; /* must be signed */
1389 /* if the lut is already there we assume that also all the
1390 * clusters have already been allocated, possibily by somebody
1391 * else (e.g., extmem). In the latter case, the alloc_done flag
1392 * will remain at zero, so that we will not attempt to
1393 * deallocate the clusters by ourselves in
1394 * netmap_reset_obj_allocator.
1399 /* optimistically assume we have enough memory */
1400 p->numclusters = p->_numclusters;
1401 p->objtotal = p->_objtotal;
1404 p->lut = nm_alloc_lut(p->objtotal);
1405 if (p->lut == NULL) {
1406 D("Unable to create lookup table for '%s'", p->name);
1411 * Allocate clusters, init pointers
1415 for (i = 0; i < (int)p->objtotal;) {
1416 int lim = i + p->_clustentries;
1420 * XXX Note, we only need contigmalloc() for buffers attached
1421 * to native interfaces. In all other cases (nifp, netmap rings
1422 * and even buffers for VALE ports or emulated interfaces) we
1423 * can live with standard malloc, because the hardware will not
1424 * access the pages directly.
1426 clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1427 (size_t)0, -1UL, PAGE_SIZE, 0);
1428 if (clust == NULL) {
1430 * If we get here, there is a severe memory shortage,
1431 * so halve the allocated memory to reclaim some.
1433 D("Unable to create cluster at %d for '%s' allocator",
1435 if (i < 2) /* nothing to halve */
1438 for (i--; i >= lim; i--) {
1439 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1440 contigfree(p->lut[i].vaddr,
1442 p->lut[i].vaddr = NULL;
1446 /* we may have stopped in the middle of a cluster */
1447 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1451 * Set lut state for all buffers in the current cluster.
1453 * [i, lim) is the set of buffer indexes that cover the
1456 * 'clust' is really the address of the current buffer in
1457 * the current cluster as we index through it with a stride
1460 for (; i < lim; i++, clust += p->_objsize) {
1461 p->lut[i].vaddr = clust;
1462 #if !defined(linux) && !defined(_WIN32)
1463 p->lut[i].paddr = vtophys(clust);
1467 p->memtotal = p->numclusters * p->_clustsize;
1469 D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1470 p->numclusters, p->_clustsize >> 10,
1471 p->memtotal >> 10, p->name);
1476 netmap_reset_obj_allocator(p);
1480 /* call with lock held */
1482 netmap_mem_params_changed(struct netmap_obj_params* p)
1486 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1487 if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1488 p[i].last_size = p[i].size;
1489 p[i].last_num = p[i].num;
1497 netmap_mem_reset_all(struct netmap_mem_d *nmd)
1502 D("resetting %p", nmd);
1503 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1504 netmap_reset_obj_allocator(&nmd->pools[i]);
1506 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1510 netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
1512 int i, lim = p->objtotal;
1513 struct netmap_lut *lut = &na->na_lut;
1515 if (na == NULL || na->pdev == NULL)
1518 #if defined(__FreeBSD__)
1519 /* On FreeBSD mapping and unmapping is performed by the txsync
1520 * and rxsync routine, packet by packet. */
1524 #elif defined(_WIN32)
1528 D("unsupported on Windows");
1530 ND("unmapping and freeing plut for %s", na->name);
1531 if (lut->plut == NULL)
1533 for (i = 0; i < lim; i += p->_clustentries) {
1534 if (lut->plut[i].paddr)
1535 netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, p->_clustsize);
1537 nm_free_plut(lut->plut);
1545 netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1548 int i, lim = p->objtotal;
1549 struct netmap_lut *lut = &na->na_lut;
1551 if (na->pdev == NULL)
1554 #if defined(__FreeBSD__)
1555 /* On FreeBSD mapping and unmapping is performed by the txsync
1556 * and rxsync routine, packet by packet. */
1560 #elif defined(_WIN32)
1564 D("unsupported on Windows");
1567 if (lut->plut != NULL) {
1568 ND("plut already allocated for %s", na->name);
1572 ND("allocating physical lut for %s", na->name);
1573 lut->plut = nm_alloc_plut(lim);
1574 if (lut->plut == NULL) {
1575 D("Failed to allocate physical lut for %s", na->name);
1579 for (i = 0; i < lim; i += p->_clustentries) {
1580 lut->plut[i].paddr = 0;
1583 for (i = 0; i < lim; i += p->_clustentries) {
1586 if (p->lut[i].vaddr == NULL)
1589 error = netmap_load_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr,
1590 p->lut[i].vaddr, p->_clustsize);
1592 D("Failed to map cluster #%d from the %s pool", i, p->name);
1596 for (j = 1; j < p->_clustentries; j++) {
1597 lut->plut[i + j].paddr = lut->plut[i + j - 1].paddr + p->_objsize;
1602 netmap_mem_unmap(p, na);
1610 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1613 if (nmd->flags & NETMAP_MEM_FINALIZED)
1616 nmd->nm_totalsize = 0;
1617 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1618 nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1621 nmd->nm_totalsize += nmd->pools[i].memtotal;
1623 nmd->lasterr = netmap_mem_init_bitmaps(nmd);
1627 nmd->flags |= NETMAP_MEM_FINALIZED;
1630 D("interfaces %d KB, rings %d KB, buffers %d MB",
1631 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1632 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1633 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1636 D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1641 netmap_mem_reset_all(nmd);
1642 return nmd->lasterr;
1646 * allocator for private memory
1649 _netmap_mem_private_new(size_t size, struct netmap_obj_params *p,
1650 struct netmap_mem_ops *ops, int *perr)
1652 struct netmap_mem_d *d = NULL;
1655 d = nm_os_malloc(size);
1664 err = nm_mem_assign_id(d);
1667 snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1669 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1670 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1671 nm_blueprint.pools[i].name,
1673 d->params[i].num = p[i].num;
1674 d->params[i].size = p[i].size;
1679 err = netmap_mem_config(d);
1683 d->flags &= ~NETMAP_MEM_FINALIZED;
1688 NMA_LOCK_DESTROY(d);
1689 nm_mem_release_id(d);
1698 struct netmap_mem_d *
1699 netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1700 u_int extra_bufs, u_int npipes, int *perr)
1702 struct netmap_mem_d *d = NULL;
1703 struct netmap_obj_params p[NETMAP_POOLS_NR];
1706 /* account for the fake host rings */
1710 /* copy the min values */
1711 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1712 p[i] = netmap_min_priv_params[i];
1715 /* possibly increase them to fit user request */
1716 v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1717 if (p[NETMAP_IF_POOL].size < v)
1718 p[NETMAP_IF_POOL].size = v;
1720 if (p[NETMAP_IF_POOL].num < v)
1721 p[NETMAP_IF_POOL].num = v;
1722 maxd = (txd > rxd) ? txd : rxd;
1723 v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1724 if (p[NETMAP_RING_POOL].size < v)
1725 p[NETMAP_RING_POOL].size = v;
1726 /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1727 * and two rx rings (again, 1 normal and 1 fake host)
1729 v = txr + rxr + 8 * npipes;
1730 if (p[NETMAP_RING_POOL].num < v)
1731 p[NETMAP_RING_POOL].num = v;
1732 /* for each pipe we only need the buffers for the 4 "real" rings.
1733 * On the other end, the pipe ring dimension may be different from
1734 * the parent port ring dimension. As a compromise, we allocate twice the
1735 * space actually needed if the pipe rings were the same size as the parent rings
1737 v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1738 /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1739 if (p[NETMAP_BUF_POOL].num < v)
1740 p[NETMAP_BUF_POOL].num = v;
1743 D("req if %d*%d ring %d*%d buf %d*%d",
1744 p[NETMAP_IF_POOL].num,
1745 p[NETMAP_IF_POOL].size,
1746 p[NETMAP_RING_POOL].num,
1747 p[NETMAP_RING_POOL].size,
1748 p[NETMAP_BUF_POOL].num,
1749 p[NETMAP_BUF_POOL].size);
1751 d = _netmap_mem_private_new(sizeof(*d), p, &netmap_mem_global_ops, perr);
1757 /* call with lock held */
1759 netmap_mem2_config(struct netmap_mem_d *nmd)
1763 if (!netmap_mem_params_changed(nmd->params))
1766 ND("reconfiguring");
1768 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1769 /* reset previous allocation */
1770 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1771 netmap_reset_obj_allocator(&nmd->pools[i]);
1773 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1776 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1777 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1778 nmd->params[i].num, nmd->params[i].size);
1785 return nmd->lasterr;
1789 netmap_mem2_finalize(struct netmap_mem_d *nmd)
1791 if (nmd->flags & NETMAP_MEM_FINALIZED)
1794 if (netmap_mem_finalize_all(nmd))
1800 return nmd->lasterr;
1804 netmap_mem2_delete(struct netmap_mem_d *nmd)
1808 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1809 netmap_destroy_obj_allocator(&nmd->pools[i]);
1812 NMA_LOCK_DESTROY(nmd);
1818 /* doubly linekd list of all existing external allocators */
1819 static struct netmap_mem_ext *netmap_mem_ext_list = NULL;
1820 NM_MTX_T nm_mem_ext_list_lock;
1821 #endif /* WITH_EXTMEM */
1824 netmap_mem_init(void)
1826 NM_MTX_INIT(nm_mem_list_lock);
1827 NMA_LOCK_INIT(&nm_mem);
1828 netmap_mem_get(&nm_mem);
1830 NM_MTX_INIT(nm_mem_ext_list_lock);
1831 #endif /* WITH_EXTMEM */
1836 netmap_mem_fini(void)
1838 netmap_mem_put(&nm_mem);
1842 netmap_free_rings(struct netmap_adapter *na)
1848 for (i = 0; i < netmap_all_rings(na, t); i++) {
1849 struct netmap_kring *kring = NMR(na, t)[i];
1850 struct netmap_ring *ring = kring->ring;
1852 if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) {
1854 D("NOT deleting ring %s (ring %p, users %d neekring %d)",
1855 kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
1859 D("deleting ring %s", kring->name);
1860 if (!(kring->nr_kflags & NKR_FAKERING)) {
1861 ND("freeing bufs for %s", kring->name);
1862 netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1864 ND("NOT freeing bufs for %s", kring->name);
1866 netmap_ring_free(na->nm_mem, ring);
1872 /* call with NMA_LOCK held *
1874 * Allocate netmap rings and buffers for this card
1875 * The rings are contiguous, but have variable size.
1876 * The kring array must follow the layout described
1877 * in netmap_krings_create().
1880 netmap_mem2_rings_create(struct netmap_adapter *na)
1887 for (i = 0; i < netmap_all_rings(na, t); i++) {
1888 struct netmap_kring *kring = NMR(na, t)[i];
1889 struct netmap_ring *ring = kring->ring;
1892 if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) {
1893 /* uneeded, or already created by somebody else */
1895 D("NOT creating ring %s (ring %p, users %d neekring %d)",
1896 kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
1900 D("creating %s", kring->name);
1901 ndesc = kring->nkr_num_slots;
1902 len = sizeof(struct netmap_ring) +
1903 ndesc * sizeof(struct netmap_slot);
1904 ring = netmap_ring_malloc(na->nm_mem, len);
1906 D("Cannot allocate %s_ring", nm_txrx2str(t));
1909 ND("txring at %p", ring);
1911 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1912 *(int64_t *)(uintptr_t)&ring->buf_ofs =
1913 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1914 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1915 netmap_ring_offset(na->nm_mem, ring);
1917 /* copy values from kring */
1918 ring->head = kring->rhead;
1919 ring->cur = kring->rcur;
1920 ring->tail = kring->rtail;
1921 *(uint32_t *)(uintptr_t)&ring->nr_buf_size =
1922 netmap_mem_bufsize(na->nm_mem);
1923 ND("%s h %d c %d t %d", kring->name,
1924 ring->head, ring->cur, ring->tail);
1925 ND("initializing slots for %s_ring", nm_txrx2str(t));
1926 if (!(kring->nr_kflags & NKR_FAKERING)) {
1927 /* this is a real ring */
1928 ND("allocating buffers for %s", kring->name);
1929 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1930 D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1934 /* this is a fake ring, set all indices to 0 */
1935 ND("NOT allocating buffers for %s", kring->name);
1936 netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1939 *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1940 *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1947 /* we cannot actually cleanup here, since we don't own kring->users
1948 * and kring->nr_klags & NKR_NEEDRING. The caller must decrement
1949 * the first or zero-out the second, then call netmap_free_rings()
1957 netmap_mem2_rings_delete(struct netmap_adapter *na)
1959 /* last instance, release bufs and rings */
1960 netmap_free_rings(na);
1964 /* call with NMA_LOCK held */
1966 * Allocate the per-fd structure netmap_if.
1968 * We assume that the configuration stored in na
1969 * (number of tx/rx rings and descs) does not change while
1970 * the interface is in netmap mode.
1972 static struct netmap_if *
1973 netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
1975 struct netmap_if *nifp;
1976 ssize_t base; /* handy for relative offsets between rings and nifp */
1977 u_int i, len, n[NR_TXRX], ntot;
1982 /* account for the (eventually fake) host rings */
1983 n[t] = netmap_all_rings(na, t);
1987 * the descriptor is followed inline by an array of offsets
1988 * to the tx and rx rings in the shared memory region.
1991 len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1992 nifp = netmap_if_malloc(na->nm_mem, len);
1994 NMA_UNLOCK(na->nm_mem);
1998 /* initialize base fields -- override const */
1999 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
2000 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
2001 strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
2004 * fill the slots for the rx and tx rings. They contain the offset
2005 * between the ring and nifp, so the information is usable in
2006 * userspace to reach the ring from the nifp.
2008 base = netmap_if_offset(na->nm_mem, nifp);
2009 for (i = 0; i < n[NR_TX]; i++) {
2010 /* XXX instead of ofs == 0 maybe use the offset of an error
2011 * ring, like we do for buffers? */
2014 if (na->tx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_TX]
2015 && i < priv->np_qlast[NR_TX]) {
2016 ofs = netmap_ring_offset(na->nm_mem,
2017 na->tx_rings[i]->ring) - base;
2019 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
2021 for (i = 0; i < n[NR_RX]; i++) {
2022 /* XXX instead of ofs == 0 maybe use the offset of an error
2023 * ring, like we do for buffers? */
2026 if (na->rx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_RX]
2027 && i < priv->np_qlast[NR_RX]) {
2028 ofs = netmap_ring_offset(na->nm_mem,
2029 na->rx_rings[i]->ring) - base;
2031 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
2038 netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2043 if (nifp->ni_bufs_head)
2044 netmap_extra_free(na, nifp->ni_bufs_head);
2045 netmap_if_free(na->nm_mem, nifp);
2049 netmap_mem2_deref(struct netmap_mem_d *nmd)
2053 D("active = %d", nmd->active);
2057 struct netmap_mem_ops netmap_mem_global_ops = {
2058 .nmd_get_lut = netmap_mem2_get_lut,
2059 .nmd_get_info = netmap_mem2_get_info,
2060 .nmd_ofstophys = netmap_mem2_ofstophys,
2061 .nmd_config = netmap_mem2_config,
2062 .nmd_finalize = netmap_mem2_finalize,
2063 .nmd_deref = netmap_mem2_deref,
2064 .nmd_delete = netmap_mem2_delete,
2065 .nmd_if_offset = netmap_mem2_if_offset,
2066 .nmd_if_new = netmap_mem2_if_new,
2067 .nmd_if_delete = netmap_mem2_if_delete,
2068 .nmd_rings_create = netmap_mem2_rings_create,
2069 .nmd_rings_delete = netmap_mem2_rings_delete
2073 netmap_mem_pools_info_get(struct nmreq_pools_info *req,
2074 struct netmap_mem_d *nmd)
2078 ret = netmap_mem_get_info(nmd, &req->nr_memsize, NULL,
2085 req->nr_if_pool_offset = 0;
2086 req->nr_if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
2087 req->nr_if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
2089 req->nr_ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
2090 req->nr_ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
2091 req->nr_ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
2093 req->nr_buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
2094 nmd->pools[NETMAP_RING_POOL].memtotal;
2095 req->nr_buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
2096 req->nr_buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
2103 struct netmap_mem_ext {
2104 struct netmap_mem_d up;
2106 struct nm_os_extmem *os;
2107 struct netmap_mem_ext *next, *prev;
2110 /* call with nm_mem_list_lock held */
2112 netmap_mem_ext_register(struct netmap_mem_ext *e)
2114 NM_MTX_LOCK(nm_mem_ext_list_lock);
2115 if (netmap_mem_ext_list)
2116 netmap_mem_ext_list->prev = e;
2117 e->next = netmap_mem_ext_list;
2118 netmap_mem_ext_list = e;
2120 NM_MTX_UNLOCK(nm_mem_ext_list_lock);
2123 /* call with nm_mem_list_lock held */
2125 netmap_mem_ext_unregister(struct netmap_mem_ext *e)
2128 e->prev->next = e->next;
2130 netmap_mem_ext_list = e->next;
2132 e->next->prev = e->prev;
2133 e->prev = e->next = NULL;
2136 static struct netmap_mem_ext *
2137 netmap_mem_ext_search(struct nm_os_extmem *os)
2139 struct netmap_mem_ext *e;
2141 NM_MTX_LOCK(nm_mem_ext_list_lock);
2142 for (e = netmap_mem_ext_list; e; e = e->next) {
2143 if (nm_os_extmem_isequal(e->os, os)) {
2144 netmap_mem_get(&e->up);
2148 NM_MTX_UNLOCK(nm_mem_ext_list_lock);
2154 netmap_mem_ext_delete(struct netmap_mem_d *d)
2157 struct netmap_mem_ext *e =
2158 (struct netmap_mem_ext *)d;
2160 netmap_mem_ext_unregister(e);
2162 for (i = 0; i < NETMAP_POOLS_NR; i++) {
2163 struct netmap_obj_pool *p = &d->pools[i];
2166 nm_free_lut(p->lut, p->objtotal);
2171 nm_os_extmem_delete(e->os);
2172 netmap_mem2_delete(d);
2176 netmap_mem_ext_config(struct netmap_mem_d *nmd)
2181 struct netmap_mem_ops netmap_mem_ext_ops = {
2182 .nmd_get_lut = netmap_mem2_get_lut,
2183 .nmd_get_info = netmap_mem2_get_info,
2184 .nmd_ofstophys = netmap_mem2_ofstophys,
2185 .nmd_config = netmap_mem_ext_config,
2186 .nmd_finalize = netmap_mem2_finalize,
2187 .nmd_deref = netmap_mem2_deref,
2188 .nmd_delete = netmap_mem_ext_delete,
2189 .nmd_if_offset = netmap_mem2_if_offset,
2190 .nmd_if_new = netmap_mem2_if_new,
2191 .nmd_if_delete = netmap_mem2_if_delete,
2192 .nmd_rings_create = netmap_mem2_rings_create,
2193 .nmd_rings_delete = netmap_mem2_rings_delete
2196 struct netmap_mem_d *
2197 netmap_mem_ext_create(uint64_t usrptr, struct nmreq_pools_info *pi, int *perror)
2201 struct netmap_mem_ext *nme;
2204 struct nm_os_extmem *os = NULL;
2207 // XXX sanity checks
2208 if (pi->nr_if_pool_objtotal == 0)
2209 pi->nr_if_pool_objtotal = netmap_min_priv_params[NETMAP_IF_POOL].num;
2210 if (pi->nr_if_pool_objsize == 0)
2211 pi->nr_if_pool_objsize = netmap_min_priv_params[NETMAP_IF_POOL].size;
2212 if (pi->nr_ring_pool_objtotal == 0)
2213 pi->nr_ring_pool_objtotal = netmap_min_priv_params[NETMAP_RING_POOL].num;
2214 if (pi->nr_ring_pool_objsize == 0)
2215 pi->nr_ring_pool_objsize = netmap_min_priv_params[NETMAP_RING_POOL].size;
2216 if (pi->nr_buf_pool_objtotal == 0)
2217 pi->nr_buf_pool_objtotal = netmap_min_priv_params[NETMAP_BUF_POOL].num;
2218 if (pi->nr_buf_pool_objsize == 0)
2219 pi->nr_buf_pool_objsize = netmap_min_priv_params[NETMAP_BUF_POOL].size;
2220 D("if %d %d ring %d %d buf %d %d",
2221 pi->nr_if_pool_objtotal, pi->nr_if_pool_objsize,
2222 pi->nr_ring_pool_objtotal, pi->nr_ring_pool_objsize,
2223 pi->nr_buf_pool_objtotal, pi->nr_buf_pool_objsize);
2225 os = nm_os_extmem_create(usrptr, pi, &error);
2227 D("os extmem creation failed");
2231 nme = netmap_mem_ext_search(os);
2233 nm_os_extmem_delete(os);
2236 D("not found, creating new");
2238 nme = _netmap_mem_private_new(sizeof(*nme),
2239 (struct netmap_obj_params[]){
2240 { pi->nr_if_pool_objsize, pi->nr_if_pool_objtotal },
2241 { pi->nr_ring_pool_objsize, pi->nr_ring_pool_objtotal },
2242 { pi->nr_buf_pool_objsize, pi->nr_buf_pool_objtotal }},
2243 &netmap_mem_ext_ops,
2248 nr_pages = nm_os_extmem_nr_pages(os);
2250 /* from now on pages will be released by nme destructor;
2251 * we let res = 0 to prevent release in out_unmap below
2254 os = NULL; /* pass ownership */
2256 clust = nm_os_extmem_nextpage(nme->os);
2258 for (i = 0; i < NETMAP_POOLS_NR; i++) {
2259 struct netmap_obj_pool *p = &nme->up.pools[i];
2260 struct netmap_obj_params *o = &nme->up.params[i];
2262 p->_objsize = o->size;
2263 p->_clustsize = o->size;
2264 p->_clustentries = 1;
2266 p->lut = nm_alloc_lut(o->num);
2267 if (p->lut == NULL) {
2272 p->bitmap_slots = (o->num + sizeof(uint32_t) - 1) / sizeof(uint32_t);
2273 p->invalid_bitmap = nm_os_malloc(sizeof(uint32_t) * p->bitmap_slots);
2274 if (p->invalid_bitmap == NULL) {
2279 if (nr_pages == 0) {
2286 for (j = 0; j < o->num && nr_pages > 0; j++) {
2289 p->lut[j].vaddr = clust + off;
2290 #if !defined(linux) && !defined(_WIN32)
2291 p->lut[j].paddr = vtophys(p->lut[j].vaddr);
2293 ND("%s %d at %p", p->name, j, p->lut[j].vaddr);
2294 noff = off + p->_objsize;
2295 if (noff < PAGE_SIZE) {
2299 ND("too big, recomputing offset...");
2300 while (noff >= PAGE_SIZE) {
2301 char *old_clust = clust;
2303 clust = nm_os_extmem_nextpage(nme->os);
2305 ND("noff %zu page %p nr_pages %d", noff,
2306 page_to_virt(*pages), nr_pages);
2307 if (noff > 0 && !nm_isset(p->invalid_bitmap, j) &&
2309 old_clust + PAGE_SIZE != clust))
2311 /* out of space or non contiguous,
2314 p->invalid_bitmap[ (j>>5) ] |= 1U << (j & 31U);
2315 ND("non contiguous at off %zu, drop", noff);
2323 p->numclusters = p->objtotal;
2324 p->memtotal = j * p->_objsize;
2325 ND("%d memtotal %u", j, p->memtotal);
2328 netmap_mem_ext_register(nme);
2333 netmap_mem_put(&nme->up);
2336 nm_os_extmem_delete(os);
2343 #endif /* WITH_EXTMEM */
2346 #ifdef WITH_PTNETMAP_GUEST
2348 struct mem_pt_if *next;
2350 unsigned int nifp_offset;
2353 /* Netmap allocator for ptnetmap guests. */
2354 struct netmap_mem_ptg {
2355 struct netmap_mem_d up;
2357 vm_paddr_t nm_paddr; /* physical address in the guest */
2358 void *nm_addr; /* virtual address in the guest */
2359 struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */
2360 nm_memid_t host_mem_id; /* allocator identifier in the host */
2361 struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
2362 struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */
2365 /* Link a passthrough interface to a passthrough netmap allocator. */
2367 netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp,
2368 unsigned int nifp_offset)
2370 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2371 struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
2380 ptif->nifp_offset = nifp_offset;
2382 if (ptnmd->pt_ifs) {
2383 ptif->next = ptnmd->pt_ifs;
2385 ptnmd->pt_ifs = ptif;
2389 D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset);
2394 /* Called with NMA_LOCK(nmd) held. */
2395 static struct mem_pt_if *
2396 netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp)
2398 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2399 struct mem_pt_if *curr;
2401 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
2402 if (curr->ifp == ifp) {
2410 /* Unlink a passthrough interface from a passthrough netmap allocator. */
2412 netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp)
2414 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2415 struct mem_pt_if *prev = NULL;
2416 struct mem_pt_if *curr;
2421 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
2422 if (curr->ifp == ifp) {
2424 prev->next = curr->next;
2426 ptnmd->pt_ifs = curr->next;
2428 D("removed (ifp=%p,nifp_offset=%u)",
2429 curr->ifp, curr->nifp_offset);
2443 netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
2445 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2447 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
2451 *lut = ptnmd->buf_lut;
2456 netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, uint64_t *size,
2457 u_int *memflags, uint16_t *id)
2461 error = nmd->ops->nmd_config(nmd);
2466 *size = nmd->nm_totalsize;
2468 *memflags = nmd->flags;
2478 netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
2480 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2482 /* if the offset is valid, just return csb->base_addr + off */
2483 paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
2484 ND("off %lx padr %lx", off, (unsigned long)paddr);
2489 netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
2491 /* nothing to do, we are configured on creation
2492 * and configuration never changes thereafter
2498 netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd)
2500 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2510 if (nmd->flags & NETMAP_MEM_FINALIZED)
2513 if (ptnmd->ptn_dev == NULL) {
2514 D("ptnetmap memdev not attached");
2518 /* Map memory through ptnetmap-memdev BAR. */
2519 error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2520 &ptnmd->nm_addr, &mem_size);
2524 /* Initialize the lut using the information contained in the
2525 * ptnetmap memory device. */
2526 bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2527 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2528 nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2529 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2531 /* allocate the lut */
2532 if (ptnmd->buf_lut.lut == NULL) {
2533 D("allocating lut");
2534 ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2535 if (ptnmd->buf_lut.lut == NULL) {
2536 D("lut allocation failed");
2541 /* we have physically contiguous memory mapped through PCI BAR */
2542 poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2543 PTNET_MDEV_IO_BUF_POOL_OFS);
2544 vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2545 paddr = ptnmd->nm_paddr + poolofs;
2547 for (i = 0; i < nbuffers; i++) {
2548 ptnmd->buf_lut.lut[i].vaddr = vaddr;
2553 ptnmd->buf_lut.objtotal = nbuffers;
2554 ptnmd->buf_lut.objsize = bufsize;
2555 nmd->nm_totalsize = (unsigned int)mem_size;
2557 /* Initialize these fields as are needed by
2558 * netmap_mem_bufsize().
2559 * XXX please improve this, why do we need this
2560 * replication? maybe we nmd->pools[] should no be
2561 * there for the guest allocator? */
2562 nmd->pools[NETMAP_BUF_POOL]._objsize = bufsize;
2563 nmd->pools[NETMAP_BUF_POOL]._objtotal = nbuffers;
2565 nmd->flags |= NETMAP_MEM_FINALIZED;
2571 netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd)
2573 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2575 if (nmd->active == 1 &&
2576 (nmd->flags & NETMAP_MEM_FINALIZED)) {
2577 nmd->flags &= ~NETMAP_MEM_FINALIZED;
2578 /* unmap ptnetmap-memdev memory */
2579 if (ptnmd->ptn_dev) {
2580 nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
2582 ptnmd->nm_addr = NULL;
2583 ptnmd->nm_paddr = 0;
2588 netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
2590 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2592 return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
2596 netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
2601 D("deleting %p", nmd);
2602 if (nmd->active > 0)
2603 D("bug: deleting mem allocator with active=%d!", nmd->active);
2605 D("done deleting %p", nmd);
2606 NMA_LOCK_DESTROY(nmd);
2610 static struct netmap_if *
2611 netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
2613 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2614 struct mem_pt_if *ptif;
2615 struct netmap_if *nifp = NULL;
2617 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2619 D("Error: interface %p is not in passthrough", na->ifp);
2623 nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
2630 netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2632 struct mem_pt_if *ptif;
2634 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2636 D("Error: interface %p is not in passthrough", na->ifp);
2641 netmap_mem_pt_guest_rings_create(struct netmap_adapter *na)
2643 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2644 struct mem_pt_if *ptif;
2645 struct netmap_if *nifp;
2648 ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2650 D("Error: interface %p is not in passthrough", na->ifp);
2655 /* point each kring to the corresponding backend ring */
2656 nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
2657 for (i = 0; i < netmap_all_rings(na, NR_TX); i++) {
2658 struct netmap_kring *kring = na->tx_rings[i];
2661 kring->ring = (struct netmap_ring *)
2662 ((char *)nifp + nifp->ring_ofs[i]);
2664 for (i = 0; i < netmap_all_rings(na, NR_RX); i++) {
2665 struct netmap_kring *kring = na->rx_rings[i];
2668 kring->ring = (struct netmap_ring *)
2670 nifp->ring_ofs[i + na->num_tx_rings + 1]);
2679 netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na)
2686 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
2687 struct netmap_kring *kring = &NMR(na, t)[i];
2695 static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
2696 .nmd_get_lut = netmap_mem_pt_guest_get_lut,
2697 .nmd_get_info = netmap_mem_pt_guest_get_info,
2698 .nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
2699 .nmd_config = netmap_mem_pt_guest_config,
2700 .nmd_finalize = netmap_mem_pt_guest_finalize,
2701 .nmd_deref = netmap_mem_pt_guest_deref,
2702 .nmd_if_offset = netmap_mem_pt_guest_if_offset,
2703 .nmd_delete = netmap_mem_pt_guest_delete,
2704 .nmd_if_new = netmap_mem_pt_guest_if_new,
2705 .nmd_if_delete = netmap_mem_pt_guest_if_delete,
2706 .nmd_rings_create = netmap_mem_pt_guest_rings_create,
2707 .nmd_rings_delete = netmap_mem_pt_guest_rings_delete
2710 /* Called with nm_mem_list_lock held. */
2711 static struct netmap_mem_d *
2712 netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
2714 struct netmap_mem_d *mem = NULL;
2715 struct netmap_mem_d *scan = netmap_last_mem_d;
2718 /* find ptnetmap allocator through host ID */
2719 if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2720 ((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
2723 NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
2727 } while (scan != netmap_last_mem_d);
2732 /* Called with nm_mem_list_lock held. */
2733 static struct netmap_mem_d *
2734 netmap_mem_pt_guest_create(nm_memid_t mem_id)
2736 struct netmap_mem_ptg *ptnmd;
2739 ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
2740 if (ptnmd == NULL) {
2745 ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2746 ptnmd->host_mem_id = mem_id;
2747 ptnmd->pt_ifs = NULL;
2749 /* Assign new id in the guest (We have the lock) */
2750 err = nm_mem_assign_id_locked(&ptnmd->up);
2754 ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
2755 ptnmd->up.flags |= NETMAP_MEM_IO;
2757 NMA_LOCK_INIT(&ptnmd->up);
2759 snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2764 netmap_mem_pt_guest_delete(&ptnmd->up);
2769 * find host id in guest allocators and create guest allocator
2770 * if it is not there
2772 static struct netmap_mem_d *
2773 netmap_mem_pt_guest_get(nm_memid_t mem_id)
2775 struct netmap_mem_d *nmd;
2777 NM_MTX_LOCK(nm_mem_list_lock);
2778 nmd = netmap_mem_pt_guest_find_memid(mem_id);
2780 nmd = netmap_mem_pt_guest_create(mem_id);
2782 NM_MTX_UNLOCK(nm_mem_list_lock);
2788 * The guest allocator can be created by ptnetmap_memdev (during the device
2789 * attach) or by ptnetmap device (ptnet), during the netmap_attach.
2791 * The order is not important (we have different order in LINUX and FreeBSD).
2792 * The first one, creates the device, and the second one simply attaches it.
2795 /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
2797 struct netmap_mem_d *
2798 netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
2800 struct netmap_mem_d *nmd;
2801 struct netmap_mem_ptg *ptnmd;
2803 nmd = netmap_mem_pt_guest_get(mem_id);
2805 /* assign this device to the guest allocator */
2807 ptnmd = (struct netmap_mem_ptg *)nmd;
2808 ptnmd->ptn_dev = ptn_dev;
2814 /* Called when ptnet device is attaching */
2815 struct netmap_mem_d *
2816 netmap_mem_pt_guest_new(struct ifnet *ifp,
2817 unsigned int nifp_offset,
2820 struct netmap_mem_d *nmd;
2826 nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
2829 netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
2835 #endif /* WITH_PTNETMAP_GUEST */