1 /*- 2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * 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 ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /* Driver for VirtIO network devices. */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/eventhandler.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/sockio.h> 37 #include <sys/mbuf.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/socket.h> 41 #include <sys/sysctl.h> 42 #include <sys/random.h> 43 #include <sys/sglist.h> 44 #include <sys/lock.h> 45 #include <sys/mutex.h> 46 #include <sys/taskqueue.h> 47 #include <sys/smp.h> 48 #include <machine/smp.h> 49 50 #include <vm/uma.h> 51 52 #include <net/ethernet.h> 53 #include <net/if.h> 54 #include <net/if_var.h> 55 #include <net/if_arp.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/if_media.h> 59 #include <net/if_vlan_var.h> 60 61 #include <net/bpf.h> 62 63 #include <netinet/in_systm.h> 64 #include <netinet/in.h> 65 #include <netinet/ip.h> 66 #include <netinet/ip6.h> 67 #include <netinet6/ip6_var.h> 68 #include <netinet/udp.h> 69 #include <netinet/tcp.h> 70 #include <netinet/sctp.h> 71 72 #include <machine/bus.h> 73 #include <machine/resource.h> 74 #include <sys/bus.h> 75 #include <sys/rman.h> 76 77 #include <dev/virtio/virtio.h> 78 #include <dev/virtio/virtqueue.h> 79 #include <dev/virtio/network/virtio_net.h> 80 #include <dev/virtio/network/if_vtnetvar.h> 81 82 #include "virtio_if.h" 83 84 #include "opt_inet.h" 85 #include "opt_inet6.h" 86 87 static int vtnet_modevent(module_t, int, void *); 88 89 static int vtnet_probe(device_t); 90 static int vtnet_attach(device_t); 91 static int vtnet_detach(device_t); 92 static int vtnet_suspend(device_t); 93 static int vtnet_resume(device_t); 94 static int vtnet_shutdown(device_t); 95 static int vtnet_attach_completed(device_t); 96 static int vtnet_config_change(device_t); 97 98 static void vtnet_negotiate_features(struct vtnet_softc *); 99 static void vtnet_setup_features(struct vtnet_softc *); 100 static int vtnet_init_rxq(struct vtnet_softc *, int); 101 static int vtnet_init_txq(struct vtnet_softc *, int); 102 static int vtnet_alloc_rxtx_queues(struct vtnet_softc *); 103 static void vtnet_free_rxtx_queues(struct vtnet_softc *); 104 static int vtnet_alloc_rx_filters(struct vtnet_softc *); 105 static void vtnet_free_rx_filters(struct vtnet_softc *); 106 static int vtnet_alloc_virtqueues(struct vtnet_softc *); 107 static int vtnet_setup_interface(struct vtnet_softc *); 108 static int vtnet_change_mtu(struct vtnet_softc *, int); 109 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t); 110 static uint64_t vtnet_get_counter(struct ifnet *, ift_counter); 111 112 static int vtnet_rxq_populate(struct vtnet_rxq *); 113 static void vtnet_rxq_free_mbufs(struct vtnet_rxq *); 114 static struct mbuf * 115 vtnet_rx_alloc_buf(struct vtnet_softc *, int , struct mbuf **); 116 static int vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *, 117 struct mbuf *, int); 118 static int vtnet_rxq_replace_buf(struct vtnet_rxq *, struct mbuf *, int); 119 static int vtnet_rxq_enqueue_buf(struct vtnet_rxq *, struct mbuf *); 120 static int vtnet_rxq_new_buf(struct vtnet_rxq *); 121 static int vtnet_rxq_csum(struct vtnet_rxq *, struct mbuf *, 122 struct virtio_net_hdr *); 123 static void vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *, int); 124 static void vtnet_rxq_discard_buf(struct vtnet_rxq *, struct mbuf *); 125 static int vtnet_rxq_merged_eof(struct vtnet_rxq *, struct mbuf *, int); 126 static void vtnet_rxq_input(struct vtnet_rxq *, struct mbuf *, 127 struct virtio_net_hdr *); 128 static int vtnet_rxq_eof(struct vtnet_rxq *); 129 static void vtnet_rx_vq_intr(void *); 130 static void vtnet_rxq_tq_intr(void *, int); 131 132 static int vtnet_txq_below_threshold(struct vtnet_txq *); 133 static int vtnet_txq_notify(struct vtnet_txq *); 134 static void vtnet_txq_free_mbufs(struct vtnet_txq *); 135 static int vtnet_txq_offload_ctx(struct vtnet_txq *, struct mbuf *, 136 int *, int *, int *); 137 static int vtnet_txq_offload_tso(struct vtnet_txq *, struct mbuf *, int, 138 int, struct virtio_net_hdr *); 139 static struct mbuf * 140 vtnet_txq_offload(struct vtnet_txq *, struct mbuf *, 141 struct virtio_net_hdr *); 142 static int vtnet_txq_enqueue_buf(struct vtnet_txq *, struct mbuf **, 143 struct vtnet_tx_header *); 144 static int vtnet_txq_encap(struct vtnet_txq *, struct mbuf **); 145 #ifdef VTNET_LEGACY_TX 146 static void vtnet_start_locked(struct vtnet_txq *, struct ifnet *); 147 static void vtnet_start(struct ifnet *); 148 #else 149 static int vtnet_txq_mq_start_locked(struct vtnet_txq *, struct mbuf *); 150 static int vtnet_txq_mq_start(struct ifnet *, struct mbuf *); 151 static void vtnet_txq_tq_deferred(void *, int); 152 #endif 153 static void vtnet_txq_start(struct vtnet_txq *); 154 static void vtnet_txq_tq_intr(void *, int); 155 static int vtnet_txq_eof(struct vtnet_txq *); 156 static void vtnet_tx_vq_intr(void *); 157 static void vtnet_tx_start_all(struct vtnet_softc *); 158 159 #ifndef VTNET_LEGACY_TX 160 static void vtnet_qflush(struct ifnet *); 161 #endif 162 163 static int vtnet_watchdog(struct vtnet_txq *); 164 static void vtnet_accum_stats(struct vtnet_softc *, 165 struct vtnet_rxq_stats *, struct vtnet_txq_stats *); 166 static void vtnet_tick(void *); 167 168 static void vtnet_start_taskqueues(struct vtnet_softc *); 169 static void vtnet_free_taskqueues(struct vtnet_softc *); 170 static void vtnet_drain_taskqueues(struct vtnet_softc *); 171 172 static void vtnet_drain_rxtx_queues(struct vtnet_softc *); 173 static void vtnet_stop_rendezvous(struct vtnet_softc *); 174 static void vtnet_stop(struct vtnet_softc *); 175 static int vtnet_virtio_reinit(struct vtnet_softc *); 176 static void vtnet_init_rx_filters(struct vtnet_softc *); 177 static int vtnet_init_rx_queues(struct vtnet_softc *); 178 static int vtnet_init_tx_queues(struct vtnet_softc *); 179 static int vtnet_init_rxtx_queues(struct vtnet_softc *); 180 static void vtnet_set_active_vq_pairs(struct vtnet_softc *); 181 static int vtnet_reinit(struct vtnet_softc *); 182 static void vtnet_init_locked(struct vtnet_softc *); 183 static void vtnet_init(void *); 184 185 static void vtnet_free_ctrl_vq(struct vtnet_softc *); 186 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *, 187 struct sglist *, int, int); 188 static int vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *); 189 static int vtnet_ctrl_mq_cmd(struct vtnet_softc *, uint16_t); 190 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int); 191 static int vtnet_set_promisc(struct vtnet_softc *, int); 192 static int vtnet_set_allmulti(struct vtnet_softc *, int); 193 static void vtnet_attach_disable_promisc(struct vtnet_softc *); 194 static void vtnet_rx_filter(struct vtnet_softc *); 195 static void vtnet_rx_filter_mac(struct vtnet_softc *); 196 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t); 197 static void vtnet_rx_filter_vlan(struct vtnet_softc *); 198 static void vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t); 199 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t); 200 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t); 201 202 static int vtnet_is_link_up(struct vtnet_softc *); 203 static void vtnet_update_link_status(struct vtnet_softc *); 204 static int vtnet_ifmedia_upd(struct ifnet *); 205 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *); 206 static void vtnet_get_hwaddr(struct vtnet_softc *); 207 static void vtnet_set_hwaddr(struct vtnet_softc *); 208 static void vtnet_vlan_tag_remove(struct mbuf *); 209 static void vtnet_set_rx_process_limit(struct vtnet_softc *); 210 static void vtnet_set_tx_intr_threshold(struct vtnet_softc *); 211 212 static void vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *, 213 struct sysctl_oid_list *, struct vtnet_rxq *); 214 static void vtnet_setup_txq_sysctl(struct sysctl_ctx_list *, 215 struct sysctl_oid_list *, struct vtnet_txq *); 216 static void vtnet_setup_queue_sysctl(struct vtnet_softc *); 217 static void vtnet_setup_sysctl(struct vtnet_softc *); 218 219 static int vtnet_rxq_enable_intr(struct vtnet_rxq *); 220 static void vtnet_rxq_disable_intr(struct vtnet_rxq *); 221 static int vtnet_txq_enable_intr(struct vtnet_txq *); 222 static void vtnet_txq_disable_intr(struct vtnet_txq *); 223 static void vtnet_enable_rx_interrupts(struct vtnet_softc *); 224 static void vtnet_enable_tx_interrupts(struct vtnet_softc *); 225 static void vtnet_enable_interrupts(struct vtnet_softc *); 226 static void vtnet_disable_rx_interrupts(struct vtnet_softc *); 227 static void vtnet_disable_tx_interrupts(struct vtnet_softc *); 228 static void vtnet_disable_interrupts(struct vtnet_softc *); 229 230 static int vtnet_tunable_int(struct vtnet_softc *, const char *, int); 231 232 /* Tunables. */ 233 static int vtnet_csum_disable = 0; 234 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable); 235 static int vtnet_tso_disable = 0; 236 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable); 237 static int vtnet_lro_disable = 0; 238 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable); 239 static int vtnet_mq_disable = 0; 240 TUNABLE_INT("hw.vtnet.mq_disable", &vtnet_mq_disable); 241 static int vtnet_mq_max_pairs = 0; 242 TUNABLE_INT("hw.vtnet.mq_max_pairs", &vtnet_mq_max_pairs); 243 static int vtnet_rx_process_limit = 512; 244 TUNABLE_INT("hw.vtnet.rx_process_limit", &vtnet_rx_process_limit); 245 246 static uma_zone_t vtnet_tx_header_zone; 247 248 static struct virtio_feature_desc vtnet_feature_desc[] = { 249 { VIRTIO_NET_F_CSUM, "TxChecksum" }, 250 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" }, 251 { VIRTIO_NET_F_MAC, "MacAddress" }, 252 { VIRTIO_NET_F_GSO, "TxAllGSO" }, 253 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" }, 254 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" }, 255 { VIRTIO_NET_F_GUEST_ECN, "RxECN" }, 256 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" }, 257 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" }, 258 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" }, 259 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" }, 260 { VIRTIO_NET_F_HOST_UFO, "TxUFO" }, 261 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" }, 262 { VIRTIO_NET_F_STATUS, "Status" }, 263 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" }, 264 { VIRTIO_NET_F_CTRL_RX, "RxMode" }, 265 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" }, 266 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" }, 267 { VIRTIO_NET_F_GUEST_ANNOUNCE, "GuestAnnounce" }, 268 { VIRTIO_NET_F_MQ, "Multiqueue" }, 269 { VIRTIO_NET_F_CTRL_MAC_ADDR, "SetMacAddress" }, 270 271 { 0, NULL } 272 }; 273 274 static device_method_t vtnet_methods[] = { 275 /* Device methods. */ 276 DEVMETHOD(device_probe, vtnet_probe), 277 DEVMETHOD(device_attach, vtnet_attach), 278 DEVMETHOD(device_detach, vtnet_detach), 279 DEVMETHOD(device_suspend, vtnet_suspend), 280 DEVMETHOD(device_resume, vtnet_resume), 281 DEVMETHOD(device_shutdown, vtnet_shutdown), 282 283 /* VirtIO methods. */ 284 DEVMETHOD(virtio_attach_completed, vtnet_attach_completed), 285 DEVMETHOD(virtio_config_change, vtnet_config_change), 286 287 DEVMETHOD_END 288 }; 289 290 #ifdef DEV_NETMAP 291 #include <dev/netmap/if_vtnet_netmap.h> 292 #endif /* DEV_NETMAP */ 293 294 static driver_t vtnet_driver = { 295 "vtnet", 296 vtnet_methods, 297 sizeof(struct vtnet_softc) 298 }; 299 static devclass_t vtnet_devclass; 300 301 DRIVER_MODULE(vtnet, virtio_mmio, vtnet_driver, vtnet_devclass, 302 vtnet_modevent, 0); 303 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass, 304 vtnet_modevent, 0); 305 MODULE_VERSION(vtnet, 1); 306 MODULE_DEPEND(vtnet, virtio, 1, 1, 1); 307 #ifdef DEV_NETMAP 308 MODULE_DEPEND(vtnet, netmap, 1, 1, 1); 309 #endif /* DEV_NETMAP */ 310 311 static int 312 vtnet_modevent(module_t mod, int type, void *unused) 313 { 314 int error; 315 316 error = 0; 317 318 switch (type) { 319 case MOD_LOAD: 320 vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr", 321 sizeof(struct vtnet_tx_header), 322 NULL, NULL, NULL, NULL, 0, 0); 323 break; 324 case MOD_QUIESCE: 325 case MOD_UNLOAD: 326 if (uma_zone_get_cur(vtnet_tx_header_zone) > 0) 327 error = EBUSY; 328 else if (type == MOD_UNLOAD) { 329 uma_zdestroy(vtnet_tx_header_zone); 330 vtnet_tx_header_zone = NULL; 331 } 332 break; 333 case MOD_SHUTDOWN: 334 break; 335 default: 336 error = EOPNOTSUPP; 337 break; 338 } 339 340 return (error); 341 } 342 343 static int 344 vtnet_probe(device_t dev) 345 { 346 347 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK) 348 return (ENXIO); 349 350 device_set_desc(dev, "VirtIO Networking Adapter"); 351 352 return (BUS_PROBE_DEFAULT); 353 } 354 355 static int 356 vtnet_attach(device_t dev) 357 { 358 struct vtnet_softc *sc; 359 int error; 360 361 sc = device_get_softc(dev); 362 sc->vtnet_dev = dev; 363 364 /* Register our feature descriptions. */ 365 virtio_set_feature_desc(dev, vtnet_feature_desc); 366 367 VTNET_CORE_LOCK_INIT(sc); 368 callout_init_mtx(&sc->vtnet_tick_ch, VTNET_CORE_MTX(sc), 0); 369 370 vtnet_setup_sysctl(sc); 371 vtnet_setup_features(sc); 372 373 error = vtnet_alloc_rx_filters(sc); 374 if (error) { 375 device_printf(dev, "cannot allocate Rx filters\n"); 376 goto fail; 377 } 378 379 error = vtnet_alloc_rxtx_queues(sc); 380 if (error) { 381 device_printf(dev, "cannot allocate queues\n"); 382 goto fail; 383 } 384 385 error = vtnet_alloc_virtqueues(sc); 386 if (error) { 387 device_printf(dev, "cannot allocate virtqueues\n"); 388 goto fail; 389 } 390 391 error = vtnet_setup_interface(sc); 392 if (error) { 393 device_printf(dev, "cannot setup interface\n"); 394 goto fail; 395 } 396 397 error = virtio_setup_intr(dev, INTR_TYPE_NET); 398 if (error) { 399 device_printf(dev, "cannot setup virtqueue interrupts\n"); 400 /* BMV: This will crash if during boot! */ 401 ether_ifdetach(sc->vtnet_ifp); 402 goto fail; 403 } 404 405 #ifdef DEV_NETMAP 406 vtnet_netmap_attach(sc); 407 #endif /* DEV_NETMAP */ 408 409 vtnet_start_taskqueues(sc); 410 411 fail: 412 if (error) 413 vtnet_detach(dev); 414 415 return (error); 416 } 417 418 static int 419 vtnet_detach(device_t dev) 420 { 421 struct vtnet_softc *sc; 422 struct ifnet *ifp; 423 424 sc = device_get_softc(dev); 425 ifp = sc->vtnet_ifp; 426 427 if (device_is_attached(dev)) { 428 VTNET_CORE_LOCK(sc); 429 vtnet_stop(sc); 430 VTNET_CORE_UNLOCK(sc); 431 432 callout_drain(&sc->vtnet_tick_ch); 433 vtnet_drain_taskqueues(sc); 434 435 ether_ifdetach(ifp); 436 } 437 438 #ifdef DEV_NETMAP 439 netmap_detach(ifp); 440 #endif /* DEV_NETMAP */ 441 442 vtnet_free_taskqueues(sc); 443 444 if (sc->vtnet_vlan_attach != NULL) { 445 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach); 446 sc->vtnet_vlan_attach = NULL; 447 } 448 if (sc->vtnet_vlan_detach != NULL) { 449 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach); 450 sc->vtnet_vlan_detach = NULL; 451 } 452 453 ifmedia_removeall(&sc->vtnet_media); 454 455 if (ifp != NULL) { 456 if_free(ifp); 457 sc->vtnet_ifp = NULL; 458 } 459 460 vtnet_free_rxtx_queues(sc); 461 vtnet_free_rx_filters(sc); 462 463 if (sc->vtnet_ctrl_vq != NULL) 464 vtnet_free_ctrl_vq(sc); 465 466 VTNET_CORE_LOCK_DESTROY(sc); 467 468 return (0); 469 } 470 471 static int 472 vtnet_suspend(device_t dev) 473 { 474 struct vtnet_softc *sc; 475 476 sc = device_get_softc(dev); 477 478 VTNET_CORE_LOCK(sc); 479 vtnet_stop(sc); 480 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED; 481 VTNET_CORE_UNLOCK(sc); 482 483 return (0); 484 } 485 486 static int 487 vtnet_resume(device_t dev) 488 { 489 struct vtnet_softc *sc; 490 struct ifnet *ifp; 491 492 sc = device_get_softc(dev); 493 ifp = sc->vtnet_ifp; 494 495 VTNET_CORE_LOCK(sc); 496 if (ifp->if_flags & IFF_UP) 497 vtnet_init_locked(sc); 498 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED; 499 VTNET_CORE_UNLOCK(sc); 500 501 return (0); 502 } 503 504 static int 505 vtnet_shutdown(device_t dev) 506 { 507 508 /* 509 * Suspend already does all of what we need to 510 * do here; we just never expect to be resumed. 511 */ 512 return (vtnet_suspend(dev)); 513 } 514 515 static int 516 vtnet_attach_completed(device_t dev) 517 { 518 519 vtnet_attach_disable_promisc(device_get_softc(dev)); 520 521 return (0); 522 } 523 524 static int 525 vtnet_config_change(device_t dev) 526 { 527 struct vtnet_softc *sc; 528 529 sc = device_get_softc(dev); 530 531 VTNET_CORE_LOCK(sc); 532 vtnet_update_link_status(sc); 533 if (sc->vtnet_link_active != 0) 534 vtnet_tx_start_all(sc); 535 VTNET_CORE_UNLOCK(sc); 536 537 return (0); 538 } 539 540 static void 541 vtnet_negotiate_features(struct vtnet_softc *sc) 542 { 543 device_t dev; 544 uint64_t mask, features; 545 546 dev = sc->vtnet_dev; 547 mask = 0; 548 549 /* 550 * TSO and LRO are only available when their corresponding checksum 551 * offload feature is also negotiated. 552 */ 553 if (vtnet_tunable_int(sc, "csum_disable", vtnet_csum_disable)) { 554 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM; 555 mask |= VTNET_TSO_FEATURES | VTNET_LRO_FEATURES; 556 } 557 if (vtnet_tunable_int(sc, "tso_disable", vtnet_tso_disable)) 558 mask |= VTNET_TSO_FEATURES; 559 if (vtnet_tunable_int(sc, "lro_disable", vtnet_lro_disable)) 560 mask |= VTNET_LRO_FEATURES; 561 #ifndef VTNET_LEGACY_TX 562 if (vtnet_tunable_int(sc, "mq_disable", vtnet_mq_disable)) 563 mask |= VIRTIO_NET_F_MQ; 564 #else 565 mask |= VIRTIO_NET_F_MQ; 566 #endif 567 568 features = VTNET_FEATURES & ~mask; 569 sc->vtnet_features = virtio_negotiate_features(dev, features); 570 571 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) && 572 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) { 573 /* 574 * LRO without mergeable buffers requires special care. This 575 * is not ideal because every receive buffer must be large 576 * enough to hold the maximum TCP packet, the Ethernet header, 577 * and the header. This requires up to 34 descriptors with 578 * MCLBYTES clusters. If we do not have indirect descriptors, 579 * LRO is disabled since the virtqueue will not contain very 580 * many receive buffers. 581 */ 582 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) { 583 device_printf(dev, 584 "LRO disabled due to both mergeable buffers and " 585 "indirect descriptors not negotiated\n"); 586 587 features &= ~VTNET_LRO_FEATURES; 588 sc->vtnet_features = 589 virtio_negotiate_features(dev, features); 590 } else 591 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG; 592 } 593 } 594 595 static void 596 vtnet_setup_features(struct vtnet_softc *sc) 597 { 598 device_t dev; 599 int max_pairs, max; 600 601 dev = sc->vtnet_dev; 602 603 vtnet_negotiate_features(sc); 604 605 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) 606 sc->vtnet_flags |= VTNET_FLAG_INDIRECT; 607 if (virtio_with_feature(dev, VIRTIO_RING_F_EVENT_IDX)) 608 sc->vtnet_flags |= VTNET_FLAG_EVENT_IDX; 609 610 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) { 611 /* This feature should always be negotiated. */ 612 sc->vtnet_flags |= VTNET_FLAG_MAC; 613 } 614 615 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) { 616 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS; 617 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf); 618 } else 619 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr); 620 621 if (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) 622 sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS; 623 else if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) 624 sc->vtnet_rx_nsegs = VTNET_MAX_RX_SEGS; 625 else 626 sc->vtnet_rx_nsegs = VTNET_MIN_RX_SEGS; 627 628 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO) || 629 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) || 630 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6)) 631 sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS; 632 else 633 sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS; 634 635 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) { 636 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ; 637 638 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX)) 639 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX; 640 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN)) 641 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER; 642 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR)) 643 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC; 644 } 645 646 if (virtio_with_feature(dev, VIRTIO_NET_F_MQ) && 647 sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) { 648 max_pairs = virtio_read_dev_config_2(dev, 649 offsetof(struct virtio_net_config, max_virtqueue_pairs)); 650 if (max_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || 651 max_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX) 652 max_pairs = 1; 653 } else 654 max_pairs = 1; 655 656 if (max_pairs > 1) { 657 /* 658 * Limit the maximum number of queue pairs to the number of 659 * CPUs or the configured maximum. The actual number of 660 * queues that get used may be less. 661 */ 662 max = vtnet_tunable_int(sc, "mq_max_pairs", vtnet_mq_max_pairs); 663 if (max > 0 && max_pairs > max) 664 max_pairs = max; 665 if (max_pairs > mp_ncpus) 666 max_pairs = mp_ncpus; 667 if (max_pairs > VTNET_MAX_QUEUE_PAIRS) 668 max_pairs = VTNET_MAX_QUEUE_PAIRS; 669 if (max_pairs > 1) 670 sc->vtnet_flags |= VTNET_FLAG_MULTIQ; 671 } 672 673 sc->vtnet_max_vq_pairs = max_pairs; 674 } 675 676 static int 677 vtnet_init_rxq(struct vtnet_softc *sc, int id) 678 { 679 struct vtnet_rxq *rxq; 680 681 rxq = &sc->vtnet_rxqs[id]; 682 683 snprintf(rxq->vtnrx_name, sizeof(rxq->vtnrx_name), "%s-rx%d", 684 device_get_nameunit(sc->vtnet_dev), id); 685 mtx_init(&rxq->vtnrx_mtx, rxq->vtnrx_name, NULL, MTX_DEF); 686 687 rxq->vtnrx_sc = sc; 688 rxq->vtnrx_id = id; 689 690 rxq->vtnrx_sg = sglist_alloc(sc->vtnet_rx_nsegs, M_NOWAIT); 691 if (rxq->vtnrx_sg == NULL) 692 return (ENOMEM); 693 694 TASK_INIT(&rxq->vtnrx_intrtask, 0, vtnet_rxq_tq_intr, rxq); 695 rxq->vtnrx_tq = taskqueue_create(rxq->vtnrx_name, M_NOWAIT, 696 taskqueue_thread_enqueue, &rxq->vtnrx_tq); 697 698 return (rxq->vtnrx_tq == NULL ? ENOMEM : 0); 699 } 700 701 static int 702 vtnet_init_txq(struct vtnet_softc *sc, int id) 703 { 704 struct vtnet_txq *txq; 705 706 txq = &sc->vtnet_txqs[id]; 707 708 snprintf(txq->vtntx_name, sizeof(txq->vtntx_name), "%s-tx%d", 709 device_get_nameunit(sc->vtnet_dev), id); 710 mtx_init(&txq->vtntx_mtx, txq->vtntx_name, NULL, MTX_DEF); 711 712 txq->vtntx_sc = sc; 713 txq->vtntx_id = id; 714 715 txq->vtntx_sg = sglist_alloc(sc->vtnet_tx_nsegs, M_NOWAIT); 716 if (txq->vtntx_sg == NULL) 717 return (ENOMEM); 718 719 #ifndef VTNET_LEGACY_TX 720 txq->vtntx_br = buf_ring_alloc(VTNET_DEFAULT_BUFRING_SIZE, M_DEVBUF, 721 M_NOWAIT, &txq->vtntx_mtx); 722 if (txq->vtntx_br == NULL) 723 return (ENOMEM); 724 725 TASK_INIT(&txq->vtntx_defrtask, 0, vtnet_txq_tq_deferred, txq); 726 #endif 727 TASK_INIT(&txq->vtntx_intrtask, 0, vtnet_txq_tq_intr, txq); 728 txq->vtntx_tq = taskqueue_create(txq->vtntx_name, M_NOWAIT, 729 taskqueue_thread_enqueue, &txq->vtntx_tq); 730 if (txq->vtntx_tq == NULL) 731 return (ENOMEM); 732 733 return (0); 734 } 735 736 static int 737 vtnet_alloc_rxtx_queues(struct vtnet_softc *sc) 738 { 739 int i, npairs, error; 740 741 npairs = sc->vtnet_max_vq_pairs; 742 743 sc->vtnet_rxqs = malloc(sizeof(struct vtnet_rxq) * npairs, M_DEVBUF, 744 M_NOWAIT | M_ZERO); 745 sc->vtnet_txqs = malloc(sizeof(struct vtnet_txq) * npairs, M_DEVBUF, 746 M_NOWAIT | M_ZERO); 747 if (sc->vtnet_rxqs == NULL || sc->vtnet_txqs == NULL) 748 return (ENOMEM); 749 750 for (i = 0; i < npairs; i++) { 751 error = vtnet_init_rxq(sc, i); 752 if (error) 753 return (error); 754 error = vtnet_init_txq(sc, i); 755 if (error) 756 return (error); 757 } 758 759 vtnet_setup_queue_sysctl(sc); 760 761 return (0); 762 } 763 764 static void 765 vtnet_destroy_rxq(struct vtnet_rxq *rxq) 766 { 767 768 rxq->vtnrx_sc = NULL; 769 rxq->vtnrx_id = -1; 770 771 if (rxq->vtnrx_sg != NULL) { 772 sglist_free(rxq->vtnrx_sg); 773 rxq->vtnrx_sg = NULL; 774 } 775 776 if (mtx_initialized(&rxq->vtnrx_mtx) != 0) 777 mtx_destroy(&rxq->vtnrx_mtx); 778 } 779 780 static void 781 vtnet_destroy_txq(struct vtnet_txq *txq) 782 { 783 784 txq->vtntx_sc = NULL; 785 txq->vtntx_id = -1; 786 787 if (txq->vtntx_sg != NULL) { 788 sglist_free(txq->vtntx_sg); 789 txq->vtntx_sg = NULL; 790 } 791 792 #ifndef VTNET_LEGACY_TX 793 if (txq->vtntx_br != NULL) { 794 buf_ring_free(txq->vtntx_br, M_DEVBUF); 795 txq->vtntx_br = NULL; 796 } 797 #endif 798 799 if (mtx_initialized(&txq->vtntx_mtx) != 0) 800 mtx_destroy(&txq->vtntx_mtx); 801 } 802 803 static void 804 vtnet_free_rxtx_queues(struct vtnet_softc *sc) 805 { 806 int i; 807 808 if (sc->vtnet_rxqs != NULL) { 809 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) 810 vtnet_destroy_rxq(&sc->vtnet_rxqs[i]); 811 free(sc->vtnet_rxqs, M_DEVBUF); 812 sc->vtnet_rxqs = NULL; 813 } 814 815 if (sc->vtnet_txqs != NULL) { 816 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) 817 vtnet_destroy_txq(&sc->vtnet_txqs[i]); 818 free(sc->vtnet_txqs, M_DEVBUF); 819 sc->vtnet_txqs = NULL; 820 } 821 } 822 823 static int 824 vtnet_alloc_rx_filters(struct vtnet_softc *sc) 825 { 826 827 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) { 828 sc->vtnet_mac_filter = malloc(sizeof(struct vtnet_mac_filter), 829 M_DEVBUF, M_NOWAIT | M_ZERO); 830 if (sc->vtnet_mac_filter == NULL) 831 return (ENOMEM); 832 } 833 834 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) { 835 sc->vtnet_vlan_filter = malloc(sizeof(uint32_t) * 836 VTNET_VLAN_FILTER_NWORDS, M_DEVBUF, M_NOWAIT | M_ZERO); 837 if (sc->vtnet_vlan_filter == NULL) 838 return (ENOMEM); 839 } 840 841 return (0); 842 } 843 844 static void 845 vtnet_free_rx_filters(struct vtnet_softc *sc) 846 { 847 848 if (sc->vtnet_mac_filter != NULL) { 849 free(sc->vtnet_mac_filter, M_DEVBUF); 850 sc->vtnet_mac_filter = NULL; 851 } 852 853 if (sc->vtnet_vlan_filter != NULL) { 854 free(sc->vtnet_vlan_filter, M_DEVBUF); 855 sc->vtnet_vlan_filter = NULL; 856 } 857 } 858 859 static int 860 vtnet_alloc_virtqueues(struct vtnet_softc *sc) 861 { 862 device_t dev; 863 struct vq_alloc_info *info; 864 struct vtnet_rxq *rxq; 865 struct vtnet_txq *txq; 866 int i, idx, flags, nvqs, error; 867 868 dev = sc->vtnet_dev; 869 flags = 0; 870 871 nvqs = sc->vtnet_max_vq_pairs * 2; 872 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) 873 nvqs++; 874 875 info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT); 876 if (info == NULL) 877 return (ENOMEM); 878 879 for (i = 0, idx = 0; i < sc->vtnet_max_vq_pairs; i++, idx+=2) { 880 rxq = &sc->vtnet_rxqs[i]; 881 VQ_ALLOC_INFO_INIT(&info[idx], sc->vtnet_rx_nsegs, 882 vtnet_rx_vq_intr, rxq, &rxq->vtnrx_vq, 883 "%s-%d rx", device_get_nameunit(dev), rxq->vtnrx_id); 884 885 txq = &sc->vtnet_txqs[i]; 886 VQ_ALLOC_INFO_INIT(&info[idx+1], sc->vtnet_tx_nsegs, 887 vtnet_tx_vq_intr, txq, &txq->vtntx_vq, 888 "%s-%d tx", device_get_nameunit(dev), txq->vtntx_id); 889 } 890 891 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) { 892 VQ_ALLOC_INFO_INIT(&info[idx], 0, NULL, NULL, 893 &sc->vtnet_ctrl_vq, "%s ctrl", device_get_nameunit(dev)); 894 } 895 896 /* 897 * Enable interrupt binding if this is multiqueue. This only matters 898 * when per-vq MSIX is available. 899 */ 900 if (sc->vtnet_flags & VTNET_FLAG_MULTIQ) 901 flags |= 0; 902 903 error = virtio_alloc_virtqueues(dev, flags, nvqs, info); 904 free(info, M_TEMP); 905 906 return (error); 907 } 908 909 static int 910 vtnet_setup_interface(struct vtnet_softc *sc) 911 { 912 device_t dev; 913 struct ifnet *ifp; 914 915 dev = sc->vtnet_dev; 916 917 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER); 918 if (ifp == NULL) { 919 device_printf(dev, "cannot allocate ifnet structure\n"); 920 return (ENOSPC); 921 } 922 923 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 924 ifp->if_baudrate = IF_Gbps(10); /* Approx. */ 925 ifp->if_softc = sc; 926 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 927 ifp->if_init = vtnet_init; 928 ifp->if_ioctl = vtnet_ioctl; 929 ifp->if_get_counter = vtnet_get_counter; 930 #ifndef VTNET_LEGACY_TX 931 ifp->if_transmit = vtnet_txq_mq_start; 932 ifp->if_qflush = vtnet_qflush; 933 #else 934 struct virtqueue *vq = sc->vtnet_txqs[0].vtntx_vq; 935 ifp->if_start = vtnet_start; 936 IFQ_SET_MAXLEN(&ifp->if_snd, virtqueue_size(vq) - 1); 937 ifp->if_snd.ifq_drv_maxlen = virtqueue_size(vq) - 1; 938 IFQ_SET_READY(&ifp->if_snd); 939 #endif 940 941 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd, 942 vtnet_ifmedia_sts); 943 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL); 944 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE); 945 946 /* Read (or generate) the MAC address for the adapter. */ 947 vtnet_get_hwaddr(sc); 948 949 ether_ifattach(ifp, sc->vtnet_hwaddr); 950 951 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) 952 ifp->if_capabilities |= IFCAP_LINKSTATE; 953 954 /* Tell the upper layer(s) we support long frames. */ 955 ifp->if_hdrlen = sizeof(struct ether_vlan_header); 956 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU; 957 958 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) { 959 ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6; 960 961 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO)) { 962 ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6; 963 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN; 964 } else { 965 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4)) 966 ifp->if_capabilities |= IFCAP_TSO4; 967 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6)) 968 ifp->if_capabilities |= IFCAP_TSO6; 969 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN)) 970 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN; 971 } 972 973 if (ifp->if_capabilities & IFCAP_TSO) 974 ifp->if_capabilities |= IFCAP_VLAN_HWTSO; 975 } 976 977 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) { 978 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6; 979 980 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) || 981 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6)) 982 ifp->if_capabilities |= IFCAP_LRO; 983 } 984 985 if (ifp->if_capabilities & IFCAP_HWCSUM) { 986 /* 987 * VirtIO does not support VLAN tagging, but we can fake 988 * it by inserting and removing the 802.1Q header during 989 * transmit and receive. We are then able to do checksum 990 * offloading of VLAN frames. 991 */ 992 ifp->if_capabilities |= 993 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM; 994 } 995 996 ifp->if_capenable = ifp->if_capabilities; 997 998 /* 999 * Capabilities after here are not enabled by default. 1000 */ 1001 1002 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) { 1003 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER; 1004 1005 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config, 1006 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST); 1007 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig, 1008 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST); 1009 } 1010 1011 vtnet_set_rx_process_limit(sc); 1012 vtnet_set_tx_intr_threshold(sc); 1013 1014 return (0); 1015 } 1016 1017 static int 1018 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu) 1019 { 1020 struct ifnet *ifp; 1021 int frame_size, clsize; 1022 1023 ifp = sc->vtnet_ifp; 1024 1025 if (new_mtu < ETHERMIN || new_mtu > VTNET_MAX_MTU) 1026 return (EINVAL); 1027 1028 frame_size = sc->vtnet_hdr_size + sizeof(struct ether_vlan_header) + 1029 new_mtu; 1030 1031 /* 1032 * Based on the new MTU (and hence frame size) determine which 1033 * cluster size is most appropriate for the receive queues. 1034 */ 1035 if (frame_size <= MCLBYTES) { 1036 clsize = MCLBYTES; 1037 } else if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 1038 /* Avoid going past 9K jumbos. */ 1039 if (frame_size > MJUM9BYTES) 1040 return (EINVAL); 1041 clsize = MJUM9BYTES; 1042 } else 1043 clsize = MJUMPAGESIZE; 1044 1045 ifp->if_mtu = new_mtu; 1046 sc->vtnet_rx_new_clsize = clsize; 1047 1048 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1049 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1050 vtnet_init_locked(sc); 1051 } 1052 1053 return (0); 1054 } 1055 1056 static int 1057 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1058 { 1059 struct vtnet_softc *sc; 1060 struct ifreq *ifr; 1061 int reinit, mask, error; 1062 1063 sc = ifp->if_softc; 1064 ifr = (struct ifreq *) data; 1065 error = 0; 1066 1067 switch (cmd) { 1068 case SIOCSIFMTU: 1069 if (ifp->if_mtu != ifr->ifr_mtu) { 1070 VTNET_CORE_LOCK(sc); 1071 error = vtnet_change_mtu(sc, ifr->ifr_mtu); 1072 VTNET_CORE_UNLOCK(sc); 1073 } 1074 break; 1075 1076 case SIOCSIFFLAGS: 1077 VTNET_CORE_LOCK(sc); 1078 if ((ifp->if_flags & IFF_UP) == 0) { 1079 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1080 vtnet_stop(sc); 1081 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1082 if ((ifp->if_flags ^ sc->vtnet_if_flags) & 1083 (IFF_PROMISC | IFF_ALLMULTI)) { 1084 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) 1085 vtnet_rx_filter(sc); 1086 else { 1087 ifp->if_flags |= IFF_PROMISC; 1088 if ((ifp->if_flags ^ sc->vtnet_if_flags) 1089 & IFF_ALLMULTI) 1090 error = ENOTSUP; 1091 } 1092 } 1093 } else 1094 vtnet_init_locked(sc); 1095 1096 if (error == 0) 1097 sc->vtnet_if_flags = ifp->if_flags; 1098 VTNET_CORE_UNLOCK(sc); 1099 break; 1100 1101 case SIOCADDMULTI: 1102 case SIOCDELMULTI: 1103 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) 1104 break; 1105 VTNET_CORE_LOCK(sc); 1106 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1107 vtnet_rx_filter_mac(sc); 1108 VTNET_CORE_UNLOCK(sc); 1109 break; 1110 1111 case SIOCSIFMEDIA: 1112 case SIOCGIFMEDIA: 1113 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd); 1114 break; 1115 1116 case SIOCSIFCAP: 1117 VTNET_CORE_LOCK(sc); 1118 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 1119 1120 if (mask & IFCAP_TXCSUM) 1121 ifp->if_capenable ^= IFCAP_TXCSUM; 1122 if (mask & IFCAP_TXCSUM_IPV6) 1123 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6; 1124 if (mask & IFCAP_TSO4) 1125 ifp->if_capenable ^= IFCAP_TSO4; 1126 if (mask & IFCAP_TSO6) 1127 ifp->if_capenable ^= IFCAP_TSO6; 1128 1129 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO | 1130 IFCAP_VLAN_HWFILTER)) { 1131 /* These Rx features require us to renegotiate. */ 1132 reinit = 1; 1133 1134 if (mask & IFCAP_RXCSUM) 1135 ifp->if_capenable ^= IFCAP_RXCSUM; 1136 if (mask & IFCAP_RXCSUM_IPV6) 1137 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6; 1138 if (mask & IFCAP_LRO) 1139 ifp->if_capenable ^= IFCAP_LRO; 1140 if (mask & IFCAP_VLAN_HWFILTER) 1141 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER; 1142 } else 1143 reinit = 0; 1144 1145 if (mask & IFCAP_VLAN_HWTSO) 1146 ifp->if_capenable ^= IFCAP_VLAN_HWTSO; 1147 if (mask & IFCAP_VLAN_HWTAGGING) 1148 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 1149 1150 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1151 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1152 vtnet_init_locked(sc); 1153 } 1154 1155 VTNET_CORE_UNLOCK(sc); 1156 VLAN_CAPABILITIES(ifp); 1157 1158 break; 1159 1160 default: 1161 error = ether_ioctl(ifp, cmd, data); 1162 break; 1163 } 1164 1165 VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc); 1166 1167 return (error); 1168 } 1169 1170 static int 1171 vtnet_rxq_populate(struct vtnet_rxq *rxq) 1172 { 1173 struct virtqueue *vq; 1174 int nbufs, error; 1175 1176 vq = rxq->vtnrx_vq; 1177 error = ENOSPC; 1178 1179 for (nbufs = 0; !virtqueue_full(vq); nbufs++) { 1180 error = vtnet_rxq_new_buf(rxq); 1181 if (error) 1182 break; 1183 } 1184 1185 if (nbufs > 0) { 1186 virtqueue_notify(vq); 1187 /* 1188 * EMSGSIZE signifies the virtqueue did not have enough 1189 * entries available to hold the last mbuf. This is not 1190 * an error. 1191 */ 1192 if (error == EMSGSIZE) 1193 error = 0; 1194 } 1195 1196 return (error); 1197 } 1198 1199 static void 1200 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq) 1201 { 1202 struct virtqueue *vq; 1203 struct mbuf *m; 1204 int last; 1205 1206 vq = rxq->vtnrx_vq; 1207 last = 0; 1208 1209 while ((m = virtqueue_drain(vq, &last)) != NULL) 1210 m_freem(m); 1211 1212 KASSERT(virtqueue_empty(vq), 1213 ("%s: mbufs remaining in rx queue %p", __func__, rxq)); 1214 } 1215 1216 static struct mbuf * 1217 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp) 1218 { 1219 struct mbuf *m_head, *m_tail, *m; 1220 int i, clsize; 1221 1222 clsize = sc->vtnet_rx_clsize; 1223 1224 KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG, 1225 ("%s: chained mbuf %d request without LRO_NOMRG", __func__, nbufs)); 1226 1227 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize); 1228 if (m_head == NULL) 1229 goto fail; 1230 1231 m_head->m_len = clsize; 1232 m_tail = m_head; 1233 1234 /* Allocate the rest of the chain. */ 1235 for (i = 1; i < nbufs; i++) { 1236 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize); 1237 if (m == NULL) 1238 goto fail; 1239 1240 m->m_len = clsize; 1241 m_tail->m_next = m; 1242 m_tail = m; 1243 } 1244 1245 if (m_tailp != NULL) 1246 *m_tailp = m_tail; 1247 1248 return (m_head); 1249 1250 fail: 1251 sc->vtnet_stats.mbuf_alloc_failed++; 1252 m_freem(m_head); 1253 1254 return (NULL); 1255 } 1256 1257 /* 1258 * Slow path for when LRO without mergeable buffers is negotiated. 1259 */ 1260 static int 1261 vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *rxq, struct mbuf *m0, 1262 int len0) 1263 { 1264 struct vtnet_softc *sc; 1265 struct mbuf *m, *m_prev; 1266 struct mbuf *m_new, *m_tail; 1267 int len, clsize, nreplace, error; 1268 1269 sc = rxq->vtnrx_sc; 1270 clsize = sc->vtnet_rx_clsize; 1271 1272 m_prev = NULL; 1273 m_tail = NULL; 1274 nreplace = 0; 1275 1276 m = m0; 1277 len = len0; 1278 1279 /* 1280 * Since these mbuf chains are so large, we avoid allocating an 1281 * entire replacement chain if possible. When the received frame 1282 * did not consume the entire chain, the unused mbufs are moved 1283 * to the replacement chain. 1284 */ 1285 while (len > 0) { 1286 /* 1287 * Something is seriously wrong if we received a frame 1288 * larger than the chain. Drop it. 1289 */ 1290 if (m == NULL) { 1291 sc->vtnet_stats.rx_frame_too_large++; 1292 return (EMSGSIZE); 1293 } 1294 1295 /* We always allocate the same cluster size. */ 1296 KASSERT(m->m_len == clsize, 1297 ("%s: mbuf size %d is not the cluster size %d", 1298 __func__, m->m_len, clsize)); 1299 1300 m->m_len = MIN(m->m_len, len); 1301 len -= m->m_len; 1302 1303 m_prev = m; 1304 m = m->m_next; 1305 nreplace++; 1306 } 1307 1308 KASSERT(nreplace <= sc->vtnet_rx_nmbufs, 1309 ("%s: too many replacement mbufs %d max %d", __func__, nreplace, 1310 sc->vtnet_rx_nmbufs)); 1311 1312 m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail); 1313 if (m_new == NULL) { 1314 m_prev->m_len = clsize; 1315 return (ENOBUFS); 1316 } 1317 1318 /* 1319 * Move any unused mbufs from the received chain onto the end 1320 * of the new chain. 1321 */ 1322 if (m_prev->m_next != NULL) { 1323 m_tail->m_next = m_prev->m_next; 1324 m_prev->m_next = NULL; 1325 } 1326 1327 error = vtnet_rxq_enqueue_buf(rxq, m_new); 1328 if (error) { 1329 /* 1330 * BAD! We could not enqueue the replacement mbuf chain. We 1331 * must restore the m0 chain to the original state if it was 1332 * modified so we can subsequently discard it. 1333 * 1334 * NOTE: The replacement is suppose to be an identical copy 1335 * to the one just dequeued so this is an unexpected error. 1336 */ 1337 sc->vtnet_stats.rx_enq_replacement_failed++; 1338 1339 if (m_tail->m_next != NULL) { 1340 m_prev->m_next = m_tail->m_next; 1341 m_tail->m_next = NULL; 1342 } 1343 1344 m_prev->m_len = clsize; 1345 m_freem(m_new); 1346 } 1347 1348 return (error); 1349 } 1350 1351 static int 1352 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len) 1353 { 1354 struct vtnet_softc *sc; 1355 struct mbuf *m_new; 1356 int error; 1357 1358 sc = rxq->vtnrx_sc; 1359 1360 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL, 1361 ("%s: chained mbuf without LRO_NOMRG", __func__)); 1362 1363 if (m->m_next == NULL) { 1364 /* Fast-path for the common case of just one mbuf. */ 1365 if (m->m_len < len) 1366 return (EINVAL); 1367 1368 m_new = vtnet_rx_alloc_buf(sc, 1, NULL); 1369 if (m_new == NULL) 1370 return (ENOBUFS); 1371 1372 error = vtnet_rxq_enqueue_buf(rxq, m_new); 1373 if (error) { 1374 /* 1375 * The new mbuf is suppose to be an identical 1376 * copy of the one just dequeued so this is an 1377 * unexpected error. 1378 */ 1379 m_freem(m_new); 1380 sc->vtnet_stats.rx_enq_replacement_failed++; 1381 } else 1382 m->m_len = len; 1383 } else 1384 error = vtnet_rxq_replace_lro_nomgr_buf(rxq, m, len); 1385 1386 return (error); 1387 } 1388 1389 static int 1390 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m) 1391 { 1392 struct vtnet_softc *sc; 1393 struct sglist *sg; 1394 struct vtnet_rx_header *rxhdr; 1395 uint8_t *mdata; 1396 int offset, error; 1397 1398 sc = rxq->vtnrx_sc; 1399 sg = rxq->vtnrx_sg; 1400 mdata = mtod(m, uint8_t *); 1401 1402 VTNET_RXQ_LOCK_ASSERT(rxq); 1403 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL, 1404 ("%s: chained mbuf without LRO_NOMRG", __func__)); 1405 KASSERT(m->m_len == sc->vtnet_rx_clsize, 1406 ("%s: unexpected cluster size %d/%d", __func__, m->m_len, 1407 sc->vtnet_rx_clsize)); 1408 1409 sglist_reset(sg); 1410 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 1411 MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr)); 1412 rxhdr = (struct vtnet_rx_header *) mdata; 1413 sglist_append(sg, &rxhdr->vrh_hdr, sc->vtnet_hdr_size); 1414 offset = sizeof(struct vtnet_rx_header); 1415 } else 1416 offset = 0; 1417 1418 sglist_append(sg, mdata + offset, m->m_len - offset); 1419 if (m->m_next != NULL) { 1420 error = sglist_append_mbuf(sg, m->m_next); 1421 MPASS(error == 0); 1422 } 1423 1424 error = virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg); 1425 1426 return (error); 1427 } 1428 1429 static int 1430 vtnet_rxq_new_buf(struct vtnet_rxq *rxq) 1431 { 1432 struct vtnet_softc *sc; 1433 struct mbuf *m; 1434 int error; 1435 1436 sc = rxq->vtnrx_sc; 1437 1438 m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL); 1439 if (m == NULL) 1440 return (ENOBUFS); 1441 1442 error = vtnet_rxq_enqueue_buf(rxq, m); 1443 if (error) 1444 m_freem(m); 1445 1446 return (error); 1447 } 1448 1449 /* 1450 * Use the checksum offset in the VirtIO header to set the 1451 * correct CSUM_* flags. 1452 */ 1453 static int 1454 vtnet_rxq_csum_by_offset(struct vtnet_rxq *rxq, struct mbuf *m, 1455 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr) 1456 { 1457 struct vtnet_softc *sc; 1458 #if defined(INET) || defined(INET6) 1459 int offset = hdr->csum_start + hdr->csum_offset; 1460 #endif 1461 1462 sc = rxq->vtnrx_sc; 1463 1464 /* Only do a basic sanity check on the offset. */ 1465 switch (eth_type) { 1466 #if defined(INET) 1467 case ETHERTYPE_IP: 1468 if (__predict_false(offset < ip_start + sizeof(struct ip))) 1469 return (1); 1470 break; 1471 #endif 1472 #if defined(INET6) 1473 case ETHERTYPE_IPV6: 1474 if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr))) 1475 return (1); 1476 break; 1477 #endif 1478 default: 1479 sc->vtnet_stats.rx_csum_bad_ethtype++; 1480 return (1); 1481 } 1482 1483 /* 1484 * Use the offset to determine the appropriate CSUM_* flags. This is 1485 * a bit dirty, but we can get by with it since the checksum offsets 1486 * happen to be different. We assume the host host does not do IPv4 1487 * header checksum offloading. 1488 */ 1489 switch (hdr->csum_offset) { 1490 case offsetof(struct udphdr, uh_sum): 1491 case offsetof(struct tcphdr, th_sum): 1492 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1493 m->m_pkthdr.csum_data = 0xFFFF; 1494 break; 1495 case offsetof(struct sctphdr, checksum): 1496 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 1497 break; 1498 default: 1499 sc->vtnet_stats.rx_csum_bad_offset++; 1500 return (1); 1501 } 1502 1503 return (0); 1504 } 1505 1506 static int 1507 vtnet_rxq_csum_by_parse(struct vtnet_rxq *rxq, struct mbuf *m, 1508 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr) 1509 { 1510 struct vtnet_softc *sc; 1511 int offset, proto; 1512 1513 sc = rxq->vtnrx_sc; 1514 1515 switch (eth_type) { 1516 #if defined(INET) 1517 case ETHERTYPE_IP: { 1518 struct ip *ip; 1519 if (__predict_false(m->m_len < ip_start + sizeof(struct ip))) 1520 return (1); 1521 ip = (struct ip *)(m->m_data + ip_start); 1522 proto = ip->ip_p; 1523 offset = ip_start + (ip->ip_hl << 2); 1524 break; 1525 } 1526 #endif 1527 #if defined(INET6) 1528 case ETHERTYPE_IPV6: 1529 if (__predict_false(m->m_len < ip_start + 1530 sizeof(struct ip6_hdr))) 1531 return (1); 1532 offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto); 1533 if (__predict_false(offset < 0)) 1534 return (1); 1535 break; 1536 #endif 1537 default: 1538 sc->vtnet_stats.rx_csum_bad_ethtype++; 1539 return (1); 1540 } 1541 1542 switch (proto) { 1543 case IPPROTO_TCP: 1544 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) 1545 return (1); 1546 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1547 m->m_pkthdr.csum_data = 0xFFFF; 1548 break; 1549 case IPPROTO_UDP: 1550 if (__predict_false(m->m_len < offset + sizeof(struct udphdr))) 1551 return (1); 1552 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1553 m->m_pkthdr.csum_data = 0xFFFF; 1554 break; 1555 case IPPROTO_SCTP: 1556 if (__predict_false(m->m_len < offset + sizeof(struct sctphdr))) 1557 return (1); 1558 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 1559 break; 1560 default: 1561 /* 1562 * For the remaining protocols, FreeBSD does not support 1563 * checksum offloading, so the checksum will be recomputed. 1564 */ 1565 #if 0 1566 if_printf(sc->vtnet_ifp, "cksum offload of unsupported " 1567 "protocol eth_type=%#x proto=%d csum_start=%d " 1568 "csum_offset=%d\n", __func__, eth_type, proto, 1569 hdr->csum_start, hdr->csum_offset); 1570 #endif 1571 break; 1572 } 1573 1574 return (0); 1575 } 1576 1577 /* 1578 * Set the appropriate CSUM_* flags. Unfortunately, the information 1579 * provided is not directly useful to us. The VirtIO header gives the 1580 * offset of the checksum, which is all Linux needs, but this is not 1581 * how FreeBSD does things. We are forced to peek inside the packet 1582 * a bit. 1583 * 1584 * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD 1585 * could accept the offsets and let the stack figure it out. 1586 */ 1587 static int 1588 vtnet_rxq_csum(struct vtnet_rxq *rxq, struct mbuf *m, 1589 struct virtio_net_hdr *hdr) 1590 { 1591 struct ether_header *eh; 1592 struct ether_vlan_header *evh; 1593 uint16_t eth_type; 1594 int offset, error; 1595 1596 eh = mtod(m, struct ether_header *); 1597 eth_type = ntohs(eh->ether_type); 1598 if (eth_type == ETHERTYPE_VLAN) { 1599 /* BMV: We should handle nested VLAN tags too. */ 1600 evh = mtod(m, struct ether_vlan_header *); 1601 eth_type = ntohs(evh->evl_proto); 1602 offset = sizeof(struct ether_vlan_header); 1603 } else 1604 offset = sizeof(struct ether_header); 1605 1606 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) 1607 error = vtnet_rxq_csum_by_offset(rxq, m, eth_type, offset, hdr); 1608 else 1609 error = vtnet_rxq_csum_by_parse(rxq, m, eth_type, offset, hdr); 1610 1611 return (error); 1612 } 1613 1614 static void 1615 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs) 1616 { 1617 struct mbuf *m; 1618 1619 while (--nbufs > 0) { 1620 m = virtqueue_dequeue(rxq->vtnrx_vq, NULL); 1621 if (m == NULL) 1622 break; 1623 vtnet_rxq_discard_buf(rxq, m); 1624 } 1625 } 1626 1627 static void 1628 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m) 1629 { 1630 int error; 1631 1632 /* 1633 * Requeue the discarded mbuf. This should always be successful 1634 * since it was just dequeued. 1635 */ 1636 error = vtnet_rxq_enqueue_buf(rxq, m); 1637 KASSERT(error == 0, 1638 ("%s: cannot requeue discarded mbuf %d", __func__, error)); 1639 } 1640 1641 static int 1642 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs) 1643 { 1644 struct vtnet_softc *sc; 1645 struct ifnet *ifp; 1646 struct virtqueue *vq; 1647 struct mbuf *m, *m_tail; 1648 int len; 1649 1650 sc = rxq->vtnrx_sc; 1651 vq = rxq->vtnrx_vq; 1652 ifp = sc->vtnet_ifp; 1653 m_tail = m_head; 1654 1655 while (--nbufs > 0) { 1656 m = virtqueue_dequeue(vq, &len); 1657 if (m == NULL) { 1658 rxq->vtnrx_stats.vrxs_ierrors++; 1659 goto fail; 1660 } 1661 1662 if (vtnet_rxq_new_buf(rxq) != 0) { 1663 rxq->vtnrx_stats.vrxs_iqdrops++; 1664 vtnet_rxq_discard_buf(rxq, m); 1665 if (nbufs > 1) 1666 vtnet_rxq_discard_merged_bufs(rxq, nbufs); 1667 goto fail; 1668 } 1669 1670 if (m->m_len < len) 1671 len = m->m_len; 1672 1673 m->m_len = len; 1674 m->m_flags &= ~M_PKTHDR; 1675 1676 m_head->m_pkthdr.len += len; 1677 m_tail->m_next = m; 1678 m_tail = m; 1679 } 1680 1681 return (0); 1682 1683 fail: 1684 sc->vtnet_stats.rx_mergeable_failed++; 1685 m_freem(m_head); 1686 1687 return (1); 1688 } 1689 1690 static void 1691 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m, 1692 struct virtio_net_hdr *hdr) 1693 { 1694 struct vtnet_softc *sc; 1695 struct ifnet *ifp; 1696 struct ether_header *eh; 1697 1698 sc = rxq->vtnrx_sc; 1699 ifp = sc->vtnet_ifp; 1700 1701 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) { 1702 eh = mtod(m, struct ether_header *); 1703 if (eh->ether_type == htons(ETHERTYPE_VLAN)) { 1704 vtnet_vlan_tag_remove(m); 1705 /* 1706 * With the 802.1Q header removed, update the 1707 * checksum starting location accordingly. 1708 */ 1709 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) 1710 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN; 1711 } 1712 } 1713 1714 m->m_pkthdr.flowid = rxq->vtnrx_id; 1715 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE); 1716 1717 /* 1718 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum 1719 * distinction that Linux does. Need to reevaluate if performing 1720 * offloading for the NEEDS_CSUM case is really appropriate. 1721 */ 1722 if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM | 1723 VIRTIO_NET_HDR_F_DATA_VALID)) { 1724 if (vtnet_rxq_csum(rxq, m, hdr) == 0) 1725 rxq->vtnrx_stats.vrxs_csum++; 1726 else 1727 rxq->vtnrx_stats.vrxs_csum_failed++; 1728 } 1729 1730 rxq->vtnrx_stats.vrxs_ipackets++; 1731 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len; 1732 1733 VTNET_RXQ_UNLOCK(rxq); 1734 (*ifp->if_input)(ifp, m); 1735 VTNET_RXQ_LOCK(rxq); 1736 } 1737 1738 static int 1739 vtnet_rxq_eof(struct vtnet_rxq *rxq) 1740 { 1741 struct virtio_net_hdr lhdr, *hdr; 1742 struct vtnet_softc *sc; 1743 struct ifnet *ifp; 1744 struct virtqueue *vq; 1745 struct mbuf *m; 1746 struct virtio_net_hdr_mrg_rxbuf *mhdr; 1747 int len, deq, nbufs, adjsz, count; 1748 1749 sc = rxq->vtnrx_sc; 1750 vq = rxq->vtnrx_vq; 1751 ifp = sc->vtnet_ifp; 1752 hdr = &lhdr; 1753 deq = 0; 1754 count = sc->vtnet_rx_process_limit; 1755 1756 VTNET_RXQ_LOCK_ASSERT(rxq); 1757 1758 #ifdef DEV_NETMAP 1759 if (netmap_rx_irq(ifp, 0, &deq)) { 1760 return (FALSE); 1761 } 1762 #endif /* DEV_NETMAP */ 1763 1764 while (count-- > 0) { 1765 m = virtqueue_dequeue(vq, &len); 1766 if (m == NULL) 1767 break; 1768 deq++; 1769 1770 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) { 1771 rxq->vtnrx_stats.vrxs_ierrors++; 1772 vtnet_rxq_discard_buf(rxq, m); 1773 continue; 1774 } 1775 1776 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 1777 nbufs = 1; 1778 adjsz = sizeof(struct vtnet_rx_header); 1779 /* 1780 * Account for our pad inserted between the header 1781 * and the actual start of the frame. 1782 */ 1783 len += VTNET_RX_HEADER_PAD; 1784 } else { 1785 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *); 1786 nbufs = mhdr->num_buffers; 1787 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1788 } 1789 1790 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) { 1791 rxq->vtnrx_stats.vrxs_iqdrops++; 1792 vtnet_rxq_discard_buf(rxq, m); 1793 if (nbufs > 1) 1794 vtnet_rxq_discard_merged_bufs(rxq, nbufs); 1795 continue; 1796 } 1797 1798 m->m_pkthdr.len = len; 1799 m->m_pkthdr.rcvif = ifp; 1800 m->m_pkthdr.csum_flags = 0; 1801 1802 if (nbufs > 1) { 1803 /* Dequeue the rest of chain. */ 1804 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0) 1805 continue; 1806 } 1807 1808 /* 1809 * Save copy of header before we strip it. For both mergeable 1810 * and non-mergeable, the header is at the beginning of the 1811 * mbuf data. We no longer need num_buffers, so always use a 1812 * regular header. 1813 * 1814 * BMV: Is this memcpy() expensive? We know the mbuf data is 1815 * still valid even after the m_adj(). 1816 */ 1817 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr)); 1818 m_adj(m, adjsz); 1819 1820 vtnet_rxq_input(rxq, m, hdr); 1821 1822 /* Must recheck after dropping the Rx lock. */ 1823 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1824 break; 1825 } 1826 1827 if (deq > 0) 1828 virtqueue_notify(vq); 1829 1830 return (count > 0 ? 0 : EAGAIN); 1831 } 1832 1833 static void 1834 vtnet_rx_vq_intr(void *xrxq) 1835 { 1836 struct vtnet_softc *sc; 1837 struct vtnet_rxq *rxq; 1838 struct ifnet *ifp; 1839 int tries, more; 1840 1841 rxq = xrxq; 1842 sc = rxq->vtnrx_sc; 1843 ifp = sc->vtnet_ifp; 1844 tries = 0; 1845 1846 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) { 1847 /* 1848 * Ignore this interrupt. Either this is a spurious interrupt 1849 * or multiqueue without per-VQ MSIX so every queue needs to 1850 * be polled (a brain dead configuration we could try harder 1851 * to avoid). 1852 */ 1853 vtnet_rxq_disable_intr(rxq); 1854 return; 1855 } 1856 1857 VTNET_RXQ_LOCK(rxq); 1858 1859 again: 1860 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1861 VTNET_RXQ_UNLOCK(rxq); 1862 return; 1863 } 1864 1865 more = vtnet_rxq_eof(rxq); 1866 if (more || vtnet_rxq_enable_intr(rxq) != 0) { 1867 if (!more) 1868 vtnet_rxq_disable_intr(rxq); 1869 /* 1870 * This is an occasional condition or race (when !more), 1871 * so retry a few times before scheduling the taskqueue. 1872 */ 1873 if (tries++ < VTNET_INTR_DISABLE_RETRIES) 1874 goto again; 1875 1876 VTNET_RXQ_UNLOCK(rxq); 1877 rxq->vtnrx_stats.vrxs_rescheduled++; 1878 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask); 1879 } else 1880 VTNET_RXQ_UNLOCK(rxq); 1881 } 1882 1883 static void 1884 vtnet_rxq_tq_intr(void *xrxq, int pending) 1885 { 1886 struct vtnet_softc *sc; 1887 struct vtnet_rxq *rxq; 1888 struct ifnet *ifp; 1889 int more; 1890 1891 rxq = xrxq; 1892 sc = rxq->vtnrx_sc; 1893 ifp = sc->vtnet_ifp; 1894 1895 VTNET_RXQ_LOCK(rxq); 1896 1897 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1898 VTNET_RXQ_UNLOCK(rxq); 1899 return; 1900 } 1901 1902 more = vtnet_rxq_eof(rxq); 1903 if (more || vtnet_rxq_enable_intr(rxq) != 0) { 1904 if (!more) 1905 vtnet_rxq_disable_intr(rxq); 1906 rxq->vtnrx_stats.vrxs_rescheduled++; 1907 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask); 1908 } 1909 1910 VTNET_RXQ_UNLOCK(rxq); 1911 } 1912 1913 static int 1914 vtnet_txq_below_threshold(struct vtnet_txq *txq) 1915 { 1916 struct vtnet_softc *sc; 1917 struct virtqueue *vq; 1918 1919 sc = txq->vtntx_sc; 1920 vq = txq->vtntx_vq; 1921 1922 return (virtqueue_nfree(vq) <= sc->vtnet_tx_intr_thresh); 1923 } 1924 1925 static int 1926 vtnet_txq_notify(struct vtnet_txq *txq) 1927 { 1928 struct virtqueue *vq; 1929 1930 vq = txq->vtntx_vq; 1931 1932 txq->vtntx_watchdog = VTNET_TX_TIMEOUT; 1933 virtqueue_notify(vq); 1934 1935 if (vtnet_txq_enable_intr(txq) == 0) 1936 return (0); 1937 1938 /* 1939 * Drain frames that were completed since last checked. If this 1940 * causes the queue to go above the threshold, the caller should 1941 * continue transmitting. 1942 */ 1943 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) { 1944 virtqueue_disable_intr(vq); 1945 return (1); 1946 } 1947 1948 return (0); 1949 } 1950 1951 static void 1952 vtnet_txq_free_mbufs(struct vtnet_txq *txq) 1953 { 1954 struct virtqueue *vq; 1955 struct vtnet_tx_header *txhdr; 1956 int last; 1957 1958 vq = txq->vtntx_vq; 1959 last = 0; 1960 1961 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) { 1962 m_freem(txhdr->vth_mbuf); 1963 uma_zfree(vtnet_tx_header_zone, txhdr); 1964 } 1965 1966 KASSERT(virtqueue_empty(vq), 1967 ("%s: mbufs remaining in tx queue %p", __func__, txq)); 1968 } 1969 1970 /* 1971 * BMV: Much of this can go away once we finally have offsets in 1972 * the mbuf packet header. Bug andre@. 1973 */ 1974 static int 1975 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m, 1976 int *etype, int *proto, int *start) 1977 { 1978 struct vtnet_softc *sc; 1979 struct ether_vlan_header *evh; 1980 int offset; 1981 1982 sc = txq->vtntx_sc; 1983 1984 evh = mtod(m, struct ether_vlan_header *); 1985 if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 1986 /* BMV: We should handle nested VLAN tags too. */ 1987 *etype = ntohs(evh->evl_proto); 1988 offset = sizeof(struct ether_vlan_header); 1989 } else { 1990 *etype = ntohs(evh->evl_encap_proto); 1991 offset = sizeof(struct ether_header); 1992 } 1993 1994 switch (*etype) { 1995 #if defined(INET) 1996 case ETHERTYPE_IP: { 1997 struct ip *ip, iphdr; 1998 if (__predict_false(m->m_len < offset + sizeof(struct ip))) { 1999 m_copydata(m, offset, sizeof(struct ip), 2000 (caddr_t) &iphdr); 2001 ip = &iphdr; 2002 } else 2003 ip = (struct ip *)(m->m_data + offset); 2004 *proto = ip->ip_p; 2005 *start = offset + (ip->ip_hl << 2); 2006 break; 2007 } 2008 #endif 2009 #if defined(INET6) 2010 case ETHERTYPE_IPV6: 2011 *proto = -1; 2012 *start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto); 2013 /* Assert the network stack sent us a valid packet. */ 2014 KASSERT(*start > offset, 2015 ("%s: mbuf %p start %d offset %d proto %d", __func__, m, 2016 *start, offset, *proto)); 2017 break; 2018 #endif 2019 default: 2020 sc->vtnet_stats.tx_csum_bad_ethtype++; 2021 return (EINVAL); 2022 } 2023 2024 return (0); 2025 } 2026 2027 static int 2028 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type, 2029 int offset, struct virtio_net_hdr *hdr) 2030 { 2031 static struct timeval lastecn; 2032 static int curecn; 2033 struct vtnet_softc *sc; 2034 struct tcphdr *tcp, tcphdr; 2035 2036 sc = txq->vtntx_sc; 2037 2038 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) { 2039 m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr); 2040 tcp = &tcphdr; 2041 } else 2042 tcp = (struct tcphdr *)(m->m_data + offset); 2043 2044 hdr->hdr_len = offset + (tcp->th_off << 2); 2045 hdr->gso_size = m->m_pkthdr.tso_segsz; 2046 hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 : 2047 VIRTIO_NET_HDR_GSO_TCPV6; 2048 2049 if (tcp->th_flags & TH_CWR) { 2050 /* 2051 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD, 2052 * ECN support is not on a per-interface basis, but globally via 2053 * the net.inet.tcp.ecn.enable sysctl knob. The default is off. 2054 */ 2055 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) { 2056 if (ppsratecheck(&lastecn, &curecn, 1)) 2057 if_printf(sc->vtnet_ifp, 2058 "TSO with ECN not negotiated with host\n"); 2059 return (ENOTSUP); 2060 } 2061 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; 2062 } 2063 2064 txq->vtntx_stats.vtxs_tso++; 2065 2066 return (0); 2067 } 2068 2069 static struct mbuf * 2070 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m, 2071 struct virtio_net_hdr *hdr) 2072 { 2073 struct vtnet_softc *sc; 2074 int flags, etype, csum_start, proto, error; 2075 2076 sc = txq->vtntx_sc; 2077 flags = m->m_pkthdr.csum_flags; 2078 2079 error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start); 2080 if (error) 2081 goto drop; 2082 2083 if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) || 2084 (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) { 2085 /* 2086 * We could compare the IP protocol vs the CSUM_ flag too, 2087 * but that really should not be necessary. 2088 */ 2089 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM; 2090 hdr->csum_start = csum_start; 2091 hdr->csum_offset = m->m_pkthdr.csum_data; 2092 txq->vtntx_stats.vtxs_csum++; 2093 } 2094 2095 if (flags & CSUM_TSO) { 2096 if (__predict_false(proto != IPPROTO_TCP)) { 2097 /* Likely failed to correctly parse the mbuf. */ 2098 sc->vtnet_stats.tx_tso_not_tcp++; 2099 goto drop; 2100 } 2101 2102 KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM, 2103 ("%s: mbuf %p TSO without checksum offload %#x", 2104 __func__, m, flags)); 2105 2106 error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr); 2107 if (error) 2108 goto drop; 2109 } 2110 2111 return (m); 2112 2113 drop: 2114 m_freem(m); 2115 return (NULL); 2116 } 2117 2118 static int 2119 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head, 2120 struct vtnet_tx_header *txhdr) 2121 { 2122 struct vtnet_softc *sc; 2123 struct virtqueue *vq; 2124 struct sglist *sg; 2125 struct mbuf *m; 2126 int error; 2127 2128 sc = txq->vtntx_sc; 2129 vq = txq->vtntx_vq; 2130 sg = txq->vtntx_sg; 2131 m = *m_head; 2132 2133 sglist_reset(sg); 2134 error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size); 2135 KASSERT(error == 0 && sg->sg_nseg == 1, 2136 ("%s: error %d adding header to sglist", __func__, error)); 2137 2138 error = sglist_append_mbuf(sg, m); 2139 if (error) { 2140 m = m_defrag(m, M_NOWAIT); 2141 if (m == NULL) 2142 goto fail; 2143 2144 *m_head = m; 2145 sc->vtnet_stats.tx_defragged++; 2146 2147 error = sglist_append_mbuf(sg, m); 2148 if (error) 2149 goto fail; 2150 } 2151 2152 txhdr->vth_mbuf = m; 2153 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0); 2154 2155 return (error); 2156 2157 fail: 2158 sc->vtnet_stats.tx_defrag_failed++; 2159 m_freem(*m_head); 2160 *m_head = NULL; 2161 2162 return (ENOBUFS); 2163 } 2164 2165 static int 2166 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head) 2167 { 2168 struct vtnet_tx_header *txhdr; 2169 struct virtio_net_hdr *hdr; 2170 struct mbuf *m; 2171 int error; 2172 2173 m = *m_head; 2174 M_ASSERTPKTHDR(m); 2175 2176 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO); 2177 if (txhdr == NULL) { 2178 m_freem(m); 2179 *m_head = NULL; 2180 return (ENOMEM); 2181 } 2182 2183 /* 2184 * Always use the non-mergeable header, regardless if the feature 2185 * was negotiated. For transmit, num_buffers is always zero. The 2186 * vtnet_hdr_size is used to enqueue the correct header size. 2187 */ 2188 hdr = &txhdr->vth_uhdr.hdr; 2189 2190 if (m->m_flags & M_VLANTAG) { 2191 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); 2192 if ((*m_head = m) == NULL) { 2193 error = ENOBUFS; 2194 goto fail; 2195 } 2196 m->m_flags &= ~M_VLANTAG; 2197 } 2198 2199 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) { 2200 m = vtnet_txq_offload(txq, m, hdr); 2201 if ((*m_head = m) == NULL) { 2202 error = ENOBUFS; 2203 goto fail; 2204 } 2205 } 2206 2207 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr); 2208 if (error == 0) 2209 return (0); 2210 2211 fail: 2212 uma_zfree(vtnet_tx_header_zone, txhdr); 2213 2214 return (error); 2215 } 2216 2217 #ifdef VTNET_LEGACY_TX 2218 2219 static void 2220 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp) 2221 { 2222 struct vtnet_softc *sc; 2223 struct virtqueue *vq; 2224 struct mbuf *m0; 2225 int tries, enq; 2226 2227 sc = txq->vtntx_sc; 2228 vq = txq->vtntx_vq; 2229 tries = 0; 2230 2231 VTNET_TXQ_LOCK_ASSERT(txq); 2232 2233 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2234 sc->vtnet_link_active == 0) 2235 return; 2236 2237 vtnet_txq_eof(txq); 2238 2239 again: 2240 enq = 0; 2241 2242 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 2243 if (virtqueue_full(vq)) 2244 break; 2245 2246 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2247 if (m0 == NULL) 2248 break; 2249 2250 if (vtnet_txq_encap(txq, &m0) != 0) { 2251 if (m0 != NULL) 2252 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2253 break; 2254 } 2255 2256 enq++; 2257 ETHER_BPF_MTAP(ifp, m0); 2258 } 2259 2260 if (enq > 0 && vtnet_txq_notify(txq) != 0) { 2261 if (tries++ < VTNET_NOTIFY_RETRIES) 2262 goto again; 2263 2264 txq->vtntx_stats.vtxs_rescheduled++; 2265 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask); 2266 } 2267 } 2268 2269 static void 2270 vtnet_start(struct ifnet *ifp) 2271 { 2272 struct vtnet_softc *sc; 2273 struct vtnet_txq *txq; 2274 2275 sc = ifp->if_softc; 2276 txq = &sc->vtnet_txqs[0]; 2277 2278 VTNET_TXQ_LOCK(txq); 2279 vtnet_start_locked(txq, ifp); 2280 VTNET_TXQ_UNLOCK(txq); 2281 } 2282 2283 #else /* !VTNET_LEGACY_TX */ 2284 2285 static int 2286 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m) 2287 { 2288 struct vtnet_softc *sc; 2289 struct virtqueue *vq; 2290 struct buf_ring *br; 2291 struct ifnet *ifp; 2292 int enq, tries, error; 2293 2294 sc = txq->vtntx_sc; 2295 vq = txq->vtntx_vq; 2296 br = txq->vtntx_br; 2297 ifp = sc->vtnet_ifp; 2298 tries = 0; 2299 error = 0; 2300 2301 VTNET_TXQ_LOCK_ASSERT(txq); 2302 2303 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2304 sc->vtnet_link_active == 0) { 2305 if (m != NULL) 2306 error = drbr_enqueue(ifp, br, m); 2307 return (error); 2308 } 2309 2310 if (m != NULL) { 2311 error = drbr_enqueue(ifp, br, m); 2312 if (error) 2313 return (error); 2314 } 2315 2316 vtnet_txq_eof(txq); 2317 2318 again: 2319 enq = 0; 2320 2321 while ((m = drbr_peek(ifp, br)) != NULL) { 2322 if (virtqueue_full(vq)) { 2323 drbr_putback(ifp, br, m); 2324 break; 2325 } 2326 2327 if (vtnet_txq_encap(txq, &m) != 0) { 2328 if (m != NULL) 2329 drbr_putback(ifp, br, m); 2330 else 2331 drbr_advance(ifp, br); 2332 break; 2333 } 2334 drbr_advance(ifp, br); 2335 2336 enq++; 2337 ETHER_BPF_MTAP(ifp, m); 2338 } 2339 2340 if (enq > 0 && vtnet_txq_notify(txq) != 0) { 2341 if (tries++ < VTNET_NOTIFY_RETRIES) 2342 goto again; 2343 2344 txq->vtntx_stats.vtxs_rescheduled++; 2345 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask); 2346 } 2347 2348 return (0); 2349 } 2350 2351 static int 2352 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m) 2353 { 2354 struct vtnet_softc *sc; 2355 struct vtnet_txq *txq; 2356 int i, npairs, error; 2357 2358 sc = ifp->if_softc; 2359 npairs = sc->vtnet_act_vq_pairs; 2360 2361 /* check if flowid is set */ 2362 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 2363 i = m->m_pkthdr.flowid % npairs; 2364 else 2365 i = curcpu % npairs; 2366 2367 txq = &sc->vtnet_txqs[i]; 2368 2369 if (VTNET_TXQ_TRYLOCK(txq) != 0) { 2370 error = vtnet_txq_mq_start_locked(txq, m); 2371 VTNET_TXQ_UNLOCK(txq); 2372 } else { 2373 error = drbr_enqueue(ifp, txq->vtntx_br, m); 2374 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask); 2375 } 2376 2377 return (error); 2378 } 2379 2380 static void 2381 vtnet_txq_tq_deferred(void *xtxq, int pending) 2382 { 2383 struct vtnet_softc *sc; 2384 struct vtnet_txq *txq; 2385 2386 txq = xtxq; 2387 sc = txq->vtntx_sc; 2388 2389 VTNET_TXQ_LOCK(txq); 2390 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br)) 2391 vtnet_txq_mq_start_locked(txq, NULL); 2392 VTNET_TXQ_UNLOCK(txq); 2393 } 2394 2395 #endif /* VTNET_LEGACY_TX */ 2396 2397 static void 2398 vtnet_txq_start(struct vtnet_txq *txq) 2399 { 2400 struct vtnet_softc *sc; 2401 struct ifnet *ifp; 2402 2403 sc = txq->vtntx_sc; 2404 ifp = sc->vtnet_ifp; 2405 2406 #ifdef VTNET_LEGACY_TX 2407 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2408 vtnet_start_locked(txq, ifp); 2409 #else 2410 if (!drbr_empty(ifp, txq->vtntx_br)) 2411 vtnet_txq_mq_start_locked(txq, NULL); 2412 #endif 2413 } 2414 2415 static void 2416 vtnet_txq_tq_intr(void *xtxq, int pending) 2417 { 2418 struct vtnet_softc *sc; 2419 struct vtnet_txq *txq; 2420 struct ifnet *ifp; 2421 2422 txq = xtxq; 2423 sc = txq->vtntx_sc; 2424 ifp = sc->vtnet_ifp; 2425 2426 VTNET_TXQ_LOCK(txq); 2427 2428 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2429 VTNET_TXQ_UNLOCK(txq); 2430 return; 2431 } 2432 2433 vtnet_txq_eof(txq); 2434 vtnet_txq_start(txq); 2435 2436 VTNET_TXQ_UNLOCK(txq); 2437 } 2438 2439 static int 2440 vtnet_txq_eof(struct vtnet_txq *txq) 2441 { 2442 struct virtqueue *vq; 2443 struct vtnet_tx_header *txhdr; 2444 struct mbuf *m; 2445 int deq; 2446 2447 vq = txq->vtntx_vq; 2448 deq = 0; 2449 VTNET_TXQ_LOCK_ASSERT(txq); 2450 2451 #ifdef DEV_NETMAP 2452 if (netmap_tx_irq(txq->vtntx_sc->vtnet_ifp, txq->vtntx_id)) { 2453 virtqueue_disable_intr(vq); // XXX luigi 2454 return 0; // XXX or 1 ? 2455 } 2456 #endif /* DEV_NETMAP */ 2457 2458 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) { 2459 m = txhdr->vth_mbuf; 2460 deq++; 2461 2462 txq->vtntx_stats.vtxs_opackets++; 2463 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len; 2464 if (m->m_flags & M_MCAST) 2465 txq->vtntx_stats.vtxs_omcasts++; 2466 2467 m_freem(m); 2468 uma_zfree(vtnet_tx_header_zone, txhdr); 2469 } 2470 2471 if (virtqueue_empty(vq)) 2472 txq->vtntx_watchdog = 0; 2473 2474 return (deq); 2475 } 2476 2477 static void 2478 vtnet_tx_vq_intr(void *xtxq) 2479 { 2480 struct vtnet_softc *sc; 2481 struct vtnet_txq *txq; 2482 struct ifnet *ifp; 2483 2484 txq = xtxq; 2485 sc = txq->vtntx_sc; 2486 ifp = sc->vtnet_ifp; 2487 2488 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) { 2489 /* 2490 * Ignore this interrupt. Either this is a spurious interrupt 2491 * or multiqueue without per-VQ MSIX so every queue needs to 2492 * be polled (a brain dead configuration we could try harder 2493 * to avoid). 2494 */ 2495 vtnet_txq_disable_intr(txq); 2496 return; 2497 } 2498 2499 VTNET_TXQ_LOCK(txq); 2500 2501 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2502 VTNET_TXQ_UNLOCK(txq); 2503 return; 2504 } 2505 2506 vtnet_txq_eof(txq); 2507 vtnet_txq_start(txq); 2508 2509 VTNET_TXQ_UNLOCK(txq); 2510 } 2511 2512 static void 2513 vtnet_tx_start_all(struct vtnet_softc *sc) 2514 { 2515 struct vtnet_txq *txq; 2516 int i; 2517 2518 VTNET_CORE_LOCK_ASSERT(sc); 2519 2520 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2521 txq = &sc->vtnet_txqs[i]; 2522 2523 VTNET_TXQ_LOCK(txq); 2524 vtnet_txq_start(txq); 2525 VTNET_TXQ_UNLOCK(txq); 2526 } 2527 } 2528 2529 #ifndef VTNET_LEGACY_TX 2530 static void 2531 vtnet_qflush(struct ifnet *ifp) 2532 { 2533 struct vtnet_softc *sc; 2534 struct vtnet_txq *txq; 2535 struct mbuf *m; 2536 int i; 2537 2538 sc = ifp->if_softc; 2539 2540 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2541 txq = &sc->vtnet_txqs[i]; 2542 2543 VTNET_TXQ_LOCK(txq); 2544 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL) 2545 m_freem(m); 2546 VTNET_TXQ_UNLOCK(txq); 2547 } 2548 2549 if_qflush(ifp); 2550 } 2551 #endif 2552 2553 static int 2554 vtnet_watchdog(struct vtnet_txq *txq) 2555 { 2556 struct ifnet *ifp; 2557 2558 ifp = txq->vtntx_sc->vtnet_ifp; 2559 2560 VTNET_TXQ_LOCK(txq); 2561 if (txq->vtntx_watchdog == 1) { 2562 /* 2563 * Only drain completed frames if the watchdog is about to 2564 * expire. If any frames were drained, there may be enough 2565 * free descriptors now available to transmit queued frames. 2566 * In that case, the timer will immediately be decremented 2567 * below, but the timeout is generous enough that should not 2568 * be a problem. 2569 */ 2570 if (vtnet_txq_eof(txq) != 0) 2571 vtnet_txq_start(txq); 2572 } 2573 2574 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) { 2575 VTNET_TXQ_UNLOCK(txq); 2576 return (0); 2577 } 2578 VTNET_TXQ_UNLOCK(txq); 2579 2580 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id); 2581 return (1); 2582 } 2583 2584 static void 2585 vtnet_accum_stats(struct vtnet_softc *sc, struct vtnet_rxq_stats *rxacc, 2586 struct vtnet_txq_stats *txacc) 2587 { 2588 2589 bzero(rxacc, sizeof(struct vtnet_rxq_stats)); 2590 bzero(txacc, sizeof(struct vtnet_txq_stats)); 2591 2592 for (int i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2593 struct vtnet_rxq_stats *rxst; 2594 struct vtnet_txq_stats *txst; 2595 2596 rxst = &sc->vtnet_rxqs[i].vtnrx_stats; 2597 rxacc->vrxs_ipackets += rxst->vrxs_ipackets; 2598 rxacc->vrxs_ibytes += rxst->vrxs_ibytes; 2599 rxacc->vrxs_iqdrops += rxst->vrxs_iqdrops; 2600 rxacc->vrxs_csum += rxst->vrxs_csum; 2601 rxacc->vrxs_csum_failed += rxst->vrxs_csum_failed; 2602 rxacc->vrxs_rescheduled += rxst->vrxs_rescheduled; 2603 2604 txst = &sc->vtnet_txqs[i].vtntx_stats; 2605 txacc->vtxs_opackets += txst->vtxs_opackets; 2606 txacc->vtxs_obytes += txst->vtxs_obytes; 2607 txacc->vtxs_csum += txst->vtxs_csum; 2608 txacc->vtxs_tso += txst->vtxs_tso; 2609 txacc->vtxs_rescheduled += txst->vtxs_rescheduled; 2610 } 2611 } 2612 2613 static uint64_t 2614 vtnet_get_counter(if_t ifp, ift_counter cnt) 2615 { 2616 struct vtnet_softc *sc; 2617 struct vtnet_rxq_stats rxaccum; 2618 struct vtnet_txq_stats txaccum; 2619 2620 sc = if_getsoftc(ifp); 2621 vtnet_accum_stats(sc, &rxaccum, &txaccum); 2622 2623 switch (cnt) { 2624 case IFCOUNTER_IPACKETS: 2625 return (rxaccum.vrxs_ipackets); 2626 case IFCOUNTER_IQDROPS: 2627 return (rxaccum.vrxs_iqdrops); 2628 case IFCOUNTER_IERRORS: 2629 return (rxaccum.vrxs_ierrors); 2630 case IFCOUNTER_OPACKETS: 2631 return (txaccum.vtxs_opackets); 2632 #ifndef VTNET_LEGACY_TX 2633 case IFCOUNTER_OBYTES: 2634 return (txaccum.vtxs_obytes); 2635 case IFCOUNTER_OMCASTS: 2636 return (txaccum.vtxs_omcasts); 2637 #endif 2638 default: 2639 return (if_get_counter_default(ifp, cnt)); 2640 } 2641 } 2642 2643 static void 2644 vtnet_tick(void *xsc) 2645 { 2646 struct vtnet_softc *sc; 2647 struct ifnet *ifp; 2648 int i, timedout; 2649 2650 sc = xsc; 2651 ifp = sc->vtnet_ifp; 2652 timedout = 0; 2653 2654 VTNET_CORE_LOCK_ASSERT(sc); 2655 2656 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 2657 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]); 2658 2659 if (timedout != 0) { 2660 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2661 vtnet_init_locked(sc); 2662 } else 2663 callout_schedule(&sc->vtnet_tick_ch, hz); 2664 } 2665 2666 static void 2667 vtnet_start_taskqueues(struct vtnet_softc *sc) 2668 { 2669 device_t dev; 2670 struct vtnet_rxq *rxq; 2671 struct vtnet_txq *txq; 2672 int i, error; 2673 2674 dev = sc->vtnet_dev; 2675 2676 /* 2677 * Errors here are very difficult to recover from - we cannot 2678 * easily fail because, if this is during boot, we will hang 2679 * when freeing any successfully started taskqueues because 2680 * the scheduler isn't up yet. 2681 * 2682 * Most drivers just ignore the return value - it only fails 2683 * with ENOMEM so an error is not likely. 2684 */ 2685 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2686 rxq = &sc->vtnet_rxqs[i]; 2687 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET, 2688 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id); 2689 if (error) { 2690 device_printf(dev, "failed to start rx taskq %d\n", 2691 rxq->vtnrx_id); 2692 } 2693 2694 txq = &sc->vtnet_txqs[i]; 2695 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET, 2696 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id); 2697 if (error) { 2698 device_printf(dev, "failed to start tx taskq %d\n", 2699 txq->vtntx_id); 2700 } 2701 } 2702 } 2703 2704 static void 2705 vtnet_free_taskqueues(struct vtnet_softc *sc) 2706 { 2707 struct vtnet_rxq *rxq; 2708 struct vtnet_txq *txq; 2709 int i; 2710 2711 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2712 rxq = &sc->vtnet_rxqs[i]; 2713 if (rxq->vtnrx_tq != NULL) { 2714 taskqueue_free(rxq->vtnrx_tq); 2715 rxq->vtnrx_vq = NULL; 2716 } 2717 2718 txq = &sc->vtnet_txqs[i]; 2719 if (txq->vtntx_tq != NULL) { 2720 taskqueue_free(txq->vtntx_tq); 2721 txq->vtntx_tq = NULL; 2722 } 2723 } 2724 } 2725 2726 static void 2727 vtnet_drain_taskqueues(struct vtnet_softc *sc) 2728 { 2729 struct vtnet_rxq *rxq; 2730 struct vtnet_txq *txq; 2731 int i; 2732 2733 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2734 rxq = &sc->vtnet_rxqs[i]; 2735 if (rxq->vtnrx_tq != NULL) 2736 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask); 2737 2738 txq = &sc->vtnet_txqs[i]; 2739 if (txq->vtntx_tq != NULL) { 2740 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask); 2741 #ifndef VTNET_LEGACY_TX 2742 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask); 2743 #endif 2744 } 2745 } 2746 } 2747 2748 static void 2749 vtnet_drain_rxtx_queues(struct vtnet_softc *sc) 2750 { 2751 struct vtnet_rxq *rxq; 2752 struct vtnet_txq *txq; 2753 int i; 2754 2755 #ifdef DEV_NETMAP 2756 if (nm_native_on(NA(sc->vtnet_ifp))) 2757 return; 2758 #endif /* DEV_NETMAP */ 2759 2760 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2761 rxq = &sc->vtnet_rxqs[i]; 2762 vtnet_rxq_free_mbufs(rxq); 2763 2764 txq = &sc->vtnet_txqs[i]; 2765 vtnet_txq_free_mbufs(txq); 2766 } 2767 } 2768 2769 static void 2770 vtnet_stop_rendezvous(struct vtnet_softc *sc) 2771 { 2772 struct vtnet_rxq *rxq; 2773 struct vtnet_txq *txq; 2774 int i; 2775 2776 /* 2777 * Lock and unlock the per-queue mutex so we known the stop 2778 * state is visible. Doing only the active queues should be 2779 * sufficient, but it does not cost much extra to do all the 2780 * queues. Note we hold the core mutex here too. 2781 */ 2782 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2783 rxq = &sc->vtnet_rxqs[i]; 2784 VTNET_RXQ_LOCK(rxq); 2785 VTNET_RXQ_UNLOCK(rxq); 2786 2787 txq = &sc->vtnet_txqs[i]; 2788 VTNET_TXQ_LOCK(txq); 2789 VTNET_TXQ_UNLOCK(txq); 2790 } 2791 } 2792 2793 static void 2794 vtnet_stop(struct vtnet_softc *sc) 2795 { 2796 device_t dev; 2797 struct ifnet *ifp; 2798 2799 dev = sc->vtnet_dev; 2800 ifp = sc->vtnet_ifp; 2801 2802 VTNET_CORE_LOCK_ASSERT(sc); 2803 2804 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2805 sc->vtnet_link_active = 0; 2806 callout_stop(&sc->vtnet_tick_ch); 2807 2808 /* Only advisory. */ 2809 vtnet_disable_interrupts(sc); 2810 2811 /* 2812 * Stop the host adapter. This resets it to the pre-initialized 2813 * state. It will not generate any interrupts until after it is 2814 * reinitialized. 2815 */ 2816 virtio_stop(dev); 2817 vtnet_stop_rendezvous(sc); 2818 2819 /* Free any mbufs left in the virtqueues. */ 2820 vtnet_drain_rxtx_queues(sc); 2821 } 2822 2823 static int 2824 vtnet_virtio_reinit(struct vtnet_softc *sc) 2825 { 2826 device_t dev; 2827 struct ifnet *ifp; 2828 uint64_t features; 2829 int mask, error; 2830 2831 dev = sc->vtnet_dev; 2832 ifp = sc->vtnet_ifp; 2833 features = sc->vtnet_features; 2834 2835 mask = 0; 2836 #if defined(INET) 2837 mask |= IFCAP_RXCSUM; 2838 #endif 2839 #if defined (INET6) 2840 mask |= IFCAP_RXCSUM_IPV6; 2841 #endif 2842 2843 /* 2844 * Re-negotiate with the host, removing any disabled receive 2845 * features. Transmit features are disabled only on our side 2846 * via if_capenable and if_hwassist. 2847 */ 2848 2849 if (ifp->if_capabilities & mask) { 2850 /* 2851 * We require both IPv4 and IPv6 offloading to be enabled 2852 * in order to negotiated it: VirtIO does not distinguish 2853 * between the two. 2854 */ 2855 if ((ifp->if_capenable & mask) != mask) 2856 features &= ~VIRTIO_NET_F_GUEST_CSUM; 2857 } 2858 2859 if (ifp->if_capabilities & IFCAP_LRO) { 2860 if ((ifp->if_capenable & IFCAP_LRO) == 0) 2861 features &= ~VTNET_LRO_FEATURES; 2862 } 2863 2864 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) { 2865 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0) 2866 features &= ~VIRTIO_NET_F_CTRL_VLAN; 2867 } 2868 2869 error = virtio_reinit(dev, features); 2870 if (error) 2871 device_printf(dev, "virtio reinit error %d\n", error); 2872 2873 return (error); 2874 } 2875 2876 static void 2877 vtnet_init_rx_filters(struct vtnet_softc *sc) 2878 { 2879 struct ifnet *ifp; 2880 2881 ifp = sc->vtnet_ifp; 2882 2883 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) { 2884 /* Restore promiscuous and all-multicast modes. */ 2885 vtnet_rx_filter(sc); 2886 /* Restore filtered MAC addresses. */ 2887 vtnet_rx_filter_mac(sc); 2888 } 2889 2890 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) 2891 vtnet_rx_filter_vlan(sc); 2892 } 2893 2894 static int 2895 vtnet_init_rx_queues(struct vtnet_softc *sc) 2896 { 2897 device_t dev; 2898 struct vtnet_rxq *rxq; 2899 int i, clsize, error; 2900 2901 dev = sc->vtnet_dev; 2902 2903 /* 2904 * Use the new cluster size if one has been set (via a MTU 2905 * change). Otherwise, use the standard 2K clusters. 2906 * 2907 * BMV: It might make sense to use page sized clusters as 2908 * the default (depending on the features negotiated). 2909 */ 2910 if (sc->vtnet_rx_new_clsize != 0) { 2911 clsize = sc->vtnet_rx_new_clsize; 2912 sc->vtnet_rx_new_clsize = 0; 2913 } else 2914 clsize = MCLBYTES; 2915 2916 sc->vtnet_rx_clsize = clsize; 2917 sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize); 2918 2919 KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS || 2920 sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs, 2921 ("%s: too many rx mbufs %d for %d segments", __func__, 2922 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs)); 2923 2924 #ifdef DEV_NETMAP 2925 if (vtnet_netmap_init_rx_buffers(sc)) 2926 return 0; 2927 #endif /* DEV_NETMAP */ 2928 2929 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2930 rxq = &sc->vtnet_rxqs[i]; 2931 2932 /* Hold the lock to satisfy asserts. */ 2933 VTNET_RXQ_LOCK(rxq); 2934 error = vtnet_rxq_populate(rxq); 2935 VTNET_RXQ_UNLOCK(rxq); 2936 2937 if (error) { 2938 device_printf(dev, 2939 "cannot allocate mbufs for Rx queue %d\n", i); 2940 return (error); 2941 } 2942 } 2943 2944 return (0); 2945 } 2946 2947 static int 2948 vtnet_init_tx_queues(struct vtnet_softc *sc) 2949 { 2950 struct vtnet_txq *txq; 2951 int i; 2952 2953 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2954 txq = &sc->vtnet_txqs[i]; 2955 txq->vtntx_watchdog = 0; 2956 } 2957 2958 return (0); 2959 } 2960 2961 static int 2962 vtnet_init_rxtx_queues(struct vtnet_softc *sc) 2963 { 2964 int error; 2965 2966 error = vtnet_init_rx_queues(sc); 2967 if (error) 2968 return (error); 2969 2970 error = vtnet_init_tx_queues(sc); 2971 if (error) 2972 return (error); 2973 2974 return (0); 2975 } 2976 2977 static void 2978 vtnet_set_active_vq_pairs(struct vtnet_softc *sc) 2979 { 2980 device_t dev; 2981 int npairs; 2982 2983 dev = sc->vtnet_dev; 2984 2985 if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) { 2986 MPASS(sc->vtnet_max_vq_pairs == 1); 2987 sc->vtnet_act_vq_pairs = 1; 2988 return; 2989 } 2990 2991 /* BMV: Just use the maximum configured for now. */ 2992 npairs = sc->vtnet_max_vq_pairs; 2993 2994 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) { 2995 device_printf(dev, 2996 "cannot set active queue pairs to %d\n", npairs); 2997 npairs = 1; 2998 } 2999 3000 sc->vtnet_act_vq_pairs = npairs; 3001 } 3002 3003 static int 3004 vtnet_reinit(struct vtnet_softc *sc) 3005 { 3006 struct ifnet *ifp; 3007 int error; 3008 3009 ifp = sc->vtnet_ifp; 3010 3011 /* Use the current MAC address. */ 3012 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN); 3013 vtnet_set_hwaddr(sc); 3014 3015 vtnet_set_active_vq_pairs(sc); 3016 3017 ifp->if_hwassist = 0; 3018 if (ifp->if_capenable & IFCAP_TXCSUM) 3019 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD; 3020 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6) 3021 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6; 3022 if (ifp->if_capenable & IFCAP_TSO4) 3023 ifp->if_hwassist |= CSUM_IP_TSO; 3024 if (ifp->if_capenable & IFCAP_TSO6) 3025 ifp->if_hwassist |= CSUM_IP6_TSO; 3026 3027 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) 3028 vtnet_init_rx_filters(sc); 3029 3030 error = vtnet_init_rxtx_queues(sc); 3031 if (error) 3032 return (error); 3033 3034 vtnet_enable_interrupts(sc); 3035 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3036 3037 return (0); 3038 } 3039 3040 static void 3041 vtnet_init_locked(struct vtnet_softc *sc) 3042 { 3043 device_t dev; 3044 struct ifnet *ifp; 3045 3046 dev = sc->vtnet_dev; 3047 ifp = sc->vtnet_ifp; 3048 3049 VTNET_CORE_LOCK_ASSERT(sc); 3050 3051 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 3052 return; 3053 3054 vtnet_stop(sc); 3055 3056 /* Reinitialize with the host. */ 3057 if (vtnet_virtio_reinit(sc) != 0) 3058 goto fail; 3059 3060 if (vtnet_reinit(sc) != 0) 3061 goto fail; 3062 3063 virtio_reinit_complete(dev); 3064 3065 vtnet_update_link_status(sc); 3066 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc); 3067 3068 return; 3069 3070 fail: 3071 vtnet_stop(sc); 3072 } 3073 3074 static void 3075 vtnet_init(void *xsc) 3076 { 3077 struct vtnet_softc *sc; 3078 3079 sc = xsc; 3080 3081 #ifdef DEV_NETMAP 3082 if (!NA(sc->vtnet_ifp)) { 3083 D("try to attach again"); 3084 vtnet_netmap_attach(sc); 3085 } 3086 #endif /* DEV_NETMAP */ 3087 3088 VTNET_CORE_LOCK(sc); 3089 vtnet_init_locked(sc); 3090 VTNET_CORE_UNLOCK(sc); 3091 } 3092 3093 static void 3094 vtnet_free_ctrl_vq(struct vtnet_softc *sc) 3095 { 3096 struct virtqueue *vq; 3097 3098 vq = sc->vtnet_ctrl_vq; 3099 3100 /* 3101 * The control virtqueue is only polled and therefore it should 3102 * already be empty. 3103 */ 3104 KASSERT(virtqueue_empty(vq), 3105 ("%s: ctrl vq %p not empty", __func__, vq)); 3106 } 3107 3108 static void 3109 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie, 3110 struct sglist *sg, int readable, int writable) 3111 { 3112 struct virtqueue *vq; 3113 3114 vq = sc->vtnet_ctrl_vq; 3115 3116 VTNET_CORE_LOCK_ASSERT(sc); 3117 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ, 3118 ("%s: CTRL_VQ feature not negotiated", __func__)); 3119 3120 if (!virtqueue_empty(vq)) 3121 return; 3122 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0) 3123 return; 3124 3125 /* 3126 * Poll for the response, but the command is likely already 3127 * done when we return from the notify. 3128 */ 3129 virtqueue_notify(vq); 3130 virtqueue_poll(vq, NULL); 3131 } 3132 3133 static int 3134 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr) 3135 { 3136 struct virtio_net_ctrl_hdr hdr __aligned(2); 3137 struct sglist_seg segs[3]; 3138 struct sglist sg; 3139 uint8_t ack; 3140 int error; 3141 3142 hdr.class = VIRTIO_NET_CTRL_MAC; 3143 hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET; 3144 ack = VIRTIO_NET_ERR; 3145 3146 sglist_init(&sg, 3, segs); 3147 error = 0; 3148 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 3149 error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN); 3150 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 3151 KASSERT(error == 0 && sg.sg_nseg == 3, 3152 ("%s: error %d adding set MAC msg to sglist", __func__, error)); 3153 3154 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 3155 3156 return (ack == VIRTIO_NET_OK ? 0 : EIO); 3157 } 3158 3159 static int 3160 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs) 3161 { 3162 struct sglist_seg segs[3]; 3163 struct sglist sg; 3164 struct { 3165 struct virtio_net_ctrl_hdr hdr; 3166 uint8_t pad1; 3167 struct virtio_net_ctrl_mq mq; 3168 uint8_t pad2; 3169 uint8_t ack; 3170 } s __aligned(2); 3171 int error; 3172 3173 s.hdr.class = VIRTIO_NET_CTRL_MQ; 3174 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; 3175 s.mq.virtqueue_pairs = npairs; 3176 s.ack = VIRTIO_NET_ERR; 3177 3178 sglist_init(&sg, 3, segs); 3179 error = 0; 3180 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr)); 3181 error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq)); 3182 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t)); 3183 KASSERT(error == 0 && sg.sg_nseg == 3, 3184 ("%s: error %d adding MQ message to sglist", __func__, error)); 3185 3186 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1); 3187 3188 return (s.ack == VIRTIO_NET_OK ? 0 : EIO); 3189 } 3190 3191 static int 3192 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on) 3193 { 3194 struct sglist_seg segs[3]; 3195 struct sglist sg; 3196 struct { 3197 struct virtio_net_ctrl_hdr hdr; 3198 uint8_t pad1; 3199 uint8_t onoff; 3200 uint8_t pad2; 3201 uint8_t ack; 3202 } s __aligned(2); 3203 int error; 3204 3205 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX, 3206 ("%s: CTRL_RX feature not negotiated", __func__)); 3207 3208 s.hdr.class = VIRTIO_NET_CTRL_RX; 3209 s.hdr.cmd = cmd; 3210 s.onoff = !!on; 3211 s.ack = VIRTIO_NET_ERR; 3212 3213 sglist_init(&sg, 3, segs); 3214 error = 0; 3215 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr)); 3216 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t)); 3217 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t)); 3218 KASSERT(error == 0 && sg.sg_nseg == 3, 3219 ("%s: error %d adding Rx message to sglist", __func__, error)); 3220 3221 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1); 3222 3223 return (s.ack == VIRTIO_NET_OK ? 0 : EIO); 3224 } 3225 3226 static int 3227 vtnet_set_promisc(struct vtnet_softc *sc, int on) 3228 { 3229 3230 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on)); 3231 } 3232 3233 static int 3234 vtnet_set_allmulti(struct vtnet_softc *sc, int on) 3235 { 3236 3237 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on)); 3238 } 3239 3240 /* 3241 * The device defaults to promiscuous mode for backwards compatibility. 3242 * Turn it off at attach time if possible. 3243 */ 3244 static void 3245 vtnet_attach_disable_promisc(struct vtnet_softc *sc) 3246 { 3247 struct ifnet *ifp; 3248 3249 ifp = sc->vtnet_ifp; 3250 3251 VTNET_CORE_LOCK(sc); 3252 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) { 3253 ifp->if_flags |= IFF_PROMISC; 3254 } else if (vtnet_set_promisc(sc, 0) != 0) { 3255 ifp->if_flags |= IFF_PROMISC; 3256 device_printf(sc->vtnet_dev, 3257 "cannot disable default promiscuous mode\n"); 3258 } 3259 VTNET_CORE_UNLOCK(sc); 3260 } 3261 3262 static void 3263 vtnet_rx_filter(struct vtnet_softc *sc) 3264 { 3265 device_t dev; 3266 struct ifnet *ifp; 3267 3268 dev = sc->vtnet_dev; 3269 ifp = sc->vtnet_ifp; 3270 3271 VTNET_CORE_LOCK_ASSERT(sc); 3272 3273 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0) 3274 device_printf(dev, "cannot %s promiscuous mode\n", 3275 ifp->if_flags & IFF_PROMISC ? "enable" : "disable"); 3276 3277 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0) 3278 device_printf(dev, "cannot %s all-multicast mode\n", 3279 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable"); 3280 } 3281 3282 static void 3283 vtnet_rx_filter_mac(struct vtnet_softc *sc) 3284 { 3285 struct virtio_net_ctrl_hdr hdr __aligned(2); 3286 struct vtnet_mac_filter *filter; 3287 struct sglist_seg segs[4]; 3288 struct sglist sg; 3289 struct ifnet *ifp; 3290 struct ifaddr *ifa; 3291 struct ifmultiaddr *ifma; 3292 int ucnt, mcnt, promisc, allmulti, error; 3293 uint8_t ack; 3294 3295 ifp = sc->vtnet_ifp; 3296 filter = sc->vtnet_mac_filter; 3297 ucnt = 0; 3298 mcnt = 0; 3299 promisc = 0; 3300 allmulti = 0; 3301 3302 VTNET_CORE_LOCK_ASSERT(sc); 3303 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX, 3304 ("%s: CTRL_RX feature not negotiated", __func__)); 3305 3306 /* Unicast MAC addresses: */ 3307 if_addr_rlock(ifp); 3308 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3309 if (ifa->ifa_addr->sa_family != AF_LINK) 3310 continue; 3311 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 3312 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0) 3313 continue; 3314 else if (ucnt == VTNET_MAX_MAC_ENTRIES) { 3315 promisc = 1; 3316 break; 3317 } 3318 3319 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 3320 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN); 3321 ucnt++; 3322 } 3323 if_addr_runlock(ifp); 3324 3325 if (promisc != 0) { 3326 filter->vmf_unicast.nentries = 0; 3327 if_printf(ifp, "more than %d MAC addresses assigned, " 3328 "falling back to promiscuous mode\n", 3329 VTNET_MAX_MAC_ENTRIES); 3330 } else 3331 filter->vmf_unicast.nentries = ucnt; 3332 3333 /* Multicast MAC addresses: */ 3334 if_maddr_rlock(ifp); 3335 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3336 if (ifma->ifma_addr->sa_family != AF_LINK) 3337 continue; 3338 else if (mcnt == VTNET_MAX_MAC_ENTRIES) { 3339 allmulti = 1; 3340 break; 3341 } 3342 3343 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 3344 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN); 3345 mcnt++; 3346 } 3347 if_maddr_runlock(ifp); 3348 3349 if (allmulti != 0) { 3350 filter->vmf_multicast.nentries = 0; 3351 if_printf(ifp, "more than %d multicast MAC addresses " 3352 "assigned, falling back to all-multicast mode\n", 3353 VTNET_MAX_MAC_ENTRIES); 3354 } else 3355 filter->vmf_multicast.nentries = mcnt; 3356 3357 if (promisc != 0 && allmulti != 0) 3358 goto out; 3359 3360 hdr.class = VIRTIO_NET_CTRL_MAC; 3361 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 3362 ack = VIRTIO_NET_ERR; 3363 3364 sglist_init(&sg, 4, segs); 3365 error = 0; 3366 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 3367 error |= sglist_append(&sg, &filter->vmf_unicast, 3368 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN); 3369 error |= sglist_append(&sg, &filter->vmf_multicast, 3370 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN); 3371 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 3372 KASSERT(error == 0 && sg.sg_nseg == 4, 3373 ("%s: error %d adding MAC filter msg to sglist", __func__, error)); 3374 3375 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 3376 3377 if (ack != VIRTIO_NET_OK) 3378 if_printf(ifp, "error setting host MAC filter table\n"); 3379 3380 out: 3381 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0) 3382 if_printf(ifp, "cannot enable promiscuous mode\n"); 3383 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0) 3384 if_printf(ifp, "cannot enable all-multicast mode\n"); 3385 } 3386 3387 static int 3388 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag) 3389 { 3390 struct sglist_seg segs[3]; 3391 struct sglist sg; 3392 struct { 3393 struct virtio_net_ctrl_hdr hdr; 3394 uint8_t pad1; 3395 uint16_t tag; 3396 uint8_t pad2; 3397 uint8_t ack; 3398 } s __aligned(2); 3399 int error; 3400 3401 s.hdr.class = VIRTIO_NET_CTRL_VLAN; 3402 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 3403 s.tag = tag; 3404 s.ack = VIRTIO_NET_ERR; 3405 3406 sglist_init(&sg, 3, segs); 3407 error = 0; 3408 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr)); 3409 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t)); 3410 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t)); 3411 KASSERT(error == 0 && sg.sg_nseg == 3, 3412 ("%s: error %d adding VLAN message to sglist", __func__, error)); 3413 3414 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1); 3415 3416 return (s.ack == VIRTIO_NET_OK ? 0 : EIO); 3417 } 3418 3419 static void 3420 vtnet_rx_filter_vlan(struct vtnet_softc *sc) 3421 { 3422 uint32_t w; 3423 uint16_t tag; 3424 int i, bit; 3425 3426 VTNET_CORE_LOCK_ASSERT(sc); 3427 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER, 3428 ("%s: VLAN_FILTER feature not negotiated", __func__)); 3429 3430 /* Enable the filter for each configured VLAN. */ 3431 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) { 3432 w = sc->vtnet_vlan_filter[i]; 3433 3434 while ((bit = ffs(w) - 1) != -1) { 3435 w &= ~(1 << bit); 3436 tag = sizeof(w) * CHAR_BIT * i + bit; 3437 3438 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) { 3439 device_printf(sc->vtnet_dev, 3440 "cannot enable VLAN %d filter\n", tag); 3441 } 3442 } 3443 } 3444 } 3445 3446 static void 3447 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag) 3448 { 3449 struct ifnet *ifp; 3450 int idx, bit; 3451 3452 ifp = sc->vtnet_ifp; 3453 idx = (tag >> 5) & 0x7F; 3454 bit = tag & 0x1F; 3455 3456 if (tag == 0 || tag > 4095) 3457 return; 3458 3459 VTNET_CORE_LOCK(sc); 3460 3461 if (add) 3462 sc->vtnet_vlan_filter[idx] |= (1 << bit); 3463 else 3464 sc->vtnet_vlan_filter[idx] &= ~(1 << bit); 3465 3466 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER && 3467 vtnet_exec_vlan_filter(sc, add, tag) != 0) { 3468 device_printf(sc->vtnet_dev, 3469 "cannot %s VLAN %d %s the host filter table\n", 3470 add ? "add" : "remove", tag, add ? "to" : "from"); 3471 } 3472 3473 VTNET_CORE_UNLOCK(sc); 3474 } 3475 3476 static void 3477 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag) 3478 { 3479 3480 if (ifp->if_softc != arg) 3481 return; 3482 3483 vtnet_update_vlan_filter(arg, 1, tag); 3484 } 3485 3486 static void 3487 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag) 3488 { 3489 3490 if (ifp->if_softc != arg) 3491 return; 3492 3493 vtnet_update_vlan_filter(arg, 0, tag); 3494 } 3495 3496 static int 3497 vtnet_is_link_up(struct vtnet_softc *sc) 3498 { 3499 device_t dev; 3500 struct ifnet *ifp; 3501 uint16_t status; 3502 3503 dev = sc->vtnet_dev; 3504 ifp = sc->vtnet_ifp; 3505 3506 if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0) 3507 status = VIRTIO_NET_S_LINK_UP; 3508 else 3509 status = virtio_read_dev_config_2(dev, 3510 offsetof(struct virtio_net_config, status)); 3511 3512 return ((status & VIRTIO_NET_S_LINK_UP) != 0); 3513 } 3514 3515 static void 3516 vtnet_update_link_status(struct vtnet_softc *sc) 3517 { 3518 struct ifnet *ifp; 3519 int link; 3520 3521 ifp = sc->vtnet_ifp; 3522 3523 VTNET_CORE_LOCK_ASSERT(sc); 3524 link = vtnet_is_link_up(sc); 3525 3526 /* Notify if the link status has changed. */ 3527 if (link != 0 && sc->vtnet_link_active == 0) { 3528 sc->vtnet_link_active = 1; 3529 if_link_state_change(ifp, LINK_STATE_UP); 3530 } else if (link == 0 && sc->vtnet_link_active != 0) { 3531 sc->vtnet_link_active = 0; 3532 if_link_state_change(ifp, LINK_STATE_DOWN); 3533 } 3534 } 3535 3536 static int 3537 vtnet_ifmedia_upd(struct ifnet *ifp) 3538 { 3539 struct vtnet_softc *sc; 3540 struct ifmedia *ifm; 3541 3542 sc = ifp->if_softc; 3543 ifm = &sc->vtnet_media; 3544 3545 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 3546 return (EINVAL); 3547 3548 return (0); 3549 } 3550 3551 static void 3552 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 3553 { 3554 struct vtnet_softc *sc; 3555 3556 sc = ifp->if_softc; 3557 3558 ifmr->ifm_status = IFM_AVALID; 3559 ifmr->ifm_active = IFM_ETHER; 3560 3561 VTNET_CORE_LOCK(sc); 3562 if (vtnet_is_link_up(sc) != 0) { 3563 ifmr->ifm_status |= IFM_ACTIVE; 3564 ifmr->ifm_active |= VTNET_MEDIATYPE; 3565 } else 3566 ifmr->ifm_active |= IFM_NONE; 3567 VTNET_CORE_UNLOCK(sc); 3568 } 3569 3570 static void 3571 vtnet_set_hwaddr(struct vtnet_softc *sc) 3572 { 3573 device_t dev; 3574 int i; 3575 3576 dev = sc->vtnet_dev; 3577 3578 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) { 3579 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0) 3580 device_printf(dev, "unable to set MAC address\n"); 3581 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) { 3582 for (i = 0; i < ETHER_ADDR_LEN; i++) { 3583 virtio_write_dev_config_1(dev, 3584 offsetof(struct virtio_net_config, mac) + i, 3585 sc->vtnet_hwaddr[i]); 3586 } 3587 } 3588 } 3589 3590 static void 3591 vtnet_get_hwaddr(struct vtnet_softc *sc) 3592 { 3593 device_t dev; 3594 int i; 3595 3596 dev = sc->vtnet_dev; 3597 3598 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) { 3599 /* 3600 * Generate a random locally administered unicast address. 3601 * 3602 * It would be nice to generate the same MAC address across 3603 * reboots, but it seems all the hosts currently available 3604 * support the MAC feature, so this isn't too important. 3605 */ 3606 sc->vtnet_hwaddr[0] = 0xB2; 3607 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0); 3608 vtnet_set_hwaddr(sc); 3609 return; 3610 } 3611 3612 for (i = 0; i < ETHER_ADDR_LEN; i++) { 3613 sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev, 3614 offsetof(struct virtio_net_config, mac) + i); 3615 } 3616 } 3617 3618 static void 3619 vtnet_vlan_tag_remove(struct mbuf *m) 3620 { 3621 struct ether_vlan_header *evh; 3622 3623 evh = mtod(m, struct ether_vlan_header *); 3624 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag); 3625 m->m_flags |= M_VLANTAG; 3626 3627 /* Strip the 802.1Q header. */ 3628 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN, 3629 ETHER_HDR_LEN - ETHER_TYPE_LEN); 3630 m_adj(m, ETHER_VLAN_ENCAP_LEN); 3631 } 3632 3633 static void 3634 vtnet_set_rx_process_limit(struct vtnet_softc *sc) 3635 { 3636 int limit; 3637 3638 limit = vtnet_tunable_int(sc, "rx_process_limit", 3639 vtnet_rx_process_limit); 3640 if (limit < 0) 3641 limit = INT_MAX; 3642 sc->vtnet_rx_process_limit = limit; 3643 } 3644 3645 static void 3646 vtnet_set_tx_intr_threshold(struct vtnet_softc *sc) 3647 { 3648 device_t dev; 3649 int size, thresh; 3650 3651 dev = sc->vtnet_dev; 3652 size = virtqueue_size(sc->vtnet_txqs[0].vtntx_vq); 3653 3654 /* 3655 * The Tx interrupt is disabled until the queue free count falls 3656 * below our threshold. Completed frames are drained from the Tx 3657 * virtqueue before transmitting new frames and in the watchdog 3658 * callout, so the frequency of Tx interrupts is greatly reduced, 3659 * at the cost of not freeing mbufs as quickly as they otherwise 3660 * would be. 3661 * 3662 * N.B. We assume all the Tx queues are the same size. 3663 */ 3664 thresh = size / 4; 3665 3666 /* 3667 * Without indirect descriptors, leave enough room for the most 3668 * segments we handle. 3669 */ 3670 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 && 3671 thresh < sc->vtnet_tx_nsegs) 3672 thresh = sc->vtnet_tx_nsegs; 3673 3674 sc->vtnet_tx_intr_thresh = thresh; 3675 } 3676 3677 static void 3678 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx, 3679 struct sysctl_oid_list *child, struct vtnet_rxq *rxq) 3680 { 3681 struct sysctl_oid *node; 3682 struct sysctl_oid_list *list; 3683 struct vtnet_rxq_stats *stats; 3684 char namebuf[16]; 3685 3686 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id); 3687 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf, 3688 CTLFLAG_RD, NULL, "Receive Queue"); 3689 list = SYSCTL_CHILDREN(node); 3690 3691 stats = &rxq->vtnrx_stats; 3692 3693 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD, 3694 &stats->vrxs_ipackets, "Receive packets"); 3695 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD, 3696 &stats->vrxs_ibytes, "Receive bytes"); 3697 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD, 3698 &stats->vrxs_iqdrops, "Receive drops"); 3699 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD, 3700 &stats->vrxs_ierrors, "Receive errors"); 3701 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD, 3702 &stats->vrxs_csum, "Receive checksum offloaded"); 3703 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD, 3704 &stats->vrxs_csum_failed, "Receive checksum offload failed"); 3705 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD, 3706 &stats->vrxs_rescheduled, 3707 "Receive interrupt handler rescheduled"); 3708 } 3709 3710 static void 3711 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx, 3712 struct sysctl_oid_list *child, struct vtnet_txq *txq) 3713 { 3714 struct sysctl_oid *node; 3715 struct sysctl_oid_list *list; 3716 struct vtnet_txq_stats *stats; 3717 char namebuf[16]; 3718 3719 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id); 3720 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf, 3721 CTLFLAG_RD, NULL, "Transmit Queue"); 3722 list = SYSCTL_CHILDREN(node); 3723 3724 stats = &txq->vtntx_stats; 3725 3726 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD, 3727 &stats->vtxs_opackets, "Transmit packets"); 3728 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD, 3729 &stats->vtxs_obytes, "Transmit bytes"); 3730 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD, 3731 &stats->vtxs_omcasts, "Transmit multicasts"); 3732 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD, 3733 &stats->vtxs_csum, "Transmit checksum offloaded"); 3734 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD, 3735 &stats->vtxs_tso, "Transmit segmentation offloaded"); 3736 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD, 3737 &stats->vtxs_rescheduled, 3738 "Transmit interrupt handler rescheduled"); 3739 } 3740 3741 static void 3742 vtnet_setup_queue_sysctl(struct vtnet_softc *sc) 3743 { 3744 device_t dev; 3745 struct sysctl_ctx_list *ctx; 3746 struct sysctl_oid *tree; 3747 struct sysctl_oid_list *child; 3748 int i; 3749 3750 dev = sc->vtnet_dev; 3751 ctx = device_get_sysctl_ctx(dev); 3752 tree = device_get_sysctl_tree(dev); 3753 child = SYSCTL_CHILDREN(tree); 3754 3755 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 3756 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]); 3757 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]); 3758 } 3759 } 3760 3761 static void 3762 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx, 3763 struct sysctl_oid_list *child, struct vtnet_softc *sc) 3764 { 3765 struct vtnet_statistics *stats; 3766 struct vtnet_rxq_stats rxaccum; 3767 struct vtnet_txq_stats txaccum; 3768 3769 vtnet_accum_stats(sc, &rxaccum, &txaccum); 3770 3771 stats = &sc->vtnet_stats; 3772 stats->rx_csum_offloaded = rxaccum.vrxs_csum; 3773 stats->rx_csum_failed = rxaccum.vrxs_csum_failed; 3774 stats->rx_task_rescheduled = rxaccum.vrxs_rescheduled; 3775 stats->tx_csum_offloaded = txaccum.vtxs_csum; 3776 stats->tx_tso_offloaded = txaccum.vtxs_tso; 3777 stats->tx_task_rescheduled = txaccum.vtxs_rescheduled; 3778 3779 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed", 3780 CTLFLAG_RD, &stats->mbuf_alloc_failed, 3781 "Mbuf cluster allocation failures"); 3782 3783 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large", 3784 CTLFLAG_RD, &stats->rx_frame_too_large, 3785 "Received frame larger than the mbuf chain"); 3786 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed", 3787 CTLFLAG_RD, &stats->rx_enq_replacement_failed, 3788 "Enqueuing the replacement receive mbuf failed"); 3789 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed", 3790 CTLFLAG_RD, &stats->rx_mergeable_failed, 3791 "Mergeable buffers receive failures"); 3792 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype", 3793 CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 3794 "Received checksum offloaded buffer with unsupported " 3795 "Ethernet type"); 3796 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto", 3797 CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 3798 "Received checksum offloaded buffer with incorrect IP protocol"); 3799 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset", 3800 CTLFLAG_RD, &stats->rx_csum_bad_offset, 3801 "Received checksum offloaded buffer with incorrect offset"); 3802 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto", 3803 CTLFLAG_RD, &stats->rx_csum_bad_proto, 3804 "Received checksum offloaded buffer with incorrect protocol"); 3805 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed", 3806 CTLFLAG_RD, &stats->rx_csum_failed, 3807 "Received buffer checksum offload failed"); 3808 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded", 3809 CTLFLAG_RD, &stats->rx_csum_offloaded, 3810 "Received buffer checksum offload succeeded"); 3811 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled", 3812 CTLFLAG_RD, &stats->rx_task_rescheduled, 3813 "Times the receive interrupt task rescheduled itself"); 3814 3815 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype", 3816 CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 3817 "Aborted transmit of checksum offloaded buffer with unknown " 3818 "Ethernet type"); 3819 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype", 3820 CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 3821 "Aborted transmit of TSO buffer with unknown Ethernet type"); 3822 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp", 3823 CTLFLAG_RD, &stats->tx_tso_not_tcp, 3824 "Aborted transmit of TSO buffer with non TCP protocol"); 3825 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged", 3826 CTLFLAG_RD, &stats->tx_defragged, 3827 "Transmit mbufs defragged"); 3828 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed", 3829 CTLFLAG_RD, &stats->tx_defrag_failed, 3830 "Aborted transmit of buffer because defrag failed"); 3831 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded", 3832 CTLFLAG_RD, &stats->tx_csum_offloaded, 3833 "Offloaded checksum of transmitted buffer"); 3834 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded", 3835 CTLFLAG_RD, &stats->tx_tso_offloaded, 3836 "Segmentation offload of transmitted buffer"); 3837 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled", 3838 CTLFLAG_RD, &stats->tx_task_rescheduled, 3839 "Times the transmit interrupt task rescheduled itself"); 3840 } 3841 3842 static void 3843 vtnet_setup_sysctl(struct vtnet_softc *sc) 3844 { 3845 device_t dev; 3846 struct sysctl_ctx_list *ctx; 3847 struct sysctl_oid *tree; 3848 struct sysctl_oid_list *child; 3849 3850 dev = sc->vtnet_dev; 3851 ctx = device_get_sysctl_ctx(dev); 3852 tree = device_get_sysctl_tree(dev); 3853 child = SYSCTL_CHILDREN(tree); 3854 3855 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs", 3856 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0, 3857 "Maximum number of supported virtqueue pairs"); 3858 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs", 3859 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0, 3860 "Number of active virtqueue pairs"); 3861 3862 vtnet_setup_stat_sysctl(ctx, child, sc); 3863 } 3864 3865 static int 3866 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq) 3867 { 3868 3869 return (virtqueue_enable_intr(rxq->vtnrx_vq)); 3870 } 3871 3872 static void 3873 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq) 3874 { 3875 3876 virtqueue_disable_intr(rxq->vtnrx_vq); 3877 } 3878 3879 static int 3880 vtnet_txq_enable_intr(struct vtnet_txq *txq) 3881 { 3882 struct virtqueue *vq; 3883 3884 vq = txq->vtntx_vq; 3885 3886 if (vtnet_txq_below_threshold(txq) != 0) 3887 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG)); 3888 3889 /* 3890 * The free count is above our threshold. Keep the Tx interrupt 3891 * disabled until the queue is fuller. 3892 */ 3893 return (0); 3894 } 3895 3896 static void 3897 vtnet_txq_disable_intr(struct vtnet_txq *txq) 3898 { 3899 3900 virtqueue_disable_intr(txq->vtntx_vq); 3901 } 3902 3903 static void 3904 vtnet_enable_rx_interrupts(struct vtnet_softc *sc) 3905 { 3906 int i; 3907 3908 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3909 vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]); 3910 } 3911 3912 static void 3913 vtnet_enable_tx_interrupts(struct vtnet_softc *sc) 3914 { 3915 int i; 3916 3917 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3918 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]); 3919 } 3920 3921 static void 3922 vtnet_enable_interrupts(struct vtnet_softc *sc) 3923 { 3924 3925 vtnet_enable_rx_interrupts(sc); 3926 vtnet_enable_tx_interrupts(sc); 3927 } 3928 3929 static void 3930 vtnet_disable_rx_interrupts(struct vtnet_softc *sc) 3931 { 3932 int i; 3933 3934 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3935 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]); 3936 } 3937 3938 static void 3939 vtnet_disable_tx_interrupts(struct vtnet_softc *sc) 3940 { 3941 int i; 3942 3943 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3944 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]); 3945 } 3946 3947 static void 3948 vtnet_disable_interrupts(struct vtnet_softc *sc) 3949 { 3950 3951 vtnet_disable_rx_interrupts(sc); 3952 vtnet_disable_tx_interrupts(sc); 3953 } 3954 3955 static int 3956 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def) 3957 { 3958 char path[64]; 3959 3960 snprintf(path, sizeof(path), 3961 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob); 3962 TUNABLE_INT_FETCH(path, &def); 3963 3964 return (def); 3965 } 3966