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