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,
85 struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
90 [NETMAP_RING_POOL] = {
102 * nm_mem is the memory allocator used for all physical interfaces
103 * running in netmap mode.
104 * Virtual (VALE) ports will have each its own allocator.
106 static int netmap_mem_global_config(struct netmap_mem_d *nmd);
107 static int netmap_mem_global_finalize(struct netmap_mem_d *nmd);
108 static void netmap_mem_global_deref(struct netmap_mem_d *nmd);
109 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
113 .objminsize = sizeof(struct netmap_if),
115 .nummin = 10, /* don't be stingy */
116 .nummax = 10000, /* XXX very large */
118 [NETMAP_RING_POOL] = {
119 .name = "netmap_ring",
120 .objminsize = sizeof(struct netmap_ring),
121 .objmaxsize = 32*PAGE_SIZE,
125 [NETMAP_BUF_POOL] = {
126 .name = "netmap_buf",
130 .nummax = 1000000, /* one million! */
133 .config = netmap_mem_global_config,
134 .finalize = netmap_mem_global_finalize,
135 .deref = netmap_mem_global_deref,
144 struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
146 // XXX logically belongs to nm_mem
147 struct lut_entry *netmap_buffer_lut; /* exported */
149 /* blueprint for the private memory allocators */
150 static int netmap_mem_private_config(struct netmap_mem_d *nmd);
151 static int netmap_mem_private_finalize(struct netmap_mem_d *nmd);
152 static void netmap_mem_private_deref(struct netmap_mem_d *nmd);
153 const struct netmap_mem_d nm_blueprint = {
157 .objminsize = sizeof(struct netmap_if),
162 [NETMAP_RING_POOL] = {
164 .objminsize = sizeof(struct netmap_ring),
165 .objmaxsize = 32*PAGE_SIZE,
169 [NETMAP_BUF_POOL] = {
174 .nummax = 1000000, /* one million! */
177 .config = netmap_mem_private_config,
178 .finalize = netmap_mem_private_finalize,
179 .deref = netmap_mem_private_deref,
181 .flags = NETMAP_MEM_PRIVATE,
184 /* memory allocator related sysctls */
186 #define STRINGIFY(x) #x
189 #define DECLARE_SYSCTLS(id, name) \
190 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
191 CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
192 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
193 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
194 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
195 CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
196 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
197 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
198 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
199 CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
200 "Default size of private netmap " STRINGIFY(name) "s"); \
201 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
202 CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
203 "Default number of private netmap " STRINGIFY(name) "s")
205 SYSCTL_DECL(_dev_netmap);
206 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
207 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
208 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
211 nm_mem_assign_id(struct netmap_mem_d *nmd)
214 struct netmap_mem_d *scan = netmap_last_mem_d;
220 /* we rely on unsigned wrap around */
221 id = scan->nm_id + 1;
222 if (id == 0) /* reserve 0 as error value */
225 if (id != scan->nm_id) {
227 nmd->prev = scan->prev;
229 scan->prev->next = nmd;
231 netmap_last_mem_d = nmd;
235 } while (scan != netmap_last_mem_d);
242 nm_mem_release_id(struct netmap_mem_d *nmd)
246 nmd->prev->next = nmd->next;
247 nmd->next->prev = nmd->prev;
249 if (netmap_last_mem_d == nmd)
250 netmap_last_mem_d = nmd->prev;
252 nmd->prev = nmd->next = NULL;
259 * First, find the allocator that contains the requested offset,
260 * then locate the cluster through a lookup table.
263 netmap_mem_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
266 vm_ooffset_t o = offset;
268 struct netmap_obj_pool *p;
273 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
274 if (offset >= p[i].memtotal)
276 // now lookup the cluster's address
277 pa = p[i].lut[offset / p[i]._objsize].paddr +
278 offset % p[i]._objsize;
282 /* this is only in case of errors */
283 D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
284 p[NETMAP_IF_POOL].memtotal,
285 p[NETMAP_IF_POOL].memtotal
286 + p[NETMAP_RING_POOL].memtotal,
287 p[NETMAP_IF_POOL].memtotal
288 + p[NETMAP_RING_POOL].memtotal
289 + p[NETMAP_BUF_POOL].memtotal);
291 return 0; // XXX bad address
295 netmap_mem_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags,
300 error = nmd->config(nmd);
303 if (nmd->flags & NETMAP_MEM_FINALIZED) {
304 *size = nmd->nm_totalsize;
308 for (i = 0; i < NETMAP_POOLS_NR; i++) {
309 struct netmap_obj_pool *p = nmd->pools + i;
310 *size += (p->_numclusters * p->_clustsize);
313 *memflags = nmd->flags;
321 * we store objects by kernel address, need to find the offset
322 * within the pool to export the value to userspace.
323 * Algorithm: scan until we find the cluster, then add the
324 * actual offset in the cluster
327 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
329 int i, k = p->_clustentries, n = p->objtotal;
332 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
333 const char *base = p->lut[i].vaddr;
334 ssize_t relofs = (const char *) vaddr - base;
336 if (relofs < 0 || relofs >= p->_clustsize)
340 ND("%s: return offset %d (cluster %d) for pointer %p",
341 p->name, ofs, i, vaddr);
344 D("address %p is not contained inside any cluster (%s)",
346 return 0; /* An error occurred */
349 /* Helper functions which convert virtual addresses to offsets */
350 #define netmap_if_offset(n, v) \
351 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
353 #define netmap_ring_offset(n, v) \
354 ((n)->pools[NETMAP_IF_POOL].memtotal + \
355 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
357 #define netmap_buf_offset(n, v) \
358 ((n)->pools[NETMAP_IF_POOL].memtotal + \
359 (n)->pools[NETMAP_RING_POOL].memtotal + \
360 netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)))
364 netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *addr)
368 v = netmap_if_offset(nmd, addr);
374 * report the index, and use start position as a hint,
375 * otherwise buffer allocation becomes terribly expensive.
378 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
380 uint32_t i = 0; /* index in the bitmap */
381 uint32_t mask, j; /* slot counter */
384 if (len > p->_objsize) {
385 D("%s request size %d too large", p->name, len);
386 // XXX cannot reduce the size
390 if (p->objfree == 0) {
391 D("no more %s objects", p->name);
397 /* termination is guaranteed by p->free, but better check bounds on i */
398 while (vaddr == NULL && i < p->bitmap_slots) {
399 uint32_t cur = p->bitmap[i];
400 if (cur == 0) { /* bitmask is fully used */
405 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
408 p->bitmap[i] &= ~mask; /* mark object as in use */
411 vaddr = p->lut[i * 32 + j].vaddr;
415 ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr);
424 * free by index, not by address.
425 * XXX should we also cleanup the content ?
428 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
432 if (j >= p->objtotal) {
433 D("invalid index %u, max %u", j, p->objtotal);
436 ptr = &p->bitmap[j / 32];
437 mask = (1 << (j % 32));
439 D("ouch, double free on buffer %d", j);
449 * free by address. This is slow but is only used for a few
450 * objects (rings, nifp)
453 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
455 u_int i, j, n = p->numclusters;
457 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
458 void *base = p->lut[i * p->_clustentries].vaddr;
459 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
461 /* Given address, is out of the scope of the current cluster.*/
462 if (vaddr < base || relofs >= p->_clustsize)
465 j = j + relofs / p->_objsize;
466 /* KASSERT(j != 0, ("Cannot free object 0")); */
467 netmap_obj_free(p, j);
470 D("address %p is not contained inside any cluster (%s)",
474 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
475 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
476 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
477 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
478 #define netmap_buf_malloc(n, _pos, _index) \
479 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], NETMAP_BDG_BUF_SIZE(n), _pos, _index)
483 /* Return the index associated to the given packet buffer */
484 #define netmap_buf_index(n, v) \
485 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
489 * allocate extra buffers in a linked list.
490 * returns the actual number.
493 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
495 struct netmap_mem_d *nmd = na->nm_mem;
496 uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
500 *head = 0; /* default, 'null' index ie empty list */
501 for (i = 0 ; i < n; i++) {
502 uint32_t cur = *head; /* save current head */
503 uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
505 D("no more buffers after %d of %d", i, n);
506 *head = cur; /* restore */
509 RD(5, "allocate buffer %d -> %d", *head, cur);
510 *p = cur; /* link to previous head */
519 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
521 struct lut_entry *lut = na->na_lut;
522 struct netmap_mem_d *nmd = na->nm_mem;
523 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
524 uint32_t i, cur, *buf;
526 D("freeing the extra list");
527 for (i = 0; head >=2 && head < p->objtotal; i++) {
529 buf = lut[head].vaddr;
532 if (netmap_obj_free(p, cur))
536 D("breaking with head %d", head);
537 D("freed %d buffers", i);
541 /* Return nonzero on error */
543 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
545 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
546 u_int i = 0; /* slot counter */
547 uint32_t pos = 0; /* slot in p->bitmap */
548 uint32_t index = 0; /* buffer index */
550 for (i = 0; i < n; i++) {
551 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
553 D("no more buffers after %d of %d", i, n);
556 slot[i].buf_idx = index;
557 slot[i].len = p->_objsize;
561 ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
567 netmap_obj_free(p, slot[i].buf_idx);
569 bzero(slot, n * sizeof(slot[0]));
574 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
576 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
579 for (i = 0; i < n; i++) {
580 slot[i].buf_idx = index;
581 slot[i].len = p->_objsize;
588 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
590 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
592 if (i < 2 || i >= p->objtotal) {
593 D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
596 netmap_obj_free(p, i);
601 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
605 for (i = 0; i < n; i++) {
606 if (slot[i].buf_idx > 2)
607 netmap_free_buf(nmd, slot[i].buf_idx);
612 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
618 free(p->bitmap, M_NETMAP);
622 size_t sz = p->_clustsize;
624 for (i = 0; i < p->objtotal; i += p->_clustentries) {
626 contigfree(p->lut[i].vaddr, sz, M_NETMAP);
628 bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
632 free(p->lut, M_NETMAP);
643 * Free all resources related to an allocator.
646 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
650 netmap_reset_obj_allocator(p);
654 * We receive a request for objtotal objects, of size objsize each.
655 * Internally we may round up both numbers, as we allocate objects
656 * in small clusters multiple of the page size.
657 * We need to keep track of objtotal and clustentries,
658 * as they are needed when freeing memory.
660 * XXX note -- userspace needs the buffers to be contiguous,
661 * so we cannot afford gaps at the end of a cluster.
665 /* call with NMA_LOCK held */
667 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
670 u_int clustsize; /* the cluster size, multiple of page size */
671 u_int clustentries; /* how many objects per entry */
673 /* we store the current request, so we can
674 * detect configuration changes later */
675 p->r_objtotal = objtotal;
676 p->r_objsize = objsize;
678 #define MAX_CLUSTSIZE (1<<17)
679 #define LINE_ROUND NM_CACHE_ALIGN // 64
680 if (objsize >= MAX_CLUSTSIZE) {
681 /* we could do it but there is no point */
682 D("unsupported allocation for %d bytes", objsize);
685 /* make sure objsize is a multiple of LINE_ROUND */
686 i = (objsize & (LINE_ROUND - 1));
688 D("XXX aligning object by %d bytes", LINE_ROUND - i);
689 objsize += LINE_ROUND - i;
691 if (objsize < p->objminsize || objsize > p->objmaxsize) {
692 D("requested objsize %d out of range [%d, %d]",
693 objsize, p->objminsize, p->objmaxsize);
696 if (objtotal < p->nummin || objtotal > p->nummax) {
697 D("requested objtotal %d out of range [%d, %d]",
698 objtotal, p->nummin, p->nummax);
702 * Compute number of objects using a brute-force approach:
703 * given a max cluster size,
704 * we try to fill it with objects keeping track of the
705 * wasted space to the next page boundary.
707 for (clustentries = 0, i = 1;; i++) {
708 u_int delta, used = i * objsize;
709 if (used > MAX_CLUSTSIZE)
711 delta = used % PAGE_SIZE;
712 if (delta == 0) { // exact solution
716 if (delta > ( (clustentries*objsize) % PAGE_SIZE) )
719 // D("XXX --- ouch, delta %d (bad for buffers)", delta);
720 /* compute clustsize and round to the next page */
721 clustsize = clustentries * objsize;
722 i = (clustsize & (PAGE_SIZE - 1));
724 clustsize += PAGE_SIZE - i;
726 D("objsize %d clustsize %d objects %d",
727 objsize, clustsize, clustentries);
730 * The number of clusters is n = ceil(objtotal/clustentries)
731 * objtotal' = n * clustentries
733 p->_clustentries = clustentries;
734 p->_clustsize = clustsize;
735 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
737 /* actual values (may be larger than requested) */
738 p->_objsize = objsize;
739 p->_objtotal = p->_numclusters * clustentries;
745 /* call with NMA_LOCK held */
747 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
749 int i; /* must be signed */
752 /* optimistically assume we have enough memory */
753 p->numclusters = p->_numclusters;
754 p->objtotal = p->_objtotal;
756 n = sizeof(struct lut_entry) * p->objtotal;
760 p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
762 if (p->lut == NULL) {
763 D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
767 /* Allocate the bitmap */
768 n = (p->objtotal + 31) / 32;
769 p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
770 if (p->bitmap == NULL) {
771 D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
778 * Allocate clusters, init pointers and bitmap
782 for (i = 0; i < (int)p->objtotal;) {
783 int lim = i + p->_clustentries;
786 clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
787 (size_t)0, -1UL, PAGE_SIZE, 0);
790 * If we get here, there is a severe memory shortage,
791 * so halve the allocated memory to reclaim some.
793 D("Unable to create cluster at %d for '%s' allocator",
795 if (i < 2) /* nothing to halve */
798 for (i--; i >= lim; i--) {
799 p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) );
800 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
801 contigfree(p->lut[i].vaddr,
806 /* we may have stopped in the middle of a cluster */
807 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
810 for (; i < lim; i++, clust += p->_objsize) {
811 p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) );
812 p->lut[i].vaddr = clust;
813 p->lut[i].paddr = vtophys(clust);
816 p->objfree = p->objtotal;
817 p->memtotal = p->numclusters * p->_clustsize;
821 D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
822 p->numclusters, p->_clustsize >> 10,
823 p->memtotal >> 10, p->name);
828 netmap_reset_obj_allocator(p);
832 /* call with lock held */
834 netmap_memory_config_changed(struct netmap_mem_d *nmd)
838 for (i = 0; i < NETMAP_POOLS_NR; i++) {
839 if (nmd->pools[i].r_objsize != netmap_params[i].size ||
840 nmd->pools[i].r_objtotal != netmap_params[i].num)
847 netmap_mem_reset_all(struct netmap_mem_d *nmd)
852 D("resetting %p", nmd);
853 for (i = 0; i < NETMAP_POOLS_NR; i++) {
854 netmap_reset_obj_allocator(&nmd->pools[i]);
856 nmd->flags &= ~NETMAP_MEM_FINALIZED;
860 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
863 if (nmd->flags & NETMAP_MEM_FINALIZED)
866 nmd->nm_totalsize = 0;
867 for (i = 0; i < NETMAP_POOLS_NR; i++) {
868 nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
871 nmd->nm_totalsize += nmd->pools[i].memtotal;
873 /* buffers 0 and 1 are reserved */
874 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
875 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
876 nmd->flags |= NETMAP_MEM_FINALIZED;
879 D("interfaces %d KB, rings %d KB, buffers %d MB",
880 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
881 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
882 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
885 D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
890 netmap_mem_reset_all(nmd);
897 netmap_mem_private_delete(struct netmap_mem_d *nmd)
902 D("deleting %p", nmd);
903 if (nmd->refcount > 0)
904 D("bug: deleting mem allocator with refcount=%d!", nmd->refcount);
905 nm_mem_release_id(nmd);
907 D("done deleting %p", nmd);
908 NMA_LOCK_DESTROY(nmd);
913 netmap_mem_private_config(struct netmap_mem_d *nmd)
915 /* nothing to do, we are configured on creation
916 * and configuration never changes thereafter
922 netmap_mem_private_finalize(struct netmap_mem_d *nmd)
927 err = netmap_mem_finalize_all(nmd);
934 netmap_mem_private_deref(struct netmap_mem_d *nmd)
937 if (--nmd->refcount <= 0)
938 netmap_mem_reset_all(nmd);
944 * allocator for private memory
946 struct netmap_mem_d *
947 netmap_mem_private_new(const char *name, u_int txr, u_int txd,
948 u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr)
950 struct netmap_mem_d *d = NULL;
951 struct netmap_obj_params p[NETMAP_POOLS_NR];
955 d = malloc(sizeof(struct netmap_mem_d),
956 M_DEVBUF, M_NOWAIT | M_ZERO);
964 err = nm_mem_assign_id(d);
968 /* account for the fake host rings */
972 /* copy the min values */
973 for (i = 0; i < NETMAP_POOLS_NR; i++) {
974 p[i] = netmap_min_priv_params[i];
977 /* possibly increase them to fit user request */
978 v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
979 if (p[NETMAP_IF_POOL].size < v)
980 p[NETMAP_IF_POOL].size = v;
982 if (p[NETMAP_IF_POOL].num < v)
983 p[NETMAP_IF_POOL].num = v;
984 maxd = (txd > rxd) ? txd : rxd;
985 v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
986 if (p[NETMAP_RING_POOL].size < v)
987 p[NETMAP_RING_POOL].size = v;
988 /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
989 * and two rx rings (again, 1 normal and 1 fake host)
991 v = txr + rxr + 8 * npipes;
992 if (p[NETMAP_RING_POOL].num < v)
993 p[NETMAP_RING_POOL].num = v;
994 /* for each pipe we only need the buffers for the 4 "real" rings.
995 * On the other end, the pipe ring dimension may be different from
996 * the parent port ring dimension. As a compromise, we allocate twice the
997 * space actually needed if the pipe rings were the same size as the parent rings
999 v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1000 /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1001 if (p[NETMAP_BUF_POOL].num < v)
1002 p[NETMAP_BUF_POOL].num = v;
1005 D("req if %d*%d ring %d*%d buf %d*%d",
1006 p[NETMAP_IF_POOL].num,
1007 p[NETMAP_IF_POOL].size,
1008 p[NETMAP_RING_POOL].num,
1009 p[NETMAP_RING_POOL].size,
1010 p[NETMAP_BUF_POOL].num,
1011 p[NETMAP_BUF_POOL].size);
1013 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1014 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1015 nm_blueprint.pools[i].name,
1017 err = netmap_config_obj_allocator(&d->pools[i],
1018 p[i].num, p[i].size);
1023 d->flags &= ~NETMAP_MEM_FINALIZED;
1029 netmap_mem_private_delete(d);
1036 /* call with lock held */
1038 netmap_mem_global_config(struct netmap_mem_d *nmd)
1043 /* already in use, we cannot change the configuration */
1046 if (!netmap_memory_config_changed(nmd))
1051 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1052 /* reset previous allocation */
1053 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1054 netmap_reset_obj_allocator(&nmd->pools[i]);
1056 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1059 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1060 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1061 netmap_params[i].num, netmap_params[i].size);
1068 return nmd->lasterr;
1072 netmap_mem_global_finalize(struct netmap_mem_d *nmd)
1079 /* update configuration if changed */
1080 if (netmap_mem_global_config(nmd))
1085 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1086 /* may happen if config is not changed */
1087 ND("nothing to do");
1091 if (netmap_mem_finalize_all(nmd))
1094 /* backward compatibility */
1095 netmap_buf_size = nmd->pools[NETMAP_BUF_POOL]._objsize;
1096 netmap_total_buffers = nmd->pools[NETMAP_BUF_POOL].objtotal;
1098 netmap_buffer_lut = nmd->pools[NETMAP_BUF_POOL].lut;
1099 netmap_buffer_base = nmd->pools[NETMAP_BUF_POOL].lut[0].vaddr;
1115 netmap_mem_init(void)
1117 NMA_LOCK_INIT(&nm_mem);
1122 netmap_mem_fini(void)
1126 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1127 netmap_destroy_obj_allocator(&nm_mem.pools[i]);
1129 NMA_LOCK_DESTROY(&nm_mem);
1133 netmap_free_rings(struct netmap_adapter *na)
1135 struct netmap_kring *kring;
1136 struct netmap_ring *ring;
1139 for (kring = na->tx_rings; kring != na->rx_rings; kring++) {
1143 netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1144 netmap_ring_free(na->nm_mem, ring);
1147 for (/* cont'd from above */; kring != na->tailroom; kring++) {
1151 netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1152 netmap_ring_free(na->nm_mem, ring);
1157 /* call with NMA_LOCK held *
1159 * Allocate netmap rings and buffers for this card
1160 * The rings are contiguous, but have variable size.
1161 * The kring array must follow the layout described
1162 * in netmap_krings_create().
1165 netmap_mem_rings_create(struct netmap_adapter *na)
1167 struct netmap_ring *ring;
1169 struct netmap_kring *kring;
1172 NMA_LOCK(na->nm_mem);
1174 /* transmit rings */
1175 for (i =0, kring = na->tx_rings; kring != na->rx_rings; kring++, i++) {
1177 ND("%s %ld already created", kring->name, kring - na->tx_rings);
1178 continue; /* already created by somebody else */
1180 ndesc = kring->nkr_num_slots;
1181 len = sizeof(struct netmap_ring) +
1182 ndesc * sizeof(struct netmap_slot);
1183 ring = netmap_ring_malloc(na->nm_mem, len);
1185 D("Cannot allocate tx_ring");
1188 ND("txring at %p", ring);
1190 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1191 *(int64_t *)(uintptr_t)&ring->buf_ofs =
1192 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1193 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1194 netmap_ring_offset(na->nm_mem, ring);
1196 /* copy values from kring */
1197 ring->head = kring->rhead;
1198 ring->cur = kring->rcur;
1199 ring->tail = kring->rtail;
1200 *(uint16_t *)(uintptr_t)&ring->nr_buf_size =
1201 NETMAP_BDG_BUF_SIZE(na->nm_mem);
1202 ND("%s h %d c %d t %d", kring->name,
1203 ring->head, ring->cur, ring->tail);
1204 ND("initializing slots for txring");
1205 if (i != na->num_tx_rings || (na->na_flags & NAF_HOST_RINGS)) {
1206 /* this is a real ring */
1207 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1208 D("Cannot allocate buffers for tx_ring");
1212 /* this is a fake tx ring, set all indices to 0 */
1213 netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1218 for ( i = 0 /* kring cont'd from above */ ; kring != na->tailroom; kring++, i++) {
1220 ND("%s %ld already created", kring->name, kring - na->rx_rings);
1221 continue; /* already created by somebody else */
1223 ndesc = kring->nkr_num_slots;
1224 len = sizeof(struct netmap_ring) +
1225 ndesc * sizeof(struct netmap_slot);
1226 ring = netmap_ring_malloc(na->nm_mem, len);
1228 D("Cannot allocate rx_ring");
1231 ND("rxring at %p", ring);
1233 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1234 *(int64_t *)(uintptr_t)&ring->buf_ofs =
1235 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1236 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1237 netmap_ring_offset(na->nm_mem, ring);
1239 /* copy values from kring */
1240 ring->head = kring->rhead;
1241 ring->cur = kring->rcur;
1242 ring->tail = kring->rtail;
1243 *(int *)(uintptr_t)&ring->nr_buf_size =
1244 NETMAP_BDG_BUF_SIZE(na->nm_mem);
1245 ND("%s h %d c %d t %d", kring->name,
1246 ring->head, ring->cur, ring->tail);
1247 ND("initializing slots for rxring %p", ring);
1248 if (i != na->num_rx_rings || (na->na_flags & NAF_HOST_RINGS)) {
1249 /* this is a real ring */
1250 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1251 D("Cannot allocate buffers for rx_ring");
1255 /* this is a fake rx ring, set all indices to 1 */
1256 netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 1);
1260 NMA_UNLOCK(na->nm_mem);
1265 netmap_free_rings(na);
1267 NMA_UNLOCK(na->nm_mem);
1273 netmap_mem_rings_delete(struct netmap_adapter *na)
1275 /* last instance, release bufs and rings */
1276 NMA_LOCK(na->nm_mem);
1278 netmap_free_rings(na);
1280 NMA_UNLOCK(na->nm_mem);
1284 /* call with NMA_LOCK held */
1286 * Allocate the per-fd structure netmap_if.
1288 * We assume that the configuration stored in na
1289 * (number of tx/rx rings and descs) does not change while
1290 * the interface is in netmap mode.
1293 netmap_mem_if_new(const char *ifname, struct netmap_adapter *na)
1295 struct netmap_if *nifp;
1296 ssize_t base; /* handy for relative offsets between rings and nifp */
1297 u_int i, len, ntx, nrx;
1299 /* account for the (eventually fake) host rings */
1300 ntx = na->num_tx_rings + 1;
1301 nrx = na->num_rx_rings + 1;
1303 * the descriptor is followed inline by an array of offsets
1304 * to the tx and rx rings in the shared memory region.
1307 NMA_LOCK(na->nm_mem);
1309 len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
1310 nifp = netmap_if_malloc(na->nm_mem, len);
1312 NMA_UNLOCK(na->nm_mem);
1316 /* initialize base fields -- override const */
1317 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1318 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1319 strncpy(nifp->ni_name, ifname, (size_t)IFNAMSIZ);
1322 * fill the slots for the rx and tx rings. They contain the offset
1323 * between the ring and nifp, so the information is usable in
1324 * userspace to reach the ring from the nifp.
1326 base = netmap_if_offset(na->nm_mem, nifp);
1327 for (i = 0; i < ntx; i++) {
1328 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1329 netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1331 for (i = 0; i < nrx; i++) {
1332 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
1333 netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1336 NMA_UNLOCK(na->nm_mem);
1342 netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1347 NMA_LOCK(na->nm_mem);
1348 if (nifp->ni_bufs_head)
1349 netmap_extra_free(na, nifp->ni_bufs_head);
1350 netmap_if_free(na->nm_mem, nifp);
1352 NMA_UNLOCK(na->nm_mem);
1356 netmap_mem_global_deref(struct netmap_mem_d *nmd)
1362 D("refcount = %d", nmd->refcount);
1368 netmap_mem_finalize(struct netmap_mem_d *nmd)
1370 return nmd->finalize(nmd);
1374 netmap_mem_deref(struct netmap_mem_d *nmd)
1376 return nmd->deref(nmd);