1f9790aebSLuigi Rizzo /* 217885a7bSLuigi Rizzo * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved. 3f9790aebSLuigi Rizzo * 4f9790aebSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 5f9790aebSLuigi Rizzo * modification, are permitted provided that the following conditions 6f9790aebSLuigi Rizzo * are met: 7f9790aebSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 8f9790aebSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 9f9790aebSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 10f9790aebSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 11f9790aebSLuigi Rizzo * documentation and/or other materials provided with the distribution. 12f9790aebSLuigi Rizzo * 13f9790aebSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14f9790aebSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15f9790aebSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16f9790aebSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17f9790aebSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18f9790aebSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19f9790aebSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20f9790aebSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21f9790aebSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22f9790aebSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23f9790aebSLuigi Rizzo * SUCH DAMAGE. 24f9790aebSLuigi Rizzo */ 25f9790aebSLuigi Rizzo 26f9790aebSLuigi Rizzo /* $FreeBSD$ */ 27847bf383SLuigi Rizzo #include "opt_inet.h" 28847bf383SLuigi Rizzo #include "opt_inet6.h" 29f9790aebSLuigi Rizzo 30*ffaa5debSSepherosa Ziehau #include <sys/param.h> 31f9790aebSLuigi Rizzo #include <sys/module.h> 32f9790aebSLuigi Rizzo #include <sys/errno.h> 33*ffaa5debSSepherosa Ziehau #include <sys/jail.h> 34f0ea3689SLuigi Rizzo #include <sys/poll.h> /* POLLIN, POLLOUT */ 35f9790aebSLuigi Rizzo #include <sys/kernel.h> /* types used in module initialization */ 3637e3a6d3SLuigi Rizzo #include <sys/conf.h> /* DEV_MODULE_ORDERED */ 37f0ea3689SLuigi Rizzo #include <sys/endian.h> 3837e3a6d3SLuigi Rizzo #include <sys/syscallsubr.h> /* kern_ioctl() */ 39f9790aebSLuigi Rizzo 40f9790aebSLuigi Rizzo #include <sys/rwlock.h> 41f9790aebSLuigi Rizzo 42f9790aebSLuigi Rizzo #include <vm/vm.h> /* vtophys */ 43f9790aebSLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 44f9790aebSLuigi Rizzo #include <vm/vm_param.h> 45f9790aebSLuigi Rizzo #include <vm/vm_object.h> 46f9790aebSLuigi Rizzo #include <vm/vm_page.h> 47f9790aebSLuigi Rizzo #include <vm/vm_pager.h> 48f9790aebSLuigi Rizzo #include <vm/uma.h> 49f9790aebSLuigi Rizzo 50f9790aebSLuigi Rizzo 51f9790aebSLuigi Rizzo #include <sys/malloc.h> 52f9790aebSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 53f9790aebSLuigi Rizzo #include <sys/selinfo.h> 5437e3a6d3SLuigi Rizzo #include <sys/kthread.h> /* kthread_add() */ 5537e3a6d3SLuigi Rizzo #include <sys/proc.h> /* PROC_LOCK() */ 5637e3a6d3SLuigi Rizzo #include <sys/unistd.h> /* RFNOWAIT */ 5737e3a6d3SLuigi Rizzo #include <sys/sched.h> /* sched_bind() */ 5837e3a6d3SLuigi Rizzo #include <sys/smp.h> /* mp_maxid */ 59f9790aebSLuigi Rizzo #include <net/if.h> 60f9790aebSLuigi Rizzo #include <net/if_var.h> 614bf50f18SLuigi Rizzo #include <net/if_types.h> /* IFT_ETHER */ 624bf50f18SLuigi Rizzo #include <net/ethernet.h> /* ether_ifdetach */ 634bf50f18SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */ 64f9790aebSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 65f0ea3689SLuigi Rizzo #include <netinet/in.h> /* in6_cksum_pseudo() */ 66f0ea3689SLuigi Rizzo #include <machine/in_cksum.h> /* in_pseudo(), in_cksum_hdr() */ 67f9790aebSLuigi Rizzo 68f9790aebSLuigi Rizzo #include <net/netmap.h> 69f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h> 7037e3a6d3SLuigi Rizzo #include <net/netmap_virt.h> 71f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h> 72f9790aebSLuigi Rizzo 73f9790aebSLuigi Rizzo 74f9790aebSLuigi Rizzo /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */ 75f9790aebSLuigi Rizzo 7637e3a6d3SLuigi Rizzo void nm_os_selinfo_init(NM_SELINFO_T *si) { 7737e3a6d3SLuigi Rizzo struct mtx *m = &si->m; 7837e3a6d3SLuigi Rizzo mtx_init(m, "nm_kn_lock", NULL, MTX_DEF); 7937e3a6d3SLuigi Rizzo knlist_init_mtx(&si->si.si_note, m); 8037e3a6d3SLuigi Rizzo } 8137e3a6d3SLuigi Rizzo 8237e3a6d3SLuigi Rizzo void 8337e3a6d3SLuigi Rizzo nm_os_selinfo_uninit(NM_SELINFO_T *si) 8437e3a6d3SLuigi Rizzo { 8537e3a6d3SLuigi Rizzo /* XXX kqueue(9) needed; these will mirror knlist_init. */ 8637e3a6d3SLuigi Rizzo knlist_delete(&si->si.si_note, curthread, 0 /* not locked */ ); 8737e3a6d3SLuigi Rizzo knlist_destroy(&si->si.si_note); 8837e3a6d3SLuigi Rizzo /* now we don't need the mutex anymore */ 8937e3a6d3SLuigi Rizzo mtx_destroy(&si->m); 9037e3a6d3SLuigi Rizzo } 9137e3a6d3SLuigi Rizzo 9237e3a6d3SLuigi Rizzo void 9337e3a6d3SLuigi Rizzo nm_os_ifnet_lock(void) 9437e3a6d3SLuigi Rizzo { 9537e3a6d3SLuigi Rizzo IFNET_WLOCK(); 9637e3a6d3SLuigi Rizzo } 9737e3a6d3SLuigi Rizzo 9837e3a6d3SLuigi Rizzo void 9937e3a6d3SLuigi Rizzo nm_os_ifnet_unlock(void) 10037e3a6d3SLuigi Rizzo { 10137e3a6d3SLuigi Rizzo IFNET_WUNLOCK(); 10237e3a6d3SLuigi Rizzo } 10337e3a6d3SLuigi Rizzo 10437e3a6d3SLuigi Rizzo static int netmap_use_count = 0; 10537e3a6d3SLuigi Rizzo 10637e3a6d3SLuigi Rizzo void 10737e3a6d3SLuigi Rizzo nm_os_get_module(void) 10837e3a6d3SLuigi Rizzo { 10937e3a6d3SLuigi Rizzo netmap_use_count++; 11037e3a6d3SLuigi Rizzo } 11137e3a6d3SLuigi Rizzo 11237e3a6d3SLuigi Rizzo void 11337e3a6d3SLuigi Rizzo nm_os_put_module(void) 11437e3a6d3SLuigi Rizzo { 11537e3a6d3SLuigi Rizzo netmap_use_count--; 11637e3a6d3SLuigi Rizzo } 11737e3a6d3SLuigi Rizzo 11837e3a6d3SLuigi Rizzo static void 11937e3a6d3SLuigi Rizzo netmap_ifnet_arrival_handler(void *arg __unused, struct ifnet *ifp) 12037e3a6d3SLuigi Rizzo { 12137e3a6d3SLuigi Rizzo netmap_undo_zombie(ifp); 12237e3a6d3SLuigi Rizzo } 12337e3a6d3SLuigi Rizzo 12437e3a6d3SLuigi Rizzo static void 12537e3a6d3SLuigi Rizzo netmap_ifnet_departure_handler(void *arg __unused, struct ifnet *ifp) 12637e3a6d3SLuigi Rizzo { 12737e3a6d3SLuigi Rizzo netmap_make_zombie(ifp); 12837e3a6d3SLuigi Rizzo } 12937e3a6d3SLuigi Rizzo 13037e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_ah_tag; 13137e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_dh_tag; 13237e3a6d3SLuigi Rizzo 13337e3a6d3SLuigi Rizzo int 13437e3a6d3SLuigi Rizzo nm_os_ifnet_init(void) 13537e3a6d3SLuigi Rizzo { 13637e3a6d3SLuigi Rizzo nm_ifnet_ah_tag = 13737e3a6d3SLuigi Rizzo EVENTHANDLER_REGISTER(ifnet_arrival_event, 13837e3a6d3SLuigi Rizzo netmap_ifnet_arrival_handler, 13937e3a6d3SLuigi Rizzo NULL, EVENTHANDLER_PRI_ANY); 14037e3a6d3SLuigi Rizzo nm_ifnet_dh_tag = 14137e3a6d3SLuigi Rizzo EVENTHANDLER_REGISTER(ifnet_departure_event, 14237e3a6d3SLuigi Rizzo netmap_ifnet_departure_handler, 14337e3a6d3SLuigi Rizzo NULL, EVENTHANDLER_PRI_ANY); 14437e3a6d3SLuigi Rizzo return 0; 14537e3a6d3SLuigi Rizzo } 14637e3a6d3SLuigi Rizzo 14737e3a6d3SLuigi Rizzo void 14837e3a6d3SLuigi Rizzo nm_os_ifnet_fini(void) 14937e3a6d3SLuigi Rizzo { 15037e3a6d3SLuigi Rizzo EVENTHANDLER_DEREGISTER(ifnet_arrival_event, 15137e3a6d3SLuigi Rizzo nm_ifnet_ah_tag); 15237e3a6d3SLuigi Rizzo EVENTHANDLER_DEREGISTER(ifnet_departure_event, 15337e3a6d3SLuigi Rizzo nm_ifnet_dh_tag); 15437e3a6d3SLuigi Rizzo } 15537e3a6d3SLuigi Rizzo 156e4166283SLuigi Rizzo rawsum_t 15737e3a6d3SLuigi Rizzo nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum) 158f0ea3689SLuigi Rizzo { 159f0ea3689SLuigi Rizzo /* TODO XXX please use the FreeBSD implementation for this. */ 160f0ea3689SLuigi Rizzo uint16_t *words = (uint16_t *)data; 161f0ea3689SLuigi Rizzo int nw = len / 2; 162f0ea3689SLuigi Rizzo int i; 163f0ea3689SLuigi Rizzo 164f0ea3689SLuigi Rizzo for (i = 0; i < nw; i++) 165f0ea3689SLuigi Rizzo cur_sum += be16toh(words[i]); 166f0ea3689SLuigi Rizzo 167f0ea3689SLuigi Rizzo if (len & 1) 168f0ea3689SLuigi Rizzo cur_sum += (data[len-1] << 8); 169f0ea3689SLuigi Rizzo 170f0ea3689SLuigi Rizzo return cur_sum; 171f0ea3689SLuigi Rizzo } 172f0ea3689SLuigi Rizzo 173f0ea3689SLuigi Rizzo /* Fold a raw checksum: 'cur_sum' is in host byte order, while the 174f0ea3689SLuigi Rizzo * return value is in network byte order. 175f0ea3689SLuigi Rizzo */ 176e4166283SLuigi Rizzo uint16_t 17737e3a6d3SLuigi Rizzo nm_os_csum_fold(rawsum_t cur_sum) 178f0ea3689SLuigi Rizzo { 179f0ea3689SLuigi Rizzo /* TODO XXX please use the FreeBSD implementation for this. */ 180f0ea3689SLuigi Rizzo while (cur_sum >> 16) 181f0ea3689SLuigi Rizzo cur_sum = (cur_sum & 0xFFFF) + (cur_sum >> 16); 182f0ea3689SLuigi Rizzo 183f0ea3689SLuigi Rizzo return htobe16((~cur_sum) & 0xFFFF); 184f0ea3689SLuigi Rizzo } 185f0ea3689SLuigi Rizzo 18637e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph) 187f0ea3689SLuigi Rizzo { 188f0ea3689SLuigi Rizzo #if 0 189f0ea3689SLuigi Rizzo return in_cksum_hdr((void *)iph); 190f0ea3689SLuigi Rizzo #else 19137e3a6d3SLuigi Rizzo return nm_os_csum_fold(nm_os_csum_raw((uint8_t*)iph, sizeof(struct nm_iphdr), 0)); 192f0ea3689SLuigi Rizzo #endif 193f0ea3689SLuigi Rizzo } 194f0ea3689SLuigi Rizzo 195e4166283SLuigi Rizzo void 19637e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data, 197f0ea3689SLuigi Rizzo size_t datalen, uint16_t *check) 198f0ea3689SLuigi Rizzo { 1995a067ae1SLuigi Rizzo #ifdef INET 200f0ea3689SLuigi Rizzo uint16_t pseudolen = datalen + iph->protocol; 201f0ea3689SLuigi Rizzo 202f0ea3689SLuigi Rizzo /* Compute and insert the pseudo-header cheksum. */ 203f0ea3689SLuigi Rizzo *check = in_pseudo(iph->saddr, iph->daddr, 204f0ea3689SLuigi Rizzo htobe16(pseudolen)); 205f0ea3689SLuigi Rizzo /* Compute the checksum on TCP/UDP header + payload 206f0ea3689SLuigi Rizzo * (includes the pseudo-header). 207f0ea3689SLuigi Rizzo */ 20837e3a6d3SLuigi Rizzo *check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0)); 2095a067ae1SLuigi Rizzo #else 2105a067ae1SLuigi Rizzo static int notsupported = 0; 2115a067ae1SLuigi Rizzo if (!notsupported) { 2125a067ae1SLuigi Rizzo notsupported = 1; 2135a067ae1SLuigi Rizzo D("inet4 segmentation not supported"); 2145a067ae1SLuigi Rizzo } 2155a067ae1SLuigi Rizzo #endif 216f0ea3689SLuigi Rizzo } 217f0ea3689SLuigi Rizzo 218e4166283SLuigi Rizzo void 21937e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data, 220f0ea3689SLuigi Rizzo size_t datalen, uint16_t *check) 221f0ea3689SLuigi Rizzo { 222f0ea3689SLuigi Rizzo #ifdef INET6 223f0ea3689SLuigi Rizzo *check = in6_cksum_pseudo((void*)ip6h, datalen, ip6h->nexthdr, 0); 22437e3a6d3SLuigi Rizzo *check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0)); 225f0ea3689SLuigi Rizzo #else 226f0ea3689SLuigi Rizzo static int notsupported = 0; 227f0ea3689SLuigi Rizzo if (!notsupported) { 228f0ea3689SLuigi Rizzo notsupported = 1; 229f0ea3689SLuigi Rizzo D("inet6 segmentation not supported"); 230f0ea3689SLuigi Rizzo } 231f0ea3689SLuigi Rizzo #endif 232f0ea3689SLuigi Rizzo } 233f0ea3689SLuigi Rizzo 23437e3a6d3SLuigi Rizzo /* on FreeBSD we send up one packet at a time */ 23537e3a6d3SLuigi Rizzo void * 23637e3a6d3SLuigi Rizzo nm_os_send_up(struct ifnet *ifp, struct mbuf *m, struct mbuf *prev) 23737e3a6d3SLuigi Rizzo { 23837e3a6d3SLuigi Rizzo 23937e3a6d3SLuigi Rizzo NA(ifp)->if_input(ifp, m); 24037e3a6d3SLuigi Rizzo return NULL; 24137e3a6d3SLuigi Rizzo } 24237e3a6d3SLuigi Rizzo 24337e3a6d3SLuigi Rizzo int 24437e3a6d3SLuigi Rizzo nm_os_mbuf_has_offld(struct mbuf *m) 24537e3a6d3SLuigi Rizzo { 24637e3a6d3SLuigi Rizzo return m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_SCTP | 24737e3a6d3SLuigi Rizzo CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | 24837e3a6d3SLuigi Rizzo CSUM_SCTP_IPV6 | CSUM_TSO); 24937e3a6d3SLuigi Rizzo } 25037e3a6d3SLuigi Rizzo 25137e3a6d3SLuigi Rizzo static void 25237e3a6d3SLuigi Rizzo freebsd_generic_rx_handler(struct ifnet *ifp, struct mbuf *m) 25337e3a6d3SLuigi Rizzo { 25437e3a6d3SLuigi Rizzo struct netmap_generic_adapter *gna = 25537e3a6d3SLuigi Rizzo (struct netmap_generic_adapter *)NA(ifp); 25637e3a6d3SLuigi Rizzo int stolen = generic_rx_handler(ifp, m); 25737e3a6d3SLuigi Rizzo 25837e3a6d3SLuigi Rizzo if (!stolen) { 25937e3a6d3SLuigi Rizzo gna->save_if_input(ifp, m); 26037e3a6d3SLuigi Rizzo } 26137e3a6d3SLuigi Rizzo } 262f0ea3689SLuigi Rizzo 263f9790aebSLuigi Rizzo /* 264f9790aebSLuigi Rizzo * Intercept the rx routine in the standard device driver. 265f9790aebSLuigi Rizzo * Second argument is non-zero to intercept, 0 to restore 266f9790aebSLuigi Rizzo */ 267f9790aebSLuigi Rizzo int 26837e3a6d3SLuigi Rizzo nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept) 269f9790aebSLuigi Rizzo { 270847bf383SLuigi Rizzo struct netmap_adapter *na = &gna->up.up; 271f9790aebSLuigi Rizzo struct ifnet *ifp = na->ifp; 272f9790aebSLuigi Rizzo 273f9790aebSLuigi Rizzo if (intercept) { 274f9790aebSLuigi Rizzo if (gna->save_if_input) { 275f9790aebSLuigi Rizzo D("cannot intercept again"); 276f9790aebSLuigi Rizzo return EINVAL; /* already set */ 277f9790aebSLuigi Rizzo } 278f9790aebSLuigi Rizzo gna->save_if_input = ifp->if_input; 27937e3a6d3SLuigi Rizzo ifp->if_input = freebsd_generic_rx_handler; 280f9790aebSLuigi Rizzo } else { 281f9790aebSLuigi Rizzo if (!gna->save_if_input){ 282f9790aebSLuigi Rizzo D("cannot restore"); 283f9790aebSLuigi Rizzo return EINVAL; /* not saved */ 284f9790aebSLuigi Rizzo } 285f9790aebSLuigi Rizzo ifp->if_input = gna->save_if_input; 286f9790aebSLuigi Rizzo gna->save_if_input = NULL; 287f9790aebSLuigi Rizzo } 288f9790aebSLuigi Rizzo 289f9790aebSLuigi Rizzo return 0; 290f9790aebSLuigi Rizzo } 291f9790aebSLuigi Rizzo 29217885a7bSLuigi Rizzo 293f9790aebSLuigi Rizzo /* 294f9790aebSLuigi Rizzo * Intercept the packet steering routine in the tx path, 295f9790aebSLuigi Rizzo * so that we can decide which queue is used for an mbuf. 296f9790aebSLuigi Rizzo * Second argument is non-zero to intercept, 0 to restore. 297f0ea3689SLuigi Rizzo * On freebsd we just intercept if_transmit. 298f9790aebSLuigi Rizzo */ 29937e3a6d3SLuigi Rizzo int 30037e3a6d3SLuigi Rizzo nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept) 301f9790aebSLuigi Rizzo { 30217885a7bSLuigi Rizzo struct netmap_adapter *na = &gna->up.up; 303847bf383SLuigi Rizzo struct ifnet *ifp = netmap_generic_getifp(gna); 30417885a7bSLuigi Rizzo 30537e3a6d3SLuigi Rizzo if (intercept) { 30617885a7bSLuigi Rizzo na->if_transmit = ifp->if_transmit; 30717885a7bSLuigi Rizzo ifp->if_transmit = netmap_transmit; 308f9790aebSLuigi Rizzo } else { 30917885a7bSLuigi Rizzo ifp->if_transmit = na->if_transmit; 310f9790aebSLuigi Rizzo } 31137e3a6d3SLuigi Rizzo 31237e3a6d3SLuigi Rizzo return 0; 313f9790aebSLuigi Rizzo } 314f9790aebSLuigi Rizzo 31517885a7bSLuigi Rizzo 316f0ea3689SLuigi Rizzo /* 317f0ea3689SLuigi Rizzo * Transmit routine used by generic_netmap_txsync(). Returns 0 on success 318f9790aebSLuigi Rizzo * and non-zero on error (which may be packet drops or other errors). 319f9790aebSLuigi Rizzo * addr and len identify the netmap buffer, m is the (preallocated) 320f9790aebSLuigi Rizzo * mbuf to use for transmissions. 321f9790aebSLuigi Rizzo * 322f9790aebSLuigi Rizzo * We should add a reference to the mbuf so the m_freem() at the end 323f9790aebSLuigi Rizzo * of the transmission does not consume resources. 324f9790aebSLuigi Rizzo * 325f9790aebSLuigi Rizzo * On FreeBSD, and on multiqueue cards, we can force the queue using 326c2529042SHans Petter Selasky * if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 327f9790aebSLuigi Rizzo * i = m->m_pkthdr.flowid % adapter->num_queues; 328f9790aebSLuigi Rizzo * else 329f9790aebSLuigi Rizzo * i = curcpu % adapter->num_queues; 330f9790aebSLuigi Rizzo * 331f9790aebSLuigi Rizzo */ 332f9790aebSLuigi Rizzo int 33337e3a6d3SLuigi Rizzo nm_os_generic_xmit_frame(struct nm_os_gen_arg *a) 334f9790aebSLuigi Rizzo { 335f9790aebSLuigi Rizzo int ret; 33637e3a6d3SLuigi Rizzo u_int len = a->len; 33737e3a6d3SLuigi Rizzo struct ifnet *ifp = a->ifp; 33837e3a6d3SLuigi Rizzo struct mbuf *m = a->m; 339f9790aebSLuigi Rizzo 34037e3a6d3SLuigi Rizzo #if __FreeBSD_version < 1100000 341e4166283SLuigi Rizzo /* 34237e3a6d3SLuigi Rizzo * Old FreeBSD versions. The mbuf has a cluster attached, 34337e3a6d3SLuigi Rizzo * we need to copy from the cluster to the netmap buffer. 344e4166283SLuigi Rizzo */ 34537e3a6d3SLuigi Rizzo if (MBUF_REFCNT(m) != 1) { 34637e3a6d3SLuigi Rizzo D("invalid refcnt %d for %p", MBUF_REFCNT(m), m); 347e4166283SLuigi Rizzo panic("in generic_xmit_frame"); 348e4166283SLuigi Rizzo } 349e4166283SLuigi Rizzo if (m->m_ext.ext_size < len) { 350e4166283SLuigi Rizzo RD(5, "size %d < len %d", m->m_ext.ext_size, len); 351e4166283SLuigi Rizzo len = m->m_ext.ext_size; 352e4166283SLuigi Rizzo } 35337e3a6d3SLuigi Rizzo bcopy(a->addr, m->m_data, len); 35437e3a6d3SLuigi Rizzo #else /* __FreeBSD_version >= 1100000 */ 35537e3a6d3SLuigi Rizzo /* New FreeBSD versions. Link the external storage to 35637e3a6d3SLuigi Rizzo * the netmap buffer, so that no copy is necessary. */ 35737e3a6d3SLuigi Rizzo m->m_ext.ext_buf = m->m_data = a->addr; 35837e3a6d3SLuigi Rizzo m->m_ext.ext_size = len; 35937e3a6d3SLuigi Rizzo #endif /* __FreeBSD_version >= 1100000 */ 36037e3a6d3SLuigi Rizzo 361e4166283SLuigi Rizzo m->m_len = m->m_pkthdr.len = len; 36237e3a6d3SLuigi Rizzo 36337e3a6d3SLuigi Rizzo /* mbuf refcnt is not contended, no need to use atomic 36437e3a6d3SLuigi Rizzo * (a memory barrier is enough). */ 36537e3a6d3SLuigi Rizzo SET_MBUF_REFCNT(m, 2); 366c2529042SHans Petter Selasky M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE); 36737e3a6d3SLuigi Rizzo m->m_pkthdr.flowid = a->ring_nr; 368f9790aebSLuigi Rizzo m->m_pkthdr.rcvif = ifp; /* used for tx notification */ 36917885a7bSLuigi Rizzo ret = NA(ifp)->if_transmit(ifp, m); 37037e3a6d3SLuigi Rizzo return ret ? -1 : 0; 371f9790aebSLuigi Rizzo } 372f9790aebSLuigi Rizzo 37317885a7bSLuigi Rizzo 3740dc809c0SLuigi Rizzo #if __FreeBSD_version >= 1100005 3750dc809c0SLuigi Rizzo struct netmap_adapter * 3760dc809c0SLuigi Rizzo netmap_getna(if_t ifp) 3770dc809c0SLuigi Rizzo { 3780dc809c0SLuigi Rizzo return (NA((struct ifnet *)ifp)); 3790dc809c0SLuigi Rizzo } 3800dc809c0SLuigi Rizzo #endif /* __FreeBSD_version >= 1100005 */ 3810dc809c0SLuigi Rizzo 382f9790aebSLuigi Rizzo /* 383f9790aebSLuigi Rizzo * The following two functions are empty until we have a generic 384f9790aebSLuigi Rizzo * way to extract the info from the ifp 385f9790aebSLuigi Rizzo */ 386f9790aebSLuigi Rizzo int 38737e3a6d3SLuigi Rizzo nm_os_generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx) 388f9790aebSLuigi Rizzo { 389e4166283SLuigi Rizzo D("called, in tx %d rx %d", *tx, *rx); 390f9790aebSLuigi Rizzo return 0; 391f9790aebSLuigi Rizzo } 392f9790aebSLuigi Rizzo 39317885a7bSLuigi Rizzo 394f9790aebSLuigi Rizzo void 39537e3a6d3SLuigi Rizzo nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq) 396f9790aebSLuigi Rizzo { 397e4166283SLuigi Rizzo D("called, in txq %d rxq %d", *txq, *rxq); 398f0ea3689SLuigi Rizzo *txq = netmap_generic_rings; 399f0ea3689SLuigi Rizzo *rxq = netmap_generic_rings; 400f9790aebSLuigi Rizzo } 401f9790aebSLuigi Rizzo 40237e3a6d3SLuigi Rizzo void 40337e3a6d3SLuigi Rizzo nm_os_generic_set_features(struct netmap_generic_adapter *gna) 40437e3a6d3SLuigi Rizzo { 40537e3a6d3SLuigi Rizzo 40637e3a6d3SLuigi Rizzo gna->rxsg = 1; /* Supported through m_copydata. */ 40737e3a6d3SLuigi Rizzo gna->txqdisc = 0; /* Not supported. */ 40837e3a6d3SLuigi Rizzo } 40917885a7bSLuigi Rizzo 410e4166283SLuigi Rizzo void 41137e3a6d3SLuigi Rizzo nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, struct netmap_adapter *na) 412f9790aebSLuigi Rizzo { 413f9790aebSLuigi Rizzo ND("called"); 414f0ea3689SLuigi Rizzo mit->mit_pending = 0; 4154bf50f18SLuigi Rizzo mit->mit_ring_idx = idx; 416f0ea3689SLuigi Rizzo mit->mit_na = na; 417f9790aebSLuigi Rizzo } 418f9790aebSLuigi Rizzo 419f9790aebSLuigi Rizzo 420e4166283SLuigi Rizzo void 42137e3a6d3SLuigi Rizzo nm_os_mitigation_start(struct nm_generic_mit *mit) 422f9790aebSLuigi Rizzo { 423f9790aebSLuigi Rizzo ND("called"); 424f9790aebSLuigi Rizzo } 425f9790aebSLuigi Rizzo 42617885a7bSLuigi Rizzo 427e4166283SLuigi Rizzo void 42837e3a6d3SLuigi Rizzo nm_os_mitigation_restart(struct nm_generic_mit *mit) 429f9790aebSLuigi Rizzo { 430f9790aebSLuigi Rizzo ND("called"); 431f9790aebSLuigi Rizzo } 432f9790aebSLuigi Rizzo 43317885a7bSLuigi Rizzo 434e4166283SLuigi Rizzo int 43537e3a6d3SLuigi Rizzo nm_os_mitigation_active(struct nm_generic_mit *mit) 436f9790aebSLuigi Rizzo { 437f9790aebSLuigi Rizzo ND("called"); 438f9790aebSLuigi Rizzo return 0; 439f9790aebSLuigi Rizzo } 440f9790aebSLuigi Rizzo 44117885a7bSLuigi Rizzo 442e4166283SLuigi Rizzo void 44337e3a6d3SLuigi Rizzo nm_os_mitigation_cleanup(struct nm_generic_mit *mit) 444f9790aebSLuigi Rizzo { 445f9790aebSLuigi Rizzo ND("called"); 446f9790aebSLuigi Rizzo } 447f9790aebSLuigi Rizzo 4484bf50f18SLuigi Rizzo static int 4494bf50f18SLuigi Rizzo nm_vi_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr) 4504bf50f18SLuigi Rizzo { 4514bf50f18SLuigi Rizzo return EINVAL; 4524bf50f18SLuigi Rizzo } 4534bf50f18SLuigi Rizzo 4544bf50f18SLuigi Rizzo static void 4554bf50f18SLuigi Rizzo nm_vi_start(struct ifnet *ifp) 4564bf50f18SLuigi Rizzo { 4574bf50f18SLuigi Rizzo panic("nm_vi_start() must not be called"); 4584bf50f18SLuigi Rizzo } 4594bf50f18SLuigi Rizzo 4604bf50f18SLuigi Rizzo /* 4614bf50f18SLuigi Rizzo * Index manager of persistent virtual interfaces. 4624bf50f18SLuigi Rizzo * It is used to decide the lowest byte of the MAC address. 4634bf50f18SLuigi Rizzo * We use the same algorithm with management of bridge port index. 4644bf50f18SLuigi Rizzo */ 4654bf50f18SLuigi Rizzo #define NM_VI_MAX 255 4664bf50f18SLuigi Rizzo static struct { 4674bf50f18SLuigi Rizzo uint8_t index[NM_VI_MAX]; /* XXX just for a reasonable number */ 4684bf50f18SLuigi Rizzo uint8_t active; 4694bf50f18SLuigi Rizzo struct mtx lock; 4704bf50f18SLuigi Rizzo } nm_vi_indices; 4714bf50f18SLuigi Rizzo 4724bf50f18SLuigi Rizzo void 47337e3a6d3SLuigi Rizzo nm_os_vi_init_index(void) 4744bf50f18SLuigi Rizzo { 4754bf50f18SLuigi Rizzo int i; 4764bf50f18SLuigi Rizzo for (i = 0; i < NM_VI_MAX; i++) 4774bf50f18SLuigi Rizzo nm_vi_indices.index[i] = i; 4784bf50f18SLuigi Rizzo nm_vi_indices.active = 0; 4794bf50f18SLuigi Rizzo mtx_init(&nm_vi_indices.lock, "nm_vi_indices_lock", NULL, MTX_DEF); 4804bf50f18SLuigi Rizzo } 4814bf50f18SLuigi Rizzo 4824bf50f18SLuigi Rizzo /* return -1 if no index available */ 4834bf50f18SLuigi Rizzo static int 4844bf50f18SLuigi Rizzo nm_vi_get_index(void) 4854bf50f18SLuigi Rizzo { 4864bf50f18SLuigi Rizzo int ret; 4874bf50f18SLuigi Rizzo 4884bf50f18SLuigi Rizzo mtx_lock(&nm_vi_indices.lock); 4894bf50f18SLuigi Rizzo ret = nm_vi_indices.active == NM_VI_MAX ? -1 : 4904bf50f18SLuigi Rizzo nm_vi_indices.index[nm_vi_indices.active++]; 4914bf50f18SLuigi Rizzo mtx_unlock(&nm_vi_indices.lock); 4924bf50f18SLuigi Rizzo return ret; 4934bf50f18SLuigi Rizzo } 4944bf50f18SLuigi Rizzo 4954bf50f18SLuigi Rizzo static void 4964bf50f18SLuigi Rizzo nm_vi_free_index(uint8_t val) 4974bf50f18SLuigi Rizzo { 4984bf50f18SLuigi Rizzo int i, lim; 4994bf50f18SLuigi Rizzo 5004bf50f18SLuigi Rizzo mtx_lock(&nm_vi_indices.lock); 5014bf50f18SLuigi Rizzo lim = nm_vi_indices.active; 5024bf50f18SLuigi Rizzo for (i = 0; i < lim; i++) { 5034bf50f18SLuigi Rizzo if (nm_vi_indices.index[i] == val) { 5044bf50f18SLuigi Rizzo /* swap index[lim-1] and j */ 5054bf50f18SLuigi Rizzo int tmp = nm_vi_indices.index[lim-1]; 5064bf50f18SLuigi Rizzo nm_vi_indices.index[lim-1] = val; 5074bf50f18SLuigi Rizzo nm_vi_indices.index[i] = tmp; 5084bf50f18SLuigi Rizzo nm_vi_indices.active--; 5094bf50f18SLuigi Rizzo break; 5104bf50f18SLuigi Rizzo } 5114bf50f18SLuigi Rizzo } 5124bf50f18SLuigi Rizzo if (lim == nm_vi_indices.active) 5134bf50f18SLuigi Rizzo D("funny, index %u didn't found", val); 5144bf50f18SLuigi Rizzo mtx_unlock(&nm_vi_indices.lock); 5154bf50f18SLuigi Rizzo } 5164bf50f18SLuigi Rizzo #undef NM_VI_MAX 5174bf50f18SLuigi Rizzo 5184bf50f18SLuigi Rizzo /* 5194bf50f18SLuigi Rizzo * Implementation of a netmap-capable virtual interface that 5204bf50f18SLuigi Rizzo * registered to the system. 5214bf50f18SLuigi Rizzo * It is based on if_tap.c and ip_fw_log.c in FreeBSD 9. 5224bf50f18SLuigi Rizzo * 5234bf50f18SLuigi Rizzo * Note: Linux sets refcount to 0 on allocation of net_device, 5244bf50f18SLuigi Rizzo * then increments it on registration to the system. 5254bf50f18SLuigi Rizzo * FreeBSD sets refcount to 1 on if_alloc(), and does not 5264bf50f18SLuigi Rizzo * increment this refcount on if_attach(). 5274bf50f18SLuigi Rizzo */ 5284bf50f18SLuigi Rizzo int 52937e3a6d3SLuigi Rizzo nm_os_vi_persist(const char *name, struct ifnet **ret) 5304bf50f18SLuigi Rizzo { 5314bf50f18SLuigi Rizzo struct ifnet *ifp; 5324bf50f18SLuigi Rizzo u_short macaddr_hi; 5334bf50f18SLuigi Rizzo uint32_t macaddr_mid; 5344bf50f18SLuigi Rizzo u_char eaddr[6]; 5354bf50f18SLuigi Rizzo int unit = nm_vi_get_index(); /* just to decide MAC address */ 5364bf50f18SLuigi Rizzo 5374bf50f18SLuigi Rizzo if (unit < 0) 5384bf50f18SLuigi Rizzo return EBUSY; 5394bf50f18SLuigi Rizzo /* 5404bf50f18SLuigi Rizzo * We use the same MAC address generation method with tap 5414bf50f18SLuigi Rizzo * except for the highest octet is 00:be instead of 00:bd 5424bf50f18SLuigi Rizzo */ 5434bf50f18SLuigi Rizzo macaddr_hi = htons(0x00be); /* XXX tap + 1 */ 5444bf50f18SLuigi Rizzo macaddr_mid = (uint32_t) ticks; 5454bf50f18SLuigi Rizzo bcopy(&macaddr_hi, eaddr, sizeof(short)); 5464bf50f18SLuigi Rizzo bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t)); 5474bf50f18SLuigi Rizzo eaddr[5] = (uint8_t)unit; 5484bf50f18SLuigi Rizzo 5494bf50f18SLuigi Rizzo ifp = if_alloc(IFT_ETHER); 5504bf50f18SLuigi Rizzo if (ifp == NULL) { 5514bf50f18SLuigi Rizzo D("if_alloc failed"); 5524bf50f18SLuigi Rizzo return ENOMEM; 5534bf50f18SLuigi Rizzo } 5544bf50f18SLuigi Rizzo if_initname(ifp, name, IF_DUNIT_NONE); 5554bf50f18SLuigi Rizzo ifp->if_mtu = 65536; 5564bf50f18SLuigi Rizzo ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST; 5574bf50f18SLuigi Rizzo ifp->if_init = (void *)nm_vi_dummy; 5584bf50f18SLuigi Rizzo ifp->if_ioctl = nm_vi_dummy; 5594bf50f18SLuigi Rizzo ifp->if_start = nm_vi_start; 5604bf50f18SLuigi Rizzo ifp->if_mtu = ETHERMTU; 5614bf50f18SLuigi Rizzo IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 5624bf50f18SLuigi Rizzo ifp->if_capabilities |= IFCAP_LINKSTATE; 5634bf50f18SLuigi Rizzo ifp->if_capenable |= IFCAP_LINKSTATE; 5644bf50f18SLuigi Rizzo 5654bf50f18SLuigi Rizzo ether_ifattach(ifp, eaddr); 5664bf50f18SLuigi Rizzo *ret = ifp; 5674bf50f18SLuigi Rizzo return 0; 5684bf50f18SLuigi Rizzo } 56937e3a6d3SLuigi Rizzo 5704bf50f18SLuigi Rizzo /* unregister from the system and drop the final refcount */ 5714bf50f18SLuigi Rizzo void 57237e3a6d3SLuigi Rizzo nm_os_vi_detach(struct ifnet *ifp) 5734bf50f18SLuigi Rizzo { 5744bf50f18SLuigi Rizzo nm_vi_free_index(((char *)IF_LLADDR(ifp))[5]); 5754bf50f18SLuigi Rizzo ether_ifdetach(ifp); 5764bf50f18SLuigi Rizzo if_free(ifp); 5774bf50f18SLuigi Rizzo } 57817885a7bSLuigi Rizzo 57937e3a6d3SLuigi Rizzo /* ======================== PTNETMAP SUPPORT ========================== */ 58037e3a6d3SLuigi Rizzo 58137e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST 58237e3a6d3SLuigi Rizzo #include <sys/bus.h> 58337e3a6d3SLuigi Rizzo #include <sys/rman.h> 58437e3a6d3SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 58537e3a6d3SLuigi Rizzo #include <machine/resource.h> 58637e3a6d3SLuigi Rizzo #include <dev/pci/pcivar.h> 58737e3a6d3SLuigi Rizzo #include <dev/pci/pcireg.h> 58837e3a6d3SLuigi Rizzo /* 58937e3a6d3SLuigi Rizzo * ptnetmap memory device (memdev) for freebsd guest, 59037e3a6d3SLuigi Rizzo * ssed to expose host netmap memory to the guest through a PCI BAR. 59137e3a6d3SLuigi Rizzo */ 59237e3a6d3SLuigi Rizzo 59337e3a6d3SLuigi Rizzo /* 59437e3a6d3SLuigi Rizzo * ptnetmap memdev private data structure 59537e3a6d3SLuigi Rizzo */ 59637e3a6d3SLuigi Rizzo struct ptnetmap_memdev { 59737e3a6d3SLuigi Rizzo device_t dev; 59837e3a6d3SLuigi Rizzo struct resource *pci_io; 59937e3a6d3SLuigi Rizzo struct resource *pci_mem; 60037e3a6d3SLuigi Rizzo struct netmap_mem_d *nm_mem; 60137e3a6d3SLuigi Rizzo }; 60237e3a6d3SLuigi Rizzo 60337e3a6d3SLuigi Rizzo static int ptn_memdev_probe(device_t); 60437e3a6d3SLuigi Rizzo static int ptn_memdev_attach(device_t); 60537e3a6d3SLuigi Rizzo static int ptn_memdev_detach(device_t); 60637e3a6d3SLuigi Rizzo static int ptn_memdev_shutdown(device_t); 60737e3a6d3SLuigi Rizzo 60837e3a6d3SLuigi Rizzo static device_method_t ptn_memdev_methods[] = { 60937e3a6d3SLuigi Rizzo DEVMETHOD(device_probe, ptn_memdev_probe), 61037e3a6d3SLuigi Rizzo DEVMETHOD(device_attach, ptn_memdev_attach), 61137e3a6d3SLuigi Rizzo DEVMETHOD(device_detach, ptn_memdev_detach), 61237e3a6d3SLuigi Rizzo DEVMETHOD(device_shutdown, ptn_memdev_shutdown), 61337e3a6d3SLuigi Rizzo DEVMETHOD_END 61437e3a6d3SLuigi Rizzo }; 61537e3a6d3SLuigi Rizzo 61637e3a6d3SLuigi Rizzo static driver_t ptn_memdev_driver = { 61737e3a6d3SLuigi Rizzo PTNETMAP_MEMDEV_NAME, 61837e3a6d3SLuigi Rizzo ptn_memdev_methods, 61937e3a6d3SLuigi Rizzo sizeof(struct ptnetmap_memdev), 62037e3a6d3SLuigi Rizzo }; 62137e3a6d3SLuigi Rizzo 62237e3a6d3SLuigi Rizzo /* We use (SI_ORDER_MIDDLE+1) here, see DEV_MODULE_ORDERED() invocation 62337e3a6d3SLuigi Rizzo * below. */ 62437e3a6d3SLuigi Rizzo static devclass_t ptnetmap_devclass; 62537e3a6d3SLuigi Rizzo DRIVER_MODULE_ORDERED(ptn_memdev, pci, ptn_memdev_driver, ptnetmap_devclass, 62637e3a6d3SLuigi Rizzo NULL, NULL, SI_ORDER_MIDDLE + 1); 62737e3a6d3SLuigi Rizzo 62837e3a6d3SLuigi Rizzo /* 62937e3a6d3SLuigi Rizzo * I/O port read/write wrappers. 63037e3a6d3SLuigi Rizzo * Some are not used, so we keep them commented out until needed 63137e3a6d3SLuigi Rizzo */ 63237e3a6d3SLuigi Rizzo #define ptn_ioread16(ptn_dev, reg) bus_read_2((ptn_dev)->pci_io, (reg)) 63337e3a6d3SLuigi Rizzo #define ptn_ioread32(ptn_dev, reg) bus_read_4((ptn_dev)->pci_io, (reg)) 63437e3a6d3SLuigi Rizzo #if 0 63537e3a6d3SLuigi Rizzo #define ptn_ioread8(ptn_dev, reg) bus_read_1((ptn_dev)->pci_io, (reg)) 63637e3a6d3SLuigi Rizzo #define ptn_iowrite8(ptn_dev, reg, val) bus_write_1((ptn_dev)->pci_io, (reg), (val)) 63737e3a6d3SLuigi Rizzo #define ptn_iowrite16(ptn_dev, reg, val) bus_write_2((ptn_dev)->pci_io, (reg), (val)) 63837e3a6d3SLuigi Rizzo #define ptn_iowrite32(ptn_dev, reg, val) bus_write_4((ptn_dev)->pci_io, (reg), (val)) 63937e3a6d3SLuigi Rizzo #endif /* unused */ 64037e3a6d3SLuigi Rizzo 64137e3a6d3SLuigi Rizzo /* 64237e3a6d3SLuigi Rizzo * Map host netmap memory through PCI-BAR in the guest OS, 64337e3a6d3SLuigi Rizzo * returning physical (nm_paddr) and virtual (nm_addr) addresses 64437e3a6d3SLuigi Rizzo * of the netmap memory mapped in the guest. 64537e3a6d3SLuigi Rizzo */ 64637e3a6d3SLuigi Rizzo int 647a2a74091SLuigi Rizzo nm_os_pt_memdev_iomap(struct ptnetmap_memdev *ptn_dev, vm_paddr_t *nm_paddr, 648a2a74091SLuigi Rizzo void **nm_addr) 64937e3a6d3SLuigi Rizzo { 65037e3a6d3SLuigi Rizzo uint32_t mem_size; 65137e3a6d3SLuigi Rizzo int rid; 65237e3a6d3SLuigi Rizzo 65337e3a6d3SLuigi Rizzo D("ptn_memdev_driver iomap"); 65437e3a6d3SLuigi Rizzo 65537e3a6d3SLuigi Rizzo rid = PCIR_BAR(PTNETMAP_MEM_PCI_BAR); 65637e3a6d3SLuigi Rizzo mem_size = ptn_ioread32(ptn_dev, PTNETMAP_IO_PCI_MEMSIZE); 65737e3a6d3SLuigi Rizzo 65837e3a6d3SLuigi Rizzo /* map memory allocator */ 65937e3a6d3SLuigi Rizzo ptn_dev->pci_mem = bus_alloc_resource(ptn_dev->dev, SYS_RES_MEMORY, 66037e3a6d3SLuigi Rizzo &rid, 0, ~0, mem_size, RF_ACTIVE); 66137e3a6d3SLuigi Rizzo if (ptn_dev->pci_mem == NULL) { 66237e3a6d3SLuigi Rizzo *nm_paddr = 0; 66337e3a6d3SLuigi Rizzo *nm_addr = 0; 66437e3a6d3SLuigi Rizzo return ENOMEM; 66537e3a6d3SLuigi Rizzo } 66637e3a6d3SLuigi Rizzo 66737e3a6d3SLuigi Rizzo *nm_paddr = rman_get_start(ptn_dev->pci_mem); 66837e3a6d3SLuigi Rizzo *nm_addr = rman_get_virtual(ptn_dev->pci_mem); 66937e3a6d3SLuigi Rizzo 67037e3a6d3SLuigi Rizzo D("=== BAR %d start %lx len %lx mem_size %x ===", 67137e3a6d3SLuigi Rizzo PTNETMAP_MEM_PCI_BAR, 672a2a74091SLuigi Rizzo (unsigned long)(*nm_paddr), 673a2a74091SLuigi Rizzo (unsigned long)rman_get_size(ptn_dev->pci_mem), 67437e3a6d3SLuigi Rizzo mem_size); 67537e3a6d3SLuigi Rizzo return (0); 67637e3a6d3SLuigi Rizzo } 67737e3a6d3SLuigi Rizzo 67837e3a6d3SLuigi Rizzo /* Unmap host netmap memory. */ 67937e3a6d3SLuigi Rizzo void 68037e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *ptn_dev) 68137e3a6d3SLuigi Rizzo { 68237e3a6d3SLuigi Rizzo D("ptn_memdev_driver iounmap"); 68337e3a6d3SLuigi Rizzo 68437e3a6d3SLuigi Rizzo if (ptn_dev->pci_mem) { 68537e3a6d3SLuigi Rizzo bus_release_resource(ptn_dev->dev, SYS_RES_MEMORY, 68637e3a6d3SLuigi Rizzo PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem); 68737e3a6d3SLuigi Rizzo ptn_dev->pci_mem = NULL; 68837e3a6d3SLuigi Rizzo } 68937e3a6d3SLuigi Rizzo } 69037e3a6d3SLuigi Rizzo 69137e3a6d3SLuigi Rizzo /* Device identification routine, return BUS_PROBE_DEFAULT on success, 69237e3a6d3SLuigi Rizzo * positive on failure */ 69337e3a6d3SLuigi Rizzo static int 69437e3a6d3SLuigi Rizzo ptn_memdev_probe(device_t dev) 69537e3a6d3SLuigi Rizzo { 69637e3a6d3SLuigi Rizzo char desc[256]; 69737e3a6d3SLuigi Rizzo 69837e3a6d3SLuigi Rizzo if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID) 69937e3a6d3SLuigi Rizzo return (ENXIO); 70037e3a6d3SLuigi Rizzo if (pci_get_device(dev) != PTNETMAP_PCI_DEVICE_ID) 70137e3a6d3SLuigi Rizzo return (ENXIO); 70237e3a6d3SLuigi Rizzo 70337e3a6d3SLuigi Rizzo snprintf(desc, sizeof(desc), "%s PCI adapter", 70437e3a6d3SLuigi Rizzo PTNETMAP_MEMDEV_NAME); 70537e3a6d3SLuigi Rizzo device_set_desc_copy(dev, desc); 70637e3a6d3SLuigi Rizzo 70737e3a6d3SLuigi Rizzo return (BUS_PROBE_DEFAULT); 70837e3a6d3SLuigi Rizzo } 70937e3a6d3SLuigi Rizzo 71037e3a6d3SLuigi Rizzo /* Device initialization routine. */ 71137e3a6d3SLuigi Rizzo static int 71237e3a6d3SLuigi Rizzo ptn_memdev_attach(device_t dev) 71337e3a6d3SLuigi Rizzo { 71437e3a6d3SLuigi Rizzo struct ptnetmap_memdev *ptn_dev; 71537e3a6d3SLuigi Rizzo int rid; 71637e3a6d3SLuigi Rizzo uint16_t mem_id; 71737e3a6d3SLuigi Rizzo 71837e3a6d3SLuigi Rizzo D("ptn_memdev_driver attach"); 71937e3a6d3SLuigi Rizzo 72037e3a6d3SLuigi Rizzo ptn_dev = device_get_softc(dev); 72137e3a6d3SLuigi Rizzo ptn_dev->dev = dev; 72237e3a6d3SLuigi Rizzo 72337e3a6d3SLuigi Rizzo pci_enable_busmaster(dev); 72437e3a6d3SLuigi Rizzo 72537e3a6d3SLuigi Rizzo rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR); 72637e3a6d3SLuigi Rizzo ptn_dev->pci_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 72737e3a6d3SLuigi Rizzo RF_ACTIVE); 72837e3a6d3SLuigi Rizzo if (ptn_dev->pci_io == NULL) { 72937e3a6d3SLuigi Rizzo device_printf(dev, "cannot map I/O space\n"); 73037e3a6d3SLuigi Rizzo return (ENXIO); 73137e3a6d3SLuigi Rizzo } 73237e3a6d3SLuigi Rizzo 73337e3a6d3SLuigi Rizzo mem_id = ptn_ioread16(ptn_dev, PTNETMAP_IO_PCI_HOSTID); 73437e3a6d3SLuigi Rizzo 73537e3a6d3SLuigi Rizzo /* create guest allocator */ 73637e3a6d3SLuigi Rizzo ptn_dev->nm_mem = netmap_mem_pt_guest_attach(ptn_dev, mem_id); 73737e3a6d3SLuigi Rizzo if (ptn_dev->nm_mem == NULL) { 73837e3a6d3SLuigi Rizzo ptn_memdev_detach(dev); 73937e3a6d3SLuigi Rizzo return (ENOMEM); 74037e3a6d3SLuigi Rizzo } 74137e3a6d3SLuigi Rizzo netmap_mem_get(ptn_dev->nm_mem); 74237e3a6d3SLuigi Rizzo 74337e3a6d3SLuigi Rizzo D("ptn_memdev_driver probe OK - host_id: %d", mem_id); 74437e3a6d3SLuigi Rizzo 74537e3a6d3SLuigi Rizzo return (0); 74637e3a6d3SLuigi Rizzo } 74737e3a6d3SLuigi Rizzo 74837e3a6d3SLuigi Rizzo /* Device removal routine. */ 74937e3a6d3SLuigi Rizzo static int 75037e3a6d3SLuigi Rizzo ptn_memdev_detach(device_t dev) 75137e3a6d3SLuigi Rizzo { 75237e3a6d3SLuigi Rizzo struct ptnetmap_memdev *ptn_dev; 75337e3a6d3SLuigi Rizzo 75437e3a6d3SLuigi Rizzo D("ptn_memdev_driver detach"); 75537e3a6d3SLuigi Rizzo ptn_dev = device_get_softc(dev); 75637e3a6d3SLuigi Rizzo 75737e3a6d3SLuigi Rizzo if (ptn_dev->nm_mem) { 75837e3a6d3SLuigi Rizzo netmap_mem_put(ptn_dev->nm_mem); 75937e3a6d3SLuigi Rizzo ptn_dev->nm_mem = NULL; 76037e3a6d3SLuigi Rizzo } 76137e3a6d3SLuigi Rizzo if (ptn_dev->pci_mem) { 76237e3a6d3SLuigi Rizzo bus_release_resource(dev, SYS_RES_MEMORY, 76337e3a6d3SLuigi Rizzo PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem); 76437e3a6d3SLuigi Rizzo ptn_dev->pci_mem = NULL; 76537e3a6d3SLuigi Rizzo } 76637e3a6d3SLuigi Rizzo if (ptn_dev->pci_io) { 76737e3a6d3SLuigi Rizzo bus_release_resource(dev, SYS_RES_IOPORT, 76837e3a6d3SLuigi Rizzo PCIR_BAR(PTNETMAP_IO_PCI_BAR), ptn_dev->pci_io); 76937e3a6d3SLuigi Rizzo ptn_dev->pci_io = NULL; 77037e3a6d3SLuigi Rizzo } 77137e3a6d3SLuigi Rizzo 77237e3a6d3SLuigi Rizzo return (0); 77337e3a6d3SLuigi Rizzo } 77437e3a6d3SLuigi Rizzo 77537e3a6d3SLuigi Rizzo static int 77637e3a6d3SLuigi Rizzo ptn_memdev_shutdown(device_t dev) 77737e3a6d3SLuigi Rizzo { 77837e3a6d3SLuigi Rizzo D("ptn_memdev_driver shutdown"); 77937e3a6d3SLuigi Rizzo return bus_generic_shutdown(dev); 78037e3a6d3SLuigi Rizzo } 78137e3a6d3SLuigi Rizzo 78237e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */ 78337e3a6d3SLuigi Rizzo 784f9790aebSLuigi Rizzo /* 785f9790aebSLuigi Rizzo * In order to track whether pages are still mapped, we hook into 786f9790aebSLuigi Rizzo * the standard cdev_pager and intercept the constructor and 787f9790aebSLuigi Rizzo * destructor. 788f9790aebSLuigi Rizzo */ 789f9790aebSLuigi Rizzo 790f9790aebSLuigi Rizzo struct netmap_vm_handle_t { 791f9790aebSLuigi Rizzo struct cdev *dev; 792f9790aebSLuigi Rizzo struct netmap_priv_d *priv; 793f9790aebSLuigi Rizzo }; 794f9790aebSLuigi Rizzo 79517885a7bSLuigi Rizzo 796f9790aebSLuigi Rizzo static int 797f9790aebSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot, 798f9790aebSLuigi Rizzo vm_ooffset_t foff, struct ucred *cred, u_short *color) 799f9790aebSLuigi Rizzo { 800f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh = handle; 801f0ea3689SLuigi Rizzo 802f0ea3689SLuigi Rizzo if (netmap_verbose) 803f9790aebSLuigi Rizzo D("handle %p size %jd prot %d foff %jd", 804f9790aebSLuigi Rizzo handle, (intmax_t)size, prot, (intmax_t)foff); 8054e93beffSLuigi Rizzo if (color) 8064e93beffSLuigi Rizzo *color = 0; 807f9790aebSLuigi Rizzo dev_ref(vmh->dev); 808f9790aebSLuigi Rizzo return 0; 809f9790aebSLuigi Rizzo } 810f9790aebSLuigi Rizzo 811f9790aebSLuigi Rizzo 812f9790aebSLuigi Rizzo static void 813f9790aebSLuigi Rizzo netmap_dev_pager_dtor(void *handle) 814f9790aebSLuigi Rizzo { 815f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh = handle; 816f9790aebSLuigi Rizzo struct cdev *dev = vmh->dev; 817f9790aebSLuigi Rizzo struct netmap_priv_d *priv = vmh->priv; 818f0ea3689SLuigi Rizzo 819f0ea3689SLuigi Rizzo if (netmap_verbose) 820f9790aebSLuigi Rizzo D("handle %p", handle); 821f9790aebSLuigi Rizzo netmap_dtor(priv); 822f9790aebSLuigi Rizzo free(vmh, M_DEVBUF); 823f9790aebSLuigi Rizzo dev_rel(dev); 824f9790aebSLuigi Rizzo } 825f9790aebSLuigi Rizzo 82617885a7bSLuigi Rizzo 827f9790aebSLuigi Rizzo static int 828f9790aebSLuigi Rizzo netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset, 829f9790aebSLuigi Rizzo int prot, vm_page_t *mres) 830f9790aebSLuigi Rizzo { 831f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh = object->handle; 832f9790aebSLuigi Rizzo struct netmap_priv_d *priv = vmh->priv; 833847bf383SLuigi Rizzo struct netmap_adapter *na = priv->np_na; 834f9790aebSLuigi Rizzo vm_paddr_t paddr; 835f9790aebSLuigi Rizzo vm_page_t page; 836f9790aebSLuigi Rizzo vm_memattr_t memattr; 837f9790aebSLuigi Rizzo vm_pindex_t pidx; 838f9790aebSLuigi Rizzo 839f9790aebSLuigi Rizzo ND("object %p offset %jd prot %d mres %p", 840f9790aebSLuigi Rizzo object, (intmax_t)offset, prot, mres); 841f9790aebSLuigi Rizzo memattr = object->memattr; 842f9790aebSLuigi Rizzo pidx = OFF_TO_IDX(offset); 843847bf383SLuigi Rizzo paddr = netmap_mem_ofstophys(na->nm_mem, offset); 844f9790aebSLuigi Rizzo if (paddr == 0) 845f9790aebSLuigi Rizzo return VM_PAGER_FAIL; 846f9790aebSLuigi Rizzo 847f9790aebSLuigi Rizzo if (((*mres)->flags & PG_FICTITIOUS) != 0) { 848f9790aebSLuigi Rizzo /* 849f9790aebSLuigi Rizzo * If the passed in result page is a fake page, update it with 850f9790aebSLuigi Rizzo * the new physical address. 851f9790aebSLuigi Rizzo */ 852f9790aebSLuigi Rizzo page = *mres; 853f9790aebSLuigi Rizzo vm_page_updatefake(page, paddr, memattr); 854f9790aebSLuigi Rizzo } else { 855f9790aebSLuigi Rizzo /* 856f9790aebSLuigi Rizzo * Replace the passed in reqpage page with our own fake page and 857f9790aebSLuigi Rizzo * free up the all of the original pages. 858f9790aebSLuigi Rizzo */ 859f9790aebSLuigi Rizzo #ifndef VM_OBJECT_WUNLOCK /* FreeBSD < 10.x */ 860f9790aebSLuigi Rizzo #define VM_OBJECT_WUNLOCK VM_OBJECT_UNLOCK 861f9790aebSLuigi Rizzo #define VM_OBJECT_WLOCK VM_OBJECT_LOCK 862f9790aebSLuigi Rizzo #endif /* VM_OBJECT_WUNLOCK */ 863f9790aebSLuigi Rizzo 864f9790aebSLuigi Rizzo VM_OBJECT_WUNLOCK(object); 865f9790aebSLuigi Rizzo page = vm_page_getfake(paddr, memattr); 866f9790aebSLuigi Rizzo VM_OBJECT_WLOCK(object); 867f9790aebSLuigi Rizzo vm_page_lock(*mres); 868f9790aebSLuigi Rizzo vm_page_free(*mres); 869f9790aebSLuigi Rizzo vm_page_unlock(*mres); 870f9790aebSLuigi Rizzo *mres = page; 871f9790aebSLuigi Rizzo vm_page_insert(page, object, pidx); 872f9790aebSLuigi Rizzo } 873f9790aebSLuigi Rizzo page->valid = VM_PAGE_BITS_ALL; 874f9790aebSLuigi Rizzo return (VM_PAGER_OK); 875f9790aebSLuigi Rizzo } 876f9790aebSLuigi Rizzo 877f9790aebSLuigi Rizzo 878f9790aebSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = { 879f9790aebSLuigi Rizzo .cdev_pg_ctor = netmap_dev_pager_ctor, 880f9790aebSLuigi Rizzo .cdev_pg_dtor = netmap_dev_pager_dtor, 881f9790aebSLuigi Rizzo .cdev_pg_fault = netmap_dev_pager_fault, 882f9790aebSLuigi Rizzo }; 883f9790aebSLuigi Rizzo 884f9790aebSLuigi Rizzo 885f9790aebSLuigi Rizzo static int 886f9790aebSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff, 887f9790aebSLuigi Rizzo vm_size_t objsize, vm_object_t *objp, int prot) 888f9790aebSLuigi Rizzo { 889f9790aebSLuigi Rizzo int error; 890f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh; 891f9790aebSLuigi Rizzo struct netmap_priv_d *priv; 892f9790aebSLuigi Rizzo vm_object_t obj; 893f9790aebSLuigi Rizzo 894f0ea3689SLuigi Rizzo if (netmap_verbose) 895f9790aebSLuigi Rizzo D("cdev %p foff %jd size %jd objp %p prot %d", cdev, 896f9790aebSLuigi Rizzo (intmax_t )*foff, (intmax_t )objsize, objp, prot); 897f9790aebSLuigi Rizzo 898f9790aebSLuigi Rizzo vmh = malloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF, 899f9790aebSLuigi Rizzo M_NOWAIT | M_ZERO); 900f9790aebSLuigi Rizzo if (vmh == NULL) 901f9790aebSLuigi Rizzo return ENOMEM; 902f9790aebSLuigi Rizzo vmh->dev = cdev; 903f9790aebSLuigi Rizzo 904f9790aebSLuigi Rizzo NMG_LOCK(); 905f9790aebSLuigi Rizzo error = devfs_get_cdevpriv((void**)&priv); 906f9790aebSLuigi Rizzo if (error) 907f9790aebSLuigi Rizzo goto err_unlock; 908847bf383SLuigi Rizzo if (priv->np_nifp == NULL) { 909847bf383SLuigi Rizzo error = EINVAL; 910847bf383SLuigi Rizzo goto err_unlock; 911847bf383SLuigi Rizzo } 912f9790aebSLuigi Rizzo vmh->priv = priv; 9138fd44c93SLuigi Rizzo priv->np_refs++; 914f9790aebSLuigi Rizzo NMG_UNLOCK(); 915f9790aebSLuigi Rizzo 916f9790aebSLuigi Rizzo obj = cdev_pager_allocate(vmh, OBJT_DEVICE, 917f9790aebSLuigi Rizzo &netmap_cdev_pager_ops, objsize, prot, 918f9790aebSLuigi Rizzo *foff, NULL); 919f9790aebSLuigi Rizzo if (obj == NULL) { 920f9790aebSLuigi Rizzo D("cdev_pager_allocate failed"); 921f9790aebSLuigi Rizzo error = EINVAL; 922f9790aebSLuigi Rizzo goto err_deref; 923f9790aebSLuigi Rizzo } 924f9790aebSLuigi Rizzo 925f9790aebSLuigi Rizzo *objp = obj; 926f9790aebSLuigi Rizzo return 0; 927f9790aebSLuigi Rizzo 928f9790aebSLuigi Rizzo err_deref: 929f9790aebSLuigi Rizzo NMG_LOCK(); 9308fd44c93SLuigi Rizzo priv->np_refs--; 931f9790aebSLuigi Rizzo err_unlock: 932f9790aebSLuigi Rizzo NMG_UNLOCK(); 933f9790aebSLuigi Rizzo // err: 934f9790aebSLuigi Rizzo free(vmh, M_DEVBUF); 935f9790aebSLuigi Rizzo return error; 936f9790aebSLuigi Rizzo } 937f9790aebSLuigi Rizzo 938847bf383SLuigi Rizzo /* 9398fd44c93SLuigi Rizzo * On FreeBSD the close routine is only called on the last close on 9408fd44c93SLuigi Rizzo * the device (/dev/netmap) so we cannot do anything useful. 9418fd44c93SLuigi Rizzo * To track close() on individual file descriptors we pass netmap_dtor() to 942847bf383SLuigi Rizzo * devfs_set_cdevpriv() on open(). The FreeBSD kernel will call the destructor 943847bf383SLuigi Rizzo * when the last fd pointing to the device is closed. 944847bf383SLuigi Rizzo * 9458fd44c93SLuigi Rizzo * Note that FreeBSD does not even munmap() on close() so we also have 9468fd44c93SLuigi Rizzo * to track mmap() ourselves, and postpone the call to 947847bf383SLuigi Rizzo * netmap_dtor() is called when the process has no open fds and no active 948847bf383SLuigi Rizzo * memory maps on /dev/netmap, as in linux. 949847bf383SLuigi Rizzo */ 950f9790aebSLuigi Rizzo static int 951f9790aebSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 952f9790aebSLuigi Rizzo { 953f9790aebSLuigi Rizzo if (netmap_verbose) 954f9790aebSLuigi Rizzo D("dev %p fflag 0x%x devtype %d td %p", 955f9790aebSLuigi Rizzo dev, fflag, devtype, td); 956f9790aebSLuigi Rizzo return 0; 957f9790aebSLuigi Rizzo } 958f9790aebSLuigi Rizzo 959f9790aebSLuigi Rizzo 960f9790aebSLuigi Rizzo static int 961f9790aebSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 962f9790aebSLuigi Rizzo { 963f9790aebSLuigi Rizzo struct netmap_priv_d *priv; 964f9790aebSLuigi Rizzo int error; 965f9790aebSLuigi Rizzo 966f9790aebSLuigi Rizzo (void)dev; 967f9790aebSLuigi Rizzo (void)oflags; 968f9790aebSLuigi Rizzo (void)devtype; 969f9790aebSLuigi Rizzo (void)td; 970f9790aebSLuigi Rizzo 97137e3a6d3SLuigi Rizzo NMG_LOCK(); 97237e3a6d3SLuigi Rizzo priv = netmap_priv_new(); 97337e3a6d3SLuigi Rizzo if (priv == NULL) { 97437e3a6d3SLuigi Rizzo error = ENOMEM; 97537e3a6d3SLuigi Rizzo goto out; 97637e3a6d3SLuigi Rizzo } 977f9790aebSLuigi Rizzo error = devfs_set_cdevpriv(priv, netmap_dtor); 9788fd44c93SLuigi Rizzo if (error) { 97937e3a6d3SLuigi Rizzo netmap_priv_delete(priv); 9808fd44c93SLuigi Rizzo } 98137e3a6d3SLuigi Rizzo out: 98237e3a6d3SLuigi Rizzo NMG_UNLOCK(); 983f9790aebSLuigi Rizzo return error; 984f9790aebSLuigi Rizzo } 985f9790aebSLuigi Rizzo 98637e3a6d3SLuigi Rizzo /******************** kthread wrapper ****************/ 98737e3a6d3SLuigi Rizzo #include <sys/sysproto.h> 98837e3a6d3SLuigi Rizzo u_int 98937e3a6d3SLuigi Rizzo nm_os_ncpus(void) 99037e3a6d3SLuigi Rizzo { 99137e3a6d3SLuigi Rizzo return mp_maxid + 1; 99237e3a6d3SLuigi Rizzo } 99337e3a6d3SLuigi Rizzo 99437e3a6d3SLuigi Rizzo struct nm_kthread_ctx { 99537e3a6d3SLuigi Rizzo struct thread *user_td; /* thread user-space (kthread creator) to send ioctl */ 99637e3a6d3SLuigi Rizzo /* notification to guest (interrupt) */ 99737e3a6d3SLuigi Rizzo int irq_fd; /* ioctl fd */ 99837e3a6d3SLuigi Rizzo struct nm_kth_ioctl irq_ioctl; /* ioctl arguments */ 99937e3a6d3SLuigi Rizzo 100037e3a6d3SLuigi Rizzo /* notification from guest */ 100137e3a6d3SLuigi Rizzo void *ioevent_file; /* tsleep() argument */ 100237e3a6d3SLuigi Rizzo 100337e3a6d3SLuigi Rizzo /* worker function and parameter */ 100437e3a6d3SLuigi Rizzo nm_kthread_worker_fn_t worker_fn; 100537e3a6d3SLuigi Rizzo void *worker_private; 100637e3a6d3SLuigi Rizzo 100737e3a6d3SLuigi Rizzo struct nm_kthread *nmk; 100837e3a6d3SLuigi Rizzo 100937e3a6d3SLuigi Rizzo /* integer to manage multiple worker contexts (e.g., RX or TX on ptnetmap) */ 101037e3a6d3SLuigi Rizzo long type; 101137e3a6d3SLuigi Rizzo }; 101237e3a6d3SLuigi Rizzo 101337e3a6d3SLuigi Rizzo struct nm_kthread { 101437e3a6d3SLuigi Rizzo struct thread *worker; 101537e3a6d3SLuigi Rizzo struct mtx worker_lock; 101637e3a6d3SLuigi Rizzo uint64_t scheduled; /* pending wake_up request */ 101737e3a6d3SLuigi Rizzo struct nm_kthread_ctx worker_ctx; 101837e3a6d3SLuigi Rizzo int run; /* used to stop kthread */ 101937e3a6d3SLuigi Rizzo int attach_user; /* kthread attached to user_process */ 102037e3a6d3SLuigi Rizzo int affinity; 102137e3a6d3SLuigi Rizzo }; 102237e3a6d3SLuigi Rizzo 102337e3a6d3SLuigi Rizzo void inline 102437e3a6d3SLuigi Rizzo nm_os_kthread_wakeup_worker(struct nm_kthread *nmk) 102537e3a6d3SLuigi Rizzo { 102637e3a6d3SLuigi Rizzo /* 102737e3a6d3SLuigi Rizzo * There may be a race between FE and BE, 102837e3a6d3SLuigi Rizzo * which call both this function, and worker kthread, 102937e3a6d3SLuigi Rizzo * that reads nmk->scheduled. 103037e3a6d3SLuigi Rizzo * 103137e3a6d3SLuigi Rizzo * For us it is not important the counter value, 103237e3a6d3SLuigi Rizzo * but simply that it has changed since the last 103337e3a6d3SLuigi Rizzo * time the kthread saw it. 103437e3a6d3SLuigi Rizzo */ 103537e3a6d3SLuigi Rizzo mtx_lock(&nmk->worker_lock); 103637e3a6d3SLuigi Rizzo nmk->scheduled++; 103737e3a6d3SLuigi Rizzo if (nmk->worker_ctx.ioevent_file) { 103837e3a6d3SLuigi Rizzo wakeup(nmk->worker_ctx.ioevent_file); 103937e3a6d3SLuigi Rizzo } 104037e3a6d3SLuigi Rizzo mtx_unlock(&nmk->worker_lock); 104137e3a6d3SLuigi Rizzo } 104237e3a6d3SLuigi Rizzo 104337e3a6d3SLuigi Rizzo void inline 104437e3a6d3SLuigi Rizzo nm_os_kthread_send_irq(struct nm_kthread *nmk) 104537e3a6d3SLuigi Rizzo { 104637e3a6d3SLuigi Rizzo struct nm_kthread_ctx *ctx = &nmk->worker_ctx; 104737e3a6d3SLuigi Rizzo int err; 104837e3a6d3SLuigi Rizzo 104937e3a6d3SLuigi Rizzo if (ctx->user_td && ctx->irq_fd > 0) { 105037e3a6d3SLuigi Rizzo err = kern_ioctl(ctx->user_td, ctx->irq_fd, ctx->irq_ioctl.com, (caddr_t)&ctx->irq_ioctl.data.msix); 105137e3a6d3SLuigi Rizzo if (err) { 1052e3f94e51SSepherosa Ziehau D("kern_ioctl error: %d ioctl parameters: fd %d com %ju data %p", 1053e3f94e51SSepherosa Ziehau err, ctx->irq_fd, (uintmax_t)ctx->irq_ioctl.com, &ctx->irq_ioctl.data); 105437e3a6d3SLuigi Rizzo } 105537e3a6d3SLuigi Rizzo } 105637e3a6d3SLuigi Rizzo } 105737e3a6d3SLuigi Rizzo 105837e3a6d3SLuigi Rizzo static void 105937e3a6d3SLuigi Rizzo nm_kthread_worker(void *data) 106037e3a6d3SLuigi Rizzo { 106137e3a6d3SLuigi Rizzo struct nm_kthread *nmk = data; 106237e3a6d3SLuigi Rizzo struct nm_kthread_ctx *ctx = &nmk->worker_ctx; 106337e3a6d3SLuigi Rizzo uint64_t old_scheduled = nmk->scheduled; 106437e3a6d3SLuigi Rizzo 106537e3a6d3SLuigi Rizzo if (nmk->affinity >= 0) { 106637e3a6d3SLuigi Rizzo thread_lock(curthread); 106737e3a6d3SLuigi Rizzo sched_bind(curthread, nmk->affinity); 106837e3a6d3SLuigi Rizzo thread_unlock(curthread); 106937e3a6d3SLuigi Rizzo } 107037e3a6d3SLuigi Rizzo 107137e3a6d3SLuigi Rizzo while (nmk->run) { 107237e3a6d3SLuigi Rizzo /* 107337e3a6d3SLuigi Rizzo * check if the parent process dies 107437e3a6d3SLuigi Rizzo * (when kthread is attached to user process) 107537e3a6d3SLuigi Rizzo */ 107637e3a6d3SLuigi Rizzo if (ctx->user_td) { 107737e3a6d3SLuigi Rizzo PROC_LOCK(curproc); 107837e3a6d3SLuigi Rizzo thread_suspend_check(0); 107937e3a6d3SLuigi Rizzo PROC_UNLOCK(curproc); 108037e3a6d3SLuigi Rizzo } else { 108137e3a6d3SLuigi Rizzo kthread_suspend_check(); 108237e3a6d3SLuigi Rizzo } 108337e3a6d3SLuigi Rizzo 108437e3a6d3SLuigi Rizzo /* 108537e3a6d3SLuigi Rizzo * if ioevent_file is not defined, we don't have notification 108637e3a6d3SLuigi Rizzo * mechanism and we continually execute worker_fn() 108737e3a6d3SLuigi Rizzo */ 108837e3a6d3SLuigi Rizzo if (!ctx->ioevent_file) { 108937e3a6d3SLuigi Rizzo ctx->worker_fn(ctx->worker_private); /* worker body */ 109037e3a6d3SLuigi Rizzo } else { 109137e3a6d3SLuigi Rizzo /* checks if there is a pending notification */ 109237e3a6d3SLuigi Rizzo mtx_lock(&nmk->worker_lock); 109337e3a6d3SLuigi Rizzo if (likely(nmk->scheduled != old_scheduled)) { 109437e3a6d3SLuigi Rizzo old_scheduled = nmk->scheduled; 109537e3a6d3SLuigi Rizzo mtx_unlock(&nmk->worker_lock); 109637e3a6d3SLuigi Rizzo 109737e3a6d3SLuigi Rizzo ctx->worker_fn(ctx->worker_private); /* worker body */ 109837e3a6d3SLuigi Rizzo 109937e3a6d3SLuigi Rizzo continue; 110037e3a6d3SLuigi Rizzo } else if (nmk->run) { 110137e3a6d3SLuigi Rizzo /* wait on event with one second timeout */ 110237e3a6d3SLuigi Rizzo msleep_spin(ctx->ioevent_file, &nmk->worker_lock, 110337e3a6d3SLuigi Rizzo "nmk_ev", hz); 110437e3a6d3SLuigi Rizzo nmk->scheduled++; 110537e3a6d3SLuigi Rizzo } 110637e3a6d3SLuigi Rizzo mtx_unlock(&nmk->worker_lock); 110737e3a6d3SLuigi Rizzo } 110837e3a6d3SLuigi Rizzo } 110937e3a6d3SLuigi Rizzo 111037e3a6d3SLuigi Rizzo kthread_exit(); 111137e3a6d3SLuigi Rizzo } 111237e3a6d3SLuigi Rizzo 111337e3a6d3SLuigi Rizzo static int 111437e3a6d3SLuigi Rizzo nm_kthread_open_files(struct nm_kthread *nmk, struct nm_kthread_cfg *cfg) 111537e3a6d3SLuigi Rizzo { 111637e3a6d3SLuigi Rizzo /* send irq through ioctl to bhyve (vmm.ko) */ 111737e3a6d3SLuigi Rizzo if (cfg->event.irqfd) { 111837e3a6d3SLuigi Rizzo nmk->worker_ctx.irq_fd = cfg->event.irqfd; 111937e3a6d3SLuigi Rizzo nmk->worker_ctx.irq_ioctl = cfg->event.ioctl; 112037e3a6d3SLuigi Rizzo } 112137e3a6d3SLuigi Rizzo /* ring.ioeventfd contains the chan where do tsleep to wait events */ 112237e3a6d3SLuigi Rizzo if (cfg->event.ioeventfd) { 112337e3a6d3SLuigi Rizzo nmk->worker_ctx.ioevent_file = (void *)cfg->event.ioeventfd; 112437e3a6d3SLuigi Rizzo } 112537e3a6d3SLuigi Rizzo 112637e3a6d3SLuigi Rizzo return 0; 112737e3a6d3SLuigi Rizzo } 112837e3a6d3SLuigi Rizzo 112937e3a6d3SLuigi Rizzo static void 113037e3a6d3SLuigi Rizzo nm_kthread_close_files(struct nm_kthread *nmk) 113137e3a6d3SLuigi Rizzo { 113237e3a6d3SLuigi Rizzo nmk->worker_ctx.irq_fd = 0; 113337e3a6d3SLuigi Rizzo nmk->worker_ctx.ioevent_file = NULL; 113437e3a6d3SLuigi Rizzo } 113537e3a6d3SLuigi Rizzo 113637e3a6d3SLuigi Rizzo void 113737e3a6d3SLuigi Rizzo nm_os_kthread_set_affinity(struct nm_kthread *nmk, int affinity) 113837e3a6d3SLuigi Rizzo { 113937e3a6d3SLuigi Rizzo nmk->affinity = affinity; 114037e3a6d3SLuigi Rizzo } 114137e3a6d3SLuigi Rizzo 114237e3a6d3SLuigi Rizzo struct nm_kthread * 114337e3a6d3SLuigi Rizzo nm_os_kthread_create(struct nm_kthread_cfg *cfg) 114437e3a6d3SLuigi Rizzo { 114537e3a6d3SLuigi Rizzo struct nm_kthread *nmk = NULL; 114637e3a6d3SLuigi Rizzo int error; 114737e3a6d3SLuigi Rizzo 114837e3a6d3SLuigi Rizzo nmk = malloc(sizeof(*nmk), M_DEVBUF, M_NOWAIT | M_ZERO); 114937e3a6d3SLuigi Rizzo if (!nmk) 115037e3a6d3SLuigi Rizzo return NULL; 115137e3a6d3SLuigi Rizzo 115237e3a6d3SLuigi Rizzo mtx_init(&nmk->worker_lock, "nm_kthread lock", NULL, MTX_SPIN); 115337e3a6d3SLuigi Rizzo nmk->worker_ctx.worker_fn = cfg->worker_fn; 115437e3a6d3SLuigi Rizzo nmk->worker_ctx.worker_private = cfg->worker_private; 115537e3a6d3SLuigi Rizzo nmk->worker_ctx.type = cfg->type; 115637e3a6d3SLuigi Rizzo nmk->affinity = -1; 115737e3a6d3SLuigi Rizzo 115837e3a6d3SLuigi Rizzo /* attach kthread to user process (ptnetmap) */ 115937e3a6d3SLuigi Rizzo nmk->attach_user = cfg->attach_user; 116037e3a6d3SLuigi Rizzo 116137e3a6d3SLuigi Rizzo /* open event fd */ 116237e3a6d3SLuigi Rizzo error = nm_kthread_open_files(nmk, cfg); 116337e3a6d3SLuigi Rizzo if (error) 116437e3a6d3SLuigi Rizzo goto err; 116537e3a6d3SLuigi Rizzo 116637e3a6d3SLuigi Rizzo return nmk; 116737e3a6d3SLuigi Rizzo err: 116837e3a6d3SLuigi Rizzo free(nmk, M_DEVBUF); 116937e3a6d3SLuigi Rizzo return NULL; 117037e3a6d3SLuigi Rizzo } 117137e3a6d3SLuigi Rizzo 117237e3a6d3SLuigi Rizzo int 117337e3a6d3SLuigi Rizzo nm_os_kthread_start(struct nm_kthread *nmk) 117437e3a6d3SLuigi Rizzo { 117537e3a6d3SLuigi Rizzo struct proc *p = NULL; 117637e3a6d3SLuigi Rizzo int error = 0; 117737e3a6d3SLuigi Rizzo 117837e3a6d3SLuigi Rizzo if (nmk->worker) { 117937e3a6d3SLuigi Rizzo return EBUSY; 118037e3a6d3SLuigi Rizzo } 118137e3a6d3SLuigi Rizzo 118237e3a6d3SLuigi Rizzo /* check if we want to attach kthread to user process */ 118337e3a6d3SLuigi Rizzo if (nmk->attach_user) { 118437e3a6d3SLuigi Rizzo nmk->worker_ctx.user_td = curthread; 118537e3a6d3SLuigi Rizzo p = curthread->td_proc; 118637e3a6d3SLuigi Rizzo } 118737e3a6d3SLuigi Rizzo 118837e3a6d3SLuigi Rizzo /* enable kthread main loop */ 118937e3a6d3SLuigi Rizzo nmk->run = 1; 119037e3a6d3SLuigi Rizzo /* create kthread */ 119137e3a6d3SLuigi Rizzo if((error = kthread_add(nm_kthread_worker, nmk, p, 119237e3a6d3SLuigi Rizzo &nmk->worker, RFNOWAIT /* to be checked */, 0, "nm-kthread-%ld", 119337e3a6d3SLuigi Rizzo nmk->worker_ctx.type))) { 119437e3a6d3SLuigi Rizzo goto err; 119537e3a6d3SLuigi Rizzo } 119637e3a6d3SLuigi Rizzo 119737e3a6d3SLuigi Rizzo D("nm_kthread started td 0x%p", nmk->worker); 119837e3a6d3SLuigi Rizzo 119937e3a6d3SLuigi Rizzo return 0; 120037e3a6d3SLuigi Rizzo err: 120137e3a6d3SLuigi Rizzo D("nm_kthread start failed err %d", error); 120237e3a6d3SLuigi Rizzo nmk->worker = NULL; 120337e3a6d3SLuigi Rizzo return error; 120437e3a6d3SLuigi Rizzo } 120537e3a6d3SLuigi Rizzo 120637e3a6d3SLuigi Rizzo void 120737e3a6d3SLuigi Rizzo nm_os_kthread_stop(struct nm_kthread *nmk) 120837e3a6d3SLuigi Rizzo { 120937e3a6d3SLuigi Rizzo if (!nmk->worker) { 121037e3a6d3SLuigi Rizzo return; 121137e3a6d3SLuigi Rizzo } 121237e3a6d3SLuigi Rizzo /* tell to kthread to exit from main loop */ 121337e3a6d3SLuigi Rizzo nmk->run = 0; 121437e3a6d3SLuigi Rizzo 121537e3a6d3SLuigi Rizzo /* wake up kthread if it sleeps */ 121637e3a6d3SLuigi Rizzo kthread_resume(nmk->worker); 121737e3a6d3SLuigi Rizzo nm_os_kthread_wakeup_worker(nmk); 121837e3a6d3SLuigi Rizzo 121937e3a6d3SLuigi Rizzo nmk->worker = NULL; 122037e3a6d3SLuigi Rizzo } 122137e3a6d3SLuigi Rizzo 122237e3a6d3SLuigi Rizzo void 122337e3a6d3SLuigi Rizzo nm_os_kthread_delete(struct nm_kthread *nmk) 122437e3a6d3SLuigi Rizzo { 122537e3a6d3SLuigi Rizzo if (!nmk) 122637e3a6d3SLuigi Rizzo return; 122737e3a6d3SLuigi Rizzo if (nmk->worker) { 122837e3a6d3SLuigi Rizzo nm_os_kthread_stop(nmk); 122937e3a6d3SLuigi Rizzo } 123037e3a6d3SLuigi Rizzo 123137e3a6d3SLuigi Rizzo nm_kthread_close_files(nmk); 123237e3a6d3SLuigi Rizzo 123337e3a6d3SLuigi Rizzo free(nmk, M_DEVBUF); 123437e3a6d3SLuigi Rizzo } 123537e3a6d3SLuigi Rizzo 1236f0ea3689SLuigi Rizzo /******************** kqueue support ****************/ 1237f0ea3689SLuigi Rizzo 1238f0ea3689SLuigi Rizzo /* 123937e3a6d3SLuigi Rizzo * nm_os_selwakeup also needs to issue a KNOTE_UNLOCKED. 1240f0ea3689SLuigi Rizzo * We use a non-zero argument to distinguish the call from the one 1241f0ea3689SLuigi Rizzo * in kevent_scan() which instead also needs to run netmap_poll(). 1242f0ea3689SLuigi Rizzo * The knote uses a global mutex for the time being. We might 1243f0ea3689SLuigi Rizzo * try to reuse the one in the si, but it is not allocated 1244f0ea3689SLuigi Rizzo * permanently so it might be a bit tricky. 1245f0ea3689SLuigi Rizzo * 1246f0ea3689SLuigi Rizzo * The *kqfilter function registers one or another f_event 1247f0ea3689SLuigi Rizzo * depending on read or write mode. 1248f0ea3689SLuigi Rizzo * In the call to f_event() td_fpop is NULL so any child function 1249f0ea3689SLuigi Rizzo * calling devfs_get_cdevpriv() would fail - and we need it in 1250f0ea3689SLuigi Rizzo * netmap_poll(). As a workaround we store priv into kn->kn_hook 1251f0ea3689SLuigi Rizzo * and pass it as first argument to netmap_poll(), which then 1252f0ea3689SLuigi Rizzo * uses the failure to tell that we are called from f_event() 1253f0ea3689SLuigi Rizzo * and do not need the selrecord(). 1254f0ea3689SLuigi Rizzo */ 1255f0ea3689SLuigi Rizzo 1256f0ea3689SLuigi Rizzo 1257f0ea3689SLuigi Rizzo void 125837e3a6d3SLuigi Rizzo nm_os_selwakeup(struct nm_selinfo *si) 1259f0ea3689SLuigi Rizzo { 1260f0ea3689SLuigi Rizzo if (netmap_verbose) 12610e73f29aSLuigi Rizzo D("on knote %p", &si->si.si_note); 126237e3a6d3SLuigi Rizzo selwakeuppri(&si->si, PI_NET); 1263f0ea3689SLuigi Rizzo /* use a non-zero hint to tell the notification from the 1264f0ea3689SLuigi Rizzo * call done in kqueue_scan() which uses 0 1265f0ea3689SLuigi Rizzo */ 12660e73f29aSLuigi Rizzo KNOTE_UNLOCKED(&si->si.si_note, 0x100 /* notification */); 1267f0ea3689SLuigi Rizzo } 1268f0ea3689SLuigi Rizzo 126937e3a6d3SLuigi Rizzo void 127037e3a6d3SLuigi Rizzo nm_os_selrecord(struct thread *td, struct nm_selinfo *si) 127137e3a6d3SLuigi Rizzo { 127237e3a6d3SLuigi Rizzo selrecord(td, &si->si); 127337e3a6d3SLuigi Rizzo } 127437e3a6d3SLuigi Rizzo 1275f0ea3689SLuigi Rizzo static void 1276f0ea3689SLuigi Rizzo netmap_knrdetach(struct knote *kn) 1277f0ea3689SLuigi Rizzo { 1278f0ea3689SLuigi Rizzo struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook; 1279847bf383SLuigi Rizzo struct selinfo *si = &priv->np_si[NR_RX]->si; 1280f0ea3689SLuigi Rizzo 1281f0ea3689SLuigi Rizzo D("remove selinfo %p", si); 1282f0ea3689SLuigi Rizzo knlist_remove(&si->si_note, kn, 0); 1283f0ea3689SLuigi Rizzo } 1284f0ea3689SLuigi Rizzo 1285f0ea3689SLuigi Rizzo static void 1286f0ea3689SLuigi Rizzo netmap_knwdetach(struct knote *kn) 1287f0ea3689SLuigi Rizzo { 1288f0ea3689SLuigi Rizzo struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook; 1289847bf383SLuigi Rizzo struct selinfo *si = &priv->np_si[NR_TX]->si; 1290f0ea3689SLuigi Rizzo 1291f0ea3689SLuigi Rizzo D("remove selinfo %p", si); 1292f0ea3689SLuigi Rizzo knlist_remove(&si->si_note, kn, 0); 1293f0ea3689SLuigi Rizzo } 1294f0ea3689SLuigi Rizzo 1295f0ea3689SLuigi Rizzo /* 1296f0ea3689SLuigi Rizzo * callback from notifies (generated externally) and our 1297f0ea3689SLuigi Rizzo * calls to kevent(). The former we just return 1 (ready) 1298f0ea3689SLuigi Rizzo * since we do not know better. 1299f0ea3689SLuigi Rizzo * In the latter we call netmap_poll and return 0/1 accordingly. 1300f0ea3689SLuigi Rizzo */ 1301f0ea3689SLuigi Rizzo static int 1302f0ea3689SLuigi Rizzo netmap_knrw(struct knote *kn, long hint, int events) 1303f0ea3689SLuigi Rizzo { 1304f0ea3689SLuigi Rizzo struct netmap_priv_d *priv; 1305f0ea3689SLuigi Rizzo int revents; 1306f0ea3689SLuigi Rizzo 1307f0ea3689SLuigi Rizzo if (hint != 0) { 1308f0ea3689SLuigi Rizzo ND(5, "call from notify"); 1309f0ea3689SLuigi Rizzo return 1; /* assume we are ready */ 1310f0ea3689SLuigi Rizzo } 1311f0ea3689SLuigi Rizzo priv = kn->kn_hook; 1312f0ea3689SLuigi Rizzo /* the notification may come from an external thread, 1313f0ea3689SLuigi Rizzo * in which case we do not want to run the netmap_poll 1314f0ea3689SLuigi Rizzo * This should be filtered above, but check just in case. 1315f0ea3689SLuigi Rizzo */ 1316f0ea3689SLuigi Rizzo if (curthread != priv->np_td) { /* should not happen */ 1317f0ea3689SLuigi Rizzo RD(5, "curthread changed %p %p", curthread, priv->np_td); 1318f0ea3689SLuigi Rizzo return 1; 1319f0ea3689SLuigi Rizzo } else { 132037e3a6d3SLuigi Rizzo revents = netmap_poll(priv, events, NULL); 1321f0ea3689SLuigi Rizzo return (events & revents) ? 1 : 0; 1322f0ea3689SLuigi Rizzo } 1323f0ea3689SLuigi Rizzo } 1324f0ea3689SLuigi Rizzo 1325f0ea3689SLuigi Rizzo static int 1326f0ea3689SLuigi Rizzo netmap_knread(struct knote *kn, long hint) 1327f0ea3689SLuigi Rizzo { 1328f0ea3689SLuigi Rizzo return netmap_knrw(kn, hint, POLLIN); 1329f0ea3689SLuigi Rizzo } 1330f0ea3689SLuigi Rizzo 1331f0ea3689SLuigi Rizzo static int 1332f0ea3689SLuigi Rizzo netmap_knwrite(struct knote *kn, long hint) 1333f0ea3689SLuigi Rizzo { 1334f0ea3689SLuigi Rizzo return netmap_knrw(kn, hint, POLLOUT); 1335f0ea3689SLuigi Rizzo } 1336f0ea3689SLuigi Rizzo 1337f0ea3689SLuigi Rizzo static struct filterops netmap_rfiltops = { 1338f0ea3689SLuigi Rizzo .f_isfd = 1, 1339f0ea3689SLuigi Rizzo .f_detach = netmap_knrdetach, 1340f0ea3689SLuigi Rizzo .f_event = netmap_knread, 1341f0ea3689SLuigi Rizzo }; 1342f0ea3689SLuigi Rizzo 1343f0ea3689SLuigi Rizzo static struct filterops netmap_wfiltops = { 1344f0ea3689SLuigi Rizzo .f_isfd = 1, 1345f0ea3689SLuigi Rizzo .f_detach = netmap_knwdetach, 1346f0ea3689SLuigi Rizzo .f_event = netmap_knwrite, 1347f0ea3689SLuigi Rizzo }; 1348f0ea3689SLuigi Rizzo 1349f0ea3689SLuigi Rizzo 1350f0ea3689SLuigi Rizzo /* 1351f0ea3689SLuigi Rizzo * This is called when a thread invokes kevent() to record 1352f0ea3689SLuigi Rizzo * a change in the configuration of the kqueue(). 1353f0ea3689SLuigi Rizzo * The 'priv' should be the same as in the netmap device. 1354f0ea3689SLuigi Rizzo */ 1355f0ea3689SLuigi Rizzo static int 1356f0ea3689SLuigi Rizzo netmap_kqfilter(struct cdev *dev, struct knote *kn) 1357f0ea3689SLuigi Rizzo { 1358f0ea3689SLuigi Rizzo struct netmap_priv_d *priv; 1359f0ea3689SLuigi Rizzo int error; 1360f0ea3689SLuigi Rizzo struct netmap_adapter *na; 13610e73f29aSLuigi Rizzo struct nm_selinfo *si; 1362f0ea3689SLuigi Rizzo int ev = kn->kn_filter; 1363f0ea3689SLuigi Rizzo 1364f0ea3689SLuigi Rizzo if (ev != EVFILT_READ && ev != EVFILT_WRITE) { 1365f0ea3689SLuigi Rizzo D("bad filter request %d", ev); 1366f0ea3689SLuigi Rizzo return 1; 1367f0ea3689SLuigi Rizzo } 1368f0ea3689SLuigi Rizzo error = devfs_get_cdevpriv((void**)&priv); 1369f0ea3689SLuigi Rizzo if (error) { 1370f0ea3689SLuigi Rizzo D("device not yet setup"); 1371f0ea3689SLuigi Rizzo return 1; 1372f0ea3689SLuigi Rizzo } 1373f0ea3689SLuigi Rizzo na = priv->np_na; 1374f0ea3689SLuigi Rizzo if (na == NULL) { 1375f0ea3689SLuigi Rizzo D("no netmap adapter for this file descriptor"); 1376f0ea3689SLuigi Rizzo return 1; 1377f0ea3689SLuigi Rizzo } 1378f0ea3689SLuigi Rizzo /* the si is indicated in the priv */ 1379847bf383SLuigi Rizzo si = priv->np_si[(ev == EVFILT_WRITE) ? NR_TX : NR_RX]; 1380f0ea3689SLuigi Rizzo // XXX lock(priv) ? 1381f0ea3689SLuigi Rizzo kn->kn_fop = (ev == EVFILT_WRITE) ? 1382f0ea3689SLuigi Rizzo &netmap_wfiltops : &netmap_rfiltops; 1383f0ea3689SLuigi Rizzo kn->kn_hook = priv; 13840e73f29aSLuigi Rizzo knlist_add(&si->si.si_note, kn, 1); 1385f0ea3689SLuigi Rizzo // XXX unlock(priv) 1386f0ea3689SLuigi Rizzo ND("register %p %s td %p priv %p kn %p np_nifp %p kn_fp/fpop %s", 1387f0ea3689SLuigi Rizzo na, na->ifp->if_xname, curthread, priv, kn, 1388f0ea3689SLuigi Rizzo priv->np_nifp, 1389f0ea3689SLuigi Rizzo kn->kn_fp == curthread->td_fpop ? "match" : "MISMATCH"); 1390f0ea3689SLuigi Rizzo return 0; 1391f0ea3689SLuigi Rizzo } 1392f9790aebSLuigi Rizzo 139337e3a6d3SLuigi Rizzo static int 139437e3a6d3SLuigi Rizzo freebsd_netmap_poll(struct cdev *cdevi __unused, int events, struct thread *td) 139537e3a6d3SLuigi Rizzo { 139637e3a6d3SLuigi Rizzo struct netmap_priv_d *priv; 139737e3a6d3SLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv)) { 139837e3a6d3SLuigi Rizzo return POLLERR; 139937e3a6d3SLuigi Rizzo } 140037e3a6d3SLuigi Rizzo return netmap_poll(priv, events, td); 140137e3a6d3SLuigi Rizzo } 140237e3a6d3SLuigi Rizzo 140337e3a6d3SLuigi Rizzo static int 140437e3a6d3SLuigi Rizzo freebsd_netmap_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data, 140537e3a6d3SLuigi Rizzo int ffla __unused, struct thread *td) 140637e3a6d3SLuigi Rizzo { 140737e3a6d3SLuigi Rizzo int error; 140837e3a6d3SLuigi Rizzo struct netmap_priv_d *priv; 140937e3a6d3SLuigi Rizzo 1410*ffaa5debSSepherosa Ziehau CURVNET_SET(TD_TO_VNET(td)); 141137e3a6d3SLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv); 141237e3a6d3SLuigi Rizzo if (error) { 141337e3a6d3SLuigi Rizzo /* XXX ENOENT should be impossible, since the priv 141437e3a6d3SLuigi Rizzo * is now created in the open */ 141537e3a6d3SLuigi Rizzo if (error == ENOENT) 141637e3a6d3SLuigi Rizzo error = ENXIO; 141737e3a6d3SLuigi Rizzo goto out; 141837e3a6d3SLuigi Rizzo } 141937e3a6d3SLuigi Rizzo error = netmap_ioctl(priv, cmd, data, td); 142037e3a6d3SLuigi Rizzo out: 142137e3a6d3SLuigi Rizzo CURVNET_RESTORE(); 142237e3a6d3SLuigi Rizzo 142337e3a6d3SLuigi Rizzo return error; 142437e3a6d3SLuigi Rizzo } 142537e3a6d3SLuigi Rizzo 142637e3a6d3SLuigi Rizzo extern struct cdevsw netmap_cdevsw; /* XXX used in netmap.c, should go elsewhere */ 1427f9790aebSLuigi Rizzo struct cdevsw netmap_cdevsw = { 1428f9790aebSLuigi Rizzo .d_version = D_VERSION, 1429f9790aebSLuigi Rizzo .d_name = "netmap", 1430f9790aebSLuigi Rizzo .d_open = netmap_open, 1431f9790aebSLuigi Rizzo .d_mmap_single = netmap_mmap_single, 143237e3a6d3SLuigi Rizzo .d_ioctl = freebsd_netmap_ioctl, 143337e3a6d3SLuigi Rizzo .d_poll = freebsd_netmap_poll, 1434f0ea3689SLuigi Rizzo .d_kqfilter = netmap_kqfilter, 1435f9790aebSLuigi Rizzo .d_close = netmap_close, 1436f9790aebSLuigi Rizzo }; 1437f0ea3689SLuigi Rizzo /*--- end of kqueue support ----*/ 1438f9790aebSLuigi Rizzo 1439f9790aebSLuigi Rizzo /* 1440f9790aebSLuigi Rizzo * Kernel entry point. 1441f9790aebSLuigi Rizzo * 1442f9790aebSLuigi Rizzo * Initialize/finalize the module and return. 1443f9790aebSLuigi Rizzo * 1444f9790aebSLuigi Rizzo * Return 0 on success, errno on failure. 1445f9790aebSLuigi Rizzo */ 1446f9790aebSLuigi Rizzo static int 1447f9790aebSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg) 1448f9790aebSLuigi Rizzo { 1449f9790aebSLuigi Rizzo int error = 0; 1450f9790aebSLuigi Rizzo 1451f9790aebSLuigi Rizzo switch (event) { 1452f9790aebSLuigi Rizzo case MOD_LOAD: 1453f9790aebSLuigi Rizzo error = netmap_init(); 1454f9790aebSLuigi Rizzo break; 1455f9790aebSLuigi Rizzo 1456f9790aebSLuigi Rizzo case MOD_UNLOAD: 1457847adfb7SLuigi Rizzo /* 1458847adfb7SLuigi Rizzo * if some one is still using netmap, 1459847adfb7SLuigi Rizzo * then the module can not be unloaded. 1460847adfb7SLuigi Rizzo */ 1461847adfb7SLuigi Rizzo if (netmap_use_count) { 1462847adfb7SLuigi Rizzo D("netmap module can not be unloaded - netmap_use_count: %d", 1463847adfb7SLuigi Rizzo netmap_use_count); 1464847adfb7SLuigi Rizzo error = EBUSY; 1465847adfb7SLuigi Rizzo break; 1466847adfb7SLuigi Rizzo } 1467f9790aebSLuigi Rizzo netmap_fini(); 1468f9790aebSLuigi Rizzo break; 1469f9790aebSLuigi Rizzo 1470f9790aebSLuigi Rizzo default: 1471f9790aebSLuigi Rizzo error = EOPNOTSUPP; 1472f9790aebSLuigi Rizzo break; 1473f9790aebSLuigi Rizzo } 1474f9790aebSLuigi Rizzo 1475f9790aebSLuigi Rizzo return (error); 1476f9790aebSLuigi Rizzo } 1477f9790aebSLuigi Rizzo 147837e3a6d3SLuigi Rizzo #ifdef DEV_MODULE_ORDERED 147937e3a6d3SLuigi Rizzo /* 148037e3a6d3SLuigi Rizzo * The netmap module contains three drivers: (i) the netmap character device 148137e3a6d3SLuigi Rizzo * driver; (ii) the ptnetmap memdev PCI device driver, (iii) the ptnet PCI 148237e3a6d3SLuigi Rizzo * device driver. The attach() routines of both (ii) and (iii) need the 148337e3a6d3SLuigi Rizzo * lock of the global allocator, and such lock is initialized in netmap_init(), 148437e3a6d3SLuigi Rizzo * which is part of (i). 148537e3a6d3SLuigi Rizzo * Therefore, we make sure that (i) is loaded before (ii) and (iii), using 148637e3a6d3SLuigi Rizzo * the 'order' parameter of driver declaration macros. For (i), we specify 148737e3a6d3SLuigi Rizzo * SI_ORDER_MIDDLE, while higher orders are used with the DRIVER_MODULE_ORDERED 148837e3a6d3SLuigi Rizzo * macros for (ii) and (iii). 148937e3a6d3SLuigi Rizzo */ 149037e3a6d3SLuigi Rizzo DEV_MODULE_ORDERED(netmap, netmap_loader, NULL, SI_ORDER_MIDDLE); 149137e3a6d3SLuigi Rizzo #else /* !DEV_MODULE_ORDERED */ 1492f9790aebSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL); 149337e3a6d3SLuigi Rizzo #endif /* DEV_MODULE_ORDERED */ 149437e3a6d3SLuigi Rizzo MODULE_DEPEND(netmap, pci, 1, 1, 1); 1495735c8d95SLuigi Rizzo MODULE_VERSION(netmap, 1); 149637e3a6d3SLuigi Rizzo /* reduce conditional code */ 149737e3a6d3SLuigi Rizzo // linux API, use for the knlist in FreeBSD 149837e3a6d3SLuigi Rizzo /* use a private mutex for the knlist */ 1499