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);
1085 vtnet_init_locked(sc);
1088 sc->vtnet_if_flags = ifp->if_flags;
1089 VTNET_CORE_UNLOCK(sc);
1094 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
1096 VTNET_CORE_LOCK(sc);
1097 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1098 vtnet_rx_filter_mac(sc);
1099 VTNET_CORE_UNLOCK(sc);
1104 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1108 VTNET_CORE_LOCK(sc);
1109 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1111 if (mask & IFCAP_TXCSUM)
1112 ifp->if_capenable ^= IFCAP_TXCSUM;
1113 if (mask & IFCAP_TXCSUM_IPV6)
1114 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
1115 if (mask & IFCAP_TSO4)
1116 ifp->if_capenable ^= IFCAP_TSO4;
1117 if (mask & IFCAP_TSO6)
1118 ifp->if_capenable ^= IFCAP_TSO6;
1120 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO |
1121 IFCAP_VLAN_HWFILTER)) {
1122 /* These Rx features require us to renegotiate. */
1125 if (mask & IFCAP_RXCSUM)
1126 ifp->if_capenable ^= IFCAP_RXCSUM;
1127 if (mask & IFCAP_RXCSUM_IPV6)
1128 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
1129 if (mask & IFCAP_LRO)
1130 ifp->if_capenable ^= IFCAP_LRO;
1131 if (mask & IFCAP_VLAN_HWFILTER)
1132 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1136 if (mask & IFCAP_VLAN_HWTSO)
1137 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1138 if (mask & IFCAP_VLAN_HWTAGGING)
1139 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1141 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1142 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1143 vtnet_init_locked(sc);
1146 VTNET_CORE_UNLOCK(sc);
1147 VLAN_CAPABILITIES(ifp);
1152 error = ether_ioctl(ifp, cmd, data);
1156 VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc);
1162 vtnet_rxq_populate(struct vtnet_rxq *rxq)
1164 struct virtqueue *vq;
1170 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1171 error = vtnet_rxq_new_buf(rxq);
1177 virtqueue_notify(vq);
1179 * EMSGSIZE signifies the virtqueue did not have enough
1180 * entries available to hold the last mbuf. This is not
1183 if (error == EMSGSIZE)
1191 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq)
1193 struct virtqueue *vq;
1200 while ((m = virtqueue_drain(vq, &last)) != NULL)
1203 KASSERT(virtqueue_empty(vq),
1204 ("%s: mbufs remaining in rx queue %p", __func__, rxq));
1207 static struct mbuf *
1208 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1210 struct mbuf *m_head, *m_tail, *m;
1213 clsize = sc->vtnet_rx_clsize;
1215 KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1216 ("%s: chained mbuf %d request without LRO_NOMRG", __func__, nbufs));
1218 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize);
1222 m_head->m_len = clsize;
1225 /* Allocate the rest of the chain. */
1226 for (i = 1; i < nbufs; i++) {
1227 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize);
1236 if (m_tailp != NULL)
1242 sc->vtnet_stats.mbuf_alloc_failed++;
1249 * Slow path for when LRO without mergeable buffers is negotiated.
1252 vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *rxq, struct mbuf *m0,
1255 struct vtnet_softc *sc;
1256 struct mbuf *m, *m_prev;
1257 struct mbuf *m_new, *m_tail;
1258 int len, clsize, nreplace, error;
1261 clsize = sc->vtnet_rx_clsize;
1271 * Since these mbuf chains are so large, we avoid allocating an
1272 * entire replacement chain if possible. When the received frame
1273 * did not consume the entire chain, the unused mbufs are moved
1274 * to the replacement chain.
1278 * Something is seriously wrong if we received a frame
1279 * larger than the chain. Drop it.
1282 sc->vtnet_stats.rx_frame_too_large++;
1286 /* We always allocate the same cluster size. */
1287 KASSERT(m->m_len == clsize,
1288 ("%s: mbuf size %d is not the cluster size %d",
1289 __func__, m->m_len, clsize));
1291 m->m_len = MIN(m->m_len, len);
1299 KASSERT(nreplace <= sc->vtnet_rx_nmbufs,
1300 ("%s: too many replacement mbufs %d max %d", __func__, nreplace,
1301 sc->vtnet_rx_nmbufs));
1303 m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail);
1304 if (m_new == NULL) {
1305 m_prev->m_len = clsize;
1310 * Move any unused mbufs from the received chain onto the end
1313 if (m_prev->m_next != NULL) {
1314 m_tail->m_next = m_prev->m_next;
1315 m_prev->m_next = NULL;
1318 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1321 * BAD! We could not enqueue the replacement mbuf chain. We
1322 * must restore the m0 chain to the original state if it was
1323 * modified so we can subsequently discard it.
1325 * NOTE: The replacement is suppose to be an identical copy
1326 * to the one just dequeued so this is an unexpected error.
1328 sc->vtnet_stats.rx_enq_replacement_failed++;
1330 if (m_tail->m_next != NULL) {
1331 m_prev->m_next = m_tail->m_next;
1332 m_tail->m_next = NULL;
1335 m_prev->m_len = clsize;
1343 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len)
1345 struct vtnet_softc *sc;
1351 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1352 ("%s: chained mbuf without LRO_NOMRG", __func__));
1354 if (m->m_next == NULL) {
1355 /* Fast-path for the common case of just one mbuf. */
1359 m_new = vtnet_rx_alloc_buf(sc, 1, NULL);
1363 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1366 * The new mbuf is suppose to be an identical
1367 * copy of the one just dequeued so this is an
1371 sc->vtnet_stats.rx_enq_replacement_failed++;
1375 error = vtnet_rxq_replace_lro_nomgr_buf(rxq, m, len);
1381 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1383 struct vtnet_softc *sc;
1385 struct vtnet_rx_header *rxhdr;
1391 mdata = mtod(m, uint8_t *);
1393 VTNET_RXQ_LOCK_ASSERT(rxq);
1394 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1395 ("%s: chained mbuf without LRO_NOMRG", __func__));
1396 KASSERT(m->m_len == sc->vtnet_rx_clsize,
1397 ("%s: unexpected cluster size %d/%d", __func__, m->m_len,
1398 sc->vtnet_rx_clsize));
1401 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1402 MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr));
1403 rxhdr = (struct vtnet_rx_header *) mdata;
1404 sglist_append(sg, &rxhdr->vrh_hdr, sc->vtnet_hdr_size);
1405 offset = sizeof(struct vtnet_rx_header);
1409 sglist_append(sg, mdata + offset, m->m_len - offset);
1410 if (m->m_next != NULL) {
1411 error = sglist_append_mbuf(sg, m->m_next);
1415 error = virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg);
1421 vtnet_rxq_new_buf(struct vtnet_rxq *rxq)
1423 struct vtnet_softc *sc;
1429 m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL);
1433 error = vtnet_rxq_enqueue_buf(rxq, m);
1441 * Use the checksum offset in the VirtIO header to set the
1442 * correct CSUM_* flags.
1445 vtnet_rxq_csum_by_offset(struct vtnet_rxq *rxq, struct mbuf *m,
1446 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1448 struct vtnet_softc *sc;
1449 #if defined(INET) || defined(INET6)
1450 int offset = hdr->csum_start + hdr->csum_offset;
1455 /* Only do a basic sanity check on the offset. */
1459 if (__predict_false(offset < ip_start + sizeof(struct ip)))
1464 case ETHERTYPE_IPV6:
1465 if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
1470 sc->vtnet_stats.rx_csum_bad_ethtype++;
1475 * Use the offset to determine the appropriate CSUM_* flags. This is
1476 * a bit dirty, but we can get by with it since the checksum offsets
1477 * happen to be different. We assume the host host does not do IPv4
1478 * header checksum offloading.
1480 switch (hdr->csum_offset) {
1481 case offsetof(struct udphdr, uh_sum):
1482 case offsetof(struct tcphdr, th_sum):
1483 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1484 m->m_pkthdr.csum_data = 0xFFFF;
1486 case offsetof(struct sctphdr, checksum):
1487 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1490 sc->vtnet_stats.rx_csum_bad_offset++;
1498 vtnet_rxq_csum_by_parse(struct vtnet_rxq *rxq, struct mbuf *m,
1499 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1501 struct vtnet_softc *sc;
1508 case ETHERTYPE_IP: {
1510 if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
1512 ip = (struct ip *)(m->m_data + ip_start);
1514 offset = ip_start + (ip->ip_hl << 2);
1519 case ETHERTYPE_IPV6:
1520 if (__predict_false(m->m_len < ip_start +
1521 sizeof(struct ip6_hdr)))
1523 offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
1524 if (__predict_false(offset < 0))
1529 sc->vtnet_stats.rx_csum_bad_ethtype++;
1535 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
1537 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1538 m->m_pkthdr.csum_data = 0xFFFF;
1541 if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
1543 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1544 m->m_pkthdr.csum_data = 0xFFFF;
1547 if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
1549 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1553 * For the remaining protocols, FreeBSD does not support
1554 * checksum offloading, so the checksum will be recomputed.
1557 if_printf(sc->vtnet_ifp, "cksum offload of unsupported "
1558 "protocol eth_type=%#x proto=%d csum_start=%d "
1559 "csum_offset=%d\n", __func__, eth_type, proto,
1560 hdr->csum_start, hdr->csum_offset);
1569 * Set the appropriate CSUM_* flags. Unfortunately, the information
1570 * provided is not directly useful to us. The VirtIO header gives the
1571 * offset of the checksum, which is all Linux needs, but this is not
1572 * how FreeBSD does things. We are forced to peek inside the packet
1575 * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
1576 * could accept the offsets and let the stack figure it out.
1579 vtnet_rxq_csum(struct vtnet_rxq *rxq, struct mbuf *m,
1580 struct virtio_net_hdr *hdr)
1582 struct ether_header *eh;
1583 struct ether_vlan_header *evh;
1587 eh = mtod(m, struct ether_header *);
1588 eth_type = ntohs(eh->ether_type);
1589 if (eth_type == ETHERTYPE_VLAN) {
1590 /* BMV: We should handle nested VLAN tags too. */
1591 evh = mtod(m, struct ether_vlan_header *);
1592 eth_type = ntohs(evh->evl_proto);
1593 offset = sizeof(struct ether_vlan_header);
1595 offset = sizeof(struct ether_header);
1597 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1598 error = vtnet_rxq_csum_by_offset(rxq, m, eth_type, offset, hdr);
1600 error = vtnet_rxq_csum_by_parse(rxq, m, eth_type, offset, hdr);
1606 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs)
1610 while (--nbufs > 0) {
1611 m = virtqueue_dequeue(rxq->vtnrx_vq, NULL);
1614 vtnet_rxq_discard_buf(rxq, m);
1619 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1624 * Requeue the discarded mbuf. This should always be successful
1625 * since it was just dequeued.
1627 error = vtnet_rxq_enqueue_buf(rxq, m);
1629 ("%s: cannot requeue discarded mbuf %d", __func__, error));
1633 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs)
1635 struct vtnet_softc *sc;
1637 struct virtqueue *vq;
1638 struct mbuf *m, *m_tail;
1643 ifp = sc->vtnet_ifp;
1646 while (--nbufs > 0) {
1647 m = virtqueue_dequeue(vq, &len);
1649 rxq->vtnrx_stats.vrxs_ierrors++;
1653 if (vtnet_rxq_new_buf(rxq) != 0) {
1654 rxq->vtnrx_stats.vrxs_iqdrops++;
1655 vtnet_rxq_discard_buf(rxq, m);
1657 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1665 m->m_flags &= ~M_PKTHDR;
1667 m_head->m_pkthdr.len += len;
1675 sc->vtnet_stats.rx_mergeable_failed++;
1682 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m,
1683 struct virtio_net_hdr *hdr)
1685 struct vtnet_softc *sc;
1687 struct ether_header *eh;
1690 ifp = sc->vtnet_ifp;
1692 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1693 eh = mtod(m, struct ether_header *);
1694 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1695 vtnet_vlan_tag_remove(m);
1697 * With the 802.1Q header removed, update the
1698 * checksum starting location accordingly.
1700 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1701 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN;
1705 m->m_pkthdr.flowid = rxq->vtnrx_id;
1706 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
1709 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum
1710 * distinction that Linux does. Need to reevaluate if performing
1711 * offloading for the NEEDS_CSUM case is really appropriate.
1713 if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM |
1714 VIRTIO_NET_HDR_F_DATA_VALID)) {
1715 if (vtnet_rxq_csum(rxq, m, hdr) == 0)
1716 rxq->vtnrx_stats.vrxs_csum++;
1718 rxq->vtnrx_stats.vrxs_csum_failed++;
1721 rxq->vtnrx_stats.vrxs_ipackets++;
1722 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len;
1724 VTNET_RXQ_UNLOCK(rxq);
1725 (*ifp->if_input)(ifp, m);
1726 VTNET_RXQ_LOCK(rxq);
1730 vtnet_rxq_eof(struct vtnet_rxq *rxq)
1732 struct virtio_net_hdr lhdr, *hdr;
1733 struct vtnet_softc *sc;
1735 struct virtqueue *vq;
1737 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1738 int len, deq, nbufs, adjsz, count;
1742 ifp = sc->vtnet_ifp;
1745 count = sc->vtnet_rx_process_limit;
1747 VTNET_RXQ_LOCK_ASSERT(rxq);
1750 if (netmap_rx_irq(ifp, 0, &deq)) {
1753 #endif /* DEV_NETMAP */
1755 while (count-- > 0) {
1756 m = virtqueue_dequeue(vq, &len);
1761 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1762 rxq->vtnrx_stats.vrxs_ierrors++;
1763 vtnet_rxq_discard_buf(rxq, m);
1767 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1769 adjsz = sizeof(struct vtnet_rx_header);
1771 * Account for our pad inserted between the header
1772 * and the actual start of the frame.
1774 len += VTNET_RX_HEADER_PAD;
1776 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1777 nbufs = mhdr->num_buffers;
1778 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1781 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) {
1782 rxq->vtnrx_stats.vrxs_iqdrops++;
1783 vtnet_rxq_discard_buf(rxq, m);
1785 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1789 m->m_pkthdr.len = len;
1790 m->m_pkthdr.rcvif = ifp;
1791 m->m_pkthdr.csum_flags = 0;
1794 /* Dequeue the rest of chain. */
1795 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0)
1800 * Save copy of header before we strip it. For both mergeable
1801 * and non-mergeable, the header is at the beginning of the
1802 * mbuf data. We no longer need num_buffers, so always use a
1805 * BMV: Is this memcpy() expensive? We know the mbuf data is
1806 * still valid even after the m_adj().
1808 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1811 vtnet_rxq_input(rxq, m, hdr);
1813 /* Must recheck after dropping the Rx lock. */
1814 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1819 virtqueue_notify(vq);
1821 return (count > 0 ? 0 : EAGAIN);
1825 vtnet_rx_vq_intr(void *xrxq)
1827 struct vtnet_softc *sc;
1828 struct vtnet_rxq *rxq;
1834 ifp = sc->vtnet_ifp;
1837 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) {
1839 * Ignore this interrupt. Either this is a spurious interrupt
1840 * or multiqueue without per-VQ MSIX so every queue needs to
1841 * be polled (a brain dead configuration we could try harder
1844 vtnet_rxq_disable_intr(rxq);
1848 VTNET_RXQ_LOCK(rxq);
1851 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1852 VTNET_RXQ_UNLOCK(rxq);
1856 more = vtnet_rxq_eof(rxq);
1857 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1859 vtnet_rxq_disable_intr(rxq);
1861 * This is an occasional condition or race (when !more),
1862 * so retry a few times before scheduling the taskqueue.
1864 if (tries++ < VTNET_INTR_DISABLE_RETRIES)
1867 VTNET_RXQ_UNLOCK(rxq);
1868 rxq->vtnrx_stats.vrxs_rescheduled++;
1869 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1871 VTNET_RXQ_UNLOCK(rxq);
1875 vtnet_rxq_tq_intr(void *xrxq, int pending)
1877 struct vtnet_softc *sc;
1878 struct vtnet_rxq *rxq;
1884 ifp = sc->vtnet_ifp;
1886 VTNET_RXQ_LOCK(rxq);
1888 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1889 VTNET_RXQ_UNLOCK(rxq);
1893 more = vtnet_rxq_eof(rxq);
1894 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1896 vtnet_rxq_disable_intr(rxq);
1897 rxq->vtnrx_stats.vrxs_rescheduled++;
1898 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1901 VTNET_RXQ_UNLOCK(rxq);
1905 vtnet_txq_below_threshold(struct vtnet_txq *txq)
1907 struct vtnet_softc *sc;
1908 struct virtqueue *vq;
1913 return (virtqueue_nfree(vq) <= sc->vtnet_tx_intr_thresh);
1917 vtnet_txq_notify(struct vtnet_txq *txq)
1919 struct virtqueue *vq;
1923 txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
1924 virtqueue_notify(vq);
1926 if (vtnet_txq_enable_intr(txq) == 0)
1930 * Drain frames that were completed since last checked. If this
1931 * causes the queue to go above the threshold, the caller should
1932 * continue transmitting.
1934 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) {
1935 virtqueue_disable_intr(vq);
1943 vtnet_txq_free_mbufs(struct vtnet_txq *txq)
1945 struct virtqueue *vq;
1946 struct vtnet_tx_header *txhdr;
1952 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1953 m_freem(txhdr->vth_mbuf);
1954 uma_zfree(vtnet_tx_header_zone, txhdr);
1957 KASSERT(virtqueue_empty(vq),
1958 ("%s: mbufs remaining in tx queue %p", __func__, txq));
1962 * BMV: Much of this can go away once we finally have offsets in
1963 * the mbuf packet header. Bug andre@.
1966 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m,
1967 int *etype, int *proto, int *start)
1969 struct vtnet_softc *sc;
1970 struct ether_vlan_header *evh;
1975 evh = mtod(m, struct ether_vlan_header *);
1976 if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1977 /* BMV: We should handle nested VLAN tags too. */
1978 *etype = ntohs(evh->evl_proto);
1979 offset = sizeof(struct ether_vlan_header);
1981 *etype = ntohs(evh->evl_encap_proto);
1982 offset = sizeof(struct ether_header);
1987 case ETHERTYPE_IP: {
1988 struct ip *ip, iphdr;
1989 if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
1990 m_copydata(m, offset, sizeof(struct ip),
1994 ip = (struct ip *)(m->m_data + offset);
1996 *start = offset + (ip->ip_hl << 2);
2001 case ETHERTYPE_IPV6:
2003 *start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
2004 /* Assert the network stack sent us a valid packet. */
2005 KASSERT(*start > offset,
2006 ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
2007 *start, offset, *proto));
2011 sc->vtnet_stats.tx_csum_bad_ethtype++;
2019 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type,
2020 int offset, struct virtio_net_hdr *hdr)
2022 static struct timeval lastecn;
2024 struct vtnet_softc *sc;
2025 struct tcphdr *tcp, tcphdr;
2029 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
2030 m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
2033 tcp = (struct tcphdr *)(m->m_data + offset);
2035 hdr->hdr_len = offset + (tcp->th_off << 2);
2036 hdr->gso_size = m->m_pkthdr.tso_segsz;
2037 hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
2038 VIRTIO_NET_HDR_GSO_TCPV6;
2040 if (tcp->th_flags & TH_CWR) {
2042 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
2043 * ECN support is not on a per-interface basis, but globally via
2044 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
2046 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
2047 if (ppsratecheck(&lastecn, &curecn, 1))
2048 if_printf(sc->vtnet_ifp,
2049 "TSO with ECN not negotiated with host\n");
2052 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2055 txq->vtntx_stats.vtxs_tso++;
2060 static struct mbuf *
2061 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m,
2062 struct virtio_net_hdr *hdr)
2064 struct vtnet_softc *sc;
2065 int flags, etype, csum_start, proto, error;
2068 flags = m->m_pkthdr.csum_flags;
2070 error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start);
2074 if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) ||
2075 (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) {
2077 * We could compare the IP protocol vs the CSUM_ flag too,
2078 * but that really should not be necessary.
2080 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
2081 hdr->csum_start = csum_start;
2082 hdr->csum_offset = m->m_pkthdr.csum_data;
2083 txq->vtntx_stats.vtxs_csum++;
2086 if (flags & CSUM_TSO) {
2087 if (__predict_false(proto != IPPROTO_TCP)) {
2088 /* Likely failed to correctly parse the mbuf. */
2089 sc->vtnet_stats.tx_tso_not_tcp++;
2093 KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
2094 ("%s: mbuf %p TSO without checksum offload %#x",
2095 __func__, m, flags));
2097 error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr);
2110 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head,
2111 struct vtnet_tx_header *txhdr)
2113 struct vtnet_softc *sc;
2114 struct virtqueue *vq;
2125 error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
2126 KASSERT(error == 0 && sg->sg_nseg == 1,
2127 ("%s: error %d adding header to sglist", __func__, error));
2129 error = sglist_append_mbuf(sg, m);
2131 m = m_defrag(m, M_NOWAIT);
2136 sc->vtnet_stats.tx_defragged++;
2138 error = sglist_append_mbuf(sg, m);
2143 txhdr->vth_mbuf = m;
2144 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0);
2149 sc->vtnet_stats.tx_defrag_failed++;
2157 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head)
2159 struct vtnet_tx_header *txhdr;
2160 struct virtio_net_hdr *hdr;
2167 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO);
2168 if (txhdr == NULL) {
2175 * Always use the non-mergeable header, regardless if the feature
2176 * was negotiated. For transmit, num_buffers is always zero. The
2177 * vtnet_hdr_size is used to enqueue the correct header size.
2179 hdr = &txhdr->vth_uhdr.hdr;
2181 if (m->m_flags & M_VLANTAG) {
2182 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2183 if ((*m_head = m) == NULL) {
2187 m->m_flags &= ~M_VLANTAG;
2190 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) {
2191 m = vtnet_txq_offload(txq, m, hdr);
2192 if ((*m_head = m) == NULL) {
2198 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr);
2203 uma_zfree(vtnet_tx_header_zone, txhdr);
2208 #ifdef VTNET_LEGACY_TX
2211 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp)
2213 struct vtnet_softc *sc;
2214 struct virtqueue *vq;
2222 VTNET_TXQ_LOCK_ASSERT(txq);
2224 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2225 sc->vtnet_link_active == 0)
2233 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2234 if (virtqueue_full(vq))
2237 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2241 if (vtnet_txq_encap(txq, &m0) != 0) {
2243 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2248 ETHER_BPF_MTAP(ifp, m0);
2251 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2252 if (tries++ < VTNET_NOTIFY_RETRIES)
2255 txq->vtntx_stats.vtxs_rescheduled++;
2256 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2261 vtnet_start(struct ifnet *ifp)
2263 struct vtnet_softc *sc;
2264 struct vtnet_txq *txq;
2267 txq = &sc->vtnet_txqs[0];
2269 VTNET_TXQ_LOCK(txq);
2270 vtnet_start_locked(txq, ifp);
2271 VTNET_TXQ_UNLOCK(txq);
2274 #else /* !VTNET_LEGACY_TX */
2277 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m)
2279 struct vtnet_softc *sc;
2280 struct virtqueue *vq;
2281 struct buf_ring *br;
2283 int enq, tries, error;
2288 ifp = sc->vtnet_ifp;
2292 VTNET_TXQ_LOCK_ASSERT(txq);
2294 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2295 sc->vtnet_link_active == 0) {
2297 error = drbr_enqueue(ifp, br, m);
2302 error = drbr_enqueue(ifp, br, m);
2312 while ((m = drbr_peek(ifp, br)) != NULL) {
2313 if (virtqueue_full(vq)) {
2314 drbr_putback(ifp, br, m);
2318 if (vtnet_txq_encap(txq, &m) != 0) {
2320 drbr_putback(ifp, br, m);
2322 drbr_advance(ifp, br);
2325 drbr_advance(ifp, br);
2328 ETHER_BPF_MTAP(ifp, m);
2331 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2332 if (tries++ < VTNET_NOTIFY_RETRIES)
2335 txq->vtntx_stats.vtxs_rescheduled++;
2336 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2343 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m)
2345 struct vtnet_softc *sc;
2346 struct vtnet_txq *txq;
2347 int i, npairs, error;
2350 npairs = sc->vtnet_act_vq_pairs;
2352 /* check if flowid is set */
2353 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2354 i = m->m_pkthdr.flowid % npairs;
2356 i = curcpu % npairs;
2358 txq = &sc->vtnet_txqs[i];
2360 if (VTNET_TXQ_TRYLOCK(txq) != 0) {
2361 error = vtnet_txq_mq_start_locked(txq, m);
2362 VTNET_TXQ_UNLOCK(txq);
2364 error = drbr_enqueue(ifp, txq->vtntx_br, m);
2365 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask);
2372 vtnet_txq_tq_deferred(void *xtxq, int pending)
2374 struct vtnet_softc *sc;
2375 struct vtnet_txq *txq;
2380 VTNET_TXQ_LOCK(txq);
2381 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br))
2382 vtnet_txq_mq_start_locked(txq, NULL);
2383 VTNET_TXQ_UNLOCK(txq);
2386 #endif /* VTNET_LEGACY_TX */
2389 vtnet_txq_start(struct vtnet_txq *txq)
2391 struct vtnet_softc *sc;
2395 ifp = sc->vtnet_ifp;
2397 #ifdef VTNET_LEGACY_TX
2398 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2399 vtnet_start_locked(txq, ifp);
2401 if (!drbr_empty(ifp, txq->vtntx_br))
2402 vtnet_txq_mq_start_locked(txq, NULL);
2407 vtnet_txq_tq_intr(void *xtxq, int pending)
2409 struct vtnet_softc *sc;
2410 struct vtnet_txq *txq;
2415 ifp = sc->vtnet_ifp;
2417 VTNET_TXQ_LOCK(txq);
2419 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2420 VTNET_TXQ_UNLOCK(txq);
2425 vtnet_txq_start(txq);
2427 VTNET_TXQ_UNLOCK(txq);
2431 vtnet_txq_eof(struct vtnet_txq *txq)
2433 struct virtqueue *vq;
2434 struct vtnet_tx_header *txhdr;
2440 VTNET_TXQ_LOCK_ASSERT(txq);
2443 if (netmap_tx_irq(txq->vtntx_sc->vtnet_ifp, txq->vtntx_id)) {
2444 virtqueue_disable_intr(vq); // XXX luigi
2445 return 0; // XXX or 1 ?
2447 #endif /* DEV_NETMAP */
2449 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
2450 m = txhdr->vth_mbuf;
2453 txq->vtntx_stats.vtxs_opackets++;
2454 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len;
2455 if (m->m_flags & M_MCAST)
2456 txq->vtntx_stats.vtxs_omcasts++;
2459 uma_zfree(vtnet_tx_header_zone, txhdr);
2462 if (virtqueue_empty(vq))
2463 txq->vtntx_watchdog = 0;
2469 vtnet_tx_vq_intr(void *xtxq)
2471 struct vtnet_softc *sc;
2472 struct vtnet_txq *txq;
2477 ifp = sc->vtnet_ifp;
2479 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) {
2481 * Ignore this interrupt. Either this is a spurious interrupt
2482 * or multiqueue without per-VQ MSIX so every queue needs to
2483 * be polled (a brain dead configuration we could try harder
2486 vtnet_txq_disable_intr(txq);
2490 VTNET_TXQ_LOCK(txq);
2492 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2493 VTNET_TXQ_UNLOCK(txq);
2498 vtnet_txq_start(txq);
2500 VTNET_TXQ_UNLOCK(txq);
2504 vtnet_tx_start_all(struct vtnet_softc *sc)
2506 struct vtnet_txq *txq;
2509 VTNET_CORE_LOCK_ASSERT(sc);
2511 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2512 txq = &sc->vtnet_txqs[i];
2514 VTNET_TXQ_LOCK(txq);
2515 vtnet_txq_start(txq);
2516 VTNET_TXQ_UNLOCK(txq);
2520 #ifndef VTNET_LEGACY_TX
2522 vtnet_qflush(struct ifnet *ifp)
2524 struct vtnet_softc *sc;
2525 struct vtnet_txq *txq;
2531 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2532 txq = &sc->vtnet_txqs[i];
2534 VTNET_TXQ_LOCK(txq);
2535 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL)
2537 VTNET_TXQ_UNLOCK(txq);
2545 vtnet_watchdog(struct vtnet_txq *txq)
2549 ifp = txq->vtntx_sc->vtnet_ifp;
2551 VTNET_TXQ_LOCK(txq);
2552 if (txq->vtntx_watchdog == 1) {
2554 * Only drain completed frames if the watchdog is about to
2555 * expire. If any frames were drained, there may be enough
2556 * free descriptors now available to transmit queued frames.
2557 * In that case, the timer will immediately be decremented
2558 * below, but the timeout is generous enough that should not
2561 if (vtnet_txq_eof(txq) != 0)
2562 vtnet_txq_start(txq);
2565 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) {
2566 VTNET_TXQ_UNLOCK(txq);
2569 VTNET_TXQ_UNLOCK(txq);
2571 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id);
2576 vtnet_rxq_accum_stats(struct vtnet_rxq *rxq, struct vtnet_rxq_stats *accum)
2578 struct vtnet_rxq_stats *st;
2580 st = &rxq->vtnrx_stats;
2582 accum->vrxs_ipackets += st->vrxs_ipackets;
2583 accum->vrxs_ibytes += st->vrxs_ibytes;
2584 accum->vrxs_iqdrops += st->vrxs_iqdrops;
2585 accum->vrxs_csum += st->vrxs_csum;
2586 accum->vrxs_csum_failed += st->vrxs_csum_failed;
2587 accum->vrxs_rescheduled += st->vrxs_rescheduled;
2591 vtnet_txq_accum_stats(struct vtnet_txq *txq, struct vtnet_txq_stats *accum)
2593 struct vtnet_txq_stats *st;
2595 st = &txq->vtntx_stats;
2597 accum->vtxs_opackets += st->vtxs_opackets;
2598 accum->vtxs_obytes += st->vtxs_obytes;
2599 accum->vtxs_csum += st->vtxs_csum;
2600 accum->vtxs_tso += st->vtxs_tso;
2601 accum->vtxs_rescheduled += st->vtxs_rescheduled;
2605 vtnet_accumulate_stats(struct vtnet_softc *sc)
2608 struct vtnet_statistics *st;
2609 struct vtnet_rxq_stats rxaccum;
2610 struct vtnet_txq_stats txaccum;
2613 ifp = sc->vtnet_ifp;
2614 st = &sc->vtnet_stats;
2615 bzero(&rxaccum, sizeof(struct vtnet_rxq_stats));
2616 bzero(&txaccum, sizeof(struct vtnet_txq_stats));
2618 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2619 vtnet_rxq_accum_stats(&sc->vtnet_rxqs[i], &rxaccum);
2620 vtnet_txq_accum_stats(&sc->vtnet_txqs[i], &txaccum);
2623 st->rx_csum_offloaded = rxaccum.vrxs_csum;
2624 st->rx_csum_failed = rxaccum.vrxs_csum_failed;
2625 st->rx_task_rescheduled = rxaccum.vrxs_rescheduled;
2626 st->tx_csum_offloaded = txaccum.vtxs_csum;
2627 st->tx_tso_offloaded = txaccum.vtxs_tso;
2628 st->tx_task_rescheduled = txaccum.vtxs_rescheduled;
2631 * With the exception of if_ierrors, these ifnet statistics are
2632 * only updated in the driver, so just set them to our accumulated
2633 * values. if_ierrors is updated in ether_input() for malformed
2634 * frames that we should have already discarded.
2636 ifp->if_ipackets = rxaccum.vrxs_ipackets;
2637 ifp->if_iqdrops = rxaccum.vrxs_iqdrops;
2638 ifp->if_ierrors = rxaccum.vrxs_ierrors;
2639 ifp->if_opackets = txaccum.vtxs_opackets;
2640 #ifndef VTNET_LEGACY_TX
2641 ifp->if_obytes = txaccum.vtxs_obytes;
2642 ifp->if_omcasts = txaccum.vtxs_omcasts;
2647 vtnet_tick(void *xsc)
2649 struct vtnet_softc *sc;
2654 ifp = sc->vtnet_ifp;
2657 VTNET_CORE_LOCK_ASSERT(sc);
2658 vtnet_accumulate_stats(sc);
2660 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
2661 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]);
2663 if (timedout != 0) {
2664 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2665 vtnet_init_locked(sc);
2667 callout_schedule(&sc->vtnet_tick_ch, hz);
2671 vtnet_start_taskqueues(struct vtnet_softc *sc)
2674 struct vtnet_rxq *rxq;
2675 struct vtnet_txq *txq;
2678 dev = sc->vtnet_dev;
2681 * Errors here are very difficult to recover from - we cannot
2682 * easily fail because, if this is during boot, we will hang
2683 * when freeing any successfully started taskqueues because
2684 * the scheduler isn't up yet.
2686 * Most drivers just ignore the return value - it only fails
2687 * with ENOMEM so an error is not likely.
2689 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2690 rxq = &sc->vtnet_rxqs[i];
2691 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET,
2692 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id);
2694 device_printf(dev, "failed to start rx taskq %d\n",
2698 txq = &sc->vtnet_txqs[i];
2699 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET,
2700 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id);
2702 device_printf(dev, "failed to start tx taskq %d\n",
2709 vtnet_free_taskqueues(struct vtnet_softc *sc)
2711 struct vtnet_rxq *rxq;
2712 struct vtnet_txq *txq;
2715 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2716 rxq = &sc->vtnet_rxqs[i];
2717 if (rxq->vtnrx_tq != NULL) {
2718 taskqueue_free(rxq->vtnrx_tq);
2719 rxq->vtnrx_vq = NULL;
2722 txq = &sc->vtnet_txqs[i];
2723 if (txq->vtntx_tq != NULL) {
2724 taskqueue_free(txq->vtntx_tq);
2725 txq->vtntx_tq = NULL;
2731 vtnet_drain_taskqueues(struct vtnet_softc *sc)
2733 struct vtnet_rxq *rxq;
2734 struct vtnet_txq *txq;
2737 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2738 rxq = &sc->vtnet_rxqs[i];
2739 if (rxq->vtnrx_tq != NULL)
2740 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2742 txq = &sc->vtnet_txqs[i];
2743 if (txq->vtntx_tq != NULL) {
2744 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask);
2745 #ifndef VTNET_LEGACY_TX
2746 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask);
2753 vtnet_drain_rxtx_queues(struct vtnet_softc *sc)
2755 struct vtnet_rxq *rxq;
2756 struct vtnet_txq *txq;
2759 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2760 rxq = &sc->vtnet_rxqs[i];
2761 vtnet_rxq_free_mbufs(rxq);
2763 txq = &sc->vtnet_txqs[i];
2764 vtnet_txq_free_mbufs(txq);
2769 vtnet_stop_rendezvous(struct vtnet_softc *sc)
2771 struct vtnet_rxq *rxq;
2772 struct vtnet_txq *txq;
2776 * Lock and unlock the per-queue mutex so we known the stop
2777 * state is visible. Doing only the active queues should be
2778 * sufficient, but it does not cost much extra to do all the
2779 * queues. Note we hold the core mutex here too.
2781 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2782 rxq = &sc->vtnet_rxqs[i];
2783 VTNET_RXQ_LOCK(rxq);
2784 VTNET_RXQ_UNLOCK(rxq);
2786 txq = &sc->vtnet_txqs[i];
2787 VTNET_TXQ_LOCK(txq);
2788 VTNET_TXQ_UNLOCK(txq);
2793 vtnet_stop(struct vtnet_softc *sc)
2798 dev = sc->vtnet_dev;
2799 ifp = sc->vtnet_ifp;
2801 VTNET_CORE_LOCK_ASSERT(sc);
2803 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2804 sc->vtnet_link_active = 0;
2805 callout_stop(&sc->vtnet_tick_ch);
2807 /* Only advisory. */
2808 vtnet_disable_interrupts(sc);
2811 * Stop the host adapter. This resets it to the pre-initialized
2812 * state. It will not generate any interrupts until after it is
2816 vtnet_stop_rendezvous(sc);
2818 /* Free any mbufs left in the virtqueues. */
2819 vtnet_drain_rxtx_queues(sc);
2823 vtnet_virtio_reinit(struct vtnet_softc *sc)
2830 dev = sc->vtnet_dev;
2831 ifp = sc->vtnet_ifp;
2832 features = sc->vtnet_features;
2836 mask |= IFCAP_RXCSUM;
2839 mask |= IFCAP_RXCSUM_IPV6;
2843 * Re-negotiate with the host, removing any disabled receive
2844 * features. Transmit features are disabled only on our side
2845 * via if_capenable and if_hwassist.
2848 if (ifp->if_capabilities & mask) {
2850 * We require both IPv4 and IPv6 offloading to be enabled
2851 * in order to negotiated it: VirtIO does not distinguish
2854 if ((ifp->if_capenable & mask) != mask)
2855 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2858 if (ifp->if_capabilities & IFCAP_LRO) {
2859 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2860 features &= ~VTNET_LRO_FEATURES;
2863 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2864 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2865 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2868 error = virtio_reinit(dev, features);
2870 device_printf(dev, "virtio reinit error %d\n", error);
2876 vtnet_init_rx_filters(struct vtnet_softc *sc)
2880 ifp = sc->vtnet_ifp;
2882 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2883 /* Restore promiscuous and all-multicast modes. */
2884 vtnet_rx_filter(sc);
2885 /* Restore filtered MAC addresses. */
2886 vtnet_rx_filter_mac(sc);
2889 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2890 vtnet_rx_filter_vlan(sc);
2894 vtnet_init_rx_queues(struct vtnet_softc *sc)
2897 struct vtnet_rxq *rxq;
2898 int i, clsize, error;
2900 dev = sc->vtnet_dev;
2903 * Use the new cluster size if one has been set (via a MTU
2904 * change). Otherwise, use the standard 2K clusters.
2906 * BMV: It might make sense to use page sized clusters as
2907 * the default (depending on the features negotiated).
2909 if (sc->vtnet_rx_new_clsize != 0) {
2910 clsize = sc->vtnet_rx_new_clsize;
2911 sc->vtnet_rx_new_clsize = 0;
2915 sc->vtnet_rx_clsize = clsize;
2916 sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize);
2918 KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS ||
2919 sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs,
2920 ("%s: too many rx mbufs %d for %d segments", __func__,
2921 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs));
2924 if (vtnet_netmap_init_rx_buffers(sc))
2926 #endif /* DEV_NETMAP */
2928 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2929 rxq = &sc->vtnet_rxqs[i];
2931 /* Hold the lock to satisfy asserts. */
2932 VTNET_RXQ_LOCK(rxq);
2933 error = vtnet_rxq_populate(rxq);
2934 VTNET_RXQ_UNLOCK(rxq);
2938 "cannot allocate mbufs for Rx queue %d\n", i);
2947 vtnet_init_tx_queues(struct vtnet_softc *sc)
2949 struct vtnet_txq *txq;
2952 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2953 txq = &sc->vtnet_txqs[i];
2954 txq->vtntx_watchdog = 0;
2961 vtnet_init_rxtx_queues(struct vtnet_softc *sc)
2965 error = vtnet_init_rx_queues(sc);
2969 error = vtnet_init_tx_queues(sc);
2977 vtnet_set_active_vq_pairs(struct vtnet_softc *sc)
2982 dev = sc->vtnet_dev;
2984 if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) {
2985 MPASS(sc->vtnet_max_vq_pairs == 1);
2986 sc->vtnet_act_vq_pairs = 1;
2990 /* BMV: Just use the maximum configured for now. */
2991 npairs = sc->vtnet_max_vq_pairs;
2993 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) {
2995 "cannot set active queue pairs to %d\n", npairs);
2999 sc->vtnet_act_vq_pairs = npairs;
3003 vtnet_reinit(struct vtnet_softc *sc)
3008 ifp = sc->vtnet_ifp;
3010 /* Use the current MAC address. */
3011 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
3012 vtnet_set_hwaddr(sc);
3014 vtnet_set_active_vq_pairs(sc);
3016 ifp->if_hwassist = 0;
3017 if (ifp->if_capenable & IFCAP_TXCSUM)
3018 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
3019 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3020 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6;
3021 if (ifp->if_capenable & IFCAP_TSO4)
3022 ifp->if_hwassist |= CSUM_IP_TSO;
3023 if (ifp->if_capenable & IFCAP_TSO6)
3024 ifp->if_hwassist |= CSUM_IP6_TSO;
3026 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
3027 vtnet_init_rx_filters(sc);
3029 error = vtnet_init_rxtx_queues(sc);
3033 vtnet_enable_interrupts(sc);
3034 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3040 vtnet_init_locked(struct vtnet_softc *sc)
3045 dev = sc->vtnet_dev;
3046 ifp = sc->vtnet_ifp;
3048 VTNET_CORE_LOCK_ASSERT(sc);
3050 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3055 /* Reinitialize with the host. */
3056 if (vtnet_virtio_reinit(sc) != 0)
3059 if (vtnet_reinit(sc) != 0)
3062 virtio_reinit_complete(dev);
3064 vtnet_update_link_status(sc);
3065 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
3074 vtnet_init(void *xsc)
3076 struct vtnet_softc *sc;
3081 if (!NA(sc->vtnet_ifp)) {
3082 D("try to attach again");
3083 vtnet_netmap_attach(sc);
3085 #endif /* DEV_NETMAP */
3087 VTNET_CORE_LOCK(sc);
3088 vtnet_init_locked(sc);
3089 VTNET_CORE_UNLOCK(sc);
3093 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
3095 struct virtqueue *vq;
3097 vq = sc->vtnet_ctrl_vq;
3100 * The control virtqueue is only polled and therefore it should
3103 KASSERT(virtqueue_empty(vq),
3104 ("%s: ctrl vq %p not empty", __func__, vq));
3108 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
3109 struct sglist *sg, int readable, int writable)
3111 struct virtqueue *vq;
3113 vq = sc->vtnet_ctrl_vq;
3115 VTNET_CORE_LOCK_ASSERT(sc);
3116 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
3117 ("%s: CTRL_VQ feature not negotiated", __func__));
3119 if (!virtqueue_empty(vq))
3121 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
3125 * Poll for the response, but the command is likely already
3126 * done when we return from the notify.
3128 virtqueue_notify(vq);
3129 virtqueue_poll(vq, NULL);
3133 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
3135 struct virtio_net_ctrl_hdr hdr __aligned(2);
3136 struct sglist_seg segs[3];
3141 hdr.class = VIRTIO_NET_CTRL_MAC;
3142 hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
3143 ack = VIRTIO_NET_ERR;
3145 sglist_init(&sg, 3, segs);
3147 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3148 error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN);
3149 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3150 KASSERT(error == 0 && sg.sg_nseg == 3,
3151 ("%s: error %d adding set MAC msg to sglist", __func__, error));
3153 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3155 return (ack == VIRTIO_NET_OK ? 0 : EIO);
3159 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs)
3161 struct sglist_seg segs[3];
3164 struct virtio_net_ctrl_hdr hdr;
3166 struct virtio_net_ctrl_mq mq;
3172 s.hdr.class = VIRTIO_NET_CTRL_MQ;
3173 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
3174 s.mq.virtqueue_pairs = npairs;
3175 s.ack = VIRTIO_NET_ERR;
3177 sglist_init(&sg, 3, segs);
3179 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3180 error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq));
3181 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3182 KASSERT(error == 0 && sg.sg_nseg == 3,
3183 ("%s: error %d adding MQ message to sglist", __func__, error));
3185 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3187 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3191 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
3193 struct sglist_seg segs[3];
3196 struct virtio_net_ctrl_hdr hdr;
3204 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3205 ("%s: CTRL_RX feature not negotiated", __func__));
3207 s.hdr.class = VIRTIO_NET_CTRL_RX;
3210 s.ack = VIRTIO_NET_ERR;
3212 sglist_init(&sg, 3, segs);
3214 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3215 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
3216 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3217 KASSERT(error == 0 && sg.sg_nseg == 3,
3218 ("%s: error %d adding Rx message to sglist", __func__, error));
3220 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3222 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3226 vtnet_set_promisc(struct vtnet_softc *sc, int on)
3229 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
3233 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
3236 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
3240 * The device defaults to promiscuous mode for backwards compatibility.
3241 * Turn it off at attach time if possible.
3244 vtnet_attach_disable_promisc(struct vtnet_softc *sc)
3248 ifp = sc->vtnet_ifp;
3250 VTNET_CORE_LOCK(sc);
3251 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) {
3252 ifp->if_flags |= IFF_PROMISC;
3253 } else if (vtnet_set_promisc(sc, 0) != 0) {
3254 ifp->if_flags |= IFF_PROMISC;
3255 device_printf(sc->vtnet_dev,
3256 "cannot disable default promiscuous mode\n");
3258 VTNET_CORE_UNLOCK(sc);
3262 vtnet_rx_filter(struct vtnet_softc *sc)
3267 dev = sc->vtnet_dev;
3268 ifp = sc->vtnet_ifp;
3270 VTNET_CORE_LOCK_ASSERT(sc);
3272 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
3273 device_printf(dev, "cannot %s promiscuous mode\n",
3274 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
3276 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
3277 device_printf(dev, "cannot %s all-multicast mode\n",
3278 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
3282 vtnet_rx_filter_mac(struct vtnet_softc *sc)
3284 struct virtio_net_ctrl_hdr hdr __aligned(2);
3285 struct vtnet_mac_filter *filter;
3286 struct sglist_seg segs[4];
3290 struct ifmultiaddr *ifma;
3291 int ucnt, mcnt, promisc, allmulti, error;
3294 ifp = sc->vtnet_ifp;
3295 filter = sc->vtnet_mac_filter;
3301 VTNET_CORE_LOCK_ASSERT(sc);
3302 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3303 ("%s: CTRL_RX feature not negotiated", __func__));
3305 /* Unicast MAC addresses: */
3307 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3308 if (ifa->ifa_addr->sa_family != AF_LINK)
3310 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3311 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
3313 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
3318 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3319 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
3322 if_addr_runlock(ifp);
3325 filter->vmf_unicast.nentries = 0;
3326 if_printf(ifp, "more than %d MAC addresses assigned, "
3327 "falling back to promiscuous mode\n",
3328 VTNET_MAX_MAC_ENTRIES);
3330 filter->vmf_unicast.nentries = ucnt;
3332 /* Multicast MAC addresses: */
3333 if_maddr_rlock(ifp);
3334 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3335 if (ifma->ifma_addr->sa_family != AF_LINK)
3337 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
3342 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3343 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
3346 if_maddr_runlock(ifp);
3348 if (allmulti != 0) {
3349 filter->vmf_multicast.nentries = 0;
3350 if_printf(ifp, "more than %d multicast MAC addresses "
3351 "assigned, falling back to all-multicast mode\n",
3352 VTNET_MAX_MAC_ENTRIES);
3354 filter->vmf_multicast.nentries = mcnt;
3356 if (promisc != 0 && allmulti != 0)
3359 hdr.class = VIRTIO_NET_CTRL_MAC;
3360 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
3361 ack = VIRTIO_NET_ERR;
3363 sglist_init(&sg, 4, segs);
3365 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3366 error |= sglist_append(&sg, &filter->vmf_unicast,
3367 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
3368 error |= sglist_append(&sg, &filter->vmf_multicast,
3369 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
3370 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3371 KASSERT(error == 0 && sg.sg_nseg == 4,
3372 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
3374 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3376 if (ack != VIRTIO_NET_OK)
3377 if_printf(ifp, "error setting host MAC filter table\n");
3380 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
3381 if_printf(ifp, "cannot enable promiscuous mode\n");
3382 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
3383 if_printf(ifp, "cannot enable all-multicast mode\n");
3387 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3389 struct sglist_seg segs[3];
3392 struct virtio_net_ctrl_hdr hdr;
3400 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
3401 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
3403 s.ack = VIRTIO_NET_ERR;
3405 sglist_init(&sg, 3, segs);
3407 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3408 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
3409 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3410 KASSERT(error == 0 && sg.sg_nseg == 3,
3411 ("%s: error %d adding VLAN message to sglist", __func__, error));
3413 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3415 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3419 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
3425 VTNET_CORE_LOCK_ASSERT(sc);
3426 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
3427 ("%s: VLAN_FILTER feature not negotiated", __func__));
3429 /* Enable the filter for each configured VLAN. */
3430 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) {
3431 w = sc->vtnet_vlan_filter[i];
3433 while ((bit = ffs(w) - 1) != -1) {
3435 tag = sizeof(w) * CHAR_BIT * i + bit;
3437 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
3438 device_printf(sc->vtnet_dev,
3439 "cannot enable VLAN %d filter\n", tag);
3446 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3451 ifp = sc->vtnet_ifp;
3452 idx = (tag >> 5) & 0x7F;
3455 if (tag == 0 || tag > 4095)
3458 VTNET_CORE_LOCK(sc);
3461 sc->vtnet_vlan_filter[idx] |= (1 << bit);
3463 sc->vtnet_vlan_filter[idx] &= ~(1 << bit);
3465 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
3466 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
3467 device_printf(sc->vtnet_dev,
3468 "cannot %s VLAN %d %s the host filter table\n",
3469 add ? "add" : "remove", tag, add ? "to" : "from");
3472 VTNET_CORE_UNLOCK(sc);
3476 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3479 if (ifp->if_softc != arg)
3482 vtnet_update_vlan_filter(arg, 1, tag);
3486 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3489 if (ifp->if_softc != arg)
3492 vtnet_update_vlan_filter(arg, 0, tag);
3496 vtnet_is_link_up(struct vtnet_softc *sc)
3502 dev = sc->vtnet_dev;
3503 ifp = sc->vtnet_ifp;
3505 if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0)
3506 status = VIRTIO_NET_S_LINK_UP;
3508 status = virtio_read_dev_config_2(dev,
3509 offsetof(struct virtio_net_config, status));
3511 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
3515 vtnet_update_link_status(struct vtnet_softc *sc)
3520 ifp = sc->vtnet_ifp;
3522 VTNET_CORE_LOCK_ASSERT(sc);
3523 link = vtnet_is_link_up(sc);
3525 /* Notify if the link status has changed. */
3526 if (link != 0 && sc->vtnet_link_active == 0) {
3527 sc->vtnet_link_active = 1;
3528 if_link_state_change(ifp, LINK_STATE_UP);
3529 } else if (link == 0 && sc->vtnet_link_active != 0) {
3530 sc->vtnet_link_active = 0;
3531 if_link_state_change(ifp, LINK_STATE_DOWN);
3536 vtnet_ifmedia_upd(struct ifnet *ifp)
3538 struct vtnet_softc *sc;
3539 struct ifmedia *ifm;
3542 ifm = &sc->vtnet_media;
3544 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3551 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3553 struct vtnet_softc *sc;
3557 ifmr->ifm_status = IFM_AVALID;
3558 ifmr->ifm_active = IFM_ETHER;
3560 VTNET_CORE_LOCK(sc);
3561 if (vtnet_is_link_up(sc) != 0) {
3562 ifmr->ifm_status |= IFM_ACTIVE;
3563 ifmr->ifm_active |= VTNET_MEDIATYPE;
3565 ifmr->ifm_active |= IFM_NONE;
3566 VTNET_CORE_UNLOCK(sc);
3570 vtnet_set_hwaddr(struct vtnet_softc *sc)
3575 dev = sc->vtnet_dev;
3577 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) {
3578 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
3579 device_printf(dev, "unable to set MAC address\n");
3580 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
3581 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3582 virtio_write_dev_config_1(dev,
3583 offsetof(struct virtio_net_config, mac) + i,
3584 sc->vtnet_hwaddr[i]);
3590 vtnet_get_hwaddr(struct vtnet_softc *sc)
3595 dev = sc->vtnet_dev;
3597 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
3599 * Generate a random locally administered unicast address.
3601 * It would be nice to generate the same MAC address across
3602 * reboots, but it seems all the hosts currently available
3603 * support the MAC feature, so this isn't too important.
3605 sc->vtnet_hwaddr[0] = 0xB2;
3606 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
3607 vtnet_set_hwaddr(sc);
3611 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3612 sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev,
3613 offsetof(struct virtio_net_config, mac) + i);
3618 vtnet_vlan_tag_remove(struct mbuf *m)
3620 struct ether_vlan_header *evh;
3622 evh = mtod(m, struct ether_vlan_header *);
3623 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
3624 m->m_flags |= M_VLANTAG;
3626 /* Strip the 802.1Q header. */
3627 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
3628 ETHER_HDR_LEN - ETHER_TYPE_LEN);
3629 m_adj(m, ETHER_VLAN_ENCAP_LEN);
3633 vtnet_set_rx_process_limit(struct vtnet_softc *sc)
3637 limit = vtnet_tunable_int(sc, "rx_process_limit",
3638 vtnet_rx_process_limit);
3641 sc->vtnet_rx_process_limit = limit;
3645 vtnet_set_tx_intr_threshold(struct vtnet_softc *sc)
3650 dev = sc->vtnet_dev;
3651 size = virtqueue_size(sc->vtnet_txqs[0].vtntx_vq);
3654 * The Tx interrupt is disabled until the queue free count falls
3655 * below our threshold. Completed frames are drained from the Tx
3656 * virtqueue before transmitting new frames and in the watchdog
3657 * callout, so the frequency of Tx interrupts is greatly reduced,
3658 * at the cost of not freeing mbufs as quickly as they otherwise
3661 * N.B. We assume all the Tx queues are the same size.
3666 * Without indirect descriptors, leave enough room for the most
3667 * segments we handle.
3669 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 &&
3670 thresh < sc->vtnet_tx_nsegs)
3671 thresh = sc->vtnet_tx_nsegs;
3673 sc->vtnet_tx_intr_thresh = thresh;
3677 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx,
3678 struct sysctl_oid_list *child, struct vtnet_rxq *rxq)
3680 struct sysctl_oid *node;
3681 struct sysctl_oid_list *list;
3682 struct vtnet_rxq_stats *stats;
3685 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id);
3686 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3687 CTLFLAG_RD, NULL, "Receive Queue");
3688 list = SYSCTL_CHILDREN(node);
3690 stats = &rxq->vtnrx_stats;
3692 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD,
3693 &stats->vrxs_ipackets, "Receive packets");
3694 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD,
3695 &stats->vrxs_ibytes, "Receive bytes");
3696 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD,
3697 &stats->vrxs_iqdrops, "Receive drops");
3698 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD,
3699 &stats->vrxs_ierrors, "Receive errors");
3700 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3701 &stats->vrxs_csum, "Receive checksum offloaded");
3702 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD,
3703 &stats->vrxs_csum_failed, "Receive checksum offload failed");
3704 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3705 &stats->vrxs_rescheduled,
3706 "Receive interrupt handler rescheduled");
3710 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx,
3711 struct sysctl_oid_list *child, struct vtnet_txq *txq)
3713 struct sysctl_oid *node;
3714 struct sysctl_oid_list *list;
3715 struct vtnet_txq_stats *stats;
3718 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id);
3719 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3720 CTLFLAG_RD, NULL, "Transmit Queue");
3721 list = SYSCTL_CHILDREN(node);
3723 stats = &txq->vtntx_stats;
3725 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD,
3726 &stats->vtxs_opackets, "Transmit packets");
3727 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD,
3728 &stats->vtxs_obytes, "Transmit bytes");
3729 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD,
3730 &stats->vtxs_omcasts, "Transmit multicasts");
3731 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3732 &stats->vtxs_csum, "Transmit checksum offloaded");
3733 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD,
3734 &stats->vtxs_tso, "Transmit segmentation offloaded");
3735 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3736 &stats->vtxs_rescheduled,
3737 "Transmit interrupt handler rescheduled");
3741 vtnet_setup_queue_sysctl(struct vtnet_softc *sc)
3744 struct sysctl_ctx_list *ctx;
3745 struct sysctl_oid *tree;
3746 struct sysctl_oid_list *child;
3749 dev = sc->vtnet_dev;
3750 ctx = device_get_sysctl_ctx(dev);
3751 tree = device_get_sysctl_tree(dev);
3752 child = SYSCTL_CHILDREN(tree);
3754 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3755 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]);
3756 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]);
3761 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx,
3762 struct sysctl_oid_list *child, struct vtnet_softc *sc)
3764 struct vtnet_statistics *stats;
3766 stats = &sc->vtnet_stats;
3768 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
3769 CTLFLAG_RD, &stats->mbuf_alloc_failed,
3770 "Mbuf cluster allocation failures");
3772 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
3773 CTLFLAG_RD, &stats->rx_frame_too_large,
3774 "Received frame larger than the mbuf chain");
3775 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
3776 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
3777 "Enqueuing the replacement receive mbuf failed");
3778 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
3779 CTLFLAG_RD, &stats->rx_mergeable_failed,
3780 "Mergeable buffers receive failures");
3781 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
3782 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
3783 "Received checksum offloaded buffer with unsupported "
3785 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
3786 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
3787 "Received checksum offloaded buffer with incorrect IP protocol");
3788 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
3789 CTLFLAG_RD, &stats->rx_csum_bad_offset,
3790 "Received checksum offloaded buffer with incorrect offset");
3791 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto",
3792 CTLFLAG_RD, &stats->rx_csum_bad_proto,
3793 "Received checksum offloaded buffer with incorrect protocol");
3794 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
3795 CTLFLAG_RD, &stats->rx_csum_failed,
3796 "Received buffer checksum offload failed");
3797 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
3798 CTLFLAG_RD, &stats->rx_csum_offloaded,
3799 "Received buffer checksum offload succeeded");
3800 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
3801 CTLFLAG_RD, &stats->rx_task_rescheduled,
3802 "Times the receive interrupt task rescheduled itself");
3804 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
3805 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
3806 "Aborted transmit of checksum offloaded buffer with unknown "
3808 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
3809 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
3810 "Aborted transmit of TSO buffer with unknown Ethernet type");
3811 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp",
3812 CTLFLAG_RD, &stats->tx_tso_not_tcp,
3813 "Aborted transmit of TSO buffer with non TCP protocol");
3814 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
3815 CTLFLAG_RD, &stats->tx_defragged,
3816 "Transmit mbufs defragged");
3817 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
3818 CTLFLAG_RD, &stats->tx_defrag_failed,
3819 "Aborted transmit of buffer because defrag failed");
3820 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
3821 CTLFLAG_RD, &stats->tx_csum_offloaded,
3822 "Offloaded checksum of transmitted buffer");
3823 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
3824 CTLFLAG_RD, &stats->tx_tso_offloaded,
3825 "Segmentation offload of transmitted buffer");
3826 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
3827 CTLFLAG_RD, &stats->tx_task_rescheduled,
3828 "Times the transmit interrupt task rescheduled itself");
3832 vtnet_setup_sysctl(struct vtnet_softc *sc)
3835 struct sysctl_ctx_list *ctx;
3836 struct sysctl_oid *tree;
3837 struct sysctl_oid_list *child;
3839 dev = sc->vtnet_dev;
3840 ctx = device_get_sysctl_ctx(dev);
3841 tree = device_get_sysctl_tree(dev);
3842 child = SYSCTL_CHILDREN(tree);
3844 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs",
3845 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0,
3846 "Maximum number of supported virtqueue pairs");
3847 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs",
3848 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0,
3849 "Number of active virtqueue pairs");
3851 vtnet_setup_stat_sysctl(ctx, child, sc);
3855 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq)
3858 return (virtqueue_enable_intr(rxq->vtnrx_vq));
3862 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq)
3865 virtqueue_disable_intr(rxq->vtnrx_vq);
3869 vtnet_txq_enable_intr(struct vtnet_txq *txq)
3871 struct virtqueue *vq;
3875 if (vtnet_txq_below_threshold(txq) != 0)
3876 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG));
3879 * The free count is above our threshold. Keep the Tx interrupt
3880 * disabled until the queue is fuller.
3886 vtnet_txq_disable_intr(struct vtnet_txq *txq)
3889 virtqueue_disable_intr(txq->vtntx_vq);
3893 vtnet_enable_rx_interrupts(struct vtnet_softc *sc)
3897 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3898 vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]);
3902 vtnet_enable_tx_interrupts(struct vtnet_softc *sc)
3906 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3907 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]);
3911 vtnet_enable_interrupts(struct vtnet_softc *sc)
3914 vtnet_enable_rx_interrupts(sc);
3915 vtnet_enable_tx_interrupts(sc);
3919 vtnet_disable_rx_interrupts(struct vtnet_softc *sc)
3923 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3924 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]);
3928 vtnet_disable_tx_interrupts(struct vtnet_softc *sc)
3932 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3933 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]);
3937 vtnet_disable_interrupts(struct vtnet_softc *sc)
3940 vtnet_disable_rx_interrupts(sc);
3941 vtnet_disable_tx_interrupts(sc);
3945 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def)
3949 snprintf(path, sizeof(path),
3950 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob);
3951 TUNABLE_INT_FETCH(path, &def);