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