1ccdc3305SLuigi Rizzo /* 237e3a6d3SLuigi Rizzo * Copyright (C) 2012-2014 Matteo Landi 337e3a6d3SLuigi Rizzo * Copyright (C) 2012-2016 Luigi Rizzo 437e3a6d3SLuigi Rizzo * Copyright (C) 2012-2016 Giuseppe Lettieri 537e3a6d3SLuigi Rizzo * All rights reserved. 6ccdc3305SLuigi Rizzo * 7ccdc3305SLuigi Rizzo * Redistribution and use in source and binary forms, with or without 8ccdc3305SLuigi Rizzo * modification, are permitted provided that the following conditions 9ccdc3305SLuigi Rizzo * are met: 10ccdc3305SLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 11ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer. 12ccdc3305SLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 13ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 14ccdc3305SLuigi Rizzo * documentation and/or other materials provided with the distribution. 15ccdc3305SLuigi Rizzo * 16ccdc3305SLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17ccdc3305SLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18ccdc3305SLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19ccdc3305SLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20ccdc3305SLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21ccdc3305SLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22ccdc3305SLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23ccdc3305SLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24ccdc3305SLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25ccdc3305SLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26ccdc3305SLuigi Rizzo * SUCH DAMAGE. 27ccdc3305SLuigi Rizzo */ 28ccdc3305SLuigi Rizzo 298241616dSLuigi Rizzo #ifdef linux 30ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 318241616dSLuigi Rizzo #endif /* linux */ 328241616dSLuigi Rizzo 33ce3ee1e7SLuigi Rizzo #ifdef __APPLE__ 34ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 35ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */ 368241616dSLuigi Rizzo 37ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 38ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 39ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$"); 408241616dSLuigi Rizzo 41ce3ee1e7SLuigi Rizzo #include <sys/types.h> 42ce3ee1e7SLuigi Rizzo #include <sys/malloc.h> 4337e3a6d3SLuigi Rizzo #include <sys/kernel.h> /* MALLOC_DEFINE */ 44ce3ee1e7SLuigi Rizzo #include <sys/proc.h> 45ce3ee1e7SLuigi Rizzo #include <vm/vm.h> /* vtophys */ 46ce3ee1e7SLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 47ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 48ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h> 49ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h> 50ce3ee1e7SLuigi Rizzo #include <net/if.h> 51ce3ee1e7SLuigi Rizzo #include <net/if_var.h> 52ce3ee1e7SLuigi Rizzo #include <net/vnet.h> 53ce3ee1e7SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 54ce3ee1e7SLuigi Rizzo 5537e3a6d3SLuigi Rizzo /* M_NETMAP only used in here */ 5637e3a6d3SLuigi Rizzo MALLOC_DECLARE(M_NETMAP); 5737e3a6d3SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 5837e3a6d3SLuigi Rizzo 59ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 60ce3ee1e7SLuigi Rizzo 6137e3a6d3SLuigi Rizzo #ifdef _WIN32 6237e3a6d3SLuigi Rizzo #include <win_glue.h> 6337e3a6d3SLuigi Rizzo #endif 6437e3a6d3SLuigi Rizzo 65ce3ee1e7SLuigi Rizzo #include <net/netmap.h> 66ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 6737e3a6d3SLuigi Rizzo #include <net/netmap_virt.h> 68ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h" 69ce3ee1e7SLuigi Rizzo 7037e3a6d3SLuigi Rizzo #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY 7137e3a6d3SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 8*4096 /* if too big takes too much time to allocate */ 7237e3a6d3SLuigi Rizzo #else 734bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */ 7437e3a6d3SLuigi Rizzo #endif 754bf50f18SLuigi Rizzo 764bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ 32 774bf50f18SLuigi Rizzo 784bf50f18SLuigi Rizzo 794bf50f18SLuigi Rizzo enum { 804bf50f18SLuigi Rizzo NETMAP_IF_POOL = 0, 814bf50f18SLuigi Rizzo NETMAP_RING_POOL, 824bf50f18SLuigi Rizzo NETMAP_BUF_POOL, 834bf50f18SLuigi Rizzo NETMAP_POOLS_NR 844bf50f18SLuigi Rizzo }; 854bf50f18SLuigi Rizzo 864bf50f18SLuigi Rizzo 874bf50f18SLuigi Rizzo struct netmap_obj_params { 884bf50f18SLuigi Rizzo u_int size; 894bf50f18SLuigi Rizzo u_int num; 904bf50f18SLuigi Rizzo }; 91847bf383SLuigi Rizzo 924bf50f18SLuigi Rizzo struct netmap_obj_pool { 934bf50f18SLuigi Rizzo char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */ 944bf50f18SLuigi Rizzo 954bf50f18SLuigi Rizzo /* ---------------------------------------------------*/ 964bf50f18SLuigi Rizzo /* these are only meaningful if the pool is finalized */ 974bf50f18SLuigi Rizzo /* (see 'finalized' field in netmap_mem_d) */ 984bf50f18SLuigi Rizzo u_int objtotal; /* actual total number of objects. */ 994bf50f18SLuigi Rizzo u_int memtotal; /* actual total memory space */ 1004bf50f18SLuigi Rizzo u_int numclusters; /* actual number of clusters */ 1014bf50f18SLuigi Rizzo 1024bf50f18SLuigi Rizzo u_int objfree; /* number of free objects. */ 1034bf50f18SLuigi Rizzo 1044bf50f18SLuigi Rizzo struct lut_entry *lut; /* virt,phys addresses, objtotal entries */ 1054bf50f18SLuigi Rizzo uint32_t *bitmap; /* one bit per buffer, 1 means free */ 1064bf50f18SLuigi Rizzo uint32_t bitmap_slots; /* number of uint32 entries in bitmap */ 1074bf50f18SLuigi Rizzo /* ---------------------------------------------------*/ 1084bf50f18SLuigi Rizzo 1094bf50f18SLuigi Rizzo /* limits */ 1104bf50f18SLuigi Rizzo u_int objminsize; /* minimum object size */ 1114bf50f18SLuigi Rizzo u_int objmaxsize; /* maximum object size */ 1124bf50f18SLuigi Rizzo u_int nummin; /* minimum number of objects */ 1134bf50f18SLuigi Rizzo u_int nummax; /* maximum number of objects */ 1144bf50f18SLuigi Rizzo 1154bf50f18SLuigi Rizzo /* these are changed only by config */ 1164bf50f18SLuigi Rizzo u_int _objtotal; /* total number of objects */ 1174bf50f18SLuigi Rizzo u_int _objsize; /* object size */ 1184bf50f18SLuigi Rizzo u_int _clustsize; /* cluster size */ 1194bf50f18SLuigi Rizzo u_int _clustentries; /* objects per cluster */ 1204bf50f18SLuigi Rizzo u_int _numclusters; /* number of clusters */ 1214bf50f18SLuigi Rizzo 1224bf50f18SLuigi Rizzo /* requested values */ 1234bf50f18SLuigi Rizzo u_int r_objtotal; 1244bf50f18SLuigi Rizzo u_int r_objsize; 1254bf50f18SLuigi Rizzo }; 1264bf50f18SLuigi Rizzo 127847bf383SLuigi Rizzo #define NMA_LOCK_T NM_MTX_T 1284bf50f18SLuigi Rizzo 129847bf383SLuigi Rizzo 130847bf383SLuigi Rizzo struct netmap_mem_ops { 13137e3a6d3SLuigi Rizzo int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*); 132847bf383SLuigi Rizzo int (*nmd_get_info)(struct netmap_mem_d *, u_int *size, 133847bf383SLuigi Rizzo u_int *memflags, uint16_t *id); 134847bf383SLuigi Rizzo 135847bf383SLuigi Rizzo vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t); 136847bf383SLuigi Rizzo int (*nmd_config)(struct netmap_mem_d *); 137847bf383SLuigi Rizzo int (*nmd_finalize)(struct netmap_mem_d *); 138847bf383SLuigi Rizzo void (*nmd_deref)(struct netmap_mem_d *); 139847bf383SLuigi Rizzo ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr); 140847bf383SLuigi Rizzo void (*nmd_delete)(struct netmap_mem_d *); 141847bf383SLuigi Rizzo 142847bf383SLuigi Rizzo struct netmap_if * (*nmd_if_new)(struct netmap_adapter *); 143847bf383SLuigi Rizzo void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *); 144847bf383SLuigi Rizzo int (*nmd_rings_create)(struct netmap_adapter *); 145847bf383SLuigi Rizzo void (*nmd_rings_delete)(struct netmap_adapter *); 146847bf383SLuigi Rizzo }; 1474bf50f18SLuigi Rizzo 1484bf50f18SLuigi Rizzo typedef uint16_t nm_memid_t; 1494bf50f18SLuigi Rizzo 15037e3a6d3SLuigi Rizzo /* 15137e3a6d3SLuigi Rizzo * Shared info for netmap allocator 15237e3a6d3SLuigi Rizzo * 15337e3a6d3SLuigi Rizzo * Each allocator contains this structur as first netmap_if. 15437e3a6d3SLuigi Rizzo * In this way, we can share same details about allocator 15537e3a6d3SLuigi Rizzo * to the VM. 15637e3a6d3SLuigi Rizzo * Used in ptnetmap. 15737e3a6d3SLuigi Rizzo */ 15837e3a6d3SLuigi Rizzo struct netmap_mem_shared_info { 15937e3a6d3SLuigi Rizzo #ifndef _WIN32 16037e3a6d3SLuigi Rizzo struct netmap_if up; /* ends with a 0-sized array, which VSC does not like */ 16137e3a6d3SLuigi Rizzo #else /* !_WIN32 */ 16237e3a6d3SLuigi Rizzo char up[sizeof(struct netmap_if)]; 16337e3a6d3SLuigi Rizzo #endif /* !_WIN32 */ 16437e3a6d3SLuigi Rizzo uint64_t features; 16537e3a6d3SLuigi Rizzo #define NMS_FEAT_BUF_POOL 0x0001 16637e3a6d3SLuigi Rizzo #define NMS_FEAT_MEMSIZE 0x0002 16737e3a6d3SLuigi Rizzo 16837e3a6d3SLuigi Rizzo uint32_t buf_pool_offset; 16937e3a6d3SLuigi Rizzo uint32_t buf_pool_objtotal; 17037e3a6d3SLuigi Rizzo uint32_t buf_pool_objsize; 17137e3a6d3SLuigi Rizzo uint32_t totalsize; 17237e3a6d3SLuigi Rizzo }; 17337e3a6d3SLuigi Rizzo 17437e3a6d3SLuigi Rizzo #define NMS_NAME "nms_info" 17537e3a6d3SLuigi Rizzo #define NMS_VERSION 1 17637e3a6d3SLuigi Rizzo static const struct netmap_if nms_if_blueprint = { 17737e3a6d3SLuigi Rizzo .ni_name = NMS_NAME, 17837e3a6d3SLuigi Rizzo .ni_version = NMS_VERSION, 17937e3a6d3SLuigi Rizzo .ni_tx_rings = 0, 18037e3a6d3SLuigi Rizzo .ni_rx_rings = 0 18137e3a6d3SLuigi Rizzo }; 18237e3a6d3SLuigi Rizzo 1834bf50f18SLuigi Rizzo struct netmap_mem_d { 1844bf50f18SLuigi Rizzo NMA_LOCK_T nm_mtx; /* protect the allocator */ 1854bf50f18SLuigi Rizzo u_int nm_totalsize; /* shorthand */ 1864bf50f18SLuigi Rizzo 1874bf50f18SLuigi Rizzo u_int flags; 1884bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */ 1894bf50f18SLuigi Rizzo int lasterr; /* last error for curr config */ 190847bf383SLuigi Rizzo int active; /* active users */ 191847bf383SLuigi Rizzo int refcount; 1924bf50f18SLuigi Rizzo /* the three allocators */ 1934bf50f18SLuigi Rizzo struct netmap_obj_pool pools[NETMAP_POOLS_NR]; 1944bf50f18SLuigi Rizzo 1954bf50f18SLuigi Rizzo nm_memid_t nm_id; /* allocator identifier */ 1964bf50f18SLuigi Rizzo int nm_grp; /* iommu groupd id */ 1974bf50f18SLuigi Rizzo 1984bf50f18SLuigi Rizzo /* list of all existing allocators, sorted by nm_id */ 1994bf50f18SLuigi Rizzo struct netmap_mem_d *prev, *next; 200847bf383SLuigi Rizzo 201847bf383SLuigi Rizzo struct netmap_mem_ops *ops; 2024bf50f18SLuigi Rizzo }; 2034bf50f18SLuigi Rizzo 20437e3a6d3SLuigi Rizzo /* 20537e3a6d3SLuigi Rizzo * XXX need to fix the case of t0 == void 20637e3a6d3SLuigi Rizzo */ 207847bf383SLuigi Rizzo #define NMD_DEFCB(t0, name) \ 208847bf383SLuigi Rizzo t0 \ 209847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd) \ 210847bf383SLuigi Rizzo { \ 211847bf383SLuigi Rizzo return nmd->ops->nmd_##name(nmd); \ 212847bf383SLuigi Rizzo } 213847bf383SLuigi Rizzo 214847bf383SLuigi Rizzo #define NMD_DEFCB1(t0, name, t1) \ 215847bf383SLuigi Rizzo t0 \ 216847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1) \ 217847bf383SLuigi Rizzo { \ 218847bf383SLuigi Rizzo return nmd->ops->nmd_##name(nmd, a1); \ 219847bf383SLuigi Rizzo } 220847bf383SLuigi Rizzo 221847bf383SLuigi Rizzo #define NMD_DEFCB3(t0, name, t1, t2, t3) \ 222847bf383SLuigi Rizzo t0 \ 223847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1, t2 a2, t3 a3) \ 224847bf383SLuigi Rizzo { \ 225847bf383SLuigi Rizzo return nmd->ops->nmd_##name(nmd, a1, a2, a3); \ 226847bf383SLuigi Rizzo } 227847bf383SLuigi Rizzo 228847bf383SLuigi Rizzo #define NMD_DEFNACB(t0, name) \ 229847bf383SLuigi Rizzo t0 \ 230847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_adapter *na) \ 231847bf383SLuigi Rizzo { \ 232847bf383SLuigi Rizzo return na->nm_mem->ops->nmd_##name(na); \ 233847bf383SLuigi Rizzo } 234847bf383SLuigi Rizzo 235847bf383SLuigi Rizzo #define NMD_DEFNACB1(t0, name, t1) \ 236847bf383SLuigi Rizzo t0 \ 237847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_adapter *na, t1 a1) \ 238847bf383SLuigi Rizzo { \ 239847bf383SLuigi Rizzo return na->nm_mem->ops->nmd_##name(na, a1); \ 240847bf383SLuigi Rizzo } 241847bf383SLuigi Rizzo 24237e3a6d3SLuigi Rizzo NMD_DEFCB1(int, get_lut, struct netmap_lut *); 243847bf383SLuigi Rizzo NMD_DEFCB3(int, get_info, u_int *, u_int *, uint16_t *); 244847bf383SLuigi Rizzo NMD_DEFCB1(vm_paddr_t, ofstophys, vm_ooffset_t); 245847bf383SLuigi Rizzo static int netmap_mem_config(struct netmap_mem_d *); 246847bf383SLuigi Rizzo NMD_DEFCB(int, config); 247847bf383SLuigi Rizzo NMD_DEFCB1(ssize_t, if_offset, const void *); 248847bf383SLuigi Rizzo NMD_DEFCB(void, delete); 249847bf383SLuigi Rizzo 250847bf383SLuigi Rizzo NMD_DEFNACB(struct netmap_if *, if_new); 251847bf383SLuigi Rizzo NMD_DEFNACB1(void, if_delete, struct netmap_if *); 252847bf383SLuigi Rizzo NMD_DEFNACB(int, rings_create); 253847bf383SLuigi Rizzo NMD_DEFNACB(void, rings_delete); 254847bf383SLuigi Rizzo 255847bf383SLuigi Rizzo static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *); 256847bf383SLuigi Rizzo static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *); 25737e3a6d3SLuigi Rizzo static int nm_mem_assign_group(struct netmap_mem_d *, struct device *); 258847bf383SLuigi Rizzo 259847bf383SLuigi Rizzo #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx) 260847bf383SLuigi Rizzo #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx) 261847bf383SLuigi Rizzo #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx) 262847bf383SLuigi Rizzo #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx) 263847bf383SLuigi Rizzo 264847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET 265847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line) \ 266847bf383SLuigi Rizzo printf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount); 267847bf383SLuigi Rizzo #else 268847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line) 269847bf383SLuigi Rizzo #endif 270847bf383SLuigi Rizzo 271847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET 272847bf383SLuigi Rizzo void __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line) 273847bf383SLuigi Rizzo #else 274847bf383SLuigi Rizzo void netmap_mem_get(struct netmap_mem_d *nmd) 275847bf383SLuigi Rizzo #endif 276847bf383SLuigi Rizzo { 277847bf383SLuigi Rizzo NMA_LOCK(nmd); 278847bf383SLuigi Rizzo nmd->refcount++; 279847bf383SLuigi Rizzo NM_DBG_REFC(nmd, func, line); 280847bf383SLuigi Rizzo NMA_UNLOCK(nmd); 281847bf383SLuigi Rizzo } 282847bf383SLuigi Rizzo 283847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET 284847bf383SLuigi Rizzo void __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line) 285847bf383SLuigi Rizzo #else 286847bf383SLuigi Rizzo void netmap_mem_put(struct netmap_mem_d *nmd) 287847bf383SLuigi Rizzo #endif 288847bf383SLuigi Rizzo { 289847bf383SLuigi Rizzo int last; 290847bf383SLuigi Rizzo NMA_LOCK(nmd); 291847bf383SLuigi Rizzo last = (--nmd->refcount == 0); 292847bf383SLuigi Rizzo NM_DBG_REFC(nmd, func, line); 293847bf383SLuigi Rizzo NMA_UNLOCK(nmd); 294847bf383SLuigi Rizzo if (last) 295847bf383SLuigi Rizzo netmap_mem_delete(nmd); 296847bf383SLuigi Rizzo } 297847bf383SLuigi Rizzo 298847bf383SLuigi Rizzo int 299847bf383SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na) 300847bf383SLuigi Rizzo { 301847bf383SLuigi Rizzo if (nm_mem_assign_group(nmd, na->pdev) < 0) { 302847bf383SLuigi Rizzo return ENOMEM; 303847bf383SLuigi Rizzo } else { 30437e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 30537e3a6d3SLuigi Rizzo nmd->lasterr = nmd->ops->nmd_finalize(nmd); 30637e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 307847bf383SLuigi Rizzo } 308847bf383SLuigi Rizzo 309847bf383SLuigi Rizzo if (!nmd->lasterr && na->pdev) 310847bf383SLuigi Rizzo netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na); 311847bf383SLuigi Rizzo 312847bf383SLuigi Rizzo return nmd->lasterr; 313847bf383SLuigi Rizzo } 314847bf383SLuigi Rizzo 31537e3a6d3SLuigi Rizzo static int netmap_mem_init_shared_info(struct netmap_mem_d *nmd); 31637e3a6d3SLuigi Rizzo 317847bf383SLuigi Rizzo void 318847bf383SLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na) 319847bf383SLuigi Rizzo { 320847bf383SLuigi Rizzo NMA_LOCK(nmd); 321847bf383SLuigi Rizzo netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na); 32237e3a6d3SLuigi Rizzo if (nmd->active == 1) { 32337e3a6d3SLuigi Rizzo u_int i; 32437e3a6d3SLuigi Rizzo 32537e3a6d3SLuigi Rizzo /* 32637e3a6d3SLuigi Rizzo * Reset the allocator when it falls out of use so that any 32737e3a6d3SLuigi Rizzo * pool resources leaked by unclean application exits are 32837e3a6d3SLuigi Rizzo * reclaimed. 32937e3a6d3SLuigi Rizzo */ 33037e3a6d3SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 33137e3a6d3SLuigi Rizzo struct netmap_obj_pool *p; 33237e3a6d3SLuigi Rizzo u_int j; 33337e3a6d3SLuigi Rizzo 33437e3a6d3SLuigi Rizzo p = &nmd->pools[i]; 33537e3a6d3SLuigi Rizzo p->objfree = p->objtotal; 33637e3a6d3SLuigi Rizzo /* 33737e3a6d3SLuigi Rizzo * Reproduce the net effect of the M_ZERO malloc() 33837e3a6d3SLuigi Rizzo * and marking of free entries in the bitmap that 33937e3a6d3SLuigi Rizzo * occur in finalize_obj_allocator() 34037e3a6d3SLuigi Rizzo */ 34137e3a6d3SLuigi Rizzo memset(p->bitmap, 34237e3a6d3SLuigi Rizzo '\0', 34337e3a6d3SLuigi Rizzo sizeof(uint32_t) * ((p->objtotal + 31) / 32)); 34437e3a6d3SLuigi Rizzo 34537e3a6d3SLuigi Rizzo /* 34637e3a6d3SLuigi Rizzo * Set all the bits in the bitmap that have 34737e3a6d3SLuigi Rizzo * corresponding buffers to 1 to indicate they are 34837e3a6d3SLuigi Rizzo * free. 34937e3a6d3SLuigi Rizzo */ 35037e3a6d3SLuigi Rizzo for (j = 0; j < p->objtotal; j++) { 35137e3a6d3SLuigi Rizzo if (p->lut[j].vaddr != NULL) { 35237e3a6d3SLuigi Rizzo p->bitmap[ (j>>5) ] |= ( 1 << (j & 31) ); 35337e3a6d3SLuigi Rizzo } 35437e3a6d3SLuigi Rizzo } 35537e3a6d3SLuigi Rizzo } 35637e3a6d3SLuigi Rizzo 35737e3a6d3SLuigi Rizzo /* 35837e3a6d3SLuigi Rizzo * Per netmap_mem_finalize_all(), 35937e3a6d3SLuigi Rizzo * buffers 0 and 1 are reserved 36037e3a6d3SLuigi Rizzo */ 36137e3a6d3SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].objfree -= 2; 36237e3a6d3SLuigi Rizzo if (nmd->pools[NETMAP_BUF_POOL].bitmap) { 36337e3a6d3SLuigi Rizzo /* XXX This check is a workaround that prevents a 36437e3a6d3SLuigi Rizzo * NULL pointer crash which currently happens only 36537e3a6d3SLuigi Rizzo * with ptnetmap guests. Also, 36637e3a6d3SLuigi Rizzo * netmap_mem_init_shared_info must not be called 36737e3a6d3SLuigi Rizzo * by ptnetmap guest. */ 36837e3a6d3SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3; 36937e3a6d3SLuigi Rizzo 37037e3a6d3SLuigi Rizzo /* expose info to the ptnetmap guest */ 37137e3a6d3SLuigi Rizzo netmap_mem_init_shared_info(nmd); 37237e3a6d3SLuigi Rizzo } 37337e3a6d3SLuigi Rizzo } 37437e3a6d3SLuigi Rizzo nmd->ops->nmd_deref(nmd); 37537e3a6d3SLuigi Rizzo 376847bf383SLuigi Rizzo NMA_UNLOCK(nmd); 377847bf383SLuigi Rizzo } 378847bf383SLuigi Rizzo 379847bf383SLuigi Rizzo 3804bf50f18SLuigi Rizzo /* accessor functions */ 38137e3a6d3SLuigi Rizzo static int 382847bf383SLuigi Rizzo netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 3834bf50f18SLuigi Rizzo { 384847bf383SLuigi Rizzo lut->lut = nmd->pools[NETMAP_BUF_POOL].lut; 385847bf383SLuigi Rizzo lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal; 386847bf383SLuigi Rizzo lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize; 38737e3a6d3SLuigi Rizzo 38837e3a6d3SLuigi Rizzo return 0; 3894bf50f18SLuigi Rizzo } 3904bf50f18SLuigi Rizzo 39137e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = { 3928241616dSLuigi Rizzo [NETMAP_IF_POOL] = { 3938241616dSLuigi Rizzo .size = 1024, 3948241616dSLuigi Rizzo .num = 100, 3958241616dSLuigi Rizzo }, 3968241616dSLuigi Rizzo [NETMAP_RING_POOL] = { 3978241616dSLuigi Rizzo .size = 9*PAGE_SIZE, 3988241616dSLuigi Rizzo .num = 200, 3998241616dSLuigi Rizzo }, 4008241616dSLuigi Rizzo [NETMAP_BUF_POOL] = { 4018241616dSLuigi Rizzo .size = 2048, 4028241616dSLuigi Rizzo .num = NETMAP_BUF_MAX_NUM, 4038241616dSLuigi Rizzo }, 4048241616dSLuigi Rizzo }; 4058241616dSLuigi Rizzo 40637e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = { 407f0ea3689SLuigi Rizzo [NETMAP_IF_POOL] = { 408f0ea3689SLuigi Rizzo .size = 1024, 40937e3a6d3SLuigi Rizzo .num = 2, 410f0ea3689SLuigi Rizzo }, 411f0ea3689SLuigi Rizzo [NETMAP_RING_POOL] = { 412f0ea3689SLuigi Rizzo .size = 5*PAGE_SIZE, 413f0ea3689SLuigi Rizzo .num = 4, 414f0ea3689SLuigi Rizzo }, 415f0ea3689SLuigi Rizzo [NETMAP_BUF_POOL] = { 416f0ea3689SLuigi Rizzo .size = 2048, 417f0ea3689SLuigi Rizzo .num = 4098, 418f0ea3689SLuigi Rizzo }, 419f0ea3689SLuigi Rizzo }; 420f0ea3689SLuigi Rizzo 421ccdc3305SLuigi Rizzo 4222579e2d7SLuigi Rizzo /* 4232579e2d7SLuigi Rizzo * nm_mem is the memory allocator used for all physical interfaces 4242579e2d7SLuigi Rizzo * running in netmap mode. 4252579e2d7SLuigi Rizzo * Virtual (VALE) ports will have each its own allocator. 4262579e2d7SLuigi Rizzo */ 427847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */ 428ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = { /* Our memory allocator. */ 4298241616dSLuigi Rizzo .pools = { 4308241616dSLuigi Rizzo [NETMAP_IF_POOL] = { 4318241616dSLuigi Rizzo .name = "netmap_if", 4328241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_if), 4338241616dSLuigi Rizzo .objmaxsize = 4096, 4348241616dSLuigi Rizzo .nummin = 10, /* don't be stingy */ 4358241616dSLuigi Rizzo .nummax = 10000, /* XXX very large */ 4368241616dSLuigi Rizzo }, 4378241616dSLuigi Rizzo [NETMAP_RING_POOL] = { 4388241616dSLuigi Rizzo .name = "netmap_ring", 4398241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 4408241616dSLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 4418241616dSLuigi Rizzo .nummin = 2, 4428241616dSLuigi Rizzo .nummax = 1024, 4438241616dSLuigi Rizzo }, 4448241616dSLuigi Rizzo [NETMAP_BUF_POOL] = { 4458241616dSLuigi Rizzo .name = "netmap_buf", 4468241616dSLuigi Rizzo .objminsize = 64, 4478241616dSLuigi Rizzo .objmaxsize = 65536, 4488241616dSLuigi Rizzo .nummin = 4, 4498241616dSLuigi Rizzo .nummax = 1000000, /* one million! */ 4508241616dSLuigi Rizzo }, 4518241616dSLuigi Rizzo }, 452f0ea3689SLuigi Rizzo 453f0ea3689SLuigi Rizzo .nm_id = 1, 4544bf50f18SLuigi Rizzo .nm_grp = -1, 455f0ea3689SLuigi Rizzo 456f0ea3689SLuigi Rizzo .prev = &nm_mem, 457f0ea3689SLuigi Rizzo .next = &nm_mem, 458847bf383SLuigi Rizzo 459847bf383SLuigi Rizzo .ops = &netmap_mem_global_ops 460ccdc3305SLuigi Rizzo }; 461ccdc3305SLuigi Rizzo 462ce3ee1e7SLuigi Rizzo 46337e3a6d3SLuigi Rizzo static struct netmap_mem_d *netmap_last_mem_d = &nm_mem; 464f0ea3689SLuigi Rizzo 465ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */ 466847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_private_ops; /* forward */ 46737e3a6d3SLuigi Rizzo /* XXX clang is not happy about using name as a print format */ 46837e3a6d3SLuigi Rizzo static const struct netmap_mem_d nm_blueprint = { 469ce3ee1e7SLuigi Rizzo .pools = { 470ce3ee1e7SLuigi Rizzo [NETMAP_IF_POOL] = { 471ce3ee1e7SLuigi Rizzo .name = "%s_if", 472ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_if), 473ce3ee1e7SLuigi Rizzo .objmaxsize = 4096, 474ce3ee1e7SLuigi Rizzo .nummin = 1, 475f0ea3689SLuigi Rizzo .nummax = 100, 476ce3ee1e7SLuigi Rizzo }, 477ce3ee1e7SLuigi Rizzo [NETMAP_RING_POOL] = { 478ce3ee1e7SLuigi Rizzo .name = "%s_ring", 479ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 480ce3ee1e7SLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 481ce3ee1e7SLuigi Rizzo .nummin = 2, 482ce3ee1e7SLuigi Rizzo .nummax = 1024, 483ce3ee1e7SLuigi Rizzo }, 484ce3ee1e7SLuigi Rizzo [NETMAP_BUF_POOL] = { 485ce3ee1e7SLuigi Rizzo .name = "%s_buf", 486ce3ee1e7SLuigi Rizzo .objminsize = 64, 487ce3ee1e7SLuigi Rizzo .objmaxsize = 65536, 488ce3ee1e7SLuigi Rizzo .nummin = 4, 489ce3ee1e7SLuigi Rizzo .nummax = 1000000, /* one million! */ 490ce3ee1e7SLuigi Rizzo }, 491ce3ee1e7SLuigi Rizzo }, 492ce3ee1e7SLuigi Rizzo 493ce3ee1e7SLuigi Rizzo .flags = NETMAP_MEM_PRIVATE, 494847bf383SLuigi Rizzo 495847bf383SLuigi Rizzo .ops = &netmap_mem_private_ops 496ce3ee1e7SLuigi Rizzo }; 497ce3ee1e7SLuigi Rizzo 4988241616dSLuigi Rizzo /* memory allocator related sysctls */ 4998241616dSLuigi Rizzo 5008241616dSLuigi Rizzo #define STRINGIFY(x) #x 5018241616dSLuigi Rizzo 502ce3ee1e7SLuigi Rizzo 5038241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \ 50437e3a6d3SLuigi Rizzo SYSBEGIN(mem2_ ## name); \ 5058241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \ 5068241616dSLuigi Rizzo CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \ 5078241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \ 5088241616dSLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \ 5098241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \ 5108241616dSLuigi Rizzo CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \ 5118241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \ 512f0ea3689SLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \ 513f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \ 514f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \ 515f0ea3689SLuigi Rizzo "Default size of private netmap " STRINGIFY(name) "s"); \ 516f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \ 517f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \ 51837e3a6d3SLuigi Rizzo "Default number of private netmap " STRINGIFY(name) "s"); \ 51937e3a6d3SLuigi Rizzo SYSEND 5208241616dSLuigi Rizzo 521*984ff0d9SEd Maste SYSCTL_DECL(_dev_netmap); 5228241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if); 5238241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring); 5248241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf); 525ccdc3305SLuigi Rizzo 52637e3a6d3SLuigi Rizzo /* call with NMA_LOCK(&nm_mem) held */ 527f0ea3689SLuigi Rizzo static int 52837e3a6d3SLuigi Rizzo nm_mem_assign_id_locked(struct netmap_mem_d *nmd) 529f0ea3689SLuigi Rizzo { 530f0ea3689SLuigi Rizzo nm_memid_t id; 531f0ea3689SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 532f0ea3689SLuigi Rizzo int error = ENOMEM; 533f0ea3689SLuigi Rizzo 534f0ea3689SLuigi Rizzo do { 535f0ea3689SLuigi Rizzo /* we rely on unsigned wrap around */ 536f0ea3689SLuigi Rizzo id = scan->nm_id + 1; 537f0ea3689SLuigi Rizzo if (id == 0) /* reserve 0 as error value */ 538f0ea3689SLuigi Rizzo id = 1; 539f0ea3689SLuigi Rizzo scan = scan->next; 540f0ea3689SLuigi Rizzo if (id != scan->nm_id) { 541f0ea3689SLuigi Rizzo nmd->nm_id = id; 542f0ea3689SLuigi Rizzo nmd->prev = scan->prev; 543f0ea3689SLuigi Rizzo nmd->next = scan; 544f0ea3689SLuigi Rizzo scan->prev->next = nmd; 545f0ea3689SLuigi Rizzo scan->prev = nmd; 546f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd; 547f0ea3689SLuigi Rizzo error = 0; 548f0ea3689SLuigi Rizzo break; 549f0ea3689SLuigi Rizzo } 550f0ea3689SLuigi Rizzo } while (scan != netmap_last_mem_d); 551f0ea3689SLuigi Rizzo 552f0ea3689SLuigi Rizzo return error; 553f0ea3689SLuigi Rizzo } 554f0ea3689SLuigi Rizzo 55537e3a6d3SLuigi Rizzo /* call with NMA_LOCK(&nm_mem) *not* held */ 55637e3a6d3SLuigi Rizzo static int 55737e3a6d3SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd) 55837e3a6d3SLuigi Rizzo { 55937e3a6d3SLuigi Rizzo int ret; 56037e3a6d3SLuigi Rizzo 56137e3a6d3SLuigi Rizzo NMA_LOCK(&nm_mem); 56237e3a6d3SLuigi Rizzo ret = nm_mem_assign_id_locked(nmd); 56337e3a6d3SLuigi Rizzo NMA_UNLOCK(&nm_mem); 56437e3a6d3SLuigi Rizzo 56537e3a6d3SLuigi Rizzo return ret; 56637e3a6d3SLuigi Rizzo } 56737e3a6d3SLuigi Rizzo 568f0ea3689SLuigi Rizzo static void 569f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd) 570f0ea3689SLuigi Rizzo { 571f0ea3689SLuigi Rizzo NMA_LOCK(&nm_mem); 572f0ea3689SLuigi Rizzo 573f0ea3689SLuigi Rizzo nmd->prev->next = nmd->next; 574f0ea3689SLuigi Rizzo nmd->next->prev = nmd->prev; 575f0ea3689SLuigi Rizzo 576f0ea3689SLuigi Rizzo if (netmap_last_mem_d == nmd) 577f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd->prev; 578f0ea3689SLuigi Rizzo 579f0ea3689SLuigi Rizzo nmd->prev = nmd->next = NULL; 580f0ea3689SLuigi Rizzo 581f0ea3689SLuigi Rizzo NMA_UNLOCK(&nm_mem); 582f0ea3689SLuigi Rizzo } 583f0ea3689SLuigi Rizzo 5844bf50f18SLuigi Rizzo static int 58537e3a6d3SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev) 5864bf50f18SLuigi Rizzo { 5874bf50f18SLuigi Rizzo int err = 0, id; 5884bf50f18SLuigi Rizzo id = nm_iommu_group_id(dev); 5894bf50f18SLuigi Rizzo if (netmap_verbose) 5904bf50f18SLuigi Rizzo D("iommu_group %d", id); 5914bf50f18SLuigi Rizzo 5924bf50f18SLuigi Rizzo NMA_LOCK(nmd); 5934bf50f18SLuigi Rizzo 5944bf50f18SLuigi Rizzo if (nmd->nm_grp < 0) 5954bf50f18SLuigi Rizzo nmd->nm_grp = id; 5964bf50f18SLuigi Rizzo 5974bf50f18SLuigi Rizzo if (nmd->nm_grp != id) 5984bf50f18SLuigi Rizzo nmd->lasterr = err = ENOMEM; 5994bf50f18SLuigi Rizzo 6004bf50f18SLuigi Rizzo NMA_UNLOCK(nmd); 6014bf50f18SLuigi Rizzo return err; 6024bf50f18SLuigi Rizzo } 603f0ea3689SLuigi Rizzo 604ccdc3305SLuigi Rizzo /* 6052579e2d7SLuigi Rizzo * First, find the allocator that contains the requested offset, 6062579e2d7SLuigi Rizzo * then locate the cluster through a lookup table. 607ccdc3305SLuigi Rizzo */ 608847bf383SLuigi Rizzo static vm_paddr_t 609847bf383SLuigi Rizzo netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset) 610ccdc3305SLuigi Rizzo { 611ccdc3305SLuigi Rizzo int i; 612ce3ee1e7SLuigi Rizzo vm_ooffset_t o = offset; 613ce3ee1e7SLuigi Rizzo vm_paddr_t pa; 614ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p; 615ccdc3305SLuigi Rizzo 616ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 617ce3ee1e7SLuigi Rizzo p = nmd->pools; 618ce3ee1e7SLuigi Rizzo 619ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) { 620ce3ee1e7SLuigi Rizzo if (offset >= p[i].memtotal) 621ccdc3305SLuigi Rizzo continue; 6222579e2d7SLuigi Rizzo // now lookup the cluster's address 62337e3a6d3SLuigi Rizzo #ifndef _WIN32 6244bf50f18SLuigi Rizzo pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) + 6258241616dSLuigi Rizzo offset % p[i]._objsize; 62637e3a6d3SLuigi Rizzo #else 62737e3a6d3SLuigi Rizzo pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr); 62837e3a6d3SLuigi Rizzo pa.QuadPart += offset % p[i]._objsize; 62937e3a6d3SLuigi Rizzo #endif 630ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 631ce3ee1e7SLuigi Rizzo return pa; 632ccdc3305SLuigi Rizzo } 6338241616dSLuigi Rizzo /* this is only in case of errors */ 634b1123b01SLuigi Rizzo D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o, 635ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal, 636ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 637ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal, 638ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 639ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal 640ce3ee1e7SLuigi Rizzo + p[NETMAP_BUF_POOL].memtotal); 641ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 64237e3a6d3SLuigi Rizzo #ifndef _WIN32 643ccdc3305SLuigi Rizzo return 0; // XXX bad address 64437e3a6d3SLuigi Rizzo #else 64537e3a6d3SLuigi Rizzo vm_paddr_t res; 64637e3a6d3SLuigi Rizzo res.QuadPart = 0; 64737e3a6d3SLuigi Rizzo return res; 64837e3a6d3SLuigi Rizzo #endif 64937e3a6d3SLuigi Rizzo } 65037e3a6d3SLuigi Rizzo 65137e3a6d3SLuigi Rizzo #ifdef _WIN32 65237e3a6d3SLuigi Rizzo 65337e3a6d3SLuigi Rizzo /* 65437e3a6d3SLuigi Rizzo * win32_build_virtual_memory_for_userspace 65537e3a6d3SLuigi Rizzo * 65637e3a6d3SLuigi Rizzo * This function get all the object making part of the pools and maps 65737e3a6d3SLuigi Rizzo * a contiguous virtual memory space for the userspace 65837e3a6d3SLuigi Rizzo * It works this way 65937e3a6d3SLuigi Rizzo * 1 - allocate a Memory Descriptor List wide as the sum 66037e3a6d3SLuigi Rizzo * of the memory needed for the pools 66137e3a6d3SLuigi Rizzo * 2 - cycle all the objects in every pool and for every object do 66237e3a6d3SLuigi Rizzo * 66337e3a6d3SLuigi Rizzo * 2a - cycle all the objects in every pool, get the list 66437e3a6d3SLuigi Rizzo * of the physical address descriptors 66537e3a6d3SLuigi Rizzo * 2b - calculate the offset in the array of pages desciptor in the 66637e3a6d3SLuigi Rizzo * main MDL 66737e3a6d3SLuigi Rizzo * 2c - copy the descriptors of the object in the main MDL 66837e3a6d3SLuigi Rizzo * 66937e3a6d3SLuigi Rizzo * 3 - return the resulting MDL that needs to be mapped in userland 67037e3a6d3SLuigi Rizzo * 67137e3a6d3SLuigi Rizzo * In this way we will have an MDL that describes all the memory for the 67237e3a6d3SLuigi Rizzo * objects in a single object 67337e3a6d3SLuigi Rizzo */ 67437e3a6d3SLuigi Rizzo 67537e3a6d3SLuigi Rizzo PMDL 67637e3a6d3SLuigi Rizzo win32_build_user_vm_map(struct netmap_mem_d* nmd) 67737e3a6d3SLuigi Rizzo { 67837e3a6d3SLuigi Rizzo int i, j; 67937e3a6d3SLuigi Rizzo u_int memsize, memflags, ofs = 0; 68037e3a6d3SLuigi Rizzo PMDL mainMdl, tempMdl; 68137e3a6d3SLuigi Rizzo 68237e3a6d3SLuigi Rizzo if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) { 68337e3a6d3SLuigi Rizzo D("memory not finalised yet"); 68437e3a6d3SLuigi Rizzo return NULL; 68537e3a6d3SLuigi Rizzo } 68637e3a6d3SLuigi Rizzo 68737e3a6d3SLuigi Rizzo mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL); 68837e3a6d3SLuigi Rizzo if (mainMdl == NULL) { 68937e3a6d3SLuigi Rizzo D("failed to allocate mdl"); 69037e3a6d3SLuigi Rizzo return NULL; 69137e3a6d3SLuigi Rizzo } 69237e3a6d3SLuigi Rizzo 69337e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 69437e3a6d3SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 69537e3a6d3SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[i]; 69637e3a6d3SLuigi Rizzo int clsz = p->_clustsize; 69737e3a6d3SLuigi Rizzo int clobjs = p->_clustentries; /* objects per cluster */ 69837e3a6d3SLuigi Rizzo int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz); 69937e3a6d3SLuigi Rizzo PPFN_NUMBER pSrc, pDst; 70037e3a6d3SLuigi Rizzo 70137e3a6d3SLuigi Rizzo /* each pool has a different cluster size so we need to reallocate */ 70237e3a6d3SLuigi Rizzo tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL); 70337e3a6d3SLuigi Rizzo if (tempMdl == NULL) { 70437e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 70537e3a6d3SLuigi Rizzo D("fail to allocate tempMdl"); 70637e3a6d3SLuigi Rizzo IoFreeMdl(mainMdl); 70737e3a6d3SLuigi Rizzo return NULL; 70837e3a6d3SLuigi Rizzo } 70937e3a6d3SLuigi Rizzo pSrc = MmGetMdlPfnArray(tempMdl); 71037e3a6d3SLuigi Rizzo /* create one entry per cluster, the lut[] has one entry per object */ 71137e3a6d3SLuigi Rizzo for (j = 0; j < p->numclusters; j++, ofs += clsz) { 71237e3a6d3SLuigi Rizzo pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)]; 71337e3a6d3SLuigi Rizzo MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz); 71437e3a6d3SLuigi Rizzo MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */ 71537e3a6d3SLuigi Rizzo RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */ 71637e3a6d3SLuigi Rizzo mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */ 71737e3a6d3SLuigi Rizzo } 71837e3a6d3SLuigi Rizzo IoFreeMdl(tempMdl); 71937e3a6d3SLuigi Rizzo } 72037e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 72137e3a6d3SLuigi Rizzo return mainMdl; 72237e3a6d3SLuigi Rizzo } 72337e3a6d3SLuigi Rizzo 72437e3a6d3SLuigi Rizzo #endif /* _WIN32 */ 72537e3a6d3SLuigi Rizzo 72637e3a6d3SLuigi Rizzo /* 72737e3a6d3SLuigi Rizzo * helper function for OS-specific mmap routines (currently only windows). 72837e3a6d3SLuigi Rizzo * Given an nmd and a pool index, returns the cluster size and number of clusters. 72937e3a6d3SLuigi Rizzo * Returns 0 if memory is finalised and the pool is valid, otherwise 1. 73037e3a6d3SLuigi Rizzo * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change. 73137e3a6d3SLuigi Rizzo */ 73237e3a6d3SLuigi Rizzo 73337e3a6d3SLuigi Rizzo int 73437e3a6d3SLuigi Rizzo netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters) 73537e3a6d3SLuigi Rizzo { 73637e3a6d3SLuigi Rizzo if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR) 73737e3a6d3SLuigi Rizzo return 1; /* invalid arguments */ 73837e3a6d3SLuigi Rizzo // NMA_LOCK_ASSERT(nmd); 73937e3a6d3SLuigi Rizzo if (!(nmd->flags & NETMAP_MEM_FINALIZED)) { 74037e3a6d3SLuigi Rizzo *clustsize = *numclusters = 0; 74137e3a6d3SLuigi Rizzo return 1; /* not ready yet */ 74237e3a6d3SLuigi Rizzo } 74337e3a6d3SLuigi Rizzo *clustsize = nmd->pools[pool]._clustsize; 74437e3a6d3SLuigi Rizzo *numclusters = nmd->pools[pool].numclusters; 74537e3a6d3SLuigi Rizzo return 0; /* success */ 746ccdc3305SLuigi Rizzo } 747ccdc3305SLuigi Rizzo 748847bf383SLuigi Rizzo static int 749847bf383SLuigi Rizzo netmap_mem2_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags, 750f0ea3689SLuigi Rizzo nm_memid_t *id) 751ce3ee1e7SLuigi Rizzo { 752ce3ee1e7SLuigi Rizzo int error = 0; 753ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 754847bf383SLuigi Rizzo error = netmap_mem_config(nmd); 755ce3ee1e7SLuigi Rizzo if (error) 756ce3ee1e7SLuigi Rizzo goto out; 7574bf50f18SLuigi Rizzo if (size) { 758ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 759ce3ee1e7SLuigi Rizzo *size = nmd->nm_totalsize; 760ce3ee1e7SLuigi Rizzo } else { 761ce3ee1e7SLuigi Rizzo int i; 762ce3ee1e7SLuigi Rizzo *size = 0; 763ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 764ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = nmd->pools + i; 765ce3ee1e7SLuigi Rizzo *size += (p->_numclusters * p->_clustsize); 766ce3ee1e7SLuigi Rizzo } 767ce3ee1e7SLuigi Rizzo } 7684bf50f18SLuigi Rizzo } 7694bf50f18SLuigi Rizzo if (memflags) 770ce3ee1e7SLuigi Rizzo *memflags = nmd->flags; 7714bf50f18SLuigi Rizzo if (id) 772f0ea3689SLuigi Rizzo *id = nmd->nm_id; 773ce3ee1e7SLuigi Rizzo out: 774ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 775ce3ee1e7SLuigi Rizzo return error; 776ce3ee1e7SLuigi Rizzo } 777ce3ee1e7SLuigi Rizzo 778ccdc3305SLuigi Rizzo /* 779ccdc3305SLuigi Rizzo * we store objects by kernel address, need to find the offset 780ccdc3305SLuigi Rizzo * within the pool to export the value to userspace. 781ccdc3305SLuigi Rizzo * Algorithm: scan until we find the cluster, then add the 782ccdc3305SLuigi Rizzo * actual offset in the cluster 783ccdc3305SLuigi Rizzo */ 784ce2cb792SLuigi Rizzo static ssize_t 785ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr) 786ccdc3305SLuigi Rizzo { 787ce3ee1e7SLuigi Rizzo int i, k = p->_clustentries, n = p->objtotal; 788ccdc3305SLuigi Rizzo ssize_t ofs = 0; 789ccdc3305SLuigi Rizzo 790ccdc3305SLuigi Rizzo for (i = 0; i < n; i += k, ofs += p->_clustsize) { 791ccdc3305SLuigi Rizzo const char *base = p->lut[i].vaddr; 792ccdc3305SLuigi Rizzo ssize_t relofs = (const char *) vaddr - base; 793ccdc3305SLuigi Rizzo 794aa76317cSLuigi Rizzo if (relofs < 0 || relofs >= p->_clustsize) 795ccdc3305SLuigi Rizzo continue; 796ccdc3305SLuigi Rizzo 797ccdc3305SLuigi Rizzo ofs = ofs + relofs; 798ccdc3305SLuigi Rizzo ND("%s: return offset %d (cluster %d) for pointer %p", 799ccdc3305SLuigi Rizzo p->name, ofs, i, vaddr); 800ccdc3305SLuigi Rizzo return ofs; 801ccdc3305SLuigi Rizzo } 802ccdc3305SLuigi Rizzo D("address %p is not contained inside any cluster (%s)", 803ccdc3305SLuigi Rizzo vaddr, p->name); 804ccdc3305SLuigi Rizzo return 0; /* An error occurred */ 805ccdc3305SLuigi Rizzo } 806ccdc3305SLuigi Rizzo 807ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */ 808ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v) \ 809ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v)) 810ccdc3305SLuigi Rizzo 811ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v) \ 812ce3ee1e7SLuigi Rizzo ((n)->pools[NETMAP_IF_POOL].memtotal + \ 813ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v))) 814ccdc3305SLuigi Rizzo 815847bf383SLuigi Rizzo static ssize_t 816847bf383SLuigi Rizzo netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr) 817ce3ee1e7SLuigi Rizzo { 818ce3ee1e7SLuigi Rizzo ssize_t v; 819ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 820ce3ee1e7SLuigi Rizzo v = netmap_if_offset(nmd, addr); 821ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 822ce3ee1e7SLuigi Rizzo return v; 823ce3ee1e7SLuigi Rizzo } 824ce3ee1e7SLuigi Rizzo 8258241616dSLuigi Rizzo /* 8268241616dSLuigi Rizzo * report the index, and use start position as a hint, 8278241616dSLuigi Rizzo * otherwise buffer allocation becomes terribly expensive. 8288241616dSLuigi Rizzo */ 829ccdc3305SLuigi Rizzo static void * 830ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index) 831ccdc3305SLuigi Rizzo { 832ccdc3305SLuigi Rizzo uint32_t i = 0; /* index in the bitmap */ 83337e3a6d3SLuigi Rizzo uint32_t mask, j = 0; /* slot counter */ 834ccdc3305SLuigi Rizzo void *vaddr = NULL; 835ccdc3305SLuigi Rizzo 836ccdc3305SLuigi Rizzo if (len > p->_objsize) { 837ccdc3305SLuigi Rizzo D("%s request size %d too large", p->name, len); 838ccdc3305SLuigi Rizzo // XXX cannot reduce the size 839ccdc3305SLuigi Rizzo return NULL; 840ccdc3305SLuigi Rizzo } 841ccdc3305SLuigi Rizzo 842ccdc3305SLuigi Rizzo if (p->objfree == 0) { 843f9790aebSLuigi Rizzo D("no more %s objects", p->name); 844ccdc3305SLuigi Rizzo return NULL; 845ccdc3305SLuigi Rizzo } 8468241616dSLuigi Rizzo if (start) 8478241616dSLuigi Rizzo i = *start; 848ccdc3305SLuigi Rizzo 8498241616dSLuigi Rizzo /* termination is guaranteed by p->free, but better check bounds on i */ 8508241616dSLuigi Rizzo while (vaddr == NULL && i < p->bitmap_slots) { 851ccdc3305SLuigi Rizzo uint32_t cur = p->bitmap[i]; 852ccdc3305SLuigi Rizzo if (cur == 0) { /* bitmask is fully used */ 853ccdc3305SLuigi Rizzo i++; 854ccdc3305SLuigi Rizzo continue; 855ccdc3305SLuigi Rizzo } 856ccdc3305SLuigi Rizzo /* locate a slot */ 857ccdc3305SLuigi Rizzo for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) 858ccdc3305SLuigi Rizzo ; 859ccdc3305SLuigi Rizzo 860ccdc3305SLuigi Rizzo p->bitmap[i] &= ~mask; /* mark object as in use */ 861ccdc3305SLuigi Rizzo p->objfree--; 862ccdc3305SLuigi Rizzo 863ccdc3305SLuigi Rizzo vaddr = p->lut[i * 32 + j].vaddr; 8648241616dSLuigi Rizzo if (index) 8658241616dSLuigi Rizzo *index = i * 32 + j; 866ccdc3305SLuigi Rizzo } 86737e3a6d3SLuigi Rizzo ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr); 868ccdc3305SLuigi Rizzo 8698241616dSLuigi Rizzo if (start) 8708241616dSLuigi Rizzo *start = i; 871ccdc3305SLuigi Rizzo return vaddr; 872ccdc3305SLuigi Rizzo } 873ccdc3305SLuigi Rizzo 874ccdc3305SLuigi Rizzo 875ccdc3305SLuigi Rizzo /* 876f0ea3689SLuigi Rizzo * free by index, not by address. 877f0ea3689SLuigi Rizzo * XXX should we also cleanup the content ? 878ccdc3305SLuigi Rizzo */ 879f0ea3689SLuigi Rizzo static int 880ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j) 881ccdc3305SLuigi Rizzo { 882f0ea3689SLuigi Rizzo uint32_t *ptr, mask; 883f0ea3689SLuigi Rizzo 884ccdc3305SLuigi Rizzo if (j >= p->objtotal) { 885ccdc3305SLuigi Rizzo D("invalid index %u, max %u", j, p->objtotal); 886f0ea3689SLuigi Rizzo return 1; 887ccdc3305SLuigi Rizzo } 888f0ea3689SLuigi Rizzo ptr = &p->bitmap[j / 32]; 889f0ea3689SLuigi Rizzo mask = (1 << (j % 32)); 890f0ea3689SLuigi Rizzo if (*ptr & mask) { 891f0ea3689SLuigi Rizzo D("ouch, double free on buffer %d", j); 892f0ea3689SLuigi Rizzo return 1; 893f0ea3689SLuigi Rizzo } else { 894f0ea3689SLuigi Rizzo *ptr |= mask; 895ccdc3305SLuigi Rizzo p->objfree++; 896f0ea3689SLuigi Rizzo return 0; 897f0ea3689SLuigi Rizzo } 898ccdc3305SLuigi Rizzo } 899ccdc3305SLuigi Rizzo 900f0ea3689SLuigi Rizzo /* 901f0ea3689SLuigi Rizzo * free by address. This is slow but is only used for a few 902f0ea3689SLuigi Rizzo * objects (rings, nifp) 903f0ea3689SLuigi Rizzo */ 904ccdc3305SLuigi Rizzo static void 905ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr) 906ccdc3305SLuigi Rizzo { 907ce3ee1e7SLuigi Rizzo u_int i, j, n = p->numclusters; 908ccdc3305SLuigi Rizzo 909ce3ee1e7SLuigi Rizzo for (i = 0, j = 0; i < n; i++, j += p->_clustentries) { 910ce3ee1e7SLuigi Rizzo void *base = p->lut[i * p->_clustentries].vaddr; 911ccdc3305SLuigi Rizzo ssize_t relofs = (ssize_t) vaddr - (ssize_t) base; 912ccdc3305SLuigi Rizzo 913ccdc3305SLuigi Rizzo /* Given address, is out of the scope of the current cluster.*/ 914ede69cffSLuigi Rizzo if (vaddr < base || relofs >= p->_clustsize) 915ccdc3305SLuigi Rizzo continue; 916ccdc3305SLuigi Rizzo 917ccdc3305SLuigi Rizzo j = j + relofs / p->_objsize; 918ce3ee1e7SLuigi Rizzo /* KASSERT(j != 0, ("Cannot free object 0")); */ 919ccdc3305SLuigi Rizzo netmap_obj_free(p, j); 920ccdc3305SLuigi Rizzo return; 921ccdc3305SLuigi Rizzo } 922ae10d1afSLuigi Rizzo D("address %p is not contained inside any cluster (%s)", 923ccdc3305SLuigi Rizzo vaddr, p->name); 924ccdc3305SLuigi Rizzo } 925ccdc3305SLuigi Rizzo 9264bf50f18SLuigi Rizzo #define netmap_mem_bufsize(n) \ 9274bf50f18SLuigi Rizzo ((n)->pools[NETMAP_BUF_POOL]._objsize) 9284bf50f18SLuigi Rizzo 929ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL) 930ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v)) 931ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL) 932ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v)) 933ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index) \ 9344bf50f18SLuigi Rizzo netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index) 935ccdc3305SLuigi Rizzo 936ccdc3305SLuigi Rizzo 937f0ea3689SLuigi Rizzo #if 0 // XXX unused 938ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */ 939ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v) \ 940ce3ee1e7SLuigi Rizzo (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n)) 941f0ea3689SLuigi Rizzo #endif 942f0ea3689SLuigi Rizzo 943f0ea3689SLuigi Rizzo /* 944f0ea3689SLuigi Rizzo * allocate extra buffers in a linked list. 945f0ea3689SLuigi Rizzo * returns the actual number. 946f0ea3689SLuigi Rizzo */ 947f0ea3689SLuigi Rizzo uint32_t 948f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n) 949f0ea3689SLuigi Rizzo { 950f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 951f0ea3689SLuigi Rizzo uint32_t i, pos = 0; /* opaque, scan position in the bitmap */ 952f0ea3689SLuigi Rizzo 953f0ea3689SLuigi Rizzo NMA_LOCK(nmd); 954f0ea3689SLuigi Rizzo 955f0ea3689SLuigi Rizzo *head = 0; /* default, 'null' index ie empty list */ 956f0ea3689SLuigi Rizzo for (i = 0 ; i < n; i++) { 957f0ea3689SLuigi Rizzo uint32_t cur = *head; /* save current head */ 958f0ea3689SLuigi Rizzo uint32_t *p = netmap_buf_malloc(nmd, &pos, head); 959f0ea3689SLuigi Rizzo if (p == NULL) { 960f0ea3689SLuigi Rizzo D("no more buffers after %d of %d", i, n); 961f0ea3689SLuigi Rizzo *head = cur; /* restore */ 962f0ea3689SLuigi Rizzo break; 963f0ea3689SLuigi Rizzo } 96437e3a6d3SLuigi Rizzo ND(5, "allocate buffer %d -> %d", *head, cur); 965f0ea3689SLuigi Rizzo *p = cur; /* link to previous head */ 966f0ea3689SLuigi Rizzo } 967f0ea3689SLuigi Rizzo 968f0ea3689SLuigi Rizzo NMA_UNLOCK(nmd); 969f0ea3689SLuigi Rizzo 970f0ea3689SLuigi Rizzo return i; 971f0ea3689SLuigi Rizzo } 972f0ea3689SLuigi Rizzo 973f0ea3689SLuigi Rizzo static void 974f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head) 975f0ea3689SLuigi Rizzo { 976847bf383SLuigi Rizzo struct lut_entry *lut = na->na_lut.lut; 977f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 978f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 979f0ea3689SLuigi Rizzo uint32_t i, cur, *buf; 980f0ea3689SLuigi Rizzo 98137e3a6d3SLuigi Rizzo ND("freeing the extra list"); 982f0ea3689SLuigi Rizzo for (i = 0; head >=2 && head < p->objtotal; i++) { 983f0ea3689SLuigi Rizzo cur = head; 984f0ea3689SLuigi Rizzo buf = lut[head].vaddr; 985f0ea3689SLuigi Rizzo head = *buf; 986f0ea3689SLuigi Rizzo *buf = 0; 987f0ea3689SLuigi Rizzo if (netmap_obj_free(p, cur)) 988f0ea3689SLuigi Rizzo break; 989f0ea3689SLuigi Rizzo } 990f0ea3689SLuigi Rizzo if (head != 0) 991f0ea3689SLuigi Rizzo D("breaking with head %d", head); 99237e3a6d3SLuigi Rizzo if (netmap_verbose) 993f0ea3689SLuigi Rizzo D("freed %d buffers", i); 994f0ea3689SLuigi Rizzo } 995ccdc3305SLuigi Rizzo 996ccdc3305SLuigi Rizzo 9978241616dSLuigi Rizzo /* Return nonzero on error */ 9988241616dSLuigi Rizzo static int 999f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 1000ccdc3305SLuigi Rizzo { 1001ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1002ce3ee1e7SLuigi Rizzo u_int i = 0; /* slot counter */ 10038241616dSLuigi Rizzo uint32_t pos = 0; /* slot in p->bitmap */ 10048241616dSLuigi Rizzo uint32_t index = 0; /* buffer index */ 1005ccdc3305SLuigi Rizzo 1006ccdc3305SLuigi Rizzo for (i = 0; i < n; i++) { 1007ce3ee1e7SLuigi Rizzo void *vaddr = netmap_buf_malloc(nmd, &pos, &index); 1008ccdc3305SLuigi Rizzo if (vaddr == NULL) { 1009f9790aebSLuigi Rizzo D("no more buffers after %d of %d", i, n); 1010ccdc3305SLuigi Rizzo goto cleanup; 1011ccdc3305SLuigi Rizzo } 10128241616dSLuigi Rizzo slot[i].buf_idx = index; 1013ccdc3305SLuigi Rizzo slot[i].len = p->_objsize; 1014f9790aebSLuigi Rizzo slot[i].flags = 0; 1015ccdc3305SLuigi Rizzo } 1016ccdc3305SLuigi Rizzo 10178241616dSLuigi Rizzo ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos); 10188241616dSLuigi Rizzo return (0); 1019ccdc3305SLuigi Rizzo 1020ccdc3305SLuigi Rizzo cleanup: 10214cf8455fSEd Maste while (i > 0) { 10224cf8455fSEd Maste i--; 10238241616dSLuigi Rizzo netmap_obj_free(p, slot[i].buf_idx); 1024ccdc3305SLuigi Rizzo } 10258241616dSLuigi Rizzo bzero(slot, n * sizeof(slot[0])); 10268241616dSLuigi Rizzo return (ENOMEM); 1027ccdc3305SLuigi Rizzo } 1028ccdc3305SLuigi Rizzo 1029f0ea3689SLuigi Rizzo static void 1030f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index) 1031f0ea3689SLuigi Rizzo { 1032f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1033f0ea3689SLuigi Rizzo u_int i; 1034f0ea3689SLuigi Rizzo 1035f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 1036f0ea3689SLuigi Rizzo slot[i].buf_idx = index; 1037f0ea3689SLuigi Rizzo slot[i].len = p->_objsize; 1038f0ea3689SLuigi Rizzo slot[i].flags = 0; 1039f0ea3689SLuigi Rizzo } 1040f0ea3689SLuigi Rizzo } 1041f0ea3689SLuigi Rizzo 1042ccdc3305SLuigi Rizzo 1043ccdc3305SLuigi Rizzo static void 1044f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i) 1045ccdc3305SLuigi Rizzo { 1046ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 10478241616dSLuigi Rizzo 1048ccdc3305SLuigi Rizzo if (i < 2 || i >= p->objtotal) { 1049ccdc3305SLuigi Rizzo D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal); 1050ccdc3305SLuigi Rizzo return; 1051ccdc3305SLuigi Rizzo } 10528241616dSLuigi Rizzo netmap_obj_free(p, i); 1053ccdc3305SLuigi Rizzo } 1054ccdc3305SLuigi Rizzo 1055f0ea3689SLuigi Rizzo 1056f0ea3689SLuigi Rizzo static void 1057f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 1058f0ea3689SLuigi Rizzo { 1059f0ea3689SLuigi Rizzo u_int i; 1060f0ea3689SLuigi Rizzo 1061f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 1062f0ea3689SLuigi Rizzo if (slot[i].buf_idx > 2) 1063f0ea3689SLuigi Rizzo netmap_free_buf(nmd, slot[i].buf_idx); 1064f0ea3689SLuigi Rizzo } 1065f0ea3689SLuigi Rizzo } 1066f0ea3689SLuigi Rizzo 10678241616dSLuigi Rizzo static void 10688241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p) 10698241616dSLuigi Rizzo { 1070ce3ee1e7SLuigi Rizzo 10718241616dSLuigi Rizzo if (p == NULL) 10728241616dSLuigi Rizzo return; 10738241616dSLuigi Rizzo if (p->bitmap) 10748241616dSLuigi Rizzo free(p->bitmap, M_NETMAP); 10758241616dSLuigi Rizzo p->bitmap = NULL; 10768241616dSLuigi Rizzo if (p->lut) { 1077ce3ee1e7SLuigi Rizzo u_int i; 1078ce3ee1e7SLuigi Rizzo 1079dd4fcbc5SPatrick Kelsey /* 1080dd4fcbc5SPatrick Kelsey * Free each cluster allocated in 1081dd4fcbc5SPatrick Kelsey * netmap_finalize_obj_allocator(). The cluster start 1082dd4fcbc5SPatrick Kelsey * addresses are stored at multiples of p->_clusterentries 1083dd4fcbc5SPatrick Kelsey * in the lut. 1084dd4fcbc5SPatrick Kelsey */ 1085ce3ee1e7SLuigi Rizzo for (i = 0; i < p->objtotal; i += p->_clustentries) { 10868241616dSLuigi Rizzo if (p->lut[i].vaddr) 108737e3a6d3SLuigi Rizzo contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP); 10888241616dSLuigi Rizzo } 10898241616dSLuigi Rizzo bzero(p->lut, sizeof(struct lut_entry) * p->objtotal); 10908241616dSLuigi Rizzo #ifdef linux 10918241616dSLuigi Rizzo vfree(p->lut); 10928241616dSLuigi Rizzo #else 10938241616dSLuigi Rizzo free(p->lut, M_NETMAP); 10948241616dSLuigi Rizzo #endif 10958241616dSLuigi Rizzo } 10968241616dSLuigi Rizzo p->lut = NULL; 1097ce3ee1e7SLuigi Rizzo p->objtotal = 0; 1098ce3ee1e7SLuigi Rizzo p->memtotal = 0; 1099ce3ee1e7SLuigi Rizzo p->numclusters = 0; 1100ce3ee1e7SLuigi Rizzo p->objfree = 0; 11018241616dSLuigi Rizzo } 1102ccdc3305SLuigi Rizzo 1103ccdc3305SLuigi Rizzo /* 1104ccdc3305SLuigi Rizzo * Free all resources related to an allocator. 1105ccdc3305SLuigi Rizzo */ 1106ccdc3305SLuigi Rizzo static void 1107ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p) 1108ccdc3305SLuigi Rizzo { 1109ccdc3305SLuigi Rizzo if (p == NULL) 1110ccdc3305SLuigi Rizzo return; 11118241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 1112ccdc3305SLuigi Rizzo } 1113ccdc3305SLuigi Rizzo 1114ccdc3305SLuigi Rizzo /* 1115ccdc3305SLuigi Rizzo * We receive a request for objtotal objects, of size objsize each. 1116ccdc3305SLuigi Rizzo * Internally we may round up both numbers, as we allocate objects 1117ccdc3305SLuigi Rizzo * in small clusters multiple of the page size. 1118ce3ee1e7SLuigi Rizzo * We need to keep track of objtotal and clustentries, 1119ccdc3305SLuigi Rizzo * as they are needed when freeing memory. 1120ccdc3305SLuigi Rizzo * 1121ccdc3305SLuigi Rizzo * XXX note -- userspace needs the buffers to be contiguous, 1122ccdc3305SLuigi Rizzo * so we cannot afford gaps at the end of a cluster. 1123ccdc3305SLuigi Rizzo */ 11248241616dSLuigi Rizzo 11258241616dSLuigi Rizzo 11268241616dSLuigi Rizzo /* call with NMA_LOCK held */ 11278241616dSLuigi Rizzo static int 11288241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize) 1129ccdc3305SLuigi Rizzo { 1130ce3ee1e7SLuigi Rizzo int i; 1131ccdc3305SLuigi Rizzo u_int clustsize; /* the cluster size, multiple of page size */ 1132ccdc3305SLuigi Rizzo u_int clustentries; /* how many objects per entry */ 1133ccdc3305SLuigi Rizzo 1134ce3ee1e7SLuigi Rizzo /* we store the current request, so we can 1135ce3ee1e7SLuigi Rizzo * detect configuration changes later */ 1136ce3ee1e7SLuigi Rizzo p->r_objtotal = objtotal; 1137ce3ee1e7SLuigi Rizzo p->r_objsize = objsize; 1138ce3ee1e7SLuigi Rizzo 11394bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE (1<<22) // 4 MB 114017885a7bSLuigi Rizzo #define LINE_ROUND NM_CACHE_ALIGN // 64 1141ccdc3305SLuigi Rizzo if (objsize >= MAX_CLUSTSIZE) { 1142ccdc3305SLuigi Rizzo /* we could do it but there is no point */ 1143ccdc3305SLuigi Rizzo D("unsupported allocation for %d bytes", objsize); 1144ce3ee1e7SLuigi Rizzo return EINVAL; 1145ccdc3305SLuigi Rizzo } 1146ccdc3305SLuigi Rizzo /* make sure objsize is a multiple of LINE_ROUND */ 1147ccdc3305SLuigi Rizzo i = (objsize & (LINE_ROUND - 1)); 1148ccdc3305SLuigi Rizzo if (i) { 1149ccdc3305SLuigi Rizzo D("XXX aligning object by %d bytes", LINE_ROUND - i); 1150ccdc3305SLuigi Rizzo objsize += LINE_ROUND - i; 1151ccdc3305SLuigi Rizzo } 11528241616dSLuigi Rizzo if (objsize < p->objminsize || objsize > p->objmaxsize) { 11538241616dSLuigi Rizzo D("requested objsize %d out of range [%d, %d]", 11548241616dSLuigi Rizzo objsize, p->objminsize, p->objmaxsize); 1155ce3ee1e7SLuigi Rizzo return EINVAL; 11568241616dSLuigi Rizzo } 11578241616dSLuigi Rizzo if (objtotal < p->nummin || objtotal > p->nummax) { 11588241616dSLuigi Rizzo D("requested objtotal %d out of range [%d, %d]", 11598241616dSLuigi Rizzo objtotal, p->nummin, p->nummax); 1160ce3ee1e7SLuigi Rizzo return EINVAL; 11618241616dSLuigi Rizzo } 1162ccdc3305SLuigi Rizzo /* 1163ccdc3305SLuigi Rizzo * Compute number of objects using a brute-force approach: 1164ccdc3305SLuigi Rizzo * given a max cluster size, 1165ccdc3305SLuigi Rizzo * we try to fill it with objects keeping track of the 1166ccdc3305SLuigi Rizzo * wasted space to the next page boundary. 1167ccdc3305SLuigi Rizzo */ 1168ccdc3305SLuigi Rizzo for (clustentries = 0, i = 1;; i++) { 1169ccdc3305SLuigi Rizzo u_int delta, used = i * objsize; 1170ccdc3305SLuigi Rizzo if (used > MAX_CLUSTSIZE) 1171ccdc3305SLuigi Rizzo break; 1172ccdc3305SLuigi Rizzo delta = used % PAGE_SIZE; 1173ccdc3305SLuigi Rizzo if (delta == 0) { // exact solution 1174ccdc3305SLuigi Rizzo clustentries = i; 1175ccdc3305SLuigi Rizzo break; 1176ccdc3305SLuigi Rizzo } 1177ccdc3305SLuigi Rizzo } 11784bf50f18SLuigi Rizzo /* exact solution not found */ 11794bf50f18SLuigi Rizzo if (clustentries == 0) { 11804bf50f18SLuigi Rizzo D("unsupported allocation for %d bytes", objsize); 11814bf50f18SLuigi Rizzo return EINVAL; 11824bf50f18SLuigi Rizzo } 11834bf50f18SLuigi Rizzo /* compute clustsize */ 1184ccdc3305SLuigi Rizzo clustsize = clustentries * objsize; 1185ae10d1afSLuigi Rizzo if (netmap_verbose) 1186ccdc3305SLuigi Rizzo D("objsize %d clustsize %d objects %d", 1187ccdc3305SLuigi Rizzo objsize, clustsize, clustentries); 1188ccdc3305SLuigi Rizzo 1189ccdc3305SLuigi Rizzo /* 1190ccdc3305SLuigi Rizzo * The number of clusters is n = ceil(objtotal/clustentries) 1191ccdc3305SLuigi Rizzo * objtotal' = n * clustentries 1192ccdc3305SLuigi Rizzo */ 1193ce3ee1e7SLuigi Rizzo p->_clustentries = clustentries; 1194ccdc3305SLuigi Rizzo p->_clustsize = clustsize; 1195ce3ee1e7SLuigi Rizzo p->_numclusters = (objtotal + clustentries - 1) / clustentries; 1196ce3ee1e7SLuigi Rizzo 1197ce3ee1e7SLuigi Rizzo /* actual values (may be larger than requested) */ 11988241616dSLuigi Rizzo p->_objsize = objsize; 1199ce3ee1e7SLuigi Rizzo p->_objtotal = p->_numclusters * clustentries; 1200ccdc3305SLuigi Rizzo 12018241616dSLuigi Rizzo return 0; 12028241616dSLuigi Rizzo } 12038241616dSLuigi Rizzo 120437e3a6d3SLuigi Rizzo static struct lut_entry * 120537e3a6d3SLuigi Rizzo nm_alloc_lut(u_int nobj) 120637e3a6d3SLuigi Rizzo { 120737e3a6d3SLuigi Rizzo size_t n = sizeof(struct lut_entry) * nobj; 120837e3a6d3SLuigi Rizzo struct lut_entry *lut; 120937e3a6d3SLuigi Rizzo #ifdef linux 121037e3a6d3SLuigi Rizzo lut = vmalloc(n); 121137e3a6d3SLuigi Rizzo #else 121237e3a6d3SLuigi Rizzo lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO); 121337e3a6d3SLuigi Rizzo #endif 121437e3a6d3SLuigi Rizzo return lut; 121537e3a6d3SLuigi Rizzo } 12168241616dSLuigi Rizzo 12178241616dSLuigi Rizzo /* call with NMA_LOCK held */ 12188241616dSLuigi Rizzo static int 12198241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p) 12208241616dSLuigi Rizzo { 1221ce3ee1e7SLuigi Rizzo int i; /* must be signed */ 1222ce3ee1e7SLuigi Rizzo size_t n; 1223ce3ee1e7SLuigi Rizzo 1224ce3ee1e7SLuigi Rizzo /* optimistically assume we have enough memory */ 1225ce3ee1e7SLuigi Rizzo p->numclusters = p->_numclusters; 1226ce3ee1e7SLuigi Rizzo p->objtotal = p->_objtotal; 12278241616dSLuigi Rizzo 122837e3a6d3SLuigi Rizzo p->lut = nm_alloc_lut(p->objtotal); 1229ccdc3305SLuigi Rizzo if (p->lut == NULL) { 123037e3a6d3SLuigi Rizzo D("Unable to create lookup table for '%s'", p->name); 1231ccdc3305SLuigi Rizzo goto clean; 1232ccdc3305SLuigi Rizzo } 1233ccdc3305SLuigi Rizzo 1234ccdc3305SLuigi Rizzo /* Allocate the bitmap */ 1235ccdc3305SLuigi Rizzo n = (p->objtotal + 31) / 32; 1236d2b91851SEd Maste p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO); 1237ccdc3305SLuigi Rizzo if (p->bitmap == NULL) { 1238ce3ee1e7SLuigi Rizzo D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n, 12398241616dSLuigi Rizzo p->name); 1240ccdc3305SLuigi Rizzo goto clean; 1241ccdc3305SLuigi Rizzo } 12428241616dSLuigi Rizzo p->bitmap_slots = n; 1243ccdc3305SLuigi Rizzo 1244ccdc3305SLuigi Rizzo /* 1245ccdc3305SLuigi Rizzo * Allocate clusters, init pointers and bitmap 1246ccdc3305SLuigi Rizzo */ 1247ce3ee1e7SLuigi Rizzo 1248ce3ee1e7SLuigi Rizzo n = p->_clustsize; 1249ce3ee1e7SLuigi Rizzo for (i = 0; i < (int)p->objtotal;) { 1250ce3ee1e7SLuigi Rizzo int lim = i + p->_clustentries; 1251ccdc3305SLuigi Rizzo char *clust; 1252ccdc3305SLuigi Rizzo 125337e3a6d3SLuigi Rizzo /* 125437e3a6d3SLuigi Rizzo * XXX Note, we only need contigmalloc() for buffers attached 125537e3a6d3SLuigi Rizzo * to native interfaces. In all other cases (nifp, netmap rings 125637e3a6d3SLuigi Rizzo * and even buffers for VALE ports or emulated interfaces) we 125737e3a6d3SLuigi Rizzo * can live with standard malloc, because the hardware will not 125837e3a6d3SLuigi Rizzo * access the pages directly. 125937e3a6d3SLuigi Rizzo */ 1260ce3ee1e7SLuigi Rizzo clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO, 1261ce3ee1e7SLuigi Rizzo (size_t)0, -1UL, PAGE_SIZE, 0); 1262ccdc3305SLuigi Rizzo if (clust == NULL) { 1263ccdc3305SLuigi Rizzo /* 1264ccdc3305SLuigi Rizzo * If we get here, there is a severe memory shortage, 1265ccdc3305SLuigi Rizzo * so halve the allocated memory to reclaim some. 1266ccdc3305SLuigi Rizzo */ 1267ccdc3305SLuigi Rizzo D("Unable to create cluster at %d for '%s' allocator", 12688241616dSLuigi Rizzo i, p->name); 1269ce3ee1e7SLuigi Rizzo if (i < 2) /* nothing to halve */ 1270ce3ee1e7SLuigi Rizzo goto out; 1271ccdc3305SLuigi Rizzo lim = i / 2; 12728241616dSLuigi Rizzo for (i--; i >= lim; i--) { 1273ccdc3305SLuigi Rizzo p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) ); 1274ce3ee1e7SLuigi Rizzo if (i % p->_clustentries == 0 && p->lut[i].vaddr) 1275ccdc3305SLuigi Rizzo contigfree(p->lut[i].vaddr, 1276ce3ee1e7SLuigi Rizzo n, M_NETMAP); 1277dd4fcbc5SPatrick Kelsey p->lut[i].vaddr = NULL; 1278ccdc3305SLuigi Rizzo } 1279ce3ee1e7SLuigi Rizzo out: 1280ccdc3305SLuigi Rizzo p->objtotal = i; 1281ce3ee1e7SLuigi Rizzo /* we may have stopped in the middle of a cluster */ 1282ce3ee1e7SLuigi Rizzo p->numclusters = (i + p->_clustentries - 1) / p->_clustentries; 1283ccdc3305SLuigi Rizzo break; 1284ccdc3305SLuigi Rizzo } 1285dd4fcbc5SPatrick Kelsey /* 1286dd4fcbc5SPatrick Kelsey * Set bitmap and lut state for all buffers in the current 1287dd4fcbc5SPatrick Kelsey * cluster. 1288dd4fcbc5SPatrick Kelsey * 1289dd4fcbc5SPatrick Kelsey * [i, lim) is the set of buffer indexes that cover the 1290dd4fcbc5SPatrick Kelsey * current cluster. 1291dd4fcbc5SPatrick Kelsey * 1292dd4fcbc5SPatrick Kelsey * 'clust' is really the address of the current buffer in 1293dd4fcbc5SPatrick Kelsey * the current cluster as we index through it with a stride 1294dd4fcbc5SPatrick Kelsey * of p->_objsize. 1295dd4fcbc5SPatrick Kelsey */ 12968241616dSLuigi Rizzo for (; i < lim; i++, clust += p->_objsize) { 1297ccdc3305SLuigi Rizzo p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) ); 1298ccdc3305SLuigi Rizzo p->lut[i].vaddr = clust; 1299ccdc3305SLuigi Rizzo p->lut[i].paddr = vtophys(clust); 1300ccdc3305SLuigi Rizzo } 1301ccdc3305SLuigi Rizzo } 1302ce3ee1e7SLuigi Rizzo p->objfree = p->objtotal; 1303ce3ee1e7SLuigi Rizzo p->memtotal = p->numclusters * p->_clustsize; 1304ce3ee1e7SLuigi Rizzo if (p->objfree == 0) 1305ce3ee1e7SLuigi Rizzo goto clean; 1306ae10d1afSLuigi Rizzo if (netmap_verbose) 1307ccdc3305SLuigi Rizzo D("Pre-allocated %d clusters (%d/%dKB) for '%s'", 1308ce3ee1e7SLuigi Rizzo p->numclusters, p->_clustsize >> 10, 1309ce3ee1e7SLuigi Rizzo p->memtotal >> 10, p->name); 1310ccdc3305SLuigi Rizzo 13118241616dSLuigi Rizzo return 0; 1312ccdc3305SLuigi Rizzo 1313ccdc3305SLuigi Rizzo clean: 13148241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 13158241616dSLuigi Rizzo return ENOMEM; 13168241616dSLuigi Rizzo } 13178241616dSLuigi Rizzo 13188241616dSLuigi Rizzo /* call with lock held */ 13198241616dSLuigi Rizzo static int 1320ce3ee1e7SLuigi Rizzo netmap_memory_config_changed(struct netmap_mem_d *nmd) 13218241616dSLuigi Rizzo { 13228241616dSLuigi Rizzo int i; 13238241616dSLuigi Rizzo 13248241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1325ce3ee1e7SLuigi Rizzo if (nmd->pools[i].r_objsize != netmap_params[i].size || 1326ce3ee1e7SLuigi Rizzo nmd->pools[i].r_objtotal != netmap_params[i].num) 13278241616dSLuigi Rizzo return 1; 13288241616dSLuigi Rizzo } 13298241616dSLuigi Rizzo return 0; 13308241616dSLuigi Rizzo } 13318241616dSLuigi Rizzo 1332ce3ee1e7SLuigi Rizzo static void 1333ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd) 1334ce3ee1e7SLuigi Rizzo { 1335ce3ee1e7SLuigi Rizzo int i; 1336f0ea3689SLuigi Rizzo 1337f0ea3689SLuigi Rizzo if (netmap_verbose) 1338ce3ee1e7SLuigi Rizzo D("resetting %p", nmd); 1339ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1340ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 1341ce3ee1e7SLuigi Rizzo } 1342ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 1343ce3ee1e7SLuigi Rizzo } 1344ce3ee1e7SLuigi Rizzo 1345ce3ee1e7SLuigi Rizzo static int 13464bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na) 13474bf50f18SLuigi Rizzo { 13484bf50f18SLuigi Rizzo int i, lim = p->_objtotal; 13494bf50f18SLuigi Rizzo 13504bf50f18SLuigi Rizzo if (na->pdev == NULL) 13514bf50f18SLuigi Rizzo return 0; 13524bf50f18SLuigi Rizzo 135337e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 13544bf50f18SLuigi Rizzo (void)i; 13554bf50f18SLuigi Rizzo (void)lim; 13564bf50f18SLuigi Rizzo D("unsupported on FreeBSD"); 135737e3a6d3SLuigi Rizzo 135837e3a6d3SLuigi Rizzo #elif defined(_WIN32) 135937e3a6d3SLuigi Rizzo (void)i; 136037e3a6d3SLuigi Rizzo (void)lim; 136137e3a6d3SLuigi Rizzo D("unsupported on Windows"); //XXX_ale, really? 13624bf50f18SLuigi Rizzo #else /* linux */ 13634bf50f18SLuigi Rizzo for (i = 2; i < lim; i++) { 13644bf50f18SLuigi Rizzo netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr); 13654bf50f18SLuigi Rizzo } 13664bf50f18SLuigi Rizzo #endif /* linux */ 13674bf50f18SLuigi Rizzo 13684bf50f18SLuigi Rizzo return 0; 13694bf50f18SLuigi Rizzo } 13704bf50f18SLuigi Rizzo 13714bf50f18SLuigi Rizzo static int 13724bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na) 13734bf50f18SLuigi Rizzo { 137437e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 13754bf50f18SLuigi Rizzo D("unsupported on FreeBSD"); 137637e3a6d3SLuigi Rizzo #elif defined(_WIN32) 137737e3a6d3SLuigi Rizzo D("unsupported on Windows"); //XXX_ale, really? 13784bf50f18SLuigi Rizzo #else /* linux */ 13794bf50f18SLuigi Rizzo int i, lim = p->_objtotal; 13804bf50f18SLuigi Rizzo 13814bf50f18SLuigi Rizzo if (na->pdev == NULL) 13824bf50f18SLuigi Rizzo return 0; 13834bf50f18SLuigi Rizzo 13844bf50f18SLuigi Rizzo for (i = 2; i < lim; i++) { 13854bf50f18SLuigi Rizzo netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr, 13864bf50f18SLuigi Rizzo p->lut[i].vaddr); 13874bf50f18SLuigi Rizzo } 13884bf50f18SLuigi Rizzo #endif /* linux */ 13894bf50f18SLuigi Rizzo 13904bf50f18SLuigi Rizzo return 0; 13914bf50f18SLuigi Rizzo } 13924bf50f18SLuigi Rizzo 13934bf50f18SLuigi Rizzo static int 139437e3a6d3SLuigi Rizzo netmap_mem_init_shared_info(struct netmap_mem_d *nmd) 139537e3a6d3SLuigi Rizzo { 139637e3a6d3SLuigi Rizzo struct netmap_mem_shared_info *nms_info; 139737e3a6d3SLuigi Rizzo ssize_t base; 139837e3a6d3SLuigi Rizzo 139937e3a6d3SLuigi Rizzo /* Use the first slot in IF_POOL */ 140037e3a6d3SLuigi Rizzo nms_info = netmap_if_malloc(nmd, sizeof(*nms_info)); 140137e3a6d3SLuigi Rizzo if (nms_info == NULL) { 140237e3a6d3SLuigi Rizzo return ENOMEM; 140337e3a6d3SLuigi Rizzo } 140437e3a6d3SLuigi Rizzo 140537e3a6d3SLuigi Rizzo base = netmap_if_offset(nmd, nms_info); 140637e3a6d3SLuigi Rizzo 140737e3a6d3SLuigi Rizzo memcpy(&nms_info->up, &nms_if_blueprint, sizeof(nms_if_blueprint)); 140837e3a6d3SLuigi Rizzo nms_info->buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal + nmd->pools[NETMAP_RING_POOL].memtotal; 140937e3a6d3SLuigi Rizzo nms_info->buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal; 141037e3a6d3SLuigi Rizzo nms_info->buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize; 141137e3a6d3SLuigi Rizzo nms_info->totalsize = nmd->nm_totalsize; 141237e3a6d3SLuigi Rizzo nms_info->features = NMS_FEAT_BUF_POOL | NMS_FEAT_MEMSIZE; 141337e3a6d3SLuigi Rizzo 141437e3a6d3SLuigi Rizzo return 0; 141537e3a6d3SLuigi Rizzo } 141637e3a6d3SLuigi Rizzo 141737e3a6d3SLuigi Rizzo static int 1418ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd) 1419ce3ee1e7SLuigi Rizzo { 1420ce3ee1e7SLuigi Rizzo int i; 1421ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 1422ce3ee1e7SLuigi Rizzo return 0; 1423ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 1424ce3ee1e7SLuigi Rizzo nmd->nm_totalsize = 0; 1425ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1426ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]); 1427ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 1428ce3ee1e7SLuigi Rizzo goto error; 1429ce3ee1e7SLuigi Rizzo nmd->nm_totalsize += nmd->pools[i].memtotal; 1430ce3ee1e7SLuigi Rizzo } 1431ce3ee1e7SLuigi Rizzo /* buffers 0 and 1 are reserved */ 1432ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].objfree -= 2; 1433ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3; 1434ce3ee1e7SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 1435ce3ee1e7SLuigi Rizzo 143637e3a6d3SLuigi Rizzo /* expose info to the ptnetmap guest */ 143737e3a6d3SLuigi Rizzo nmd->lasterr = netmap_mem_init_shared_info(nmd); 143837e3a6d3SLuigi Rizzo if (nmd->lasterr) 143937e3a6d3SLuigi Rizzo goto error; 144037e3a6d3SLuigi Rizzo 1441f0ea3689SLuigi Rizzo if (netmap_verbose) 1442f0ea3689SLuigi Rizzo D("interfaces %d KB, rings %d KB, buffers %d MB", 1443ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_IF_POOL].memtotal >> 10, 1444ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_RING_POOL].memtotal >> 10, 1445ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].memtotal >> 20); 1446ce3ee1e7SLuigi Rizzo 1447f0ea3689SLuigi Rizzo if (netmap_verbose) 1448ce3ee1e7SLuigi Rizzo D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree); 1449ce3ee1e7SLuigi Rizzo 1450ce3ee1e7SLuigi Rizzo 1451ce3ee1e7SLuigi Rizzo return 0; 1452ce3ee1e7SLuigi Rizzo error: 1453ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(nmd); 1454ce3ee1e7SLuigi Rizzo return nmd->lasterr; 1455ce3ee1e7SLuigi Rizzo } 1456ce3ee1e7SLuigi Rizzo 1457ce3ee1e7SLuigi Rizzo 1458ce3ee1e7SLuigi Rizzo 1459847bf383SLuigi Rizzo static void 1460ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(struct netmap_mem_d *nmd) 1461ce3ee1e7SLuigi Rizzo { 1462ce3ee1e7SLuigi Rizzo if (nmd == NULL) 1463ce3ee1e7SLuigi Rizzo return; 1464f0ea3689SLuigi Rizzo if (netmap_verbose) 1465ce3ee1e7SLuigi Rizzo D("deleting %p", nmd); 1466847bf383SLuigi Rizzo if (nmd->active > 0) 1467847bf383SLuigi Rizzo D("bug: deleting mem allocator with active=%d!", nmd->active); 1468f0ea3689SLuigi Rizzo nm_mem_release_id(nmd); 1469f0ea3689SLuigi Rizzo if (netmap_verbose) 1470ce3ee1e7SLuigi Rizzo D("done deleting %p", nmd); 1471ce3ee1e7SLuigi Rizzo NMA_LOCK_DESTROY(nmd); 1472ce3ee1e7SLuigi Rizzo free(nmd, M_DEVBUF); 1473ce3ee1e7SLuigi Rizzo } 1474ce3ee1e7SLuigi Rizzo 1475ce3ee1e7SLuigi Rizzo static int 1476ce3ee1e7SLuigi Rizzo netmap_mem_private_config(struct netmap_mem_d *nmd) 1477ce3ee1e7SLuigi Rizzo { 1478ce3ee1e7SLuigi Rizzo /* nothing to do, we are configured on creation 1479ce3ee1e7SLuigi Rizzo * and configuration never changes thereafter 1480ce3ee1e7SLuigi Rizzo */ 1481ce3ee1e7SLuigi Rizzo return 0; 1482ce3ee1e7SLuigi Rizzo } 1483ce3ee1e7SLuigi Rizzo 1484ce3ee1e7SLuigi Rizzo static int 1485ce3ee1e7SLuigi Rizzo netmap_mem_private_finalize(struct netmap_mem_d *nmd) 1486ce3ee1e7SLuigi Rizzo { 1487ce3ee1e7SLuigi Rizzo int err; 1488ce3ee1e7SLuigi Rizzo err = netmap_mem_finalize_all(nmd); 148937e3a6d3SLuigi Rizzo if (!err) 149037e3a6d3SLuigi Rizzo nmd->active++; 1491ce3ee1e7SLuigi Rizzo return err; 1492ce3ee1e7SLuigi Rizzo 1493ce3ee1e7SLuigi Rizzo } 1494ce3ee1e7SLuigi Rizzo 1495f9790aebSLuigi Rizzo static void 1496f9790aebSLuigi Rizzo netmap_mem_private_deref(struct netmap_mem_d *nmd) 1497ce3ee1e7SLuigi Rizzo { 1498847bf383SLuigi Rizzo if (--nmd->active <= 0) 1499ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(nmd); 1500ce3ee1e7SLuigi Rizzo } 1501ce3ee1e7SLuigi Rizzo 1502f0ea3689SLuigi Rizzo 1503f0ea3689SLuigi Rizzo /* 1504f0ea3689SLuigi Rizzo * allocator for private memory 1505f0ea3689SLuigi Rizzo */ 1506ce3ee1e7SLuigi Rizzo struct netmap_mem_d * 1507f0ea3689SLuigi Rizzo netmap_mem_private_new(const char *name, u_int txr, u_int txd, 1508f0ea3689SLuigi Rizzo u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr) 1509ce3ee1e7SLuigi Rizzo { 1510ce3ee1e7SLuigi Rizzo struct netmap_mem_d *d = NULL; 1511ce3ee1e7SLuigi Rizzo struct netmap_obj_params p[NETMAP_POOLS_NR]; 1512f0ea3689SLuigi Rizzo int i, err; 1513f0ea3689SLuigi Rizzo u_int v, maxd; 1514ce3ee1e7SLuigi Rizzo 1515ce3ee1e7SLuigi Rizzo d = malloc(sizeof(struct netmap_mem_d), 1516ce3ee1e7SLuigi Rizzo M_DEVBUF, M_NOWAIT | M_ZERO); 1517f0ea3689SLuigi Rizzo if (d == NULL) { 1518f0ea3689SLuigi Rizzo err = ENOMEM; 1519f0ea3689SLuigi Rizzo goto error; 1520f0ea3689SLuigi Rizzo } 1521ce3ee1e7SLuigi Rizzo 1522ce3ee1e7SLuigi Rizzo *d = nm_blueprint; 1523ce3ee1e7SLuigi Rizzo 1524f0ea3689SLuigi Rizzo err = nm_mem_assign_id(d); 1525f0ea3689SLuigi Rizzo if (err) 1526f0ea3689SLuigi Rizzo goto error; 1527f0ea3689SLuigi Rizzo 1528f0ea3689SLuigi Rizzo /* account for the fake host rings */ 1529ce3ee1e7SLuigi Rizzo txr++; 1530ce3ee1e7SLuigi Rizzo rxr++; 1531ce3ee1e7SLuigi Rizzo 1532f0ea3689SLuigi Rizzo /* copy the min values */ 1533f0ea3689SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1534f0ea3689SLuigi Rizzo p[i] = netmap_min_priv_params[i]; 1535f0ea3689SLuigi Rizzo } 1536f0ea3689SLuigi Rizzo 1537f0ea3689SLuigi Rizzo /* possibly increase them to fit user request */ 1538f0ea3689SLuigi Rizzo v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr); 1539f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].size < v) 1540f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].size = v; 1541f0ea3689SLuigi Rizzo v = 2 + 4 * npipes; 1542f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].num < v) 1543f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].num = v; 1544f0ea3689SLuigi Rizzo maxd = (txd > rxd) ? txd : rxd; 1545f0ea3689SLuigi Rizzo v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd; 1546f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].size < v) 1547f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].size = v; 1548f0ea3689SLuigi Rizzo /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake) 1549f0ea3689SLuigi Rizzo * and two rx rings (again, 1 normal and 1 fake host) 1550f0ea3689SLuigi Rizzo */ 1551f0ea3689SLuigi Rizzo v = txr + rxr + 8 * npipes; 1552f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].num < v) 1553f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].num = v; 1554f0ea3689SLuigi Rizzo /* for each pipe we only need the buffers for the 4 "real" rings. 1555f0ea3689SLuigi Rizzo * On the other end, the pipe ring dimension may be different from 1556f0ea3689SLuigi Rizzo * the parent port ring dimension. As a compromise, we allocate twice the 1557f0ea3689SLuigi Rizzo * space actually needed if the pipe rings were the same size as the parent rings 1558f0ea3689SLuigi Rizzo */ 1559f0ea3689SLuigi Rizzo v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs; 1560f0ea3689SLuigi Rizzo /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */ 1561f0ea3689SLuigi Rizzo if (p[NETMAP_BUF_POOL].num < v) 1562f0ea3689SLuigi Rizzo p[NETMAP_BUF_POOL].num = v; 1563f0ea3689SLuigi Rizzo 1564f0ea3689SLuigi Rizzo if (netmap_verbose) 1565ce3ee1e7SLuigi Rizzo D("req if %d*%d ring %d*%d buf %d*%d", 1566ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].num, 1567ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].size, 1568ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].num, 1569ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].size, 1570ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].num, 1571ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].size); 1572ce3ee1e7SLuigi Rizzo 1573ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1574ce3ee1e7SLuigi Rizzo snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ, 1575ce3ee1e7SLuigi Rizzo nm_blueprint.pools[i].name, 1576ce3ee1e7SLuigi Rizzo name); 1577f0ea3689SLuigi Rizzo err = netmap_config_obj_allocator(&d->pools[i], 1578f0ea3689SLuigi Rizzo p[i].num, p[i].size); 1579f0ea3689SLuigi Rizzo if (err) 1580ce3ee1e7SLuigi Rizzo goto error; 1581ce3ee1e7SLuigi Rizzo } 1582ce3ee1e7SLuigi Rizzo 1583ce3ee1e7SLuigi Rizzo d->flags &= ~NETMAP_MEM_FINALIZED; 1584ce3ee1e7SLuigi Rizzo 1585ce3ee1e7SLuigi Rizzo NMA_LOCK_INIT(d); 1586ce3ee1e7SLuigi Rizzo 1587ce3ee1e7SLuigi Rizzo return d; 1588ce3ee1e7SLuigi Rizzo error: 1589ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(d); 1590f0ea3689SLuigi Rizzo if (perr) 1591f0ea3689SLuigi Rizzo *perr = err; 1592ce3ee1e7SLuigi Rizzo return NULL; 1593ce3ee1e7SLuigi Rizzo } 1594ce3ee1e7SLuigi Rizzo 15958241616dSLuigi Rizzo 15968241616dSLuigi Rizzo /* call with lock held */ 15978241616dSLuigi Rizzo static int 1598ce3ee1e7SLuigi Rizzo netmap_mem_global_config(struct netmap_mem_d *nmd) 15998241616dSLuigi Rizzo { 16008241616dSLuigi Rizzo int i; 16018241616dSLuigi Rizzo 1602847bf383SLuigi Rizzo if (nmd->active) 1603ce3ee1e7SLuigi Rizzo /* already in use, we cannot change the configuration */ 1604ce3ee1e7SLuigi Rizzo goto out; 1605ce3ee1e7SLuigi Rizzo 1606ce3ee1e7SLuigi Rizzo if (!netmap_memory_config_changed(nmd)) 16078241616dSLuigi Rizzo goto out; 16088241616dSLuigi Rizzo 1609847bf383SLuigi Rizzo ND("reconfiguring"); 16108241616dSLuigi Rizzo 1611ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 16128241616dSLuigi Rizzo /* reset previous allocation */ 16138241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1614ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 16158241616dSLuigi Rizzo } 1616ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 16178241616dSLuigi Rizzo } 16188241616dSLuigi Rizzo 16198241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1620ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i], 16218241616dSLuigi Rizzo netmap_params[i].num, netmap_params[i].size); 1622ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 16238241616dSLuigi Rizzo goto out; 16248241616dSLuigi Rizzo } 16258241616dSLuigi Rizzo 16268241616dSLuigi Rizzo out: 16278241616dSLuigi Rizzo 1628ce3ee1e7SLuigi Rizzo return nmd->lasterr; 16298241616dSLuigi Rizzo } 16308241616dSLuigi Rizzo 16318241616dSLuigi Rizzo static int 1632ce3ee1e7SLuigi Rizzo netmap_mem_global_finalize(struct netmap_mem_d *nmd) 16338241616dSLuigi Rizzo { 1634ce3ee1e7SLuigi Rizzo int err; 16358241616dSLuigi Rizzo 16368241616dSLuigi Rizzo /* update configuration if changed */ 1637ce3ee1e7SLuigi Rizzo if (netmap_mem_global_config(nmd)) 163837e3a6d3SLuigi Rizzo return nmd->lasterr; 16398241616dSLuigi Rizzo 1640847bf383SLuigi Rizzo nmd->active++; 1641ce3ee1e7SLuigi Rizzo 1642ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 16438241616dSLuigi Rizzo /* may happen if config is not changed */ 16448241616dSLuigi Rizzo ND("nothing to do"); 16458241616dSLuigi Rizzo goto out; 16468241616dSLuigi Rizzo } 16478241616dSLuigi Rizzo 1648ce3ee1e7SLuigi Rizzo if (netmap_mem_finalize_all(nmd)) 1649ce3ee1e7SLuigi Rizzo goto out; 16508241616dSLuigi Rizzo 1651ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 16528241616dSLuigi Rizzo 16538241616dSLuigi Rizzo out: 1654ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 1655847bf383SLuigi Rizzo nmd->active--; 1656ce3ee1e7SLuigi Rizzo err = nmd->lasterr; 16578241616dSLuigi Rizzo 1658ce3ee1e7SLuigi Rizzo return err; 16598241616dSLuigi Rizzo 1660ccdc3305SLuigi Rizzo } 1661ccdc3305SLuigi Rizzo 1662847bf383SLuigi Rizzo static void 1663847bf383SLuigi Rizzo netmap_mem_global_delete(struct netmap_mem_d *nmd) 1664ccdc3305SLuigi Rizzo { 16658241616dSLuigi Rizzo int i; 16668241616dSLuigi Rizzo 16678241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 16688241616dSLuigi Rizzo netmap_destroy_obj_allocator(&nm_mem.pools[i]); 16698241616dSLuigi Rizzo } 1670847bf383SLuigi Rizzo 1671ce3ee1e7SLuigi Rizzo NMA_LOCK_DESTROY(&nm_mem); 16728241616dSLuigi Rizzo } 16738241616dSLuigi Rizzo 1674847bf383SLuigi Rizzo int 1675847bf383SLuigi Rizzo netmap_mem_init(void) 1676847bf383SLuigi Rizzo { 1677847bf383SLuigi Rizzo NMA_LOCK_INIT(&nm_mem); 1678847bf383SLuigi Rizzo netmap_mem_get(&nm_mem); 1679847bf383SLuigi Rizzo return (0); 1680847bf383SLuigi Rizzo } 1681847bf383SLuigi Rizzo 1682847bf383SLuigi Rizzo void 1683847bf383SLuigi Rizzo netmap_mem_fini(void) 1684847bf383SLuigi Rizzo { 1685847bf383SLuigi Rizzo netmap_mem_put(&nm_mem); 1686847bf383SLuigi Rizzo } 1687847bf383SLuigi Rizzo 16888241616dSLuigi Rizzo static void 16898241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na) 16908241616dSLuigi Rizzo { 1691847bf383SLuigi Rizzo enum txrx t; 1692847bf383SLuigi Rizzo 1693847bf383SLuigi Rizzo for_rx_tx(t) { 1694847bf383SLuigi Rizzo u_int i; 169537e3a6d3SLuigi Rizzo for (i = 0; i < nma_get_nrings(na, t) + 1; i++) { 1696847bf383SLuigi Rizzo struct netmap_kring *kring = &NMR(na, t)[i]; 1697847bf383SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1698847bf383SLuigi Rizzo 169937e3a6d3SLuigi Rizzo if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) { 170037e3a6d3SLuigi Rizzo ND("skipping ring %s (ring %p, users %d)", 170137e3a6d3SLuigi Rizzo kring->name, ring, kring->users); 1702f0ea3689SLuigi Rizzo continue; 170337e3a6d3SLuigi Rizzo } 170437e3a6d3SLuigi Rizzo if (i != nma_get_nrings(na, t) || na->na_flags & NAF_HOST_RINGS) 1705f0ea3689SLuigi Rizzo netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots); 1706f0ea3689SLuigi Rizzo netmap_ring_free(na->nm_mem, ring); 1707f0ea3689SLuigi Rizzo kring->ring = NULL; 17088241616dSLuigi Rizzo } 1709ce3ee1e7SLuigi Rizzo } 1710ccdc3305SLuigi Rizzo } 1711ccdc3305SLuigi Rizzo 1712f9790aebSLuigi Rizzo /* call with NMA_LOCK held * 1713f9790aebSLuigi Rizzo * 1714f9790aebSLuigi Rizzo * Allocate netmap rings and buffers for this card 1715f9790aebSLuigi Rizzo * The rings are contiguous, but have variable size. 1716f0ea3689SLuigi Rizzo * The kring array must follow the layout described 1717f0ea3689SLuigi Rizzo * in netmap_krings_create(). 1718f9790aebSLuigi Rizzo */ 1719847bf383SLuigi Rizzo static int 1720847bf383SLuigi Rizzo netmap_mem2_rings_create(struct netmap_adapter *na) 1721f9790aebSLuigi Rizzo { 1722847bf383SLuigi Rizzo enum txrx t; 1723f9790aebSLuigi Rizzo 1724f9790aebSLuigi Rizzo NMA_LOCK(na->nm_mem); 1725f9790aebSLuigi Rizzo 1726847bf383SLuigi Rizzo for_rx_tx(t) { 1727847bf383SLuigi Rizzo u_int i; 1728847bf383SLuigi Rizzo 1729847bf383SLuigi Rizzo for (i = 0; i <= nma_get_nrings(na, t); i++) { 1730847bf383SLuigi Rizzo struct netmap_kring *kring = &NMR(na, t)[i]; 1731847bf383SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1732847bf383SLuigi Rizzo u_int len, ndesc; 1733847bf383SLuigi Rizzo 173437e3a6d3SLuigi Rizzo if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) { 173537e3a6d3SLuigi Rizzo /* uneeded, or already created by somebody else */ 173637e3a6d3SLuigi Rizzo ND("skipping ring %s", kring->name); 173737e3a6d3SLuigi Rizzo continue; 1738f0ea3689SLuigi Rizzo } 1739f9790aebSLuigi Rizzo ndesc = kring->nkr_num_slots; 1740f9790aebSLuigi Rizzo len = sizeof(struct netmap_ring) + 1741f9790aebSLuigi Rizzo ndesc * sizeof(struct netmap_slot); 1742f9790aebSLuigi Rizzo ring = netmap_ring_malloc(na->nm_mem, len); 1743f9790aebSLuigi Rizzo if (ring == NULL) { 1744847bf383SLuigi Rizzo D("Cannot allocate %s_ring", nm_txrx2str(t)); 1745f9790aebSLuigi Rizzo goto cleanup; 1746f9790aebSLuigi Rizzo } 1747f6c2a31fSLuigi Rizzo ND("txring at %p", ring); 1748f9790aebSLuigi Rizzo kring->ring = ring; 1749f9790aebSLuigi Rizzo *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc; 175017885a7bSLuigi Rizzo *(int64_t *)(uintptr_t)&ring->buf_ofs = 1751f9790aebSLuigi Rizzo (na->nm_mem->pools[NETMAP_IF_POOL].memtotal + 1752f9790aebSLuigi Rizzo na->nm_mem->pools[NETMAP_RING_POOL].memtotal) - 1753f9790aebSLuigi Rizzo netmap_ring_offset(na->nm_mem, ring); 1754f9790aebSLuigi Rizzo 175517885a7bSLuigi Rizzo /* copy values from kring */ 175617885a7bSLuigi Rizzo ring->head = kring->rhead; 175717885a7bSLuigi Rizzo ring->cur = kring->rcur; 175817885a7bSLuigi Rizzo ring->tail = kring->rtail; 1759f9790aebSLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->nr_buf_size = 17604bf50f18SLuigi Rizzo netmap_mem_bufsize(na->nm_mem); 1761f0ea3689SLuigi Rizzo ND("%s h %d c %d t %d", kring->name, 1762f0ea3689SLuigi Rizzo ring->head, ring->cur, ring->tail); 1763847bf383SLuigi Rizzo ND("initializing slots for %s_ring", nm_txrx2str(txrx)); 1764847bf383SLuigi Rizzo if (i != nma_get_nrings(na, t) || (na->na_flags & NAF_HOST_RINGS)) { 1765f0ea3689SLuigi Rizzo /* this is a real ring */ 1766f9790aebSLuigi Rizzo if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) { 1767847bf383SLuigi Rizzo D("Cannot allocate buffers for %s_ring", nm_txrx2str(t)); 1768f9790aebSLuigi Rizzo goto cleanup; 1769f9790aebSLuigi Rizzo } 1770f0ea3689SLuigi Rizzo } else { 1771847bf383SLuigi Rizzo /* this is a fake ring, set all indices to 0 */ 1772f0ea3689SLuigi Rizzo netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0); 1773f0ea3689SLuigi Rizzo } 1774847bf383SLuigi Rizzo /* ring info */ 1775847bf383SLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id; 1776847bf383SLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->dir = kring->tx; 1777f0ea3689SLuigi Rizzo } 1778f9790aebSLuigi Rizzo } 1779f9790aebSLuigi Rizzo 1780f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1781f9790aebSLuigi Rizzo 1782f9790aebSLuigi Rizzo return 0; 1783f9790aebSLuigi Rizzo 1784f9790aebSLuigi Rizzo cleanup: 1785f9790aebSLuigi Rizzo netmap_free_rings(na); 1786f9790aebSLuigi Rizzo 1787f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1788f9790aebSLuigi Rizzo 1789f9790aebSLuigi Rizzo return ENOMEM; 1790f9790aebSLuigi Rizzo } 1791f9790aebSLuigi Rizzo 1792847bf383SLuigi Rizzo static void 1793847bf383SLuigi Rizzo netmap_mem2_rings_delete(struct netmap_adapter *na) 1794f9790aebSLuigi Rizzo { 1795f9790aebSLuigi Rizzo /* last instance, release bufs and rings */ 1796f9790aebSLuigi Rizzo NMA_LOCK(na->nm_mem); 1797f9790aebSLuigi Rizzo 1798f9790aebSLuigi Rizzo netmap_free_rings(na); 1799f9790aebSLuigi Rizzo 1800f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1801f9790aebSLuigi Rizzo } 1802ccdc3305SLuigi Rizzo 1803ccdc3305SLuigi Rizzo 18048241616dSLuigi Rizzo /* call with NMA_LOCK held */ 1805ae10d1afSLuigi Rizzo /* 1806ae10d1afSLuigi Rizzo * Allocate the per-fd structure netmap_if. 1807ce3ee1e7SLuigi Rizzo * 1808ce3ee1e7SLuigi Rizzo * We assume that the configuration stored in na 1809ce3ee1e7SLuigi Rizzo * (number of tx/rx rings and descs) does not change while 1810ce3ee1e7SLuigi Rizzo * the interface is in netmap mode. 1811ae10d1afSLuigi Rizzo */ 1812847bf383SLuigi Rizzo static struct netmap_if * 1813847bf383SLuigi Rizzo netmap_mem2_if_new(struct netmap_adapter *na) 1814ccdc3305SLuigi Rizzo { 1815ccdc3305SLuigi Rizzo struct netmap_if *nifp; 1816ccdc3305SLuigi Rizzo ssize_t base; /* handy for relative offsets between rings and nifp */ 1817847bf383SLuigi Rizzo u_int i, len, n[NR_TXRX], ntot; 1818847bf383SLuigi Rizzo enum txrx t; 1819ccdc3305SLuigi Rizzo 1820847bf383SLuigi Rizzo ntot = 0; 1821847bf383SLuigi Rizzo for_rx_tx(t) { 1822f0ea3689SLuigi Rizzo /* account for the (eventually fake) host rings */ 1823847bf383SLuigi Rizzo n[t] = nma_get_nrings(na, t) + 1; 1824847bf383SLuigi Rizzo ntot += n[t]; 1825847bf383SLuigi Rizzo } 1826ccdc3305SLuigi Rizzo /* 1827ccdc3305SLuigi Rizzo * the descriptor is followed inline by an array of offsets 1828ccdc3305SLuigi Rizzo * to the tx and rx rings in the shared memory region. 1829ccdc3305SLuigi Rizzo */ 1830ce3ee1e7SLuigi Rizzo 1831ce3ee1e7SLuigi Rizzo NMA_LOCK(na->nm_mem); 1832ce3ee1e7SLuigi Rizzo 1833847bf383SLuigi Rizzo len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t)); 1834ce3ee1e7SLuigi Rizzo nifp = netmap_if_malloc(na->nm_mem, len); 1835ccdc3305SLuigi Rizzo if (nifp == NULL) { 1836ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1837ccdc3305SLuigi Rizzo return NULL; 1838ccdc3305SLuigi Rizzo } 1839ccdc3305SLuigi Rizzo 1840ccdc3305SLuigi Rizzo /* initialize base fields -- override const */ 1841ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings; 1842ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings; 18434bf50f18SLuigi Rizzo strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ); 1844ccdc3305SLuigi Rizzo 1845ccdc3305SLuigi Rizzo /* 1846ccdc3305SLuigi Rizzo * fill the slots for the rx and tx rings. They contain the offset 1847ccdc3305SLuigi Rizzo * between the ring and nifp, so the information is usable in 1848ccdc3305SLuigi Rizzo * userspace to reach the ring from the nifp. 1849ccdc3305SLuigi Rizzo */ 1850ce3ee1e7SLuigi Rizzo base = netmap_if_offset(na->nm_mem, nifp); 1851847bf383SLuigi Rizzo for (i = 0; i < n[NR_TX]; i++) { 185237e3a6d3SLuigi Rizzo if (na->tx_rings[i].ring == NULL) { 185337e3a6d3SLuigi Rizzo // XXX maybe use the offset of an error ring, 185437e3a6d3SLuigi Rizzo // like we do for buffers? 185537e3a6d3SLuigi Rizzo *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = 0; 185637e3a6d3SLuigi Rizzo continue; 185737e3a6d3SLuigi Rizzo } 1858ccdc3305SLuigi Rizzo *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = 1859ce3ee1e7SLuigi Rizzo netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base; 1860ccdc3305SLuigi Rizzo } 1861847bf383SLuigi Rizzo for (i = 0; i < n[NR_RX]; i++) { 186237e3a6d3SLuigi Rizzo if (na->rx_rings[i].ring == NULL) { 186337e3a6d3SLuigi Rizzo // XXX maybe use the offset of an error ring, 186437e3a6d3SLuigi Rizzo // like we do for buffers? 186537e3a6d3SLuigi Rizzo *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = 0; 186637e3a6d3SLuigi Rizzo continue; 186737e3a6d3SLuigi Rizzo } 1868847bf383SLuigi Rizzo *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = 1869ce3ee1e7SLuigi Rizzo netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base; 1870ccdc3305SLuigi Rizzo } 1871ce3ee1e7SLuigi Rizzo 1872ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1873ce3ee1e7SLuigi Rizzo 1874ccdc3305SLuigi Rizzo return (nifp); 1875ccdc3305SLuigi Rizzo } 1876ccdc3305SLuigi Rizzo 1877847bf383SLuigi Rizzo static void 1878847bf383SLuigi Rizzo netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 1879ccdc3305SLuigi Rizzo { 1880ce3ee1e7SLuigi Rizzo if (nifp == NULL) 1881ce3ee1e7SLuigi Rizzo /* nothing to do */ 1882ce3ee1e7SLuigi Rizzo return; 1883ce3ee1e7SLuigi Rizzo NMA_LOCK(na->nm_mem); 1884f0ea3689SLuigi Rizzo if (nifp->ni_bufs_head) 1885f0ea3689SLuigi Rizzo netmap_extra_free(na, nifp->ni_bufs_head); 1886ce3ee1e7SLuigi Rizzo netmap_if_free(na->nm_mem, nifp); 1887ce3ee1e7SLuigi Rizzo 1888ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1889ce3ee1e7SLuigi Rizzo } 1890ce3ee1e7SLuigi Rizzo 1891ce3ee1e7SLuigi Rizzo static void 1892ce3ee1e7SLuigi Rizzo netmap_mem_global_deref(struct netmap_mem_d *nmd) 1893ce3ee1e7SLuigi Rizzo { 1894ce3ee1e7SLuigi Rizzo 1895847bf383SLuigi Rizzo nmd->active--; 1896847bf383SLuigi Rizzo if (!nmd->active) 18974bf50f18SLuigi Rizzo nmd->nm_grp = -1; 1898ae10d1afSLuigi Rizzo if (netmap_verbose) 1899847bf383SLuigi Rizzo D("active = %d", nmd->active); 1900ce3ee1e7SLuigi Rizzo 1901ce3ee1e7SLuigi Rizzo } 1902ce3ee1e7SLuigi Rizzo 1903847bf383SLuigi Rizzo struct netmap_mem_ops netmap_mem_global_ops = { 1904847bf383SLuigi Rizzo .nmd_get_lut = netmap_mem2_get_lut, 1905847bf383SLuigi Rizzo .nmd_get_info = netmap_mem2_get_info, 1906847bf383SLuigi Rizzo .nmd_ofstophys = netmap_mem2_ofstophys, 1907847bf383SLuigi Rizzo .nmd_config = netmap_mem_global_config, 1908847bf383SLuigi Rizzo .nmd_finalize = netmap_mem_global_finalize, 1909847bf383SLuigi Rizzo .nmd_deref = netmap_mem_global_deref, 1910847bf383SLuigi Rizzo .nmd_delete = netmap_mem_global_delete, 1911847bf383SLuigi Rizzo .nmd_if_offset = netmap_mem2_if_offset, 1912847bf383SLuigi Rizzo .nmd_if_new = netmap_mem2_if_new, 1913847bf383SLuigi Rizzo .nmd_if_delete = netmap_mem2_if_delete, 1914847bf383SLuigi Rizzo .nmd_rings_create = netmap_mem2_rings_create, 1915847bf383SLuigi Rizzo .nmd_rings_delete = netmap_mem2_rings_delete 1916847bf383SLuigi Rizzo }; 1917847bf383SLuigi Rizzo struct netmap_mem_ops netmap_mem_private_ops = { 1918847bf383SLuigi Rizzo .nmd_get_lut = netmap_mem2_get_lut, 1919847bf383SLuigi Rizzo .nmd_get_info = netmap_mem2_get_info, 1920847bf383SLuigi Rizzo .nmd_ofstophys = netmap_mem2_ofstophys, 1921847bf383SLuigi Rizzo .nmd_config = netmap_mem_private_config, 1922847bf383SLuigi Rizzo .nmd_finalize = netmap_mem_private_finalize, 1923847bf383SLuigi Rizzo .nmd_deref = netmap_mem_private_deref, 1924847bf383SLuigi Rizzo .nmd_if_offset = netmap_mem2_if_offset, 1925847bf383SLuigi Rizzo .nmd_delete = netmap_mem_private_delete, 1926847bf383SLuigi Rizzo .nmd_if_new = netmap_mem2_if_new, 1927847bf383SLuigi Rizzo .nmd_if_delete = netmap_mem2_if_delete, 1928847bf383SLuigi Rizzo .nmd_rings_create = netmap_mem2_rings_create, 1929847bf383SLuigi Rizzo .nmd_rings_delete = netmap_mem2_rings_delete 1930847bf383SLuigi Rizzo }; 193137e3a6d3SLuigi Rizzo 193237e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST 193337e3a6d3SLuigi Rizzo struct mem_pt_if { 193437e3a6d3SLuigi Rizzo struct mem_pt_if *next; 193537e3a6d3SLuigi Rizzo struct ifnet *ifp; 193637e3a6d3SLuigi Rizzo unsigned int nifp_offset; 193737e3a6d3SLuigi Rizzo nm_pt_guest_ptctl_t ptctl; 193837e3a6d3SLuigi Rizzo }; 193937e3a6d3SLuigi Rizzo 194037e3a6d3SLuigi Rizzo /* Netmap allocator for ptnetmap guests. */ 194137e3a6d3SLuigi Rizzo struct netmap_mem_ptg { 194237e3a6d3SLuigi Rizzo struct netmap_mem_d up; 194337e3a6d3SLuigi Rizzo 194437e3a6d3SLuigi Rizzo vm_paddr_t nm_paddr; /* physical address in the guest */ 194537e3a6d3SLuigi Rizzo void *nm_addr; /* virtual address in the guest */ 194637e3a6d3SLuigi Rizzo struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */ 194737e3a6d3SLuigi Rizzo nm_memid_t nm_host_id; /* allocator identifier in the host */ 194837e3a6d3SLuigi Rizzo struct ptnetmap_memdev *ptn_dev; 194937e3a6d3SLuigi Rizzo struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */ 195037e3a6d3SLuigi Rizzo }; 195137e3a6d3SLuigi Rizzo 195237e3a6d3SLuigi Rizzo /* Link a passthrough interface to a passthrough netmap allocator. */ 195337e3a6d3SLuigi Rizzo static int 195437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp, 195537e3a6d3SLuigi Rizzo unsigned int nifp_offset, 195637e3a6d3SLuigi Rizzo nm_pt_guest_ptctl_t ptctl) 195737e3a6d3SLuigi Rizzo { 195837e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 195937e3a6d3SLuigi Rizzo struct mem_pt_if *ptif = malloc(sizeof(*ptif), M_NETMAP, 196037e3a6d3SLuigi Rizzo M_NOWAIT | M_ZERO); 196137e3a6d3SLuigi Rizzo 196237e3a6d3SLuigi Rizzo if (!ptif) { 196337e3a6d3SLuigi Rizzo return ENOMEM; 196437e3a6d3SLuigi Rizzo } 196537e3a6d3SLuigi Rizzo 196637e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 196737e3a6d3SLuigi Rizzo 196837e3a6d3SLuigi Rizzo ptif->ifp = ifp; 196937e3a6d3SLuigi Rizzo ptif->nifp_offset = nifp_offset; 197037e3a6d3SLuigi Rizzo ptif->ptctl = ptctl; 197137e3a6d3SLuigi Rizzo 197237e3a6d3SLuigi Rizzo if (ptnmd->pt_ifs) { 197337e3a6d3SLuigi Rizzo ptif->next = ptnmd->pt_ifs; 197437e3a6d3SLuigi Rizzo } 197537e3a6d3SLuigi Rizzo ptnmd->pt_ifs = ptif; 197637e3a6d3SLuigi Rizzo 197737e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 197837e3a6d3SLuigi Rizzo 197937e3a6d3SLuigi Rizzo D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset); 198037e3a6d3SLuigi Rizzo 198137e3a6d3SLuigi Rizzo return 0; 198237e3a6d3SLuigi Rizzo } 198337e3a6d3SLuigi Rizzo 198437e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(nmd) held. */ 198537e3a6d3SLuigi Rizzo static struct mem_pt_if * 198637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp) 198737e3a6d3SLuigi Rizzo { 198837e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 198937e3a6d3SLuigi Rizzo struct mem_pt_if *curr; 199037e3a6d3SLuigi Rizzo 199137e3a6d3SLuigi Rizzo for (curr = ptnmd->pt_ifs; curr; curr = curr->next) { 199237e3a6d3SLuigi Rizzo if (curr->ifp == ifp) { 199337e3a6d3SLuigi Rizzo return curr; 199437e3a6d3SLuigi Rizzo } 199537e3a6d3SLuigi Rizzo } 199637e3a6d3SLuigi Rizzo 199737e3a6d3SLuigi Rizzo return NULL; 199837e3a6d3SLuigi Rizzo } 199937e3a6d3SLuigi Rizzo 200037e3a6d3SLuigi Rizzo /* Unlink a passthrough interface from a passthrough netmap allocator. */ 200137e3a6d3SLuigi Rizzo int 200237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp) 200337e3a6d3SLuigi Rizzo { 200437e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 200537e3a6d3SLuigi Rizzo struct mem_pt_if *prev = NULL; 200637e3a6d3SLuigi Rizzo struct mem_pt_if *curr; 200737e3a6d3SLuigi Rizzo int ret = -1; 200837e3a6d3SLuigi Rizzo 200937e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 201037e3a6d3SLuigi Rizzo 201137e3a6d3SLuigi Rizzo for (curr = ptnmd->pt_ifs; curr; curr = curr->next) { 201237e3a6d3SLuigi Rizzo if (curr->ifp == ifp) { 201337e3a6d3SLuigi Rizzo if (prev) { 201437e3a6d3SLuigi Rizzo prev->next = curr->next; 201537e3a6d3SLuigi Rizzo } else { 201637e3a6d3SLuigi Rizzo ptnmd->pt_ifs = curr->next; 201737e3a6d3SLuigi Rizzo } 201837e3a6d3SLuigi Rizzo D("removed (ifp=%p,nifp_offset=%u)", 201937e3a6d3SLuigi Rizzo curr->ifp, curr->nifp_offset); 202037e3a6d3SLuigi Rizzo free(curr, M_NETMAP); 202137e3a6d3SLuigi Rizzo ret = 0; 202237e3a6d3SLuigi Rizzo break; 202337e3a6d3SLuigi Rizzo } 202437e3a6d3SLuigi Rizzo prev = curr; 202537e3a6d3SLuigi Rizzo } 202637e3a6d3SLuigi Rizzo 202737e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 202837e3a6d3SLuigi Rizzo 202937e3a6d3SLuigi Rizzo return ret; 203037e3a6d3SLuigi Rizzo } 203137e3a6d3SLuigi Rizzo 203237e3a6d3SLuigi Rizzo /* Read allocator info from the first netmap_if (only on finalize) */ 203337e3a6d3SLuigi Rizzo static int 203437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_read_shared_info(struct netmap_mem_d *nmd) 203537e3a6d3SLuigi Rizzo { 203637e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 203737e3a6d3SLuigi Rizzo struct netmap_mem_shared_info *nms_info; 203837e3a6d3SLuigi Rizzo uint32_t bufsize; 203937e3a6d3SLuigi Rizzo uint32_t nbuffers; 204037e3a6d3SLuigi Rizzo char *vaddr; 204137e3a6d3SLuigi Rizzo vm_paddr_t paddr; 204237e3a6d3SLuigi Rizzo int i; 204337e3a6d3SLuigi Rizzo 204437e3a6d3SLuigi Rizzo nms_info = (struct netmap_mem_shared_info *)ptnmd->nm_addr; 204537e3a6d3SLuigi Rizzo if (strncmp(nms_info->up.ni_name, NMS_NAME, sizeof(NMS_NAME)) != 0) { 204637e3a6d3SLuigi Rizzo D("error, the first slot does not contain shared info"); 204737e3a6d3SLuigi Rizzo return EINVAL; 204837e3a6d3SLuigi Rizzo } 204937e3a6d3SLuigi Rizzo /* check features mem_shared info */ 205037e3a6d3SLuigi Rizzo if ((nms_info->features & (NMS_FEAT_BUF_POOL | NMS_FEAT_MEMSIZE)) != 205137e3a6d3SLuigi Rizzo (NMS_FEAT_BUF_POOL | NMS_FEAT_MEMSIZE)) { 205237e3a6d3SLuigi Rizzo D("error, the shared info does not contain BUF_POOL and MEMSIZE"); 205337e3a6d3SLuigi Rizzo return EINVAL; 205437e3a6d3SLuigi Rizzo } 205537e3a6d3SLuigi Rizzo 205637e3a6d3SLuigi Rizzo bufsize = nms_info->buf_pool_objsize; 205737e3a6d3SLuigi Rizzo nbuffers = nms_info->buf_pool_objtotal; 205837e3a6d3SLuigi Rizzo 205937e3a6d3SLuigi Rizzo /* allocate the lut */ 206037e3a6d3SLuigi Rizzo if (ptnmd->buf_lut.lut == NULL) { 206137e3a6d3SLuigi Rizzo D("allocating lut"); 206237e3a6d3SLuigi Rizzo ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers); 206337e3a6d3SLuigi Rizzo if (ptnmd->buf_lut.lut == NULL) { 206437e3a6d3SLuigi Rizzo D("lut allocation failed"); 206537e3a6d3SLuigi Rizzo return ENOMEM; 206637e3a6d3SLuigi Rizzo } 206737e3a6d3SLuigi Rizzo } 206837e3a6d3SLuigi Rizzo 206937e3a6d3SLuigi Rizzo /* we have physically contiguous memory mapped through PCI BAR */ 207037e3a6d3SLuigi Rizzo vaddr = (char *)(ptnmd->nm_addr) + nms_info->buf_pool_offset; 207137e3a6d3SLuigi Rizzo paddr = ptnmd->nm_paddr + nms_info->buf_pool_offset; 207237e3a6d3SLuigi Rizzo 207337e3a6d3SLuigi Rizzo for (i = 0; i < nbuffers; i++) { 207437e3a6d3SLuigi Rizzo ptnmd->buf_lut.lut[i].vaddr = vaddr; 207537e3a6d3SLuigi Rizzo ptnmd->buf_lut.lut[i].paddr = paddr; 207637e3a6d3SLuigi Rizzo vaddr += bufsize; 207737e3a6d3SLuigi Rizzo paddr += bufsize; 207837e3a6d3SLuigi Rizzo } 207937e3a6d3SLuigi Rizzo 208037e3a6d3SLuigi Rizzo ptnmd->buf_lut.objtotal = nbuffers; 208137e3a6d3SLuigi Rizzo ptnmd->buf_lut.objsize = bufsize; 208237e3a6d3SLuigi Rizzo 208337e3a6d3SLuigi Rizzo nmd->nm_totalsize = nms_info->totalsize; 208437e3a6d3SLuigi Rizzo 208537e3a6d3SLuigi Rizzo return 0; 208637e3a6d3SLuigi Rizzo } 208737e3a6d3SLuigi Rizzo 208837e3a6d3SLuigi Rizzo static int 208937e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 209037e3a6d3SLuigi Rizzo { 209137e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 209237e3a6d3SLuigi Rizzo 209337e3a6d3SLuigi Rizzo if (!(nmd->flags & NETMAP_MEM_FINALIZED)) { 209437e3a6d3SLuigi Rizzo return EINVAL; 209537e3a6d3SLuigi Rizzo } 209637e3a6d3SLuigi Rizzo 209737e3a6d3SLuigi Rizzo *lut = ptnmd->buf_lut; 209837e3a6d3SLuigi Rizzo return 0; 209937e3a6d3SLuigi Rizzo } 210037e3a6d3SLuigi Rizzo 210137e3a6d3SLuigi Rizzo static int 210237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, u_int *size, 210337e3a6d3SLuigi Rizzo u_int *memflags, uint16_t *id) 210437e3a6d3SLuigi Rizzo { 210537e3a6d3SLuigi Rizzo int error = 0; 210637e3a6d3SLuigi Rizzo 210737e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 210837e3a6d3SLuigi Rizzo 210937e3a6d3SLuigi Rizzo error = nmd->ops->nmd_config(nmd); 211037e3a6d3SLuigi Rizzo if (error) 211137e3a6d3SLuigi Rizzo goto out; 211237e3a6d3SLuigi Rizzo 211337e3a6d3SLuigi Rizzo if (size) 211437e3a6d3SLuigi Rizzo *size = nmd->nm_totalsize; 211537e3a6d3SLuigi Rizzo if (memflags) 211637e3a6d3SLuigi Rizzo *memflags = nmd->flags; 211737e3a6d3SLuigi Rizzo if (id) 211837e3a6d3SLuigi Rizzo *id = nmd->nm_id; 211937e3a6d3SLuigi Rizzo 212037e3a6d3SLuigi Rizzo out: 212137e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 212237e3a6d3SLuigi Rizzo 212337e3a6d3SLuigi Rizzo return error; 212437e3a6d3SLuigi Rizzo } 212537e3a6d3SLuigi Rizzo 212637e3a6d3SLuigi Rizzo static vm_paddr_t 212737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off) 212837e3a6d3SLuigi Rizzo { 212937e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 213037e3a6d3SLuigi Rizzo vm_paddr_t paddr; 213137e3a6d3SLuigi Rizzo /* if the offset is valid, just return csb->base_addr + off */ 213237e3a6d3SLuigi Rizzo paddr = (vm_paddr_t)(ptnmd->nm_paddr + off); 213337e3a6d3SLuigi Rizzo ND("off %lx padr %lx", off, (unsigned long)paddr); 213437e3a6d3SLuigi Rizzo return paddr; 213537e3a6d3SLuigi Rizzo } 213637e3a6d3SLuigi Rizzo 213737e3a6d3SLuigi Rizzo static int 213837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_config(struct netmap_mem_d *nmd) 213937e3a6d3SLuigi Rizzo { 214037e3a6d3SLuigi Rizzo /* nothing to do, we are configured on creation 214137e3a6d3SLuigi Rizzo * and configuration never changes thereafter 214237e3a6d3SLuigi Rizzo */ 214337e3a6d3SLuigi Rizzo return 0; 214437e3a6d3SLuigi Rizzo } 214537e3a6d3SLuigi Rizzo 214637e3a6d3SLuigi Rizzo static int 214737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd) 214837e3a6d3SLuigi Rizzo { 214937e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 215037e3a6d3SLuigi Rizzo int error = 0; 215137e3a6d3SLuigi Rizzo 215237e3a6d3SLuigi Rizzo nmd->active++; 215337e3a6d3SLuigi Rizzo 215437e3a6d3SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 215537e3a6d3SLuigi Rizzo goto out; 215637e3a6d3SLuigi Rizzo 215737e3a6d3SLuigi Rizzo if (ptnmd->ptn_dev == NULL) { 215837e3a6d3SLuigi Rizzo D("ptnetmap memdev not attached"); 215937e3a6d3SLuigi Rizzo error = ENOMEM; 216037e3a6d3SLuigi Rizzo goto err; 216137e3a6d3SLuigi Rizzo } 216237e3a6d3SLuigi Rizzo /* map memory through ptnetmap-memdev BAR */ 216337e3a6d3SLuigi Rizzo error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr, 216437e3a6d3SLuigi Rizzo &ptnmd->nm_addr); 216537e3a6d3SLuigi Rizzo if (error) 216637e3a6d3SLuigi Rizzo goto err; 216737e3a6d3SLuigi Rizzo 216837e3a6d3SLuigi Rizzo /* read allcator info and create lut */ 216937e3a6d3SLuigi Rizzo error = netmap_mem_pt_guest_read_shared_info(nmd); 217037e3a6d3SLuigi Rizzo if (error) 217137e3a6d3SLuigi Rizzo goto err; 217237e3a6d3SLuigi Rizzo 217337e3a6d3SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 217437e3a6d3SLuigi Rizzo out: 217537e3a6d3SLuigi Rizzo return 0; 217637e3a6d3SLuigi Rizzo err: 217737e3a6d3SLuigi Rizzo nmd->active--; 217837e3a6d3SLuigi Rizzo return error; 217937e3a6d3SLuigi Rizzo } 218037e3a6d3SLuigi Rizzo 218137e3a6d3SLuigi Rizzo static void 218237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd) 218337e3a6d3SLuigi Rizzo { 218437e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 218537e3a6d3SLuigi Rizzo 218637e3a6d3SLuigi Rizzo nmd->active--; 218737e3a6d3SLuigi Rizzo if (nmd->active <= 0 && 218837e3a6d3SLuigi Rizzo (nmd->flags & NETMAP_MEM_FINALIZED)) { 218937e3a6d3SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 219037e3a6d3SLuigi Rizzo /* unmap ptnetmap-memdev memory */ 219137e3a6d3SLuigi Rizzo if (ptnmd->ptn_dev) { 219237e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(ptnmd->ptn_dev); 219337e3a6d3SLuigi Rizzo } 219437e3a6d3SLuigi Rizzo ptnmd->nm_addr = 0; 219537e3a6d3SLuigi Rizzo ptnmd->nm_paddr = 0; 219637e3a6d3SLuigi Rizzo } 219737e3a6d3SLuigi Rizzo } 219837e3a6d3SLuigi Rizzo 219937e3a6d3SLuigi Rizzo static ssize_t 220037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr) 220137e3a6d3SLuigi Rizzo { 220237e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 220337e3a6d3SLuigi Rizzo 220437e3a6d3SLuigi Rizzo return (const char *)(vaddr) - (char *)(ptnmd->nm_addr); 220537e3a6d3SLuigi Rizzo } 220637e3a6d3SLuigi Rizzo 220737e3a6d3SLuigi Rizzo static void 220837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd) 220937e3a6d3SLuigi Rizzo { 221037e3a6d3SLuigi Rizzo if (nmd == NULL) 221137e3a6d3SLuigi Rizzo return; 221237e3a6d3SLuigi Rizzo if (netmap_verbose) 221337e3a6d3SLuigi Rizzo D("deleting %p", nmd); 221437e3a6d3SLuigi Rizzo if (nmd->active > 0) 221537e3a6d3SLuigi Rizzo D("bug: deleting mem allocator with active=%d!", nmd->active); 221637e3a6d3SLuigi Rizzo nm_mem_release_id(nmd); 221737e3a6d3SLuigi Rizzo if (netmap_verbose) 221837e3a6d3SLuigi Rizzo D("done deleting %p", nmd); 221937e3a6d3SLuigi Rizzo NMA_LOCK_DESTROY(nmd); 222037e3a6d3SLuigi Rizzo free(nmd, M_DEVBUF); 222137e3a6d3SLuigi Rizzo } 222237e3a6d3SLuigi Rizzo 222337e3a6d3SLuigi Rizzo static struct netmap_if * 222437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_new(struct netmap_adapter *na) 222537e3a6d3SLuigi Rizzo { 222637e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 222737e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 222837e3a6d3SLuigi Rizzo struct netmap_if *nifp = NULL; 222937e3a6d3SLuigi Rizzo 223037e3a6d3SLuigi Rizzo NMA_LOCK(na->nm_mem); 223137e3a6d3SLuigi Rizzo 223237e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 223337e3a6d3SLuigi Rizzo if (ptif == NULL) { 223437e3a6d3SLuigi Rizzo D("Error: interface %p is not in passthrough", na->ifp); 223537e3a6d3SLuigi Rizzo goto out; 223637e3a6d3SLuigi Rizzo } 223737e3a6d3SLuigi Rizzo 223837e3a6d3SLuigi Rizzo nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) + 223937e3a6d3SLuigi Rizzo ptif->nifp_offset); 224037e3a6d3SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 224137e3a6d3SLuigi Rizzo out: 224237e3a6d3SLuigi Rizzo return nifp; 224337e3a6d3SLuigi Rizzo } 224437e3a6d3SLuigi Rizzo 224537e3a6d3SLuigi Rizzo static void 224637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 224737e3a6d3SLuigi Rizzo { 224837e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 224937e3a6d3SLuigi Rizzo 225037e3a6d3SLuigi Rizzo NMA_LOCK(na->nm_mem); 225137e3a6d3SLuigi Rizzo 225237e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 225337e3a6d3SLuigi Rizzo if (ptif == NULL) { 225437e3a6d3SLuigi Rizzo D("Error: interface %p is not in passthrough", na->ifp); 225537e3a6d3SLuigi Rizzo goto out; 225637e3a6d3SLuigi Rizzo } 225737e3a6d3SLuigi Rizzo 225837e3a6d3SLuigi Rizzo ptif->ptctl(na->ifp, PTNETMAP_PTCTL_IFDELETE); 225937e3a6d3SLuigi Rizzo out: 226037e3a6d3SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 226137e3a6d3SLuigi Rizzo } 226237e3a6d3SLuigi Rizzo 226337e3a6d3SLuigi Rizzo static int 226437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_create(struct netmap_adapter *na) 226537e3a6d3SLuigi Rizzo { 226637e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 226737e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 226837e3a6d3SLuigi Rizzo struct netmap_if *nifp; 226937e3a6d3SLuigi Rizzo int i, error = -1; 227037e3a6d3SLuigi Rizzo 227137e3a6d3SLuigi Rizzo NMA_LOCK(na->nm_mem); 227237e3a6d3SLuigi Rizzo 227337e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 227437e3a6d3SLuigi Rizzo if (ptif == NULL) { 227537e3a6d3SLuigi Rizzo D("Error: interface %p is not in passthrough", na->ifp); 227637e3a6d3SLuigi Rizzo goto out; 227737e3a6d3SLuigi Rizzo } 227837e3a6d3SLuigi Rizzo 227937e3a6d3SLuigi Rizzo 228037e3a6d3SLuigi Rizzo /* point each kring to the corresponding backend ring */ 228137e3a6d3SLuigi Rizzo nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset); 228237e3a6d3SLuigi Rizzo for (i = 0; i <= na->num_tx_rings; i++) { 228337e3a6d3SLuigi Rizzo struct netmap_kring *kring = na->tx_rings + i; 228437e3a6d3SLuigi Rizzo if (kring->ring) 228537e3a6d3SLuigi Rizzo continue; 228637e3a6d3SLuigi Rizzo kring->ring = (struct netmap_ring *) 228737e3a6d3SLuigi Rizzo ((char *)nifp + nifp->ring_ofs[i]); 228837e3a6d3SLuigi Rizzo } 228937e3a6d3SLuigi Rizzo for (i = 0; i <= na->num_rx_rings; i++) { 229037e3a6d3SLuigi Rizzo struct netmap_kring *kring = na->rx_rings + i; 229137e3a6d3SLuigi Rizzo if (kring->ring) 229237e3a6d3SLuigi Rizzo continue; 229337e3a6d3SLuigi Rizzo kring->ring = (struct netmap_ring *) 229437e3a6d3SLuigi Rizzo ((char *)nifp + 229537e3a6d3SLuigi Rizzo nifp->ring_ofs[i + na->num_tx_rings + 1]); 229637e3a6d3SLuigi Rizzo } 229737e3a6d3SLuigi Rizzo 229837e3a6d3SLuigi Rizzo //error = ptif->ptctl->nm_ptctl(ifp, PTNETMAP_PTCTL_RINGSCREATE); 229937e3a6d3SLuigi Rizzo error = 0; 230037e3a6d3SLuigi Rizzo out: 230137e3a6d3SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 230237e3a6d3SLuigi Rizzo 230337e3a6d3SLuigi Rizzo return error; 230437e3a6d3SLuigi Rizzo } 230537e3a6d3SLuigi Rizzo 230637e3a6d3SLuigi Rizzo static void 230737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na) 230837e3a6d3SLuigi Rizzo { 230937e3a6d3SLuigi Rizzo /* TODO: remove?? */ 231037e3a6d3SLuigi Rizzo #if 0 231137e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 231237e3a6d3SLuigi Rizzo struct mem_pt_if *ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, 231337e3a6d3SLuigi Rizzo na->ifp); 231437e3a6d3SLuigi Rizzo #endif 231537e3a6d3SLuigi Rizzo } 231637e3a6d3SLuigi Rizzo 231737e3a6d3SLuigi Rizzo static struct netmap_mem_ops netmap_mem_pt_guest_ops = { 231837e3a6d3SLuigi Rizzo .nmd_get_lut = netmap_mem_pt_guest_get_lut, 231937e3a6d3SLuigi Rizzo .nmd_get_info = netmap_mem_pt_guest_get_info, 232037e3a6d3SLuigi Rizzo .nmd_ofstophys = netmap_mem_pt_guest_ofstophys, 232137e3a6d3SLuigi Rizzo .nmd_config = netmap_mem_pt_guest_config, 232237e3a6d3SLuigi Rizzo .nmd_finalize = netmap_mem_pt_guest_finalize, 232337e3a6d3SLuigi Rizzo .nmd_deref = netmap_mem_pt_guest_deref, 232437e3a6d3SLuigi Rizzo .nmd_if_offset = netmap_mem_pt_guest_if_offset, 232537e3a6d3SLuigi Rizzo .nmd_delete = netmap_mem_pt_guest_delete, 232637e3a6d3SLuigi Rizzo .nmd_if_new = netmap_mem_pt_guest_if_new, 232737e3a6d3SLuigi Rizzo .nmd_if_delete = netmap_mem_pt_guest_if_delete, 232837e3a6d3SLuigi Rizzo .nmd_rings_create = netmap_mem_pt_guest_rings_create, 232937e3a6d3SLuigi Rizzo .nmd_rings_delete = netmap_mem_pt_guest_rings_delete 233037e3a6d3SLuigi Rizzo }; 233137e3a6d3SLuigi Rizzo 233237e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(&nm_mem) held. */ 233337e3a6d3SLuigi Rizzo static struct netmap_mem_d * 233437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_find_hostid(nm_memid_t host_id) 233537e3a6d3SLuigi Rizzo { 233637e3a6d3SLuigi Rizzo struct netmap_mem_d *mem = NULL; 233737e3a6d3SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 233837e3a6d3SLuigi Rizzo 233937e3a6d3SLuigi Rizzo do { 234037e3a6d3SLuigi Rizzo /* find ptnetmap allocator through host ID */ 234137e3a6d3SLuigi Rizzo if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref && 234237e3a6d3SLuigi Rizzo ((struct netmap_mem_ptg *)(scan))->nm_host_id == host_id) { 234337e3a6d3SLuigi Rizzo mem = scan; 234437e3a6d3SLuigi Rizzo break; 234537e3a6d3SLuigi Rizzo } 234637e3a6d3SLuigi Rizzo scan = scan->next; 234737e3a6d3SLuigi Rizzo } while (scan != netmap_last_mem_d); 234837e3a6d3SLuigi Rizzo 234937e3a6d3SLuigi Rizzo return mem; 235037e3a6d3SLuigi Rizzo } 235137e3a6d3SLuigi Rizzo 235237e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(&nm_mem) held. */ 235337e3a6d3SLuigi Rizzo static struct netmap_mem_d * 235437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_create(nm_memid_t host_id) 235537e3a6d3SLuigi Rizzo { 235637e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd; 235737e3a6d3SLuigi Rizzo int err = 0; 235837e3a6d3SLuigi Rizzo 235937e3a6d3SLuigi Rizzo ptnmd = malloc(sizeof(struct netmap_mem_ptg), 236037e3a6d3SLuigi Rizzo M_DEVBUF, M_NOWAIT | M_ZERO); 236137e3a6d3SLuigi Rizzo if (ptnmd == NULL) { 236237e3a6d3SLuigi Rizzo err = ENOMEM; 236337e3a6d3SLuigi Rizzo goto error; 236437e3a6d3SLuigi Rizzo } 236537e3a6d3SLuigi Rizzo 236637e3a6d3SLuigi Rizzo ptnmd->up.ops = &netmap_mem_pt_guest_ops; 236737e3a6d3SLuigi Rizzo ptnmd->nm_host_id = host_id; 236837e3a6d3SLuigi Rizzo ptnmd->pt_ifs = NULL; 236937e3a6d3SLuigi Rizzo 237037e3a6d3SLuigi Rizzo /* Assign new id in the guest (We have the lock) */ 237137e3a6d3SLuigi Rizzo err = nm_mem_assign_id_locked(&ptnmd->up); 237237e3a6d3SLuigi Rizzo if (err) 237337e3a6d3SLuigi Rizzo goto error; 237437e3a6d3SLuigi Rizzo 237537e3a6d3SLuigi Rizzo ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED; 237637e3a6d3SLuigi Rizzo ptnmd->up.flags |= NETMAP_MEM_IO; 237737e3a6d3SLuigi Rizzo 237837e3a6d3SLuigi Rizzo NMA_LOCK_INIT(&ptnmd->up); 237937e3a6d3SLuigi Rizzo 238037e3a6d3SLuigi Rizzo return &ptnmd->up; 238137e3a6d3SLuigi Rizzo error: 238237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(&ptnmd->up); 238337e3a6d3SLuigi Rizzo return NULL; 238437e3a6d3SLuigi Rizzo } 238537e3a6d3SLuigi Rizzo 238637e3a6d3SLuigi Rizzo /* 238737e3a6d3SLuigi Rizzo * find host id in guest allocators and create guest allocator 238837e3a6d3SLuigi Rizzo * if it is not there 238937e3a6d3SLuigi Rizzo */ 239037e3a6d3SLuigi Rizzo static struct netmap_mem_d * 239137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get(nm_memid_t host_id) 239237e3a6d3SLuigi Rizzo { 239337e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 239437e3a6d3SLuigi Rizzo 239537e3a6d3SLuigi Rizzo NMA_LOCK(&nm_mem); 239637e3a6d3SLuigi Rizzo nmd = netmap_mem_pt_guest_find_hostid(host_id); 239737e3a6d3SLuigi Rizzo if (nmd == NULL) { 239837e3a6d3SLuigi Rizzo nmd = netmap_mem_pt_guest_create(host_id); 239937e3a6d3SLuigi Rizzo } 240037e3a6d3SLuigi Rizzo NMA_UNLOCK(&nm_mem); 240137e3a6d3SLuigi Rizzo 240237e3a6d3SLuigi Rizzo return nmd; 240337e3a6d3SLuigi Rizzo } 240437e3a6d3SLuigi Rizzo 240537e3a6d3SLuigi Rizzo /* 240637e3a6d3SLuigi Rizzo * The guest allocator can be created by ptnetmap_memdev (during the device 240737e3a6d3SLuigi Rizzo * attach) or by ptnetmap device (e1000/virtio), during the netmap_attach. 240837e3a6d3SLuigi Rizzo * 240937e3a6d3SLuigi Rizzo * The order is not important (we have different order in LINUX and FreeBSD). 241037e3a6d3SLuigi Rizzo * The first one, creates the device, and the second one simply attaches it. 241137e3a6d3SLuigi Rizzo */ 241237e3a6d3SLuigi Rizzo 241337e3a6d3SLuigi Rizzo /* Called when ptnetmap_memdev is attaching, to attach a new allocator in 241437e3a6d3SLuigi Rizzo * the guest */ 241537e3a6d3SLuigi Rizzo struct netmap_mem_d * 241637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t host_id) 241737e3a6d3SLuigi Rizzo { 241837e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 241937e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd; 242037e3a6d3SLuigi Rizzo 242137e3a6d3SLuigi Rizzo nmd = netmap_mem_pt_guest_get(host_id); 242237e3a6d3SLuigi Rizzo 242337e3a6d3SLuigi Rizzo /* assign this device to the guest allocator */ 242437e3a6d3SLuigi Rizzo if (nmd) { 242537e3a6d3SLuigi Rizzo ptnmd = (struct netmap_mem_ptg *)nmd; 242637e3a6d3SLuigi Rizzo ptnmd->ptn_dev = ptn_dev; 242737e3a6d3SLuigi Rizzo } 242837e3a6d3SLuigi Rizzo 242937e3a6d3SLuigi Rizzo return nmd; 243037e3a6d3SLuigi Rizzo } 243137e3a6d3SLuigi Rizzo 243237e3a6d3SLuigi Rizzo /* Called when ptnetmap device (virtio/e1000) is attaching */ 243337e3a6d3SLuigi Rizzo struct netmap_mem_d * 243437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_new(struct ifnet *ifp, 243537e3a6d3SLuigi Rizzo unsigned int nifp_offset, 243637e3a6d3SLuigi Rizzo nm_pt_guest_ptctl_t ptctl) 243737e3a6d3SLuigi Rizzo { 243837e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 243937e3a6d3SLuigi Rizzo nm_memid_t host_id; 244037e3a6d3SLuigi Rizzo 244137e3a6d3SLuigi Rizzo if (ifp == NULL || ptctl == NULL) { 244237e3a6d3SLuigi Rizzo return NULL; 244337e3a6d3SLuigi Rizzo } 244437e3a6d3SLuigi Rizzo 244537e3a6d3SLuigi Rizzo /* Get the host id allocator. */ 244637e3a6d3SLuigi Rizzo host_id = ptctl(ifp, PTNETMAP_PTCTL_HOSTMEMID); 244737e3a6d3SLuigi Rizzo 244837e3a6d3SLuigi Rizzo nmd = netmap_mem_pt_guest_get(host_id); 244937e3a6d3SLuigi Rizzo 245037e3a6d3SLuigi Rizzo if (nmd) { 245137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset, 245237e3a6d3SLuigi Rizzo ptctl); 245337e3a6d3SLuigi Rizzo } 245437e3a6d3SLuigi Rizzo 245537e3a6d3SLuigi Rizzo return nmd; 245637e3a6d3SLuigi Rizzo } 245737e3a6d3SLuigi Rizzo 245837e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */ 2459