1*9fbe302bSMichael S. Tsirkin #ifndef _LINUX_VIRTIO_NET_H
2*9fbe302bSMichael S. Tsirkin #define _LINUX_VIRTIO_NET_H
3*9fbe302bSMichael S. Tsirkin /* This header is BSD licensed so anyone can use the definitions to implement
4*9fbe302bSMichael S. Tsirkin  * compatible drivers/servers.
5*9fbe302bSMichael S. Tsirkin  *
6*9fbe302bSMichael S. Tsirkin  * Redistribution and use in source and binary forms, with or without
7*9fbe302bSMichael S. Tsirkin  * modification, are permitted provided that the following conditions
8*9fbe302bSMichael S. Tsirkin  * are met:
9*9fbe302bSMichael S. Tsirkin  * 1. Redistributions of source code must retain the above copyright
10*9fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer.
11*9fbe302bSMichael S. Tsirkin  * 2. Redistributions in binary form must reproduce the above copyright
12*9fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer in the
13*9fbe302bSMichael S. Tsirkin  *    documentation and/or other materials provided with the distribution.
14*9fbe302bSMichael S. Tsirkin  * 3. Neither the name of IBM nor the names of its contributors
15*9fbe302bSMichael S. Tsirkin  *    may be used to endorse or promote products derived from this software
16*9fbe302bSMichael S. Tsirkin  *    without specific prior written permission.
17*9fbe302bSMichael S. Tsirkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
18*9fbe302bSMichael S. Tsirkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*9fbe302bSMichael S. Tsirkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*9fbe302bSMichael S. Tsirkin  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
21*9fbe302bSMichael S. Tsirkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*9fbe302bSMichael S. Tsirkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*9fbe302bSMichael S. Tsirkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*9fbe302bSMichael S. Tsirkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*9fbe302bSMichael S. Tsirkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*9fbe302bSMichael S. Tsirkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*9fbe302bSMichael S. Tsirkin  * SUCH DAMAGE. */
28*9fbe302bSMichael S. Tsirkin #include "standard-headers/linux/types.h"
29*9fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_ids.h"
30*9fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_config.h"
31*9fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_types.h"
32*9fbe302bSMichael S. Tsirkin #include "standard-headers/linux/if_ether.h"
33*9fbe302bSMichael S. Tsirkin 
34*9fbe302bSMichael S. Tsirkin /* The feature bitmap for virtio net */
35*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CSUM	0	/* Host handles pkts w/ partial csum */
36*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_CSUM	1	/* Guest handles pkts w/ partial csum */
37*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_MAC	5	/* Host has given MAC address. */
38*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_TSO4	7	/* Guest can handle TSOv4 in. */
39*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_TSO6	8	/* Guest can handle TSOv6 in. */
40*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_ECN	9	/* Guest can handle TSO[6] w/ ECN in. */
41*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_UFO	10	/* Guest can handle UFO in. */
42*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_TSO4	11	/* Host can handle TSOv4 in. */
43*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_TSO6	12	/* Host can handle TSOv6 in. */
44*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_ECN	13	/* Host can handle TSO[6] w/ ECN in. */
45*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_UFO	14	/* Host can handle UFO in. */
46*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_MRG_RXBUF	15	/* Host can merge receive buffers. */
47*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_STATUS	16	/* virtio_net_config.status available */
48*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_VQ	17	/* Control channel available */
49*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_RX	18	/* Control channel RX mode support */
50*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_VLAN	19	/* Control channel VLAN filtering */
51*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_RX_EXTRA 20	/* Extra RX mode control support */
52*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_ANNOUNCE 21	/* Guest can announce device on the
53*9fbe302bSMichael S. Tsirkin 					 * network */
54*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_MQ	22	/* Device supports Receive Flow
55*9fbe302bSMichael S. Tsirkin 					 * Steering */
56*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
57*9fbe302bSMichael S. Tsirkin 
58*9fbe302bSMichael S. Tsirkin #ifndef VIRTIO_NET_NO_LEGACY
59*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GSO	6	/* Host handles pkts w/ any GSO type */
60*9fbe302bSMichael S. Tsirkin #endif /* VIRTIO_NET_NO_LEGACY */
61*9fbe302bSMichael S. Tsirkin 
62*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_S_LINK_UP	1	/* Link is up */
63*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_S_ANNOUNCE	2	/* Announcement is needed */
64*9fbe302bSMichael S. Tsirkin 
65*9fbe302bSMichael S. Tsirkin struct virtio_net_config {
66*9fbe302bSMichael S. Tsirkin 	/* The config defining mac address (if VIRTIO_NET_F_MAC) */
67*9fbe302bSMichael S. Tsirkin 	uint8_t mac[ETH_ALEN];
68*9fbe302bSMichael S. Tsirkin 	/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
69*9fbe302bSMichael S. Tsirkin 	uint16_t status;
70*9fbe302bSMichael S. Tsirkin 	/* Maximum number of each of transmit and receive queues;
71*9fbe302bSMichael S. Tsirkin 	 * see VIRTIO_NET_F_MQ and VIRTIO_NET_CTRL_MQ.
72*9fbe302bSMichael S. Tsirkin 	 * Legal values are between 1 and 0x8000
73*9fbe302bSMichael S. Tsirkin 	 */
74*9fbe302bSMichael S. Tsirkin 	uint16_t max_virtqueue_pairs;
75*9fbe302bSMichael S. Tsirkin } QEMU_PACKED;
76*9fbe302bSMichael S. Tsirkin 
77*9fbe302bSMichael S. Tsirkin #ifndef VIRTIO_NET_NO_LEGACY
78*9fbe302bSMichael S. Tsirkin /* This header comes first in the scatter-gather list.
79*9fbe302bSMichael S. Tsirkin  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, it must
80*9fbe302bSMichael S. Tsirkin  * be the first element of the scatter-gather list.  If you don't
81*9fbe302bSMichael S. Tsirkin  * specify GSO or CSUM features, you can simply ignore the header. */
82*9fbe302bSMichael S. Tsirkin struct virtio_net_hdr {
83*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_F_NEEDS_CSUM	1	// Use csum_start, csum_offset
84*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_F_DATA_VALID	2	// Csum is valid
85*9fbe302bSMichael S. Tsirkin 	uint8_t flags;
86*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_NONE		0	// Not a GSO frame
87*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_TCPV4	1	// GSO frame, IPv4 TCP (TSO)
88*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_UDP		3	// GSO frame, IPv4 UDP (UFO)
89*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_TCPV6	4	// GSO frame, IPv6 TCP
90*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_ECN		0x80	// TCP has ECN set
91*9fbe302bSMichael S. Tsirkin 	uint8_t gso_type;
92*9fbe302bSMichael S. Tsirkin 	__virtio16 hdr_len;		/* Ethernet + IP + tcp/udp hdrs */
93*9fbe302bSMichael S. Tsirkin 	__virtio16 gso_size;		/* Bytes to append to hdr_len per frame */
94*9fbe302bSMichael S. Tsirkin 	__virtio16 csum_start;	/* Position to start checksumming from */
95*9fbe302bSMichael S. Tsirkin 	__virtio16 csum_offset;	/* Offset after that to place checksum */
96*9fbe302bSMichael S. Tsirkin };
97*9fbe302bSMichael S. Tsirkin 
98*9fbe302bSMichael S. Tsirkin /* This is the version of the header to use when the MRG_RXBUF
99*9fbe302bSMichael S. Tsirkin  * feature has been negotiated. */
100*9fbe302bSMichael S. Tsirkin struct virtio_net_hdr_mrg_rxbuf {
101*9fbe302bSMichael S. Tsirkin 	struct virtio_net_hdr hdr;
102*9fbe302bSMichael S. Tsirkin 	__virtio16 num_buffers;	/* Number of merged rx buffers */
103*9fbe302bSMichael S. Tsirkin };
104*9fbe302bSMichael S. Tsirkin #else /* ... VIRTIO_NET_NO_LEGACY */
105*9fbe302bSMichael S. Tsirkin /*
106*9fbe302bSMichael S. Tsirkin  * This header comes first in the scatter-gather list.  If you don't
107*9fbe302bSMichael S. Tsirkin  * specify GSO or CSUM features, you can simply ignore the header.
108*9fbe302bSMichael S. Tsirkin  *
109*9fbe302bSMichael S. Tsirkin  * This is bitwise-equivalent to the legacy struct virtio_net_hdr_mrg_rxbuf.
110*9fbe302bSMichael S. Tsirkin  */
111*9fbe302bSMichael S. Tsirkin struct virtio_net_hdr_v1 {
112*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_F_NEEDS_CSUM	1	/* Use csum_start, csum_offset */
113*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_F_DATA_VALID	2	/* Csum is valid */
114*9fbe302bSMichael S. Tsirkin 	uint8_t flags;
115*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_NONE		0	/* Not a GSO frame */
116*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_TCPV4	1	/* GSO frame, IPv4 TCP (TSO) */
117*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_UDP		3	/* GSO frame, IPv4 UDP (UFO) */
118*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_TCPV6	4	/* GSO frame, IPv6 TCP */
119*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_ECN		0x80	/* TCP has ECN set */
120*9fbe302bSMichael S. Tsirkin 	uint8_t gso_type;
121*9fbe302bSMichael S. Tsirkin 	__virtio16 hdr_len;	/* Ethernet + IP + tcp/udp hdrs */
122*9fbe302bSMichael S. Tsirkin 	__virtio16 gso_size;	/* Bytes to append to hdr_len per frame */
123*9fbe302bSMichael S. Tsirkin 	__virtio16 csum_start;	/* Position to start checksumming from */
124*9fbe302bSMichael S. Tsirkin 	__virtio16 csum_offset;	/* Offset after that to place checksum */
125*9fbe302bSMichael S. Tsirkin 	__virtio16 num_buffers;	/* Number of merged rx buffers */
126*9fbe302bSMichael S. Tsirkin };
127*9fbe302bSMichael S. Tsirkin #endif /* ...VIRTIO_NET_NO_LEGACY */
128*9fbe302bSMichael S. Tsirkin 
129*9fbe302bSMichael S. Tsirkin /*
130*9fbe302bSMichael S. Tsirkin  * Control virtqueue data structures
131*9fbe302bSMichael S. Tsirkin  *
132*9fbe302bSMichael S. Tsirkin  * The control virtqueue expects a header in the first sg entry
133*9fbe302bSMichael S. Tsirkin  * and an ack/status response in the last entry.  Data for the
134*9fbe302bSMichael S. Tsirkin  * command goes in between.
135*9fbe302bSMichael S. Tsirkin  */
136*9fbe302bSMichael S. Tsirkin struct virtio_net_ctrl_hdr {
137*9fbe302bSMichael S. Tsirkin 	uint8_t class;
138*9fbe302bSMichael S. Tsirkin 	uint8_t cmd;
139*9fbe302bSMichael S. Tsirkin } QEMU_PACKED;
140*9fbe302bSMichael S. Tsirkin 
141*9fbe302bSMichael S. Tsirkin typedef uint8_t virtio_net_ctrl_ack;
142*9fbe302bSMichael S. Tsirkin 
143*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_OK     0
144*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_ERR    1
145*9fbe302bSMichael S. Tsirkin 
146*9fbe302bSMichael S. Tsirkin /*
147*9fbe302bSMichael S. Tsirkin  * Control the RX mode, ie. promisucous, allmulti, etc...
148*9fbe302bSMichael S. Tsirkin  * All commands require an "out" sg entry containing a 1 byte
149*9fbe302bSMichael S. Tsirkin  * state value, zero = disable, non-zero = enable.  Commands
150*9fbe302bSMichael S. Tsirkin  * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
151*9fbe302bSMichael S. Tsirkin  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
152*9fbe302bSMichael S. Tsirkin  */
153*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_RX    0
154*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_PROMISC      0
155*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_ALLMULTI     1
156*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_ALLUNI       2
157*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_NOMULTI      3
158*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_NOUNI        4
159*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_NOBCAST      5
160*9fbe302bSMichael S. Tsirkin 
161*9fbe302bSMichael S. Tsirkin /*
162*9fbe302bSMichael S. Tsirkin  * Control the MAC
163*9fbe302bSMichael S. Tsirkin  *
164*9fbe302bSMichael S. Tsirkin  * The MAC filter table is managed by the hypervisor, the guest should
165*9fbe302bSMichael S. Tsirkin  * assume the size is infinite.  Filtering should be considered
166*9fbe302bSMichael S. Tsirkin  * non-perfect, ie. based on hypervisor resources, the guest may
167*9fbe302bSMichael S. Tsirkin  * received packets from sources not specified in the filter list.
168*9fbe302bSMichael S. Tsirkin  *
169*9fbe302bSMichael S. Tsirkin  * In addition to the class/cmd header, the TABLE_SET command requires
170*9fbe302bSMichael S. Tsirkin  * two out scatterlists.  Each contains a 4 byte count of entries followed
171*9fbe302bSMichael S. Tsirkin  * by a concatenated byte stream of the ETH_ALEN MAC addresses.  The
172*9fbe302bSMichael S. Tsirkin  * first sg list contains unicast addresses, the second is for multicast.
173*9fbe302bSMichael S. Tsirkin  * This functionality is present if the VIRTIO_NET_F_CTRL_RX feature
174*9fbe302bSMichael S. Tsirkin  * is available.
175*9fbe302bSMichael S. Tsirkin  *
176*9fbe302bSMichael S. Tsirkin  * The ADDR_SET command requests one out scatterlist, it contains a
177*9fbe302bSMichael S. Tsirkin  * 6 bytes MAC address. This functionality is present if the
178*9fbe302bSMichael S. Tsirkin  * VIRTIO_NET_F_CTRL_MAC_ADDR feature is available.
179*9fbe302bSMichael S. Tsirkin  */
180*9fbe302bSMichael S. Tsirkin struct virtio_net_ctrl_mac {
181*9fbe302bSMichael S. Tsirkin 	__virtio32 entries;
182*9fbe302bSMichael S. Tsirkin 	uint8_t macs[][ETH_ALEN];
183*9fbe302bSMichael S. Tsirkin } QEMU_PACKED;
184*9fbe302bSMichael S. Tsirkin 
185*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_MAC    1
186*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MAC_TABLE_SET        0
187*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MAC_ADDR_SET         1
188*9fbe302bSMichael S. Tsirkin 
189*9fbe302bSMichael S. Tsirkin /*
190*9fbe302bSMichael S. Tsirkin  * Control VLAN filtering
191*9fbe302bSMichael S. Tsirkin  *
192*9fbe302bSMichael S. Tsirkin  * The VLAN filter table is controlled via a simple ADD/DEL interface.
193*9fbe302bSMichael S. Tsirkin  * VLAN IDs not added may be filterd by the hypervisor.  Del is the
194*9fbe302bSMichael S. Tsirkin  * opposite of add.  Both commands expect an out entry containing a 2
195*9fbe302bSMichael S. Tsirkin  * byte VLAN ID.  VLAN filterting is available with the
196*9fbe302bSMichael S. Tsirkin  * VIRTIO_NET_F_CTRL_VLAN feature bit.
197*9fbe302bSMichael S. Tsirkin  */
198*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_VLAN       2
199*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_VLAN_ADD             0
200*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_VLAN_DEL             1
201*9fbe302bSMichael S. Tsirkin 
202*9fbe302bSMichael S. Tsirkin /*
203*9fbe302bSMichael S. Tsirkin  * Control link announce acknowledgement
204*9fbe302bSMichael S. Tsirkin  *
205*9fbe302bSMichael S. Tsirkin  * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
206*9fbe302bSMichael S. Tsirkin  * driver has recevied the notification; device would clear the
207*9fbe302bSMichael S. Tsirkin  * VIRTIO_NET_S_ANNOUNCE bit in the status field after it receives
208*9fbe302bSMichael S. Tsirkin  * this command.
209*9fbe302bSMichael S. Tsirkin  */
210*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_ANNOUNCE       3
211*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_ANNOUNCE_ACK         0
212*9fbe302bSMichael S. Tsirkin 
213*9fbe302bSMichael S. Tsirkin /*
214*9fbe302bSMichael S. Tsirkin  * Control Receive Flow Steering
215*9fbe302bSMichael S. Tsirkin  *
216*9fbe302bSMichael S. Tsirkin  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
217*9fbe302bSMichael S. Tsirkin  * enables Receive Flow Steering, specifying the number of the transmit and
218*9fbe302bSMichael S. Tsirkin  * receive queues that will be used. After the command is consumed and acked by
219*9fbe302bSMichael S. Tsirkin  * the device, the device will not steer new packets on receive virtqueues
220*9fbe302bSMichael S. Tsirkin  * other than specified nor read from transmit virtqueues other than specified.
221*9fbe302bSMichael S. Tsirkin  * Accordingly, driver should not transmit new packets  on virtqueues other than
222*9fbe302bSMichael S. Tsirkin  * specified.
223*9fbe302bSMichael S. Tsirkin  */
224*9fbe302bSMichael S. Tsirkin struct virtio_net_ctrl_mq {
225*9fbe302bSMichael S. Tsirkin 	__virtio16 virtqueue_pairs;
226*9fbe302bSMichael S. Tsirkin };
227*9fbe302bSMichael S. Tsirkin 
228*9fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_MQ   4
229*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
230*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
231*9fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
232*9fbe302bSMichael S. Tsirkin 
233*9fbe302bSMichael S. Tsirkin #endif /* _LINUX_VIRTIO_NET_H */
234