1fb578518SFranco Fichtner /* 2fb578518SFranco Fichtner * Copyright (C) 2012-2013 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved. 3fb578518SFranco Fichtner * 4fb578518SFranco Fichtner * Redistribution and use in source and binary forms, with or without 5fb578518SFranco Fichtner * modification, are permitted provided that the following conditions 6fb578518SFranco Fichtner * are met: 7fb578518SFranco Fichtner * 1. Redistributions of source code must retain the above copyright 8fb578518SFranco Fichtner * notice, this list of conditions and the following disclaimer. 9fb578518SFranco Fichtner * 2. Redistributions in binary form must reproduce the above copyright 10fb578518SFranco Fichtner * notice, this list of conditions and the following disclaimer in the 11fb578518SFranco Fichtner * documentation and/or other materials provided with the distribution. 12fb578518SFranco Fichtner * 13fb578518SFranco Fichtner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14fb578518SFranco Fichtner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15fb578518SFranco Fichtner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16fb578518SFranco Fichtner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17fb578518SFranco Fichtner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18fb578518SFranco Fichtner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19fb578518SFranco Fichtner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20fb578518SFranco Fichtner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21fb578518SFranco Fichtner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22fb578518SFranco Fichtner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23fb578518SFranco Fichtner * SUCH DAMAGE. 24fb578518SFranco Fichtner */ 25fb578518SFranco Fichtner 26fb578518SFranco Fichtner /* 27fb578518SFranco Fichtner * $FreeBSD: head/sys/dev/netmap/netmap_mem2.c 234290 2012-04-14 16:44:18Z luigi $ 28fb578518SFranco Fichtner * 29fb578518SFranco Fichtner * (New) memory allocator for netmap 30fb578518SFranco Fichtner */ 31fb578518SFranco Fichtner 32fb578518SFranco Fichtner /* 33fb578518SFranco Fichtner * This allocator creates three memory pools: 34fb578518SFranco Fichtner * nm_if_pool for the struct netmap_if 35fb578518SFranco Fichtner * nm_ring_pool for the struct netmap_ring 36fb578518SFranco Fichtner * nm_buf_pool for the packet buffers. 37fb578518SFranco Fichtner * 38fb578518SFranco Fichtner * that contain netmap objects. Each pool is made of a number of clusters, 39fb578518SFranco Fichtner * multiple of a page size, each containing an integer number of objects. 40fb578518SFranco Fichtner * The clusters are contiguous in user space but not in the kernel. 41fb578518SFranco Fichtner * Only nm_buf_pool needs to be dma-able, 42fb578518SFranco Fichtner * but for convenience use the same type of allocator for all. 43fb578518SFranco Fichtner * 44fb578518SFranco Fichtner * Once mapped, the three pools are exported to userspace 45fb578518SFranco Fichtner * as a contiguous block, starting from nm_if_pool. Each 46fb578518SFranco Fichtner * cluster (and pool) is an integral number of pages. 47fb578518SFranco Fichtner * [ . . . ][ . . . . . .][ . . . . . . . . . .] 48fb578518SFranco Fichtner * nm_if nm_ring nm_buf 49fb578518SFranco Fichtner * 50fb578518SFranco Fichtner * The userspace areas contain offsets of the objects in userspace. 51fb578518SFranco Fichtner * When (at init time) we write these offsets, we find out the index 52fb578518SFranco Fichtner * of the object, and from there locate the offset from the beginning 53fb578518SFranco Fichtner * of the region. 54fb578518SFranco Fichtner * 55fb578518SFranco Fichtner * The invididual allocators manage a pool of memory for objects of 56fb578518SFranco Fichtner * the same size. 57fb578518SFranco Fichtner * The pool is split into smaller clusters, whose size is a 58fb578518SFranco Fichtner * multiple of the page size. The cluster size is chosen 59fb578518SFranco Fichtner * to minimize the waste for a given max cluster size 60fb578518SFranco Fichtner * (we do it by brute force, as we have relatively few objects 61fb578518SFranco Fichtner * per cluster). 62fb578518SFranco Fichtner * 63fb578518SFranco Fichtner * Objects are aligned to the cache line (64 bytes) rounding up object 64fb578518SFranco Fichtner * sizes when needed. A bitmap contains the state of each object. 65fb578518SFranco Fichtner * Allocation scans the bitmap; this is done only on attach, so we are not 66fb578518SFranco Fichtner * too worried about performance 67fb578518SFranco Fichtner * 68fb578518SFranco Fichtner * For each allocator we can define (thorugh sysctl) the size and 69fb578518SFranco Fichtner * number of each object. Memory is allocated at the first use of a 70fb578518SFranco Fichtner * netmap file descriptor, and can be freed when all such descriptors 71fb578518SFranco Fichtner * have been released (including unmapping the memory). 72fb578518SFranco Fichtner * If memory is scarce, the system tries to get as much as possible 73fb578518SFranco Fichtner * and the sysctl values reflect the actual allocation. 74fb578518SFranco Fichtner * Together with desired values, the sysctl export also absolute 75fb578518SFranco Fichtner * min and maximum values that cannot be overridden. 76fb578518SFranco Fichtner * 77fb578518SFranco Fichtner * struct netmap_if: 78fb578518SFranco Fichtner * variable size, max 16 bytes per ring pair plus some fixed amount. 79fb578518SFranco Fichtner * 1024 bytes should be large enough in practice. 80fb578518SFranco Fichtner * 81fb578518SFranco Fichtner * In the worst case we have one netmap_if per ring in the system. 82fb578518SFranco Fichtner * 83fb578518SFranco Fichtner * struct netmap_ring 84fb578518SFranco Fichtner * variable size, 8 byte per slot plus some fixed amount. 85fb578518SFranco Fichtner * Rings can be large (e.g. 4k slots, or >32Kbytes). 86fb578518SFranco Fichtner * We default to 36 KB (9 pages), and a few hundred rings. 87fb578518SFranco Fichtner * 88fb578518SFranco Fichtner * struct netmap_buffer 89fb578518SFranco Fichtner * The more the better, both because fast interfaces tend to have 90fb578518SFranco Fichtner * many slots, and because we may want to use buffers to store 91fb578518SFranco Fichtner * packets in userspace avoiding copies. 92fb578518SFranco Fichtner * Must contain a full frame (eg 1518, or more for vlans, jumbo 93fb578518SFranco Fichtner * frames etc.) plus be nicely aligned, plus some NICs restrict 94fb578518SFranco Fichtner * the size to multiple of 1K or so. Default to 2K 95fb578518SFranco Fichtner */ 96fb578518SFranco Fichtner #ifndef _NET_NETMAP_MEM2_H_ 97fb578518SFranco Fichtner #define _NET_NETMAP_MEM2_H_ 98fb578518SFranco Fichtner 99fb578518SFranco Fichtner 100fb578518SFranco Fichtner #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */ 101fb578518SFranco Fichtner 102fb578518SFranco Fichtner #define NETMAP_POOL_MAX_NAMSZ 32 103fb578518SFranco Fichtner 104fb578518SFranco Fichtner 105fb578518SFranco Fichtner enum { 106fb578518SFranco Fichtner NETMAP_IF_POOL = 0, 107fb578518SFranco Fichtner NETMAP_RING_POOL, 108fb578518SFranco Fichtner NETMAP_BUF_POOL, 109fb578518SFranco Fichtner NETMAP_POOLS_NR 110fb578518SFranco Fichtner }; 111fb578518SFranco Fichtner 112fb578518SFranco Fichtner 113fb578518SFranco Fichtner struct netmap_obj_params { 114fb578518SFranco Fichtner u_int size; 115fb578518SFranco Fichtner u_int num; 116fb578518SFranco Fichtner }; 117fb578518SFranco Fichtner struct netmap_obj_pool { 118fb578518SFranco Fichtner char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */ 119fb578518SFranco Fichtner 120fb578518SFranco Fichtner /* ---------------------------------------------------*/ 121fb578518SFranco Fichtner /* these are only meaningful if the pool is finalized */ 122fb578518SFranco Fichtner /* (see 'finalized' field in netmap_mem_d) */ 123fb578518SFranco Fichtner u_int objtotal; /* actual total number of objects. */ 124fb578518SFranco Fichtner u_int memtotal; /* actual total memory space */ 125fb578518SFranco Fichtner u_int numclusters; /* actual number of clusters */ 126fb578518SFranco Fichtner 127fb578518SFranco Fichtner u_int objfree; /* number of free objects. */ 128fb578518SFranco Fichtner 129fb578518SFranco Fichtner struct lut_entry *lut; /* virt,phys addresses, objtotal entries */ 130fb578518SFranco Fichtner uint32_t *bitmap; /* one bit per buffer, 1 means free */ 131fb578518SFranco Fichtner uint32_t bitmap_slots; /* number of uint32 entries in bitmap */ 132fb578518SFranco Fichtner /* ---------------------------------------------------*/ 133fb578518SFranco Fichtner 134fb578518SFranco Fichtner /* limits */ 135fb578518SFranco Fichtner u_int objminsize; /* minimum object size */ 136fb578518SFranco Fichtner u_int objmaxsize; /* maximum object size */ 137fb578518SFranco Fichtner u_int nummin; /* minimum number of objects */ 138fb578518SFranco Fichtner u_int nummax; /* maximum number of objects */ 139fb578518SFranco Fichtner 140fb578518SFranco Fichtner /* these are changed only by config */ 141fb578518SFranco Fichtner u_int _objtotal; /* total number of objects */ 142fb578518SFranco Fichtner u_int _objsize; /* object size */ 143fb578518SFranco Fichtner u_int _clustsize; /* cluster size */ 144fb578518SFranco Fichtner u_int _clustentries; /* objects per cluster */ 145fb578518SFranco Fichtner u_int _numclusters; /* number of clusters */ 146fb578518SFranco Fichtner 147fb578518SFranco Fichtner /* requested values */ 148fb578518SFranco Fichtner u_int r_objtotal; 149fb578518SFranco Fichtner u_int r_objsize; 150fb578518SFranco Fichtner }; 151fb578518SFranco Fichtner 152*ed9bd855SFranco Fichtner #define NMA_LOCK_T struct lock 153fb578518SFranco Fichtner 154fb578518SFranco Fichtner typedef int (*netmap_mem_config_t)(struct netmap_mem_d*); 155fb578518SFranco Fichtner typedef int (*netmap_mem_finalize_t)(struct netmap_mem_d*); 156fb578518SFranco Fichtner typedef void (*netmap_mem_deref_t)(struct netmap_mem_d*); 157fb578518SFranco Fichtner 158fb578518SFranco Fichtner 159fb578518SFranco Fichtner /* We implement two kinds of netmap_mem_d structures: 160fb578518SFranco Fichtner * 161fb578518SFranco Fichtner * - global: used by hardware NICS; 162fb578518SFranco Fichtner * 163fb578518SFranco Fichtner * - private: used by VALE ports. 164fb578518SFranco Fichtner * 165fb578518SFranco Fichtner * In both cases, the netmap_mem_d structure has the same lifetime as the 166fb578518SFranco Fichtner * netmap_adapter of the corresponding NIC or port. It is the responsibility of 167fb578518SFranco Fichtner * the client code to delete the private allocator when the associated 168fb578518SFranco Fichtner * netmap_adapter is freed (this is implemented by the NAF_MEM_OWNER flag in 169fb578518SFranco Fichtner * netmap.c). The 'refcount' field counts the number of active users of the 170fb578518SFranco Fichtner * structure. The global allocator uses this information to prevent/allow 171fb578518SFranco Fichtner * reconfiguration. The private allocators release all their memory when there 172fb578518SFranco Fichtner * are no active users. By 'active user' we mean an existing netmap_priv 173fb578518SFranco Fichtner * structure holding a reference to the allocator. 174fb578518SFranco Fichtner */ 175fb578518SFranco Fichtner struct netmap_mem_d { 176fb578518SFranco Fichtner NMA_LOCK_T nm_mtx; /* protect the allocator */ 177fb578518SFranco Fichtner u_int nm_totalsize; /* shorthand */ 178fb578518SFranco Fichtner 179fb578518SFranco Fichtner u_int flags; 180fb578518SFranco Fichtner #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */ 181fb578518SFranco Fichtner #define NETMAP_MEM_PRIVATE 0x2 /* uses private address space */ 182fb578518SFranco Fichtner int lasterr; /* last error for curr config */ 183fb578518SFranco Fichtner int refcount; /* existing priv structures */ 184fb578518SFranco Fichtner /* the three allocators */ 185fb578518SFranco Fichtner struct netmap_obj_pool pools[NETMAP_POOLS_NR]; 186fb578518SFranco Fichtner 187fb578518SFranco Fichtner netmap_mem_config_t config; 188fb578518SFranco Fichtner netmap_mem_finalize_t finalize; 189fb578518SFranco Fichtner netmap_mem_deref_t deref; 190fb578518SFranco Fichtner }; 191fb578518SFranco Fichtner 192fb578518SFranco Fichtner extern struct netmap_mem_d nm_mem; 193fb578518SFranco Fichtner 194fb578518SFranco Fichtner vm_paddr_t netmap_mem_ofstophys(struct netmap_mem_d *, vm_ooffset_t); 195fb578518SFranco Fichtner int netmap_mem_finalize(struct netmap_mem_d *); 196fb578518SFranco Fichtner int netmap_mem_init(void); 197fb578518SFranco Fichtner void netmap_mem_fini(void); 198fb578518SFranco Fichtner struct netmap_if * 199fb578518SFranco Fichtner netmap_mem_if_new(const char *, struct netmap_adapter *); 200fb578518SFranco Fichtner void netmap_mem_if_delete(struct netmap_adapter *, struct netmap_if *); 201fb578518SFranco Fichtner int netmap_mem_rings_create(struct netmap_adapter *); 202fb578518SFranco Fichtner void netmap_mem_rings_delete(struct netmap_adapter *); 203fb578518SFranco Fichtner void netmap_mem_deref(struct netmap_mem_d *); 204fb578518SFranco Fichtner int netmap_mem_get_info(struct netmap_mem_d *, u_int *size, u_int *memflags); 205fb578518SFranco Fichtner ssize_t netmap_mem_if_offset(struct netmap_mem_d *, const void *vaddr); 206fb578518SFranco Fichtner struct netmap_mem_d* 207fb578518SFranco Fichtner netmap_mem_private_new(const char *name, u_int txr, u_int txd, u_int rxr, u_int rxd); 208fb578518SFranco Fichtner void netmap_mem_private_delete(struct netmap_mem_d *); 209fb578518SFranco Fichtner 210fb578518SFranco Fichtner #define NETMAP_BDG_BUF_SIZE(n) ((n)->pools[NETMAP_BUF_POOL]._objsize) 211fb578518SFranco Fichtner 212fb578518SFranco Fichtner 213fb578518SFranco Fichtner 214fb578518SFranco Fichtner #endif 215