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