19fbe302bSMichael S. Tsirkin #ifndef _LINUX_VIRTIO_NET_H
29fbe302bSMichael S. Tsirkin #define _LINUX_VIRTIO_NET_H
39fbe302bSMichael S. Tsirkin /* This header is BSD licensed so anyone can use the definitions to implement
49fbe302bSMichael S. Tsirkin  * compatible drivers/servers.
59fbe302bSMichael S. Tsirkin  *
69fbe302bSMichael S. Tsirkin  * Redistribution and use in source and binary forms, with or without
79fbe302bSMichael S. Tsirkin  * modification, are permitted provided that the following conditions
89fbe302bSMichael S. Tsirkin  * are met:
99fbe302bSMichael S. Tsirkin  * 1. Redistributions of source code must retain the above copyright
109fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer.
119fbe302bSMichael S. Tsirkin  * 2. Redistributions in binary form must reproduce the above copyright
129fbe302bSMichael S. Tsirkin  *    notice, this list of conditions and the following disclaimer in the
139fbe302bSMichael S. Tsirkin  *    documentation and/or other materials provided with the distribution.
149fbe302bSMichael S. Tsirkin  * 3. Neither the name of IBM nor the names of its contributors
159fbe302bSMichael S. Tsirkin  *    may be used to endorse or promote products derived from this software
169fbe302bSMichael S. Tsirkin  *    without specific prior written permission.
179fbe302bSMichael S. Tsirkin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
189fbe302bSMichael S. Tsirkin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199fbe302bSMichael S. Tsirkin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209fbe302bSMichael S. Tsirkin  * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
219fbe302bSMichael S. Tsirkin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229fbe302bSMichael S. Tsirkin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239fbe302bSMichael S. Tsirkin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249fbe302bSMichael S. Tsirkin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259fbe302bSMichael S. Tsirkin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269fbe302bSMichael S. Tsirkin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279fbe302bSMichael S. Tsirkin  * SUCH DAMAGE. */
289fbe302bSMichael S. Tsirkin #include "standard-headers/linux/types.h"
299fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_ids.h"
309fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_config.h"
319fbe302bSMichael S. Tsirkin #include "standard-headers/linux/virtio_types.h"
329fbe302bSMichael S. Tsirkin #include "standard-headers/linux/if_ether.h"
339fbe302bSMichael S. Tsirkin 
349fbe302bSMichael S. Tsirkin /* The feature bitmap for virtio net */
359fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CSUM	0	/* Host handles pkts w/ partial csum */
369fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_CSUM	1	/* Guest handles pkts w/ partial csum */
37*f56fc2d3SMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS 2 /* Dynamic offload configuration. */
389fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_MAC	5	/* Host has given MAC address. */
399fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_TSO4	7	/* Guest can handle TSOv4 in. */
409fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_TSO6	8	/* Guest can handle TSOv6 in. */
419fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_ECN	9	/* Guest can handle TSO[6] w/ ECN in. */
429fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_UFO	10	/* Guest can handle UFO in. */
439fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_TSO4	11	/* Host can handle TSOv4 in. */
449fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_TSO6	12	/* Host can handle TSOv6 in. */
459fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_ECN	13	/* Host can handle TSO[6] w/ ECN in. */
469fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_HOST_UFO	14	/* Host can handle UFO in. */
479fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_MRG_RXBUF	15	/* Host can merge receive buffers. */
489fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_STATUS	16	/* virtio_net_config.status available */
499fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_VQ	17	/* Control channel available */
509fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_RX	18	/* Control channel RX mode support */
519fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_VLAN	19	/* Control channel VLAN filtering */
529fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_RX_EXTRA 20	/* Extra RX mode control support */
539fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GUEST_ANNOUNCE 21	/* Guest can announce device on the
549fbe302bSMichael S. Tsirkin 					 * network */
559fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_MQ	22	/* Device supports Receive Flow
569fbe302bSMichael S. Tsirkin 					 * Steering */
579fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
589fbe302bSMichael S. Tsirkin 
599fbe302bSMichael S. Tsirkin #ifndef VIRTIO_NET_NO_LEGACY
609fbe302bSMichael S. Tsirkin #define VIRTIO_NET_F_GSO	6	/* Host handles pkts w/ any GSO type */
619fbe302bSMichael S. Tsirkin #endif /* VIRTIO_NET_NO_LEGACY */
629fbe302bSMichael S. Tsirkin 
639fbe302bSMichael S. Tsirkin #define VIRTIO_NET_S_LINK_UP	1	/* Link is up */
649fbe302bSMichael S. Tsirkin #define VIRTIO_NET_S_ANNOUNCE	2	/* Announcement is needed */
659fbe302bSMichael S. Tsirkin 
669fbe302bSMichael S. Tsirkin struct virtio_net_config {
679fbe302bSMichael S. Tsirkin 	/* The config defining mac address (if VIRTIO_NET_F_MAC) */
689fbe302bSMichael S. Tsirkin 	uint8_t mac[ETH_ALEN];
699fbe302bSMichael S. Tsirkin 	/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
709fbe302bSMichael S. Tsirkin 	uint16_t status;
719fbe302bSMichael S. Tsirkin 	/* Maximum number of each of transmit and receive queues;
729fbe302bSMichael S. Tsirkin 	 * see VIRTIO_NET_F_MQ and VIRTIO_NET_CTRL_MQ.
739fbe302bSMichael S. Tsirkin 	 * Legal values are between 1 and 0x8000
749fbe302bSMichael S. Tsirkin 	 */
759fbe302bSMichael S. Tsirkin 	uint16_t max_virtqueue_pairs;
769fbe302bSMichael S. Tsirkin } QEMU_PACKED;
779fbe302bSMichael S. Tsirkin 
789fbe302bSMichael S. Tsirkin /*
799fbe302bSMichael S. Tsirkin  * This header comes first in the scatter-gather list.  If you don't
809fbe302bSMichael S. Tsirkin  * specify GSO or CSUM features, you can simply ignore the header.
819fbe302bSMichael S. Tsirkin  *
8251628b18SChristian Borntraeger  * This is bitwise-equivalent to the legacy struct virtio_net_hdr_mrg_rxbuf,
8351628b18SChristian Borntraeger  * only flattened.
849fbe302bSMichael S. Tsirkin  */
859fbe302bSMichael S. Tsirkin struct virtio_net_hdr_v1 {
869fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_F_NEEDS_CSUM	1	/* Use csum_start, csum_offset */
879fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_F_DATA_VALID	2	/* Csum is valid */
889fbe302bSMichael S. Tsirkin 	uint8_t flags;
899fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_NONE		0	/* Not a GSO frame */
909fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_TCPV4	1	/* GSO frame, IPv4 TCP (TSO) */
919fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_UDP		3	/* GSO frame, IPv4 UDP (UFO) */
929fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_TCPV6	4	/* GSO frame, IPv6 TCP */
939fbe302bSMichael S. Tsirkin #define VIRTIO_NET_HDR_GSO_ECN		0x80	/* TCP has ECN set */
949fbe302bSMichael S. Tsirkin 	uint8_t gso_type;
959fbe302bSMichael S. Tsirkin 	__virtio16 hdr_len;	/* Ethernet + IP + tcp/udp hdrs */
969fbe302bSMichael S. Tsirkin 	__virtio16 gso_size;	/* Bytes to append to hdr_len per frame */
979fbe302bSMichael S. Tsirkin 	__virtio16 csum_start;	/* Position to start checksumming from */
989fbe302bSMichael S. Tsirkin 	__virtio16 csum_offset;	/* Offset after that to place checksum */
999fbe302bSMichael S. Tsirkin 	__virtio16 num_buffers;	/* Number of merged rx buffers */
1009fbe302bSMichael S. Tsirkin };
10151628b18SChristian Borntraeger 
10251628b18SChristian Borntraeger #ifndef VIRTIO_NET_NO_LEGACY
10351628b18SChristian Borntraeger /* This header comes first in the scatter-gather list.
10451628b18SChristian Borntraeger  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, it must
10551628b18SChristian Borntraeger  * be the first element of the scatter-gather list.  If you don't
10651628b18SChristian Borntraeger  * specify GSO or CSUM features, you can simply ignore the header. */
10751628b18SChristian Borntraeger struct virtio_net_hdr {
10851628b18SChristian Borntraeger 	/* See VIRTIO_NET_HDR_F_* */
10951628b18SChristian Borntraeger 	uint8_t flags;
11051628b18SChristian Borntraeger 	/* See VIRTIO_NET_HDR_GSO_* */
11151628b18SChristian Borntraeger 	uint8_t gso_type;
11251628b18SChristian Borntraeger 	__virtio16 hdr_len;		/* Ethernet + IP + tcp/udp hdrs */
11351628b18SChristian Borntraeger 	__virtio16 gso_size;		/* Bytes to append to hdr_len per frame */
11451628b18SChristian Borntraeger 	__virtio16 csum_start;	/* Position to start checksumming from */
11551628b18SChristian Borntraeger 	__virtio16 csum_offset;	/* Offset after that to place checksum */
11651628b18SChristian Borntraeger };
11751628b18SChristian Borntraeger 
11851628b18SChristian Borntraeger /* This is the version of the header to use when the MRG_RXBUF
11951628b18SChristian Borntraeger  * feature has been negotiated. */
12051628b18SChristian Borntraeger struct virtio_net_hdr_mrg_rxbuf {
12151628b18SChristian Borntraeger 	struct virtio_net_hdr hdr;
12251628b18SChristian Borntraeger 	__virtio16 num_buffers;	/* Number of merged rx buffers */
12351628b18SChristian Borntraeger };
1249fbe302bSMichael S. Tsirkin #endif /* ...VIRTIO_NET_NO_LEGACY */
1259fbe302bSMichael S. Tsirkin 
1269fbe302bSMichael S. Tsirkin /*
1279fbe302bSMichael S. Tsirkin  * Control virtqueue data structures
1289fbe302bSMichael S. Tsirkin  *
1299fbe302bSMichael S. Tsirkin  * The control virtqueue expects a header in the first sg entry
1309fbe302bSMichael S. Tsirkin  * and an ack/status response in the last entry.  Data for the
1319fbe302bSMichael S. Tsirkin  * command goes in between.
1329fbe302bSMichael S. Tsirkin  */
1339fbe302bSMichael S. Tsirkin struct virtio_net_ctrl_hdr {
1349fbe302bSMichael S. Tsirkin 	uint8_t class;
1359fbe302bSMichael S. Tsirkin 	uint8_t cmd;
1369fbe302bSMichael S. Tsirkin } QEMU_PACKED;
1379fbe302bSMichael S. Tsirkin 
1389fbe302bSMichael S. Tsirkin typedef uint8_t virtio_net_ctrl_ack;
1399fbe302bSMichael S. Tsirkin 
1409fbe302bSMichael S. Tsirkin #define VIRTIO_NET_OK     0
1419fbe302bSMichael S. Tsirkin #define VIRTIO_NET_ERR    1
1429fbe302bSMichael S. Tsirkin 
1439fbe302bSMichael S. Tsirkin /*
1449fbe302bSMichael S. Tsirkin  * Control the RX mode, ie. promisucous, allmulti, etc...
1459fbe302bSMichael S. Tsirkin  * All commands require an "out" sg entry containing a 1 byte
1469fbe302bSMichael S. Tsirkin  * state value, zero = disable, non-zero = enable.  Commands
1479fbe302bSMichael S. Tsirkin  * 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
1489fbe302bSMichael S. Tsirkin  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
1499fbe302bSMichael S. Tsirkin  */
1509fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_RX    0
1519fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_PROMISC      0
1529fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_ALLMULTI     1
1539fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_ALLUNI       2
1549fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_NOMULTI      3
1559fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_NOUNI        4
1569fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_RX_NOBCAST      5
1579fbe302bSMichael S. Tsirkin 
1589fbe302bSMichael S. Tsirkin /*
1599fbe302bSMichael S. Tsirkin  * Control the MAC
1609fbe302bSMichael S. Tsirkin  *
1619fbe302bSMichael S. Tsirkin  * The MAC filter table is managed by the hypervisor, the guest should
1629fbe302bSMichael S. Tsirkin  * assume the size is infinite.  Filtering should be considered
1639fbe302bSMichael S. Tsirkin  * non-perfect, ie. based on hypervisor resources, the guest may
1649fbe302bSMichael S. Tsirkin  * received packets from sources not specified in the filter list.
1659fbe302bSMichael S. Tsirkin  *
1669fbe302bSMichael S. Tsirkin  * In addition to the class/cmd header, the TABLE_SET command requires
1679fbe302bSMichael S. Tsirkin  * two out scatterlists.  Each contains a 4 byte count of entries followed
1689fbe302bSMichael S. Tsirkin  * by a concatenated byte stream of the ETH_ALEN MAC addresses.  The
1699fbe302bSMichael S. Tsirkin  * first sg list contains unicast addresses, the second is for multicast.
1709fbe302bSMichael S. Tsirkin  * This functionality is present if the VIRTIO_NET_F_CTRL_RX feature
1719fbe302bSMichael S. Tsirkin  * is available.
1729fbe302bSMichael S. Tsirkin  *
1739fbe302bSMichael S. Tsirkin  * The ADDR_SET command requests one out scatterlist, it contains a
1749fbe302bSMichael S. Tsirkin  * 6 bytes MAC address. This functionality is present if the
1759fbe302bSMichael S. Tsirkin  * VIRTIO_NET_F_CTRL_MAC_ADDR feature is available.
1769fbe302bSMichael S. Tsirkin  */
1779fbe302bSMichael S. Tsirkin struct virtio_net_ctrl_mac {
1789fbe302bSMichael S. Tsirkin 	__virtio32 entries;
1799fbe302bSMichael S. Tsirkin 	uint8_t macs[][ETH_ALEN];
1809fbe302bSMichael S. Tsirkin } QEMU_PACKED;
1819fbe302bSMichael S. Tsirkin 
1829fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_MAC    1
1839fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MAC_TABLE_SET        0
1849fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MAC_ADDR_SET         1
1859fbe302bSMichael S. Tsirkin 
1869fbe302bSMichael S. Tsirkin /*
1879fbe302bSMichael S. Tsirkin  * Control VLAN filtering
1889fbe302bSMichael S. Tsirkin  *
1899fbe302bSMichael S. Tsirkin  * The VLAN filter table is controlled via a simple ADD/DEL interface.
1909fbe302bSMichael S. Tsirkin  * VLAN IDs not added may be filterd by the hypervisor.  Del is the
1919fbe302bSMichael S. Tsirkin  * opposite of add.  Both commands expect an out entry containing a 2
1929fbe302bSMichael S. Tsirkin  * byte VLAN ID.  VLAN filterting is available with the
1939fbe302bSMichael S. Tsirkin  * VIRTIO_NET_F_CTRL_VLAN feature bit.
1949fbe302bSMichael S. Tsirkin  */
1959fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_VLAN       2
1969fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_VLAN_ADD             0
1979fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_VLAN_DEL             1
1989fbe302bSMichael S. Tsirkin 
1999fbe302bSMichael S. Tsirkin /*
2009fbe302bSMichael S. Tsirkin  * Control link announce acknowledgement
2019fbe302bSMichael S. Tsirkin  *
2029fbe302bSMichael S. Tsirkin  * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that
2039fbe302bSMichael S. Tsirkin  * driver has recevied the notification; device would clear the
2049fbe302bSMichael S. Tsirkin  * VIRTIO_NET_S_ANNOUNCE bit in the status field after it receives
2059fbe302bSMichael S. Tsirkin  * this command.
2069fbe302bSMichael S. Tsirkin  */
2079fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_ANNOUNCE       3
2089fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_ANNOUNCE_ACK         0
2099fbe302bSMichael S. Tsirkin 
2109fbe302bSMichael S. Tsirkin /*
2119fbe302bSMichael S. Tsirkin  * Control Receive Flow Steering
2129fbe302bSMichael S. Tsirkin  *
2139fbe302bSMichael S. Tsirkin  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
2149fbe302bSMichael S. Tsirkin  * enables Receive Flow Steering, specifying the number of the transmit and
2159fbe302bSMichael S. Tsirkin  * receive queues that will be used. After the command is consumed and acked by
2169fbe302bSMichael S. Tsirkin  * the device, the device will not steer new packets on receive virtqueues
2179fbe302bSMichael S. Tsirkin  * other than specified nor read from transmit virtqueues other than specified.
2189fbe302bSMichael S. Tsirkin  * Accordingly, driver should not transmit new packets  on virtqueues other than
2199fbe302bSMichael S. Tsirkin  * specified.
2209fbe302bSMichael S. Tsirkin  */
2219fbe302bSMichael S. Tsirkin struct virtio_net_ctrl_mq {
2229fbe302bSMichael S. Tsirkin 	__virtio16 virtqueue_pairs;
2239fbe302bSMichael S. Tsirkin };
2249fbe302bSMichael S. Tsirkin 
2259fbe302bSMichael S. Tsirkin #define VIRTIO_NET_CTRL_MQ   4
2269fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
2279fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
2289fbe302bSMichael S. Tsirkin  #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
2299fbe302bSMichael S. Tsirkin 
230*f56fc2d3SMichael S. Tsirkin /*
231*f56fc2d3SMichael S. Tsirkin  * Control network offloads
232*f56fc2d3SMichael S. Tsirkin  *
233*f56fc2d3SMichael S. Tsirkin  * Reconfigures the network offloads that Guest can handle.
234*f56fc2d3SMichael S. Tsirkin  *
235*f56fc2d3SMichael S. Tsirkin  * Available with the VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit.
236*f56fc2d3SMichael S. Tsirkin  *
237*f56fc2d3SMichael S. Tsirkin  * Command data format matches the feature bit mask exactly.
238*f56fc2d3SMichael S. Tsirkin  *
239*f56fc2d3SMichael S. Tsirkin  * See VIRTIO_NET_F_GUEST_* for the list of offloads
240*f56fc2d3SMichael S. Tsirkin  * that can be enabled/disabled.
241*f56fc2d3SMichael S. Tsirkin  */
242*f56fc2d3SMichael S. Tsirkin #define VIRTIO_NET_CTRL_GUEST_OFFLOADS   5
243*f56fc2d3SMichael S. Tsirkin #define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET        0
244*f56fc2d3SMichael S. Tsirkin 
2459fbe302bSMichael S. Tsirkin #endif /* _LINUX_VIRTIO_NET_H */
246