2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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>
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>
47 #include <machine/limits.h>
49 #include <net/ethernet.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>
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
*);
90 static void vtnet_watchdog(struct vtnet_softc
*);
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,
103 static int vtnet_replace_rxbuf(struct vtnet_softc
*,
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
*);
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" },
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
),
216 static driver_t vtnet_driver
= {
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);
229 vtnet_probe(device_t dev
)
231 if (virtio_get_device_type(dev
) != VIRTIO_ID_NETWORK
)
234 device_set_desc(dev
, "VirtIO Networking Adapter");
236 return (BUS_PROBE_DEFAULT
);
241 driver_intr_t
*handler
;
245 vtnet_attach(device_t dev
)
247 struct vtnet_softc
*sc
;
250 sc
= device_get_softc(dev
);
253 lwkt_serialize_init(&sc
->vtnet_slz
);
255 ifmedia_init(&sc
->vtnet_media
, IFM_IMASK
, vtnet_ifmedia_upd
,
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
);
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
);
299 device_printf(dev
, "cannot allocate interrupts\n");
303 error
= vtnet_alloc_virtqueues(sc
);
305 device_printf(dev
, "cannot allocate virtqueues\n");
309 /* XXX Separate function */
310 struct irqmap info
[2];
312 /* Possible "Virtqueue <-> IRQ" configurations */
313 switch (sc
->vtnet_nintr
) {
315 info
[0] = (struct irqmap
){0, vtnet_rx_vq_intr
};
316 info
[1] = (struct irqmap
){0, vtnet_tx_vq_intr
};
319 if (virtio_with_feature(dev
, VIRTIO_NET_F_STATUS
)) {
320 info
[0] = (struct irqmap
){1, vtnet_rx_vq_intr
};
322 info
[0] = (struct irqmap
){0, vtnet_rx_vq_intr
};
324 info
[1] = (struct irqmap
){1, vtnet_tx_vq_intr
};
327 info
[0] = (struct irqmap
){1, vtnet_rx_vq_intr
};
328 info
[1] = (struct irqmap
){2, vtnet_tx_vq_intr
};
331 device_printf(dev
, "Invalid interrupt vector count: %d\n",
335 for (i
= 0; i
< 2; i
++) {
336 error
= virtio_bind_intr(dev
, info
[i
].irq
, i
,
337 info
[i
].handler
, sc
);
339 device_printf(dev
, "cannot bind virtqueue IRQs\n");
343 if (virtio_with_feature(dev
, VIRTIO_NET_F_STATUS
)) {
344 error
= virtio_bind_intr(dev
, 0, -1, vtnet_config_intr
, sc
);
346 device_printf(dev
, "cannot bind config_change IRQ\n");
351 /* Read (or generate) the MAC address for the adapter. */
352 vtnet_get_hwaddr(sc
);
354 error
= vtnet_setup_interface(sc
);
356 device_printf(dev
, "cannot setup interface\n");
360 for (i
= 0; i
< sc
->vtnet_nintr
; i
++) {
361 error
= virtio_setup_intr(dev
, i
, &sc
->vtnet_slz
);
363 device_printf(dev
, "cannot setup virtqueue "
365 ether_ifdetach(sc
->vtnet_ifp
);
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
;
385 "cannot disable promiscuous mode\n");
387 lwkt_serialize_exit(&sc
->vtnet_slz
);
389 sc
->vtnet_ifp
->if_flags
|= IFF_PROMISC
;
399 vtnet_detach(device_t dev
)
401 struct vtnet_softc
*sc
;
405 sc
= device_get_softc(dev
);
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
);
414 lwkt_serialize_exit(&sc
->vtnet_slz
);
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
;
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
),
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
);
459 vtnet_suspend(device_t dev
)
461 struct vtnet_softc
*sc
;
463 sc
= device_get_softc(dev
);
465 lwkt_serialize_enter(&sc
->vtnet_slz
);
467 sc
->vtnet_flags
|= VTNET_FLAG_SUSPENDED
;
468 lwkt_serialize_exit(&sc
->vtnet_slz
);
474 vtnet_resume(device_t dev
)
476 struct vtnet_softc
*sc
;
479 sc
= device_get_softc(dev
);
482 lwkt_serialize_enter(&sc
->vtnet_slz
);
483 if (ifp
->if_flags
& IFF_UP
)
485 sc
->vtnet_flags
&= ~VTNET_FLAG_SUSPENDED
;
486 lwkt_serialize_exit(&sc
->vtnet_slz
);
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
));
503 vtnet_negotiate_features(struct vtnet_softc
*sc
)
506 uint64_t mask
, features
;
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
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
)) {
551 "LRO disabled due to both mergeable buffers and "
552 "indirect descriptors not negotiated\n");
554 features
&= ~VTNET_LRO_FEATURES
;
556 virtio_negotiate_features(dev
, features
);
558 sc
->vtnet_flags
|= VTNET_FLAG_LRO_NOMRG
;
563 vtnet_alloc_intrs(struct vtnet_softc
*sc
)
566 int intrcount
= virtio_intr_count(sc
->vtnet_dev
);
570 if (virtio_with_feature(sc
->vtnet_dev
, VIRTIO_NET_F_STATUS
)) {
572 /* We can use a maximum of 3 interrupt vectors. */
573 intrcount
= imin(intrcount
, 3);
575 /* We can use a maximum of 2 interrupt vectors. */
576 intrcount
= imin(intrcount
, 2);
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;
590 error
= virtio_intr_alloc(sc
->vtnet_dev
, &cnt
, use_config
,
593 virtio_intr_release(sc
->vtnet_dev
);
596 sc
->vtnet_nintr
= cnt
;
602 vtnet_alloc_virtqueues(struct vtnet_softc
*sc
)
605 struct vq_alloc_info vq_info
[3];
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
;
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
;
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
) {
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
));
647 vtnet_setup_interface(struct vtnet_softc
*sc
)
655 ifp
= sc
->vtnet_ifp
= if_alloc(IFT_ETHER
);
657 device_printf(dev
, "cannot allocate ifnet structure\n");
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
;
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");
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
) {
688 "cannot contigmalloc the mac filter table\n");
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
;
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
);
756 vtnet_set_hwaddr(struct vtnet_softc
*sc
)
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
);
774 vtnet_get_hwaddr(struct vtnet_softc
*sc
)
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);
793 virtio_read_device_config(dev
,
794 offsetof(struct virtio_net_config
, mac
),
795 sc
->vtnet_hwaddr
, ETHER_ADDR_LEN
);
799 vtnet_is_link_up(struct vtnet_softc
*sc
)
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
));
814 status
= VIRTIO_NET_S_LINK_UP
;
817 return ((status
& VIRTIO_NET_S_LINK_UP
) != 0);
821 vtnet_update_link_status(struct vtnet_softc
*sc
)
825 struct ifaltq_subque
*ifsq
;
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
;
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
;
845 device_printf(dev
, "Link is down\n");
847 ifp
->if_link_state
= LINK_STATE_DOWN
;
848 if_link_state_change(ifp
);
854 vtnet_watchdog(struct vtnet_softc
*sc
)
860 #ifdef VTNET_TX_INTR_MODERATION
864 if (sc
->vtnet_watchdog_timer
== 0 || --sc
->vtnet_watchdog_timer
)
867 if_printf(ifp
, "watchdog timeout -- resetting\n");
869 virtqueue_dump(sc
->vtnet_tx_vq
);
872 ifp
->if_flags
&= ~IFF_RUNNING
;
878 vtnet_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
,struct ucred
*cr
)
880 struct vtnet_softc
*sc
;
882 int reinit
, mask
, error
;
885 ifr
= (struct ifreq
*) data
;
891 if (ifr
->ifr_mtu
< ETHERMIN
|| ifr
->ifr_mtu
> VTNET_MAX_MTU
)
893 else if (ifp
->if_mtu
!= ifr
->ifr_mtu
)
894 error
= vtnet_change_mtu(sc
, ifr
->ifr_mtu
);
898 if ((ifp
->if_flags
& IFF_UP
) == 0) {
899 if (ifp
->if_flags
& IFF_RUNNING
)
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
)
914 sc
->vtnet_if_flags
= ifp
->if_flags
;
919 if ((sc
->vtnet_flags
& VTNET_FLAG_CTRL_RX
) &&
920 (ifp
->if_flags
& IFF_RUNNING
))
921 vtnet_rx_filter_mac(sc
);
926 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->vtnet_media
, cmd
);
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
;
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
;
946 ifp
->if_hwassist
&= ~CSUM_TSO
;
949 if (mask
& IFCAP_RXCSUM
) {
950 ifp
->if_capenable
^= IFCAP_RXCSUM
;
954 #if 0 /* IFCAP_LRO doesn't exist in DragonFly. */
955 if (mask
& IFCAP_LRO
) {
956 ifp
->if_capenable
^= IFCAP_LRO
;
961 if (mask
& IFCAP_VLAN_HWFILTER
) {
962 ifp
->if_capenable
^= IFCAP_VLAN_HWFILTER
;
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
;
976 //VLAN_CAPABILITIES(ifp);
981 error
= ether_ioctl(ifp
, cmd
, data
);
989 vtnet_change_mtu(struct vtnet_softc
*sc
, int new_mtu
)
992 int new_frame_size
, clsize
;
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
)
1003 if (new_frame_size
<= MCLBYTES
)
1006 clsize
= MJUM9BYTES
;
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
)
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
;
1033 vtnet_init_rx_vq(struct vtnet_softc
*sc
)
1035 struct virtqueue
*vq
;
1038 vq
= sc
->vtnet_rx_vq
;
1042 while (!virtqueue_full(vq
)) {
1043 if ((error
= vtnet_newbuf(sc
)) != 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
1058 if (error
== EMSGSIZE
)
1066 vtnet_free_rx_mbufs(struct vtnet_softc
*sc
)
1068 struct virtqueue
*vq
;
1072 vq
= sc
->vtnet_rx_vq
;
1075 while ((m
= virtqueue_drain(vq
, &last
)) != NULL
)
1078 KASSERT(virtqueue_empty(vq
), ("mbufs remaining in Rx Vq"));
1082 vtnet_free_tx_mbufs(struct vtnet_softc
*sc
)
1084 struct virtqueue
*vq
;
1085 struct vtnet_tx_header
*txhdr
;
1088 vq
= sc
->vtnet_tx_vq
;
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"));
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
;
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
);
1124 m_head
->m_len
= clsize
;
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);
1143 if (m_tailp
!= NULL
)
1149 sc
->vtnet_stats
.mbuf_alloc_failed
++;
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
;
1167 clsize
= sc
->vtnet_rx_mbuf_size
;
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.
1182 * Something is seriously wrong if we received
1183 * a frame larger than the mbuf chain. Drop it.
1186 sc
->vtnet_stats
.rx_frame_too_large
++;
1190 KASSERT(m
->m_len
== clsize
,
1191 ("mbuf length not expected cluster size: %d",
1194 m
->m_len
= MIN(m
->m_len
, len
);
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
;
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
);
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
;
1247 vtnet_newbuf(struct vtnet_softc
*sc
)
1252 m
= vtnet_alloc_rxbuf(sc
, sc
->vtnet_rx_mbuf_count
, NULL
);
1256 error
= vtnet_enqueue_rxbuf(sc
, m
);
1264 vtnet_discard_merged_rxbuf(struct vtnet_softc
*sc
, int nbufs
)
1266 struct virtqueue
*vq
;
1269 vq
= sc
->vtnet_rx_vq
;
1271 while (--nbufs
> 0) {
1272 if ((m
= virtqueue_dequeue(vq
, NULL
)) == NULL
)
1274 vtnet_discard_rxbuf(sc
, m
);
1279 vtnet_discard_rxbuf(struct vtnet_softc
*sc
, struct mbuf
*m
)
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"));
1292 vtnet_enqueue_rxbuf(struct vtnet_softc
*sc
, struct mbuf
*m
)
1295 struct sglist_seg segs
[VTNET_MAX_RX_SEGS
];
1296 struct vtnet_rx_header
*rxhdr
;
1297 struct virtio_net_hdr
*hdr
;
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 *);
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
);
1323 if (m
->m_next
!= NULL
) {
1324 error
= sglist_append_mbuf(&sg
, m
->m_next
);
1329 return (virtqueue_enqueue(sc
->vtnet_rx_vq
, m
, &sg
, 0, sg
.sg_nseg
));
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.
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
;
1365 csum_len
= hdr
->csum_start
+ hdr
->csum_offset
;
1367 if (csum_len
< sizeof(struct ether_header
) + sizeof(struct ip
))
1369 if (m
->m_len
< csum_len
)
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
++;
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
))
1389 udp
= (struct udphdr
*)(mtod(m
, uint8_t *) + hdr
->csum_start
);
1390 if (udp
->uh_sum
== 0)
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;
1401 sc
->vtnet_stats
.rx_csum_bad_offset
++;
1405 sc
->vtnet_stats
.rx_csum_offloaded
++;
1411 vtnet_rxeof_merged(struct vtnet_softc
*sc
, struct mbuf
*m_head
, int nbufs
)
1414 struct virtqueue
*vq
;
1415 struct mbuf
*m
, *m_tail
;
1418 ifp
= sc
->vtnet_ifp
;
1419 vq
= sc
->vtnet_rx_vq
;
1422 while (--nbufs
> 0) {
1423 m
= virtqueue_dequeue(vq
, &len
);
1429 if (vtnet_newbuf(sc
) != 0) {
1431 vtnet_discard_rxbuf(sc
, m
);
1433 vtnet_discard_merged_rxbuf(sc
, nbufs
);
1441 m
->m_flags
&= ~M_PKTHDR
;
1443 m_head
->m_pkthdr
.len
+= len
;
1451 sc
->vtnet_stats
.rx_mergeable_failed
++;
1458 vtnet_rxeof(struct vtnet_softc
*sc
, int count
, int *rx_npktsp
)
1460 struct virtio_net_hdr lhdr
;
1462 struct virtqueue
*vq
;
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
;
1475 ASSERT_SERIALIZED(&sc
->vtnet_slz
);
1477 while (--count
>= 0) {
1478 m
= virtqueue_dequeue(vq
, &len
);
1483 if (len
< sc
->vtnet_hdr_size
+ ETHER_HDR_LEN
) {
1485 vtnet_discard_rxbuf(sc
, m
);
1489 if ((sc
->vtnet_flags
& VTNET_FLAG_MRG_RXBUFS
) == 0) {
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
;
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) {
1505 vtnet_discard_rxbuf(sc
, m
);
1507 vtnet_discard_merged_rxbuf(sc
, nbufs
);
1511 m
->m_pkthdr
.len
= len
;
1512 m
->m_pkthdr
.rcvif
= ifp
;
1513 m
->m_pkthdr
.csum_flags
= 0;
1516 if (vtnet_rxeof_merged(sc
, m
, nbufs
) != 0)
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
1528 memcpy(hdr
, mtod(m
, void *), sizeof(struct virtio_net_hdr
));
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
)
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
);
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)
1565 virtqueue_notify(vq
, &sc
->vtnet_slz
);
1567 if (rx_npktsp
!= NULL
)
1568 *rx_npktsp
= rx_npkts
;
1570 return (count
> 0 ? 0 : EAGAIN
);
1574 vtnet_rx_intr_task(void *arg
)
1576 struct vtnet_softc
*sc
;
1581 ifp
= sc
->vtnet_ifp
;
1584 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
1585 vtnet_enable_rx_intr(sc
);
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
);
1596 sc
->vtnet_stats
.rx_task_rescheduled
++;
1602 vtnet_rx_vq_intr(void *xsc
)
1604 struct vtnet_softc
*sc
;
1608 if (!virtqueue_pending(sc
->vtnet_rx_vq
))
1611 vtnet_disable_rx_intr(sc
);
1612 vtnet_rx_intr_task(sc
);
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
);
1623 vtnet_txeof(struct vtnet_softc
*sc
)
1625 struct virtqueue
*vq
;
1627 struct vtnet_tx_header
*txhdr
;
1630 vq
= sc
->vtnet_tx_vq
;
1631 ifp
= sc
->vtnet_ifp
;
1634 ASSERT_SERIALIZED(&sc
->vtnet_slz
);
1636 while ((txhdr
= virtqueue_dequeue(vq
, NULL
)) != NULL
) {
1639 m_freem(txhdr
->vth_mbuf
);
1640 vtnet_enqueue_txhdr(sc
, txhdr
);
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
)
1655 struct ether_header
*eh
;
1656 struct ether_vlan_header
*evh
;
1658 struct ip6_hdr
*ip6
;
1661 uint16_t eth_type
, csum_start
;
1662 uint8_t ip_proto
, gso_type
;
1664 ifp
= sc
->vtnet_ifp
;
1667 ip_offset
= sizeof(struct ether_header
);
1668 if (m
->m_len
< ip_offset
) {
1669 if ((m
= m_pullup(m
, ip_offset
)) == 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
)
1681 evh
= mtod(m
, struct ether_vlan_header
*);
1682 eth_type
= ntohs(evh
->evl_proto
);
1687 if (m
->m_len
< ip_offset
+ sizeof(struct ip
)) {
1688 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
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
;
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
));
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
;
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
)
1733 if (m
->m_len
< csum_start
+ sizeof(struct tcphdr
)) {
1734 m
= m_pullup(m
, csum_start
+ sizeof(struct tcphdr
));
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 "
1757 hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
1760 sc
->vtnet_stats
.tx_tso_offloaded
++;
1767 vtnet_enqueue_txbuf(struct vtnet_softc
*sc
, struct mbuf
**m_head
,
1768 struct vtnet_tx_header
*txhdr
)
1771 struct sglist_seg segs
[VTNET_MAX_TX_SEGS
];
1772 struct virtqueue
*vq
;
1776 vq
= sc
->vtnet_tx_vq
;
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
);
1786 m
= m_defrag(m
, M_NOWAIT
);
1791 sc
->vtnet_stats
.tx_defragged
++;
1793 error
= sglist_append_mbuf(&sg
, m
);
1798 txhdr
->vth_mbuf
= m
;
1799 error
= virtqueue_enqueue(vq
, txhdr
, &sg
, sg
.sg_nseg
, 0);
1804 sc
->vtnet_stats
.tx_defrag_failed
++;
1811 static struct mbuf
*
1812 vtnet_vlan_tag_insert(struct mbuf
*m
)
1815 struct ether_vlan_header
*evl
;
1817 if (M_WRITABLE(m
) == 0) {
1818 n
= m_dup(m
, M_NOWAIT
);
1820 if ((m
= n
) == NULL
)
1824 M_PREPEND(m
, ETHER_VLAN_ENCAP_LEN
, M_NOWAIT
);
1827 if (m
->m_len
< sizeof(struct ether_vlan_header
)) {
1828 m
= m_pullup(m
, sizeof(struct ether_vlan_header
));
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
;
1845 vtnet_encap(struct vtnet_softc
*sc
, struct mbuf
**m_head
)
1847 struct vtnet_tx_header
*txhdr
;
1848 struct virtio_net_hdr
*hdr
;
1852 txhdr
= SLIST_FIRST(&sc
->vtnet_txhdr_free
);
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
;
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
)
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
)
1882 error
= vtnet_enqueue_txbuf(sc
, m_head
, txhdr
);
1885 vtnet_enqueue_txhdr(sc
, txhdr
);
1890 vtnet_start(struct ifnet
*ifp
, struct ifaltq_subque
*ifsq
)
1892 struct vtnet_softc
*sc
;
1893 struct virtqueue
*vq
;
1898 vq
= sc
->vtnet_tx_vq
;
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))
1908 #ifdef VTNET_TX_INTR_MODERATION
1909 if (virtqueue_nused(vq
) >= sc
->vtnet_tx_size
/ 2)
1913 while (!ifsq_is_empty(ifsq
)) {
1914 if (virtqueue_full(vq
)) {
1915 ifq_set_oactive(&ifp
->if_snd
);
1919 m0
= ifq_dequeue(&ifp
->if_snd
);
1923 if (vtnet_encap(sc
, &m0
) != 0) {
1926 ifq_prepend(&ifp
->if_snd
, m0
);
1927 ifq_set_oactive(&ifp
->if_snd
);
1932 ETHER_BPF_MTAP(ifp
, m0
);
1936 virtqueue_notify(vq
, &sc
->vtnet_slz
);
1937 sc
->vtnet_watchdog_timer
= VTNET_WATCHDOG_TIMEOUT
;
1942 vtnet_tx_intr_task(void *arg
)
1944 struct vtnet_softc
*sc
;
1946 struct ifaltq_subque
*ifsq
;
1949 ifp
= sc
->vtnet_ifp
;
1950 ifsq
= ifq_get_subq_default(&ifp
->if_snd
);
1953 if ((ifp
->if_flags
& IFF_RUNNING
) == 0) {
1954 vtnet_enable_tx_intr(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
++;
1971 vtnet_tx_vq_intr(void *xsc
)
1973 struct vtnet_softc
*sc
;
1977 if (!virtqueue_pending(sc
->vtnet_tx_vq
))
1980 vtnet_disable_tx_intr(sc
);
1981 vtnet_tx_intr_task(sc
);
1985 vtnet_config_intr(void *arg
)
1987 struct vtnet_softc
*sc
;
1991 vtnet_update_link_status(sc
);
1995 vtnet_stop(struct vtnet_softc
*sc
)
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().
2020 sc
->vtnet_flags
&= ~VTNET_FLAG_LINK
;
2022 vtnet_free_rx_mbufs(sc
);
2023 vtnet_free_tx_mbufs(sc
);
2027 vtnet_virtio_reinit(struct vtnet_softc
*sc
)
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
;
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
);
2063 device_printf(dev
, "virtio reinit error %d\n", error
);
2069 vtnet_init(void *xsc
)
2071 struct vtnet_softc
*sc
;
2077 dev
= sc
->vtnet_dev
;
2078 ifp
= sc
->vtnet_ifp
;
2080 ASSERT_SERIALIZED(&sc
->vtnet_slz
);
2082 if (ifp
->if_flags
& IFF_RUNNING
)
2085 /* Stop host's adapter, cancel any pending I/O. */
2088 /* Reinitialize the host device. */
2089 error
= vtnet_virtio_reinit(sc
);
2092 "reinitialization failed, stopping device...\n");
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
);
2110 "cannot allocate mbufs for Rx virtqueue\n");
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
);
2143 vtnet_exec_ctrl_cmd(struct vtnet_softc
*sc
, void *cookie
,
2144 struct sglist
*sg
, int readable
, int writable
)
2146 struct virtqueue
*vq
;
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)
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
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"));
2178 vtnet_ctrl_mac_cmd(struct vtnet_softc
*sc
, uint8_t *hwaddr
)
2181 struct virtio_net_ctrl_hdr hdr
__aligned(2);
2183 char aligned_hwaddr
[ETHER_ADDR_LEN
] __aligned(8);
2187 struct sglist_seg segs
[3];
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
);
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
);
2213 vtnet_rx_filter(struct vtnet_softc
*sc
)
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");
2235 vtnet_ctrl_rx_cmd(struct vtnet_softc
*sc
, int cmd
, int on
)
2237 struct sglist_seg segs
[3];
2240 struct virtio_net_ctrl_hdr hdr
__aligned(2);
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
;
2254 s
.ack
= VIRTIO_NET_ERR
;
2256 sglist_init(&sg
, 3, segs
);
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
);
2270 vtnet_set_promisc(struct vtnet_softc
*sc
, int on
)
2273 return (vtnet_ctrl_rx_cmd(sc
, VIRTIO_NET_CTRL_RX_PROMISC
, on
));
2277 vtnet_set_allmulti(struct vtnet_softc
*sc
, int on
)
2280 return (vtnet_ctrl_rx_cmd(sc
, VIRTIO_NET_CTRL_RX_ALLMULTI
, on
));
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];
2292 struct ifaddr_container
*ifac
;
2293 struct ifmultiaddr
*ifma
;
2294 int ucnt
, mcnt
, promisc
, allmulti
, error
;
2297 ifp
= sc
->vtnet_ifp
;
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
) {
2315 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
2317 else if (memcmp(LLADDR((struct sockaddr_dl
*)ifa
->ifa_addr
),
2318 sc
->vtnet_hwaddr
, ETHER_ADDR_LEN
) == 0)
2320 else if (ucnt
== VTNET_MAX_MAC_ENTRIES
) {
2325 bcopy(LLADDR((struct sockaddr_dl
*)ifa
->ifa_addr
),
2326 &filter
->vmf_unicast
.macs
[ucnt
], ETHER_ADDR_LEN
);
2329 //if_addr_runlock(ifp);
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
);
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
)
2344 else if (mcnt
== VTNET_MAX_MAC_ENTRIES
) {
2349 bcopy(LLADDR((struct sockaddr_dl
*)ifma
->ifma_addr
),
2350 &filter
->vmf_multicast
.macs
[mcnt
], ETHER_ADDR_LEN
);
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
);
2361 filter
->vmf_multicast
.nentries
= mcnt
;
2363 if (promisc
!= 0 && allmulti
!= 0)
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
);
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");
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");
2394 vtnet_exec_vlan_filter(struct vtnet_softc
*sc
, int add
, uint16_t tag
)
2396 struct sglist_seg segs
[3];
2399 struct virtio_net_ctrl_hdr hdr
__aligned(2);
2407 s
.hdr
.class = VIRTIO_NET_CTRL_VLAN
;
2408 s
.hdr
.cmd
= add
? VIRTIO_NET_CTRL_VLAN_ADD
: VIRTIO_NET_CTRL_VLAN_DEL
;
2410 s
.ack
= VIRTIO_NET_ERR
;
2412 sglist_init(&sg
, 3, segs
);
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
);
2426 vtnet_rx_filter_vlan(struct vtnet_softc
*sc
)
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) {
2443 tag
= sizeof(w
) * CHAR_BIT
* i
+ bit
;
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"));
2457 vtnet_update_vlan_filter(struct vtnet_softc
*sc
, int add
, uint16_t tag
)
2462 ifp
= sc
->vtnet_ifp
;
2463 idx
= (tag
>> 5) & 0x7F;
2466 if (tag
== 0 || tag
> 4095)
2469 lwkt_serialize_enter(&sc
->vtnet_slz
);
2471 /* Update shadow VLAN table. */
2474 sc
->vtnet_vlan_shadow
[idx
] |= (1 << bit
);
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
);
2491 vtnet_register_vlan(void *arg
, struct ifnet
*ifp
, uint16_t tag
)
2494 if (ifp
->if_softc
!= arg
)
2497 vtnet_update_vlan_filter(arg
, 1, tag
);
2501 vtnet_unregister_vlan(void *arg
, struct ifnet
*ifp
, uint16_t tag
)
2504 if (ifp
->if_softc
!= arg
)
2507 vtnet_update_vlan_filter(arg
, 0, tag
);
2511 vtnet_ifmedia_upd(struct ifnet
*ifp
)
2513 struct vtnet_softc
*sc
;
2514 struct ifmedia
*ifm
;
2517 ifm
= &sc
->vtnet_media
;
2519 if (IFM_TYPE(ifm
->ifm_media
) != IFM_ETHER
)
2526 vtnet_ifmedia_sts(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
2528 struct vtnet_softc
*sc
;
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
;
2539 ifmr
->ifm_active
|= IFM_NONE
;
2543 vtnet_add_statistics(struct vtnet_softc
*sc
)
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 "
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 "
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");
2615 vtnet_enable_rx_intr(struct vtnet_softc
*sc
)
2618 return (virtqueue_enable_intr(sc
->vtnet_rx_vq
));
2622 vtnet_disable_rx_intr(struct vtnet_softc
*sc
)
2625 virtqueue_disable_intr(sc
->vtnet_rx_vq
);
2629 vtnet_enable_tx_intr(struct vtnet_softc
*sc
)
2632 #ifdef VTNET_TX_INTR_MODERATION
2635 return (virtqueue_enable_intr(sc
->vtnet_tx_vq
));
2640 vtnet_disable_tx_intr(struct vtnet_softc
*sc
)
2643 virtqueue_disable_intr(sc
->vtnet_tx_vq
);