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