FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
virtio.h
Go to the documentation of this file.
1 /*
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:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17 
18 #ifndef _VNET_DEVICES_VIRTIO_VIRTIO_H_
19 #define _VNET_DEVICES_VIRTIO_VIRTIO_H_
20 
24 #include <vnet/gso/gro.h>
25 
26 #define foreach_virtio_if_flag \
27  _(0, ADMIN_UP, "admin-up") \
28  _(1, DELETING, "deleting")
29 
30 typedef enum
31 {
32 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
34 #undef _
36 
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)
41 
42 #define VIRTIO_NUM_RX_DESC 256
43 #define VIRTIO_NUM_TX_DESC 256
44 
45 #define foreach_virtio_if_types \
46  _ (TAP, 0) \
47  _ (TUN, 1) \
48  _ (PCI, 2)
49 
50 typedef enum
51 {
52 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
54 #undef _
55  VIRTIO_IF_N_TYPES = (1 << 3),
57 
58 #define VIRTIO_RING_FLAG_MASK_INT 1
59 
60 #define VIRTIO_EVENT_START_TIMER 1
61 #define VIRTIO_EVENT_STOP_TIMER 2
62 
63 typedef struct
64 {
65  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
67  union
68  {
69  struct
70  {
74  };
75  struct
76  {
77  vring_packed_desc_t *packed_desc;
78  vring_desc_event_t *driver_event;
79  vring_desc_event_t *device_event;
80  };
81  };
90  union
91  {
92  struct
93  {
94  int kick_fd;
95  int call_fd;
97  };
98  struct
99  {
103  };
104  };
105 #define VRING_TX_OUT_OF_ORDER 1
112 
113 typedef union
114 {
115  struct
116  {
119  u8 slot:5;
120  u8 function:3;
121  };
123 } pci_addr_t;
124 
125 /* forward declaration */
126 typedef struct _virtio_pci_func virtio_pci_func_t;
127 
128 typedef struct
129 {
130  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
140  union
141  {
142  int *tap_fds;
143  struct
144  {
147  };
148  };
151 
154 
155  CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
161 
162  /* error */
164  union
165  {
166  struct
167  {
170  };
172  };
173  union
174  {
175  struct /* tun/tap interface */
176  {
177  ip6_address_t host_ip6_addr;
178  int *vhost_fds;
182  u8 host_mac_addr[6];
186  int ifindex;
190  u8 host_carrier_up; /* host tun/tap driver link carrier state */
191  };
192  struct /* native virtio */
193  {
194  void *bar;
206  u8 support_int_mode; /* support interrupt mode */
208  };
209  };
212 } virtio_if_t;
213 
214 typedef struct
215 {
217  /* logging */
219 
222 } virtio_main_t;
223 
228 
230  u16 sz);
232  u32 idx);
234  u32 idx);
236 extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring);
237 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
238 extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
240 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
243  virtio_if_t * vif, u16 queue_id,
244  u16 queue_notify_offset);
246  virtio_if_t * vif, u16 queue_id,
247  u16 queue_notify_offset);
250 
253 {
254  if (vif->type == VIRTIO_IF_TYPE_PCI)
255  {
256  if (vif->is_modern)
258  vring->queue_notify_offset);
259  else
261  vring->queue_notify_offset);
262  }
263  else
264  {
265  u64 x = 1;
266  int __clib_unused r;
267 
268  r = write (vring->kick_fd, &x, sizeof (x));
269  vring->last_kick_avail_idx = vring->avail->idx;
270  }
271 }
272 
273 #define virtio_log_debug(vif, f, ...) \
274 { \
275  vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
276  "%U: " f, format_virtio_log_name, vif, \
277  ##__VA_ARGS__); \
278 };
279 
280 #define virtio_log_warning(vif, f, ...) \
281 { \
282  vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
283  "%U: " f, format_virtio_log_name, vif, \
284  ##__VA_ARGS__); \
285 };
286 
287 #define virtio_log_error(vif, f, ...) \
288 { \
289  vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
290  "%U: " f, format_virtio_log_name, vif, \
291  ##__VA_ARGS__); \
292 };
293 
294 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
295 
296 /*
297  * fd.io coding-style-patch-verification: ON
298  *
299  * Local Variables:
300  * eval: (c-set-style "gnu")
301  * End:
302  */
virtio_device_class
vnet_device_class_t virtio_device_class
virtio_if_t::host_ip4_prefix_len
u8 host_ip4_prefix_len
Definition: virtio.h:188
virtio_if_t::ifindex
int ifindex
Definition: virtio.h:186
virtio_vring_t::flow_table
gro_flow_table_t * flow_table
Definition: virtio.h:110
virtio_vring_t::call_fd
int call_fd
Definition: virtio.h:95
vnet_device_class_t
struct _vnet_device_class vnet_device_class_t
virtio_vring_t
Definition: virtio.h:63
foreach_virtio_if_types
#define foreach_virtio_if_types
Definition: virtio.h:45
CLIB_CACHE_LINE_ALIGN_MARK
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
vlib_log_class_t
u32 vlib_log_class_t
Definition: vlib.h:52
virtio_if_t::flags
u32 flags
Definition: virtio.h:132
virtio_if_t::error
clib_error_t * error
Definition: virtio.h:163
virtio_kick
static_always_inline void virtio_kick(vlib_main_t *vm, virtio_vring_t *vring, virtio_if_t *vif)
Definition: virtio.h:252
virtio_vring_t::queue_notify_offset
u16 queue_notify_offset
Definition: virtio.h:102
VIRTIO_IF_N_TYPES
@ VIRTIO_IF_N_TYPES
Definition: virtio.h:55
pci_addr_t::bus
u8 bus
Definition: virtio.h:118
virtio_if_t::host_carrier_up
u8 host_carrier_up
Definition: virtio.h:190
virtio_if_t::per_interface_next_index
u32 per_interface_next_index
Definition: virtio.h:133
virtio_vring_t::kick_fd
int kick_fd
Definition: virtio.h:94
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
virtio_vring_t::queue_id
u16 queue_id
Definition: virtio.h:84
virtio_if_t::numa_node
u32 numa_node
Definition: virtio.h:159
virtio_if_t::packet_coalesce
int packet_coalesce
Definition: virtio.h:156
virtio_vring_free_rx
clib_error_t * virtio_vring_free_rx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:143
virtio_main_t::interrupt_queues_count
u32 interrupt_queues_count
Definition: virtio.h:216
virtio_main_t::log_default
vlib_log_class_t log_default
Definition: virtio.h:218
r
vnet_hw_if_output_node_runtime_t * r
Definition: interface_output.c:1089
virtio_if_t::virtio_pci_func
const virtio_pci_func_t * virtio_pci_func
Definition: virtio.h:210
virtio_if_flag_t
virtio_if_flag_t
Definition: virtio.h:30
virtio_vring_t::size
u16 size
Definition: virtio.h:83
virtio_vring_t::avail
vring_avail_t * avail
Definition: virtio.h:73
virtio_if_t::notify_off_multiplier
u32 notify_off_multiplier
Definition: virtio.h:198
virtio_vring_set_rx_queues
void virtio_vring_set_rx_queues(vlib_main_t *vm, virtio_if_t *vif)
Definition: virtio.c:238
virtio_if_t::num_rxqs
u16 num_rxqs
Definition: virtio.h:134
virtio_send_interrupt_node
vlib_node_registration_t virtio_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (virtio_send_interrupt_node)
Definition: virtio_process.c:74
virtio_if_t::vhost_fds
int * vhost_fds
Definition: virtio.h:178
virtio_if_t::bar_id
u32 bar_id
Definition: virtio.h:197
virtio_if_t::notify_offset
u16 notify_offset
Definition: virtio.h:201
virtio_if_t::msix_table_size
u16 msix_table_size
Definition: virtio.h:205
virtio_if_t::packet_buffering
int packet_buffering
Definition: virtio.h:157
virtio_vring_t::buffer_pool_index
u8 buffer_pool_index
Definition: virtio.h:107
virtio_if_t::host_ip4_addr
ip4_address_t host_ip4_addr
Definition: virtio.h:187
virtio_if_t::txq_vrings
virtio_vring_t * txq_vrings
Definition: virtio.h:137
virtio_vring_t::desc_next
u16 desc_next
Definition: virtio.h:87
virtio_vring_t::used
vring_used_t * used
Definition: virtio.h:72
virtio_if_t::pci_addr
pci_addr_t pci_addr
Definition: virtio.h:196
virtio_if_t::common_offset
u16 common_offset
Definition: virtio.h:200
virtio_if_t::pci_dev_handle
u32 pci_dev_handle
Definition: virtio.h:145
virtio_vring_t::lockp
clib_spinlock_t lockp
Definition: virtio.h:66
vring_desc_t
Definition: virtio_std.h:93
virtio_if_t::host_ip6_addr
ip6_address_t host_ip6_addr
Definition: virtio.h:177
virtio_if_t::net_ns
u8 * net_ns
Definition: virtio.h:180
vhost_std.h
virtio_vring_t::desc
vring_desc_t * desc
Definition: virtio.h:71
virtio_buffering.h
virtio_if_type_t
virtio_if_type_t
Definition: virtio.h:50
pci_addr_t::domain
u16 domain
Definition: virtio.h:117
virtio_if_t::features
u64 features
Definition: virtio.h:131
virtio_if_t::device_offset
u16 device_offset
Definition: virtio.h:202
virtio_std.h
virtio_if_t::mac_addr32
u32 mac_addr32
Definition: virtio.h:168
vnet_hw_if_rx_mode
vnet_hw_if_rx_mode
Definition: interface.h:53
static_always_inline
#define static_always_inline
Definition: clib.h:112
clib_spinlock_s
Definition: lock.h:51
virtio_if_t::mac_addr16
u16 mac_addr16
Definition: virtio.h:169
virtio_if_t::csum_offload_enabled
int csum_offload_enabled
Definition: virtio.h:139
virtio_vring_t::last_kick_avail_idx
u16 last_kick_avail_idx
Definition: virtio.h:89
virtio_if_t::msix_enabled
u32 msix_enabled
Definition: virtio.h:146
virtio_if_t::hw_if_index
u32 hw_if_index
Definition: virtio.h:152
virtio_vring_t::mode
vnet_hw_if_rx_mode mode
Definition: virtio.h:108
foreach_virtio_if_flag
#define foreach_virtio_if_flag
Definition: virtio.h:26
virtio_if_t::host_if_name
u8 * host_if_name
Definition: virtio.h:179
ip4_address_t
Definition: ip4_packet.h:50
virtio_set_net_hdr_size
void virtio_set_net_hdr_size(virtio_if_t *vif)
Definition: virtio.c:288
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
virtio_if_t::sw_if_index
u32 sw_if_index
Definition: virtio.h:153
format_virtio_log_name
format_function_t format_virtio_log_name
Definition: virtio.h:249
virtio_pci_modern_notify_queue
void virtio_pci_modern_notify_queue(vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, u16 queue_notify_offset)
Definition: virtio_pci_modern.c:405
virtio_main_t::msg_id_base
u16 msg_id_base
Definition: virtio.h:221
virtio_vring_t::queue_index
u32 queue_index
Definition: virtio.h:85
mac_addr
vl_api_mac_address_t mac_addr
Definition: l2.api:210
virtio_if_t::max_queue_pairs
u16 max_queue_pairs
Definition: virtio.h:204
virtio_vring_t::driver_event
vring_desc_event_t * driver_event
Definition: virtio.h:78
virtio_vring_t::last_used_idx
u16 last_used_idx
Definition: virtio.h:88
virtio_main_t
Definition: virtio.h:214
format_function_t
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
virtio_vring_t::avail_wrap_counter
u16 avail_wrap_counter
Definition: virtio.h:100
size
u32 size
Definition: vhost_user.h:125
virtio_if_t::dev_instance
u32 dev_instance
Definition: virtio.h:158
vring_avail_t
Definition: virtio_std.h:101
virtio_free_buffers
void virtio_free_buffers(vlib_main_t *vm, virtio_vring_t *vring)
Definition: virtio.c:128
virtio_if_t::cxq_vring
virtio_vring_t * cxq_vring
Definition: virtio.h:195
virtio_vring_free_tx
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:165
u64
unsigned long u64
Definition: types.h:89
virtio_if_t::num_txqs
u16 num_txqs
Definition: virtio.h:135
u32
unsigned int u32
Definition: types.h:88
virtio_if_t::virtio_net_hdr_sz
u16 virtio_net_hdr_sz
Definition: virtio.h:149
virtio_vring_init
clib_error_t * virtio_vring_init(vlib_main_t *vm, virtio_if_t *vif, u16 idx, u16 sz)
Definition: virtio.c:60
virtio_if_t::remote_features
u64 remote_features
Definition: virtio.h:160
virtio_pci_legacy_notify_queue
void virtio_pci_legacy_notify_queue(vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, u16 queue_notify_offset)
Definition: virtio_pci_legacy.c:214
virtio_if_t::support_int_mode
u8 support_int_mode
Definition: virtio.h:206
virtio_vring_t::packed_desc
vring_packed_desc_t * packed_desc
Definition: virtio.h:77
virtio_if_t::tap_fds
int * tap_fds
Definition: virtio.h:142
virtio_show
void virtio_show(vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, virtio_if_type_t type)
Definition: virtio.c:298
virtio_if_t::type
virtio_if_type_t type
Definition: virtio.h:150
virtio_if_t
Definition: virtio.h:128
gro_flow_table_t
Definition: gro.h:53
virtio_if_t::tap_flags
u32 tap_flags
Definition: virtio.h:185
virtio_vring_t::call_file_index
u32 call_file_index
Definition: virtio.h:96
virtio_if_t::rxq_vrings
virtio_vring_t * rxq_vrings
Definition: virtio.h:136
virtio_if_t::isr_offset
u16 isr_offset
Definition: virtio.h:203
virtio_main_t::interfaces
virtio_if_t * interfaces
Definition: virtio.h:220
format_virtio_device_name
format_function_t format_virtio_device_name
Definition: virtio.h:248
vlib_main_t
Definition: main.h:102
virtio_if_t::host_bridge
u8 * host_bridge
Definition: virtio.h:181
virtio_vring_t::buffers
u32 * buffers
Definition: virtio.h:82
virtio_if_t::bar
void * bar
Definition: virtio.h:194
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
virtio_vring_t::buffering
virtio_vring_buffering_t * buffering
Definition: virtio.h:109
virtio_vring_t::desc_in_use
u16 desc_in_use
Definition: virtio.h:86
virtio_vring_t::flags
u16 flags
Definition: virtio.h:106
pci_addr_t
Definition: virtio.h:113
virtio_if_t::host_ip6_prefix_len
u8 host_ip6_prefix_len
Definition: virtio.h:189
pci_addr_t::as_u32
u32 as_u32
Definition: virtio.h:122
virtio_vring_t::device_event
vring_desc_event_t * device_event
Definition: virtio.h:79
virtio_set_packet_buffering
clib_error_t * virtio_set_packet_buffering(virtio_if_t *vif, u16 size)
Definition: virtio.c:203
gro.h
virtio_vring_buffering_t
Definition: virtio_buffering.h:24
virtio_if_t::is_packed
int is_packed
Definition: virtio.h:211
virtio_main
virtio_main_t virtio_main
Definition: virtio.c:37
virtio_vring_t::used_wrap_counter
u16 used_wrap_counter
Definition: virtio.h:101
virtio_if_t::status
u8 status
Definition: virtio.h:207
virtio_set_packet_coalesce
void virtio_set_packet_coalesce(virtio_if_t *vif)
Definition: virtio.c:188
vring_avail_t::idx
u16 idx
Definition: virtio_std.h:104
virtio_pci_func_t
struct _virtio_pci_func virtio_pci_func_t
virtio_input_node
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
Definition: node.c:498
virtio_if_t::host_mtu_size
u32 host_mtu_size
Definition: virtio.h:184
virtio_if_t::is_modern
u32 is_modern
Definition: virtio.h:199
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
virtio_if_t::gso_enabled
int gso_enabled
Definition: virtio.h:138
vring_used_t
Definition: virtio_std.h:115
virtio_if_t::id
u32 id
Definition: virtio.h:183
pci_addr_t::slot
u8 slot
Definition: virtio.h:119