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 
27 #ifndef _IF_VTNETVAR_H
28 #define _IF_VTNETVAR_H
29 
30 struct vtnet_statistics {
31 	uint64_t	mbuf_alloc_failed;
32 
33 	uint64_t	rx_frame_too_large;
34 	uint64_t	rx_enq_replacement_failed;
35 	uint64_t	rx_mergeable_failed;
36 	uint64_t	rx_csum_bad_ethtype;
37 	uint64_t	rx_csum_bad_ipproto;
38 	uint64_t	rx_csum_bad_offset;
39 	uint64_t	rx_csum_failed;
40 	uint64_t	rx_csum_offloaded;
41 	uint64_t	rx_task_rescheduled;
42 
43 	uint64_t	tx_csum_offloaded;
44 	uint64_t	tx_tso_offloaded;
45 	uint64_t	tx_csum_bad_ethtype;
46 	uint64_t	tx_tso_bad_ethtype;
47 	uint64_t	tx_defragged;
48 	uint64_t	tx_defrag_failed;
49 	uint64_t	tx_task_rescheduled;
50 };
51 
52 struct vtnet_softc {
53 	device_t		vtnet_dev;
54 	struct ifnet		*vtnet_ifp;
55 	struct lwkt_serialize	vtnet_slz;
56 
57 	uint32_t		vtnet_flags;
58 #define VTNET_FLAG_LINK		0x0001
59 #define VTNET_FLAG_SUSPENDED	0x0002
60 #define VTNET_FLAG_MAC		0x0004
61 #define VTNET_FLAG_CTRL_VQ	0x0008
62 #define VTNET_FLAG_CTRL_RX	0x0010
63 #define VTNET_FLAG_CTRL_MAC	0x0020
64 #define VTNET_FLAG_VLAN_FILTER	0x0040
65 #define VTNET_FLAG_TSO_ECN	0x0080
66 #define VTNET_FLAG_MRG_RXBUFS	0x0100
67 #define VTNET_FLAG_LRO_NOMRG	0x0200
68 #define VTNET_FLAG_INDIRECT	0x0400
69 
70 	struct virtqueue	*vtnet_rx_vq;
71 	struct virtqueue	*vtnet_tx_vq;
72 	struct virtqueue	*vtnet_ctrl_vq;
73 
74 	int			vtnet_txhdrcount;
75 	struct vtnet_tx_header	*vtnet_txhdrarea;
76 	uint32_t		vtnet_txhdridx;
77 	struct vtnet_mac_filter *vtnet_macfilter;
78 
79 	int			vtnet_hdr_size;
80 	int			vtnet_tx_size;
81 	int			vtnet_tx_nsegs;
82 	int			vtnet_rx_size;
83 	int			vtnet_rx_process_limit;
84 	int			vtnet_rx_nsegs;
85 	int			vtnet_rx_mbuf_size;
86 	int			vtnet_rx_mbuf_count;
87 	int			vtnet_if_flags;
88 	int			vtnet_watchdog_timer;
89 	uint64_t		vtnet_features;
90 
91 	struct task		vtnet_cfgchg_task;
92 
93 	struct vtnet_statistics	vtnet_stats;
94 
95 	struct callout		vtnet_tick_ch;
96 
97 	eventhandler_tag	vtnet_vlan_attach;
98 	eventhandler_tag	vtnet_vlan_detach;
99 
100 	struct ifmedia		vtnet_media;
101 	/*
102 	 * Fake media type; the host does not provide us with
103 	 * any real media information.
104 	 */
105 #define VTNET_MEDIATYPE		(IFM_ETHER | IFM_1000_T | IFM_FDX)
106 	char			vtnet_hwaddr[ETHER_ADDR_LEN];
107 
108 	/*
109 	 * During reset, the host's VLAN filtering table is lost. The
110 	 * array below is used to restore all the VLANs configured on
111 	 * this interface after a reset.
112 	 */
113 #define VTNET_VLAN_SHADOW_SIZE	(4096 / 32)
114 	int			vtnet_nvlans;
115 	uint32_t		vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE];
116 
117 	char			vtnet_mtx_name[16];
118 };
119 
120 /*
121  * When mergeable buffers are not negotiated, the vtnet_rx_header structure
122  * below is placed at the beginning of the mbuf data. Use 4 bytes of pad to
123  * both keep the VirtIO header and the data non-contiguous and to keep the
124  * frame's payload 4 byte aligned.
125  *
126  * When mergeable buffers are negotiated, the host puts the VirtIO header in
127  * the beginning of the first mbuf's data.
128  */
129 #define VTNET_RX_HEADER_PAD	4
130 struct vtnet_rx_header {
131 	struct virtio_net_hdr	vrh_hdr;
132 	char			vrh_pad[VTNET_RX_HEADER_PAD];
133 } __packed;
134 
135 /*
136  * For each outgoing frame, the vtnet_tx_header below is allocated from
137  * the vtnet_tx_header_zone.
138  */
139 struct vtnet_tx_header {
140 	union {
141 		struct virtio_net_hdr		hdr;
142 		struct virtio_net_hdr_mrg_rxbuf	mhdr;
143 	} vth_uhdr;
144 
145 	struct mbuf *vth_mbuf;
146 };
147 
148 /*
149  * The VirtIO specification does not place a limit on the number of MAC
150  * addresses the guest driver may request to be filtered. In practice,
151  * the host is constrained by available resources. To simplify this driver,
152  * impose a reasonably high limit of MAC addresses we will filter before
153  * falling back to promiscuous or all-multicast modes.
154  */
155 #define VTNET_MAX_MAC_ENTRIES	128
156 
157 struct vtnet_mac_table {
158 	uint32_t	nentries;
159 	uint8_t		macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN];
160 } __packed;
161 
162 struct vtnet_mac_filter {
163 	struct vtnet_mac_table	vmf_unicast;
164 	uint32_t		vmf_pad; /* Make tables non-contiguous. */
165 	struct vtnet_mac_table	vmf_multicast;
166 };
167 
168 #define VTNET_WATCHDOG_TIMEOUT	5
169 #define VTNET_CSUM_OFFLOAD	(CSUM_TCP | CSUM_UDP)
170 
171 /* Features desired/implemented by this driver. */
172 #define VTNET_FEATURES 		\
173     (VIRTIO_NET_F_MAC		| \
174      VIRTIO_NET_F_STATUS	| \
175      VIRTIO_NET_F_CTRL_VQ	| \
176      VIRTIO_NET_F_CTRL_RX	| \
177      VIRTIO_NET_F_CTRL_MAC_ADDR	| \
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_MRG_RX_SEGS	1
206 #define VTNET_MIN_RX_SEGS	2
207 #define VTNET_MAX_RX_SEGS	34
208 #define VTNET_MIN_TX_SEGS	4
209 #define VTNET_MAX_TX_SEGS	64
210 
211 #define IFCAP_LRO               0x00400 /* can do Large Receive Offload */
212 #define IFCAP_VLAN_HWFILTER     0x10000 /* interface hw can filter vlan tag */
213 #define IFCAP_VLAN_HWTSO        0x40000 /* can do IFCAP_TSO on VLANs */
214 
215 /*
216  * Assert we can receive and transmit the maximum with regular
217  * size clusters.
218  */
219 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE);
220 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU);
221 
222 /*
223  * Determine how many mbufs are in each receive buffer. For LRO without
224  * mergeable buffers, we must allocate an mbuf chain large enough to
225  * hold both the vtnet_rx_header and the maximum receivable data.
226  */
227 #define VTNET_NEEDED_RX_MBUFS(_sc)					\
228 	((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 :		\
229 	howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE,	\
230 	(_sc)->vtnet_rx_mbuf_size)
231 
232 #endif /* _IF_VTNETVAR_H */
233