Network communication method, device and equipment between virtual machine and host machine
By setting up a semi-virtualized driver module on the virtual machine and using epoll instances, the virtual machine and the host realize network communication without a physical network card, solving the problem of inability to communicate and achieving efficient cross-node communication.
Patent Information
- Application Number
- CN202411280831.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-12
- Publication Date
- 2024-12-27
AI Technical Summary
When the host does not have a network device or the virtual machine cannot access the network device on the host, the virtual machine and the host cannot communicate.
By setting up a semi-virtualized driver module on the virtual machine, establishing a transmission channel and registering a channel number and port number, network communication between the virtual machine and the host is realized, using the epoll instance and eventfd connection handle for thread or inter-process communication, simulated socket network connection.
Without a physical network card, network communication between the virtual machine and the host is realized, communication barriers are solved, resource usage is reduced, socket communication and cross-node communication are realized in an environment without a network card.
Smart Images

Figure CN119201341A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a network communication method, device and equipment between a virtual machine and a host machine. Background Art
[0002] Virtio-net is a standardized solution for network devices based on the Virtio framework. It allows virtual machines to directly access network hardware on the host machine and achieve high-performance and low-latency network communication through virtualization. Virtio-net requires the existence of corresponding network devices on the host machine to achieve communication between the virtual machine and the host. The virtual machine uses the virtio-net driver to access the physical network card of the host machine and achieve communication with the external network through the virtualized network device.
[0003] However, if the host does not have a physical network card, the virtual machine will not be able to communicate over the network through virtio-net. Although virtualization software can simulate virtual network cards, they still need to be connected to the physical network card on the host to achieve network access. Therefore, the host needs at least one physical network card to support network communication for the virtual machine. However, in some usage scenarios, the physical network card is not set up on the host, or the virtual machine may not be able to access the network device on the host, or even there is no network device available on the host, which will cause the virtual machine and the host to be unable to communicate. Summary of the invention
[0004] In view of this, the present invention provides a network communication method, device and equipment between a virtual machine and a host machine to solve the problem that the virtual machine and the host machine cannot communicate when there is no network device on the host machine or the virtual machine cannot access the network device on the host machine.
[0005] In a first aspect, the present invention provides a network communication method between a virtual machine and a host machine, the method being applied to a virtual machine, wherein a paravirtualization driver module is provided on the virtual machine, the method comprising:
[0006] The virtual machine uses the semi-virtualized driver module to establish a transmission channel with the host machine;
[0007] The virtual machine registers a first instance after the transmission channel is established, and records the channel number and the port number in a connection array, wherein the first instance includes a handle for communication between threads or processes;
[0008] The virtual machine sends a first message to the host machine, and a message header of the first message carries indication information, a source address, and a destination address, and the indication information is used to indicate a message type of the first message, so that the host machine forwards the first message through the recorded channel number and port number according to the message type, the source address, and the destination address.
[0009] In combination with the first aspect, in a possible implementation manner, the virtual machine uses the paravirtualization driver module to establish a transmission channel with the host machine, including:
[0010] The virtual machine receives a second message from the host machine;
[0011] The virtual machine parses the second message to obtain a message type and a message address;
[0012] If the message type is a negotiation type, it is determined that the link with the host is established for the first time, and the channel number and the port number of the message address are obtained; the channel number and the port number are saved in the corresponding message to the message queue;
[0013] The virtual machine registers and records the channel number and the port number in the first array;
[0014] The method further comprises:
[0015] When a third message sent by using the channel number and the port number is received again, parsing the third message to obtain a message type and a message address of the third message;
[0016] If the message type is not a negotiation type, determining that the third message is a normal communication message;
[0017] Searching in the first array whether the third message is registered or not;
[0018] If so, it is determined that the negotiation is successful, the third message is processed, and the channel establishment with the host machine is completed.
[0019] In combination with the first aspect, in another possible implementation, searching in the first array whether the third message is registered and exists includes:
[0020] If not, the third message is determined to be abnormal, and the third message is discarded or refused to be sent.
[0021] In combination with the first aspect, in another possible implementation manner, after the channel to be transmitted is established, registering the first instance, and before recording the channel number and the port number in the connection array, further comprising:
[0022] The virtual machine receives the current channel number and port number sent by the host machine to form the current client address;
[0023] The virtual machine sends a read request message to the host machine, wherein the read request message includes the current channel number and port number;
[0024] When the host machine detects that the read request message is sent on the current channel number and port number, the read request message is forwarded using the current channel number and port number.
[0025] In combination with the first aspect, in another possible implementation manner, after the channel to be transmitted is established, the first instance is registered, and the channel number and the port number are recorded in a connection array;
[0026] Create an epoll instance and apply for an eventfd connection handle;
[0027] The eventfd connection handle is added to the epoll instance, and the current channel number and port number are recorded in the connection array, which is receiving type connection data.
[0028] In combination with the first aspect, in another possible implementation manner, after the virtual machine sends the first message to the host machine, the method further includes:
[0029] The virtual machine receives a fourth message sent from the host machine;
[0030] Parsing the fourth message to determine a corresponding message type;
[0031] If the message type is a data type, forwarding the fourth message to a corresponding destination.
[0032] In a second aspect, the present invention further provides a network communication method between a virtual machine and a host machine, which is applied to the host machine, wherein the host machine and the virtual machine are connected via a transmission channel established by a paravirtualization driver module, and the method comprises:
[0033] The host machine sends a fifth message to the virtual machine, wherein the fifth message includes a current channel number and a port number;
[0034] The host machine creates a second instance, and records the current channel number and the port number in a monitoring array, wherein the second instance includes a handle for monitoring messages between threads or processes;
[0035] When a sixth message sent from the virtual machine to the current channel number and the port number is monitored, the sixth message is forwarded to a destination end corresponding to the destination address carried by the sixth message.
[0036] In combination with the second aspect, in a possible implementation manner, forwarding the sixth message to a destination end corresponding to a destination address carried by the sixth message includes:
[0037] The host machine parses the sixth message to determine a message type of the sixth message;
[0038] If the message type is a negotiation message, determining whether the port number and the channel number in the address in the sixth message are registered in the listening array;
[0039] If yes, the sixth message is received, and the operation of the sixth message is executed to write data into the monitoring array.
[0040] In a third aspect, the present invention provides a network communication device between a virtual machine and a host machine, wherein a paravirtualization driver module is provided on the device, and the device comprises:
[0041] A channel establishment module, used to establish a transmission channel with a host machine by using the semi-virtualization driver module;
[0042] An instance registration module, used for registering a first instance after the transmission channel is established, and recording the channel number and the port number in a connection array, wherein the first instance includes a handle for communication between threads or processes;
[0043] The first sending module is used to send a first message to the host machine. The message header of the first message carries indication information, a source address, and a destination address. The indication information is used to indicate the message type of the first message, so that the host machine forwards the first message through the recorded channel number and port number according to the message type, the source address, and the destination address.
[0044] In a fourth aspect, the present invention further provides a network communication device between a virtual machine and a host machine, wherein the device and the virtual machine are connected via a transmission channel established by a semi-virtualized driver module, and the device comprises:
[0045] A second sending module, used to send a fifth message to the virtual machine, where the fifth message includes a current channel number and a port number;
[0046] An instance creation module, used for creating a second instance and recording the current channel number and the port number in a monitoring array, wherein the second instance includes a handle for monitoring messages between threads or processes;
[0047] The forwarding module is used to forward the sixth message to a destination end corresponding to a destination address carried by the sixth message when monitoring a sixth message sent from the virtual machine to the current channel number and the port number.
[0048] In a fifth aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the network communication method between a virtual machine and a host machine of the above-mentioned first aspect, second aspect or any corresponding embodiment thereof by executing the computer instructions.
[0049] In a sixth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the network communication method between a virtual machine and a host machine of the first aspect, the second aspect or any corresponding embodiment thereof.
[0050] In addition, the present invention provides a computer program product, including computer instructions, which are used to enable a computer to execute the network communication method between a virtual machine and a host machine of the above-mentioned first aspect, second aspect or any corresponding embodiment.
[0051] The present invention provides a network communication method, device and equipment between a virtual machine and a host machine. By setting a semi-virtualized driver module on the virtual machine, two requests of storage and message communication can be realized by using the semi-virtualized driver module, thereby solving the problem that the virtual machine and the host machine cannot communicate when there is no network device on the host machine or the virtual machine cannot access the network device on the host machine.
[0052] In addition, during the process of establishing network communication between the host and the virtual machine, a first instance containing a handle for communication between threads or processes is also created. The first instance replaces the actions such as monitoring of the socket network connection. For the monitoring action of the receiving end, only an eventfd event needs to be created and added to the first instance, which is set to edge triggered and readable. After the above instance creation and setting, the functions of mutual recognition and communication between the host and the virtual machine are realized. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0054] Figure 1 is an application scenario diagram of network communication between a virtual machine and a host machine according to an embodiment of the present invention;
[0055] Figure 2 is a flow chart of a network communication method between a virtual machine and a host machine according to an embodiment of the present invention;
[0056] Figure 3 is a flow chart of another network communication method between a virtual machine and a host machine according to an embodiment of the present invention;
[0057] Figure 4 is a schematic diagram of message transmission between a virtual machine and a host machine according to an embodiment of the present invention;
[0058] Figure 5 is a flow chart of another method for network communication between a virtual machine and a host machine according to an embodiment of the present invention;
[0059] Figure 6 is a structural block diagram of a network communication device according to an embodiment of the present invention;
[0060] Figure 7 is a structural block diagram of another network communication device according to an embodiment of the present invention;
[0061] Figure 8 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0062] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0063] In the description of the present invention, it should be noted that the terms "first", "second", and "third" are used for descriptive purposes only and should not be understood as indicating or implying relative importance. Unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a physical connection or a software or virtual connection. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0064] First, the application scenarios and related technical terms of the technical solution of the present invention are introduced.
[0065] The technical solution provided by the embodiment of the present invention is applied to data reading and writing scenarios in a virtualized environment, such as Figure 1 , which is an application scenario diagram of network communication between a virtual machine and a host machine provided by an embodiment of the present invention.
[0066] See also Figure 1, which includes Virtio or Virtio modules. Specifically, Virtio is an I / O virtualization framework for virtualized environments that aims to provide high-performance and low-overhead device virtualization solutions. The design goal of the Virtio framework is to improve the I / O performance of virtual machines by providing a set of standard device interfaces for virtual machines and defining a common I / O communication protocol between the host and the client.
[0067] The Virtio framework exists in virtualization environments such as QEMU and KVM, and is supported by many virtualization platforms and operating systems. Features of the Virtio framework include:
[0068] Feature 1: Standardized device interface: Virtio defines a series of standard device interfaces, such as network device (virtio-net) interface, storage device (virtio-blk) interface, memory device (virtio-mem) interface, etc. These standardized device interfaces provide virtual machines with the ability to communicate with the host machine.
[0069] Feature 2: Lightweight and efficient communication protocol: The Virtio framework uses a lightweight and efficient communication protocol to implement I / O data transmission between the host and the client, including device configuration, control, and data transmission operations.
[0070] Feature 3: Wide range of supported platforms: The Virtio framework is supported by many virtualization platforms and operating systems, including virtualization platforms such as QEMU, KVM, and Xen, as well as operating systems such as Linux and FreeBSD.
[0071] Feature 4: Providing high performance and low overhead: Due to the use of efficient communication protocols and sharing of data structures and memory space between virtual machines and host machines, Virtio can provide high-performance I / O virtualization solutions and reduce the overhead of the virtualization environment.
[0072] In addition, with the Virtio framework at the front end, different processes can be run, such as process 1, process 2, and process 3. Each process transmits messages to the host or Vhost through channel 1, channel 2, or channel 3 through a series of processes.
[0073] In virtualization technology, Vhost is a backend implementation of virtio. Virtio is a semi-virtualization implementation that requires both the virtual machine and the host to provide drivers to complete communication. Usually, the virtio host driver is implemented in qemu in user space, while vhost is implemented in the kernel and is a module of the kernel (such as vhost-net.ko). The event communication between vhost and kvm is implemented through the eventfd mechanism, which mainly includes two directions of events, one is the kick event from guest to vhost, which is implemented through ioeventfd; the other is the call event from vhost to guest, which is implemented through irqfd.
[0074] In addition, the Virtio framework includes: virtio-scsi module and virtio-net module.
[0075] Among them, SCSI (Small Computer System Interface) is an interface standard for connection and communication, mainly used for high-speed data transmission between computer systems and external devices.
[0076] Virtio-net is a standardized solution for network devices based on the Virtio framework. It allows virtual machines to directly access network hardware on the host machine and achieve high-performance and low-latency network communication through virtualization. Virtio-net provides direct and efficient network connections for virtual machines, allowing virtual machines to use network resources like physical machines. A common usage scenario is to run multiple virtual machines in a virtualized environment, and they all need to communicate with the external network. Virtio-net requires the presence of corresponding network devices on the host to achieve communication between virtual machines and the host. The virtual machine uses the virtio-net driver to access the physical network card of the host machine and achieve communication with the external network through the virtualized network device. If there is no physical network card on the host, the virtual machine will not be able to communicate on the network through virtio-net. Although virtualization software can simulate virtual network cards, they still need to be connected to the physical network card on the host machine to achieve network access.
[0077] When storage is used, virtio-scsi is needed to allow virtual machines to directly access SCSI storage devices on the host machine, and achieve high-performance and low-latency storage access through virtualization. Virtio-scsi is usually used in virtualized environments where high-performance access to storage devices is required. At this time, if message communication is required, additional virtio-net needs to be loaded, which consumes resources.
[0078] To solve the above technical problems, an embodiment of the present invention provides a network communication method between a virtual machine and a host machine. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than that here.
[0079] The technical solution of the embodiment of the present invention utilizes virtualization technology. Virtio on a virtual machine / virtual machine utilizes non-4k aligned data as message information. Virtio first sends a read request to vhost, and the request carries a port number and a channel number. After vhost receives the read request (pending_read) of virtio, it parses the port number and channel number in the address and determines which channel and port are listening for the message. When vhost receives the message, it is responsible for forwarding it to the designated listening channel according to the address in the message, including the channel number and the port number, so that virtio receives the message forwarded by vhost, thereby realizing virtio message transmission of different channels and ports.
[0080] The technical solution of this embodiment imitates the socket mechanism and converts the above into network communication. It listens and sends messages between the front-end virtio and the back-end vhost. It registers epoll and then waits for epoll_wait to realize message transmission, thereby realizing network communication between processes or threads.
[0081] The technical solution provided in this embodiment is described in detail below.
[0082] In this embodiment, a network communication method between a virtual machine and a host machine is provided, which can be used for a front-end device, such as node 1 (node1), wherein the node 1 includes a virtual machine, such as virtio, and a back-end host machine, such as vhost.
[0083] Figure 2 This is a flow chart of a network communication method according to an embodiment of the present invention, and the method flow includes the following steps:
[0084] Step S101: The virtual machine uses a paravirtualization driver module to establish a transmission channel with the host machine.
[0085] The virtual machine may be a virtio virtual machine, in which a paravirtualization driver module is provided, and further, the paravirtualization driver module is a virtio-scsi module. The virtio-scsi module is mainly used for disk I / O operations in a KVM virtualization environment. It simulates a SCSI device and provides high-performance disk access capabilities for virtual machines. It provides interfaces and functions similar to physical SCSI devices and is used to simulate SCSI devices in a virtualization environment.
[0086] Inside the virtual machine, a specific front-end driver is pre-installed to identify and use the virtio-scsi module. In the physical machine, QEMU is responsible for simulating the back-end service of the virtio-scsi device and processing I / O requests from the virtual machine. The virtio-scsi module enables communication and data transmission between the front-end driver and the back-end service through the virtio bus, achieving efficient disk I / O operations.
[0087] Before initializing the virtio-scsi module, when processing normal data IO requests, ensure that data IO requests are 4KB aligned at the virtual machine. Generally, for storage devices in a virtualized environment, 4KB alignment can provide better performance because it conforms to the page size of most common storage devices. For special message requests, LBA can be set to a special address that is not 4KB aligned, such as 666. In this way, when issuing IO requests, the backend can distinguish between normal data IO requests and message IO requests through this special address, thereby realizing communication between the virtual machine and the host machine.
[0088] For example, after receiving a data packet, the data packet is 4K aligned data. If parsed to contain no special address, the data is determined to be normal data data. If parsed to carry a destination address such as 666, the data is determined to be message data (message, msg).
[0089] Step S102: registering a first instance after the transmission channel is established, and recording the channel number and the port number in a connection array, wherein the first instance includes a handle for communication between threads or processes.
[0090] Specifically, the virtual machine registers the first instance after the transmission channel is established, and records the channel number and port number in the connection array, including: first creating an epoll instance and applying for an eventfd connection handle; adding the eventfd connection handle to the epoll instance, and recording the current channel number and port number in the connection array, which is a receiving type connection data.
[0091] Among them, the epoll instance refers to a specific program or application scenario implemented using the epoll mechanism in the Linux system. epoll is an efficient I / O event notification mechanism under Linux. It allows users to monitor multiple file descriptors at the same time through system calls such as epoll_create, epoll_ctl, and epoll_wait, and can efficiently handle these events when events occur on a file descriptor.
[0092] The channel number is the channel number of a transmission channel connecting virtio and vhost, for example Figure 1 As shown, channel 1, channel 2 or channel 3 are all the channel numbers, and the port number (port number) is used to distinguish the ports of processes or threads.
[0093] In this step, eventfd is used and added to epoll to replace the listen accept and other actions of the socket. For the listen action, only an eventfd event listern_fd needs to be generated, added to epoll, set to edge triggered, and set to readable. Similarly, when simulating the accept action, an eventfd event accept_fd is also generated, added to epoll, set to edge triggered, and set to readable.
[0094] For the address required for socket communication, it is set through the channel of the vhost backend. The channel number and port number form addr. For example, the channel number is 2 bytes, the port number is also 2 bytes, and they are concatenated into the IPv4 address format, totaling four bytes. The channel number is the high 2 bytes of the address, and the port number is the low 2 bytes of the address.
[0095] Step S103: the virtual machine sends a first message to the host machine, and the message header of the first message carries indication information, a source address, and a destination address.
[0096] The indication information is used to indicate the message type of the first message, so that the host machine forwards the first message through the recorded channel number and port number according to the message type, source address and destination address.
[0097] The first encapsulated message is parsed to obtain a message header, which includes: message type indication information, source address and destination address, for example, including: msg_hdr a, src_node_idx, src_port_num, dst_node_idx and dst_port_num. Among them, msg_hdr a indicates the message type, src_node_idx and src_port_num respectively indicate the channel number and port number of the source address, and dst_node_idx and dst_port_num respectively indicate the channel number and port number of the destination address.
[0098] In this embodiment, after initializing the virtio-scsi module, the host machine first sends a read request to the back-end vhost and carries its own port number and channel number. When the back-end perceives that the front-end has a read request and has a specified address, such as a message with a special address of 666 passed to the requested port, the host vhost passes the message to the front-end to realize the message receiving and forwarding functions.
[0099] This embodiment provides a method for multiplexing virtio-scsi to realize socket network communication between a virtual machine and a host machine. The method only needs to use the virtio-scsi module to realize two requests of storage and message communication, and solves the problem that the virtual machine and the host machine cannot communicate when there is no network device on the host machine or the virtual machine cannot access the network device on the host machine.
[0100] In a possible implementation, before the virtual machine and the host machine transmit messages, a link or transmission channel is first established. The difference between this link establishment process and the normal socket network link establishment process is that both the client (front end) and the server (back end) need to bind the bind address, that is, both sending and receiving need to register the channel in advance, otherwise the sender receiving data may not be able to perceive the message during the link establishment process. Specifically, Figure 3 As shown, establishing a link or establishing a transmission channel with the host includes:
[0101] Step S201: The virtual machine receives a second message from the host machine. When virtio receives the second message sent by the vhost, it first parses and determines the message type of the second message.
[0102] Step S202: The virtual machine parses the second message to obtain a message type and a message address. The second message carries the message type and the message address, and the message address includes a port number and a channel number.
[0103] Step S203: Determine whether the message type is a negotiation type.
[0104] Step S204: If the message type is a negotiation type, it is determined that it is the first time to establish a link with the host machine, and the channel number and port number of the message address are obtained.
[0105] If the message type is negotiation type, it means that the link is established for the first time, the channel number and port number of the message address are parsed, and the corresponding message is saved in the message queue.
[0106] Step S205: Save the channel number and the port number in the corresponding message to the message queue, and register the channel number and the port number in the first array. Virtio registers the channel number and the port number in accept_fd.
[0107] The method further comprises:
[0108] Step S206: when a third message sent by using the channel number and the port number is received again, the third message is parsed to obtain a message type and a message address of the third message.
[0109] Step S207: If the message type is not a negotiation type, determine that the third message is a normal communication message.
[0110] When a message with the channel number and port number is sent later, if the message type is not the negotiation type, it means that the message is a normal communication message.
[0111] Step S208: Search the first array to see whether the third message is registered or not.
[0112] Step S209: If yes, it is determined that the negotiation is successful, the third message is processed, and the channel establishment with the host is completed. Specifically, directly check whether the channel number and port number have been registered before in accept_fd. If yes, it means that the negotiation is successful, and the subsequent message processing is performed.
[0113] In addition, the method further includes: if the third message does not exist in step S208, the third message is determined to be abnormal, and the third message is discarded or rejected. If the third message does not exist, the message information is abnormal, and the message is discarded or a rejection message is sent.
[0114] Among them, the negotiation message can add a normal authentication process and add a series of status authentication, such as connecting hello, auth, ready and other states to ensure the stability of the message channel.
[0115] The backend vhost is only responsible for receiving and forwarding messages. According to the channel number and port number in the message, it directly forwards the message to the corresponding channel without doing any other processing.
[0116] In a possible implementation of this embodiment, before the above step S102, it also includes: the virtual machine receives the current channel number and port number sent by the host machine, forms the current client address, and generates a client IP address, which includes the current channel number and port number (port number); the virtual machine sends a read request message (pending_read message) to the host machine, and the read request message includes the current channel number and port number; the host machine receives and binds the current channel number and port number. When the host machine detects that the read request message is sent on the current channel number and port number, the read request message is forwarded using the current channel number and port number.
[0117] In a specific embodiment, Figure 4 As shown, the communication process of the client (virtual machine virtio) includes the following:
[0118] Step 1: Virtio receives the current port number and channel number sent by vhost to form the current client address.
[0119] Step 2: Virtio sends a read request to vhost, carrying the channel number and port number, and then vhost senses that the channel and port are listening for messages. If a message for the channel and port arrives, it forwards the message directly to the channel and port.
[0120] Step 3: The application (such as process 1) creates an epoll instance, applies for eventfd_connect (handle), and adds the eventfd to the epoll instance, records the corresponding channel number and port number in the connected_accepted array, indicating that there is a message path in the current virtio. The connected_accepted array is a connection array.
[0121] Step 4: The client corresponding to the application sends a message to the server (such as process 2). The message is encapsulated with a message header, which carries information indicating the message type. If the message type is parsed as a negotiation type, virtio sends the message to the vhost, thereby realizing the connection action between virtio and the vhost.
[0122] In addition, the above method also includes: the virtual machine receives a fourth message sent from the host machine; parses the fourth message to determine the corresponding message type; if the message type is a data type, forwards the fourth message to the corresponding destination.
[0123] In combination with the above specific embodiments, the present invention further includes:
[0124] Step 5: During the subsequent transmission process, virtio directly sends a message to the vhost through the destination address, and determines the message type as the data type to implement the message sending mechanism.
[0125] In addition, an embodiment of the present invention further provides another network communication method between a virtual machine and a host machine, which is applied to the host machine. The host machine and the virtual machine are connected through a transmission channel established by a semi-virtualized driver module. The method of this embodiment corresponds to the aforementioned vhost side method flow, such as Figure 5 As shown, the method includes:
[0126] Step S301: the host machine sends a fifth message to the virtual machine, where the fifth message includes a current channel number and a port number.
[0127] Step S302: The host machine creates a second instance and records the current channel number and port number in the monitoring array. The second instance includes a handle for monitoring messages between threads or processes.
[0128] Step S303: when a sixth message sent from the virtual machine to the current channel number and the port number is monitored, the sixth message is forwarded to a destination end corresponding to the destination address carried by the sixth message.
[0129] In combination with the second aspect, in a possible implementation manner, step S303, forwarding the sixth message to a destination end corresponding to a destination address carried by the sixth message, includes:
[0130] The host machine parses the sixth message and determines the message type of the sixth message; if the message type is a negotiation message, it determines whether the port number and channel number in the address in the sixth message are registered in the listening array; if so, it receives the sixth message, executes the operation of the sixth message, and writes data to the listening array.
[0131] In one embodiment, in combination Figure 4 As shown, the method flow on the server side or the host Vhost side includes:
[0132] Virtio obtains the current port number and channel number from vhost to form the current client address. Virtio sends a read request to vhost, carrying the channel number and port number, and then vhost senses that the channel and port are listening for messages. If a message for the channel and port arrives, it forwards the message directly to the channel and port.
[0133] The host vhost creates an epoll instance, applies for eventfd_listen (listen message), and adds the eventfd to the epoll instance, records the corresponding channel number and port number in the message listening array, indicating that there is a message channel listening in the current virtio, and implements the listen action in the socket.
[0134] When vhost receives a message sent to the port and channel, it directly forwards the message to virtio, for example, forwarding it to the second process (process 2) at the destination.
[0135] When virtio receives a message from vhost, it first parses the message. If the message type is negotiation type, it means the action is connect. Then it determines whether the port number and channel number in the address in the message have been registered in the listened array. If not, it means the message is abnormal and will not be processed.
[0136] If it exists, it means that the connect is indeed received and the accept action needs to be executed to write data to eventfd_listen. Then epoll_wait senses that there is a message incoming and schedules the execution of the accept action. The address is parsed in the accept action, and its port number and channel number are added to the connected_accepted array. Eventfd_connect is generated and epoll_wait is added, indicating that the channel can communicate and the read and write process can be triggered through the corresponding fd.
[0137] When virtio receives a message again and the message type is a data type, the path exists in the connected_accepted array and data is written to eventfd_connect, triggering epoll_wait to schedule a read action, and message parsing is implemented in the read action, thereby realizing message interaction with the client / virtual machine.
[0138] The method provided in this embodiment only needs to use the virtio-scsi module to implement two requests for storage and message communication, solving the problem that the virtual machine and the host cannot communicate when there is no network device on the host or the virtual machine cannot access the network device on the host. It reduces resource usage and can also implement socket communication in an environment without a network card or cross-node communication.
[0139] In addition, at the application layer, eventfd is used to add an epoll instance to implement socket communication, reduce the use of the virtio-net module, and solve the problem that the virtual machine and the host cannot communicate when there is no network device on the host or the virtual machine cannot access the network device on the host. Socket communication and cross-node communication can also be achieved in an environment without a network card.
[0140] In this embodiment, a network communication device between a virtual machine and a host machine is also provided, and the device is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0141] This embodiment provides a network communication device between a virtual machine and a host machine, such as Figure 6 As shown, the device includes: a channel establishment module 610, an instance registration module 620 and a first sending module 630. In addition, the device may also include other modules, such as a first receiving module, a first storage module, etc., which is not limited in this embodiment.
[0142] Wherein, a semi-virtualized driver module is provided on the device, and the device comprises:
[0143] The channel establishing module 610 is used to establish a transmission channel with the host machine by using the paravirtualization driver module.
[0144] The instance registration module 620 is used to register the first instance after the transmission channel is established, and record the channel number and the port number in the connection array, wherein the first instance includes a handle for communication between threads or processes.
[0145] The first sending module 630 is used to send a first message to the host machine. The message header of the first message carries indication information, a source address, and a destination address. The indication information is used to indicate the message type of the first message, so that the host machine forwards the first message through the recorded channel number and port number according to the message type, source address, and destination address.
[0146] Optionally, the semi-virtualized driver module is a virtio-scsi module.
[0147] In addition, the network communication device of this embodiment can be integrated into a server node or a client node.
[0148] In another embodiment, the present invention also provides another network communication device between a virtual machine and a host machine, wherein the device is connected to the virtual machine through a transmission channel established by a semi-virtualized driver module, such as Figure 7 As shown, the network communication device includes: a second sending module 710, an instance creation module 720 and a forwarding module 730. In addition, the device may also include other modules, such as a second receiving module, a second storage module, etc., which is not limited in this embodiment.
[0149] The second sending module 710 is configured to send a fifth message to the virtual machine, wherein the fifth message includes a current channel number and a port number.
[0150] The instance creation module 720 is used to create a second instance and record the current channel number and the port number in a monitoring array. The second instance includes a handle for monitoring messages between threads or processes.
[0151] The forwarding module 730 is configured to forward the sixth message to a destination end corresponding to a destination address carried by the sixth message when monitoring a sixth message sent from the virtual machine to the current channel number and the port number.
[0152] In addition, the network communication device of this embodiment can be integrated into a server node or a client node.
[0153] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.
[0154] The network communication device between the virtual machine and the host machine in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.
[0155] The embodiment of the present invention also provides a computer device having the above Figure 6 or Figure 7 The network communication device shown.
[0156] See also Figure 7, is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. The various components are connected to each other using different buses for communication, and can be installed on a common motherboard or in other ways as needed. The processor can process instructions executed in the computer device, including instructions stored in or on the memory to display graphical information of the GUI on an external input / output device (such as a display device coupled to the interface).
[0157] In some optional embodiments, if desired, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides part of the necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 8 A processor 10 is taken as an example.
[0158] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0159] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the network communication method between the virtual machine and the host machine shown in the above embodiment.
[0160] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0161] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0162] The computer device also includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Figure 8 The example of connecting through bus is taken in the following.
[0163] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the computer device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device can be a touch screen.
[0164] In addition, the computer device also includes at least one communication interface for the computer device to communicate with other devices or communication networks, and the universal interface includes but is not limited to a virtio-scsi module and a virtio-net module.
[0165] An embodiment of the present invention also provides a computer-readable storage medium, and the above-mentioned method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented by downloading through a network and originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware.
[0166] The storage medium may be a disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid state drive, etc.; further, the storage medium may also include a combination of the above-mentioned types of memories. It is understood that a computer, a processor, a microprocessor controller or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by the computer, processor or hardware, the network communication method between the virtual machine and the host machine shown in the above embodiment is implemented.
[0167] Embodiments of the present application may also provide a computer program product, including computer program instructions, which, when executed by a processor, cause the processor to perform the steps in the above method. Wherein, the computer program product may be written in any combination of one or more programming languages to perform program codes for performing the operations of the disclosed embodiments, wherein the programming languages include object-oriented programming languages, such as Java, C++, etc., and also include conventional procedural programming languages, such as "C" language or similar programming languages. The program code may be executed entirely on a user computing device, partially on a user device, as an independent software package, partially on a user computing device, partially on a remote computing device, or entirely on a remote computing device or server.
[0168] The above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention, rather than to limit them. Although the embodiments of the present invention are described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some of the technical features therein by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A network communication method between a virtual machine and a host machine, characterized in that: Applied to a virtual machine, a paravirtualization driver module is set on the virtual machine, and the method includes: The virtual machine uses the paravirtualization driver module to establish a transmission channel with the host machine; The virtual machine registers a first instance after the transmission channel is established, and records the channel number and the port number in a connection array, wherein the first instance includes a handle for communication between threads or processes; The virtual machine sends a first message to the host machine, and a message header of the first message carries indication information, a source address, and a destination address, and the indication information is used to indicate a message type of the first message, so that the host machine forwards the first message through the recorded channel number and port number according to the message type, the source address, and the destination address.
2. The method according to claim 1, characterized in that: The virtual machine uses the paravirtualization driver module to establish a transmission channel with the host machine, including: The virtual machine receives a second message from the host machine; The virtual machine parses the second message to obtain a message type and a message address; If the message type is a negotiation type, determining that it is the first time to establish a link with the host machine, and obtaining the channel number and port number of the message address; The channel number and the port number are stored in a corresponding message queue; The virtual machine registers and records the channel number and the port number in the first array; The method further comprises: When a third message sent by using the channel number and the port number is received again, parsing the third message to obtain a message type and a message address of the third message; If the message type is not a negotiation type, determining that the third message is a normal communication message; Searching in the first array whether the third message is registered or not; If so, it is determined that the negotiation is successful, the third message is processed, and the channel establishment with the host machine is completed.
3. The method according to claim 2, characterized in that The searching in the first array whether the third message is registered or not includes: If not, the third message is determined to be abnormal, and the third message is discarded or refused to be sent.
4. The method according to any one of claims 1 to 3, characterized in that: After the transmission channel is established, registering the first instance and before recording the channel number and the port number in the connection array, the method further includes: The virtual machine receives the current channel number and port number sent by the host machine to form the current client address; The virtual machine sends a read request message to the host machine, wherein the read request message includes the current channel number and port number; When the host machine detects that the read request message is sent on the current channel number and port number, the read request message is forwarded using the current channel number and port number.
5. The method according to claim 4, characterized in that After the transmission channel is established, register the first instance, and record the channel number and port number in the connection array; Create an epoll instance and apply for an eventfd connection handle; The eventfd connection handle is added to the epoll instance, and the current channel number and port number are recorded in the connection array, which is receiving type connection data.
6. The method according to claim 1, characterized in that After the virtual machine sends the first message to the host machine, the method further includes: The virtual machine receives a fourth message sent from the host machine; Parsing the fourth message to determine a corresponding message type; If the message type is a data type, forwarding the fourth message to a corresponding destination.
7. A network communication method between a virtual machine and a host machine, characterized in that: Applied to a host machine, the host machine and the virtual machine are connected through a transmission channel established by a paravirtualization driver module, and the method includes: The host machine sends a fifth message to the virtual machine, wherein the fifth message includes a current channel number and a port number; The host machine creates a second instance, and records the current channel number and the port number in a monitoring array, wherein the second instance includes a handle for monitoring messages between threads or processes; When a sixth message sent from the virtual machine to the current channel number and the port number is monitored, the sixth message is forwarded to a destination end corresponding to the destination address carried by the sixth message.
8. The method according to claim 7, characterized in that Forwarding the sixth message to a destination end corresponding to the destination address carried by the sixth message includes: The host machine parses the sixth message to determine a message type of the sixth message; If the message type is a negotiation message, determining whether the port number and the channel number in the address in the sixth message are registered in the listening array; If yes, the sixth message is received, and the operation of the sixth message is executed to write data into the monitoring array.
9. A network communication device between a virtual machine and a host machine, characterized in that: The device is provided with a semi-virtualized driver module, and the device comprises: A channel establishment module, used to establish a transmission channel with a host machine by using the semi-virtualization driver module; An instance registration module, used for registering a first instance after the transmission channel is established, and recording the channel number and the port number in a connection array, wherein the first instance includes a handle for communication between threads or processes; The first sending module is used to send a first message to the host machine. The message header of the first message carries indication information, a source address, and a destination address. The indication information is used to indicate the message type of the first message, so that the host machine forwards the first message through the recorded channel number and port number according to the message type, the source address, and the destination address.
10. A network communication device between a virtual machine and a host machine, characterized in that: The device is connected to the virtual machine through a transmission channel established by a paravirtualization driver module, and the device includes: A second sending module, used to send a fifth message to the virtual machine, wherein the fifth message includes a current channel number and a port number; An instance creation module, used for creating a second instance and recording the current channel number and the port number in a monitoring array, wherein the second instance includes a handle for monitoring messages between threads or processes; The forwarding module is used to forward the sixth message to a destination end corresponding to a destination address carried by the sixth message when monitoring a sixth message sent from the virtual machine to the current channel number and the port number.
11. A computer device, characterized in that: comprising a memory and a processor, wherein the memory and the processor are connected; The memory stores computer instructions, and the processor executes the network communication method between a virtual machine and a host machine according to any one of claims 1 to 6 or any one of claims 7 to 8 by executing the computer instructions.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute any one of claims 1 to 6, or the network communication method between a virtual machine and a host machine according to any one of claims 7 to 8.
Citation Information
Cited By
Method for file transmission between host machine and client machine
CN119520513A
A method for file transfer between a host and a guest machine
CN119520513B
Distributed system-oriented vhost-user identity authentication and I / O authentication method
CN120675814A
A vhost-user authentication and I / O authorization method for distributed systems
CN120675814B