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 virtqueue *vq; 1646 struct mbuf *m, *m_tail; 1647 int len; 1648 1649 sc = rxq->vtnrx_sc; 1650 vq = rxq->vtnrx_vq; 1651 m_tail = m_head; 1652 1653 while (--nbufs > 0) { 1654 m = virtqueue_dequeue(vq, &len); 1655 if (m == NULL) { 1656 rxq->vtnrx_stats.vrxs_ierrors++; 1657 goto fail; 1658 } 1659 1660 if (vtnet_rxq_new_buf(rxq) != 0) { 1661 rxq->vtnrx_stats.vrxs_iqdrops++; 1662 vtnet_rxq_discard_buf(rxq, m); 1663 if (nbufs > 1) 1664 vtnet_rxq_discard_merged_bufs(rxq, nbufs); 1665 goto fail; 1666 } 1667 1668 if (m->m_len < len) 1669 len = m->m_len; 1670 1671 m->m_len = len; 1672 m->m_flags &= ~M_PKTHDR; 1673 1674 m_head->m_pkthdr.len += len; 1675 m_tail->m_next = m; 1676 m_tail = m; 1677 } 1678 1679 return (0); 1680 1681 fail: 1682 sc->vtnet_stats.rx_mergeable_failed++; 1683 m_freem(m_head); 1684 1685 return (1); 1686 } 1687 1688 static void 1689 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m, 1690 struct virtio_net_hdr *hdr) 1691 { 1692 struct vtnet_softc *sc; 1693 struct ifnet *ifp; 1694 struct ether_header *eh; 1695 1696 sc = rxq->vtnrx_sc; 1697 ifp = sc->vtnet_ifp; 1698 1699 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) { 1700 eh = mtod(m, struct ether_header *); 1701 if (eh->ether_type == htons(ETHERTYPE_VLAN)) { 1702 vtnet_vlan_tag_remove(m); 1703 /* 1704 * With the 802.1Q header removed, update the 1705 * checksum starting location accordingly. 1706 */ 1707 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) 1708 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN; 1709 } 1710 } 1711 1712 m->m_pkthdr.flowid = rxq->vtnrx_id; 1713 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE); 1714 1715 /* 1716 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum 1717 * distinction that Linux does. Need to reevaluate if performing 1718 * offloading for the NEEDS_CSUM case is really appropriate. 1719 */ 1720 if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM | 1721 VIRTIO_NET_HDR_F_DATA_VALID)) { 1722 if (vtnet_rxq_csum(rxq, m, hdr) == 0) 1723 rxq->vtnrx_stats.vrxs_csum++; 1724 else 1725 rxq->vtnrx_stats.vrxs_csum_failed++; 1726 } 1727 1728 rxq->vtnrx_stats.vrxs_ipackets++; 1729 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len; 1730 1731 VTNET_RXQ_UNLOCK(rxq); 1732 (*ifp->if_input)(ifp, m); 1733 VTNET_RXQ_LOCK(rxq); 1734 } 1735 1736 static int 1737 vtnet_rxq_eof(struct vtnet_rxq *rxq) 1738 { 1739 struct virtio_net_hdr lhdr, *hdr; 1740 struct vtnet_softc *sc; 1741 struct ifnet *ifp; 1742 struct virtqueue *vq; 1743 struct mbuf *m; 1744 struct virtio_net_hdr_mrg_rxbuf *mhdr; 1745 int len, deq, nbufs, adjsz, count; 1746 1747 sc = rxq->vtnrx_sc; 1748 vq = rxq->vtnrx_vq; 1749 ifp = sc->vtnet_ifp; 1750 hdr = &lhdr; 1751 deq = 0; 1752 count = sc->vtnet_rx_process_limit; 1753 1754 VTNET_RXQ_LOCK_ASSERT(rxq); 1755 1756 #ifdef DEV_NETMAP 1757 if (netmap_rx_irq(ifp, 0, &deq)) { 1758 return (FALSE); 1759 } 1760 #endif /* DEV_NETMAP */ 1761 1762 while (count-- > 0) { 1763 m = virtqueue_dequeue(vq, &len); 1764 if (m == NULL) 1765 break; 1766 deq++; 1767 1768 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) { 1769 rxq->vtnrx_stats.vrxs_ierrors++; 1770 vtnet_rxq_discard_buf(rxq, m); 1771 continue; 1772 } 1773 1774 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 1775 nbufs = 1; 1776 adjsz = sizeof(struct vtnet_rx_header); 1777 /* 1778 * Account for our pad inserted between the header 1779 * and the actual start of the frame. 1780 */ 1781 len += VTNET_RX_HEADER_PAD; 1782 } else { 1783 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *); 1784 nbufs = mhdr->num_buffers; 1785 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1786 } 1787 1788 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) { 1789 rxq->vtnrx_stats.vrxs_iqdrops++; 1790 vtnet_rxq_discard_buf(rxq, m); 1791 if (nbufs > 1) 1792 vtnet_rxq_discard_merged_bufs(rxq, nbufs); 1793 continue; 1794 } 1795 1796 m->m_pkthdr.len = len; 1797 m->m_pkthdr.rcvif = ifp; 1798 m->m_pkthdr.csum_flags = 0; 1799 1800 if (nbufs > 1) { 1801 /* Dequeue the rest of chain. */ 1802 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0) 1803 continue; 1804 } 1805 1806 /* 1807 * Save copy of header before we strip it. For both mergeable 1808 * and non-mergeable, the header is at the beginning of the 1809 * mbuf data. We no longer need num_buffers, so always use a 1810 * regular header. 1811 * 1812 * BMV: Is this memcpy() expensive? We know the mbuf data is 1813 * still valid even after the m_adj(). 1814 */ 1815 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr)); 1816 m_adj(m, adjsz); 1817 1818 vtnet_rxq_input(rxq, m, hdr); 1819 1820 /* Must recheck after dropping the Rx lock. */ 1821 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1822 break; 1823 } 1824 1825 if (deq > 0) 1826 virtqueue_notify(vq); 1827 1828 return (count > 0 ? 0 : EAGAIN); 1829 } 1830 1831 static void 1832 vtnet_rx_vq_intr(void *xrxq) 1833 { 1834 struct vtnet_softc *sc; 1835 struct vtnet_rxq *rxq; 1836 struct ifnet *ifp; 1837 int tries, more; 1838 1839 rxq = xrxq; 1840 sc = rxq->vtnrx_sc; 1841 ifp = sc->vtnet_ifp; 1842 tries = 0; 1843 1844 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) { 1845 /* 1846 * Ignore this interrupt. Either this is a spurious interrupt 1847 * or multiqueue without per-VQ MSIX so every queue needs to 1848 * be polled (a brain dead configuration we could try harder 1849 * to avoid). 1850 */ 1851 vtnet_rxq_disable_intr(rxq); 1852 return; 1853 } 1854 1855 VTNET_RXQ_LOCK(rxq); 1856 1857 again: 1858 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1859 VTNET_RXQ_UNLOCK(rxq); 1860 return; 1861 } 1862 1863 more = vtnet_rxq_eof(rxq); 1864 if (more || vtnet_rxq_enable_intr(rxq) != 0) { 1865 if (!more) 1866 vtnet_rxq_disable_intr(rxq); 1867 /* 1868 * This is an occasional condition or race (when !more), 1869 * so retry a few times before scheduling the taskqueue. 1870 */ 1871 if (tries++ < VTNET_INTR_DISABLE_RETRIES) 1872 goto again; 1873 1874 VTNET_RXQ_UNLOCK(rxq); 1875 rxq->vtnrx_stats.vrxs_rescheduled++; 1876 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask); 1877 } else 1878 VTNET_RXQ_UNLOCK(rxq); 1879 } 1880 1881 static void 1882 vtnet_rxq_tq_intr(void *xrxq, int pending) 1883 { 1884 struct vtnet_softc *sc; 1885 struct vtnet_rxq *rxq; 1886 struct ifnet *ifp; 1887 int more; 1888 1889 rxq = xrxq; 1890 sc = rxq->vtnrx_sc; 1891 ifp = sc->vtnet_ifp; 1892 1893 VTNET_RXQ_LOCK(rxq); 1894 1895 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1896 VTNET_RXQ_UNLOCK(rxq); 1897 return; 1898 } 1899 1900 more = vtnet_rxq_eof(rxq); 1901 if (more || vtnet_rxq_enable_intr(rxq) != 0) { 1902 if (!more) 1903 vtnet_rxq_disable_intr(rxq); 1904 rxq->vtnrx_stats.vrxs_rescheduled++; 1905 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask); 1906 } 1907 1908 VTNET_RXQ_UNLOCK(rxq); 1909 } 1910 1911 static int 1912 vtnet_txq_below_threshold(struct vtnet_txq *txq) 1913 { 1914 struct vtnet_softc *sc; 1915 struct virtqueue *vq; 1916 1917 sc = txq->vtntx_sc; 1918 vq = txq->vtntx_vq; 1919 1920 return (virtqueue_nfree(vq) <= sc->vtnet_tx_intr_thresh); 1921 } 1922 1923 static int 1924 vtnet_txq_notify(struct vtnet_txq *txq) 1925 { 1926 struct virtqueue *vq; 1927 1928 vq = txq->vtntx_vq; 1929 1930 txq->vtntx_watchdog = VTNET_TX_TIMEOUT; 1931 virtqueue_notify(vq); 1932 1933 if (vtnet_txq_enable_intr(txq) == 0) 1934 return (0); 1935 1936 /* 1937 * Drain frames that were completed since last checked. If this 1938 * causes the queue to go above the threshold, the caller should 1939 * continue transmitting. 1940 */ 1941 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) { 1942 virtqueue_disable_intr(vq); 1943 return (1); 1944 } 1945 1946 return (0); 1947 } 1948 1949 static void 1950 vtnet_txq_free_mbufs(struct vtnet_txq *txq) 1951 { 1952 struct virtqueue *vq; 1953 struct vtnet_tx_header *txhdr; 1954 int last; 1955 1956 vq = txq->vtntx_vq; 1957 last = 0; 1958 1959 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) { 1960 m_freem(txhdr->vth_mbuf); 1961 uma_zfree(vtnet_tx_header_zone, txhdr); 1962 } 1963 1964 KASSERT(virtqueue_empty(vq), 1965 ("%s: mbufs remaining in tx queue %p", __func__, txq)); 1966 } 1967 1968 /* 1969 * BMV: Much of this can go away once we finally have offsets in 1970 * the mbuf packet header. Bug andre@. 1971 */ 1972 static int 1973 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m, 1974 int *etype, int *proto, int *start) 1975 { 1976 struct vtnet_softc *sc; 1977 struct ether_vlan_header *evh; 1978 int offset; 1979 1980 sc = txq->vtntx_sc; 1981 1982 evh = mtod(m, struct ether_vlan_header *); 1983 if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 1984 /* BMV: We should handle nested VLAN tags too. */ 1985 *etype = ntohs(evh->evl_proto); 1986 offset = sizeof(struct ether_vlan_header); 1987 } else { 1988 *etype = ntohs(evh->evl_encap_proto); 1989 offset = sizeof(struct ether_header); 1990 } 1991 1992 switch (*etype) { 1993 #if defined(INET) 1994 case ETHERTYPE_IP: { 1995 struct ip *ip, iphdr; 1996 if (__predict_false(m->m_len < offset + sizeof(struct ip))) { 1997 m_copydata(m, offset, sizeof(struct ip), 1998 (caddr_t) &iphdr); 1999 ip = &iphdr; 2000 } else 2001 ip = (struct ip *)(m->m_data + offset); 2002 *proto = ip->ip_p; 2003 *start = offset + (ip->ip_hl << 2); 2004 break; 2005 } 2006 #endif 2007 #if defined(INET6) 2008 case ETHERTYPE_IPV6: 2009 *proto = -1; 2010 *start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto); 2011 /* Assert the network stack sent us a valid packet. */ 2012 KASSERT(*start > offset, 2013 ("%s: mbuf %p start %d offset %d proto %d", __func__, m, 2014 *start, offset, *proto)); 2015 break; 2016 #endif 2017 default: 2018 sc->vtnet_stats.tx_csum_bad_ethtype++; 2019 return (EINVAL); 2020 } 2021 2022 return (0); 2023 } 2024 2025 static int 2026 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type, 2027 int offset, struct virtio_net_hdr *hdr) 2028 { 2029 static struct timeval lastecn; 2030 static int curecn; 2031 struct vtnet_softc *sc; 2032 struct tcphdr *tcp, tcphdr; 2033 2034 sc = txq->vtntx_sc; 2035 2036 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) { 2037 m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr); 2038 tcp = &tcphdr; 2039 } else 2040 tcp = (struct tcphdr *)(m->m_data + offset); 2041 2042 hdr->hdr_len = offset + (tcp->th_off << 2); 2043 hdr->gso_size = m->m_pkthdr.tso_segsz; 2044 hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 : 2045 VIRTIO_NET_HDR_GSO_TCPV6; 2046 2047 if (tcp->th_flags & TH_CWR) { 2048 /* 2049 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD, 2050 * ECN support is not on a per-interface basis, but globally via 2051 * the net.inet.tcp.ecn.enable sysctl knob. The default is off. 2052 */ 2053 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) { 2054 if (ppsratecheck(&lastecn, &curecn, 1)) 2055 if_printf(sc->vtnet_ifp, 2056 "TSO with ECN not negotiated with host\n"); 2057 return (ENOTSUP); 2058 } 2059 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; 2060 } 2061 2062 txq->vtntx_stats.vtxs_tso++; 2063 2064 return (0); 2065 } 2066 2067 static struct mbuf * 2068 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m, 2069 struct virtio_net_hdr *hdr) 2070 { 2071 struct vtnet_softc *sc; 2072 int flags, etype, csum_start, proto, error; 2073 2074 sc = txq->vtntx_sc; 2075 flags = m->m_pkthdr.csum_flags; 2076 2077 error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start); 2078 if (error) 2079 goto drop; 2080 2081 if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) || 2082 (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) { 2083 /* 2084 * We could compare the IP protocol vs the CSUM_ flag too, 2085 * but that really should not be necessary. 2086 */ 2087 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM; 2088 hdr->csum_start = csum_start; 2089 hdr->csum_offset = m->m_pkthdr.csum_data; 2090 txq->vtntx_stats.vtxs_csum++; 2091 } 2092 2093 if (flags & CSUM_TSO) { 2094 if (__predict_false(proto != IPPROTO_TCP)) { 2095 /* Likely failed to correctly parse the mbuf. */ 2096 sc->vtnet_stats.tx_tso_not_tcp++; 2097 goto drop; 2098 } 2099 2100 KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM, 2101 ("%s: mbuf %p TSO without checksum offload %#x", 2102 __func__, m, flags)); 2103 2104 error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr); 2105 if (error) 2106 goto drop; 2107 } 2108 2109 return (m); 2110 2111 drop: 2112 m_freem(m); 2113 return (NULL); 2114 } 2115 2116 static int 2117 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head, 2118 struct vtnet_tx_header *txhdr) 2119 { 2120 struct vtnet_softc *sc; 2121 struct virtqueue *vq; 2122 struct sglist *sg; 2123 struct mbuf *m; 2124 int error; 2125 2126 sc = txq->vtntx_sc; 2127 vq = txq->vtntx_vq; 2128 sg = txq->vtntx_sg; 2129 m = *m_head; 2130 2131 sglist_reset(sg); 2132 error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size); 2133 KASSERT(error == 0 && sg->sg_nseg == 1, 2134 ("%s: error %d adding header to sglist", __func__, error)); 2135 2136 error = sglist_append_mbuf(sg, m); 2137 if (error) { 2138 m = m_defrag(m, M_NOWAIT); 2139 if (m == NULL) 2140 goto fail; 2141 2142 *m_head = m; 2143 sc->vtnet_stats.tx_defragged++; 2144 2145 error = sglist_append_mbuf(sg, m); 2146 if (error) 2147 goto fail; 2148 } 2149 2150 txhdr->vth_mbuf = m; 2151 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0); 2152 2153 return (error); 2154 2155 fail: 2156 sc->vtnet_stats.tx_defrag_failed++; 2157 m_freem(*m_head); 2158 *m_head = NULL; 2159 2160 return (ENOBUFS); 2161 } 2162 2163 static int 2164 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head) 2165 { 2166 struct vtnet_tx_header *txhdr; 2167 struct virtio_net_hdr *hdr; 2168 struct mbuf *m; 2169 int error; 2170 2171 m = *m_head; 2172 M_ASSERTPKTHDR(m); 2173 2174 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO); 2175 if (txhdr == NULL) { 2176 m_freem(m); 2177 *m_head = NULL; 2178 return (ENOMEM); 2179 } 2180 2181 /* 2182 * Always use the non-mergeable header, regardless if the feature 2183 * was negotiated. For transmit, num_buffers is always zero. The 2184 * vtnet_hdr_size is used to enqueue the correct header size. 2185 */ 2186 hdr = &txhdr->vth_uhdr.hdr; 2187 2188 if (m->m_flags & M_VLANTAG) { 2189 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); 2190 if ((*m_head = m) == NULL) { 2191 error = ENOBUFS; 2192 goto fail; 2193 } 2194 m->m_flags &= ~M_VLANTAG; 2195 } 2196 2197 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) { 2198 m = vtnet_txq_offload(txq, m, hdr); 2199 if ((*m_head = m) == NULL) { 2200 error = ENOBUFS; 2201 goto fail; 2202 } 2203 } 2204 2205 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr); 2206 if (error == 0) 2207 return (0); 2208 2209 fail: 2210 uma_zfree(vtnet_tx_header_zone, txhdr); 2211 2212 return (error); 2213 } 2214 2215 #ifdef VTNET_LEGACY_TX 2216 2217 static void 2218 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp) 2219 { 2220 struct vtnet_softc *sc; 2221 struct virtqueue *vq; 2222 struct mbuf *m0; 2223 int tries, enq; 2224 2225 sc = txq->vtntx_sc; 2226 vq = txq->vtntx_vq; 2227 tries = 0; 2228 2229 VTNET_TXQ_LOCK_ASSERT(txq); 2230 2231 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2232 sc->vtnet_link_active == 0) 2233 return; 2234 2235 vtnet_txq_eof(txq); 2236 2237 again: 2238 enq = 0; 2239 2240 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 2241 if (virtqueue_full(vq)) 2242 break; 2243 2244 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2245 if (m0 == NULL) 2246 break; 2247 2248 if (vtnet_txq_encap(txq, &m0) != 0) { 2249 if (m0 != NULL) 2250 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2251 break; 2252 } 2253 2254 enq++; 2255 ETHER_BPF_MTAP(ifp, m0); 2256 } 2257 2258 if (enq > 0 && vtnet_txq_notify(txq) != 0) { 2259 if (tries++ < VTNET_NOTIFY_RETRIES) 2260 goto again; 2261 2262 txq->vtntx_stats.vtxs_rescheduled++; 2263 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask); 2264 } 2265 } 2266 2267 static void 2268 vtnet_start(struct ifnet *ifp) 2269 { 2270 struct vtnet_softc *sc; 2271 struct vtnet_txq *txq; 2272 2273 sc = ifp->if_softc; 2274 txq = &sc->vtnet_txqs[0]; 2275 2276 VTNET_TXQ_LOCK(txq); 2277 vtnet_start_locked(txq, ifp); 2278 VTNET_TXQ_UNLOCK(txq); 2279 } 2280 2281 #else /* !VTNET_LEGACY_TX */ 2282 2283 static int 2284 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m) 2285 { 2286 struct vtnet_softc *sc; 2287 struct virtqueue *vq; 2288 struct buf_ring *br; 2289 struct ifnet *ifp; 2290 int enq, tries, error; 2291 2292 sc = txq->vtntx_sc; 2293 vq = txq->vtntx_vq; 2294 br = txq->vtntx_br; 2295 ifp = sc->vtnet_ifp; 2296 tries = 0; 2297 error = 0; 2298 2299 VTNET_TXQ_LOCK_ASSERT(txq); 2300 2301 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 2302 sc->vtnet_link_active == 0) { 2303 if (m != NULL) 2304 error = drbr_enqueue(ifp, br, m); 2305 return (error); 2306 } 2307 2308 if (m != NULL) { 2309 error = drbr_enqueue(ifp, br, m); 2310 if (error) 2311 return (error); 2312 } 2313 2314 vtnet_txq_eof(txq); 2315 2316 again: 2317 enq = 0; 2318 2319 while ((m = drbr_peek(ifp, br)) != NULL) { 2320 if (virtqueue_full(vq)) { 2321 drbr_putback(ifp, br, m); 2322 break; 2323 } 2324 2325 if (vtnet_txq_encap(txq, &m) != 0) { 2326 if (m != NULL) 2327 drbr_putback(ifp, br, m); 2328 else 2329 drbr_advance(ifp, br); 2330 break; 2331 } 2332 drbr_advance(ifp, br); 2333 2334 enq++; 2335 ETHER_BPF_MTAP(ifp, m); 2336 } 2337 2338 if (enq > 0 && vtnet_txq_notify(txq) != 0) { 2339 if (tries++ < VTNET_NOTIFY_RETRIES) 2340 goto again; 2341 2342 txq->vtntx_stats.vtxs_rescheduled++; 2343 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask); 2344 } 2345 2346 return (0); 2347 } 2348 2349 static int 2350 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m) 2351 { 2352 struct vtnet_softc *sc; 2353 struct vtnet_txq *txq; 2354 int i, npairs, error; 2355 2356 sc = ifp->if_softc; 2357 npairs = sc->vtnet_act_vq_pairs; 2358 2359 /* check if flowid is set */ 2360 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 2361 i = m->m_pkthdr.flowid % npairs; 2362 else 2363 i = curcpu % npairs; 2364 2365 txq = &sc->vtnet_txqs[i]; 2366 2367 if (VTNET_TXQ_TRYLOCK(txq) != 0) { 2368 error = vtnet_txq_mq_start_locked(txq, m); 2369 VTNET_TXQ_UNLOCK(txq); 2370 } else { 2371 error = drbr_enqueue(ifp, txq->vtntx_br, m); 2372 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask); 2373 } 2374 2375 return (error); 2376 } 2377 2378 static void 2379 vtnet_txq_tq_deferred(void *xtxq, int pending) 2380 { 2381 struct vtnet_softc *sc; 2382 struct vtnet_txq *txq; 2383 2384 txq = xtxq; 2385 sc = txq->vtntx_sc; 2386 2387 VTNET_TXQ_LOCK(txq); 2388 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br)) 2389 vtnet_txq_mq_start_locked(txq, NULL); 2390 VTNET_TXQ_UNLOCK(txq); 2391 } 2392 2393 #endif /* VTNET_LEGACY_TX */ 2394 2395 static void 2396 vtnet_txq_start(struct vtnet_txq *txq) 2397 { 2398 struct vtnet_softc *sc; 2399 struct ifnet *ifp; 2400 2401 sc = txq->vtntx_sc; 2402 ifp = sc->vtnet_ifp; 2403 2404 #ifdef VTNET_LEGACY_TX 2405 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2406 vtnet_start_locked(txq, ifp); 2407 #else 2408 if (!drbr_empty(ifp, txq->vtntx_br)) 2409 vtnet_txq_mq_start_locked(txq, NULL); 2410 #endif 2411 } 2412 2413 static void 2414 vtnet_txq_tq_intr(void *xtxq, int pending) 2415 { 2416 struct vtnet_softc *sc; 2417 struct vtnet_txq *txq; 2418 struct ifnet *ifp; 2419 2420 txq = xtxq; 2421 sc = txq->vtntx_sc; 2422 ifp = sc->vtnet_ifp; 2423 2424 VTNET_TXQ_LOCK(txq); 2425 2426 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2427 VTNET_TXQ_UNLOCK(txq); 2428 return; 2429 } 2430 2431 vtnet_txq_eof(txq); 2432 vtnet_txq_start(txq); 2433 2434 VTNET_TXQ_UNLOCK(txq); 2435 } 2436 2437 static int 2438 vtnet_txq_eof(struct vtnet_txq *txq) 2439 { 2440 struct virtqueue *vq; 2441 struct vtnet_tx_header *txhdr; 2442 struct mbuf *m; 2443 int deq; 2444 2445 vq = txq->vtntx_vq; 2446 deq = 0; 2447 VTNET_TXQ_LOCK_ASSERT(txq); 2448 2449 #ifdef DEV_NETMAP 2450 if (netmap_tx_irq(txq->vtntx_sc->vtnet_ifp, txq->vtntx_id)) { 2451 virtqueue_disable_intr(vq); // XXX luigi 2452 return 0; // XXX or 1 ? 2453 } 2454 #endif /* DEV_NETMAP */ 2455 2456 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) { 2457 m = txhdr->vth_mbuf; 2458 deq++; 2459 2460 txq->vtntx_stats.vtxs_opackets++; 2461 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len; 2462 if (m->m_flags & M_MCAST) 2463 txq->vtntx_stats.vtxs_omcasts++; 2464 2465 m_freem(m); 2466 uma_zfree(vtnet_tx_header_zone, txhdr); 2467 } 2468 2469 if (virtqueue_empty(vq)) 2470 txq->vtntx_watchdog = 0; 2471 2472 return (deq); 2473 } 2474 2475 static void 2476 vtnet_tx_vq_intr(void *xtxq) 2477 { 2478 struct vtnet_softc *sc; 2479 struct vtnet_txq *txq; 2480 struct ifnet *ifp; 2481 2482 txq = xtxq; 2483 sc = txq->vtntx_sc; 2484 ifp = sc->vtnet_ifp; 2485 2486 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) { 2487 /* 2488 * Ignore this interrupt. Either this is a spurious interrupt 2489 * or multiqueue without per-VQ MSIX so every queue needs to 2490 * be polled (a brain dead configuration we could try harder 2491 * to avoid). 2492 */ 2493 vtnet_txq_disable_intr(txq); 2494 return; 2495 } 2496 2497 VTNET_TXQ_LOCK(txq); 2498 2499 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2500 VTNET_TXQ_UNLOCK(txq); 2501 return; 2502 } 2503 2504 vtnet_txq_eof(txq); 2505 vtnet_txq_start(txq); 2506 2507 VTNET_TXQ_UNLOCK(txq); 2508 } 2509 2510 static void 2511 vtnet_tx_start_all(struct vtnet_softc *sc) 2512 { 2513 struct vtnet_txq *txq; 2514 int i; 2515 2516 VTNET_CORE_LOCK_ASSERT(sc); 2517 2518 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2519 txq = &sc->vtnet_txqs[i]; 2520 2521 VTNET_TXQ_LOCK(txq); 2522 vtnet_txq_start(txq); 2523 VTNET_TXQ_UNLOCK(txq); 2524 } 2525 } 2526 2527 #ifndef VTNET_LEGACY_TX 2528 static void 2529 vtnet_qflush(struct ifnet *ifp) 2530 { 2531 struct vtnet_softc *sc; 2532 struct vtnet_txq *txq; 2533 struct mbuf *m; 2534 int i; 2535 2536 sc = ifp->if_softc; 2537 2538 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2539 txq = &sc->vtnet_txqs[i]; 2540 2541 VTNET_TXQ_LOCK(txq); 2542 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL) 2543 m_freem(m); 2544 VTNET_TXQ_UNLOCK(txq); 2545 } 2546 2547 if_qflush(ifp); 2548 } 2549 #endif 2550 2551 static int 2552 vtnet_watchdog(struct vtnet_txq *txq) 2553 { 2554 struct ifnet *ifp; 2555 2556 ifp = txq->vtntx_sc->vtnet_ifp; 2557 2558 VTNET_TXQ_LOCK(txq); 2559 if (txq->vtntx_watchdog == 1) { 2560 /* 2561 * Only drain completed frames if the watchdog is about to 2562 * expire. If any frames were drained, there may be enough 2563 * free descriptors now available to transmit queued frames. 2564 * In that case, the timer will immediately be decremented 2565 * below, but the timeout is generous enough that should not 2566 * be a problem. 2567 */ 2568 if (vtnet_txq_eof(txq) != 0) 2569 vtnet_txq_start(txq); 2570 } 2571 2572 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) { 2573 VTNET_TXQ_UNLOCK(txq); 2574 return (0); 2575 } 2576 VTNET_TXQ_UNLOCK(txq); 2577 2578 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id); 2579 return (1); 2580 } 2581 2582 static void 2583 vtnet_accum_stats(struct vtnet_softc *sc, struct vtnet_rxq_stats *rxacc, 2584 struct vtnet_txq_stats *txacc) 2585 { 2586 2587 bzero(rxacc, sizeof(struct vtnet_rxq_stats)); 2588 bzero(txacc, sizeof(struct vtnet_txq_stats)); 2589 2590 for (int i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2591 struct vtnet_rxq_stats *rxst; 2592 struct vtnet_txq_stats *txst; 2593 2594 rxst = &sc->vtnet_rxqs[i].vtnrx_stats; 2595 rxacc->vrxs_ipackets += rxst->vrxs_ipackets; 2596 rxacc->vrxs_ibytes += rxst->vrxs_ibytes; 2597 rxacc->vrxs_iqdrops += rxst->vrxs_iqdrops; 2598 rxacc->vrxs_csum += rxst->vrxs_csum; 2599 rxacc->vrxs_csum_failed += rxst->vrxs_csum_failed; 2600 rxacc->vrxs_rescheduled += rxst->vrxs_rescheduled; 2601 2602 txst = &sc->vtnet_txqs[i].vtntx_stats; 2603 txacc->vtxs_opackets += txst->vtxs_opackets; 2604 txacc->vtxs_obytes += txst->vtxs_obytes; 2605 txacc->vtxs_csum += txst->vtxs_csum; 2606 txacc->vtxs_tso += txst->vtxs_tso; 2607 txacc->vtxs_rescheduled += txst->vtxs_rescheduled; 2608 } 2609 } 2610 2611 static uint64_t 2612 vtnet_get_counter(if_t ifp, ift_counter cnt) 2613 { 2614 struct vtnet_softc *sc; 2615 struct vtnet_rxq_stats rxaccum; 2616 struct vtnet_txq_stats txaccum; 2617 2618 sc = if_getsoftc(ifp); 2619 vtnet_accum_stats(sc, &rxaccum, &txaccum); 2620 2621 switch (cnt) { 2622 case IFCOUNTER_IPACKETS: 2623 return (rxaccum.vrxs_ipackets); 2624 case IFCOUNTER_IQDROPS: 2625 return (rxaccum.vrxs_iqdrops); 2626 case IFCOUNTER_IERRORS: 2627 return (rxaccum.vrxs_ierrors); 2628 case IFCOUNTER_OPACKETS: 2629 return (txaccum.vtxs_opackets); 2630 #ifndef VTNET_LEGACY_TX 2631 case IFCOUNTER_OBYTES: 2632 return (txaccum.vtxs_obytes); 2633 case IFCOUNTER_OMCASTS: 2634 return (txaccum.vtxs_omcasts); 2635 #endif 2636 default: 2637 return (if_get_counter_default(ifp, cnt)); 2638 } 2639 } 2640 2641 static void 2642 vtnet_tick(void *xsc) 2643 { 2644 struct vtnet_softc *sc; 2645 struct ifnet *ifp; 2646 int i, timedout; 2647 2648 sc = xsc; 2649 ifp = sc->vtnet_ifp; 2650 timedout = 0; 2651 2652 VTNET_CORE_LOCK_ASSERT(sc); 2653 2654 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 2655 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]); 2656 2657 if (timedout != 0) { 2658 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2659 vtnet_init_locked(sc); 2660 } else 2661 callout_schedule(&sc->vtnet_tick_ch, hz); 2662 } 2663 2664 static void 2665 vtnet_start_taskqueues(struct vtnet_softc *sc) 2666 { 2667 device_t dev; 2668 struct vtnet_rxq *rxq; 2669 struct vtnet_txq *txq; 2670 int i, error; 2671 2672 dev = sc->vtnet_dev; 2673 2674 /* 2675 * Errors here are very difficult to recover from - we cannot 2676 * easily fail because, if this is during boot, we will hang 2677 * when freeing any successfully started taskqueues because 2678 * the scheduler isn't up yet. 2679 * 2680 * Most drivers just ignore the return value - it only fails 2681 * with ENOMEM so an error is not likely. 2682 */ 2683 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2684 rxq = &sc->vtnet_rxqs[i]; 2685 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET, 2686 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id); 2687 if (error) { 2688 device_printf(dev, "failed to start rx taskq %d\n", 2689 rxq->vtnrx_id); 2690 } 2691 2692 txq = &sc->vtnet_txqs[i]; 2693 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET, 2694 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id); 2695 if (error) { 2696 device_printf(dev, "failed to start tx taskq %d\n", 2697 txq->vtntx_id); 2698 } 2699 } 2700 } 2701 2702 static void 2703 vtnet_free_taskqueues(struct vtnet_softc *sc) 2704 { 2705 struct vtnet_rxq *rxq; 2706 struct vtnet_txq *txq; 2707 int i; 2708 2709 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2710 rxq = &sc->vtnet_rxqs[i]; 2711 if (rxq->vtnrx_tq != NULL) { 2712 taskqueue_free(rxq->vtnrx_tq); 2713 rxq->vtnrx_vq = NULL; 2714 } 2715 2716 txq = &sc->vtnet_txqs[i]; 2717 if (txq->vtntx_tq != NULL) { 2718 taskqueue_free(txq->vtntx_tq); 2719 txq->vtntx_tq = NULL; 2720 } 2721 } 2722 } 2723 2724 static void 2725 vtnet_drain_taskqueues(struct vtnet_softc *sc) 2726 { 2727 struct vtnet_rxq *rxq; 2728 struct vtnet_txq *txq; 2729 int i; 2730 2731 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2732 rxq = &sc->vtnet_rxqs[i]; 2733 if (rxq->vtnrx_tq != NULL) 2734 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask); 2735 2736 txq = &sc->vtnet_txqs[i]; 2737 if (txq->vtntx_tq != NULL) { 2738 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask); 2739 #ifndef VTNET_LEGACY_TX 2740 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask); 2741 #endif 2742 } 2743 } 2744 } 2745 2746 static void 2747 vtnet_drain_rxtx_queues(struct vtnet_softc *sc) 2748 { 2749 struct vtnet_rxq *rxq; 2750 struct vtnet_txq *txq; 2751 int i; 2752 2753 #ifdef DEV_NETMAP 2754 if (nm_native_on(NA(sc->vtnet_ifp))) 2755 return; 2756 #endif /* DEV_NETMAP */ 2757 2758 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2759 rxq = &sc->vtnet_rxqs[i]; 2760 vtnet_rxq_free_mbufs(rxq); 2761 2762 txq = &sc->vtnet_txqs[i]; 2763 vtnet_txq_free_mbufs(txq); 2764 } 2765 } 2766 2767 static void 2768 vtnet_stop_rendezvous(struct vtnet_softc *sc) 2769 { 2770 struct vtnet_rxq *rxq; 2771 struct vtnet_txq *txq; 2772 int i; 2773 2774 /* 2775 * Lock and unlock the per-queue mutex so we known the stop 2776 * state is visible. Doing only the active queues should be 2777 * sufficient, but it does not cost much extra to do all the 2778 * queues. Note we hold the core mutex here too. 2779 */ 2780 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 2781 rxq = &sc->vtnet_rxqs[i]; 2782 VTNET_RXQ_LOCK(rxq); 2783 VTNET_RXQ_UNLOCK(rxq); 2784 2785 txq = &sc->vtnet_txqs[i]; 2786 VTNET_TXQ_LOCK(txq); 2787 VTNET_TXQ_UNLOCK(txq); 2788 } 2789 } 2790 2791 static void 2792 vtnet_stop(struct vtnet_softc *sc) 2793 { 2794 device_t dev; 2795 struct ifnet *ifp; 2796 2797 dev = sc->vtnet_dev; 2798 ifp = sc->vtnet_ifp; 2799 2800 VTNET_CORE_LOCK_ASSERT(sc); 2801 2802 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2803 sc->vtnet_link_active = 0; 2804 callout_stop(&sc->vtnet_tick_ch); 2805 2806 /* Only advisory. */ 2807 vtnet_disable_interrupts(sc); 2808 2809 /* 2810 * Stop the host adapter. This resets it to the pre-initialized 2811 * state. It will not generate any interrupts until after it is 2812 * reinitialized. 2813 */ 2814 virtio_stop(dev); 2815 vtnet_stop_rendezvous(sc); 2816 2817 /* Free any mbufs left in the virtqueues. */ 2818 vtnet_drain_rxtx_queues(sc); 2819 } 2820 2821 static int 2822 vtnet_virtio_reinit(struct vtnet_softc *sc) 2823 { 2824 device_t dev; 2825 struct ifnet *ifp; 2826 uint64_t features; 2827 int mask, error; 2828 2829 dev = sc->vtnet_dev; 2830 ifp = sc->vtnet_ifp; 2831 features = sc->vtnet_features; 2832 2833 mask = 0; 2834 #if defined(INET) 2835 mask |= IFCAP_RXCSUM; 2836 #endif 2837 #if defined (INET6) 2838 mask |= IFCAP_RXCSUM_IPV6; 2839 #endif 2840 2841 /* 2842 * Re-negotiate with the host, removing any disabled receive 2843 * features. Transmit features are disabled only on our side 2844 * via if_capenable and if_hwassist. 2845 */ 2846 2847 if (ifp->if_capabilities & mask) { 2848 /* 2849 * We require both IPv4 and IPv6 offloading to be enabled 2850 * in order to negotiated it: VirtIO does not distinguish 2851 * between the two. 2852 */ 2853 if ((ifp->if_capenable & mask) != mask) 2854 features &= ~VIRTIO_NET_F_GUEST_CSUM; 2855 } 2856 2857 if (ifp->if_capabilities & IFCAP_LRO) { 2858 if ((ifp->if_capenable & IFCAP_LRO) == 0) 2859 features &= ~VTNET_LRO_FEATURES; 2860 } 2861 2862 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) { 2863 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0) 2864 features &= ~VIRTIO_NET_F_CTRL_VLAN; 2865 } 2866 2867 error = virtio_reinit(dev, features); 2868 if (error) 2869 device_printf(dev, "virtio reinit error %d\n", error); 2870 2871 return (error); 2872 } 2873 2874 static void 2875 vtnet_init_rx_filters(struct vtnet_softc *sc) 2876 { 2877 struct ifnet *ifp; 2878 2879 ifp = sc->vtnet_ifp; 2880 2881 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) { 2882 /* Restore promiscuous and all-multicast modes. */ 2883 vtnet_rx_filter(sc); 2884 /* Restore filtered MAC addresses. */ 2885 vtnet_rx_filter_mac(sc); 2886 } 2887 2888 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) 2889 vtnet_rx_filter_vlan(sc); 2890 } 2891 2892 static int 2893 vtnet_init_rx_queues(struct vtnet_softc *sc) 2894 { 2895 device_t dev; 2896 struct vtnet_rxq *rxq; 2897 int i, clsize, error; 2898 2899 dev = sc->vtnet_dev; 2900 2901 /* 2902 * Use the new cluster size if one has been set (via a MTU 2903 * change). Otherwise, use the standard 2K clusters. 2904 * 2905 * BMV: It might make sense to use page sized clusters as 2906 * the default (depending on the features negotiated). 2907 */ 2908 if (sc->vtnet_rx_new_clsize != 0) { 2909 clsize = sc->vtnet_rx_new_clsize; 2910 sc->vtnet_rx_new_clsize = 0; 2911 } else 2912 clsize = MCLBYTES; 2913 2914 sc->vtnet_rx_clsize = clsize; 2915 sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize); 2916 2917 KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS || 2918 sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs, 2919 ("%s: too many rx mbufs %d for %d segments", __func__, 2920 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs)); 2921 2922 #ifdef DEV_NETMAP 2923 if (vtnet_netmap_init_rx_buffers(sc)) 2924 return 0; 2925 #endif /* DEV_NETMAP */ 2926 2927 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2928 rxq = &sc->vtnet_rxqs[i]; 2929 2930 /* Hold the lock to satisfy asserts. */ 2931 VTNET_RXQ_LOCK(rxq); 2932 error = vtnet_rxq_populate(rxq); 2933 VTNET_RXQ_UNLOCK(rxq); 2934 2935 if (error) { 2936 device_printf(dev, 2937 "cannot allocate mbufs for Rx queue %d\n", i); 2938 return (error); 2939 } 2940 } 2941 2942 return (0); 2943 } 2944 2945 static int 2946 vtnet_init_tx_queues(struct vtnet_softc *sc) 2947 { 2948 struct vtnet_txq *txq; 2949 int i; 2950 2951 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) { 2952 txq = &sc->vtnet_txqs[i]; 2953 txq->vtntx_watchdog = 0; 2954 } 2955 2956 return (0); 2957 } 2958 2959 static int 2960 vtnet_init_rxtx_queues(struct vtnet_softc *sc) 2961 { 2962 int error; 2963 2964 error = vtnet_init_rx_queues(sc); 2965 if (error) 2966 return (error); 2967 2968 error = vtnet_init_tx_queues(sc); 2969 if (error) 2970 return (error); 2971 2972 return (0); 2973 } 2974 2975 static void 2976 vtnet_set_active_vq_pairs(struct vtnet_softc *sc) 2977 { 2978 device_t dev; 2979 int npairs; 2980 2981 dev = sc->vtnet_dev; 2982 2983 if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) { 2984 MPASS(sc->vtnet_max_vq_pairs == 1); 2985 sc->vtnet_act_vq_pairs = 1; 2986 return; 2987 } 2988 2989 /* BMV: Just use the maximum configured for now. */ 2990 npairs = sc->vtnet_max_vq_pairs; 2991 2992 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) { 2993 device_printf(dev, 2994 "cannot set active queue pairs to %d\n", npairs); 2995 npairs = 1; 2996 } 2997 2998 sc->vtnet_act_vq_pairs = npairs; 2999 } 3000 3001 static int 3002 vtnet_reinit(struct vtnet_softc *sc) 3003 { 3004 struct ifnet *ifp; 3005 int error; 3006 3007 ifp = sc->vtnet_ifp; 3008 3009 /* Use the current MAC address. */ 3010 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN); 3011 vtnet_set_hwaddr(sc); 3012 3013 vtnet_set_active_vq_pairs(sc); 3014 3015 ifp->if_hwassist = 0; 3016 if (ifp->if_capenable & IFCAP_TXCSUM) 3017 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD; 3018 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6) 3019 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6; 3020 if (ifp->if_capenable & IFCAP_TSO4) 3021 ifp->if_hwassist |= CSUM_IP_TSO; 3022 if (ifp->if_capenable & IFCAP_TSO6) 3023 ifp->if_hwassist |= CSUM_IP6_TSO; 3024 3025 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) 3026 vtnet_init_rx_filters(sc); 3027 3028 error = vtnet_init_rxtx_queues(sc); 3029 if (error) 3030 return (error); 3031 3032 vtnet_enable_interrupts(sc); 3033 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3034 3035 return (0); 3036 } 3037 3038 static void 3039 vtnet_init_locked(struct vtnet_softc *sc) 3040 { 3041 device_t dev; 3042 struct ifnet *ifp; 3043 3044 dev = sc->vtnet_dev; 3045 ifp = sc->vtnet_ifp; 3046 3047 VTNET_CORE_LOCK_ASSERT(sc); 3048 3049 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 3050 return; 3051 3052 vtnet_stop(sc); 3053 3054 /* Reinitialize with the host. */ 3055 if (vtnet_virtio_reinit(sc) != 0) 3056 goto fail; 3057 3058 if (vtnet_reinit(sc) != 0) 3059 goto fail; 3060 3061 virtio_reinit_complete(dev); 3062 3063 vtnet_update_link_status(sc); 3064 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc); 3065 3066 return; 3067 3068 fail: 3069 vtnet_stop(sc); 3070 } 3071 3072 static void 3073 vtnet_init(void *xsc) 3074 { 3075 struct vtnet_softc *sc; 3076 3077 sc = xsc; 3078 3079 #ifdef DEV_NETMAP 3080 if (!NA(sc->vtnet_ifp)) { 3081 D("try to attach again"); 3082 vtnet_netmap_attach(sc); 3083 } 3084 #endif /* DEV_NETMAP */ 3085 3086 VTNET_CORE_LOCK(sc); 3087 vtnet_init_locked(sc); 3088 VTNET_CORE_UNLOCK(sc); 3089 } 3090 3091 static void 3092 vtnet_free_ctrl_vq(struct vtnet_softc *sc) 3093 { 3094 struct virtqueue *vq; 3095 3096 vq = sc->vtnet_ctrl_vq; 3097 3098 /* 3099 * The control virtqueue is only polled and therefore it should 3100 * already be empty. 3101 */ 3102 KASSERT(virtqueue_empty(vq), 3103 ("%s: ctrl vq %p not empty", __func__, vq)); 3104 } 3105 3106 static void 3107 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie, 3108 struct sglist *sg, int readable, int writable) 3109 { 3110 struct virtqueue *vq; 3111 3112 vq = sc->vtnet_ctrl_vq; 3113 3114 VTNET_CORE_LOCK_ASSERT(sc); 3115 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ, 3116 ("%s: CTRL_VQ feature not negotiated", __func__)); 3117 3118 if (!virtqueue_empty(vq)) 3119 return; 3120 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0) 3121 return; 3122 3123 /* 3124 * Poll for the response, but the command is likely already 3125 * done when we return from the notify. 3126 */ 3127 virtqueue_notify(vq); 3128 virtqueue_poll(vq, NULL); 3129 } 3130 3131 static int 3132 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr) 3133 { 3134 struct virtio_net_ctrl_hdr hdr __aligned(2); 3135 struct sglist_seg segs[3]; 3136 struct sglist sg; 3137 uint8_t ack; 3138 int error; 3139 3140 hdr.class = VIRTIO_NET_CTRL_MAC; 3141 hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET; 3142 ack = VIRTIO_NET_ERR; 3143 3144 sglist_init(&sg, 3, segs); 3145 error = 0; 3146 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 3147 error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN); 3148 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 3149 KASSERT(error == 0 && sg.sg_nseg == 3, 3150 ("%s: error %d adding set MAC msg to sglist", __func__, error)); 3151 3152 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 3153 3154 return (ack == VIRTIO_NET_OK ? 0 : EIO); 3155 } 3156 3157 static int 3158 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs) 3159 { 3160 struct sglist_seg segs[3]; 3161 struct sglist sg; 3162 struct { 3163 struct virtio_net_ctrl_hdr hdr; 3164 uint8_t pad1; 3165 struct virtio_net_ctrl_mq mq; 3166 uint8_t pad2; 3167 uint8_t ack; 3168 } s __aligned(2); 3169 int error; 3170 3171 s.hdr.class = VIRTIO_NET_CTRL_MQ; 3172 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; 3173 s.mq.virtqueue_pairs = npairs; 3174 s.ack = VIRTIO_NET_ERR; 3175 3176 sglist_init(&sg, 3, segs); 3177 error = 0; 3178 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr)); 3179 error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq)); 3180 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t)); 3181 KASSERT(error == 0 && sg.sg_nseg == 3, 3182 ("%s: error %d adding MQ message to sglist", __func__, error)); 3183 3184 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1); 3185 3186 return (s.ack == VIRTIO_NET_OK ? 0 : EIO); 3187 } 3188 3189 static int 3190 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on) 3191 { 3192 struct sglist_seg segs[3]; 3193 struct sglist sg; 3194 struct { 3195 struct virtio_net_ctrl_hdr hdr; 3196 uint8_t pad1; 3197 uint8_t onoff; 3198 uint8_t pad2; 3199 uint8_t ack; 3200 } s __aligned(2); 3201 int error; 3202 3203 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX, 3204 ("%s: CTRL_RX feature not negotiated", __func__)); 3205 3206 s.hdr.class = VIRTIO_NET_CTRL_RX; 3207 s.hdr.cmd = cmd; 3208 s.onoff = !!on; 3209 s.ack = VIRTIO_NET_ERR; 3210 3211 sglist_init(&sg, 3, segs); 3212 error = 0; 3213 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr)); 3214 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t)); 3215 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t)); 3216 KASSERT(error == 0 && sg.sg_nseg == 3, 3217 ("%s: error %d adding Rx message to sglist", __func__, error)); 3218 3219 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1); 3220 3221 return (s.ack == VIRTIO_NET_OK ? 0 : EIO); 3222 } 3223 3224 static int 3225 vtnet_set_promisc(struct vtnet_softc *sc, int on) 3226 { 3227 3228 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on)); 3229 } 3230 3231 static int 3232 vtnet_set_allmulti(struct vtnet_softc *sc, int on) 3233 { 3234 3235 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on)); 3236 } 3237 3238 /* 3239 * The device defaults to promiscuous mode for backwards compatibility. 3240 * Turn it off at attach time if possible. 3241 */ 3242 static void 3243 vtnet_attach_disable_promisc(struct vtnet_softc *sc) 3244 { 3245 struct ifnet *ifp; 3246 3247 ifp = sc->vtnet_ifp; 3248 3249 VTNET_CORE_LOCK(sc); 3250 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) { 3251 ifp->if_flags |= IFF_PROMISC; 3252 } else if (vtnet_set_promisc(sc, 0) != 0) { 3253 ifp->if_flags |= IFF_PROMISC; 3254 device_printf(sc->vtnet_dev, 3255 "cannot disable default promiscuous mode\n"); 3256 } 3257 VTNET_CORE_UNLOCK(sc); 3258 } 3259 3260 static void 3261 vtnet_rx_filter(struct vtnet_softc *sc) 3262 { 3263 device_t dev; 3264 struct ifnet *ifp; 3265 3266 dev = sc->vtnet_dev; 3267 ifp = sc->vtnet_ifp; 3268 3269 VTNET_CORE_LOCK_ASSERT(sc); 3270 3271 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0) 3272 device_printf(dev, "cannot %s promiscuous mode\n", 3273 ifp->if_flags & IFF_PROMISC ? "enable" : "disable"); 3274 3275 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0) 3276 device_printf(dev, "cannot %s all-multicast mode\n", 3277 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable"); 3278 } 3279 3280 static void 3281 vtnet_rx_filter_mac(struct vtnet_softc *sc) 3282 { 3283 struct virtio_net_ctrl_hdr hdr __aligned(2); 3284 struct vtnet_mac_filter *filter; 3285 struct sglist_seg segs[4]; 3286 struct sglist sg; 3287 struct ifnet *ifp; 3288 struct ifaddr *ifa; 3289 struct ifmultiaddr *ifma; 3290 int ucnt, mcnt, promisc, allmulti, error; 3291 uint8_t ack; 3292 3293 ifp = sc->vtnet_ifp; 3294 filter = sc->vtnet_mac_filter; 3295 ucnt = 0; 3296 mcnt = 0; 3297 promisc = 0; 3298 allmulti = 0; 3299 3300 VTNET_CORE_LOCK_ASSERT(sc); 3301 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX, 3302 ("%s: CTRL_RX feature not negotiated", __func__)); 3303 3304 /* Unicast MAC addresses: */ 3305 if_addr_rlock(ifp); 3306 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3307 if (ifa->ifa_addr->sa_family != AF_LINK) 3308 continue; 3309 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 3310 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0) 3311 continue; 3312 else if (ucnt == VTNET_MAX_MAC_ENTRIES) { 3313 promisc = 1; 3314 break; 3315 } 3316 3317 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 3318 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN); 3319 ucnt++; 3320 } 3321 if_addr_runlock(ifp); 3322 3323 if (promisc != 0) { 3324 filter->vmf_unicast.nentries = 0; 3325 if_printf(ifp, "more than %d MAC addresses assigned, " 3326 "falling back to promiscuous mode\n", 3327 VTNET_MAX_MAC_ENTRIES); 3328 } else 3329 filter->vmf_unicast.nentries = ucnt; 3330 3331 /* Multicast MAC addresses: */ 3332 if_maddr_rlock(ifp); 3333 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3334 if (ifma->ifma_addr->sa_family != AF_LINK) 3335 continue; 3336 else if (mcnt == VTNET_MAX_MAC_ENTRIES) { 3337 allmulti = 1; 3338 break; 3339 } 3340 3341 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 3342 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN); 3343 mcnt++; 3344 } 3345 if_maddr_runlock(ifp); 3346 3347 if (allmulti != 0) { 3348 filter->vmf_multicast.nentries = 0; 3349 if_printf(ifp, "more than %d multicast MAC addresses " 3350 "assigned, falling back to all-multicast mode\n", 3351 VTNET_MAX_MAC_ENTRIES); 3352 } else 3353 filter->vmf_multicast.nentries = mcnt; 3354 3355 if (promisc != 0 && allmulti != 0) 3356 goto out; 3357 3358 hdr.class = VIRTIO_NET_CTRL_MAC; 3359 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 3360 ack = VIRTIO_NET_ERR; 3361 3362 sglist_init(&sg, 4, segs); 3363 error = 0; 3364 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 3365 error |= sglist_append(&sg, &filter->vmf_unicast, 3366 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN); 3367 error |= sglist_append(&sg, &filter->vmf_multicast, 3368 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN); 3369 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 3370 KASSERT(error == 0 && sg.sg_nseg == 4, 3371 ("%s: error %d adding MAC filter msg to sglist", __func__, error)); 3372 3373 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 3374 3375 if (ack != VIRTIO_NET_OK) 3376 if_printf(ifp, "error setting host MAC filter table\n"); 3377 3378 out: 3379 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0) 3380 if_printf(ifp, "cannot enable promiscuous mode\n"); 3381 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0) 3382 if_printf(ifp, "cannot enable all-multicast mode\n"); 3383 } 3384 3385 static int 3386 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag) 3387 { 3388 struct sglist_seg segs[3]; 3389 struct sglist sg; 3390 struct { 3391 struct virtio_net_ctrl_hdr hdr; 3392 uint8_t pad1; 3393 uint16_t tag; 3394 uint8_t pad2; 3395 uint8_t ack; 3396 } s __aligned(2); 3397 int error; 3398 3399 s.hdr.class = VIRTIO_NET_CTRL_VLAN; 3400 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 3401 s.tag = tag; 3402 s.ack = VIRTIO_NET_ERR; 3403 3404 sglist_init(&sg, 3, segs); 3405 error = 0; 3406 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr)); 3407 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t)); 3408 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t)); 3409 KASSERT(error == 0 && sg.sg_nseg == 3, 3410 ("%s: error %d adding VLAN message to sglist", __func__, error)); 3411 3412 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1); 3413 3414 return (s.ack == VIRTIO_NET_OK ? 0 : EIO); 3415 } 3416 3417 static void 3418 vtnet_rx_filter_vlan(struct vtnet_softc *sc) 3419 { 3420 uint32_t w; 3421 uint16_t tag; 3422 int i, bit; 3423 3424 VTNET_CORE_LOCK_ASSERT(sc); 3425 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER, 3426 ("%s: VLAN_FILTER feature not negotiated", __func__)); 3427 3428 /* Enable the filter for each configured VLAN. */ 3429 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) { 3430 w = sc->vtnet_vlan_filter[i]; 3431 3432 while ((bit = ffs(w) - 1) != -1) { 3433 w &= ~(1 << bit); 3434 tag = sizeof(w) * CHAR_BIT * i + bit; 3435 3436 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) { 3437 device_printf(sc->vtnet_dev, 3438 "cannot enable VLAN %d filter\n", tag); 3439 } 3440 } 3441 } 3442 } 3443 3444 static void 3445 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag) 3446 { 3447 struct ifnet *ifp; 3448 int idx, bit; 3449 3450 ifp = sc->vtnet_ifp; 3451 idx = (tag >> 5) & 0x7F; 3452 bit = tag & 0x1F; 3453 3454 if (tag == 0 || tag > 4095) 3455 return; 3456 3457 VTNET_CORE_LOCK(sc); 3458 3459 if (add) 3460 sc->vtnet_vlan_filter[idx] |= (1 << bit); 3461 else 3462 sc->vtnet_vlan_filter[idx] &= ~(1 << bit); 3463 3464 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER && 3465 vtnet_exec_vlan_filter(sc, add, tag) != 0) { 3466 device_printf(sc->vtnet_dev, 3467 "cannot %s VLAN %d %s the host filter table\n", 3468 add ? "add" : "remove", tag, add ? "to" : "from"); 3469 } 3470 3471 VTNET_CORE_UNLOCK(sc); 3472 } 3473 3474 static void 3475 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag) 3476 { 3477 3478 if (ifp->if_softc != arg) 3479 return; 3480 3481 vtnet_update_vlan_filter(arg, 1, tag); 3482 } 3483 3484 static void 3485 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag) 3486 { 3487 3488 if (ifp->if_softc != arg) 3489 return; 3490 3491 vtnet_update_vlan_filter(arg, 0, tag); 3492 } 3493 3494 static int 3495 vtnet_is_link_up(struct vtnet_softc *sc) 3496 { 3497 device_t dev; 3498 struct ifnet *ifp; 3499 uint16_t status; 3500 3501 dev = sc->vtnet_dev; 3502 ifp = sc->vtnet_ifp; 3503 3504 if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0) 3505 status = VIRTIO_NET_S_LINK_UP; 3506 else 3507 status = virtio_read_dev_config_2(dev, 3508 offsetof(struct virtio_net_config, status)); 3509 3510 return ((status & VIRTIO_NET_S_LINK_UP) != 0); 3511 } 3512 3513 static void 3514 vtnet_update_link_status(struct vtnet_softc *sc) 3515 { 3516 struct ifnet *ifp; 3517 int link; 3518 3519 ifp = sc->vtnet_ifp; 3520 3521 VTNET_CORE_LOCK_ASSERT(sc); 3522 link = vtnet_is_link_up(sc); 3523 3524 /* Notify if the link status has changed. */ 3525 if (link != 0 && sc->vtnet_link_active == 0) { 3526 sc->vtnet_link_active = 1; 3527 if_link_state_change(ifp, LINK_STATE_UP); 3528 } else if (link == 0 && sc->vtnet_link_active != 0) { 3529 sc->vtnet_link_active = 0; 3530 if_link_state_change(ifp, LINK_STATE_DOWN); 3531 } 3532 } 3533 3534 static int 3535 vtnet_ifmedia_upd(struct ifnet *ifp) 3536 { 3537 struct vtnet_softc *sc; 3538 struct ifmedia *ifm; 3539 3540 sc = ifp->if_softc; 3541 ifm = &sc->vtnet_media; 3542 3543 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 3544 return (EINVAL); 3545 3546 return (0); 3547 } 3548 3549 static void 3550 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 3551 { 3552 struct vtnet_softc *sc; 3553 3554 sc = ifp->if_softc; 3555 3556 ifmr->ifm_status = IFM_AVALID; 3557 ifmr->ifm_active = IFM_ETHER; 3558 3559 VTNET_CORE_LOCK(sc); 3560 if (vtnet_is_link_up(sc) != 0) { 3561 ifmr->ifm_status |= IFM_ACTIVE; 3562 ifmr->ifm_active |= VTNET_MEDIATYPE; 3563 } else 3564 ifmr->ifm_active |= IFM_NONE; 3565 VTNET_CORE_UNLOCK(sc); 3566 } 3567 3568 static void 3569 vtnet_set_hwaddr(struct vtnet_softc *sc) 3570 { 3571 device_t dev; 3572 int i; 3573 3574 dev = sc->vtnet_dev; 3575 3576 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) { 3577 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0) 3578 device_printf(dev, "unable to set MAC address\n"); 3579 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) { 3580 for (i = 0; i < ETHER_ADDR_LEN; i++) { 3581 virtio_write_dev_config_1(dev, 3582 offsetof(struct virtio_net_config, mac) + i, 3583 sc->vtnet_hwaddr[i]); 3584 } 3585 } 3586 } 3587 3588 static void 3589 vtnet_get_hwaddr(struct vtnet_softc *sc) 3590 { 3591 device_t dev; 3592 int i; 3593 3594 dev = sc->vtnet_dev; 3595 3596 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) { 3597 /* 3598 * Generate a random locally administered unicast address. 3599 * 3600 * It would be nice to generate the same MAC address across 3601 * reboots, but it seems all the hosts currently available 3602 * support the MAC feature, so this isn't too important. 3603 */ 3604 sc->vtnet_hwaddr[0] = 0xB2; 3605 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0); 3606 vtnet_set_hwaddr(sc); 3607 return; 3608 } 3609 3610 for (i = 0; i < ETHER_ADDR_LEN; i++) { 3611 sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev, 3612 offsetof(struct virtio_net_config, mac) + i); 3613 } 3614 } 3615 3616 static void 3617 vtnet_vlan_tag_remove(struct mbuf *m) 3618 { 3619 struct ether_vlan_header *evh; 3620 3621 evh = mtod(m, struct ether_vlan_header *); 3622 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag); 3623 m->m_flags |= M_VLANTAG; 3624 3625 /* Strip the 802.1Q header. */ 3626 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN, 3627 ETHER_HDR_LEN - ETHER_TYPE_LEN); 3628 m_adj(m, ETHER_VLAN_ENCAP_LEN); 3629 } 3630 3631 static void 3632 vtnet_set_rx_process_limit(struct vtnet_softc *sc) 3633 { 3634 int limit; 3635 3636 limit = vtnet_tunable_int(sc, "rx_process_limit", 3637 vtnet_rx_process_limit); 3638 if (limit < 0) 3639 limit = INT_MAX; 3640 sc->vtnet_rx_process_limit = limit; 3641 } 3642 3643 static void 3644 vtnet_set_tx_intr_threshold(struct vtnet_softc *sc) 3645 { 3646 int size, thresh; 3647 3648 size = virtqueue_size(sc->vtnet_txqs[0].vtntx_vq); 3649 3650 /* 3651 * The Tx interrupt is disabled until the queue free count falls 3652 * below our threshold. Completed frames are drained from the Tx 3653 * virtqueue before transmitting new frames and in the watchdog 3654 * callout, so the frequency of Tx interrupts is greatly reduced, 3655 * at the cost of not freeing mbufs as quickly as they otherwise 3656 * would be. 3657 * 3658 * N.B. We assume all the Tx queues are the same size. 3659 */ 3660 thresh = size / 4; 3661 3662 /* 3663 * Without indirect descriptors, leave enough room for the most 3664 * segments we handle. 3665 */ 3666 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 && 3667 thresh < sc->vtnet_tx_nsegs) 3668 thresh = sc->vtnet_tx_nsegs; 3669 3670 sc->vtnet_tx_intr_thresh = thresh; 3671 } 3672 3673 static void 3674 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx, 3675 struct sysctl_oid_list *child, struct vtnet_rxq *rxq) 3676 { 3677 struct sysctl_oid *node; 3678 struct sysctl_oid_list *list; 3679 struct vtnet_rxq_stats *stats; 3680 char namebuf[16]; 3681 3682 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id); 3683 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf, 3684 CTLFLAG_RD, NULL, "Receive Queue"); 3685 list = SYSCTL_CHILDREN(node); 3686 3687 stats = &rxq->vtnrx_stats; 3688 3689 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD, 3690 &stats->vrxs_ipackets, "Receive packets"); 3691 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD, 3692 &stats->vrxs_ibytes, "Receive bytes"); 3693 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD, 3694 &stats->vrxs_iqdrops, "Receive drops"); 3695 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD, 3696 &stats->vrxs_ierrors, "Receive errors"); 3697 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD, 3698 &stats->vrxs_csum, "Receive checksum offloaded"); 3699 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD, 3700 &stats->vrxs_csum_failed, "Receive checksum offload failed"); 3701 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD, 3702 &stats->vrxs_rescheduled, 3703 "Receive interrupt handler rescheduled"); 3704 } 3705 3706 static void 3707 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx, 3708 struct sysctl_oid_list *child, struct vtnet_txq *txq) 3709 { 3710 struct sysctl_oid *node; 3711 struct sysctl_oid_list *list; 3712 struct vtnet_txq_stats *stats; 3713 char namebuf[16]; 3714 3715 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id); 3716 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf, 3717 CTLFLAG_RD, NULL, "Transmit Queue"); 3718 list = SYSCTL_CHILDREN(node); 3719 3720 stats = &txq->vtntx_stats; 3721 3722 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD, 3723 &stats->vtxs_opackets, "Transmit packets"); 3724 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD, 3725 &stats->vtxs_obytes, "Transmit bytes"); 3726 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD, 3727 &stats->vtxs_omcasts, "Transmit multicasts"); 3728 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD, 3729 &stats->vtxs_csum, "Transmit checksum offloaded"); 3730 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD, 3731 &stats->vtxs_tso, "Transmit segmentation offloaded"); 3732 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD, 3733 &stats->vtxs_rescheduled, 3734 "Transmit interrupt handler rescheduled"); 3735 } 3736 3737 static void 3738 vtnet_setup_queue_sysctl(struct vtnet_softc *sc) 3739 { 3740 device_t dev; 3741 struct sysctl_ctx_list *ctx; 3742 struct sysctl_oid *tree; 3743 struct sysctl_oid_list *child; 3744 int i; 3745 3746 dev = sc->vtnet_dev; 3747 ctx = device_get_sysctl_ctx(dev); 3748 tree = device_get_sysctl_tree(dev); 3749 child = SYSCTL_CHILDREN(tree); 3750 3751 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) { 3752 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]); 3753 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]); 3754 } 3755 } 3756 3757 static void 3758 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx, 3759 struct sysctl_oid_list *child, struct vtnet_softc *sc) 3760 { 3761 struct vtnet_statistics *stats; 3762 struct vtnet_rxq_stats rxaccum; 3763 struct vtnet_txq_stats txaccum; 3764 3765 vtnet_accum_stats(sc, &rxaccum, &txaccum); 3766 3767 stats = &sc->vtnet_stats; 3768 stats->rx_csum_offloaded = rxaccum.vrxs_csum; 3769 stats->rx_csum_failed = rxaccum.vrxs_csum_failed; 3770 stats->rx_task_rescheduled = rxaccum.vrxs_rescheduled; 3771 stats->tx_csum_offloaded = txaccum.vtxs_csum; 3772 stats->tx_tso_offloaded = txaccum.vtxs_tso; 3773 stats->tx_task_rescheduled = txaccum.vtxs_rescheduled; 3774 3775 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed", 3776 CTLFLAG_RD, &stats->mbuf_alloc_failed, 3777 "Mbuf cluster allocation failures"); 3778 3779 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large", 3780 CTLFLAG_RD, &stats->rx_frame_too_large, 3781 "Received frame larger than the mbuf chain"); 3782 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed", 3783 CTLFLAG_RD, &stats->rx_enq_replacement_failed, 3784 "Enqueuing the replacement receive mbuf failed"); 3785 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed", 3786 CTLFLAG_RD, &stats->rx_mergeable_failed, 3787 "Mergeable buffers receive failures"); 3788 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype", 3789 CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 3790 "Received checksum offloaded buffer with unsupported " 3791 "Ethernet type"); 3792 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto", 3793 CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 3794 "Received checksum offloaded buffer with incorrect IP protocol"); 3795 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset", 3796 CTLFLAG_RD, &stats->rx_csum_bad_offset, 3797 "Received checksum offloaded buffer with incorrect offset"); 3798 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto", 3799 CTLFLAG_RD, &stats->rx_csum_bad_proto, 3800 "Received checksum offloaded buffer with incorrect protocol"); 3801 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed", 3802 CTLFLAG_RD, &stats->rx_csum_failed, 3803 "Received buffer checksum offload failed"); 3804 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded", 3805 CTLFLAG_RD, &stats->rx_csum_offloaded, 3806 "Received buffer checksum offload succeeded"); 3807 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled", 3808 CTLFLAG_RD, &stats->rx_task_rescheduled, 3809 "Times the receive interrupt task rescheduled itself"); 3810 3811 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype", 3812 CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 3813 "Aborted transmit of checksum offloaded buffer with unknown " 3814 "Ethernet type"); 3815 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype", 3816 CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 3817 "Aborted transmit of TSO buffer with unknown Ethernet type"); 3818 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp", 3819 CTLFLAG_RD, &stats->tx_tso_not_tcp, 3820 "Aborted transmit of TSO buffer with non TCP protocol"); 3821 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged", 3822 CTLFLAG_RD, &stats->tx_defragged, 3823 "Transmit mbufs defragged"); 3824 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed", 3825 CTLFLAG_RD, &stats->tx_defrag_failed, 3826 "Aborted transmit of buffer because defrag failed"); 3827 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded", 3828 CTLFLAG_RD, &stats->tx_csum_offloaded, 3829 "Offloaded checksum of transmitted buffer"); 3830 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded", 3831 CTLFLAG_RD, &stats->tx_tso_offloaded, 3832 "Segmentation offload of transmitted buffer"); 3833 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled", 3834 CTLFLAG_RD, &stats->tx_task_rescheduled, 3835 "Times the transmit interrupt task rescheduled itself"); 3836 } 3837 3838 static void 3839 vtnet_setup_sysctl(struct vtnet_softc *sc) 3840 { 3841 device_t dev; 3842 struct sysctl_ctx_list *ctx; 3843 struct sysctl_oid *tree; 3844 struct sysctl_oid_list *child; 3845 3846 dev = sc->vtnet_dev; 3847 ctx = device_get_sysctl_ctx(dev); 3848 tree = device_get_sysctl_tree(dev); 3849 child = SYSCTL_CHILDREN(tree); 3850 3851 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs", 3852 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0, 3853 "Maximum number of supported virtqueue pairs"); 3854 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs", 3855 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0, 3856 "Number of active virtqueue pairs"); 3857 3858 vtnet_setup_stat_sysctl(ctx, child, sc); 3859 } 3860 3861 static int 3862 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq) 3863 { 3864 3865 return (virtqueue_enable_intr(rxq->vtnrx_vq)); 3866 } 3867 3868 static void 3869 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq) 3870 { 3871 3872 virtqueue_disable_intr(rxq->vtnrx_vq); 3873 } 3874 3875 static int 3876 vtnet_txq_enable_intr(struct vtnet_txq *txq) 3877 { 3878 struct virtqueue *vq; 3879 3880 vq = txq->vtntx_vq; 3881 3882 if (vtnet_txq_below_threshold(txq) != 0) 3883 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG)); 3884 3885 /* 3886 * The free count is above our threshold. Keep the Tx interrupt 3887 * disabled until the queue is fuller. 3888 */ 3889 return (0); 3890 } 3891 3892 static void 3893 vtnet_txq_disable_intr(struct vtnet_txq *txq) 3894 { 3895 3896 virtqueue_disable_intr(txq->vtntx_vq); 3897 } 3898 3899 static void 3900 vtnet_enable_rx_interrupts(struct vtnet_softc *sc) 3901 { 3902 int i; 3903 3904 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3905 vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]); 3906 } 3907 3908 static void 3909 vtnet_enable_tx_interrupts(struct vtnet_softc *sc) 3910 { 3911 int i; 3912 3913 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3914 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]); 3915 } 3916 3917 static void 3918 vtnet_enable_interrupts(struct vtnet_softc *sc) 3919 { 3920 3921 vtnet_enable_rx_interrupts(sc); 3922 vtnet_enable_tx_interrupts(sc); 3923 } 3924 3925 static void 3926 vtnet_disable_rx_interrupts(struct vtnet_softc *sc) 3927 { 3928 int i; 3929 3930 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3931 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]); 3932 } 3933 3934 static void 3935 vtnet_disable_tx_interrupts(struct vtnet_softc *sc) 3936 { 3937 int i; 3938 3939 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) 3940 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]); 3941 } 3942 3943 static void 3944 vtnet_disable_interrupts(struct vtnet_softc *sc) 3945 { 3946 3947 vtnet_disable_rx_interrupts(sc); 3948 vtnet_disable_tx_interrupts(sc); 3949 } 3950 3951 static int 3952 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def) 3953 { 3954 char path[64]; 3955 3956 snprintf(path, sizeof(path), 3957 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob); 3958 TUNABLE_INT_FETCH(path, &def); 3959 3960 return (def); 3961 } 3962