Network communication method and device and computer equipment
By setting up network traffic control programs at the ports of network cards and virtual switches and using eBPF technology to redirect network traffic, the problem of unstable network communication in virtual machines is solved and the stability and efficiency of network communication are improved.
Patent Information
- Application Number
- CN202510823953.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-19
- Publication Date
- 2025-09-05
AI Technical Summary
In the prior art, when a virtual machine or container communicates with an external network, the network card is connected to a virtual switch and the IP address is migrated, causing network communication instability.
By setting up network traffic control programs at the ports of the network card and virtual switch, and using eBPF technology to redirect network traffic, network card IP address migration is avoided, and network traffic is transmitted directly between the network card and the virtual switch.
It improves the stability and efficiency of network communications, decouples the upper-layer application platform from the underlying Linux system, and protects necessary traffic from being affected by the virtual switch.
Smart Images

Figure CN120602439A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of network communication technology, and in particular to a network communication method, apparatus, computer equipment, computer-readable storage medium, and computer program product. Background Art
[0002] Linux servers require virtualization technologies based on virtual machines or containers for network communication. For example, virtual switches are used to connect to the network. When a virtual machine or container needs to communicate with the external network, the network card (NIC) must be connected to the virtual switch and the IP (Internet Protocol) address must be migrated to another virtual NIC. However, this network communication method of connecting the NIC to the virtual switch and migrating the NIC's IP address couples the application platform with the Linux server and relies on complex network communication configuration in the virtual switch. Errors in the virtual network communication configuration can affect server IP communication, reducing network communication stability.
[0003] Therefore, the current network communication method has the defect of low stability. Summary of the Invention
[0004] Based on this, it is necessary to provide a network communication method, apparatus, computer equipment, computer-readable storage medium and computer program product that can improve stability in response to the above technical problems.
[0005] In a first aspect, the present application provides a network communication method, applied to a network card, the method comprising:
[0006] Obtaining network traffic to be processed and determining a target receiving location for the network traffic;
[0007] Loading a network traffic control program, and redirecting the network traffic to a first target port in a virtual switch through the network traffic control program; the virtual switch is used to redirect the network traffic to the network card through a second target port and the network traffic control program;
[0008] Target network traffic is determined from the network traffic, and the target network traffic is sent to the target receiving location.
[0009] In a second aspect, the present application provides a network communication method, applied to a virtual switch, the method comprising:
[0010] Acquire network traffic sent by the network card through the first target port, and forward the network traffic to the second target port;
[0011] At the second target port, the network traffic is redirected to the network card through the network traffic control program; the network card is used to determine target network traffic from the network traffic and send the target network traffic to a target receiving location corresponding to the network traffic.
[0012] In a third aspect, the present application further provides a network communication device, applied to a network card, comprising:
[0013] A first acquisition module is used to acquire network traffic to be processed and determine a target receiving location of the network traffic;
[0014] a first redirection module, configured to load a network traffic control program and redirect the network traffic to a first target port in the virtual switch through the network traffic control program; the virtual switch is configured to redirect the network traffic to the network card through a second target port and the network traffic control program;
[0015] The transmission module is used to determine the target network traffic from the network traffic and send the target network traffic to the target receiving location.
[0016] In a fourth aspect, the present application further provides a network communication device, applied to a virtual switch, comprising:
[0017] A second acquisition module is used to acquire network traffic sent by the network card through the first target port and forward the network traffic to the second target port;
[0018] The second redirection module is used to redirect the network traffic to the network card through the network traffic control program at the second target port; the network card is used to determine the target network traffic from the network traffic and send the target network traffic to the target receiving location corresponding to the network traffic.
[0019] In a fifth aspect, the present application further provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.
[0020] In a sixth aspect, the present application also provides a computer-readable storage medium having a computer program stored thereon, which implements the steps of the above method when executed by a processor.
[0021] In a seventh aspect, the present application also provides a computer program product, comprising a computer program, which implements the steps of the above method when executed by a processor.
[0022] The above-mentioned network communication method, apparatus, computer device, computer-readable storage medium, and computer program product determine the target receiving location of the network traffic, redirect the network traffic to the first target port in the virtual switch through a network traffic control program, and redirect the network traffic to the network card through the second target port in the virtual switch and the network traffic control program. The network card determines the target network traffic from the network traffic and sends the target network traffic to the target receiving location. Compared with the traditional communication method of connecting the network card to the virtual switch and then migrating the address of the network card, this solution sets a network traffic control program at the ports of the network card and the virtual switch, and uses the network traffic control program to redirect the network traffic, thereby achieving network communication for the virtual machine using the original settings of the network card, thereby improving the stability of network communication. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the following briefly introduces the drawings required for use in the embodiments of the present application or related technical descriptions. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying any creative work.
[0024] Figure 1 A diagram of an application environment of a network communication method in one embodiment;
[0025] Figure 2 1 is a flow chart of a network communication method according to an embodiment;
[0026] Figure 3 is a flow chart of a network communication method according to another embodiment;
[0027] Figure 4 1 is a flow chart of a network communication method in another embodiment;
[0028] Figure 5 A schematic flow chart of a network communication method according to another embodiment;
[0029] Figure 6 is a structural block diagram of a network communication device in one embodiment;
[0030] Figure 7 is a structural block diagram of a network communication device in another embodiment;
[0031] Figure 8 FIG. 1 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0032] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.
[0033] In the related art, virtualization technology (based on virtual machines or containers) is widely used in Linux servers. Virtual machines or containers typically access the network through a virtual switch. For example, if a virtual machine or container needs to communicate directly with the external physical network, the server network interface card (NIC) must also be connected to the virtual switch. Once the server NIC is connected to the virtual switch, the IP address configured on the NIC will no longer function properly. The IP address must be migrated to another virtual NIC, typically to the local port of the virtual switch. However, the server NIC IP address configuration is specific to the Linux distribution, and different Linux distributions have specific configuration tools and configuration files. IP address migration leads to coupling between the application platform and the Linux distribution. Any anomaly in the server NIC IP address migration operation can cause a server network outage. Furthermore, server NIC IP communication must pass through the virtual switch, which may contain complex virtual machine network communication configurations. Any configuration errors in the virtual switch can affect server NIC IP communication, leading to server network outages.
[0034] Based on this, this solution sets up a network traffic control program at the ports of the network card and the virtual switch, and uses the network traffic control program to redirect network traffic, thereby achieving network communication for the virtual machine with the original settings of the network card, thereby improving the stability of network communication.
[0035] The network communication method provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown, the network card communicates with the virtual switch via a network traffic control program. The network card, through the network traffic control program, sends the network traffic to be transmitted to the virtual switch for forwarding and processing. The virtual switch, through the network traffic control program, redirects the network traffic to the network card, thereby enabling network traffic transmission between the network card and the virtual switch. The server where the network card and virtual switch reside can be an independent physical server, a server cluster or distributed system consisting of multiple physical servers, or a cloud server providing cloud computing services.
[0036] In an exemplary embodiment, Figure 2 As shown, a network communication method is provided, which is applied to Figure 1 The network card in FIG. 1 is taken as an example to illustrate the method, including the following steps S202 to S206. In which:
[0037] Step S202: obtaining the network traffic to be processed and determining the target receiving location of the network traffic.
[0038] The network card can be a physical network card in the server. It has a corresponding IP address. Specifically, it can be eth0 (the first Ethernet port). The network traffic to be processed can be network traffic from external devices or network traffic within the server where the network card is located. For network traffic from external devices, the network card can receive the traffic; for network traffic within the server, the network card can send the traffic. Specifically, the network card can perform network communication processing for this network traffic, including but not limited to receiving and sending the traffic. Furthermore, the network card can also determine the target receiving location for the network traffic.
[0039] Different network traffic flow directions can have different target receiving locations. For example, when receiving traffic, the target receiving location can be the server's kernel protocol stack. A protocol stack is a specific software implementation of a computer network protocol suite, employing a layered structure to collaborate and enable data communication. When sending traffic, the target receiving location can be an external device, such as another server, personal computer, mobile phone, or other network device.
[0040] Step S204, loading a network traffic control program, and redirecting the network traffic to the first target port in the virtual switch through the network traffic control program; the virtual switch is used to redirect the network traffic to the network card through the second target port and the network traffic control program.
[0041] The network card can communicate with the virtual switch based on a network traffic control program, and the network card may not be connected to the virtual switch. A network traffic control program may be provided in the network card. The network traffic control program may be a program based on eBPF technology. eBPF (Extended Berkeley Packet Filter) is a kernel technology that allows users to intercept and process kernel events by loading eBPF programs without modifying the kernel source code. Therefore, when the network card is communicating network traffic, it can load the network traffic control program in the network card and redirect the network traffic through the network traffic control program, for example, to the first target port in the virtual switch. The first target port may be determined based on the direction of the network traffic. For example, the first target port in the virtual switch may be different for the process of receiving traffic and sending traffic.
[0042] The virtual switch may receive the network traffic through the first destination port and perform traffic forwarding processing. A second destination port may be provided in the virtual switch, and the second destination port may be a port of a different nature from the first destination port. The virtual switch may then transmit the network traffic to the second destination port. The virtual switch may include the network traffic control program, which may run on the second destination port as needed. The virtual switch may then process the network traffic accordingly on the second destination port, load the network traffic control program, and use the network traffic control program to redirect the processed network traffic to the network card via the second destination port.
[0043] Step S206: determine the target network traffic from the above network traffic, and send the above target network traffic to the above target receiving location.
[0044] The above-mentioned network traffic may be network traffic forwarded by a virtual switch. After the network card receives the network traffic from the virtual switch, it may determine the target network traffic from the network traffic. The above-mentioned target network traffic may be network traffic with a preset identifier. That is, the virtual switch may identify the network traffic accordingly, and the network card may determine the direction of the network traffic based on whether the network traffic carries the preset identifier. For example, a judgment may be made as to whether it is sent to the virtual switch or to the target receiving location. The network traffic that needs to be sent to the target receiving location may be determined as the target network traffic. After the network card determines the target network traffic from the above-mentioned network traffic, it may send the above-mentioned target network traffic to the target receiving location. For network traffic with different flow directions, the target receiving location determined by the network card may be different. For example, for network traffic with a receiving flow direction and network traffic with a sending flow direction, their respective corresponding target receiving locations may be different. The above-mentioned target receiving location includes but is not limited to the above-mentioned protocol stack and the above-mentioned external device.
[0045] In the above-mentioned network communication method, the target receiving location of the network traffic is determined, and the network traffic is redirected to the first target port in the virtual switch through a network traffic control program. The network traffic is then redirected to the network card through the second target port in the virtual switch and the network traffic control program. The network card determines the target network traffic from the network traffic and sends the target network traffic to the target receiving location. Compared to the traditional communication method of connecting the network card to the virtual switch and then migrating the address of the network card, this solution redirects the network traffic by setting a network traffic control program at the ports of the network card and the virtual switch, thereby achieving network communication for the virtual machine based on the original settings of the network card, thereby improving the stability of network communication.
[0046] In one embodiment, the network traffic is redirected to the first target port in the virtual switch through the network traffic control program, including: if the network traffic includes first network traffic from an external device, determining that the first target port is the uplink port corresponding to the virtual switch, querying the port index table through the network traffic control program to determine the first port index corresponding to the uplink port, and redirecting the first network traffic to the uplink port according to the first target port; if the network traffic includes second network traffic from the protocol stack, determining that the first port index is the virtual bridge port corresponding to the virtual switch, querying the port index table through the network traffic control program to determine the second port index corresponding to the uplink port, and redirecting the second network traffic to the virtual bridge port according to the second port index.
[0047] In this embodiment, the first target port can be adjusted based on the direction of network traffic. The network traffic includes, but is not limited to, first network traffic from an external device and second network traffic from the protocol stack of the server where the network card resides. The virtual switch is configured with multiple ports. For example, the virtual switch includes, but is not limited to, uplink ports and virtual bridge ports. The network card can then determine the location to redirect the network traffic based on the direction of the network traffic.
[0048] Among them, if the network card detects that the above-mentioned network traffic includes the first network traffic from the external device, the network card can determine that the above-mentioned first target port is the uplink port corresponding to the above-mentioned virtual switch. Among them, the network card redirects with the above-mentioned port, and it is necessary to determine the port index of the above-mentioned uplink port, and a port index table is provided in the network card, and the port index table includes the port index corresponding to each port involved in the network communication. Therefore, the network card can query the port index table through the network traffic control program and determine the first port index corresponding to the uplink port. Among them, the first port index represents the index of the above-mentioned uplink port in the Linux kernel. Therefore, the network card can redirect the first network traffic to the above-mentioned uplink port according to the above-mentioned first port index, thereby realizing the reception of network traffic.
[0049] If the network card detects that the network traffic includes the second network traffic from the protocol stack, the network card can determine that the first target port is the virtual bridge port of the virtual switch. When the network card transmits network traffic to the virtual bridge port, it is necessary to determine the port index of the virtual bridge port. The network card can query the port index table through the network traffic control program to determine the second port index corresponding to the virtual bridge port. The second port index represents the index of the virtual bridge port in the Linux kernel. The network card can redirect the second network traffic to the virtual bridge port based on the second port index to achieve the transmission of network traffic.
[0050] Specifically, the network card can be eth0. The network card in the server is not directly connected to the virtual switch, and the IP address on the server network card remains unchanged. The uplink port can be port 1 in the virtual switch; the virtual bridge port can be br0 (the first virtual bridge port) in the virtual switch; and the port index table can be an eBPF map (port index table) using eBPF technology. The eBPF map is global data in the network traffic control program, which can share information between the kernel and user space and stores the indexes of various ports involved in network traffic transmission.
[0051] The aforementioned network traffic control program can be a program using eBPF technology. The network interface card (NIC) can load the eBPF program into the tc classact hook (network traffic control rule hook) of the NIC. TC (Traffic Control) is the network traffic control subsystem in the Linux kernel; clsact (queuing discipline) is a queuing discipline in network traffic control that can load the aforementioned eBPF program. clsact is a queue-less queuing discipline that supports both inbound and outbound traffic.
[0052] When an eBPF program needs to redirect traffic, it needs to know the ifindex (port index) of the network device in the Linux kernel, including but not limited to the first and second port indices mentioned above. The ifindex is dynamically assigned by the Linux kernel. The network card, through the network traffic control program, writes the ifindex of each network device involved in network traffic transmission to the eBPF map mentioned above. When eBPF redirects traffic, it can query the actual ifindex from the map.
[0053] When the network card receives the first network traffic from the external device, the network card redirects the first network traffic to the port1 port in the virtual switch through the network traffic control program; when the network card receives the second network traffic from the protocol stack, the network card redirects the second network traffic to the br0 port in the virtual switch through the network traffic control program.
[0054] Through this embodiment, the network card can determine the redirection location of the network traffic in combination with the flow direction of the network traffic, and determine the port index of the target port in combination with the port index table, thereby redirecting the network traffic to the port corresponding to the virtual switch without the need to migrate the IP address of the network card, thereby improving the stability of network traffic communication.
[0055] In one embodiment, the target network traffic is sent to the target receiving location, including: if the target network traffic includes the first network traffic from the external device, the target network traffic is sent to the protocol stack; if the target network traffic includes the second network traffic from the protocol stack, the target network traffic is sent to the external device corresponding to the target network traffic.
[0056] In this embodiment, for network traffic with different flow directions, the corresponding target receiving locations may be different. The above-mentioned target receiving location includes but is not limited to the protocol stack of the kernel in the server where the network card is located, the external device outside the above-mentioned server, etc. The network card can determine the target receiving location of the target network traffic based on the flow direction of the target network traffic, and send the target network traffic to the corresponding target receiving location. For example, the network card can detect the flow direction of the above-mentioned target network traffic. If the network card detects that the above-mentioned network traffic includes the first network traffic from the external device, that is, the first network traffic belongs to the network traffic of the receiving flow direction, then the corresponding target network traffic belongs to the traffic of the receiving flow direction, the network card can determine that the target receiving location can be the above-mentioned protocol stack, and the network card can send the above-mentioned target network traffic to the above-mentioned protocol stack.
[0057] If the network card detects that the above-mentioned network traffic includes the second network traffic from the protocol stack, that is, the second network traffic belongs to the network traffic of the sending direction, then the corresponding target network traffic belongs to the traffic of the sending direction. The network card can determine that the target receiving location can be the above-mentioned external device, so that the network card can send the above-mentioned target network traffic to the above-mentioned corresponding external device.
[0058] Through this embodiment, the network card can determine the target receiving position of the target network traffic based on the flow direction of the network traffic, thereby improving the transmission efficiency of the network communication.
[0059] In one embodiment, the network traffic is redirected to the first target port in the virtual switch through the network traffic control program, including: redirecting the network traffic that does not carry a preset identifier to the first target port in the virtual switch through the network traffic control program; and the virtual switch is used to add the preset identifier to the network traffic through the second target port and the network traffic control program, and then redirect it to the network card.
[0060] In this embodiment, when redirecting network traffic, the network card can also mark and identify the network traffic through a network traffic control program. Since network traffic reaches the network card multiple times during the network traffic redirection process, for example, network traffic first reaches the network card when initiating transmission and then reaches the network card again after passing through the virtual switch, the network card needs to identify which network traffic needs to be redirected to the virtual switch and which network traffic needs to be sent to the target receiving location.
[0061] Among them, the network card can identify the identifier in the network traffic through the above-mentioned network traffic control program. For example, the network card can identify whether there is a preset identifier in each network traffic through the network traffic control program. The network card can redirect the above-mentioned network traffic that does not carry the preset identifier to the first target port in the virtual switch, including but not limited to the uplink port and the virtual bridge port. Among them, the above-mentioned preset identifier can be an identifier carried by the network traffic, used to indicate that the network traffic has been processed by the virtual switch. The virtual switch can load the network traffic control program on the second target port, and use the network traffic control program to add a preset identifier to the network traffic, and redirect the network traffic with the preset identifier added to the above-mentioned network card. Among them, the above-mentioned preset identifier can be a field in the data packet that marks the network traffic.
[0062] In one embodiment, determining the target network traffic from the network traffic includes: obtaining network traffic carrying a preset identifier from the network traffic as the target network traffic.
[0063] In this embodiment, the virtual switch can mark the received network traffic, and the network traffic passing through the virtual switch can carry the aforementioned preset identifier. After receiving the network traffic, the network card can detect whether the network traffic carries the preset identifier. The network traffic with the preset identifier can be used as the target network traffic for the next step of receiving or sending. The network card can then extract the network traffic carrying the preset identifier from the network traffic as the target network traffic and send the target network traffic to the target receiving location, such as a protocol stack or external device.
[0064] Specifically, to resolve loops caused by redirection on the server's network interface card (NIC), take the receiving direction as an example. When a network traffic packet is received on the server NIC eth0 and redirected to the virtual switch via the eBPF program, it eventually returns to the receiving direction of the server NIC eth0. This triggers further eBPF redirection, resulting in a loop. Therefore, after the NIC redirects the network traffic, the virtual switch can set the skb mark (socket buffer mark, kernel packet identifier) of the network traffic to a special value of 0xffff as the preset identifier. When the NIC detects a skb mark of 0xffff during the receiving process, it indicates that the packet has been redirected. The NIC can then proceed with the normal NIC receiving process. The NIC also performs similar processing on the sending direction. In the receiving direction, the NIC uses the network traffic control program to redirect network traffic with a skb mark other than 0xffff to the uplink port 1 of the virtual switch. The network traffic packets are then forwarded on the virtual switch. Network traffic that needs to be processed by the server's protocol stack can be transmitted to the br0 port of the virtual switch. The virtual switch loads the network traffic control program on br0, sets the skb mark of the network traffic to 0xffff, that is, adds a preset identifier, and redirects it to the receiving direction of the server's network card. The network card sends the target network traffic carrying the preset identifier to the kernel protocol stack.
[0065] In the outbound direction, the NIC uses the network traffic control program to redirect network traffic with a skb mark other than 0xffff to the virtual bridge port br0 of the virtual switch. The network traffic packets are forwarded by the virtual switch and transmitted to its uplink port, port1. The virtual switch loads the network traffic control program on port1, sets the skb mark of the network traffic to 0xffff, adding a preset identifier, and redirects it to the outbound direction of the server's NIC. The NIC then sends the target network traffic carrying the preset identifier to the external device, completing traffic transmission. The skb can be the sk_buff (socket buffer) structure in the Linux kernel, which describes a network packet.
[0066] Through the above embodiment, the network card can detect whether the network traffic carries a preset identifier, thereby implementing diversion processing of the network traffic and avoiding repeated redirection, thereby improving the efficiency of network communication.
[0067] In an exemplary embodiment, Figure 3 As shown, a network communication method is provided, which is applied to Figure 1 The virtual switch in FIG. 1 is taken as an example to illustrate the method, which includes the following steps S302 to S304. In which:
[0068] Step S302: Acquire network traffic sent by the network card through the first target port, and forward the network traffic to the second target port.
[0069] The network card can be a physical network card in the server. The network card has a corresponding IP address. Specifically, the network card can be eth0 (the first Ethernet port). The network traffic can be from an external device or internal to the server where the network card is located.
[0070] The above-mentioned network card can communicate with the virtual switch based on the network traffic control program, and the above-mentioned network card may not be connected to the above-mentioned virtual switch. The above-mentioned network card may be provided with a network traffic control program. Among them, the above-mentioned network traffic control program may be a program based on eBPF technology. eBPF (Extended Berkeley Packet Filter) is a kernel technology that allows users to intercept and process kernel events by loading eBPF programs without modifying the kernel source code. Therefore, when the network card is performing network traffic communication, it can load the network traffic control program in the network card and redirect the network traffic through the network traffic control program, for example, redirecting it to the first target port in the virtual switch. Among them, the above-mentioned first target port can be determined based on the direction of the network traffic. For example, for the process of receiving traffic and sending traffic, the first target port in the above-mentioned virtual switch may be different.
[0071] The virtual switch may receive the network traffic through the first destination port and forward the traffic. A second destination port may be provided in the virtual switch, and the second destination port may be a port of a different nature from the first destination port. The virtual switch may then transmit the network traffic to the second destination port.
[0072] Step S304, at the second target port, redirect the network traffic to the network card through the network traffic control program; the network card is used to determine the target network traffic from the network traffic and send the target network traffic to the target receiving location corresponding to the network traffic.
[0073] The virtual switch may be provided with the network traffic control program, which may be run on the second target port as needed. Thus, the virtual switch may process network traffic on the second target port, load the network traffic control program, and redirect the processed network traffic to the network card via the second target port using the network traffic control program.
[0074] Different network traffic flow directions may have different target receiving locations. For example, when receiving traffic, the target receiving location may be the protocol stack of the server kernel. When sending traffic, the target receiving location may be an external device, such as another external server, personal computer, mobile phone, or other network device.
[0075] After the network card receives network traffic from the virtual switch, it can determine the target network traffic from the network traffic. The target network traffic can be network traffic with a preset identifier. That is, the virtual switch can identify the network traffic accordingly, and the network card can determine the direction of the network traffic based on whether the network traffic carries the preset identifier. For example, it can determine whether to send the traffic to the virtual switch or to the target receiving location. The network traffic that needs to be sent to the target receiving location can be determined as the target network traffic. After the network card determines the target network traffic from the network traffic, it can send the target network traffic to the target receiving location.
[0076] The aforementioned network traffic control program can be loaded into the tc classact hook (network traffic control rule hook) of the virtual switch. TC (Traffic Control) is the network traffic control subsystem in the Linux kernel; clsact (queuing discipline) is a queuing discipline in network traffic control, which can load the aforementioned eBPF program. clsact is a queue-less queuing discipline that supports both inbound and outbound traffic.
[0077] In the above-mentioned network communication method, the target receiving location of the network traffic is determined, and the network traffic is redirected to the first target port in the virtual switch through a network traffic control program. The network traffic is then redirected to the network card through the second target port in the virtual switch and the network traffic control program. The network card determines the target network traffic from the network traffic and sends the target network traffic to the target receiving location. Compared to the traditional communication method of connecting the network card to the virtual switch and then migrating the address of the network card, this solution redirects the network traffic by setting a network traffic control program at the ports of the network card and the virtual switch, thereby achieving network communication for the virtual machine based on the original settings of the network card, thereby improving the stability of network communication.
[0078] In one embodiment, the network traffic is redirected to the network card through the network traffic control program, including: querying the port index table through the network traffic control program to obtain the third port index corresponding to the network card; and redirecting the network traffic to the network card according to the third port index.
[0079] In this embodiment, the above-mentioned network card has a corresponding port index. For example, the index corresponding to the port of the network card. The above-mentioned network traffic control program is provided with a port index table, and the virtual switch can query the port index table through the network traffic control program to obtain the third port index corresponding to the above-mentioned network card. Among them, the third port index represents the index of the port of the above-mentioned network card used to receive network traffic. Thus, the virtual switch redirects the network traffic to the network card according to the third port index, so that the network card can receive the above-mentioned network traffic. Among them, for network traffic with different flow directions, the virtual switch can load the above-mentioned network traffic control program on different ports to determine the third port index.
[0080] For example, for the first network traffic in the receiving direction, the virtual switch can load the network traffic control program on the br0 port to determine the above-mentioned third port index; for the second network traffic in the sending direction, the virtual switch can load the above-mentioned network traffic control program on the port1 port to determine the above-mentioned third port index.
[0081] Through this embodiment, the virtual switch can use the network traffic control program in combination with the port index table to determine the third port index of the network card, thereby achieving network traffic redirection and improving the stability of network communication.
[0082] In one embodiment, the above-mentioned network traffic is redirected to the above-mentioned network card through the above-mentioned network traffic control program, including: adding a preset identifier to the above-mentioned network traffic through the above-mentioned network traffic control program, and redirecting the above-mentioned network traffic carrying the above-mentioned preset identifier to the above-mentioned network card; the above-mentioned network card is used to obtain the network traffic carrying the above-mentioned preset identifier from the above-mentioned network traffic as the above-mentioned target network traffic.
[0083] In this embodiment, the preset identifier may be an identifier carried by the network traffic, indicating that the network traffic has been processed by the virtual switch. The virtual switch may load a network traffic control program on the second target port, use the network traffic control program to add the preset identifier to the network traffic, and redirect the network traffic with the preset identifier to the network card. The preset identifier may be a field in a data packet that marks the network traffic.
[0084] The network traffic passing through the virtual switch can carry the aforementioned preset identifier. After receiving the network traffic, the network card can detect whether the network traffic carries the preset identifier. The network traffic with the preset identifier can be used as the target network traffic for the next receiving or sending process. The network card can then extract the network traffic carrying the preset identifier from the network traffic as the target network traffic and send the target network traffic to the target receiving location, such as a protocol stack or external device.
[0085] Specifically, on the receiving side, the NIC uses the network traffic control program to redirect network traffic with a non-0xffff skb mark to the virtual switch's uplink port, port 1. Network traffic packets are forwarded on the virtual switch. Network traffic that needs to be processed by the server's protocol stack can be transmitted to the virtual switch's port br0. The virtual switch loads the network traffic control program on br0, sets the skb mark of the network traffic to 0xffff, adding the preset identifier, and redirects it to the server's NIC's receiving direction. The NIC then sends the target network traffic carrying the preset identifier to the kernel protocol stack.
[0086] In the outbound direction, the NIC uses the network traffic control program to redirect network traffic with a skb mark other than 0xffff to the virtual bridge port br0 of the virtual switch. The network traffic packets are forwarded on the virtual switch and transmitted to the uplink port br1 of the virtual switch. The virtual switch loads the network traffic control program on port 1, sets the skb mark of the network traffic to 0xffff, adding the preset identifier, and redirects the traffic to the outbound direction of the server's NIC. The NIC then sends the target network traffic carrying the preset identifier to the external device, completing traffic forwarding.
[0087] Through this embodiment, the virtual switch can add a preset identifier to the network traffic, so that the network card can combine the preset identifier to divert the network traffic and avoid repeated redirection, thereby improving the efficiency of network communication.
[0088] In an exemplary embodiment, Figure 4 As shown, Figure 4 The flowchart of a network communication method in another embodiment is shown. In this embodiment, the network card may be eth0, and the network traffic control program may be an eBPF program. The network card is not directly connected to the virtual switch. Instead, the eBPF program is loaded on the network card to redirect traffic to the virtual switch. The IP address of the server's network card remains unchanged and is not migrated.
[0089] In the receiving direction, after receiving network traffic, the NIC can load an eBPF program to redirect network traffic with a skb mark other than 0xffff to the virtual switch's uplink port, port 1. Network traffic packets are forwarded on the virtual switch. Network traffic that needs to be processed by the server's protocol stack can be transmitted to the virtual switch's port br0. The virtual switch loads a network traffic control program on br0, sets the skb mark of the network traffic to 0xffff, adding a preset identifier, and redirects it to the server's NIC's receiving direction. The NIC then sends the target network traffic carrying the preset identifier to the kernel protocol stack. The eBPF program can be loaded into the tc classact hook (network traffic control rule hook) of the NIC and virtual switch, including both outbound and inbound directions, with different ports corresponding to different directions.
[0090] The process of sending direction can be as follows Figure 5 As shown, Figure 5 The following is a flow chart of a network communication method in another embodiment. In the sending direction, the protocol stack sends network traffic packets, which arrive at the sending direction of the network card through routing. The network card, through the network traffic control program, redirects network traffic with a skb mark other than 0xffff to the virtual bridge port br0 of the virtual switch. The network traffic packets are forwarded on the virtual switch and transmitted to the uplink port port 1 of the virtual switch. The virtual switch loads the network traffic control program on port 1, sets the skb mark of the network traffic to 0xffff, i.e., adds a preset identifier, and redirects the packets to the sending direction of the server's network card. The network card then sends the target network traffic carrying the preset identifier to the external device, thus achieving traffic transmission.
[0091] Through the above embodiment, by setting a network traffic control program at the ports of the network card and the virtual switch, the network traffic redirection is achieved using the network traffic control program, thereby eliminating the need to migrate the IP address of the network card, decoupling the upper-layer application platform and the underlying specific Linux system, and achieving network communication for virtual machines using the original settings of the network card, thereby improving the stability of network communication. In addition, the programmability of the eBPF program can flexibly control which traffic needs to be redirected. This can protect necessary control traffic from being affected by the virtual switch, and also isolate business flows and control flows, thereby improving network reliability.
[0092] It should be understood that, although the various steps in the flowcharts involved in the various embodiments described above are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the flowcharts involved in the various embodiments described above can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of steps or stages in other steps.
[0093] Based on the same inventive concept, embodiments of the present application also provide a network communication device for implementing the aforementioned network communication method. The implementation solution provided by this device is similar to the implementation solution described in the aforementioned method. Therefore, the specific limitations in one or more network communication device embodiments provided below can be found in the above-mentioned limitations on the network communication method and will not be further elaborated here.
[0094] In an exemplary embodiment, Figure 6 As shown, a network communication device is provided, including: a first acquisition module 600, a first redirection module 602 and a transmission module 604, wherein:
[0095] The first acquisition module 600 is used to acquire the network traffic to be processed and determine a target receiving location of the network traffic.
[0096] The first redirection module 602 is used to load the network traffic control program and redirect the network traffic to the first target port in the virtual switch through the network traffic control program; the virtual switch is used to redirect the network traffic to the network card through the second target port and the network traffic control program.
[0097] The transmission module 604 is configured to determine target network traffic from the network traffic and send the target network traffic to the target receiving location.
[0098] In one embodiment, the first redirection module 602 is used to determine that the first target port is the uplink port corresponding to the virtual switch if the network traffic includes the first network traffic from the external device, query the port index table through the network traffic control program, determine the first port index corresponding to the uplink port, and redirect the first network traffic to the uplink port according to the first port index; if the network traffic includes the second network traffic from the protocol stack, determine that the first target port is the virtual bridge port corresponding to the virtual switch, query the port index table through the network traffic control program, determine the second port index corresponding to the uplink port, and redirect the second network traffic to the virtual bridge port according to the second port index.
[0099] In one embodiment, the transmission module 604 is used to send the target network traffic to the protocol stack if the network traffic includes the first network traffic from the external device; and to send the target network traffic to the external device corresponding to the target network traffic if the network traffic includes the second network traffic from the protocol stack.
[0100] In one embodiment, the first redirection module 602 is used to redirect the network traffic that does not carry the preset identifier to the first target port in the virtual switch through the network traffic control program; the virtual switch is used to add the preset identifier to the network traffic through the second target port and the network traffic control program, and then redirect it to the network card.
[0101] In one embodiment, the transmission module 604 is configured to obtain network traffic carrying a preset identifier from the network traffic as the target network traffic.
[0102] In an exemplary embodiment, Figure 7 As shown, a network communication device is provided, including: a second acquisition module 700 and a second redirection module 702, wherein:
[0103] The second acquisition module 700 is configured to acquire network traffic sent by the network card through the first target port, and forward the network traffic to the second target port.
[0104] The second redirection module 702 is used to redirect the above-mentioned network traffic to the above-mentioned network card through the above-mentioned network traffic control program at the above-mentioned second target port; the above-mentioned network card is used to determine the target network traffic from the above-mentioned network traffic and send the above-mentioned target network traffic to the target receiving location corresponding to the above-mentioned network traffic.
[0105] In one embodiment, the second redirection module 702 is configured to query the port index table through the network traffic control program to obtain a third port index corresponding to the network card; and redirect the network traffic to the network card according to the third port index.
[0106] In one embodiment, the second redirection module 702 is used to add a preset identifier to the network traffic through the network traffic control program, and redirect the network traffic carrying the preset identifier to the network card; the network card is used to obtain the network traffic carrying the preset identifier from the network traffic as the target network traffic.
[0107] Each module in the above-mentioned network communication device can be implemented in whole or in part through software, hardware, or a combination thereof. Each of the above-mentioned modules can be embedded in or independent of the processor of the computer device in hardware form, or can be stored in the memory of the computer device in software form, so that the processor can call and execute the corresponding operations of each of the above modules.
[0108] In an exemplary embodiment, a computer device is provided. The computer device may be a network card or a virtual switch. The internal structure diagram thereof may be as follows: Figure 8 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, abbreviated as I / O) and a communication interface. The processor, memory and input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a network communication method is implemented.
[0109] Those skilled in the art will understand that Figure 8 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0110] In an exemplary embodiment, a computer device is provided, including a memory and a processor. The memory stores a computer program, and the processor implements the above-mentioned network communication method when executing the computer program.
[0111] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the network communication method described above is implemented.
[0112] In one embodiment, a computer program product is provided, including a computer program, which implements the above-mentioned network communication method when executed by a processor.
[0113] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations.
[0114] Those skilled in the art will appreciate that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the above-mentioned embodiments. In particular, any reference to memory, database, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in the various embodiments provided herein may include at least one of a relational database and a non-relational database. Non-relational databases may include, but are not limited to, distributed databases based on blockchains. The processor involved in the various embodiments provided herein may be, but are not limited to, a general-purpose processor, a central processing unit (CPU), a graphics processing unit (GPU), a digital signal processor (DSP), a programmable logic unit (PLC), a data processing logic unit based on quantum computing, an artificial intelligence (AI) processor, and the like.
[0115] The technical features of the above embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0116] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.
Claims
1. A network communication method, characterized in that: Applied to a network card, the method includes: Obtaining network traffic to be processed and determining a target receiving location for the network traffic; Loading a network traffic control program, and redirecting the network traffic to a first target port in a virtual switch through the network traffic control program; the virtual switch is used to redirect the network traffic to the network card through a second target port and the network traffic control program; Target network traffic is determined from the network traffic, and the target network traffic is sent to the target receiving location.
2. The method according to claim 1, characterized in that The step of redirecting the network traffic to the first target port in the virtual switch by the network traffic control program includes: If the network traffic includes first network traffic from an external device, determining that the first target port is an uplink port corresponding to the virtual switch, querying a port index table through the network traffic control program to determine a first port index corresponding to the uplink port, and redirecting the first network traffic to the uplink port according to the first port index; If the network traffic includes second network traffic from the protocol stack, the first target port is determined to be the virtual bridge port corresponding to the virtual switch, the port index table is queried through the network traffic control program, the second port index corresponding to the uplink port is determined, and the second network traffic is redirected to the virtual bridge port according to the second port index.
3. The method according to claim 1, characterized in that The sending the target network traffic to the target receiving location includes: If the network traffic includes first network traffic from an external device, sending the target network traffic to a protocol stack; If the network traffic includes second network traffic from a protocol stack, the target network traffic is sent to an external device corresponding to the target network traffic.
4. The method according to any one of claims 1 to 3, characterized in that The step of redirecting the network traffic to the first target port in the virtual switch by the network traffic control program includes: The network traffic that does not carry the preset identifier is redirected to the first target port in the virtual switch through the network traffic control program; the virtual switch is used to add the preset identifier to the network traffic through the second target port and the network traffic control program, and then redirect it to the network card; Determining target network traffic from the network traffic includes: The network traffic carrying a preset identifier is obtained from the network traffic as the target network traffic.
5. A network communication method, characterized in that: Applied to a virtual switch, the method includes: Acquire network traffic sent by the network card through the first target port, and forward the network traffic to the second target port; At the second target port, the network traffic is redirected to the network card through the network traffic control program; the network card is used to determine target network traffic from the network traffic and send the target network traffic to a target receiving location corresponding to the network traffic.
6. The method according to claim 5, characterized in that The step of redirecting the network traffic to the network card through the network traffic control program includes: Querying a port index table through the network traffic control program to obtain a third port index corresponding to the network card; The network traffic is redirected to the network card according to the third port index.
7. The method according to any one of claims 5 to 6, characterized in that The step of redirecting the network traffic to the network card through the network traffic control program includes: The network traffic control program adds a preset identifier to the network traffic, and redirects the network traffic carrying the preset identifier to the network card; the network card is used to obtain the network traffic carrying the preset identifier from the network traffic as the target network traffic.
8. A network communication device, characterized in that: Applied to a network card, the device includes: A first acquisition module is used to acquire network traffic to be processed and determine a target receiving location of the network traffic; a first redirection module, configured to load a network traffic control program and redirect the network traffic to a first target port in the virtual switch through the network traffic control program; the virtual switch is configured to redirect the network traffic to the network card through a second target port and the network traffic control program; The transmission module is used to determine the target network traffic from the network traffic and send the target network traffic to the target receiving location.
9. A network communication device, characterized in that: Applied to a virtual switch, the device includes: A second acquisition module is used to acquire network traffic sent by the network card through the first target port and forward the network traffic to the second target port; The second redirection module is used to redirect the network traffic to the network card through the network traffic control program at the second target port; the network card is used to determine the target network traffic from the network traffic and send the target network traffic to the target receiving location corresponding to the network traffic.
10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.