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, reinit, update;
1460
1461 ifp = sc->vtnet_ifp;
1462 mask = (ifr->ifr_reqcap & if_getcapabilities(ifp)) ^ if_getcapenable(ifp);
1463 reinit = update = 0;
1464
1465 VTNET_CORE_LOCK_ASSERT(sc);
1466
1467 if (mask & IFCAP_TXCSUM) {
1468 if (if_getcapenable(ifp) & IFCAP_TXCSUM &&
1469 if_getcapenable(ifp) & IFCAP_TSO4) {
1470 /* Disable tso4, because txcsum will be disabled. */
1471 if_setcapenablebit(ifp, 0, IFCAP_TSO4);
1472 if_sethwassistbits(ifp, 0, CSUM_IP_TSO);
1473 mask &= ~IFCAP_TSO4;
1474 }
1475 if_togglecapenable(ifp, IFCAP_TXCSUM);
1476 if_togglehwassist(ifp, VTNET_CSUM_OFFLOAD);
1477 }
1478 if (mask & IFCAP_TXCSUM_IPV6) {
1479 if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6 &&
1480 if_getcapenable(ifp) & IFCAP_TSO6) {
1481 /* Disable tso6, because txcsum6 will be disabled. */
1482 if_setcapenablebit(ifp, 0, IFCAP_TSO6);
1483 if_sethwassistbits(ifp, 0, CSUM_IP6_TSO);
1484 mask &= ~IFCAP_TSO6;
1485 }
1486 if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6);
1487 if_togglehwassist(ifp, VTNET_CSUM_OFFLOAD_IPV6);
1488 }
1489 if (mask & IFCAP_TSO4) {
1490 if (if_getcapenable(ifp) & (IFCAP_TXCSUM | IFCAP_TSO4)) {
1491 /* tso4 can only be enabled, if txcsum is enabled. */
1492 if_togglecapenable(ifp, IFCAP_TSO4);
1493 if_togglehwassist(ifp, CSUM_IP_TSO);
1494 }
1495 }
1496 if (mask & IFCAP_TSO6) {
1497 if (if_getcapenable(ifp) & (IFCAP_TXCSUM_IPV6 | IFCAP_TSO6)) {
1498 /* tso6 can only be enabled, if txcsum6 is enabled. */
1499 if_togglecapenable(ifp, IFCAP_TSO6);
1500 if_togglehwassist(ifp, CSUM_IP6_TSO);
1501 }
1502 }
1503
1504 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO)) {
1505 /*
1506 * These Rx features require the negotiated features to
1507 * be updated. Avoid a full reinit if possible.
1508 */
1509 if (sc->vtnet_features & VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
1510 update = 1;
1511 else
1512 reinit = 1;
1513
1514 /* BMV: Avoid needless renegotiation for just software LRO. */
1515 if ((mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO)) ==
1516 IFCAP_LRO && vtnet_software_lro(sc))
1517 reinit = update = 0;
1518 /*
1519 * VirtIO does not distinguish between receive checksum offload
1520 * for IPv4 and IPv6 packets, so treat them as a pair.
1521 */
1522 if (mask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
1523 if_togglecapenable(ifp, IFCAP_RXCSUM);
1524 if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6);
1525 }
1526 if (mask & IFCAP_LRO)
1527 if_togglecapenable(ifp, IFCAP_LRO);
1528 /* Both SW and HW TCP LRO require receive checksum offload. */
1529 if ((if_getcapenable(ifp) &
1530 (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) == 0)
1531 if_setcapenablebit(ifp, 0, IFCAP_LRO);
1532 }
1533
1534 if (mask & IFCAP_VLAN_HWFILTER) {
1535 /* These Rx features require renegotiation. */
1536 reinit = 1;
1537
1538 if (mask & IFCAP_VLAN_HWFILTER)
1539 if_togglecapenable(ifp, IFCAP_VLAN_HWFILTER);
1540 }
1541
1542 if (mask & IFCAP_VLAN_HWTSO)
1543 if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
1544 if (mask & IFCAP_VLAN_HWTAGGING)
1545 if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
1546
1547 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1548 if (reinit) {
1549 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1550 vtnet_init_locked(sc, 0);
1551 } else if (update)
1552 vtnet_update_rx_offloads(sc);
1553 }
1554
1555 return (0);
1556 }
1557
1558 static int
vtnet_ioctl(if_t ifp,u_long cmd,caddr_t data)1559 vtnet_ioctl(if_t ifp, u_long cmd, caddr_t data)
1560 {
1561 struct vtnet_softc *sc;
1562 struct ifreq *ifr;
1563 int error;
1564
1565 sc = if_getsoftc(ifp);
1566 ifr = (struct ifreq *) data;
1567 error = 0;
1568
1569 switch (cmd) {
1570 case SIOCSIFMTU:
1571 VTNET_CORE_LOCK(sc);
1572 error = vtnet_ioctl_mtu(sc, ifr->ifr_mtu);
1573 VTNET_CORE_UNLOCK(sc);
1574 break;
1575
1576 case SIOCSIFFLAGS:
1577 VTNET_CORE_LOCK(sc);
1578 error = vtnet_ioctl_ifflags(sc);
1579 VTNET_CORE_UNLOCK(sc);
1580 break;
1581
1582 case SIOCADDMULTI:
1583 case SIOCDELMULTI:
1584 VTNET_CORE_LOCK(sc);
1585 error = vtnet_ioctl_multi(sc);
1586 VTNET_CORE_UNLOCK(sc);
1587 break;
1588
1589 case SIOCSIFMEDIA:
1590 case SIOCGIFMEDIA:
1591 error = ifmedia_ioctl(ifp, ifr, &sc->vtnet_media, cmd);
1592 break;
1593
1594 case SIOCSIFCAP:
1595 VTNET_CORE_LOCK(sc);
1596 error = vtnet_ioctl_ifcap(sc, ifr);
1597 VTNET_CORE_UNLOCK(sc);
1598 VLAN_CAPABILITIES(ifp);
1599 break;
1600
1601 default:
1602 error = ether_ioctl(ifp, cmd, data);
1603 break;
1604 }
1605
1606 VTNET_CORE_LOCK_ASSERT_NOTOWNED(sc);
1607
1608 return (error);
1609 }
1610
1611 static int
vtnet_rxq_populate(struct vtnet_rxq * rxq)1612 vtnet_rxq_populate(struct vtnet_rxq *rxq)
1613 {
1614 struct virtqueue *vq;
1615 int nbufs, error;
1616
1617 #ifdef DEV_NETMAP
1618 error = vtnet_netmap_rxq_populate(rxq);
1619 if (error >= 0)
1620 return (error);
1621 #endif /* DEV_NETMAP */
1622
1623 vq = rxq->vtnrx_vq;
1624 error = ENOSPC;
1625
1626 for (nbufs = 0; !virtqueue_full(vq); nbufs++) {
1627 error = vtnet_rxq_new_buf(rxq);
1628 if (error)
1629 break;
1630 }
1631
1632 if (nbufs > 0) {
1633 virtqueue_notify(vq);
1634 /*
1635 * EMSGSIZE signifies the virtqueue did not have enough
1636 * entries available to hold the last mbuf. This is not
1637 * an error.
1638 */
1639 if (error == EMSGSIZE)
1640 error = 0;
1641 }
1642
1643 return (error);
1644 }
1645
1646 static void
vtnet_rxq_free_mbufs(struct vtnet_rxq * rxq)1647 vtnet_rxq_free_mbufs(struct vtnet_rxq *rxq)
1648 {
1649 struct virtqueue *vq;
1650 struct mbuf *m;
1651 int last;
1652 #ifdef DEV_NETMAP
1653 struct netmap_kring *kring = netmap_kring_on(NA(rxq->vtnrx_sc->vtnet_ifp),
1654 rxq->vtnrx_id, NR_RX);
1655 #else /* !DEV_NETMAP */
1656 void *kring = NULL;
1657 #endif /* !DEV_NETMAP */
1658
1659 vq = rxq->vtnrx_vq;
1660 last = 0;
1661
1662 while ((m = virtqueue_drain(vq, &last)) != NULL) {
1663 if (kring == NULL)
1664 m_freem(m);
1665 }
1666
1667 KASSERT(virtqueue_empty(vq),
1668 ("%s: mbufs remaining in rx queue %p", __func__, rxq));
1669 }
1670
1671 static struct mbuf *
vtnet_rx_alloc_buf(struct vtnet_softc * sc,int nbufs,struct mbuf ** m_tailp)1672 vtnet_rx_alloc_buf(struct vtnet_softc *sc, int nbufs, struct mbuf **m_tailp)
1673 {
1674 struct mbuf *m_head, *m_tail, *m;
1675 struct vtnet_rx_buffer_header *vthdr;
1676 bus_dma_segment_t segs[1];
1677 bus_dmamap_t dmap;
1678 int nsegs;
1679 int err;
1680 int i, size;
1681
1682 m_head = NULL;
1683 size = sc->vtnet_rx_clustersz;
1684
1685 KASSERT(nbufs == 1 || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1686 ("%s: mbuf %d chain requested without LRO_NOMRG", __func__, nbufs));
1687
1688 for (i = 0; i < nbufs; i++) {
1689 m = m_getjcl(M_NOWAIT, MT_DATA, i == 0 ? M_PKTHDR : 0, size);
1690 if (m == NULL) {
1691 sc->vtnet_stats.mbuf_alloc_failed++;
1692 m_freem(m_head);
1693 return (NULL);
1694 }
1695
1696 m->m_len = size;
1697 vthdr = (struct vtnet_rx_buffer_header *)m->m_data;
1698
1699 /* Reserve space for header */
1700 m_adj(m, VTNET_RX_BUFFER_HEADER_OFFSET);
1701
1702 /*
1703 * Need to offset the mbuf if the header we're going to add
1704 * will misalign.
1705 */
1706 if (VTNET_ETHER_ALIGN != 0 && sc->vtnet_hdr_size % 4 == 0)
1707 m_adj(m, VTNET_ETHER_ALIGN);
1708
1709 err = bus_dmamap_create(sc->vtnet_rx_dmat, 0, &dmap);
1710 if (err) {
1711 printf("Failed to create dmamap, err :%d\n",
1712 err);
1713 m_freem(m);
1714 return (NULL);
1715 }
1716
1717 nsegs = 0;
1718 err = bus_dmamap_load_mbuf_sg(sc->vtnet_rx_dmat, dmap, m, segs,
1719 &nsegs, BUS_DMA_NOWAIT);
1720 if (err != 0) {
1721 printf("Failed to map mbuf into DMA visible memory, err: %d\n",
1722 err);
1723 m_freem(m);
1724 bus_dmamap_destroy(sc->vtnet_rx_dmat, dmap);
1725 return (NULL);
1726 }
1727 KASSERT(nsegs == 1,
1728 ("%s: unexpected number of DMA segments for rx buffer: %d",
1729 __func__, nsegs));
1730
1731 vthdr->addr = segs[0].ds_addr;
1732 vthdr->dmap = dmap;
1733
1734 if (m_head != NULL) {
1735 m_tail->m_next = m;
1736 m_tail = m;
1737 } else
1738 m_head = m_tail = m;
1739 }
1740
1741 if (m_tailp != NULL)
1742 *m_tailp = m_tail;
1743
1744 return (m_head);
1745 }
1746
1747 /*
1748 * Slow path for when LRO without mergeable buffers is negotiated.
1749 */
1750 static int
vtnet_rxq_replace_lro_nomrg_buf(struct vtnet_rxq * rxq,struct mbuf * m0,int len0)1751 vtnet_rxq_replace_lro_nomrg_buf(struct vtnet_rxq *rxq, struct mbuf *m0,
1752 int len0)
1753 {
1754 struct vtnet_softc *sc;
1755 struct mbuf *m, *m_prev, *m_new, *m_tail;
1756 int len, clustersz, nreplace, error;
1757
1758 sc = rxq->vtnrx_sc;
1759 clustersz = sc->vtnet_rx_clustersz - VTNET_RX_BUFFER_HEADER_OFFSET;
1760 /*
1761 * Need to offset the mbuf if the header we're going to add will
1762 * misalign, account for that here.
1763 */
1764 if (VTNET_ETHER_ALIGN != 0 && sc->vtnet_hdr_size % 4 == 0)
1765 clustersz -= VTNET_ETHER_ALIGN;
1766
1767 m_prev = NULL;
1768 m_tail = NULL;
1769 nreplace = 0;
1770
1771 m = m0;
1772 len = len0;
1773
1774 /*
1775 * Since these mbuf chains are so large, avoid allocating a complete
1776 * replacement when the received frame did not consume the entire
1777 * chain. Unused mbufs are moved to the tail of the replacement mbuf.
1778 */
1779 while (len > 0) {
1780 if (m == NULL) {
1781 sc->vtnet_stats.rx_frame_too_large++;
1782 return (EMSGSIZE);
1783 }
1784
1785 /*
1786 * Every mbuf should have the expected cluster size since that
1787 * is also used to allocate the replacements.
1788 */
1789 KASSERT(m->m_len == clustersz,
1790 ("%s: mbuf size %d not expected cluster size %d", __func__,
1791 m->m_len, clustersz));
1792
1793 m->m_len = MIN(m->m_len, len);
1794 len -= m->m_len;
1795
1796 m_prev = m;
1797 m = m->m_next;
1798 nreplace++;
1799 }
1800
1801 KASSERT(nreplace > 0 && nreplace <= sc->vtnet_rx_nmbufs,
1802 ("%s: invalid replacement mbuf count %d max %d", __func__,
1803 nreplace, sc->vtnet_rx_nmbufs));
1804
1805 m_new = vtnet_rx_alloc_buf(sc, nreplace, &m_tail);
1806 if (m_new == NULL) {
1807 m_prev->m_len = clustersz;
1808 return (ENOBUFS);
1809 }
1810
1811 /*
1812 * Move any unused mbufs from the received mbuf chain onto the
1813 * end of the replacement chain.
1814 */
1815 if (m_prev->m_next != NULL) {
1816 m_tail->m_next = m_prev->m_next;
1817 m_prev->m_next = NULL;
1818 }
1819
1820 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1821 if (error) {
1822 /*
1823 * The replacement is suppose to be an copy of the one
1824 * dequeued so this is a very unexpected error.
1825 *
1826 * Restore the m0 chain to the original state if it was
1827 * modified so we can then discard it.
1828 */
1829 if (m_tail->m_next != NULL) {
1830 m_prev->m_next = m_tail->m_next;
1831 m_tail->m_next = NULL;
1832 }
1833 m_prev->m_len = clustersz;
1834 sc->vtnet_stats.rx_enq_replacement_failed++;
1835 m_freem(m_new);
1836 }
1837
1838 return (error);
1839 }
1840
1841 static int
vtnet_rxq_replace_buf(struct vtnet_rxq * rxq,struct mbuf * m,int len)1842 vtnet_rxq_replace_buf(struct vtnet_rxq *rxq, struct mbuf *m, int len)
1843 {
1844 struct vtnet_softc *sc;
1845 struct mbuf *m_new;
1846 int error;
1847
1848 sc = rxq->vtnrx_sc;
1849
1850 if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG)
1851 return (vtnet_rxq_replace_lro_nomrg_buf(rxq, m, len));
1852
1853 MPASS(m->m_next == NULL);
1854 if (m->m_len < len)
1855 return (EMSGSIZE);
1856
1857 m_new = vtnet_rx_alloc_buf(sc, 1, NULL);
1858 if (m_new == NULL)
1859 return (ENOBUFS);
1860
1861 error = vtnet_rxq_enqueue_buf(rxq, m_new);
1862 if (error) {
1863 sc->vtnet_stats.rx_enq_replacement_failed++;
1864 m_freem(m_new);
1865 } else
1866 m->m_len = len;
1867
1868 return (error);
1869 }
1870
1871 static int
vtnet_rxq_enqueue_buf(struct vtnet_rxq * rxq,struct mbuf * m)1872 vtnet_rxq_enqueue_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1873 {
1874 struct vtnet_rx_buffer_header *hdr;
1875 struct vtnet_softc *sc;
1876 struct sglist *sg;
1877 int header_inlined, error;
1878 bus_addr_t paddr;
1879 struct mbuf *mp;
1880
1881 sc = rxq->vtnrx_sc;
1882 sg = rxq->vtnrx_sg;
1883
1884 KASSERT(m->m_next == NULL || sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG,
1885 ("%s: mbuf chain without LRO_NOMRG", __func__));
1886 VTNET_RXQ_LOCK_ASSERT(rxq);
1887
1888 sglist_reset(sg);
1889 header_inlined = vtnet_modern(sc) ||
1890 (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) != 0; /* TODO: ANY_LAYOUT */
1891
1892 hdr = vtnet_mbuf_to_rx_buffer_header(sc, m);
1893 paddr = hdr->addr;
1894
1895 /*
1896 * Note: The mbuf has been already adjusted when we allocate it if we
1897 * have to do strict alignment.
1898 */
1899 if (header_inlined) {
1900 error = sglist_append_phys(sg, paddr, m->m_len);
1901 } else {
1902 MPASS(sc->vtnet_hdr_size == sizeof(struct virtio_net_hdr));
1903
1904 /* Append the header and remaining mbuf data. */
1905 error = sglist_append_phys(sg, paddr, sc->vtnet_hdr_size);
1906 if (error)
1907 return (error);
1908 error = sglist_append_phys(sg,
1909 paddr + sizeof(struct vtnet_rx_header),
1910 m->m_len - sizeof(struct vtnet_rx_header));
1911 if (error)
1912 return (error);
1913
1914 mp = m->m_next;
1915 while (mp) {
1916 hdr = vtnet_mbuf_to_rx_buffer_header(sc, mp);
1917 paddr = hdr->addr;
1918 error = sglist_append_phys(sg, paddr, mp->m_len);
1919 if (error)
1920 return (error);
1921
1922 mp = mp->m_next;
1923 }
1924 }
1925
1926 if (error)
1927 return (error);
1928
1929 return (virtqueue_enqueue(rxq->vtnrx_vq, m, sg, 0, sg->sg_nseg));
1930 }
1931
1932 static int
vtnet_rxq_new_buf(struct vtnet_rxq * rxq)1933 vtnet_rxq_new_buf(struct vtnet_rxq *rxq)
1934 {
1935 struct vtnet_softc *sc;
1936 struct mbuf *m;
1937 int error;
1938
1939 sc = rxq->vtnrx_sc;
1940
1941 m = vtnet_rx_alloc_buf(sc, sc->vtnet_rx_nmbufs, NULL);
1942 if (m == NULL)
1943 return (ENOBUFS);
1944
1945 error = vtnet_rxq_enqueue_buf(rxq, m);
1946 if (error)
1947 m_freem(m);
1948
1949 return (error);
1950 }
1951
1952 static void
vtnet_rxq_discard_merged_bufs(struct vtnet_rxq * rxq,int nbufs)1953 vtnet_rxq_discard_merged_bufs(struct vtnet_rxq *rxq, int nbufs)
1954 {
1955 struct mbuf *m;
1956
1957 while (--nbufs > 0) {
1958 m = virtqueue_dequeue(rxq->vtnrx_vq, NULL);
1959 if (m == NULL)
1960 break;
1961 vtnet_rxq_discard_buf(rxq, m);
1962 }
1963 }
1964
1965 static void
vtnet_rxq_discard_buf(struct vtnet_rxq * rxq,struct mbuf * m)1966 vtnet_rxq_discard_buf(struct vtnet_rxq *rxq, struct mbuf *m)
1967 {
1968 int error __diagused;
1969
1970 /*
1971 * Requeue the discarded mbuf. This should always be successful
1972 * since it was just dequeued.
1973 */
1974 error = vtnet_rxq_enqueue_buf(rxq, m);
1975 KASSERT(error == 0,
1976 ("%s: cannot requeue discarded mbuf %d", __func__, error));
1977 }
1978
1979 static int
vtnet_rxq_merged_eof(struct vtnet_rxq * rxq,struct mbuf * m_head,int nbufs)1980 vtnet_rxq_merged_eof(struct vtnet_rxq *rxq, struct mbuf *m_head, int nbufs)
1981 {
1982 struct vtnet_softc *sc;
1983 struct virtqueue *vq;
1984 struct mbuf *m_tail;
1985
1986 sc = rxq->vtnrx_sc;
1987 vq = rxq->vtnrx_vq;
1988 m_tail = m_head;
1989
1990 while (--nbufs > 0) {
1991 struct vtnet_rx_buffer_header *vthdr;
1992 struct mbuf *m;
1993 uint32_t len;
1994
1995 m = virtqueue_dequeue(vq, &len);
1996 if (m == NULL) {
1997 rxq->vtnrx_stats.vrxs_ierrors++;
1998 goto fail;
1999 }
2000
2001 vthdr = vtnet_mbuf_to_rx_buffer_header(sc, m);
2002 bus_dmamap_sync(sc->vtnet_rx_dmat, vthdr->dmap,
2003 BUS_DMASYNC_POSTREAD);
2004
2005 if (vtnet_rxq_new_buf(rxq) != 0) {
2006 rxq->vtnrx_stats.vrxs_iqdrops++;
2007 vtnet_rxq_discard_buf(rxq, m);
2008 if (nbufs > 1)
2009 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
2010 goto fail;
2011 }
2012
2013 bus_dmamap_unload(sc->vtnet_rx_dmat, vthdr->dmap);
2014 bus_dmamap_destroy(sc->vtnet_rx_dmat, vthdr->dmap);
2015
2016 if (m->m_len < len)
2017 len = m->m_len;
2018
2019 m->m_len = len;
2020 m->m_flags &= ~M_PKTHDR;
2021
2022 m_head->m_pkthdr.len += len;
2023 m_tail->m_next = m;
2024 m_tail = m;
2025 }
2026
2027 return (0);
2028
2029 fail:
2030 sc->vtnet_stats.rx_mergeable_failed++;
2031 m_freem(m_head);
2032
2033 return (1);
2034 }
2035
2036 #if defined(INET) || defined(INET6)
2037 static int
vtnet_lro_rx(struct vtnet_rxq * rxq,struct mbuf * m)2038 vtnet_lro_rx(struct vtnet_rxq *rxq, struct mbuf *m)
2039 {
2040 struct lro_ctrl *lro;
2041
2042 lro = &rxq->vtnrx_lro;
2043
2044 if (lro->lro_mbuf_max != 0) {
2045 tcp_lro_queue_mbuf(lro, m);
2046 return (0);
2047 }
2048
2049 return (tcp_lro_rx(lro, m, 0));
2050 }
2051 #endif
2052
2053 static void
vtnet_rxq_input(struct vtnet_rxq * rxq,struct mbuf * m,struct virtio_net_hdr * hdr)2054 vtnet_rxq_input(struct vtnet_rxq *rxq, struct mbuf *m,
2055 struct virtio_net_hdr *hdr)
2056 {
2057 struct vtnet_softc *sc;
2058 if_t ifp;
2059
2060 sc = rxq->vtnrx_sc;
2061 ifp = sc->vtnet_ifp;
2062
2063 if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) {
2064 struct ether_header *eh = mtod(m, struct ether_header *);
2065 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
2066 vtnet_vlan_tag_remove(m);
2067 /*
2068 * With the 802.1Q header removed, update the
2069 * checksum starting location accordingly.
2070 */
2071 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
2072 hdr->csum_start -= ETHER_VLAN_ENCAP_LEN;
2073 }
2074 }
2075
2076 if (sc->vtnet_act_vq_pairs == 1) {
2077 /*
2078 * When RSS is not needed (one active rx queue), let the upper
2079 * layer know and react.
2080 */
2081 M_HASHTYPE_CLEAR(m);
2082 } else {
2083 m->m_pkthdr.flowid = rxq->vtnrx_id;
2084 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
2085 }
2086
2087 if (hdr->flags &
2088 (VIRTIO_NET_HDR_F_NEEDS_CSUM | VIRTIO_NET_HDR_F_DATA_VALID)) {
2089 #if defined(INET) || defined(INET6)
2090 int ret;
2091
2092 /*
2093 * Translate the VirtIO header flags to the corresponding
2094 * CSUM_* flags in the mbuf.
2095 */
2096 ret = virtio_net_rx_csum(m, hdr);
2097 if (ret == 0)
2098 rxq->vtnrx_stats.vrxs_csum++;
2099 else {
2100 switch (ret) {
2101 case VIRTIO_NET_RX_CSUM_INACCESSIBLE_IPPROTO:
2102 sc->vtnet_stats.rx_csum_inaccessible_ipproto++;
2103 break;
2104 case VIRTIO_NET_RX_CSUM_BAD_ETHTYPE:
2105 sc->vtnet_stats.rx_csum_bad_ethtype++;
2106 break;
2107 case VIRTIO_NET_RX_CSUM_BAD_IPPROTO:
2108 sc->vtnet_stats.rx_csum_bad_ipproto++;
2109 break;
2110 }
2111 rxq->vtnrx_stats.vrxs_csum_failed++;
2112 }
2113 #else
2114 sc->vtnet_stats.rx_csum_bad_ethtype++;
2115 rxq->vtnrx_stats.vrxs_csum_failed++;
2116 #endif
2117 }
2118
2119 if (hdr->gso_size != 0) {
2120 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2121 case VIRTIO_NET_HDR_GSO_TCPV4:
2122 case VIRTIO_NET_HDR_GSO_TCPV6:
2123 m->m_pkthdr.lro_nsegs =
2124 howmany(m->m_pkthdr.len, hdr->gso_size);
2125 rxq->vtnrx_stats.vrxs_host_lro++;
2126 break;
2127 }
2128 }
2129
2130 rxq->vtnrx_stats.vrxs_ipackets++;
2131 rxq->vtnrx_stats.vrxs_ibytes += m->m_pkthdr.len;
2132
2133 #if defined(INET) || defined(INET6)
2134 if (vtnet_software_lro(sc) && if_getcapenable(ifp) & IFCAP_LRO) {
2135 if (vtnet_lro_rx(rxq, m) == 0)
2136 return;
2137 }
2138 #endif
2139
2140 if_input(ifp, m);
2141 }
2142
2143 static int
vtnet_rxq_eof(struct vtnet_rxq * rxq)2144 vtnet_rxq_eof(struct vtnet_rxq *rxq)
2145 {
2146 struct virtio_net_hdr lhdr, *hdr;
2147 struct vtnet_rx_buffer_header *vthdr;
2148 struct vtnet_softc *sc;
2149 if_t ifp;
2150 struct virtqueue *vq;
2151 int deq, count;
2152
2153 sc = rxq->vtnrx_sc;
2154 vq = rxq->vtnrx_vq;
2155 ifp = sc->vtnet_ifp;
2156 deq = 0;
2157 count = sc->vtnet_rx_process_limit;
2158
2159 VTNET_RXQ_LOCK_ASSERT(rxq);
2160
2161 CURVNET_SET(if_getvnet(ifp));
2162 while (count-- > 0) {
2163 struct mbuf *m, *mp;
2164 uint32_t len, nbufs, adjsz;
2165 uint32_t synced;
2166
2167 m = virtqueue_dequeue(vq, &len);
2168 if (m == NULL)
2169 break;
2170 deq++;
2171
2172 mp = m;
2173
2174 /*
2175 * Sync all mbufs in this packet. There will only be a single
2176 * mbuf unless LRO is in use.
2177 */
2178 synced = 0;
2179 while (mp && synced < len) {
2180 vthdr = vtnet_mbuf_to_rx_buffer_header(sc, mp);
2181 bus_dmamap_sync(sc->vtnet_rx_dmat, vthdr->dmap,
2182 BUS_DMASYNC_POSTREAD);
2183
2184 synced += mp->m_len;
2185 mp = mp->m_next;
2186 }
2187
2188 if (len < sc->vtnet_hdr_size + ETHER_HDR_LEN) {
2189 rxq->vtnrx_stats.vrxs_ierrors++;
2190 vtnet_rxq_discard_buf(rxq, m);
2191 continue;
2192 }
2193
2194 if (sc->vtnet_flags & VTNET_FLAG_MRG_RXBUFS) {
2195 struct virtio_net_hdr_mrg_rxbuf *mhdr =
2196 mtod(m, struct virtio_net_hdr_mrg_rxbuf *);
2197 kmsan_mark(mhdr, sizeof(*mhdr), KMSAN_STATE_INITED);
2198 nbufs = vtnet_htog16(sc, mhdr->num_buffers);
2199 adjsz = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2200 } else if (vtnet_modern(sc)) {
2201 nbufs = 1; /* num_buffers is always 1 */
2202 adjsz = sizeof(struct virtio_net_hdr_v1);
2203 } else {
2204 nbufs = 1;
2205 adjsz = sizeof(struct vtnet_rx_header);
2206 /*
2207 * Account for our gap between the header and start of
2208 * data to keep the segments separated.
2209 */
2210 len += VTNET_RX_HEADER_PAD;
2211 }
2212
2213 if (vtnet_rxq_replace_buf(rxq, m, len) != 0) {
2214 rxq->vtnrx_stats.vrxs_iqdrops++;
2215 vtnet_rxq_discard_buf(rxq, m);
2216 if (nbufs > 1)
2217 vtnet_rxq_discard_merged_bufs(rxq, nbufs);
2218 continue;
2219 }
2220
2221 mp = m;
2222 synced = 0;
2223 while (mp && synced < len) {
2224 vthdr = vtnet_mbuf_to_rx_buffer_header(sc, mp);
2225
2226 bus_dmamap_unload(sc->vtnet_rx_dmat, vthdr->dmap);
2227 bus_dmamap_destroy(sc->vtnet_rx_dmat, vthdr->dmap);
2228
2229 synced += mp->m_len;
2230 mp = mp->m_next;
2231 }
2232
2233 m->m_pkthdr.len = len;
2234 m->m_pkthdr.rcvif = ifp;
2235 m->m_pkthdr.csum_flags = 0;
2236
2237 if (nbufs > 1) {
2238 /* Dequeue the rest of chain. */
2239 if (vtnet_rxq_merged_eof(rxq, m, nbufs) != 0)
2240 continue;
2241 }
2242
2243 kmsan_mark_mbuf(m, KMSAN_STATE_INITED);
2244
2245 /*
2246 * Save an endian swapped version of the header prior to it
2247 * being stripped. The header is always at the start of the
2248 * mbuf data. num_buffers was already saved (and not needed)
2249 * so use the standard header.
2250 */
2251 hdr = mtod(m, struct virtio_net_hdr *);
2252 lhdr.flags = hdr->flags;
2253 lhdr.gso_type = hdr->gso_type;
2254 lhdr.hdr_len = vtnet_htog16(sc, hdr->hdr_len);
2255 lhdr.gso_size = vtnet_htog16(sc, hdr->gso_size);
2256 lhdr.csum_start = vtnet_htog16(sc, hdr->csum_start);
2257 lhdr.csum_offset = vtnet_htog16(sc, hdr->csum_offset);
2258 m_adj(m, adjsz);
2259
2260 if (PFIL_HOOKED_IN(sc->vtnet_pfil)) {
2261 pfil_return_t pfil;
2262
2263 pfil = pfil_mbuf_in(sc->vtnet_pfil, &m, ifp, NULL);
2264 switch (pfil) {
2265 case PFIL_DROPPED:
2266 case PFIL_CONSUMED:
2267 continue;
2268 default:
2269 KASSERT(pfil == PFIL_PASS,
2270 ("Filter returned %d!", pfil));
2271 }
2272 }
2273
2274 vtnet_rxq_input(rxq, m, &lhdr);
2275 }
2276
2277 if (deq > 0) {
2278 #if defined(INET) || defined(INET6)
2279 if (vtnet_software_lro(sc))
2280 tcp_lro_flush_all(&rxq->vtnrx_lro);
2281 #endif
2282 virtqueue_notify(vq);
2283 }
2284 CURVNET_RESTORE();
2285
2286 return (count > 0 ? 0 : EAGAIN);
2287 }
2288
2289 static void
vtnet_rx_vq_process(struct vtnet_rxq * rxq,int tries)2290 vtnet_rx_vq_process(struct vtnet_rxq *rxq, int tries)
2291 {
2292 struct vtnet_softc *sc;
2293 if_t ifp;
2294 u_int more;
2295 #ifdef DEV_NETMAP
2296 int nmirq;
2297 #endif /* DEV_NETMAP */
2298
2299 sc = rxq->vtnrx_sc;
2300 ifp = sc->vtnet_ifp;
2301
2302 if (__predict_false(rxq->vtnrx_id >= sc->vtnet_act_vq_pairs)) {
2303 /*
2304 * Ignore this interrupt. Either this is a spurious interrupt
2305 * or multiqueue without per-VQ MSIX so every queue needs to
2306 * be polled (a brain dead configuration we could try harder
2307 * to avoid).
2308 */
2309 vtnet_rxq_disable_intr(rxq);
2310 return;
2311 }
2312
2313 VTNET_RXQ_LOCK(rxq);
2314
2315 #ifdef DEV_NETMAP
2316 /*
2317 * We call netmap_rx_irq() under lock to prevent concurrent calls.
2318 * This is not necessary to serialize the access to the RX vq, but
2319 * rather to avoid races that may happen if this interface is
2320 * attached to a VALE switch, which would cause received packets
2321 * to stall in the RX queue (nm_kr_tryget() could find the kring
2322 * busy when called from netmap_bwrap_intr_notify()).
2323 */
2324 nmirq = netmap_rx_irq(ifp, rxq->vtnrx_id, &more);
2325 if (nmirq != NM_IRQ_PASS) {
2326 VTNET_RXQ_UNLOCK(rxq);
2327 if (nmirq == NM_IRQ_RESCHED) {
2328 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2329 }
2330 return;
2331 }
2332 #endif /* DEV_NETMAP */
2333
2334 again:
2335 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2336 VTNET_RXQ_UNLOCK(rxq);
2337 return;
2338 }
2339
2340 more = vtnet_rxq_eof(rxq);
2341 if (more || vtnet_rxq_enable_intr(rxq) != 0) {
2342 if (!more)
2343 vtnet_rxq_disable_intr(rxq);
2344 /*
2345 * This is an occasional condition or race (when !more),
2346 * so retry a few times before scheduling the taskqueue.
2347 */
2348 if (tries-- > 0)
2349 goto again;
2350
2351 rxq->vtnrx_stats.vrxs_rescheduled++;
2352 VTNET_RXQ_UNLOCK(rxq);
2353 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
2354 } else
2355 VTNET_RXQ_UNLOCK(rxq);
2356 }
2357
2358 static void
vtnet_rx_vq_intr(void * xrxq)2359 vtnet_rx_vq_intr(void *xrxq)
2360 {
2361 struct vtnet_rxq *rxq;
2362
2363 rxq = xrxq;
2364 vtnet_rx_vq_process(rxq, VTNET_INTR_DISABLE_RETRIES);
2365 }
2366
2367 static void
vtnet_rxq_tq_intr(void * xrxq,int pending __unused)2368 vtnet_rxq_tq_intr(void *xrxq, int pending __unused)
2369 {
2370 struct vtnet_rxq *rxq;
2371
2372 rxq = xrxq;
2373 vtnet_rx_vq_process(rxq, 0);
2374 }
2375
2376 static int
vtnet_txq_intr_threshold(struct vtnet_txq * txq)2377 vtnet_txq_intr_threshold(struct vtnet_txq *txq)
2378 {
2379 struct vtnet_softc *sc;
2380 int threshold;
2381
2382 sc = txq->vtntx_sc;
2383
2384 /*
2385 * The Tx interrupt is disabled until the queue free count falls
2386 * below our threshold. Completed frames are drained from the Tx
2387 * virtqueue before transmitting new frames and in the watchdog
2388 * callout, so the frequency of Tx interrupts is greatly reduced,
2389 * at the cost of not freeing mbufs as quickly as they otherwise
2390 * would be.
2391 */
2392 threshold = virtqueue_size(txq->vtntx_vq) / 4;
2393
2394 /*
2395 * Without indirect descriptors, leave enough room for the most
2396 * segments we handle.
2397 */
2398 if ((sc->vtnet_flags & VTNET_FLAG_INDIRECT) == 0 &&
2399 threshold < sc->vtnet_tx_nsegs)
2400 threshold = sc->vtnet_tx_nsegs;
2401
2402 return (threshold);
2403 }
2404
2405 static int
vtnet_txq_below_threshold(struct vtnet_txq * txq)2406 vtnet_txq_below_threshold(struct vtnet_txq *txq)
2407 {
2408 struct virtqueue *vq;
2409
2410 vq = txq->vtntx_vq;
2411
2412 return (virtqueue_nfree(vq) <= txq->vtntx_intr_threshold);
2413 }
2414
2415 static int
vtnet_txq_notify(struct vtnet_txq * txq)2416 vtnet_txq_notify(struct vtnet_txq *txq)
2417 {
2418 struct virtqueue *vq;
2419
2420 vq = txq->vtntx_vq;
2421
2422 txq->vtntx_watchdog = VTNET_TX_TIMEOUT;
2423 virtqueue_notify(vq);
2424
2425 if (vtnet_txq_enable_intr(txq) == 0)
2426 return (0);
2427
2428 /*
2429 * Drain frames that were completed since last checked. If this
2430 * causes the queue to go above the threshold, the caller should
2431 * continue transmitting.
2432 */
2433 if (vtnet_txq_eof(txq) != 0 && vtnet_txq_below_threshold(txq) == 0) {
2434 virtqueue_disable_intr(vq);
2435 return (1);
2436 }
2437
2438 return (0);
2439 }
2440
2441 static void
vtnet_txq_free_mbufs(struct vtnet_txq * txq)2442 vtnet_txq_free_mbufs(struct vtnet_txq *txq)
2443 {
2444 struct virtqueue *vq;
2445 struct vtnet_tx_header *txhdr;
2446 int last;
2447 #ifdef DEV_NETMAP
2448 struct netmap_kring *kring = netmap_kring_on(NA(txq->vtntx_sc->vtnet_ifp),
2449 txq->vtntx_id, NR_TX);
2450 #else /* !DEV_NETMAP */
2451 void *kring = NULL;
2452 #endif /* !DEV_NETMAP */
2453
2454 vq = txq->vtntx_vq;
2455 last = 0;
2456
2457 while ((txhdr = virtqueue_drain(vq, &last)) != NULL) {
2458 if (kring == NULL) {
2459 bus_dmamap_unload(txq->vtntx_sc->vtnet_tx_dmat,
2460 txhdr->dmap);
2461 bus_dmamap_destroy(txq->vtntx_sc->vtnet_tx_dmat,
2462 txhdr->dmap);
2463 bus_dmamap_unload(txq->vtntx_sc->vtnet_tx_dmat,
2464 txhdr->hdr_dmap);
2465 bus_dmamap_destroy(txq->vtntx_sc->vtnet_tx_dmat,
2466 txhdr->hdr_dmap);
2467 m_freem(txhdr->vth_mbuf);
2468 uma_zfree(vtnet_tx_header_zone, txhdr);
2469 }
2470 }
2471
2472 KASSERT(virtqueue_empty(vq),
2473 ("%s: mbufs remaining in tx queue %p", __func__, txq));
2474 }
2475
2476 static void
vtnet_txq_enqueue_callback(void * arg,bus_dma_segment_t * segs,int nsegs,int error)2477 vtnet_txq_enqueue_callback(void *arg, bus_dma_segment_t *segs,
2478 int nsegs, int error)
2479 {
2480 vm_paddr_t *hdr_paddr;
2481
2482 if (error != 0)
2483 return;
2484
2485 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2486
2487 hdr_paddr = (vm_paddr_t *)arg;
2488 *hdr_paddr = segs[0].ds_addr;
2489 }
2490
2491 static int
vtnet_txq_enqueue_buf(struct vtnet_txq * txq,struct mbuf ** m_head,struct vtnet_tx_header * txhdr)2492 vtnet_txq_enqueue_buf(struct vtnet_txq *txq, struct mbuf **m_head,
2493 struct vtnet_tx_header *txhdr)
2494 {
2495 bus_dma_segment_t segs[VTNET_TX_SEGS_MAX];
2496 int nsegs;
2497 struct vtnet_softc *sc;
2498 struct virtqueue *vq;
2499 struct sglist *sg;
2500 struct mbuf *m;
2501 int error;
2502 vm_paddr_t hdr_paddr;
2503 bus_dmamap_t hdr_dmap;
2504 bus_dmamap_t dmap;
2505 int i;
2506
2507 sc = txq->vtntx_sc;
2508 vq = txq->vtntx_vq;
2509 sg = txq->vtntx_sg;
2510 m = *m_head;
2511
2512 sglist_reset(sg);
2513
2514 error = bus_dmamap_create(sc->vtnet_tx_dmat, 0, &hdr_dmap);
2515 if (error)
2516 goto fail;
2517
2518 error = bus_dmamap_load(sc->vtnet_tx_dmat, hdr_dmap, &txhdr->vth_uhdr,
2519 sc->vtnet_hdr_size, vtnet_txq_enqueue_callback, &hdr_paddr,
2520 BUS_DMA_NOWAIT);
2521 if (error)
2522 goto fail_hdr_dmamap_destroy;
2523
2524 error = sglist_append_phys(sg, hdr_paddr, sc->vtnet_hdr_size);
2525 if (error != 0 || sg->sg_nseg != 1) {
2526 KASSERT(0, ("%s: cannot add header to sglist error %d nseg %d",
2527 __func__, error, sg->sg_nseg));
2528 goto fail_hdr_dmamap_unload;
2529 }
2530
2531 bus_dmamap_sync(sc->vtnet_tx_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
2532
2533 error = bus_dmamap_create(sc->vtnet_tx_dmat, 0, &dmap);
2534 if (error)
2535 goto fail_hdr_dmamap_unload;
2536
2537 nsegs = 0;
2538 error = bus_dmamap_load_mbuf_sg(sc->vtnet_tx_dmat, dmap, m, segs,
2539 &nsegs, BUS_DMA_NOWAIT);
2540 if (error != 0)
2541 goto fail_dmamap_destroy;
2542 KASSERT(nsegs <= sc->vtnet_tx_nsegs,
2543 ("%s: unexpected number of DMA segments for tx buffer: %d (max %d)",
2544 __func__, nsegs, sc->vtnet_tx_nsegs));
2545
2546 bus_dmamap_sync(sc->vtnet_tx_dmat, dmap, BUS_DMASYNC_PREWRITE);
2547
2548 for (i = 0; i < nsegs && !error; i++)
2549 error = sglist_append_phys(sg, segs[i].ds_addr, segs[i].ds_len);
2550
2551 if (error) {
2552 sglist_reset(sg);
2553 bus_dmamap_unload(sc->vtnet_tx_dmat, dmap);
2554
2555 error = sglist_append_phys(sg, hdr_paddr, sc->vtnet_hdr_size);
2556 if (error != 0 || sg->sg_nseg != 1) {
2557 KASSERT(0, ("%s: cannot add header to sglist error %d nseg %d",
2558 __func__, error, sg->sg_nseg));
2559 goto fail_dmamap_destroy;
2560 }
2561
2562 m = m_defrag(m, M_NOWAIT);
2563 if (m == NULL) {
2564 sc->vtnet_stats.tx_defrag_failed++;
2565 goto fail;
2566 }
2567
2568 *m_head = m;
2569 sc->vtnet_stats.tx_defragged++;
2570
2571 nsegs = 0;
2572 error = bus_dmamap_load_mbuf_sg(sc->vtnet_tx_dmat, dmap, m,
2573 segs, &nsegs, BUS_DMA_NOWAIT);
2574 if (error != 0)
2575 goto fail_dmamap_destroy;
2576 KASSERT(nsegs <= sc->vtnet_tx_nsegs,
2577 ("%s: unexpected number of DMA segments for tx buffer: %d (max %d)",
2578 __func__, nsegs, sc->vtnet_tx_nsegs));
2579
2580 bus_dmamap_sync(sc->vtnet_tx_dmat, dmap, BUS_DMASYNC_PREWRITE);
2581
2582 for (i = 0; i < nsegs && !error; i++)
2583 error = sglist_append_phys(sg, segs[i].ds_addr,
2584 segs[i].ds_len);
2585
2586 if (error)
2587 goto fail_dmamap_unload;
2588 }
2589
2590 txhdr->vth_mbuf = m;
2591 txhdr->dmap = dmap;
2592 txhdr->hdr_dmap = hdr_dmap;
2593
2594 error = virtqueue_enqueue(vq, txhdr, sg, sg->sg_nseg, 0);
2595
2596 return (error);
2597
2598 fail_dmamap_unload:
2599 bus_dmamap_unload(sc->vtnet_tx_dmat, dmap);
2600 fail_dmamap_destroy:
2601 bus_dmamap_destroy(sc->vtnet_tx_dmat, dmap);
2602 fail_hdr_dmamap_unload:
2603 bus_dmamap_unload(sc->vtnet_tx_dmat, hdr_dmap);
2604 fail_hdr_dmamap_destroy:
2605 bus_dmamap_destroy(sc->vtnet_tx_dmat, hdr_dmap);
2606 fail:
2607 m_freem(*m_head);
2608 *m_head = NULL;
2609
2610 return (ENOBUFS);
2611 }
2612
2613 static int
vtnet_txq_encap(struct vtnet_txq * txq,struct mbuf ** m_head,int flags)2614 vtnet_txq_encap(struct vtnet_txq *txq, struct mbuf **m_head, int flags)
2615 {
2616 struct vtnet_tx_header *txhdr;
2617 struct mbuf *m;
2618 int error;
2619
2620 m = *m_head;
2621 M_ASSERTPKTHDR(m);
2622
2623 txhdr = uma_zalloc(vtnet_tx_header_zone, flags | M_ZERO);
2624 if (txhdr == NULL) {
2625 m_freem(m);
2626 *m_head = NULL;
2627 return (ENOMEM);
2628 }
2629
2630 if (m->m_flags & M_VLANTAG) {
2631 m = ether_vlanencap(m, m->m_pkthdr.ether_vtag);
2632 if ((*m_head = m) == NULL) {
2633 error = ENOBUFS;
2634 goto fail;
2635 }
2636 m->m_flags &= ~M_VLANTAG;
2637 }
2638
2639 if (m->m_pkthdr.csum_flags & VTNET_CSUM_ALL_OFFLOAD) {
2640 #if defined(INET) || defined(INET6)
2641 struct virtio_net_hdr *hdr;
2642 int ret;
2643
2644 /*
2645 * Always use the non-mergeable header, regardless if mergable
2646 * headers were negotiated, because for transmit num_buffers is
2647 * always zero. The vtnet_hdr_size is used to enqueue the right
2648 * header size segment.
2649 */
2650 hdr = &txhdr->vth_uhdr.hdr;
2651
2652 /*
2653 * Translate the CSUM_* flags in the mbuf to the corresponding
2654 * flags in the VirtIO header.
2655 */
2656 ret = virtio_net_tx_offload(txq->vtntx_sc->vtnet_ifp, &m, hdr,
2657 (txq->vtntx_sc->vtnet_flags & VTNET_FLAG_TSO_ECN),
2658 vtnet_modern(txq->vtntx_sc));
2659 switch (ret) {
2660 case VIRTIO_NET_TX_OFFLOAD_UNKNOWN_ETHTYPE:
2661 txq->vtntx_sc->vtnet_stats.tx_csum_unknown_ethtype++;
2662 break;
2663 case VIRTIO_NET_TX_OFFLOAD_PROTO_MISMATCH:
2664 txq->vtntx_sc->vtnet_stats.tx_csum_proto_mismatch++;
2665 break;
2666 case VIRTIO_NET_TX_OFFLOAD_TSO_NOT_TCP:
2667 txq->vtntx_sc->vtnet_stats.tx_tso_not_tcp++;
2668 break;
2669 case VIRTIO_NET_TX_OFFLOAD_TSO_WITHOUT_CSUM:
2670 txq->vtntx_sc->vtnet_stats.tx_tso_without_csum++;
2671 break;
2672 }
2673 if ((*m_head = m) == NULL) {
2674 error = ENOBUFS;
2675 goto fail;
2676 }
2677 if (m->m_pkthdr.csum_flags &
2678 (VTNET_CSUM_OFFLOAD | VTNET_CSUM_OFFLOAD_IPV6))
2679 txq->vtntx_stats.vtxs_csum++;
2680 if (m->m_pkthdr.csum_flags & (CSUM_IP_TSO | CSUM_IP6_TSO))
2681 txq->vtntx_stats.vtxs_tso++;
2682 #else
2683 panic("INET/INET6 csum_flags set without INET/INET6 support");
2684 #endif /* defined(INET) || defined(INET6) */
2685 }
2686
2687 error = vtnet_txq_enqueue_buf(txq, m_head, txhdr);
2688 fail:
2689 if (error)
2690 uma_zfree(vtnet_tx_header_zone, txhdr);
2691
2692 return (error);
2693 }
2694
2695
2696 static void
vtnet_start_locked(struct vtnet_txq * txq,if_t ifp)2697 vtnet_start_locked(struct vtnet_txq *txq, if_t ifp)
2698 {
2699 struct vtnet_softc *sc;
2700 struct virtqueue *vq;
2701 struct mbuf *m0;
2702 int tries, enq;
2703
2704 sc = txq->vtntx_sc;
2705 vq = txq->vtntx_vq;
2706 tries = 0;
2707
2708 VTNET_TXQ_LOCK_ASSERT(txq);
2709
2710 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
2711 sc->vtnet_link_active == 0)
2712 return;
2713
2714 vtnet_txq_eof(txq);
2715
2716 again:
2717 enq = 0;
2718
2719 while (!if_sendq_empty(ifp)) {
2720 if (virtqueue_full(vq))
2721 break;
2722
2723 m0 = if_dequeue(ifp);
2724 if (m0 == NULL)
2725 break;
2726
2727 if (vtnet_txq_encap(txq, &m0, M_NOWAIT) != 0) {
2728 if (m0 != NULL)
2729 if_sendq_prepend(ifp, m0);
2730 break;
2731 }
2732
2733 enq++;
2734 ETHER_BPF_MTAP(ifp, m0);
2735 }
2736
2737 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2738 if (tries++ < VTNET_NOTIFY_RETRIES)
2739 goto again;
2740
2741 txq->vtntx_stats.vtxs_rescheduled++;
2742 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2743 }
2744 }
2745
2746 static void
vtnet_start(if_t ifp)2747 vtnet_start(if_t ifp)
2748 {
2749 struct vtnet_softc *sc;
2750 struct vtnet_txq *txq;
2751
2752 sc = if_getsoftc(ifp);
2753 txq = &sc->vtnet_txqs[0];
2754
2755 VTNET_TXQ_LOCK(txq);
2756 vtnet_start_locked(txq, ifp);
2757 VTNET_TXQ_UNLOCK(txq);
2758 }
2759
2760
2761 static int
vtnet_txq_mq_start_locked(struct vtnet_txq * txq,struct mbuf * m)2762 vtnet_txq_mq_start_locked(struct vtnet_txq *txq, struct mbuf *m)
2763 {
2764 struct vtnet_softc *sc;
2765 struct virtqueue *vq;
2766 struct buf_ring *br;
2767 if_t ifp;
2768 int enq, tries, error;
2769
2770 sc = txq->vtntx_sc;
2771 vq = txq->vtntx_vq;
2772 br = txq->vtntx_br;
2773 ifp = sc->vtnet_ifp;
2774 tries = 0;
2775 error = 0;
2776
2777 VTNET_TXQ_LOCK_ASSERT(txq);
2778
2779 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
2780 sc->vtnet_link_active == 0) {
2781 if (m != NULL)
2782 error = drbr_enqueue(ifp, br, m);
2783 return (error);
2784 }
2785
2786 if (m != NULL) {
2787 error = drbr_enqueue(ifp, br, m);
2788 if (error)
2789 return (error);
2790 }
2791
2792 vtnet_txq_eof(txq);
2793
2794 again:
2795 enq = 0;
2796
2797 while ((m = drbr_peek(ifp, br)) != NULL) {
2798 if (virtqueue_full(vq)) {
2799 drbr_putback(ifp, br, m);
2800 break;
2801 }
2802
2803 if (vtnet_txq_encap(txq, &m, M_NOWAIT) != 0) {
2804 if (m != NULL)
2805 drbr_putback(ifp, br, m);
2806 else
2807 drbr_advance(ifp, br);
2808 break;
2809 }
2810 drbr_advance(ifp, br);
2811
2812 enq++;
2813 ETHER_BPF_MTAP(ifp, m);
2814 }
2815
2816 if (enq > 0 && vtnet_txq_notify(txq) != 0) {
2817 if (tries++ < VTNET_NOTIFY_RETRIES)
2818 goto again;
2819
2820 txq->vtntx_stats.vtxs_rescheduled++;
2821 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_intrtask);
2822 }
2823
2824 return (0);
2825 }
2826
2827 static int
vtnet_txq_mq_start(if_t ifp,struct mbuf * m)2828 vtnet_txq_mq_start(if_t ifp, struct mbuf *m)
2829 {
2830 struct vtnet_softc *sc;
2831 struct vtnet_txq *txq;
2832 int i, npairs, error;
2833
2834 sc = if_getsoftc(ifp);
2835 npairs = sc->vtnet_act_vq_pairs;
2836
2837 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
2838 i = m->m_pkthdr.flowid % npairs;
2839 else
2840 i = curcpu % npairs;
2841
2842 txq = &sc->vtnet_txqs[i];
2843
2844 if (VTNET_TXQ_TRYLOCK(txq) != 0) {
2845 error = vtnet_txq_mq_start_locked(txq, m);
2846 VTNET_TXQ_UNLOCK(txq);
2847 } else {
2848 error = drbr_enqueue(ifp, txq->vtntx_br, m);
2849 taskqueue_enqueue(txq->vtntx_tq, &txq->vtntx_defrtask);
2850 }
2851
2852 return (error);
2853 }
2854
2855 static void
vtnet_txq_tq_deferred(void * xtxq,int pending __unused)2856 vtnet_txq_tq_deferred(void *xtxq, int pending __unused)
2857 {
2858 struct vtnet_softc *sc;
2859 struct vtnet_txq *txq;
2860
2861 txq = xtxq;
2862 sc = txq->vtntx_sc;
2863
2864 VTNET_TXQ_LOCK(txq);
2865 if (!drbr_empty(sc->vtnet_ifp, txq->vtntx_br))
2866 vtnet_txq_mq_start_locked(txq, NULL);
2867 VTNET_TXQ_UNLOCK(txq);
2868 }
2869
2870
2871 static void
vtnet_txq_start(struct vtnet_txq * txq)2872 vtnet_txq_start(struct vtnet_txq *txq)
2873 {
2874 struct vtnet_softc *sc;
2875 if_t ifp;
2876
2877 sc = txq->vtntx_sc;
2878 ifp = sc->vtnet_ifp;
2879
2880 if (!VTNET_ALTQ_ENABLED) {
2881 if (!drbr_empty(ifp, txq->vtntx_br))
2882 vtnet_txq_mq_start_locked(txq, NULL);
2883 } else {
2884 if (!if_sendq_empty(ifp))
2885 vtnet_start_locked(txq, ifp);
2886
2887 }
2888 }
2889
2890 static void
vtnet_txq_tq_intr(void * xtxq,int pending __unused)2891 vtnet_txq_tq_intr(void *xtxq, int pending __unused)
2892 {
2893 struct vtnet_softc *sc;
2894 struct vtnet_txq *txq;
2895 if_t ifp;
2896
2897 txq = xtxq;
2898 sc = txq->vtntx_sc;
2899 ifp = sc->vtnet_ifp;
2900
2901 VTNET_TXQ_LOCK(txq);
2902
2903 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2904 VTNET_TXQ_UNLOCK(txq);
2905 return;
2906 }
2907
2908 vtnet_txq_eof(txq);
2909 vtnet_txq_start(txq);
2910
2911 VTNET_TXQ_UNLOCK(txq);
2912 }
2913
2914 static int
vtnet_txq_eof(struct vtnet_txq * txq)2915 vtnet_txq_eof(struct vtnet_txq *txq)
2916 {
2917 struct vtnet_softc *sc;
2918 struct virtqueue *vq;
2919 struct vtnet_tx_header *txhdr;
2920 struct mbuf *m;
2921 int deq;
2922
2923 vq = txq->vtntx_vq;
2924 deq = 0;
2925 VTNET_TXQ_LOCK_ASSERT(txq);
2926
2927 sc = txq->vtntx_sc;
2928
2929 while ((txhdr = virtqueue_dequeue(vq, NULL)) != NULL) {
2930 m = txhdr->vth_mbuf;
2931 deq++;
2932
2933 txq->vtntx_stats.vtxs_opackets++;
2934 txq->vtntx_stats.vtxs_obytes += m->m_pkthdr.len;
2935 if (m->m_flags & M_MCAST)
2936 txq->vtntx_stats.vtxs_omcasts++;
2937
2938 bus_dmamap_unload(sc->vtnet_tx_dmat, txhdr->dmap);
2939 bus_dmamap_destroy(sc->vtnet_tx_dmat, txhdr->dmap);
2940 bus_dmamap_unload(sc->vtnet_tx_dmat, txhdr->hdr_dmap);
2941 bus_dmamap_destroy(sc->vtnet_tx_dmat, txhdr->hdr_dmap);
2942
2943 m_freem(m);
2944 uma_zfree(vtnet_tx_header_zone, txhdr);
2945 }
2946
2947 if (virtqueue_empty(vq))
2948 txq->vtntx_watchdog = 0;
2949
2950 return (deq);
2951 }
2952
2953 static void
vtnet_tx_vq_intr(void * xtxq)2954 vtnet_tx_vq_intr(void *xtxq)
2955 {
2956 struct vtnet_softc *sc;
2957 struct vtnet_txq *txq;
2958 if_t ifp;
2959
2960 txq = xtxq;
2961 sc = txq->vtntx_sc;
2962 ifp = sc->vtnet_ifp;
2963
2964 if (__predict_false(txq->vtntx_id >= sc->vtnet_act_vq_pairs)) {
2965 /*
2966 * Ignore this interrupt. Either this is a spurious interrupt
2967 * or multiqueue without per-VQ MSIX so every queue needs to
2968 * be polled (a brain dead configuration we could try harder
2969 * to avoid).
2970 */
2971 vtnet_txq_disable_intr(txq);
2972 return;
2973 }
2974
2975 #ifdef DEV_NETMAP
2976 if (netmap_tx_irq(ifp, txq->vtntx_id) != NM_IRQ_PASS)
2977 return;
2978 #endif /* DEV_NETMAP */
2979
2980 VTNET_TXQ_LOCK(txq);
2981
2982 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
2983 VTNET_TXQ_UNLOCK(txq);
2984 return;
2985 }
2986
2987 vtnet_txq_eof(txq);
2988 vtnet_txq_start(txq);
2989
2990 VTNET_TXQ_UNLOCK(txq);
2991 }
2992
2993 static void
vtnet_tx_start_all(struct vtnet_softc * sc)2994 vtnet_tx_start_all(struct vtnet_softc *sc)
2995 {
2996 struct vtnet_txq *txq;
2997 int i;
2998
2999 VTNET_CORE_LOCK_ASSERT(sc);
3000
3001 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
3002 txq = &sc->vtnet_txqs[i];
3003
3004 VTNET_TXQ_LOCK(txq);
3005 vtnet_txq_start(txq);
3006 VTNET_TXQ_UNLOCK(txq);
3007 }
3008 }
3009
3010 static void
vtnet_qflush(if_t ifp)3011 vtnet_qflush(if_t ifp)
3012 {
3013 struct vtnet_softc *sc;
3014 struct vtnet_txq *txq;
3015 struct mbuf *m;
3016 int i;
3017
3018 sc = if_getsoftc(ifp);
3019
3020 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
3021 txq = &sc->vtnet_txqs[i];
3022
3023 VTNET_TXQ_LOCK(txq);
3024 while ((m = buf_ring_dequeue_sc(txq->vtntx_br)) != NULL)
3025 m_freem(m);
3026 VTNET_TXQ_UNLOCK(txq);
3027 }
3028
3029 if_qflush(ifp);
3030 }
3031
3032 static int
vtnet_watchdog(struct vtnet_txq * txq)3033 vtnet_watchdog(struct vtnet_txq *txq)
3034 {
3035 if_t ifp;
3036
3037 ifp = txq->vtntx_sc->vtnet_ifp;
3038
3039 VTNET_TXQ_LOCK(txq);
3040 if (txq->vtntx_watchdog == 1) {
3041 /*
3042 * Only drain completed frames if the watchdog is about to
3043 * expire. If any frames were drained, there may be enough
3044 * free descriptors now available to transmit queued frames.
3045 * In that case, the timer will immediately be decremented
3046 * below, but the timeout is generous enough that should not
3047 * be a problem.
3048 */
3049 if (vtnet_txq_eof(txq) != 0)
3050 vtnet_txq_start(txq);
3051 }
3052
3053 if (txq->vtntx_watchdog == 0 || --txq->vtntx_watchdog) {
3054 VTNET_TXQ_UNLOCK(txq);
3055 return (0);
3056 }
3057 VTNET_TXQ_UNLOCK(txq);
3058
3059 if_printf(ifp, "watchdog timeout on queue %d\n", txq->vtntx_id);
3060 return (1);
3061 }
3062
3063 static void
vtnet_accum_stats(struct vtnet_softc * sc,struct vtnet_rxq_stats * rxacc,struct vtnet_txq_stats * txacc)3064 vtnet_accum_stats(struct vtnet_softc *sc, struct vtnet_rxq_stats *rxacc,
3065 struct vtnet_txq_stats *txacc)
3066 {
3067
3068 bzero(rxacc, sizeof(struct vtnet_rxq_stats));
3069 bzero(txacc, sizeof(struct vtnet_txq_stats));
3070
3071 for (int i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3072 struct vtnet_rxq_stats *rxst;
3073 struct vtnet_txq_stats *txst;
3074
3075 rxst = &sc->vtnet_rxqs[i].vtnrx_stats;
3076 rxacc->vrxs_ipackets += rxst->vrxs_ipackets;
3077 rxacc->vrxs_ibytes += rxst->vrxs_ibytes;
3078 rxacc->vrxs_iqdrops += rxst->vrxs_iqdrops;
3079 rxacc->vrxs_csum += rxst->vrxs_csum;
3080 rxacc->vrxs_csum_failed += rxst->vrxs_csum_failed;
3081 rxacc->vrxs_rescheduled += rxst->vrxs_rescheduled;
3082
3083 txst = &sc->vtnet_txqs[i].vtntx_stats;
3084 txacc->vtxs_opackets += txst->vtxs_opackets;
3085 txacc->vtxs_obytes += txst->vtxs_obytes;
3086 txacc->vtxs_csum += txst->vtxs_csum;
3087 txacc->vtxs_tso += txst->vtxs_tso;
3088 txacc->vtxs_rescheduled += txst->vtxs_rescheduled;
3089 }
3090 }
3091
3092 static uint64_t
vtnet_get_counter(if_t ifp,ift_counter cnt)3093 vtnet_get_counter(if_t ifp, ift_counter cnt)
3094 {
3095 struct vtnet_softc *sc;
3096 struct vtnet_rxq_stats rxaccum;
3097 struct vtnet_txq_stats txaccum;
3098
3099 sc = if_getsoftc(ifp);
3100 vtnet_accum_stats(sc, &rxaccum, &txaccum);
3101
3102 switch (cnt) {
3103 case IFCOUNTER_IPACKETS:
3104 return (rxaccum.vrxs_ipackets);
3105 case IFCOUNTER_IQDROPS:
3106 return (rxaccum.vrxs_iqdrops);
3107 case IFCOUNTER_IERRORS:
3108 return (rxaccum.vrxs_ierrors);
3109 case IFCOUNTER_IBYTES:
3110 return (rxaccum.vrxs_ibytes);
3111 case IFCOUNTER_OPACKETS:
3112 return (txaccum.vtxs_opackets);
3113 case IFCOUNTER_OBYTES:
3114 return (txaccum.vtxs_obytes);
3115 case IFCOUNTER_OMCASTS:
3116 return (txaccum.vtxs_omcasts);
3117 default:
3118 return (if_get_counter_default(ifp, cnt));
3119 }
3120 }
3121
3122 static void
vtnet_tick(void * xsc)3123 vtnet_tick(void *xsc)
3124 {
3125 struct vtnet_softc *sc;
3126 if_t ifp;
3127 int i, timedout;
3128
3129 sc = xsc;
3130 ifp = sc->vtnet_ifp;
3131 timedout = 0;
3132
3133 VTNET_CORE_LOCK_ASSERT(sc);
3134
3135 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
3136 timedout |= vtnet_watchdog(&sc->vtnet_txqs[i]);
3137
3138 if (timedout != 0) {
3139 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3140 vtnet_init_locked(sc, 0);
3141 } else
3142 callout_schedule(&sc->vtnet_tick_ch, hz);
3143 }
3144
3145 static void
vtnet_start_taskqueues(struct vtnet_softc * sc)3146 vtnet_start_taskqueues(struct vtnet_softc *sc)
3147 {
3148 device_t dev;
3149 struct vtnet_rxq *rxq;
3150 struct vtnet_txq *txq;
3151 int i, error;
3152
3153 dev = sc->vtnet_dev;
3154
3155 /*
3156 * Errors here are very difficult to recover from - we cannot
3157 * easily fail because, if this is during boot, we will hang
3158 * when freeing any successfully started taskqueues because
3159 * the scheduler isn't up yet.
3160 *
3161 * Most drivers just ignore the return value - it only fails
3162 * with ENOMEM so an error is not likely.
3163 */
3164 for (i = 0; i < sc->vtnet_req_vq_pairs; i++) {
3165 rxq = &sc->vtnet_rxqs[i];
3166 error = taskqueue_start_threads(&rxq->vtnrx_tq, 1, PI_NET,
3167 "%s rxq %d", device_get_nameunit(dev), rxq->vtnrx_id);
3168 if (error) {
3169 device_printf(dev, "failed to start rx taskq %d\n",
3170 rxq->vtnrx_id);
3171 }
3172
3173 txq = &sc->vtnet_txqs[i];
3174 error = taskqueue_start_threads(&txq->vtntx_tq, 1, PI_NET,
3175 "%s txq %d", device_get_nameunit(dev), txq->vtntx_id);
3176 if (error) {
3177 device_printf(dev, "failed to start tx taskq %d\n",
3178 txq->vtntx_id);
3179 }
3180 }
3181 }
3182
3183 static void
vtnet_free_taskqueues(struct vtnet_softc * sc)3184 vtnet_free_taskqueues(struct vtnet_softc *sc)
3185 {
3186 struct vtnet_rxq *rxq;
3187 struct vtnet_txq *txq;
3188 int i;
3189
3190 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3191 rxq = &sc->vtnet_rxqs[i];
3192 if (rxq->vtnrx_tq != NULL) {
3193 taskqueue_free(rxq->vtnrx_tq);
3194 rxq->vtnrx_tq = NULL;
3195 }
3196
3197 txq = &sc->vtnet_txqs[i];
3198 if (txq->vtntx_tq != NULL) {
3199 taskqueue_free(txq->vtntx_tq);
3200 txq->vtntx_tq = NULL;
3201 }
3202 }
3203 }
3204
3205 static void
vtnet_drain_taskqueues(struct vtnet_softc * sc)3206 vtnet_drain_taskqueues(struct vtnet_softc *sc)
3207 {
3208 struct vtnet_rxq *rxq;
3209 struct vtnet_txq *txq;
3210 int i;
3211
3212 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3213 rxq = &sc->vtnet_rxqs[i];
3214 if (rxq->vtnrx_tq != NULL)
3215 taskqueue_drain(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
3216
3217 txq = &sc->vtnet_txqs[i];
3218 if (txq->vtntx_tq != NULL) {
3219 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_intrtask);
3220 if (!VTNET_ALTQ_ENABLED)
3221 taskqueue_drain(txq->vtntx_tq, &txq->vtntx_defrtask);
3222 }
3223 }
3224 }
3225
3226 static void
vtnet_drain_rxtx_queues(struct vtnet_softc * sc)3227 vtnet_drain_rxtx_queues(struct vtnet_softc *sc)
3228 {
3229 struct vtnet_rxq *rxq;
3230 struct vtnet_txq *txq;
3231 int i;
3232
3233 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3234 rxq = &sc->vtnet_rxqs[i];
3235 vtnet_rxq_free_mbufs(rxq);
3236
3237 txq = &sc->vtnet_txqs[i];
3238 vtnet_txq_free_mbufs(txq);
3239 }
3240 }
3241
3242 static void
vtnet_stop_rendezvous(struct vtnet_softc * sc)3243 vtnet_stop_rendezvous(struct vtnet_softc *sc)
3244 {
3245 struct vtnet_rxq *rxq;
3246 struct vtnet_txq *txq;
3247 int i;
3248
3249 VTNET_CORE_LOCK_ASSERT(sc);
3250
3251 /*
3252 * Lock and unlock the per-queue mutex so we known the stop
3253 * state is visible. Doing only the active queues should be
3254 * sufficient, but it does not cost much extra to do all the
3255 * queues.
3256 */
3257 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
3258 rxq = &sc->vtnet_rxqs[i];
3259 VTNET_RXQ_LOCK(rxq);
3260 VTNET_RXQ_UNLOCK(rxq);
3261
3262 txq = &sc->vtnet_txqs[i];
3263 VTNET_TXQ_LOCK(txq);
3264 VTNET_TXQ_UNLOCK(txq);
3265 }
3266 }
3267
3268 static void
vtnet_stop(struct vtnet_softc * sc)3269 vtnet_stop(struct vtnet_softc *sc)
3270 {
3271 device_t dev;
3272 if_t ifp;
3273
3274 dev = sc->vtnet_dev;
3275 ifp = sc->vtnet_ifp;
3276
3277 VTNET_CORE_LOCK_ASSERT(sc);
3278
3279 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3280 sc->vtnet_link_active = 0;
3281 callout_stop(&sc->vtnet_tick_ch);
3282
3283 /* Only advisory. */
3284 vtnet_disable_interrupts(sc);
3285
3286 #ifdef DEV_NETMAP
3287 /* Stop any pending txsync/rxsync and disable them. */
3288 netmap_disable_all_rings(ifp);
3289 #endif /* DEV_NETMAP */
3290
3291 /*
3292 * Stop the host adapter. This resets it to the pre-initialized
3293 * state. It will not generate any interrupts until after it is
3294 * reinitialized.
3295 */
3296 virtio_stop(dev);
3297 vtnet_stop_rendezvous(sc);
3298
3299 vtnet_drain_rxtx_queues(sc);
3300 sc->vtnet_act_vq_pairs = 1;
3301 }
3302
3303 static int
vtnet_virtio_reinit(struct vtnet_softc * sc)3304 vtnet_virtio_reinit(struct vtnet_softc *sc)
3305 {
3306 device_t dev;
3307 if_t ifp;
3308 uint64_t features;
3309 int error;
3310
3311 dev = sc->vtnet_dev;
3312 ifp = sc->vtnet_ifp;
3313 features = sc->vtnet_negotiated_features;
3314
3315 /*
3316 * Re-negotiate with the host, removing any disabled receive
3317 * features. Transmit features are disabled only on our side
3318 * via if_capenable and if_hwassist.
3319 */
3320
3321 if ((if_getcapenable(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) == 0)
3322 features &= ~(VIRTIO_NET_F_GUEST_CSUM | VTNET_LRO_FEATURES);
3323
3324 if ((if_getcapenable(ifp) & IFCAP_LRO) == 0)
3325 features &= ~VTNET_LRO_FEATURES;
3326
3327 if ((if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) == 0)
3328 features &= ~VIRTIO_NET_F_CTRL_VLAN;
3329
3330 error = virtio_reinit(dev, features);
3331 if (error) {
3332 device_printf(dev, "virtio reinit error %d\n", error);
3333 return (error);
3334 }
3335
3336 sc->vtnet_features = features;
3337 virtio_reinit_complete(dev);
3338
3339 return (0);
3340 }
3341
3342 static void
vtnet_init_rx_filters(struct vtnet_softc * sc)3343 vtnet_init_rx_filters(struct vtnet_softc *sc)
3344 {
3345 if_t ifp;
3346
3347 ifp = sc->vtnet_ifp;
3348
3349 if (sc->vtnet_flags & VTNET_FLAG_CTRL_RX) {
3350 vtnet_rx_filter(sc);
3351 vtnet_rx_filter_mac(sc);
3352 }
3353
3354 if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
3355 vtnet_rx_filter_vlan(sc);
3356 }
3357
3358 static int
vtnet_init_rx_queues(struct vtnet_softc * sc)3359 vtnet_init_rx_queues(struct vtnet_softc *sc)
3360 {
3361 device_t dev;
3362 if_t ifp;
3363 struct vtnet_rxq *rxq;
3364 int i, clustersz, error;
3365
3366 dev = sc->vtnet_dev;
3367 ifp = sc->vtnet_ifp;
3368
3369 clustersz = vtnet_rx_cluster_size(sc, if_getmtu(ifp));
3370 sc->vtnet_rx_clustersz = clustersz;
3371
3372 if (sc->vtnet_flags & VTNET_FLAG_LRO_NOMRG) {
3373 sc->vtnet_rx_nmbufs = howmany(sizeof(struct vtnet_rx_header) +
3374 VTNET_MAX_RX_SIZE, clustersz);
3375 KASSERT(sc->vtnet_rx_nmbufs < sc->vtnet_rx_nsegs,
3376 ("%s: too many rx mbufs %d for %d segments", __func__,
3377 sc->vtnet_rx_nmbufs, sc->vtnet_rx_nsegs));
3378 } else
3379 sc->vtnet_rx_nmbufs = 1;
3380
3381 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
3382 rxq = &sc->vtnet_rxqs[i];
3383
3384 /* Hold the lock to satisfy asserts. */
3385 VTNET_RXQ_LOCK(rxq);
3386 error = vtnet_rxq_populate(rxq);
3387 VTNET_RXQ_UNLOCK(rxq);
3388
3389 if (error) {
3390 device_printf(dev, "cannot populate Rx queue %d\n", i);
3391 return (error);
3392 }
3393 }
3394
3395 return (0);
3396 }
3397
3398 static int
vtnet_init_tx_queues(struct vtnet_softc * sc)3399 vtnet_init_tx_queues(struct vtnet_softc *sc)
3400 {
3401 struct vtnet_txq *txq;
3402 int i;
3403
3404 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
3405 txq = &sc->vtnet_txqs[i];
3406 txq->vtntx_watchdog = 0;
3407 txq->vtntx_intr_threshold = vtnet_txq_intr_threshold(txq);
3408 #ifdef DEV_NETMAP
3409 netmap_reset(NA(sc->vtnet_ifp), NR_TX, i, 0);
3410 #endif /* DEV_NETMAP */
3411 }
3412
3413 return (0);
3414 }
3415
3416 static int
vtnet_init_rxtx_queues(struct vtnet_softc * sc)3417 vtnet_init_rxtx_queues(struct vtnet_softc *sc)
3418 {
3419 int error;
3420
3421 error = vtnet_init_rx_queues(sc);
3422 if (error)
3423 return (error);
3424
3425 error = vtnet_init_tx_queues(sc);
3426 if (error)
3427 return (error);
3428
3429 return (0);
3430 }
3431
3432 static void
vtnet_set_active_vq_pairs(struct vtnet_softc * sc)3433 vtnet_set_active_vq_pairs(struct vtnet_softc *sc)
3434 {
3435 device_t dev;
3436 int npairs;
3437
3438 dev = sc->vtnet_dev;
3439
3440 if ((sc->vtnet_flags & VTNET_FLAG_MQ) == 0) {
3441 sc->vtnet_act_vq_pairs = 1;
3442 return;
3443 }
3444
3445 npairs = sc->vtnet_req_vq_pairs;
3446
3447 if (vtnet_ctrl_mq_cmd(sc, npairs) != 0) {
3448 device_printf(dev, "cannot set active queue pairs to %d, "
3449 "falling back to 1 queue pair\n", npairs);
3450 npairs = 1;
3451 }
3452
3453 sc->vtnet_act_vq_pairs = npairs;
3454 }
3455
3456 static void
vtnet_update_rx_offloads(struct vtnet_softc * sc)3457 vtnet_update_rx_offloads(struct vtnet_softc *sc)
3458 {
3459 if_t ifp;
3460 uint64_t features;
3461 int error;
3462
3463 ifp = sc->vtnet_ifp;
3464 features = sc->vtnet_features;
3465
3466 VTNET_CORE_LOCK_ASSERT(sc);
3467
3468 if (if_getcapabilities(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
3469 if (if_getcapenable(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
3470 features |= VIRTIO_NET_F_GUEST_CSUM;
3471 else
3472 features &= ~VIRTIO_NET_F_GUEST_CSUM;
3473 }
3474
3475 if (if_getcapabilities(ifp) & IFCAP_LRO && !vtnet_software_lro(sc)) {
3476 if (if_getcapenable(ifp) & IFCAP_LRO)
3477 features |= VTNET_LRO_FEATURES;
3478 else
3479 features &= ~VTNET_LRO_FEATURES;
3480 }
3481
3482 error = vtnet_ctrl_guest_offloads(sc,
3483 features & (VIRTIO_NET_F_GUEST_CSUM | VIRTIO_NET_F_GUEST_TSO4 |
3484 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN |
3485 VIRTIO_NET_F_GUEST_UFO));
3486 if (error) {
3487 device_printf(sc->vtnet_dev,
3488 "%s: cannot update Rx features\n", __func__);
3489 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3490 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3491 vtnet_init_locked(sc, 0);
3492 }
3493 } else
3494 sc->vtnet_features = features;
3495 }
3496
3497 static int
vtnet_reinit(struct vtnet_softc * sc)3498 vtnet_reinit(struct vtnet_softc *sc)
3499 {
3500 if_t ifp;
3501 int error;
3502
3503 ifp = sc->vtnet_ifp;
3504
3505 bcopy(if_getlladdr(ifp), sc->vtnet_hwaddr, ETHER_ADDR_LEN);
3506
3507 error = vtnet_virtio_reinit(sc);
3508 if (error)
3509 return (error);
3510
3511 vtnet_set_macaddr(sc);
3512 vtnet_set_active_vq_pairs(sc);
3513
3514 if (sc->vtnet_flags & VTNET_FLAG_CTRL_VQ)
3515 vtnet_init_rx_filters(sc);
3516
3517 if_sethwassist(ifp, 0);
3518 if (if_getcapenable(ifp) & IFCAP_TXCSUM)
3519 if_sethwassistbits(ifp, VTNET_CSUM_OFFLOAD, 0);
3520 if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
3521 if_sethwassistbits(ifp, VTNET_CSUM_OFFLOAD_IPV6, 0);
3522 if (if_getcapenable(ifp) & IFCAP_TSO4)
3523 if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
3524 if (if_getcapenable(ifp) & IFCAP_TSO6)
3525 if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
3526
3527 error = vtnet_init_rxtx_queues(sc);
3528 if (error)
3529 return (error);
3530
3531 return (0);
3532 }
3533
3534 static void
vtnet_init_locked(struct vtnet_softc * sc,int init_mode)3535 vtnet_init_locked(struct vtnet_softc *sc, int init_mode)
3536 {
3537 if_t ifp;
3538
3539 ifp = sc->vtnet_ifp;
3540
3541 VTNET_CORE_LOCK_ASSERT(sc);
3542
3543 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
3544 return;
3545
3546 vtnet_stop(sc);
3547
3548 #ifdef DEV_NETMAP
3549 /* Once stopped we can update the netmap flags, if necessary. */
3550 switch (init_mode) {
3551 case VTNET_INIT_NETMAP_ENTER:
3552 nm_set_native_flags(NA(ifp));
3553 break;
3554 case VTNET_INIT_NETMAP_EXIT:
3555 nm_clear_native_flags(NA(ifp));
3556 break;
3557 }
3558 #endif /* DEV_NETMAP */
3559
3560 if (vtnet_reinit(sc) != 0) {
3561 vtnet_stop(sc);
3562 return;
3563 }
3564
3565 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
3566 vtnet_update_link_status(sc);
3567 vtnet_enable_interrupts(sc);
3568 callout_reset(&sc->vtnet_tick_ch, hz, vtnet_tick, sc);
3569
3570 #ifdef DEV_NETMAP
3571 /* Re-enable txsync/rxsync. */
3572 netmap_enable_all_rings(ifp);
3573 #endif /* DEV_NETMAP */
3574 }
3575
3576 static void
vtnet_init(void * xsc)3577 vtnet_init(void *xsc)
3578 {
3579 struct vtnet_softc *sc;
3580
3581 sc = xsc;
3582
3583 VTNET_CORE_LOCK(sc);
3584 vtnet_init_locked(sc, 0);
3585 VTNET_CORE_UNLOCK(sc);
3586 }
3587
3588 static void
vtnet_free_ctrl_vq(struct vtnet_softc * sc)3589 vtnet_free_ctrl_vq(struct vtnet_softc *sc)
3590 {
3591
3592 /*
3593 * The control virtqueue is only polled and therefore it should
3594 * already be empty.
3595 */
3596 KASSERT(virtqueue_empty(sc->vtnet_ctrl_vq),
3597 ("%s: ctrl vq %p not empty", __func__, sc->vtnet_ctrl_vq));
3598 }
3599
3600 static void
vtnet_load_callback(void * arg,bus_dma_segment_t * segs,int nsegs,int error)3601 vtnet_load_callback(void *arg, bus_dma_segment_t *segs, int nsegs,
3602 int error)
3603 {
3604 bus_addr_t *paddr;
3605
3606 if (error != 0)
3607 return;
3608
3609 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
3610
3611 paddr = (bus_addr_t *)arg;
3612 *paddr = segs[0].ds_addr;
3613 }
3614
3615 static int
vtnet_exec_ctrl_cmd(struct vtnet_softc * sc,uint8_t * ack,struct sglist * sg,int readable,int writable)3616 vtnet_exec_ctrl_cmd(struct vtnet_softc *sc, uint8_t *ack, struct sglist *sg,
3617 int readable, int writable)
3618 {
3619 bus_dmamap_t ack_dmap;
3620 bus_addr_t ack_paddr;
3621 struct virtqueue *vq;
3622 int error;
3623
3624 error = bus_dmamap_create(sc->vtnet_ack_dmat, 0, &ack_dmap);
3625 if (error)
3626 goto error_out;
3627
3628 error = bus_dmamap_load(sc->vtnet_ack_dmat, ack_dmap, ack,
3629 sizeof(uint8_t), vtnet_load_callback, &ack_paddr, BUS_DMA_NOWAIT);
3630 if (error)
3631 goto error_destroy;
3632
3633 bus_dmamap_sync(sc->vtnet_ack_dmat, ack_dmap, BUS_DMASYNC_PREWRITE);
3634
3635 error = sglist_append_phys(sg, ack_paddr, sizeof(uint8_t));
3636 if (error)
3637 goto error_unload;
3638
3639 vq = sc->vtnet_ctrl_vq;
3640
3641 MPASS(sc->vtnet_flags & VTNET_FLAG_CTRL_VQ);
3642 VTNET_CORE_LOCK_ASSERT(sc);
3643
3644 if (!virtqueue_empty(vq))
3645 goto error_unload;
3646
3647 /*
3648 * Poll for the response, but the command is likely completed before
3649 * returning from the notify.
3650 */
3651 if (virtqueue_enqueue(vq, (void *)ack, sg, readable, writable) == 0) {
3652 virtqueue_notify(vq);
3653 virtqueue_poll(vq, NULL);
3654 }
3655
3656 bus_dmamap_sync(sc->vtnet_ack_dmat, ack_dmap, BUS_DMASYNC_POSTREAD);
3657
3658 error_unload:
3659 bus_dmamap_unload(sc->vtnet_ack_dmat, ack_dmap);
3660 error_destroy:
3661 bus_dmamap_destroy(sc->vtnet_ack_dmat, ack_dmap);
3662 error_out:
3663 return (error);
3664 }
3665
3666 static int
vtnet_ctrl_mac_cmd(struct vtnet_softc * sc,uint8_t * hwaddr)3667 vtnet_ctrl_mac_cmd(struct vtnet_softc *sc, uint8_t *hwaddr)
3668 {
3669 struct sglist_seg segs[3];
3670 bus_dmamap_t hdr_dmap;
3671 bus_addr_t hdr_paddr;
3672 struct sglist sg;
3673 struct {
3674 struct virtio_net_ctrl_hdr hdr __aligned(2);
3675 uint8_t pad1;
3676 uint8_t addr[ETHER_ADDR_LEN] __aligned(8);
3677 uint8_t pad2;
3678 } s;
3679 uint8_t ack;
3680 int error;
3681
3682 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
3683 if (error)
3684 goto error_out;
3685
3686 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &s,
3687 sizeof(s), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
3688 if (error)
3689 goto error_destroy_hdr;
3690
3691 MPASS(sc->vtnet_flags & VTNET_FLAG_CTRL_MAC);
3692
3693 s.hdr.class = VIRTIO_NET_CTRL_MAC;
3694 s.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
3695 bcopy(hwaddr, &s.addr[0], ETHER_ADDR_LEN);
3696 ack = VIRTIO_NET_ERR;
3697 bus_dmamap_sync(sc->vtnet_hdr_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
3698
3699 sglist_init(&sg, nitems(segs), segs);
3700 error |= sglist_append_phys(&sg, hdr_paddr,
3701 sizeof(struct virtio_net_ctrl_hdr));
3702 error |= sglist_append_phys(&sg,
3703 hdr_paddr + ((uintptr_t)&s.addr - (uintptr_t)&s),
3704 ETHER_ADDR_LEN);
3705 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
3706
3707 if (error == 0)
3708 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
3709 if (error == 0)
3710 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
3711
3712 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
3713 error_destroy_hdr:
3714 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
3715 error_out:
3716 return (error);
3717 }
3718
3719 static int
vtnet_ctrl_guest_offloads(struct vtnet_softc * sc,uint64_t offloads)3720 vtnet_ctrl_guest_offloads(struct vtnet_softc *sc, uint64_t offloads)
3721 {
3722 struct sglist_seg segs[3];
3723 bus_dmamap_t hdr_dmap;
3724 bus_addr_t hdr_paddr;
3725 struct sglist sg;
3726 struct {
3727 struct virtio_net_ctrl_hdr hdr __aligned(2);
3728 uint8_t pad1;
3729 uint64_t offloads __aligned(8);
3730 uint8_t pad2;
3731 } s;
3732 uint8_t ack;
3733 int error;
3734
3735 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
3736 if (error)
3737 goto error_out;
3738
3739 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &s,
3740 sizeof(s), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
3741 if (error)
3742 goto error_destroy_hdr;
3743
3744 MPASS(sc->vtnet_features & VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
3745
3746 s.hdr.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
3747 s.hdr.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
3748 s.offloads = vtnet_gtoh64(sc, offloads);
3749 ack = VIRTIO_NET_ERR;
3750 bus_dmamap_sync(sc->vtnet_hdr_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
3751
3752 sglist_init(&sg, nitems(segs), segs);
3753 error |= sglist_append_phys(&sg, hdr_paddr,
3754 sizeof(struct virtio_net_ctrl_hdr));
3755 error |= sglist_append_phys(&sg,
3756 hdr_paddr + ((uintptr_t)&s.offloads - (uintptr_t)&s),
3757 sizeof(uint64_t));
3758 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
3759
3760 if (error == 0)
3761 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
3762 if (error == 0)
3763 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
3764
3765 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
3766 error_destroy_hdr:
3767 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
3768 error_out:
3769 return (error);
3770 }
3771
3772 static int
vtnet_ctrl_mq_cmd(struct vtnet_softc * sc,uint16_t npairs)3773 vtnet_ctrl_mq_cmd(struct vtnet_softc *sc, uint16_t npairs)
3774 {
3775 struct sglist_seg segs[3];
3776 bus_dmamap_t hdr_dmap;
3777 bus_addr_t hdr_paddr;
3778 struct sglist sg;
3779 struct {
3780 struct virtio_net_ctrl_hdr hdr __aligned(2);
3781 uint8_t pad1;
3782 struct virtio_net_ctrl_mq mq __aligned(2);
3783 uint8_t pad2;
3784 } s;
3785 uint8_t ack;
3786 int error;
3787
3788 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
3789 if (error)
3790 goto error_out;
3791
3792 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &s,
3793 sizeof(s), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
3794 if (error)
3795 goto error_destroy_hdr;
3796
3797 MPASS(sc->vtnet_flags & VTNET_FLAG_MQ);
3798
3799 s.hdr.class = VIRTIO_NET_CTRL_MQ;
3800 s.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
3801 s.mq.virtqueue_pairs = vtnet_gtoh16(sc, npairs);
3802 ack = VIRTIO_NET_ERR;
3803 bus_dmamap_sync(sc->vtnet_hdr_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
3804
3805 sglist_init(&sg, nitems(segs), segs);
3806 error |= sglist_append_phys(&sg, hdr_paddr,
3807 sizeof(struct virtio_net_ctrl_hdr));
3808 error |= sglist_append_phys(&sg,
3809 hdr_paddr + ((uintptr_t)&s.mq - (uintptr_t)&s),
3810 sizeof(struct virtio_net_ctrl_mq));
3811 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
3812
3813 if (error == 0)
3814 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
3815 if (error == 0)
3816 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
3817
3818 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
3819 error_destroy_hdr:
3820 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
3821 error_out:
3822 return (error);
3823 }
3824
3825 static int
vtnet_ctrl_announce_ack_cmd(struct vtnet_softc * sc)3826 vtnet_ctrl_announce_ack_cmd(struct vtnet_softc *sc)
3827 {
3828 struct sglist_seg segs[2];
3829 bus_dmamap_t hdr_dmap;
3830 bus_addr_t hdr_paddr;
3831 struct sglist sg;
3832 struct virtio_net_ctrl_hdr hdr __aligned(2);
3833 uint8_t ack;
3834 int error;
3835
3836 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
3837 if (error != 0)
3838 goto error_out;
3839
3840 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &hdr,
3841 sizeof(hdr), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
3842 if (error != 0)
3843 goto error_destroy_hdr;
3844
3845 hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
3846 hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
3847 ack = VIRTIO_NET_ERR;
3848 bus_dmamap_sync(sc->vtnet_hdr_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
3849
3850 sglist_init(&sg, nitems(segs), segs);
3851 error = sglist_append_phys(&sg, hdr_paddr, sizeof(hdr));
3852 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
3853
3854 if (error == 0)
3855 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
3856 if (error == 0)
3857 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
3858
3859 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
3860 error_destroy_hdr:
3861 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
3862 error_out:
3863 return (error);
3864 }
3865
3866 static bool
vtnet_announce_pending(struct vtnet_softc * sc)3867 vtnet_announce_pending(struct vtnet_softc *sc)
3868 {
3869 uint16_t status;
3870
3871 if ((sc->vtnet_features & VIRTIO_NET_F_GUEST_ANNOUNCE) == 0 ||
3872 (sc->vtnet_features & VIRTIO_NET_F_CTRL_VQ) == 0 ||
3873 (sc->vtnet_features & VIRTIO_NET_F_STATUS) == 0)
3874 return (false);
3875
3876 status = virtio_read_dev_config_2(sc->vtnet_dev,
3877 offsetof(struct virtio_net_config, status));
3878
3879 return ((status & VIRTIO_NET_S_ANNOUNCE) != 0);
3880 }
3881
3882 static void
vtnet_announce(void * xsc,int pending __unused)3883 vtnet_announce(void *xsc, int pending __unused)
3884 {
3885 struct vtnet_softc *sc;
3886 if_t ifp;
3887
3888 sc = xsc;
3889 ifp = sc->vtnet_ifp;
3890
3891 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
3892 return;
3893
3894 CURVNET_SET(if_getvnet(ifp));
3895 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
3896 CURVNET_RESTORE();
3897
3898 VTNET_CORE_LOCK(sc);
3899 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
3900 vtnet_ctrl_announce_ack_cmd(sc) != 0)
3901 device_printf(sc->vtnet_dev, "cannot ack announcement\n");
3902 VTNET_CORE_UNLOCK(sc);
3903 }
3904
3905 static int
vtnet_ctrl_rx_cmd(struct vtnet_softc * sc,uint8_t cmd,bool on)3906 vtnet_ctrl_rx_cmd(struct vtnet_softc *sc, uint8_t cmd, bool on)
3907 {
3908 struct sglist_seg segs[3];
3909 bus_dmamap_t hdr_dmap;
3910 bus_addr_t hdr_paddr;
3911 struct sglist sg;
3912 struct {
3913 struct virtio_net_ctrl_hdr hdr __aligned(2);
3914 uint8_t pad1;
3915 uint8_t onoff;
3916 uint8_t pad2;
3917 } s;
3918 uint8_t ack;
3919 int error;
3920
3921 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
3922 if (error)
3923 goto error_out;
3924
3925 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &s,
3926 sizeof(s), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
3927 if (error)
3928 goto error_destroy_hdr;
3929
3930 MPASS(sc->vtnet_flags & VTNET_FLAG_CTRL_RX);
3931
3932 s.hdr.class = VIRTIO_NET_CTRL_RX;
3933 s.hdr.cmd = cmd;
3934 s.onoff = on;
3935 ack = VIRTIO_NET_ERR;
3936 bus_dmamap_sync(sc->vtnet_hdr_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
3937
3938 sglist_init(&sg, nitems(segs), segs);
3939 error |= sglist_append_phys(&sg, hdr_paddr,
3940 sizeof(struct virtio_net_ctrl_hdr));
3941 error |= sglist_append_phys(&sg,
3942 hdr_paddr + ((uintptr_t)&s.onoff - (uintptr_t)&s),
3943 sizeof(uint8_t));
3944 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
3945
3946 if (error == 0)
3947 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
3948 if (error == 0)
3949 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
3950
3951 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
3952 error_destroy_hdr:
3953 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
3954 error_out:
3955 return (error);
3956 }
3957
3958 static int
vtnet_set_promisc(struct vtnet_softc * sc,bool on)3959 vtnet_set_promisc(struct vtnet_softc *sc, bool on)
3960 {
3961 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_PROMISC, on));
3962 }
3963
3964 static int
vtnet_set_allmulti(struct vtnet_softc * sc,bool on)3965 vtnet_set_allmulti(struct vtnet_softc *sc, bool on)
3966 {
3967 return (vtnet_ctrl_rx_cmd(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, on));
3968 }
3969
3970 static void
vtnet_rx_filter(struct vtnet_softc * sc)3971 vtnet_rx_filter(struct vtnet_softc *sc)
3972 {
3973 device_t dev;
3974 if_t ifp;
3975
3976 dev = sc->vtnet_dev;
3977 ifp = sc->vtnet_ifp;
3978
3979 VTNET_CORE_LOCK_ASSERT(sc);
3980
3981 if (vtnet_set_promisc(sc, if_getflags(ifp) & IFF_PROMISC) != 0) {
3982 device_printf(dev, "cannot %s promiscuous mode\n",
3983 if_getflags(ifp) & IFF_PROMISC ? "enable" : "disable");
3984 }
3985
3986 if (vtnet_set_allmulti(sc, if_getflags(ifp) & IFF_ALLMULTI) != 0) {
3987 device_printf(dev, "cannot %s all-multicast mode\n",
3988 if_getflags(ifp) & IFF_ALLMULTI ? "enable" : "disable");
3989 }
3990 }
3991
3992 static u_int
vtnet_copy_ifaddr(void * arg,struct sockaddr_dl * sdl,u_int ucnt)3993 vtnet_copy_ifaddr(void *arg, struct sockaddr_dl *sdl, u_int ucnt)
3994 {
3995 struct vtnet_softc *sc = arg;
3996
3997 if (memcmp(LLADDR(sdl), sc->vtnet_hwaddr, ETHER_ADDR_LEN) == 0)
3998 return (0);
3999
4000 if (ucnt < VTNET_MAX_MAC_ENTRIES)
4001 bcopy(LLADDR(sdl),
4002 &sc->vtnet_mac_filter->vmf_unicast.macs[ucnt],
4003 ETHER_ADDR_LEN);
4004
4005 return (1);
4006 }
4007
4008 static u_int
vtnet_copy_maddr(void * arg,struct sockaddr_dl * sdl,u_int mcnt)4009 vtnet_copy_maddr(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
4010 {
4011 struct vtnet_mac_filter *filter = arg;
4012
4013 if (mcnt < VTNET_MAX_MAC_ENTRIES)
4014 bcopy(LLADDR(sdl), &filter->vmf_multicast.macs[mcnt],
4015 ETHER_ADDR_LEN);
4016
4017 return (1);
4018 }
4019
4020 static void
vtnet_rx_filter_mac(struct vtnet_softc * sc)4021 vtnet_rx_filter_mac(struct vtnet_softc *sc)
4022 {
4023 struct virtio_net_ctrl_hdr hdr __aligned(2);
4024 struct vtnet_mac_filter *filter;
4025 struct sglist_seg segs[4];
4026 bus_dmamap_t filter_dmap;
4027 bus_addr_t filter_paddr;
4028 bus_dmamap_t hdr_dmap;
4029 bus_addr_t hdr_paddr;
4030 struct sglist sg;
4031 if_t ifp;
4032 bool promisc, allmulti;
4033 u_int ucnt, mcnt;
4034 int error;
4035 uint8_t ack;
4036
4037 ifp = sc->vtnet_ifp;
4038 filter = sc->vtnet_mac_filter;
4039 error = 0;
4040
4041 MPASS(sc->vtnet_flags & VTNET_FLAG_CTRL_RX);
4042 VTNET_CORE_LOCK_ASSERT(sc);
4043
4044 /* Unicast MAC addresses: */
4045 ucnt = if_foreach_lladdr(ifp, vtnet_copy_ifaddr, sc);
4046 promisc = (ucnt > VTNET_MAX_MAC_ENTRIES);
4047
4048 if (promisc) {
4049 ucnt = 0;
4050 if_printf(ifp, "more than %d MAC addresses assigned, "
4051 "falling back to promiscuous mode\n",
4052 VTNET_MAX_MAC_ENTRIES);
4053 }
4054
4055 /* Multicast MAC addresses: */
4056 mcnt = if_foreach_llmaddr(ifp, vtnet_copy_maddr, filter);
4057 allmulti = (mcnt > VTNET_MAX_MAC_ENTRIES);
4058
4059 if (allmulti) {
4060 mcnt = 0;
4061 if_printf(ifp, "more than %d multicast MAC addresses "
4062 "assigned, falling back to all-multicast mode\n",
4063 VTNET_MAX_MAC_ENTRIES);
4064 }
4065
4066 if (promisc && allmulti)
4067 goto out;
4068
4069 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
4070 if (error)
4071 goto out_error;
4072
4073 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &hdr,
4074 sizeof(hdr), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
4075 if (error)
4076 goto out_destroy_hdr;
4077
4078 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &filter_dmap);
4079 if (error)
4080 goto out_unload_hdr;
4081
4082 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, filter,
4083 sizeof(*filter), vtnet_load_callback, &filter_paddr,
4084 BUS_DMA_NOWAIT);
4085 if (error)
4086 goto out_destroy_filter;
4087
4088 filter->vmf_unicast.nentries = vtnet_gtoh32(sc, ucnt);
4089 filter->vmf_multicast.nentries = vtnet_gtoh32(sc, mcnt);
4090
4091 hdr.class = VIRTIO_NET_CTRL_MAC;
4092 hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
4093 ack = VIRTIO_NET_ERR;
4094
4095 sglist_init(&sg, nitems(segs), segs);
4096 error |= sglist_append_phys(&sg, hdr_paddr,
4097 sizeof(struct virtio_net_ctrl_hdr));
4098 error |= sglist_append_phys(&sg,
4099 filter_paddr + ((uintptr_t)&filter->vmf_unicast -
4100 (uintptr_t)filter),
4101 sizeof(uint32_t) + ucnt * ETHER_ADDR_LEN);
4102 error |= sglist_append_phys(&sg,
4103 filter_paddr + ((uintptr_t)&filter->vmf_multicast -
4104 (uintptr_t)filter),
4105 sizeof(uint32_t) + mcnt * ETHER_ADDR_LEN);
4106 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
4107
4108 if (error == 0)
4109 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
4110 if (error == 0)
4111 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
4112
4113 bus_dmamap_unload(sc->vtnet_hdr_dmat, filter_dmap);
4114 out_destroy_filter:
4115 bus_dmamap_destroy(sc->vtnet_hdr_dmat, filter_dmap);
4116 out_unload_hdr:
4117 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
4118 out_destroy_hdr:
4119 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
4120 out_error:
4121 if (error != 0)
4122 if_printf(ifp, "error setting host MAC filter table\n");
4123 out:
4124 if (promisc && vtnet_set_promisc(sc, true) != 0)
4125 if_printf(ifp, "cannot enable promiscuous mode\n");
4126 if (allmulti && vtnet_set_allmulti(sc, true) != 0)
4127 if_printf(ifp, "cannot enable all-multicast mode\n");
4128 }
4129
4130 static int
vtnet_exec_vlan_filter(struct vtnet_softc * sc,int add,uint16_t tag)4131 vtnet_exec_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
4132 {
4133 struct sglist_seg segs[3];
4134 bus_dmamap_t hdr_dmap;
4135 bus_addr_t hdr_paddr;
4136 struct sglist sg;
4137 struct {
4138 struct virtio_net_ctrl_hdr hdr __aligned(2);
4139 uint8_t pad1;
4140 uint16_t tag __aligned(2);
4141 uint8_t pad2;
4142 } s;
4143 uint8_t ack;
4144 int error;
4145
4146 error = bus_dmamap_create(sc->vtnet_hdr_dmat, 0, &hdr_dmap);
4147 if (error)
4148 goto error_out;
4149
4150 error = bus_dmamap_load(sc->vtnet_hdr_dmat, hdr_dmap, &s,
4151 sizeof(s), vtnet_load_callback, &hdr_paddr, BUS_DMA_NOWAIT);
4152 if (error)
4153 goto error_destroy_hdr;
4154
4155 MPASS(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER);
4156
4157 s.hdr.class = VIRTIO_NET_CTRL_VLAN;
4158 s.hdr.cmd = add ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
4159 s.tag = vtnet_gtoh16(sc, tag);
4160 ack = VIRTIO_NET_ERR;
4161 bus_dmamap_sync(sc->vtnet_hdr_dmat, hdr_dmap, BUS_DMASYNC_PREWRITE);
4162
4163 sglist_init(&sg, nitems(segs), segs);
4164 error |= sglist_append_phys(&sg, hdr_paddr,
4165 sizeof(struct virtio_net_ctrl_hdr));
4166 error |= sglist_append_phys(&sg,
4167 hdr_paddr + ((uintptr_t)&s.tag - (uintptr_t)&s),
4168 sizeof(uint16_t));
4169 MPASS(error == 0 && sg.sg_nseg == nitems(segs) - 1);
4170
4171 if (error == 0)
4172 error = vtnet_exec_ctrl_cmd(sc, &ack, &sg, sg.sg_nseg, 1);
4173 if (error == 0)
4174 error = (ack == VIRTIO_NET_OK ? 0 : EIO);
4175
4176 bus_dmamap_unload(sc->vtnet_hdr_dmat, hdr_dmap);
4177 error_destroy_hdr:
4178 bus_dmamap_destroy(sc->vtnet_hdr_dmat, hdr_dmap);
4179 error_out:
4180 return (error);
4181 }
4182
4183 static void
vtnet_rx_filter_vlan(struct vtnet_softc * sc)4184 vtnet_rx_filter_vlan(struct vtnet_softc *sc)
4185 {
4186 int i, bit;
4187 uint32_t w;
4188 uint16_t tag;
4189
4190 MPASS(sc->vtnet_flags & VTNET_FLAG_VLAN_FILTER);
4191 VTNET_CORE_LOCK_ASSERT(sc);
4192
4193 /* Enable the filter for each configured VLAN. */
4194 for (i = 0; i < VTNET_VLAN_FILTER_NWORDS; i++) {
4195 w = sc->vtnet_vlan_filter[i];
4196
4197 while ((bit = ffs(w) - 1) != -1) {
4198 w &= ~(1 << bit);
4199 tag = sizeof(w) * CHAR_BIT * i + bit;
4200
4201 if (vtnet_exec_vlan_filter(sc, 1, tag) != 0) {
4202 device_printf(sc->vtnet_dev,
4203 "cannot enable VLAN %d filter\n", tag);
4204 }
4205 }
4206 }
4207 }
4208
4209 static void
vtnet_update_vlan_filter(struct vtnet_softc * sc,int add,uint16_t tag)4210 vtnet_update_vlan_filter(struct vtnet_softc *sc, int add, uint16_t tag)
4211 {
4212 if_t ifp;
4213 int idx, bit;
4214
4215 ifp = sc->vtnet_ifp;
4216 idx = (tag >> 5) & 0x7F;
4217 bit = tag & 0x1F;
4218
4219 if (tag == 0 || tag > 4095)
4220 return;
4221
4222 VTNET_CORE_LOCK(sc);
4223
4224 if (add)
4225 sc->vtnet_vlan_filter[idx] |= (1 << bit);
4226 else
4227 sc->vtnet_vlan_filter[idx] &= ~(1 << bit);
4228
4229 if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER &&
4230 if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4231 vtnet_exec_vlan_filter(sc, add, tag) != 0) {
4232 device_printf(sc->vtnet_dev,
4233 "cannot %s VLAN %d %s the host filter table\n",
4234 add ? "add" : "remove", tag, add ? "to" : "from");
4235 }
4236
4237 VTNET_CORE_UNLOCK(sc);
4238 }
4239
4240 static void
vtnet_register_vlan(void * arg,if_t ifp,uint16_t tag)4241 vtnet_register_vlan(void *arg, if_t ifp, uint16_t tag)
4242 {
4243
4244 if (if_getsoftc(ifp) != arg)
4245 return;
4246
4247 vtnet_update_vlan_filter(arg, 1, tag);
4248 }
4249
4250 static void
vtnet_unregister_vlan(void * arg,if_t ifp,uint16_t tag)4251 vtnet_unregister_vlan(void *arg, if_t ifp, uint16_t tag)
4252 {
4253
4254 if (if_getsoftc(ifp) != arg)
4255 return;
4256
4257 vtnet_update_vlan_filter(arg, 0, tag);
4258 }
4259
4260 static void
vtnet_update_speed_duplex(struct vtnet_softc * sc)4261 vtnet_update_speed_duplex(struct vtnet_softc *sc)
4262 {
4263 if_t ifp;
4264 uint32_t speed;
4265
4266 ifp = sc->vtnet_ifp;
4267
4268 if ((sc->vtnet_features & VIRTIO_NET_F_SPEED_DUPLEX) == 0)
4269 return;
4270
4271 /* BMV: Ignore duplex. */
4272 speed = virtio_read_dev_config_4(sc->vtnet_dev,
4273 offsetof(struct virtio_net_config, speed));
4274 if (speed != UINT32_MAX)
4275 if_setbaudrate(ifp, IF_Mbps(speed));
4276 }
4277
4278 static int
vtnet_is_link_up(struct vtnet_softc * sc)4279 vtnet_is_link_up(struct vtnet_softc *sc)
4280 {
4281 uint16_t status;
4282
4283 if ((sc->vtnet_features & VIRTIO_NET_F_STATUS) == 0)
4284 return (1);
4285
4286 status = virtio_read_dev_config_2(sc->vtnet_dev,
4287 offsetof(struct virtio_net_config, status));
4288
4289 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
4290 }
4291
4292 static void
vtnet_update_link_status(struct vtnet_softc * sc)4293 vtnet_update_link_status(struct vtnet_softc *sc)
4294 {
4295 if_t ifp;
4296 int link;
4297
4298 ifp = sc->vtnet_ifp;
4299 VTNET_CORE_LOCK_ASSERT(sc);
4300 link = vtnet_is_link_up(sc);
4301
4302 /* Notify if the link status has changed. */
4303 if (link != 0 && sc->vtnet_link_active == 0) {
4304 vtnet_update_speed_duplex(sc);
4305 sc->vtnet_link_active = 1;
4306 if_link_state_change(ifp, LINK_STATE_UP);
4307 } else if (link == 0 && sc->vtnet_link_active != 0) {
4308 sc->vtnet_link_active = 0;
4309 if_link_state_change(ifp, LINK_STATE_DOWN);
4310 }
4311 }
4312
4313 static int
vtnet_ifmedia_upd(if_t ifp __unused)4314 vtnet_ifmedia_upd(if_t ifp __unused)
4315 {
4316 return (EOPNOTSUPP);
4317 }
4318
4319 static void
vtnet_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)4320 vtnet_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
4321 {
4322 struct vtnet_softc *sc;
4323
4324 sc = if_getsoftc(ifp);
4325
4326 ifmr->ifm_status = IFM_AVALID;
4327 ifmr->ifm_active = IFM_ETHER;
4328
4329 VTNET_CORE_LOCK(sc);
4330 if (vtnet_is_link_up(sc) != 0) {
4331 ifmr->ifm_status |= IFM_ACTIVE;
4332 ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
4333 } else
4334 ifmr->ifm_active |= IFM_NONE;
4335 VTNET_CORE_UNLOCK(sc);
4336 }
4337
4338 static void
vtnet_get_macaddr(struct vtnet_softc * sc)4339 vtnet_get_macaddr(struct vtnet_softc *sc)
4340 {
4341
4342 if (sc->vtnet_flags & VTNET_FLAG_MAC) {
4343 virtio_read_device_config_array(sc->vtnet_dev,
4344 offsetof(struct virtio_net_config, mac),
4345 &sc->vtnet_hwaddr[0], sizeof(uint8_t), ETHER_ADDR_LEN);
4346 } else {
4347 /* Generate a random locally administered unicast address. */
4348 sc->vtnet_hwaddr[0] = 0xB2;
4349 arc4rand(&sc->vtnet_hwaddr[1], ETHER_ADDR_LEN - 1, 0);
4350 }
4351 }
4352
4353 static void
vtnet_set_macaddr(struct vtnet_softc * sc)4354 vtnet_set_macaddr(struct vtnet_softc *sc)
4355 {
4356 device_t dev;
4357 int error;
4358
4359 dev = sc->vtnet_dev;
4360
4361 if (sc->vtnet_flags & VTNET_FLAG_CTRL_MAC) {
4362 error = vtnet_ctrl_mac_cmd(sc, sc->vtnet_hwaddr);
4363 if (error)
4364 device_printf(dev, "unable to set MAC address\n");
4365 return;
4366 }
4367
4368 /* MAC in config is read-only in modern VirtIO. */
4369 if (!vtnet_modern(sc) && sc->vtnet_flags & VTNET_FLAG_MAC) {
4370 for (int i = 0; i < ETHER_ADDR_LEN; i++) {
4371 virtio_write_dev_config_1(dev,
4372 offsetof(struct virtio_net_config, mac) + i,
4373 sc->vtnet_hwaddr[i]);
4374 }
4375 }
4376 }
4377
4378 static void
vtnet_attached_set_macaddr(struct vtnet_softc * sc)4379 vtnet_attached_set_macaddr(struct vtnet_softc *sc)
4380 {
4381
4382 /* Assign MAC address if it was generated. */
4383 if ((sc->vtnet_flags & VTNET_FLAG_MAC) == 0)
4384 vtnet_set_macaddr(sc);
4385 }
4386
4387 static void
vtnet_vlan_tag_remove(struct mbuf * m)4388 vtnet_vlan_tag_remove(struct mbuf *m)
4389 {
4390 struct ether_vlan_header *evh;
4391
4392 evh = mtod(m, struct ether_vlan_header *);
4393 m->m_pkthdr.ether_vtag = ntohs(evh->evl_tag);
4394 m->m_flags |= M_VLANTAG;
4395
4396 /* Strip the 802.1Q header. */
4397 bcopy((char *) evh, (char *) evh + ETHER_VLAN_ENCAP_LEN,
4398 ETHER_HDR_LEN - ETHER_TYPE_LEN);
4399 m_adj(m, ETHER_VLAN_ENCAP_LEN);
4400 }
4401
4402 static void
vtnet_set_rx_process_limit(struct vtnet_softc * sc)4403 vtnet_set_rx_process_limit(struct vtnet_softc *sc)
4404 {
4405 int limit;
4406
4407 limit = vtnet_tunable_int(sc, "rx_process_limit",
4408 vtnet_rx_process_limit);
4409 if (limit < 0)
4410 limit = INT_MAX;
4411 sc->vtnet_rx_process_limit = limit;
4412 }
4413
4414 static void
vtnet_setup_rxq_sysctl(struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child,struct vtnet_rxq * rxq)4415 vtnet_setup_rxq_sysctl(struct sysctl_ctx_list *ctx,
4416 struct sysctl_oid_list *child, struct vtnet_rxq *rxq)
4417 {
4418 struct sysctl_oid *node;
4419 struct sysctl_oid_list *list;
4420 struct vtnet_rxq_stats *stats;
4421 char namebuf[16];
4422
4423 snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vtnrx_id);
4424 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
4425 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Receive Queue");
4426 list = SYSCTL_CHILDREN(node);
4427
4428 stats = &rxq->vtnrx_stats;
4429
4430 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ipackets",
4431 CTLFLAG_RD | CTLFLAG_STATS,
4432 &stats->vrxs_ipackets, "Receive packets");
4433 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ibytes",
4434 CTLFLAG_RD | CTLFLAG_STATS,
4435 &stats->vrxs_ibytes, "Receive bytes");
4436 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "iqdrops",
4437 CTLFLAG_RD | CTLFLAG_STATS,
4438 &stats->vrxs_iqdrops, "Receive drops");
4439 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "ierrors",
4440 CTLFLAG_RD | CTLFLAG_STATS,
4441 &stats->vrxs_ierrors, "Receive errors");
4442 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum",
4443 CTLFLAG_RD | CTLFLAG_STATS,
4444 &stats->vrxs_csum, "Receive checksum offloaded");
4445 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum_failed",
4446 CTLFLAG_RD | CTLFLAG_STATS,
4447 &stats->vrxs_csum_failed, "Receive checksum offload failed");
4448 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "host_lro",
4449 CTLFLAG_RD | CTLFLAG_STATS,
4450 &stats->vrxs_host_lro, "Receive host segmentation offloaded");
4451 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled",
4452 CTLFLAG_RD | CTLFLAG_STATS,
4453 &stats->vrxs_rescheduled,
4454 "Receive interrupt handler rescheduled");
4455 }
4456
4457 static void
vtnet_setup_txq_sysctl(struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child,struct vtnet_txq * txq)4458 vtnet_setup_txq_sysctl(struct sysctl_ctx_list *ctx,
4459 struct sysctl_oid_list *child, struct vtnet_txq *txq)
4460 {
4461 struct sysctl_oid *node;
4462 struct sysctl_oid_list *list;
4463 struct vtnet_txq_stats *stats;
4464 char namebuf[16];
4465
4466 snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vtntx_id);
4467 node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
4468 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Transmit Queue");
4469 list = SYSCTL_CHILDREN(node);
4470
4471 stats = &txq->vtntx_stats;
4472
4473 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "opackets",
4474 CTLFLAG_RD | CTLFLAG_STATS,
4475 &stats->vtxs_opackets, "Transmit packets");
4476 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "obytes",
4477 CTLFLAG_RD | CTLFLAG_STATS,
4478 &stats->vtxs_obytes, "Transmit bytes");
4479 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "omcasts",
4480 CTLFLAG_RD | CTLFLAG_STATS,
4481 &stats->vtxs_omcasts, "Transmit multicasts");
4482 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "csum",
4483 CTLFLAG_RD | CTLFLAG_STATS,
4484 &stats->vtxs_csum, "Transmit checksum offloaded");
4485 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "tso",
4486 CTLFLAG_RD | CTLFLAG_STATS,
4487 &stats->vtxs_tso, "Transmit TCP segmentation offloaded");
4488 SYSCTL_ADD_UQUAD(ctx, list, OID_AUTO, "rescheduled",
4489 CTLFLAG_RD | CTLFLAG_STATS,
4490 &stats->vtxs_rescheduled,
4491 "Transmit interrupt handler rescheduled");
4492 }
4493
4494 static void
vtnet_setup_queue_sysctl(struct vtnet_softc * sc)4495 vtnet_setup_queue_sysctl(struct vtnet_softc *sc)
4496 {
4497 device_t dev;
4498 struct sysctl_ctx_list *ctx;
4499 struct sysctl_oid *tree;
4500 struct sysctl_oid_list *child;
4501 int i;
4502
4503 dev = sc->vtnet_dev;
4504 ctx = device_get_sysctl_ctx(dev);
4505 tree = device_get_sysctl_tree(dev);
4506 child = SYSCTL_CHILDREN(tree);
4507
4508 for (i = 0; i < sc->vtnet_req_vq_pairs; i++) {
4509 vtnet_setup_rxq_sysctl(ctx, child, &sc->vtnet_rxqs[i]);
4510 vtnet_setup_txq_sysctl(ctx, child, &sc->vtnet_txqs[i]);
4511 }
4512 }
4513
4514 static int
vtnet_sysctl_rx_csum_failed(SYSCTL_HANDLER_ARGS)4515 vtnet_sysctl_rx_csum_failed(SYSCTL_HANDLER_ARGS)
4516 {
4517 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4518 struct vtnet_statistics *stats = &sc->vtnet_stats;
4519 struct vtnet_rxq_stats *rxst;
4520 int i;
4521
4522 stats->rx_csum_failed = 0;
4523 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
4524 rxst = &sc->vtnet_rxqs[i].vtnrx_stats;
4525 stats->rx_csum_failed += rxst->vrxs_csum_failed;
4526 }
4527 return (sysctl_handle_64(oidp, NULL, stats->rx_csum_failed, req));
4528 }
4529
4530 static int
vtnet_sysctl_rx_csum_offloaded(SYSCTL_HANDLER_ARGS)4531 vtnet_sysctl_rx_csum_offloaded(SYSCTL_HANDLER_ARGS)
4532 {
4533 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4534 struct vtnet_statistics *stats = &sc->vtnet_stats;
4535 struct vtnet_rxq_stats *rxst;
4536 int i;
4537
4538 stats->rx_csum_offloaded = 0;
4539 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
4540 rxst = &sc->vtnet_rxqs[i].vtnrx_stats;
4541 stats->rx_csum_offloaded += rxst->vrxs_csum;
4542 }
4543 return (sysctl_handle_64(oidp, NULL, stats->rx_csum_offloaded, req));
4544 }
4545
4546 static int
vtnet_sysctl_rx_task_rescheduled(SYSCTL_HANDLER_ARGS)4547 vtnet_sysctl_rx_task_rescheduled(SYSCTL_HANDLER_ARGS)
4548 {
4549 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4550 struct vtnet_statistics *stats = &sc->vtnet_stats;
4551 struct vtnet_rxq_stats *rxst;
4552 int i;
4553
4554 stats->rx_task_rescheduled = 0;
4555 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
4556 rxst = &sc->vtnet_rxqs[i].vtnrx_stats;
4557 stats->rx_task_rescheduled += rxst->vrxs_rescheduled;
4558 }
4559 return (sysctl_handle_64(oidp, NULL, stats->rx_task_rescheduled, req));
4560 }
4561
4562 static int
vtnet_sysctl_tx_csum_offloaded(SYSCTL_HANDLER_ARGS)4563 vtnet_sysctl_tx_csum_offloaded(SYSCTL_HANDLER_ARGS)
4564 {
4565 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4566 struct vtnet_statistics *stats = &sc->vtnet_stats;
4567 struct vtnet_txq_stats *txst;
4568 int i;
4569
4570 stats->tx_csum_offloaded = 0;
4571 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
4572 txst = &sc->vtnet_txqs[i].vtntx_stats;
4573 stats->tx_csum_offloaded += txst->vtxs_csum;
4574 }
4575 return (sysctl_handle_64(oidp, NULL, stats->tx_csum_offloaded, req));
4576 }
4577
4578 static int
vtnet_sysctl_tx_tso_offloaded(SYSCTL_HANDLER_ARGS)4579 vtnet_sysctl_tx_tso_offloaded(SYSCTL_HANDLER_ARGS)
4580 {
4581 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4582 struct vtnet_statistics *stats = &sc->vtnet_stats;
4583 struct vtnet_txq_stats *txst;
4584 int i;
4585
4586 stats->tx_tso_offloaded = 0;
4587 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
4588 txst = &sc->vtnet_txqs[i].vtntx_stats;
4589 stats->tx_tso_offloaded += txst->vtxs_tso;
4590 }
4591 return (sysctl_handle_64(oidp, NULL, stats->tx_tso_offloaded, req));
4592 }
4593
4594 static int
vtnet_sysctl_tx_task_rescheduled(SYSCTL_HANDLER_ARGS)4595 vtnet_sysctl_tx_task_rescheduled(SYSCTL_HANDLER_ARGS)
4596 {
4597 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4598 struct vtnet_statistics *stats = &sc->vtnet_stats;
4599 struct vtnet_txq_stats *txst;
4600 int i;
4601
4602 stats->tx_task_rescheduled = 0;
4603 for (i = 0; i < sc->vtnet_max_vq_pairs; i++) {
4604 txst = &sc->vtnet_txqs[i].vtntx_stats;
4605 stats->tx_task_rescheduled += txst->vtxs_rescheduled;
4606 }
4607 return (sysctl_handle_64(oidp, NULL, stats->tx_task_rescheduled, req));
4608 }
4609
4610 static void
vtnet_setup_stat_sysctl(struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child,struct vtnet_softc * sc)4611 vtnet_setup_stat_sysctl(struct sysctl_ctx_list *ctx,
4612 struct sysctl_oid_list *child, struct vtnet_softc *sc)
4613 {
4614 struct vtnet_statistics *stats;
4615 struct vtnet_rxq_stats rxaccum;
4616 struct vtnet_txq_stats txaccum;
4617
4618 vtnet_accum_stats(sc, &rxaccum, &txaccum);
4619
4620 stats = &sc->vtnet_stats;
4621 stats->rx_csum_offloaded = rxaccum.vrxs_csum;
4622 stats->rx_csum_failed = rxaccum.vrxs_csum_failed;
4623 stats->rx_task_rescheduled = rxaccum.vrxs_rescheduled;
4624 stats->tx_csum_offloaded = txaccum.vtxs_csum;
4625 stats->tx_tso_offloaded = txaccum.vtxs_tso;
4626 stats->tx_task_rescheduled = txaccum.vtxs_rescheduled;
4627
4628 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "mbuf_alloc_failed",
4629 CTLFLAG_RD | CTLFLAG_STATS, &stats->mbuf_alloc_failed,
4630 "Mbuf cluster allocation failures");
4631
4632 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_frame_too_large",
4633 CTLFLAG_RD | CTLFLAG_STATS, &stats->rx_frame_too_large,
4634 "Received frame larger than the mbuf chain");
4635 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_enq_replacement_failed",
4636 CTLFLAG_RD | CTLFLAG_STATS, &stats->rx_enq_replacement_failed,
4637 "Enqueuing the replacement receive mbuf failed");
4638 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_mergeable_failed",
4639 CTLFLAG_RD | CTLFLAG_STATS, &stats->rx_mergeable_failed,
4640 "Mergeable buffers receive failures");
4641 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ethtype",
4642 CTLFLAG_RD | CTLFLAG_STATS, &stats->rx_csum_bad_ethtype,
4643 "Received checksum offloaded buffer with unsupported "
4644 "Ethernet type");
4645 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_bad_ipproto",
4646 CTLFLAG_RD | CTLFLAG_STATS, &stats->rx_csum_bad_ipproto,
4647 "Received checksum offloaded buffer with incorrect IP protocol");
4648 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "rx_csum_inaccessible_ipproto",
4649 CTLFLAG_RD | CTLFLAG_STATS, &stats->rx_csum_inaccessible_ipproto,
4650 "Received checksum offloaded buffer with inaccessible IP protocol");
4651 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_csum_failed",
4652 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
4653 sc, 0, vtnet_sysctl_rx_csum_failed, "QU",
4654 "Received buffer checksum offload failed");
4655 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_csum_offloaded",
4656 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
4657 sc, 0, vtnet_sysctl_rx_csum_offloaded, "QU",
4658 "Received buffer checksum offload succeeded");
4659 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "rx_task_rescheduled",
4660 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
4661 sc, 0, vtnet_sysctl_rx_task_rescheduled, "QU",
4662 "Times the receive interrupt task rescheduled itself");
4663
4664 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_unknown_ethtype",
4665 CTLFLAG_RD | CTLFLAG_STATS, &stats->tx_csum_unknown_ethtype,
4666 "Aborted transmit of checksum offloaded buffer with unknown "
4667 "Ethernet type");
4668 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_csum_proto_mismatch",
4669 CTLFLAG_RD | CTLFLAG_STATS, &stats->tx_csum_proto_mismatch,
4670 "Aborted transmit of checksum offloaded buffer because mismatched "
4671 "protocols");
4672 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_not_tcp",
4673 CTLFLAG_RD | CTLFLAG_STATS, &stats->tx_tso_not_tcp,
4674 "Aborted transmit of TSO buffer with non TCP protocol");
4675 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_tso_without_csum",
4676 CTLFLAG_RD | CTLFLAG_STATS, &stats->tx_tso_without_csum,
4677 "Aborted transmit of TSO buffer without TCP checksum offload");
4678 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defragged",
4679 CTLFLAG_RD | CTLFLAG_STATS, &stats->tx_defragged,
4680 "Transmit mbufs defragged");
4681 SYSCTL_ADD_UQUAD(ctx, child, OID_AUTO, "tx_defrag_failed",
4682 CTLFLAG_RD | CTLFLAG_STATS, &stats->tx_defrag_failed,
4683 "Aborted transmit of buffer because defrag failed");
4684 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_csum_offloaded",
4685 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
4686 sc, 0, vtnet_sysctl_tx_csum_offloaded, "QU",
4687 "Offloaded checksum of transmitted buffer");
4688 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_tso_offloaded",
4689 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
4690 sc, 0, vtnet_sysctl_tx_tso_offloaded, "QU",
4691 "Segmentation offload of transmitted buffer");
4692 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "tx_task_rescheduled",
4693 CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_STATS,
4694 sc, 0, vtnet_sysctl_tx_task_rescheduled, "QU",
4695 "Times the transmit interrupt task rescheduled itself");
4696 }
4697
4698 static int
vtnet_sysctl_features(SYSCTL_HANDLER_ARGS)4699 vtnet_sysctl_features(SYSCTL_HANDLER_ARGS)
4700 {
4701 struct sbuf sb;
4702 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4703 int error;
4704
4705 sbuf_new_for_sysctl(&sb, NULL, 0, req);
4706 sbuf_printf(&sb, "%b", (uint32_t)sc->vtnet_features,
4707 VIRTIO_NET_FEATURE_BITS);
4708 error = sbuf_finish(&sb);
4709 sbuf_delete(&sb);
4710 return (error);
4711 }
4712
4713 static int
vtnet_sysctl_flags(SYSCTL_HANDLER_ARGS)4714 vtnet_sysctl_flags(SYSCTL_HANDLER_ARGS)
4715 {
4716 struct sbuf sb;
4717 struct vtnet_softc *sc = (struct vtnet_softc *)arg1;
4718 int error;
4719
4720 sbuf_new_for_sysctl(&sb, NULL, 0, req);
4721 sbuf_printf(&sb, "%b", sc->vtnet_flags, VTNET_FLAGS_BITS);
4722 error = sbuf_finish(&sb);
4723 sbuf_delete(&sb);
4724 return (error);
4725 }
4726
4727 static void
vtnet_setup_sysctl(struct vtnet_softc * sc)4728 vtnet_setup_sysctl(struct vtnet_softc *sc)
4729 {
4730 device_t dev;
4731 struct sysctl_ctx_list *ctx;
4732 struct sysctl_oid *tree;
4733 struct sysctl_oid_list *child;
4734
4735 dev = sc->vtnet_dev;
4736 ctx = device_get_sysctl_ctx(dev);
4737 tree = device_get_sysctl_tree(dev);
4738 child = SYSCTL_CHILDREN(tree);
4739
4740 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "max_vq_pairs",
4741 CTLFLAG_RD, &sc->vtnet_max_vq_pairs, 0,
4742 "Number of maximum supported virtqueue pairs");
4743 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "req_vq_pairs",
4744 CTLFLAG_RD, &sc->vtnet_req_vq_pairs, 0,
4745 "Number of requested virtqueue pairs");
4746 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "act_vq_pairs",
4747 CTLFLAG_RD, &sc->vtnet_act_vq_pairs, 0,
4748 "Number of active virtqueue pairs");
4749 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "features",
4750 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
4751 vtnet_sysctl_features, "A", "Features");
4752 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "flags",
4753 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 0,
4754 vtnet_sysctl_flags, "A", "Flags");
4755
4756 vtnet_setup_stat_sysctl(ctx, child, sc);
4757 }
4758
4759 static void
vtnet_load_tunables(struct vtnet_softc * sc)4760 vtnet_load_tunables(struct vtnet_softc *sc)
4761 {
4762
4763 sc->vtnet_lro_entry_count = vtnet_tunable_int(sc,
4764 "lro_entry_count", vtnet_lro_entry_count);
4765 if (sc->vtnet_lro_entry_count < TCP_LRO_ENTRIES)
4766 sc->vtnet_lro_entry_count = TCP_LRO_ENTRIES;
4767
4768 sc->vtnet_lro_mbufq_depth = vtnet_tunable_int(sc,
4769 "lro_mbufq_depth", vtnet_lro_mbufq_depth);
4770 }
4771
4772 static int
vtnet_rxq_enable_intr(struct vtnet_rxq * rxq)4773 vtnet_rxq_enable_intr(struct vtnet_rxq *rxq)
4774 {
4775
4776 return (virtqueue_enable_intr(rxq->vtnrx_vq));
4777 }
4778
4779 static void
vtnet_rxq_disable_intr(struct vtnet_rxq * rxq)4780 vtnet_rxq_disable_intr(struct vtnet_rxq *rxq)
4781 {
4782
4783 virtqueue_disable_intr(rxq->vtnrx_vq);
4784 }
4785
4786 static int
vtnet_txq_enable_intr(struct vtnet_txq * txq)4787 vtnet_txq_enable_intr(struct vtnet_txq *txq)
4788 {
4789 struct virtqueue *vq;
4790
4791 vq = txq->vtntx_vq;
4792
4793 if (vtnet_txq_below_threshold(txq) != 0)
4794 return (virtqueue_postpone_intr(vq, VQ_POSTPONE_LONG));
4795
4796 /*
4797 * The free count is above our threshold. Keep the Tx interrupt
4798 * disabled until the queue is fuller.
4799 */
4800 return (0);
4801 }
4802
4803 static void
vtnet_txq_disable_intr(struct vtnet_txq * txq)4804 vtnet_txq_disable_intr(struct vtnet_txq *txq)
4805 {
4806
4807 virtqueue_disable_intr(txq->vtntx_vq);
4808 }
4809
4810 static void
vtnet_enable_rx_interrupts(struct vtnet_softc * sc)4811 vtnet_enable_rx_interrupts(struct vtnet_softc *sc)
4812 {
4813 struct vtnet_rxq *rxq;
4814 int i;
4815
4816 for (i = 0; i < sc->vtnet_act_vq_pairs; i++) {
4817 rxq = &sc->vtnet_rxqs[i];
4818 if (vtnet_rxq_enable_intr(rxq) != 0)
4819 taskqueue_enqueue(rxq->vtnrx_tq, &rxq->vtnrx_intrtask);
4820 }
4821 }
4822
4823 static void
vtnet_enable_tx_interrupts(struct vtnet_softc * sc)4824 vtnet_enable_tx_interrupts(struct vtnet_softc *sc)
4825 {
4826 int i;
4827
4828 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
4829 vtnet_txq_enable_intr(&sc->vtnet_txqs[i]);
4830 }
4831
4832 static void
vtnet_enable_interrupts(struct vtnet_softc * sc)4833 vtnet_enable_interrupts(struct vtnet_softc *sc)
4834 {
4835
4836 vtnet_enable_rx_interrupts(sc);
4837 vtnet_enable_tx_interrupts(sc);
4838 }
4839
4840 static void
vtnet_disable_rx_interrupts(struct vtnet_softc * sc)4841 vtnet_disable_rx_interrupts(struct vtnet_softc *sc)
4842 {
4843 int i;
4844
4845 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
4846 vtnet_rxq_disable_intr(&sc->vtnet_rxqs[i]);
4847 }
4848
4849 static void
vtnet_disable_tx_interrupts(struct vtnet_softc * sc)4850 vtnet_disable_tx_interrupts(struct vtnet_softc *sc)
4851 {
4852 int i;
4853
4854 for (i = 0; i < sc->vtnet_max_vq_pairs; i++)
4855 vtnet_txq_disable_intr(&sc->vtnet_txqs[i]);
4856 }
4857
4858 static void
vtnet_disable_interrupts(struct vtnet_softc * sc)4859 vtnet_disable_interrupts(struct vtnet_softc *sc)
4860 {
4861
4862 vtnet_disable_rx_interrupts(sc);
4863 vtnet_disable_tx_interrupts(sc);
4864 }
4865
4866 static int
vtnet_tunable_int(struct vtnet_softc * sc,const char * knob,int def)4867 vtnet_tunable_int(struct vtnet_softc *sc, const char *knob, int def)
4868 {
4869 char path[64];
4870
4871 snprintf(path, sizeof(path),
4872 "hw.vtnet.%d.%s", device_get_unit(sc->vtnet_dev), knob);
4873 TUNABLE_INT_FETCH(path, &def);
4874
4875 return (def);
4876 }
4877
4878 #ifdef DEBUGNET
4879 static void
vtnet_debugnet_init(if_t ifp,int * nrxr,int * ncl,int * clsize)4880 vtnet_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
4881 {
4882 struct vtnet_softc *sc;
4883
4884 sc = if_getsoftc(ifp);
4885
4886 VTNET_CORE_LOCK(sc);
4887 *nrxr = sc->vtnet_req_vq_pairs;
4888 *ncl = DEBUGNET_MAX_IN_FLIGHT;
4889 *clsize = sc->vtnet_rx_clustersz;
4890 VTNET_CORE_UNLOCK(sc);
4891 }
4892
4893 static void
vtnet_debugnet_event(if_t ifp __unused,enum debugnet_ev event)4894 vtnet_debugnet_event(if_t ifp __unused, enum debugnet_ev event)
4895 {
4896 struct vtnet_softc *sc;
4897 static bool sw_lro_enabled = false;
4898
4899 /*
4900 * Disable software LRO, since it would require entering the network
4901 * epoch when calling vtnet_txq_eof() in vtnet_debugnet_poll().
4902 */
4903 sc = if_getsoftc(ifp);
4904 switch (event) {
4905 case DEBUGNET_START:
4906 sw_lro_enabled = (sc->vtnet_flags & VTNET_FLAG_SW_LRO) != 0;
4907 if (sw_lro_enabled)
4908 sc->vtnet_flags &= ~VTNET_FLAG_SW_LRO;
4909 break;
4910 case DEBUGNET_END:
4911 if (sw_lro_enabled)
4912 sc->vtnet_flags |= VTNET_FLAG_SW_LRO;
4913 break;
4914 }
4915 }
4916
4917 static int
vtnet_debugnet_transmit(if_t ifp,struct mbuf * m)4918 vtnet_debugnet_transmit(if_t ifp, struct mbuf *m)
4919 {
4920 struct vtnet_softc *sc;
4921 struct vtnet_txq *txq;
4922 int error;
4923
4924 sc = if_getsoftc(ifp);
4925 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4926 IFF_DRV_RUNNING)
4927 return (EBUSY);
4928
4929 txq = &sc->vtnet_txqs[0];
4930 error = vtnet_txq_encap(txq, &m, M_NOWAIT | M_USE_RESERVE);
4931 if (error == 0)
4932 (void)vtnet_txq_notify(txq);
4933 return (error);
4934 }
4935
4936 static int
vtnet_debugnet_poll(if_t ifp,int count)4937 vtnet_debugnet_poll(if_t ifp, int count)
4938 {
4939 struct vtnet_softc *sc;
4940 int i;
4941
4942 sc = if_getsoftc(ifp);
4943 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4944 IFF_DRV_RUNNING)
4945 return (EBUSY);
4946
4947 (void)vtnet_txq_eof(&sc->vtnet_txqs[0]);
4948 for (i = 0; i < sc->vtnet_act_vq_pairs; i++)
4949 (void)vtnet_rxq_eof(&sc->vtnet_rxqs[i]);
4950 return (0);
4951 }
4952 #endif /* DEBUGNET */
4953