blob: b2e74bed799fb6e6e92ab3d201b70e2682d9f3fd [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 */ \
52 _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descripor 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
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
Damjan Marion8389fb92017-10-13 18:29:53 +020078typedef enum
79{
80 VIRTIO_IF_TYPE_TAP,
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020081 VIRTIO_IF_TYPE_PCI,
Damjan Marion8389fb92017-10-13 18:29:53 +020082 VIRTIO_IF_N_TYPES,
83} virtio_if_type_t;
84
85
86typedef struct
87{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020088 u8 mac[6];
89 u16 status;
90 u16 max_virtqueue_pairs;
91 u16 mtu;
92} virtio_net_config_t;
93
94#define VIRTIO_RING_FLAG_MASK_INT 1
95
96typedef struct
97{
David Johnsond9818dd2018-12-14 14:53:41 -050098 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion8389fb92017-10-13 18:29:53 +020099 struct vring_desc *desc;
100 struct vring_used *used;
101 struct vring_avail *avail;
102 u16 desc_in_use;
103 u16 desc_next;
104 int kick_fd;
105 int call_fd;
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000106 u8 buffer_pool_index;
Damjan Marion8389fb92017-10-13 18:29:53 +0200107 u16 size;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200108 u16 queue_id;
109 u16 flags;
Damjan Marion8389fb92017-10-13 18:29:53 +0200110 u32 call_file_index;
111 u32 *buffers;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200112 u32 *indirect_buffers;
Damjan Marion8389fb92017-10-13 18:29:53 +0200113 u16 last_used_idx;
Damjan Marione40231b2018-12-20 10:44:47 +0100114 u16 last_kick_avail_idx;
Damjan Marion8389fb92017-10-13 18:29:53 +0200115} virtio_vring_t;
116
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200117typedef union
118{
119 struct
120 {
121 u16 domain;
122 u8 bus;
123 u8 slot:5;
124 u8 function:3;
125 };
126 u32 as_u32;
127} pci_addr_t;
128
Damjan Marion8389fb92017-10-13 18:29:53 +0200129typedef struct
130{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200131 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion8389fb92017-10-13 18:29:53 +0200132 u32 flags;
Damjan Marion829ee532018-02-16 16:13:32 +0100133 clib_spinlock_t lockp;
134
Damjan Marion8389fb92017-10-13 18:29:53 +0200135 u32 dev_instance;
136 u32 hw_if_index;
137 u32 sw_if_index;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200138 u16 virtio_net_hdr_sz;
139 virtio_if_type_t type;
140 union
141 {
142 u32 id;
143 pci_addr_t pci_addr;
144 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200145 u32 per_interface_next_index;
146 int fd;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200147 union
148 {
149 int tap_fd;
150 u32 pci_dev_handle;
151 };
Damjan Marion8389fb92017-10-13 18:29:53 +0200152 virtio_vring_t *vrings;
153
154 u64 features, remote_features;
155
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200156 /* error */
157 clib_error_t *error;
158 u16 max_queue_pairs;
Milan Lenco73e7f422017-12-14 10:04:25 +0100159 u16 tx_ring_sz;
160 u16 rx_ring_sz;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200161 u8 status;
162 u8 mac_addr[6];
163 u64 bar[2];
Damjan Marion2df39092017-12-04 20:03:37 +0100164 u8 *host_if_name;
Damjan Marion8389fb92017-10-13 18:29:53 +0200165 u8 *net_ns;
Milan Lenco73e7f422017-12-14 10:04:25 +0100166 u8 *host_bridge;
167 u8 host_mac_addr[6];
168 ip4_address_t host_ip4_addr;
169 u8 host_ip4_prefix_len;
170 ip6_address_t host_ip6_addr;
171 u8 host_ip6_prefix_len;
172
Damjan Marion8389fb92017-10-13 18:29:53 +0200173 int ifindex;
174} virtio_if_t;
175
176typedef struct
177{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000178 /* logging */
179 vlib_log_class_t log_default;
180
Damjan Marion8389fb92017-10-13 18:29:53 +0200181 virtio_if_t *interfaces;
182} virtio_main_t;
183
184extern virtio_main_t virtio_main;
185extern vnet_device_class_t virtio_device_class;
186extern vlib_node_registration_t virtio_input_node;
187
188clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
189 u16 sz);
Stevena624dbe2018-01-09 11:13:29 -0800190clib_error_t *virtio_vring_free (vlib_main_t * vm, virtio_if_t * vif,
191 u32 idx);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000192void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
193 u32 idx);
Stevena624dbe2018-01-09 11:13:29 -0800194extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200195extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
196extern void virtio_set_net_hdr_size (virtio_if_t * vif);
197extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
198 u32 type);
199extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
200 virtio_if_t * vif, u16 queue_id);
Damjan Marionceab7882018-01-19 20:56:12 +0100201format_function_t format_virtio_device_name;
202
Damjan Marione40231b2018-12-20 10:44:47 +0100203static_always_inline void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200204virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100205{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200206 if (vif->type == VIRTIO_IF_TYPE_PCI)
207 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
208 else
209 {
210 u64 x = 1;
211 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100212
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200213 r = write (vring->kick_fd, &x, sizeof (x));
214 vring->last_kick_avail_idx = vring->avail->idx;
215 }
Damjan Marione40231b2018-12-20 10:44:47 +0100216}
217
Damjan Marion8389fb92017-10-13 18:29:53 +0200218#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
219
220/*
221 * fd.io coding-style-patch-verification: ON
222 *
223 * Local Variables:
224 * eval: (c-set-style "gnu")
225 * End:
226 */