if_vtnet - Remove vtnet_tick_ch and vtnet_cfgchg_task from softc struct.
[dragonfly.git] / sys / dev / virtual / virtio / net / if_vtnet.c
blobf4f5dd3c98e36db405988967a551fbbffec231a6
1 /*-
2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
3 * All rights reserved.
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.
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.
27 /* Driver for VirtIO network devices. */
29 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/sockio.h>
35 #include <sys/mbuf.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
41 #include <sys/random.h>
42 #include <sys/sglist.h>
43 #include <sys/serialize.h>
44 #include <sys/bus.h>
45 #include <sys/rman.h>
47 #include <machine/limits.h>
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_media.h>
55 #include <net/vlan/if_vlan_var.h>
56 #include <net/vlan/if_vlan_ether.h>
57 #include <net/ifq_var.h>
59 #include <net/bpf.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip6.h>
65 #include <netinet/udp.h>
66 #include <netinet/tcp.h>
68 #include <dev/virtual/virtio/virtio/virtio.h>
69 #include <dev/virtual/virtio/virtio/virtqueue.h>
70 #include <dev/virtual/virtio/net/virtio_net.h>
71 #include <dev/virtual/virtio/net/if_vtnetvar.h>
73 MALLOC_DEFINE(M_VTNET, "VTNET_TX", "Outgoing VTNET TX frame header");
75 static int vtnet_probe(device_t);
76 static int vtnet_attach(device_t);
77 static int vtnet_detach(device_t);
78 static int vtnet_suspend(device_t);
79 static int vtnet_resume(device_t);
80 static int vtnet_shutdown(device_t);
82 static void vtnet_negotiate_features(struct vtnet_softc *);
83 static int vtnet_alloc_intrs(struct vtnet_softc *);
84 static int vtnet_alloc_virtqueues(struct vtnet_softc *);
85 static void vtnet_get_hwaddr(struct vtnet_softc *);
86 static void vtnet_set_hwaddr(struct vtnet_softc *);
87 static int vtnet_is_link_up(struct vtnet_softc *);
88 static void vtnet_update_link_status(struct vtnet_softc *);
89 #if 0
90 static void vtnet_watchdog(struct vtnet_softc *);
91 #endif
92 static int vtnet_setup_interface(struct vtnet_softc *);
93 static int vtnet_change_mtu(struct vtnet_softc *, int);
94 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
96 static int vtnet_init_rx_vq(struct vtnet_softc *);
97 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
98 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
99 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
101 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
102 struct mbuf **);
103 static int vtnet_replace_rxbuf(struct vtnet_softc *,
104 struct mbuf *, int);
105 static int vtnet_newbuf(struct vtnet_softc *);
106 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
107 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
108 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
109 static void vtnet_vlan_tag_remove(struct mbuf *);
110 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
111 struct virtio_net_hdr *);
112 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
113 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
114 static void vtnet_rx_intr_task(void *);
115 static void vtnet_rx_vq_intr(void *);
117 static void vtnet_enqueue_txhdr(struct vtnet_softc *,
118 struct vtnet_tx_header *);
119 static void vtnet_txeof(struct vtnet_softc *);
120 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
121 struct virtio_net_hdr *);
122 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
123 struct vtnet_tx_header *);
124 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
125 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
126 static void vtnet_tx_intr_task(void *);
127 static void vtnet_tx_vq_intr(void *);
129 static void vtnet_config_intr(void *);
131 static void vtnet_stop(struct vtnet_softc *);
132 static int vtnet_virtio_reinit(struct vtnet_softc *);
133 static void vtnet_init(void *);
135 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
136 struct sglist *, int, int);
138 static int vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
139 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
140 static int vtnet_set_promisc(struct vtnet_softc *, int);
141 static int vtnet_set_allmulti(struct vtnet_softc *, int);
142 static void vtnet_rx_filter(struct vtnet_softc *sc);
143 static void vtnet_rx_filter_mac(struct vtnet_softc *);
145 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
146 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
147 static void vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t);
148 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
149 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
151 static int vtnet_ifmedia_upd(struct ifnet *);
152 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
154 static void vtnet_add_statistics(struct vtnet_softc *);
156 static int vtnet_enable_rx_intr(struct vtnet_softc *);
157 static int vtnet_enable_tx_intr(struct vtnet_softc *);
158 static void vtnet_disable_rx_intr(struct vtnet_softc *);
159 static void vtnet_disable_tx_intr(struct vtnet_softc *);
161 /* Tunables. */
162 static int vtnet_csum_disable = 0;
163 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
164 static int vtnet_tso_disable = 1;
165 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
166 static int vtnet_lro_disable = 0;
167 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
170 * Reducing the number of transmit completed interrupts can
171 * improve performance. To do so, the define below keeps the
172 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
173 * in the start path. The price to pay for this is the m_free'ing
174 * of transmitted mbufs may be delayed.
176 #define VTNET_TX_INTR_MODERATION
178 static struct virtio_feature_desc vtnet_feature_desc[] = {
179 { VIRTIO_NET_F_CSUM, "TxChecksum" },
180 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
181 { VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, "DynOffload" },
182 { VIRTIO_NET_F_MAC, "MacAddress" },
183 { VIRTIO_NET_F_GSO, "TxAllGSO" },
184 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
185 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
186 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
187 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
188 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
189 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
190 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
191 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
192 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
193 { VIRTIO_NET_F_STATUS, "Status" },
194 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
195 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
196 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
197 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
198 { VIRTIO_NET_F_GUEST_ANNOUNCE, "GuestAnnounce" },
199 { VIRTIO_NET_F_MQ, "RFS" },
200 { VIRTIO_NET_F_CTRL_MAC_ADDR, "SetMacAddress" },
201 { 0, NULL }
204 static device_method_t vtnet_methods[] = {
205 /* Device methods. */
206 DEVMETHOD(device_probe, vtnet_probe),
207 DEVMETHOD(device_attach, vtnet_attach),
208 DEVMETHOD(device_detach, vtnet_detach),
209 DEVMETHOD(device_suspend, vtnet_suspend),
210 DEVMETHOD(device_resume, vtnet_resume),
211 DEVMETHOD(device_shutdown, vtnet_shutdown),
213 DEVMETHOD_END
216 static driver_t vtnet_driver = {
217 "vtnet",
218 vtnet_methods,
219 sizeof(struct vtnet_softc)
222 static devclass_t vtnet_devclass;
224 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass, NULL, NULL);
225 MODULE_VERSION(vtnet, 1);
226 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
228 static int
229 vtnet_probe(device_t dev)
231 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
232 return (ENXIO);
234 device_set_desc(dev, "VirtIO Networking Adapter");
236 return (BUS_PROBE_DEFAULT);
239 struct irqmap {
240 int irq;
241 driver_intr_t *handler;
244 static int
245 vtnet_attach(device_t dev)
247 struct vtnet_softc *sc;
248 int i, error;
250 sc = device_get_softc(dev);
251 sc->vtnet_dev = dev;
253 lwkt_serialize_init(&sc->vtnet_slz);
255 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
256 vtnet_ifmedia_sts);
257 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
258 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
260 vtnet_add_statistics(sc);
261 SLIST_INIT(&sc->vtnet_txhdr_free);
263 /* Register our feature descriptions. */
264 virtio_set_feature_desc(dev, vtnet_feature_desc);
265 vtnet_negotiate_features(sc);
267 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
268 sc->vtnet_flags |= VTNET_FLAG_INDIRECT;
270 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
271 /* This feature should always be negotiated. */
272 sc->vtnet_flags |= VTNET_FLAG_MAC;
275 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
276 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
277 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
278 } else {
279 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
282 sc->vtnet_rx_mbuf_size = MCLBYTES;
283 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
285 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
286 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
288 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
289 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
290 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
291 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
292 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
293 virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
294 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
297 error = vtnet_alloc_intrs(sc);
298 if (error) {
299 device_printf(dev, "cannot allocate interrupts\n");
300 goto fail;
303 error = vtnet_alloc_virtqueues(sc);
304 if (error) {
305 device_printf(dev, "cannot allocate virtqueues\n");
306 goto fail;
309 /* XXX Separate function */
310 struct irqmap info[2];
312 /* Possible "Virtqueue <-> IRQ" configurations */
313 switch (sc->vtnet_nintr) {
314 case 1:
315 info[0] = (struct irqmap){0, vtnet_rx_vq_intr};
316 info[1] = (struct irqmap){0, vtnet_tx_vq_intr};
317 break;
318 case 2:
319 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
320 info[0] = (struct irqmap){1, vtnet_rx_vq_intr};
321 } else {
322 info[0] = (struct irqmap){0, vtnet_rx_vq_intr};
324 info[1] = (struct irqmap){1, vtnet_tx_vq_intr};
325 break;
326 case 3:
327 info[0] = (struct irqmap){1, vtnet_rx_vq_intr};
328 info[1] = (struct irqmap){2, vtnet_tx_vq_intr};
329 break;
330 default:
331 device_printf(dev, "Invalid interrupt vector count: %d\n",
332 sc->vtnet_nintr);
333 goto fail;
335 for (i = 0; i < 2; i++) {
336 error = virtio_bind_intr(dev, info[i].irq, i,
337 info[i].handler, sc);
338 if (error) {
339 device_printf(dev, "cannot bind virtqueue IRQs\n");
340 goto fail;
343 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
344 error = virtio_bind_intr(dev, 0, -1, vtnet_config_intr, sc);
345 if (error) {
346 device_printf(dev, "cannot bind config_change IRQ\n");
347 goto fail;
351 /* Read (or generate) the MAC address for the adapter. */
352 vtnet_get_hwaddr(sc);
354 error = vtnet_setup_interface(sc);
355 if (error) {
356 device_printf(dev, "cannot setup interface\n");
357 goto fail;
360 for (i = 0; i < sc->vtnet_nintr; i++) {
361 error = virtio_setup_intr(dev, i, &sc->vtnet_slz);
362 if (error) {
363 device_printf(dev, "cannot setup virtqueue "
364 "interrupts\n");
365 ether_ifdetach(sc->vtnet_ifp);
366 goto fail;
370 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
371 lwkt_serialize_enter(&sc->vtnet_slz);
372 vtnet_set_hwaddr(sc);
373 lwkt_serialize_exit(&sc->vtnet_slz);
377 * Device defaults to promiscuous mode for backwards
378 * compatibility. Turn it off if possible.
380 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
381 lwkt_serialize_enter(&sc->vtnet_slz);
382 if (vtnet_set_promisc(sc, 0) != 0) {
383 sc->vtnet_ifp->if_flags |= IFF_PROMISC;
384 device_printf(dev,
385 "cannot disable promiscuous mode\n");
387 lwkt_serialize_exit(&sc->vtnet_slz);
388 } else
389 sc->vtnet_ifp->if_flags |= IFF_PROMISC;
391 fail:
392 if (error)
393 vtnet_detach(dev);
395 return (error);
398 static int
399 vtnet_detach(device_t dev)
401 struct vtnet_softc *sc;
402 struct ifnet *ifp;
403 int i;
405 sc = device_get_softc(dev);
406 ifp = sc->vtnet_ifp;
408 for (i = 0; i < sc->vtnet_nintr; i++)
409 virtio_teardown_intr(dev, i);
411 if (device_is_attached(dev)) {
412 lwkt_serialize_enter(&sc->vtnet_slz);
413 vtnet_stop(sc);
414 lwkt_serialize_exit(&sc->vtnet_slz);
416 ether_ifdetach(ifp);
419 if (sc->vtnet_vlan_attach != NULL) {
420 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
421 sc->vtnet_vlan_attach = NULL;
423 if (sc->vtnet_vlan_detach != NULL) {
424 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
425 sc->vtnet_vlan_detach = NULL;
428 if (ifp) {
429 if_free(ifp);
430 sc->vtnet_ifp = NULL;
433 if (sc->vtnet_rx_vq != NULL)
434 vtnet_free_rx_mbufs(sc);
435 if (sc->vtnet_tx_vq != NULL)
436 vtnet_free_tx_mbufs(sc);
437 if (sc->vtnet_ctrl_vq != NULL)
438 vtnet_free_ctrl_vq(sc);
440 if (sc->vtnet_txhdrarea != NULL) {
441 contigfree(sc->vtnet_txhdrarea,
442 sc->vtnet_txhdrcount * sizeof(struct vtnet_tx_header),
443 M_VTNET);
444 sc->vtnet_txhdrarea = NULL;
446 SLIST_INIT(&sc->vtnet_txhdr_free);
447 if (sc->vtnet_macfilter != NULL) {
448 contigfree(sc->vtnet_macfilter,
449 sizeof(struct vtnet_mac_filter), M_DEVBUF);
450 sc->vtnet_macfilter = NULL;
453 ifmedia_removeall(&sc->vtnet_media);
455 return (0);
458 static int
459 vtnet_suspend(device_t dev)
461 struct vtnet_softc *sc;
463 sc = device_get_softc(dev);
465 lwkt_serialize_enter(&sc->vtnet_slz);
466 vtnet_stop(sc);
467 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
468 lwkt_serialize_exit(&sc->vtnet_slz);
470 return (0);
473 static int
474 vtnet_resume(device_t dev)
476 struct vtnet_softc *sc;
477 struct ifnet *ifp;
479 sc = device_get_softc(dev);
480 ifp = sc->vtnet_ifp;
482 lwkt_serialize_enter(&sc->vtnet_slz);
483 if (ifp->if_flags & IFF_UP)
484 vtnet_init(sc);
485 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
486 lwkt_serialize_exit(&sc->vtnet_slz);
488 return (0);
491 static int
492 vtnet_shutdown(device_t dev)
496 * Suspend already does all of what we need to
497 * do here; we just never expect to be resumed.
499 return (vtnet_suspend(dev));
502 static void
503 vtnet_negotiate_features(struct vtnet_softc *sc)
505 device_t dev;
506 uint64_t mask, features;
508 dev = sc->vtnet_dev;
509 mask = 0;
511 if (vtnet_csum_disable)
512 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
515 * XXX DragonFly doesn't support receive checksum offload for ipv6 yet,
516 * hence always disable the virtio feature for now.
517 * XXX We need to support the DynOffload feature, in order to
518 * dynamically enable/disable this feature.
520 mask |= VIRTIO_NET_F_GUEST_CSUM;
523 * TSO is only available when the tx checksum offload feature is also
524 * negotiated.
526 if (vtnet_csum_disable || vtnet_tso_disable)
527 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
528 VIRTIO_NET_F_HOST_ECN;
530 if (vtnet_lro_disable)
531 mask |= VTNET_LRO_FEATURES;
533 features = VTNET_FEATURES & ~mask;
534 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
535 features |= VIRTIO_F_ANY_LAYOUT;
536 sc->vtnet_features = virtio_negotiate_features(dev, features);
538 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
539 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
541 * LRO without mergeable buffers requires special care. This
542 * is not ideal because every receive buffer must be large
543 * enough to hold the maximum TCP packet, the Ethernet header,
544 * and the header. This requires up to 34 descriptors with
545 * MCLBYTES clusters. If we do not have indirect descriptors,
546 * LRO is disabled since the virtqueue will not contain very
547 * many receive buffers.
549 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
550 device_printf(dev,
551 "LRO disabled due to both mergeable buffers and "
552 "indirect descriptors not negotiated\n");
554 features &= ~VTNET_LRO_FEATURES;
555 sc->vtnet_features =
556 virtio_negotiate_features(dev, features);
557 } else
558 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
562 static int
563 vtnet_alloc_intrs(struct vtnet_softc *sc)
565 int cnt, error;
566 int intrcount = virtio_intr_count(sc->vtnet_dev);
567 int i;
568 int use_config;
570 if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS)) {
571 use_config = 1;
572 /* We can use a maximum of 3 interrupt vectors. */
573 intrcount = imin(intrcount, 3);
574 } else {
575 /* We can use a maximum of 2 interrupt vectors. */
576 intrcount = imin(intrcount, 2);
579 if (intrcount < 1)
580 return (ENXIO);
583 * XXX We should explicitly set the cpus for the rx/tx threads, to
584 * only use cpus, where the network stack is running.
586 for (i = 0; i < intrcount; i++)
587 sc->vtnet_cpus[i] = -1;
589 cnt = intrcount;
590 error = virtio_intr_alloc(sc->vtnet_dev, &cnt, use_config,
591 sc->vtnet_cpus);
592 if (error != 0) {
593 virtio_intr_release(sc->vtnet_dev);
594 return (error);
596 sc->vtnet_nintr = cnt;
598 return (0);
601 static int
602 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
604 device_t dev;
605 struct vq_alloc_info vq_info[3];
606 int nvqs;
608 dev = sc->vtnet_dev;
609 nvqs = 2;
612 * Indirect descriptors are not needed for the Rx
613 * virtqueue when mergeable buffers are negotiated.
614 * The header is placed inline with the data, not
615 * in a separate descriptor, and mbuf clusters are
616 * always physically contiguous.
618 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
619 sc->vtnet_rx_nsegs = (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) ?
620 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
621 } else
622 sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
624 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
625 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
626 sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
627 else
628 sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
630 VQ_ALLOC_INFO_INIT(&vq_info[0], sc->vtnet_rx_nsegs, &sc->vtnet_rx_vq,
631 "%s receive", device_get_nameunit(dev));
633 VQ_ALLOC_INFO_INIT(&vq_info[1], sc->vtnet_tx_nsegs, &sc->vtnet_tx_vq,
634 "%s transmit", device_get_nameunit(dev));
636 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
637 nvqs++;
639 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, &sc->vtnet_ctrl_vq,
640 "%s control", device_get_nameunit(dev));
643 return (virtio_alloc_virtqueues(dev, nvqs, vq_info));
646 static int
647 vtnet_setup_interface(struct vtnet_softc *sc)
649 device_t dev;
650 struct ifnet *ifp;
651 int i;
653 dev = sc->vtnet_dev;
655 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
656 if (ifp == NULL) {
657 device_printf(dev, "cannot allocate ifnet structure\n");
658 return (ENOSPC);
661 ifp->if_softc = sc;
662 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
663 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
664 ifp->if_init = vtnet_init;
665 ifp->if_start = vtnet_start;
666 ifp->if_ioctl = vtnet_ioctl;
668 sc->vtnet_rx_process_limit = virtqueue_size(sc->vtnet_rx_vq);
669 sc->vtnet_tx_size = virtqueue_size(sc->vtnet_tx_vq);
670 if (sc->vtnet_flags & VTNET_FLAG_INDIRECT)
671 sc->vtnet_txhdrcount = sc->vtnet_tx_size;
672 else
673 sc->vtnet_txhdrcount = (sc->vtnet_tx_size / 2) + 1;
674 sc->vtnet_txhdrarea = contigmalloc(
675 sc->vtnet_txhdrcount * sizeof(struct vtnet_tx_header),
676 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
677 if (sc->vtnet_txhdrarea == NULL) {
678 device_printf(dev, "cannot contigmalloc the tx headers\n");
679 return (ENOMEM);
681 for (i = 0; i < sc->vtnet_txhdrcount; i++)
682 vtnet_enqueue_txhdr(sc, &sc->vtnet_txhdrarea[i]);
683 sc->vtnet_macfilter = contigmalloc(
684 sizeof(struct vtnet_mac_filter),
685 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
686 if (sc->vtnet_macfilter == NULL) {
687 device_printf(dev,
688 "cannot contigmalloc the mac filter table\n");
689 return (ENOMEM);
691 ifq_set_maxlen(&ifp->if_snd, sc->vtnet_tx_size - 1);
692 ifq_set_ready(&ifp->if_snd);
694 ether_ifattach(ifp, sc->vtnet_hwaddr, &sc->vtnet_slz);
696 /* The Tx IRQ is currently always the last allocated interrupt. */
697 ifq_set_cpuid(&ifp->if_snd, sc->vtnet_cpus[sc->vtnet_nintr - 1]);
699 /* Tell the upper layer(s) we support long frames. */
700 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
701 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
703 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
704 ifp->if_capabilities |= IFCAP_TXCSUM;
706 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
707 ifp->if_capabilities |= IFCAP_TSO4;
708 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
709 ifp->if_capabilities |= IFCAP_TSO6;
710 if (ifp->if_capabilities & IFCAP_TSO)
711 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
713 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
714 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
717 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM))
718 ifp->if_capabilities |= IFCAP_RXCSUM;
720 #if 0 /* IFCAP_LRO doesn't exist in DragonFly. */
721 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
722 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
723 ifp->if_capabilities |= IFCAP_LRO;
724 #endif
726 if ((ifp->if_capabilities & IFCAP_HWCSUM) == IFCAP_HWCSUM) {
728 * VirtIO does not support VLAN tagging, but we can fake
729 * it by inserting and removing the 802.1Q header during
730 * transmit and receive. We are then able to do checksum
731 * offloading of VLAN frames.
733 ifp->if_capabilities |=
734 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
737 ifp->if_capenable = ifp->if_capabilities;
740 * Capabilities after here are not enabled by default.
743 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
744 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
746 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
747 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
748 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
749 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
752 return (0);
755 static void
756 vtnet_set_hwaddr(struct vtnet_softc *sc)
758 device_t dev;
760 dev = sc->vtnet_dev;
762 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) &&
763 (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)) {
764 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
765 device_printf(dev, "unable to set MAC address\n");
766 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
767 virtio_write_device_config(dev,
768 offsetof(struct virtio_net_config, mac),
769 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
773 static void
774 vtnet_get_hwaddr(struct vtnet_softc *sc)
776 device_t dev;
778 dev = sc->vtnet_dev;
780 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
782 * Generate a random locally administered unicast address.
784 * It would be nice to generate the same MAC address across
785 * reboots, but it seems all the hosts currently available
786 * support the MAC feature, so this isn't too important.
788 sc->vtnet_hwaddr[0] = 0xB2;
789 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
790 return;
793 virtio_read_device_config(dev,
794 offsetof(struct virtio_net_config, mac),
795 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
798 static int
799 vtnet_is_link_up(struct vtnet_softc *sc)
801 device_t dev;
802 struct ifnet *ifp;
803 uint16_t status;
805 dev = sc->vtnet_dev;
806 ifp = sc->vtnet_ifp;
808 ASSERT_SERIALIZED(&sc->vtnet_slz);
810 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
811 status = virtio_read_dev_config_2(dev,
812 offsetof(struct virtio_net_config, status));
813 } else {
814 status = VIRTIO_NET_S_LINK_UP;
817 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
820 static void
821 vtnet_update_link_status(struct vtnet_softc *sc)
823 device_t dev;
824 struct ifnet *ifp;
825 struct ifaltq_subque *ifsq;
826 int link;
828 dev = sc->vtnet_dev;
829 ifp = sc->vtnet_ifp;
830 ifsq = ifq_get_subq_default(&ifp->if_snd);
832 link = vtnet_is_link_up(sc);
834 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
835 sc->vtnet_flags |= VTNET_FLAG_LINK;
836 if (bootverbose)
837 device_printf(dev, "Link is up\n");
838 ifp->if_link_state = LINK_STATE_UP;
839 if_link_state_change(ifp);
840 if (!ifsq_is_empty(ifsq))
841 vtnet_start(ifp, ifsq);
842 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
843 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
844 if (bootverbose)
845 device_printf(dev, "Link is down\n");
847 ifp->if_link_state = LINK_STATE_DOWN;
848 if_link_state_change(ifp);
852 #if 0
853 static void
854 vtnet_watchdog(struct vtnet_softc *sc)
856 struct ifnet *ifp;
858 ifp = sc->vtnet_ifp;
860 #ifdef VTNET_TX_INTR_MODERATION
861 vtnet_txeof(sc);
862 #endif
864 if (sc->vtnet_watchdog_timer == 0 || --sc->vtnet_watchdog_timer)
865 return;
867 if_printf(ifp, "watchdog timeout -- resetting\n");
868 #ifdef VTNET_DEBUG
869 virtqueue_dump(sc->vtnet_tx_vq);
870 #endif
871 ifp->if_oerrors++;
872 ifp->if_flags &= ~IFF_RUNNING;
873 vtnet_init(sc);
875 #endif
877 static int
878 vtnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data,struct ucred *cr)
880 struct vtnet_softc *sc;
881 struct ifreq *ifr;
882 int reinit, mask, error;
884 sc = ifp->if_softc;
885 ifr = (struct ifreq *) data;
886 reinit = 0;
887 error = 0;
889 switch (cmd) {
890 case SIOCSIFMTU:
891 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VTNET_MAX_MTU)
892 error = EINVAL;
893 else if (ifp->if_mtu != ifr->ifr_mtu)
894 error = vtnet_change_mtu(sc, ifr->ifr_mtu);
895 break;
897 case SIOCSIFFLAGS:
898 if ((ifp->if_flags & IFF_UP) == 0) {
899 if (ifp->if_flags & IFF_RUNNING)
900 vtnet_stop(sc);
901 } else if (ifp->if_flags & IFF_RUNNING) {
902 if ((ifp->if_flags ^ sc->vtnet_if_flags) &
903 (IFF_PROMISC | IFF_ALLMULTI)) {
904 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)
905 vtnet_rx_filter(sc);
906 else
907 error = ENOTSUP;
909 } else {
910 vtnet_init(sc);
913 if (error == 0)
914 sc->vtnet_if_flags = ifp->if_flags;
915 break;
917 case SIOCADDMULTI:
918 case SIOCDELMULTI:
919 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_RX) &&
920 (ifp->if_flags & IFF_RUNNING))
921 vtnet_rx_filter_mac(sc);
922 break;
924 case SIOCSIFMEDIA:
925 case SIOCGIFMEDIA:
926 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
927 break;
929 case SIOCSIFCAP:
930 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
933 if (mask & IFCAP_TXCSUM) {
934 ifp->if_capenable ^= IFCAP_TXCSUM;
935 if (ifp->if_capenable & IFCAP_TXCSUM)
936 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
937 else
938 ifp->if_hwassist &= ~VTNET_CSUM_OFFLOAD;
941 if (mask & IFCAP_TSO4) {
942 ifp->if_capenable ^= IFCAP_TSO4;
943 if (ifp->if_capenable & IFCAP_TSO4)
944 ifp->if_hwassist |= CSUM_TSO;
945 else
946 ifp->if_hwassist &= ~CSUM_TSO;
949 if (mask & IFCAP_RXCSUM) {
950 ifp->if_capenable ^= IFCAP_RXCSUM;
951 reinit = 1;
954 #if 0 /* IFCAP_LRO doesn't exist in DragonFly. */
955 if (mask & IFCAP_LRO) {
956 ifp->if_capenable ^= IFCAP_LRO;
957 reinit = 1;
959 #endif
961 if (mask & IFCAP_VLAN_HWFILTER) {
962 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
963 reinit = 1;
966 if (mask & IFCAP_VLAN_HWTSO)
967 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
969 if (mask & IFCAP_VLAN_HWTAGGING)
970 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
972 if (reinit && (ifp->if_flags & IFF_RUNNING)) {
973 ifp->if_flags &= ~IFF_RUNNING;
974 vtnet_init(sc);
976 //VLAN_CAPABILITIES(ifp);
978 break;
980 default:
981 error = ether_ioctl(ifp, cmd, data);
982 break;
985 return (error);
988 static int
989 vtnet_change_mtu(struct vtnet_softc *sc, int new_mtu)
991 struct ifnet *ifp;
992 int new_frame_size, clsize;
994 ifp = sc->vtnet_ifp;
996 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
997 new_frame_size = sizeof(struct vtnet_rx_header) +
998 sizeof(struct ether_vlan_header) + new_mtu;
1000 if (new_frame_size > MJUM9BYTES)
1001 return (EINVAL);
1003 if (new_frame_size <= MCLBYTES)
1004 clsize = MCLBYTES;
1005 else
1006 clsize = MJUM9BYTES;
1007 } else {
1008 new_frame_size = sizeof(struct virtio_net_hdr_mrg_rxbuf) +
1009 sizeof(struct ether_vlan_header) + new_mtu;
1011 if (new_frame_size <= MCLBYTES)
1012 clsize = MCLBYTES;
1013 else
1014 clsize = MJUMPAGESIZE;
1017 sc->vtnet_rx_mbuf_size = clsize;
1018 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
1019 KASSERT(sc->vtnet_rx_mbuf_count < VTNET_MAX_RX_SEGS,
1020 ("too many rx mbufs: %d", sc->vtnet_rx_mbuf_count));
1022 ifp->if_mtu = new_mtu;
1024 if (ifp->if_flags & IFF_RUNNING) {
1025 ifp->if_flags &= ~IFF_RUNNING;
1026 vtnet_init(sc);
1029 return (0);
1032 static int
1033 vtnet_init_rx_vq(struct vtnet_softc *sc)
1035 struct virtqueue *vq;
1036 int nbufs, error;
1038 vq = sc->vtnet_rx_vq;
1039 nbufs = 0;
1040 error = ENOSPC;
1042 while (!virtqueue_full(vq)) {
1043 if ((error = vtnet_newbuf(sc)) != 0)
1044 break;
1045 nbufs++;
1048 if (nbufs > 0) {
1049 virtqueue_notify(vq, &sc->vtnet_slz);
1052 * EMSGSIZE signifies the virtqueue did not have enough
1053 * entries available to hold the last mbuf. This is not
1054 * an error. We should not get ENOSPC since we check if
1055 * the virtqueue is full before attempting to add a
1056 * buffer.
1058 if (error == EMSGSIZE)
1059 error = 0;
1062 return (error);
1065 static void
1066 vtnet_free_rx_mbufs(struct vtnet_softc *sc)
1068 struct virtqueue *vq;
1069 struct mbuf *m;
1070 int last;
1072 vq = sc->vtnet_rx_vq;
1073 last = 0;
1075 while ((m = virtqueue_drain(vq, &last)) != NULL)
1076 m_freem(m);
1078 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Rx Vq"));
1081 static void
1082 vtnet_free_tx_mbufs(struct vtnet_softc *sc)
1084 struct virtqueue *vq;
1085 struct vtnet_tx_header *txhdr;
1086 int last;
1088 vq = sc->vtnet_tx_vq;
1089 last = 0;
1091 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
1092 m_freem(txhdr->vth_mbuf);
1093 vtnet_enqueue_txhdr(sc, txhdr);
1096 KASSERT(virtqueue_empty(vq), ("mbufs remaining in Tx Vq"));
1099 static void
1100 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
1103 * The control virtqueue is only polled, therefore
1104 * it should already be empty.
1106 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
1107 ("Ctrl Vq not empty"));
1110 static struct mbuf *
1111 vtnet_alloc_rxbuf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1113 struct mbuf *m_head, *m_tail, *m;
1114 int i, clsize;
1116 clsize = sc->vtnet_rx_mbuf_size;
1118 /*use getcl instead of getjcl. see if_mxge.c comment line 2398*/
1119 //m_head = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, clsize);
1120 m_head = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR );
1121 if (m_head == NULL)
1122 goto fail;
1124 m_head->m_len = clsize;
1125 m_tail = m_head;
1127 if (nbufs > 1) {
1128 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1129 ("chained Rx mbuf requested without LRO_NOMRG"));
1131 for (i = 0; i < nbufs - 1; i++) {
1132 //m = m_getjcl(M_DONTWAIT, MT_DATA, 0, clsize);
1133 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1134 if (m == NULL)
1135 goto fail;
1137 m->m_len = clsize;
1138 m_tail->m_next = m;
1139 m_tail = m;
1143 if (m_tailp != NULL)
1144 *m_tailp = m_tail;
1146 return (m_head);
1148 fail:
1149 sc->vtnet_stats.mbuf_alloc_failed++;
1150 m_freem(m_head);
1152 return (NULL);
1155 static int
1156 vtnet_replace_rxbuf(struct vtnet_softc *sc, struct mbuf *m0, int len0)
1158 struct mbuf *m, *m_prev;
1159 struct mbuf *m_new, *m_tail;
1160 int len, clsize, nreplace, error;
1162 m = m0;
1163 m_prev = NULL;
1164 len = len0;
1166 m_tail = NULL;
1167 clsize = sc->vtnet_rx_mbuf_size;
1168 nreplace = 0;
1170 if (m->m_next != NULL)
1171 KASSERT(sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1172 ("chained Rx mbuf without LRO_NOMRG"));
1175 * Since LRO_NOMRG mbuf chains are so large, we want to avoid
1176 * allocating an entire chain for each received frame. When
1177 * the received frame's length is less than that of the chain,
1178 * the unused mbufs are reassigned to the new chain.
1180 while (len > 0) {
1182 * Something is seriously wrong if we received
1183 * a frame larger than the mbuf chain. Drop it.
1185 if (m == NULL) {
1186 sc->vtnet_stats.rx_frame_too_large++;
1187 return (EMSGSIZE);
1190 KASSERT(m->m_len == clsize,
1191 ("mbuf length not expected cluster size: %d",
1192 m->m_len));
1194 m->m_len = MIN(m->m_len, len);
1195 len -= m->m_len;
1197 m_prev = m;
1198 m = m->m_next;
1199 nreplace++;
1202 KASSERT(m_prev != NULL, ("m_prev == NULL"));
1203 KASSERT(nreplace <= sc->vtnet_rx_mbuf_count,
1204 ("too many replacement mbufs: %d/%d", nreplace,
1205 sc->vtnet_rx_mbuf_count));
1207 m_new = vtnet_alloc_rxbuf(sc, nreplace, &m_tail);
1208 if (m_new == NULL) {
1209 m_prev->m_len = clsize;
1210 return (ENOBUFS);
1214 * Move unused mbufs, if any, from the original chain
1215 * onto the end of the new chain.
1217 if (m_prev->m_next != NULL) {
1218 m_tail->m_next = m_prev->m_next;
1219 m_prev->m_next = NULL;
1222 error = vtnet_enqueue_rxbuf(sc, m_new);
1223 if (error) {
1225 * BAD! We could not enqueue the replacement mbuf chain. We
1226 * must restore the m0 chain to the original state if it was
1227 * modified so we can subsequently discard it.
1229 * NOTE: The replacement is suppose to be an identical copy
1230 * to the one just dequeued so this is an unexpected error.
1232 sc->vtnet_stats.rx_enq_replacement_failed++;
1234 if (m_tail->m_next != NULL) {
1235 m_prev->m_next = m_tail->m_next;
1236 m_tail->m_next = NULL;
1239 m_prev->m_len = clsize;
1240 m_freem(m_new);
1243 return (error);
1246 static int
1247 vtnet_newbuf(struct vtnet_softc *sc)
1249 struct mbuf *m;
1250 int error;
1252 m = vtnet_alloc_rxbuf(sc, sc->vtnet_rx_mbuf_count, NULL);
1253 if (m == NULL)
1254 return (ENOBUFS);
1256 error = vtnet_enqueue_rxbuf(sc, m);
1257 if (error)
1258 m_freem(m);
1260 return (error);
1263 static void
1264 vtnet_discard_merged_rxbuf(struct vtnet_softc *sc, int nbufs)
1266 struct virtqueue *vq;
1267 struct mbuf *m;
1269 vq = sc->vtnet_rx_vq;
1271 while (--nbufs > 0) {
1272 if ((m = virtqueue_dequeue(vq, NULL)) == NULL)
1273 break;
1274 vtnet_discard_rxbuf(sc, m);
1278 static void
1279 vtnet_discard_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1281 int error;
1284 * Requeue the discarded mbuf. This should always be
1285 * successful since it was just dequeued.
1287 error = vtnet_enqueue_rxbuf(sc, m);
1288 KASSERT(error == 0, ("cannot requeue discarded mbuf"));
1291 static int
1292 vtnet_enqueue_rxbuf(struct vtnet_softc *sc, struct mbuf *m)
1294 struct sglist sg;
1295 struct sglist_seg segs[VTNET_MAX_RX_SEGS];
1296 struct vtnet_rx_header *rxhdr;
1297 struct virtio_net_hdr *hdr;
1298 uint8_t *mdata;
1299 int offset, error;
1301 ASSERT_SERIALIZED(&sc->vtnet_slz);
1302 if ((sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0)
1303 KASSERT(m->m_next == NULL, ("chained Rx mbuf"));
1305 sglist_init(&sg, sc->vtnet_rx_nsegs, segs);
1307 mdata = mtod(m, uint8_t *);
1308 offset = 0;
1310 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1311 rxhdr = (struct vtnet_rx_header *) mdata;
1312 hdr = &rxhdr->vrh_hdr;
1313 offset += sizeof(struct vtnet_rx_header);
1315 error = sglist_append(&sg, hdr, sc->vtnet_hdr_size);
1316 KASSERT(error == 0, ("cannot add header to sglist"));
1319 error = sglist_append(&sg, mdata + offset, m->m_len - offset);
1320 if (error)
1321 return (error);
1323 if (m->m_next != NULL) {
1324 error = sglist_append_mbuf(&sg, m->m_next);
1325 if (error)
1326 return (error);
1329 return (virtqueue_enqueue(sc->vtnet_rx_vq, m, &sg, 0, sg.sg_nseg));
1332 static void
1333 vtnet_vlan_tag_remove(struct mbuf *m)
1335 struct ether_vlan_header *evl;
1337 evl = mtod(m, struct ether_vlan_header *);
1339 m->m_pkthdr.ether_vlantag = ntohs(evl->evl_tag);
1340 m->m_flags |= M_VLANTAG;
1342 /* Strip the 802.1Q header. */
1343 bcopy((char *) evl, (char *) evl + ETHER_VLAN_ENCAP_LEN,
1344 ETHER_HDR_LEN - ETHER_TYPE_LEN);
1345 m_adj(m, ETHER_VLAN_ENCAP_LEN);
1349 * Alternative method of doing receive checksum offloading. Rather
1350 * than parsing the received frame down to the IP header, use the
1351 * csum_offset to determine which CSUM_* flags are appropriate. We
1352 * can get by with doing this only because the checksum offsets are
1353 * unique for the things we care about.
1355 static int
1356 vtnet_rx_csum(struct vtnet_softc *sc, struct mbuf *m,
1357 struct virtio_net_hdr *hdr)
1359 struct ether_header *eh;
1360 struct ether_vlan_header *evh;
1361 struct udphdr *udp;
1362 int csum_len;
1363 uint16_t eth_type;
1365 csum_len = hdr->csum_start + hdr->csum_offset;
1367 if (csum_len < sizeof(struct ether_header) + sizeof(struct ip))
1368 return (1);
1369 if (m->m_len < csum_len)
1370 return (1);
1372 eh = mtod(m, struct ether_header *);
1373 eth_type = ntohs(eh->ether_type);
1374 if (eth_type == ETHERTYPE_VLAN) {
1375 evh = mtod(m, struct ether_vlan_header *);
1376 eth_type = ntohs(evh->evl_proto);
1379 if (eth_type != ETHERTYPE_IP && eth_type != ETHERTYPE_IPV6) {
1380 sc->vtnet_stats.rx_csum_bad_ethtype++;
1381 return (1);
1384 /* Use the offset to determine the appropriate CSUM_* flags. */
1385 switch (hdr->csum_offset) {
1386 case offsetof(struct udphdr, uh_sum):
1387 if (m->m_len < hdr->csum_start + sizeof(struct udphdr))
1388 return (1);
1389 udp = (struct udphdr *)(mtod(m, uint8_t *) + hdr->csum_start);
1390 if (udp->uh_sum == 0)
1391 return (0);
1393 /* FALLTHROUGH */
1395 case offsetof(struct tcphdr, th_sum):
1396 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1397 m->m_pkthdr.csum_data = 0xFFFF;
1398 break;
1400 default:
1401 sc->vtnet_stats.rx_csum_bad_offset++;
1402 return (1);
1405 sc->vtnet_stats.rx_csum_offloaded++;
1407 return (0);
1410 static int
1411 vtnet_rxeof_merged(struct vtnet_softc *sc, struct mbuf *m_head, int nbufs)
1413 struct ifnet *ifp;
1414 struct virtqueue *vq;
1415 struct mbuf *m, *m_tail;
1416 int len;
1418 ifp = sc->vtnet_ifp;
1419 vq = sc->vtnet_rx_vq;
1420 m_tail = m_head;
1422 while (--nbufs > 0) {
1423 m = virtqueue_dequeue(vq, &len);
1424 if (m == NULL) {
1425 ifp->if_ierrors++;
1426 goto fail;
1429 if (vtnet_newbuf(sc) != 0) {
1430 ifp->if_iqdrops++;
1431 vtnet_discard_rxbuf(sc, m);
1432 if (nbufs > 1)
1433 vtnet_discard_merged_rxbuf(sc, nbufs);
1434 goto fail;
1437 if (m->m_len < len)
1438 len = m->m_len;
1440 m->m_len = len;
1441 m->m_flags &= ~M_PKTHDR;
1443 m_head->m_pkthdr.len += len;
1444 m_tail->m_next = m;
1445 m_tail = m;
1448 return (0);
1450 fail:
1451 sc->vtnet_stats.rx_mergeable_failed++;
1452 m_freem(m_head);
1454 return (1);
1457 static int
1458 vtnet_rxeof(struct vtnet_softc *sc, int count, int *rx_npktsp)
1460 struct virtio_net_hdr lhdr;
1461 struct ifnet *ifp;
1462 struct virtqueue *vq;
1463 struct mbuf *m;
1464 struct ether_header *eh;
1465 struct virtio_net_hdr *hdr;
1466 struct virtio_net_hdr_mrg_rxbuf *mhdr;
1467 int len, deq, nbufs, adjsz, rx_npkts;
1469 ifp = sc->vtnet_ifp;
1470 vq = sc->vtnet_rx_vq;
1471 hdr = &lhdr;
1472 deq = 0;
1473 rx_npkts = 0;
1475 ASSERT_SERIALIZED(&sc->vtnet_slz);
1477 while (--count >= 0) {
1478 m = virtqueue_dequeue(vq, &len);
1479 if (m == NULL)
1480 break;
1481 deq++;
1483 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
1484 ifp->if_ierrors++;
1485 vtnet_discard_rxbuf(sc, m);
1486 continue;
1489 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
1490 nbufs = 1;
1491 adjsz = sizeof(struct vtnet_rx_header);
1493 * Account for our pad between the header and
1494 * the actual start of the frame.
1496 len += VTNET_RX_HEADER_PAD;
1497 } else {
1498 mhdr = mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
1499 nbufs = mhdr->num_buffers;
1500 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1503 if (vtnet_replace_rxbuf(sc, m, len) != 0) {
1504 ifp->if_iqdrops++;
1505 vtnet_discard_rxbuf(sc, m);
1506 if (nbufs > 1)
1507 vtnet_discard_merged_rxbuf(sc, nbufs);
1508 continue;
1511 m->m_pkthdr.len = len;
1512 m->m_pkthdr.rcvif = ifp;
1513 m->m_pkthdr.csum_flags = 0;
1515 if (nbufs > 1) {
1516 if (vtnet_rxeof_merged(sc, m, nbufs) != 0)
1517 continue;
1520 ifp->if_ipackets++;
1523 * Save copy of header before we strip it. For both mergeable
1524 * and non-mergeable, the VirtIO header is placed first in the
1525 * mbuf's data. We no longer need num_buffers, so always use a
1526 * virtio_net_hdr.
1528 memcpy(hdr, mtod(m, void *), sizeof(struct virtio_net_hdr));
1529 m_adj(m, adjsz);
1531 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) {
1532 eh = mtod(m, struct ether_header *);
1533 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1534 vtnet_vlan_tag_remove(m);
1537 * With the 802.1Q header removed, update the
1538 * checksum starting location accordingly.
1540 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
1541 hdr->csum_start -=
1542 ETHER_VLAN_ENCAP_LEN;
1546 if (ifp->if_capenable & IFCAP_RXCSUM &&
1547 hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1548 if (vtnet_rx_csum(sc, m, hdr) != 0)
1549 sc->vtnet_stats.rx_csum_failed++;
1552 lwkt_serialize_exit(&sc->vtnet_slz);
1553 rx_npkts++;
1554 ifp->if_input(ifp, m, NULL, -1);
1555 lwkt_serialize_enter(&sc->vtnet_slz);
1558 * The interface may have been stopped while we were
1559 * passing the packet up the network stack.
1561 if ((ifp->if_flags & IFF_RUNNING) == 0)
1562 break;
1565 virtqueue_notify(vq, &sc->vtnet_slz);
1567 if (rx_npktsp != NULL)
1568 *rx_npktsp = rx_npkts;
1570 return (count > 0 ? 0 : EAGAIN);
1573 static void
1574 vtnet_rx_intr_task(void *arg)
1576 struct vtnet_softc *sc;
1577 struct ifnet *ifp;
1578 int more;
1580 sc = arg;
1581 ifp = sc->vtnet_ifp;
1583 next:
1584 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1585 vtnet_enable_rx_intr(sc);
1586 return;
1589 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1590 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1591 vtnet_disable_rx_intr(sc);
1592 more = 1;
1595 if (more) {
1596 sc->vtnet_stats.rx_task_rescheduled++;
1597 goto next;
1601 static void
1602 vtnet_rx_vq_intr(void *xsc)
1604 struct vtnet_softc *sc;
1606 sc = xsc;
1608 if (!virtqueue_pending(sc->vtnet_rx_vq))
1609 return;
1611 vtnet_disable_rx_intr(sc);
1612 vtnet_rx_intr_task(sc);
1615 static void
1616 vtnet_enqueue_txhdr(struct vtnet_softc *sc, struct vtnet_tx_header *txhdr)
1618 bzero(txhdr, sizeof(*txhdr));
1619 SLIST_INSERT_HEAD(&sc->vtnet_txhdr_free, txhdr, link);
1622 static void
1623 vtnet_txeof(struct vtnet_softc *sc)
1625 struct virtqueue *vq;
1626 struct ifnet *ifp;
1627 struct vtnet_tx_header *txhdr;
1628 int deq;
1630 vq = sc->vtnet_tx_vq;
1631 ifp = sc->vtnet_ifp;
1632 deq = 0;
1634 ASSERT_SERIALIZED(&sc->vtnet_slz);
1636 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1637 deq++;
1638 ifp->if_opackets++;
1639 m_freem(txhdr->vth_mbuf);
1640 vtnet_enqueue_txhdr(sc, txhdr);
1643 if (deq > 0) {
1644 ifq_clr_oactive(&ifp->if_snd);
1645 if (virtqueue_empty(vq))
1646 sc->vtnet_watchdog_timer = 0;
1650 static struct mbuf *
1651 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1652 struct virtio_net_hdr *hdr)
1654 struct ifnet *ifp;
1655 struct ether_header *eh;
1656 struct ether_vlan_header *evh;
1657 struct ip *ip;
1658 struct ip6_hdr *ip6;
1659 struct tcphdr *tcp;
1660 int ip_offset;
1661 uint16_t eth_type, csum_start;
1662 uint8_t ip_proto, gso_type;
1664 ifp = sc->vtnet_ifp;
1665 M_ASSERTPKTHDR(m);
1667 ip_offset = sizeof(struct ether_header);
1668 if (m->m_len < ip_offset) {
1669 if ((m = m_pullup(m, ip_offset)) == NULL)
1670 return (NULL);
1673 eh = mtod(m, struct ether_header *);
1674 eth_type = ntohs(eh->ether_type);
1675 if (eth_type == ETHERTYPE_VLAN) {
1676 ip_offset = sizeof(struct ether_vlan_header);
1677 if (m->m_len < ip_offset) {
1678 if ((m = m_pullup(m, ip_offset)) == NULL)
1679 return (NULL);
1681 evh = mtod(m, struct ether_vlan_header *);
1682 eth_type = ntohs(evh->evl_proto);
1685 switch (eth_type) {
1686 case ETHERTYPE_IP:
1687 if (m->m_len < ip_offset + sizeof(struct ip)) {
1688 m = m_pullup(m, ip_offset + sizeof(struct ip));
1689 if (m == NULL)
1690 return (NULL);
1693 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1694 ip_proto = ip->ip_p;
1695 csum_start = ip_offset + (ip->ip_hl << 2);
1696 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1697 break;
1699 case ETHERTYPE_IPV6:
1700 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1701 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1702 if (m == NULL)
1703 return (NULL);
1706 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1708 * XXX Assume no extension headers are present. Presently,
1709 * this will always be true in the case of TSO, and FreeBSD
1710 * does not perform checksum offloading of IPv6 yet.
1712 ip_proto = ip6->ip6_nxt;
1713 csum_start = ip_offset + sizeof(struct ip6_hdr);
1714 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1715 break;
1717 default:
1718 return (m);
1721 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1722 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1723 hdr->csum_start = csum_start;
1724 hdr->csum_offset = m->m_pkthdr.csum_data;
1726 sc->vtnet_stats.tx_csum_offloaded++;
1729 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1730 if (ip_proto != IPPROTO_TCP)
1731 return (m);
1733 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1734 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1735 if (m == NULL)
1736 return (NULL);
1739 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1740 hdr->gso_type = gso_type;
1741 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1742 hdr->gso_size = m->m_pkthdr.tso_segsz;
1744 if (tcp->th_flags & TH_CWR) {
1746 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1747 * ECN support is only configurable globally with the
1748 * net.inet.tcp.ecn.enable sysctl knob.
1750 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1751 if_printf(ifp, "TSO with ECN not supported "
1752 "by host\n");
1753 m_freem(m);
1754 return (NULL);
1757 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1760 sc->vtnet_stats.tx_tso_offloaded++;
1763 return (m);
1766 static int
1767 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1768 struct vtnet_tx_header *txhdr)
1770 struct sglist sg;
1771 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1772 struct virtqueue *vq;
1773 struct mbuf *m;
1774 int error;
1776 vq = sc->vtnet_tx_vq;
1777 m = *m_head;
1779 sglist_init(&sg, sc->vtnet_tx_nsegs, segs);
1780 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1781 KASSERT(error == 0 && sg.sg_nseg == 1,
1782 ("%s: error %d adding header to sglist", __func__, error));
1784 error = sglist_append_mbuf(&sg, m);
1785 if (error) {
1786 m = m_defrag(m, M_NOWAIT);
1787 if (m == NULL)
1788 goto fail;
1790 *m_head = m;
1791 sc->vtnet_stats.tx_defragged++;
1793 error = sglist_append_mbuf(&sg, m);
1794 if (error)
1795 goto fail;
1798 txhdr->vth_mbuf = m;
1799 error = virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0);
1801 return (error);
1803 fail:
1804 sc->vtnet_stats.tx_defrag_failed++;
1805 m_freem(*m_head);
1806 *m_head = NULL;
1808 return (ENOBUFS);
1811 static struct mbuf *
1812 vtnet_vlan_tag_insert(struct mbuf *m)
1814 struct mbuf *n;
1815 struct ether_vlan_header *evl;
1817 if (M_WRITABLE(m) == 0) {
1818 n = m_dup(m, M_NOWAIT);
1819 m_freem(m);
1820 if ((m = n) == NULL)
1821 return (NULL);
1824 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1825 if (m == NULL)
1826 return (NULL);
1827 if (m->m_len < sizeof(struct ether_vlan_header)) {
1828 m = m_pullup(m, sizeof(struct ether_vlan_header));
1829 if (m == NULL)
1830 return (NULL);
1833 /* Insert 802.1Q header into the existing Ethernet header. */
1834 evl = mtod(m, struct ether_vlan_header *);
1835 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1836 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1837 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1838 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1839 m->m_flags &= ~M_VLANTAG;
1841 return (m);
1844 static int
1845 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1847 struct vtnet_tx_header *txhdr;
1848 struct virtio_net_hdr *hdr;
1849 struct mbuf *m;
1850 int error;
1852 txhdr = SLIST_FIRST(&sc->vtnet_txhdr_free);
1853 if (txhdr == NULL)
1854 return (ENOBUFS);
1855 SLIST_REMOVE_HEAD(&sc->vtnet_txhdr_free, link);
1858 * Always use the non-mergeable header to simplify things. When
1859 * the mergeable feature is negotiated, the num_buffers field
1860 * must be set to zero. We use vtnet_hdr_size later to enqueue
1861 * the correct header size to the host.
1863 hdr = &txhdr->vth_uhdr.hdr;
1864 m = *m_head;
1866 error = ENOBUFS;
1868 if (m->m_flags & M_VLANTAG) {
1869 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1870 m = vtnet_vlan_tag_insert(m);
1871 if ((*m_head = m) == NULL)
1872 goto fail;
1873 m->m_flags &= ~M_VLANTAG;
1876 if (m->m_pkthdr.csum_flags != 0) {
1877 m = vtnet_tx_offload(sc, m, hdr);
1878 if ((*m_head = m) == NULL)
1879 goto fail;
1882 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1883 fail:
1884 if (error != 0)
1885 vtnet_enqueue_txhdr(sc, txhdr);
1886 return (error);
1889 static void
1890 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1892 struct vtnet_softc *sc;
1893 struct virtqueue *vq;
1894 struct mbuf *m0;
1895 int enq;
1897 sc = ifp->if_softc;
1898 vq = sc->vtnet_tx_vq;
1899 enq = 0;
1901 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1902 ASSERT_SERIALIZED(&sc->vtnet_slz);
1904 if ((ifp->if_flags & (IFF_RUNNING)) !=
1905 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
1906 return;
1908 #ifdef VTNET_TX_INTR_MODERATION
1909 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
1910 vtnet_txeof(sc);
1911 #endif
1913 while (!ifsq_is_empty(ifsq)) {
1914 if (virtqueue_full(vq)) {
1915 ifq_set_oactive(&ifp->if_snd);
1916 break;
1919 m0 = ifq_dequeue(&ifp->if_snd);
1920 if (m0 == NULL)
1921 break;
1923 if (vtnet_encap(sc, &m0) != 0) {
1924 if (m0 == NULL)
1925 break;
1926 ifq_prepend(&ifp->if_snd, m0);
1927 ifq_set_oactive(&ifp->if_snd);
1928 break;
1931 enq++;
1932 ETHER_BPF_MTAP(ifp, m0);
1935 if (enq > 0) {
1936 virtqueue_notify(vq, &sc->vtnet_slz);
1937 sc->vtnet_watchdog_timer = VTNET_WATCHDOG_TIMEOUT;
1941 static void
1942 vtnet_tx_intr_task(void *arg)
1944 struct vtnet_softc *sc;
1945 struct ifnet *ifp;
1946 struct ifaltq_subque *ifsq;
1948 sc = arg;
1949 ifp = sc->vtnet_ifp;
1950 ifsq = ifq_get_subq_default(&ifp->if_snd);
1952 next:
1953 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1954 vtnet_enable_tx_intr(sc);
1955 return;
1958 vtnet_txeof(sc);
1960 if (!ifsq_is_empty(ifsq))
1961 vtnet_start(ifp, ifsq);
1963 if (vtnet_enable_tx_intr(sc) != 0) {
1964 vtnet_disable_tx_intr(sc);
1965 sc->vtnet_stats.tx_task_rescheduled++;
1966 goto next;
1970 static void
1971 vtnet_tx_vq_intr(void *xsc)
1973 struct vtnet_softc *sc;
1975 sc = xsc;
1977 if (!virtqueue_pending(sc->vtnet_tx_vq))
1978 return;
1980 vtnet_disable_tx_intr(sc);
1981 vtnet_tx_intr_task(sc);
1984 static void
1985 vtnet_config_intr(void *arg)
1987 struct vtnet_softc *sc;
1989 sc = arg;
1991 vtnet_update_link_status(sc);
1994 static void
1995 vtnet_stop(struct vtnet_softc *sc)
1997 device_t dev;
1998 struct ifnet *ifp;
2000 dev = sc->vtnet_dev;
2001 ifp = sc->vtnet_ifp;
2003 ASSERT_SERIALIZED(&sc->vtnet_slz);
2005 sc->vtnet_watchdog_timer = 0;
2006 ifq_clr_oactive(&ifp->if_snd);
2007 ifp->if_flags &= ~(IFF_RUNNING);
2009 vtnet_disable_rx_intr(sc);
2010 vtnet_disable_tx_intr(sc);
2013 * Stop the host VirtIO adapter. Note this will reset the host
2014 * adapter's state back to the pre-initialized state, so in
2015 * order to make the device usable again, we must drive it
2016 * through virtio_reinit() and virtio_reinit_complete().
2018 virtio_stop(dev);
2020 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2022 vtnet_free_rx_mbufs(sc);
2023 vtnet_free_tx_mbufs(sc);
2026 static int
2027 vtnet_virtio_reinit(struct vtnet_softc *sc)
2029 device_t dev;
2030 struct ifnet *ifp;
2031 uint64_t features;
2032 int error;
2034 dev = sc->vtnet_dev;
2035 ifp = sc->vtnet_ifp;
2036 features = sc->vtnet_features;
2039 * Re-negotiate with the host, removing any disabled receive
2040 * features. Transmit features are disabled only on our side
2041 * via if_capenable and if_hwassist.
2044 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2045 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2046 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2049 #if 0 /* IFCAP_LRO doesn't exist in DragonFly. */
2050 if (ifp->if_capabilities & IFCAP_LRO) {
2051 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2052 features &= ~VTNET_LRO_FEATURES;
2054 #endif
2056 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2057 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2058 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2061 error = virtio_reinit(dev, features);
2062 if (error)
2063 device_printf(dev, "virtio reinit error %d\n", error);
2065 return (error);
2068 static void
2069 vtnet_init(void *xsc)
2071 struct vtnet_softc *sc;
2072 device_t dev;
2073 struct ifnet *ifp;
2074 int error;
2076 sc = xsc;
2077 dev = sc->vtnet_dev;
2078 ifp = sc->vtnet_ifp;
2080 ASSERT_SERIALIZED(&sc->vtnet_slz);
2082 if (ifp->if_flags & IFF_RUNNING)
2083 return;
2085 /* Stop host's adapter, cancel any pending I/O. */
2086 vtnet_stop(sc);
2088 /* Reinitialize the host device. */
2089 error = vtnet_virtio_reinit(sc);
2090 if (error) {
2091 device_printf(dev,
2092 "reinitialization failed, stopping device...\n");
2093 vtnet_stop(sc);
2094 return;
2097 /* Update host with assigned MAC address. */
2098 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2099 vtnet_set_hwaddr(sc);
2101 ifp->if_hwassist = 0;
2102 if (ifp->if_capenable & IFCAP_TXCSUM)
2103 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2104 if (ifp->if_capenable & IFCAP_TSO4)
2105 ifp->if_hwassist |= CSUM_TSO;
2107 error = vtnet_init_rx_vq(sc);
2108 if (error) {
2109 device_printf(dev,
2110 "cannot allocate mbufs for Rx virtqueue\n");
2111 vtnet_stop(sc);
2112 return;
2115 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2116 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2117 /* Restore promiscuous and all-multicast modes. */
2118 vtnet_rx_filter(sc);
2120 /* Restore filtered MAC addresses. */
2121 vtnet_rx_filter_mac(sc);
2124 /* Restore VLAN filters. */
2125 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2126 vtnet_rx_filter_vlan(sc);
2130 vtnet_enable_rx_intr(sc);
2131 vtnet_enable_tx_intr(sc);
2134 ifp->if_flags |= IFF_RUNNING;
2135 ifq_clr_oactive(&ifp->if_snd);
2137 virtio_reinit_complete(dev);
2139 vtnet_update_link_status(sc);
2142 static void
2143 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2144 struct sglist *sg, int readable, int writable)
2146 struct virtqueue *vq;
2147 void *c;
2149 vq = sc->vtnet_ctrl_vq;
2151 ASSERT_SERIALIZED(&sc->vtnet_slz);
2152 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2153 ("no control virtqueue"));
2154 KASSERT(virtqueue_empty(vq),
2155 ("control command already enqueued"));
2157 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2158 return;
2160 virtqueue_notify(vq, &sc->vtnet_slz);
2163 * Poll until the command is complete. Previously, we would
2164 * sleep until the control virtqueue interrupt handler woke
2165 * us up, but dropping the VTNET_MTX leads to serialization
2166 * difficulties.
2168 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2169 * vectors. Two of those vectors are needed for the Rx and Tx
2170 * virtqueues. We do not support sharing both a Vq and config
2171 * changed notification on the same MSIX vector.
2173 c = virtqueue_poll(vq, NULL);
2174 KASSERT(c == cookie, ("unexpected control command response"));
2177 static int
2178 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
2180 struct {
2181 struct virtio_net_ctrl_hdr hdr __aligned(2);
2182 uint8_t pad1;
2183 char aligned_hwaddr[ETHER_ADDR_LEN] __aligned(8);
2184 uint8_t pad2;
2185 uint8_t ack;
2186 } s;
2187 struct sglist_seg segs[3];
2188 struct sglist sg;
2189 int error;
2191 s.hdr.class = VIRTIO_NET_CTRL_MAC;
2192 s.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
2193 s.ack = VIRTIO_NET_ERR;
2195 /* Copy the mac address into physically contiguous memory */
2196 memcpy(s.aligned_hwaddr, hwaddr, ETHER_ADDR_LEN);
2198 sglist_init(&sg, 3, segs);
2199 error = 0;
2200 error |= sglist_append(&sg, &s.hdr,
2201 sizeof(struct virtio_net_ctrl_hdr));
2202 error |= sglist_append(&sg, s.aligned_hwaddr, ETHER_ADDR_LEN);
2203 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2204 KASSERT(error == 0 && sg.sg_nseg == 3,
2205 ("%s: error %d adding set MAC msg to sglist", __func__, error));
2207 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2209 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2212 static void
2213 vtnet_rx_filter(struct vtnet_softc *sc)
2215 device_t dev;
2216 struct ifnet *ifp;
2218 dev = sc->vtnet_dev;
2219 ifp = sc->vtnet_ifp;
2221 ASSERT_SERIALIZED(&sc->vtnet_slz);
2222 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2223 ("CTRL_RX feature not negotiated"));
2225 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2226 device_printf(dev, "cannot %s promiscuous mode\n",
2227 (ifp->if_flags & IFF_PROMISC) ? "enable" : "disable");
2229 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2230 device_printf(dev, "cannot %s all-multicast mode\n",
2231 (ifp->if_flags & IFF_ALLMULTI) ? "enable" : "disable");
2234 static int
2235 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2237 struct sglist_seg segs[3];
2238 struct sglist sg;
2239 struct {
2240 struct virtio_net_ctrl_hdr hdr __aligned(2);
2241 uint8_t pad1;
2242 uint8_t onoff;
2243 uint8_t pad2;
2244 uint8_t ack;
2245 } s;
2246 int error;
2248 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2249 ("%s: CTRL_RX feature not negotiated", __func__));
2251 s.hdr.class = VIRTIO_NET_CTRL_RX;
2252 s.hdr.cmd = cmd;
2253 s.onoff = !!on;
2254 s.ack = VIRTIO_NET_ERR;
2256 sglist_init(&sg, 3, segs);
2257 error = 0;
2258 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2259 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
2260 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2261 KASSERT(error == 0 && sg.sg_nseg == 3,
2262 ("%s: error %d adding Rx message to sglist", __func__, error));
2264 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2266 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2269 static int
2270 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2273 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2276 static int
2277 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2280 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2283 static void
2284 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2286 struct virtio_net_ctrl_hdr hdr __aligned(2);
2287 struct vtnet_mac_filter *filter;
2288 struct sglist_seg segs[4];
2289 struct sglist sg;
2290 struct ifnet *ifp;
2291 struct ifaddr *ifa;
2292 struct ifaddr_container *ifac;
2293 struct ifmultiaddr *ifma;
2294 int ucnt, mcnt, promisc, allmulti, error;
2295 uint8_t ack;
2297 ifp = sc->vtnet_ifp;
2298 ucnt = 0;
2299 mcnt = 0;
2300 promisc = 0;
2301 allmulti = 0;
2303 ASSERT_SERIALIZED(&sc->vtnet_slz);
2304 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2305 ("%s: CTRL_RX feature not negotiated", __func__));
2307 /* Use the MAC filtering table allocated in vtnet_attach. */
2308 filter = sc->vtnet_macfilter;
2309 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2311 /* Unicast MAC addresses: */
2312 //if_addr_rlock(ifp);
2313 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2314 ifa = ifac->ifa;
2315 if (ifa->ifa_addr->sa_family != AF_LINK)
2316 continue;
2317 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2318 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
2319 continue;
2320 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
2321 promisc = 1;
2322 break;
2325 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2326 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2327 ucnt++;
2329 //if_addr_runlock(ifp);
2331 if (promisc != 0) {
2332 filter->vmf_unicast.nentries = 0;
2333 if_printf(ifp, "more than %d MAC addresses assigned, "
2334 "falling back to promiscuous mode\n",
2335 VTNET_MAX_MAC_ENTRIES);
2336 } else
2337 filter->vmf_unicast.nentries = ucnt;
2339 /* Multicast MAC addresses: */
2340 //if_maddr_rlock(ifp);
2341 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2342 if (ifma->ifma_addr->sa_family != AF_LINK)
2343 continue;
2344 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
2345 allmulti = 1;
2346 break;
2349 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2350 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2351 mcnt++;
2353 //if_maddr_runlock(ifp);
2355 if (allmulti != 0) {
2356 filter->vmf_multicast.nentries = 0;
2357 if_printf(ifp, "more than %d multicast MAC addresses "
2358 "assigned, falling back to all-multicast mode\n",
2359 VTNET_MAX_MAC_ENTRIES);
2360 } else
2361 filter->vmf_multicast.nentries = mcnt;
2363 if (promisc != 0 && allmulti != 0)
2364 goto out;
2366 hdr.class = VIRTIO_NET_CTRL_MAC;
2367 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2368 ack = VIRTIO_NET_ERR;
2370 sglist_init(&sg, 4, segs);
2371 error = 0;
2372 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2373 error |= sglist_append(&sg, &filter->vmf_unicast,
2374 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
2375 error |= sglist_append(&sg, &filter->vmf_multicast,
2376 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
2377 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2378 KASSERT(error == 0 && sg.sg_nseg == 4,
2379 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
2381 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2383 if (ack != VIRTIO_NET_OK)
2384 if_printf(ifp, "error setting host MAC filter table\n");
2386 out:
2387 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
2388 if_printf(ifp, "cannot enable promiscuous mode\n");
2389 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
2390 if_printf(ifp, "cannot enable all-multicast mode\n");
2393 static int
2394 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2396 struct sglist_seg segs[3];
2397 struct sglist sg;
2398 struct {
2399 struct virtio_net_ctrl_hdr hdr __aligned(2);
2400 uint8_t pad1;
2401 uint16_t tag;
2402 uint8_t pad2;
2403 uint8_t ack;
2404 } s;
2405 int error;
2407 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
2408 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2409 s.tag = tag;
2410 s.ack = VIRTIO_NET_ERR;
2412 sglist_init(&sg, 3, segs);
2413 error = 0;
2414 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2415 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
2416 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2417 KASSERT(error == 0 && sg.sg_nseg == 3,
2418 ("%s: error %d adding VLAN message to sglist", __func__, error));
2420 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2422 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2425 static void
2426 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2428 uint32_t w;
2429 uint16_t tag;
2430 int i, bit, nvlans;
2432 ASSERT_SERIALIZED(&sc->vtnet_slz);
2433 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2434 ("%s: VLAN_FILTER feature not negotiated", __func__));
2436 nvlans = sc->vtnet_nvlans;
2438 /* Enable the filter for each configured VLAN. */
2439 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2440 w = sc->vtnet_vlan_shadow[i];
2441 while ((bit = ffs(w) - 1) != -1) {
2442 w &= ~(1 << bit);
2443 tag = sizeof(w) * CHAR_BIT * i + bit;
2444 nvlans--;
2446 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
2447 device_printf(sc->vtnet_dev,
2448 "cannot enable VLAN %d filter\n", tag);
2453 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2456 static void
2457 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2459 struct ifnet *ifp;
2460 int idx, bit;
2462 ifp = sc->vtnet_ifp;
2463 idx = (tag >> 5) & 0x7F;
2464 bit = tag & 0x1F;
2466 if (tag == 0 || tag > 4095)
2467 return;
2469 lwkt_serialize_enter(&sc->vtnet_slz);
2471 /* Update shadow VLAN table. */
2472 if (add) {
2473 sc->vtnet_nvlans++;
2474 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2475 } else {
2476 sc->vtnet_nvlans--;
2477 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2480 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
2481 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2482 device_printf(sc->vtnet_dev,
2483 "cannot %s VLAN %d %s the host filter table\n",
2484 add ? "add" : "remove", tag, add ? "to" : "from");
2487 lwkt_serialize_exit(&sc->vtnet_slz);
2490 static void
2491 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2494 if (ifp->if_softc != arg)
2495 return;
2497 vtnet_update_vlan_filter(arg, 1, tag);
2500 static void
2501 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2504 if (ifp->if_softc != arg)
2505 return;
2507 vtnet_update_vlan_filter(arg, 0, tag);
2510 static int
2511 vtnet_ifmedia_upd(struct ifnet *ifp)
2513 struct vtnet_softc *sc;
2514 struct ifmedia *ifm;
2516 sc = ifp->if_softc;
2517 ifm = &sc->vtnet_media;
2519 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2520 return (EINVAL);
2522 return (0);
2525 static void
2526 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2528 struct vtnet_softc *sc;
2530 sc = ifp->if_softc;
2532 ifmr->ifm_status = IFM_AVALID;
2533 ifmr->ifm_active = IFM_ETHER;
2535 if (vtnet_is_link_up(sc) != 0) {
2536 ifmr->ifm_status |= IFM_ACTIVE;
2537 ifmr->ifm_active |= VTNET_MEDIATYPE;
2538 } else
2539 ifmr->ifm_active |= IFM_NONE;
2542 static void
2543 vtnet_add_statistics(struct vtnet_softc *sc)
2545 device_t dev;
2546 struct vtnet_statistics *stats;
2547 struct sysctl_ctx_list *ctx;
2548 struct sysctl_oid *tree;
2549 struct sysctl_oid_list *child;
2551 dev = sc->vtnet_dev;
2552 stats = &sc->vtnet_stats;
2553 ctx = device_get_sysctl_ctx(dev);
2554 tree = device_get_sysctl_tree(dev);
2555 child = SYSCTL_CHILDREN(tree);
2557 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2558 CTLFLAG_RD, &stats->mbuf_alloc_failed, 0,
2559 "Mbuf cluster allocation failures");
2561 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
2562 CTLFLAG_RD, &stats->rx_frame_too_large, 0,
2563 "Received frame larger than the mbuf chain");
2564 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2565 CTLFLAG_RD, &stats->rx_enq_replacement_failed, 0,
2566 "Enqueuing the replacement receive mbuf failed");
2567 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
2568 CTLFLAG_RD, &stats->rx_mergeable_failed, 0,
2569 "Mergeable buffers receive failures");
2570 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2571 CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 0,
2572 "Received checksum offloaded buffer with unsupported "
2573 "Ethernet type");
2574 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2575 CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 0,
2576 "Received checksum offloaded buffer with incorrect IP protocol");
2577 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2578 CTLFLAG_RD, &stats->rx_csum_bad_offset, 0,
2579 "Received checksum offloaded buffer with incorrect offset");
2580 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
2581 CTLFLAG_RD, &stats->rx_csum_failed, 0,
2582 "Received buffer checksum offload failed");
2583 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
2584 CTLFLAG_RD, &stats->rx_csum_offloaded, 0,
2585 "Received buffer checksum offload succeeded");
2586 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
2587 CTLFLAG_RD, &stats->rx_task_rescheduled, 0,
2588 "Times the receive interrupt task rescheduled itself");
2590 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2591 CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 0,
2592 "Aborted transmit of checksum offloaded buffer with unknown "
2593 "Ethernet type");
2594 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2595 CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 0,
2596 "Aborted transmit of TSO buffer with unknown Ethernet type");
2597 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
2598 CTLFLAG_RD, &stats->tx_defragged, 0,
2599 "Transmit mbufs defragged");
2600 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
2601 CTLFLAG_RD, &stats->tx_defrag_failed, 0,
2602 "Aborted transmit of buffer because defrag failed");
2603 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
2604 CTLFLAG_RD, &stats->tx_csum_offloaded, 0,
2605 "Offloaded checksum of transmitted buffer");
2606 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
2607 CTLFLAG_RD, &stats->tx_tso_offloaded, 0,
2608 "Segmentation offload of transmitted buffer");
2609 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
2610 CTLFLAG_RD, &stats->tx_task_rescheduled, 0,
2611 "Times the transmit interrupt task rescheduled itself");
2614 static int
2615 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2618 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2621 static void
2622 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2625 virtqueue_disable_intr(sc->vtnet_rx_vq);
2628 static int
2629 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2632 #ifdef VTNET_TX_INTR_MODERATION
2633 return (0);
2634 #else
2635 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2636 #endif
2639 static void
2640 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2643 virtqueue_disable_intr(sc->vtnet_tx_vq);