2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include "opt_inet6.h"
32 #include <sys/param.h>
33 #include <sys/module.h>
34 #include <sys/errno.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() */
42 #include <sys/rwlock.h>
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>
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 */
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() */
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>
76 /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
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);
85 nm_os_selinfo_uninit(NM_SELINFO_T *si)
87 /* XXX kqueue(9) needed; these will mirror knlist_init. */
88 knlist_delete(&si->si.si_note, curthread, 0 /* not locked */ );
89 knlist_destroy(&si->si.si_note);
90 /* now we don't need the mutex anymore */
95 nm_os_malloc(size_t size)
97 return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
101 nm_os_realloc(void *addr, size_t new_size, size_t old_size __unused)
103 return realloc(addr, new_size, M_DEVBUF, M_NOWAIT | M_ZERO);
107 nm_os_free(void *addr)
109 free(addr, M_DEVBUF);
113 nm_os_ifnet_lock(void)
119 nm_os_ifnet_unlock(void)
124 static int netmap_use_count = 0;
127 nm_os_get_module(void)
133 nm_os_put_module(void)
139 netmap_ifnet_arrival_handler(void *arg __unused, struct ifnet *ifp)
141 netmap_undo_zombie(ifp);
145 netmap_ifnet_departure_handler(void *arg __unused, struct ifnet *ifp)
147 netmap_make_zombie(ifp);
150 static eventhandler_tag nm_ifnet_ah_tag;
151 static eventhandler_tag nm_ifnet_dh_tag;
154 nm_os_ifnet_init(void)
157 EVENTHANDLER_REGISTER(ifnet_arrival_event,
158 netmap_ifnet_arrival_handler,
159 NULL, EVENTHANDLER_PRI_ANY);
161 EVENTHANDLER_REGISTER(ifnet_departure_event,
162 netmap_ifnet_departure_handler,
163 NULL, EVENTHANDLER_PRI_ANY);
168 nm_os_ifnet_fini(void)
170 EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
172 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
177 nm_os_ifnet_mtu(struct ifnet *ifp)
179 #if __FreeBSD_version < 1100030
180 return ifp->if_data.ifi_mtu;
181 #else /* __FreeBSD_version >= 1100030 */
187 nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum)
189 /* TODO XXX please use the FreeBSD implementation for this. */
190 uint16_t *words = (uint16_t *)data;
194 for (i = 0; i < nw; i++)
195 cur_sum += be16toh(words[i]);
198 cur_sum += (data[len-1] << 8);
203 /* Fold a raw checksum: 'cur_sum' is in host byte order, while the
204 * return value is in network byte order.
207 nm_os_csum_fold(rawsum_t cur_sum)
209 /* TODO XXX please use the FreeBSD implementation for this. */
210 while (cur_sum >> 16)
211 cur_sum = (cur_sum & 0xFFFF) + (cur_sum >> 16);
213 return htobe16((~cur_sum) & 0xFFFF);
216 uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph)
219 return in_cksum_hdr((void *)iph);
221 return nm_os_csum_fold(nm_os_csum_raw((uint8_t*)iph, sizeof(struct nm_iphdr), 0));
226 nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
227 size_t datalen, uint16_t *check)
230 uint16_t pseudolen = datalen + iph->protocol;
232 /* Compute and insert the pseudo-header cheksum. */
233 *check = in_pseudo(iph->saddr, iph->daddr,
235 /* Compute the checksum on TCP/UDP header + payload
236 * (includes the pseudo-header).
238 *check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
240 static int notsupported = 0;
243 D("inet4 segmentation not supported");
249 nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
250 size_t datalen, uint16_t *check)
253 *check = in6_cksum_pseudo((void*)ip6h, datalen, ip6h->nexthdr, 0);
254 *check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
256 static int notsupported = 0;
259 D("inet6 segmentation not supported");
264 /* on FreeBSD we send up one packet at a time */
266 nm_os_send_up(struct ifnet *ifp, struct mbuf *m, struct mbuf *prev)
268 NA(ifp)->if_input(ifp, m);
273 nm_os_mbuf_has_csum_offld(struct mbuf *m)
275 return m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_SCTP |
276 CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |
281 nm_os_mbuf_has_seg_offld(struct mbuf *m)
283 return m->m_pkthdr.csum_flags & CSUM_TSO;
287 freebsd_generic_rx_handler(struct ifnet *ifp, struct mbuf *m)
291 if (!NM_NA_VALID(ifp)) {
292 RD(1, "Warning: got RX packet for invalid emulated adapter");
296 stolen = generic_rx_handler(ifp, m);
298 struct netmap_generic_adapter *gna =
299 (struct netmap_generic_adapter *)NA(ifp);
300 gna->save_if_input(ifp, m);
305 * Intercept the rx routine in the standard device driver.
306 * Second argument is non-zero to intercept, 0 to restore
309 nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept)
311 struct netmap_adapter *na = &gna->up.up;
312 struct ifnet *ifp = na->ifp;
317 if (gna->save_if_input) {
318 D("cannot intercept again");
319 ret = EINVAL; /* already set */
322 gna->save_if_input = ifp->if_input;
323 ifp->if_input = freebsd_generic_rx_handler;
325 if (!gna->save_if_input){
327 ret = EINVAL; /* not saved */
330 ifp->if_input = gna->save_if_input;
331 gna->save_if_input = NULL;
334 nm_os_ifnet_unlock();
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.
347 nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
349 struct netmap_adapter *na = &gna->up.up;
350 struct ifnet *ifp = netmap_generic_getifp(gna);
354 na->if_transmit = ifp->if_transmit;
355 ifp->if_transmit = netmap_transmit;
357 ifp->if_transmit = na->if_transmit;
359 nm_os_ifnet_unlock();
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.
371 * We should add a reference to the mbuf so the m_freem() at the end
372 * of the transmission does not consume resources.
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;
378 * i = curcpu % adapter->num_queues;
382 nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
386 struct ifnet *ifp = a->ifp;
387 struct mbuf *m = a->m;
389 #if __FreeBSD_version < 1100000
391 * Old FreeBSD versions. The mbuf has a cluster attached,
392 * we need to copy from the cluster to the netmap buffer.
394 if (MBUF_REFCNT(m) != 1) {
395 D("invalid refcnt %d for %p", MBUF_REFCNT(m), m);
396 panic("in generic_xmit_frame");
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;
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 */
410 m->m_len = m->m_pkthdr.len = len;
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);
423 #if __FreeBSD_version >= 1100005
424 struct netmap_adapter *
425 netmap_getna(if_t ifp)
427 return (NA((struct ifnet *)ifp));
429 #endif /* __FreeBSD_version >= 1100005 */
432 * The following two functions are empty until we have a generic
433 * way to extract the info from the ifp
436 nm_os_generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
443 nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
445 unsigned num_rings = netmap_generic_rings ? netmap_generic_rings : 1;
452 nm_os_generic_set_features(struct netmap_generic_adapter *gna)
455 gna->rxsg = 1; /* Supported through m_copydata. */
456 gna->txqdisc = 0; /* Not supported. */
460 nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, struct netmap_adapter *na)
463 mit->mit_pending = 0;
464 mit->mit_ring_idx = idx;
470 nm_os_mitigation_start(struct nm_generic_mit *mit)
477 nm_os_mitigation_restart(struct nm_generic_mit *mit)
484 nm_os_mitigation_active(struct nm_generic_mit *mit)
492 nm_os_mitigation_cleanup(struct nm_generic_mit *mit)
498 nm_vi_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
504 nm_vi_start(struct ifnet *ifp)
506 panic("nm_vi_start() must not be called");
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.
514 #define NM_VI_MAX 255
516 uint8_t index[NM_VI_MAX]; /* XXX just for a reasonable number */
522 nm_os_vi_init_index(void)
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);
531 /* return -1 if no index available */
533 nm_vi_get_index(void)
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);
545 nm_vi_free_index(uint8_t val)
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--;
561 if (lim == nm_vi_indices.active)
562 D("funny, index %u didn't found", val);
563 mtx_unlock(&nm_vi_indices.lock);
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.
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().
578 nm_os_vi_persist(const char *name, struct ifnet **ret)
582 uint32_t macaddr_mid;
584 int unit = nm_vi_get_index(); /* just to decide MAC address */
589 * We use the same MAC address generation method with tap
590 * except for the highest octet is 00:be instead of 00:bd
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;
598 ifp = if_alloc(IFT_ETHER);
600 D("if_alloc failed");
603 if_initname(ifp, name, IF_DUNIT_NONE);
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;
614 ether_ifattach(ifp, eaddr);
619 /* unregister from the system and drop the final refcount */
621 nm_os_vi_detach(struct ifnet *ifp)
623 nm_vi_free_index(((char *)IF_LLADDR(ifp))[5]);
629 #include <vm/vm_map.h>
630 #include <vm/vm_kern.h>
631 struct nm_os_extmem {
639 nm_os_extmem_delete(struct nm_os_extmem *e)
641 D("freeing %zx bytes", (size_t)e->size);
642 vm_map_remove(kernel_map, e->kva, e->kva + e->size);
647 nm_os_extmem_nextpage(struct nm_os_extmem *e)
650 if (e->scan < e->kva + e->size) {
651 rv = (char *)e->scan;
652 e->scan += PAGE_SIZE;
658 nm_os_extmem_isequal(struct nm_os_extmem *e1, struct nm_os_extmem *e2)
660 return (e1->obj == e2->obj);
664 nm_os_extmem_nr_pages(struct nm_os_extmem *e)
666 return e->size >> PAGE_SHIFT;
669 struct nm_os_extmem *
670 nm_os_extmem_create(unsigned long p, struct nmreq_pools_info *pi, int *perror)
673 vm_map_entry_t entry;
678 struct nm_os_extmem *e = NULL;
681 e = nm_os_malloc(sizeof(*e));
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);
694 /* check that we are given the whole vm_object ? */
695 vm_map_lookup_done(map, entry);
697 // XXX can we really use obj after releasing the map lock?
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
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);
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");
725 vm_map_remove(kernel_map, e->kva, e->kva + e->size);
728 vm_object_deallocate(e->obj);
736 #endif /* WITH_EXTMEM */
738 /* ======================== PTNETMAP SUPPORT ========================== */
740 #ifdef WITH_PTNETMAP_GUEST
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>
748 * ptnetmap memory device (memdev) for freebsd guest,
749 * ssed to expose host netmap memory to the guest through a PCI BAR.
753 * ptnetmap memdev private data structure
755 struct ptnetmap_memdev {
757 struct resource *pci_io;
758 struct resource *pci_mem;
759 struct netmap_mem_d *nm_mem;
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);
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),
775 static driver_t ptn_memdev_driver = {
776 PTNETMAP_MEMDEV_NAME,
778 sizeof(struct ptnetmap_memdev),
781 /* We use (SI_ORDER_MIDDLE+1) here, see DEV_MODULE_ORDERED() invocation
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);
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.
793 nm_os_pt_memdev_iomap(struct ptnetmap_memdev *ptn_dev, vm_paddr_t *nm_paddr,
794 void **nm_addr, uint64_t *mem_size)
798 D("ptn_memdev_driver iomap");
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) |
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) {
814 *nm_paddr = rman_get_start(ptn_dev->pci_mem);
815 *nm_addr = rman_get_virtual(ptn_dev->pci_mem);
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);
826 nm_os_pt_memdev_ioread(struct ptnetmap_memdev *ptn_dev, unsigned int reg)
828 return bus_read_4(ptn_dev->pci_io, reg);
831 /* Unmap host netmap memory. */
833 nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *ptn_dev)
835 D("ptn_memdev_driver iounmap");
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;
844 /* Device identification routine, return BUS_PROBE_DEFAULT on success,
845 * positive on failure */
847 ptn_memdev_probe(device_t dev)
851 if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID)
853 if (pci_get_device(dev) != PTNETMAP_PCI_DEVICE_ID)
856 snprintf(desc, sizeof(desc), "%s PCI adapter",
857 PTNETMAP_MEMDEV_NAME);
858 device_set_desc_copy(dev, desc);
860 return (BUS_PROBE_DEFAULT);
863 /* Device initialization routine. */
865 ptn_memdev_attach(device_t dev)
867 struct ptnetmap_memdev *ptn_dev;
871 D("ptn_memdev_driver attach");
873 ptn_dev = device_get_softc(dev);
876 pci_enable_busmaster(dev);
878 rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
879 ptn_dev->pci_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
881 if (ptn_dev->pci_io == NULL) {
882 device_printf(dev, "cannot map I/O space\n");
886 mem_id = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMID);
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);
894 netmap_mem_get(ptn_dev->nm_mem);
896 D("ptn_memdev_driver probe OK - host_mem_id: %d", mem_id);
901 /* Device removal routine. */
903 ptn_memdev_detach(device_t dev)
905 struct ptnetmap_memdev *ptn_dev;
907 D("ptn_memdev_driver detach");
908 ptn_dev = device_get_softc(dev);
910 if (ptn_dev->nm_mem) {
911 netmap_mem_put(ptn_dev->nm_mem);
912 ptn_dev->nm_mem = NULL;
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;
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;
929 ptn_memdev_shutdown(device_t dev)
931 D("ptn_memdev_driver shutdown");
932 return bus_generic_shutdown(dev);
935 #endif /* WITH_PTNETMAP_GUEST */
938 * In order to track whether pages are still mapped, we hook into
939 * the standard cdev_pager and intercept the constructor and
943 struct netmap_vm_handle_t {
945 struct netmap_priv_d *priv;
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)
953 struct netmap_vm_handle_t *vmh = handle;
956 D("handle %p size %jd prot %d foff %jd",
957 handle, (intmax_t)size, prot, (intmax_t)foff);
966 netmap_dev_pager_dtor(void *handle)
968 struct netmap_vm_handle_t *vmh = handle;
969 struct cdev *dev = vmh->dev;
970 struct netmap_priv_d *priv = vmh->priv;
973 D("handle %p", handle);
981 netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
982 int prot, vm_page_t *mres)
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;
989 vm_memattr_t memattr;
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);
998 return VM_PAGER_FAIL;
1000 if (((*mres)->flags & PG_FICTITIOUS) != 0) {
1002 * If the passed in result page is a fake page, update it with
1003 * the new physical address.
1006 vm_page_updatefake(page, paddr, memattr);
1009 * Replace the passed in reqpage page with our own fake page and
1010 * free up the all of the original pages.
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 */
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);
1024 vm_page_insert(page, object, pidx);
1026 page->valid = VM_PAGE_BITS_ALL;
1027 return (VM_PAGER_OK);
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,
1039 netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
1040 vm_size_t objsize, vm_object_t *objp, int prot)
1043 struct netmap_vm_handle_t *vmh;
1044 struct netmap_priv_d *priv;
1048 D("cdev %p foff %jd size %jd objp %p prot %d", cdev,
1049 (intmax_t )*foff, (intmax_t )objsize, objp, prot);
1051 vmh = malloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
1058 error = devfs_get_cdevpriv((void**)&priv);
1061 if (priv->np_nifp == NULL) {
1069 obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
1070 &netmap_cdev_pager_ops, objsize, prot,
1073 D("cdev_pager_allocate failed");
1087 free(vmh, M_DEVBUF);
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.
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.
1104 netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1107 D("dev %p fflag 0x%x devtype %d td %p",
1108 dev, fflag, devtype, td);
1114 netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1116 struct netmap_priv_d *priv;
1125 priv = netmap_priv_new();
1130 error = devfs_set_cdevpriv(priv, netmap_dtor);
1132 netmap_priv_delete(priv);
1139 /******************** kthread wrapper ****************/
1140 #include <sys/sysproto.h>
1144 return mp_maxid + 1;
1147 struct nm_kctx_ctx {
1148 struct thread *user_td; /* thread user-space (kthread creator) to send ioctl */
1149 struct ptnetmap_cfgentry_bhyve cfg;
1151 /* worker function and parameter */
1152 nm_kctx_worker_fn_t worker_fn;
1153 void *worker_private;
1155 struct nm_kctx *nmk;
1157 /* integer to manage multiple worker contexts (e.g., RX or TX on ptnetmap) */
1162 struct thread *worker;
1163 struct mtx worker_lock;
1164 uint64_t scheduled; /* pending wake_up request */
1165 struct nm_kctx_ctx worker_ctx;
1166 int run; /* used to stop kthread */
1167 int attach_user; /* kthread attached to user_process */
1172 nm_os_kctx_worker_wakeup(struct nm_kctx *nmk)
1175 * There may be a race between FE and BE,
1176 * which call both this function, and worker kthread,
1177 * that reads nmk->scheduled.
1179 * For us it is not important the counter value,
1180 * but simply that it has changed since the last
1181 * time the kthread saw it.
1183 mtx_lock(&nmk->worker_lock);
1185 if (nmk->worker_ctx.cfg.wchan) {
1186 wakeup((void *)(uintptr_t)nmk->worker_ctx.cfg.wchan);
1188 mtx_unlock(&nmk->worker_lock);
1192 nm_os_kctx_send_irq(struct nm_kctx *nmk)
1194 struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
1197 if (ctx->user_td && ctx->cfg.ioctl_fd > 0) {
1198 err = kern_ioctl(ctx->user_td, ctx->cfg.ioctl_fd, ctx->cfg.ioctl_cmd,
1199 (caddr_t)&ctx->cfg.ioctl_data);
1201 D("kern_ioctl error: %d ioctl parameters: fd %d com %lu data %p",
1202 err, ctx->cfg.ioctl_fd, (unsigned long)ctx->cfg.ioctl_cmd,
1203 &ctx->cfg.ioctl_data);
1209 nm_kctx_worker(void *data)
1211 struct nm_kctx *nmk = data;
1212 struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
1213 uint64_t old_scheduled = nmk->scheduled;
1215 if (nmk->affinity >= 0) {
1216 thread_lock(curthread);
1217 sched_bind(curthread, nmk->affinity);
1218 thread_unlock(curthread);
1223 * check if the parent process dies
1224 * (when kthread is attached to user process)
1228 thread_suspend_check(0);
1229 PROC_UNLOCK(curproc);
1231 kthread_suspend_check();
1235 * if wchan is not defined, we don't have notification
1236 * mechanism and we continually execute worker_fn()
1238 if (!ctx->cfg.wchan) {
1239 ctx->worker_fn(ctx->worker_private, 1); /* worker body */
1241 /* checks if there is a pending notification */
1242 mtx_lock(&nmk->worker_lock);
1243 if (likely(nmk->scheduled != old_scheduled)) {
1244 old_scheduled = nmk->scheduled;
1245 mtx_unlock(&nmk->worker_lock);
1247 ctx->worker_fn(ctx->worker_private, 1); /* worker body */
1250 } else if (nmk->run) {
1251 /* wait on event with one second timeout */
1252 msleep((void *)(uintptr_t)ctx->cfg.wchan, &nmk->worker_lock,
1256 mtx_unlock(&nmk->worker_lock);
1264 nm_os_kctx_worker_setaff(struct nm_kctx *nmk, int affinity)
1266 nmk->affinity = affinity;
1270 nm_os_kctx_create(struct nm_kctx_cfg *cfg, void *opaque)
1272 struct nm_kctx *nmk = NULL;
1274 nmk = malloc(sizeof(*nmk), M_DEVBUF, M_NOWAIT | M_ZERO);
1278 mtx_init(&nmk->worker_lock, "nm_kthread lock", NULL, MTX_DEF);
1279 nmk->worker_ctx.worker_fn = cfg->worker_fn;
1280 nmk->worker_ctx.worker_private = cfg->worker_private;
1281 nmk->worker_ctx.type = cfg->type;
1284 /* attach kthread to user process (ptnetmap) */
1285 nmk->attach_user = cfg->attach_user;
1287 /* store kick/interrupt configuration */
1289 nmk->worker_ctx.cfg = *((struct ptnetmap_cfgentry_bhyve *)opaque);
1296 nm_os_kctx_worker_start(struct nm_kctx *nmk)
1298 struct proc *p = NULL;
1305 /* check if we want to attach kthread to user process */
1306 if (nmk->attach_user) {
1307 nmk->worker_ctx.user_td = curthread;
1308 p = curthread->td_proc;
1311 /* enable kthread main loop */
1313 /* create kthread */
1314 if((error = kthread_add(nm_kctx_worker, nmk, p,
1315 &nmk->worker, RFNOWAIT /* to be checked */, 0, "nm-kthread-%ld",
1316 nmk->worker_ctx.type))) {
1320 D("nm_kthread started td %p", nmk->worker);
1324 D("nm_kthread start failed err %d", error);
1330 nm_os_kctx_worker_stop(struct nm_kctx *nmk)
1335 /* tell to kthread to exit from main loop */
1338 /* wake up kthread if it sleeps */
1339 kthread_resume(nmk->worker);
1340 nm_os_kctx_worker_wakeup(nmk);
1346 nm_os_kctx_destroy(struct nm_kctx *nmk)
1351 nm_os_kctx_worker_stop(nmk);
1354 memset(&nmk->worker_ctx.cfg, 0, sizeof(nmk->worker_ctx.cfg));
1356 free(nmk, M_DEVBUF);
1359 /******************** kqueue support ****************/
1362 * nm_os_selwakeup also needs to issue a KNOTE_UNLOCKED.
1363 * We use a non-zero argument to distinguish the call from the one
1364 * in kevent_scan() which instead also needs to run netmap_poll().
1365 * The knote uses a global mutex for the time being. We might
1366 * try to reuse the one in the si, but it is not allocated
1367 * permanently so it might be a bit tricky.
1369 * The *kqfilter function registers one or another f_event
1370 * depending on read or write mode.
1371 * In the call to f_event() td_fpop is NULL so any child function
1372 * calling devfs_get_cdevpriv() would fail - and we need it in
1373 * netmap_poll(). As a workaround we store priv into kn->kn_hook
1374 * and pass it as first argument to netmap_poll(), which then
1375 * uses the failure to tell that we are called from f_event()
1376 * and do not need the selrecord().
1381 nm_os_selwakeup(struct nm_selinfo *si)
1384 D("on knote %p", &si->si.si_note);
1385 selwakeuppri(&si->si, PI_NET);
1386 /* use a non-zero hint to tell the notification from the
1387 * call done in kqueue_scan() which uses 0
1389 KNOTE_UNLOCKED(&si->si.si_note, 0x100 /* notification */);
1393 nm_os_selrecord(struct thread *td, struct nm_selinfo *si)
1395 selrecord(td, &si->si);
1399 netmap_knrdetach(struct knote *kn)
1401 struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1402 struct selinfo *si = &priv->np_si[NR_RX]->si;
1404 D("remove selinfo %p", si);
1405 knlist_remove(&si->si_note, kn, 0);
1409 netmap_knwdetach(struct knote *kn)
1411 struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1412 struct selinfo *si = &priv->np_si[NR_TX]->si;
1414 D("remove selinfo %p", si);
1415 knlist_remove(&si->si_note, kn, 0);
1419 * callback from notifies (generated externally) and our
1420 * calls to kevent(). The former we just return 1 (ready)
1421 * since we do not know better.
1422 * In the latter we call netmap_poll and return 0/1 accordingly.
1425 netmap_knrw(struct knote *kn, long hint, int events)
1427 struct netmap_priv_d *priv;
1431 ND(5, "call from notify");
1432 return 1; /* assume we are ready */
1435 /* the notification may come from an external thread,
1436 * in which case we do not want to run the netmap_poll
1437 * This should be filtered above, but check just in case.
1439 if (curthread != priv->np_td) { /* should not happen */
1440 RD(5, "curthread changed %p %p", curthread, priv->np_td);
1443 revents = netmap_poll(priv, events, NULL);
1444 return (events & revents) ? 1 : 0;
1449 netmap_knread(struct knote *kn, long hint)
1451 return netmap_knrw(kn, hint, POLLIN);
1455 netmap_knwrite(struct knote *kn, long hint)
1457 return netmap_knrw(kn, hint, POLLOUT);
1460 static struct filterops netmap_rfiltops = {
1462 .f_detach = netmap_knrdetach,
1463 .f_event = netmap_knread,
1466 static struct filterops netmap_wfiltops = {
1468 .f_detach = netmap_knwdetach,
1469 .f_event = netmap_knwrite,
1474 * This is called when a thread invokes kevent() to record
1475 * a change in the configuration of the kqueue().
1476 * The 'priv' should be the same as in the netmap device.
1479 netmap_kqfilter(struct cdev *dev, struct knote *kn)
1481 struct netmap_priv_d *priv;
1483 struct netmap_adapter *na;
1484 struct nm_selinfo *si;
1485 int ev = kn->kn_filter;
1487 if (ev != EVFILT_READ && ev != EVFILT_WRITE) {
1488 D("bad filter request %d", ev);
1491 error = devfs_get_cdevpriv((void**)&priv);
1493 D("device not yet setup");
1498 D("no netmap adapter for this file descriptor");
1501 /* the si is indicated in the priv */
1502 si = priv->np_si[(ev == EVFILT_WRITE) ? NR_TX : NR_RX];
1504 kn->kn_fop = (ev == EVFILT_WRITE) ?
1505 &netmap_wfiltops : &netmap_rfiltops;
1507 knlist_add(&si->si.si_note, kn, 0);
1509 ND("register %p %s td %p priv %p kn %p np_nifp %p kn_fp/fpop %s",
1510 na, na->ifp->if_xname, curthread, priv, kn,
1512 kn->kn_fp == curthread->td_fpop ? "match" : "MISMATCH");
1517 freebsd_netmap_poll(struct cdev *cdevi __unused, int events, struct thread *td)
1519 struct netmap_priv_d *priv;
1520 if (devfs_get_cdevpriv((void **)&priv)) {
1523 return netmap_poll(priv, events, td);
1527 freebsd_netmap_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
1528 int ffla __unused, struct thread *td)
1531 struct netmap_priv_d *priv;
1533 CURVNET_SET(TD_TO_VNET(td));
1534 error = devfs_get_cdevpriv((void **)&priv);
1536 /* XXX ENOENT should be impossible, since the priv
1537 * is now created in the open */
1538 if (error == ENOENT)
1542 error = netmap_ioctl(priv, cmd, data, td, /*nr_body_is_user=*/1);
1550 nm_os_onattach(struct ifnet *ifp)
1555 nm_os_onenter(struct ifnet *ifp)
1557 struct netmap_adapter *na = NA(ifp);
1559 na->if_transmit = ifp->if_transmit;
1560 ifp->if_transmit = netmap_transmit;
1561 ifp->if_capenable |= IFCAP_NETMAP;
1565 nm_os_onexit(struct ifnet *ifp)
1567 struct netmap_adapter *na = NA(ifp);
1569 ifp->if_transmit = na->if_transmit;
1570 ifp->if_capenable &= ~IFCAP_NETMAP;
1573 extern struct cdevsw netmap_cdevsw; /* XXX used in netmap.c, should go elsewhere */
1574 struct cdevsw netmap_cdevsw = {
1575 .d_version = D_VERSION,
1577 .d_open = netmap_open,
1578 .d_mmap_single = netmap_mmap_single,
1579 .d_ioctl = freebsd_netmap_ioctl,
1580 .d_poll = freebsd_netmap_poll,
1581 .d_kqfilter = netmap_kqfilter,
1582 .d_close = netmap_close,
1584 /*--- end of kqueue support ----*/
1587 * Kernel entry point.
1589 * Initialize/finalize the module and return.
1591 * Return 0 on success, errno on failure.
1594 netmap_loader(__unused struct module *module, int event, __unused void *arg)
1600 error = netmap_init();
1605 * if some one is still using netmap,
1606 * then the module can not be unloaded.
1608 if (netmap_use_count) {
1609 D("netmap module can not be unloaded - netmap_use_count: %d",
1625 #ifdef DEV_MODULE_ORDERED
1627 * The netmap module contains three drivers: (i) the netmap character device
1628 * driver; (ii) the ptnetmap memdev PCI device driver, (iii) the ptnet PCI
1629 * device driver. The attach() routines of both (ii) and (iii) need the
1630 * lock of the global allocator, and such lock is initialized in netmap_init(),
1631 * which is part of (i).
1632 * Therefore, we make sure that (i) is loaded before (ii) and (iii), using
1633 * the 'order' parameter of driver declaration macros. For (i), we specify
1634 * SI_ORDER_MIDDLE, while higher orders are used with the DRIVER_MODULE_ORDERED
1635 * macros for (ii) and (iii).
1637 DEV_MODULE_ORDERED(netmap, netmap_loader, NULL, SI_ORDER_MIDDLE);
1638 #else /* !DEV_MODULE_ORDERED */
1639 DEV_MODULE(netmap, netmap_loader, NULL);
1640 #endif /* DEV_MODULE_ORDERED */
1641 MODULE_DEPEND(netmap, pci, 1, 1, 1);
1642 MODULE_VERSION(netmap, 1);
1643 /* reduce conditional code */
1644 // linux API, use for the knlist in FreeBSD
1645 /* use a private mutex for the knlist */