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