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