if_vtnet - Cleanup ifalt_subque handling a bit.
[dragonfly.git] / sys / dev / virtual / virtio / net / if_vtnet.c
blobbd076e22a0a24d2b1103f3936d22bed10cec8985
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 static void vtnet_watchdog(struct ifaltq_subque *);
90 static int vtnet_setup_interface(struct vtnet_softc *);
91 static int vtnet_change_mtu(struct vtnet_softc *, int);
92 static int vtnet_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
94 static int vtnet_init_rx_vq(struct vtnet_softc *);
95 static void vtnet_free_rx_mbufs(struct vtnet_softc *);
96 static void vtnet_free_tx_mbufs(struct vtnet_softc *);
97 static void vtnet_free_ctrl_vq(struct vtnet_softc *);
99 static struct mbuf * vtnet_alloc_rxbuf(struct vtnet_softc *, int,
100 struct mbuf **);
101 static int vtnet_replace_rxbuf(struct vtnet_softc *,
102 struct mbuf *, int);
103 static int vtnet_newbuf(struct vtnet_softc *);
104 static void vtnet_discard_merged_rxbuf(struct vtnet_softc *, int);
105 static void vtnet_discard_rxbuf(struct vtnet_softc *, struct mbuf *);
106 static int vtnet_enqueue_rxbuf(struct vtnet_softc *, struct mbuf *);
107 static void vtnet_vlan_tag_remove(struct mbuf *);
108 static int vtnet_rx_csum(struct vtnet_softc *, struct mbuf *,
109 struct virtio_net_hdr *);
110 static int vtnet_rxeof_merged(struct vtnet_softc *, struct mbuf *, int);
111 static int vtnet_rxeof(struct vtnet_softc *, int, int *);
112 static void vtnet_rx_vq_intr(void *);
114 static void vtnet_enqueue_txhdr(struct vtnet_softc *,
115 struct vtnet_tx_header *);
116 static void vtnet_txeof(struct vtnet_softc *);
117 static struct mbuf * vtnet_tx_offload(struct vtnet_softc *, struct mbuf *,
118 struct virtio_net_hdr *);
119 static int vtnet_enqueue_txbuf(struct vtnet_softc *, struct mbuf **,
120 struct vtnet_tx_header *);
121 static int vtnet_encap(struct vtnet_softc *, struct mbuf **);
122 static void vtnet_start(struct ifnet *, struct ifaltq_subque *);
123 static void vtnet_tx_vq_intr(void *);
125 static void vtnet_config_intr(void *);
127 static void vtnet_stop(struct vtnet_softc *);
128 static int vtnet_virtio_reinit(struct vtnet_softc *);
129 static void vtnet_init(void *);
131 static void vtnet_exec_ctrl_cmd(struct vtnet_softc *, void *,
132 struct sglist *, int, int);
134 static int vtnet_ctrl_mac_cmd(struct vtnet_softc *, uint8_t *);
135 static int vtnet_ctrl_rx_cmd(struct vtnet_softc *, int, int);
136 static int vtnet_set_promisc(struct vtnet_softc *, int);
137 static int vtnet_set_allmulti(struct vtnet_softc *, int);
138 static void vtnet_rx_filter(struct vtnet_softc *sc);
139 static void vtnet_rx_filter_mac(struct vtnet_softc *);
141 static int vtnet_exec_vlan_filter(struct vtnet_softc *, int, uint16_t);
142 static void vtnet_rx_filter_vlan(struct vtnet_softc *);
143 static void vtnet_update_vlan_filter(struct vtnet_softc *, int, uint16_t);
144 static void vtnet_register_vlan(void *, struct ifnet *, uint16_t);
145 static void vtnet_unregister_vlan(void *, struct ifnet *, uint16_t);
147 static int vtnet_ifmedia_upd(struct ifnet *);
148 static void vtnet_ifmedia_sts(struct ifnet *, struct ifmediareq *);
150 static void vtnet_add_statistics(struct vtnet_softc *);
152 static int vtnet_enable_rx_intr(struct vtnet_softc *);
153 static int vtnet_enable_tx_intr(struct vtnet_softc *);
154 static void vtnet_disable_rx_intr(struct vtnet_softc *);
155 static void vtnet_disable_tx_intr(struct vtnet_softc *);
157 /* Tunables. */
158 static int vtnet_csum_disable = 0;
159 TUNABLE_INT("hw.vtnet.csum_disable", &vtnet_csum_disable);
160 static int vtnet_tso_disable = 1;
161 TUNABLE_INT("hw.vtnet.tso_disable", &vtnet_tso_disable);
162 static int vtnet_lro_disable = 0;
163 TUNABLE_INT("hw.vtnet.lro_disable", &vtnet_lro_disable);
166 * Reducing the number of transmit completed interrupts can
167 * improve performance. To do so, the define below keeps the
168 * Tx vq interrupt disabled and adds calls to vtnet_txeof()
169 * in the start path. The price to pay for this is the m_free'ing
170 * of transmitted mbufs may be delayed.
172 #define VTNET_TX_INTR_MODERATION
174 static struct virtio_feature_desc vtnet_feature_desc[] = {
175 { VIRTIO_NET_F_CSUM, "TxChecksum" },
176 { VIRTIO_NET_F_GUEST_CSUM, "RxChecksum" },
177 { VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, "DynOffload" },
178 { VIRTIO_NET_F_MAC, "MacAddress" },
179 { VIRTIO_NET_F_GSO, "TxAllGSO" },
180 { VIRTIO_NET_F_GUEST_TSO4, "RxTSOv4" },
181 { VIRTIO_NET_F_GUEST_TSO6, "RxTSOv6" },
182 { VIRTIO_NET_F_GUEST_ECN, "RxECN" },
183 { VIRTIO_NET_F_GUEST_UFO, "RxUFO" },
184 { VIRTIO_NET_F_HOST_TSO4, "TxTSOv4" },
185 { VIRTIO_NET_F_HOST_TSO6, "TxTSOv6" },
186 { VIRTIO_NET_F_HOST_ECN, "TxTSOECN" },
187 { VIRTIO_NET_F_HOST_UFO, "TxUFO" },
188 { VIRTIO_NET_F_MRG_RXBUF, "MrgRxBuf" },
189 { VIRTIO_NET_F_STATUS, "Status" },
190 { VIRTIO_NET_F_CTRL_VQ, "ControlVq" },
191 { VIRTIO_NET_F_CTRL_RX, "RxMode" },
192 { VIRTIO_NET_F_CTRL_VLAN, "VLanFilter" },
193 { VIRTIO_NET_F_CTRL_RX_EXTRA, "RxModeExtra" },
194 { VIRTIO_NET_F_GUEST_ANNOUNCE, "GuestAnnounce" },
195 { VIRTIO_NET_F_MQ, "Multiqueue" },
196 { VIRTIO_NET_F_CTRL_MAC_ADDR, "SetMacAddress" },
197 { 0, NULL }
200 static device_method_t vtnet_methods[] = {
201 /* Device methods. */
202 DEVMETHOD(device_probe, vtnet_probe),
203 DEVMETHOD(device_attach, vtnet_attach),
204 DEVMETHOD(device_detach, vtnet_detach),
205 DEVMETHOD(device_suspend, vtnet_suspend),
206 DEVMETHOD(device_resume, vtnet_resume),
207 DEVMETHOD(device_shutdown, vtnet_shutdown),
209 DEVMETHOD_END
212 static driver_t vtnet_driver = {
213 "vtnet",
214 vtnet_methods,
215 sizeof(struct vtnet_softc)
218 static devclass_t vtnet_devclass;
220 DRIVER_MODULE(vtnet, virtio_pci, vtnet_driver, vtnet_devclass, NULL, NULL);
221 MODULE_VERSION(vtnet, 1);
222 MODULE_DEPEND(vtnet, virtio, 1, 1, 1);
224 static int
225 vtnet_probe(device_t dev)
227 if (virtio_get_device_type(dev) != VIRTIO_ID_NETWORK)
228 return (ENXIO);
230 device_set_desc(dev, "VirtIO Networking Adapter");
232 return (BUS_PROBE_DEFAULT);
235 struct irqmap {
236 int irq;
237 driver_intr_t *handler;
240 static int
241 vtnet_attach(device_t dev)
243 struct vtnet_softc *sc;
244 int i, error;
246 sc = device_get_softc(dev);
247 sc->vtnet_dev = dev;
249 lwkt_serialize_init(&sc->vtnet_slz);
251 ifmedia_init(&sc->vtnet_media, IFM_IMASK, vtnet_ifmedia_upd,
252 vtnet_ifmedia_sts);
253 ifmedia_add(&sc->vtnet_media, VTNET_MEDIATYPE, 0, NULL);
254 ifmedia_set(&sc->vtnet_media, VTNET_MEDIATYPE);
256 vtnet_add_statistics(sc);
257 SLIST_INIT(&sc->vtnet_txhdr_free);
259 /* Register our feature descriptions. */
260 virtio_set_feature_desc(dev, vtnet_feature_desc);
261 vtnet_negotiate_features(sc);
263 if (virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC))
264 sc->vtnet_flags |= VTNET_FLAG_INDIRECT;
266 if (virtio_with_feature(dev, VIRTIO_NET_F_MAC)) {
267 /* This feature should always be negotiated. */
268 sc->vtnet_flags |= VTNET_FLAG_MAC;
271 if (virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF)) {
272 sc->vtnet_flags |= VTNET_FLAG_MRG_RXBUFS;
273 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
274 } else {
275 sc->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
278 sc->vtnet_rx_mbuf_size = MCLBYTES;
279 sc->vtnet_rx_mbuf_count = VTNET_NEEDED_RX_MBUFS(sc);
281 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VQ)) {
282 sc->vtnet_flags |= VTNET_FLAG_CTRL_VQ;
284 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
285 sc->vtnet_flags |= VTNET_FLAG_CTRL_RX;
286 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_VLAN))
287 sc->vtnet_flags |= VTNET_FLAG_VLAN_FILTER;
288 if (virtio_with_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
289 virtio_with_feature(dev, VIRTIO_NET_F_CTRL_RX))
290 sc->vtnet_flags |= VTNET_FLAG_CTRL_MAC;
293 error = vtnet_alloc_intrs(sc);
294 if (error) {
295 device_printf(dev, "cannot allocate interrupts\n");
296 goto fail;
299 error = vtnet_alloc_virtqueues(sc);
300 if (error) {
301 device_printf(dev, "cannot allocate virtqueues\n");
302 goto fail;
305 /* XXX Separate function */
306 struct irqmap info[2];
308 /* Possible "Virtqueue <-> IRQ" configurations */
309 switch (sc->vtnet_nintr) {
310 case 1:
311 info[0] = (struct irqmap){0, vtnet_rx_vq_intr};
312 info[1] = (struct irqmap){0, vtnet_tx_vq_intr};
313 break;
314 case 2:
315 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
316 info[0] = (struct irqmap){1, vtnet_rx_vq_intr};
317 } else {
318 info[0] = (struct irqmap){0, vtnet_rx_vq_intr};
320 info[1] = (struct irqmap){1, vtnet_tx_vq_intr};
321 break;
322 case 3:
323 info[0] = (struct irqmap){1, vtnet_rx_vq_intr};
324 info[1] = (struct irqmap){2, vtnet_tx_vq_intr};
325 break;
326 default:
327 device_printf(dev, "Invalid interrupt vector count: %d\n",
328 sc->vtnet_nintr);
329 goto fail;
331 for (i = 0; i < 2; i++) {
332 error = virtio_bind_intr(dev, info[i].irq, i,
333 info[i].handler, sc);
334 if (error) {
335 device_printf(dev, "cannot bind virtqueue IRQs\n");
336 goto fail;
339 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
340 error = virtio_bind_intr(dev, 0, -1, vtnet_config_intr, sc);
341 if (error) {
342 device_printf(dev, "cannot bind config_change IRQ\n");
343 goto fail;
347 /* Read (or generate) the MAC address for the adapter. */
348 vtnet_get_hwaddr(sc);
350 error = vtnet_setup_interface(sc);
351 if (error) {
352 device_printf(dev, "cannot setup interface\n");
353 goto fail;
356 for (i = 0; i < sc->vtnet_nintr; i++) {
357 error = virtio_setup_intr(dev, i, &sc->vtnet_slz);
358 if (error) {
359 device_printf(dev, "cannot setup virtqueue "
360 "interrupts\n");
361 ether_ifdetach(sc->vtnet_ifp);
362 goto fail;
366 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
367 lwkt_serialize_enter(&sc->vtnet_slz);
368 vtnet_set_hwaddr(sc);
369 lwkt_serialize_exit(&sc->vtnet_slz);
373 * Device defaults to promiscuous mode for backwards
374 * compatibility. Turn it off if possible.
376 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
377 lwkt_serialize_enter(&sc->vtnet_slz);
378 if (vtnet_set_promisc(sc, 0) != 0) {
379 sc->vtnet_ifp->if_flags |= IFF_PROMISC;
380 device_printf(dev,
381 "cannot disable promiscuous mode\n");
383 lwkt_serialize_exit(&sc->vtnet_slz);
384 } else
385 sc->vtnet_ifp->if_flags |= IFF_PROMISC;
387 fail:
388 if (error)
389 vtnet_detach(dev);
391 return (error);
394 static int
395 vtnet_detach(device_t dev)
397 struct vtnet_softc *sc;
398 struct ifnet *ifp;
399 int i;
401 sc = device_get_softc(dev);
402 ifp = sc->vtnet_ifp;
404 for (i = 0; i < sc->vtnet_nintr; i++)
405 virtio_teardown_intr(dev, i);
407 if (device_is_attached(dev)) {
408 lwkt_serialize_enter(&sc->vtnet_slz);
409 vtnet_stop(sc);
410 lwkt_serialize_handler_disable(&sc->vtnet_slz);
411 lwkt_serialize_exit(&sc->vtnet_slz);
413 ether_ifdetach(ifp);
416 if (sc->vtnet_vlan_attach != NULL) {
417 EVENTHANDLER_DEREGISTER(vlan_config, sc->vtnet_vlan_attach);
418 sc->vtnet_vlan_attach = NULL;
420 if (sc->vtnet_vlan_detach != NULL) {
421 EVENTHANDLER_DEREGISTER(vlan_unconfig, sc->vtnet_vlan_detach);
422 sc->vtnet_vlan_detach = NULL;
425 if (ifp) {
426 if_free(ifp);
427 sc->vtnet_ifp = NULL;
430 if (sc->vtnet_rx_vq != NULL)
431 vtnet_free_rx_mbufs(sc);
432 if (sc->vtnet_tx_vq != NULL)
433 vtnet_free_tx_mbufs(sc);
434 if (sc->vtnet_ctrl_vq != NULL)
435 vtnet_free_ctrl_vq(sc);
437 if (sc->vtnet_txhdrarea != NULL) {
438 contigfree(sc->vtnet_txhdrarea,
439 sc->vtnet_txhdrcount * sizeof(struct vtnet_tx_header),
440 M_VTNET);
441 sc->vtnet_txhdrarea = NULL;
443 SLIST_INIT(&sc->vtnet_txhdr_free);
444 if (sc->vtnet_macfilter != NULL) {
445 contigfree(sc->vtnet_macfilter,
446 sizeof(struct vtnet_mac_filter), M_DEVBUF);
447 sc->vtnet_macfilter = NULL;
450 ifmedia_removeall(&sc->vtnet_media);
452 return (0);
455 static int
456 vtnet_suspend(device_t dev)
458 struct vtnet_softc *sc;
460 sc = device_get_softc(dev);
462 lwkt_serialize_enter(&sc->vtnet_slz);
463 vtnet_stop(sc);
464 sc->vtnet_flags |= VTNET_FLAG_SUSPENDED;
465 lwkt_serialize_exit(&sc->vtnet_slz);
467 return (0);
470 static int
471 vtnet_resume(device_t dev)
473 struct vtnet_softc *sc;
474 struct ifnet *ifp;
476 sc = device_get_softc(dev);
477 ifp = sc->vtnet_ifp;
479 lwkt_serialize_enter(&sc->vtnet_slz);
480 if (ifp->if_flags & IFF_UP)
481 vtnet_init(sc);
482 sc->vtnet_flags &= ~VTNET_FLAG_SUSPENDED;
483 lwkt_serialize_exit(&sc->vtnet_slz);
485 return (0);
488 static int
489 vtnet_shutdown(device_t dev)
493 * Suspend already does all of what we need to
494 * do here; we just never expect to be resumed.
496 return (vtnet_suspend(dev));
499 static void
500 vtnet_negotiate_features(struct vtnet_softc *sc)
502 device_t dev;
503 uint64_t mask, features;
505 dev = sc->vtnet_dev;
506 mask = 0;
508 if (vtnet_csum_disable)
509 mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
512 * XXX DragonFly doesn't support receive checksum offload for ipv6 yet,
513 * hence always disable the virtio feature for now.
514 * XXX We need to support the DynOffload feature, in order to
515 * dynamically enable/disable this feature.
517 mask |= VIRTIO_NET_F_GUEST_CSUM;
520 * TSO is only available when the tx checksum offload feature is also
521 * negotiated.
523 if (vtnet_csum_disable || vtnet_tso_disable)
524 mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
525 VIRTIO_NET_F_HOST_ECN;
527 if (vtnet_lro_disable)
528 mask |= VTNET_LRO_FEATURES;
530 features = VTNET_FEATURES & ~mask;
531 features |= VIRTIO_F_NOTIFY_ON_EMPTY;
532 features |= VIRTIO_F_ANY_LAYOUT;
533 sc->vtnet_features = virtio_negotiate_features(dev, features);
535 if (virtio_with_feature(dev, VTNET_LRO_FEATURES) &&
536 virtio_with_feature(dev, VIRTIO_NET_F_MRG_RXBUF) == 0) {
538 * LRO without mergeable buffers requires special care. This
539 * is not ideal because every receive buffer must be large
540 * enough to hold the maximum TCP packet, the Ethernet header,
541 * and the header. This requires up to 34 descriptors with
542 * MCLBYTES clusters. If we do not have indirect descriptors,
543 * LRO is disabled since the virtqueue will not contain very
544 * many receive buffers.
546 if (!virtio_with_feature(dev, VIRTIO_RING_F_INDIRECT_DESC)) {
547 device_printf(dev,
548 "LRO disabled due to both mergeable buffers and "
549 "indirect descriptors not negotiated\n");
551 features &= ~VTNET_LRO_FEATURES;
552 sc->vtnet_features =
553 virtio_negotiate_features(dev, features);
554 } else
555 sc->vtnet_flags |= VTNET_FLAG_LRO_NOMRG;
559 static int
560 vtnet_alloc_intrs(struct vtnet_softc *sc)
562 int cnt, error;
563 int intrcount = virtio_intr_count(sc->vtnet_dev);
564 int i;
565 int use_config;
567 if (virtio_with_feature(sc->vtnet_dev, VIRTIO_NET_F_STATUS)) {
568 use_config = 1;
569 /* We can use a maximum of 3 interrupt vectors. */
570 intrcount = imin(intrcount, 3);
571 } else {
572 /* We can use a maximum of 2 interrupt vectors. */
573 intrcount = imin(intrcount, 2);
576 if (intrcount < 1)
577 return (ENXIO);
580 * XXX We should explicitly set the cpus for the rx/tx threads, to
581 * only use cpus, where the network stack is running.
583 for (i = 0; i < intrcount; i++)
584 sc->vtnet_cpus[i] = -1;
586 cnt = intrcount;
587 error = virtio_intr_alloc(sc->vtnet_dev, &cnt, use_config,
588 sc->vtnet_cpus);
589 if (error != 0) {
590 virtio_intr_release(sc->vtnet_dev);
591 return (error);
593 sc->vtnet_nintr = cnt;
595 return (0);
598 static int
599 vtnet_alloc_virtqueues(struct vtnet_softc *sc)
601 device_t dev;
602 struct vq_alloc_info vq_info[3];
603 int nvqs;
605 dev = sc->vtnet_dev;
606 nvqs = 2;
609 * Indirect descriptors are not needed for the Rx
610 * virtqueue when mergeable buffers are negotiated.
611 * The header is placed inline with the data, not
612 * in a separate descriptor, and mbuf clusters are
613 * always physically contiguous.
615 if ((sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) == 0) {
616 sc->vtnet_rx_nsegs = (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) ?
617 VTNET_MAX_RX_SEGS : VTNET_MIN_RX_SEGS;
618 } else
619 sc->vtnet_rx_nsegs = VTNET_MRG_RX_SEGS;
621 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4) ||
622 virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
623 sc->vtnet_tx_nsegs = VTNET_MAX_TX_SEGS;
624 else
625 sc->vtnet_tx_nsegs = VTNET_MIN_TX_SEGS;
627 VQ_ALLOC_INFO_INIT(&vq_info[0], sc->vtnet_rx_nsegs, &sc->vtnet_rx_vq,
628 "%s receive", device_get_nameunit(dev));
630 VQ_ALLOC_INFO_INIT(&vq_info[1], sc->vtnet_tx_nsegs, &sc->vtnet_tx_vq,
631 "%s transmit", device_get_nameunit(dev));
633 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
634 nvqs++;
636 VQ_ALLOC_INFO_INIT(&vq_info[2], 0, &sc->vtnet_ctrl_vq,
637 "%s control", device_get_nameunit(dev));
640 return (virtio_alloc_virtqueues(dev, nvqs, vq_info));
643 static int
644 vtnet_setup_interface(struct vtnet_softc *sc)
646 device_t dev;
647 struct ifnet *ifp;
648 int i;
650 dev = sc->vtnet_dev;
652 ifp = sc->vtnet_ifp = if_alloc(IFT_ETHER);
653 if (ifp == NULL) {
654 device_printf(dev, "cannot allocate ifnet structure\n");
655 return (ENOSPC);
658 ifp->if_softc = sc;
659 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
660 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
661 ifp->if_init = vtnet_init;
662 ifp->if_start = vtnet_start;
663 ifp->if_ioctl = vtnet_ioctl;
665 sc->vtnet_rx_process_limit = virtqueue_size(sc->vtnet_rx_vq);
666 sc->vtnet_tx_size = virtqueue_size(sc->vtnet_tx_vq);
667 if (sc->vtnet_flags & VTNET_FLAG_INDIRECT)
668 sc->vtnet_txhdrcount = sc->vtnet_tx_size;
669 else
670 sc->vtnet_txhdrcount = (sc->vtnet_tx_size / 2) + 1;
671 sc->vtnet_txhdrarea = contigmalloc(
672 sc->vtnet_txhdrcount * sizeof(struct vtnet_tx_header),
673 M_VTNET, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
674 if (sc->vtnet_txhdrarea == NULL) {
675 device_printf(dev, "cannot contigmalloc the tx headers\n");
676 return (ENOMEM);
678 for (i = 0; i < sc->vtnet_txhdrcount; i++)
679 vtnet_enqueue_txhdr(sc, &sc->vtnet_txhdrarea[i]);
680 sc->vtnet_macfilter = contigmalloc(
681 sizeof(struct vtnet_mac_filter),
682 M_DEVBUF, M_WAITOK, 0, BUS_SPACE_MAXADDR, 4, 0);
683 if (sc->vtnet_macfilter == NULL) {
684 device_printf(dev,
685 "cannot contigmalloc the mac filter table\n");
686 return (ENOMEM);
688 ifq_set_maxlen(&ifp->if_snd, sc->vtnet_tx_size - 1);
689 ifq_set_ready(&ifp->if_snd);
691 ether_ifattach(ifp, sc->vtnet_hwaddr, &sc->vtnet_slz);
693 /* The Tx IRQ is currently always the last allocated interrupt. */
694 ifq_set_cpuid(&ifp->if_snd, sc->vtnet_cpus[sc->vtnet_nintr - 1]);
695 ifsq_watchdog_init(&sc->vtnet_tx_watchdog,
696 ifq_get_subq_default(&ifp->if_snd), vtnet_watchdog);
698 /* Tell the upper layer(s) we support long frames. */
699 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
700 ifp->if_capabilities |= IFCAP_JUMBO_MTU | IFCAP_VLAN_MTU;
702 if (virtio_with_feature(dev, VIRTIO_NET_F_CSUM)) {
703 ifp->if_capabilities |= IFCAP_TXCSUM;
705 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO4))
706 ifp->if_capabilities |= IFCAP_TSO4;
707 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_TSO6))
708 ifp->if_capabilities |= IFCAP_TSO6;
709 if (ifp->if_capabilities & IFCAP_TSO)
710 ifp->if_capabilities |= IFCAP_VLAN_HWTSO;
712 if (virtio_with_feature(dev, VIRTIO_NET_F_HOST_ECN))
713 sc->vtnet_flags |= VTNET_FLAG_TSO_ECN;
716 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_CSUM))
717 ifp->if_capabilities |= IFCAP_RXCSUM;
719 #if 0 /* IFCAP_LRO doesn't exist in DragonFly. */
720 if (virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO4) ||
721 virtio_with_feature(dev, VIRTIO_NET_F_GUEST_TSO6))
722 ifp->if_capabilities |= IFCAP_LRO;
723 #endif
725 if ((ifp->if_capabilities & IFCAP_HWCSUM) == IFCAP_HWCSUM) {
727 * VirtIO does not support VLAN tagging, but we can fake
728 * it by inserting and removing the 802.1Q header during
729 * transmit and receive. We are then able to do checksum
730 * offloading of VLAN frames.
732 ifp->if_capabilities |=
733 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM;
736 ifp->if_capenable = ifp->if_capabilities;
739 * Capabilities after here are not enabled by default.
742 if (sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER) {
743 ifp->if_capabilities |= IFCAP_VLAN_HWFILTER;
745 sc->vtnet_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
746 vtnet_register_vlan, sc, EVENTHANDLER_PRI_FIRST);
747 sc->vtnet_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
748 vtnet_unregister_vlan, sc, EVENTHANDLER_PRI_FIRST);
751 return (0);
754 static void
755 vtnet_set_hwaddr(struct vtnet_softc *sc)
757 device_t dev;
759 dev = sc->vtnet_dev;
761 if ((sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) &&
762 (sc->vtnet_flags & VTNET_FLAG_CTRL_RX)) {
763 if (vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr) != 0)
764 device_printf(dev, "unable to set MAC address\n");
765 } else if (sc->vtnet_flags & VTNET_FLAG_MAC) {
766 virtio_write_device_config(dev,
767 offsetof(struct virtio_net_config, mac),
768 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
772 static void
773 vtnet_get_hwaddr(struct vtnet_softc *sc)
775 device_t dev;
777 dev = sc->vtnet_dev;
779 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0) {
781 * Generate a random locally administered unicast address.
783 * It would be nice to generate the same MAC address across
784 * reboots, but it seems all the hosts currently available
785 * support the MAC feature, so this isn't too important.
787 sc->vtnet_hwaddr[0] = 0xB2;
788 karc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1);
789 return;
792 virtio_read_device_config(dev,
793 offsetof(struct virtio_net_config, mac),
794 sc->vtnet_hwaddr, ETHER_ADDR_LEN);
797 static int
798 vtnet_is_link_up(struct vtnet_softc *sc)
800 device_t dev;
801 struct ifnet *ifp;
802 uint16_t status;
804 dev = sc->vtnet_dev;
805 ifp = sc->vtnet_ifp;
807 ASSERT_SERIALIZED(&sc->vtnet_slz);
809 if (virtio_with_feature(dev, VIRTIO_NET_F_STATUS)) {
810 status = virtio_read_dev_config_2(dev,
811 offsetof(struct virtio_net_config, status));
812 } else {
813 status = VIRTIO_NET_S_LINK_UP;
816 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
819 static void
820 vtnet_update_link_status(struct vtnet_softc *sc)
822 device_t dev;
823 struct ifnet *ifp;
824 struct ifaltq_subque *ifsq;
825 int link;
827 dev = sc->vtnet_dev;
828 ifp = sc->vtnet_ifp;
829 ifsq = ifq_get_subq_default(&ifp->if_snd);
831 link = vtnet_is_link_up(sc);
833 if (link && ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0)) {
834 sc->vtnet_flags |= VTNET_FLAG_LINK;
835 if (bootverbose)
836 device_printf(dev, "Link is up\n");
837 ifp->if_link_state = LINK_STATE_UP;
838 if_link_state_change(ifp);
839 if (!ifsq_is_empty(ifsq))
840 ifsq_devstart_sched(ifsq);
841 } else if (!link && (sc->vtnet_flags & VTNET_FLAG_LINK)) {
842 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
843 if (bootverbose)
844 device_printf(dev, "Link is down\n");
846 ifp->if_link_state = LINK_STATE_DOWN;
847 if_link_state_change(ifp);
851 static void
852 vtnet_watchdog(struct ifaltq_subque *ifsq)
854 struct ifnet *ifp;
855 struct vtnet_softc *sc;
857 ifp = ifsq_get_ifp(ifsq);
858 sc = ifp->if_softc;
859 ASSERT_IFNET_SERIALIZED_ALL(ifp);
861 if (virtqueue_empty(sc->vtnet_tx_vq)) {
862 if_printf(ifp, "Spurious TX watchdog timeout -- ignoring\n");
863 sc->vtnet_tx_watchdog.wd_timer = 0;
864 return;
867 if_printf(ifp, "TX 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);
874 ifsq_devstart_sched(ifsq);
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, NULL);
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 /* XXX Don't drop the serializer, when we use MULTI SERIALIZERS MODE. */
1566 virtqueue_notify(vq, &sc->vtnet_slz);
1568 if (rx_npktsp != NULL)
1569 *rx_npktsp = rx_npkts;
1571 return (count > 0 ? 0 : EAGAIN);
1574 static void
1575 vtnet_rx_vq_intr(void *xsc)
1577 struct vtnet_softc *sc;
1578 struct ifnet *ifp;
1579 int more;
1581 sc = xsc;
1582 ifp = sc->vtnet_ifp;
1584 if (!virtqueue_pending(sc->vtnet_rx_vq))
1585 return;
1587 vtnet_disable_rx_intr(sc);
1588 next:
1589 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1590 vtnet_enable_rx_intr(sc);
1591 return;
1594 more = vtnet_rxeof(sc, sc->vtnet_rx_process_limit, NULL);
1595 if (!more && vtnet_enable_rx_intr(sc) != 0) {
1596 vtnet_disable_rx_intr(sc);
1597 more = 1;
1600 if (more) {
1601 sc->vtnet_stats.rx_task_rescheduled++;
1602 goto next;
1606 static void
1607 vtnet_enqueue_txhdr(struct vtnet_softc *sc, struct vtnet_tx_header *txhdr)
1609 bzero(txhdr, sizeof(*txhdr));
1610 SLIST_INSERT_HEAD(&sc->vtnet_txhdr_free, txhdr, link);
1613 static void
1614 vtnet_txeof(struct vtnet_softc *sc)
1616 struct virtqueue *vq;
1617 struct ifnet *ifp;
1618 struct vtnet_tx_header *txhdr;
1619 int deq;
1621 vq = sc->vtnet_tx_vq;
1622 ifp = sc->vtnet_ifp;
1623 deq = 0;
1625 ASSERT_SERIALIZED(&sc->vtnet_slz);
1627 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
1628 deq++;
1629 ifp->if_opackets++;
1630 m_freem(txhdr->vth_mbuf);
1631 vtnet_enqueue_txhdr(sc, txhdr);
1634 if (deq > 0) {
1635 ifq_clr_oactive(&ifp->if_snd);
1636 if (virtqueue_empty(vq))
1637 sc->vtnet_tx_watchdog.wd_timer = 0;
1638 else
1639 sc->vtnet_tx_watchdog.wd_timer = VTNET_WATCHDOG_TIMEOUT;
1643 static struct mbuf *
1644 vtnet_tx_offload(struct vtnet_softc *sc, struct mbuf *m,
1645 struct virtio_net_hdr *hdr)
1647 struct ifnet *ifp;
1648 struct ether_header *eh;
1649 struct ether_vlan_header *evh;
1650 struct ip *ip;
1651 struct ip6_hdr *ip6;
1652 struct tcphdr *tcp;
1653 int ip_offset;
1654 uint16_t eth_type, csum_start;
1655 uint8_t ip_proto, gso_type;
1657 ifp = sc->vtnet_ifp;
1658 M_ASSERTPKTHDR(m);
1660 ip_offset = sizeof(struct ether_header);
1661 if (m->m_len < ip_offset) {
1662 if ((m = m_pullup(m, ip_offset)) == NULL)
1663 return (NULL);
1666 eh = mtod(m, struct ether_header *);
1667 eth_type = ntohs(eh->ether_type);
1668 if (eth_type == ETHERTYPE_VLAN) {
1669 ip_offset = sizeof(struct ether_vlan_header);
1670 if (m->m_len < ip_offset) {
1671 if ((m = m_pullup(m, ip_offset)) == NULL)
1672 return (NULL);
1674 evh = mtod(m, struct ether_vlan_header *);
1675 eth_type = ntohs(evh->evl_proto);
1678 switch (eth_type) {
1679 case ETHERTYPE_IP:
1680 if (m->m_len < ip_offset + sizeof(struct ip)) {
1681 m = m_pullup(m, ip_offset + sizeof(struct ip));
1682 if (m == NULL)
1683 return (NULL);
1686 ip = (struct ip *)(mtod(m, uint8_t *) + ip_offset);
1687 ip_proto = ip->ip_p;
1688 csum_start = ip_offset + (ip->ip_hl << 2);
1689 gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1690 break;
1692 case ETHERTYPE_IPV6:
1693 if (m->m_len < ip_offset + sizeof(struct ip6_hdr)) {
1694 m = m_pullup(m, ip_offset + sizeof(struct ip6_hdr));
1695 if (m == NULL)
1696 return (NULL);
1699 ip6 = (struct ip6_hdr *)(mtod(m, uint8_t *) + ip_offset);
1701 * XXX Assume no extension headers are present. Presently,
1702 * this will always be true in the case of TSO, and FreeBSD
1703 * does not perform checksum offloading of IPv6 yet.
1705 ip_proto = ip6->ip6_nxt;
1706 csum_start = ip_offset + sizeof(struct ip6_hdr);
1707 gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1708 break;
1710 default:
1711 return (m);
1714 if (m->m_pkthdr.csum_flags & VTNET_CSUM_OFFLOAD) {
1715 hdr->flags |= VIRTIO_NET_HDR_F_NEEDS_CSUM;
1716 hdr->csum_start = csum_start;
1717 hdr->csum_offset = m->m_pkthdr.csum_data;
1719 sc->vtnet_stats.tx_csum_offloaded++;
1722 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
1723 if (ip_proto != IPPROTO_TCP)
1724 return (m);
1726 if (m->m_len < csum_start + sizeof(struct tcphdr)) {
1727 m = m_pullup(m, csum_start + sizeof(struct tcphdr));
1728 if (m == NULL)
1729 return (NULL);
1732 tcp = (struct tcphdr *)(mtod(m, uint8_t *) + csum_start);
1733 hdr->gso_type = gso_type;
1734 hdr->hdr_len = csum_start + (tcp->th_off << 2);
1735 hdr->gso_size = m->m_pkthdr.tso_segsz;
1737 if (tcp->th_flags & TH_CWR) {
1739 * Drop if we did not negotiate VIRTIO_NET_F_HOST_ECN.
1740 * ECN support is only configurable globally with the
1741 * net.inet.tcp.ecn.enable sysctl knob.
1743 if ((sc->vtnet_flags & VTNET_FLAG_TSO_ECN) == 0) {
1744 if_printf(ifp, "TSO with ECN not supported "
1745 "by host\n");
1746 m_freem(m);
1747 return (NULL);
1750 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1753 sc->vtnet_stats.tx_tso_offloaded++;
1756 return (m);
1759 static int
1760 vtnet_enqueue_txbuf(struct vtnet_softc *sc, struct mbuf **m_head,
1761 struct vtnet_tx_header *txhdr)
1763 struct sglist sg;
1764 struct sglist_seg segs[VTNET_MAX_TX_SEGS];
1765 struct virtqueue *vq;
1766 struct mbuf *m;
1767 int error;
1769 vq = sc->vtnet_tx_vq;
1770 m = *m_head;
1772 sglist_init(&sg, sc->vtnet_tx_nsegs, segs);
1773 error = sglist_append(&sg, &txhdr->vth_uhdr, sc->vtnet_hdr_size);
1774 KASSERT(error == 0 && sg.sg_nseg == 1,
1775 ("%s: error %d adding header to sglist", __func__, error));
1777 error = sglist_append_mbuf(&sg, m);
1778 if (error) {
1779 m = m_defrag(m, M_NOWAIT);
1780 if (m == NULL)
1781 goto fail;
1783 *m_head = m;
1784 sc->vtnet_stats.tx_defragged++;
1786 error = sglist_append_mbuf(&sg, m);
1787 if (error)
1788 goto fail;
1791 txhdr->vth_mbuf = m;
1792 error = virtqueue_enqueue(vq, txhdr, &sg, sg.sg_nseg, 0);
1794 return (error);
1796 fail:
1797 sc->vtnet_stats.tx_defrag_failed++;
1798 m_freem(*m_head);
1799 *m_head = NULL;
1801 return (ENOBUFS);
1804 static struct mbuf *
1805 vtnet_vlan_tag_insert(struct mbuf *m)
1807 struct mbuf *n;
1808 struct ether_vlan_header *evl;
1810 if (M_WRITABLE(m) == 0) {
1811 n = m_dup(m, M_NOWAIT);
1812 m_freem(m);
1813 if ((m = n) == NULL)
1814 return (NULL);
1817 M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1818 if (m == NULL)
1819 return (NULL);
1820 if (m->m_len < sizeof(struct ether_vlan_header)) {
1821 m = m_pullup(m, sizeof(struct ether_vlan_header));
1822 if (m == NULL)
1823 return (NULL);
1826 /* Insert 802.1Q header into the existing Ethernet header. */
1827 evl = mtod(m, struct ether_vlan_header *);
1828 bcopy((char *) evl + ETHER_VLAN_ENCAP_LEN,
1829 (char *) evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1830 evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1831 evl->evl_tag = htons(m->m_pkthdr.ether_vlantag);
1832 m->m_flags &= ~M_VLANTAG;
1834 return (m);
1837 static int
1838 vtnet_encap(struct vtnet_softc *sc, struct mbuf **m_head)
1840 struct vtnet_tx_header *txhdr;
1841 struct virtio_net_hdr *hdr;
1842 struct mbuf *m;
1843 int error;
1845 txhdr = SLIST_FIRST(&sc->vtnet_txhdr_free);
1846 if (txhdr == NULL)
1847 return (ENOBUFS);
1848 SLIST_REMOVE_HEAD(&sc->vtnet_txhdr_free, link);
1851 * Always use the non-mergeable header to simplify things. When
1852 * the mergeable feature is negotiated, the num_buffers field
1853 * must be set to zero. We use vtnet_hdr_size later to enqueue
1854 * the correct header size to the host.
1856 hdr = &txhdr->vth_uhdr.hdr;
1857 m = *m_head;
1859 error = ENOBUFS;
1861 if (m->m_flags & M_VLANTAG) {
1862 //m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
1863 m = vtnet_vlan_tag_insert(m);
1864 if ((*m_head = m) == NULL)
1865 goto fail;
1866 m->m_flags &= ~M_VLANTAG;
1869 if (m->m_pkthdr.csum_flags != 0) {
1870 m = vtnet_tx_offload(sc, m, hdr);
1871 if ((*m_head = m) == NULL)
1872 goto fail;
1875 error = vtnet_enqueue_txbuf(sc, m_head, txhdr);
1876 fail:
1877 if (error != 0)
1878 vtnet_enqueue_txhdr(sc, txhdr);
1879 return (error);
1882 static void
1883 vtnet_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1885 struct vtnet_softc *sc;
1886 struct virtqueue *vq;
1887 struct mbuf *m0;
1888 int enq;
1890 sc = ifp->if_softc;
1891 vq = sc->vtnet_tx_vq;
1892 enq = 0;
1894 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1895 ASSERT_SERIALIZED(&sc->vtnet_slz);
1897 if ((ifp->if_flags & (IFF_RUNNING)) !=
1898 IFF_RUNNING || ((sc->vtnet_flags & VTNET_FLAG_LINK) == 0))
1899 return;
1901 #ifdef VTNET_TX_INTR_MODERATION
1902 if (virtqueue_nused(vq) >= sc->vtnet_tx_size / 2)
1903 vtnet_txeof(sc);
1904 #endif
1906 while (!ifsq_is_empty(ifsq)) {
1907 if (virtqueue_full(vq)) {
1908 ifsq_set_oactive(ifsq);
1909 break;
1912 m0 = ifsq_dequeue(ifsq);
1913 if (m0 == NULL)
1914 break;
1916 if (vtnet_encap(sc, &m0) != 0) {
1917 if (m0 == NULL)
1918 break;
1919 ifsq_prepend(ifsq, m0);
1920 ifsq_set_oactive(ifsq);
1921 break;
1924 enq++;
1925 ETHER_BPF_MTAP(ifp, m0);
1928 if (enq > 0) {
1930 * XXX Don't drop the serializer, when we use
1931 * MULTI SERIALIZERS MODE.
1933 virtqueue_notify(vq, &sc->vtnet_slz);
1934 sc->vtnet_tx_watchdog.wd_timer = VTNET_WATCHDOG_TIMEOUT;
1938 static void
1939 vtnet_tx_vq_intr(void *xsc)
1941 struct vtnet_softc *sc;
1942 struct ifnet *ifp;
1943 struct ifaltq_subque *ifsq;
1945 sc = xsc;
1946 ifp = sc->vtnet_ifp;
1947 ifsq = ifq_get_subq_default(&ifp->if_snd);
1949 if (!virtqueue_pending(sc->vtnet_tx_vq))
1950 return;
1952 vtnet_disable_tx_intr(sc);
1953 next:
1954 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1955 vtnet_enable_tx_intr(sc);
1956 return;
1959 vtnet_txeof(sc);
1961 if (!ifsq_is_empty(ifsq))
1962 ifsq_devstart(ifsq);
1964 if (vtnet_enable_tx_intr(sc) != 0) {
1965 vtnet_disable_tx_intr(sc);
1966 sc->vtnet_stats.tx_task_rescheduled++;
1967 goto next;
1971 static void
1972 vtnet_config_intr(void *arg)
1974 struct vtnet_softc *sc;
1976 sc = arg;
1978 vtnet_update_link_status(sc);
1981 static void
1982 vtnet_stop(struct vtnet_softc *sc)
1984 device_t dev;
1985 struct ifnet *ifp;
1987 dev = sc->vtnet_dev;
1988 ifp = sc->vtnet_ifp;
1990 ASSERT_SERIALIZED(&sc->vtnet_slz);
1992 ifq_clr_oactive(&ifp->if_snd);
1993 ifsq_watchdog_stop(&sc->vtnet_tx_watchdog);
1994 ifp->if_flags &= ~(IFF_RUNNING);
1996 vtnet_disable_rx_intr(sc);
1997 vtnet_disable_tx_intr(sc);
2000 * Stop the host VirtIO adapter. Note this will reset the host
2001 * adapter's state back to the pre-initialized state, so in
2002 * order to make the device usable again, we must drive it
2003 * through virtio_reinit() and virtio_reinit_complete().
2005 virtio_stop(dev);
2007 sc->vtnet_flags &= ~VTNET_FLAG_LINK;
2009 vtnet_free_rx_mbufs(sc);
2010 vtnet_free_tx_mbufs(sc);
2013 static int
2014 vtnet_virtio_reinit(struct vtnet_softc *sc)
2016 device_t dev;
2017 struct ifnet *ifp;
2018 uint64_t features;
2019 int error;
2021 dev = sc->vtnet_dev;
2022 ifp = sc->vtnet_ifp;
2023 features = sc->vtnet_features;
2026 * Re-negotiate with the host, removing any disabled receive
2027 * features. Transmit features are disabled only on our side
2028 * via if_capenable and if_hwassist.
2031 if (ifp->if_capabilities & IFCAP_RXCSUM) {
2032 if ((ifp->if_capenable & IFCAP_RXCSUM) == 0)
2033 features &= ~VIRTIO_NET_F_GUEST_CSUM;
2036 #if 0 /* IFCAP_LRO doesn't exist in DragonFly. */
2037 if (ifp->if_capabilities & IFCAP_LRO) {
2038 if ((ifp->if_capenable & IFCAP_LRO) == 0)
2039 features &= ~VTNET_LRO_FEATURES;
2041 #endif
2043 if (ifp->if_capabilities & IFCAP_VLAN_HWFILTER) {
2044 if ((ifp->if_capenable & IFCAP_VLAN_HWFILTER) == 0)
2045 features &= ~VIRTIO_NET_F_CTRL_VLAN;
2048 error = virtio_reinit(dev, features);
2049 if (error)
2050 device_printf(dev, "virtio reinit error %d\n", error);
2052 return (error);
2055 static void
2056 vtnet_init(void *xsc)
2058 struct vtnet_softc *sc;
2059 device_t dev;
2060 struct ifnet *ifp;
2061 int error;
2063 sc = xsc;
2064 dev = sc->vtnet_dev;
2065 ifp = sc->vtnet_ifp;
2067 ASSERT_SERIALIZED(&sc->vtnet_slz);
2069 if (ifp->if_flags & IFF_RUNNING)
2070 return;
2072 /* Stop host's adapter, cancel any pending I/O. */
2073 vtnet_stop(sc);
2075 /* Reinitialize the host device. */
2076 error = vtnet_virtio_reinit(sc);
2077 if (error) {
2078 device_printf(dev,
2079 "reinitialization failed, stopping device...\n");
2080 vtnet_stop(sc);
2081 return;
2084 /* Update host with assigned MAC address. */
2085 bcopy(IF_LLADDR(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
2086 vtnet_set_hwaddr(sc);
2088 ifp->if_hwassist = 0;
2089 if (ifp->if_capenable & IFCAP_TXCSUM)
2090 ifp->if_hwassist |= VTNET_CSUM_OFFLOAD;
2091 if (ifp->if_capenable & IFCAP_TSO4)
2092 ifp->if_hwassist |= CSUM_TSO;
2094 error = vtnet_init_rx_vq(sc);
2095 if (error) {
2096 device_printf(dev,
2097 "cannot allocate mbufs for Rx virtqueue\n");
2098 vtnet_stop(sc);
2099 return;
2102 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ) {
2103 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
2104 /* Restore promiscuous and all-multicast modes. */
2105 vtnet_rx_filter(sc);
2107 /* Restore filtered MAC addresses. */
2108 vtnet_rx_filter_mac(sc);
2111 /* Restore VLAN filters. */
2112 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2113 vtnet_rx_filter_vlan(sc);
2117 vtnet_enable_rx_intr(sc);
2118 vtnet_enable_tx_intr(sc);
2121 ifp->if_flags |= IFF_RUNNING;
2122 ifq_clr_oactive(&ifp->if_snd);
2123 ifsq_watchdog_start(&sc->vtnet_tx_watchdog);
2125 virtio_reinit_complete(dev);
2127 vtnet_update_link_status(sc);
2130 static void
2131 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, void *cookie,
2132 struct sglist *sg, int readable, int writable)
2134 struct virtqueue *vq;
2135 void *c;
2137 vq = sc->vtnet_ctrl_vq;
2139 ASSERT_SERIALIZED(&sc->vtnet_slz);
2140 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ,
2141 ("no control virtqueue"));
2142 KASSERT(virtqueue_empty(vq),
2143 ("control command already enqueued"));
2145 if (virtqueue_enqueue(vq, cookie, sg, readable, writable) != 0)
2146 return;
2149 * XXX We can safely drop the serializer between here, and the end of
2150 * the function, when we can correctly sleep for this command to
2151 * be finished.
2153 virtqueue_notify(vq, NULL);
2156 * Poll until the command is complete. Previously, we would
2157 * sleep until the control virtqueue interrupt handler woke
2158 * us up, but dropping the VTNET_MTX leads to serialization
2159 * difficulties.
2161 * Furthermore, it appears QEMU/KVM only allocates three MSIX
2162 * vectors. Two of those vectors are needed for the Rx and Tx
2163 * virtqueues. We do not support sharing both a Vq and config
2164 * changed notification on the same MSIX vector.
2166 c = virtqueue_poll(vq, NULL);
2167 KASSERT(c == cookie, ("unexpected control command response"));
2170 static int
2171 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
2173 struct {
2174 struct virtio_net_ctrl_hdr hdr __aligned(2);
2175 uint8_t pad1;
2176 char aligned_hwaddr[ETHER_ADDR_LEN] __aligned(8);
2177 uint8_t pad2;
2178 uint8_t ack;
2179 } s;
2180 struct sglist_seg segs[3];
2181 struct sglist sg;
2182 int error;
2184 s.hdr.class = VIRTIO_NET_CTRL_MAC;
2185 s.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
2186 s.ack = VIRTIO_NET_ERR;
2188 /* Copy the mac address into physically contiguous memory */
2189 memcpy(s.aligned_hwaddr, hwaddr, ETHER_ADDR_LEN);
2191 sglist_init(&sg, 3, segs);
2192 error = 0;
2193 error |= sglist_append(&sg, &s.hdr,
2194 sizeof(struct virtio_net_ctrl_hdr));
2195 error |= sglist_append(&sg, s.aligned_hwaddr, ETHER_ADDR_LEN);
2196 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2197 KASSERT(error == 0 && sg.sg_nseg == 3,
2198 ("%s: error %d adding set MAC msg to sglist", __func__, error));
2200 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2202 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2205 static void
2206 vtnet_rx_filter(struct vtnet_softc *sc)
2208 device_t dev;
2209 struct ifnet *ifp;
2211 dev = sc->vtnet_dev;
2212 ifp = sc->vtnet_ifp;
2214 ASSERT_SERIALIZED(&sc->vtnet_slz);
2215 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2216 ("CTRL_RX feature not negotiated"));
2218 if (vtnet_set_promisc(sc, ifp->if_flags & IFF_PROMISC) != 0)
2219 device_printf(dev, "cannot %s promiscuous mode\n",
2220 (ifp->if_flags & IFF_PROMISC) ? "enable" : "disable");
2222 if (vtnet_set_allmulti(sc, ifp->if_flags & IFF_ALLMULTI) != 0)
2223 device_printf(dev, "cannot %s all-multicast mode\n",
2224 (ifp->if_flags & IFF_ALLMULTI) ? "enable" : "disable");
2227 static int
2228 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, int cmd, int on)
2230 struct sglist_seg segs[3];
2231 struct sglist sg;
2232 struct {
2233 struct virtio_net_ctrl_hdr hdr __aligned(2);
2234 uint8_t pad1;
2235 uint8_t onoff;
2236 uint8_t pad2;
2237 uint8_t ack;
2238 } s;
2239 int error;
2241 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2242 ("%s: CTRL_RX feature not negotiated", __func__));
2244 s.hdr.class = VIRTIO_NET_CTRL_RX;
2245 s.hdr.cmd = cmd;
2246 s.onoff = !!on;
2247 s.ack = VIRTIO_NET_ERR;
2249 sglist_init(&sg, 3, segs);
2250 error = 0;
2251 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2252 error |= sglist_append(&sg, &s.onoff, sizeof(uint8_t));
2253 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2254 KASSERT(error == 0 && sg.sg_nseg == 3,
2255 ("%s: error %d adding Rx message to sglist", __func__, error));
2257 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2259 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2262 static int
2263 vtnet_set_promisc(struct vtnet_softc *sc, int on)
2266 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
2269 static int
2270 vtnet_set_allmulti(struct vtnet_softc *sc, int on)
2273 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
2276 static void
2277 vtnet_rx_filter_mac(struct vtnet_softc *sc)
2279 struct virtio_net_ctrl_hdr hdr __aligned(2);
2280 struct vtnet_mac_filter *filter;
2281 struct sglist_seg segs[4];
2282 struct sglist sg;
2283 struct ifnet *ifp;
2284 struct ifaddr *ifa;
2285 struct ifaddr_container *ifac;
2286 struct ifmultiaddr *ifma;
2287 int ucnt, mcnt, promisc, allmulti, error;
2288 uint8_t ack;
2290 ifp = sc->vtnet_ifp;
2291 ucnt = 0;
2292 mcnt = 0;
2293 promisc = 0;
2294 allmulti = 0;
2296 ASSERT_SERIALIZED(&sc->vtnet_slz);
2297 KASSERT(sc->vtnet_flags & VTNET_FLAG_CTRL_RX,
2298 ("%s: CTRL_RX feature not negotiated", __func__));
2300 /* Use the MAC filtering table allocated in vtnet_attach. */
2301 filter = sc->vtnet_macfilter;
2302 memset(filter, 0, sizeof(struct vtnet_mac_filter));
2304 /* Unicast MAC addresses: */
2305 //if_addr_rlock(ifp);
2306 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2307 ifa = ifac->ifa;
2308 if (ifa->ifa_addr->sa_family != AF_LINK)
2309 continue;
2310 else if (memcmp(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2311 sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
2312 continue;
2313 else if (ucnt == VTNET_MAX_MAC_ENTRIES) {
2314 promisc = 1;
2315 break;
2318 bcopy(LLADDR((struct sockaddr_dl *)ifa->ifa_addr),
2319 &filter->vmf_unicast.macs[ucnt], ETHER_ADDR_LEN);
2320 ucnt++;
2322 //if_addr_runlock(ifp);
2324 if (promisc != 0) {
2325 filter->vmf_unicast.nentries = 0;
2326 if_printf(ifp, "more than %d MAC addresses assigned, "
2327 "falling back to promiscuous mode\n",
2328 VTNET_MAX_MAC_ENTRIES);
2329 } else
2330 filter->vmf_unicast.nentries = ucnt;
2332 /* Multicast MAC addresses: */
2333 //if_maddr_rlock(ifp);
2334 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2335 if (ifma->ifma_addr->sa_family != AF_LINK)
2336 continue;
2337 else if (mcnt == VTNET_MAX_MAC_ENTRIES) {
2338 allmulti = 1;
2339 break;
2342 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
2343 &filter->vmf_multicast.macs[mcnt], ETHER_ADDR_LEN);
2344 mcnt++;
2346 //if_maddr_runlock(ifp);
2348 if (allmulti != 0) {
2349 filter->vmf_multicast.nentries = 0;
2350 if_printf(ifp, "more than %d multicast MAC addresses "
2351 "assigned, falling back to all-multicast mode\n",
2352 VTNET_MAX_MAC_ENTRIES);
2353 } else
2354 filter->vmf_multicast.nentries = mcnt;
2356 if (promisc != 0 && allmulti != 0)
2357 goto out;
2359 hdr.class = VIRTIO_NET_CTRL_MAC;
2360 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
2361 ack = VIRTIO_NET_ERR;
2363 sglist_init(&sg, 4, segs);
2364 error = 0;
2365 error |= sglist_append(&sg, &hdr, sizeof(struct virtio_net_ctrl_hdr));
2366 error |= sglist_append(&sg, &filter->vmf_unicast,
2367 sizeof(uint32_t) + filter->vmf_unicast.nentries * ETHER_ADDR_LEN);
2368 error |= sglist_append(&sg, &filter->vmf_multicast,
2369 sizeof(uint32_t) + filter->vmf_multicast.nentries * ETHER_ADDR_LEN);
2370 error |= sglist_append(&sg, &ack, sizeof(uint8_t));
2371 KASSERT(error == 0 && sg.sg_nseg == 4,
2372 ("%s: error %d adding MAC filter msg to sglist", __func__, error));
2374 vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg - 1, 1);
2376 if (ack != VIRTIO_NET_OK)
2377 if_printf(ifp, "error setting host MAC filter table\n");
2379 out:
2380 if (promisc != 0 && vtnet_set_promisc(sc, 1) != 0)
2381 if_printf(ifp, "cannot enable promiscuous mode\n");
2382 if (allmulti != 0 && vtnet_set_allmulti(sc, 1) != 0)
2383 if_printf(ifp, "cannot enable all-multicast mode\n");
2386 static int
2387 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2389 struct sglist_seg segs[3];
2390 struct sglist sg;
2391 struct {
2392 struct virtio_net_ctrl_hdr hdr __aligned(2);
2393 uint8_t pad1;
2394 uint16_t tag;
2395 uint8_t pad2;
2396 uint8_t ack;
2397 } s;
2398 int error;
2400 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
2401 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
2402 s.tag = tag;
2403 s.ack = VIRTIO_NET_ERR;
2405 sglist_init(&sg, 3, segs);
2406 error = 0;
2407 error |= sglist_append(&sg, &s.hdr, sizeof(struct virtio_net_ctrl_hdr));
2408 error |= sglist_append(&sg, &s.tag, sizeof(uint16_t));
2409 error |= sglist_append(&sg, &s.ack, sizeof(uint8_t));
2410 KASSERT(error == 0 && sg.sg_nseg == 3,
2411 ("%s: error %d adding VLAN message to sglist", __func__, error));
2413 vtnet_exec_ctrl_cmd(sc, &s.ack, &sg, sg.sg_nseg - 1, 1);
2415 return (s.ack == VIRTIO_NET_OK ? 0 : EIO);
2418 static void
2419 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
2421 uint32_t w;
2422 uint16_t tag;
2423 int i, bit, nvlans;
2425 ASSERT_SERIALIZED(&sc->vtnet_slz);
2426 KASSERT(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER,
2427 ("%s: VLAN_FILTER feature not negotiated", __func__));
2429 nvlans = sc->vtnet_nvlans;
2431 /* Enable the filter for each configured VLAN. */
2432 for (i = 0; i < VTNET_VLAN_SHADOW_SIZE && nvlans > 0; i++) {
2433 w = sc->vtnet_vlan_shadow[i];
2434 while ((bit = ffs(w) - 1) != -1) {
2435 w &= ~(1 << bit);
2436 tag = sizeof(w) * CHAR_BIT * i + bit;
2437 nvlans--;
2439 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
2440 device_printf(sc->vtnet_dev,
2441 "cannot enable VLAN %d filter\n", tag);
2446 KASSERT(nvlans == 0, ("VLAN count incorrect"));
2449 static void
2450 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
2452 struct ifnet *ifp;
2453 int idx, bit;
2455 ifp = sc->vtnet_ifp;
2456 idx = (tag >> 5) & 0x7F;
2457 bit = tag & 0x1F;
2459 if (tag == 0 || tag > 4095)
2460 return;
2462 lwkt_serialize_enter(&sc->vtnet_slz);
2464 /* Update shadow VLAN table. */
2465 if (add) {
2466 sc->vtnet_nvlans++;
2467 sc->vtnet_vlan_shadow[idx] |= (1 << bit);
2468 } else {
2469 sc->vtnet_nvlans--;
2470 sc->vtnet_vlan_shadow[idx] &= ~(1 << bit);
2473 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER &&
2474 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
2475 device_printf(sc->vtnet_dev,
2476 "cannot %s VLAN %d %s the host filter table\n",
2477 add ? "add" : "remove", tag, add ? "to" : "from");
2480 lwkt_serialize_exit(&sc->vtnet_slz);
2483 static void
2484 vtnet_register_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2487 if (ifp->if_softc != arg)
2488 return;
2490 vtnet_update_vlan_filter(arg, 1, tag);
2493 static void
2494 vtnet_unregister_vlan(void *arg, struct ifnet *ifp, uint16_t tag)
2497 if (ifp->if_softc != arg)
2498 return;
2500 vtnet_update_vlan_filter(arg, 0, tag);
2503 static int
2504 vtnet_ifmedia_upd(struct ifnet *ifp)
2506 struct vtnet_softc *sc;
2507 struct ifmedia *ifm;
2509 sc = ifp->if_softc;
2510 ifm = &sc->vtnet_media;
2512 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2513 return (EINVAL);
2515 return (0);
2518 static void
2519 vtnet_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2521 struct vtnet_softc *sc;
2523 sc = ifp->if_softc;
2525 ifmr->ifm_status = IFM_AVALID;
2526 ifmr->ifm_active = IFM_ETHER;
2528 if (vtnet_is_link_up(sc) != 0) {
2529 ifmr->ifm_status |= IFM_ACTIVE;
2530 ifmr->ifm_active |= VTNET_MEDIATYPE;
2531 } else
2532 ifmr->ifm_active |= IFM_NONE;
2535 static void
2536 vtnet_add_statistics(struct vtnet_softc *sc)
2538 device_t dev;
2539 struct vtnet_statistics *stats;
2540 struct sysctl_ctx_list *ctx;
2541 struct sysctl_oid *tree;
2542 struct sysctl_oid_list *child;
2544 dev = sc->vtnet_dev;
2545 stats = &sc->vtnet_stats;
2546 ctx = device_get_sysctl_ctx(dev);
2547 tree = device_get_sysctl_tree(dev);
2548 child = SYSCTL_CHILDREN(tree);
2550 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
2551 CTLFLAG_RD, &stats->mbuf_alloc_failed, 0,
2552 "Mbuf cluster allocation failures");
2554 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
2555 CTLFLAG_RD, &stats->rx_frame_too_large, 0,
2556 "Received frame larger than the mbuf chain");
2557 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
2558 CTLFLAG_RD, &stats->rx_enq_replacement_failed, 0,
2559 "Enqueuing the replacement receive mbuf failed");
2560 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
2561 CTLFLAG_RD, &stats->rx_mergeable_failed, 0,
2562 "Mergeable buffers receive failures");
2563 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
2564 CTLFLAG_RD, &stats->rx_csum_bad_ethtype, 0,
2565 "Received checksum offloaded buffer with unsupported "
2566 "Ethernet type");
2567 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
2568 CTLFLAG_RD, &stats->rx_csum_bad_ipproto, 0,
2569 "Received checksum offloaded buffer with incorrect IP protocol");
2570 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_offset",
2571 CTLFLAG_RD, &stats->rx_csum_bad_offset, 0,
2572 "Received checksum offloaded buffer with incorrect offset");
2573 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_failed",
2574 CTLFLAG_RD, &stats->rx_csum_failed, 0,
2575 "Received buffer checksum offload failed");
2576 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_offloaded",
2577 CTLFLAG_RD, &stats->rx_csum_offloaded, 0,
2578 "Received buffer checksum offload succeeded");
2579 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_task_rescheduled",
2580 CTLFLAG_RD, &stats->rx_task_rescheduled, 0,
2581 "Times the receive interrupt task rescheduled itself");
2583 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_bad_ethtype",
2584 CTLFLAG_RD, &stats->tx_csum_bad_ethtype, 0,
2585 "Aborted transmit of checksum offloaded buffer with unknown "
2586 "Ethernet type");
2587 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_bad_ethtype",
2588 CTLFLAG_RD, &stats->tx_tso_bad_ethtype, 0,
2589 "Aborted transmit of TSO buffer with unknown Ethernet type");
2590 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
2591 CTLFLAG_RD, &stats->tx_defragged, 0,
2592 "Transmit mbufs defragged");
2593 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
2594 CTLFLAG_RD, &stats->tx_defrag_failed, 0,
2595 "Aborted transmit of buffer because defrag failed");
2596 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_offloaded",
2597 CTLFLAG_RD, &stats->tx_csum_offloaded, 0,
2598 "Offloaded checksum of transmitted buffer");
2599 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_offloaded",
2600 CTLFLAG_RD, &stats->tx_tso_offloaded, 0,
2601 "Segmentation offload of transmitted buffer");
2602 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_task_rescheduled",
2603 CTLFLAG_RD, &stats->tx_task_rescheduled, 0,
2604 "Times the transmit interrupt task rescheduled itself");
2607 static int
2608 vtnet_enable_rx_intr(struct vtnet_softc *sc)
2611 return (virtqueue_enable_intr(sc->vtnet_rx_vq));
2614 static void
2615 vtnet_disable_rx_intr(struct vtnet_softc *sc)
2618 virtqueue_disable_intr(sc->vtnet_rx_vq);
2621 static int
2622 vtnet_enable_tx_intr(struct vtnet_softc *sc)
2625 #ifdef VTNET_TX_INTR_MODERATION
2626 return (0);
2627 #else
2628 return (virtqueue_enable_intr(sc->vtnet_tx_vq));
2629 #endif
2632 static void
2633 vtnet_disable_tx_intr(struct vtnet_softc *sc)
2636 virtqueue_disable_intr(sc->vtnet_tx_vq);