FD.io VPP  v21.06
Vector Packet Processing
vhost_user.h
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #ifndef __VIRTIO_VHOST_USER_H__
16 #define __VIRTIO_VHOST_USER_H__
17 
20 
21 /* vhost-user data structures */
22 
23 #define VHOST_MEMORY_MAX_NREGIONS 8
24 #define VHOST_USER_MSG_HDR_SZ 12
25 #define VHOST_VRING_INIT_MQ_PAIR_SZ 8 //8TX + 8RX
26 
27 /*
28  * qid is one byte in size in the spec. Please see VHOST_USER_SET_VRING_CALL,
29  * VHOST_USER_SET_VRING_KICK, and VHOST_USER_SET_VRING_ERR.
30  * The max number for q pair is naturally 128.
31  */
32 #define VHOST_VRING_MAX_MQ_PAIR_SZ 128
33 #define VHOST_VRING_IDX_RX(qid) (2*qid)
34 #define VHOST_VRING_IDX_TX(qid) (2*qid + 1)
35 
36 #define VHOST_USER_VRING_NOFD_MASK 0x100
37 
38 #define VHOST_USER_PROTOCOL_F_MQ 0
39 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
40 #define VHOST_VRING_F_LOG 0
41 
42 #define VHOST_USER_PROTOCOL_FEATURES ((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
43  (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))
44 
45 #define vu_log_debug(dev, f, ...) \
46 { \
47  vlib_log(VLIB_LOG_LEVEL_DEBUG, vhost_user_main.log_default, "%U: " f, \
48  format_vnet_hw_if_index_name, vnet_get_main(), \
49  dev->hw_if_index, ##__VA_ARGS__); \
50 };
51 
52 #define vu_log_warn(dev, f, ...) \
53 { \
54  vlib_log(VLIB_LOG_LEVEL_WARNING, vhost_user_main.log_default, "%U: " f, \
55  format_vnet_hw_if_index_name, vnet_get_main(), \
56  dev->hw_if_index, ##__VA_ARGS__); \
57 };
58 #define vu_log_err(dev, f, ...) \
59 { \
60  vlib_log(VLIB_LOG_LEVEL_ERR, vhost_user_main.log_default, "%U: " f, \
61  format_vnet_hw_if_index_name, vnet_get_main(), \
62  dev->hw_if_index, ##__VA_ARGS__); \
63 };
64 
65 #define UNIX_GET_FD(unixfd_idx) ({ \
66  typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \
67  (__unixfd_idx != ~0) ? \
68  pool_elt_at_index (file_main.file_pool, \
69  __unixfd_idx)->file_descriptor : -1; })
70 
71 #define foreach_virtio_trace_flags \
72  _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
73  _ (SINGLE_DESC, 1, "Single descriptor packet") \
74  _ (INDIRECT, 2, "Indirect descriptor") \
75  _ (MAP_ERROR, 4, "Memory mapping error")
76 
77 typedef enum
78 {
79 #define _(n,i,s) VIRTIO_TRACE_F_##n,
81 #undef _
83 
84 #define FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS \
85  (VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) | \
86  VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) | \
87  VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | \
88  VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))
89 
90 #define FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS \
91  (VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) | \
92  VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) | \
93  VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) | \
94  VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6))
95 
96 #define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS \
97  (FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS | \
98  FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS)
99 
100 
101 typedef struct
102 {
106  u8 hwaddr[6];
113 
114  /* return */
117 
125 /* *INDENT-OFF* */
127 {
132 } __attribute ((packed)) vhost_user_memory_region_t;
133 
134 typedef struct vhost_user_memory
135 {
138  vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
139 } __attribute ((packed)) vhost_user_memory_t;
140 
141 typedef enum vhost_user_req
142 {
164 
165 typedef struct vhost_user_msg {
166  vhost_user_req_t request;
169  union
170  {
174  vhost_user_memory_t memory;
176  };
177 } __attribute ((packed)) vhost_user_msg_t;
178 /* *INDENT-ON* */
179 
180 typedef struct
181 {
182  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
187  union
188  {
190  vring_packed_desc_t *packed_desc;
191  };
192  union
193  {
195  vring_desc_event_t *avail_event;
196  };
197  union
198  {
200  vring_desc_event_t *used_event;
201  };
210 
211  //Put non-runtime in a different cache line
212  CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
213  int errfd;
217 
218  /* The rx queue policy (interrupt/adaptive/polling) for this queue */
220 
221  /*
222  * It contains the device queue number. -1 if it does not. The idea is
223  * to not invoke vnet_hw_interface_assign_rx_thread and
224  * vnet_hw_interface_unassign_rx_thread more than once for the duration of
225  * the interface even if it is disconnected and reconnected.
226  */
228 
235 
236 #define VHOST_USER_EVENT_START_TIMER 1
237 #define VHOST_USER_EVENT_STOP_TIMER 2
238 
239 typedef struct
240 {
241  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
246  char sock_filename[256];
249  u32 hw_if_index, sw_if_index;
250 
251  //Feature negotiation
255 
256  //Memory region information
258  vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
259  void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
260  u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS];
261  u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS];
263 
264  //Virtual rings
266 
267  /*
268  * vrings is a dynamic array. It may have more elements than it is
269  * currently used. num_qid indicates the current total qid's in the
270  * vrings. For example, vec_len(vrings) = 64, num_qid = 60, so the
271  * current valid/used qid is (0, 59) in the vrings array.
272  */
274 
277 
280 
281  /* Whether to use spinlock or per_cpu_tx_qid assignment */
284 
286 
287  /* Packed ring configured */
289 
292 
293 typedef struct
294 {
298 } vhost_copy_t;
299 
300 typedef struct
301 {
302  u16 qid; /** The interface queue index (Not the virtio vring idx) */
303  u16 device_index; /** The device index */
304  u32 virtio_ring_flags; /** Runtime queue flags **/
305  u16 first_desc_len; /** Length of the first data descriptor **/
306  virtio_net_hdr_mrg_rxbuf_t hdr; /** Virtio header **/
307 } vhost_trace_t;
308 
309 #define VHOST_USER_RX_BUFFERS_N (2 * VLIB_FRAME_SIZE + 2)
310 #define VHOST_USER_COPY_ARRAY_N (4 * VLIB_FRAME_SIZE)
311 
312 typedef struct
313 {
316 
317  virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE];
319 
320  /* This is here so it doesn't end-up
321  * using stack or registers. */
323 
326 } vhost_cpu_t;
327 
328 typedef struct
329 {
337 
338  /** Per-CPU data for vhost-user */
340 
341  /** Pseudo random iterator */
343 
344  /* The number of rx interface/queue pairs in interrupt mode */
346 
347  /* logging */
349 
350  /* gso interface count */
353 
354 typedef struct
355 {
356  u8 if_name[64];
361  u8 sock_filename[256];
365 
367  vhost_user_intf_details_t ** out_vuids);
368 
373 
374 #endif
375 
376 /*
377  * fd.io coding-style-patch-verification: ON
378  *
379  * Local Variables:
380  * eval: (c-set-style "gnu")
381  * End:
382  */
u32 * to_next_list
Definition: vhost_user.h:324
Definition: mhash.h:46
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
vring_desc_t * desc
Definition: vhost_user.h:189
vhost_user_log_t log
Definition: vhost_user.h:175
u32 virtio_ring_flags
The device index.
Definition: vhost_user.h:304
virtio_net_hdr_mrg_rxbuf_t hdr
Length of the first data descriptor.
Definition: vhost_user.h:306
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:339
int vhost_user_modify_if(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_create_if_args_t *args)
Definition: vhost_user.c:1678
vring_desc_event_t * used_event
Definition: vhost_user.h:200
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost_user.c:1843
unsigned long u64
Definition: types.h:89
enum vhost_user_req vhost_user_req_t
vring_packed_desc_t * packed_desc
Definition: vhost_user.h:190
vring_avail_t * avail
Definition: vhost_user.h:194
vnet_device_class_t vhost_user_device_class
vhost_vring_state_t state
Definition: vhost_user.h:172
#define foreach_virtio_trace_flags
Definition: vhost_user.h:71
struct _vnet_device_class vnet_device_class_t
vring_used_t * used
Definition: vhost_user.h:199
vhost_trace_t * current_trace
Definition: vhost_user.h:322
unsigned char u8
Definition: types.h:56
int vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost_user.c:1387
double f64
Definition: types.h:142
unsigned int u32
Definition: types.h:88
u32 vlib_log_class_t
Definition: vlib.h:52
void * log_base_addr
Definition: vhost_user.h:278
#define VLIB_FRAME_SIZE
Definition: node.h:369
#define VHOST_USER_COPY_ARRAY_N
Definition: vhost_user.h:310
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
u32 random
Pseudo random iterator.
Definition: vhost_user.h:342
vlib_node_registration_t vhost_user_input_node
(constructor) VLIB_REGISTER_NODE (vhost_user_input_node)
unsigned short u16
Definition: types.h:57
vhost_user_vring_t * vrings
Definition: vhost_user.h:265
virtio_trace_flag_t
Definition: vhost_user.h:77
clib_spinlock_t vring_lock
Definition: vhost_user.h:209
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vhost_vring_addr_t addr
Definition: vhost_user.h:173
vlib_node_registration_t vhost_user_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (vhost_user_send_interrupt_node)
Definition: vhost_user.c:52
vlib_log_class_t log_default
Definition: vhost_user.h:348
u32 * show_dev_instance_by_real_dev_instance
Definition: vhost_user.h:333
u16 device_index
The interface queue index (Not the virtio vring idx)
Definition: vhost_user.h:303
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost_user.h:332
vhost_user_req
Definition: vhost_user.h:141
u16 first_desc_len
Runtime queue flags.
Definition: vhost_user.h:305
#define VHOST_USER_RX_BUFFERS_N
Definition: vhost_user.h:309
vlib_buffer_t ** rx_buffers_pdesc
Definition: vhost_user.h:325
vhost_user_main_t vhost_user_main
Definition: vhost_user.c:55
vring_desc_event_t * avail_event
Definition: vhost_user.h:195
mhash_t if_index_by_sock_name
Definition: vhost_user.h:330
struct _vlib_node_registration vlib_node_registration_t
VLIB buffer representation.
Definition: buffer.h:111
u64 uword
Definition: types.h:112
#define VHOST_MEMORY_MAX_NREGIONS
Definition: vhost_user.h:23
u32 rx_buffers_len
Definition: vhost_user.h:314
vhost_user_memory_t memory
Definition: vhost_user.h:174
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:125
vhost_user_req_t request
Definition: vhost_user.h:166
int dont_dump_vhost_user_memory
Definition: vhost_user.h:336
signed short i16
Definition: types.h:46
int vhost_user_create_if(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_create_if_args_t *args)
Definition: vhost_user.c:1622