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 <machine/limits.h>
49 #include <net/ethernet.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_media.h>
55 #include <net/vlan/if_vlan_var.h>
56 #include <net/vlan/if_vlan_ether.h>
57 #include <net/ifq_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip6.h>
65 #include <netinet/udp.h>
66 #include <netinet/tcp.h>
68 #include <dev/virtual/virtio/virtio/virtio.h>
69 #include <dev/virtual/virtio/virtio/virtqueue.h>
71 #include "virtio_net.h"
72 #include "virtio_if.h"
74 struct vtnet_statistics {
75 uint64_t mbuf_alloc_failed;
77 uint64_t rx_frame_too_large;
78 uint64_t rx_enq_replacement_failed;
79 uint64_t rx_mergeable_failed;
80 uint64_t rx_csum_bad_ethtype;
81 uint64_t rx_csum_bad_ipproto;
82 uint64_t rx_csum_bad_offset;
83 uint64_t rx_csum_failed;
84 uint64_t rx_csum_offloaded;
85 uint64_t rx_task_rescheduled;
87 uint64_t tx_csum_offloaded;
88 uint64_t tx_tso_offloaded;
89 uint64_t tx_csum_bad_ethtype;
90 uint64_t tx_tso_bad_ethtype;
91 uint64_t tx_task_rescheduled;
96 struct ifnet *vtnet_ifp;
97 struct lwkt_serialize vtnet_slz;
100 #define VTNET_FLAG_LINK 0x0001
101 #define VTNET_FLAG_SUSPENDED 0x0002
102 #define VTNET_FLAG_MAC 0x0004
103 #define VTNET_FLAG_CTRL_VQ 0x0008
104 #define VTNET_FLAG_CTRL_RX 0x0010
105 #define VTNET_FLAG_CTRL_MAC 0x0020
106 #define VTNET_FLAG_VLAN_FILTER 0x0040
107 #define VTNET_FLAG_TSO_ECN 0x0080
108 #define VTNET_FLAG_MRG_RXBUFS 0x0100
109 #define VTNET_FLAG_LRO_NOMRG 0x0200
111 struct virtqueue *vtnet_rx_vq;
112 struct virtqueue *vtnet_tx_vq;
113 struct virtqueue *vtnet_ctrl_vq;
115 struct vtnet_tx_header *vtnet_txhdrarea;
116 uint32_t vtnet_txhdridx;
117 struct vtnet_mac_filter *vtnet_macfilter;
122 int vtnet_rx_process_limit;
123 int vtnet_rx_mbuf_size;
124 int vtnet_rx_mbuf_count;
126 int vtnet_watchdog_timer;
127 uint64_t vtnet_features;
129 struct task vtnet_cfgchg_task;
131 struct vtnet_statistics vtnet_stats;
133 struct callout vtnet_tick_ch;
135 eventhandler_tag vtnet_vlan_attach;
136 eventhandler_tag vtnet_vlan_detach;
138 struct ifmedia vtnet_media;
140 * Fake media type; the host does not provide us with
141 * any real media information.
143 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX)
144 char vtnet_hwaddr[ETHER_ADDR_LEN];
147 * During reset, the host's VLAN filtering table is lost. The
148 * array below is used to restore all the VLANs configured on
149 * this interface after a reset.
151 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32)
153 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
155 char vtnet_mtx_name[16];
159 * When mergeable buffers are not negotiated, the vtnet_rx_header structure
160 * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
161 * both keep the VirtIO header and the data non-contiguous and to keep the
162 * frame's payload 4 byte aligned.
164 * When mergeable buffers are negotiated, the host puts the VirtIO header in
165 * the beginning of the first mbuf's data.
167 #define VTNET_RX_HEADER_PAD 4
168 struct vtnet_rx_header {
169 struct virtio_net_hdr vrh_hdr;
170 char vrh_pad[VTNET_RX_HEADER_PAD];
174 * For each outgoing frame, the vtnet_tx_header below is allocated from
175 * the vtnet_tx_header_zone.
177 struct vtnet_tx_header {
179 struct virtio_net_hdr hdr;
180 struct virtio_net_hdr_mrg_rxbuf mhdr;
183 struct mbuf *vth_mbuf;
186 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
189 * The VirtIO specification does not place a limit on the number of MAC
190 * addresses the guest driver may request to be filtered. In practice,
191 * the host is constrained by available resources. To simplify this driver,
192 * impose a reasonably high limit of MAC addresses we will filter before
193 * falling back to promiscuous or all-multicast modes.
195 #define VTNET_MAX_MAC_ENTRIES 128
197 struct vtnet_mac_table {
199 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
202 struct vtnet_mac_filter {
203 struct vtnet_mac_table vmf_unicast;
204 uint32_t vmf_pad; /* Make tables non-contiguous. */
205 struct vtnet_mac_table vmf_multicast;
208 #define VTNET_WATCHDOG_TIMEOUT 5
209 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP)
211 /* Features desired/implemented by this driver. */
212 #define VTNET_FEATURES \
213 (VIRTIO_NET_F_MAC | \
214 VIRTIO_NET_F_STATUS | \
215 VIRTIO_NET_F_CTRL_VQ | \
216 VIRTIO_NET_F_CTRL_RX | \
217 VIRTIO_NET_F_CTRL_MAC_ADDR | \
218 VIRTIO_NET_F_CTRL_VLAN | \
219 VIRTIO_NET_F_CSUM | \
220 VIRTIO_NET_F_HOST_TSO4 | \
221 VIRTIO_NET_F_HOST_TSO6 | \
222 VIRTIO_NET_F_HOST_ECN | \
223 VIRTIO_NET_F_GUEST_CSUM | \
224 VIRTIO_NET_F_GUEST_TSO4 | \
225 VIRTIO_NET_F_GUEST_TSO6 | \
226 VIRTIO_NET_F_GUEST_ECN | \
227 VIRTIO_NET_F_MRG_RXBUF)
230 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
231 * frames larger than 1514 bytes. We do not yet support software LRO
234 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
235 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
237 #define VTNET_MAX_MTU 65536
238 #define VTNET_MAX_RX_SIZE 65550
241 * Used to preallocate the Vq indirect descriptors. The first segment
242 * is reserved for the header.
244 #define VTNET_MIN_RX_SEGS 2
245 #define VTNET_MAX_RX_SEGS 34
246 #define VTNET_MAX_TX_SEGS 34
248 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */
249 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */
250 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */
254 * Assert we can receive and transmit the maximum with regular
257 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
258 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
261 * Determine how many mbufs are in each receive buffer. For LRO without
262 * mergeable descriptors, we must allocate an mbuf chain large enough to
263 * hold both the vtnet_rx_header and the maximum receivable data.
265 #define VTNET_NEEDED_RX_MBUFS(_sc) \
266 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \
267 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \
268 (_sc)->vtnet_rx_mbuf_size)
270 static int vtnet_modevent(module_t, int, void *);
272 static int vtnet_probe(device_t);
273 static int vtnet_attach(device_t);
274 static int vtnet_detach(device_t);
275 static int vtnet_suspend(device_t);
276 static int vtnet_resume(device_t);
277 static int vtnet_shutdown(device_t);
278 static int vtnet_config_change(device_t);
280 static void vtnet_negotiate_features(struct vtnet_softc *);
281 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
282 static void vtnet_get_hwaddr(struct vtnet_softc *);
283 static void vtnet_set_hwaddr(struct vtnet_softc *);
284 static int vtnet_is_link_up(struct vtnet_softc *);
285 static void vtnet_update_link_status(struct vtnet_softc *);
287 static void vtnet_watchdog(struct vtnet_softc *);
289 static void vtnet_config_change_task(void *, int);
290 static int vtnet_setup_interface(struct vtnet_softc *);
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_virtio_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_update_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;
468 sc = device_get_softc(dev);
471 lwkt_serialize_init(&sc->vtnet_slz);
472 callout_init(&sc->vtnet_tick_ch);
474 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
476 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
477 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
479 vtnet_add_statistics(sc);
481 /* Register our feature descriptions. */
482 virtio_set_feature_desc(dev, vtnet_feature_desc);
483 vtnet_negotiate_features(sc);
485 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
486 /* This feature should always be negotiated. */
487 sc->vtnet_flags |= VTNET_FLAG_MAC;
490 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
491 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
492 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
494 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
497 sc->vtnet_rx_mbuf_size = MCLBYTES;
498 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
500 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
501 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
503 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
504 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
505 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
506 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
507 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
508 virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
509 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
512 /* Read (or generate) the MAC address for the adapter. */
513 vtnet_get_hwaddr(sc);
515 error = vtnet_alloc_virtqueues(sc);
517 device_printf(dev, "cannot allocate virtqueues\n");
521 error = vtnet_setup_interface(sc);
523 device_printf(dev, "cannot setup interface\n");
527 TASK_INIT(&sc->vtnet_cfgchg_task, 0, vtnet_config_change_task, sc);
529 error = virtio_setup_intr(dev, &sc->vtnet_slz);
531 device_printf(dev, "cannot setup virtqueue interrupts\n");
532 ether_ifdetach(sc->vtnet_ifp);
537 * Device defaults to promiscuous mode for backwards
538 * compatibility. Turn it off if possible.
540 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
541 lwkt_serialize_enter(&sc->vtnet_slz);
542 if (vtnet_set_promisc(sc, 0) != 0) {
543 sc->vtnet_ifp->if_flags |= IFF_PROMISC;
545 "cannot disable promiscuous mode\n");
547 lwkt_serialize_exit(&sc->vtnet_slz);
549 sc->vtnet_ifp->if_flags |= IFF_PROMISC;
559 vtnet_detach(device_t dev)
561 struct vtnet_softc *sc;
564 sc = device_get_softc(dev);
567 if (device_is_attached(dev)) {
568 lwkt_serialize_enter(&sc->vtnet_slz);
570 lwkt_serialize_exit(&sc->vtnet_slz);
572 callout_stop(&sc->vtnet_tick_ch);
573 taskqueue_drain(taskqueue_swi, &sc->vtnet_cfgchg_task);
578 if (sc->vtnet_vlan_attach != NULL) {
579 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
580 sc->vtnet_vlan_attach = NULL;
582 if (sc->vtnet_vlan_detach != NULL) {
583 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
584 sc->vtnet_vlan_detach = NULL;
589 sc->vtnet_ifp = NULL;
592 if (sc->vtnet_rx_vq != NULL)
593 vtnet_free_rx_mbufs(sc);
594 if (sc->vtnet_tx_vq != NULL)
595 vtnet_free_tx_mbufs(sc);
596 if (sc->vtnet_ctrl_vq != NULL)
597 vtnet_free_ctrl_vq(sc);
599 if (sc->vtnet_txhdrarea != NULL) {
600 contigfree(sc->vtnet_txhdrarea,
601 ((sc->vtnet_tx_size / 2) + 1) *
602 sizeof(struct vtnet_tx_header), M_VTNET);
603 sc->vtnet_txhdrarea = NULL;
605 if (sc->vtnet_macfilter != NULL) {
606 contigfree(sc->vtnet_macfilter,
607 sizeof(struct vtnet_mac_filter), M_DEVBUF);
608 sc->vtnet_macfilter = NULL;
611 ifmedia_removeall(&sc->vtnet_media);
617 vtnet_suspend(device_t dev)
619 struct vtnet_softc *sc;
621 sc = device_get_softc(dev);
623 lwkt_serialize_enter(&sc->vtnet_slz);
625 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
626 lwkt_serialize_exit(&sc->vtnet_slz);
632 vtnet_resume(device_t dev)
634 struct vtnet_softc *sc;
637 sc = device_get_softc(dev);
640 lwkt_serialize_enter(&sc->vtnet_slz);
641 if (ifp->if_flags & IFF_UP)
642 vtnet_init_locked(sc);
643 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
644 lwkt_serialize_exit(&sc->vtnet_slz);
650 vtnet_shutdown(device_t dev)
654 * Suspend already does all of what we need to
655 * do here; we just never expect to be resumed.
657 return (vtnet_suspend(dev));
661 vtnet_config_change(device_t dev)
663 struct vtnet_softc *sc;
665 sc = device_get_softc(dev);
667 taskqueue_enqueue(taskqueue_thread[mycpuid], &sc->vtnet_cfgchg_task);
673 vtnet_negotiate_features(struct vtnet_softc *sc)
676 uint64_t mask, features;
681 if (vtnet_csum_disable)
682 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
685 * TSO and LRO are only available when their corresponding checksum
686 * offload feature is also negotiated.
689 if (vtnet_csum_disable || vtnet_tso_disable)
690 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
691 VIRTIO_NET_F_HOST_ECN;
693 if (vtnet_csum_disable || vtnet_lro_disable)
694 mask |= VTNET_LRO_FEATURES;
696 features = VTNET_FEATURES & ~mask;
697 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
698 features |= VIRTIO_F_ANY_LAYOUT;
699 sc->vtnet_features = virtio_negotiate_features(dev, features);
701 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
702 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
704 * LRO without mergeable buffers requires special care. This
705 * is not ideal because every receive buffer must be large
706 * enough to hold the maximum TCP packet, the Ethernet header,
707 * and the header. This requires up to 34 descriptors with
708 * MCLBYTES clusters. If we do not have indirect descriptors,
709 * LRO is disabled since the virtqueue will not contain very
710 * many receive buffers.
712 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
714 "LRO disabled due to both mergeable buffers and "
715 "indirect descriptors not negotiated\n");
717 features &= ~VTNET_LRO_FEATURES;
719 virtio_negotiate_features(dev, features);
721 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
726 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
729 struct vq_alloc_info vq_info[3];
736 * Indirect descriptors are not needed for the Rx
737 * virtqueue when mergeable buffers are negotiated.
738 * The header is placed inline with the data, not
739 * in a separate descriptor, and mbuf clusters are
740 * always physically contiguous.
742 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
743 rxsegs = sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG ?
744 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
748 VQ_ALLOC_INFO_INIT(&vq_info[0], rxsegs,
749 vtnet_rx_vq_intr, sc, &sc->vtnet_rx_vq,
750 "%s receive", device_get_nameunit(dev));
752 VQ_ALLOC_INFO_INIT(&vq_info[1], VTNET_MAX_TX_SEGS,
753 vtnet_tx_vq_intr, sc, &sc->vtnet_tx_vq,
754 "%s transmit", device_get_nameunit(dev));
756 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
759 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, NULL, NULL,
760 &sc->vtnet_ctrl_vq, "%s control",
761 device_get_nameunit(dev));
764 return (virtio_alloc_virtqueues(dev, 0, nvqs, vq_info));
768 vtnet_setup_interface(struct vtnet_softc *sc)
776 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
778 device_printf(dev, "cannot allocate ifnet structure\n");
783 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
784 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
785 ifp->if_init = vtnet_init;
786 ifp->if_start = vtnet_start;
787 ifp->if_ioctl = vtnet_ioctl;
789 sc->vtnet_rx_size = virtqueue_size(sc->vtnet_rx_vq);
790 sc->vtnet_rx_process_limit = sc->vtnet_rx_size;
792 tx_size = virtqueue_size(sc->vtnet_tx_vq);
793 sc->vtnet_tx_size = tx_size;
794 sc->vtnet_txhdridx = 0;
795 sc->vtnet_txhdrarea = contigmalloc(
796 ((sc->vtnet_tx_size / 2) + 1) * sizeof(struct vtnet_tx_header),
797 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
798 if (sc->vtnet_txhdrarea == NULL) {
799 device_printf(dev, "cannot contigmalloc the tx headers\n");
802 sc->vtnet_macfilter = contigmalloc(
803 sizeof(struct vtnet_mac_filter),
804 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
805 if (sc->vtnet_macfilter == NULL) {
807 "cannot contigmalloc the mac filter table\n");
810 ifq_set_maxlen(&ifp->if_snd, tx_size - 1);
811 ifq_set_ready(&ifp->if_snd);
813 ether_ifattach(ifp, sc->vtnet_hwaddr, NULL);
815 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)){
816 //ifp->if_capabilities |= IFCAP_LINKSTATE;
817 kprintf("add dynamic link state\n");
820 /* Tell the upper layer(s) we support long frames. */
821 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
822 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
824 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
825 ifp->if_capabilities |= IFCAP_TXCSUM;
827 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
828 ifp->if_capabilities |= IFCAP_TSO4;
829 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
830 ifp->if_capabilities |= IFCAP_TSO6;
831 if (ifp->if_capabilities & IFCAP_TSO)
832 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
834 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
835 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
838 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM)) {
839 ifp->if_capabilities |= IFCAP_RXCSUM;
841 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
842 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
843 ifp->if_capabilities |= IFCAP_LRO;
846 if (ifp->if_capabilities & IFCAP_HWCSUM) {
848 * VirtIO does not support VLAN tagging, but we can fake
849 * it by inserting and removing the 802.1Q header during
850 * transmit and receive. We are then able to do checksum
851 * offloading of VLAN frames.
853 ifp->if_capabilities |=
854 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
857 ifp->if_capenable = ifp->if_capabilities;
860 * Capabilities after here are not enabled by default.
863 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
864 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
866 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
867 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
868 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
869 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
876 vtnet_set_hwaddr(struct vtnet_softc *sc)
882 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) &&
883 (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)) {
884 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
885 device_printf(dev, "unable to set MAC address\n");
886 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
887 virtio_write_device_config(dev,
888 offsetof(struct virtio_net_config, mac),
889 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
894 vtnet_get_hwaddr(struct vtnet_softc *sc)
900 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
902 * Generate a random locally administered unicast address.
904 * It would be nice to generate the same MAC address across
905 * reboots, but it seems all the hosts currently available
906 * support the MAC feature, so this isn't too important.
908 sc->vtnet_hwaddr[0] = 0xB2;
909 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
910 vtnet_set_hwaddr(sc);
914 virtio_read_device_config(dev,
915 offsetof(struct virtio_net_config, mac),
916 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
920 vtnet_is_link_up(struct vtnet_softc *sc)
929 ASSERT_SERIALIZED(&sc->vtnet_slz);
931 status = virtio_read_dev_config_2(dev,
932 offsetof(struct virtio_net_config, status));
934 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
938 vtnet_update_link_status(struct vtnet_softc *sc)
942 struct ifaltq_subque *ifsq;
947 ifsq = ifq_get_subq_default(&ifp->if_snd);
949 link = vtnet_is_link_up(sc);
951 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
952 sc->vtnet_flags |= VTNET_FLAG_LINK;
954 device_printf(dev, "Link is up\n");
955 ifp->if_link_state = LINK_STATE_UP;
956 if_link_state_change(ifp);
957 if (!ifsq_is_empty(ifsq))
958 vtnet_start_locked(ifp, ifsq);
959 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
960 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
962 device_printf(dev, "Link is down\n");
964 ifp->if_link_state = LINK_STATE_DOWN;
965 if_link_state_change(ifp);
971 vtnet_watchdog(struct vtnet_softc *sc)
977 #ifdef VTNET_TX_INTR_MODERATION
981 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
984 if_printf(ifp, "watchdog timeout -- resetting\n");
986 virtqueue_dump(sc->vtnet_tx_vq);
989 ifp->if_flags &= ~IFF_RUNNING;
990 vtnet_init_locked(sc);
995 vtnet_config_change_task(void *arg, int pending)
997 struct vtnet_softc *sc;
1001 lwkt_serialize_enter(&sc->vtnet_slz);
1002 vtnet_update_link_status(sc);
1003 lwkt_serialize_exit(&sc->vtnet_slz);
1007 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
1009 struct vtnet_softc *sc;
1011 int reinit, mask, error;
1014 ifr = (struct ifreq *) data;
1020 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
1022 else if (ifp->if_mtu != ifr->ifr_mtu) {
1023 lwkt_serialize_enter(&sc->vtnet_slz);
1024 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
1025 lwkt_serialize_exit(&sc->vtnet_slz);
1030 lwkt_serialize_enter(&sc->vtnet_slz);
1031 if ((ifp->if_flags & IFF_UP) == 0) {
1032 if (ifp->if_flags & IFF_RUNNING)
1034 } else if (ifp->if_flags & IFF_RUNNING) {
1035 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
1036 (IFF_PROMISC | IFF_ALLMULTI)) {
1037 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1038 vtnet_rx_filter(sc);
1043 vtnet_init_locked(sc);
1046 sc->vtnet_if_flags = ifp->if_flags;
1047 lwkt_serialize_exit(&sc->vtnet_slz);
1052 lwkt_serialize_enter(&sc->vtnet_slz);
1053 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
1054 (ifp->if_flags & IFF_RUNNING))
1055 vtnet_rx_filter_mac(sc);
1056 lwkt_serialize_exit(&sc->vtnet_slz);
1061 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1065 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1067 lwkt_serialize_enter(&sc->vtnet_slz);
1069 if (mask & IFCAP_TXCSUM) {
1070 ifp->if_capenable ^= IFCAP_TXCSUM;
1071 if (ifp->if_capenable & IFCAP_TXCSUM)
1072 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1074 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1077 if (mask & IFCAP_TSO4) {
1078 ifp->if_capenable ^= IFCAP_TSO4;
1079 if (ifp->if_capenable & IFCAP_TSO4)
1080 ifp->if_hwassist |= CSUM_TSO;
1082 ifp->if_hwassist &= ~CSUM_TSO;
1085 if (mask & IFCAP_RXCSUM) {
1086 ifp->if_capenable ^= IFCAP_RXCSUM;
1090 if (mask & IFCAP_LRO) {
1091 ifp->if_capenable ^= IFCAP_LRO;
1095 if (mask & IFCAP_VLAN_HWFILTER) {
1096 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1100 if (mask & IFCAP_VLAN_HWTSO)
1101 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1103 if (mask & IFCAP_VLAN_HWTAGGING)
1104 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1106 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1107 ifp->if_flags &= ~IFF_RUNNING;
1108 vtnet_init_locked(sc);
1110 //VLAN_CAPABILITIES(ifp);
1112 lwkt_serialize_exit(&sc->vtnet_slz);
1116 error = ether_ioctl(ifp, cmd, data);
1124 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1127 int new_frame_size, clsize;
1129 ifp = sc->vtnet_ifp;
1131 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1132 new_frame_size = sizeof(struct vtnet_rx_header) +
1133 sizeof(struct ether_vlan_header) + new_mtu;
1135 if (new_frame_size > MJUM9BYTES)
1138 if (new_frame_size <= MCLBYTES)
1141 clsize = MJUM9BYTES;
1143 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1144 sizeof(struct ether_vlan_header) + new_mtu;
1146 if (new_frame_size <= MCLBYTES)
1149 clsize = MJUMPAGESIZE;
1152 sc->vtnet_rx_mbuf_size = clsize;
1153 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1154 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1155 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1157 ifp->if_mtu = new_mtu;
1159 if (ifp->if_flags & IFF_RUNNING) {
1160 ifp->if_flags &= ~IFF_RUNNING;
1161 vtnet_init_locked(sc);
1168 vtnet_init_rx_vq(struct vtnet_softc *sc)
1170 struct virtqueue *vq;
1173 vq = sc->vtnet_rx_vq;
1177 while (!virtqueue_full(vq)) {
1178 if ((error = vtnet_newbuf(sc)) != 0)
1184 virtqueue_notify(vq, &sc->vtnet_slz);
1187 * EMSGSIZE signifies the virtqueue did not have enough
1188 * entries available to hold the last mbuf. This is not
1189 * an error. We should not get ENOSPC since we check if
1190 * the virtqueue is full before attempting to add a
1193 if (error == EMSGSIZE)
1201 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1203 struct virtqueue *vq;
1207 vq = sc->vtnet_rx_vq;
1210 while ((m = virtqueue_drain(vq, &last)) != NULL)
1213 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1217 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1219 struct virtqueue *vq;
1220 struct vtnet_tx_header *txhdr;
1223 vq = sc->vtnet_tx_vq;
1226 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1227 m_freem(txhdr->vth_mbuf);
1230 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1234 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1237 * The control virtqueue is only polled, therefore
1238 * it should already be empty.
1240 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1241 ("Ctrl Vq not empty"));
1244 static struct mbuf *
1245 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1247 struct mbuf *m_head, *m_tail, *m;
1250 clsize = sc->vtnet_rx_mbuf_size;
1252 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1253 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1254 m_head = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR );
1258 m_head->m_len = clsize;
1262 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1263 ("chained Rx mbuf requested without LRO_NOMRG"));
1265 for (i = 0; i < nbufs - 1; i++) {
1266 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1267 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1277 if (m_tailp != NULL)
1283 sc->vtnet_stats.mbuf_alloc_failed++;
1290 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1292 struct mbuf *m, *m_prev;
1293 struct mbuf *m_new, *m_tail;
1294 int len, clsize, nreplace, error;
1301 clsize = sc->vtnet_rx_mbuf_size;
1304 if (m->m_next != NULL)
1305 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1306 ("chained Rx mbuf without LRO_NOMRG"));
1309 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1310 * allocating an entire chain for each received frame. When
1311 * the received frame's length is less than that of the chain,
1312 * the unused mbufs are reassigned to the new chain.
1316 * Something is seriously wrong if we received
1317 * a frame larger than the mbuf chain. Drop it.
1320 sc->vtnet_stats.rx_frame_too_large++;
1324 KASSERT(m->m_len == clsize,
1325 ("mbuf length not expected cluster size: %d",
1328 m->m_len = MIN(m->m_len, len);
1336 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1337 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1338 ("too many replacement mbufs: %d/%d", nreplace,
1339 sc->vtnet_rx_mbuf_count));
1341 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1342 if (m_new == NULL) {
1343 m_prev->m_len = clsize;
1348 * Move unused mbufs, if any, from the original chain
1349 * onto the end of the new chain.
1351 if (m_prev->m_next != NULL) {
1352 m_tail->m_next = m_prev->m_next;
1353 m_prev->m_next = NULL;
1356 error = vtnet_enqueue_rxbuf(sc, m_new);
1359 * BAD! We could not enqueue the replacement mbuf chain. We
1360 * must restore the m0 chain to the original state if it was
1361 * modified so we can subsequently discard it.
1363 * NOTE: The replacement is suppose to be an identical copy
1364 * to the one just dequeued so this is an unexpected error.
1366 sc->vtnet_stats.rx_enq_replacement_failed++;
1368 if (m_tail->m_next != NULL) {
1369 m_prev->m_next = m_tail->m_next;
1370 m_tail->m_next = NULL;
1373 m_prev->m_len = clsize;
1381 vtnet_newbuf(struct vtnet_softc *sc)
1386 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1390 error = vtnet_enqueue_rxbuf(sc, m);
1398 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1400 struct virtqueue *vq;
1403 vq = sc->vtnet_rx_vq;
1405 while (--nbufs > 0) {
1406 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1408 vtnet_discard_rxbuf(sc, m);
1413 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1418 * Requeue the discarded mbuf. This should always be
1419 * successful since it was just dequeued.
1421 error = vtnet_enqueue_rxbuf(sc, m);
1422 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1426 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1429 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1430 struct vtnet_rx_header *rxhdr;
1431 struct virtio_net_hdr *hdr;
1435 ASSERT_SERIALIZED(&sc->vtnet_slz);
1436 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1437 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1439 sglist_init(&sg, VTNET_MAX_RX_SEGS, segs);
1441 mdata = mtod(m, uint8_t *);
1444 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1445 rxhdr = (struct vtnet_rx_header *) mdata;
1446 hdr = &rxhdr->vrh_hdr;
1447 offset += sizeof(struct vtnet_rx_header);
1449 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1450 KASSERT(error == 0, ("cannot add header to sglist"));
1453 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1457 if (m->m_next != NULL) {
1458 error = sglist_append_mbuf(&sg, m->m_next);
1463 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1467 vtnet_vlan_tag_remove(struct mbuf *m)
1469 struct ether_vlan_header *evl;
1471 evl = mtod(m, struct ether_vlan_header *);
1473 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1474 m->m_flags |= M_VLANTAG;
1476 /* Strip the 802.1Q header. */
1477 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1478 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1479 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1483 * Alternative method of doing receive checksum offloading. Rather
1484 * than parsing the received frame down to the IP header, use the
1485 * csum_offset to determine which CSUM_* flags are appropriate. We
1486 * can get by with doing this only because the checksum offsets are
1487 * unique for the things we care about.
1490 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1491 struct virtio_net_hdr *hdr)
1493 struct ether_header *eh;
1494 struct ether_vlan_header *evh;
1499 csum_len = hdr->csum_start + hdr->csum_offset;
1501 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1503 if (m->m_len < csum_len)
1506 eh = mtod(m, struct ether_header *);
1507 eth_type = ntohs(eh->ether_type);
1508 if (eth_type == ETHERTYPE_VLAN) {
1509 evh = mtod(m, struct ether_vlan_header *);
1510 eth_type = ntohs(evh->evl_proto);
1513 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1514 sc->vtnet_stats.rx_csum_bad_ethtype++;
1518 /* Use the offset to determine the appropriate CSUM_* flags. */
1519 switch (hdr->csum_offset) {
1520 case offsetof(struct udphdr, uh_sum):
1521 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1523 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1524 if (udp->uh_sum == 0)
1529 case offsetof(struct tcphdr, th_sum):
1530 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1531 m->m_pkthdr.csum_data = 0xFFFF;
1535 sc->vtnet_stats.rx_csum_bad_offset++;
1539 sc->vtnet_stats.rx_csum_offloaded++;
1545 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1548 struct virtqueue *vq;
1549 struct mbuf *m, *m_tail;
1552 ifp = sc->vtnet_ifp;
1553 vq = sc->vtnet_rx_vq;
1556 while (--nbufs > 0) {
1557 m = virtqueue_dequeue(vq, &len);
1563 if (vtnet_newbuf(sc) != 0) {
1565 vtnet_discard_rxbuf(sc, m);
1567 vtnet_discard_merged_rxbuf(sc, nbufs);
1575 m->m_flags &= ~M_PKTHDR;
1577 m_head->m_pkthdr.len += len;
1585 sc->vtnet_stats.rx_mergeable_failed++;
1592 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1594 struct virtio_net_hdr lhdr;
1596 struct virtqueue *vq;
1598 struct ether_header *eh;
1599 struct virtio_net_hdr *hdr;
1600 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1601 int len, deq, nbufs, adjsz, rx_npkts;
1603 ifp = sc->vtnet_ifp;
1604 vq = sc->vtnet_rx_vq;
1609 ASSERT_SERIALIZED(&sc->vtnet_slz);
1611 while (--count >= 0) {
1612 m = virtqueue_dequeue(vq, &len);
1617 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1619 vtnet_discard_rxbuf(sc, m);
1623 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1625 adjsz = sizeof(struct vtnet_rx_header);
1627 * Account for our pad between the header and
1628 * the actual start of the frame.
1630 len += VTNET_RX_HEADER_PAD;
1632 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1633 nbufs = mhdr->num_buffers;
1634 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1637 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1639 vtnet_discard_rxbuf(sc, m);
1641 vtnet_discard_merged_rxbuf(sc, nbufs);
1645 m->m_pkthdr.len = len;
1646 m->m_pkthdr.rcvif = ifp;
1647 m->m_pkthdr.csum_flags = 0;
1650 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1657 * Save copy of header before we strip it. For both mergeable
1658 * and non-mergeable, the VirtIO header is placed first in the
1659 * mbuf's data. We no longer need num_buffers, so always use a
1662 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1665 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1666 eh = mtod(m, struct ether_header *);
1667 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1668 vtnet_vlan_tag_remove(m);
1671 * With the 802.1Q header removed, update the
1672 * checksum starting location accordingly.
1674 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1676 ETHER_VLAN_ENCAP_LEN;
1680 if (ifp->if_capenable & IFCAP_RXCSUM &&
1681 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1682 if (vtnet_rx_csum(sc, m, hdr) != 0)
1683 sc->vtnet_stats.rx_csum_failed++;
1686 lwkt_serialize_exit(&sc->vtnet_slz);
1688 ifp->if_input(ifp, m, NULL, -1);
1689 lwkt_serialize_enter(&sc->vtnet_slz);
1692 * The interface may have been stopped while we were
1693 * passing the packet up the network stack.
1695 if ((ifp->if_flags & IFF_RUNNING) == 0)
1699 virtqueue_notify(vq, &sc->vtnet_slz);
1701 if (rx_npktsp != NULL)
1702 *rx_npktsp = rx_npkts;
1704 return (count > 0 ? 0 : EAGAIN);
1708 vtnet_rx_intr_task(void *arg)
1710 struct vtnet_softc *sc;
1715 ifp = sc->vtnet_ifp;
1718 // lwkt_serialize_enter(&sc->vtnet_slz);
1720 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1721 vtnet_enable_rx_intr(sc);
1722 // lwkt_serialize_exit(&sc->vtnet_slz);
1726 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1727 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1728 vtnet_disable_rx_intr(sc);
1732 // lwkt_serialize_exit(&sc->vtnet_slz);
1735 sc->vtnet_stats.rx_task_rescheduled++;
1741 vtnet_rx_vq_intr(void *xsc)
1743 struct vtnet_softc *sc;
1747 vtnet_disable_rx_intr(sc);
1748 vtnet_rx_intr_task(sc);
1754 vtnet_txeof(struct vtnet_softc *sc)
1756 struct virtqueue *vq;
1758 struct vtnet_tx_header *txhdr;
1761 vq = sc->vtnet_tx_vq;
1762 ifp = sc->vtnet_ifp;
1765 ASSERT_SERIALIZED(&sc->vtnet_slz);
1767 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1770 m_freem(txhdr->vth_mbuf);
1774 ifq_clr_oactive(&ifp->if_snd);
1775 if (virtqueue_empty(vq))
1776 sc->vtnet_watchdog_timer = 0;
1780 static struct mbuf *
1781 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1782 struct virtio_net_hdr *hdr)
1785 struct ether_header *eh;
1786 struct ether_vlan_header *evh;
1788 struct ip6_hdr *ip6;
1791 uint16_t eth_type, csum_start;
1792 uint8_t ip_proto, gso_type;
1794 ifp = sc->vtnet_ifp;
1797 ip_offset = sizeof(struct ether_header);
1798 if (m->m_len < ip_offset) {
1799 if ((m = m_pullup(m, ip_offset)) == NULL)
1803 eh = mtod(m, struct ether_header *);
1804 eth_type = ntohs(eh->ether_type);
1805 if (eth_type == ETHERTYPE_VLAN) {
1806 ip_offset = sizeof(struct ether_vlan_header);
1807 if (m->m_len < ip_offset) {
1808 if ((m = m_pullup(m, ip_offset)) == NULL)
1811 evh = mtod(m, struct ether_vlan_header *);
1812 eth_type = ntohs(evh->evl_proto);
1817 if (m->m_len < ip_offset + sizeof(struct ip)) {
1818 m = m_pullup(m, ip_offset + sizeof(struct ip));
1823 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1824 ip_proto = ip->ip_p;
1825 csum_start = ip_offset + (ip->ip_hl << 2);
1826 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1829 case ETHERTYPE_IPV6:
1830 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1831 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1836 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1838 * XXX Assume no extension headers are present. Presently,
1839 * this will always be true in the case of TSO, and FreeBSD
1840 * does not perform checksum offloading of IPv6 yet.
1842 ip_proto = ip6->ip6_nxt;
1843 csum_start = ip_offset + sizeof(struct ip6_hdr);
1844 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1851 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1852 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1853 hdr->csum_start = csum_start;
1854 hdr->csum_offset = m->m_pkthdr.csum_data;
1856 sc->vtnet_stats.tx_csum_offloaded++;
1859 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1860 if (ip_proto != IPPROTO_TCP)
1863 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1864 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1869 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1870 hdr->gso_type = gso_type;
1871 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1872 hdr->gso_size = m->m_pkthdr.tso_segsz;
1874 if (tcp->th_flags & TH_CWR) {
1876 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1877 * ECN support is only configurable globally with the
1878 * net.inet.tcp.ecn.enable sysctl knob.
1880 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1881 if_printf(ifp, "TSO with ECN not supported "
1887 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1890 sc->vtnet_stats.tx_tso_offloaded++;
1897 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1898 struct vtnet_tx_header *txhdr)
1901 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1902 struct virtqueue *vq;
1904 int collapsed, error;
1906 vq = sc->vtnet_tx_vq;
1910 sglist_init(&sg, VTNET_MAX_TX_SEGS, segs);
1911 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1912 KASSERT(error == 0 && sg.sg_nseg == 1,
1913 ("cannot add header to sglist"));
1916 error = sglist_append_mbuf(&sg, m);
1921 //m = m_collapse(m, M_NOWAIT, VTNET_MAX_TX_SEGS - 1);
1922 m = m_defrag(m, M_NOWAIT);
1931 txhdr->vth_mbuf = m;
1933 return (virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0));
1942 static struct mbuf *
1943 vtnet_vlan_tag_insert(struct mbuf *m)
1946 struct ether_vlan_header *evl;
1948 if (M_WRITABLE(m) == 0) {
1949 n = m_dup(m, M_NOWAIT);
1951 if ((m = n) == NULL)
1955 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1958 if (m->m_len < sizeof(struct ether_vlan_header)) {
1959 m = m_pullup(m, sizeof(struct ether_vlan_header));
1964 /* Insert 802.1Q header into the existing Ethernet header. */
1965 evl = mtod(m, struct ether_vlan_header *);
1966 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1967 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1968 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1969 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1970 m->m_flags &= ~M_VLANTAG;
1976 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1978 struct vtnet_tx_header *txhdr;
1979 struct virtio_net_hdr *hdr;
1983 txhdr = &sc->vtnet_txhdrarea[sc->vtnet_txhdridx];
1984 memset(txhdr, 0, sizeof(struct vtnet_tx_header));
1987 * Always use the non-mergeable header to simplify things. When
1988 * the mergeable feature is negotiated, the num_buffers field
1989 * must be set to zero. We use vtnet_hdr_size later to enqueue
1990 * the correct header size to the host.
1992 hdr = &txhdr->vth_uhdr.hdr;
1997 if (m->m_flags & M_VLANTAG) {
1998 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1999 m = vtnet_vlan_tag_insert(m);
2000 if ((*m_head = m) == NULL)
2002 m->m_flags &= ~M_VLANTAG;
2005 if (m->m_pkthdr.csum_flags != 0) {
2006 m = vtnet_tx_offload(sc, m, hdr);
2007 if ((*m_head = m) == NULL)
2011 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
2013 sc->vtnet_txhdridx =
2014 (sc->vtnet_txhdridx + 1) % ((sc->vtnet_tx_size / 2) + 1);
2020 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2022 struct vtnet_softc *sc;
2026 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2027 lwkt_serialize_enter(&sc->vtnet_slz);
2028 vtnet_start_locked(ifp, ifsq);
2029 lwkt_serialize_exit(&sc->vtnet_slz);
2033 vtnet_start_locked(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2035 struct vtnet_softc *sc;
2036 struct virtqueue *vq;
2041 vq = sc->vtnet_tx_vq;
2044 ASSERT_SERIALIZED(&sc->vtnet_slz);
2046 if ((ifp->if_flags & (IFF_RUNNING)) !=
2047 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
2050 #ifdef VTNET_TX_INTR_MODERATION
2051 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
2055 while (!ifsq_is_empty(ifsq)) {
2056 if (virtqueue_full(vq)) {
2057 ifq_set_oactive(&ifp->if_snd);
2061 m0 = ifq_dequeue(&ifp->if_snd);
2065 if (vtnet_encap(sc, &m0) != 0) {
2068 ifq_prepend(&ifp->if_snd, m0);
2069 ifq_set_oactive(&ifp->if_snd);
2074 ETHER_BPF_MTAP(ifp, m0);
2078 virtqueue_notify(vq, &sc->vtnet_slz);
2079 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
2084 vtnet_tick(void *xsc)
2086 struct vtnet_softc *sc;
2091 ASSERT_SERIALIZED(&sc->vtnet_slz);
2093 virtqueue_dump(sc->vtnet_rx_vq);
2094 virtqueue_dump(sc->vtnet_tx_vq);
2098 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2103 vtnet_tx_intr_task(void *arg)
2105 struct vtnet_softc *sc;
2107 struct ifaltq_subque *ifsq;
2110 ifp = sc->vtnet_ifp;
2111 ifsq = ifq_get_subq_default(&ifp->if_snd);
2114 // lwkt_serialize_enter(&sc->vtnet_slz);
2116 if ((ifp->if_flags & IFF_RUNNING) == 0) {
2117 vtnet_enable_tx_intr(sc);
2118 // lwkt_serialize_exit(&sc->vtnet_slz);
2124 if (!ifsq_is_empty(ifsq))
2125 vtnet_start_locked(ifp, ifsq);
2127 if (vtnet_enable_tx_intr(sc) != 0) {
2128 vtnet_disable_tx_intr(sc);
2129 sc->vtnet_stats.tx_task_rescheduled++;
2130 // lwkt_serialize_exit(&sc->vtnet_slz);
2134 // lwkt_serialize_exit(&sc->vtnet_slz);
2138 vtnet_tx_vq_intr(void *xsc)
2140 struct vtnet_softc *sc;
2144 vtnet_disable_tx_intr(sc);
2145 vtnet_tx_intr_task(sc);
2151 vtnet_stop(struct vtnet_softc *sc)
2156 dev = sc->vtnet_dev;
2157 ifp = sc->vtnet_ifp;
2159 ASSERT_SERIALIZED(&sc->vtnet_slz);
2161 sc->vtnet_watchdog_timer = 0;
2162 callout_stop(&sc->vtnet_tick_ch);
2163 ifq_clr_oactive(&ifp->if_snd);
2164 ifp->if_flags &= ~(IFF_RUNNING);
2166 vtnet_disable_rx_intr(sc);
2167 vtnet_disable_tx_intr(sc);
2170 * Stop the host VirtIO adapter. Note this will reset the host
2171 * adapter's state back to the pre-initialized state, so in
2172 * order to make the device usable again, we must drive it
2173 * through virtio_reinit() and virtio_reinit_complete().
2177 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2179 vtnet_free_rx_mbufs(sc);
2180 vtnet_free_tx_mbufs(sc);
2184 vtnet_virtio_reinit(struct vtnet_softc *sc)
2191 dev = sc->vtnet_dev;
2192 ifp = sc->vtnet_ifp;
2193 features = sc->vtnet_features;
2196 * Re-negotiate with the host, removing any disabled receive
2197 * features. Transmit features are disabled only on our side
2198 * via if_capenable and if_hwassist.
2201 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2202 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2203 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2206 if (ifp->if_capabilities & IFCAP_LRO) {
2207 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2208 features &= ~VTNET_LRO_FEATURES;
2211 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2212 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2213 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2216 error = virtio_reinit(dev, features);
2218 device_printf(dev, "virtio reinit error %d\n", error);
2224 vtnet_init_locked(struct vtnet_softc *sc)
2230 dev = sc->vtnet_dev;
2231 ifp = sc->vtnet_ifp;
2233 ASSERT_SERIALIZED(&sc->vtnet_slz);
2235 if (ifp->if_flags & IFF_RUNNING)
2238 /* Stop host's adapter, cancel any pending I/O. */
2241 /* Reinitialize the host device. */
2242 error = vtnet_virtio_reinit(sc);
2245 "reinitialization failed, stopping device...\n");
2250 /* Update host with assigned MAC address. */
2251 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2252 vtnet_set_hwaddr(sc);
2254 ifp->if_hwassist = 0;
2255 if (ifp->if_capenable & IFCAP_TXCSUM)
2256 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2257 if (ifp->if_capenable & IFCAP_TSO4)
2258 ifp->if_hwassist |= CSUM_TSO;
2260 error = vtnet_init_rx_vq(sc);
2263 "cannot allocate mbufs for Rx virtqueue\n");
2268 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2269 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2270 /* Restore promiscuous and all-multicast modes. */
2271 vtnet_rx_filter(sc);
2273 /* Restore filtered MAC addresses. */
2274 vtnet_rx_filter_mac(sc);
2277 /* Restore VLAN filters. */
2278 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2279 vtnet_rx_filter_vlan(sc);
2283 vtnet_enable_rx_intr(sc);
2284 vtnet_enable_tx_intr(sc);
2287 ifp->if_flags |= IFF_RUNNING;
2288 ifq_clr_oactive(&ifp->if_snd);
2290 virtio_reinit_complete(dev);
2292 vtnet_update_link_status(sc);
2293 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
2297 vtnet_init(void *xsc)
2299 struct vtnet_softc *sc;
2303 lwkt_serialize_enter(&sc->vtnet_slz);
2304 vtnet_init_locked(sc);
2305 lwkt_serialize_exit(&sc->vtnet_slz);
2309 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2310 struct sglist *sg, int readable, int writable)
2312 struct virtqueue *vq;
2315 vq = sc->vtnet_ctrl_vq;
2317 ASSERT_SERIALIZED(&sc->vtnet_slz);
2318 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2319 ("no control virtqueue"));
2320 KASSERT(virtqueue_empty(vq),
2321 ("control command already enqueued"));
2323 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2326 virtqueue_notify(vq, &sc->vtnet_slz);
2329 * Poll until the command is complete. Previously, we would
2330 * sleep until the control virtqueue interrupt handler woke
2331 * us up, but dropping the VTNET_MTX leads to serialization
2334 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2335 * vectors. Two of those vectors are needed for the Rx and Tx
2336 * virtqueues. We do not support sharing both a Vq and config
2337 * changed notification on the same MSIX vector.
2339 c = virtqueue_poll(vq, NULL);
2340 KASSERT(c == cookie, ("unexpected control command response"));
2344 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
2347 struct virtio_net_ctrl_hdr hdr __aligned(2);
2349 char aligned_hwaddr[ETHER_ADDR_LEN] __aligned(8);
2353 struct sglist_seg segs[3];
2357 s.hdr.class = VIRTIO_NET_CTRL_MAC;
2358 s.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
2359 s.ack = VIRTIO_NET_ERR;
2361 /* Copy the mac address into physically contiguous memory */
2362 memcpy(s.aligned_hwaddr, hwaddr, ETHER_ADDR_LEN);
2364 sglist_init(&sg, 3, segs);
2366 error |= sglist_append(&sg, &s.hdr,
2367 sizeof(struct virtio_net_ctrl_hdr));
2368 error |= sglist_append(&sg, s.aligned_hwaddr, ETHER_ADDR_LEN);
2369 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2370 KASSERT(error == 0 && sg.sg_nseg == 3,
2371 ("%s: error %d adding set MAC msg to sglist", __func__, error));
2373 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2375 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2379 vtnet_rx_filter(struct vtnet_softc *sc)
2384 dev = sc->vtnet_dev;
2385 ifp = sc->vtnet_ifp;
2387 ASSERT_SERIALIZED(&sc->vtnet_slz);
2388 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2389 ("CTRL_RX feature not negotiated"));
2391 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2392 device_printf(dev, "cannot %s promiscuous mode\n",
2393 ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
2395 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2396 device_printf(dev, "cannot %s all-multicast mode\n",
2397 ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
2401 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2403 struct sglist_seg segs[3];
2406 struct virtio_net_ctrl_hdr hdr __aligned(2);
2414 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2415 ("%s: CTRL_RX feature not negotiated", __func__));
2417 s.hdr.class = VIRTIO_NET_CTRL_RX;
2420 s.ack = VIRTIO_NET_ERR;
2422 sglist_init(&sg, 3, segs);
2424 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2425 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
2426 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2427 KASSERT(error == 0 && sg.sg_nseg == 3,
2428 ("%s: error %d adding Rx message to sglist", __func__, error));
2430 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2432 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2436 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2439 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2443 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2446 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2450 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2452 struct virtio_net_ctrl_hdr hdr __aligned(2);
2453 struct vtnet_mac_filter *filter;
2454 struct sglist_seg segs[4];
2458 struct ifaddr_container *ifac;
2459 struct ifmultiaddr *ifma;
2460 int ucnt, mcnt, promisc, allmulti, error;
2463 ifp = sc->vtnet_ifp;
2469 ASSERT_SERIALIZED(&sc->vtnet_slz);
2470 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2471 ("%s: CTRL_RX feature not negotiated", __func__));
2473 /* Use the MAC filtering table allocated in vtnet_attach. */
2474 filter = sc->vtnet_macfilter;
2475 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2477 /* Unicast MAC addresses: */
2478 //if_addr_rlock(ifp);
2479 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2481 if (ifa->ifa_addr->sa_family != AF_LINK)
2483 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2484 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
2486 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
2491 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2492 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2495 //if_addr_runlock(ifp);
2498 filter->vmf_unicast.nentries = 0;
2499 if_printf(ifp, "more than %d MAC addresses assigned, "
2500 "falling back to promiscuous mode\n",
2501 VTNET_MAX_MAC_ENTRIES);
2503 filter->vmf_unicast.nentries = ucnt;
2505 /* Multicast MAC addresses: */
2506 //if_maddr_rlock(ifp);
2507 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2508 if (ifma->ifma_addr->sa_family != AF_LINK)
2510 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
2515 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2516 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2519 //if_maddr_runlock(ifp);
2521 if (allmulti != 0) {
2522 filter->vmf_multicast.nentries = 0;
2523 if_printf(ifp, "more than %d multicast MAC addresses "
2524 "assigned, falling back to all-multicast mode\n",
2525 VTNET_MAX_MAC_ENTRIES);
2527 filter->vmf_multicast.nentries = mcnt;
2529 if (promisc != 0 && allmulti != 0)
2532 hdr.class = VIRTIO_NET_CTRL_MAC;
2533 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2534 ack = VIRTIO_NET_ERR;
2536 sglist_init(&sg, 4, segs);
2538 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2539 error |= sglist_append(&sg, &filter->vmf_unicast,
2540 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
2541 error |= sglist_append(&sg, &filter->vmf_multicast,
2542 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
2543 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2544 KASSERT(error == 0 && sg.sg_nseg == 4,
2545 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
2547 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2549 if (ack != VIRTIO_NET_OK)
2550 if_printf(ifp, "error setting host MAC filter table\n");
2553 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
2554 if_printf(ifp, "cannot enable promiscuous mode\n");
2555 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
2556 if_printf(ifp, "cannot enable all-multicast mode\n");
2560 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2562 struct sglist_seg segs[3];
2565 struct virtio_net_ctrl_hdr hdr __aligned(2);
2573 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
2574 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2576 s.ack = VIRTIO_NET_ERR;
2578 sglist_init(&sg, 3, segs);
2580 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2581 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
2582 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2583 KASSERT(error == 0 && sg.sg_nseg == 3,
2584 ("%s: error %d adding VLAN message to sglist", __func__, error));
2586 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2588 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2592 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2598 ASSERT_SERIALIZED(&sc->vtnet_slz);
2599 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2600 ("%s: VLAN_FILTER feature not negotiated", __func__));
2602 nvlans = sc->vtnet_nvlans;
2604 /* Enable the filter for each configured VLAN. */
2605 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2606 w = sc->vtnet_vlan_shadow[i];
2607 while ((bit = ffs(w) - 1) != -1) {
2609 tag = sizeof(w) * CHAR_BIT * i + bit;
2612 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
2613 device_printf(sc->vtnet_dev,
2614 "cannot enable VLAN %d filter\n", tag);
2619 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2623 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2628 ifp = sc->vtnet_ifp;
2629 idx = (tag >> 5) & 0x7F;
2632 if (tag == 0 || tag > 4095)
2635 lwkt_serialize_enter(&sc->vtnet_slz);
2637 /* Update shadow VLAN table. */
2640 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2643 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2646 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
2647 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2648 device_printf(sc->vtnet_dev,
2649 "cannot %s VLAN %d %s the host filter table\n",
2650 add ? "add" : "remove", tag, add ? "to" : "from");
2653 lwkt_serialize_exit(&sc->vtnet_slz);
2657 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2660 if (ifp->if_softc != arg)
2663 vtnet_update_vlan_filter(arg, 1, tag);
2667 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2670 if (ifp->if_softc != arg)
2673 vtnet_update_vlan_filter(arg, 0, tag);
2677 vtnet_ifmedia_upd(struct ifnet *ifp)
2679 struct vtnet_softc *sc;
2680 struct ifmedia *ifm;
2683 ifm = &sc->vtnet_media;
2685 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2692 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2694 struct vtnet_softc *sc;
2698 ifmr->ifm_status = IFM_AVALID;
2699 ifmr->ifm_active = IFM_ETHER;
2701 lwkt_serialize_enter(&sc->vtnet_slz);
2702 if (vtnet_is_link_up(sc) != 0) {
2703 ifmr->ifm_status |= IFM_ACTIVE;
2704 ifmr->ifm_active |= VTNET_MEDIATYPE;
2706 ifmr->ifm_active |= IFM_NONE;
2707 lwkt_serialize_exit(&sc->vtnet_slz);
2711 vtnet_add_statistics(struct vtnet_softc *sc)
2714 struct vtnet_statistics *stats;
2715 struct sysctl_ctx_list *ctx;
2716 struct sysctl_oid *tree;
2717 struct sysctl_oid_list *child;
2719 dev = sc->vtnet_dev;
2720 stats = &sc->vtnet_stats;
2721 ctx = device_get_sysctl_ctx(dev);
2722 tree = device_get_sysctl_tree(dev);
2723 child = SYSCTL_CHILDREN(tree);
2725 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2726 CTLFLAG_RD, &stats->mbuf_alloc_failed, 0,
2727 "Mbuf cluster allocation failures");
2729 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
2730 CTLFLAG_RD, &stats->rx_frame_too_large, 0,
2731 "Received frame larger than the mbuf chain");
2732 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2733 CTLFLAG_RD, &stats->rx_enq_replacement_failed, 0,
2734 "Enqueuing the replacement receive mbuf failed");
2735 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
2736 CTLFLAG_RD, &stats->rx_mergeable_failed, 0,
2737 "Mergeable buffers receive failures");
2738 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2739 CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 0,
2740 "Received checksum offloaded buffer with unsupported "
2742 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2743 CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 0,
2744 "Received checksum offloaded buffer with incorrect IP protocol");
2745 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2746 CTLFLAG_RD, &stats->rx_csum_bad_offset, 0,
2747 "Received checksum offloaded buffer with incorrect offset");
2748 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
2749 CTLFLAG_RD, &stats->rx_csum_failed, 0,
2750 "Received buffer checksum offload failed");
2751 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
2752 CTLFLAG_RD, &stats->rx_csum_offloaded, 0,
2753 "Received buffer checksum offload succeeded");
2754 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
2755 CTLFLAG_RD, &stats->rx_task_rescheduled, 0,
2756 "Times the receive interrupt task rescheduled itself");
2758 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2759 CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 0,
2760 "Aborted transmit of checksum offloaded buffer with unknown "
2762 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2763 CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 0,
2764 "Aborted transmit of TSO buffer with unknown Ethernet type");
2765 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
2766 CTLFLAG_RD, &stats->tx_csum_offloaded, 0,
2767 "Offloaded checksum of transmitted buffer");
2768 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
2769 CTLFLAG_RD, &stats->tx_tso_offloaded, 0,
2770 "Segmentation offload of transmitted buffer");
2771 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
2772 CTLFLAG_RD, &stats->tx_task_rescheduled, 0,
2773 "Times the transmit interrupt task rescheduled itself");
2777 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2780 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2784 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2787 virtqueue_disable_intr(sc->vtnet_rx_vq);
2791 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2794 #ifdef VTNET_TX_INTR_MODERATION
2797 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2802 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2805 virtqueue_disable_intr(sc->vtnet_tx_vq);