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> |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 27 | #include <vnet/interface/rx_queue_funcs.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 28 | |
| 29 | #define PCI_VENDOR_ID_VIRTIO 0x1af4 |
| 30 | #define PCI_DEVICE_ID_VIRTIO_NIC 0x1000 |
| 31 | /* Doesn't support modern device */ |
| 32 | #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041 |
| 33 | |
| 34 | #define PCI_CAPABILITY_LIST 0x34 |
| 35 | #define PCI_CAP_ID_VNDR 0x09 |
| 36 | #define PCI_CAP_ID_MSIX 0x11 |
| 37 | |
| 38 | #define PCI_MSIX_ENABLE 0x8000 |
| 39 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 40 | static pci_device_id_t virtio_pci_device_ids[] = { |
| 41 | { |
| 42 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 43 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC}, |
| 44 | { |
| 45 | .vendor_id = PCI_VENDOR_ID_VIRTIO, |
| 46 | .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN}, |
| 47 | {0}, |
| 48 | }; |
| 49 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 50 | static u32 |
| 51 | virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, |
| 52 | u32 flags) |
| 53 | { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static clib_error_t * |
| 58 | virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif) |
| 59 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 60 | clib_error_t *error = 0; |
| 61 | u16 max_queue_pairs = 1; |
| 62 | |
| 63 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 64 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 65 | 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] | 66 | } |
| 67 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 68 | virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 69 | if (max_queue_pairs < 1 || max_queue_pairs > 0x8000) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 70 | return clib_error_return (error, "max queue pair is %x," |
| 71 | " should be in range [1, 0x8000]", |
| 72 | max_queue_pairs); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 73 | |
| 74 | vif->max_queue_pairs = max_queue_pairs; |
| 75 | return error; |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 80 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 81 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
| 82 | vif->virtio_pci_func->set_mac (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static u32 |
| 86 | virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif) |
| 87 | { |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 88 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 89 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 90 | vif->virtio_pci_func->get_mac (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | static u16 |
| 97 | virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif) |
| 98 | { |
| 99 | /* |
| 100 | * Minimal driver: assumes link is up |
| 101 | */ |
| 102 | u16 status = 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 103 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)) |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 104 | status = vif->virtio_pci_func->get_device_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 105 | return status; |
| 106 | } |
| 107 | |
| 108 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 109 | virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 110 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 111 | { |
| 112 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 113 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 114 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 115 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 116 | line--; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 117 | u16 qid = line; |
| 118 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 119 | vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 120 | vnet_hw_if_rx_queue_set_int_pending (vnm, vring->queue_index); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 124 | virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, |
| 125 | u16 line) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 126 | { |
| 127 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 128 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 129 | uword pd = vlib_pci_get_private_data (vm, h); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 130 | virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 131 | |
| 132 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 133 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 134 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 135 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 136 | } |
| 137 | else |
| 138 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 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 | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 197 | static int |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 198 | virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif, |
| 199 | virtio_ctrl_msg_t * data, u32 len) |
| 200 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 201 | vnet_virtio_vring_t *vring = vif->cxq_vring; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 202 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
| 203 | virtio_ctrl_msg_t result; |
| 204 | u32 buffer_index; |
| 205 | vlib_buffer_t *b; |
| 206 | u16 used, next; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 207 | u16 sz = vring->queue_size; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 208 | u16 flags = 0, first_desc_flags = 0; |
| 209 | |
| 210 | used = vring->desc_in_use; |
| 211 | next = vring->desc_next; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 212 | vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next]; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 213 | |
| 214 | if (vlib_buffer_alloc (vm, &buffer_index, 1)) |
| 215 | b = vlib_get_buffer (vm, buffer_index); |
| 216 | else |
| 217 | return VIRTIO_NET_ERR; |
| 218 | /* |
| 219 | * current_data may not be initialized with 0 and may contain |
| 220 | * previous offset. |
| 221 | */ |
| 222 | b->current_data = 0; |
| 223 | clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t)); |
| 224 | |
| 225 | first_desc_flags = VRING_DESC_F_NEXT; |
| 226 | if (vring->avail_wrap_counter) |
| 227 | { |
| 228 | first_desc_flags |= VRING_DESC_F_AVAIL; |
| 229 | first_desc_flags &= ~VRING_DESC_F_USED; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | first_desc_flags &= ~VRING_DESC_F_AVAIL; |
| 234 | first_desc_flags |= VRING_DESC_F_USED; |
| 235 | } |
| 236 | d->addr = vlib_buffer_get_current_pa (vm, b); |
| 237 | d->len = sizeof (virtio_net_ctrl_hdr_t); |
| 238 | d->id = next; |
| 239 | |
| 240 | next++; |
| 241 | if (next >= sz) |
| 242 | { |
| 243 | next = 0; |
| 244 | vring->avail_wrap_counter ^= 1; |
| 245 | } |
| 246 | used++; |
| 247 | |
| 248 | d = &vring->packed_desc[next]; |
| 249 | flags = VRING_DESC_F_NEXT; |
| 250 | if (vring->avail_wrap_counter) |
| 251 | { |
| 252 | flags |= VRING_DESC_F_AVAIL; |
| 253 | flags &= ~VRING_DESC_F_USED; |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | flags &= ~VRING_DESC_F_AVAIL; |
| 258 | flags |= VRING_DESC_F_USED; |
| 259 | } |
| 260 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
| 261 | STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data); |
| 262 | d->len = len; |
| 263 | d->id = next; |
| 264 | d->flags = flags; |
| 265 | |
| 266 | next++; |
| 267 | if (next >= sz) |
| 268 | { |
| 269 | next = 0; |
| 270 | vring->avail_wrap_counter ^= 1; |
| 271 | } |
| 272 | used++; |
| 273 | |
| 274 | d = &vring->packed_desc[next]; |
| 275 | flags = VRING_DESC_F_WRITE; |
| 276 | if (vring->avail_wrap_counter) |
| 277 | { |
| 278 | flags |= VRING_DESC_F_AVAIL; |
| 279 | flags &= ~VRING_DESC_F_USED; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | flags &= ~VRING_DESC_F_AVAIL; |
| 284 | flags |= VRING_DESC_F_USED; |
| 285 | } |
| 286 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
| 287 | STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status); |
| 288 | d->len = sizeof (data->status); |
| 289 | d->id = next; |
| 290 | d->flags = flags; |
| 291 | |
| 292 | next++; |
| 293 | if (next >= sz) |
| 294 | { |
| 295 | next = 0; |
| 296 | vring->avail_wrap_counter ^= 1; |
| 297 | } |
| 298 | used++; |
| 299 | |
| 300 | CLIB_MEMORY_STORE_BARRIER (); |
| 301 | vring->packed_desc[vring->desc_next].flags = first_desc_flags; |
| 302 | vring->desc_next = next; |
| 303 | vring->desc_in_use = used; |
| 304 | CLIB_MEMORY_BARRIER (); |
| 305 | if (vring->device_event->flags != VRING_EVENT_F_DISABLE) |
| 306 | { |
| 307 | virtio_kick (vm, vring, vif); |
| 308 | } |
| 309 | |
| 310 | u16 last = vring->last_used_idx; |
| 311 | d = &vring->packed_desc[last]; |
| 312 | do |
| 313 | { |
| 314 | flags = d->flags; |
| 315 | } |
| 316 | while ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7) |
| 317 | || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15)); |
| 318 | |
| 319 | last += 3; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 320 | if (last >= vring->queue_size) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 321 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 322 | last = last - vring->queue_size; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 323 | vring->used_wrap_counter ^= 1; |
| 324 | } |
| 325 | vring->desc_in_use -= 3; |
| 326 | vring->last_used_idx = last; |
| 327 | |
| 328 | CLIB_MEMORY_BARRIER (); |
| 329 | clib_memcpy (&result, vlib_buffer_get_current (b), |
| 330 | sizeof (virtio_ctrl_msg_t)); |
| 331 | virtio_log_debug (vif, "ctrl-queue: status %u", result.status); |
| 332 | status = result.status; |
| 333 | vlib_buffer_free (vm, &buffer_index, 1); |
| 334 | return status; |
| 335 | } |
| 336 | |
| 337 | static int |
| 338 | virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif, |
| 339 | virtio_ctrl_msg_t * data, u32 len) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 340 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 341 | vnet_virtio_vring_t *vring = vif->cxq_vring; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 342 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
| 343 | virtio_ctrl_msg_t result; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 344 | u32 buffer_index; |
| 345 | vlib_buffer_t *b; |
| 346 | u16 used, next, avail; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 347 | u16 sz = vring->queue_size; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 348 | u16 mask = sz - 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 349 | |
| 350 | used = vring->desc_in_use; |
| 351 | next = vring->desc_next; |
| 352 | avail = vring->avail->idx; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 353 | vnet_virtio_vring_desc_t *d = &vring->desc[next]; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 354 | |
| 355 | if (vlib_buffer_alloc (vm, &buffer_index, 1)) |
| 356 | b = vlib_get_buffer (vm, buffer_index); |
| 357 | else |
| 358 | return VIRTIO_NET_ERR; |
| 359 | /* |
| 360 | * current_data may not be initialized with 0 and may contain |
| 361 | * previous offset. |
| 362 | */ |
| 363 | b->current_data = 0; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 364 | clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t)); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 365 | d->flags = VRING_DESC_F_NEXT; |
| 366 | d->addr = vlib_buffer_get_current_pa (vm, b); |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 367 | d->len = sizeof (virtio_net_ctrl_hdr_t); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 368 | vring->avail->ring[avail & mask] = next; |
| 369 | avail++; |
| 370 | next = (next + 1) & mask; |
| 371 | d->next = next; |
| 372 | used++; |
| 373 | |
| 374 | d = &vring->desc[next]; |
| 375 | d->flags = VRING_DESC_F_NEXT; |
| 376 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 377 | STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 378 | d->len = len; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 379 | next = (next + 1) & mask; |
| 380 | d->next = next; |
| 381 | used++; |
| 382 | |
| 383 | d = &vring->desc[next]; |
| 384 | d->flags = VRING_DESC_F_WRITE; |
| 385 | d->addr = vlib_buffer_get_current_pa (vm, b) + |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 386 | STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 387 | d->len = sizeof (data->status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 388 | next = (next + 1) & mask; |
| 389 | used++; |
| 390 | |
| 391 | CLIB_MEMORY_STORE_BARRIER (); |
| 392 | vring->avail->idx = avail; |
| 393 | vring->desc_next = next; |
| 394 | vring->desc_in_use = used; |
| 395 | |
| 396 | if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0) |
| 397 | { |
| 398 | virtio_kick (vm, vring, vif); |
| 399 | } |
| 400 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 401 | u16 last = vring->last_used_idx, n_left = 0; |
| 402 | n_left = vring->used->idx - last; |
| 403 | |
| 404 | while (n_left) |
| 405 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 406 | vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask]; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 407 | u16 slot = e->id; |
| 408 | |
| 409 | d = &vring->desc[slot]; |
| 410 | while (d->flags & VRING_DESC_F_NEXT) |
| 411 | { |
| 412 | used--; |
| 413 | slot = d->next; |
| 414 | d = &vring->desc[slot]; |
| 415 | } |
| 416 | used--; |
| 417 | last++; |
| 418 | n_left--; |
| 419 | } |
| 420 | vring->desc_in_use = used; |
| 421 | vring->last_used_idx = last; |
| 422 | |
| 423 | CLIB_MEMORY_BARRIER (); |
| 424 | clib_memcpy (&result, vlib_buffer_get_current (b), |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 425 | sizeof (virtio_ctrl_msg_t)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 426 | virtio_log_debug (vif, "ctrl-queue: status %u", result.status); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 427 | status = result.status; |
| 428 | vlib_buffer_free (vm, &buffer_index, 1); |
| 429 | return status; |
| 430 | } |
| 431 | |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 432 | static int |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 433 | virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif, |
| 434 | virtio_ctrl_msg_t * data, u32 len) |
| 435 | { |
| 436 | if (vif->is_packed) |
| 437 | return virtio_pci_send_ctrl_msg_packed (vm, vif, data, len); |
| 438 | else |
| 439 | return virtio_pci_send_ctrl_msg_split (vm, vif, data, len); |
| 440 | } |
| 441 | |
| 442 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 443 | virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 444 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 445 | virtio_ctrl_msg_t offload_hdr; |
| 446 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 447 | |
| 448 | offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 449 | offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 450 | offload_hdr.status = VIRTIO_NET_ERR; |
| 451 | u64 offloads = 0ULL; |
| 452 | clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads)); |
| 453 | |
| 454 | status = |
| 455 | virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads)); |
| 456 | virtio_log_debug (vif, "disable offloads"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 457 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 458 | vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 459 | return status; |
| 460 | } |
| 461 | |
| 462 | static int |
| 463 | virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif) |
| 464 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 465 | virtio_ctrl_msg_t csum_offload_hdr; |
| 466 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 467 | |
| 468 | csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 469 | csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 470 | csum_offload_hdr.status = VIRTIO_NET_ERR; |
| 471 | u64 offloads = 0ULL; |
| 472 | offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM); |
| 473 | clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads)); |
| 474 | |
| 475 | status = |
| 476 | virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads)); |
| 477 | virtio_log_debug (vif, "enable checksum offload"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 478 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 479 | vif->features = vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 480 | return status; |
| 481 | } |
| 482 | |
| 483 | static int |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 484 | virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif) |
| 485 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 486 | virtio_ctrl_msg_t gso_hdr; |
| 487 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 488 | |
| 489 | gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS; |
| 490 | gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET; |
| 491 | gso_hdr.status = VIRTIO_NET_ERR; |
| 492 | u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 493 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 494 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 495 | clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads)); |
| 496 | |
| 497 | status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads)); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 498 | virtio_log_debug (vif, "enable gso"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 499 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
| 500 | vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 501 | return status; |
| 502 | } |
| 503 | |
| 504 | static int |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 505 | virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, |
| 506 | int csum_offload_enabled) |
| 507 | { |
| 508 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 509 | vnet_hw_if_caps_change_t cc = {}; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 510 | |
| 511 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 512 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))) |
| 513 | { |
| 514 | if (gso_enabled |
| 515 | && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | |
| 516 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)))) |
| 517 | { |
| 518 | if (virtio_pci_enable_gso (vm, vif)) |
| 519 | { |
| 520 | virtio_log_warning (vif, "gso is not enabled"); |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | vif->gso_enabled = 1; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 525 | vif->csum_offload_enabled = 1; |
| 526 | cc.val = cc.mask = VNET_HW_IF_CAP_TCP_GSO | |
| 527 | VNET_HW_IF_CAP_TX_TCP_CKSUM | |
| 528 | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | else if (csum_offload_enabled |
| 532 | && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM))) |
| 533 | { |
| 534 | if (virtio_pci_enable_checksum_offload (vm, vif)) |
| 535 | { |
| 536 | virtio_log_warning (vif, "checksum offload is not enabled"); |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | vif->csum_offload_enabled = 1; |
| 541 | vif->gso_enabled = 0; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 542 | cc.val = |
Damjan Marion | d4f88cc | 2022-01-05 01:52:38 +0100 | [diff] [blame] | 543 | VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 544 | cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM | |
| 545 | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | if (virtio_pci_disable_offload (vm, vif)) |
| 551 | { |
| 552 | virtio_log_warning (vif, "offloads are not disabled"); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | vif->csum_offload_enabled = 0; |
| 557 | vif->gso_enabled = 0; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 558 | cc.val = 0; |
| 559 | cc.mask = VNET_HW_IF_CAP_L4_TX_CKSUM | VNET_HW_IF_CAP_TCP_GSO; |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 564 | if (cc.mask) |
| 565 | vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); |
| 566 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static int |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 571 | virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif, |
| 572 | u16 num_queues) |
| 573 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 574 | virtio_ctrl_msg_t mq_hdr; |
| 575 | virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 576 | |
| 577 | mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ; |
| 578 | mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; |
| 579 | mq_hdr.status = VIRTIO_NET_ERR; |
| 580 | clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues)); |
| 581 | |
| 582 | 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] | 583 | virtio_log_debug (vif, "multi-queue enable %u queues", num_queues); |
Mohsin Kazmi | 0549378 | 2019-05-01 14:26:17 +0200 | [diff] [blame] | 584 | return status; |
| 585 | } |
| 586 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 587 | static u8 |
| 588 | virtio_pci_queue_size_valid (u16 qsz) |
| 589 | { |
| 590 | if (qsz < 64 || qsz > 4096) |
| 591 | return 0; |
| 592 | if ((qsz % 64) != 0) |
| 593 | return 0; |
| 594 | return 1; |
| 595 | } |
| 596 | |
| 597 | clib_error_t * |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 598 | virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, |
| 599 | u16 queue_num) |
| 600 | { |
| 601 | clib_error_t *error = 0; |
| 602 | u16 queue_size = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 603 | vnet_virtio_vring_t *vring; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 604 | u32 i = 0; |
| 605 | void *ptr = NULL; |
| 606 | |
| 607 | queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num); |
| 608 | |
| 609 | if (queue_size > 32768) |
| 610 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 611 | |
| 612 | if (queue_size == 0) |
| 613 | queue_size = 256; |
| 614 | |
| 615 | vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); |
| 616 | vring = vec_elt_at_index (vif->cxq_vring, 0); |
| 617 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 618 | i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + |
| 619 | sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN - |
| 620 | 1) & |
| 621 | ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) + |
| 622 | sizeof (vnet_virtio_vring_desc_event_t); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 623 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 624 | ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, |
| 625 | vif->numa_node); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 626 | if (!ptr) |
| 627 | return vlib_physmem_last_error (vm); |
| 628 | clib_memset (ptr, 0, i); |
| 629 | |
| 630 | vring->packed_desc = ptr; |
| 631 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 632 | vring->driver_event = |
| 633 | ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t)); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 634 | vring->driver_event->off_wrap = 0; |
| 635 | vring->driver_event->flags = VRING_EVENT_F_DISABLE; |
| 636 | |
| 637 | vring->device_event = |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 638 | ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + |
| 639 | sizeof (vnet_virtio_vring_desc_event_t) + |
| 640 | VNET_VIRTIO_PCI_VRING_ALIGN - 1) & |
| 641 | ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 642 | vring->device_event->off_wrap = 0; |
| 643 | vring->device_event->flags = 0; |
| 644 | |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 645 | vring->total_packets = 0; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 646 | vring->queue_id = queue_num; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 647 | vring->queue_size = queue_size; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 648 | vring->avail_wrap_counter = 1; |
| 649 | vring->used_wrap_counter = 1; |
| 650 | |
| 651 | ASSERT (vring->buffers == 0); |
| 652 | |
| 653 | virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, |
| 654 | queue_size); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 655 | vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 656 | vring->queue_notify_offset = |
| 657 | vif->notify_off_multiplier * |
| 658 | vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num); |
| 659 | virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u", |
| 660 | queue_num, vring->queue_notify_offset); |
| 661 | return error; |
| 662 | } |
| 663 | |
| 664 | clib_error_t * |
| 665 | virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, |
| 666 | u16 queue_num) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 667 | { |
| 668 | clib_error_t *error = 0; |
| 669 | u16 queue_size = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 670 | vnet_virtio_vring_t *vring; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 671 | u32 i = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 672 | void *ptr = NULL; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 673 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 674 | 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] | 675 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 676 | clib_warning ("queue size is not valid"); |
| 677 | |
| 678 | if (!is_pow2 (queue_size)) |
| 679 | return clib_error_return (0, "ring size must be power of 2"); |
| 680 | |
| 681 | if (queue_size > 32768) |
| 682 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 683 | |
| 684 | if (queue_size == 0) |
| 685 | queue_size = 256; |
| 686 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 687 | vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); |
| 688 | vring = vec_elt_at_index (vif->cxq_vring, 0); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 689 | i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN); |
| 690 | i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN); |
| 691 | ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, |
| 692 | vif->numa_node); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 693 | if (!ptr) |
| 694 | return vlib_physmem_last_error (vm); |
| 695 | clib_memset (ptr, 0, i); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 696 | vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 697 | vring->queue_id = queue_num; |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 698 | vring->total_packets = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 699 | |
| 700 | ASSERT (vring->buffers == 0); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 701 | virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 702 | queue_size); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 703 | vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring); |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 704 | vring->queue_notify_offset = |
| 705 | vif->notify_off_multiplier * |
| 706 | vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num); |
| 707 | virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u", |
| 708 | queue_num, vring->queue_notify_offset); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 709 | |
| 710 | return error; |
| 711 | } |
| 712 | |
| 713 | clib_error_t * |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 714 | virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif, |
| 715 | u16 queue_num) |
| 716 | { |
| 717 | if (vif->is_packed) |
| 718 | return virtio_pci_control_vring_packed_init (vm, vif, queue_num); |
| 719 | else |
| 720 | return virtio_pci_control_vring_split_init (vm, vif, queue_num); |
| 721 | } |
| 722 | |
| 723 | clib_error_t * |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 724 | virtio_pci_vring_split_init (vlib_main_t *vm, virtio_if_t *vif, u16 queue_num, |
| 725 | u16 txq_size) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 726 | { |
| 727 | clib_error_t *error = 0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 728 | u16 queue_size = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 729 | vnet_virtio_vring_t *vring; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 730 | u32 i = 0; |
| 731 | void *ptr = NULL; |
| 732 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 733 | 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] | 734 | if (!virtio_pci_queue_size_valid (queue_size)) |
| 735 | clib_warning ("queue size is not valid"); |
| 736 | |
| 737 | if (!is_pow2 (queue_size)) |
| 738 | return clib_error_return (0, "ring size must be power of 2"); |
| 739 | |
| 740 | if (queue_size > 32768) |
| 741 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 742 | |
| 743 | if (queue_size == 0) |
| 744 | queue_size = 256; |
| 745 | |
| 746 | if (queue_num % 2) |
| 747 | { |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 748 | if (txq_size) |
| 749 | { |
| 750 | virtio_log_debug (vif, "tx-queue: number %u, default-size %u", |
| 751 | queue_num, queue_size); |
| 752 | vif->virtio_pci_func->set_queue_size (vm, vif, queue_num, txq_size); |
| 753 | queue_size = |
| 754 | vif->virtio_pci_func->get_queue_size (vm, vif, queue_num); |
| 755 | virtio_log_debug (vif, "tx-queue: number %u, new size %u", queue_num, |
| 756 | queue_size); |
| 757 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 758 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), |
| 759 | CLIB_CACHE_LINE_BYTES); |
| 760 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 761 | clib_spinlock_init (&vring->lockp); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 762 | } |
| 763 | else |
| 764 | { |
| 765 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num), |
| 766 | CLIB_CACHE_LINE_BYTES); |
| 767 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); |
| 768 | } |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 769 | i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN); |
| 770 | i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN); |
| 771 | ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, |
| 772 | vif->numa_node); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 773 | if (!ptr) |
| 774 | return vlib_physmem_last_error (vm); |
| 775 | clib_memset (ptr, 0, i); |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 776 | vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 777 | vring->queue_id = queue_num; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 778 | vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 779 | vring->flow_table = 0; |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 780 | vring->total_packets = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 781 | |
| 782 | ASSERT (vring->buffers == 0); |
| 783 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 784 | if (queue_num % 2) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 785 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 786 | virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 787 | queue_size); |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 788 | clib_memset_u32 (vring->buffers, ~0, queue_size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 789 | } |
| 790 | else |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 791 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 792 | virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 793 | queue_size); |
| 794 | } |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 795 | vring->queue_size = queue_size; |
| 796 | if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring)) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 797 | return clib_error_return (0, "error in queue address setup"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 798 | |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 799 | vring->queue_notify_offset = |
| 800 | vif->notify_off_multiplier * |
| 801 | vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num); |
| 802 | virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u", |
| 803 | queue_num, vring->queue_notify_offset); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 804 | return error; |
| 805 | } |
| 806 | |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 807 | clib_error_t * |
| 808 | virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, |
| 809 | u16 queue_num) |
| 810 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 811 | clib_error_t *error = 0; |
| 812 | u16 queue_size = 0; |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 813 | vnet_virtio_vring_t *vring; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 814 | u32 i = 0; |
| 815 | void *ptr = NULL; |
| 816 | |
| 817 | queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num); |
| 818 | |
| 819 | if (queue_size > 32768) |
| 820 | return clib_error_return (0, "ring size must be 32768 or lower"); |
| 821 | |
| 822 | if (queue_size == 0) |
| 823 | queue_size = 256; |
| 824 | |
| 825 | if (queue_num % 2) |
| 826 | { |
| 827 | vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), |
| 828 | CLIB_CACHE_LINE_BYTES); |
| 829 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 830 | clib_spinlock_init (&vring->lockp); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 831 | } |
| 832 | else |
| 833 | { |
| 834 | vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num), |
| 835 | CLIB_CACHE_LINE_BYTES); |
| 836 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); |
| 837 | } |
| 838 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 839 | i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + |
| 840 | sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN - |
| 841 | 1) & |
| 842 | ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) + |
| 843 | sizeof (vnet_virtio_vring_desc_event_t); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 844 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 845 | ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, |
| 846 | vif->numa_node); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 847 | if (!ptr) |
| 848 | return vlib_physmem_last_error (vm); |
| 849 | |
| 850 | clib_memset (ptr, 0, i); |
| 851 | vring->packed_desc = ptr; |
| 852 | |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 853 | vring->driver_event = |
| 854 | ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t)); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 855 | vring->driver_event->off_wrap = 0; |
| 856 | vring->driver_event->flags = VRING_EVENT_F_DISABLE; |
| 857 | |
| 858 | vring->device_event = |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 859 | ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + |
| 860 | sizeof (vnet_virtio_vring_desc_event_t) + |
| 861 | VNET_VIRTIO_PCI_VRING_ALIGN - 1) & |
| 862 | ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 863 | vring->device_event->off_wrap = 0; |
| 864 | vring->device_event->flags = 0; |
| 865 | |
| 866 | vring->queue_id = queue_num; |
| 867 | |
| 868 | vring->avail_wrap_counter = 1; |
| 869 | vring->used_wrap_counter = 1; |
Mohsin Kazmi | dd0144a | 2022-09-14 11:25:54 +0000 | [diff] [blame] | 870 | vring->total_packets = 0; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 871 | |
| 872 | ASSERT (vring->buffers == 0); |
| 873 | vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); |
| 874 | if (queue_num % 2) |
| 875 | { |
| 876 | virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num, |
| 877 | queue_size); |
| 878 | clib_memset_u32 (vring->buffers, ~0, queue_size); |
| 879 | } |
| 880 | else |
| 881 | { |
| 882 | virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, |
| 883 | queue_size); |
| 884 | } |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 885 | vring->queue_size = queue_size; |
| 886 | if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring)) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 887 | return clib_error_return (0, "error in queue address setup"); |
| 888 | |
| 889 | vring->queue_notify_offset = |
| 890 | vif->notify_off_multiplier * |
| 891 | vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num); |
| 892 | virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u", |
| 893 | queue_num, vring->queue_notify_offset); |
| 894 | |
| 895 | return error; |
| 896 | } |
| 897 | |
| 898 | clib_error_t * |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 899 | virtio_pci_vring_init (vlib_main_t *vm, virtio_if_t *vif, u16 queue_num, |
| 900 | u16 txq_size) |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 901 | { |
| 902 | if (vif->is_packed) |
| 903 | return virtio_pci_vring_packed_init (vm, vif, queue_num); |
| 904 | else |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 905 | return virtio_pci_vring_split_init (vm, vif, queue_num, txq_size); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 908 | static void |
| 909 | virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif, |
| 910 | u64 req_features) |
| 911 | { |
| 912 | /* |
| 913 | * if features are not requested |
| 914 | * default: all supported features |
| 915 | */ |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 916 | u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) |
| 917 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |
| 918 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
| 919 | | VIRTIO_FEATURE (VIRTIO_NET_F_MTU) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 920 | | VIRTIO_FEATURE (VIRTIO_NET_F_MAC) |
Mohsin Kazmi | bbd6b74 | 2019-05-02 13:54:59 +0200 | [diff] [blame] | 921 | | VIRTIO_FEATURE (VIRTIO_NET_F_GSO) |
| 922 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |
| 923 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6) |
| 924 | | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) |
| 925 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
| 926 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6) |
| 927 | | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 928 | | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
| 929 | | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 930 | | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
| 931 | | VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 932 | | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 933 | | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
| 934 | | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 935 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 936 | if (vif->is_modern) |
| 937 | supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 938 | |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 939 | if (vif->is_packed) |
| 940 | { |
| 941 | supported_features |= |
| 942 | (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) | |
| 943 | VIRTIO_FEATURE (VIRTIO_F_IN_ORDER)); |
| 944 | } |
| 945 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 946 | if (req_features == 0) |
| 947 | { |
| 948 | req_features = supported_features; |
| 949 | } |
| 950 | |
| 951 | vif->features = req_features & vif->remote_features & supported_features; |
| 952 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 953 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 954 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 955 | u16 mtu = 0; |
| 956 | mtu = vif->virtio_pci_func->get_mtu (vm, vif); |
| 957 | |
| 958 | if (mtu < 64) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 959 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU); |
| 960 | } |
| 961 | |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 962 | if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0) |
| 963 | vif->is_packed = 0; |
| 964 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 965 | vif->virtio_pci_func->set_driver_features (vm, vif, vif->features); |
| 966 | vif->features = vif->virtio_pci_func->get_driver_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | void |
| 970 | virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif) |
| 971 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 972 | vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | int |
| 976 | virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif) |
| 977 | { |
| 978 | u8 status = 0; |
| 979 | |
| 980 | /* |
| 981 | * Reset the device |
| 982 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 983 | status = vif->virtio_pci_func->device_reset (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 984 | |
| 985 | /* |
| 986 | * Set the Acknowledge status bit |
| 987 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 988 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 989 | |
| 990 | /* |
| 991 | * Set the Driver status bit |
| 992 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 993 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * Read the status and verify it |
| 997 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 998 | status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 999 | if ((status & VIRTIO_CONFIG_STATUS_ACK) |
| 1000 | && (status & VIRTIO_CONFIG_STATUS_DRIVER)) |
| 1001 | vif->status = status; |
| 1002 | else |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1003 | return -1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | clib_error_t * |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1009 | virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1010 | { |
| 1011 | clib_error_t *error = 0; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 1012 | virtio_pci_cap_t cap; |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1013 | u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1014 | vlib_pci_dev_handle_t h = vif->pci_dev_handle; |
| 1015 | |
| 1016 | 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] | 1017 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1018 | virtio_log_error (vif, "error in reading capabilty list position"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 1019 | return clib_error_return (error, |
| 1020 | "error in reading capabilty list position"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1021 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1022 | while (pos) |
| 1023 | { |
| 1024 | if ((error = |
| 1025 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap, |
| 1026 | sizeof (cap)))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1027 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1028 | virtio_log_error (vif, "%s [%2x]", |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1029 | "error in reading the capability at", pos); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 1030 | return clib_error_return (error, |
| 1031 | "error in reading the capability at [%2x]", |
| 1032 | pos); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1033 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1034 | |
| 1035 | if (cap.cap_vndr == PCI_CAP_ID_MSIX) |
| 1036 | { |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1037 | u16 flags, table_size, table_size_mask = 0x07FF; |
| 1038 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1039 | if ((error = |
| 1040 | vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags, |
| 1041 | sizeof (flags)))) |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 1042 | return clib_error_return (error, |
| 1043 | "error in reading the capability at [%2x]", |
| 1044 | pos + 2); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1045 | |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1046 | table_size = flags & table_size_mask; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1047 | virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1048 | "msix interrupt vector table-size", table_size); |
| 1049 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1050 | if (flags & PCI_MSIX_ENABLE) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1051 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1052 | virtio_log_debug (vif, "msix interrupt enabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1053 | vif->msix_enabled = VIRTIO_MSIX_ENABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1054 | vif->msix_table_size = table_size; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1055 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1056 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1057 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1058 | virtio_log_debug (vif, "msix interrupt disabled"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1059 | vif->msix_enabled = VIRTIO_MSIX_DISABLED; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1060 | vif->msix_table_size = 0; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1061 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | if (cap.cap_vndr != PCI_CAP_ID_VNDR) |
| 1065 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1066 | virtio_log_debug (vif, "[%2x] %s %2x ", pos, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1067 | "skipping non VNDR cap id:", cap.cap_vndr); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1068 | goto next; |
| 1069 | } |
| 1070 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1071 | virtio_log_debug (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1072 | "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u", |
| 1073 | pos, cap.cfg_type, cap.bar, cap.offset, cap.length); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1074 | |
Mohsin Kazmi | 0a507d7 | 2020-09-01 18:18:48 +0200 | [diff] [blame] | 1075 | if (cap.bar >= 0 && cap.bar <= 5) |
Mohsin Kazmi | 133c91c1 | 2020-08-20 17:18:56 +0200 | [diff] [blame] | 1076 | { |
| 1077 | vif->bar = bar[cap.bar]; |
| 1078 | vif->bar_id = cap.bar; |
| 1079 | } |
| 1080 | else |
| 1081 | return clib_error_return (error, "invalid bar %u", cap.bar); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1082 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1083 | switch (cap.cfg_type) |
| 1084 | { |
| 1085 | case VIRTIO_PCI_CAP_COMMON_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1086 | vif->common_offset = cap.offset; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1087 | common_cfg = 1; |
| 1088 | break; |
| 1089 | case VIRTIO_PCI_CAP_NOTIFY_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1090 | if ((error = |
| 1091 | vlib_pci_read_write_config (vm, h, VLIB_READ, |
| 1092 | pos + sizeof (cap), |
| 1093 | &vif->notify_off_multiplier, |
| 1094 | sizeof |
| 1095 | (vif->notify_off_multiplier)))) |
| 1096 | { |
| 1097 | virtio_log_error (vif, "notify off multiplier is not given"); |
| 1098 | } |
| 1099 | else |
| 1100 | { |
| 1101 | virtio_log_debug (vif, "notify off multiplier is %u", |
| 1102 | vif->notify_off_multiplier); |
| 1103 | vif->notify_offset = cap.offset; |
| 1104 | notify = 1; |
| 1105 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1106 | break; |
| 1107 | case VIRTIO_PCI_CAP_DEVICE_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1108 | vif->device_offset = cap.offset; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1109 | dev_cfg = 1; |
| 1110 | break; |
| 1111 | case VIRTIO_PCI_CAP_ISR_CFG: |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1112 | vif->isr_offset = cap.offset; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1113 | isr = 1; |
| 1114 | break; |
Mohsin Kazmi | 22c0ece | 2019-01-28 19:30:21 +0000 | [diff] [blame] | 1115 | case VIRTIO_PCI_CAP_PCI_CFG: |
| 1116 | if (cap.bar == 0) |
| 1117 | pci_cfg = 1; |
| 1118 | break; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1119 | } |
| 1120 | next: |
| 1121 | pos = cap.cap_next; |
| 1122 | } |
| 1123 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1124 | if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1125 | { |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1126 | vif->virtio_pci_func = &virtio_pci_legacy_func; |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 1127 | vif->notify_off_multiplier = 0; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1128 | virtio_log_debug (vif, "legacy virtio pci device found"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1129 | return error; |
| 1130 | } |
| 1131 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1132 | vif->is_modern = 1; |
| 1133 | vif->virtio_pci_func = &virtio_pci_modern_func; |
| 1134 | |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 1135 | if (!pci_cfg) |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 1136 | { |
| 1137 | virtio_log_debug (vif, "modern virtio pci device found"); |
| 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | virtio_log_debug (vif, "transitional virtio pci device found"); |
| 1142 | } |
Mohsin Kazmi | 0e2bc63 | 2019-01-30 13:36:57 +0000 | [diff] [blame] | 1143 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1144 | return error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | static clib_error_t * |
| 1148 | virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1149 | virtio_pci_create_if_args_t * args, void **bar) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1150 | { |
| 1151 | clib_error_t *error = 0; |
| 1152 | u8 status = 0; |
| 1153 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1154 | if ((error = virtio_pci_read_caps (vm, vif, bar))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1155 | { |
| 1156 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 1157 | virtio_log_error (vif, "Device is not supported"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 1158 | return clib_error_return (error, "Device is not supported"); |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1159 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1160 | |
| 1161 | if (virtio_pci_reset_device (vm, vif) < 0) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1162 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1163 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1164 | virtio_log_error (vif, "Failed to reset the device"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 1165 | return clib_error_return (error, "Failed to reset the device"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1166 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1167 | /* |
| 1168 | * read device features and negotiate (user) requested features |
| 1169 | */ |
| 1170 | virtio_pci_read_device_feature (vm, vif); |
Mohsin Kazmi | ce21599 | 2020-04-10 14:57:06 +0000 | [diff] [blame] | 1171 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 1172 | 0) |
| 1173 | { |
| 1174 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 1175 | "support VIRTIO_RING_F_INDIRECT_DESC features"); |
| 1176 | } |
| 1177 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 1178 | { |
| 1179 | virtio_log_warning (vif, "error encountered: vhost-net backend doesn't " |
| 1180 | "support VIRTIO_NET_F_MRG_RXBUF features"); |
| 1181 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1182 | virtio_negotiate_features (vm, vif, args->features); |
| 1183 | |
| 1184 | /* |
| 1185 | * After FEATURE_OK, driver should not accept new feature bits |
| 1186 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1187 | vif->virtio_pci_func->set_status (vm, vif, |
| 1188 | VIRTIO_CONFIG_STATUS_FEATURES_OK); |
| 1189 | status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1190 | if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK)) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1191 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1192 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1193 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1194 | "error encountered: Device doesn't support requested features"); |
Mohsin Kazmi | c96e64b | 2020-09-01 17:12:50 +0200 | [diff] [blame] | 1195 | return clib_error_return (error, |
| 1196 | "Device doesn't support requested features"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1197 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1198 | vif->status = status; |
| 1199 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1200 | /* |
| 1201 | * get or set the mac address |
| 1202 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1203 | if (virtio_pci_get_mac (vm, vif)) |
| 1204 | { |
| 1205 | f64 now = vlib_time_now (vm); |
| 1206 | u32 rnd; |
| 1207 | rnd = (u32) (now * 1e6); |
| 1208 | rnd = random_u32 (&rnd); |
| 1209 | |
| 1210 | memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd)); |
| 1211 | vif->mac_addr[0] = 2; |
| 1212 | vif->mac_addr[1] = 0xfe; |
| 1213 | virtio_pci_set_mac (vm, vif); |
| 1214 | } |
| 1215 | |
| 1216 | virtio_set_net_hdr_size (vif); |
| 1217 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1218 | /* |
| 1219 | * Initialize the virtqueues |
| 1220 | */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1221 | if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif))) |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1222 | { |
| 1223 | args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY; |
| 1224 | goto err; |
| 1225 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1226 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1227 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
| 1228 | { |
| 1229 | if (vif->msix_table_size <= vif->max_queue_pairs) |
| 1230 | { |
| 1231 | virtio_log_error (vif, |
| 1232 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 1233 | vif->msix_table_size, vif->max_queue_pairs); |
| 1234 | return clib_error_return (error, |
| 1235 | "error MSIX lines (%u) <= Number of RXQs (%u)", |
| 1236 | vif->msix_table_size, |
| 1237 | vif->max_queue_pairs); |
| 1238 | } |
| 1239 | } |
| 1240 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1241 | for (int i = 0; i < vif->max_queue_pairs; i++) |
| 1242 | { |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 1243 | if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i), 0))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1244 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1245 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 1246 | virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue", |
| 1247 | RX_QUEUE (i), "initialization"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1248 | error = |
| 1249 | clib_error_return (error, "%s (%u) %s", "error in rxq-queue", |
| 1250 | RX_QUEUE (i), "initialization"); |
| 1251 | goto err; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1252 | } |
| 1253 | else |
| 1254 | { |
| 1255 | vif->num_rxqs++; |
| 1256 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1257 | |
Mohsin Kazmi | a181eaa | 2023-08-29 09:18:20 +0000 | [diff] [blame] | 1258 | if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i), |
| 1259 | args->tx_queue_size))) |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1260 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1261 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 1262 | virtio_log_error (vif, "%s (%u) %s", "error in txq-queue", |
| 1263 | TX_QUEUE (i), "initialization"); |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1264 | error = |
| 1265 | clib_error_return (error, "%s (%u) %s", "error in txq-queue", |
| 1266 | TX_QUEUE (i), "initialization"); |
| 1267 | goto err; |
Mohsin Kazmi | 43b512c | 2019-04-30 17:25:26 +0200 | [diff] [blame] | 1268 | } |
| 1269 | else |
| 1270 | { |
| 1271 | vif->num_txqs++; |
| 1272 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1273 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1274 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1275 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 1276 | { |
| 1277 | if ((error = |
| 1278 | virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2))) |
| 1279 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1280 | virtio_log_warning (vif, "%s (%u) %s", "error in control-queue", |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1281 | vif->max_queue_pairs * 2, "initialization"); |
| 1282 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)) |
| 1283 | vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ); |
| 1284 | } |
| 1285 | } |
| 1286 | else |
| 1287 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1288 | virtio_log_debug (vif, "control queue is not available"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1289 | vif->cxq_vring = NULL; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * set the msix interrupts |
| 1294 | */ |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1295 | if (vif->msix_enabled == VIRTIO_MSIX_ENABLED) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1296 | { |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1297 | int i, j; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1298 | if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) == |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1299 | VIRTIO_MSI_NO_VECTOR) |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1300 | { |
| 1301 | virtio_log_warning (vif, "config vector 0 is not set"); |
| 1302 | } |
| 1303 | else |
| 1304 | { |
| 1305 | virtio_log_debug (vif, "config msix vector is set at 0"); |
| 1306 | } |
| 1307 | for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++) |
| 1308 | { |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1309 | if (vif->virtio_pci_func->set_queue_irq (vm, vif, j, |
| 1310 | RX_QUEUE (i)) == |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1311 | VIRTIO_MSI_NO_VECTOR) |
| 1312 | { |
| 1313 | virtio_log_warning (vif, "queue (%u) vector is not set at %u", |
| 1314 | RX_QUEUE (i), j); |
| 1315 | } |
| 1316 | else |
| 1317 | { |
| 1318 | virtio_log_debug (vif, "%s (%u) %s %u", "queue", |
| 1319 | RX_QUEUE (i), "msix vector is set at", j); |
| 1320 | } |
| 1321 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1322 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1323 | |
| 1324 | /* |
| 1325 | * set the driver status OK |
| 1326 | */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 1327 | vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK); |
| 1328 | vif->status = vif->virtio_pci_func->get_status (vm, vif); |
Mohsin Kazmi | 46f877e | 2019-04-08 10:36:18 +0200 | [diff] [blame] | 1329 | err: |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1330 | return error; |
| 1331 | } |
| 1332 | |
| 1333 | void |
| 1334 | virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) |
| 1335 | { |
| 1336 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1337 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1338 | virtio_if_t *vif; |
| 1339 | vlib_pci_dev_handle_t h; |
| 1340 | clib_error_t *error = 0; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1341 | u32 interrupt_count = 0; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1342 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1343 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1344 | pool_foreach (vif, vim->interfaces) { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1345 | if (vif->pci_addr.as_u32 == args->addr) |
| 1346 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1347 | args->rv = VNET_API_ERROR_ADDRESS_IN_USE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1348 | args->error = |
| 1349 | clib_error_return (error, "PCI address in use"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1350 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1351 | format_vlib_pci_addr, &args->addr, |
| 1352 | " PCI address in use"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1353 | return; |
| 1354 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 1355 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1356 | /* *INDENT-ON* */ |
| 1357 | |
Benoît Ganne | c04d8c4 | 2022-10-13 14:01:03 +0200 | [diff] [blame] | 1358 | if (args->bind) |
| 1359 | { |
| 1360 | vlib_pci_addr_t pci = { .as_u32 = args->addr }; |
| 1361 | error = vlib_pci_bind_to_uio (vm, &pci, (char *) "auto", |
| 1362 | VIRTIO_BIND_FORCE == args->bind); |
| 1363 | if (error) |
| 1364 | { |
| 1365 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1366 | args->error = |
| 1367 | clib_error_return (error, "%U: %s", format_vlib_pci_addr, &pci, |
| 1368 | "error encountered on binding pci device"); |
| 1369 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1370 | format_vlib_pci_addr, &pci, |
| 1371 | "error encountered on binding pci devicee"); |
| 1372 | return; |
| 1373 | } |
| 1374 | } |
| 1375 | |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1376 | pool_get (vim->interfaces, vif); |
| 1377 | vif->dev_instance = vif - vim->interfaces; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1378 | vif->per_interface_next_index = ~0; |
| 1379 | vif->pci_addr.as_u32 = args->addr; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1380 | if (args->virtio_flags & VIRTIO_FLAG_PACKED) |
| 1381 | vif->is_packed = 1; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1382 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1383 | if ((error = |
| 1384 | vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr, |
| 1385 | virtio_pci_device_ids, &h))) |
| 1386 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1387 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 1388 | args->error = |
| 1389 | clib_error_return (error, "pci-addr %U", format_vlib_pci_addr, |
| 1390 | &vif->pci_addr); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1391 | vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", |
| 1392 | format_vlib_pci_addr, &vif->pci_addr, |
| 1393 | "error encountered on pci device open"); |
Benoît Ganne | 8f01183 | 2019-11-20 14:32:38 +0100 | [diff] [blame] | 1394 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1395 | return; |
| 1396 | } |
| 1397 | vif->pci_dev_handle = h; |
| 1398 | vlib_pci_set_private_data (vm, h, vif->dev_instance); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1399 | vif->numa_node = vlib_pci_get_numa_node (vm, h); |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1400 | vif->type = VIRTIO_IF_TYPE_PCI; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1401 | |
| 1402 | if ((error = vlib_pci_bus_master_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1403 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1404 | virtio_log_error (vif, "error encountered on pci bus master enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1405 | goto error; |
| 1406 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1407 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1408 | void *bar[6]; |
| 1409 | for (u32 i = 0; i <= 5; i++) |
| 1410 | { |
| 1411 | |
| 1412 | if ((error = vlib_pci_map_region (vm, h, i, &bar[i]))) |
| 1413 | { |
| 1414 | virtio_log_debug (vif, "no pci map region for bar %u", i); |
| 1415 | } |
| 1416 | else |
| 1417 | { |
| 1418 | virtio_log_debug (vif, "pci map region for bar %u at %p", i, |
| 1419 | bar[i]); |
| 1420 | } |
| 1421 | } |
| 1422 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1423 | if ((error = vlib_pci_io_region (vm, h, 0))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1424 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1425 | virtio_log_error (vif, "error encountered on pci io region"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1426 | goto error; |
| 1427 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1428 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1429 | interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h); |
| 1430 | if (interrupt_count > 1) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1431 | { |
| 1432 | if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1, |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1433 | &virtio_pci_irq_config_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1434 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1435 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1436 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1437 | "error encountered on pci register msix handler 0"); |
| 1438 | goto error; |
| 1439 | } |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1440 | |
| 1441 | if ((error = |
| 1442 | vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1), |
| 1443 | &virtio_pci_irq_queue_handler))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1444 | { |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1445 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1446 | virtio_log_error (vif, |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1447 | "error encountered on pci register msix handler 1"); |
| 1448 | goto error; |
| 1449 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1450 | |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame] | 1451 | if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1452 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1453 | virtio_log_error (vif, "error encountered on pci enable msix irq"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1454 | goto error; |
| 1455 | } |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1456 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1457 | virtio_log_debug (vif, "device supports msix interrupts"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1458 | } |
Mohsin Kazmi | 5db2f4a | 2021-02-22 18:27:57 +0000 | [diff] [blame] | 1459 | else |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1460 | { |
| 1461 | /* |
Mohsin Kazmi | 5db2f4a | 2021-02-22 18:27:57 +0000 | [diff] [blame] | 1462 | * WARN: performance will be sub-optimal. |
| 1463 | * Fall back to intX, if msix table-size is 1 or |
| 1464 | * if UIO driver is being used. |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1465 | */ |
| 1466 | if ((error = |
| 1467 | vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler))) |
| 1468 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1469 | virtio_log_error (vif, |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1470 | "error encountered on pci register interrupt handler"); |
| 1471 | goto error; |
| 1472 | } |
| 1473 | vif->support_int_mode = 1; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1474 | virtio_log_debug (vif, "pci register interrupt handler"); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1475 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1476 | |
| 1477 | if ((error = vlib_pci_intr_enable (vm, h))) |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1478 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1479 | virtio_log_error (vif, "error encountered on pci interrupt enable"); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1480 | goto error; |
| 1481 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1482 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 1483 | if ((error = virtio_pci_device_init (vm, vif, args, bar))) |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1484 | { |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 1485 | virtio_log_error (vif, "error encountered on device init"); |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 1486 | goto error; |
| 1487 | } |
| 1488 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1489 | /* create interface */ |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 1490 | vnet_eth_interface_registration_t eir = {}; |
| 1491 | eir.dev_class_index = virtio_device_class.index; |
| 1492 | eir.dev_instance = vif->dev_instance; |
| 1493 | eir.address = vif->mac_addr; |
| 1494 | eir.cb.flag_change = virtio_pci_flag_change; |
| 1495 | vif->hw_if_index = vnet_eth_register_interface (vnm, &eir); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1496 | |
| 1497 | vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 1498 | vif->sw_if_index = sw->sw_if_index; |
| 1499 | args->sw_if_index = sw->sw_if_index; |
| 1500 | |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 1501 | vnet_hw_if_set_caps (vnm, vif->hw_if_index, VNET_HW_IF_CAP_INT_MODE); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (args->virtio_flags & VIRTIO_FLAG_BUFFERING) |
| 1504 | { |
| 1505 | error = virtio_set_packet_buffering (vif, args->buffering_size); |
| 1506 | if (error) |
| 1507 | { |
| 1508 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 1509 | virtio_log_error (vif, |
| 1510 | "error encountered during packet buffering init"); |
| 1511 | goto error; |
| 1512 | } |
Mohsin Kazmi | 562ac2f | 2023-05-24 11:25:39 +0000 | [diff] [blame] | 1513 | /* |
| 1514 | * packet buffering flag needs to be set 1 before calling the |
| 1515 | * virtio_pre_input_node_enable but after the successful initialization |
| 1516 | * of buffering queues above. |
| 1517 | * Packet buffering flag set to 0 if there will be any error during |
| 1518 | * buffering initialization. |
| 1519 | */ |
| 1520 | vif->packet_buffering = 1; |
| 1521 | virtio_pre_input_node_enable (vm, vif); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1524 | virtio_vring_set_rx_queues (vm, vif); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 1525 | virtio_vring_set_tx_queues (vm, vif); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 1526 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1527 | if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) |
| 1528 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1529 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 1530 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 1531 | } |
| 1532 | else |
| 1533 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1534 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1535 | virtio_pci_offloads (vm, vif, args->gso_enabled, |
| 1536 | args->checksum_offload_enabled); |
| 1537 | |
| 1538 | if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && |
| 1539 | (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))) |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1540 | { |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1541 | if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs)) |
| 1542 | virtio_log_warning (vif, "multiqueue is not set"); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1543 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1544 | return; |
| 1545 | |
| 1546 | error: |
| 1547 | virtio_pci_delete_if (vm, vif); |
Mohsin Kazmi | 57d7321 | 2020-03-05 12:22:41 +0000 | [diff] [blame] | 1548 | if (args->rv == 0) |
| 1549 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1550 | args->error = error; |
| 1551 | } |
| 1552 | |
| 1553 | int |
| 1554 | virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) |
| 1555 | { |
| 1556 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1557 | virtio_main_t *vim = &virtio_main; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1558 | u32 i = 0; |
| 1559 | |
| 1560 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1561 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1562 | |
| 1563 | vlib_pci_intr_disable (vm, vif->pci_dev_handle); |
| 1564 | |
Mohsin Kazmi | c79d735 | 2020-09-01 17:45:56 +0200 | [diff] [blame] | 1565 | if (vif->virtio_pci_func) |
Mohsin Kazmi | 562ac2f | 2023-05-24 11:25:39 +0000 | [diff] [blame] | 1566 | { |
| 1567 | for (i = 0; i < vif->max_queue_pairs; i++) |
| 1568 | { |
| 1569 | vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i)); |
| 1570 | vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i)); |
| 1571 | } |
| 1572 | |
| 1573 | if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) |
| 1574 | vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2); |
| 1575 | |
| 1576 | vif->virtio_pci_func->device_reset (vm, vif); |
| 1577 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1578 | |
| 1579 | if (vif->hw_if_index) |
| 1580 | { |
| 1581 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1582 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 1583 | } |
| 1584 | |
| 1585 | vlib_pci_device_close (vm, vif->pci_dev_handle); |
| 1586 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1587 | vec_foreach_index (i, vif->rxq_vrings) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1588 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1589 | vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1590 | if (vring->used) |
| 1591 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1592 | virtio_free_buffers (vm, vring); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1593 | } |
| 1594 | vec_free (vring->buffers); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1595 | vlib_physmem_free (vm, vring->desc); |
| 1596 | } |
| 1597 | |
Mohsin Kazmi | 562ac2f | 2023-05-24 11:25:39 +0000 | [diff] [blame] | 1598 | if (vif->packet_buffering) |
| 1599 | virtio_pre_input_node_disable (vm, vif); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 1600 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1601 | vec_foreach_index (i, vif->txq_vrings) |
| 1602 | { |
Mohsin Kazmi | 0f8912f | 2022-02-01 18:35:59 +0000 | [diff] [blame] | 1603 | vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1604 | if (vring->used) |
| 1605 | { |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1606 | virtio_free_buffers (vm, vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1607 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1608 | vec_free (vring->buffers); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 1609 | gro_flow_table_free (vring->flow_table); |
| 1610 | virtio_vring_buffering_free (vm, vring->buffering); |
Steven Luong | e558496 | 2019-06-25 13:47:06 -0700 | [diff] [blame] | 1611 | clib_spinlock_free (&vring->lockp); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1612 | vlib_physmem_free (vm, vring->desc); |
| 1613 | } |
| 1614 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 1615 | if (vif->cxq_vring != NULL) |
| 1616 | { |
| 1617 | u16 last = vif->cxq_vring->last_used_idx; |
| 1618 | u16 n_left = vif->cxq_vring->used->idx - last; |
| 1619 | while (n_left) |
| 1620 | { |
| 1621 | last++; |
| 1622 | n_left--; |
| 1623 | } |
| 1624 | |
| 1625 | vif->cxq_vring->last_used_idx = last; |
| 1626 | vlib_physmem_free (vm, vif->cxq_vring->desc); |
| 1627 | } |
| 1628 | |
| 1629 | vec_free (vif->rxq_vrings); |
| 1630 | vec_free (vif->txq_vrings); |
| 1631 | vec_free (vif->cxq_vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1632 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1633 | clib_error_free (vif->error); |
| 1634 | memset (vif, 0, sizeof (*vif)); |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 1635 | pool_put (vim->interfaces, vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1636 | |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Mohsin Kazmi | 6d4af89 | 2020-01-03 15:11:53 +0000 | [diff] [blame] | 1640 | int |
| 1641 | virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif, |
| 1642 | int gso_enabled, |
| 1643 | int checksum_offload_enabled, |
| 1644 | int offloads_disabled) |
| 1645 | { |
| 1646 | if (vif->type != VIRTIO_IF_TYPE_PCI) |
| 1647 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 1648 | |
| 1649 | if (gso_enabled) |
| 1650 | virtio_pci_offloads (vm, vif, 1, 0); |
| 1651 | else if (checksum_offload_enabled) |
| 1652 | virtio_pci_offloads (vm, vif, 0, 1); |
| 1653 | else if (offloads_disabled) |
| 1654 | virtio_pci_offloads (vm, vif, 0, 0); |
| 1655 | |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 1659 | /* |
| 1660 | * fd.io coding-style-patch-verification: ON |
| 1661 | * |
| 1662 | * Local Variables: |
| 1663 | * eval: (c-set-style "gnu") |
| 1664 | * End: |
| 1665 | */ |