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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/ioctl.h>
19 #include <vppinfra/types.h>
20 #include <vlib/vlib.h>
21 #include <vlib/pci/pci.h>
22 #include <vnet/ethernet/ethernet.h>
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>
28 #define PCI_VENDOR_ID_VIRTIO 0x1af4
29 #define PCI_DEVICE_ID_VIRTIO_NIC 0x1000
30 /* Doesn't support modern device */
31 #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041
33 #define PCI_CAPABILITY_LIST 0x34
34 #define PCI_CAP_ID_VNDR 0x09
35 #define PCI_CAP_ID_MSIX 0x11
37 #define PCI_MSIX_ENABLE 0x8000
39 static pci_device_id_t virtio_pci_device_ids[] = {
41 .vendor_id = PCI_VENDOR_ID_VIRTIO,
42 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
44 .vendor_id = PCI_VENDOR_ID_VIRTIO,
45 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
50 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
57 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
59 clib_error_t *error = 0;
60 u16 max_queue_pairs = 1;
62 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
64 max_queue_pairs = vif->virtio_pci_func->get_max_queue_pairs (vm, vif);
67 virtio_log_debug (vif, "max queue pair is %x", max_queue_pairs);
68 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
69 return clib_error_return (error, "max queue pair is %x,"
70 " should be in range [1, 0x8000]",
73 vif->max_queue_pairs = max_queue_pairs;
78 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
80 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
81 vif->virtio_pci_func->set_mac (vm, vif);
85 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
87 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
89 vif->virtio_pci_func->get_mac (vm, vif);
96 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
99 * Minimal driver: assumes link is up
102 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
103 status = vif->virtio_pci_func->get_device_status (vm, vif);
108 virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
111 vnet_main_t *vnm = vnet_get_main ();
112 virtio_main_t *vim = &virtio_main;
113 uword pd = vlib_pci_get_private_data (vm, h);
114 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
118 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
122 virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
125 vnet_main_t *vnm = vnet_get_main ();
126 virtio_main_t *vim = &virtio_main;
127 uword pd = vlib_pci_get_private_data (vm, h);
128 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
130 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
132 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
133 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
134 VNET_HW_INTERFACE_FLAG_LINK_UP);
138 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
139 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
144 virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
146 virtio_main_t *vim = &virtio_main;
147 uword pd = vlib_pci_get_private_data (vm, h);
148 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
152 isr = vif->virtio_pci_func->get_isr (vm, vif);
155 * If the lower bit is set: look through the used rings of
156 * all virtqueues for the device, to see if any progress has
157 * been made by the device which requires servicing.
159 if (isr & VIRTIO_PCI_ISR_INTR)
161 for (; line < vif->num_rxqs; line++)
162 virtio_pci_irq_queue_handler (vm, h, (line + 1));
165 if (isr & VIRTIO_PCI_ISR_CONFIG)
166 virtio_pci_irq_config_handler (vm, h, line);
170 device_status (vlib_main_t * vm, virtio_if_t * vif)
177 struct status_struct *status_entry;
178 static struct status_struct status_array[] = {
179 #define _(s,b) { .str = #s, .bit = b, },
180 foreach_virtio_config_status_flags
185 vlib_cli_output (vm, " status 0x%x", vif->status);
187 status_entry = (struct status_struct *) &status_array;
188 while (status_entry->str)
190 if (vif->status & status_entry->bit)
191 vlib_cli_output (vm, " %s (%x)", status_entry->str,
198 virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif,
199 virtio_ctrl_msg_t * data, u32 len)
201 virtio_vring_t *vring = vif->cxq_vring;
202 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
203 virtio_ctrl_msg_t result;
207 u16 sz = vring->size;
208 u16 flags = 0, first_desc_flags = 0;
210 used = vring->desc_in_use;
211 next = vring->desc_next;
212 vring_packed_desc_t *d = &vring->packed_desc[next];
214 if (vlib_buffer_alloc (vm, &buffer_index, 1))
215 b = vlib_get_buffer (vm, buffer_index);
217 return VIRTIO_NET_ERR;
219 * current_data may not be initialized with 0 and may contain
223 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
225 first_desc_flags = VRING_DESC_F_NEXT;
226 if (vring->avail_wrap_counter)
228 first_desc_flags |= VRING_DESC_F_AVAIL;
229 first_desc_flags &= ~VRING_DESC_F_USED;
233 first_desc_flags &= ~VRING_DESC_F_AVAIL;
234 first_desc_flags |= VRING_DESC_F_USED;
236 d->addr = vlib_buffer_get_current_pa (vm, b);
237 d->len = sizeof (virtio_net_ctrl_hdr_t);
244 vring->avail_wrap_counter ^= 1;
248 d = &vring->packed_desc[next];
249 flags = VRING_DESC_F_NEXT;
250 if (vring->avail_wrap_counter)
252 flags |= VRING_DESC_F_AVAIL;
253 flags &= ~VRING_DESC_F_USED;
257 flags &= ~VRING_DESC_F_AVAIL;
258 flags |= VRING_DESC_F_USED;
260 d->addr = vlib_buffer_get_current_pa (vm, b) +
261 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
270 vring->avail_wrap_counter ^= 1;
274 d = &vring->packed_desc[next];
275 flags = VRING_DESC_F_WRITE;
276 if (vring->avail_wrap_counter)
278 flags |= VRING_DESC_F_AVAIL;
279 flags &= ~VRING_DESC_F_USED;
283 flags &= ~VRING_DESC_F_AVAIL;
284 flags |= VRING_DESC_F_USED;
286 d->addr = vlib_buffer_get_current_pa (vm, b) +
287 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
288 d->len = sizeof (data->status);
296 vring->avail_wrap_counter ^= 1;
300 CLIB_MEMORY_STORE_BARRIER ();
301 vring->packed_desc[vring->desc_next].flags = first_desc_flags;
302 vring->desc_next = next;
303 vring->desc_in_use = used;
304 CLIB_MEMORY_BARRIER ();
305 if (vring->device_event->flags != VRING_EVENT_F_DISABLE)
307 virtio_kick (vm, vring, vif);
310 u16 last = vring->last_used_idx;
311 d = &vring->packed_desc[last];
316 while ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7)
317 || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15));
320 if (last >= vring->size)
322 last = last - vring->size;
323 vring->used_wrap_counter ^= 1;
325 vring->desc_in_use -= 3;
326 vring->last_used_idx = last;
328 CLIB_MEMORY_BARRIER ();
329 clib_memcpy (&result, vlib_buffer_get_current (b),
330 sizeof (virtio_ctrl_msg_t));
331 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
332 status = result.status;
333 vlib_buffer_free (vm, &buffer_index, 1);
338 virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif,
339 virtio_ctrl_msg_t * data, u32 len)
341 virtio_vring_t *vring = vif->cxq_vring;
342 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
343 virtio_ctrl_msg_t result;
346 u16 used, next, avail;
347 u16 sz = vring->size;
350 used = vring->desc_in_use;
351 next = vring->desc_next;
352 avail = vring->avail->idx;
353 vring_desc_t *d = &vring->desc[next];
355 if (vlib_buffer_alloc (vm, &buffer_index, 1))
356 b = vlib_get_buffer (vm, buffer_index);
358 return VIRTIO_NET_ERR;
360 * current_data may not be initialized with 0 and may contain
364 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
365 d->flags = VRING_DESC_F_NEXT;
366 d->addr = vlib_buffer_get_current_pa (vm, b);
367 d->len = sizeof (virtio_net_ctrl_hdr_t);
368 vring->avail->ring[avail & mask] = next;
370 next = (next + 1) & mask;
374 d = &vring->desc[next];
375 d->flags = VRING_DESC_F_NEXT;
376 d->addr = vlib_buffer_get_current_pa (vm, b) +
377 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
379 next = (next + 1) & mask;
383 d = &vring->desc[next];
384 d->flags = VRING_DESC_F_WRITE;
385 d->addr = vlib_buffer_get_current_pa (vm, b) +
386 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
387 d->len = sizeof (data->status);
388 next = (next + 1) & mask;
391 CLIB_MEMORY_STORE_BARRIER ();
392 vring->avail->idx = avail;
393 vring->desc_next = next;
394 vring->desc_in_use = used;
396 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
398 virtio_kick (vm, vring, vif);
401 u16 last = vring->last_used_idx, n_left = 0;
402 n_left = vring->used->idx - last;
406 vring_used_elem_t *e = &vring->used->ring[last & mask];
409 d = &vring->desc[slot];
410 while (d->flags & VRING_DESC_F_NEXT)
414 d = &vring->desc[slot];
420 vring->desc_in_use = used;
421 vring->last_used_idx = last;
423 CLIB_MEMORY_BARRIER ();
424 clib_memcpy (&result, vlib_buffer_get_current (b),
425 sizeof (virtio_ctrl_msg_t));
426 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
427 status = result.status;
428 vlib_buffer_free (vm, &buffer_index, 1);
433 virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
434 virtio_ctrl_msg_t * data, u32 len)
437 return virtio_pci_send_ctrl_msg_packed (vm, vif, data, len);
439 return virtio_pci_send_ctrl_msg_split (vm, vif, data, len);
443 virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
445 virtio_ctrl_msg_t offload_hdr;
446 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
448 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
449 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
450 offload_hdr.status = VIRTIO_NET_ERR;
452 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
455 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
456 virtio_log_debug (vif, "disable offloads");
457 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
458 vif->virtio_pci_func->get_driver_features (vm, vif);
463 virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
465 virtio_ctrl_msg_t csum_offload_hdr;
466 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
468 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
469 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
470 csum_offload_hdr.status = VIRTIO_NET_ERR;
472 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
473 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
476 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
477 virtio_log_debug (vif, "enable checksum offload");
478 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
479 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
484 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
486 virtio_ctrl_msg_t gso_hdr;
487 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
489 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
490 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
491 gso_hdr.status = VIRTIO_NET_ERR;
492 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
493 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
494 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
495 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
497 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
498 virtio_log_debug (vif, "enable gso");
499 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
500 vif->virtio_pci_func->get_driver_features (vm, vif);
505 virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
506 int csum_offload_enabled)
508 vnet_main_t *vnm = vnet_get_main ();
509 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
511 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
512 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
515 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
516 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
518 if (virtio_pci_enable_gso (vm, vif))
520 virtio_log_warning (vif, "gso is not enabled");
524 vif->gso_enabled = 1;
525 vif->csum_offload_enabled = 0;
526 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
527 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
530 else if (csum_offload_enabled
531 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
533 if (virtio_pci_enable_checksum_offload (vm, vif))
535 virtio_log_warning (vif, "checksum offload is not enabled");
539 vif->csum_offload_enabled = 1;
540 vif->gso_enabled = 0;
541 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
543 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
548 if (virtio_pci_disable_offload (vm, vif))
550 virtio_log_warning (vif, "offloads are not disabled");
554 vif->csum_offload_enabled = 0;
555 vif->gso_enabled = 0;
556 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
557 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
566 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
569 virtio_ctrl_msg_t mq_hdr;
570 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
572 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
573 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
574 mq_hdr.status = VIRTIO_NET_ERR;
575 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
577 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
578 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
583 virtio_pci_queue_size_valid (u16 qsz)
585 if (qsz < 64 || qsz > 4096)
593 virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
596 clib_error_t *error = 0;
598 virtio_vring_t *vring;
602 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
604 if (queue_size > 32768)
605 return clib_error_return (0, "ring size must be 32768 or lower");
610 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
611 vring = vec_elt_at_index (vif->cxq_vring, 0);
614 (((queue_size * sizeof (vring_packed_desc_t)) +
615 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
616 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
619 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
622 return vlib_physmem_last_error (vm);
623 clib_memset (ptr, 0, i);
625 vring->packed_desc = ptr;
627 vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
628 vring->driver_event->off_wrap = 0;
629 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
631 vring->device_event =
633 (((queue_size * sizeof (vring_packed_desc_t)) +
634 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
635 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
636 vring->device_event->off_wrap = 0;
637 vring->device_event->flags = 0;
639 vring->queue_id = queue_num;
640 vring->size = queue_size;
641 vring->avail_wrap_counter = 1;
642 vring->used_wrap_counter = 1;
644 ASSERT (vring->buffers == 0);
646 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
648 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring);
649 vring->queue_notify_offset =
650 vif->notify_off_multiplier *
651 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
652 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
653 queue_num, vring->queue_notify_offset);
658 virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
661 clib_error_t *error = 0;
663 virtio_vring_t *vring;
668 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
669 if (!virtio_pci_queue_size_valid (queue_size))
670 clib_warning ("queue size is not valid");
672 if (!is_pow2 (queue_size))
673 return clib_error_return (0, "ring size must be power of 2");
675 if (queue_size > 32768)
676 return clib_error_return (0, "ring size must be 32768 or lower");
681 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
682 vring = vec_elt_at_index (vif->cxq_vring, 0);
683 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
684 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
686 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
689 return vlib_physmem_last_error (vm);
690 clib_memset (ptr, 0, i);
691 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
692 vring->desc = vr.desc;
693 vring->avail = vr.avail;
694 vring->used = vr.used;
695 vring->queue_id = queue_num;
696 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
698 ASSERT (vring->buffers == 0);
700 vring->size = queue_size;
701 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
703 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
704 vring->queue_notify_offset =
705 vif->notify_off_multiplier *
706 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
707 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
708 queue_num, vring->queue_notify_offset);
714 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
718 return virtio_pci_control_vring_packed_init (vm, vif, queue_num);
720 return virtio_pci_control_vring_split_init (vm, vif, queue_num);
724 virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif,
727 vlib_thread_main_t *vtm = vlib_get_thread_main ();
728 clib_error_t *error = 0;
730 virtio_vring_t *vring;
735 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
736 if (!virtio_pci_queue_size_valid (queue_size))
737 clib_warning ("queue size is not valid");
739 if (!is_pow2 (queue_size))
740 return clib_error_return (0, "ring size must be power of 2");
742 if (queue_size > 32768)
743 return clib_error_return (0, "ring size must be 32768 or lower");
750 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
751 CLIB_CACHE_LINE_BYTES);
752 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
753 if (vif->max_queue_pairs < vtm->n_vlib_mains)
754 clib_spinlock_init (&vring->lockp);
758 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
759 CLIB_CACHE_LINE_BYTES);
760 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
762 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
763 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
765 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
768 return vlib_physmem_last_error (vm);
769 clib_memset (ptr, 0, i);
770 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
771 vring->desc = vr.desc;
772 vring->avail = vr.avail;
773 vring->used = vr.used;
774 vring->queue_id = queue_num;
775 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
776 vring->flow_table = 0;
778 ASSERT (vring->buffers == 0);
779 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
782 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
784 clib_memset_u32 (vring->buffers, ~0, queue_size);
788 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
791 vring->size = queue_size;
792 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
793 return clib_error_return (0, "error in queue address setup");
795 vring->queue_notify_offset =
796 vif->notify_off_multiplier *
797 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
798 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
799 queue_num, vring->queue_notify_offset);
804 virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif,
807 vlib_thread_main_t *vtm = vlib_get_thread_main ();
808 clib_error_t *error = 0;
810 virtio_vring_t *vring;
814 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
816 if (queue_size > 32768)
817 return clib_error_return (0, "ring size must be 32768 or lower");
824 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
825 CLIB_CACHE_LINE_BYTES);
826 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
827 if (vif->max_queue_pairs < vtm->n_vlib_mains)
828 clib_spinlock_init (&vring->lockp);
832 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
833 CLIB_CACHE_LINE_BYTES);
834 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
838 (((queue_size * sizeof (vring_packed_desc_t)) +
839 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
840 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t);
843 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
846 return vlib_physmem_last_error (vm);
848 clib_memset (ptr, 0, i);
849 vring->packed_desc = ptr;
851 vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t));
852 vring->driver_event->off_wrap = 0;
853 vring->driver_event->flags = VRING_EVENT_F_DISABLE;
855 vring->device_event =
857 (((queue_size * sizeof (vring_packed_desc_t)) +
858 sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN -
859 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1));
860 vring->device_event->off_wrap = 0;
861 vring->device_event->flags = 0;
863 vring->queue_id = queue_num;
865 vring->avail_wrap_counter = 1;
866 vring->used_wrap_counter = 1;
868 ASSERT (vring->buffers == 0);
869 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
872 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
874 clib_memset_u32 (vring->buffers, ~0, queue_size);
878 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
881 vring->size = queue_size;
882 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring))
883 return clib_error_return (0, "error in queue address setup");
885 vring->queue_notify_offset =
886 vif->notify_off_multiplier *
887 vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num);
888 virtio_log_debug (vif, "queue-notify-offset: number %u, offset %u",
889 queue_num, vring->queue_notify_offset);
895 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
898 return virtio_pci_vring_packed_init (vm, vif, queue_num);
900 return virtio_pci_vring_split_init (vm, vif, queue_num);
904 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
908 * if features are not requested
909 * default: all supported features
911 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
912 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
913 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
914 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
915 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
916 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
917 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
918 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
919 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
920 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
921 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
922 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
923 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
924 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
925 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
926 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
927 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
928 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
929 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
932 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
936 supported_features |=
937 (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED) |
938 VIRTIO_FEATURE (VIRTIO_F_IN_ORDER));
941 if (req_features == 0)
943 req_features = supported_features;
946 vif->features = req_features & vif->remote_features & supported_features;
948 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
951 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
954 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
957 if ((vif->features & (VIRTIO_FEATURE (VIRTIO_F_RING_PACKED))) == 0)
960 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
961 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
965 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
967 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
971 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
978 status = vif->virtio_pci_func->device_reset (vm, vif);
981 * Set the Acknowledge status bit
983 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
986 * Set the Driver status bit
988 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
991 * Read the status and verify it
993 status = vif->virtio_pci_func->get_status (vm, vif);
994 if ((status & VIRTIO_CONFIG_STATUS_ACK)
995 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
996 vif->status = status;
1004 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
1006 clib_error_t *error = 0;
1007 virtio_pci_cap_t cap;
1008 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
1009 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
1011 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
1013 virtio_log_error (vif, "error in reading capabilty list position");
1014 return clib_error_return (error,
1015 "error in reading capabilty list position");
1020 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
1023 virtio_log_error (vif, "%s [%2x]",
1024 "error in reading the capability at", pos);
1025 return clib_error_return (error,
1026 "error in reading the capability at [%2x]",
1030 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
1032 u16 flags, table_size, table_size_mask = 0x07FF;
1035 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
1037 return clib_error_return (error,
1038 "error in reading the capability at [%2x]",
1041 table_size = flags & table_size_mask;
1042 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
1043 "msix interrupt vector table-size", table_size);
1045 if (flags & PCI_MSIX_ENABLE)
1047 virtio_log_debug (vif, "msix interrupt enabled");
1048 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
1049 vif->msix_table_size = table_size;
1053 virtio_log_debug (vif, "msix interrupt disabled");
1054 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
1055 vif->msix_table_size = 0;
1059 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
1061 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
1062 "skipping non VNDR cap id:", cap.cap_vndr);
1066 virtio_log_debug (vif,
1067 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
1068 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
1070 if (cap.bar >= 0 && cap.bar <= 5)
1072 vif->bar = bar[cap.bar];
1073 vif->bar_id = cap.bar;
1076 return clib_error_return (error, "invalid bar %u", cap.bar);
1078 switch (cap.cfg_type)
1080 case VIRTIO_PCI_CAP_COMMON_CFG:
1081 vif->common_offset = cap.offset;
1084 case VIRTIO_PCI_CAP_NOTIFY_CFG:
1086 vlib_pci_read_write_config (vm, h, VLIB_READ,
1088 &vif->notify_off_multiplier,
1090 (vif->notify_off_multiplier))))
1092 virtio_log_error (vif, "notify off multiplier is not given");
1096 virtio_log_debug (vif, "notify off multiplier is %u",
1097 vif->notify_off_multiplier);
1098 vif->notify_offset = cap.offset;
1102 case VIRTIO_PCI_CAP_DEVICE_CFG:
1103 vif->device_offset = cap.offset;
1106 case VIRTIO_PCI_CAP_ISR_CFG:
1107 vif->isr_offset = cap.offset;
1110 case VIRTIO_PCI_CAP_PCI_CFG:
1119 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
1121 vif->virtio_pci_func = &virtio_pci_legacy_func;
1122 vif->notify_off_multiplier = 0;
1123 virtio_log_debug (vif, "legacy virtio pci device found");
1128 vif->virtio_pci_func = &virtio_pci_modern_func;
1132 virtio_log_debug (vif, "modern virtio pci device found");
1136 virtio_log_debug (vif, "transitional virtio pci device found");
1142 static clib_error_t *
1143 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
1144 virtio_pci_create_if_args_t * args, void **bar)
1146 clib_error_t *error = 0;
1147 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1150 if ((error = virtio_pci_read_caps (vm, vif, bar)))
1152 args->rv = VNET_API_ERROR_UNSUPPORTED;
1153 virtio_log_error (vif, "Device is not supported");
1154 return clib_error_return (error, "Device is not supported");
1157 if (virtio_pci_reset_device (vm, vif) < 0)
1159 args->rv = VNET_API_ERROR_INIT_FAILED;
1160 virtio_log_error (vif, "Failed to reset the device");
1161 return clib_error_return (error, "Failed to reset the device");
1164 * read device features and negotiate (user) requested features
1166 virtio_pci_read_device_feature (vm, vif);
1167 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
1170 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1171 "support VIRTIO_RING_F_INDIRECT_DESC features");
1173 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
1175 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
1176 "support VIRTIO_NET_F_MRG_RXBUF features");
1178 virtio_negotiate_features (vm, vif, args->features);
1181 * After FEATURE_OK, driver should not accept new feature bits
1183 vif->virtio_pci_func->set_status (vm, vif,
1184 VIRTIO_CONFIG_STATUS_FEATURES_OK);
1185 status = vif->virtio_pci_func->get_status (vm, vif);
1186 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
1188 args->rv = VNET_API_ERROR_UNSUPPORTED;
1189 virtio_log_error (vif,
1190 "error encountered: Device doesn't support requested features");
1191 return clib_error_return (error,
1192 "Device doesn't support requested features");
1194 vif->status = status;
1197 * get or set the mac address
1199 if (virtio_pci_get_mac (vm, vif))
1201 f64 now = vlib_time_now (vm);
1203 rnd = (u32) (now * 1e6);
1204 rnd = random_u32 (&rnd);
1206 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
1207 vif->mac_addr[0] = 2;
1208 vif->mac_addr[1] = 0xfe;
1209 virtio_pci_set_mac (vm, vif);
1212 virtio_set_net_hdr_size (vif);
1215 * Initialize the virtqueues
1217 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
1219 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1223 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1225 if (vif->msix_table_size <= vif->max_queue_pairs)
1227 virtio_log_error (vif,
1228 "error MSIX lines (%u) <= Number of RXQs (%u)",
1229 vif->msix_table_size, vif->max_queue_pairs);
1230 return clib_error_return (error,
1231 "error MSIX lines (%u) <= Number of RXQs (%u)",
1232 vif->msix_table_size,
1233 vif->max_queue_pairs);
1237 for (int i = 0; i < vif->max_queue_pairs; i++)
1239 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
1241 args->rv = VNET_API_ERROR_INIT_FAILED;
1242 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
1243 RX_QUEUE (i), "initialization");
1245 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
1246 RX_QUEUE (i), "initialization");
1254 if (i >= vtm->n_vlib_mains)
1257 * There is 1:1 mapping between tx queue and vpp worker thread.
1258 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
1259 * index 1 and so on.
1260 * Multiple worker threads can poll same tx queue when number of
1261 * workers are more than tx queues. In this case, 1:N mapping
1262 * between tx queue and vpp worker thread.
1264 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
1266 "no VPP worker thread is available");
1270 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
1272 args->rv = VNET_API_ERROR_INIT_FAILED;
1273 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
1274 TX_QUEUE (i), "initialization");
1276 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
1277 TX_QUEUE (i), "initialization");
1286 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1289 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1291 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
1292 vif->max_queue_pairs * 2, "initialization");
1293 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1294 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1299 virtio_log_debug (vif, "control queue is not available");
1300 vif->cxq_vring = NULL;
1304 * set the msix interrupts
1306 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1309 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
1310 VIRTIO_MSI_NO_VECTOR)
1312 virtio_log_warning (vif, "config vector 0 is not set");
1316 virtio_log_debug (vif, "config msix vector is set at 0");
1318 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
1320 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
1322 VIRTIO_MSI_NO_VECTOR)
1324 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
1329 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
1330 RX_QUEUE (i), "msix vector is set at", j);
1336 * set the driver status OK
1338 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1339 vif->status = vif->virtio_pci_func->get_status (vm, vif);
1345 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1347 vnet_main_t *vnm = vnet_get_main ();
1348 virtio_main_t *vim = &virtio_main;
1350 vlib_pci_dev_handle_t h;
1351 clib_error_t *error = 0;
1352 u32 interrupt_count = 0;
1355 pool_foreach (vif, vim->interfaces, ({
1356 if (vif->pci_addr.as_u32 == args->addr)
1358 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
1360 clib_error_return (error, "PCI address in use");
1361 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1362 format_vlib_pci_addr, &args->addr,
1363 " PCI address in use");
1369 pool_get (vim->interfaces, vif);
1370 vif->dev_instance = vif - vim->interfaces;
1371 vif->per_interface_next_index = ~0;
1372 vif->pci_addr.as_u32 = args->addr;
1373 if (args->virtio_flags & VIRTIO_FLAG_PACKED)
1377 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1378 virtio_pci_device_ids, &h)))
1380 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1382 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1384 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1385 format_vlib_pci_addr, &vif->pci_addr,
1386 "error encountered on pci device open");
1387 pool_put (vim->interfaces, vif);
1390 vif->pci_dev_handle = h;
1391 vlib_pci_set_private_data (vm, h, vif->dev_instance);
1392 vif->numa_node = vlib_pci_get_numa_node (vm, h);
1393 vif->type = VIRTIO_IF_TYPE_PCI;
1395 if ((error = vlib_pci_bus_master_enable (vm, h)))
1397 virtio_log_error (vif, "error encountered on pci bus master enable");
1402 for (u32 i = 0; i <= 5; i++)
1405 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1407 virtio_log_debug (vif, "no pci map region for bar %u", i);
1411 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1416 if ((error = vlib_pci_io_region (vm, h, 0)))
1418 virtio_log_error (vif, "error encountered on pci io region");
1422 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1423 if (interrupt_count > 1)
1425 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1426 &virtio_pci_irq_config_handler)))
1428 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1429 virtio_log_error (vif,
1430 "error encountered on pci register msix handler 0");
1435 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1436 &virtio_pci_irq_queue_handler)))
1438 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1439 virtio_log_error (vif,
1440 "error encountered on pci register msix handler 1");
1444 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
1446 virtio_log_error (vif, "error encountered on pci enable msix irq");
1449 vif->support_int_mode = 1;
1450 virtio_log_debug (vif, "device supports msix interrupts");
1452 else if (interrupt_count == 1)
1455 * if msix table-size is 1, fall back to intX.
1458 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1460 virtio_log_error (vif,
1461 "error encountered on pci register interrupt handler");
1464 vif->support_int_mode = 1;
1465 virtio_log_debug (vif, "pci register interrupt handler");
1470 * WARN: intX is showing some weird behaviour.
1471 * Please don't use interrupt mode with UIO driver.
1473 vif->support_int_mode = 0;
1474 virtio_log_debug (vif, "driver is configured in poll mode only");
1477 if ((error = vlib_pci_intr_enable (vm, h)))
1479 virtio_log_error (vif, "error encountered on pci interrupt enable");
1483 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
1485 virtio_log_error (vif, "error encountered on device init");
1489 /* create interface */
1490 error = ethernet_register_interface (vnm, virtio_device_class.index,
1491 vif->dev_instance, vif->mac_addr,
1493 virtio_pci_flag_change);
1497 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1498 virtio_log_error (vif,
1499 "error encountered on ethernet register interface");
1503 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1504 vif->sw_if_index = sw->sw_if_index;
1505 args->sw_if_index = sw->sw_if_index;
1507 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1508 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1510 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1512 error = virtio_set_packet_buffering (vif, args->buffering_size);
1515 args->rv = VNET_API_ERROR_INIT_FAILED;
1516 virtio_log_error (vif,
1517 "error encountered during packet buffering init");
1522 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1523 virtio_input_node.index);
1525 vec_foreach_index (i, vif->rxq_vrings)
1527 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1528 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1529 /* Set default rx mode to POLLING */
1530 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1531 VNET_HW_IF_RX_MODE_POLLING);
1533 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1535 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1536 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1537 VNET_HW_INTERFACE_FLAG_LINK_UP);
1540 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1542 virtio_pci_offloads (vm, vif, args->gso_enabled,
1543 args->checksum_offload_enabled);
1545 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1546 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
1548 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1549 virtio_log_warning (vif, "multiqueue is not set");
1554 virtio_pci_delete_if (vm, vif);
1556 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1557 args->error = error;
1561 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1563 vnet_main_t *vnm = vnet_get_main ();
1564 virtio_main_t *vim = &virtio_main;
1567 if (vif->type != VIRTIO_IF_TYPE_PCI)
1568 return VNET_API_ERROR_INVALID_INTERFACE;
1570 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1572 for (i = 0; i < vif->max_queue_pairs; i++)
1574 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1575 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
1578 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1579 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
1581 if (vif->virtio_pci_func)
1582 vif->virtio_pci_func->device_reset (vm, vif);
1584 if (vif->hw_if_index)
1586 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1587 vec_foreach_index (i, vif->rxq_vrings)
1589 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1591 ethernet_delete_interface (vnm, vif->hw_if_index);
1594 vlib_pci_device_close (vm, vif->pci_dev_handle);
1596 vec_foreach_index (i, vif->rxq_vrings)
1598 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1601 virtio_free_buffers (vm, vring);
1603 vec_free (vring->buffers);
1604 vlib_physmem_free (vm, vring->desc);
1607 vec_foreach_index (i, vif->txq_vrings)
1609 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1612 virtio_free_buffers (vm, vring);
1614 vec_free (vring->buffers);
1615 gro_flow_table_free (vring->flow_table);
1616 virtio_vring_buffering_free (vm, vring->buffering);
1617 clib_spinlock_free (&vring->lockp);
1618 vlib_physmem_free (vm, vring->desc);
1621 if (vif->cxq_vring != NULL)
1623 u16 last = vif->cxq_vring->last_used_idx;
1624 u16 n_left = vif->cxq_vring->used->idx - last;
1631 vif->cxq_vring->last_used_idx = last;
1632 vlib_physmem_free (vm, vif->cxq_vring->desc);
1635 vec_free (vif->rxq_vrings);
1636 vec_free (vif->txq_vrings);
1637 vec_free (vif->cxq_vring);
1639 clib_error_free (vif->error);
1640 memset (vif, 0, sizeof (*vif));
1641 pool_put (vim->interfaces, vif);
1647 virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1649 int checksum_offload_enabled,
1650 int offloads_disabled)
1652 if (vif->type != VIRTIO_IF_TYPE_PCI)
1653 return VNET_API_ERROR_INVALID_INTERFACE;
1656 virtio_pci_offloads (vm, vif, 1, 0);
1657 else if (checksum_offload_enabled)
1658 virtio_pci_offloads (vm, vif, 0, 1);
1659 else if (offloads_disabled)
1660 virtio_pci_offloads (vm, vif, 0, 0);
1666 * fd.io coding-style-patch-verification: ON
1669 * eval: (c-set-style "gnu")