| |

QEMU/KVM Network Mechanisms

Introduction

As we know, network subsystems are important in computer systems since they are I/O systems and need to be optimized with many algorithms and skills. This article will introduce how QEMU/KVM [2] network part works. In order to put everything simple and easy to understand, we will begin with several examples and then understand how it works internally.

Examples

In this example, we will use TAP device [1] as QEMU/KVM host network device driver and VirtIO driver will be used to send/receive network packets/data between Host OS and Guest OS. See following “Start QEMU/KVM virtual machine” commands in details.


sudo x86_64-softmmu/qemu-system-x86_64 /home/hkucs/colo_ubuntu_single.img -machine pc-i440fx-2.3,accel=kvm,usb=off -netdev tap,id=hn0,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown -device virtio-net-pci,id=net-pci0,netdev=hn0 -vnc :7 -m 2048 -smp 2 -device piix3-usb-uhci -device usb-tablet -monitor stdio

Actually, according to my understanding, TAP device and driver are used in Host OS to send/receive packets/data to/from Gust OS since TAP driver can connect Gust OS with Internet. TAP driver will send the packets to Virtio in Host OS part and Virtio puts these packets into buffers, and at last, it notifies Virtio driver in Guest OS part to receive these packets. It has the same theory for how TAP devices receive packets from Guest OS. See following function stack for this part.


tap_send -----------> HOST TAP device driver
-> qemu_send_packet_async
-> qemu_send_packet_async_with_flags
->qemu_net_queue_send
->qemu_net_queue_deliver
->qemu_deliver_packet
->nc->info->receive
->virtio_net_receive -------------> since we use virtio-net-pci driver
->virtqueue_fill, virtqueue_flush, virtio_notify
->virtio_notify_vector
->k->notify
->virtio_pci_notify
->msix_notify
-> msi_send_message
->address_space_stl_le
->address_space_stl_internal

Actually, I think there is another example in [1]’s “Connecting VLANs To TAP Devices” part, and the function call stack should be like following.


tap_send -----------> HOST TAP device driver
-> qemu_send_packet_async
-> qemu_send_packet_async_with_flags
->qemu_net_queue_send
->qemu_net_queue_deliver
->qemu_deliver_packet
->nc->info->receive
->net_hub_port_receive ---> Start VLAN
->net_hub_receive
->qemu_send_packet
-> qemu_send_packet_async
->qemu_sendv_packet_async
...
->e1000_receive ---->NIC

Mechanisms

We need to look into mechanisms from above examples. So firstly, we will find that, in order to improve system performance, network part in QEMU/KVM will handle these packets in asynchronous way (it is also the way to be used in critical section). This is also due to these packets in memory cannot be operated by two threads simultaneously. On the other hand, these layers (TAP layer, Virtio layer, etc) are decoupled so they system design/implementation will be more flexibility and scalability. At last, Virtio is a para-virtualized technology to improve the performance for I/O systems in virtualization environments.

References

[1] https://people.gnome.org/~markmc/qemu-networking.html
[2] https://lxr.missinglinkelectronics.com/qemu+v2.4.0/

Similar Posts

  • 禁止对话框关闭按钮和Alt+F4

    在某些情况下我们需要防止用户单击窗口的标题栏中的关闭按钮关闭 MFC 应用程序。可以删除窗口的WS_SYSMENU 样式, 但是,这样最大化最小化和还原按钮也被删除,并且无法添加。 这是Windows的设计依据。 可以通过禁用关闭按钮来模拟没有关闭按钮的窗口。 在 WM_CREATE 消息处理程序中禁用关闭按钮。使用下面的代码: CMenu *pSysMenu = GetSystemMenu(FALSE); ASSERT(pSysMenu != NULL); VERIFY(pSysMenu->RemoveMenu(SC_CLOSE, MF_BYCOMMAND)); 这样删除之后关闭按钮变为灰色,用户无法点击。但是使用Alt+F4仍然可以关闭程序。要将此功能也禁用需要重载CDialog的OnSysCommand方法。代码如下: void MyDlg::OnSysCommand( UINT nID, LPARAM lParam ) { if ( ( nID & 0xFFF0 ) == IDM_ABOUTBOX ) { CAboutDlg dlgAbout; //if you have an about dialog dlgAbout.DoModal(); } //add the following code else if…

  • Compress PNG Images on Linux

    PNG images already use DEFLATE data compression algorithm involving a combination of LZ77 and Huffman coding. But the PNG images can be further compressed by removing non-important metadata or using lossy compression to save storage space and/or data transfer bandwidth. In this post, we introduce 2 compression ways with tools available on Linux. Lossless compression…

  • |

    Extracting EC Public Keys with OpenSSL

    In the realm of cryptography, handling and managing keys is a crucial task. The command provided is a series of operations using OpenSSL and other command-line utilities to extract and format an elliptic curve (EC) public key. Let’s break down the command to understand its purpose and functionality. The Command Extracting EC Public Keys with…

  • Emacs Tips and Howtos

    With Emacs, I feel happy. I love the rich functions of Emacs, such as compiling, quickly jumping to the lines with compilation error and debugging with gdb, and more. I ever wrote small tips posts about Emacs before. But it is a good idea to put them together and keep adding new ones. Here comes…

Leave a Reply

Your email address will not be published. Required fields are marked *