xref: /freebsd/sys/dev/virtio/network/if_vtnet.c (revision 413a368c90ea8621329ec702d46501ac7a4ffb4b)
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 <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/eventhandler.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/random.h>
43 #include <sys/sglist.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/taskqueue.h>
47 #include <sys/smp.h>
48 #include <machine/smp.h>
49 
50 #include <vm/uma.h>
51 
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
59 #include <net/if_vlan_var.h>
60 
61 #include <net/bpf.h>
62 
63 #include <netinet/in_systm.h>
64 #include <netinet/in.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/tcp.h>
70 #include <netinet/sctp.h>
71 
72 #include <machine/bus.h>
73 #include <machine/resource.h>
74 #include <sys/bus.h>
75 #include <sys/rman.h>
76 
77 #include <dev/virtio/virtio.h>
78 #include <dev/virtio/virtqueue.h>
79 #include <dev/virtio/network/virtio_net.h>
80 #include <dev/virtio/network/if_vtnetvar.h>
81 
82 #include "virtio_if.h"
83 
84 #include "opt_inet.h"
85 #include "opt_inet6.h"
86 
87 static int	vtnet_modevent(module_t, int, void *);
88 
89 static int	vtnet_probe(device_t);
90 static int	vtnet_attach(device_t);
91 static int	vtnet_detach(device_t);
92 static int	vtnet_suspend(device_t);
93 static int	vtnet_resume(device_t);
94 static int	vtnet_shutdown(device_t);
95 static int	vtnet_attach_completed(device_t);
96 static int	vtnet_config_change(device_t);
97 
98 static void	vtnet_negotiate_features(struct vtnet_softc *);
99 static void	vtnet_setup_features(struct vtnet_softc *);
100 static int	vtnet_init_rxq(struct vtnet_softc *, int);
101 static int	vtnet_init_txq(struct vtnet_softc *, int);
102 static int	vtnet_alloc_rxtx_queues(struct vtnet_softc *);
103 static void	vtnet_free_rxtx_queues(struct vtnet_softc *);
104 static int	vtnet_alloc_rx_filters(struct vtnet_softc *);
105 static void	vtnet_free_rx_filters(struct vtnet_softc *);
106 static int	vtnet_alloc_virtqueues(struct vtnet_softc *);
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);
110 
111 static int	vtnet_rxq_populate(struct vtnet_rxq *);
112 static void	vtnet_rxq_free_mbufs(struct vtnet_rxq *);
113 static struct mbuf *
114 		vtnet_rx_alloc_buf(struct vtnet_softc *, int , struct mbuf **);
115 static int	vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *,
116 		    struct mbuf *, int);
117 static int	vtnet_rxq_replace_buf(struct vtnet_rxq *, struct mbuf *, int);
118 static int	vtnet_rxq_enqueue_buf(struct vtnet_rxq *, struct mbuf *);
119 static int	vtnet_rxq_new_buf(struct vtnet_rxq *);
120 static int	vtnet_rxq_csum(struct vtnet_rxq *, struct mbuf *,
121 		     struct virtio_net_hdr *);
122 static void	vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *, int);
123 static void	vtnet_rxq_discard_buf(struct vtnet_rxq *, struct mbuf *);
124 static int	vtnet_rxq_merged_eof(struct vtnet_rxq *, struct mbuf *, int);
125 static void	vtnet_rxq_input(struct vtnet_rxq *, struct mbuf *,
126 		    struct virtio_net_hdr *);
127 static int	vtnet_rxq_eof(struct vtnet_rxq *);
128 static void	vtnet_rx_vq_intr(void *);
129 static void	vtnet_rxq_tq_intr(void *, int);
130 
131 static void	vtnet_txq_free_mbufs(struct vtnet_txq *);
132 static int	vtnet_txq_offload_ctx(struct vtnet_txq *, struct mbuf *,
133 		    int *, int *, int *);
134 static int	vtnet_txq_offload_tso(struct vtnet_txq *, struct mbuf *, int,
135 		    int, struct virtio_net_hdr *);
136 static struct mbuf *
137 		vtnet_txq_offload(struct vtnet_txq *, struct mbuf *,
138 		    struct virtio_net_hdr *);
139 static int	vtnet_txq_enqueue_buf(struct vtnet_txq *, struct mbuf **,
140 		    struct vtnet_tx_header *);
141 static int	vtnet_txq_encap(struct vtnet_txq *, struct mbuf **);
142 #ifdef VTNET_LEGACY_TX
143 static void	vtnet_start_locked(struct vtnet_txq *, struct ifnet *);
144 static void	vtnet_start(struct ifnet *);
145 #else
146 static int	vtnet_txq_mq_start_locked(struct vtnet_txq *, struct mbuf *);
147 static int	vtnet_txq_mq_start(struct ifnet *, struct mbuf *);
148 static void	vtnet_txq_tq_deferred(void *, int);
149 #endif
150 static void	vtnet_txq_start(struct vtnet_txq *);
151 static void	vtnet_txq_tq_intr(void *, int);
152 static void	vtnet_txq_eof(struct vtnet_txq *);
153 static void	vtnet_tx_vq_intr(void *);
154 static void	vtnet_tx_start_all(struct vtnet_softc *);
155 
156 #ifndef VTNET_LEGACY_TX
157 static void	vtnet_qflush(struct ifnet *);
158 #endif
159 
160 static int	vtnet_watchdog(struct vtnet_txq *);
161 static void	vtnet_rxq_accum_stats(struct vtnet_rxq *,
162 		    struct vtnet_rxq_stats *);
163 static void	vtnet_txq_accum_stats(struct vtnet_txq *,
164 		    struct vtnet_txq_stats *);
165 static void	vtnet_accumulate_stats(struct vtnet_softc *);
166 static void	vtnet_tick(void *);
167 
168 static void	vtnet_start_taskqueues(struct vtnet_softc *);
169 static void	vtnet_free_taskqueues(struct vtnet_softc *);
170 static void	vtnet_drain_taskqueues(struct vtnet_softc *);
171 
172 static void	vtnet_drain_rxtx_queues(struct vtnet_softc *);
173 static void	vtnet_stop_rendezvous(struct vtnet_softc *);
174 static void	vtnet_stop(struct vtnet_softc *);
175 static int	vtnet_virtio_reinit(struct vtnet_softc *);
176 static void	vtnet_init_rx_filters(struct vtnet_softc *);
177 static int	vtnet_init_rx_queues(struct vtnet_softc *);
178 static int	vtnet_init_tx_queues(struct vtnet_softc *);
179 static int	vtnet_init_rxtx_queues(struct vtnet_softc *);
180 static void	vtnet_set_active_vq_pairs(struct vtnet_softc *);
181 static int	vtnet_reinit(struct vtnet_softc *);
182 static void	vtnet_init_locked(struct vtnet_softc *);
183 static void	vtnet_init(void *);
184 
185 static void	vtnet_free_ctrl_vq(struct vtnet_softc *);
186 static void	vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
187 		    struct sglist *, int, int);
188 static int	vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
189 static int	vtnet_ctrl_mq_cmd(struct vtnet_softc *, uint16_t);
190 static int	vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
191 static int	vtnet_set_promisc(struct vtnet_softc *, int);
192 static int	vtnet_set_allmulti(struct vtnet_softc *, int);
193 static void	vtnet_attach_disable_promisc(struct vtnet_softc *);
194 static void	vtnet_rx_filter(struct vtnet_softc *);
195 static void	vtnet_rx_filter_mac(struct vtnet_softc *);
196 static int	vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
197 static void	vtnet_rx_filter_vlan(struct vtnet_softc *);
198 static void	vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t);
199 static void	vtnet_register_vlan(void *, struct ifnet *, uint16_t);
200 static void	vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
201 
202 static int	vtnet_is_link_up(struct vtnet_softc *);
203 static void	vtnet_update_link_status(struct vtnet_softc *);
204 static int	vtnet_ifmedia_upd(struct ifnet *);
205 static void	vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
206 static void	vtnet_get_hwaddr(struct vtnet_softc *);
207 static void	vtnet_set_hwaddr(struct vtnet_softc *);
208 static void	vtnet_vlan_tag_remove(struct mbuf *);
209 
210 static void	vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *,
211 		    struct sysctl_oid_list *, struct vtnet_rxq *);
212 static void	vtnet_setup_txq_sysctl(struct sysctl_ctx_list *,
213 		    struct sysctl_oid_list *, struct vtnet_txq *);
214 static void	vtnet_setup_queue_sysctl(struct vtnet_softc *);
215 static void	vtnet_setup_sysctl(struct vtnet_softc *);
216 
217 static int	vtnet_rxq_enable_intr(struct vtnet_rxq *);
218 static void	vtnet_rxq_disable_intr(struct vtnet_rxq *);
219 static int	vtnet_txq_enable_intr(struct vtnet_txq *);
220 static void	vtnet_txq_disable_intr(struct vtnet_txq *);
221 static void	vtnet_enable_rx_interrupts(struct vtnet_softc *);
222 static void	vtnet_enable_tx_interrupts(struct vtnet_softc *);
223 static void	vtnet_enable_interrupts(struct vtnet_softc *);
224 static void	vtnet_disable_rx_interrupts(struct vtnet_softc *);
225 static void	vtnet_disable_tx_interrupts(struct vtnet_softc *);
226 static void	vtnet_disable_interrupts(struct vtnet_softc *);
227 
228 static int	vtnet_tunable_int(struct vtnet_softc *, const char *, int);
229 
230 /* Tunables. */
231 static int vtnet_csum_disable = 0;
232 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
233 static int vtnet_tso_disable = 0;
234 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
235 static int vtnet_lro_disable = 0;
236 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
237 static int vtnet_mq_disable = 0;
238 TUNABLE_INT("hw.vtnet.mq_disable", &vtnet_mq_disable);
239 static int vtnet_mq_max_pairs = 0;
240 TUNABLE_INT("hw.vtnet.mq_max_pairs", &vtnet_mq_max_pairs);
241 static int vtnet_rx_process_limit = 512;
242 TUNABLE_INT("hw.vtnet.rx_process_limit", &vtnet_rx_process_limit);
243 
244 /*
245  * Reducing the number of transmit completed interrupts can improve
246  * performance. To do so, the define below keeps the Tx vq interrupt
247  * disabled and adds calls to vtnet_txeof() in the start and watchdog
248  * paths. The price to pay for this is the m_free'ing of transmitted
249  * mbufs may be delayed until the watchdog fires.
250  *
251  * BMV: Reintroduce this later as a run-time option, if it makes
252  * sense after the EVENT_IDX feature is supported.
253  *
254  * #define VTNET_TX_INTR_MODERATION
255  */
256 
257 static uma_zone_t vtnet_tx_header_zone;
258 
259 static struct virtio_feature_desc vtnet_feature_desc[] = {
260 	{ VIRTIO_NET_F_CSUM,		"TxChecksum"	},
261 	{ VIRTIO_NET_F_GUEST_CSUM,	"RxChecksum"	},
262 	{ VIRTIO_NET_F_MAC,		"MacAddress"	},
263 	{ VIRTIO_NET_F_GSO,		"TxAllGSO"	},
264 	{ VIRTIO_NET_F_GUEST_TSO4,	"RxTSOv4"	},
265 	{ VIRTIO_NET_F_GUEST_TSO6,	"RxTSOv6"	},
266 	{ VIRTIO_NET_F_GUEST_ECN,	"RxECN"		},
267 	{ VIRTIO_NET_F_GUEST_UFO,	"RxUFO"		},
268 	{ VIRTIO_NET_F_HOST_TSO4,	"TxTSOv4"	},
269 	{ VIRTIO_NET_F_HOST_TSO6,	"TxTSOv6"	},
270 	{ VIRTIO_NET_F_HOST_ECN,	"TxTSOECN"	},
271 	{ VIRTIO_NET_F_HOST_UFO,	"TxUFO"		},
272 	{ VIRTIO_NET_F_MRG_RXBUF,	"MrgRxBuf"	},
273 	{ VIRTIO_NET_F_STATUS,		"Status"	},
274 	{ VIRTIO_NET_F_CTRL_VQ,		"ControlVq"	},
275 	{ VIRTIO_NET_F_CTRL_RX,		"RxMode"	},
276 	{ VIRTIO_NET_F_CTRL_VLAN,	"VLanFilter"	},
277 	{ VIRTIO_NET_F_CTRL_RX_EXTRA,	"RxModeExtra"	},
278 	{ VIRTIO_NET_F_GUEST_ANNOUNCE,	"GuestAnnounce"	},
279 	{ VIRTIO_NET_F_MQ,		"Multiqueue"	},
280 	{ VIRTIO_NET_F_CTRL_MAC_ADDR,	"SetMacAddress"	},
281 
282 	{ 0, NULL }
283 };
284 
285 static device_method_t vtnet_methods[] = {
286 	/* Device methods. */
287 	DEVMETHOD(device_probe,			vtnet_probe),
288 	DEVMETHOD(device_attach,		vtnet_attach),
289 	DEVMETHOD(device_detach,		vtnet_detach),
290 	DEVMETHOD(device_suspend,		vtnet_suspend),
291 	DEVMETHOD(device_resume,		vtnet_resume),
292 	DEVMETHOD(device_shutdown,		vtnet_shutdown),
293 
294 	/* VirtIO methods. */
295 	DEVMETHOD(virtio_attach_completed,	vtnet_attach_completed),
296 	DEVMETHOD(virtio_config_change,		vtnet_config_change),
297 
298 	DEVMETHOD_END
299 };
300 
301 static driver_t vtnet_driver = {
302 	"vtnet",
303 	vtnet_methods,
304 	sizeof(struct vtnet_softc)
305 };
306 static devclass_t vtnet_devclass;
307 
308 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass,
309     vtnet_modevent, 0);
310 MODULE_VERSION(vtnet, 1);
311 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
312 
313 static int
314 vtnet_modevent(module_t mod, int type, void *unused)
315 {
316 	int error;
317 
318 	error = 0;
319 
320 	switch (type) {
321 	case MOD_LOAD:
322 		vtnet_tx_header_zone = uma_zcreate("vtnet_tx_hdr",
323 		    sizeof(struct vtnet_tx_header),
324 		    NULL, NULL, NULL, NULL, 0, 0);
325 		break;
326 	case MOD_QUIESCE:
327 	case MOD_UNLOAD:
328 		if (uma_zone_get_cur(vtnet_tx_header_zone) > 0)
329 			error = EBUSY;
330 		else if (type == MOD_UNLOAD) {
331 			uma_zdestroy(vtnet_tx_header_zone);
332 			vtnet_tx_header_zone = NULL;
333 		}
334 		break;
335 	case MOD_SHUTDOWN:
336 		break;
337 	default:
338 		error = EOPNOTSUPP;
339 		break;
340 	}
341 
342 	return (error);
343 }
344 
345 static int
346 vtnet_probe(device_t dev)
347 {
348 
349 	if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
350 		return (ENXIO);
351 
352 	device_set_desc(dev, "VirtIO Networking Adapter");
353 
354 	return (BUS_PROBE_DEFAULT);
355 }
356 
357 static int
358 vtnet_attach(device_t dev)
359 {
360 	struct vtnet_softc *sc;
361 	int error;
362 
363 	sc = device_get_softc(dev);
364 	sc->vtnet_dev = dev;
365 
366 	/* Register our feature descriptions. */
367 	virtio_set_feature_desc(dev, vtnet_feature_desc);
368 
369 	VTNET_CORE_LOCK_INIT(sc);
370 	callout_init_mtx(&sc->vtnet_tick_ch, VTNET_CORE_MTX(sc), 0);
371 
372 	vtnet_setup_sysctl(sc);
373 	vtnet_setup_features(sc);
374 
375 	error = vtnet_alloc_rx_filters(sc);
376 	if (error) {
377 		device_printf(dev, "cannot allocate Rx filters\n");
378 		goto fail;
379 	}
380 
381 	error = vtnet_alloc_rxtx_queues(sc);
382 	if (error) {
383 		device_printf(dev, "cannot allocate queues\n");
384 		goto fail;
385 	}
386 
387 	error = vtnet_alloc_virtqueues(sc);
388 	if (error) {
389 		device_printf(dev, "cannot allocate virtqueues\n");
390 		goto fail;
391 	}
392 
393 	error = vtnet_setup_interface(sc);
394 	if (error) {
395 		device_printf(dev, "cannot setup interface\n");
396 		goto fail;
397 	}
398 
399 	error = virtio_setup_intr(dev, INTR_TYPE_NET);
400 	if (error) {
401 		device_printf(dev, "cannot setup virtqueue interrupts\n");
402 		/* BMV: This will crash if during boot! */
403 		ether_ifdetach(sc->vtnet_ifp);
404 		goto fail;
405 	}
406 
407 	vtnet_start_taskqueues(sc);
408 
409 fail:
410 	if (error)
411 		vtnet_detach(dev);
412 
413 	return (error);
414 }
415 
416 static int
417 vtnet_detach(device_t dev)
418 {
419 	struct vtnet_softc *sc;
420 	struct ifnet *ifp;
421 
422 	sc = device_get_softc(dev);
423 	ifp = sc->vtnet_ifp;
424 
425 	if (device_is_attached(dev)) {
426 		VTNET_CORE_LOCK(sc);
427 		vtnet_stop(sc);
428 		VTNET_CORE_UNLOCK(sc);
429 
430 		callout_drain(&sc->vtnet_tick_ch);
431 		vtnet_drain_taskqueues(sc);
432 
433 		ether_ifdetach(ifp);
434 	}
435 
436 	vtnet_free_taskqueues(sc);
437 
438 	if (sc->vtnet_vlan_attach != NULL) {
439 		EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
440 		sc->vtnet_vlan_attach = NULL;
441 	}
442 	if (sc->vtnet_vlan_detach != NULL) {
443 		EVENTHANDLER_DEREGISTER(vlan_unconfg, sc->vtnet_vlan_detach);
444 		sc->vtnet_vlan_detach = NULL;
445 	}
446 
447 	ifmedia_removeall(&sc->vtnet_media);
448 
449 	if (ifp != NULL) {
450 		if_free(ifp);
451 		sc->vtnet_ifp = NULL;
452 	}
453 
454 	vtnet_free_rxtx_queues(sc);
455 	vtnet_free_rx_filters(sc);
456 
457 	if (sc->vtnet_ctrl_vq != NULL)
458 		vtnet_free_ctrl_vq(sc);
459 
460 	VTNET_CORE_LOCK_DESTROY(sc);
461 
462 	return (0);
463 }
464 
465 static int
466 vtnet_suspend(device_t dev)
467 {
468 	struct vtnet_softc *sc;
469 
470 	sc = device_get_softc(dev);
471 
472 	VTNET_CORE_LOCK(sc);
473 	vtnet_stop(sc);
474 	sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
475 	VTNET_CORE_UNLOCK(sc);
476 
477 	return (0);
478 }
479 
480 static int
481 vtnet_resume(device_t dev)
482 {
483 	struct vtnet_softc *sc;
484 	struct ifnet *ifp;
485 
486 	sc = device_get_softc(dev);
487 	ifp = sc->vtnet_ifp;
488 
489 	VTNET_CORE_LOCK(sc);
490 	if (ifp->if_flags & IFF_UP)
491 		vtnet_init_locked(sc);
492 	sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
493 	VTNET_CORE_UNLOCK(sc);
494 
495 	return (0);
496 }
497 
498 static int
499 vtnet_shutdown(device_t dev)
500 {
501 
502 	/*
503 	 * Suspend already does all of what we need to
504 	 * do here; we just never expect to be resumed.
505 	 */
506 	return (vtnet_suspend(dev));
507 }
508 
509 static int
510 vtnet_attach_completed(device_t dev)
511 {
512 
513 	vtnet_attach_disable_promisc(device_get_softc(dev));
514 
515 	return (0);
516 }
517 
518 static int
519 vtnet_config_change(device_t dev)
520 {
521 	struct vtnet_softc *sc;
522 
523 	sc = device_get_softc(dev);
524 
525 	VTNET_CORE_LOCK(sc);
526 	vtnet_update_link_status(sc);
527 	if (sc->vtnet_link_active != 0)
528 		vtnet_tx_start_all(sc);
529 	VTNET_CORE_UNLOCK(sc);
530 
531 	return (0);
532 }
533 
534 static void
535 vtnet_negotiate_features(struct vtnet_softc *sc)
536 {
537 	device_t dev;
538 	uint64_t mask, features;
539 
540 	dev = sc->vtnet_dev;
541 	mask = 0;
542 
543 	/*
544 	 * TSO and LRO are only available when their corresponding checksum
545 	 * offload feature is also negotiated.
546 	 */
547 	if (vtnet_tunable_int(sc, "csum_disable", vtnet_csum_disable)) {
548 		mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
549 		mask |= VTNET_TSO_FEATURES | VTNET_LRO_FEATURES;
550 	}
551 	if (vtnet_tunable_int(sc, "tso_disable", vtnet_tso_disable))
552 		mask |= VTNET_TSO_FEATURES;
553 	if (vtnet_tunable_int(sc, "lro_disable", vtnet_lro_disable))
554 		mask |= VTNET_LRO_FEATURES;
555 #ifndef VTNET_LEGACY_TX
556 	if (vtnet_tunable_int(sc, "mq_disable", vtnet_mq_disable))
557 		mask |= VIRTIO_NET_F_MQ;
558 #else
559 	mask |= VIRTIO_NET_F_MQ;
560 #endif
561 
562 	features = VTNET_FEATURES & ~mask;
563 	sc->vtnet_features = virtio_negotiate_features(dev, features);
564 
565 	if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
566 	    virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
567 		/*
568 		 * LRO without mergeable buffers requires special care. This
569 		 * is not ideal because every receive buffer must be large
570 		 * enough to hold the maximum TCP packet, the Ethernet header,
571 		 * and the header. This requires up to 34 descriptors with
572 		 * MCLBYTES clusters. If we do not have indirect descriptors,
573 		 * LRO is disabled since the virtqueue will not contain very
574 		 * many receive buffers.
575 		 */
576 		if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
577 			device_printf(dev,
578 			    "LRO disabled due to both mergeable buffers and "
579 			    "indirect descriptors not negotiated\n");
580 
581 			features &= ~VTNET_LRO_FEATURES;
582 			sc->vtnet_features =
583 			    virtio_negotiate_features(dev, features);
584 		} else
585 			sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
586 	}
587 }
588 
589 static void
590 vtnet_setup_features(struct vtnet_softc *sc)
591 {
592 	device_t dev;
593 	int max_pairs, max;
594 
595 	dev = sc->vtnet_dev;
596 
597 	vtnet_negotiate_features(sc);
598 
599 	if (virtio_with_feature(dev, VIRTIO_RING_F_EVENT_IDX))
600 		sc->vtnet_flags |= VTNET_FLAG_EVENT_IDX;
601 
602 	if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
603 		/* This feature should always be negotiated. */
604 		sc->vtnet_flags |= VTNET_FLAG_MAC;
605 	}
606 
607 	if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
608 		sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
609 		sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
610 	} else
611 		sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
612 
613 	if (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS)
614 		sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
615 	else if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG)
616 		sc->vtnet_rx_nsegs = VTNET_MAX_RX_SEGS;
617 	else
618 		sc->vtnet_rx_nsegs = VTNET_MIN_RX_SEGS;
619 
620 	if (virtio_with_feature(dev, VIRTIO_NET_F_GSO) ||
621 	    virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
622 	    virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
623 		sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
624 	else
625 		sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
626 
627 	if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
628 		sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
629 
630 		if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
631 			sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
632 		if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
633 			sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
634 		if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR))
635 			sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
636 	}
637 
638 	if (virtio_with_feature(dev, VIRTIO_NET_F_MQ) &&
639 	    sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
640 		max_pairs = virtio_read_dev_config_2(dev,
641 		    offsetof(struct virtio_net_config, max_virtqueue_pairs));
642 		if (max_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
643 		    max_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
644 			max_pairs = 1;
645 	} else
646 		max_pairs = 1;
647 
648 	if (max_pairs > 1) {
649 		/*
650 		 * Limit the maximum number of queue pairs to the number of
651 		 * CPUs or the configured maximum. The actual number of
652 		 * queues that get used may be less.
653 		 */
654 		max = vtnet_tunable_int(sc, "mq_max_pairs", vtnet_mq_max_pairs);
655 		if (max > 0 && max_pairs > max)
656 			max_pairs = max;
657 		if (max_pairs > mp_ncpus)
658 			max_pairs = mp_ncpus;
659 		if (max_pairs > VTNET_MAX_QUEUE_PAIRS)
660 			max_pairs = VTNET_MAX_QUEUE_PAIRS;
661 		if (max_pairs > 1)
662 			sc->vtnet_flags |= VTNET_FLAG_MULTIQ;
663 	}
664 
665 	sc->vtnet_max_vq_pairs = max_pairs;
666 }
667 
668 static int
669 vtnet_init_rxq(struct vtnet_softc *sc, int id)
670 {
671 	struct vtnet_rxq *rxq;
672 
673 	rxq = &sc->vtnet_rxqs[id];
674 
675 	snprintf(rxq->vtnrx_name, sizeof(rxq->vtnrx_name), "%s-rx%d",
676 	    device_get_nameunit(sc->vtnet_dev), id);
677 	mtx_init(&rxq->vtnrx_mtx, rxq->vtnrx_name, NULL, MTX_DEF);
678 
679 	rxq->vtnrx_sc = sc;
680 	rxq->vtnrx_id = id;
681 
682 	rxq->vtnrx_sg = sglist_alloc(sc->vtnet_rx_nsegs, M_NOWAIT);
683 	if (rxq->vtnrx_sg == NULL)
684 		return (ENOMEM);
685 
686 	TASK_INIT(&rxq->vtnrx_intrtask, 0, vtnet_rxq_tq_intr, rxq);
687 	rxq->vtnrx_tq = taskqueue_create(rxq->vtnrx_name, M_NOWAIT,
688 	    taskqueue_thread_enqueue, &rxq->vtnrx_tq);
689 
690 	return (rxq->vtnrx_tq == NULL ? ENOMEM : 0);
691 }
692 
693 static int
694 vtnet_init_txq(struct vtnet_softc *sc, int id)
695 {
696 	struct vtnet_txq *txq;
697 
698 	txq = &sc->vtnet_txqs[id];
699 
700 	snprintf(txq->vtntx_name, sizeof(txq->vtntx_name), "%s-tx%d",
701 	    device_get_nameunit(sc->vtnet_dev), id);
702 	mtx_init(&txq->vtntx_mtx, txq->vtntx_name, NULL, MTX_DEF);
703 
704 	txq->vtntx_sc = sc;
705 	txq->vtntx_id = id;
706 
707 	txq->vtntx_sg = sglist_alloc(sc->vtnet_tx_nsegs, M_NOWAIT);
708 	if (txq->vtntx_sg == NULL)
709 		return (ENOMEM);
710 
711 #ifndef VTNET_LEGACY_TX
712 	txq->vtntx_br = buf_ring_alloc(VTNET_DEFAULT_BUFRING_SIZE, M_DEVBUF,
713 	    M_NOWAIT, &txq->vtntx_mtx);
714 	if (txq->vtntx_br == NULL)
715 		return (ENOMEM);
716 
717 	TASK_INIT(&txq->vtntx_defrtask, 0, vtnet_txq_tq_deferred, txq);
718 #endif
719 	TASK_INIT(&txq->vtntx_intrtask, 0, vtnet_txq_tq_intr, txq);
720 	txq->vtntx_tq = taskqueue_create(txq->vtntx_name, M_NOWAIT,
721 	    taskqueue_thread_enqueue, &txq->vtntx_tq);
722 	if (txq->vtntx_tq == NULL)
723 		return (ENOMEM);
724 
725 	return (0);
726 }
727 
728 static int
729 vtnet_alloc_rxtx_queues(struct vtnet_softc *sc)
730 {
731 	int i, npairs, error;
732 
733 	npairs = sc->vtnet_max_vq_pairs;
734 
735 	sc->vtnet_rxqs = malloc(sizeof(struct vtnet_rxq) * npairs, M_DEVBUF,
736 	    M_NOWAIT | M_ZERO);
737 	sc->vtnet_txqs = malloc(sizeof(struct vtnet_txq) * npairs, M_DEVBUF,
738 	    M_NOWAIT | M_ZERO);
739 	if (sc->vtnet_rxqs == NULL || sc->vtnet_txqs == NULL)
740 		return (ENOMEM);
741 
742 	for (i = 0; i < npairs; i++) {
743 		error = vtnet_init_rxq(sc, i);
744 		if (error)
745 			return (error);
746 		error = vtnet_init_txq(sc, i);
747 		if (error)
748 			return (error);
749 	}
750 
751 	vtnet_setup_queue_sysctl(sc);
752 
753 	return (0);
754 }
755 
756 static void
757 vtnet_destroy_rxq(struct vtnet_rxq *rxq)
758 {
759 
760 	rxq->vtnrx_sc = NULL;
761 	rxq->vtnrx_id = -1;
762 
763 	if (rxq->vtnrx_sg != NULL) {
764 		sglist_free(rxq->vtnrx_sg);
765 		rxq->vtnrx_sg = NULL;
766 	}
767 
768 	if (mtx_initialized(&rxq->vtnrx_mtx) != 0)
769 		mtx_destroy(&rxq->vtnrx_mtx);
770 }
771 
772 static void
773 vtnet_destroy_txq(struct vtnet_txq *txq)
774 {
775 
776 	txq->vtntx_sc = NULL;
777 	txq->vtntx_id = -1;
778 
779 	if (txq->vtntx_sg != NULL) {
780 		sglist_free(txq->vtntx_sg);
781 		txq->vtntx_sg = NULL;
782 	}
783 
784 #ifndef VTNET_LEGACY_TX
785 	if (txq->vtntx_br != NULL) {
786 		buf_ring_free(txq->vtntx_br, M_DEVBUF);
787 		txq->vtntx_br = NULL;
788 	}
789 #endif
790 
791 	if (mtx_initialized(&txq->vtntx_mtx) != 0)
792 		mtx_destroy(&txq->vtntx_mtx);
793 }
794 
795 static void
796 vtnet_free_rxtx_queues(struct vtnet_softc *sc)
797 {
798 	int i;
799 
800 	if (sc->vtnet_rxqs != NULL) {
801 		for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
802 			vtnet_destroy_rxq(&sc->vtnet_rxqs[i]);
803 		free(sc->vtnet_rxqs, M_DEVBUF);
804 		sc->vtnet_rxqs = NULL;
805 	}
806 
807 	if (sc->vtnet_txqs != NULL) {
808 		for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
809 			vtnet_destroy_txq(&sc->vtnet_txqs[i]);
810 		free(sc->vtnet_txqs, M_DEVBUF);
811 		sc->vtnet_txqs = NULL;
812 	}
813 }
814 
815 static int
816 vtnet_alloc_rx_filters(struct vtnet_softc *sc)
817 {
818 
819 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
820 		sc->vtnet_mac_filter = malloc(sizeof(struct vtnet_mac_filter),
821 		    M_DEVBUF, M_NOWAIT | M_ZERO);
822 		if (sc->vtnet_mac_filter == NULL)
823 			return (ENOMEM);
824 	}
825 
826 	if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
827 		sc->vtnet_vlan_filter = malloc(sizeof(uint32_t) *
828 		    VTNET_VLAN_FILTER_NWORDS, M_DEVBUF, M_NOWAIT | M_ZERO);
829 		if (sc->vtnet_vlan_filter == NULL)
830 			return (ENOMEM);
831 	}
832 
833 	return (0);
834 }
835 
836 static void
837 vtnet_free_rx_filters(struct vtnet_softc *sc)
838 {
839 
840 	if (sc->vtnet_mac_filter != NULL) {
841 		free(sc->vtnet_mac_filter, M_DEVBUF);
842 		sc->vtnet_mac_filter = NULL;
843 	}
844 
845 	if (sc->vtnet_vlan_filter != NULL) {
846 		free(sc->vtnet_vlan_filter, M_DEVBUF);
847 		sc->vtnet_vlan_filter = NULL;
848 	}
849 }
850 
851 static int
852 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
853 {
854 	device_t dev;
855 	struct vq_alloc_info *info;
856 	struct vtnet_rxq *rxq;
857 	struct vtnet_txq *txq;
858 	int i, idx, flags, nvqs, error;
859 
860 	dev = sc->vtnet_dev;
861 	flags = 0;
862 
863 	nvqs = sc->vtnet_max_vq_pairs * 2;
864 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
865 		nvqs++;
866 
867 	info = malloc(sizeof(struct vq_alloc_info) * nvqs, M_TEMP, M_NOWAIT);
868 	if (info == NULL)
869 		return (ENOMEM);
870 
871 	for (i = 0, idx = 0; i < sc->vtnet_max_vq_pairs; i++, idx+=2) {
872 		rxq = &sc->vtnet_rxqs[i];
873 		VQ_ALLOC_INFO_INIT(&info[idx], sc->vtnet_rx_nsegs,
874 		    vtnet_rx_vq_intr, rxq, &rxq->vtnrx_vq,
875 		    "%s-%d rx", device_get_nameunit(dev), rxq->vtnrx_id);
876 
877 		txq = &sc->vtnet_txqs[i];
878 		VQ_ALLOC_INFO_INIT(&info[idx+1], sc->vtnet_tx_nsegs,
879 		    vtnet_tx_vq_intr, txq, &txq->vtntx_vq,
880 		    "%s-%d tx", device_get_nameunit(dev), txq->vtntx_id);
881 	}
882 
883 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
884 		VQ_ALLOC_INFO_INIT(&info[idx], 0, NULL, NULL,
885 		    &sc->vtnet_ctrl_vq, "%s ctrl", device_get_nameunit(dev));
886 	}
887 
888 	/*
889 	 * Enable interrupt binding if this is multiqueue. This only matters
890 	 * when per-vq MSIX is available.
891 	 */
892 	if (sc->vtnet_flags & VTNET_FLAG_MULTIQ)
893 		flags |= 0;
894 
895 	error = virtio_alloc_virtqueues(dev, flags, nvqs, info);
896 	free(info, M_TEMP);
897 
898 	return (error);
899 }
900 
901 static int
902 vtnet_setup_interface(struct vtnet_softc *sc)
903 {
904 	device_t dev;
905 	struct ifnet *ifp;
906 	int limit;
907 
908 	dev = sc->vtnet_dev;
909 
910 	ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
911 	if (ifp == NULL) {
912 		device_printf(dev, "cannot allocate ifnet structure\n");
913 		return (ENOSPC);
914 	}
915 
916 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
917 	ifp->if_baudrate = IF_Gbps(10);	/* Approx. */
918 	ifp->if_softc = sc;
919 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
920 	ifp->if_init = vtnet_init;
921 	ifp->if_ioctl = vtnet_ioctl;
922 
923 #ifndef VTNET_LEGACY_TX
924 	ifp->if_transmit = vtnet_txq_mq_start;
925 	ifp->if_qflush = vtnet_qflush;
926 #else
927 	struct virtqueue *vq = sc->vtnet_txqs[0].vtntx_vq;
928 	ifp->if_start = vtnet_start;
929 	IFQ_SET_MAXLEN(&ifp->if_snd, virtqueue_size(vq) - 1);
930 	ifp->if_snd.ifq_drv_maxlen = virtqueue_size(vq) - 1;
931 	IFQ_SET_READY(&ifp->if_snd);
932 #endif
933 
934 	ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
935 	    vtnet_ifmedia_sts);
936 	ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
937 	ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
938 
939 	/* Read (or generate) the MAC address for the adapter. */
940 	vtnet_get_hwaddr(sc);
941 
942 	ether_ifattach(ifp, sc->vtnet_hwaddr);
943 
944 	if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS))
945 		ifp->if_capabilities |= IFCAP_LINKSTATE;
946 
947 	/* Tell the upper layer(s) we support long frames. */
948 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
949 	ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
950 
951 	if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
952 		ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6;
953 
954 		if (virtio_with_feature(dev, VIRTIO_NET_F_GSO)) {
955 			ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6;
956 			sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
957 		} else {
958 			if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
959 				ifp->if_capabilities |= IFCAP_TSO4;
960 			if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
961 				ifp->if_capabilities |= IFCAP_TSO6;
962 			if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
963 				sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
964 		}
965 
966 		if (ifp->if_capabilities & IFCAP_TSO)
967 			ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
968 	}
969 
970 	if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM))
971 		ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
972 
973 	if (ifp->if_capabilities & IFCAP_HWCSUM) {
974 		/*
975 		 * VirtIO does not support VLAN tagging, but we can fake
976 		 * it by inserting and removing the 802.1Q header during
977 		 * transmit and receive. We are then able to do checksum
978 		 * offloading of VLAN frames.
979 		 */
980 		ifp->if_capabilities |=
981 		    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
982 	}
983 
984 	ifp->if_capenable = ifp->if_capabilities;
985 
986 	/*
987 	 * Capabilities after here are not enabled by default.
988 	 */
989 
990 	if (ifp->if_capabilities & IFCAP_RXCSUM) {
991 		if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
992 		    virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
993 			ifp->if_capabilities |= IFCAP_LRO;
994 	}
995 
996 	if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
997 		ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
998 
999 		sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1000 		    vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
1001 		sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1002 		    vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
1003 	}
1004 
1005 	limit = vtnet_tunable_int(sc, "rx_process_limit",
1006 	    vtnet_rx_process_limit);
1007 	if (limit < 0)
1008 		limit = INT_MAX;
1009 	sc->vtnet_rx_process_limit = limit;
1010 
1011 	return (0);
1012 }
1013 
1014 static int
1015 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
1016 {
1017 	struct ifnet *ifp;
1018 	int frame_size, clsize;
1019 
1020 	ifp = sc->vtnet_ifp;
1021 
1022 	if (new_mtu < ETHERMIN || new_mtu > VTNET_MAX_MTU)
1023 		return (EINVAL);
1024 
1025 	frame_size = sc->vtnet_hdr_size + sizeof(struct ether_vlan_header) +
1026 	    new_mtu;
1027 
1028 	/*
1029 	 * Based on the new MTU (and hence frame size) determine which
1030 	 * cluster size is most appropriate for the receive queues.
1031 	 */
1032 	if (frame_size <= MCLBYTES) {
1033 		clsize = MCLBYTES;
1034 	} else if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1035 		/* Avoid going past 9K jumbos. */
1036 		if (frame_size > MJUM9BYTES)
1037 			return (EINVAL);
1038 		clsize = MJUM9BYTES;
1039 	} else
1040 		clsize = MJUMPAGESIZE;
1041 
1042 	ifp->if_mtu = new_mtu;
1043 	sc->vtnet_rx_new_clsize = clsize;
1044 
1045 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1046 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1047 		vtnet_init_locked(sc);
1048 	}
1049 
1050 	return (0);
1051 }
1052 
1053 static int
1054 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1055 {
1056 	struct vtnet_softc *sc;
1057 	struct ifreq *ifr;
1058 	int reinit, mask, error;
1059 
1060 	sc = ifp->if_softc;
1061 	ifr = (struct ifreq *) data;
1062 	error = 0;
1063 
1064 	switch (cmd) {
1065 	case SIOCSIFMTU:
1066 		if (ifp->if_mtu != ifr->ifr_mtu) {
1067 			VTNET_CORE_LOCK(sc);
1068 			error = vtnet_change_mtu(sc, ifr->ifr_mtu);
1069 			VTNET_CORE_UNLOCK(sc);
1070 		}
1071 		break;
1072 
1073 	case SIOCSIFFLAGS:
1074 		VTNET_CORE_LOCK(sc);
1075 		if ((ifp->if_flags & IFF_UP) == 0) {
1076 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1077 				vtnet_stop(sc);
1078 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1079 			if ((ifp->if_flags ^ sc->vtnet_if_flags) &
1080 			    (IFF_PROMISC | IFF_ALLMULTI)) {
1081 				if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
1082 					vtnet_rx_filter(sc);
1083 				else
1084 					error = ENOTSUP;
1085 			}
1086 		} else
1087 			vtnet_init_locked(sc);
1088 
1089 		if (error == 0)
1090 			sc->vtnet_if_flags = ifp->if_flags;
1091 		VTNET_CORE_UNLOCK(sc);
1092 		break;
1093 
1094 	case SIOCADDMULTI:
1095 	case SIOCDELMULTI:
1096 		if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0)
1097 			break;
1098 		VTNET_CORE_LOCK(sc);
1099 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1100 			vtnet_rx_filter_mac(sc);
1101 		VTNET_CORE_UNLOCK(sc);
1102 		break;
1103 
1104 	case SIOCSIFMEDIA:
1105 	case SIOCGIFMEDIA:
1106 		error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1107 		break;
1108 
1109 	case SIOCSIFCAP:
1110 		VTNET_CORE_LOCK(sc);
1111 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1112 
1113 		if (mask & IFCAP_TXCSUM)
1114 			ifp->if_capenable ^= IFCAP_TXCSUM;
1115 		if (mask & IFCAP_TXCSUM_IPV6)
1116 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
1117 		if (mask & IFCAP_TSO4)
1118 			ifp->if_capenable ^= IFCAP_TSO4;
1119 		if (mask & IFCAP_TSO6)
1120 			ifp->if_capenable ^= IFCAP_TSO6;
1121 
1122 		if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO |
1123 		    IFCAP_VLAN_HWFILTER)) {
1124 			/* These Rx features require us to renegotiate. */
1125 			reinit = 1;
1126 
1127 			if (mask & IFCAP_RXCSUM)
1128 				ifp->if_capenable ^= IFCAP_RXCSUM;
1129 			if (mask & IFCAP_RXCSUM_IPV6)
1130 				ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
1131 			if (mask & IFCAP_LRO)
1132 				ifp->if_capenable ^= IFCAP_LRO;
1133 			if (mask & IFCAP_VLAN_HWFILTER)
1134 				ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
1135 		} else
1136 			reinit = 0;
1137 
1138 		if (mask & IFCAP_VLAN_HWTSO)
1139 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1140 		if (mask & IFCAP_VLAN_HWTAGGING)
1141 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1142 
1143 		if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1144 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1145 			vtnet_init_locked(sc);
1146 		}
1147 
1148 		VTNET_CORE_UNLOCK(sc);
1149 		VLAN_CAPABILITIES(ifp);
1150 
1151 		break;
1152 
1153 	default:
1154 		error = ether_ioctl(ifp, cmd, data);
1155 		break;
1156 	}
1157 
1158 	VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc);
1159 
1160 	return (error);
1161 }
1162 
1163 static int
1164 vtnet_rxq_populate(struct vtnet_rxq *rxq)
1165 {
1166 	struct virtqueue *vq;
1167 	int nbufs, error;
1168 
1169 	vq = rxq->vtnrx_vq;
1170 	error = ENOSPC;
1171 
1172 	for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1173 		error = vtnet_rxq_new_buf(rxq);
1174 		if (error)
1175 			break;
1176 	}
1177 
1178 	if (nbufs > 0) {
1179 		virtqueue_notify(vq);
1180 		/*
1181 		 * EMSGSIZE signifies the virtqueue did not have enough
1182 		 * entries available to hold the last mbuf. This is not
1183 		 * an error.
1184 		 */
1185 		if (error == EMSGSIZE)
1186 			error = 0;
1187 	}
1188 
1189 	return (error);
1190 }
1191 
1192 static void
1193 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq)
1194 {
1195 	struct virtqueue *vq;
1196 	struct mbuf *m;
1197 	int last;
1198 
1199 	vq = rxq->vtnrx_vq;
1200 	last = 0;
1201 
1202 	while ((m = virtqueue_drain(vq, &last)) != NULL)
1203 		m_freem(m);
1204 
1205 	KASSERT(virtqueue_empty(vq),
1206 	    ("%s: mbufs remaining in rx queue %p", __func__, rxq));
1207 }
1208 
1209 static struct mbuf *
1210 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1211 {
1212 	struct mbuf *m_head, *m_tail, *m;
1213 	int i, clsize;
1214 
1215 	clsize = sc->vtnet_rx_clsize;
1216 
1217 	KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1218 	    ("%s: chained mbuf %d request without LRO_NOMRG", __func__, nbufs));
1219 
1220 	m_head = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, clsize);
1221 	if (m_head == NULL)
1222 		goto fail;
1223 
1224 	m_head->m_len = clsize;
1225 	m_tail = m_head;
1226 
1227 	/* Allocate the rest of the chain. */
1228 	for (i = 1; i < nbufs; i++) {
1229 		m = m_getjcl(M_NOWAIT, MT_DATA, 0, clsize);
1230 		if (m == NULL)
1231 			goto fail;
1232 
1233 		m->m_len = clsize;
1234 		m_tail->m_next = m;
1235 		m_tail = m;
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 /*
1251  * Slow path for when LRO without mergeable buffers is negotiated.
1252  */
1253 static int
1254 vtnet_rxq_replace_lro_nomgr_buf(struct vtnet_rxq *rxq, struct mbuf *m0,
1255     int len0)
1256 {
1257 	struct vtnet_softc *sc;
1258 	struct mbuf *m, *m_prev;
1259 	struct mbuf *m_new, *m_tail;
1260 	int len, clsize, nreplace, error;
1261 
1262 	sc = rxq->vtnrx_sc;
1263 	clsize = sc->vtnet_rx_clsize;
1264 
1265 	m_prev = NULL;
1266 	m_tail = NULL;
1267 	nreplace = 0;
1268 
1269 	m = m0;
1270 	len = len0;
1271 
1272 	/*
1273 	 * Since these mbuf chains are so large, we avoid allocating an
1274 	 * entire replacement chain if possible. When the received frame
1275 	 * did not consume the entire chain, the unused mbufs are moved
1276 	 * to the replacement chain.
1277 	 */
1278 	while (len > 0) {
1279 		/*
1280 		 * Something is seriously wrong if we received a frame
1281 		 * larger than the chain. Drop it.
1282 		 */
1283 		if (m == NULL) {
1284 			sc->vtnet_stats.rx_frame_too_large++;
1285 			return (EMSGSIZE);
1286 		}
1287 
1288 		/* We always allocate the same cluster size. */
1289 		KASSERT(m->m_len == clsize,
1290 		    ("%s: mbuf size %d is not the cluster size %d",
1291 		    __func__, m->m_len, clsize));
1292 
1293 		m->m_len = MIN(m->m_len, len);
1294 		len -= m->m_len;
1295 
1296 		m_prev = m;
1297 		m = m->m_next;
1298 		nreplace++;
1299 	}
1300 
1301 	KASSERT(nreplace <= sc->vtnet_rx_nmbufs,
1302 	    ("%s: too many replacement mbufs %d max %d", __func__, nreplace,
1303 	    sc->vtnet_rx_nmbufs));
1304 
1305 	m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail);
1306 	if (m_new == NULL) {
1307 		m_prev->m_len = clsize;
1308 		return (ENOBUFS);
1309 	}
1310 
1311 	/*
1312 	 * Move any unused mbufs from the received chain onto the end
1313 	 * of the new chain.
1314 	 */
1315 	if (m_prev->m_next != NULL) {
1316 		m_tail->m_next = m_prev->m_next;
1317 		m_prev->m_next = NULL;
1318 	}
1319 
1320 	error = vtnet_rxq_enqueue_buf(rxq, m_new);
1321 	if (error) {
1322 		/*
1323 		 * BAD! We could not enqueue the replacement mbuf chain. We
1324 		 * must restore the m0 chain to the original state if it was
1325 		 * modified so we can subsequently discard it.
1326 		 *
1327 		 * NOTE: The replacement is suppose to be an identical copy
1328 		 * to the one just dequeued so this is an unexpected error.
1329 		 */
1330 		sc->vtnet_stats.rx_enq_replacement_failed++;
1331 
1332 		if (m_tail->m_next != NULL) {
1333 			m_prev->m_next = m_tail->m_next;
1334 			m_tail->m_next = NULL;
1335 		}
1336 
1337 		m_prev->m_len = clsize;
1338 		m_freem(m_new);
1339 	}
1340 
1341 	return (error);
1342 }
1343 
1344 static int
1345 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len)
1346 {
1347 	struct vtnet_softc *sc;
1348 	struct mbuf *m_new;
1349 	int error;
1350 
1351 	sc = rxq->vtnrx_sc;
1352 
1353 	KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1354 	    ("%s: chained mbuf without LRO_NOMRG", __func__));
1355 
1356 	if (m->m_next == NULL) {
1357 		/* Fast-path for the common case of just one mbuf. */
1358 		if (m->m_len < len)
1359 			return (EINVAL);
1360 
1361 		m_new = vtnet_rx_alloc_buf(sc, 1, NULL);
1362 		if (m_new == NULL)
1363 			return (ENOBUFS);
1364 
1365 		error = vtnet_rxq_enqueue_buf(rxq, m_new);
1366 		if (error) {
1367 			/*
1368 			 * The new mbuf is suppose to be an identical
1369 			 * copy of the one just dequeued so this is an
1370 			 * unexpected error.
1371 			 */
1372 			m_freem(m_new);
1373 			sc->vtnet_stats.rx_enq_replacement_failed++;
1374 		} else
1375 			m->m_len = len;
1376 	} else
1377 		error = vtnet_rxq_replace_lro_nomgr_buf(rxq, m, len);
1378 
1379 	return (error);
1380 }
1381 
1382 static int
1383 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1384 {
1385 	struct vtnet_softc *sc;
1386 	struct sglist *sg;
1387 	struct vtnet_rx_header *rxhdr;
1388 	uint8_t *mdata;
1389 	int offset, error;
1390 
1391 	sc = rxq->vtnrx_sc;
1392 	sg = rxq->vtnrx_sg;
1393 	mdata = mtod(m, uint8_t *);
1394 
1395 	VTNET_RXQ_LOCK_ASSERT(rxq);
1396 	KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG || m->m_next == NULL,
1397 	    ("%s: chained mbuf without LRO_NOMRG", __func__));
1398 	KASSERT(m->m_len == sc->vtnet_rx_clsize,
1399 	    ("%s: unexpected cluster size %d/%d", __func__, m->m_len,
1400 	     sc->vtnet_rx_clsize));
1401 
1402 	sglist_reset(sg);
1403 	if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1404 		MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr));
1405 		rxhdr = (struct vtnet_rx_header *) mdata;
1406 		sglist_append(sg, &rxhdr->vrh_hdr, sc->vtnet_hdr_size);
1407 		offset = sizeof(struct vtnet_rx_header);
1408 	} else
1409 		offset = 0;
1410 
1411 	sglist_append(sg, mdata + offset, m->m_len - offset);
1412 	if (m->m_next != NULL) {
1413 		error = sglist_append_mbuf(sg, m->m_next);
1414 		MPASS(error == 0);
1415 	}
1416 
1417 	error = virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg);
1418 
1419 	return (error);
1420 }
1421 
1422 static int
1423 vtnet_rxq_new_buf(struct vtnet_rxq *rxq)
1424 {
1425 	struct vtnet_softc *sc;
1426 	struct mbuf *m;
1427 	int error;
1428 
1429 	sc = rxq->vtnrx_sc;
1430 
1431 	m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL);
1432 	if (m == NULL)
1433 		return (ENOBUFS);
1434 
1435 	error = vtnet_rxq_enqueue_buf(rxq, m);
1436 	if (error)
1437 		m_freem(m);
1438 
1439 	return (error);
1440 }
1441 
1442 /*
1443  * Use the checksum offset in the VirtIO header to set the
1444  * correct CSUM_* flags.
1445  */
1446 static int
1447 vtnet_rxq_csum_by_offset(struct vtnet_rxq *rxq, struct mbuf *m,
1448     uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1449 {
1450 	struct vtnet_softc *sc;
1451 #if defined(INET) || defined(INET6)
1452 	int offset = hdr->csum_start + hdr->csum_offset;
1453 #endif
1454 
1455 	sc = rxq->vtnrx_sc;
1456 
1457 	/* Only do a basic sanity check on the offset. */
1458 	switch (eth_type) {
1459 #if defined(INET)
1460 	case ETHERTYPE_IP:
1461 		if (__predict_false(offset < ip_start + sizeof(struct ip)))
1462 			return (1);
1463 		break;
1464 #endif
1465 #if defined(INET6)
1466 	case ETHERTYPE_IPV6:
1467 		if (__predict_false(offset < ip_start + sizeof(struct ip6_hdr)))
1468 			return (1);
1469 		break;
1470 #endif
1471 	default:
1472 		sc->vtnet_stats.rx_csum_bad_ethtype++;
1473 		return (1);
1474 	}
1475 
1476 	/*
1477 	 * Use the offset to determine the appropriate CSUM_* flags. This is
1478 	 * a bit dirty, but we can get by with it since the checksum offsets
1479 	 * happen to be different. We assume the host host does not do IPv4
1480 	 * header checksum offloading.
1481 	 */
1482 	switch (hdr->csum_offset) {
1483 	case offsetof(struct udphdr, uh_sum):
1484 	case offsetof(struct tcphdr, th_sum):
1485 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1486 		m->m_pkthdr.csum_data = 0xFFFF;
1487 		break;
1488 	case offsetof(struct sctphdr, checksum):
1489 		m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1490 		break;
1491 	default:
1492 		sc->vtnet_stats.rx_csum_bad_offset++;
1493 		return (1);
1494 	}
1495 
1496 	return (0);
1497 }
1498 
1499 static int
1500 vtnet_rxq_csum_by_parse(struct vtnet_rxq *rxq, struct mbuf *m,
1501     uint16_t eth_type, int ip_start, struct virtio_net_hdr *hdr)
1502 {
1503 	struct vtnet_softc *sc;
1504 	int offset, proto;
1505 
1506 	sc = rxq->vtnrx_sc;
1507 
1508 	switch (eth_type) {
1509 #if defined(INET)
1510 	case ETHERTYPE_IP: {
1511 		struct ip *ip;
1512 		if (__predict_false(m->m_len < ip_start + sizeof(struct ip)))
1513 			return (1);
1514 		ip = (struct ip *)(m->m_data + ip_start);
1515 		proto = ip->ip_p;
1516 		offset = ip_start + (ip->ip_hl << 2);
1517 		break;
1518 	}
1519 #endif
1520 #if defined(INET6)
1521 	case ETHERTYPE_IPV6:
1522 		if (__predict_false(m->m_len < ip_start +
1523 		    sizeof(struct ip6_hdr)))
1524 			return (1);
1525 		offset = ip6_lasthdr(m, ip_start, IPPROTO_IPV6, &proto);
1526 		if (__predict_false(offset < 0))
1527 			return (1);
1528 		break;
1529 #endif
1530 	default:
1531 		sc->vtnet_stats.rx_csum_bad_ethtype++;
1532 		return (1);
1533 	}
1534 
1535 	switch (proto) {
1536 	case IPPROTO_TCP:
1537 		if (__predict_false(m->m_len < offset + sizeof(struct tcphdr)))
1538 			return (1);
1539 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1540 		m->m_pkthdr.csum_data = 0xFFFF;
1541 		break;
1542 	case IPPROTO_UDP:
1543 		if (__predict_false(m->m_len < offset + sizeof(struct udphdr)))
1544 			return (1);
1545 		m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1546 		m->m_pkthdr.csum_data = 0xFFFF;
1547 		break;
1548 	case IPPROTO_SCTP:
1549 		if (__predict_false(m->m_len < offset + sizeof(struct sctphdr)))
1550 			return (1);
1551 		m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1552 		break;
1553 	default:
1554 		/*
1555 		 * For the remaining protocols, FreeBSD does not support
1556 		 * checksum offloading, so the checksum will be recomputed.
1557 		 */
1558 #if 0
1559 		if_printf(sc->vtnet_ifp, "cksum offload of unsupported "
1560 		    "protocol eth_type=%#x proto=%d csum_start=%d "
1561 		    "csum_offset=%d\n", __func__, eth_type, proto,
1562 		    hdr->csum_start, hdr->csum_offset);
1563 #endif
1564 		break;
1565 	}
1566 
1567 	return (0);
1568 }
1569 
1570 /*
1571  * Set the appropriate CSUM_* flags. Unfortunately, the information
1572  * provided is not directly useful to us. The VirtIO header gives the
1573  * offset of the checksum, which is all Linux needs, but this is not
1574  * how FreeBSD does things. We are forced to peek inside the packet
1575  * a bit.
1576  *
1577  * It would be nice if VirtIO gave us the L4 protocol or if FreeBSD
1578  * could accept the offsets and let the stack figure it out.
1579  */
1580 static int
1581 vtnet_rxq_csum(struct vtnet_rxq *rxq, struct mbuf *m,
1582     struct virtio_net_hdr *hdr)
1583 {
1584 	struct ether_header *eh;
1585 	struct ether_vlan_header *evh;
1586 	uint16_t eth_type;
1587 	int offset, error;
1588 
1589 	eh = mtod(m, struct ether_header *);
1590 	eth_type = ntohs(eh->ether_type);
1591 	if (eth_type == ETHERTYPE_VLAN) {
1592 		/* BMV: We should handle nested VLAN tags too. */
1593 		evh = mtod(m, struct ether_vlan_header *);
1594 		eth_type = ntohs(evh->evl_proto);
1595 		offset = sizeof(struct ether_vlan_header);
1596 	} else
1597 		offset = sizeof(struct ether_header);
1598 
1599 	if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1600 		error = vtnet_rxq_csum_by_offset(rxq, m, eth_type, offset, hdr);
1601 	else
1602 		error = vtnet_rxq_csum_by_parse(rxq, m, eth_type, offset, hdr);
1603 
1604 	return (error);
1605 }
1606 
1607 static void
1608 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs)
1609 {
1610 	struct mbuf *m;
1611 
1612 	while (--nbufs > 0) {
1613 		m = virtqueue_dequeue(rxq->vtnrx_vq, NULL);
1614 		if (m == NULL)
1615 			break;
1616 		vtnet_rxq_discard_buf(rxq, m);
1617 	}
1618 }
1619 
1620 static void
1621 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1622 {
1623 	int error;
1624 
1625 	/*
1626 	 * Requeue the discarded mbuf. This should always be successful
1627 	 * since it was just dequeued.
1628 	 */
1629 	error = vtnet_rxq_enqueue_buf(rxq, m);
1630 	KASSERT(error == 0,
1631 	    ("%s: cannot requeue discarded mbuf %d", __func__, error));
1632 }
1633 
1634 static int
1635 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs)
1636 {
1637 	struct vtnet_softc *sc;
1638 	struct ifnet *ifp;
1639 	struct virtqueue *vq;
1640 	struct mbuf *m, *m_tail;
1641 	int len;
1642 
1643 	sc = rxq->vtnrx_sc;
1644 	vq = rxq->vtnrx_vq;
1645 	ifp = sc->vtnet_ifp;
1646 	m_tail = m_head;
1647 
1648 	while (--nbufs > 0) {
1649 		m = virtqueue_dequeue(vq, &len);
1650 		if (m == NULL) {
1651 			rxq->vtnrx_stats.vrxs_ierrors++;
1652 			goto fail;
1653 		}
1654 
1655 		if (vtnet_rxq_new_buf(rxq) != 0) {
1656 			rxq->vtnrx_stats.vrxs_iqdrops++;
1657 			vtnet_rxq_discard_buf(rxq, m);
1658 			if (nbufs > 1)
1659 				vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1660 			goto fail;
1661 		}
1662 
1663 		if (m->m_len < len)
1664 			len = m->m_len;
1665 
1666 		m->m_len = len;
1667 		m->m_flags &= ~M_PKTHDR;
1668 
1669 		m_head->m_pkthdr.len += len;
1670 		m_tail->m_next = m;
1671 		m_tail = m;
1672 	}
1673 
1674 	return (0);
1675 
1676 fail:
1677 	sc->vtnet_stats.rx_mergeable_failed++;
1678 	m_freem(m_head);
1679 
1680 	return (1);
1681 }
1682 
1683 static void
1684 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m,
1685     struct virtio_net_hdr *hdr)
1686 {
1687 	struct vtnet_softc *sc;
1688 	struct ifnet *ifp;
1689 	struct ether_header *eh;
1690 
1691 	sc = rxq->vtnrx_sc;
1692 	ifp = sc->vtnet_ifp;
1693 
1694 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1695 		eh = mtod(m, struct ether_header *);
1696 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1697 			vtnet_vlan_tag_remove(m);
1698 			/*
1699 			 * With the 802.1Q header removed, update the
1700 			 * checksum starting location accordingly.
1701 			 */
1702 			if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1703 				hdr->csum_start -= ETHER_VLAN_ENCAP_LEN;
1704 		}
1705 	}
1706 
1707 	m->m_pkthdr.flowid = rxq->vtnrx_id;
1708 	m->m_flags |= M_FLOWID;
1709 
1710 	/*
1711 	 * BMV: FreeBSD does not have the UNNECESSARY and PARTIAL checksum
1712 	 * distinction that Linux does. Need to reevaluate if performing
1713 	 * offloading for the NEEDS_CSUM case is really appropriate.
1714 	 */
1715 	if (hdr->flags & (VIRTIO_NET_HDR_F_NEEDS_CSUM |
1716 	    VIRTIO_NET_HDR_F_DATA_VALID)) {
1717 		if (vtnet_rxq_csum(rxq, m, hdr) == 0)
1718 			rxq->vtnrx_stats.vrxs_csum++;
1719 		else
1720 			rxq->vtnrx_stats.vrxs_csum_failed++;
1721 	}
1722 
1723 	rxq->vtnrx_stats.vrxs_ipackets++;
1724 	rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len;
1725 
1726 	VTNET_RXQ_UNLOCK(rxq);
1727 	(*ifp->if_input)(ifp, m);
1728 	VTNET_RXQ_LOCK(rxq);
1729 }
1730 
1731 static int
1732 vtnet_rxq_eof(struct vtnet_rxq *rxq)
1733 {
1734 	struct virtio_net_hdr lhdr, *hdr;
1735 	struct vtnet_softc *sc;
1736 	struct ifnet *ifp;
1737 	struct virtqueue *vq;
1738 	struct mbuf *m;
1739 	struct virtio_net_hdr_mrg_rxbuf *mhdr;
1740 	int len, deq, nbufs, adjsz, count;
1741 
1742 	sc = rxq->vtnrx_sc;
1743 	vq = rxq->vtnrx_vq;
1744 	ifp = sc->vtnet_ifp;
1745 	hdr = &lhdr;
1746 	deq = 0;
1747 	count = sc->vtnet_rx_process_limit;
1748 
1749 	VTNET_RXQ_LOCK_ASSERT(rxq);
1750 
1751 	while (count-- > 0) {
1752 		m = virtqueue_dequeue(vq, &len);
1753 		if (m == NULL)
1754 			break;
1755 		deq++;
1756 
1757 		if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1758 			rxq->vtnrx_stats.vrxs_ierrors++;
1759 			vtnet_rxq_discard_buf(rxq, m);
1760 			continue;
1761 		}
1762 
1763 		if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1764 			nbufs = 1;
1765 			adjsz = sizeof(struct vtnet_rx_header);
1766 			/*
1767 			 * Account for our pad inserted between the header
1768 			 * and the actual start of the frame.
1769 			 */
1770 			len += VTNET_RX_HEADER_PAD;
1771 		} else {
1772 			mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1773 			nbufs = mhdr->num_buffers;
1774 			adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1775 		}
1776 
1777 		if (vtnet_rxq_replace_buf(rxq, m, len) != 0) {
1778 			rxq->vtnrx_stats.vrxs_iqdrops++;
1779 			vtnet_rxq_discard_buf(rxq, m);
1780 			if (nbufs > 1)
1781 				vtnet_rxq_discard_merged_bufs(rxq, nbufs);
1782 			continue;
1783 		}
1784 
1785 		m->m_pkthdr.len = len;
1786 		m->m_pkthdr.rcvif = ifp;
1787 		m->m_pkthdr.csum_flags = 0;
1788 
1789 		if (nbufs > 1) {
1790 			/* Dequeue the rest of chain. */
1791 			if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0)
1792 				continue;
1793 		}
1794 
1795 		/*
1796 		 * Save copy of header before we strip it. For both mergeable
1797 		 * and non-mergeable, the header is at the beginning of the
1798 		 * mbuf data. We no longer need num_buffers, so always use a
1799 		 * regular header.
1800 		 *
1801 		 * BMV: Is this memcpy() expensive? We know the mbuf data is
1802 		 * still valid even after the m_adj().
1803 		 */
1804 		memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1805 		m_adj(m, adjsz);
1806 
1807 		vtnet_rxq_input(rxq, m, hdr);
1808 
1809 		/* Must recheck after dropping the Rx lock. */
1810 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1811 			break;
1812 	}
1813 
1814 	if (deq > 0)
1815 		virtqueue_notify(vq);
1816 
1817 	return (count > 0 ? 0 : EAGAIN);
1818 }
1819 
1820 static void
1821 vtnet_rx_vq_intr(void *xrxq)
1822 {
1823 	struct vtnet_softc *sc;
1824 	struct vtnet_rxq *rxq;
1825 	struct ifnet *ifp;
1826 	int tries, more;
1827 
1828 	rxq = xrxq;
1829 	sc = rxq->vtnrx_sc;
1830 	ifp = sc->vtnet_ifp;
1831 	tries = 0;
1832 
1833 	if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) {
1834 		/*
1835 		 * Ignore this interrupt. Either this is a spurious interrupt
1836 		 * or multiqueue without per-VQ MSIX so every queue needs to
1837 		 * be polled (a brain dead configuration we could try harder
1838 		 * to avoid).
1839 		 */
1840 		vtnet_rxq_disable_intr(rxq);
1841 		return;
1842 	}
1843 
1844 	VTNET_RXQ_LOCK(rxq);
1845 
1846 again:
1847 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1848 		VTNET_RXQ_UNLOCK(rxq);
1849 		return;
1850 	}
1851 
1852 	more = vtnet_rxq_eof(rxq);
1853 	if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1854 		if (!more)
1855 			vtnet_rxq_disable_intr(rxq);
1856 		/*
1857 		 * This is an occasional condition or race (when !more),
1858 		 * so retry a few times before scheduling the taskqueue.
1859 		 */
1860 		if (tries++ < VTNET_INTR_DISABLE_RETRIES)
1861 			goto again;
1862 
1863 		VTNET_RXQ_UNLOCK(rxq);
1864 		rxq->vtnrx_stats.vrxs_rescheduled++;
1865 		taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1866 	} else
1867 		VTNET_RXQ_UNLOCK(rxq);
1868 }
1869 
1870 static void
1871 vtnet_rxq_tq_intr(void *xrxq, int pending)
1872 {
1873 	struct vtnet_softc *sc;
1874 	struct vtnet_rxq *rxq;
1875 	struct ifnet *ifp;
1876 	int more;
1877 
1878 	rxq = xrxq;
1879 	sc = rxq->vtnrx_sc;
1880 	ifp = sc->vtnet_ifp;
1881 
1882 	VTNET_RXQ_LOCK(rxq);
1883 
1884 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1885 		VTNET_RXQ_UNLOCK(rxq);
1886 		return;
1887 	}
1888 
1889 	more = vtnet_rxq_eof(rxq);
1890 	if (more || vtnet_rxq_enable_intr(rxq) != 0) {
1891 		if (!more)
1892 			vtnet_rxq_disable_intr(rxq);
1893 		rxq->vtnrx_stats.vrxs_rescheduled++;
1894 		taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
1895 	}
1896 
1897 	VTNET_RXQ_UNLOCK(rxq);
1898 }
1899 
1900 static void
1901 vtnet_txq_free_mbufs(struct vtnet_txq *txq)
1902 {
1903 	struct virtqueue *vq;
1904 	struct vtnet_tx_header *txhdr;
1905 	int last;
1906 
1907 	vq = txq->vtntx_vq;
1908 	last = 0;
1909 
1910 	while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1911 		m_freem(txhdr->vth_mbuf);
1912 		uma_zfree(vtnet_tx_header_zone, txhdr);
1913 	}
1914 
1915 	KASSERT(virtqueue_empty(vq),
1916 	    ("%s: mbufs remaining in tx queue %p", __func__, txq));
1917 }
1918 
1919 /*
1920  * BMV: Much of this can go away once we finally have offsets in
1921  * the mbuf packet header. Bug andre@.
1922  */
1923 static int
1924 vtnet_txq_offload_ctx(struct vtnet_txq *txq, struct mbuf *m,
1925     int *etype, int *proto, int *start)
1926 {
1927 	struct vtnet_softc *sc;
1928 	struct ether_vlan_header *evh;
1929 	int offset;
1930 
1931 	sc = txq->vtntx_sc;
1932 
1933 	evh = mtod(m, struct ether_vlan_header *);
1934 	if (evh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1935 		/* BMV: We should handle nested VLAN tags too. */
1936 		*etype = ntohs(evh->evl_proto);
1937 		offset = sizeof(struct ether_vlan_header);
1938 	} else {
1939 		*etype = ntohs(evh->evl_encap_proto);
1940 		offset = sizeof(struct ether_header);
1941 	}
1942 
1943 	switch (*etype) {
1944 #if defined(INET)
1945 	case ETHERTYPE_IP: {
1946 		struct ip *ip, iphdr;
1947 		if (__predict_false(m->m_len < offset + sizeof(struct ip))) {
1948 			m_copydata(m, offset, sizeof(struct ip),
1949 			    (caddr_t) &iphdr);
1950 			ip = &iphdr;
1951 		} else
1952 			ip = (struct ip *)(m->m_data + offset);
1953 		*proto = ip->ip_p;
1954 		*start = offset + (ip->ip_hl << 2);
1955 		break;
1956 	}
1957 #endif
1958 #if defined(INET6)
1959 	case ETHERTYPE_IPV6:
1960 		*proto = -1;
1961 		*start = ip6_lasthdr(m, offset, IPPROTO_IPV6, proto);
1962 		/* Assert the network stack sent us a valid packet. */
1963 		KASSERT(*start > offset,
1964 		    ("%s: mbuf %p start %d offset %d proto %d", __func__, m,
1965 		    *start, offset, *proto));
1966 		break;
1967 #endif
1968 	default:
1969 		sc->vtnet_stats.tx_csum_bad_ethtype++;
1970 		return (EINVAL);
1971 	}
1972 
1973 	return (0);
1974 }
1975 
1976 static int
1977 vtnet_txq_offload_tso(struct vtnet_txq *txq, struct mbuf *m, int eth_type,
1978     int offset, struct virtio_net_hdr *hdr)
1979 {
1980 	static struct timeval lastecn;
1981 	static int curecn;
1982 	struct vtnet_softc *sc;
1983 	struct tcphdr *tcp, tcphdr;
1984 
1985 	sc = txq->vtntx_sc;
1986 
1987 	if (__predict_false(m->m_len < offset + sizeof(struct tcphdr))) {
1988 		m_copydata(m, offset, sizeof(struct tcphdr), (caddr_t) &tcphdr);
1989 		tcp = &tcphdr;
1990 	} else
1991 		tcp = (struct tcphdr *)(m->m_data + offset);
1992 
1993 	hdr->hdr_len = offset + (tcp->th_off << 2);
1994 	hdr->gso_size = m->m_pkthdr.tso_segsz;
1995 	hdr->gso_type = eth_type == ETHERTYPE_IP ? VIRTIO_NET_HDR_GSO_TCPV4 :
1996 	    VIRTIO_NET_HDR_GSO_TCPV6;
1997 
1998 	if (tcp->th_flags & TH_CWR) {
1999 		/*
2000 		 * Drop if VIRTIO_NET_F_HOST_ECN was not negotiated. In FreeBSD,
2001 		 * ECN support is not on a per-interface basis, but globally via
2002 		 * the net.inet.tcp.ecn.enable sysctl knob. The default is off.
2003 		 */
2004 		if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
2005 			if (ppsratecheck(&lastecn, &curecn, 1))
2006 				if_printf(sc->vtnet_ifp,
2007 				    "TSO with ECN not negotiated with host\n");
2008 			return (ENOTSUP);
2009 		}
2010 		hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2011 	}
2012 
2013 	txq->vtntx_stats.vtxs_tso++;
2014 
2015 	return (0);
2016 }
2017 
2018 static struct mbuf *
2019 vtnet_txq_offload(struct vtnet_txq *txq, struct mbuf *m,
2020     struct virtio_net_hdr *hdr)
2021 {
2022 	struct vtnet_softc *sc;
2023 	int flags, etype, csum_start, proto, error;
2024 
2025 	sc = txq->vtntx_sc;
2026 	flags = m->m_pkthdr.csum_flags;
2027 
2028 	error = vtnet_txq_offload_ctx(txq, m, &etype, &proto, &csum_start);
2029 	if (error)
2030 		goto drop;
2031 
2032 	if ((etype == ETHERTYPE_IP && flags & VTNET_CSUM_OFFLOAD) ||
2033 	    (etype == ETHERTYPE_IPV6 && flags & VTNET_CSUM_OFFLOAD_IPV6)) {
2034 		/*
2035 		 * We could compare the IP protocol vs the CSUM_ flag too,
2036 		 * but that really should not be necessary.
2037 		 */
2038 		hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
2039 		hdr->csum_start = csum_start;
2040 		hdr->csum_offset = m->m_pkthdr.csum_data;
2041 		txq->vtntx_stats.vtxs_csum++;
2042 	}
2043 
2044 	if (flags & CSUM_TSO) {
2045 		if (__predict_false(proto != IPPROTO_TCP)) {
2046 			/* Likely failed to correctly parse the mbuf. */
2047 			sc->vtnet_stats.tx_tso_not_tcp++;
2048 			goto drop;
2049 		}
2050 
2051 		KASSERT(hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM,
2052 		    ("%s: mbuf %p TSO without checksum offload %#x",
2053 		    __func__, m, flags));
2054 
2055 		error = vtnet_txq_offload_tso(txq, m, etype, csum_start, hdr);
2056 		if (error)
2057 			goto drop;
2058 	}
2059 
2060 	return (m);
2061 
2062 drop:
2063 	m_freem(m);
2064 	return (NULL);
2065 }
2066 
2067 static int
2068 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head,
2069     struct vtnet_tx_header *txhdr)
2070 {
2071 	struct vtnet_softc *sc;
2072 	struct virtqueue *vq;
2073 	struct sglist *sg;
2074 	struct mbuf *m;
2075 	int error;
2076 
2077 	sc = txq->vtntx_sc;
2078 	vq = txq->vtntx_vq;
2079 	sg = txq->vtntx_sg;
2080 	m = *m_head;
2081 
2082 	sglist_reset(sg);
2083 	error = sglist_append(sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
2084 	KASSERT(error == 0 && sg->sg_nseg == 1,
2085 	    ("%s: error %d adding header to sglist", __func__, error));
2086 
2087 	error = sglist_append_mbuf(sg, m);
2088 	if (error) {
2089 		m = m_defrag(m, M_NOWAIT);
2090 		if (m == NULL)
2091 			goto fail;
2092 
2093 		*m_head = m;
2094 		sc->vtnet_stats.tx_defragged++;
2095 
2096 		error = sglist_append_mbuf(sg, m);
2097 		if (error)
2098 			goto fail;
2099 	}
2100 
2101 	txhdr->vth_mbuf = m;
2102 	error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0);
2103 
2104 	return (error);
2105 
2106 fail:
2107 	sc->vtnet_stats.tx_defrag_failed++;
2108 	m_freem(*m_head);
2109 	*m_head = NULL;
2110 
2111 	return (ENOBUFS);
2112 }
2113 
2114 static int
2115 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head)
2116 {
2117 	struct vtnet_tx_header *txhdr;
2118 	struct virtio_net_hdr *hdr;
2119 	struct mbuf *m;
2120 	int error;
2121 
2122 	m = *m_head;
2123 	M_ASSERTPKTHDR(m);
2124 
2125 	txhdr = uma_zalloc(vtnet_tx_header_zone, M_NOWAIT | M_ZERO);
2126 	if (txhdr == NULL) {
2127 		m_freem(m);
2128 		*m_head = NULL;
2129 		return (ENOMEM);
2130 	}
2131 
2132 	/*
2133 	 * Always use the non-mergeable header, regardless if the feature
2134 	 * was negotiated. For transmit, num_buffers is always zero. The
2135 	 * vtnet_hdr_size is used to enqueue the correct header size.
2136 	 */
2137 	hdr = &txhdr->vth_uhdr.hdr;
2138 
2139 	if (m->m_flags & M_VLANTAG) {
2140 		m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2141 		if ((*m_head = m) == NULL) {
2142 			error = ENOBUFS;
2143 			goto fail;
2144 		}
2145 		m->m_flags &= ~M_VLANTAG;
2146 	}
2147 
2148 	if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) {
2149 		m = vtnet_txq_offload(txq, m, hdr);
2150 		if ((*m_head = m) == NULL) {
2151 			error = ENOBUFS;
2152 			goto fail;
2153 		}
2154 	}
2155 
2156 	error = vtnet_txq_enqueue_buf(txq, m_head, txhdr);
2157 	if (error == 0)
2158 		return (0);
2159 
2160 fail:
2161 	uma_zfree(vtnet_tx_header_zone, txhdr);
2162 
2163 	return (error);
2164 }
2165 
2166 #ifdef VTNET_LEGACY_TX
2167 
2168 static void
2169 vtnet_start_locked(struct vtnet_txq *txq, struct ifnet *ifp)
2170 {
2171 	struct vtnet_softc *sc;
2172 	struct virtqueue *vq;
2173 	struct mbuf *m0;
2174 	int enq;
2175 
2176 	sc = txq->vtntx_sc;
2177 	vq = txq->vtntx_vq;
2178 	enq = 0;
2179 
2180 	VTNET_TXQ_LOCK_ASSERT(txq);
2181 
2182 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2183 	    sc->vtnet_link_active == 0)
2184 		return;
2185 
2186 	vtnet_txq_eof(txq);
2187 
2188 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
2189 		if (virtqueue_full(vq))
2190 			break;
2191 
2192 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2193 		if (m0 == NULL)
2194 			break;
2195 
2196 		if (vtnet_txq_encap(txq, &m0) != 0) {
2197 			if (m0 != NULL)
2198 				IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2199 			break;
2200 		}
2201 
2202 		enq++;
2203 		ETHER_BPF_MTAP(ifp, m0);
2204 	}
2205 
2206 	if (enq > 0) {
2207 		virtqueue_notify(vq);
2208 		txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
2209 	}
2210 }
2211 
2212 static void
2213 vtnet_start(struct ifnet *ifp)
2214 {
2215 	struct vtnet_softc *sc;
2216 	struct vtnet_txq *txq;
2217 
2218 	sc = ifp->if_softc;
2219 	txq = &sc->vtnet_txqs[0];
2220 
2221 	VTNET_TXQ_LOCK(txq);
2222 	vtnet_start_locked(txq, ifp);
2223 	VTNET_TXQ_UNLOCK(txq);
2224 }
2225 
2226 #else /* !VTNET_LEGACY_TX */
2227 
2228 static int
2229 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m)
2230 {
2231 	struct vtnet_softc *sc;
2232 	struct virtqueue *vq;
2233 	struct buf_ring *br;
2234 	struct ifnet *ifp;
2235 	int enq, error;
2236 
2237 	sc = txq->vtntx_sc;
2238 	vq = txq->vtntx_vq;
2239 	br = txq->vtntx_br;
2240 	ifp = sc->vtnet_ifp;
2241 	enq = 0;
2242 	error = 0;
2243 
2244 	VTNET_TXQ_LOCK_ASSERT(txq);
2245 
2246 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2247 	    sc->vtnet_link_active == 0) {
2248 		if (m != NULL)
2249 			error = drbr_enqueue(ifp, br, m);
2250 		return (error);
2251 	}
2252 
2253 	if (m != NULL) {
2254 		error = drbr_enqueue(ifp, br, m);
2255 		if (error)
2256 			return (error);
2257 	}
2258 
2259 	vtnet_txq_eof(txq);
2260 
2261 	while ((m = drbr_peek(ifp, br)) != NULL) {
2262 		if (virtqueue_full(vq)) {
2263 			drbr_putback(ifp, br, m);
2264 			break;
2265 		}
2266 
2267 		error = vtnet_txq_encap(txq, &m);
2268 		if (error) {
2269 			if (m != NULL)
2270 				drbr_putback(ifp, br, m);
2271 			else
2272 				drbr_advance(ifp, br);
2273 			break;
2274 		}
2275 		drbr_advance(ifp, br);
2276 
2277 		enq++;
2278 		ETHER_BPF_MTAP(ifp, m);
2279 	}
2280 
2281 	if (enq > 0) {
2282 		virtqueue_notify(vq);
2283 		txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
2284 	}
2285 
2286 	return (0);
2287 }
2288 
2289 static int
2290 vtnet_txq_mq_start(struct ifnet *ifp, struct mbuf *m)
2291 {
2292 	struct vtnet_softc *sc;
2293 	struct vtnet_txq *txq;
2294 	int i, npairs, error;
2295 
2296 	sc = ifp->if_softc;
2297 	npairs = sc->vtnet_act_vq_pairs;
2298 
2299 	if (m->m_flags & M_FLOWID)
2300 		i = m->m_pkthdr.flowid % npairs;
2301 	else
2302 		i = curcpu % npairs;
2303 
2304 	txq = &sc->vtnet_txqs[i];
2305 
2306 	if (VTNET_TXQ_TRYLOCK(txq) != 0) {
2307 		error = vtnet_txq_mq_start_locked(txq, m);
2308 		VTNET_TXQ_UNLOCK(txq);
2309 	} else {
2310 		error = drbr_enqueue(ifp, txq->vtntx_br, m);
2311 		taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask);
2312 	}
2313 
2314 	return (error);
2315 }
2316 
2317 static void
2318 vtnet_txq_tq_deferred(void *xtxq, int pending)
2319 {
2320 	struct vtnet_softc *sc;
2321 	struct vtnet_txq *txq;
2322 
2323 	txq = xtxq;
2324 	sc = txq->vtntx_sc;
2325 
2326 	VTNET_TXQ_LOCK(txq);
2327 	if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br))
2328 		vtnet_txq_mq_start_locked(txq, NULL);
2329 	VTNET_TXQ_UNLOCK(txq);
2330 }
2331 
2332 #endif /* VTNET_LEGACY_TX */
2333 
2334 static void
2335 vtnet_txq_start(struct vtnet_txq *txq)
2336 {
2337 	struct vtnet_softc *sc;
2338 	struct ifnet *ifp;
2339 
2340 	sc = txq->vtntx_sc;
2341 	ifp = sc->vtnet_ifp;
2342 
2343 #ifdef VTNET_LEGACY_TX
2344 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2345 		vtnet_start_locked(txq, ifp);
2346 #else
2347 	if (!drbr_empty(ifp, txq->vtntx_br))
2348 		vtnet_txq_mq_start_locked(txq, NULL);
2349 #endif
2350 }
2351 
2352 static void
2353 vtnet_txq_tq_intr(void *xtxq, int pending)
2354 {
2355 	struct vtnet_softc *sc;
2356 	struct vtnet_txq *txq;
2357 	struct ifnet *ifp;
2358 
2359 	txq = xtxq;
2360 	sc = txq->vtntx_sc;
2361 	ifp = sc->vtnet_ifp;
2362 
2363 	VTNET_TXQ_LOCK(txq);
2364 
2365 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2366 		VTNET_TXQ_UNLOCK(txq);
2367 		return;
2368 	}
2369 
2370 	vtnet_txq_eof(txq);
2371 
2372 	vtnet_txq_start(txq);
2373 
2374 	if (vtnet_txq_enable_intr(txq) != 0) {
2375 		vtnet_txq_disable_intr(txq);
2376 		txq->vtntx_stats.vtxs_rescheduled++;
2377 		taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2378 	}
2379 
2380 	VTNET_TXQ_UNLOCK(txq);
2381 }
2382 
2383 static void
2384 vtnet_txq_eof(struct vtnet_txq *txq)
2385 {
2386 	struct virtqueue *vq;
2387 	struct vtnet_tx_header *txhdr;
2388 	struct mbuf *m;
2389 
2390 	vq = txq->vtntx_vq;
2391 	VTNET_TXQ_LOCK_ASSERT(txq);
2392 
2393 	while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
2394 		m = txhdr->vth_mbuf;
2395 
2396 		txq->vtntx_stats.vtxs_opackets++;
2397 		txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len;
2398 		if (m->m_flags & M_MCAST)
2399 			txq->vtntx_stats.vtxs_omcasts++;
2400 
2401 		m_freem(m);
2402 		uma_zfree(vtnet_tx_header_zone, txhdr);
2403 	}
2404 
2405 	if (virtqueue_empty(vq))
2406 		txq->vtntx_watchdog = 0;
2407 }
2408 
2409 static void
2410 vtnet_tx_vq_intr(void *xtxq)
2411 {
2412 	struct vtnet_softc *sc;
2413 	struct vtnet_txq *txq;
2414 	struct ifnet *ifp;
2415 	int tries;
2416 
2417 	txq = xtxq;
2418 	sc = txq->vtntx_sc;
2419 	ifp = sc->vtnet_ifp;
2420 	tries = 0;
2421 
2422 	if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) {
2423 		/*
2424 		 * Ignore this interrupt. Either this is a spurious interrupt
2425 		 * or multiqueue without per-VQ MSIX so every queue needs to
2426 		 * be polled (a brain dead configuration we could try harder
2427 		 * to avoid).
2428 		 */
2429 		vtnet_txq_disable_intr(txq);
2430 		return;
2431 	}
2432 
2433 	VTNET_TXQ_LOCK(txq);
2434 
2435 again:
2436 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2437 		VTNET_TXQ_UNLOCK(txq);
2438 		return;
2439 	}
2440 
2441 	vtnet_txq_eof(txq);
2442 
2443 	vtnet_txq_start(txq);
2444 
2445 	if (vtnet_txq_enable_intr(txq) != 0) {
2446 		vtnet_txq_disable_intr(txq);
2447 		/*
2448 		 * This is an occasional race, so retry a few times
2449 		 * before scheduling the taskqueue.
2450 		 */
2451 		if (tries++ < VTNET_INTR_DISABLE_RETRIES)
2452 			goto again;
2453 
2454 		VTNET_TXQ_UNLOCK(txq);
2455 		txq->vtntx_stats.vtxs_rescheduled++;
2456 		taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2457 	} else
2458 		VTNET_TXQ_UNLOCK(txq);
2459 }
2460 
2461 static void
2462 vtnet_tx_start_all(struct vtnet_softc *sc)
2463 {
2464 	struct vtnet_txq *txq;
2465 	int i;
2466 
2467 	VTNET_CORE_LOCK_ASSERT(sc);
2468 
2469 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2470 		txq = &sc->vtnet_txqs[i];
2471 
2472 		VTNET_TXQ_LOCK(txq);
2473 		vtnet_txq_start(txq);
2474 		VTNET_TXQ_UNLOCK(txq);
2475 	}
2476 }
2477 
2478 #ifndef VTNET_LEGACY_TX
2479 static void
2480 vtnet_qflush(struct ifnet *ifp)
2481 {
2482 	struct vtnet_softc *sc;
2483 	struct vtnet_txq *txq;
2484 	struct mbuf *m;
2485 	int i;
2486 
2487 	sc = ifp->if_softc;
2488 
2489 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2490 		txq = &sc->vtnet_txqs[i];
2491 
2492 		VTNET_TXQ_LOCK(txq);
2493 		while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL)
2494 			m_freem(m);
2495 		VTNET_TXQ_UNLOCK(txq);
2496 	}
2497 
2498 	if_qflush(ifp);
2499 }
2500 #endif
2501 
2502 static int
2503 vtnet_watchdog(struct vtnet_txq *txq)
2504 {
2505 	struct vtnet_softc *sc;
2506 
2507 	sc = txq->vtntx_sc;
2508 
2509 	VTNET_TXQ_LOCK(txq);
2510 	if (sc->vtnet_flags & VTNET_FLAG_EVENT_IDX)
2511 		vtnet_txq_eof(txq);
2512 	if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) {
2513 		VTNET_TXQ_UNLOCK(txq);
2514 		return (0);
2515 	}
2516 	VTNET_TXQ_UNLOCK(txq);
2517 
2518 	if_printf(sc->vtnet_ifp, "watchdog timeout on queue %d\n",
2519 	    txq->vtntx_id);
2520 	return (1);
2521 }
2522 
2523 static void
2524 vtnet_rxq_accum_stats(struct vtnet_rxq *rxq, struct vtnet_rxq_stats *accum)
2525 {
2526 	struct vtnet_rxq_stats *st;
2527 
2528 	st = &rxq->vtnrx_stats;
2529 
2530 	accum->vrxs_ipackets += st->vrxs_ipackets;
2531 	accum->vrxs_ibytes += st->vrxs_ibytes;
2532 	accum->vrxs_iqdrops += st->vrxs_iqdrops;
2533 	accum->vrxs_csum += st->vrxs_csum;
2534 	accum->vrxs_csum_failed += st->vrxs_csum_failed;
2535 	accum->vrxs_rescheduled += st->vrxs_rescheduled;
2536 }
2537 
2538 static void
2539 vtnet_txq_accum_stats(struct vtnet_txq *txq, struct vtnet_txq_stats *accum)
2540 {
2541 	struct vtnet_txq_stats *st;
2542 
2543 	st = &txq->vtntx_stats;
2544 
2545 	accum->vtxs_opackets += st->vtxs_opackets;
2546 	accum->vtxs_obytes += st->vtxs_obytes;
2547 	accum->vtxs_csum += st->vtxs_csum;
2548 	accum->vtxs_tso += st->vtxs_tso;
2549 	accum->vtxs_rescheduled += st->vtxs_rescheduled;
2550 }
2551 
2552 static void
2553 vtnet_accumulate_stats(struct vtnet_softc *sc)
2554 {
2555 	struct ifnet *ifp;
2556 	struct vtnet_statistics *st;
2557 	struct vtnet_rxq_stats rxaccum;
2558 	struct vtnet_txq_stats txaccum;
2559 	int i;
2560 
2561 	ifp = sc->vtnet_ifp;
2562 	st = &sc->vtnet_stats;
2563 	bzero(&rxaccum, sizeof(struct vtnet_rxq_stats));
2564 	bzero(&txaccum, sizeof(struct vtnet_txq_stats));
2565 
2566 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2567 		vtnet_rxq_accum_stats(&sc->vtnet_rxqs[i], &rxaccum);
2568 		vtnet_txq_accum_stats(&sc->vtnet_txqs[i], &txaccum);
2569 	}
2570 
2571 	st->rx_csum_offloaded = rxaccum.vrxs_csum;
2572 	st->rx_csum_failed = rxaccum.vrxs_csum_failed;
2573 	st->rx_task_rescheduled = rxaccum.vrxs_rescheduled;
2574 	st->tx_csum_offloaded = txaccum.vtxs_csum;
2575 	st->tx_tso_offloaded = txaccum.vtxs_tso;
2576 	st->tx_task_rescheduled = txaccum.vtxs_rescheduled;
2577 
2578 	/*
2579 	 * With the exception of if_ierrors, these ifnet statistics are
2580 	 * only updated in the driver, so just set them to our accumulated
2581 	 * values. if_ierrors is updated in ether_input() for malformed
2582 	 * frames that we should have already discarded.
2583 	 */
2584 	ifp->if_ipackets = rxaccum.vrxs_ipackets;
2585 	ifp->if_iqdrops = rxaccum.vrxs_iqdrops;
2586 	ifp->if_ierrors = rxaccum.vrxs_ierrors;
2587 	ifp->if_opackets = txaccum.vtxs_opackets;
2588 #ifndef VTNET_LEGACY_TX
2589 	ifp->if_obytes = txaccum.vtxs_obytes;
2590 	ifp->if_omcasts = txaccum.vtxs_omcasts;
2591 #endif
2592 }
2593 
2594 static void
2595 vtnet_tick(void *xsc)
2596 {
2597 	struct vtnet_softc *sc;
2598 	struct ifnet *ifp;
2599 	int i, timedout;
2600 
2601 	sc = xsc;
2602 	ifp = sc->vtnet_ifp;
2603 	timedout = 0;
2604 
2605 	VTNET_CORE_LOCK_ASSERT(sc);
2606 	vtnet_accumulate_stats(sc);
2607 
2608 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
2609 		timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]);
2610 
2611 	if (timedout != 0) {
2612 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2613 		vtnet_init_locked(sc);
2614 	} else
2615 		callout_schedule(&sc->vtnet_tick_ch, hz);
2616 }
2617 
2618 static void
2619 vtnet_start_taskqueues(struct vtnet_softc *sc)
2620 {
2621 	device_t dev;
2622 	struct vtnet_rxq *rxq;
2623 	struct vtnet_txq *txq;
2624 	int i, error;
2625 
2626 	dev = sc->vtnet_dev;
2627 
2628 	/*
2629 	 * Errors here are very difficult to recover from - we cannot
2630 	 * easily fail because, if this is during boot, we will hang
2631 	 * when freeing any successfully started taskqueues because
2632 	 * the scheduler isn't up yet.
2633 	 *
2634 	 * Most drivers just ignore the return value - it only fails
2635 	 * with ENOMEM so an error is not likely.
2636 	 */
2637 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2638 		rxq = &sc->vtnet_rxqs[i];
2639 		error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET,
2640 		    "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id);
2641 		if (error) {
2642 			device_printf(dev, "failed to start rx taskq %d\n",
2643 			    rxq->vtnrx_id);
2644 		}
2645 
2646 		txq = &sc->vtnet_txqs[i];
2647 		error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET,
2648 		    "%s txq %d", device_get_nameunit(dev), txq->vtntx_id);
2649 		if (error) {
2650 			device_printf(dev, "failed to start tx taskq %d\n",
2651 			    txq->vtntx_id);
2652 		}
2653 	}
2654 }
2655 
2656 static void
2657 vtnet_free_taskqueues(struct vtnet_softc *sc)
2658 {
2659 	struct vtnet_rxq *rxq;
2660 	struct vtnet_txq *txq;
2661 	int i;
2662 
2663 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2664 		rxq = &sc->vtnet_rxqs[i];
2665 		if (rxq->vtnrx_tq != NULL) {
2666 			taskqueue_free(rxq->vtnrx_tq);
2667 			rxq->vtnrx_vq = NULL;
2668 		}
2669 
2670 		txq = &sc->vtnet_txqs[i];
2671 		if (txq->vtntx_tq != NULL) {
2672 			taskqueue_free(txq->vtntx_tq);
2673 			txq->vtntx_tq = NULL;
2674 		}
2675 	}
2676 }
2677 
2678 static void
2679 vtnet_drain_taskqueues(struct vtnet_softc *sc)
2680 {
2681 	struct vtnet_rxq *rxq;
2682 	struct vtnet_txq *txq;
2683 	int i;
2684 
2685 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2686 		rxq = &sc->vtnet_rxqs[i];
2687 		if (rxq->vtnrx_tq != NULL)
2688 			taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2689 
2690 		txq = &sc->vtnet_txqs[i];
2691 		if (txq->vtntx_tq != NULL) {
2692 			taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask);
2693 #ifndef VTNET_LEGACY_TX
2694 			taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask);
2695 #endif
2696 		}
2697 	}
2698 }
2699 
2700 static void
2701 vtnet_drain_rxtx_queues(struct vtnet_softc *sc)
2702 {
2703 	struct vtnet_rxq *rxq;
2704 	struct vtnet_txq *txq;
2705 	int i;
2706 
2707 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2708 		rxq = &sc->vtnet_rxqs[i];
2709 		vtnet_rxq_free_mbufs(rxq);
2710 
2711 		txq = &sc->vtnet_txqs[i];
2712 		vtnet_txq_free_mbufs(txq);
2713 	}
2714 }
2715 
2716 static void
2717 vtnet_stop_rendezvous(struct vtnet_softc *sc)
2718 {
2719 	struct vtnet_rxq *rxq;
2720 	struct vtnet_txq *txq;
2721 	int i;
2722 
2723 	/*
2724 	 * Lock and unlock the per-queue mutex so we known the stop
2725 	 * state is visible. Doing only the active queues should be
2726 	 * sufficient, but it does not cost much extra to do all the
2727 	 * queues. Note we hold the core mutex here too.
2728 	 */
2729 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
2730 		rxq = &sc->vtnet_rxqs[i];
2731 		VTNET_RXQ_LOCK(rxq);
2732 		VTNET_RXQ_UNLOCK(rxq);
2733 
2734 		txq = &sc->vtnet_txqs[i];
2735 		VTNET_TXQ_LOCK(txq);
2736 		VTNET_TXQ_UNLOCK(txq);
2737 	}
2738 }
2739 
2740 static void
2741 vtnet_stop(struct vtnet_softc *sc)
2742 {
2743 	device_t dev;
2744 	struct ifnet *ifp;
2745 
2746 	dev = sc->vtnet_dev;
2747 	ifp = sc->vtnet_ifp;
2748 
2749 	VTNET_CORE_LOCK_ASSERT(sc);
2750 
2751 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2752 	sc->vtnet_link_active = 0;
2753 	callout_stop(&sc->vtnet_tick_ch);
2754 
2755 	/* Only advisory. */
2756 	vtnet_disable_interrupts(sc);
2757 
2758 	/*
2759 	 * Stop the host adapter. This resets it to the pre-initialized
2760 	 * state. It will not generate any interrupts until after it is
2761 	 * reinitialized.
2762 	 */
2763 	virtio_stop(dev);
2764 	vtnet_stop_rendezvous(sc);
2765 
2766 	/* Free any mbufs left in the virtqueues. */
2767 	vtnet_drain_rxtx_queues(sc);
2768 }
2769 
2770 static int
2771 vtnet_virtio_reinit(struct vtnet_softc *sc)
2772 {
2773 	device_t dev;
2774 	struct ifnet *ifp;
2775 	uint64_t features;
2776 	int mask, error;
2777 
2778 	dev = sc->vtnet_dev;
2779 	ifp = sc->vtnet_ifp;
2780 	features = sc->vtnet_features;
2781 
2782 	mask = 0;
2783 #if defined(INET)
2784 	mask |= IFCAP_RXCSUM;
2785 #endif
2786 #if defined (INET6)
2787 	mask |= IFCAP_RXCSUM_IPV6;
2788 #endif
2789 
2790 	/*
2791 	 * Re-negotiate with the host, removing any disabled receive
2792 	 * features. Transmit features are disabled only on our side
2793 	 * via if_capenable and if_hwassist.
2794 	 */
2795 
2796 	if (ifp->if_capabilities & mask) {
2797 		/*
2798 		 * We require both IPv4 and IPv6 offloading to be enabled
2799 		 * in order to negotiated it: VirtIO does not distinguish
2800 		 * between the two.
2801 		 */
2802 		if ((ifp->if_capenable & mask) != mask)
2803 			features &= ~VIRTIO_NET_F_GUEST_CSUM;
2804 	}
2805 
2806 	if (ifp->if_capabilities & IFCAP_LRO) {
2807 		if ((ifp->if_capenable & IFCAP_LRO) == 0)
2808 			features &= ~VTNET_LRO_FEATURES;
2809 	}
2810 
2811 	if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2812 		if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2813 			features &= ~VIRTIO_NET_F_CTRL_VLAN;
2814 	}
2815 
2816 	error = virtio_reinit(dev, features);
2817 	if (error)
2818 		device_printf(dev, "virtio reinit error %d\n", error);
2819 
2820 	return (error);
2821 }
2822 
2823 static void
2824 vtnet_init_rx_filters(struct vtnet_softc *sc)
2825 {
2826 	struct ifnet *ifp;
2827 
2828 	ifp = sc->vtnet_ifp;
2829 
2830 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2831 		/* Restore promiscuous and all-multicast modes. */
2832 		vtnet_rx_filter(sc);
2833 		/* Restore filtered MAC addresses. */
2834 		vtnet_rx_filter_mac(sc);
2835 	}
2836 
2837 	if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2838 		vtnet_rx_filter_vlan(sc);
2839 }
2840 
2841 static int
2842 vtnet_init_rx_queues(struct vtnet_softc *sc)
2843 {
2844 	device_t dev;
2845 	struct vtnet_rxq *rxq;
2846 	int i, clsize, error;
2847 
2848 	dev = sc->vtnet_dev;
2849 
2850 	/*
2851 	 * Use the new cluster size if one has been set (via a MTU
2852 	 * change). Otherwise, use the standard 2K clusters.
2853 	 *
2854 	 * BMV: It might make sense to use page sized clusters as
2855 	 * the default (depending on the features negotiated).
2856 	 */
2857 	if (sc->vtnet_rx_new_clsize != 0) {
2858 		clsize = sc->vtnet_rx_new_clsize;
2859 		sc->vtnet_rx_new_clsize = 0;
2860 	} else
2861 		clsize = MCLBYTES;
2862 
2863 	sc->vtnet_rx_clsize = clsize;
2864 	sc->vtnet_rx_nmbufs = VTNET_NEEDED_RX_MBUFS(sc, clsize);
2865 
2866 	KASSERT(sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS ||
2867 	    sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs,
2868 	    ("%s: too many rx mbufs %d for %d segments", __func__,
2869 	    sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs));
2870 
2871 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2872 		rxq = &sc->vtnet_rxqs[i];
2873 
2874 		/* Hold the lock to satisfy asserts. */
2875 		VTNET_RXQ_LOCK(rxq);
2876 		error = vtnet_rxq_populate(rxq);
2877 		VTNET_RXQ_UNLOCK(rxq);
2878 
2879 		if (error) {
2880 			device_printf(dev,
2881 			    "cannot allocate mbufs for Rx queue %d\n", i);
2882 			return (error);
2883 		}
2884 	}
2885 
2886 	return (0);
2887 }
2888 
2889 static int
2890 vtnet_init_tx_queues(struct vtnet_softc *sc)
2891 {
2892 	struct vtnet_txq *txq;
2893 	int i;
2894 
2895 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
2896 		txq = &sc->vtnet_txqs[i];
2897 		txq->vtntx_watchdog = 0;
2898 	}
2899 
2900 	return (0);
2901 }
2902 
2903 static int
2904 vtnet_init_rxtx_queues(struct vtnet_softc *sc)
2905 {
2906 	int error;
2907 
2908 	error = vtnet_init_rx_queues(sc);
2909 	if (error)
2910 		return (error);
2911 
2912 	error = vtnet_init_tx_queues(sc);
2913 	if (error)
2914 		return (error);
2915 
2916 	return (0);
2917 }
2918 
2919 static void
2920 vtnet_set_active_vq_pairs(struct vtnet_softc *sc)
2921 {
2922 	device_t dev;
2923 	int npairs;
2924 
2925 	dev = sc->vtnet_dev;
2926 
2927 	if ((sc->vtnet_flags & VTNET_FLAG_MULTIQ) == 0) {
2928 		MPASS(sc->vtnet_max_vq_pairs == 1);
2929 		sc->vtnet_act_vq_pairs = 1;
2930 		return;
2931 	}
2932 
2933 	/* BMV: Just use the maximum configured for now. */
2934 	npairs = sc->vtnet_max_vq_pairs;
2935 
2936 	if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) {
2937 		device_printf(dev,
2938 		    "cannot set active queue pairs to %d\n", npairs);
2939 		npairs = 1;
2940 	}
2941 
2942 	sc->vtnet_act_vq_pairs = npairs;
2943 }
2944 
2945 static int
2946 vtnet_reinit(struct vtnet_softc *sc)
2947 {
2948 	struct ifnet *ifp;
2949 	int error;
2950 
2951 	ifp = sc->vtnet_ifp;
2952 
2953 	/* Use the current MAC address. */
2954 	bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2955 	vtnet_set_hwaddr(sc);
2956 
2957 	vtnet_set_active_vq_pairs(sc);
2958 
2959 	ifp->if_hwassist = 0;
2960 	if (ifp->if_capenable & IFCAP_TXCSUM)
2961 		ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2962 	if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
2963 		ifp->if_hwassist |= VTNET_CSUM_OFFLOAD_IPV6;
2964 	if (ifp->if_capenable & IFCAP_TSO4)
2965 		ifp->if_hwassist |= CSUM_TSO;
2966 	if (ifp->if_capenable & IFCAP_TSO6)
2967 		ifp->if_hwassist |= CSUM_TSO; /* No CSUM_TSO_IPV6. */
2968 
2969 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
2970 		vtnet_init_rx_filters(sc);
2971 
2972 	error = vtnet_init_rxtx_queues(sc);
2973 	if (error)
2974 		return (error);
2975 
2976 	vtnet_enable_interrupts(sc);
2977 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2978 
2979 	return (0);
2980 }
2981 
2982 static void
2983 vtnet_init_locked(struct vtnet_softc *sc)
2984 {
2985 	device_t dev;
2986 	struct ifnet *ifp;
2987 
2988 	dev = sc->vtnet_dev;
2989 	ifp = sc->vtnet_ifp;
2990 
2991 	VTNET_CORE_LOCK_ASSERT(sc);
2992 
2993 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2994 		return;
2995 
2996 	vtnet_stop(sc);
2997 
2998 	/* Reinitialize with the host. */
2999 	if (vtnet_virtio_reinit(sc) != 0)
3000 		goto fail;
3001 
3002 	if (vtnet_reinit(sc) != 0)
3003 		goto fail;
3004 
3005 	virtio_reinit_complete(dev);
3006 
3007 	vtnet_update_link_status(sc);
3008 	callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
3009 
3010 	return;
3011 
3012 fail:
3013 	vtnet_stop(sc);
3014 }
3015 
3016 static void
3017 vtnet_init(void *xsc)
3018 {
3019 	struct vtnet_softc *sc;
3020 
3021 	sc = xsc;
3022 
3023 	VTNET_CORE_LOCK(sc);
3024 	vtnet_init_locked(sc);
3025 	VTNET_CORE_UNLOCK(sc);
3026 }
3027 
3028 static void
3029 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
3030 {
3031 	struct virtqueue *vq;
3032 
3033 	vq = sc->vtnet_ctrl_vq;
3034 
3035 	/*
3036 	 * The control virtqueue is only polled and therefore it should
3037 	 * already be empty.
3038 	 */
3039 	KASSERT(virtqueue_empty(vq),
3040 	    ("%s: ctrl vq %p not empty", __func__, vq));
3041 }
3042 
3043 static void
3044 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
3045     struct sglist *sg, int readable, int writable)
3046 {
3047 	struct virtqueue *vq;
3048 
3049 	vq = sc->vtnet_ctrl_vq;
3050 
3051 	VTNET_CORE_LOCK_ASSERT(sc);
3052 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
3053 	    ("%s: CTRL_VQ feature not negotiated", __func__));
3054 
3055 	if (!virtqueue_empty(vq))
3056 		return;
3057 	if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
3058 		return;
3059 
3060 	/*
3061 	 * Poll for the response, but the command is likely already
3062 	 * done when we return from the notify.
3063 	 */
3064 	virtqueue_notify(vq);
3065 	virtqueue_poll(vq, NULL);
3066 }
3067 
3068 static int
3069 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
3070 {
3071 	struct virtio_net_ctrl_hdr hdr __aligned(2);
3072 	struct sglist_seg segs[3];
3073 	struct sglist sg;
3074 	uint8_t ack;
3075 	int error;
3076 
3077 	hdr.class = VIRTIO_NET_CTRL_MAC;
3078 	hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
3079 	ack = VIRTIO_NET_ERR;
3080 
3081 	sglist_init(&sg, 3, segs);
3082 	error = 0;
3083 	error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3084 	error |= sglist_append(&sg, hwaddr, ETHER_ADDR_LEN);
3085 	error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3086 	KASSERT(error == 0 && sg.sg_nseg == 3,
3087 	    ("%s: error %d adding set MAC msg to sglist", __func__, error));
3088 
3089 	vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3090 
3091 	return (ack == VIRTIO_NET_OK ? 0 : EIO);
3092 }
3093 
3094 static int
3095 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs)
3096 {
3097 	struct sglist_seg segs[3];
3098 	struct sglist sg;
3099 	struct {
3100 		struct virtio_net_ctrl_hdr hdr;
3101 		uint8_t pad1;
3102 		struct virtio_net_ctrl_mq mq;
3103 		uint8_t pad2;
3104 		uint8_t ack;
3105 	} s __aligned(2);
3106 	int error;
3107 
3108 	s.hdr.class = VIRTIO_NET_CTRL_MQ;
3109 	s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
3110 	s.mq.virtqueue_pairs = npairs;
3111 	s.ack = VIRTIO_NET_ERR;
3112 
3113 	sglist_init(&sg, 3, segs);
3114 	error = 0;
3115 	error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3116 	error |= sglist_append(&sg, &s.mq, sizeof(struct virtio_net_ctrl_mq));
3117 	error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3118 	KASSERT(error == 0 && sg.sg_nseg == 3,
3119 	    ("%s: error %d adding MQ message to sglist", __func__, error));
3120 
3121 	vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3122 
3123 	return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3124 }
3125 
3126 static int
3127 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
3128 {
3129 	struct sglist_seg segs[3];
3130 	struct sglist sg;
3131 	struct {
3132 		struct virtio_net_ctrl_hdr hdr;
3133 		uint8_t pad1;
3134 		uint8_t onoff;
3135 		uint8_t pad2;
3136 		uint8_t ack;
3137 	} s __aligned(2);
3138 	int error;
3139 
3140 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3141 	    ("%s: CTRL_RX feature not negotiated", __func__));
3142 
3143 	s.hdr.class = VIRTIO_NET_CTRL_RX;
3144 	s.hdr.cmd = cmd;
3145 	s.onoff = !!on;
3146 	s.ack = VIRTIO_NET_ERR;
3147 
3148 	sglist_init(&sg, 3, segs);
3149 	error = 0;
3150 	error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3151 	error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
3152 	error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3153 	KASSERT(error == 0 && sg.sg_nseg == 3,
3154 	    ("%s: error %d adding Rx message to sglist", __func__, error));
3155 
3156 	vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3157 
3158 	return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3159 }
3160 
3161 static int
3162 vtnet_set_promisc(struct vtnet_softc *sc, int on)
3163 {
3164 
3165 	return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
3166 }
3167 
3168 static int
3169 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
3170 {
3171 
3172 	return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
3173 }
3174 
3175 /*
3176  * The device defaults to promiscuous mode for backwards compatibility.
3177  * Turn it off at attach time if possible.
3178  */
3179 static void
3180 vtnet_attach_disable_promisc(struct vtnet_softc *sc)
3181 {
3182 	struct ifnet *ifp;
3183 
3184 	ifp = sc->vtnet_ifp;
3185 
3186 	VTNET_CORE_LOCK(sc);
3187 	if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) == 0) {
3188 		ifp->if_flags |= IFF_PROMISC;
3189 	} else if (vtnet_set_promisc(sc, 0) != 0) {
3190 		ifp->if_flags |= IFF_PROMISC;
3191 		device_printf(sc->vtnet_dev,
3192 		    "cannot disable default promiscuous mode\n");
3193 	}
3194 	VTNET_CORE_UNLOCK(sc);
3195 }
3196 
3197 static void
3198 vtnet_rx_filter(struct vtnet_softc *sc)
3199 {
3200 	device_t dev;
3201 	struct ifnet *ifp;
3202 
3203 	dev = sc->vtnet_dev;
3204 	ifp = sc->vtnet_ifp;
3205 
3206 	VTNET_CORE_LOCK_ASSERT(sc);
3207 
3208 	if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
3209 		device_printf(dev, "cannot %s promiscuous mode\n",
3210 		    ifp->if_flags & IFF_PROMISC ? "enable" : "disable");
3211 
3212 	if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
3213 		device_printf(dev, "cannot %s all-multicast mode\n",
3214 		    ifp->if_flags & IFF_ALLMULTI ? "enable" : "disable");
3215 }
3216 
3217 static void
3218 vtnet_rx_filter_mac(struct vtnet_softc *sc)
3219 {
3220 	struct virtio_net_ctrl_hdr hdr __aligned(2);
3221 	struct vtnet_mac_filter *filter;
3222 	struct sglist_seg segs[4];
3223 	struct sglist sg;
3224 	struct ifnet *ifp;
3225 	struct ifaddr *ifa;
3226 	struct ifmultiaddr *ifma;
3227 	int ucnt, mcnt, promisc, allmulti, error;
3228 	uint8_t ack;
3229 
3230 	ifp = sc->vtnet_ifp;
3231 	filter = sc->vtnet_mac_filter;
3232 	ucnt = 0;
3233 	mcnt = 0;
3234 	promisc = 0;
3235 	allmulti = 0;
3236 
3237 	VTNET_CORE_LOCK_ASSERT(sc);
3238 	KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
3239 	    ("%s: CTRL_RX feature not negotiated", __func__));
3240 
3241 	/* Unicast MAC addresses: */
3242 	if_addr_rlock(ifp);
3243 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
3244 		if (ifa->ifa_addr->sa_family != AF_LINK)
3245 			continue;
3246 		else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3247 		    sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
3248 			continue;
3249 		else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
3250 			promisc = 1;
3251 			break;
3252 		}
3253 
3254 		bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
3255 		    &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
3256 		ucnt++;
3257 	}
3258 	if_addr_runlock(ifp);
3259 
3260 	if (promisc != 0) {
3261 		filter->vmf_unicast.nentries = 0;
3262 		if_printf(ifp, "more than %d MAC addresses assigned, "
3263 		    "falling back to promiscuous mode\n",
3264 		    VTNET_MAX_MAC_ENTRIES);
3265 	} else
3266 		filter->vmf_unicast.nentries = ucnt;
3267 
3268 	/* Multicast MAC addresses: */
3269 	if_maddr_rlock(ifp);
3270 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3271 		if (ifma->ifma_addr->sa_family != AF_LINK)
3272 			continue;
3273 		else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
3274 			allmulti = 1;
3275 			break;
3276 		}
3277 
3278 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
3279 		    &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
3280 		mcnt++;
3281 	}
3282 	if_maddr_runlock(ifp);
3283 
3284 	if (allmulti != 0) {
3285 		filter->vmf_multicast.nentries = 0;
3286 		if_printf(ifp, "more than %d multicast MAC addresses "
3287 		    "assigned, falling back to all-multicast mode\n",
3288 		    VTNET_MAX_MAC_ENTRIES);
3289 	} else
3290 		filter->vmf_multicast.nentries = mcnt;
3291 
3292 	if (promisc != 0 && allmulti != 0)
3293 		goto out;
3294 
3295 	hdr.class = VIRTIO_NET_CTRL_MAC;
3296 	hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
3297 	ack = VIRTIO_NET_ERR;
3298 
3299 	sglist_init(&sg, 4, segs);
3300 	error = 0;
3301 	error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
3302 	error |= sglist_append(&sg, &filter->vmf_unicast,
3303 	    sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
3304 	error |= sglist_append(&sg, &filter->vmf_multicast,
3305 	    sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
3306 	error |= sglist_append(&sg, &ack, sizeof(uint8_t));
3307 	KASSERT(error == 0 && sg.sg_nseg == 4,
3308 	    ("%s: error %d adding MAC filter msg to sglist", __func__, error));
3309 
3310 	vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
3311 
3312 	if (ack != VIRTIO_NET_OK)
3313 		if_printf(ifp, "error setting host MAC filter table\n");
3314 
3315 out:
3316 	if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
3317 		if_printf(ifp, "cannot enable promiscuous mode\n");
3318 	if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
3319 		if_printf(ifp, "cannot enable all-multicast mode\n");
3320 }
3321 
3322 static int
3323 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3324 {
3325 	struct sglist_seg segs[3];
3326 	struct sglist sg;
3327 	struct {
3328 		struct virtio_net_ctrl_hdr hdr;
3329 		uint8_t pad1;
3330 		uint16_t tag;
3331 		uint8_t pad2;
3332 		uint8_t ack;
3333 	} s __aligned(2);
3334 	int error;
3335 
3336 	s.hdr.class = VIRTIO_NET_CTRL_VLAN;
3337 	s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
3338 	s.tag = tag;
3339 	s.ack = VIRTIO_NET_ERR;
3340 
3341 	sglist_init(&sg, 3, segs);
3342 	error = 0;
3343 	error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
3344 	error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
3345 	error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
3346 	KASSERT(error == 0 && sg.sg_nseg == 3,
3347 	    ("%s: error %d adding VLAN message to sglist", __func__, error));
3348 
3349 	vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
3350 
3351 	return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
3352 }
3353 
3354 static void
3355 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
3356 {
3357 	uint32_t w;
3358 	uint16_t tag;
3359 	int i, bit;
3360 
3361 	VTNET_CORE_LOCK_ASSERT(sc);
3362 	KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
3363 	    ("%s: VLAN_FILTER feature not negotiated", __func__));
3364 
3365 	/* Enable the filter for each configured VLAN. */
3366 	for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) {
3367 		w = sc->vtnet_vlan_filter[i];
3368 
3369 		while ((bit = ffs(w) - 1) != -1) {
3370 			w &= ~(1 << bit);
3371 			tag = sizeof(w) * CHAR_BIT * i + bit;
3372 
3373 			if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
3374 				device_printf(sc->vtnet_dev,
3375 				    "cannot enable VLAN %d filter\n", tag);
3376 			}
3377 		}
3378 	}
3379 }
3380 
3381 static void
3382 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
3383 {
3384 	struct ifnet *ifp;
3385 	int idx, bit;
3386 
3387 	ifp = sc->vtnet_ifp;
3388 	idx = (tag >> 5) & 0x7F;
3389 	bit = tag & 0x1F;
3390 
3391 	if (tag == 0 || tag > 4095)
3392 		return;
3393 
3394 	VTNET_CORE_LOCK(sc);
3395 
3396 	if (add)
3397 		sc->vtnet_vlan_filter[idx] |= (1 << bit);
3398 	else
3399 		sc->vtnet_vlan_filter[idx] &= ~(1 << bit);
3400 
3401 	if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
3402 	    vtnet_exec_vlan_filter(sc, add, tag) != 0) {
3403 		device_printf(sc->vtnet_dev,
3404 		    "cannot %s VLAN %d %s the host filter table\n",
3405 		    add ? "add" : "remove", tag, add ? "to" : "from");
3406 	}
3407 
3408 	VTNET_CORE_UNLOCK(sc);
3409 }
3410 
3411 static void
3412 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3413 {
3414 
3415 	if (ifp->if_softc != arg)
3416 		return;
3417 
3418 	vtnet_update_vlan_filter(arg, 1, tag);
3419 }
3420 
3421 static void
3422 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
3423 {
3424 
3425 	if (ifp->if_softc != arg)
3426 		return;
3427 
3428 	vtnet_update_vlan_filter(arg, 0, tag);
3429 }
3430 
3431 static int
3432 vtnet_is_link_up(struct vtnet_softc *sc)
3433 {
3434 	device_t dev;
3435 	struct ifnet *ifp;
3436 	uint16_t status;
3437 
3438 	dev = sc->vtnet_dev;
3439 	ifp = sc->vtnet_ifp;
3440 
3441 	if ((ifp->if_capabilities & IFCAP_LINKSTATE) == 0)
3442 		status = VIRTIO_NET_S_LINK_UP;
3443 	else
3444 		status = virtio_read_dev_config_2(dev,
3445 		    offsetof(struct virtio_net_config, status));
3446 
3447 	return ((status & VIRTIO_NET_S_LINK_UP) != 0);
3448 }
3449 
3450 static void
3451 vtnet_update_link_status(struct vtnet_softc *sc)
3452 {
3453 	struct ifnet *ifp;
3454 	int link;
3455 
3456 	ifp = sc->vtnet_ifp;
3457 
3458 	VTNET_CORE_LOCK_ASSERT(sc);
3459 	link = vtnet_is_link_up(sc);
3460 
3461 	/* Notify if the link status has changed. */
3462 	if (link != 0 && sc->vtnet_link_active == 0) {
3463 		sc->vtnet_link_active = 1;
3464 		if_link_state_change(ifp, LINK_STATE_UP);
3465 	} else if (link == 0 && sc->vtnet_link_active != 0) {
3466 		sc->vtnet_link_active = 0;
3467 		if_link_state_change(ifp, LINK_STATE_DOWN);
3468 	}
3469 }
3470 
3471 static int
3472 vtnet_ifmedia_upd(struct ifnet *ifp)
3473 {
3474 	struct vtnet_softc *sc;
3475 	struct ifmedia *ifm;
3476 
3477 	sc = ifp->if_softc;
3478 	ifm = &sc->vtnet_media;
3479 
3480 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3481 		return (EINVAL);
3482 
3483 	return (0);
3484 }
3485 
3486 static void
3487 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3488 {
3489 	struct vtnet_softc *sc;
3490 
3491 	sc = ifp->if_softc;
3492 
3493 	ifmr->ifm_status = IFM_AVALID;
3494 	ifmr->ifm_active = IFM_ETHER;
3495 
3496 	VTNET_CORE_LOCK(sc);
3497 	if (vtnet_is_link_up(sc) != 0) {
3498 		ifmr->ifm_status |= IFM_ACTIVE;
3499 		ifmr->ifm_active |= VTNET_MEDIATYPE;
3500 	} else
3501 		ifmr->ifm_active |= IFM_NONE;
3502 	VTNET_CORE_UNLOCK(sc);
3503 }
3504 
3505 static void
3506 vtnet_set_hwaddr(struct vtnet_softc *sc)
3507 {
3508 	device_t dev;
3509 	int i;
3510 
3511 	dev = sc->vtnet_dev;
3512 
3513 	if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) {
3514 		if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
3515 			device_printf(dev, "unable to set MAC address\n");
3516 	} else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
3517 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
3518 			virtio_write_dev_config_1(dev,
3519 			    offsetof(struct virtio_net_config, mac) + i,
3520 			    sc->vtnet_hwaddr[i]);
3521 		}
3522 	}
3523 }
3524 
3525 static void
3526 vtnet_get_hwaddr(struct vtnet_softc *sc)
3527 {
3528 	device_t dev;
3529 	int i;
3530 
3531 	dev = sc->vtnet_dev;
3532 
3533 	if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
3534 		/*
3535 		 * Generate a random locally administered unicast address.
3536 		 *
3537 		 * It would be nice to generate the same MAC address across
3538 		 * reboots, but it seems all the hosts currently available
3539 		 * support the MAC feature, so this isn't too important.
3540 		 */
3541 		sc->vtnet_hwaddr[0] = 0xB2;
3542 		arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
3543 		vtnet_set_hwaddr(sc);
3544 		return;
3545 	}
3546 
3547 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
3548 		sc->vtnet_hwaddr[i] = virtio_read_dev_config_1(dev,
3549 		    offsetof(struct virtio_net_config, mac) + i);
3550 	}
3551 }
3552 
3553 static void
3554 vtnet_vlan_tag_remove(struct mbuf *m)
3555 {
3556 	struct ether_vlan_header *evh;
3557 
3558 	evh = mtod(m, struct ether_vlan_header *);
3559 	m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
3560 	m->m_flags |= M_VLANTAG;
3561 
3562 	/* Strip the 802.1Q header. */
3563 	bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
3564 	    ETHER_HDR_LEN - ETHER_TYPE_LEN);
3565 	m_adj(m, ETHER_VLAN_ENCAP_LEN);
3566 }
3567 
3568 static void
3569 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx,
3570     struct sysctl_oid_list *child, struct vtnet_rxq *rxq)
3571 {
3572 	struct sysctl_oid *node;
3573 	struct sysctl_oid_list *list;
3574 	struct vtnet_rxq_stats *stats;
3575 	char namebuf[16];
3576 
3577 	snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id);
3578 	node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3579 	    CTLFLAG_RD, NULL, "Receive Queue");
3580 	list = SYSCTL_CHILDREN(node);
3581 
3582 	stats = &rxq->vtnrx_stats;
3583 
3584 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets", CTLFLAG_RD,
3585 	    &stats->vrxs_ipackets, "Receive packets");
3586 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes", CTLFLAG_RD,
3587 	    &stats->vrxs_ibytes, "Receive bytes");
3588 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops", CTLFLAG_RD,
3589 	    &stats->vrxs_iqdrops, "Receive drops");
3590 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors", CTLFLAG_RD,
3591 	    &stats->vrxs_ierrors, "Receive errors");
3592 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3593 	    &stats->vrxs_csum, "Receive checksum offloaded");
3594 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed", CTLFLAG_RD,
3595 	    &stats->vrxs_csum_failed, "Receive checksum offload failed");
3596 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3597 	    &stats->vrxs_rescheduled,
3598 	    "Receive interrupt handler rescheduled");
3599 }
3600 
3601 static void
3602 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx,
3603     struct sysctl_oid_list *child, struct vtnet_txq *txq)
3604 {
3605 	struct sysctl_oid *node;
3606 	struct sysctl_oid_list *list;
3607 	struct vtnet_txq_stats *stats;
3608 	char namebuf[16];
3609 
3610 	snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id);
3611 	node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
3612 	    CTLFLAG_RD, NULL, "Transmit Queue");
3613 	list = SYSCTL_CHILDREN(node);
3614 
3615 	stats = &txq->vtntx_stats;
3616 
3617 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets", CTLFLAG_RD,
3618 	    &stats->vtxs_opackets, "Transmit packets");
3619 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes", CTLFLAG_RD,
3620 	    &stats->vtxs_obytes, "Transmit bytes");
3621 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts", CTLFLAG_RD,
3622 	    &stats->vtxs_omcasts, "Transmit multicasts");
3623 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum", CTLFLAG_RD,
3624 	    &stats->vtxs_csum, "Transmit checksum offloaded");
3625 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso", CTLFLAG_RD,
3626 	    &stats->vtxs_tso, "Transmit segmentation offloaded");
3627 	SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled", CTLFLAG_RD,
3628 	    &stats->vtxs_rescheduled,
3629 	    "Transmit interrupt handler rescheduled");
3630 }
3631 
3632 static void
3633 vtnet_setup_queue_sysctl(struct vtnet_softc *sc)
3634 {
3635 	device_t dev;
3636 	struct sysctl_ctx_list *ctx;
3637 	struct sysctl_oid *tree;
3638 	struct sysctl_oid_list *child;
3639 	int i;
3640 
3641 	dev = sc->vtnet_dev;
3642 	ctx = device_get_sysctl_ctx(dev);
3643 	tree = device_get_sysctl_tree(dev);
3644 	child = SYSCTL_CHILDREN(tree);
3645 
3646 	for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3647 		vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]);
3648 		vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]);
3649 	}
3650 }
3651 
3652 static void
3653 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx,
3654     struct sysctl_oid_list *child, struct vtnet_softc *sc)
3655 {
3656 	struct vtnet_statistics *stats;
3657 
3658 	stats = &sc->vtnet_stats;
3659 
3660 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
3661 	    CTLFLAG_RD, &stats->mbuf_alloc_failed,
3662 	    "Mbuf cluster allocation failures");
3663 
3664 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
3665 	    CTLFLAG_RD, &stats->rx_frame_too_large,
3666 	    "Received frame larger than the mbuf chain");
3667 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
3668 	    CTLFLAG_RD, &stats->rx_enq_replacement_failed,
3669 	    "Enqueuing the replacement receive mbuf failed");
3670 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
3671 	    CTLFLAG_RD, &stats->rx_mergeable_failed,
3672 	    "Mergeable buffers receive failures");
3673 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
3674 	    CTLFLAG_RD, &stats->rx_csum_bad_ethtype,
3675 	    "Received checksum offloaded buffer with unsupported "
3676 	    "Ethernet type");
3677 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
3678 	    CTLFLAG_RD, &stats->rx_csum_bad_ipproto,
3679 	    "Received checksum offloaded buffer with incorrect IP protocol");
3680 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
3681 	    CTLFLAG_RD, &stats->rx_csum_bad_offset,
3682 	    "Received checksum offloaded buffer with incorrect offset");
3683 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_proto",
3684 	    CTLFLAG_RD, &stats->rx_csum_bad_proto,
3685 	    "Received checksum offloaded buffer with incorrect protocol");
3686 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
3687 	    CTLFLAG_RD, &stats->rx_csum_failed,
3688 	    "Received buffer checksum offload failed");
3689 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
3690 	    CTLFLAG_RD, &stats->rx_csum_offloaded,
3691 	    "Received buffer checksum offload succeeded");
3692 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
3693 	    CTLFLAG_RD, &stats->rx_task_rescheduled,
3694 	    "Times the receive interrupt task rescheduled itself");
3695 
3696 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
3697 	    CTLFLAG_RD, &stats->tx_csum_bad_ethtype,
3698 	    "Aborted transmit of checksum offloaded buffer with unknown "
3699 	    "Ethernet type");
3700 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
3701 	    CTLFLAG_RD, &stats->tx_tso_bad_ethtype,
3702 	    "Aborted transmit of TSO buffer with unknown Ethernet type");
3703 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp",
3704 	    CTLFLAG_RD, &stats->tx_tso_not_tcp,
3705 	    "Aborted transmit of TSO buffer with non TCP protocol");
3706 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
3707 	    CTLFLAG_RD, &stats->tx_defragged,
3708 	    "Transmit mbufs defragged");
3709 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
3710 	    CTLFLAG_RD, &stats->tx_defrag_failed,
3711 	    "Aborted transmit of buffer because defrag failed");
3712 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
3713 	    CTLFLAG_RD, &stats->tx_csum_offloaded,
3714 	    "Offloaded checksum of transmitted buffer");
3715 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
3716 	    CTLFLAG_RD, &stats->tx_tso_offloaded,
3717 	    "Segmentation offload of transmitted buffer");
3718 	SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
3719 	    CTLFLAG_RD, &stats->tx_task_rescheduled,
3720 	    "Times the transmit interrupt task rescheduled itself");
3721 }
3722 
3723 static void
3724 vtnet_setup_sysctl(struct vtnet_softc *sc)
3725 {
3726 	device_t dev;
3727 	struct sysctl_ctx_list *ctx;
3728 	struct sysctl_oid *tree;
3729 	struct sysctl_oid_list *child;
3730 
3731 	dev = sc->vtnet_dev;
3732 	ctx = device_get_sysctl_ctx(dev);
3733 	tree = device_get_sysctl_tree(dev);
3734 	child = SYSCTL_CHILDREN(tree);
3735 
3736 	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs",
3737 	    CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0,
3738 	    "Maximum number of supported virtqueue pairs");
3739 	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs",
3740 	    CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0,
3741 	    "Number of active virtqueue pairs");
3742 
3743 	vtnet_setup_stat_sysctl(ctx, child, sc);
3744 }
3745 
3746 static int
3747 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq)
3748 {
3749 
3750 	return (virtqueue_enable_intr(rxq->vtnrx_vq));
3751 }
3752 
3753 static void
3754 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq)
3755 {
3756 
3757 	virtqueue_disable_intr(rxq->vtnrx_vq);
3758 }
3759 
3760 static int
3761 vtnet_txq_enable_intr(struct vtnet_txq *txq)
3762 {
3763 
3764 	return (virtqueue_postpone_intr(txq->vtntx_vq, VQ_POSTPONE_LONG));
3765 }
3766 
3767 static void
3768 vtnet_txq_disable_intr(struct vtnet_txq *txq)
3769 {
3770 
3771 	virtqueue_disable_intr(txq->vtntx_vq);
3772 }
3773 
3774 static void
3775 vtnet_enable_rx_interrupts(struct vtnet_softc *sc)
3776 {
3777 	int i;
3778 
3779 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3780 		vtnet_rxq_enable_intr(&sc->vtnet_rxqs[i]);
3781 }
3782 
3783 static void
3784 vtnet_enable_tx_interrupts(struct vtnet_softc *sc)
3785 {
3786 	int i;
3787 
3788 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3789 		vtnet_txq_enable_intr(&sc->vtnet_txqs[i]);
3790 }
3791 
3792 static void
3793 vtnet_enable_interrupts(struct vtnet_softc *sc)
3794 {
3795 
3796 	vtnet_enable_rx_interrupts(sc);
3797 	vtnet_enable_tx_interrupts(sc);
3798 }
3799 
3800 static void
3801 vtnet_disable_rx_interrupts(struct vtnet_softc *sc)
3802 {
3803 	int i;
3804 
3805 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3806 		vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]);
3807 }
3808 
3809 static void
3810 vtnet_disable_tx_interrupts(struct vtnet_softc *sc)
3811 {
3812 	int i;
3813 
3814 	for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3815 		vtnet_txq_disable_intr(&sc->vtnet_txqs[i]);
3816 }
3817 
3818 static void
3819 vtnet_disable_interrupts(struct vtnet_softc *sc)
3820 {
3821 
3822 	vtnet_disable_rx_interrupts(sc);
3823 	vtnet_disable_tx_interrupts(sc);
3824 }
3825 
3826 static int
3827 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def)
3828 {
3829 	char path[64];
3830 
3831 	snprintf(path, sizeof(path),
3832 	    "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob);
3833 	TUNABLE_INT_FETCH(path, &def);
3834 
3835 	return (def);
3836 }
3837