2 * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.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 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include "opt_device_polling.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/sockio.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
45 #include <sys/taskqueue.h>
46 #include <sys/random.h>
47 #include <sys/sglist.h>
49 #include <sys/mutex.h>
53 #include <net/ethernet.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 <netinet/udp.h>
68 #include <netinet/tcp.h>
69 #include <netinet/sctp.h>
71 #include <machine/bus.h>
72 #include <machine/resource.h>
76 #include <dev/virtio/virtio.h>
77 #include <dev/virtio/virtqueue.h>
78 #include <dev/virtio/network/virtio_net.h>
79 #include <dev/virtio/network/if_vtnetvar.h>
81 #include "virtio_if.h"
83 static int vtnet_modevent(module_t, int, void *);
85 static int vtnet_probe(device_t);
86 static int vtnet_attach(device_t);
87 static int vtnet_detach(device_t);
88 static int vtnet_suspend(device_t);
89 static int vtnet_resume(device_t);
90 static int vtnet_shutdown(device_t);
91 static int vtnet_config_change(device_t);
93 static void vtnet_negotiate_features(struct vtnet_softc *);
94 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
95 static void vtnet_get_hwaddr(struct vtnet_softc *);
96 static void vtnet_set_hwaddr(struct vtnet_softc *);
97 static int vtnet_is_link_up(struct vtnet_softc *);
98 static void vtnet_update_link_status(struct vtnet_softc *);
99 static void vtnet_watchdog(struct vtnet_softc *);
100 static void vtnet_config_change_task(void *, int);
101 static int vtnet_change_mtu(struct vtnet_softc *, int);
102 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t);
104 static int vtnet_init_rx_vq(struct vtnet_softc *);
105 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
106 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
107 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
109 #ifdef DEVICE_POLLING
110 static poll_handler_t vtnet_poll;
113 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
115 static int vtnet_replace_rxbuf(struct vtnet_softc *,
117 static int vtnet_newbuf(struct vtnet_softc *);
118 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
119 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
120 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
121 static void vtnet_vlan_tag_remove(struct mbuf *);
122 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
123 struct virtio_net_hdr *);
124 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
125 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
126 static void vtnet_rx_intr_task(void *, int);
127 static int vtnet_rx_vq_intr(void *);
129 static void vtnet_txeof(struct vtnet_softc *);
130 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
131 struct virtio_net_hdr *);
132 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
133 struct vtnet_tx_header *);
134 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
135 static void vtnet_start_locked(struct ifnet *);
136 static void vtnet_start(struct ifnet *);
137 static void vtnet_tick(void *);
138 static void vtnet_tx_intr_task(void *, int);
139 static int vtnet_tx_vq_intr(void *);
141 static void vtnet_stop(struct vtnet_softc *);
142 static int vtnet_reinit(struct vtnet_softc *);
143 static void vtnet_init_locked(struct vtnet_softc *);
144 static void vtnet_init(void *);
146 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
147 struct sglist *, int, int);
149 static void vtnet_rx_filter(struct vtnet_softc *sc);
150 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
151 static int vtnet_set_promisc(struct vtnet_softc *, int);
152 static int vtnet_set_allmulti(struct vtnet_softc *, int);
153 static void vtnet_rx_filter_mac(struct vtnet_softc *);
155 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
156 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
157 static void vtnet_set_vlan_filter(struct vtnet_softc *, int, uint16_t);
158 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
159 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
161 static int vtnet_ifmedia_upd(struct ifnet *);
162 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
164 static void vtnet_add_statistics(struct vtnet_softc *);
166 static int vtnet_enable_rx_intr(struct vtnet_softc *);
167 static int vtnet_enable_tx_intr(struct vtnet_softc *);
168 static void vtnet_disable_rx_intr(struct vtnet_softc *);
169 static void vtnet_disable_tx_intr(struct vtnet_softc *);
172 static int vtnet_csum_disable = 0;
173 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
174 static int vtnet_tso_disable = 0;
175 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
176 static int vtnet_lro_disable = 0;
177 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
180 * Reducing the number of transmit completed interrupts can
181 * improve performance. To do so, the define below keeps the
182 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
183 * in the start and watchdog paths. The price to pay for this
184 * is the m_free'ing of transmitted mbufs may be delayed until
185 * the watchdog fires.
187 #define VTNET_TX_INTR_MODERATION
189 static uma_zone_t vtnet_tx_header_zone;
191 static struct virtio_feature_desc vtnet_feature_desc[] = {
192 { VIRTIO_NET_F_CSUM, "TxChecksum" },
193 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
194 { VIRTIO_NET_F_MAC, "MacAddress" },
195 { VIRTIO_NET_F_GSO, "TxAllGSO" },
196 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
197 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
198 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
199 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
200 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
201 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
202 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
203 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
204 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
205 { VIRTIO_NET_F_STATUS, "Status" },
206 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
207 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
208 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
209 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
214 static device_method_t vtnet_methods[] = {
215 /* Device methods. */
216 DEVMETHOD(device_probe, vtnet_probe),
217 DEVMETHOD(device_attach, vtnet_attach),
218 DEVMETHOD(device_detach, vtnet_detach),
219 DEVMETHOD(device_suspend, vtnet_suspend),
220 DEVMETHOD(device_resume, vtnet_resume),
221 DEVMETHOD(device_shutdown, vtnet_shutdown),
223 /* VirtIO methods. */
224 DEVMETHOD(virtio_config_change, vtnet_config_change),
229 static driver_t vtnet_driver = {
232 sizeof(struct vtnet_softc)
234 static devclass_t vtnet_devclass;
236 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
238 MODULE_VERSION(vtnet, 1);
239 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
242 vtnet_modevent(module_t mod, int type, void *unused)
250 vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr",
251 sizeof(struct vtnet_tx_header),
252 NULL, NULL, NULL, NULL, 0, 0);
256 if (uma_zone_get_cur(vtnet_tx_header_zone) > 0)
258 else if (type == MOD_UNLOAD) {
259 uma_zdestroy(vtnet_tx_header_zone);
260 vtnet_tx_header_zone = NULL;
274 vtnet_probe(device_t dev)
277 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
280 device_set_desc(dev, "VirtIO Networking Adapter");
282 return (BUS_PROBE_DEFAULT);
286 vtnet_attach(device_t dev)
288 struct vtnet_softc *sc;
292 sc = device_get_softc(dev);
296 callout_init_mtx(&sc->vtnet_tick_ch, VTNET_MTX(sc), 0);
298 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
300 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
301 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
303 vtnet_add_statistics(sc);
305 virtio_set_feature_desc(dev, vtnet_feature_desc);
306 vtnet_negotiate_features(sc);
308 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
309 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
310 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
312 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
314 sc->vtnet_rx_mbuf_size = MCLBYTES;
315 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
317 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
318 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
320 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX)) {
321 sc->vtnet_mac_filter = malloc(
322 sizeof(struct vtnet_mac_filter), M_DEVBUF,
324 if (sc->vtnet_mac_filter == NULL) {
326 "cannot allocate mac filter table\n");
331 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
334 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
335 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
338 vtnet_get_hwaddr(sc);
340 error = vtnet_alloc_virtqueues(sc);
342 device_printf(dev, "cannot allocate virtqueues\n");
346 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
348 device_printf(dev, "cannot allocate ifnet structure\n");
354 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
355 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
356 ifp->if_init = vtnet_init;
357 ifp->if_start = vtnet_start;
358 ifp->if_ioctl = vtnet_ioctl;
360 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
361 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
363 tx_size = virtqueue_size(sc->vtnet_tx_vq);
364 sc->vtnet_tx_size = tx_size;
365 IFQ_SET_MAXLEN(&ifp->if_snd, tx_size - 1);
366 ifp->if_snd.ifq_drv_maxlen = tx_size - 1;
367 IFQ_SET_READY(&ifp->if_snd);
369 ether_ifattach(ifp, sc->vtnet_hwaddr);
371 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS))
372 ifp->if_capabilities |= IFCAP_LINKSTATE;
374 /* Tell the upper layer(s) we support long frames. */
375 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
376 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
378 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
379 ifp->if_capabilities |= IFCAP_TXCSUM;
381 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
382 ifp->if_capabilities |= IFCAP_TSO4;
383 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
384 ifp->if_capabilities |= IFCAP_TSO6;
385 if (ifp->if_capabilities & IFCAP_TSO)
386 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
388 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
389 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
392 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
393 ifp->if_capabilities |= IFCAP_RXCSUM;
395 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
396 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
397 ifp->if_capabilities |= IFCAP_LRO;
400 if (ifp->if_capabilities & IFCAP_HWCSUM) {
402 * VirtIO does not support VLAN tagging, but we can fake
403 * it by inserting and removing the 802.1Q header during
404 * transmit and receive. We are then able to do checksum
405 * offloading of VLAN frames.
407 ifp->if_capabilities |=
408 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
411 ifp->if_capenable = ifp->if_capabilities;
414 * Capabilities after here are not enabled by default.
417 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
418 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
420 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
421 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
422 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
423 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
426 #ifdef DEVICE_POLLING
427 ifp->if_capabilities |= IFCAP_POLLING;
430 TASK_INIT(&sc->vtnet_rx_intr_task, 0, vtnet_rx_intr_task, sc);
431 TASK_INIT(&sc->vtnet_tx_intr_task, 0, vtnet_tx_intr_task, sc);
432 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc);
434 sc->vtnet_tq = taskqueue_create_fast("vtnet_taskq", M_NOWAIT,
435 taskqueue_thread_enqueue, &sc->vtnet_tq);
436 if (sc->vtnet_tq == NULL) {
438 device_printf(dev, "cannot allocate taskqueue\n");
443 error = virtio_setup_intr(dev, INTR_TYPE_NET);
445 device_printf(dev, "cannot setup virtqueue interrupts\n");
450 taskqueue_start_threads(&sc->vtnet_tq, 1, PI_NET, "%s taskq",
451 device_get_nameunit(dev));
454 * Device defaults to promiscuous mode for backwards
455 * compatibility. Turn it off if possible.
457 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
459 if (vtnet_set_promisc(sc, 0) != 0) {
460 ifp->if_flags |= IFF_PROMISC;
462 "cannot disable promiscuous mode\n");
466 ifp->if_flags |= IFF_PROMISC;
476 vtnet_detach(device_t dev)
478 struct vtnet_softc *sc;
481 sc = device_get_softc(dev);
484 KASSERT(mtx_initialized(VTNET_MTX(sc)),
485 ("vtnet mutex not initialized"));
487 #ifdef DEVICE_POLLING
488 if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
489 ether_poll_deregister(ifp);
492 if (device_is_attached(dev)) {
497 callout_drain(&sc->vtnet_tick_ch);
498 taskqueue_drain(taskqueue_fast, &sc->vtnet_cfgchg_task);
503 if (sc->vtnet_tq != NULL) {
504 taskqueue_drain(sc->vtnet_tq, &sc->vtnet_rx_intr_task);
505 taskqueue_drain(sc->vtnet_tq, &sc->vtnet_tx_intr_task);
506 taskqueue_free(sc->vtnet_tq);
510 if (sc->vtnet_vlan_attach != NULL) {
511 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
512 sc->vtnet_vlan_attach = NULL;
514 if (sc->vtnet_vlan_detach != NULL) {
515 EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach);
516 sc->vtnet_vlan_detach = NULL;
519 if (sc->vtnet_mac_filter != NULL) {
520 free(sc->vtnet_mac_filter, M_DEVBUF);
521 sc->vtnet_mac_filter = NULL;
526 sc->vtnet_ifp = NULL;
529 if (sc->vtnet_rx_vq != NULL)
530 vtnet_free_rx_mbufs(sc);
531 if (sc->vtnet_tx_vq != NULL)
532 vtnet_free_tx_mbufs(sc);
533 if (sc->vtnet_ctrl_vq != NULL)
534 vtnet_free_ctrl_vq(sc);
536 ifmedia_removeall(&sc->vtnet_media);
537 VTNET_LOCK_DESTROY(sc);
543 vtnet_suspend(device_t dev)
545 struct vtnet_softc *sc;
547 sc = device_get_softc(dev);
551 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
558 vtnet_resume(device_t dev)
560 struct vtnet_softc *sc;
563 sc = device_get_softc(dev);
567 if (ifp->if_flags & IFF_UP)
568 vtnet_init_locked(sc);
569 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
576 vtnet_shutdown(device_t dev)
580 * Suspend already does all of what we need to
581 * do here; we just never expect to be resumed.
583 return (vtnet_suspend(dev));
587 vtnet_config_change(device_t dev)
589 struct vtnet_softc *sc;
591 sc = device_get_softc(dev);
593 taskqueue_enqueue_fast(taskqueue_fast, &sc->vtnet_cfgchg_task);
599 vtnet_negotiate_features(struct vtnet_softc *sc)
602 uint64_t mask, features;
607 if (vtnet_csum_disable)
608 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
611 * TSO and LRO are only available when their corresponding
612 * checksum offload feature is also negotiated.
615 if (vtnet_csum_disable || vtnet_tso_disable)
616 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
617 VIRTIO_NET_F_HOST_ECN;
619 if (vtnet_csum_disable || vtnet_lro_disable)
620 mask |= VTNET_LRO_FEATURES;
622 features = VTNET_FEATURES & ~mask;
623 #ifdef VTNET_TX_INTR_MODERATION
624 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
626 sc->vtnet_features = virtio_negotiate_features(dev, features);
628 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0 &&
629 virtio_with_feature(dev, VTNET_LRO_FEATURES)) {
631 * LRO without mergeable buffers requires special care. This
632 * is not ideal because every receive buffer must be large
633 * enough to hold the maximum TCP packet, the Ethernet header,
634 * and the vtnet_rx_header. This requires up to 34 descriptors
635 * when using MCLBYTES clusters. If we do not have indirect
636 * descriptors, LRO is disabled since the virtqueue will not
637 * be able to contain very many receive buffers.
639 if (virtio_with_feature(dev,
640 VIRTIO_RING_F_INDIRECT_DESC) == 0) {
642 "LRO disabled due to lack of both mergeable "
643 "buffers and indirect descriptors\n");
645 sc->vtnet_features = virtio_negotiate_features(dev,
646 features & ~VTNET_LRO_FEATURES);
648 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
653 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
656 struct vq_alloc_info vq_info[3];
663 * Indirect descriptors are not needed for the Rx
664 * virtqueue when mergeable buffers are negotiated.
665 * The header is placed inline with the data, not
666 * in a separate descriptor, and mbuf clusters are
667 * always physically contiguous.
669 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
670 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
671 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
675 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
676 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
677 "%s receive", device_get_nameunit(dev));
679 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
680 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
681 "%s transmit", device_get_nameunit(dev));
683 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
686 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
687 &sc->vtnet_ctrl_vq, "%s control",
688 device_get_nameunit(dev));
691 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
695 vtnet_get_hwaddr(struct vtnet_softc *sc)
701 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
702 virtio_read_device_config(dev,
703 offsetof(struct virtio_net_config, mac),
704 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
706 /* Generate random locally administered unicast address. */
707 sc->vtnet_hwaddr[0] = 0xB2;
708 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
710 vtnet_set_hwaddr(sc);
715 vtnet_set_hwaddr(struct vtnet_softc *sc)
721 virtio_write_device_config(dev,
722 offsetof(struct virtio_net_config, mac),
723 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
727 vtnet_is_link_up(struct vtnet_softc *sc)
736 VTNET_LOCK_ASSERT(sc);
738 if ((ifp->if_capenable & IFCAP_LINKSTATE) == 0)
741 status = virtio_read_dev_config_2(dev,
742 offsetof(struct virtio_net_config, status));
744 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
748 vtnet_update_link_status(struct vtnet_softc *sc)
755 link = vtnet_is_link_up(sc);
757 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
758 sc->vtnet_flags |= VTNET_FLAG_LINK;
759 if_link_state_change(ifp, LINK_STATE_UP);
760 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
761 vtnet_start_locked(ifp);
762 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
763 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
764 if_link_state_change(ifp, LINK_STATE_DOWN);
769 vtnet_watchdog(struct vtnet_softc *sc)
775 #ifdef VTNET_TX_INTR_MODERATION
779 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
782 if_printf(ifp, "watchdog timeout -- resetting\n");
784 virtqueue_dump(sc->vtnet_tx_vq);
787 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
788 vtnet_init_locked(sc);
792 vtnet_config_change_task(void *arg, int pending)
794 struct vtnet_softc *sc;
799 vtnet_update_link_status(sc);
804 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
806 struct vtnet_softc *sc;
808 int reinit, mask, error;
811 ifr = (struct ifreq *) data;
817 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
819 else if (ifp->if_mtu != ifr->ifr_mtu) {
821 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
828 if ((ifp->if_flags & IFF_UP) == 0) {
829 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
831 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
832 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
833 (IFF_PROMISC | IFF_ALLMULTI)) {
834 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
840 vtnet_init_locked(sc);
843 sc->vtnet_if_flags = ifp->if_flags;
850 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
851 (ifp->if_drv_flags & IFF_DRV_RUNNING))
852 vtnet_rx_filter_mac(sc);
858 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
862 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
864 #ifdef DEVICE_POLLING
865 if (mask & IFCAP_POLLING) {
866 if (ifr->ifr_reqcap & IFCAP_POLLING) {
867 error = ether_poll_register(vtnet_poll, ifp);
872 vtnet_disable_rx_intr(sc);
873 vtnet_disable_tx_intr(sc);
874 ifp->if_capenable |= IFCAP_POLLING;
877 error = ether_poll_deregister(ifp);
879 /* Enable interrupts even in error case. */
881 vtnet_enable_tx_intr(sc);
882 vtnet_enable_rx_intr(sc);
883 ifp->if_capenable &= ~IFCAP_POLLING;
890 if (mask & IFCAP_TXCSUM) {
891 ifp->if_capenable ^= IFCAP_TXCSUM;
892 if (ifp->if_capenable & IFCAP_TXCSUM)
893 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
895 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
898 if (mask & IFCAP_TSO4) {
899 ifp->if_capenable ^= IFCAP_TSO4;
900 if (ifp->if_capenable & IFCAP_TSO4)
901 ifp->if_hwassist |= CSUM_TSO;
903 ifp->if_hwassist &= ~CSUM_TSO;
906 if (mask & IFCAP_RXCSUM) {
907 ifp->if_capenable ^= IFCAP_RXCSUM;
911 if (mask & IFCAP_LRO) {
912 ifp->if_capenable ^= IFCAP_LRO;
916 if (mask & IFCAP_VLAN_HWFILTER) {
917 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
921 if (mask & IFCAP_VLAN_HWTSO)
922 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
924 if (mask & IFCAP_VLAN_HWTAGGING)
925 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
927 if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
928 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
929 vtnet_init_locked(sc);
931 VLAN_CAPABILITIES(ifp);
937 error = ether_ioctl(ifp, cmd, data);
941 VTNET_LOCK_ASSERT_NOTOWNED(sc);
947 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
950 int new_frame_size, clsize;
954 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
955 new_frame_size = sizeof(struct vtnet_rx_header) +
956 sizeof(struct ether_vlan_header) + new_mtu;
958 if (new_frame_size > MJUM9BYTES)
961 if (new_frame_size <= MCLBYTES)
966 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
967 sizeof(struct ether_vlan_header) + new_mtu;
969 if (new_frame_size <= MCLBYTES)
972 clsize = MJUMPAGESIZE;
975 sc->vtnet_rx_mbuf_size = clsize;
976 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
977 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
978 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
980 ifp->if_mtu = new_mtu;
982 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
983 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
984 vtnet_init_locked(sc);
991 vtnet_init_rx_vq(struct vtnet_softc *sc)
993 struct virtqueue *vq;
996 vq = sc->vtnet_rx_vq;
1000 while (!virtqueue_full(vq)) {
1001 if ((error = vtnet_newbuf(sc)) != 0)
1007 virtqueue_notify(vq);
1010 * EMSGSIZE signifies the virtqueue did not have enough
1011 * entries available to hold the last mbuf. This is not
1012 * an error. We should not get ENOSPC since we check if
1013 * the virtqueue is full before attempting to add a
1016 if (error == EMSGSIZE)
1024 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1026 struct virtqueue *vq;
1030 vq = sc->vtnet_rx_vq;
1033 while ((m = virtqueue_drain(vq, &last)) != NULL)
1036 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1040 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1042 struct virtqueue *vq;
1043 struct vtnet_tx_header *txhdr;
1046 vq = sc->vtnet_tx_vq;
1049 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1050 m_freem(txhdr->vth_mbuf);
1051 uma_zfree(vtnet_tx_header_zone, txhdr);
1054 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1058 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1062 * The control virtqueue is only polled, therefore
1063 * it should already be empty.
1065 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1066 ("Ctrl Vq not empty"));
1069 #ifdef DEVICE_POLLING
1071 vtnet_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1073 struct vtnet_softc *sc;
1080 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1081 if (cmd == POLL_AND_CHECK_STATUS)
1082 vtnet_update_link_status(sc);
1084 if (virtqueue_nused(sc->vtnet_rx_vq) > 0)
1085 vtnet_rxeof(sc, count, &rx_done);
1088 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1089 vtnet_start_locked(ifp);
1095 #endif /* DEVICE_POLLING */
1097 static struct mbuf *
1098 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1100 struct mbuf *m_head, *m_tail, *m;
1103 clsize = sc->vtnet_rx_mbuf_size;
1105 m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize);
1109 m_head->m_len = clsize;
1113 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1114 ("chained Rx mbuf requested without LRO_NOMRG"));
1116 for (i = 1; i < nbufs; i++) {
1117 m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize);
1127 if (m_tailp != NULL)
1133 sc->vtnet_stats.mbuf_alloc_failed++;
1140 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1142 struct mbuf *m, *m_prev;
1143 struct mbuf *m_new, *m_tail;
1144 int len, clsize, nreplace, error;
1151 clsize = sc->vtnet_rx_mbuf_size;
1154 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ||
1155 m->m_next == NULL, ("chained Rx mbuf without LRO_NOMRG"));
1158 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1159 * allocating an entire chain for each received frame. When
1160 * the received frame's length is less than that of the chain,
1161 * the unused mbufs are reassigned to the new chain.
1165 * Something is seriously wrong if we received
1166 * a frame larger than the mbuf chain. Drop it.
1169 sc->vtnet_stats.rx_frame_too_large++;
1173 KASSERT(m->m_len == clsize,
1174 ("mbuf length not expected cluster size: %d",
1177 m->m_len = MIN(m->m_len, len);
1185 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1186 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1187 ("too many replacement mbufs: %d/%d", nreplace,
1188 sc->vtnet_rx_mbuf_count));
1190 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1191 if (m_new == NULL) {
1192 m_prev->m_len = clsize;
1197 * Move unused mbufs, if any, from the original chain
1198 * onto the end of the new chain.
1200 if (m_prev->m_next != NULL) {
1201 m_tail->m_next = m_prev->m_next;
1202 m_prev->m_next = NULL;
1205 error = vtnet_enqueue_rxbuf(sc, m_new);
1208 * BAD! We could not enqueue the replacement mbuf chain. We
1209 * must restore the m0 chain to the original state if it was
1210 * modified so we can subsequently discard it.
1212 * NOTE: The replacement is suppose to be an identical copy
1213 * to the one just dequeued so this is an unexpected error.
1215 sc->vtnet_stats.rx_enq_replacement_failed++;
1217 if (m_tail->m_next != NULL) {
1218 m_prev->m_next = m_tail->m_next;
1219 m_tail->m_next = NULL;
1222 m_prev->m_len = clsize;
1230 vtnet_newbuf(struct vtnet_softc *sc)
1235 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1239 error = vtnet_enqueue_rxbuf(sc, m);
1247 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1249 struct virtqueue *vq;
1252 vq = sc->vtnet_rx_vq;
1254 while (--nbufs > 0) {
1255 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1257 vtnet_discard_rxbuf(sc, m);
1262 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1267 * Requeue the discarded mbuf. This should always be
1268 * successful since it was just dequeued.
1270 error = vtnet_enqueue_rxbuf(sc, m);
1271 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1275 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1278 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1279 struct vtnet_rx_header *rxhdr;
1280 struct virtio_net_hdr *hdr;
1284 VTNET_LOCK_ASSERT(sc);
1285 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ||
1286 m->m_next == NULL, ("chained Rx mbuf without LRO_NOMRG"));
1288 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1290 mdata = mtod(m, uint8_t *);
1293 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1294 rxhdr = (struct vtnet_rx_header *) mdata;
1295 hdr = &rxhdr->vrh_hdr;
1296 offset += sizeof(struct vtnet_rx_header);
1298 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1299 KASSERT(error == 0, ("cannot add header to sglist"));
1302 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1306 if (m->m_next != NULL) {
1307 error = sglist_append_mbuf(&sg, m->m_next);
1312 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1316 vtnet_vlan_tag_remove(struct mbuf *m)
1318 struct ether_vlan_header *evl;
1320 evl = mtod(m, struct ether_vlan_header *);
1322 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
1323 m->m_flags |= M_VLANTAG;
1325 /* Strip the 802.1Q header. */
1326 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1327 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1328 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1333 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1334 struct virtio_net_hdr *hdr)
1336 struct ether_header *eh;
1337 struct ether_vlan_header *evh;
1339 struct ip6_hdr *ip6;
1341 int ip_offset, csum_start, csum_offset, hlen;
1346 * Convert the VirtIO checksum interface to FreeBSD's interface.
1347 * The host only provides us with the offset at which to start
1348 * checksumming, and the offset from that to place the completed
1349 * checksum. While this maps well with how Linux does checksums,
1350 * for FreeBSD, we must parse the received packet in order to set
1351 * the appropriate CSUM_* flags.
1355 * Every mbuf added to the receive virtqueue is always at least
1356 * MCLBYTES big, so assume something is amiss if the first mbuf
1357 * does not contain both the Ethernet and protocol headers.
1359 ip_offset = sizeof(struct ether_header);
1360 if (m->m_len < ip_offset)
1363 eh = mtod(m, struct ether_header *);
1364 eth_type = ntohs(eh->ether_type);
1365 if (eth_type == ETHERTYPE_VLAN) {
1366 ip_offset = sizeof(struct ether_vlan_header);
1367 if (m->m_len < ip_offset)
1369 evh = mtod(m, struct ether_vlan_header *);
1370 eth_type = ntohs(evh->evl_proto);
1375 if (m->m_len < ip_offset + sizeof(struct ip))
1378 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1379 /* Sanity check the IP header. */
1380 if (ip->ip_v != IPVERSION)
1382 hlen = ip->ip_hl << 2;
1383 if (hlen < sizeof(struct ip))
1385 if (ntohs(ip->ip_len) < hlen)
1387 if (ntohs(ip->ip_len) != (m->m_pkthdr.len - ip_offset))
1390 ip_proto = ip->ip_p;
1391 csum_start = ip_offset + hlen;
1394 case ETHERTYPE_IPV6:
1395 if (m->m_len < ip_offset + sizeof(struct ip6_hdr))
1399 * XXX FreeBSD does not handle any IPv6 checksum offloading
1403 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1404 /* XXX Assume no extension headers are present. */
1405 ip_proto = ip6->ip6_nxt;
1406 csum_start = ip_offset + sizeof(struct ip6_hdr);
1410 sc->vtnet_stats.rx_csum_bad_ethtype++;
1414 /* Assume checksum begins right after the IP header. */
1415 if (hdr->csum_start != csum_start) {
1416 sc->vtnet_stats.rx_csum_bad_start++;
1422 csum_offset = offsetof(struct tcphdr, th_sum);
1426 csum_offset = offsetof(struct udphdr, uh_sum);
1430 csum_offset = offsetof(struct sctphdr, checksum);
1434 sc->vtnet_stats.rx_csum_bad_ipproto++;
1438 if (hdr->csum_offset != csum_offset) {
1439 sc->vtnet_stats.rx_csum_bad_offset++;
1444 * The IP header checksum is almost certainly valid but I'm
1445 * uncertain if that is guaranteed.
1447 * m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID;
1452 if (m->m_len < csum_start + sizeof(struct udphdr))
1455 udp = (struct udphdr *)(mtod(m, uint8_t *) + csum_start);
1456 if (udp->uh_sum == 0)
1462 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1463 m->m_pkthdr.csum_data = 0xFFFF;
1467 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1471 sc->vtnet_stats.rx_csum_offloaded++;
1478 * Alternative method of doing receive checksum offloading. Rather
1479 * than parsing the received frame down to the IP header, use the
1480 * csum_offset to determine which CSUM_* flags are appropriate. We
1481 * can get by with doing this only because the checksum offsets are
1482 * unique for the things we care about.
1485 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1486 struct virtio_net_hdr *hdr)
1488 struct ether_header *eh;
1489 struct ether_vlan_header *evh;
1494 csum_len = hdr->csum_start + hdr->csum_offset;
1496 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1498 if (m->m_len < csum_len)
1501 eh = mtod(m, struct ether_header *);
1502 eth_type = ntohs(eh->ether_type);
1503 if (eth_type == ETHERTYPE_VLAN) {
1504 evh = mtod(m, struct ether_vlan_header *);
1505 eth_type = ntohs(evh->evl_proto);
1508 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1509 sc->vtnet_stats.rx_csum_bad_ethtype++;
1513 /* Use the offset to determine the appropriate CSUM_* flags. */
1514 switch (hdr->csum_offset) {
1515 case offsetof(struct udphdr, uh_sum):
1516 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1518 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1519 if (udp->uh_sum == 0)
1524 case offsetof(struct tcphdr, th_sum):
1525 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1526 m->m_pkthdr.csum_data = 0xFFFF;
1529 case offsetof(struct sctphdr, checksum):
1530 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1534 sc->vtnet_stats.rx_csum_bad_offset++;
1538 sc->vtnet_stats.rx_csum_offloaded++;
1544 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1547 struct virtqueue *vq;
1548 struct mbuf *m, *m_tail;
1551 ifp = sc->vtnet_ifp;
1552 vq = sc->vtnet_rx_vq;
1555 while (--nbufs > 0) {
1556 m = virtqueue_dequeue(vq, &len);
1562 if (vtnet_newbuf(sc) != 0) {
1564 vtnet_discard_rxbuf(sc, m);
1566 vtnet_discard_merged_rxbuf(sc, nbufs);
1574 m->m_flags &= ~M_PKTHDR;
1576 m_head->m_pkthdr.len += len;
1584 sc->vtnet_stats.rx_mergeable_failed++;
1591 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1593 struct virtio_net_hdr lhdr;
1595 struct virtqueue *vq;
1597 struct ether_header *eh;
1598 struct virtio_net_hdr *hdr;
1599 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1600 int len, deq, nbufs, adjsz, rx_npkts;
1602 ifp = sc->vtnet_ifp;
1603 vq = sc->vtnet_rx_vq;
1608 VTNET_LOCK_ASSERT(sc);
1610 while (--count >= 0) {
1611 m = virtqueue_dequeue(vq, &len);
1616 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1618 vtnet_discard_rxbuf(sc, m);
1622 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1624 adjsz = sizeof(struct vtnet_rx_header);
1626 * Account for our pad between the header and
1627 * the actual start of the frame.
1629 len += VTNET_RX_HEADER_PAD;
1631 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1632 nbufs = mhdr->num_buffers;
1633 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1636 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1638 vtnet_discard_rxbuf(sc, m);
1640 vtnet_discard_merged_rxbuf(sc, nbufs);
1644 m->m_pkthdr.len = len;
1645 m->m_pkthdr.rcvif = ifp;
1646 m->m_pkthdr.csum_flags = 0;
1649 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1656 * Save copy of header before we strip it. For both mergeable
1657 * and non-mergeable, the VirtIO header is placed first in the
1658 * mbuf's data. We no longer need num_buffers, so always use a
1661 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1664 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1665 eh = mtod(m, struct ether_header *);
1666 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1667 vtnet_vlan_tag_remove(m);
1670 * With the 802.1Q header removed, update the
1671 * checksum starting location accordingly.
1673 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1675 ETHER_VLAN_ENCAP_LEN;
1679 if (ifp->if_capenable & IFCAP_RXCSUM &&
1680 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1681 if (vtnet_rx_csum(sc, m, hdr) != 0)
1682 sc->vtnet_stats.rx_csum_failed++;
1687 (*ifp->if_input)(ifp, m);
1691 * The interface may have been stopped while we were
1692 * passing the packet up the network stack.
1694 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1699 virtqueue_notify(vq);
1701 if (rx_npktsp != NULL)
1702 *rx_npktsp = rx_npkts;
1704 return (count > 0 ? 0 : EAGAIN);
1708 vtnet_rx_intr_task(void *arg, int pending)
1710 struct vtnet_softc *sc;
1715 ifp = sc->vtnet_ifp;
1719 #ifdef DEVICE_POLLING
1720 if (ifp->if_capenable & IFCAP_POLLING) {
1726 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1727 vtnet_enable_rx_intr(sc);
1732 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1733 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1734 vtnet_disable_rx_intr(sc);
1741 sc->vtnet_stats.rx_task_rescheduled++;
1742 taskqueue_enqueue_fast(sc->vtnet_tq,
1743 &sc->vtnet_rx_intr_task);
1748 vtnet_rx_vq_intr(void *xsc)
1750 struct vtnet_softc *sc;
1754 vtnet_disable_rx_intr(sc);
1755 taskqueue_enqueue_fast(sc->vtnet_tq, &sc->vtnet_rx_intr_task);
1761 vtnet_txeof(struct vtnet_softc *sc)
1763 struct virtqueue *vq;
1765 struct vtnet_tx_header *txhdr;
1768 vq = sc->vtnet_tx_vq;
1769 ifp = sc->vtnet_ifp;
1772 VTNET_LOCK_ASSERT(sc);
1774 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1777 m_freem(txhdr->vth_mbuf);
1778 uma_zfree(vtnet_tx_header_zone, txhdr);
1782 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1783 if (virtqueue_empty(vq))
1784 sc->vtnet_watchdog_timer = 0;
1788 static struct mbuf *
1789 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1790 struct virtio_net_hdr *hdr)
1793 struct ether_header *eh;
1794 struct ether_vlan_header *evh;
1796 struct ip6_hdr *ip6;
1799 uint16_t eth_type, csum_start;
1800 uint8_t ip_proto, gso_type;
1802 ifp = sc->vtnet_ifp;
1805 ip_offset = sizeof(struct ether_header);
1806 if (m->m_len < ip_offset) {
1807 if ((m = m_pullup(m, ip_offset)) == NULL)
1811 eh = mtod(m, struct ether_header *);
1812 eth_type = ntohs(eh->ether_type);
1813 if (eth_type == ETHERTYPE_VLAN) {
1814 ip_offset = sizeof(struct ether_vlan_header);
1815 if (m->m_len < ip_offset) {
1816 if ((m = m_pullup(m, ip_offset)) == NULL)
1819 evh = mtod(m, struct ether_vlan_header *);
1820 eth_type = ntohs(evh->evl_proto);
1825 if (m->m_len < ip_offset + sizeof(struct ip)) {
1826 m = m_pullup(m, ip_offset + sizeof(struct ip));
1831 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1832 ip_proto = ip->ip_p;
1833 csum_start = ip_offset + (ip->ip_hl << 2);
1834 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1837 case ETHERTYPE_IPV6:
1838 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1839 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1844 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1846 * XXX Assume no extension headers are present. Presently,
1847 * this will always be true in the case of TSO, and FreeBSD
1848 * does not perform checksum offloading of IPv6 yet.
1850 ip_proto = ip6->ip6_nxt;
1851 csum_start = ip_offset + sizeof(struct ip6_hdr);
1852 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1859 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1860 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1861 hdr->csum_start = csum_start;
1862 hdr->csum_offset = m->m_pkthdr.csum_data;
1864 sc->vtnet_stats.tx_csum_offloaded++;
1867 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1868 if (ip_proto != IPPROTO_TCP)
1871 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1872 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1877 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1878 hdr->gso_type = gso_type;
1879 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1880 hdr->gso_size = m->m_pkthdr.tso_segsz;
1882 if (tcp->th_flags & TH_CWR) {
1884 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1885 * ECN support is only configurable globally with the
1886 * net.inet.tcp.ecn.enable sysctl knob.
1888 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1889 if_printf(ifp, "TSO with ECN not supported "
1895 hdr->flags |= VIRTIO_NET_HDR_GSO_ECN;
1898 sc->vtnet_stats.tx_tso_offloaded++;
1905 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1906 struct vtnet_tx_header *txhdr)
1909 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1910 struct virtqueue *vq;
1912 int collapsed, error;
1914 vq = sc->vtnet_tx_vq;
1918 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1919 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1920 KASSERT(error == 0 && sg.sg_nseg == 1,
1921 ("cannot add header to sglist"));
1924 error = sglist_append_mbuf(&sg, m);
1929 m = m_collapse(m, M_NOWAIT, VTNET_MAX_TX_SEGS - 1);
1938 txhdr->vth_mbuf = m;
1940 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1950 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1952 struct vtnet_tx_header *txhdr;
1953 struct virtio_net_hdr *hdr;
1959 txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO);
1960 if (txhdr == NULL) {
1967 * Always use the non-mergeable header to simplify things. When
1968 * the mergeable feature is negotiated, the num_buffers field
1969 * must be set to zero. We use vtnet_hdr_size later to enqueue
1970 * the correct header size to the host.
1972 hdr = &txhdr->vth_uhdr.hdr;
1974 if (m->m_flags & M_VLANTAG) {
1975 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1976 if ((*m_head = m) == NULL) {
1980 m->m_flags &= ~M_VLANTAG;
1983 if (m->m_pkthdr.csum_flags != 0) {
1984 m = vtnet_tx_offload(sc, m, hdr);
1985 if ((*m_head = m) == NULL) {
1991 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1994 uma_zfree(vtnet_tx_header_zone, txhdr);
2000 vtnet_start(struct ifnet *ifp)
2002 struct vtnet_softc *sc;
2007 vtnet_start_locked(ifp);
2012 vtnet_start_locked(struct ifnet *ifp)
2014 struct vtnet_softc *sc;
2015 struct virtqueue *vq;
2020 vq = sc->vtnet_tx_vq;
2023 VTNET_LOCK_ASSERT(sc);
2025 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2026 IFF_DRV_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
2029 #ifdef VTNET_TX_INTR_MODERATION
2030 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
2034 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2035 if (virtqueue_full(vq)) {
2036 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2040 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2044 if (vtnet_encap(sc, &m0) != 0) {
2047 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2048 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2053 ETHER_BPF_MTAP(ifp, m0);
2057 virtqueue_notify(vq);
2058 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2063 vtnet_tick(void *xsc)
2065 struct vtnet_softc *sc;
2069 VTNET_LOCK_ASSERT(sc);
2071 virtqueue_dump(sc->vtnet_rx_vq);
2072 virtqueue_dump(sc->vtnet_tx_vq);
2076 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2080 vtnet_tx_intr_task(void *arg, int pending)
2082 struct vtnet_softc *sc;
2086 ifp = sc->vtnet_ifp;
2090 #ifdef DEVICE_POLLING
2091 if (ifp->if_capenable & IFCAP_POLLING) {
2097 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2098 vtnet_enable_tx_intr(sc);
2105 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2106 vtnet_start_locked(ifp);
2108 if (vtnet_enable_tx_intr(sc) != 0) {
2109 vtnet_disable_tx_intr(sc);
2110 sc->vtnet_stats.tx_task_rescheduled++;
2112 taskqueue_enqueue_fast(sc->vtnet_tq, &sc->vtnet_tx_intr_task);
2120 vtnet_tx_vq_intr(void *xsc)
2122 struct vtnet_softc *sc;
2126 vtnet_disable_tx_intr(sc);
2127 taskqueue_enqueue_fast(sc->vtnet_tq, &sc->vtnet_tx_intr_task);
2133 vtnet_stop(struct vtnet_softc *sc)
2138 dev = sc->vtnet_dev;
2139 ifp = sc->vtnet_ifp;
2141 VTNET_LOCK_ASSERT(sc);
2143 sc->vtnet_watchdog_timer = 0;
2144 callout_stop(&sc->vtnet_tick_ch);
2145 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2147 vtnet_disable_rx_intr(sc);
2148 vtnet_disable_tx_intr(sc);
2151 * Stop the host VirtIO adapter. Note this will reset the host
2152 * adapter's state back to the pre-initialized state, so in
2153 * order to make the device usable again, we must drive it
2154 * through virtio_reinit() and virtio_reinit_complete().
2158 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2160 vtnet_free_rx_mbufs(sc);
2161 vtnet_free_tx_mbufs(sc);
2165 vtnet_reinit(struct vtnet_softc *sc)
2170 ifp = sc->vtnet_ifp;
2171 features = sc->vtnet_features;
2174 * Re-negotiate with the host, removing any disabled receive
2175 * features. Transmit features are disabled only on our side
2176 * via if_capenable and if_hwassist.
2179 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2180 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2181 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2184 if (ifp->if_capabilities & IFCAP_LRO) {
2185 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2186 features &= ~VTNET_LRO_FEATURES;
2189 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2190 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2191 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2194 return (virtio_reinit(sc->vtnet_dev, features));
2198 vtnet_init_locked(struct vtnet_softc *sc)
2204 dev = sc->vtnet_dev;
2205 ifp = sc->vtnet_ifp;
2207 VTNET_LOCK_ASSERT(sc);
2209 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2212 /* Stop host's adapter, cancel any pending I/O. */
2215 /* Reinitialize the host device. */
2216 error = vtnet_reinit(sc);
2219 "reinitialization failed, stopping device...\n");
2224 /* Update host with assigned MAC address. */
2225 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2226 vtnet_set_hwaddr(sc);
2228 ifp->if_hwassist = 0;
2229 if (ifp->if_capenable & IFCAP_TXCSUM)
2230 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2231 if (ifp->if_capenable & IFCAP_TSO4)
2232 ifp->if_hwassist |= CSUM_TSO;
2234 error = vtnet_init_rx_vq(sc);
2237 "cannot allocate mbufs for Rx virtqueue\n");
2242 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2243 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2244 /* Restore promiscuous and all-multicast modes. */
2245 vtnet_rx_filter(sc);
2247 /* Restore filtered MAC addresses. */
2248 vtnet_rx_filter_mac(sc);
2251 /* Restore VLAN filters. */
2252 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2253 vtnet_rx_filter_vlan(sc);
2256 #ifdef DEVICE_POLLING
2257 if (ifp->if_capenable & IFCAP_POLLING) {
2258 vtnet_disable_rx_intr(sc);
2259 vtnet_disable_tx_intr(sc);
2263 vtnet_enable_rx_intr(sc);
2264 vtnet_enable_tx_intr(sc);
2267 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2268 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2270 virtio_reinit_complete(dev);
2272 vtnet_update_link_status(sc);
2273 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2277 vtnet_init(void *xsc)
2279 struct vtnet_softc *sc;
2284 vtnet_init_locked(sc);
2289 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2290 struct sglist *sg, int readable, int writable)
2292 struct virtqueue *vq;
2295 vq = sc->vtnet_ctrl_vq;
2297 VTNET_LOCK_ASSERT(sc);
2298 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2299 ("no control virtqueue"));
2300 KASSERT(virtqueue_empty(vq),
2301 ("control command already enqueued"));
2303 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2306 virtqueue_notify(vq);
2309 * Poll until the command is complete. Previously, we would
2310 * sleep until the control virtqueue interrupt handler woke
2311 * us up, but dropping the VTNET_MTX leads to serialization
2314 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2315 * vectors. Two of those vectors are needed for the Rx and Tx
2316 * virtqueues. We do not support sharing both a Vq and config
2317 * changed notification on the same MSIX vector.
2319 c = virtqueue_poll(vq, NULL);
2320 KASSERT(c == cookie, ("unexpected control command response"));
2324 vtnet_rx_filter(struct vtnet_softc *sc)
2329 dev = sc->vtnet_dev;
2330 ifp = sc->vtnet_ifp;
2332 VTNET_LOCK_ASSERT(sc);
2333 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2334 ("CTRL_RX feature not negotiated"));
2336 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2337 device_printf(dev, "cannot %s promiscuous mode\n",
2338 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2340 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2341 device_printf(dev, "cannot %s all-multicast mode\n",
2342 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2346 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2348 struct virtio_net_ctrl_hdr hdr;
2349 struct sglist_seg segs[3];
2354 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
2359 hdr.class = VIRTIO_NET_CTRL_RX;
2362 ack = VIRTIO_NET_ERR;
2364 sglist_init(&sg, 3, segs);
2365 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2366 error |= sglist_append(&sg, &onoff, sizeof(uint8_t));
2367 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2368 KASSERT(error == 0 && sg.sg_nseg == 3,
2369 ("error adding Rx filter message to sglist"));
2371 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2373 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2377 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2380 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2384 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2387 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2391 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2393 struct virtio_net_ctrl_hdr hdr;
2394 struct vtnet_mac_filter *filter;
2395 struct sglist_seg segs[4];
2399 struct ifmultiaddr *ifma;
2400 int ucnt, mcnt, promisc, allmulti, error;
2403 ifp = sc->vtnet_ifp;
2404 filter = sc->vtnet_mac_filter;
2411 VTNET_LOCK_ASSERT(sc);
2412 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2413 ("CTRL_RX feature not negotiated"));
2415 /* Unicast MAC addresses: */
2417 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2418 if (ifa->ifa_addr->sa_family != AF_LINK)
2420 else if (ucnt == VTNET_MAX_MAC_ENTRIES)
2423 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2424 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2427 if_addr_runlock(ifp);
2429 if (ucnt >= VTNET_MAX_MAC_ENTRIES) {
2431 filter->vmf_unicast.nentries = 0;
2433 if_printf(ifp, "more than %d MAC addresses assigned, "
2434 "falling back to promiscuous mode\n",
2435 VTNET_MAX_MAC_ENTRIES);
2437 filter->vmf_unicast.nentries = ucnt;
2439 /* Multicast MAC addresses: */
2440 if_maddr_rlock(ifp);
2441 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2442 if (ifma->ifma_addr->sa_family != AF_LINK)
2444 else if (mcnt == VTNET_MAX_MAC_ENTRIES)
2447 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2448 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2451 if_maddr_runlock(ifp);
2453 if (mcnt >= VTNET_MAX_MAC_ENTRIES) {
2455 filter->vmf_multicast.nentries = 0;
2457 if_printf(ifp, "more than %d multicast MAC addresses "
2458 "assigned, falling back to all-multicast mode\n",
2459 VTNET_MAX_MAC_ENTRIES);
2461 filter->vmf_multicast.nentries = mcnt;
2463 if (promisc && allmulti)
2466 hdr.class = VIRTIO_NET_CTRL_MAC;
2467 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2468 ack = VIRTIO_NET_ERR;
2470 sglist_init(&sg, 4, segs);
2471 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2472 error |= sglist_append(&sg, &filter->vmf_unicast,
2473 sizeof(struct vtnet_mac_table));
2474 error |= sglist_append(&sg, &filter->vmf_multicast,
2475 sizeof(struct vtnet_mac_table));
2476 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2477 KASSERT(error == 0 && sg.sg_nseg == 4,
2478 ("error adding MAC filtering message to sglist"));
2480 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2482 if (ack != VIRTIO_NET_OK)
2483 if_printf(ifp, "error setting host MAC filter table\n");
2487 if (vtnet_set_promisc(sc, 1) != 0)
2488 if_printf(ifp, "cannot enable promiscuous mode\n");
2490 if (vtnet_set_allmulti(sc, 1) != 0)
2491 if_printf(ifp, "cannot enable all-multicast mode\n");
2495 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2497 struct virtio_net_ctrl_hdr hdr;
2498 struct sglist_seg segs[3];
2503 hdr.class = VIRTIO_NET_CTRL_VLAN;
2504 hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2505 ack = VIRTIO_NET_ERR;
2508 sglist_init(&sg, 3, segs);
2509 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2510 error |= sglist_append(&sg, &tag, sizeof(uint16_t));
2511 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2512 KASSERT(error == 0 && sg.sg_nseg == 3,
2513 ("error adding VLAN control message to sglist"));
2515 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2517 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2521 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2526 int i, nvlans, error;
2528 VTNET_LOCK_ASSERT(sc);
2529 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2530 ("VLAN_FILTER feature not negotiated"));
2532 dev = sc->vtnet_dev;
2533 nvlans = sc->vtnet_nvlans;
2536 /* Enable filtering for each configured VLAN. */
2537 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2538 w = sc->vtnet_vlan_shadow[i];
2539 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) {
2540 if ((w & mask) != 0) {
2543 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0)
2549 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2551 device_printf(dev, "cannot restore VLAN filter table\n");
2555 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2560 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2561 ("VLAN_FILTER feature not negotiated"));
2563 if ((tag == 0) || (tag > 4095))
2566 ifp = sc->vtnet_ifp;
2567 idx = (tag >> 5) & 0x7F;
2572 /* Update shadow VLAN table. */
2575 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2578 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2581 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2582 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2583 device_printf(sc->vtnet_dev,
2584 "cannot %s VLAN %d %s the host filter table\n",
2585 add ? "add" : "remove", tag,
2586 add ? "to" : "from");
2594 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2597 if (ifp->if_softc != arg)
2600 vtnet_set_vlan_filter(arg, 1, tag);
2604 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2607 if (ifp->if_softc != arg)
2610 vtnet_set_vlan_filter(arg, 0, tag);
2614 vtnet_ifmedia_upd(struct ifnet *ifp)
2616 struct vtnet_softc *sc;
2617 struct ifmedia *ifm;
2620 ifm = &sc->vtnet_media;
2622 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2629 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2631 struct vtnet_softc *sc;
2635 ifmr->ifm_status = IFM_AVALID;
2636 ifmr->ifm_active = IFM_ETHER;
2639 if (vtnet_is_link_up(sc) != 0) {
2640 ifmr->ifm_status |= IFM_ACTIVE;
2641 ifmr->ifm_active |= VTNET_MEDIATYPE;
2643 ifmr->ifm_active |= IFM_NONE;
2648 vtnet_add_statistics(struct vtnet_softc *sc)
2651 struct vtnet_statistics *stats;
2652 struct sysctl_ctx_list *ctx;
2653 struct sysctl_oid *tree;
2654 struct sysctl_oid_list *child;
2656 dev = sc->vtnet_dev;
2657 stats = &sc->vtnet_stats;
2658 ctx = device_get_sysctl_ctx(dev);
2659 tree = device_get_sysctl_tree(dev);
2660 child = SYSCTL_CHILDREN(tree);
2662 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2663 CTLFLAG_RD, &stats->mbuf_alloc_failed,
2664 "Mbuf cluster allocation failures");
2666 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_frame_too_large",
2667 CTLFLAG_RD, &stats->rx_frame_too_large,
2668 "Received frame larger than the mbuf chain");
2669 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2670 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
2671 "Enqueuing the replacement receive mbuf failed");
2672 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_mergeable_failed",
2673 CTLFLAG_RD, &stats->rx_mergeable_failed,
2674 "Mergeable buffers receive failures");
2675 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2676 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
2677 "Received checksum offloaded buffer with unsupported "
2679 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_start",
2680 CTLFLAG_RD, &stats->rx_csum_bad_start,
2681 "Received checksum offloaded buffer with incorrect start offset");
2682 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2683 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
2684 "Received checksum offloaded buffer with incorrect IP protocol");
2685 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2686 CTLFLAG_RD, &stats->rx_csum_bad_offset,
2687 "Received checksum offloaded buffer with incorrect offset");
2688 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_failed",
2689 CTLFLAG_RD, &stats->rx_csum_failed,
2690 "Received buffer checksum offload failed");
2691 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_offloaded",
2692 CTLFLAG_RD, &stats->rx_csum_offloaded,
2693 "Received buffer checksum offload succeeded");
2694 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_task_rescheduled",
2695 CTLFLAG_RD, &stats->rx_task_rescheduled,
2696 "Times the receive interrupt task rescheduled itself");
2698 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_offloaded",
2699 CTLFLAG_RD, &stats->tx_csum_offloaded,
2700 "Offloaded checksum of transmitted buffer");
2701 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_offloaded",
2702 CTLFLAG_RD, &stats->tx_tso_offloaded,
2703 "Segmentation offload of transmitted buffer");
2704 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2705 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
2706 "Aborted transmit of checksum offloaded buffer with unknown "
2708 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2709 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
2710 "Aborted transmit of TSO buffer with unknown Ethernet type");
2711 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_task_rescheduled",
2712 CTLFLAG_RD, &stats->tx_task_rescheduled,
2713 "Times the transmit interrupt task rescheduled itself");
2717 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2720 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2724 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2727 virtqueue_disable_intr(sc->vtnet_rx_vq);
2731 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2734 #ifdef VTNET_TX_INTR_MODERATION
2737 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2742 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2745 virtqueue_disable_intr(sc->vtnet_tx_vq);