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