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