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