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