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