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/eventhandler.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sockio.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/random.h>
43 #include <sys/sglist.h>
45 #include <sys/mutex.h>
46 #include <sys/taskqueue.h>
48 #include <machine/smp.h>
52 #include <net/ethernet.h>
54 #include <net/if_var.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
59 #include <net/if_vlan_var.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/tcp.h>
70 #include <netinet/sctp.h>
72 #include <machine/bus.h>
73 #include <machine/resource.h>
77 #include <dev/virtio/virtio.h>
78 #include <dev/virtio/virtqueue.h>
79 #include <dev/virtio/network/virtio_net.h>
80 #include <dev/virtio/network/if_vtnetvar.h>
82 #include "virtio_if.h"
85 #include "opt_inet6.h"
87 static int vtnet_modevent(module_t, int, void *);
89 static int vtnet_probe(device_t);
90 static int vtnet_attach(device_t);
91 static int vtnet_detach(device_t);
92 static int vtnet_suspend(device_t);
93 static int vtnet_resume(device_t);
94 static int vtnet_shutdown(device_t);
95 static int vtnet_attach_completed(device_t);
96 static int vtnet_config_change(device_t);
98 static void vtnet_negotiate_features(struct vtnet_softc *);
99 static void vtnet_setup_features(struct vtnet_softc *);
100 static int vtnet_init_rxq(struct vtnet_softc *, int);
101 static int vtnet_init_txq(struct vtnet_softc *, int);
102 static int vtnet_alloc_rxtx_queues(struct vtnet_softc *);
103 static void vtnet_free_rxtx_queues(struct vtnet_softc *);
104 static int vtnet_alloc_rx_filters(struct vtnet_softc *);
105 static void vtnet_free_rx_filters(struct vtnet_softc *);
106 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
107 static int vtnet_setup_interface(struct vtnet_softc *);
108 static int vtnet_change_mtu(struct vtnet_softc *, int);
109 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t);
110 static uint64_t vtnet_get_counter(struct ifnet *, ift_counter);
112 static int vtnet_rxq_populate(struct vtnet_rxq *);
113 static void vtnet_rxq_free_mbufs(struct vtnet_rxq *);
115 vtnet_rx_alloc_buf(struct vtnet_softc *, int , struct mbuf **);
116 static int vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *,
118 static int vtnet_rxq_replace_buf(struct vtnet_rxq *, struct mbuf *, int);
119 static int vtnet_rxq_enqueue_buf(struct vtnet_rxq *, struct mbuf *);
120 static int vtnet_rxq_new_buf(struct vtnet_rxq *);
121 static int vtnet_rxq_csum(struct vtnet_rxq *, struct mbuf *,
122 struct virtio_net_hdr *);
123 static void vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *, int);
124 static void vtnet_rxq_discard_buf(struct vtnet_rxq *, struct mbuf *);
125 static int vtnet_rxq_merged_eof(struct vtnet_rxq *, struct mbuf *, int);
126 static void vtnet_rxq_input(struct vtnet_rxq *, struct mbuf *,
127 struct virtio_net_hdr *);
128 static int vtnet_rxq_eof(struct vtnet_rxq *);
129 static void vtnet_rx_vq_intr(void *);
130 static void vtnet_rxq_tq_intr(void *, int);
132 static int vtnet_txq_below_threshold(struct vtnet_txq *);
133 static int vtnet_txq_notify(struct vtnet_txq *);
134 static void vtnet_txq_free_mbufs(struct vtnet_txq *);
135 static int vtnet_txq_offload_ctx(struct vtnet_txq *, struct mbuf *,
136 int *, int *, int *);
137 static int vtnet_txq_offload_tso(struct vtnet_txq *, struct mbuf *, int,
138 int, struct virtio_net_hdr *);
140 vtnet_txq_offload(struct vtnet_txq *, struct mbuf *,
141 struct virtio_net_hdr *);
142 static int vtnet_txq_enqueue_buf(struct vtnet_txq *, struct mbuf **,
143 struct vtnet_tx_header *);
144 static int vtnet_txq_encap(struct vtnet_txq *, struct mbuf **);
145 #ifdef VTNET_LEGACY_TX
146 static void vtnet_start_locked(struct vtnet_txq *, struct ifnet *);
147 static void vtnet_start(struct ifnet *);
149 static int vtnet_txq_mq_start_locked(struct vtnet_txq *, struct mbuf *);
150 static int vtnet_txq_mq_start(struct ifnet *, struct mbuf *);
151 static void vtnet_txq_tq_deferred(void *, int);
153 static void vtnet_txq_start(struct vtnet_txq *);
154 static void vtnet_txq_tq_intr(void *, int);
155 static int vtnet_txq_eof(struct vtnet_txq *);
156 static void vtnet_tx_vq_intr(void *);
157 static void vtnet_tx_start_all(struct vtnet_softc *);
159 #ifndef VTNET_LEGACY_TX
160 static void vtnet_qflush(struct ifnet *);
163 static int vtnet_watchdog(struct vtnet_txq *);
164 static void vtnet_accum_stats(struct vtnet_softc *,
165 struct vtnet_rxq_stats *, struct vtnet_txq_stats *);
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_mmio, vtnet_driver, vtnet_devclass,
303 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
305 MODULE_VERSION(vtnet, 1);
306 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
308 MODULE_DEPEND(vtnet, netmap, 1, 1, 1);
309 #endif /* DEV_NETMAP */
312 vtnet_modevent(module_t mod, int type, void *unused)
320 vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr",
321 sizeof(struct vtnet_tx_header),
322 NULL, NULL, NULL, NULL, 0, 0);
326 if (uma_zone_get_cur(vtnet_tx_header_zone) > 0)
328 else if (type == MOD_UNLOAD) {
329 uma_zdestroy(vtnet_tx_header_zone);
330 vtnet_tx_header_zone = NULL;
344 vtnet_probe(device_t dev)
347 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
350 device_set_desc(dev, "VirtIO Networking Adapter");
352 return (BUS_PROBE_DEFAULT);
356 vtnet_attach(device_t dev)
358 struct vtnet_softc *sc;
361 sc = device_get_softc(dev);
364 /* Register our feature descriptions. */
365 virtio_set_feature_desc(dev, vtnet_feature_desc);
367 VTNET_CORE_LOCK_INIT(sc);
368 callout_init_mtx(&sc->vtnet_tick_ch, VTNET_CORE_MTX(sc), 0);
370 vtnet_setup_sysctl(sc);
371 vtnet_setup_features(sc);
373 error = vtnet_alloc_rx_filters(sc);
375 device_printf(dev, "cannot allocate Rx filters\n");
379 error = vtnet_alloc_rxtx_queues(sc);
381 device_printf(dev, "cannot allocate queues\n");
385 error = vtnet_alloc_virtqueues(sc);
387 device_printf(dev, "cannot allocate virtqueues\n");
391 error = vtnet_setup_interface(sc);
393 device_printf(dev, "cannot setup interface\n");
397 error = virtio_setup_intr(dev, INTR_TYPE_NET);
399 device_printf(dev, "cannot setup virtqueue interrupts\n");
400 /* BMV: This will crash if during boot! */
401 ether_ifdetach(sc->vtnet_ifp);
406 vtnet_netmap_attach(sc);
407 #endif /* DEV_NETMAP */
409 vtnet_start_taskqueues(sc);
419 vtnet_detach(device_t dev)
421 struct vtnet_softc *sc;
424 sc = device_get_softc(dev);
427 if (device_is_attached(dev)) {
430 VTNET_CORE_UNLOCK(sc);
432 callout_drain(&sc->vtnet_tick_ch);
433 vtnet_drain_taskqueues(sc);
440 #endif /* DEV_NETMAP */
442 vtnet_free_taskqueues(sc);
444 if (sc->vtnet_vlan_attach != NULL) {
445 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
446 sc->vtnet_vlan_attach = NULL;
448 if (sc->vtnet_vlan_detach != NULL) {
449 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
450 sc->vtnet_vlan_detach = NULL;
453 ifmedia_removeall(&sc->vtnet_media);
457 sc->vtnet_ifp = NULL;
460 vtnet_free_rxtx_queues(sc);
461 vtnet_free_rx_filters(sc);
463 if (sc->vtnet_ctrl_vq != NULL)
464 vtnet_free_ctrl_vq(sc);
466 VTNET_CORE_LOCK_DESTROY(sc);
472 vtnet_suspend(device_t dev)
474 struct vtnet_softc *sc;
476 sc = device_get_softc(dev);
480 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
481 VTNET_CORE_UNLOCK(sc);
487 vtnet_resume(device_t dev)
489 struct vtnet_softc *sc;
492 sc = device_get_softc(dev);
496 if (ifp->if_flags & IFF_UP)
497 vtnet_init_locked(sc);
498 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
499 VTNET_CORE_UNLOCK(sc);
505 vtnet_shutdown(device_t dev)
509 * Suspend already does all of what we need to
510 * do here; we just never expect to be resumed.
512 return (vtnet_suspend(dev));
516 vtnet_attach_completed(device_t dev)
519 vtnet_attach_disable_promisc(device_get_softc(dev));
525 vtnet_config_change(device_t dev)
527 struct vtnet_softc *sc;
529 sc = device_get_softc(dev);
532 vtnet_update_link_status(sc);
533 if (sc->vtnet_link_active != 0)
534 vtnet_tx_start_all(sc);
535 VTNET_CORE_UNLOCK(sc);
541 vtnet_negotiate_features(struct vtnet_softc *sc)
544 uint64_t mask, features;
550 * TSO and LRO are only available when their corresponding checksum
551 * offload feature is also negotiated.
553 if (vtnet_tunable_int(sc, "csum_disable", vtnet_csum_disable)) {
554 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
555 mask |= VTNET_TSO_FEATURES | VTNET_LRO_FEATURES;
557 if (vtnet_tunable_int(sc, "tso_disable", vtnet_tso_disable))
558 mask |= VTNET_TSO_FEATURES;
559 if (vtnet_tunable_int(sc, "lro_disable", vtnet_lro_disable))
560 mask |= VTNET_LRO_FEATURES;
561 #ifndef VTNET_LEGACY_TX
562 if (vtnet_tunable_int(sc, "mq_disable", vtnet_mq_disable))
563 mask |= VIRTIO_NET_F_MQ;
565 mask |= VIRTIO_NET_F_MQ;
568 features = VTNET_FEATURES & ~mask;
569 sc->vtnet_features = virtio_negotiate_features(dev, features);
571 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
572 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
574 * LRO without mergeable buffers requires special care. This
575 * is not ideal because every receive buffer must be large
576 * enough to hold the maximum TCP packet, the Ethernet header,
577 * and the header. This requires up to 34 descriptors with
578 * MCLBYTES clusters. If we do not have indirect descriptors,
579 * LRO is disabled since the virtqueue will not contain very
580 * many receive buffers.
582 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
584 "LRO disabled due to both mergeable buffers and "
585 "indirect descriptors not negotiated\n");
587 features &= ~VTNET_LRO_FEATURES;
589 virtio_negotiate_features(dev, features);
591 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
596 vtnet_setup_features(struct vtnet_softc *sc)
603 vtnet_negotiate_features(sc);
605 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
606 sc->vtnet_flags |= VTNET_FLAG_INDIRECT;
607 if (virtio_with_feature(dev, VIRTIO_RING_F_EVENT_IDX))
608 sc->vtnet_flags |= VTNET_FLAG_EVENT_IDX;
610 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
611 /* This feature should always be negotiated. */
612 sc->vtnet_flags |= VTNET_FLAG_MAC;
615 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
616 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
617 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
619 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
621 if (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS)
622 sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
623 else if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG)
624 sc->vtnet_rx_nsegs = VTNET_MAX_RX_SEGS;
626 sc->vtnet_rx_nsegs = VTNET_MIN_RX_SEGS;
628 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO) ||
629 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
630 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
631 sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
633 sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
635 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
636 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
638 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
639 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
640 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
641 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
642 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR))
643 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
646 if (virtio_with_feature(dev, VIRTIO_NET_F_MQ) &&
647 sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
648 max_pairs = virtio_read_dev_config_2(dev,
649 offsetof(struct virtio_net_config, max_virtqueue_pairs));
650 if (max_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
651 max_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
658 * Limit the maximum number of queue pairs to the number of
659 * CPUs or the configured maximum. The actual number of
660 * queues that get used may be less.
662 max = vtnet_tunable_int(sc, "mq_max_pairs", vtnet_mq_max_pairs);
663 if (max > 0 && max_pairs > max)
665 if (max_pairs > mp_ncpus)
666 max_pairs = mp_ncpus;
667 if (max_pairs > VTNET_MAX_QUEUE_PAIRS)
668 max_pairs = VTNET_MAX_QUEUE_PAIRS;
670 sc->vtnet_flags |= VTNET_FLAG_MULTIQ;
673 sc->vtnet_max_vq_pairs = max_pairs;
677 vtnet_init_rxq(struct vtnet_softc *sc, int id)
679 struct vtnet_rxq *rxq;
681 rxq = &sc->vtnet_rxqs[id];
683 snprintf(rxq->vtnrx_name, sizeof(rxq->vtnrx_name), "%s-rx%d",
684 device_get_nameunit(sc->vtnet_dev), id);
685 mtx_init(&rxq->vtnrx_mtx, rxq->vtnrx_name, NULL, MTX_DEF);
690 rxq->vtnrx_sg = sglist_alloc(sc->vtnet_rx_nsegs, M_NOWAIT);
691 if (rxq->vtnrx_sg == NULL)
694 TASK_INIT(&rxq->vtnrx_intrtask, 0, vtnet_rxq_tq_intr, rxq);
695 rxq->vtnrx_tq = taskqueue_create(rxq->vtnrx_name, M_NOWAIT,
696 taskqueue_thread_enqueue, &rxq->vtnrx_tq);
698 return (rxq->vtnrx_tq == NULL ? ENOMEM : 0);
702 vtnet_init_txq(struct vtnet_softc *sc, int id)
704 struct vtnet_txq *txq;
706 txq = &sc->vtnet_txqs[id];
708 snprintf(txq->vtntx_name, sizeof(txq->vtntx_name), "%s-tx%d",
709 device_get_nameunit(sc->vtnet_dev), id);
710 mtx_init(&txq->vtntx_mtx, txq->vtntx_name, NULL, MTX_DEF);
715 txq->vtntx_sg = sglist_alloc(sc->vtnet_tx_nsegs, M_NOWAIT);
716 if (txq->vtntx_sg == NULL)
719 #ifndef VTNET_LEGACY_TX
720 txq->vtntx_br = buf_ring_alloc(VTNET_DEFAULT_BUFRING_SIZE, M_DEVBUF,
721 M_NOWAIT, &txq->vtntx_mtx);
722 if (txq->vtntx_br == NULL)
725 TASK_INIT(&txq->vtntx_defrtask, 0, vtnet_txq_tq_deferred, txq);
727 TASK_INIT(&txq->vtntx_intrtask, 0, vtnet_txq_tq_intr, txq);
728 txq->vtntx_tq = taskqueue_create(txq->vtntx_name, M_NOWAIT,
729 taskqueue_thread_enqueue, &txq->vtntx_tq);
730 if (txq->vtntx_tq == NULL)
737 vtnet_alloc_rxtx_queues(struct vtnet_softc *sc)
739 int i, npairs, error;
741 npairs = sc->vtnet_max_vq_pairs;
743 sc->vtnet_rxqs = malloc(sizeof(struct vtnet_rxq) * npairs, M_DEVBUF,
745 sc->vtnet_txqs = malloc(sizeof(struct vtnet_txq) * npairs, M_DEVBUF,
747 if (sc->vtnet_rxqs == NULL || sc->vtnet_txqs == NULL)
750 for (i = 0; i < npairs; i++) {
751 error = vtnet_init_rxq(sc, i);
754 error = vtnet_init_txq(sc, i);
759 vtnet_setup_queue_sysctl(sc);
765 vtnet_destroy_rxq(struct vtnet_rxq *rxq)
768 rxq->vtnrx_sc = NULL;
771 if (rxq->vtnrx_sg != NULL) {
772 sglist_free(rxq->vtnrx_sg);
773 rxq->vtnrx_sg = NULL;
776 if (mtx_initialized(&rxq->vtnrx_mtx) != 0)
777 mtx_destroy(&rxq->vtnrx_mtx);
781 vtnet_destroy_txq(struct vtnet_txq *txq)
784 txq->vtntx_sc = NULL;
787 if (txq->vtntx_sg != NULL) {
788 sglist_free(txq->vtntx_sg);
789 txq->vtntx_sg = NULL;
792 #ifndef VTNET_LEGACY_TX
793 if (txq->vtntx_br != NULL) {
794 buf_ring_free(txq->vtntx_br, M_DEVBUF);
795 txq->vtntx_br = NULL;
799 if (mtx_initialized(&txq->vtntx_mtx) != 0)
800 mtx_destroy(&txq->vtntx_mtx);
804 vtnet_free_rxtx_queues(struct vtnet_softc *sc)
808 if (sc->vtnet_rxqs != NULL) {
809 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
810 vtnet_destroy_rxq(&sc->vtnet_rxqs[i]);
811 free(sc->vtnet_rxqs, M_DEVBUF);
812 sc->vtnet_rxqs = NULL;
815 if (sc->vtnet_txqs != NULL) {
816 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
817 vtnet_destroy_txq(&sc->vtnet_txqs[i]);
818 free(sc->vtnet_txqs, M_DEVBUF);
819 sc->vtnet_txqs = NULL;
824 vtnet_alloc_rx_filters(struct vtnet_softc *sc)
827 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
828 sc->vtnet_mac_filter = malloc(sizeof(struct vtnet_mac_filter),
829 M_DEVBUF, M_NOWAIT | M_ZERO);
830 if (sc->vtnet_mac_filter == NULL)
834 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
835 sc->vtnet_vlan_filter = malloc(sizeof(uint32_t) *
836 VTNET_VLAN_FILTER_NWORDS, M_DEVBUF, M_NOWAIT | M_ZERO);
837 if (sc->vtnet_vlan_filter == NULL)
845 vtnet_free_rx_filters(struct vtnet_softc *sc)
848 if (sc->vtnet_mac_filter != NULL) {
849 free(sc->vtnet_mac_filter, M_DEVBUF);
850 sc->vtnet_mac_filter = NULL;
853 if (sc->vtnet_vlan_filter != NULL) {
854 free(sc->vtnet_vlan_filter, M_DEVBUF);
855 sc->vtnet_vlan_filter = NULL;
860 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
863 struct vq_alloc_info *info;
864 struct vtnet_rxq *rxq;
865 struct vtnet_txq *txq;
866 int i, idx, flags, nvqs, error;
871 nvqs = sc->vtnet_max_vq_pairs * 2;
872 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
875 info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
879 for (i = 0, idx = 0; i < sc->vtnet_max_vq_pairs; i++, idx+=2) {
880 rxq = &sc->vtnet_rxqs[i];
881 VQ_ALLOC_INFO_INIT(&info[idx], sc->vtnet_rx_nsegs,
882 vtnet_rx_vq_intr, rxq, &rxq->vtnrx_vq,
883 "%s-%d rx", device_get_nameunit(dev), rxq->vtnrx_id);
885 txq = &sc->vtnet_txqs[i];
886 VQ_ALLOC_INFO_INIT(&info[idx+1], sc->vtnet_tx_nsegs,
887 vtnet_tx_vq_intr, txq, &txq->vtntx_vq,
888 "%s-%d tx", device_get_nameunit(dev), txq->vtntx_id);
891 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
892 VQ_ALLOC_INFO_INIT(&info[idx], 0, NULL, NULL,
893 &sc->vtnet_ctrl_vq, "%s ctrl", device_get_nameunit(dev));
897 * Enable interrupt binding if this is multiqueue. This only matters
898 * when per-vq MSIX is available.
900 if (sc->vtnet_flags & VTNET_FLAG_MULTIQ)
903 error = virtio_alloc_virtqueues(dev, flags, nvqs, info);
910 vtnet_setup_interface(struct vtnet_softc *sc)
917 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
919 device_printf(dev, "cannot allocate ifnet structure\n");
923 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
924 ifp->if_baudrate = IF_Gbps(10); /* Approx. */
926 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
927 ifp->if_init = vtnet_init;
928 ifp->if_ioctl = vtnet_ioctl;
929 ifp->if_get_counter = vtnet_get_counter;
930 #ifndef VTNET_LEGACY_TX
931 ifp->if_transmit = vtnet_txq_mq_start;
932 ifp->if_qflush = vtnet_qflush;
934 struct virtqueue *vq = sc->vtnet_txqs[0].vtntx_vq;
935 ifp->if_start = vtnet_start;
936 IFQ_SET_MAXLEN(&ifp->if_snd, virtqueue_size(vq) - 1);
937 ifp->if_snd.ifq_drv_maxlen = virtqueue_size(vq) - 1;
938 IFQ_SET_READY(&ifp->if_snd);
941 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
943 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
944 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
946 /* Read (or generate) the MAC address for the adapter. */
947 vtnet_get_hwaddr(sc);
949 ether_ifattach(ifp, sc->vtnet_hwaddr);
951 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS))
952 ifp->if_capabilities |= IFCAP_LINKSTATE;
954 /* Tell the upper layer(s) we support long frames. */
955 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
956 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
958 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
959 ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6;
961 if (virtio_with_feature(dev, VIRTIO_NET_F_GSO)) {
962 ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6;
963 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
965 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
966 ifp->if_capabilities |= IFCAP_TSO4;
967 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
968 ifp->if_capabilities |= IFCAP_TSO6;
969 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
970 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
973 if (ifp->if_capabilities & IFCAP_TSO)
974 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
977 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
978 ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
980 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
981 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
982 ifp->if_capabilities |= IFCAP_LRO;
985 if (ifp->if_capabilities & IFCAP_HWCSUM) {
987 * VirtIO does not support VLAN tagging, but we can fake
988 * it by inserting and removing the 802.1Q header during
989 * transmit and receive. We are then able to do checksum
990 * offloading of VLAN frames.
992 ifp->if_capabilities |=
993 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
996 ifp->if_capenable = ifp->if_capabilities;
999 * Capabilities after here are not enabled by default.
1002 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
1003 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
1005 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1006 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
1007 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1008 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
1011 vtnet_set_rx_process_limit(sc);
1012 vtnet_set_tx_intr_threshold(sc);
1018 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1021 int frame_size, clsize;
1023 ifp = sc->vtnet_ifp;
1025 if (new_mtu < ETHERMIN || new_mtu > VTNET_MAX_MTU)
1028 frame_size = sc->vtnet_hdr_size + sizeof(struct ether_vlan_header) +
1032 * Based on the new MTU (and hence frame size) determine which
1033 * cluster size is most appropriate for the receive queues.
1035 if (frame_size <= MCLBYTES) {
1037 } else if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1038 /* Avoid going past 9K jumbos. */
1039 if (frame_size > MJUM9BYTES)
1041 clsize = MJUM9BYTES;
1043 clsize = MJUMPAGESIZE;
1045 ifp->if_mtu = new_mtu;
1046 sc->vtnet_rx_new_clsize = clsize;
1048 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1049 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1050 vtnet_init_locked(sc);
1057 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1059 struct vtnet_softc *sc;
1061 int reinit, mask, error;
1064 ifr = (struct ifreq *) data;
1069 if (ifp->if_mtu != ifr->ifr_mtu) {
1070 VTNET_CORE_LOCK(sc);
1071 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
1072 VTNET_CORE_UNLOCK(sc);
1077 VTNET_CORE_LOCK(sc);
1078 if ((ifp->if_flags & IFF_UP) == 0) {
1079 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1081 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1082 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
1083 (IFF_PROMISC | IFF_ALLMULTI)) {
1084 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1085 vtnet_rx_filter(sc);
1087 ifp->if_flags |= IFF_PROMISC;
1088 if ((ifp->if_flags ^ sc->vtnet_if_flags)
1094 vtnet_init_locked(sc);
1097 sc->vtnet_if_flags = ifp->if_flags;
1098 VTNET_CORE_UNLOCK(sc);
1103 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
1105 VTNET_CORE_LOCK(sc);
1106 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1107 vtnet_rx_filter_mac(sc);
1108 VTNET_CORE_UNLOCK(sc);
1113 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1117 VTNET_CORE_LOCK(sc);
1118 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1120 if (mask & IFCAP_TXCSUM)
1121 ifp->if_capenable ^= IFCAP_TXCSUM;
1122 if (mask & IFCAP_TXCSUM_IPV6)
1123 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
1124 if (mask & IFCAP_TSO4)
1125 ifp->if_capenable ^= IFCAP_TSO4;
1126 if (mask & IFCAP_TSO6)
1127 ifp->if_capenable ^= IFCAP_TSO6;
1129 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO |
1130 IFCAP_VLAN_HWFILTER)) {
1131 /* These Rx features require us to renegotiate. */
1134 if (mask & IFCAP_RXCSUM)
1135 ifp->if_capenable ^= IFCAP_RXCSUM;
1136 if (mask & IFCAP_RXCSUM_IPV6)
1137 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
1138 if (mask & IFCAP_LRO)
1139 ifp->if_capenable ^= IFCAP_LRO;
1140 if (mask & IFCAP_VLAN_HWFILTER)
1141 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1145 if (mask & IFCAP_VLAN_HWTSO)
1146 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1147 if (mask & IFCAP_VLAN_HWTAGGING)
1148 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1150 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1151 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1152 vtnet_init_locked(sc);
1155 VTNET_CORE_UNLOCK(sc);
1156 VLAN_CAPABILITIES(ifp);
1161 error = ether_ioctl(ifp, cmd, data);
1165 VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc);
1171 vtnet_rxq_populate(struct vtnet_rxq *rxq)
1173 struct virtqueue *vq;
1179 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1180 error = vtnet_rxq_new_buf(rxq);
1186 virtqueue_notify(vq);
1188 * EMSGSIZE signifies the virtqueue did not have enough
1189 * entries available to hold the last mbuf. This is not
1192 if (error == EMSGSIZE)
1200 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq)
1202 struct virtqueue *vq;
1209 while ((m = virtqueue_drain(vq, &last)) != NULL)
1212 KASSERT(virtqueue_empty(vq),
1213 ("%s: mbufs remaining in rx queue %p", __func__, rxq));
1216 static struct mbuf *
1217 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1219 struct mbuf *m_head, *m_tail, *m;
1222 clsize = sc->vtnet_rx_clsize;
1224 KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1225 ("%s: chained mbuf %d request without LRO_NOMRG", __func__, nbufs));
1227 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize);
1231 m_head->m_len = clsize;
1234 /* Allocate the rest of the chain. */
1235 for (i = 1; i < nbufs; i++) {
1236 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize);
1245 if (m_tailp != NULL)
1251 sc->vtnet_stats.mbuf_alloc_failed++;
1258 * Slow path for when LRO without mergeable buffers is negotiated.
1261 vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *rxq, struct mbuf *m0,
1264 struct vtnet_softc *sc;
1265 struct mbuf *m, *m_prev;
1266 struct mbuf *m_new, *m_tail;
1267 int len, clsize, nreplace, error;
1270 clsize = sc->vtnet_rx_clsize;
1280 * Since these mbuf chains are so large, we avoid allocating an
1281 * entire replacement chain if possible. When the received frame
1282 * did not consume the entire chain, the unused mbufs are moved
1283 * to the replacement chain.
1287 * Something is seriously wrong if we received a frame
1288 * larger than the chain. Drop it.
1291 sc->vtnet_stats.rx_frame_too_large++;
1295 /* We always allocate the same cluster size. */
1296 KASSERT(m->m_len == clsize,
1297 ("%s: mbuf size %d is not the cluster size %d",
1298 __func__, m->m_len, clsize));
1300 m->m_len = MIN(m->m_len, len);
1308 KASSERT(nreplace <= sc->vtnet_rx_nmbufs,
1309 ("%s: too many replacement mbufs %d max %d", __func__, nreplace,
1310 sc->vtnet_rx_nmbufs));
1312 m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail);
1313 if (m_new == NULL) {
1314 m_prev->m_len = clsize;
1319 * Move any unused mbufs from the received chain onto the end
1322 if (m_prev->m_next != NULL) {
1323 m_tail->m_next = m_prev->m_next;
1324 m_prev->m_next = NULL;
1327 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1330 * BAD! We could not enqueue the replacement mbuf chain. We
1331 * must restore the m0 chain to the original state if it was
1332 * modified so we can subsequently discard it.
1334 * NOTE: The replacement is suppose to be an identical copy
1335 * to the one just dequeued so this is an unexpected error.
1337 sc->vtnet_stats.rx_enq_replacement_failed++;
1339 if (m_tail->m_next != NULL) {
1340 m_prev->m_next = m_tail->m_next;
1341 m_tail->m_next = NULL;
1344 m_prev->m_len = clsize;
1352 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len)
1354 struct vtnet_softc *sc;
1360 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1361 ("%s: chained mbuf without LRO_NOMRG", __func__));
1363 if (m->m_next == NULL) {
1364 /* Fast-path for the common case of just one mbuf. */
1368 m_new = vtnet_rx_alloc_buf(sc, 1, NULL);
1372 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1375 * The new mbuf is suppose to be an identical
1376 * copy of the one just dequeued so this is an
1380 sc->vtnet_stats.rx_enq_replacement_failed++;
1384 error = vtnet_rxq_replace_lro_nomgr_buf(rxq, m, len);
1390 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1392 struct vtnet_softc *sc;
1394 struct vtnet_rx_header *rxhdr;
1400 mdata = mtod(m, uint8_t *);
1402 VTNET_RXQ_LOCK_ASSERT(rxq);
1403 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1404 ("%s: chained mbuf without LRO_NOMRG", __func__));
1405 KASSERT(m->m_len == sc->vtnet_rx_clsize,
1406 ("%s: unexpected cluster size %d/%d", __func__, m->m_len,
1407 sc->vtnet_rx_clsize));
1410 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1411 MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr));
1412 rxhdr = (struct vtnet_rx_header *) mdata;
1413 sglist_append(sg, &rxhdr->vrh_hdr, sc->vtnet_hdr_size);
1414 offset = sizeof(struct vtnet_rx_header);
1418 sglist_append(sg, mdata + offset, m->m_len - offset);
1419 if (m->m_next != NULL) {
1420 error = sglist_append_mbuf(sg, m->m_next);
1424 error = virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg);
1430 vtnet_rxq_new_buf(struct vtnet_rxq *rxq)
1432 struct vtnet_softc *sc;
1438 m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL);
1442 error = vtnet_rxq_enqueue_buf(rxq, m);
1450 * Use the checksum offset in the VirtIO header to set the
1451 * correct CSUM_* flags.
1454 vtnet_rxq_csum_by_offset(struct vtnet_rxq *rxq, struct mbuf *m,
1455 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1457 struct vtnet_softc *sc;
1458 #if defined(INET) || defined(INET6)
1459 int offset = hdr->csum_start + hdr->csum_offset;
1464 /* Only do a basic sanity check on the offset. */
1468 if (__predict_false(offset < ip_start + sizeof(struct ip)))
1473 case ETHERTYPE_IPV6:
1474 if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
1479 sc->vtnet_stats.rx_csum_bad_ethtype++;
1484 * Use the offset to determine the appropriate CSUM_* flags. This is
1485 * a bit dirty, but we can get by with it since the checksum offsets
1486 * happen to be different. We assume the host host does not do IPv4
1487 * header checksum offloading.
1489 switch (hdr->csum_offset) {
1490 case offsetof(struct udphdr, uh_sum):
1491 case offsetof(struct tcphdr, th_sum):
1492 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1493 m->m_pkthdr.csum_data = 0xFFFF;
1495 case offsetof(struct sctphdr, checksum):
1496 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1499 sc->vtnet_stats.rx_csum_bad_offset++;
1507 vtnet_rxq_csum_by_parse(struct vtnet_rxq *rxq, struct mbuf *m,
1508 uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1510 struct vtnet_softc *sc;
1517 case ETHERTYPE_IP: {
1519 if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
1521 ip = (struct ip *)(m->m_data + ip_start);
1523 offset = ip_start + (ip->ip_hl << 2);
1528 case ETHERTYPE_IPV6:
1529 if (__predict_false(m->m_len < ip_start +
1530 sizeof(struct ip6_hdr)))
1532 offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
1533 if (__predict_false(offset < 0))
1538 sc->vtnet_stats.rx_csum_bad_ethtype++;
1544 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
1546 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1547 m->m_pkthdr.csum_data = 0xFFFF;
1550 if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
1552 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1553 m->m_pkthdr.csum_data = 0xFFFF;
1556 if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
1558 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1562 * For the remaining protocols, FreeBSD does not support
1563 * checksum offloading, so the checksum will be recomputed.
1566 if_printf(sc->vtnet_ifp, "cksum offload of unsupported "
1567 "protocol eth_type=%#x proto=%d csum_start=%d "
1568 "csum_offset=%d\n", __func__, eth_type, proto,
1569 hdr->csum_start, hdr->csum_offset);
1578 * Set the appropriate CSUM_* flags. Unfortunately, the information
1579 * provided is not directly useful to us. The VirtIO header gives the
1580 * offset of the checksum, which is all Linux needs, but this is not
1581 * how FreeBSD does things. We are forced to peek inside the packet
1584 * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
1585 * could accept the offsets and let the stack figure it out.
1588 vtnet_rxq_csum(struct vtnet_rxq *rxq, struct mbuf *m,
1589 struct virtio_net_hdr *hdr)
1591 struct ether_header *eh;
1592 struct ether_vlan_header *evh;
1596 eh = mtod(m, struct ether_header *);
1597 eth_type = ntohs(eh->ether_type);
1598 if (eth_type == ETHERTYPE_VLAN) {
1599 /* BMV: We should handle nested VLAN tags too. */
1600 evh = mtod(m, struct ether_vlan_header *);
1601 eth_type = ntohs(evh->evl_proto);
1602 offset = sizeof(struct ether_vlan_header);
1604 offset = sizeof(struct ether_header);
1606 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1607 error = vtnet_rxq_csum_by_offset(rxq, m, eth_type, offset, hdr);
1609 error = vtnet_rxq_csum_by_parse(rxq, m, eth_type, offset, hdr);
1615 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs)
1619 while (--nbufs > 0) {
1620 m = virtqueue_dequeue(rxq->vtnrx_vq, NULL);
1623 vtnet_rxq_discard_buf(rxq, m);
1628 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1633 * Requeue the discarded mbuf. This should always be successful
1634 * since it was just dequeued.
1636 error = vtnet_rxq_enqueue_buf(rxq, m);
1638 ("%s: cannot requeue discarded mbuf %d", __func__, error));
1642 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs)
1644 struct vtnet_softc *sc;
1645 struct virtqueue *vq;
1646 struct mbuf *m, *m_tail;
1653 while (--nbufs > 0) {
1654 m = virtqueue_dequeue(vq, &len);
1656 rxq->vtnrx_stats.vrxs_ierrors++;
1660 if (vtnet_rxq_new_buf(rxq) != 0) {
1661 rxq->vtnrx_stats.vrxs_iqdrops++;
1662 vtnet_rxq_discard_buf(rxq, m);
1664 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1672 m->m_flags &= ~M_PKTHDR;
1674 m_head->m_pkthdr.len += len;
1682 sc->vtnet_stats.rx_mergeable_failed++;
1689 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m,
1690 struct virtio_net_hdr *hdr)
1692 struct vtnet_softc *sc;
1694 struct ether_header *eh;
1697 ifp = sc->vtnet_ifp;
1699 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1700 eh = mtod(m, struct ether_header *);
1701 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1702 vtnet_vlan_tag_remove(m);
1704 * With the 802.1Q header removed, update the
1705 * checksum starting location accordingly.
1707 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1708 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN;
1712 m->m_pkthdr.flowid = rxq->vtnrx_id;
1713 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
1716 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum
1717 * distinction that Linux does. Need to reevaluate if performing
1718 * offloading for the NEEDS_CSUM case is really appropriate.
1720 if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM |
1721 VIRTIO_NET_HDR_F_DATA_VALID)) {
1722 if (vtnet_rxq_csum(rxq, m, hdr) == 0)
1723 rxq->vtnrx_stats.vrxs_csum++;
1725 rxq->vtnrx_stats.vrxs_csum_failed++;
1728 rxq->vtnrx_stats.vrxs_ipackets++;
1729 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len;
1731 VTNET_RXQ_UNLOCK(rxq);
1732 (*ifp->if_input)(ifp, m);
1733 VTNET_RXQ_LOCK(rxq);
1737 vtnet_rxq_eof(struct vtnet_rxq *rxq)
1739 struct virtio_net_hdr lhdr, *hdr;
1740 struct vtnet_softc *sc;
1742 struct virtqueue *vq;
1744 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1745 int len, deq, nbufs, adjsz, count;
1749 ifp = sc->vtnet_ifp;
1752 count = sc->vtnet_rx_process_limit;
1754 VTNET_RXQ_LOCK_ASSERT(rxq);
1757 if (netmap_rx_irq(ifp, 0, &deq)) {
1760 #endif /* DEV_NETMAP */
1762 while (count-- > 0) {
1763 m = virtqueue_dequeue(vq, &len);
1768 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1769 rxq->vtnrx_stats.vrxs_ierrors++;
1770 vtnet_rxq_discard_buf(rxq, m);
1774 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1776 adjsz = sizeof(struct vtnet_rx_header);
1778 * Account for our pad inserted between the header
1779 * and the actual start of the frame.
1781 len += VTNET_RX_HEADER_PAD;
1783 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1784 nbufs = mhdr->num_buffers;
1785 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1788 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) {
1789 rxq->vtnrx_stats.vrxs_iqdrops++;
1790 vtnet_rxq_discard_buf(rxq, m);
1792 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1796 m->m_pkthdr.len = len;
1797 m->m_pkthdr.rcvif = ifp;
1798 m->m_pkthdr.csum_flags = 0;
1801 /* Dequeue the rest of chain. */
1802 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0)
1807 * Save copy of header before we strip it. For both mergeable
1808 * and non-mergeable, the header is at the beginning of the
1809 * mbuf data. We no longer need num_buffers, so always use a
1812 * BMV: Is this memcpy() expensive? We know the mbuf data is
1813 * still valid even after the m_adj().
1815 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1818 vtnet_rxq_input(rxq, m, hdr);
1820 /* Must recheck after dropping the Rx lock. */
1821 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1826 virtqueue_notify(vq);
1828 return (count > 0 ? 0 : EAGAIN);
1832 vtnet_rx_vq_intr(void *xrxq)
1834 struct vtnet_softc *sc;
1835 struct vtnet_rxq *rxq;
1841 ifp = sc->vtnet_ifp;
1844 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) {
1846 * Ignore this interrupt. Either this is a spurious interrupt
1847 * or multiqueue without per-VQ MSIX so every queue needs to
1848 * be polled (a brain dead configuration we could try harder
1851 vtnet_rxq_disable_intr(rxq);
1855 VTNET_RXQ_LOCK(rxq);
1858 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1859 VTNET_RXQ_UNLOCK(rxq);
1863 more = vtnet_rxq_eof(rxq);
1864 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1866 vtnet_rxq_disable_intr(rxq);
1868 * This is an occasional condition or race (when !more),
1869 * so retry a few times before scheduling the taskqueue.
1871 if (tries++ < VTNET_INTR_DISABLE_RETRIES)
1874 VTNET_RXQ_UNLOCK(rxq);
1875 rxq->vtnrx_stats.vrxs_rescheduled++;
1876 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1878 VTNET_RXQ_UNLOCK(rxq);
1882 vtnet_rxq_tq_intr(void *xrxq, int pending)
1884 struct vtnet_softc *sc;
1885 struct vtnet_rxq *rxq;
1891 ifp = sc->vtnet_ifp;
1893 VTNET_RXQ_LOCK(rxq);
1895 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1896 VTNET_RXQ_UNLOCK(rxq);
1900 more = vtnet_rxq_eof(rxq);
1901 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1903 vtnet_rxq_disable_intr(rxq);
1904 rxq->vtnrx_stats.vrxs_rescheduled++;
1905 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1908 VTNET_RXQ_UNLOCK(rxq);
1912 vtnet_txq_below_threshold(struct vtnet_txq *txq)
1914 struct vtnet_softc *sc;
1915 struct virtqueue *vq;
1920 return (virtqueue_nfree(vq) <= sc->vtnet_tx_intr_thresh);
1924 vtnet_txq_notify(struct vtnet_txq *txq)
1926 struct virtqueue *vq;
1930 txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
1931 virtqueue_notify(vq);
1933 if (vtnet_txq_enable_intr(txq) == 0)
1937 * Drain frames that were completed since last checked. If this
1938 * causes the queue to go above the threshold, the caller should
1939 * continue transmitting.
1941 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) {
1942 virtqueue_disable_intr(vq);
1950 vtnet_txq_free_mbufs(struct vtnet_txq *txq)
1952 struct virtqueue *vq;
1953 struct vtnet_tx_header *txhdr;
1959 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1960 m_freem(txhdr->vth_mbuf);
1961 uma_zfree(vtnet_tx_header_zone, txhdr);
1964 KASSERT(virtqueue_empty(vq),
1965 ("%s: mbufs remaining in tx queue %p", __func__, txq));
1969 * BMV: Much of this can go away once we finally have offsets in
1970 * the mbuf packet header. Bug andre@.
1973 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m,
1974 int *etype, int *proto, int *start)
1976 struct vtnet_softc *sc;
1977 struct ether_vlan_header *evh;
1982 evh = mtod(m, struct ether_vlan_header *);
1983 if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1984 /* BMV: We should handle nested VLAN tags too. */
1985 *etype = ntohs(evh->evl_proto);
1986 offset = sizeof(struct ether_vlan_header);
1988 *etype = ntohs(evh->evl_encap_proto);
1989 offset = sizeof(struct ether_header);
1994 case ETHERTYPE_IP: {
1995 struct ip *ip, iphdr;
1996 if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
1997 m_copydata(m, offset, sizeof(struct ip),
2001 ip = (struct ip *)(m->m_data + offset);
2003 *start = offset + (ip->ip_hl << 2);
2008 case ETHERTYPE_IPV6:
2010 *start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
2011 /* Assert the network stack sent us a valid packet. */
2012 KASSERT(*start > offset,
2013 ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
2014 *start, offset, *proto));
2018 sc->vtnet_stats.tx_csum_bad_ethtype++;
2026 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type,
2027 int offset, struct virtio_net_hdr *hdr)
2029 static struct timeval lastecn;
2031 struct vtnet_softc *sc;
2032 struct tcphdr *tcp, tcphdr;
2036 if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
2037 m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
2040 tcp = (struct tcphdr *)(m->m_data + offset);
2042 hdr->hdr_len = offset + (tcp->th_off << 2);
2043 hdr->gso_size = m->m_pkthdr.tso_segsz;
2044 hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
2045 VIRTIO_NET_HDR_GSO_TCPV6;
2047 if (tcp->th_flags & TH_CWR) {
2049 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
2050 * ECN support is not on a per-interface basis, but globally via
2051 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
2053 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
2054 if (ppsratecheck(&lastecn, &curecn, 1))
2055 if_printf(sc->vtnet_ifp,
2056 "TSO with ECN not negotiated with host\n");
2059 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2062 txq->vtntx_stats.vtxs_tso++;
2067 static struct mbuf *
2068 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m,
2069 struct virtio_net_hdr *hdr)
2071 struct vtnet_softc *sc;
2072 int flags, etype, csum_start, proto, error;
2075 flags = m->m_pkthdr.csum_flags;
2077 error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start);
2081 if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) ||
2082 (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) {
2084 * We could compare the IP protocol vs the CSUM_ flag too,
2085 * but that really should not be necessary.
2087 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
2088 hdr->csum_start = csum_start;
2089 hdr->csum_offset = m->m_pkthdr.csum_data;
2090 txq->vtntx_stats.vtxs_csum++;
2093 if (flags & CSUM_TSO) {
2094 if (__predict_false(proto != IPPROTO_TCP)) {
2095 /* Likely failed to correctly parse the mbuf. */
2096 sc->vtnet_stats.tx_tso_not_tcp++;
2100 KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
2101 ("%s: mbuf %p TSO without checksum offload %#x",
2102 __func__, m, flags));
2104 error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr);
2117 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head,
2118 struct vtnet_tx_header *txhdr)
2120 struct vtnet_softc *sc;
2121 struct virtqueue *vq;
2132 error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
2133 KASSERT(error == 0 && sg->sg_nseg == 1,
2134 ("%s: error %d adding header to sglist", __func__, error));
2136 error = sglist_append_mbuf(sg, m);
2138 m = m_defrag(m, M_NOWAIT);
2143 sc->vtnet_stats.tx_defragged++;
2145 error = sglist_append_mbuf(sg, m);
2150 txhdr->vth_mbuf = m;
2151 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0);
2156 sc->vtnet_stats.tx_defrag_failed++;
2164 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head)
2166 struct vtnet_tx_header *txhdr;
2167 struct virtio_net_hdr *hdr;
2174 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO);
2175 if (txhdr == NULL) {
2182 * Always use the non-mergeable header, regardless if the feature
2183 * was negotiated. For transmit, num_buffers is always zero. The
2184 * vtnet_hdr_size is used to enqueue the correct header size.
2186 hdr = &txhdr->vth_uhdr.hdr;
2188 if (m->m_flags & M_VLANTAG) {
2189 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2190 if ((*m_head = m) == NULL) {
2194 m->m_flags &= ~M_VLANTAG;
2197 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) {
2198 m = vtnet_txq_offload(txq, m, hdr);
2199 if ((*m_head = m) == NULL) {
2205 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr);
2210 uma_zfree(vtnet_tx_header_zone, txhdr);
2215 #ifdef VTNET_LEGACY_TX
2218 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp)
2220 struct vtnet_softc *sc;
2221 struct virtqueue *vq;
2229 VTNET_TXQ_LOCK_ASSERT(txq);
2231 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2232 sc->vtnet_link_active == 0)
2240 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2241 if (virtqueue_full(vq))
2244 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2248 if (vtnet_txq_encap(txq, &m0) != 0) {
2250 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2255 ETHER_BPF_MTAP(ifp, m0);
2258 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2259 if (tries++ < VTNET_NOTIFY_RETRIES)
2262 txq->vtntx_stats.vtxs_rescheduled++;
2263 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2268 vtnet_start(struct ifnet *ifp)
2270 struct vtnet_softc *sc;
2271 struct vtnet_txq *txq;
2274 txq = &sc->vtnet_txqs[0];
2276 VTNET_TXQ_LOCK(txq);
2277 vtnet_start_locked(txq, ifp);
2278 VTNET_TXQ_UNLOCK(txq);
2281 #else /* !VTNET_LEGACY_TX */
2284 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m)
2286 struct vtnet_softc *sc;
2287 struct virtqueue *vq;
2288 struct buf_ring *br;
2290 int enq, tries, error;
2295 ifp = sc->vtnet_ifp;
2299 VTNET_TXQ_LOCK_ASSERT(txq);
2301 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2302 sc->vtnet_link_active == 0) {
2304 error = drbr_enqueue(ifp, br, m);
2309 error = drbr_enqueue(ifp, br, m);
2319 while ((m = drbr_peek(ifp, br)) != NULL) {
2320 if (virtqueue_full(vq)) {
2321 drbr_putback(ifp, br, m);
2325 if (vtnet_txq_encap(txq, &m) != 0) {
2327 drbr_putback(ifp, br, m);
2329 drbr_advance(ifp, br);
2332 drbr_advance(ifp, br);
2335 ETHER_BPF_MTAP(ifp, m);
2338 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2339 if (tries++ < VTNET_NOTIFY_RETRIES)
2342 txq->vtntx_stats.vtxs_rescheduled++;
2343 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2350 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m)
2352 struct vtnet_softc *sc;
2353 struct vtnet_txq *txq;
2354 int i, npairs, error;
2357 npairs = sc->vtnet_act_vq_pairs;
2359 /* check if flowid is set */
2360 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2361 i = m->m_pkthdr.flowid % npairs;
2363 i = curcpu % npairs;
2365 txq = &sc->vtnet_txqs[i];
2367 if (VTNET_TXQ_TRYLOCK(txq) != 0) {
2368 error = vtnet_txq_mq_start_locked(txq, m);
2369 VTNET_TXQ_UNLOCK(txq);
2371 error = drbr_enqueue(ifp, txq->vtntx_br, m);
2372 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask);
2379 vtnet_txq_tq_deferred(void *xtxq, int pending)
2381 struct vtnet_softc *sc;
2382 struct vtnet_txq *txq;
2387 VTNET_TXQ_LOCK(txq);
2388 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br))
2389 vtnet_txq_mq_start_locked(txq, NULL);
2390 VTNET_TXQ_UNLOCK(txq);
2393 #endif /* VTNET_LEGACY_TX */
2396 vtnet_txq_start(struct vtnet_txq *txq)
2398 struct vtnet_softc *sc;
2402 ifp = sc->vtnet_ifp;
2404 #ifdef VTNET_LEGACY_TX
2405 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2406 vtnet_start_locked(txq, ifp);
2408 if (!drbr_empty(ifp, txq->vtntx_br))
2409 vtnet_txq_mq_start_locked(txq, NULL);
2414 vtnet_txq_tq_intr(void *xtxq, int pending)
2416 struct vtnet_softc *sc;
2417 struct vtnet_txq *txq;
2422 ifp = sc->vtnet_ifp;
2424 VTNET_TXQ_LOCK(txq);
2426 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2427 VTNET_TXQ_UNLOCK(txq);
2432 vtnet_txq_start(txq);
2434 VTNET_TXQ_UNLOCK(txq);
2438 vtnet_txq_eof(struct vtnet_txq *txq)
2440 struct virtqueue *vq;
2441 struct vtnet_tx_header *txhdr;
2447 VTNET_TXQ_LOCK_ASSERT(txq);
2450 if (netmap_tx_irq(txq->vtntx_sc->vtnet_ifp, txq->vtntx_id)) {
2451 virtqueue_disable_intr(vq); // XXX luigi
2452 return 0; // XXX or 1 ?
2454 #endif /* DEV_NETMAP */
2456 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
2457 m = txhdr->vth_mbuf;
2460 txq->vtntx_stats.vtxs_opackets++;
2461 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len;
2462 if (m->m_flags & M_MCAST)
2463 txq->vtntx_stats.vtxs_omcasts++;
2466 uma_zfree(vtnet_tx_header_zone, txhdr);
2469 if (virtqueue_empty(vq))
2470 txq->vtntx_watchdog = 0;
2476 vtnet_tx_vq_intr(void *xtxq)
2478 struct vtnet_softc *sc;
2479 struct vtnet_txq *txq;
2484 ifp = sc->vtnet_ifp;
2486 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) {
2488 * Ignore this interrupt. Either this is a spurious interrupt
2489 * or multiqueue without per-VQ MSIX so every queue needs to
2490 * be polled (a brain dead configuration we could try harder
2493 vtnet_txq_disable_intr(txq);
2497 VTNET_TXQ_LOCK(txq);
2499 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2500 VTNET_TXQ_UNLOCK(txq);
2505 vtnet_txq_start(txq);
2507 VTNET_TXQ_UNLOCK(txq);
2511 vtnet_tx_start_all(struct vtnet_softc *sc)
2513 struct vtnet_txq *txq;
2516 VTNET_CORE_LOCK_ASSERT(sc);
2518 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2519 txq = &sc->vtnet_txqs[i];
2521 VTNET_TXQ_LOCK(txq);
2522 vtnet_txq_start(txq);
2523 VTNET_TXQ_UNLOCK(txq);
2527 #ifndef VTNET_LEGACY_TX
2529 vtnet_qflush(struct ifnet *ifp)
2531 struct vtnet_softc *sc;
2532 struct vtnet_txq *txq;
2538 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2539 txq = &sc->vtnet_txqs[i];
2541 VTNET_TXQ_LOCK(txq);
2542 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL)
2544 VTNET_TXQ_UNLOCK(txq);
2552 vtnet_watchdog(struct vtnet_txq *txq)
2556 ifp = txq->vtntx_sc->vtnet_ifp;
2558 VTNET_TXQ_LOCK(txq);
2559 if (txq->vtntx_watchdog == 1) {
2561 * Only drain completed frames if the watchdog is about to
2562 * expire. If any frames were drained, there may be enough
2563 * free descriptors now available to transmit queued frames.
2564 * In that case, the timer will immediately be decremented
2565 * below, but the timeout is generous enough that should not
2568 if (vtnet_txq_eof(txq) != 0)
2569 vtnet_txq_start(txq);
2572 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) {
2573 VTNET_TXQ_UNLOCK(txq);
2576 VTNET_TXQ_UNLOCK(txq);
2578 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id);
2583 vtnet_accum_stats(struct vtnet_softc *sc, struct vtnet_rxq_stats *rxacc,
2584 struct vtnet_txq_stats *txacc)
2587 bzero(rxacc, sizeof(struct vtnet_rxq_stats));
2588 bzero(txacc, sizeof(struct vtnet_txq_stats));
2590 for (int i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2591 struct vtnet_rxq_stats *rxst;
2592 struct vtnet_txq_stats *txst;
2594 rxst = &sc->vtnet_rxqs[i].vtnrx_stats;
2595 rxacc->vrxs_ipackets += rxst->vrxs_ipackets;
2596 rxacc->vrxs_ibytes += rxst->vrxs_ibytes;
2597 rxacc->vrxs_iqdrops += rxst->vrxs_iqdrops;
2598 rxacc->vrxs_csum += rxst->vrxs_csum;
2599 rxacc->vrxs_csum_failed += rxst->vrxs_csum_failed;
2600 rxacc->vrxs_rescheduled += rxst->vrxs_rescheduled;
2602 txst = &sc->vtnet_txqs[i].vtntx_stats;
2603 txacc->vtxs_opackets += txst->vtxs_opackets;
2604 txacc->vtxs_obytes += txst->vtxs_obytes;
2605 txacc->vtxs_csum += txst->vtxs_csum;
2606 txacc->vtxs_tso += txst->vtxs_tso;
2607 txacc->vtxs_rescheduled += txst->vtxs_rescheduled;
2612 vtnet_get_counter(if_t ifp, ift_counter cnt)
2614 struct vtnet_softc *sc;
2615 struct vtnet_rxq_stats rxaccum;
2616 struct vtnet_txq_stats txaccum;
2618 sc = if_getsoftc(ifp);
2619 vtnet_accum_stats(sc, &rxaccum, &txaccum);
2622 case IFCOUNTER_IPACKETS:
2623 return (rxaccum.vrxs_ipackets);
2624 case IFCOUNTER_IQDROPS:
2625 return (rxaccum.vrxs_iqdrops);
2626 case IFCOUNTER_IERRORS:
2627 return (rxaccum.vrxs_ierrors);
2628 case IFCOUNTER_OPACKETS:
2629 return (txaccum.vtxs_opackets);
2630 #ifndef VTNET_LEGACY_TX
2631 case IFCOUNTER_OBYTES:
2632 return (txaccum.vtxs_obytes);
2633 case IFCOUNTER_OMCASTS:
2634 return (txaccum.vtxs_omcasts);
2637 return (if_get_counter_default(ifp, cnt));
2642 vtnet_tick(void *xsc)
2644 struct vtnet_softc *sc;
2649 ifp = sc->vtnet_ifp;
2652 VTNET_CORE_LOCK_ASSERT(sc);
2654 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
2655 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]);
2657 if (timedout != 0) {
2658 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2659 vtnet_init_locked(sc);
2661 callout_schedule(&sc->vtnet_tick_ch, hz);
2665 vtnet_start_taskqueues(struct vtnet_softc *sc)
2668 struct vtnet_rxq *rxq;
2669 struct vtnet_txq *txq;
2672 dev = sc->vtnet_dev;
2675 * Errors here are very difficult to recover from - we cannot
2676 * easily fail because, if this is during boot, we will hang
2677 * when freeing any successfully started taskqueues because
2678 * the scheduler isn't up yet.
2680 * Most drivers just ignore the return value - it only fails
2681 * with ENOMEM so an error is not likely.
2683 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2684 rxq = &sc->vtnet_rxqs[i];
2685 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET,
2686 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id);
2688 device_printf(dev, "failed to start rx taskq %d\n",
2692 txq = &sc->vtnet_txqs[i];
2693 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET,
2694 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id);
2696 device_printf(dev, "failed to start tx taskq %d\n",
2703 vtnet_free_taskqueues(struct vtnet_softc *sc)
2705 struct vtnet_rxq *rxq;
2706 struct vtnet_txq *txq;
2709 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2710 rxq = &sc->vtnet_rxqs[i];
2711 if (rxq->vtnrx_tq != NULL) {
2712 taskqueue_free(rxq->vtnrx_tq);
2713 rxq->vtnrx_vq = NULL;
2716 txq = &sc->vtnet_txqs[i];
2717 if (txq->vtntx_tq != NULL) {
2718 taskqueue_free(txq->vtntx_tq);
2719 txq->vtntx_tq = NULL;
2725 vtnet_drain_taskqueues(struct vtnet_softc *sc)
2727 struct vtnet_rxq *rxq;
2728 struct vtnet_txq *txq;
2731 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2732 rxq = &sc->vtnet_rxqs[i];
2733 if (rxq->vtnrx_tq != NULL)
2734 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2736 txq = &sc->vtnet_txqs[i];
2737 if (txq->vtntx_tq != NULL) {
2738 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask);
2739 #ifndef VTNET_LEGACY_TX
2740 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask);
2747 vtnet_drain_rxtx_queues(struct vtnet_softc *sc)
2749 struct vtnet_rxq *rxq;
2750 struct vtnet_txq *txq;
2754 if (nm_native_on(NA(sc->vtnet_ifp)))
2756 #endif /* DEV_NETMAP */
2758 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2759 rxq = &sc->vtnet_rxqs[i];
2760 vtnet_rxq_free_mbufs(rxq);
2762 txq = &sc->vtnet_txqs[i];
2763 vtnet_txq_free_mbufs(txq);
2768 vtnet_stop_rendezvous(struct vtnet_softc *sc)
2770 struct vtnet_rxq *rxq;
2771 struct vtnet_txq *txq;
2775 * Lock and unlock the per-queue mutex so we known the stop
2776 * state is visible. Doing only the active queues should be
2777 * sufficient, but it does not cost much extra to do all the
2778 * queues. Note we hold the core mutex here too.
2780 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2781 rxq = &sc->vtnet_rxqs[i];
2782 VTNET_RXQ_LOCK(rxq);
2783 VTNET_RXQ_UNLOCK(rxq);
2785 txq = &sc->vtnet_txqs[i];
2786 VTNET_TXQ_LOCK(txq);
2787 VTNET_TXQ_UNLOCK(txq);
2792 vtnet_stop(struct vtnet_softc *sc)
2797 dev = sc->vtnet_dev;
2798 ifp = sc->vtnet_ifp;
2800 VTNET_CORE_LOCK_ASSERT(sc);
2802 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2803 sc->vtnet_link_active = 0;
2804 callout_stop(&sc->vtnet_tick_ch);
2806 /* Only advisory. */
2807 vtnet_disable_interrupts(sc);
2810 * Stop the host adapter. This resets it to the pre-initialized
2811 * state. It will not generate any interrupts until after it is
2815 vtnet_stop_rendezvous(sc);
2817 /* Free any mbufs left in the virtqueues. */
2818 vtnet_drain_rxtx_queues(sc);
2822 vtnet_virtio_reinit(struct vtnet_softc *sc)
2829 dev = sc->vtnet_dev;
2830 ifp = sc->vtnet_ifp;
2831 features = sc->vtnet_features;
2835 mask |= IFCAP_RXCSUM;
2838 mask |= IFCAP_RXCSUM_IPV6;
2842 * Re-negotiate with the host, removing any disabled receive
2843 * features. Transmit features are disabled only on our side
2844 * via if_capenable and if_hwassist.
2847 if (ifp->if_capabilities & mask) {
2849 * We require both IPv4 and IPv6 offloading to be enabled
2850 * in order to negotiated it: VirtIO does not distinguish
2853 if ((ifp->if_capenable & mask) != mask)
2854 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2857 if (ifp->if_capabilities & IFCAP_LRO) {
2858 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2859 features &= ~VTNET_LRO_FEATURES;
2862 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2863 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2864 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2867 error = virtio_reinit(dev, features);
2869 device_printf(dev, "virtio reinit error %d\n", error);
2875 vtnet_init_rx_filters(struct vtnet_softc *sc)
2879 ifp = sc->vtnet_ifp;
2881 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2882 /* Restore promiscuous and all-multicast modes. */
2883 vtnet_rx_filter(sc);
2884 /* Restore filtered MAC addresses. */
2885 vtnet_rx_filter_mac(sc);
2888 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2889 vtnet_rx_filter_vlan(sc);
2893 vtnet_init_rx_queues(struct vtnet_softc *sc)
2896 struct vtnet_rxq *rxq;
2897 int i, clsize, error;
2899 dev = sc->vtnet_dev;
2902 * Use the new cluster size if one has been set (via a MTU
2903 * change). Otherwise, use the standard 2K clusters.
2905 * BMV: It might make sense to use page sized clusters as
2906 * the default (depending on the features negotiated).
2908 if (sc->vtnet_rx_new_clsize != 0) {
2909 clsize = sc->vtnet_rx_new_clsize;
2910 sc->vtnet_rx_new_clsize = 0;
2914 sc->vtnet_rx_clsize = clsize;
2915 sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize);
2917 KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS ||
2918 sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs,
2919 ("%s: too many rx mbufs %d for %d segments", __func__,
2920 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs));
2923 if (vtnet_netmap_init_rx_buffers(sc))
2925 #endif /* DEV_NETMAP */
2927 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2928 rxq = &sc->vtnet_rxqs[i];
2930 /* Hold the lock to satisfy asserts. */
2931 VTNET_RXQ_LOCK(rxq);
2932 error = vtnet_rxq_populate(rxq);
2933 VTNET_RXQ_UNLOCK(rxq);
2937 "cannot allocate mbufs for Rx queue %d\n", i);
2946 vtnet_init_tx_queues(struct vtnet_softc *sc)
2948 struct vtnet_txq *txq;
2951 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2952 txq = &sc->vtnet_txqs[i];
2953 txq->vtntx_watchdog = 0;
2960 vtnet_init_rxtx_queues(struct vtnet_softc *sc)
2964 error = vtnet_init_rx_queues(sc);
2968 error = vtnet_init_tx_queues(sc);
2976 vtnet_set_active_vq_pairs(struct vtnet_softc *sc)
2981 dev = sc->vtnet_dev;
2983 if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) {
2984 MPASS(sc->vtnet_max_vq_pairs == 1);
2985 sc->vtnet_act_vq_pairs = 1;
2989 /* BMV: Just use the maximum configured for now. */
2990 npairs = sc->vtnet_max_vq_pairs;
2992 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) {
2994 "cannot set active queue pairs to %d\n", npairs);
2998 sc->vtnet_act_vq_pairs = npairs;
3002 vtnet_reinit(struct vtnet_softc *sc)
3007 ifp = sc->vtnet_ifp;
3009 /* Use the current MAC address. */
3010 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
3011 vtnet_set_hwaddr(sc);
3013 vtnet_set_active_vq_pairs(sc);
3015 ifp->if_hwassist = 0;
3016 if (ifp->if_capenable & IFCAP_TXCSUM)
3017 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
3018 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3019 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6;
3020 if (ifp->if_capenable & IFCAP_TSO4)
3021 ifp->if_hwassist |= CSUM_IP_TSO;
3022 if (ifp->if_capenable & IFCAP_TSO6)
3023 ifp->if_hwassist |= CSUM_IP6_TSO;
3025 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
3026 vtnet_init_rx_filters(sc);
3028 error = vtnet_init_rxtx_queues(sc);
3032 vtnet_enable_interrupts(sc);
3033 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3039 vtnet_init_locked(struct vtnet_softc *sc)
3044 dev = sc->vtnet_dev;
3045 ifp = sc->vtnet_ifp;
3047 VTNET_CORE_LOCK_ASSERT(sc);
3049 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3054 /* Reinitialize with the host. */
3055 if (vtnet_virtio_reinit(sc) != 0)
3058 if (vtnet_reinit(sc) != 0)
3061 virtio_reinit_complete(dev);
3063 vtnet_update_link_status(sc);
3064 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
3073 vtnet_init(void *xsc)
3075 struct vtnet_softc *sc;
3080 if (!NA(sc->vtnet_ifp)) {
3081 D("try to attach again");
3082 vtnet_netmap_attach(sc);
3084 #endif /* DEV_NETMAP */
3086 VTNET_CORE_LOCK(sc);
3087 vtnet_init_locked(sc);
3088 VTNET_CORE_UNLOCK(sc);
3092 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
3094 struct virtqueue *vq;
3096 vq = sc->vtnet_ctrl_vq;
3099 * The control virtqueue is only polled and therefore it should
3102 KASSERT(virtqueue_empty(vq),
3103 ("%s: ctrl vq %p not empty", __func__, vq));
3107 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
3108 struct sglist *sg, int readable, int writable)
3110 struct virtqueue *vq;
3112 vq = sc->vtnet_ctrl_vq;
3114 VTNET_CORE_LOCK_ASSERT(sc);
3115 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
3116 ("%s: CTRL_VQ feature not negotiated", __func__));
3118 if (!virtqueue_empty(vq))
3120 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
3124 * Poll for the response, but the command is likely already
3125 * done when we return from the notify.
3127 virtqueue_notify(vq);
3128 virtqueue_poll(vq, NULL);
3132 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
3134 struct virtio_net_ctrl_hdr hdr __aligned(2);
3135 struct sglist_seg segs[3];
3140 hdr.class = VIRTIO_NET_CTRL_MAC;
3141 hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
3142 ack = VIRTIO_NET_ERR;
3144 sglist_init(&sg, 3, segs);
3146 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3147 error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN);
3148 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3149 KASSERT(error == 0 && sg.sg_nseg == 3,
3150 ("%s: error %d adding set MAC msg to sglist", __func__, error));
3152 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3154 return (ack == VIRTIO_NET_OK ? 0 : EIO);
3158 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs)
3160 struct sglist_seg segs[3];
3163 struct virtio_net_ctrl_hdr hdr;
3165 struct virtio_net_ctrl_mq mq;
3171 s.hdr.class = VIRTIO_NET_CTRL_MQ;
3172 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
3173 s.mq.virtqueue_pairs = npairs;
3174 s.ack = VIRTIO_NET_ERR;
3176 sglist_init(&sg, 3, segs);
3178 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3179 error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq));
3180 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3181 KASSERT(error == 0 && sg.sg_nseg == 3,
3182 ("%s: error %d adding MQ message to sglist", __func__, error));
3184 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3186 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3190 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
3192 struct sglist_seg segs[3];
3195 struct virtio_net_ctrl_hdr hdr;
3203 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3204 ("%s: CTRL_RX feature not negotiated", __func__));
3206 s.hdr.class = VIRTIO_NET_CTRL_RX;
3209 s.ack = VIRTIO_NET_ERR;
3211 sglist_init(&sg, 3, segs);
3213 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3214 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
3215 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3216 KASSERT(error == 0 && sg.sg_nseg == 3,
3217 ("%s: error %d adding Rx message to sglist", __func__, error));
3219 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3221 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3225 vtnet_set_promisc(struct vtnet_softc *sc, int on)
3228 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
3232 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
3235 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
3239 * The device defaults to promiscuous mode for backwards compatibility.
3240 * Turn it off at attach time if possible.
3243 vtnet_attach_disable_promisc(struct vtnet_softc *sc)
3247 ifp = sc->vtnet_ifp;
3249 VTNET_CORE_LOCK(sc);
3250 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) {
3251 ifp->if_flags |= IFF_PROMISC;
3252 } else if (vtnet_set_promisc(sc, 0) != 0) {
3253 ifp->if_flags |= IFF_PROMISC;
3254 device_printf(sc->vtnet_dev,
3255 "cannot disable default promiscuous mode\n");
3257 VTNET_CORE_UNLOCK(sc);
3261 vtnet_rx_filter(struct vtnet_softc *sc)
3266 dev = sc->vtnet_dev;
3267 ifp = sc->vtnet_ifp;
3269 VTNET_CORE_LOCK_ASSERT(sc);
3271 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
3272 device_printf(dev, "cannot %s promiscuous mode\n",
3273 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
3275 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
3276 device_printf(dev, "cannot %s all-multicast mode\n",
3277 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
3281 vtnet_rx_filter_mac(struct vtnet_softc *sc)
3283 struct virtio_net_ctrl_hdr hdr __aligned(2);
3284 struct vtnet_mac_filter *filter;
3285 struct sglist_seg segs[4];
3289 struct ifmultiaddr *ifma;
3290 int ucnt, mcnt, promisc, allmulti, error;
3293 ifp = sc->vtnet_ifp;
3294 filter = sc->vtnet_mac_filter;
3300 VTNET_CORE_LOCK_ASSERT(sc);
3301 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3302 ("%s: CTRL_RX feature not negotiated", __func__));
3304 /* Unicast MAC addresses: */
3306 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3307 if (ifa->ifa_addr->sa_family != AF_LINK)
3309 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3310 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
3312 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
3317 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3318 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
3321 if_addr_runlock(ifp);
3324 filter->vmf_unicast.nentries = 0;
3325 if_printf(ifp, "more than %d MAC addresses assigned, "
3326 "falling back to promiscuous mode\n",
3327 VTNET_MAX_MAC_ENTRIES);
3329 filter->vmf_unicast.nentries = ucnt;
3331 /* Multicast MAC addresses: */
3332 if_maddr_rlock(ifp);
3333 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3334 if (ifma->ifma_addr->sa_family != AF_LINK)
3336 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
3341 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3342 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
3345 if_maddr_runlock(ifp);
3347 if (allmulti != 0) {
3348 filter->vmf_multicast.nentries = 0;
3349 if_printf(ifp, "more than %d multicast MAC addresses "
3350 "assigned, falling back to all-multicast mode\n",
3351 VTNET_MAX_MAC_ENTRIES);
3353 filter->vmf_multicast.nentries = mcnt;
3355 if (promisc != 0 && allmulti != 0)
3358 hdr.class = VIRTIO_NET_CTRL_MAC;
3359 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
3360 ack = VIRTIO_NET_ERR;
3362 sglist_init(&sg, 4, segs);
3364 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3365 error |= sglist_append(&sg, &filter->vmf_unicast,
3366 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
3367 error |= sglist_append(&sg, &filter->vmf_multicast,
3368 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
3369 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3370 KASSERT(error == 0 && sg.sg_nseg == 4,
3371 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
3373 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3375 if (ack != VIRTIO_NET_OK)
3376 if_printf(ifp, "error setting host MAC filter table\n");
3379 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
3380 if_printf(ifp, "cannot enable promiscuous mode\n");
3381 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
3382 if_printf(ifp, "cannot enable all-multicast mode\n");
3386 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3388 struct sglist_seg segs[3];
3391 struct virtio_net_ctrl_hdr hdr;
3399 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
3400 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
3402 s.ack = VIRTIO_NET_ERR;
3404 sglist_init(&sg, 3, segs);
3406 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3407 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
3408 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3409 KASSERT(error == 0 && sg.sg_nseg == 3,
3410 ("%s: error %d adding VLAN message to sglist", __func__, error));
3412 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3414 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3418 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
3424 VTNET_CORE_LOCK_ASSERT(sc);
3425 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
3426 ("%s: VLAN_FILTER feature not negotiated", __func__));
3428 /* Enable the filter for each configured VLAN. */
3429 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) {
3430 w = sc->vtnet_vlan_filter[i];
3432 while ((bit = ffs(w) - 1) != -1) {
3434 tag = sizeof(w) * CHAR_BIT * i + bit;
3436 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
3437 device_printf(sc->vtnet_dev,
3438 "cannot enable VLAN %d filter\n", tag);
3445 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3450 ifp = sc->vtnet_ifp;
3451 idx = (tag >> 5) & 0x7F;
3454 if (tag == 0 || tag > 4095)
3457 VTNET_CORE_LOCK(sc);
3460 sc->vtnet_vlan_filter[idx] |= (1 << bit);
3462 sc->vtnet_vlan_filter[idx] &= ~(1 << bit);
3464 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
3465 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
3466 device_printf(sc->vtnet_dev,
3467 "cannot %s VLAN %d %s the host filter table\n",
3468 add ? "add" : "remove", tag, add ? "to" : "from");
3471 VTNET_CORE_UNLOCK(sc);
3475 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3478 if (ifp->if_softc != arg)
3481 vtnet_update_vlan_filter(arg, 1, tag);
3485 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3488 if (ifp->if_softc != arg)
3491 vtnet_update_vlan_filter(arg, 0, tag);
3495 vtnet_is_link_up(struct vtnet_softc *sc)
3501 dev = sc->vtnet_dev;
3502 ifp = sc->vtnet_ifp;
3504 if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0)
3505 status = VIRTIO_NET_S_LINK_UP;
3507 status = virtio_read_dev_config_2(dev,
3508 offsetof(struct virtio_net_config, status));
3510 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
3514 vtnet_update_link_status(struct vtnet_softc *sc)
3519 ifp = sc->vtnet_ifp;
3521 VTNET_CORE_LOCK_ASSERT(sc);
3522 link = vtnet_is_link_up(sc);
3524 /* Notify if the link status has changed. */
3525 if (link != 0 && sc->vtnet_link_active == 0) {
3526 sc->vtnet_link_active = 1;
3527 if_link_state_change(ifp, LINK_STATE_UP);
3528 } else if (link == 0 && sc->vtnet_link_active != 0) {
3529 sc->vtnet_link_active = 0;
3530 if_link_state_change(ifp, LINK_STATE_DOWN);
3535 vtnet_ifmedia_upd(struct ifnet *ifp)
3537 struct vtnet_softc *sc;
3538 struct ifmedia *ifm;
3541 ifm = &sc->vtnet_media;
3543 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3550 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3552 struct vtnet_softc *sc;
3556 ifmr->ifm_status = IFM_AVALID;
3557 ifmr->ifm_active = IFM_ETHER;
3559 VTNET_CORE_LOCK(sc);
3560 if (vtnet_is_link_up(sc) != 0) {
3561 ifmr->ifm_status |= IFM_ACTIVE;
3562 ifmr->ifm_active |= VTNET_MEDIATYPE;
3564 ifmr->ifm_active |= IFM_NONE;
3565 VTNET_CORE_UNLOCK(sc);
3569 vtnet_set_hwaddr(struct vtnet_softc *sc)
3574 dev = sc->vtnet_dev;
3576 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) {
3577 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
3578 device_printf(dev, "unable to set MAC address\n");
3579 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
3580 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3581 virtio_write_dev_config_1(dev,
3582 offsetof(struct virtio_net_config, mac) + i,
3583 sc->vtnet_hwaddr[i]);
3589 vtnet_get_hwaddr(struct vtnet_softc *sc)
3594 dev = sc->vtnet_dev;
3596 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
3598 * Generate a random locally administered unicast address.
3600 * It would be nice to generate the same MAC address across
3601 * reboots, but it seems all the hosts currently available
3602 * support the MAC feature, so this isn't too important.
3604 sc->vtnet_hwaddr[0] = 0xB2;
3605 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
3606 vtnet_set_hwaddr(sc);
3610 for (i = 0; i < ETHER_ADDR_LEN; i++) {
3611 sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev,
3612 offsetof(struct virtio_net_config, mac) + i);
3617 vtnet_vlan_tag_remove(struct mbuf *m)
3619 struct ether_vlan_header *evh;
3621 evh = mtod(m, struct ether_vlan_header *);
3622 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
3623 m->m_flags |= M_VLANTAG;
3625 /* Strip the 802.1Q header. */
3626 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
3627 ETHER_HDR_LEN - ETHER_TYPE_LEN);
3628 m_adj(m, ETHER_VLAN_ENCAP_LEN);
3632 vtnet_set_rx_process_limit(struct vtnet_softc *sc)
3636 limit = vtnet_tunable_int(sc, "rx_process_limit",
3637 vtnet_rx_process_limit);
3640 sc->vtnet_rx_process_limit = limit;
3644 vtnet_set_tx_intr_threshold(struct vtnet_softc *sc)
3648 size = virtqueue_size(sc->vtnet_txqs[0].vtntx_vq);
3651 * The Tx interrupt is disabled until the queue free count falls
3652 * below our threshold. Completed frames are drained from the Tx
3653 * virtqueue before transmitting new frames and in the watchdog
3654 * callout, so the frequency of Tx interrupts is greatly reduced,
3655 * at the cost of not freeing mbufs as quickly as they otherwise
3658 * N.B. We assume all the Tx queues are the same size.
3663 * Without indirect descriptors, leave enough room for the most
3664 * segments we handle.
3666 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 &&
3667 thresh < sc->vtnet_tx_nsegs)
3668 thresh = sc->vtnet_tx_nsegs;
3670 sc->vtnet_tx_intr_thresh = thresh;
3674 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx,
3675 struct sysctl_oid_list *child, struct vtnet_rxq *rxq)
3677 struct sysctl_oid *node;
3678 struct sysctl_oid_list *list;
3679 struct vtnet_rxq_stats *stats;
3682 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id);
3683 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3684 CTLFLAG_RD, NULL, "Receive Queue");
3685 list = SYSCTL_CHILDREN(node);
3687 stats = &rxq->vtnrx_stats;
3689 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD,
3690 &stats->vrxs_ipackets, "Receive packets");
3691 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD,
3692 &stats->vrxs_ibytes, "Receive bytes");
3693 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD,
3694 &stats->vrxs_iqdrops, "Receive drops");
3695 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD,
3696 &stats->vrxs_ierrors, "Receive errors");
3697 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3698 &stats->vrxs_csum, "Receive checksum offloaded");
3699 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD,
3700 &stats->vrxs_csum_failed, "Receive checksum offload failed");
3701 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3702 &stats->vrxs_rescheduled,
3703 "Receive interrupt handler rescheduled");
3707 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx,
3708 struct sysctl_oid_list *child, struct vtnet_txq *txq)
3710 struct sysctl_oid *node;
3711 struct sysctl_oid_list *list;
3712 struct vtnet_txq_stats *stats;
3715 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id);
3716 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3717 CTLFLAG_RD, NULL, "Transmit Queue");
3718 list = SYSCTL_CHILDREN(node);
3720 stats = &txq->vtntx_stats;
3722 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD,
3723 &stats->vtxs_opackets, "Transmit packets");
3724 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD,
3725 &stats->vtxs_obytes, "Transmit bytes");
3726 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD,
3727 &stats->vtxs_omcasts, "Transmit multicasts");
3728 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3729 &stats->vtxs_csum, "Transmit checksum offloaded");
3730 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD,
3731 &stats->vtxs_tso, "Transmit segmentation offloaded");
3732 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3733 &stats->vtxs_rescheduled,
3734 "Transmit interrupt handler rescheduled");
3738 vtnet_setup_queue_sysctl(struct vtnet_softc *sc)
3741 struct sysctl_ctx_list *ctx;
3742 struct sysctl_oid *tree;
3743 struct sysctl_oid_list *child;
3746 dev = sc->vtnet_dev;
3747 ctx = device_get_sysctl_ctx(dev);
3748 tree = device_get_sysctl_tree(dev);
3749 child = SYSCTL_CHILDREN(tree);
3751 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3752 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]);
3753 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]);
3758 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx,
3759 struct sysctl_oid_list *child, struct vtnet_softc *sc)
3761 struct vtnet_statistics *stats;
3762 struct vtnet_rxq_stats rxaccum;
3763 struct vtnet_txq_stats txaccum;
3765 vtnet_accum_stats(sc, &rxaccum, &txaccum);
3767 stats = &sc->vtnet_stats;
3768 stats->rx_csum_offloaded = rxaccum.vrxs_csum;
3769 stats->rx_csum_failed = rxaccum.vrxs_csum_failed;
3770 stats->rx_task_rescheduled = rxaccum.vrxs_rescheduled;
3771 stats->tx_csum_offloaded = txaccum.vtxs_csum;
3772 stats->tx_tso_offloaded = txaccum.vtxs_tso;
3773 stats->tx_task_rescheduled = txaccum.vtxs_rescheduled;
3775 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
3776 CTLFLAG_RD, &stats->mbuf_alloc_failed,
3777 "Mbuf cluster allocation failures");
3779 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
3780 CTLFLAG_RD, &stats->rx_frame_too_large,
3781 "Received frame larger than the mbuf chain");
3782 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
3783 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
3784 "Enqueuing the replacement receive mbuf failed");
3785 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
3786 CTLFLAG_RD, &stats->rx_mergeable_failed,
3787 "Mergeable buffers receive failures");
3788 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
3789 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
3790 "Received checksum offloaded buffer with unsupported "
3792 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
3793 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
3794 "Received checksum offloaded buffer with incorrect IP protocol");
3795 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
3796 CTLFLAG_RD, &stats->rx_csum_bad_offset,
3797 "Received checksum offloaded buffer with incorrect offset");
3798 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto",
3799 CTLFLAG_RD, &stats->rx_csum_bad_proto,
3800 "Received checksum offloaded buffer with incorrect protocol");
3801 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
3802 CTLFLAG_RD, &stats->rx_csum_failed,
3803 "Received buffer checksum offload failed");
3804 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
3805 CTLFLAG_RD, &stats->rx_csum_offloaded,
3806 "Received buffer checksum offload succeeded");
3807 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
3808 CTLFLAG_RD, &stats->rx_task_rescheduled,
3809 "Times the receive interrupt task rescheduled itself");
3811 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
3812 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
3813 "Aborted transmit of checksum offloaded buffer with unknown "
3815 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
3816 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
3817 "Aborted transmit of TSO buffer with unknown Ethernet type");
3818 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp",
3819 CTLFLAG_RD, &stats->tx_tso_not_tcp,
3820 "Aborted transmit of TSO buffer with non TCP protocol");
3821 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
3822 CTLFLAG_RD, &stats->tx_defragged,
3823 "Transmit mbufs defragged");
3824 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
3825 CTLFLAG_RD, &stats->tx_defrag_failed,
3826 "Aborted transmit of buffer because defrag failed");
3827 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
3828 CTLFLAG_RD, &stats->tx_csum_offloaded,
3829 "Offloaded checksum of transmitted buffer");
3830 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
3831 CTLFLAG_RD, &stats->tx_tso_offloaded,
3832 "Segmentation offload of transmitted buffer");
3833 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
3834 CTLFLAG_RD, &stats->tx_task_rescheduled,
3835 "Times the transmit interrupt task rescheduled itself");
3839 vtnet_setup_sysctl(struct vtnet_softc *sc)
3842 struct sysctl_ctx_list *ctx;
3843 struct sysctl_oid *tree;
3844 struct sysctl_oid_list *child;
3846 dev = sc->vtnet_dev;
3847 ctx = device_get_sysctl_ctx(dev);
3848 tree = device_get_sysctl_tree(dev);
3849 child = SYSCTL_CHILDREN(tree);
3851 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs",
3852 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0,
3853 "Maximum number of supported virtqueue pairs");
3854 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs",
3855 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0,
3856 "Number of active virtqueue pairs");
3858 vtnet_setup_stat_sysctl(ctx, child, sc);
3862 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq)
3865 return (virtqueue_enable_intr(rxq->vtnrx_vq));
3869 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq)
3872 virtqueue_disable_intr(rxq->vtnrx_vq);
3876 vtnet_txq_enable_intr(struct vtnet_txq *txq)
3878 struct virtqueue *vq;
3882 if (vtnet_txq_below_threshold(txq) != 0)
3883 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG));
3886 * The free count is above our threshold. Keep the Tx interrupt
3887 * disabled until the queue is fuller.
3893 vtnet_txq_disable_intr(struct vtnet_txq *txq)
3896 virtqueue_disable_intr(txq->vtntx_vq);
3900 vtnet_enable_rx_interrupts(struct vtnet_softc *sc)
3904 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3905 vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]);
3909 vtnet_enable_tx_interrupts(struct vtnet_softc *sc)
3913 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3914 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]);
3918 vtnet_enable_interrupts(struct vtnet_softc *sc)
3921 vtnet_enable_rx_interrupts(sc);
3922 vtnet_enable_tx_interrupts(sc);
3926 vtnet_disable_rx_interrupts(struct vtnet_softc *sc)
3930 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3931 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]);
3935 vtnet_disable_tx_interrupts(struct vtnet_softc *sc)
3939 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3940 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]);
3944 vtnet_disable_interrupts(struct vtnet_softc *sc)
3947 vtnet_disable_rx_interrupts(sc);
3948 vtnet_disable_tx_interrupts(sc);
3952 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def)
3956 snprintf(path, sizeof(path),
3957 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob);
3958 TUNABLE_INT_FETCH(path, &def);