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