FD.io VPP  v20.09-64-g4f7b92f0a
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 
23 #include <vnet/gso/gro.h>
24 
25 #define foreach_virtio_if_flag \
26  _(0, ADMIN_UP, "admin-up") \
27  _(1, DELETING, "deleting")
28 
29 typedef enum
30 {
31 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
33 #undef _
35 
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)
40 
41 #define VIRTIO_NUM_RX_DESC 256
42 #define VIRTIO_NUM_TX_DESC 256
43 
44 #define foreach_virtio_if_types \
45  _ (TAP, 0) \
46  _ (TUN, 1) \
47  _ (PCI, 2)
48 
49 typedef enum
50 {
51 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
53 #undef _
54  VIRTIO_IF_N_TYPES = (1 << 3),
56 
57 #define VIRTIO_RING_FLAG_MASK_INT 1
58 
59 typedef struct
60 {
61  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
68  union
69  {
70  struct
71  {
72  int kick_fd;
73  int call_fd;
74  };
76  };
80 #define VRING_TX_OUT_OF_ORDER 1
88 
89 typedef union
90 {
91  struct
92  {
95  u8 slot:5;
96  u8 function:3;
97  };
99 } pci_addr_t;
100 
101 /* forward declaration */
102 typedef struct _virtio_pci_func virtio_pci_func_t;
103 
104 typedef struct
105 {
106  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
116  union
117  {
118  int *tap_fds;
119  struct
120  {
123  };
124  };
127 
130 
131  CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
136 
137  /* error */
139  union
140  {
141  struct
142  {
145  };
147  };
148  union
149  {
150  struct /* tun/tap interface */
151  {
152  ip6_address_t host_ip6_addr;
153  int *vhost_fds;
157  u8 host_mac_addr[6];
161  int ifindex;
165  };
166  struct /* native virtio */
167  {
168  void *bar;
180  u8 support_int_mode; /* support interrupt mode */
182  };
183  };
185 } virtio_if_t;
186 
187 typedef struct
188 {
189  /* logging */
191 
193 } virtio_main_t;
194 
198 
200  u16 sz);
202  u32 idx);
204  u32 idx);
206  u32 idx);
207 extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
208 extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
209 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
210 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
211  u32 type);
212 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
214  virtio_if_t * vif, u16 queue_id,
215  u16 queue_notify_offset);
217  virtio_if_t * vif, u16 queue_id,
218  u16 queue_notify_offset);
221 
224 {
225  if (vif->type == VIRTIO_IF_TYPE_PCI)
226  {
227  if (vif->is_modern)
228  virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
229  vring->queue_notify_offset);
230  else
231  virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
232  vring->queue_notify_offset);
233  }
234  else
235  {
236  u64 x = 1;
237  int __clib_unused r;
238 
239  r = write (vring->kick_fd, &x, sizeof (x));
240  vring->last_kick_avail_idx = vring->avail->idx;
241  }
242 }
243 
244 #define virtio_log_debug(vif, f, ...) \
245 { \
246  vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
247  "%U: " f, format_virtio_log_name, vif, \
248  ##__VA_ARGS__); \
249 };
250 
251 #define virtio_log_warning(vif, f, ...) \
252 { \
253  vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
254  "%U: " f, format_virtio_log_name, vif, \
255  ##__VA_ARGS__); \
256 };
257 
258 #define virtio_log_error(vif, f, ...) \
259 { \
260  vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
261  "%U: " f, format_virtio_log_name, vif, \
262  ##__VA_ARGS__); \
263 };
264 
265 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
266 
267 /*
268  * fd.io coding-style-patch-verification: ON
269  *
270  * Local Variables:
271  * eval: (c-set-style "gnu")
272  * End:
273  */
u32 per_interface_next_index
Definition: virtio.h:109
gro_flow_table_t * flow_table
Definition: virtio.h:86
virtio_if_t * interfaces
Definition: virtio.h:192
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
u32 is_modern
Definition: virtio.h:173
ip4_address_t host_ip4_addr
Definition: virtio.h:162
void virtio_pci_modern_notify_queue(vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, u16 queue_notify_offset)
u32 bar_id
Definition: virtio.h:171
void virtio_set_net_hdr_size(virtio_if_t *vif)
Definition: virtio.c:255
void virtio_show(vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, u32 type)
Definition: virtio.c:265
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:114
u32 host_mtu_size
Definition: virtio.h:159
u32 dev_instance
Definition: virtio.h:133
u16 device_offset
Definition: virtio.h:176
int kick_fd
Definition: virtio.h:72
const virtio_pci_func_t * virtio_pci_func
Definition: virtio.h:184
vnet_device_class_t virtio_device_class
format_function_t format_virtio_log_name
Definition: virtio.h:220
vlib_main_t * vm
Definition: in2out_ed.c:1582
u16 domain
Definition: virtio.h:93
struct _vnet_device_class vnet_device_class_t
int call_fd
Definition: virtio.h:73
void * bar
Definition: virtio.h:168
unsigned char u8
Definition: types.h:56
vring_desc_t * desc
Definition: virtio.h:63
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
u32 msix_enabled
Definition: virtio.h:122
u32 vlib_log_class_t
Definition: vlib.h:51
u32 as_u32
Definition: virtio.h:98
u16 msix_table_size
Definition: virtio.h:179
u64 features
Definition: virtio.h:107
#define static_always_inline
Definition: clib.h:108
u32 hw_if_index
Definition: virtio.h:128
u8 host_ip6_prefix_len
Definition: virtio.h:164
u8 * host_bridge
Definition: virtio.h:156
int ifindex
Definition: virtio.h:161
u32 notify_off_multiplier
Definition: virtio.h:172
u8 bus
Definition: virtio.h:94
u8 support_int_mode
Definition: virtio.h:180
void virtio_set_packet_coalesce(virtio_if_t *vif)
Definition: virtio.c:224
u16 isr_offset
Definition: virtio.h:177
clib_spinlock_t lockp
Definition: virtio.h:62
virtio_main_t virtio_main
Definition: virtio.c:35
unsigned int u32
Definition: types.h:88
void virtio_free_rx_buffers(vlib_main_t *vm, virtio_vring_t *vring)
Definition: virtio.c:137
u32 id
Definition: virtio.h:158
u16 queue_id
Definition: virtio.h:79
u16 num_txqs
Definition: virtio.h:111
vl_api_fib_path_type_t type
Definition: fib_types.api:123
virtio_if_flag_t
Definition: virtio.h:29
u32 pci_dev_handle
Definition: virtio.h:121
int * vhost_fds
Definition: virtio.h:153
vring_avail_t * avail
Definition: virtio.h:65
unsigned short u16
Definition: types.h:57
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:170
u8 buffer_pool_index
Definition: virtio.h:77
int packet_coalesce
Definition: virtio.h:132
u8 status
Definition: virtio.h:181
vring_used_t * used
Definition: virtio.h:64
u16 desc_next
Definition: virtio.h:67
u16 virtio_net_hdr_sz
Definition: virtio.h:125
u16 num_rxqs
Definition: virtio.h:110
virtio_vring_t * rxq_vrings
Definition: virtio.h:112
format_function_t format_virtio_device_name
Definition: virtio.h:219
u16 last_used_idx
Definition: virtio.h:83
ip6_address_t host_ip6_addr
Definition: virtio.h:152
u32 call_file_index
Definition: virtio.h:85
u8 * net_ns
Definition: virtio.h:155
u32 flags
Definition: virtio.h:108
u16 last_kick_avail_idx
Definition: virtio.h:84
clib_error_t * error
Definition: virtio.h:138
vlib_log_class_t log_default
Definition: virtio.h:190
u32 tap_flags
Definition: virtio.h:160
u32 numa_node
Definition: virtio.h:134
virtio_if_type_t type
Definition: virtio.h:126
u64 remote_features
Definition: virtio.h:135
void virtio_free_used_desc(vlib_main_t *vm, virtio_vring_t *vring)
Definition: virtio.c:174
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
Definition: node.c:490
u32 mac_addr32
Definition: virtio.h:143
u16 notify_offset
Definition: virtio.h:175
struct _vlib_node_registration vlib_node_registration_t
struct _virtio_pci_func virtio_pci_func_t
int csum_offload_enabled
Definition: virtio.h:115
vl_api_mac_address_t mac_addr
Definition: l2.api:180
#define foreach_virtio_if_flag
Definition: virtio.h:25
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:200
u32 * buffers
Definition: virtio.h:82
u8 host_ip4_prefix_len
Definition: virtio.h:163
u32 sw_if_index
Definition: virtio.h:129
virtio_vring_t * cxq_vring
Definition: virtio.h:169
u16 mac_addr16
Definition: virtio.h:144
#define foreach_virtio_if_types
Definition: virtio.h:44
u16 max_queue_pairs
Definition: virtio.h:178
virtio_if_type_t
Definition: virtio.h:49
void virtio_vring_set_numa_node(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:239
int * tap_fds
Definition: virtio.h:118
u16 common_offset
Definition: virtio.h:174
u16 desc_in_use
Definition: virtio.h:66
virtio_vring_t * txq_vrings
Definition: virtio.h:113
static_always_inline void virtio_kick(vlib_main_t *vm, virtio_vring_t *vring, virtio_if_t *vif)
Definition: virtio.h:223
u16 queue_notify_offset
Definition: virtio.h:75
u8 * host_if_name
Definition: virtio.h:154
u8 slot
Definition: virtio.h:95