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>
66 #include <dev/virtual/virtio/virtio/virtio.h>
67 #include <dev/virtual/virtio/virtio/virtqueue.h>
69 #include "virtio_net.h"
70 #include "virtio_if.h"
72 struct vtnet_statistics {
73 unsigned long mbuf_alloc_failed;
75 unsigned long rx_frame_too_large;
76 unsigned long rx_enq_replacement_failed;
77 unsigned long rx_mergeable_failed;
78 unsigned long rx_csum_bad_ethtype;
79 unsigned long rx_csum_bad_start;
80 unsigned long rx_csum_bad_ipproto;
81 unsigned long rx_csum_bad_offset;
82 unsigned long rx_csum_failed;
83 unsigned long rx_csum_offloaded;
84 unsigned long rx_task_rescheduled;
86 unsigned long tx_csum_offloaded;
87 unsigned long tx_tso_offloaded;
88 unsigned long tx_csum_bad_ethtype;
89 unsigned long tx_tso_bad_ethtype;
90 unsigned long tx_task_rescheduled;
95 struct ifnet *vtnet_ifp;
96 struct lwkt_serialize vtnet_slz;
99 #define VTNET_FLAG_LINK 0x0001
100 #define VTNET_FLAG_SUSPENDED 0x0002
101 #define VTNET_FLAG_MAC 0x0004
102 #define VTNET_FLAG_CTRL_VQ 0x0008
103 #define VTNET_FLAG_CTRL_RX 0x0010
104 #define VTNET_FLAG_CTRL_MAC 0x0020
105 #define VTNET_FLAG_VLAN_FILTER 0x0040
106 #define VTNET_FLAG_TSO_ECN 0x0080
107 #define VTNET_FLAG_MRG_RXBUFS 0x0100
108 #define VTNET_FLAG_LRO_NOMRG 0x0200
110 struct virtqueue *vtnet_rx_vq;
111 struct virtqueue *vtnet_tx_vq;
112 struct virtqueue *vtnet_ctrl_vq;
114 struct vtnet_tx_header *vtnet_txhdrarea;
115 uint32_t vtnet_txhdridx;
116 struct vtnet_mac_filter *vtnet_macfilter;
121 int vtnet_rx_process_limit;
122 int vtnet_rx_mbuf_size;
123 int vtnet_rx_mbuf_count;
125 int vtnet_watchdog_timer;
126 uint64_t vtnet_features;
128 struct task vtnet_cfgchg_task;
130 struct vtnet_statistics vtnet_stats;
132 struct callout vtnet_tick_ch;
134 eventhandler_tag vtnet_vlan_attach;
135 eventhandler_tag vtnet_vlan_detach;
137 struct ifmedia vtnet_media;
139 * Fake media type; the host does not provide us with
140 * any real media information.
142 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX)
143 char vtnet_hwaddr[ETHER_ADDR_LEN];
146 * During reset, the host's VLAN filtering table is lost. The
147 * array below is used to restore all the VLANs configured on
148 * this interface after a reset.
150 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32)
152 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
154 char vtnet_mtx_name[16];
158 * When mergeable buffers are not negotiated, the vtnet_rx_header structure
159 * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
160 * both keep the VirtIO header and the data non-contiguous and to keep the
161 * frame's payload 4 byte aligned.
163 * When mergeable buffers are negotiated, the host puts the VirtIO header in
164 * the beginning of the first mbuf's data.
166 #define VTNET_RX_HEADER_PAD 4
167 struct vtnet_rx_header {
168 struct virtio_net_hdr vrh_hdr;
169 char vrh_pad[VTNET_RX_HEADER_PAD];
173 * For each outgoing frame, the vtnet_tx_header below is allocated from
174 * the vtnet_tx_header_zone.
176 struct vtnet_tx_header {
178 struct virtio_net_hdr hdr;
179 struct virtio_net_hdr_mrg_rxbuf mhdr;
182 struct mbuf *vth_mbuf;
185 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
188 * The VirtIO specification does not place a limit on the number of MAC
189 * addresses the guest driver may request to be filtered. In practice,
190 * the host is constrained by available resources. To simplify this driver,
191 * impose a reasonably high limit of MAC addresses we will filter before
192 * falling back to promiscuous or all-multicast modes.
194 #define VTNET_MAX_MAC_ENTRIES 128
196 struct vtnet_mac_table {
198 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
201 struct vtnet_mac_filter {
202 struct vtnet_mac_table vmf_unicast;
203 uint32_t vmf_pad; /* Make tables non-contiguous. */
204 struct vtnet_mac_table vmf_multicast;
207 #define VTNET_WATCHDOG_TIMEOUT 5
208 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP)
210 /* Features desired/implemented by this driver. */
211 #define VTNET_FEATURES \
212 (VIRTIO_NET_F_MAC | \
213 VIRTIO_NET_F_STATUS | \
214 VIRTIO_NET_F_CTRL_VQ | \
215 VIRTIO_NET_F_CTRL_RX | \
216 VIRTIO_NET_F_CTRL_MAC_ADDR | \
217 VIRTIO_NET_F_CTRL_VLAN | \
218 VIRTIO_NET_F_CSUM | \
219 VIRTIO_NET_F_HOST_TSO4 | \
220 VIRTIO_NET_F_HOST_TSO6 | \
221 VIRTIO_NET_F_HOST_ECN | \
222 VIRTIO_NET_F_GUEST_CSUM | \
223 VIRTIO_NET_F_GUEST_TSO4 | \
224 VIRTIO_NET_F_GUEST_TSO6 | \
225 VIRTIO_NET_F_GUEST_ECN | \
226 VIRTIO_NET_F_MRG_RXBUF)
229 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
230 * frames larger than 1514 bytes. We do not yet support software LRO
233 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
234 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
236 #define VTNET_MAX_MTU 65536
237 #define VTNET_MAX_RX_SIZE 65550
240 * Used to preallocate the Vq indirect descriptors. The first segment
241 * is reserved for the header.
243 #define VTNET_MIN_RX_SEGS 2
244 #define VTNET_MAX_RX_SEGS 34
245 #define VTNET_MAX_TX_SEGS 34
247 #define IFCAP_TSO4 0x00100 /* can do TCP Segmentation Offload */
248 #define IFCAP_TSO6 0x00200 /* can do TCP6 Segmentation Offload */
249 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */
250 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */
251 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */
255 * Assert we can receive and transmit the maximum with regular
258 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
259 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
262 * Determine how many mbufs are in each receive buffer. For LRO without
263 * mergeable descriptors, we must allocate an mbuf chain large enough to
264 * hold both the vtnet_rx_header and the maximum receivable data.
266 #define VTNET_NEEDED_RX_MBUFS(_sc) \
267 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \
268 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \
269 (_sc)->vtnet_rx_mbuf_size)
271 static int vtnet_modevent(module_t, int, void *);
273 static int vtnet_probe(device_t);
274 static int vtnet_attach(device_t);
275 static int vtnet_detach(device_t);
276 static int vtnet_suspend(device_t);
277 static int vtnet_resume(device_t);
278 static int vtnet_shutdown(device_t);
279 static int vtnet_config_change(device_t);
281 static void vtnet_negotiate_features(struct vtnet_softc *);
282 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
283 static void vtnet_get_hwaddr(struct vtnet_softc *);
284 static void vtnet_set_hwaddr(struct vtnet_softc *);
285 static int vtnet_is_link_up(struct vtnet_softc *);
286 static void vtnet_update_link_status(struct vtnet_softc *);
288 static void vtnet_watchdog(struct vtnet_softc *);
290 static void vtnet_config_change_task(void *, int);
291 static int vtnet_change_mtu(struct vtnet_softc *, int);
292 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
294 static int vtnet_init_rx_vq(struct vtnet_softc *);
295 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
296 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
297 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
299 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
301 static int vtnet_replace_rxbuf(struct vtnet_softc *,
303 static int vtnet_newbuf(struct vtnet_softc *);
304 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
305 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
306 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
307 static void vtnet_vlan_tag_remove(struct mbuf *);
308 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
309 struct virtio_net_hdr *);
310 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
311 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
312 static void vtnet_rx_intr_task(void *);
313 static int vtnet_rx_vq_intr(void *);
315 static void vtnet_txeof(struct vtnet_softc *);
316 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
317 struct virtio_net_hdr *);
318 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
319 struct vtnet_tx_header *);
320 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
321 static void vtnet_start_locked(struct ifnet *, struct ifaltq_subque *);
322 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
323 static void vtnet_tick(void *);
324 static void vtnet_tx_intr_task(void *);
325 static int vtnet_tx_vq_intr(void *);
327 static void vtnet_stop(struct vtnet_softc *);
328 static int vtnet_reinit(struct vtnet_softc *);
329 static void vtnet_init_locked(struct vtnet_softc *);
330 static void vtnet_init(void *);
332 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
333 struct sglist *, int, int);
335 static int vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
336 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
337 static int vtnet_set_promisc(struct vtnet_softc *, int);
338 static int vtnet_set_allmulti(struct vtnet_softc *, int);
339 static void vtnet_rx_filter(struct vtnet_softc *sc);
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_GUEST_ANNOUNCE, "GuestAnnounce" },
396 { VIRTIO_NET_F_MQ, "RFS" },
397 { VIRTIO_NET_F_CTRL_MAC_ADDR, "SetMacAddress" },
401 static device_method_t vtnet_methods[] = {
402 /* Device methods. */
403 DEVMETHOD(device_probe, vtnet_probe),
404 DEVMETHOD(device_attach, vtnet_attach),
405 DEVMETHOD(device_detach, vtnet_detach),
406 DEVMETHOD(device_suspend, vtnet_suspend),
407 DEVMETHOD(device_resume, vtnet_resume),
408 DEVMETHOD(device_shutdown, vtnet_shutdown),
410 /* VirtIO methods. */
411 DEVMETHOD(virtio_config_change, vtnet_config_change),
416 static driver_t vtnet_driver = {
419 sizeof(struct vtnet_softc)
422 static devclass_t vtnet_devclass;
424 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
426 MODULE_VERSION(vtnet, 1);
427 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
430 vtnet_modevent(module_t mod, int type, void *unused)
452 vtnet_probe(device_t dev)
454 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
457 device_set_desc(dev, "VirtIO Networking Adapter");
459 return (BUS_PROBE_DEFAULT);
463 vtnet_attach(device_t dev)
465 struct vtnet_softc *sc;
469 sc = device_get_softc(dev);
472 lwkt_serialize_init(&sc->vtnet_slz);
473 callout_init(&sc->vtnet_tick_ch);
475 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
477 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
478 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
480 vtnet_add_statistics(sc);
482 /* Register our feature descriptions. */
483 virtio_set_feature_desc(dev, vtnet_feature_desc);
484 vtnet_negotiate_features(sc);
486 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
487 /* This feature should always be negotiated. */
488 sc->vtnet_flags |= VTNET_FLAG_MAC;
491 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
492 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
493 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
495 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
498 sc->vtnet_rx_mbuf_size = MCLBYTES;
499 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
501 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
502 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
504 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
505 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
506 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
507 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
508 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
509 virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
510 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
513 /* Read (or generate) the MAC address for the adapter. */
514 vtnet_get_hwaddr(sc);
516 error = vtnet_alloc_virtqueues(sc);
518 device_printf(dev, "cannot allocate virtqueues\n");
522 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
524 device_printf(dev, "cannot allocate ifnet structure\n");
530 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
531 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
532 ifp->if_init = vtnet_init;
533 ifp->if_start = vtnet_start;
534 ifp->if_ioctl = vtnet_ioctl;
536 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
537 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
539 tx_size = virtqueue_size(sc->vtnet_tx_vq);
540 sc->vtnet_tx_size = tx_size;
541 sc->vtnet_txhdridx = 0;
542 sc->vtnet_txhdrarea = contigmalloc(
543 ((sc->vtnet_tx_size / 2) + 1) * sizeof(struct vtnet_tx_header),
544 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
545 if (sc->vtnet_txhdrarea == NULL) {
546 device_printf(dev, "cannot contigmalloc the tx headers\n");
549 sc->vtnet_macfilter = contigmalloc(
550 sizeof(struct vtnet_mac_filter),
551 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
552 if (sc->vtnet_macfilter == NULL) {
554 "cannot contigmalloc the mac filter table\n");
557 ifq_set_maxlen(&ifp->if_snd, tx_size - 1);
558 ifq_set_ready(&ifp->if_snd);
560 ether_ifattach(ifp, sc->vtnet_hwaddr, NULL);
562 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)){
563 //ifp->if_capabilities |= IFCAP_LINKSTATE;
564 kprintf("add dynamic link state\n");
567 /* Tell the upper layer(s) we support long frames. */
568 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
569 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
571 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
572 ifp->if_capabilities |= IFCAP_TXCSUM;
574 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
575 ifp->if_capabilities |= IFCAP_TSO4;
576 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
577 ifp->if_capabilities |= IFCAP_TSO6;
578 if (ifp->if_capabilities & IFCAP_TSO)
579 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
581 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
582 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
585 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
586 ifp->if_capabilities |= IFCAP_RXCSUM;
588 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
589 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
590 ifp->if_capabilities |= IFCAP_LRO;
593 if (ifp->if_capabilities & IFCAP_HWCSUM) {
595 * VirtIO does not support VLAN tagging, but we can fake
596 * it by inserting and removing the 802.1Q header during
597 * transmit and receive. We are then able to do checksum
598 * offloading of VLAN frames.
600 ifp->if_capabilities |=
601 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
604 ifp->if_capenable = ifp->if_capabilities;
607 * Capabilities after here are not enabled by default.
610 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
611 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
613 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
614 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
615 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
616 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
619 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc);
621 error = virtio_setup_intr(dev, &sc->vtnet_slz);
623 device_printf(dev, "cannot setup virtqueue interrupts\n");
629 * Device defaults to promiscuous mode for backwards
630 * compatibility. Turn it off if possible.
632 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
633 lwkt_serialize_enter(&sc->vtnet_slz);
634 if (vtnet_set_promisc(sc, 0) != 0) {
635 ifp->if_flags |= IFF_PROMISC;
637 "cannot disable promiscuous mode\n");
639 lwkt_serialize_exit(&sc->vtnet_slz);
641 ifp->if_flags |= IFF_PROMISC;
651 vtnet_detach(device_t dev)
653 struct vtnet_softc *sc;
656 sc = device_get_softc(dev);
659 if (device_is_attached(dev)) {
660 lwkt_serialize_enter(&sc->vtnet_slz);
662 lwkt_serialize_exit(&sc->vtnet_slz);
664 callout_stop(&sc->vtnet_tick_ch);
665 taskqueue_drain(taskqueue_swi, &sc->vtnet_cfgchg_task);
670 if (sc->vtnet_vlan_attach != NULL) {
671 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
672 sc->vtnet_vlan_attach = NULL;
674 if (sc->vtnet_vlan_detach != NULL) {
675 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
676 sc->vtnet_vlan_detach = NULL;
681 sc->vtnet_ifp = NULL;
684 if (sc->vtnet_rx_vq != NULL)
685 vtnet_free_rx_mbufs(sc);
686 if (sc->vtnet_tx_vq != NULL)
687 vtnet_free_tx_mbufs(sc);
688 if (sc->vtnet_ctrl_vq != NULL)
689 vtnet_free_ctrl_vq(sc);
691 if (sc->vtnet_txhdrarea != NULL) {
692 contigfree(sc->vtnet_txhdrarea,
693 ((sc->vtnet_tx_size / 2) + 1) *
694 sizeof(struct vtnet_tx_header), M_VTNET);
695 sc->vtnet_txhdrarea = NULL;
697 if (sc->vtnet_macfilter != NULL) {
698 contigfree(sc->vtnet_macfilter,
699 sizeof(struct vtnet_mac_filter), M_DEVBUF);
700 sc->vtnet_macfilter = NULL;
703 ifmedia_removeall(&sc->vtnet_media);
709 vtnet_suspend(device_t dev)
711 struct vtnet_softc *sc;
713 sc = device_get_softc(dev);
715 lwkt_serialize_enter(&sc->vtnet_slz);
717 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
718 lwkt_serialize_exit(&sc->vtnet_slz);
724 vtnet_resume(device_t dev)
726 struct vtnet_softc *sc;
729 sc = device_get_softc(dev);
732 lwkt_serialize_enter(&sc->vtnet_slz);
733 if (ifp->if_flags & IFF_UP)
734 vtnet_init_locked(sc);
735 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
736 lwkt_serialize_exit(&sc->vtnet_slz);
742 vtnet_shutdown(device_t dev)
746 * Suspend already does all of what we need to
747 * do here; we just never expect to be resumed.
749 return (vtnet_suspend(dev));
753 vtnet_config_change(device_t dev)
755 struct vtnet_softc *sc;
757 sc = device_get_softc(dev);
759 taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->vtnet_cfgchg_task);
765 vtnet_negotiate_features(struct vtnet_softc *sc)
768 uint64_t mask, features;
773 if (vtnet_csum_disable)
774 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
777 * TSO and LRO are only available when their corresponding checksum
778 * offload feature is also negotiated.
781 if (vtnet_csum_disable || vtnet_tso_disable)
782 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
783 VIRTIO_NET_F_HOST_ECN;
785 if (vtnet_csum_disable || vtnet_lro_disable)
786 mask |= VTNET_LRO_FEATURES;
788 features = VTNET_FEATURES & ~mask;
789 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
790 sc->vtnet_features = virtio_negotiate_features(dev, features);
792 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
793 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
795 * LRO without mergeable buffers requires special care. This
796 * is not ideal because every receive buffer must be large
797 * enough to hold the maximum TCP packet, the Ethernet header,
798 * and the header. This requires up to 34 descriptors with
799 * MCLBYTES clusters. If we do not have indirect descriptors,
800 * LRO is disabled since the virtqueue will not contain very
801 * many receive buffers.
803 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
805 "LRO disabled due to both mergeable buffers and "
806 "indirect descriptors not negotiated\n");
808 features &= ~VTNET_LRO_FEATURES;
810 virtio_negotiate_features(dev, features);
812 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
817 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
820 struct vq_alloc_info vq_info[3];
827 * Indirect descriptors are not needed for the Rx
828 * virtqueue when mergeable buffers are negotiated.
829 * The header is placed inline with the data, not
830 * in a separate descriptor, and mbuf clusters are
831 * always physically contiguous.
833 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
834 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
835 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
839 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
840 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
841 "%s receive", device_get_nameunit(dev));
843 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
844 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
845 "%s transmit", device_get_nameunit(dev));
847 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
850 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
851 &sc->vtnet_ctrl_vq, "%s control",
852 device_get_nameunit(dev));
855 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
859 vtnet_set_hwaddr(struct vtnet_softc *sc)
865 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) &&
866 (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)) {
867 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
868 device_printf(dev, "unable to set MAC address\n");
869 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
870 virtio_write_device_config(dev,
871 offsetof(struct virtio_net_config, mac),
872 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
877 vtnet_get_hwaddr(struct vtnet_softc *sc)
883 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
885 * Generate a random locally administered unicast address.
887 * It would be nice to generate the same MAC address across
888 * reboots, but it seems all the hosts currently available
889 * support the MAC feature, so this isn't too important.
891 sc->vtnet_hwaddr[0] = 0xB2;
892 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
893 vtnet_set_hwaddr(sc);
897 virtio_read_device_config(dev,
898 offsetof(struct virtio_net_config, mac),
899 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
903 vtnet_is_link_up(struct vtnet_softc *sc)
912 ASSERT_SERIALIZED(&sc->vtnet_slz);
914 status = virtio_read_dev_config_2(dev,
915 offsetof(struct virtio_net_config, status));
917 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
921 vtnet_update_link_status(struct vtnet_softc *sc)
925 struct ifaltq_subque *ifsq;
930 ifsq = ifq_get_subq_default(&ifp->if_snd);
932 link = vtnet_is_link_up(sc);
934 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
935 sc->vtnet_flags |= VTNET_FLAG_LINK;
937 device_printf(dev, "Link is up\n");
938 ifp->if_link_state = LINK_STATE_UP;
939 if_link_state_change(ifp);
940 if (!ifsq_is_empty(ifsq))
941 vtnet_start_locked(ifp, ifsq);
942 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
943 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
945 device_printf(dev, "Link is down\n");
947 ifp->if_link_state = LINK_STATE_DOWN;
948 if_link_state_change(ifp);
954 vtnet_watchdog(struct vtnet_softc *sc)
960 #ifdef VTNET_TX_INTR_MODERATION
964 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
967 if_printf(ifp, "watchdog timeout -- resetting\n");
969 virtqueue_dump(sc->vtnet_tx_vq);
972 ifp->if_flags &= ~IFF_RUNNING;
973 vtnet_init_locked(sc);
978 vtnet_config_change_task(void *arg, int pending)
980 struct vtnet_softc *sc;
984 lwkt_serialize_enter(&sc->vtnet_slz);
985 vtnet_update_link_status(sc);
986 lwkt_serialize_exit(&sc->vtnet_slz);
990 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
992 struct vtnet_softc *sc;
994 int reinit, mask, error;
997 ifr = (struct ifreq *) data;
1003 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
1005 else if (ifp->if_mtu != ifr->ifr_mtu) {
1006 lwkt_serialize_enter(&sc->vtnet_slz);
1007 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
1008 lwkt_serialize_exit(&sc->vtnet_slz);
1013 lwkt_serialize_enter(&sc->vtnet_slz);
1014 if ((ifp->if_flags & IFF_UP) == 0) {
1015 if (ifp->if_flags & IFF_RUNNING)
1017 } else if (ifp->if_flags & IFF_RUNNING) {
1018 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
1019 (IFF_PROMISC | IFF_ALLMULTI)) {
1020 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1021 vtnet_rx_filter(sc);
1026 vtnet_init_locked(sc);
1029 sc->vtnet_if_flags = ifp->if_flags;
1030 lwkt_serialize_exit(&sc->vtnet_slz);
1035 lwkt_serialize_enter(&sc->vtnet_slz);
1036 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
1037 (ifp->if_flags & IFF_RUNNING))
1038 vtnet_rx_filter_mac(sc);
1039 lwkt_serialize_exit(&sc->vtnet_slz);
1044 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1048 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1050 lwkt_serialize_enter(&sc->vtnet_slz);
1052 if (mask & IFCAP_TXCSUM) {
1053 ifp->if_capenable ^= IFCAP_TXCSUM;
1054 if (ifp->if_capenable & IFCAP_TXCSUM)
1055 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1057 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1060 if (mask & IFCAP_TSO4) {
1061 ifp->if_capenable ^= IFCAP_TSO4;
1062 if (ifp->if_capenable & IFCAP_TSO4)
1063 ifp->if_hwassist |= CSUM_TSO;
1065 ifp->if_hwassist &= ~CSUM_TSO;
1068 if (mask & IFCAP_RXCSUM) {
1069 ifp->if_capenable ^= IFCAP_RXCSUM;
1073 if (mask & IFCAP_LRO) {
1074 ifp->if_capenable ^= IFCAP_LRO;
1078 if (mask & IFCAP_VLAN_HWFILTER) {
1079 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1083 if (mask & IFCAP_VLAN_HWTSO)
1084 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1086 if (mask & IFCAP_VLAN_HWTAGGING)
1087 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1089 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1090 ifp->if_flags &= ~IFF_RUNNING;
1091 vtnet_init_locked(sc);
1093 //VLAN_CAPABILITIES(ifp);
1095 lwkt_serialize_exit(&sc->vtnet_slz);
1099 error = ether_ioctl(ifp, cmd, data);
1107 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1110 int new_frame_size, clsize;
1112 ifp = sc->vtnet_ifp;
1114 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1115 new_frame_size = sizeof(struct vtnet_rx_header) +
1116 sizeof(struct ether_vlan_header) + new_mtu;
1118 if (new_frame_size > MJUM9BYTES)
1121 if (new_frame_size <= MCLBYTES)
1124 clsize = MJUM9BYTES;
1126 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1127 sizeof(struct ether_vlan_header) + new_mtu;
1129 if (new_frame_size <= MCLBYTES)
1132 clsize = MJUMPAGESIZE;
1135 sc->vtnet_rx_mbuf_size = clsize;
1136 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1137 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1138 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1140 ifp->if_mtu = new_mtu;
1142 if (ifp->if_flags & IFF_RUNNING) {
1143 ifp->if_flags &= ~IFF_RUNNING;
1144 vtnet_init_locked(sc);
1151 vtnet_init_rx_vq(struct vtnet_softc *sc)
1153 struct virtqueue *vq;
1156 vq = sc->vtnet_rx_vq;
1160 while (!virtqueue_full(vq)) {
1161 if ((error = vtnet_newbuf(sc)) != 0)
1167 virtqueue_notify(vq, &sc->vtnet_slz);
1170 * EMSGSIZE signifies the virtqueue did not have enough
1171 * entries available to hold the last mbuf. This is not
1172 * an error. We should not get ENOSPC since we check if
1173 * the virtqueue is full before attempting to add a
1176 if (error == EMSGSIZE)
1184 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1186 struct virtqueue *vq;
1190 vq = sc->vtnet_rx_vq;
1193 while ((m = virtqueue_drain(vq, &last)) != NULL)
1196 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1200 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1202 struct virtqueue *vq;
1203 struct vtnet_tx_header *txhdr;
1206 vq = sc->vtnet_tx_vq;
1209 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1210 m_freem(txhdr->vth_mbuf);
1213 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1217 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1220 * The control virtqueue is only polled, therefore
1221 * it should already be empty.
1223 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1224 ("Ctrl Vq not empty"));
1227 static struct mbuf *
1228 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1230 struct mbuf *m_head, *m_tail, *m;
1233 clsize = sc->vtnet_rx_mbuf_size;
1235 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1236 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1237 m_head = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR );
1241 m_head->m_len = clsize;
1245 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1246 ("chained Rx mbuf requested without LRO_NOMRG"));
1248 for (i = 0; i < nbufs - 1; i++) {
1249 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1250 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1260 if (m_tailp != NULL)
1266 sc->vtnet_stats.mbuf_alloc_failed++;
1273 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1275 struct mbuf *m, *m_prev;
1276 struct mbuf *m_new, *m_tail;
1277 int len, clsize, nreplace, error;
1284 clsize = sc->vtnet_rx_mbuf_size;
1287 if (m->m_next != NULL)
1288 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1289 ("chained Rx mbuf without LRO_NOMRG"));
1292 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1293 * allocating an entire chain for each received frame. When
1294 * the received frame's length is less than that of the chain,
1295 * the unused mbufs are reassigned to the new chain.
1299 * Something is seriously wrong if we received
1300 * a frame larger than the mbuf chain. Drop it.
1303 sc->vtnet_stats.rx_frame_too_large++;
1307 KASSERT(m->m_len == clsize,
1308 ("mbuf length not expected cluster size: %d",
1311 m->m_len = MIN(m->m_len, len);
1319 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1320 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1321 ("too many replacement mbufs: %d/%d", nreplace,
1322 sc->vtnet_rx_mbuf_count));
1324 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1325 if (m_new == NULL) {
1326 m_prev->m_len = clsize;
1331 * Move unused mbufs, if any, from the original chain
1332 * onto the end of the new chain.
1334 if (m_prev->m_next != NULL) {
1335 m_tail->m_next = m_prev->m_next;
1336 m_prev->m_next = NULL;
1339 error = vtnet_enqueue_rxbuf(sc, m_new);
1342 * BAD! We could not enqueue the replacement mbuf chain. We
1343 * must restore the m0 chain to the original state if it was
1344 * modified so we can subsequently discard it.
1346 * NOTE: The replacement is suppose to be an identical copy
1347 * to the one just dequeued so this is an unexpected error.
1349 sc->vtnet_stats.rx_enq_replacement_failed++;
1351 if (m_tail->m_next != NULL) {
1352 m_prev->m_next = m_tail->m_next;
1353 m_tail->m_next = NULL;
1356 m_prev->m_len = clsize;
1364 vtnet_newbuf(struct vtnet_softc *sc)
1369 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1373 error = vtnet_enqueue_rxbuf(sc, m);
1381 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1383 struct virtqueue *vq;
1386 vq = sc->vtnet_rx_vq;
1388 while (--nbufs > 0) {
1389 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1391 vtnet_discard_rxbuf(sc, m);
1396 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1401 * Requeue the discarded mbuf. This should always be
1402 * successful since it was just dequeued.
1404 error = vtnet_enqueue_rxbuf(sc, m);
1405 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1409 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1412 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1413 struct vtnet_rx_header *rxhdr;
1414 struct virtio_net_hdr *hdr;
1418 ASSERT_SERIALIZED(&sc->vtnet_slz);
1419 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1420 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1422 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1424 mdata = mtod(m, uint8_t *);
1427 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1428 rxhdr = (struct vtnet_rx_header *) mdata;
1429 hdr = &rxhdr->vrh_hdr;
1430 offset += sizeof(struct vtnet_rx_header);
1432 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1433 KASSERT(error == 0, ("cannot add header to sglist"));
1436 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1440 if (m->m_next != NULL) {
1441 error = sglist_append_mbuf(&sg, m->m_next);
1446 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1450 vtnet_vlan_tag_remove(struct mbuf *m)
1452 struct ether_vlan_header *evl;
1454 evl = mtod(m, struct ether_vlan_header *);
1456 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1457 m->m_flags |= M_VLANTAG;
1459 /* Strip the 802.1Q header. */
1460 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1461 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1462 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1466 * Alternative method of doing receive checksum offloading. Rather
1467 * than parsing the received frame down to the IP header, use the
1468 * csum_offset to determine which CSUM_* flags are appropriate. We
1469 * can get by with doing this only because the checksum offsets are
1470 * unique for the things we care about.
1473 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1474 struct virtio_net_hdr *hdr)
1476 struct ether_header *eh;
1477 struct ether_vlan_header *evh;
1482 csum_len = hdr->csum_start + hdr->csum_offset;
1484 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1486 if (m->m_len < csum_len)
1489 eh = mtod(m, struct ether_header *);
1490 eth_type = ntohs(eh->ether_type);
1491 if (eth_type == ETHERTYPE_VLAN) {
1492 evh = mtod(m, struct ether_vlan_header *);
1493 eth_type = ntohs(evh->evl_proto);
1496 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1497 sc->vtnet_stats.rx_csum_bad_ethtype++;
1501 /* Use the offset to determine the appropriate CSUM_* flags. */
1502 switch (hdr->csum_offset) {
1503 case offsetof(struct udphdr, uh_sum):
1504 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1506 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1507 if (udp->uh_sum == 0)
1512 case offsetof(struct tcphdr, th_sum):
1513 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1514 m->m_pkthdr.csum_data = 0xFFFF;
1518 sc->vtnet_stats.rx_csum_bad_offset++;
1522 sc->vtnet_stats.rx_csum_offloaded++;
1528 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1531 struct virtqueue *vq;
1532 struct mbuf *m, *m_tail;
1535 ifp = sc->vtnet_ifp;
1536 vq = sc->vtnet_rx_vq;
1539 while (--nbufs > 0) {
1540 m = virtqueue_dequeue(vq, &len);
1546 if (vtnet_newbuf(sc) != 0) {
1548 vtnet_discard_rxbuf(sc, m);
1550 vtnet_discard_merged_rxbuf(sc, nbufs);
1558 m->m_flags &= ~M_PKTHDR;
1560 m_head->m_pkthdr.len += len;
1568 sc->vtnet_stats.rx_mergeable_failed++;
1575 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1577 struct virtio_net_hdr lhdr;
1579 struct virtqueue *vq;
1581 struct ether_header *eh;
1582 struct virtio_net_hdr *hdr;
1583 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1584 int len, deq, nbufs, adjsz, rx_npkts;
1586 ifp = sc->vtnet_ifp;
1587 vq = sc->vtnet_rx_vq;
1592 ASSERT_SERIALIZED(&sc->vtnet_slz);
1594 while (--count >= 0) {
1595 m = virtqueue_dequeue(vq, &len);
1600 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1602 vtnet_discard_rxbuf(sc, m);
1606 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1608 adjsz = sizeof(struct vtnet_rx_header);
1610 * Account for our pad between the header and
1611 * the actual start of the frame.
1613 len += VTNET_RX_HEADER_PAD;
1615 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1616 nbufs = mhdr->num_buffers;
1617 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1620 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1622 vtnet_discard_rxbuf(sc, m);
1624 vtnet_discard_merged_rxbuf(sc, nbufs);
1628 m->m_pkthdr.len = len;
1629 m->m_pkthdr.rcvif = ifp;
1630 m->m_pkthdr.csum_flags = 0;
1633 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1640 * Save copy of header before we strip it. For both mergeable
1641 * and non-mergeable, the VirtIO header is placed first in the
1642 * mbuf's data. We no longer need num_buffers, so always use a
1645 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1648 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1649 eh = mtod(m, struct ether_header *);
1650 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1651 vtnet_vlan_tag_remove(m);
1654 * With the 802.1Q header removed, update the
1655 * checksum starting location accordingly.
1657 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1659 ETHER_VLAN_ENCAP_LEN;
1663 if (ifp->if_capenable & IFCAP_RXCSUM &&
1664 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1665 if (vtnet_rx_csum(sc, m, hdr) != 0)
1666 sc->vtnet_stats.rx_csum_failed++;
1669 lwkt_serialize_exit(&sc->vtnet_slz);
1671 ifp->if_input(ifp, m, NULL, -1);
1672 lwkt_serialize_enter(&sc->vtnet_slz);
1675 * The interface may have been stopped while we were
1676 * passing the packet up the network stack.
1678 if ((ifp->if_flags & IFF_RUNNING) == 0)
1682 virtqueue_notify(vq, &sc->vtnet_slz);
1684 if (rx_npktsp != NULL)
1685 *rx_npktsp = rx_npkts;
1687 return (count > 0 ? 0 : EAGAIN);
1691 vtnet_rx_intr_task(void *arg)
1693 struct vtnet_softc *sc;
1698 ifp = sc->vtnet_ifp;
1701 // lwkt_serialize_enter(&sc->vtnet_slz);
1703 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1704 vtnet_enable_rx_intr(sc);
1705 // lwkt_serialize_exit(&sc->vtnet_slz);
1709 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1710 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1711 vtnet_disable_rx_intr(sc);
1715 // lwkt_serialize_exit(&sc->vtnet_slz);
1718 sc->vtnet_stats.rx_task_rescheduled++;
1724 vtnet_rx_vq_intr(void *xsc)
1726 struct vtnet_softc *sc;
1730 vtnet_disable_rx_intr(sc);
1731 vtnet_rx_intr_task(sc);
1737 vtnet_txeof(struct vtnet_softc *sc)
1739 struct virtqueue *vq;
1741 struct vtnet_tx_header *txhdr;
1744 vq = sc->vtnet_tx_vq;
1745 ifp = sc->vtnet_ifp;
1748 ASSERT_SERIALIZED(&sc->vtnet_slz);
1750 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1753 m_freem(txhdr->vth_mbuf);
1757 ifq_clr_oactive(&ifp->if_snd);
1758 if (virtqueue_empty(vq))
1759 sc->vtnet_watchdog_timer = 0;
1763 static struct mbuf *
1764 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1765 struct virtio_net_hdr *hdr)
1768 struct ether_header *eh;
1769 struct ether_vlan_header *evh;
1771 struct ip6_hdr *ip6;
1774 uint16_t eth_type, csum_start;
1775 uint8_t ip_proto, gso_type;
1777 ifp = sc->vtnet_ifp;
1780 ip_offset = sizeof(struct ether_header);
1781 if (m->m_len < ip_offset) {
1782 if ((m = m_pullup(m, ip_offset)) == NULL)
1786 eh = mtod(m, struct ether_header *);
1787 eth_type = ntohs(eh->ether_type);
1788 if (eth_type == ETHERTYPE_VLAN) {
1789 ip_offset = sizeof(struct ether_vlan_header);
1790 if (m->m_len < ip_offset) {
1791 if ((m = m_pullup(m, ip_offset)) == NULL)
1794 evh = mtod(m, struct ether_vlan_header *);
1795 eth_type = ntohs(evh->evl_proto);
1800 if (m->m_len < ip_offset + sizeof(struct ip)) {
1801 m = m_pullup(m, ip_offset + sizeof(struct ip));
1806 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1807 ip_proto = ip->ip_p;
1808 csum_start = ip_offset + (ip->ip_hl << 2);
1809 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1812 case ETHERTYPE_IPV6:
1813 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1814 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1819 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1821 * XXX Assume no extension headers are present. Presently,
1822 * this will always be true in the case of TSO, and FreeBSD
1823 * does not perform checksum offloading of IPv6 yet.
1825 ip_proto = ip6->ip6_nxt;
1826 csum_start = ip_offset + sizeof(struct ip6_hdr);
1827 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1834 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1835 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1836 hdr->csum_start = csum_start;
1837 hdr->csum_offset = m->m_pkthdr.csum_data;
1839 sc->vtnet_stats.tx_csum_offloaded++;
1842 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1843 if (ip_proto != IPPROTO_TCP)
1846 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1847 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1852 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1853 hdr->gso_type = gso_type;
1854 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1855 hdr->gso_size = m->m_pkthdr.tso_segsz;
1857 if (tcp->th_flags & TH_CWR) {
1859 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1860 * ECN support is only configurable globally with the
1861 * net.inet.tcp.ecn.enable sysctl knob.
1863 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1864 if_printf(ifp, "TSO with ECN not supported "
1870 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1873 sc->vtnet_stats.tx_tso_offloaded++;
1880 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1881 struct vtnet_tx_header *txhdr)
1884 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1885 struct virtqueue *vq;
1887 int collapsed, error;
1889 vq = sc->vtnet_tx_vq;
1893 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1894 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1895 KASSERT(error == 0 && sg.sg_nseg == 1,
1896 ("cannot add header to sglist"));
1899 error = sglist_append_mbuf(&sg, m);
1904 //m = m_collapse(m, M_NOWAIT, VTNET_MAX_TX_SEGS - 1);
1905 m = m_defrag(m, M_NOWAIT);
1914 txhdr->vth_mbuf = m;
1916 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1925 static struct mbuf *
1926 vtnet_vlan_tag_insert(struct mbuf *m)
1929 struct ether_vlan_header *evl;
1931 if (M_WRITABLE(m) == 0) {
1932 n = m_dup(m, M_NOWAIT);
1934 if ((m = n) == NULL)
1938 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1941 if (m->m_len < sizeof(struct ether_vlan_header)) {
1942 m = m_pullup(m, sizeof(struct ether_vlan_header));
1947 /* Insert 802.1Q header into the existing Ethernet header. */
1948 evl = mtod(m, struct ether_vlan_header *);
1949 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1950 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1951 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1952 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1953 m->m_flags &= ~M_VLANTAG;
1959 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1961 struct vtnet_tx_header *txhdr;
1962 struct virtio_net_hdr *hdr;
1966 txhdr = &sc->vtnet_txhdrarea[sc->vtnet_txhdridx];
1967 memset(txhdr, 0, sizeof(struct vtnet_tx_header));
1970 * Always use the non-mergeable header to simplify things. When
1971 * the mergeable feature is negotiated, the num_buffers field
1972 * must be set to zero. We use vtnet_hdr_size later to enqueue
1973 * the correct header size to the host.
1975 hdr = &txhdr->vth_uhdr.hdr;
1980 if (m->m_flags & M_VLANTAG) {
1981 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1982 m = vtnet_vlan_tag_insert(m);
1983 if ((*m_head = m) == NULL)
1985 m->m_flags &= ~M_VLANTAG;
1988 if (m->m_pkthdr.csum_flags != 0) {
1989 m = vtnet_tx_offload(sc, m, hdr);
1990 if ((*m_head = m) == NULL)
1994 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1996 sc->vtnet_txhdridx =
1997 (sc->vtnet_txhdridx + 1) % ((sc->vtnet_tx_size / 2) + 1);
2003 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2005 struct vtnet_softc *sc;
2009 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2010 lwkt_serialize_enter(&sc->vtnet_slz);
2011 vtnet_start_locked(ifp, ifsq);
2012 lwkt_serialize_exit(&sc->vtnet_slz);
2016 vtnet_start_locked(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2018 struct vtnet_softc *sc;
2019 struct virtqueue *vq;
2024 vq = sc->vtnet_tx_vq;
2027 ASSERT_SERIALIZED(&sc->vtnet_slz);
2029 if ((ifp->if_flags & (IFF_RUNNING)) !=
2030 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
2033 #ifdef VTNET_TX_INTR_MODERATION
2034 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
2038 while (!ifsq_is_empty(ifsq)) {
2039 if (virtqueue_full(vq)) {
2040 ifq_set_oactive(&ifp->if_snd);
2044 m0 = ifq_dequeue(&ifp->if_snd);
2048 if (vtnet_encap(sc, &m0) != 0) {
2051 ifq_prepend(&ifp->if_snd, m0);
2052 ifq_set_oactive(&ifp->if_snd);
2057 ETHER_BPF_MTAP(ifp, m0);
2061 virtqueue_notify(vq, &sc->vtnet_slz);
2062 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2067 vtnet_tick(void *xsc)
2069 struct vtnet_softc *sc;
2074 ASSERT_SERIALIZED(&sc->vtnet_slz);
2076 virtqueue_dump(sc->vtnet_rx_vq);
2077 virtqueue_dump(sc->vtnet_tx_vq);
2081 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2086 vtnet_tx_intr_task(void *arg)
2088 struct vtnet_softc *sc;
2090 struct ifaltq_subque *ifsq;
2093 ifp = sc->vtnet_ifp;
2094 ifsq = ifq_get_subq_default(&ifp->if_snd);
2097 // lwkt_serialize_enter(&sc->vtnet_slz);
2099 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2100 vtnet_enable_tx_intr(sc);
2101 // lwkt_serialize_exit(&sc->vtnet_slz);
2107 if (!ifsq_is_empty(ifsq))
2108 vtnet_start_locked(ifp, ifsq);
2110 if (vtnet_enable_tx_intr(sc) != 0) {
2111 vtnet_disable_tx_intr(sc);
2112 sc->vtnet_stats.tx_task_rescheduled++;
2113 // lwkt_serialize_exit(&sc->vtnet_slz);
2117 // lwkt_serialize_exit(&sc->vtnet_slz);
2121 vtnet_tx_vq_intr(void *xsc)
2123 struct vtnet_softc *sc;
2127 vtnet_disable_tx_intr(sc);
2128 vtnet_tx_intr_task(sc);
2134 vtnet_stop(struct vtnet_softc *sc)
2139 dev = sc->vtnet_dev;
2140 ifp = sc->vtnet_ifp;
2142 ASSERT_SERIALIZED(&sc->vtnet_slz);
2144 sc->vtnet_watchdog_timer = 0;
2145 callout_stop(&sc->vtnet_tick_ch);
2146 ifq_clr_oactive(&ifp->if_snd);
2147 ifp->if_flags &= ~(IFF_RUNNING);
2149 vtnet_disable_rx_intr(sc);
2150 vtnet_disable_tx_intr(sc);
2153 * Stop the host VirtIO adapter. Note this will reset the host
2154 * adapter's state back to the pre-initialized state, so in
2155 * order to make the device usable again, we must drive it
2156 * through virtio_reinit() and virtio_reinit_complete().
2160 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2162 vtnet_free_rx_mbufs(sc);
2163 vtnet_free_tx_mbufs(sc);
2167 vtnet_reinit(struct vtnet_softc *sc)
2172 ifp = sc->vtnet_ifp;
2173 features = sc->vtnet_features;
2176 * Re-negotiate with the host, removing any disabled receive
2177 * features. Transmit features are disabled only on our side
2178 * via if_capenable and if_hwassist.
2181 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2182 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2183 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2186 if (ifp->if_capabilities & IFCAP_LRO) {
2187 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2188 features &= ~VTNET_LRO_FEATURES;
2191 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2192 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2193 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2196 return (virtio_reinit(sc->vtnet_dev, features));
2200 vtnet_init_locked(struct vtnet_softc *sc)
2206 dev = sc->vtnet_dev;
2207 ifp = sc->vtnet_ifp;
2209 ASSERT_SERIALIZED(&sc->vtnet_slz);
2211 if (ifp->if_flags & IFF_RUNNING)
2214 /* Stop host's adapter, cancel any pending I/O. */
2217 /* Reinitialize the host device. */
2218 error = vtnet_reinit(sc);
2221 "reinitialization failed, stopping device...\n");
2226 /* Update host with assigned MAC address. */
2227 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2228 vtnet_set_hwaddr(sc);
2230 ifp->if_hwassist = 0;
2231 if (ifp->if_capenable & IFCAP_TXCSUM)
2232 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2233 if (ifp->if_capenable & IFCAP_TSO4)
2234 ifp->if_hwassist |= CSUM_TSO;
2236 error = vtnet_init_rx_vq(sc);
2239 "cannot allocate mbufs for Rx virtqueue\n");
2244 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2245 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2246 /* Restore promiscuous and all-multicast modes. */
2247 vtnet_rx_filter(sc);
2249 /* Restore filtered MAC addresses. */
2250 vtnet_rx_filter_mac(sc);
2253 /* Restore VLAN filters. */
2254 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2255 vtnet_rx_filter_vlan(sc);
2259 vtnet_enable_rx_intr(sc);
2260 vtnet_enable_tx_intr(sc);
2263 ifp->if_flags |= IFF_RUNNING;
2264 ifq_clr_oactive(&ifp->if_snd);
2266 virtio_reinit_complete(dev);
2268 vtnet_update_link_status(sc);
2269 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2273 vtnet_init(void *xsc)
2275 struct vtnet_softc *sc;
2279 lwkt_serialize_enter(&sc->vtnet_slz);
2280 vtnet_init_locked(sc);
2281 lwkt_serialize_exit(&sc->vtnet_slz);
2285 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2286 struct sglist *sg, int readable, int writable)
2288 struct virtqueue *vq;
2291 vq = sc->vtnet_ctrl_vq;
2293 ASSERT_SERIALIZED(&sc->vtnet_slz);
2294 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2295 ("no control virtqueue"));
2296 KASSERT(virtqueue_empty(vq),
2297 ("control command already enqueued"));
2299 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2302 virtqueue_notify(vq, &sc->vtnet_slz);
2305 * Poll until the command is complete. Previously, we would
2306 * sleep until the control virtqueue interrupt handler woke
2307 * us up, but dropping the VTNET_MTX leads to serialization
2310 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2311 * vectors. Two of those vectors are needed for the Rx and Tx
2312 * virtqueues. We do not support sharing both a Vq and config
2313 * changed notification on the same MSIX vector.
2315 c = virtqueue_poll(vq, NULL);
2316 KASSERT(c == cookie, ("unexpected control command response"));
2320 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
2323 struct virtio_net_ctrl_hdr hdr __aligned(2);
2325 char aligned_hwaddr[ETHER_ADDR_LEN] __aligned(8);
2329 struct sglist_seg segs[3];
2333 s.hdr.class = VIRTIO_NET_CTRL_MAC;
2334 s.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
2335 s.ack = VIRTIO_NET_ERR;
2337 /* Copy the mac address into physically contiguous memory */
2338 memcpy(s.aligned_hwaddr, hwaddr, ETHER_ADDR_LEN);
2340 sglist_init(&sg, 3, segs);
2342 error |= sglist_append(&sg, &s.hdr,
2343 sizeof(struct virtio_net_ctrl_hdr));
2344 error |= sglist_append(&sg, s.aligned_hwaddr, ETHER_ADDR_LEN);
2345 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2346 KASSERT(error == 0 && sg.sg_nseg == 3,
2347 ("%s: error %d adding set MAC msg to sglist", __func__, error));
2349 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2351 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2355 vtnet_rx_filter(struct vtnet_softc *sc)
2360 dev = sc->vtnet_dev;
2361 ifp = sc->vtnet_ifp;
2363 ASSERT_SERIALIZED(&sc->vtnet_slz);
2364 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2365 ("CTRL_RX feature not negotiated"));
2367 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2368 device_printf(dev, "cannot %s promiscuous mode\n",
2369 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2371 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2372 device_printf(dev, "cannot %s all-multicast mode\n",
2373 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2377 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2379 struct virtio_net_ctrl_hdr hdr __aligned(2);
2380 struct sglist_seg segs[3];
2385 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
2390 hdr.class = VIRTIO_NET_CTRL_RX;
2393 ack = VIRTIO_NET_ERR;
2395 sglist_init(&sg, 3, segs);
2396 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2397 error |= sglist_append(&sg, &onoff, sizeof(uint8_t));
2398 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2399 KASSERT(error == 0 && sg.sg_nseg == 3,
2400 ("error adding Rx filter message to sglist"));
2402 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2404 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2408 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2411 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2415 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2418 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2422 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2424 struct virtio_net_ctrl_hdr hdr __aligned(2);
2425 struct vtnet_mac_filter *filter;
2426 struct sglist_seg segs[4];
2430 struct ifaddr_container *ifac;
2431 struct ifmultiaddr *ifma;
2432 int ucnt, mcnt, promisc, allmulti, error;
2435 ifp = sc->vtnet_ifp;
2441 ASSERT_SERIALIZED(&sc->vtnet_slz);
2442 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2443 ("%s: CTRL_RX feature not negotiated", __func__));
2445 /* Use the MAC filtering table allocated in vtnet_attach. */
2446 filter = sc->vtnet_macfilter;
2447 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2449 /* Unicast MAC addresses: */
2450 //if_addr_rlock(ifp);
2451 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2453 if (ifa->ifa_addr->sa_family != AF_LINK)
2455 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2456 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
2458 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
2463 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2464 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2467 //if_addr_runlock(ifp);
2470 filter->vmf_unicast.nentries = 0;
2471 if_printf(ifp, "more than %d MAC addresses assigned, "
2472 "falling back to promiscuous mode\n",
2473 VTNET_MAX_MAC_ENTRIES);
2475 filter->vmf_unicast.nentries = ucnt;
2477 /* Multicast MAC addresses: */
2478 //if_maddr_rlock(ifp);
2479 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2480 if (ifma->ifma_addr->sa_family != AF_LINK)
2482 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
2487 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2488 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2491 //if_maddr_runlock(ifp);
2493 if (allmulti != 0) {
2494 filter->vmf_multicast.nentries = 0;
2495 if_printf(ifp, "more than %d multicast MAC addresses "
2496 "assigned, falling back to all-multicast mode\n",
2497 VTNET_MAX_MAC_ENTRIES);
2499 filter->vmf_multicast.nentries = mcnt;
2501 if (promisc != 0 && allmulti != 0)
2504 hdr.class = VIRTIO_NET_CTRL_MAC;
2505 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2506 ack = VIRTIO_NET_ERR;
2508 sglist_init(&sg, 4, segs);
2510 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2511 error |= sglist_append(&sg, &filter->vmf_unicast,
2512 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
2513 error |= sglist_append(&sg, &filter->vmf_multicast,
2514 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
2515 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2516 KASSERT(error == 0 && sg.sg_nseg == 4,
2517 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
2519 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2521 if (ack != VIRTIO_NET_OK)
2522 if_printf(ifp, "error setting host MAC filter table\n");
2525 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
2526 if_printf(ifp, "cannot enable promiscuous mode\n");
2527 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
2528 if_printf(ifp, "cannot enable all-multicast mode\n");
2532 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2534 struct sglist_seg segs[3];
2537 struct virtio_net_ctrl_hdr hdr __aligned(2);
2545 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
2546 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2548 s.ack = VIRTIO_NET_ERR;
2550 sglist_init(&sg, 3, segs);
2552 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2553 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
2554 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2555 KASSERT(error == 0 && sg.sg_nseg == 3,
2556 ("%s: error %d adding VLAN message to sglist", __func__, error));
2558 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2560 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2564 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2569 int i, nvlans, error;
2571 ASSERT_SERIALIZED(&sc->vtnet_slz);
2572 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2573 ("VLAN_FILTER feature not negotiated"));
2575 dev = sc->vtnet_dev;
2576 nvlans = sc->vtnet_nvlans;
2579 /* Enable filtering for each configured VLAN. */
2580 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2581 w = sc->vtnet_vlan_shadow[i];
2582 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) {
2583 if ((w & mask) != 0) {
2586 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0)
2592 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2594 device_printf(dev, "cannot restore VLAN filter table\n");
2598 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2603 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2604 ("VLAN_FILTER feature not negotiated"));
2606 if ((tag == 0) || (tag > 4095))
2609 ifp = sc->vtnet_ifp;
2610 idx = (tag >> 5) & 0x7F;
2613 lwkt_serialize_enter(&sc->vtnet_slz);
2615 /* Update shadow VLAN table. */
2618 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2621 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2624 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2625 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2626 device_printf(sc->vtnet_dev,
2627 "cannot %s VLAN %d %s the host filter table\n",
2628 add ? "add" : "remove", tag,
2629 add ? "to" : "from");
2633 lwkt_serialize_exit(&sc->vtnet_slz);
2637 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2640 if (ifp->if_softc != arg)
2643 vtnet_set_vlan_filter(arg, 1, tag);
2647 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2650 if (ifp->if_softc != arg)
2653 vtnet_set_vlan_filter(arg, 0, tag);
2657 vtnet_ifmedia_upd(struct ifnet *ifp)
2659 struct vtnet_softc *sc;
2660 struct ifmedia *ifm;
2663 ifm = &sc->vtnet_media;
2665 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2672 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2674 struct vtnet_softc *sc;
2678 ifmr->ifm_status = IFM_AVALID;
2679 ifmr->ifm_active = IFM_ETHER;
2681 lwkt_serialize_enter(&sc->vtnet_slz);
2682 if (vtnet_is_link_up(sc) != 0) {
2683 ifmr->ifm_status |= IFM_ACTIVE;
2684 ifmr->ifm_active |= VTNET_MEDIATYPE;
2686 ifmr->ifm_active |= IFM_NONE;
2687 lwkt_serialize_exit(&sc->vtnet_slz);
2691 vtnet_add_statistics(struct vtnet_softc *sc)
2694 struct vtnet_statistics *stats;
2695 struct sysctl_ctx_list *ctx;
2696 struct sysctl_oid *tree;
2697 struct sysctl_oid_list *child;
2699 dev = sc->vtnet_dev;
2700 stats = &sc->vtnet_stats;
2701 ctx = device_get_sysctl_ctx(dev);
2702 tree = device_get_sysctl_tree(dev);
2703 child = SYSCTL_CHILDREN(tree);
2705 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2706 CTLFLAG_RD, &stats->mbuf_alloc_failed,
2707 "Mbuf cluster allocation failures");
2708 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_frame_too_large",
2709 CTLFLAG_RD, &stats->rx_frame_too_large,
2710 "Received frame larger than the mbuf chain");
2711 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2712 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
2713 "Enqueuing the replacement receive mbuf failed");
2714 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_mergeable_failed",
2715 CTLFLAG_RD, &stats->rx_mergeable_failed,
2716 "Mergeable buffers receive failures");
2717 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2718 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
2719 "Received checksum offloaded buffer with unsupported "
2721 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_start",
2722 CTLFLAG_RD, &stats->rx_csum_bad_start,
2723 "Received checksum offloaded buffer with incorrect start offset");
2724 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2725 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
2726 "Received checksum offloaded buffer with incorrect IP protocol");
2727 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2728 CTLFLAG_RD, &stats->rx_csum_bad_offset,
2729 "Received checksum offloaded buffer with incorrect offset");
2730 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_failed",
2731 CTLFLAG_RD, &stats->rx_csum_failed,
2732 "Received buffer checksum offload failed");
2733 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_offloaded",
2734 CTLFLAG_RD, &stats->rx_csum_offloaded,
2735 "Received buffer checksum offload succeeded");
2736 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_task_rescheduled",
2737 CTLFLAG_RD, &stats->rx_task_rescheduled,
2738 "Times the receive interrupt task rescheduled itself");
2740 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_offloaded",
2741 CTLFLAG_RD, &stats->tx_csum_offloaded,
2742 "Offloaded checksum of transmitted buffer");
2743 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_offloaded",
2744 CTLFLAG_RD, &stats->tx_tso_offloaded,
2745 "Segmentation offload of transmitted buffer");
2746 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2747 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
2748 "Aborted transmit of checksum offloaded buffer with unknown "
2750 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2751 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
2752 "Aborted transmit of TSO buffer with unknown Ethernet type");
2753 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_task_rescheduled",
2754 CTLFLAG_RD, &stats->tx_task_rescheduled,
2755 "Times the transmit interrupt task rescheduled itself");
2759 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2762 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2766 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2769 virtqueue_disable_intr(sc->vtnet_rx_vq);
2773 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2776 #ifdef VTNET_TX_INTR_MODERATION
2779 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2784 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2787 virtqueue_disable_intr(sc->vtnet_tx_vq);