1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4  * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5  *
6  * From Linux kernel include/uapi/linux/virtio_net.h
7  */
8 
9 #ifndef _LINUX_VIRTIO_NET_H
10 #define _LINUX_VIRTIO_NET_H
11 
12 /* TODO: needs to be removed! */
13 #define ETH_ALEN				6
14 
15 /* The feature bitmap for virtio net */
16 
17 /* Host handles pkts w/ partial csum */
18 #define VIRTIO_NET_F_CSUM			0
19 /* Guest handles pkts w/ partial csum */
20 #define VIRTIO_NET_F_GUEST_CSUM			1
21 /* Dynamic offload configuration */
22 #define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS	2
23 /* Initial MTU advice */
24 #define VIRTIO_NET_F_MTU			3
25 /* Host has given MAC address */
26 #define VIRTIO_NET_F_MAC			5
27 /* Guest can handle TSOv4 in */
28 #define VIRTIO_NET_F_GUEST_TSO4			7
29 /* Guest can handle TSOv6 in */
30 #define VIRTIO_NET_F_GUEST_TSO6			8
31 /* Guest can handle TSO[6] w/ ECN in */
32 #define VIRTIO_NET_F_GUEST_ECN			9
33 /* Guest can handle UFO in */
34 #define VIRTIO_NET_F_GUEST_UFO			10
35 /* Host can handle TSOv4 in */
36 #define VIRTIO_NET_F_HOST_TSO4			11
37 /* Host can handle TSOv6 in */
38 #define VIRTIO_NET_F_HOST_TSO6			12
39 /* Host can handle TSO[6] w/ ECN in */
40 #define VIRTIO_NET_F_HOST_ECN			13
41 /* Host can handle UFO in */
42 #define VIRTIO_NET_F_HOST_UFO			14
43 /* Host can merge receive buffers */
44 #define VIRTIO_NET_F_MRG_RXBUF			15
45 /* virtio_net_config.status available */
46 #define VIRTIO_NET_F_STATUS			16
47 /* Control channel available */
48 #define VIRTIO_NET_F_CTRL_VQ			17
49 /* Control channel RX mode support */
50 #define VIRTIO_NET_F_CTRL_RX			18
51 /* Control channel VLAN filtering */
52 #define VIRTIO_NET_F_CTRL_VLAN			19
53 /* Extra RX mode control support */
54 #define VIRTIO_NET_F_CTRL_RX_EXTRA		20
55 /* Guest can announce device on the network */
56 #define VIRTIO_NET_F_GUEST_ANNOUNCE		21
57 /* Device supports receive flow steering */
58 #define VIRTIO_NET_F_MQ				22
59 /* Set MAC address */
60 #define VIRTIO_NET_F_CTRL_MAC_ADDR		23
61 /* Device set linkspeed and duplex */
62 #define VIRTIO_NET_F_SPEED_DUPLEX		63
63 
64 #ifndef VIRTIO_NET_NO_LEGACY
65 /* Host handles pkts w/ any GSO type */
66 #define VIRTIO_NET_F_GSO			6
67 #endif /* VIRTIO_NET_NO_LEGACY */
68 
69 #define VIRTIO_NET_S_LINK_UP			1 /* Link is up */
70 #define VIRTIO_NET_S_ANNOUNCE			2 /* Announcement is needed */
71 
72 struct __packed virtio_net_config {
73 	/* The config defining mac address (if VIRTIO_NET_F_MAC) */
74 	__u8 mac[ETH_ALEN];
75 	/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
76 	__u16 status;
77 	/*
78 	 * Maximum number of each of transmit and receive queues;
79 	 * see VIRTIO_NET_F_MQ and VIRTIO_NET_CTRL_MQ.
80 	 * Legal values are between 1 and 0x8000
81 	 */
82 	__u16 max_virtqueue_pairs;
83 	/* Default maximum transmit unit advice */
84 	__u16 mtu;
85 	/*
86 	 * speed, in units of 1Mb. All values 0 to INT_MAX are legal.
87 	 * Any other value stands for unknown.
88 	 */
89 	__u32 speed;
90 	/*
91 	 * 0x00 - half duplex
92 	 * 0x01 - full duplex
93 	 * Any other value stands for unknown.
94 	 */
95 	__u8 duplex;
96 };
97 
98 /*
99  * This header comes first in the scatter-gather list. If you don't
100  * specify GSO or CSUM features, you can simply ignore the header.
101  *
102  * This is bitwise-equivalent to the legacy struct virtio_net_hdr_mrg_rxbuf,
103  * only flattened.
104  */
105 struct virtio_net_hdr_v1 {
106 #define VIRTIO_NET_HDR_F_NEEDS_CSUM	0x01 /* Use csum_start, csum_offset */
107 #define VIRTIO_NET_HDR_F_DATA_VALID	0x02 /* Csum is valid */
108 	__u8 flags;
109 #define VIRTIO_NET_HDR_GSO_NONE		0x00 /* Not a GSO frame */
110 #define VIRTIO_NET_HDR_GSO_TCPV4	0x01 /* GSO frame, IPv4 TCP (TSO) */
111 #define VIRTIO_NET_HDR_GSO_UDP		0x03 /* GSO frame, IPv4 UDP (UFO) */
112 #define VIRTIO_NET_HDR_GSO_TCPV6	0x04 /* GSO frame, IPv6 TCP */
113 #define VIRTIO_NET_HDR_GSO_ECN		0x80 /* TCP has ECN set */
114 	__u8 gso_type;
115 	__virtio16 hdr_len;	/* Ethernet + IP + tcp/udp hdrs */
116 	__virtio16 gso_size;	/* Bytes to append to hdr_len per frame */
117 	__virtio16 csum_start;	/* Position to start checksumming from */
118 	__virtio16 csum_offset;	/* Offset after that to place checksum */
119 	__virtio16 num_buffers;	/* Number of merged rx buffers */
120 };
121 
122 #ifndef VIRTIO_NET_NO_LEGACY
123 /*
124  * This header comes first in the scatter-gather list.
125  *
126  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, it must
127  * be the first element of the scatter-gather list. If you don't
128  * specify GSO or CSUM features, you can simply ignore the header.
129  */
130 struct virtio_net_hdr {
131 	/* See VIRTIO_NET_HDR_F_* */
132 	__u8 flags;
133 	/* See VIRTIO_NET_HDR_GSO_* */
134 	__u8 gso_type;
135 	__virtio16 hdr_len;	/* Ethernet + IP + tcp/udp hdrs */
136 	__virtio16 gso_size;	/* Bytes to append to hdr_len per frame */
137 	__virtio16 csum_start;	/* Position to start checksumming from */
138 	__virtio16 csum_offset;	/* Offset after that to place checksum */
139 };
140 
141 /*
142  * This is the version of the header to use when the MRG_RXBUF
143  * feature has been negotiated.
144  */
145 struct virtio_net_hdr_mrg_rxbuf {
146 	struct virtio_net_hdr hdr;
147 	__virtio16 num_buffers;	/* Number of merged rx buffers */
148 };
149 #endif /* ...VIRTIO_NET_NO_LEGACY */
150 
151 /*
152  * Control virtqueue data structures
153  *
154  * The control virtqueue expects a header in the first sg entry
155  * and an ack/status response in the last entry.  Data for the
156  * command goes in between.
157  */
158 struct __packed virtio_net_ctrl_hdr {
159 	__u8 class;
160 	__u8 cmd;
161 };
162 
163 typedef __u8 virtio_net_ctrl_ack;
164 
165 #define VIRTIO_NET_OK				0
166 #define VIRTIO_NET_ERR				1
167 
168 /*
169  * Control the RX mode, ie. promisucous, allmulti, etc...
170  *
171  * All commands require an "out" sg entry containing a 1 byte state value,
172  * zero = disable, non-zero = enable.
173  *
174  * Commands 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature.
175  * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA.
176  */
177 #define VIRTIO_NET_CTRL_RX			0
178 #define VIRTIO_NET_CTRL_RX_PROMISC		0
179 #define VIRTIO_NET_CTRL_RX_ALLMULTI		1
180 #define VIRTIO_NET_CTRL_RX_ALLUNI		2
181 #define VIRTIO_NET_CTRL_RX_NOMULTI		3
182 #define VIRTIO_NET_CTRL_RX_NOUNI		4
183 #define VIRTIO_NET_CTRL_RX_NOBCAST		5
184 
185 /*
186  * Control the MAC
187  *
188  * The MAC filter table is managed by the hypervisor, the guest should assume
189  * the size is infinite. Filtering should be considered non-perfect, ie. based
190  * on hypervisor resources, the guest may received packets from sources not
191  * specified in the filter list.
192  *
193  * In addition to the class/cmd header, the TABLE_SET command requires two
194  * out scatterlists. Each contains a 4 byte count of entries followed by a
195  * concatenated byte stream of the ETH_ALEN MAC addresses.  The first sg list
196  * contains unicast addresses, the second is for multicast. This functionality
197  * is present if the VIRTIO_NET_F_CTRL_RX feature is available.
198  *
199  * The ADDR_SET command requests one out scatterlist, it contains a 6 bytes MAC
200  * address. This functionality is present if the VIRTIO_NET_F_CTRL_MAC_ADDR
201  * feature is available.
202  */
203 struct __packed virtio_net_ctrl_mac {
204 	__virtio32 entries;
205 	__u8 macs[][ETH_ALEN];
206 };
207 
208 #define VIRTIO_NET_CTRL_MAC			1
209 #define VIRTIO_NET_CTRL_MAC_TABLE_SET		0
210 #define VIRTIO_NET_CTRL_MAC_ADDR_SET		1
211 
212 /*
213  * Control VLAN filtering
214  *
215  * The VLAN filter table is controlled via a simple ADD/DEL interface. VLAN IDs
216  * not added may be filterd by the hypervisor. Del is the opposite of add. Both
217  * commands expect an out entry containing a 2 byte VLAN ID. VLAN filterting is
218  * available with the VIRTIO_NET_F_CTRL_VLAN feature bit.
219  */
220 #define VIRTIO_NET_CTRL_VLAN			2
221 #define VIRTIO_NET_CTRL_VLAN_ADD		0
222 #define VIRTIO_NET_CTRL_VLAN_DEL		1
223 
224 /*
225  * Control link announce acknowledgment
226  *
227  * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that driver has
228  * recevied the notification; device would clear the VIRTIO_NET_S_ANNOUNCE bit
229  * in the status field after it receives this command.
230  */
231 #define VIRTIO_NET_CTRL_ANNOUNCE		3
232 #define VIRTIO_NET_CTRL_ANNOUNCE_ACK		0
233 
234 /*
235  * Control receive flow steering
236  *
237  * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET enables receive flow steering,
238  * specifying the number of the transmit and receive queues that will be used.
239  * After the command is consumed and acked by the device, the device will not
240  * steer new packets on receive virtqueues other than specified nor read from
241  * transmit virtqueues other than specified. Accordingly, driver should not
242  * transmit new packets  on virtqueues other than specified.
243  */
244 struct virtio_net_ctrl_mq {
245 	__virtio16 virtqueue_pairs;
246 };
247 
248 #define VIRTIO_NET_CTRL_MQ			4
249 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET		0
250 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN		1
251 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX		0x8000
252 
253 /*
254  * Control network offloads
255  *
256  * Reconfigures the network offloads that guest can handle.
257  *
258  * Available with the VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit.
259  *
260  * Command data format matches the feature bit mask exactly.
261  *
262  * See VIRTIO_NET_F_GUEST_* for the list of offloads
263  * that can be enabled/disabled.
264  */
265 #define VIRTIO_NET_CTRL_GUEST_OFFLOADS		5
266 #define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET	0
267 
268 #endif /* _LINUX_VIRTIO_NET_H */
269