FD.io VPP  v21.01.1
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  };
89  union
90  {
91  struct
92  {
93  int kick_fd;
94  int call_fd;
96  };
97  struct
98  {
102  };
103  };
104 #define VRING_TX_OUT_OF_ORDER 1
111 
112 typedef union
113 {
114  struct
115  {
118  u8 slot:5;
119  u8 function:3;
120  };
122 } pci_addr_t;
123 
124 /* forward declaration */
125 typedef struct _virtio_pci_func virtio_pci_func_t;
126 
127 typedef struct
128 {
129  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
139  union
140  {
141  int *tap_fds;
142  struct
143  {
146  };
147  };
150 
153 
154  CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
160 
161  /* error */
163  union
164  {
165  struct
166  {
169  };
171  };
172  union
173  {
174  struct /* tun/tap interface */
175  {
176  ip6_address_t host_ip6_addr;
177  int *vhost_fds;
181  u8 host_mac_addr[6];
185  int ifindex;
189  u8 host_carrier_up; /* host tun/tap driver link carrier state */
190  };
191  struct /* native virtio */
192  {
193  void *bar;
205  u8 support_int_mode; /* support interrupt mode */
207  };
208  };
211 } virtio_if_t;
212 
213 typedef struct
214 {
216  /* logging */
218 
220 } virtio_main_t;
221 
226 
228  u16 sz);
230  u32 idx);
232  u32 idx);
234  u32 idx);
235 extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring);
236 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
237 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
238  u32 type);
239 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
242  virtio_if_t * vif, u16 queue_id,
243  u16 queue_notify_offset);
245  virtio_if_t * vif, u16 queue_id,
246  u16 queue_notify_offset);
249 
252 {
253  if (vif->type == VIRTIO_IF_TYPE_PCI)
254  {
255  if (vif->is_modern)
256  virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
257  vring->queue_notify_offset);
258  else
259  virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
260  vring->queue_notify_offset);
261  }
262  else
263  {
264  u64 x = 1;
265  int __clib_unused r;
266 
267  r = write (vring->kick_fd, &x, sizeof (x));
268  vring->last_kick_avail_idx = vring->avail->idx;
269  }
270 }
271 
272 #define virtio_log_debug(vif, f, ...) \
273 { \
274  vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
275  "%U: " f, format_virtio_log_name, vif, \
276  ##__VA_ARGS__); \
277 };
278 
279 #define virtio_log_warning(vif, f, ...) \
280 { \
281  vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
282  "%U: " f, format_virtio_log_name, vif, \
283  ##__VA_ARGS__); \
284 };
285 
286 #define virtio_log_error(vif, f, ...) \
287 { \
288  vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
289  "%U: " f, format_virtio_log_name, vif, \
290  ##__VA_ARGS__); \
291 };
292 
293 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
294 
295 /*
296  * fd.io coding-style-patch-verification: ON
297  *
298  * Local Variables:
299  * eval: (c-set-style "gnu")
300  * End:
301  */
u32 per_interface_next_index
Definition: virtio.h:132
gro_flow_table_t * flow_table
Definition: virtio.h:109
vring_desc_event_t * device_event
Definition: virtio.h:79
virtio_if_t * interfaces
Definition: virtio.h:219
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
u32 is_modern
Definition: virtio.h:198
ip4_address_t host_ip4_addr
Definition: virtio.h:186
void virtio_pci_modern_notify_queue(vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, u16 queue_notify_offset)
u8 host_carrier_up
Definition: virtio.h:189
u32 bar_id
Definition: virtio.h:196
void virtio_set_net_hdr_size(virtio_if_t *vif)
Definition: virtio.c:252
void virtio_show(vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, u32 type)
Definition: virtio.c:262
clib_error_t * virtio_vring_free_rx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:152
clib_error_t * virtio_vring_init(vlib_main_t *vm, virtio_if_t *vif, u16 idx, u16 sz)
Definition: virtio.c:64
unsigned long u64
Definition: types.h:89
int gso_enabled
Definition: virtio.h:137
u32 host_mtu_size
Definition: virtio.h:183
u32 dev_instance
Definition: virtio.h:157
u16 device_offset
Definition: virtio.h:201
int kick_fd
Definition: virtio.h:93
const virtio_pci_func_t * virtio_pci_func
Definition: virtio.h:209
vnet_device_class_t virtio_device_class
format_function_t format_virtio_log_name
Definition: virtio.h:248
vlib_main_t * vm
Definition: in2out_ed.c:1580
u16 domain
Definition: virtio.h:116
struct _vnet_device_class vnet_device_class_t
int call_fd
Definition: virtio.h:94
void * bar
Definition: virtio.h:193
unsigned char u8
Definition: types.h:56
vring_desc_t * desc
Definition: virtio.h:71
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
u32 msix_enabled
Definition: virtio.h:145
u32 vlib_log_class_t
Definition: vlib.h:51
u32 as_u32
Definition: virtio.h:121
u16 msix_table_size
Definition: virtio.h:204
u64 features
Definition: virtio.h:130
#define static_always_inline
Definition: clib.h:109
u32 hw_if_index
Definition: virtio.h:151
u8 host_ip6_prefix_len
Definition: virtio.h:188
u8 * host_bridge
Definition: virtio.h:180
int ifindex
Definition: virtio.h:185
u32 notify_off_multiplier
Definition: virtio.h:197
u8 bus
Definition: virtio.h:117
u8 support_int_mode
Definition: virtio.h:205
void virtio_set_packet_coalesce(virtio_if_t *vif)
Definition: virtio.c:199
u16 isr_offset
Definition: virtio.h:202
clib_spinlock_t lockp
Definition: virtio.h:66
virtio_main_t virtio_main
Definition: virtio.c:35
unsigned int u32
Definition: types.h:88
clib_error_t * virtio_set_packet_buffering(virtio_if_t *vif, u16 size)
Definition: virtio.c:214
u32 id
Definition: virtio.h:182
u16 queue_id
Definition: virtio.h:84
u16 num_txqs
Definition: virtio.h:134
vl_api_fib_path_type_t type
Definition: fib_types.api:123
virtio_if_flag_t
Definition: virtio.h:30
u32 pci_dev_handle
Definition: virtio.h:144
int * vhost_fds
Definition: virtio.h:177
void virtio_free_buffers(vlib_main_t *vm, virtio_vring_t *vring)
Definition: virtio.c:137
vring_avail_t * avail
Definition: virtio.h:73
vring_desc_event_t * driver_event
Definition: virtio.h:78
unsigned short u16
Definition: types.h:57
vnet_hw_if_rx_mode mode
Definition: virtio.h:107
u32 size
Definition: vhost_user.h:106
void virtio_pci_legacy_notify_queue(vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, u16 queue_notify_offset)
pci_addr_t pci_addr
Definition: virtio.h:195
u32 interrupt_queues_count
Definition: virtio.h:215
u8 buffer_pool_index
Definition: virtio.h:106
int packet_coalesce
Definition: virtio.h:155
u8 status
Definition: virtio.h:206
vring_used_t * used
Definition: virtio.h:72
u16 desc_next
Definition: virtio.h:86
u16 virtio_net_hdr_sz
Definition: virtio.h:148
u16 num_rxqs
Definition: virtio.h:133
virtio_vring_t * rxq_vrings
Definition: virtio.h:135
format_function_t format_virtio_device_name
Definition: virtio.h:247
u16 last_used_idx
Definition: virtio.h:87
vlib_node_registration_t virtio_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (virtio_send_interrupt_node)
ip6_address_t host_ip6_addr
Definition: virtio.h:176
u32 call_file_index
Definition: virtio.h:95
u8 * net_ns
Definition: virtio.h:179
u32 flags
Definition: virtio.h:131
u16 last_kick_avail_idx
Definition: virtio.h:88
clib_error_t * error
Definition: virtio.h:162
vlib_log_class_t log_default
Definition: virtio.h:217
u32 tap_flags
Definition: virtio.h:184
u32 numa_node
Definition: virtio.h:158
virtio_if_type_t type
Definition: virtio.h:149
vring_packed_desc_t * packed_desc
Definition: virtio.h:77
u16 avail_wrap_counter
Definition: virtio.h:99
u64 remote_features
Definition: virtio.h:159
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
Definition: node.c:670
u32 mac_addr32
Definition: virtio.h:167
u16 notify_offset
Definition: virtio.h:200
virtio_vring_buffering_t * buffering
Definition: virtio.h:108
struct _vlib_node_registration vlib_node_registration_t
u16 used_wrap_counter
Definition: virtio.h:100
struct _virtio_pci_func virtio_pci_func_t
int csum_offload_enabled
Definition: virtio.h:138
vl_api_mac_address_t mac_addr
Definition: l2.api:180
int packet_buffering
Definition: virtio.h:156
#define foreach_virtio_if_flag
Definition: virtio.h:26
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:174
u32 * buffers
Definition: virtio.h:82
u8 host_ip4_prefix_len
Definition: virtio.h:187
u32 sw_if_index
Definition: virtio.h:152
virtio_vring_t * cxq_vring
Definition: virtio.h:194
u16 mac_addr16
Definition: virtio.h:168
vnet_hw_if_rx_mode
Definition: interface.h:53
int is_packed
Definition: virtio.h:210
#define foreach_virtio_if_types
Definition: virtio.h:45
u16 max_queue_pairs
Definition: virtio.h:203
virtio_if_type_t
Definition: virtio.h:50
void virtio_vring_set_numa_node(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:236
int * tap_fds
Definition: virtio.h:141
u16 common_offset
Definition: virtio.h:199
u16 desc_in_use
Definition: virtio.h:85
virtio_vring_t * txq_vrings
Definition: virtio.h:136
static_always_inline void virtio_kick(vlib_main_t *vm, virtio_vring_t *vring, virtio_if_t *vif)
Definition: virtio.h:251
u16 queue_notify_offset
Definition: virtio.h:101
u8 * host_if_name
Definition: virtio.h:178
u8 slot
Definition: virtio.h:118