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_CTRL_VQ 0x0004
102 #define VTNET_FLAG_CTRL_RX 0x0008
103 #define VTNET_FLAG_VLAN_FILTER 0x0010
104 #define VTNET_FLAG_TSO_ECN 0x0020
105 #define VTNET_FLAG_MRG_RXBUFS 0x0040
106 #define VTNET_FLAG_LRO_NOMRG 0x0080
108 struct virtqueue *vtnet_rx_vq;
109 struct virtqueue *vtnet_tx_vq;
110 struct virtqueue *vtnet_ctrl_vq;
112 struct vtnet_tx_header *vtnet_txhdrarea;
113 uint32_t vtnet_txhdridx;
114 struct vtnet_mac_filter *vtnet_macfilter;
119 int vtnet_rx_process_limit;
120 int vtnet_rx_mbuf_size;
121 int vtnet_rx_mbuf_count;
123 int vtnet_watchdog_timer;
124 uint64_t vtnet_features;
126 struct task vtnet_cfgchg_task;
128 struct vtnet_statistics vtnet_stats;
130 struct callout vtnet_tick_ch;
132 eventhandler_tag vtnet_vlan_attach;
133 eventhandler_tag vtnet_vlan_detach;
135 struct ifmedia vtnet_media;
137 * Fake media type; the host does not provide us with
138 * any real media information.
140 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX)
141 char vtnet_hwaddr[ETHER_ADDR_LEN];
144 * During reset, the host's VLAN filtering table is lost. The
145 * array below is used to restore all the VLANs configured on
146 * this interface after a reset.
148 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32)
150 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
152 char vtnet_mtx_name[16];
156 * When mergeable buffers are not negotiated, the vtnet_rx_header structure
157 * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
158 * both keep the VirtIO header and the data non-contiguous and to keep the
159 * frame's payload 4 byte aligned.
161 * When mergeable buffers are negotiated, the host puts the VirtIO header in
162 * the beginning of the first mbuf's data.
164 #define VTNET_RX_HEADER_PAD 4
165 struct vtnet_rx_header {
166 struct virtio_net_hdr vrh_hdr;
167 char vrh_pad[VTNET_RX_HEADER_PAD];
171 * For each outgoing frame, the vtnet_tx_header below is allocated from
172 * the vtnet_tx_header_zone.
174 struct vtnet_tx_header {
176 struct virtio_net_hdr hdr;
177 struct virtio_net_hdr_mrg_rxbuf mhdr;
180 struct mbuf *vth_mbuf;
183 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
186 * The VirtIO specification does not place a limit on the number of MAC
187 * addresses the guest driver may request to be filtered. In practice,
188 * the host is constrained by available resources. To simplify this driver,
189 * impose a reasonably high limit of MAC addresses we will filter before
190 * falling back to promiscuous or all-multicast modes.
192 #define VTNET_MAX_MAC_ENTRIES 128
194 struct vtnet_mac_table {
196 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
199 struct vtnet_mac_filter {
200 struct vtnet_mac_table vmf_unicast;
201 uint32_t vmf_pad; /* Make tables non-contiguous. */
202 struct vtnet_mac_table vmf_multicast;
205 #define VTNET_WATCHDOG_TIMEOUT 5
206 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP)
208 /* Features desired/implemented by this driver. */
209 #define VTNET_FEATURES \
210 (VIRTIO_NET_F_MAC | \
211 VIRTIO_NET_F_STATUS | \
212 VIRTIO_NET_F_CTRL_VQ | \
213 VIRTIO_NET_F_CTRL_RX | \
214 VIRTIO_NET_F_CTRL_VLAN | \
215 VIRTIO_NET_F_CSUM | \
216 VIRTIO_NET_F_HOST_TSO4 | \
217 VIRTIO_NET_F_HOST_TSO6 | \
218 VIRTIO_NET_F_HOST_ECN | \
219 VIRTIO_NET_F_GUEST_CSUM | \
220 VIRTIO_NET_F_GUEST_TSO4 | \
221 VIRTIO_NET_F_GUEST_TSO6 | \
222 VIRTIO_NET_F_GUEST_ECN | \
223 VIRTIO_NET_F_MRG_RXBUF)
226 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
227 * frames larger than 1514 bytes. We do not yet support software LRO
230 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
231 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
233 #define VTNET_MAX_MTU 65536
234 #define VTNET_MAX_RX_SIZE 65550
237 * Used to preallocate the Vq indirect descriptors. The first segment
238 * is reserved for the header.
240 #define VTNET_MIN_RX_SEGS 2
241 #define VTNET_MAX_RX_SEGS 34
242 #define VTNET_MAX_TX_SEGS 34
244 #define IFCAP_TSO4 0x00100 /* can do TCP Segmentation Offload */
245 #define IFCAP_TSO6 0x00200 /* can do TCP6 Segmentation Offload */
246 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */
247 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */
248 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */
252 * Assert we can receive and transmit the maximum with regular
255 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
256 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
259 * Determine how many mbufs are in each receive buffer. For LRO without
260 * mergeable descriptors, we must allocate an mbuf chain large enough to
261 * hold both the vtnet_rx_header and the maximum receivable data.
263 #define VTNET_NEEDED_RX_MBUFS(_sc) \
264 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \
265 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \
266 (_sc)->vtnet_rx_mbuf_size)
268 static int vtnet_modevent(module_t, int, void *);
270 static int vtnet_probe(device_t);
271 static int vtnet_attach(device_t);
272 static int vtnet_detach(device_t);
273 static int vtnet_suspend(device_t);
274 static int vtnet_resume(device_t);
275 static int vtnet_shutdown(device_t);
276 static int vtnet_config_change(device_t);
278 static void vtnet_negotiate_features(struct vtnet_softc *);
279 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
280 static void vtnet_get_hwaddr(struct vtnet_softc *);
281 static void vtnet_set_hwaddr(struct vtnet_softc *);
282 static int vtnet_is_link_up(struct vtnet_softc *);
283 static void vtnet_update_link_status(struct vtnet_softc *);
285 static void vtnet_watchdog(struct vtnet_softc *);
287 static void vtnet_config_change_task(void *, int);
288 static int vtnet_change_mtu(struct vtnet_softc *, int);
289 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
291 static int vtnet_init_rx_vq(struct vtnet_softc *);
292 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
293 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
294 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
296 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
298 static int vtnet_replace_rxbuf(struct vtnet_softc *,
300 static int vtnet_newbuf(struct vtnet_softc *);
301 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
302 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
303 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
304 static void vtnet_vlan_tag_remove(struct mbuf *);
305 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
306 struct virtio_net_hdr *);
307 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
308 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
309 static void vtnet_rx_intr_task(void *);
310 static int vtnet_rx_vq_intr(void *);
312 static void vtnet_txeof(struct vtnet_softc *);
313 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
314 struct virtio_net_hdr *);
315 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
316 struct vtnet_tx_header *);
317 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
318 static void vtnet_start_locked(struct ifnet *, struct ifaltq_subque *);
319 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
320 static void vtnet_tick(void *);
321 static void vtnet_tx_intr_task(void *);
322 static int vtnet_tx_vq_intr(void *);
324 static void vtnet_stop(struct vtnet_softc *);
325 static int vtnet_reinit(struct vtnet_softc *);
326 static void vtnet_init_locked(struct vtnet_softc *);
327 static void vtnet_init(void *);
329 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
330 struct sglist *, int, int);
332 static void vtnet_rx_filter(struct vtnet_softc *sc);
333 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
334 static int vtnet_set_promisc(struct vtnet_softc *, int);
335 static int vtnet_set_allmulti(struct vtnet_softc *, int);
336 static void vtnet_rx_filter_mac(struct vtnet_softc *);
338 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
339 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
340 static void vtnet_set_vlan_filter(struct vtnet_softc *, int, uint16_t);
341 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
342 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
344 static int vtnet_ifmedia_upd(struct ifnet *);
345 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
347 static void vtnet_add_statistics(struct vtnet_softc *);
349 static int vtnet_enable_rx_intr(struct vtnet_softc *);
350 static int vtnet_enable_tx_intr(struct vtnet_softc *);
351 static void vtnet_disable_rx_intr(struct vtnet_softc *);
352 static void vtnet_disable_tx_intr(struct vtnet_softc *);
355 static int vtnet_csum_disable = 0;
356 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
357 static int vtnet_tso_disable = 1;
358 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
359 static int vtnet_lro_disable = 1;
360 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
363 * Reducing the number of transmit completed interrupts can
364 * improve performance. To do so, the define below keeps the
365 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
366 * in the start and watchdog paths. The price to pay for this
367 * is the m_free'ing of transmitted mbufs may be delayed until
368 * the watchdog fires.
370 #define VTNET_TX_INTR_MODERATION
372 static struct virtio_feature_desc vtnet_feature_desc[] = {
373 { VIRTIO_NET_F_CSUM, "TxChecksum" },
374 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
375 { VIRTIO_NET_F_MAC, "MacAddress" },
376 { VIRTIO_NET_F_GSO, "TxAllGSO" },
377 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
378 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
379 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
380 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
381 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
382 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
383 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
384 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
385 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
386 { VIRTIO_NET_F_STATUS, "Status" },
387 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
388 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
389 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
390 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
391 { VIRTIO_NET_F_MQ, "RFS" },
395 static device_method_t vtnet_methods[] = {
396 /* Device methods. */
397 DEVMETHOD(device_probe, vtnet_probe),
398 DEVMETHOD(device_attach, vtnet_attach),
399 DEVMETHOD(device_detach, vtnet_detach),
400 DEVMETHOD(device_suspend, vtnet_suspend),
401 DEVMETHOD(device_resume, vtnet_resume),
402 DEVMETHOD(device_shutdown, vtnet_shutdown),
404 /* VirtIO methods. */
405 DEVMETHOD(virtio_config_change, vtnet_config_change),
410 static driver_t vtnet_driver = {
413 sizeof(struct vtnet_softc)
416 static devclass_t vtnet_devclass;
418 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
420 MODULE_VERSION(vtnet, 1);
421 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
424 vtnet_modevent(module_t mod, int type, void *unused)
446 vtnet_probe(device_t dev)
448 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
451 device_set_desc(dev, "VirtIO Networking Adapter");
453 return (BUS_PROBE_DEFAULT);
457 vtnet_attach(device_t dev)
459 struct vtnet_softc *sc;
463 sc = device_get_softc(dev);
466 lwkt_serialize_init(&sc->vtnet_slz);
467 callout_init(&sc->vtnet_tick_ch);
469 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
471 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
472 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
474 vtnet_add_statistics(sc);
476 virtio_set_feature_desc(dev, vtnet_feature_desc);
477 vtnet_negotiate_features(sc);
479 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
480 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
481 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
483 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
486 sc->vtnet_rx_mbuf_size = MCLBYTES;
487 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
489 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
490 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
492 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
493 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
494 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
495 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
498 vtnet_get_hwaddr(sc);
500 error = vtnet_alloc_virtqueues(sc);
502 device_printf(dev, "cannot allocate virtqueues\n");
506 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
508 device_printf(dev, "cannot allocate ifnet structure\n");
514 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
515 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
516 ifp->if_init = vtnet_init;
517 ifp->if_start = vtnet_start;
518 ifp->if_ioctl = vtnet_ioctl;
520 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
521 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
523 tx_size = virtqueue_size(sc->vtnet_tx_vq);
524 sc->vtnet_tx_size = tx_size;
525 sc->vtnet_txhdridx = 0;
526 sc->vtnet_txhdrarea = contigmalloc(
527 ((sc->vtnet_tx_size / 2) + 1) * sizeof(struct vtnet_tx_header),
528 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
529 if (sc->vtnet_txhdrarea == NULL) {
530 device_printf(dev, "cannot contigmalloc the tx headers\n");
533 sc->vtnet_macfilter = contigmalloc(
534 sizeof(struct vtnet_mac_filter),
535 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
536 if (sc->vtnet_macfilter == NULL) {
538 "cannot contigmalloc the mac filter table\n");
541 ifq_set_maxlen(&ifp->if_snd, tx_size - 1);
542 ifq_set_ready(&ifp->if_snd);
544 ether_ifattach(ifp, sc->vtnet_hwaddr, NULL);
546 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)){
547 //ifp->if_capabilities |= IFCAP_LINKSTATE;
548 kprintf("add dynamic link state\n");
551 /* Tell the upper layer(s) we support long frames. */
552 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
553 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
555 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
556 ifp->if_capabilities |= IFCAP_TXCSUM;
558 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
559 ifp->if_capabilities |= IFCAP_TSO4;
560 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
561 ifp->if_capabilities |= IFCAP_TSO6;
562 if (ifp->if_capabilities & IFCAP_TSO)
563 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
565 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
566 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
569 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
570 ifp->if_capabilities |= IFCAP_RXCSUM;
572 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
573 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
574 ifp->if_capabilities |= IFCAP_LRO;
577 if (ifp->if_capabilities & IFCAP_HWCSUM) {
579 * VirtIO does not support VLAN tagging, but we can fake
580 * it by inserting and removing the 802.1Q header during
581 * transmit and receive. We are then able to do checksum
582 * offloading of VLAN frames.
584 ifp->if_capabilities |=
585 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
588 ifp->if_capenable = ifp->if_capabilities;
591 * Capabilities after here are not enabled by default.
594 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
595 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
597 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
598 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
599 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
600 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
603 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc);
605 error = virtio_setup_intr(dev, &sc->vtnet_slz);
607 device_printf(dev, "cannot setup virtqueue interrupts\n");
613 * Device defaults to promiscuous mode for backwards
614 * compatibility. Turn it off if possible.
616 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
617 lwkt_serialize_enter(&sc->vtnet_slz);
618 if (vtnet_set_promisc(sc, 0) != 0) {
619 ifp->if_flags |= IFF_PROMISC;
621 "cannot disable promiscuous mode\n");
623 lwkt_serialize_exit(&sc->vtnet_slz);
625 ifp->if_flags |= IFF_PROMISC;
635 vtnet_detach(device_t dev)
637 struct vtnet_softc *sc;
640 sc = device_get_softc(dev);
643 if (device_is_attached(dev)) {
644 lwkt_serialize_enter(&sc->vtnet_slz);
646 lwkt_serialize_exit(&sc->vtnet_slz);
648 callout_stop(&sc->vtnet_tick_ch);
649 taskqueue_drain(taskqueue_swi, &sc->vtnet_cfgchg_task);
654 if (sc->vtnet_vlan_attach != NULL) {
655 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
656 sc->vtnet_vlan_attach = NULL;
658 if (sc->vtnet_vlan_detach != NULL) {
659 EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach);
660 sc->vtnet_vlan_detach = NULL;
665 sc->vtnet_ifp = NULL;
668 if (sc->vtnet_rx_vq != NULL)
669 vtnet_free_rx_mbufs(sc);
670 if (sc->vtnet_tx_vq != NULL)
671 vtnet_free_tx_mbufs(sc);
672 if (sc->vtnet_ctrl_vq != NULL)
673 vtnet_free_ctrl_vq(sc);
675 if (sc->vtnet_txhdrarea != NULL) {
676 contigfree(sc->vtnet_txhdrarea,
677 ((sc->vtnet_tx_size / 2) + 1) *
678 sizeof(struct vtnet_tx_header), M_VTNET);
679 sc->vtnet_txhdrarea = NULL;
681 if (sc->vtnet_macfilter != NULL) {
682 contigfree(sc->vtnet_macfilter,
683 sizeof(struct vtnet_mac_filter), M_DEVBUF);
684 sc->vtnet_macfilter = NULL;
687 ifmedia_removeall(&sc->vtnet_media);
693 vtnet_suspend(device_t dev)
695 struct vtnet_softc *sc;
697 sc = device_get_softc(dev);
699 lwkt_serialize_enter(&sc->vtnet_slz);
701 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
702 lwkt_serialize_exit(&sc->vtnet_slz);
708 vtnet_resume(device_t dev)
710 struct vtnet_softc *sc;
713 sc = device_get_softc(dev);
716 lwkt_serialize_enter(&sc->vtnet_slz);
717 if (ifp->if_flags & IFF_UP)
718 vtnet_init_locked(sc);
719 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
720 lwkt_serialize_exit(&sc->vtnet_slz);
726 vtnet_shutdown(device_t dev)
730 * Suspend already does all of what we need to
731 * do here; we just never expect to be resumed.
733 return (vtnet_suspend(dev));
737 vtnet_config_change(device_t dev)
739 struct vtnet_softc *sc;
741 sc = device_get_softc(dev);
743 taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->vtnet_cfgchg_task);
749 vtnet_negotiate_features(struct vtnet_softc *sc)
752 uint64_t mask, features;
757 if (vtnet_csum_disable)
758 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
761 * TSO and LRO are only available when their corresponding
762 * checksum offload feature is also negotiated.
765 if (vtnet_csum_disable || vtnet_tso_disable)
766 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
767 VIRTIO_NET_F_HOST_ECN;
769 if (vtnet_csum_disable || vtnet_lro_disable)
770 mask |= VTNET_LRO_FEATURES;
772 features = VTNET_FEATURES & ~mask;
773 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
774 sc->vtnet_features = virtio_negotiate_features(dev, features);
778 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
781 struct vq_alloc_info vq_info[3];
788 * Indirect descriptors are not needed for the Rx
789 * virtqueue when mergeable buffers are negotiated.
790 * The header is placed inline with the data, not
791 * in a separate descriptor, and mbuf clusters are
792 * always physically contiguous.
794 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
795 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
796 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
800 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
801 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
802 "%s receive", device_get_nameunit(dev));
804 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
805 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
806 "%s transmit", device_get_nameunit(dev));
808 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
811 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
812 &sc->vtnet_ctrl_vq, "%s control",
813 device_get_nameunit(dev));
816 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
820 vtnet_get_hwaddr(struct vtnet_softc *sc)
826 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
827 virtio_read_device_config(dev,
828 offsetof(struct virtio_net_config, mac),
829 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
831 /* Generate random locally administered unicast address. */
832 sc->vtnet_hwaddr[0] = 0xB2;
833 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
835 vtnet_set_hwaddr(sc);
840 vtnet_set_hwaddr(struct vtnet_softc *sc)
846 virtio_write_device_config(dev,
847 offsetof(struct virtio_net_config, mac),
848 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
852 vtnet_is_link_up(struct vtnet_softc *sc)
861 ASSERT_SERIALIZED(&sc->vtnet_slz);
863 status = virtio_read_dev_config_2(dev,
864 offsetof(struct virtio_net_config, status));
866 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
870 vtnet_update_link_status(struct vtnet_softc *sc)
874 struct ifaltq_subque *ifsq;
879 ifsq = ifq_get_subq_default(&ifp->if_snd);
881 link = vtnet_is_link_up(sc);
883 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
884 sc->vtnet_flags |= VTNET_FLAG_LINK;
886 device_printf(dev, "Link is up\n");
887 ifp->if_link_state = LINK_STATE_UP;
888 if_link_state_change(ifp);
889 if (!ifsq_is_empty(ifsq))
890 vtnet_start_locked(ifp, ifsq);
891 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
892 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
894 device_printf(dev, "Link is down\n");
896 ifp->if_link_state = LINK_STATE_DOWN;
897 if_link_state_change(ifp);
903 vtnet_watchdog(struct vtnet_softc *sc)
909 #ifdef VTNET_TX_INTR_MODERATION
913 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
916 if_printf(ifp, "watchdog timeout -- resetting\n");
918 virtqueue_dump(sc->vtnet_tx_vq);
921 ifp->if_flags &= ~IFF_RUNNING;
922 vtnet_init_locked(sc);
927 vtnet_config_change_task(void *arg, int pending)
929 struct vtnet_softc *sc;
933 lwkt_serialize_enter(&sc->vtnet_slz);
934 vtnet_update_link_status(sc);
935 lwkt_serialize_exit(&sc->vtnet_slz);
939 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
941 struct vtnet_softc *sc;
943 int reinit, mask, error;
946 ifr = (struct ifreq *) data;
952 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
954 else if (ifp->if_mtu != ifr->ifr_mtu) {
955 lwkt_serialize_enter(&sc->vtnet_slz);
956 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
957 lwkt_serialize_exit(&sc->vtnet_slz);
962 lwkt_serialize_enter(&sc->vtnet_slz);
963 if ((ifp->if_flags & IFF_UP) == 0) {
964 if (ifp->if_flags & IFF_RUNNING)
966 } else if (ifp->if_flags & IFF_RUNNING) {
967 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
968 (IFF_PROMISC | IFF_ALLMULTI)) {
969 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
975 vtnet_init_locked(sc);
978 sc->vtnet_if_flags = ifp->if_flags;
979 lwkt_serialize_exit(&sc->vtnet_slz);
984 lwkt_serialize_enter(&sc->vtnet_slz);
985 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
986 (ifp->if_flags & IFF_RUNNING))
987 vtnet_rx_filter_mac(sc);
988 lwkt_serialize_exit(&sc->vtnet_slz);
993 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
997 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
999 lwkt_serialize_enter(&sc->vtnet_slz);
1001 if (mask & IFCAP_TXCSUM) {
1002 ifp->if_capenable ^= IFCAP_TXCSUM;
1003 if (ifp->if_capenable & IFCAP_TXCSUM)
1004 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1006 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1009 if (mask & IFCAP_TSO4) {
1010 ifp->if_capenable ^= IFCAP_TSO4;
1011 if (ifp->if_capenable & IFCAP_TSO4)
1012 ifp->if_hwassist |= CSUM_TSO;
1014 ifp->if_hwassist &= ~CSUM_TSO;
1017 if (mask & IFCAP_RXCSUM) {
1018 ifp->if_capenable ^= IFCAP_RXCSUM;
1022 if (mask & IFCAP_LRO) {
1023 ifp->if_capenable ^= IFCAP_LRO;
1027 if (mask & IFCAP_VLAN_HWFILTER) {
1028 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1032 if (mask & IFCAP_VLAN_HWTSO)
1033 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1035 if (mask & IFCAP_VLAN_HWTAGGING)
1036 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1038 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1039 ifp->if_flags &= ~IFF_RUNNING;
1040 vtnet_init_locked(sc);
1042 //VLAN_CAPABILITIES(ifp);
1044 lwkt_serialize_exit(&sc->vtnet_slz);
1048 error = ether_ioctl(ifp, cmd, data);
1056 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1059 int new_frame_size, clsize;
1061 ifp = sc->vtnet_ifp;
1063 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1064 new_frame_size = sizeof(struct vtnet_rx_header) +
1065 sizeof(struct ether_vlan_header) + new_mtu;
1067 if (new_frame_size > MJUM9BYTES)
1070 if (new_frame_size <= MCLBYTES)
1073 clsize = MJUM9BYTES;
1075 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1076 sizeof(struct ether_vlan_header) + new_mtu;
1078 if (new_frame_size <= MCLBYTES)
1081 clsize = MJUMPAGESIZE;
1084 sc->vtnet_rx_mbuf_size = clsize;
1085 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1086 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1087 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1089 ifp->if_mtu = new_mtu;
1091 if (ifp->if_flags & IFF_RUNNING) {
1092 ifp->if_flags &= ~IFF_RUNNING;
1093 vtnet_init_locked(sc);
1100 vtnet_init_rx_vq(struct vtnet_softc *sc)
1102 struct virtqueue *vq;
1105 vq = sc->vtnet_rx_vq;
1109 while (!virtqueue_full(vq)) {
1110 if ((error = vtnet_newbuf(sc)) != 0)
1116 virtqueue_notify(vq, &sc->vtnet_slz);
1119 * EMSGSIZE signifies the virtqueue did not have enough
1120 * entries available to hold the last mbuf. This is not
1121 * an error. We should not get ENOSPC since we check if
1122 * the virtqueue is full before attempting to add a
1125 if (error == EMSGSIZE)
1133 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1135 struct virtqueue *vq;
1139 vq = sc->vtnet_rx_vq;
1142 while ((m = virtqueue_drain(vq, &last)) != NULL)
1145 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1149 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1151 struct virtqueue *vq;
1152 struct vtnet_tx_header *txhdr;
1155 vq = sc->vtnet_tx_vq;
1158 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1159 m_freem(txhdr->vth_mbuf);
1162 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1166 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1169 * The control virtqueue is only polled, therefore
1170 * it should already be empty.
1172 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1173 ("Ctrl Vq not empty"));
1176 static struct mbuf *
1177 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1179 struct mbuf *m_head, *m_tail, *m;
1182 clsize = sc->vtnet_rx_mbuf_size;
1184 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1185 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1186 m_head = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR );
1190 m_head->m_len = clsize;
1194 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1195 ("chained Rx mbuf requested without LRO_NOMRG"));
1197 for (i = 0; i < nbufs - 1; i++) {
1198 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1199 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1209 if (m_tailp != NULL)
1215 sc->vtnet_stats.mbuf_alloc_failed++;
1222 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1224 struct mbuf *m, *m_prev;
1225 struct mbuf *m_new, *m_tail;
1226 int len, clsize, nreplace, error;
1233 clsize = sc->vtnet_rx_mbuf_size;
1236 if (m->m_next != NULL)
1237 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1238 ("chained Rx mbuf without LRO_NOMRG"));
1241 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1242 * allocating an entire chain for each received frame. When
1243 * the received frame's length is less than that of the chain,
1244 * the unused mbufs are reassigned to the new chain.
1248 * Something is seriously wrong if we received
1249 * a frame larger than the mbuf chain. Drop it.
1252 sc->vtnet_stats.rx_frame_too_large++;
1256 KASSERT(m->m_len == clsize,
1257 ("mbuf length not expected cluster size: %d",
1260 m->m_len = MIN(m->m_len, len);
1268 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1269 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1270 ("too many replacement mbufs: %d/%d", nreplace,
1271 sc->vtnet_rx_mbuf_count));
1273 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1274 if (m_new == NULL) {
1275 m_prev->m_len = clsize;
1280 * Move unused mbufs, if any, from the original chain
1281 * onto the end of the new chain.
1283 if (m_prev->m_next != NULL) {
1284 m_tail->m_next = m_prev->m_next;
1285 m_prev->m_next = NULL;
1288 error = vtnet_enqueue_rxbuf(sc, m_new);
1291 * BAD! We could not enqueue the replacement mbuf chain. We
1292 * must restore the m0 chain to the original state if it was
1293 * modified so we can subsequently discard it.
1295 * NOTE: The replacement is suppose to be an identical copy
1296 * to the one just dequeued so this is an unexpected error.
1298 sc->vtnet_stats.rx_enq_replacement_failed++;
1300 if (m_tail->m_next != NULL) {
1301 m_prev->m_next = m_tail->m_next;
1302 m_tail->m_next = NULL;
1305 m_prev->m_len = clsize;
1313 vtnet_newbuf(struct vtnet_softc *sc)
1318 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1322 error = vtnet_enqueue_rxbuf(sc, m);
1330 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1332 struct virtqueue *vq;
1335 vq = sc->vtnet_rx_vq;
1337 while (--nbufs > 0) {
1338 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1340 vtnet_discard_rxbuf(sc, m);
1345 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1350 * Requeue the discarded mbuf. This should always be
1351 * successful since it was just dequeued.
1353 error = vtnet_enqueue_rxbuf(sc, m);
1354 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1358 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1361 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1362 struct vtnet_rx_header *rxhdr;
1363 struct virtio_net_hdr *hdr;
1367 ASSERT_SERIALIZED(&sc->vtnet_slz);
1368 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1369 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1371 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1373 mdata = mtod(m, uint8_t *);
1376 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1377 rxhdr = (struct vtnet_rx_header *) mdata;
1378 hdr = &rxhdr->vrh_hdr;
1379 offset += sizeof(struct vtnet_rx_header);
1381 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1382 KASSERT(error == 0, ("cannot add header to sglist"));
1385 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1389 if (m->m_next != NULL) {
1390 error = sglist_append_mbuf(&sg, m->m_next);
1395 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1399 vtnet_vlan_tag_remove(struct mbuf *m)
1401 struct ether_vlan_header *evl;
1403 evl = mtod(m, struct ether_vlan_header *);
1405 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1406 m->m_flags |= M_VLANTAG;
1408 /* Strip the 802.1Q header. */
1409 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1410 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1411 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1415 * Alternative method of doing receive checksum offloading. Rather
1416 * than parsing the received frame down to the IP header, use the
1417 * csum_offset to determine which CSUM_* flags are appropriate. We
1418 * can get by with doing this only because the checksum offsets are
1419 * unique for the things we care about.
1422 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1423 struct virtio_net_hdr *hdr)
1425 struct ether_header *eh;
1426 struct ether_vlan_header *evh;
1431 csum_len = hdr->csum_start + hdr->csum_offset;
1433 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1435 if (m->m_len < csum_len)
1438 eh = mtod(m, struct ether_header *);
1439 eth_type = ntohs(eh->ether_type);
1440 if (eth_type == ETHERTYPE_VLAN) {
1441 evh = mtod(m, struct ether_vlan_header *);
1442 eth_type = ntohs(evh->evl_proto);
1445 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1446 sc->vtnet_stats.rx_csum_bad_ethtype++;
1450 /* Use the offset to determine the appropriate CSUM_* flags. */
1451 switch (hdr->csum_offset) {
1452 case offsetof(struct udphdr, uh_sum):
1453 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1455 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1456 if (udp->uh_sum == 0)
1461 case offsetof(struct tcphdr, th_sum):
1462 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1463 m->m_pkthdr.csum_data = 0xFFFF;
1467 sc->vtnet_stats.rx_csum_bad_offset++;
1471 sc->vtnet_stats.rx_csum_offloaded++;
1477 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1480 struct virtqueue *vq;
1481 struct mbuf *m, *m_tail;
1484 ifp = sc->vtnet_ifp;
1485 vq = sc->vtnet_rx_vq;
1488 while (--nbufs > 0) {
1489 m = virtqueue_dequeue(vq, &len);
1495 if (vtnet_newbuf(sc) != 0) {
1497 vtnet_discard_rxbuf(sc, m);
1499 vtnet_discard_merged_rxbuf(sc, nbufs);
1507 m->m_flags &= ~M_PKTHDR;
1509 m_head->m_pkthdr.len += len;
1517 sc->vtnet_stats.rx_mergeable_failed++;
1524 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1526 struct virtio_net_hdr lhdr;
1528 struct virtqueue *vq;
1530 struct ether_header *eh;
1531 struct virtio_net_hdr *hdr;
1532 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1533 int len, deq, nbufs, adjsz, rx_npkts;
1535 ifp = sc->vtnet_ifp;
1536 vq = sc->vtnet_rx_vq;
1541 ASSERT_SERIALIZED(&sc->vtnet_slz);
1543 while (--count >= 0) {
1544 m = virtqueue_dequeue(vq, &len);
1549 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1551 vtnet_discard_rxbuf(sc, m);
1555 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1557 adjsz = sizeof(struct vtnet_rx_header);
1559 * Account for our pad between the header and
1560 * the actual start of the frame.
1562 len += VTNET_RX_HEADER_PAD;
1564 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1565 nbufs = mhdr->num_buffers;
1566 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1569 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1571 vtnet_discard_rxbuf(sc, m);
1573 vtnet_discard_merged_rxbuf(sc, nbufs);
1577 m->m_pkthdr.len = len;
1578 m->m_pkthdr.rcvif = ifp;
1579 m->m_pkthdr.csum_flags = 0;
1582 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1589 * Save copy of header before we strip it. For both mergeable
1590 * and non-mergeable, the VirtIO header is placed first in the
1591 * mbuf's data. We no longer need num_buffers, so always use a
1594 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1597 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1598 eh = mtod(m, struct ether_header *);
1599 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1600 vtnet_vlan_tag_remove(m);
1603 * With the 802.1Q header removed, update the
1604 * checksum starting location accordingly.
1606 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1608 ETHER_VLAN_ENCAP_LEN;
1612 if (ifp->if_capenable & IFCAP_RXCSUM &&
1613 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1614 if (vtnet_rx_csum(sc, m, hdr) != 0)
1615 sc->vtnet_stats.rx_csum_failed++;
1618 lwkt_serialize_exit(&sc->vtnet_slz);
1620 ifp->if_input(ifp, m, NULL, -1);
1621 lwkt_serialize_enter(&sc->vtnet_slz);
1624 * The interface may have been stopped while we were
1625 * passing the packet up the network stack.
1627 if ((ifp->if_flags & IFF_RUNNING) == 0)
1631 virtqueue_notify(vq, &sc->vtnet_slz);
1633 if (rx_npktsp != NULL)
1634 *rx_npktsp = rx_npkts;
1636 return (count > 0 ? 0 : EAGAIN);
1640 vtnet_rx_intr_task(void *arg)
1642 struct vtnet_softc *sc;
1647 ifp = sc->vtnet_ifp;
1650 // lwkt_serialize_enter(&sc->vtnet_slz);
1652 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1653 vtnet_enable_rx_intr(sc);
1654 // lwkt_serialize_exit(&sc->vtnet_slz);
1658 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1659 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1660 vtnet_disable_rx_intr(sc);
1664 // lwkt_serialize_exit(&sc->vtnet_slz);
1667 sc->vtnet_stats.rx_task_rescheduled++;
1673 vtnet_rx_vq_intr(void *xsc)
1675 struct vtnet_softc *sc;
1679 vtnet_disable_rx_intr(sc);
1680 vtnet_rx_intr_task(sc);
1686 vtnet_txeof(struct vtnet_softc *sc)
1688 struct virtqueue *vq;
1690 struct vtnet_tx_header *txhdr;
1693 vq = sc->vtnet_tx_vq;
1694 ifp = sc->vtnet_ifp;
1697 ASSERT_SERIALIZED(&sc->vtnet_slz);
1699 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1702 m_freem(txhdr->vth_mbuf);
1706 ifq_clr_oactive(&ifp->if_snd);
1707 if (virtqueue_empty(vq))
1708 sc->vtnet_watchdog_timer = 0;
1712 static struct mbuf *
1713 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1714 struct virtio_net_hdr *hdr)
1717 struct ether_header *eh;
1718 struct ether_vlan_header *evh;
1720 struct ip6_hdr *ip6;
1723 uint16_t eth_type, csum_start;
1724 uint8_t ip_proto, gso_type;
1726 ifp = sc->vtnet_ifp;
1729 ip_offset = sizeof(struct ether_header);
1730 if (m->m_len < ip_offset) {
1731 if ((m = m_pullup(m, ip_offset)) == NULL)
1735 eh = mtod(m, struct ether_header *);
1736 eth_type = ntohs(eh->ether_type);
1737 if (eth_type == ETHERTYPE_VLAN) {
1738 ip_offset = sizeof(struct ether_vlan_header);
1739 if (m->m_len < ip_offset) {
1740 if ((m = m_pullup(m, ip_offset)) == NULL)
1743 evh = mtod(m, struct ether_vlan_header *);
1744 eth_type = ntohs(evh->evl_proto);
1749 if (m->m_len < ip_offset + sizeof(struct ip)) {
1750 m = m_pullup(m, ip_offset + sizeof(struct ip));
1755 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1756 ip_proto = ip->ip_p;
1757 csum_start = ip_offset + (ip->ip_hl << 2);
1758 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1761 case ETHERTYPE_IPV6:
1762 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1763 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1768 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1770 * XXX Assume no extension headers are present. Presently,
1771 * this will always be true in the case of TSO, and FreeBSD
1772 * does not perform checksum offloading of IPv6 yet.
1774 ip_proto = ip6->ip6_nxt;
1775 csum_start = ip_offset + sizeof(struct ip6_hdr);
1776 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1783 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1784 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1785 hdr->csum_start = csum_start;
1786 hdr->csum_offset = m->m_pkthdr.csum_data;
1788 sc->vtnet_stats.tx_csum_offloaded++;
1791 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1792 if (ip_proto != IPPROTO_TCP)
1795 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1796 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1801 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1802 hdr->gso_type = gso_type;
1803 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1804 hdr->gso_size = m->m_pkthdr.tso_segsz;
1806 if (tcp->th_flags & TH_CWR) {
1808 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1809 * ECN support is only configurable globally with the
1810 * net.inet.tcp.ecn.enable sysctl knob.
1812 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1813 if_printf(ifp, "TSO with ECN not supported "
1819 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1822 sc->vtnet_stats.tx_tso_offloaded++;
1829 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1830 struct vtnet_tx_header *txhdr)
1833 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1834 struct virtqueue *vq;
1836 int collapsed, error;
1838 vq = sc->vtnet_tx_vq;
1842 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1843 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1844 KASSERT(error == 0 && sg.sg_nseg == 1,
1845 ("cannot add header to sglist"));
1848 error = sglist_append_mbuf(&sg, m);
1853 //m = m_collapse(m, M_NOWAIT, VTNET_MAX_TX_SEGS - 1);
1854 m = m_defrag(m, M_NOWAIT);
1863 txhdr->vth_mbuf = m;
1865 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1874 static struct mbuf *
1875 vtnet_vlan_tag_insert(struct mbuf *m)
1878 struct ether_vlan_header *evl;
1880 if (M_WRITABLE(m) == 0) {
1881 n = m_dup(m, M_NOWAIT);
1883 if ((m = n) == NULL)
1887 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1890 if (m->m_len < sizeof(struct ether_vlan_header)) {
1891 m = m_pullup(m, sizeof(struct ether_vlan_header));
1896 /* Insert 802.1Q header into the existing Ethernet header. */
1897 evl = mtod(m, struct ether_vlan_header *);
1898 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1899 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1900 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1901 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1902 m->m_flags &= ~M_VLANTAG;
1908 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1910 struct vtnet_tx_header *txhdr;
1911 struct virtio_net_hdr *hdr;
1915 txhdr = &sc->vtnet_txhdrarea[sc->vtnet_txhdridx];
1916 memset(txhdr, 0, sizeof(struct vtnet_tx_header));
1919 * Always use the non-mergeable header to simplify things. When
1920 * the mergeable feature is negotiated, the num_buffers field
1921 * must be set to zero. We use vtnet_hdr_size later to enqueue
1922 * the correct header size to the host.
1924 hdr = &txhdr->vth_uhdr.hdr;
1929 if (m->m_flags & M_VLANTAG) {
1930 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1931 m = vtnet_vlan_tag_insert(m);
1932 if ((*m_head = m) == NULL)
1934 m->m_flags &= ~M_VLANTAG;
1937 if (m->m_pkthdr.csum_flags != 0) {
1938 m = vtnet_tx_offload(sc, m, hdr);
1939 if ((*m_head = m) == NULL)
1943 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1945 sc->vtnet_txhdridx =
1946 (sc->vtnet_txhdridx + 1) % ((sc->vtnet_tx_size / 2) + 1);
1952 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1954 struct vtnet_softc *sc;
1958 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1959 lwkt_serialize_enter(&sc->vtnet_slz);
1960 vtnet_start_locked(ifp, ifsq);
1961 lwkt_serialize_exit(&sc->vtnet_slz);
1965 vtnet_start_locked(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1967 struct vtnet_softc *sc;
1968 struct virtqueue *vq;
1973 vq = sc->vtnet_tx_vq;
1976 ASSERT_SERIALIZED(&sc->vtnet_slz);
1978 if ((ifp->if_flags & (IFF_RUNNING)) !=
1979 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
1982 #ifdef VTNET_TX_INTR_MODERATION
1983 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
1987 while (!ifsq_is_empty(ifsq)) {
1988 if (virtqueue_full(vq)) {
1989 ifq_set_oactive(&ifp->if_snd);
1993 m0 = ifq_dequeue(&ifp->if_snd);
1997 if (vtnet_encap(sc, &m0) != 0) {
2000 ifq_prepend(&ifp->if_snd, m0);
2001 ifq_set_oactive(&ifp->if_snd);
2006 ETHER_BPF_MTAP(ifp, m0);
2010 virtqueue_notify(vq, &sc->vtnet_slz);
2011 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2016 vtnet_tick(void *xsc)
2018 struct vtnet_softc *sc;
2023 ASSERT_SERIALIZED(&sc->vtnet_slz);
2025 virtqueue_dump(sc->vtnet_rx_vq);
2026 virtqueue_dump(sc->vtnet_tx_vq);
2030 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2035 vtnet_tx_intr_task(void *arg)
2037 struct vtnet_softc *sc;
2039 struct ifaltq_subque *ifsq;
2042 ifp = sc->vtnet_ifp;
2043 ifsq = ifq_get_subq_default(&ifp->if_snd);
2046 // lwkt_serialize_enter(&sc->vtnet_slz);
2048 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2049 vtnet_enable_tx_intr(sc);
2050 // lwkt_serialize_exit(&sc->vtnet_slz);
2056 if (!ifsq_is_empty(ifsq))
2057 vtnet_start_locked(ifp, ifsq);
2059 if (vtnet_enable_tx_intr(sc) != 0) {
2060 vtnet_disable_tx_intr(sc);
2061 sc->vtnet_stats.tx_task_rescheduled++;
2062 // lwkt_serialize_exit(&sc->vtnet_slz);
2066 // lwkt_serialize_exit(&sc->vtnet_slz);
2070 vtnet_tx_vq_intr(void *xsc)
2072 struct vtnet_softc *sc;
2076 vtnet_disable_tx_intr(sc);
2077 vtnet_tx_intr_task(sc);
2083 vtnet_stop(struct vtnet_softc *sc)
2088 dev = sc->vtnet_dev;
2089 ifp = sc->vtnet_ifp;
2091 ASSERT_SERIALIZED(&sc->vtnet_slz);
2093 sc->vtnet_watchdog_timer = 0;
2094 callout_stop(&sc->vtnet_tick_ch);
2095 ifq_clr_oactive(&ifp->if_snd);
2096 ifp->if_flags &= ~(IFF_RUNNING);
2098 vtnet_disable_rx_intr(sc);
2099 vtnet_disable_tx_intr(sc);
2102 * Stop the host VirtIO adapter. Note this will reset the host
2103 * adapter's state back to the pre-initialized state, so in
2104 * order to make the device usable again, we must drive it
2105 * through virtio_reinit() and virtio_reinit_complete().
2109 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2111 vtnet_free_rx_mbufs(sc);
2112 vtnet_free_tx_mbufs(sc);
2116 vtnet_reinit(struct vtnet_softc *sc)
2121 ifp = sc->vtnet_ifp;
2122 features = sc->vtnet_features;
2125 * Re-negotiate with the host, removing any disabled receive
2126 * features. Transmit features are disabled only on our side
2127 * via if_capenable and if_hwassist.
2130 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2131 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2132 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2135 if (ifp->if_capabilities & IFCAP_LRO) {
2136 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2137 features &= ~VTNET_LRO_FEATURES;
2140 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2141 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2142 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2145 return (virtio_reinit(sc->vtnet_dev, features));
2149 vtnet_init_locked(struct vtnet_softc *sc)
2155 dev = sc->vtnet_dev;
2156 ifp = sc->vtnet_ifp;
2158 ASSERT_SERIALIZED(&sc->vtnet_slz);
2160 if (ifp->if_flags & IFF_RUNNING)
2163 /* Stop host's adapter, cancel any pending I/O. */
2166 /* Reinitialize the host device. */
2167 error = vtnet_reinit(sc);
2170 "reinitialization failed, stopping device...\n");
2175 /* Update host with assigned MAC address. */
2176 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2177 vtnet_set_hwaddr(sc);
2179 ifp->if_hwassist = 0;
2180 if (ifp->if_capenable & IFCAP_TXCSUM)
2181 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2182 if (ifp->if_capenable & IFCAP_TSO4)
2183 ifp->if_hwassist |= CSUM_TSO;
2185 error = vtnet_init_rx_vq(sc);
2188 "cannot allocate mbufs for Rx virtqueue\n");
2193 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2194 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2195 /* Restore promiscuous and all-multicast modes. */
2196 vtnet_rx_filter(sc);
2198 /* Restore filtered MAC addresses. */
2199 vtnet_rx_filter_mac(sc);
2202 /* Restore VLAN filters. */
2203 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2204 vtnet_rx_filter_vlan(sc);
2208 vtnet_enable_rx_intr(sc);
2209 vtnet_enable_tx_intr(sc);
2212 ifp->if_flags |= IFF_RUNNING;
2213 ifq_clr_oactive(&ifp->if_snd);
2215 virtio_reinit_complete(dev);
2217 vtnet_update_link_status(sc);
2218 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2222 vtnet_init(void *xsc)
2224 struct vtnet_softc *sc;
2228 lwkt_serialize_enter(&sc->vtnet_slz);
2229 vtnet_init_locked(sc);
2230 lwkt_serialize_exit(&sc->vtnet_slz);
2234 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2235 struct sglist *sg, int readable, int writable)
2237 struct virtqueue *vq;
2240 vq = sc->vtnet_ctrl_vq;
2242 ASSERT_SERIALIZED(&sc->vtnet_slz);
2243 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2244 ("no control virtqueue"));
2245 KASSERT(virtqueue_empty(vq),
2246 ("control command already enqueued"));
2248 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2251 virtqueue_notify(vq, &sc->vtnet_slz);
2254 * Poll until the command is complete. Previously, we would
2255 * sleep until the control virtqueue interrupt handler woke
2256 * us up, but dropping the VTNET_MTX leads to serialization
2259 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2260 * vectors. Two of those vectors are needed for the Rx and Tx
2261 * virtqueues. We do not support sharing both a Vq and config
2262 * changed notification on the same MSIX vector.
2264 c = virtqueue_poll(vq, NULL);
2265 KASSERT(c == cookie, ("unexpected control command response"));
2269 vtnet_rx_filter(struct vtnet_softc *sc)
2274 dev = sc->vtnet_dev;
2275 ifp = sc->vtnet_ifp;
2277 ASSERT_SERIALIZED(&sc->vtnet_slz);
2278 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2279 ("CTRL_RX feature not negotiated"));
2281 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2282 device_printf(dev, "cannot %s promiscuous mode\n",
2283 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2285 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2286 device_printf(dev, "cannot %s all-multicast mode\n",
2287 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2291 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2293 struct virtio_net_ctrl_hdr hdr __aligned(2);
2294 struct sglist_seg segs[3];
2299 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
2304 hdr.class = VIRTIO_NET_CTRL_RX;
2307 ack = VIRTIO_NET_ERR;
2309 sglist_init(&sg, 3, segs);
2310 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2311 error |= sglist_append(&sg, &onoff, sizeof(uint8_t));
2312 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2313 KASSERT(error == 0 && sg.sg_nseg == 3,
2314 ("error adding Rx filter message to sglist"));
2316 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2318 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2322 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2325 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2329 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2332 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2336 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2338 struct virtio_net_ctrl_hdr hdr __aligned(2);
2339 struct vtnet_mac_filter *filter;
2340 struct sglist_seg segs[4];
2344 struct ifaddr_container *ifac;
2345 struct ifmultiaddr *ifma;
2346 int ucnt, mcnt, promisc, allmulti, error;
2349 ifp = sc->vtnet_ifp;
2356 ASSERT_SERIALIZED(&sc->vtnet_slz);
2357 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2358 ("CTRL_RX feature not negotiated"));
2360 /* Use the MAC filtering table allocated in vtnet_attach. */
2361 filter = sc->vtnet_macfilter;
2362 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2364 /* Unicast MAC addresses: */
2365 //if_addr_rlock(ifp);
2366 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2368 if (ifa->ifa_addr->sa_family != AF_LINK)
2370 else if (ucnt == VTNET_MAX_MAC_ENTRIES)
2373 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2374 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2377 //if_addr_runlock(ifp);
2379 if (ucnt >= VTNET_MAX_MAC_ENTRIES) {
2381 filter->vmf_unicast.nentries = 0;
2383 if_printf(ifp, "more than %d MAC addresses assigned, "
2384 "falling back to promiscuous mode\n",
2385 VTNET_MAX_MAC_ENTRIES);
2387 filter->vmf_unicast.nentries = ucnt;
2389 /* Multicast MAC addresses: */
2390 //if_maddr_rlock(ifp);
2391 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2392 if (ifma->ifma_addr->sa_family != AF_LINK)
2394 else if (mcnt == VTNET_MAX_MAC_ENTRIES)
2397 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2398 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2401 //if_maddr_runlock(ifp);
2403 if (mcnt >= VTNET_MAX_MAC_ENTRIES) {
2405 filter->vmf_multicast.nentries = 0;
2407 if_printf(ifp, "more than %d multicast MAC addresses "
2408 "assigned, falling back to all-multicast mode\n",
2409 VTNET_MAX_MAC_ENTRIES);
2411 filter->vmf_multicast.nentries = mcnt;
2413 if (promisc && allmulti)
2416 hdr.class = VIRTIO_NET_CTRL_MAC;
2417 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2418 ack = VIRTIO_NET_ERR;
2420 sglist_init(&sg, 4, segs);
2421 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2422 error |= sglist_append(&sg, &filter->vmf_unicast,
2423 sizeof(struct vtnet_mac_table));
2424 error |= sglist_append(&sg, &filter->vmf_multicast,
2425 sizeof(struct vtnet_mac_table));
2426 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2427 KASSERT(error == 0 && sg.sg_nseg == 4,
2428 ("error adding MAC filtering message to sglist"));
2430 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2432 if (ack != VIRTIO_NET_OK)
2433 if_printf(ifp, "error setting host MAC filter table\n");
2437 if (vtnet_set_promisc(sc, 1) != 0)
2438 if_printf(ifp, "cannot enable promiscuous mode\n");
2440 if (vtnet_set_allmulti(sc, 1) != 0)
2441 if_printf(ifp, "cannot enable all-multicast mode\n");
2445 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2447 struct virtio_net_ctrl_hdr hdr __aligned(2);
2448 struct sglist_seg segs[3];
2453 hdr.class = VIRTIO_NET_CTRL_VLAN;
2454 hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2455 ack = VIRTIO_NET_ERR;
2458 sglist_init(&sg, 3, segs);
2459 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2460 error |= sglist_append(&sg, &tag, sizeof(uint16_t));
2461 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2462 KASSERT(error == 0 && sg.sg_nseg == 3,
2463 ("error adding VLAN control message to sglist"));
2465 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2467 return (ack == VIRTIO_NET_OK ? 0 : EIO);
2471 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2476 int i, nvlans, error;
2478 ASSERT_SERIALIZED(&sc->vtnet_slz);
2479 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2480 ("VLAN_FILTER feature not negotiated"));
2482 dev = sc->vtnet_dev;
2483 nvlans = sc->vtnet_nvlans;
2486 /* Enable filtering for each configured VLAN. */
2487 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2488 w = sc->vtnet_vlan_shadow[i];
2489 for (mask = 1, tag = i * 32; w != 0; mask <<= 1, tag++) {
2490 if ((w & mask) != 0) {
2493 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0)
2499 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2501 device_printf(dev, "cannot restore VLAN filter table\n");
2505 vtnet_set_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2510 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2511 ("VLAN_FILTER feature not negotiated"));
2513 if ((tag == 0) || (tag > 4095))
2516 ifp = sc->vtnet_ifp;
2517 idx = (tag >> 5) & 0x7F;
2520 lwkt_serialize_enter(&sc->vtnet_slz);
2522 /* Update shadow VLAN table. */
2525 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2528 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2531 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER) {
2532 if (vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2533 device_printf(sc->vtnet_dev,
2534 "cannot %s VLAN %d %s the host filter table\n",
2535 add ? "add" : "remove", tag,
2536 add ? "to" : "from");
2540 lwkt_serialize_exit(&sc->vtnet_slz);
2544 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2547 if (ifp->if_softc != arg)
2550 vtnet_set_vlan_filter(arg, 1, tag);
2554 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2557 if (ifp->if_softc != arg)
2560 vtnet_set_vlan_filter(arg, 0, tag);
2564 vtnet_ifmedia_upd(struct ifnet *ifp)
2566 struct vtnet_softc *sc;
2567 struct ifmedia *ifm;
2570 ifm = &sc->vtnet_media;
2572 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2579 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2581 struct vtnet_softc *sc;
2585 ifmr->ifm_status = IFM_AVALID;
2586 ifmr->ifm_active = IFM_ETHER;
2588 lwkt_serialize_enter(&sc->vtnet_slz);
2589 if (vtnet_is_link_up(sc) != 0) {
2590 ifmr->ifm_status |= IFM_ACTIVE;
2591 ifmr->ifm_active |= VTNET_MEDIATYPE;
2593 ifmr->ifm_active |= IFM_NONE;
2594 lwkt_serialize_exit(&sc->vtnet_slz);
2598 vtnet_add_statistics(struct vtnet_softc *sc)
2601 struct vtnet_statistics *stats;
2602 struct sysctl_ctx_list *ctx;
2603 struct sysctl_oid *tree;
2604 struct sysctl_oid_list *child;
2606 dev = sc->vtnet_dev;
2607 stats = &sc->vtnet_stats;
2608 ctx = device_get_sysctl_ctx(dev);
2609 tree = device_get_sysctl_tree(dev);
2610 child = SYSCTL_CHILDREN(tree);
2612 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2613 CTLFLAG_RD, &stats->mbuf_alloc_failed,
2614 "Mbuf cluster allocation failures");
2615 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_frame_too_large",
2616 CTLFLAG_RD, &stats->rx_frame_too_large,
2617 "Received frame larger than the mbuf chain");
2618 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2619 CTLFLAG_RD, &stats->rx_enq_replacement_failed,
2620 "Enqueuing the replacement receive mbuf failed");
2621 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_mergeable_failed",
2622 CTLFLAG_RD, &stats->rx_mergeable_failed,
2623 "Mergeable buffers receive failures");
2624 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2625 CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
2626 "Received checksum offloaded buffer with unsupported "
2628 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_start",
2629 CTLFLAG_RD, &stats->rx_csum_bad_start,
2630 "Received checksum offloaded buffer with incorrect start offset");
2631 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2632 CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
2633 "Received checksum offloaded buffer with incorrect IP protocol");
2634 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2635 CTLFLAG_RD, &stats->rx_csum_bad_offset,
2636 "Received checksum offloaded buffer with incorrect offset");
2637 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_failed",
2638 CTLFLAG_RD, &stats->rx_csum_failed,
2639 "Received buffer checksum offload failed");
2640 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_csum_offloaded",
2641 CTLFLAG_RD, &stats->rx_csum_offloaded,
2642 "Received buffer checksum offload succeeded");
2643 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "rx_task_rescheduled",
2644 CTLFLAG_RD, &stats->rx_task_rescheduled,
2645 "Times the receive interrupt task rescheduled itself");
2647 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_offloaded",
2648 CTLFLAG_RD, &stats->tx_csum_offloaded,
2649 "Offloaded checksum of transmitted buffer");
2650 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_offloaded",
2651 CTLFLAG_RD, &stats->tx_tso_offloaded,
2652 "Segmentation offload of transmitted buffer");
2653 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2654 CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
2655 "Aborted transmit of checksum offloaded buffer with unknown "
2657 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2658 CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
2659 "Aborted transmit of TSO buffer with unknown Ethernet type");
2660 SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "tx_task_rescheduled",
2661 CTLFLAG_RD, &stats->tx_task_rescheduled,
2662 "Times the transmit interrupt task rescheduled itself");
2666 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2669 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2673 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2676 virtqueue_disable_intr(sc->vtnet_rx_vq);
2680 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2683 #ifdef VTNET_TX_INTR_MODERATION
2686 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2691 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2694 virtqueue_disable_intr(sc->vtnet_tx_vq);