2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
18 #ifndef _VNET_DEVICES_VIRTIO_VIRTIO_H_
19 #define _VNET_DEVICES_VIRTIO_VIRTIO_H_
21 #include <vnet/devices/virtio/virtio_std.h>
22 #include <vnet/devices/virtio/vhost_std.h>
23 #include <vnet/devices/virtio/virtio_buffering.h>
24 #include <vnet/gso/gro.h>
26 #define foreach_virtio_if_flag \
27 _(0, ADMIN_UP, "admin-up") \
28 _(1, DELETING, "deleting")
32 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
33 foreach_virtio_if_flag
37 #define TX_QUEUE(X) ((X*2) + 1)
38 #define RX_QUEUE(X) (X*2)
39 #define TX_QUEUE_ACCESS(X) (X/2)
40 #define RX_QUEUE_ACCESS(X) (X/2)
42 #define VIRTIO_NUM_RX_DESC 256
43 #define VIRTIO_NUM_TX_DESC 256
45 #define foreach_virtio_if_types \
52 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
53 foreach_virtio_if_types
55 VIRTIO_IF_N_TYPES = (1 << 3),
58 #define VIRTIO_RING_FLAG_MASK_INT 1
60 #define VIRTIO_EVENT_START_TIMER 1
61 #define VIRTIO_EVENT_STOP_TIMER 2
65 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
66 clib_spinlock_t lockp;
77 vring_packed_desc_t *packed_desc;
78 vring_desc_event_t *driver_event;
79 vring_desc_event_t *device_event;
89 u16 last_kick_avail_idx;
100 u16 avail_wrap_counter;
101 u16 used_wrap_counter;
102 u16 queue_notify_offset;
105 #define VRING_TX_OUT_OF_ORDER 1
107 u8 buffer_pool_index;
108 vnet_hw_if_rx_mode mode;
109 virtio_vring_buffering_t *buffering;
110 gro_flow_table_t *flow_table;
125 /* forward declaration */
126 typedef struct _virtio_pci_func virtio_pci_func_t;
130 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
133 u32 per_interface_next_index;
136 virtio_vring_t *rxq_vrings;
137 virtio_vring_t *txq_vrings;
139 int csum_offload_enabled;
149 u16 virtio_net_hdr_sz;
150 virtio_if_type_t type;
155 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
157 int packet_buffering;
175 struct /* tun/tap interface */
177 ip6_address_t host_ip6_addr;
187 ip4_address_t host_ip4_addr;
188 u8 host_ip4_prefix_len;
189 u8 host_ip6_prefix_len;
190 u8 host_carrier_up; /* host tun/tap driver link carrier state */
192 struct /* native virtio */
195 virtio_vring_t *cxq_vring;
198 u32 notify_off_multiplier;
206 u8 support_int_mode; /* support interrupt mode */
210 const virtio_pci_func_t *virtio_pci_func;
216 u32 interrupt_queues_count;
218 vlib_log_class_t log_default;
220 virtio_if_t *interfaces;
223 extern virtio_main_t virtio_main;
224 extern vnet_device_class_t virtio_device_class;
225 extern vlib_node_registration_t virtio_input_node;
226 extern vlib_node_registration_t virtio_send_interrupt_node;
228 clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
230 clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
232 clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
234 void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif);
235 extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring);
236 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
237 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
239 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
240 clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
241 extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
242 virtio_if_t * vif, u16 queue_id,
243 u16 queue_notify_offset);
244 extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
245 virtio_if_t * vif, u16 queue_id,
246 u16 queue_notify_offset);
247 format_function_t format_virtio_device_name;
248 format_function_t format_virtio_log_name;
250 static_always_inline void
251 virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
253 if (vif->type == VIRTIO_IF_TYPE_PCI)
256 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
257 vring->queue_notify_offset);
259 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
260 vring->queue_notify_offset);
267 r = write (vring->kick_fd, &x, sizeof (x));
268 vring->last_kick_avail_idx = vring->avail->idx;
272 #define virtio_log_debug(vif, f, ...) \
274 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
275 "%U: " f, format_virtio_log_name, vif, \
279 #define virtio_log_warning(vif, f, ...) \
281 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
282 "%U: " f, format_virtio_log_name, vif, \
286 #define virtio_log_error(vif, f, ...) \
288 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
289 "%U: " f, format_virtio_log_name, vif, \
293 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
296 * fd.io coding-style-patch-verification: ON
299 * eval: (c-set-style "gnu")