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
252 #define IFCAP_TSO4 0x00100 /* can do TCP Segmentation Offload */
253 #define IFCAP_TSO6 0x00200 /* can do TCP6 Segmentation Offload */
254 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */
255 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */
256 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */
260 * Assert we can receive and transmit the maximum with regular
263 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
264 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
267 * Determine how many mbufs are in each receive buffer. For LRO without
268 * mergeable descriptors, we must allocate an mbuf chain large enough to
269 * hold both the vtnet_rx_header and the maximum receivable data.
271 #define VTNET_NEEDED_RX_MBUFS(_sc) \
272 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \
273 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \
274 (_sc)->vtnet_rx_mbuf_size)
276 static int vtnet_modevent(module_t, int, void *);
278 static int vtnet_probe(device_t);
279 static int vtnet_attach(device_t);
280 static int vtnet_detach(device_t);
281 static int vtnet_suspend(device_t);
282 static int vtnet_resume(device_t);
283 static int vtnet_shutdown(device_t);
284 static int vtnet_config_change(device_t);
286 static void vtnet_negotiate_features(struct vtnet_softc *);
287 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
288 static void vtnet_get_hwaddr(struct vtnet_softc *);
289 static void vtnet_set_hwaddr(struct vtnet_softc *);
290 static int vtnet_is_link_up(struct vtnet_softc *);
291 static void vtnet_update_link_status(struct vtnet_softc *);
293 static void vtnet_watchdog(struct vtnet_softc *);
295 static void vtnet_config_change_task(void *, int);
296 static int vtnet_change_mtu(struct vtnet_softc *, int);
297 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
299 static int vtnet_init_rx_vq(struct vtnet_softc *);
300 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
301 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
302 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
304 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
306 static int vtnet_replace_rxbuf(struct vtnet_softc *,
308 static int vtnet_newbuf(struct vtnet_softc *);
309 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
310 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
311 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
312 static void vtnet_vlan_tag_remove(struct mbuf *);
313 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
314 struct virtio_net_hdr *);
315 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
316 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
317 static void vtnet_rx_intr_task(void *);
318 static int vtnet_rx_vq_intr(void *);
320 static void vtnet_txeof(struct vtnet_softc *);
321 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
322 struct virtio_net_hdr *);
323 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
324 struct vtnet_tx_header *);
325 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
326 static void vtnet_start_locked(struct ifnet *, struct ifaltq_subque *);
327 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
328 static void vtnet_tick(void *);
329 static void vtnet_tx_intr_task(void *);
330 static int vtnet_tx_vq_intr(void *);
332 static void vtnet_stop(struct vtnet_softc *);
333 static int vtnet_reinit(struct vtnet_softc *);
334 static void vtnet_init_locked(struct vtnet_softc *);
335 static void vtnet_init(void *);
337 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
338 struct sglist *, int, int);
340 static void vtnet_rx_filter(struct vtnet_softc *sc);
341 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
342 static int vtnet_set_promisc(struct vtnet_softc *, int);
343 static int vtnet_set_allmulti(struct vtnet_softc *, int);
344 static void vtnet_rx_filter_mac(struct vtnet_softc *);
346 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
347 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
348 static void vtnet_set_vlan_filter(struct vtnet_softc *, int, uint16_t);
349 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
350 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
352 static int vtnet_ifmedia_upd(struct ifnet *);
353 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
355 static void vtnet_add_statistics(struct vtnet_softc *);
357 static int vtnet_enable_rx_intr(struct vtnet_softc *);
358 static int vtnet_enable_tx_intr(struct vtnet_softc *);
359 static void vtnet_disable_rx_intr(struct vtnet_softc *);
360 static void vtnet_disable_tx_intr(struct vtnet_softc *);
363 static int vtnet_csum_disable = 0;
364 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
365 static int vtnet_tso_disable = 1;
366 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
367 static int vtnet_lro_disable = 1;
368 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
371 * Reducing the number of transmit completed interrupts can
372 * improve performance. To do so, the define below keeps the
373 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
374 * in the start and watchdog paths. The price to pay for this
375 * is the m_free'ing of transmitted mbufs may be delayed until
376 * the watchdog fires.
378 #define VTNET_TX_INTR_MODERATION
380 static struct virtio_feature_desc vtnet_feature_desc[] = {
381 { VIRTIO_NET_F_CSUM, "TxChecksum" },
382 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
383 { VIRTIO_NET_F_MAC, "MacAddress" },
384 { VIRTIO_NET_F_GSO, "TxAllGSO" },
385 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
386 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
387 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
388 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
389 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
390 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
391 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
392 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
393 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
394 { VIRTIO_NET_F_STATUS, "Status" },
395 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
396 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
397 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
398 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
399 { VIRTIO_NET_F_MQ, "RFS" },
403 static device_method_t vtnet_methods[] = {
404 /* Device methods. */
405 DEVMETHOD(device_probe, vtnet_probe),
406 DEVMETHOD(device_attach, vtnet_attach),
407 DEVMETHOD(device_detach, vtnet_detach),
408 DEVMETHOD(device_suspend, vtnet_suspend),
409 DEVMETHOD(device_resume, vtnet_resume),
410 DEVMETHOD(device_shutdown, vtnet_shutdown),
412 /* VirtIO methods. */
413 DEVMETHOD(virtio_config_change, vtnet_config_change),
418 static driver_t vtnet_driver = {
421 sizeof(struct vtnet_softc)
424 static devclass_t vtnet_devclass;
426 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
428 MODULE_VERSION(vtnet, 1);
429 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
432 vtnet_modevent(module_t mod, int type, void *unused)
454 vtnet_probe(device_t dev)
456 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
459 device_set_desc(dev, "VirtIO Networking Adapter");
461 return (BUS_PROBE_DEFAULT);
465 vtnet_attach(device_t dev)
467 struct vtnet_softc *sc;
471 sc = device_get_softc(dev);
474 lwkt_serialize_init(&sc->vtnet_slz);
475 callout_init(&sc->vtnet_tick_ch);
477 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
479 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
480 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
482 vtnet_add_statistics(sc);
484 virtio_set_feature_desc(dev, vtnet_feature_desc);
485 vtnet_negotiate_features(sc);
487 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
488 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
489 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
491 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
494 sc->vtnet_rx_mbuf_size = MCLBYTES;
495 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
497 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
498 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
500 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
501 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
502 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
503 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
506 vtnet_get_hwaddr(sc);
508 error = vtnet_alloc_virtqueues(sc);
510 device_printf(dev, "cannot allocate virtqueues\n");
514 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
516 device_printf(dev, "cannot allocate ifnet structure\n");
522 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
523 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
524 ifp->if_init = vtnet_init;
525 ifp->if_start = vtnet_start;
526 ifp->if_ioctl = vtnet_ioctl;
528 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
529 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
531 tx_size = virtqueue_size(sc->vtnet_tx_vq);
532 sc->vtnet_tx_size = tx_size;
533 sc->vtnet_txhdridx = 0;
534 sc->vtnet_txhdrarea = contigmalloc(
535 ((sc->vtnet_tx_size / 2) + 1) * sizeof(struct vtnet_tx_header),
536 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
537 if (sc->vtnet_txhdrarea == NULL) {
538 device_printf(dev, "cannot contigmalloc the tx headers\n");
541 sc->vtnet_macfilter = contigmalloc(
542 sizeof(struct vtnet_mac_filter),
543 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
544 if (sc->vtnet_macfilter == NULL) {
546 "cannot contigmalloc the mac filter table\n");
549 ifq_set_maxlen(&ifp->if_snd, tx_size - 1);
550 ifq_set_ready(&ifp->if_snd);
552 ether_ifattach(ifp, sc->vtnet_hwaddr, NULL);
554 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)){
555 //ifp->if_capabilities |= IFCAP_LINKSTATE;
556 kprintf("add dynamic link state\n");
559 /* Tell the upper layer(s) we support long frames. */
560 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
561 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
563 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
564 ifp->if_capabilities |= IFCAP_TXCSUM;
566 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
567 ifp->if_capabilities |= IFCAP_TSO4;
568 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
569 ifp->if_capabilities |= IFCAP_TSO6;
570 if (ifp->if_capabilities & IFCAP_TSO)
571 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
573 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
574 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
577 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
578 ifp->if_capabilities |= IFCAP_RXCSUM;
580 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
581 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
582 ifp->if_capabilities |= IFCAP_LRO;
585 if (ifp->if_capabilities & IFCAP_HWCSUM) {
587 * VirtIO does not support VLAN tagging, but we can fake
588 * it by inserting and removing the 802.1Q header during
589 * transmit and receive. We are then able to do checksum
590 * offloading of VLAN frames.
592 ifp->if_capabilities |=
593 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
596 ifp->if_capenable = ifp->if_capabilities;
599 * Capabilities after here are not enabled by default.
602 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
603 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
605 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
606 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
607 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
608 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
611 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc);
613 error = virtio_setup_intr(dev, &sc->vtnet_slz);
615 device_printf(dev, "cannot setup virtqueue interrupts\n");
621 * Device defaults to promiscuous mode for backwards
622 * compatibility. Turn it off if possible.
624 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
625 lwkt_serialize_enter(&sc->vtnet_slz);
626 if (vtnet_set_promisc(sc, 0) != 0) {
627 ifp->if_flags |= IFF_PROMISC;
629 "cannot disable promiscuous mode\n");
631 lwkt_serialize_exit(&sc->vtnet_slz);
633 ifp->if_flags |= IFF_PROMISC;
643 vtnet_detach(device_t dev)
645 struct vtnet_softc *sc;
648 sc = device_get_softc(dev);
651 if (device_is_attached(dev)) {
652 lwkt_serialize_enter(&sc->vtnet_slz);
654 lwkt_serialize_exit(&sc->vtnet_slz);
656 callout_stop(&sc->vtnet_tick_ch);
657 taskqueue_drain(taskqueue_swi, &sc->vtnet_cfgchg_task);
662 if (sc->vtnet_vlan_attach != NULL) {
663 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
664 sc->vtnet_vlan_attach = NULL;
666 if (sc->vtnet_vlan_detach != NULL) {
667 EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach);
668 sc->vtnet_vlan_detach = NULL;
673 sc->vtnet_ifp = NULL;
676 if (sc->vtnet_rx_vq != NULL)
677 vtnet_free_rx_mbufs(sc);
678 if (sc->vtnet_tx_vq != NULL)
679 vtnet_free_tx_mbufs(sc);
680 if (sc->vtnet_ctrl_vq != NULL)
681 vtnet_free_ctrl_vq(sc);
683 if (sc->vtnet_txhdrarea != NULL) {
684 contigfree(sc->vtnet_txhdrarea,
685 ((sc->vtnet_tx_size / 2) + 1) *
686 sizeof(struct vtnet_tx_header), M_VTNET);
687 sc->vtnet_txhdrarea = NULL;
689 if (sc->vtnet_macfilter != NULL) {
690 contigfree(sc->vtnet_macfilter,
691 sizeof(struct vtnet_mac_filter), M_DEVBUF);
692 sc->vtnet_macfilter = NULL;
695 ifmedia_removeall(&sc->vtnet_media);
701 vtnet_suspend(device_t dev)
703 struct vtnet_softc *sc;
705 sc = device_get_softc(dev);
707 lwkt_serialize_enter(&sc->vtnet_slz);
709 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
710 lwkt_serialize_exit(&sc->vtnet_slz);
716 vtnet_resume(device_t dev)
718 struct vtnet_softc *sc;
721 sc = device_get_softc(dev);
724 lwkt_serialize_enter(&sc->vtnet_slz);
725 if (ifp->if_flags & IFF_UP)
726 vtnet_init_locked(sc);
727 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
728 lwkt_serialize_exit(&sc->vtnet_slz);
734 vtnet_shutdown(device_t dev)
738 * Suspend already does all of what we need to
739 * do here; we just never expect to be resumed.
741 return (vtnet_suspend(dev));
745 vtnet_config_change(device_t dev)
747 struct vtnet_softc *sc;
749 sc = device_get_softc(dev);
751 taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->vtnet_cfgchg_task);
757 vtnet_negotiate_features(struct vtnet_softc *sc)
760 uint64_t mask, features;
765 if (vtnet_csum_disable)
766 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
769 * TSO and LRO are only available when their corresponding
770 * checksum offload feature is also negotiated.
773 if (vtnet_csum_disable || vtnet_tso_disable)
774 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
775 VIRTIO_NET_F_HOST_ECN;
777 if (vtnet_csum_disable || vtnet_lro_disable)
778 mask |= VTNET_LRO_FEATURES;
780 features = VTNET_FEATURES & ~mask;
781 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
782 sc->vtnet_features = virtio_negotiate_features(dev, features);
786 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
789 struct vq_alloc_info vq_info[3];
796 * Indirect descriptors are not needed for the Rx
797 * virtqueue when mergeable buffers are negotiated.
798 * The header is placed inline with the data, not
799 * in a separate descriptor, and mbuf clusters are
800 * always physically contiguous.
802 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
803 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
804 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
808 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
809 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
810 "%s receive", device_get_nameunit(dev));
812 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
813 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
814 "%s transmit", device_get_nameunit(dev));
816 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
819 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
820 &sc->vtnet_ctrl_vq, "%s control",
821 device_get_nameunit(dev));
824 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
828 vtnet_get_hwaddr(struct vtnet_softc *sc)
834 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
835 virtio_read_device_config(dev,
836 offsetof(struct virtio_net_config, mac),
837 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
839 /* Generate random locally administered unicast address. */
840 sc->vtnet_hwaddr[0] = 0xB2;
841 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
843 vtnet_set_hwaddr(sc);
848 vtnet_set_hwaddr(struct vtnet_softc *sc)
854 virtio_write_device_config(dev,
855 offsetof(struct virtio_net_config, mac),
856 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
860 vtnet_is_link_up(struct vtnet_softc *sc)
869 ASSERT_SERIALIZED(&sc->vtnet_slz);
871 status = virtio_read_dev_config_2(dev,
872 offsetof(struct virtio_net_config, status));
874 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
878 vtnet_update_link_status(struct vtnet_softc *sc)
882 struct ifaltq_subque *ifsq;
887 ifsq = ifq_get_subq_default(&ifp->if_snd);
889 link = vtnet_is_link_up(sc);
891 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
892 sc->vtnet_flags |= VTNET_FLAG_LINK;
894 device_printf(dev, "Link is up\n");
895 ifp->if_link_state = LINK_STATE_UP;
896 if_link_state_change(ifp);
897 if (!ifsq_is_empty(ifsq))
898 vtnet_start_locked(ifp, ifsq);
899 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
900 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
902 device_printf(dev, "Link is down\n");
904 ifp->if_link_state = LINK_STATE_DOWN;
905 if_link_state_change(ifp);
911 vtnet_watchdog(struct vtnet_softc *sc)
917 #ifdef VTNET_TX_INTR_MODERATION
921 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
924 if_printf(ifp, "watchdog timeout -- resetting\n");
926 virtqueue_dump(sc->vtnet_tx_vq);
929 ifp->if_flags &= ~IFF_RUNNING;
930 vtnet_init_locked(sc);
935 vtnet_config_change_task(void *arg, int pending)
937 struct vtnet_softc *sc;
941 lwkt_serialize_enter(&sc->vtnet_slz);
942 vtnet_update_link_status(sc);
943 lwkt_serialize_exit(&sc->vtnet_slz);
947 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
949 struct vtnet_softc *sc;
951 int reinit, mask, error;
954 ifr = (struct ifreq *) data;
960 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
962 else if (ifp->if_mtu != ifr->ifr_mtu) {
963 lwkt_serialize_enter(&sc->vtnet_slz);
964 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
965 lwkt_serialize_exit(&sc->vtnet_slz);
970 lwkt_serialize_enter(&sc->vtnet_slz);
971 if ((ifp->if_flags & IFF_UP) == 0) {
972 if (ifp->if_flags & IFF_RUNNING)
974 } else if (ifp->if_flags & IFF_RUNNING) {
975 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
976 (IFF_PROMISC | IFF_ALLMULTI)) {
977 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
983 vtnet_init_locked(sc);
986 sc->vtnet_if_flags = ifp->if_flags;
987 lwkt_serialize_exit(&sc->vtnet_slz);
992 lwkt_serialize_enter(&sc->vtnet_slz);
993 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
994 (ifp->if_flags & IFF_RUNNING))
995 vtnet_rx_filter_mac(sc);
996 lwkt_serialize_exit(&sc->vtnet_slz);
1001 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1005 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1007 lwkt_serialize_enter(&sc->vtnet_slz);
1009 if (mask & IFCAP_TXCSUM) {
1010 ifp->if_capenable ^= IFCAP_TXCSUM;
1011 if (ifp->if_capenable & IFCAP_TXCSUM)
1012 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1014 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1017 if (mask & IFCAP_TSO4) {
1018 ifp->if_capenable ^= IFCAP_TSO4;
1019 if (ifp->if_capenable & IFCAP_TSO4)
1020 ifp->if_hwassist |= CSUM_TSO;
1022 ifp->if_hwassist &= ~CSUM_TSO;
1025 if (mask & IFCAP_RXCSUM) {
1026 ifp->if_capenable ^= IFCAP_RXCSUM;
1030 if (mask & IFCAP_LRO) {
1031 ifp->if_capenable ^= IFCAP_LRO;
1035 if (mask & IFCAP_VLAN_HWFILTER) {
1036 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1040 if (mask & IFCAP_VLAN_HWTSO)
1041 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1043 if (mask & IFCAP_VLAN_HWTAGGING)
1044 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1046 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1047 ifp->if_flags &= ~IFF_RUNNING;
1048 vtnet_init_locked(sc);
1050 //VLAN_CAPABILITIES(ifp);
1052 lwkt_serialize_exit(&sc->vtnet_slz);
1056 error = ether_ioctl(ifp, cmd, data);
1064 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1067 int new_frame_size, clsize;
1069 ifp = sc->vtnet_ifp;
1071 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1072 new_frame_size = sizeof(struct vtnet_rx_header) +
1073 sizeof(struct ether_vlan_header) + new_mtu;
1075 if (new_frame_size > MJUM9BYTES)
1078 if (new_frame_size <= MCLBYTES)
1081 clsize = MJUM9BYTES;
1083 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1084 sizeof(struct ether_vlan_header) + new_mtu;
1086 if (new_frame_size <= MCLBYTES)
1089 clsize = MJUMPAGESIZE;
1092 sc->vtnet_rx_mbuf_size = clsize;
1093 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1094 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1095 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1097 ifp->if_mtu = new_mtu;
1099 if (ifp->if_flags & IFF_RUNNING) {
1100 ifp->if_flags &= ~IFF_RUNNING;
1101 vtnet_init_locked(sc);
1108 vtnet_init_rx_vq(struct vtnet_softc *sc)
1110 struct virtqueue *vq;
1113 vq = sc->vtnet_rx_vq;
1117 while (!virtqueue_full(vq)) {
1118 if ((error = vtnet_newbuf(sc)) != 0)
1124 virtqueue_notify(vq, &sc->vtnet_slz);
1127 * EMSGSIZE signifies the virtqueue did not have enough
1128 * entries available to hold the last mbuf. This is not
1129 * an error. We should not get ENOSPC since we check if
1130 * the virtqueue is full before attempting to add a
1133 if (error == EMSGSIZE)
1141 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1143 struct virtqueue *vq;
1147 vq = sc->vtnet_rx_vq;
1150 while ((m = virtqueue_drain(vq, &last)) != NULL)
1153 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1157 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1159 struct virtqueue *vq;
1160 struct vtnet_tx_header *txhdr;
1163 vq = sc->vtnet_tx_vq;
1166 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1167 m_freem(txhdr->vth_mbuf);
1170 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1174 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1177 * The control virtqueue is only polled, therefore
1178 * it should already be empty.
1180 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1181 ("Ctrl Vq not empty"));
1184 static struct mbuf *
1185 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1187 struct mbuf *m_head, *m_tail, *m;
1190 clsize = sc->vtnet_rx_mbuf_size;
1192 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1193 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1194 m_head = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR );
1198 m_head->m_len = clsize;
1202 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1203 ("chained Rx mbuf requested without LRO_NOMRG"));
1205 for (i = 0; i < nbufs - 1; i++) {
1206 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1207 m = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1217 if (m_tailp != NULL)
1223 sc->vtnet_stats.mbuf_alloc_failed++;
1230 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1232 struct mbuf *m, *m_prev;
1233 struct mbuf *m_new, *m_tail;
1234 int len, clsize, nreplace, error;
1241 clsize = sc->vtnet_rx_mbuf_size;
1244 if (m->m_next != NULL)
1245 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1246 ("chained Rx mbuf without LRO_NOMRG"));
1249 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1250 * allocating an entire chain for each received frame. When
1251 * the received frame's length is less than that of the chain,
1252 * the unused mbufs are reassigned to the new chain.
1256 * Something is seriously wrong if we received
1257 * a frame larger than the mbuf chain. Drop it.
1260 sc->vtnet_stats.rx_frame_too_large++;
1264 KASSERT(m->m_len == clsize,
1265 ("mbuf length not expected cluster size: %d",
1268 m->m_len = MIN(m->m_len, len);
1276 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1277 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1278 ("too many replacement mbufs: %d/%d", nreplace,
1279 sc->vtnet_rx_mbuf_count));
1281 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1282 if (m_new == NULL) {
1283 m_prev->m_len = clsize;
1288 * Move unused mbufs, if any, from the original chain
1289 * onto the end of the new chain.
1291 if (m_prev->m_next != NULL) {
1292 m_tail->m_next = m_prev->m_next;
1293 m_prev->m_next = NULL;
1296 error = vtnet_enqueue_rxbuf(sc, m_new);
1299 * BAD! We could not enqueue the replacement mbuf chain. We
1300 * must restore the m0 chain to the original state if it was
1301 * modified so we can subsequently discard it.
1303 * NOTE: The replacement is suppose to be an identical copy
1304 * to the one just dequeued so this is an unexpected error.
1306 sc->vtnet_stats.rx_enq_replacement_failed++;
1308 if (m_tail->m_next != NULL) {
1309 m_prev->m_next = m_tail->m_next;
1310 m_tail->m_next = NULL;
1313 m_prev->m_len = clsize;
1321 vtnet_newbuf(struct vtnet_softc *sc)
1326 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1330 error = vtnet_enqueue_rxbuf(sc, m);
1338 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1340 struct virtqueue *vq;
1343 vq = sc->vtnet_rx_vq;
1345 while (--nbufs > 0) {
1346 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1348 vtnet_discard_rxbuf(sc, m);
1353 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1358 * Requeue the discarded mbuf. This should always be
1359 * successful since it was just dequeued.
1361 error = vtnet_enqueue_rxbuf(sc, m);
1362 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1366 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1369 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1370 struct vtnet_rx_header *rxhdr;
1371 struct virtio_net_hdr *hdr;
1375 ASSERT_SERIALIZED(&sc->vtnet_slz);
1376 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1377 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1379 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1381 mdata = mtod(m, uint8_t *);
1384 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1385 rxhdr = (struct vtnet_rx_header *) mdata;
1386 hdr = &rxhdr->vrh_hdr;
1387 offset += sizeof(struct vtnet_rx_header);
1389 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1390 KASSERT(error == 0, ("cannot add header to sglist"));
1393 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1397 if (m->m_next != NULL) {
1398 error = sglist_append_mbuf(&sg, m->m_next);
1403 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1407 vtnet_vlan_tag_remove(struct mbuf *m)
1409 struct ether_vlan_header *evl;
1411 evl = mtod(m, struct ether_vlan_header *);
1413 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1414 m->m_flags |= M_VLANTAG;
1416 /* Strip the 802.1Q header. */
1417 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1418 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1419 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1423 * Alternative method of doing receive checksum offloading. Rather
1424 * than parsing the received frame down to the IP header, use the
1425 * csum_offset to determine which CSUM_* flags are appropriate. We
1426 * can get by with doing this only because the checksum offsets are
1427 * unique for the things we care about.
1430 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1431 struct virtio_net_hdr *hdr)
1433 struct ether_header *eh;
1434 struct ether_vlan_header *evh;
1439 csum_len = hdr->csum_start + hdr->csum_offset;
1441 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1443 if (m->m_len < csum_len)
1446 eh = mtod(m, struct ether_header *);
1447 eth_type = ntohs(eh->ether_type);
1448 if (eth_type == ETHERTYPE_VLAN) {
1449 evh = mtod(m, struct ether_vlan_header *);
1450 eth_type = ntohs(evh->evl_proto);
1453 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1454 sc->vtnet_stats.rx_csum_bad_ethtype++;
1458 /* Use the offset to determine the appropriate CSUM_* flags. */
1459 switch (hdr->csum_offset) {
1460 case offsetof(struct udphdr, uh_sum):
1461 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1463 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1464 if (udp->uh_sum == 0)
1469 case offsetof(struct tcphdr, th_sum):
1470 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1471 m->m_pkthdr.csum_data = 0xFFFF;
1474 case offsetof(struct sctphdr, checksum):
1475 //m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1479 sc->vtnet_stats.rx_csum_bad_offset++;
1483 sc->vtnet_stats.rx_csum_offloaded++;
1489 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1492 struct virtqueue *vq;
1493 struct mbuf *m, *m_tail;
1496 ifp = sc->vtnet_ifp;
1497 vq = sc->vtnet_rx_vq;
1500 while (--nbufs > 0) {
1501 m = virtqueue_dequeue(vq, &len);
1507 if (vtnet_newbuf(sc) != 0) {
1509 vtnet_discard_rxbuf(sc, m);
1511 vtnet_discard_merged_rxbuf(sc, nbufs);
1519 m->m_flags &= ~M_PKTHDR;
1521 m_head->m_pkthdr.len += len;
1529 sc->vtnet_stats.rx_mergeable_failed++;
1536 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1538 struct virtio_net_hdr lhdr;
1540 struct virtqueue *vq;
1542 struct ether_header *eh;
1543 struct virtio_net_hdr *hdr;
1544 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1545 int len, deq, nbufs, adjsz, rx_npkts;
1547 ifp = sc->vtnet_ifp;
1548 vq = sc->vtnet_rx_vq;
1553 ASSERT_SERIALIZED(&sc->vtnet_slz);
1555 while (--count >= 0) {
1556 m = virtqueue_dequeue(vq, &len);
1561 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1563 vtnet_discard_rxbuf(sc, m);
1567 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1569 adjsz = sizeof(struct vtnet_rx_header);
1571 * Account for our pad between the header and
1572 * the actual start of the frame.
1574 len += VTNET_RX_HEADER_PAD;
1576 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1577 nbufs = mhdr->num_buffers;
1578 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1581 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1583 vtnet_discard_rxbuf(sc, m);
1585 vtnet_discard_merged_rxbuf(sc, nbufs);
1589 m->m_pkthdr.len = len;
1590 m->m_pkthdr.rcvif = ifp;
1591 m->m_pkthdr.csum_flags = 0;
1594 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1601 * Save copy of header before we strip it. For both mergeable
1602 * and non-mergeable, the VirtIO header is placed first in the
1603 * mbuf's data. We no longer need num_buffers, so always use a
1606 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1609 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1610 eh = mtod(m, struct ether_header *);
1611 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1612 vtnet_vlan_tag_remove(m);
1615 * With the 802.1Q header removed, update the
1616 * checksum starting location accordingly.
1618 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1620 ETHER_VLAN_ENCAP_LEN;
1624 if (ifp->if_capenable & IFCAP_RXCSUM &&
1625 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1626 if (vtnet_rx_csum(sc, m, hdr) != 0)
1627 sc->vtnet_stats.rx_csum_failed++;
1630 lwkt_serialize_exit(&sc->vtnet_slz);
1632 ifp->if_input(ifp, m, NULL, -1);
1633 lwkt_serialize_enter(&sc->vtnet_slz);
1636 * The interface may have been stopped while we were
1637 * passing the packet up the network stack.
1639 if ((ifp->if_flags & IFF_RUNNING) == 0)
1643 virtqueue_notify(vq, &sc->vtnet_slz);
1645 if (rx_npktsp != NULL)
1646 *rx_npktsp = rx_npkts;
1648 return (count > 0 ? 0 : EAGAIN);
1652 vtnet_rx_intr_task(void *arg)
1654 struct vtnet_softc *sc;
1659 ifp = sc->vtnet_ifp;
1661 // lwkt_serialize_enter(&sc->vtnet_slz);
1663 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1664 vtnet_enable_rx_intr(sc);
1665 // lwkt_serialize_exit(&sc->vtnet_slz);
1669 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1670 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1671 vtnet_disable_rx_intr(sc);
1675 // lwkt_serialize_exit(&sc->vtnet_slz);
1678 sc->vtnet_stats.rx_task_rescheduled++;
1680 vtnet_rx_intr_task(sc);
1685 vtnet_rx_vq_intr(void *xsc)
1687 struct vtnet_softc *sc;
1691 vtnet_disable_rx_intr(sc);
1692 vtnet_rx_intr_task(sc);
1698 vtnet_txeof(struct vtnet_softc *sc)
1700 struct virtqueue *vq;
1702 struct vtnet_tx_header *txhdr;
1705 vq = sc->vtnet_tx_vq;
1706 ifp = sc->vtnet_ifp;
1709 ASSERT_SERIALIZED(&sc->vtnet_slz);
1711 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1714 m_freem(txhdr->vth_mbuf);
1718 ifq_clr_oactive(&ifp->if_snd);
1719 if (virtqueue_empty(vq))
1720 sc->vtnet_watchdog_timer = 0;
1724 static struct mbuf *
1725 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1726 struct virtio_net_hdr *hdr)
1729 struct ether_header *eh;
1730 struct ether_vlan_header *evh;
1732 struct ip6_hdr *ip6;
1735 uint16_t eth_type, csum_start;
1736 uint8_t ip_proto, gso_type;
1738 ifp = sc->vtnet_ifp;
1741 ip_offset = sizeof(struct ether_header);
1742 if (m->m_len < ip_offset) {
1743 if ((m = m_pullup(m, ip_offset)) == NULL)
1747 eh = mtod(m, struct ether_header *);
1748 eth_type = ntohs(eh->ether_type);
1749 if (eth_type == ETHERTYPE_VLAN) {
1750 ip_offset = sizeof(struct ether_vlan_header);
1751 if (m->m_len < ip_offset) {
1752 if ((m = m_pullup(m, ip_offset)) == NULL)
1755 evh = mtod(m, struct ether_vlan_header *);
1756 eth_type = ntohs(evh->evl_proto);
1761 if (m->m_len < ip_offset + sizeof(struct ip)) {
1762 m = m_pullup(m, ip_offset + sizeof(struct ip));
1767 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1768 ip_proto = ip->ip_p;
1769 csum_start = ip_offset + (ip->ip_hl << 2);
1770 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1773 case ETHERTYPE_IPV6:
1774 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1775 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1780 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1782 * XXX Assume no extension headers are present. Presently,
1783 * this will always be true in the case of TSO, and FreeBSD
1784 * does not perform checksum offloading of IPv6 yet.
1786 ip_proto = ip6->ip6_nxt;
1787 csum_start = ip_offset + sizeof(struct ip6_hdr);
1788 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1795 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1796 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1797 hdr->csum_start = csum_start;
1798 hdr->csum_offset = m->m_pkthdr.csum_data;
1800 sc->vtnet_stats.tx_csum_offloaded++;
1803 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1804 if (ip_proto != IPPROTO_TCP)
1807 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1808 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1813 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1814 hdr->gso_type = gso_type;
1815 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1816 hdr->gso_size = m->m_pkthdr.tso_segsz;
1818 if (tcp->th_flags & TH_CWR) {
1820 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1821 * ECN support is only configurable globally with the
1822 * net.inet.tcp.ecn.enable sysctl knob.
1824 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1825 if_printf(ifp, "TSO with ECN not supported "
1831 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1834 sc->vtnet_stats.tx_tso_offloaded++;
1841 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1842 struct vtnet_tx_header *txhdr)
1845 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1846 struct virtqueue *vq;
1848 int collapsed, error;
1850 vq = sc->vtnet_tx_vq;
1854 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1855 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1856 KASSERT(error == 0 && sg.sg_nseg == 1,
1857 ("cannot add header to sglist"));
1860 error = sglist_append_mbuf(&sg, m);
1865 //m = m_collapse(m, MB_DONTWAIT, VTNET_MAX_TX_SEGS - 1);
1866 m = m_defrag(m, MB_DONTWAIT);
1875 txhdr->vth_mbuf = m;
1877 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1886 static struct mbuf *
1887 vtnet_vlan_tag_insert(struct mbuf *m)
1890 struct ether_vlan_header *evl;
1892 if (M_WRITABLE(m) == 0) {
1893 n = m_dup(m, MB_DONTWAIT);
1895 if ((m = n) == NULL)
1899 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, MB_DONTWAIT);
1902 if (m->m_len < sizeof(struct ether_vlan_header)) {
1903 m = m_pullup(m, sizeof(struct ether_vlan_header));
1908 /* Insert 802.1Q header into the existing Ethernet header. */
1909 evl = mtod(m, struct ether_vlan_header *);
1910 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1911 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1912 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1913 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1914 m->m_flags &= ~M_VLANTAG;
1920 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1922 struct vtnet_tx_header *txhdr;
1923 struct virtio_net_hdr *hdr;
1927 txhdr = &sc->vtnet_txhdrarea[sc->vtnet_txhdridx];
1928 memset(txhdr, 0, sizeof(struct vtnet_tx_header));
1931 * Always use the non-mergeable header to simplify things. When
1932 * the mergeable feature is negotiated, the num_buffers field
1933 * must be set to zero. We use vtnet_hdr_size later to enqueue
1934 * the correct header size to the host.
1936 hdr = &txhdr->vth_uhdr.hdr;
1941 if (m->m_flags & M_VLANTAG) {
1942 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1943 m = vtnet_vlan_tag_insert(m);
1944 if ((*m_head = m) == NULL)
1946 m->m_flags &= ~M_VLANTAG;
1949 if (m->m_pkthdr.csum_flags != 0) {
1950 m = vtnet_tx_offload(sc, m, hdr);
1951 if ((*m_head = m) == NULL)
1955 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1957 sc->vtnet_txhdridx =
1958 (sc->vtnet_txhdridx + 1) % ((sc->vtnet_tx_size / 2) + 1);
1964 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1966 struct vtnet_softc *sc;
1970 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1971 lwkt_serialize_enter(&sc->vtnet_slz);
1972 vtnet_start_locked(ifp, ifsq);
1973 lwkt_serialize_exit(&sc->vtnet_slz);
1977 vtnet_start_locked(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1979 struct vtnet_softc *sc;
1980 struct virtqueue *vq;
1985 vq = sc->vtnet_tx_vq;
1988 ASSERT_SERIALIZED(&sc->vtnet_slz);
1990 if ((ifp->if_flags & (IFF_RUNNING)) !=
1991 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
1994 #ifdef VTNET_TX_INTR_MODERATION
1995 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
1999 while (!ifsq_is_empty(ifsq)) {
2000 if (virtqueue_full(vq)) {
2001 ifq_set_oactive(&ifp->if_snd);
2005 m0 = ifq_dequeue(&ifp->if_snd);
2009 if (vtnet_encap(sc, &m0) != 0) {
2012 ifq_prepend(&ifp->if_snd, m0);
2013 ifq_set_oactive(&ifp->if_snd);
2018 ETHER_BPF_MTAP(ifp, m0);
2022 virtqueue_notify(vq, &sc->vtnet_slz);
2023 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2028 vtnet_tick(void *xsc)
2030 struct vtnet_softc *sc;
2035 ASSERT_SERIALIZED(&sc->vtnet_slz);
2037 virtqueue_dump(sc->vtnet_rx_vq);
2038 virtqueue_dump(sc->vtnet_tx_vq);
2042 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2047 vtnet_tx_intr_task(void *arg)
2049 struct vtnet_softc *sc;
2051 struct ifaltq_subque *ifsq;
2054 ifp = sc->vtnet_ifp;
2055 ifsq = ifq_get_subq_default(&ifp->if_snd);
2057 // lwkt_serialize_enter(&sc->vtnet_slz);
2059 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2060 vtnet_enable_tx_intr(sc);
2061 // lwkt_serialize_exit(&sc->vtnet_slz);
2067 if (!ifsq_is_empty(ifsq))
2068 vtnet_start_locked(ifp, ifsq);
2070 if (vtnet_enable_tx_intr(sc) != 0) {
2071 vtnet_disable_tx_intr(sc);
2072 sc->vtnet_stats.tx_task_rescheduled++;
2073 // lwkt_serialize_exit(&sc->vtnet_slz);
2074 vtnet_tx_intr_task(sc);
2079 // lwkt_serialize_exit(&sc->vtnet_slz);
2083 vtnet_tx_vq_intr(void *xsc)
2085 struct vtnet_softc *sc;
2089 vtnet_disable_tx_intr(sc);
2090 vtnet_tx_intr_task(sc);
2096 vtnet_stop(struct vtnet_softc *sc)
2101 dev = sc->vtnet_dev;
2102 ifp = sc->vtnet_ifp;
2104 ASSERT_SERIALIZED(&sc->vtnet_slz);
2106 sc->vtnet_watchdog_timer = 0;
2107 callout_stop(&sc->vtnet_tick_ch);
2108 ifq_clr_oactive(&ifp->if_snd);
2109 ifp->if_flags &= ~(IFF_RUNNING);
2111 vtnet_disable_rx_intr(sc);
2112 vtnet_disable_tx_intr(sc);
2115 * Stop the host VirtIO adapter. Note this will reset the host
2116 * adapter's state back to the pre-initialized state, so in
2117 * order to make the device usable again, we must drive it
2118 * through virtio_reinit() and virtio_reinit_complete().
2122 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2124 vtnet_free_rx_mbufs(sc);
2125 vtnet_free_tx_mbufs(sc);
2129 vtnet_reinit(struct vtnet_softc *sc)
2134 ifp = sc->vtnet_ifp;
2135 features = sc->vtnet_features;
2138 * Re-negotiate with the host, removing any disabled receive
2139 * features. Transmit features are disabled only on our side
2140 * via if_capenable and if_hwassist.
2143 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2144 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2145 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2148 if (ifp->if_capabilities & IFCAP_LRO) {
2149 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2150 features &= ~VTNET_LRO_FEATURES;
2153 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2154 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2155 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2158 return (virtio_reinit(sc->vtnet_dev, features));
2162 vtnet_init_locked(struct vtnet_softc *sc)
2168 dev = sc->vtnet_dev;
2169 ifp = sc->vtnet_ifp;
2171 ASSERT_SERIALIZED(&sc->vtnet_slz);
2173 if (ifp->if_flags & IFF_RUNNING)
2176 /* Stop host's adapter, cancel any pending I/O. */
2179 /* Reinitialize the host device. */
2180 error = vtnet_reinit(sc);
2183 "reinitialization failed, stopping device...\n");
2188 /* Update host with assigned MAC address. */
2189 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2190 vtnet_set_hwaddr(sc);
2192 ifp->if_hwassist = 0;
2193 if (ifp->if_capenable & IFCAP_TXCSUM)
2194 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2195 if (ifp->if_capenable & IFCAP_TSO4)
2196 ifp->if_hwassist |= CSUM_TSO;
2198 error = vtnet_init_rx_vq(sc);
2201 "cannot allocate mbufs for Rx virtqueue\n");
2206 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2207 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2208 /* Restore promiscuous and all-multicast modes. */
2209 vtnet_rx_filter(sc);
2211 /* Restore filtered MAC addresses. */
2212 vtnet_rx_filter_mac(sc);
2215 /* Restore VLAN filters. */
2216 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2217 vtnet_rx_filter_vlan(sc);
2221 vtnet_enable_rx_intr(sc);
2222 vtnet_enable_tx_intr(sc);
2225 ifp->if_flags |= IFF_RUNNING;
2226 ifq_clr_oactive(&ifp->if_snd);
2228 virtio_reinit_complete(dev);
2230 vtnet_update_link_status(sc);
2231 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2235 vtnet_init(void *xsc)
2237 struct vtnet_softc *sc;
2241 lwkt_serialize_enter(&sc->vtnet_slz);
2242 vtnet_init_locked(sc);
2243 lwkt_serialize_exit(&sc->vtnet_slz);
2247 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2248 struct sglist *sg, int readable, int writable)
2250 struct virtqueue *vq;
2253 vq = sc->vtnet_ctrl_vq;
2255 ASSERT_SERIALIZED(&sc->vtnet_slz);
2256 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2257 ("no control virtqueue"));
2258 KASSERT(virtqueue_empty(vq),
2259 ("control command already enqueued"));
2261 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2264 virtqueue_notify(vq, &sc->vtnet_slz);
2267 * Poll until the command is complete. Previously, we would
2268 * sleep until the control virtqueue interrupt handler woke
2269 * us up, but dropping the VTNET_MTX leads to serialization
2272 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2273 * vectors. Two of those vectors are needed for the Rx and Tx
2274 * virtqueues. We do not support sharing both a Vq and config
2275 * changed notification on the same MSIX vector.
2277 c = virtqueue_poll(vq, NULL);
2278 KASSERT(c == cookie, ("unexpected control command response"));
2282 vtnet_rx_filter(struct vtnet_softc *sc)
2287 dev = sc->vtnet_dev;
2288 ifp = sc->vtnet_ifp;
2290 ASSERT_SERIALIZED(&sc->vtnet_slz);
2291 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2292 ("CTRL_RX feature not negotiated"));
2294 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2295 device_printf(dev, "cannot %s promiscuous mode\n",
2296 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2298 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2299 device_printf(dev, "cannot %s all-multicast mode\n",
2300 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2304 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2306 struct virtio_net_ctrl_hdr hdr __aligned(2);
2307 struct sglist_seg segs[3];
2312 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
2317 hdr.class = VIRTIO_NET_CTRL_RX;
2320 ack = VIRTIO_NET_ERR;
2322 sglist_init(&sg, 3, segs);
2323 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2324 error |= sglist_append(&sg, &onoff, sizeof(uint8_t));
2325 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2326 KASSERT(error == 0 && sg.sg_nseg == 3,
2327 ("error adding Rx filter message to sglist"));
2329 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2331 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2335 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2338 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2342 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2345 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2349 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2351 struct virtio_net_ctrl_hdr hdr __aligned(2);
2352 struct vtnet_mac_filter *filter;
2353 struct sglist_seg segs[4];
2357 struct ifaddr_container *ifac;
2358 struct ifmultiaddr *ifma;
2359 int ucnt, mcnt, promisc, allmulti, error;
2362 ifp = sc->vtnet_ifp;
2369 ASSERT_SERIALIZED(&sc->vtnet_slz);
2370 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2371 ("CTRL_RX feature not negotiated"));
2373 /* Use the MAC filtering table allocated in vtnet_attach. */
2374 filter = sc->vtnet_macfilter;
2375 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2377 /* Unicast MAC addresses: */
2378 //if_addr_rlock(ifp);
2379 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2381 if (ifa->ifa_addr->sa_family != AF_LINK)
2383 else if (ucnt == VTNET_MAX_MAC_ENTRIES)
2386 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2387 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2390 //if_addr_runlock(ifp);
2392 if (ucnt >= VTNET_MAX_MAC_ENTRIES) {
2394 filter->vmf_unicast.nentries = 0;
2396 if_printf(ifp, "more than %d MAC addresses assigned, "
2397 "falling back to promiscuous mode\n",
2398 VTNET_MAX_MAC_ENTRIES);
2400 filter->vmf_unicast.nentries = ucnt;
2402 /* Multicast MAC addresses: */
2403 //if_maddr_rlock(ifp);
2404 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2405 if (ifma->ifma_addr->sa_family != AF_LINK)
2407 else if (mcnt == VTNET_MAX_MAC_ENTRIES)
2410 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2411 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2414 //if_maddr_runlock(ifp);
2416 if (mcnt >= VTNET_MAX_MAC_ENTRIES) {
2418 filter->vmf_multicast.nentries = 0;
2420 if_printf(ifp, "more than %d multicast MAC addresses "
2421 "assigned, falling back to all-multicast mode\n",
2422 VTNET_MAX_MAC_ENTRIES);
2424 filter->vmf_multicast.nentries = mcnt;
2426 if (promisc && allmulti)
2429 hdr.class = VIRTIO_NET_CTRL_MAC;
2430 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2431 ack = VIRTIO_NET_ERR;
2433 sglist_init(&sg, 4, segs);
2434 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2435 error |= sglist_append(&sg, &filter->vmf_unicast,
2436 sizeof(struct vtnet_mac_table));
2437 error |= sglist_append(&sg, &filter->vmf_multicast,
2438 sizeof(struct vtnet_mac_table));
2439 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2440 KASSERT(error == 0 && sg.sg_nseg == 4,
2441 ("error adding MAC filtering message to sglist"));
2443 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2445 if (ack != VIRTIO_NET_OK)
2446 if_printf(ifp, "error setting host MAC filter table\n");
2450 if (vtnet_set_promisc(sc, 1) != 0)
2451 if_printf(ifp, "cannot enable promiscuous mode\n");
2453 if (vtnet_set_allmulti(sc, 1) != 0)
2454 if_printf(ifp, "cannot enable all-multicast mode\n");
2458 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2460 struct virtio_net_ctrl_hdr hdr __aligned(2);
2461 struct sglist_seg segs[3];
2466 hdr.class = VIRTIO_NET_CTRL_VLAN;
2467 hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2468 ack = VIRTIO_NET_ERR;
2471 sglist_init(&sg, 3, segs);
2472 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2473 error |= sglist_append(&sg, &tag, sizeof(uint16_t));
2474 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2475 KASSERT(error == 0 && sg.sg_nseg == 3,
2476 ("error adding VLAN control message to sglist"));
2478 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2480 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2484 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2489 int i, nvlans, error;
2491 ASSERT_SERIALIZED(&sc->vtnet_slz);
2492 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2493 ("VLAN_FILTER feature not negotiated"));
2495 dev = sc->vtnet_dev;
2496 nvlans = sc->vtnet_nvlans;
2499 /* Enable filtering for each configured VLAN. */
2500 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2501 w = sc->vtnet_vlan_shadow[i];
2502 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) {
2503 if ((w & mask) != 0) {
2506 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0)
2512 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2514 device_printf(dev, "cannot restore VLAN filter table\n");
2518 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2523 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2524 ("VLAN_FILTER feature not negotiated"));
2526 if ((tag == 0) || (tag > 4095))
2529 ifp = sc->vtnet_ifp;
2530 idx = (tag >> 5) & 0x7F;
2533 lwkt_serialize_enter(&sc->vtnet_slz);
2535 /* Update shadow VLAN table. */
2538 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2541 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2544 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2545 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2546 device_printf(sc->vtnet_dev,
2547 "cannot %s VLAN %d %s the host filter table\n",
2548 add ? "add" : "remove", tag,
2549 add ? "to" : "from");
2553 lwkt_serialize_exit(&sc->vtnet_slz);
2557 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2560 if (ifp->if_softc != arg)
2563 vtnet_set_vlan_filter(arg, 1, tag);
2567 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2570 if (ifp->if_softc != arg)
2573 vtnet_set_vlan_filter(arg, 0, tag);
2577 vtnet_ifmedia_upd(struct ifnet *ifp)
2579 struct vtnet_softc *sc;
2580 struct ifmedia *ifm;
2583 ifm = &sc->vtnet_media;
2585 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2592 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2594 struct vtnet_softc *sc;
2598 ifmr->ifm_status = IFM_AVALID;
2599 ifmr->ifm_active = IFM_ETHER;
2601 lwkt_serialize_enter(&sc->vtnet_slz);
2602 if (vtnet_is_link_up(sc) != 0) {
2603 ifmr->ifm_status |= IFM_ACTIVE;
2604 ifmr->ifm_active |= VTNET_MEDIATYPE;
2606 ifmr->ifm_active |= IFM_NONE;
2607 lwkt_serialize_exit(&sc->vtnet_slz);
2611 vtnet_add_statistics(struct vtnet_softc *sc)
2614 struct vtnet_statistics *stats;
2615 //struct sysctl_ctx_list *ctx;
2616 //struct sysctl_oid *tree;
2617 //struct sysctl_oid_list *child;
2620 dev = sc->vtnet_dev;
2621 stats = &sc->vtnet_stats;
2622 sysctl_ctx_init(&sc->vtnet_sysctl_ctx);
2623 sc->vtnet_sysctl_tree = SYSCTL_ADD_NODE(&sc->vtnet_sysctl_ctx,
2624 SYSCTL_STATIC_CHILDREN(_hw),
2626 device_get_nameunit(dev),
2629 if (sc->vtnet_sysctl_tree == NULL) {
2630 device_printf(dev, "can't add sysctl node\n");
2635 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,
2636 SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO,
2637 "mbuf_alloc_failed", CTLFLAG_RD, &stats->mbuf_alloc_failed,
2638 "Mbuf cluster allocation failures");
2639 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,
2640 SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO,
2641 "rx_frame_too_large", CTLFLAG_RD, &stats->rx_frame_too_large,
2642 "Received frame larger than the mbuf chain");
2643 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx,SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_enq_replacement_failed",
2644 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
2645 "Enqueuing the replacement receive mbuf failed");
2646 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_mergeable_failed",
2647 CTLFLAG_RD, &stats->rx_mergeable_failed,
2648 "Mergeable buffers receive failures");
2649 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_ethtype",
2650 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
2651 "Received checksum offloaded buffer with unsupported "
2653 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_start",
2654 CTLFLAG_RD, &stats->rx_csum_bad_start,
2655 "Received checksum offloaded buffer with incorrect start offset");
2656 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_ipproto",
2657 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
2658 "Received checksum offloaded buffer with incorrect IP protocol");
2659 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_bad_offset",
2660 CTLFLAG_RD, &stats->rx_csum_bad_offset,
2661 "Received checksum offloaded buffer with incorrect offset");
2662 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_failed",
2663 CTLFLAG_RD, &stats->rx_csum_failed,
2664 "Received buffer checksum offload failed");
2665 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_csum_offloaded",
2666 CTLFLAG_RD, &stats->rx_csum_offloaded,
2667 "Received buffer checksum offload succeeded");
2668 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "rx_task_rescheduled",
2669 CTLFLAG_RD, &stats->rx_task_rescheduled,
2670 "Times the receive interrupt task rescheduled itself");
2672 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_csum_offloaded",
2673 CTLFLAG_RD, &stats->tx_csum_offloaded,
2674 "Offloaded checksum of transmitted buffer");
2675 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_tso_offloaded",
2676 CTLFLAG_RD, &stats->tx_tso_offloaded,
2677 "Segmentation offload of transmitted buffer");
2678 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_csum_bad_ethtype",
2679 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
2680 "Aborted transmit of checksum offloaded buffer with unknown "
2682 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_tso_bad_ethtype",
2683 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
2684 "Aborted transmit of TSO buffer with unknown Ethernet type");
2685 SYSCTL_ADD_ULONG(&sc->vtnet_sysctl_ctx, SYSCTL_CHILDREN(sc->vtnet_sysctl_tree), OID_AUTO, "tx_task_rescheduled",
2686 CTLFLAG_RD, &stats->tx_task_rescheduled,
2687 "Times the transmit interrupt task rescheduled itself");
2691 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2694 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2698 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2701 virtqueue_disable_intr(sc->vtnet_rx_vq);
2705 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2708 #ifdef VTNET_TX_INTR_MODERATION
2711 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2716 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2719 virtqueue_disable_intr(sc->vtnet_tx_vq);