Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <fcntl.h> |
| 17 | #include <sys/ioctl.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 18 | |
| 19 | #include <vppinfra/types.h> |
| 20 | #include <vlib/vlib.h> |
| 21 | #include <vlib/pci/pci.h> |
| 22 | #include <vnet/ethernet/ethernet.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 23 | #include <vnet/ip/ip4_packet.h> |
| 24 | #include <vnet/ip/ip6_packet.h> |
| 25 | #include <vnet/devices/virtio/virtio.h> |
| 26 | #include <vnet/devices/virtio/pci.h> |
| 27 | |
| 28 | #define PCI_VENDOR_ID_VIRTIO 0x1af4 |
| 29 | #define PCI_DEVICE_ID_VIRTIO_NIC 0x1000 |
| 30 | /* Doesn't support modern device */ |
| 31 | #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041 |
| 32 | |
| 33 | #define PCI_CAPABILITY_LIST 0x34 |
| 34 | #define PCI_CAP_ID_VNDR 0x09 |
| 35 | #define PCI_CAP_ID_MSIX 0x11 |
| 36 | |
| 37 | #define PCI_MSIX_ENABLE 0x8000 |
| 38 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 39 | static pci_device_id_t virtio_pci_device_ids[] = { |
| 40 | { |
| 41 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 42 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC}, |
| 43 | { |
| 44 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 45 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN}, |
| 46 | {0}, |
| 47 | }; |
| 48 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 49 | static u32 |
| 50 | virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, |
| 51 | u32 flags) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static clib_error_t * |
| 57 | virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 58 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 59 | clib_error_t *error = 0; |
| 60 | u16 max_queue_pairs = 1; |
| 61 | |
| 62 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 63 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 64 | max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 67 | virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 68 | if (max_queue_pairs < 1 || max_queue_pairs > 0x8000) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 69 | return clib_error_return (error, "max queue pair is %x," |
| 70 | " should be in range [1, 0x8000]", |
| 71 | max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 72 | |
| 73 | vif->max_queue_pairs = max_queue_pairs; |
| 74 | return error; |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 79 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 80 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
| 81 | vif->virtio_pci_func->set_mac (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static u32 |
| 85 | virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 86 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 87 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 88 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 89 | vif->virtio_pci_func->get_mac (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | static u16 |
| 96 | virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif) |
| 97 | { |
| 98 | /* |
| 99 | * Minimal driver: assumes link is up |
| 100 | */ |
| 101 | u16 status = 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 102 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 103 | status = vif->virtio_pci_func->get_device_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 104 | return status; |
| 105 | } |
| 106 | |
| 107 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 108 | virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 109 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 110 | { |
| 111 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 112 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 113 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 114 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 115 | line--; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 116 | u16 qid = line; |
| 117 | |
| 118 | vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid); |
| 119 | } |
| 120 | |
| 121 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 122 | virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 123 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 124 | { |
| 125 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 126 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 127 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 128 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 129 | |
| 130 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 131 | { |
| 132 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 133 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 134 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; |
| 139 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void |
| 144 | virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h) |
| 145 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 146 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 147 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 148 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 149 | u8 isr = 0; |
| 150 | u16 line = 0; |
| 151 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 152 | isr = vif->virtio_pci_func->get_isr (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * If the lower bit is set: look through the used rings of |
| 156 | * all virtqueues for the device, to see if any progress has |
| 157 | * been made by the device which requires servicing. |
| 158 | */ |
| 159 | if (isr & VIRTIO_PCI_ISR_INTR) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 160 | { |
| 161 | for (; line < vif->num_rxqs; line++) |
| 162 | virtio_pci_irq_queue_handler (vm, h, (line + 1)); |
| 163 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 164 | |
| 165 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 166 | virtio_pci_irq_config_handler (vm, h, line); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | inline void |
| 170 | device_status (vlib_main_t * vm, virtio_if_t * vif) |
| 171 | { |
| 172 | struct status_struct |
| 173 | { |
| 174 | u8 bit; |
| 175 | char *str; |
| 176 | }; |
| 177 | struct status_struct *status_entry; |
| 178 | static struct status_struct status_array[] = { |
| 179 | #define _(s,b) { .str = #s, .bit = b, }, |
| 180 | foreach_virtio_config_status_flags |
| 181 | #undef _ |
| 182 | {.str = NULL} |
| 183 | }; |
| 184 | |
| 185 | vlib_cli_output (vm, " status 0x%x", vif->status); |
| 186 | |
| 187 | status_entry = (struct status_struct *) &status_array; |
| 188 | while (status_entry->str) |
| 189 | { |
| 190 | if (vif->status & status_entry->bit) |
| 191 | vlib_cli_output (vm, " %s (%x)", status_entry->str, |
| 192 | status_entry->bit); |
| 193 | status_entry++; |
| 194 | } |
| 195 | } |
| 196 | |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 197 | struct virtio_ctrl_msg |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 198 | { |
| 199 | struct virtio_net_ctrl_hdr ctrl; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 200 | virtio_net_ctrl_ack status; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 201 | u8 data[1024]; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | static int |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 205 | virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif, |
| 206 | struct virtio_ctrl_msg *data, u32 len) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 207 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 208 | virtio_vring_t *vring = vif->cxq_vring; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 209 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 210 | struct virtio_ctrl_msg result; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 211 | u32 buffer_index; |
| 212 | vlib_buffer_t *b; |
| 213 | u16 used, next, avail; |
| 214 | u16 sz = vring->size; |
| 215 | u16 mask = sz - 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 216 | |
| 217 | used = vring->desc_in_use; |
| 218 | next = vring->desc_next; |
| 219 | avail = vring->avail->idx; |
| 220 | struct vring_desc *d = &vring->desc[next]; |
| 221 | |
| 222 | if (vlib_buffer_alloc (vm, &buffer_index, 1)) |
| 223 | b = vlib_get_buffer (vm, buffer_index); |
| 224 | else |
| 225 | return VIRTIO_NET_ERR; |
| 226 | /* |
| 227 | * current_data may not be initialized with 0 and may contain |
| 228 | * previous offset. |
| 229 | */ |
| 230 | b->current_data = 0; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 231 | clib_memcpy (vlib_buffer_get_current (b), data, |
| 232 | sizeof (struct virtio_ctrl_msg)); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 233 | d->flags = VRING_DESC_F_NEXT; |
| 234 | d->addr = vlib_buffer_get_current_pa (vm, b); |
| 235 | d->len = sizeof (struct virtio_net_ctrl_hdr); |
| 236 | vring->avail->ring[avail & mask] = next; |
| 237 | avail++; |
| 238 | next = (next + 1) & mask; |
| 239 | d->next = next; |
| 240 | used++; |
| 241 | |
| 242 | d = &vring->desc[next]; |
| 243 | d->flags = VRING_DESC_F_NEXT; |
| 244 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 245 | STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data); |
| 246 | d->len = len; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 247 | next = (next + 1) & mask; |
| 248 | d->next = next; |
| 249 | used++; |
| 250 | |
| 251 | d = &vring->desc[next]; |
| 252 | d->flags = VRING_DESC_F_WRITE; |
| 253 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 254 | STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status); |
| 255 | d->len = sizeof (data->status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 256 | next = (next + 1) & mask; |
| 257 | used++; |
| 258 | |
| 259 | CLIB_MEMORY_STORE_BARRIER (); |
| 260 | vring->avail->idx = avail; |
| 261 | vring->desc_next = next; |
| 262 | vring->desc_in_use = used; |
| 263 | |
| 264 | if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0) |
| 265 | { |
| 266 | virtio_kick (vm, vring, vif); |
| 267 | } |
| 268 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 269 | u16 last = vring->last_used_idx, n_left = 0; |
| 270 | n_left = vring->used->idx - last; |
| 271 | |
| 272 | while (n_left) |
| 273 | { |
| 274 | struct vring_used_elem *e = &vring->used->ring[last & mask]; |
| 275 | u16 slot = e->id; |
| 276 | |
| 277 | d = &vring->desc[slot]; |
| 278 | while (d->flags & VRING_DESC_F_NEXT) |
| 279 | { |
| 280 | used--; |
| 281 | slot = d->next; |
| 282 | d = &vring->desc[slot]; |
| 283 | } |
| 284 | used--; |
| 285 | last++; |
| 286 | n_left--; |
| 287 | } |
| 288 | vring->desc_in_use = used; |
| 289 | vring->last_used_idx = last; |
| 290 | |
| 291 | CLIB_MEMORY_BARRIER (); |
| 292 | clib_memcpy (&result, vlib_buffer_get_current (b), |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 293 | sizeof (struct virtio_ctrl_msg)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 294 | virtio_log_debug (vif, "ctrl-queue: status %u", result.status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 295 | status = result.status; |
| 296 | vlib_buffer_free (vm, &buffer_index, 1); |
| 297 | return status; |
| 298 | } |
| 299 | |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 300 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 301 | virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 302 | { |
| 303 | struct virtio_ctrl_msg offload_hdr; |
| 304 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 305 | |
| 306 | offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 307 | offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 308 | offload_hdr.status = VIRTIO_NET_ERR; |
| 309 | u64 offloads = 0ULL; |
| 310 | clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads)); |
| 311 | |
| 312 | status = |
| 313 | virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads)); |
| 314 | virtio_log_debug (vif, "disable offloads"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 315 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 316 | vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 317 | return status; |
| 318 | } |
| 319 | |
| 320 | static int |
| 321 | virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 322 | { |
| 323 | struct virtio_ctrl_msg csum_offload_hdr; |
| 324 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 325 | |
| 326 | csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 327 | csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 328 | csum_offload_hdr.status = VIRTIO_NET_ERR; |
| 329 | u64 offloads = 0ULL; |
| 330 | offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM); |
| 331 | clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads)); |
| 332 | |
| 333 | status = |
| 334 | virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads)); |
| 335 | virtio_log_debug (vif, "enable checksum offload"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 336 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 337 | vif->features = vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 338 | return status; |
| 339 | } |
| 340 | |
| 341 | static int |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 342 | virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif) |
| 343 | { |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 344 | struct virtio_ctrl_msg gso_hdr; |
| 345 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 346 | |
| 347 | gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 348 | gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 349 | gso_hdr.status = VIRTIO_NET_ERR; |
| 350 | u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 351 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 352 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 353 | clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads)); |
| 354 | |
| 355 | status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 356 | virtio_log_debug (vif, "enable gso"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 357 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 358 | vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 359 | return status; |
| 360 | } |
| 361 | |
| 362 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 363 | virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, |
| 364 | int csum_offload_enabled) |
| 365 | { |
| 366 | vnet_main_t *vnm = vnet_get_main (); |
| 367 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 368 | |
| 369 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 370 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))) |
| 371 | { |
| 372 | if (gso_enabled |
| 373 | && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | |
| 374 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)))) |
| 375 | { |
| 376 | if (virtio_pci_enable_gso (vm, vif)) |
| 377 | { |
| 378 | virtio_log_warning (vif, "gso is not enabled"); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | vif->gso_enabled = 1; |
| 383 | vif->csum_offload_enabled = 0; |
| 384 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO | |
| 385 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD; |
| 386 | } |
| 387 | } |
| 388 | else if (csum_offload_enabled |
| 389 | && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))) |
| 390 | { |
| 391 | if (virtio_pci_enable_checksum_offload (vm, vif)) |
| 392 | { |
| 393 | virtio_log_warning (vif, "checksum offload is not enabled"); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | vif->csum_offload_enabled = 1; |
| 398 | vif->gso_enabled = 0; |
| 399 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 400 | hw->flags |= |
| 401 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD; |
| 402 | } |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | if (virtio_pci_disable_offload (vm, vif)) |
| 407 | { |
| 408 | virtio_log_warning (vif, "offloads are not disabled"); |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | vif->csum_offload_enabled = 0; |
| 413 | vif->gso_enabled = 0; |
| 414 | hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO | |
| 415 | VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static int |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 424 | virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif, |
| 425 | u16 num_queues) |
| 426 | { |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 427 | struct virtio_ctrl_msg mq_hdr; |
| 428 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; |
| 429 | |
| 430 | mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ; |
| 431 | mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; |
| 432 | mq_hdr.status = VIRTIO_NET_ERR; |
| 433 | clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues)); |
| 434 | |
| 435 | status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 436 | virtio_log_debug (vif, "multi-queue enable %u queues", num_queues); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 437 | return status; |
| 438 | } |
| 439 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 440 | static u8 |
| 441 | virtio_pci_queue_size_valid (u16 qsz) |
| 442 | { |
| 443 | if (qsz < 64 || qsz > 4096) |
| 444 | return 0; |
| 445 | if ((qsz % 64) != 0) |
| 446 | return 0; |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | clib_error_t * |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 451 | virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif, |
| 452 | u16 queue_num) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 453 | { |
| 454 | clib_error_t *error = 0; |
| 455 | u16 queue_size = 0; |
| 456 | virtio_vring_t *vring; |
| 457 | struct vring vr; |
| 458 | u32 i = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 459 | void *ptr = NULL; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 460 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 461 | queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 462 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 463 | clib_warning ("queue size is not valid"); |
| 464 | |
| 465 | if (!is_pow2 (queue_size)) |
| 466 | return clib_error_return (0, "ring size must be power of 2"); |
| 467 | |
| 468 | if (queue_size > 32768) |
| 469 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 470 | |
| 471 | if (queue_size == 0) |
| 472 | queue_size = 256; |
| 473 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 474 | vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); |
| 475 | vring = vec_elt_at_index (vif->cxq_vring, 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 476 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 477 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 478 | ptr = |
| 479 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 480 | vif->numa_node); |
| 481 | if (!ptr) |
| 482 | return vlib_physmem_last_error (vm); |
| 483 | clib_memset (ptr, 0, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 484 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 485 | vring->desc = vr.desc; |
| 486 | vring->avail = vr.avail; |
| 487 | vring->used = vr.used; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 488 | vring->queue_id = queue_num; |
| 489 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
| 490 | |
| 491 | ASSERT (vring->buffers == 0); |
| 492 | |
| 493 | vring->size = queue_size; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 494 | virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 495 | queue_size); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 496 | vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 497 | vring->kick_fd = -1; |
| 498 | |
| 499 | return error; |
| 500 | } |
| 501 | |
| 502 | clib_error_t * |
| 503 | virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num) |
| 504 | { |
| 505 | clib_error_t *error = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 506 | u16 queue_size = 0; |
| 507 | virtio_vring_t *vring; |
| 508 | struct vring vr; |
| 509 | u32 i = 0; |
| 510 | void *ptr = NULL; |
| 511 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 512 | queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 513 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 514 | clib_warning ("queue size is not valid"); |
| 515 | |
| 516 | if (!is_pow2 (queue_size)) |
| 517 | return clib_error_return (0, "ring size must be power of 2"); |
| 518 | |
| 519 | if (queue_size > 32768) |
| 520 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 521 | |
| 522 | if (queue_size == 0) |
| 523 | queue_size = 256; |
| 524 | |
| 525 | if (queue_num % 2) |
| 526 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 527 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), |
| 528 | CLIB_CACHE_LINE_BYTES); |
| 529 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); |
| 530 | clib_spinlock_init (&vring->lockp); |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num), |
| 535 | CLIB_CACHE_LINE_BYTES); |
| 536 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); |
| 537 | } |
| 538 | i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); |
| 539 | i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); |
| 540 | ptr = |
| 541 | vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, |
| 542 | vif->numa_node); |
| 543 | if (!ptr) |
| 544 | return vlib_physmem_last_error (vm); |
| 545 | clib_memset (ptr, 0, i); |
| 546 | vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); |
| 547 | vring->desc = vr.desc; |
| 548 | vring->avail = vr.avail; |
| 549 | vring->used = vr.used; |
| 550 | vring->queue_id = queue_num; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 551 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 552 | vring->flow_table = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 553 | |
| 554 | ASSERT (vring->buffers == 0); |
| 555 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 556 | if (queue_num % 2) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 557 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 558 | virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 559 | queue_size); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 560 | clib_memset_u32 (vring->buffers, ~0, queue_size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 561 | } |
| 562 | else |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 563 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 564 | virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 565 | queue_size); |
| 566 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 567 | vring->size = queue_size; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 568 | if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr)) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 569 | return clib_error_return (0, "error in queue address setup"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 570 | |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 571 | vring->kick_fd = -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 572 | return error; |
| 573 | } |
| 574 | |
| 575 | static void |
| 576 | virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif, |
| 577 | u64 req_features) |
| 578 | { |
| 579 | /* |
| 580 | * if features are not requested |
| 581 | * default: all supported features |
| 582 | */ |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 583 | u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) |
| 584 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 585 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
| 586 | | VIRTIO_FEATURE (VIRTIO_NET_F_MTU) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 587 | | VIRTIO_FEATURE (VIRTIO_NET_F_MAC) |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 588 | | VIRTIO_FEATURE (VIRTIO_NET_F_GSO) |
| 589 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
| 590 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6) |
| 591 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) |
| 592 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
| 593 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6) |
| 594 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 595 | | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
| 596 | | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 597 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
| 598 | | VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 599 | | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 600 | | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
| 601 | | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 602 | |
| 603 | if (req_features == 0) |
| 604 | { |
| 605 | req_features = supported_features; |
| 606 | } |
| 607 | |
| 608 | vif->features = req_features & vif->remote_features & supported_features; |
| 609 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 610 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 611 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 612 | u16 mtu = 0; |
| 613 | mtu = vif->virtio_pci_func->get_mtu (vm, vif); |
| 614 | |
| 615 | if (mtu < 64) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 616 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU); |
| 617 | } |
| 618 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 619 | vif->virtio_pci_func->set_driver_features (vm, vif, vif->features); |
| 620 | vif->features = vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | void |
| 624 | virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif) |
| 625 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 626 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | int |
| 630 | virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif) |
| 631 | { |
| 632 | u8 status = 0; |
| 633 | |
| 634 | /* |
| 635 | * Reset the device |
| 636 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 637 | status = vif->virtio_pci_func->device_reset (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | * Set the Acknowledge status bit |
| 641 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 642 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Set the Driver status bit |
| 646 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 647 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | * Read the status and verify it |
| 651 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 652 | status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 653 | if (! |
| 654 | ((status & VIRTIO_CONFIG_STATUS_ACK) |
| 655 | && (status & VIRTIO_CONFIG_STATUS_DRIVER))) |
| 656 | return -1; |
| 657 | vif->status = status; |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | clib_error_t * |
| 663 | virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif) |
| 664 | { |
| 665 | clib_error_t *error = 0; |
| 666 | struct virtio_pci_cap cap; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 667 | u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 668 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 669 | |
| 670 | if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 671 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 672 | virtio_log_error (vif, "error in reading capabilty list position"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 673 | clib_error_return (error, "error in reading capabilty list position"); |
| 674 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 675 | while (pos) |
| 676 | { |
| 677 | if ((error = |
| 678 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap, |
| 679 | sizeof (cap)))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 680 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 681 | virtio_log_error (vif, "%s [%2x]", |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 682 | "error in reading the capability at", pos); |
| 683 | clib_error_return (error, |
| 684 | "error in reading the capability at [%2x]", pos); |
| 685 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 686 | |
| 687 | if (cap.cap_vndr == PCI_CAP_ID_MSIX) |
| 688 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 689 | u16 flags, table_size, table_size_mask = 0x07FF; |
| 690 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 691 | if ((error = |
| 692 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags, |
| 693 | sizeof (flags)))) |
| 694 | clib_error_return (error, |
| 695 | "error in reading the capability at [%2x]", |
| 696 | pos + 2); |
| 697 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 698 | table_size = flags & table_size_mask; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 699 | virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 700 | "msix interrupt vector table-size", table_size); |
| 701 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 702 | if (flags & PCI_MSIX_ENABLE) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 703 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 704 | virtio_log_debug (vif, "msix interrupt enabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 705 | vif->msix_enabled = VIRTIO_MSIX_ENABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 706 | vif->msix_table_size = table_size; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 707 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 708 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 709 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 710 | virtio_log_debug (vif, "msix interrupt disabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 711 | vif->msix_enabled = VIRTIO_MSIX_DISABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 712 | vif->msix_table_size = 0; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 713 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (cap.cap_vndr != PCI_CAP_ID_VNDR) |
| 717 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 718 | virtio_log_debug (vif, "[%2x] %s %2x ", pos, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 719 | "skipping non VNDR cap id:", cap.cap_vndr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 720 | goto next; |
| 721 | } |
| 722 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 723 | virtio_log_debug (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 724 | "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u", |
| 725 | pos, cap.cfg_type, cap.bar, cap.offset, cap.length); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 726 | switch (cap.cfg_type) |
| 727 | { |
| 728 | case VIRTIO_PCI_CAP_COMMON_CFG: |
| 729 | common_cfg = 1; |
| 730 | break; |
| 731 | case VIRTIO_PCI_CAP_NOTIFY_CFG: |
| 732 | notify_base = 1; |
| 733 | break; |
| 734 | case VIRTIO_PCI_CAP_DEVICE_CFG: |
| 735 | dev_cfg = 1; |
| 736 | break; |
| 737 | case VIRTIO_PCI_CAP_ISR_CFG: |
| 738 | isr = 1; |
| 739 | break; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 740 | case VIRTIO_PCI_CAP_PCI_CFG: |
| 741 | if (cap.bar == 0) |
| 742 | pci_cfg = 1; |
| 743 | break; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 744 | } |
| 745 | next: |
| 746 | pos = cap.cap_next; |
| 747 | } |
| 748 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 749 | vif->virtio_pci_func = &virtio_pci_legacy_func; |
| 750 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 751 | if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0) |
| 752 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 753 | virtio_log_debug (vif, "legacy virtio pci device found"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 754 | return error; |
| 755 | } |
| 756 | |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 757 | if (!pci_cfg) |
| 758 | clib_error_return (error, "modern virtio pci device found"); |
| 759 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 760 | virtio_log_debug (vif, "transitional virtio pci device found"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 761 | return error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static clib_error_t * |
| 765 | virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, |
| 766 | virtio_pci_create_if_args_t * args) |
| 767 | { |
| 768 | clib_error_t *error = 0; |
Mohsin Kazmi | edf3b4b | 2019-10-30 15:00:04 +0100 | [diff] [blame] | 769 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 770 | u8 status = 0; |
| 771 | |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 772 | if ((error = virtio_pci_read_caps (vm, vif))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 773 | { |
| 774 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 775 | virtio_log_error (vif, "Device is not supported"); |
| 776 | clib_error_return (error, "Device is not supported"); |
| 777 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 778 | |
| 779 | if (virtio_pci_reset_device (vm, vif) < 0) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 780 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 781 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 782 | virtio_log_error (vif, "Failed to reset the device"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 783 | clib_error_return (error, "Failed to reset the device"); |
| 784 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 785 | /* |
| 786 | * read device features and negotiate (user) requested features |
| 787 | */ |
| 788 | virtio_pci_read_device_feature (vm, vif); |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 789 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 790 | 0) |
| 791 | { |
| 792 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 793 | "support VIRTIO_RING_F_INDIRECT_DESC features"); |
| 794 | } |
| 795 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 796 | { |
| 797 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 798 | "support VIRTIO_NET_F_MRG_RXBUF features"); |
| 799 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 800 | virtio_negotiate_features (vm, vif, args->features); |
| 801 | |
| 802 | /* |
| 803 | * After FEATURE_OK, driver should not accept new feature bits |
| 804 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 805 | vif->virtio_pci_func->set_status (vm, vif, |
| 806 | VIRTIO_CONFIG_STATUS_FEATURES_OK); |
| 807 | status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 808 | if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK)) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 809 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 810 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 811 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 812 | "error encountered: Device doesn't support requested features"); |
| 813 | clib_error_return (error, "Device doesn't support requested features"); |
| 814 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 815 | vif->status = status; |
| 816 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 817 | /* |
| 818 | * get or set the mac address |
| 819 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 820 | if (virtio_pci_get_mac (vm, vif)) |
| 821 | { |
| 822 | f64 now = vlib_time_now (vm); |
| 823 | u32 rnd; |
| 824 | rnd = (u32) (now * 1e6); |
| 825 | rnd = random_u32 (&rnd); |
| 826 | |
| 827 | memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd)); |
| 828 | vif->mac_addr[0] = 2; |
| 829 | vif->mac_addr[1] = 0xfe; |
| 830 | virtio_pci_set_mac (vm, vif); |
| 831 | } |
| 832 | |
| 833 | virtio_set_net_hdr_size (vif); |
| 834 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 835 | /* |
| 836 | * Initialize the virtqueues |
| 837 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 838 | if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 839 | { |
| 840 | args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY; |
| 841 | goto err; |
| 842 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 843 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 844 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 845 | { |
| 846 | if (vif->msix_table_size <= vif->max_queue_pairs) |
| 847 | { |
| 848 | virtio_log_error (vif, |
| 849 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 850 | vif->msix_table_size, vif->max_queue_pairs); |
| 851 | return clib_error_return (error, |
| 852 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 853 | vif->msix_table_size, |
| 854 | vif->max_queue_pairs); |
| 855 | } |
| 856 | } |
| 857 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 858 | for (int i = 0; i < vif->max_queue_pairs; i++) |
| 859 | { |
| 860 | if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 861 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 862 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 863 | virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue", |
| 864 | RX_QUEUE (i), "initialization"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 865 | error = |
| 866 | clib_error_return (error, "%s (%u) %s", "error in rxq-queue", |
| 867 | RX_QUEUE (i), "initialization"); |
| 868 | goto err; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 869 | } |
| 870 | else |
| 871 | { |
| 872 | vif->num_rxqs++; |
| 873 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 874 | |
Mohsin Kazmi | edf3b4b | 2019-10-30 15:00:04 +0100 | [diff] [blame] | 875 | if (i >= vtm->n_vlib_mains) |
| 876 | { |
| 877 | /* |
| 878 | * There is 1:1 mapping between tx queue and vpp worker thread. |
| 879 | * tx queue 0 is bind with thread index 0, tx queue 1 on thread |
| 880 | * index 1 and so on. |
| 881 | * Multiple worker threads can poll same tx queue when number of |
| 882 | * workers are more than tx queues. In this case, 1:N mapping |
| 883 | * between tx queue and vpp worker thread. |
| 884 | */ |
| 885 | virtio_log_debug (vif, "%s %u, %s", "tx-queue: number", |
| 886 | TX_QUEUE (i), |
| 887 | "no VPP worker thread is available"); |
| 888 | continue; |
| 889 | } |
| 890 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 891 | if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i)))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 892 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 893 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 894 | virtio_log_error (vif, "%s (%u) %s", "error in txq-queue", |
| 895 | TX_QUEUE (i), "initialization"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 896 | error = |
| 897 | clib_error_return (error, "%s (%u) %s", "error in txq-queue", |
| 898 | TX_QUEUE (i), "initialization"); |
| 899 | goto err; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 900 | } |
| 901 | else |
| 902 | { |
| 903 | vif->num_txqs++; |
| 904 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 905 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 906 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 907 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 908 | { |
| 909 | if ((error = |
| 910 | virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2))) |
| 911 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 912 | virtio_log_warning (vif, "%s (%u) %s", "error in control-queue", |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 913 | vif->max_queue_pairs * 2, "initialization"); |
| 914 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 915 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ); |
| 916 | } |
| 917 | } |
| 918 | else |
| 919 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 920 | virtio_log_debug (vif, "control queue is not available"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 921 | vif->cxq_vring = NULL; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * set the msix interrupts |
| 926 | */ |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 927 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 928 | { |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 929 | int i, j; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 930 | if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) == |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 931 | VIRTIO_MSI_NO_VECTOR) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 932 | { |
| 933 | virtio_log_warning (vif, "config vector 0 is not set"); |
| 934 | } |
| 935 | else |
| 936 | { |
| 937 | virtio_log_debug (vif, "config msix vector is set at 0"); |
| 938 | } |
| 939 | for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++) |
| 940 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 941 | if (vif->virtio_pci_func->set_queue_irq (vm, vif, j, |
| 942 | RX_QUEUE (i)) == |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 943 | VIRTIO_MSI_NO_VECTOR) |
| 944 | { |
| 945 | virtio_log_warning (vif, "queue (%u) vector is not set at %u", |
| 946 | RX_QUEUE (i), j); |
| 947 | } |
| 948 | else |
| 949 | { |
| 950 | virtio_log_debug (vif, "%s (%u) %s %u", "queue", |
| 951 | RX_QUEUE (i), "msix vector is set at", j); |
| 952 | } |
| 953 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 954 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 955 | |
| 956 | /* |
| 957 | * set the driver status OK |
| 958 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 959 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK); |
| 960 | vif->status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 961 | err: |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 962 | return error; |
| 963 | } |
| 964 | |
| 965 | void |
| 966 | virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) |
| 967 | { |
| 968 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 969 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 970 | virtio_if_t *vif; |
| 971 | vlib_pci_dev_handle_t h; |
| 972 | clib_error_t *error = 0; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 973 | u32 interrupt_count = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 974 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 975 | /* *INDENT-OFF* */ |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 976 | pool_foreach (vif, vim->interfaces, ({ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 977 | if (vif->pci_addr.as_u32 == args->addr) |
| 978 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 979 | args->rv = VNET_API_ERROR_ADDRESS_IN_USE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 980 | args->error = |
| 981 | clib_error_return (error, "PCI address in use"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 982 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 983 | format_vlib_pci_addr, &args->addr, |
| 984 | " PCI address in use"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 985 | return; |
| 986 | } |
| 987 | })); |
| 988 | /* *INDENT-ON* */ |
| 989 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 990 | pool_get (vim->interfaces, vif); |
| 991 | vif->dev_instance = vif - vim->interfaces; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 992 | vif->per_interface_next_index = ~0; |
| 993 | vif->pci_addr.as_u32 = args->addr; |
| 994 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 995 | if ((error = |
| 996 | vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr, |
| 997 | virtio_pci_device_ids, &h))) |
| 998 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 999 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1000 | args->error = |
| 1001 | clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, |
| 1002 | &vif->pci_addr); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1003 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1004 | format_vlib_pci_addr, &vif->pci_addr, |
| 1005 | "error encountered on pci device open"); |
Benoît Ganne | 8f01183 | 2019-11-20 14:32:38 +0100 | [diff] [blame] | 1006 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | vif->pci_dev_handle = h; |
| 1010 | vlib_pci_set_private_data (vm, h, vif->dev_instance); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1011 | vif->numa_node = vlib_pci_get_numa_node (vm, h); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1012 | vif->type = VIRTIO_IF_TYPE_PCI; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1013 | |
| 1014 | if ((error = vlib_pci_bus_master_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1015 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1016 | virtio_log_error (vif, "error encountered on pci bus master enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1017 | goto error; |
| 1018 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1019 | |
| 1020 | if ((error = vlib_pci_io_region (vm, h, 0))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1021 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1022 | virtio_log_error (vif, "error encountered on pci io region"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1023 | goto error; |
| 1024 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1025 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1026 | interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h); |
| 1027 | if (interrupt_count > 1) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1028 | { |
| 1029 | if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1030 | &virtio_pci_irq_config_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1031 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1032 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1033 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1034 | "error encountered on pci register msix handler 0"); |
| 1035 | goto error; |
| 1036 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1037 | |
| 1038 | if ((error = |
| 1039 | vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1), |
| 1040 | &virtio_pci_irq_queue_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1041 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1042 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1043 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1044 | "error encountered on pci register msix handler 1"); |
| 1045 | goto error; |
| 1046 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1047 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1048 | if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1049 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1050 | virtio_log_error (vif, "error encountered on pci enable msix irq"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1051 | goto error; |
| 1052 | } |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1053 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1054 | virtio_log_debug (vif, "device supports msix interrupts"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1055 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1056 | else if (interrupt_count == 1) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1057 | { |
| 1058 | /* |
| 1059 | * if msix table-size is 1, fall back to intX. |
| 1060 | */ |
| 1061 | if ((error = |
| 1062 | vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler))) |
| 1063 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1064 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1065 | "error encountered on pci register interrupt handler"); |
| 1066 | goto error; |
| 1067 | } |
| 1068 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1069 | virtio_log_debug (vif, "pci register interrupt handler"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1070 | } |
| 1071 | else |
| 1072 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1073 | /* |
| 1074 | * WARN: intX is showing some weird behaviour. |
| 1075 | * Please don't use interrupt mode with UIO driver. |
| 1076 | */ |
| 1077 | vif->support_int_mode = 0; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1078 | virtio_log_debug (vif, "driver is configured in poll mode only"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | if ((error = vlib_pci_intr_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1082 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1083 | virtio_log_error (vif, "error encountered on pci interrupt enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1084 | goto error; |
| 1085 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1086 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1087 | if ((error = virtio_pci_device_init (vm, vif, args))) |
| 1088 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1089 | virtio_log_error (vif, "error encountered on device init"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1090 | goto error; |
| 1091 | } |
| 1092 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1093 | /* create interface */ |
| 1094 | error = ethernet_register_interface (vnm, virtio_device_class.index, |
| 1095 | vif->dev_instance, vif->mac_addr, |
| 1096 | &vif->hw_if_index, |
| 1097 | virtio_pci_flag_change); |
| 1098 | |
| 1099 | if (error) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1100 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1101 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1102 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1103 | "error encountered on ethernet register interface"); |
| 1104 | goto error; |
| 1105 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1106 | |
| 1107 | vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 1108 | vif->sw_if_index = sw->sw_if_index; |
| 1109 | args->sw_if_index = sw->sw_if_index; |
| 1110 | |
| 1111 | vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 1112 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 1113 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 1114 | virtio_input_node.index); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1115 | u32 i = 0; |
| 1116 | vec_foreach_index (i, vif->rxq_vrings) |
| 1117 | { |
| 1118 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0); |
| 1119 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i)); |
| 1120 | /* Set default rx mode to POLLING */ |
| 1121 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i, |
| 1122 | VNET_HW_INTERFACE_RX_MODE_POLLING); |
| 1123 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1124 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 1125 | { |
| 1126 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 1127 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 1128 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 1129 | } |
| 1130 | else |
| 1131 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1132 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1133 | virtio_pci_offloads (vm, vif, args->gso_enabled, |
| 1134 | args->checksum_offload_enabled); |
| 1135 | |
| 1136 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 1137 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1138 | { |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1139 | if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs)) |
| 1140 | virtio_log_warning (vif, "multiqueue is not set"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1141 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1142 | return; |
| 1143 | |
| 1144 | error: |
| 1145 | virtio_pci_delete_if (vm, vif); |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1146 | if (args->rv == 0) |
| 1147 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1148 | args->error = error; |
| 1149 | } |
| 1150 | |
| 1151 | int |
| 1152 | virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) |
| 1153 | { |
| 1154 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1155 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1156 | u32 i = 0; |
| 1157 | |
| 1158 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1159 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1160 | |
| 1161 | vlib_pci_intr_disable (vm, vif->pci_dev_handle); |
| 1162 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1163 | for (i = 0; i < vif->max_queue_pairs; i++) |
| 1164 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 1165 | vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i)); |
| 1166 | vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i)); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 1170 | vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1171 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame^] | 1172 | vif->virtio_pci_func->device_reset (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1173 | |
| 1174 | if (vif->hw_if_index) |
| 1175 | { |
| 1176 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1177 | vec_foreach_index (i, vif->rxq_vrings) |
| 1178 | { |
| 1179 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i); |
| 1180 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1181 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 1182 | } |
| 1183 | |
| 1184 | vlib_pci_device_close (vm, vif->pci_dev_handle); |
| 1185 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1186 | vec_foreach_index (i, vif->rxq_vrings) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1187 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1188 | virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1189 | if (vring->kick_fd != -1) |
| 1190 | close (vring->kick_fd); |
| 1191 | if (vring->used) |
| 1192 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1193 | virtio_free_rx_buffers (vm, vring); |
| 1194 | } |
| 1195 | vec_free (vring->buffers); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1196 | vlib_physmem_free (vm, vring->desc); |
| 1197 | } |
| 1198 | |
| 1199 | vec_foreach_index (i, vif->txq_vrings) |
| 1200 | { |
| 1201 | virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); |
| 1202 | if (vring->kick_fd != -1) |
| 1203 | close (vring->kick_fd); |
| 1204 | if (vring->used) |
| 1205 | { |
| 1206 | virtio_free_used_desc (vm, vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1207 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1208 | vec_free (vring->buffers); |
Steven Luong | e558496 | 2019-06-25 13:47:06 -0700 | [diff] [blame] | 1209 | clib_spinlock_free (&vring->lockp); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1210 | vlib_physmem_free (vm, vring->desc); |
| 1211 | } |
| 1212 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1213 | if (vif->cxq_vring != NULL) |
| 1214 | { |
| 1215 | u16 last = vif->cxq_vring->last_used_idx; |
| 1216 | u16 n_left = vif->cxq_vring->used->idx - last; |
| 1217 | while (n_left) |
| 1218 | { |
| 1219 | last++; |
| 1220 | n_left--; |
| 1221 | } |
| 1222 | |
| 1223 | vif->cxq_vring->last_used_idx = last; |
| 1224 | vlib_physmem_free (vm, vif->cxq_vring->desc); |
| 1225 | } |
| 1226 | |
| 1227 | vec_free (vif->rxq_vrings); |
| 1228 | vec_free (vif->txq_vrings); |
| 1229 | vec_free (vif->cxq_vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1230 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1231 | clib_error_free (vif->error); |
| 1232 | memset (vif, 0, sizeof (*vif)); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1233 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1238 | int |
| 1239 | virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif, |
| 1240 | int gso_enabled, |
| 1241 | int checksum_offload_enabled, |
| 1242 | int offloads_disabled) |
| 1243 | { |
| 1244 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1245 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1246 | |
| 1247 | if (gso_enabled) |
| 1248 | virtio_pci_offloads (vm, vif, 1, 0); |
| 1249 | else if (checksum_offload_enabled) |
| 1250 | virtio_pci_offloads (vm, vif, 0, 1); |
| 1251 | else if (offloads_disabled) |
| 1252 | virtio_pci_offloads (vm, vif, 0, 0); |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1257 | /* |
| 1258 | * fd.io coding-style-patch-verification: ON |
| 1259 | * |
| 1260 | * Local Variables: |
| 1261 | * eval: (c-set-style "gnu") |
| 1262 | * End: |
| 1263 | */ |