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 #define PCI_CONFIG_SIZE(vif) ((vif->msix_enabled == VIRTIO_MSIX_ENABLED) ? \
42 static pci_device_id_t virtio_pci_device_ids[] = {
44 .vendor_id = PCI_VENDOR_ID_VIRTIO,
45 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
47 .vendor_id = PCI_VENDOR_ID_VIRTIO,
48 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
53 virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst,
57 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
64 vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
69 vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
74 vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, dst);
76 dst = (u8 *) dst + size;
83 virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif,
84 void *src, int len, u32 addr)
87 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
94 vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
99 vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
104 vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE (vif) + addr, src);
106 src = (u8 *) src + size;
113 virtio_pci_legacy_get_features (vlib_main_t * vm, virtio_if_t * vif)
116 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
122 virtio_pci_legacy_set_features (vlib_main_t * vm, virtio_if_t * vif,
125 if ((features >> 32) != 0)
127 clib_warning ("only 32 bit features are allowed for legacy virtio!");
129 u32 feature = 0, guest_features = (u32) features;
130 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
132 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
138 virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif)
141 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
146 virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status)
148 if (status != VIRTIO_CONFIG_STATUS_RESET)
149 status |= virtio_pci_legacy_get_status (vm, vif);
150 vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
154 virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif)
156 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET);
157 return virtio_pci_legacy_get_status (vm, vif);
161 virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif)
164 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr);
169 virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif,
173 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
175 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
181 virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
182 u16 queue_id, void *p)
184 u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
186 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
188 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
190 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
192 if ((u32) addr == addr2)
198 virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
202 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
204 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
208 virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
211 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
215 /* Enable one vector (0) for Link State Intrerrupt */
217 virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
220 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
222 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
228 virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
231 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
233 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
235 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
241 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
247 static clib_error_t *
248 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
250 virtio_main_t *vim = &virtio_main;
251 virtio_net_config_t config;
252 clib_error_t *error = 0;
253 u16 max_queue_pairs = 1;
255 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
257 virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
258 sizeof (config.max_virtqueue_pairs),
259 STRUCT_OFFSET_OF (virtio_net_config_t,
260 max_virtqueue_pairs));
261 max_queue_pairs = config.max_virtqueue_pairs;
264 virtio_log_debug (vim, vif, "max queue pair is %x", max_queue_pairs);
265 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
266 return clib_error_return (error, "max queue pair is %x", max_queue_pairs);
268 vif->max_queue_pairs = max_queue_pairs;
273 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
275 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
276 sizeof (vif->mac_addr), 0);
280 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
282 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
284 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
285 sizeof (vif->mac_addr), 0);
292 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
295 * Minimal driver: assumes link is up
298 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
299 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
300 STRUCT_OFFSET_OF (virtio_net_config_t,
306 virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
308 vnet_main_t *vnm = vnet_get_main ();
309 virtio_main_t *vim = &virtio_main;
310 uword pd = vlib_pci_get_private_data (vm, h);
311 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
314 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
318 virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
320 vnet_main_t *vnm = vnet_get_main ();
321 virtio_main_t *vim = &virtio_main;
322 uword pd = vlib_pci_get_private_data (vm, h);
323 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
325 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
327 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
328 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
329 VNET_HW_INTERFACE_FLAG_LINK_UP);
333 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
334 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
339 virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
341 virtio_main_t *vim = &virtio_main;
342 uword pd = vlib_pci_get_private_data (vm, h);
343 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
347 isr = virtio_pci_legacy_get_isr (vm, vif);
350 * If the lower bit is set: look through the used rings of
351 * all virtqueues for the device, to see if any progress has
352 * been made by the device which requires servicing.
354 if (isr & VIRTIO_PCI_ISR_INTR)
355 virtio_pci_irq_0_handler (vm, h, line);
357 if (isr & VIRTIO_PCI_ISR_CONFIG)
358 virtio_pci_irq_1_handler (vm, h, line);
362 device_status (vlib_main_t * vm, virtio_if_t * vif)
369 struct status_struct *status_entry;
370 static struct status_struct status_array[] = {
371 #define _(s,b) { .str = #s, .bit = b, },
372 foreach_virtio_config_status_flags
377 vlib_cli_output (vm, " status 0x%x", vif->status);
379 status_entry = (struct status_struct *) &status_array;
380 while (status_entry->str)
382 if (vif->status & status_entry->bit)
383 vlib_cli_output (vm, " %s (%x)", status_entry->str,
390 debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif)
395 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
397 vlib_cli_output (vm, "remote features 0x%lx", data_u32);
398 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
400 vlib_cli_output (vm, "guest features 0x%lx", data_u32);
401 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
403 vlib_cli_output (vm, "queue address 0x%lx", data_u32);
404 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
406 vlib_cli_output (vm, "queue size 0x%x", data_u16);
407 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
409 vlib_cli_output (vm, "queue select 0x%x", data_u16);
410 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
412 vlib_cli_output (vm, "queue notify 0x%x", data_u16);
413 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8);
414 vlib_cli_output (vm, "status 0x%x", data_u8);
415 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8);
416 vlib_cli_output (vm, "isr 0x%x", data_u8);
418 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
420 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
422 vlib_cli_output (vm, "config vector 0x%x", data_u16);
424 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
426 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
428 vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16);
432 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
433 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
434 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
436 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
437 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
438 /* offset to max_virtqueue */ 8);
439 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
440 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
442 vlib_cli_output (vm, "mtu 0x%x", data_u16);
444 u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4;
447 for (; i < 64; i += 4)
450 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
451 vlib_cli_output (vm, "0x%lx", data);
455 struct virtio_ctrl_msg
457 struct virtio_net_ctrl_hdr ctrl;
458 virtio_net_ctrl_ack status;
463 virtio_pci_send_ctrl_msg (vlib_main_t * vm, virtio_if_t * vif,
464 struct virtio_ctrl_msg *data, u32 len)
466 virtio_main_t *vim = &virtio_main;
467 virtio_vring_t *vring = vif->cxq_vring;
468 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
469 struct virtio_ctrl_msg result;
472 u16 used, next, avail;
473 u16 sz = vring->size;
476 used = vring->desc_in_use;
477 next = vring->desc_next;
478 avail = vring->avail->idx;
479 struct vring_desc *d = &vring->desc[next];
481 if (vlib_buffer_alloc (vm, &buffer_index, 1))
482 b = vlib_get_buffer (vm, buffer_index);
484 return VIRTIO_NET_ERR;
486 * current_data may not be initialized with 0 and may contain
490 clib_memcpy (vlib_buffer_get_current (b), data,
491 sizeof (struct virtio_ctrl_msg));
492 d->flags = VRING_DESC_F_NEXT;
493 d->addr = vlib_buffer_get_current_pa (vm, b);
494 d->len = sizeof (struct virtio_net_ctrl_hdr);
495 vring->avail->ring[avail & mask] = next;
497 next = (next + 1) & mask;
501 d = &vring->desc[next];
502 d->flags = VRING_DESC_F_NEXT;
503 d->addr = vlib_buffer_get_current_pa (vm, b) +
504 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, data);
506 next = (next + 1) & mask;
510 d = &vring->desc[next];
511 d->flags = VRING_DESC_F_WRITE;
512 d->addr = vlib_buffer_get_current_pa (vm, b) +
513 STRUCT_OFFSET_OF (struct virtio_ctrl_msg, status);
514 d->len = sizeof (data->status);
515 next = (next + 1) & mask;
518 CLIB_MEMORY_STORE_BARRIER ();
519 vring->avail->idx = avail;
520 vring->desc_next = next;
521 vring->desc_in_use = used;
523 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
525 virtio_kick (vm, vring, vif);
528 u16 last = vring->last_used_idx, n_left = 0;
529 n_left = vring->used->idx - last;
533 struct vring_used_elem *e = &vring->used->ring[last & mask];
536 d = &vring->desc[slot];
537 while (d->flags & VRING_DESC_F_NEXT)
541 d = &vring->desc[slot];
547 vring->desc_in_use = used;
548 vring->last_used_idx = last;
550 CLIB_MEMORY_BARRIER ();
551 clib_memcpy (&result, vlib_buffer_get_current (b),
552 sizeof (struct virtio_ctrl_msg));
553 virtio_log_debug (vim, vif, "ctrl-queue: status %u", result.status);
554 status = result.status;
555 vlib_buffer_free (vm, &buffer_index, 1);
560 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
562 virtio_main_t *vim = &virtio_main;
563 struct virtio_ctrl_msg gso_hdr;
564 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
566 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
567 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
568 gso_hdr.status = VIRTIO_NET_ERR;
569 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
570 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
571 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
572 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO);
573 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
575 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
576 virtio_log_debug (vim, vif, "enable gso");
581 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
584 virtio_main_t *vim = &virtio_main;
585 struct virtio_ctrl_msg mq_hdr;
586 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
588 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
589 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
590 mq_hdr.status = VIRTIO_NET_ERR;
591 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
593 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
594 virtio_log_debug (vim, vif, "multi-queue enable %u queues", num_queues);
599 virtio_pci_queue_size_valid (u16 qsz)
601 if (qsz < 64 || qsz > 4096)
609 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
612 clib_error_t *error = 0;
613 virtio_main_t *vim = &virtio_main;
615 virtio_vring_t *vring;
620 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
621 if (!virtio_pci_queue_size_valid (queue_size))
622 clib_warning ("queue size is not valid");
624 if (!is_pow2 (queue_size))
625 return clib_error_return (0, "ring size must be power of 2");
627 if (queue_size > 32768)
628 return clib_error_return (0, "ring size must be 32768 or lower");
633 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
634 vring = vec_elt_at_index (vif->cxq_vring, 0);
635 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
636 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
638 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
641 return vlib_physmem_last_error (vm);
642 clib_memset (ptr, 0, i);
643 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
644 vring->desc = vr.desc;
645 vring->avail = vr.avail;
646 vring->used = vr.used;
647 vring->queue_id = queue_num;
648 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
650 ASSERT (vring->buffers == 0);
652 vring->size = queue_size;
653 virtio_log_debug (vim, vif, "control-queue: number %u, size %u", queue_num,
655 virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
662 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
664 clib_error_t *error = 0;
665 virtio_main_t *vim = &virtio_main;
666 vlib_thread_main_t *vtm = vlib_get_thread_main ();
668 virtio_vring_t *vring;
673 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
674 if (!virtio_pci_queue_size_valid (queue_size))
675 clib_warning ("queue size is not valid");
677 if (!is_pow2 (queue_size))
678 return clib_error_return (0, "ring size must be power of 2");
680 if (queue_size > 32768)
681 return clib_error_return (0, "ring size must be 32768 or lower");
688 if (TX_QUEUE_ACCESS (queue_num) > vtm->n_vlib_mains)
690 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
691 CLIB_CACHE_LINE_BYTES);
692 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
693 clib_spinlock_init (&vring->lockp);
697 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
698 CLIB_CACHE_LINE_BYTES);
699 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
701 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
702 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
704 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
707 return vlib_physmem_last_error (vm);
708 clib_memset (ptr, 0, i);
709 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
710 vring->desc = vr.desc;
711 vring->avail = vr.avail;
712 vring->used = vr.used;
713 vring->queue_id = queue_num;
714 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
716 ASSERT (vring->buffers == 0);
717 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
720 virtio_log_debug (vim, vif, "tx-queue: number %u, size %u", queue_num,
725 virtio_log_debug (vim, vif, "rx-queue: number %u, size %u", queue_num,
728 vring->size = queue_size;
729 if (virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr))
730 return clib_error_return (0, "error in queue address setup");
737 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
741 * if features are not requested
742 * default: all supported features
744 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
745 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
746 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
747 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
748 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
749 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
750 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
751 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
752 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
753 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
754 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
755 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
756 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
757 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
758 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
759 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
760 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
761 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
762 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
764 if (req_features == 0)
766 req_features = supported_features;
769 vif->features = req_features & vif->remote_features & supported_features;
771 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
773 virtio_net_config_t config;
774 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
776 STRUCT_OFFSET_OF (virtio_net_config_t,
779 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
782 vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
786 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
788 vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
792 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
799 status = virtio_pci_legacy_reset (vm, vif);
802 * Set the Acknowledge status bit
804 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
807 * Set the Driver status bit
809 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
812 * Read the status and verify it
814 status = virtio_pci_legacy_get_status (vm, vif);
816 ((status & VIRTIO_CONFIG_STATUS_ACK)
817 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
819 vif->status = status;
825 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
827 clib_error_t *error = 0;
828 virtio_main_t *vim = &virtio_main;
829 struct virtio_pci_cap cap;
830 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
831 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
833 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
835 virtio_log_error (vim, vif, "error in reading capabilty list position");
836 clib_error_return (error, "error in reading capabilty list position");
841 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
844 virtio_log_error (vim, vif, "%s [%2x]",
845 "error in reading the capability at", pos);
846 clib_error_return (error,
847 "error in reading the capability at [%2x]", pos);
850 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
852 u16 flags, table_size, table_size_mask = 0x07FF;
855 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
857 clib_error_return (error,
858 "error in reading the capability at [%2x]",
861 table_size = flags & table_size_mask;
862 virtio_log_debug (vim, vif, "flags:0x%x %s 0x%x", flags,
863 "msix interrupt vector table-size", table_size);
865 if (flags & PCI_MSIX_ENABLE)
867 virtio_log_debug (vim, vif, "msix interrupt enabled");
868 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
872 virtio_log_debug (vim, vif, "msix interrupt disabled");
873 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
877 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
879 virtio_log_debug (vim, vif, "[%2x] %s %2x ", pos,
880 "skipping non VNDR cap id:", cap.cap_vndr);
884 virtio_log_debug (vim, vif,
885 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
886 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
887 switch (cap.cfg_type)
889 case VIRTIO_PCI_CAP_COMMON_CFG:
892 case VIRTIO_PCI_CAP_NOTIFY_CFG:
895 case VIRTIO_PCI_CAP_DEVICE_CFG:
898 case VIRTIO_PCI_CAP_ISR_CFG:
901 case VIRTIO_PCI_CAP_PCI_CFG:
910 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
912 virtio_log_debug (vim, vif, "legacy virtio pci device found");
917 clib_error_return (error, "modern virtio pci device found");
919 virtio_log_debug (vim, vif, "transitional virtio pci device found");
923 static clib_error_t *
924 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
925 virtio_pci_create_if_args_t * args)
927 clib_error_t *error = 0;
928 virtio_main_t *vim = &virtio_main;
931 if ((error = virtio_pci_read_caps (vm, vif)))
932 clib_error_return (error, "Device is not supported");
934 if (virtio_pci_reset_device (vm, vif) < 0)
936 virtio_log_error (vim, vif, "Failed to reset the device");
937 clib_error_return (error, "Failed to reset the device");
940 * read device features and negotiate (user) requested features
942 virtio_pci_read_device_feature (vm, vif);
943 virtio_negotiate_features (vm, vif, args->features);
946 * After FEATURE_OK, driver should not accept new feature bits
948 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
949 status = virtio_pci_legacy_get_status (vm, vif);
950 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
952 virtio_log_error (vim, vif,
953 "error encountered: Device doesn't support requested features");
954 clib_error_return (error, "Device doesn't support requested features");
956 vif->status = status;
959 * get or set the mac address
961 if (virtio_pci_get_mac (vm, vif))
963 f64 now = vlib_time_now (vm);
965 rnd = (u32) (now * 1e6);
966 rnd = random_u32 (&rnd);
968 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
969 vif->mac_addr[0] = 2;
970 vif->mac_addr[1] = 0xfe;
971 virtio_pci_set_mac (vm, vif);
974 virtio_set_net_hdr_size (vif);
977 * Initialize the virtqueues
979 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
982 for (int i = 0; i < vif->max_queue_pairs; i++)
984 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
986 virtio_log_warning (vim, vif, "%s (%u) %s", "error in rxq-queue",
987 RX_QUEUE (i), "initialization");
994 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
996 virtio_log_warning (vim, vif, "%s (%u) %s", "error in txq-queue",
997 TX_QUEUE (i), "initialization");
1005 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1008 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
1010 virtio_log_warning (vim, vif, "%s (%u) %s",
1011 "error in control-queue",
1012 vif->max_queue_pairs * 2, "initialization");
1013 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1014 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
1019 virtio_log_debug (vim, vif, "control queue is not available");
1020 vif->cxq_vring = NULL;
1024 * set the msix interrupts
1026 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
1028 if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
1029 VIRTIO_MSI_NO_VECTOR)
1030 virtio_log_warning (vim, vif, "config vector 1 is not set");
1031 if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
1032 VIRTIO_MSI_NO_VECTOR)
1033 virtio_log_warning (vim, vif, "queue vector 0 is not set");
1037 * set the driver status OK
1039 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
1040 vif->status = virtio_pci_legacy_get_status (vm, vif);
1046 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1048 vnet_main_t *vnm = vnet_get_main ();
1049 virtio_main_t *vim = &virtio_main;
1051 vlib_pci_dev_handle_t h;
1052 clib_error_t *error = 0;
1055 pool_foreach (vif, vim->interfaces, ({
1056 if (vif->pci_addr.as_u32 == args->addr)
1058 args->rv = VNET_API_ERROR_INVALID_VALUE;
1060 clib_error_return (error, "PCI address in use");
1061 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1062 format_vlib_pci_addr, &args->addr,
1063 " PCI address in use");
1069 pool_get (vim->interfaces, vif);
1070 vif->dev_instance = vif - vim->interfaces;
1071 vif->per_interface_next_index = ~0;
1072 vif->pci_addr.as_u32 = args->addr;
1075 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1076 virtio_pci_device_ids, &h)))
1078 pool_put (vim->interfaces, vif);
1079 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1081 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1083 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1084 format_vlib_pci_addr, &vif->pci_addr,
1085 "error encountered on pci device open");
1088 vif->pci_dev_handle = h;
1089 vlib_pci_set_private_data (vm, h, vif->dev_instance);
1090 vif->numa_node = vlib_pci_get_numa_node (vm, h);
1092 if ((error = vlib_pci_bus_master_enable (vm, h)))
1094 virtio_log_error (vim, vif,
1095 "error encountered on pci bus master enable");
1099 if ((error = vlib_pci_io_region (vm, h, 0)))
1101 virtio_log_error (vim, vif, "error encountered on pci io region");
1105 if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
1107 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1108 &virtio_pci_irq_0_handler)))
1110 virtio_log_error (vim, vif,
1111 "error encountered on pci register msix handler 0");
1114 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1115 &virtio_pci_irq_1_handler)))
1117 virtio_log_error (vim, vif,
1118 "error encountered on pci register msix handler 1");
1122 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
1124 virtio_log_error (vim, vif,
1125 "error encountered on pci enable msix irq");
1128 vif->support_int_mode = 1;
1129 virtio_log_debug (vim, vif, "device supports msix interrupts");
1131 else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1134 * if msix table-size is 1, fall back to intX.
1137 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1139 virtio_log_error (vim, vif,
1140 "error encountered on pci register interrupt handler");
1143 vif->support_int_mode = 1;
1144 virtio_log_debug (vim, vif, "pci register interrupt handler");
1149 * WARN: intX is showing some weird behaviour.
1150 * Please don't use interrupt mode with UIO driver.
1152 vif->support_int_mode = 0;
1153 virtio_log_debug (vim, vif, "driver is configured in poll mode only");
1156 if ((error = vlib_pci_intr_enable (vm, h)))
1158 virtio_log_error (vim, vif,
1159 "error encountered on pci interrupt enable");
1163 if ((error = virtio_pci_device_init (vm, vif, args)))
1165 virtio_log_error (vim, vif, "error encountered on device init");
1169 vif->type = VIRTIO_IF_TYPE_PCI;
1170 /* create interface */
1171 error = ethernet_register_interface (vnm, virtio_device_class.index,
1172 vif->dev_instance, vif->mac_addr,
1174 virtio_pci_flag_change);
1178 virtio_log_error (vim, vif,
1179 "error encountered on ethernet register interface");
1183 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1184 vif->sw_if_index = sw->sw_if_index;
1185 args->sw_if_index = sw->sw_if_index;
1187 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1188 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1189 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1190 virtio_input_node.index);
1192 vec_foreach_index (i, vif->rxq_vrings)
1194 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1195 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1196 /* Set default rx mode to POLLING */
1197 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1198 VNET_HW_INTERFACE_RX_MODE_POLLING);
1200 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1202 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1203 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1204 VNET_HW_INTERFACE_FLAG_LINK_UP);
1207 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1209 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1211 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) &&
1214 if (virtio_pci_enable_gso (vm, vif))
1216 virtio_log_warning (vim, vif, "gso is not enabled");
1220 vif->gso_enabled = 1;
1221 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
1222 vnm->interface_main.gso_interface_count++;
1225 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
1227 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1228 virtio_log_warning (vim, vif, "multiqueue is not set");
1235 virtio_pci_delete_if (vm, vif);
1236 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1237 args->error = error;
1241 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1243 vnet_main_t *vnm = vnet_get_main ();
1244 virtio_main_t *vim = &virtio_main;
1247 if (vif->type != VIRTIO_IF_TYPE_PCI)
1248 return VNET_API_ERROR_INVALID_INTERFACE;
1250 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1252 for (i = 0; i < vif->max_queue_pairs; i++)
1254 virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1255 virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1258 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1259 virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
1261 virtio_pci_legacy_reset (vm, vif);
1263 if (vif->gso_enabled)
1264 vnm->interface_main.gso_interface_count--;
1266 if (vif->hw_if_index)
1268 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1269 vec_foreach_index (i, vif->rxq_vrings)
1271 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1273 ethernet_delete_interface (vnm, vif->hw_if_index);
1276 vlib_pci_device_close (vm, vif->pci_dev_handle);
1278 vec_foreach_index (i, vif->rxq_vrings)
1280 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1281 if (vring->kick_fd != -1)
1282 close (vring->kick_fd);
1285 virtio_free_rx_buffers (vm, vring);
1287 vec_free (vring->buffers);
1288 vlib_physmem_free (vm, vring->desc);
1291 vec_foreach_index (i, vif->txq_vrings)
1293 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1294 if (vring->kick_fd != -1)
1295 close (vring->kick_fd);
1298 virtio_free_used_desc (vm, vring);
1300 vec_free (vring->buffers);
1301 vlib_physmem_free (vm, vring->desc);
1304 if (vif->cxq_vring != NULL)
1306 u16 last = vif->cxq_vring->last_used_idx;
1307 u16 n_left = vif->cxq_vring->used->idx - last;
1314 vif->cxq_vring->last_used_idx = last;
1315 vlib_physmem_free (vm, vif->cxq_vring->desc);
1318 vec_free (vif->rxq_vrings);
1319 vec_free (vif->txq_vrings);
1320 vec_free (vif->cxq_vring);
1324 if (vif->tap_fd != -1)
1326 clib_error_free (vif->error);
1327 memset (vif, 0, sizeof (*vif));
1328 pool_put (vim->interfaces, vif);
1334 * fd.io coding-style-patch-verification: ON
1337 * eval: (c-set-style "gnu")