1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2012-2014 Matteo Landi
5 * Copyright (C) 2012-2016 Luigi Rizzo
6 * Copyright (C) 2012-2016 Giuseppe Lettieri
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifdef linux
32 #include "bsd_glue.h"
33 #endif /* linux */
34
35 #ifdef __APPLE__
36 #include "osx_glue.h"
37 #endif /* __APPLE__ */
38
39 #ifdef __FreeBSD__
40 #include <sys/types.h>
41 #include <sys/ckdint.h>
42 #include <sys/domainset.h>
43 #include <sys/limits.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h> /* MALLOC_DEFINE */
46 #include <sys/proc.h>
47 #include <vm/vm.h> /* vtophys */
48 #include <vm/pmap.h> /* vtophys */
49 #include <sys/socket.h> /* sockaddrs */
50 #include <sys/selinfo.h>
51 #include <sys/sysctl.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/vnet.h>
55 #include <machine/bus.h> /* bus_dmamap_* */
56
57 /* M_NETMAP only used in here */
58 MALLOC_DECLARE(M_NETMAP);
59 MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
60
61 #endif /* __FreeBSD__ */
62
63 #ifdef _WIN32
64 #include <win_glue.h>
65 #endif
66
67 #include <net/netmap.h>
68 #include <dev/netmap/netmap_kern.h>
69 #include <net/netmap_virt.h>
70 #include "netmap_mem2.h"
71
72 #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY
73 #define NETMAP_BUF_MAX_NUM 8*4096 /* if too big takes too much time to allocate */
74 #else
75 #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */
76 #endif
77
78 #define NETMAP_POOL_MAX_NAMSZ 32
79
80
81 enum {
82 NETMAP_IF_POOL = 0,
83 NETMAP_RING_POOL,
84 NETMAP_BUF_POOL,
85 NETMAP_POOLS_NR
86 };
87
88
89 struct netmap_obj_params {
90 u_int size;
91 u_int num;
92
93 u_int last_size;
94 u_int last_num;
95 };
96
97 struct netmap_obj_pool {
98 char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */
99
100 /* ---------------------------------------------------*/
101 /* these are only meaningful if the pool is finalized */
102 /* (see 'finalized' field in netmap_mem_d) */
103 size_t memtotal; /* actual total memory space */
104
105 struct lut_entry *lut; /* virt,phys addresses, objtotal entries */
106 uint32_t *bitmap; /* one bit per buffer, 1 means free */
107 uint32_t *invalid_bitmap;/* one bit per buffer, 1 means invalid */
108 uint32_t bitmap_slots; /* number of uint32 entries in bitmap */
109
110 u_int objtotal; /* actual total number of objects. */
111 u_int numclusters; /* actual number of clusters */
112 u_int objfree; /* number of free objects. */
113
114 int alloc_done; /* we have allocated the memory */
115 /* ---------------------------------------------------*/
116
117 /* limits */
118 u_int objminsize; /* minimum object size */
119 u_int objmaxsize; /* maximum object size */
120 u_int nummin; /* minimum number of objects */
121 u_int nummax; /* maximum number of objects */
122
123 /* these are changed only by config */
124 u_int _objtotal; /* total number of objects */
125 u_int _objsize; /* object size */
126 u_int _clustsize; /* cluster size */
127 u_int _clustentries; /* objects per cluster */
128 u_int _numclusters; /* number of clusters */
129
130 /* requested values */
131 u_int r_objtotal;
132 u_int r_objsize;
133 };
134
135 #define NMA_LOCK_T NM_MTX_T
136 #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx)
137 #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx)
138 #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx)
139 #define NMA_SPINLOCK(n) NM_MTX_SPINLOCK((n)->nm_mtx)
140 #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx)
141
142 struct netmap_mem_ops {
143 int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
144 int (*nmd_get_info)(struct netmap_mem_d *, uint64_t *size,
145 u_int *memflags, uint16_t *id);
146
147 vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
148 int (*nmd_config)(struct netmap_mem_d *);
149 int (*nmd_finalize)(struct netmap_mem_d *, struct netmap_adapter *);
150 void (*nmd_deref)(struct netmap_mem_d *, struct netmap_adapter *);
151 ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
152 void (*nmd_delete)(struct netmap_mem_d *);
153
154 struct netmap_if * (*nmd_if_new)(struct netmap_mem_d *,
155 struct netmap_adapter *, struct netmap_priv_d *);
156 void (*nmd_if_delete)(struct netmap_mem_d *,
157 struct netmap_adapter *, struct netmap_if *);
158 int (*nmd_rings_create)(struct netmap_mem_d *,
159 struct netmap_adapter *);
160 void (*nmd_rings_delete)(struct netmap_mem_d *,
161 struct netmap_adapter *);
162 };
163
164 struct netmap_mem_d {
165 NMA_LOCK_T nm_mtx; /* protect the allocator */
166 size_t nm_totalsize; /* shorthand */
167
168 u_int flags;
169 #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */
170 #define NETMAP_MEM_HIDDEN 0x8 /* being prepared */
171 #define NETMAP_MEM_NOMAP 0x10 /* do not map/unmap pdevs */
172 int lasterr; /* last error for curr config */
173 int active; /* active users */
174 int refcount;
175 /* the three allocators */
176 struct netmap_obj_pool pools[NETMAP_POOLS_NR];
177
178 nm_memid_t nm_id; /* allocator identifier */
179 int nm_grp; /* iommu group id */
180 int nm_numa_domain; /* local NUMA domain */
181
182 /* list of all existing allocators, sorted by nm_id */
183 struct netmap_mem_d *prev, *next;
184
185 const struct netmap_mem_ops *ops;
186
187 struct netmap_obj_params params[NETMAP_POOLS_NR];
188
189 #define NM_MEM_NAMESZ 16
190 char name[NM_MEM_NAMESZ];
191 };
192
193 int
netmap_mem_get_lut(struct netmap_mem_d * nmd,struct netmap_lut * lut)194 netmap_mem_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
195 {
196 int rv;
197
198 NMA_LOCK(nmd);
199 rv = nmd->ops->nmd_get_lut(nmd, lut);
200 NMA_UNLOCK(nmd);
201
202 return rv;
203 }
204
205 int
netmap_mem_get_info(struct netmap_mem_d * nmd,uint64_t * size,u_int * memflags,nm_memid_t * memid)206 netmap_mem_get_info(struct netmap_mem_d *nmd, uint64_t *size,
207 u_int *memflags, nm_memid_t *memid)
208 {
209 int rv;
210
211 NMA_LOCK(nmd);
212 rv = nmd->ops->nmd_get_info(nmd, size, memflags, memid);
213 NMA_UNLOCK(nmd);
214
215 return rv;
216 }
217
218 vm_paddr_t
netmap_mem_ofstophys(struct netmap_mem_d * nmd,vm_ooffset_t off)219 netmap_mem_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
220 {
221 vm_paddr_t pa;
222
223 #if defined(__FreeBSD__)
224 /* This function is called by netmap_dev_pager_fault(), which holds a
225 * non-sleepable lock since FreeBSD 12. Since we cannot sleep, we
226 * spin on the trylock. */
227 NMA_SPINLOCK(nmd);
228 #else
229 NMA_LOCK(nmd);
230 #endif
231 pa = nmd->ops->nmd_ofstophys(nmd, off);
232 NMA_UNLOCK(nmd);
233
234 return pa;
235 }
236
237 static int
netmap_mem_config(struct netmap_mem_d * nmd)238 netmap_mem_config(struct netmap_mem_d *nmd)
239 {
240 if (nmd->active) {
241 /* already in use. Not fatal, but we
242 * cannot change the configuration
243 */
244 return 0;
245 }
246
247 return nmd->ops->nmd_config(nmd);
248 }
249
250 ssize_t
netmap_mem_if_offset(struct netmap_mem_d * nmd,const void * off)251 netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *off)
252 {
253 ssize_t rv;
254
255 NMA_LOCK(nmd);
256 rv = nmd->ops->nmd_if_offset(nmd, off);
257 NMA_UNLOCK(nmd);
258
259 return rv;
260 }
261
262 static void
netmap_mem_delete(struct netmap_mem_d * nmd)263 netmap_mem_delete(struct netmap_mem_d *nmd)
264 {
265 nmd->ops->nmd_delete(nmd);
266 }
267
268 struct netmap_if *
netmap_mem_if_new(struct netmap_adapter * na,struct netmap_priv_d * priv)269 netmap_mem_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
270 {
271 struct netmap_if *nifp;
272 struct netmap_mem_d *nmd = na->nm_mem;
273
274 NMA_LOCK(nmd);
275 nifp = nmd->ops->nmd_if_new(nmd, na, priv);
276 NMA_UNLOCK(nmd);
277
278 return nifp;
279 }
280
281 void
netmap_mem_if_delete(struct netmap_adapter * na,struct netmap_if * nif)282 netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nif)
283 {
284 struct netmap_mem_d *nmd = na->nm_mem;
285
286 NMA_LOCK(nmd);
287 nmd->ops->nmd_if_delete(nmd, na, nif);
288 NMA_UNLOCK(nmd);
289 }
290
291 int
netmap_mem_rings_create(struct netmap_adapter * na)292 netmap_mem_rings_create(struct netmap_adapter *na)
293 {
294 int rv;
295 struct netmap_mem_d *nmd = na->nm_mem;
296
297 NMA_LOCK(nmd);
298 rv = nmd->ops->nmd_rings_create(nmd, na);
299 NMA_UNLOCK(nmd);
300
301 return rv;
302 }
303
304 void
netmap_mem_rings_delete(struct netmap_adapter * na)305 netmap_mem_rings_delete(struct netmap_adapter *na)
306 {
307 struct netmap_mem_d *nmd = na->nm_mem;
308
309 NMA_LOCK(nmd);
310 nmd->ops->nmd_rings_delete(nmd, na);
311 NMA_UNLOCK(nmd);
312 }
313
314 static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
315 static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
316 static int nm_mem_check_group(struct netmap_mem_d *, void *);
317 static void nm_mem_release_id(struct netmap_mem_d *);
318
319 nm_memid_t
netmap_mem_get_id(struct netmap_mem_d * nmd)320 netmap_mem_get_id(struct netmap_mem_d *nmd)
321 {
322 return nmd->nm_id;
323 }
324
325 #ifdef NM_DEBUG_MEM_PUTGET
326 #define NM_DBG_REFC(nmd, func, line) \
327 nm_prinf("%s:%d mem[%d:%d] -> %d", func, line, (nmd)->nm_id, (nmd)->nm_grp, (nmd)->refcount);
328 #else
329 #define NM_DBG_REFC(nmd, func, line)
330 #endif
331
332 /* circular list of all existing allocators */
333 static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
334 static NM_MTX_T nm_mem_list_lock;
335
336 struct netmap_mem_d *
__netmap_mem_get(struct netmap_mem_d * nmd,const char * func,int line)337 __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
338 {
339 NM_MTX_LOCK(nm_mem_list_lock);
340 nmd->refcount++;
341 NM_DBG_REFC(nmd, func, line);
342 NM_MTX_UNLOCK(nm_mem_list_lock);
343 return nmd;
344 }
345
346 void
__netmap_mem_put(struct netmap_mem_d * nmd,const char * func,int line)347 __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
348 {
349 int last;
350 NM_MTX_LOCK(nm_mem_list_lock);
351 last = (--nmd->refcount == 0);
352 if (last)
353 nm_mem_release_id(nmd);
354 NM_DBG_REFC(nmd, func, line);
355 NM_MTX_UNLOCK(nm_mem_list_lock);
356 if (last)
357 netmap_mem_delete(nmd);
358 }
359
360 int
netmap_mem_finalize(struct netmap_mem_d * nmd,struct netmap_adapter * na)361 netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
362 {
363 int lasterr = 0;
364 if (nm_mem_check_group(nmd, na->pdev) < 0) {
365 return ENOMEM;
366 }
367
368 NMA_LOCK(nmd);
369
370 if (netmap_mem_config(nmd))
371 goto out;
372
373 nmd->active++;
374
375 nmd->lasterr = nmd->ops->nmd_finalize(nmd, na);
376
377 if (!nmd->lasterr && !(nmd->flags & NETMAP_MEM_NOMAP)) {
378 nmd->lasterr = netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
379 }
380
381 out:
382 lasterr = nmd->lasterr;
383 NMA_UNLOCK(nmd);
384
385 if (lasterr)
386 netmap_mem_deref(nmd, na);
387
388 return lasterr;
389 }
390
391 static int
nm_isset(uint32_t * bitmap,u_int i)392 nm_isset(uint32_t *bitmap, u_int i)
393 {
394 return bitmap[ (i>>5) ] & ( 1U << (i & 31U) );
395 }
396
397
398 static int
netmap_init_obj_allocator_bitmap(struct netmap_obj_pool * p)399 netmap_init_obj_allocator_bitmap(struct netmap_obj_pool *p)
400 {
401 u_int n, j;
402
403 if (p->bitmap == NULL) {
404 /* Allocate the bitmap */
405 n = (p->objtotal + 31) / 32;
406 p->bitmap = nm_os_malloc(sizeof(p->bitmap[0]) * n);
407 if (p->bitmap == NULL) {
408 nm_prerr("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
409 p->name);
410 return ENOMEM;
411 }
412 p->bitmap_slots = n;
413 } else {
414 memset(p->bitmap, 0, p->bitmap_slots * sizeof(p->bitmap[0]));
415 }
416
417 p->objfree = 0;
418 /*
419 * Set all the bits in the bitmap that have
420 * corresponding buffers to 1 to indicate they are
421 * free.
422 */
423 for (j = 0; j < p->objtotal; j++) {
424 if (p->invalid_bitmap && nm_isset(p->invalid_bitmap, j)) {
425 if (netmap_debug & NM_DEBUG_MEM)
426 nm_prinf("skipping %s %d", p->name, j);
427 continue;
428 }
429 p->bitmap[ (j>>5) ] |= ( 1U << (j & 31U) );
430 p->objfree++;
431 }
432
433 if (netmap_verbose)
434 nm_prinf("%s free %u", p->name, p->objfree);
435 if (p->objfree == 0) {
436 if (netmap_verbose)
437 nm_prerr("%s: no objects available", p->name);
438 return ENOMEM;
439 }
440
441 return 0;
442 }
443
444 static int
netmap_mem_init_bitmaps(struct netmap_mem_d * nmd)445 netmap_mem_init_bitmaps(struct netmap_mem_d *nmd)
446 {
447 int i, error = 0;
448
449 for (i = 0; i < NETMAP_POOLS_NR; i++) {
450 struct netmap_obj_pool *p = &nmd->pools[i];
451
452 error = netmap_init_obj_allocator_bitmap(p);
453 if (error)
454 return error;
455 }
456
457 /*
458 * buffers 0 and 1 are reserved
459 */
460 if (nmd->pools[NETMAP_BUF_POOL].objfree < 2) {
461 nm_prerr("%s: not enough buffers", nmd->pools[NETMAP_BUF_POOL].name);
462 return ENOMEM;
463 }
464
465 nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
466 if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
467 /* XXX This check is a workaround that prevents a
468 * NULL pointer crash which currently happens only
469 * with ptnetmap guests.
470 * Removed shared-info --> is the bug still there? */
471 nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3U;
472 }
473 return 0;
474 }
475
476 int
netmap_mem_deref(struct netmap_mem_d * nmd,struct netmap_adapter * na)477 netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
478 {
479 int last_user = 0;
480 NMA_LOCK(nmd);
481 if (na->active_fds <= 0 && !(nmd->flags & NETMAP_MEM_NOMAP))
482 netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
483 if (nmd->active == 1) {
484 last_user = 1;
485 /*
486 * Reset the allocator when it falls out of use so that any
487 * pool resources leaked by unclean application exits are
488 * reclaimed.
489 */
490 netmap_mem_init_bitmaps(nmd);
491 }
492 nmd->ops->nmd_deref(nmd, na);
493
494 nmd->active--;
495 if (last_user) {
496 nmd->lasterr = 0;
497 }
498
499 NMA_UNLOCK(nmd);
500 return last_user;
501 }
502
503
504 /* accessor functions */
505 static int
netmap_mem2_get_lut(struct netmap_mem_d * nmd,struct netmap_lut * lut)506 netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
507 {
508 lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
509 #ifdef __FreeBSD__
510 lut->plut = lut->lut;
511 #endif
512 lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
513 lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
514
515 return 0;
516 }
517
518 static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
519 [NETMAP_IF_POOL] = {
520 .size = 1024,
521 .num = 2,
522 },
523 [NETMAP_RING_POOL] = {
524 .size = 5*PAGE_SIZE,
525 .num = 4,
526 },
527 [NETMAP_BUF_POOL] = {
528 .size = 2048,
529 .num = 4098,
530 },
531 };
532
533
534 /*
535 * nm_mem is the memory allocator used for all physical interfaces
536 * running in netmap mode.
537 * Virtual (VALE) ports will have each its own allocator.
538 */
539 extern const struct netmap_mem_ops netmap_mem_global_ops; /* forward */
540 struct netmap_mem_d nm_mem = { /* Our memory allocator. */
541 .pools = {
542 [NETMAP_IF_POOL] = {
543 .name = "netmap_if",
544 .objminsize = sizeof(struct netmap_if),
545 .objmaxsize = 4096,
546 .nummin = 10, /* don't be stingy */
547 .nummax = 10000, /* XXX very large */
548 },
549 [NETMAP_RING_POOL] = {
550 .name = "netmap_ring",
551 .objminsize = sizeof(struct netmap_ring),
552 .objmaxsize = 32*PAGE_SIZE,
553 .nummin = 2,
554 .nummax = 1024,
555 },
556 [NETMAP_BUF_POOL] = {
557 .name = "netmap_buf",
558 .objminsize = 64,
559 .objmaxsize = 65536,
560 .nummin = 4,
561 .nummax = 1000000, /* one million! */
562 },
563 },
564
565 .params = {
566 [NETMAP_IF_POOL] = {
567 .size = 1024,
568 .num = 100,
569 },
570 [NETMAP_RING_POOL] = {
571 .size = 9*PAGE_SIZE,
572 .num = 200,
573 },
574 [NETMAP_BUF_POOL] = {
575 .size = 2048,
576 .num = NETMAP_BUF_MAX_NUM,
577 },
578 },
579
580 .nm_id = 1,
581 .nm_grp = -1,
582 .nm_numa_domain = -1,
583
584 .prev = &nm_mem,
585 .next = &nm_mem,
586
587 .ops = &netmap_mem_global_ops,
588
589 .name = "1"
590 };
591
592 static struct netmap_mem_d nm_mem_blueprint;
593
594 /* blueprint for the private memory allocators */
595 /* XXX clang is not happy about using name as a print format */
596 static const struct netmap_mem_d nm_blueprint = {
597 .pools = {
598 [NETMAP_IF_POOL] = {
599 .name = "%s_if",
600 .objminsize = sizeof(struct netmap_if),
601 .objmaxsize = 4096,
602 .nummin = 1,
603 .nummax = 100,
604 },
605 [NETMAP_RING_POOL] = {
606 .name = "%s_ring",
607 .objminsize = sizeof(struct netmap_ring),
608 .objmaxsize = 32*PAGE_SIZE,
609 .nummin = 2,
610 .nummax = 1024,
611 },
612 [NETMAP_BUF_POOL] = {
613 .name = "%s_buf",
614 .objminsize = 64,
615 .objmaxsize = 65536,
616 .nummin = 4,
617 .nummax = 1000000, /* one million! */
618 },
619 },
620
621 .nm_grp = -1,
622 .nm_numa_domain = -1,
623
624 .flags = NETMAP_MEM_PRIVATE,
625
626 .ops = &netmap_mem_global_ops,
627 };
628
629 /* memory allocator related sysctls */
630
631 #define STRINGIFY(x) #x
632
633 #define DECLARE_SYSCTLS(id, name) \
634 SYSBEGIN(mem2_ ## name); \
635 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
636 CTLFLAG_RWTUN, &nm_mem.params[id].size, 0, \
637 "Requested size of netmap " STRINGIFY(name) "s"); \
638 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
639 CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, \
640 "Current size of netmap " STRINGIFY(name) "s"); \
641 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
642 CTLFLAG_RWTUN, &nm_mem.params[id].num, 0, \
643 "Requested number of netmap " STRINGIFY(name) "s"); \
644 SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
645 CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, \
646 "Current number of netmap " STRINGIFY(name) "s"); \
647 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
648 CTLFLAG_RWTUN, &netmap_min_priv_params[id].size, 0, \
649 "Default size of private netmap " STRINGIFY(name) "s"); \
650 SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
651 CTLFLAG_RWTUN, &netmap_min_priv_params[id].num, 0, \
652 "Default number of private netmap " STRINGIFY(name) "s"); \
653 SYSEND
654
655 SYSCTL_DECL(_dev_netmap);
656 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
657 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
658 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
659
660 int netmap_port_numa_affinity = 0;
661 SYSCTL_INT(_dev_netmap, OID_AUTO, port_numa_affinity,
662 CTLFLAG_RDTUN, &netmap_port_numa_affinity, 0,
663 "Use NUMA-local memory for memory pools when possible");
664
665 /* call with nm_mem_list_lock held */
666 static int
nm_mem_assign_id_locked(struct netmap_mem_d * nmd,int grp_id,int domain)667 nm_mem_assign_id_locked(struct netmap_mem_d *nmd, int grp_id, int domain)
668 {
669 nm_memid_t id;
670 struct netmap_mem_d *scan = netmap_last_mem_d;
671 int error = ENOMEM;
672
673 do {
674 /* we rely on unsigned wrap around */
675 id = scan->nm_id + 1;
676 if (id == 0) /* reserve 0 as error value */
677 id = 1;
678 scan = scan->next;
679 if (id != scan->nm_id) {
680 nmd->nm_id = id;
681 nmd->nm_grp = grp_id;
682 nmd->nm_numa_domain = domain;
683 nmd->prev = scan->prev;
684 nmd->next = scan;
685 scan->prev->next = nmd;
686 scan->prev = nmd;
687 netmap_last_mem_d = nmd;
688 nmd->refcount = 1;
689 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
690 error = 0;
691 break;
692 }
693 } while (scan != netmap_last_mem_d);
694
695 return error;
696 }
697
698 /* call with nm_mem_list_lock *not* held */
699 static int
nm_mem_assign_id(struct netmap_mem_d * nmd,int grp_id)700 nm_mem_assign_id(struct netmap_mem_d *nmd, int grp_id)
701 {
702 int ret;
703
704 NM_MTX_LOCK(nm_mem_list_lock);
705 ret = nm_mem_assign_id_locked(nmd, grp_id, -1);
706 NM_MTX_UNLOCK(nm_mem_list_lock);
707
708 return ret;
709 }
710
711 /* call with nm_mem_list_lock held */
712 static void
nm_mem_release_id(struct netmap_mem_d * nmd)713 nm_mem_release_id(struct netmap_mem_d *nmd)
714 {
715 nmd->prev->next = nmd->next;
716 nmd->next->prev = nmd->prev;
717
718 if (netmap_last_mem_d == nmd)
719 netmap_last_mem_d = nmd->prev;
720
721 nmd->prev = nmd->next = NULL;
722 }
723
724 struct netmap_mem_d *
netmap_mem_find(nm_memid_t id)725 netmap_mem_find(nm_memid_t id)
726 {
727 struct netmap_mem_d *nmd;
728
729 NM_MTX_LOCK(nm_mem_list_lock);
730 nmd = netmap_last_mem_d;
731 do {
732 if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
733 nmd->refcount++;
734 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
735 NM_MTX_UNLOCK(nm_mem_list_lock);
736 return nmd;
737 }
738 nmd = nmd->next;
739 } while (nmd != netmap_last_mem_d);
740 NM_MTX_UNLOCK(nm_mem_list_lock);
741 return NULL;
742 }
743
744 static int
nm_mem_check_group(struct netmap_mem_d * nmd,void * dev)745 nm_mem_check_group(struct netmap_mem_d *nmd, void *dev)
746 {
747 int err = 0, id;
748
749 /* Skip not hw adapters.
750 * Vale port can use particular allocator through vale-ctl -m option
751 */
752 if (!dev)
753 return 0;
754 id = nm_iommu_group_id(dev);
755 if (netmap_debug & NM_DEBUG_MEM)
756 nm_prinf("iommu_group %d", id);
757
758 NMA_LOCK(nmd);
759
760 if (nmd->nm_grp != id) {
761 if (netmap_verbose)
762 nm_prerr("iommu group mismatch: %d vs %d",
763 nmd->nm_grp, id);
764 nmd->lasterr = err = ENOMEM;
765 }
766
767 NMA_UNLOCK(nmd);
768 return err;
769 }
770
771 static struct lut_entry *
nm_alloc_lut(u_int nobj)772 nm_alloc_lut(u_int nobj)
773 {
774 size_t n = sizeof(struct lut_entry) * nobj;
775 struct lut_entry *lut;
776 #ifdef linux
777 lut = vmalloc(n);
778 #else
779 lut = nm_os_malloc(n);
780 #endif
781 return lut;
782 }
783
784 static void
nm_free_lut(struct lut_entry * lut,u_int objtotal)785 nm_free_lut(struct lut_entry *lut, u_int objtotal)
786 {
787 bzero(lut, sizeof(struct lut_entry) * objtotal);
788 #ifdef linux
789 vfree(lut);
790 #else
791 nm_os_free(lut);
792 #endif
793 }
794
795 #if defined(linux) || defined(_WIN32)
796 static struct plut_entry *
nm_alloc_plut(u_int nobj)797 nm_alloc_plut(u_int nobj)
798 {
799 size_t n = sizeof(struct plut_entry) * nobj;
800 struct plut_entry *lut;
801 lut = vmalloc(n);
802 return lut;
803 }
804
805 static void
nm_free_plut(struct plut_entry * lut)806 nm_free_plut(struct plut_entry * lut)
807 {
808 vfree(lut);
809 }
810 #endif /* linux or _WIN32 */
811
812
813 /*
814 * First, find the allocator that contains the requested offset,
815 * then locate the cluster through a lookup table.
816 */
817 static vm_paddr_t
netmap_mem2_ofstophys(struct netmap_mem_d * nmd,vm_ooffset_t offset)818 netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
819 {
820 int i;
821 vm_ooffset_t o = offset;
822 vm_paddr_t pa;
823 struct netmap_obj_pool *p;
824
825 p = nmd->pools;
826
827 for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
828 if (offset >= p[i].memtotal)
829 continue;
830 // now lookup the cluster's address
831 #ifndef _WIN32
832 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
833 offset % p[i]._objsize;
834 #else
835 pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
836 pa.QuadPart += offset % p[i]._objsize;
837 #endif
838 return pa;
839 }
840 /* this is only in case of errors */
841 nm_prerr("invalid ofs 0x%x out of 0x%zx 0x%zx 0x%zx", (u_int)o,
842 p[NETMAP_IF_POOL].memtotal,
843 p[NETMAP_IF_POOL].memtotal
844 + p[NETMAP_RING_POOL].memtotal,
845 p[NETMAP_IF_POOL].memtotal
846 + p[NETMAP_RING_POOL].memtotal
847 + p[NETMAP_BUF_POOL].memtotal);
848 #ifndef _WIN32
849 return 0; /* bad address */
850 #else
851 vm_paddr_t res;
852 res.QuadPart = 0;
853 return res;
854 #endif
855 }
856
857 #ifdef _WIN32
858
859 /*
860 * win32_build_virtual_memory_for_userspace
861 *
862 * This function get all the object making part of the pools and maps
863 * a contiguous virtual memory space for the userspace
864 * It works this way
865 * 1 - allocate a Memory Descriptor List wide as the sum
866 * of the memory needed for the pools
867 * 2 - cycle all the objects in every pool and for every object do
868 *
869 * 2a - cycle all the objects in every pool, get the list
870 * of the physical address descriptors
871 * 2b - calculate the offset in the array of pages descriptor in the
872 * main MDL
873 * 2c - copy the descriptors of the object in the main MDL
874 *
875 * 3 - return the resulting MDL that needs to be mapped in userland
876 *
877 * In this way we will have an MDL that describes all the memory for the
878 * objects in a single object
879 */
880
881 PMDL
win32_build_user_vm_map(struct netmap_mem_d * nmd)882 win32_build_user_vm_map(struct netmap_mem_d* nmd)
883 {
884 u_int memflags, ofs = 0;
885 PMDL mainMdl, tempMdl;
886 uint64_t memsize;
887 int i, j;
888
889 if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
890 nm_prerr("memory not finalised yet");
891 return NULL;
892 }
893
894 mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
895 if (mainMdl == NULL) {
896 nm_prerr("failed to allocate mdl");
897 return NULL;
898 }
899
900 NMA_LOCK(nmd);
901 for (i = 0; i < NETMAP_POOLS_NR; i++) {
902 struct netmap_obj_pool *p = &nmd->pools[i];
903 int clsz = p->_clustsize;
904 int clobjs = p->_clustentries; /* objects per cluster */
905 int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
906 PPFN_NUMBER pSrc, pDst;
907
908 /* each pool has a different cluster size so we need to reallocate */
909 tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
910 if (tempMdl == NULL) {
911 NMA_UNLOCK(nmd);
912 nm_prerr("fail to allocate tempMdl");
913 IoFreeMdl(mainMdl);
914 return NULL;
915 }
916 pSrc = MmGetMdlPfnArray(tempMdl);
917 /* create one entry per cluster, the lut[] has one entry per object */
918 for (j = 0; j < p->numclusters; j++, ofs += clsz) {
919 pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
920 MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
921 MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
922 RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
923 mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
924 }
925 IoFreeMdl(tempMdl);
926 }
927 NMA_UNLOCK(nmd);
928 return mainMdl;
929 }
930
931 #endif /* _WIN32 */
932
933 /*
934 * helper function for OS-specific mmap routines (currently only windows).
935 * Given an nmd and a pool index, returns the cluster size and number of clusters.
936 * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
937 * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
938 */
939
940 int
netmap_mem2_get_pool_info(struct netmap_mem_d * nmd,u_int pool,u_int * clustsize,u_int * numclusters)941 netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
942 {
943 if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
944 return 1; /* invalid arguments */
945 // NMA_LOCK_ASSERT(nmd);
946 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
947 *clustsize = *numclusters = 0;
948 return 1; /* not ready yet */
949 }
950 *clustsize = nmd->pools[pool]._clustsize;
951 *numclusters = nmd->pools[pool].numclusters;
952 return 0; /* success */
953 }
954
955 static int
netmap_mem2_get_info(struct netmap_mem_d * nmd,uint64_t * size,u_int * memflags,nm_memid_t * id)956 netmap_mem2_get_info(struct netmap_mem_d* nmd, uint64_t* size,
957 u_int *memflags, nm_memid_t *id)
958 {
959 int error = 0;
960 error = netmap_mem_config(nmd);
961 if (error)
962 goto out;
963 if (size) {
964 if (nmd->flags & NETMAP_MEM_FINALIZED) {
965 *size = nmd->nm_totalsize;
966 } else {
967 int i;
968 *size = 0;
969 for (i = 0; i < NETMAP_POOLS_NR; i++) {
970 struct netmap_obj_pool *p = nmd->pools + i;
971 *size += ((size_t)p->_numclusters * (size_t)p->_clustsize);
972 }
973 }
974 }
975 if (memflags)
976 *memflags = nmd->flags;
977 if (id)
978 *id = nmd->nm_id;
979 out:
980 return error;
981 }
982
983 /*
984 * we store objects by kernel address, need to find the offset
985 * within the pool to export the value to userspace.
986 * Algorithm: scan until we find the cluster, then add the
987 * actual offset in the cluster
988 */
989 static ssize_t
netmap_obj_offset(struct netmap_obj_pool * p,const void * vaddr)990 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
991 {
992 int i, k = p->_clustentries, n = p->objtotal;
993 ssize_t ofs = 0;
994
995 for (i = 0; i < n; i += k, ofs += p->_clustsize) {
996 const char *base = p->lut[i].vaddr;
997 ssize_t relofs = (const char *) vaddr - base;
998
999 if (relofs < 0 || relofs >= p->_clustsize)
1000 continue;
1001
1002 ofs = ofs + relofs;
1003 nm_prdis("%s: return offset %d (cluster %d) for pointer %p",
1004 p->name, ofs, i, vaddr);
1005 return ofs;
1006 }
1007 nm_prerr("address %p is not contained inside any cluster (%s)",
1008 vaddr, p->name);
1009 return 0; /* An error occurred */
1010 }
1011
1012 /* Helper functions which convert virtual addresses to offsets */
1013 #define netmap_if_offset(n, v) \
1014 netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
1015
1016 #define netmap_ring_offset(n, v) \
1017 ((n)->pools[NETMAP_IF_POOL].memtotal + \
1018 netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
1019
1020 static ssize_t
netmap_mem2_if_offset(struct netmap_mem_d * nmd,const void * addr)1021 netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
1022 {
1023 return netmap_if_offset(nmd, addr);
1024 }
1025
1026 /*
1027 * report the index, and use start position as a hint,
1028 * otherwise buffer allocation becomes terribly expensive.
1029 */
1030 static void *
netmap_obj_malloc(struct netmap_obj_pool * p,u_int len,uint32_t * start,uint32_t * index)1031 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
1032 {
1033 uint32_t i = 0; /* index in the bitmap */
1034 uint32_t mask, j = 0; /* slot counter */
1035 void *vaddr = NULL;
1036
1037 if (len > p->_objsize) {
1038 nm_prerr("%s request size %d too large", p->name, len);
1039 return NULL;
1040 }
1041
1042 if (p->objfree == 0) {
1043 nm_prerr("no more %s objects", p->name);
1044 return NULL;
1045 }
1046 if (start)
1047 i = *start;
1048
1049 /* termination is guaranteed by p->free, but better check bounds on i */
1050 while (vaddr == NULL && i < p->bitmap_slots) {
1051 uint32_t cur = p->bitmap[i];
1052 if (cur == 0) { /* bitmask is fully used */
1053 i++;
1054 continue;
1055 }
1056 /* locate a slot */
1057 for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
1058 ;
1059
1060 p->bitmap[i] &= ~mask; /* mark object as in use */
1061 p->objfree--;
1062
1063 vaddr = p->lut[i * 32 + j].vaddr;
1064 if (index)
1065 *index = i * 32 + j;
1066 }
1067 nm_prdis("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
1068
1069 if (start)
1070 *start = i;
1071 return vaddr;
1072 }
1073
1074
1075 /*
1076 * free by index, not by address.
1077 * XXX should we also cleanup the content ?
1078 */
1079 static int
netmap_obj_free(struct netmap_obj_pool * p,uint32_t j)1080 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
1081 {
1082 uint32_t *ptr, mask;
1083
1084 if (j >= p->objtotal) {
1085 nm_prerr("invalid index %u, max %u", j, p->objtotal);
1086 return 1;
1087 }
1088 ptr = &p->bitmap[j / 32];
1089 mask = (1 << (j % 32));
1090 if (*ptr & mask) {
1091 nm_prerr("ouch, double free on buffer %d", j);
1092 return 1;
1093 } else {
1094 *ptr |= mask;
1095 p->objfree++;
1096 return 0;
1097 }
1098 }
1099
1100 /*
1101 * free by address. This is slow but is only used for a few
1102 * objects (rings, nifp)
1103 */
1104 static void
netmap_obj_free_va(struct netmap_obj_pool * p,void * vaddr)1105 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
1106 {
1107 u_int i, j, n = p->numclusters;
1108
1109 for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
1110 void *base = p->lut[i * p->_clustentries].vaddr;
1111 ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
1112
1113 /* Given address, is out of the scope of the current cluster.*/
1114 if (base == NULL || vaddr < base || relofs >= p->_clustsize)
1115 continue;
1116
1117 j = j + relofs / p->_objsize;
1118 /* KASSERT(j != 0, ("Cannot free object 0")); */
1119 netmap_obj_free(p, j);
1120 return;
1121 }
1122 nm_prerr("address %p is not contained inside any cluster (%s)",
1123 vaddr, p->name);
1124 }
1125
1126 unsigned
netmap_mem_bufsize(struct netmap_mem_d * nmd)1127 netmap_mem_bufsize(struct netmap_mem_d *nmd)
1128 {
1129 return nmd->pools[NETMAP_BUF_POOL]._objsize;
1130 }
1131
1132 #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
1133 #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
1134 #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
1135 #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
1136 #define netmap_buf_malloc(n, _pos, _index) \
1137 netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
1138
1139
1140 #if 0 /* currently unused */
1141 /* Return the index associated to the given packet buffer */
1142 #define netmap_buf_index(n, v) \
1143 (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
1144 #endif
1145
1146 /*
1147 * allocate extra buffers in a linked list.
1148 * returns the actual number.
1149 */
1150 uint32_t
netmap_extra_alloc(struct netmap_adapter * na,uint32_t * head,uint32_t n)1151 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
1152 {
1153 struct netmap_mem_d *nmd = na->nm_mem;
1154 uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
1155
1156 NMA_LOCK(nmd);
1157
1158 *head = 0; /* default, 'null' index ie empty list */
1159 for (i = 0 ; i < n; i++) {
1160 uint32_t cur = *head; /* save current head */
1161 uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
1162 if (p == NULL) {
1163 nm_prerr("no more buffers after %d of %d", i, n);
1164 *head = cur; /* restore */
1165 break;
1166 }
1167 nm_prdis(5, "allocate buffer %d -> %d", *head, cur);
1168 *p = cur; /* link to previous head */
1169 }
1170
1171 NMA_UNLOCK(nmd);
1172
1173 return i;
1174 }
1175
1176 static void
netmap_extra_free(struct netmap_adapter * na,uint32_t head)1177 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
1178 {
1179 struct lut_entry *lut = na->na_lut.lut;
1180 struct netmap_mem_d *nmd = na->nm_mem;
1181 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1182 uint32_t i, cur, *buf;
1183
1184 nm_prdis("freeing the extra list");
1185 for (i = 0; head >=2 && head < p->objtotal; i++) {
1186 cur = head;
1187 buf = lut[head].vaddr;
1188 head = *buf;
1189 *buf = 0;
1190 if (netmap_obj_free(p, cur))
1191 break;
1192 }
1193 if (head != 0)
1194 nm_prerr("breaking with head %d", head);
1195 if (netmap_debug & NM_DEBUG_MEM)
1196 nm_prinf("freed %d buffers", i);
1197 }
1198
1199
1200 /* Return nonzero on error */
1201 static int
netmap_new_bufs(struct netmap_mem_d * nmd,struct netmap_slot * slot,u_int n)1202 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1203 {
1204 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1205 u_int i = 0; /* slot counter */
1206 uint32_t pos = 0; /* slot in p->bitmap */
1207 uint32_t index = 0; /* buffer index */
1208
1209 for (i = 0; i < n; i++) {
1210 void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1211 if (vaddr == NULL) {
1212 nm_prerr("no more buffers after %d of %d", i, n);
1213 goto cleanup;
1214 }
1215 slot[i].buf_idx = index;
1216 slot[i].len = p->_objsize;
1217 slot[i].flags = 0;
1218 slot[i].ptr = 0;
1219 }
1220
1221 nm_prdis("%s: allocated %d buffers, %d available, first at %d", p->name, n, p->objfree, pos);
1222 return (0);
1223
1224 cleanup:
1225 while (i > 0) {
1226 i--;
1227 netmap_obj_free(p, slot[i].buf_idx);
1228 }
1229 bzero(slot, n * sizeof(slot[0]));
1230 return (ENOMEM);
1231 }
1232
1233 static void
netmap_mem_set_ring(struct netmap_mem_d * nmd,struct netmap_slot * slot,u_int n,uint32_t index)1234 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1235 {
1236 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1237 u_int i;
1238
1239 for (i = 0; i < n; i++) {
1240 slot[i].buf_idx = index;
1241 slot[i].len = p->_objsize;
1242 slot[i].flags = 0;
1243 }
1244 }
1245
1246
1247 static void
netmap_free_buf(struct netmap_mem_d * nmd,uint32_t i)1248 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1249 {
1250 struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1251
1252 if (i < 2 || i >= p->objtotal) {
1253 nm_prerr("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1254 return;
1255 }
1256 netmap_obj_free(p, i);
1257 }
1258
1259
1260 static void
netmap_free_bufs(struct netmap_mem_d * nmd,struct netmap_slot * slot,u_int n)1261 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1262 {
1263 u_int i;
1264
1265 for (i = 0; i < n; i++) {
1266 if (slot[i].buf_idx > 1)
1267 netmap_free_buf(nmd, slot[i].buf_idx);
1268 }
1269 nm_prdis("%s: released some buffers, available: %u",
1270 p->name, p->objfree);
1271 }
1272
1273 static void
netmap_reset_obj_allocator(struct netmap_obj_pool * p)1274 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
1275 {
1276
1277 if (p == NULL)
1278 return;
1279 if (p->bitmap)
1280 nm_os_free(p->bitmap);
1281 p->bitmap = NULL;
1282 if (p->invalid_bitmap)
1283 nm_os_free(p->invalid_bitmap);
1284 p->invalid_bitmap = NULL;
1285 if (!p->alloc_done) {
1286 /* allocation was done by somebody else.
1287 * Let them clean up after themselves.
1288 */
1289 return;
1290 }
1291 if (p->lut) {
1292 u_int i;
1293
1294 /*
1295 * Free each cluster allocated in
1296 * netmap_finalize_obj_allocator(). The cluster start
1297 * addresses are stored at multiples of p->_clusterentries
1298 * in the lut.
1299 */
1300 for (i = 0; i < p->objtotal; i += p->_clustentries) {
1301 free(p->lut[i].vaddr, M_NETMAP);
1302 }
1303 nm_free_lut(p->lut, p->objtotal);
1304 }
1305 p->lut = NULL;
1306 p->objtotal = 0;
1307 p->memtotal = 0;
1308 p->numclusters = 0;
1309 p->objfree = 0;
1310 p->alloc_done = 0;
1311 }
1312
1313 /*
1314 * Free all resources related to an allocator.
1315 */
1316 static void
netmap_destroy_obj_allocator(struct netmap_obj_pool * p)1317 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1318 {
1319 if (p == NULL)
1320 return;
1321 netmap_reset_obj_allocator(p);
1322 }
1323
1324 /*
1325 * We receive a request for objtotal objects, of size objsize each.
1326 * Internally we may round up both numbers, as we allocate objects
1327 * in small clusters multiple of the page size.
1328 * We need to keep track of objtotal and clustentries,
1329 * as they are needed when freeing memory.
1330 *
1331 * XXX note -- userspace needs the buffers to be contiguous,
1332 * so we cannot afford gaps at the end of a cluster.
1333 */
1334
1335
1336 /* call with NMA_LOCK held */
1337 static int
netmap_config_obj_allocator(struct netmap_obj_pool * p,u_int objtotal,u_int objsize)1338 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1339 {
1340 int i;
1341 u_int clustsize; /* the cluster size, multiple of page size */
1342 u_int clustentries; /* how many objects per entry */
1343
1344 /* we store the current request, so we can
1345 * detect configuration changes later */
1346 p->r_objtotal = objtotal;
1347 p->r_objsize = objsize;
1348
1349 #define MAX_CLUSTSIZE (1<<22) // 4 MB
1350 #define LINE_ROUND NM_BUF_ALIGN // 64
1351 if (objsize >= MAX_CLUSTSIZE) {
1352 /* we could do it but there is no point */
1353 nm_prerr("unsupported allocation for %d bytes", objsize);
1354 return EINVAL;
1355 }
1356 /* make sure objsize is a multiple of LINE_ROUND */
1357 i = (objsize & (LINE_ROUND - 1));
1358 if (i) {
1359 nm_prinf("aligning object by %d bytes", LINE_ROUND - i);
1360 objsize += LINE_ROUND - i;
1361 }
1362 if (objsize < p->objminsize || objsize > p->objmaxsize) {
1363 nm_prerr("requested objsize %d out of range [%d, %d]",
1364 objsize, p->objminsize, p->objmaxsize);
1365 return EINVAL;
1366 }
1367 if (objtotal < p->nummin || objtotal > p->nummax) {
1368 nm_prerr("requested objtotal %d out of range [%d, %d]",
1369 objtotal, p->nummin, p->nummax);
1370 return EINVAL;
1371 }
1372 /*
1373 * Compute number of objects using a brute-force approach:
1374 * given a max cluster size,
1375 * we try to fill it with objects keeping track of the
1376 * wasted space to the next page boundary.
1377 */
1378 for (clustentries = 0, i = 1;; i++) {
1379 u_int delta, used = i * objsize;
1380 if (used > MAX_CLUSTSIZE)
1381 break;
1382 delta = used % PAGE_SIZE;
1383 if (delta == 0) { // exact solution
1384 clustentries = i;
1385 break;
1386 }
1387 }
1388 /* exact solution not found */
1389 if (clustentries == 0) {
1390 nm_prerr("unsupported allocation for %d bytes", objsize);
1391 return EINVAL;
1392 }
1393 /* compute clustsize */
1394 clustsize = clustentries * objsize;
1395 if (netmap_debug & NM_DEBUG_MEM)
1396 nm_prinf("objsize %d clustsize %d objects %d",
1397 objsize, clustsize, clustentries);
1398
1399 /*
1400 * The number of clusters is n = ceil(objtotal/clustentries)
1401 * objtotal' = n * clustentries
1402 */
1403 p->_clustentries = clustentries;
1404 p->_clustsize = clustsize;
1405 p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1406
1407 /* actual values (may be larger than requested) */
1408 p->_objsize = objsize;
1409 p->_objtotal = p->_numclusters * clustentries;
1410
1411 return 0;
1412 }
1413
1414 /* call with NMA_LOCK held */
1415 static int
netmap_finalize_obj_allocator(struct netmap_mem_d * nmd,struct netmap_obj_pool * p)1416 netmap_finalize_obj_allocator(struct netmap_mem_d *nmd, struct netmap_obj_pool *p)
1417 {
1418 int i; /* must be signed */
1419
1420 if (p->lut) {
1421 /* if the lut is already there we assume that also all the
1422 * clusters have already been allocated, possibly by somebody
1423 * else (e.g., extmem). In the latter case, the alloc_done flag
1424 * will remain at zero, so that we will not attempt to
1425 * deallocate the clusters by ourselves in
1426 * netmap_reset_obj_allocator.
1427 */
1428 return 0;
1429 }
1430
1431 /* optimistically assume we have enough memory */
1432 p->numclusters = p->_numclusters;
1433 p->objtotal = p->_objtotal;
1434 p->alloc_done = 1;
1435
1436 p->lut = nm_alloc_lut(p->objtotal);
1437 if (p->lut == NULL) {
1438 nm_prerr("Unable to create lookup table for '%s'", p->name);
1439 goto clean;
1440 }
1441
1442 /*
1443 * Allocate clusters, init pointers
1444 */
1445
1446 for (i = 0; i < (int)p->objtotal;) {
1447 int lim = i + p->_clustentries;
1448 char *clust;
1449
1450 /*
1451 * XXX Note, we only need contigmalloc() for buffers attached
1452 * to native interfaces. In all other cases (nifp, netmap rings
1453 * and even buffers for VALE ports or emulated interfaces) we
1454 * can live with standard malloc, because the hardware will not
1455 * access the pages directly.
1456 */
1457 if (nmd->nm_numa_domain == -1) {
1458 clust = contigmalloc(p->_clustsize, M_NETMAP,
1459 M_NOWAIT | M_ZERO, (size_t)0, -1UL, PAGE_SIZE, 0);
1460 } else {
1461 struct domainset *ds;
1462
1463 ds = DOMAINSET_PREF(nmd->nm_numa_domain);
1464 clust = contigmalloc_domainset(p->_clustsize, M_NETMAP,
1465 ds, M_NOWAIT | M_ZERO, (size_t)0, -1UL, PAGE_SIZE, 0);
1466 }
1467 if (clust == NULL) {
1468 /*
1469 * If we get here, there is a severe memory shortage,
1470 * so halve the allocated memory to reclaim some.
1471 */
1472 nm_prerr("Unable to create cluster at %d for '%s' allocator",
1473 i, p->name);
1474 if (i < 2) /* nothing to halve */
1475 goto out;
1476 lim = i / 2;
1477 for (i--; i >= lim; i--) {
1478 if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1479 free(p->lut[i].vaddr, M_NETMAP);
1480 p->lut[i].vaddr = NULL;
1481 }
1482 out:
1483 p->objtotal = i;
1484 /* we may have stopped in the middle of a cluster */
1485 p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1486 break;
1487 }
1488 /*
1489 * Set lut state for all buffers in the current cluster.
1490 *
1491 * [i, lim) is the set of buffer indexes that cover the
1492 * current cluster.
1493 *
1494 * 'clust' is really the address of the current buffer in
1495 * the current cluster as we index through it with a stride
1496 * of p->_objsize.
1497 */
1498 for (; i < lim; i++, clust += p->_objsize) {
1499 p->lut[i].vaddr = clust;
1500 #if !defined(linux) && !defined(_WIN32)
1501 p->lut[i].paddr = vtophys(clust);
1502 #endif
1503 }
1504 }
1505 p->memtotal = (size_t)p->numclusters * (size_t)p->_clustsize;
1506 if (netmap_verbose)
1507 nm_prinf("Pre-allocated %d clusters (%d/%zuKB) for '%s'",
1508 p->numclusters, p->_clustsize >> 10,
1509 p->memtotal >> 10, p->name);
1510
1511 return 0;
1512
1513 clean:
1514 netmap_reset_obj_allocator(p);
1515 return ENOMEM;
1516 }
1517
1518 /* call with lock held */
1519 static int
netmap_mem_params_changed(struct netmap_obj_params * p)1520 netmap_mem_params_changed(struct netmap_obj_params* p)
1521 {
1522 int i, rv = 0;
1523
1524 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1525 if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1526 p[i].last_size = p[i].size;
1527 p[i].last_num = p[i].num;
1528 rv = 1;
1529 }
1530 }
1531 return rv;
1532 }
1533
1534 static void
netmap_mem_reset_all(struct netmap_mem_d * nmd)1535 netmap_mem_reset_all(struct netmap_mem_d *nmd)
1536 {
1537 int i;
1538
1539 if (netmap_debug & NM_DEBUG_MEM)
1540 nm_prinf("resetting %p", nmd);
1541 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1542 netmap_reset_obj_allocator(&nmd->pools[i]);
1543 }
1544 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1545 }
1546
1547 static int
netmap_mem_unmap(struct netmap_obj_pool * p,struct netmap_adapter * na)1548 netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
1549 {
1550 int i, lim = p->objtotal;
1551 struct netmap_lut *lut;
1552 if (na == NULL || na->pdev == NULL)
1553 return 0;
1554
1555 lut = &na->na_lut;
1556
1557
1558
1559 #if defined(__FreeBSD__)
1560 /* On FreeBSD mapping and unmapping is performed by the txsync
1561 * and rxsync routine, packet by packet. */
1562 (void)i;
1563 (void)lim;
1564 (void)lut;
1565 #elif defined(_WIN32)
1566 (void)i;
1567 (void)lim;
1568 (void)lut;
1569 nm_prerr("unsupported on Windows");
1570 #else /* linux */
1571 nm_prdis("unmapping and freeing plut for %s", na->name);
1572 if (lut->plut == NULL || na->pdev == NULL)
1573 return 0;
1574 for (i = 0; i < lim; i += p->_clustentries) {
1575 if (lut->plut[i].paddr)
1576 netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, p->_clustsize);
1577 }
1578 nm_free_plut(lut->plut);
1579 lut->plut = NULL;
1580 #endif /* linux */
1581
1582 return 0;
1583 }
1584
1585 static int
netmap_mem_map(struct netmap_obj_pool * p,struct netmap_adapter * na)1586 netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1587 {
1588 int error = 0;
1589 int i, lim = p->objtotal;
1590 struct netmap_lut *lut = &na->na_lut;
1591
1592 if (na->pdev == NULL)
1593 return 0;
1594
1595 #if defined(__FreeBSD__)
1596 /* On FreeBSD mapping and unmapping is performed by the txsync
1597 * and rxsync routine, packet by packet. */
1598 (void)i;
1599 (void)lim;
1600 (void)lut;
1601 #elif defined(_WIN32)
1602 (void)i;
1603 (void)lim;
1604 (void)lut;
1605 nm_prerr("unsupported on Windows");
1606 #else /* linux */
1607
1608 if (lut->plut != NULL) {
1609 nm_prdis("plut already allocated for %s", na->name);
1610 return 0;
1611 }
1612
1613 nm_prdis("allocating physical lut for %s", na->name);
1614 lut->plut = nm_alloc_plut(lim);
1615 if (lut->plut == NULL) {
1616 nm_prerr("Failed to allocate physical lut for %s", na->name);
1617 return ENOMEM;
1618 }
1619
1620 for (i = 0; i < lim; i += p->_clustentries) {
1621 lut->plut[i].paddr = 0;
1622 }
1623
1624 for (i = 0; i < lim; i += p->_clustentries) {
1625 int j;
1626
1627 if (p->lut[i].vaddr == NULL)
1628 continue;
1629
1630 error = netmap_load_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr,
1631 p->lut[i].vaddr, p->_clustsize);
1632 if (error) {
1633 nm_prerr("Failed to map cluster #%d from the %s pool", i, p->name);
1634 break;
1635 }
1636
1637 for (j = 1; j < p->_clustentries; j++) {
1638 lut->plut[i + j].paddr = lut->plut[i + j - 1].paddr + p->_objsize;
1639 }
1640 }
1641
1642 if (error)
1643 netmap_mem_unmap(p, na);
1644
1645 #endif /* linux */
1646
1647 return error;
1648 }
1649
1650 static int
netmap_mem_finalize_all(struct netmap_mem_d * nmd)1651 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1652 {
1653 int i;
1654 if (nmd->flags & NETMAP_MEM_FINALIZED)
1655 return 0;
1656 nmd->lasterr = 0;
1657 nmd->nm_totalsize = 0;
1658 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1659 nmd->lasterr = netmap_finalize_obj_allocator(nmd, &nmd->pools[i]);
1660 if (nmd->lasterr)
1661 goto error;
1662 nmd->nm_totalsize += nmd->pools[i].memtotal;
1663 }
1664 nmd->nm_totalsize = (nmd->nm_totalsize + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1665 nmd->lasterr = netmap_mem_init_bitmaps(nmd);
1666 if (nmd->lasterr)
1667 goto error;
1668
1669 nmd->flags |= NETMAP_MEM_FINALIZED;
1670
1671 if (netmap_verbose)
1672 nm_prinf("interfaces %zd KB, rings %zd KB, buffers %zd MB",
1673 nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1674 nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1675 nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1676
1677 if (netmap_verbose)
1678 nm_prinf("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1679
1680
1681 return 0;
1682 error:
1683 netmap_mem_reset_all(nmd);
1684 return nmd->lasterr;
1685 }
1686
1687 /*
1688 * allocator for private memory
1689 */
1690 static void *
_netmap_mem_private_new(size_t size,struct netmap_obj_params * p,int grp_id,const struct netmap_mem_ops * ops,uint64_t memtotal,int * perr)1691 _netmap_mem_private_new(size_t size, struct netmap_obj_params *p, int grp_id,
1692 const struct netmap_mem_ops *ops, uint64_t memtotal, int *perr)
1693 {
1694 struct netmap_mem_d *d = NULL;
1695 int i, err = 0;
1696 int checksz = 0;
1697
1698 /* if memtotal is !=0 we check that the request fits the available
1699 * memory. Moreover, any surprlus memory is assigned to buffers.
1700 */
1701 checksz = (memtotal > 0);
1702
1703 d = nm_os_malloc(size);
1704 if (d == NULL) {
1705 err = ENOMEM;
1706 goto error;
1707 }
1708
1709 *d = nm_blueprint;
1710 d->ops = ops;
1711
1712 err = nm_mem_assign_id(d, grp_id);
1713 if (err)
1714 goto error_free;
1715 snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1716
1717 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1718 snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1719 nm_blueprint.pools[i].name,
1720 d->name);
1721 if (checksz) {
1722 uint64_t poolsz = (uint64_t)p[i].num * p[i].size;
1723 if (memtotal < poolsz) {
1724 nm_prerr("%s: request too large", d->pools[i].name);
1725 err = ENOMEM;
1726 goto error_rel_id;
1727 }
1728 memtotal -= poolsz;
1729 }
1730 d->params[i].num = p[i].num;
1731 d->params[i].size = p[i].size;
1732 }
1733 if (checksz && memtotal > 0) {
1734 uint64_t sz = d->params[NETMAP_BUF_POOL].size;
1735 uint64_t n = (memtotal + sz - 1) / sz;
1736
1737 if (n) {
1738 if (netmap_verbose) {
1739 nm_prinf("%s: adding %llu more buffers",
1740 d->pools[NETMAP_BUF_POOL].name,
1741 (unsigned long long)n);
1742 }
1743 d->params[NETMAP_BUF_POOL].num += n;
1744 }
1745 }
1746
1747 NMA_LOCK_INIT(d);
1748
1749 err = netmap_mem_config(d);
1750 if (err)
1751 goto error_destroy_lock;
1752
1753 d->flags &= ~NETMAP_MEM_FINALIZED;
1754
1755 return d;
1756
1757 error_destroy_lock:
1758 NMA_LOCK_DESTROY(d);
1759 error_rel_id:
1760 nm_mem_release_id(d);
1761 error_free:
1762 nm_os_free(d);
1763 error:
1764 if (perr)
1765 *perr = err;
1766 return NULL;
1767 }
1768
1769 struct netmap_mem_d *
netmap_mem_private_new(u_int txr,u_int txd,u_int rxr,u_int rxd,u_int extra_bufs,u_int npipes,int * perr)1770 netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1771 u_int extra_bufs, u_int npipes, int *perr)
1772 {
1773 struct netmap_mem_d *d = NULL;
1774 struct netmap_obj_params p[NETMAP_POOLS_NR];
1775 int i;
1776 u_int v, maxd;
1777 /* account for the fake host rings */
1778 txr++;
1779 rxr++;
1780
1781 /* copy the min values */
1782 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1783 p[i] = netmap_min_priv_params[i];
1784 }
1785
1786 /* possibly increase them to fit user request */
1787 v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1788 if (p[NETMAP_IF_POOL].size < v)
1789 p[NETMAP_IF_POOL].size = v;
1790 v = 2 + 4 * npipes;
1791 if (p[NETMAP_IF_POOL].num < v)
1792 p[NETMAP_IF_POOL].num = v;
1793 maxd = (txd > rxd) ? txd : rxd;
1794 v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1795 if (p[NETMAP_RING_POOL].size < v)
1796 p[NETMAP_RING_POOL].size = v;
1797 /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1798 * and two rx rings (again, 1 normal and 1 fake host)
1799 */
1800 v = txr + rxr + 8 * npipes;
1801 if (p[NETMAP_RING_POOL].num < v)
1802 p[NETMAP_RING_POOL].num = v;
1803 /* for each pipe we only need the buffers for the 4 "real" rings.
1804 * On the other end, the pipe ring dimension may be different from
1805 * the parent port ring dimension. As a compromise, we allocate twice the
1806 * space actually needed if the pipe rings were the same size as the parent rings
1807 */
1808 v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1809 /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1810 if (p[NETMAP_BUF_POOL].num < v)
1811 p[NETMAP_BUF_POOL].num = v;
1812
1813 if (netmap_verbose)
1814 nm_prinf("req if %d*%d ring %d*%d buf %d*%d",
1815 p[NETMAP_IF_POOL].num,
1816 p[NETMAP_IF_POOL].size,
1817 p[NETMAP_RING_POOL].num,
1818 p[NETMAP_RING_POOL].size,
1819 p[NETMAP_BUF_POOL].num,
1820 p[NETMAP_BUF_POOL].size);
1821
1822 d = _netmap_mem_private_new(sizeof(*d), p, -1, &netmap_mem_global_ops, 0, perr);
1823
1824 return d;
1825 }
1826
1827 /* Reference IOMMU and NUMA local allocator - find existing or create new,
1828 * for non-hw adapters, fall back to global allocator.
1829 */
1830 struct netmap_mem_d *
netmap_mem_get_allocator(struct netmap_adapter * na)1831 netmap_mem_get_allocator(struct netmap_adapter *na)
1832 {
1833 int i, domain, err, grp_id;
1834 struct netmap_mem_d *nmd;
1835
1836 if (na == NULL || na->pdev == NULL)
1837 return netmap_mem_get(&nm_mem);
1838
1839 domain = nm_numa_domain(na->pdev);
1840 grp_id = nm_iommu_group_id(na->pdev);
1841
1842 NM_MTX_LOCK(nm_mem_list_lock);
1843 nmd = netmap_last_mem_d;
1844 do {
1845 if (!(nmd->flags & NETMAP_MEM_HIDDEN) &&
1846 nmd->nm_grp == grp_id && nmd->nm_numa_domain == domain) {
1847 nmd->refcount++;
1848 NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
1849 NM_MTX_UNLOCK(nm_mem_list_lock);
1850 return nmd;
1851 }
1852 nmd = nmd->next;
1853 } while (nmd != netmap_last_mem_d);
1854
1855 nmd = nm_os_malloc(sizeof(*nmd));
1856 if (nmd == NULL)
1857 goto error;
1858
1859 *nmd = nm_mem_blueprint;
1860
1861 err = nm_mem_assign_id_locked(nmd, grp_id, domain);
1862 if (err)
1863 goto error_free;
1864
1865 snprintf(nmd->name, sizeof(nmd->name), "%d", nmd->nm_id);
1866
1867 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1868 snprintf(nmd->pools[i].name, NETMAP_POOL_MAX_NAMSZ, "%s-%s",
1869 nm_mem_blueprint.pools[i].name, nmd->name);
1870 }
1871
1872 NMA_LOCK_INIT(nmd);
1873
1874 NM_MTX_UNLOCK(nm_mem_list_lock);
1875 return nmd;
1876
1877 error_free:
1878 nm_os_free(nmd);
1879 error:
1880 NM_MTX_UNLOCK(nm_mem_list_lock);
1881 return NULL;
1882 }
1883
1884 /* call with lock held */
1885 static int
netmap_mem2_config(struct netmap_mem_d * nmd)1886 netmap_mem2_config(struct netmap_mem_d *nmd)
1887 {
1888 int i;
1889
1890 if (!netmap_mem_params_changed(nmd->params))
1891 goto out;
1892
1893 nm_prdis("reconfiguring");
1894
1895 if (nmd->flags & NETMAP_MEM_FINALIZED) {
1896 /* reset previous allocation */
1897 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1898 netmap_reset_obj_allocator(&nmd->pools[i]);
1899 }
1900 nmd->flags &= ~NETMAP_MEM_FINALIZED;
1901 }
1902
1903 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1904 nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1905 nmd->params[i].num, nmd->params[i].size);
1906 if (nmd->lasterr)
1907 goto out;
1908 }
1909
1910 out:
1911
1912 return nmd->lasterr;
1913 }
1914
1915 static int
netmap_mem2_finalize(struct netmap_mem_d * nmd,struct netmap_adapter * na)1916 netmap_mem2_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1917 {
1918 if (nmd->flags & NETMAP_MEM_FINALIZED)
1919 goto out;
1920
1921 if (netmap_mem_finalize_all(nmd))
1922 goto out;
1923
1924 nmd->lasterr = 0;
1925
1926 out:
1927 return nmd->lasterr;
1928 }
1929
1930 static void
netmap_mem2_delete(struct netmap_mem_d * nmd)1931 netmap_mem2_delete(struct netmap_mem_d *nmd)
1932 {
1933 int i;
1934
1935 for (i = 0; i < NETMAP_POOLS_NR; i++) {
1936 netmap_destroy_obj_allocator(&nmd->pools[i]);
1937 }
1938
1939 NMA_LOCK_DESTROY(nmd);
1940 if (nmd != &nm_mem)
1941 nm_os_free(nmd);
1942 }
1943
1944 #ifdef WITH_EXTMEM
1945 /* doubly linekd list of all existing external allocators */
1946 static struct netmap_mem_ext *netmap_mem_ext_list = NULL;
1947 NM_MTX_T nm_mem_ext_list_lock;
1948 #endif /* WITH_EXTMEM */
1949
1950 int
netmap_mem_init(void)1951 netmap_mem_init(void)
1952 {
1953 nm_mem_blueprint = nm_mem;
1954 NM_MTX_INIT(nm_mem_list_lock);
1955 NMA_LOCK_INIT(&nm_mem);
1956 netmap_mem_get(&nm_mem);
1957 #ifdef WITH_EXTMEM
1958 NM_MTX_INIT(nm_mem_ext_list_lock);
1959 #endif /* WITH_EXTMEM */
1960 return (0);
1961 }
1962
1963 void
netmap_mem_fini(void)1964 netmap_mem_fini(void)
1965 {
1966 netmap_mem_put(&nm_mem);
1967 }
1968
1969 static int
netmap_mem_ring_needed(struct netmap_kring * kring)1970 netmap_mem_ring_needed(struct netmap_kring *kring)
1971 {
1972 return kring->ring == NULL &&
1973 (kring->users > 0 ||
1974 (kring->nr_kflags & NKR_NEEDRING));
1975 }
1976
1977 static int
netmap_mem_ring_todelete(struct netmap_kring * kring)1978 netmap_mem_ring_todelete(struct netmap_kring *kring)
1979 {
1980 return kring->ring != NULL &&
1981 kring->users == 0 &&
1982 !(kring->nr_kflags & NKR_NEEDRING);
1983 }
1984
1985
1986 /* call with NMA_LOCK held *
1987 *
1988 * Allocate netmap rings and buffers for this card
1989 * The rings are contiguous, but have variable size.
1990 * The kring array must follow the layout described
1991 * in netmap_krings_create().
1992 */
1993 static int
netmap_mem2_rings_create(struct netmap_mem_d * nmd,struct netmap_adapter * na)1994 netmap_mem2_rings_create(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1995 {
1996 enum txrx t;
1997 int error;
1998
1999 for_rx_tx(t) {
2000 u_int i;
2001
2002 for (i = 0; i < netmap_all_rings(na, t); i++) {
2003 struct netmap_kring *kring = NMR(na, t)[i];
2004 struct netmap_ring *ring = kring->ring;
2005 u_int len, ndesc;
2006
2007 if (!netmap_mem_ring_needed(kring)) {
2008 /* unneeded, or already created by somebody else */
2009 if (netmap_debug & NM_DEBUG_MEM)
2010 nm_prinf("NOT creating ring %s (ring %p, users %d neekring %d)",
2011 kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
2012 continue;
2013 }
2014 if (netmap_debug & NM_DEBUG_MEM)
2015 nm_prinf("creating %s", kring->name);
2016 ndesc = kring->nkr_num_slots;
2017 if (ckd_mul(&len, ndesc, sizeof(struct netmap_slot)) ||
2018 ckd_add(&len, len, sizeof(struct netmap_ring))) {
2019 error = EINVAL;
2020 goto cleanup;
2021 }
2022 ring = netmap_ring_malloc(nmd, len);
2023 if (ring == NULL) {
2024 nm_prerr("Cannot allocate %s_ring", nm_txrx2str(t));
2025 error = ENOMEM;
2026 goto cleanup;
2027 }
2028 nm_prdis("txring at %p", ring);
2029 kring->ring = ring;
2030 *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
2031 *(int64_t *)(uintptr_t)&ring->buf_ofs =
2032 (nmd->pools[NETMAP_IF_POOL].memtotal +
2033 nmd->pools[NETMAP_RING_POOL].memtotal) -
2034 netmap_ring_offset(nmd, ring);
2035
2036 /* copy values from kring */
2037 ring->head = kring->rhead;
2038 ring->cur = kring->rcur;
2039 ring->tail = kring->rtail;
2040 *(uint32_t *)(uintptr_t)&ring->nr_buf_size =
2041 netmap_mem_bufsize(nmd);
2042 nm_prdis("%s h %d c %d t %d", kring->name,
2043 ring->head, ring->cur, ring->tail);
2044 nm_prdis("initializing slots for %s_ring", nm_txrx2str(t));
2045 if (!(kring->nr_kflags & NKR_FAKERING)) {
2046 /* this is a real ring */
2047 if (netmap_debug & NM_DEBUG_MEM)
2048 nm_prinf("allocating buffers for %s", kring->name);
2049 if (netmap_new_bufs(nmd, ring->slot, ndesc)) {
2050 nm_prerr(
2051 "Cannot allocate buffers for %s_ring",
2052 nm_txrx2str(t));
2053 error = ENOMEM;
2054 goto cleanup;
2055 }
2056 } else {
2057 /* this is a fake ring, set all indices to 0 */
2058 if (netmap_debug & NM_DEBUG_MEM)
2059 nm_prinf("NOT allocating buffers for %s", kring->name);
2060 netmap_mem_set_ring(nmd, ring->slot, ndesc, 0);
2061 }
2062 /* ring info */
2063 *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
2064 *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
2065 }
2066 }
2067
2068 return 0;
2069
2070 cleanup:
2071 /* we cannot actually cleanup here, since we don't own kring->users
2072 * and kring->nr_klags & NKR_NEEDRING. The caller must decrement
2073 * the first or zero-out the second, then call netmap_free_rings()
2074 * to do the cleanup
2075 */
2076
2077 return error;
2078 }
2079
2080 static void
netmap_mem2_rings_delete(struct netmap_mem_d * nmd,struct netmap_adapter * na)2081 netmap_mem2_rings_delete(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2082 {
2083 enum txrx t;
2084
2085 for_rx_tx(t) {
2086 u_int i;
2087 for (i = 0; i < netmap_all_rings(na, t); i++) {
2088 struct netmap_kring *kring = NMR(na, t)[i];
2089 struct netmap_ring *ring = kring->ring;
2090
2091 if (!netmap_mem_ring_todelete(kring)) {
2092 if (netmap_debug & NM_DEBUG_MEM)
2093 nm_prinf("NOT deleting ring %s (ring %p, users %d neekring %d)",
2094 kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
2095 continue;
2096 }
2097 if (netmap_debug & NM_DEBUG_MEM)
2098 nm_prinf("deleting ring %s", kring->name);
2099 if (!(kring->nr_kflags & NKR_FAKERING)) {
2100 nm_prdis("freeing bufs for %s", kring->name);
2101 netmap_free_bufs(nmd, ring->slot, kring->nkr_num_slots);
2102 } else {
2103 nm_prdis("NOT freeing bufs for %s", kring->name);
2104 }
2105 netmap_ring_free(nmd, ring);
2106 kring->ring = NULL;
2107 }
2108 }
2109 }
2110
2111 /* call with NMA_LOCK held */
2112 /*
2113 * Allocate the per-fd structure netmap_if.
2114 *
2115 * We assume that the configuration stored in na
2116 * (number of tx/rx rings and descs) does not change while
2117 * the interface is in netmap mode.
2118 */
2119 static struct netmap_if *
netmap_mem2_if_new(struct netmap_mem_d * nmd,struct netmap_adapter * na,struct netmap_priv_d * priv)2120 netmap_mem2_if_new(struct netmap_mem_d *nmd,
2121 struct netmap_adapter *na, struct netmap_priv_d *priv)
2122 {
2123 struct netmap_if *nifp;
2124 ssize_t base; /* handy for relative offsets between rings and nifp */
2125 u_int i, len, n[NR_TXRX], ntot;
2126 enum txrx t;
2127
2128 ntot = 0;
2129 for_rx_tx(t) {
2130 /* account for the (eventually fake) host rings */
2131 n[t] = netmap_all_rings(na, t);
2132 ntot += n[t];
2133 }
2134 /*
2135 * the descriptor is followed inline by an array of offsets
2136 * to the tx and rx rings in the shared memory region.
2137 */
2138
2139 len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
2140 nifp = netmap_if_malloc(nmd, len);
2141 if (nifp == NULL) {
2142 return NULL;
2143 }
2144
2145 /* initialize base fields -- override const */
2146 *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
2147 *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
2148 *(u_int *)(uintptr_t)&nifp->ni_host_tx_rings =
2149 (na->num_host_tx_rings ? na->num_host_tx_rings : 1);
2150 *(u_int *)(uintptr_t)&nifp->ni_host_rx_rings =
2151 (na->num_host_rx_rings ? na->num_host_rx_rings : 1);
2152 strlcpy(nifp->ni_name, na->name, sizeof(nifp->ni_name));
2153
2154 /*
2155 * fill the slots for the rx and tx rings. They contain the offset
2156 * between the ring and nifp, so the information is usable in
2157 * userspace to reach the ring from the nifp.
2158 */
2159 base = netmap_if_offset(nmd, nifp);
2160 for (i = 0; i < n[NR_TX]; i++) {
2161 /* XXX instead of ofs == 0 maybe use the offset of an error
2162 * ring, like we do for buffers? */
2163 ssize_t ofs = 0;
2164
2165 if (na->tx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_TX]
2166 && i < priv->np_qlast[NR_TX]) {
2167 ofs = netmap_ring_offset(nmd,
2168 na->tx_rings[i]->ring) - base;
2169 }
2170 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
2171 }
2172 for (i = 0; i < n[NR_RX]; i++) {
2173 /* XXX instead of ofs == 0 maybe use the offset of an error
2174 * ring, like we do for buffers? */
2175 ssize_t ofs = 0;
2176
2177 if (na->rx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_RX]
2178 && i < priv->np_qlast[NR_RX]) {
2179 ofs = netmap_ring_offset(nmd,
2180 na->rx_rings[i]->ring) - base;
2181 }
2182 *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
2183 }
2184
2185 return (nifp);
2186 }
2187
2188 static void
netmap_mem2_if_delete(struct netmap_mem_d * nmd,struct netmap_adapter * na,struct netmap_if * nifp)2189 netmap_mem2_if_delete(struct netmap_mem_d *nmd,
2190 struct netmap_adapter *na, struct netmap_if *nifp)
2191 {
2192 if (nifp == NULL)
2193 /* nothing to do */
2194 return;
2195 if (nifp->ni_bufs_head)
2196 netmap_extra_free(na, nifp->ni_bufs_head);
2197 netmap_if_free(nmd, nifp);
2198 }
2199
2200 static void
netmap_mem2_deref(struct netmap_mem_d * nmd,struct netmap_adapter * na)2201 netmap_mem2_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2202 {
2203
2204 if (netmap_debug & NM_DEBUG_MEM)
2205 nm_prinf("active = %d", nmd->active);
2206
2207 }
2208
2209 const struct netmap_mem_ops netmap_mem_global_ops = {
2210 .nmd_get_lut = netmap_mem2_get_lut,
2211 .nmd_get_info = netmap_mem2_get_info,
2212 .nmd_ofstophys = netmap_mem2_ofstophys,
2213 .nmd_config = netmap_mem2_config,
2214 .nmd_finalize = netmap_mem2_finalize,
2215 .nmd_deref = netmap_mem2_deref,
2216 .nmd_delete = netmap_mem2_delete,
2217 .nmd_if_offset = netmap_mem2_if_offset,
2218 .nmd_if_new = netmap_mem2_if_new,
2219 .nmd_if_delete = netmap_mem2_if_delete,
2220 .nmd_rings_create = netmap_mem2_rings_create,
2221 .nmd_rings_delete = netmap_mem2_rings_delete
2222 };
2223
2224 int
netmap_mem_pools_info_get(struct nmreq_pools_info * req,struct netmap_mem_d * nmd)2225 netmap_mem_pools_info_get(struct nmreq_pools_info *req,
2226 struct netmap_mem_d *nmd)
2227 {
2228 int ret;
2229
2230 ret = netmap_mem_get_info(nmd, &req->nr_memsize, NULL,
2231 &req->nr_mem_id);
2232 if (ret) {
2233 return ret;
2234 }
2235
2236 NMA_LOCK(nmd);
2237 req->nr_if_pool_offset = 0;
2238 req->nr_if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
2239 req->nr_if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
2240
2241 req->nr_ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
2242 req->nr_ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
2243 req->nr_ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
2244
2245 req->nr_buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
2246 nmd->pools[NETMAP_RING_POOL].memtotal;
2247 req->nr_buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
2248 req->nr_buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
2249 NMA_UNLOCK(nmd);
2250
2251 return 0;
2252 }
2253
2254 #ifdef WITH_EXTMEM
2255 struct netmap_mem_ext {
2256 struct netmap_mem_d up;
2257
2258 struct nm_os_extmem *os;
2259 struct netmap_mem_ext *next, *prev;
2260 };
2261
2262 /* call with nm_mem_list_lock held */
2263 static void
netmap_mem_ext_register(struct netmap_mem_ext * e)2264 netmap_mem_ext_register(struct netmap_mem_ext *e)
2265 {
2266 NM_MTX_LOCK(nm_mem_ext_list_lock);
2267 if (netmap_mem_ext_list)
2268 netmap_mem_ext_list->prev = e;
2269 e->next = netmap_mem_ext_list;
2270 netmap_mem_ext_list = e;
2271 e->prev = NULL;
2272 NM_MTX_UNLOCK(nm_mem_ext_list_lock);
2273 }
2274
2275 /* call with nm_mem_list_lock held */
2276 static void
netmap_mem_ext_unregister(struct netmap_mem_ext * e)2277 netmap_mem_ext_unregister(struct netmap_mem_ext *e)
2278 {
2279 if (e->prev)
2280 e->prev->next = e->next;
2281 else
2282 netmap_mem_ext_list = e->next;
2283 if (e->next)
2284 e->next->prev = e->prev;
2285 e->prev = e->next = NULL;
2286 }
2287
2288 static struct netmap_mem_ext *
netmap_mem_ext_search(struct nm_os_extmem * os)2289 netmap_mem_ext_search(struct nm_os_extmem *os)
2290 {
2291 struct netmap_mem_ext *e;
2292
2293 NM_MTX_LOCK(nm_mem_ext_list_lock);
2294 for (e = netmap_mem_ext_list; e; e = e->next) {
2295 if (nm_os_extmem_isequal(e->os, os)) {
2296 netmap_mem_get(&e->up);
2297 break;
2298 }
2299 }
2300 NM_MTX_UNLOCK(nm_mem_ext_list_lock);
2301 return e;
2302 }
2303
2304
2305 static void
netmap_mem_ext_delete(struct netmap_mem_d * d)2306 netmap_mem_ext_delete(struct netmap_mem_d *d)
2307 {
2308 int i;
2309 struct netmap_mem_ext *e =
2310 (struct netmap_mem_ext *)d;
2311
2312 netmap_mem_ext_unregister(e);
2313
2314 for (i = 0; i < NETMAP_POOLS_NR; i++) {
2315 struct netmap_obj_pool *p = &d->pools[i];
2316
2317 if (p->lut) {
2318 nm_free_lut(p->lut, p->objtotal);
2319 p->lut = NULL;
2320 }
2321 }
2322 if (e->os)
2323 nm_os_extmem_delete(e->os);
2324 netmap_mem2_delete(d);
2325 }
2326
2327 static int
netmap_mem_ext_config(struct netmap_mem_d * nmd)2328 netmap_mem_ext_config(struct netmap_mem_d *nmd)
2329 {
2330 return 0;
2331 }
2332
2333 struct netmap_mem_ops netmap_mem_ext_ops = {
2334 .nmd_get_lut = netmap_mem2_get_lut,
2335 .nmd_get_info = netmap_mem2_get_info,
2336 .nmd_ofstophys = netmap_mem2_ofstophys,
2337 .nmd_config = netmap_mem_ext_config,
2338 .nmd_finalize = netmap_mem2_finalize,
2339 .nmd_deref = netmap_mem2_deref,
2340 .nmd_delete = netmap_mem_ext_delete,
2341 .nmd_if_offset = netmap_mem2_if_offset,
2342 .nmd_if_new = netmap_mem2_if_new,
2343 .nmd_if_delete = netmap_mem2_if_delete,
2344 .nmd_rings_create = netmap_mem2_rings_create,
2345 .nmd_rings_delete = netmap_mem2_rings_delete
2346 };
2347
2348 struct netmap_mem_d *
netmap_mem_ext_create(uint64_t usrptr,struct nmreq_pools_info * pi,int * perror)2349 netmap_mem_ext_create(uint64_t usrptr, struct nmreq_pools_info *pi, int *perror)
2350 {
2351 int error = 0;
2352 int i, j;
2353 struct netmap_mem_ext *nme;
2354 char *clust;
2355 size_t off;
2356 struct nm_os_extmem *os = NULL;
2357 int nr_pages;
2358
2359 // XXX sanity checks
2360 if (pi->nr_if_pool_objtotal == 0)
2361 pi->nr_if_pool_objtotal = netmap_min_priv_params[NETMAP_IF_POOL].num;
2362 if (pi->nr_if_pool_objsize == 0)
2363 pi->nr_if_pool_objsize = netmap_min_priv_params[NETMAP_IF_POOL].size;
2364 if (pi->nr_ring_pool_objtotal == 0)
2365 pi->nr_ring_pool_objtotal = netmap_min_priv_params[NETMAP_RING_POOL].num;
2366 if (pi->nr_ring_pool_objsize == 0)
2367 pi->nr_ring_pool_objsize = netmap_min_priv_params[NETMAP_RING_POOL].size;
2368 if (pi->nr_buf_pool_objtotal == 0)
2369 pi->nr_buf_pool_objtotal = netmap_min_priv_params[NETMAP_BUF_POOL].num;
2370 if (pi->nr_buf_pool_objsize == 0)
2371 pi->nr_buf_pool_objsize = netmap_min_priv_params[NETMAP_BUF_POOL].size;
2372 if (netmap_verbose & NM_DEBUG_MEM)
2373 nm_prinf("if %d %d ring %d %d buf %d %d",
2374 pi->nr_if_pool_objtotal, pi->nr_if_pool_objsize,
2375 pi->nr_ring_pool_objtotal, pi->nr_ring_pool_objsize,
2376 pi->nr_buf_pool_objtotal, pi->nr_buf_pool_objsize);
2377
2378 os = nm_os_extmem_create(usrptr, pi, &error);
2379 if (os == NULL) {
2380 nm_prerr("os extmem creation failed");
2381 goto out;
2382 }
2383
2384 nme = netmap_mem_ext_search(os);
2385 if (nme) {
2386 nm_os_extmem_delete(os);
2387 return &nme->up;
2388 }
2389 if (netmap_verbose & NM_DEBUG_MEM)
2390 nm_prinf("not found, creating new");
2391
2392 nme = _netmap_mem_private_new(sizeof(*nme),
2393
2394 (struct netmap_obj_params[]){
2395 { pi->nr_if_pool_objsize, pi->nr_if_pool_objtotal },
2396 { pi->nr_ring_pool_objsize, pi->nr_ring_pool_objtotal },
2397 { pi->nr_buf_pool_objsize, pi->nr_buf_pool_objtotal }},
2398 -1,
2399 &netmap_mem_ext_ops,
2400 pi->nr_memsize,
2401 &error);
2402 if (nme == NULL)
2403 goto out_unmap;
2404
2405 nr_pages = nm_os_extmem_nr_pages(os);
2406
2407 /* from now on pages will be released by nme destructor;
2408 * we let res = 0 to prevent release in out_unmap below
2409 */
2410 nme->os = os;
2411 os = NULL; /* pass ownership */
2412
2413 clust = nm_os_extmem_nextpage(nme->os);
2414 off = 0;
2415 for (i = 0; i < NETMAP_POOLS_NR; i++) {
2416 struct netmap_obj_pool *p = &nme->up.pools[i];
2417 struct netmap_obj_params *o = &nme->up.params[i];
2418
2419 p->_objsize = o->size;
2420 p->_clustsize = o->size;
2421 p->_clustentries = 1;
2422
2423 p->lut = nm_alloc_lut(o->num);
2424 if (p->lut == NULL) {
2425 error = ENOMEM;
2426 goto out_delete;
2427 }
2428
2429 p->bitmap_slots = (o->num + sizeof(uint32_t) - 1) / sizeof(uint32_t);
2430 p->invalid_bitmap = nm_os_malloc(sizeof(uint32_t) * p->bitmap_slots);
2431 if (p->invalid_bitmap == NULL) {
2432 error = ENOMEM;
2433 goto out_delete;
2434 }
2435
2436 if (nr_pages == 0) {
2437 p->objtotal = 0;
2438 p->memtotal = 0;
2439 p->objfree = 0;
2440 continue;
2441 }
2442
2443 for (j = 0; j < o->num && nr_pages > 0; j++) {
2444 size_t noff;
2445
2446 p->lut[j].vaddr = clust + off;
2447 #if !defined(linux) && !defined(_WIN32)
2448 p->lut[j].paddr = vtophys(p->lut[j].vaddr);
2449 #endif
2450 nm_prdis("%s %d at %p", p->name, j, p->lut[j].vaddr);
2451 noff = off + p->_objsize;
2452 if (noff < PAGE_SIZE) {
2453 off = noff;
2454 continue;
2455 }
2456 nm_prdis("too big, recomputing offset...");
2457 while (noff >= PAGE_SIZE) {
2458 char *old_clust = clust;
2459 noff -= PAGE_SIZE;
2460 clust = nm_os_extmem_nextpage(nme->os);
2461 nr_pages--;
2462 nm_prdis("noff %zu page %p nr_pages %d", noff,
2463 page_to_virt(*pages), nr_pages);
2464 if (noff > 0 && !nm_isset(p->invalid_bitmap, j) &&
2465 (nr_pages == 0 ||
2466 old_clust + PAGE_SIZE != clust))
2467 {
2468 /* out of space or non contiguous,
2469 * drop this object
2470 * */
2471 p->invalid_bitmap[ (j>>5) ] |= 1U << (j & 31U);
2472 nm_prdis("non contiguous at off %zu, drop", noff);
2473 }
2474 if (nr_pages == 0)
2475 break;
2476 }
2477 off = noff;
2478 }
2479 p->objtotal = j;
2480 p->numclusters = p->objtotal;
2481 p->memtotal = j * (size_t)p->_objsize;
2482 nm_prdis("%d memtotal %zu", j, p->memtotal);
2483 }
2484
2485 netmap_mem_ext_register(nme);
2486
2487 return &nme->up;
2488
2489 out_delete:
2490 netmap_mem_put(&nme->up);
2491 out_unmap:
2492 if (os)
2493 nm_os_extmem_delete(os);
2494 out:
2495 if (perror)
2496 *perror = error;
2497 return NULL;
2498
2499 }
2500 #endif /* WITH_EXTMEM */
2501
2502
2503 #ifdef WITH_PTNETMAP
2504 struct mem_pt_if {
2505 struct mem_pt_if *next;
2506 if_t ifp;
2507 unsigned int nifp_offset;
2508 };
2509
2510 /* Netmap allocator for ptnetmap guests. */
2511 struct netmap_mem_ptg {
2512 struct netmap_mem_d up;
2513
2514 vm_paddr_t nm_paddr; /* physical address in the guest */
2515 void *nm_addr; /* virtual address in the guest */
2516 struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */
2517 nm_memid_t host_mem_id; /* allocator identifier in the host */
2518 struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
2519 struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */
2520 };
2521
2522 /* Link a passthrough interface to a passthrough netmap allocator. */
2523 static int
netmap_mem_pt_guest_ifp_add(struct netmap_mem_d * nmd,if_t ifp,unsigned int nifp_offset)2524 netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, if_t ifp,
2525 unsigned int nifp_offset)
2526 {
2527 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2528 struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
2529
2530 if (!ptif) {
2531 return ENOMEM;
2532 }
2533
2534 NMA_LOCK(nmd);
2535
2536 ptif->ifp = ifp;
2537 ptif->nifp_offset = nifp_offset;
2538
2539 if (ptnmd->pt_ifs) {
2540 ptif->next = ptnmd->pt_ifs;
2541 }
2542 ptnmd->pt_ifs = ptif;
2543
2544 NMA_UNLOCK(nmd);
2545
2546 nm_prinf("ifp=%s,nifp_offset=%u",
2547 if_name(ptif->ifp), ptif->nifp_offset);
2548
2549 return 0;
2550 }
2551
2552 /* Called with NMA_LOCK(nmd) held. */
2553 static struct mem_pt_if *
netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d * nmd,if_t ifp)2554 netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, if_t ifp)
2555 {
2556 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2557 struct mem_pt_if *curr;
2558
2559 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
2560 if (curr->ifp == ifp) {
2561 return curr;
2562 }
2563 }
2564
2565 return NULL;
2566 }
2567
2568 /* Unlink a passthrough interface from a passthrough netmap allocator. */
2569 int
netmap_mem_pt_guest_ifp_del(struct netmap_mem_d * nmd,if_t ifp)2570 netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, if_t ifp)
2571 {
2572 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2573 struct mem_pt_if *prev = NULL;
2574 struct mem_pt_if *curr;
2575 int ret = -1;
2576
2577 NMA_LOCK(nmd);
2578
2579 for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
2580 if (curr->ifp == ifp) {
2581 if (prev) {
2582 prev->next = curr->next;
2583 } else {
2584 ptnmd->pt_ifs = curr->next;
2585 }
2586 nm_prinf("removed (ifp=%s,nifp_offset=%u)",
2587 if_name(curr->ifp), curr->nifp_offset);
2588 nm_os_free(curr);
2589 ret = 0;
2590 break;
2591 }
2592 prev = curr;
2593 }
2594
2595 NMA_UNLOCK(nmd);
2596
2597 return ret;
2598 }
2599
2600 static int
netmap_mem_pt_guest_get_lut(struct netmap_mem_d * nmd,struct netmap_lut * lut)2601 netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
2602 {
2603 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2604
2605 if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
2606 return EINVAL;
2607 }
2608
2609 *lut = ptnmd->buf_lut;
2610 return 0;
2611 }
2612
2613 static int
netmap_mem_pt_guest_get_info(struct netmap_mem_d * nmd,uint64_t * size,u_int * memflags,uint16_t * id)2614 netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, uint64_t *size,
2615 u_int *memflags, uint16_t *id)
2616 {
2617 int error = 0;
2618
2619 error = nmd->ops->nmd_config(nmd);
2620 if (error)
2621 goto out;
2622
2623 if (size)
2624 *size = nmd->nm_totalsize;
2625 if (memflags)
2626 *memflags = nmd->flags;
2627 if (id)
2628 *id = nmd->nm_id;
2629
2630 out:
2631
2632 return error;
2633 }
2634
2635 static vm_paddr_t
netmap_mem_pt_guest_ofstophys(struct netmap_mem_d * nmd,vm_ooffset_t off)2636 netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
2637 {
2638 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2639 vm_paddr_t paddr;
2640 /* if the offset is valid, just return csb->base_addr + off */
2641 paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
2642 nm_prdis("off %lx padr %lx", off, (unsigned long)paddr);
2643 return paddr;
2644 }
2645
2646 static int
netmap_mem_pt_guest_config(struct netmap_mem_d * nmd)2647 netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
2648 {
2649 /* nothing to do, we are configured on creation
2650 * and configuration never changes thereafter
2651 */
2652 return 0;
2653 }
2654
2655 static int
netmap_mem_pt_guest_finalize(struct netmap_mem_d * nmd,struct netmap_adapter * na)2656 netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2657 {
2658 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2659 uint64_t mem_size;
2660 uint32_t bufsize;
2661 uint32_t nbuffers;
2662 uint32_t poolofs;
2663 vm_paddr_t paddr;
2664 char *vaddr;
2665 int i;
2666 int error = 0;
2667
2668 if (nmd->flags & NETMAP_MEM_FINALIZED)
2669 goto out;
2670
2671 if (ptnmd->ptn_dev == NULL) {
2672 nm_prerr("ptnetmap memdev not attached");
2673 error = ENOMEM;
2674 goto out;
2675 }
2676 /* Map memory through ptnetmap-memdev BAR. */
2677 error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2678 &ptnmd->nm_addr, &mem_size);
2679 if (error)
2680 goto out;
2681
2682 /* Initialize the lut using the information contained in the
2683 * ptnetmap memory device. */
2684 bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2685 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2686 nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2687 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2688
2689 /* allocate the lut */
2690 if (ptnmd->buf_lut.lut == NULL) {
2691 nm_prinf("allocating lut");
2692 ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2693 if (ptnmd->buf_lut.lut == NULL) {
2694 nm_prerr("lut allocation failed");
2695 return ENOMEM;
2696 }
2697 }
2698
2699 /* we have physically contiguous memory mapped through PCI BAR */
2700 poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2701 PTNET_MDEV_IO_BUF_POOL_OFS);
2702 vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2703 paddr = ptnmd->nm_paddr + poolofs;
2704
2705 for (i = 0; i < nbuffers; i++) {
2706 ptnmd->buf_lut.lut[i].vaddr = vaddr;
2707 vaddr += bufsize;
2708 paddr += bufsize;
2709 }
2710
2711 ptnmd->buf_lut.objtotal = nbuffers;
2712 ptnmd->buf_lut.objsize = bufsize;
2713 nmd->nm_totalsize = mem_size;
2714
2715 /* Initialize these fields as are needed by
2716 * netmap_mem_bufsize().
2717 * XXX please improve this, why do we need this
2718 * replication? maybe we nmd->pools[] should no be
2719 * there for the guest allocator? */
2720 nmd->pools[NETMAP_BUF_POOL]._objsize = bufsize;
2721 nmd->pools[NETMAP_BUF_POOL]._objtotal = nbuffers;
2722
2723 nmd->flags |= NETMAP_MEM_FINALIZED;
2724 out:
2725 return error;
2726 }
2727
2728 static void
netmap_mem_pt_guest_deref(struct netmap_mem_d * nmd,struct netmap_adapter * na)2729 netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2730 {
2731 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2732
2733 if (nmd->active == 1 &&
2734 (nmd->flags & NETMAP_MEM_FINALIZED)) {
2735 nmd->flags &= ~NETMAP_MEM_FINALIZED;
2736 /* unmap ptnetmap-memdev memory */
2737 if (ptnmd->ptn_dev) {
2738 nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
2739 }
2740 ptnmd->nm_addr = NULL;
2741 ptnmd->nm_paddr = 0;
2742 }
2743 }
2744
2745 static ssize_t
netmap_mem_pt_guest_if_offset(struct netmap_mem_d * nmd,const void * vaddr)2746 netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
2747 {
2748 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2749
2750 return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
2751 }
2752
2753 static void
netmap_mem_pt_guest_delete(struct netmap_mem_d * nmd)2754 netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
2755 {
2756 if (nmd == NULL)
2757 return;
2758 if (netmap_verbose)
2759 nm_prinf("deleting %p", nmd);
2760 if (nmd->active > 0)
2761 nm_prerr("bug: deleting mem allocator with active=%d!", nmd->active);
2762 if (netmap_verbose)
2763 nm_prinf("done deleting %p", nmd);
2764 NMA_LOCK_DESTROY(nmd);
2765 nm_os_free(nmd);
2766 }
2767
2768 static struct netmap_if *
netmap_mem_pt_guest_if_new(struct netmap_mem_d * nmd,struct netmap_adapter * na,struct netmap_priv_d * priv)2769 netmap_mem_pt_guest_if_new(struct netmap_mem_d *nmd,
2770 struct netmap_adapter *na, struct netmap_priv_d *priv)
2771 {
2772 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2773 struct mem_pt_if *ptif;
2774 struct netmap_if *nifp = NULL;
2775
2776 ptif = netmap_mem_pt_guest_ifp_lookup(nmd, na->ifp);
2777 if (ptif == NULL) {
2778 nm_prerr("interface %s is not in passthrough", na->name);
2779 goto out;
2780 }
2781
2782 nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
2783 ptif->nifp_offset);
2784 out:
2785 return nifp;
2786 }
2787
2788 static void
netmap_mem_pt_guest_if_delete(struct netmap_mem_d * nmd,struct netmap_adapter * na,struct netmap_if * nifp)2789 netmap_mem_pt_guest_if_delete(struct netmap_mem_d * nmd,
2790 struct netmap_adapter *na, struct netmap_if *nifp)
2791 {
2792 struct mem_pt_if *ptif;
2793
2794 ptif = netmap_mem_pt_guest_ifp_lookup(nmd, na->ifp);
2795 if (ptif == NULL) {
2796 nm_prerr("interface %s is not in passthrough", na->name);
2797 }
2798 }
2799
2800 static int
netmap_mem_pt_guest_rings_create(struct netmap_mem_d * nmd,struct netmap_adapter * na)2801 netmap_mem_pt_guest_rings_create(struct netmap_mem_d *nmd,
2802 struct netmap_adapter *na)
2803 {
2804 struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2805 struct mem_pt_if *ptif;
2806 struct netmap_if *nifp;
2807 int i, error = -1;
2808
2809 ptif = netmap_mem_pt_guest_ifp_lookup(nmd, na->ifp);
2810 if (ptif == NULL) {
2811 nm_prerr("interface %s is not in passthrough", na->name);
2812 goto out;
2813 }
2814
2815
2816 /* point each kring to the corresponding backend ring */
2817 nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
2818 for (i = 0; i < netmap_all_rings(na, NR_TX); i++) {
2819 struct netmap_kring *kring = na->tx_rings[i];
2820 if (kring->ring)
2821 continue;
2822 kring->ring = (struct netmap_ring *)
2823 ((char *)nifp + nifp->ring_ofs[i]);
2824 }
2825 for (i = 0; i < netmap_all_rings(na, NR_RX); i++) {
2826 struct netmap_kring *kring = na->rx_rings[i];
2827 if (kring->ring)
2828 continue;
2829 kring->ring = (struct netmap_ring *)
2830 ((char *)nifp +
2831 nifp->ring_ofs[netmap_all_rings(na, NR_TX) + i]);
2832 }
2833
2834 error = 0;
2835 out:
2836 return error;
2837 }
2838
2839 static void
netmap_mem_pt_guest_rings_delete(struct netmap_mem_d * nmd,struct netmap_adapter * na)2840 netmap_mem_pt_guest_rings_delete(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2841 {
2842 #if 0
2843 enum txrx t;
2844
2845 for_rx_tx(t) {
2846 u_int i;
2847 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
2848 struct netmap_kring *kring = &NMR(na, t)[i];
2849
2850 kring->ring = NULL;
2851 }
2852 }
2853 #endif
2854 (void)nmd;
2855 (void)na;
2856 }
2857
2858 static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
2859 .nmd_get_lut = netmap_mem_pt_guest_get_lut,
2860 .nmd_get_info = netmap_mem_pt_guest_get_info,
2861 .nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
2862 .nmd_config = netmap_mem_pt_guest_config,
2863 .nmd_finalize = netmap_mem_pt_guest_finalize,
2864 .nmd_deref = netmap_mem_pt_guest_deref,
2865 .nmd_if_offset = netmap_mem_pt_guest_if_offset,
2866 .nmd_delete = netmap_mem_pt_guest_delete,
2867 .nmd_if_new = netmap_mem_pt_guest_if_new,
2868 .nmd_if_delete = netmap_mem_pt_guest_if_delete,
2869 .nmd_rings_create = netmap_mem_pt_guest_rings_create,
2870 .nmd_rings_delete = netmap_mem_pt_guest_rings_delete
2871 };
2872
2873 /* Called with nm_mem_list_lock held. */
2874 static struct netmap_mem_d *
netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)2875 netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
2876 {
2877 struct netmap_mem_d *mem = NULL;
2878 struct netmap_mem_d *scan = netmap_last_mem_d;
2879
2880 do {
2881 /* find ptnetmap allocator through host ID */
2882 if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2883 ((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
2884 mem = scan;
2885 mem->refcount++;
2886 NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
2887 break;
2888 }
2889 scan = scan->next;
2890 } while (scan != netmap_last_mem_d);
2891
2892 return mem;
2893 }
2894
2895 /* Called with nm_mem_list_lock held. */
2896 static struct netmap_mem_d *
netmap_mem_pt_guest_create(nm_memid_t mem_id)2897 netmap_mem_pt_guest_create(nm_memid_t mem_id)
2898 {
2899 struct netmap_mem_ptg *ptnmd;
2900 int err = 0;
2901
2902 ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
2903 if (ptnmd == NULL) {
2904 err = ENOMEM;
2905 goto error;
2906 }
2907
2908 ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2909 ptnmd->host_mem_id = mem_id;
2910 ptnmd->pt_ifs = NULL;
2911
2912 /* Assign new id in the guest (We have the lock) */
2913 err = nm_mem_assign_id_locked(&ptnmd->up, -1, -1);
2914 if (err)
2915 goto error;
2916
2917 ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
2918 ptnmd->up.flags |= NETMAP_MEM_IO;
2919
2920 NMA_LOCK_INIT(&ptnmd->up);
2921
2922 snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2923
2924
2925 return &ptnmd->up;
2926 error:
2927 netmap_mem_pt_guest_delete(&ptnmd->up);
2928 return NULL;
2929 }
2930
2931 /*
2932 * find host id in guest allocators and create guest allocator
2933 * if it is not there
2934 */
2935 static struct netmap_mem_d *
netmap_mem_pt_guest_get(nm_memid_t mem_id)2936 netmap_mem_pt_guest_get(nm_memid_t mem_id)
2937 {
2938 struct netmap_mem_d *nmd;
2939
2940 NM_MTX_LOCK(nm_mem_list_lock);
2941 nmd = netmap_mem_pt_guest_find_memid(mem_id);
2942 if (nmd == NULL) {
2943 nmd = netmap_mem_pt_guest_create(mem_id);
2944 }
2945 NM_MTX_UNLOCK(nm_mem_list_lock);
2946
2947 return nmd;
2948 }
2949
2950 /*
2951 * The guest allocator can be created by ptnetmap_memdev (during the device
2952 * attach) or by ptnetmap device (ptnet), during the netmap_attach.
2953 *
2954 * The order is not important (we have different order in LINUX and FreeBSD).
2955 * The first one, creates the device, and the second one simply attaches it.
2956 */
2957
2958 /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
2959 * the guest */
2960 struct netmap_mem_d *
netmap_mem_pt_guest_attach(struct ptnetmap_memdev * ptn_dev,nm_memid_t mem_id)2961 netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
2962 {
2963 struct netmap_mem_d *nmd;
2964 struct netmap_mem_ptg *ptnmd;
2965
2966 nmd = netmap_mem_pt_guest_get(mem_id);
2967
2968 /* assign this device to the guest allocator */
2969 if (nmd) {
2970 ptnmd = (struct netmap_mem_ptg *)nmd;
2971 ptnmd->ptn_dev = ptn_dev;
2972 }
2973
2974 return nmd;
2975 }
2976
2977 /* Called when ptnet device is attaching */
2978 struct netmap_mem_d *
netmap_mem_pt_guest_new(if_t ifp,unsigned int nifp_offset,unsigned int memid)2979 netmap_mem_pt_guest_new(if_t ifp,
2980 unsigned int nifp_offset,
2981 unsigned int memid)
2982 {
2983 struct netmap_mem_d *nmd;
2984
2985 if (ifp == NULL) {
2986 return NULL;
2987 }
2988
2989 nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
2990
2991 if (nmd) {
2992 netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
2993 }
2994
2995 return nmd;
2996 }
2997
2998 #endif /* WITH_PTNETMAP */
2999