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>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/sockio.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
41 #include <sys/random.h>
42 #include <sys/sglist.h>
43 #include <sys/serialize.h>
47 #include <net/ethernet.h>
49 #include <net/if_arp.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/if_media.h>
53 #include <net/vlan/if_vlan_var.h>
54 #include <net/vlan/if_vlan_ether.h>
55 #include <net/ifq_var.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
63 #include <netinet/udp.h>
64 #include <netinet/tcp.h>
65 #include <netinet/sctp.h>
67 #include <dev/virtual/virtio/virtio/virtio.h>
68 #include <dev/virtual/virtio/virtio/virtqueue.h>
70 #include "virtio_net.h"
71 #include "virtio_if.h"
73 struct vtnet_statistics {
74 unsigned long mbuf_alloc_failed;
76 unsigned long rx_frame_too_large;
77 unsigned long rx_enq_replacement_failed;
78 unsigned long rx_mergeable_failed;
79 unsigned long rx_csum_bad_ethtype;
80 unsigned long rx_csum_bad_start;
81 unsigned long rx_csum_bad_ipproto;
82 unsigned long rx_csum_bad_offset;
83 unsigned long rx_csum_failed;
84 unsigned long rx_csum_offloaded;
85 unsigned long rx_task_rescheduled;
87 unsigned long tx_csum_offloaded;
88 unsigned long tx_tso_offloaded;
89 unsigned long tx_csum_bad_ethtype;
90 unsigned long tx_tso_bad_ethtype;
91 unsigned long tx_task_rescheduled;
96 struct ifnet *vtnet_ifp;
97 struct lwkt_serialize vtnet_slz;
100 #define VTNET_FLAG_LINK 0x0001
101 #define VTNET_FLAG_SUSPENDED 0x0002
102 #define VTNET_FLAG_CTRL_VQ 0x0004
103 #define VTNET_FLAG_CTRL_RX 0x0008
104 #define VTNET_FLAG_VLAN_FILTER 0x0010
105 #define VTNET_FLAG_TSO_ECN 0x0020
106 #define VTNET_FLAG_MRG_RXBUFS 0x0040
107 #define VTNET_FLAG_LRO_NOMRG 0x0080
109 struct virtqueue *vtnet_rx_vq;
110 struct virtqueue *vtnet_tx_vq;
111 struct virtqueue *vtnet_ctrl_vq;
113 struct vtnet_tx_header *txhdrarea;
119 int vtnet_rx_process_limit;
120 int vtnet_rx_mbuf_size;
121 int vtnet_rx_mbuf_count;
123 int vtnet_watchdog_timer;
124 uint64_t vtnet_features;
126 struct task vtnet_cfgchg_task;
128 struct vtnet_statistics vtnet_stats;
130 struct sysctl_ctx_list vtnet_sysctl_ctx;
131 struct sysctl_oid *vtnet_sysctl_tree;
133 struct callout vtnet_tick_ch;
135 eventhandler_tag vtnet_vlan_attach;
136 eventhandler_tag vtnet_vlan_detach;
138 struct ifmedia vtnet_media;
140 * Fake media type; the host does not provide us with
141 * any real media information.
143 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX)
144 char vtnet_hwaddr[ETHER_ADDR_LEN];
147 * During reset, the host's VLAN filtering table is lost. The
148 * array below is used to restore all the VLANs configured on
149 * this interface after a reset.
151 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32)
153 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
155 char vtnet_mtx_name[16];
159 * When mergeable buffers are not negotiated, the vtnet_rx_header structure
160 * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
161 * both keep the VirtIO header and the data non-contiguous and to keep the
162 * frame's payload 4 byte aligned.
164 * When mergeable buffers are negotiated, the host puts the VirtIO header in
165 * the beginning of the first mbuf's data.
167 #define VTNET_RX_HEADER_PAD 4
168 struct vtnet_rx_header {
169 struct virtio_net_hdr vrh_hdr;
170 char vrh_pad[VTNET_RX_HEADER_PAD];
174 * For each outgoing frame, the vtnet_tx_header below is allocated from
175 * the vtnet_tx_header_zone.
177 struct vtnet_tx_header {
179 struct virtio_net_hdr hdr;
180 struct virtio_net_hdr_mrg_rxbuf mhdr;
183 struct mbuf *vth_mbuf;
186 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
189 * The VirtIO specification does not place a limit on the number of MAC
190 * addresses the guest driver may request to be filtered. In practice,
191 * the host is constrained by available resources. To simplify this driver,
192 * impose a reasonably high limit of MAC addresses we will filter before
193 * falling back to promiscuous or all-multicast modes.
195 #define VTNET_MAX_MAC_ENTRIES 128
197 struct vtnet_mac_table {
199 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
202 struct vtnet_mac_filter {
203 struct vtnet_mac_table vmf_unicast;
204 uint32_t vmf_pad; /* Make tables non-contiguous. */
205 struct vtnet_mac_table vmf_multicast;
209 * The MAC filter table is malloc(9)'d when needed. Ensure it will
210 * always fit in one segment.
212 CTASSERT(sizeof(struct vtnet_mac_filter) <= PAGE_SIZE);
214 #define VTNET_WATCHDOG_TIMEOUT 5
215 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP)// | CSUM_SCTP)
217 /* Features desired/implemented by this driver. */
218 #define VTNET_FEATURES \
219 (VIRTIO_NET_F_MAC | \
220 VIRTIO_NET_F_STATUS | \
221 VIRTIO_NET_F_CTRL_VQ | \
222 VIRTIO_NET_F_CTRL_RX | \
223 VIRTIO_NET_F_CTRL_VLAN | \
224 VIRTIO_NET_F_CSUM | \
225 VIRTIO_NET_F_HOST_TSO4 | \
226 VIRTIO_NET_F_HOST_TSO6 | \
227 VIRTIO_NET_F_HOST_ECN | \
228 VIRTIO_NET_F_GUEST_CSUM | \
229 VIRTIO_NET_F_GUEST_TSO4 | \
230 VIRTIO_NET_F_GUEST_TSO6 | \
231 VIRTIO_NET_F_GUEST_ECN | \
232 VIRTIO_NET_F_MRG_RXBUF)
235 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
236 * frames larger than 1514 bytes. We do not yet support software LRO
239 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
240 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
242 #define VTNET_MAX_MTU 65536
243 #define VTNET_MAX_RX_SIZE 65550
246 * Used to preallocate the Vq indirect descriptors. The first segment
247 * is reserved for the header.
249 #define VTNET_MIN_RX_SEGS 2
250 #define VTNET_MAX_RX_SEGS 34
251 #define VTNET_MAX_TX_SEGS 34
257 #define IFCAP_TSO4 0x00100 /* can do TCP Segmentation Offload */
258 #define IFCAP_TSO6 0x00200 /* can do TCP6 Segmentation Offload */
259 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */
260 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */
261 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */
265 * Assert we can receive and transmit the maximum with regular
268 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
269 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
272 * Determine how many mbufs are in each receive buffer. For LRO without
273 * mergeable descriptors, we must allocate an mbuf chain large enough to
274 * hold both the vtnet_rx_header and the maximum receivable data.
276 #define VTNET_NEEDED_RX_MBUFS(_sc) \
277 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \
278 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \
279 (_sc)->vtnet_rx_mbuf_size)
281 static int vtnet_modevent(module_t, int, void *);
283 static int vtnet_probe(device_t);
284 static int vtnet_attach(device_t);
285 static int vtnet_detach(device_t);
286 static int vtnet_suspend(device_t);
287 static int vtnet_resume(device_t);
288 static int vtnet_shutdown(device_t);
289 static int vtnet_config_change(device_t);
291 static void vtnet_negotiate_features(struct vtnet_softc *);
292 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
293 static void vtnet_get_hwaddr(struct vtnet_softc *);
294 static void vtnet_set_hwaddr(struct vtnet_softc *);
295 static int vtnet_is_link_up(struct vtnet_softc *);
296 static void vtnet_update_link_status(struct vtnet_softc *);
298 static void vtnet_watchdog(struct vtnet_softc *);
300 static void vtnet_config_change_task(void *, int);
301 static int vtnet_change_mtu(struct vtnet_softc *, int);
302 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
304 static int vtnet_init_rx_vq(struct vtnet_softc *);
305 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
306 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
307 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
309 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
311 static int vtnet_replace_rxbuf(struct vtnet_softc *,
313 static int vtnet_newbuf(struct vtnet_softc *);
314 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
315 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
316 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
317 static void vtnet_vlan_tag_remove(struct mbuf *);
318 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
319 struct virtio_net_hdr *);
320 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
321 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
322 static void vtnet_rx_intr_task(void *);
323 static int vtnet_rx_vq_intr(void *);
325 static void vtnet_txeof(struct vtnet_softc *);
326 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
327 struct virtio_net_hdr *);
328 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
329 struct vtnet_tx_header *);
330 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
331 static void vtnet_start_locked(struct ifnet *, struct ifaltq_subque *);
332 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
333 static void vtnet_tick(void *);
334 static void vtnet_tx_intr_task(void *);
335 static int vtnet_tx_vq_intr(void *);
337 static void vtnet_stop(struct vtnet_softc *);
338 static int vtnet_reinit(struct vtnet_softc *);
339 static void vtnet_init_locked(struct vtnet_softc *);
340 static void vtnet_init(void *);
342 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
343 struct sglist *, int, int);
345 static void vtnet_rx_filter(struct vtnet_softc *sc);
346 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
347 static int vtnet_set_promisc(struct vtnet_softc *, int);
348 static int vtnet_set_allmulti(struct vtnet_softc *, int);
349 static void vtnet_rx_filter_mac(struct vtnet_softc *);
351 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
352 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
353 static void vtnet_set_vlan_filter(struct vtnet_softc *, int, uint16_t);
354 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
355 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
357 static int vtnet_ifmedia_upd(struct ifnet *);
358 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
360 static void vtnet_add_statistics(struct vtnet_softc *);
362 static int vtnet_enable_rx_intr(struct vtnet_softc *);
363 static int vtnet_enable_tx_intr(struct vtnet_softc *);
364 static void vtnet_disable_rx_intr(struct vtnet_softc *);
365 static void vtnet_disable_tx_intr(struct vtnet_softc *);
368 static int vtnet_csum_disable = 0;
369 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
370 static int vtnet_tso_disable = 1;
371 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
372 static int vtnet_lro_disable = 1;
373 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
376 * Reducing the number of transmit completed interrupts can
377 * improve performance. To do so, the define below keeps the
378 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
379 * in the start and watchdog paths. The price to pay for this
380 * is the m_free'ing of transmitted mbufs may be delayed until
381 * the watchdog fires.
383 #define VTNET_TX_INTR_MODERATION
385 static struct virtio_feature_desc vtnet_feature_desc[] = {
386 { VIRTIO_NET_F_CSUM, "TxChecksum" },
387 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
388 { VIRTIO_NET_F_MAC, "MacAddress" },
389 { VIRTIO_NET_F_GSO, "TxAllGSO" },
390 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
391 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
392 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
393 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
394 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
395 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
396 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
397 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
398 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
399 { VIRTIO_NET_F_STATUS, "Status" },
400 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
401 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
402 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
403 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
404 { VIRTIO_NET_F_MQ, "RFS" },
408 static device_method_t vtnet_methods[] = {
409 /* Device methods. */
410 DEVMETHOD(device_probe, vtnet_probe),
411 DEVMETHOD(device_attach, vtnet_attach),
412 DEVMETHOD(device_detach, vtnet_detach),
413 DEVMETHOD(device_suspend, vtnet_suspend),
414 DEVMETHOD(device_resume, vtnet_resume),
415 DEVMETHOD(device_shutdown, vtnet_shutdown),
417 /* VirtIO methods. */
418 DEVMETHOD(virtio_config_change, vtnet_config_change),
423 static driver_t vtnet_driver = {
426 sizeof(struct vtnet_softc)
429 static devclass_t vtnet_devclass;
431 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
433 MODULE_VERSION(vtnet, 1);
434 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
437 vtnet_modevent(module_t mod, int type, void *unused)
459 vtnet_probe(device_t dev)
461 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
464 device_set_desc(dev, "VirtIO Networking Adapter");
466 return (BUS_PROBE_DEFAULT);
470 vtnet_attach(device_t dev)
472 struct vtnet_softc *sc;
476 sc = device_get_softc(dev);
479 lwkt_serialize_init(&sc->vtnet_slz);
480 callout_init(&sc->vtnet_tick_ch);
482 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
484 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
485 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
487 vtnet_add_statistics(sc);
489 virtio_set_feature_desc(dev, vtnet_feature_desc);
490 vtnet_negotiate_features(sc);
492 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
493 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
494 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
496 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
499 sc->vtnet_rx_mbuf_size = MCLBYTES;
500 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
502 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
503 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
505 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
506 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
507 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
508 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
511 vtnet_get_hwaddr(sc);
513 error = vtnet_alloc_virtqueues(sc);
515 device_printf(dev, "cannot allocate virtqueues\n");
519 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
521 device_printf(dev, "cannot allocate ifnet structure\n");
527 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
528 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
529 ifp->if_init = vtnet_init;
530 ifp->if_start = vtnet_start;
531 ifp->if_ioctl = vtnet_ioctl;
533 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
534 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
536 tx_size = virtqueue_size(sc->vtnet_tx_vq);
537 sc->vtnet_tx_size = tx_size;
539 sc->txhdrarea = contigmalloc(
540 ((sc->vtnet_tx_size + 3) / 2) * sizeof(struct vtnet_tx_header),
541 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
542 if (sc->txhdrarea == NULL) {
543 panic("cannot contigmalloc the tx headers\n");
545 ifq_set_maxlen(&ifp->if_snd, tx_size - 1);
546 ifq_set_ready(&ifp->if_snd);
548 ether_ifattach(ifp, sc->vtnet_hwaddr, NULL);
550 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)){
551 //ifp->if_capabilities |= IFCAP_LINKSTATE;
552 kprintf("add dynamic link state\n");
555 /* Tell the upper layer(s) we support long frames. */
556 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
557 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
559 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
560 ifp->if_capabilities |= IFCAP_TXCSUM;
562 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
563 ifp->if_capabilities |= IFCAP_TSO4;
564 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
565 ifp->if_capabilities |= IFCAP_TSO6;
566 if (ifp->if_capabilities & IFCAP_TSO)
567 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
569 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
570 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
573 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
574 ifp->if_capabilities |= IFCAP_RXCSUM;
576 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
577 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
578 ifp->if_capabilities |= IFCAP_LRO;
581 if (ifp->if_capabilities & IFCAP_HWCSUM) {
583 * VirtIO does not support VLAN tagging, but we can fake
584 * it by inserting and removing the 802.1Q header during
585 * transmit and receive. We are then able to do checksum
586 * offloading of VLAN frames.
588 ifp->if_capabilities |=
589 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
592 ifp->if_capenable = ifp->if_capabilities;
595 * Capabilities after here are not enabled by default.
598 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
599 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
601 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
602 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
603 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
604 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
607 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc);
609 error = virtio_setup_intr(dev, &sc->vtnet_slz);
611 device_printf(dev, "cannot setup virtqueue interrupts\n");
617 * Device defaults to promiscuous mode for backwards
618 * compatibility. Turn it off if possible.
620 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
621 lwkt_serialize_enter(&sc->vtnet_slz);
622 if (vtnet_set_promisc(sc, 0) != 0) {
623 ifp->if_flags |= IFF_PROMISC;
625 "cannot disable promiscuous mode\n");
627 lwkt_serialize_exit(&sc->vtnet_slz);
629 ifp->if_flags |= IFF_PROMISC;
639 vtnet_detach(device_t dev)
641 struct vtnet_softc *sc;
644 sc = device_get_softc(dev);
647 if (device_is_attached(dev)) {
648 lwkt_serialize_enter(&sc->vtnet_slz);
650 lwkt_serialize_exit(&sc->vtnet_slz);
652 callout_stop(&sc->vtnet_tick_ch);
653 taskqueue_drain(taskqueue_swi, &sc->vtnet_cfgchg_task);
658 if (sc->vtnet_vlan_attach != NULL) {
659 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
660 sc->vtnet_vlan_attach = NULL;
662 if (sc->vtnet_vlan_detach != NULL) {
663 EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach);
664 sc->vtnet_vlan_detach = NULL;
669 sc->vtnet_ifp = NULL;
672 if (sc->vtnet_rx_vq != NULL)
673 vtnet_free_rx_mbufs(sc);
674 if (sc->vtnet_tx_vq != NULL)
675 vtnet_free_tx_mbufs(sc);
676 if (sc->vtnet_ctrl_vq != NULL)
677 vtnet_free_ctrl_vq(sc);
679 contigfree(sc->txhdrarea,
680 ((sc->vtnet_tx_size + 3) / 2) * sizeof(struct vtnet_tx_header),
683 ifmedia_removeall(&sc->vtnet_media);
689 vtnet_suspend(device_t dev)
691 struct vtnet_softc *sc;
693 sc = device_get_softc(dev);
695 lwkt_serialize_enter(&sc->vtnet_slz);
697 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
698 lwkt_serialize_exit(&sc->vtnet_slz);
704 vtnet_resume(device_t dev)
706 struct vtnet_softc *sc;
709 sc = device_get_softc(dev);
712 lwkt_serialize_enter(&sc->vtnet_slz);
713 if (ifp->if_flags & IFF_UP)
714 vtnet_init_locked(sc);
715 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
716 lwkt_serialize_exit(&sc->vtnet_slz);
722 vtnet_shutdown(device_t dev)
726 * Suspend already does all of what we need to
727 * do here; we just never expect to be resumed.
729 return (vtnet_suspend(dev));
733 vtnet_config_change(device_t dev)
735 struct vtnet_softc *sc;
737 sc = device_get_softc(dev);
739 taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->vtnet_cfgchg_task);
745 vtnet_negotiate_features(struct vtnet_softc *sc)
748 uint64_t mask, features;
753 if (vtnet_csum_disable)
754 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
757 * TSO and LRO are only available when their corresponding
758 * checksum offload feature is also negotiated.
761 if (vtnet_csum_disable || vtnet_tso_disable)
762 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
763 VIRTIO_NET_F_HOST_ECN;
765 if (vtnet_csum_disable || vtnet_lro_disable)
766 mask |= VTNET_LRO_FEATURES;
768 features = VTNET_FEATURES & ~mask;
769 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
770 sc->vtnet_features = virtio_negotiate_features(dev, features);
774 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
777 struct vq_alloc_info vq_info[3];
784 * Indirect descriptors are not needed for the Rx
785 * virtqueue when mergeable buffers are negotiated.
786 * The header is placed inline with the data, not
787 * in a separate descriptor, and mbuf clusters are
788 * always physically contiguous.
790 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
791 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
792 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
796 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
797 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
798 "%s receive", device_get_nameunit(dev));
800 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
801 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
802 "%s transmit", device_get_nameunit(dev));
804 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
807 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
808 &sc->vtnet_ctrl_vq, "%s control",
809 device_get_nameunit(dev));
812 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
816 vtnet_get_hwaddr(struct vtnet_softc *sc)
822 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
823 virtio_read_device_config(dev,
824 offsetof(struct virtio_net_config, mac),
825 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
827 /* Generate random locally administered unicast address. */
828 sc->vtnet_hwaddr[0] = 0xB2;
829 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
831 vtnet_set_hwaddr(sc);
836 vtnet_set_hwaddr(struct vtnet_softc *sc)
842 virtio_write_device_config(dev,
843 offsetof(struct virtio_net_config, mac),
844 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
848 vtnet_is_link_up(struct vtnet_softc *sc)
857 ASSERT_SERIALIZED(&sc->vtnet_slz);
859 status = virtio_read_dev_config_2(dev,
860 offsetof(struct virtio_net_config, status));
862 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
866 vtnet_update_link_status(struct vtnet_softc *sc)
870 struct ifaltq_subque *ifsq;
875 ifsq = ifq_get_subq_default(&ifp->if_snd);
877 link = vtnet_is_link_up(sc);
879 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
880 sc->vtnet_flags |= VTNET_FLAG_LINK;
882 device_printf(dev, "Link is up\n");
883 ifp->if_link_state = LINK_STATE_UP;
884 if_link_state_change(ifp);
885 if (!ifsq_is_empty(ifsq))
886 vtnet_start_locked(ifp, ifsq);
887 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
888 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
890 device_printf(dev, "Link is down\n");
892 ifp->if_link_state = LINK_STATE_DOWN;
893 if_link_state_change(ifp);
899 vtnet_watchdog(struct vtnet_softc *sc)
905 #ifdef VTNET_TX_INTR_MODERATION
909 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
912 if_printf(ifp, "watchdog timeout -- resetting\n");
914 virtqueue_dump(sc->vtnet_tx_vq);
917 ifp->if_flags &= ~IFF_RUNNING;
918 vtnet_init_locked(sc);
923 vtnet_config_change_task(void *arg, int pending)
925 struct vtnet_softc *sc;
929 lwkt_serialize_enter(&sc->vtnet_slz);
930 vtnet_update_link_status(sc);
931 lwkt_serialize_exit(&sc->vtnet_slz);
935 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
937 struct vtnet_softc *sc;
939 int reinit, mask, error;
942 ifr = (struct ifreq *) data;
948 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
950 else if (ifp->if_mtu != ifr->ifr_mtu) {
951 lwkt_serialize_enter(&sc->vtnet_slz);
952 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
953 lwkt_serialize_exit(&sc->vtnet_slz);
958 lwkt_serialize_enter(&sc->vtnet_slz);
959 if ((ifp->if_flags & IFF_UP) == 0) {
960 if (ifp->if_flags & IFF_RUNNING)
962 } else if (ifp->if_flags & IFF_RUNNING) {
963 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
964 (IFF_PROMISC | IFF_ALLMULTI)) {
965 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
971 vtnet_init_locked(sc);
974 sc->vtnet_if_flags = ifp->if_flags;
975 lwkt_serialize_exit(&sc->vtnet_slz);
980 lwkt_serialize_enter(&sc->vtnet_slz);
981 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
982 (ifp->if_flags & IFF_RUNNING))
983 vtnet_rx_filter_mac(sc);
984 lwkt_serialize_exit(&sc->vtnet_slz);
989 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
993 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
995 lwkt_serialize_enter(&sc->vtnet_slz);
997 if (mask & IFCAP_TXCSUM) {
998 ifp->if_capenable ^= IFCAP_TXCSUM;
999 if (ifp->if_capenable & IFCAP_TXCSUM)
1000 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1002 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1005 if (mask & IFCAP_TSO4) {
1006 ifp->if_capenable ^= IFCAP_TSO4;
1007 if (ifp->if_capenable & IFCAP_TSO4)
1008 ifp->if_hwassist |= CSUM_TSO;
1010 ifp->if_hwassist &= ~CSUM_TSO;
1013 if (mask & IFCAP_RXCSUM) {
1014 ifp->if_capenable ^= IFCAP_RXCSUM;
1018 if (mask & IFCAP_LRO) {
1019 ifp->if_capenable ^= IFCAP_LRO;
1023 if (mask & IFCAP_VLAN_HWFILTER) {
1024 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1028 if (mask & IFCAP_VLAN_HWTSO)
1029 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1031 if (mask & IFCAP_VLAN_HWTAGGING)
1032 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1034 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1035 ifp->if_flags &= ~IFF_RUNNING;
1036 vtnet_init_locked(sc);
1038 //VLAN_CAPABILITIES(ifp);
1040 lwkt_serialize_exit(&sc->vtnet_slz);
1044 error = ether_ioctl(ifp, cmd, data);
1052 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1055 int new_frame_size, clsize;
1057 ifp = sc->vtnet_ifp;
1059 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1060 new_frame_size = sizeof(struct vtnet_rx_header) +
1061 sizeof(struct ether_vlan_header) + new_mtu;
1063 if (new_frame_size > MJUM9BYTES)
1066 if (new_frame_size <= MCLBYTES)
1069 clsize = MJUM9BYTES;
1071 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1072 sizeof(struct ether_vlan_header) + new_mtu;
1074 if (new_frame_size <= MCLBYTES)
1077 clsize = MJUMPAGESIZE;
1080 sc->vtnet_rx_mbuf_size = clsize;
1081 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1082 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1083 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1085 ifp->if_mtu = new_mtu;
1087 if (ifp->if_flags & IFF_RUNNING) {
1088 ifp->if_flags &= ~IFF_RUNNING;
1089 vtnet_init_locked(sc);
1096 vtnet_init_rx_vq(struct vtnet_softc *sc)
1098 struct virtqueue *vq;
1101 vq = sc->vtnet_rx_vq;
1105 while (!virtqueue_full(vq)) {
1106 if ((error = vtnet_newbuf(sc)) != 0)
1112 virtqueue_notify(vq, &sc->vtnet_slz);
1115 * EMSGSIZE signifies the virtqueue did not have enough
1116 * entries available to hold the last mbuf. This is not
1117 * an error. We should not get ENOSPC since we check if
1118 * the virtqueue is full before attempting to add a
1121 if (error == EMSGSIZE)
1129 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1131 struct virtqueue *vq;
1135 vq = sc->vtnet_rx_vq;
1138 while ((m = virtqueue_drain(vq, &last)) != NULL)
1141 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1145 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1147 struct virtqueue *vq;
1148 struct vtnet_tx_header *txhdr;
1151 vq = sc->vtnet_tx_vq;
1154 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1155 m_freem(txhdr->vth_mbuf);
1158 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1162 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1165 * The control virtqueue is only polled, therefore
1166 * it should already be empty.
1168 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1169 ("Ctrl Vq not empty"));
1172 static struct mbuf *
1173 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1175 struct mbuf *m_head, *m_tail, *m;
1178 clsize = sc->vtnet_rx_mbuf_size;
1180 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1181 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1182 m_head = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR );
1186 m_head->m_len = clsize;
1190 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1191 ("chained Rx mbuf requested without LRO_NOMRG"));
1193 for (i = 0; i < nbufs - 1; i++) {
1194 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1195 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1205 if (m_tailp != NULL)
1211 sc->vtnet_stats.mbuf_alloc_failed++;
1218 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1220 struct mbuf *m, *m_prev;
1221 struct mbuf *m_new, *m_tail;
1222 int len, clsize, nreplace, error;
1229 clsize = sc->vtnet_rx_mbuf_size;
1232 if (m->m_next != NULL)
1233 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1234 ("chained Rx mbuf without LRO_NOMRG"));
1237 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1238 * allocating an entire chain for each received frame. When
1239 * the received frame's length is less than that of the chain,
1240 * the unused mbufs are reassigned to the new chain.
1244 * Something is seriously wrong if we received
1245 * a frame larger than the mbuf chain. Drop it.
1248 sc->vtnet_stats.rx_frame_too_large++;
1252 KASSERT(m->m_len == clsize,
1253 ("mbuf length not expected cluster size: %d",
1256 m->m_len = MIN(m->m_len, len);
1264 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1265 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1266 ("too many replacement mbufs: %d/%d", nreplace,
1267 sc->vtnet_rx_mbuf_count));
1269 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1270 if (m_new == NULL) {
1271 m_prev->m_len = clsize;
1276 * Move unused mbufs, if any, from the original chain
1277 * onto the end of the new chain.
1279 if (m_prev->m_next != NULL) {
1280 m_tail->m_next = m_prev->m_next;
1281 m_prev->m_next = NULL;
1284 error = vtnet_enqueue_rxbuf(sc, m_new);
1287 * BAD! We could not enqueue the replacement mbuf chain. We
1288 * must restore the m0 chain to the original state if it was
1289 * modified so we can subsequently discard it.
1291 * NOTE: The replacement is suppose to be an identical copy
1292 * to the one just dequeued so this is an unexpected error.
1294 sc->vtnet_stats.rx_enq_replacement_failed++;
1296 if (m_tail->m_next != NULL) {
1297 m_prev->m_next = m_tail->m_next;
1298 m_tail->m_next = NULL;
1301 m_prev->m_len = clsize;
1309 vtnet_newbuf(struct vtnet_softc *sc)
1314 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1318 error = vtnet_enqueue_rxbuf(sc, m);
1326 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1328 struct virtqueue *vq;
1331 vq = sc->vtnet_rx_vq;
1333 while (--nbufs > 0) {
1334 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1336 vtnet_discard_rxbuf(sc, m);
1341 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1346 * Requeue the discarded mbuf. This should always be
1347 * successful since it was just dequeued.
1349 error = vtnet_enqueue_rxbuf(sc, m);
1350 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1354 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1357 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1358 struct vtnet_rx_header *rxhdr;
1359 struct virtio_net_hdr *hdr;
1363 ASSERT_SERIALIZED(&sc->vtnet_slz);
1364 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1365 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1367 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1369 mdata = mtod(m, uint8_t *);
1372 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1373 rxhdr = (struct vtnet_rx_header *) mdata;
1374 hdr = &rxhdr->vrh_hdr;
1375 offset += sizeof(struct vtnet_rx_header);
1377 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1378 KASSERT(error == 0, ("cannot add header to sglist"));
1381 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1385 if (m->m_next != NULL) {
1386 error = sglist_append_mbuf(&sg, m->m_next);
1391 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1395 vtnet_vlan_tag_remove(struct mbuf *m)
1397 struct ether_vlan_header *evl;
1399 evl = mtod(m, struct ether_vlan_header *);
1401 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1402 m->m_flags |= M_VLANTAG;
1404 /* Strip the 802.1Q header. */
1405 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1406 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1407 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1411 * Alternative method of doing receive checksum offloading. Rather
1412 * than parsing the received frame down to the IP header, use the
1413 * csum_offset to determine which CSUM_* flags are appropriate. We
1414 * can get by with doing this only because the checksum offsets are
1415 * unique for the things we care about.
1418 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1419 struct virtio_net_hdr *hdr)
1421 struct ether_header *eh;
1422 struct ether_vlan_header *evh;
1427 csum_len = hdr->csum_start + hdr->csum_offset;
1429 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1431 if (m->m_len < csum_len)
1434 eh = mtod(m, struct ether_header *);
1435 eth_type = ntohs(eh->ether_type);
1436 if (eth_type == ETHERTYPE_VLAN) {
1437 evh = mtod(m, struct ether_vlan_header *);
1438 eth_type = ntohs(evh->evl_proto);
1441 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1442 sc->vtnet_stats.rx_csum_bad_ethtype++;
1446 /* Use the offset to determine the appropriate CSUM_* flags. */
1447 switch (hdr->csum_offset) {
1448 case offsetof(struct udphdr, uh_sum):
1449 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1451 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1452 if (udp->uh_sum == 0)
1457 case offsetof(struct tcphdr, th_sum):
1458 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1459 m->m_pkthdr.csum_data = 0xFFFF;
1462 case offsetof(struct sctphdr, checksum):
1463 //m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1467 sc->vtnet_stats.rx_csum_bad_offset++;
1471 sc->vtnet_stats.rx_csum_offloaded++;
1477 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1480 struct virtqueue *vq;
1481 struct mbuf *m, *m_tail;
1484 ifp = sc->vtnet_ifp;
1485 vq = sc->vtnet_rx_vq;
1488 while (--nbufs > 0) {
1489 m = virtqueue_dequeue(vq, &len);
1495 if (vtnet_newbuf(sc) != 0) {
1497 vtnet_discard_rxbuf(sc, m);
1499 vtnet_discard_merged_rxbuf(sc, nbufs);
1507 m->m_flags &= ~M_PKTHDR;
1509 m_head->m_pkthdr.len += len;
1517 sc->vtnet_stats.rx_mergeable_failed++;
1524 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1526 struct virtio_net_hdr lhdr;
1528 struct virtqueue *vq;
1530 struct ether_header *eh;
1531 struct virtio_net_hdr *hdr;
1532 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1533 int len, deq, nbufs, adjsz, rx_npkts;
1535 ifp = sc->vtnet_ifp;
1536 vq = sc->vtnet_rx_vq;
1541 ASSERT_SERIALIZED(&sc->vtnet_slz);
1543 while (--count >= 0) {
1544 m = virtqueue_dequeue(vq, &len);
1549 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1551 vtnet_discard_rxbuf(sc, m);
1555 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1557 adjsz = sizeof(struct vtnet_rx_header);
1559 * Account for our pad between the header and
1560 * the actual start of the frame.
1562 len += VTNET_RX_HEADER_PAD;
1564 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1565 nbufs = mhdr->num_buffers;
1566 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1569 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1571 vtnet_discard_rxbuf(sc, m);
1573 vtnet_discard_merged_rxbuf(sc, nbufs);
1577 m->m_pkthdr.len = len;
1578 m->m_pkthdr.rcvif = ifp;
1579 m->m_pkthdr.csum_flags = 0;
1582 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1589 * Save copy of header before we strip it. For both mergeable
1590 * and non-mergeable, the VirtIO header is placed first in the
1591 * mbuf's data. We no longer need num_buffers, so always use a
1594 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1597 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1598 eh = mtod(m, struct ether_header *);
1599 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1600 vtnet_vlan_tag_remove(m);
1603 * With the 802.1Q header removed, update the
1604 * checksum starting location accordingly.
1606 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1608 ETHER_VLAN_ENCAP_LEN;
1612 if (ifp->if_capenable & IFCAP_RXCSUM &&
1613 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1614 if (vtnet_rx_csum(sc, m, hdr) != 0)
1615 sc->vtnet_stats.rx_csum_failed++;
1618 lwkt_serialize_exit(&sc->vtnet_slz);
1620 (*ifp->if_input)(ifp, m);
1621 lwkt_serialize_enter(&sc->vtnet_slz);
1624 * The interface may have been stopped while we were
1625 * passing the packet up the network stack.
1627 if ((ifp->if_flags & IFF_RUNNING) == 0)
1631 virtqueue_notify(vq, &sc->vtnet_slz);
1633 if (rx_npktsp != NULL)
1634 *rx_npktsp = rx_npkts;
1636 return (count > 0 ? 0 : EAGAIN);
1640 vtnet_rx_intr_task(void *arg)
1642 struct vtnet_softc *sc;
1647 ifp = sc->vtnet_ifp;
1649 // lwkt_serialize_enter(&sc->vtnet_slz);
1651 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1652 vtnet_enable_rx_intr(sc);
1653 // lwkt_serialize_exit(&sc->vtnet_slz);
1657 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1658 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1659 vtnet_disable_rx_intr(sc);
1663 // lwkt_serialize_exit(&sc->vtnet_slz);
1666 sc->vtnet_stats.rx_task_rescheduled++;
1668 vtnet_rx_intr_task(sc);
1673 vtnet_rx_vq_intr(void *xsc)
1675 struct vtnet_softc *sc;
1679 vtnet_disable_rx_intr(sc);
1680 vtnet_rx_intr_task(sc);
1686 vtnet_txeof(struct vtnet_softc *sc)
1688 struct virtqueue *vq;
1690 struct vtnet_tx_header *txhdr;
1693 vq = sc->vtnet_tx_vq;
1694 ifp = sc->vtnet_ifp;
1697 ASSERT_SERIALIZED(&sc->vtnet_slz);
1699 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1702 m_freem(txhdr->vth_mbuf);
1706 ifq_clr_oactive(&ifp->if_snd);
1707 if (virtqueue_empty(vq))
1708 sc->vtnet_watchdog_timer = 0;
1712 static struct mbuf *
1713 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1714 struct virtio_net_hdr *hdr)
1717 struct ether_header *eh;
1718 struct ether_vlan_header *evh;
1720 struct ip6_hdr *ip6;
1723 uint16_t eth_type, csum_start;
1724 uint8_t ip_proto, gso_type;
1726 ifp = sc->vtnet_ifp;
1729 ip_offset = sizeof(struct ether_header);
1730 if (m->m_len < ip_offset) {
1731 if ((m = m_pullup(m, ip_offset)) == NULL)
1735 eh = mtod(m, struct ether_header *);
1736 eth_type = ntohs(eh->ether_type);
1737 if (eth_type == ETHERTYPE_VLAN) {
1738 ip_offset = sizeof(struct ether_vlan_header);
1739 if (m->m_len < ip_offset) {
1740 if ((m = m_pullup(m, ip_offset)) == NULL)
1743 evh = mtod(m, struct ether_vlan_header *);
1744 eth_type = ntohs(evh->evl_proto);
1749 if (m->m_len < ip_offset + sizeof(struct ip)) {
1750 m = m_pullup(m, ip_offset + sizeof(struct ip));
1755 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1756 ip_proto = ip->ip_p;
1757 csum_start = ip_offset + (ip->ip_hl << 2);
1758 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1761 case ETHERTYPE_IPV6:
1762 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1763 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1768 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1770 * XXX Assume no extension headers are present. Presently,
1771 * this will always be true in the case of TSO, and FreeBSD
1772 * does not perform checksum offloading of IPv6 yet.
1774 ip_proto = ip6->ip6_nxt;
1775 csum_start = ip_offset + sizeof(struct ip6_hdr);
1776 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1783 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1784 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1785 hdr->csum_start = csum_start;
1786 hdr->csum_offset = m->m_pkthdr.csum_data;
1788 sc->vtnet_stats.tx_csum_offloaded++;
1791 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1792 if (ip_proto != IPPROTO_TCP)
1795 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1796 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1801 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1802 hdr->gso_type = gso_type;
1803 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1804 hdr->gso_size = m->m_pkthdr.tso_segsz;
1806 if (tcp->th_flags & TH_CWR) {
1808 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1809 * ECN support is only configurable globally with the
1810 * net.inet.tcp.ecn.enable sysctl knob.
1812 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1813 if_printf(ifp, "TSO with ECN not supported "
1819 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1822 sc->vtnet_stats.tx_tso_offloaded++;
1829 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1830 struct vtnet_tx_header *txhdr)
1833 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1834 struct virtqueue *vq;
1836 int collapsed, error;
1838 vq = sc->vtnet_tx_vq;
1842 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1843 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1844 KASSERT(error == 0 && sg.sg_nseg == 1,
1845 ("cannot add header to sglist"));
1848 error = sglist_append_mbuf(&sg, m);
1853 //m = m_collapse(m, MB_DONTWAIT, VTNET_MAX_TX_SEGS - 1);
1854 m = m_defrag(m, MB_DONTWAIT);
1863 txhdr->vth_mbuf = m;
1865 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1874 static struct mbuf *
1875 vtnet_vlan_tag_insert(struct mbuf *m)
1878 struct ether_vlan_header *evl;
1880 if (M_WRITABLE(m) == 0) {
1881 n = m_dup(m, MB_DONTWAIT);
1883 if ((m = n) == NULL)
1887 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, MB_DONTWAIT);
1890 if (m->m_len < sizeof(struct ether_vlan_header)) {
1891 m = m_pullup(m, sizeof(struct ether_vlan_header));
1896 /* Insert 802.1Q header into the existing Ethernet header. */
1897 evl = mtod(m, struct ether_vlan_header *);
1898 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1899 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1900 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1901 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1902 m->m_flags &= ~M_VLANTAG;
1908 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1910 struct vtnet_tx_header *txhdr;
1911 struct virtio_net_hdr *hdr;
1915 txhdr = &sc->txhdrarea[sc->txhdridx];
1916 memset(txhdr, 0, sizeof(struct vtnet_tx_header));
1919 * Always use the non-mergeable header to simplify things. When
1920 * the mergeable feature is negotiated, the num_buffers field
1921 * must be set to zero. We use vtnet_hdr_size later to enqueue
1922 * the correct header size to the host.
1924 hdr = &txhdr->vth_uhdr.hdr;
1929 if (m->m_flags & M_VLANTAG) {
1930 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1931 m = vtnet_vlan_tag_insert(m);
1932 if ((*m_head = m) == NULL)
1934 m->m_flags &= ~M_VLANTAG;
1937 if (m->m_pkthdr.csum_flags != 0) {
1938 m = vtnet_tx_offload(sc, m, hdr);
1939 if ((*m_head = m) == NULL)
1943 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1946 (sc->txhdridx + 1) % ((sc->vtnet_tx_size + 3) / 2);
1952 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1954 struct vtnet_softc *sc;
1958 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1959 lwkt_serialize_enter(&sc->vtnet_slz);
1960 vtnet_start_locked(ifp, ifsq);
1961 lwkt_serialize_exit(&sc->vtnet_slz);
1965 vtnet_start_locked(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1967 struct vtnet_softc *sc;
1968 struct virtqueue *vq;
1973 vq = sc->vtnet_tx_vq;
1976 ASSERT_SERIALIZED(&sc->vtnet_slz);
1978 if ((ifp->if_flags & (IFF_RUNNING)) !=
1979 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
1982 #ifdef VTNET_TX_INTR_MODERATION
1983 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
1987 while (!ifsq_is_empty(ifsq)) {
1988 if (virtqueue_full(vq)) {
1989 ifq_set_oactive(&ifp->if_snd);
1993 m0 = ifq_dequeue(&ifp->if_snd);
1997 if (vtnet_encap(sc, &m0) != 0) {
2000 ifq_prepend(&ifp->if_snd, m0);
2001 ifq_set_oactive(&ifp->if_snd);
2006 ETHER_BPF_MTAP(ifp, m0);
2010 virtqueue_notify(vq, &sc->vtnet_slz);
2011 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2016 vtnet_tick(void *xsc)
2018 struct vtnet_softc *sc;
2023 ASSERT_SERIALIZED(&sc->vtnet_slz);
2025 virtqueue_dump(sc->vtnet_rx_vq);
2026 virtqueue_dump(sc->vtnet_tx_vq);
2030 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2035 vtnet_tx_intr_task(void *arg)
2037 struct vtnet_softc *sc;
2039 struct ifaltq_subque *ifsq;
2042 ifp = sc->vtnet_ifp;
2043 ifsq = ifq_get_subq_default(&ifp->if_snd);
2045 // lwkt_serialize_enter(&sc->vtnet_slz);
2047 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2048 vtnet_enable_tx_intr(sc);
2049 // lwkt_serialize_exit(&sc->vtnet_slz);
2055 if (!ifsq_is_empty(ifsq))
2056 vtnet_start_locked(ifp, ifsq);
2058 if (vtnet_enable_tx_intr(sc) != 0) {
2059 vtnet_disable_tx_intr(sc);
2060 sc->vtnet_stats.tx_task_rescheduled++;
2061 // lwkt_serialize_exit(&sc->vtnet_slz);
2062 vtnet_tx_intr_task(sc);
2067 // lwkt_serialize_exit(&sc->vtnet_slz);
2071 vtnet_tx_vq_intr(void *xsc)
2073 struct vtnet_softc *sc;
2077 vtnet_disable_tx_intr(sc);
2078 vtnet_tx_intr_task(sc);
2084 vtnet_stop(struct vtnet_softc *sc)
2089 dev = sc->vtnet_dev;
2090 ifp = sc->vtnet_ifp;
2092 ASSERT_SERIALIZED(&sc->vtnet_slz);
2094 sc->vtnet_watchdog_timer = 0;
2095 callout_stop(&sc->vtnet_tick_ch);
2096 ifq_clr_oactive(&ifp->if_snd);
2097 ifp->if_flags &= ~(IFF_RUNNING);
2099 vtnet_disable_rx_intr(sc);
2100 vtnet_disable_tx_intr(sc);
2103 * Stop the host VirtIO adapter. Note this will reset the host
2104 * adapter's state back to the pre-initialized state, so in
2105 * order to make the device usable again, we must drive it
2106 * through virtio_reinit() and virtio_reinit_complete().
2110 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2112 vtnet_free_rx_mbufs(sc);
2113 vtnet_free_tx_mbufs(sc);
2117 vtnet_reinit(struct vtnet_softc *sc)
2122 ifp = sc->vtnet_ifp;
2123 features = sc->vtnet_features;
2126 * Re-negotiate with the host, removing any disabled receive
2127 * features. Transmit features are disabled only on our side
2128 * via if_capenable and if_hwassist.
2131 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2132 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2133 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2136 if (ifp->if_capabilities & IFCAP_LRO) {
2137 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2138 features &= ~VTNET_LRO_FEATURES;
2141 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2142 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2143 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2146 return (virtio_reinit(sc->vtnet_dev, features));
2150 vtnet_init_locked(struct vtnet_softc *sc)
2156 dev = sc->vtnet_dev;
2157 ifp = sc->vtnet_ifp;
2159 ASSERT_SERIALIZED(&sc->vtnet_slz);
2161 if (ifp->if_flags & IFF_RUNNING)
2164 /* Stop host's adapter, cancel any pending I/O. */
2167 /* Reinitialize the host device. */
2168 error = vtnet_reinit(sc);
2171 "reinitialization failed, stopping device...\n");
2176 /* Update host with assigned MAC address. */
2177 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2178 vtnet_set_hwaddr(sc);
2180 ifp->if_hwassist = 0;
2181 if (ifp->if_capenable & IFCAP_TXCSUM)
2182 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2183 if (ifp->if_capenable & IFCAP_TSO4)
2184 ifp->if_hwassist |= CSUM_TSO;
2186 error = vtnet_init_rx_vq(sc);
2189 "cannot allocate mbufs for Rx virtqueue\n");
2194 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2195 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2196 /* Restore promiscuous and all-multicast modes. */
2197 vtnet_rx_filter(sc);
2199 /* Restore filtered MAC addresses. */
2200 vtnet_rx_filter_mac(sc);
2203 /* Restore VLAN filters. */
2204 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2205 vtnet_rx_filter_vlan(sc);
2209 vtnet_enable_rx_intr(sc);
2210 vtnet_enable_tx_intr(sc);
2213 ifp->if_flags |= IFF_RUNNING;
2214 ifq_clr_oactive(&ifp->if_snd);
2216 virtio_reinit_complete(dev);
2218 vtnet_update_link_status(sc);
2219 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2223 vtnet_init(void *xsc)
2225 struct vtnet_softc *sc;
2229 lwkt_serialize_enter(&sc->vtnet_slz);
2230 vtnet_init_locked(sc);
2231 lwkt_serialize_exit(&sc->vtnet_slz);
2235 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2236 struct sglist *sg, int readable, int writable)
2238 struct virtqueue *vq;
2241 vq = sc->vtnet_ctrl_vq;
2243 ASSERT_SERIALIZED(&sc->vtnet_slz);
2244 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2245 ("no control virtqueue"));
2246 KASSERT(virtqueue_empty(vq),
2247 ("control command already enqueued"));
2249 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2252 virtqueue_notify(vq, &sc->vtnet_slz);
2255 * Poll until the command is complete. Previously, we would
2256 * sleep until the control virtqueue interrupt handler woke
2257 * us up, but dropping the VTNET_MTX leads to serialization
2260 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2261 * vectors. Two of those vectors are needed for the Rx and Tx
2262 * virtqueues. We do not support sharing both a Vq and config
2263 * changed notification on the same MSIX vector.
2265 c = virtqueue_poll(vq, NULL);
2266 KASSERT(c == cookie, ("unexpected control command response"));
2270 vtnet_rx_filter(struct vtnet_softc *sc)
2275 dev = sc->vtnet_dev;
2276 ifp = sc->vtnet_ifp;
2278 ASSERT_SERIALIZED(&sc->vtnet_slz);
2279 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2280 ("CTRL_RX feature not negotiated"));
2282 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2283 device_printf(dev, "cannot %s promiscuous mode\n",
2284 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2286 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2287 device_printf(dev, "cannot %s all-multicast mode\n",
2288 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2292 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2294 struct virtio_net_ctrl_hdr hdr;
2295 struct sglist_seg segs[3];
2300 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
2305 hdr.class = VIRTIO_NET_CTRL_RX;
2308 ack = VIRTIO_NET_ERR;
2310 sglist_init(&sg, 3, segs);
2311 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2312 error |= sglist_append(&sg, &onoff, sizeof(uint8_t));
2313 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2314 KASSERT(error == 0 && sg.sg_nseg == 3,
2315 ("error adding Rx filter message to sglist"));
2317 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2319 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2323 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2326 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2330 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2333 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2337 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2339 struct virtio_net_ctrl_hdr hdr;
2340 struct vtnet_mac_filter *filter;
2341 struct sglist_seg segs[4];
2345 struct ifaddr_container *ifac;
2346 struct ifmultiaddr *ifma;
2347 int ucnt, mcnt, promisc, allmulti, error;
2350 ifp = sc->vtnet_ifp;
2357 ASSERT_SERIALIZED(&sc->vtnet_slz);
2358 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2359 ("CTRL_RX feature not negotiated"));
2362 * Allocate the MAC filtering table. Note we could do this
2363 * at attach time, but it is probably not worth keeping it
2364 * around for an infrequent occurrence.
2366 filter = kmalloc(sizeof(struct vtnet_mac_filter), M_DEVBUF,
2367 M_INTWAIT | M_ZERO);
2369 /* Unicast MAC addresses: */
2370 //if_addr_rlock(ifp);
2371 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2373 if (ifa->ifa_addr->sa_family != AF_LINK)
2375 else if (ucnt == VTNET_MAX_MAC_ENTRIES)
2378 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2379 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2382 //if_addr_runlock(ifp);
2384 if (ucnt >= VTNET_MAX_MAC_ENTRIES) {
2386 filter->vmf_unicast.nentries = 0;
2388 if_printf(ifp, "more than %d MAC addresses assigned, "
2389 "falling back to promiscuous mode\n",
2390 VTNET_MAX_MAC_ENTRIES);
2392 filter->vmf_unicast.nentries = ucnt;
2394 /* Multicast MAC addresses: */
2395 //if_maddr_rlock(ifp);
2396 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2397 if (ifma->ifma_addr->sa_family != AF_LINK)
2399 else if (mcnt == VTNET_MAX_MAC_ENTRIES)
2402 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2403 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2406 //if_maddr_runlock(ifp);
2408 if (mcnt >= VTNET_MAX_MAC_ENTRIES) {
2410 filter->vmf_multicast.nentries = 0;
2412 if_printf(ifp, "more than %d multicast MAC addresses "
2413 "assigned, falling back to all-multicast mode\n",
2414 VTNET_MAX_MAC_ENTRIES);
2416 filter->vmf_multicast.nentries = mcnt;
2418 if (promisc && allmulti)
2421 hdr.class = VIRTIO_NET_CTRL_MAC;
2422 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2423 ack = VIRTIO_NET_ERR;
2425 sglist_init(&sg, 4, segs);
2426 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2427 error |= sglist_append(&sg, &filter->vmf_unicast,
2428 sizeof(struct vtnet_mac_table));
2429 error |= sglist_append(&sg, &filter->vmf_multicast,
2430 sizeof(struct vtnet_mac_table));
2431 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2432 KASSERT(error == 0 && sg.sg_nseg == 4,
2433 ("error adding MAC filtering message to sglist"));
2435 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2437 if (ack != VIRTIO_NET_OK)
2438 if_printf(ifp, "error setting host MAC filter table\n");
2441 kfree(filter, M_DEVBUF);
2444 if (vtnet_set_promisc(sc, 1) != 0)
2445 if_printf(ifp, "cannot enable promiscuous mode\n");
2447 if (vtnet_set_allmulti(sc, 1) != 0)
2448 if_printf(ifp, "cannot enable all-multicast mode\n");
2452 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2454 struct virtio_net_ctrl_hdr hdr;
2455 struct sglist_seg segs[3];
2460 hdr.class = VIRTIO_NET_CTRL_VLAN;
2461 hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2462 ack = VIRTIO_NET_ERR;
2465 sglist_init(&sg, 3, segs);
2466 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2467 error |= sglist_append(&sg, &tag, sizeof(uint16_t));
2468 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2469 KASSERT(error == 0 && sg.sg_nseg == 3,
2470 ("error adding VLAN control message to sglist"));
2472 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2474 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2478 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2483 int i, nvlans, error;
2485 ASSERT_SERIALIZED(&sc->vtnet_slz);
2486 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2487 ("VLAN_FILTER feature not negotiated"));
2489 dev = sc->vtnet_dev;
2490 nvlans = sc->vtnet_nvlans;
2493 /* Enable filtering for each configured VLAN. */
2494 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2495 w = sc->vtnet_vlan_shadow[i];
2496 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) {
2497 if ((w & mask) != 0) {
2500 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0)
2506 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2508 device_printf(dev, "cannot restore VLAN filter table\n");
2512 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2517 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2518 ("VLAN_FILTER feature not negotiated"));
2520 if ((tag == 0) || (tag > 4095))
2523 ifp = sc->vtnet_ifp;
2524 idx = (tag >> 5) & 0x7F;
2527 lwkt_serialize_enter(&sc->vtnet_slz);
2529 /* Update shadow VLAN table. */
2532 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2535 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2538 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2539 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2540 device_printf(sc->vtnet_dev,
2541 "cannot %s VLAN %d %s the host filter table\n",
2542 add ? "add" : "remove", tag,
2543 add ? "to" : "from");
2547 lwkt_serialize_exit(&sc->vtnet_slz);
2551 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2554 if (ifp->if_softc != arg)
2557 vtnet_set_vlan_filter(arg, 1, tag);
2561 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2564 if (ifp->if_softc != arg)
2567 vtnet_set_vlan_filter(arg, 0, tag);
2571 vtnet_ifmedia_upd(struct ifnet *ifp)
2573 struct vtnet_softc *sc;
2574 struct ifmedia *ifm;
2577 ifm = &sc->vtnet_media;
2579 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2586 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2588 struct vtnet_softc *sc;
2592 ifmr->ifm_status = IFM_AVALID;
2593 ifmr->ifm_active = IFM_ETHER;
2595 lwkt_serialize_enter(&sc->vtnet_slz);
2596 if (vtnet_is_link_up(sc) != 0) {
2597 ifmr->ifm_status |= IFM_ACTIVE;
2598 ifmr->ifm_active |= VTNET_MEDIATYPE;
2600 ifmr->ifm_active |= IFM_NONE;
2601 lwkt_serialize_exit(&sc->vtnet_slz);
2605 vtnet_add_statistics(struct vtnet_softc *sc)
2608 struct vtnet_statistics *stats;
2609 //struct sysctl_ctx_list *ctx;
2610 //struct sysctl_oid *tree;
2611 //struct sysctl_oid_list *child;
2614 dev = sc->vtnet_dev;
2615 stats = &sc->vtnet_stats;
2616 sysctl_ctx_init(&sc->vtnet_sysctl_ctx);
2617 sc->vtnet_sysctl_tree = SYSCTL_ADD_NODE(&sc->vtnet_sysctl_ctx,
2618 SYSCTL_STATIC_CHILDREN(_hw),
2620 device_get_nameunit(dev),
2623 if (sc->vtnet_sysctl_tree == NULL) {
2624 device_printf(dev, "can't add sysctl node\n");
2629 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,
2630 SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO,
2631 "mbuf_alloc_failed", CTLFLAG_RD, &stats->mbuf_alloc_failed,
2632 "Mbuf cluster allocation failures");
2633 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,
2634 SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO,
2635 "rx_frame_too_large", CTLFLAG_RD, &stats->rx_frame_too_large,
2636 "Received frame larger than the mbuf chain");
2637 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_enq_replacement_failed",
2638 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
2639 "Enqueuing the replacement receive mbuf failed");
2640 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_mergeable_failed",
2641 CTLFLAG_RD, &stats->rx_mergeable_failed,
2642 "Mergeable buffers receive failures");
2643 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_ethtype",
2644 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
2645 "Received checksum offloaded buffer with unsupported "
2647 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_start",
2648 CTLFLAG_RD, &stats->rx_csum_bad_start,
2649 "Received checksum offloaded buffer with incorrect start offset");
2650 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_ipproto",
2651 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
2652 "Received checksum offloaded buffer with incorrect IP protocol");
2653 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_offset",
2654 CTLFLAG_RD, &stats->rx_csum_bad_offset,
2655 "Received checksum offloaded buffer with incorrect offset");
2656 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_failed",
2657 CTLFLAG_RD, &stats->rx_csum_failed,
2658 "Received buffer checksum offload failed");
2659 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_offloaded",
2660 CTLFLAG_RD, &stats->rx_csum_offloaded,
2661 "Received buffer checksum offload succeeded");
2662 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_task_rescheduled",
2663 CTLFLAG_RD, &stats->rx_task_rescheduled,
2664 "Times the receive interrupt task rescheduled itself");
2666 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_csum_offloaded",
2667 CTLFLAG_RD, &stats->tx_csum_offloaded,
2668 "Offloaded checksum of transmitted buffer");
2669 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_tso_offloaded",
2670 CTLFLAG_RD, &stats->tx_tso_offloaded,
2671 "Segmentation offload of transmitted buffer");
2672 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_csum_bad_ethtype",
2673 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
2674 "Aborted transmit of checksum offloaded buffer with unknown "
2676 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_tso_bad_ethtype",
2677 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
2678 "Aborted transmit of TSO buffer with unknown Ethernet type");
2679 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_task_rescheduled",
2680 CTLFLAG_RD, &stats->tx_task_rescheduled,
2681 "Times the transmit interrupt task rescheduled itself");
2685 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2688 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2692 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2695 virtqueue_disable_intr(sc->vtnet_rx_vq);
2699 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2702 #ifdef VTNET_TX_INTR_MODERATION
2705 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2710 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2713 virtqueue_disable_intr(sc->vtnet_tx_vq);