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 *vtnet_txhdrarea;
114 uint32_t vtnet_txhdridx;
115 struct vtnet_mac_filter *vtnet_macfilter;
120 int vtnet_rx_process_limit;
121 int vtnet_rx_mbuf_size;
122 int vtnet_rx_mbuf_count;
124 int vtnet_watchdog_timer;
125 uint64_t vtnet_features;
127 struct task vtnet_cfgchg_task;
129 struct vtnet_statistics vtnet_stats;
131 struct sysctl_ctx_list vtnet_sysctl_ctx;
132 struct sysctl_oid *vtnet_sysctl_tree;
134 struct callout vtnet_tick_ch;
136 eventhandler_tag vtnet_vlan_attach;
137 eventhandler_tag vtnet_vlan_detach;
139 struct ifmedia vtnet_media;
141 * Fake media type; the host does not provide us with
142 * any real media information.
144 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX)
145 char vtnet_hwaddr[ETHER_ADDR_LEN];
148 * During reset, the host's VLAN filtering table is lost. The
149 * array below is used to restore all the VLANs configured on
150 * this interface after a reset.
152 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32)
154 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
156 char vtnet_mtx_name[16];
160 * When mergeable buffers are not negotiated, the vtnet_rx_header structure
161 * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
162 * both keep the VirtIO header and the data non-contiguous and to keep the
163 * frame's payload 4 byte aligned.
165 * When mergeable buffers are negotiated, the host puts the VirtIO header in
166 * the beginning of the first mbuf's data.
168 #define VTNET_RX_HEADER_PAD 4
169 struct vtnet_rx_header {
170 struct virtio_net_hdr vrh_hdr;
171 char vrh_pad[VTNET_RX_HEADER_PAD];
175 * For each outgoing frame, the vtnet_tx_header below is allocated from
176 * the vtnet_tx_header_zone.
178 struct vtnet_tx_header {
180 struct virtio_net_hdr hdr;
181 struct virtio_net_hdr_mrg_rxbuf mhdr;
184 struct mbuf *vth_mbuf;
187 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
190 * The VirtIO specification does not place a limit on the number of MAC
191 * addresses the guest driver may request to be filtered. In practice,
192 * the host is constrained by available resources. To simplify this driver,
193 * impose a reasonably high limit of MAC addresses we will filter before
194 * falling back to promiscuous or all-multicast modes.
196 #define VTNET_MAX_MAC_ENTRIES 128
198 struct vtnet_mac_table {
200 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
203 struct vtnet_mac_filter {
204 struct vtnet_mac_table vmf_unicast;
205 uint32_t vmf_pad; /* Make tables non-contiguous. */
206 struct vtnet_mac_table vmf_multicast;
209 #define VTNET_WATCHDOG_TIMEOUT 5
210 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP)// | CSUM_SCTP)
212 /* Features desired/implemented by this driver. */
213 #define VTNET_FEATURES \
214 (VIRTIO_NET_F_MAC | \
215 VIRTIO_NET_F_STATUS | \
216 VIRTIO_NET_F_CTRL_VQ | \
217 VIRTIO_NET_F_CTRL_RX | \
218 VIRTIO_NET_F_CTRL_VLAN | \
219 VIRTIO_NET_F_CSUM | \
220 VIRTIO_NET_F_HOST_TSO4 | \
221 VIRTIO_NET_F_HOST_TSO6 | \
222 VIRTIO_NET_F_HOST_ECN | \
223 VIRTIO_NET_F_GUEST_CSUM | \
224 VIRTIO_NET_F_GUEST_TSO4 | \
225 VIRTIO_NET_F_GUEST_TSO6 | \
226 VIRTIO_NET_F_GUEST_ECN | \
227 VIRTIO_NET_F_MRG_RXBUF)
230 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
231 * frames larger than 1514 bytes. We do not yet support software LRO
234 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
235 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
237 #define VTNET_MAX_MTU 65536
238 #define VTNET_MAX_RX_SIZE 65550
241 * Used to preallocate the Vq indirect descriptors. The first segment
242 * is reserved for the header.
244 #define VTNET_MIN_RX_SEGS 2
245 #define VTNET_MAX_RX_SEGS 34
246 #define VTNET_MAX_TX_SEGS 34
248 #define IFCAP_TSO4 0x00100 /* can do TCP Segmentation Offload */
249 #define IFCAP_TSO6 0x00200 /* can do TCP6 Segmentation Offload */
250 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */
251 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */
252 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */
256 * Assert we can receive and transmit the maximum with regular
259 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
260 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
263 * Determine how many mbufs are in each receive buffer. For LRO without
264 * mergeable descriptors, we must allocate an mbuf chain large enough to
265 * hold both the vtnet_rx_header and the maximum receivable data.
267 #define VTNET_NEEDED_RX_MBUFS(_sc) \
268 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \
269 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \
270 (_sc)->vtnet_rx_mbuf_size)
272 static int vtnet_modevent(module_t, int, void *);
274 static int vtnet_probe(device_t);
275 static int vtnet_attach(device_t);
276 static int vtnet_detach(device_t);
277 static int vtnet_suspend(device_t);
278 static int vtnet_resume(device_t);
279 static int vtnet_shutdown(device_t);
280 static int vtnet_config_change(device_t);
282 static void vtnet_negotiate_features(struct vtnet_softc *);
283 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
284 static void vtnet_get_hwaddr(struct vtnet_softc *);
285 static void vtnet_set_hwaddr(struct vtnet_softc *);
286 static int vtnet_is_link_up(struct vtnet_softc *);
287 static void vtnet_update_link_status(struct vtnet_softc *);
289 static void vtnet_watchdog(struct vtnet_softc *);
291 static void vtnet_config_change_task(void *, int);
292 static int vtnet_change_mtu(struct vtnet_softc *, int);
293 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
295 static int vtnet_init_rx_vq(struct vtnet_softc *);
296 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
297 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
298 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
300 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
302 static int vtnet_replace_rxbuf(struct vtnet_softc *,
304 static int vtnet_newbuf(struct vtnet_softc *);
305 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
306 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
307 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
308 static void vtnet_vlan_tag_remove(struct mbuf *);
309 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
310 struct virtio_net_hdr *);
311 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
312 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
313 static void vtnet_rx_intr_task(void *);
314 static int vtnet_rx_vq_intr(void *);
316 static void vtnet_txeof(struct vtnet_softc *);
317 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
318 struct virtio_net_hdr *);
319 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
320 struct vtnet_tx_header *);
321 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
322 static void vtnet_start_locked(struct ifnet *, struct ifaltq_subque *);
323 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
324 static void vtnet_tick(void *);
325 static void vtnet_tx_intr_task(void *);
326 static int vtnet_tx_vq_intr(void *);
328 static void vtnet_stop(struct vtnet_softc *);
329 static int vtnet_reinit(struct vtnet_softc *);
330 static void vtnet_init_locked(struct vtnet_softc *);
331 static void vtnet_init(void *);
333 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
334 struct sglist *, int, int);
336 static void vtnet_rx_filter(struct vtnet_softc *sc);
337 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
338 static int vtnet_set_promisc(struct vtnet_softc *, int);
339 static int vtnet_set_allmulti(struct vtnet_softc *, int);
340 static void vtnet_rx_filter_mac(struct vtnet_softc *);
342 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
343 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
344 static void vtnet_set_vlan_filter(struct vtnet_softc *, int, uint16_t);
345 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
346 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
348 static int vtnet_ifmedia_upd(struct ifnet *);
349 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
351 static void vtnet_add_statistics(struct vtnet_softc *);
353 static int vtnet_enable_rx_intr(struct vtnet_softc *);
354 static int vtnet_enable_tx_intr(struct vtnet_softc *);
355 static void vtnet_disable_rx_intr(struct vtnet_softc *);
356 static void vtnet_disable_tx_intr(struct vtnet_softc *);
359 static int vtnet_csum_disable = 0;
360 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
361 static int vtnet_tso_disable = 1;
362 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
363 static int vtnet_lro_disable = 1;
364 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
367 * Reducing the number of transmit completed interrupts can
368 * improve performance. To do so, the define below keeps the
369 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
370 * in the start and watchdog paths. The price to pay for this
371 * is the m_free'ing of transmitted mbufs may be delayed until
372 * the watchdog fires.
374 #define VTNET_TX_INTR_MODERATION
376 static struct virtio_feature_desc vtnet_feature_desc[] = {
377 { VIRTIO_NET_F_CSUM, "TxChecksum" },
378 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
379 { VIRTIO_NET_F_MAC, "MacAddress" },
380 { VIRTIO_NET_F_GSO, "TxAllGSO" },
381 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
382 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
383 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
384 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
385 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
386 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
387 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
388 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
389 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
390 { VIRTIO_NET_F_STATUS, "Status" },
391 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
392 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
393 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
394 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
395 { VIRTIO_NET_F_MQ, "RFS" },
399 static device_method_t vtnet_methods[] = {
400 /* Device methods. */
401 DEVMETHOD(device_probe, vtnet_probe),
402 DEVMETHOD(device_attach, vtnet_attach),
403 DEVMETHOD(device_detach, vtnet_detach),
404 DEVMETHOD(device_suspend, vtnet_suspend),
405 DEVMETHOD(device_resume, vtnet_resume),
406 DEVMETHOD(device_shutdown, vtnet_shutdown),
408 /* VirtIO methods. */
409 DEVMETHOD(virtio_config_change, vtnet_config_change),
414 static driver_t vtnet_driver = {
417 sizeof(struct vtnet_softc)
420 static devclass_t vtnet_devclass;
422 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
424 MODULE_VERSION(vtnet, 1);
425 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
428 vtnet_modevent(module_t mod, int type, void *unused)
450 vtnet_probe(device_t dev)
452 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
455 device_set_desc(dev, "VirtIO Networking Adapter");
457 return (BUS_PROBE_DEFAULT);
461 vtnet_attach(device_t dev)
463 struct vtnet_softc *sc;
467 sc = device_get_softc(dev);
470 lwkt_serialize_init(&sc->vtnet_slz);
471 callout_init(&sc->vtnet_tick_ch);
473 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
475 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
476 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
478 vtnet_add_statistics(sc);
480 virtio_set_feature_desc(dev, vtnet_feature_desc);
481 vtnet_negotiate_features(sc);
483 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
484 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
485 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
487 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
490 sc->vtnet_rx_mbuf_size = MCLBYTES;
491 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
493 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
494 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
496 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
497 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
498 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
499 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
502 vtnet_get_hwaddr(sc);
504 error = vtnet_alloc_virtqueues(sc);
506 device_printf(dev, "cannot allocate virtqueues\n");
510 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
512 device_printf(dev, "cannot allocate ifnet structure\n");
518 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
519 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
520 ifp->if_init = vtnet_init;
521 ifp->if_start = vtnet_start;
522 ifp->if_ioctl = vtnet_ioctl;
524 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
525 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
527 tx_size = virtqueue_size(sc->vtnet_tx_vq);
528 sc->vtnet_tx_size = tx_size;
529 sc->vtnet_txhdridx = 0;
530 sc->vtnet_txhdrarea = contigmalloc(
531 ((sc->vtnet_tx_size / 2) + 1) * sizeof(struct vtnet_tx_header),
532 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
533 if (sc->vtnet_txhdrarea == NULL) {
534 device_printf(dev, "cannot contigmalloc the tx headers\n");
537 sc->vtnet_macfilter = contigmalloc(
538 sizeof(struct vtnet_mac_filter),
539 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
540 if (sc->vtnet_macfilter == NULL) {
542 "cannot contigmalloc the mac filter table\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 if (sc->vtnet_txhdrarea != NULL) {
680 contigfree(sc->vtnet_txhdrarea,
681 ((sc->vtnet_tx_size / 2) + 1) *
682 sizeof(struct vtnet_tx_header), M_VTNET);
683 sc->vtnet_txhdrarea = NULL;
685 if (sc->vtnet_macfilter != NULL) {
686 contigfree(sc->vtnet_macfilter,
687 sizeof(struct vtnet_mac_filter), M_DEVBUF);
688 sc->vtnet_macfilter = NULL;
691 ifmedia_removeall(&sc->vtnet_media);
697 vtnet_suspend(device_t dev)
699 struct vtnet_softc *sc;
701 sc = device_get_softc(dev);
703 lwkt_serialize_enter(&sc->vtnet_slz);
705 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
706 lwkt_serialize_exit(&sc->vtnet_slz);
712 vtnet_resume(device_t dev)
714 struct vtnet_softc *sc;
717 sc = device_get_softc(dev);
720 lwkt_serialize_enter(&sc->vtnet_slz);
721 if (ifp->if_flags & IFF_UP)
722 vtnet_init_locked(sc);
723 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
724 lwkt_serialize_exit(&sc->vtnet_slz);
730 vtnet_shutdown(device_t dev)
734 * Suspend already does all of what we need to
735 * do here; we just never expect to be resumed.
737 return (vtnet_suspend(dev));
741 vtnet_config_change(device_t dev)
743 struct vtnet_softc *sc;
745 sc = device_get_softc(dev);
747 taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->vtnet_cfgchg_task);
753 vtnet_negotiate_features(struct vtnet_softc *sc)
756 uint64_t mask, features;
761 if (vtnet_csum_disable)
762 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
765 * TSO and LRO are only available when their corresponding
766 * checksum offload feature is also negotiated.
769 if (vtnet_csum_disable || vtnet_tso_disable)
770 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
771 VIRTIO_NET_F_HOST_ECN;
773 if (vtnet_csum_disable || vtnet_lro_disable)
774 mask |= VTNET_LRO_FEATURES;
776 features = VTNET_FEATURES & ~mask;
777 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
778 sc->vtnet_features = virtio_negotiate_features(dev, features);
782 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
785 struct vq_alloc_info vq_info[3];
792 * Indirect descriptors are not needed for the Rx
793 * virtqueue when mergeable buffers are negotiated.
794 * The header is placed inline with the data, not
795 * in a separate descriptor, and mbuf clusters are
796 * always physically contiguous.
798 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
799 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
800 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
804 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
805 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
806 "%s receive", device_get_nameunit(dev));
808 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
809 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
810 "%s transmit", device_get_nameunit(dev));
812 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
815 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
816 &sc->vtnet_ctrl_vq, "%s control",
817 device_get_nameunit(dev));
820 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
824 vtnet_get_hwaddr(struct vtnet_softc *sc)
830 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
831 virtio_read_device_config(dev,
832 offsetof(struct virtio_net_config, mac),
833 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
835 /* Generate random locally administered unicast address. */
836 sc->vtnet_hwaddr[0] = 0xB2;
837 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
839 vtnet_set_hwaddr(sc);
844 vtnet_set_hwaddr(struct vtnet_softc *sc)
850 virtio_write_device_config(dev,
851 offsetof(struct virtio_net_config, mac),
852 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
856 vtnet_is_link_up(struct vtnet_softc *sc)
865 ASSERT_SERIALIZED(&sc->vtnet_slz);
867 status = virtio_read_dev_config_2(dev,
868 offsetof(struct virtio_net_config, status));
870 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
874 vtnet_update_link_status(struct vtnet_softc *sc)
878 struct ifaltq_subque *ifsq;
883 ifsq = ifq_get_subq_default(&ifp->if_snd);
885 link = vtnet_is_link_up(sc);
887 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
888 sc->vtnet_flags |= VTNET_FLAG_LINK;
890 device_printf(dev, "Link is up\n");
891 ifp->if_link_state = LINK_STATE_UP;
892 if_link_state_change(ifp);
893 if (!ifsq_is_empty(ifsq))
894 vtnet_start_locked(ifp, ifsq);
895 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
896 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
898 device_printf(dev, "Link is down\n");
900 ifp->if_link_state = LINK_STATE_DOWN;
901 if_link_state_change(ifp);
907 vtnet_watchdog(struct vtnet_softc *sc)
913 #ifdef VTNET_TX_INTR_MODERATION
917 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
920 if_printf(ifp, "watchdog timeout -- resetting\n");
922 virtqueue_dump(sc->vtnet_tx_vq);
925 ifp->if_flags &= ~IFF_RUNNING;
926 vtnet_init_locked(sc);
931 vtnet_config_change_task(void *arg, int pending)
933 struct vtnet_softc *sc;
937 lwkt_serialize_enter(&sc->vtnet_slz);
938 vtnet_update_link_status(sc);
939 lwkt_serialize_exit(&sc->vtnet_slz);
943 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
945 struct vtnet_softc *sc;
947 int reinit, mask, error;
950 ifr = (struct ifreq *) data;
956 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
958 else if (ifp->if_mtu != ifr->ifr_mtu) {
959 lwkt_serialize_enter(&sc->vtnet_slz);
960 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
961 lwkt_serialize_exit(&sc->vtnet_slz);
966 lwkt_serialize_enter(&sc->vtnet_slz);
967 if ((ifp->if_flags & IFF_UP) == 0) {
968 if (ifp->if_flags & IFF_RUNNING)
970 } else if (ifp->if_flags & IFF_RUNNING) {
971 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
972 (IFF_PROMISC | IFF_ALLMULTI)) {
973 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
979 vtnet_init_locked(sc);
982 sc->vtnet_if_flags = ifp->if_flags;
983 lwkt_serialize_exit(&sc->vtnet_slz);
988 lwkt_serialize_enter(&sc->vtnet_slz);
989 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
990 (ifp->if_flags & IFF_RUNNING))
991 vtnet_rx_filter_mac(sc);
992 lwkt_serialize_exit(&sc->vtnet_slz);
997 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1001 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1003 lwkt_serialize_enter(&sc->vtnet_slz);
1005 if (mask & IFCAP_TXCSUM) {
1006 ifp->if_capenable ^= IFCAP_TXCSUM;
1007 if (ifp->if_capenable & IFCAP_TXCSUM)
1008 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1010 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1013 if (mask & IFCAP_TSO4) {
1014 ifp->if_capenable ^= IFCAP_TSO4;
1015 if (ifp->if_capenable & IFCAP_TSO4)
1016 ifp->if_hwassist |= CSUM_TSO;
1018 ifp->if_hwassist &= ~CSUM_TSO;
1021 if (mask & IFCAP_RXCSUM) {
1022 ifp->if_capenable ^= IFCAP_RXCSUM;
1026 if (mask & IFCAP_LRO) {
1027 ifp->if_capenable ^= IFCAP_LRO;
1031 if (mask & IFCAP_VLAN_HWFILTER) {
1032 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1036 if (mask & IFCAP_VLAN_HWTSO)
1037 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1039 if (mask & IFCAP_VLAN_HWTAGGING)
1040 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1042 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1043 ifp->if_flags &= ~IFF_RUNNING;
1044 vtnet_init_locked(sc);
1046 //VLAN_CAPABILITIES(ifp);
1048 lwkt_serialize_exit(&sc->vtnet_slz);
1052 error = ether_ioctl(ifp, cmd, data);
1060 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1063 int new_frame_size, clsize;
1065 ifp = sc->vtnet_ifp;
1067 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1068 new_frame_size = sizeof(struct vtnet_rx_header) +
1069 sizeof(struct ether_vlan_header) + new_mtu;
1071 if (new_frame_size > MJUM9BYTES)
1074 if (new_frame_size <= MCLBYTES)
1077 clsize = MJUM9BYTES;
1079 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1080 sizeof(struct ether_vlan_header) + new_mtu;
1082 if (new_frame_size <= MCLBYTES)
1085 clsize = MJUMPAGESIZE;
1088 sc->vtnet_rx_mbuf_size = clsize;
1089 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1090 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1091 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1093 ifp->if_mtu = new_mtu;
1095 if (ifp->if_flags & IFF_RUNNING) {
1096 ifp->if_flags &= ~IFF_RUNNING;
1097 vtnet_init_locked(sc);
1104 vtnet_init_rx_vq(struct vtnet_softc *sc)
1106 struct virtqueue *vq;
1109 vq = sc->vtnet_rx_vq;
1113 while (!virtqueue_full(vq)) {
1114 if ((error = vtnet_newbuf(sc)) != 0)
1120 virtqueue_notify(vq, &sc->vtnet_slz);
1123 * EMSGSIZE signifies the virtqueue did not have enough
1124 * entries available to hold the last mbuf. This is not
1125 * an error. We should not get ENOSPC since we check if
1126 * the virtqueue is full before attempting to add a
1129 if (error == EMSGSIZE)
1137 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1139 struct virtqueue *vq;
1143 vq = sc->vtnet_rx_vq;
1146 while ((m = virtqueue_drain(vq, &last)) != NULL)
1149 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1153 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1155 struct virtqueue *vq;
1156 struct vtnet_tx_header *txhdr;
1159 vq = sc->vtnet_tx_vq;
1162 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1163 m_freem(txhdr->vth_mbuf);
1166 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1170 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1173 * The control virtqueue is only polled, therefore
1174 * it should already be empty.
1176 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1177 ("Ctrl Vq not empty"));
1180 static struct mbuf *
1181 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1183 struct mbuf *m_head, *m_tail, *m;
1186 clsize = sc->vtnet_rx_mbuf_size;
1188 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1189 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1190 m_head = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR );
1194 m_head->m_len = clsize;
1198 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1199 ("chained Rx mbuf requested without LRO_NOMRG"));
1201 for (i = 0; i < nbufs - 1; i++) {
1202 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1203 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1213 if (m_tailp != NULL)
1219 sc->vtnet_stats.mbuf_alloc_failed++;
1226 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1228 struct mbuf *m, *m_prev;
1229 struct mbuf *m_new, *m_tail;
1230 int len, clsize, nreplace, error;
1237 clsize = sc->vtnet_rx_mbuf_size;
1240 if (m->m_next != NULL)
1241 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1242 ("chained Rx mbuf without LRO_NOMRG"));
1245 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1246 * allocating an entire chain for each received frame. When
1247 * the received frame's length is less than that of the chain,
1248 * the unused mbufs are reassigned to the new chain.
1252 * Something is seriously wrong if we received
1253 * a frame larger than the mbuf chain. Drop it.
1256 sc->vtnet_stats.rx_frame_too_large++;
1260 KASSERT(m->m_len == clsize,
1261 ("mbuf length not expected cluster size: %d",
1264 m->m_len = MIN(m->m_len, len);
1272 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1273 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1274 ("too many replacement mbufs: %d/%d", nreplace,
1275 sc->vtnet_rx_mbuf_count));
1277 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1278 if (m_new == NULL) {
1279 m_prev->m_len = clsize;
1284 * Move unused mbufs, if any, from the original chain
1285 * onto the end of the new chain.
1287 if (m_prev->m_next != NULL) {
1288 m_tail->m_next = m_prev->m_next;
1289 m_prev->m_next = NULL;
1292 error = vtnet_enqueue_rxbuf(sc, m_new);
1295 * BAD! We could not enqueue the replacement mbuf chain. We
1296 * must restore the m0 chain to the original state if it was
1297 * modified so we can subsequently discard it.
1299 * NOTE: The replacement is suppose to be an identical copy
1300 * to the one just dequeued so this is an unexpected error.
1302 sc->vtnet_stats.rx_enq_replacement_failed++;
1304 if (m_tail->m_next != NULL) {
1305 m_prev->m_next = m_tail->m_next;
1306 m_tail->m_next = NULL;
1309 m_prev->m_len = clsize;
1317 vtnet_newbuf(struct vtnet_softc *sc)
1322 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1326 error = vtnet_enqueue_rxbuf(sc, m);
1334 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1336 struct virtqueue *vq;
1339 vq = sc->vtnet_rx_vq;
1341 while (--nbufs > 0) {
1342 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1344 vtnet_discard_rxbuf(sc, m);
1349 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1354 * Requeue the discarded mbuf. This should always be
1355 * successful since it was just dequeued.
1357 error = vtnet_enqueue_rxbuf(sc, m);
1358 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1362 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1365 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1366 struct vtnet_rx_header *rxhdr;
1367 struct virtio_net_hdr *hdr;
1371 ASSERT_SERIALIZED(&sc->vtnet_slz);
1372 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1373 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1375 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1377 mdata = mtod(m, uint8_t *);
1380 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1381 rxhdr = (struct vtnet_rx_header *) mdata;
1382 hdr = &rxhdr->vrh_hdr;
1383 offset += sizeof(struct vtnet_rx_header);
1385 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1386 KASSERT(error == 0, ("cannot add header to sglist"));
1389 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1393 if (m->m_next != NULL) {
1394 error = sglist_append_mbuf(&sg, m->m_next);
1399 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1403 vtnet_vlan_tag_remove(struct mbuf *m)
1405 struct ether_vlan_header *evl;
1407 evl = mtod(m, struct ether_vlan_header *);
1409 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1410 m->m_flags |= M_VLANTAG;
1412 /* Strip the 802.1Q header. */
1413 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1414 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1415 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1419 * Alternative method of doing receive checksum offloading. Rather
1420 * than parsing the received frame down to the IP header, use the
1421 * csum_offset to determine which CSUM_* flags are appropriate. We
1422 * can get by with doing this only because the checksum offsets are
1423 * unique for the things we care about.
1426 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1427 struct virtio_net_hdr *hdr)
1429 struct ether_header *eh;
1430 struct ether_vlan_header *evh;
1435 csum_len = hdr->csum_start + hdr->csum_offset;
1437 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1439 if (m->m_len < csum_len)
1442 eh = mtod(m, struct ether_header *);
1443 eth_type = ntohs(eh->ether_type);
1444 if (eth_type == ETHERTYPE_VLAN) {
1445 evh = mtod(m, struct ether_vlan_header *);
1446 eth_type = ntohs(evh->evl_proto);
1449 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1450 sc->vtnet_stats.rx_csum_bad_ethtype++;
1454 /* Use the offset to determine the appropriate CSUM_* flags. */
1455 switch (hdr->csum_offset) {
1456 case offsetof(struct udphdr, uh_sum):
1457 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1459 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1460 if (udp->uh_sum == 0)
1465 case offsetof(struct tcphdr, th_sum):
1466 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1467 m->m_pkthdr.csum_data = 0xFFFF;
1470 case offsetof(struct sctphdr, checksum):
1471 //m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1475 sc->vtnet_stats.rx_csum_bad_offset++;
1479 sc->vtnet_stats.rx_csum_offloaded++;
1485 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1488 struct virtqueue *vq;
1489 struct mbuf *m, *m_tail;
1492 ifp = sc->vtnet_ifp;
1493 vq = sc->vtnet_rx_vq;
1496 while (--nbufs > 0) {
1497 m = virtqueue_dequeue(vq, &len);
1503 if (vtnet_newbuf(sc) != 0) {
1505 vtnet_discard_rxbuf(sc, m);
1507 vtnet_discard_merged_rxbuf(sc, nbufs);
1515 m->m_flags &= ~M_PKTHDR;
1517 m_head->m_pkthdr.len += len;
1525 sc->vtnet_stats.rx_mergeable_failed++;
1532 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1534 struct virtio_net_hdr lhdr;
1536 struct virtqueue *vq;
1538 struct ether_header *eh;
1539 struct virtio_net_hdr *hdr;
1540 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1541 int len, deq, nbufs, adjsz, rx_npkts;
1543 ifp = sc->vtnet_ifp;
1544 vq = sc->vtnet_rx_vq;
1549 ASSERT_SERIALIZED(&sc->vtnet_slz);
1551 while (--count >= 0) {
1552 m = virtqueue_dequeue(vq, &len);
1557 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1559 vtnet_discard_rxbuf(sc, m);
1563 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1565 adjsz = sizeof(struct vtnet_rx_header);
1567 * Account for our pad between the header and
1568 * the actual start of the frame.
1570 len += VTNET_RX_HEADER_PAD;
1572 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1573 nbufs = mhdr->num_buffers;
1574 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1577 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1579 vtnet_discard_rxbuf(sc, m);
1581 vtnet_discard_merged_rxbuf(sc, nbufs);
1585 m->m_pkthdr.len = len;
1586 m->m_pkthdr.rcvif = ifp;
1587 m->m_pkthdr.csum_flags = 0;
1590 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1597 * Save copy of header before we strip it. For both mergeable
1598 * and non-mergeable, the VirtIO header is placed first in the
1599 * mbuf's data. We no longer need num_buffers, so always use a
1602 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1605 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1606 eh = mtod(m, struct ether_header *);
1607 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1608 vtnet_vlan_tag_remove(m);
1611 * With the 802.1Q header removed, update the
1612 * checksum starting location accordingly.
1614 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1616 ETHER_VLAN_ENCAP_LEN;
1620 if (ifp->if_capenable & IFCAP_RXCSUM &&
1621 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1622 if (vtnet_rx_csum(sc, m, hdr) != 0)
1623 sc->vtnet_stats.rx_csum_failed++;
1626 lwkt_serialize_exit(&sc->vtnet_slz);
1628 ifp->if_input(ifp, m, NULL, -1);
1629 lwkt_serialize_enter(&sc->vtnet_slz);
1632 * The interface may have been stopped while we were
1633 * passing the packet up the network stack.
1635 if ((ifp->if_flags & IFF_RUNNING) == 0)
1639 virtqueue_notify(vq, &sc->vtnet_slz);
1641 if (rx_npktsp != NULL)
1642 *rx_npktsp = rx_npkts;
1644 return (count > 0 ? 0 : EAGAIN);
1648 vtnet_rx_intr_task(void *arg)
1650 struct vtnet_softc *sc;
1655 ifp = sc->vtnet_ifp;
1658 // lwkt_serialize_enter(&sc->vtnet_slz);
1660 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1661 vtnet_enable_rx_intr(sc);
1662 // lwkt_serialize_exit(&sc->vtnet_slz);
1666 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1667 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1668 vtnet_disable_rx_intr(sc);
1672 // lwkt_serialize_exit(&sc->vtnet_slz);
1675 sc->vtnet_stats.rx_task_rescheduled++;
1681 vtnet_rx_vq_intr(void *xsc)
1683 struct vtnet_softc *sc;
1687 vtnet_disable_rx_intr(sc);
1688 vtnet_rx_intr_task(sc);
1694 vtnet_txeof(struct vtnet_softc *sc)
1696 struct virtqueue *vq;
1698 struct vtnet_tx_header *txhdr;
1701 vq = sc->vtnet_tx_vq;
1702 ifp = sc->vtnet_ifp;
1705 ASSERT_SERIALIZED(&sc->vtnet_slz);
1707 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1710 m_freem(txhdr->vth_mbuf);
1714 ifq_clr_oactive(&ifp->if_snd);
1715 if (virtqueue_empty(vq))
1716 sc->vtnet_watchdog_timer = 0;
1720 static struct mbuf *
1721 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1722 struct virtio_net_hdr *hdr)
1725 struct ether_header *eh;
1726 struct ether_vlan_header *evh;
1728 struct ip6_hdr *ip6;
1731 uint16_t eth_type, csum_start;
1732 uint8_t ip_proto, gso_type;
1734 ifp = sc->vtnet_ifp;
1737 ip_offset = sizeof(struct ether_header);
1738 if (m->m_len < ip_offset) {
1739 if ((m = m_pullup(m, ip_offset)) == NULL)
1743 eh = mtod(m, struct ether_header *);
1744 eth_type = ntohs(eh->ether_type);
1745 if (eth_type == ETHERTYPE_VLAN) {
1746 ip_offset = sizeof(struct ether_vlan_header);
1747 if (m->m_len < ip_offset) {
1748 if ((m = m_pullup(m, ip_offset)) == NULL)
1751 evh = mtod(m, struct ether_vlan_header *);
1752 eth_type = ntohs(evh->evl_proto);
1757 if (m->m_len < ip_offset + sizeof(struct ip)) {
1758 m = m_pullup(m, ip_offset + sizeof(struct ip));
1763 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1764 ip_proto = ip->ip_p;
1765 csum_start = ip_offset + (ip->ip_hl << 2);
1766 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1769 case ETHERTYPE_IPV6:
1770 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1771 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1776 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1778 * XXX Assume no extension headers are present. Presently,
1779 * this will always be true in the case of TSO, and FreeBSD
1780 * does not perform checksum offloading of IPv6 yet.
1782 ip_proto = ip6->ip6_nxt;
1783 csum_start = ip_offset + sizeof(struct ip6_hdr);
1784 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1791 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1792 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1793 hdr->csum_start = csum_start;
1794 hdr->csum_offset = m->m_pkthdr.csum_data;
1796 sc->vtnet_stats.tx_csum_offloaded++;
1799 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1800 if (ip_proto != IPPROTO_TCP)
1803 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1804 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1809 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1810 hdr->gso_type = gso_type;
1811 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1812 hdr->gso_size = m->m_pkthdr.tso_segsz;
1814 if (tcp->th_flags & TH_CWR) {
1816 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1817 * ECN support is only configurable globally with the
1818 * net.inet.tcp.ecn.enable sysctl knob.
1820 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1821 if_printf(ifp, "TSO with ECN not supported "
1827 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1830 sc->vtnet_stats.tx_tso_offloaded++;
1837 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1838 struct vtnet_tx_header *txhdr)
1841 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1842 struct virtqueue *vq;
1844 int collapsed, error;
1846 vq = sc->vtnet_tx_vq;
1850 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1851 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1852 KASSERT(error == 0 && sg.sg_nseg == 1,
1853 ("cannot add header to sglist"));
1856 error = sglist_append_mbuf(&sg, m);
1861 //m = m_collapse(m, MB_DONTWAIT, VTNET_MAX_TX_SEGS - 1);
1862 m = m_defrag(m, MB_DONTWAIT);
1871 txhdr->vth_mbuf = m;
1873 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1882 static struct mbuf *
1883 vtnet_vlan_tag_insert(struct mbuf *m)
1886 struct ether_vlan_header *evl;
1888 if (M_WRITABLE(m) == 0) {
1889 n = m_dup(m, MB_DONTWAIT);
1891 if ((m = n) == NULL)
1895 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, MB_DONTWAIT);
1898 if (m->m_len < sizeof(struct ether_vlan_header)) {
1899 m = m_pullup(m, sizeof(struct ether_vlan_header));
1904 /* Insert 802.1Q header into the existing Ethernet header. */
1905 evl = mtod(m, struct ether_vlan_header *);
1906 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1907 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1908 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1909 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1910 m->m_flags &= ~M_VLANTAG;
1916 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1918 struct vtnet_tx_header *txhdr;
1919 struct virtio_net_hdr *hdr;
1923 txhdr = &sc->vtnet_txhdrarea[sc->vtnet_txhdridx];
1924 memset(txhdr, 0, sizeof(struct vtnet_tx_header));
1927 * Always use the non-mergeable header to simplify things. When
1928 * the mergeable feature is negotiated, the num_buffers field
1929 * must be set to zero. We use vtnet_hdr_size later to enqueue
1930 * the correct header size to the host.
1932 hdr = &txhdr->vth_uhdr.hdr;
1937 if (m->m_flags & M_VLANTAG) {
1938 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1939 m = vtnet_vlan_tag_insert(m);
1940 if ((*m_head = m) == NULL)
1942 m->m_flags &= ~M_VLANTAG;
1945 if (m->m_pkthdr.csum_flags != 0) {
1946 m = vtnet_tx_offload(sc, m, hdr);
1947 if ((*m_head = m) == NULL)
1951 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1953 sc->vtnet_txhdridx =
1954 (sc->vtnet_txhdridx + 1) % ((sc->vtnet_tx_size / 2) + 1);
1960 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1962 struct vtnet_softc *sc;
1966 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1967 lwkt_serialize_enter(&sc->vtnet_slz);
1968 vtnet_start_locked(ifp, ifsq);
1969 lwkt_serialize_exit(&sc->vtnet_slz);
1973 vtnet_start_locked(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1975 struct vtnet_softc *sc;
1976 struct virtqueue *vq;
1981 vq = sc->vtnet_tx_vq;
1984 ASSERT_SERIALIZED(&sc->vtnet_slz);
1986 if ((ifp->if_flags & (IFF_RUNNING)) !=
1987 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
1990 #ifdef VTNET_TX_INTR_MODERATION
1991 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
1995 while (!ifsq_is_empty(ifsq)) {
1996 if (virtqueue_full(vq)) {
1997 ifq_set_oactive(&ifp->if_snd);
2001 m0 = ifq_dequeue(&ifp->if_snd);
2005 if (vtnet_encap(sc, &m0) != 0) {
2008 ifq_prepend(&ifp->if_snd, m0);
2009 ifq_set_oactive(&ifp->if_snd);
2014 ETHER_BPF_MTAP(ifp, m0);
2018 virtqueue_notify(vq, &sc->vtnet_slz);
2019 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2024 vtnet_tick(void *xsc)
2026 struct vtnet_softc *sc;
2031 ASSERT_SERIALIZED(&sc->vtnet_slz);
2033 virtqueue_dump(sc->vtnet_rx_vq);
2034 virtqueue_dump(sc->vtnet_tx_vq);
2038 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2043 vtnet_tx_intr_task(void *arg)
2045 struct vtnet_softc *sc;
2047 struct ifaltq_subque *ifsq;
2050 ifp = sc->vtnet_ifp;
2051 ifsq = ifq_get_subq_default(&ifp->if_snd);
2054 // lwkt_serialize_enter(&sc->vtnet_slz);
2056 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2057 vtnet_enable_tx_intr(sc);
2058 // lwkt_serialize_exit(&sc->vtnet_slz);
2064 if (!ifsq_is_empty(ifsq))
2065 vtnet_start_locked(ifp, ifsq);
2067 if (vtnet_enable_tx_intr(sc) != 0) {
2068 vtnet_disable_tx_intr(sc);
2069 sc->vtnet_stats.tx_task_rescheduled++;
2070 // lwkt_serialize_exit(&sc->vtnet_slz);
2074 // lwkt_serialize_exit(&sc->vtnet_slz);
2078 vtnet_tx_vq_intr(void *xsc)
2080 struct vtnet_softc *sc;
2084 vtnet_disable_tx_intr(sc);
2085 vtnet_tx_intr_task(sc);
2091 vtnet_stop(struct vtnet_softc *sc)
2096 dev = sc->vtnet_dev;
2097 ifp = sc->vtnet_ifp;
2099 ASSERT_SERIALIZED(&sc->vtnet_slz);
2101 sc->vtnet_watchdog_timer = 0;
2102 callout_stop(&sc->vtnet_tick_ch);
2103 ifq_clr_oactive(&ifp->if_snd);
2104 ifp->if_flags &= ~(IFF_RUNNING);
2106 vtnet_disable_rx_intr(sc);
2107 vtnet_disable_tx_intr(sc);
2110 * Stop the host VirtIO adapter. Note this will reset the host
2111 * adapter's state back to the pre-initialized state, so in
2112 * order to make the device usable again, we must drive it
2113 * through virtio_reinit() and virtio_reinit_complete().
2117 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2119 vtnet_free_rx_mbufs(sc);
2120 vtnet_free_tx_mbufs(sc);
2124 vtnet_reinit(struct vtnet_softc *sc)
2129 ifp = sc->vtnet_ifp;
2130 features = sc->vtnet_features;
2133 * Re-negotiate with the host, removing any disabled receive
2134 * features. Transmit features are disabled only on our side
2135 * via if_capenable and if_hwassist.
2138 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2139 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2140 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2143 if (ifp->if_capabilities & IFCAP_LRO) {
2144 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2145 features &= ~VTNET_LRO_FEATURES;
2148 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2149 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2150 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2153 return (virtio_reinit(sc->vtnet_dev, features));
2157 vtnet_init_locked(struct vtnet_softc *sc)
2163 dev = sc->vtnet_dev;
2164 ifp = sc->vtnet_ifp;
2166 ASSERT_SERIALIZED(&sc->vtnet_slz);
2168 if (ifp->if_flags & IFF_RUNNING)
2171 /* Stop host's adapter, cancel any pending I/O. */
2174 /* Reinitialize the host device. */
2175 error = vtnet_reinit(sc);
2178 "reinitialization failed, stopping device...\n");
2183 /* Update host with assigned MAC address. */
2184 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2185 vtnet_set_hwaddr(sc);
2187 ifp->if_hwassist = 0;
2188 if (ifp->if_capenable & IFCAP_TXCSUM)
2189 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2190 if (ifp->if_capenable & IFCAP_TSO4)
2191 ifp->if_hwassist |= CSUM_TSO;
2193 error = vtnet_init_rx_vq(sc);
2196 "cannot allocate mbufs for Rx virtqueue\n");
2201 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2202 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2203 /* Restore promiscuous and all-multicast modes. */
2204 vtnet_rx_filter(sc);
2206 /* Restore filtered MAC addresses. */
2207 vtnet_rx_filter_mac(sc);
2210 /* Restore VLAN filters. */
2211 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2212 vtnet_rx_filter_vlan(sc);
2216 vtnet_enable_rx_intr(sc);
2217 vtnet_enable_tx_intr(sc);
2220 ifp->if_flags |= IFF_RUNNING;
2221 ifq_clr_oactive(&ifp->if_snd);
2223 virtio_reinit_complete(dev);
2225 vtnet_update_link_status(sc);
2226 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2230 vtnet_init(void *xsc)
2232 struct vtnet_softc *sc;
2236 lwkt_serialize_enter(&sc->vtnet_slz);
2237 vtnet_init_locked(sc);
2238 lwkt_serialize_exit(&sc->vtnet_slz);
2242 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2243 struct sglist *sg, int readable, int writable)
2245 struct virtqueue *vq;
2248 vq = sc->vtnet_ctrl_vq;
2250 ASSERT_SERIALIZED(&sc->vtnet_slz);
2251 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2252 ("no control virtqueue"));
2253 KASSERT(virtqueue_empty(vq),
2254 ("control command already enqueued"));
2256 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2259 virtqueue_notify(vq, &sc->vtnet_slz);
2262 * Poll until the command is complete. Previously, we would
2263 * sleep until the control virtqueue interrupt handler woke
2264 * us up, but dropping the VTNET_MTX leads to serialization
2267 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2268 * vectors. Two of those vectors are needed for the Rx and Tx
2269 * virtqueues. We do not support sharing both a Vq and config
2270 * changed notification on the same MSIX vector.
2272 c = virtqueue_poll(vq, NULL);
2273 KASSERT(c == cookie, ("unexpected control command response"));
2277 vtnet_rx_filter(struct vtnet_softc *sc)
2282 dev = sc->vtnet_dev;
2283 ifp = sc->vtnet_ifp;
2285 ASSERT_SERIALIZED(&sc->vtnet_slz);
2286 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2287 ("CTRL_RX feature not negotiated"));
2289 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2290 device_printf(dev, "cannot %s promiscuous mode\n",
2291 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2293 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2294 device_printf(dev, "cannot %s all-multicast mode\n",
2295 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2299 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2301 struct virtio_net_ctrl_hdr hdr __aligned(2);
2302 struct sglist_seg segs[3];
2307 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
2312 hdr.class = VIRTIO_NET_CTRL_RX;
2315 ack = VIRTIO_NET_ERR;
2317 sglist_init(&sg, 3, segs);
2318 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2319 error |= sglist_append(&sg, &onoff, sizeof(uint8_t));
2320 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2321 KASSERT(error == 0 && sg.sg_nseg == 3,
2322 ("error adding Rx filter message to sglist"));
2324 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2326 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2330 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2333 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2337 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2340 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2344 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2346 struct virtio_net_ctrl_hdr hdr __aligned(2);
2347 struct vtnet_mac_filter *filter;
2348 struct sglist_seg segs[4];
2352 struct ifaddr_container *ifac;
2353 struct ifmultiaddr *ifma;
2354 int ucnt, mcnt, promisc, allmulti, error;
2357 ifp = sc->vtnet_ifp;
2364 ASSERT_SERIALIZED(&sc->vtnet_slz);
2365 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2366 ("CTRL_RX feature not negotiated"));
2368 /* Use the MAC filtering table allocated in vtnet_attach. */
2369 filter = sc->vtnet_macfilter;
2370 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2372 /* Unicast MAC addresses: */
2373 //if_addr_rlock(ifp);
2374 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2376 if (ifa->ifa_addr->sa_family != AF_LINK)
2378 else if (ucnt == VTNET_MAX_MAC_ENTRIES)
2381 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2382 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2385 //if_addr_runlock(ifp);
2387 if (ucnt >= VTNET_MAX_MAC_ENTRIES) {
2389 filter->vmf_unicast.nentries = 0;
2391 if_printf(ifp, "more than %d MAC addresses assigned, "
2392 "falling back to promiscuous mode\n",
2393 VTNET_MAX_MAC_ENTRIES);
2395 filter->vmf_unicast.nentries = ucnt;
2397 /* Multicast MAC addresses: */
2398 //if_maddr_rlock(ifp);
2399 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2400 if (ifma->ifma_addr->sa_family != AF_LINK)
2402 else if (mcnt == VTNET_MAX_MAC_ENTRIES)
2405 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2406 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2409 //if_maddr_runlock(ifp);
2411 if (mcnt >= VTNET_MAX_MAC_ENTRIES) {
2413 filter->vmf_multicast.nentries = 0;
2415 if_printf(ifp, "more than %d multicast MAC addresses "
2416 "assigned, falling back to all-multicast mode\n",
2417 VTNET_MAX_MAC_ENTRIES);
2419 filter->vmf_multicast.nentries = mcnt;
2421 if (promisc && allmulti)
2424 hdr.class = VIRTIO_NET_CTRL_MAC;
2425 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2426 ack = VIRTIO_NET_ERR;
2428 sglist_init(&sg, 4, segs);
2429 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2430 error |= sglist_append(&sg, &filter->vmf_unicast,
2431 sizeof(struct vtnet_mac_table));
2432 error |= sglist_append(&sg, &filter->vmf_multicast,
2433 sizeof(struct vtnet_mac_table));
2434 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2435 KASSERT(error == 0 && sg.sg_nseg == 4,
2436 ("error adding MAC filtering message to sglist"));
2438 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2440 if (ack != VIRTIO_NET_OK)
2441 if_printf(ifp, "error setting host MAC filter table\n");
2445 if (vtnet_set_promisc(sc, 1) != 0)
2446 if_printf(ifp, "cannot enable promiscuous mode\n");
2448 if (vtnet_set_allmulti(sc, 1) != 0)
2449 if_printf(ifp, "cannot enable all-multicast mode\n");
2453 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2455 struct virtio_net_ctrl_hdr hdr __aligned(2);
2456 struct sglist_seg segs[3];
2461 hdr.class = VIRTIO_NET_CTRL_VLAN;
2462 hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2463 ack = VIRTIO_NET_ERR;
2466 sglist_init(&sg, 3, segs);
2467 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2468 error |= sglist_append(&sg, &tag, sizeof(uint16_t));
2469 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2470 KASSERT(error == 0 && sg.sg_nseg == 3,
2471 ("error adding VLAN control message to sglist"));
2473 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2475 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2479 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2484 int i, nvlans, error;
2486 ASSERT_SERIALIZED(&sc->vtnet_slz);
2487 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2488 ("VLAN_FILTER feature not negotiated"));
2490 dev = sc->vtnet_dev;
2491 nvlans = sc->vtnet_nvlans;
2494 /* Enable filtering for each configured VLAN. */
2495 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2496 w = sc->vtnet_vlan_shadow[i];
2497 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) {
2498 if ((w & mask) != 0) {
2501 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0)
2507 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2509 device_printf(dev, "cannot restore VLAN filter table\n");
2513 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2518 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2519 ("VLAN_FILTER feature not negotiated"));
2521 if ((tag == 0) || (tag > 4095))
2524 ifp = sc->vtnet_ifp;
2525 idx = (tag >> 5) & 0x7F;
2528 lwkt_serialize_enter(&sc->vtnet_slz);
2530 /* Update shadow VLAN table. */
2533 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2536 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2539 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2540 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2541 device_printf(sc->vtnet_dev,
2542 "cannot %s VLAN %d %s the host filter table\n",
2543 add ? "add" : "remove", tag,
2544 add ? "to" : "from");
2548 lwkt_serialize_exit(&sc->vtnet_slz);
2552 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2555 if (ifp->if_softc != arg)
2558 vtnet_set_vlan_filter(arg, 1, tag);
2562 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2565 if (ifp->if_softc != arg)
2568 vtnet_set_vlan_filter(arg, 0, tag);
2572 vtnet_ifmedia_upd(struct ifnet *ifp)
2574 struct vtnet_softc *sc;
2575 struct ifmedia *ifm;
2578 ifm = &sc->vtnet_media;
2580 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2587 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2589 struct vtnet_softc *sc;
2593 ifmr->ifm_status = IFM_AVALID;
2594 ifmr->ifm_active = IFM_ETHER;
2596 lwkt_serialize_enter(&sc->vtnet_slz);
2597 if (vtnet_is_link_up(sc) != 0) {
2598 ifmr->ifm_status |= IFM_ACTIVE;
2599 ifmr->ifm_active |= VTNET_MEDIATYPE;
2601 ifmr->ifm_active |= IFM_NONE;
2602 lwkt_serialize_exit(&sc->vtnet_slz);
2606 vtnet_add_statistics(struct vtnet_softc *sc)
2609 struct vtnet_statistics *stats;
2610 //struct sysctl_ctx_list *ctx;
2611 //struct sysctl_oid *tree;
2612 //struct sysctl_oid_list *child;
2615 dev = sc->vtnet_dev;
2616 stats = &sc->vtnet_stats;
2617 sysctl_ctx_init(&sc->vtnet_sysctl_ctx);
2618 sc->vtnet_sysctl_tree = SYSCTL_ADD_NODE(&sc->vtnet_sysctl_ctx,
2619 SYSCTL_STATIC_CHILDREN(_hw),
2621 device_get_nameunit(dev),
2624 if (sc->vtnet_sysctl_tree == NULL) {
2625 device_printf(dev, "can't add sysctl node\n");
2630 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,
2631 SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO,
2632 "mbuf_alloc_failed", CTLFLAG_RD, &stats->mbuf_alloc_failed,
2633 "Mbuf cluster allocation failures");
2634 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,
2635 SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO,
2636 "rx_frame_too_large", CTLFLAG_RD, &stats->rx_frame_too_large,
2637 "Received frame larger than the mbuf chain");
2638 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_enq_replacement_failed",
2639 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
2640 "Enqueuing the replacement receive mbuf failed");
2641 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_mergeable_failed",
2642 CTLFLAG_RD, &stats->rx_mergeable_failed,
2643 "Mergeable buffers receive failures");
2644 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_ethtype",
2645 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
2646 "Received checksum offloaded buffer with unsupported "
2648 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_start",
2649 CTLFLAG_RD, &stats->rx_csum_bad_start,
2650 "Received checksum offloaded buffer with incorrect start offset");
2651 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_ipproto",
2652 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
2653 "Received checksum offloaded buffer with incorrect IP protocol");
2654 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_offset",
2655 CTLFLAG_RD, &stats->rx_csum_bad_offset,
2656 "Received checksum offloaded buffer with incorrect offset");
2657 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_failed",
2658 CTLFLAG_RD, &stats->rx_csum_failed,
2659 "Received buffer checksum offload failed");
2660 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_offloaded",
2661 CTLFLAG_RD, &stats->rx_csum_offloaded,
2662 "Received buffer checksum offload succeeded");
2663 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_task_rescheduled",
2664 CTLFLAG_RD, &stats->rx_task_rescheduled,
2665 "Times the receive interrupt task rescheduled itself");
2667 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_csum_offloaded",
2668 CTLFLAG_RD, &stats->tx_csum_offloaded,
2669 "Offloaded checksum of transmitted buffer");
2670 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_tso_offloaded",
2671 CTLFLAG_RD, &stats->tx_tso_offloaded,
2672 "Segmentation offload of transmitted buffer");
2673 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_csum_bad_ethtype",
2674 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
2675 "Aborted transmit of checksum offloaded buffer with unknown "
2677 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_tso_bad_ethtype",
2678 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
2679 "Aborted transmit of TSO buffer with unknown Ethernet type");
2680 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_task_rescheduled",
2681 CTLFLAG_RD, &stats->tx_task_rescheduled,
2682 "Times the transmit interrupt task rescheduled itself");
2686 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2689 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2693 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2696 virtqueue_disable_intr(sc->vtnet_rx_vq);
2700 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2703 #ifdef VTNET_TX_INTR_MODERATION
2706 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2711 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2714 virtqueue_disable_intr(sc->vtnet_tx_vq);