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