1ccdc3305SLuigi Rizzo /* 217885a7bSLuigi Rizzo * Copyright (C) 2012-2014 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved. 3ccdc3305SLuigi Rizzo * 4ccdc3305SLuigi Rizzo * Redistribution and use in source and binary forms, with or without 5ccdc3305SLuigi Rizzo * modification, are permitted provided that the following conditions 6ccdc3305SLuigi Rizzo * are met: 7ccdc3305SLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 8ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer. 9ccdc3305SLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 10ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 11ccdc3305SLuigi Rizzo * documentation and/or other materials provided with the distribution. 12ccdc3305SLuigi Rizzo * 13ccdc3305SLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14ccdc3305SLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15ccdc3305SLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16ccdc3305SLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17ccdc3305SLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18ccdc3305SLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19ccdc3305SLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20ccdc3305SLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21ccdc3305SLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22ccdc3305SLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23ccdc3305SLuigi Rizzo * SUCH DAMAGE. 24ccdc3305SLuigi Rizzo */ 25ccdc3305SLuigi Rizzo 268241616dSLuigi Rizzo #ifdef linux 27ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 288241616dSLuigi Rizzo #endif /* linux */ 298241616dSLuigi Rizzo 30ce3ee1e7SLuigi Rizzo #ifdef __APPLE__ 31ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 32ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */ 338241616dSLuigi Rizzo 34ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 35ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 36ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$"); 378241616dSLuigi Rizzo 38ce3ee1e7SLuigi Rizzo #include <sys/types.h> 39ce3ee1e7SLuigi Rizzo #include <sys/malloc.h> 40ce3ee1e7SLuigi Rizzo #include <sys/proc.h> 41ce3ee1e7SLuigi Rizzo #include <vm/vm.h> /* vtophys */ 42ce3ee1e7SLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 43ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 44ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h> 45ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h> 46ce3ee1e7SLuigi Rizzo #include <net/if.h> 47ce3ee1e7SLuigi Rizzo #include <net/if_var.h> 48ce3ee1e7SLuigi Rizzo #include <net/vnet.h> 49ce3ee1e7SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 50ce3ee1e7SLuigi Rizzo 51ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 52ce3ee1e7SLuigi Rizzo 53ce3ee1e7SLuigi Rizzo #include <net/netmap.h> 54ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 55ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h" 56ce3ee1e7SLuigi Rizzo 57ce3ee1e7SLuigi Rizzo #ifdef linux 58ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n) sema_init(&(n)->nm_mtx, 1) 59ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n) 60ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n) down(&(n)->nm_mtx) 61ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n) up(&(n)->nm_mtx) 62ce3ee1e7SLuigi Rizzo #else /* !linux */ 63ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n) mtx_init(&(n)->nm_mtx, "netmap memory allocator lock", NULL, MTX_DEF) 64ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n) mtx_destroy(&(n)->nm_mtx) 65ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n) mtx_lock(&(n)->nm_mtx) 66ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n) mtx_unlock(&(n)->nm_mtx) 67ce3ee1e7SLuigi Rizzo #endif /* linux */ 688241616dSLuigi Rizzo 698241616dSLuigi Rizzo 708241616dSLuigi Rizzo struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = { 718241616dSLuigi Rizzo [NETMAP_IF_POOL] = { 728241616dSLuigi Rizzo .size = 1024, 738241616dSLuigi Rizzo .num = 100, 748241616dSLuigi Rizzo }, 758241616dSLuigi Rizzo [NETMAP_RING_POOL] = { 768241616dSLuigi Rizzo .size = 9*PAGE_SIZE, 778241616dSLuigi Rizzo .num = 200, 788241616dSLuigi Rizzo }, 798241616dSLuigi Rizzo [NETMAP_BUF_POOL] = { 808241616dSLuigi Rizzo .size = 2048, 818241616dSLuigi Rizzo .num = NETMAP_BUF_MAX_NUM, 828241616dSLuigi Rizzo }, 838241616dSLuigi Rizzo }; 848241616dSLuigi Rizzo 85*f0ea3689SLuigi Rizzo struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = { 86*f0ea3689SLuigi Rizzo [NETMAP_IF_POOL] = { 87*f0ea3689SLuigi Rizzo .size = 1024, 88*f0ea3689SLuigi Rizzo .num = 1, 89*f0ea3689SLuigi Rizzo }, 90*f0ea3689SLuigi Rizzo [NETMAP_RING_POOL] = { 91*f0ea3689SLuigi Rizzo .size = 5*PAGE_SIZE, 92*f0ea3689SLuigi Rizzo .num = 4, 93*f0ea3689SLuigi Rizzo }, 94*f0ea3689SLuigi Rizzo [NETMAP_BUF_POOL] = { 95*f0ea3689SLuigi Rizzo .size = 2048, 96*f0ea3689SLuigi Rizzo .num = 4098, 97*f0ea3689SLuigi Rizzo }, 98*f0ea3689SLuigi Rizzo }; 99*f0ea3689SLuigi Rizzo 100ccdc3305SLuigi Rizzo 1012579e2d7SLuigi Rizzo /* 1022579e2d7SLuigi Rizzo * nm_mem is the memory allocator used for all physical interfaces 1032579e2d7SLuigi Rizzo * running in netmap mode. 1042579e2d7SLuigi Rizzo * Virtual (VALE) ports will have each its own allocator. 1052579e2d7SLuigi Rizzo */ 106ce3ee1e7SLuigi Rizzo static int netmap_mem_global_config(struct netmap_mem_d *nmd); 107ce3ee1e7SLuigi Rizzo static int netmap_mem_global_finalize(struct netmap_mem_d *nmd); 108ce3ee1e7SLuigi Rizzo static void netmap_mem_global_deref(struct netmap_mem_d *nmd); 109ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = { /* Our memory allocator. */ 1108241616dSLuigi Rizzo .pools = { 1118241616dSLuigi Rizzo [NETMAP_IF_POOL] = { 1128241616dSLuigi Rizzo .name = "netmap_if", 1138241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_if), 1148241616dSLuigi Rizzo .objmaxsize = 4096, 1158241616dSLuigi Rizzo .nummin = 10, /* don't be stingy */ 1168241616dSLuigi Rizzo .nummax = 10000, /* XXX very large */ 1178241616dSLuigi Rizzo }, 1188241616dSLuigi Rizzo [NETMAP_RING_POOL] = { 1198241616dSLuigi Rizzo .name = "netmap_ring", 1208241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 1218241616dSLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 1228241616dSLuigi Rizzo .nummin = 2, 1238241616dSLuigi Rizzo .nummax = 1024, 1248241616dSLuigi Rizzo }, 1258241616dSLuigi Rizzo [NETMAP_BUF_POOL] = { 1268241616dSLuigi Rizzo .name = "netmap_buf", 1278241616dSLuigi Rizzo .objminsize = 64, 1288241616dSLuigi Rizzo .objmaxsize = 65536, 1298241616dSLuigi Rizzo .nummin = 4, 1308241616dSLuigi Rizzo .nummax = 1000000, /* one million! */ 1318241616dSLuigi Rizzo }, 1328241616dSLuigi Rizzo }, 133ce3ee1e7SLuigi Rizzo .config = netmap_mem_global_config, 134ce3ee1e7SLuigi Rizzo .finalize = netmap_mem_global_finalize, 135ce3ee1e7SLuigi Rizzo .deref = netmap_mem_global_deref, 136*f0ea3689SLuigi Rizzo 137*f0ea3689SLuigi Rizzo .nm_id = 1, 138*f0ea3689SLuigi Rizzo 139*f0ea3689SLuigi Rizzo .prev = &nm_mem, 140*f0ea3689SLuigi Rizzo .next = &nm_mem, 141ccdc3305SLuigi Rizzo }; 142ccdc3305SLuigi Rizzo 143ce3ee1e7SLuigi Rizzo 144*f0ea3689SLuigi Rizzo struct netmap_mem_d *netmap_last_mem_d = &nm_mem; 145*f0ea3689SLuigi Rizzo 1462579e2d7SLuigi Rizzo // XXX logically belongs to nm_mem 147ccdc3305SLuigi Rizzo struct lut_entry *netmap_buffer_lut; /* exported */ 148ccdc3305SLuigi Rizzo 149ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */ 150ce3ee1e7SLuigi Rizzo static int netmap_mem_private_config(struct netmap_mem_d *nmd); 151ce3ee1e7SLuigi Rizzo static int netmap_mem_private_finalize(struct netmap_mem_d *nmd); 152ce3ee1e7SLuigi Rizzo static void netmap_mem_private_deref(struct netmap_mem_d *nmd); 153ce3ee1e7SLuigi Rizzo const struct netmap_mem_d nm_blueprint = { 154ce3ee1e7SLuigi Rizzo .pools = { 155ce3ee1e7SLuigi Rizzo [NETMAP_IF_POOL] = { 156ce3ee1e7SLuigi Rizzo .name = "%s_if", 157ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_if), 158ce3ee1e7SLuigi Rizzo .objmaxsize = 4096, 159ce3ee1e7SLuigi Rizzo .nummin = 1, 160*f0ea3689SLuigi Rizzo .nummax = 100, 161ce3ee1e7SLuigi Rizzo }, 162ce3ee1e7SLuigi Rizzo [NETMAP_RING_POOL] = { 163ce3ee1e7SLuigi Rizzo .name = "%s_ring", 164ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 165ce3ee1e7SLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 166ce3ee1e7SLuigi Rizzo .nummin = 2, 167ce3ee1e7SLuigi Rizzo .nummax = 1024, 168ce3ee1e7SLuigi Rizzo }, 169ce3ee1e7SLuigi Rizzo [NETMAP_BUF_POOL] = { 170ce3ee1e7SLuigi Rizzo .name = "%s_buf", 171ce3ee1e7SLuigi Rizzo .objminsize = 64, 172ce3ee1e7SLuigi Rizzo .objmaxsize = 65536, 173ce3ee1e7SLuigi Rizzo .nummin = 4, 174ce3ee1e7SLuigi Rizzo .nummax = 1000000, /* one million! */ 175ce3ee1e7SLuigi Rizzo }, 176ce3ee1e7SLuigi Rizzo }, 177ce3ee1e7SLuigi Rizzo .config = netmap_mem_private_config, 178ce3ee1e7SLuigi Rizzo .finalize = netmap_mem_private_finalize, 179ce3ee1e7SLuigi Rizzo .deref = netmap_mem_private_deref, 180ce3ee1e7SLuigi Rizzo 181ce3ee1e7SLuigi Rizzo .flags = NETMAP_MEM_PRIVATE, 182ce3ee1e7SLuigi Rizzo }; 183ce3ee1e7SLuigi Rizzo 1848241616dSLuigi Rizzo /* memory allocator related sysctls */ 1858241616dSLuigi Rizzo 1868241616dSLuigi Rizzo #define STRINGIFY(x) #x 1878241616dSLuigi Rizzo 188ce3ee1e7SLuigi Rizzo 1898241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \ 1908241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \ 1918241616dSLuigi Rizzo CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \ 1928241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \ 1938241616dSLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \ 1948241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \ 1958241616dSLuigi Rizzo CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \ 1968241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \ 197*f0ea3689SLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \ 198*f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \ 199*f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \ 200*f0ea3689SLuigi Rizzo "Default size of private netmap " STRINGIFY(name) "s"); \ 201*f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \ 202*f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \ 203*f0ea3689SLuigi Rizzo "Default number of private netmap " STRINGIFY(name) "s") 2048241616dSLuigi Rizzo 205ce3ee1e7SLuigi Rizzo SYSCTL_DECL(_dev_netmap); 2068241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if); 2078241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring); 2088241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf); 209ccdc3305SLuigi Rizzo 210*f0ea3689SLuigi Rizzo static int 211*f0ea3689SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd) 212*f0ea3689SLuigi Rizzo { 213*f0ea3689SLuigi Rizzo nm_memid_t id; 214*f0ea3689SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 215*f0ea3689SLuigi Rizzo int error = ENOMEM; 216*f0ea3689SLuigi Rizzo 217*f0ea3689SLuigi Rizzo NMA_LOCK(&nm_mem); 218*f0ea3689SLuigi Rizzo 219*f0ea3689SLuigi Rizzo do { 220*f0ea3689SLuigi Rizzo /* we rely on unsigned wrap around */ 221*f0ea3689SLuigi Rizzo id = scan->nm_id + 1; 222*f0ea3689SLuigi Rizzo if (id == 0) /* reserve 0 as error value */ 223*f0ea3689SLuigi Rizzo id = 1; 224*f0ea3689SLuigi Rizzo scan = scan->next; 225*f0ea3689SLuigi Rizzo if (id != scan->nm_id) { 226*f0ea3689SLuigi Rizzo nmd->nm_id = id; 227*f0ea3689SLuigi Rizzo nmd->prev = scan->prev; 228*f0ea3689SLuigi Rizzo nmd->next = scan; 229*f0ea3689SLuigi Rizzo scan->prev->next = nmd; 230*f0ea3689SLuigi Rizzo scan->prev = nmd; 231*f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd; 232*f0ea3689SLuigi Rizzo error = 0; 233*f0ea3689SLuigi Rizzo break; 234*f0ea3689SLuigi Rizzo } 235*f0ea3689SLuigi Rizzo } while (scan != netmap_last_mem_d); 236*f0ea3689SLuigi Rizzo 237*f0ea3689SLuigi Rizzo NMA_UNLOCK(&nm_mem); 238*f0ea3689SLuigi Rizzo return error; 239*f0ea3689SLuigi Rizzo } 240*f0ea3689SLuigi Rizzo 241*f0ea3689SLuigi Rizzo static void 242*f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd) 243*f0ea3689SLuigi Rizzo { 244*f0ea3689SLuigi Rizzo NMA_LOCK(&nm_mem); 245*f0ea3689SLuigi Rizzo 246*f0ea3689SLuigi Rizzo nmd->prev->next = nmd->next; 247*f0ea3689SLuigi Rizzo nmd->next->prev = nmd->prev; 248*f0ea3689SLuigi Rizzo 249*f0ea3689SLuigi Rizzo if (netmap_last_mem_d == nmd) 250*f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd->prev; 251*f0ea3689SLuigi Rizzo 252*f0ea3689SLuigi Rizzo nmd->prev = nmd->next = NULL; 253*f0ea3689SLuigi Rizzo 254*f0ea3689SLuigi Rizzo NMA_UNLOCK(&nm_mem); 255*f0ea3689SLuigi Rizzo } 256*f0ea3689SLuigi Rizzo 257*f0ea3689SLuigi Rizzo 258ccdc3305SLuigi Rizzo /* 2592579e2d7SLuigi Rizzo * First, find the allocator that contains the requested offset, 2602579e2d7SLuigi Rizzo * then locate the cluster through a lookup table. 261ccdc3305SLuigi Rizzo */ 262ce3ee1e7SLuigi Rizzo vm_paddr_t 263ce3ee1e7SLuigi Rizzo netmap_mem_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset) 264ccdc3305SLuigi Rizzo { 265ccdc3305SLuigi Rizzo int i; 266ce3ee1e7SLuigi Rizzo vm_ooffset_t o = offset; 267ce3ee1e7SLuigi Rizzo vm_paddr_t pa; 268ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p; 269ccdc3305SLuigi Rizzo 270ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 271ce3ee1e7SLuigi Rizzo p = nmd->pools; 272ce3ee1e7SLuigi Rizzo 273ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) { 274ce3ee1e7SLuigi Rizzo if (offset >= p[i].memtotal) 275ccdc3305SLuigi Rizzo continue; 2762579e2d7SLuigi Rizzo // now lookup the cluster's address 277ce3ee1e7SLuigi Rizzo pa = p[i].lut[offset / p[i]._objsize].paddr + 2788241616dSLuigi Rizzo offset % p[i]._objsize; 279ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 280ce3ee1e7SLuigi Rizzo return pa; 281ccdc3305SLuigi Rizzo } 2828241616dSLuigi Rizzo /* this is only in case of errors */ 283b1123b01SLuigi Rizzo D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o, 284ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal, 285ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 286ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal, 287ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 288ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal 289ce3ee1e7SLuigi Rizzo + p[NETMAP_BUF_POOL].memtotal); 290ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 291ccdc3305SLuigi Rizzo return 0; // XXX bad address 292ccdc3305SLuigi Rizzo } 293ccdc3305SLuigi Rizzo 294ce3ee1e7SLuigi Rizzo int 295*f0ea3689SLuigi Rizzo netmap_mem_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags, 296*f0ea3689SLuigi Rizzo nm_memid_t *id) 297ce3ee1e7SLuigi Rizzo { 298ce3ee1e7SLuigi Rizzo int error = 0; 299ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 300ce3ee1e7SLuigi Rizzo error = nmd->config(nmd); 301ce3ee1e7SLuigi Rizzo if (error) 302ce3ee1e7SLuigi Rizzo goto out; 303ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 304ce3ee1e7SLuigi Rizzo *size = nmd->nm_totalsize; 305ce3ee1e7SLuigi Rizzo } else { 306ce3ee1e7SLuigi Rizzo int i; 307ce3ee1e7SLuigi Rizzo *size = 0; 308ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 309ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = nmd->pools + i; 310ce3ee1e7SLuigi Rizzo *size += (p->_numclusters * p->_clustsize); 311ce3ee1e7SLuigi Rizzo } 312ce3ee1e7SLuigi Rizzo } 313ce3ee1e7SLuigi Rizzo *memflags = nmd->flags; 314*f0ea3689SLuigi Rizzo *id = nmd->nm_id; 315ce3ee1e7SLuigi Rizzo out: 316ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 317ce3ee1e7SLuigi Rizzo return error; 318ce3ee1e7SLuigi Rizzo } 319ce3ee1e7SLuigi Rizzo 320ccdc3305SLuigi Rizzo /* 321ccdc3305SLuigi Rizzo * we store objects by kernel address, need to find the offset 322ccdc3305SLuigi Rizzo * within the pool to export the value to userspace. 323ccdc3305SLuigi Rizzo * Algorithm: scan until we find the cluster, then add the 324ccdc3305SLuigi Rizzo * actual offset in the cluster 325ccdc3305SLuigi Rizzo */ 326ce2cb792SLuigi Rizzo static ssize_t 327ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr) 328ccdc3305SLuigi Rizzo { 329ce3ee1e7SLuigi Rizzo int i, k = p->_clustentries, n = p->objtotal; 330ccdc3305SLuigi Rizzo ssize_t ofs = 0; 331ccdc3305SLuigi Rizzo 332ccdc3305SLuigi Rizzo for (i = 0; i < n; i += k, ofs += p->_clustsize) { 333ccdc3305SLuigi Rizzo const char *base = p->lut[i].vaddr; 334ccdc3305SLuigi Rizzo ssize_t relofs = (const char *) vaddr - base; 335ccdc3305SLuigi Rizzo 336aa76317cSLuigi Rizzo if (relofs < 0 || relofs >= p->_clustsize) 337ccdc3305SLuigi Rizzo continue; 338ccdc3305SLuigi Rizzo 339ccdc3305SLuigi Rizzo ofs = ofs + relofs; 340ccdc3305SLuigi Rizzo ND("%s: return offset %d (cluster %d) for pointer %p", 341ccdc3305SLuigi Rizzo p->name, ofs, i, vaddr); 342ccdc3305SLuigi Rizzo return ofs; 343ccdc3305SLuigi Rizzo } 344ccdc3305SLuigi Rizzo D("address %p is not contained inside any cluster (%s)", 345ccdc3305SLuigi Rizzo vaddr, p->name); 346ccdc3305SLuigi Rizzo return 0; /* An error occurred */ 347ccdc3305SLuigi Rizzo } 348ccdc3305SLuigi Rizzo 349ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */ 350ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v) \ 351ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v)) 352ccdc3305SLuigi Rizzo 353ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v) \ 354ce3ee1e7SLuigi Rizzo ((n)->pools[NETMAP_IF_POOL].memtotal + \ 355ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v))) 356ccdc3305SLuigi Rizzo 357ce3ee1e7SLuigi Rizzo #define netmap_buf_offset(n, v) \ 358ce3ee1e7SLuigi Rizzo ((n)->pools[NETMAP_IF_POOL].memtotal + \ 359ce3ee1e7SLuigi Rizzo (n)->pools[NETMAP_RING_POOL].memtotal + \ 360ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v))) 361ccdc3305SLuigi Rizzo 362ccdc3305SLuigi Rizzo 363ce3ee1e7SLuigi Rizzo ssize_t 364ce3ee1e7SLuigi Rizzo netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *addr) 365ce3ee1e7SLuigi Rizzo { 366ce3ee1e7SLuigi Rizzo ssize_t v; 367ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 368ce3ee1e7SLuigi Rizzo v = netmap_if_offset(nmd, addr); 369ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 370ce3ee1e7SLuigi Rizzo return v; 371ce3ee1e7SLuigi Rizzo } 372ce3ee1e7SLuigi Rizzo 3738241616dSLuigi Rizzo /* 3748241616dSLuigi Rizzo * report the index, and use start position as a hint, 3758241616dSLuigi Rizzo * otherwise buffer allocation becomes terribly expensive. 3768241616dSLuigi Rizzo */ 377ccdc3305SLuigi Rizzo static void * 378ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index) 379ccdc3305SLuigi Rizzo { 380ccdc3305SLuigi Rizzo uint32_t i = 0; /* index in the bitmap */ 381ccdc3305SLuigi Rizzo uint32_t mask, j; /* slot counter */ 382ccdc3305SLuigi Rizzo void *vaddr = NULL; 383ccdc3305SLuigi Rizzo 384ccdc3305SLuigi Rizzo if (len > p->_objsize) { 385ccdc3305SLuigi Rizzo D("%s request size %d too large", p->name, len); 386ccdc3305SLuigi Rizzo // XXX cannot reduce the size 387ccdc3305SLuigi Rizzo return NULL; 388ccdc3305SLuigi Rizzo } 389ccdc3305SLuigi Rizzo 390ccdc3305SLuigi Rizzo if (p->objfree == 0) { 391f9790aebSLuigi Rizzo D("no more %s objects", p->name); 392ccdc3305SLuigi Rizzo return NULL; 393ccdc3305SLuigi Rizzo } 3948241616dSLuigi Rizzo if (start) 3958241616dSLuigi Rizzo i = *start; 396ccdc3305SLuigi Rizzo 3978241616dSLuigi Rizzo /* termination is guaranteed by p->free, but better check bounds on i */ 3988241616dSLuigi Rizzo while (vaddr == NULL && i < p->bitmap_slots) { 399ccdc3305SLuigi Rizzo uint32_t cur = p->bitmap[i]; 400ccdc3305SLuigi Rizzo if (cur == 0) { /* bitmask is fully used */ 401ccdc3305SLuigi Rizzo i++; 402ccdc3305SLuigi Rizzo continue; 403ccdc3305SLuigi Rizzo } 404ccdc3305SLuigi Rizzo /* locate a slot */ 405ccdc3305SLuigi Rizzo for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) 406ccdc3305SLuigi Rizzo ; 407ccdc3305SLuigi Rizzo 408ccdc3305SLuigi Rizzo p->bitmap[i] &= ~mask; /* mark object as in use */ 409ccdc3305SLuigi Rizzo p->objfree--; 410ccdc3305SLuigi Rizzo 411ccdc3305SLuigi Rizzo vaddr = p->lut[i * 32 + j].vaddr; 4128241616dSLuigi Rizzo if (index) 4138241616dSLuigi Rizzo *index = i * 32 + j; 414ccdc3305SLuigi Rizzo } 415ccdc3305SLuigi Rizzo ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr); 416ccdc3305SLuigi Rizzo 4178241616dSLuigi Rizzo if (start) 4188241616dSLuigi Rizzo *start = i; 419ccdc3305SLuigi Rizzo return vaddr; 420ccdc3305SLuigi Rizzo } 421ccdc3305SLuigi Rizzo 422ccdc3305SLuigi Rizzo 423ccdc3305SLuigi Rizzo /* 424*f0ea3689SLuigi Rizzo * free by index, not by address. 425*f0ea3689SLuigi Rizzo * XXX should we also cleanup the content ? 426ccdc3305SLuigi Rizzo */ 427*f0ea3689SLuigi Rizzo static int 428ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j) 429ccdc3305SLuigi Rizzo { 430*f0ea3689SLuigi Rizzo uint32_t *ptr, mask; 431*f0ea3689SLuigi Rizzo 432ccdc3305SLuigi Rizzo if (j >= p->objtotal) { 433ccdc3305SLuigi Rizzo D("invalid index %u, max %u", j, p->objtotal); 434*f0ea3689SLuigi Rizzo return 1; 435ccdc3305SLuigi Rizzo } 436*f0ea3689SLuigi Rizzo ptr = &p->bitmap[j / 32]; 437*f0ea3689SLuigi Rizzo mask = (1 << (j % 32)); 438*f0ea3689SLuigi Rizzo if (*ptr & mask) { 439*f0ea3689SLuigi Rizzo D("ouch, double free on buffer %d", j); 440*f0ea3689SLuigi Rizzo return 1; 441*f0ea3689SLuigi Rizzo } else { 442*f0ea3689SLuigi Rizzo *ptr |= mask; 443ccdc3305SLuigi Rizzo p->objfree++; 444*f0ea3689SLuigi Rizzo return 0; 445*f0ea3689SLuigi Rizzo } 446ccdc3305SLuigi Rizzo } 447ccdc3305SLuigi Rizzo 448*f0ea3689SLuigi Rizzo /* 449*f0ea3689SLuigi Rizzo * free by address. This is slow but is only used for a few 450*f0ea3689SLuigi Rizzo * objects (rings, nifp) 451*f0ea3689SLuigi Rizzo */ 452ccdc3305SLuigi Rizzo static void 453ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr) 454ccdc3305SLuigi Rizzo { 455ce3ee1e7SLuigi Rizzo u_int i, j, n = p->numclusters; 456ccdc3305SLuigi Rizzo 457ce3ee1e7SLuigi Rizzo for (i = 0, j = 0; i < n; i++, j += p->_clustentries) { 458ce3ee1e7SLuigi Rizzo void *base = p->lut[i * p->_clustentries].vaddr; 459ccdc3305SLuigi Rizzo ssize_t relofs = (ssize_t) vaddr - (ssize_t) base; 460ccdc3305SLuigi Rizzo 461ccdc3305SLuigi Rizzo /* Given address, is out of the scope of the current cluster.*/ 462ede69cffSLuigi Rizzo if (vaddr < base || relofs >= p->_clustsize) 463ccdc3305SLuigi Rizzo continue; 464ccdc3305SLuigi Rizzo 465ccdc3305SLuigi Rizzo j = j + relofs / p->_objsize; 466ce3ee1e7SLuigi Rizzo /* KASSERT(j != 0, ("Cannot free object 0")); */ 467ccdc3305SLuigi Rizzo netmap_obj_free(p, j); 468ccdc3305SLuigi Rizzo return; 469ccdc3305SLuigi Rizzo } 470ae10d1afSLuigi Rizzo D("address %p is not contained inside any cluster (%s)", 471ccdc3305SLuigi Rizzo vaddr, p->name); 472ccdc3305SLuigi Rizzo } 473ccdc3305SLuigi Rizzo 474ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL) 475ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v)) 476ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL) 477ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v)) 478ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index) \ 479ce3ee1e7SLuigi Rizzo netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], NETMAP_BDG_BUF_SIZE(n), _pos, _index) 480ccdc3305SLuigi Rizzo 481ccdc3305SLuigi Rizzo 482*f0ea3689SLuigi Rizzo #if 0 // XXX unused 483ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */ 484ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v) \ 485ce3ee1e7SLuigi Rizzo (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n)) 486*f0ea3689SLuigi Rizzo #endif 487*f0ea3689SLuigi Rizzo 488*f0ea3689SLuigi Rizzo /* 489*f0ea3689SLuigi Rizzo * allocate extra buffers in a linked list. 490*f0ea3689SLuigi Rizzo * returns the actual number. 491*f0ea3689SLuigi Rizzo */ 492*f0ea3689SLuigi Rizzo uint32_t 493*f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n) 494*f0ea3689SLuigi Rizzo { 495*f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 496*f0ea3689SLuigi Rizzo uint32_t i, pos = 0; /* opaque, scan position in the bitmap */ 497*f0ea3689SLuigi Rizzo 498*f0ea3689SLuigi Rizzo NMA_LOCK(nmd); 499*f0ea3689SLuigi Rizzo 500*f0ea3689SLuigi Rizzo *head = 0; /* default, 'null' index ie empty list */ 501*f0ea3689SLuigi Rizzo for (i = 0 ; i < n; i++) { 502*f0ea3689SLuigi Rizzo uint32_t cur = *head; /* save current head */ 503*f0ea3689SLuigi Rizzo uint32_t *p = netmap_buf_malloc(nmd, &pos, head); 504*f0ea3689SLuigi Rizzo if (p == NULL) { 505*f0ea3689SLuigi Rizzo D("no more buffers after %d of %d", i, n); 506*f0ea3689SLuigi Rizzo *head = cur; /* restore */ 507*f0ea3689SLuigi Rizzo break; 508*f0ea3689SLuigi Rizzo } 509*f0ea3689SLuigi Rizzo RD(5, "allocate buffer %d -> %d", *head, cur); 510*f0ea3689SLuigi Rizzo *p = cur; /* link to previous head */ 511*f0ea3689SLuigi Rizzo } 512*f0ea3689SLuigi Rizzo 513*f0ea3689SLuigi Rizzo NMA_UNLOCK(nmd); 514*f0ea3689SLuigi Rizzo 515*f0ea3689SLuigi Rizzo return i; 516*f0ea3689SLuigi Rizzo } 517*f0ea3689SLuigi Rizzo 518*f0ea3689SLuigi Rizzo static void 519*f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head) 520*f0ea3689SLuigi Rizzo { 521*f0ea3689SLuigi Rizzo struct lut_entry *lut = na->na_lut; 522*f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 523*f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 524*f0ea3689SLuigi Rizzo uint32_t i, cur, *buf; 525*f0ea3689SLuigi Rizzo 526*f0ea3689SLuigi Rizzo D("freeing the extra list"); 527*f0ea3689SLuigi Rizzo for (i = 0; head >=2 && head < p->objtotal; i++) { 528*f0ea3689SLuigi Rizzo cur = head; 529*f0ea3689SLuigi Rizzo buf = lut[head].vaddr; 530*f0ea3689SLuigi Rizzo head = *buf; 531*f0ea3689SLuigi Rizzo *buf = 0; 532*f0ea3689SLuigi Rizzo if (netmap_obj_free(p, cur)) 533*f0ea3689SLuigi Rizzo break; 534*f0ea3689SLuigi Rizzo } 535*f0ea3689SLuigi Rizzo if (head != 0) 536*f0ea3689SLuigi Rizzo D("breaking with head %d", head); 537*f0ea3689SLuigi Rizzo D("freed %d buffers", i); 538*f0ea3689SLuigi Rizzo } 539ccdc3305SLuigi Rizzo 540ccdc3305SLuigi Rizzo 5418241616dSLuigi Rizzo /* Return nonzero on error */ 5428241616dSLuigi Rizzo static int 543f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 544ccdc3305SLuigi Rizzo { 545ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 546ce3ee1e7SLuigi Rizzo u_int i = 0; /* slot counter */ 5478241616dSLuigi Rizzo uint32_t pos = 0; /* slot in p->bitmap */ 5488241616dSLuigi Rizzo uint32_t index = 0; /* buffer index */ 549ccdc3305SLuigi Rizzo 550ccdc3305SLuigi Rizzo for (i = 0; i < n; i++) { 551ce3ee1e7SLuigi Rizzo void *vaddr = netmap_buf_malloc(nmd, &pos, &index); 552ccdc3305SLuigi Rizzo if (vaddr == NULL) { 553f9790aebSLuigi Rizzo D("no more buffers after %d of %d", i, n); 554ccdc3305SLuigi Rizzo goto cleanup; 555ccdc3305SLuigi Rizzo } 5568241616dSLuigi Rizzo slot[i].buf_idx = index; 557ccdc3305SLuigi Rizzo slot[i].len = p->_objsize; 558f9790aebSLuigi Rizzo slot[i].flags = 0; 559ccdc3305SLuigi Rizzo } 560ccdc3305SLuigi Rizzo 5618241616dSLuigi Rizzo ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos); 5628241616dSLuigi Rizzo return (0); 563ccdc3305SLuigi Rizzo 564ccdc3305SLuigi Rizzo cleanup: 5654cf8455fSEd Maste while (i > 0) { 5664cf8455fSEd Maste i--; 5678241616dSLuigi Rizzo netmap_obj_free(p, slot[i].buf_idx); 568ccdc3305SLuigi Rizzo } 5698241616dSLuigi Rizzo bzero(slot, n * sizeof(slot[0])); 5708241616dSLuigi Rizzo return (ENOMEM); 571ccdc3305SLuigi Rizzo } 572ccdc3305SLuigi Rizzo 573*f0ea3689SLuigi Rizzo static void 574*f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index) 575*f0ea3689SLuigi Rizzo { 576*f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 577*f0ea3689SLuigi Rizzo u_int i; 578*f0ea3689SLuigi Rizzo 579*f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 580*f0ea3689SLuigi Rizzo slot[i].buf_idx = index; 581*f0ea3689SLuigi Rizzo slot[i].len = p->_objsize; 582*f0ea3689SLuigi Rizzo slot[i].flags = 0; 583*f0ea3689SLuigi Rizzo } 584*f0ea3689SLuigi Rizzo } 585*f0ea3689SLuigi Rizzo 586ccdc3305SLuigi Rizzo 587ccdc3305SLuigi Rizzo static void 588f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i) 589ccdc3305SLuigi Rizzo { 590ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 5918241616dSLuigi Rizzo 592ccdc3305SLuigi Rizzo if (i < 2 || i >= p->objtotal) { 593ccdc3305SLuigi Rizzo D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal); 594ccdc3305SLuigi Rizzo return; 595ccdc3305SLuigi Rizzo } 5968241616dSLuigi Rizzo netmap_obj_free(p, i); 597ccdc3305SLuigi Rizzo } 598ccdc3305SLuigi Rizzo 599*f0ea3689SLuigi Rizzo 600*f0ea3689SLuigi Rizzo static void 601*f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 602*f0ea3689SLuigi Rizzo { 603*f0ea3689SLuigi Rizzo u_int i; 604*f0ea3689SLuigi Rizzo 605*f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 606*f0ea3689SLuigi Rizzo if (slot[i].buf_idx > 2) 607*f0ea3689SLuigi Rizzo netmap_free_buf(nmd, slot[i].buf_idx); 608*f0ea3689SLuigi Rizzo } 609*f0ea3689SLuigi Rizzo } 610*f0ea3689SLuigi Rizzo 6118241616dSLuigi Rizzo static void 6128241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p) 6138241616dSLuigi Rizzo { 614ce3ee1e7SLuigi Rizzo 6158241616dSLuigi Rizzo if (p == NULL) 6168241616dSLuigi Rizzo return; 6178241616dSLuigi Rizzo if (p->bitmap) 6188241616dSLuigi Rizzo free(p->bitmap, M_NETMAP); 6198241616dSLuigi Rizzo p->bitmap = NULL; 6208241616dSLuigi Rizzo if (p->lut) { 621ce3ee1e7SLuigi Rizzo u_int i; 622ce3ee1e7SLuigi Rizzo size_t sz = p->_clustsize; 623ce3ee1e7SLuigi Rizzo 624ce3ee1e7SLuigi Rizzo for (i = 0; i < p->objtotal; i += p->_clustentries) { 6258241616dSLuigi Rizzo if (p->lut[i].vaddr) 626ce3ee1e7SLuigi Rizzo contigfree(p->lut[i].vaddr, sz, M_NETMAP); 6278241616dSLuigi Rizzo } 6288241616dSLuigi Rizzo bzero(p->lut, sizeof(struct lut_entry) * p->objtotal); 6298241616dSLuigi Rizzo #ifdef linux 6308241616dSLuigi Rizzo vfree(p->lut); 6318241616dSLuigi Rizzo #else 6328241616dSLuigi Rizzo free(p->lut, M_NETMAP); 6338241616dSLuigi Rizzo #endif 6348241616dSLuigi Rizzo } 6358241616dSLuigi Rizzo p->lut = NULL; 636ce3ee1e7SLuigi Rizzo p->objtotal = 0; 637ce3ee1e7SLuigi Rizzo p->memtotal = 0; 638ce3ee1e7SLuigi Rizzo p->numclusters = 0; 639ce3ee1e7SLuigi Rizzo p->objfree = 0; 6408241616dSLuigi Rizzo } 641ccdc3305SLuigi Rizzo 642ccdc3305SLuigi Rizzo /* 643ccdc3305SLuigi Rizzo * Free all resources related to an allocator. 644ccdc3305SLuigi Rizzo */ 645ccdc3305SLuigi Rizzo static void 646ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p) 647ccdc3305SLuigi Rizzo { 648ccdc3305SLuigi Rizzo if (p == NULL) 649ccdc3305SLuigi Rizzo return; 6508241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 651ccdc3305SLuigi Rizzo } 652ccdc3305SLuigi Rizzo 653ccdc3305SLuigi Rizzo /* 654ccdc3305SLuigi Rizzo * We receive a request for objtotal objects, of size objsize each. 655ccdc3305SLuigi Rizzo * Internally we may round up both numbers, as we allocate objects 656ccdc3305SLuigi Rizzo * in small clusters multiple of the page size. 657ce3ee1e7SLuigi Rizzo * We need to keep track of objtotal and clustentries, 658ccdc3305SLuigi Rizzo * as they are needed when freeing memory. 659ccdc3305SLuigi Rizzo * 660ccdc3305SLuigi Rizzo * XXX note -- userspace needs the buffers to be contiguous, 661ccdc3305SLuigi Rizzo * so we cannot afford gaps at the end of a cluster. 662ccdc3305SLuigi Rizzo */ 6638241616dSLuigi Rizzo 6648241616dSLuigi Rizzo 6658241616dSLuigi Rizzo /* call with NMA_LOCK held */ 6668241616dSLuigi Rizzo static int 6678241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize) 668ccdc3305SLuigi Rizzo { 669ce3ee1e7SLuigi Rizzo int i; 670ccdc3305SLuigi Rizzo u_int clustsize; /* the cluster size, multiple of page size */ 671ccdc3305SLuigi Rizzo u_int clustentries; /* how many objects per entry */ 672ccdc3305SLuigi Rizzo 673ce3ee1e7SLuigi Rizzo /* we store the current request, so we can 674ce3ee1e7SLuigi Rizzo * detect configuration changes later */ 675ce3ee1e7SLuigi Rizzo p->r_objtotal = objtotal; 676ce3ee1e7SLuigi Rizzo p->r_objsize = objsize; 677ce3ee1e7SLuigi Rizzo 678ccdc3305SLuigi Rizzo #define MAX_CLUSTSIZE (1<<17) 67917885a7bSLuigi Rizzo #define LINE_ROUND NM_CACHE_ALIGN // 64 680ccdc3305SLuigi Rizzo if (objsize >= MAX_CLUSTSIZE) { 681ccdc3305SLuigi Rizzo /* we could do it but there is no point */ 682ccdc3305SLuigi Rizzo D("unsupported allocation for %d bytes", objsize); 683ce3ee1e7SLuigi Rizzo return EINVAL; 684ccdc3305SLuigi Rizzo } 685ccdc3305SLuigi Rizzo /* make sure objsize is a multiple of LINE_ROUND */ 686ccdc3305SLuigi Rizzo i = (objsize & (LINE_ROUND - 1)); 687ccdc3305SLuigi Rizzo if (i) { 688ccdc3305SLuigi Rizzo D("XXX aligning object by %d bytes", LINE_ROUND - i); 689ccdc3305SLuigi Rizzo objsize += LINE_ROUND - i; 690ccdc3305SLuigi Rizzo } 6918241616dSLuigi Rizzo if (objsize < p->objminsize || objsize > p->objmaxsize) { 6928241616dSLuigi Rizzo D("requested objsize %d out of range [%d, %d]", 6938241616dSLuigi Rizzo objsize, p->objminsize, p->objmaxsize); 694ce3ee1e7SLuigi Rizzo return EINVAL; 6958241616dSLuigi Rizzo } 6968241616dSLuigi Rizzo if (objtotal < p->nummin || objtotal > p->nummax) { 6978241616dSLuigi Rizzo D("requested objtotal %d out of range [%d, %d]", 6988241616dSLuigi Rizzo objtotal, p->nummin, p->nummax); 699ce3ee1e7SLuigi Rizzo return EINVAL; 7008241616dSLuigi Rizzo } 701ccdc3305SLuigi Rizzo /* 702ccdc3305SLuigi Rizzo * Compute number of objects using a brute-force approach: 703ccdc3305SLuigi Rizzo * given a max cluster size, 704ccdc3305SLuigi Rizzo * we try to fill it with objects keeping track of the 705ccdc3305SLuigi Rizzo * wasted space to the next page boundary. 706ccdc3305SLuigi Rizzo */ 707ccdc3305SLuigi Rizzo for (clustentries = 0, i = 1;; i++) { 708ccdc3305SLuigi Rizzo u_int delta, used = i * objsize; 709ccdc3305SLuigi Rizzo if (used > MAX_CLUSTSIZE) 710ccdc3305SLuigi Rizzo break; 711ccdc3305SLuigi Rizzo delta = used % PAGE_SIZE; 712ccdc3305SLuigi Rizzo if (delta == 0) { // exact solution 713ccdc3305SLuigi Rizzo clustentries = i; 714ccdc3305SLuigi Rizzo break; 715ccdc3305SLuigi Rizzo } 716ccdc3305SLuigi Rizzo if (delta > ( (clustentries*objsize) % PAGE_SIZE) ) 717ccdc3305SLuigi Rizzo clustentries = i; 718ccdc3305SLuigi Rizzo } 719ccdc3305SLuigi Rizzo // D("XXX --- ouch, delta %d (bad for buffers)", delta); 720ccdc3305SLuigi Rizzo /* compute clustsize and round to the next page */ 721ccdc3305SLuigi Rizzo clustsize = clustentries * objsize; 722ccdc3305SLuigi Rizzo i = (clustsize & (PAGE_SIZE - 1)); 723ccdc3305SLuigi Rizzo if (i) 724ccdc3305SLuigi Rizzo clustsize += PAGE_SIZE - i; 725ae10d1afSLuigi Rizzo if (netmap_verbose) 726ccdc3305SLuigi Rizzo D("objsize %d clustsize %d objects %d", 727ccdc3305SLuigi Rizzo objsize, clustsize, clustentries); 728ccdc3305SLuigi Rizzo 729ccdc3305SLuigi Rizzo /* 730ccdc3305SLuigi Rizzo * The number of clusters is n = ceil(objtotal/clustentries) 731ccdc3305SLuigi Rizzo * objtotal' = n * clustentries 732ccdc3305SLuigi Rizzo */ 733ce3ee1e7SLuigi Rizzo p->_clustentries = clustentries; 734ccdc3305SLuigi Rizzo p->_clustsize = clustsize; 735ce3ee1e7SLuigi Rizzo p->_numclusters = (objtotal + clustentries - 1) / clustentries; 736ce3ee1e7SLuigi Rizzo 737ce3ee1e7SLuigi Rizzo /* actual values (may be larger than requested) */ 7388241616dSLuigi Rizzo p->_objsize = objsize; 739ce3ee1e7SLuigi Rizzo p->_objtotal = p->_numclusters * clustentries; 740ccdc3305SLuigi Rizzo 7418241616dSLuigi Rizzo return 0; 7428241616dSLuigi Rizzo } 7438241616dSLuigi Rizzo 7448241616dSLuigi Rizzo 7458241616dSLuigi Rizzo /* call with NMA_LOCK held */ 7468241616dSLuigi Rizzo static int 7478241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p) 7488241616dSLuigi Rizzo { 749ce3ee1e7SLuigi Rizzo int i; /* must be signed */ 750ce3ee1e7SLuigi Rizzo size_t n; 751ce3ee1e7SLuigi Rizzo 752ce3ee1e7SLuigi Rizzo /* optimistically assume we have enough memory */ 753ce3ee1e7SLuigi Rizzo p->numclusters = p->_numclusters; 754ce3ee1e7SLuigi Rizzo p->objtotal = p->_objtotal; 7558241616dSLuigi Rizzo 7568241616dSLuigi Rizzo n = sizeof(struct lut_entry) * p->objtotal; 7578241616dSLuigi Rizzo #ifdef linux 7588241616dSLuigi Rizzo p->lut = vmalloc(n); 7598241616dSLuigi Rizzo #else 760d2b91851SEd Maste p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO); 7618241616dSLuigi Rizzo #endif 762ccdc3305SLuigi Rizzo if (p->lut == NULL) { 763ce3ee1e7SLuigi Rizzo D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name); 764ccdc3305SLuigi Rizzo goto clean; 765ccdc3305SLuigi Rizzo } 766ccdc3305SLuigi Rizzo 767ccdc3305SLuigi Rizzo /* Allocate the bitmap */ 768ccdc3305SLuigi Rizzo n = (p->objtotal + 31) / 32; 769d2b91851SEd Maste p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO); 770ccdc3305SLuigi Rizzo if (p->bitmap == NULL) { 771ce3ee1e7SLuigi Rizzo D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n, 7728241616dSLuigi Rizzo p->name); 773ccdc3305SLuigi Rizzo goto clean; 774ccdc3305SLuigi Rizzo } 7758241616dSLuigi Rizzo p->bitmap_slots = n; 776ccdc3305SLuigi Rizzo 777ccdc3305SLuigi Rizzo /* 778ccdc3305SLuigi Rizzo * Allocate clusters, init pointers and bitmap 779ccdc3305SLuigi Rizzo */ 780ce3ee1e7SLuigi Rizzo 781ce3ee1e7SLuigi Rizzo n = p->_clustsize; 782ce3ee1e7SLuigi Rizzo for (i = 0; i < (int)p->objtotal;) { 783ce3ee1e7SLuigi Rizzo int lim = i + p->_clustentries; 784ccdc3305SLuigi Rizzo char *clust; 785ccdc3305SLuigi Rizzo 786ce3ee1e7SLuigi Rizzo clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO, 787ce3ee1e7SLuigi Rizzo (size_t)0, -1UL, PAGE_SIZE, 0); 788ccdc3305SLuigi Rizzo if (clust == NULL) { 789ccdc3305SLuigi Rizzo /* 790ccdc3305SLuigi Rizzo * If we get here, there is a severe memory shortage, 791ccdc3305SLuigi Rizzo * so halve the allocated memory to reclaim some. 792ccdc3305SLuigi Rizzo */ 793ccdc3305SLuigi Rizzo D("Unable to create cluster at %d for '%s' allocator", 7948241616dSLuigi Rizzo i, p->name); 795ce3ee1e7SLuigi Rizzo if (i < 2) /* nothing to halve */ 796ce3ee1e7SLuigi Rizzo goto out; 797ccdc3305SLuigi Rizzo lim = i / 2; 7988241616dSLuigi Rizzo for (i--; i >= lim; i--) { 799ccdc3305SLuigi Rizzo p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) ); 800ce3ee1e7SLuigi Rizzo if (i % p->_clustentries == 0 && p->lut[i].vaddr) 801ccdc3305SLuigi Rizzo contigfree(p->lut[i].vaddr, 802ce3ee1e7SLuigi Rizzo n, M_NETMAP); 803ccdc3305SLuigi Rizzo } 804ce3ee1e7SLuigi Rizzo out: 805ccdc3305SLuigi Rizzo p->objtotal = i; 806ce3ee1e7SLuigi Rizzo /* we may have stopped in the middle of a cluster */ 807ce3ee1e7SLuigi Rizzo p->numclusters = (i + p->_clustentries - 1) / p->_clustentries; 808ccdc3305SLuigi Rizzo break; 809ccdc3305SLuigi Rizzo } 8108241616dSLuigi Rizzo for (; i < lim; i++, clust += p->_objsize) { 811ccdc3305SLuigi Rizzo p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) ); 812ccdc3305SLuigi Rizzo p->lut[i].vaddr = clust; 813ccdc3305SLuigi Rizzo p->lut[i].paddr = vtophys(clust); 814ccdc3305SLuigi Rizzo } 815ccdc3305SLuigi Rizzo } 816ce3ee1e7SLuigi Rizzo p->objfree = p->objtotal; 817ce3ee1e7SLuigi Rizzo p->memtotal = p->numclusters * p->_clustsize; 818ce3ee1e7SLuigi Rizzo if (p->objfree == 0) 819ce3ee1e7SLuigi Rizzo goto clean; 820ae10d1afSLuigi Rizzo if (netmap_verbose) 821ccdc3305SLuigi Rizzo D("Pre-allocated %d clusters (%d/%dKB) for '%s'", 822ce3ee1e7SLuigi Rizzo p->numclusters, p->_clustsize >> 10, 823ce3ee1e7SLuigi Rizzo p->memtotal >> 10, p->name); 824ccdc3305SLuigi Rizzo 8258241616dSLuigi Rizzo return 0; 826ccdc3305SLuigi Rizzo 827ccdc3305SLuigi Rizzo clean: 8288241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 8298241616dSLuigi Rizzo return ENOMEM; 8308241616dSLuigi Rizzo } 8318241616dSLuigi Rizzo 8328241616dSLuigi Rizzo /* call with lock held */ 8338241616dSLuigi Rizzo static int 834ce3ee1e7SLuigi Rizzo netmap_memory_config_changed(struct netmap_mem_d *nmd) 8358241616dSLuigi Rizzo { 8368241616dSLuigi Rizzo int i; 8378241616dSLuigi Rizzo 8388241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 839ce3ee1e7SLuigi Rizzo if (nmd->pools[i].r_objsize != netmap_params[i].size || 840ce3ee1e7SLuigi Rizzo nmd->pools[i].r_objtotal != netmap_params[i].num) 8418241616dSLuigi Rizzo return 1; 8428241616dSLuigi Rizzo } 8438241616dSLuigi Rizzo return 0; 8448241616dSLuigi Rizzo } 8458241616dSLuigi Rizzo 846ce3ee1e7SLuigi Rizzo static void 847ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd) 848ce3ee1e7SLuigi Rizzo { 849ce3ee1e7SLuigi Rizzo int i; 850*f0ea3689SLuigi Rizzo 851*f0ea3689SLuigi Rizzo if (netmap_verbose) 852ce3ee1e7SLuigi Rizzo D("resetting %p", nmd); 853ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 854ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 855ce3ee1e7SLuigi Rizzo } 856ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 857ce3ee1e7SLuigi Rizzo } 858ce3ee1e7SLuigi Rizzo 859ce3ee1e7SLuigi Rizzo static int 860ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd) 861ce3ee1e7SLuigi Rizzo { 862ce3ee1e7SLuigi Rizzo int i; 863ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 864ce3ee1e7SLuigi Rizzo return 0; 865ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 866ce3ee1e7SLuigi Rizzo nmd->nm_totalsize = 0; 867ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 868ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]); 869ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 870ce3ee1e7SLuigi Rizzo goto error; 871ce3ee1e7SLuigi Rizzo nmd->nm_totalsize += nmd->pools[i].memtotal; 872ce3ee1e7SLuigi Rizzo } 873ce3ee1e7SLuigi Rizzo /* buffers 0 and 1 are reserved */ 874ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].objfree -= 2; 875ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3; 876ce3ee1e7SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 877ce3ee1e7SLuigi Rizzo 878*f0ea3689SLuigi Rizzo if (netmap_verbose) 879*f0ea3689SLuigi Rizzo D("interfaces %d KB, rings %d KB, buffers %d MB", 880ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_IF_POOL].memtotal >> 10, 881ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_RING_POOL].memtotal >> 10, 882ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].memtotal >> 20); 883ce3ee1e7SLuigi Rizzo 884*f0ea3689SLuigi Rizzo if (netmap_verbose) 885ce3ee1e7SLuigi Rizzo D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree); 886ce3ee1e7SLuigi Rizzo 887ce3ee1e7SLuigi Rizzo 888ce3ee1e7SLuigi Rizzo return 0; 889ce3ee1e7SLuigi Rizzo error: 890ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(nmd); 891ce3ee1e7SLuigi Rizzo return nmd->lasterr; 892ce3ee1e7SLuigi Rizzo } 893ce3ee1e7SLuigi Rizzo 894ce3ee1e7SLuigi Rizzo 895ce3ee1e7SLuigi Rizzo 896ce3ee1e7SLuigi Rizzo void 897ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(struct netmap_mem_d *nmd) 898ce3ee1e7SLuigi Rizzo { 899ce3ee1e7SLuigi Rizzo if (nmd == NULL) 900ce3ee1e7SLuigi Rizzo return; 901*f0ea3689SLuigi Rizzo if (netmap_verbose) 902ce3ee1e7SLuigi Rizzo D("deleting %p", nmd); 903ce3ee1e7SLuigi Rizzo if (nmd->refcount > 0) 904ce3ee1e7SLuigi Rizzo D("bug: deleting mem allocator with refcount=%d!", nmd->refcount); 905*f0ea3689SLuigi Rizzo nm_mem_release_id(nmd); 906*f0ea3689SLuigi Rizzo if (netmap_verbose) 907ce3ee1e7SLuigi Rizzo D("done deleting %p", nmd); 908ce3ee1e7SLuigi Rizzo NMA_LOCK_DESTROY(nmd); 909ce3ee1e7SLuigi Rizzo free(nmd, M_DEVBUF); 910ce3ee1e7SLuigi Rizzo } 911ce3ee1e7SLuigi Rizzo 912ce3ee1e7SLuigi Rizzo static int 913ce3ee1e7SLuigi Rizzo netmap_mem_private_config(struct netmap_mem_d *nmd) 914ce3ee1e7SLuigi Rizzo { 915ce3ee1e7SLuigi Rizzo /* nothing to do, we are configured on creation 916ce3ee1e7SLuigi Rizzo * and configuration never changes thereafter 917ce3ee1e7SLuigi Rizzo */ 918ce3ee1e7SLuigi Rizzo return 0; 919ce3ee1e7SLuigi Rizzo } 920ce3ee1e7SLuigi Rizzo 921ce3ee1e7SLuigi Rizzo static int 922ce3ee1e7SLuigi Rizzo netmap_mem_private_finalize(struct netmap_mem_d *nmd) 923ce3ee1e7SLuigi Rizzo { 924ce3ee1e7SLuigi Rizzo int err; 925ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 926ce3ee1e7SLuigi Rizzo nmd->refcount++; 927ce3ee1e7SLuigi Rizzo err = netmap_mem_finalize_all(nmd); 928ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 929ce3ee1e7SLuigi Rizzo return err; 930ce3ee1e7SLuigi Rizzo 931ce3ee1e7SLuigi Rizzo } 932ce3ee1e7SLuigi Rizzo 933f9790aebSLuigi Rizzo static void 934f9790aebSLuigi Rizzo netmap_mem_private_deref(struct netmap_mem_d *nmd) 935ce3ee1e7SLuigi Rizzo { 936ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 937ce3ee1e7SLuigi Rizzo if (--nmd->refcount <= 0) 938ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(nmd); 939ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 940ce3ee1e7SLuigi Rizzo } 941ce3ee1e7SLuigi Rizzo 942*f0ea3689SLuigi Rizzo 943*f0ea3689SLuigi Rizzo /* 944*f0ea3689SLuigi Rizzo * allocator for private memory 945*f0ea3689SLuigi Rizzo */ 946ce3ee1e7SLuigi Rizzo struct netmap_mem_d * 947*f0ea3689SLuigi Rizzo netmap_mem_private_new(const char *name, u_int txr, u_int txd, 948*f0ea3689SLuigi Rizzo u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr) 949ce3ee1e7SLuigi Rizzo { 950ce3ee1e7SLuigi Rizzo struct netmap_mem_d *d = NULL; 951ce3ee1e7SLuigi Rizzo struct netmap_obj_params p[NETMAP_POOLS_NR]; 952*f0ea3689SLuigi Rizzo int i, err; 953*f0ea3689SLuigi Rizzo u_int v, maxd; 954ce3ee1e7SLuigi Rizzo 955ce3ee1e7SLuigi Rizzo d = malloc(sizeof(struct netmap_mem_d), 956ce3ee1e7SLuigi Rizzo M_DEVBUF, M_NOWAIT | M_ZERO); 957*f0ea3689SLuigi Rizzo if (d == NULL) { 958*f0ea3689SLuigi Rizzo err = ENOMEM; 959*f0ea3689SLuigi Rizzo goto error; 960*f0ea3689SLuigi Rizzo } 961ce3ee1e7SLuigi Rizzo 962ce3ee1e7SLuigi Rizzo *d = nm_blueprint; 963ce3ee1e7SLuigi Rizzo 964*f0ea3689SLuigi Rizzo err = nm_mem_assign_id(d); 965*f0ea3689SLuigi Rizzo if (err) 966*f0ea3689SLuigi Rizzo goto error; 967*f0ea3689SLuigi Rizzo 968*f0ea3689SLuigi Rizzo /* account for the fake host rings */ 969ce3ee1e7SLuigi Rizzo txr++; 970ce3ee1e7SLuigi Rizzo rxr++; 971ce3ee1e7SLuigi Rizzo 972*f0ea3689SLuigi Rizzo /* copy the min values */ 973*f0ea3689SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 974*f0ea3689SLuigi Rizzo p[i] = netmap_min_priv_params[i]; 975*f0ea3689SLuigi Rizzo } 976*f0ea3689SLuigi Rizzo 977*f0ea3689SLuigi Rizzo /* possibly increase them to fit user request */ 978*f0ea3689SLuigi Rizzo v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr); 979*f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].size < v) 980*f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].size = v; 981*f0ea3689SLuigi Rizzo v = 2 + 4 * npipes; 982*f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].num < v) 983*f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].num = v; 984*f0ea3689SLuigi Rizzo maxd = (txd > rxd) ? txd : rxd; 985*f0ea3689SLuigi Rizzo v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd; 986*f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].size < v) 987*f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].size = v; 988*f0ea3689SLuigi Rizzo /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake) 989*f0ea3689SLuigi Rizzo * and two rx rings (again, 1 normal and 1 fake host) 990*f0ea3689SLuigi Rizzo */ 991*f0ea3689SLuigi Rizzo v = txr + rxr + 8 * npipes; 992*f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].num < v) 993*f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].num = v; 994*f0ea3689SLuigi Rizzo /* for each pipe we only need the buffers for the 4 "real" rings. 995*f0ea3689SLuigi Rizzo * On the other end, the pipe ring dimension may be different from 996*f0ea3689SLuigi Rizzo * the parent port ring dimension. As a compromise, we allocate twice the 997*f0ea3689SLuigi Rizzo * space actually needed if the pipe rings were the same size as the parent rings 998*f0ea3689SLuigi Rizzo */ 999*f0ea3689SLuigi Rizzo v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs; 1000*f0ea3689SLuigi Rizzo /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */ 1001*f0ea3689SLuigi Rizzo if (p[NETMAP_BUF_POOL].num < v) 1002*f0ea3689SLuigi Rizzo p[NETMAP_BUF_POOL].num = v; 1003*f0ea3689SLuigi Rizzo 1004*f0ea3689SLuigi Rizzo if (netmap_verbose) 1005ce3ee1e7SLuigi Rizzo D("req if %d*%d ring %d*%d buf %d*%d", 1006ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].num, 1007ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].size, 1008ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].num, 1009ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].size, 1010ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].num, 1011ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].size); 1012ce3ee1e7SLuigi Rizzo 1013ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1014ce3ee1e7SLuigi Rizzo snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ, 1015ce3ee1e7SLuigi Rizzo nm_blueprint.pools[i].name, 1016ce3ee1e7SLuigi Rizzo name); 1017*f0ea3689SLuigi Rizzo err = netmap_config_obj_allocator(&d->pools[i], 1018*f0ea3689SLuigi Rizzo p[i].num, p[i].size); 1019*f0ea3689SLuigi Rizzo if (err) 1020ce3ee1e7SLuigi Rizzo goto error; 1021ce3ee1e7SLuigi Rizzo } 1022ce3ee1e7SLuigi Rizzo 1023ce3ee1e7SLuigi Rizzo d->flags &= ~NETMAP_MEM_FINALIZED; 1024ce3ee1e7SLuigi Rizzo 1025ce3ee1e7SLuigi Rizzo NMA_LOCK_INIT(d); 1026ce3ee1e7SLuigi Rizzo 1027ce3ee1e7SLuigi Rizzo return d; 1028ce3ee1e7SLuigi Rizzo error: 1029ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(d); 1030*f0ea3689SLuigi Rizzo if (perr) 1031*f0ea3689SLuigi Rizzo *perr = err; 1032ce3ee1e7SLuigi Rizzo return NULL; 1033ce3ee1e7SLuigi Rizzo } 1034ce3ee1e7SLuigi Rizzo 10358241616dSLuigi Rizzo 10368241616dSLuigi Rizzo /* call with lock held */ 10378241616dSLuigi Rizzo static int 1038ce3ee1e7SLuigi Rizzo netmap_mem_global_config(struct netmap_mem_d *nmd) 10398241616dSLuigi Rizzo { 10408241616dSLuigi Rizzo int i; 10418241616dSLuigi Rizzo 1042ce3ee1e7SLuigi Rizzo if (nmd->refcount) 1043ce3ee1e7SLuigi Rizzo /* already in use, we cannot change the configuration */ 1044ce3ee1e7SLuigi Rizzo goto out; 1045ce3ee1e7SLuigi Rizzo 1046ce3ee1e7SLuigi Rizzo if (!netmap_memory_config_changed(nmd)) 10478241616dSLuigi Rizzo goto out; 10488241616dSLuigi Rizzo 10498241616dSLuigi Rizzo D("reconfiguring"); 10508241616dSLuigi Rizzo 1051ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 10528241616dSLuigi Rizzo /* reset previous allocation */ 10538241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1054ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 10558241616dSLuigi Rizzo } 1056ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 10578241616dSLuigi Rizzo } 10588241616dSLuigi Rizzo 10598241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1060ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i], 10618241616dSLuigi Rizzo netmap_params[i].num, netmap_params[i].size); 1062ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 10638241616dSLuigi Rizzo goto out; 10648241616dSLuigi Rizzo } 10658241616dSLuigi Rizzo 10668241616dSLuigi Rizzo out: 10678241616dSLuigi Rizzo 1068ce3ee1e7SLuigi Rizzo return nmd->lasterr; 10698241616dSLuigi Rizzo } 10708241616dSLuigi Rizzo 10718241616dSLuigi Rizzo static int 1072ce3ee1e7SLuigi Rizzo netmap_mem_global_finalize(struct netmap_mem_d *nmd) 10738241616dSLuigi Rizzo { 1074ce3ee1e7SLuigi Rizzo int err; 10758241616dSLuigi Rizzo 1076ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 1077ce3ee1e7SLuigi Rizzo 10788241616dSLuigi Rizzo 10798241616dSLuigi Rizzo /* update configuration if changed */ 1080ce3ee1e7SLuigi Rizzo if (netmap_mem_global_config(nmd)) 10818241616dSLuigi Rizzo goto out; 10828241616dSLuigi Rizzo 1083ce3ee1e7SLuigi Rizzo nmd->refcount++; 1084ce3ee1e7SLuigi Rizzo 1085ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 10868241616dSLuigi Rizzo /* may happen if config is not changed */ 10878241616dSLuigi Rizzo ND("nothing to do"); 10888241616dSLuigi Rizzo goto out; 10898241616dSLuigi Rizzo } 10908241616dSLuigi Rizzo 1091ce3ee1e7SLuigi Rizzo if (netmap_mem_finalize_all(nmd)) 1092ce3ee1e7SLuigi Rizzo goto out; 10938241616dSLuigi Rizzo 10948241616dSLuigi Rizzo /* backward compatibility */ 1095ce3ee1e7SLuigi Rizzo netmap_buf_size = nmd->pools[NETMAP_BUF_POOL]._objsize; 1096ce3ee1e7SLuigi Rizzo netmap_total_buffers = nmd->pools[NETMAP_BUF_POOL].objtotal; 10978241616dSLuigi Rizzo 1098ce3ee1e7SLuigi Rizzo netmap_buffer_lut = nmd->pools[NETMAP_BUF_POOL].lut; 1099ce3ee1e7SLuigi Rizzo netmap_buffer_base = nmd->pools[NETMAP_BUF_POOL].lut[0].vaddr; 11008241616dSLuigi Rizzo 1101ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 11028241616dSLuigi Rizzo 11038241616dSLuigi Rizzo out: 1104ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 1105ce3ee1e7SLuigi Rizzo nmd->refcount--; 1106ce3ee1e7SLuigi Rizzo err = nmd->lasterr; 11078241616dSLuigi Rizzo 1108ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 11098241616dSLuigi Rizzo 1110ce3ee1e7SLuigi Rizzo return err; 11118241616dSLuigi Rizzo 1112ccdc3305SLuigi Rizzo } 1113ccdc3305SLuigi Rizzo 1114ce3ee1e7SLuigi Rizzo int 1115ce3ee1e7SLuigi Rizzo netmap_mem_init(void) 1116ccdc3305SLuigi Rizzo { 1117ce3ee1e7SLuigi Rizzo NMA_LOCK_INIT(&nm_mem); 11188241616dSLuigi Rizzo return (0); 1119ccdc3305SLuigi Rizzo } 1120ccdc3305SLuigi Rizzo 1121ce3ee1e7SLuigi Rizzo void 1122ce3ee1e7SLuigi Rizzo netmap_mem_fini(void) 1123ccdc3305SLuigi Rizzo { 11248241616dSLuigi Rizzo int i; 11258241616dSLuigi Rizzo 11268241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 11278241616dSLuigi Rizzo netmap_destroy_obj_allocator(&nm_mem.pools[i]); 11288241616dSLuigi Rizzo } 1129ce3ee1e7SLuigi Rizzo NMA_LOCK_DESTROY(&nm_mem); 11308241616dSLuigi Rizzo } 11318241616dSLuigi Rizzo 11328241616dSLuigi Rizzo static void 11338241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na) 11348241616dSLuigi Rizzo { 1135*f0ea3689SLuigi Rizzo struct netmap_kring *kring; 1136*f0ea3689SLuigi Rizzo struct netmap_ring *ring; 1137ae10d1afSLuigi Rizzo if (!na->tx_rings) 1138ae10d1afSLuigi Rizzo return; 1139*f0ea3689SLuigi Rizzo for (kring = na->tx_rings; kring != na->rx_rings; kring++) { 1140*f0ea3689SLuigi Rizzo ring = kring->ring; 1141*f0ea3689SLuigi Rizzo if (ring == NULL) 1142*f0ea3689SLuigi Rizzo continue; 1143*f0ea3689SLuigi Rizzo netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots); 1144*f0ea3689SLuigi Rizzo netmap_ring_free(na->nm_mem, ring); 1145*f0ea3689SLuigi Rizzo kring->ring = NULL; 11468241616dSLuigi Rizzo } 1147*f0ea3689SLuigi Rizzo for (/* cont'd from above */; kring != na->tailroom; kring++) { 1148*f0ea3689SLuigi Rizzo ring = kring->ring; 1149*f0ea3689SLuigi Rizzo if (ring == NULL) 1150*f0ea3689SLuigi Rizzo continue; 1151*f0ea3689SLuigi Rizzo netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots); 1152*f0ea3689SLuigi Rizzo netmap_ring_free(na->nm_mem, ring); 1153*f0ea3689SLuigi Rizzo kring->ring = NULL; 1154ce3ee1e7SLuigi Rizzo } 1155ccdc3305SLuigi Rizzo } 1156ccdc3305SLuigi Rizzo 1157f9790aebSLuigi Rizzo /* call with NMA_LOCK held * 1158f9790aebSLuigi Rizzo * 1159f9790aebSLuigi Rizzo * Allocate netmap rings and buffers for this card 1160f9790aebSLuigi Rizzo * The rings are contiguous, but have variable size. 1161*f0ea3689SLuigi Rizzo * The kring array must follow the layout described 1162*f0ea3689SLuigi Rizzo * in netmap_krings_create(). 1163f9790aebSLuigi Rizzo */ 1164f9790aebSLuigi Rizzo int 1165f9790aebSLuigi Rizzo netmap_mem_rings_create(struct netmap_adapter *na) 1166f9790aebSLuigi Rizzo { 1167f9790aebSLuigi Rizzo struct netmap_ring *ring; 1168f9790aebSLuigi Rizzo u_int len, ndesc; 1169f9790aebSLuigi Rizzo struct netmap_kring *kring; 1170*f0ea3689SLuigi Rizzo u_int i; 1171f9790aebSLuigi Rizzo 1172f9790aebSLuigi Rizzo NMA_LOCK(na->nm_mem); 1173f9790aebSLuigi Rizzo 1174*f0ea3689SLuigi Rizzo /* transmit rings */ 1175*f0ea3689SLuigi Rizzo for (i =0, kring = na->tx_rings; kring != na->rx_rings; kring++, i++) { 1176*f0ea3689SLuigi Rizzo if (kring->ring) { 1177*f0ea3689SLuigi Rizzo ND("%s %ld already created", kring->name, kring - na->tx_rings); 1178*f0ea3689SLuigi Rizzo continue; /* already created by somebody else */ 1179*f0ea3689SLuigi Rizzo } 1180f9790aebSLuigi Rizzo ndesc = kring->nkr_num_slots; 1181f9790aebSLuigi Rizzo len = sizeof(struct netmap_ring) + 1182f9790aebSLuigi Rizzo ndesc * sizeof(struct netmap_slot); 1183f9790aebSLuigi Rizzo ring = netmap_ring_malloc(na->nm_mem, len); 1184f9790aebSLuigi Rizzo if (ring == NULL) { 1185f9790aebSLuigi Rizzo D("Cannot allocate tx_ring"); 1186f9790aebSLuigi Rizzo goto cleanup; 1187f9790aebSLuigi Rizzo } 1188f6c2a31fSLuigi Rizzo ND("txring at %p", ring); 1189f9790aebSLuigi Rizzo kring->ring = ring; 1190f9790aebSLuigi Rizzo *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc; 119117885a7bSLuigi Rizzo *(int64_t *)(uintptr_t)&ring->buf_ofs = 1192f9790aebSLuigi Rizzo (na->nm_mem->pools[NETMAP_IF_POOL].memtotal + 1193f9790aebSLuigi Rizzo na->nm_mem->pools[NETMAP_RING_POOL].memtotal) - 1194f9790aebSLuigi Rizzo netmap_ring_offset(na->nm_mem, ring); 1195f9790aebSLuigi Rizzo 119617885a7bSLuigi Rizzo /* copy values from kring */ 119717885a7bSLuigi Rizzo ring->head = kring->rhead; 119817885a7bSLuigi Rizzo ring->cur = kring->rcur; 119917885a7bSLuigi Rizzo ring->tail = kring->rtail; 1200f9790aebSLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->nr_buf_size = 1201f9790aebSLuigi Rizzo NETMAP_BDG_BUF_SIZE(na->nm_mem); 1202*f0ea3689SLuigi Rizzo ND("%s h %d c %d t %d", kring->name, 1203*f0ea3689SLuigi Rizzo ring->head, ring->cur, ring->tail); 1204f9790aebSLuigi Rizzo ND("initializing slots for txring"); 1205*f0ea3689SLuigi Rizzo if (i != na->num_tx_rings || (na->na_flags & NAF_HOST_RINGS)) { 1206*f0ea3689SLuigi Rizzo /* this is a real ring */ 1207f9790aebSLuigi Rizzo if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) { 1208f9790aebSLuigi Rizzo D("Cannot allocate buffers for tx_ring"); 1209f9790aebSLuigi Rizzo goto cleanup; 1210f9790aebSLuigi Rizzo } 1211*f0ea3689SLuigi Rizzo } else { 1212*f0ea3689SLuigi Rizzo /* this is a fake tx ring, set all indices to 0 */ 1213*f0ea3689SLuigi Rizzo netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0); 1214*f0ea3689SLuigi Rizzo } 1215f9790aebSLuigi Rizzo } 1216f9790aebSLuigi Rizzo 1217*f0ea3689SLuigi Rizzo /* receive rings */ 1218*f0ea3689SLuigi Rizzo for ( i = 0 /* kring cont'd from above */ ; kring != na->tailroom; kring++, i++) { 1219*f0ea3689SLuigi Rizzo if (kring->ring) { 1220*f0ea3689SLuigi Rizzo ND("%s %ld already created", kring->name, kring - na->rx_rings); 1221*f0ea3689SLuigi Rizzo continue; /* already created by somebody else */ 1222*f0ea3689SLuigi Rizzo } 1223f9790aebSLuigi Rizzo ndesc = kring->nkr_num_slots; 1224f9790aebSLuigi Rizzo len = sizeof(struct netmap_ring) + 1225f9790aebSLuigi Rizzo ndesc * sizeof(struct netmap_slot); 1226f9790aebSLuigi Rizzo ring = netmap_ring_malloc(na->nm_mem, len); 1227f9790aebSLuigi Rizzo if (ring == NULL) { 1228f9790aebSLuigi Rizzo D("Cannot allocate rx_ring"); 1229f9790aebSLuigi Rizzo goto cleanup; 1230f9790aebSLuigi Rizzo } 1231f6c2a31fSLuigi Rizzo ND("rxring at %p", ring); 1232f9790aebSLuigi Rizzo kring->ring = ring; 1233f9790aebSLuigi Rizzo *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc; 123417885a7bSLuigi Rizzo *(int64_t *)(uintptr_t)&ring->buf_ofs = 1235f9790aebSLuigi Rizzo (na->nm_mem->pools[NETMAP_IF_POOL].memtotal + 1236f9790aebSLuigi Rizzo na->nm_mem->pools[NETMAP_RING_POOL].memtotal) - 1237f9790aebSLuigi Rizzo netmap_ring_offset(na->nm_mem, ring); 1238f9790aebSLuigi Rizzo 123917885a7bSLuigi Rizzo /* copy values from kring */ 124017885a7bSLuigi Rizzo ring->head = kring->rhead; 124117885a7bSLuigi Rizzo ring->cur = kring->rcur; 124217885a7bSLuigi Rizzo ring->tail = kring->rtail; 1243f9790aebSLuigi Rizzo *(int *)(uintptr_t)&ring->nr_buf_size = 1244f9790aebSLuigi Rizzo NETMAP_BDG_BUF_SIZE(na->nm_mem); 1245*f0ea3689SLuigi Rizzo ND("%s h %d c %d t %d", kring->name, 1246*f0ea3689SLuigi Rizzo ring->head, ring->cur, ring->tail); 1247f6c2a31fSLuigi Rizzo ND("initializing slots for rxring %p", ring); 1248*f0ea3689SLuigi Rizzo if (i != na->num_rx_rings || (na->na_flags & NAF_HOST_RINGS)) { 1249*f0ea3689SLuigi Rizzo /* this is a real ring */ 1250f9790aebSLuigi Rizzo if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) { 1251f9790aebSLuigi Rizzo D("Cannot allocate buffers for rx_ring"); 1252f9790aebSLuigi Rizzo goto cleanup; 1253f9790aebSLuigi Rizzo } 1254*f0ea3689SLuigi Rizzo } else { 1255*f0ea3689SLuigi Rizzo /* this is a fake rx ring, set all indices to 1 */ 1256*f0ea3689SLuigi Rizzo netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 1); 1257*f0ea3689SLuigi Rizzo } 1258f9790aebSLuigi Rizzo } 1259f9790aebSLuigi Rizzo 1260f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1261f9790aebSLuigi Rizzo 1262f9790aebSLuigi Rizzo return 0; 1263f9790aebSLuigi Rizzo 1264f9790aebSLuigi Rizzo cleanup: 1265f9790aebSLuigi Rizzo netmap_free_rings(na); 1266f9790aebSLuigi Rizzo 1267f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1268f9790aebSLuigi Rizzo 1269f9790aebSLuigi Rizzo return ENOMEM; 1270f9790aebSLuigi Rizzo } 1271f9790aebSLuigi Rizzo 1272f9790aebSLuigi Rizzo void 1273f9790aebSLuigi Rizzo netmap_mem_rings_delete(struct netmap_adapter *na) 1274f9790aebSLuigi Rizzo { 1275f9790aebSLuigi Rizzo /* last instance, release bufs and rings */ 1276f9790aebSLuigi Rizzo NMA_LOCK(na->nm_mem); 1277f9790aebSLuigi Rizzo 1278f9790aebSLuigi Rizzo netmap_free_rings(na); 1279f9790aebSLuigi Rizzo 1280f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1281f9790aebSLuigi Rizzo } 1282ccdc3305SLuigi Rizzo 1283ccdc3305SLuigi Rizzo 12848241616dSLuigi Rizzo /* call with NMA_LOCK held */ 1285ae10d1afSLuigi Rizzo /* 1286ae10d1afSLuigi Rizzo * Allocate the per-fd structure netmap_if. 1287ce3ee1e7SLuigi Rizzo * 1288ce3ee1e7SLuigi Rizzo * We assume that the configuration stored in na 1289ce3ee1e7SLuigi Rizzo * (number of tx/rx rings and descs) does not change while 1290ce3ee1e7SLuigi Rizzo * the interface is in netmap mode. 1291ae10d1afSLuigi Rizzo */ 1292ce3ee1e7SLuigi Rizzo struct netmap_if * 1293ce3ee1e7SLuigi Rizzo netmap_mem_if_new(const char *ifname, struct netmap_adapter *na) 1294ccdc3305SLuigi Rizzo { 1295ccdc3305SLuigi Rizzo struct netmap_if *nifp; 1296ccdc3305SLuigi Rizzo ssize_t base; /* handy for relative offsets between rings and nifp */ 1297f9790aebSLuigi Rizzo u_int i, len, ntx, nrx; 1298ccdc3305SLuigi Rizzo 1299*f0ea3689SLuigi Rizzo /* account for the (eventually fake) host rings */ 1300*f0ea3689SLuigi Rizzo ntx = na->num_tx_rings + 1; 1301*f0ea3689SLuigi Rizzo nrx = na->num_rx_rings + 1; 1302ccdc3305SLuigi Rizzo /* 1303ccdc3305SLuigi Rizzo * the descriptor is followed inline by an array of offsets 1304ccdc3305SLuigi Rizzo * to the tx and rx rings in the shared memory region. 1305ccdc3305SLuigi Rizzo */ 1306ce3ee1e7SLuigi Rizzo 1307ce3ee1e7SLuigi Rizzo NMA_LOCK(na->nm_mem); 1308ce3ee1e7SLuigi Rizzo 1309ccdc3305SLuigi Rizzo len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t); 1310ce3ee1e7SLuigi Rizzo nifp = netmap_if_malloc(na->nm_mem, len); 1311ccdc3305SLuigi Rizzo if (nifp == NULL) { 1312ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1313ccdc3305SLuigi Rizzo return NULL; 1314ccdc3305SLuigi Rizzo } 1315ccdc3305SLuigi Rizzo 1316ccdc3305SLuigi Rizzo /* initialize base fields -- override const */ 1317ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings; 1318ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings; 1319ce3ee1e7SLuigi Rizzo strncpy(nifp->ni_name, ifname, (size_t)IFNAMSIZ); 1320ccdc3305SLuigi Rizzo 1321ccdc3305SLuigi Rizzo /* 1322ccdc3305SLuigi Rizzo * fill the slots for the rx and tx rings. They contain the offset 1323ccdc3305SLuigi Rizzo * between the ring and nifp, so the information is usable in 1324ccdc3305SLuigi Rizzo * userspace to reach the ring from the nifp. 1325ccdc3305SLuigi Rizzo */ 1326ce3ee1e7SLuigi Rizzo base = netmap_if_offset(na->nm_mem, nifp); 1327ccdc3305SLuigi Rizzo for (i = 0; i < ntx; i++) { 1328ccdc3305SLuigi Rizzo *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = 1329ce3ee1e7SLuigi Rizzo netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base; 1330ccdc3305SLuigi Rizzo } 1331ccdc3305SLuigi Rizzo for (i = 0; i < nrx; i++) { 1332ccdc3305SLuigi Rizzo *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] = 1333ce3ee1e7SLuigi Rizzo netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base; 1334ccdc3305SLuigi Rizzo } 1335ce3ee1e7SLuigi Rizzo 1336ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1337ce3ee1e7SLuigi Rizzo 1338ccdc3305SLuigi Rizzo return (nifp); 1339ccdc3305SLuigi Rizzo } 1340ccdc3305SLuigi Rizzo 1341ce3ee1e7SLuigi Rizzo void 1342ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 1343ccdc3305SLuigi Rizzo { 1344ce3ee1e7SLuigi Rizzo if (nifp == NULL) 1345ce3ee1e7SLuigi Rizzo /* nothing to do */ 1346ce3ee1e7SLuigi Rizzo return; 1347ce3ee1e7SLuigi Rizzo NMA_LOCK(na->nm_mem); 1348*f0ea3689SLuigi Rizzo if (nifp->ni_bufs_head) 1349*f0ea3689SLuigi Rizzo netmap_extra_free(na, nifp->ni_bufs_head); 1350ce3ee1e7SLuigi Rizzo netmap_if_free(na->nm_mem, nifp); 1351ce3ee1e7SLuigi Rizzo 1352ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1353ce3ee1e7SLuigi Rizzo } 1354ce3ee1e7SLuigi Rizzo 1355ce3ee1e7SLuigi Rizzo static void 1356ce3ee1e7SLuigi Rizzo netmap_mem_global_deref(struct netmap_mem_d *nmd) 1357ce3ee1e7SLuigi Rizzo { 1358ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 1359ce3ee1e7SLuigi Rizzo 1360ce3ee1e7SLuigi Rizzo nmd->refcount--; 1361ae10d1afSLuigi Rizzo if (netmap_verbose) 1362ce3ee1e7SLuigi Rizzo D("refcount = %d", nmd->refcount); 1363ce3ee1e7SLuigi Rizzo 1364ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 1365ce3ee1e7SLuigi Rizzo } 1366ce3ee1e7SLuigi Rizzo 1367f9790aebSLuigi Rizzo int 1368f9790aebSLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd) 1369ce3ee1e7SLuigi Rizzo { 1370ce3ee1e7SLuigi Rizzo return nmd->finalize(nmd); 1371ce3ee1e7SLuigi Rizzo } 1372ce3ee1e7SLuigi Rizzo 1373f9790aebSLuigi Rizzo void 1374f9790aebSLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd) 1375ce3ee1e7SLuigi Rizzo { 1376ce3ee1e7SLuigi Rizzo return nmd->deref(nmd); 1377ccdc3305SLuigi Rizzo } 1378