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