virtio_net.h 8.7 KB

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