In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how the virtio driver interacts with the device. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Irtio is an optimization of io operation in guest kernel in a virtualized environment.
First of all, it should be noted that from the kernel point of view, virtio devices and their driver, like other devices and drivers, are ordinary devices with no particularity. That is, the kernel is not aware of this io optimization.
Virtio devices, at the system level, are pci devices. However, in order to improve the efficiency of io, the io operation is optimized.
The main solutions are:
1) when the virtio device outputs the data, the driver sends the data to the buffer queue (from the code driven by the virtio Nic, this operation has no memory copy, and the memory occupied by the data is directly added to the queue as buffer), and then writes the device register (vp_dev- > ioaddr + VIRTIO_PCI_QUEUE_NOTIFY) through the io instruction to notify the virtual machine system (kvm+qemu). When the virtual machine system captures the io instruction, it is notified to get the data output from the device from the buffer queue.
2) when data needs to be entered into the virtio device, the virtual machine system sends the data to the buffer queue and then triggers the device interrupt. After driver receives the interrupt, you can directly extract the data from the queue (from the code driven by the virtio Nic, the data in the queue no longer needs to be copied in memory, and the data in the queue is already sk_buff structure).
Judging from the above mechanism, virtio is not completely free of io operations. For example, when the device outputs data, the io operation is performed to notify the virtual machine system after the data has been sent to the buffer queue. However, this io operation does not write the output data to the device, but the fact that the data has been queued to the device.
The above is the general principle of virtio. Let's take a look at the design ideas of virtio.
It is roughly divided into the following four levels.
1. Buffer queue
Since virtio implements device input and output through buffer queues. So, wouldn't it be a waste if every device were to implement a buffer queue? Yes, virtio takes into account this common need, so it implements a common buffer queue module-virtio_ring (a ring queue). But what if one day the implementation of the buffer queue needs to be redesigned? With this in mind, a layer of abstraction-- struct virtqueue_ops-- is wrapped for the operation of the buffer queue. For each buffer queue implementation, you only need to provide a virtqueue_ops structure variable for the user to use. This decouples the queue operation from the queue implementation.
Struct virtqueue_ops
{
Int (* add_buf) (struct virtqueue * vq)
Struct scatterlist sg []
Unsigned int out_num
Unsigned int in_num
Void * data)
Void (* kick) (struct virtqueue * vq)
Void * (* get_buf) (struct virtqueue * vq, unsigned int * len)
Void (* disable_cb) (struct virtqueue * vq)
Bool (* enable_cb) (struct virtqueue * vq)
}
2. Pci layer
Every virtio device (for example, block device or network card) is a pci device at the system level. Between these devices, there are both common and different parts.
1) Common part: all these devices need to be connected to the corresponding buffer queue operation virtqueue_ops, need to apply for several buffer queues, and write data to the queue when performing io output; all need to execute pci_iomap to map the device configuration register interval to the memory interval; all need to set interrupt handling; and so on, they all need to read data from the queue and notify the virtual machine system that the data has been queued.
2) difference part: how to relate to the business in the system in the equipment. Each device is different. For example, the network card is a net_device in the kernel associated with the protocol stack system. At the same time, what data is written to the queue and the meaning of the data varies from device to device. The data in the queue, what it means and how to deal with it, varies from device to device.
If each virtio device fully implements its own functions, it will lead to waste.
In response to this phenomenon, virtio also designed the virtio_pci module to deal with the common parts of all virtio devices. In this way, all virtio devices, at the system level, are pci devices whose device drivers are virtio_pci.
However, virtio_pci can not completely drive any device. Therefore, when virtio_pci probe each device, it identifies which kind of pci device it is based on the subsystem vendor/device id of each virtio device, and then registers a virtio device with the kernel accordingly. Of course, before registering the virtio device, the virtio_pci driver has done a lot of common operations for the device. At the same time, it also provides an adaptation interface for various operations for the device, such as some common pci device operations and the operation of applying for buffer queues. These operations are adapted by virtio_config_ops structure variables.
3. Virtio driver
Here we talk about the virtio driver, which refers to the specific driver of each device. For example, a network card or block device. With the work of each module described above, the driver implementation of each device in virtio is relatively simple. Generally speaking, in addition to completing the unique functions of this device, the rest is basically buffer queue-related operations.
That is to apply for several queues and provide the corresponding callback function. If you have data to output, just send it to the queue. When the data comes from the queue, there will naturally be an interrupt, and during the interrupt processing, a callback will be triggered to deal with it.
IV. Virtio_bus
All kinds of objects in the kernel are always orderly. In order to manage each specific virtio driver and each specific virtio device, simply create a virtio_bus. Of course, there is no actual hardware circuit for this bus, and it plays a purely management and adaptation role. In terms of this management and adaptation function, it is similar to the pci bus. All virtio driver and virtio device can be found in virtio_bus. Whenever a new virtio driver or virtio device is registered with the system, the system performs a matching operation between the device and the driver.
So much for sharing about how the virtio driver interacts with the device. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.