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