xref: /freebsd/sys/dev/virtio/network/if_vtnetvar.h (revision bdafb02fcb88389fd1ab684cfe734cb429d35618)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29  */
30 
31 #ifndef _IF_VTNETVAR_H
32 #define _IF_VTNETVAR_H
33 
34 struct vtnet_softc;
35 
36 struct vtnet_statistics {
37 	uint64_t	mbuf_alloc_failed;
38 
39 	uint64_t	rx_frame_too_large;
40 	uint64_t	rx_enq_replacement_failed;
41 	uint64_t	rx_mergeable_failed;
42 	uint64_t	rx_csum_bad_ethtype;
43 	uint64_t	rx_csum_bad_ipproto;
44 	uint64_t	rx_csum_bad_offset;
45 	uint64_t	rx_csum_bad_proto;
46 	uint64_t	tx_csum_bad_ethtype;
47 	uint64_t	tx_tso_bad_ethtype;
48 	uint64_t	tx_tso_not_tcp;
49 	uint64_t	tx_defragged;
50 	uint64_t	tx_defrag_failed;
51 
52 	/*
53 	 * These are accumulated from each Rx/Tx queue.
54 	 */
55 	uint64_t	rx_csum_failed;
56 	uint64_t	rx_csum_offloaded;
57 	uint64_t	rx_task_rescheduled;
58 	uint64_t	tx_csum_offloaded;
59 	uint64_t	tx_tso_offloaded;
60 	uint64_t	tx_task_rescheduled;
61 };
62 
63 struct vtnet_rxq_stats {
64 	uint64_t	vrxs_ipackets;	/* if_ipackets */
65 	uint64_t	vrxs_ibytes;	/* if_ibytes */
66 	uint64_t	vrxs_iqdrops;	/* if_iqdrops */
67 	uint64_t	vrxs_ierrors;	/* if_ierrors */
68 	uint64_t	vrxs_csum;
69 	uint64_t	vrxs_csum_failed;
70 	uint64_t	vrxs_rescheduled;
71 };
72 
73 struct vtnet_rxq {
74 	struct mtx		 vtnrx_mtx;
75 	struct vtnet_softc	*vtnrx_sc;
76 	struct virtqueue	*vtnrx_vq;
77 	struct sglist		*vtnrx_sg;
78 	int			 vtnrx_id;
79 	struct vtnet_rxq_stats	 vtnrx_stats;
80 	struct taskqueue	*vtnrx_tq;
81 	struct task		 vtnrx_intrtask;
82 	char			 vtnrx_name[16];
83 } __aligned(CACHE_LINE_SIZE);
84 
85 #define VTNET_RXQ_LOCK(_rxq)	mtx_lock(&(_rxq)->vtnrx_mtx)
86 #define VTNET_RXQ_UNLOCK(_rxq)	mtx_unlock(&(_rxq)->vtnrx_mtx)
87 #define VTNET_RXQ_LOCK_ASSERT(_rxq)		\
88     mtx_assert(&(_rxq)->vtnrx_mtx, MA_OWNED)
89 #define VTNET_RXQ_LOCK_ASSERT_NOTOWNED(_rxq)	\
90     mtx_assert(&(_rxq)->vtnrx_mtx, MA_NOTOWNED)
91 
92 struct vtnet_txq_stats {
93 	uint64_t vtxs_opackets;	/* if_opackets */
94 	uint64_t vtxs_obytes;	/* if_obytes */
95 	uint64_t vtxs_omcasts;	/* if_omcasts */
96 	uint64_t vtxs_csum;
97 	uint64_t vtxs_tso;
98 	uint64_t vtxs_rescheduled;
99 };
100 
101 struct vtnet_txq {
102 	struct mtx		 vtntx_mtx;
103 	struct vtnet_softc	*vtntx_sc;
104 	struct virtqueue	*vtntx_vq;
105 	struct sglist		*vtntx_sg;
106 #ifndef VTNET_LEGACY_TX
107 	struct buf_ring		*vtntx_br;
108 #endif
109 	int			 vtntx_id;
110 	int			 vtntx_watchdog;
111 	struct vtnet_txq_stats	 vtntx_stats;
112 	struct taskqueue	*vtntx_tq;
113 	struct task		 vtntx_intrtask;
114 #ifndef VTNET_LEGACY_TX
115 	struct task		 vtntx_defrtask;
116 #endif
117 	char			 vtntx_name[16];
118 } __aligned(CACHE_LINE_SIZE);
119 
120 #define VTNET_TXQ_LOCK(_txq)	mtx_lock(&(_txq)->vtntx_mtx)
121 #define VTNET_TXQ_TRYLOCK(_txq)	mtx_trylock(&(_txq)->vtntx_mtx)
122 #define VTNET_TXQ_UNLOCK(_txq)	mtx_unlock(&(_txq)->vtntx_mtx)
123 #define VTNET_TXQ_LOCK_ASSERT(_txq)		\
124     mtx_assert(&(_txq)->vtntx_mtx, MA_OWNED)
125 #define VTNET_TXQ_LOCK_ASSERT_NOTOWNED(_txq)	\
126     mtx_assert(&(_txq)->vtntx_mtx, MA_NOTOWNED)
127 
128 struct vtnet_softc {
129 	device_t		 vtnet_dev;
130 	struct ifnet		*vtnet_ifp;
131 	struct vtnet_rxq	*vtnet_rxqs;
132 	struct vtnet_txq	*vtnet_txqs;
133 
134 	uint32_t		 vtnet_flags;
135 #define VTNET_FLAG_SUSPENDED	 0x0001
136 #define VTNET_FLAG_MAC		 0x0002
137 #define VTNET_FLAG_CTRL_VQ	 0x0004
138 #define VTNET_FLAG_CTRL_RX	 0x0008
139 #define VTNET_FLAG_CTRL_MAC	 0x0010
140 #define VTNET_FLAG_VLAN_FILTER	 0x0020
141 #define VTNET_FLAG_TSO_ECN	 0x0040
142 #define VTNET_FLAG_MRG_RXBUFS	 0x0080
143 #define VTNET_FLAG_LRO_NOMRG	 0x0100
144 #define VTNET_FLAG_MULTIQ	 0x0200
145 #define VTNET_FLAG_INDIRECT	 0x0400
146 #define VTNET_FLAG_EVENT_IDX	 0x0800
147 
148 	int			 vtnet_link_active;
149 	int			 vtnet_hdr_size;
150 	int			 vtnet_rx_process_limit;
151 	int			 vtnet_rx_nsegs;
152 	int			 vtnet_rx_nmbufs;
153 	int			 vtnet_rx_clsize;
154 	int			 vtnet_rx_new_clsize;
155 	int			 vtnet_tx_intr_thresh;
156 	int			 vtnet_tx_nsegs;
157 	int			 vtnet_if_flags;
158 	int			 vtnet_act_vq_pairs;
159 	int			 vtnet_max_vq_pairs;
160 	int			 vtnet_requested_vq_pairs;
161 
162 	struct virtqueue	*vtnet_ctrl_vq;
163 	struct vtnet_mac_filter	*vtnet_mac_filter;
164 	uint32_t		*vtnet_vlan_filter;
165 
166 	uint64_t		 vtnet_features;
167 	struct vtnet_statistics	 vtnet_stats;
168 	struct callout		 vtnet_tick_ch;
169 	struct ifmedia		 vtnet_media;
170 	eventhandler_tag	 vtnet_vlan_attach;
171 	eventhandler_tag	 vtnet_vlan_detach;
172 
173 	struct mtx		 vtnet_mtx;
174 	char			 vtnet_mtx_name[16];
175 	char			 vtnet_hwaddr[ETHER_ADDR_LEN];
176 };
177 
178 /*
179  * Maximum number of queue pairs we will autoconfigure to.
180  */
181 #define VTNET_MAX_QUEUE_PAIRS	8
182 
183 /*
184  * Additional completed entries can appear in a virtqueue before we can
185  * reenable interrupts. Number of times to retry before scheduling the
186  * taskqueue to process the completed entries.
187  */
188 #define VTNET_INTR_DISABLE_RETRIES	4
189 
190 /*
191  * Similarly, additional completed entries can appear in a virtqueue
192  * between when lasted checked and before notifying the host. Number
193  * of times to retry before scheduling the taskqueue to process the
194  * queue.
195  */
196 #define VTNET_NOTIFY_RETRIES		4
197 
198 /*
199  * Fake the media type. The host does not provide us with any real media
200  * information.
201  */
202 #define VTNET_MEDIATYPE		 (IFM_ETHER | IFM_10G_T | IFM_FDX)
203 
204 /*
205  * Number of words to allocate for the VLAN shadow table. There is one
206  * bit for each VLAN.
207  */
208 #define VTNET_VLAN_FILTER_NWORDS	(4096 / 32)
209 
210 /*
211  * When mergeable buffers are not negotiated, the vtnet_rx_header structure
212  * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
213  * both keep the VirtIO header and the data non-contiguous and to keep the
214  * frame's payload 4 byte aligned.
215  *
216  * When mergeable buffers are negotiated, the host puts the VirtIO header in
217  * the beginning of the first mbuf's data.
218  */
219 #define VTNET_RX_HEADER_PAD	4
220 struct vtnet_rx_header {
221 	struct virtio_net_hdr	vrh_hdr;
222 	char			vrh_pad[VTNET_RX_HEADER_PAD];
223 } __packed;
224 
225 /*
226  * For each outgoing frame, the vtnet_tx_header below is allocated from
227  * the vtnet_tx_header_zone.
228  */
229 struct vtnet_tx_header {
230 	union {
231 		struct virtio_net_hdr		hdr;
232 		struct virtio_net_hdr_mrg_rxbuf	mhdr;
233 	} vth_uhdr;
234 
235 	struct mbuf *vth_mbuf;
236 };
237 
238 /*
239  * The VirtIO specification does not place a limit on the number of MAC
240  * addresses the guest driver may request to be filtered. In practice,
241  * the host is constrained by available resources. To simplify this driver,
242  * impose a reasonably high limit of MAC addresses we will filter before
243  * falling back to promiscuous or all-multicast modes.
244  */
245 #define VTNET_MAX_MAC_ENTRIES	128
246 
247 struct vtnet_mac_table {
248 	uint32_t	nentries;
249 	uint8_t		macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
250 } __packed;
251 
252 struct vtnet_mac_filter {
253 	struct vtnet_mac_table	vmf_unicast;
254 	uint32_t		vmf_pad; /* Make tables non-contiguous. */
255 	struct vtnet_mac_table	vmf_multicast;
256 };
257 
258 /*
259  * The MAC filter table is malloc(9)'d when needed. Ensure it will
260  * always fit in one segment.
261  */
262 CTASSERT(sizeof(struct vtnet_mac_filter) <= PAGE_SIZE);
263 
264 #define VTNET_TX_TIMEOUT	5
265 #define VTNET_CSUM_OFFLOAD	(CSUM_TCP | CSUM_UDP | CSUM_SCTP)
266 #define VTNET_CSUM_OFFLOAD_IPV6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
267 
268 #define VTNET_CSUM_ALL_OFFLOAD	\
269     (VTNET_CSUM_OFFLOAD | VTNET_CSUM_OFFLOAD_IPV6 | CSUM_TSO)
270 
271 /* Features desired/implemented by this driver. */
272 #define VTNET_FEATURES \
273     (VIRTIO_NET_F_MAC			| \
274      VIRTIO_NET_F_STATUS		| \
275      VIRTIO_NET_F_CTRL_VQ		| \
276      VIRTIO_NET_F_CTRL_RX		| \
277      VIRTIO_NET_F_CTRL_MAC_ADDR		| \
278      VIRTIO_NET_F_CTRL_VLAN		| \
279      VIRTIO_NET_F_CSUM			| \
280      VIRTIO_NET_F_GSO			| \
281      VIRTIO_NET_F_HOST_TSO4		| \
282      VIRTIO_NET_F_HOST_TSO6		| \
283      VIRTIO_NET_F_HOST_ECN		| \
284      VIRTIO_NET_F_GUEST_CSUM		| \
285      VIRTIO_NET_F_GUEST_TSO4		| \
286      VIRTIO_NET_F_GUEST_TSO6		| \
287      VIRTIO_NET_F_GUEST_ECN		| \
288      VIRTIO_NET_F_MRG_RXBUF		| \
289      VIRTIO_NET_F_MQ			| \
290      VIRTIO_RING_F_EVENT_IDX		| \
291      VIRTIO_RING_F_INDIRECT_DESC)
292 
293 /*
294  * The VIRTIO_NET_F_HOST_TSO[46] features permit us to send the host
295  * frames larger than 1514 bytes.
296  */
297 #define VTNET_TSO_FEATURES (VIRTIO_NET_F_GSO | VIRTIO_NET_F_HOST_TSO4 | \
298     VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN)
299 
300 /*
301  * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
302  * frames larger than 1514 bytes. We do not yet support software LRO
303  * via tcp_lro_rx().
304  */
305 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
306     VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
307 
308 #define VTNET_MAX_MTU		65536
309 #define VTNET_MAX_RX_SIZE	65550
310 
311 /*
312  * Used to preallocate the Vq indirect descriptors. The first segment
313  * is reserved for the header, except for mergeable buffers since the
314  * header is placed inline with the data.
315  */
316 #define VTNET_MRG_RX_SEGS	1
317 #define VTNET_MIN_RX_SEGS	2
318 #define VTNET_MAX_RX_SEGS	34
319 #define VTNET_MIN_TX_SEGS	32
320 #define VTNET_MAX_TX_SEGS	64
321 
322 /*
323  * Assert we can receive and transmit the maximum with regular
324  * size clusters.
325  */
326 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
327 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
328 
329 /*
330  * Number of slots in the Tx bufrings. This value matches most other
331  * multiqueue drivers.
332  */
333 #define VTNET_DEFAULT_BUFRING_SIZE	4096
334 
335 /*
336  * Determine how many mbufs are in each receive buffer. For LRO without
337  * mergeable buffers, we must allocate an mbuf chain large enough to
338  * hold both the vtnet_rx_header and the maximum receivable data.
339  */
340 #define VTNET_NEEDED_RX_MBUFS(_sc, _clsize)				\
341 	((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 :		\
342 	    howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE,	\
343 	        (_clsize))
344 
345 #define VTNET_CORE_MTX(_sc)		&(_sc)->vtnet_mtx
346 #define VTNET_CORE_LOCK(_sc)		mtx_lock(VTNET_CORE_MTX((_sc)))
347 #define VTNET_CORE_UNLOCK(_sc)		mtx_unlock(VTNET_CORE_MTX((_sc)))
348 #define VTNET_CORE_LOCK_DESTROY(_sc)	mtx_destroy(VTNET_CORE_MTX((_sc)))
349 #define VTNET_CORE_LOCK_ASSERT(_sc)		\
350     mtx_assert(VTNET_CORE_MTX((_sc)), MA_OWNED)
351 #define VTNET_CORE_LOCK_ASSERT_NOTOWNED(_sc)	\
352     mtx_assert(VTNET_CORE_MTX((_sc)), MA_NOTOWNED)
353 
354 #define VTNET_CORE_LOCK_INIT(_sc) do {					\
355     snprintf((_sc)->vtnet_mtx_name, sizeof((_sc)->vtnet_mtx_name),	\
356         "%s", device_get_nameunit((_sc)->vtnet_dev));			\
357     mtx_init(VTNET_CORE_MTX((_sc)), (_sc)->vtnet_mtx_name,		\
358         "VTNET Core Lock", MTX_DEF);					\
359 } while (0)
360 
361 #endif /* _IF_VTNETVAR_H */
362