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