Full analysis of QEMU source code - virtio (4)

Continuing from the previous article:

References to this article:

"Interesting TalkLinux Operating System" - Liu Chao,Geek Time

"QEMU/KVM" source code analysis and application - Li Qiang, Machinery Industry Press

Thanks!

In the last chapter, we started to formally explain the implementation mechanism and details of virtio based on the code. First, the definition of the virtio PCI agent device type is given, as well as some virtio devices under it, such as virtio balloon PCI device, virtio scsi PCI device and virtio crypto PCI device. And the location of the virtio device in the system's device tree is given, as shown in the following figure:

As can be seen from the above figure, all virtio devices have a common parent class TYPE_VIRTIO_DEVICE. For example:

  • virtual balloon device

The virtual balloon device is defined as follows (in hw/virtio/virtio-balloon.c):

static const TypeInfo virtio_balloon_info = {
    .name = TYPE_VIRTIO_BALLOON,
    .parent = TYPE_VIRTIO_DEVICE,
    .instance_size = sizeof(VirtIOBalloon),
    .instance_init = virtio_balloon_instance_init,
    .class_init = virtio_balloon_class_init,
};

static void virtio_register_types(void)
{
    type_register_static(&virtio_balloon_info);
}

type_init(virtio_register_types)

Related:

  • virtio balloon PCI device

The definition of virtio balloon PCI device is in hw/virtio/virtio-balloon-pci.c, the code is as follows:

static const VirtioPCIDeviceTypeInfo virtio_balloon_pci_info = {
    .base_name             = TYPE_VIRTIO_BALLOON_PCI,
    .generic_name          = "virtio-balloon-pci",
    .transitional_name     = "virtio-balloon-pci-transitional",
    .non_transitional_name = "virtio-balloon-pci-non-transitional",
    .instance_size = sizeof(VirtIOBalloonPCI),
    .instance_init = virtio_balloon_pci_instance_init,
    .class_init    = virtio_balloon_pci_class_init,
};
 
static void virtio_balloon_pci_register(void)
{
    virtio_pci_types_register(&virtio_balloon_pci_info);
}
 
type_init(virtio_balloon_pci_register)
  • virtual scsi device

The virtual scsi device is defined as follows (in hw/virtio/virtio-scsi.c):

static const TypeInfo virtio_scsi_info = {
    .name = TYPE_VIRTIO_SCSI,
    .parent = TYPE_VIRTIO_SCSI_COMMON,
    .instance_size = sizeof(VirtIOSCSI),
    .class_init = virtio_scsi_class_init,
    .interfaces = (InterfaceInfo[]) {
        { TYPE_HOTPLUG_HANDLER },
        { }
    }
};

static void virtio_register_types(void)
{
    type_register_static(&virtio_scsi_common_info);
    type_register_static(&virtio_scsi_info);
}

type_init(virtio_register_types)

Related:

  • virtio scsi PCI device

The definition of virtio scsi PCI device is in hw/virtio/virtio-scsi-pci.c, the code is as follows:

static const VirtioPCIDeviceTypeInfo virtio_scsi_pci_info = {
    .base_name              = TYPE_VIRTIO_SCSI_PCI,
    .generic_name           = "virtio-scsi-pci",
    .transitional_name      = "virtio-scsi-pci-transitional",
    .non_transitional_name  = "virtio-scsi-pci-non-transitional",
    .instance_size = sizeof(VirtIOSCSIPCI),
    .instance_init = virtio_scsi_pci_instance_init,
    .class_init    = virtio_scsi_pci_class_init,
};
 
static void virtio_scsi_pci_register(void)
{
    virtio_pci_types_register(&virtio_scsi_pci_info);
}
 
type_init(virtio_scsi_pci_register)
  • virtual crypto device

The definition of virtual crypto device is as follows (in hw/virtio/virtio-crypto.c):

static const TypeInfo virtio_crypto_info = {
    .name = TYPE_VIRTIO_CRYPTO,
    .parent = TYPE_VIRTIO_DEVICE,
    .instance_size = sizeof(VirtIOCrypto),
    .instance_init = virtio_crypto_instance_init,
    .class_init = virtio_crypto_class_init,
};

static void virtio_register_types(void)
{
    type_register_static(&virtio_crypto_info);
}

type_init(virtio_register_types)

Related:

  • virtio crypto PCI device

The definition of virtio crypto PCI device is in hw/virtio/virtio-crypto-pci.c, the code is as follows:

static const VirtioPCIDeviceTypeInfo virtio_crypto_pci_info = {
    .generic_name  = TYPE_VIRTIO_CRYPTO_PCI,
    .instance_size = sizeof(VirtIOCryptoPCI),
    .instance_init = virtio_crypto_initfn,
    .class_init    = virtio_crypto_pci_class_init,
};
 
static void virtio_crypto_pci_register_types(void)
{
    virtio_pci_types_register(&virtio_crypto_pci_info);
}
type_init(virtio_crypto_pci_register_types)

Take the virtio balloon device as an example. Post the source code again, in hw/virtio/virtio-balloon.c, as follows:

static const TypeInfo virtio_balloon_info = {
    .name = TYPE_VIRTIO_BALLOON,
    .parent = TYPE_VIRTIO_DEVICE,
    .instance_size = sizeof(VirtIOBalloon),
    .instance_init = virtio_balloon_instance_init,
    .class_init = virtio_balloon_class_init,
};

static void virtio_register_types(void)
{
    type_register_static(&virtio_balloon_info);
}

type_init(virtio_register_types)

The instance object of the virtio balloon device is VirtIOBalloon. The relationship between the specific virtio device, virtio PCI proxy device, and virtio public device is as shown in the figure below:

The above figure shows several data structures corresponding to the virtio balloon device and their relationships. VirtIOBalloonPCI is an instance object of the virtio balloon PCI proxy device, which includes two parts: one is VirtIOPCIProxy, which is the general structure of the virtio PCI proxy device, which stores the relevant members of the specific virtio PCI proxy device; the other is VirtIOBalloon, this structure Stored in is the relevant data of the virtio balloon device. Its first member is VirtIODevice, which is the instance object of the virtio public device. The remaining members of VirtIOBalloon are data related to the virtio balloon device.

If you want to know what happens next, let’s look at the breakdown in the next chapter.

Guess you like

Origin blog.csdn.net/phmatthaus/article/details/134952445