2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
29 /* Driver for VirtIO network devices. */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
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>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 #include <sys/random.h>
45 #include <sys/sglist.h>
47 #include <sys/mutex.h>
48 #include <sys/taskqueue.h>
50 #include <machine/smp.h>
54 #include <net/ethernet.h>
56 #include <net/if_var.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_media.h>
61 #include <net/if_vlan_var.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet/udp.h>
71 #include <netinet/tcp.h>
72 #include <netinet/sctp.h>
73 #include <netinet/netdump/netdump.h>
75 #include <machine/bus.h>
76 #include <machine/resource.h>
80 #include <dev/virtio/virtio.h>
81 #include <dev/virtio/virtqueue.h>
82 #include <dev/virtio/network/virtio_net.h>
83 #include <dev/virtio/network/if_vtnetvar.h>
84 #include "virtio_if.h"
87 #include "opt_inet6.h"
89 static int vtnet_modevent(module_t, int, void *);
91 static int vtnet_probe(device_t);
92 static int vtnet_attach(device_t);
93 static int vtnet_detach(device_t);
94 static int vtnet_suspend(device_t);
95 static int vtnet_resume(device_t);
96 static int vtnet_shutdown(device_t);
97 static int vtnet_attach_completed(device_t);
98 static int vtnet_config_change(device_t);
100 static void vtnet_negotiate_features(struct vtnet_softc *);
101 static void vtnet_setup_features(struct vtnet_softc *);
102 static int vtnet_init_rxq(struct vtnet_softc *, int);
103 static int vtnet_init_txq(struct vtnet_softc *, int);
104 static int vtnet_alloc_rxtx_queues(struct vtnet_softc *);
105 static void vtnet_free_rxtx_queues(struct vtnet_softc *);
106 static int vtnet_alloc_rx_filters(struct vtnet_softc *);
107 static void vtnet_free_rx_filters(struct vtnet_softc *);
108 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
109 static int vtnet_setup_interface(struct vtnet_softc *);
110 static int vtnet_change_mtu(struct vtnet_softc *, int);
111 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t);
112 static uint64_t vtnet_get_counter(struct ifnet *, ift_counter);
114 static int vtnet_rxq_populate(struct vtnet_rxq *);
115 static void vtnet_rxq_free_mbufs(struct vtnet_rxq *);
117 vtnet_rx_alloc_buf(struct vtnet_softc *, int , struct mbuf **);
118 static int vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *,
120 static int vtnet_rxq_replace_buf(struct vtnet_rxq *, struct mbuf *, int);
121 static int vtnet_rxq_enqueue_buf(struct vtnet_rxq *, struct mbuf *);
122 static int vtnet_rxq_new_buf(struct vtnet_rxq *);
123 static int vtnet_rxq_csum(struct vtnet_rxq *, struct mbuf *,
124 struct virtio_net_hdr *);
125 static void vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *, int);
126 static void vtnet_rxq_discard_buf(struct vtnet_rxq *, struct mbuf *);
127 static int vtnet_rxq_merged_eof(struct vtnet_rxq *, struct mbuf *, int);
128 static void vtnet_rxq_input(struct vtnet_rxq *, struct mbuf *,
129 struct virtio_net_hdr *);
130 static int vtnet_rxq_eof(struct vtnet_rxq *);
131 static void vtnet_rx_vq_intr(void *);
132 static void vtnet_rxq_tq_intr(void *, int);
134 static int vtnet_txq_below_threshold(struct vtnet_txq *);
135 static int vtnet_txq_notify(struct vtnet_txq *);
136 static void vtnet_txq_free_mbufs(struct vtnet_txq *);
137 static int vtnet_txq_offload_ctx(struct vtnet_txq *, struct mbuf *,
138 int *, int *, int *);
139 static int vtnet_txq_offload_tso(struct vtnet_txq *, struct mbuf *, int,
140 int, struct virtio_net_hdr *);
142 vtnet_txq_offload(struct vtnet_txq *, struct mbuf *,
143 struct virtio_net_hdr *);
144 static int vtnet_txq_enqueue_buf(struct vtnet_txq *, struct mbuf **,
145 struct vtnet_tx_header *);
146 static int vtnet_txq_encap(struct vtnet_txq *, struct mbuf **, int);
147 #ifdef VTNET_LEGACY_TX
148 static void vtnet_start_locked(struct vtnet_txq *, struct ifnet *);
149 static void vtnet_start(struct ifnet *);
151 static int vtnet_txq_mq_start_locked(struct vtnet_txq *, struct mbuf *);
152 static int vtnet_txq_mq_start(struct ifnet *, struct mbuf *);
153 static void vtnet_txq_tq_deferred(void *, int);
155 static void vtnet_txq_start(struct vtnet_txq *);
156 static void vtnet_txq_tq_intr(void *, int);
157 static int vtnet_txq_eof(struct vtnet_txq *);
158 static void vtnet_tx_vq_intr(void *);
159 static void vtnet_tx_start_all(struct vtnet_softc *);
161 #ifndef VTNET_LEGACY_TX
162 static void vtnet_qflush(struct ifnet *);
165 static int vtnet_watchdog(struct vtnet_txq *);
166 static void vtnet_accum_stats(struct vtnet_softc *,
167 struct vtnet_rxq_stats *, struct vtnet_txq_stats *);
168 static void vtnet_tick(void *);
170 static void vtnet_start_taskqueues(struct vtnet_softc *);
171 static void vtnet_free_taskqueues(struct vtnet_softc *);
172 static void vtnet_drain_taskqueues(struct vtnet_softc *);
174 static void vtnet_drain_rxtx_queues(struct vtnet_softc *);
175 static void vtnet_stop_rendezvous(struct vtnet_softc *);
176 static void vtnet_stop(struct vtnet_softc *);
177 static int vtnet_virtio_reinit(struct vtnet_softc *);
178 static void vtnet_init_rx_filters(struct vtnet_softc *);
179 static int vtnet_init_rx_queues(struct vtnet_softc *);
180 static int vtnet_init_tx_queues(struct vtnet_softc *);
181 static int vtnet_init_rxtx_queues(struct vtnet_softc *);
182 static void vtnet_set_active_vq_pairs(struct vtnet_softc *);
183 static int vtnet_reinit(struct vtnet_softc *);
184 static void vtnet_init_locked(struct vtnet_softc *);
185 static void vtnet_init(void *);
187 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
188 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
189 struct sglist *, int, int);
190 static int vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
191 static int vtnet_ctrl_mq_cmd(struct vtnet_softc *, uint16_t);
192 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
193 static int vtnet_set_promisc(struct vtnet_softc *, int);
194 static int vtnet_set_allmulti(struct vtnet_softc *, int);
195 static void vtnet_attach_disable_promisc(struct vtnet_softc *);
196 static void vtnet_rx_filter(struct vtnet_softc *);
197 static void vtnet_rx_filter_mac(struct vtnet_softc *);
198 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
199 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
200 static void vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t);
201 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
202 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
204 static int vtnet_is_link_up(struct vtnet_softc *);
205 static void vtnet_update_link_status(struct vtnet_softc *);
206 static int vtnet_ifmedia_upd(struct ifnet *);
207 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
208 static void vtnet_get_hwaddr(struct vtnet_softc *);
209 static void vtnet_set_hwaddr(struct vtnet_softc *);
210 static void vtnet_vlan_tag_remove(struct mbuf *);
211 static void vtnet_set_rx_process_limit(struct vtnet_softc *);
212 static void vtnet_set_tx_intr_threshold(struct vtnet_softc *);
214 static void vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *,
215 struct sysctl_oid_list *, struct vtnet_rxq *);
216 static void vtnet_setup_txq_sysctl(struct sysctl_ctx_list *,
217 struct sysctl_oid_list *, struct vtnet_txq *);
218 static void vtnet_setup_queue_sysctl(struct vtnet_softc *);
219 static void vtnet_setup_sysctl(struct vtnet_softc *);
221 static int vtnet_rxq_enable_intr(struct vtnet_rxq *);
222 static void vtnet_rxq_disable_intr(struct vtnet_rxq *);
223 static int vtnet_txq_enable_intr(struct vtnet_txq *);
224 static void vtnet_txq_disable_intr(struct vtnet_txq *);
225 static void vtnet_enable_rx_interrupts(struct vtnet_softc *);
226 static void vtnet_enable_tx_interrupts(struct vtnet_softc *);
227 static void vtnet_enable_interrupts(struct vtnet_softc *);
228 static void vtnet_disable_rx_interrupts(struct vtnet_softc *);
229 static void vtnet_disable_tx_interrupts(struct vtnet_softc *);
230 static void vtnet_disable_interrupts(struct vtnet_softc *);
232 static int vtnet_tunable_int(struct vtnet_softc *, const char *, int);
234 NETDUMP_DEFINE(vtnet);
237 static SYSCTL_NODE(_hw, OID_AUTO, vtnet, CTLFLAG_RD, 0, "VNET driver parameters");
238 static int vtnet_csum_disable = 0;
239 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
240 SYSCTL_INT(_hw_vtnet, OID_AUTO, csum_disable, CTLFLAG_RDTUN,
241 &vtnet_csum_disable, 0, "Disables receive and send checksum offload");
242 static int vtnet_tso_disable = 0;
243 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
244 SYSCTL_INT(_hw_vtnet, OID_AUTO, tso_disable, CTLFLAG_RDTUN, &vtnet_tso_disable,
245 0, "Disables TCP Segmentation Offload");
246 static int vtnet_lro_disable = 0;
247 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
248 SYSCTL_INT(_hw_vtnet, OID_AUTO, lro_disable, CTLFLAG_RDTUN, &vtnet_lro_disable,
249 0, "Disables TCP Large Receive Offload");
250 static int vtnet_mq_disable = 0;
251 TUNABLE_INT("hw.vtnet.mq_disable", &vtnet_mq_disable);
252 SYSCTL_INT(_hw_vtnet, OID_AUTO, mq_disable, CTLFLAG_RDTUN, &vtnet_mq_disable,
253 0, "Disables Multi Queue support");
254 static int vtnet_mq_max_pairs = VTNET_MAX_QUEUE_PAIRS;
255 TUNABLE_INT("hw.vtnet.mq_max_pairs", &vtnet_mq_max_pairs);
256 SYSCTL_INT(_hw_vtnet, OID_AUTO, mq_max_pairs, CTLFLAG_RDTUN,
257 &vtnet_mq_max_pairs, 0, "Sets the maximum number of Multi Queue pairs");
258 static int vtnet_rx_process_limit = 512;
259 TUNABLE_INT("hw.vtnet.rx_process_limit", &vtnet_rx_process_limit);
260 SYSCTL_INT(_hw_vtnet, OID_AUTO, rx_process_limit, CTLFLAG_RDTUN,
261 &vtnet_rx_process_limit, 0,
262 "Limits the number RX segments processed in a single pass");
264 static uma_zone_t vtnet_tx_header_zone;
266 static struct virtio_feature_desc vtnet_feature_desc[] = {
267 { VIRTIO_NET_F_CSUM, "TxChecksum" },
268 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
269 { VIRTIO_NET_F_MAC, "MacAddress" },
270 { VIRTIO_NET_F_GSO, "TxAllGSO" },
271 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
272 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
273 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
274 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
275 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
276 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
277 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
278 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
279 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
280 { VIRTIO_NET_F_STATUS, "Status" },
281 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
282 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
283 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
284 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
285 { VIRTIO_NET_F_GUEST_ANNOUNCE, "GuestAnnounce" },
286 { VIRTIO_NET_F_MQ, "Multiqueue" },
287 { VIRTIO_NET_F_CTRL_MAC_ADDR, "SetMacAddress" },
292 static device_method_t vtnet_methods[] = {
293 /* Device methods. */
294 DEVMETHOD(device_probe, vtnet_probe),
295 DEVMETHOD(device_attach, vtnet_attach),
296 DEVMETHOD(device_detach, vtnet_detach),
297 DEVMETHOD(device_suspend, vtnet_suspend),
298 DEVMETHOD(device_resume, vtnet_resume),
299 DEVMETHOD(device_shutdown, vtnet_shutdown),
301 /* VirtIO methods. */
302 DEVMETHOD(virtio_attach_completed, vtnet_attach_completed),
303 DEVMETHOD(virtio_config_change, vtnet_config_change),
309 #include <dev/netmap/if_vtnet_netmap.h>
310 #endif /* DEV_NETMAP */
312 static driver_t vtnet_driver = {
315 sizeof(struct vtnet_softc)
317 static devclass_t vtnet_devclass;
319 DRIVER_MODULE(vtnet, virtio_mmio, vtnet_driver, vtnet_devclass,
321 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
323 MODULE_VERSION(vtnet, 1);
324 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
326 MODULE_DEPEND(vtnet, netmap, 1, 1, 1);
327 #endif /* DEV_NETMAP */
330 vtnet_modevent(module_t mod, int type, void *unused)
333 static int loaded = 0;
338 vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr",
339 sizeof(struct vtnet_tx_header),
340 NULL, NULL, NULL, NULL, 0, 0);
343 if (uma_zone_get_cur(vtnet_tx_header_zone) > 0)
348 uma_zdestroy(vtnet_tx_header_zone);
349 vtnet_tx_header_zone = NULL;
363 vtnet_probe(device_t dev)
366 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
369 device_set_desc(dev, "VirtIO Networking Adapter");
371 return (BUS_PROBE_DEFAULT);
375 vtnet_attach(device_t dev)
377 struct vtnet_softc *sc;
380 sc = device_get_softc(dev);
383 /* Register our feature descriptions. */
384 virtio_set_feature_desc(dev, vtnet_feature_desc);
386 VTNET_CORE_LOCK_INIT(sc);
387 callout_init_mtx(&sc->vtnet_tick_ch, VTNET_CORE_MTX(sc), 0);
389 vtnet_setup_sysctl(sc);
390 vtnet_setup_features(sc);
392 error = vtnet_alloc_rx_filters(sc);
394 device_printf(dev, "cannot allocate Rx filters\n");
398 error = vtnet_alloc_rxtx_queues(sc);
400 device_printf(dev, "cannot allocate queues\n");
404 error = vtnet_alloc_virtqueues(sc);
406 device_printf(dev, "cannot allocate virtqueues\n");
410 error = vtnet_setup_interface(sc);
412 device_printf(dev, "cannot setup interface\n");
416 error = virtio_setup_intr(dev, INTR_TYPE_NET);
418 device_printf(dev, "cannot setup virtqueue interrupts\n");
419 /* BMV: This will crash if during boot! */
420 ether_ifdetach(sc->vtnet_ifp);
425 vtnet_netmap_attach(sc);
426 #endif /* DEV_NETMAP */
428 vtnet_start_taskqueues(sc);
438 vtnet_detach(device_t dev)
440 struct vtnet_softc *sc;
443 sc = device_get_softc(dev);
446 if (device_is_attached(dev)) {
449 VTNET_CORE_UNLOCK(sc);
451 callout_drain(&sc->vtnet_tick_ch);
452 vtnet_drain_taskqueues(sc);
459 #endif /* DEV_NETMAP */
461 vtnet_free_taskqueues(sc);
463 if (sc->vtnet_vlan_attach != NULL) {
464 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
465 sc->vtnet_vlan_attach = NULL;
467 if (sc->vtnet_vlan_detach != NULL) {
468 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
469 sc->vtnet_vlan_detach = NULL;
472 ifmedia_removeall(&sc->vtnet_media);
476 sc->vtnet_ifp = NULL;
479 vtnet_free_rxtx_queues(sc);
480 vtnet_free_rx_filters(sc);
482 if (sc->vtnet_ctrl_vq != NULL)
483 vtnet_free_ctrl_vq(sc);
485 VTNET_CORE_LOCK_DESTROY(sc);
491 vtnet_suspend(device_t dev)
493 struct vtnet_softc *sc;
495 sc = device_get_softc(dev);
499 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
500 VTNET_CORE_UNLOCK(sc);
506 vtnet_resume(device_t dev)
508 struct vtnet_softc *sc;
511 sc = device_get_softc(dev);
515 if (ifp->if_flags & IFF_UP)
516 vtnet_init_locked(sc);
517 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
518 VTNET_CORE_UNLOCK(sc);
524 vtnet_shutdown(device_t dev)
528 * Suspend already does all of what we need to
529 * do here; we just never expect to be resumed.
531 return (vtnet_suspend(dev));
535 vtnet_attach_completed(device_t dev)
538 vtnet_attach_disable_promisc(device_get_softc(dev));
544 vtnet_config_change(device_t dev)
546 struct vtnet_softc *sc;
548 sc = device_get_softc(dev);
551 vtnet_update_link_status(sc);
552 if (sc->vtnet_link_active != 0)
553 vtnet_tx_start_all(sc);
554 VTNET_CORE_UNLOCK(sc);
560 vtnet_negotiate_features(struct vtnet_softc *sc)
563 uint64_t mask, features;
569 * TSO and LRO are only available when their corresponding checksum
570 * offload feature is also negotiated.
572 if (vtnet_tunable_int(sc, "csum_disable", vtnet_csum_disable)) {
573 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
574 mask |= VTNET_TSO_FEATURES | VTNET_LRO_FEATURES;
576 if (vtnet_tunable_int(sc, "tso_disable", vtnet_tso_disable))
577 mask |= VTNET_TSO_FEATURES;
578 if (vtnet_tunable_int(sc, "lro_disable", vtnet_lro_disable))
579 mask |= VTNET_LRO_FEATURES;
580 #ifndef VTNET_LEGACY_TX
581 if (vtnet_tunable_int(sc, "mq_disable", vtnet_mq_disable))
582 mask |= VIRTIO_NET_F_MQ;
584 mask |= VIRTIO_NET_F_MQ;
587 features = VTNET_FEATURES & ~mask;
588 sc->vtnet_features = virtio_negotiate_features(dev, features);
590 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
591 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
593 * LRO without mergeable buffers requires special care. This
594 * is not ideal because every receive buffer must be large
595 * enough to hold the maximum TCP packet, the Ethernet header,
596 * and the header. This requires up to 34 descriptors with
597 * MCLBYTES clusters. If we do not have indirect descriptors,
598 * LRO is disabled since the virtqueue will not contain very
599 * many receive buffers.
601 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
603 "LRO disabled due to both mergeable buffers and "
604 "indirect descriptors not negotiated\n");
606 features &= ~VTNET_LRO_FEATURES;
608 virtio_negotiate_features(dev, features);
610 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
615 vtnet_setup_features(struct vtnet_softc *sc)
621 vtnet_negotiate_features(sc);
623 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
624 sc->vtnet_flags |= VTNET_FLAG_INDIRECT;
625 if (virtio_with_feature(dev, VIRTIO_RING_F_EVENT_IDX))
626 sc->vtnet_flags |= VTNET_FLAG_EVENT_IDX;
628 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
629 /* This feature should always be negotiated. */
630 sc->vtnet_flags |= VTNET_FLAG_MAC;
633 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
634 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
635 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
637 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
639 if (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS)
640 sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
641 else if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG)
642 sc->vtnet_rx_nsegs = VTNET_MAX_RX_SEGS;
644 sc->vtnet_rx_nsegs = VTNET_MIN_RX_SEGS;
646 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO) ||
647 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
648 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
649 sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
651 sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
653 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
654 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
656 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
657 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
658 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
659 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
660 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR))
661 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
664 if (virtio_with_feature(dev, VIRTIO_NET_F_MQ) &&
665 sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
666 sc->vtnet_max_vq_pairs = virtio_read_dev_config_2(dev,
667 offsetof(struct virtio_net_config, max_virtqueue_pairs));
669 sc->vtnet_max_vq_pairs = 1;
671 if (sc->vtnet_max_vq_pairs > 1) {
673 * Limit the maximum number of queue pairs to the lower of
674 * the number of CPUs and the configured maximum.
675 * The actual number of queues that get used may be less.
679 max = vtnet_tunable_int(sc, "mq_max_pairs", vtnet_mq_max_pairs);
680 if (max > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN) {
683 if (max > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
684 max = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX;
686 sc->vtnet_requested_vq_pairs = max;
687 sc->vtnet_flags |= VTNET_FLAG_MULTIQ;
694 vtnet_init_rxq(struct vtnet_softc *sc, int id)
696 struct vtnet_rxq *rxq;
698 rxq = &sc->vtnet_rxqs[id];
700 snprintf(rxq->vtnrx_name, sizeof(rxq->vtnrx_name), "%s-rx%d",
701 device_get_nameunit(sc->vtnet_dev), id);
702 mtx_init(&rxq->vtnrx_mtx, rxq->vtnrx_name, NULL, MTX_DEF);
707 rxq->vtnrx_sg = sglist_alloc(sc->vtnet_rx_nsegs, M_NOWAIT);
708 if (rxq->vtnrx_sg == NULL)
711 TASK_INIT(&rxq->vtnrx_intrtask, 0, vtnet_rxq_tq_intr, rxq);
712 rxq->vtnrx_tq = taskqueue_create(rxq->vtnrx_name, M_NOWAIT,
713 taskqueue_thread_enqueue, &rxq->vtnrx_tq);
715 return (rxq->vtnrx_tq == NULL ? ENOMEM : 0);
719 vtnet_init_txq(struct vtnet_softc *sc, int id)
721 struct vtnet_txq *txq;
723 txq = &sc->vtnet_txqs[id];
725 snprintf(txq->vtntx_name, sizeof(txq->vtntx_name), "%s-tx%d",
726 device_get_nameunit(sc->vtnet_dev), id);
727 mtx_init(&txq->vtntx_mtx, txq->vtntx_name, NULL, MTX_DEF);
732 txq->vtntx_sg = sglist_alloc(sc->vtnet_tx_nsegs, M_NOWAIT);
733 if (txq->vtntx_sg == NULL)
736 #ifndef VTNET_LEGACY_TX
737 txq->vtntx_br = buf_ring_alloc(VTNET_DEFAULT_BUFRING_SIZE, M_DEVBUF,
738 M_NOWAIT, &txq->vtntx_mtx);
739 if (txq->vtntx_br == NULL)
742 TASK_INIT(&txq->vtntx_defrtask, 0, vtnet_txq_tq_deferred, txq);
744 TASK_INIT(&txq->vtntx_intrtask, 0, vtnet_txq_tq_intr, txq);
745 txq->vtntx_tq = taskqueue_create(txq->vtntx_name, M_NOWAIT,
746 taskqueue_thread_enqueue, &txq->vtntx_tq);
747 if (txq->vtntx_tq == NULL)
754 vtnet_alloc_rxtx_queues(struct vtnet_softc *sc)
756 int i, npairs, error;
758 npairs = sc->vtnet_max_vq_pairs;
760 sc->vtnet_rxqs = malloc(sizeof(struct vtnet_rxq) * npairs, M_DEVBUF,
762 sc->vtnet_txqs = malloc(sizeof(struct vtnet_txq) * npairs, M_DEVBUF,
764 if (sc->vtnet_rxqs == NULL || sc->vtnet_txqs == NULL)
767 for (i = 0; i < npairs; i++) {
768 error = vtnet_init_rxq(sc, i);
771 error = vtnet_init_txq(sc, i);
776 vtnet_setup_queue_sysctl(sc);
782 vtnet_destroy_rxq(struct vtnet_rxq *rxq)
785 rxq->vtnrx_sc = NULL;
788 if (rxq->vtnrx_sg != NULL) {
789 sglist_free(rxq->vtnrx_sg);
790 rxq->vtnrx_sg = NULL;
793 if (mtx_initialized(&rxq->vtnrx_mtx) != 0)
794 mtx_destroy(&rxq->vtnrx_mtx);
798 vtnet_destroy_txq(struct vtnet_txq *txq)
801 txq->vtntx_sc = NULL;
804 if (txq->vtntx_sg != NULL) {
805 sglist_free(txq->vtntx_sg);
806 txq->vtntx_sg = NULL;
809 #ifndef VTNET_LEGACY_TX
810 if (txq->vtntx_br != NULL) {
811 buf_ring_free(txq->vtntx_br, M_DEVBUF);
812 txq->vtntx_br = NULL;
816 if (mtx_initialized(&txq->vtntx_mtx) != 0)
817 mtx_destroy(&txq->vtntx_mtx);
821 vtnet_free_rxtx_queues(struct vtnet_softc *sc)
825 if (sc->vtnet_rxqs != NULL) {
826 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
827 vtnet_destroy_rxq(&sc->vtnet_rxqs[i]);
828 free(sc->vtnet_rxqs, M_DEVBUF);
829 sc->vtnet_rxqs = NULL;
832 if (sc->vtnet_txqs != NULL) {
833 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
834 vtnet_destroy_txq(&sc->vtnet_txqs[i]);
835 free(sc->vtnet_txqs, M_DEVBUF);
836 sc->vtnet_txqs = NULL;
841 vtnet_alloc_rx_filters(struct vtnet_softc *sc)
844 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
845 sc->vtnet_mac_filter = malloc(sizeof(struct vtnet_mac_filter),
846 M_DEVBUF, M_NOWAIT | M_ZERO);
847 if (sc->vtnet_mac_filter == NULL)
851 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
852 sc->vtnet_vlan_filter = malloc(sizeof(uint32_t) *
853 VTNET_VLAN_FILTER_NWORDS, M_DEVBUF, M_NOWAIT | M_ZERO);
854 if (sc->vtnet_vlan_filter == NULL)
862 vtnet_free_rx_filters(struct vtnet_softc *sc)
865 if (sc->vtnet_mac_filter != NULL) {
866 free(sc->vtnet_mac_filter, M_DEVBUF);
867 sc->vtnet_mac_filter = NULL;
870 if (sc->vtnet_vlan_filter != NULL) {
871 free(sc->vtnet_vlan_filter, M_DEVBUF);
872 sc->vtnet_vlan_filter = NULL;
877 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
880 struct vq_alloc_info *info;
881 struct vtnet_rxq *rxq;
882 struct vtnet_txq *txq;
883 int i, idx, flags, nvqs, error;
888 nvqs = sc->vtnet_max_vq_pairs * 2;
889 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
892 info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
896 for (i = 0, idx = 0; i < sc->vtnet_max_vq_pairs; i++, idx+=2) {
897 rxq = &sc->vtnet_rxqs[i];
898 VQ_ALLOC_INFO_INIT(&info[idx], sc->vtnet_rx_nsegs,
899 vtnet_rx_vq_intr, rxq, &rxq->vtnrx_vq,
900 "%s-%d rx", device_get_nameunit(dev), rxq->vtnrx_id);
902 txq = &sc->vtnet_txqs[i];
903 VQ_ALLOC_INFO_INIT(&info[idx+1], sc->vtnet_tx_nsegs,
904 vtnet_tx_vq_intr, txq, &txq->vtntx_vq,
905 "%s-%d tx", device_get_nameunit(dev), txq->vtntx_id);
908 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
909 VQ_ALLOC_INFO_INIT(&info[idx], 0, NULL, NULL,
910 &sc->vtnet_ctrl_vq, "%s ctrl", device_get_nameunit(dev));
914 * Enable interrupt binding if this is multiqueue. This only matters
915 * when per-vq MSIX is available.
917 if (sc->vtnet_flags & VTNET_FLAG_MULTIQ)
920 error = virtio_alloc_virtqueues(dev, flags, nvqs, info);
927 vtnet_setup_interface(struct vtnet_softc *sc)
934 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
936 device_printf(dev, "cannot allocate ifnet structure\n");
940 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
941 ifp->if_baudrate = IF_Gbps(10); /* Approx. */
943 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
944 ifp->if_init = vtnet_init;
945 ifp->if_ioctl = vtnet_ioctl;
946 ifp->if_get_counter = vtnet_get_counter;
947 #ifndef VTNET_LEGACY_TX
948 ifp->if_transmit = vtnet_txq_mq_start;
949 ifp->if_qflush = vtnet_qflush;
951 struct virtqueue *vq = sc->vtnet_txqs[0].vtntx_vq;
952 ifp->if_start = vtnet_start;
953 IFQ_SET_MAXLEN(&ifp->if_snd, virtqueue_size(vq) - 1);
954 ifp->if_snd.ifq_drv_maxlen = virtqueue_size(vq) - 1;
955 IFQ_SET_READY(&ifp->if_snd);
958 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
960 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
961 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
963 /* Read (or generate) the MAC address for the adapter. */
964 vtnet_get_hwaddr(sc);
966 ether_ifattach(ifp, sc->vtnet_hwaddr);
968 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS))
969 ifp->if_capabilities |= IFCAP_LINKSTATE;
971 /* Tell the upper layer(s) we support long frames. */
972 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
973 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
975 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
976 ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6;
978 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO)) {
979 ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6;
980 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
982 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
983 ifp->if_capabilities |= IFCAP_TSO4;
984 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
985 ifp->if_capabilities |= IFCAP_TSO6;
986 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
987 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
990 if (ifp->if_capabilities & IFCAP_TSO)
991 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
994 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
995 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
997 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
998 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
999 ifp->if_capabilities |= IFCAP_LRO;
1002 if (ifp->if_capabilities & IFCAP_HWCSUM) {
1004 * VirtIO does not support VLAN tagging, but we can fake
1005 * it by inserting and removing the 802.1Q header during
1006 * transmit and receive. We are then able to do checksum
1007 * offloading of VLAN frames.
1009 ifp->if_capabilities |=
1010 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
1013 ifp->if_capenable = ifp->if_capabilities;
1016 * Capabilities after here are not enabled by default.
1019 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
1020 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
1022 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1023 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
1024 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1025 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
1028 vtnet_set_rx_process_limit(sc);
1029 vtnet_set_tx_intr_threshold(sc);
1031 NETDUMP_SET(ifp, vtnet);
1037 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1040 int frame_size, clsize;
1042 ifp = sc->vtnet_ifp;
1044 if (new_mtu < ETHERMIN || new_mtu > VTNET_MAX_MTU)
1047 frame_size = sc->vtnet_hdr_size + sizeof(struct ether_vlan_header) +
1051 * Based on the new MTU (and hence frame size) determine which
1052 * cluster size is most appropriate for the receive queues.
1054 if (frame_size <= MCLBYTES) {
1056 } else if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1057 /* Avoid going past 9K jumbos. */
1058 if (frame_size > MJUM9BYTES)
1060 clsize = MJUM9BYTES;
1062 clsize = MJUMPAGESIZE;
1064 ifp->if_mtu = new_mtu;
1065 sc->vtnet_rx_new_clsize = clsize;
1067 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1068 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1069 vtnet_init_locked(sc);
1076 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1078 struct vtnet_softc *sc;
1080 int reinit, mask, error;
1083 ifr = (struct ifreq *) data;
1088 if (ifp->if_mtu != ifr->ifr_mtu) {
1089 VTNET_CORE_LOCK(sc);
1090 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
1091 VTNET_CORE_UNLOCK(sc);
1096 VTNET_CORE_LOCK(sc);
1097 if ((ifp->if_flags & IFF_UP) == 0) {
1098 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1100 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1101 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
1102 (IFF_PROMISC | IFF_ALLMULTI)) {
1103 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1104 vtnet_rx_filter(sc);
1106 ifp->if_flags |= IFF_PROMISC;
1107 if ((ifp->if_flags ^ sc->vtnet_if_flags)
1113 vtnet_init_locked(sc);
1116 sc->vtnet_if_flags = ifp->if_flags;
1117 VTNET_CORE_UNLOCK(sc);
1122 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
1124 VTNET_CORE_LOCK(sc);
1125 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1126 vtnet_rx_filter_mac(sc);
1127 VTNET_CORE_UNLOCK(sc);
1132 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1136 VTNET_CORE_LOCK(sc);
1137 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1139 if (mask & IFCAP_TXCSUM)
1140 ifp->if_capenable ^= IFCAP_TXCSUM;
1141 if (mask & IFCAP_TXCSUM_IPV6)
1142 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
1143 if (mask & IFCAP_TSO4)
1144 ifp->if_capenable ^= IFCAP_TSO4;
1145 if (mask & IFCAP_TSO6)
1146 ifp->if_capenable ^= IFCAP_TSO6;
1148 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO |
1149 IFCAP_VLAN_HWFILTER)) {
1150 /* These Rx features require us to renegotiate. */
1153 if (mask & IFCAP_RXCSUM)
1154 ifp->if_capenable ^= IFCAP_RXCSUM;
1155 if (mask & IFCAP_RXCSUM_IPV6)
1156 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
1157 if (mask & IFCAP_LRO)
1158 ifp->if_capenable ^= IFCAP_LRO;
1159 if (mask & IFCAP_VLAN_HWFILTER)
1160 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1164 if (mask & IFCAP_VLAN_HWTSO)
1165 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1166 if (mask & IFCAP_VLAN_HWTAGGING)
1167 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1169 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1170 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1171 vtnet_init_locked(sc);
1174 VTNET_CORE_UNLOCK(sc);
1175 VLAN_CAPABILITIES(ifp);
1180 error = ether_ioctl(ifp, cmd, data);
1184 VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc);
1190 vtnet_rxq_populate(struct vtnet_rxq *rxq)
1192 struct virtqueue *vq;
1196 error = vtnet_netmap_rxq_populate(rxq);
1199 #endif /* DEV_NETMAP */
1204 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1205 error = vtnet_rxq_new_buf(rxq);
1211 virtqueue_notify(vq);
1213 * EMSGSIZE signifies the virtqueue did not have enough
1214 * entries available to hold the last mbuf. This is not
1217 if (error == EMSGSIZE)
1225 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq)
1227 struct virtqueue *vq;
1231 int netmap_bufs = vtnet_netmap_queue_on(rxq->vtnrx_sc, NR_RX,
1233 #else /* !DEV_NETMAP */
1234 int netmap_bufs = 0;
1235 #endif /* !DEV_NETMAP */
1240 while ((m = virtqueue_drain(vq, &last)) != NULL) {
1245 KASSERT(virtqueue_empty(vq),
1246 ("%s: mbufs remaining in rx queue %p", __func__, rxq));
1249 static struct mbuf *
1250 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1252 struct mbuf *m_head, *m_tail, *m;
1255 clsize = sc->vtnet_rx_clsize;
1257 KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1258 ("%s: chained mbuf %d request without LRO_NOMRG", __func__, nbufs));
1260 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize);
1264 m_head->m_len = clsize;
1267 /* Allocate the rest of the chain. */
1268 for (i = 1; i < nbufs; i++) {
1269 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize);
1278 if (m_tailp != NULL)
1284 sc->vtnet_stats.mbuf_alloc_failed++;
1291 * Slow path for when LRO without mergeable buffers is negotiated.
1294 vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *rxq, struct mbuf *m0,
1297 struct vtnet_softc *sc;
1298 struct mbuf *m, *m_prev;
1299 struct mbuf *m_new, *m_tail;
1300 int len, clsize, nreplace, error;
1303 clsize = sc->vtnet_rx_clsize;
1313 * Since these mbuf chains are so large, we avoid allocating an
1314 * entire replacement chain if possible. When the received frame
1315 * did not consume the entire chain, the unused mbufs are moved
1316 * to the replacement chain.
1320 * Something is seriously wrong if we received a frame
1321 * larger than the chain. Drop it.
1324 sc->vtnet_stats.rx_frame_too_large++;
1328 /* We always allocate the same cluster size. */
1329 KASSERT(m->m_len == clsize,
1330 ("%s: mbuf size %d is not the cluster size %d",
1331 __func__, m->m_len, clsize));
1333 m->m_len = MIN(m->m_len, len);
1341 KASSERT(nreplace <= sc->vtnet_rx_nmbufs,
1342 ("%s: too many replacement mbufs %d max %d", __func__, nreplace,
1343 sc->vtnet_rx_nmbufs));
1345 m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail);
1346 if (m_new == NULL) {
1347 m_prev->m_len = clsize;
1352 * Move any unused mbufs from the received chain onto the end
1355 if (m_prev->m_next != NULL) {
1356 m_tail->m_next = m_prev->m_next;
1357 m_prev->m_next = NULL;
1360 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1363 * BAD! We could not enqueue the replacement mbuf chain. We
1364 * must restore the m0 chain to the original state if it was
1365 * modified so we can subsequently discard it.
1367 * NOTE: The replacement is suppose to be an identical copy
1368 * to the one just dequeued so this is an unexpected error.
1370 sc->vtnet_stats.rx_enq_replacement_failed++;
1372 if (m_tail->m_next != NULL) {
1373 m_prev->m_next = m_tail->m_next;
1374 m_tail->m_next = NULL;
1377 m_prev->m_len = clsize;
1385 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len)
1387 struct vtnet_softc *sc;
1393 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1394 ("%s: chained mbuf without LRO_NOMRG", __func__));
1396 if (m->m_next == NULL) {
1397 /* Fast-path for the common case of just one mbuf. */
1401 m_new = vtnet_rx_alloc_buf(sc, 1, NULL);
1405 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1408 * The new mbuf is suppose to be an identical
1409 * copy of the one just dequeued so this is an
1413 sc->vtnet_stats.rx_enq_replacement_failed++;
1417 error = vtnet_rxq_replace_lro_nomgr_buf(rxq, m, len);
1423 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1425 struct vtnet_softc *sc;
1427 struct vtnet_rx_header *rxhdr;
1433 mdata = mtod(m, uint8_t *);
1435 VTNET_RXQ_LOCK_ASSERT(rxq);
1436 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1437 ("%s: chained mbuf without LRO_NOMRG", __func__));
1438 KASSERT(m->m_len == sc->vtnet_rx_clsize,
1439 ("%s: unexpected cluster size %d/%d", __func__, m->m_len,
1440 sc->vtnet_rx_clsize));
1443 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1444 MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr));
1445 rxhdr = (struct vtnet_rx_header *) mdata;
1446 sglist_append(sg, &rxhdr->vrh_hdr, sc->vtnet_hdr_size);
1447 offset = sizeof(struct vtnet_rx_header);
1451 sglist_append(sg, mdata + offset, m->m_len - offset);
1452 if (m->m_next != NULL) {
1453 error = sglist_append_mbuf(sg, m->m_next);
1457 error = virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg);
1463 vtnet_rxq_new_buf(struct vtnet_rxq *rxq)
1465 struct vtnet_softc *sc;
1471 m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL);
1475 error = vtnet_rxq_enqueue_buf(rxq, m);
1483 * Use the checksum offset in the VirtIO header to set the
1484 * correct CSUM_* flags.
1487 vtnet_rxq_csum_by_offset(struct vtnet_rxq *rxq, struct mbuf *m,
1488 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1490 struct vtnet_softc *sc;
1491 #if defined(INET) || defined(INET6)
1492 int offset = hdr->csum_start + hdr->csum_offset;
1497 /* Only do a basic sanity check on the offset. */
1501 if (__predict_false(offset < ip_start + sizeof(struct ip)))
1506 case ETHERTYPE_IPV6:
1507 if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
1512 sc->vtnet_stats.rx_csum_bad_ethtype++;
1517 * Use the offset to determine the appropriate CSUM_* flags. This is
1518 * a bit dirty, but we can get by with it since the checksum offsets
1519 * happen to be different. We assume the host host does not do IPv4
1520 * header checksum offloading.
1522 switch (hdr->csum_offset) {
1523 case offsetof(struct udphdr, uh_sum):
1524 case offsetof(struct tcphdr, th_sum):
1525 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1526 m->m_pkthdr.csum_data = 0xFFFF;
1528 case offsetof(struct sctphdr, checksum):
1529 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1532 sc->vtnet_stats.rx_csum_bad_offset++;
1540 vtnet_rxq_csum_by_parse(struct vtnet_rxq *rxq, struct mbuf *m,
1541 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1543 struct vtnet_softc *sc;
1550 case ETHERTYPE_IP: {
1552 if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
1554 ip = (struct ip *)(m->m_data + ip_start);
1556 offset = ip_start + (ip->ip_hl << 2);
1561 case ETHERTYPE_IPV6:
1562 if (__predict_false(m->m_len < ip_start +
1563 sizeof(struct ip6_hdr)))
1565 offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
1566 if (__predict_false(offset < 0))
1571 sc->vtnet_stats.rx_csum_bad_ethtype++;
1577 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
1579 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1580 m->m_pkthdr.csum_data = 0xFFFF;
1583 if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
1585 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1586 m->m_pkthdr.csum_data = 0xFFFF;
1589 if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
1591 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1595 * For the remaining protocols, FreeBSD does not support
1596 * checksum offloading, so the checksum will be recomputed.
1599 if_printf(sc->vtnet_ifp, "cksum offload of unsupported "
1600 "protocol eth_type=%#x proto=%d csum_start=%d "
1601 "csum_offset=%d\n", __func__, eth_type, proto,
1602 hdr->csum_start, hdr->csum_offset);
1611 * Set the appropriate CSUM_* flags. Unfortunately, the information
1612 * provided is not directly useful to us. The VirtIO header gives the
1613 * offset of the checksum, which is all Linux needs, but this is not
1614 * how FreeBSD does things. We are forced to peek inside the packet
1617 * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
1618 * could accept the offsets and let the stack figure it out.
1621 vtnet_rxq_csum(struct vtnet_rxq *rxq, struct mbuf *m,
1622 struct virtio_net_hdr *hdr)
1624 struct ether_header *eh;
1625 struct ether_vlan_header *evh;
1629 eh = mtod(m, struct ether_header *);
1630 eth_type = ntohs(eh->ether_type);
1631 if (eth_type == ETHERTYPE_VLAN) {
1632 /* BMV: We should handle nested VLAN tags too. */
1633 evh = mtod(m, struct ether_vlan_header *);
1634 eth_type = ntohs(evh->evl_proto);
1635 offset = sizeof(struct ether_vlan_header);
1637 offset = sizeof(struct ether_header);
1639 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1640 error = vtnet_rxq_csum_by_offset(rxq, m, eth_type, offset, hdr);
1642 error = vtnet_rxq_csum_by_parse(rxq, m, eth_type, offset, hdr);
1648 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs)
1652 while (--nbufs > 0) {
1653 m = virtqueue_dequeue(rxq->vtnrx_vq, NULL);
1656 vtnet_rxq_discard_buf(rxq, m);
1661 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1666 * Requeue the discarded mbuf. This should always be successful
1667 * since it was just dequeued.
1669 error = vtnet_rxq_enqueue_buf(rxq, m);
1671 ("%s: cannot requeue discarded mbuf %d", __func__, error));
1675 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs)
1677 struct vtnet_softc *sc;
1678 struct virtqueue *vq;
1679 struct mbuf *m, *m_tail;
1686 while (--nbufs > 0) {
1687 m = virtqueue_dequeue(vq, &len);
1689 rxq->vtnrx_stats.vrxs_ierrors++;
1693 if (vtnet_rxq_new_buf(rxq) != 0) {
1694 rxq->vtnrx_stats.vrxs_iqdrops++;
1695 vtnet_rxq_discard_buf(rxq, m);
1697 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1705 m->m_flags &= ~M_PKTHDR;
1707 m_head->m_pkthdr.len += len;
1715 sc->vtnet_stats.rx_mergeable_failed++;
1722 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m,
1723 struct virtio_net_hdr *hdr)
1725 struct vtnet_softc *sc;
1727 struct ether_header *eh;
1730 ifp = sc->vtnet_ifp;
1732 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1733 eh = mtod(m, struct ether_header *);
1734 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1735 vtnet_vlan_tag_remove(m);
1737 * With the 802.1Q header removed, update the
1738 * checksum starting location accordingly.
1740 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1741 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN;
1745 m->m_pkthdr.flowid = rxq->vtnrx_id;
1746 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
1749 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum
1750 * distinction that Linux does. Need to reevaluate if performing
1751 * offloading for the NEEDS_CSUM case is really appropriate.
1753 if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM |
1754 VIRTIO_NET_HDR_F_DATA_VALID)) {
1755 if (vtnet_rxq_csum(rxq, m, hdr) == 0)
1756 rxq->vtnrx_stats.vrxs_csum++;
1758 rxq->vtnrx_stats.vrxs_csum_failed++;
1761 rxq->vtnrx_stats.vrxs_ipackets++;
1762 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len;
1764 VTNET_RXQ_UNLOCK(rxq);
1765 (*ifp->if_input)(ifp, m);
1766 VTNET_RXQ_LOCK(rxq);
1770 vtnet_rxq_eof(struct vtnet_rxq *rxq)
1772 struct virtio_net_hdr lhdr, *hdr;
1773 struct vtnet_softc *sc;
1775 struct virtqueue *vq;
1777 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1778 int len, deq, nbufs, adjsz, count;
1782 ifp = sc->vtnet_ifp;
1785 count = sc->vtnet_rx_process_limit;
1787 VTNET_RXQ_LOCK_ASSERT(rxq);
1789 while (count-- > 0) {
1790 m = virtqueue_dequeue(vq, &len);
1795 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1796 rxq->vtnrx_stats.vrxs_ierrors++;
1797 vtnet_rxq_discard_buf(rxq, m);
1801 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1803 adjsz = sizeof(struct vtnet_rx_header);
1805 * Account for our pad inserted between the header
1806 * and the actual start of the frame.
1808 len += VTNET_RX_HEADER_PAD;
1810 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1811 nbufs = mhdr->num_buffers;
1812 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1815 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) {
1816 rxq->vtnrx_stats.vrxs_iqdrops++;
1817 vtnet_rxq_discard_buf(rxq, m);
1819 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1823 m->m_pkthdr.len = len;
1824 m->m_pkthdr.rcvif = ifp;
1825 m->m_pkthdr.csum_flags = 0;
1828 /* Dequeue the rest of chain. */
1829 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0)
1834 * Save copy of header before we strip it. For both mergeable
1835 * and non-mergeable, the header is at the beginning of the
1836 * mbuf data. We no longer need num_buffers, so always use a
1839 * BMV: Is this memcpy() expensive? We know the mbuf data is
1840 * still valid even after the m_adj().
1842 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1845 vtnet_rxq_input(rxq, m, hdr);
1847 /* Must recheck after dropping the Rx lock. */
1848 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1853 virtqueue_notify(vq);
1855 return (count > 0 ? 0 : EAGAIN);
1859 vtnet_rx_vq_intr(void *xrxq)
1861 struct vtnet_softc *sc;
1862 struct vtnet_rxq *rxq;
1868 ifp = sc->vtnet_ifp;
1871 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) {
1873 * Ignore this interrupt. Either this is a spurious interrupt
1874 * or multiqueue without per-VQ MSIX so every queue needs to
1875 * be polled (a brain dead configuration we could try harder
1878 vtnet_rxq_disable_intr(rxq);
1883 if (netmap_rx_irq(ifp, rxq->vtnrx_id, &more) != NM_IRQ_PASS)
1885 #endif /* DEV_NETMAP */
1887 VTNET_RXQ_LOCK(rxq);
1890 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1891 VTNET_RXQ_UNLOCK(rxq);
1895 more = vtnet_rxq_eof(rxq);
1896 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1898 vtnet_rxq_disable_intr(rxq);
1900 * This is an occasional condition or race (when !more),
1901 * so retry a few times before scheduling the taskqueue.
1903 if (tries++ < VTNET_INTR_DISABLE_RETRIES)
1906 VTNET_RXQ_UNLOCK(rxq);
1907 rxq->vtnrx_stats.vrxs_rescheduled++;
1908 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1910 VTNET_RXQ_UNLOCK(rxq);
1914 vtnet_rxq_tq_intr(void *xrxq, int pending)
1916 struct vtnet_softc *sc;
1917 struct vtnet_rxq *rxq;
1923 ifp = sc->vtnet_ifp;
1925 VTNET_RXQ_LOCK(rxq);
1927 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1928 VTNET_RXQ_UNLOCK(rxq);
1932 more = vtnet_rxq_eof(rxq);
1933 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1935 vtnet_rxq_disable_intr(rxq);
1936 rxq->vtnrx_stats.vrxs_rescheduled++;
1937 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1940 VTNET_RXQ_UNLOCK(rxq);
1944 vtnet_txq_below_threshold(struct vtnet_txq *txq)
1946 struct vtnet_softc *sc;
1947 struct virtqueue *vq;
1952 return (virtqueue_nfree(vq) <= sc->vtnet_tx_intr_thresh);
1956 vtnet_txq_notify(struct vtnet_txq *txq)
1958 struct virtqueue *vq;
1962 txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
1963 virtqueue_notify(vq);
1965 if (vtnet_txq_enable_intr(txq) == 0)
1969 * Drain frames that were completed since last checked. If this
1970 * causes the queue to go above the threshold, the caller should
1971 * continue transmitting.
1973 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) {
1974 virtqueue_disable_intr(vq);
1982 vtnet_txq_free_mbufs(struct vtnet_txq *txq)
1984 struct virtqueue *vq;
1985 struct vtnet_tx_header *txhdr;
1988 int netmap_bufs = vtnet_netmap_queue_on(txq->vtntx_sc, NR_TX,
1990 #else /* !DEV_NETMAP */
1991 int netmap_bufs = 0;
1992 #endif /* !DEV_NETMAP */
1997 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1999 m_freem(txhdr->vth_mbuf);
2000 uma_zfree(vtnet_tx_header_zone, txhdr);
2004 KASSERT(virtqueue_empty(vq),
2005 ("%s: mbufs remaining in tx queue %p", __func__, txq));
2009 * BMV: Much of this can go away once we finally have offsets in
2010 * the mbuf packet header. Bug andre@.
2013 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m,
2014 int *etype, int *proto, int *start)
2016 struct vtnet_softc *sc;
2017 struct ether_vlan_header *evh;
2022 evh = mtod(m, struct ether_vlan_header *);
2023 if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
2024 /* BMV: We should handle nested VLAN tags too. */
2025 *etype = ntohs(evh->evl_proto);
2026 offset = sizeof(struct ether_vlan_header);
2028 *etype = ntohs(evh->evl_encap_proto);
2029 offset = sizeof(struct ether_header);
2034 case ETHERTYPE_IP: {
2035 struct ip *ip, iphdr;
2036 if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
2037 m_copydata(m, offset, sizeof(struct ip),
2041 ip = (struct ip *)(m->m_data + offset);
2043 *start = offset + (ip->ip_hl << 2);
2048 case ETHERTYPE_IPV6:
2050 *start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
2051 /* Assert the network stack sent us a valid packet. */
2052 KASSERT(*start > offset,
2053 ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
2054 *start, offset, *proto));
2058 sc->vtnet_stats.tx_csum_bad_ethtype++;
2066 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type,
2067 int offset, struct virtio_net_hdr *hdr)
2069 static struct timeval lastecn;
2071 struct vtnet_softc *sc;
2072 struct tcphdr *tcp, tcphdr;
2076 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
2077 m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
2080 tcp = (struct tcphdr *)(m->m_data + offset);
2082 hdr->hdr_len = offset + (tcp->th_off << 2);
2083 hdr->gso_size = m->m_pkthdr.tso_segsz;
2084 hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
2085 VIRTIO_NET_HDR_GSO_TCPV6;
2087 if (tcp->th_flags & TH_CWR) {
2089 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
2090 * ECN support is not on a per-interface basis, but globally via
2091 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
2093 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
2094 if (ppsratecheck(&lastecn, &curecn, 1))
2095 if_printf(sc->vtnet_ifp,
2096 "TSO with ECN not negotiated with host\n");
2099 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2102 txq->vtntx_stats.vtxs_tso++;
2107 static struct mbuf *
2108 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m,
2109 struct virtio_net_hdr *hdr)
2111 struct vtnet_softc *sc;
2112 int flags, etype, csum_start, proto, error;
2115 flags = m->m_pkthdr.csum_flags;
2117 error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start);
2121 if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) ||
2122 (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) {
2124 * We could compare the IP protocol vs the CSUM_ flag too,
2125 * but that really should not be necessary.
2127 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
2128 hdr->csum_start = csum_start;
2129 hdr->csum_offset = m->m_pkthdr.csum_data;
2130 txq->vtntx_stats.vtxs_csum++;
2133 if (flags & CSUM_TSO) {
2134 if (__predict_false(proto != IPPROTO_TCP)) {
2135 /* Likely failed to correctly parse the mbuf. */
2136 sc->vtnet_stats.tx_tso_not_tcp++;
2140 KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
2141 ("%s: mbuf %p TSO without checksum offload %#x",
2142 __func__, m, flags));
2144 error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr);
2157 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head,
2158 struct vtnet_tx_header *txhdr)
2160 struct vtnet_softc *sc;
2161 struct virtqueue *vq;
2172 error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
2173 KASSERT(error == 0 && sg->sg_nseg == 1,
2174 ("%s: error %d adding header to sglist", __func__, error));
2176 error = sglist_append_mbuf(sg, m);
2178 m = m_defrag(m, M_NOWAIT);
2183 sc->vtnet_stats.tx_defragged++;
2185 error = sglist_append_mbuf(sg, m);
2190 txhdr->vth_mbuf = m;
2191 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0);
2196 sc->vtnet_stats.tx_defrag_failed++;
2204 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head, int flags)
2206 struct vtnet_tx_header *txhdr;
2207 struct virtio_net_hdr *hdr;
2214 txhdr = uma_zalloc(vtnet_tx_header_zone, flags | M_ZERO);
2215 if (txhdr == NULL) {
2222 * Always use the non-mergeable header, regardless if the feature
2223 * was negotiated. For transmit, num_buffers is always zero. The
2224 * vtnet_hdr_size is used to enqueue the correct header size.
2226 hdr = &txhdr->vth_uhdr.hdr;
2228 if (m->m_flags & M_VLANTAG) {
2229 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2230 if ((*m_head = m) == NULL) {
2234 m->m_flags &= ~M_VLANTAG;
2237 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) {
2238 m = vtnet_txq_offload(txq, m, hdr);
2239 if ((*m_head = m) == NULL) {
2245 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr);
2250 uma_zfree(vtnet_tx_header_zone, txhdr);
2255 #ifdef VTNET_LEGACY_TX
2258 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp)
2260 struct vtnet_softc *sc;
2261 struct virtqueue *vq;
2269 VTNET_TXQ_LOCK_ASSERT(txq);
2271 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2272 sc->vtnet_link_active == 0)
2280 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2281 if (virtqueue_full(vq))
2284 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2288 if (vtnet_txq_encap(txq, &m0, M_NOWAIT) != 0) {
2290 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2295 ETHER_BPF_MTAP(ifp, m0);
2298 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2299 if (tries++ < VTNET_NOTIFY_RETRIES)
2302 txq->vtntx_stats.vtxs_rescheduled++;
2303 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2308 vtnet_start(struct ifnet *ifp)
2310 struct vtnet_softc *sc;
2311 struct vtnet_txq *txq;
2314 txq = &sc->vtnet_txqs[0];
2316 VTNET_TXQ_LOCK(txq);
2317 vtnet_start_locked(txq, ifp);
2318 VTNET_TXQ_UNLOCK(txq);
2321 #else /* !VTNET_LEGACY_TX */
2324 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m)
2326 struct vtnet_softc *sc;
2327 struct virtqueue *vq;
2328 struct buf_ring *br;
2330 int enq, tries, error;
2335 ifp = sc->vtnet_ifp;
2339 VTNET_TXQ_LOCK_ASSERT(txq);
2341 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2342 sc->vtnet_link_active == 0) {
2344 error = drbr_enqueue(ifp, br, m);
2349 error = drbr_enqueue(ifp, br, m);
2359 while ((m = drbr_peek(ifp, br)) != NULL) {
2360 if (virtqueue_full(vq)) {
2361 drbr_putback(ifp, br, m);
2365 if (vtnet_txq_encap(txq, &m, M_NOWAIT) != 0) {
2367 drbr_putback(ifp, br, m);
2369 drbr_advance(ifp, br);
2372 drbr_advance(ifp, br);
2375 ETHER_BPF_MTAP(ifp, m);
2378 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2379 if (tries++ < VTNET_NOTIFY_RETRIES)
2382 txq->vtntx_stats.vtxs_rescheduled++;
2383 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2390 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m)
2392 struct vtnet_softc *sc;
2393 struct vtnet_txq *txq;
2394 int i, npairs, error;
2397 npairs = sc->vtnet_act_vq_pairs;
2399 /* check if flowid is set */
2400 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2401 i = m->m_pkthdr.flowid % npairs;
2403 i = curcpu % npairs;
2405 txq = &sc->vtnet_txqs[i];
2407 if (VTNET_TXQ_TRYLOCK(txq) != 0) {
2408 error = vtnet_txq_mq_start_locked(txq, m);
2409 VTNET_TXQ_UNLOCK(txq);
2411 error = drbr_enqueue(ifp, txq->vtntx_br, m);
2412 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask);
2419 vtnet_txq_tq_deferred(void *xtxq, int pending)
2421 struct vtnet_softc *sc;
2422 struct vtnet_txq *txq;
2427 VTNET_TXQ_LOCK(txq);
2428 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br))
2429 vtnet_txq_mq_start_locked(txq, NULL);
2430 VTNET_TXQ_UNLOCK(txq);
2433 #endif /* VTNET_LEGACY_TX */
2436 vtnet_txq_start(struct vtnet_txq *txq)
2438 struct vtnet_softc *sc;
2442 ifp = sc->vtnet_ifp;
2444 #ifdef VTNET_LEGACY_TX
2445 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2446 vtnet_start_locked(txq, ifp);
2448 if (!drbr_empty(ifp, txq->vtntx_br))
2449 vtnet_txq_mq_start_locked(txq, NULL);
2454 vtnet_txq_tq_intr(void *xtxq, int pending)
2456 struct vtnet_softc *sc;
2457 struct vtnet_txq *txq;
2462 ifp = sc->vtnet_ifp;
2464 VTNET_TXQ_LOCK(txq);
2466 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2467 VTNET_TXQ_UNLOCK(txq);
2472 vtnet_txq_start(txq);
2474 VTNET_TXQ_UNLOCK(txq);
2478 vtnet_txq_eof(struct vtnet_txq *txq)
2480 struct virtqueue *vq;
2481 struct vtnet_tx_header *txhdr;
2487 VTNET_TXQ_LOCK_ASSERT(txq);
2489 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
2490 m = txhdr->vth_mbuf;
2493 txq->vtntx_stats.vtxs_opackets++;
2494 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len;
2495 if (m->m_flags & M_MCAST)
2496 txq->vtntx_stats.vtxs_omcasts++;
2499 uma_zfree(vtnet_tx_header_zone, txhdr);
2502 if (virtqueue_empty(vq))
2503 txq->vtntx_watchdog = 0;
2509 vtnet_tx_vq_intr(void *xtxq)
2511 struct vtnet_softc *sc;
2512 struct vtnet_txq *txq;
2517 ifp = sc->vtnet_ifp;
2519 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) {
2521 * Ignore this interrupt. Either this is a spurious interrupt
2522 * or multiqueue without per-VQ MSIX so every queue needs to
2523 * be polled (a brain dead configuration we could try harder
2526 vtnet_txq_disable_intr(txq);
2531 if (netmap_tx_irq(ifp, txq->vtntx_id) != NM_IRQ_PASS)
2533 #endif /* DEV_NETMAP */
2535 VTNET_TXQ_LOCK(txq);
2537 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2538 VTNET_TXQ_UNLOCK(txq);
2543 vtnet_txq_start(txq);
2545 VTNET_TXQ_UNLOCK(txq);
2549 vtnet_tx_start_all(struct vtnet_softc *sc)
2551 struct vtnet_txq *txq;
2554 VTNET_CORE_LOCK_ASSERT(sc);
2556 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2557 txq = &sc->vtnet_txqs[i];
2559 VTNET_TXQ_LOCK(txq);
2560 vtnet_txq_start(txq);
2561 VTNET_TXQ_UNLOCK(txq);
2565 #ifndef VTNET_LEGACY_TX
2567 vtnet_qflush(struct ifnet *ifp)
2569 struct vtnet_softc *sc;
2570 struct vtnet_txq *txq;
2576 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2577 txq = &sc->vtnet_txqs[i];
2579 VTNET_TXQ_LOCK(txq);
2580 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL)
2582 VTNET_TXQ_UNLOCK(txq);
2590 vtnet_watchdog(struct vtnet_txq *txq)
2594 ifp = txq->vtntx_sc->vtnet_ifp;
2596 VTNET_TXQ_LOCK(txq);
2597 if (txq->vtntx_watchdog == 1) {
2599 * Only drain completed frames if the watchdog is about to
2600 * expire. If any frames were drained, there may be enough
2601 * free descriptors now available to transmit queued frames.
2602 * In that case, the timer will immediately be decremented
2603 * below, but the timeout is generous enough that should not
2606 if (vtnet_txq_eof(txq) != 0)
2607 vtnet_txq_start(txq);
2610 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) {
2611 VTNET_TXQ_UNLOCK(txq);
2614 VTNET_TXQ_UNLOCK(txq);
2616 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id);
2621 vtnet_accum_stats(struct vtnet_softc *sc, struct vtnet_rxq_stats *rxacc,
2622 struct vtnet_txq_stats *txacc)
2625 bzero(rxacc, sizeof(struct vtnet_rxq_stats));
2626 bzero(txacc, sizeof(struct vtnet_txq_stats));
2628 for (int i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2629 struct vtnet_rxq_stats *rxst;
2630 struct vtnet_txq_stats *txst;
2632 rxst = &sc->vtnet_rxqs[i].vtnrx_stats;
2633 rxacc->vrxs_ipackets += rxst->vrxs_ipackets;
2634 rxacc->vrxs_ibytes += rxst->vrxs_ibytes;
2635 rxacc->vrxs_iqdrops += rxst->vrxs_iqdrops;
2636 rxacc->vrxs_csum += rxst->vrxs_csum;
2637 rxacc->vrxs_csum_failed += rxst->vrxs_csum_failed;
2638 rxacc->vrxs_rescheduled += rxst->vrxs_rescheduled;
2640 txst = &sc->vtnet_txqs[i].vtntx_stats;
2641 txacc->vtxs_opackets += txst->vtxs_opackets;
2642 txacc->vtxs_obytes += txst->vtxs_obytes;
2643 txacc->vtxs_csum += txst->vtxs_csum;
2644 txacc->vtxs_tso += txst->vtxs_tso;
2645 txacc->vtxs_rescheduled += txst->vtxs_rescheduled;
2650 vtnet_get_counter(if_t ifp, ift_counter cnt)
2652 struct vtnet_softc *sc;
2653 struct vtnet_rxq_stats rxaccum;
2654 struct vtnet_txq_stats txaccum;
2656 sc = if_getsoftc(ifp);
2657 vtnet_accum_stats(sc, &rxaccum, &txaccum);
2660 case IFCOUNTER_IPACKETS:
2661 return (rxaccum.vrxs_ipackets);
2662 case IFCOUNTER_IQDROPS:
2663 return (rxaccum.vrxs_iqdrops);
2664 case IFCOUNTER_IERRORS:
2665 return (rxaccum.vrxs_ierrors);
2666 case IFCOUNTER_OPACKETS:
2667 return (txaccum.vtxs_opackets);
2668 #ifndef VTNET_LEGACY_TX
2669 case IFCOUNTER_OBYTES:
2670 return (txaccum.vtxs_obytes);
2671 case IFCOUNTER_OMCASTS:
2672 return (txaccum.vtxs_omcasts);
2675 return (if_get_counter_default(ifp, cnt));
2680 vtnet_tick(void *xsc)
2682 struct vtnet_softc *sc;
2687 ifp = sc->vtnet_ifp;
2690 VTNET_CORE_LOCK_ASSERT(sc);
2692 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
2693 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]);
2695 if (timedout != 0) {
2696 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2697 vtnet_init_locked(sc);
2699 callout_schedule(&sc->vtnet_tick_ch, hz);
2703 vtnet_start_taskqueues(struct vtnet_softc *sc)
2706 struct vtnet_rxq *rxq;
2707 struct vtnet_txq *txq;
2710 dev = sc->vtnet_dev;
2713 * Errors here are very difficult to recover from - we cannot
2714 * easily fail because, if this is during boot, we will hang
2715 * when freeing any successfully started taskqueues because
2716 * the scheduler isn't up yet.
2718 * Most drivers just ignore the return value - it only fails
2719 * with ENOMEM so an error is not likely.
2721 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2722 rxq = &sc->vtnet_rxqs[i];
2723 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET,
2724 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id);
2726 device_printf(dev, "failed to start rx taskq %d\n",
2730 txq = &sc->vtnet_txqs[i];
2731 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET,
2732 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id);
2734 device_printf(dev, "failed to start tx taskq %d\n",
2741 vtnet_free_taskqueues(struct vtnet_softc *sc)
2743 struct vtnet_rxq *rxq;
2744 struct vtnet_txq *txq;
2747 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2748 rxq = &sc->vtnet_rxqs[i];
2749 if (rxq->vtnrx_tq != NULL) {
2750 taskqueue_free(rxq->vtnrx_tq);
2751 rxq->vtnrx_tq = NULL;
2754 txq = &sc->vtnet_txqs[i];
2755 if (txq->vtntx_tq != NULL) {
2756 taskqueue_free(txq->vtntx_tq);
2757 txq->vtntx_tq = NULL;
2763 vtnet_drain_taskqueues(struct vtnet_softc *sc)
2765 struct vtnet_rxq *rxq;
2766 struct vtnet_txq *txq;
2769 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2770 rxq = &sc->vtnet_rxqs[i];
2771 if (rxq->vtnrx_tq != NULL)
2772 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2774 txq = &sc->vtnet_txqs[i];
2775 if (txq->vtntx_tq != NULL) {
2776 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask);
2777 #ifndef VTNET_LEGACY_TX
2778 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask);
2785 vtnet_drain_rxtx_queues(struct vtnet_softc *sc)
2787 struct vtnet_rxq *rxq;
2788 struct vtnet_txq *txq;
2791 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2792 rxq = &sc->vtnet_rxqs[i];
2793 vtnet_rxq_free_mbufs(rxq);
2795 txq = &sc->vtnet_txqs[i];
2796 vtnet_txq_free_mbufs(txq);
2801 vtnet_stop_rendezvous(struct vtnet_softc *sc)
2803 struct vtnet_rxq *rxq;
2804 struct vtnet_txq *txq;
2808 * Lock and unlock the per-queue mutex so we known the stop
2809 * state is visible. Doing only the active queues should be
2810 * sufficient, but it does not cost much extra to do all the
2811 * queues. Note we hold the core mutex here too.
2813 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2814 rxq = &sc->vtnet_rxqs[i];
2815 VTNET_RXQ_LOCK(rxq);
2816 VTNET_RXQ_UNLOCK(rxq);
2818 txq = &sc->vtnet_txqs[i];
2819 VTNET_TXQ_LOCK(txq);
2820 VTNET_TXQ_UNLOCK(txq);
2825 vtnet_stop(struct vtnet_softc *sc)
2830 dev = sc->vtnet_dev;
2831 ifp = sc->vtnet_ifp;
2833 VTNET_CORE_LOCK_ASSERT(sc);
2835 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2836 sc->vtnet_link_active = 0;
2837 callout_stop(&sc->vtnet_tick_ch);
2839 /* Only advisory. */
2840 vtnet_disable_interrupts(sc);
2843 * Stop the host adapter. This resets it to the pre-initialized
2844 * state. It will not generate any interrupts until after it is
2848 vtnet_stop_rendezvous(sc);
2850 /* Free any mbufs left in the virtqueues. */
2851 vtnet_drain_rxtx_queues(sc);
2855 vtnet_virtio_reinit(struct vtnet_softc *sc)
2862 dev = sc->vtnet_dev;
2863 ifp = sc->vtnet_ifp;
2864 features = sc->vtnet_features;
2868 mask |= IFCAP_RXCSUM;
2871 mask |= IFCAP_RXCSUM_IPV6;
2875 * Re-negotiate with the host, removing any disabled receive
2876 * features. Transmit features are disabled only on our side
2877 * via if_capenable and if_hwassist.
2880 if (ifp->if_capabilities & mask) {
2882 * We require both IPv4 and IPv6 offloading to be enabled
2883 * in order to negotiated it: VirtIO does not distinguish
2886 if ((ifp->if_capenable & mask) != mask)
2887 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2890 if (ifp->if_capabilities & IFCAP_LRO) {
2891 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2892 features &= ~VTNET_LRO_FEATURES;
2895 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2896 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2897 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2900 error = virtio_reinit(dev, features);
2902 device_printf(dev, "virtio reinit error %d\n", error);
2908 vtnet_init_rx_filters(struct vtnet_softc *sc)
2912 ifp = sc->vtnet_ifp;
2914 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2915 /* Restore promiscuous and all-multicast modes. */
2916 vtnet_rx_filter(sc);
2917 /* Restore filtered MAC addresses. */
2918 vtnet_rx_filter_mac(sc);
2921 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2922 vtnet_rx_filter_vlan(sc);
2926 vtnet_init_rx_queues(struct vtnet_softc *sc)
2929 struct vtnet_rxq *rxq;
2930 int i, clsize, error;
2932 dev = sc->vtnet_dev;
2935 * Use the new cluster size if one has been set (via a MTU
2936 * change). Otherwise, use the standard 2K clusters.
2938 * BMV: It might make sense to use page sized clusters as
2939 * the default (depending on the features negotiated).
2941 if (sc->vtnet_rx_new_clsize != 0) {
2942 clsize = sc->vtnet_rx_new_clsize;
2943 sc->vtnet_rx_new_clsize = 0;
2947 sc->vtnet_rx_clsize = clsize;
2948 sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize);
2950 KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS ||
2951 sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs,
2952 ("%s: too many rx mbufs %d for %d segments", __func__,
2953 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs));
2955 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2956 rxq = &sc->vtnet_rxqs[i];
2958 /* Hold the lock to satisfy asserts. */
2959 VTNET_RXQ_LOCK(rxq);
2960 error = vtnet_rxq_populate(rxq);
2961 VTNET_RXQ_UNLOCK(rxq);
2965 "cannot allocate mbufs for Rx queue %d\n", i);
2974 vtnet_init_tx_queues(struct vtnet_softc *sc)
2976 struct vtnet_txq *txq;
2979 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2980 txq = &sc->vtnet_txqs[i];
2981 txq->vtntx_watchdog = 0;
2988 vtnet_init_rxtx_queues(struct vtnet_softc *sc)
2992 error = vtnet_init_rx_queues(sc);
2996 error = vtnet_init_tx_queues(sc);
3004 vtnet_set_active_vq_pairs(struct vtnet_softc *sc)
3009 dev = sc->vtnet_dev;
3011 if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) {
3012 sc->vtnet_act_vq_pairs = 1;
3016 npairs = sc->vtnet_requested_vq_pairs;
3018 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) {
3020 "cannot set active queue pairs to %d\n", npairs);
3024 sc->vtnet_act_vq_pairs = npairs;
3028 vtnet_reinit(struct vtnet_softc *sc)
3033 ifp = sc->vtnet_ifp;
3035 /* Use the current MAC address. */
3036 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
3037 vtnet_set_hwaddr(sc);
3039 vtnet_set_active_vq_pairs(sc);
3041 ifp->if_hwassist = 0;
3042 if (ifp->if_capenable & IFCAP_TXCSUM)
3043 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
3044 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3045 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6;
3046 if (ifp->if_capenable & IFCAP_TSO4)
3047 ifp->if_hwassist |= CSUM_IP_TSO;
3048 if (ifp->if_capenable & IFCAP_TSO6)
3049 ifp->if_hwassist |= CSUM_IP6_TSO;
3051 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
3052 vtnet_init_rx_filters(sc);
3054 error = vtnet_init_rxtx_queues(sc);
3058 vtnet_enable_interrupts(sc);
3059 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3065 vtnet_init_locked(struct vtnet_softc *sc)
3070 dev = sc->vtnet_dev;
3071 ifp = sc->vtnet_ifp;
3073 VTNET_CORE_LOCK_ASSERT(sc);
3075 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3080 /* Reinitialize with the host. */
3081 if (vtnet_virtio_reinit(sc) != 0)
3084 if (vtnet_reinit(sc) != 0)
3087 virtio_reinit_complete(dev);
3089 vtnet_update_link_status(sc);
3090 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
3099 vtnet_init(void *xsc)
3101 struct vtnet_softc *sc;
3105 VTNET_CORE_LOCK(sc);
3106 vtnet_init_locked(sc);
3107 VTNET_CORE_UNLOCK(sc);
3111 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
3113 struct virtqueue *vq;
3115 vq = sc->vtnet_ctrl_vq;
3118 * The control virtqueue is only polled and therefore it should
3121 KASSERT(virtqueue_empty(vq),
3122 ("%s: ctrl vq %p not empty", __func__, vq));
3126 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
3127 struct sglist *sg, int readable, int writable)
3129 struct virtqueue *vq;
3131 vq = sc->vtnet_ctrl_vq;
3133 VTNET_CORE_LOCK_ASSERT(sc);
3134 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
3135 ("%s: CTRL_VQ feature not negotiated", __func__));
3137 if (!virtqueue_empty(vq))
3139 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
3143 * Poll for the response, but the command is likely already
3144 * done when we return from the notify.
3146 virtqueue_notify(vq);
3147 virtqueue_poll(vq, NULL);
3151 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
3153 struct virtio_net_ctrl_hdr hdr __aligned(2);
3154 struct sglist_seg segs[3];
3159 hdr.class = VIRTIO_NET_CTRL_MAC;
3160 hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
3161 ack = VIRTIO_NET_ERR;
3163 sglist_init(&sg, 3, segs);
3165 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3166 error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN);
3167 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3168 KASSERT(error == 0 && sg.sg_nseg == 3,
3169 ("%s: error %d adding set MAC msg to sglist", __func__, error));
3171 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3173 return (ack == VIRTIO_NET_OK ? 0 : EIO);
3177 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs)
3179 struct sglist_seg segs[3];
3182 struct virtio_net_ctrl_hdr hdr;
3184 struct virtio_net_ctrl_mq mq;
3190 s.hdr.class = VIRTIO_NET_CTRL_MQ;
3191 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
3192 s.mq.virtqueue_pairs = npairs;
3193 s.ack = VIRTIO_NET_ERR;
3195 sglist_init(&sg, 3, segs);
3197 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3198 error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq));
3199 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3200 KASSERT(error == 0 && sg.sg_nseg == 3,
3201 ("%s: error %d adding MQ message to sglist", __func__, error));
3203 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3205 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3209 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
3211 struct sglist_seg segs[3];
3214 struct virtio_net_ctrl_hdr hdr;
3222 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3223 ("%s: CTRL_RX feature not negotiated", __func__));
3225 s.hdr.class = VIRTIO_NET_CTRL_RX;
3228 s.ack = VIRTIO_NET_ERR;
3230 sglist_init(&sg, 3, segs);
3232 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3233 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
3234 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3235 KASSERT(error == 0 && sg.sg_nseg == 3,
3236 ("%s: error %d adding Rx message to sglist", __func__, error));
3238 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3240 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3244 vtnet_set_promisc(struct vtnet_softc *sc, int on)
3247 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
3251 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
3254 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
3258 * The device defaults to promiscuous mode for backwards compatibility.
3259 * Turn it off at attach time if possible.
3262 vtnet_attach_disable_promisc(struct vtnet_softc *sc)
3266 ifp = sc->vtnet_ifp;
3268 VTNET_CORE_LOCK(sc);
3269 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) {
3270 ifp->if_flags |= IFF_PROMISC;
3271 } else if (vtnet_set_promisc(sc, 0) != 0) {
3272 ifp->if_flags |= IFF_PROMISC;
3273 device_printf(sc->vtnet_dev,
3274 "cannot disable default promiscuous mode\n");
3276 VTNET_CORE_UNLOCK(sc);
3280 vtnet_rx_filter(struct vtnet_softc *sc)
3285 dev = sc->vtnet_dev;
3286 ifp = sc->vtnet_ifp;
3288 VTNET_CORE_LOCK_ASSERT(sc);
3290 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
3291 device_printf(dev, "cannot %s promiscuous mode\n",
3292 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
3294 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
3295 device_printf(dev, "cannot %s all-multicast mode\n",
3296 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
3300 vtnet_rx_filter_mac(struct vtnet_softc *sc)
3302 struct virtio_net_ctrl_hdr hdr __aligned(2);
3303 struct vtnet_mac_filter *filter;
3304 struct sglist_seg segs[4];
3308 struct ifmultiaddr *ifma;
3309 int ucnt, mcnt, promisc, allmulti, error;
3312 ifp = sc->vtnet_ifp;
3313 filter = sc->vtnet_mac_filter;
3319 VTNET_CORE_LOCK_ASSERT(sc);
3320 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3321 ("%s: CTRL_RX feature not negotiated", __func__));
3323 /* Unicast MAC addresses: */
3325 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3326 if (ifa->ifa_addr->sa_family != AF_LINK)
3328 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3329 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
3331 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
3336 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3337 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
3340 if_addr_runlock(ifp);
3343 filter->vmf_unicast.nentries = 0;
3344 if_printf(ifp, "more than %d MAC addresses assigned, "
3345 "falling back to promiscuous mode\n",
3346 VTNET_MAX_MAC_ENTRIES);
3348 filter->vmf_unicast.nentries = ucnt;
3350 /* Multicast MAC addresses: */
3351 if_maddr_rlock(ifp);
3352 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3353 if (ifma->ifma_addr->sa_family != AF_LINK)
3355 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
3360 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3361 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
3364 if_maddr_runlock(ifp);
3366 if (allmulti != 0) {
3367 filter->vmf_multicast.nentries = 0;
3368 if_printf(ifp, "more than %d multicast MAC addresses "
3369 "assigned, falling back to all-multicast mode\n",
3370 VTNET_MAX_MAC_ENTRIES);
3372 filter->vmf_multicast.nentries = mcnt;
3374 if (promisc != 0 && allmulti != 0)
3377 hdr.class = VIRTIO_NET_CTRL_MAC;
3378 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
3379 ack = VIRTIO_NET_ERR;
3381 sglist_init(&sg, 4, segs);
3383 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3384 error |= sglist_append(&sg, &filter->vmf_unicast,
3385 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
3386 error |= sglist_append(&sg, &filter->vmf_multicast,
3387 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
3388 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3389 KASSERT(error == 0 && sg.sg_nseg == 4,
3390 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
3392 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3394 if (ack != VIRTIO_NET_OK)
3395 if_printf(ifp, "error setting host MAC filter table\n");
3398 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
3399 if_printf(ifp, "cannot enable promiscuous mode\n");
3400 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
3401 if_printf(ifp, "cannot enable all-multicast mode\n");
3405 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3407 struct sglist_seg segs[3];
3410 struct virtio_net_ctrl_hdr hdr;
3418 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
3419 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
3421 s.ack = VIRTIO_NET_ERR;
3423 sglist_init(&sg, 3, segs);
3425 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3426 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
3427 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3428 KASSERT(error == 0 && sg.sg_nseg == 3,
3429 ("%s: error %d adding VLAN message to sglist", __func__, error));
3431 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3433 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3437 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
3443 VTNET_CORE_LOCK_ASSERT(sc);
3444 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
3445 ("%s: VLAN_FILTER feature not negotiated", __func__));
3447 /* Enable the filter for each configured VLAN. */
3448 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) {
3449 w = sc->vtnet_vlan_filter[i];
3451 while ((bit = ffs(w) - 1) != -1) {
3453 tag = sizeof(w) * CHAR_BIT * i + bit;
3455 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
3456 device_printf(sc->vtnet_dev,
3457 "cannot enable VLAN %d filter\n", tag);
3464 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3469 ifp = sc->vtnet_ifp;
3470 idx = (tag >> 5) & 0x7F;
3473 if (tag == 0 || tag > 4095)
3476 VTNET_CORE_LOCK(sc);
3479 sc->vtnet_vlan_filter[idx] |= (1 << bit);
3481 sc->vtnet_vlan_filter[idx] &= ~(1 << bit);
3483 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
3484 ifp->if_drv_flags & IFF_DRV_RUNNING &&
3485 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
3486 device_printf(sc->vtnet_dev,
3487 "cannot %s VLAN %d %s the host filter table\n",
3488 add ? "add" : "remove", tag, add ? "to" : "from");
3491 VTNET_CORE_UNLOCK(sc);
3495 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3498 if (ifp->if_softc != arg)
3501 vtnet_update_vlan_filter(arg, 1, tag);
3505 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3508 if (ifp->if_softc != arg)
3511 vtnet_update_vlan_filter(arg, 0, tag);
3515 vtnet_is_link_up(struct vtnet_softc *sc)
3521 dev = sc->vtnet_dev;
3522 ifp = sc->vtnet_ifp;
3524 if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0)
3525 status = VIRTIO_NET_S_LINK_UP;
3527 status = virtio_read_dev_config_2(dev,
3528 offsetof(struct virtio_net_config, status));
3530 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
3534 vtnet_update_link_status(struct vtnet_softc *sc)
3539 ifp = sc->vtnet_ifp;
3541 VTNET_CORE_LOCK_ASSERT(sc);
3542 link = vtnet_is_link_up(sc);
3544 /* Notify if the link status has changed. */
3545 if (link != 0 && sc->vtnet_link_active == 0) {
3546 sc->vtnet_link_active = 1;
3547 if_link_state_change(ifp, LINK_STATE_UP);
3548 } else if (link == 0 && sc->vtnet_link_active != 0) {
3549 sc->vtnet_link_active = 0;
3550 if_link_state_change(ifp, LINK_STATE_DOWN);
3555 vtnet_ifmedia_upd(struct ifnet *ifp)
3557 struct vtnet_softc *sc;
3558 struct ifmedia *ifm;
3561 ifm = &sc->vtnet_media;
3563 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3570 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3572 struct vtnet_softc *sc;
3576 ifmr->ifm_status = IFM_AVALID;
3577 ifmr->ifm_active = IFM_ETHER;
3579 VTNET_CORE_LOCK(sc);
3580 if (vtnet_is_link_up(sc) != 0) {
3581 ifmr->ifm_status |= IFM_ACTIVE;
3582 ifmr->ifm_active |= VTNET_MEDIATYPE;
3584 ifmr->ifm_active |= IFM_NONE;
3585 VTNET_CORE_UNLOCK(sc);
3589 vtnet_set_hwaddr(struct vtnet_softc *sc)
3594 dev = sc->vtnet_dev;
3596 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) {
3597 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
3598 device_printf(dev, "unable to set MAC address\n");
3599 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
3600 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3601 virtio_write_dev_config_1(dev,
3602 offsetof(struct virtio_net_config, mac) + i,
3603 sc->vtnet_hwaddr[i]);
3609 vtnet_get_hwaddr(struct vtnet_softc *sc)
3614 dev = sc->vtnet_dev;
3616 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
3618 * Generate a random locally administered unicast address.
3620 * It would be nice to generate the same MAC address across
3621 * reboots, but it seems all the hosts currently available
3622 * support the MAC feature, so this isn't too important.
3624 sc->vtnet_hwaddr[0] = 0xB2;
3625 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
3626 vtnet_set_hwaddr(sc);
3630 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3631 sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev,
3632 offsetof(struct virtio_net_config, mac) + i);
3637 vtnet_vlan_tag_remove(struct mbuf *m)
3639 struct ether_vlan_header *evh;
3641 evh = mtod(m, struct ether_vlan_header *);
3642 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
3643 m->m_flags |= M_VLANTAG;
3645 /* Strip the 802.1Q header. */
3646 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
3647 ETHER_HDR_LEN - ETHER_TYPE_LEN);
3648 m_adj(m, ETHER_VLAN_ENCAP_LEN);
3652 vtnet_set_rx_process_limit(struct vtnet_softc *sc)
3656 limit = vtnet_tunable_int(sc, "rx_process_limit",
3657 vtnet_rx_process_limit);
3660 sc->vtnet_rx_process_limit = limit;
3664 vtnet_set_tx_intr_threshold(struct vtnet_softc *sc)
3668 size = virtqueue_size(sc->vtnet_txqs[0].vtntx_vq);
3671 * The Tx interrupt is disabled until the queue free count falls
3672 * below our threshold. Completed frames are drained from the Tx
3673 * virtqueue before transmitting new frames and in the watchdog
3674 * callout, so the frequency of Tx interrupts is greatly reduced,
3675 * at the cost of not freeing mbufs as quickly as they otherwise
3678 * N.B. We assume all the Tx queues are the same size.
3683 * Without indirect descriptors, leave enough room for the most
3684 * segments we handle.
3686 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 &&
3687 thresh < sc->vtnet_tx_nsegs)
3688 thresh = sc->vtnet_tx_nsegs;
3690 sc->vtnet_tx_intr_thresh = thresh;
3694 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx,
3695 struct sysctl_oid_list *child, struct vtnet_rxq *rxq)
3697 struct sysctl_oid *node;
3698 struct sysctl_oid_list *list;
3699 struct vtnet_rxq_stats *stats;
3702 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id);
3703 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3704 CTLFLAG_RD, NULL, "Receive Queue");
3705 list = SYSCTL_CHILDREN(node);
3707 stats = &rxq->vtnrx_stats;
3709 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD,
3710 &stats->vrxs_ipackets, "Receive packets");
3711 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD,
3712 &stats->vrxs_ibytes, "Receive bytes");
3713 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD,
3714 &stats->vrxs_iqdrops, "Receive drops");
3715 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD,
3716 &stats->vrxs_ierrors, "Receive errors");
3717 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3718 &stats->vrxs_csum, "Receive checksum offloaded");
3719 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD,
3720 &stats->vrxs_csum_failed, "Receive checksum offload failed");
3721 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3722 &stats->vrxs_rescheduled,
3723 "Receive interrupt handler rescheduled");
3727 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx,
3728 struct sysctl_oid_list *child, struct vtnet_txq *txq)
3730 struct sysctl_oid *node;
3731 struct sysctl_oid_list *list;
3732 struct vtnet_txq_stats *stats;
3735 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id);
3736 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3737 CTLFLAG_RD, NULL, "Transmit Queue");
3738 list = SYSCTL_CHILDREN(node);
3740 stats = &txq->vtntx_stats;
3742 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD,
3743 &stats->vtxs_opackets, "Transmit packets");
3744 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD,
3745 &stats->vtxs_obytes, "Transmit bytes");
3746 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD,
3747 &stats->vtxs_omcasts, "Transmit multicasts");
3748 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3749 &stats->vtxs_csum, "Transmit checksum offloaded");
3750 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD,
3751 &stats->vtxs_tso, "Transmit segmentation offloaded");
3752 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3753 &stats->vtxs_rescheduled,
3754 "Transmit interrupt handler rescheduled");
3758 vtnet_setup_queue_sysctl(struct vtnet_softc *sc)
3761 struct sysctl_ctx_list *ctx;
3762 struct sysctl_oid *tree;
3763 struct sysctl_oid_list *child;
3766 dev = sc->vtnet_dev;
3767 ctx = device_get_sysctl_ctx(dev);
3768 tree = device_get_sysctl_tree(dev);
3769 child = SYSCTL_CHILDREN(tree);
3771 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3772 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]);
3773 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]);
3778 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx,
3779 struct sysctl_oid_list *child, struct vtnet_softc *sc)
3781 struct vtnet_statistics *stats;
3782 struct vtnet_rxq_stats rxaccum;
3783 struct vtnet_txq_stats txaccum;
3785 vtnet_accum_stats(sc, &rxaccum, &txaccum);
3787 stats = &sc->vtnet_stats;
3788 stats->rx_csum_offloaded = rxaccum.vrxs_csum;
3789 stats->rx_csum_failed = rxaccum.vrxs_csum_failed;
3790 stats->rx_task_rescheduled = rxaccum.vrxs_rescheduled;
3791 stats->tx_csum_offloaded = txaccum.vtxs_csum;
3792 stats->tx_tso_offloaded = txaccum.vtxs_tso;
3793 stats->tx_task_rescheduled = txaccum.vtxs_rescheduled;
3795 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
3796 CTLFLAG_RD, &stats->mbuf_alloc_failed,
3797 "Mbuf cluster allocation failures");
3799 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
3800 CTLFLAG_RD, &stats->rx_frame_too_large,
3801 "Received frame larger than the mbuf chain");
3802 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
3803 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
3804 "Enqueuing the replacement receive mbuf failed");
3805 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
3806 CTLFLAG_RD, &stats->rx_mergeable_failed,
3807 "Mergeable buffers receive failures");
3808 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
3809 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
3810 "Received checksum offloaded buffer with unsupported "
3812 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
3813 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
3814 "Received checksum offloaded buffer with incorrect IP protocol");
3815 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
3816 CTLFLAG_RD, &stats->rx_csum_bad_offset,
3817 "Received checksum offloaded buffer with incorrect offset");
3818 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto",
3819 CTLFLAG_RD, &stats->rx_csum_bad_proto,
3820 "Received checksum offloaded buffer with incorrect protocol");
3821 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
3822 CTLFLAG_RD, &stats->rx_csum_failed,
3823 "Received buffer checksum offload failed");
3824 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
3825 CTLFLAG_RD, &stats->rx_csum_offloaded,
3826 "Received buffer checksum offload succeeded");
3827 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
3828 CTLFLAG_RD, &stats->rx_task_rescheduled,
3829 "Times the receive interrupt task rescheduled itself");
3831 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
3832 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
3833 "Aborted transmit of checksum offloaded buffer with unknown "
3835 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
3836 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
3837 "Aborted transmit of TSO buffer with unknown Ethernet type");
3838 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp",
3839 CTLFLAG_RD, &stats->tx_tso_not_tcp,
3840 "Aborted transmit of TSO buffer with non TCP protocol");
3841 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
3842 CTLFLAG_RD, &stats->tx_defragged,
3843 "Transmit mbufs defragged");
3844 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
3845 CTLFLAG_RD, &stats->tx_defrag_failed,
3846 "Aborted transmit of buffer because defrag failed");
3847 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
3848 CTLFLAG_RD, &stats->tx_csum_offloaded,
3849 "Offloaded checksum of transmitted buffer");
3850 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
3851 CTLFLAG_RD, &stats->tx_tso_offloaded,
3852 "Segmentation offload of transmitted buffer");
3853 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
3854 CTLFLAG_RD, &stats->tx_task_rescheduled,
3855 "Times the transmit interrupt task rescheduled itself");
3859 vtnet_setup_sysctl(struct vtnet_softc *sc)
3862 struct sysctl_ctx_list *ctx;
3863 struct sysctl_oid *tree;
3864 struct sysctl_oid_list *child;
3866 dev = sc->vtnet_dev;
3867 ctx = device_get_sysctl_ctx(dev);
3868 tree = device_get_sysctl_tree(dev);
3869 child = SYSCTL_CHILDREN(tree);
3871 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs",
3872 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0,
3873 "Maximum number of supported virtqueue pairs");
3874 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "requested_vq_pairs",
3875 CTLFLAG_RD, &sc->vtnet_requested_vq_pairs, 0,
3876 "Requested number of virtqueue pairs");
3877 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs",
3878 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0,
3879 "Number of active virtqueue pairs");
3881 vtnet_setup_stat_sysctl(ctx, child, sc);
3885 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq)
3888 return (virtqueue_enable_intr(rxq->vtnrx_vq));
3892 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq)
3895 virtqueue_disable_intr(rxq->vtnrx_vq);
3899 vtnet_txq_enable_intr(struct vtnet_txq *txq)
3901 struct virtqueue *vq;
3905 if (vtnet_txq_below_threshold(txq) != 0)
3906 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG));
3909 * The free count is above our threshold. Keep the Tx interrupt
3910 * disabled until the queue is fuller.
3916 vtnet_txq_disable_intr(struct vtnet_txq *txq)
3919 virtqueue_disable_intr(txq->vtntx_vq);
3923 vtnet_enable_rx_interrupts(struct vtnet_softc *sc)
3927 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3928 vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]);
3932 vtnet_enable_tx_interrupts(struct vtnet_softc *sc)
3936 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3937 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]);
3941 vtnet_enable_interrupts(struct vtnet_softc *sc)
3944 vtnet_enable_rx_interrupts(sc);
3945 vtnet_enable_tx_interrupts(sc);
3949 vtnet_disable_rx_interrupts(struct vtnet_softc *sc)
3953 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3954 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]);
3958 vtnet_disable_tx_interrupts(struct vtnet_softc *sc)
3962 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3963 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]);
3967 vtnet_disable_interrupts(struct vtnet_softc *sc)
3970 vtnet_disable_rx_interrupts(sc);
3971 vtnet_disable_tx_interrupts(sc);
3975 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def)
3979 snprintf(path, sizeof(path),
3980 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob);
3981 TUNABLE_INT_FETCH(path, &def);
3988 vtnet_netdump_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize)
3990 struct vtnet_softc *sc;
3992 sc = if_getsoftc(ifp);
3994 VTNET_CORE_LOCK(sc);
3995 *nrxr = sc->vtnet_max_vq_pairs;
3996 *ncl = NETDUMP_MAX_IN_FLIGHT;
3997 *clsize = sc->vtnet_rx_clsize;
3998 VTNET_CORE_UNLOCK(sc);
4001 * We need to allocate from this zone in the transmit path, so ensure
4002 * that we have at least one item per header available.
4003 * XXX add a separate zone like we do for mbufs? otherwise we may alloc
4006 uma_zone_reserve(vtnet_tx_header_zone, NETDUMP_MAX_IN_FLIGHT * 2);
4007 uma_prealloc(vtnet_tx_header_zone, NETDUMP_MAX_IN_FLIGHT * 2);
4011 vtnet_netdump_event(struct ifnet *ifp __unused, enum netdump_ev event __unused)
4016 vtnet_netdump_transmit(struct ifnet *ifp, struct mbuf *m)
4018 struct vtnet_softc *sc;
4019 struct vtnet_txq *txq;
4022 sc = if_getsoftc(ifp);
4023 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4027 txq = &sc->vtnet_txqs[0];
4028 error = vtnet_txq_encap(txq, &m, M_NOWAIT | M_USE_RESERVE);
4030 (void)vtnet_txq_notify(txq);
4035 vtnet_netdump_poll(struct ifnet *ifp, int count)
4037 struct vtnet_softc *sc;
4040 sc = if_getsoftc(ifp);
4041 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4045 (void)vtnet_txq_eof(&sc->vtnet_txqs[0]);
4046 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
4047 (void)vtnet_rxq_eof(&sc->vtnet_rxqs[i]);
4050 #endif /* NETDUMP */