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