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,
182 virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
183 u16 queue_id, void *p)
185 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,
193 virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
197 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
199 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
203 virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
206 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
210 /* Enable one vector (0) for Link State Intrerrupt */
212 virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
215 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
217 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
223 virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
226 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
228 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
230 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
236 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
242 static clib_error_t *
243 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
245 virtio_main_t *vim = &virtio_main;
246 virtio_net_config_t config;
247 clib_error_t *error = 0;
248 u16 max_queue_pairs = 1;
250 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
252 virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
253 sizeof (config.max_virtqueue_pairs),
254 STRUCT_OFFSET_OF (virtio_net_config_t,
255 max_virtqueue_pairs));
256 max_queue_pairs = config.max_virtqueue_pairs;
259 virtio_log_debug (vim, vif, "max queue pair is %x", max_queue_pairs);
260 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
261 return clib_error_return (error, "max queue pair is %x", max_queue_pairs);
263 vif->max_queue_pairs = max_queue_pairs;
268 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
270 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
271 sizeof (vif->mac_addr), 0);
275 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
277 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
279 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
280 sizeof (vif->mac_addr), 0);
287 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
290 * Minimal driver: assumes link is up
293 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
294 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
295 STRUCT_OFFSET_OF (virtio_net_config_t,
301 virtio_pci_irq_0_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
303 vnet_main_t *vnm = vnet_get_main ();
304 virtio_main_t *vim = &virtio_main;
305 uword pd = vlib_pci_get_private_data (vm, h);
306 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
309 vnet_device_input_set_interrupt_pending (vnm, vif->hw_if_index, qid);
313 virtio_pci_irq_1_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, u16 line)
315 vnet_main_t *vnm = vnet_get_main ();
316 virtio_main_t *vim = &virtio_main;
317 uword pd = vlib_pci_get_private_data (vm, h);
318 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
320 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
322 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
323 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
324 VNET_HW_INTERFACE_FLAG_LINK_UP);
328 vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
329 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
334 virtio_pci_irq_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h)
336 virtio_main_t *vim = &virtio_main;
337 uword pd = vlib_pci_get_private_data (vm, h);
338 virtio_if_t *vif = pool_elt_at_index (vim->interfaces, pd);
342 isr = virtio_pci_legacy_get_isr (vm, vif);
345 * If the lower bit is set: look through the used rings of
346 * all virtqueues for the device, to see if any progress has
347 * been made by the device which requires servicing.
349 if (isr & VIRTIO_PCI_ISR_INTR)
350 virtio_pci_irq_0_handler (vm, h, line);
352 if (isr & VIRTIO_PCI_ISR_CONFIG)
353 virtio_pci_irq_1_handler (vm, h, line);
357 device_status (vlib_main_t * vm, virtio_if_t * vif)
364 struct status_struct *status_entry;
365 static struct status_struct status_array[] = {
366 #define _(s,b) { .str = #s, .bit = b, },
367 foreach_virtio_config_status_flags
372 vlib_cli_output (vm, " status 0x%x", vif->status);
374 status_entry = (struct status_struct *) &status_array;
375 while (status_entry->str)
377 if (vif->status & status_entry->bit)
378 vlib_cli_output (vm, " %s (%x)", status_entry->str,
385 debug_device_config_space (vlib_main_t * vm, virtio_if_t * vif)
390 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
392 vlib_cli_output (vm, "remote features 0x%lx", data_u32);
393 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
395 vlib_cli_output (vm, "guest features 0x%lx", data_u32);
396 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
398 vlib_cli_output (vm, "queue address 0x%lx", data_u32);
399 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
401 vlib_cli_output (vm, "queue size 0x%x", data_u16);
402 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
404 vlib_cli_output (vm, "queue select 0x%x", data_u16);
405 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
407 vlib_cli_output (vm, "queue notify 0x%x", data_u16);
408 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &data_u8);
409 vlib_cli_output (vm, "status 0x%x", data_u8);
410 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &data_u8);
411 vlib_cli_output (vm, "isr 0x%x", data_u8);
413 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
415 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
417 vlib_cli_output (vm, "config vector 0x%x", data_u16);
419 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
421 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
423 vlib_cli_output (vm, "queue vector for queue (0) 0x%x", data_u16);
427 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
428 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
429 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
431 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
432 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
433 /* offset to max_virtqueue */ 8);
434 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
435 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
437 vlib_cli_output (vm, "mtu 0x%x", data_u16);
439 u32 i = PCI_CONFIG_SIZE (vif) + 12, a = 4;
442 for (; i < 64; i += 4)
445 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
446 vlib_cli_output (vm, "0x%lx", data);
450 struct virtio_ctrl_mq_status_hdr
452 struct virtio_net_ctrl_hdr ctrl;
453 struct virtio_net_ctrl_mq num_mqs;
454 virtio_net_ctrl_ack status;
458 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
461 virtio_main_t *vim = &virtio_main;
462 virtio_vring_t *vring = vif->cxq_vring;
465 u16 used, next, avail;
466 u16 sz = vring->size;
468 struct virtio_ctrl_mq_status_hdr mq_hdr, result;
469 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
471 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
472 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
473 mq_hdr.status = VIRTIO_NET_ERR;
474 mq_hdr.num_mqs.virtqueue_pairs = num_queues;
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), &mq_hdr,
491 sizeof (struct virtio_ctrl_mq_status_hdr));
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_mq_status_hdr, num_mqs);
505 d->len = sizeof (struct virtio_net_ctrl_mq);
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_mq_status_hdr, status);
514 d->len = sizeof (mq_hdr.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 clib_memset (&result, 0, sizeof (result));
529 u16 last = vring->last_used_idx, n_left = 0;
530 n_left = vring->used->idx - last;
534 struct vring_used_elem *e = &vring->used->ring[last & mask];
537 d = &vring->desc[slot];
538 while (d->flags & VRING_DESC_F_NEXT)
542 d = &vring->desc[slot];
548 vring->desc_in_use = used;
549 vring->last_used_idx = last;
551 CLIB_MEMORY_BARRIER ();
552 clib_memcpy (&result, vlib_buffer_get_current (b),
553 sizeof (struct virtio_ctrl_mq_status_hdr));
555 virtio_log_debug (vim, vif, "multi-queue enable status on Ctrl queue : %u",
557 status = result.status;
558 vlib_buffer_free (vm, &buffer_index, 1);
563 virtio_pci_queue_size_valid (u16 qsz)
565 if (qsz < 64 || qsz > 4096)
573 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
576 clib_error_t *error = 0;
577 virtio_main_t *vim = &virtio_main;
579 virtio_vring_t *vring;
584 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
585 if (!virtio_pci_queue_size_valid (queue_size))
586 clib_warning ("queue size is not valid");
588 if (!is_pow2 (queue_size))
589 return clib_error_return (0, "ring size must be power of 2");
591 if (queue_size > 32768)
592 return clib_error_return (0, "ring size must be 32768 or lower");
597 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
598 vring = vec_elt_at_index (vif->cxq_vring, 0);
599 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
600 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
602 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
605 return vlib_physmem_last_error (vm);
606 clib_memset (ptr, 0, i);
607 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
608 vring->desc = vr.desc;
609 vring->avail = vr.avail;
610 vring->used = vr.used;
611 vring->queue_id = queue_num;
612 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
614 ASSERT (vring->buffers == 0);
616 vring->size = queue_size;
617 virtio_log_debug (vim, vif, "control-queue: number %u, size %u", queue_num,
619 virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
626 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
628 clib_error_t *error = 0;
629 virtio_main_t *vim = &virtio_main;
630 vlib_thread_main_t *vtm = vlib_get_thread_main ();
632 virtio_vring_t *vring;
637 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, queue_num);
638 if (!virtio_pci_queue_size_valid (queue_size))
639 clib_warning ("queue size is not valid");
641 if (!is_pow2 (queue_size))
642 return clib_error_return (0, "ring size must be power of 2");
644 if (queue_size > 32768)
645 return clib_error_return (0, "ring size must be 32768 or lower");
652 if (TX_QUEUE_ACCESS (queue_num) > vtm->n_vlib_mains)
654 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
655 CLIB_CACHE_LINE_BYTES);
656 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
657 clib_spinlock_init (&vring->lockp);
661 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
662 CLIB_CACHE_LINE_BYTES);
663 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
665 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
666 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
668 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
671 return vlib_physmem_last_error (vm);
672 clib_memset (ptr, 0, i);
673 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
674 vring->desc = vr.desc;
675 vring->avail = vr.avail;
676 vring->used = vr.used;
677 vring->queue_id = queue_num;
678 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
680 ASSERT (vring->buffers == 0);
681 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
682 ASSERT (vring->indirect_buffers == 0);
683 vec_validate_aligned (vring->indirect_buffers, queue_size,
684 CLIB_CACHE_LINE_BYTES);
690 if (n_alloc < queue_size)
692 vlib_buffer_alloc (vm, vring->indirect_buffers + n_alloc,
693 queue_size - n_alloc);
695 while (n_alloc != queue_size);
697 virtio_log_debug (vim, vif, "tx-queue: number %u, size %u", queue_num,
703 virtio_log_debug (vim, vif, "rx-queue: number %u, size %u", queue_num,
706 vring->size = queue_size;
707 virtio_pci_legacy_setup_queue (vm, vif, queue_num, ptr);
714 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
718 * if features are not requested
719 * default: all supported features
721 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
722 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
723 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
724 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
725 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
726 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
727 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
728 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
729 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
731 if (req_features == 0)
733 req_features = supported_features;
736 vif->features = req_features & vif->remote_features & supported_features;
738 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
740 virtio_net_config_t config;
741 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
743 STRUCT_OFFSET_OF (virtio_net_config_t,
746 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
749 vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
753 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
755 vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
759 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
766 status = virtio_pci_legacy_reset (vm, vif);
769 * Set the Acknowledge status bit
771 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
774 * Set the Driver status bit
776 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
779 * Read the status and verify it
781 status = virtio_pci_legacy_get_status (vm, vif);
783 ((status & VIRTIO_CONFIG_STATUS_ACK)
784 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
786 vif->status = status;
792 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
794 clib_error_t *error = 0;
795 virtio_main_t *vim = &virtio_main;
796 struct virtio_pci_cap cap;
797 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
798 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
800 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
802 virtio_log_error (vim, vif, "error in reading capabilty list position");
803 clib_error_return (error, "error in reading capabilty list position");
808 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
811 virtio_log_error (vim, vif, "%s [%2x]",
812 "error in reading the capability at", pos);
813 clib_error_return (error,
814 "error in reading the capability at [%2x]", pos);
817 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
819 u16 flags, table_size, table_size_mask = 0x07FF;
822 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
824 clib_error_return (error,
825 "error in reading the capability at [%2x]",
828 table_size = flags & table_size_mask;
829 virtio_log_debug (vim, vif, "flags:0x%x %s 0x%x", flags,
830 "msix interrupt vector table-size", table_size);
832 if (flags & PCI_MSIX_ENABLE)
834 virtio_log_debug (vim, vif, "msix interrupt enabled");
835 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
839 virtio_log_debug (vim, vif, "msix interrupt disabled");
840 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
844 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
846 virtio_log_debug (vim, vif, "[%2x] %s %2x ", pos,
847 "skipping non VNDR cap id:", cap.cap_vndr);
851 virtio_log_debug (vim, vif,
852 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
853 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
854 switch (cap.cfg_type)
856 case VIRTIO_PCI_CAP_COMMON_CFG:
859 case VIRTIO_PCI_CAP_NOTIFY_CFG:
862 case VIRTIO_PCI_CAP_DEVICE_CFG:
865 case VIRTIO_PCI_CAP_ISR_CFG:
868 case VIRTIO_PCI_CAP_PCI_CFG:
877 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
879 virtio_log_debug (vim, vif, "legacy virtio pci device found");
884 clib_error_return (error, "modern virtio pci device found");
886 virtio_log_debug (vim, vif, "transitional virtio pci device found");
890 static clib_error_t *
891 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
892 virtio_pci_create_if_args_t * args)
894 clib_error_t *error = 0;
895 virtio_main_t *vim = &virtio_main;
898 if ((error = virtio_pci_read_caps (vm, vif)))
899 clib_error_return (error, "Device is not supported");
901 if (virtio_pci_reset_device (vm, vif) < 0)
903 virtio_log_error (vim, vif, "Failed to reset the device");
904 clib_error_return (error, "Failed to reset the device");
907 * read device features and negotiate (user) requested features
909 virtio_pci_read_device_feature (vm, vif);
910 virtio_negotiate_features (vm, vif, args->features);
913 * After FEATURE_OK, driver should not accept new feature bits
915 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
916 status = virtio_pci_legacy_get_status (vm, vif);
917 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
919 virtio_log_error (vim, vif,
920 "error encountered: Device doesn't support requested features");
921 clib_error_return (error, "Device doesn't support requested features");
923 vif->status = status;
926 * get or set the mac address
928 if (virtio_pci_get_mac (vm, vif))
930 f64 now = vlib_time_now (vm);
932 rnd = (u32) (now * 1e6);
933 rnd = random_u32 (&rnd);
935 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
936 vif->mac_addr[0] = 2;
937 vif->mac_addr[1] = 0xfe;
938 virtio_pci_set_mac (vm, vif);
941 virtio_set_net_hdr_size (vif);
944 * Initialize the virtqueues
946 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
949 for (int i = 0; i < vif->max_queue_pairs; i++)
951 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
952 virtio_log_warning (vim, vif, "%s (%u) %s", "error in rxq-queue",
953 RX_QUEUE (i), "initialization");
955 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
956 virtio_log_warning (vim, vif, "%s (%u) %s", "error in txq-queue",
957 TX_QUEUE (i), "initialization");
960 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
963 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
965 virtio_log_warning (vim, vif, "%s (%u) %s",
966 "error in control-queue",
967 vif->max_queue_pairs * 2, "initialization");
968 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
969 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
974 virtio_log_debug (vim, vif, "control queue is not available");
975 vif->cxq_vring = NULL;
979 * set the msix interrupts
981 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
983 if (virtio_pci_legacy_set_config_irq (vm, vif, 1) ==
984 VIRTIO_MSI_NO_VECTOR)
985 virtio_log_warning (vim, vif, "config vector 1 is not set");
986 if (virtio_pci_legacy_set_queue_irq (vm, vif, 0, 0) ==
987 VIRTIO_MSI_NO_VECTOR)
988 virtio_log_warning (vim, vif, "queue vector 0 is not set");
992 * set the driver status OK
994 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
995 vif->status = virtio_pci_legacy_get_status (vm, vif);
1001 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
1003 vnet_main_t *vnm = vnet_get_main ();
1004 virtio_main_t *vim = &virtio_main;
1006 vlib_pci_dev_handle_t h;
1007 clib_error_t *error = 0;
1009 if (args->rxq_size == 0)
1010 args->rxq_size = VIRTIO_NUM_RX_DESC;
1011 if (args->txq_size == 0)
1012 args->txq_size = VIRTIO_NUM_TX_DESC;
1014 if (!virtio_pci_queue_size_valid (args->rxq_size) ||
1015 !virtio_pci_queue_size_valid (args->txq_size))
1017 args->rv = VNET_API_ERROR_INVALID_VALUE;
1019 clib_error_return (error,
1020 "queue size must be <= 4096, >= 64, "
1021 "and multiples of 64");
1022 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1023 format_vlib_pci_addr, &args->addr,
1024 "queue size must be <= 4096, >= 64, and multiples of 64");
1029 pool_foreach (vif, vim->interfaces, ({
1030 if (vif->pci_addr.as_u32 == args->addr)
1032 args->rv = VNET_API_ERROR_INVALID_VALUE;
1034 clib_error_return (error, "PCI address in use");
1035 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1036 format_vlib_pci_addr, &args->addr,
1037 " PCI address in use");
1043 pool_get (vim->interfaces, vif);
1044 vif->dev_instance = vif - vim->interfaces;
1045 vif->per_interface_next_index = ~0;
1046 vif->pci_addr.as_u32 = args->addr;
1049 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1050 virtio_pci_device_ids, &h)))
1052 pool_put (vim->interfaces, vif);
1053 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1055 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1057 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1058 format_vlib_pci_addr, &vif->pci_addr,
1059 "error encountered on pci device open");
1062 vif->pci_dev_handle = h;
1063 vlib_pci_set_private_data (vm, h, vif->dev_instance);
1064 vif->numa_node = vlib_pci_get_numa_node (vm, h);
1066 if ((error = vlib_pci_bus_master_enable (vm, h)))
1068 virtio_log_error (vim, vif,
1069 "error encountered on pci bus master enable");
1073 if ((error = vlib_pci_io_region (vm, h, 0)))
1075 virtio_log_error (vim, vif, "error encountered on pci io region");
1079 if (vlib_pci_get_num_msix_interrupts (vm, h) > 1)
1081 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1082 &virtio_pci_irq_0_handler)))
1084 virtio_log_error (vim, vif,
1085 "error encountered on pci register msix handler 0");
1088 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
1089 &virtio_pci_irq_1_handler)))
1091 virtio_log_error (vim, vif,
1092 "error encountered on pci register msix handler 1");
1096 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
1098 virtio_log_error (vim, vif,
1099 "error encountered on pci enable msix irq");
1102 vif->support_int_mode = 1;
1103 virtio_log_debug (vim, vif, "device supports msix interrupts");
1105 else if (vlib_pci_get_num_msix_interrupts (vm, h) == 1)
1108 * if msix table-size is 1, fall back to intX.
1111 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1113 virtio_log_error (vim, vif,
1114 "error encountered on pci register interrupt handler");
1117 vif->support_int_mode = 1;
1118 virtio_log_debug (vim, vif, "pci register interrupt handler");
1123 * WARN: intX is showing some weird behaviour.
1124 * Please don't use interrupt mode with UIO driver.
1126 vif->support_int_mode = 0;
1127 virtio_log_debug (vim, vif, "driver is configured in poll mode only");
1130 if ((error = vlib_pci_intr_enable (vm, h)))
1132 virtio_log_error (vim, vif,
1133 "error encountered on pci interrupt enable");
1137 if ((error = virtio_pci_device_init (vm, vif, args)))
1139 virtio_log_error (vim, vif, "error encountered on device init");
1143 vif->type = VIRTIO_IF_TYPE_PCI;
1144 /* create interface */
1145 error = ethernet_register_interface (vnm, virtio_device_class.index,
1146 vif->dev_instance, vif->mac_addr,
1148 virtio_pci_flag_change);
1152 virtio_log_error (vim, vif,
1153 "error encountered on ethernet register interface");
1157 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1158 vif->sw_if_index = sw->sw_if_index;
1159 args->sw_if_index = sw->sw_if_index;
1161 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1162 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1163 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1164 virtio_input_node.index);
1166 vec_foreach_index (i, vif->rxq_vrings)
1168 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1169 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1170 /* Set default rx mode to POLLING */
1171 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1172 VNET_HW_INTERFACE_RX_MODE_POLLING);
1174 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1176 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1177 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1178 VNET_HW_INTERFACE_FLAG_LINK_UP);
1181 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1183 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1184 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
1186 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1187 virtio_log_warning (vim, vif, "multiqueue is not set");
1192 virtio_pci_delete_if (vm, vif);
1193 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1194 args->error = error;
1198 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1200 vnet_main_t *vnm = vnet_get_main ();
1201 virtio_main_t *vim = &virtio_main;
1204 if (vif->type != VIRTIO_IF_TYPE_PCI)
1205 return VNET_API_ERROR_INVALID_INTERFACE;
1207 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1209 for (i = 0; i < vif->max_queue_pairs; i++)
1211 virtio_pci_legacy_del_queue (vm, vif, RX_QUEUE (i));
1212 virtio_pci_legacy_del_queue (vm, vif, TX_QUEUE (i));
1215 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1216 virtio_pci_legacy_del_queue (vm, vif, vif->max_queue_pairs * 2);
1218 virtio_pci_legacy_reset (vm, vif);
1220 if (vif->hw_if_index)
1222 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1223 vec_foreach_index (i, vif->rxq_vrings)
1225 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1227 ethernet_delete_interface (vnm, vif->hw_if_index);
1230 vlib_pci_device_close (vm, vif->pci_dev_handle);
1232 vec_foreach_index (i, vif->rxq_vrings)
1234 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1235 if (vring->kick_fd != -1)
1236 close (vring->kick_fd);
1239 virtio_free_rx_buffers (vm, vring);
1241 vec_free (vring->buffers);
1242 vec_free (vring->indirect_buffers);
1243 vlib_physmem_free (vm, vring->desc);
1246 vec_foreach_index (i, vif->txq_vrings)
1248 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1249 if (vring->kick_fd != -1)
1250 close (vring->kick_fd);
1253 virtio_free_used_desc (vm, vring);
1255 if (vring->queue_id % 2)
1257 vlib_buffer_free_no_next (vm, vring->indirect_buffers, vring->size);
1259 vec_free (vring->buffers);
1260 vec_free (vring->indirect_buffers);
1261 vlib_physmem_free (vm, vring->desc);
1264 if (vif->cxq_vring != NULL)
1266 u16 last = vif->cxq_vring->last_used_idx;
1267 u16 n_left = vif->cxq_vring->used->idx - last;
1274 vif->cxq_vring->last_used_idx = last;
1275 vlib_physmem_free (vm, vif->cxq_vring->desc);
1278 vec_free (vif->rxq_vrings);
1279 vec_free (vif->txq_vrings);
1280 vec_free (vif->cxq_vring);
1284 if (vif->tap_fd != -1)
1286 clib_error_free (vif->error);
1287 memset (vif, 0, sizeof (*vif));
1288 pool_put (vim->interfaces, vif);
1294 * fd.io coding-style-patch-verification: ON
1297 * eval: (c-set-style "gnu")