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