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/gso/gro.h>
25 #define foreach_virtio_if_flag \
26 _(0, ADMIN_UP, "admin-up") \
27 _(1, DELETING, "deleting")
31 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
32 foreach_virtio_if_flag
36 #define TX_QUEUE(X) ((X*2) + 1)
37 #define RX_QUEUE(X) (X*2)
38 #define TX_QUEUE_ACCESS(X) (X/2)
39 #define RX_QUEUE_ACCESS(X) (X/2)
41 #define VIRTIO_NUM_RX_DESC 256
42 #define VIRTIO_NUM_TX_DESC 256
44 #define foreach_virtio_if_types \
51 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
52 foreach_virtio_if_types
54 VIRTIO_IF_N_TYPES = (1 << 3),
57 #define VIRTIO_RING_FLAG_MASK_INT 1
61 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
62 clib_spinlock_t lockp;
73 #define VRING_TX_OUT_OF_ORDER 1
77 u16 last_kick_avail_idx;
79 gro_flow_table_t *flow_table;
94 /* forward declaration */
95 typedef struct _virtio_pci_func virtio_pci_func_t;
99 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
102 u32 per_interface_next_index;
105 virtio_vring_t *rxq_vrings;
106 virtio_vring_t *txq_vrings;
108 int csum_offload_enabled;
118 u16 virtio_net_hdr_sz;
119 virtio_if_type_t type;
124 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
143 struct /* tun/tap interface */
145 ip6_address_t host_ip6_addr;
155 ip4_address_t host_ip4_addr;
156 u8 host_ip4_prefix_len;
157 u8 host_ip6_prefix_len;
159 struct /* native virtio */
162 virtio_vring_t *cxq_vring;
165 u32 notify_off_multiplier;
173 u8 support_int_mode; /* support interrupt mode */
177 const virtio_pci_func_t *virtio_pci_func;
183 vlib_log_class_t log_default;
185 virtio_if_t *interfaces;
188 extern virtio_main_t virtio_main;
189 extern vnet_device_class_t virtio_device_class;
190 extern vlib_node_registration_t virtio_input_node;
192 clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
194 clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
196 clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
198 void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
200 extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
201 extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
202 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
203 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
205 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
206 extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
207 virtio_if_t * vif, u16 queue_id);
208 extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
209 virtio_if_t * vif, u16 queue_id);
210 format_function_t format_virtio_device_name;
211 format_function_t format_virtio_log_name;
213 static_always_inline void
214 virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
216 if (vif->type == VIRTIO_IF_TYPE_PCI)
219 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id);
221 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
228 r = write (vring->kick_fd, &x, sizeof (x));
229 vring->last_kick_avail_idx = vring->avail->idx;
233 #define virtio_log_debug(vif, f, ...) \
235 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
236 "%U: " f, format_virtio_log_name, vif, \
240 #define virtio_log_warning(vif, f, ...) \
242 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
243 "%U: " f, format_virtio_log_name, vif, \
247 #define virtio_log_error(vif, f, ...) \
249 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
250 "%U: " f, format_virtio_log_name, vif, \
254 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
257 * fd.io coding-style-patch-verification: ON
260 * eval: (c-set-style "gnu")