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