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