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