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 (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;
206 u16 used, next, avail;
207 u16 sz = vring->size;
210 used = vring->desc_in_use;
211 next = vring->desc_next;
212 avail = vring->avail->idx;
213 vring_desc_t *d = &vring->desc[next];
215 if (vlib_buffer_alloc (vm, &buffer_index, 1))
216 b = vlib_get_buffer (vm, buffer_index);
218 return VIRTIO_NET_ERR;
220 * current_data may not be initialized with 0 and may contain
224 clib_memcpy (vlib_buffer_get_current (b), data, sizeof (virtio_ctrl_msg_t));
225 d->flags = VRING_DESC_F_NEXT;
226 d->addr = vlib_buffer_get_current_pa (vm, b);
227 d->len = sizeof (virtio_net_ctrl_hdr_t);
228 vring->avail->ring[avail & mask] = next;
230 next = (next + 1) & mask;
234 d = &vring->desc[next];
235 d->flags = VRING_DESC_F_NEXT;
236 d->addr = vlib_buffer_get_current_pa (vm, b) +
237 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, data);
239 next = (next + 1) & mask;
243 d = &vring->desc[next];
244 d->flags = VRING_DESC_F_WRITE;
245 d->addr = vlib_buffer_get_current_pa (vm, b) +
246 STRUCT_OFFSET_OF (virtio_ctrl_msg_t, status);
247 d->len = sizeof (data->status);
248 next = (next + 1) & mask;
251 CLIB_MEMORY_STORE_BARRIER ();
252 vring->avail->idx = avail;
253 vring->desc_next = next;
254 vring->desc_in_use = used;
256 if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
258 virtio_kick (vm, vring, vif);
261 u16 last = vring->last_used_idx, n_left = 0;
262 n_left = vring->used->idx - last;
266 vring_used_elem_t *e = &vring->used->ring[last & mask];
269 d = &vring->desc[slot];
270 while (d->flags & VRING_DESC_F_NEXT)
274 d = &vring->desc[slot];
280 vring->desc_in_use = used;
281 vring->last_used_idx = last;
283 CLIB_MEMORY_BARRIER ();
284 clib_memcpy (&result, vlib_buffer_get_current (b),
285 sizeof (virtio_ctrl_msg_t));
286 virtio_log_debug (vif, "ctrl-queue: status %u", result.status);
287 status = result.status;
288 vlib_buffer_free (vm, &buffer_index, 1);
293 virtio_pci_disable_offload (vlib_main_t * vm, virtio_if_t * vif)
295 virtio_ctrl_msg_t offload_hdr;
296 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
298 offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
299 offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
300 offload_hdr.status = VIRTIO_NET_ERR;
302 clib_memcpy (offload_hdr.data, &offloads, sizeof (offloads));
305 virtio_pci_send_ctrl_msg (vm, vif, &offload_hdr, sizeof (offloads));
306 virtio_log_debug (vif, "disable offloads");
307 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
308 vif->virtio_pci_func->get_driver_features (vm, vif);
313 virtio_pci_enable_checksum_offload (vlib_main_t * vm, virtio_if_t * vif)
315 virtio_ctrl_msg_t csum_offload_hdr;
316 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
318 csum_offload_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
319 csum_offload_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
320 csum_offload_hdr.status = VIRTIO_NET_ERR;
322 offloads |= VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM);
323 clib_memcpy (csum_offload_hdr.data, &offloads, sizeof (offloads));
326 virtio_pci_send_ctrl_msg (vm, vif, &csum_offload_hdr, sizeof (offloads));
327 virtio_log_debug (vif, "enable checksum offload");
328 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
329 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
334 virtio_pci_enable_gso (vlib_main_t * vm, virtio_if_t * vif)
336 virtio_ctrl_msg_t gso_hdr;
337 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
339 gso_hdr.ctrl.class = VIRTIO_NET_CTRL_GUEST_OFFLOADS;
340 gso_hdr.ctrl.cmd = VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET;
341 gso_hdr.status = VIRTIO_NET_ERR;
342 u64 offloads = VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
343 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
344 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6);
345 clib_memcpy (gso_hdr.data, &offloads, sizeof (offloads));
347 status = virtio_pci_send_ctrl_msg (vm, vif, &gso_hdr, sizeof (offloads));
348 virtio_log_debug (vif, "enable gso");
349 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
350 vif->virtio_pci_func->get_driver_features (vm, vif);
355 virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled,
356 int csum_offload_enabled)
358 vnet_main_t *vnm = vnet_get_main ();
359 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
361 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
362 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)))
365 && (vif->features & (VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |
366 VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))))
368 if (virtio_pci_enable_gso (vm, vif))
370 virtio_log_warning (vif, "gso is not enabled");
374 vif->gso_enabled = 1;
375 vif->csum_offload_enabled = 0;
376 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
377 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
380 else if (csum_offload_enabled
381 && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)))
383 if (virtio_pci_enable_checksum_offload (vm, vif))
385 virtio_log_warning (vif, "checksum offload is not enabled");
389 vif->csum_offload_enabled = 1;
390 vif->gso_enabled = 0;
391 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
393 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
398 if (virtio_pci_disable_offload (vm, vif))
400 virtio_log_warning (vif, "offloads are not disabled");
404 vif->csum_offload_enabled = 0;
405 vif->gso_enabled = 0;
406 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
407 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
416 virtio_pci_enable_multiqueue (vlib_main_t * vm, virtio_if_t * vif,
419 virtio_ctrl_msg_t mq_hdr;
420 virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR;
422 mq_hdr.ctrl.class = VIRTIO_NET_CTRL_MQ;
423 mq_hdr.ctrl.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
424 mq_hdr.status = VIRTIO_NET_ERR;
425 clib_memcpy (mq_hdr.data, &num_queues, sizeof (num_queues));
427 status = virtio_pci_send_ctrl_msg (vm, vif, &mq_hdr, sizeof (num_queues));
428 virtio_log_debug (vif, "multi-queue enable %u queues", num_queues);
433 virtio_pci_queue_size_valid (u16 qsz)
435 if (qsz < 64 || qsz > 4096)
443 virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif,
446 clib_error_t *error = 0;
448 virtio_vring_t *vring;
453 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
454 if (!virtio_pci_queue_size_valid (queue_size))
455 clib_warning ("queue size is not valid");
457 if (!is_pow2 (queue_size))
458 return clib_error_return (0, "ring size must be power of 2");
460 if (queue_size > 32768)
461 return clib_error_return (0, "ring size must be 32768 or lower");
466 vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES);
467 vring = vec_elt_at_index (vif->cxq_vring, 0);
468 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
469 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
471 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
474 return vlib_physmem_last_error (vm);
475 clib_memset (ptr, 0, i);
476 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
477 vring->desc = vr.desc;
478 vring->avail = vr.avail;
479 vring->used = vr.used;
480 vring->queue_id = queue_num;
481 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
483 ASSERT (vring->buffers == 0);
485 vring->size = queue_size;
486 virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num,
488 vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr);
495 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num)
497 vlib_thread_main_t *vtm = vlib_get_thread_main ();
498 clib_error_t *error = 0;
500 virtio_vring_t *vring;
505 queue_size = vif->virtio_pci_func->get_queue_size (vm, vif, queue_num);
506 if (!virtio_pci_queue_size_valid (queue_size))
507 clib_warning ("queue size is not valid");
509 if (!is_pow2 (queue_size))
510 return clib_error_return (0, "ring size must be power of 2");
512 if (queue_size > 32768)
513 return clib_error_return (0, "ring size must be 32768 or lower");
520 vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num),
521 CLIB_CACHE_LINE_BYTES);
522 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num));
523 if (vif->max_queue_pairs < vtm->n_vlib_mains)
524 clib_spinlock_init (&vring->lockp);
528 vec_validate_aligned (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num),
529 CLIB_CACHE_LINE_BYTES);
530 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num));
532 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
533 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
535 vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN,
538 return vlib_physmem_last_error (vm);
539 clib_memset (ptr, 0, i);
540 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
541 vring->desc = vr.desc;
542 vring->avail = vr.avail;
543 vring->used = vr.used;
544 vring->queue_id = queue_num;
545 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
546 vring->flow_table = 0;
548 ASSERT (vring->buffers == 0);
549 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
552 virtio_log_debug (vif, "tx-queue: number %u, size %u", queue_num,
554 clib_memset_u32 (vring->buffers, ~0, queue_size);
558 virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num,
561 vring->size = queue_size;
562 if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr))
563 return clib_error_return (0, "error in queue address setup");
570 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
574 * if features are not requested
575 * default: all supported features
577 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)
578 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM)
579 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
580 | VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
581 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
582 | VIRTIO_FEATURE (VIRTIO_NET_F_GSO)
583 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4)
584 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)
585 | VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)
586 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4)
587 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)
588 | VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO)
589 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
590 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
591 | VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)
592 | VIRTIO_FEATURE (VIRTIO_NET_F_MQ)
593 | VIRTIO_FEATURE (VIRTIO_F_NOTIFY_ON_EMPTY)
594 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
595 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
598 supported_features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
600 if (req_features == 0)
602 req_features = supported_features;
605 vif->features = req_features & vif->remote_features & supported_features;
607 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
610 mtu = vif->virtio_pci_func->get_mtu (vm, vif);
613 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
616 vif->virtio_pci_func->set_driver_features (vm, vif, vif->features);
617 vif->features = vif->virtio_pci_func->get_driver_features (vm, vif);
621 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
623 vif->remote_features = vif->virtio_pci_func->get_device_features (vm, vif);
627 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
634 status = vif->virtio_pci_func->device_reset (vm, vif);
637 * Set the Acknowledge status bit
639 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
642 * Set the Driver status bit
644 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
647 * Read the status and verify it
649 status = vif->virtio_pci_func->get_status (vm, vif);
650 if ((status & VIRTIO_CONFIG_STATUS_ACK)
651 && (status & VIRTIO_CONFIG_STATUS_DRIVER))
652 vif->status = status;
660 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif, void **bar)
662 clib_error_t *error = 0;
663 virtio_pci_cap_t cap;
664 u8 pos, common_cfg = 0, notify = 0, dev_cfg = 0, isr = 0, pci_cfg = 0;
665 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
667 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
669 virtio_log_error (vif, "error in reading capabilty list position");
670 return clib_error_return (error,
671 "error in reading capabilty list position");
676 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
679 virtio_log_error (vif, "%s [%2x]",
680 "error in reading the capability at", pos);
681 return clib_error_return (error,
682 "error in reading the capability at [%2x]",
686 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
688 u16 flags, table_size, table_size_mask = 0x07FF;
691 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
693 return clib_error_return (error,
694 "error in reading the capability at [%2x]",
697 table_size = flags & table_size_mask;
698 virtio_log_debug (vif, "flags:0x%x %s 0x%x", flags,
699 "msix interrupt vector table-size", table_size);
701 if (flags & PCI_MSIX_ENABLE)
703 virtio_log_debug (vif, "msix interrupt enabled");
704 vif->msix_enabled = VIRTIO_MSIX_ENABLED;
705 vif->msix_table_size = table_size;
709 virtio_log_debug (vif, "msix interrupt disabled");
710 vif->msix_enabled = VIRTIO_MSIX_DISABLED;
711 vif->msix_table_size = 0;
715 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
717 virtio_log_debug (vif, "[%2x] %s %2x ", pos,
718 "skipping non VNDR cap id:", cap.cap_vndr);
722 virtio_log_debug (vif,
723 "[%4x] cfg type: %u, bar: %u, offset: %04x, len: %u",
724 pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
726 if (cap.bar >= 0 && cap.bar <= 5)
728 vif->bar = bar[cap.bar];
729 vif->bar_id = cap.bar;
732 return clib_error_return (error, "invalid bar %u", cap.bar);
734 switch (cap.cfg_type)
736 case VIRTIO_PCI_CAP_COMMON_CFG:
737 vif->common_offset = cap.offset;
740 case VIRTIO_PCI_CAP_NOTIFY_CFG:
742 vlib_pci_read_write_config (vm, h, VLIB_READ,
744 &vif->notify_off_multiplier,
746 (vif->notify_off_multiplier))))
748 virtio_log_error (vif, "notify off multiplier is not given");
752 virtio_log_debug (vif, "notify off multiplier is %u",
753 vif->notify_off_multiplier);
754 vif->notify_offset = cap.offset;
758 case VIRTIO_PCI_CAP_DEVICE_CFG:
759 vif->device_offset = cap.offset;
762 case VIRTIO_PCI_CAP_ISR_CFG:
763 vif->isr_offset = cap.offset;
766 case VIRTIO_PCI_CAP_PCI_CFG:
775 if (common_cfg == 0 || notify == 0 || dev_cfg == 0 || isr == 0)
777 vif->virtio_pci_func = &virtio_pci_legacy_func;
778 virtio_log_debug (vif, "legacy virtio pci device found");
783 vif->virtio_pci_func = &virtio_pci_modern_func;
786 virtio_log_debug (vif, "modern virtio pci device found");
788 virtio_log_debug (vif, "transitional virtio pci device found");
792 static clib_error_t *
793 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
794 virtio_pci_create_if_args_t * args, void **bar)
796 clib_error_t *error = 0;
797 vlib_thread_main_t *vtm = vlib_get_thread_main ();
800 if ((error = virtio_pci_read_caps (vm, vif, bar)))
802 args->rv = VNET_API_ERROR_UNSUPPORTED;
803 virtio_log_error (vif, "Device is not supported");
804 return clib_error_return (error, "Device is not supported");
807 if (virtio_pci_reset_device (vm, vif) < 0)
809 args->rv = VNET_API_ERROR_INIT_FAILED;
810 virtio_log_error (vif, "Failed to reset the device");
811 return clib_error_return (error, "Failed to reset the device");
814 * read device features and negotiate (user) requested features
816 virtio_pci_read_device_feature (vm, vif);
817 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
820 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
821 "support VIRTIO_RING_F_INDIRECT_DESC features");
823 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
825 virtio_log_warning (vif, "error encountered: vhost-net backend doesn't "
826 "support VIRTIO_NET_F_MRG_RXBUF features");
828 virtio_negotiate_features (vm, vif, args->features);
831 * After FEATURE_OK, driver should not accept new feature bits
833 vif->virtio_pci_func->set_status (vm, vif,
834 VIRTIO_CONFIG_STATUS_FEATURES_OK);
835 status = vif->virtio_pci_func->get_status (vm, vif);
836 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
838 args->rv = VNET_API_ERROR_UNSUPPORTED;
839 virtio_log_error (vif,
840 "error encountered: Device doesn't support requested features");
841 return clib_error_return (error,
842 "Device doesn't support requested features");
844 vif->status = status;
847 * get or set the mac address
849 if (virtio_pci_get_mac (vm, vif))
851 f64 now = vlib_time_now (vm);
853 rnd = (u32) (now * 1e6);
854 rnd = random_u32 (&rnd);
856 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
857 vif->mac_addr[0] = 2;
858 vif->mac_addr[1] = 0xfe;
859 virtio_pci_set_mac (vm, vif);
862 virtio_set_net_hdr_size (vif);
865 * Initialize the virtqueues
867 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
869 args->rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
873 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
875 if (vif->msix_table_size <= vif->max_queue_pairs)
877 virtio_log_error (vif,
878 "error MSIX lines (%u) <= Number of RXQs (%u)",
879 vif->msix_table_size, vif->max_queue_pairs);
880 return clib_error_return (error,
881 "error MSIX lines (%u) <= Number of RXQs (%u)",
882 vif->msix_table_size,
883 vif->max_queue_pairs);
887 for (int i = 0; i < vif->max_queue_pairs; i++)
889 if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i))))
891 args->rv = VNET_API_ERROR_INIT_FAILED;
892 virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue",
893 RX_QUEUE (i), "initialization");
895 clib_error_return (error, "%s (%u) %s", "error in rxq-queue",
896 RX_QUEUE (i), "initialization");
904 if (i >= vtm->n_vlib_mains)
907 * There is 1:1 mapping between tx queue and vpp worker thread.
908 * tx queue 0 is bind with thread index 0, tx queue 1 on thread
910 * Multiple worker threads can poll same tx queue when number of
911 * workers are more than tx queues. In this case, 1:N mapping
912 * between tx queue and vpp worker thread.
914 virtio_log_debug (vif, "%s %u, %s", "tx-queue: number",
916 "no VPP worker thread is available");
920 if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i))))
922 args->rv = VNET_API_ERROR_INIT_FAILED;
923 virtio_log_error (vif, "%s (%u) %s", "error in txq-queue",
924 TX_QUEUE (i), "initialization");
926 clib_error_return (error, "%s (%u) %s", "error in txq-queue",
927 TX_QUEUE (i), "initialization");
936 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
939 virtio_pci_control_vring_init (vm, vif, vif->max_queue_pairs * 2)))
941 virtio_log_warning (vif, "%s (%u) %s", "error in control-queue",
942 vif->max_queue_pairs * 2, "initialization");
943 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
944 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MQ);
949 virtio_log_debug (vif, "control queue is not available");
950 vif->cxq_vring = NULL;
954 * set the msix interrupts
956 if (vif->msix_enabled == VIRTIO_MSIX_ENABLED)
959 if (vif->virtio_pci_func->set_config_irq (vm, vif, 0) ==
960 VIRTIO_MSI_NO_VECTOR)
962 virtio_log_warning (vif, "config vector 0 is not set");
966 virtio_log_debug (vif, "config msix vector is set at 0");
968 for (i = 0, j = 1; i < vif->max_queue_pairs; i++, j++)
970 if (vif->virtio_pci_func->set_queue_irq (vm, vif, j,
972 VIRTIO_MSI_NO_VECTOR)
974 virtio_log_warning (vif, "queue (%u) vector is not set at %u",
979 virtio_log_debug (vif, "%s (%u) %s %u", "queue",
980 RX_QUEUE (i), "msix vector is set at", j);
986 * set the driver status OK
988 vif->virtio_pci_func->set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
989 vif->status = vif->virtio_pci_func->get_status (vm, vif);
995 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
997 vnet_main_t *vnm = vnet_get_main ();
998 virtio_main_t *vim = &virtio_main;
1000 vlib_pci_dev_handle_t h;
1001 clib_error_t *error = 0;
1002 u32 interrupt_count = 0;
1005 pool_foreach (vif, vim->interfaces, ({
1006 if (vif->pci_addr.as_u32 == args->addr)
1008 args->rv = VNET_API_ERROR_ADDRESS_IN_USE;
1010 clib_error_return (error, "PCI address in use");
1011 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1012 format_vlib_pci_addr, &args->addr,
1013 " PCI address in use");
1019 pool_get (vim->interfaces, vif);
1020 vif->dev_instance = vif - vim->interfaces;
1021 vif->per_interface_next_index = ~0;
1022 vif->pci_addr.as_u32 = args->addr;
1025 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
1026 virtio_pci_device_ids, &h)))
1028 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1030 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
1032 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
1033 format_vlib_pci_addr, &vif->pci_addr,
1034 "error encountered on pci device open");
1035 pool_put (vim->interfaces, vif);
1038 vif->pci_dev_handle = h;
1039 vlib_pci_set_private_data (vm, h, vif->dev_instance);
1040 vif->numa_node = vlib_pci_get_numa_node (vm, h);
1041 vif->type = VIRTIO_IF_TYPE_PCI;
1043 if ((error = vlib_pci_bus_master_enable (vm, h)))
1045 virtio_log_error (vif, "error encountered on pci bus master enable");
1050 for (u32 i = 0; i <= 5; i++)
1053 if ((error = vlib_pci_map_region (vm, h, i, &bar[i])))
1055 virtio_log_debug (vif, "no pci map region for bar %u", i);
1059 virtio_log_debug (vif, "pci map region for bar %u at %p", i,
1064 if ((error = vlib_pci_io_region (vm, h, 0)))
1066 virtio_log_error (vif, "error encountered on pci io region");
1070 interrupt_count = vlib_pci_get_num_msix_interrupts (vm, h);
1071 if (interrupt_count > 1)
1073 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
1074 &virtio_pci_irq_config_handler)))
1076 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1077 virtio_log_error (vif,
1078 "error encountered on pci register msix handler 0");
1083 vlib_pci_register_msix_handler (vm, h, 1, (interrupt_count - 1),
1084 &virtio_pci_irq_queue_handler)))
1086 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1087 virtio_log_error (vif,
1088 "error encountered on pci register msix handler 1");
1092 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, interrupt_count)))
1094 virtio_log_error (vif, "error encountered on pci enable msix irq");
1097 vif->support_int_mode = 1;
1098 virtio_log_debug (vif, "device supports msix interrupts");
1100 else if (interrupt_count == 1)
1103 * if msix table-size is 1, fall back to intX.
1106 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler)))
1108 virtio_log_error (vif,
1109 "error encountered on pci register interrupt handler");
1112 vif->support_int_mode = 1;
1113 virtio_log_debug (vif, "pci register interrupt handler");
1118 * WARN: intX is showing some weird behaviour.
1119 * Please don't use interrupt mode with UIO driver.
1121 vif->support_int_mode = 0;
1122 virtio_log_debug (vif, "driver is configured in poll mode only");
1125 if ((error = vlib_pci_intr_enable (vm, h)))
1127 virtio_log_error (vif, "error encountered on pci interrupt enable");
1131 if ((error = virtio_pci_device_init (vm, vif, args, bar)))
1133 virtio_log_error (vif, "error encountered on device init");
1137 /* create interface */
1138 error = ethernet_register_interface (vnm, virtio_device_class.index,
1139 vif->dev_instance, vif->mac_addr,
1141 virtio_pci_flag_change);
1145 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
1146 virtio_log_error (vif,
1147 "error encountered on ethernet register interface");
1151 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
1152 vif->sw_if_index = sw->sw_if_index;
1153 args->sw_if_index = sw->sw_if_index;
1155 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
1156 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
1158 if (args->virtio_flags & VIRTIO_FLAG_BUFFERING)
1160 error = virtio_set_packet_buffering (vif, args->buffering_size);
1163 args->rv = VNET_API_ERROR_INIT_FAILED;
1164 virtio_log_error (vif,
1165 "error encountered during packet buffering init");
1170 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
1171 virtio_input_node.index);
1173 vec_foreach_index (i, vif->rxq_vrings)
1175 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
1176 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
1177 /* Set default rx mode to POLLING */
1178 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
1179 VNET_HW_INTERFACE_RX_MODE_POLLING);
1181 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
1183 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
1184 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
1185 VNET_HW_INTERFACE_FLAG_LINK_UP);
1188 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1190 virtio_pci_offloads (vm, vif, args->gso_enabled,
1191 args->checksum_offload_enabled);
1193 if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) &&
1194 (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ)))
1196 if (virtio_pci_enable_multiqueue (vm, vif, vif->max_queue_pairs))
1197 virtio_log_warning (vif, "multiqueue is not set");
1202 virtio_pci_delete_if (vm, vif);
1204 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
1205 args->error = error;
1209 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
1211 vnet_main_t *vnm = vnet_get_main ();
1212 virtio_main_t *vim = &virtio_main;
1215 if (vif->type != VIRTIO_IF_TYPE_PCI)
1216 return VNET_API_ERROR_INVALID_INTERFACE;
1218 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
1220 for (i = 0; i < vif->max_queue_pairs; i++)
1222 vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i));
1223 vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i));
1226 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ))
1227 vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2);
1229 if (vif->virtio_pci_func)
1230 vif->virtio_pci_func->device_reset (vm, vif);
1232 if (vif->hw_if_index)
1234 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
1235 vec_foreach_index (i, vif->rxq_vrings)
1237 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
1239 ethernet_delete_interface (vnm, vif->hw_if_index);
1242 vlib_pci_device_close (vm, vif->pci_dev_handle);
1244 vec_foreach_index (i, vif->rxq_vrings)
1246 virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i);
1247 if (vring->kick_fd != -1)
1248 close (vring->kick_fd);
1251 virtio_free_rx_buffers (vm, vring);
1253 vec_free (vring->buffers);
1254 vlib_physmem_free (vm, vring->desc);
1257 vec_foreach_index (i, vif->txq_vrings)
1259 virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i);
1260 if (vring->kick_fd != -1)
1261 close (vring->kick_fd);
1264 virtio_free_used_desc (vm, vring);
1266 vec_free (vring->buffers);
1267 clib_spinlock_free (&vring->lockp);
1268 vlib_physmem_free (vm, vring->desc);
1271 if (vif->cxq_vring != NULL)
1273 u16 last = vif->cxq_vring->last_used_idx;
1274 u16 n_left = vif->cxq_vring->used->idx - last;
1281 vif->cxq_vring->last_used_idx = last;
1282 vlib_physmem_free (vm, vif->cxq_vring->desc);
1285 vec_free (vif->rxq_vrings);
1286 vec_free (vif->txq_vrings);
1287 vec_free (vif->cxq_vring);
1289 clib_error_free (vif->error);
1290 memset (vif, 0, sizeof (*vif));
1291 pool_put (vim->interfaces, vif);
1297 virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
1299 int checksum_offload_enabled,
1300 int offloads_disabled)
1302 if (vif->type != VIRTIO_IF_TYPE_PCI)
1303 return VNET_API_ERROR_INVALID_INTERFACE;
1306 virtio_pci_offloads (vm, vif, 1, 0);
1307 else if (checksum_offload_enabled)
1308 virtio_pci_offloads (vm, vif, 0, 1);
1309 else if (offloads_disabled)
1310 virtio_pci_offloads (vm, vif, 0, 0);
1316 * fd.io coding-style-patch-verification: ON
1319 * eval: (c-set-style "gnu")