Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : * Copyright(c) 2021 Red Hat, Inc.
4 : : */
5 : :
6 : : #ifndef _VIRTIO_H_
7 : : #define _VIRTIO_H_
8 : :
9 : : #include <rte_ether.h>
10 : :
11 : : /* The feature bitmap for virtio net */
12 : : #define VIRTIO_NET_F_CSUM 0 /* Host handles pkts w/ partial csum */
13 : : #define VIRTIO_NET_F_GUEST_CSUM 1 /* Guest handles pkts w/ partial csum */
14 : : #define VIRTIO_NET_F_MTU 3 /* Initial MTU advice. */
15 : : #define VIRTIO_NET_F_MAC 5 /* Host has given MAC address. */
16 : : #define VIRTIO_NET_F_GUEST_TSO4 7 /* Guest can handle TSOv4 in. */
17 : : #define VIRTIO_NET_F_GUEST_TSO6 8 /* Guest can handle TSOv6 in. */
18 : : #define VIRTIO_NET_F_GUEST_ECN 9 /* Guest can handle TSO[6] w/ ECN in. */
19 : : #define VIRTIO_NET_F_GUEST_UFO 10 /* Guest can handle UFO in. */
20 : : #define VIRTIO_NET_F_HOST_TSO4 11 /* Host can handle TSOv4 in. */
21 : : #define VIRTIO_NET_F_HOST_TSO6 12 /* Host can handle TSOv6 in. */
22 : : #define VIRTIO_NET_F_HOST_ECN 13 /* Host can handle TSO[6] w/ ECN in. */
23 : : #define VIRTIO_NET_F_HOST_UFO 14 /* Host can handle UFO in. */
24 : : #define VIRTIO_NET_F_MRG_RXBUF 15 /* Host can merge receive buffers. */
25 : : #define VIRTIO_NET_F_STATUS 16 /* virtio_net_config.status available */
26 : : #define VIRTIO_NET_F_CTRL_VQ 17 /* Control channel available */
27 : : #define VIRTIO_NET_F_CTRL_RX 18 /* Control channel RX mode support */
28 : : #define VIRTIO_NET_F_CTRL_VLAN 19 /* Control channel VLAN filtering */
29 : : #define VIRTIO_NET_F_CTRL_RX_EXTRA 20 /* Extra RX mode control support */
30 : : #define VIRTIO_NET_F_GUEST_ANNOUNCE 21 /* Guest can announce device on the network */
31 : : #define VIRTIO_NET_F_MQ 22 /* Device supports Receive Flow Steering */
32 : : #define VIRTIO_NET_F_CTRL_MAC_ADDR 23 /* Set MAC address */
33 : : #define VIRTIO_NET_F_HASH_REPORT 57 /* Supports hash report */
34 : : #define VIRTIO_NET_F_RSS 60 /* RSS supported */
35 : :
36 : : /*
37 : : * Do we get callbacks when the ring is completely used,
38 : : * even if we've suppressed them?
39 : : */
40 : : #define VIRTIO_F_NOTIFY_ON_EMPTY 24
41 : :
42 : : /* Can the device handle any descriptor layout? */
43 : : #define VIRTIO_F_ANY_LAYOUT 27
44 : :
45 : : /* We support indirect buffer descriptors */
46 : : #define VIRTIO_RING_F_INDIRECT_DESC 28
47 : :
48 : : #define VIRTIO_F_VERSION_1 32
49 : : #define VIRTIO_F_IOMMU_PLATFORM 33
50 : : #define VIRTIO_F_RING_PACKED 34
51 : :
52 : : /*
53 : : * Some VirtIO feature bits (currently bits 28 through 31) are
54 : : * reserved for the transport being used (eg. virtio_ring), the
55 : : * rest are per-device feature bits.
56 : : */
57 : : #define VIRTIO_TRANSPORT_F_START 28
58 : : #define VIRTIO_TRANSPORT_F_END 34
59 : :
60 : : /*
61 : : * Inorder feature indicates that all buffers are used by the device
62 : : * in the same order in which they have been made available.
63 : : */
64 : : #define VIRTIO_F_IN_ORDER 35
65 : :
66 : : /*
67 : : * This feature indicates that memory accesses by the driver and the device
68 : : * are ordered in a way described by the platform.
69 : : */
70 : : #define VIRTIO_F_ORDER_PLATFORM 36
71 : :
72 : : /*
73 : : * This feature indicates that the driver passes extra data (besides
74 : : * identifying the virtqueue) in its device notifications.
75 : : */
76 : : #define VIRTIO_F_NOTIFICATION_DATA 38
77 : :
78 : : /* Device set linkspeed and duplex */
79 : : #define VIRTIO_NET_F_SPEED_DUPLEX 63
80 : :
81 : : /*
82 : : * The Guest publishes the used index for which it expects an interrupt
83 : : * at the end of the avail ring. Host should ignore the avail->flags field
84 : : *
85 : : * The Host publishes the avail index for which it expects a kick
86 : : * at the end of the used ring. Guest should ignore the used->flags field.
87 : : */
88 : : #define VIRTIO_RING_F_EVENT_IDX 29
89 : :
90 : : #define VIRTIO_NET_S_LINK_UP 1 /* Link is up */
91 : : #define VIRTIO_NET_S_ANNOUNCE 2 /* Announcement is needed */
92 : :
93 : : /*
94 : : * Each virtqueue indirect descriptor list must be physically contiguous.
95 : : * To allow us to malloc(9) each list individually, limit the number
96 : : * supported to what will fit in one page. With 4KB pages, this is a limit
97 : : * of 256 descriptors. If there is ever a need for more, we can switch to
98 : : * contigmalloc(9) for the larger allocations, similar to what
99 : : * bus_dmamem_alloc(9) does.
100 : : *
101 : : * Note the sizeof(struct vring_desc) is 16 bytes.
102 : : */
103 : : #define VIRTIO_MAX_INDIRECT ((int)(rte_mem_page_size() / 16))
104 : :
105 : : /* Virtio RSS hash types */
106 : : #define VIRTIO_NET_HASH_TYPE_IPV4 RTE_BIT32(0)
107 : : #define VIRTIO_NET_HASH_TYPE_TCPV4 RTE_BIT32(1)
108 : : #define VIRTIO_NET_HASH_TYPE_UDPV4 RTE_BIT32(2)
109 : : #define VIRTIO_NET_HASH_TYPE_IPV6 RTE_BIT32(3)
110 : : #define VIRTIO_NET_HASH_TYPE_TCPV6 RTE_BIT32(4)
111 : : #define VIRTIO_NET_HASH_TYPE_UDPV6 RTE_BIT32(5)
112 : : #define VIRTIO_NET_HASH_TYPE_IP_EX RTE_BIT32(6)
113 : : #define VIRTIO_NET_HASH_TYPE_TCP_EX RTE_BIT32(7)
114 : : #define VIRTIO_NET_HASH_TYPE_UDP_EX RTE_BIT32(8)
115 : :
116 : : #define VIRTIO_NET_HASH_TYPE_MASK ( \
117 : : VIRTIO_NET_HASH_TYPE_IPV4 | \
118 : : VIRTIO_NET_HASH_TYPE_TCPV4 | \
119 : : VIRTIO_NET_HASH_TYPE_UDPV4 | \
120 : : VIRTIO_NET_HASH_TYPE_IPV6 | \
121 : : VIRTIO_NET_HASH_TYPE_TCPV6 | \
122 : : VIRTIO_NET_HASH_TYPE_UDPV6 | \
123 : : VIRTIO_NET_HASH_TYPE_IP_EX | \
124 : : VIRTIO_NET_HASH_TYPE_TCP_EX | \
125 : : VIRTIO_NET_HASH_TYPE_UDP_EX)
126 : :
127 : :
128 : : /* VirtIO device IDs. */
129 : : #define VIRTIO_ID_NETWORK 0x01
130 : : #define VIRTIO_ID_BLOCK 0x02
131 : : #define VIRTIO_ID_CONSOLE 0x03
132 : : #define VIRTIO_ID_ENTROPY 0x04
133 : : #define VIRTIO_ID_BALLOON 0x05
134 : : #define VIRTIO_ID_IOMEMORY 0x06
135 : : #define VIRTIO_ID_9P 0x09
136 : :
137 : : /* Status byte for guest to report progress. */
138 : : #define VIRTIO_CONFIG_STATUS_RESET 0x00
139 : : #define VIRTIO_CONFIG_STATUS_ACK 0x01
140 : : #define VIRTIO_CONFIG_STATUS_DRIVER 0x02
141 : : #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04
142 : : #define VIRTIO_CONFIG_STATUS_FEATURES_OK 0x08
143 : : #define VIRTIO_CONFIG_STATUS_DEV_NEED_RESET 0x40
144 : : #define VIRTIO_CONFIG_STATUS_FAILED 0x80
145 : :
146 : : /* The bit of the ISR which indicates a device has an interrupt. */
147 : : #define VIRTIO_ISR_INTR 0x1
148 : : /* The bit of the ISR which indicates a device configuration change. */
149 : : #define VIRTIO_ISR_CONFIG 0x2
150 : : /* Vector value used to disable MSI for queue. */
151 : : #define VIRTIO_MSI_NO_VECTOR 0xFFFF
152 : :
153 : : /* The alignment to use between consumer and producer parts of vring. */
154 : : #define VIRTIO_VRING_ALIGN 4096
155 : :
156 : : /*
157 : : * This structure is just a reference to read net device specific
158 : : * config space; it is just a shadow structure.
159 : : *
160 : : */
161 : : struct __rte_packed_begin virtio_net_config {
162 : : /* The config defining mac address (if VIRTIO_NET_F_MAC) */
163 : : uint8_t mac[RTE_ETHER_ADDR_LEN];
164 : : /* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
165 : : uint16_t status;
166 : : uint16_t max_virtqueue_pairs;
167 : : uint16_t mtu;
168 : : /*
169 : : * speed, in units of 1Mb. All values 0 to INT_MAX are legal.
170 : : * Any other value stands for unknown.
171 : : */
172 : : uint32_t speed;
173 : : /*
174 : : * 0x00 - half duplex
175 : : * 0x01 - full duplex
176 : : * Any other value stands for unknown.
177 : : */
178 : : uint8_t duplex;
179 : : uint8_t rss_max_key_size;
180 : : uint16_t rss_max_indirection_table_length;
181 : : uint32_t supported_hash_types;
182 : : } __rte_packed_end;
183 : :
184 : : struct virtio_hw {
185 : : struct virtqueue **vqs;
186 : : uint64_t guest_features;
187 : : uint16_t vtnet_hdr_size;
188 : : uint8_t started;
189 : : uint8_t weak_barriers;
190 : : uint8_t vlan_strip;
191 : : uint8_t has_hash_report;
192 : : bool rx_ol_scatter;
193 : : uint8_t has_tx_offload;
194 : : uint8_t has_rx_offload;
195 : : uint8_t use_vec_rx;
196 : : uint8_t use_vec_tx;
197 : : uint8_t use_inorder_rx;
198 : : uint8_t use_inorder_tx;
199 : : uint8_t opened;
200 : : uint16_t port_id;
201 : : uint8_t mac_addr[RTE_ETHER_ADDR_LEN];
202 : : /*
203 : : * Speed is specified via 'speed' devarg or
204 : : * negotiated via VIRTIO_NET_F_SPEED_DUPLEX
205 : : */
206 : : bool get_speed_via_feat;
207 : : uint32_t speed; /* link speed in MB */
208 : : uint8_t duplex;
209 : : uint8_t intr_lsc;
210 : : uint16_t max_mtu;
211 : : size_t max_rx_pkt_len;
212 : : /*
213 : : * App management thread and virtio interrupt handler thread
214 : : * both can change device state, this lock is meant to avoid
215 : : * such a contention.
216 : : */
217 : : rte_spinlock_t state_lock;
218 : : struct rte_mbuf **inject_pkts;
219 : : uint16_t max_queue_pairs;
220 : : uint16_t rss_rx_queues;
221 : : uint32_t rss_hash_types;
222 : : uint16_t *rss_reta;
223 : : uint8_t *rss_key;
224 : : uint64_t req_guest_features;
225 : : struct virtnet_ctl *cvq;
226 : : bool use_va;
227 : : };
228 : :
229 : : struct virtio_ops {
230 : : void (*read_dev_cfg)(struct virtio_hw *hw, size_t offset, void *dst, int len);
231 : : void (*write_dev_cfg)(struct virtio_hw *hw, size_t offset, const void *src, int len);
232 : : uint8_t (*get_status)(struct virtio_hw *hw);
233 : : void (*set_status)(struct virtio_hw *hw, uint8_t status);
234 : : uint64_t (*get_features)(struct virtio_hw *hw);
235 : : void (*set_features)(struct virtio_hw *hw, uint64_t features);
236 : : int (*features_ok)(struct virtio_hw *hw);
237 : : uint8_t (*get_isr)(struct virtio_hw *hw);
238 : : uint16_t (*set_config_irq)(struct virtio_hw *hw, uint16_t vec);
239 : : uint16_t (*set_queue_irq)(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec);
240 : : uint16_t (*get_queue_num)(struct virtio_hw *hw, uint16_t queue_id);
241 : : int (*setup_queue)(struct virtio_hw *hw, struct virtqueue *vq);
242 : : void (*del_queue)(struct virtio_hw *hw, struct virtqueue *vq);
243 : : void (*notify_queue)(struct virtio_hw *hw, struct virtqueue *vq);
244 : : void (*intr_detect)(struct virtio_hw *hw);
245 : : int (*dev_close)(struct virtio_hw *hw);
246 : : };
247 : :
248 : : /*
249 : : * This structure stores per-process data. Only virtio_ops for now.
250 : : */
251 : : struct virtio_hw_internal {
252 : : const struct virtio_ops *virtio_ops;
253 : : };
254 : :
255 : : #define VIRTIO_OPS(hw) (virtio_hw_internal[(hw)->port_id].virtio_ops)
256 : :
257 : : extern struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
258 : :
259 : :
260 : : static inline int
261 : : virtio_with_feature(struct virtio_hw *hw, uint64_t bit)
262 : : {
263 [ # # # # : 0 : return (hw->guest_features & (1ULL << bit)) != 0;
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
264 : : }
265 : :
266 : : static inline int
267 : : virtio_with_packed_queue(struct virtio_hw *hw)
268 : : {
269 : : return virtio_with_feature(hw, VIRTIO_F_RING_PACKED);
270 : : }
271 : :
272 : : uint64_t virtio_negotiate_features(struct virtio_hw *hw, uint64_t host_features);
273 : : uint8_t virtio_get_status(struct virtio_hw *hw);
274 : : void virtio_set_status(struct virtio_hw *hw, uint8_t status);
275 : : void virtio_write_dev_config(struct virtio_hw *hw, size_t offset, const void *src, int length);
276 : : void virtio_read_dev_config(struct virtio_hw *hw, size_t offset, void *dst, int length);
277 : : void virtio_reset(struct virtio_hw *hw);
278 : : void virtio_reinit_complete(struct virtio_hw *hw);
279 : : uint8_t virtio_get_isr(struct virtio_hw *hw);
280 : : #endif /* _VIRTIO_H_ */
|