Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a qemu Virtio device

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how to create a qemu Virtio device, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.

What is Virtio?

The roadmap of virtual mechanization of qemu devices can be summarized as full virtualization-> paravirtualization (which can be divided into user space and kernel space)-> device penetration (which can be divided into full device penetration and single virtualization). All of these evolutions are designed to improve the performance of virtual devices.

Virtio is a semi-virtualized solution, which is a front-end and front-end architecture. The virtual machine needs to install a specific virtio device driver as the front-end, the simulated device as the back-end, and the back-end can be simulated in user space or kernel space. Simulation in kernel space is the implementation of vhost, such as DPDK,SPDK and so on.

The advantage of Virtio agent is that the front and back ends of specific device drivers communicate through virtqueue, and virtqueue is a piece of memory shared by front and back ends. Thus the communication between the front and back ends can be done by reading and writing the shared memory. Compared with the full virtual machine, each read and write virtual device needs to do vm exit and vm entry, so the cost of communication becomes smaller.

The inconvenience brought by Virtio is the need to develop specific virtio device drivers, and virtual machines need to install device drivers.

II. Virtio balloon device creation process

This paper analyzes the creation process of the virtio balloon device, from the qemu specified parameter-device xxx device to the final instantiation process of the device.

The implementation of device object in qemu adopts QOM model, which simulates encapsulation, inheritance and polymorphism in object-oriented language. Following this framework, it is very convenient to develop new devices. The following figure shows the inheritance relationship between classes. What's special here is that every virtio device has an accompanying PCI device, called a virtio PCI proxy device. The main function of the device is to realize the functions related to the device bus, while the device class only needs to pay attention to the realization of the specific functions of the device.

The process of creating a virtio balloon device is as follows:

1. In the main function, it iterates through the specified-device parameter, and then calls device_init_func to initialize the device.

two。 Device_init_func- > qdev_device_add-> object_new-> object_initialize_with_type is called in turn. The main initialization work is done in object_initialize_with_type.

3. Type_initialize is first called to complete the initialization of the class. In the initialization of the class, the realize callback function of the class is set to virtio_balloon_pci_realize. This function is called when the class object is instantiated.

4. Object_init_with_type is then called to initialize the class object. The instance_init is executed recursively from the parent object. The instance_init function of TYPE_DEVICE is device_initfn. This function call executes object_property_add_bool with three additional properties. Where the realized property is called when the object is actually created. Its set callback function is set to device_set_realized. This function is called when any device is created.

5. After the device class and object instance are initialized, they will return to the qdev_device_add function, and then the specific device implementation process will be carried out.

6. Call object_property_set_bool to set the realized property to true, which calls the callback function device_set_realized that you just set.

7. The realize function of DeviceClass, virtio_pci_dc_realize, is called first. This function is set in virtio_pci_class_init.

8. The realize function of the parent class calls the realize function of the subclass once. Then the realize function of PCIDeviceClass, pci_qdev_realize, is called.

9. Then call the realize function of VirtioPCIClass and VirtioBalloonPCIClass in turn. As a result, the creation of VirtioBalloonPCI device is realized. Finally, you need to create a VirtioBalloon device and mount it to the PCI bus.

10. That is, call the realize function of VirtioDeviceClass and VirtioBalloonDeviceClass in turn. The specific implementation of the VirtioBalloon device is in the function virtio_balloon_device_realize.

The whole process is shown in the following figure, and the call flow between classes is shown in the red line in the class inheritance diagram.

In virtio_balloon_device_realize, calling virtio_add_queue adds three virtqueue to the device. The callback handler for two of them is virtio_ballon_handle_output, and the other is virtio_balloon_receive_stats. Thus, the input and output channel of the slave device and the callback processing function called when the input and output information is received are constructed.

At this point, the device is created, but the last step is to mount the device to the virtio bus. This step is implemented in the function virtio_device_realize. After this function calls vdc- > realize to create a specific device, it calls virtio_bus_device_plugged, which is used to plug the virtio device into the virtio bus.

The above is how to create qemu Virtio devices. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report