xref: /freebsd/sys/dev/virtio/network/if_vtnetvar.h (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /*-
2  * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.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$
27  */
28 
29 #ifndef _IF_VTNETVAR_H
30 #define _IF_VTNETVAR_H
31 
32 struct vtnet_statistics {
33 	unsigned long	mbuf_alloc_failed;
34 
35 	unsigned long	rx_frame_too_large;
36 	unsigned long	rx_enq_replacement_failed;
37 	unsigned long	rx_mergeable_failed;
38 	unsigned long	rx_csum_bad_ethtype;
39 	unsigned long	rx_csum_bad_start;
40 	unsigned long	rx_csum_bad_ipproto;
41 	unsigned long	rx_csum_bad_offset;
42 	unsigned long	rx_csum_failed;
43 	unsigned long	rx_csum_offloaded;
44 	unsigned long	rx_task_rescheduled;
45 
46 	unsigned long	tx_csum_offloaded;
47 	unsigned long	tx_tso_offloaded;
48 	unsigned long	tx_csum_bad_ethtype;
49 	unsigned long	tx_tso_bad_ethtype;
50 	unsigned long	tx_task_rescheduled;
51 };
52 
53 struct vtnet_softc {
54 	device_t		 vtnet_dev;
55 	struct ifnet		*vtnet_ifp;
56 	struct mtx		 vtnet_mtx;
57 
58 	uint32_t		 vtnet_flags;
59 #define VTNET_FLAG_LINK		 0x0001
60 #define VTNET_FLAG_SUSPENDED	 0x0002
61 #define VTNET_FLAG_CTRL_VQ	 0x0004
62 #define VTNET_FLAG_CTRL_RX	 0x0008
63 #define VTNET_FLAG_VLAN_FILTER	 0x0010
64 #define VTNET_FLAG_TSO_ECN	 0x0020
65 #define VTNET_FLAG_MRG_RXBUFS	 0x0040
66 #define VTNET_FLAG_LRO_NOMRG	 0x0080
67 
68 	struct virtqueue	*vtnet_rx_vq;
69 	struct virtqueue	*vtnet_tx_vq;
70 	struct virtqueue	*vtnet_ctrl_vq;
71 
72 	int			 vtnet_hdr_size;
73 	int			 vtnet_tx_size;
74 	int			 vtnet_rx_size;
75 	int			 vtnet_rx_process_limit;
76 	int			 vtnet_rx_mbuf_size;
77 	int			 vtnet_rx_mbuf_count;
78 	int			 vtnet_if_flags;
79 	int			 vtnet_watchdog_timer;
80 	uint64_t		 vtnet_features;
81 
82 	struct taskqueue	*vtnet_tq;
83 	struct task		 vtnet_rx_intr_task;
84 	struct task		 vtnet_tx_intr_task;
85 	struct task		 vtnet_cfgchg_task;
86 
87 	struct vtnet_statistics	 vtnet_stats;
88 
89 	struct callout		 vtnet_tick_ch;
90 
91 	eventhandler_tag	 vtnet_vlan_attach;
92 	eventhandler_tag	 vtnet_vlan_detach;
93 
94 	struct ifmedia		 vtnet_media;
95 	/*
96 	 * Fake media type; the host does not provide us with
97 	 * any real media information.
98 	 */
99 #define VTNET_MEDIATYPE		 (IFM_ETHER | IFM_1000_T | IFM_FDX)
100 	char			 vtnet_hwaddr[ETHER_ADDR_LEN];
101 
102 	struct vtnet_mac_filter	*vtnet_mac_filter;
103 	/*
104 	 * During reset, the host's VLAN filtering table is lost. The
105 	 * array below is used to restore all the VLANs configured on
106 	 * this interface after a reset.
107 	 */
108 #define VTNET_VLAN_SHADOW_SIZE	 (4096 / 32)
109 	int			 vtnet_nvlans;
110 	uint32_t		 vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
111 
112 	char			 vtnet_mtx_name[16];
113 };
114 
115 /*
116  * When mergeable buffers are not negotiated, the vtnet_rx_header structure
117  * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
118  * both keep the VirtIO header and the data non-contiguous and to keep the
119  * frame's payload 4 byte aligned.
120  *
121  * When mergeable buffers are negotiated, the host puts the VirtIO header in
122  * the beginning of the first mbuf's data.
123  */
124 #define VTNET_RX_HEADER_PAD	4
125 struct vtnet_rx_header {
126 	struct virtio_net_hdr	vrh_hdr;
127 	char			vrh_pad[VTNET_RX_HEADER_PAD];
128 } __packed;
129 
130 /*
131  * For each outgoing frame, the vtnet_tx_header below is allocated from
132  * the vtnet_tx_header_zone.
133  */
134 struct vtnet_tx_header {
135 	union {
136 		struct virtio_net_hdr		hdr;
137 		struct virtio_net_hdr_mrg_rxbuf	mhdr;
138 	} vth_uhdr;
139 
140 	struct mbuf *vth_mbuf;
141 };
142 
143 /*
144  * The VirtIO specification does not place a limit on the number of MAC
145  * addresses the guest driver may request to be filtered. In practice,
146  * the host is constrained by available resources. To simplify this driver,
147  * impose a reasonably high limit of MAC addresses we will filter before
148  * falling back to promiscuous or all-multicast modes.
149  */
150 #define VTNET_MAX_MAC_ENTRIES	128
151 
152 struct vtnet_mac_table {
153 	uint32_t	nentries;
154 	uint8_t		macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
155 } __packed;
156 
157 struct vtnet_mac_filter {
158 	struct vtnet_mac_table	vmf_unicast;
159 	uint32_t		vmf_pad; /* Make tables non-contiguous. */
160 	struct vtnet_mac_table	vmf_multicast;
161 };
162 
163 /*
164  * The MAC filter table is malloc(9)'d when needed. Ensure it will
165  * always fit in one segment.
166  */
167 CTASSERT(sizeof(struct vtnet_mac_filter) <= PAGE_SIZE);
168 
169 #define VTNET_WATCHDOG_TIMEOUT	5
170 #define VTNET_CSUM_OFFLOAD	(CSUM_TCP | CSUM_UDP | CSUM_SCTP)
171 
172 /* Features desired/implemented by this driver. */
173 #define VTNET_FEATURES \
174     (VIRTIO_NET_F_MAC			| \
175      VIRTIO_NET_F_STATUS		| \
176      VIRTIO_NET_F_CTRL_VQ		| \
177      VIRTIO_NET_F_CTRL_RX		| \
178      VIRTIO_NET_F_CTRL_VLAN		| \
179      VIRTIO_NET_F_CSUM			| \
180      VIRTIO_NET_F_HOST_TSO4		| \
181      VIRTIO_NET_F_HOST_TSO6		| \
182      VIRTIO_NET_F_HOST_ECN		| \
183      VIRTIO_NET_F_GUEST_CSUM		| \
184      VIRTIO_NET_F_GUEST_TSO4		| \
185      VIRTIO_NET_F_GUEST_TSO6		| \
186      VIRTIO_NET_F_GUEST_ECN		| \
187      VIRTIO_NET_F_MRG_RXBUF		| \
188      VIRTIO_RING_F_INDIRECT_DESC)
189 
190 /*
191  * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us
192  * frames larger than 1514 bytes. We do not yet support software LRO
193  * via tcp_lro_rx().
194  */
195 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
196     VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
197 
198 #define VTNET_MAX_MTU		65536
199 #define VTNET_MAX_RX_SIZE	65550
200 
201 /*
202  * Used to preallocate the Vq indirect descriptors. The first segment
203  * is reserved for the header.
204  */
205 #define VTNET_MIN_RX_SEGS	2
206 #define VTNET_MAX_RX_SEGS	34
207 #define VTNET_MAX_TX_SEGS	34
208 
209 /*
210  * Assert we can receive and transmit the maximum with regular
211  * size clusters.
212  */
213 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
214 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
215 
216 /*
217  * Determine how many mbufs are in each receive buffer. For LRO without
218  * mergeable descriptors, we must allocate an mbuf chain large enough to
219  * hold both the vtnet_rx_header and the maximum receivable data.
220  */
221 #define VTNET_NEEDED_RX_MBUFS(_sc)					\
222 	((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 :		\
223 	    howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE,	\
224 	        (_sc)->vtnet_rx_mbuf_size)
225 
226 #define VTNET_MTX(_sc)		&(_sc)->vtnet_mtx
227 #define VTNET_LOCK(_sc)		mtx_lock(VTNET_MTX((_sc)))
228 #define VTNET_UNLOCK(_sc)	mtx_unlock(VTNET_MTX((_sc)))
229 #define VTNET_LOCK_DESTROY(_sc)	mtx_destroy(VTNET_MTX((_sc)))
230 #define VTNET_LOCK_ASSERT(_sc)	mtx_assert(VTNET_MTX((_sc)), MA_OWNED)
231 #define VTNET_LOCK_ASSERT_NOTOWNED(_sc)	\
232 	 			mtx_assert(VTNET_MTX((_sc)), MA_NOTOWNED)
233 
234 #define VTNET_LOCK_INIT(_sc) do {					\
235     snprintf((_sc)->vtnet_mtx_name, sizeof((_sc)->vtnet_mtx_name),	\
236         "%s", device_get_nameunit((_sc)->vtnet_dev));			\
237     mtx_init(VTNET_MTX((_sc)), (_sc)->vtnet_mtx_name,			\
238         "VTNET Core Lock", MTX_DEF);					\
239 } while (0)
240 
241 #endif /* _IF_VTNETVAR_H */
242