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