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"
57 #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */
59 #define NETMAP_POOL_MAX_NAMSZ 32
70 struct netmap_obj_params {
75 struct netmap_obj_pool {
76 char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */
78 /* ---------------------------------------------------*/
79 /* these are only meaningful if the pool is finalized */
80 /* (see 'finalized' field in netmap_mem_d) */
81 u_int objtotal; /* actual total number of objects. */
82 u_int memtotal; /* actual total memory space */
83 u_int numclusters; /* actual number of clusters */
85 u_int objfree; /* number of free objects. */
87 struct lut_entry *lut; /* virt,phys addresses, objtotal entries */
88 uint32_t *bitmap; /* one bit per buffer, 1 means free */
89 uint32_t bitmap_slots; /* number of uint32 entries in bitmap */
90 /* ---------------------------------------------------*/
93 u_int objminsize; /* minimum object size */
94 u_int objmaxsize; /* maximum object size */
95 u_int nummin; /* minimum number of objects */
96 u_int nummax; /* maximum number of objects */
98 /* these are changed only by config */
99 u_int _objtotal; /* total number of objects */
100 u_int _objsize; /* object size */
101 u_int _clustsize; /* cluster size */
102 u_int _clustentries; /* objects per cluster */
103 u_int _numclusters; /* number of clusters */
105 /* requested values */
110 #define NMA_LOCK_T NM_MTX_T
113 struct netmap_mem_ops {
114 void (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
115 int (*nmd_get_info)(struct netmap_mem_d *, u_int *size,
116 u_int *memflags, uint16_t *id);
118 vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
119 int (*nmd_config)(struct netmap_mem_d *);
120 int (*nmd_finalize)(struct netmap_mem_d *);
121 void (*nmd_deref)(struct netmap_mem_d *);
122 ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
123 void (*nmd_delete)(struct netmap_mem_d *);
125 struct netmap_if * (*nmd_if_new)(struct netmap_adapter *);
126 void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
127 int (*nmd_rings_create)(struct netmap_adapter *);
128 void (*nmd_rings_delete)(struct netmap_adapter *);
131 typedef uint16_t nm_memid_t;
133 struct netmap_mem_d {
134 NMA_LOCK_T nm_mtx; /* protect the allocator */
135 u_int nm_totalsize; /* shorthand */
138 #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */
139 int lasterr; /* last error for curr config */
140 int active; /* active users */
142 /* the three allocators */
143 struct netmap_obj_pool pools[NETMAP_POOLS_NR];
145 nm_memid_t nm_id; /* allocator identifier */
146 int nm_grp; /* iommu groupd id */
148 /* list of all existing allocators, sorted by nm_id */
149 struct netmap_mem_d *prev, *next;
151 struct netmap_mem_ops *ops;
154 #define NMD_DEFCB(t0, name) \
156 netmap_mem_##name(struct netmap_mem_d *nmd) \
158 return nmd->ops->nmd_##name(nmd); \
161 #define NMD_DEFCB1(t0, name, t1) \
163 netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1) \
165 return nmd->ops->nmd_##name(nmd, a1); \
168 #define NMD_DEFCB3(t0, name, t1, t2, t3) \
170 netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1, t2 a2, t3 a3) \
172 return nmd->ops->nmd_##name(nmd, a1, a2, a3); \
175 #define NMD_DEFNACB(t0, name) \
177 netmap_mem_##name(struct netmap_adapter *na) \
179 return na->nm_mem->ops->nmd_##name(na); \
182 #define NMD_DEFNACB1(t0, name, t1) \
184 netmap_mem_##name(struct netmap_adapter *na, t1 a1) \
186 return na->nm_mem->ops->nmd_##name(na, a1); \
189 NMD_DEFCB1(void, get_lut, struct netmap_lut *);
190 NMD_DEFCB3(int, get_info, u_int *, u_int *, uint16_t *);
191 NMD_DEFCB1(vm_paddr_t, ofstophys, vm_ooffset_t);
192 static int netmap_mem_config(struct netmap_mem_d *);
193 NMD_DEFCB(int, config);
194 NMD_DEFCB1(ssize_t, if_offset, const void *);
195 NMD_DEFCB(void, delete);
197 NMD_DEFNACB(struct netmap_if *, if_new);
198 NMD_DEFNACB1(void, if_delete, struct netmap_if *);
199 NMD_DEFNACB(int, rings_create);
200 NMD_DEFNACB(void, rings_delete);
202 static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
203 static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
204 static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
206 #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx)
207 #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx)
208 #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx)
209 #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx)
211 #ifdef NM_DEBUG_MEM_PUTGET
212 #define NM_DBG_REFC(nmd, func, line) \
213 printf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
215 #define NM_DBG_REFC(nmd, func, line)
218 #ifdef NM_DEBUG_MEM_PUTGET
219 void __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
221 void netmap_mem_get(struct netmap_mem_d *nmd)
226 NM_DBG_REFC(nmd, func, line);
230 #ifdef NM_DEBUG_MEM_PUTGET
231 void __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
233 void netmap_mem_put(struct netmap_mem_d *nmd)
238 last = (--nmd->refcount == 0);
239 NM_DBG_REFC(nmd, func, line);
242 netmap_mem_delete(nmd);
246 netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
248 if (nm_mem_assign_group(nmd, na->pdev) < 0) {
251 nmd->ops->nmd_finalize(nmd);
254 if (!nmd->lasterr && na->pdev)
255 netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
261 netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
264 netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
266 return nmd->ops->nmd_deref(nmd);
270 /* accessor functions */
272 netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
274 lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
275 lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
276 lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
279 struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = {
284 [NETMAP_RING_POOL] = {
288 [NETMAP_BUF_POOL] = {
290 .num = NETMAP_BUF_MAX_NUM,
294 struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
299 [NETMAP_RING_POOL] = {
303 [NETMAP_BUF_POOL] = {
311 * nm_mem is the memory allocator used for all physical interfaces
312 * running in netmap mode.
313 * Virtual (VALE) ports will have each its own allocator.
315 extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
316 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
320 .objminsize = sizeof(struct netmap_if),
322 .nummin = 10, /* don't be stingy */
323 .nummax = 10000, /* XXX very large */
325 [NETMAP_RING_POOL] = {
326 .name = "netmap_ring",
327 .objminsize = sizeof(struct netmap_ring),
328 .objmaxsize = 32*PAGE_SIZE,
332 [NETMAP_BUF_POOL] = {
333 .name = "netmap_buf",
337 .nummax = 1000000, /* one million! */
347 .ops = &netmap_mem_global_ops
351 struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
353 /* blueprint for the private memory allocators */
354 extern struct netmap_mem_ops netmap_mem_private_ops; /* forward */
355 const struct netmap_mem_d nm_blueprint = {
359 .objminsize = sizeof(struct netmap_if),
364 [NETMAP_RING_POOL] = {
366 .objminsize = sizeof(struct netmap_ring),
367 .objmaxsize = 32*PAGE_SIZE,
371 [NETMAP_BUF_POOL] = {
376 .nummax = 1000000, /* one million! */
380 .flags = NETMAP_MEM_PRIVATE,
382 .ops = &netmap_mem_private_ops
385 /* memory allocator related sysctls */
387 #define STRINGIFY(x) #x
390 #define DECLARE_SYSCTLS(id, name) \
391 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
392 CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
393 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
394 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
395 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
396 CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
397 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
398 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
399 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
400 CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
401 "Default size of private netmap " STRINGIFY(name) "s"); \
402 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
403 CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
404 "Default number of private netmap " STRINGIFY(name) "s")
406 SYSCTL_DECL(_dev_netmap);
407 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
408 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
409 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
412 nm_mem_assign_id(struct netmap_mem_d *nmd)
415 struct netmap_mem_d *scan = netmap_last_mem_d;
421 /* we rely on unsigned wrap around */
422 id = scan->nm_id + 1;
423 if (id == 0) /* reserve 0 as error value */
426 if (id != scan->nm_id) {
428 nmd->prev = scan->prev;
430 scan->prev->next = nmd;
432 netmap_last_mem_d = nmd;
436 } while (scan != netmap_last_mem_d);
443 nm_mem_release_id(struct netmap_mem_d *nmd)
447 nmd->prev->next = nmd->next;
448 nmd->next->prev = nmd->prev;
450 if (netmap_last_mem_d == nmd)
451 netmap_last_mem_d = nmd->prev;
453 nmd->prev = nmd->next = NULL;
459 nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
462 id = nm_iommu_group_id(dev);
464 D("iommu_group %d", id);
471 if (nmd->nm_grp != id)
472 nmd->lasterr = err = ENOMEM;
479 * First, find the allocator that contains the requested offset,
480 * then locate the cluster through a lookup table.
483 netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
486 vm_ooffset_t o = offset;
488 struct netmap_obj_pool *p;
493 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
494 if (offset >= p[i].memtotal)
496 // now lookup the cluster's address
497 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
498 offset % p[i]._objsize;
502 /* this is only in case of errors */
503 D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
504 p[NETMAP_IF_POOL].memtotal,
505 p[NETMAP_IF_POOL].memtotal
506 + p[NETMAP_RING_POOL].memtotal,
507 p[NETMAP_IF_POOL].memtotal
508 + p[NETMAP_RING_POOL].memtotal
509 + p[NETMAP_BUF_POOL].memtotal);
511 return 0; // XXX bad address
515 netmap_mem2_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags,
520 error = netmap_mem_config(nmd);
524 if (nmd->flags & NETMAP_MEM_FINALIZED) {
525 *size = nmd->nm_totalsize;
529 for (i = 0; i < NETMAP_POOLS_NR; i++) {
530 struct netmap_obj_pool *p = nmd->pools + i;
531 *size += (p->_numclusters * p->_clustsize);
536 *memflags = nmd->flags;
545 * we store objects by kernel address, need to find the offset
546 * within the pool to export the value to userspace.
547 * Algorithm: scan until we find the cluster, then add the
548 * actual offset in the cluster
551 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
553 int i, k = p->_clustentries, n = p->objtotal;
556 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
557 const char *base = p->lut[i].vaddr;
558 ssize_t relofs = (const char *) vaddr - base;
560 if (relofs < 0 || relofs >= p->_clustsize)
564 ND("%s: return offset %d (cluster %d) for pointer %p",
565 p->name, ofs, i, vaddr);
568 D("address %p is not contained inside any cluster (%s)",
570 return 0; /* An error occurred */
573 /* Helper functions which convert virtual addresses to offsets */
574 #define netmap_if_offset(n, v) \
575 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
577 #define netmap_ring_offset(n, v) \
578 ((n)->pools[NETMAP_IF_POOL].memtotal + \
579 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
581 #define netmap_buf_offset(n, v) \
582 ((n)->pools[NETMAP_IF_POOL].memtotal + \
583 (n)->pools[NETMAP_RING_POOL].memtotal + \
584 netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)))
588 netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
592 v = netmap_if_offset(nmd, addr);
598 * report the index, and use start position as a hint,
599 * otherwise buffer allocation becomes terribly expensive.
602 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
604 uint32_t i = 0; /* index in the bitmap */
605 uint32_t mask, j; /* slot counter */
608 if (len > p->_objsize) {
609 D("%s request size %d too large", p->name, len);
610 // XXX cannot reduce the size
614 if (p->objfree == 0) {
615 D("no more %s objects", p->name);
621 /* termination is guaranteed by p->free, but better check bounds on i */
622 while (vaddr == NULL && i < p->bitmap_slots) {
623 uint32_t cur = p->bitmap[i];
624 if (cur == 0) { /* bitmask is fully used */
629 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
632 p->bitmap[i] &= ~mask; /* mark object as in use */
635 vaddr = p->lut[i * 32 + j].vaddr;
639 ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr);
648 * free by index, not by address.
649 * XXX should we also cleanup the content ?
652 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
656 if (j >= p->objtotal) {
657 D("invalid index %u, max %u", j, p->objtotal);
660 ptr = &p->bitmap[j / 32];
661 mask = (1 << (j % 32));
663 D("ouch, double free on buffer %d", j);
673 * free by address. This is slow but is only used for a few
674 * objects (rings, nifp)
677 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
679 u_int i, j, n = p->numclusters;
681 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
682 void *base = p->lut[i * p->_clustentries].vaddr;
683 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
685 /* Given address, is out of the scope of the current cluster.*/
686 if (vaddr < base || relofs >= p->_clustsize)
689 j = j + relofs / p->_objsize;
690 /* KASSERT(j != 0, ("Cannot free object 0")); */
691 netmap_obj_free(p, j);
694 D("address %p is not contained inside any cluster (%s)",
698 #define netmap_mem_bufsize(n) \
699 ((n)->pools[NETMAP_BUF_POOL]._objsize)
701 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
702 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
703 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
704 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
705 #define netmap_buf_malloc(n, _pos, _index) \
706 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
710 /* Return the index associated to the given packet buffer */
711 #define netmap_buf_index(n, v) \
712 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
716 * allocate extra buffers in a linked list.
717 * returns the actual number.
720 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
722 struct netmap_mem_d *nmd = na->nm_mem;
723 uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
727 *head = 0; /* default, 'null' index ie empty list */
728 for (i = 0 ; i < n; i++) {
729 uint32_t cur = *head; /* save current head */
730 uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
732 D("no more buffers after %d of %d", i, n);
733 *head = cur; /* restore */
736 RD(5, "allocate buffer %d -> %d", *head, cur);
737 *p = cur; /* link to previous head */
746 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
748 struct lut_entry *lut = na->na_lut.lut;
749 struct netmap_mem_d *nmd = na->nm_mem;
750 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
751 uint32_t i, cur, *buf;
753 D("freeing the extra list");
754 for (i = 0; head >=2 && head < p->objtotal; i++) {
756 buf = lut[head].vaddr;
759 if (netmap_obj_free(p, cur))
763 D("breaking with head %d", head);
764 D("freed %d buffers", i);
768 /* Return nonzero on error */
770 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
772 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
773 u_int i = 0; /* slot counter */
774 uint32_t pos = 0; /* slot in p->bitmap */
775 uint32_t index = 0; /* buffer index */
777 for (i = 0; i < n; i++) {
778 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
780 D("no more buffers after %d of %d", i, n);
783 slot[i].buf_idx = index;
784 slot[i].len = p->_objsize;
788 ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
794 netmap_obj_free(p, slot[i].buf_idx);
796 bzero(slot, n * sizeof(slot[0]));
801 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
803 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
806 for (i = 0; i < n; i++) {
807 slot[i].buf_idx = index;
808 slot[i].len = p->_objsize;
815 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
817 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
819 if (i < 2 || i >= p->objtotal) {
820 D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
823 netmap_obj_free(p, i);
828 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
832 for (i = 0; i < n; i++) {
833 if (slot[i].buf_idx > 2)
834 netmap_free_buf(nmd, slot[i].buf_idx);
839 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
845 free(p->bitmap, M_NETMAP);
849 size_t sz = p->_clustsize;
852 * Free each cluster allocated in
853 * netmap_finalize_obj_allocator(). The cluster start
854 * addresses are stored at multiples of p->_clusterentries
857 for (i = 0; i < p->objtotal; i += p->_clustentries) {
859 contigfree(p->lut[i].vaddr, sz, M_NETMAP);
861 bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
865 free(p->lut, M_NETMAP);
876 * Free all resources related to an allocator.
879 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
883 netmap_reset_obj_allocator(p);
887 * We receive a request for objtotal objects, of size objsize each.
888 * Internally we may round up both numbers, as we allocate objects
889 * in small clusters multiple of the page size.
890 * We need to keep track of objtotal and clustentries,
891 * as they are needed when freeing memory.
893 * XXX note -- userspace needs the buffers to be contiguous,
894 * so we cannot afford gaps at the end of a cluster.
898 /* call with NMA_LOCK held */
900 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
903 u_int clustsize; /* the cluster size, multiple of page size */
904 u_int clustentries; /* how many objects per entry */
906 /* we store the current request, so we can
907 * detect configuration changes later */
908 p->r_objtotal = objtotal;
909 p->r_objsize = objsize;
911 #define MAX_CLUSTSIZE (1<<22) // 4 MB
912 #define LINE_ROUND NM_CACHE_ALIGN // 64
913 if (objsize >= MAX_CLUSTSIZE) {
914 /* we could do it but there is no point */
915 D("unsupported allocation for %d bytes", objsize);
918 /* make sure objsize is a multiple of LINE_ROUND */
919 i = (objsize & (LINE_ROUND - 1));
921 D("XXX aligning object by %d bytes", LINE_ROUND - i);
922 objsize += LINE_ROUND - i;
924 if (objsize < p->objminsize || objsize > p->objmaxsize) {
925 D("requested objsize %d out of range [%d, %d]",
926 objsize, p->objminsize, p->objmaxsize);
929 if (objtotal < p->nummin || objtotal > p->nummax) {
930 D("requested objtotal %d out of range [%d, %d]",
931 objtotal, p->nummin, p->nummax);
935 * Compute number of objects using a brute-force approach:
936 * given a max cluster size,
937 * we try to fill it with objects keeping track of the
938 * wasted space to the next page boundary.
940 for (clustentries = 0, i = 1;; i++) {
941 u_int delta, used = i * objsize;
942 if (used > MAX_CLUSTSIZE)
944 delta = used % PAGE_SIZE;
945 if (delta == 0) { // exact solution
950 /* exact solution not found */
951 if (clustentries == 0) {
952 D("unsupported allocation for %d bytes", objsize);
955 /* compute clustsize */
956 clustsize = clustentries * objsize;
958 D("objsize %d clustsize %d objects %d",
959 objsize, clustsize, clustentries);
962 * The number of clusters is n = ceil(objtotal/clustentries)
963 * objtotal' = n * clustentries
965 p->_clustentries = clustentries;
966 p->_clustsize = clustsize;
967 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
969 /* actual values (may be larger than requested) */
970 p->_objsize = objsize;
971 p->_objtotal = p->_numclusters * clustentries;
977 /* call with NMA_LOCK held */
979 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
981 int i; /* must be signed */
984 /* optimistically assume we have enough memory */
985 p->numclusters = p->_numclusters;
986 p->objtotal = p->_objtotal;
988 n = sizeof(struct lut_entry) * p->objtotal;
992 p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
994 if (p->lut == NULL) {
995 D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
999 /* Allocate the bitmap */
1000 n = (p->objtotal + 31) / 32;
1001 p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
1002 if (p->bitmap == NULL) {
1003 D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
1007 p->bitmap_slots = n;
1010 * Allocate clusters, init pointers and bitmap
1014 for (i = 0; i < (int)p->objtotal;) {
1015 int lim = i + p->_clustentries;
1018 clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1019 (size_t)0, -1UL, PAGE_SIZE, 0);
1020 if (clust == NULL) {
1022 * If we get here, there is a severe memory shortage,
1023 * so halve the allocated memory to reclaim some.
1025 D("Unable to create cluster at %d for '%s' allocator",
1027 if (i < 2) /* nothing to halve */
1030 for (i--; i >= lim; i--) {
1031 p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) );
1032 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1033 contigfree(p->lut[i].vaddr,
1035 p->lut[i].vaddr = NULL;
1039 /* we may have stopped in the middle of a cluster */
1040 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1044 * Set bitmap and lut state for all buffers in the current
1047 * [i, lim) is the set of buffer indexes that cover the
1050 * 'clust' is really the address of the current buffer in
1051 * the current cluster as we index through it with a stride
1054 for (; i < lim; i++, clust += p->_objsize) {
1055 p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) );
1056 p->lut[i].vaddr = clust;
1057 p->lut[i].paddr = vtophys(clust);
1060 p->objfree = p->objtotal;
1061 p->memtotal = p->numclusters * p->_clustsize;
1062 if (p->objfree == 0)
1065 D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1066 p->numclusters, p->_clustsize >> 10,
1067 p->memtotal >> 10, p->name);
1072 netmap_reset_obj_allocator(p);
1076 /* call with lock held */
1078 netmap_memory_config_changed(struct netmap_mem_d *nmd)
1082 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1083 if (nmd->pools[i].r_objsize != netmap_params[i].size ||
1084 nmd->pools[i].r_objtotal != netmap_params[i].num)
1091 netmap_mem_reset_all(struct netmap_mem_d *nmd)
1096 D("resetting %p", nmd);
1097 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1098 netmap_reset_obj_allocator(&nmd->pools[i]);
1100 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1104 netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
1106 int i, lim = p->_objtotal;
1108 if (na->pdev == NULL)
1114 D("unsupported on FreeBSD");
1116 for (i = 2; i < lim; i++) {
1117 netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr);
1125 netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1128 D("unsupported on FreeBSD");
1130 int i, lim = p->_objtotal;
1132 if (na->pdev == NULL)
1135 for (i = 2; i < lim; i++) {
1136 netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr,
1145 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1148 if (nmd->flags & NETMAP_MEM_FINALIZED)
1151 nmd->nm_totalsize = 0;
1152 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1153 nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1156 nmd->nm_totalsize += nmd->pools[i].memtotal;
1158 /* buffers 0 and 1 are reserved */
1159 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1160 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1161 nmd->flags |= NETMAP_MEM_FINALIZED;
1164 D("interfaces %d KB, rings %d KB, buffers %d MB",
1165 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1166 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1167 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1170 D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1175 netmap_mem_reset_all(nmd);
1176 return nmd->lasterr;
1182 netmap_mem_private_delete(struct netmap_mem_d *nmd)
1187 D("deleting %p", nmd);
1188 if (nmd->active > 0)
1189 D("bug: deleting mem allocator with active=%d!", nmd->active);
1190 nm_mem_release_id(nmd);
1192 D("done deleting %p", nmd);
1193 NMA_LOCK_DESTROY(nmd);
1194 free(nmd, M_DEVBUF);
1198 netmap_mem_private_config(struct netmap_mem_d *nmd)
1200 /* nothing to do, we are configured on creation
1201 * and configuration never changes thereafter
1207 netmap_mem_private_finalize(struct netmap_mem_d *nmd)
1212 err = netmap_mem_finalize_all(nmd);
1219 netmap_mem_private_deref(struct netmap_mem_d *nmd)
1222 if (--nmd->active <= 0)
1223 netmap_mem_reset_all(nmd);
1229 * allocator for private memory
1231 struct netmap_mem_d *
1232 netmap_mem_private_new(const char *name, u_int txr, u_int txd,
1233 u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr)
1235 struct netmap_mem_d *d = NULL;
1236 struct netmap_obj_params p[NETMAP_POOLS_NR];
1240 d = malloc(sizeof(struct netmap_mem_d),
1241 M_DEVBUF, M_NOWAIT | M_ZERO);
1249 err = nm_mem_assign_id(d);
1253 /* account for the fake host rings */
1257 /* copy the min values */
1258 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1259 p[i] = netmap_min_priv_params[i];
1262 /* possibly increase them to fit user request */
1263 v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1264 if (p[NETMAP_IF_POOL].size < v)
1265 p[NETMAP_IF_POOL].size = v;
1267 if (p[NETMAP_IF_POOL].num < v)
1268 p[NETMAP_IF_POOL].num = v;
1269 maxd = (txd > rxd) ? txd : rxd;
1270 v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1271 if (p[NETMAP_RING_POOL].size < v)
1272 p[NETMAP_RING_POOL].size = v;
1273 /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1274 * and two rx rings (again, 1 normal and 1 fake host)
1276 v = txr + rxr + 8 * npipes;
1277 if (p[NETMAP_RING_POOL].num < v)
1278 p[NETMAP_RING_POOL].num = v;
1279 /* for each pipe we only need the buffers for the 4 "real" rings.
1280 * On the other end, the pipe ring dimension may be different from
1281 * the parent port ring dimension. As a compromise, we allocate twice the
1282 * space actually needed if the pipe rings were the same size as the parent rings
1284 v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1285 /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1286 if (p[NETMAP_BUF_POOL].num < v)
1287 p[NETMAP_BUF_POOL].num = v;
1290 D("req if %d*%d ring %d*%d buf %d*%d",
1291 p[NETMAP_IF_POOL].num,
1292 p[NETMAP_IF_POOL].size,
1293 p[NETMAP_RING_POOL].num,
1294 p[NETMAP_RING_POOL].size,
1295 p[NETMAP_BUF_POOL].num,
1296 p[NETMAP_BUF_POOL].size);
1298 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1299 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1300 nm_blueprint.pools[i].name,
1302 err = netmap_config_obj_allocator(&d->pools[i],
1303 p[i].num, p[i].size);
1308 d->flags &= ~NETMAP_MEM_FINALIZED;
1314 netmap_mem_private_delete(d);
1321 /* call with lock held */
1323 netmap_mem_global_config(struct netmap_mem_d *nmd)
1328 /* already in use, we cannot change the configuration */
1331 if (!netmap_memory_config_changed(nmd))
1334 ND("reconfiguring");
1336 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1337 /* reset previous allocation */
1338 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1339 netmap_reset_obj_allocator(&nmd->pools[i]);
1341 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1344 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1345 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1346 netmap_params[i].num, netmap_params[i].size);
1353 return nmd->lasterr;
1357 netmap_mem_global_finalize(struct netmap_mem_d *nmd)
1361 /* update configuration if changed */
1362 if (netmap_mem_global_config(nmd))
1367 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1368 /* may happen if config is not changed */
1369 ND("nothing to do");
1373 if (netmap_mem_finalize_all(nmd))
1388 netmap_mem_global_delete(struct netmap_mem_d *nmd)
1392 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1393 netmap_destroy_obj_allocator(&nm_mem.pools[i]);
1396 NMA_LOCK_DESTROY(&nm_mem);
1400 netmap_mem_init(void)
1402 NMA_LOCK_INIT(&nm_mem);
1403 netmap_mem_get(&nm_mem);
1408 netmap_mem_fini(void)
1410 netmap_mem_put(&nm_mem);
1414 netmap_free_rings(struct netmap_adapter *na)
1420 for (i = 0; i < netmap_real_rings(na, t); i++) {
1421 struct netmap_kring *kring = &NMR(na, t)[i];
1422 struct netmap_ring *ring = kring->ring;
1426 netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1427 netmap_ring_free(na->nm_mem, ring);
1433 /* call with NMA_LOCK held *
1435 * Allocate netmap rings and buffers for this card
1436 * The rings are contiguous, but have variable size.
1437 * The kring array must follow the layout described
1438 * in netmap_krings_create().
1441 netmap_mem2_rings_create(struct netmap_adapter *na)
1445 NMA_LOCK(na->nm_mem);
1450 for (i = 0; i <= nma_get_nrings(na, t); i++) {
1451 struct netmap_kring *kring = &NMR(na, t)[i];
1452 struct netmap_ring *ring = kring->ring;
1456 ND("%s already created", kring->name);
1457 continue; /* already created by somebody else */
1459 ndesc = kring->nkr_num_slots;
1460 len = sizeof(struct netmap_ring) +
1461 ndesc * sizeof(struct netmap_slot);
1462 ring = netmap_ring_malloc(na->nm_mem, len);
1464 D("Cannot allocate %s_ring", nm_txrx2str(t));
1467 ND("txring at %p", ring);
1469 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1470 *(int64_t *)(uintptr_t)&ring->buf_ofs =
1471 (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1472 na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1473 netmap_ring_offset(na->nm_mem, ring);
1475 /* copy values from kring */
1476 ring->head = kring->rhead;
1477 ring->cur = kring->rcur;
1478 ring->tail = kring->rtail;
1479 *(uint16_t *)(uintptr_t)&ring->nr_buf_size =
1480 netmap_mem_bufsize(na->nm_mem);
1481 ND("%s h %d c %d t %d", kring->name,
1482 ring->head, ring->cur, ring->tail);
1483 ND("initializing slots for %s_ring", nm_txrx2str(txrx));
1484 if (i != nma_get_nrings(na, t) || (na->na_flags & NAF_HOST_RINGS)) {
1485 /* this is a real ring */
1486 if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1487 D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1491 /* this is a fake ring, set all indices to 0 */
1492 netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1495 *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1496 *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1500 NMA_UNLOCK(na->nm_mem);
1505 netmap_free_rings(na);
1507 NMA_UNLOCK(na->nm_mem);
1513 netmap_mem2_rings_delete(struct netmap_adapter *na)
1515 /* last instance, release bufs and rings */
1516 NMA_LOCK(na->nm_mem);
1518 netmap_free_rings(na);
1520 NMA_UNLOCK(na->nm_mem);
1524 /* call with NMA_LOCK held */
1526 * Allocate the per-fd structure netmap_if.
1528 * We assume that the configuration stored in na
1529 * (number of tx/rx rings and descs) does not change while
1530 * the interface is in netmap mode.
1532 static struct netmap_if *
1533 netmap_mem2_if_new(struct netmap_adapter *na)
1535 struct netmap_if *nifp;
1536 ssize_t base; /* handy for relative offsets between rings and nifp */
1537 u_int i, len, n[NR_TXRX], ntot;
1542 /* account for the (eventually fake) host rings */
1543 n[t] = nma_get_nrings(na, t) + 1;
1547 * the descriptor is followed inline by an array of offsets
1548 * to the tx and rx rings in the shared memory region.
1551 NMA_LOCK(na->nm_mem);
1553 len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1554 nifp = netmap_if_malloc(na->nm_mem, len);
1556 NMA_UNLOCK(na->nm_mem);
1560 /* initialize base fields -- override const */
1561 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1562 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1563 strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1566 * fill the slots for the rx and tx rings. They contain the offset
1567 * between the ring and nifp, so the information is usable in
1568 * userspace to reach the ring from the nifp.
1570 base = netmap_if_offset(na->nm_mem, nifp);
1571 for (i = 0; i < n[NR_TX]; i++) {
1572 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1573 netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1575 for (i = 0; i < n[NR_RX]; i++) {
1576 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] =
1577 netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1580 NMA_UNLOCK(na->nm_mem);
1586 netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1591 NMA_LOCK(na->nm_mem);
1592 if (nifp->ni_bufs_head)
1593 netmap_extra_free(na, nifp->ni_bufs_head);
1594 netmap_if_free(na->nm_mem, nifp);
1596 NMA_UNLOCK(na->nm_mem);
1600 netmap_mem_global_deref(struct netmap_mem_d *nmd)
1607 D("active = %d", nmd->active);
1611 struct netmap_mem_ops netmap_mem_global_ops = {
1612 .nmd_get_lut = netmap_mem2_get_lut,
1613 .nmd_get_info = netmap_mem2_get_info,
1614 .nmd_ofstophys = netmap_mem2_ofstophys,
1615 .nmd_config = netmap_mem_global_config,
1616 .nmd_finalize = netmap_mem_global_finalize,
1617 .nmd_deref = netmap_mem_global_deref,
1618 .nmd_delete = netmap_mem_global_delete,
1619 .nmd_if_offset = netmap_mem2_if_offset,
1620 .nmd_if_new = netmap_mem2_if_new,
1621 .nmd_if_delete = netmap_mem2_if_delete,
1622 .nmd_rings_create = netmap_mem2_rings_create,
1623 .nmd_rings_delete = netmap_mem2_rings_delete
1625 struct netmap_mem_ops netmap_mem_private_ops = {
1626 .nmd_get_lut = netmap_mem2_get_lut,
1627 .nmd_get_info = netmap_mem2_get_info,
1628 .nmd_ofstophys = netmap_mem2_ofstophys,
1629 .nmd_config = netmap_mem_private_config,
1630 .nmd_finalize = netmap_mem_private_finalize,
1631 .nmd_deref = netmap_mem_private_deref,
1632 .nmd_if_offset = netmap_mem2_if_offset,
1633 .nmd_delete = netmap_mem_private_delete,
1634 .nmd_if_new = netmap_mem2_if_new,
1635 .nmd_if_delete = netmap_mem2_if_delete,
1636 .nmd_rings_create = netmap_mem2_rings_create,
1637 .nmd_rings_delete = netmap_mem2_rings_delete