2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /* Driver for VirtIO network devices. */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41 #include <sys/random.h>
42 #include <sys/sglist.h>
44 #include <sys/mutex.h>
45 #include <sys/taskqueue.h>
47 #include <machine/smp.h>
51 #include <net/ethernet.h>
53 #include <net/if_arp.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/if_media.h>
57 #include <net/if_vlan_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip6.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/tcp.h>
68 #include <netinet/sctp.h>
70 #include <machine/bus.h>
71 #include <machine/resource.h>
75 #include <dev/virtio/virtio.h>
76 #include <dev/virtio/virtqueue.h>
77 #include <dev/virtio/network/virtio_net.h>
78 #include <dev/virtio/network/if_vtnetvar.h>
80 #include "virtio_if.h"
83 #include "opt_inet6.h"
85 static int vtnet_modevent(module_t, int, void *);
87 static int vtnet_probe(device_t);
88 static int vtnet_attach(device_t);
89 static int vtnet_detach(device_t);
90 static int vtnet_suspend(device_t);
91 static int vtnet_resume(device_t);
92 static int vtnet_shutdown(device_t);
93 static int vtnet_attach_completed(device_t);
94 static int vtnet_config_change(device_t);
96 static void vtnet_negotiate_features(struct vtnet_softc *);
97 static void vtnet_setup_features(struct vtnet_softc *);
98 static int vtnet_init_rxq(struct vtnet_softc *, int);
99 static int vtnet_init_txq(struct vtnet_softc *, int);
100 static int vtnet_alloc_rxtx_queues(struct vtnet_softc *);
101 static void vtnet_free_rxtx_queues(struct vtnet_softc *);
102 static int vtnet_alloc_rx_filters(struct vtnet_softc *);
103 static void vtnet_free_rx_filters(struct vtnet_softc *);
104 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
105 static int vtnet_setup_interface(struct vtnet_softc *);
106 static int vtnet_change_mtu(struct vtnet_softc *, int);
107 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t);
109 static int vtnet_rxq_populate(struct vtnet_rxq *);
110 static void vtnet_rxq_free_mbufs(struct vtnet_rxq *);
112 vtnet_rx_alloc_buf(struct vtnet_softc *, int , struct mbuf **);
113 static int vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *,
115 static int vtnet_rxq_replace_buf(struct vtnet_rxq *, struct mbuf *, int);
116 static int vtnet_rxq_enqueue_buf(struct vtnet_rxq *, struct mbuf *);
117 static int vtnet_rxq_new_buf(struct vtnet_rxq *);
118 static int vtnet_rxq_csum(struct vtnet_rxq *, struct mbuf *,
119 struct virtio_net_hdr *);
120 static void vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *, int);
121 static void vtnet_rxq_discard_buf(struct vtnet_rxq *, struct mbuf *);
122 static int vtnet_rxq_merged_eof(struct vtnet_rxq *, struct mbuf *, int);
123 static void vtnet_rxq_input(struct vtnet_rxq *, struct mbuf *,
124 struct virtio_net_hdr *);
125 static int vtnet_rxq_eof(struct vtnet_rxq *);
126 static void vtnet_rx_vq_intr(void *);
127 static void vtnet_rxq_tq_intr(void *, int);
129 static int vtnet_txq_below_threshold(struct vtnet_txq *);
130 static int vtnet_txq_notify(struct vtnet_txq *);
131 static void vtnet_txq_free_mbufs(struct vtnet_txq *);
132 static int vtnet_txq_offload_ctx(struct vtnet_txq *, struct mbuf *,
133 int *, int *, int *);
134 static int vtnet_txq_offload_tso(struct vtnet_txq *, struct mbuf *, int,
135 int, struct virtio_net_hdr *);
137 vtnet_txq_offload(struct vtnet_txq *, struct mbuf *,
138 struct virtio_net_hdr *);
139 static int vtnet_txq_enqueue_buf(struct vtnet_txq *, struct mbuf **,
140 struct vtnet_tx_header *);
141 static int vtnet_txq_encap(struct vtnet_txq *, struct mbuf **);
142 #ifdef VTNET_LEGACY_TX
143 static void vtnet_start_locked(struct vtnet_txq *, struct ifnet *);
144 static void vtnet_start(struct ifnet *);
146 static int vtnet_txq_mq_start_locked(struct vtnet_txq *, struct mbuf *);
147 static int vtnet_txq_mq_start(struct ifnet *, struct mbuf *);
148 static void vtnet_txq_tq_deferred(void *, int);
150 static void vtnet_txq_start(struct vtnet_txq *);
151 static void vtnet_txq_tq_intr(void *, int);
152 static int vtnet_txq_eof(struct vtnet_txq *);
153 static void vtnet_tx_vq_intr(void *);
154 static void vtnet_tx_start_all(struct vtnet_softc *);
156 #ifndef VTNET_LEGACY_TX
157 static void vtnet_qflush(struct ifnet *);
160 static int vtnet_watchdog(struct vtnet_txq *);
161 static void vtnet_rxq_accum_stats(struct vtnet_rxq *,
162 struct vtnet_rxq_stats *);
163 static void vtnet_txq_accum_stats(struct vtnet_txq *,
164 struct vtnet_txq_stats *);
165 static void vtnet_accumulate_stats(struct vtnet_softc *);
166 static void vtnet_tick(void *);
168 static void vtnet_start_taskqueues(struct vtnet_softc *);
169 static void vtnet_free_taskqueues(struct vtnet_softc *);
170 static void vtnet_drain_taskqueues(struct vtnet_softc *);
172 static void vtnet_drain_rxtx_queues(struct vtnet_softc *);
173 static void vtnet_stop_rendezvous(struct vtnet_softc *);
174 static void vtnet_stop(struct vtnet_softc *);
175 static int vtnet_virtio_reinit(struct vtnet_softc *);
176 static void vtnet_init_rx_filters(struct vtnet_softc *);
177 static int vtnet_init_rx_queues(struct vtnet_softc *);
178 static int vtnet_init_tx_queues(struct vtnet_softc *);
179 static int vtnet_init_rxtx_queues(struct vtnet_softc *);
180 static void vtnet_set_active_vq_pairs(struct vtnet_softc *);
181 static int vtnet_reinit(struct vtnet_softc *);
182 static void vtnet_init_locked(struct vtnet_softc *);
183 static void vtnet_init(void *);
185 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
186 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
187 struct sglist *, int, int);
188 static int vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
189 static int vtnet_ctrl_mq_cmd(struct vtnet_softc *, uint16_t);
190 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
191 static int vtnet_set_promisc(struct vtnet_softc *, int);
192 static int vtnet_set_allmulti(struct vtnet_softc *, int);
193 static void vtnet_attach_disable_promisc(struct vtnet_softc *);
194 static void vtnet_rx_filter(struct vtnet_softc *);
195 static void vtnet_rx_filter_mac(struct vtnet_softc *);
196 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
197 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
198 static void vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t);
199 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
200 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
202 static int vtnet_is_link_up(struct vtnet_softc *);
203 static void vtnet_update_link_status(struct vtnet_softc *);
204 static int vtnet_ifmedia_upd(struct ifnet *);
205 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
206 static void vtnet_get_hwaddr(struct vtnet_softc *);
207 static void vtnet_set_hwaddr(struct vtnet_softc *);
208 static void vtnet_vlan_tag_remove(struct mbuf *);
209 static void vtnet_set_rx_process_limit(struct vtnet_softc *);
210 static void vtnet_set_tx_intr_threshold(struct vtnet_softc *);
212 static void vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *,
213 struct sysctl_oid_list *, struct vtnet_rxq *);
214 static void vtnet_setup_txq_sysctl(struct sysctl_ctx_list *,
215 struct sysctl_oid_list *, struct vtnet_txq *);
216 static void vtnet_setup_queue_sysctl(struct vtnet_softc *);
217 static void vtnet_setup_sysctl(struct vtnet_softc *);
219 static int vtnet_rxq_enable_intr(struct vtnet_rxq *);
220 static void vtnet_rxq_disable_intr(struct vtnet_rxq *);
221 static int vtnet_txq_enable_intr(struct vtnet_txq *);
222 static void vtnet_txq_disable_intr(struct vtnet_txq *);
223 static void vtnet_enable_rx_interrupts(struct vtnet_softc *);
224 static void vtnet_enable_tx_interrupts(struct vtnet_softc *);
225 static void vtnet_enable_interrupts(struct vtnet_softc *);
226 static void vtnet_disable_rx_interrupts(struct vtnet_softc *);
227 static void vtnet_disable_tx_interrupts(struct vtnet_softc *);
228 static void vtnet_disable_interrupts(struct vtnet_softc *);
230 static int vtnet_tunable_int(struct vtnet_softc *, const char *, int);
233 static int vtnet_csum_disable = 0;
234 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
235 static int vtnet_tso_disable = 0;
236 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
237 static int vtnet_lro_disable = 0;
238 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
239 static int vtnet_mq_disable = 0;
240 TUNABLE_INT("hw.vtnet.mq_disable", &vtnet_mq_disable);
241 static int vtnet_mq_max_pairs = 0;
242 TUNABLE_INT("hw.vtnet.mq_max_pairs", &vtnet_mq_max_pairs);
243 static int vtnet_rx_process_limit = 512;
244 TUNABLE_INT("hw.vtnet.rx_process_limit", &vtnet_rx_process_limit);
246 static uma_zone_t vtnet_tx_header_zone;
248 static struct virtio_feature_desc vtnet_feature_desc[] = {
249 { VIRTIO_NET_F_CSUM, "TxChecksum" },
250 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
251 { VIRTIO_NET_F_MAC, "MacAddress" },
252 { VIRTIO_NET_F_GSO, "TxAllGSO" },
253 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
254 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
255 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
256 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
257 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
258 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
259 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
260 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
261 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
262 { VIRTIO_NET_F_STATUS, "Status" },
263 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
264 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
265 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
266 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
267 { VIRTIO_NET_F_GUEST_ANNOUNCE, "GuestAnnounce" },
268 { VIRTIO_NET_F_MQ, "Multiqueue" },
269 { VIRTIO_NET_F_CTRL_MAC_ADDR, "SetMacAddress" },
274 static device_method_t vtnet_methods[] = {
275 /* Device methods. */
276 DEVMETHOD(device_probe, vtnet_probe),
277 DEVMETHOD(device_attach, vtnet_attach),
278 DEVMETHOD(device_detach, vtnet_detach),
279 DEVMETHOD(device_suspend, vtnet_suspend),
280 DEVMETHOD(device_resume, vtnet_resume),
281 DEVMETHOD(device_shutdown, vtnet_shutdown),
283 /* VirtIO methods. */
284 DEVMETHOD(virtio_attach_completed, vtnet_attach_completed),
285 DEVMETHOD(virtio_config_change, vtnet_config_change),
291 #include <dev/netmap/if_vtnet_netmap.h>
292 #endif /* DEV_NETMAP */
294 static driver_t vtnet_driver = {
297 sizeof(struct vtnet_softc)
299 static devclass_t vtnet_devclass;
301 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
303 MODULE_VERSION(vtnet, 1);
304 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
307 vtnet_modevent(module_t mod, int type, void *unused)
315 vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr",
316 sizeof(struct vtnet_tx_header),
317 NULL, NULL, NULL, NULL, 0, 0);
321 if (uma_zone_get_cur(vtnet_tx_header_zone) > 0)
323 else if (type == MOD_UNLOAD) {
324 uma_zdestroy(vtnet_tx_header_zone);
325 vtnet_tx_header_zone = NULL;
339 vtnet_probe(device_t dev)
342 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
345 device_set_desc(dev, "VirtIO Networking Adapter");
347 return (BUS_PROBE_DEFAULT);
351 vtnet_attach(device_t dev)
353 struct vtnet_softc *sc;
356 sc = device_get_softc(dev);
359 /* Register our feature descriptions. */
360 virtio_set_feature_desc(dev, vtnet_feature_desc);
362 VTNET_CORE_LOCK_INIT(sc);
363 callout_init_mtx(&sc->vtnet_tick_ch, VTNET_CORE_MTX(sc), 0);
365 vtnet_setup_sysctl(sc);
366 vtnet_setup_features(sc);
368 error = vtnet_alloc_rx_filters(sc);
370 device_printf(dev, "cannot allocate Rx filters\n");
374 error = vtnet_alloc_rxtx_queues(sc);
376 device_printf(dev, "cannot allocate queues\n");
380 error = vtnet_alloc_virtqueues(sc);
382 device_printf(dev, "cannot allocate virtqueues\n");
386 error = vtnet_setup_interface(sc);
388 device_printf(dev, "cannot setup interface\n");
392 error = virtio_setup_intr(dev, INTR_TYPE_NET);
394 device_printf(dev, "cannot setup virtqueue interrupts\n");
395 /* BMV: This will crash if during boot! */
396 ether_ifdetach(sc->vtnet_ifp);
401 vtnet_netmap_attach(sc);
402 #endif /* DEV_NETMAP */
404 vtnet_start_taskqueues(sc);
414 vtnet_detach(device_t dev)
416 struct vtnet_softc *sc;
419 sc = device_get_softc(dev);
422 if (device_is_attached(dev)) {
425 VTNET_CORE_UNLOCK(sc);
427 callout_drain(&sc->vtnet_tick_ch);
428 vtnet_drain_taskqueues(sc);
435 #endif /* DEV_NETMAP */
437 vtnet_free_taskqueues(sc);
439 if (sc->vtnet_vlan_attach != NULL) {
440 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
441 sc->vtnet_vlan_attach = NULL;
443 if (sc->vtnet_vlan_detach != NULL) {
444 EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach);
445 sc->vtnet_vlan_detach = NULL;
448 ifmedia_removeall(&sc->vtnet_media);
452 sc->vtnet_ifp = NULL;
455 vtnet_free_rxtx_queues(sc);
456 vtnet_free_rx_filters(sc);
458 if (sc->vtnet_ctrl_vq != NULL)
459 vtnet_free_ctrl_vq(sc);
461 VTNET_CORE_LOCK_DESTROY(sc);
467 vtnet_suspend(device_t dev)
469 struct vtnet_softc *sc;
471 sc = device_get_softc(dev);
475 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
476 VTNET_CORE_UNLOCK(sc);
482 vtnet_resume(device_t dev)
484 struct vtnet_softc *sc;
487 sc = device_get_softc(dev);
491 if (ifp->if_flags & IFF_UP)
492 vtnet_init_locked(sc);
493 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
494 VTNET_CORE_UNLOCK(sc);
500 vtnet_shutdown(device_t dev)
504 * Suspend already does all of what we need to
505 * do here; we just never expect to be resumed.
507 return (vtnet_suspend(dev));
511 vtnet_attach_completed(device_t dev)
514 vtnet_attach_disable_promisc(device_get_softc(dev));
520 vtnet_config_change(device_t dev)
522 struct vtnet_softc *sc;
524 sc = device_get_softc(dev);
527 vtnet_update_link_status(sc);
528 if (sc->vtnet_link_active != 0)
529 vtnet_tx_start_all(sc);
530 VTNET_CORE_UNLOCK(sc);
536 vtnet_negotiate_features(struct vtnet_softc *sc)
539 uint64_t mask, features;
545 * TSO and LRO are only available when their corresponding checksum
546 * offload feature is also negotiated.
548 if (vtnet_tunable_int(sc, "csum_disable", vtnet_csum_disable)) {
549 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
550 mask |= VTNET_TSO_FEATURES | VTNET_LRO_FEATURES;
552 if (vtnet_tunable_int(sc, "tso_disable", vtnet_tso_disable))
553 mask |= VTNET_TSO_FEATURES;
554 if (vtnet_tunable_int(sc, "lro_disable", vtnet_lro_disable))
555 mask |= VTNET_LRO_FEATURES;
556 #ifndef VTNET_LEGACY_TX
557 if (vtnet_tunable_int(sc, "mq_disable", vtnet_mq_disable))
558 mask |= VIRTIO_NET_F_MQ;
560 mask |= VIRTIO_NET_F_MQ;
563 features = VTNET_FEATURES & ~mask;
564 sc->vtnet_features = virtio_negotiate_features(dev, features);
566 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
567 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
569 * LRO without mergeable buffers requires special care. This
570 * is not ideal because every receive buffer must be large
571 * enough to hold the maximum TCP packet, the Ethernet header,
572 * and the header. This requires up to 34 descriptors with
573 * MCLBYTES clusters. If we do not have indirect descriptors,
574 * LRO is disabled since the virtqueue will not contain very
575 * many receive buffers.
577 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
579 "LRO disabled due to both mergeable buffers and "
580 "indirect descriptors not negotiated\n");
582 features &= ~VTNET_LRO_FEATURES;
584 virtio_negotiate_features(dev, features);
586 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
591 vtnet_setup_features(struct vtnet_softc *sc)
598 vtnet_negotiate_features(sc);
600 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
601 sc->vtnet_flags |= VTNET_FLAG_INDIRECT;
602 if (virtio_with_feature(dev, VIRTIO_RING_F_EVENT_IDX))
603 sc->vtnet_flags |= VTNET_FLAG_EVENT_IDX;
605 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
606 /* This feature should always be negotiated. */
607 sc->vtnet_flags |= VTNET_FLAG_MAC;
610 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
611 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
612 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
614 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
616 if (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS)
617 sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
618 else if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG)
619 sc->vtnet_rx_nsegs = VTNET_MAX_RX_SEGS;
621 sc->vtnet_rx_nsegs = VTNET_MIN_RX_SEGS;
623 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO) ||
624 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
625 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
626 sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
628 sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
630 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
631 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
633 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
634 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
635 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
636 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
637 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR))
638 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
641 if (virtio_with_feature(dev, VIRTIO_NET_F_MQ) &&
642 sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
643 max_pairs = virtio_read_dev_config_2(dev,
644 offsetof(struct virtio_net_config, max_virtqueue_pairs));
645 if (max_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
646 max_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
653 * Limit the maximum number of queue pairs to the number of
654 * CPUs or the configured maximum. The actual number of
655 * queues that get used may be less.
657 max = vtnet_tunable_int(sc, "mq_max_pairs", vtnet_mq_max_pairs);
658 if (max > 0 && max_pairs > max)
660 if (max_pairs > mp_ncpus)
661 max_pairs = mp_ncpus;
662 if (max_pairs > VTNET_MAX_QUEUE_PAIRS)
663 max_pairs = VTNET_MAX_QUEUE_PAIRS;
665 sc->vtnet_flags |= VTNET_FLAG_MULTIQ;
668 sc->vtnet_max_vq_pairs = max_pairs;
672 vtnet_init_rxq(struct vtnet_softc *sc, int id)
674 struct vtnet_rxq *rxq;
676 rxq = &sc->vtnet_rxqs[id];
678 snprintf(rxq->vtnrx_name, sizeof(rxq->vtnrx_name), "%s-rx%d",
679 device_get_nameunit(sc->vtnet_dev), id);
680 mtx_init(&rxq->vtnrx_mtx, rxq->vtnrx_name, NULL, MTX_DEF);
685 rxq->vtnrx_sg = sglist_alloc(sc->vtnet_rx_nsegs, M_NOWAIT);
686 if (rxq->vtnrx_sg == NULL)
689 TASK_INIT(&rxq->vtnrx_intrtask, 0, vtnet_rxq_tq_intr, rxq);
690 rxq->vtnrx_tq = taskqueue_create(rxq->vtnrx_name, M_NOWAIT,
691 taskqueue_thread_enqueue, &rxq->vtnrx_tq);
693 return (rxq->vtnrx_tq == NULL ? ENOMEM : 0);
697 vtnet_init_txq(struct vtnet_softc *sc, int id)
699 struct vtnet_txq *txq;
701 txq = &sc->vtnet_txqs[id];
703 snprintf(txq->vtntx_name, sizeof(txq->vtntx_name), "%s-tx%d",
704 device_get_nameunit(sc->vtnet_dev), id);
705 mtx_init(&txq->vtntx_mtx, txq->vtntx_name, NULL, MTX_DEF);
710 txq->vtntx_sg = sglist_alloc(sc->vtnet_tx_nsegs, M_NOWAIT);
711 if (txq->vtntx_sg == NULL)
714 #ifndef VTNET_LEGACY_TX
715 txq->vtntx_br = buf_ring_alloc(VTNET_DEFAULT_BUFRING_SIZE, M_DEVBUF,
716 M_NOWAIT, &txq->vtntx_mtx);
717 if (txq->vtntx_br == NULL)
720 TASK_INIT(&txq->vtntx_defrtask, 0, vtnet_txq_tq_deferred, txq);
722 TASK_INIT(&txq->vtntx_intrtask, 0, vtnet_txq_tq_intr, txq);
723 txq->vtntx_tq = taskqueue_create(txq->vtntx_name, M_NOWAIT,
724 taskqueue_thread_enqueue, &txq->vtntx_tq);
725 if (txq->vtntx_tq == NULL)
732 vtnet_alloc_rxtx_queues(struct vtnet_softc *sc)
734 int i, npairs, error;
736 npairs = sc->vtnet_max_vq_pairs;
738 sc->vtnet_rxqs = malloc(sizeof(struct vtnet_rxq) * npairs, M_DEVBUF,
740 sc->vtnet_txqs = malloc(sizeof(struct vtnet_txq) * npairs, M_DEVBUF,
742 if (sc->vtnet_rxqs == NULL || sc->vtnet_txqs == NULL)
745 for (i = 0; i < npairs; i++) {
746 error = vtnet_init_rxq(sc, i);
749 error = vtnet_init_txq(sc, i);
754 vtnet_setup_queue_sysctl(sc);
760 vtnet_destroy_rxq(struct vtnet_rxq *rxq)
763 rxq->vtnrx_sc = NULL;
766 if (rxq->vtnrx_sg != NULL) {
767 sglist_free(rxq->vtnrx_sg);
768 rxq->vtnrx_sg = NULL;
771 if (mtx_initialized(&rxq->vtnrx_mtx) != 0)
772 mtx_destroy(&rxq->vtnrx_mtx);
776 vtnet_destroy_txq(struct vtnet_txq *txq)
779 txq->vtntx_sc = NULL;
782 if (txq->vtntx_sg != NULL) {
783 sglist_free(txq->vtntx_sg);
784 txq->vtntx_sg = NULL;
787 #ifndef VTNET_LEGACY_TX
788 if (txq->vtntx_br != NULL) {
789 buf_ring_free(txq->vtntx_br, M_DEVBUF);
790 txq->vtntx_br = NULL;
794 if (mtx_initialized(&txq->vtntx_mtx) != 0)
795 mtx_destroy(&txq->vtntx_mtx);
799 vtnet_free_rxtx_queues(struct vtnet_softc *sc)
803 if (sc->vtnet_rxqs != NULL) {
804 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
805 vtnet_destroy_rxq(&sc->vtnet_rxqs[i]);
806 free(sc->vtnet_rxqs, M_DEVBUF);
807 sc->vtnet_rxqs = NULL;
810 if (sc->vtnet_txqs != NULL) {
811 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
812 vtnet_destroy_txq(&sc->vtnet_txqs[i]);
813 free(sc->vtnet_txqs, M_DEVBUF);
814 sc->vtnet_txqs = NULL;
819 vtnet_alloc_rx_filters(struct vtnet_softc *sc)
822 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
823 sc->vtnet_mac_filter = malloc(sizeof(struct vtnet_mac_filter),
824 M_DEVBUF, M_NOWAIT | M_ZERO);
825 if (sc->vtnet_mac_filter == NULL)
829 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
830 sc->vtnet_vlan_filter = malloc(sizeof(uint32_t) *
831 VTNET_VLAN_FILTER_NWORDS, M_DEVBUF, M_NOWAIT | M_ZERO);
832 if (sc->vtnet_vlan_filter == NULL)
840 vtnet_free_rx_filters(struct vtnet_softc *sc)
843 if (sc->vtnet_mac_filter != NULL) {
844 free(sc->vtnet_mac_filter, M_DEVBUF);
845 sc->vtnet_mac_filter = NULL;
848 if (sc->vtnet_vlan_filter != NULL) {
849 free(sc->vtnet_vlan_filter, M_DEVBUF);
850 sc->vtnet_vlan_filter = NULL;
855 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
858 struct vq_alloc_info *info;
859 struct vtnet_rxq *rxq;
860 struct vtnet_txq *txq;
861 int i, idx, flags, nvqs, error;
866 nvqs = sc->vtnet_max_vq_pairs * 2;
867 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
870 info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
874 for (i = 0, idx = 0; i < sc->vtnet_max_vq_pairs; i++, idx+=2) {
875 rxq = &sc->vtnet_rxqs[i];
876 VQ_ALLOC_INFO_INIT(&info[idx], sc->vtnet_rx_nsegs,
877 vtnet_rx_vq_intr, rxq, &rxq->vtnrx_vq,
878 "%s-%d rx", device_get_nameunit(dev), rxq->vtnrx_id);
880 txq = &sc->vtnet_txqs[i];
881 VQ_ALLOC_INFO_INIT(&info[idx+1], sc->vtnet_tx_nsegs,
882 vtnet_tx_vq_intr, txq, &txq->vtntx_vq,
883 "%s-%d tx", device_get_nameunit(dev), txq->vtntx_id);
886 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
887 VQ_ALLOC_INFO_INIT(&info[idx], 0, NULL, NULL,
888 &sc->vtnet_ctrl_vq, "%s ctrl", device_get_nameunit(dev));
892 * Enable interrupt binding if this is multiqueue. This only matters
893 * when per-vq MSIX is available.
895 if (sc->vtnet_flags & VTNET_FLAG_MULTIQ)
898 error = virtio_alloc_virtqueues(dev, flags, nvqs, info);
905 vtnet_setup_interface(struct vtnet_softc *sc)
912 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
914 device_printf(dev, "cannot allocate ifnet structure\n");
918 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
919 if_initbaudrate(ifp, IF_Gbps(10)); /* Approx. */
921 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
922 ifp->if_init = vtnet_init;
923 ifp->if_ioctl = vtnet_ioctl;
925 #ifndef VTNET_LEGACY_TX
926 ifp->if_transmit = vtnet_txq_mq_start;
927 ifp->if_qflush = vtnet_qflush;
929 struct virtqueue *vq = sc->vtnet_txqs[0].vtntx_vq;
930 ifp->if_start = vtnet_start;
931 IFQ_SET_MAXLEN(&ifp->if_snd, virtqueue_size(vq) - 1);
932 ifp->if_snd.ifq_drv_maxlen = virtqueue_size(vq) - 1;
933 IFQ_SET_READY(&ifp->if_snd);
936 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
938 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
939 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
941 /* Read (or generate) the MAC address for the adapter. */
942 vtnet_get_hwaddr(sc);
944 ether_ifattach(ifp, sc->vtnet_hwaddr);
946 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS))
947 ifp->if_capabilities |= IFCAP_LINKSTATE;
949 /* Tell the upper layer(s) we support long frames. */
950 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
951 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
953 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
954 ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6;
956 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO)) {
957 ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6;
958 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
960 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
961 ifp->if_capabilities |= IFCAP_TSO4;
962 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
963 ifp->if_capabilities |= IFCAP_TSO6;
964 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
965 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
968 if (ifp->if_capabilities & IFCAP_TSO)
969 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
972 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
973 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
975 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
976 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
977 ifp->if_capabilities |= IFCAP_LRO;
980 if (ifp->if_capabilities & IFCAP_HWCSUM) {
982 * VirtIO does not support VLAN tagging, but we can fake
983 * it by inserting and removing the 802.1Q header during
984 * transmit and receive. We are then able to do checksum
985 * offloading of VLAN frames.
987 ifp->if_capabilities |=
988 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
991 ifp->if_capenable = ifp->if_capabilities;
994 * Capabilities after here are not enabled by default.
997 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
998 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
1000 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1001 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
1002 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1003 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
1006 vtnet_set_rx_process_limit(sc);
1007 vtnet_set_tx_intr_threshold(sc);
1013 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1016 int frame_size, clsize;
1018 ifp = sc->vtnet_ifp;
1020 if (new_mtu < ETHERMIN || new_mtu > VTNET_MAX_MTU)
1023 frame_size = sc->vtnet_hdr_size + sizeof(struct ether_vlan_header) +
1027 * Based on the new MTU (and hence frame size) determine which
1028 * cluster size is most appropriate for the receive queues.
1030 if (frame_size <= MCLBYTES) {
1032 } else if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1033 /* Avoid going past 9K jumbos. */
1034 if (frame_size > MJUM9BYTES)
1036 clsize = MJUM9BYTES;
1038 clsize = MJUMPAGESIZE;
1040 ifp->if_mtu = new_mtu;
1041 sc->vtnet_rx_new_clsize = clsize;
1043 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1044 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1045 vtnet_init_locked(sc);
1052 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1054 struct vtnet_softc *sc;
1056 int reinit, mask, error;
1059 ifr = (struct ifreq *) data;
1064 if (ifp->if_mtu != ifr->ifr_mtu) {
1065 VTNET_CORE_LOCK(sc);
1066 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
1067 VTNET_CORE_UNLOCK(sc);
1072 VTNET_CORE_LOCK(sc);
1073 if ((ifp->if_flags & IFF_UP) == 0) {
1074 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1076 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1077 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
1078 (IFF_PROMISC | IFF_ALLMULTI)) {
1079 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1080 vtnet_rx_filter(sc);
1082 ifp->if_flags |= IFF_PROMISC;
1083 if ((ifp->if_flags ^ sc->vtnet_if_flags)
1089 vtnet_init_locked(sc);
1092 sc->vtnet_if_flags = ifp->if_flags;
1093 VTNET_CORE_UNLOCK(sc);
1098 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
1100 VTNET_CORE_LOCK(sc);
1101 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1102 vtnet_rx_filter_mac(sc);
1103 VTNET_CORE_UNLOCK(sc);
1108 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1112 VTNET_CORE_LOCK(sc);
1113 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1115 if (mask & IFCAP_TXCSUM)
1116 ifp->if_capenable ^= IFCAP_TXCSUM;
1117 if (mask & IFCAP_TXCSUM_IPV6)
1118 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
1119 if (mask & IFCAP_TSO4)
1120 ifp->if_capenable ^= IFCAP_TSO4;
1121 if (mask & IFCAP_TSO6)
1122 ifp->if_capenable ^= IFCAP_TSO6;
1124 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO |
1125 IFCAP_VLAN_HWFILTER)) {
1126 /* These Rx features require us to renegotiate. */
1129 if (mask & IFCAP_RXCSUM)
1130 ifp->if_capenable ^= IFCAP_RXCSUM;
1131 if (mask & IFCAP_RXCSUM_IPV6)
1132 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
1133 if (mask & IFCAP_LRO)
1134 ifp->if_capenable ^= IFCAP_LRO;
1135 if (mask & IFCAP_VLAN_HWFILTER)
1136 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1140 if (mask & IFCAP_VLAN_HWTSO)
1141 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1142 if (mask & IFCAP_VLAN_HWTAGGING)
1143 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1145 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1146 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1147 vtnet_init_locked(sc);
1150 VTNET_CORE_UNLOCK(sc);
1151 VLAN_CAPABILITIES(ifp);
1156 error = ether_ioctl(ifp, cmd, data);
1160 VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc);
1166 vtnet_rxq_populate(struct vtnet_rxq *rxq)
1168 struct virtqueue *vq;
1174 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1175 error = vtnet_rxq_new_buf(rxq);
1181 virtqueue_notify(vq);
1183 * EMSGSIZE signifies the virtqueue did not have enough
1184 * entries available to hold the last mbuf. This is not
1187 if (error == EMSGSIZE)
1195 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq)
1197 struct virtqueue *vq;
1204 while ((m = virtqueue_drain(vq, &last)) != NULL)
1207 KASSERT(virtqueue_empty(vq),
1208 ("%s: mbufs remaining in rx queue %p", __func__, rxq));
1211 static struct mbuf *
1212 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1214 struct mbuf *m_head, *m_tail, *m;
1217 clsize = sc->vtnet_rx_clsize;
1219 KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1220 ("%s: chained mbuf %d request without LRO_NOMRG", __func__, nbufs));
1222 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize);
1226 m_head->m_len = clsize;
1229 /* Allocate the rest of the chain. */
1230 for (i = 1; i < nbufs; i++) {
1231 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize);
1240 if (m_tailp != NULL)
1246 sc->vtnet_stats.mbuf_alloc_failed++;
1253 * Slow path for when LRO without mergeable buffers is negotiated.
1256 vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *rxq, struct mbuf *m0,
1259 struct vtnet_softc *sc;
1260 struct mbuf *m, *m_prev;
1261 struct mbuf *m_new, *m_tail;
1262 int len, clsize, nreplace, error;
1265 clsize = sc->vtnet_rx_clsize;
1275 * Since these mbuf chains are so large, we avoid allocating an
1276 * entire replacement chain if possible. When the received frame
1277 * did not consume the entire chain, the unused mbufs are moved
1278 * to the replacement chain.
1282 * Something is seriously wrong if we received a frame
1283 * larger than the chain. Drop it.
1286 sc->vtnet_stats.rx_frame_too_large++;
1290 /* We always allocate the same cluster size. */
1291 KASSERT(m->m_len == clsize,
1292 ("%s: mbuf size %d is not the cluster size %d",
1293 __func__, m->m_len, clsize));
1295 m->m_len = MIN(m->m_len, len);
1303 KASSERT(nreplace <= sc->vtnet_rx_nmbufs,
1304 ("%s: too many replacement mbufs %d max %d", __func__, nreplace,
1305 sc->vtnet_rx_nmbufs));
1307 m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail);
1308 if (m_new == NULL) {
1309 m_prev->m_len = clsize;
1314 * Move any unused mbufs from the received chain onto the end
1317 if (m_prev->m_next != NULL) {
1318 m_tail->m_next = m_prev->m_next;
1319 m_prev->m_next = NULL;
1322 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1325 * BAD! We could not enqueue the replacement mbuf chain. We
1326 * must restore the m0 chain to the original state if it was
1327 * modified so we can subsequently discard it.
1329 * NOTE: The replacement is suppose to be an identical copy
1330 * to the one just dequeued so this is an unexpected error.
1332 sc->vtnet_stats.rx_enq_replacement_failed++;
1334 if (m_tail->m_next != NULL) {
1335 m_prev->m_next = m_tail->m_next;
1336 m_tail->m_next = NULL;
1339 m_prev->m_len = clsize;
1347 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len)
1349 struct vtnet_softc *sc;
1355 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1356 ("%s: chained mbuf without LRO_NOMRG", __func__));
1358 if (m->m_next == NULL) {
1359 /* Fast-path for the common case of just one mbuf. */
1363 m_new = vtnet_rx_alloc_buf(sc, 1, NULL);
1367 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1370 * The new mbuf is suppose to be an identical
1371 * copy of the one just dequeued so this is an
1375 sc->vtnet_stats.rx_enq_replacement_failed++;
1379 error = vtnet_rxq_replace_lro_nomgr_buf(rxq, m, len);
1385 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1387 struct vtnet_softc *sc;
1389 struct vtnet_rx_header *rxhdr;
1395 mdata = mtod(m, uint8_t *);
1397 VTNET_RXQ_LOCK_ASSERT(rxq);
1398 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1399 ("%s: chained mbuf without LRO_NOMRG", __func__));
1400 KASSERT(m->m_len == sc->vtnet_rx_clsize,
1401 ("%s: unexpected cluster size %d/%d", __func__, m->m_len,
1402 sc->vtnet_rx_clsize));
1405 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1406 MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr));
1407 rxhdr = (struct vtnet_rx_header *) mdata;
1408 sglist_append(sg, &rxhdr->vrh_hdr, sc->vtnet_hdr_size);
1409 offset = sizeof(struct vtnet_rx_header);
1413 sglist_append(sg, mdata + offset, m->m_len - offset);
1414 if (m->m_next != NULL) {
1415 error = sglist_append_mbuf(sg, m->m_next);
1419 error = virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg);
1425 vtnet_rxq_new_buf(struct vtnet_rxq *rxq)
1427 struct vtnet_softc *sc;
1433 m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL);
1437 error = vtnet_rxq_enqueue_buf(rxq, m);
1445 * Use the checksum offset in the VirtIO header to set the
1446 * correct CSUM_* flags.
1449 vtnet_rxq_csum_by_offset(struct vtnet_rxq *rxq, struct mbuf *m,
1450 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1452 struct vtnet_softc *sc;
1453 #if defined(INET) || defined(INET6)
1454 int offset = hdr->csum_start + hdr->csum_offset;
1459 /* Only do a basic sanity check on the offset. */
1463 if (__predict_false(offset < ip_start + sizeof(struct ip)))
1468 case ETHERTYPE_IPV6:
1469 if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
1474 sc->vtnet_stats.rx_csum_bad_ethtype++;
1479 * Use the offset to determine the appropriate CSUM_* flags. This is
1480 * a bit dirty, but we can get by with it since the checksum offsets
1481 * happen to be different. We assume the host host does not do IPv4
1482 * header checksum offloading.
1484 switch (hdr->csum_offset) {
1485 case offsetof(struct udphdr, uh_sum):
1486 case offsetof(struct tcphdr, th_sum):
1487 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1488 m->m_pkthdr.csum_data = 0xFFFF;
1490 case offsetof(struct sctphdr, checksum):
1491 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1494 sc->vtnet_stats.rx_csum_bad_offset++;
1502 vtnet_rxq_csum_by_parse(struct vtnet_rxq *rxq, struct mbuf *m,
1503 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1505 struct vtnet_softc *sc;
1512 case ETHERTYPE_IP: {
1514 if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
1516 ip = (struct ip *)(m->m_data + ip_start);
1518 offset = ip_start + (ip->ip_hl << 2);
1523 case ETHERTYPE_IPV6:
1524 if (__predict_false(m->m_len < ip_start +
1525 sizeof(struct ip6_hdr)))
1527 offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
1528 if (__predict_false(offset < 0))
1533 sc->vtnet_stats.rx_csum_bad_ethtype++;
1539 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
1541 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1542 m->m_pkthdr.csum_data = 0xFFFF;
1545 if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
1547 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1548 m->m_pkthdr.csum_data = 0xFFFF;
1551 if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
1553 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1557 * For the remaining protocols, FreeBSD does not support
1558 * checksum offloading, so the checksum will be recomputed.
1561 if_printf(sc->vtnet_ifp, "cksum offload of unsupported "
1562 "protocol eth_type=%#x proto=%d csum_start=%d "
1563 "csum_offset=%d\n", __func__, eth_type, proto,
1564 hdr->csum_start, hdr->csum_offset);
1573 * Set the appropriate CSUM_* flags. Unfortunately, the information
1574 * provided is not directly useful to us. The VirtIO header gives the
1575 * offset of the checksum, which is all Linux needs, but this is not
1576 * how FreeBSD does things. We are forced to peek inside the packet
1579 * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
1580 * could accept the offsets and let the stack figure it out.
1583 vtnet_rxq_csum(struct vtnet_rxq *rxq, struct mbuf *m,
1584 struct virtio_net_hdr *hdr)
1586 struct ether_header *eh;
1587 struct ether_vlan_header *evh;
1591 eh = mtod(m, struct ether_header *);
1592 eth_type = ntohs(eh->ether_type);
1593 if (eth_type == ETHERTYPE_VLAN) {
1594 /* BMV: We should handle nested VLAN tags too. */
1595 evh = mtod(m, struct ether_vlan_header *);
1596 eth_type = ntohs(evh->evl_proto);
1597 offset = sizeof(struct ether_vlan_header);
1599 offset = sizeof(struct ether_header);
1601 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1602 error = vtnet_rxq_csum_by_offset(rxq, m, eth_type, offset, hdr);
1604 error = vtnet_rxq_csum_by_parse(rxq, m, eth_type, offset, hdr);
1610 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs)
1614 while (--nbufs > 0) {
1615 m = virtqueue_dequeue(rxq->vtnrx_vq, NULL);
1618 vtnet_rxq_discard_buf(rxq, m);
1623 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1628 * Requeue the discarded mbuf. This should always be successful
1629 * since it was just dequeued.
1631 error = vtnet_rxq_enqueue_buf(rxq, m);
1633 ("%s: cannot requeue discarded mbuf %d", __func__, error));
1637 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs)
1639 struct vtnet_softc *sc;
1641 struct virtqueue *vq;
1642 struct mbuf *m, *m_tail;
1647 ifp = sc->vtnet_ifp;
1650 while (--nbufs > 0) {
1651 m = virtqueue_dequeue(vq, &len);
1653 rxq->vtnrx_stats.vrxs_ierrors++;
1657 if (vtnet_rxq_new_buf(rxq) != 0) {
1658 rxq->vtnrx_stats.vrxs_iqdrops++;
1659 vtnet_rxq_discard_buf(rxq, m);
1661 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1669 m->m_flags &= ~M_PKTHDR;
1671 m_head->m_pkthdr.len += len;
1679 sc->vtnet_stats.rx_mergeable_failed++;
1686 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m,
1687 struct virtio_net_hdr *hdr)
1689 struct vtnet_softc *sc;
1691 struct ether_header *eh;
1694 ifp = sc->vtnet_ifp;
1696 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1697 eh = mtod(m, struct ether_header *);
1698 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1699 vtnet_vlan_tag_remove(m);
1701 * With the 802.1Q header removed, update the
1702 * checksum starting location accordingly.
1704 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1705 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN;
1709 m->m_pkthdr.flowid = rxq->vtnrx_id;
1710 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
1713 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum
1714 * distinction that Linux does. Need to reevaluate if performing
1715 * offloading for the NEEDS_CSUM case is really appropriate.
1717 if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM |
1718 VIRTIO_NET_HDR_F_DATA_VALID)) {
1719 if (vtnet_rxq_csum(rxq, m, hdr) == 0)
1720 rxq->vtnrx_stats.vrxs_csum++;
1722 rxq->vtnrx_stats.vrxs_csum_failed++;
1725 rxq->vtnrx_stats.vrxs_ipackets++;
1726 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len;
1728 VTNET_RXQ_UNLOCK(rxq);
1729 (*ifp->if_input)(ifp, m);
1730 VTNET_RXQ_LOCK(rxq);
1734 vtnet_rxq_eof(struct vtnet_rxq *rxq)
1736 struct virtio_net_hdr lhdr, *hdr;
1737 struct vtnet_softc *sc;
1739 struct virtqueue *vq;
1741 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1742 int len, deq, nbufs, adjsz, count;
1746 ifp = sc->vtnet_ifp;
1749 count = sc->vtnet_rx_process_limit;
1751 VTNET_RXQ_LOCK_ASSERT(rxq);
1754 if (netmap_rx_irq(ifp, 0, &deq)) {
1757 #endif /* DEV_NETMAP */
1759 while (count-- > 0) {
1760 m = virtqueue_dequeue(vq, &len);
1765 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1766 rxq->vtnrx_stats.vrxs_ierrors++;
1767 vtnet_rxq_discard_buf(rxq, m);
1771 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1773 adjsz = sizeof(struct vtnet_rx_header);
1775 * Account for our pad inserted between the header
1776 * and the actual start of the frame.
1778 len += VTNET_RX_HEADER_PAD;
1780 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1781 nbufs = mhdr->num_buffers;
1782 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1785 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) {
1786 rxq->vtnrx_stats.vrxs_iqdrops++;
1787 vtnet_rxq_discard_buf(rxq, m);
1789 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1793 m->m_pkthdr.len = len;
1794 m->m_pkthdr.rcvif = ifp;
1795 m->m_pkthdr.csum_flags = 0;
1798 /* Dequeue the rest of chain. */
1799 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0)
1804 * Save copy of header before we strip it. For both mergeable
1805 * and non-mergeable, the header is at the beginning of the
1806 * mbuf data. We no longer need num_buffers, so always use a
1809 * BMV: Is this memcpy() expensive? We know the mbuf data is
1810 * still valid even after the m_adj().
1812 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1815 vtnet_rxq_input(rxq, m, hdr);
1817 /* Must recheck after dropping the Rx lock. */
1818 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1823 virtqueue_notify(vq);
1825 return (count > 0 ? 0 : EAGAIN);
1829 vtnet_rx_vq_intr(void *xrxq)
1831 struct vtnet_softc *sc;
1832 struct vtnet_rxq *rxq;
1838 ifp = sc->vtnet_ifp;
1841 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) {
1843 * Ignore this interrupt. Either this is a spurious interrupt
1844 * or multiqueue without per-VQ MSIX so every queue needs to
1845 * be polled (a brain dead configuration we could try harder
1848 vtnet_rxq_disable_intr(rxq);
1852 VTNET_RXQ_LOCK(rxq);
1855 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1856 VTNET_RXQ_UNLOCK(rxq);
1860 more = vtnet_rxq_eof(rxq);
1861 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1863 vtnet_rxq_disable_intr(rxq);
1865 * This is an occasional condition or race (when !more),
1866 * so retry a few times before scheduling the taskqueue.
1868 if (tries++ < VTNET_INTR_DISABLE_RETRIES)
1871 VTNET_RXQ_UNLOCK(rxq);
1872 rxq->vtnrx_stats.vrxs_rescheduled++;
1873 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1875 VTNET_RXQ_UNLOCK(rxq);
1879 vtnet_rxq_tq_intr(void *xrxq, int pending)
1881 struct vtnet_softc *sc;
1882 struct vtnet_rxq *rxq;
1888 ifp = sc->vtnet_ifp;
1890 VTNET_RXQ_LOCK(rxq);
1892 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1893 VTNET_RXQ_UNLOCK(rxq);
1897 more = vtnet_rxq_eof(rxq);
1898 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1900 vtnet_rxq_disable_intr(rxq);
1901 rxq->vtnrx_stats.vrxs_rescheduled++;
1902 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1905 VTNET_RXQ_UNLOCK(rxq);
1909 vtnet_txq_below_threshold(struct vtnet_txq *txq)
1911 struct vtnet_softc *sc;
1912 struct virtqueue *vq;
1917 return (virtqueue_nfree(vq) <= sc->vtnet_tx_intr_thresh);
1921 vtnet_txq_notify(struct vtnet_txq *txq)
1923 struct virtqueue *vq;
1927 txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
1928 virtqueue_notify(vq);
1930 if (vtnet_txq_enable_intr(txq) == 0)
1934 * Drain frames that were completed since last checked. If this
1935 * causes the queue to go above the threshold, the caller should
1936 * continue transmitting.
1938 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) {
1939 virtqueue_disable_intr(vq);
1947 vtnet_txq_free_mbufs(struct vtnet_txq *txq)
1949 struct virtqueue *vq;
1950 struct vtnet_tx_header *txhdr;
1956 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1957 m_freem(txhdr->vth_mbuf);
1958 uma_zfree(vtnet_tx_header_zone, txhdr);
1961 KASSERT(virtqueue_empty(vq),
1962 ("%s: mbufs remaining in tx queue %p", __func__, txq));
1966 * BMV: Much of this can go away once we finally have offsets in
1967 * the mbuf packet header. Bug andre@.
1970 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m,
1971 int *etype, int *proto, int *start)
1973 struct vtnet_softc *sc;
1974 struct ether_vlan_header *evh;
1979 evh = mtod(m, struct ether_vlan_header *);
1980 if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1981 /* BMV: We should handle nested VLAN tags too. */
1982 *etype = ntohs(evh->evl_proto);
1983 offset = sizeof(struct ether_vlan_header);
1985 *etype = ntohs(evh->evl_encap_proto);
1986 offset = sizeof(struct ether_header);
1991 case ETHERTYPE_IP: {
1992 struct ip *ip, iphdr;
1993 if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
1994 m_copydata(m, offset, sizeof(struct ip),
1998 ip = (struct ip *)(m->m_data + offset);
2000 *start = offset + (ip->ip_hl << 2);
2005 case ETHERTYPE_IPV6:
2007 *start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
2008 /* Assert the network stack sent us a valid packet. */
2009 KASSERT(*start > offset,
2010 ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
2011 *start, offset, *proto));
2015 sc->vtnet_stats.tx_csum_bad_ethtype++;
2023 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type,
2024 int offset, struct virtio_net_hdr *hdr)
2026 static struct timeval lastecn;
2028 struct vtnet_softc *sc;
2029 struct tcphdr *tcp, tcphdr;
2033 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
2034 m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
2037 tcp = (struct tcphdr *)(m->m_data + offset);
2039 hdr->hdr_len = offset + (tcp->th_off << 2);
2040 hdr->gso_size = m->m_pkthdr.tso_segsz;
2041 hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
2042 VIRTIO_NET_HDR_GSO_TCPV6;
2044 if (tcp->th_flags & TH_CWR) {
2046 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
2047 * ECN support is not on a per-interface basis, but globally via
2048 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
2050 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
2051 if (ppsratecheck(&lastecn, &curecn, 1))
2052 if_printf(sc->vtnet_ifp,
2053 "TSO with ECN not negotiated with host\n");
2056 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2059 txq->vtntx_stats.vtxs_tso++;
2064 static struct mbuf *
2065 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m,
2066 struct virtio_net_hdr *hdr)
2068 struct vtnet_softc *sc;
2069 int flags, etype, csum_start, proto, error;
2072 flags = m->m_pkthdr.csum_flags;
2074 error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start);
2078 if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) ||
2079 (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) {
2081 * We could compare the IP protocol vs the CSUM_ flag too,
2082 * but that really should not be necessary.
2084 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
2085 hdr->csum_start = csum_start;
2086 hdr->csum_offset = m->m_pkthdr.csum_data;
2087 txq->vtntx_stats.vtxs_csum++;
2090 if (flags & CSUM_TSO) {
2091 if (__predict_false(proto != IPPROTO_TCP)) {
2092 /* Likely failed to correctly parse the mbuf. */
2093 sc->vtnet_stats.tx_tso_not_tcp++;
2097 KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
2098 ("%s: mbuf %p TSO without checksum offload %#x",
2099 __func__, m, flags));
2101 error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr);
2114 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head,
2115 struct vtnet_tx_header *txhdr)
2117 struct vtnet_softc *sc;
2118 struct virtqueue *vq;
2129 error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
2130 KASSERT(error == 0 && sg->sg_nseg == 1,
2131 ("%s: error %d adding header to sglist", __func__, error));
2133 error = sglist_append_mbuf(sg, m);
2135 m = m_defrag(m, M_NOWAIT);
2140 sc->vtnet_stats.tx_defragged++;
2142 error = sglist_append_mbuf(sg, m);
2147 txhdr->vth_mbuf = m;
2148 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0);
2153 sc->vtnet_stats.tx_defrag_failed++;
2161 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head)
2163 struct vtnet_tx_header *txhdr;
2164 struct virtio_net_hdr *hdr;
2171 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO);
2172 if (txhdr == NULL) {
2179 * Always use the non-mergeable header, regardless if the feature
2180 * was negotiated. For transmit, num_buffers is always zero. The
2181 * vtnet_hdr_size is used to enqueue the correct header size.
2183 hdr = &txhdr->vth_uhdr.hdr;
2185 if (m->m_flags & M_VLANTAG) {
2186 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2187 if ((*m_head = m) == NULL) {
2191 m->m_flags &= ~M_VLANTAG;
2194 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) {
2195 m = vtnet_txq_offload(txq, m, hdr);
2196 if ((*m_head = m) == NULL) {
2202 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr);
2207 uma_zfree(vtnet_tx_header_zone, txhdr);
2212 #ifdef VTNET_LEGACY_TX
2215 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp)
2217 struct vtnet_softc *sc;
2218 struct virtqueue *vq;
2226 VTNET_TXQ_LOCK_ASSERT(txq);
2228 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2229 sc->vtnet_link_active == 0)
2237 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2238 if (virtqueue_full(vq))
2241 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2245 if (vtnet_txq_encap(txq, &m0) != 0) {
2247 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2252 ETHER_BPF_MTAP(ifp, m0);
2255 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2256 if (tries++ < VTNET_NOTIFY_RETRIES)
2259 txq->vtntx_stats.vtxs_rescheduled++;
2260 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2265 vtnet_start(struct ifnet *ifp)
2267 struct vtnet_softc *sc;
2268 struct vtnet_txq *txq;
2271 txq = &sc->vtnet_txqs[0];
2273 VTNET_TXQ_LOCK(txq);
2274 vtnet_start_locked(txq, ifp);
2275 VTNET_TXQ_UNLOCK(txq);
2278 #else /* !VTNET_LEGACY_TX */
2281 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m)
2283 struct vtnet_softc *sc;
2284 struct virtqueue *vq;
2285 struct buf_ring *br;
2287 int enq, tries, error;
2292 ifp = sc->vtnet_ifp;
2296 VTNET_TXQ_LOCK_ASSERT(txq);
2298 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2299 sc->vtnet_link_active == 0) {
2301 error = drbr_enqueue(ifp, br, m);
2306 error = drbr_enqueue(ifp, br, m);
2316 while ((m = drbr_peek(ifp, br)) != NULL) {
2317 if (virtqueue_full(vq)) {
2318 drbr_putback(ifp, br, m);
2322 if (vtnet_txq_encap(txq, &m) != 0) {
2324 drbr_putback(ifp, br, m);
2326 drbr_advance(ifp, br);
2329 drbr_advance(ifp, br);
2332 ETHER_BPF_MTAP(ifp, m);
2335 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2336 if (tries++ < VTNET_NOTIFY_RETRIES)
2339 txq->vtntx_stats.vtxs_rescheduled++;
2340 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2347 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m)
2349 struct vtnet_softc *sc;
2350 struct vtnet_txq *txq;
2351 int i, npairs, error;
2354 npairs = sc->vtnet_act_vq_pairs;
2356 /* check if flowid is set */
2357 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2358 i = m->m_pkthdr.flowid % npairs;
2360 i = curcpu % npairs;
2362 txq = &sc->vtnet_txqs[i];
2364 if (VTNET_TXQ_TRYLOCK(txq) != 0) {
2365 error = vtnet_txq_mq_start_locked(txq, m);
2366 VTNET_TXQ_UNLOCK(txq);
2368 error = drbr_enqueue(ifp, txq->vtntx_br, m);
2369 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask);
2376 vtnet_txq_tq_deferred(void *xtxq, int pending)
2378 struct vtnet_softc *sc;
2379 struct vtnet_txq *txq;
2384 VTNET_TXQ_LOCK(txq);
2385 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br))
2386 vtnet_txq_mq_start_locked(txq, NULL);
2387 VTNET_TXQ_UNLOCK(txq);
2390 #endif /* VTNET_LEGACY_TX */
2393 vtnet_txq_start(struct vtnet_txq *txq)
2395 struct vtnet_softc *sc;
2399 ifp = sc->vtnet_ifp;
2401 #ifdef VTNET_LEGACY_TX
2402 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2403 vtnet_start_locked(txq, ifp);
2405 if (!drbr_empty(ifp, txq->vtntx_br))
2406 vtnet_txq_mq_start_locked(txq, NULL);
2411 vtnet_txq_tq_intr(void *xtxq, int pending)
2413 struct vtnet_softc *sc;
2414 struct vtnet_txq *txq;
2419 ifp = sc->vtnet_ifp;
2421 VTNET_TXQ_LOCK(txq);
2423 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2424 VTNET_TXQ_UNLOCK(txq);
2429 vtnet_txq_start(txq);
2431 VTNET_TXQ_UNLOCK(txq);
2435 vtnet_txq_eof(struct vtnet_txq *txq)
2437 struct virtqueue *vq;
2438 struct vtnet_tx_header *txhdr;
2444 VTNET_TXQ_LOCK_ASSERT(txq);
2447 if (netmap_tx_irq(txq->vtntx_sc->vtnet_ifp, txq->vtntx_id)) {
2448 virtqueue_disable_intr(vq); // XXX luigi
2449 return 0; // XXX or 1 ?
2451 #endif /* DEV_NETMAP */
2453 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
2454 m = txhdr->vth_mbuf;
2457 txq->vtntx_stats.vtxs_opackets++;
2458 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len;
2459 if (m->m_flags & M_MCAST)
2460 txq->vtntx_stats.vtxs_omcasts++;
2463 uma_zfree(vtnet_tx_header_zone, txhdr);
2466 if (virtqueue_empty(vq))
2467 txq->vtntx_watchdog = 0;
2473 vtnet_tx_vq_intr(void *xtxq)
2475 struct vtnet_softc *sc;
2476 struct vtnet_txq *txq;
2481 ifp = sc->vtnet_ifp;
2483 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) {
2485 * Ignore this interrupt. Either this is a spurious interrupt
2486 * or multiqueue without per-VQ MSIX so every queue needs to
2487 * be polled (a brain dead configuration we could try harder
2490 vtnet_txq_disable_intr(txq);
2494 VTNET_TXQ_LOCK(txq);
2496 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2497 VTNET_TXQ_UNLOCK(txq);
2502 vtnet_txq_start(txq);
2504 VTNET_TXQ_UNLOCK(txq);
2508 vtnet_tx_start_all(struct vtnet_softc *sc)
2510 struct vtnet_txq *txq;
2513 VTNET_CORE_LOCK_ASSERT(sc);
2515 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2516 txq = &sc->vtnet_txqs[i];
2518 VTNET_TXQ_LOCK(txq);
2519 vtnet_txq_start(txq);
2520 VTNET_TXQ_UNLOCK(txq);
2524 #ifndef VTNET_LEGACY_TX
2526 vtnet_qflush(struct ifnet *ifp)
2528 struct vtnet_softc *sc;
2529 struct vtnet_txq *txq;
2535 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2536 txq = &sc->vtnet_txqs[i];
2538 VTNET_TXQ_LOCK(txq);
2539 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL)
2541 VTNET_TXQ_UNLOCK(txq);
2549 vtnet_watchdog(struct vtnet_txq *txq)
2553 ifp = txq->vtntx_sc->vtnet_ifp;
2555 VTNET_TXQ_LOCK(txq);
2556 if (txq->vtntx_watchdog == 1) {
2558 * Only drain completed frames if the watchdog is about to
2559 * expire. If any frames were drained, there may be enough
2560 * free descriptors now available to transmit queued frames.
2561 * In that case, the timer will immediately be decremented
2562 * below, but the timeout is generous enough that should not
2565 if (vtnet_txq_eof(txq) != 0)
2566 vtnet_txq_start(txq);
2569 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) {
2570 VTNET_TXQ_UNLOCK(txq);
2573 VTNET_TXQ_UNLOCK(txq);
2575 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id);
2580 vtnet_rxq_accum_stats(struct vtnet_rxq *rxq, struct vtnet_rxq_stats *accum)
2582 struct vtnet_rxq_stats *st;
2584 st = &rxq->vtnrx_stats;
2586 accum->vrxs_ipackets += st->vrxs_ipackets;
2587 accum->vrxs_ibytes += st->vrxs_ibytes;
2588 accum->vrxs_iqdrops += st->vrxs_iqdrops;
2589 accum->vrxs_csum += st->vrxs_csum;
2590 accum->vrxs_csum_failed += st->vrxs_csum_failed;
2591 accum->vrxs_rescheduled += st->vrxs_rescheduled;
2595 vtnet_txq_accum_stats(struct vtnet_txq *txq, struct vtnet_txq_stats *accum)
2597 struct vtnet_txq_stats *st;
2599 st = &txq->vtntx_stats;
2601 accum->vtxs_opackets += st->vtxs_opackets;
2602 accum->vtxs_obytes += st->vtxs_obytes;
2603 accum->vtxs_csum += st->vtxs_csum;
2604 accum->vtxs_tso += st->vtxs_tso;
2605 accum->vtxs_rescheduled += st->vtxs_rescheduled;
2609 vtnet_accumulate_stats(struct vtnet_softc *sc)
2612 struct vtnet_statistics *st;
2613 struct vtnet_rxq_stats rxaccum;
2614 struct vtnet_txq_stats txaccum;
2617 ifp = sc->vtnet_ifp;
2618 st = &sc->vtnet_stats;
2619 bzero(&rxaccum, sizeof(struct vtnet_rxq_stats));
2620 bzero(&txaccum, sizeof(struct vtnet_txq_stats));
2622 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2623 vtnet_rxq_accum_stats(&sc->vtnet_rxqs[i], &rxaccum);
2624 vtnet_txq_accum_stats(&sc->vtnet_txqs[i], &txaccum);
2627 st->rx_csum_offloaded = rxaccum.vrxs_csum;
2628 st->rx_csum_failed = rxaccum.vrxs_csum_failed;
2629 st->rx_task_rescheduled = rxaccum.vrxs_rescheduled;
2630 st->tx_csum_offloaded = txaccum.vtxs_csum;
2631 st->tx_tso_offloaded = txaccum.vtxs_tso;
2632 st->tx_task_rescheduled = txaccum.vtxs_rescheduled;
2635 * With the exception of if_ierrors, these ifnet statistics are
2636 * only updated in the driver, so just set them to our accumulated
2637 * values. if_ierrors is updated in ether_input() for malformed
2638 * frames that we should have already discarded.
2640 ifp->if_ipackets = rxaccum.vrxs_ipackets;
2641 ifp->if_iqdrops = rxaccum.vrxs_iqdrops;
2642 ifp->if_ierrors = rxaccum.vrxs_ierrors;
2643 ifp->if_opackets = txaccum.vtxs_opackets;
2644 #ifndef VTNET_LEGACY_TX
2645 ifp->if_obytes = txaccum.vtxs_obytes;
2646 ifp->if_omcasts = txaccum.vtxs_omcasts;
2651 vtnet_tick(void *xsc)
2653 struct vtnet_softc *sc;
2658 ifp = sc->vtnet_ifp;
2661 VTNET_CORE_LOCK_ASSERT(sc);
2662 vtnet_accumulate_stats(sc);
2664 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
2665 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]);
2667 if (timedout != 0) {
2668 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2669 vtnet_init_locked(sc);
2671 callout_schedule(&sc->vtnet_tick_ch, hz);
2675 vtnet_start_taskqueues(struct vtnet_softc *sc)
2678 struct vtnet_rxq *rxq;
2679 struct vtnet_txq *txq;
2682 dev = sc->vtnet_dev;
2685 * Errors here are very difficult to recover from - we cannot
2686 * easily fail because, if this is during boot, we will hang
2687 * when freeing any successfully started taskqueues because
2688 * the scheduler isn't up yet.
2690 * Most drivers just ignore the return value - it only fails
2691 * with ENOMEM so an error is not likely.
2693 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2694 rxq = &sc->vtnet_rxqs[i];
2695 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET,
2696 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id);
2698 device_printf(dev, "failed to start rx taskq %d\n",
2702 txq = &sc->vtnet_txqs[i];
2703 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET,
2704 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id);
2706 device_printf(dev, "failed to start tx taskq %d\n",
2713 vtnet_free_taskqueues(struct vtnet_softc *sc)
2715 struct vtnet_rxq *rxq;
2716 struct vtnet_txq *txq;
2719 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2720 rxq = &sc->vtnet_rxqs[i];
2721 if (rxq->vtnrx_tq != NULL) {
2722 taskqueue_free(rxq->vtnrx_tq);
2723 rxq->vtnrx_vq = NULL;
2726 txq = &sc->vtnet_txqs[i];
2727 if (txq->vtntx_tq != NULL) {
2728 taskqueue_free(txq->vtntx_tq);
2729 txq->vtntx_tq = NULL;
2735 vtnet_drain_taskqueues(struct vtnet_softc *sc)
2737 struct vtnet_rxq *rxq;
2738 struct vtnet_txq *txq;
2741 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2742 rxq = &sc->vtnet_rxqs[i];
2743 if (rxq->vtnrx_tq != NULL)
2744 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2746 txq = &sc->vtnet_txqs[i];
2747 if (txq->vtntx_tq != NULL) {
2748 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask);
2749 #ifndef VTNET_LEGACY_TX
2750 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask);
2757 vtnet_drain_rxtx_queues(struct vtnet_softc *sc)
2759 struct vtnet_rxq *rxq;
2760 struct vtnet_txq *txq;
2764 if (nm_native_on(NA(sc->vtnet_ifp)))
2766 #endif /* DEV_NETMAP */
2768 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2769 rxq = &sc->vtnet_rxqs[i];
2770 vtnet_rxq_free_mbufs(rxq);
2772 txq = &sc->vtnet_txqs[i];
2773 vtnet_txq_free_mbufs(txq);
2778 vtnet_stop_rendezvous(struct vtnet_softc *sc)
2780 struct vtnet_rxq *rxq;
2781 struct vtnet_txq *txq;
2785 * Lock and unlock the per-queue mutex so we known the stop
2786 * state is visible. Doing only the active queues should be
2787 * sufficient, but it does not cost much extra to do all the
2788 * queues. Note we hold the core mutex here too.
2790 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2791 rxq = &sc->vtnet_rxqs[i];
2792 VTNET_RXQ_LOCK(rxq);
2793 VTNET_RXQ_UNLOCK(rxq);
2795 txq = &sc->vtnet_txqs[i];
2796 VTNET_TXQ_LOCK(txq);
2797 VTNET_TXQ_UNLOCK(txq);
2802 vtnet_stop(struct vtnet_softc *sc)
2807 dev = sc->vtnet_dev;
2808 ifp = sc->vtnet_ifp;
2810 VTNET_CORE_LOCK_ASSERT(sc);
2812 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2813 sc->vtnet_link_active = 0;
2814 callout_stop(&sc->vtnet_tick_ch);
2816 /* Only advisory. */
2817 vtnet_disable_interrupts(sc);
2820 * Stop the host adapter. This resets it to the pre-initialized
2821 * state. It will not generate any interrupts until after it is
2825 vtnet_stop_rendezvous(sc);
2827 /* Free any mbufs left in the virtqueues. */
2828 vtnet_drain_rxtx_queues(sc);
2832 vtnet_virtio_reinit(struct vtnet_softc *sc)
2839 dev = sc->vtnet_dev;
2840 ifp = sc->vtnet_ifp;
2841 features = sc->vtnet_features;
2845 mask |= IFCAP_RXCSUM;
2848 mask |= IFCAP_RXCSUM_IPV6;
2852 * Re-negotiate with the host, removing any disabled receive
2853 * features. Transmit features are disabled only on our side
2854 * via if_capenable and if_hwassist.
2857 if (ifp->if_capabilities & mask) {
2859 * We require both IPv4 and IPv6 offloading to be enabled
2860 * in order to negotiated it: VirtIO does not distinguish
2863 if ((ifp->if_capenable & mask) != mask)
2864 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2867 if (ifp->if_capabilities & IFCAP_LRO) {
2868 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2869 features &= ~VTNET_LRO_FEATURES;
2872 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2873 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2874 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2877 error = virtio_reinit(dev, features);
2879 device_printf(dev, "virtio reinit error %d\n", error);
2885 vtnet_init_rx_filters(struct vtnet_softc *sc)
2889 ifp = sc->vtnet_ifp;
2891 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2892 /* Restore promiscuous and all-multicast modes. */
2893 vtnet_rx_filter(sc);
2894 /* Restore filtered MAC addresses. */
2895 vtnet_rx_filter_mac(sc);
2898 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2899 vtnet_rx_filter_vlan(sc);
2903 vtnet_init_rx_queues(struct vtnet_softc *sc)
2906 struct vtnet_rxq *rxq;
2907 int i, clsize, error;
2909 dev = sc->vtnet_dev;
2912 * Use the new cluster size if one has been set (via a MTU
2913 * change). Otherwise, use the standard 2K clusters.
2915 * BMV: It might make sense to use page sized clusters as
2916 * the default (depending on the features negotiated).
2918 if (sc->vtnet_rx_new_clsize != 0) {
2919 clsize = sc->vtnet_rx_new_clsize;
2920 sc->vtnet_rx_new_clsize = 0;
2924 sc->vtnet_rx_clsize = clsize;
2925 sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize);
2927 KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS ||
2928 sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs,
2929 ("%s: too many rx mbufs %d for %d segments", __func__,
2930 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs));
2933 if (vtnet_netmap_init_rx_buffers(sc))
2935 #endif /* DEV_NETMAP */
2937 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2938 rxq = &sc->vtnet_rxqs[i];
2940 /* Hold the lock to satisfy asserts. */
2941 VTNET_RXQ_LOCK(rxq);
2942 error = vtnet_rxq_populate(rxq);
2943 VTNET_RXQ_UNLOCK(rxq);
2947 "cannot allocate mbufs for Rx queue %d\n", i);
2956 vtnet_init_tx_queues(struct vtnet_softc *sc)
2958 struct vtnet_txq *txq;
2961 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2962 txq = &sc->vtnet_txqs[i];
2963 txq->vtntx_watchdog = 0;
2970 vtnet_init_rxtx_queues(struct vtnet_softc *sc)
2974 error = vtnet_init_rx_queues(sc);
2978 error = vtnet_init_tx_queues(sc);
2986 vtnet_set_active_vq_pairs(struct vtnet_softc *sc)
2991 dev = sc->vtnet_dev;
2993 if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) {
2994 MPASS(sc->vtnet_max_vq_pairs == 1);
2995 sc->vtnet_act_vq_pairs = 1;
2999 /* BMV: Just use the maximum configured for now. */
3000 npairs = sc->vtnet_max_vq_pairs;
3002 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) {
3004 "cannot set active queue pairs to %d\n", npairs);
3008 sc->vtnet_act_vq_pairs = npairs;
3012 vtnet_reinit(struct vtnet_softc *sc)
3017 ifp = sc->vtnet_ifp;
3019 /* Use the current MAC address. */
3020 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
3021 vtnet_set_hwaddr(sc);
3023 vtnet_set_active_vq_pairs(sc);
3025 ifp->if_hwassist = 0;
3026 if (ifp->if_capenable & IFCAP_TXCSUM)
3027 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
3028 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3029 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6;
3030 if (ifp->if_capenable & IFCAP_TSO4)
3031 ifp->if_hwassist |= CSUM_IP_TSO;
3032 if (ifp->if_capenable & IFCAP_TSO6)
3033 ifp->if_hwassist |= CSUM_IP6_TSO;
3035 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
3036 vtnet_init_rx_filters(sc);
3038 error = vtnet_init_rxtx_queues(sc);
3042 vtnet_enable_interrupts(sc);
3043 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3049 vtnet_init_locked(struct vtnet_softc *sc)
3054 dev = sc->vtnet_dev;
3055 ifp = sc->vtnet_ifp;
3057 VTNET_CORE_LOCK_ASSERT(sc);
3059 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3064 /* Reinitialize with the host. */
3065 if (vtnet_virtio_reinit(sc) != 0)
3068 if (vtnet_reinit(sc) != 0)
3071 virtio_reinit_complete(dev);
3073 vtnet_update_link_status(sc);
3074 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
3083 vtnet_init(void *xsc)
3085 struct vtnet_softc *sc;
3090 if (!NA(sc->vtnet_ifp)) {
3091 D("try to attach again");
3092 vtnet_netmap_attach(sc);
3094 #endif /* DEV_NETMAP */
3096 VTNET_CORE_LOCK(sc);
3097 vtnet_init_locked(sc);
3098 VTNET_CORE_UNLOCK(sc);
3102 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
3104 struct virtqueue *vq;
3106 vq = sc->vtnet_ctrl_vq;
3109 * The control virtqueue is only polled and therefore it should
3112 KASSERT(virtqueue_empty(vq),
3113 ("%s: ctrl vq %p not empty", __func__, vq));
3117 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
3118 struct sglist *sg, int readable, int writable)
3120 struct virtqueue *vq;
3122 vq = sc->vtnet_ctrl_vq;
3124 VTNET_CORE_LOCK_ASSERT(sc);
3125 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
3126 ("%s: CTRL_VQ feature not negotiated", __func__));
3128 if (!virtqueue_empty(vq))
3130 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
3134 * Poll for the response, but the command is likely already
3135 * done when we return from the notify.
3137 virtqueue_notify(vq);
3138 virtqueue_poll(vq, NULL);
3142 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
3144 struct virtio_net_ctrl_hdr hdr __aligned(2);
3145 struct sglist_seg segs[3];
3150 hdr.class = VIRTIO_NET_CTRL_MAC;
3151 hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
3152 ack = VIRTIO_NET_ERR;
3154 sglist_init(&sg, 3, segs);
3156 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3157 error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN);
3158 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3159 KASSERT(error == 0 && sg.sg_nseg == 3,
3160 ("%s: error %d adding set MAC msg to sglist", __func__, error));
3162 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3164 return (ack == VIRTIO_NET_OK ? 0 : EIO);
3168 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs)
3170 struct sglist_seg segs[3];
3173 struct virtio_net_ctrl_hdr hdr;
3175 struct virtio_net_ctrl_mq mq;
3181 s.hdr.class = VIRTIO_NET_CTRL_MQ;
3182 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
3183 s.mq.virtqueue_pairs = npairs;
3184 s.ack = VIRTIO_NET_ERR;
3186 sglist_init(&sg, 3, segs);
3188 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3189 error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq));
3190 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3191 KASSERT(error == 0 && sg.sg_nseg == 3,
3192 ("%s: error %d adding MQ message to sglist", __func__, error));
3194 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3196 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3200 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
3202 struct sglist_seg segs[3];
3205 struct virtio_net_ctrl_hdr hdr;
3213 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3214 ("%s: CTRL_RX feature not negotiated", __func__));
3216 s.hdr.class = VIRTIO_NET_CTRL_RX;
3219 s.ack = VIRTIO_NET_ERR;
3221 sglist_init(&sg, 3, segs);
3223 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3224 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
3225 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3226 KASSERT(error == 0 && sg.sg_nseg == 3,
3227 ("%s: error %d adding Rx message to sglist", __func__, error));
3229 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3231 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3235 vtnet_set_promisc(struct vtnet_softc *sc, int on)
3238 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
3242 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
3245 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
3249 * The device defaults to promiscuous mode for backwards compatibility.
3250 * Turn it off at attach time if possible.
3253 vtnet_attach_disable_promisc(struct vtnet_softc *sc)
3257 ifp = sc->vtnet_ifp;
3259 VTNET_CORE_LOCK(sc);
3260 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) {
3261 ifp->if_flags |= IFF_PROMISC;
3262 } else if (vtnet_set_promisc(sc, 0) != 0) {
3263 ifp->if_flags |= IFF_PROMISC;
3264 device_printf(sc->vtnet_dev,
3265 "cannot disable default promiscuous mode\n");
3267 VTNET_CORE_UNLOCK(sc);
3271 vtnet_rx_filter(struct vtnet_softc *sc)
3276 dev = sc->vtnet_dev;
3277 ifp = sc->vtnet_ifp;
3279 VTNET_CORE_LOCK_ASSERT(sc);
3281 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
3282 device_printf(dev, "cannot %s promiscuous mode\n",
3283 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
3285 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
3286 device_printf(dev, "cannot %s all-multicast mode\n",
3287 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
3291 vtnet_rx_filter_mac(struct vtnet_softc *sc)
3293 struct virtio_net_ctrl_hdr hdr __aligned(2);
3294 struct vtnet_mac_filter *filter;
3295 struct sglist_seg segs[4];
3299 struct ifmultiaddr *ifma;
3300 int ucnt, mcnt, promisc, allmulti, error;
3303 ifp = sc->vtnet_ifp;
3304 filter = sc->vtnet_mac_filter;
3310 VTNET_CORE_LOCK_ASSERT(sc);
3311 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3312 ("%s: CTRL_RX feature not negotiated", __func__));
3314 /* Unicast MAC addresses: */
3316 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3317 if (ifa->ifa_addr->sa_family != AF_LINK)
3319 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3320 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
3322 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
3327 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3328 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
3331 if_addr_runlock(ifp);
3334 filter->vmf_unicast.nentries = 0;
3335 if_printf(ifp, "more than %d MAC addresses assigned, "
3336 "falling back to promiscuous mode\n",
3337 VTNET_MAX_MAC_ENTRIES);
3339 filter->vmf_unicast.nentries = ucnt;
3341 /* Multicast MAC addresses: */
3342 if_maddr_rlock(ifp);
3343 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3344 if (ifma->ifma_addr->sa_family != AF_LINK)
3346 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
3351 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3352 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
3355 if_maddr_runlock(ifp);
3357 if (allmulti != 0) {
3358 filter->vmf_multicast.nentries = 0;
3359 if_printf(ifp, "more than %d multicast MAC addresses "
3360 "assigned, falling back to all-multicast mode\n",
3361 VTNET_MAX_MAC_ENTRIES);
3363 filter->vmf_multicast.nentries = mcnt;
3365 if (promisc != 0 && allmulti != 0)
3368 hdr.class = VIRTIO_NET_CTRL_MAC;
3369 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
3370 ack = VIRTIO_NET_ERR;
3372 sglist_init(&sg, 4, segs);
3374 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3375 error |= sglist_append(&sg, &filter->vmf_unicast,
3376 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
3377 error |= sglist_append(&sg, &filter->vmf_multicast,
3378 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
3379 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3380 KASSERT(error == 0 && sg.sg_nseg == 4,
3381 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
3383 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3385 if (ack != VIRTIO_NET_OK)
3386 if_printf(ifp, "error setting host MAC filter table\n");
3389 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
3390 if_printf(ifp, "cannot enable promiscuous mode\n");
3391 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
3392 if_printf(ifp, "cannot enable all-multicast mode\n");
3396 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3398 struct sglist_seg segs[3];
3401 struct virtio_net_ctrl_hdr hdr;
3409 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
3410 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
3412 s.ack = VIRTIO_NET_ERR;
3414 sglist_init(&sg, 3, segs);
3416 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3417 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
3418 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3419 KASSERT(error == 0 && sg.sg_nseg == 3,
3420 ("%s: error %d adding VLAN message to sglist", __func__, error));
3422 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3424 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3428 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
3434 VTNET_CORE_LOCK_ASSERT(sc);
3435 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
3436 ("%s: VLAN_FILTER feature not negotiated", __func__));
3438 /* Enable the filter for each configured VLAN. */
3439 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) {
3440 w = sc->vtnet_vlan_filter[i];
3442 while ((bit = ffs(w) - 1) != -1) {
3444 tag = sizeof(w) * CHAR_BIT * i + bit;
3446 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
3447 device_printf(sc->vtnet_dev,
3448 "cannot enable VLAN %d filter\n", tag);
3455 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3460 ifp = sc->vtnet_ifp;
3461 idx = (tag >> 5) & 0x7F;
3464 if (tag == 0 || tag > 4095)
3467 VTNET_CORE_LOCK(sc);
3470 sc->vtnet_vlan_filter[idx] |= (1 << bit);
3472 sc->vtnet_vlan_filter[idx] &= ~(1 << bit);
3474 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
3475 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
3476 device_printf(sc->vtnet_dev,
3477 "cannot %s VLAN %d %s the host filter table\n",
3478 add ? "add" : "remove", tag, add ? "to" : "from");
3481 VTNET_CORE_UNLOCK(sc);
3485 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3488 if (ifp->if_softc != arg)
3491 vtnet_update_vlan_filter(arg, 1, tag);
3495 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3498 if (ifp->if_softc != arg)
3501 vtnet_update_vlan_filter(arg, 0, tag);
3505 vtnet_is_link_up(struct vtnet_softc *sc)
3511 dev = sc->vtnet_dev;
3512 ifp = sc->vtnet_ifp;
3514 if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0)
3515 status = VIRTIO_NET_S_LINK_UP;
3517 status = virtio_read_dev_config_2(dev,
3518 offsetof(struct virtio_net_config, status));
3520 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
3524 vtnet_update_link_status(struct vtnet_softc *sc)
3529 ifp = sc->vtnet_ifp;
3531 VTNET_CORE_LOCK_ASSERT(sc);
3532 link = vtnet_is_link_up(sc);
3534 /* Notify if the link status has changed. */
3535 if (link != 0 && sc->vtnet_link_active == 0) {
3536 sc->vtnet_link_active = 1;
3537 if_link_state_change(ifp, LINK_STATE_UP);
3538 } else if (link == 0 && sc->vtnet_link_active != 0) {
3539 sc->vtnet_link_active = 0;
3540 if_link_state_change(ifp, LINK_STATE_DOWN);
3545 vtnet_ifmedia_upd(struct ifnet *ifp)
3547 struct vtnet_softc *sc;
3548 struct ifmedia *ifm;
3551 ifm = &sc->vtnet_media;
3553 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3560 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3562 struct vtnet_softc *sc;
3566 ifmr->ifm_status = IFM_AVALID;
3567 ifmr->ifm_active = IFM_ETHER;
3569 VTNET_CORE_LOCK(sc);
3570 if (vtnet_is_link_up(sc) != 0) {
3571 ifmr->ifm_status |= IFM_ACTIVE;
3572 ifmr->ifm_active |= VTNET_MEDIATYPE;
3574 ifmr->ifm_active |= IFM_NONE;
3575 VTNET_CORE_UNLOCK(sc);
3579 vtnet_set_hwaddr(struct vtnet_softc *sc)
3584 dev = sc->vtnet_dev;
3586 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) {
3587 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
3588 device_printf(dev, "unable to set MAC address\n");
3589 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
3590 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3591 virtio_write_dev_config_1(dev,
3592 offsetof(struct virtio_net_config, mac) + i,
3593 sc->vtnet_hwaddr[i]);
3599 vtnet_get_hwaddr(struct vtnet_softc *sc)
3604 dev = sc->vtnet_dev;
3606 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
3608 * Generate a random locally administered unicast address.
3610 * It would be nice to generate the same MAC address across
3611 * reboots, but it seems all the hosts currently available
3612 * support the MAC feature, so this isn't too important.
3614 sc->vtnet_hwaddr[0] = 0xB2;
3615 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
3616 vtnet_set_hwaddr(sc);
3620 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3621 sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev,
3622 offsetof(struct virtio_net_config, mac) + i);
3627 vtnet_vlan_tag_remove(struct mbuf *m)
3629 struct ether_vlan_header *evh;
3631 evh = mtod(m, struct ether_vlan_header *);
3632 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
3633 m->m_flags |= M_VLANTAG;
3635 /* Strip the 802.1Q header. */
3636 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
3637 ETHER_HDR_LEN - ETHER_TYPE_LEN);
3638 m_adj(m, ETHER_VLAN_ENCAP_LEN);
3642 vtnet_set_rx_process_limit(struct vtnet_softc *sc)
3646 limit = vtnet_tunable_int(sc, "rx_process_limit",
3647 vtnet_rx_process_limit);
3650 sc->vtnet_rx_process_limit = limit;
3654 vtnet_set_tx_intr_threshold(struct vtnet_softc *sc)
3659 dev = sc->vtnet_dev;
3660 size = virtqueue_size(sc->vtnet_txqs[0].vtntx_vq);
3663 * The Tx interrupt is disabled until the queue free count falls
3664 * below our threshold. Completed frames are drained from the Tx
3665 * virtqueue before transmitting new frames and in the watchdog
3666 * callout, so the frequency of Tx interrupts is greatly reduced,
3667 * at the cost of not freeing mbufs as quickly as they otherwise
3670 * N.B. We assume all the Tx queues are the same size.
3675 * Without indirect descriptors, leave enough room for the most
3676 * segments we handle.
3678 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 &&
3679 thresh < sc->vtnet_tx_nsegs)
3680 thresh = sc->vtnet_tx_nsegs;
3682 sc->vtnet_tx_intr_thresh = thresh;
3686 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx,
3687 struct sysctl_oid_list *child, struct vtnet_rxq *rxq)
3689 struct sysctl_oid *node;
3690 struct sysctl_oid_list *list;
3691 struct vtnet_rxq_stats *stats;
3694 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id);
3695 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3696 CTLFLAG_RD, NULL, "Receive Queue");
3697 list = SYSCTL_CHILDREN(node);
3699 stats = &rxq->vtnrx_stats;
3701 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD,
3702 &stats->vrxs_ipackets, "Receive packets");
3703 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD,
3704 &stats->vrxs_ibytes, "Receive bytes");
3705 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD,
3706 &stats->vrxs_iqdrops, "Receive drops");
3707 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD,
3708 &stats->vrxs_ierrors, "Receive errors");
3709 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3710 &stats->vrxs_csum, "Receive checksum offloaded");
3711 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD,
3712 &stats->vrxs_csum_failed, "Receive checksum offload failed");
3713 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3714 &stats->vrxs_rescheduled,
3715 "Receive interrupt handler rescheduled");
3719 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx,
3720 struct sysctl_oid_list *child, struct vtnet_txq *txq)
3722 struct sysctl_oid *node;
3723 struct sysctl_oid_list *list;
3724 struct vtnet_txq_stats *stats;
3727 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id);
3728 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3729 CTLFLAG_RD, NULL, "Transmit Queue");
3730 list = SYSCTL_CHILDREN(node);
3732 stats = &txq->vtntx_stats;
3734 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD,
3735 &stats->vtxs_opackets, "Transmit packets");
3736 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD,
3737 &stats->vtxs_obytes, "Transmit bytes");
3738 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD,
3739 &stats->vtxs_omcasts, "Transmit multicasts");
3740 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3741 &stats->vtxs_csum, "Transmit checksum offloaded");
3742 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD,
3743 &stats->vtxs_tso, "Transmit segmentation offloaded");
3744 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3745 &stats->vtxs_rescheduled,
3746 "Transmit interrupt handler rescheduled");
3750 vtnet_setup_queue_sysctl(struct vtnet_softc *sc)
3753 struct sysctl_ctx_list *ctx;
3754 struct sysctl_oid *tree;
3755 struct sysctl_oid_list *child;
3758 dev = sc->vtnet_dev;
3759 ctx = device_get_sysctl_ctx(dev);
3760 tree = device_get_sysctl_tree(dev);
3761 child = SYSCTL_CHILDREN(tree);
3763 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3764 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]);
3765 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]);
3770 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx,
3771 struct sysctl_oid_list *child, struct vtnet_softc *sc)
3773 struct vtnet_statistics *stats;
3775 stats = &sc->vtnet_stats;
3777 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
3778 CTLFLAG_RD, &stats->mbuf_alloc_failed,
3779 "Mbuf cluster allocation failures");
3781 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
3782 CTLFLAG_RD, &stats->rx_frame_too_large,
3783 "Received frame larger than the mbuf chain");
3784 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
3785 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
3786 "Enqueuing the replacement receive mbuf failed");
3787 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
3788 CTLFLAG_RD, &stats->rx_mergeable_failed,
3789 "Mergeable buffers receive failures");
3790 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
3791 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
3792 "Received checksum offloaded buffer with unsupported "
3794 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
3795 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
3796 "Received checksum offloaded buffer with incorrect IP protocol");
3797 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
3798 CTLFLAG_RD, &stats->rx_csum_bad_offset,
3799 "Received checksum offloaded buffer with incorrect offset");
3800 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto",
3801 CTLFLAG_RD, &stats->rx_csum_bad_proto,
3802 "Received checksum offloaded buffer with incorrect protocol");
3803 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
3804 CTLFLAG_RD, &stats->rx_csum_failed,
3805 "Received buffer checksum offload failed");
3806 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
3807 CTLFLAG_RD, &stats->rx_csum_offloaded,
3808 "Received buffer checksum offload succeeded");
3809 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
3810 CTLFLAG_RD, &stats->rx_task_rescheduled,
3811 "Times the receive interrupt task rescheduled itself");
3813 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
3814 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
3815 "Aborted transmit of checksum offloaded buffer with unknown "
3817 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
3818 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
3819 "Aborted transmit of TSO buffer with unknown Ethernet type");
3820 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp",
3821 CTLFLAG_RD, &stats->tx_tso_not_tcp,
3822 "Aborted transmit of TSO buffer with non TCP protocol");
3823 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
3824 CTLFLAG_RD, &stats->tx_defragged,
3825 "Transmit mbufs defragged");
3826 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
3827 CTLFLAG_RD, &stats->tx_defrag_failed,
3828 "Aborted transmit of buffer because defrag failed");
3829 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
3830 CTLFLAG_RD, &stats->tx_csum_offloaded,
3831 "Offloaded checksum of transmitted buffer");
3832 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
3833 CTLFLAG_RD, &stats->tx_tso_offloaded,
3834 "Segmentation offload of transmitted buffer");
3835 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
3836 CTLFLAG_RD, &stats->tx_task_rescheduled,
3837 "Times the transmit interrupt task rescheduled itself");
3841 vtnet_setup_sysctl(struct vtnet_softc *sc)
3844 struct sysctl_ctx_list *ctx;
3845 struct sysctl_oid *tree;
3846 struct sysctl_oid_list *child;
3848 dev = sc->vtnet_dev;
3849 ctx = device_get_sysctl_ctx(dev);
3850 tree = device_get_sysctl_tree(dev);
3851 child = SYSCTL_CHILDREN(tree);
3853 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs",
3854 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0,
3855 "Maximum number of supported virtqueue pairs");
3856 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs",
3857 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0,
3858 "Number of active virtqueue pairs");
3860 vtnet_setup_stat_sysctl(ctx, child, sc);
3864 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq)
3867 return (virtqueue_enable_intr(rxq->vtnrx_vq));
3871 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq)
3874 virtqueue_disable_intr(rxq->vtnrx_vq);
3878 vtnet_txq_enable_intr(struct vtnet_txq *txq)
3880 struct virtqueue *vq;
3884 if (vtnet_txq_below_threshold(txq) != 0)
3885 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG));
3888 * The free count is above our threshold. Keep the Tx interrupt
3889 * disabled until the queue is fuller.
3895 vtnet_txq_disable_intr(struct vtnet_txq *txq)
3898 virtqueue_disable_intr(txq->vtntx_vq);
3902 vtnet_enable_rx_interrupts(struct vtnet_softc *sc)
3906 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3907 vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]);
3911 vtnet_enable_tx_interrupts(struct vtnet_softc *sc)
3915 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3916 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]);
3920 vtnet_enable_interrupts(struct vtnet_softc *sc)
3923 vtnet_enable_rx_interrupts(sc);
3924 vtnet_enable_tx_interrupts(sc);
3928 vtnet_disable_rx_interrupts(struct vtnet_softc *sc)
3932 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3933 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]);
3937 vtnet_disable_tx_interrupts(struct vtnet_softc *sc)
3941 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3942 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]);
3946 vtnet_disable_interrupts(struct vtnet_softc *sc)
3949 vtnet_disable_rx_interrupts(sc);
3950 vtnet_disable_tx_interrupts(sc);
3954 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def)
3958 snprintf(path, sizeof(path),
3959 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob);
3960 TUNABLE_INT_FETCH(path, &def);