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