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