2 * Copyright (C) 2012-2014 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #endif /* __APPLE__ */
35 #include <sys/cdefs.h> /* prerequisite */
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
39 #include <sys/malloc.h>
41 #include <vm/vm.h> /* vtophys */
42 #include <vm/pmap.h> /* vtophys */
43 #include <sys/socket.h> /* sockaddrs */
44 #include <sys/selinfo.h>
45 #include <sys/sysctl.h>
47 #include <net/if_var.h>
49 #include <machine/bus.h> /* bus_dmamap_* */
51 #endif /* __FreeBSD__ */
53 #include <net/netmap.h>
54 #include <dev/netmap/netmap_kern.h>
55 #include "netmap_mem2.h"
58 #define NMA_LOCK_INIT(n) sema_init(&(n)->nm_mtx, 1)
59 #define NMA_LOCK_DESTROY(n)
60 #define NMA_LOCK(n) down(&(n)->nm_mtx)
61 #define NMA_UNLOCK(n) up(&(n)->nm_mtx)
63 #define NMA_LOCK_INIT(n) mtx_init(&(n)->nm_mtx, "netmap memory allocator lock", NULL, MTX_DEF)
64 #define NMA_LOCK_DESTROY(n) mtx_destroy(&(n)->nm_mtx)
65 #define NMA_LOCK(n) mtx_lock(&(n)->nm_mtx)
66 #define NMA_UNLOCK(n) mtx_unlock(&(n)->nm_mtx)
70 struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = {
75 [NETMAP_RING_POOL] = {
81 .num = NETMAP_BUF_MAX_NUM,
87 * nm_mem is the memory allocator used for all physical interfaces
88 * running in netmap mode.
89 * Virtual (VALE) ports will have each its own allocator.
91 static int netmap_mem_global_config(struct netmap_mem_d *nmd);
92 static int netmap_mem_global_finalize(struct netmap_mem_d *nmd);
93 static void netmap_mem_global_deref(struct netmap_mem_d *nmd);
94 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
98 .objminsize = sizeof(struct netmap_if),
100 .nummin = 10, /* don't be stingy */
101 .nummax = 10000, /* XXX very large */
103 [NETMAP_RING_POOL] = {
104 .name = "netmap_ring",
105 .objminsize = sizeof(struct netmap_ring),
106 .objmaxsize = 32*PAGE_SIZE,
110 [NETMAP_BUF_POOL] = {
111 .name = "netmap_buf",
115 .nummax = 1000000, /* one million! */
118 .config = netmap_mem_global_config,
119 .finalize = netmap_mem_global_finalize,
120 .deref = netmap_mem_global_deref,
124 // XXX logically belongs to nm_mem
125 struct lut_entry *netmap_buffer_lut; /* exported */
127 /* blueprint for the private memory allocators */
128 static int netmap_mem_private_config(struct netmap_mem_d *nmd);
129 static int netmap_mem_private_finalize(struct netmap_mem_d *nmd);
130 static void netmap_mem_private_deref(struct netmap_mem_d *nmd);
131 const struct netmap_mem_d nm_blueprint = {
135 .objminsize = sizeof(struct netmap_if),
140 [NETMAP_RING_POOL] = {
142 .objminsize = sizeof(struct netmap_ring),
143 .objmaxsize = 32*PAGE_SIZE,
147 [NETMAP_BUF_POOL] = {
152 .nummax = 1000000, /* one million! */
155 .config = netmap_mem_private_config,
156 .finalize = netmap_mem_private_finalize,
157 .deref = netmap_mem_private_deref,
159 .flags = NETMAP_MEM_PRIVATE,
162 /* memory allocator related sysctls */
164 #define STRINGIFY(x) #x
167 #define DECLARE_SYSCTLS(id, name) \
168 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
169 CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
170 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
171 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
172 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
173 CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
174 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
175 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s")
177 SYSCTL_DECL(_dev_netmap);
178 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
179 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
180 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
183 * First, find the allocator that contains the requested offset,
184 * then locate the cluster through a lookup table.
187 netmap_mem_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
190 vm_ooffset_t o = offset;
192 struct netmap_obj_pool *p;
197 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
198 if (offset >= p[i].memtotal)
200 // now lookup the cluster's address
201 pa = p[i].lut[offset / p[i]._objsize].paddr +
202 offset % p[i]._objsize;
206 /* this is only in case of errors */
207 D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
208 p[NETMAP_IF_POOL].memtotal,
209 p[NETMAP_IF_POOL].memtotal
210 + p[NETMAP_RING_POOL].memtotal,
211 p[NETMAP_IF_POOL].memtotal
212 + p[NETMAP_RING_POOL].memtotal
213 + p[NETMAP_BUF_POOL].memtotal);
215 return 0; // XXX bad address
219 netmap_mem_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags)
223 error = nmd->config(nmd);
226 if (nmd->flags & NETMAP_MEM_FINALIZED) {
227 *size = nmd->nm_totalsize;
231 for (i = 0; i < NETMAP_POOLS_NR; i++) {
232 struct netmap_obj_pool *p = nmd->pools + i;
233 *size += (p->_numclusters * p->_clustsize);
236 *memflags = nmd->flags;
243 * we store objects by kernel address, need to find the offset
244 * within the pool to export the value to userspace.
245 * Algorithm: scan until we find the cluster, then add the
246 * actual offset in the cluster
249 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
251 int i, k = p->_clustentries, n = p->objtotal;
254 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
255 const char *base = p->lut[i].vaddr;
256 ssize_t relofs = (const char *) vaddr - base;
258 if (relofs < 0 || relofs >= p->_clustsize)
262 ND("%s: return offset %d (cluster %d) for pointer %p",
263 p->name, ofs, i, vaddr);
266 D("address %p is not contained inside any cluster (%s)",
268 return 0; /* An error occurred */
271 /* Helper functions which convert virtual addresses to offsets */
272 #define netmap_if_offset(n, v) \
273 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
275 #define netmap_ring_offset(n, v) \
276 ((n)->pools[NETMAP_IF_POOL].memtotal + \
277 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
279 #define netmap_buf_offset(n, v) \
280 ((n)->pools[NETMAP_IF_POOL].memtotal + \
281 (n)->pools[NETMAP_RING_POOL].memtotal + \
282 netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)))
286 netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *addr)
290 v = netmap_if_offset(nmd, addr);
296 * report the index, and use start position as a hint,
297 * otherwise buffer allocation becomes terribly expensive.
300 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
302 uint32_t i = 0; /* index in the bitmap */
303 uint32_t mask, j; /* slot counter */
306 if (len > p->_objsize) {
307 D("%s request size %d too large", p->name, len);
308 // XXX cannot reduce the size
312 if (p->objfree == 0) {
313 D("no more %s objects", p->name);
319 /* termination is guaranteed by p->free, but better check bounds on i */
320 while (vaddr == NULL && i < p->bitmap_slots) {
321 uint32_t cur = p->bitmap[i];
322 if (cur == 0) { /* bitmask is fully used */
327 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
330 p->bitmap[i] &= ~mask; /* mark object as in use */
333 vaddr = p->lut[i * 32 + j].vaddr;
337 ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr);
346 * free by index, not by address. This is slow, but is only used
347 * for a small number of objects (rings, nifp)
350 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
352 if (j >= p->objtotal) {
353 D("invalid index %u, max %u", j, p->objtotal);
356 p->bitmap[j / 32] |= (1 << (j % 32));
362 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
364 u_int i, j, n = p->numclusters;
366 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
367 void *base = p->lut[i * p->_clustentries].vaddr;
368 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
370 /* Given address, is out of the scope of the current cluster.*/
371 if (vaddr < base || relofs >= p->_clustsize)
374 j = j + relofs / p->_objsize;
375 /* KASSERT(j != 0, ("Cannot free object 0")); */
376 netmap_obj_free(p, j);
379 D("address %p is not contained inside any cluster (%s)",
383 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
384 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
385 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
386 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
387 #define netmap_buf_malloc(n, _pos, _index) \
388 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], NETMAP_BDG_BUF_SIZE(n), _pos, _index)
391 /* Return the index associated to the given packet buffer */
392 #define netmap_buf_index(n, v) \
393 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
396 /* Return nonzero on error */
398 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
400 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
401 u_int i = 0; /* slot counter */
402 uint32_t pos = 0; /* slot in p->bitmap */
403 uint32_t index = 0; /* buffer index */
405 for (i = 0; i < n; i++) {
406 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
408 D("no more buffers after %d of %d", i, n);
411 slot[i].buf_idx = index;
412 slot[i].len = p->_objsize;
416 ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
422 netmap_obj_free(p, slot[i].buf_idx);
424 bzero(slot, n * sizeof(slot[0]));
430 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
432 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
434 if (i < 2 || i >= p->objtotal) {
435 D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
438 netmap_obj_free(p, i);
442 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
448 free(p->bitmap, M_NETMAP);
452 size_t sz = p->_clustsize;
454 for (i = 0; i < p->objtotal; i += p->_clustentries) {
456 contigfree(p->lut[i].vaddr, sz, M_NETMAP);
458 bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
462 free(p->lut, M_NETMAP);
473 * Free all resources related to an allocator.
476 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
480 netmap_reset_obj_allocator(p);
484 * We receive a request for objtotal objects, of size objsize each.
485 * Internally we may round up both numbers, as we allocate objects
486 * in small clusters multiple of the page size.
487 * We need to keep track of objtotal and clustentries,
488 * as they are needed when freeing memory.
490 * XXX note -- userspace needs the buffers to be contiguous,
491 * so we cannot afford gaps at the end of a cluster.
495 /* call with NMA_LOCK held */
497 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
500 u_int clustsize; /* the cluster size, multiple of page size */
501 u_int clustentries; /* how many objects per entry */
503 /* we store the current request, so we can
504 * detect configuration changes later */
505 p->r_objtotal = objtotal;
506 p->r_objsize = objsize;
508 #define MAX_CLUSTSIZE (1<<17)
509 #define LINE_ROUND NM_CACHE_ALIGN // 64
510 if (objsize >= MAX_CLUSTSIZE) {
511 /* we could do it but there is no point */
512 D("unsupported allocation for %d bytes", objsize);
515 /* make sure objsize is a multiple of LINE_ROUND */
516 i = (objsize & (LINE_ROUND - 1));
518 D("XXX aligning object by %d bytes", LINE_ROUND - i);
519 objsize += LINE_ROUND - i;
521 if (objsize < p->objminsize || objsize > p->objmaxsize) {
522 D("requested objsize %d out of range [%d, %d]",
523 objsize, p->objminsize, p->objmaxsize);
526 if (objtotal < p->nummin || objtotal > p->nummax) {
527 D("requested objtotal %d out of range [%d, %d]",
528 objtotal, p->nummin, p->nummax);
532 * Compute number of objects using a brute-force approach:
533 * given a max cluster size,
534 * we try to fill it with objects keeping track of the
535 * wasted space to the next page boundary.
537 for (clustentries = 0, i = 1;; i++) {
538 u_int delta, used = i * objsize;
539 if (used > MAX_CLUSTSIZE)
541 delta = used % PAGE_SIZE;
542 if (delta == 0) { // exact solution
546 if (delta > ( (clustentries*objsize) % PAGE_SIZE) )
549 // D("XXX --- ouch, delta %d (bad for buffers)", delta);
550 /* compute clustsize and round to the next page */
551 clustsize = clustentries * objsize;
552 i = (clustsize & (PAGE_SIZE - 1));
554 clustsize += PAGE_SIZE - i;
556 D("objsize %d clustsize %d objects %d",
557 objsize, clustsize, clustentries);
560 * The number of clusters is n = ceil(objtotal/clustentries)
561 * objtotal' = n * clustentries
563 p->_clustentries = clustentries;
564 p->_clustsize = clustsize;
565 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
567 /* actual values (may be larger than requested) */
568 p->_objsize = objsize;
569 p->_objtotal = p->_numclusters * clustentries;
575 /* call with NMA_LOCK held */
577 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
579 int i; /* must be signed */
582 /* optimistically assume we have enough memory */
583 p->numclusters = p->_numclusters;
584 p->objtotal = p->_objtotal;
586 n = sizeof(struct lut_entry) * p->objtotal;
590 p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
592 if (p->lut == NULL) {
593 D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
597 /* Allocate the bitmap */
598 n = (p->objtotal + 31) / 32;
599 p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
600 if (p->bitmap == NULL) {
601 D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
608 * Allocate clusters, init pointers and bitmap
612 for (i = 0; i < (int)p->objtotal;) {
613 int lim = i + p->_clustentries;
616 clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
617 (size_t)0, -1UL, PAGE_SIZE, 0);
620 * If we get here, there is a severe memory shortage,
621 * so halve the allocated memory to reclaim some.
623 D("Unable to create cluster at %d for '%s' allocator",
625 if (i < 2) /* nothing to halve */
628 for (i--; i >= lim; i--) {
629 p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) );
630 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
631 contigfree(p->lut[i].vaddr,
636 /* we may have stopped in the middle of a cluster */
637 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
640 for (; i < lim; i++, clust += p->_objsize) {
641 p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) );
642 p->lut[i].vaddr = clust;
643 p->lut[i].paddr = vtophys(clust);
646 p->objfree = p->objtotal;
647 p->memtotal = p->numclusters * p->_clustsize;
651 D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
652 p->numclusters, p->_clustsize >> 10,
653 p->memtotal >> 10, p->name);
658 netmap_reset_obj_allocator(p);
662 /* call with lock held */
664 netmap_memory_config_changed(struct netmap_mem_d *nmd)
668 for (i = 0; i < NETMAP_POOLS_NR; i++) {
669 if (nmd->pools[i].r_objsize != netmap_params[i].size ||
670 nmd->pools[i].r_objtotal != netmap_params[i].num)
677 netmap_mem_reset_all(struct netmap_mem_d *nmd)
680 D("resetting %p", nmd);
681 for (i = 0; i < NETMAP_POOLS_NR; i++) {
682 netmap_reset_obj_allocator(&nmd->pools[i]);
684 nmd->flags &= ~NETMAP_MEM_FINALIZED;
688 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
691 if (nmd->flags & NETMAP_MEM_FINALIZED)
694 nmd->nm_totalsize = 0;
695 for (i = 0; i < NETMAP_POOLS_NR; i++) {
696 nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
699 nmd->nm_totalsize += nmd->pools[i].memtotal;
701 /* buffers 0 and 1 are reserved */
702 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
703 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
704 nmd->flags |= NETMAP_MEM_FINALIZED;
706 D("Have %d KB for interfaces, %d KB for rings and %d MB for buffers",
707 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
708 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
709 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
711 D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
716 netmap_mem_reset_all(nmd);
723 netmap_mem_private_delete(struct netmap_mem_d *nmd)
727 D("deleting %p", nmd);
728 if (nmd->refcount > 0)
729 D("bug: deleting mem allocator with refcount=%d!", nmd->refcount);
730 D("done deleting %p", nmd);
731 NMA_LOCK_DESTROY(nmd);
736 netmap_mem_private_config(struct netmap_mem_d *nmd)
738 /* nothing to do, we are configured on creation
739 * and configuration never changes thereafter
745 netmap_mem_private_finalize(struct netmap_mem_d *nmd)
750 err = netmap_mem_finalize_all(nmd);
757 netmap_mem_private_deref(struct netmap_mem_d *nmd)
760 if (--nmd->refcount <= 0)
761 netmap_mem_reset_all(nmd);
765 struct netmap_mem_d *
766 netmap_mem_private_new(const char *name, u_int txr, u_int txd, u_int rxr, u_int rxd)
768 struct netmap_mem_d *d = NULL;
769 struct netmap_obj_params p[NETMAP_POOLS_NR];
773 d = malloc(sizeof(struct netmap_mem_d),
774 M_DEVBUF, M_NOWAIT | M_ZERO);
780 /* XXX the rest of the code assumes the stack rings are alwasy present */
783 p[NETMAP_IF_POOL].size = sizeof(struct netmap_if) +
784 sizeof(ssize_t) * (txr + rxr);
785 p[NETMAP_IF_POOL].num = 2;
786 maxd = (txd > rxd) ? txd : rxd;
787 p[NETMAP_RING_POOL].size = sizeof(struct netmap_ring) +
788 sizeof(struct netmap_slot) * maxd;
789 p[NETMAP_RING_POOL].num = txr + rxr;
790 p[NETMAP_BUF_POOL].size = 2048; /* XXX find a way to let the user choose this */
791 p[NETMAP_BUF_POOL].num = rxr * (rxd + 2) + txr * (txd + 2);
793 D("req if %d*%d ring %d*%d buf %d*%d",
794 p[NETMAP_IF_POOL].num,
795 p[NETMAP_IF_POOL].size,
796 p[NETMAP_RING_POOL].num,
797 p[NETMAP_RING_POOL].size,
798 p[NETMAP_BUF_POOL].num,
799 p[NETMAP_BUF_POOL].size);
801 for (i = 0; i < NETMAP_POOLS_NR; i++) {
802 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
803 nm_blueprint.pools[i].name,
805 if (netmap_config_obj_allocator(&d->pools[i],
806 p[i].num, p[i].size))
810 d->flags &= ~NETMAP_MEM_FINALIZED;
816 netmap_mem_private_delete(d);
821 /* call with lock held */
823 netmap_mem_global_config(struct netmap_mem_d *nmd)
828 /* already in use, we cannot change the configuration */
831 if (!netmap_memory_config_changed(nmd))
836 if (nmd->flags & NETMAP_MEM_FINALIZED) {
837 /* reset previous allocation */
838 for (i = 0; i < NETMAP_POOLS_NR; i++) {
839 netmap_reset_obj_allocator(&nmd->pools[i]);
841 nmd->flags &= ~NETMAP_MEM_FINALIZED;
844 for (i = 0; i < NETMAP_POOLS_NR; i++) {
845 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
846 netmap_params[i].num, netmap_params[i].size);
857 netmap_mem_global_finalize(struct netmap_mem_d *nmd)
864 /* update configuration if changed */
865 if (netmap_mem_global_config(nmd))
870 if (nmd->flags & NETMAP_MEM_FINALIZED) {
871 /* may happen if config is not changed */
876 if (netmap_mem_finalize_all(nmd))
879 /* backward compatibility */
880 netmap_buf_size = nmd->pools[NETMAP_BUF_POOL]._objsize;
881 netmap_total_buffers = nmd->pools[NETMAP_BUF_POOL].objtotal;
883 netmap_buffer_lut = nmd->pools[NETMAP_BUF_POOL].lut;
884 netmap_buffer_base = nmd->pools[NETMAP_BUF_POOL].lut[0].vaddr;
900 netmap_mem_init(void)
902 NMA_LOCK_INIT(&nm_mem);
907 netmap_mem_fini(void)
911 for (i = 0; i < NETMAP_POOLS_NR; i++) {
912 netmap_destroy_obj_allocator(&nm_mem.pools[i]);
914 NMA_LOCK_DESTROY(&nm_mem);
918 netmap_free_rings(struct netmap_adapter *na)
923 for (i = 0; i < na->num_tx_rings + 1; i++) {
924 if (na->tx_rings[i].ring) {
925 netmap_ring_free(na->nm_mem, na->tx_rings[i].ring);
926 na->tx_rings[i].ring = NULL;
929 for (i = 0; i < na->num_rx_rings + 1; i++) {
930 if (na->rx_rings[i].ring) {
931 netmap_ring_free(na->nm_mem, na->rx_rings[i].ring);
932 na->rx_rings[i].ring = NULL;
937 /* call with NMA_LOCK held *
939 * Allocate netmap rings and buffers for this card
940 * The rings are contiguous, but have variable size.
943 netmap_mem_rings_create(struct netmap_adapter *na)
945 struct netmap_ring *ring;
947 struct netmap_kring *kring;
949 NMA_LOCK(na->nm_mem);
951 for (kring = na->tx_rings; kring != na->rx_rings; kring++) { /* Transmit rings */
952 ndesc = kring->nkr_num_slots;
953 len = sizeof(struct netmap_ring) +
954 ndesc * sizeof(struct netmap_slot);
955 ring = netmap_ring_malloc(na->nm_mem, len);
957 D("Cannot allocate tx_ring");
960 ND("txring at %p", ring);
962 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
963 *(int64_t *)(uintptr_t)&ring->buf_ofs =
964 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
965 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
966 netmap_ring_offset(na->nm_mem, ring);
968 /* copy values from kring */
969 ring->head = kring->rhead;
970 ring->cur = kring->rcur;
971 ring->tail = kring->rtail;
972 *(uint16_t *)(uintptr_t)&ring->nr_buf_size =
973 NETMAP_BDG_BUF_SIZE(na->nm_mem);
974 ND("initializing slots for txring");
975 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
976 D("Cannot allocate buffers for tx_ring");
981 for ( ; kring != na->tailroom; kring++) { /* Receive rings */
982 ndesc = kring->nkr_num_slots;
983 len = sizeof(struct netmap_ring) +
984 ndesc * sizeof(struct netmap_slot);
985 ring = netmap_ring_malloc(na->nm_mem, len);
987 D("Cannot allocate rx_ring");
990 ND("rxring at %p", ring);
992 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
993 *(int64_t *)(uintptr_t)&ring->buf_ofs =
994 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
995 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
996 netmap_ring_offset(na->nm_mem, ring);
998 /* copy values from kring */
999 ring->head = kring->rhead;
1000 ring->cur = kring->rcur;
1001 ring->tail = kring->rtail;
1002 *(int *)(uintptr_t)&ring->nr_buf_size =
1003 NETMAP_BDG_BUF_SIZE(na->nm_mem);
1004 ND("initializing slots for rxring %p", ring);
1005 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1006 D("Cannot allocate buffers for rx_ring");
1011 NMA_UNLOCK(na->nm_mem);
1016 netmap_free_rings(na);
1018 NMA_UNLOCK(na->nm_mem);
1024 netmap_mem_rings_delete(struct netmap_adapter *na)
1026 /* last instance, release bufs and rings */
1028 struct netmap_kring *kring;
1029 struct netmap_ring *ring;
1031 NMA_LOCK(na->nm_mem);
1033 for (kring = na->tx_rings; kring != na->tailroom; kring++) {
1037 lim = kring->nkr_num_slots;
1038 for (i = 0; i < lim; i++)
1039 netmap_free_buf(na->nm_mem, ring->slot[i].buf_idx);
1041 netmap_free_rings(na);
1043 NMA_UNLOCK(na->nm_mem);
1047 /* call with NMA_LOCK held */
1049 * Allocate the per-fd structure netmap_if.
1051 * We assume that the configuration stored in na
1052 * (number of tx/rx rings and descs) does not change while
1053 * the interface is in netmap mode.
1056 netmap_mem_if_new(const char *ifname, struct netmap_adapter *na)
1058 struct netmap_if *nifp;
1059 ssize_t base; /* handy for relative offsets between rings and nifp */
1060 u_int i, len, ntx, nrx;
1063 * verify whether virtual port need the stack ring
1065 ntx = na->num_tx_rings + 1; /* shorthand, include stack ring */
1066 nrx = na->num_rx_rings + 1; /* shorthand, include stack ring */
1068 * the descriptor is followed inline by an array of offsets
1069 * to the tx and rx rings in the shared memory region.
1070 * For virtual rx rings we also allocate an array of
1071 * pointers to assign to nkr_leases.
1074 NMA_LOCK(na->nm_mem);
1076 len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
1077 nifp = netmap_if_malloc(na->nm_mem, len);
1079 NMA_UNLOCK(na->nm_mem);
1083 /* initialize base fields -- override const */
1084 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1085 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1086 strncpy(nifp->ni_name, ifname, (size_t)IFNAMSIZ);
1089 * fill the slots for the rx and tx rings. They contain the offset
1090 * between the ring and nifp, so the information is usable in
1091 * userspace to reach the ring from the nifp.
1093 base = netmap_if_offset(na->nm_mem, nifp);
1094 for (i = 0; i < ntx; i++) {
1095 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1096 netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1098 for (i = 0; i < nrx; i++) {
1099 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
1100 netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1103 NMA_UNLOCK(na->nm_mem);
1109 netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1114 NMA_LOCK(na->nm_mem);
1116 netmap_if_free(na->nm_mem, nifp);
1118 NMA_UNLOCK(na->nm_mem);
1122 netmap_mem_global_deref(struct netmap_mem_d *nmd)
1128 D("refcount = %d", nmd->refcount);
1134 netmap_mem_finalize(struct netmap_mem_d *nmd)
1136 return nmd->finalize(nmd);
1140 netmap_mem_deref(struct netmap_mem_d *nmd)
1142 return nmd->deref(nmd);