1718cf2ccSPedro F. Giffuni /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 437e3a6d3SLuigi Rizzo * Copyright (C) 2012-2014 Matteo Landi 537e3a6d3SLuigi Rizzo * Copyright (C) 2012-2016 Luigi Rizzo 637e3a6d3SLuigi Rizzo * Copyright (C) 2012-2016 Giuseppe Lettieri 737e3a6d3SLuigi Rizzo * All rights reserved. 8ccdc3305SLuigi Rizzo * 9ccdc3305SLuigi Rizzo * Redistribution and use in source and binary forms, with or without 10ccdc3305SLuigi Rizzo * modification, are permitted provided that the following conditions 11ccdc3305SLuigi Rizzo * are met: 12ccdc3305SLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 13ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer. 14ccdc3305SLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 15ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 16ccdc3305SLuigi Rizzo * documentation and/or other materials provided with the distribution. 17ccdc3305SLuigi Rizzo * 18ccdc3305SLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19ccdc3305SLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20ccdc3305SLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21ccdc3305SLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22ccdc3305SLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23ccdc3305SLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24ccdc3305SLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25ccdc3305SLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26ccdc3305SLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27ccdc3305SLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28ccdc3305SLuigi Rizzo * SUCH DAMAGE. 29ccdc3305SLuigi Rizzo */ 30ccdc3305SLuigi Rizzo 318241616dSLuigi Rizzo #ifdef linux 32ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 338241616dSLuigi Rizzo #endif /* linux */ 348241616dSLuigi Rizzo 35ce3ee1e7SLuigi Rizzo #ifdef __APPLE__ 36ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 37ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */ 388241616dSLuigi Rizzo 39ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 40ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 41ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$"); 428241616dSLuigi Rizzo 43ce3ee1e7SLuigi Rizzo #include <sys/types.h> 44ce3ee1e7SLuigi Rizzo #include <sys/malloc.h> 4537e3a6d3SLuigi Rizzo #include <sys/kernel.h> /* MALLOC_DEFINE */ 46ce3ee1e7SLuigi Rizzo #include <sys/proc.h> 47ce3ee1e7SLuigi Rizzo #include <vm/vm.h> /* vtophys */ 48ce3ee1e7SLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 49ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 50ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h> 51ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h> 52ce3ee1e7SLuigi Rizzo #include <net/if.h> 53ce3ee1e7SLuigi Rizzo #include <net/if_var.h> 54ce3ee1e7SLuigi Rizzo #include <net/vnet.h> 55ce3ee1e7SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 56ce3ee1e7SLuigi Rizzo 5737e3a6d3SLuigi Rizzo /* M_NETMAP only used in here */ 5837e3a6d3SLuigi Rizzo MALLOC_DECLARE(M_NETMAP); 5937e3a6d3SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 6037e3a6d3SLuigi Rizzo 61ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 62ce3ee1e7SLuigi Rizzo 6337e3a6d3SLuigi Rizzo #ifdef _WIN32 6437e3a6d3SLuigi Rizzo #include <win_glue.h> 6537e3a6d3SLuigi Rizzo #endif 6637e3a6d3SLuigi Rizzo 67ce3ee1e7SLuigi Rizzo #include <net/netmap.h> 68ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 6937e3a6d3SLuigi Rizzo #include <net/netmap_virt.h> 70ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h" 71ce3ee1e7SLuigi Rizzo 7237e3a6d3SLuigi Rizzo #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY 7337e3a6d3SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 8*4096 /* if too big takes too much time to allocate */ 7437e3a6d3SLuigi Rizzo #else 754bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */ 7637e3a6d3SLuigi Rizzo #endif 774bf50f18SLuigi Rizzo 784bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ 32 794bf50f18SLuigi Rizzo 804bf50f18SLuigi Rizzo 814bf50f18SLuigi Rizzo enum { 824bf50f18SLuigi Rizzo NETMAP_IF_POOL = 0, 834bf50f18SLuigi Rizzo NETMAP_RING_POOL, 844bf50f18SLuigi Rizzo NETMAP_BUF_POOL, 854bf50f18SLuigi Rizzo NETMAP_POOLS_NR 864bf50f18SLuigi Rizzo }; 874bf50f18SLuigi Rizzo 884bf50f18SLuigi Rizzo 894bf50f18SLuigi Rizzo struct netmap_obj_params { 904bf50f18SLuigi Rizzo u_int size; 914bf50f18SLuigi Rizzo u_int num; 92c3e9b4dbSLuiz Otavio O Souza 93c3e9b4dbSLuiz Otavio O Souza u_int last_size; 94c3e9b4dbSLuiz Otavio O Souza u_int last_num; 954bf50f18SLuigi Rizzo }; 96847bf383SLuigi Rizzo 974bf50f18SLuigi Rizzo struct netmap_obj_pool { 984bf50f18SLuigi Rizzo char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */ 994bf50f18SLuigi Rizzo 1004bf50f18SLuigi Rizzo /* ---------------------------------------------------*/ 1014bf50f18SLuigi Rizzo /* these are only meaningful if the pool is finalized */ 1024bf50f18SLuigi Rizzo /* (see 'finalized' field in netmap_mem_d) */ 103253b2ec1SVincenzo Maffione size_t memtotal; /* actual total memory space */ 1044bf50f18SLuigi Rizzo 1054bf50f18SLuigi Rizzo struct lut_entry *lut; /* virt,phys addresses, objtotal entries */ 1064bf50f18SLuigi Rizzo uint32_t *bitmap; /* one bit per buffer, 1 means free */ 1074f80b14cSVincenzo Maffione uint32_t *invalid_bitmap;/* one bit per buffer, 1 means invalid */ 1084bf50f18SLuigi Rizzo uint32_t bitmap_slots; /* number of uint32 entries in bitmap */ 109253b2ec1SVincenzo Maffione 110253b2ec1SVincenzo Maffione u_int objtotal; /* actual total number of objects. */ 111253b2ec1SVincenzo Maffione u_int numclusters; /* actual number of clusters */ 112253b2ec1SVincenzo Maffione u_int objfree; /* number of free objects. */ 113253b2ec1SVincenzo Maffione 1142ff91c17SVincenzo Maffione int alloc_done; /* we have allocated the memory */ 1154bf50f18SLuigi Rizzo /* ---------------------------------------------------*/ 1164bf50f18SLuigi Rizzo 1174bf50f18SLuigi Rizzo /* limits */ 1184bf50f18SLuigi Rizzo u_int objminsize; /* minimum object size */ 1194bf50f18SLuigi Rizzo u_int objmaxsize; /* maximum object size */ 1204bf50f18SLuigi Rizzo u_int nummin; /* minimum number of objects */ 1214bf50f18SLuigi Rizzo u_int nummax; /* maximum number of objects */ 1224bf50f18SLuigi Rizzo 1234bf50f18SLuigi Rizzo /* these are changed only by config */ 1244bf50f18SLuigi Rizzo u_int _objtotal; /* total number of objects */ 1254bf50f18SLuigi Rizzo u_int _objsize; /* object size */ 1264bf50f18SLuigi Rizzo u_int _clustsize; /* cluster size */ 1274bf50f18SLuigi Rizzo u_int _clustentries; /* objects per cluster */ 1284bf50f18SLuigi Rizzo u_int _numclusters; /* number of clusters */ 1294bf50f18SLuigi Rizzo 1304bf50f18SLuigi Rizzo /* requested values */ 1314bf50f18SLuigi Rizzo u_int r_objtotal; 1324bf50f18SLuigi Rizzo u_int r_objsize; 1334bf50f18SLuigi Rizzo }; 1344bf50f18SLuigi Rizzo 135847bf383SLuigi Rizzo #define NMA_LOCK_T NM_MTX_T 1362ff91c17SVincenzo Maffione #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx) 1372ff91c17SVincenzo Maffione #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx) 1382ff91c17SVincenzo Maffione #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx) 1392ff91c17SVincenzo Maffione #define NMA_SPINLOCK(n) NM_MTX_SPINLOCK((n)->nm_mtx) 1402ff91c17SVincenzo Maffione #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx) 141847bf383SLuigi Rizzo 142847bf383SLuigi Rizzo struct netmap_mem_ops { 14337e3a6d3SLuigi Rizzo int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*); 1444f80b14cSVincenzo Maffione int (*nmd_get_info)(struct netmap_mem_d *, uint64_t *size, 145847bf383SLuigi Rizzo u_int *memflags, uint16_t *id); 146847bf383SLuigi Rizzo 147847bf383SLuigi Rizzo vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t); 148847bf383SLuigi Rizzo int (*nmd_config)(struct netmap_mem_d *); 149847bf383SLuigi Rizzo int (*nmd_finalize)(struct netmap_mem_d *); 150847bf383SLuigi Rizzo void (*nmd_deref)(struct netmap_mem_d *); 151847bf383SLuigi Rizzo ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr); 152847bf383SLuigi Rizzo void (*nmd_delete)(struct netmap_mem_d *); 153847bf383SLuigi Rizzo 154c3e9b4dbSLuiz Otavio O Souza struct netmap_if * (*nmd_if_new)(struct netmap_adapter *, 155c3e9b4dbSLuiz Otavio O Souza struct netmap_priv_d *); 156847bf383SLuigi Rizzo void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *); 157847bf383SLuigi Rizzo int (*nmd_rings_create)(struct netmap_adapter *); 158847bf383SLuigi Rizzo void (*nmd_rings_delete)(struct netmap_adapter *); 159847bf383SLuigi Rizzo }; 1604bf50f18SLuigi Rizzo 1614bf50f18SLuigi Rizzo struct netmap_mem_d { 1624bf50f18SLuigi Rizzo NMA_LOCK_T nm_mtx; /* protect the allocator */ 163253b2ec1SVincenzo Maffione size_t nm_totalsize; /* shorthand */ 1644bf50f18SLuigi Rizzo 1654bf50f18SLuigi Rizzo u_int flags; 1664bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */ 167c3e9b4dbSLuiz Otavio O Souza #define NETMAP_MEM_HIDDEN 0x8 /* beeing prepared */ 1684bf50f18SLuigi Rizzo int lasterr; /* last error for curr config */ 169847bf383SLuigi Rizzo int active; /* active users */ 170847bf383SLuigi Rizzo int refcount; 1714bf50f18SLuigi Rizzo /* the three allocators */ 1724bf50f18SLuigi Rizzo struct netmap_obj_pool pools[NETMAP_POOLS_NR]; 1734bf50f18SLuigi Rizzo 1744bf50f18SLuigi Rizzo nm_memid_t nm_id; /* allocator identifier */ 1754bf50f18SLuigi Rizzo int nm_grp; /* iommu groupd id */ 1764bf50f18SLuigi Rizzo 1774bf50f18SLuigi Rizzo /* list of all existing allocators, sorted by nm_id */ 1784bf50f18SLuigi Rizzo struct netmap_mem_d *prev, *next; 179847bf383SLuigi Rizzo 180847bf383SLuigi Rizzo struct netmap_mem_ops *ops; 181c3e9b4dbSLuiz Otavio O Souza 182c3e9b4dbSLuiz Otavio O Souza struct netmap_obj_params params[NETMAP_POOLS_NR]; 183c3e9b4dbSLuiz Otavio O Souza 184c3e9b4dbSLuiz Otavio O Souza #define NM_MEM_NAMESZ 16 185c3e9b4dbSLuiz Otavio O Souza char name[NM_MEM_NAMESZ]; 1864bf50f18SLuigi Rizzo }; 1874bf50f18SLuigi Rizzo 1882ff91c17SVincenzo Maffione int 1892ff91c17SVincenzo Maffione netmap_mem_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 1902ff91c17SVincenzo Maffione { 1912ff91c17SVincenzo Maffione int rv; 1922ff91c17SVincenzo Maffione 1932ff91c17SVincenzo Maffione NMA_LOCK(nmd); 1942ff91c17SVincenzo Maffione rv = nmd->ops->nmd_get_lut(nmd, lut); 1952ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 1962ff91c17SVincenzo Maffione 1972ff91c17SVincenzo Maffione return rv; 1982ff91c17SVincenzo Maffione } 1992ff91c17SVincenzo Maffione 2002ff91c17SVincenzo Maffione int 2012ff91c17SVincenzo Maffione netmap_mem_get_info(struct netmap_mem_d *nmd, uint64_t *size, 2022ff91c17SVincenzo Maffione u_int *memflags, nm_memid_t *memid) 2032ff91c17SVincenzo Maffione { 2042ff91c17SVincenzo Maffione int rv; 2052ff91c17SVincenzo Maffione 2062ff91c17SVincenzo Maffione NMA_LOCK(nmd); 2072ff91c17SVincenzo Maffione rv = nmd->ops->nmd_get_info(nmd, size, memflags, memid); 2082ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 2092ff91c17SVincenzo Maffione 2102ff91c17SVincenzo Maffione return rv; 2112ff91c17SVincenzo Maffione } 2122ff91c17SVincenzo Maffione 2132ff91c17SVincenzo Maffione vm_paddr_t 2142ff91c17SVincenzo Maffione netmap_mem_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off) 2152ff91c17SVincenzo Maffione { 2162ff91c17SVincenzo Maffione vm_paddr_t pa; 2172ff91c17SVincenzo Maffione 2182ff91c17SVincenzo Maffione #if defined(__FreeBSD__) 2192ff91c17SVincenzo Maffione /* This function is called by netmap_dev_pager_fault(), which holds a 2202ff91c17SVincenzo Maffione * non-sleepable lock since FreeBSD 12. Since we cannot sleep, we 2212ff91c17SVincenzo Maffione * spin on the trylock. */ 2222ff91c17SVincenzo Maffione NMA_SPINLOCK(nmd); 2232ff91c17SVincenzo Maffione #else 2242ff91c17SVincenzo Maffione NMA_LOCK(nmd); 2252ff91c17SVincenzo Maffione #endif 2262ff91c17SVincenzo Maffione pa = nmd->ops->nmd_ofstophys(nmd, off); 2272ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 2282ff91c17SVincenzo Maffione 2292ff91c17SVincenzo Maffione return pa; 2302ff91c17SVincenzo Maffione } 2312ff91c17SVincenzo Maffione 2322ff91c17SVincenzo Maffione static int 2332ff91c17SVincenzo Maffione netmap_mem_config(struct netmap_mem_d *nmd) 2342ff91c17SVincenzo Maffione { 2352ff91c17SVincenzo Maffione if (nmd->active) { 2362ff91c17SVincenzo Maffione /* already in use. Not fatal, but we 2372ff91c17SVincenzo Maffione * cannot change the configuration 23837e3a6d3SLuigi Rizzo */ 2392ff91c17SVincenzo Maffione return 0; 240847bf383SLuigi Rizzo } 241847bf383SLuigi Rizzo 2422ff91c17SVincenzo Maffione return nmd->ops->nmd_config(nmd); 243847bf383SLuigi Rizzo } 244847bf383SLuigi Rizzo 2452ff91c17SVincenzo Maffione ssize_t 2462ff91c17SVincenzo Maffione netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *off) 2472ff91c17SVincenzo Maffione { 2482ff91c17SVincenzo Maffione ssize_t rv; 2492ff91c17SVincenzo Maffione 2502ff91c17SVincenzo Maffione NMA_LOCK(nmd); 2512ff91c17SVincenzo Maffione rv = nmd->ops->nmd_if_offset(nmd, off); 2522ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 2532ff91c17SVincenzo Maffione 2542ff91c17SVincenzo Maffione return rv; 255847bf383SLuigi Rizzo } 256847bf383SLuigi Rizzo 2572ff91c17SVincenzo Maffione static void 2582ff91c17SVincenzo Maffione netmap_mem_delete(struct netmap_mem_d *nmd) 2592ff91c17SVincenzo Maffione { 2602ff91c17SVincenzo Maffione nmd->ops->nmd_delete(nmd); 261847bf383SLuigi Rizzo } 262847bf383SLuigi Rizzo 2632ff91c17SVincenzo Maffione struct netmap_if * 2642ff91c17SVincenzo Maffione netmap_mem_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv) 2652ff91c17SVincenzo Maffione { 2662ff91c17SVincenzo Maffione struct netmap_if *nifp; 2672ff91c17SVincenzo Maffione struct netmap_mem_d *nmd = na->nm_mem; 2682ff91c17SVincenzo Maffione 2692ff91c17SVincenzo Maffione NMA_LOCK(nmd); 2702ff91c17SVincenzo Maffione nifp = nmd->ops->nmd_if_new(na, priv); 2712ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 2722ff91c17SVincenzo Maffione 2732ff91c17SVincenzo Maffione return nifp; 274847bf383SLuigi Rizzo } 275847bf383SLuigi Rizzo 2762ff91c17SVincenzo Maffione void 2772ff91c17SVincenzo Maffione netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nif) 2782ff91c17SVincenzo Maffione { 2792ff91c17SVincenzo Maffione struct netmap_mem_d *nmd = na->nm_mem; 280847bf383SLuigi Rizzo 2812ff91c17SVincenzo Maffione NMA_LOCK(nmd); 2822ff91c17SVincenzo Maffione nmd->ops->nmd_if_delete(na, nif); 2832ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 2842ff91c17SVincenzo Maffione } 2852ff91c17SVincenzo Maffione 2862ff91c17SVincenzo Maffione int 2872ff91c17SVincenzo Maffione netmap_mem_rings_create(struct netmap_adapter *na) 2882ff91c17SVincenzo Maffione { 2892ff91c17SVincenzo Maffione int rv; 2902ff91c17SVincenzo Maffione struct netmap_mem_d *nmd = na->nm_mem; 2912ff91c17SVincenzo Maffione 2922ff91c17SVincenzo Maffione NMA_LOCK(nmd); 2932ff91c17SVincenzo Maffione rv = nmd->ops->nmd_rings_create(na); 2942ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 2952ff91c17SVincenzo Maffione 2962ff91c17SVincenzo Maffione return rv; 2972ff91c17SVincenzo Maffione } 2982ff91c17SVincenzo Maffione 2992ff91c17SVincenzo Maffione void 3002ff91c17SVincenzo Maffione netmap_mem_rings_delete(struct netmap_adapter *na) 3012ff91c17SVincenzo Maffione { 3022ff91c17SVincenzo Maffione struct netmap_mem_d *nmd = na->nm_mem; 3032ff91c17SVincenzo Maffione 3042ff91c17SVincenzo Maffione NMA_LOCK(nmd); 3052ff91c17SVincenzo Maffione nmd->ops->nmd_rings_delete(na); 3062ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 3072ff91c17SVincenzo Maffione } 308847bf383SLuigi Rizzo 309847bf383SLuigi Rizzo static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *); 310847bf383SLuigi Rizzo static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *); 31137e3a6d3SLuigi Rizzo static int nm_mem_assign_group(struct netmap_mem_d *, struct device *); 312c3e9b4dbSLuiz Otavio O Souza static void nm_mem_release_id(struct netmap_mem_d *); 313c3e9b4dbSLuiz Otavio O Souza 314c3e9b4dbSLuiz Otavio O Souza nm_memid_t 315c3e9b4dbSLuiz Otavio O Souza netmap_mem_get_id(struct netmap_mem_d *nmd) 316c3e9b4dbSLuiz Otavio O Souza { 317c3e9b4dbSLuiz Otavio O Souza return nmd->nm_id; 318c3e9b4dbSLuiz Otavio O Souza } 319847bf383SLuigi Rizzo 320847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET 321847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line) \ 322b6e66be2SVincenzo Maffione nm_prinf("%d mem[%d] -> %d", line, (nmd)->nm_id, (nmd)->refcount); 323847bf383SLuigi Rizzo #else 324847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line) 325847bf383SLuigi Rizzo #endif 326847bf383SLuigi Rizzo 327c3e9b4dbSLuiz Otavio O Souza /* circular list of all existing allocators */ 328c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d *netmap_last_mem_d = &nm_mem; 329c3e9b4dbSLuiz Otavio O Souza NM_MTX_T nm_mem_list_lock; 330c3e9b4dbSLuiz Otavio O Souza 331c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * 332c3e9b4dbSLuiz Otavio O Souza __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line) 333847bf383SLuigi Rizzo { 334c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 335847bf383SLuigi Rizzo nmd->refcount++; 336847bf383SLuigi Rizzo NM_DBG_REFC(nmd, func, line); 337c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 338c3e9b4dbSLuiz Otavio O Souza return nmd; 339847bf383SLuigi Rizzo } 340847bf383SLuigi Rizzo 341c3e9b4dbSLuiz Otavio O Souza void 342c3e9b4dbSLuiz Otavio O Souza __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line) 343847bf383SLuigi Rizzo { 344847bf383SLuigi Rizzo int last; 345c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 346847bf383SLuigi Rizzo last = (--nmd->refcount == 0); 347c3e9b4dbSLuiz Otavio O Souza if (last) 348c3e9b4dbSLuiz Otavio O Souza nm_mem_release_id(nmd); 349847bf383SLuigi Rizzo NM_DBG_REFC(nmd, func, line); 350c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 351847bf383SLuigi Rizzo if (last) 352847bf383SLuigi Rizzo netmap_mem_delete(nmd); 353847bf383SLuigi Rizzo } 354847bf383SLuigi Rizzo 355847bf383SLuigi Rizzo int 356847bf383SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na) 357847bf383SLuigi Rizzo { 3582ff91c17SVincenzo Maffione int lasterr = 0; 359847bf383SLuigi Rizzo if (nm_mem_assign_group(nmd, na->pdev) < 0) { 360847bf383SLuigi Rizzo return ENOMEM; 361847bf383SLuigi Rizzo } 362847bf383SLuigi Rizzo 3632ff91c17SVincenzo Maffione NMA_LOCK(nmd); 3642ff91c17SVincenzo Maffione 3652ff91c17SVincenzo Maffione if (netmap_mem_config(nmd)) 3662ff91c17SVincenzo Maffione goto out; 3672ff91c17SVincenzo Maffione 3682ff91c17SVincenzo Maffione nmd->active++; 3692ff91c17SVincenzo Maffione 3702ff91c17SVincenzo Maffione nmd->lasterr = nmd->ops->nmd_finalize(nmd); 3712ff91c17SVincenzo Maffione 3724f80b14cSVincenzo Maffione if (!nmd->lasterr && na->pdev) { 3734f80b14cSVincenzo Maffione nmd->lasterr = netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na); 3744f80b14cSVincenzo Maffione } 375847bf383SLuigi Rizzo 3762ff91c17SVincenzo Maffione out: 3772ff91c17SVincenzo Maffione lasterr = nmd->lasterr; 3782ff91c17SVincenzo Maffione NMA_UNLOCK(nmd); 3792ff91c17SVincenzo Maffione 3802ff91c17SVincenzo Maffione if (lasterr) 3812ff91c17SVincenzo Maffione netmap_mem_deref(nmd, na); 3822ff91c17SVincenzo Maffione 3832ff91c17SVincenzo Maffione return lasterr; 384847bf383SLuigi Rizzo } 385847bf383SLuigi Rizzo 3864f80b14cSVincenzo Maffione static int 3874f80b14cSVincenzo Maffione nm_isset(uint32_t *bitmap, u_int i) 388847bf383SLuigi Rizzo { 3894f80b14cSVincenzo Maffione return bitmap[ (i>>5) ] & ( 1U << (i & 31U) ); 3904f80b14cSVincenzo Maffione } 39137e3a6d3SLuigi Rizzo 39237e3a6d3SLuigi Rizzo 3934f80b14cSVincenzo Maffione static int 3944f80b14cSVincenzo Maffione netmap_init_obj_allocator_bitmap(struct netmap_obj_pool *p) 3954f80b14cSVincenzo Maffione { 3964f80b14cSVincenzo Maffione u_int n, j; 39737e3a6d3SLuigi Rizzo 3984f80b14cSVincenzo Maffione if (p->bitmap == NULL) { 3994f80b14cSVincenzo Maffione /* Allocate the bitmap */ 4004f80b14cSVincenzo Maffione n = (p->objtotal + 31) / 32; 401b6e66be2SVincenzo Maffione p->bitmap = nm_os_malloc(sizeof(p->bitmap[0]) * n); 4024f80b14cSVincenzo Maffione if (p->bitmap == NULL) { 403b6e66be2SVincenzo Maffione nm_prerr("Unable to create bitmap (%d entries) for allocator '%s'", (int)n, 4044f80b14cSVincenzo Maffione p->name); 4054f80b14cSVincenzo Maffione return ENOMEM; 4064f80b14cSVincenzo Maffione } 4074f80b14cSVincenzo Maffione p->bitmap_slots = n; 4084f80b14cSVincenzo Maffione } else { 409b6e66be2SVincenzo Maffione memset(p->bitmap, 0, p->bitmap_slots * sizeof(p->bitmap[0])); 4104f80b14cSVincenzo Maffione } 4114f80b14cSVincenzo Maffione 4124f80b14cSVincenzo Maffione p->objfree = 0; 41337e3a6d3SLuigi Rizzo /* 41437e3a6d3SLuigi Rizzo * Set all the bits in the bitmap that have 41537e3a6d3SLuigi Rizzo * corresponding buffers to 1 to indicate they are 41637e3a6d3SLuigi Rizzo * free. 41737e3a6d3SLuigi Rizzo */ 41837e3a6d3SLuigi Rizzo for (j = 0; j < p->objtotal; j++) { 4194f80b14cSVincenzo Maffione if (p->invalid_bitmap && nm_isset(p->invalid_bitmap, j)) { 420b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 421b6e66be2SVincenzo Maffione nm_prinf("skipping %s %d", p->name, j); 4224f80b14cSVincenzo Maffione continue; 42337e3a6d3SLuigi Rizzo } 4244f80b14cSVincenzo Maffione p->bitmap[ (j>>5) ] |= ( 1U << (j & 31U) ); 4254f80b14cSVincenzo Maffione p->objfree++; 42637e3a6d3SLuigi Rizzo } 4274f80b14cSVincenzo Maffione 428b6e66be2SVincenzo Maffione if (netmap_verbose) 429b6e66be2SVincenzo Maffione nm_prinf("%s free %u", p->name, p->objfree); 430b6e66be2SVincenzo Maffione if (p->objfree == 0) { 431b6e66be2SVincenzo Maffione if (netmap_verbose) 432b6e66be2SVincenzo Maffione nm_prerr("%s: no objects available", p->name); 4334f80b14cSVincenzo Maffione return ENOMEM; 434b6e66be2SVincenzo Maffione } 4354f80b14cSVincenzo Maffione 4364f80b14cSVincenzo Maffione return 0; 4374f80b14cSVincenzo Maffione } 4384f80b14cSVincenzo Maffione 4394f80b14cSVincenzo Maffione static int 4404f80b14cSVincenzo Maffione netmap_mem_init_bitmaps(struct netmap_mem_d *nmd) 4414f80b14cSVincenzo Maffione { 4424f80b14cSVincenzo Maffione int i, error = 0; 4434f80b14cSVincenzo Maffione 4444f80b14cSVincenzo Maffione for (i = 0; i < NETMAP_POOLS_NR; i++) { 4454f80b14cSVincenzo Maffione struct netmap_obj_pool *p = &nmd->pools[i]; 4464f80b14cSVincenzo Maffione 4474f80b14cSVincenzo Maffione error = netmap_init_obj_allocator_bitmap(p); 4484f80b14cSVincenzo Maffione if (error) 4494f80b14cSVincenzo Maffione return error; 45037e3a6d3SLuigi Rizzo } 45137e3a6d3SLuigi Rizzo 45237e3a6d3SLuigi Rizzo /* 45337e3a6d3SLuigi Rizzo * buffers 0 and 1 are reserved 45437e3a6d3SLuigi Rizzo */ 4554f80b14cSVincenzo Maffione if (nmd->pools[NETMAP_BUF_POOL].objfree < 2) { 456b6e66be2SVincenzo Maffione nm_prerr("%s: not enough buffers", nmd->pools[NETMAP_BUF_POOL].name); 4574f80b14cSVincenzo Maffione return ENOMEM; 4584f80b14cSVincenzo Maffione } 4594f80b14cSVincenzo Maffione 46037e3a6d3SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].objfree -= 2; 46137e3a6d3SLuigi Rizzo if (nmd->pools[NETMAP_BUF_POOL].bitmap) { 46237e3a6d3SLuigi Rizzo /* XXX This check is a workaround that prevents a 46337e3a6d3SLuigi Rizzo * NULL pointer crash which currently happens only 464844a6f0cSLuigi Rizzo * with ptnetmap guests. 465844a6f0cSLuigi Rizzo * Removed shared-info --> is the bug still there? */ 4664f80b14cSVincenzo Maffione nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3U; 46737e3a6d3SLuigi Rizzo } 4684f80b14cSVincenzo Maffione return 0; 4694f80b14cSVincenzo Maffione } 4704f80b14cSVincenzo Maffione 4714f80b14cSVincenzo Maffione int 4724f80b14cSVincenzo Maffione netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na) 4734f80b14cSVincenzo Maffione { 4744f80b14cSVincenzo Maffione int last_user = 0; 4754f80b14cSVincenzo Maffione NMA_LOCK(nmd); 4764f80b14cSVincenzo Maffione if (na->active_fds <= 0) 4774f80b14cSVincenzo Maffione netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na); 4784f80b14cSVincenzo Maffione if (nmd->active == 1) { 4794f80b14cSVincenzo Maffione last_user = 1; 4804f80b14cSVincenzo Maffione /* 4814f80b14cSVincenzo Maffione * Reset the allocator when it falls out of use so that any 4824f80b14cSVincenzo Maffione * pool resources leaked by unclean application exits are 4834f80b14cSVincenzo Maffione * reclaimed. 4844f80b14cSVincenzo Maffione */ 4854f80b14cSVincenzo Maffione netmap_mem_init_bitmaps(nmd); 48637e3a6d3SLuigi Rizzo } 48737e3a6d3SLuigi Rizzo nmd->ops->nmd_deref(nmd); 48837e3a6d3SLuigi Rizzo 4892ff91c17SVincenzo Maffione nmd->active--; 490b6e66be2SVincenzo Maffione if (last_user) { 4912ff91c17SVincenzo Maffione nmd->nm_grp = -1; 492b6e66be2SVincenzo Maffione nmd->lasterr = 0; 493b6e66be2SVincenzo Maffione } 4942ff91c17SVincenzo Maffione 495847bf383SLuigi Rizzo NMA_UNLOCK(nmd); 4964f80b14cSVincenzo Maffione return last_user; 497847bf383SLuigi Rizzo } 498847bf383SLuigi Rizzo 499847bf383SLuigi Rizzo 5004bf50f18SLuigi Rizzo /* accessor functions */ 50137e3a6d3SLuigi Rizzo static int 502847bf383SLuigi Rizzo netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 5034bf50f18SLuigi Rizzo { 504847bf383SLuigi Rizzo lut->lut = nmd->pools[NETMAP_BUF_POOL].lut; 5054f80b14cSVincenzo Maffione #ifdef __FreeBSD__ 5064f80b14cSVincenzo Maffione lut->plut = lut->lut; 5074f80b14cSVincenzo Maffione #endif 508847bf383SLuigi Rizzo lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal; 509847bf383SLuigi Rizzo lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize; 51037e3a6d3SLuigi Rizzo 51137e3a6d3SLuigi Rizzo return 0; 5124bf50f18SLuigi Rizzo } 5134bf50f18SLuigi Rizzo 51437e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = { 515f0ea3689SLuigi Rizzo [NETMAP_IF_POOL] = { 516f0ea3689SLuigi Rizzo .size = 1024, 51737e3a6d3SLuigi Rizzo .num = 2, 518f0ea3689SLuigi Rizzo }, 519f0ea3689SLuigi Rizzo [NETMAP_RING_POOL] = { 520f0ea3689SLuigi Rizzo .size = 5*PAGE_SIZE, 521f0ea3689SLuigi Rizzo .num = 4, 522f0ea3689SLuigi Rizzo }, 523f0ea3689SLuigi Rizzo [NETMAP_BUF_POOL] = { 524f0ea3689SLuigi Rizzo .size = 2048, 525f0ea3689SLuigi Rizzo .num = 4098, 526f0ea3689SLuigi Rizzo }, 527f0ea3689SLuigi Rizzo }; 528f0ea3689SLuigi Rizzo 529ccdc3305SLuigi Rizzo 5302579e2d7SLuigi Rizzo /* 5312579e2d7SLuigi Rizzo * nm_mem is the memory allocator used for all physical interfaces 5322579e2d7SLuigi Rizzo * running in netmap mode. 5332579e2d7SLuigi Rizzo * Virtual (VALE) ports will have each its own allocator. 5342579e2d7SLuigi Rizzo */ 535847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */ 536ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = { /* Our memory allocator. */ 5378241616dSLuigi Rizzo .pools = { 5388241616dSLuigi Rizzo [NETMAP_IF_POOL] = { 5398241616dSLuigi Rizzo .name = "netmap_if", 5408241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_if), 5418241616dSLuigi Rizzo .objmaxsize = 4096, 5428241616dSLuigi Rizzo .nummin = 10, /* don't be stingy */ 5438241616dSLuigi Rizzo .nummax = 10000, /* XXX very large */ 5448241616dSLuigi Rizzo }, 5458241616dSLuigi Rizzo [NETMAP_RING_POOL] = { 5468241616dSLuigi Rizzo .name = "netmap_ring", 5478241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 5488241616dSLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 5498241616dSLuigi Rizzo .nummin = 2, 5508241616dSLuigi Rizzo .nummax = 1024, 5518241616dSLuigi Rizzo }, 5528241616dSLuigi Rizzo [NETMAP_BUF_POOL] = { 5538241616dSLuigi Rizzo .name = "netmap_buf", 5548241616dSLuigi Rizzo .objminsize = 64, 5558241616dSLuigi Rizzo .objmaxsize = 65536, 5568241616dSLuigi Rizzo .nummin = 4, 5578241616dSLuigi Rizzo .nummax = 1000000, /* one million! */ 5588241616dSLuigi Rizzo }, 5598241616dSLuigi Rizzo }, 560f0ea3689SLuigi Rizzo 561c3e9b4dbSLuiz Otavio O Souza .params = { 562c3e9b4dbSLuiz Otavio O Souza [NETMAP_IF_POOL] = { 563c3e9b4dbSLuiz Otavio O Souza .size = 1024, 564c3e9b4dbSLuiz Otavio O Souza .num = 100, 565c3e9b4dbSLuiz Otavio O Souza }, 566c3e9b4dbSLuiz Otavio O Souza [NETMAP_RING_POOL] = { 567c3e9b4dbSLuiz Otavio O Souza .size = 9*PAGE_SIZE, 568c3e9b4dbSLuiz Otavio O Souza .num = 200, 569c3e9b4dbSLuiz Otavio O Souza }, 570c3e9b4dbSLuiz Otavio O Souza [NETMAP_BUF_POOL] = { 571c3e9b4dbSLuiz Otavio O Souza .size = 2048, 572c3e9b4dbSLuiz Otavio O Souza .num = NETMAP_BUF_MAX_NUM, 573c3e9b4dbSLuiz Otavio O Souza }, 574c3e9b4dbSLuiz Otavio O Souza }, 575c3e9b4dbSLuiz Otavio O Souza 576f0ea3689SLuigi Rizzo .nm_id = 1, 5774bf50f18SLuigi Rizzo .nm_grp = -1, 578f0ea3689SLuigi Rizzo 579f0ea3689SLuigi Rizzo .prev = &nm_mem, 580f0ea3689SLuigi Rizzo .next = &nm_mem, 581847bf383SLuigi Rizzo 582c3e9b4dbSLuiz Otavio O Souza .ops = &netmap_mem_global_ops, 583c3e9b4dbSLuiz Otavio O Souza 584c3e9b4dbSLuiz Otavio O Souza .name = "1" 585ccdc3305SLuigi Rizzo }; 586ccdc3305SLuigi Rizzo 587ce3ee1e7SLuigi Rizzo 588ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */ 58937e3a6d3SLuigi Rizzo /* XXX clang is not happy about using name as a print format */ 59037e3a6d3SLuigi Rizzo static const struct netmap_mem_d nm_blueprint = { 591ce3ee1e7SLuigi Rizzo .pools = { 592ce3ee1e7SLuigi Rizzo [NETMAP_IF_POOL] = { 593ce3ee1e7SLuigi Rizzo .name = "%s_if", 594ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_if), 595ce3ee1e7SLuigi Rizzo .objmaxsize = 4096, 596ce3ee1e7SLuigi Rizzo .nummin = 1, 597f0ea3689SLuigi Rizzo .nummax = 100, 598ce3ee1e7SLuigi Rizzo }, 599ce3ee1e7SLuigi Rizzo [NETMAP_RING_POOL] = { 600ce3ee1e7SLuigi Rizzo .name = "%s_ring", 601ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 602ce3ee1e7SLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 603ce3ee1e7SLuigi Rizzo .nummin = 2, 604ce3ee1e7SLuigi Rizzo .nummax = 1024, 605ce3ee1e7SLuigi Rizzo }, 606ce3ee1e7SLuigi Rizzo [NETMAP_BUF_POOL] = { 607ce3ee1e7SLuigi Rizzo .name = "%s_buf", 608ce3ee1e7SLuigi Rizzo .objminsize = 64, 609ce3ee1e7SLuigi Rizzo .objmaxsize = 65536, 610ce3ee1e7SLuigi Rizzo .nummin = 4, 611ce3ee1e7SLuigi Rizzo .nummax = 1000000, /* one million! */ 612ce3ee1e7SLuigi Rizzo }, 613ce3ee1e7SLuigi Rizzo }, 614ce3ee1e7SLuigi Rizzo 615c3e9b4dbSLuiz Otavio O Souza .nm_grp = -1, 616c3e9b4dbSLuiz Otavio O Souza 617ce3ee1e7SLuigi Rizzo .flags = NETMAP_MEM_PRIVATE, 618847bf383SLuigi Rizzo 619c3e9b4dbSLuiz Otavio O Souza .ops = &netmap_mem_global_ops, 620ce3ee1e7SLuigi Rizzo }; 621ce3ee1e7SLuigi Rizzo 6228241616dSLuigi Rizzo /* memory allocator related sysctls */ 6238241616dSLuigi Rizzo 6248241616dSLuigi Rizzo #define STRINGIFY(x) #x 6258241616dSLuigi Rizzo 626ce3ee1e7SLuigi Rizzo 6278241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \ 62837e3a6d3SLuigi Rizzo SYSBEGIN(mem2_ ## name); \ 6298241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \ 630c3e9b4dbSLuiz Otavio O Souza CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \ 6318241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \ 6328241616dSLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \ 6338241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \ 634c3e9b4dbSLuiz Otavio O Souza CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \ 6358241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \ 636f0ea3689SLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \ 637f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \ 638f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \ 639f0ea3689SLuigi Rizzo "Default size of private netmap " STRINGIFY(name) "s"); \ 640f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \ 641f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \ 64237e3a6d3SLuigi Rizzo "Default number of private netmap " STRINGIFY(name) "s"); \ 64337e3a6d3SLuigi Rizzo SYSEND 6448241616dSLuigi Rizzo 645984ff0d9SEd Maste SYSCTL_DECL(_dev_netmap); 6468241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if); 6478241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring); 6488241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf); 649ccdc3305SLuigi Rizzo 650c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */ 651f0ea3689SLuigi Rizzo static int 65237e3a6d3SLuigi Rizzo nm_mem_assign_id_locked(struct netmap_mem_d *nmd) 653f0ea3689SLuigi Rizzo { 654f0ea3689SLuigi Rizzo nm_memid_t id; 655f0ea3689SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 656f0ea3689SLuigi Rizzo int error = ENOMEM; 657f0ea3689SLuigi Rizzo 658f0ea3689SLuigi Rizzo do { 659f0ea3689SLuigi Rizzo /* we rely on unsigned wrap around */ 660f0ea3689SLuigi Rizzo id = scan->nm_id + 1; 661f0ea3689SLuigi Rizzo if (id == 0) /* reserve 0 as error value */ 662f0ea3689SLuigi Rizzo id = 1; 663f0ea3689SLuigi Rizzo scan = scan->next; 664f0ea3689SLuigi Rizzo if (id != scan->nm_id) { 665f0ea3689SLuigi Rizzo nmd->nm_id = id; 666f0ea3689SLuigi Rizzo nmd->prev = scan->prev; 667f0ea3689SLuigi Rizzo nmd->next = scan; 668f0ea3689SLuigi Rizzo scan->prev->next = nmd; 669f0ea3689SLuigi Rizzo scan->prev = nmd; 670f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd; 671c3e9b4dbSLuiz Otavio O Souza nmd->refcount = 1; 672c3e9b4dbSLuiz Otavio O Souza NM_DBG_REFC(nmd, __FUNCTION__, __LINE__); 673f0ea3689SLuigi Rizzo error = 0; 674f0ea3689SLuigi Rizzo break; 675f0ea3689SLuigi Rizzo } 676f0ea3689SLuigi Rizzo } while (scan != netmap_last_mem_d); 677f0ea3689SLuigi Rizzo 678f0ea3689SLuigi Rizzo return error; 679f0ea3689SLuigi Rizzo } 680f0ea3689SLuigi Rizzo 681c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock *not* held */ 68237e3a6d3SLuigi Rizzo static int 68337e3a6d3SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd) 68437e3a6d3SLuigi Rizzo { 68537e3a6d3SLuigi Rizzo int ret; 68637e3a6d3SLuigi Rizzo 687c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 68837e3a6d3SLuigi Rizzo ret = nm_mem_assign_id_locked(nmd); 689c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 69037e3a6d3SLuigi Rizzo 69137e3a6d3SLuigi Rizzo return ret; 69237e3a6d3SLuigi Rizzo } 69337e3a6d3SLuigi Rizzo 694c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */ 695f0ea3689SLuigi Rizzo static void 696f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd) 697f0ea3689SLuigi Rizzo { 698f0ea3689SLuigi Rizzo nmd->prev->next = nmd->next; 699f0ea3689SLuigi Rizzo nmd->next->prev = nmd->prev; 700f0ea3689SLuigi Rizzo 701f0ea3689SLuigi Rizzo if (netmap_last_mem_d == nmd) 702f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd->prev; 703f0ea3689SLuigi Rizzo 704f0ea3689SLuigi Rizzo nmd->prev = nmd->next = NULL; 705c3e9b4dbSLuiz Otavio O Souza } 706f0ea3689SLuigi Rizzo 707c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * 708c3e9b4dbSLuiz Otavio O Souza netmap_mem_find(nm_memid_t id) 709c3e9b4dbSLuiz Otavio O Souza { 710c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *nmd; 711c3e9b4dbSLuiz Otavio O Souza 712c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 713c3e9b4dbSLuiz Otavio O Souza nmd = netmap_last_mem_d; 714c3e9b4dbSLuiz Otavio O Souza do { 715c3e9b4dbSLuiz Otavio O Souza if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) { 716c3e9b4dbSLuiz Otavio O Souza nmd->refcount++; 717c3e9b4dbSLuiz Otavio O Souza NM_DBG_REFC(nmd, __FUNCTION__, __LINE__); 718c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 719c3e9b4dbSLuiz Otavio O Souza return nmd; 720c3e9b4dbSLuiz Otavio O Souza } 721c3e9b4dbSLuiz Otavio O Souza nmd = nmd->next; 722c3e9b4dbSLuiz Otavio O Souza } while (nmd != netmap_last_mem_d); 723c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 724c3e9b4dbSLuiz Otavio O Souza return NULL; 725f0ea3689SLuigi Rizzo } 726f0ea3689SLuigi Rizzo 7274bf50f18SLuigi Rizzo static int 72837e3a6d3SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev) 7294bf50f18SLuigi Rizzo { 7304bf50f18SLuigi Rizzo int err = 0, id; 7314bf50f18SLuigi Rizzo id = nm_iommu_group_id(dev); 732b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 733b6e66be2SVincenzo Maffione nm_prinf("iommu_group %d", id); 7344bf50f18SLuigi Rizzo 7354bf50f18SLuigi Rizzo NMA_LOCK(nmd); 7364bf50f18SLuigi Rizzo 7374bf50f18SLuigi Rizzo if (nmd->nm_grp < 0) 7384bf50f18SLuigi Rizzo nmd->nm_grp = id; 7394bf50f18SLuigi Rizzo 740b6e66be2SVincenzo Maffione if (nmd->nm_grp != id) { 741b6e66be2SVincenzo Maffione if (netmap_verbose) 742b6e66be2SVincenzo Maffione nm_prerr("iommu group mismatch: %u vs %u", 743b6e66be2SVincenzo Maffione nmd->nm_grp, id); 7444bf50f18SLuigi Rizzo nmd->lasterr = err = ENOMEM; 745b6e66be2SVincenzo Maffione } 7464bf50f18SLuigi Rizzo 7474bf50f18SLuigi Rizzo NMA_UNLOCK(nmd); 7484bf50f18SLuigi Rizzo return err; 7494bf50f18SLuigi Rizzo } 750f0ea3689SLuigi Rizzo 7514f80b14cSVincenzo Maffione static struct lut_entry * 7524f80b14cSVincenzo Maffione nm_alloc_lut(u_int nobj) 7534f80b14cSVincenzo Maffione { 7544f80b14cSVincenzo Maffione size_t n = sizeof(struct lut_entry) * nobj; 7554f80b14cSVincenzo Maffione struct lut_entry *lut; 7564f80b14cSVincenzo Maffione #ifdef linux 7574f80b14cSVincenzo Maffione lut = vmalloc(n); 7584f80b14cSVincenzo Maffione #else 7594f80b14cSVincenzo Maffione lut = nm_os_malloc(n); 7604f80b14cSVincenzo Maffione #endif 7614f80b14cSVincenzo Maffione return lut; 7624f80b14cSVincenzo Maffione } 7634f80b14cSVincenzo Maffione 7644f80b14cSVincenzo Maffione static void 7654f80b14cSVincenzo Maffione nm_free_lut(struct lut_entry *lut, u_int objtotal) 7664f80b14cSVincenzo Maffione { 7674f80b14cSVincenzo Maffione bzero(lut, sizeof(struct lut_entry) * objtotal); 7684f80b14cSVincenzo Maffione #ifdef linux 7694f80b14cSVincenzo Maffione vfree(lut); 7704f80b14cSVincenzo Maffione #else 7714f80b14cSVincenzo Maffione nm_os_free(lut); 7724f80b14cSVincenzo Maffione #endif 7734f80b14cSVincenzo Maffione } 7744f80b14cSVincenzo Maffione 7754f80b14cSVincenzo Maffione #if defined(linux) || defined(_WIN32) 7764f80b14cSVincenzo Maffione static struct plut_entry * 7774f80b14cSVincenzo Maffione nm_alloc_plut(u_int nobj) 7784f80b14cSVincenzo Maffione { 7794f80b14cSVincenzo Maffione size_t n = sizeof(struct plut_entry) * nobj; 7804f80b14cSVincenzo Maffione struct plut_entry *lut; 7814f80b14cSVincenzo Maffione lut = vmalloc(n); 7824f80b14cSVincenzo Maffione return lut; 7834f80b14cSVincenzo Maffione } 7844f80b14cSVincenzo Maffione 7854f80b14cSVincenzo Maffione static void 7864f80b14cSVincenzo Maffione nm_free_plut(struct plut_entry * lut) 7874f80b14cSVincenzo Maffione { 7884f80b14cSVincenzo Maffione vfree(lut); 7894f80b14cSVincenzo Maffione } 7904f80b14cSVincenzo Maffione #endif /* linux or _WIN32 */ 7914f80b14cSVincenzo Maffione 7924f80b14cSVincenzo Maffione 793ccdc3305SLuigi Rizzo /* 7942579e2d7SLuigi Rizzo * First, find the allocator that contains the requested offset, 7952579e2d7SLuigi Rizzo * then locate the cluster through a lookup table. 796ccdc3305SLuigi Rizzo */ 797847bf383SLuigi Rizzo static vm_paddr_t 798847bf383SLuigi Rizzo netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset) 799ccdc3305SLuigi Rizzo { 800ccdc3305SLuigi Rizzo int i; 801ce3ee1e7SLuigi Rizzo vm_ooffset_t o = offset; 802ce3ee1e7SLuigi Rizzo vm_paddr_t pa; 803ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p; 804ccdc3305SLuigi Rizzo 805ce3ee1e7SLuigi Rizzo p = nmd->pools; 806ce3ee1e7SLuigi Rizzo 807ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) { 808ce3ee1e7SLuigi Rizzo if (offset >= p[i].memtotal) 809ccdc3305SLuigi Rizzo continue; 8102579e2d7SLuigi Rizzo // now lookup the cluster's address 81137e3a6d3SLuigi Rizzo #ifndef _WIN32 8124bf50f18SLuigi Rizzo pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) + 8138241616dSLuigi Rizzo offset % p[i]._objsize; 81437e3a6d3SLuigi Rizzo #else 81537e3a6d3SLuigi Rizzo pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr); 81637e3a6d3SLuigi Rizzo pa.QuadPart += offset % p[i]._objsize; 81737e3a6d3SLuigi Rizzo #endif 818ce3ee1e7SLuigi Rizzo return pa; 819ccdc3305SLuigi Rizzo } 8208241616dSLuigi Rizzo /* this is only in case of errors */ 821253b2ec1SVincenzo Maffione nm_prerr("invalid ofs 0x%x out of 0x%zx 0x%zx 0x%zx", (u_int)o, 822ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal, 823ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 824ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal, 825ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 826ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal 827ce3ee1e7SLuigi Rizzo + p[NETMAP_BUF_POOL].memtotal); 82837e3a6d3SLuigi Rizzo #ifndef _WIN32 8294f80b14cSVincenzo Maffione return 0; /* bad address */ 83037e3a6d3SLuigi Rizzo #else 83137e3a6d3SLuigi Rizzo vm_paddr_t res; 83237e3a6d3SLuigi Rizzo res.QuadPart = 0; 83337e3a6d3SLuigi Rizzo return res; 83437e3a6d3SLuigi Rizzo #endif 83537e3a6d3SLuigi Rizzo } 83637e3a6d3SLuigi Rizzo 83737e3a6d3SLuigi Rizzo #ifdef _WIN32 83837e3a6d3SLuigi Rizzo 83937e3a6d3SLuigi Rizzo /* 84037e3a6d3SLuigi Rizzo * win32_build_virtual_memory_for_userspace 84137e3a6d3SLuigi Rizzo * 84237e3a6d3SLuigi Rizzo * This function get all the object making part of the pools and maps 84337e3a6d3SLuigi Rizzo * a contiguous virtual memory space for the userspace 84437e3a6d3SLuigi Rizzo * It works this way 84537e3a6d3SLuigi Rizzo * 1 - allocate a Memory Descriptor List wide as the sum 84637e3a6d3SLuigi Rizzo * of the memory needed for the pools 84737e3a6d3SLuigi Rizzo * 2 - cycle all the objects in every pool and for every object do 84837e3a6d3SLuigi Rizzo * 84937e3a6d3SLuigi Rizzo * 2a - cycle all the objects in every pool, get the list 85037e3a6d3SLuigi Rizzo * of the physical address descriptors 85137e3a6d3SLuigi Rizzo * 2b - calculate the offset in the array of pages desciptor in the 85237e3a6d3SLuigi Rizzo * main MDL 85337e3a6d3SLuigi Rizzo * 2c - copy the descriptors of the object in the main MDL 85437e3a6d3SLuigi Rizzo * 85537e3a6d3SLuigi Rizzo * 3 - return the resulting MDL that needs to be mapped in userland 85637e3a6d3SLuigi Rizzo * 85737e3a6d3SLuigi Rizzo * In this way we will have an MDL that describes all the memory for the 85837e3a6d3SLuigi Rizzo * objects in a single object 85937e3a6d3SLuigi Rizzo */ 86037e3a6d3SLuigi Rizzo 86137e3a6d3SLuigi Rizzo PMDL 86237e3a6d3SLuigi Rizzo win32_build_user_vm_map(struct netmap_mem_d* nmd) 86337e3a6d3SLuigi Rizzo { 8644f80b14cSVincenzo Maffione u_int memflags, ofs = 0; 86537e3a6d3SLuigi Rizzo PMDL mainMdl, tempMdl; 8662ff91c17SVincenzo Maffione uint64_t memsize; 8672ff91c17SVincenzo Maffione int i, j; 86837e3a6d3SLuigi Rizzo 86937e3a6d3SLuigi Rizzo if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) { 870b6e66be2SVincenzo Maffione nm_prerr("memory not finalised yet"); 87137e3a6d3SLuigi Rizzo return NULL; 87237e3a6d3SLuigi Rizzo } 87337e3a6d3SLuigi Rizzo 87437e3a6d3SLuigi Rizzo mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL); 87537e3a6d3SLuigi Rizzo if (mainMdl == NULL) { 876b6e66be2SVincenzo Maffione nm_prerr("failed to allocate mdl"); 87737e3a6d3SLuigi Rizzo return NULL; 87837e3a6d3SLuigi Rizzo } 87937e3a6d3SLuigi Rizzo 88037e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 88137e3a6d3SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 88237e3a6d3SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[i]; 88337e3a6d3SLuigi Rizzo int clsz = p->_clustsize; 88437e3a6d3SLuigi Rizzo int clobjs = p->_clustentries; /* objects per cluster */ 88537e3a6d3SLuigi Rizzo int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz); 88637e3a6d3SLuigi Rizzo PPFN_NUMBER pSrc, pDst; 88737e3a6d3SLuigi Rizzo 88837e3a6d3SLuigi Rizzo /* each pool has a different cluster size so we need to reallocate */ 88937e3a6d3SLuigi Rizzo tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL); 89037e3a6d3SLuigi Rizzo if (tempMdl == NULL) { 89137e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 892b6e66be2SVincenzo Maffione nm_prerr("fail to allocate tempMdl"); 89337e3a6d3SLuigi Rizzo IoFreeMdl(mainMdl); 89437e3a6d3SLuigi Rizzo return NULL; 89537e3a6d3SLuigi Rizzo } 89637e3a6d3SLuigi Rizzo pSrc = MmGetMdlPfnArray(tempMdl); 89737e3a6d3SLuigi Rizzo /* create one entry per cluster, the lut[] has one entry per object */ 89837e3a6d3SLuigi Rizzo for (j = 0; j < p->numclusters; j++, ofs += clsz) { 89937e3a6d3SLuigi Rizzo pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)]; 90037e3a6d3SLuigi Rizzo MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz); 90137e3a6d3SLuigi Rizzo MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */ 90237e3a6d3SLuigi Rizzo RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */ 90337e3a6d3SLuigi Rizzo mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */ 90437e3a6d3SLuigi Rizzo } 90537e3a6d3SLuigi Rizzo IoFreeMdl(tempMdl); 90637e3a6d3SLuigi Rizzo } 90737e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 90837e3a6d3SLuigi Rizzo return mainMdl; 90937e3a6d3SLuigi Rizzo } 91037e3a6d3SLuigi Rizzo 91137e3a6d3SLuigi Rizzo #endif /* _WIN32 */ 91237e3a6d3SLuigi Rizzo 91337e3a6d3SLuigi Rizzo /* 91437e3a6d3SLuigi Rizzo * helper function for OS-specific mmap routines (currently only windows). 91537e3a6d3SLuigi Rizzo * Given an nmd and a pool index, returns the cluster size and number of clusters. 91637e3a6d3SLuigi Rizzo * Returns 0 if memory is finalised and the pool is valid, otherwise 1. 91737e3a6d3SLuigi Rizzo * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change. 91837e3a6d3SLuigi Rizzo */ 91937e3a6d3SLuigi Rizzo 92037e3a6d3SLuigi Rizzo int 92137e3a6d3SLuigi Rizzo netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters) 92237e3a6d3SLuigi Rizzo { 92337e3a6d3SLuigi Rizzo if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR) 92437e3a6d3SLuigi Rizzo return 1; /* invalid arguments */ 92537e3a6d3SLuigi Rizzo // NMA_LOCK_ASSERT(nmd); 92637e3a6d3SLuigi Rizzo if (!(nmd->flags & NETMAP_MEM_FINALIZED)) { 92737e3a6d3SLuigi Rizzo *clustsize = *numclusters = 0; 92837e3a6d3SLuigi Rizzo return 1; /* not ready yet */ 92937e3a6d3SLuigi Rizzo } 93037e3a6d3SLuigi Rizzo *clustsize = nmd->pools[pool]._clustsize; 93137e3a6d3SLuigi Rizzo *numclusters = nmd->pools[pool].numclusters; 93237e3a6d3SLuigi Rizzo return 0; /* success */ 933ccdc3305SLuigi Rizzo } 934ccdc3305SLuigi Rizzo 935847bf383SLuigi Rizzo static int 9362ff91c17SVincenzo Maffione netmap_mem2_get_info(struct netmap_mem_d* nmd, uint64_t* size, 9372ff91c17SVincenzo Maffione u_int *memflags, nm_memid_t *id) 938ce3ee1e7SLuigi Rizzo { 939ce3ee1e7SLuigi Rizzo int error = 0; 940847bf383SLuigi Rizzo error = netmap_mem_config(nmd); 941ce3ee1e7SLuigi Rizzo if (error) 942ce3ee1e7SLuigi Rizzo goto out; 9434bf50f18SLuigi Rizzo if (size) { 944ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 945ce3ee1e7SLuigi Rizzo *size = nmd->nm_totalsize; 946ce3ee1e7SLuigi Rizzo } else { 947ce3ee1e7SLuigi Rizzo int i; 948ce3ee1e7SLuigi Rizzo *size = 0; 949ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 950ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = nmd->pools + i; 951253b2ec1SVincenzo Maffione *size += ((size_t)p->_numclusters * (size_t)p->_clustsize); 952ce3ee1e7SLuigi Rizzo } 953ce3ee1e7SLuigi Rizzo } 9544bf50f18SLuigi Rizzo } 9554bf50f18SLuigi Rizzo if (memflags) 956ce3ee1e7SLuigi Rizzo *memflags = nmd->flags; 9574bf50f18SLuigi Rizzo if (id) 958f0ea3689SLuigi Rizzo *id = nmd->nm_id; 959ce3ee1e7SLuigi Rizzo out: 960ce3ee1e7SLuigi Rizzo return error; 961ce3ee1e7SLuigi Rizzo } 962ce3ee1e7SLuigi Rizzo 963ccdc3305SLuigi Rizzo /* 964ccdc3305SLuigi Rizzo * we store objects by kernel address, need to find the offset 965ccdc3305SLuigi Rizzo * within the pool to export the value to userspace. 966ccdc3305SLuigi Rizzo * Algorithm: scan until we find the cluster, then add the 967ccdc3305SLuigi Rizzo * actual offset in the cluster 968ccdc3305SLuigi Rizzo */ 969ce2cb792SLuigi Rizzo static ssize_t 970ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr) 971ccdc3305SLuigi Rizzo { 972ce3ee1e7SLuigi Rizzo int i, k = p->_clustentries, n = p->objtotal; 973ccdc3305SLuigi Rizzo ssize_t ofs = 0; 974ccdc3305SLuigi Rizzo 975ccdc3305SLuigi Rizzo for (i = 0; i < n; i += k, ofs += p->_clustsize) { 976ccdc3305SLuigi Rizzo const char *base = p->lut[i].vaddr; 977ccdc3305SLuigi Rizzo ssize_t relofs = (const char *) vaddr - base; 978ccdc3305SLuigi Rizzo 979aa76317cSLuigi Rizzo if (relofs < 0 || relofs >= p->_clustsize) 980ccdc3305SLuigi Rizzo continue; 981ccdc3305SLuigi Rizzo 982ccdc3305SLuigi Rizzo ofs = ofs + relofs; 98375f4f3edSVincenzo Maffione nm_prdis("%s: return offset %d (cluster %d) for pointer %p", 984ccdc3305SLuigi Rizzo p->name, ofs, i, vaddr); 985ccdc3305SLuigi Rizzo return ofs; 986ccdc3305SLuigi Rizzo } 987b6e66be2SVincenzo Maffione nm_prerr("address %p is not contained inside any cluster (%s)", 988ccdc3305SLuigi Rizzo vaddr, p->name); 989ccdc3305SLuigi Rizzo return 0; /* An error occurred */ 990ccdc3305SLuigi Rizzo } 991ccdc3305SLuigi Rizzo 992ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */ 993ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v) \ 994ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v)) 995ccdc3305SLuigi Rizzo 996ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v) \ 997ce3ee1e7SLuigi Rizzo ((n)->pools[NETMAP_IF_POOL].memtotal + \ 998ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v))) 999ccdc3305SLuigi Rizzo 1000847bf383SLuigi Rizzo static ssize_t 1001847bf383SLuigi Rizzo netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr) 1002ce3ee1e7SLuigi Rizzo { 10032ff91c17SVincenzo Maffione return netmap_if_offset(nmd, addr); 1004ce3ee1e7SLuigi Rizzo } 1005ce3ee1e7SLuigi Rizzo 10068241616dSLuigi Rizzo /* 10078241616dSLuigi Rizzo * report the index, and use start position as a hint, 10088241616dSLuigi Rizzo * otherwise buffer allocation becomes terribly expensive. 10098241616dSLuigi Rizzo */ 1010ccdc3305SLuigi Rizzo static void * 1011ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index) 1012ccdc3305SLuigi Rizzo { 1013ccdc3305SLuigi Rizzo uint32_t i = 0; /* index in the bitmap */ 101437e3a6d3SLuigi Rizzo uint32_t mask, j = 0; /* slot counter */ 1015ccdc3305SLuigi Rizzo void *vaddr = NULL; 1016ccdc3305SLuigi Rizzo 1017ccdc3305SLuigi Rizzo if (len > p->_objsize) { 1018b6e66be2SVincenzo Maffione nm_prerr("%s request size %d too large", p->name, len); 1019ccdc3305SLuigi Rizzo return NULL; 1020ccdc3305SLuigi Rizzo } 1021ccdc3305SLuigi Rizzo 1022ccdc3305SLuigi Rizzo if (p->objfree == 0) { 1023b6e66be2SVincenzo Maffione nm_prerr("no more %s objects", p->name); 1024ccdc3305SLuigi Rizzo return NULL; 1025ccdc3305SLuigi Rizzo } 10268241616dSLuigi Rizzo if (start) 10278241616dSLuigi Rizzo i = *start; 1028ccdc3305SLuigi Rizzo 10298241616dSLuigi Rizzo /* termination is guaranteed by p->free, but better check bounds on i */ 10308241616dSLuigi Rizzo while (vaddr == NULL && i < p->bitmap_slots) { 1031ccdc3305SLuigi Rizzo uint32_t cur = p->bitmap[i]; 1032ccdc3305SLuigi Rizzo if (cur == 0) { /* bitmask is fully used */ 1033ccdc3305SLuigi Rizzo i++; 1034ccdc3305SLuigi Rizzo continue; 1035ccdc3305SLuigi Rizzo } 1036ccdc3305SLuigi Rizzo /* locate a slot */ 1037ccdc3305SLuigi Rizzo for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) 1038ccdc3305SLuigi Rizzo ; 1039ccdc3305SLuigi Rizzo 1040ccdc3305SLuigi Rizzo p->bitmap[i] &= ~mask; /* mark object as in use */ 1041ccdc3305SLuigi Rizzo p->objfree--; 1042ccdc3305SLuigi Rizzo 1043ccdc3305SLuigi Rizzo vaddr = p->lut[i * 32 + j].vaddr; 10448241616dSLuigi Rizzo if (index) 10458241616dSLuigi Rizzo *index = i * 32 + j; 1046ccdc3305SLuigi Rizzo } 104775f4f3edSVincenzo Maffione nm_prdis("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr); 1048ccdc3305SLuigi Rizzo 10498241616dSLuigi Rizzo if (start) 10508241616dSLuigi Rizzo *start = i; 1051ccdc3305SLuigi Rizzo return vaddr; 1052ccdc3305SLuigi Rizzo } 1053ccdc3305SLuigi Rizzo 1054ccdc3305SLuigi Rizzo 1055ccdc3305SLuigi Rizzo /* 1056f0ea3689SLuigi Rizzo * free by index, not by address. 1057f0ea3689SLuigi Rizzo * XXX should we also cleanup the content ? 1058ccdc3305SLuigi Rizzo */ 1059f0ea3689SLuigi Rizzo static int 1060ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j) 1061ccdc3305SLuigi Rizzo { 1062f0ea3689SLuigi Rizzo uint32_t *ptr, mask; 1063f0ea3689SLuigi Rizzo 1064ccdc3305SLuigi Rizzo if (j >= p->objtotal) { 1065b6e66be2SVincenzo Maffione nm_prerr("invalid index %u, max %u", j, p->objtotal); 1066f0ea3689SLuigi Rizzo return 1; 1067ccdc3305SLuigi Rizzo } 1068f0ea3689SLuigi Rizzo ptr = &p->bitmap[j / 32]; 1069f0ea3689SLuigi Rizzo mask = (1 << (j % 32)); 1070f0ea3689SLuigi Rizzo if (*ptr & mask) { 1071b6e66be2SVincenzo Maffione nm_prerr("ouch, double free on buffer %d", j); 1072f0ea3689SLuigi Rizzo return 1; 1073f0ea3689SLuigi Rizzo } else { 1074f0ea3689SLuigi Rizzo *ptr |= mask; 1075ccdc3305SLuigi Rizzo p->objfree++; 1076f0ea3689SLuigi Rizzo return 0; 1077f0ea3689SLuigi Rizzo } 1078ccdc3305SLuigi Rizzo } 1079ccdc3305SLuigi Rizzo 1080f0ea3689SLuigi Rizzo /* 1081f0ea3689SLuigi Rizzo * free by address. This is slow but is only used for a few 1082f0ea3689SLuigi Rizzo * objects (rings, nifp) 1083f0ea3689SLuigi Rizzo */ 1084ccdc3305SLuigi Rizzo static void 1085ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr) 1086ccdc3305SLuigi Rizzo { 1087ce3ee1e7SLuigi Rizzo u_int i, j, n = p->numclusters; 1088ccdc3305SLuigi Rizzo 1089ce3ee1e7SLuigi Rizzo for (i = 0, j = 0; i < n; i++, j += p->_clustentries) { 1090ce3ee1e7SLuigi Rizzo void *base = p->lut[i * p->_clustentries].vaddr; 1091ccdc3305SLuigi Rizzo ssize_t relofs = (ssize_t) vaddr - (ssize_t) base; 1092ccdc3305SLuigi Rizzo 1093ccdc3305SLuigi Rizzo /* Given address, is out of the scope of the current cluster.*/ 10944f80b14cSVincenzo Maffione if (base == NULL || vaddr < base || relofs >= p->_clustsize) 1095ccdc3305SLuigi Rizzo continue; 1096ccdc3305SLuigi Rizzo 1097ccdc3305SLuigi Rizzo j = j + relofs / p->_objsize; 1098ce3ee1e7SLuigi Rizzo /* KASSERT(j != 0, ("Cannot free object 0")); */ 1099ccdc3305SLuigi Rizzo netmap_obj_free(p, j); 1100ccdc3305SLuigi Rizzo return; 1101ccdc3305SLuigi Rizzo } 1102b6e66be2SVincenzo Maffione nm_prerr("address %p is not contained inside any cluster (%s)", 1103ccdc3305SLuigi Rizzo vaddr, p->name); 1104ccdc3305SLuigi Rizzo } 1105ccdc3305SLuigi Rizzo 11064f80b14cSVincenzo Maffione unsigned 11074f80b14cSVincenzo Maffione netmap_mem_bufsize(struct netmap_mem_d *nmd) 11084f80b14cSVincenzo Maffione { 11094f80b14cSVincenzo Maffione return nmd->pools[NETMAP_BUF_POOL]._objsize; 11104f80b14cSVincenzo Maffione } 11114bf50f18SLuigi Rizzo 1112ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL) 1113ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v)) 1114ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL) 1115ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v)) 1116ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index) \ 11174bf50f18SLuigi Rizzo netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index) 1118ccdc3305SLuigi Rizzo 1119ccdc3305SLuigi Rizzo 11204f80b14cSVincenzo Maffione #if 0 /* currently unused */ 1121ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */ 1122ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v) \ 1123ce3ee1e7SLuigi Rizzo (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n)) 1124f0ea3689SLuigi Rizzo #endif 1125f0ea3689SLuigi Rizzo 1126f0ea3689SLuigi Rizzo /* 1127f0ea3689SLuigi Rizzo * allocate extra buffers in a linked list. 1128f0ea3689SLuigi Rizzo * returns the actual number. 1129f0ea3689SLuigi Rizzo */ 1130f0ea3689SLuigi Rizzo uint32_t 1131f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n) 1132f0ea3689SLuigi Rizzo { 1133f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 1134f0ea3689SLuigi Rizzo uint32_t i, pos = 0; /* opaque, scan position in the bitmap */ 1135f0ea3689SLuigi Rizzo 1136f0ea3689SLuigi Rizzo NMA_LOCK(nmd); 1137f0ea3689SLuigi Rizzo 1138f0ea3689SLuigi Rizzo *head = 0; /* default, 'null' index ie empty list */ 1139f0ea3689SLuigi Rizzo for (i = 0 ; i < n; i++) { 1140f0ea3689SLuigi Rizzo uint32_t cur = *head; /* save current head */ 1141f0ea3689SLuigi Rizzo uint32_t *p = netmap_buf_malloc(nmd, &pos, head); 1142f0ea3689SLuigi Rizzo if (p == NULL) { 1143b6e66be2SVincenzo Maffione nm_prerr("no more buffers after %d of %d", i, n); 1144f0ea3689SLuigi Rizzo *head = cur; /* restore */ 1145f0ea3689SLuigi Rizzo break; 1146f0ea3689SLuigi Rizzo } 114775f4f3edSVincenzo Maffione nm_prdis(5, "allocate buffer %d -> %d", *head, cur); 1148f0ea3689SLuigi Rizzo *p = cur; /* link to previous head */ 1149f0ea3689SLuigi Rizzo } 1150f0ea3689SLuigi Rizzo 1151f0ea3689SLuigi Rizzo NMA_UNLOCK(nmd); 1152f0ea3689SLuigi Rizzo 1153f0ea3689SLuigi Rizzo return i; 1154f0ea3689SLuigi Rizzo } 1155f0ea3689SLuigi Rizzo 1156f0ea3689SLuigi Rizzo static void 1157f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head) 1158f0ea3689SLuigi Rizzo { 1159847bf383SLuigi Rizzo struct lut_entry *lut = na->na_lut.lut; 1160f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 1161f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1162f0ea3689SLuigi Rizzo uint32_t i, cur, *buf; 1163f0ea3689SLuigi Rizzo 116475f4f3edSVincenzo Maffione nm_prdis("freeing the extra list"); 1165f0ea3689SLuigi Rizzo for (i = 0; head >=2 && head < p->objtotal; i++) { 1166f0ea3689SLuigi Rizzo cur = head; 1167f0ea3689SLuigi Rizzo buf = lut[head].vaddr; 1168f0ea3689SLuigi Rizzo head = *buf; 1169f0ea3689SLuigi Rizzo *buf = 0; 1170f0ea3689SLuigi Rizzo if (netmap_obj_free(p, cur)) 1171f0ea3689SLuigi Rizzo break; 1172f0ea3689SLuigi Rizzo } 1173f0ea3689SLuigi Rizzo if (head != 0) 1174b6e66be2SVincenzo Maffione nm_prerr("breaking with head %d", head); 1175b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1176b6e66be2SVincenzo Maffione nm_prinf("freed %d buffers", i); 1177f0ea3689SLuigi Rizzo } 1178ccdc3305SLuigi Rizzo 1179ccdc3305SLuigi Rizzo 11808241616dSLuigi Rizzo /* Return nonzero on error */ 11818241616dSLuigi Rizzo static int 1182f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 1183ccdc3305SLuigi Rizzo { 1184ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1185ce3ee1e7SLuigi Rizzo u_int i = 0; /* slot counter */ 11868241616dSLuigi Rizzo uint32_t pos = 0; /* slot in p->bitmap */ 11878241616dSLuigi Rizzo uint32_t index = 0; /* buffer index */ 1188ccdc3305SLuigi Rizzo 1189ccdc3305SLuigi Rizzo for (i = 0; i < n; i++) { 1190ce3ee1e7SLuigi Rizzo void *vaddr = netmap_buf_malloc(nmd, &pos, &index); 1191ccdc3305SLuigi Rizzo if (vaddr == NULL) { 1192b6e66be2SVincenzo Maffione nm_prerr("no more buffers after %d of %d", i, n); 1193ccdc3305SLuigi Rizzo goto cleanup; 1194ccdc3305SLuigi Rizzo } 11958241616dSLuigi Rizzo slot[i].buf_idx = index; 1196ccdc3305SLuigi Rizzo slot[i].len = p->_objsize; 1197f9790aebSLuigi Rizzo slot[i].flags = 0; 11984f80b14cSVincenzo Maffione slot[i].ptr = 0; 1199ccdc3305SLuigi Rizzo } 1200ccdc3305SLuigi Rizzo 120175f4f3edSVincenzo Maffione nm_prdis("%s: allocated %d buffers, %d available, first at %d", p->name, n, p->objfree, pos); 12028241616dSLuigi Rizzo return (0); 1203ccdc3305SLuigi Rizzo 1204ccdc3305SLuigi Rizzo cleanup: 12054cf8455fSEd Maste while (i > 0) { 12064cf8455fSEd Maste i--; 12078241616dSLuigi Rizzo netmap_obj_free(p, slot[i].buf_idx); 1208ccdc3305SLuigi Rizzo } 12098241616dSLuigi Rizzo bzero(slot, n * sizeof(slot[0])); 12108241616dSLuigi Rizzo return (ENOMEM); 1211ccdc3305SLuigi Rizzo } 1212ccdc3305SLuigi Rizzo 1213f0ea3689SLuigi Rizzo static void 1214f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index) 1215f0ea3689SLuigi Rizzo { 1216f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1217f0ea3689SLuigi Rizzo u_int i; 1218f0ea3689SLuigi Rizzo 1219f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 1220f0ea3689SLuigi Rizzo slot[i].buf_idx = index; 1221f0ea3689SLuigi Rizzo slot[i].len = p->_objsize; 1222f0ea3689SLuigi Rizzo slot[i].flags = 0; 1223f0ea3689SLuigi Rizzo } 1224f0ea3689SLuigi Rizzo } 1225f0ea3689SLuigi Rizzo 1226ccdc3305SLuigi Rizzo 1227ccdc3305SLuigi Rizzo static void 1228f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i) 1229ccdc3305SLuigi Rizzo { 1230ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 12318241616dSLuigi Rizzo 1232ccdc3305SLuigi Rizzo if (i < 2 || i >= p->objtotal) { 1233b6e66be2SVincenzo Maffione nm_prerr("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal); 1234ccdc3305SLuigi Rizzo return; 1235ccdc3305SLuigi Rizzo } 12368241616dSLuigi Rizzo netmap_obj_free(p, i); 1237ccdc3305SLuigi Rizzo } 1238ccdc3305SLuigi Rizzo 1239f0ea3689SLuigi Rizzo 1240f0ea3689SLuigi Rizzo static void 1241f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 1242f0ea3689SLuigi Rizzo { 1243f0ea3689SLuigi Rizzo u_int i; 1244f0ea3689SLuigi Rizzo 1245f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 12462ff91c17SVincenzo Maffione if (slot[i].buf_idx > 1) 1247f0ea3689SLuigi Rizzo netmap_free_buf(nmd, slot[i].buf_idx); 1248f0ea3689SLuigi Rizzo } 124975f4f3edSVincenzo Maffione nm_prdis("%s: released some buffers, available: %u", 12502ff91c17SVincenzo Maffione p->name, p->objfree); 1251f0ea3689SLuigi Rizzo } 1252f0ea3689SLuigi Rizzo 12538241616dSLuigi Rizzo static void 12548241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p) 12558241616dSLuigi Rizzo { 1256ce3ee1e7SLuigi Rizzo 12578241616dSLuigi Rizzo if (p == NULL) 12588241616dSLuigi Rizzo return; 12598241616dSLuigi Rizzo if (p->bitmap) 1260c3e9b4dbSLuiz Otavio O Souza nm_os_free(p->bitmap); 12618241616dSLuigi Rizzo p->bitmap = NULL; 12624f80b14cSVincenzo Maffione if (p->invalid_bitmap) 12634f80b14cSVincenzo Maffione nm_os_free(p->invalid_bitmap); 12644f80b14cSVincenzo Maffione p->invalid_bitmap = NULL; 12652ff91c17SVincenzo Maffione if (!p->alloc_done) { 12662ff91c17SVincenzo Maffione /* allocation was done by somebody else. 12672ff91c17SVincenzo Maffione * Let them clean up after themselves. 12682ff91c17SVincenzo Maffione */ 12692ff91c17SVincenzo Maffione return; 12702ff91c17SVincenzo Maffione } 12718241616dSLuigi Rizzo if (p->lut) { 1272ce3ee1e7SLuigi Rizzo u_int i; 1273ce3ee1e7SLuigi Rizzo 1274dd4fcbc5SPatrick Kelsey /* 1275dd4fcbc5SPatrick Kelsey * Free each cluster allocated in 1276dd4fcbc5SPatrick Kelsey * netmap_finalize_obj_allocator(). The cluster start 1277dd4fcbc5SPatrick Kelsey * addresses are stored at multiples of p->_clusterentries 1278dd4fcbc5SPatrick Kelsey * in the lut. 1279dd4fcbc5SPatrick Kelsey */ 1280ce3ee1e7SLuigi Rizzo for (i = 0; i < p->objtotal; i += p->_clustentries) { 128137e3a6d3SLuigi Rizzo contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP); 12828241616dSLuigi Rizzo } 12834f80b14cSVincenzo Maffione nm_free_lut(p->lut, p->objtotal); 12848241616dSLuigi Rizzo } 12858241616dSLuigi Rizzo p->lut = NULL; 1286ce3ee1e7SLuigi Rizzo p->objtotal = 0; 1287ce3ee1e7SLuigi Rizzo p->memtotal = 0; 1288ce3ee1e7SLuigi Rizzo p->numclusters = 0; 1289ce3ee1e7SLuigi Rizzo p->objfree = 0; 12902ff91c17SVincenzo Maffione p->alloc_done = 0; 12918241616dSLuigi Rizzo } 1292ccdc3305SLuigi Rizzo 1293ccdc3305SLuigi Rizzo /* 1294ccdc3305SLuigi Rizzo * Free all resources related to an allocator. 1295ccdc3305SLuigi Rizzo */ 1296ccdc3305SLuigi Rizzo static void 1297ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p) 1298ccdc3305SLuigi Rizzo { 1299ccdc3305SLuigi Rizzo if (p == NULL) 1300ccdc3305SLuigi Rizzo return; 13018241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 1302ccdc3305SLuigi Rizzo } 1303ccdc3305SLuigi Rizzo 1304ccdc3305SLuigi Rizzo /* 1305ccdc3305SLuigi Rizzo * We receive a request for objtotal objects, of size objsize each. 1306ccdc3305SLuigi Rizzo * Internally we may round up both numbers, as we allocate objects 1307ccdc3305SLuigi Rizzo * in small clusters multiple of the page size. 1308ce3ee1e7SLuigi Rizzo * We need to keep track of objtotal and clustentries, 1309ccdc3305SLuigi Rizzo * as they are needed when freeing memory. 1310ccdc3305SLuigi Rizzo * 1311ccdc3305SLuigi Rizzo * XXX note -- userspace needs the buffers to be contiguous, 1312ccdc3305SLuigi Rizzo * so we cannot afford gaps at the end of a cluster. 1313ccdc3305SLuigi Rizzo */ 13148241616dSLuigi Rizzo 13158241616dSLuigi Rizzo 13168241616dSLuigi Rizzo /* call with NMA_LOCK held */ 13178241616dSLuigi Rizzo static int 13188241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize) 1319ccdc3305SLuigi Rizzo { 1320ce3ee1e7SLuigi Rizzo int i; 1321ccdc3305SLuigi Rizzo u_int clustsize; /* the cluster size, multiple of page size */ 1322ccdc3305SLuigi Rizzo u_int clustentries; /* how many objects per entry */ 1323ccdc3305SLuigi Rizzo 1324ce3ee1e7SLuigi Rizzo /* we store the current request, so we can 1325ce3ee1e7SLuigi Rizzo * detect configuration changes later */ 1326ce3ee1e7SLuigi Rizzo p->r_objtotal = objtotal; 1327ce3ee1e7SLuigi Rizzo p->r_objsize = objsize; 1328ce3ee1e7SLuigi Rizzo 13294bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE (1<<22) // 4 MB 133017885a7bSLuigi Rizzo #define LINE_ROUND NM_CACHE_ALIGN // 64 1331ccdc3305SLuigi Rizzo if (objsize >= MAX_CLUSTSIZE) { 1332ccdc3305SLuigi Rizzo /* we could do it but there is no point */ 1333b6e66be2SVincenzo Maffione nm_prerr("unsupported allocation for %d bytes", objsize); 1334ce3ee1e7SLuigi Rizzo return EINVAL; 1335ccdc3305SLuigi Rizzo } 1336ccdc3305SLuigi Rizzo /* make sure objsize is a multiple of LINE_ROUND */ 1337ccdc3305SLuigi Rizzo i = (objsize & (LINE_ROUND - 1)); 1338ccdc3305SLuigi Rizzo if (i) { 1339b6e66be2SVincenzo Maffione nm_prinf("aligning object by %d bytes", LINE_ROUND - i); 1340ccdc3305SLuigi Rizzo objsize += LINE_ROUND - i; 1341ccdc3305SLuigi Rizzo } 13428241616dSLuigi Rizzo if (objsize < p->objminsize || objsize > p->objmaxsize) { 1343b6e66be2SVincenzo Maffione nm_prerr("requested objsize %d out of range [%d, %d]", 13448241616dSLuigi Rizzo objsize, p->objminsize, p->objmaxsize); 1345ce3ee1e7SLuigi Rizzo return EINVAL; 13468241616dSLuigi Rizzo } 13478241616dSLuigi Rizzo if (objtotal < p->nummin || objtotal > p->nummax) { 1348b6e66be2SVincenzo Maffione nm_prerr("requested objtotal %d out of range [%d, %d]", 13498241616dSLuigi Rizzo objtotal, p->nummin, p->nummax); 1350ce3ee1e7SLuigi Rizzo return EINVAL; 13518241616dSLuigi Rizzo } 1352ccdc3305SLuigi Rizzo /* 1353ccdc3305SLuigi Rizzo * Compute number of objects using a brute-force approach: 1354ccdc3305SLuigi Rizzo * given a max cluster size, 1355ccdc3305SLuigi Rizzo * we try to fill it with objects keeping track of the 1356ccdc3305SLuigi Rizzo * wasted space to the next page boundary. 1357ccdc3305SLuigi Rizzo */ 1358ccdc3305SLuigi Rizzo for (clustentries = 0, i = 1;; i++) { 1359ccdc3305SLuigi Rizzo u_int delta, used = i * objsize; 1360ccdc3305SLuigi Rizzo if (used > MAX_CLUSTSIZE) 1361ccdc3305SLuigi Rizzo break; 1362ccdc3305SLuigi Rizzo delta = used % PAGE_SIZE; 1363ccdc3305SLuigi Rizzo if (delta == 0) { // exact solution 1364ccdc3305SLuigi Rizzo clustentries = i; 1365ccdc3305SLuigi Rizzo break; 1366ccdc3305SLuigi Rizzo } 1367ccdc3305SLuigi Rizzo } 13684bf50f18SLuigi Rizzo /* exact solution not found */ 13694bf50f18SLuigi Rizzo if (clustentries == 0) { 1370b6e66be2SVincenzo Maffione nm_prerr("unsupported allocation for %d bytes", objsize); 13714bf50f18SLuigi Rizzo return EINVAL; 13724bf50f18SLuigi Rizzo } 13734bf50f18SLuigi Rizzo /* compute clustsize */ 1374ccdc3305SLuigi Rizzo clustsize = clustentries * objsize; 1375b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1376b6e66be2SVincenzo Maffione nm_prinf("objsize %d clustsize %d objects %d", 1377ccdc3305SLuigi Rizzo objsize, clustsize, clustentries); 1378ccdc3305SLuigi Rizzo 1379ccdc3305SLuigi Rizzo /* 1380ccdc3305SLuigi Rizzo * The number of clusters is n = ceil(objtotal/clustentries) 1381ccdc3305SLuigi Rizzo * objtotal' = n * clustentries 1382ccdc3305SLuigi Rizzo */ 1383ce3ee1e7SLuigi Rizzo p->_clustentries = clustentries; 1384ccdc3305SLuigi Rizzo p->_clustsize = clustsize; 1385ce3ee1e7SLuigi Rizzo p->_numclusters = (objtotal + clustentries - 1) / clustentries; 1386ce3ee1e7SLuigi Rizzo 1387ce3ee1e7SLuigi Rizzo /* actual values (may be larger than requested) */ 13888241616dSLuigi Rizzo p->_objsize = objsize; 1389ce3ee1e7SLuigi Rizzo p->_objtotal = p->_numclusters * clustentries; 1390ccdc3305SLuigi Rizzo 13918241616dSLuigi Rizzo return 0; 13928241616dSLuigi Rizzo } 13938241616dSLuigi Rizzo 13948241616dSLuigi Rizzo /* call with NMA_LOCK held */ 13958241616dSLuigi Rizzo static int 13968241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p) 13978241616dSLuigi Rizzo { 1398ce3ee1e7SLuigi Rizzo int i; /* must be signed */ 1399ce3ee1e7SLuigi Rizzo size_t n; 1400ce3ee1e7SLuigi Rizzo 14014f80b14cSVincenzo Maffione if (p->lut) { 14022ff91c17SVincenzo Maffione /* if the lut is already there we assume that also all the 14032ff91c17SVincenzo Maffione * clusters have already been allocated, possibily by somebody 14042ff91c17SVincenzo Maffione * else (e.g., extmem). In the latter case, the alloc_done flag 14052ff91c17SVincenzo Maffione * will remain at zero, so that we will not attempt to 14062ff91c17SVincenzo Maffione * deallocate the clusters by ourselves in 14072ff91c17SVincenzo Maffione * netmap_reset_obj_allocator. 14082ff91c17SVincenzo Maffione */ 14094f80b14cSVincenzo Maffione return 0; 14104f80b14cSVincenzo Maffione } 14114f80b14cSVincenzo Maffione 1412ce3ee1e7SLuigi Rizzo /* optimistically assume we have enough memory */ 1413ce3ee1e7SLuigi Rizzo p->numclusters = p->_numclusters; 1414ce3ee1e7SLuigi Rizzo p->objtotal = p->_objtotal; 14152ff91c17SVincenzo Maffione p->alloc_done = 1; 14168241616dSLuigi Rizzo 141737e3a6d3SLuigi Rizzo p->lut = nm_alloc_lut(p->objtotal); 1418ccdc3305SLuigi Rizzo if (p->lut == NULL) { 1419b6e66be2SVincenzo Maffione nm_prerr("Unable to create lookup table for '%s'", p->name); 1420ccdc3305SLuigi Rizzo goto clean; 1421ccdc3305SLuigi Rizzo } 1422ccdc3305SLuigi Rizzo 1423ccdc3305SLuigi Rizzo /* 14244f80b14cSVincenzo Maffione * Allocate clusters, init pointers 1425ccdc3305SLuigi Rizzo */ 1426ce3ee1e7SLuigi Rizzo 1427ce3ee1e7SLuigi Rizzo n = p->_clustsize; 1428ce3ee1e7SLuigi Rizzo for (i = 0; i < (int)p->objtotal;) { 1429ce3ee1e7SLuigi Rizzo int lim = i + p->_clustentries; 1430ccdc3305SLuigi Rizzo char *clust; 1431ccdc3305SLuigi Rizzo 143237e3a6d3SLuigi Rizzo /* 143337e3a6d3SLuigi Rizzo * XXX Note, we only need contigmalloc() for buffers attached 143437e3a6d3SLuigi Rizzo * to native interfaces. In all other cases (nifp, netmap rings 143537e3a6d3SLuigi Rizzo * and even buffers for VALE ports or emulated interfaces) we 143637e3a6d3SLuigi Rizzo * can live with standard malloc, because the hardware will not 143737e3a6d3SLuigi Rizzo * access the pages directly. 143837e3a6d3SLuigi Rizzo */ 1439ce3ee1e7SLuigi Rizzo clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO, 1440ce3ee1e7SLuigi Rizzo (size_t)0, -1UL, PAGE_SIZE, 0); 1441ccdc3305SLuigi Rizzo if (clust == NULL) { 1442ccdc3305SLuigi Rizzo /* 1443ccdc3305SLuigi Rizzo * If we get here, there is a severe memory shortage, 1444ccdc3305SLuigi Rizzo * so halve the allocated memory to reclaim some. 1445ccdc3305SLuigi Rizzo */ 1446b6e66be2SVincenzo Maffione nm_prerr("Unable to create cluster at %d for '%s' allocator", 14478241616dSLuigi Rizzo i, p->name); 1448ce3ee1e7SLuigi Rizzo if (i < 2) /* nothing to halve */ 1449ce3ee1e7SLuigi Rizzo goto out; 1450ccdc3305SLuigi Rizzo lim = i / 2; 14518241616dSLuigi Rizzo for (i--; i >= lim; i--) { 1452ce3ee1e7SLuigi Rizzo if (i % p->_clustentries == 0 && p->lut[i].vaddr) 1453ccdc3305SLuigi Rizzo contigfree(p->lut[i].vaddr, 1454ce3ee1e7SLuigi Rizzo n, M_NETMAP); 1455dd4fcbc5SPatrick Kelsey p->lut[i].vaddr = NULL; 1456ccdc3305SLuigi Rizzo } 1457ce3ee1e7SLuigi Rizzo out: 1458ccdc3305SLuigi Rizzo p->objtotal = i; 1459ce3ee1e7SLuigi Rizzo /* we may have stopped in the middle of a cluster */ 1460ce3ee1e7SLuigi Rizzo p->numclusters = (i + p->_clustentries - 1) / p->_clustentries; 1461ccdc3305SLuigi Rizzo break; 1462ccdc3305SLuigi Rizzo } 1463dd4fcbc5SPatrick Kelsey /* 14644f80b14cSVincenzo Maffione * Set lut state for all buffers in the current cluster. 1465dd4fcbc5SPatrick Kelsey * 1466dd4fcbc5SPatrick Kelsey * [i, lim) is the set of buffer indexes that cover the 1467dd4fcbc5SPatrick Kelsey * current cluster. 1468dd4fcbc5SPatrick Kelsey * 1469dd4fcbc5SPatrick Kelsey * 'clust' is really the address of the current buffer in 1470dd4fcbc5SPatrick Kelsey * the current cluster as we index through it with a stride 1471dd4fcbc5SPatrick Kelsey * of p->_objsize. 1472dd4fcbc5SPatrick Kelsey */ 14738241616dSLuigi Rizzo for (; i < lim; i++, clust += p->_objsize) { 1474ccdc3305SLuigi Rizzo p->lut[i].vaddr = clust; 14754f80b14cSVincenzo Maffione #if !defined(linux) && !defined(_WIN32) 1476ccdc3305SLuigi Rizzo p->lut[i].paddr = vtophys(clust); 14774f80b14cSVincenzo Maffione #endif 1478ccdc3305SLuigi Rizzo } 1479ccdc3305SLuigi Rizzo } 1480253b2ec1SVincenzo Maffione p->memtotal = (size_t)p->numclusters * (size_t)p->_clustsize; 1481ae10d1afSLuigi Rizzo if (netmap_verbose) 1482253b2ec1SVincenzo Maffione nm_prinf("Pre-allocated %d clusters (%d/%zuKB) for '%s'", 1483ce3ee1e7SLuigi Rizzo p->numclusters, p->_clustsize >> 10, 1484ce3ee1e7SLuigi Rizzo p->memtotal >> 10, p->name); 1485ccdc3305SLuigi Rizzo 14868241616dSLuigi Rizzo return 0; 1487ccdc3305SLuigi Rizzo 1488ccdc3305SLuigi Rizzo clean: 14898241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 14908241616dSLuigi Rizzo return ENOMEM; 14918241616dSLuigi Rizzo } 14928241616dSLuigi Rizzo 14938241616dSLuigi Rizzo /* call with lock held */ 14948241616dSLuigi Rizzo static int 1495c3e9b4dbSLuiz Otavio O Souza netmap_mem_params_changed(struct netmap_obj_params* p) 14968241616dSLuigi Rizzo { 1497c3e9b4dbSLuiz Otavio O Souza int i, rv = 0; 14988241616dSLuigi Rizzo 14998241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1500c3e9b4dbSLuiz Otavio O Souza if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) { 1501c3e9b4dbSLuiz Otavio O Souza p[i].last_size = p[i].size; 1502c3e9b4dbSLuiz Otavio O Souza p[i].last_num = p[i].num; 1503c3e9b4dbSLuiz Otavio O Souza rv = 1; 15048241616dSLuigi Rizzo } 1505c3e9b4dbSLuiz Otavio O Souza } 1506c3e9b4dbSLuiz Otavio O Souza return rv; 15078241616dSLuigi Rizzo } 15088241616dSLuigi Rizzo 1509ce3ee1e7SLuigi Rizzo static void 1510ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd) 1511ce3ee1e7SLuigi Rizzo { 1512ce3ee1e7SLuigi Rizzo int i; 1513f0ea3689SLuigi Rizzo 1514b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1515b6e66be2SVincenzo Maffione nm_prinf("resetting %p", nmd); 1516ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1517ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 1518ce3ee1e7SLuigi Rizzo } 1519ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 1520ce3ee1e7SLuigi Rizzo } 1521ce3ee1e7SLuigi Rizzo 1522ce3ee1e7SLuigi Rizzo static int 15234bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na) 15244bf50f18SLuigi Rizzo { 15252ff91c17SVincenzo Maffione int i, lim = p->objtotal; 15264f80b14cSVincenzo Maffione struct netmap_lut *lut = &na->na_lut; 15274bf50f18SLuigi Rizzo 1528c3e9b4dbSLuiz Otavio O Souza if (na == NULL || na->pdev == NULL) 15294bf50f18SLuigi Rizzo return 0; 15304bf50f18SLuigi Rizzo 153137e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 1532cfa866f6SMatt Macy /* On FreeBSD mapping and unmapping is performed by the txsync 1533cfa866f6SMatt Macy * and rxsync routine, packet by packet. */ 15344bf50f18SLuigi Rizzo (void)i; 15354bf50f18SLuigi Rizzo (void)lim; 15364f80b14cSVincenzo Maffione (void)lut; 153737e3a6d3SLuigi Rizzo #elif defined(_WIN32) 153837e3a6d3SLuigi Rizzo (void)i; 153937e3a6d3SLuigi Rizzo (void)lim; 15404f80b14cSVincenzo Maffione (void)lut; 1541b6e66be2SVincenzo Maffione nm_prerr("unsupported on Windows"); 15424bf50f18SLuigi Rizzo #else /* linux */ 154375f4f3edSVincenzo Maffione nm_prdis("unmapping and freeing plut for %s", na->name); 15444f80b14cSVincenzo Maffione if (lut->plut == NULL) 15454f80b14cSVincenzo Maffione return 0; 15464f80b14cSVincenzo Maffione for (i = 0; i < lim; i += p->_clustentries) { 15474f80b14cSVincenzo Maffione if (lut->plut[i].paddr) 15484f80b14cSVincenzo Maffione netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, p->_clustsize); 15494bf50f18SLuigi Rizzo } 15504f80b14cSVincenzo Maffione nm_free_plut(lut->plut); 15514f80b14cSVincenzo Maffione lut->plut = NULL; 15524bf50f18SLuigi Rizzo #endif /* linux */ 15534bf50f18SLuigi Rizzo 15544bf50f18SLuigi Rizzo return 0; 15554bf50f18SLuigi Rizzo } 15564bf50f18SLuigi Rizzo 15574bf50f18SLuigi Rizzo static int 15584bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na) 15594bf50f18SLuigi Rizzo { 15604f80b14cSVincenzo Maffione int error = 0; 15614f80b14cSVincenzo Maffione int i, lim = p->objtotal; 15624f80b14cSVincenzo Maffione struct netmap_lut *lut = &na->na_lut; 15634bf50f18SLuigi Rizzo 15644bf50f18SLuigi Rizzo if (na->pdev == NULL) 15654bf50f18SLuigi Rizzo return 0; 15664bf50f18SLuigi Rizzo 15674f80b14cSVincenzo Maffione #if defined(__FreeBSD__) 1568cfa866f6SMatt Macy /* On FreeBSD mapping and unmapping is performed by the txsync 1569cfa866f6SMatt Macy * and rxsync routine, packet by packet. */ 15704f80b14cSVincenzo Maffione (void)i; 15714f80b14cSVincenzo Maffione (void)lim; 15724f80b14cSVincenzo Maffione (void)lut; 15734f80b14cSVincenzo Maffione #elif defined(_WIN32) 15744f80b14cSVincenzo Maffione (void)i; 15754f80b14cSVincenzo Maffione (void)lim; 15764f80b14cSVincenzo Maffione (void)lut; 1577b6e66be2SVincenzo Maffione nm_prerr("unsupported on Windows"); 15784f80b14cSVincenzo Maffione #else /* linux */ 15794f80b14cSVincenzo Maffione 15804f80b14cSVincenzo Maffione if (lut->plut != NULL) { 158175f4f3edSVincenzo Maffione nm_prdis("plut already allocated for %s", na->name); 15824f80b14cSVincenzo Maffione return 0; 15834bf50f18SLuigi Rizzo } 15844f80b14cSVincenzo Maffione 158575f4f3edSVincenzo Maffione nm_prdis("allocating physical lut for %s", na->name); 15864f80b14cSVincenzo Maffione lut->plut = nm_alloc_plut(lim); 15874f80b14cSVincenzo Maffione if (lut->plut == NULL) { 1588b6e66be2SVincenzo Maffione nm_prerr("Failed to allocate physical lut for %s", na->name); 15894f80b14cSVincenzo Maffione return ENOMEM; 15904f80b14cSVincenzo Maffione } 15914f80b14cSVincenzo Maffione 15924f80b14cSVincenzo Maffione for (i = 0; i < lim; i += p->_clustentries) { 15934f80b14cSVincenzo Maffione lut->plut[i].paddr = 0; 15944f80b14cSVincenzo Maffione } 15954f80b14cSVincenzo Maffione 15964f80b14cSVincenzo Maffione for (i = 0; i < lim; i += p->_clustentries) { 15974f80b14cSVincenzo Maffione int j; 15984f80b14cSVincenzo Maffione 15994f80b14cSVincenzo Maffione if (p->lut[i].vaddr == NULL) 16004f80b14cSVincenzo Maffione continue; 16014f80b14cSVincenzo Maffione 16024f80b14cSVincenzo Maffione error = netmap_load_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, 16034f80b14cSVincenzo Maffione p->lut[i].vaddr, p->_clustsize); 16044f80b14cSVincenzo Maffione if (error) { 1605b6e66be2SVincenzo Maffione nm_prerr("Failed to map cluster #%d from the %s pool", i, p->name); 16064f80b14cSVincenzo Maffione break; 16074f80b14cSVincenzo Maffione } 16084f80b14cSVincenzo Maffione 16094f80b14cSVincenzo Maffione for (j = 1; j < p->_clustentries; j++) { 16104f80b14cSVincenzo Maffione lut->plut[i + j].paddr = lut->plut[i + j - 1].paddr + p->_objsize; 16114f80b14cSVincenzo Maffione } 16124f80b14cSVincenzo Maffione } 16134f80b14cSVincenzo Maffione 16144f80b14cSVincenzo Maffione if (error) 16154f80b14cSVincenzo Maffione netmap_mem_unmap(p, na); 16164f80b14cSVincenzo Maffione 16174bf50f18SLuigi Rizzo #endif /* linux */ 16184bf50f18SLuigi Rizzo 16194f80b14cSVincenzo Maffione return error; 16204bf50f18SLuigi Rizzo } 16214bf50f18SLuigi Rizzo 16224bf50f18SLuigi Rizzo static int 1623ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd) 1624ce3ee1e7SLuigi Rizzo { 1625ce3ee1e7SLuigi Rizzo int i; 1626ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 1627ce3ee1e7SLuigi Rizzo return 0; 1628ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 1629ce3ee1e7SLuigi Rizzo nmd->nm_totalsize = 0; 1630ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1631ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]); 1632ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 1633ce3ee1e7SLuigi Rizzo goto error; 1634ce3ee1e7SLuigi Rizzo nmd->nm_totalsize += nmd->pools[i].memtotal; 1635ce3ee1e7SLuigi Rizzo } 16364f80b14cSVincenzo Maffione nmd->lasterr = netmap_mem_init_bitmaps(nmd); 16374f80b14cSVincenzo Maffione if (nmd->lasterr) 16384f80b14cSVincenzo Maffione goto error; 16394f80b14cSVincenzo Maffione 1640ce3ee1e7SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 1641ce3ee1e7SLuigi Rizzo 1642f0ea3689SLuigi Rizzo if (netmap_verbose) 1643253b2ec1SVincenzo Maffione nm_prinf("interfaces %zd KB, rings %zd KB, buffers %zd MB", 1644ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_IF_POOL].memtotal >> 10, 1645ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_RING_POOL].memtotal >> 10, 1646ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].memtotal >> 20); 1647ce3ee1e7SLuigi Rizzo 1648f0ea3689SLuigi Rizzo if (netmap_verbose) 1649b6e66be2SVincenzo Maffione nm_prinf("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree); 1650ce3ee1e7SLuigi Rizzo 1651ce3ee1e7SLuigi Rizzo 1652ce3ee1e7SLuigi Rizzo return 0; 1653ce3ee1e7SLuigi Rizzo error: 1654ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(nmd); 1655ce3ee1e7SLuigi Rizzo return nmd->lasterr; 1656ce3ee1e7SLuigi Rizzo } 1657ce3ee1e7SLuigi Rizzo 1658f0ea3689SLuigi Rizzo /* 1659f0ea3689SLuigi Rizzo * allocator for private memory 1660f0ea3689SLuigi Rizzo */ 16614f80b14cSVincenzo Maffione static void * 16624f80b14cSVincenzo Maffione _netmap_mem_private_new(size_t size, struct netmap_obj_params *p, 16634f80b14cSVincenzo Maffione struct netmap_mem_ops *ops, int *perr) 1664ce3ee1e7SLuigi Rizzo { 1665ce3ee1e7SLuigi Rizzo struct netmap_mem_d *d = NULL; 1666c3e9b4dbSLuiz Otavio O Souza int i, err = 0; 1667ce3ee1e7SLuigi Rizzo 16684f80b14cSVincenzo Maffione d = nm_os_malloc(size); 1669f0ea3689SLuigi Rizzo if (d == NULL) { 1670f0ea3689SLuigi Rizzo err = ENOMEM; 1671f0ea3689SLuigi Rizzo goto error; 1672f0ea3689SLuigi Rizzo } 1673ce3ee1e7SLuigi Rizzo 1674ce3ee1e7SLuigi Rizzo *d = nm_blueprint; 16754f80b14cSVincenzo Maffione d->ops = ops; 1676ce3ee1e7SLuigi Rizzo 1677f0ea3689SLuigi Rizzo err = nm_mem_assign_id(d); 1678f0ea3689SLuigi Rizzo if (err) 16794f80b14cSVincenzo Maffione goto error_free; 1680c3e9b4dbSLuiz Otavio O Souza snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id); 1681f0ea3689SLuigi Rizzo 1682c3e9b4dbSLuiz Otavio O Souza for (i = 0; i < NETMAP_POOLS_NR; i++) { 1683c3e9b4dbSLuiz Otavio O Souza snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ, 1684c3e9b4dbSLuiz Otavio O Souza nm_blueprint.pools[i].name, 1685c3e9b4dbSLuiz Otavio O Souza d->name); 1686c3e9b4dbSLuiz Otavio O Souza d->params[i].num = p[i].num; 1687c3e9b4dbSLuiz Otavio O Souza d->params[i].size = p[i].size; 1688c3e9b4dbSLuiz Otavio O Souza } 1689c3e9b4dbSLuiz Otavio O Souza 1690c3e9b4dbSLuiz Otavio O Souza NMA_LOCK_INIT(d); 1691c3e9b4dbSLuiz Otavio O Souza 1692c3e9b4dbSLuiz Otavio O Souza err = netmap_mem_config(d); 1693c3e9b4dbSLuiz Otavio O Souza if (err) 16944f80b14cSVincenzo Maffione goto error_rel_id; 1695c3e9b4dbSLuiz Otavio O Souza 1696c3e9b4dbSLuiz Otavio O Souza d->flags &= ~NETMAP_MEM_FINALIZED; 1697c3e9b4dbSLuiz Otavio O Souza 1698c3e9b4dbSLuiz Otavio O Souza return d; 1699c3e9b4dbSLuiz Otavio O Souza 17004f80b14cSVincenzo Maffione error_rel_id: 17014f80b14cSVincenzo Maffione NMA_LOCK_DESTROY(d); 17024f80b14cSVincenzo Maffione nm_mem_release_id(d); 17034f80b14cSVincenzo Maffione error_free: 17044f80b14cSVincenzo Maffione nm_os_free(d); 1705c3e9b4dbSLuiz Otavio O Souza error: 1706c3e9b4dbSLuiz Otavio O Souza if (perr) 1707c3e9b4dbSLuiz Otavio O Souza *perr = err; 1708c3e9b4dbSLuiz Otavio O Souza return NULL; 1709c3e9b4dbSLuiz Otavio O Souza } 1710c3e9b4dbSLuiz Otavio O Souza 1711c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * 1712c3e9b4dbSLuiz Otavio O Souza netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd, 1713c3e9b4dbSLuiz Otavio O Souza u_int extra_bufs, u_int npipes, int *perr) 1714c3e9b4dbSLuiz Otavio O Souza { 1715c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *d = NULL; 1716c3e9b4dbSLuiz Otavio O Souza struct netmap_obj_params p[NETMAP_POOLS_NR]; 17174f80b14cSVincenzo Maffione int i; 1718c3e9b4dbSLuiz Otavio O Souza u_int v, maxd; 1719f0ea3689SLuigi Rizzo /* account for the fake host rings */ 1720ce3ee1e7SLuigi Rizzo txr++; 1721ce3ee1e7SLuigi Rizzo rxr++; 1722ce3ee1e7SLuigi Rizzo 1723f0ea3689SLuigi Rizzo /* copy the min values */ 1724f0ea3689SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1725f0ea3689SLuigi Rizzo p[i] = netmap_min_priv_params[i]; 1726f0ea3689SLuigi Rizzo } 1727f0ea3689SLuigi Rizzo 1728f0ea3689SLuigi Rizzo /* possibly increase them to fit user request */ 1729f0ea3689SLuigi Rizzo v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr); 1730f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].size < v) 1731f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].size = v; 1732f0ea3689SLuigi Rizzo v = 2 + 4 * npipes; 1733f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].num < v) 1734f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].num = v; 1735f0ea3689SLuigi Rizzo maxd = (txd > rxd) ? txd : rxd; 1736f0ea3689SLuigi Rizzo v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd; 1737f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].size < v) 1738f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].size = v; 1739f0ea3689SLuigi Rizzo /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake) 1740f0ea3689SLuigi Rizzo * and two rx rings (again, 1 normal and 1 fake host) 1741f0ea3689SLuigi Rizzo */ 1742f0ea3689SLuigi Rizzo v = txr + rxr + 8 * npipes; 1743f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].num < v) 1744f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].num = v; 1745f0ea3689SLuigi Rizzo /* for each pipe we only need the buffers for the 4 "real" rings. 1746f0ea3689SLuigi Rizzo * On the other end, the pipe ring dimension may be different from 1747f0ea3689SLuigi Rizzo * the parent port ring dimension. As a compromise, we allocate twice the 1748f0ea3689SLuigi Rizzo * space actually needed if the pipe rings were the same size as the parent rings 1749f0ea3689SLuigi Rizzo */ 1750f0ea3689SLuigi Rizzo v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs; 1751f0ea3689SLuigi Rizzo /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */ 1752f0ea3689SLuigi Rizzo if (p[NETMAP_BUF_POOL].num < v) 1753f0ea3689SLuigi Rizzo p[NETMAP_BUF_POOL].num = v; 1754f0ea3689SLuigi Rizzo 1755f0ea3689SLuigi Rizzo if (netmap_verbose) 1756b6e66be2SVincenzo Maffione nm_prinf("req if %d*%d ring %d*%d buf %d*%d", 1757ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].num, 1758ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].size, 1759ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].num, 1760ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].size, 1761ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].num, 1762ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].size); 1763ce3ee1e7SLuigi Rizzo 17644f80b14cSVincenzo Maffione d = _netmap_mem_private_new(sizeof(*d), p, &netmap_mem_global_ops, perr); 1765ce3ee1e7SLuigi Rizzo 1766ce3ee1e7SLuigi Rizzo return d; 1767ce3ee1e7SLuigi Rizzo } 1768ce3ee1e7SLuigi Rizzo 17698241616dSLuigi Rizzo 17708241616dSLuigi Rizzo /* call with lock held */ 17718241616dSLuigi Rizzo static int 1772c3e9b4dbSLuiz Otavio O Souza netmap_mem2_config(struct netmap_mem_d *nmd) 17738241616dSLuigi Rizzo { 17748241616dSLuigi Rizzo int i; 17758241616dSLuigi Rizzo 1776c3e9b4dbSLuiz Otavio O Souza if (!netmap_mem_params_changed(nmd->params)) 17778241616dSLuigi Rizzo goto out; 17788241616dSLuigi Rizzo 177975f4f3edSVincenzo Maffione nm_prdis("reconfiguring"); 17808241616dSLuigi Rizzo 1781ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 17828241616dSLuigi Rizzo /* reset previous allocation */ 17838241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1784ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 17858241616dSLuigi Rizzo } 1786ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 17878241616dSLuigi Rizzo } 17888241616dSLuigi Rizzo 17898241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1790ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i], 1791c3e9b4dbSLuiz Otavio O Souza nmd->params[i].num, nmd->params[i].size); 1792ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 17938241616dSLuigi Rizzo goto out; 17948241616dSLuigi Rizzo } 17958241616dSLuigi Rizzo 17968241616dSLuigi Rizzo out: 17978241616dSLuigi Rizzo 1798ce3ee1e7SLuigi Rizzo return nmd->lasterr; 17998241616dSLuigi Rizzo } 18008241616dSLuigi Rizzo 18018241616dSLuigi Rizzo static int 1802c3e9b4dbSLuiz Otavio O Souza netmap_mem2_finalize(struct netmap_mem_d *nmd) 18038241616dSLuigi Rizzo { 18042ff91c17SVincenzo Maffione if (nmd->flags & NETMAP_MEM_FINALIZED) 18058241616dSLuigi Rizzo goto out; 18068241616dSLuigi Rizzo 1807ce3ee1e7SLuigi Rizzo if (netmap_mem_finalize_all(nmd)) 1808ce3ee1e7SLuigi Rizzo goto out; 18098241616dSLuigi Rizzo 1810ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 18118241616dSLuigi Rizzo 18128241616dSLuigi Rizzo out: 18132ff91c17SVincenzo Maffione return nmd->lasterr; 1814ccdc3305SLuigi Rizzo } 1815ccdc3305SLuigi Rizzo 1816847bf383SLuigi Rizzo static void 1817c3e9b4dbSLuiz Otavio O Souza netmap_mem2_delete(struct netmap_mem_d *nmd) 1818ccdc3305SLuigi Rizzo { 18198241616dSLuigi Rizzo int i; 18208241616dSLuigi Rizzo 18218241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1822c3e9b4dbSLuiz Otavio O Souza netmap_destroy_obj_allocator(&nmd->pools[i]); 18238241616dSLuigi Rizzo } 1824847bf383SLuigi Rizzo 1825c3e9b4dbSLuiz Otavio O Souza NMA_LOCK_DESTROY(nmd); 1826c3e9b4dbSLuiz Otavio O Souza if (nmd != &nm_mem) 1827c3e9b4dbSLuiz Otavio O Souza nm_os_free(nmd); 18288241616dSLuigi Rizzo } 18298241616dSLuigi Rizzo 18304f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM 18314f80b14cSVincenzo Maffione /* doubly linekd list of all existing external allocators */ 18324f80b14cSVincenzo Maffione static struct netmap_mem_ext *netmap_mem_ext_list = NULL; 18334f80b14cSVincenzo Maffione NM_MTX_T nm_mem_ext_list_lock; 18344f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */ 18354f80b14cSVincenzo Maffione 1836847bf383SLuigi Rizzo int 1837847bf383SLuigi Rizzo netmap_mem_init(void) 1838847bf383SLuigi Rizzo { 1839c3e9b4dbSLuiz Otavio O Souza NM_MTX_INIT(nm_mem_list_lock); 1840847bf383SLuigi Rizzo NMA_LOCK_INIT(&nm_mem); 1841847bf383SLuigi Rizzo netmap_mem_get(&nm_mem); 18424f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM 18434f80b14cSVincenzo Maffione NM_MTX_INIT(nm_mem_ext_list_lock); 18444f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */ 1845847bf383SLuigi Rizzo return (0); 1846847bf383SLuigi Rizzo } 1847847bf383SLuigi Rizzo 1848847bf383SLuigi Rizzo void 1849847bf383SLuigi Rizzo netmap_mem_fini(void) 1850847bf383SLuigi Rizzo { 1851847bf383SLuigi Rizzo netmap_mem_put(&nm_mem); 1852847bf383SLuigi Rizzo } 1853847bf383SLuigi Rizzo 18548241616dSLuigi Rizzo static void 18558241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na) 18568241616dSLuigi Rizzo { 1857847bf383SLuigi Rizzo enum txrx t; 1858847bf383SLuigi Rizzo 1859847bf383SLuigi Rizzo for_rx_tx(t) { 1860847bf383SLuigi Rizzo u_int i; 18612a7db7a6SVincenzo Maffione for (i = 0; i < netmap_all_rings(na, t); i++) { 18622ff91c17SVincenzo Maffione struct netmap_kring *kring = NMR(na, t)[i]; 1863847bf383SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1864847bf383SLuigi Rizzo 186537e3a6d3SLuigi Rizzo if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) { 1866b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1867b6e66be2SVincenzo Maffione nm_prinf("NOT deleting ring %s (ring %p, users %d neekring %d)", 18684f80b14cSVincenzo Maffione kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING); 1869f0ea3689SLuigi Rizzo continue; 187037e3a6d3SLuigi Rizzo } 1871b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1872b6e66be2SVincenzo Maffione nm_prinf("deleting ring %s", kring->name); 18732ff91c17SVincenzo Maffione if (!(kring->nr_kflags & NKR_FAKERING)) { 187475f4f3edSVincenzo Maffione nm_prdis("freeing bufs for %s", kring->name); 1875f0ea3689SLuigi Rizzo netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots); 18762ff91c17SVincenzo Maffione } else { 187775f4f3edSVincenzo Maffione nm_prdis("NOT freeing bufs for %s", kring->name); 18782ff91c17SVincenzo Maffione } 1879f0ea3689SLuigi Rizzo netmap_ring_free(na->nm_mem, ring); 1880f0ea3689SLuigi Rizzo kring->ring = NULL; 18818241616dSLuigi Rizzo } 1882ce3ee1e7SLuigi Rizzo } 1883ccdc3305SLuigi Rizzo } 1884ccdc3305SLuigi Rizzo 1885f9790aebSLuigi Rizzo /* call with NMA_LOCK held * 1886f9790aebSLuigi Rizzo * 1887f9790aebSLuigi Rizzo * Allocate netmap rings and buffers for this card 1888f9790aebSLuigi Rizzo * The rings are contiguous, but have variable size. 1889f0ea3689SLuigi Rizzo * The kring array must follow the layout described 1890f0ea3689SLuigi Rizzo * in netmap_krings_create(). 1891f9790aebSLuigi Rizzo */ 1892847bf383SLuigi Rizzo static int 1893847bf383SLuigi Rizzo netmap_mem2_rings_create(struct netmap_adapter *na) 1894f9790aebSLuigi Rizzo { 1895847bf383SLuigi Rizzo enum txrx t; 1896f9790aebSLuigi Rizzo 1897847bf383SLuigi Rizzo for_rx_tx(t) { 1898847bf383SLuigi Rizzo u_int i; 1899847bf383SLuigi Rizzo 19002a7db7a6SVincenzo Maffione for (i = 0; i < netmap_all_rings(na, t); i++) { 19012ff91c17SVincenzo Maffione struct netmap_kring *kring = NMR(na, t)[i]; 1902847bf383SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1903847bf383SLuigi Rizzo u_int len, ndesc; 1904847bf383SLuigi Rizzo 190537e3a6d3SLuigi Rizzo if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) { 190637e3a6d3SLuigi Rizzo /* uneeded, or already created by somebody else */ 1907b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1908b6e66be2SVincenzo Maffione nm_prinf("NOT creating ring %s (ring %p, users %d neekring %d)", 19094f80b14cSVincenzo Maffione kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING); 191037e3a6d3SLuigi Rizzo continue; 1911f0ea3689SLuigi Rizzo } 1912b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1913b6e66be2SVincenzo Maffione nm_prinf("creating %s", kring->name); 1914f9790aebSLuigi Rizzo ndesc = kring->nkr_num_slots; 1915f9790aebSLuigi Rizzo len = sizeof(struct netmap_ring) + 1916f9790aebSLuigi Rizzo ndesc * sizeof(struct netmap_slot); 1917f9790aebSLuigi Rizzo ring = netmap_ring_malloc(na->nm_mem, len); 1918f9790aebSLuigi Rizzo if (ring == NULL) { 1919b6e66be2SVincenzo Maffione nm_prerr("Cannot allocate %s_ring", nm_txrx2str(t)); 1920f9790aebSLuigi Rizzo goto cleanup; 1921f9790aebSLuigi Rizzo } 192275f4f3edSVincenzo Maffione nm_prdis("txring at %p", ring); 1923f9790aebSLuigi Rizzo kring->ring = ring; 1924f9790aebSLuigi Rizzo *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc; 192517885a7bSLuigi Rizzo *(int64_t *)(uintptr_t)&ring->buf_ofs = 1926f9790aebSLuigi Rizzo (na->nm_mem->pools[NETMAP_IF_POOL].memtotal + 1927f9790aebSLuigi Rizzo na->nm_mem->pools[NETMAP_RING_POOL].memtotal) - 1928f9790aebSLuigi Rizzo netmap_ring_offset(na->nm_mem, ring); 1929f9790aebSLuigi Rizzo 193017885a7bSLuigi Rizzo /* copy values from kring */ 193117885a7bSLuigi Rizzo ring->head = kring->rhead; 193217885a7bSLuigi Rizzo ring->cur = kring->rcur; 193317885a7bSLuigi Rizzo ring->tail = kring->rtail; 19344f80b14cSVincenzo Maffione *(uint32_t *)(uintptr_t)&ring->nr_buf_size = 19354bf50f18SLuigi Rizzo netmap_mem_bufsize(na->nm_mem); 193675f4f3edSVincenzo Maffione nm_prdis("%s h %d c %d t %d", kring->name, 1937f0ea3689SLuigi Rizzo ring->head, ring->cur, ring->tail); 193875f4f3edSVincenzo Maffione nm_prdis("initializing slots for %s_ring", nm_txrx2str(t)); 19392ff91c17SVincenzo Maffione if (!(kring->nr_kflags & NKR_FAKERING)) { 1940f0ea3689SLuigi Rizzo /* this is a real ring */ 1941b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1942b6e66be2SVincenzo Maffione nm_prinf("allocating buffers for %s", kring->name); 1943f9790aebSLuigi Rizzo if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) { 1944b6e66be2SVincenzo Maffione nm_prerr("Cannot allocate buffers for %s_ring", nm_txrx2str(t)); 1945f9790aebSLuigi Rizzo goto cleanup; 1946f9790aebSLuigi Rizzo } 1947f0ea3689SLuigi Rizzo } else { 1948847bf383SLuigi Rizzo /* this is a fake ring, set all indices to 0 */ 1949b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 1950b6e66be2SVincenzo Maffione nm_prinf("NOT allocating buffers for %s", kring->name); 1951f0ea3689SLuigi Rizzo netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0); 1952f0ea3689SLuigi Rizzo } 1953847bf383SLuigi Rizzo /* ring info */ 1954847bf383SLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id; 1955847bf383SLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->dir = kring->tx; 1956f0ea3689SLuigi Rizzo } 1957f9790aebSLuigi Rizzo } 1958f9790aebSLuigi Rizzo 1959f9790aebSLuigi Rizzo return 0; 1960f9790aebSLuigi Rizzo 1961f9790aebSLuigi Rizzo cleanup: 1962cfa866f6SMatt Macy /* we cannot actually cleanup here, since we don't own kring->users 1963cfa866f6SMatt Macy * and kring->nr_klags & NKR_NEEDRING. The caller must decrement 1964cfa866f6SMatt Macy * the first or zero-out the second, then call netmap_free_rings() 1965cfa866f6SMatt Macy * to do the cleanup 1966cfa866f6SMatt Macy */ 1967f9790aebSLuigi Rizzo 1968f9790aebSLuigi Rizzo return ENOMEM; 1969f9790aebSLuigi Rizzo } 1970f9790aebSLuigi Rizzo 1971847bf383SLuigi Rizzo static void 1972847bf383SLuigi Rizzo netmap_mem2_rings_delete(struct netmap_adapter *na) 1973f9790aebSLuigi Rizzo { 1974f9790aebSLuigi Rizzo /* last instance, release bufs and rings */ 1975f9790aebSLuigi Rizzo netmap_free_rings(na); 1976f9790aebSLuigi Rizzo } 1977ccdc3305SLuigi Rizzo 1978ccdc3305SLuigi Rizzo 19798241616dSLuigi Rizzo /* call with NMA_LOCK held */ 1980ae10d1afSLuigi Rizzo /* 1981ae10d1afSLuigi Rizzo * Allocate the per-fd structure netmap_if. 1982ce3ee1e7SLuigi Rizzo * 1983ce3ee1e7SLuigi Rizzo * We assume that the configuration stored in na 1984ce3ee1e7SLuigi Rizzo * (number of tx/rx rings and descs) does not change while 1985ce3ee1e7SLuigi Rizzo * the interface is in netmap mode. 1986ae10d1afSLuigi Rizzo */ 1987847bf383SLuigi Rizzo static struct netmap_if * 1988c3e9b4dbSLuiz Otavio O Souza netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv) 1989ccdc3305SLuigi Rizzo { 1990ccdc3305SLuigi Rizzo struct netmap_if *nifp; 1991ccdc3305SLuigi Rizzo ssize_t base; /* handy for relative offsets between rings and nifp */ 1992847bf383SLuigi Rizzo u_int i, len, n[NR_TXRX], ntot; 1993847bf383SLuigi Rizzo enum txrx t; 1994ccdc3305SLuigi Rizzo 1995847bf383SLuigi Rizzo ntot = 0; 1996847bf383SLuigi Rizzo for_rx_tx(t) { 1997f0ea3689SLuigi Rizzo /* account for the (eventually fake) host rings */ 19982a7db7a6SVincenzo Maffione n[t] = netmap_all_rings(na, t); 1999847bf383SLuigi Rizzo ntot += n[t]; 2000847bf383SLuigi Rizzo } 2001ccdc3305SLuigi Rizzo /* 2002ccdc3305SLuigi Rizzo * the descriptor is followed inline by an array of offsets 2003ccdc3305SLuigi Rizzo * to the tx and rx rings in the shared memory region. 2004ccdc3305SLuigi Rizzo */ 2005ce3ee1e7SLuigi Rizzo 2006847bf383SLuigi Rizzo len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t)); 2007ce3ee1e7SLuigi Rizzo nifp = netmap_if_malloc(na->nm_mem, len); 2008ccdc3305SLuigi Rizzo if (nifp == NULL) { 2009ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 2010ccdc3305SLuigi Rizzo return NULL; 2011ccdc3305SLuigi Rizzo } 2012ccdc3305SLuigi Rizzo 2013ccdc3305SLuigi Rizzo /* initialize base fields -- override const */ 2014ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings; 2015ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings; 2016d12354a5SVincenzo Maffione *(u_int *)(uintptr_t)&nifp->ni_host_tx_rings = 2017d12354a5SVincenzo Maffione (na->num_host_tx_rings ? na->num_host_tx_rings : 1); 2018d12354a5SVincenzo Maffione *(u_int *)(uintptr_t)&nifp->ni_host_rx_rings = 2019d12354a5SVincenzo Maffione (na->num_host_rx_rings ? na->num_host_rx_rings : 1); 2020b6e66be2SVincenzo Maffione strlcpy(nifp->ni_name, na->name, sizeof(nifp->ni_name)); 2021ccdc3305SLuigi Rizzo 2022ccdc3305SLuigi Rizzo /* 2023ccdc3305SLuigi Rizzo * fill the slots for the rx and tx rings. They contain the offset 2024ccdc3305SLuigi Rizzo * between the ring and nifp, so the information is usable in 2025ccdc3305SLuigi Rizzo * userspace to reach the ring from the nifp. 2026ccdc3305SLuigi Rizzo */ 2027ce3ee1e7SLuigi Rizzo base = netmap_if_offset(na->nm_mem, nifp); 2028847bf383SLuigi Rizzo for (i = 0; i < n[NR_TX]; i++) { 2029c3e9b4dbSLuiz Otavio O Souza /* XXX instead of ofs == 0 maybe use the offset of an error 2030c3e9b4dbSLuiz Otavio O Souza * ring, like we do for buffers? */ 2031c3e9b4dbSLuiz Otavio O Souza ssize_t ofs = 0; 2032c3e9b4dbSLuiz Otavio O Souza 20332ff91c17SVincenzo Maffione if (na->tx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_TX] 2034c3e9b4dbSLuiz Otavio O Souza && i < priv->np_qlast[NR_TX]) { 2035c3e9b4dbSLuiz Otavio O Souza ofs = netmap_ring_offset(na->nm_mem, 20362ff91c17SVincenzo Maffione na->tx_rings[i]->ring) - base; 203737e3a6d3SLuigi Rizzo } 2038c3e9b4dbSLuiz Otavio O Souza *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs; 2039ccdc3305SLuigi Rizzo } 2040847bf383SLuigi Rizzo for (i = 0; i < n[NR_RX]; i++) { 2041c3e9b4dbSLuiz Otavio O Souza /* XXX instead of ofs == 0 maybe use the offset of an error 2042c3e9b4dbSLuiz Otavio O Souza * ring, like we do for buffers? */ 2043c3e9b4dbSLuiz Otavio O Souza ssize_t ofs = 0; 2044c3e9b4dbSLuiz Otavio O Souza 20452ff91c17SVincenzo Maffione if (na->rx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_RX] 2046c3e9b4dbSLuiz Otavio O Souza && i < priv->np_qlast[NR_RX]) { 2047c3e9b4dbSLuiz Otavio O Souza ofs = netmap_ring_offset(na->nm_mem, 20482ff91c17SVincenzo Maffione na->rx_rings[i]->ring) - base; 204937e3a6d3SLuigi Rizzo } 2050c3e9b4dbSLuiz Otavio O Souza *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs; 2051ccdc3305SLuigi Rizzo } 2052ce3ee1e7SLuigi Rizzo 2053ccdc3305SLuigi Rizzo return (nifp); 2054ccdc3305SLuigi Rizzo } 2055ccdc3305SLuigi Rizzo 2056847bf383SLuigi Rizzo static void 2057847bf383SLuigi Rizzo netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 2058ccdc3305SLuigi Rizzo { 2059ce3ee1e7SLuigi Rizzo if (nifp == NULL) 2060ce3ee1e7SLuigi Rizzo /* nothing to do */ 2061ce3ee1e7SLuigi Rizzo return; 2062f0ea3689SLuigi Rizzo if (nifp->ni_bufs_head) 2063f0ea3689SLuigi Rizzo netmap_extra_free(na, nifp->ni_bufs_head); 2064ce3ee1e7SLuigi Rizzo netmap_if_free(na->nm_mem, nifp); 2065ce3ee1e7SLuigi Rizzo } 2066ce3ee1e7SLuigi Rizzo 2067ce3ee1e7SLuigi Rizzo static void 2068c3e9b4dbSLuiz Otavio O Souza netmap_mem2_deref(struct netmap_mem_d *nmd) 2069ce3ee1e7SLuigi Rizzo { 2070ce3ee1e7SLuigi Rizzo 2071b6e66be2SVincenzo Maffione if (netmap_debug & NM_DEBUG_MEM) 2072b6e66be2SVincenzo Maffione nm_prinf("active = %d", nmd->active); 2073ce3ee1e7SLuigi Rizzo 2074ce3ee1e7SLuigi Rizzo } 2075ce3ee1e7SLuigi Rizzo 2076847bf383SLuigi Rizzo struct netmap_mem_ops netmap_mem_global_ops = { 2077847bf383SLuigi Rizzo .nmd_get_lut = netmap_mem2_get_lut, 2078847bf383SLuigi Rizzo .nmd_get_info = netmap_mem2_get_info, 2079847bf383SLuigi Rizzo .nmd_ofstophys = netmap_mem2_ofstophys, 2080c3e9b4dbSLuiz Otavio O Souza .nmd_config = netmap_mem2_config, 2081c3e9b4dbSLuiz Otavio O Souza .nmd_finalize = netmap_mem2_finalize, 2082c3e9b4dbSLuiz Otavio O Souza .nmd_deref = netmap_mem2_deref, 2083c3e9b4dbSLuiz Otavio O Souza .nmd_delete = netmap_mem2_delete, 2084847bf383SLuigi Rizzo .nmd_if_offset = netmap_mem2_if_offset, 2085847bf383SLuigi Rizzo .nmd_if_new = netmap_mem2_if_new, 2086847bf383SLuigi Rizzo .nmd_if_delete = netmap_mem2_if_delete, 2087847bf383SLuigi Rizzo .nmd_rings_create = netmap_mem2_rings_create, 2088847bf383SLuigi Rizzo .nmd_rings_delete = netmap_mem2_rings_delete 2089847bf383SLuigi Rizzo }; 209037e3a6d3SLuigi Rizzo 2091844a6f0cSLuigi Rizzo int 20922ff91c17SVincenzo Maffione netmap_mem_pools_info_get(struct nmreq_pools_info *req, 20932ff91c17SVincenzo Maffione struct netmap_mem_d *nmd) 2094844a6f0cSLuigi Rizzo { 2095844a6f0cSLuigi Rizzo int ret; 2096844a6f0cSLuigi Rizzo 20972ff91c17SVincenzo Maffione ret = netmap_mem_get_info(nmd, &req->nr_memsize, NULL, 20982ff91c17SVincenzo Maffione &req->nr_mem_id); 2099844a6f0cSLuigi Rizzo if (ret) { 2100844a6f0cSLuigi Rizzo return ret; 2101844a6f0cSLuigi Rizzo } 2102844a6f0cSLuigi Rizzo 2103c3e9b4dbSLuiz Otavio O Souza NMA_LOCK(nmd); 21042ff91c17SVincenzo Maffione req->nr_if_pool_offset = 0; 21052ff91c17SVincenzo Maffione req->nr_if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal; 21062ff91c17SVincenzo Maffione req->nr_if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize; 2107844a6f0cSLuigi Rizzo 21082ff91c17SVincenzo Maffione req->nr_ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal; 21092ff91c17SVincenzo Maffione req->nr_ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal; 21102ff91c17SVincenzo Maffione req->nr_ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize; 2111844a6f0cSLuigi Rizzo 21122ff91c17SVincenzo Maffione req->nr_buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal + 2113844a6f0cSLuigi Rizzo nmd->pools[NETMAP_RING_POOL].memtotal; 21142ff91c17SVincenzo Maffione req->nr_buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal; 21152ff91c17SVincenzo Maffione req->nr_buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize; 2116c3e9b4dbSLuiz Otavio O Souza NMA_UNLOCK(nmd); 2117844a6f0cSLuigi Rizzo 2118844a6f0cSLuigi Rizzo return 0; 2119844a6f0cSLuigi Rizzo } 2120844a6f0cSLuigi Rizzo 21214f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM 21224f80b14cSVincenzo Maffione struct netmap_mem_ext { 21234f80b14cSVincenzo Maffione struct netmap_mem_d up; 21244f80b14cSVincenzo Maffione 21252ff91c17SVincenzo Maffione struct nm_os_extmem *os; 21264f80b14cSVincenzo Maffione struct netmap_mem_ext *next, *prev; 21274f80b14cSVincenzo Maffione }; 21284f80b14cSVincenzo Maffione 21294f80b14cSVincenzo Maffione /* call with nm_mem_list_lock held */ 21304f80b14cSVincenzo Maffione static void 21314f80b14cSVincenzo Maffione netmap_mem_ext_register(struct netmap_mem_ext *e) 21324f80b14cSVincenzo Maffione { 21334f80b14cSVincenzo Maffione NM_MTX_LOCK(nm_mem_ext_list_lock); 21344f80b14cSVincenzo Maffione if (netmap_mem_ext_list) 21354f80b14cSVincenzo Maffione netmap_mem_ext_list->prev = e; 21364f80b14cSVincenzo Maffione e->next = netmap_mem_ext_list; 21374f80b14cSVincenzo Maffione netmap_mem_ext_list = e; 21384f80b14cSVincenzo Maffione e->prev = NULL; 21394f80b14cSVincenzo Maffione NM_MTX_UNLOCK(nm_mem_ext_list_lock); 21404f80b14cSVincenzo Maffione } 21414f80b14cSVincenzo Maffione 21424f80b14cSVincenzo Maffione /* call with nm_mem_list_lock held */ 21434f80b14cSVincenzo Maffione static void 21444f80b14cSVincenzo Maffione netmap_mem_ext_unregister(struct netmap_mem_ext *e) 21454f80b14cSVincenzo Maffione { 21464f80b14cSVincenzo Maffione if (e->prev) 21474f80b14cSVincenzo Maffione e->prev->next = e->next; 21484f80b14cSVincenzo Maffione else 21494f80b14cSVincenzo Maffione netmap_mem_ext_list = e->next; 21504f80b14cSVincenzo Maffione if (e->next) 21514f80b14cSVincenzo Maffione e->next->prev = e->prev; 21524f80b14cSVincenzo Maffione e->prev = e->next = NULL; 21534f80b14cSVincenzo Maffione } 21544f80b14cSVincenzo Maffione 21554f80b14cSVincenzo Maffione static struct netmap_mem_ext * 21562ff91c17SVincenzo Maffione netmap_mem_ext_search(struct nm_os_extmem *os) 21574f80b14cSVincenzo Maffione { 21584f80b14cSVincenzo Maffione struct netmap_mem_ext *e; 21594f80b14cSVincenzo Maffione 21604f80b14cSVincenzo Maffione NM_MTX_LOCK(nm_mem_ext_list_lock); 21614f80b14cSVincenzo Maffione for (e = netmap_mem_ext_list; e; e = e->next) { 21622ff91c17SVincenzo Maffione if (nm_os_extmem_isequal(e->os, os)) { 21634f80b14cSVincenzo Maffione netmap_mem_get(&e->up); 21644f80b14cSVincenzo Maffione break; 21654f80b14cSVincenzo Maffione } 21664f80b14cSVincenzo Maffione } 21674f80b14cSVincenzo Maffione NM_MTX_UNLOCK(nm_mem_ext_list_lock); 21684f80b14cSVincenzo Maffione return e; 21694f80b14cSVincenzo Maffione } 21704f80b14cSVincenzo Maffione 21714f80b14cSVincenzo Maffione 21724f80b14cSVincenzo Maffione static void 21734f80b14cSVincenzo Maffione netmap_mem_ext_delete(struct netmap_mem_d *d) 21744f80b14cSVincenzo Maffione { 21754f80b14cSVincenzo Maffione int i; 21764f80b14cSVincenzo Maffione struct netmap_mem_ext *e = 21774f80b14cSVincenzo Maffione (struct netmap_mem_ext *)d; 21784f80b14cSVincenzo Maffione 21794f80b14cSVincenzo Maffione netmap_mem_ext_unregister(e); 21804f80b14cSVincenzo Maffione 21814f80b14cSVincenzo Maffione for (i = 0; i < NETMAP_POOLS_NR; i++) { 21824f80b14cSVincenzo Maffione struct netmap_obj_pool *p = &d->pools[i]; 21834f80b14cSVincenzo Maffione 21844f80b14cSVincenzo Maffione if (p->lut) { 21854f80b14cSVincenzo Maffione nm_free_lut(p->lut, p->objtotal); 21864f80b14cSVincenzo Maffione p->lut = NULL; 21874f80b14cSVincenzo Maffione } 21884f80b14cSVincenzo Maffione } 21892ff91c17SVincenzo Maffione if (e->os) 21902ff91c17SVincenzo Maffione nm_os_extmem_delete(e->os); 21914f80b14cSVincenzo Maffione netmap_mem2_delete(d); 21924f80b14cSVincenzo Maffione } 21934f80b14cSVincenzo Maffione 21944f80b14cSVincenzo Maffione static int 21954f80b14cSVincenzo Maffione netmap_mem_ext_config(struct netmap_mem_d *nmd) 21964f80b14cSVincenzo Maffione { 21974f80b14cSVincenzo Maffione return 0; 21984f80b14cSVincenzo Maffione } 21994f80b14cSVincenzo Maffione 22004f80b14cSVincenzo Maffione struct netmap_mem_ops netmap_mem_ext_ops = { 22014f80b14cSVincenzo Maffione .nmd_get_lut = netmap_mem2_get_lut, 22024f80b14cSVincenzo Maffione .nmd_get_info = netmap_mem2_get_info, 22034f80b14cSVincenzo Maffione .nmd_ofstophys = netmap_mem2_ofstophys, 22044f80b14cSVincenzo Maffione .nmd_config = netmap_mem_ext_config, 22054f80b14cSVincenzo Maffione .nmd_finalize = netmap_mem2_finalize, 22064f80b14cSVincenzo Maffione .nmd_deref = netmap_mem2_deref, 22074f80b14cSVincenzo Maffione .nmd_delete = netmap_mem_ext_delete, 22084f80b14cSVincenzo Maffione .nmd_if_offset = netmap_mem2_if_offset, 22094f80b14cSVincenzo Maffione .nmd_if_new = netmap_mem2_if_new, 22104f80b14cSVincenzo Maffione .nmd_if_delete = netmap_mem2_if_delete, 22114f80b14cSVincenzo Maffione .nmd_rings_create = netmap_mem2_rings_create, 22124f80b14cSVincenzo Maffione .nmd_rings_delete = netmap_mem2_rings_delete 22134f80b14cSVincenzo Maffione }; 22144f80b14cSVincenzo Maffione 22154f80b14cSVincenzo Maffione struct netmap_mem_d * 22162ff91c17SVincenzo Maffione netmap_mem_ext_create(uint64_t usrptr, struct nmreq_pools_info *pi, int *perror) 22174f80b14cSVincenzo Maffione { 22184f80b14cSVincenzo Maffione int error = 0; 22192ff91c17SVincenzo Maffione int i, j; 22204f80b14cSVincenzo Maffione struct netmap_mem_ext *nme; 22214f80b14cSVincenzo Maffione char *clust; 22224f80b14cSVincenzo Maffione size_t off; 22232ff91c17SVincenzo Maffione struct nm_os_extmem *os = NULL; 22242ff91c17SVincenzo Maffione int nr_pages; 22254f80b14cSVincenzo Maffione 22264f80b14cSVincenzo Maffione // XXX sanity checks 22272ff91c17SVincenzo Maffione if (pi->nr_if_pool_objtotal == 0) 22282ff91c17SVincenzo Maffione pi->nr_if_pool_objtotal = netmap_min_priv_params[NETMAP_IF_POOL].num; 22292ff91c17SVincenzo Maffione if (pi->nr_if_pool_objsize == 0) 22302ff91c17SVincenzo Maffione pi->nr_if_pool_objsize = netmap_min_priv_params[NETMAP_IF_POOL].size; 22312ff91c17SVincenzo Maffione if (pi->nr_ring_pool_objtotal == 0) 22322ff91c17SVincenzo Maffione pi->nr_ring_pool_objtotal = netmap_min_priv_params[NETMAP_RING_POOL].num; 22332ff91c17SVincenzo Maffione if (pi->nr_ring_pool_objsize == 0) 22342ff91c17SVincenzo Maffione pi->nr_ring_pool_objsize = netmap_min_priv_params[NETMAP_RING_POOL].size; 22352ff91c17SVincenzo Maffione if (pi->nr_buf_pool_objtotal == 0) 22362ff91c17SVincenzo Maffione pi->nr_buf_pool_objtotal = netmap_min_priv_params[NETMAP_BUF_POOL].num; 22372ff91c17SVincenzo Maffione if (pi->nr_buf_pool_objsize == 0) 22382ff91c17SVincenzo Maffione pi->nr_buf_pool_objsize = netmap_min_priv_params[NETMAP_BUF_POOL].size; 2239b6e66be2SVincenzo Maffione if (netmap_verbose & NM_DEBUG_MEM) 2240b6e66be2SVincenzo Maffione nm_prinf("if %d %d ring %d %d buf %d %d", 22412ff91c17SVincenzo Maffione pi->nr_if_pool_objtotal, pi->nr_if_pool_objsize, 22422ff91c17SVincenzo Maffione pi->nr_ring_pool_objtotal, pi->nr_ring_pool_objsize, 22432ff91c17SVincenzo Maffione pi->nr_buf_pool_objtotal, pi->nr_buf_pool_objsize); 22444f80b14cSVincenzo Maffione 22452ff91c17SVincenzo Maffione os = nm_os_extmem_create(usrptr, pi, &error); 22462ff91c17SVincenzo Maffione if (os == NULL) { 2247b6e66be2SVincenzo Maffione nm_prerr("os extmem creation failed"); 22484f80b14cSVincenzo Maffione goto out; 22494f80b14cSVincenzo Maffione } 22504f80b14cSVincenzo Maffione 22512ff91c17SVincenzo Maffione nme = netmap_mem_ext_search(os); 22524f80b14cSVincenzo Maffione if (nme) { 22532ff91c17SVincenzo Maffione nm_os_extmem_delete(os); 22544f80b14cSVincenzo Maffione return &nme->up; 22554f80b14cSVincenzo Maffione } 2256b6e66be2SVincenzo Maffione if (netmap_verbose & NM_DEBUG_MEM) 2257b6e66be2SVincenzo Maffione nm_prinf("not found, creating new"); 22584f80b14cSVincenzo Maffione 22594f80b14cSVincenzo Maffione nme = _netmap_mem_private_new(sizeof(*nme), 22604f80b14cSVincenzo Maffione (struct netmap_obj_params[]){ 22612ff91c17SVincenzo Maffione { pi->nr_if_pool_objsize, pi->nr_if_pool_objtotal }, 22622ff91c17SVincenzo Maffione { pi->nr_ring_pool_objsize, pi->nr_ring_pool_objtotal }, 22632ff91c17SVincenzo Maffione { pi->nr_buf_pool_objsize, pi->nr_buf_pool_objtotal }}, 22644f80b14cSVincenzo Maffione &netmap_mem_ext_ops, 22654f80b14cSVincenzo Maffione &error); 22664f80b14cSVincenzo Maffione if (nme == NULL) 22674f80b14cSVincenzo Maffione goto out_unmap; 22684f80b14cSVincenzo Maffione 22692ff91c17SVincenzo Maffione nr_pages = nm_os_extmem_nr_pages(os); 22702ff91c17SVincenzo Maffione 22714f80b14cSVincenzo Maffione /* from now on pages will be released by nme destructor; 22724f80b14cSVincenzo Maffione * we let res = 0 to prevent release in out_unmap below 22734f80b14cSVincenzo Maffione */ 22742ff91c17SVincenzo Maffione nme->os = os; 22752ff91c17SVincenzo Maffione os = NULL; /* pass ownership */ 22764f80b14cSVincenzo Maffione 22772ff91c17SVincenzo Maffione clust = nm_os_extmem_nextpage(nme->os); 22784f80b14cSVincenzo Maffione off = 0; 22794f80b14cSVincenzo Maffione for (i = 0; i < NETMAP_POOLS_NR; i++) { 22804f80b14cSVincenzo Maffione struct netmap_obj_pool *p = &nme->up.pools[i]; 22814f80b14cSVincenzo Maffione struct netmap_obj_params *o = &nme->up.params[i]; 22824f80b14cSVincenzo Maffione 22834f80b14cSVincenzo Maffione p->_objsize = o->size; 22844f80b14cSVincenzo Maffione p->_clustsize = o->size; 22854f80b14cSVincenzo Maffione p->_clustentries = 1; 22864f80b14cSVincenzo Maffione 22874f80b14cSVincenzo Maffione p->lut = nm_alloc_lut(o->num); 22884f80b14cSVincenzo Maffione if (p->lut == NULL) { 22894f80b14cSVincenzo Maffione error = ENOMEM; 22904f80b14cSVincenzo Maffione goto out_delete; 22914f80b14cSVincenzo Maffione } 22924f80b14cSVincenzo Maffione 22934f80b14cSVincenzo Maffione p->bitmap_slots = (o->num + sizeof(uint32_t) - 1) / sizeof(uint32_t); 22944f80b14cSVincenzo Maffione p->invalid_bitmap = nm_os_malloc(sizeof(uint32_t) * p->bitmap_slots); 22954f80b14cSVincenzo Maffione if (p->invalid_bitmap == NULL) { 22964f80b14cSVincenzo Maffione error = ENOMEM; 22974f80b14cSVincenzo Maffione goto out_delete; 22984f80b14cSVincenzo Maffione } 22994f80b14cSVincenzo Maffione 23004f80b14cSVincenzo Maffione if (nr_pages == 0) { 23014f80b14cSVincenzo Maffione p->objtotal = 0; 23024f80b14cSVincenzo Maffione p->memtotal = 0; 23034f80b14cSVincenzo Maffione p->objfree = 0; 23044f80b14cSVincenzo Maffione continue; 23054f80b14cSVincenzo Maffione } 23064f80b14cSVincenzo Maffione 23074f80b14cSVincenzo Maffione for (j = 0; j < o->num && nr_pages > 0; j++) { 23084f80b14cSVincenzo Maffione size_t noff; 23094f80b14cSVincenzo Maffione 23104f80b14cSVincenzo Maffione p->lut[j].vaddr = clust + off; 23112ff91c17SVincenzo Maffione #if !defined(linux) && !defined(_WIN32) 23122ff91c17SVincenzo Maffione p->lut[j].paddr = vtophys(p->lut[j].vaddr); 23132ff91c17SVincenzo Maffione #endif 231475f4f3edSVincenzo Maffione nm_prdis("%s %d at %p", p->name, j, p->lut[j].vaddr); 23154f80b14cSVincenzo Maffione noff = off + p->_objsize; 23164f80b14cSVincenzo Maffione if (noff < PAGE_SIZE) { 23174f80b14cSVincenzo Maffione off = noff; 23184f80b14cSVincenzo Maffione continue; 23194f80b14cSVincenzo Maffione } 232075f4f3edSVincenzo Maffione nm_prdis("too big, recomputing offset..."); 23214f80b14cSVincenzo Maffione while (noff >= PAGE_SIZE) { 23222ff91c17SVincenzo Maffione char *old_clust = clust; 23232ff91c17SVincenzo Maffione noff -= PAGE_SIZE; 23242ff91c17SVincenzo Maffione clust = nm_os_extmem_nextpage(nme->os); 23254f80b14cSVincenzo Maffione nr_pages--; 232675f4f3edSVincenzo Maffione nm_prdis("noff %zu page %p nr_pages %d", noff, 23274f80b14cSVincenzo Maffione page_to_virt(*pages), nr_pages); 23284f80b14cSVincenzo Maffione if (noff > 0 && !nm_isset(p->invalid_bitmap, j) && 23292ff91c17SVincenzo Maffione (nr_pages == 0 || 23302ff91c17SVincenzo Maffione old_clust + PAGE_SIZE != clust)) 23314f80b14cSVincenzo Maffione { 23324f80b14cSVincenzo Maffione /* out of space or non contiguous, 23334f80b14cSVincenzo Maffione * drop this object 23344f80b14cSVincenzo Maffione * */ 23354f80b14cSVincenzo Maffione p->invalid_bitmap[ (j>>5) ] |= 1U << (j & 31U); 233675f4f3edSVincenzo Maffione nm_prdis("non contiguous at off %zu, drop", noff); 23374f80b14cSVincenzo Maffione } 23384f80b14cSVincenzo Maffione if (nr_pages == 0) 23394f80b14cSVincenzo Maffione break; 23404f80b14cSVincenzo Maffione } 23414f80b14cSVincenzo Maffione off = noff; 23424f80b14cSVincenzo Maffione } 23434f80b14cSVincenzo Maffione p->objtotal = j; 23444f80b14cSVincenzo Maffione p->numclusters = p->objtotal; 2345253b2ec1SVincenzo Maffione p->memtotal = j * (size_t)p->_objsize; 2346253b2ec1SVincenzo Maffione nm_prdis("%d memtotal %zu", j, p->memtotal); 23474f80b14cSVincenzo Maffione } 23484f80b14cSVincenzo Maffione 23494f80b14cSVincenzo Maffione netmap_mem_ext_register(nme); 23504f80b14cSVincenzo Maffione 23514f80b14cSVincenzo Maffione return &nme->up; 23524f80b14cSVincenzo Maffione 23534f80b14cSVincenzo Maffione out_delete: 23544f80b14cSVincenzo Maffione netmap_mem_put(&nme->up); 23554f80b14cSVincenzo Maffione out_unmap: 23562ff91c17SVincenzo Maffione if (os) 23572ff91c17SVincenzo Maffione nm_os_extmem_delete(os); 23584f80b14cSVincenzo Maffione out: 23594f80b14cSVincenzo Maffione if (perror) 23604f80b14cSVincenzo Maffione *perror = error; 23614f80b14cSVincenzo Maffione return NULL; 23624f80b14cSVincenzo Maffione 23634f80b14cSVincenzo Maffione } 23644f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */ 23654f80b14cSVincenzo Maffione 23664f80b14cSVincenzo Maffione 2367b6e66be2SVincenzo Maffione #ifdef WITH_PTNETMAP 236837e3a6d3SLuigi Rizzo struct mem_pt_if { 236937e3a6d3SLuigi Rizzo struct mem_pt_if *next; 237037e3a6d3SLuigi Rizzo struct ifnet *ifp; 237137e3a6d3SLuigi Rizzo unsigned int nifp_offset; 237237e3a6d3SLuigi Rizzo }; 237337e3a6d3SLuigi Rizzo 237437e3a6d3SLuigi Rizzo /* Netmap allocator for ptnetmap guests. */ 237537e3a6d3SLuigi Rizzo struct netmap_mem_ptg { 237637e3a6d3SLuigi Rizzo struct netmap_mem_d up; 237737e3a6d3SLuigi Rizzo 237837e3a6d3SLuigi Rizzo vm_paddr_t nm_paddr; /* physical address in the guest */ 237937e3a6d3SLuigi Rizzo void *nm_addr; /* virtual address in the guest */ 238037e3a6d3SLuigi Rizzo struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */ 2381844a6f0cSLuigi Rizzo nm_memid_t host_mem_id; /* allocator identifier in the host */ 2382844a6f0cSLuigi Rizzo struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */ 238337e3a6d3SLuigi Rizzo struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */ 238437e3a6d3SLuigi Rizzo }; 238537e3a6d3SLuigi Rizzo 238637e3a6d3SLuigi Rizzo /* Link a passthrough interface to a passthrough netmap allocator. */ 238737e3a6d3SLuigi Rizzo static int 238837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp, 2389844a6f0cSLuigi Rizzo unsigned int nifp_offset) 239037e3a6d3SLuigi Rizzo { 239137e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 2392c3e9b4dbSLuiz Otavio O Souza struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif)); 239337e3a6d3SLuigi Rizzo 239437e3a6d3SLuigi Rizzo if (!ptif) { 239537e3a6d3SLuigi Rizzo return ENOMEM; 239637e3a6d3SLuigi Rizzo } 239737e3a6d3SLuigi Rizzo 239837e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 239937e3a6d3SLuigi Rizzo 240037e3a6d3SLuigi Rizzo ptif->ifp = ifp; 240137e3a6d3SLuigi Rizzo ptif->nifp_offset = nifp_offset; 240237e3a6d3SLuigi Rizzo 240337e3a6d3SLuigi Rizzo if (ptnmd->pt_ifs) { 240437e3a6d3SLuigi Rizzo ptif->next = ptnmd->pt_ifs; 240537e3a6d3SLuigi Rizzo } 240637e3a6d3SLuigi Rizzo ptnmd->pt_ifs = ptif; 240737e3a6d3SLuigi Rizzo 240837e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 240937e3a6d3SLuigi Rizzo 2410b6e66be2SVincenzo Maffione nm_prinf("ifp=%s,nifp_offset=%u", 2411b6e66be2SVincenzo Maffione ptif->ifp->if_xname, ptif->nifp_offset); 241237e3a6d3SLuigi Rizzo 241337e3a6d3SLuigi Rizzo return 0; 241437e3a6d3SLuigi Rizzo } 241537e3a6d3SLuigi Rizzo 241637e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(nmd) held. */ 241737e3a6d3SLuigi Rizzo static struct mem_pt_if * 241837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp) 241937e3a6d3SLuigi Rizzo { 242037e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 242137e3a6d3SLuigi Rizzo struct mem_pt_if *curr; 242237e3a6d3SLuigi Rizzo 242337e3a6d3SLuigi Rizzo for (curr = ptnmd->pt_ifs; curr; curr = curr->next) { 242437e3a6d3SLuigi Rizzo if (curr->ifp == ifp) { 242537e3a6d3SLuigi Rizzo return curr; 242637e3a6d3SLuigi Rizzo } 242737e3a6d3SLuigi Rizzo } 242837e3a6d3SLuigi Rizzo 242937e3a6d3SLuigi Rizzo return NULL; 243037e3a6d3SLuigi Rizzo } 243137e3a6d3SLuigi Rizzo 243237e3a6d3SLuigi Rizzo /* Unlink a passthrough interface from a passthrough netmap allocator. */ 243337e3a6d3SLuigi Rizzo int 243437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp) 243537e3a6d3SLuigi Rizzo { 243637e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 243737e3a6d3SLuigi Rizzo struct mem_pt_if *prev = NULL; 243837e3a6d3SLuigi Rizzo struct mem_pt_if *curr; 243937e3a6d3SLuigi Rizzo int ret = -1; 244037e3a6d3SLuigi Rizzo 244137e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 244237e3a6d3SLuigi Rizzo 244337e3a6d3SLuigi Rizzo for (curr = ptnmd->pt_ifs; curr; curr = curr->next) { 244437e3a6d3SLuigi Rizzo if (curr->ifp == ifp) { 244537e3a6d3SLuigi Rizzo if (prev) { 244637e3a6d3SLuigi Rizzo prev->next = curr->next; 244737e3a6d3SLuigi Rizzo } else { 244837e3a6d3SLuigi Rizzo ptnmd->pt_ifs = curr->next; 244937e3a6d3SLuigi Rizzo } 2450*760fa2abSVincenzo Maffione nm_prinf("removed (ifp=%s,nifp_offset=%u)", 2451*760fa2abSVincenzo Maffione curr->ifp->if_xname, curr->nifp_offset); 2452c3e9b4dbSLuiz Otavio O Souza nm_os_free(curr); 245337e3a6d3SLuigi Rizzo ret = 0; 245437e3a6d3SLuigi Rizzo break; 245537e3a6d3SLuigi Rizzo } 245637e3a6d3SLuigi Rizzo prev = curr; 245737e3a6d3SLuigi Rizzo } 245837e3a6d3SLuigi Rizzo 245937e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 246037e3a6d3SLuigi Rizzo 246137e3a6d3SLuigi Rizzo return ret; 246237e3a6d3SLuigi Rizzo } 246337e3a6d3SLuigi Rizzo 246437e3a6d3SLuigi Rizzo static int 246537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 246637e3a6d3SLuigi Rizzo { 246737e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 246837e3a6d3SLuigi Rizzo 246937e3a6d3SLuigi Rizzo if (!(nmd->flags & NETMAP_MEM_FINALIZED)) { 247037e3a6d3SLuigi Rizzo return EINVAL; 247137e3a6d3SLuigi Rizzo } 247237e3a6d3SLuigi Rizzo 247337e3a6d3SLuigi Rizzo *lut = ptnmd->buf_lut; 247437e3a6d3SLuigi Rizzo return 0; 247537e3a6d3SLuigi Rizzo } 247637e3a6d3SLuigi Rizzo 247737e3a6d3SLuigi Rizzo static int 24784f80b14cSVincenzo Maffione netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, uint64_t *size, 247937e3a6d3SLuigi Rizzo u_int *memflags, uint16_t *id) 248037e3a6d3SLuigi Rizzo { 248137e3a6d3SLuigi Rizzo int error = 0; 248237e3a6d3SLuigi Rizzo 248337e3a6d3SLuigi Rizzo error = nmd->ops->nmd_config(nmd); 248437e3a6d3SLuigi Rizzo if (error) 248537e3a6d3SLuigi Rizzo goto out; 248637e3a6d3SLuigi Rizzo 248737e3a6d3SLuigi Rizzo if (size) 248837e3a6d3SLuigi Rizzo *size = nmd->nm_totalsize; 248937e3a6d3SLuigi Rizzo if (memflags) 249037e3a6d3SLuigi Rizzo *memflags = nmd->flags; 249137e3a6d3SLuigi Rizzo if (id) 249237e3a6d3SLuigi Rizzo *id = nmd->nm_id; 249337e3a6d3SLuigi Rizzo 249437e3a6d3SLuigi Rizzo out: 249537e3a6d3SLuigi Rizzo 249637e3a6d3SLuigi Rizzo return error; 249737e3a6d3SLuigi Rizzo } 249837e3a6d3SLuigi Rizzo 249937e3a6d3SLuigi Rizzo static vm_paddr_t 250037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off) 250137e3a6d3SLuigi Rizzo { 250237e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 250337e3a6d3SLuigi Rizzo vm_paddr_t paddr; 250437e3a6d3SLuigi Rizzo /* if the offset is valid, just return csb->base_addr + off */ 250537e3a6d3SLuigi Rizzo paddr = (vm_paddr_t)(ptnmd->nm_paddr + off); 250675f4f3edSVincenzo Maffione nm_prdis("off %lx padr %lx", off, (unsigned long)paddr); 250737e3a6d3SLuigi Rizzo return paddr; 250837e3a6d3SLuigi Rizzo } 250937e3a6d3SLuigi Rizzo 251037e3a6d3SLuigi Rizzo static int 251137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_config(struct netmap_mem_d *nmd) 251237e3a6d3SLuigi Rizzo { 251337e3a6d3SLuigi Rizzo /* nothing to do, we are configured on creation 251437e3a6d3SLuigi Rizzo * and configuration never changes thereafter 251537e3a6d3SLuigi Rizzo */ 251637e3a6d3SLuigi Rizzo return 0; 251737e3a6d3SLuigi Rizzo } 251837e3a6d3SLuigi Rizzo 251937e3a6d3SLuigi Rizzo static int 252037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd) 252137e3a6d3SLuigi Rizzo { 252237e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 2523844a6f0cSLuigi Rizzo uint64_t mem_size; 2524844a6f0cSLuigi Rizzo uint32_t bufsize; 2525844a6f0cSLuigi Rizzo uint32_t nbuffers; 2526844a6f0cSLuigi Rizzo uint32_t poolofs; 2527844a6f0cSLuigi Rizzo vm_paddr_t paddr; 2528844a6f0cSLuigi Rizzo char *vaddr; 2529844a6f0cSLuigi Rizzo int i; 253037e3a6d3SLuigi Rizzo int error = 0; 253137e3a6d3SLuigi Rizzo 253237e3a6d3SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 253337e3a6d3SLuigi Rizzo goto out; 253437e3a6d3SLuigi Rizzo 253537e3a6d3SLuigi Rizzo if (ptnmd->ptn_dev == NULL) { 253675f4f3edSVincenzo Maffione nm_prerr("ptnetmap memdev not attached"); 253737e3a6d3SLuigi Rizzo error = ENOMEM; 25382ff91c17SVincenzo Maffione goto out; 253937e3a6d3SLuigi Rizzo } 2540844a6f0cSLuigi Rizzo /* Map memory through ptnetmap-memdev BAR. */ 254137e3a6d3SLuigi Rizzo error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr, 2542844a6f0cSLuigi Rizzo &ptnmd->nm_addr, &mem_size); 254337e3a6d3SLuigi Rizzo if (error) 25442ff91c17SVincenzo Maffione goto out; 254537e3a6d3SLuigi Rizzo 2546844a6f0cSLuigi Rizzo /* Initialize the lut using the information contained in the 2547844a6f0cSLuigi Rizzo * ptnetmap memory device. */ 2548844a6f0cSLuigi Rizzo bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev, 2549844a6f0cSLuigi Rizzo PTNET_MDEV_IO_BUF_POOL_OBJSZ); 2550844a6f0cSLuigi Rizzo nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev, 2551844a6f0cSLuigi Rizzo PTNET_MDEV_IO_BUF_POOL_OBJNUM); 2552844a6f0cSLuigi Rizzo 2553844a6f0cSLuigi Rizzo /* allocate the lut */ 2554844a6f0cSLuigi Rizzo if (ptnmd->buf_lut.lut == NULL) { 255575f4f3edSVincenzo Maffione nm_prinf("allocating lut"); 2556844a6f0cSLuigi Rizzo ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers); 2557844a6f0cSLuigi Rizzo if (ptnmd->buf_lut.lut == NULL) { 255875f4f3edSVincenzo Maffione nm_prerr("lut allocation failed"); 2559844a6f0cSLuigi Rizzo return ENOMEM; 2560844a6f0cSLuigi Rizzo } 2561844a6f0cSLuigi Rizzo } 2562844a6f0cSLuigi Rizzo 2563844a6f0cSLuigi Rizzo /* we have physically contiguous memory mapped through PCI BAR */ 2564844a6f0cSLuigi Rizzo poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev, 2565844a6f0cSLuigi Rizzo PTNET_MDEV_IO_BUF_POOL_OFS); 2566844a6f0cSLuigi Rizzo vaddr = (char *)(ptnmd->nm_addr) + poolofs; 2567844a6f0cSLuigi Rizzo paddr = ptnmd->nm_paddr + poolofs; 2568844a6f0cSLuigi Rizzo 2569844a6f0cSLuigi Rizzo for (i = 0; i < nbuffers; i++) { 2570844a6f0cSLuigi Rizzo ptnmd->buf_lut.lut[i].vaddr = vaddr; 2571844a6f0cSLuigi Rizzo vaddr += bufsize; 2572844a6f0cSLuigi Rizzo paddr += bufsize; 2573844a6f0cSLuigi Rizzo } 2574844a6f0cSLuigi Rizzo 2575844a6f0cSLuigi Rizzo ptnmd->buf_lut.objtotal = nbuffers; 2576844a6f0cSLuigi Rizzo ptnmd->buf_lut.objsize = bufsize; 2577253b2ec1SVincenzo Maffione nmd->nm_totalsize = mem_size; 257837e3a6d3SLuigi Rizzo 25792ff91c17SVincenzo Maffione /* Initialize these fields as are needed by 25802ff91c17SVincenzo Maffione * netmap_mem_bufsize(). 25812ff91c17SVincenzo Maffione * XXX please improve this, why do we need this 25822ff91c17SVincenzo Maffione * replication? maybe we nmd->pools[] should no be 25832ff91c17SVincenzo Maffione * there for the guest allocator? */ 25842ff91c17SVincenzo Maffione nmd->pools[NETMAP_BUF_POOL]._objsize = bufsize; 25852ff91c17SVincenzo Maffione nmd->pools[NETMAP_BUF_POOL]._objtotal = nbuffers; 25862ff91c17SVincenzo Maffione 258737e3a6d3SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 258837e3a6d3SLuigi Rizzo out: 258937e3a6d3SLuigi Rizzo return error; 259037e3a6d3SLuigi Rizzo } 259137e3a6d3SLuigi Rizzo 259237e3a6d3SLuigi Rizzo static void 259337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd) 259437e3a6d3SLuigi Rizzo { 259537e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 259637e3a6d3SLuigi Rizzo 25972ff91c17SVincenzo Maffione if (nmd->active == 1 && 259837e3a6d3SLuigi Rizzo (nmd->flags & NETMAP_MEM_FINALIZED)) { 259937e3a6d3SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 260037e3a6d3SLuigi Rizzo /* unmap ptnetmap-memdev memory */ 260137e3a6d3SLuigi Rizzo if (ptnmd->ptn_dev) { 260237e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(ptnmd->ptn_dev); 260337e3a6d3SLuigi Rizzo } 26044dd44461SLuiz Otavio O Souza ptnmd->nm_addr = NULL; 260537e3a6d3SLuigi Rizzo ptnmd->nm_paddr = 0; 260637e3a6d3SLuigi Rizzo } 260737e3a6d3SLuigi Rizzo } 260837e3a6d3SLuigi Rizzo 260937e3a6d3SLuigi Rizzo static ssize_t 261037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr) 261137e3a6d3SLuigi Rizzo { 261237e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 261337e3a6d3SLuigi Rizzo 261437e3a6d3SLuigi Rizzo return (const char *)(vaddr) - (char *)(ptnmd->nm_addr); 261537e3a6d3SLuigi Rizzo } 261637e3a6d3SLuigi Rizzo 261737e3a6d3SLuigi Rizzo static void 261837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd) 261937e3a6d3SLuigi Rizzo { 262037e3a6d3SLuigi Rizzo if (nmd == NULL) 262137e3a6d3SLuigi Rizzo return; 262237e3a6d3SLuigi Rizzo if (netmap_verbose) 262375f4f3edSVincenzo Maffione nm_prinf("deleting %p", nmd); 262437e3a6d3SLuigi Rizzo if (nmd->active > 0) 262575f4f3edSVincenzo Maffione nm_prerr("bug: deleting mem allocator with active=%d!", nmd->active); 262637e3a6d3SLuigi Rizzo if (netmap_verbose) 262775f4f3edSVincenzo Maffione nm_prinf("done deleting %p", nmd); 262837e3a6d3SLuigi Rizzo NMA_LOCK_DESTROY(nmd); 2629c3e9b4dbSLuiz Otavio O Souza nm_os_free(nmd); 263037e3a6d3SLuigi Rizzo } 263137e3a6d3SLuigi Rizzo 263237e3a6d3SLuigi Rizzo static struct netmap_if * 2633c3e9b4dbSLuiz Otavio O Souza netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv) 263437e3a6d3SLuigi Rizzo { 263537e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 263637e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 263737e3a6d3SLuigi Rizzo struct netmap_if *nifp = NULL; 263837e3a6d3SLuigi Rizzo 263937e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 264037e3a6d3SLuigi Rizzo if (ptif == NULL) { 264175f4f3edSVincenzo Maffione nm_prerr("interface %s is not in passthrough", na->name); 264237e3a6d3SLuigi Rizzo goto out; 264337e3a6d3SLuigi Rizzo } 264437e3a6d3SLuigi Rizzo 264537e3a6d3SLuigi Rizzo nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) + 264637e3a6d3SLuigi Rizzo ptif->nifp_offset); 264737e3a6d3SLuigi Rizzo out: 264837e3a6d3SLuigi Rizzo return nifp; 264937e3a6d3SLuigi Rizzo } 265037e3a6d3SLuigi Rizzo 265137e3a6d3SLuigi Rizzo static void 265237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 265337e3a6d3SLuigi Rizzo { 265437e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 265537e3a6d3SLuigi Rizzo 265637e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 265737e3a6d3SLuigi Rizzo if (ptif == NULL) { 265875f4f3edSVincenzo Maffione nm_prerr("interface %s is not in passthrough", na->name); 265937e3a6d3SLuigi Rizzo } 266037e3a6d3SLuigi Rizzo } 266137e3a6d3SLuigi Rizzo 266237e3a6d3SLuigi Rizzo static int 266337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_create(struct netmap_adapter *na) 266437e3a6d3SLuigi Rizzo { 266537e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 266637e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 266737e3a6d3SLuigi Rizzo struct netmap_if *nifp; 266837e3a6d3SLuigi Rizzo int i, error = -1; 266937e3a6d3SLuigi Rizzo 267037e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 267137e3a6d3SLuigi Rizzo if (ptif == NULL) { 267275f4f3edSVincenzo Maffione nm_prerr("interface %s is not in passthrough", na->name); 267337e3a6d3SLuigi Rizzo goto out; 267437e3a6d3SLuigi Rizzo } 267537e3a6d3SLuigi Rizzo 267637e3a6d3SLuigi Rizzo 267737e3a6d3SLuigi Rizzo /* point each kring to the corresponding backend ring */ 267837e3a6d3SLuigi Rizzo nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset); 26792a7db7a6SVincenzo Maffione for (i = 0; i < netmap_all_rings(na, NR_TX); i++) { 26802ff91c17SVincenzo Maffione struct netmap_kring *kring = na->tx_rings[i]; 268137e3a6d3SLuigi Rizzo if (kring->ring) 268237e3a6d3SLuigi Rizzo continue; 268337e3a6d3SLuigi Rizzo kring->ring = (struct netmap_ring *) 268437e3a6d3SLuigi Rizzo ((char *)nifp + nifp->ring_ofs[i]); 268537e3a6d3SLuigi Rizzo } 26862a7db7a6SVincenzo Maffione for (i = 0; i < netmap_all_rings(na, NR_RX); i++) { 26872ff91c17SVincenzo Maffione struct netmap_kring *kring = na->rx_rings[i]; 268837e3a6d3SLuigi Rizzo if (kring->ring) 268937e3a6d3SLuigi Rizzo continue; 269037e3a6d3SLuigi Rizzo kring->ring = (struct netmap_ring *) 269137e3a6d3SLuigi Rizzo ((char *)nifp + 2692b6e66be2SVincenzo Maffione nifp->ring_ofs[netmap_all_rings(na, NR_TX) + i]); 269337e3a6d3SLuigi Rizzo } 269437e3a6d3SLuigi Rizzo 269537e3a6d3SLuigi Rizzo error = 0; 269637e3a6d3SLuigi Rizzo out: 269737e3a6d3SLuigi Rizzo return error; 269837e3a6d3SLuigi Rizzo } 269937e3a6d3SLuigi Rizzo 270037e3a6d3SLuigi Rizzo static void 270137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na) 270237e3a6d3SLuigi Rizzo { 270337e3a6d3SLuigi Rizzo #if 0 27044f80b14cSVincenzo Maffione enum txrx t; 27054f80b14cSVincenzo Maffione 27064f80b14cSVincenzo Maffione for_rx_tx(t) { 27074f80b14cSVincenzo Maffione u_int i; 27084f80b14cSVincenzo Maffione for (i = 0; i < nma_get_nrings(na, t) + 1; i++) { 27094f80b14cSVincenzo Maffione struct netmap_kring *kring = &NMR(na, t)[i]; 27104f80b14cSVincenzo Maffione 27114f80b14cSVincenzo Maffione kring->ring = NULL; 27124f80b14cSVincenzo Maffione } 27134f80b14cSVincenzo Maffione } 271437e3a6d3SLuigi Rizzo #endif 271537e3a6d3SLuigi Rizzo } 271637e3a6d3SLuigi Rizzo 271737e3a6d3SLuigi Rizzo static struct netmap_mem_ops netmap_mem_pt_guest_ops = { 271837e3a6d3SLuigi Rizzo .nmd_get_lut = netmap_mem_pt_guest_get_lut, 271937e3a6d3SLuigi Rizzo .nmd_get_info = netmap_mem_pt_guest_get_info, 272037e3a6d3SLuigi Rizzo .nmd_ofstophys = netmap_mem_pt_guest_ofstophys, 272137e3a6d3SLuigi Rizzo .nmd_config = netmap_mem_pt_guest_config, 272237e3a6d3SLuigi Rizzo .nmd_finalize = netmap_mem_pt_guest_finalize, 272337e3a6d3SLuigi Rizzo .nmd_deref = netmap_mem_pt_guest_deref, 272437e3a6d3SLuigi Rizzo .nmd_if_offset = netmap_mem_pt_guest_if_offset, 272537e3a6d3SLuigi Rizzo .nmd_delete = netmap_mem_pt_guest_delete, 272637e3a6d3SLuigi Rizzo .nmd_if_new = netmap_mem_pt_guest_if_new, 272737e3a6d3SLuigi Rizzo .nmd_if_delete = netmap_mem_pt_guest_if_delete, 272837e3a6d3SLuigi Rizzo .nmd_rings_create = netmap_mem_pt_guest_rings_create, 272937e3a6d3SLuigi Rizzo .nmd_rings_delete = netmap_mem_pt_guest_rings_delete 273037e3a6d3SLuigi Rizzo }; 273137e3a6d3SLuigi Rizzo 2732c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */ 273337e3a6d3SLuigi Rizzo static struct netmap_mem_d * 2734844a6f0cSLuigi Rizzo netmap_mem_pt_guest_find_memid(nm_memid_t mem_id) 273537e3a6d3SLuigi Rizzo { 273637e3a6d3SLuigi Rizzo struct netmap_mem_d *mem = NULL; 273737e3a6d3SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 273837e3a6d3SLuigi Rizzo 273937e3a6d3SLuigi Rizzo do { 274037e3a6d3SLuigi Rizzo /* find ptnetmap allocator through host ID */ 274137e3a6d3SLuigi Rizzo if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref && 2742844a6f0cSLuigi Rizzo ((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) { 274337e3a6d3SLuigi Rizzo mem = scan; 2744c3e9b4dbSLuiz Otavio O Souza mem->refcount++; 2745c3e9b4dbSLuiz Otavio O Souza NM_DBG_REFC(mem, __FUNCTION__, __LINE__); 274637e3a6d3SLuigi Rizzo break; 274737e3a6d3SLuigi Rizzo } 274837e3a6d3SLuigi Rizzo scan = scan->next; 274937e3a6d3SLuigi Rizzo } while (scan != netmap_last_mem_d); 275037e3a6d3SLuigi Rizzo 275137e3a6d3SLuigi Rizzo return mem; 275237e3a6d3SLuigi Rizzo } 275337e3a6d3SLuigi Rizzo 2754c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */ 275537e3a6d3SLuigi Rizzo static struct netmap_mem_d * 2756844a6f0cSLuigi Rizzo netmap_mem_pt_guest_create(nm_memid_t mem_id) 275737e3a6d3SLuigi Rizzo { 275837e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd; 275937e3a6d3SLuigi Rizzo int err = 0; 276037e3a6d3SLuigi Rizzo 2761c3e9b4dbSLuiz Otavio O Souza ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg)); 276237e3a6d3SLuigi Rizzo if (ptnmd == NULL) { 276337e3a6d3SLuigi Rizzo err = ENOMEM; 276437e3a6d3SLuigi Rizzo goto error; 276537e3a6d3SLuigi Rizzo } 276637e3a6d3SLuigi Rizzo 276737e3a6d3SLuigi Rizzo ptnmd->up.ops = &netmap_mem_pt_guest_ops; 2768844a6f0cSLuigi Rizzo ptnmd->host_mem_id = mem_id; 276937e3a6d3SLuigi Rizzo ptnmd->pt_ifs = NULL; 277037e3a6d3SLuigi Rizzo 277137e3a6d3SLuigi Rizzo /* Assign new id in the guest (We have the lock) */ 277237e3a6d3SLuigi Rizzo err = nm_mem_assign_id_locked(&ptnmd->up); 277337e3a6d3SLuigi Rizzo if (err) 277437e3a6d3SLuigi Rizzo goto error; 277537e3a6d3SLuigi Rizzo 277637e3a6d3SLuigi Rizzo ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED; 277737e3a6d3SLuigi Rizzo ptnmd->up.flags |= NETMAP_MEM_IO; 277837e3a6d3SLuigi Rizzo 277937e3a6d3SLuigi Rizzo NMA_LOCK_INIT(&ptnmd->up); 278037e3a6d3SLuigi Rizzo 2781c3e9b4dbSLuiz Otavio O Souza snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id); 2782c3e9b4dbSLuiz Otavio O Souza 2783c3e9b4dbSLuiz Otavio O Souza 278437e3a6d3SLuigi Rizzo return &ptnmd->up; 278537e3a6d3SLuigi Rizzo error: 278637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(&ptnmd->up); 278737e3a6d3SLuigi Rizzo return NULL; 278837e3a6d3SLuigi Rizzo } 278937e3a6d3SLuigi Rizzo 279037e3a6d3SLuigi Rizzo /* 279137e3a6d3SLuigi Rizzo * find host id in guest allocators and create guest allocator 279237e3a6d3SLuigi Rizzo * if it is not there 279337e3a6d3SLuigi Rizzo */ 279437e3a6d3SLuigi Rizzo static struct netmap_mem_d * 2795844a6f0cSLuigi Rizzo netmap_mem_pt_guest_get(nm_memid_t mem_id) 279637e3a6d3SLuigi Rizzo { 279737e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 279837e3a6d3SLuigi Rizzo 2799c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 2800844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_find_memid(mem_id); 280137e3a6d3SLuigi Rizzo if (nmd == NULL) { 2802844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_create(mem_id); 280337e3a6d3SLuigi Rizzo } 2804c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 280537e3a6d3SLuigi Rizzo 280637e3a6d3SLuigi Rizzo return nmd; 280737e3a6d3SLuigi Rizzo } 280837e3a6d3SLuigi Rizzo 280937e3a6d3SLuigi Rizzo /* 281037e3a6d3SLuigi Rizzo * The guest allocator can be created by ptnetmap_memdev (during the device 2811844a6f0cSLuigi Rizzo * attach) or by ptnetmap device (ptnet), during the netmap_attach. 281237e3a6d3SLuigi Rizzo * 281337e3a6d3SLuigi Rizzo * The order is not important (we have different order in LINUX and FreeBSD). 281437e3a6d3SLuigi Rizzo * The first one, creates the device, and the second one simply attaches it. 281537e3a6d3SLuigi Rizzo */ 281637e3a6d3SLuigi Rizzo 281737e3a6d3SLuigi Rizzo /* Called when ptnetmap_memdev is attaching, to attach a new allocator in 281837e3a6d3SLuigi Rizzo * the guest */ 281937e3a6d3SLuigi Rizzo struct netmap_mem_d * 2820844a6f0cSLuigi Rizzo netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id) 282137e3a6d3SLuigi Rizzo { 282237e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 282337e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd; 282437e3a6d3SLuigi Rizzo 2825844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_get(mem_id); 282637e3a6d3SLuigi Rizzo 282737e3a6d3SLuigi Rizzo /* assign this device to the guest allocator */ 282837e3a6d3SLuigi Rizzo if (nmd) { 282937e3a6d3SLuigi Rizzo ptnmd = (struct netmap_mem_ptg *)nmd; 283037e3a6d3SLuigi Rizzo ptnmd->ptn_dev = ptn_dev; 283137e3a6d3SLuigi Rizzo } 283237e3a6d3SLuigi Rizzo 283337e3a6d3SLuigi Rizzo return nmd; 283437e3a6d3SLuigi Rizzo } 283537e3a6d3SLuigi Rizzo 2836844a6f0cSLuigi Rizzo /* Called when ptnet device is attaching */ 283737e3a6d3SLuigi Rizzo struct netmap_mem_d * 283837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_new(struct ifnet *ifp, 283937e3a6d3SLuigi Rizzo unsigned int nifp_offset, 2840844a6f0cSLuigi Rizzo unsigned int memid) 284137e3a6d3SLuigi Rizzo { 284237e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 284337e3a6d3SLuigi Rizzo 2844844a6f0cSLuigi Rizzo if (ifp == NULL) { 284537e3a6d3SLuigi Rizzo return NULL; 284637e3a6d3SLuigi Rizzo } 284737e3a6d3SLuigi Rizzo 2848844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_get((nm_memid_t)memid); 284937e3a6d3SLuigi Rizzo 285037e3a6d3SLuigi Rizzo if (nmd) { 2851844a6f0cSLuigi Rizzo netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset); 285237e3a6d3SLuigi Rizzo } 285337e3a6d3SLuigi Rizzo 285437e3a6d3SLuigi Rizzo return nmd; 285537e3a6d3SLuigi Rizzo } 285637e3a6d3SLuigi Rizzo 2857b6e66be2SVincenzo Maffione #endif /* WITH_PTNETMAP */ 2858