xref: /dragonfly/sys/dev/virtual/virtio/net/if_vtnet.c (revision abf903a5)
1 /*-
2  * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26 
27 /* Driver for VirtIO network devices. */
28 
29 #include "opt_ifpoll.h"
30 
31 #include <sys/cdefs.h>
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sockio.h>
37 #include <sys/mbuf.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/taskqueue.h>
43 #include <sys/random.h>
44 #include <sys/sglist.h>
45 #include <sys/serialize.h>
46 #include <sys/bus.h>
47 #include <sys/rman.h>
48 
49 #include <machine/limits.h>
50 
51 #include <net/ethernet.h>
52 #include <net/if.h>
53 #include <net/if_arp.h>
54 #include <net/if_dl.h>
55 #include <net/if_types.h>
56 #include <net/if_media.h>
57 #include <net/vlan/if_vlan_var.h>
58 #include <net/vlan/if_vlan_ether.h>
59 #include <net/if_poll.h>
60 #include <net/ifq_var.h>
61 
62 #include <net/bpf.h>
63 
64 #include <netinet/in_systm.h>
65 #include <netinet/in.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip6.h>
68 #include <netinet/udp.h>
69 #include <netinet/tcp.h>
70 
71 #include <dev/virtual/virtio/virtio/virtio.h>
72 #include <dev/virtual/virtio/virtio/virtqueue.h>
73 #include <dev/virtual/virtio/net/virtio_net.h>
74 #include <dev/virtual/virtio/net/if_vtnetvar.h>
75 
76 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
77 
78 static int	vtnet_probe(device_t);
79 static int	vtnet_attach(device_t);
80 static int	vtnet_detach(device_t);
81 static int	vtnet_suspend(device_t);
82 static int	vtnet_resume(device_t);
83 static int	vtnet_shutdown(device_t);
84 
85 static void	vtnet_negotiate_features(struct vtnet_softc *);
86 #ifdef IFPOLL_ENABLE
87 static void	vtnet_npoll(struct ifnet *, struct ifpoll_info *);
88 static void	vtnet_npoll_status(struct ifnet *);
89 static void	vtnet_npoll_rx(struct ifnet *, void *, int);
90 static void	vtnet_npoll_tx(struct ifnet *, void *, int);
91 #endif
92 static void	vtnet_serialize(struct ifnet *, enum ifnet_serialize);
93 static void	vtnet_deserialize(struct ifnet *, enum ifnet_serialize);
94 static int	vtnet_tryserialize(struct ifnet *, enum ifnet_serialize);
95 #ifdef INVARIANTS
96 static void	vtnet_serialize_assert(struct ifnet *, enum ifnet_serialize,
97 		    boolean_t);
98 #endif  /* INVARIANTS */
99 static int	vtnet_alloc_intrs(struct vtnet_softc *);
100 static int	vtnet_alloc_virtqueues(struct vtnet_softc *);
101 static int	vtnet_bind_intrs(struct vtnet_softc *);
102 static void	vtnet_get_hwaddr(struct vtnet_softc *);
103 static void	vtnet_set_hwaddr(struct vtnet_softc *);
104 static int	vtnet_is_link_up(struct vtnet_softc *);
105 static void	vtnet_update_link_status(struct vtnet_softc *);
106 static void	vtnet_watchdog(struct ifaltq_subque *);
107 static int	vtnet_setup_interface(struct vtnet_softc *);
108 static int	vtnet_change_mtu(struct vtnet_softc *, int);
109 static int	vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
110 
111 static int	vtnet_init_rx_vq(struct vtnet_softc *);
112 static void	vtnet_free_rx_mbufs(struct vtnet_softc *);
113 static void	vtnet_free_tx_mbufs(struct vtnet_softc *);
114 static void	vtnet_free_ctrl_vq(struct vtnet_softc *);
115 
116 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
117 		    struct mbuf **);
118 static int	vtnet_replace_rxbuf(struct vtnet_softc *,
119 		    struct mbuf *, int);
120 static int	vtnet_newbuf(struct vtnet_softc *);
121 static void	vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
122 static void	vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
123 static int	vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
124 static void	vtnet_vlan_tag_remove(struct mbuf *);
125 static int	vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
126 		    struct virtio_net_hdr *);
127 static int	vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
128 static int	vtnet_rxeof(struct vtnet_softc *, int, int *);
129 static void	vtnet_rx_msix_intr(void *);
130 static void	vtnet_rx_vq_intr(void *);
131 
132 static void	vtnet_enqueue_txhdr(struct vtnet_softc *,
133 		    struct vtnet_tx_header *);
134 static void	vtnet_txeof(struct vtnet_softc *);
135 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
136 		    struct virtio_net_hdr *);
137 static int	vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
138 		    struct vtnet_tx_header *);
139 static int	vtnet_encap(struct vtnet_softc *, struct mbuf **);
140 static void	vtnet_start(struct ifnet *, struct ifaltq_subque *);
141 
142 static void	vtnet_config_intr(void *);
143 static void	vtnet_tx_msix_intr(void *);
144 static void	vtnet_tx_vq_intr(void *);
145 
146 static void	vtnet_stop(struct vtnet_softc *);
147 static int	vtnet_virtio_reinit(struct vtnet_softc *);
148 static void	vtnet_init(void *);
149 
150 static void	vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
151 		    struct sglist *, int, int);
152 
153 static int	vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
154 static int	vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
155 static int	vtnet_set_promisc(struct vtnet_softc *, int);
156 static int	vtnet_set_allmulti(struct vtnet_softc *, int);
157 static void	vtnet_rx_filter(struct vtnet_softc *sc);
158 static void	vtnet_rx_filter_mac(struct vtnet_softc *);
159 
160 static int	vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
161 static void	vtnet_rx_filter_vlan(struct vtnet_softc *);
162 static void	vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t);
163 static void	vtnet_register_vlan(void *, struct ifnet *, uint16_t);
164 static void	vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
165 
166 static int	vtnet_ifmedia_upd(struct ifnet *);
167 static void	vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
168 
169 static void	vtnet_add_statistics(struct vtnet_softc *);
170 
171 static int	vtnet_enable_rx_intr(struct vtnet_softc *);
172 static int	vtnet_enable_tx_intr(struct vtnet_softc *);
173 static void	vtnet_disable_rx_intr(struct vtnet_softc *);
174 static void	vtnet_disable_tx_intr(struct vtnet_softc *);
175 
176 /* Tunables. */
177 static int vtnet_csum_disable = 0;
178 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
179 static int vtnet_tso_disable = 1;
180 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
181 static int vtnet_lro_disable = 0;
182 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
183 
184 /*
185  * Reducing the number of transmit completed interrupts can
186  * improve performance. To do so, the define below keeps the
187  * Tx vq interrupt disabled and adds calls to vtnet_txeof()
188  * in the start path. The price to pay for this is the m_free'ing
189  * of transmitted mbufs may be delayed.
190  */
191 #define VTNET_TX_INTR_MODERATION
192 
193 static struct virtio_feature_desc vtnet_feature_desc[] = {
194 	{ VIRTIO_NET_F_CSUM,		"TxChecksum"	},
195 	{ VIRTIO_NET_F_GUEST_CSUM,	"RxChecksum"	},
196 	{ VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, "DynOffload"	},
197 	{ VIRTIO_NET_F_MAC,		"MacAddress"	},
198 	{ VIRTIO_NET_F_GSO,		"TxAllGSO"	},
199 	{ VIRTIO_NET_F_GUEST_TSO4,	"RxTSOv4"	},
200 	{ VIRTIO_NET_F_GUEST_TSO6,	"RxTSOv6"	},
201 	{ VIRTIO_NET_F_GUEST_ECN,	"RxECN"		},
202 	{ VIRTIO_NET_F_GUEST_UFO,	"RxUFO"		},
203 	{ VIRTIO_NET_F_HOST_TSO4,	"TxTSOv4"	},
204 	{ VIRTIO_NET_F_HOST_TSO6,	"TxTSOv6"	},
205 	{ VIRTIO_NET_F_HOST_ECN,	"TxTSOECN"	},
206 	{ VIRTIO_NET_F_HOST_UFO,	"TxUFO"		},
207 	{ VIRTIO_NET_F_MRG_RXBUF,	"MrgRxBuf"	},
208 	{ VIRTIO_NET_F_STATUS,		"Status"	},
209 	{ VIRTIO_NET_F_CTRL_VQ,		"ControlVq"	},
210 	{ VIRTIO_NET_F_CTRL_RX,		"RxMode"	},
211 	{ VIRTIO_NET_F_CTRL_VLAN,	"VLanFilter"	},
212 	{ VIRTIO_NET_F_CTRL_RX_EXTRA,	"RxModeExtra"	},
213 	{ VIRTIO_NET_F_GUEST_ANNOUNCE,	"GuestAnnounce"	},
214 	{ VIRTIO_NET_F_MQ,		"Multiqueue"	},
215 	{ VIRTIO_NET_F_CTRL_MAC_ADDR,	"SetMacAddress"	},
216 	{ 0, NULL }
217 };
218 
219 static device_method_t vtnet_methods[] = {
220 	/* Device methods. */
221 	DEVMETHOD(device_probe,		vtnet_probe),
222 	DEVMETHOD(device_attach,	vtnet_attach),
223 	DEVMETHOD(device_detach,	vtnet_detach),
224 	DEVMETHOD(device_suspend,	vtnet_suspend),
225 	DEVMETHOD(device_resume,	vtnet_resume),
226 	DEVMETHOD(device_shutdown,	vtnet_shutdown),
227 
228 	DEVMETHOD_END
229 };
230 
231 static driver_t vtnet_driver = {
232 	"vtnet",
233 	vtnet_methods,
234 	sizeof(struct vtnet_softc)
235 };
236 
237 static devclass_t vtnet_devclass;
238 
239 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass, NULL, NULL);
240 MODULE_VERSION(vtnet, 1);
241 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
242 
243 static int
244 vtnet_probe(device_t dev)
245 {
246 	if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
247 		return (ENXIO);
248 
249 	device_set_desc(dev, "VirtIO Networking Adapter");
250 
251 	return (BUS_PROBE_DEFAULT);
252 }
253 
254 static int
255 vtnet_attach(device_t dev)
256 {
257 	struct vtnet_softc *sc;
258 	int i, error;
259 
260 	sc = device_get_softc(dev);
261 	sc->vtnet_dev = dev;
262 
263 	lwkt_serialize_init(&sc->vtnet_slz);
264 	lwkt_serialize_init(&sc->vtnet_rx_slz);
265 	lwkt_serialize_init(&sc->vtnet_tx_slz);
266 	sc->serializes[0] = &sc->vtnet_slz;
267 	sc->serializes[1] = &sc->vtnet_rx_slz;
268 	sc->serializes[2] = &sc->vtnet_tx_slz;
269 
270 	ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
271 		     vtnet_ifmedia_sts);
272 	ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
273 	ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
274 
275 	vtnet_add_statistics(sc);
276 	SLIST_INIT(&sc->vtnet_txhdr_free);
277 
278 	/* Register our feature descriptions. */
279 	virtio_set_feature_desc(dev, vtnet_feature_desc);
280 	vtnet_negotiate_features(sc);
281 
282 	if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
283 		sc->vtnet_flags |= VTNET_FLAG_INDIRECT;
284 
285 	if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
286 		/* This feature should always be negotiated. */
287 		sc->vtnet_flags |= VTNET_FLAG_MAC;
288 	}
289 
290 	if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
291 		sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
292 		sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
293 	} else {
294 		sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
295 	}
296 
297 	sc->vtnet_rx_mbuf_size = MCLBYTES;
298 	sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
299 
300 	if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
301 		sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
302 
303 		if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
304 			sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
305 		if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
306 			sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
307 		if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
308 		    virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
309 			sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
310 	}
311 
312 	error = vtnet_alloc_intrs(sc);
313 	if (error) {
314 		device_printf(dev, "cannot allocate interrupts\n");
315 		goto fail;
316 	}
317 
318 	error = vtnet_alloc_virtqueues(sc);
319 	if (error) {
320 		device_printf(dev, "cannot allocate virtqueues\n");
321 		goto fail;
322 	}
323 
324 	error = vtnet_bind_intrs(sc);
325 	if (error) {
326 		device_printf(dev, "cannot bind virtqueues to interrupts\n");
327 		goto fail;
328 	}
329 
330 	/* Read (or generate) the MAC address for the adapter. */
331 	vtnet_get_hwaddr(sc);
332 
333 	error = vtnet_setup_interface(sc);
334 	if (error) {
335 		device_printf(dev, "cannot setup interface\n");
336 		goto fail;
337 	}
338 
339 	for (i = 0; i < sc->vtnet_nintr; i++) {
340 		error = virtio_setup_intr(dev, i, sc->vtnet_intr_slz[i]);
341 		if (error) {
342 			device_printf(dev, "cannot setup virtqueue "
343 			    "interrupts\n");
344 			ether_ifdetach(sc->vtnet_ifp);
345 			goto fail;
346 		}
347 	}
348 
349 	if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
350 		ifnet_serialize_all(sc->vtnet_ifp);
351 		vtnet_set_hwaddr(sc);
352 		ifnet_deserialize_all(sc->vtnet_ifp);
353 	}
354 
355 	/*
356 	 * Device defaults to promiscuous mode for backwards
357 	 * compatibility. Turn it off if possible.
358 	 */
359 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
360 		ifnet_serialize_all(sc->vtnet_ifp);
361 		if (vtnet_set_promisc(sc, 0) != 0) {
362 			sc->vtnet_ifp->if_flags |= IFF_PROMISC;
363 			device_printf(dev,
364 			    "cannot disable promiscuous mode\n");
365 		}
366 		ifnet_deserialize_all(sc->vtnet_ifp);
367 	} else
368 		sc->vtnet_ifp->if_flags |= IFF_PROMISC;
369 
370 fail:
371 	if (error)
372 		vtnet_detach(dev);
373 
374 	return (error);
375 }
376 
377 static int
378 vtnet_detach(device_t dev)
379 {
380 	struct vtnet_softc *sc;
381 	struct ifnet *ifp;
382 	int i;
383 
384 	sc = device_get_softc(dev);
385 	ifp = sc->vtnet_ifp;
386 
387 	for (i = 0; i < sc->vtnet_nintr; i++)
388 		virtio_teardown_intr(dev, i);
389 
390 	if (device_is_attached(dev)) {
391 		ifnet_serialize_all(ifp);
392 		vtnet_stop(sc);
393 		lwkt_serialize_handler_disable(&sc->vtnet_slz);
394 		lwkt_serialize_handler_disable(&sc->vtnet_rx_slz);
395 		lwkt_serialize_handler_disable(&sc->vtnet_tx_slz);
396 		ifnet_deserialize_all(ifp);
397 
398 		ether_ifdetach(ifp);
399 	}
400 
401 	if (sc->vtnet_vlan_attach != NULL) {
402 		EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
403 		sc->vtnet_vlan_attach = NULL;
404 	}
405 	if (sc->vtnet_vlan_detach != NULL) {
406 		EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
407 		sc->vtnet_vlan_detach = NULL;
408 	}
409 
410 	if (ifp) {
411 		if_free(ifp);
412 		sc->vtnet_ifp = NULL;
413 	}
414 
415 	if (sc->vtnet_rx_vq != NULL)
416 		vtnet_free_rx_mbufs(sc);
417 	if (sc->vtnet_tx_vq != NULL)
418 		vtnet_free_tx_mbufs(sc);
419 	if (sc->vtnet_ctrl_vq != NULL)
420 		vtnet_free_ctrl_vq(sc);
421 
422 	if (sc->vtnet_txhdrarea != NULL) {
423 		contigfree(sc->vtnet_txhdrarea,
424 		    sc->vtnet_txhdrcount * sizeof(struct vtnet_tx_header),
425 		    M_VTNET);
426 		sc->vtnet_txhdrarea = NULL;
427 	}
428 	SLIST_INIT(&sc->vtnet_txhdr_free);
429 	if (sc->vtnet_macfilter != NULL) {
430 		contigfree(sc->vtnet_macfilter,
431 		    sizeof(struct vtnet_mac_filter), M_DEVBUF);
432 		sc->vtnet_macfilter = NULL;
433 	}
434 
435 	ifmedia_removeall(&sc->vtnet_media);
436 
437 	return (0);
438 }
439 
440 static int
441 vtnet_suspend(device_t dev)
442 {
443 	struct vtnet_softc *sc;
444 
445 	sc = device_get_softc(dev);
446 
447 	ifnet_serialize_all(sc->vtnet_ifp);
448 	vtnet_stop(sc);
449 	sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
450 	ifnet_deserialize_all(sc->vtnet_ifp);
451 
452 	return (0);
453 }
454 
455 static int
456 vtnet_resume(device_t dev)
457 {
458 	struct vtnet_softc *sc;
459 	struct ifnet *ifp;
460 
461 	sc = device_get_softc(dev);
462 	ifp = sc->vtnet_ifp;
463 
464 	ifnet_serialize_all(ifp);
465 	if (ifp->if_flags & IFF_UP)
466 		vtnet_init(sc);
467 	sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
468 	ifnet_deserialize_all(ifp);
469 
470 	return (0);
471 }
472 
473 static int
474 vtnet_shutdown(device_t dev)
475 {
476 
477 	/*
478 	 * Suspend already does all of what we need to
479 	 * do here; we just never expect to be resumed.
480 	 */
481 	return (vtnet_suspend(dev));
482 }
483 
484 static void
485 vtnet_negotiate_features(struct vtnet_softc *sc)
486 {
487 	device_t dev;
488 	uint64_t mask, features;
489 
490 	dev = sc->vtnet_dev;
491 	mask = 0;
492 
493 	if (vtnet_csum_disable)
494 		mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
495 
496 	/*
497 	 * XXX DragonFly doesn't support receive checksum offload for ipv6 yet,
498 	 *     hence always disable the virtio feature for now.
499 	 * XXX We need to support the DynOffload feature, in order to
500 	 *     dynamically enable/disable this feature.
501 	 */
502 	mask |= VIRTIO_NET_F_GUEST_CSUM;
503 
504 	/*
505 	 * TSO is only available when the tx checksum offload feature is also
506 	 * negotiated.
507 	 */
508 	if (vtnet_csum_disable || vtnet_tso_disable)
509 		mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
510 		    VIRTIO_NET_F_HOST_ECN;
511 
512 	if (vtnet_lro_disable)
513 		mask |= VTNET_LRO_FEATURES;
514 
515 	features = VTNET_FEATURES & ~mask;
516 	features |= VIRTIO_F_NOTIFY_ON_EMPTY;
517 	features |= VIRTIO_F_ANY_LAYOUT;
518 	sc->vtnet_features = virtio_negotiate_features(dev, features);
519 
520 	if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
521 	    virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
522 		/*
523 		 * LRO without mergeable buffers requires special care. This
524 		 * is not ideal because every receive buffer must be large
525 		 * enough to hold the maximum TCP packet, the Ethernet header,
526 		 * and the header. This requires up to 34 descriptors with
527 		 * MCLBYTES clusters. If we do not have indirect descriptors,
528 		 * LRO is disabled since the virtqueue will not contain very
529 		 * many receive buffers.
530 		 */
531 		if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
532 			device_printf(dev,
533 			    "LRO disabled due to both mergeable buffers and "
534 			    "indirect descriptors not negotiated\n");
535 
536 			features &= ~VTNET_LRO_FEATURES;
537 			sc->vtnet_features =
538 			    virtio_negotiate_features(dev, features);
539 		} else
540 			sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
541 	}
542 }
543 
544 static void
545 vtnet_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
546 {
547 	struct vtnet_softc *sc = ifp->if_softc;
548 
549 	ifnet_serialize_array_enter(sc->serializes, 3, slz);
550 }
551 
552 static void
553 vtnet_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
554 {
555 	struct vtnet_softc *sc = ifp->if_softc;
556 
557 	ifnet_serialize_array_exit(sc->serializes, 3, slz);
558 }
559 
560 static int
561 vtnet_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
562 {
563 	struct vtnet_softc *sc = ifp->if_softc;
564 
565 	return ifnet_serialize_array_try(sc->serializes, 3, slz);
566 }
567 
568 #ifdef INVARIANTS
569 
570 static void
571 vtnet_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
572     boolean_t serialized)
573 {
574 	struct vtnet_softc *sc = ifp->if_softc;
575 
576 	ifnet_serialize_array_assert(sc->serializes, 3, slz, serialized);
577 }
578 
579 #endif  /* INVARIANTS */
580 
581 static int
582 vtnet_alloc_intrs(struct vtnet_softc *sc)
583 {
584 	int cnt, error;
585 	int intrcount = virtio_intr_count(sc->vtnet_dev);
586 	int i;
587 	int use_config;
588 
589 	if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS)) {
590 		use_config = 1;
591 		/* We can use a maximum of 3 interrupt vectors. */
592 		intrcount = imin(intrcount, 3);
593 	} else {
594 		/* We can use a maximum of 2 interrupt vectors. */
595 		intrcount = imin(intrcount, 2);
596 	}
597 
598 	if (intrcount < 1)
599 		return (ENXIO);
600 
601 	for (i = 0; i < intrcount; i++)
602 		sc->vtnet_cpus[i] = -1;
603 
604 	cnt = intrcount;
605 	error = virtio_intr_alloc(sc->vtnet_dev, &cnt, use_config,
606 	    sc->vtnet_cpus);
607 	if (error != 0) {
608 		virtio_intr_release(sc->vtnet_dev);
609 		return (error);
610 	}
611 	sc->vtnet_nintr = cnt;
612 
613 	return (0);
614 }
615 
616 static int
617 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
618 {
619 	device_t dev;
620 	struct vq_alloc_info vq_info[3];
621 	int nvqs;
622 
623 	dev = sc->vtnet_dev;
624 	nvqs = 2;
625 
626 	/*
627 	 * Indirect descriptors are not needed for the Rx
628 	 * virtqueue when mergeable buffers are negotiated.
629 	 * The header is placed inline with the data, not
630 	 * in a separate descriptor, and mbuf clusters are
631 	 * always physically contiguous.
632 	 */
633 	if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
634 		sc->vtnet_rx_nsegs = (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) ?
635 		    VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
636 	} else
637 		sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
638 
639 	if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
640 	    virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
641 		sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
642 	else
643 		sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
644 
645 	VQ_ALLOC_INFO_INIT(&vq_info[0], sc->vtnet_rx_nsegs, &sc->vtnet_rx_vq,
646 	    "%s receive", device_get_nameunit(dev));
647 
648 	VQ_ALLOC_INFO_INIT(&vq_info[1], sc->vtnet_tx_nsegs, &sc->vtnet_tx_vq,
649 	    "%s transmit", device_get_nameunit(dev));
650 
651 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
652 		nvqs++;
653 
654 		VQ_ALLOC_INFO_INIT(&vq_info[2], 0, &sc->vtnet_ctrl_vq,
655 		    "%s control", device_get_nameunit(dev));
656 	}
657 
658 	return (virtio_alloc_virtqueues(dev, nvqs, vq_info));
659 }
660 
661 static int
662 vtnet_bind_intrs(struct vtnet_softc *sc)
663 {
664 	int error = 0;
665 	int i;
666 
667 	for (i = 0; i < 3; i++)
668 		sc->vtnet_intr_slz[i] = &sc->vtnet_slz;
669 
670 	/* Possible "Virtqueue <-> IRQ" configurations */
671 	switch (sc->vtnet_nintr) {
672 	case 1:
673 		sc->vtnet_irqmap[0] = (struct irqmap){0, vtnet_rx_vq_intr};
674 		sc->vtnet_irqmap[1] = (struct irqmap){0, vtnet_tx_vq_intr};
675 		break;
676 	case 2:
677 		if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS)) {
678 			sc->vtnet_irqmap[0] =
679 			    (struct irqmap){1, vtnet_rx_vq_intr};
680 			sc->vtnet_irqmap[1] =
681 			    (struct irqmap){1, vtnet_tx_vq_intr};
682 		} else {
683 			sc->vtnet_irqmap[0] =
684 			    (struct irqmap){0, vtnet_rx_msix_intr};
685 			sc->vtnet_irqmap[1] =
686 			    (struct irqmap){1, vtnet_tx_msix_intr};
687 			sc->vtnet_intr_slz[0] = &sc->vtnet_rx_slz;
688 			sc->vtnet_intr_slz[1] = &sc->vtnet_tx_slz;
689 		}
690 		break;
691 	case 3:
692 		sc->vtnet_irqmap[0] = (struct irqmap){1, vtnet_rx_msix_intr};
693 		sc->vtnet_irqmap[1] = (struct irqmap){2, vtnet_tx_msix_intr};
694 		sc->vtnet_intr_slz[1] = &sc->vtnet_rx_slz;
695 		sc->vtnet_intr_slz[2] = &sc->vtnet_tx_slz;
696 		break;
697 	default:
698 		device_printf(sc->vtnet_dev,
699 		    "Invalid interrupt vector count: %d\n", sc->vtnet_nintr);
700 		error = EINVAL;
701 		goto fail;
702 	}
703 
704 	for (i = 0; i < 2; i++) {
705 		error = virtio_bind_intr(sc->vtnet_dev,
706 		    sc->vtnet_irqmap[i].irq, i, sc->vtnet_irqmap[i].handler,
707 		    sc);
708 		if (error) {
709 			device_printf(sc->vtnet_dev,
710 			    "cannot bind virtqueue IRQs\n");
711 			goto fail;
712 		}
713 	}
714 	if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS)) {
715 		error = virtio_bind_intr(sc->vtnet_dev, 0, -1,
716 		    vtnet_config_intr, sc);
717 		if (error) {
718 			device_printf(sc->vtnet_dev,
719 			    "cannot bind config_change IRQ\n");
720 			goto fail;
721 		}
722 	}
723 
724 fail:
725 	return (error);
726 }
727 
728 static int
729 vtnet_setup_interface(struct vtnet_softc *sc)
730 {
731 	device_t dev;
732 	struct ifnet *ifp;
733 	int i;
734 
735 	dev = sc->vtnet_dev;
736 
737 	ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
738 	if (ifp == NULL) {
739 		device_printf(dev, "cannot allocate ifnet structure\n");
740 		return (ENOSPC);
741 	}
742 
743 	ifp->if_softc = sc;
744 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
745 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
746 	ifp->if_init = vtnet_init;
747 	ifp->if_start = vtnet_start;
748 #ifdef IFPOLL_ENABLE
749 	ifp->if_npoll = vtnet_npoll;
750 #endif
751 	ifp->if_serialize = vtnet_serialize;
752 	ifp->if_deserialize = vtnet_deserialize;
753 	ifp->if_tryserialize = vtnet_tryserialize;
754 #ifdef INVARIANTS
755 	ifp->if_serialize_assert = vtnet_serialize_assert;
756 #endif
757 	ifp->if_ioctl = vtnet_ioctl;
758 
759 	sc->vtnet_rx_process_limit = virtqueue_size(sc->vtnet_rx_vq);
760 	sc->vtnet_tx_size = virtqueue_size(sc->vtnet_tx_vq);
761 	if (sc->vtnet_flags & VTNET_FLAG_INDIRECT)
762 		sc->vtnet_txhdrcount = sc->vtnet_tx_size;
763 	else
764 		sc->vtnet_txhdrcount = (sc->vtnet_tx_size / 2) + 1;
765 	sc->vtnet_txhdrarea = contigmalloc(
766 	    sc->vtnet_txhdrcount * sizeof(struct vtnet_tx_header),
767 	    M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
768 	if (sc->vtnet_txhdrarea == NULL) {
769 		device_printf(dev, "cannot contigmalloc the tx headers\n");
770 		return (ENOMEM);
771 	}
772 	for (i = 0; i < sc->vtnet_txhdrcount; i++)
773 		vtnet_enqueue_txhdr(sc, &sc->vtnet_txhdrarea[i]);
774 	sc->vtnet_macfilter = contigmalloc(
775 	    sizeof(struct vtnet_mac_filter),
776 	    M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
777 	if (sc->vtnet_macfilter == NULL) {
778 		device_printf(dev,
779 		    "cannot contigmalloc the mac filter table\n");
780 		return (ENOMEM);
781 	}
782 	ifq_set_maxlen(&ifp->if_snd, sc->vtnet_tx_size - 1);
783 	ifq_set_ready(&ifp->if_snd);
784 
785 	ether_ifattach(ifp, sc->vtnet_hwaddr, NULL);
786 
787 	/* The Tx IRQ is currently always the last allocated interrupt. */
788 	ifq_set_cpuid(&ifp->if_snd, sc->vtnet_cpus[sc->vtnet_nintr - 1]);
789 	ifsq_watchdog_init(&sc->vtnet_tx_watchdog,
790 	    ifq_get_subq_default(&ifp->if_snd), vtnet_watchdog);
791 	ifq_set_hw_serialize(&ifp->if_snd, &sc->vtnet_tx_slz);
792 
793 	/* Tell the upper layer(s) we support long frames. */
794 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
795 	ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
796 
797 	if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
798 		ifp->if_capabilities |= IFCAP_TXCSUM;
799 
800 		if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
801 			ifp->if_capabilities |= IFCAP_TSO4;
802 		if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
803 			ifp->if_capabilities |= IFCAP_TSO6;
804 		if (ifp->if_capabilities & IFCAP_TSO)
805 			ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
806 
807 		if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
808 			sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
809 	}
810 
811 	if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM))
812 		ifp->if_capabilities |= IFCAP_RXCSUM;
813 
814 #if 0	/* IFCAP_LRO doesn't exist in DragonFly. */
815 	if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
816 	    virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
817 		ifp->if_capabilities |= IFCAP_LRO;
818 #endif
819 
820 	if ((ifp->if_capabilities & IFCAP_HWCSUM) == IFCAP_HWCSUM) {
821 		/*
822 		 * VirtIO does not support VLAN tagging, but we can fake
823 		 * it by inserting and removing the 802.1Q header during
824 		 * transmit and receive. We are then able to do checksum
825 		 * offloading of VLAN frames.
826 		 */
827 		ifp->if_capabilities |=
828 			IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
829 	}
830 
831 	ifp->if_capenable = ifp->if_capabilities;
832 
833 	/*
834 	 * Capabilities after here are not enabled by default.
835 	 */
836 
837 	if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
838 		ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
839 
840 		sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
841 		    vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
842 		sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
843 		    vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
844 	}
845 
846 	return (0);
847 }
848 
849 static void
850 vtnet_set_hwaddr(struct vtnet_softc *sc)
851 {
852 	device_t dev;
853 
854 	dev = sc->vtnet_dev;
855 
856 	if ((sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) &&
857 	    (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)) {
858 		if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
859 			device_printf(dev, "unable to set MAC address\n");
860 	} else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
861 		virtio_write_device_config(dev,
862 		    offsetof(struct virtio_net_config, mac),
863 		    sc->vtnet_hwaddr, ETHER_ADDR_LEN);
864 	}
865 }
866 
867 static void
868 vtnet_get_hwaddr(struct vtnet_softc *sc)
869 {
870 	device_t dev;
871 
872 	dev = sc->vtnet_dev;
873 
874 	if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
875 		/*
876 		 * Generate a random locally administered unicast address.
877 		 *
878 		 * It would be nice to generate the same MAC address across
879 		 * reboots, but it seems all the hosts currently available
880 		 * support the MAC feature, so this isn't too important.
881 		 */
882 		sc->vtnet_hwaddr[0] = 0xB2;
883 		karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
884 		return;
885 	}
886 
887 	virtio_read_device_config(dev,
888 	    offsetof(struct virtio_net_config, mac),
889 	    sc->vtnet_hwaddr, ETHER_ADDR_LEN);
890 }
891 
892 static int
893 vtnet_is_link_up(struct vtnet_softc *sc)
894 {
895 	device_t dev;
896 	struct ifnet *ifp;
897 	uint16_t status;
898 
899 	dev = sc->vtnet_dev;
900 	ifp = sc->vtnet_ifp;
901 
902 	ASSERT_SERIALIZED(&sc->vtnet_slz);
903 
904 	if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
905 		status = virtio_read_dev_config_2(dev,
906 				offsetof(struct virtio_net_config, status));
907 	} else {
908 		status = VIRTIO_NET_S_LINK_UP;
909 	}
910 
911 	return ((status & VIRTIO_NET_S_LINK_UP) != 0);
912 }
913 
914 static void
915 vtnet_update_link_status(struct vtnet_softc *sc)
916 {
917 	device_t dev;
918 	struct ifnet *ifp;
919 	struct ifaltq_subque *ifsq;
920 	int link;
921 
922 	dev = sc->vtnet_dev;
923 	ifp = sc->vtnet_ifp;
924 	ifsq = ifq_get_subq_default(&ifp->if_snd);
925 
926 	link = vtnet_is_link_up(sc);
927 
928 	if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
929 		sc->vtnet_flags |= VTNET_FLAG_LINK;
930 		if (bootverbose)
931 			device_printf(dev, "Link is up\n");
932 		ifp->if_link_state = LINK_STATE_UP;
933 		if_link_state_change(ifp);
934 		if (!ifsq_is_empty(ifsq))
935 			ifsq_devstart_sched(ifsq);
936 	} else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
937 		sc->vtnet_flags &= ~VTNET_FLAG_LINK;
938 		if (bootverbose)
939 			device_printf(dev, "Link is down\n");
940 
941 		ifp->if_link_state = LINK_STATE_DOWN;
942 		if_link_state_change(ifp);
943 	}
944 }
945 
946 static void
947 vtnet_watchdog(struct ifaltq_subque *ifsq)
948 {
949 	struct ifnet *ifp;
950 	struct vtnet_softc *sc;
951 
952 	ifp = ifsq_get_ifp(ifsq);
953 	sc = ifp->if_softc;
954 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
955 
956 	if (virtqueue_empty(sc->vtnet_tx_vq)) {
957 		if_printf(ifp, "Spurious TX watchdog timeout -- ignoring\n");
958 		sc->vtnet_tx_watchdog.wd_timer = 0;
959 		return;
960 	}
961 
962 	if_printf(ifp, "TX watchdog timeout -- resetting\n");
963 #ifdef VTNET_DEBUG
964 	virtqueue_dump(sc->vtnet_tx_vq);
965 #endif
966 	ifp->if_oerrors++;
967 	ifp->if_flags &= ~IFF_RUNNING;
968 	vtnet_init(sc);
969 	ifsq_devstart_sched(ifsq);
970 }
971 
972 static int
973 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
974 {
975 	struct vtnet_softc *sc;
976 	struct ifreq *ifr;
977 	int reinit, mask, error;
978 
979 	sc = ifp->if_softc;
980 	ifr = (struct ifreq *) data;
981 	reinit = 0;
982 	error = 0;
983 
984 	switch (cmd) {
985 	case SIOCSIFMTU:
986 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
987 			error = EINVAL;
988 		else if (ifp->if_mtu != ifr->ifr_mtu)
989 			error = vtnet_change_mtu(sc, ifr->ifr_mtu);
990 		break;
991 
992 	case SIOCSIFFLAGS:
993 		if ((ifp->if_flags & IFF_UP) == 0) {
994 			if (ifp->if_flags & IFF_RUNNING)
995 				vtnet_stop(sc);
996 		} else if (ifp->if_flags & IFF_RUNNING) {
997 			if ((ifp->if_flags ^ sc->vtnet_if_flags) &
998 			    (IFF_PROMISC | IFF_ALLMULTI)) {
999 				if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1000 					vtnet_rx_filter(sc);
1001 				else
1002 					error = ENOTSUP;
1003 			}
1004 		} else {
1005 			vtnet_init(sc);
1006 		}
1007 
1008 		if (error == 0)
1009 			sc->vtnet_if_flags = ifp->if_flags;
1010 		break;
1011 
1012 	case SIOCADDMULTI:
1013 	case SIOCDELMULTI:
1014 		if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
1015 		    (ifp->if_flags & IFF_RUNNING))
1016 			vtnet_rx_filter_mac(sc);
1017 		break;
1018 
1019 	case SIOCSIFMEDIA:
1020 	case SIOCGIFMEDIA:
1021 		error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1022 		break;
1023 
1024 	case SIOCSIFCAP:
1025 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1026 
1027 
1028 		if (mask & IFCAP_TXCSUM) {
1029 			ifp->if_capenable ^= IFCAP_TXCSUM;
1030 			if (ifp->if_capenable & IFCAP_TXCSUM)
1031 				ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
1032 			else
1033 				ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
1034 		}
1035 
1036 		if (mask & IFCAP_TSO4) {
1037 			ifp->if_capenable ^= IFCAP_TSO4;
1038 			if (ifp->if_capenable & IFCAP_TSO4)
1039 				ifp->if_hwassist |= CSUM_TSO;
1040 			else
1041 				ifp->if_hwassist &= ~CSUM_TSO;
1042 		}
1043 
1044 		if (mask & IFCAP_RXCSUM) {
1045 			ifp->if_capenable ^= IFCAP_RXCSUM;
1046 			reinit = 1;
1047 		}
1048 
1049 #if 0	/* IFCAP_LRO doesn't exist in DragonFly. */
1050 		if (mask & IFCAP_LRO) {
1051 			ifp->if_capenable ^= IFCAP_LRO;
1052 			reinit = 1;
1053 		}
1054 #endif
1055 
1056 		if (mask & IFCAP_VLAN_HWFILTER) {
1057 			ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1058 			reinit = 1;
1059 		}
1060 
1061 		if (mask & IFCAP_VLAN_HWTSO)
1062 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1063 
1064 		if (mask & IFCAP_VLAN_HWTAGGING)
1065 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1066 
1067 		if (reinit && (ifp->if_flags & IFF_RUNNING)) {
1068 			ifp->if_flags &= ~IFF_RUNNING;
1069 			vtnet_init(sc);
1070 		}
1071 		//VLAN_CAPABILITIES(ifp);
1072 
1073 		break;
1074 
1075 	default:
1076 		error = ether_ioctl(ifp, cmd, data);
1077 		break;
1078 	}
1079 
1080 	return (error);
1081 }
1082 
1083 static int
1084 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1085 {
1086 	struct ifnet *ifp;
1087 	int new_frame_size, clsize;
1088 
1089 	ifp = sc->vtnet_ifp;
1090 
1091 	if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1092 		new_frame_size = sizeof(struct vtnet_rx_header) +
1093 		    sizeof(struct ether_vlan_header) + new_mtu;
1094 
1095 		if (new_frame_size > MJUM9BYTES)
1096 			return (EINVAL);
1097 
1098 		if (new_frame_size <= MCLBYTES)
1099 			clsize = MCLBYTES;
1100 		else
1101 			clsize = MJUM9BYTES;
1102 	} else {
1103 		new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1104 		    sizeof(struct ether_vlan_header) + new_mtu;
1105 
1106 		if (new_frame_size <= MCLBYTES)
1107 			clsize = MCLBYTES;
1108 		else
1109 			clsize = MJUMPAGESIZE;
1110 	}
1111 
1112 	sc->vtnet_rx_mbuf_size = clsize;
1113 	sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1114 	KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1115 	    ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1116 
1117 	ifp->if_mtu = new_mtu;
1118 
1119 	if (ifp->if_flags & IFF_RUNNING) {
1120 		ifp->if_flags &= ~IFF_RUNNING;
1121 		vtnet_init(sc);
1122 	}
1123 
1124 	return (0);
1125 }
1126 
1127 static int
1128 vtnet_init_rx_vq(struct vtnet_softc *sc)
1129 {
1130 	struct virtqueue *vq;
1131 	int nbufs, error;
1132 
1133 	vq = sc->vtnet_rx_vq;
1134 	nbufs = 0;
1135 	error = ENOSPC;
1136 
1137 	while (!virtqueue_full(vq)) {
1138 		if ((error = vtnet_newbuf(sc)) != 0)
1139 			break;
1140 		nbufs++;
1141 	}
1142 
1143 	if (nbufs > 0) {
1144 		virtqueue_notify(vq, NULL);
1145 
1146 		/*
1147 		 * EMSGSIZE signifies the virtqueue did not have enough
1148 		 * entries available to hold the last mbuf. This is not
1149 		 * an error. We should not get ENOSPC since we check if
1150 		 * the virtqueue is full before attempting to add a
1151 		 * buffer.
1152 		 */
1153 		if (error == EMSGSIZE)
1154 			error = 0;
1155 	}
1156 
1157 	return (error);
1158 }
1159 
1160 static void
1161 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1162 {
1163 	struct virtqueue *vq;
1164 	struct mbuf *m;
1165 	int last;
1166 
1167 	vq = sc->vtnet_rx_vq;
1168 	last = 0;
1169 
1170 	while ((m = virtqueue_drain(vq, &last)) != NULL)
1171 		m_freem(m);
1172 
1173 	KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1174 }
1175 
1176 static void
1177 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1178 {
1179 	struct virtqueue *vq;
1180 	struct vtnet_tx_header *txhdr;
1181 	int last;
1182 
1183 	vq = sc->vtnet_tx_vq;
1184 	last = 0;
1185 
1186 	while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1187 		m_freem(txhdr->vth_mbuf);
1188 		vtnet_enqueue_txhdr(sc, txhdr);
1189 	}
1190 
1191 	KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1192 }
1193 
1194 static void
1195 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1196 {
1197 	/*
1198 	 * The control virtqueue is only polled, therefore
1199 	 * it should already be empty.
1200 	 */
1201 	KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1202 		("Ctrl Vq not empty"));
1203 }
1204 
1205 static struct mbuf *
1206 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1207 {
1208 	struct mbuf *m_head, *m_tail, *m;
1209 	int i, clsize;
1210 
1211 	clsize = sc->vtnet_rx_mbuf_size;
1212 
1213 	/*use getcl instead of getjcl. see  if_mxge.c comment line 2398*/
1214 	//m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1215 	m_head = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR );
1216 	if (m_head == NULL)
1217 		goto fail;
1218 
1219 	m_head->m_len = clsize;
1220 	m_tail = m_head;
1221 
1222 	if (nbufs > 1) {
1223 		KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1224 			("chained Rx mbuf requested without LRO_NOMRG"));
1225 
1226 		for (i = 0; i < nbufs - 1; i++) {
1227 			//m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1228 			m = m_getcl(M_NOWAIT, MT_DATA, 0);
1229 			if (m == NULL)
1230 				goto fail;
1231 
1232 			m->m_len = clsize;
1233 			m_tail->m_next = m;
1234 			m_tail = m;
1235 		}
1236 	}
1237 
1238 	if (m_tailp != NULL)
1239 		*m_tailp = m_tail;
1240 
1241 	return (m_head);
1242 
1243 fail:
1244 	sc->vtnet_stats.mbuf_alloc_failed++;
1245 	m_freem(m_head);
1246 
1247 	return (NULL);
1248 }
1249 
1250 static int
1251 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1252 {
1253 	struct mbuf *m, *m_prev;
1254 	struct mbuf *m_new, *m_tail;
1255 	int len, clsize, nreplace, error;
1256 
1257 	m = m0;
1258 	m_prev = NULL;
1259 	len = len0;
1260 
1261 	m_tail = NULL;
1262 	clsize = sc->vtnet_rx_mbuf_size;
1263 	nreplace = 0;
1264 
1265 	if (m->m_next != NULL)
1266 		KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1267 		    ("chained Rx mbuf without LRO_NOMRG"));
1268 
1269 	/*
1270 	 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1271 	 * allocating an entire chain for each received frame. When
1272 	 * the received frame's length is less than that of the chain,
1273 	 * the unused mbufs are reassigned to the new chain.
1274 	 */
1275 	while (len > 0) {
1276 		/*
1277 		 * Something is seriously wrong if we received
1278 		 * a frame larger than the mbuf chain. Drop it.
1279 		 */
1280 		if (m == NULL) {
1281 			sc->vtnet_stats.rx_frame_too_large++;
1282 			return (EMSGSIZE);
1283 		}
1284 
1285 		KASSERT(m->m_len == clsize,
1286 		    ("mbuf length not expected cluster size: %d",
1287 		    m->m_len));
1288 
1289 		m->m_len = MIN(m->m_len, len);
1290 		len -= m->m_len;
1291 
1292 		m_prev = m;
1293 		m = m->m_next;
1294 		nreplace++;
1295 	}
1296 
1297 	KASSERT(m_prev != NULL, ("m_prev == NULL"));
1298 	KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1299 		("too many replacement mbufs: %d/%d", nreplace,
1300 		sc->vtnet_rx_mbuf_count));
1301 
1302 	m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1303 	if (m_new == NULL) {
1304 		m_prev->m_len = clsize;
1305 		return (ENOBUFS);
1306 	}
1307 
1308 	/*
1309 	 * Move unused mbufs, if any, from the original chain
1310 	 * onto the end of the new chain.
1311 	 */
1312 	if (m_prev->m_next != NULL) {
1313 		m_tail->m_next = m_prev->m_next;
1314 		m_prev->m_next = NULL;
1315 	}
1316 
1317 	error = vtnet_enqueue_rxbuf(sc, m_new);
1318 	if (error) {
1319 		/*
1320 		 * BAD! We could not enqueue the replacement mbuf chain. We
1321 		 * must restore the m0 chain to the original state if it was
1322 		 * modified so we can subsequently discard it.
1323 		 *
1324 		 * NOTE: The replacement is suppose to be an identical copy
1325 		 * to the one just dequeued so this is an unexpected error.
1326 		 */
1327 		sc->vtnet_stats.rx_enq_replacement_failed++;
1328 
1329 		if (m_tail->m_next != NULL) {
1330 			m_prev->m_next = m_tail->m_next;
1331 			m_tail->m_next = NULL;
1332 		}
1333 
1334 		m_prev->m_len = clsize;
1335 		m_freem(m_new);
1336 	}
1337 
1338 	return (error);
1339 }
1340 
1341 static int
1342 vtnet_newbuf(struct vtnet_softc *sc)
1343 {
1344 	struct mbuf *m;
1345 	int error;
1346 
1347 	m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1348 	if (m == NULL)
1349 		return (ENOBUFS);
1350 
1351 	error = vtnet_enqueue_rxbuf(sc, m);
1352 	if (error)
1353 		m_freem(m);
1354 
1355 	return (error);
1356 }
1357 
1358 static void
1359 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1360 {
1361 	struct virtqueue *vq;
1362 	struct mbuf *m;
1363 
1364 	vq = sc->vtnet_rx_vq;
1365 
1366 	while (--nbufs > 0) {
1367 		if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1368 			break;
1369 		vtnet_discard_rxbuf(sc, m);
1370 	}
1371 }
1372 
1373 static void
1374 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1375 {
1376 	int error;
1377 
1378 	/*
1379 	 * Requeue the discarded mbuf. This should always be
1380 	 * successful since it was just dequeued.
1381 	 */
1382 	error = vtnet_enqueue_rxbuf(sc, m);
1383 	KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1384 }
1385 
1386 static int
1387 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1388 {
1389 	struct sglist sg;
1390 	struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1391 	struct vtnet_rx_header *rxhdr;
1392 	struct virtio_net_hdr *hdr;
1393 	uint8_t *mdata;
1394 	int offset, error;
1395 
1396 	ASSERT_SERIALIZED(&sc->vtnet_rx_slz);
1397 	if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1398 		KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1399 
1400 	sglist_init(&sg, sc->vtnet_rx_nsegs, segs);
1401 
1402 	mdata = mtod(m, uint8_t *);
1403 	offset = 0;
1404 
1405 	if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1406 		rxhdr = (struct vtnet_rx_header *) mdata;
1407 		hdr = &rxhdr->vrh_hdr;
1408 		offset += sizeof(struct vtnet_rx_header);
1409 
1410 		error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1411 		KASSERT(error == 0, ("cannot add header to sglist"));
1412 	}
1413 
1414 	error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1415 	if (error)
1416 		return (error);
1417 
1418 	if (m->m_next != NULL) {
1419 		error = sglist_append_mbuf(&sg, m->m_next);
1420 		if (error)
1421 			return (error);
1422 	}
1423 
1424 	return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1425 }
1426 
1427 #ifdef IFPOLL_ENABLE
1428 
1429 static void
1430 vtnet_npoll_status(struct ifnet *ifp)
1431 {
1432 	struct vtnet_softc *sc = ifp->if_softc;
1433 
1434 	ASSERT_SERIALIZED(&sc->vtnet_slz);
1435 
1436 	vtnet_update_link_status(sc);
1437 }
1438 
1439 static void
1440 vtnet_npoll_rx(struct ifnet *ifp, void *arg __unused, int cycle)
1441 {
1442 	struct vtnet_softc *sc = ifp->if_softc;
1443 
1444 	vtnet_rxeof(sc, cycle, NULL);
1445 }
1446 
1447 static void
1448 vtnet_npoll_tx(struct ifnet *ifp, void *arg __unused, int cycle __unused)
1449 {
1450 	struct vtnet_softc *sc = ifp->if_softc;
1451 
1452 	ASSERT_SERIALIZED(&sc->vtnet_tx_slz);
1453 
1454 	vtnet_txeof(sc);
1455 	if (!ifq_is_empty(&ifp->if_snd))
1456 		if_devstart(ifp);
1457 }
1458 
1459 static void
1460 vtnet_npoll(struct ifnet *ifp, struct ifpoll_info *info)
1461 {
1462 	struct vtnet_softc *sc = ifp->if_softc;
1463 	int i;
1464 
1465 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
1466 
1467 	if (info) {
1468 		int cpu;
1469 
1470 		info->ifpi_status.status_func = vtnet_npoll_status;
1471 		info->ifpi_status.serializer = &sc->vtnet_slz;
1472 
1473 		/* Use the same cpu for rx and tx. */
1474 		cpu = device_get_unit(device_get_parent(sc->vtnet_dev));
1475 		/* Shuffle a bit. */
1476 		cpu = (cpu * 61) % netisr_ncpus;
1477 		KKASSERT(cpu < netisr_ncpus);
1478 		info->ifpi_tx[cpu].poll_func = vtnet_npoll_tx;
1479 		info->ifpi_tx[cpu].arg = NULL;
1480 		info->ifpi_tx[cpu].serializer = &sc->vtnet_tx_slz;
1481 		ifq_set_cpuid(&ifp->if_snd, cpu);
1482 
1483 		info->ifpi_rx[cpu].poll_func = vtnet_npoll_rx;
1484 		info->ifpi_rx[cpu].arg = NULL;
1485 		info->ifpi_rx[cpu].serializer = &sc->vtnet_rx_slz;
1486 
1487 		for (i = 0; i < 3; i++)
1488 			lwkt_serialize_handler_disable(sc->serializes[i]);
1489 		vtnet_disable_rx_intr(sc);
1490 		vtnet_disable_tx_intr(sc);
1491 		for (i = 0; i < sc->vtnet_nintr; i++)
1492 			virtio_teardown_intr(sc->vtnet_dev, i);
1493 		if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS))
1494 			virtio_unbind_intr(sc->vtnet_dev, -1);
1495 		for (i = 0; i < 2; i++)
1496 			virtio_unbind_intr(sc->vtnet_dev, i);
1497 	} else {
1498 		int error;
1499 
1500 		ifq_set_cpuid(&ifp->if_snd,
1501 		    sc->vtnet_cpus[sc->vtnet_nintr - 1]);
1502 		for (i = 0; i < 3; i++)
1503 			lwkt_serialize_handler_enable(sc->serializes[i]);
1504 		for (i = 0; i < 2; i++) {
1505 			error = virtio_bind_intr(sc->vtnet_dev,
1506 			    sc->vtnet_irqmap[i].irq, i,
1507 			    sc->vtnet_irqmap[i].handler, sc);
1508 			if (error) {
1509 				device_printf(sc->vtnet_dev,
1510 				    "cannot re-bind virtqueue IRQs\n");
1511 			}
1512 		}
1513 		if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS)) {
1514 			error = virtio_bind_intr(sc->vtnet_dev, 0, -1,
1515 			    vtnet_config_intr, sc);
1516 			if (error) {
1517 				device_printf(sc->vtnet_dev,
1518 				    "cannot re-bind config_change IRQ\n");
1519 			}
1520 		}
1521 		for (i = 0; i < sc->vtnet_nintr; i++) {
1522 			error = virtio_setup_intr(sc->vtnet_dev, i,
1523 			    sc->vtnet_intr_slz[i]);
1524 			if (error) {
1525 				device_printf(sc->vtnet_dev,
1526 				    "cannot setup virtqueue interrupts\n");
1527 			}
1528 		}
1529 		vtnet_enable_rx_intr(sc);
1530 		vtnet_enable_tx_intr(sc);
1531 	}
1532 }
1533 
1534 #endif	/* IFPOLL_ENABLE */
1535 
1536 static void
1537 vtnet_vlan_tag_remove(struct mbuf *m)
1538 {
1539 	struct ether_vlan_header *evl;
1540 
1541 	evl = mtod(m, struct ether_vlan_header *);
1542 
1543 	m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1544 	m->m_flags |= M_VLANTAG;
1545 
1546 	/* Strip the 802.1Q header. */
1547 	bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1548 	    ETHER_HDR_LEN - ETHER_TYPE_LEN);
1549 	m_adj(m, ETHER_VLAN_ENCAP_LEN);
1550 }
1551 
1552 /*
1553  * Alternative method of doing receive checksum offloading. Rather
1554  * than parsing the received frame down to the IP header, use the
1555  * csum_offset to determine which CSUM_* flags are appropriate. We
1556  * can get by with doing this only because the checksum offsets are
1557  * unique for the things we care about.
1558  */
1559 static int
1560 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1561     struct virtio_net_hdr *hdr)
1562 {
1563 	struct ether_header *eh;
1564 	struct ether_vlan_header *evh;
1565 	struct udphdr *udp;
1566 	int csum_len;
1567 	uint16_t eth_type;
1568 
1569 	csum_len = hdr->csum_start + hdr->csum_offset;
1570 
1571 	if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1572 		return (1);
1573 	if (m->m_len < csum_len)
1574 		return (1);
1575 
1576 	eh = mtod(m, struct ether_header *);
1577 	eth_type = ntohs(eh->ether_type);
1578 	if (eth_type == ETHERTYPE_VLAN) {
1579 		evh = mtod(m, struct ether_vlan_header *);
1580 		eth_type = ntohs(evh->evl_proto);
1581 	}
1582 
1583 	if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1584 		sc->vtnet_stats.rx_csum_bad_ethtype++;
1585 		return (1);
1586 	}
1587 
1588 	/* Use the offset to determine the appropriate CSUM_* flags. */
1589 	switch (hdr->csum_offset) {
1590 	case offsetof(struct udphdr, uh_sum):
1591 		if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1592 			return (1);
1593 		udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1594 		if (udp->uh_sum == 0)
1595 			return (0);
1596 
1597 		/* FALLTHROUGH */
1598 
1599 	case offsetof(struct tcphdr, th_sum):
1600 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1601 		m->m_pkthdr.csum_data = 0xFFFF;
1602 		break;
1603 
1604 	default:
1605 		sc->vtnet_stats.rx_csum_bad_offset++;
1606 		return (1);
1607 	}
1608 
1609 	sc->vtnet_stats.rx_csum_offloaded++;
1610 
1611 	return (0);
1612 }
1613 
1614 static int
1615 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1616 {
1617 	struct ifnet *ifp;
1618 	struct virtqueue *vq;
1619 	struct mbuf *m, *m_tail;
1620 	int len;
1621 
1622 	ifp = sc->vtnet_ifp;
1623 	vq = sc->vtnet_rx_vq;
1624 	m_tail = m_head;
1625 
1626 	while (--nbufs > 0) {
1627 		m = virtqueue_dequeue(vq, &len);
1628 		if (m == NULL) {
1629 			ifp->if_ierrors++;
1630 			goto fail;
1631 		}
1632 
1633 		if (vtnet_newbuf(sc) != 0) {
1634 			ifp->if_iqdrops++;
1635 			vtnet_discard_rxbuf(sc, m);
1636 			if (nbufs > 1)
1637 				vtnet_discard_merged_rxbuf(sc, nbufs);
1638 			goto fail;
1639 		}
1640 
1641 		if (m->m_len < len)
1642 			len = m->m_len;
1643 
1644 		m->m_len = len;
1645 		m->m_flags &= ~M_PKTHDR;
1646 
1647 		m_head->m_pkthdr.len += len;
1648 		m_tail->m_next = m;
1649 		m_tail = m;
1650 	}
1651 
1652 	return (0);
1653 
1654 fail:
1655 	sc->vtnet_stats.rx_mergeable_failed++;
1656 	m_freem(m_head);
1657 
1658 	return (1);
1659 }
1660 
1661 static int
1662 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1663 {
1664 	struct virtio_net_hdr lhdr;
1665 	struct ifnet *ifp;
1666 	struct virtqueue *vq;
1667 	struct mbuf *m;
1668 	struct ether_header *eh;
1669 	struct virtio_net_hdr *hdr;
1670 	struct virtio_net_hdr_mrg_rxbuf *mhdr;
1671 	int len, deq, nbufs, adjsz, rx_npkts;
1672 
1673 	ifp = sc->vtnet_ifp;
1674 	vq = sc->vtnet_rx_vq;
1675 	hdr = &lhdr;
1676 	deq = 0;
1677 	rx_npkts = 0;
1678 
1679 	while (--count >= 0) {
1680 		m = virtqueue_dequeue(vq, &len);
1681 		if (m == NULL)
1682 			break;
1683 		deq++;
1684 
1685 		if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1686 			ifp->if_ierrors++;
1687 			vtnet_discard_rxbuf(sc, m);
1688 			continue;
1689 		}
1690 
1691 		if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1692 			nbufs = 1;
1693 			adjsz = sizeof(struct vtnet_rx_header);
1694 			/*
1695 			 * Account for our pad between the header and
1696 			 * the actual start of the frame.
1697 			 */
1698 			len += VTNET_RX_HEADER_PAD;
1699 		} else {
1700 			mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1701 			nbufs = mhdr->num_buffers;
1702 			adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1703 		}
1704 
1705 		if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1706 			ifp->if_iqdrops++;
1707 			vtnet_discard_rxbuf(sc, m);
1708 			if (nbufs > 1)
1709 				vtnet_discard_merged_rxbuf(sc, nbufs);
1710 			continue;
1711 		}
1712 
1713 		m->m_pkthdr.len = len;
1714 		m->m_pkthdr.rcvif = ifp;
1715 		m->m_pkthdr.csum_flags = 0;
1716 
1717 		if (nbufs > 1) {
1718 			if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1719 				continue;
1720 		}
1721 
1722 		ifp->if_ipackets++;
1723 
1724 		/*
1725 		 * Save copy of header before we strip it. For both mergeable
1726 		 * and non-mergeable, the VirtIO header is placed first in the
1727 		 * mbuf's data. We no longer need num_buffers, so always use a
1728 		 * virtio_net_hdr.
1729 		 */
1730 		memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1731 		m_adj(m, adjsz);
1732 
1733 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1734 			eh = mtod(m, struct ether_header *);
1735 			if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1736 				vtnet_vlan_tag_remove(m);
1737 
1738 				/*
1739 				 * With the 802.1Q header removed, update the
1740 				 * checksum starting location accordingly.
1741 				 */
1742 				if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1743 					hdr->csum_start -=
1744 					    ETHER_VLAN_ENCAP_LEN;
1745 			}
1746 		}
1747 
1748 		if (ifp->if_capenable & IFCAP_RXCSUM &&
1749 		    hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1750 			if (vtnet_rx_csum(sc, m, hdr) != 0)
1751 				sc->vtnet_stats.rx_csum_failed++;
1752 		}
1753 
1754 		rx_npkts++;
1755 		ifp->if_input(ifp, m, NULL, mycpuid);
1756 
1757 		/*
1758 		 * The interface may have been stopped while we were
1759 		 * passing the packet up the network stack.
1760 		 */
1761 		if ((ifp->if_flags & IFF_RUNNING) == 0)
1762 			break;
1763 	}
1764 
1765 	if (deq > 0)
1766 		virtqueue_notify(vq, NULL);
1767 
1768 	if (rx_npktsp != NULL)
1769 		*rx_npktsp = rx_npkts;
1770 
1771 	return (count > 0 ? 0 : EAGAIN);
1772 }
1773 
1774 static void
1775 vtnet_rx_msix_intr(void *xsc)
1776 {
1777 	struct vtnet_softc *sc;
1778 	struct ifnet *ifp;
1779 	int more;
1780 
1781 	sc = xsc;
1782 	ifp = sc->vtnet_ifp;
1783 
1784 	if (!virtqueue_pending(sc->vtnet_rx_vq))
1785 		return;
1786 
1787 	vtnet_disable_rx_intr(sc);
1788 next:
1789 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
1790 		vtnet_enable_rx_intr(sc);
1791 		return;
1792 	}
1793 
1794 	more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1795 	if (!more && vtnet_enable_rx_intr(sc) != 0) {
1796 		vtnet_disable_rx_intr(sc);
1797 		more = 1;
1798 	}
1799 
1800 	if (more) {
1801 		sc->vtnet_stats.rx_task_rescheduled++;
1802 		goto next;
1803 	}
1804 }
1805 
1806 static void
1807 vtnet_rx_vq_intr(void *xsc)
1808 {
1809 	struct vtnet_softc *sc = xsc;
1810 
1811 	lwkt_serialize_enter(&sc->vtnet_rx_slz);
1812 	vtnet_rx_msix_intr(xsc);
1813 	lwkt_serialize_exit(&sc->vtnet_rx_slz);
1814 }
1815 
1816 static void
1817 vtnet_enqueue_txhdr(struct vtnet_softc *sc, struct vtnet_tx_header *txhdr)
1818 {
1819 	bzero(txhdr, sizeof(*txhdr));
1820 	SLIST_INSERT_HEAD(&sc->vtnet_txhdr_free, txhdr, link);
1821 }
1822 
1823 static void
1824 vtnet_txeof(struct vtnet_softc *sc)
1825 {
1826 	struct virtqueue *vq;
1827 	struct ifnet *ifp;
1828 	struct vtnet_tx_header *txhdr;
1829 	int deq;
1830 
1831 	vq = sc->vtnet_tx_vq;
1832 	ifp = sc->vtnet_ifp;
1833 	deq = 0;
1834 
1835 	while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1836 		deq++;
1837 		ifp->if_opackets++;
1838 		m_freem(txhdr->vth_mbuf);
1839 		vtnet_enqueue_txhdr(sc, txhdr);
1840 	}
1841 
1842 	if (deq > 0) {
1843 		ifq_clr_oactive(&ifp->if_snd);
1844 		if (virtqueue_empty(vq))
1845 			sc->vtnet_tx_watchdog.wd_timer = 0;
1846 		else
1847 			sc->vtnet_tx_watchdog.wd_timer = VTNET_WATCHDOG_TIMEOUT;
1848 	}
1849 }
1850 
1851 static struct mbuf *
1852 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1853     struct virtio_net_hdr *hdr)
1854 {
1855 	struct ifnet *ifp;
1856 	struct ether_header *eh;
1857 	struct ether_vlan_header *evh;
1858 	struct ip *ip;
1859 	struct ip6_hdr *ip6;
1860 	struct tcphdr *tcp;
1861 	int ip_offset;
1862 	uint16_t eth_type, csum_start;
1863 	uint8_t ip_proto, gso_type;
1864 
1865 	ifp = sc->vtnet_ifp;
1866 	M_ASSERTPKTHDR(m);
1867 
1868 	ip_offset = sizeof(struct ether_header);
1869 	if (m->m_len < ip_offset) {
1870 		if ((m = m_pullup(m, ip_offset)) == NULL)
1871 			return (NULL);
1872 	}
1873 
1874 	eh = mtod(m, struct ether_header *);
1875 	eth_type = ntohs(eh->ether_type);
1876 	if (eth_type == ETHERTYPE_VLAN) {
1877 		ip_offset = sizeof(struct ether_vlan_header);
1878 		if (m->m_len < ip_offset) {
1879 			if ((m = m_pullup(m, ip_offset)) == NULL)
1880 				return (NULL);
1881 		}
1882 		evh = mtod(m, struct ether_vlan_header *);
1883 		eth_type = ntohs(evh->evl_proto);
1884 	}
1885 
1886 	switch (eth_type) {
1887 	case ETHERTYPE_IP:
1888 		if (m->m_len < ip_offset + sizeof(struct ip)) {
1889 			m = m_pullup(m, ip_offset + sizeof(struct ip));
1890 			if (m == NULL)
1891 				return (NULL);
1892 		}
1893 
1894 		ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1895 		ip_proto = ip->ip_p;
1896 		csum_start = ip_offset + (ip->ip_hl << 2);
1897 		gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1898 		break;
1899 
1900 	case ETHERTYPE_IPV6:
1901 		if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1902 			m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1903 			if (m == NULL)
1904 				return (NULL);
1905 		}
1906 
1907 		ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1908 		/*
1909 		 * XXX Assume no extension headers are present. Presently,
1910 		 * this will always be true in the case of TSO, and FreeBSD
1911 		 * does not perform checksum offloading of IPv6 yet.
1912 		 */
1913 		ip_proto = ip6->ip6_nxt;
1914 		csum_start = ip_offset + sizeof(struct ip6_hdr);
1915 		gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1916 		break;
1917 
1918 	default:
1919 		return (m);
1920 	}
1921 
1922 	if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1923 		hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1924 		hdr->csum_start = csum_start;
1925 		hdr->csum_offset = m->m_pkthdr.csum_data;
1926 
1927 		sc->vtnet_stats.tx_csum_offloaded++;
1928 	}
1929 
1930 	if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1931 		if (ip_proto != IPPROTO_TCP)
1932 			return (m);
1933 
1934 		if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1935 			m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1936 			if (m == NULL)
1937 				return (NULL);
1938 		}
1939 
1940 		tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1941 		hdr->gso_type = gso_type;
1942 		hdr->hdr_len = csum_start + (tcp->th_off << 2);
1943 		hdr->gso_size = m->m_pkthdr.tso_segsz;
1944 
1945 		if (tcp->th_flags & TH_CWR) {
1946 			/*
1947 			 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1948 			 * ECN support is only configurable globally with the
1949 			 * net.inet.tcp.ecn.enable sysctl knob.
1950 			 */
1951 			if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1952 				if_printf(ifp, "TSO with ECN not supported "
1953 				    "by host\n");
1954 				m_freem(m);
1955 				return (NULL);
1956 			}
1957 
1958 			hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1959 		}
1960 
1961 		sc->vtnet_stats.tx_tso_offloaded++;
1962 	}
1963 
1964 	return (m);
1965 }
1966 
1967 static int
1968 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1969     struct vtnet_tx_header *txhdr)
1970 {
1971 	struct sglist sg;
1972 	struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1973 	struct virtqueue *vq;
1974 	struct mbuf *m;
1975 	int error;
1976 
1977 	vq = sc->vtnet_tx_vq;
1978 	m = *m_head;
1979 
1980 	sglist_init(&sg, sc->vtnet_tx_nsegs, segs);
1981 	error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1982 	KASSERT(error == 0 && sg.sg_nseg == 1,
1983 	    ("%s: error %d adding header to sglist", __func__, error));
1984 
1985 	error = sglist_append_mbuf(&sg, m);
1986 	if (error) {
1987 		m = m_defrag(m, M_NOWAIT);
1988 		if (m == NULL)
1989 			goto fail;
1990 
1991 		*m_head = m;
1992 		sc->vtnet_stats.tx_defragged++;
1993 
1994 		error = sglist_append_mbuf(&sg, m);
1995 		if (error)
1996 			goto fail;
1997 	}
1998 
1999 	txhdr->vth_mbuf = m;
2000 	error = virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0);
2001 
2002 	return (error);
2003 
2004 fail:
2005 	sc->vtnet_stats.tx_defrag_failed++;
2006 	m_freem(*m_head);
2007 	*m_head = NULL;
2008 
2009 	return (ENOBUFS);
2010 }
2011 
2012 static struct mbuf *
2013 vtnet_vlan_tag_insert(struct mbuf *m)
2014 {
2015 	struct mbuf *n;
2016 	struct ether_vlan_header *evl;
2017 
2018 	if (M_WRITABLE(m) == 0) {
2019 		n = m_dup(m, M_NOWAIT);
2020 		m_freem(m);
2021 		if ((m = n) == NULL)
2022 			return (NULL);
2023 	}
2024 
2025 	M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
2026 	if (m == NULL)
2027 		return (NULL);
2028 	if (m->m_len < sizeof(struct ether_vlan_header)) {
2029 		m = m_pullup(m, sizeof(struct ether_vlan_header));
2030 		if (m == NULL)
2031 			return (NULL);
2032 	}
2033 
2034 	/* Insert 802.1Q header into the existing Ethernet header. */
2035 	evl = mtod(m, struct ether_vlan_header *);
2036 	bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
2037 	      (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
2038 	evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
2039 	evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
2040 	m->m_flags &= ~M_VLANTAG;
2041 
2042 	return (m);
2043 }
2044 
2045 static int
2046 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
2047 {
2048 	struct vtnet_tx_header *txhdr;
2049 	struct virtio_net_hdr *hdr;
2050 	struct mbuf *m;
2051 	int error;
2052 
2053 	txhdr = SLIST_FIRST(&sc->vtnet_txhdr_free);
2054 	if (txhdr == NULL)
2055 		return (ENOBUFS);
2056 	SLIST_REMOVE_HEAD(&sc->vtnet_txhdr_free, link);
2057 
2058 	/*
2059 	 * Always use the non-mergeable header to simplify things. When
2060 	 * the mergeable feature is negotiated, the num_buffers field
2061 	 * must be set to zero. We use vtnet_hdr_size later to enqueue
2062 	 * the correct header size to the host.
2063 	 */
2064 	hdr = &txhdr->vth_uhdr.hdr;
2065 	m = *m_head;
2066 
2067 	error = ENOBUFS;
2068 
2069 	if (m->m_flags & M_VLANTAG) {
2070 		//m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2071 		m = vtnet_vlan_tag_insert(m);
2072 		if ((*m_head = m) == NULL)
2073 			goto fail;
2074 		m->m_flags &= ~M_VLANTAG;
2075 	}
2076 
2077 	if (m->m_pkthdr.csum_flags != 0) {
2078 		m = vtnet_tx_offload(sc, m, hdr);
2079 		if ((*m_head = m) == NULL)
2080 			goto fail;
2081 	}
2082 
2083 	error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
2084 fail:
2085 	if (error != 0)
2086 		vtnet_enqueue_txhdr(sc, txhdr);
2087 	return (error);
2088 }
2089 
2090 static void
2091 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2092 {
2093 	struct vtnet_softc *sc;
2094 	struct virtqueue *vq;
2095 	struct mbuf *m0;
2096 	int enq;
2097 
2098 	sc = ifp->if_softc;
2099 	vq = sc->vtnet_tx_vq;
2100 	enq = 0;
2101 
2102 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2103 	ASSERT_SERIALIZED(&sc->vtnet_tx_slz);
2104 
2105 	if ((ifp->if_flags & (IFF_RUNNING)) !=
2106 	    IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
2107 		return;
2108 
2109 #ifdef VTNET_TX_INTR_MODERATION
2110 	if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
2111 		vtnet_txeof(sc);
2112 #endif
2113 
2114 	while (!ifsq_is_empty(ifsq)) {
2115 		if (virtqueue_full(vq)) {
2116 			ifsq_set_oactive(ifsq);
2117 			break;
2118 		}
2119 
2120 		m0 = ifsq_dequeue(ifsq);
2121 		if (m0 == NULL)
2122 			break;
2123 
2124 		if (vtnet_encap(sc, &m0) != 0) {
2125 			if (m0 == NULL)
2126 				break;
2127 			ifsq_prepend(ifsq, m0);
2128 			ifsq_set_oactive(ifsq);
2129 			break;
2130 		}
2131 
2132 		enq++;
2133 		ETHER_BPF_MTAP(ifp, m0);
2134 	}
2135 
2136 	if (enq > 0) {
2137 		virtqueue_notify(vq, NULL);
2138 		sc->vtnet_tx_watchdog.wd_timer = VTNET_WATCHDOG_TIMEOUT;
2139 	}
2140 }
2141 
2142 static void
2143 vtnet_tx_msix_intr(void *xsc)
2144 {
2145 	struct vtnet_softc *sc;
2146 	struct ifnet *ifp;
2147 	struct ifaltq_subque *ifsq;
2148 
2149 	sc = xsc;
2150 	ifp = sc->vtnet_ifp;
2151 	ifsq = ifq_get_subq_default(&ifp->if_snd);
2152 
2153 	if (!virtqueue_pending(sc->vtnet_tx_vq))
2154 		return;
2155 
2156 	vtnet_disable_tx_intr(sc);
2157 next:
2158 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
2159 		vtnet_enable_tx_intr(sc);
2160 		return;
2161 	}
2162 
2163 	vtnet_txeof(sc);
2164 
2165 	if (!ifsq_is_empty(ifsq))
2166 		ifsq_devstart(ifsq);
2167 
2168 	if (vtnet_enable_tx_intr(sc) != 0) {
2169 		vtnet_disable_tx_intr(sc);
2170 		sc->vtnet_stats.tx_task_rescheduled++;
2171 		goto next;
2172 	}
2173 }
2174 
2175 static void
2176 vtnet_tx_vq_intr(void *xsc)
2177 {
2178 	struct vtnet_softc *sc = xsc;
2179 
2180 	lwkt_serialize_enter(&sc->vtnet_tx_slz);
2181 	vtnet_tx_msix_intr(xsc);
2182 	lwkt_serialize_exit(&sc->vtnet_tx_slz);
2183 }
2184 
2185 static void
2186 vtnet_config_intr(void *arg)
2187 {
2188 	struct vtnet_softc *sc;
2189 
2190 	sc = arg;
2191 
2192 	vtnet_update_link_status(sc);
2193 }
2194 
2195 static void
2196 vtnet_stop(struct vtnet_softc *sc)
2197 {
2198 	device_t dev;
2199 	struct ifnet *ifp;
2200 
2201 	dev = sc->vtnet_dev;
2202 	ifp = sc->vtnet_ifp;
2203 
2204 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2205 
2206 	ifq_clr_oactive(&ifp->if_snd);
2207 	ifsq_watchdog_stop(&sc->vtnet_tx_watchdog);
2208 	ifp->if_flags &= ~(IFF_RUNNING);
2209 
2210 	vtnet_disable_rx_intr(sc);
2211 	vtnet_disable_tx_intr(sc);
2212 
2213 	/*
2214 	 * Stop the host VirtIO adapter. Note this will reset the host
2215 	 * adapter's state back to the pre-initialized state, so in
2216 	 * order to make the device usable again, we must drive it
2217 	 * through virtio_reinit() and virtio_reinit_complete().
2218 	 */
2219 	virtio_stop(dev);
2220 
2221 	sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2222 
2223 	vtnet_free_rx_mbufs(sc);
2224 	vtnet_free_tx_mbufs(sc);
2225 }
2226 
2227 static int
2228 vtnet_virtio_reinit(struct vtnet_softc *sc)
2229 {
2230 	device_t dev;
2231 	struct ifnet *ifp;
2232 	uint64_t features;
2233 	int error;
2234 
2235 	dev = sc->vtnet_dev;
2236 	ifp = sc->vtnet_ifp;
2237 	features = sc->vtnet_features;
2238 
2239 	/*
2240 	 * Re-negotiate with the host, removing any disabled receive
2241 	 * features. Transmit features are disabled only on our side
2242 	 * via if_capenable and if_hwassist.
2243 	 */
2244 
2245 	if (ifp->if_capabilities & IFCAP_RXCSUM) {
2246 		if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2247 			features &= ~VIRTIO_NET_F_GUEST_CSUM;
2248 	}
2249 
2250 #if 0	/* IFCAP_LRO doesn't exist in DragonFly. */
2251 	if (ifp->if_capabilities & IFCAP_LRO) {
2252 		if ((ifp->if_capenable & IFCAP_LRO) == 0)
2253 			features &= ~VTNET_LRO_FEATURES;
2254 	}
2255 #endif
2256 
2257 	if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2258 		if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2259 			features &= ~VIRTIO_NET_F_CTRL_VLAN;
2260 	}
2261 
2262 	error = virtio_reinit(dev, features);
2263 	if (error)
2264 		device_printf(dev, "virtio reinit error %d\n", error);
2265 
2266 	return (error);
2267 }
2268 
2269 static void
2270 vtnet_init(void *xsc)
2271 {
2272 	struct vtnet_softc *sc;
2273 	device_t dev;
2274 	struct ifnet *ifp;
2275 	int error;
2276 
2277 	sc = xsc;
2278 	dev = sc->vtnet_dev;
2279 	ifp = sc->vtnet_ifp;
2280 
2281 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2282 
2283 	if (ifp->if_flags & IFF_RUNNING)
2284 		return;
2285 
2286 	/* Stop host's adapter, cancel any pending I/O. */
2287 	vtnet_stop(sc);
2288 
2289 	/* Reinitialize the host device. */
2290 	error = vtnet_virtio_reinit(sc);
2291 	if (error) {
2292 		device_printf(dev,
2293 		    "reinitialization failed, stopping device...\n");
2294 		vtnet_stop(sc);
2295 		return;
2296 	}
2297 
2298 	/* Update host with assigned MAC address. */
2299 	bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2300 	vtnet_set_hwaddr(sc);
2301 
2302 	ifp->if_hwassist = 0;
2303 	if (ifp->if_capenable & IFCAP_TXCSUM)
2304 		ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2305 	if (ifp->if_capenable & IFCAP_TSO4)
2306 		ifp->if_hwassist |= CSUM_TSO;
2307 
2308 	error = vtnet_init_rx_vq(sc);
2309 	if (error) {
2310 		device_printf(dev,
2311 		    "cannot allocate mbufs for Rx virtqueue\n");
2312 		vtnet_stop(sc);
2313 		return;
2314 	}
2315 
2316 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2317 		if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2318 			/* Restore promiscuous and all-multicast modes. */
2319 			vtnet_rx_filter(sc);
2320 
2321 			/* Restore filtered MAC addresses. */
2322 			vtnet_rx_filter_mac(sc);
2323 		}
2324 
2325 		/* Restore VLAN filters. */
2326 		if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2327 			vtnet_rx_filter_vlan(sc);
2328 	}
2329 
2330 #ifdef IFPOLL_ENABLE
2331 	if (!(ifp->if_flags & IFF_NPOLLING))
2332 #endif
2333 	{
2334 		vtnet_enable_rx_intr(sc);
2335 		vtnet_enable_tx_intr(sc);
2336 	}
2337 
2338 	ifp->if_flags |= IFF_RUNNING;
2339 	ifq_clr_oactive(&ifp->if_snd);
2340 	ifsq_watchdog_start(&sc->vtnet_tx_watchdog);
2341 
2342 	virtio_reinit_complete(dev);
2343 
2344 	vtnet_update_link_status(sc);
2345 }
2346 
2347 static void
2348 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2349     struct sglist *sg, int readable, int writable)
2350 {
2351 	struct virtqueue *vq;
2352 	void *c;
2353 
2354 	vq = sc->vtnet_ctrl_vq;
2355 
2356 	ASSERT_IFNET_SERIALIZED_ALL(sc->vtnet_ifp);
2357 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2358 	    ("no control virtqueue"));
2359 	KASSERT(virtqueue_empty(vq),
2360 	    ("control command already enqueued"));
2361 
2362 	if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2363 		return;
2364 
2365 	/*
2366 	 * XXX We can safely drop the serializer between here, and the end of
2367 	 *     the function, when we can correctly sleep for this command to
2368 	 *     be finished.
2369 	 */
2370 	virtqueue_notify(vq, NULL);
2371 
2372 	/*
2373 	 * Poll until the command is complete. Previously, we would
2374 	 * sleep until the control virtqueue interrupt handler woke
2375 	 * us up, but dropping the VTNET_MTX leads to serialization
2376 	 * difficulties.
2377 	 *
2378 	 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2379 	 * vectors. Two of those vectors are needed for the Rx and Tx
2380 	 * virtqueues. We do not support sharing both a Vq and config
2381 	 * changed notification on the same MSIX vector.
2382 	 */
2383 	c = virtqueue_poll(vq, NULL);
2384 	KASSERT(c == cookie, ("unexpected control command response"));
2385 }
2386 
2387 static int
2388 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
2389 {
2390 	struct {
2391 		struct virtio_net_ctrl_hdr hdr __aligned(2);
2392 		uint8_t pad1;
2393 		char aligned_hwaddr[ETHER_ADDR_LEN] __aligned(8);
2394 		uint8_t pad2;
2395 		uint8_t ack;
2396 	} s;
2397 	struct sglist_seg segs[3];
2398 	struct sglist sg;
2399 	int error;
2400 
2401 	s.hdr.class = VIRTIO_NET_CTRL_MAC;
2402 	s.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
2403 	s.ack = VIRTIO_NET_ERR;
2404 
2405 	/* Copy the mac address into physically contiguous memory */
2406 	memcpy(s.aligned_hwaddr, hwaddr, ETHER_ADDR_LEN);
2407 
2408 	sglist_init(&sg, 3, segs);
2409 	error = 0;
2410 	error |= sglist_append(&sg, &s.hdr,
2411 	    sizeof(struct virtio_net_ctrl_hdr));
2412 	error |= sglist_append(&sg, s.aligned_hwaddr, ETHER_ADDR_LEN);
2413 	error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2414 	KASSERT(error == 0 && sg.sg_nseg == 3,
2415 	    ("%s: error %d adding set MAC msg to sglist", __func__, error));
2416 
2417 	vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2418 
2419 	return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2420 }
2421 
2422 static void
2423 vtnet_rx_filter(struct vtnet_softc *sc)
2424 {
2425 	device_t dev;
2426 	struct ifnet *ifp;
2427 
2428 	dev = sc->vtnet_dev;
2429 	ifp = sc->vtnet_ifp;
2430 
2431 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2432 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2433 	    ("CTRL_RX feature not negotiated"));
2434 
2435 	if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2436 		device_printf(dev, "cannot %s promiscuous mode\n",
2437 		    (ifp->if_flags & IFF_PROMISC) ? "enable" : "disable");
2438 
2439 	if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2440 		device_printf(dev, "cannot %s all-multicast mode\n",
2441 		    (ifp->if_flags & IFF_ALLMULTI) ? "enable" : "disable");
2442 }
2443 
2444 static int
2445 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2446 {
2447 	struct sglist_seg segs[3];
2448 	struct sglist sg;
2449 	struct {
2450 		struct virtio_net_ctrl_hdr hdr __aligned(2);
2451 		uint8_t pad1;
2452 		uint8_t onoff;
2453 		uint8_t pad2;
2454 		uint8_t ack;
2455 	} s;
2456 	int error;
2457 
2458 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2459 	    ("%s: CTRL_RX feature not negotiated", __func__));
2460 
2461 	s.hdr.class = VIRTIO_NET_CTRL_RX;
2462 	s.hdr.cmd = cmd;
2463 	s.onoff = !!on;
2464 	s.ack = VIRTIO_NET_ERR;
2465 
2466 	sglist_init(&sg, 3, segs);
2467 	error = 0;
2468 	error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2469 	error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
2470 	error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2471 	KASSERT(error == 0 && sg.sg_nseg == 3,
2472 	    ("%s: error %d adding Rx message to sglist", __func__, error));
2473 
2474 	vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2475 
2476 	return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2477 }
2478 
2479 static int
2480 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2481 {
2482 
2483 	return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2484 }
2485 
2486 static int
2487 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2488 {
2489 
2490 	return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2491 }
2492 
2493 static void
2494 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2495 {
2496 	struct virtio_net_ctrl_hdr hdr __aligned(2);
2497 	struct vtnet_mac_filter *filter;
2498 	struct sglist_seg segs[4];
2499 	struct sglist sg;
2500 	struct ifnet *ifp;
2501 	struct ifaddr *ifa;
2502 	struct ifaddr_container *ifac;
2503 	struct ifmultiaddr *ifma;
2504 	int ucnt, mcnt, promisc, allmulti, error;
2505 	uint8_t ack;
2506 
2507 	ifp = sc->vtnet_ifp;
2508 	ucnt = 0;
2509 	mcnt = 0;
2510 	promisc = 0;
2511 	allmulti = 0;
2512 
2513 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2514 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2515 	    ("%s: CTRL_RX feature not negotiated", __func__));
2516 
2517 	/* Use the MAC filtering table allocated in vtnet_attach. */
2518 	filter = sc->vtnet_macfilter;
2519 	memset(filter, 0, sizeof(struct vtnet_mac_filter));
2520 
2521 	/* Unicast MAC addresses: */
2522 	//if_addr_rlock(ifp);
2523 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2524 		ifa = ifac->ifa;
2525 		if (ifa->ifa_addr->sa_family != AF_LINK)
2526 			continue;
2527 		else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2528 		    sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
2529 			continue;
2530 		else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
2531 			promisc = 1;
2532 			break;
2533 		}
2534 
2535 		bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2536 		    &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2537 		ucnt++;
2538 	}
2539 	//if_addr_runlock(ifp);
2540 
2541 	if (promisc != 0) {
2542 		filter->vmf_unicast.nentries = 0;
2543 		if_printf(ifp, "more than %d MAC addresses assigned, "
2544 		    "falling back to promiscuous mode\n",
2545 		    VTNET_MAX_MAC_ENTRIES);
2546 	} else
2547 		filter->vmf_unicast.nentries = ucnt;
2548 
2549 	/* Multicast MAC addresses: */
2550 	//if_maddr_rlock(ifp);
2551 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2552 		if (ifma->ifma_addr->sa_family != AF_LINK)
2553 			continue;
2554 		else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
2555 			allmulti = 1;
2556 			break;
2557 		}
2558 
2559 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2560 		    &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2561 		mcnt++;
2562 	}
2563 	//if_maddr_runlock(ifp);
2564 
2565 	if (allmulti != 0) {
2566 		filter->vmf_multicast.nentries = 0;
2567 		if_printf(ifp, "more than %d multicast MAC addresses "
2568 		    "assigned, falling back to all-multicast mode\n",
2569 		    VTNET_MAX_MAC_ENTRIES);
2570 	} else
2571 		filter->vmf_multicast.nentries = mcnt;
2572 
2573 	if (promisc != 0 && allmulti != 0)
2574 		goto out;
2575 
2576 	hdr.class = VIRTIO_NET_CTRL_MAC;
2577 	hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2578 	ack = VIRTIO_NET_ERR;
2579 
2580 	sglist_init(&sg, 4, segs);
2581 	error = 0;
2582 	error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2583 	error |= sglist_append(&sg, &filter->vmf_unicast,
2584 	    sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
2585 	error |= sglist_append(&sg, &filter->vmf_multicast,
2586 	    sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
2587 	error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2588 	KASSERT(error == 0 && sg.sg_nseg == 4,
2589 	    ("%s: error %d adding MAC filter msg to sglist", __func__, error));
2590 
2591 	vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2592 
2593 	if (ack != VIRTIO_NET_OK)
2594 		if_printf(ifp, "error setting host MAC filter table\n");
2595 
2596 out:
2597 	if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
2598 		if_printf(ifp, "cannot enable promiscuous mode\n");
2599 	if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
2600 		if_printf(ifp, "cannot enable all-multicast mode\n");
2601 }
2602 
2603 static int
2604 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2605 {
2606 	struct sglist_seg segs[3];
2607 	struct sglist sg;
2608 	struct {
2609 		struct virtio_net_ctrl_hdr hdr __aligned(2);
2610 		uint8_t pad1;
2611 		uint16_t tag;
2612 		uint8_t pad2;
2613 		uint8_t ack;
2614 	} s;
2615 	int error;
2616 
2617 	s.hdr.class = VIRTIO_NET_CTRL_VLAN;
2618 	s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2619 	s.tag = tag;
2620 	s.ack = VIRTIO_NET_ERR;
2621 
2622 	sglist_init(&sg, 3, segs);
2623 	error = 0;
2624 	error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2625 	error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
2626 	error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2627 	KASSERT(error == 0 && sg.sg_nseg == 3,
2628 	    ("%s: error %d adding VLAN message to sglist", __func__, error));
2629 
2630 	vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2631 
2632 	return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2633 }
2634 
2635 static void
2636 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2637 {
2638 	uint32_t w;
2639 	uint16_t tag;
2640 	int i, bit, nvlans;
2641 
2642 	ASSERT_IFNET_SERIALIZED_ALL(sc->vtnet_ifp);
2643 	KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2644 	    ("%s: VLAN_FILTER feature not negotiated", __func__));
2645 
2646 	nvlans = sc->vtnet_nvlans;
2647 
2648 	/* Enable the filter for each configured VLAN. */
2649 	for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2650 		w = sc->vtnet_vlan_shadow[i];
2651 		while ((bit = ffs(w) - 1) != -1) {
2652 			w &= ~(1 << bit);
2653 			tag = sizeof(w) * CHAR_BIT * i + bit;
2654 			nvlans--;
2655 
2656 			if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
2657 				device_printf(sc->vtnet_dev,
2658 				    "cannot enable VLAN %d filter\n", tag);
2659 			}
2660 		}
2661 	}
2662 
2663 	KASSERT(nvlans == 0, ("VLAN count incorrect"));
2664 }
2665 
2666 static void
2667 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2668 {
2669 	struct ifnet *ifp;
2670 	int idx, bit;
2671 
2672 	ifp = sc->vtnet_ifp;
2673 	idx = (tag >> 5) & 0x7F;
2674 	bit = tag & 0x1F;
2675 
2676 	if (tag == 0 || tag > 4095)
2677 		return;
2678 
2679 	ifnet_serialize_all(ifp);
2680 
2681 	/* Update shadow VLAN table. */
2682 	if (add) {
2683 		sc->vtnet_nvlans++;
2684 		sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2685 	} else {
2686 		sc->vtnet_nvlans--;
2687 		sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2688 	}
2689 
2690 	if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
2691 	    vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2692 		device_printf(sc->vtnet_dev,
2693 		    "cannot %s VLAN %d %s the host filter table\n",
2694 		    add ? "add" : "remove", tag, add ? "to" : "from");
2695 	}
2696 
2697 	ifnet_deserialize_all(ifp);
2698 }
2699 
2700 static void
2701 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2702 {
2703 
2704 	if (ifp->if_softc != arg)
2705 		return;
2706 
2707 	vtnet_update_vlan_filter(arg, 1, tag);
2708 }
2709 
2710 static void
2711 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2712 {
2713 
2714 	if (ifp->if_softc != arg)
2715 		return;
2716 
2717 	vtnet_update_vlan_filter(arg, 0, tag);
2718 }
2719 
2720 static int
2721 vtnet_ifmedia_upd(struct ifnet *ifp)
2722 {
2723 	struct vtnet_softc *sc;
2724 	struct ifmedia *ifm;
2725 
2726 	sc = ifp->if_softc;
2727 	ifm = &sc->vtnet_media;
2728 
2729 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2730 		return (EINVAL);
2731 
2732 	return (0);
2733 }
2734 
2735 static void
2736 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2737 {
2738 	struct vtnet_softc *sc;
2739 
2740 	sc = ifp->if_softc;
2741 
2742 	ifmr->ifm_status = IFM_AVALID;
2743 	ifmr->ifm_active = IFM_ETHER;
2744 
2745 	if (vtnet_is_link_up(sc) != 0) {
2746 		ifmr->ifm_status |= IFM_ACTIVE;
2747 		ifmr->ifm_active |= VTNET_MEDIATYPE;
2748 	} else
2749 		ifmr->ifm_active |= IFM_NONE;
2750 }
2751 
2752 static void
2753 vtnet_add_statistics(struct vtnet_softc *sc)
2754 {
2755 	device_t dev;
2756 	struct vtnet_statistics *stats;
2757 	struct sysctl_ctx_list *ctx;
2758 	struct sysctl_oid *tree;
2759 	struct sysctl_oid_list *child;
2760 
2761 	dev = sc->vtnet_dev;
2762 	stats = &sc->vtnet_stats;
2763 	ctx = device_get_sysctl_ctx(dev);
2764 	tree = device_get_sysctl_tree(dev);
2765 	child = SYSCTL_CHILDREN(tree);
2766 
2767 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2768 	    CTLFLAG_RD, &stats->mbuf_alloc_failed, 0,
2769 	    "Mbuf cluster allocation failures");
2770 
2771 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
2772 	    CTLFLAG_RD, &stats->rx_frame_too_large, 0,
2773 	    "Received frame larger than the mbuf chain");
2774 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2775 	    CTLFLAG_RD, &stats->rx_enq_replacement_failed, 0,
2776 	    "Enqueuing the replacement receive mbuf failed");
2777 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
2778 	    CTLFLAG_RD, &stats->rx_mergeable_failed, 0,
2779 	    "Mergeable buffers receive failures");
2780 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2781 	    CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 0,
2782 	    "Received checksum offloaded buffer with unsupported "
2783 	    "Ethernet type");
2784 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2785 	    CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 0,
2786 	    "Received checksum offloaded buffer with incorrect IP protocol");
2787 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2788 	    CTLFLAG_RD, &stats->rx_csum_bad_offset, 0,
2789 	    "Received checksum offloaded buffer with incorrect offset");
2790 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
2791 	    CTLFLAG_RD, &stats->rx_csum_failed, 0,
2792 	    "Received buffer checksum offload failed");
2793 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
2794 	    CTLFLAG_RD, &stats->rx_csum_offloaded, 0,
2795 	    "Received buffer checksum offload succeeded");
2796 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
2797 	    CTLFLAG_RD, &stats->rx_task_rescheduled, 0,
2798 	    "Times the receive interrupt task rescheduled itself");
2799 
2800 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2801 	    CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 0,
2802 	    "Aborted transmit of checksum offloaded buffer with unknown "
2803 	    "Ethernet type");
2804 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2805 	    CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 0,
2806 	    "Aborted transmit of TSO buffer with unknown Ethernet type");
2807 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
2808 	    CTLFLAG_RD, &stats->tx_defragged, 0,
2809 	    "Transmit mbufs defragged");
2810 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
2811 	    CTLFLAG_RD, &stats->tx_defrag_failed, 0,
2812 	    "Aborted transmit of buffer because defrag failed");
2813 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
2814 	    CTLFLAG_RD, &stats->tx_csum_offloaded, 0,
2815 	    "Offloaded checksum of transmitted buffer");
2816 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
2817 	    CTLFLAG_RD, &stats->tx_tso_offloaded, 0,
2818 	    "Segmentation offload of transmitted buffer");
2819 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
2820 	    CTLFLAG_RD, &stats->tx_task_rescheduled, 0,
2821 	    "Times the transmit interrupt task rescheduled itself");
2822 }
2823 
2824 static int
2825 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2826 {
2827 
2828 	return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2829 }
2830 
2831 static void
2832 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2833 {
2834 
2835 	virtqueue_disable_intr(sc->vtnet_rx_vq);
2836 }
2837 
2838 static int
2839 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2840 {
2841 
2842 #ifdef VTNET_TX_INTR_MODERATION
2843 	return (0);
2844 #else
2845 	return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2846 #endif
2847 }
2848 
2849 static void
2850 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2851 {
2852 
2853 	virtqueue_disable_intr(sc->vtnet_tx_vq);
2854 }
2855