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