2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #ifndef __VIRTIO_VHOST_USER_H__
16 #define __VIRTIO_VHOST_USER_H__
18 #include <vnet/devices/virtio/virtio_std.h>
19 #include <vnet/devices/virtio/vhost_std.h>
21 /* vhost-user data structures */
23 #define VHOST_MEMORY_MAX_NREGIONS 8
24 #define VHOST_USER_MSG_HDR_SZ 12
25 #define VHOST_VRING_MAX_N 16 //8TX + 8RX
26 #define VHOST_VRING_IDX_RX(qid) (2*qid)
27 #define VHOST_VRING_IDX_TX(qid) (2*qid + 1)
29 #define VHOST_USER_VRING_NOFD_MASK 0x100
31 #define VHOST_USER_PROTOCOL_F_MQ 0
32 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
33 #define VHOST_VRING_F_LOG 0
35 #define VHOST_USER_PROTOCOL_FEATURES ((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
36 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))
38 #define vu_log_debug(dev, f, ...) \
40 vlib_log(VLIB_LOG_LEVEL_DEBUG, vhost_user_main.log_default, "%U: " f, \
41 format_vnet_hw_if_index_name, vnet_get_main(), \
42 dev->hw_if_index, ##__VA_ARGS__); \
45 #define vu_log_warn(dev, f, ...) \
47 vlib_log(VLIB_LOG_LEVEL_WARNING, vhost_user_main.log_default, "%U: " f, \
48 format_vnet_hw_if_index_name, vnet_get_main(), \
49 dev->hw_if_index, ##__VA_ARGS__); \
51 #define vu_log_err(dev, f, ...) \
53 vlib_log(VLIB_LOG_LEVEL_ERR, vhost_user_main.log_default, "%U: " f, \
54 format_vnet_hw_if_index_name, vnet_get_main(), \
55 dev->hw_if_index, ##__VA_ARGS__); \
58 #define UNIX_GET_FD(unixfd_idx) ({ \
59 typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \
60 (__unixfd_idx != ~0) ? \
61 pool_elt_at_index (file_main.file_pool, \
62 __unixfd_idx)->file_descriptor : -1; })
64 #define foreach_virtio_trace_flags \
65 _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
66 _ (SINGLE_DESC, 1, "Single descriptor packet") \
67 _ (INDIRECT, 2, "Indirect descriptor") \
68 _ (MAP_ERROR, 4, "Memory mapping error")
72 #define _(n,i,s) VIRTIO_TRACE_F_##n,
73 foreach_virtio_trace_flags
75 } virtio_trace_flag_t;
77 #define FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS \
78 (VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) | \
79 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) | \
80 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | \
81 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))
83 #define FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS \
84 (VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) | \
85 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) | \
86 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) | \
87 VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6))
89 #define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS \
90 (FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS | \
91 FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS)
93 int vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
94 const char *sock_filename, u8 is_server,
95 u32 * sw_if_index, u64 feature_mask,
96 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
97 u8 enable_gso, u8 enable_packed);
98 int vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
99 const char *sock_filename, u8 is_server,
100 u32 sw_if_index, u64 feature_mask,
101 u8 renumber, u32 custom_dev_instance,
102 u8 enable_gso, u8 enable_packed);
103 int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
107 typedef struct vhost_user_memory_region
113 } __attribute ((packed)) vhost_user_memory_region_t;
115 typedef struct vhost_user_memory
119 vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
120 } __attribute ((packed)) vhost_user_memory_t;
122 typedef enum vhost_user_req
125 VHOST_USER_GET_FEATURES = 1,
126 VHOST_USER_SET_FEATURES = 2,
127 VHOST_USER_SET_OWNER = 3,
128 VHOST_USER_RESET_OWNER = 4,
129 VHOST_USER_SET_MEM_TABLE = 5,
130 VHOST_USER_SET_LOG_BASE = 6,
131 VHOST_USER_SET_LOG_FD = 7,
132 VHOST_USER_SET_VRING_NUM = 8,
133 VHOST_USER_SET_VRING_ADDR = 9,
134 VHOST_USER_SET_VRING_BASE = 10,
135 VHOST_USER_GET_VRING_BASE = 11,
136 VHOST_USER_SET_VRING_KICK = 12,
137 VHOST_USER_SET_VRING_CALL = 13,
138 VHOST_USER_SET_VRING_ERR = 14,
139 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
140 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
141 VHOST_USER_GET_QUEUE_NUM = 17,
142 VHOST_USER_SET_VRING_ENABLE = 18,
146 typedef struct vhost_user_msg {
147 vhost_user_req_t request;
153 vhost_vring_state_t state;
154 vhost_vring_addr_t addr;
155 vhost_user_memory_t memory;
156 vhost_user_log_t log;
158 } __attribute ((packed)) vhost_user_msg_t;
163 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
167 u16 n_since_last_int;
171 vring_packed_desc_t *packed_desc;
175 vring_avail_t *avail;
176 vring_desc_event_t *avail_event;
181 vring_desc_event_t *used_event;
183 uword desc_user_addr;
184 uword used_user_addr;
185 uword avail_user_addr;
190 //Put non-runtime in a different cache line
191 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
197 /* The rx queue policy (interrupt/adaptive/polling) for this queue */
201 * It contains the device queue number. -1 if it does not. The idea is
202 * to not invoke vnet_hw_interface_assign_rx_thread and
203 * vnet_hw_interface_unassign_rx_thread more than once for the duration of
204 * the interface even if it is disconnected and reconnected.
208 u16 used_wrap_counter;
209 u16 avail_wrap_counter;
210 } vhost_user_vring_t;
212 #define VHOST_USER_EVENT_START_TIMER 1
213 #define VHOST_USER_EVENT_STOP_TIMER 2
217 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
220 u32 unix_server_index;
222 char sock_filename[256];
225 u32 hw_if_index, sw_if_index;
227 //Feature negotiation
230 u64 protocol_features;
232 //Memory region information
234 vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
235 void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
236 u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS];
237 u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS];
238 u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS];
241 vhost_user_vring_t vrings[VHOST_VRING_MAX_N];
242 volatile u32 *vring_locks[VHOST_VRING_MAX_N];
244 int virtio_net_hdr_sz;
250 /* Whether to use spinlock or per_cpu_tx_qid assignment */
256 /* Packed ring configured */
270 u16 qid; /** The interface queue index (Not the virtio vring idx) */
271 u16 device_index; /** The device index */
272 u32 virtio_ring_flags; /** Runtime queue flags **/
273 u16 first_desc_len; /** Length of the first data descriptor **/
274 virtio_net_hdr_mrg_rxbuf_t hdr; /** Virtio header **/
277 #define VHOST_USER_RX_BUFFERS_N (2 * VLIB_FRAME_SIZE + 2)
278 #define VHOST_USER_COPY_ARRAY_N (4 * VLIB_FRAME_SIZE)
283 u32 rx_buffers[VHOST_USER_RX_BUFFERS_N];
285 virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE];
286 vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N];
288 /* This is here so it doesn't end-up
289 * using stack or registers. */
290 vhost_trace_t *current_trace;
293 vlib_buffer_t **rx_buffers_pdesc;
298 mhash_t if_index_by_sock_name;
300 vhost_user_intf_t *vhost_user_interfaces;
301 u32 *show_dev_instance_by_real_dev_instance;
304 int dont_dump_vhost_user_memory;
306 /** Per-CPU data for vhost-user */
309 /** Pseudo random iterator */
312 /* The number of rx interface/queue pairs in interrupt mode */
316 vlib_log_class_t log_default;
318 /* gso interface count */
326 u32 virtio_net_hdr_sz;
329 u8 sock_filename[256];
332 } vhost_user_intf_details_t;
334 int vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
335 vhost_user_intf_details_t ** out_vuids);
337 extern vlib_node_registration_t vhost_user_send_interrupt_node;
338 extern vnet_device_class_t vhost_user_device_class;
339 extern vlib_node_registration_t vhost_user_input_node;
340 extern vhost_user_main_t vhost_user_main;
345 * fd.io coding-style-patch-verification: ON
348 * eval: (c-set-style "gnu")