FD.io VPP  v20.05.1-6-gf53edbc3b
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 
21 #include <linux/virtio_config.h>
22 #include <linux/virtio_net.h>
23 #include <linux/virtio_pci.h>
24 #include <linux/virtio_ring.h>
25 
26 #define foreach_virtio_net_features \
27  _ (VIRTIO_NET_F_CSUM, 0) /* Host handles pkts w/ partial csum */ \
28  _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \
29  _ (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 2) /* Dynamic offload configuration. */ \
30  _ (VIRTIO_NET_F_MTU, 3) /* Initial MTU advice. */ \
31  _ (VIRTIO_NET_F_MAC, 5) /* Host has given MAC address. */ \
32  _ (VIRTIO_NET_F_GSO, 6) /* Host handles pkts w/ any GSO. */ \
33  _ (VIRTIO_NET_F_GUEST_TSO4, 7) /* Guest can handle TSOv4 in. */ \
34  _ (VIRTIO_NET_F_GUEST_TSO6, 8) /* Guest can handle TSOv6 in. */ \
35  _ (VIRTIO_NET_F_GUEST_ECN, 9) /* Guest can handle TSO[6] w/ ECN in. */ \
36  _ (VIRTIO_NET_F_GUEST_UFO, 10) /* Guest can handle UFO in. */ \
37  _ (VIRTIO_NET_F_HOST_TSO4, 11) /* Host can handle TSOv4 in. */ \
38  _ (VIRTIO_NET_F_HOST_TSO6, 12) /* Host can handle TSOv6 in. */ \
39  _ (VIRTIO_NET_F_HOST_ECN, 13) /* Host can handle TSO[6] w/ ECN in. */ \
40  _ (VIRTIO_NET_F_HOST_UFO, 14) /* Host can handle UFO in. */ \
41  _ (VIRTIO_NET_F_MRG_RXBUF, 15) /* Host can merge receive buffers. */ \
42  _ (VIRTIO_NET_F_STATUS, 16) /* virtio_net_config.status available */ \
43  _ (VIRTIO_NET_F_CTRL_VQ, 17) /* Control channel available */ \
44  _ (VIRTIO_NET_F_CTRL_RX, 18) /* Control channel RX mode support */ \
45  _ (VIRTIO_NET_F_CTRL_VLAN, 19) /* Control channel VLAN filtering */ \
46  _ (VIRTIO_NET_F_CTRL_RX_EXTRA, 20) /* Extra RX mode control support */ \
47  _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21) /* Guest can announce device on the network */ \
48  _ (VIRTIO_NET_F_MQ, 22) /* Device supports Receive Flow Steering */ \
49  _ (VIRTIO_NET_F_CTRL_MAC_ADDR, 23) /* Set MAC address */ \
50  _ (VIRTIO_F_NOTIFY_ON_EMPTY, 24) \
51  _ (VHOST_F_LOG_ALL, 26) /* Log all write descriptors */ \
52  _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descriptor layout */ \
53  _ (VIRTIO_RING_F_INDIRECT_DESC, 28) /* Support indirect buffer descriptors */ \
54  _ (VIRTIO_RING_F_EVENT_IDX, 29) /* The Guest publishes the used index for which it expects an interrupt \
55  * at the end of the avail ring. Host should ignore the avail->flags field. */ \
56 /* The Host publishes the avail index for which it expects a kick \
57  * at the end of the used ring. Guest should ignore the used->flags field. */ \
58  _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
59  _ (VIRTIO_F_VERSION_1, 32)
60 
61 #define foreach_virtio_if_flag \
62  _(0, ADMIN_UP, "admin-up") \
63  _(1, DELETING, "deleting")
64 
65 typedef enum
66 {
67 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
69 #undef _
71 
72 #define VIRTIO_NUM_RX_DESC 256
73 #define VIRTIO_NUM_TX_DESC 256
74 
75 #define VIRTIO_FEATURE(X) (1ULL << X)
76 
77 #define TX_QUEUE(X) ((X*2) + 1)
78 #define RX_QUEUE(X) (X*2)
79 #define TX_QUEUE_ACCESS(X) (X/2)
80 #define RX_QUEUE_ACCESS(X) (X/2)
81 
82 #define foreach_virtio_if_types \
83  _ (TAP, 0) \
84  _ (TUN, 1) \
85  _ (PCI, 2)
86 
87 typedef enum
88 {
89 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
91 #undef _
92  VIRTIO_IF_N_TYPES = (1 << 3),
94 
95 
96 typedef struct
97 {
98  u8 mac[6];
103 
104 #define VIRTIO_RING_FLAG_MASK_INT 1
105 
106 typedef struct
107 {
108  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
109  struct vring_desc *desc;
110  struct vring_used *used;
111  struct vring_avail *avail;
115  int kick_fd;
116  int call_fd;
120 #define VRING_TX_OUT_OF_ORDER 1
127 
128 typedef union
129 {
130  struct
131  {
134  u8 slot:5;
135  u8 function:3;
136  };
138 } pci_addr_t;
139 
140 typedef struct
141 {
142  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
144 
151  union
152  {
155  };
157  int *vhost_fds;
158  int *tap_fds;
163  u64 features, remote_features;
164 
165  /* error */
167  u8 support_int_mode; /* support interrupt mode */
177  u8 host_mac_addr[6];
180  ip6_address_t host_ip6_addr;
186  int ifindex;
188 } virtio_if_t;
189 
190 typedef struct
191 {
192  /* logging */
194 
196 } virtio_main_t;
197 
201 
203  u16 sz);
205  u32 idx);
207  u32 idx);
209  u32 idx);
210 extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
211 extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
212 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
213 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
214  u32 type);
216  virtio_if_t * vif, u16 queue_id);
219 
222 {
223  if (vif->type == VIRTIO_IF_TYPE_PCI)
224  virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
225  else
226  {
227  u64 x = 1;
228  int __clib_unused r;
229 
230  r = write (vring->kick_fd, &x, sizeof (x));
231  vring->last_kick_avail_idx = vring->avail->idx;
232  }
233 }
234 
235 
236 #define virtio_log_debug(vif, f, ...) \
237 { \
238  vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
239  "%U: " f, format_virtio_log_name, vif, \
240  ##__VA_ARGS__); \
241 };
242 
243 #define virtio_log_warning(vif, f, ...) \
244 { \
245  vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
246  "%U: " f, format_virtio_log_name, vif, \
247  ##__VA_ARGS__); \
248 };
249 
250 #define virtio_log_error(vif, f, ...) \
251 { \
252  vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
253  "%U: " f, format_virtio_log_name, vif, \
254  ##__VA_ARGS__); \
255 };
256 
257 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
258 
259 /*
260  * fd.io coding-style-patch-verification: ON
261  *
262  * Local Variables:
263  * eval: (c-set-style "gnu")
264  * End:
265  */
u32 per_interface_next_index
Definition: virtio.h:156
struct vring_used * used
Definition: virtio.h:110
virtio_if_t * interfaces
Definition: virtio.h:195
#define CLIB_CACHE_LINE_ALIGN_MARK(mark)
Definition: cache.h:60
vl_api_mac_address_t mac
Definition: l2.api:502
ip4_address_t host_ip4_addr
Definition: virtio.h:178
u16 max_virtqueue_pairs
Definition: virtio.h:100
void virtio_set_net_hdr_size(virtio_if_t *vif)
Definition: virtio.c:240
void virtio_show(vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, u32 type)
Definition: virtio.c:250
clib_error_t * virtio_vring_free_rx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:153
clib_error_t * virtio_vring_init(vlib_main_t *vm, virtio_if_t *vif, u16 idx, u16 sz)
Definition: virtio.c:65
unsigned long u64
Definition: types.h:89
int gso_enabled
Definition: virtio.h:183
u32 host_mtu_size
Definition: virtio.h:182
u32 dev_instance
Definition: virtio.h:145
vnet_device_class_t virtio_device_class
format_function_t format_virtio_log_name
Definition: virtio.h:218
u16 domain
Definition: virtio.h:132
struct _vnet_device_class vnet_device_class_t
unsigned char u8
Definition: types.h:56
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
u32 msix_enabled
Definition: virtio.h:159
u32 vlib_log_class_t
Definition: vlib.h:51
u32 as_u32
Definition: virtio.h:137
u16 msix_table_size
Definition: virtio.h:171
struct vring_avail * avail
Definition: virtio.h:111
#define static_always_inline
Definition: clib.h:106
u32 hw_if_index
Definition: virtio.h:146
u8 host_ip6_prefix_len
Definition: virtio.h:181
u8 * host_bridge
Definition: virtio.h:176
int ifindex
Definition: virtio.h:186
u8 bus
Definition: virtio.h:133
u8 support_int_mode
Definition: virtio.h:167
clib_spinlock_t lockp
Definition: virtio.h:112
virtio_main_t virtio_main
Definition: virtio.c:37
unsigned int u32
Definition: types.h:88
void virtio_free_rx_buffers(vlib_main_t *vm, virtio_vring_t *vring)
Definition: virtio.c:138
u32 id
Definition: virtio.h:153
u16 queue_id
Definition: virtio.h:119
u16 num_txqs
Definition: virtio.h:170
vl_api_fib_path_type_t type
Definition: fib_types.api:123
virtio_if_flag_t
Definition: virtio.h:65
u32 pci_dev_handle
Definition: virtio.h:160
int * vhost_fds
Definition: virtio.h:157
unsigned short u16
Definition: types.h:57
pci_addr_t pci_addr
Definition: virtio.h:154
void virtio_pci_legacy_notify_queue(vlib_main_t *vm, virtio_if_t *vif, u16 queue_id)
Definition: pci.c:218
u8 buffer_pool_index
Definition: virtio.h:117
vlib_main_t * vm
Definition: in2out_ed.c:1599
u8 status
Definition: virtio.h:172
u16 desc_next
Definition: virtio.h:114
u16 virtio_net_hdr_sz
Definition: virtio.h:149
u16 num_rxqs
Definition: virtio.h:169
virtio_vring_t * rxq_vrings
Definition: virtio.h:161
format_function_t format_virtio_device_name
Definition: virtio.h:217
u16 last_used_idx
Definition: virtio.h:124
ip6_address_t host_ip6_addr
Definition: virtio.h:180
u32 call_file_index
Definition: virtio.h:122
u8 * net_ns
Definition: virtio.h:175
u32 flags
Definition: virtio.h:143
u16 last_kick_avail_idx
Definition: virtio.h:125
clib_error_t * error
Definition: virtio.h:166
vlib_log_class_t log_default
Definition: virtio.h:193
u32 tap_flags
Definition: virtio.h:185
u32 numa_node
Definition: virtio.h:148
virtio_if_type_t type
Definition: virtio.h:150
u64 remote_features
Definition: virtio.h:163
void virtio_free_used_desc(vlib_main_t *vm, virtio_vring_t *vring)
Definition: virtio.c:175
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
Definition: node.c:442
struct _vlib_node_registration vlib_node_registration_t
int csum_offload_enabled
Definition: virtio.h:184
vl_api_mac_address_t mac_addr
Definition: l2.api:180
#define foreach_virtio_if_flag
Definition: virtio.h:61
clib_error_t * virtio_vring_free_tx(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:201
u32 * buffers
Definition: virtio.h:123
u8 host_ip4_prefix_len
Definition: virtio.h:179
u32 sw_if_index
Definition: virtio.h:147
virtio_vring_t * cxq_vring
Definition: virtio.h:187
struct vring_desc * desc
Definition: virtio.h:109
#define foreach_virtio_if_types
Definition: virtio.h:82
u16 max_queue_pairs
Definition: virtio.h:168
virtio_if_type_t
Definition: virtio.h:87
void virtio_vring_set_numa_node(vlib_main_t *vm, virtio_if_t *vif, u32 idx)
Definition: virtio.c:224
int * tap_fds
Definition: virtio.h:158
u16 desc_in_use
Definition: virtio.h:113
virtio_vring_t * txq_vrings
Definition: virtio.h:162
static_always_inline void virtio_kick(vlib_main_t *vm, virtio_vring_t *vring, virtio_if_t *vif)
Definition: virtio.h:221
u8 * host_if_name
Definition: virtio.h:174
u8 slot
Definition: virtio.h:134