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>
18 #include <linux/virtio_net.h>
19 #include <linux/virtio_ring.h>
20 #include <linux/vhost.h>
21 #include <sys/eventfd.h>
22 #if defined(__x86_64__)
26 #include <vppinfra/types.h>
27 #include <vlib/vlib.h>
28 #include <vlib/pci/pci.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vpp/app/version.h>
31 #include <vnet/ip/ip4_packet.h>
32 #include <vnet/ip/ip6_packet.h>
33 #include <vnet/devices/virtio/virtio.h>
34 #include <vnet/devices/virtio/pci.h>
36 #define PCI_VENDOR_ID_VIRTIO 0x1af4
37 #define PCI_DEVICE_ID_VIRTIO_NIC 0x1000
38 /* Doesn't support modern device */
39 #define PCI_DEVICE_ID_VIRTIO_NIC_MODERN 0x1041
41 #define PCI_CAPABILITY_LIST 0x34
42 #define PCI_CAP_ID_VNDR 0x09
43 #define PCI_CAP_ID_MSIX 0x11
45 #define PCI_MSIX_ENABLE 0x8000
47 static u32 msix_enabled = 0;
49 #define PCI_CONFIG_SIZE ((msix_enabled == VIRTIO_MSIX_ENABLED) ? \
52 static pci_device_id_t virtio_pci_device_ids[] = {
54 .vendor_id = PCI_VENDOR_ID_VIRTIO,
55 .device_id = PCI_DEVICE_ID_VIRTIO_NIC},
57 .vendor_id = PCI_VENDOR_ID_VIRTIO,
58 .device_id = PCI_DEVICE_ID_VIRTIO_NIC_MODERN},
63 virtio_pci_legacy_read_config (vlib_main_t * vm, virtio_if_t * vif, void *dst,
67 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
74 vlib_pci_read_io_u32 (vm, h, PCI_CONFIG_SIZE + addr, dst);
79 vlib_pci_read_io_u16 (vm, h, PCI_CONFIG_SIZE + addr, dst);
84 vlib_pci_read_io_u8 (vm, h, PCI_CONFIG_SIZE + addr, dst);
86 dst = (u8 *) dst + size;
93 virtio_pci_legacy_write_config (vlib_main_t * vm, virtio_if_t * vif,
94 void *src, int len, u32 addr)
97 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
104 vlib_pci_write_io_u32 (vm, h, PCI_CONFIG_SIZE + addr, src);
109 vlib_pci_write_io_u16 (vm, h, PCI_CONFIG_SIZE + addr, src);
114 vlib_pci_write_io_u8 (vm, h, PCI_CONFIG_SIZE + addr, src);
116 src = (u8 *) src + size;
123 virtio_pci_legacy_get_features (vlib_main_t * vm, virtio_if_t * vif)
126 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_HOST_FEATURES,
132 virtio_pci_legacy_set_features (vlib_main_t * vm, virtio_if_t * vif,
135 if ((features >> 32) != 0)
137 clib_warning ("only 32 bit features are allowed for legacy virtio!");
139 u32 feature = 0, guest_features = (u32) features;
140 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
142 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_GUEST_FEATURES,
148 virtio_pci_legacy_get_status (vlib_main_t * vm, virtio_if_t * vif)
151 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
156 virtio_pci_legacy_set_status (vlib_main_t * vm, virtio_if_t * vif, u8 status)
158 if (status != VIRTIO_CONFIG_STATUS_RESET)
159 status |= virtio_pci_legacy_get_status (vm, vif);
160 vlib_pci_write_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_STATUS, &status);
164 virtio_pci_legacy_reset (vlib_main_t * vm, virtio_if_t * vif)
166 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_RESET);
167 return virtio_pci_legacy_get_status (vm, vif);
171 virtio_pci_legacy_get_isr (vlib_main_t * vm, virtio_if_t * vif)
174 vlib_pci_read_io_u8 (vm, vif->pci_dev_handle, VIRTIO_PCI_ISR, &isr);
179 virtio_pci_legacy_get_queue_num (vlib_main_t * vm, virtio_if_t * vif,
183 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
185 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NUM,
192 virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif,
193 u16 queue_id, void *p)
195 u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
196 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
198 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN,
203 virtio_pci_legacy_del_queue (vlib_main_t * vm, virtio_if_t * vif,
207 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
209 vlib_pci_write_io_u32 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_PFN, &src);
213 virtio_pci_legacy_notify_queue (vlib_main_t * vm, virtio_if_t * vif,
216 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_NOTIFY,
220 /* Enable one vector (0) for Link State Intrerrupt */
222 virtio_pci_legacy_set_config_irq (vlib_main_t * vm, virtio_if_t * vif,
225 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
227 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_CONFIG_VECTOR,
233 virtio_pci_legacy_set_queue_irq (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
236 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL,
238 vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
240 vlib_pci_read_io_u16 (vm, vif->pci_dev_handle, VIRTIO_MSI_QUEUE_VECTOR,
246 virtio_pci_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw,
252 static clib_error_t *
253 virtio_pci_get_max_virtqueue_pairs (vlib_main_t * vm, virtio_if_t * vif)
255 virtio_net_config_t config;
256 clib_error_t *error = 0;
257 u16 max_queue_pairs = 1;
259 if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MQ))
261 virtio_pci_legacy_read_config (vm, vif, &config.max_virtqueue_pairs,
262 sizeof (config.max_virtqueue_pairs), 8);
263 max_queue_pairs = config.max_virtqueue_pairs;
266 if (max_queue_pairs < 1 || max_queue_pairs > 0x8000)
267 clib_error_return (error, "max queue pair is %x", max_queue_pairs);
269 vif->max_queue_pairs = max_queue_pairs;
274 virtio_pci_set_mac (vlib_main_t * vm, virtio_if_t * vif)
276 virtio_pci_legacy_write_config (vm, vif, vif->mac_addr,
277 sizeof (vif->mac_addr), 0);
281 virtio_pci_get_mac (vlib_main_t * vm, virtio_if_t * vif)
283 if (vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MAC))
285 virtio_pci_legacy_read_config (vm, vif, vif->mac_addr,
286 sizeof (vif->mac_addr), 0);
293 virtio_pci_is_link_up (vlib_main_t * vm, virtio_if_t * vif)
296 * Minimal driver: assumes link is up
299 if (vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_STATUS))
300 virtio_pci_legacy_read_config (vm, vif, &status, sizeof (status), /* mac */
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);
419 virtio_pci_legacy_read_config (vm, vif, mac, sizeof (mac), 0);
420 vlib_cli_output (vm, "mac %U", format_ethernet_address, mac);
421 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to status */
423 vlib_cli_output (vm, "link up/down status 0x%x", data_u16);
424 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16),
425 /* offset to max_virtqueue */ 8);
426 vlib_cli_output (vm, "num of virtqueue 0x%x", data_u16);
427 virtio_pci_legacy_read_config (vm, vif, &data_u16, sizeof (u16), /* offset to mtu */
429 vlib_cli_output (vm, "mtu 0x%x", data_u16);
431 u32 i = PCI_CONFIG_SIZE + 12, a = 4;
434 for (; i < 64; i += 4)
437 vlib_pci_read_io_u32 (vm, vif->pci_dev_handle, i, &data);
438 vlib_cli_output (vm, "0x%lx", data);
443 virtio_pci_queue_size_valid (u16 qsz)
445 if (qsz < 64 || qsz > 4096)
453 virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx)
455 clib_error_t *error = 0;
457 virtio_vring_t *vring;
462 queue_size = virtio_pci_legacy_get_queue_num (vm, vif, idx);
463 if (!virtio_pci_queue_size_valid (queue_size))
464 clib_warning ("queue size is not valid");
466 if (!is_pow2 (queue_size))
467 return clib_error_return (0, "ring size must be power of 2");
469 if (queue_size > 32768)
470 return clib_error_return (0, "ring size must be 32768 or lower");
475 vec_validate_aligned (vif->vrings, idx, CLIB_CACHE_LINE_BYTES);
476 vring = vec_elt_at_index (vif->vrings, idx);
478 i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN);
479 i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN);
480 ptr = vlib_physmem_alloc_aligned (vm, i, VIRTIO_PCI_VRING_ALIGN);
482 vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN);
483 vring->desc = vr.desc;
484 vring->avail = vr.avail;
485 vring->used = vr.used;
486 vring->queue_id = idx;
487 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
489 ASSERT (vring->buffers == 0);
490 vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES);
491 ASSERT (vring->indirect_buffers == 0);
492 vec_validate_aligned (vring->indirect_buffers, queue_size,
493 CLIB_CACHE_LINE_BYTES);
499 if (n_alloc < queue_size)
501 vlib_buffer_alloc (vm, vring->indirect_buffers + n_alloc,
502 queue_size - n_alloc);
504 while (n_alloc != queue_size);
505 vif->tx_ring_sz = queue_size;
508 vif->rx_ring_sz = queue_size;
509 vring->size = queue_size;
511 virtio_pci_legacy_setup_queue (vm, vif, idx, ptr);
518 virtio_negotiate_features (vlib_main_t * vm, virtio_if_t * vif,
522 * if features are not requested
523 * default: all supported features
525 u64 supported_features = VIRTIO_FEATURE (VIRTIO_NET_F_MTU)
526 | VIRTIO_FEATURE (VIRTIO_NET_F_MAC)
527 | VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)
528 | VIRTIO_FEATURE (VIRTIO_NET_F_STATUS)
529 | VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)
530 | VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
532 if (req_features == 0)
534 req_features = supported_features;
537 vif->features = req_features & vif->remote_features & supported_features;
540 remote_features & vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MTU))
542 virtio_net_config_t config;
543 virtio_pci_legacy_read_config (vm, vif, &config.mtu,
544 sizeof (config.mtu), 10);
546 vif->features &= ~VIRTIO_FEATURE (VIRTIO_NET_F_MTU);
549 vif->features = virtio_pci_legacy_set_features (vm, vif, vif->features);
553 virtio_pci_read_device_feature (vlib_main_t * vm, virtio_if_t * vif)
555 vif->remote_features = virtio_pci_legacy_get_features (vm, vif);
559 virtio_pci_reset_device (vlib_main_t * vm, virtio_if_t * vif)
566 status = virtio_pci_legacy_reset (vm, vif);
569 * Set the Acknowledge status bit
571 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_ACK);
574 * Set the Driver status bit
576 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER);
579 * Read the status and verify it
581 status = virtio_pci_legacy_get_status (vm, vif);
583 ((status & VIRTIO_CONFIG_STATUS_ACK)
584 && (status & VIRTIO_CONFIG_STATUS_DRIVER)))
586 vif->status = status;
592 virtio_pci_read_caps (vlib_main_t * vm, virtio_if_t * vif)
594 clib_error_t *error = 0;
595 virtio_main_t *vim = &virtio_main;
596 struct virtio_pci_cap cap;
597 u8 pos, common_cfg = 0, notify_base = 0, dev_cfg = 0, isr = 0;
598 vlib_pci_dev_handle_t h = vif->pci_dev_handle;
600 if ((error = vlib_pci_read_config_u8 (vm, h, PCI_CAPABILITY_LIST, &pos)))
601 clib_error_return (error, "error in reading capabilty list position");
606 vlib_pci_read_write_config (vm, h, VLIB_READ, pos, &cap,
608 clib_error_return (error, "error in reading the capability at [%2x]",
611 if (cap.cap_vndr == PCI_CAP_ID_MSIX)
615 vlib_pci_read_write_config (vm, h, VLIB_READ, pos + 2, &flags,
617 clib_error_return (error,
618 "error in reading the capability at [%2x]",
621 if (flags & PCI_MSIX_ENABLE)
622 msix_enabled = VIRTIO_MSIX_ENABLED;
624 msix_enabled = VIRTIO_MSIX_DISABLED;
627 if (cap.cap_vndr != PCI_CAP_ID_VNDR)
629 virtio_log_debug (vim, vif, "[%2x] %s %2x ", pos,
630 "skipping non VNDR cap id:", cap.cap_vndr);
634 switch (cap.cfg_type)
636 case VIRTIO_PCI_CAP_COMMON_CFG:
639 case VIRTIO_PCI_CAP_NOTIFY_CFG:
642 case VIRTIO_PCI_CAP_DEVICE_CFG:
645 case VIRTIO_PCI_CAP_ISR_CFG:
653 if (common_cfg == 0 || notify_base == 0 || dev_cfg == 0 || isr == 0)
655 virtio_log_debug (vim, vif, "legacy virtio pci device found");
659 virtio_log_debug (vim, vif, "modern virtio pci device found");
663 static clib_error_t *
664 virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif,
665 virtio_pci_create_if_args_t * args)
667 clib_error_t *error = 0;
670 virtio_pci_read_caps (vm, vif);
672 if (virtio_pci_reset_device (vm, vif) < 0)
673 clib_error_return (error, "Failed to reset the device");
676 * read device features and negotiate (user) requested features
678 virtio_pci_read_device_feature (vm, vif);
679 virtio_negotiate_features (vm, vif, args->features);
682 * After FEATURE_OK, driver should not accept new feature bits
684 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_FEATURES_OK);
685 status = virtio_pci_legacy_get_status (vm, vif);
686 if (!(status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
687 clib_error_return (error, "Device doesn't support requested features");
689 vif->status = status;
691 if (virtio_pci_get_mac (vm, vif))
693 f64 now = vlib_time_now (vm);
695 rnd = (u32) (now * 1e6);
696 rnd = random_u32 (&rnd);
698 memcpy (vif->mac_addr + 2, &rnd, sizeof (rnd));
699 vif->mac_addr[0] = 2;
700 vif->mac_addr[1] = 0xfe;
701 virtio_pci_set_mac (vm, vif);
704 virtio_set_net_hdr_size (vif);
706 if ((error = virtio_pci_get_max_virtqueue_pairs (vm, vif)))
709 if ((error = virtio_pci_vring_init (vm, vif, 0)))
712 if ((error = virtio_pci_vring_init (vm, vif, 1)))
715 if (msix_enabled == VIRTIO_MSIX_ENABLED)
717 virtio_pci_legacy_set_config_irq (vm, vif, VIRTIO_MSI_NO_VECTOR);
718 virtio_pci_legacy_set_queue_irq (vm, vif, VIRTIO_MSI_NO_VECTOR, 0);
720 virtio_pci_legacy_set_status (vm, vif, VIRTIO_CONFIG_STATUS_DRIVER_OK);
721 vif->status = virtio_pci_legacy_get_status (vm, vif);
727 virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args)
729 vnet_main_t *vnm = vnet_get_main ();
730 virtio_main_t *vim = &virtio_main;
732 vlib_pci_dev_handle_t h;
733 clib_error_t *error = 0;
735 if (args->rxq_size == 0)
736 args->rxq_size = VIRTIO_NUM_RX_DESC;
737 if (args->txq_size == 0)
738 args->txq_size = VIRTIO_NUM_TX_DESC;
740 if (!virtio_pci_queue_size_valid (args->rxq_size) ||
741 !virtio_pci_queue_size_valid (args->txq_size))
743 args->rv = VNET_API_ERROR_INVALID_VALUE;
745 clib_error_return (error,
746 "queue size must be <= 4096, >= 64, "
747 "and multiples of 64");
748 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
749 format_vlib_pci_addr, &args->addr,
750 "queue size must be <= 4096, >= 64, and multiples of 64");
755 pool_foreach (vif, vim->interfaces, ({
756 if (vif->pci_addr.as_u32 == args->addr)
758 args->rv = VNET_API_ERROR_INVALID_VALUE;
760 clib_error_return (error, "PCI address in use");
761 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
762 format_vlib_pci_addr, &args->addr,
763 " PCI address in use");
769 pool_get (vim->interfaces, vif);
770 vif->dev_instance = vif - vim->interfaces;
771 vif->per_interface_next_index = ~0;
772 vif->pci_addr.as_u32 = args->addr;
774 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
776 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
777 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
782 vlib_pci_device_open (vm, (vlib_pci_addr_t *) & vif->pci_addr,
783 virtio_pci_device_ids, &h)))
785 pool_put (vim->interfaces, vif);
786 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
788 clib_error_return (error, "pci-addr %U", format_vlib_pci_addr,
790 vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s",
791 format_vlib_pci_addr, &vif->pci_addr,
792 "error encountered on pci device open");
795 vif->pci_dev_handle = h;
796 vlib_pci_set_private_data (vm, h, vif->dev_instance);
798 if ((error = vlib_pci_bus_master_enable (vm, h)))
800 virtio_log_error (vim, vif,
801 "error encountered on pci bus master enable");
805 if ((error = vlib_pci_io_region (vm, h, 0)))
807 virtio_log_error (vim, vif, "error encountered on pci io region");
811 if ((error = virtio_pci_device_init (vm, vif, args)))
813 virtio_log_error (vim, vif, "error encountered on device init");
817 if (msix_enabled == VIRTIO_MSIX_ENABLED)
819 if ((error = vlib_pci_register_msix_handler (vm, h, 0, 1,
820 &virtio_pci_irq_0_handler)))
822 virtio_log_error (vim, vif,
823 "error encountered on pci register msix handler 0");
826 if ((error = vlib_pci_register_msix_handler (vm, h, 1, 1,
827 &virtio_pci_irq_1_handler)))
829 virtio_log_error (vim, vif,
830 "error encountered on pci register msix handler 1");
834 if ((error = vlib_pci_enable_msix_irq (vm, h, 0, 2)))
836 virtio_log_error (vim, vif,
837 "error encountered on pci enable msix irq");
843 vlib_pci_register_intx_handler (vm, h, &virtio_pci_irq_handler);
846 if ((error = vlib_pci_intr_enable (vm, h)))
848 virtio_log_error (vim, vif,
849 "error encountered on pci interrupt enable");
853 vif->type = VIRTIO_IF_TYPE_PCI;
854 /* create interface */
855 error = ethernet_register_interface (vnm, virtio_device_class.index,
856 vif->dev_instance, vif->mac_addr,
858 virtio_pci_flag_change);
862 virtio_log_error (vim, vif,
863 "error encountered on ethernet register interface");
867 vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
868 vif->sw_if_index = sw->sw_if_index;
869 args->sw_if_index = sw->sw_if_index;
871 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
872 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
873 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
874 virtio_input_node.index);
875 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
877 if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP)
879 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
880 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
881 VNET_HW_INTERFACE_FLAG_LINK_UP);
884 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
888 virtio_pci_delete_if (vm, vif);
889 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
894 virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif)
896 vnet_main_t *vnm = vnet_get_main ();
897 virtio_main_t *vim = &virtio_main;
900 if (vif->type != VIRTIO_IF_TYPE_PCI)
901 return VNET_API_ERROR_INVALID_INTERFACE;
903 vlib_pci_intr_disable (vm, vif->pci_dev_handle);
905 virtio_pci_legacy_del_queue (vm, vif, 0);
906 virtio_pci_legacy_del_queue (vm, vif, 1);
908 virtio_pci_legacy_reset (vm, vif);
910 if (vif->hw_if_index)
912 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
913 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0);
914 ethernet_delete_interface (vnm, vif->hw_if_index);
917 vlib_pci_device_close (vm, vif->pci_dev_handle);
919 vec_foreach_index (i, vif->vrings)
921 virtio_vring_t *vring = vec_elt_at_index (vif->vrings, i);
922 if (vring->kick_fd != -1)
923 close (vring->kick_fd);
927 virtio_free_used_desc (vm, vring);
929 virtio_free_rx_buffers (vm, vring);
931 if (vring->queue_id % 2)
933 vlib_buffer_free_no_next (vm, vring->indirect_buffers, vring->size);
935 vec_free (vring->buffers);
936 vec_free (vring->indirect_buffers);
937 vlib_physmem_free (vm, vring->desc);
940 vec_free (vif->vrings);
944 if (vif->tap_fd != -1)
946 clib_error_free (vif->error);
947 memset (vif, 0, sizeof (*vif));
948 pool_put (vim->interfaces, vif);
954 * fd.io coding-style-patch-verification: ON
957 * eval: (c-set-style "gnu")