blob: 3e8f94164831da003d0570fdca875cd0f3b1de95 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
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
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020021#include <linux/virtio_config.h>
22#include <linux/virtio_net.h>
23#include <linux/virtio_pci.h>
24#include <linux/virtio_ring.h>
25
Damjan Marion8389fb92017-10-13 18:29:53 +020026#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 */ \
Paul Vinciguerra97c998c2019-10-29 16:11:09 -040052 _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descriptor layout */ \
Damjan Marion8389fb92017-10-13 18:29:53 +020053 _ (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
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020061
Damjan Marion8389fb92017-10-13 18:29:53 +020062#define foreach_virtio_if_flag \
63 _(0, ADMIN_UP, "admin-up") \
64 _(1, DELETING, "deleting")
65
66typedef enum
67{
68#define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
69 foreach_virtio_if_flag
70#undef _
71} virtio_if_flag_t;
72
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020073#define VIRTIO_NUM_RX_DESC 256
74#define VIRTIO_NUM_TX_DESC 256
75
76#define VIRTIO_FEATURE(X) (1ULL << X)
77
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000078#define TX_QUEUE(X) ((X*2) + 1)
79#define RX_QUEUE(X) (X*2)
80#define TX_QUEUE_ACCESS(X) (X/2)
81#define RX_QUEUE_ACCESS(X) (X/2)
82
Mohsin Kazmi206acf82020-04-06 14:19:54 +020083#define foreach_virtio_if_types \
84 _ (TAP, 1) \
85 _ (TUN, 2) \
86 _ (PCI, 3)
87
Damjan Marion8389fb92017-10-13 18:29:53 +020088typedef enum
89{
Mohsin Kazmi206acf82020-04-06 14:19:54 +020090#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
91 foreach_virtio_if_types
92#undef _
93 VIRTIO_IF_N_TYPES = (1 << 4),
Damjan Marion8389fb92017-10-13 18:29:53 +020094} virtio_if_type_t;
95
96
97typedef struct
98{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020099 u8 mac[6];
100 u16 status;
101 u16 max_virtqueue_pairs;
102 u16 mtu;
103} virtio_net_config_t;
104
105#define VIRTIO_RING_FLAG_MASK_INT 1
106
107typedef struct
108{
David Johnsond9818dd2018-12-14 14:53:41 -0500109 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200110 struct vring_desc *desc;
111 struct vring_used *used;
112 struct vring_avail *avail;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000113 clib_spinlock_t lockp;
Damjan Marion8389fb92017-10-13 18:29:53 +0200114 u16 desc_in_use;
115 u16 desc_next;
116 int kick_fd;
117 int call_fd;
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000118 u8 buffer_pool_index;
Damjan Marion8389fb92017-10-13 18:29:53 +0200119 u16 size;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200120 u16 queue_id;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100121#define VRING_TX_OUT_OF_ORDER 1
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200122 u16 flags;
Damjan Marion8389fb92017-10-13 18:29:53 +0200123 u32 call_file_index;
124 u32 *buffers;
125 u16 last_used_idx;
Damjan Marione40231b2018-12-20 10:44:47 +0100126 u16 last_kick_avail_idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200127} virtio_vring_t;
128
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200129typedef union
130{
131 struct
132 {
133 u16 domain;
134 u8 bus;
135 u8 slot:5;
136 u8 function:3;
137 };
138 u32 as_u32;
139} pci_addr_t;
140
Damjan Marion8389fb92017-10-13 18:29:53 +0200141typedef struct
142{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200143 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200144 u32 flags;
Damjan Marion829ee532018-02-16 16:13:32 +0100145
Damjan Marion8389fb92017-10-13 18:29:53 +0200146 u32 dev_instance;
147 u32 hw_if_index;
148 u32 sw_if_index;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000149 u32 numa_node;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200150 u16 virtio_net_hdr_sz;
151 virtio_if_type_t type;
152 union
153 {
154 u32 id;
155 pci_addr_t pci_addr;
156 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200157 u32 per_interface_next_index;
Damjan Marion7c6102b2019-11-08 17:59:56 +0100158 int *vhost_fds;
159 int tap_fd;
160 u32 msix_enabled;
161 u32 pci_dev_handle;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000162 virtio_vring_t *rxq_vrings;
163 virtio_vring_t *txq_vrings;
Damjan Marion8389fb92017-10-13 18:29:53 +0200164 u64 features, remote_features;
165
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200166 /* error */
167 clib_error_t *error;
Mohsin Kazmib74fe322019-01-31 13:50:56 +0000168 u8 support_int_mode; /* support interrupt mode */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200169 u16 max_queue_pairs;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000170 u16 num_rxqs;
171 u16 num_txqs;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200172 u8 status;
173 u8 mac_addr[6];
Damjan Marion2df39092017-12-04 20:03:37 +0100174 u8 *host_if_name;
Damjan Marion8389fb92017-10-13 18:29:53 +0200175 u8 *net_ns;
Milan Lenco73e7f422017-12-14 10:04:25 +0100176 u8 *host_bridge;
177 u8 host_mac_addr[6];
178 ip4_address_t host_ip4_addr;
179 u8 host_ip4_prefix_len;
180 ip6_address_t host_ip6_addr;
181 u8 host_ip6_prefix_len;
Mohsin Kazmi97d54ed2019-06-10 11:20:15 +0200182 u32 host_mtu_size;
Andrew Yourtchenko6a7cff72018-10-12 16:09:22 +0200183 int gso_enabled;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000184 int csum_offload_enabled;
Damjan Marion8389fb92017-10-13 18:29:53 +0200185 int ifindex;
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000186 virtio_vring_t *cxq_vring;
Damjan Marion8389fb92017-10-13 18:29:53 +0200187} virtio_if_t;
188
189typedef struct
190{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000191 /* logging */
192 vlib_log_class_t log_default;
193
Damjan Marion8389fb92017-10-13 18:29:53 +0200194 virtio_if_t *interfaces;
195} virtio_main_t;
196
197extern virtio_main_t virtio_main;
198extern vnet_device_class_t virtio_device_class;
199extern vlib_node_registration_t virtio_input_node;
200
201clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
202 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000203clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
204 u32 idx);
205clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
206 u32 idx);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000207void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
208 u32 idx);
Stevena624dbe2018-01-09 11:13:29 -0800209extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200210extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
211extern void virtio_set_net_hdr_size (virtio_if_t * vif);
212extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
213 u32 type);
214extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
215 virtio_if_t * vif, u16 queue_id);
Damjan Marionceab7882018-01-19 20:56:12 +0100216format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100217format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100218
Damjan Marione40231b2018-12-20 10:44:47 +0100219static_always_inline void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200220virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100221{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200222 if (vif->type == VIRTIO_IF_TYPE_PCI)
223 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
224 else
225 {
226 u64 x = 1;
227 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100228
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200229 r = write (vring->kick_fd, &x, sizeof (x));
230 vring->last_kick_avail_idx = vring->avail->idx;
231 }
Damjan Marione40231b2018-12-20 10:44:47 +0100232}
233
Damjan Marionf41244f2019-11-08 17:41:06 +0100234
235#define virtio_log_debug(vif, f, ...) \
236{ \
237 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
238 "%U: " f, format_virtio_log_name, vif, \
239 ##__VA_ARGS__); \
240};
241
242#define virtio_log_warning(vif, f, ...) \
243{ \
244 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
245 "%U: " f, format_virtio_log_name, vif, \
246 ##__VA_ARGS__); \
247};
248
249#define virtio_log_error(vif, f, ...) \
250{ \
251 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
252 "%U: " f, format_virtio_log_name, vif, \
253 ##__VA_ARGS__); \
254};
255
Damjan Marion8389fb92017-10-13 18:29:53 +0200256#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
257
258/*
259 * fd.io coding-style-patch-verification: ON
260 *
261 * Local Variables:
262 * eval: (c-set-style "gnu")
263 * End:
264 */