xref: /freebsd/sys/dev/netmap/netmap_freebsd.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /* $FreeBSD$ */
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <sys/errno.h>
35 #include <sys/jail.h>
36 #include <sys/poll.h>  /* POLLIN, POLLOUT */
37 #include <sys/kernel.h> /* types used in module initialization */
38 #include <sys/conf.h>	/* DEV_MODULE_ORDERED */
39 #include <sys/endian.h>
40 #include <sys/syscallsubr.h> /* kern_ioctl() */
41 
42 #include <sys/rwlock.h>
43 
44 #include <vm/vm.h>      /* vtophys */
45 #include <vm/pmap.h>    /* vtophys */
46 #include <vm/vm_param.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_pager.h>
50 #include <vm/uma.h>
51 
52 
53 #include <sys/malloc.h>
54 #include <sys/socket.h> /* sockaddrs */
55 #include <sys/selinfo.h>
56 #include <sys/kthread.h> /* kthread_add() */
57 #include <sys/proc.h> /* PROC_LOCK() */
58 #include <sys/unistd.h> /* RFNOWAIT */
59 #include <sys/sched.h> /* sched_bind() */
60 #include <sys/smp.h> /* mp_maxid */
61 #include <sys/taskqueue.h> /* taskqueue_enqueue(), taskqueue_create(), ... */
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/if_types.h> /* IFT_ETHER */
65 #include <net/ethernet.h> /* ether_ifdetach */
66 #include <net/if_dl.h> /* LLADDR */
67 #include <machine/bus.h>        /* bus_dmamap_* */
68 #include <netinet/in.h>		/* in6_cksum_pseudo() */
69 #include <machine/in_cksum.h>  /* in_pseudo(), in_cksum_hdr() */
70 
71 #include <net/netmap.h>
72 #include <dev/netmap/netmap_kern.h>
73 #include <net/netmap_virt.h>
74 #include <dev/netmap/netmap_mem2.h>
75 
76 
77 /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
78 
79 static void
80 nm_kqueue_notify(void *opaque, int pending)
81 {
82 	struct nm_selinfo *si = opaque;
83 
84 	/* We use a non-zero hint to distinguish this notification call
85 	 * from the call done in kqueue_scan(), which uses hint=0.
86 	 */
87 	KNOTE_UNLOCKED(&si->si.si_note, /*hint=*/0x100);
88 }
89 
90 int nm_os_selinfo_init(NM_SELINFO_T *si, const char *name) {
91 	int err;
92 
93 	TASK_INIT(&si->ntfytask, 0, nm_kqueue_notify, si);
94 	si->ntfytq = taskqueue_create(name, M_NOWAIT,
95 	    taskqueue_thread_enqueue, &si->ntfytq);
96 	if (si->ntfytq == NULL)
97 		return -ENOMEM;
98 	err = taskqueue_start_threads(&si->ntfytq, 1, PI_NET, "tq %s", name);
99 	if (err) {
100 		taskqueue_free(si->ntfytq);
101 		si->ntfytq = NULL;
102 		return err;
103 	}
104 
105 	snprintf(si->mtxname, sizeof(si->mtxname), "nmkl%s", name);
106 	mtx_init(&si->m, si->mtxname, NULL, MTX_DEF);
107 	knlist_init_mtx(&si->si.si_note, &si->m);
108 	si->kqueue_users = 0;
109 
110 	return (0);
111 }
112 
113 void
114 nm_os_selinfo_uninit(NM_SELINFO_T *si)
115 {
116 	if (si->ntfytq == NULL) {
117 		return;	/* si was not initialized */
118 	}
119 	taskqueue_drain(si->ntfytq, &si->ntfytask);
120 	taskqueue_free(si->ntfytq);
121 	si->ntfytq = NULL;
122 	knlist_delete(&si->si.si_note, curthread, /*islocked=*/0);
123 	knlist_destroy(&si->si.si_note);
124 	/* now we don't need the mutex anymore */
125 	mtx_destroy(&si->m);
126 }
127 
128 void *
129 nm_os_malloc(size_t size)
130 {
131 	return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
132 }
133 
134 void *
135 nm_os_realloc(void *addr, size_t new_size, size_t old_size __unused)
136 {
137 	return realloc(addr, new_size, M_DEVBUF, M_NOWAIT | M_ZERO);
138 }
139 
140 void
141 nm_os_free(void *addr)
142 {
143 	free(addr, M_DEVBUF);
144 }
145 
146 void
147 nm_os_ifnet_lock(void)
148 {
149 	IFNET_RLOCK();
150 }
151 
152 void
153 nm_os_ifnet_unlock(void)
154 {
155 	IFNET_RUNLOCK();
156 }
157 
158 static int netmap_use_count = 0;
159 
160 void
161 nm_os_get_module(void)
162 {
163 	netmap_use_count++;
164 }
165 
166 void
167 nm_os_put_module(void)
168 {
169 	netmap_use_count--;
170 }
171 
172 static void
173 netmap_ifnet_arrival_handler(void *arg __unused, struct ifnet *ifp)
174 {
175 	netmap_undo_zombie(ifp);
176 }
177 
178 static void
179 netmap_ifnet_departure_handler(void *arg __unused, struct ifnet *ifp)
180 {
181 	netmap_make_zombie(ifp);
182 }
183 
184 static eventhandler_tag nm_ifnet_ah_tag;
185 static eventhandler_tag nm_ifnet_dh_tag;
186 
187 int
188 nm_os_ifnet_init(void)
189 {
190 	nm_ifnet_ah_tag =
191 		EVENTHANDLER_REGISTER(ifnet_arrival_event,
192 				netmap_ifnet_arrival_handler,
193 				NULL, EVENTHANDLER_PRI_ANY);
194 	nm_ifnet_dh_tag =
195 		EVENTHANDLER_REGISTER(ifnet_departure_event,
196 				netmap_ifnet_departure_handler,
197 				NULL, EVENTHANDLER_PRI_ANY);
198 	return 0;
199 }
200 
201 void
202 nm_os_ifnet_fini(void)
203 {
204 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
205 			nm_ifnet_ah_tag);
206 	EVENTHANDLER_DEREGISTER(ifnet_departure_event,
207 			nm_ifnet_dh_tag);
208 }
209 
210 unsigned
211 nm_os_ifnet_mtu(struct ifnet *ifp)
212 {
213 #if __FreeBSD_version < 1100030
214 	return ifp->if_data.ifi_mtu;
215 #else /* __FreeBSD_version >= 1100030 */
216 	return ifp->if_mtu;
217 #endif
218 }
219 
220 rawsum_t
221 nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum)
222 {
223 	/* TODO XXX please use the FreeBSD implementation for this. */
224 	uint16_t *words = (uint16_t *)data;
225 	int nw = len / 2;
226 	int i;
227 
228 	for (i = 0; i < nw; i++)
229 		cur_sum += be16toh(words[i]);
230 
231 	if (len & 1)
232 		cur_sum += (data[len-1] << 8);
233 
234 	return cur_sum;
235 }
236 
237 /* Fold a raw checksum: 'cur_sum' is in host byte order, while the
238  * return value is in network byte order.
239  */
240 uint16_t
241 nm_os_csum_fold(rawsum_t cur_sum)
242 {
243 	/* TODO XXX please use the FreeBSD implementation for this. */
244 	while (cur_sum >> 16)
245 		cur_sum = (cur_sum & 0xFFFF) + (cur_sum >> 16);
246 
247 	return htobe16((~cur_sum) & 0xFFFF);
248 }
249 
250 uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph)
251 {
252 #if 0
253 	return in_cksum_hdr((void *)iph);
254 #else
255 	return nm_os_csum_fold(nm_os_csum_raw((uint8_t*)iph, sizeof(struct nm_iphdr), 0));
256 #endif
257 }
258 
259 void
260 nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
261 					size_t datalen, uint16_t *check)
262 {
263 #ifdef INET
264 	uint16_t pseudolen = datalen + iph->protocol;
265 
266 	/* Compute and insert the pseudo-header cheksum. */
267 	*check = in_pseudo(iph->saddr, iph->daddr,
268 				 htobe16(pseudolen));
269 	/* Compute the checksum on TCP/UDP header + payload
270 	 * (includes the pseudo-header).
271 	 */
272 	*check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
273 #else
274 	static int notsupported = 0;
275 	if (!notsupported) {
276 		notsupported = 1;
277 		nm_prerr("inet4 segmentation not supported");
278 	}
279 #endif
280 }
281 
282 void
283 nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
284 					size_t datalen, uint16_t *check)
285 {
286 #ifdef INET6
287 	*check = in6_cksum_pseudo((void*)ip6h, datalen, ip6h->nexthdr, 0);
288 	*check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
289 #else
290 	static int notsupported = 0;
291 	if (!notsupported) {
292 		notsupported = 1;
293 		nm_prerr("inet6 segmentation not supported");
294 	}
295 #endif
296 }
297 
298 /* on FreeBSD we send up one packet at a time */
299 void *
300 nm_os_send_up(struct ifnet *ifp, struct mbuf *m, struct mbuf *prev)
301 {
302 	NA(ifp)->if_input(ifp, m);
303 	return NULL;
304 }
305 
306 int
307 nm_os_mbuf_has_csum_offld(struct mbuf *m)
308 {
309 	return m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_SCTP |
310 					 CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |
311 					 CSUM_SCTP_IPV6);
312 }
313 
314 int
315 nm_os_mbuf_has_seg_offld(struct mbuf *m)
316 {
317 	return m->m_pkthdr.csum_flags & CSUM_TSO;
318 }
319 
320 static void
321 freebsd_generic_rx_handler(struct ifnet *ifp, struct mbuf *m)
322 {
323 	int stolen;
324 
325 	if (unlikely(!NM_NA_VALID(ifp))) {
326 		nm_prlim(1, "Warning: RX packet intercepted, but no"
327 				" emulated adapter");
328 		return;
329 	}
330 
331 	stolen = generic_rx_handler(ifp, m);
332 	if (!stolen) {
333 		struct netmap_generic_adapter *gna =
334 				(struct netmap_generic_adapter *)NA(ifp);
335 		gna->save_if_input(ifp, m);
336 	}
337 }
338 
339 /*
340  * Intercept the rx routine in the standard device driver.
341  * Second argument is non-zero to intercept, 0 to restore
342  */
343 int
344 nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept)
345 {
346 	struct netmap_adapter *na = &gna->up.up;
347 	struct ifnet *ifp = na->ifp;
348 	int ret = 0;
349 
350 	nm_os_ifnet_lock();
351 	if (intercept) {
352 		if (gna->save_if_input) {
353 			nm_prerr("RX on %s already intercepted", na->name);
354 			ret = EBUSY; /* already set */
355 			goto out;
356 		}
357 		gna->save_if_input = ifp->if_input;
358 		ifp->if_input = freebsd_generic_rx_handler;
359 	} else {
360 		if (!gna->save_if_input) {
361 			nm_prerr("Failed to undo RX intercept on %s",
362 				na->name);
363 			ret = EINVAL;  /* not saved */
364 			goto out;
365 		}
366 		ifp->if_input = gna->save_if_input;
367 		gna->save_if_input = NULL;
368 	}
369 out:
370 	nm_os_ifnet_unlock();
371 
372 	return ret;
373 }
374 
375 
376 /*
377  * Intercept the packet steering routine in the tx path,
378  * so that we can decide which queue is used for an mbuf.
379  * Second argument is non-zero to intercept, 0 to restore.
380  * On freebsd we just intercept if_transmit.
381  */
382 int
383 nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
384 {
385 	struct netmap_adapter *na = &gna->up.up;
386 	struct ifnet *ifp = netmap_generic_getifp(gna);
387 
388 	nm_os_ifnet_lock();
389 	if (intercept) {
390 		na->if_transmit = ifp->if_transmit;
391 		ifp->if_transmit = netmap_transmit;
392 	} else {
393 		ifp->if_transmit = na->if_transmit;
394 	}
395 	nm_os_ifnet_unlock();
396 
397 	return 0;
398 }
399 
400 
401 /*
402  * Transmit routine used by generic_netmap_txsync(). Returns 0 on success
403  * and non-zero on error (which may be packet drops or other errors).
404  * addr and len identify the netmap buffer, m is the (preallocated)
405  * mbuf to use for transmissions.
406  *
407  * We should add a reference to the mbuf so the m_freem() at the end
408  * of the transmission does not consume resources.
409  *
410  * On FreeBSD, and on multiqueue cards, we can force the queue using
411  *      if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
412  *              i = m->m_pkthdr.flowid % adapter->num_queues;
413  *      else
414  *              i = curcpu % adapter->num_queues;
415  *
416  */
417 int
418 nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
419 {
420 	int ret;
421 	u_int len = a->len;
422 	struct ifnet *ifp = a->ifp;
423 	struct mbuf *m = a->m;
424 
425 #if __FreeBSD_version < 1100000
426 	/*
427 	 * Old FreeBSD versions. The mbuf has a cluster attached,
428 	 * we need to copy from the cluster to the netmap buffer.
429 	 */
430 	if (MBUF_REFCNT(m) != 1) {
431 		nm_prerr("invalid refcnt %d for %p", MBUF_REFCNT(m), m);
432 		panic("in generic_xmit_frame");
433 	}
434 	if (m->m_ext.ext_size < len) {
435 		nm_prlim(2, "size %d < len %d", m->m_ext.ext_size, len);
436 		len = m->m_ext.ext_size;
437 	}
438 	bcopy(a->addr, m->m_data, len);
439 #else  /* __FreeBSD_version >= 1100000 */
440 	/* New FreeBSD versions. Link the external storage to
441 	 * the netmap buffer, so that no copy is necessary. */
442 	m->m_ext.ext_buf = m->m_data = a->addr;
443 	m->m_ext.ext_size = len;
444 #endif /* __FreeBSD_version >= 1100000 */
445 
446 	m->m_len = m->m_pkthdr.len = len;
447 
448 	/* mbuf refcnt is not contended, no need to use atomic
449 	 * (a memory barrier is enough). */
450 	SET_MBUF_REFCNT(m, 2);
451 	M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
452 	m->m_pkthdr.flowid = a->ring_nr;
453 	m->m_pkthdr.rcvif = ifp; /* used for tx notification */
454 	ret = NA(ifp)->if_transmit(ifp, m);
455 	return ret ? -1 : 0;
456 }
457 
458 
459 #if __FreeBSD_version >= 1100005
460 struct netmap_adapter *
461 netmap_getna(if_t ifp)
462 {
463 	return (NA((struct ifnet *)ifp));
464 }
465 #endif /* __FreeBSD_version >= 1100005 */
466 
467 /*
468  * The following two functions are empty until we have a generic
469  * way to extract the info from the ifp
470  */
471 int
472 nm_os_generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
473 {
474 	return 0;
475 }
476 
477 
478 void
479 nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
480 {
481 	unsigned num_rings = netmap_generic_rings ? netmap_generic_rings : 1;
482 
483 	*txq = num_rings;
484 	*rxq = num_rings;
485 }
486 
487 void
488 nm_os_generic_set_features(struct netmap_generic_adapter *gna)
489 {
490 
491 	gna->rxsg = 1; /* Supported through m_copydata. */
492 	gna->txqdisc = 0; /* Not supported. */
493 }
494 
495 void
496 nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, struct netmap_adapter *na)
497 {
498 	mit->mit_pending = 0;
499 	mit->mit_ring_idx = idx;
500 	mit->mit_na = na;
501 }
502 
503 
504 void
505 nm_os_mitigation_start(struct nm_generic_mit *mit)
506 {
507 }
508 
509 
510 void
511 nm_os_mitigation_restart(struct nm_generic_mit *mit)
512 {
513 }
514 
515 
516 int
517 nm_os_mitigation_active(struct nm_generic_mit *mit)
518 {
519 
520 	return 0;
521 }
522 
523 
524 void
525 nm_os_mitigation_cleanup(struct nm_generic_mit *mit)
526 {
527 }
528 
529 static int
530 nm_vi_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
531 {
532 
533 	return EINVAL;
534 }
535 
536 static void
537 nm_vi_start(struct ifnet *ifp)
538 {
539 	panic("nm_vi_start() must not be called");
540 }
541 
542 /*
543  * Index manager of persistent virtual interfaces.
544  * It is used to decide the lowest byte of the MAC address.
545  * We use the same algorithm with management of bridge port index.
546  */
547 #define NM_VI_MAX	255
548 static struct {
549 	uint8_t index[NM_VI_MAX]; /* XXX just for a reasonable number */
550 	uint8_t active;
551 	struct mtx lock;
552 } nm_vi_indices;
553 
554 void
555 nm_os_vi_init_index(void)
556 {
557 	int i;
558 	for (i = 0; i < NM_VI_MAX; i++)
559 		nm_vi_indices.index[i] = i;
560 	nm_vi_indices.active = 0;
561 	mtx_init(&nm_vi_indices.lock, "nm_vi_indices_lock", NULL, MTX_DEF);
562 }
563 
564 /* return -1 if no index available */
565 static int
566 nm_vi_get_index(void)
567 {
568 	int ret;
569 
570 	mtx_lock(&nm_vi_indices.lock);
571 	ret = nm_vi_indices.active == NM_VI_MAX ? -1 :
572 		nm_vi_indices.index[nm_vi_indices.active++];
573 	mtx_unlock(&nm_vi_indices.lock);
574 	return ret;
575 }
576 
577 static void
578 nm_vi_free_index(uint8_t val)
579 {
580 	int i, lim;
581 
582 	mtx_lock(&nm_vi_indices.lock);
583 	lim = nm_vi_indices.active;
584 	for (i = 0; i < lim; i++) {
585 		if (nm_vi_indices.index[i] == val) {
586 			/* swap index[lim-1] and j */
587 			int tmp = nm_vi_indices.index[lim-1];
588 			nm_vi_indices.index[lim-1] = val;
589 			nm_vi_indices.index[i] = tmp;
590 			nm_vi_indices.active--;
591 			break;
592 		}
593 	}
594 	if (lim == nm_vi_indices.active)
595 		nm_prerr("Index %u not found", val);
596 	mtx_unlock(&nm_vi_indices.lock);
597 }
598 #undef NM_VI_MAX
599 
600 /*
601  * Implementation of a netmap-capable virtual interface that
602  * registered to the system.
603  * It is based on if_tap.c and ip_fw_log.c in FreeBSD 9.
604  *
605  * Note: Linux sets refcount to 0 on allocation of net_device,
606  * then increments it on registration to the system.
607  * FreeBSD sets refcount to 1 on if_alloc(), and does not
608  * increment this refcount on if_attach().
609  */
610 int
611 nm_os_vi_persist(const char *name, struct ifnet **ret)
612 {
613 	struct ifnet *ifp;
614 	u_short macaddr_hi;
615 	uint32_t macaddr_mid;
616 	u_char eaddr[6];
617 	int unit = nm_vi_get_index(); /* just to decide MAC address */
618 
619 	if (unit < 0)
620 		return EBUSY;
621 	/*
622 	 * We use the same MAC address generation method with tap
623 	 * except for the highest octet is 00:be instead of 00:bd
624 	 */
625 	macaddr_hi = htons(0x00be); /* XXX tap + 1 */
626 	macaddr_mid = (uint32_t) ticks;
627 	bcopy(&macaddr_hi, eaddr, sizeof(short));
628 	bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
629 	eaddr[5] = (uint8_t)unit;
630 
631 	ifp = if_alloc(IFT_ETHER);
632 	if (ifp == NULL) {
633 		nm_prerr("if_alloc failed");
634 		return ENOMEM;
635 	}
636 	if_initname(ifp, name, IF_DUNIT_NONE);
637 	ifp->if_mtu = 65536;
638 	ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
639 	ifp->if_init = (void *)nm_vi_dummy;
640 	ifp->if_ioctl = nm_vi_dummy;
641 	ifp->if_start = nm_vi_start;
642 	ifp->if_mtu = ETHERMTU;
643 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
644 	ifp->if_capabilities |= IFCAP_LINKSTATE;
645 	ifp->if_capenable |= IFCAP_LINKSTATE;
646 
647 	ether_ifattach(ifp, eaddr);
648 	*ret = ifp;
649 	return 0;
650 }
651 
652 /* unregister from the system and drop the final refcount */
653 void
654 nm_os_vi_detach(struct ifnet *ifp)
655 {
656 	nm_vi_free_index(((char *)IF_LLADDR(ifp))[5]);
657 	ether_ifdetach(ifp);
658 	if_free(ifp);
659 }
660 
661 #ifdef WITH_EXTMEM
662 #include <vm/vm_map.h>
663 #include <vm/vm_kern.h>
664 struct nm_os_extmem {
665 	vm_object_t obj;
666 	vm_offset_t kva;
667 	vm_offset_t size;
668 	uintptr_t scan;
669 };
670 
671 void
672 nm_os_extmem_delete(struct nm_os_extmem *e)
673 {
674 	nm_prinf("freeing %zx bytes", (size_t)e->size);
675 	vm_map_remove(kernel_map, e->kva, e->kva + e->size);
676 	nm_os_free(e);
677 }
678 
679 char *
680 nm_os_extmem_nextpage(struct nm_os_extmem *e)
681 {
682 	char *rv = NULL;
683 	if (e->scan < e->kva + e->size) {
684 		rv = (char *)e->scan;
685 		e->scan += PAGE_SIZE;
686 	}
687 	return rv;
688 }
689 
690 int
691 nm_os_extmem_isequal(struct nm_os_extmem *e1, struct nm_os_extmem *e2)
692 {
693 	return (e1->obj == e2->obj);
694 }
695 
696 int
697 nm_os_extmem_nr_pages(struct nm_os_extmem *e)
698 {
699 	return e->size >> PAGE_SHIFT;
700 }
701 
702 struct nm_os_extmem *
703 nm_os_extmem_create(unsigned long p, struct nmreq_pools_info *pi, int *perror)
704 {
705 	vm_map_t map;
706 	vm_map_entry_t entry;
707 	vm_object_t obj;
708 	vm_prot_t prot;
709 	vm_pindex_t index;
710 	boolean_t wired;
711 	struct nm_os_extmem *e = NULL;
712 	int rv, error = 0;
713 
714 	e = nm_os_malloc(sizeof(*e));
715 	if (e == NULL) {
716 		error = ENOMEM;
717 		goto out;
718 	}
719 
720 	map = &curthread->td_proc->p_vmspace->vm_map;
721 	rv = vm_map_lookup(&map, p, VM_PROT_RW, &entry,
722 			&obj, &index, &prot, &wired);
723 	if (rv != KERN_SUCCESS) {
724 		nm_prerr("address %lx not found", p);
725 		goto out_free;
726 	}
727 	/* check that we are given the whole vm_object ? */
728 	vm_map_lookup_done(map, entry);
729 
730 	// XXX can we really use obj after releasing the map lock?
731 	e->obj = obj;
732 	vm_object_reference(obj);
733 	/* wire the memory and add the vm_object to the kernel map,
734 	 * to make sure that it is not fred even if the processes that
735 	 * are mmap()ing it all exit
736 	 */
737 	e->kva = vm_map_min(kernel_map);
738 	e->size = obj->size << PAGE_SHIFT;
739 	rv = vm_map_find(kernel_map, obj, 0, &e->kva, e->size, 0,
740 			VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE,
741 			VM_PROT_READ | VM_PROT_WRITE, 0);
742 	if (rv != KERN_SUCCESS) {
743 		nm_prerr("vm_map_find(%zx) failed", (size_t)e->size);
744 		goto out_rel;
745 	}
746 	rv = vm_map_wire(kernel_map, e->kva, e->kva + e->size,
747 			VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
748 	if (rv != KERN_SUCCESS) {
749 		nm_prerr("vm_map_wire failed");
750 		goto out_rem;
751 	}
752 
753 	e->scan = e->kva;
754 
755 	return e;
756 
757 out_rem:
758 	vm_map_remove(kernel_map, e->kva, e->kva + e->size);
759 	e->obj = NULL;
760 out_rel:
761 	vm_object_deallocate(e->obj);
762 out_free:
763 	nm_os_free(e);
764 out:
765 	if (perror)
766 		*perror = error;
767 	return NULL;
768 }
769 #endif /* WITH_EXTMEM */
770 
771 /* ================== PTNETMAP GUEST SUPPORT ==================== */
772 
773 #ifdef WITH_PTNETMAP
774 #include <sys/bus.h>
775 #include <sys/rman.h>
776 #include <machine/bus.h>        /* bus_dmamap_* */
777 #include <machine/resource.h>
778 #include <dev/pci/pcivar.h>
779 #include <dev/pci/pcireg.h>
780 /*
781  * ptnetmap memory device (memdev) for freebsd guest,
782  * ssed to expose host netmap memory to the guest through a PCI BAR.
783  */
784 
785 /*
786  * ptnetmap memdev private data structure
787  */
788 struct ptnetmap_memdev {
789 	device_t dev;
790 	struct resource *pci_io;
791 	struct resource *pci_mem;
792 	struct netmap_mem_d *nm_mem;
793 };
794 
795 static int	ptn_memdev_probe(device_t);
796 static int	ptn_memdev_attach(device_t);
797 static int	ptn_memdev_detach(device_t);
798 static int	ptn_memdev_shutdown(device_t);
799 
800 static device_method_t ptn_memdev_methods[] = {
801 	DEVMETHOD(device_probe, ptn_memdev_probe),
802 	DEVMETHOD(device_attach, ptn_memdev_attach),
803 	DEVMETHOD(device_detach, ptn_memdev_detach),
804 	DEVMETHOD(device_shutdown, ptn_memdev_shutdown),
805 	DEVMETHOD_END
806 };
807 
808 static driver_t ptn_memdev_driver = {
809 	PTNETMAP_MEMDEV_NAME,
810 	ptn_memdev_methods,
811 	sizeof(struct ptnetmap_memdev),
812 };
813 
814 /* We use (SI_ORDER_MIDDLE+1) here, see DEV_MODULE_ORDERED() invocation
815  * below. */
816 static devclass_t ptnetmap_devclass;
817 DRIVER_MODULE_ORDERED(ptn_memdev, pci, ptn_memdev_driver, ptnetmap_devclass,
818 		      NULL, NULL, SI_ORDER_MIDDLE + 1);
819 
820 /*
821  * Map host netmap memory through PCI-BAR in the guest OS,
822  * returning physical (nm_paddr) and virtual (nm_addr) addresses
823  * of the netmap memory mapped in the guest.
824  */
825 int
826 nm_os_pt_memdev_iomap(struct ptnetmap_memdev *ptn_dev, vm_paddr_t *nm_paddr,
827 		      void **nm_addr, uint64_t *mem_size)
828 {
829 	int rid;
830 
831 	nm_prinf("ptn_memdev_driver iomap");
832 
833 	rid = PCIR_BAR(PTNETMAP_MEM_PCI_BAR);
834 	*mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_HI);
835 	*mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_LO) |
836 			(*mem_size << 32);
837 
838 	/* map memory allocator */
839 	ptn_dev->pci_mem = bus_alloc_resource(ptn_dev->dev, SYS_RES_MEMORY,
840 			&rid, 0, ~0, *mem_size, RF_ACTIVE);
841 	if (ptn_dev->pci_mem == NULL) {
842 		*nm_paddr = 0;
843 		*nm_addr = NULL;
844 		return ENOMEM;
845 	}
846 
847 	*nm_paddr = rman_get_start(ptn_dev->pci_mem);
848 	*nm_addr = rman_get_virtual(ptn_dev->pci_mem);
849 
850 	nm_prinf("=== BAR %d start %lx len %lx mem_size %lx ===",
851 			PTNETMAP_MEM_PCI_BAR,
852 			(unsigned long)(*nm_paddr),
853 			(unsigned long)rman_get_size(ptn_dev->pci_mem),
854 			(unsigned long)*mem_size);
855 	return (0);
856 }
857 
858 uint32_t
859 nm_os_pt_memdev_ioread(struct ptnetmap_memdev *ptn_dev, unsigned int reg)
860 {
861 	return bus_read_4(ptn_dev->pci_io, reg);
862 }
863 
864 /* Unmap host netmap memory. */
865 void
866 nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *ptn_dev)
867 {
868 	nm_prinf("ptn_memdev_driver iounmap");
869 
870 	if (ptn_dev->pci_mem) {
871 		bus_release_resource(ptn_dev->dev, SYS_RES_MEMORY,
872 			PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
873 		ptn_dev->pci_mem = NULL;
874 	}
875 }
876 
877 /* Device identification routine, return BUS_PROBE_DEFAULT on success,
878  * positive on failure */
879 static int
880 ptn_memdev_probe(device_t dev)
881 {
882 	char desc[256];
883 
884 	if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID)
885 		return (ENXIO);
886 	if (pci_get_device(dev) != PTNETMAP_PCI_DEVICE_ID)
887 		return (ENXIO);
888 
889 	snprintf(desc, sizeof(desc), "%s PCI adapter",
890 			PTNETMAP_MEMDEV_NAME);
891 	device_set_desc_copy(dev, desc);
892 
893 	return (BUS_PROBE_DEFAULT);
894 }
895 
896 /* Device initialization routine. */
897 static int
898 ptn_memdev_attach(device_t dev)
899 {
900 	struct ptnetmap_memdev *ptn_dev;
901 	int rid;
902 	uint16_t mem_id;
903 
904 	ptn_dev = device_get_softc(dev);
905 	ptn_dev->dev = dev;
906 
907 	pci_enable_busmaster(dev);
908 
909 	rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
910 	ptn_dev->pci_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
911 						 RF_ACTIVE);
912 	if (ptn_dev->pci_io == NULL) {
913 	        device_printf(dev, "cannot map I/O space\n");
914 	        return (ENXIO);
915 	}
916 
917 	mem_id = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMID);
918 
919 	/* create guest allocator */
920 	ptn_dev->nm_mem = netmap_mem_pt_guest_attach(ptn_dev, mem_id);
921 	if (ptn_dev->nm_mem == NULL) {
922 		ptn_memdev_detach(dev);
923 	        return (ENOMEM);
924 	}
925 	netmap_mem_get(ptn_dev->nm_mem);
926 
927 	nm_prinf("ptnetmap memdev attached, host memid: %u", mem_id);
928 
929 	return (0);
930 }
931 
932 /* Device removal routine. */
933 static int
934 ptn_memdev_detach(device_t dev)
935 {
936 	struct ptnetmap_memdev *ptn_dev;
937 
938 	ptn_dev = device_get_softc(dev);
939 
940 	if (ptn_dev->nm_mem) {
941 		nm_prinf("ptnetmap memdev detached, host memid %u",
942 			netmap_mem_get_id(ptn_dev->nm_mem));
943 		netmap_mem_put(ptn_dev->nm_mem);
944 		ptn_dev->nm_mem = NULL;
945 	}
946 	if (ptn_dev->pci_mem) {
947 		bus_release_resource(dev, SYS_RES_MEMORY,
948 			PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
949 		ptn_dev->pci_mem = NULL;
950 	}
951 	if (ptn_dev->pci_io) {
952 		bus_release_resource(dev, SYS_RES_IOPORT,
953 			PCIR_BAR(PTNETMAP_IO_PCI_BAR), ptn_dev->pci_io);
954 		ptn_dev->pci_io = NULL;
955 	}
956 
957 	return (0);
958 }
959 
960 static int
961 ptn_memdev_shutdown(device_t dev)
962 {
963 	return bus_generic_shutdown(dev);
964 }
965 
966 #endif /* WITH_PTNETMAP */
967 
968 /*
969  * In order to track whether pages are still mapped, we hook into
970  * the standard cdev_pager and intercept the constructor and
971  * destructor.
972  */
973 
974 struct netmap_vm_handle_t {
975 	struct cdev 		*dev;
976 	struct netmap_priv_d	*priv;
977 };
978 
979 
980 static int
981 netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
982 		vm_ooffset_t foff, struct ucred *cred, u_short *color)
983 {
984 	struct netmap_vm_handle_t *vmh = handle;
985 
986 	if (netmap_verbose)
987 		nm_prinf("handle %p size %jd prot %d foff %jd",
988 			handle, (intmax_t)size, prot, (intmax_t)foff);
989 	if (color)
990 		*color = 0;
991 	dev_ref(vmh->dev);
992 	return 0;
993 }
994 
995 
996 static void
997 netmap_dev_pager_dtor(void *handle)
998 {
999 	struct netmap_vm_handle_t *vmh = handle;
1000 	struct cdev *dev = vmh->dev;
1001 	struct netmap_priv_d *priv = vmh->priv;
1002 
1003 	if (netmap_verbose)
1004 		nm_prinf("handle %p", handle);
1005 	netmap_dtor(priv);
1006 	free(vmh, M_DEVBUF);
1007 	dev_rel(dev);
1008 }
1009 
1010 
1011 static int
1012 netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
1013 	int prot, vm_page_t *mres)
1014 {
1015 	struct netmap_vm_handle_t *vmh = object->handle;
1016 	struct netmap_priv_d *priv = vmh->priv;
1017 	struct netmap_adapter *na = priv->np_na;
1018 	vm_paddr_t paddr;
1019 	vm_page_t page;
1020 	vm_memattr_t memattr;
1021 	vm_pindex_t pidx;
1022 
1023 	nm_prdis("object %p offset %jd prot %d mres %p",
1024 			object, (intmax_t)offset, prot, mres);
1025 	memattr = object->memattr;
1026 	pidx = OFF_TO_IDX(offset);
1027 	paddr = netmap_mem_ofstophys(na->nm_mem, offset);
1028 	if (paddr == 0)
1029 		return VM_PAGER_FAIL;
1030 
1031 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
1032 		/*
1033 		 * If the passed in result page is a fake page, update it with
1034 		 * the new physical address.
1035 		 */
1036 		page = *mres;
1037 		vm_page_updatefake(page, paddr, memattr);
1038 	} else {
1039 		/*
1040 		 * Replace the passed in reqpage page with our own fake page and
1041 		 * free up the all of the original pages.
1042 		 */
1043 #ifndef VM_OBJECT_WUNLOCK	/* FreeBSD < 10.x */
1044 #define VM_OBJECT_WUNLOCK VM_OBJECT_UNLOCK
1045 #define VM_OBJECT_WLOCK	VM_OBJECT_LOCK
1046 #endif /* VM_OBJECT_WUNLOCK */
1047 
1048 		VM_OBJECT_WUNLOCK(object);
1049 		page = vm_page_getfake(paddr, memattr);
1050 		VM_OBJECT_WLOCK(object);
1051 		vm_page_lock(*mres);
1052 		vm_page_free(*mres);
1053 		vm_page_unlock(*mres);
1054 		*mres = page;
1055 		vm_page_insert(page, object, pidx);
1056 	}
1057 	page->valid = VM_PAGE_BITS_ALL;
1058 	return (VM_PAGER_OK);
1059 }
1060 
1061 
1062 static struct cdev_pager_ops netmap_cdev_pager_ops = {
1063 	.cdev_pg_ctor = netmap_dev_pager_ctor,
1064 	.cdev_pg_dtor = netmap_dev_pager_dtor,
1065 	.cdev_pg_fault = netmap_dev_pager_fault,
1066 };
1067 
1068 
1069 static int
1070 netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
1071 	vm_size_t objsize,  vm_object_t *objp, int prot)
1072 {
1073 	int error;
1074 	struct netmap_vm_handle_t *vmh;
1075 	struct netmap_priv_d *priv;
1076 	vm_object_t obj;
1077 
1078 	if (netmap_verbose)
1079 		nm_prinf("cdev %p foff %jd size %jd objp %p prot %d", cdev,
1080 		    (intmax_t )*foff, (intmax_t )objsize, objp, prot);
1081 
1082 	vmh = malloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
1083 			      M_NOWAIT | M_ZERO);
1084 	if (vmh == NULL)
1085 		return ENOMEM;
1086 	vmh->dev = cdev;
1087 
1088 	NMG_LOCK();
1089 	error = devfs_get_cdevpriv((void**)&priv);
1090 	if (error)
1091 		goto err_unlock;
1092 	if (priv->np_nifp == NULL) {
1093 		error = EINVAL;
1094 		goto err_unlock;
1095 	}
1096 	vmh->priv = priv;
1097 	priv->np_refs++;
1098 	NMG_UNLOCK();
1099 
1100 	obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
1101 		&netmap_cdev_pager_ops, objsize, prot,
1102 		*foff, NULL);
1103 	if (obj == NULL) {
1104 		nm_prerr("cdev_pager_allocate failed");
1105 		error = EINVAL;
1106 		goto err_deref;
1107 	}
1108 
1109 	*objp = obj;
1110 	return 0;
1111 
1112 err_deref:
1113 	NMG_LOCK();
1114 	priv->np_refs--;
1115 err_unlock:
1116 	NMG_UNLOCK();
1117 // err:
1118 	free(vmh, M_DEVBUF);
1119 	return error;
1120 }
1121 
1122 /*
1123  * On FreeBSD the close routine is only called on the last close on
1124  * the device (/dev/netmap) so we cannot do anything useful.
1125  * To track close() on individual file descriptors we pass netmap_dtor() to
1126  * devfs_set_cdevpriv() on open(). The FreeBSD kernel will call the destructor
1127  * when the last fd pointing to the device is closed.
1128  *
1129  * Note that FreeBSD does not even munmap() on close() so we also have
1130  * to track mmap() ourselves, and postpone the call to
1131  * netmap_dtor() is called when the process has no open fds and no active
1132  * memory maps on /dev/netmap, as in linux.
1133  */
1134 static int
1135 netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1136 {
1137 	if (netmap_verbose)
1138 		nm_prinf("dev %p fflag 0x%x devtype %d td %p",
1139 			dev, fflag, devtype, td);
1140 	return 0;
1141 }
1142 
1143 
1144 static int
1145 netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1146 {
1147 	struct netmap_priv_d *priv;
1148 	int error;
1149 
1150 	(void)dev;
1151 	(void)oflags;
1152 	(void)devtype;
1153 	(void)td;
1154 
1155 	NMG_LOCK();
1156 	priv = netmap_priv_new();
1157 	if (priv == NULL) {
1158 		error = ENOMEM;
1159 		goto out;
1160 	}
1161 	error = devfs_set_cdevpriv(priv, netmap_dtor);
1162 	if (error) {
1163 		netmap_priv_delete(priv);
1164 	}
1165 out:
1166 	NMG_UNLOCK();
1167 	return error;
1168 }
1169 
1170 /******************** kthread wrapper ****************/
1171 #include <sys/sysproto.h>
1172 u_int
1173 nm_os_ncpus(void)
1174 {
1175 	return mp_maxid + 1;
1176 }
1177 
1178 struct nm_kctx_ctx {
1179 	/* Userspace thread (kthread creator). */
1180 	struct thread *user_td;
1181 
1182 	/* worker function and parameter */
1183 	nm_kctx_worker_fn_t worker_fn;
1184 	void *worker_private;
1185 
1186 	struct nm_kctx *nmk;
1187 
1188 	/* integer to manage multiple worker contexts (e.g., RX or TX on ptnetmap) */
1189 	long type;
1190 };
1191 
1192 struct nm_kctx {
1193 	struct thread *worker;
1194 	struct mtx worker_lock;
1195 	struct nm_kctx_ctx worker_ctx;
1196 	int run;			/* used to stop kthread */
1197 	int attach_user;		/* kthread attached to user_process */
1198 	int affinity;
1199 };
1200 
1201 static void
1202 nm_kctx_worker(void *data)
1203 {
1204 	struct nm_kctx *nmk = data;
1205 	struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
1206 
1207 	if (nmk->affinity >= 0) {
1208 		thread_lock(curthread);
1209 		sched_bind(curthread, nmk->affinity);
1210 		thread_unlock(curthread);
1211 	}
1212 
1213 	while (nmk->run) {
1214 		/*
1215 		 * check if the parent process dies
1216 		 * (when kthread is attached to user process)
1217 		 */
1218 		if (ctx->user_td) {
1219 			PROC_LOCK(curproc);
1220 			thread_suspend_check(0);
1221 			PROC_UNLOCK(curproc);
1222 		} else {
1223 			kthread_suspend_check();
1224 		}
1225 
1226 		/* Continuously execute worker process. */
1227 		ctx->worker_fn(ctx->worker_private); /* worker body */
1228 	}
1229 
1230 	kthread_exit();
1231 }
1232 
1233 void
1234 nm_os_kctx_worker_setaff(struct nm_kctx *nmk, int affinity)
1235 {
1236 	nmk->affinity = affinity;
1237 }
1238 
1239 struct nm_kctx *
1240 nm_os_kctx_create(struct nm_kctx_cfg *cfg, void *opaque)
1241 {
1242 	struct nm_kctx *nmk = NULL;
1243 
1244 	nmk = malloc(sizeof(*nmk),  M_DEVBUF, M_NOWAIT | M_ZERO);
1245 	if (!nmk)
1246 		return NULL;
1247 
1248 	mtx_init(&nmk->worker_lock, "nm_kthread lock", NULL, MTX_DEF);
1249 	nmk->worker_ctx.worker_fn = cfg->worker_fn;
1250 	nmk->worker_ctx.worker_private = cfg->worker_private;
1251 	nmk->worker_ctx.type = cfg->type;
1252 	nmk->affinity = -1;
1253 
1254 	/* attach kthread to user process (ptnetmap) */
1255 	nmk->attach_user = cfg->attach_user;
1256 
1257 	return nmk;
1258 }
1259 
1260 int
1261 nm_os_kctx_worker_start(struct nm_kctx *nmk)
1262 {
1263 	struct proc *p = NULL;
1264 	int error = 0;
1265 
1266 	/* Temporarily disable this function as it is currently broken
1267 	 * and causes kernel crashes. The failure can be triggered by
1268 	 * the "vale_polling_enable_disable" test in ctrl-api-test.c. */
1269 	return EOPNOTSUPP;
1270 
1271 	if (nmk->worker)
1272 		return EBUSY;
1273 
1274 	/* check if we want to attach kthread to user process */
1275 	if (nmk->attach_user) {
1276 		nmk->worker_ctx.user_td = curthread;
1277 		p = curthread->td_proc;
1278 	}
1279 
1280 	/* enable kthread main loop */
1281 	nmk->run = 1;
1282 	/* create kthread */
1283 	if((error = kthread_add(nm_kctx_worker, nmk, p,
1284 			&nmk->worker, RFNOWAIT /* to be checked */, 0, "nm-kthread-%ld",
1285 			nmk->worker_ctx.type))) {
1286 		goto err;
1287 	}
1288 
1289 	nm_prinf("nm_kthread started td %p", nmk->worker);
1290 
1291 	return 0;
1292 err:
1293 	nm_prerr("nm_kthread start failed err %d", error);
1294 	nmk->worker = NULL;
1295 	return error;
1296 }
1297 
1298 void
1299 nm_os_kctx_worker_stop(struct nm_kctx *nmk)
1300 {
1301 	if (!nmk->worker)
1302 		return;
1303 
1304 	/* tell to kthread to exit from main loop */
1305 	nmk->run = 0;
1306 
1307 	/* wake up kthread if it sleeps */
1308 	kthread_resume(nmk->worker);
1309 
1310 	nmk->worker = NULL;
1311 }
1312 
1313 void
1314 nm_os_kctx_destroy(struct nm_kctx *nmk)
1315 {
1316 	if (!nmk)
1317 		return;
1318 
1319 	if (nmk->worker)
1320 		nm_os_kctx_worker_stop(nmk);
1321 
1322 	free(nmk, M_DEVBUF);
1323 }
1324 
1325 /******************** kqueue support ****************/
1326 
1327 /*
1328  * In addition to calling selwakeuppri(), nm_os_selwakeup() also
1329  * needs to call knote() to wake up kqueue listeners.
1330  * This operation is deferred to a taskqueue in order to avoid possible
1331  * lock order reversals; these may happen because knote() grabs a
1332  * private lock associated to the 'si' (see struct selinfo,
1333  * struct nm_selinfo, and nm_os_selinfo_init), and nm_os_selwakeup()
1334  * can be called while holding the lock associated to a different
1335  * 'si'.
1336  * When calling knote() we use a non-zero 'hint' argument to inform
1337  * the netmap_knrw() function that it is being called from
1338  * 'nm_os_selwakeup'; this is necessary because when netmap_knrw() is
1339  * called by the kevent subsystem (i.e. kevent_scan()) we also need to
1340  * call netmap_poll().
1341  *
1342  * The netmap_kqfilter() function registers one or another f_event
1343  * depending on read or write mode. A pointer to the struct
1344  * 'netmap_priv_d' is stored into kn->kn_hook, so that it can later
1345  * be passed to netmap_poll(). We pass NULL as a third argument to
1346  * netmap_poll(), so that the latter only runs the txsync/rxsync
1347  * (if necessary), and skips the nm_os_selrecord() calls.
1348  */
1349 
1350 
1351 void
1352 nm_os_selwakeup(struct nm_selinfo *si)
1353 {
1354 	selwakeuppri(&si->si, PI_NET);
1355 	if (si->kqueue_users > 0) {
1356 		taskqueue_enqueue(si->ntfytq, &si->ntfytask);
1357 	}
1358 }
1359 
1360 void
1361 nm_os_selrecord(struct thread *td, struct nm_selinfo *si)
1362 {
1363 	selrecord(td, &si->si);
1364 }
1365 
1366 static void
1367 netmap_knrdetach(struct knote *kn)
1368 {
1369 	struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1370 	struct nm_selinfo *si = priv->np_si[NR_RX];
1371 
1372 	knlist_remove(&si->si.si_note, kn, /*islocked=*/0);
1373 	NMG_LOCK();
1374 	KASSERT(si->kqueue_users > 0, ("kqueue_user underflow on %s",
1375 	    si->mtxname));
1376 	si->kqueue_users--;
1377 	nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
1378 	NMG_UNLOCK();
1379 }
1380 
1381 static void
1382 netmap_knwdetach(struct knote *kn)
1383 {
1384 	struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1385 	struct nm_selinfo *si = priv->np_si[NR_TX];
1386 
1387 	knlist_remove(&si->si.si_note, kn, /*islocked=*/0);
1388 	NMG_LOCK();
1389 	si->kqueue_users--;
1390 	nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
1391 	NMG_UNLOCK();
1392 }
1393 
1394 /*
1395  * Callback triggered by netmap notifications (see netmap_notify()),
1396  * and by the application calling kevent(). In the former case we
1397  * just return 1 (events ready), since we are not able to do better.
1398  * In the latter case we use netmap_poll() to see which events are
1399  * ready.
1400  */
1401 static int
1402 netmap_knrw(struct knote *kn, long hint, int events)
1403 {
1404 	struct netmap_priv_d *priv;
1405 	int revents;
1406 
1407 	if (hint != 0) {
1408 		/* Called from netmap_notify(), typically from a
1409 		 * thread different from the one issuing kevent().
1410 		 * Assume we are ready. */
1411 		return 1;
1412 	}
1413 
1414 	/* Called from kevent(). */
1415 	priv = kn->kn_hook;
1416 	revents = netmap_poll(priv, events, /*thread=*/NULL);
1417 
1418 	return (events & revents) ? 1 : 0;
1419 }
1420 
1421 static int
1422 netmap_knread(struct knote *kn, long hint)
1423 {
1424 	return netmap_knrw(kn, hint, POLLIN);
1425 }
1426 
1427 static int
1428 netmap_knwrite(struct knote *kn, long hint)
1429 {
1430 	return netmap_knrw(kn, hint, POLLOUT);
1431 }
1432 
1433 static struct filterops netmap_rfiltops = {
1434 	.f_isfd = 1,
1435 	.f_detach = netmap_knrdetach,
1436 	.f_event = netmap_knread,
1437 };
1438 
1439 static struct filterops netmap_wfiltops = {
1440 	.f_isfd = 1,
1441 	.f_detach = netmap_knwdetach,
1442 	.f_event = netmap_knwrite,
1443 };
1444 
1445 
1446 /*
1447  * This is called when a thread invokes kevent() to record
1448  * a change in the configuration of the kqueue().
1449  * The 'priv' is the one associated to the open netmap device.
1450  */
1451 static int
1452 netmap_kqfilter(struct cdev *dev, struct knote *kn)
1453 {
1454 	struct netmap_priv_d *priv;
1455 	int error;
1456 	struct netmap_adapter *na;
1457 	struct nm_selinfo *si;
1458 	int ev = kn->kn_filter;
1459 
1460 	if (ev != EVFILT_READ && ev != EVFILT_WRITE) {
1461 		nm_prerr("bad filter request %d", ev);
1462 		return 1;
1463 	}
1464 	error = devfs_get_cdevpriv((void**)&priv);
1465 	if (error) {
1466 		nm_prerr("device not yet setup");
1467 		return 1;
1468 	}
1469 	na = priv->np_na;
1470 	if (na == NULL) {
1471 		nm_prerr("no netmap adapter for this file descriptor");
1472 		return 1;
1473 	}
1474 	/* the si is indicated in the priv */
1475 	si = priv->np_si[(ev == EVFILT_WRITE) ? NR_TX : NR_RX];
1476 	kn->kn_fop = (ev == EVFILT_WRITE) ?
1477 		&netmap_wfiltops : &netmap_rfiltops;
1478 	kn->kn_hook = priv;
1479 	NMG_LOCK();
1480 	si->kqueue_users++;
1481 	nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
1482 	NMG_UNLOCK();
1483 	knlist_add(&si->si.si_note, kn, /*islocked=*/0);
1484 
1485 	return 0;
1486 }
1487 
1488 static int
1489 freebsd_netmap_poll(struct cdev *cdevi __unused, int events, struct thread *td)
1490 {
1491 	struct netmap_priv_d *priv;
1492 	if (devfs_get_cdevpriv((void **)&priv)) {
1493 		return POLLERR;
1494 	}
1495 	return netmap_poll(priv, events, td);
1496 }
1497 
1498 static int
1499 freebsd_netmap_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
1500 		int ffla __unused, struct thread *td)
1501 {
1502 	int error;
1503 	struct netmap_priv_d *priv;
1504 
1505 	CURVNET_SET(TD_TO_VNET(td));
1506 	error = devfs_get_cdevpriv((void **)&priv);
1507 	if (error) {
1508 		/* XXX ENOENT should be impossible, since the priv
1509 		 * is now created in the open */
1510 		if (error == ENOENT)
1511 			error = ENXIO;
1512 		goto out;
1513 	}
1514 	error = netmap_ioctl(priv, cmd, data, td, /*nr_body_is_user=*/1);
1515 out:
1516 	CURVNET_RESTORE();
1517 
1518 	return error;
1519 }
1520 
1521 void
1522 nm_os_onattach(struct ifnet *ifp)
1523 {
1524 	ifp->if_capabilities |= IFCAP_NETMAP;
1525 }
1526 
1527 void
1528 nm_os_onenter(struct ifnet *ifp)
1529 {
1530 	struct netmap_adapter *na = NA(ifp);
1531 
1532 	na->if_transmit = ifp->if_transmit;
1533 	ifp->if_transmit = netmap_transmit;
1534 	ifp->if_capenable |= IFCAP_NETMAP;
1535 }
1536 
1537 void
1538 nm_os_onexit(struct ifnet *ifp)
1539 {
1540 	struct netmap_adapter *na = NA(ifp);
1541 
1542 	ifp->if_transmit = na->if_transmit;
1543 	ifp->if_capenable &= ~IFCAP_NETMAP;
1544 }
1545 
1546 extern struct cdevsw netmap_cdevsw; /* XXX used in netmap.c, should go elsewhere */
1547 struct cdevsw netmap_cdevsw = {
1548 	.d_version = D_VERSION,
1549 	.d_name = "netmap",
1550 	.d_open = netmap_open,
1551 	.d_mmap_single = netmap_mmap_single,
1552 	.d_ioctl = freebsd_netmap_ioctl,
1553 	.d_poll = freebsd_netmap_poll,
1554 	.d_kqfilter = netmap_kqfilter,
1555 	.d_close = netmap_close,
1556 };
1557 /*--- end of kqueue support ----*/
1558 
1559 /*
1560  * Kernel entry point.
1561  *
1562  * Initialize/finalize the module and return.
1563  *
1564  * Return 0 on success, errno on failure.
1565  */
1566 static int
1567 netmap_loader(__unused struct module *module, int event, __unused void *arg)
1568 {
1569 	int error = 0;
1570 
1571 	switch (event) {
1572 	case MOD_LOAD:
1573 		error = netmap_init();
1574 		break;
1575 
1576 	case MOD_UNLOAD:
1577 		/*
1578 		 * if some one is still using netmap,
1579 		 * then the module can not be unloaded.
1580 		 */
1581 		if (netmap_use_count) {
1582 			nm_prerr("netmap module can not be unloaded - netmap_use_count: %d",
1583 					netmap_use_count);
1584 			error = EBUSY;
1585 			break;
1586 		}
1587 		netmap_fini();
1588 		break;
1589 
1590 	default:
1591 		error = EOPNOTSUPP;
1592 		break;
1593 	}
1594 
1595 	return (error);
1596 }
1597 
1598 #ifdef DEV_MODULE_ORDERED
1599 /*
1600  * The netmap module contains three drivers: (i) the netmap character device
1601  * driver; (ii) the ptnetmap memdev PCI device driver, (iii) the ptnet PCI
1602  * device driver. The attach() routines of both (ii) and (iii) need the
1603  * lock of the global allocator, and such lock is initialized in netmap_init(),
1604  * which is part of (i).
1605  * Therefore, we make sure that (i) is loaded before (ii) and (iii), using
1606  * the 'order' parameter of driver declaration macros. For (i), we specify
1607  * SI_ORDER_MIDDLE, while higher orders are used with the DRIVER_MODULE_ORDERED
1608  * macros for (ii) and (iii).
1609  */
1610 DEV_MODULE_ORDERED(netmap, netmap_loader, NULL, SI_ORDER_MIDDLE);
1611 #else /* !DEV_MODULE_ORDERED */
1612 DEV_MODULE(netmap, netmap_loader, NULL);
1613 #endif /* DEV_MODULE_ORDERED  */
1614 MODULE_DEPEND(netmap, pci, 1, 1, 1);
1615 MODULE_VERSION(netmap, 1);
1616 /* reduce conditional code */
1617 // linux API, use for the knlist in FreeBSD
1618 /* use a private mutex for the knlist */
1619