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>
25 #include <vnet/interface.h>
27 #define foreach_virtio_if_flag \
28 _(0, ADMIN_UP, "admin-up") \
29 _(1, DELETING, "deleting")
33 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
34 foreach_virtio_if_flag
38 #define TX_QUEUE(X) ((X*2) + 1)
39 #define RX_QUEUE(X) (X*2)
40 #define TX_QUEUE_ACCESS(X) (X/2)
41 #define RX_QUEUE_ACCESS(X) (X/2)
43 #define VIRTIO_NUM_RX_DESC 256
44 #define VIRTIO_NUM_TX_DESC 256
46 #define foreach_virtio_if_types \
53 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
54 foreach_virtio_if_types
56 VIRTIO_IF_N_TYPES = (1 << 3),
59 #define VIRTIO_RING_FLAG_MASK_INT 1
61 #define VIRTIO_EVENT_START_TIMER 1
62 #define VIRTIO_EVENT_STOP_TIMER 2
66 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
67 clib_spinlock_t lockp;
78 vring_packed_desc_t *packed_desc;
79 vring_desc_event_t *driver_event;
80 vring_desc_event_t *device_event;
90 u16 last_kick_avail_idx;
101 u16 avail_wrap_counter;
102 u16 used_wrap_counter;
103 u16 queue_notify_offset;
106 #define VRING_TX_OUT_OF_ORDER 1
107 #define VRING_TX_SCHEDULED 2
109 u8 buffer_pool_index;
110 vnet_hw_if_rx_mode mode;
111 virtio_vring_buffering_t *buffering;
112 gro_flow_table_t *flow_table;
127 /* forward declaration */
128 typedef struct _virtio_pci_func virtio_pci_func_t;
132 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
135 u32 per_interface_next_index;
138 virtio_vring_t *rxq_vrings;
139 virtio_vring_t *txq_vrings;
141 int csum_offload_enabled;
151 u16 virtio_net_hdr_sz;
152 virtio_if_type_t type;
157 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
159 int packet_buffering;
177 struct /* tun/tap interface */
179 ip6_address_t host_ip6_addr;
189 ip4_address_t host_ip4_addr;
190 u8 host_ip4_prefix_len;
191 u8 host_ip6_prefix_len;
192 u8 host_carrier_up; /* host tun/tap driver link carrier state */
194 struct /* native virtio */
197 virtio_vring_t *cxq_vring;
200 u32 notify_off_multiplier;
208 u8 support_int_mode; /* support interrupt mode */
212 const virtio_pci_func_t *virtio_pci_func;
218 u32 gro_or_buffering_if_count;
220 vlib_log_class_t log_default;
222 virtio_if_t *interfaces;
226 extern virtio_main_t virtio_main;
227 extern vnet_device_class_t virtio_device_class;
228 extern vlib_node_registration_t virtio_input_node;
230 clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
232 clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
234 clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
236 void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif);
237 void virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif);
238 extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring);
239 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
240 extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
241 virtio_if_type_t type);
242 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
243 clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
244 extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
245 virtio_if_t * vif, u16 queue_id,
246 u16 queue_notify_offset);
247 extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
248 virtio_if_t * vif, u16 queue_id,
249 u16 queue_notify_offset);
250 extern void virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif);
251 extern void virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif);
253 format_function_t format_virtio_device_name;
254 format_function_t format_virtio_log_name;
256 static_always_inline void
257 virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
259 if (vif->type == VIRTIO_IF_TYPE_PCI)
262 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
263 vring->queue_notify_offset);
265 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
266 vring->queue_notify_offset);
273 r = write (vring->kick_fd, &x, sizeof (x));
274 vring->last_kick_avail_idx = vring->avail->idx;
278 static_always_inline u8
279 virtio_txq_is_scheduled (virtio_vring_t *vring)
282 return (vring->flags & VRING_TX_SCHEDULED);
286 static_always_inline void
287 virtio_txq_set_scheduled (virtio_vring_t *vring)
290 vring->flags |= VRING_TX_SCHEDULED;
293 static_always_inline void
294 virtio_txq_clear_scheduled (virtio_vring_t *vring)
297 vring->flags &= ~VRING_TX_SCHEDULED;
300 #define virtio_log_debug(vif, f, ...) \
302 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
303 "%U: " f, format_virtio_log_name, vif, \
307 #define virtio_log_warning(vif, f, ...) \
309 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
310 "%U: " f, format_virtio_log_name, vif, \
314 #define virtio_log_error(vif, f, ...) \
316 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
317 "%U: " f, format_virtio_log_name, vif, \
321 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
324 * fd.io coding-style-patch-verification: ON
327 * eval: (c-set-style "gnu")