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