1 /*- 2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.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$"); 31 32 #ifdef HAVE_KERNEL_OPTION_HEADERS 33 #include "opt_device_polling.h" 34 #endif 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/sockio.h> 40 #include <sys/mbuf.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/socket.h> 44 #include <sys/sysctl.h> 45 #include <sys/taskqueue.h> 46 #include <sys/random.h> 47 #include <sys/sglist.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 51 #include <vm/uma.h> 52 53 #include <net/ethernet.h> 54 #include <net/if.h> 55 #include <net/if_arp.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/if_media.h> 59 #include <net/if_vlan_var.h> 60 61 #include <net/bpf.h> 62 63 #include <netinet/in_systm.h> 64 #include <netinet/in.h> 65 #include <netinet/ip.h> 66 #include <netinet/ip6.h> 67 #include <netinet/udp.h> 68 #include <netinet/tcp.h> 69 #include <netinet/sctp.h> 70 71 #include <machine/bus.h> 72 #include <machine/resource.h> 73 #include <sys/bus.h> 74 #include <sys/rman.h> 75 76 #include <dev/virtio/virtio.h> 77 #include <dev/virtio/virtqueue.h> 78 #include <dev/virtio/network/virtio_net.h> 79 #include <dev/virtio/network/if_vtnetvar.h> 80 81 #include "virtio_if.h" 82 83 static int vtnet_modevent(module_t, int, void *); 84 85 static int vtnet_probe(device_t); 86 static int vtnet_attach(device_t); 87 static int vtnet_detach(device_t); 88 static int vtnet_suspend(device_t); 89 static int vtnet_resume(device_t); 90 static int vtnet_shutdown(device_t); 91 static int vtnet_config_change(device_t); 92 93 static void vtnet_negotiate_features(struct vtnet_softc *); 94 static int vtnet_alloc_virtqueues(struct vtnet_softc *); 95 static void vtnet_get_hwaddr(struct vtnet_softc *); 96 static void vtnet_set_hwaddr(struct vtnet_softc *); 97 static int vtnet_is_link_up(struct vtnet_softc *); 98 static void vtnet_update_link_status(struct vtnet_softc *); 99 static void vtnet_watchdog(struct vtnet_softc *); 100 static void vtnet_config_change_task(void *, int); 101 static int vtnet_change_mtu(struct vtnet_softc *, int); 102 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t); 103 104 static int vtnet_init_rx_vq(struct vtnet_softc *); 105 static void vtnet_free_rx_mbufs(struct vtnet_softc *); 106 static void vtnet_free_tx_mbufs(struct vtnet_softc *); 107 static void vtnet_free_ctrl_vq(struct vtnet_softc *); 108 109 #ifdef DEVICE_POLLING 110 static poll_handler_t vtnet_poll; 111 #endif 112 113 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int, 114 struct mbuf **); 115 static int vtnet_replace_rxbuf(struct vtnet_softc *, 116 struct mbuf *, int); 117 static int vtnet_newbuf(struct vtnet_softc *); 118 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int); 119 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *); 120 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *); 121 static void vtnet_vlan_tag_remove(struct mbuf *); 122 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *, 123 struct virtio_net_hdr *); 124 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int); 125 static int vtnet_rxeof(struct vtnet_softc *, int, int *); 126 static void vtnet_rx_intr_task(void *, int); 127 static int vtnet_rx_vq_intr(void *); 128 129 static void vtnet_txeof(struct vtnet_softc *); 130 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *, 131 struct virtio_net_hdr *); 132 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **, 133 struct vtnet_tx_header *); 134 static int vtnet_encap(struct vtnet_softc *, struct mbuf **); 135 static void vtnet_start_locked(struct ifnet *); 136 static void vtnet_start(struct ifnet *); 137 static void vtnet_tick(void *); 138 static void vtnet_tx_intr_task(void *, int); 139 static int vtnet_tx_vq_intr(void *); 140 141 static void vtnet_stop(struct vtnet_softc *); 142 static int vtnet_reinit(struct vtnet_softc *); 143 static void vtnet_init_locked(struct vtnet_softc *); 144 static void vtnet_init(void *); 145 146 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *, 147 struct sglist *, int, int); 148 149 static void vtnet_rx_filter(struct vtnet_softc *sc); 150 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int); 151 static int vtnet_set_promisc(struct vtnet_softc *, int); 152 static int vtnet_set_allmulti(struct vtnet_softc *, int); 153 static void vtnet_rx_filter_mac(struct vtnet_softc *); 154 155 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t); 156 static void vtnet_rx_filter_vlan(struct vtnet_softc *); 157 static void vtnet_set_vlan_filter(struct vtnet_softc *, int, uint16_t); 158 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t); 159 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t); 160 161 static int vtnet_ifmedia_upd(struct ifnet *); 162 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *); 163 164 static void vtnet_add_statistics(struct vtnet_softc *); 165 166 static int vtnet_enable_rx_intr(struct vtnet_softc *); 167 static int vtnet_enable_tx_intr(struct vtnet_softc *); 168 static void vtnet_disable_rx_intr(struct vtnet_softc *); 169 static void vtnet_disable_tx_intr(struct vtnet_softc *); 170 171 /* Tunables. */ 172 static int vtnet_csum_disable = 0; 173 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable); 174 static int vtnet_tso_disable = 0; 175 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable); 176 static int vtnet_lro_disable = 0; 177 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable); 178 179 /* 180 * Reducing the number of transmit completed interrupts can 181 * improve performance. To do so, the define below keeps the 182 * Tx vq interrupt disabled and adds calls to vtnet_txeof() 183 * in the start and watchdog paths. The price to pay for this 184 * is the m_free'ing of transmitted mbufs may be delayed until 185 * the watchdog fires. 186 */ 187 #define VTNET_TX_INTR_MODERATION 188 189 static uma_zone_t vtnet_tx_header_zone; 190 191 static struct virtio_feature_desc vtnet_feature_desc[] = { 192 { VIRTIO_NET_F_CSUM, "TxChecksum" }, 193 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" }, 194 { VIRTIO_NET_F_MAC, "MacAddress" }, 195 { VIRTIO_NET_F_GSO, "TxAllGSO" }, 196 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" }, 197 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" }, 198 { VIRTIO_NET_F_GUEST_ECN, "RxECN" }, 199 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" }, 200 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" }, 201 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" }, 202 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" }, 203 { VIRTIO_NET_F_HOST_UFO, "TxUFO" }, 204 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" }, 205 { VIRTIO_NET_F_STATUS, "Status" }, 206 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" }, 207 { VIRTIO_NET_F_CTRL_RX, "RxMode" }, 208 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" }, 209 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" }, 210 211 { 0, NULL } 212 }; 213 214 static device_method_t vtnet_methods[] = { 215 /* Device methods. */ 216 DEVMETHOD(device_probe, vtnet_probe), 217 DEVMETHOD(device_attach, vtnet_attach), 218 DEVMETHOD(device_detach, vtnet_detach), 219 DEVMETHOD(device_suspend, vtnet_suspend), 220 DEVMETHOD(device_resume, vtnet_resume), 221 DEVMETHOD(device_shutdown, vtnet_shutdown), 222 223 /* VirtIO methods. */ 224 DEVMETHOD(virtio_config_change, vtnet_config_change), 225 226 DEVMETHOD_END 227 }; 228 229 static driver_t vtnet_driver = { 230 "vtnet", 231 vtnet_methods, 232 sizeof(struct vtnet_softc) 233 }; 234 static devclass_t vtnet_devclass; 235 236 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass, 237 vtnet_modevent, 0); 238 MODULE_VERSION(vtnet, 1); 239 MODULE_DEPEND(vtnet, virtio, 1, 1, 1); 240 241 static int 242 vtnet_modevent(module_t mod, int type, void *unused) 243 { 244 int error; 245 246 error = 0; 247 248 switch (type) { 249 case MOD_LOAD: 250 vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr", 251 sizeof(struct vtnet_tx_header), 252 NULL, NULL, NULL, NULL, 0, 0); 253 break; 254 case MOD_QUIESCE: 255 case MOD_UNLOAD: 256 if (uma_zone_get_cur(vtnet_tx_header_zone) > 0) 257 error = EBUSY; 258 else if (type == MOD_UNLOAD) { 259 uma_zdestroy(vtnet_tx_header_zone); 260 vtnet_tx_header_zone = NULL; 261 } 262 break; 263 case MOD_SHUTDOWN: 264 break; 265 default: 266 error = EOPNOTSUPP; 267 break; 268 } 269 270 return (error); 271 } 272 273 static int 274 vtnet_probe(device_t dev) 275 { 276 277 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK) 278 return (ENXIO); 279 280 device_set_desc(dev, "VirtIO Networking Adapter"); 281 282 return (BUS_PROBE_DEFAULT); 283 } 284 285 static int 286 vtnet_attach(device_t dev) 287 { 288 struct vtnet_softc *sc; 289 struct ifnet *ifp; 290 int tx_size, error; 291 292 sc = device_get_softc(dev); 293 sc->vtnet_dev = dev; 294 295 VTNET_LOCK_INIT(sc); 296 callout_init_mtx(&sc->vtnet_tick_ch, VTNET_MTX(sc), 0); 297 298 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd, 299 vtnet_ifmedia_sts); 300 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL); 301 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE); 302 303 vtnet_add_statistics(sc); 304 305 virtio_set_feature_desc(dev, vtnet_feature_desc); 306 vtnet_negotiate_features(sc); 307 308 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) { 309 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS; 310 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf); 311 } else 312 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr); 313 314 sc->vtnet_rx_mbuf_size = MCLBYTES; 315 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc); 316 317 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) { 318 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ; 319 320 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX)) { 321 sc->vtnet_mac_filter = malloc( 322 sizeof(struct vtnet_mac_filter), M_DEVBUF, 323 M_NOWAIT | M_ZERO); 324 if (sc->vtnet_mac_filter == NULL) { 325 device_printf(dev, 326 "cannot allocate mac filter table\n"); 327 error = ENOMEM; 328 goto fail; 329 } 330 331 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX; 332 } 333 334 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN)) 335 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER; 336 } 337 338 vtnet_get_hwaddr(sc); 339 340 error = vtnet_alloc_virtqueues(sc); 341 if (error) { 342 device_printf(dev, "cannot allocate virtqueues\n"); 343 goto fail; 344 } 345 346 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER); 347 if (ifp == NULL) { 348 device_printf(dev, "cannot allocate ifnet structure\n"); 349 error = ENOSPC; 350 goto fail; 351 } 352 353 ifp->if_softc = sc; 354 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 355 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 356 ifp->if_init = vtnet_init; 357 ifp->if_start = vtnet_start; 358 ifp->if_ioctl = vtnet_ioctl; 359 360 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq); 361 sc->vtnet_rx_process_limit = sc->vtnet_rx_size; 362 363 tx_size = virtqueue_size(sc->vtnet_tx_vq); 364 sc->vtnet_tx_size = tx_size; 365 IFQ_SET_MAXLEN(&ifp->if_snd, tx_size - 1); 366 ifp->if_snd.ifq_drv_maxlen = tx_size - 1; 367 IFQ_SET_READY(&ifp->if_snd); 368 369 ether_ifattach(ifp, sc->vtnet_hwaddr); 370 371 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) 372 ifp->if_capabilities |= IFCAP_LINKSTATE; 373 374 /* Tell the upper layer(s) we support long frames. */ 375 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 376 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU; 377 378 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) { 379 ifp->if_capabilities |= IFCAP_TXCSUM; 380 381 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4)) 382 ifp->if_capabilities |= IFCAP_TSO4; 383 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6)) 384 ifp->if_capabilities |= IFCAP_TSO6; 385 if (ifp->if_capabilities & IFCAP_TSO) 386 ifp->if_capabilities |= IFCAP_VLAN_HWTSO; 387 388 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN)) 389 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN; 390 } 391 392 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) { 393 ifp->if_capabilities |= IFCAP_RXCSUM; 394 395 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) || 396 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6)) 397 ifp->if_capabilities |= IFCAP_LRO; 398 } 399 400 if (ifp->if_capabilities & IFCAP_HWCSUM) { 401 /* 402 * VirtIO does not support VLAN tagging, but we can fake 403 * it by inserting and removing the 802.1Q header during 404 * transmit and receive. We are then able to do checksum 405 * offloading of VLAN frames. 406 */ 407 ifp->if_capabilities |= 408 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM; 409 } 410 411 ifp->if_capenable = ifp->if_capabilities; 412 413 /* 414 * Capabilities after here are not enabled by default. 415 */ 416 417 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) { 418 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER; 419 420 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config, 421 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST); 422 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig, 423 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST); 424 } 425 426 #ifdef DEVICE_POLLING 427 ifp->if_capabilities |= IFCAP_POLLING; 428 #endif 429 430 TASK_INIT(&sc->vtnet_rx_intr_task, 0, vtnet_rx_intr_task, sc); 431 TASK_INIT(&sc->vtnet_tx_intr_task, 0, vtnet_tx_intr_task, sc); 432 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc); 433 434 sc->vtnet_tq = taskqueue_create_fast("vtnet_taskq", M_NOWAIT, 435 taskqueue_thread_enqueue, &sc->vtnet_tq); 436 if (sc->vtnet_tq == NULL) { 437 error = ENOMEM; 438 device_printf(dev, "cannot allocate taskqueue\n"); 439 ether_ifdetach(ifp); 440 goto fail; 441 } 442 443 error = virtio_setup_intr(dev, INTR_TYPE_NET); 444 if (error) { 445 device_printf(dev, "cannot setup virtqueue interrupts\n"); 446 ether_ifdetach(ifp); 447 goto fail; 448 } 449 450 taskqueue_start_threads(&sc->vtnet_tq, 1, PI_NET, "%s taskq", 451 device_get_nameunit(dev)); 452 453 /* 454 * Device defaults to promiscuous mode for backwards 455 * compatibility. Turn it off if possible. 456 */ 457 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) { 458 VTNET_LOCK(sc); 459 if (vtnet_set_promisc(sc, 0) != 0) { 460 ifp->if_flags |= IFF_PROMISC; 461 device_printf(dev, 462 "cannot disable promiscuous mode\n"); 463 } 464 VTNET_UNLOCK(sc); 465 } else 466 ifp->if_flags |= IFF_PROMISC; 467 468 fail: 469 if (error) 470 vtnet_detach(dev); 471 472 return (error); 473 } 474 475 static int 476 vtnet_detach(device_t dev) 477 { 478 struct vtnet_softc *sc; 479 struct ifnet *ifp; 480 481 sc = device_get_softc(dev); 482 ifp = sc->vtnet_ifp; 483 484 KASSERT(mtx_initialized(VTNET_MTX(sc)), 485 ("vtnet mutex not initialized")); 486 487 #ifdef DEVICE_POLLING 488 if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING) 489 ether_poll_deregister(ifp); 490 #endif 491 492 if (device_is_attached(dev)) { 493 VTNET_LOCK(sc); 494 vtnet_stop(sc); 495 VTNET_UNLOCK(sc); 496 497 callout_drain(&sc->vtnet_tick_ch); 498 taskqueue_drain(taskqueue_fast, &sc->vtnet_cfgchg_task); 499 500 ether_ifdetach(ifp); 501 } 502 503 if (sc->vtnet_tq != NULL) { 504 taskqueue_drain(sc->vtnet_tq, &sc->vtnet_rx_intr_task); 505 taskqueue_drain(sc->vtnet_tq, &sc->vtnet_tx_intr_task); 506 taskqueue_free(sc->vtnet_tq); 507 sc->vtnet_tq = NULL; 508 } 509 510 if (sc->vtnet_vlan_attach != NULL) { 511 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach); 512 sc->vtnet_vlan_attach = NULL; 513 } 514 if (sc->vtnet_vlan_detach != NULL) { 515 EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach); 516 sc->vtnet_vlan_detach = NULL; 517 } 518 519 if (sc->vtnet_mac_filter != NULL) { 520 free(sc->vtnet_mac_filter, M_DEVBUF); 521 sc->vtnet_mac_filter = NULL; 522 } 523 524 if (ifp != NULL) { 525 if_free(ifp); 526 sc->vtnet_ifp = NULL; 527 } 528 529 if (sc->vtnet_rx_vq != NULL) 530 vtnet_free_rx_mbufs(sc); 531 if (sc->vtnet_tx_vq != NULL) 532 vtnet_free_tx_mbufs(sc); 533 if (sc->vtnet_ctrl_vq != NULL) 534 vtnet_free_ctrl_vq(sc); 535 536 ifmedia_removeall(&sc->vtnet_media); 537 VTNET_LOCK_DESTROY(sc); 538 539 return (0); 540 } 541 542 static int 543 vtnet_suspend(device_t dev) 544 { 545 struct vtnet_softc *sc; 546 547 sc = device_get_softc(dev); 548 549 VTNET_LOCK(sc); 550 vtnet_stop(sc); 551 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED; 552 VTNET_UNLOCK(sc); 553 554 return (0); 555 } 556 557 static int 558 vtnet_resume(device_t dev) 559 { 560 struct vtnet_softc *sc; 561 struct ifnet *ifp; 562 563 sc = device_get_softc(dev); 564 ifp = sc->vtnet_ifp; 565 566 VTNET_LOCK(sc); 567 if (ifp->if_flags & IFF_UP) 568 vtnet_init_locked(sc); 569 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED; 570 VTNET_UNLOCK(sc); 571 572 return (0); 573 } 574 575 static int 576 vtnet_shutdown(device_t dev) 577 { 578 579 /* 580 * Suspend already does all of what we need to 581 * do here; we just never expect to be resumed. 582 */ 583 return (vtnet_suspend(dev)); 584 } 585 586 static int 587 vtnet_config_change(device_t dev) 588 { 589 struct vtnet_softc *sc; 590 591 sc = device_get_softc(dev); 592 593 taskqueue_enqueue_fast(taskqueue_fast, &sc->vtnet_cfgchg_task); 594 595 return (1); 596 } 597 598 static void 599 vtnet_negotiate_features(struct vtnet_softc *sc) 600 { 601 device_t dev; 602 uint64_t mask, features; 603 604 dev = sc->vtnet_dev; 605 mask = 0; 606 607 if (vtnet_csum_disable) 608 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM; 609 610 /* 611 * TSO and LRO are only available when their corresponding 612 * checksum offload feature is also negotiated. 613 */ 614 615 if (vtnet_csum_disable || vtnet_tso_disable) 616 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 | 617 VIRTIO_NET_F_HOST_ECN; 618 619 if (vtnet_csum_disable || vtnet_lro_disable) 620 mask |= VTNET_LRO_FEATURES; 621 622 features = VTNET_FEATURES & ~mask; 623 #ifdef VTNET_TX_INTR_MODERATION 624 features |= VIRTIO_F_NOTIFY_ON_EMPTY; 625 #endif 626 sc->vtnet_features = virtio_negotiate_features(dev, features); 627 628 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0 && 629 virtio_with_feature(dev, VTNET_LRO_FEATURES)) { 630 /* 631 * LRO without mergeable buffers requires special care. This 632 * is not ideal because every receive buffer must be large 633 * enough to hold the maximum TCP packet, the Ethernet header, 634 * and the vtnet_rx_header. This requires up to 34 descriptors 635 * when using MCLBYTES clusters. If we do not have indirect 636 * descriptors, LRO is disabled since the virtqueue will not 637 * be able to contain very many receive buffers. 638 */ 639 if (virtio_with_feature(dev, 640 VIRTIO_RING_F_INDIRECT_DESC) == 0) { 641 device_printf(dev, 642 "LRO disabled due to lack of both mergeable " 643 "buffers and indirect descriptors\n"); 644 645 sc->vtnet_features = virtio_negotiate_features(dev, 646 features & ~VTNET_LRO_FEATURES); 647 } else 648 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG; 649 } 650 } 651 652 static int 653 vtnet_alloc_virtqueues(struct vtnet_softc *sc) 654 { 655 device_t dev; 656 struct vq_alloc_info vq_info[3]; 657 int nvqs, rxsegs; 658 659 dev = sc->vtnet_dev; 660 nvqs = 2; 661 662 /* 663 * Indirect descriptors are not needed for the Rx 664 * virtqueue when mergeable buffers are negotiated. 665 * The header is placed inline with the data, not 666 * in a separate descriptor, and mbuf clusters are 667 * always physically contiguous. 668 */ 669 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 670 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ? 671 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS; 672 } else 673 rxsegs = 0; 674 675 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs, 676 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq, 677 "%s receive", device_get_nameunit(dev)); 678 679 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS, 680 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq, 681 "%s transmit", device_get_nameunit(dev)); 682 683 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) { 684 nvqs++; 685 686 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL, 687 &sc->vtnet_ctrl_vq, "%s control", 688 device_get_nameunit(dev)); 689 } 690 691 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info)); 692 } 693 694 static void 695 vtnet_get_hwaddr(struct vtnet_softc *sc) 696 { 697 device_t dev; 698 699 dev = sc->vtnet_dev; 700 701 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) { 702 virtio_read_device_config(dev, 703 offsetof(struct virtio_net_config, mac), 704 sc->vtnet_hwaddr, ETHER_ADDR_LEN); 705 } else { 706 /* Generate random locally administered unicast address. */ 707 sc->vtnet_hwaddr[0] = 0xB2; 708 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0); 709 710 vtnet_set_hwaddr(sc); 711 } 712 } 713 714 static void 715 vtnet_set_hwaddr(struct vtnet_softc *sc) 716 { 717 device_t dev; 718 719 dev = sc->vtnet_dev; 720 721 virtio_write_device_config(dev, 722 offsetof(struct virtio_net_config, mac), 723 sc->vtnet_hwaddr, ETHER_ADDR_LEN); 724 } 725 726 static int 727 vtnet_is_link_up(struct vtnet_softc *sc) 728 { 729 device_t dev; 730 struct ifnet *ifp; 731 uint16_t status; 732 733 dev = sc->vtnet_dev; 734 ifp = sc->vtnet_ifp; 735 736 VTNET_LOCK_ASSERT(sc); 737 738 if ((ifp->if_capenable & IFCAP_LINKSTATE) == 0) 739 return (1); 740 741 status = virtio_read_dev_config_2(dev, 742 offsetof(struct virtio_net_config, status)); 743 744 return ((status & VIRTIO_NET_S_LINK_UP) != 0); 745 } 746 747 static void 748 vtnet_update_link_status(struct vtnet_softc *sc) 749 { 750 struct ifnet *ifp; 751 int link; 752 753 ifp = sc->vtnet_ifp; 754 755 link = vtnet_is_link_up(sc); 756 757 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) { 758 sc->vtnet_flags |= VTNET_FLAG_LINK; 759 if_link_state_change(ifp, LINK_STATE_UP); 760 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 761 vtnet_start_locked(ifp); 762 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) { 763 sc->vtnet_flags &= ~VTNET_FLAG_LINK; 764 if_link_state_change(ifp, LINK_STATE_DOWN); 765 } 766 } 767 768 static void 769 vtnet_watchdog(struct vtnet_softc *sc) 770 { 771 struct ifnet *ifp; 772 773 ifp = sc->vtnet_ifp; 774 775 #ifdef VTNET_TX_INTR_MODERATION 776 vtnet_txeof(sc); 777 #endif 778 779 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer) 780 return; 781 782 if_printf(ifp, "watchdog timeout -- resetting\n"); 783 #ifdef VTNET_DEBUG 784 virtqueue_dump(sc->vtnet_tx_vq); 785 #endif 786 ifp->if_oerrors++; 787 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 788 vtnet_init_locked(sc); 789 } 790 791 static void 792 vtnet_config_change_task(void *arg, int pending) 793 { 794 struct vtnet_softc *sc; 795 796 sc = arg; 797 798 VTNET_LOCK(sc); 799 vtnet_update_link_status(sc); 800 VTNET_UNLOCK(sc); 801 } 802 803 static int 804 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 805 { 806 struct vtnet_softc *sc; 807 struct ifreq *ifr; 808 int reinit, mask, error; 809 810 sc = ifp->if_softc; 811 ifr = (struct ifreq *) data; 812 reinit = 0; 813 error = 0; 814 815 switch (cmd) { 816 case SIOCSIFMTU: 817 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU) 818 error = EINVAL; 819 else if (ifp->if_mtu != ifr->ifr_mtu) { 820 VTNET_LOCK(sc); 821 error = vtnet_change_mtu(sc, ifr->ifr_mtu); 822 VTNET_UNLOCK(sc); 823 } 824 break; 825 826 case SIOCSIFFLAGS: 827 VTNET_LOCK(sc); 828 if ((ifp->if_flags & IFF_UP) == 0) { 829 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 830 vtnet_stop(sc); 831 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 832 if ((ifp->if_flags ^ sc->vtnet_if_flags) & 833 (IFF_PROMISC | IFF_ALLMULTI)) { 834 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) 835 vtnet_rx_filter(sc); 836 else 837 error = ENOTSUP; 838 } 839 } else 840 vtnet_init_locked(sc); 841 842 if (error == 0) 843 sc->vtnet_if_flags = ifp->if_flags; 844 VTNET_UNLOCK(sc); 845 break; 846 847 case SIOCADDMULTI: 848 case SIOCDELMULTI: 849 VTNET_LOCK(sc); 850 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) && 851 (ifp->if_drv_flags & IFF_DRV_RUNNING)) 852 vtnet_rx_filter_mac(sc); 853 VTNET_UNLOCK(sc); 854 break; 855 856 case SIOCSIFMEDIA: 857 case SIOCGIFMEDIA: 858 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd); 859 break; 860 861 case SIOCSIFCAP: 862 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 863 864 #ifdef DEVICE_POLLING 865 if (mask & IFCAP_POLLING) { 866 if (ifr->ifr_reqcap & IFCAP_POLLING) { 867 error = ether_poll_register(vtnet_poll, ifp); 868 if (error) 869 break; 870 871 VTNET_LOCK(sc); 872 vtnet_disable_rx_intr(sc); 873 vtnet_disable_tx_intr(sc); 874 ifp->if_capenable |= IFCAP_POLLING; 875 VTNET_UNLOCK(sc); 876 } else { 877 error = ether_poll_deregister(ifp); 878 879 /* Enable interrupts even in error case. */ 880 VTNET_LOCK(sc); 881 vtnet_enable_tx_intr(sc); 882 vtnet_enable_rx_intr(sc); 883 ifp->if_capenable &= ~IFCAP_POLLING; 884 VTNET_UNLOCK(sc); 885 } 886 } 887 #endif 888 VTNET_LOCK(sc); 889 890 if (mask & IFCAP_TXCSUM) { 891 ifp->if_capenable ^= IFCAP_TXCSUM; 892 if (ifp->if_capenable & IFCAP_TXCSUM) 893 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD; 894 else 895 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD; 896 } 897 898 if (mask & IFCAP_TSO4) { 899 ifp->if_capenable ^= IFCAP_TSO4; 900 if (ifp->if_capenable & IFCAP_TSO4) 901 ifp->if_hwassist |= CSUM_TSO; 902 else 903 ifp->if_hwassist &= ~CSUM_TSO; 904 } 905 906 if (mask & IFCAP_RXCSUM) { 907 ifp->if_capenable ^= IFCAP_RXCSUM; 908 reinit = 1; 909 } 910 911 if (mask & IFCAP_LRO) { 912 ifp->if_capenable ^= IFCAP_LRO; 913 reinit = 1; 914 } 915 916 if (mask & IFCAP_VLAN_HWFILTER) { 917 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER; 918 reinit = 1; 919 } 920 921 if (mask & IFCAP_VLAN_HWTSO) 922 ifp->if_capenable ^= IFCAP_VLAN_HWTSO; 923 924 if (mask & IFCAP_VLAN_HWTAGGING) 925 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 926 927 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) { 928 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 929 vtnet_init_locked(sc); 930 } 931 VLAN_CAPABILITIES(ifp); 932 933 VTNET_UNLOCK(sc); 934 break; 935 936 default: 937 error = ether_ioctl(ifp, cmd, data); 938 break; 939 } 940 941 VTNET_LOCK_ASSERT_NOTOWNED(sc); 942 943 return (error); 944 } 945 946 static int 947 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu) 948 { 949 struct ifnet *ifp; 950 int new_frame_size, clsize; 951 952 ifp = sc->vtnet_ifp; 953 954 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 955 new_frame_size = sizeof(struct vtnet_rx_header) + 956 sizeof(struct ether_vlan_header) + new_mtu; 957 958 if (new_frame_size > MJUM9BYTES) 959 return (EINVAL); 960 961 if (new_frame_size <= MCLBYTES) 962 clsize = MCLBYTES; 963 else 964 clsize = MJUM9BYTES; 965 } else { 966 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) + 967 sizeof(struct ether_vlan_header) + new_mtu; 968 969 if (new_frame_size <= MCLBYTES) 970 clsize = MCLBYTES; 971 else 972 clsize = MJUMPAGESIZE; 973 } 974 975 sc->vtnet_rx_mbuf_size = clsize; 976 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc); 977 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS, 978 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count)); 979 980 ifp->if_mtu = new_mtu; 981 982 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 983 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 984 vtnet_init_locked(sc); 985 } 986 987 return (0); 988 } 989 990 static int 991 vtnet_init_rx_vq(struct vtnet_softc *sc) 992 { 993 struct virtqueue *vq; 994 int nbufs, error; 995 996 vq = sc->vtnet_rx_vq; 997 nbufs = 0; 998 error = ENOSPC; 999 1000 while (!virtqueue_full(vq)) { 1001 if ((error = vtnet_newbuf(sc)) != 0) 1002 break; 1003 nbufs++; 1004 } 1005 1006 if (nbufs > 0) { 1007 virtqueue_notify(vq); 1008 1009 /* 1010 * EMSGSIZE signifies the virtqueue did not have enough 1011 * entries available to hold the last mbuf. This is not 1012 * an error. We should not get ENOSPC since we check if 1013 * the virtqueue is full before attempting to add a 1014 * buffer. 1015 */ 1016 if (error == EMSGSIZE) 1017 error = 0; 1018 } 1019 1020 return (error); 1021 } 1022 1023 static void 1024 vtnet_free_rx_mbufs(struct vtnet_softc *sc) 1025 { 1026 struct virtqueue *vq; 1027 struct mbuf *m; 1028 int last; 1029 1030 vq = sc->vtnet_rx_vq; 1031 last = 0; 1032 1033 while ((m = virtqueue_drain(vq, &last)) != NULL) 1034 m_freem(m); 1035 1036 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq")); 1037 } 1038 1039 static void 1040 vtnet_free_tx_mbufs(struct vtnet_softc *sc) 1041 { 1042 struct virtqueue *vq; 1043 struct vtnet_tx_header *txhdr; 1044 int last; 1045 1046 vq = sc->vtnet_tx_vq; 1047 last = 0; 1048 1049 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) { 1050 m_freem(txhdr->vth_mbuf); 1051 uma_zfree(vtnet_tx_header_zone, txhdr); 1052 } 1053 1054 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq")); 1055 } 1056 1057 static void 1058 vtnet_free_ctrl_vq(struct vtnet_softc *sc) 1059 { 1060 1061 /* 1062 * The control virtqueue is only polled, therefore 1063 * it should already be empty. 1064 */ 1065 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq), 1066 ("Ctrl Vq not empty")); 1067 } 1068 1069 #ifdef DEVICE_POLLING 1070 static int 1071 vtnet_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1072 { 1073 struct vtnet_softc *sc; 1074 int rx_done; 1075 1076 sc = ifp->if_softc; 1077 rx_done = 0; 1078 1079 VTNET_LOCK(sc); 1080 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1081 if (cmd == POLL_AND_CHECK_STATUS) 1082 vtnet_update_link_status(sc); 1083 1084 if (virtqueue_nused(sc->vtnet_rx_vq) > 0) 1085 vtnet_rxeof(sc, count, &rx_done); 1086 1087 vtnet_txeof(sc); 1088 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1089 vtnet_start_locked(ifp); 1090 } 1091 VTNET_UNLOCK(sc); 1092 1093 return (rx_done); 1094 } 1095 #endif /* DEVICE_POLLING */ 1096 1097 static struct mbuf * 1098 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp) 1099 { 1100 struct mbuf *m_head, *m_tail, *m; 1101 int i, clsize; 1102 1103 clsize = sc->vtnet_rx_mbuf_size; 1104 1105 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize); 1106 if (m_head == NULL) 1107 goto fail; 1108 1109 m_head->m_len = clsize; 1110 m_tail = m_head; 1111 1112 if (nbufs > 1) { 1113 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG, 1114 ("chained Rx mbuf requested without LRO_NOMRG")); 1115 1116 for (i = 1; i < nbufs; i++) { 1117 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize); 1118 if (m == NULL) 1119 goto fail; 1120 1121 m->m_len = clsize; 1122 m_tail->m_next = m; 1123 m_tail = m; 1124 } 1125 } 1126 1127 if (m_tailp != NULL) 1128 *m_tailp = m_tail; 1129 1130 return (m_head); 1131 1132 fail: 1133 sc->vtnet_stats.mbuf_alloc_failed++; 1134 m_freem(m_head); 1135 1136 return (NULL); 1137 } 1138 1139 static int 1140 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0) 1141 { 1142 struct mbuf *m, *m_prev; 1143 struct mbuf *m_new, *m_tail; 1144 int len, clsize, nreplace, error; 1145 1146 m = m0; 1147 m_prev = NULL; 1148 len = len0; 1149 1150 m_tail = NULL; 1151 clsize = sc->vtnet_rx_mbuf_size; 1152 nreplace = 0; 1153 1154 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || 1155 m->m_next == NULL, ("chained Rx mbuf without LRO_NOMRG")); 1156 1157 /* 1158 * Since LRO_NOMRG mbuf chains are so large, we want to avoid 1159 * allocating an entire chain for each received frame. When 1160 * the received frame's length is less than that of the chain, 1161 * the unused mbufs are reassigned to the new chain. 1162 */ 1163 while (len > 0) { 1164 /* 1165 * Something is seriously wrong if we received 1166 * a frame larger than the mbuf chain. Drop it. 1167 */ 1168 if (m == NULL) { 1169 sc->vtnet_stats.rx_frame_too_large++; 1170 return (EMSGSIZE); 1171 } 1172 1173 KASSERT(m->m_len == clsize, 1174 ("mbuf length not expected cluster size: %d", 1175 m->m_len)); 1176 1177 m->m_len = MIN(m->m_len, len); 1178 len -= m->m_len; 1179 1180 m_prev = m; 1181 m = m->m_next; 1182 nreplace++; 1183 } 1184 1185 KASSERT(m_prev != NULL, ("m_prev == NULL")); 1186 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count, 1187 ("too many replacement mbufs: %d/%d", nreplace, 1188 sc->vtnet_rx_mbuf_count)); 1189 1190 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail); 1191 if (m_new == NULL) { 1192 m_prev->m_len = clsize; 1193 return (ENOBUFS); 1194 } 1195 1196 /* 1197 * Move unused mbufs, if any, from the original chain 1198 * onto the end of the new chain. 1199 */ 1200 if (m_prev->m_next != NULL) { 1201 m_tail->m_next = m_prev->m_next; 1202 m_prev->m_next = NULL; 1203 } 1204 1205 error = vtnet_enqueue_rxbuf(sc, m_new); 1206 if (error) { 1207 /* 1208 * BAD! We could not enqueue the replacement mbuf chain. We 1209 * must restore the m0 chain to the original state if it was 1210 * modified so we can subsequently discard it. 1211 * 1212 * NOTE: The replacement is suppose to be an identical copy 1213 * to the one just dequeued so this is an unexpected error. 1214 */ 1215 sc->vtnet_stats.rx_enq_replacement_failed++; 1216 1217 if (m_tail->m_next != NULL) { 1218 m_prev->m_next = m_tail->m_next; 1219 m_tail->m_next = NULL; 1220 } 1221 1222 m_prev->m_len = clsize; 1223 m_freem(m_new); 1224 } 1225 1226 return (error); 1227 } 1228 1229 static int 1230 vtnet_newbuf(struct vtnet_softc *sc) 1231 { 1232 struct mbuf *m; 1233 int error; 1234 1235 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL); 1236 if (m == NULL) 1237 return (ENOBUFS); 1238 1239 error = vtnet_enqueue_rxbuf(sc, m); 1240 if (error) 1241 m_freem(m); 1242 1243 return (error); 1244 } 1245 1246 static void 1247 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs) 1248 { 1249 struct virtqueue *vq; 1250 struct mbuf *m; 1251 1252 vq = sc->vtnet_rx_vq; 1253 1254 while (--nbufs > 0) { 1255 if ((m = virtqueue_dequeue(vq, NULL)) == NULL) 1256 break; 1257 vtnet_discard_rxbuf(sc, m); 1258 } 1259 } 1260 1261 static void 1262 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m) 1263 { 1264 int error; 1265 1266 /* 1267 * Requeue the discarded mbuf. This should always be 1268 * successful since it was just dequeued. 1269 */ 1270 error = vtnet_enqueue_rxbuf(sc, m); 1271 KASSERT(error == 0, ("cannot requeue discarded mbuf")); 1272 } 1273 1274 static int 1275 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m) 1276 { 1277 struct sglist sg; 1278 struct sglist_seg segs[VTNET_MAX_RX_SEGS]; 1279 struct vtnet_rx_header *rxhdr; 1280 struct virtio_net_hdr *hdr; 1281 uint8_t *mdata; 1282 int offset, error; 1283 1284 VTNET_LOCK_ASSERT(sc); 1285 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || 1286 m->m_next == NULL, ("chained Rx mbuf without LRO_NOMRG")); 1287 1288 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs); 1289 1290 mdata = mtod(m, uint8_t *); 1291 offset = 0; 1292 1293 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 1294 rxhdr = (struct vtnet_rx_header *) mdata; 1295 hdr = &rxhdr->vrh_hdr; 1296 offset += sizeof(struct vtnet_rx_header); 1297 1298 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size); 1299 KASSERT(error == 0, ("cannot add header to sglist")); 1300 } 1301 1302 error = sglist_append(&sg, mdata + offset, m->m_len - offset); 1303 if (error) 1304 return (error); 1305 1306 if (m->m_next != NULL) { 1307 error = sglist_append_mbuf(&sg, m->m_next); 1308 if (error) 1309 return (error); 1310 } 1311 1312 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg)); 1313 } 1314 1315 static void 1316 vtnet_vlan_tag_remove(struct mbuf *m) 1317 { 1318 struct ether_vlan_header *evl; 1319 1320 evl = mtod(m, struct ether_vlan_header *); 1321 1322 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag); 1323 m->m_flags |= M_VLANTAG; 1324 1325 /* Strip the 802.1Q header. */ 1326 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN, 1327 ETHER_HDR_LEN - ETHER_TYPE_LEN); 1328 m_adj(m, ETHER_VLAN_ENCAP_LEN); 1329 } 1330 1331 #ifdef notyet 1332 static int 1333 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m, 1334 struct virtio_net_hdr *hdr) 1335 { 1336 struct ether_header *eh; 1337 struct ether_vlan_header *evh; 1338 struct ip *ip; 1339 struct ip6_hdr *ip6; 1340 struct udphdr *udp; 1341 int ip_offset, csum_start, csum_offset, hlen; 1342 uint16_t eth_type; 1343 uint8_t ip_proto; 1344 1345 /* 1346 * Convert the VirtIO checksum interface to FreeBSD's interface. 1347 * The host only provides us with the offset at which to start 1348 * checksumming, and the offset from that to place the completed 1349 * checksum. While this maps well with how Linux does checksums, 1350 * for FreeBSD, we must parse the received packet in order to set 1351 * the appropriate CSUM_* flags. 1352 */ 1353 1354 /* 1355 * Every mbuf added to the receive virtqueue is always at least 1356 * MCLBYTES big, so assume something is amiss if the first mbuf 1357 * does not contain both the Ethernet and protocol headers. 1358 */ 1359 ip_offset = sizeof(struct ether_header); 1360 if (m->m_len < ip_offset) 1361 return (1); 1362 1363 eh = mtod(m, struct ether_header *); 1364 eth_type = ntohs(eh->ether_type); 1365 if (eth_type == ETHERTYPE_VLAN) { 1366 ip_offset = sizeof(struct ether_vlan_header); 1367 if (m->m_len < ip_offset) 1368 return (1); 1369 evh = mtod(m, struct ether_vlan_header *); 1370 eth_type = ntohs(evh->evl_proto); 1371 } 1372 1373 switch (eth_type) { 1374 case ETHERTYPE_IP: 1375 if (m->m_len < ip_offset + sizeof(struct ip)) 1376 return (1); 1377 1378 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset); 1379 /* Sanity check the IP header. */ 1380 if (ip->ip_v != IPVERSION) 1381 return (1); 1382 hlen = ip->ip_hl << 2; 1383 if (hlen < sizeof(struct ip)) 1384 return (1); 1385 if (ntohs(ip->ip_len) < hlen) 1386 return (1); 1387 if (ntohs(ip->ip_len) != (m->m_pkthdr.len - ip_offset)) 1388 return (1); 1389 1390 ip_proto = ip->ip_p; 1391 csum_start = ip_offset + hlen; 1392 break; 1393 1394 case ETHERTYPE_IPV6: 1395 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) 1396 return (1); 1397 1398 /* 1399 * XXX FreeBSD does not handle any IPv6 checksum offloading 1400 * at the moment. 1401 */ 1402 1403 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset); 1404 /* XXX Assume no extension headers are present. */ 1405 ip_proto = ip6->ip6_nxt; 1406 csum_start = ip_offset + sizeof(struct ip6_hdr); 1407 break; 1408 1409 default: 1410 sc->vtnet_stats.rx_csum_bad_ethtype++; 1411 return (1); 1412 } 1413 1414 /* Assume checksum begins right after the IP header. */ 1415 if (hdr->csum_start != csum_start) { 1416 sc->vtnet_stats.rx_csum_bad_start++; 1417 return (1); 1418 } 1419 1420 switch (ip_proto) { 1421 case IPPROTO_TCP: 1422 csum_offset = offsetof(struct tcphdr, th_sum); 1423 break; 1424 1425 case IPPROTO_UDP: 1426 csum_offset = offsetof(struct udphdr, uh_sum); 1427 break; 1428 1429 case IPPROTO_SCTP: 1430 csum_offset = offsetof(struct sctphdr, checksum); 1431 break; 1432 1433 default: 1434 sc->vtnet_stats.rx_csum_bad_ipproto++; 1435 return (1); 1436 } 1437 1438 if (hdr->csum_offset != csum_offset) { 1439 sc->vtnet_stats.rx_csum_bad_offset++; 1440 return (1); 1441 } 1442 1443 /* 1444 * The IP header checksum is almost certainly valid but I'm 1445 * uncertain if that is guaranteed. 1446 * 1447 * m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID; 1448 */ 1449 1450 switch (ip_proto) { 1451 case IPPROTO_UDP: 1452 if (m->m_len < csum_start + sizeof(struct udphdr)) 1453 return (1); 1454 1455 udp = (struct udphdr *)(mtod(m, uint8_t *) + csum_start); 1456 if (udp->uh_sum == 0) 1457 return (0); 1458 1459 /* FALLTHROUGH */ 1460 1461 case IPPROTO_TCP: 1462 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1463 m->m_pkthdr.csum_data = 0xFFFF; 1464 break; 1465 1466 case IPPROTO_SCTP: 1467 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 1468 break; 1469 } 1470 1471 sc->vtnet_stats.rx_csum_offloaded++; 1472 1473 return (0); 1474 } 1475 #endif 1476 1477 /* 1478 * Alternative method of doing receive checksum offloading. Rather 1479 * than parsing the received frame down to the IP header, use the 1480 * csum_offset to determine which CSUM_* flags are appropriate. We 1481 * can get by with doing this only because the checksum offsets are 1482 * unique for the things we care about. 1483 */ 1484 static int 1485 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m, 1486 struct virtio_net_hdr *hdr) 1487 { 1488 struct ether_header *eh; 1489 struct ether_vlan_header *evh; 1490 struct udphdr *udp; 1491 int csum_len; 1492 uint16_t eth_type; 1493 1494 csum_len = hdr->csum_start + hdr->csum_offset; 1495 1496 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip)) 1497 return (1); 1498 if (m->m_len < csum_len) 1499 return (1); 1500 1501 eh = mtod(m, struct ether_header *); 1502 eth_type = ntohs(eh->ether_type); 1503 if (eth_type == ETHERTYPE_VLAN) { 1504 evh = mtod(m, struct ether_vlan_header *); 1505 eth_type = ntohs(evh->evl_proto); 1506 } 1507 1508 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) { 1509 sc->vtnet_stats.rx_csum_bad_ethtype++; 1510 return (1); 1511 } 1512 1513 /* Use the offset to determine the appropriate CSUM_* flags. */ 1514 switch (hdr->csum_offset) { 1515 case offsetof(struct udphdr, uh_sum): 1516 if (m->m_len < hdr->csum_start + sizeof(struct udphdr)) 1517 return (1); 1518 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start); 1519 if (udp->uh_sum == 0) 1520 return (0); 1521 1522 /* FALLTHROUGH */ 1523 1524 case offsetof(struct tcphdr, th_sum): 1525 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1526 m->m_pkthdr.csum_data = 0xFFFF; 1527 break; 1528 1529 case offsetof(struct sctphdr, checksum): 1530 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 1531 break; 1532 1533 default: 1534 sc->vtnet_stats.rx_csum_bad_offset++; 1535 return (1); 1536 } 1537 1538 sc->vtnet_stats.rx_csum_offloaded++; 1539 1540 return (0); 1541 } 1542 1543 static int 1544 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs) 1545 { 1546 struct ifnet *ifp; 1547 struct virtqueue *vq; 1548 struct mbuf *m, *m_tail; 1549 int len; 1550 1551 ifp = sc->vtnet_ifp; 1552 vq = sc->vtnet_rx_vq; 1553 m_tail = m_head; 1554 1555 while (--nbufs > 0) { 1556 m = virtqueue_dequeue(vq, &len); 1557 if (m == NULL) { 1558 ifp->if_ierrors++; 1559 goto fail; 1560 } 1561 1562 if (vtnet_newbuf(sc) != 0) { 1563 ifp->if_iqdrops++; 1564 vtnet_discard_rxbuf(sc, m); 1565 if (nbufs > 1) 1566 vtnet_discard_merged_rxbuf(sc, nbufs); 1567 goto fail; 1568 } 1569 1570 if (m->m_len < len) 1571 len = m->m_len; 1572 1573 m->m_len = len; 1574 m->m_flags &= ~M_PKTHDR; 1575 1576 m_head->m_pkthdr.len += len; 1577 m_tail->m_next = m; 1578 m_tail = m; 1579 } 1580 1581 return (0); 1582 1583 fail: 1584 sc->vtnet_stats.rx_mergeable_failed++; 1585 m_freem(m_head); 1586 1587 return (1); 1588 } 1589 1590 static int 1591 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp) 1592 { 1593 struct virtio_net_hdr lhdr; 1594 struct ifnet *ifp; 1595 struct virtqueue *vq; 1596 struct mbuf *m; 1597 struct ether_header *eh; 1598 struct virtio_net_hdr *hdr; 1599 struct virtio_net_hdr_mrg_rxbuf *mhdr; 1600 int len, deq, nbufs, adjsz, rx_npkts; 1601 1602 ifp = sc->vtnet_ifp; 1603 vq = sc->vtnet_rx_vq; 1604 hdr = &lhdr; 1605 deq = 0; 1606 rx_npkts = 0; 1607 1608 VTNET_LOCK_ASSERT(sc); 1609 1610 while (--count >= 0) { 1611 m = virtqueue_dequeue(vq, &len); 1612 if (m == NULL) 1613 break; 1614 deq++; 1615 1616 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) { 1617 ifp->if_ierrors++; 1618 vtnet_discard_rxbuf(sc, m); 1619 continue; 1620 } 1621 1622 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) { 1623 nbufs = 1; 1624 adjsz = sizeof(struct vtnet_rx_header); 1625 /* 1626 * Account for our pad between the header and 1627 * the actual start of the frame. 1628 */ 1629 len += VTNET_RX_HEADER_PAD; 1630 } else { 1631 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *); 1632 nbufs = mhdr->num_buffers; 1633 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf); 1634 } 1635 1636 if (vtnet_replace_rxbuf(sc, m, len) != 0) { 1637 ifp->if_iqdrops++; 1638 vtnet_discard_rxbuf(sc, m); 1639 if (nbufs > 1) 1640 vtnet_discard_merged_rxbuf(sc, nbufs); 1641 continue; 1642 } 1643 1644 m->m_pkthdr.len = len; 1645 m->m_pkthdr.rcvif = ifp; 1646 m->m_pkthdr.csum_flags = 0; 1647 1648 if (nbufs > 1) { 1649 if (vtnet_rxeof_merged(sc, m, nbufs) != 0) 1650 continue; 1651 } 1652 1653 ifp->if_ipackets++; 1654 1655 /* 1656 * Save copy of header before we strip it. For both mergeable 1657 * and non-mergeable, the VirtIO header is placed first in the 1658 * mbuf's data. We no longer need num_buffers, so always use a 1659 * virtio_net_hdr. 1660 */ 1661 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr)); 1662 m_adj(m, adjsz); 1663 1664 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) { 1665 eh = mtod(m, struct ether_header *); 1666 if (eh->ether_type == htons(ETHERTYPE_VLAN)) { 1667 vtnet_vlan_tag_remove(m); 1668 1669 /* 1670 * With the 802.1Q header removed, update the 1671 * checksum starting location accordingly. 1672 */ 1673 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) 1674 hdr->csum_start -= 1675 ETHER_VLAN_ENCAP_LEN; 1676 } 1677 } 1678 1679 if (ifp->if_capenable & IFCAP_RXCSUM && 1680 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 1681 if (vtnet_rx_csum(sc, m, hdr) != 0) 1682 sc->vtnet_stats.rx_csum_failed++; 1683 } 1684 1685 VTNET_UNLOCK(sc); 1686 rx_npkts++; 1687 (*ifp->if_input)(ifp, m); 1688 VTNET_LOCK(sc); 1689 1690 /* 1691 * The interface may have been stopped while we were 1692 * passing the packet up the network stack. 1693 */ 1694 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1695 break; 1696 } 1697 1698 if (deq > 0) 1699 virtqueue_notify(vq); 1700 1701 if (rx_npktsp != NULL) 1702 *rx_npktsp = rx_npkts; 1703 1704 return (count > 0 ? 0 : EAGAIN); 1705 } 1706 1707 static void 1708 vtnet_rx_intr_task(void *arg, int pending) 1709 { 1710 struct vtnet_softc *sc; 1711 struct ifnet *ifp; 1712 int more; 1713 1714 sc = arg; 1715 ifp = sc->vtnet_ifp; 1716 1717 VTNET_LOCK(sc); 1718 1719 #ifdef DEVICE_POLLING 1720 if (ifp->if_capenable & IFCAP_POLLING) { 1721 VTNET_UNLOCK(sc); 1722 return; 1723 } 1724 #endif 1725 1726 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1727 vtnet_enable_rx_intr(sc); 1728 VTNET_UNLOCK(sc); 1729 return; 1730 } 1731 1732 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL); 1733 if (!more && vtnet_enable_rx_intr(sc) != 0) { 1734 vtnet_disable_rx_intr(sc); 1735 more = 1; 1736 } 1737 1738 VTNET_UNLOCK(sc); 1739 1740 if (more) { 1741 sc->vtnet_stats.rx_task_rescheduled++; 1742 taskqueue_enqueue_fast(sc->vtnet_tq, 1743 &sc->vtnet_rx_intr_task); 1744 } 1745 } 1746 1747 static int 1748 vtnet_rx_vq_intr(void *xsc) 1749 { 1750 struct vtnet_softc *sc; 1751 1752 sc = xsc; 1753 1754 vtnet_disable_rx_intr(sc); 1755 taskqueue_enqueue_fast(sc->vtnet_tq, &sc->vtnet_rx_intr_task); 1756 1757 return (1); 1758 } 1759 1760 static void 1761 vtnet_txeof(struct vtnet_softc *sc) 1762 { 1763 struct virtqueue *vq; 1764 struct ifnet *ifp; 1765 struct vtnet_tx_header *txhdr; 1766 int deq; 1767 1768 vq = sc->vtnet_tx_vq; 1769 ifp = sc->vtnet_ifp; 1770 deq = 0; 1771 1772 VTNET_LOCK_ASSERT(sc); 1773 1774 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) { 1775 deq++; 1776 ifp->if_opackets++; 1777 m_freem(txhdr->vth_mbuf); 1778 uma_zfree(vtnet_tx_header_zone, txhdr); 1779 } 1780 1781 if (deq > 0) { 1782 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1783 if (virtqueue_empty(vq)) 1784 sc->vtnet_watchdog_timer = 0; 1785 } 1786 } 1787 1788 static struct mbuf * 1789 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m, 1790 struct virtio_net_hdr *hdr) 1791 { 1792 struct ifnet *ifp; 1793 struct ether_header *eh; 1794 struct ether_vlan_header *evh; 1795 struct ip *ip; 1796 struct ip6_hdr *ip6; 1797 struct tcphdr *tcp; 1798 int ip_offset; 1799 uint16_t eth_type, csum_start; 1800 uint8_t ip_proto, gso_type; 1801 1802 ifp = sc->vtnet_ifp; 1803 M_ASSERTPKTHDR(m); 1804 1805 ip_offset = sizeof(struct ether_header); 1806 if (m->m_len < ip_offset) { 1807 if ((m = m_pullup(m, ip_offset)) == NULL) 1808 return (NULL); 1809 } 1810 1811 eh = mtod(m, struct ether_header *); 1812 eth_type = ntohs(eh->ether_type); 1813 if (eth_type == ETHERTYPE_VLAN) { 1814 ip_offset = sizeof(struct ether_vlan_header); 1815 if (m->m_len < ip_offset) { 1816 if ((m = m_pullup(m, ip_offset)) == NULL) 1817 return (NULL); 1818 } 1819 evh = mtod(m, struct ether_vlan_header *); 1820 eth_type = ntohs(evh->evl_proto); 1821 } 1822 1823 switch (eth_type) { 1824 case ETHERTYPE_IP: 1825 if (m->m_len < ip_offset + sizeof(struct ip)) { 1826 m = m_pullup(m, ip_offset + sizeof(struct ip)); 1827 if (m == NULL) 1828 return (NULL); 1829 } 1830 1831 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset); 1832 ip_proto = ip->ip_p; 1833 csum_start = ip_offset + (ip->ip_hl << 2); 1834 gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 1835 break; 1836 1837 case ETHERTYPE_IPV6: 1838 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) { 1839 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr)); 1840 if (m == NULL) 1841 return (NULL); 1842 } 1843 1844 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset); 1845 /* 1846 * XXX Assume no extension headers are present. Presently, 1847 * this will always be true in the case of TSO, and FreeBSD 1848 * does not perform checksum offloading of IPv6 yet. 1849 */ 1850 ip_proto = ip6->ip6_nxt; 1851 csum_start = ip_offset + sizeof(struct ip6_hdr); 1852 gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 1853 break; 1854 1855 default: 1856 return (m); 1857 } 1858 1859 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) { 1860 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM; 1861 hdr->csum_start = csum_start; 1862 hdr->csum_offset = m->m_pkthdr.csum_data; 1863 1864 sc->vtnet_stats.tx_csum_offloaded++; 1865 } 1866 1867 if (m->m_pkthdr.csum_flags & CSUM_TSO) { 1868 if (ip_proto != IPPROTO_TCP) 1869 return (m); 1870 1871 if (m->m_len < csum_start + sizeof(struct tcphdr)) { 1872 m = m_pullup(m, csum_start + sizeof(struct tcphdr)); 1873 if (m == NULL) 1874 return (NULL); 1875 } 1876 1877 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start); 1878 hdr->gso_type = gso_type; 1879 hdr->hdr_len = csum_start + (tcp->th_off << 2); 1880 hdr->gso_size = m->m_pkthdr.tso_segsz; 1881 1882 if (tcp->th_flags & TH_CWR) { 1883 /* 1884 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN. 1885 * ECN support is only configurable globally with the 1886 * net.inet.tcp.ecn.enable sysctl knob. 1887 */ 1888 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) { 1889 if_printf(ifp, "TSO with ECN not supported " 1890 "by host\n"); 1891 m_freem(m); 1892 return (NULL); 1893 } 1894 1895 hdr->flags |= VIRTIO_NET_HDR_GSO_ECN; 1896 } 1897 1898 sc->vtnet_stats.tx_tso_offloaded++; 1899 } 1900 1901 return (m); 1902 } 1903 1904 static int 1905 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head, 1906 struct vtnet_tx_header *txhdr) 1907 { 1908 struct sglist sg; 1909 struct sglist_seg segs[VTNET_MAX_TX_SEGS]; 1910 struct virtqueue *vq; 1911 struct mbuf *m; 1912 int collapsed, error; 1913 1914 vq = sc->vtnet_tx_vq; 1915 m = *m_head; 1916 collapsed = 0; 1917 1918 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs); 1919 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size); 1920 KASSERT(error == 0 && sg.sg_nseg == 1, 1921 ("cannot add header to sglist")); 1922 1923 again: 1924 error = sglist_append_mbuf(&sg, m); 1925 if (error) { 1926 if (collapsed) 1927 goto fail; 1928 1929 m = m_collapse(m, M_NOWAIT, VTNET_MAX_TX_SEGS - 1); 1930 if (m == NULL) 1931 goto fail; 1932 1933 *m_head = m; 1934 collapsed = 1; 1935 goto again; 1936 } 1937 1938 txhdr->vth_mbuf = m; 1939 1940 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0)); 1941 1942 fail: 1943 m_freem(*m_head); 1944 *m_head = NULL; 1945 1946 return (ENOBUFS); 1947 } 1948 1949 static int 1950 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head) 1951 { 1952 struct vtnet_tx_header *txhdr; 1953 struct virtio_net_hdr *hdr; 1954 struct mbuf *m; 1955 int error; 1956 1957 m = *m_head; 1958 1959 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO); 1960 if (txhdr == NULL) { 1961 *m_head = NULL; 1962 m_freem(m); 1963 return (ENOMEM); 1964 } 1965 1966 /* 1967 * Always use the non-mergeable header to simplify things. When 1968 * the mergeable feature is negotiated, the num_buffers field 1969 * must be set to zero. We use vtnet_hdr_size later to enqueue 1970 * the correct header size to the host. 1971 */ 1972 hdr = &txhdr->vth_uhdr.hdr; 1973 1974 if (m->m_flags & M_VLANTAG) { 1975 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag); 1976 if ((*m_head = m) == NULL) { 1977 error = ENOBUFS; 1978 goto fail; 1979 } 1980 m->m_flags &= ~M_VLANTAG; 1981 } 1982 1983 if (m->m_pkthdr.csum_flags != 0) { 1984 m = vtnet_tx_offload(sc, m, hdr); 1985 if ((*m_head = m) == NULL) { 1986 error = ENOBUFS; 1987 goto fail; 1988 } 1989 } 1990 1991 error = vtnet_enqueue_txbuf(sc, m_head, txhdr); 1992 fail: 1993 if (error) 1994 uma_zfree(vtnet_tx_header_zone, txhdr); 1995 1996 return (error); 1997 } 1998 1999 static void 2000 vtnet_start(struct ifnet *ifp) 2001 { 2002 struct vtnet_softc *sc; 2003 2004 sc = ifp->if_softc; 2005 2006 VTNET_LOCK(sc); 2007 vtnet_start_locked(ifp); 2008 VTNET_UNLOCK(sc); 2009 } 2010 2011 static void 2012 vtnet_start_locked(struct ifnet *ifp) 2013 { 2014 struct vtnet_softc *sc; 2015 struct virtqueue *vq; 2016 struct mbuf *m0; 2017 int enq; 2018 2019 sc = ifp->if_softc; 2020 vq = sc->vtnet_tx_vq; 2021 enq = 0; 2022 2023 VTNET_LOCK_ASSERT(sc); 2024 2025 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2026 IFF_DRV_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) 2027 return; 2028 2029 #ifdef VTNET_TX_INTR_MODERATION 2030 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2) 2031 vtnet_txeof(sc); 2032 #endif 2033 2034 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 2035 if (virtqueue_full(vq)) { 2036 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2037 break; 2038 } 2039 2040 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2041 if (m0 == NULL) 2042 break; 2043 2044 if (vtnet_encap(sc, &m0) != 0) { 2045 if (m0 == NULL) 2046 break; 2047 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2048 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2049 break; 2050 } 2051 2052 enq++; 2053 ETHER_BPF_MTAP(ifp, m0); 2054 } 2055 2056 if (enq > 0) { 2057 virtqueue_notify(vq); 2058 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT; 2059 } 2060 } 2061 2062 static void 2063 vtnet_tick(void *xsc) 2064 { 2065 struct vtnet_softc *sc; 2066 2067 sc = xsc; 2068 2069 VTNET_LOCK_ASSERT(sc); 2070 #ifdef VTNET_DEBUG 2071 virtqueue_dump(sc->vtnet_rx_vq); 2072 virtqueue_dump(sc->vtnet_tx_vq); 2073 #endif 2074 2075 vtnet_watchdog(sc); 2076 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc); 2077 } 2078 2079 static void 2080 vtnet_tx_intr_task(void *arg, int pending) 2081 { 2082 struct vtnet_softc *sc; 2083 struct ifnet *ifp; 2084 2085 sc = arg; 2086 ifp = sc->vtnet_ifp; 2087 2088 VTNET_LOCK(sc); 2089 2090 #ifdef DEVICE_POLLING 2091 if (ifp->if_capenable & IFCAP_POLLING) { 2092 VTNET_UNLOCK(sc); 2093 return; 2094 } 2095 #endif 2096 2097 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2098 vtnet_enable_tx_intr(sc); 2099 VTNET_UNLOCK(sc); 2100 return; 2101 } 2102 2103 vtnet_txeof(sc); 2104 2105 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2106 vtnet_start_locked(ifp); 2107 2108 if (vtnet_enable_tx_intr(sc) != 0) { 2109 vtnet_disable_tx_intr(sc); 2110 sc->vtnet_stats.tx_task_rescheduled++; 2111 VTNET_UNLOCK(sc); 2112 taskqueue_enqueue_fast(sc->vtnet_tq, &sc->vtnet_tx_intr_task); 2113 return; 2114 } 2115 2116 VTNET_UNLOCK(sc); 2117 } 2118 2119 static int 2120 vtnet_tx_vq_intr(void *xsc) 2121 { 2122 struct vtnet_softc *sc; 2123 2124 sc = xsc; 2125 2126 vtnet_disable_tx_intr(sc); 2127 taskqueue_enqueue_fast(sc->vtnet_tq, &sc->vtnet_tx_intr_task); 2128 2129 return (1); 2130 } 2131 2132 static void 2133 vtnet_stop(struct vtnet_softc *sc) 2134 { 2135 device_t dev; 2136 struct ifnet *ifp; 2137 2138 dev = sc->vtnet_dev; 2139 ifp = sc->vtnet_ifp; 2140 2141 VTNET_LOCK_ASSERT(sc); 2142 2143 sc->vtnet_watchdog_timer = 0; 2144 callout_stop(&sc->vtnet_tick_ch); 2145 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2146 2147 vtnet_disable_rx_intr(sc); 2148 vtnet_disable_tx_intr(sc); 2149 2150 /* 2151 * Stop the host VirtIO adapter. Note this will reset the host 2152 * adapter's state back to the pre-initialized state, so in 2153 * order to make the device usable again, we must drive it 2154 * through virtio_reinit() and virtio_reinit_complete(). 2155 */ 2156 virtio_stop(dev); 2157 2158 sc->vtnet_flags &= ~VTNET_FLAG_LINK; 2159 2160 vtnet_free_rx_mbufs(sc); 2161 vtnet_free_tx_mbufs(sc); 2162 } 2163 2164 static int 2165 vtnet_reinit(struct vtnet_softc *sc) 2166 { 2167 struct ifnet *ifp; 2168 uint64_t features; 2169 2170 ifp = sc->vtnet_ifp; 2171 features = sc->vtnet_features; 2172 2173 /* 2174 * Re-negotiate with the host, removing any disabled receive 2175 * features. Transmit features are disabled only on our side 2176 * via if_capenable and if_hwassist. 2177 */ 2178 2179 if (ifp->if_capabilities & IFCAP_RXCSUM) { 2180 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0) 2181 features &= ~VIRTIO_NET_F_GUEST_CSUM; 2182 } 2183 2184 if (ifp->if_capabilities & IFCAP_LRO) { 2185 if ((ifp->if_capenable & IFCAP_LRO) == 0) 2186 features &= ~VTNET_LRO_FEATURES; 2187 } 2188 2189 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) { 2190 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0) 2191 features &= ~VIRTIO_NET_F_CTRL_VLAN; 2192 } 2193 2194 return (virtio_reinit(sc->vtnet_dev, features)); 2195 } 2196 2197 static void 2198 vtnet_init_locked(struct vtnet_softc *sc) 2199 { 2200 device_t dev; 2201 struct ifnet *ifp; 2202 int error; 2203 2204 dev = sc->vtnet_dev; 2205 ifp = sc->vtnet_ifp; 2206 2207 VTNET_LOCK_ASSERT(sc); 2208 2209 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2210 return; 2211 2212 /* Stop host's adapter, cancel any pending I/O. */ 2213 vtnet_stop(sc); 2214 2215 /* Reinitialize the host device. */ 2216 error = vtnet_reinit(sc); 2217 if (error) { 2218 device_printf(dev, 2219 "reinitialization failed, stopping device...\n"); 2220 vtnet_stop(sc); 2221 return; 2222 } 2223 2224 /* Update host with assigned MAC address. */ 2225 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN); 2226 vtnet_set_hwaddr(sc); 2227 2228 ifp->if_hwassist = 0; 2229 if (ifp->if_capenable & IFCAP_TXCSUM) 2230 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD; 2231 if (ifp->if_capenable & IFCAP_TSO4) 2232 ifp->if_hwassist |= CSUM_TSO; 2233 2234 error = vtnet_init_rx_vq(sc); 2235 if (error) { 2236 device_printf(dev, 2237 "cannot allocate mbufs for Rx virtqueue\n"); 2238 vtnet_stop(sc); 2239 return; 2240 } 2241 2242 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) { 2243 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) { 2244 /* Restore promiscuous and all-multicast modes. */ 2245 vtnet_rx_filter(sc); 2246 2247 /* Restore filtered MAC addresses. */ 2248 vtnet_rx_filter_mac(sc); 2249 } 2250 2251 /* Restore VLAN filters. */ 2252 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) 2253 vtnet_rx_filter_vlan(sc); 2254 } 2255 2256 #ifdef DEVICE_POLLING 2257 if (ifp->if_capenable & IFCAP_POLLING) { 2258 vtnet_disable_rx_intr(sc); 2259 vtnet_disable_tx_intr(sc); 2260 } else 2261 #endif 2262 { 2263 vtnet_enable_rx_intr(sc); 2264 vtnet_enable_tx_intr(sc); 2265 } 2266 2267 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2268 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2269 2270 virtio_reinit_complete(dev); 2271 2272 vtnet_update_link_status(sc); 2273 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc); 2274 } 2275 2276 static void 2277 vtnet_init(void *xsc) 2278 { 2279 struct vtnet_softc *sc; 2280 2281 sc = xsc; 2282 2283 VTNET_LOCK(sc); 2284 vtnet_init_locked(sc); 2285 VTNET_UNLOCK(sc); 2286 } 2287 2288 static void 2289 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie, 2290 struct sglist *sg, int readable, int writable) 2291 { 2292 struct virtqueue *vq; 2293 void *c; 2294 2295 vq = sc->vtnet_ctrl_vq; 2296 2297 VTNET_LOCK_ASSERT(sc); 2298 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ, 2299 ("no control virtqueue")); 2300 KASSERT(virtqueue_empty(vq), 2301 ("control command already enqueued")); 2302 2303 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0) 2304 return; 2305 2306 virtqueue_notify(vq); 2307 2308 /* 2309 * Poll until the command is complete. Previously, we would 2310 * sleep until the control virtqueue interrupt handler woke 2311 * us up, but dropping the VTNET_MTX leads to serialization 2312 * difficulties. 2313 * 2314 * Furthermore, it appears QEMU/KVM only allocates three MSIX 2315 * vectors. Two of those vectors are needed for the Rx and Tx 2316 * virtqueues. We do not support sharing both a Vq and config 2317 * changed notification on the same MSIX vector. 2318 */ 2319 c = virtqueue_poll(vq, NULL); 2320 KASSERT(c == cookie, ("unexpected control command response")); 2321 } 2322 2323 static void 2324 vtnet_rx_filter(struct vtnet_softc *sc) 2325 { 2326 device_t dev; 2327 struct ifnet *ifp; 2328 2329 dev = sc->vtnet_dev; 2330 ifp = sc->vtnet_ifp; 2331 2332 VTNET_LOCK_ASSERT(sc); 2333 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX, 2334 ("CTRL_RX feature not negotiated")); 2335 2336 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0) 2337 device_printf(dev, "cannot %s promiscuous mode\n", 2338 ifp->if_flags & IFF_PROMISC ? "enable" : "disable"); 2339 2340 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0) 2341 device_printf(dev, "cannot %s all-multicast mode\n", 2342 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable"); 2343 } 2344 2345 static int 2346 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on) 2347 { 2348 struct virtio_net_ctrl_hdr hdr; 2349 struct sglist_seg segs[3]; 2350 struct sglist sg; 2351 uint8_t onoff, ack; 2352 int error; 2353 2354 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) 2355 return (ENOTSUP); 2356 2357 error = 0; 2358 2359 hdr.class = VIRTIO_NET_CTRL_RX; 2360 hdr.cmd = cmd; 2361 onoff = !!on; 2362 ack = VIRTIO_NET_ERR; 2363 2364 sglist_init(&sg, 3, segs); 2365 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 2366 error |= sglist_append(&sg, &onoff, sizeof(uint8_t)); 2367 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 2368 KASSERT(error == 0 && sg.sg_nseg == 3, 2369 ("error adding Rx filter message to sglist")); 2370 2371 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 2372 2373 return (ack == VIRTIO_NET_OK ? 0 : EIO); 2374 } 2375 2376 static int 2377 vtnet_set_promisc(struct vtnet_softc *sc, int on) 2378 { 2379 2380 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on)); 2381 } 2382 2383 static int 2384 vtnet_set_allmulti(struct vtnet_softc *sc, int on) 2385 { 2386 2387 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on)); 2388 } 2389 2390 static void 2391 vtnet_rx_filter_mac(struct vtnet_softc *sc) 2392 { 2393 struct virtio_net_ctrl_hdr hdr; 2394 struct vtnet_mac_filter *filter; 2395 struct sglist_seg segs[4]; 2396 struct sglist sg; 2397 struct ifnet *ifp; 2398 struct ifaddr *ifa; 2399 struct ifmultiaddr *ifma; 2400 int ucnt, mcnt, promisc, allmulti, error; 2401 uint8_t ack; 2402 2403 ifp = sc->vtnet_ifp; 2404 filter = sc->vtnet_mac_filter; 2405 ucnt = 0; 2406 mcnt = 0; 2407 promisc = 0; 2408 allmulti = 0; 2409 error = 0; 2410 2411 VTNET_LOCK_ASSERT(sc); 2412 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX, 2413 ("CTRL_RX feature not negotiated")); 2414 2415 /* Unicast MAC addresses: */ 2416 if_addr_rlock(ifp); 2417 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2418 if (ifa->ifa_addr->sa_family != AF_LINK) 2419 continue; 2420 else if (ucnt == VTNET_MAX_MAC_ENTRIES) 2421 break; 2422 2423 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr), 2424 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN); 2425 ucnt++; 2426 } 2427 if_addr_runlock(ifp); 2428 2429 if (ucnt >= VTNET_MAX_MAC_ENTRIES) { 2430 promisc = 1; 2431 filter->vmf_unicast.nentries = 0; 2432 2433 if_printf(ifp, "more than %d MAC addresses assigned, " 2434 "falling back to promiscuous mode\n", 2435 VTNET_MAX_MAC_ENTRIES); 2436 } else 2437 filter->vmf_unicast.nentries = ucnt; 2438 2439 /* Multicast MAC addresses: */ 2440 if_maddr_rlock(ifp); 2441 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2442 if (ifma->ifma_addr->sa_family != AF_LINK) 2443 continue; 2444 else if (mcnt == VTNET_MAX_MAC_ENTRIES) 2445 break; 2446 2447 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 2448 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN); 2449 mcnt++; 2450 } 2451 if_maddr_runlock(ifp); 2452 2453 if (mcnt >= VTNET_MAX_MAC_ENTRIES) { 2454 allmulti = 1; 2455 filter->vmf_multicast.nentries = 0; 2456 2457 if_printf(ifp, "more than %d multicast MAC addresses " 2458 "assigned, falling back to all-multicast mode\n", 2459 VTNET_MAX_MAC_ENTRIES); 2460 } else 2461 filter->vmf_multicast.nentries = mcnt; 2462 2463 if (promisc && allmulti) 2464 goto out; 2465 2466 hdr.class = VIRTIO_NET_CTRL_MAC; 2467 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 2468 ack = VIRTIO_NET_ERR; 2469 2470 sglist_init(&sg, 4, segs); 2471 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 2472 error |= sglist_append(&sg, &filter->vmf_unicast, 2473 sizeof(struct vtnet_mac_table)); 2474 error |= sglist_append(&sg, &filter->vmf_multicast, 2475 sizeof(struct vtnet_mac_table)); 2476 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 2477 KASSERT(error == 0 && sg.sg_nseg == 4, 2478 ("error adding MAC filtering message to sglist")); 2479 2480 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 2481 2482 if (ack != VIRTIO_NET_OK) 2483 if_printf(ifp, "error setting host MAC filter table\n"); 2484 2485 out: 2486 if (promisc) 2487 if (vtnet_set_promisc(sc, 1) != 0) 2488 if_printf(ifp, "cannot enable promiscuous mode\n"); 2489 if (allmulti) 2490 if (vtnet_set_allmulti(sc, 1) != 0) 2491 if_printf(ifp, "cannot enable all-multicast mode\n"); 2492 } 2493 2494 static int 2495 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag) 2496 { 2497 struct virtio_net_ctrl_hdr hdr; 2498 struct sglist_seg segs[3]; 2499 struct sglist sg; 2500 uint8_t ack; 2501 int error; 2502 2503 hdr.class = VIRTIO_NET_CTRL_VLAN; 2504 hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 2505 ack = VIRTIO_NET_ERR; 2506 error = 0; 2507 2508 sglist_init(&sg, 3, segs); 2509 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr)); 2510 error |= sglist_append(&sg, &tag, sizeof(uint16_t)); 2511 error |= sglist_append(&sg, &ack, sizeof(uint8_t)); 2512 KASSERT(error == 0 && sg.sg_nseg == 3, 2513 ("error adding VLAN control message to sglist")); 2514 2515 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1); 2516 2517 return (ack == VIRTIO_NET_OK ? 0 : EIO); 2518 } 2519 2520 static void 2521 vtnet_rx_filter_vlan(struct vtnet_softc *sc) 2522 { 2523 device_t dev; 2524 uint32_t w, mask; 2525 uint16_t tag; 2526 int i, nvlans, error; 2527 2528 VTNET_LOCK_ASSERT(sc); 2529 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER, 2530 ("VLAN_FILTER feature not negotiated")); 2531 2532 dev = sc->vtnet_dev; 2533 nvlans = sc->vtnet_nvlans; 2534 error = 0; 2535 2536 /* Enable filtering for each configured VLAN. */ 2537 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) { 2538 w = sc->vtnet_vlan_shadow[i]; 2539 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) { 2540 if ((w & mask) != 0) { 2541 w &= ~mask; 2542 nvlans--; 2543 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) 2544 error++; 2545 } 2546 } 2547 } 2548 2549 KASSERT(nvlans == 0, ("VLAN count incorrect")); 2550 if (error) 2551 device_printf(dev, "cannot restore VLAN filter table\n"); 2552 } 2553 2554 static void 2555 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag) 2556 { 2557 struct ifnet *ifp; 2558 int idx, bit; 2559 2560 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER, 2561 ("VLAN_FILTER feature not negotiated")); 2562 2563 if ((tag == 0) || (tag > 4095)) 2564 return; 2565 2566 ifp = sc->vtnet_ifp; 2567 idx = (tag >> 5) & 0x7F; 2568 bit = tag & 0x1F; 2569 2570 VTNET_LOCK(sc); 2571 2572 /* Update shadow VLAN table. */ 2573 if (add) { 2574 sc->vtnet_nvlans++; 2575 sc->vtnet_vlan_shadow[idx] |= (1 << bit); 2576 } else { 2577 sc->vtnet_nvlans--; 2578 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit); 2579 } 2580 2581 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) { 2582 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) { 2583 device_printf(sc->vtnet_dev, 2584 "cannot %s VLAN %d %s the host filter table\n", 2585 add ? "add" : "remove", tag, 2586 add ? "to" : "from"); 2587 } 2588 } 2589 2590 VTNET_UNLOCK(sc); 2591 } 2592 2593 static void 2594 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag) 2595 { 2596 2597 if (ifp->if_softc != arg) 2598 return; 2599 2600 vtnet_set_vlan_filter(arg, 1, tag); 2601 } 2602 2603 static void 2604 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag) 2605 { 2606 2607 if (ifp->if_softc != arg) 2608 return; 2609 2610 vtnet_set_vlan_filter(arg, 0, tag); 2611 } 2612 2613 static int 2614 vtnet_ifmedia_upd(struct ifnet *ifp) 2615 { 2616 struct vtnet_softc *sc; 2617 struct ifmedia *ifm; 2618 2619 sc = ifp->if_softc; 2620 ifm = &sc->vtnet_media; 2621 2622 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 2623 return (EINVAL); 2624 2625 return (0); 2626 } 2627 2628 static void 2629 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2630 { 2631 struct vtnet_softc *sc; 2632 2633 sc = ifp->if_softc; 2634 2635 ifmr->ifm_status = IFM_AVALID; 2636 ifmr->ifm_active = IFM_ETHER; 2637 2638 VTNET_LOCK(sc); 2639 if (vtnet_is_link_up(sc) != 0) { 2640 ifmr->ifm_status |= IFM_ACTIVE; 2641 ifmr->ifm_active |= VTNET_MEDIATYPE; 2642 } else 2643 ifmr->ifm_active |= IFM_NONE; 2644 VTNET_UNLOCK(sc); 2645 } 2646 2647 static void 2648 vtnet_add_statistics(struct vtnet_softc *sc) 2649 { 2650 device_t dev; 2651 struct vtnet_statistics *stats; 2652 struct sysctl_ctx_list *ctx; 2653 struct sysctl_oid *tree; 2654 struct sysctl_oid_list *child; 2655 2656 dev = sc->vtnet_dev; 2657 stats = &sc->vtnet_stats; 2658 ctx = device_get_sysctl_ctx(dev); 2659 tree = device_get_sysctl_tree(dev); 2660 child = SYSCTL_CHILDREN(tree); 2661 2662 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_alloc_failed", 2663 CTLFLAG_RD, &stats->mbuf_alloc_failed, 2664 "Mbuf cluster allocation failures"); 2665 2666 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_frame_too_large", 2667 CTLFLAG_RD, &stats->rx_frame_too_large, 2668 "Received frame larger than the mbuf chain"); 2669 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_enq_replacement_failed", 2670 CTLFLAG_RD, &stats->rx_enq_replacement_failed, 2671 "Enqueuing the replacement receive mbuf failed"); 2672 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_mergeable_failed", 2673 CTLFLAG_RD, &stats->rx_mergeable_failed, 2674 "Mergeable buffers receive failures"); 2675 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ethtype", 2676 CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 2677 "Received checksum offloaded buffer with unsupported " 2678 "Ethernet type"); 2679 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_start", 2680 CTLFLAG_RD, &stats->rx_csum_bad_start, 2681 "Received checksum offloaded buffer with incorrect start offset"); 2682 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ipproto", 2683 CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 2684 "Received checksum offloaded buffer with incorrect IP protocol"); 2685 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_offset", 2686 CTLFLAG_RD, &stats->rx_csum_bad_offset, 2687 "Received checksum offloaded buffer with incorrect offset"); 2688 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_failed", 2689 CTLFLAG_RD, &stats->rx_csum_failed, 2690 "Received buffer checksum offload failed"); 2691 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_offloaded", 2692 CTLFLAG_RD, &stats->rx_csum_offloaded, 2693 "Received buffer checksum offload succeeded"); 2694 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_task_rescheduled", 2695 CTLFLAG_RD, &stats->rx_task_rescheduled, 2696 "Times the receive interrupt task rescheduled itself"); 2697 2698 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_offloaded", 2699 CTLFLAG_RD, &stats->tx_csum_offloaded, 2700 "Offloaded checksum of transmitted buffer"); 2701 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_offloaded", 2702 CTLFLAG_RD, &stats->tx_tso_offloaded, 2703 "Segmentation offload of transmitted buffer"); 2704 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_bad_ethtype", 2705 CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 2706 "Aborted transmit of checksum offloaded buffer with unknown " 2707 "Ethernet type"); 2708 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_bad_ethtype", 2709 CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 2710 "Aborted transmit of TSO buffer with unknown Ethernet type"); 2711 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_task_rescheduled", 2712 CTLFLAG_RD, &stats->tx_task_rescheduled, 2713 "Times the transmit interrupt task rescheduled itself"); 2714 } 2715 2716 static int 2717 vtnet_enable_rx_intr(struct vtnet_softc *sc) 2718 { 2719 2720 return (virtqueue_enable_intr(sc->vtnet_rx_vq)); 2721 } 2722 2723 static void 2724 vtnet_disable_rx_intr(struct vtnet_softc *sc) 2725 { 2726 2727 virtqueue_disable_intr(sc->vtnet_rx_vq); 2728 } 2729 2730 static int 2731 vtnet_enable_tx_intr(struct vtnet_softc *sc) 2732 { 2733 2734 #ifdef VTNET_TX_INTR_MODERATION 2735 return (0); 2736 #else 2737 return (virtqueue_enable_intr(sc->vtnet_tx_vq)); 2738 #endif 2739 } 2740 2741 static void 2742 vtnet_disable_tx_intr(struct vtnet_softc *sc) 2743 { 2744 2745 virtqueue_disable_intr(sc->vtnet_tx_vq); 2746 } 2747