An efficient TCP / IP datagram processing method and system
The DMA module is introduced in the network data newspaper transmission, replacing the network protocol stack processing data newspapers, and through DMA reading, writing and management driver, solves the problem of time sliced consumption and CPU resource occupation caused by network data newspaper transmission, and realizes the problem of CPU resource occupation. Efficient data newspapers and savings of CPU resources.
Patent Information
- Application Number
- CN202210557371.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-20
- Publication Date
- 2025-01-24
- Estimated Expiration
- 2042-05-20
AI Technical Summary
In the prior art, the time slice consumption caused by large copies of network datagrams and the CPU resource occupation caused by inter-process data copying.
By introducing DMA (direct memory access) module, replacing the network protocol stack to process the datagram that users will send, and through the DMA read, write and manage the driver, provide read and write interfaces for the user's server -side application to reduce the data of the CPU participation. Copy process.
Effectively increased the transmission rate of the data newspaper, reduced the resource occupation of the CPU, and reduced the time over time during the transmission of the network data newspaper.
Smart Images

Figure CN115103036B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer communications, and in particular to an efficient network message transmission method, and in particular to an efficient TCP / IP (Transmission Control Protocol / Internet Protocol) datagram processing method. Background Art
[0002] At present, the general process of computer operating systems (such as Linux) in processing network data is that the application program sends datagrams (data messages) to the network protocol stack through the Socket interface, and the network card driver obtains the data processed by the protocol stack from the network protocol stack. However, in actual scenarios, the user-mode network protocol stack transmits datagrams with the application program, while the kernel-mode network protocol stack interacts with the network card driver program.
[0003] The TCP server and client are two processes, and the two communicate via TCP / IP sockets. The server is a process that uses local information to establish a socket and monitors whether there is a socket connection; the client is a process that uses the local information of the server to establish a socket and uses it as a target to request a connection.
[0004] For kernel-mode programs and user-mode programs that interact with datagrams based on the socket mechanism, multiple datagram copy processes will occur, whether from the user program to the protocol stack or from the protocol stack to the network card driver. Taking the conventional TCP / IP communication in the prior art as an example, the conventional network card data transmission will have three datagram copy processes involving the CPU: 1. User mode to kernel mode (sent by the socket to the network protocol stack); 2. Protocol stack to network card driver (i.e., data received by the sk_buff of the network card driver); 3. The network card driver encapsulates the data packet into a MAC frame (which contains user data). This process will require a large number of time slices, which is the main bottleneck for improving the transmission rate of network datagrams. Therefore, solving the time slices consumed by the large number of copies required during the transmission process will be an effective means to improve the transmission efficiency of network datagrams. Summary of the invention
[0005] The present invention aims to solve two technical problems. First, the time slice consumption caused by the large number of copies required in the current network datagram transmission process leads to a low datagram transmission rate; second, the CPU resource occupation problem caused by data copying between processes.
[0006] In view of the deficiencies in the prior art, the present invention provides an efficient TCP / IP datagram processing method, which includes:
[0007] When the server application sends data, execute step 1; when the server application receives data, execute step 2;
[0008] Step 1, the server application establishes a Socket connection by building a TCP server process, writes the datagram to be sent into the DMA, obtains the memory offset address and the written data size fed back by the DMA, and stores the memory offset address and the data size into the sk_buff of the network protocol stack through the Socket interface. The network card driver maps the datagram from the DMA to the virtual address according to the memory offset address and the data size in the sk_buff. The network card driver reads the datagram in the DMA according to the virtual address to replace part of the data in the sk_buff, and encapsulates the replaced sk_buff into a MAC frame data packet, and the physical network card sends the MAC frame out;
[0009] Step 2. After the physical network card receives the MAC frame data packet, the network card driver will unpack it to obtain the sk_buff data packet, cut the data part of the sk_buff data packet to DMA, use the memory address returned by DMA and the written data size as the data part of the sk_buff data packet, obtain a replacement data packet, and hand the replacement data packet to the network protocol stack. The server application receives the memory address and the data size through the network protocol stack, and the server application maps the memory address to a user-mode virtual address. The server application accesses the virtual address to receive the data packet in DMA.
[0010] In the efficient TCP / IP datagram processing method, the step 1 of replacing part of the data in sk_buff specifically includes: replacing the offset address plus the data size of the user data in sk_buff with the datagram read from DMA.
[0011] The efficient TCP / IP datagram processing method, wherein the DMA is used to replace the network protocol stack to process the datagram to be sent by the user, and provides a read-write interface for the user-mode server application through the DMA read-write and management driver. The DMA read-write and management driver includes DMA read-write address allocation and conflict detection, and allocates a write address and determines whether the address can be written again based on whether the written data is read.
[0012] The efficient TCP / IP datagram processing method, wherein the network card driver is transplanted according to the device used to be applicable to a variety of network card devices and virtual network cards;
[0013] In step 1, the network card driver obtains a data packet from the kernel network protocol stack, parses the data packet to obtain the offset address and data size after DMA write, and maps the address to the network card driver, thereby obtaining the datagram of the server application, replacing the data part of sk_buff and encapsulating it into a MAC frame format;
[0014] In step 2, the network card driver will parse the MAC frame into sk_buff, write the parsed data into DMA, and replace the corresponding data in sk_buff with the memory offset address and data size fed back from DMA, and then hand over the replaced sk_buff to the network protocol stack for processing.
[0015] The present invention also proposes an efficient TCP / IP datagram processing system, which includes:
[0016] When the server application sends data, the sending module is called; when the server application receives data, the receiving module is called;
[0017] The sending module is used to enable the server application to establish a Socket connection by building a TCP server process, write the datagram to be sent into the DMA, obtain the memory offset address and the written data size fed back by the DMA, and store the memory offset address and the data size into the sk_buff of the network protocol stack through the Socket interface. The network card driver maps the datagram from the DMA to the virtual address according to the memory offset address and the data size in the sk_buff. The network card driver reads the datagram in the DMA according to the virtual address to replace part of the data in the sk_buff, and encapsulates the replaced sk_buff into a MAC frame data packet, and the physical network card sends the MAC frame out;
[0018] The receiving module is used to enable the physical network card to receive the MAC frame data packet, and then the network card driver will unpack it to obtain the sk_buff data packet, cut the data part of the sk_buff data packet to the DMA, use the memory address returned by the DMA and the written data size as the data part of the sk_buff data packet, obtain the replacement data packet, and hand over the replacement data packet to the network protocol stack. The server application receives the memory address and the data size through the network protocol stack, and the server application maps the memory address to a user-mode virtual address. The server application accesses the virtual address to receive the data packet in the DMA.
[0019] In the efficient TCP / IP datagram processing system, the sending module replaces part of the data in sk_buff by replacing the offset address plus the data size of the user data in sk_buff with the datagram read from DMA.
[0020] The efficient TCP / IP datagram processing system, wherein the DMA is used to replace the network protocol stack to process the datagram to be sent by the user, and provides a read-write interface for the user-mode server application through the DMA read-write and management driver. The DMA read-write and management driver includes DMA read-write address allocation and conflict detection, and allocates a write address and determines whether the address can be written again based on whether the written data is read.
[0021] The efficient TCP / IP datagram processing system, wherein the network card driver is transplanted according to the device used to be applicable to a variety of network card devices and virtual network cards;
[0022] In the sending module, the network card driver obtains the data packet from the kernel network protocol stack, parses the data packet to obtain the offset address and data size after DMA writing, and maps the address to the network card driver, thereby obtaining the datagram of the server application, replacing the data part of sk_buff and encapsulating it into MAC frame format;
[0023] In the receiving module, the network card driver will parse the MAC frame into sk_buff, write the parsed data into DMA, and replace the corresponding data in sk_buff with the memory offset address and data size fed back from DMA, and then hand over the replaced sk_buff to the network protocol stack for processing.
[0024] The present invention also proposes a storage medium for storing a program for executing any one of the efficient TCP / IP datagram processing methods.
[0025] The present invention also provides a client, which is used for any of the above-mentioned efficient TCP / IP datagram processing systems.
[0026] It can be seen from the above scheme that the advantages of the present invention are:
[0027] The datagram processing method provided by the present invention can be applied to a network device, which should have the basic functions of an operating system and a network subsystem as well as a user-mode network protocol stack and a kernel network protocol stack, and should also include a direct memory access DMA (Direct Memory Access) module. The method provided by the present invention can effectively improve the transmission rate of datagrams and reduce the resource occupation of the CPU. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 DMA reading, writing and management driver of the present invention;
[0029] Figure 2 The present invention is a flowchart of the process of sending a data packet by the network card driver;
[0030] Figure 3 The present invention is a flowchart of the process of receiving a data packet by the network card driver;
[0031] Figure 4 Flowchart for sending and receiving datagrams for the server application;
[0032] Figure 5 The overall framework and flow chart of datagram transmission of the present invention;
[0033] Figure 6 The overall framework and flow chart of datagram reception of the present invention;
[0034] Figure 7 It is a diagram of the data transmission process of the prior art;
[0035] Figure 8 This is a diagram of the data transmission process of the present invention. DETAILED DESCRIPTION
[0036] The present invention solves the above technical problem by providing an efficient TCP / IP datagram processing method. Different from the traditional method, the data sending process is as follows:
[0037] After the application establishes a Socket connection, the datagram to be sent is written into the DMA, and the memory offset address and the written data size fed back after writing into the DMA are handed over to the network protocol stack through the Socket interface. After being processed by the protocol stack (processing address connection information and verification, etc.), the network card driver obtains sk_buff (skb) from the protocol stack. Since only virtual addresses can be operated in the operating system, the memory offset address and data size in sk_buff are mapped to the virtual address of the driver at this time, so as to read the user's datagram, replace the skb data part and complete the encapsulation of the MAC frame in the network card driver, and then the network card sends the packet out. The sending target is the connection target established by the socket, and the skb data packet contains the source address IP and the destination address IP.
[0038] Data receiving process:
[0039] After the network card receives the data packet, the network card driver will unpack it, write the data part to DMA, and replace the original network data part with the memory offset address and data size obtained after writing, completing the replacement process of the skb data part, and then handing the replaced skb to the protocol stack. After being processed by the protocol stack, the application receives the memory offset address and data size, and completes the receiving process by reading the datagram in the mapped virtual memory.
[0040] This method introduces DMA to replace the protocol stack to process the datagrams to be sent by the user. A set of drivers for DMA reading, writing and management is implemented in the kernel and a read and write interface is provided for user-mode programs, such as Figure 1 As shown, the DMA read / write and management driver includes DMA read / write address allocation and conflict detection functions. By allocating write addresses and judging whether the address can be written again based on whether the written data is read, data overwriting that may occur when writing data and data misreading that may occur when reading data (i.e., the data read according to the address when writing is different from the data written) can be avoided.
[0041] The method also implements a set of network card drivers, which can be transplanted according to the device used and can be applied to a variety of network card devices and virtual network cards. Figure 2 The driver shown in the figure processes the datagram sent. The driver obtains the data packet from the kernel protocol stack, parses the data packet to obtain the offset address and data size after DMA write, and maps the address to the driver to obtain the datagram of the application, replaces the data part of the skb and encapsulates it into the MAC frame format. Figure 3 The driver shown in the figure processes the received datagram. The driver parses the MAC frame into the sk_buff format, writes the parsed data part into the DMA, replaces the memory offset address and data size obtained from the DMA feedback with the data part in the sk_buff, and then hands the replaced skb to the protocol stack for processing through netif_rx().
[0042] In order to make the above features and effects of the present invention more clearly understood, embodiments are given below and described in detail with reference to the accompanying drawings.
[0043] Datagram sending process:
[0044] like Figure 5 The overall framework and process of datagram sending are shown as follows: Figure 4 The user state shown in the figure first builds the TCP server process with the conventional TCP server establishment process, which includes creating a socket descriptor and binding the IP address, protocol type, port number, etc., listening to and connecting to the TCP client, and then writing the datagram to be sent to the DMA, saving the memory address returned by the DMA write function, and entering the DMA driver's write function process. Figure 1 As shown. The memory address and the size of the data to be written are written to the socket descriptor by calling the write() function. At this time, the user-mode application completes the sending process. Figure 2As shown in the figure, the data written to the socket descriptor is processed by the network protocol stack and then obtained by the network card driver from the kernel state protocol stack in the form of a data packet in the form of sk_buff. The network card driver parses sk_buff to obtain its data portion, namely the memory address and data size. The driver maps the memory address to the kernel state virtual memory address through the ioremap() function according to the data size. At this time, the data in the virtual address is the network datagram that the application process needs to send. The network card driver replaces the data portion in sk_buff with the datagram, and encapsulates skb into a MAC frame format data packet, which is then processed by the network card. At this point, the datagram sending process is completed.
[0045] like Figure 7 As shown, it is the data transmission process of the prior art. Figure 8 As shown, the data transmission process of the present invention, the method of the present invention transmits only the DMA offset address and datagram size for storing user datagrams in this process, so the sk_buff in the network card driver does not really need to be encapsulated as data for MAC frames, so the offset address plus datagram size of the user data that should be stored in the sk_buff here needs to be replaced with the datagram that the user needs to send out obtained from the DMA with this information. Since only transmitting the two key data of DMA offset address and datagram size will be the tip of the iceberg compared to the data volume of the entire user's datagram, the present invention has a technical improvement in transmission efficiency compared to the prior art.
[0046] Datagram receiving process:
[0047] like Figure 6 The overall framework and process of datagram reception are shown as follows: Figure 3 As shown in the figure, the network card driver obtains the data packet in the MAC frame format from the physical network card. The driver parses the data packet in the MAC frame format into a data packet in the sk_buff format, writes the data part of the sk_buff data packet into the DMA, uses the memory offset address and the written data size returned by the DMA as the data part of the sk_buff data packet, and passes the replaced skb to the kernel protocol stack for processing through the netif_rx() function. After the protocol stack processes the skb data packet accordingly, Figure 4 The application shown can now read the socket descriptor through the read() function to obtain the memory address and datagram size. The application calls the mmap() function to map the received memory address to the current process, that is, it is mapped to a user-mode virtual address. The data accessed by the application at this address is the network datagram sent by the sender, and the datagram reception process is completed.
[0048] Therefore, the method provided by the present invention introduces DMA in this process, so only one data copy involving the CPU will occur: 1. (Process 1 mentioned above), DMA is responsible for moving data, and the CPU does not interfere with this process; 2. (Process 2 mentioned above), DMA is responsible for moving data; 3. (Process 3 mentioned above), DMA is also responsible for obtaining data by the network card, and the CPU does not interfere. Although the CPU of the present invention still needs to copy very little content, including the offset address of DMA and the size of the datagram, this amount of data is extremely small and can be ignored when compared with the traditional method. Therefore, the present invention will reduce at least three copy processes compared with the prior art.
[0049] This process uses a TCP-based communication process to describe the use of the datagram processing method of the present invention. Since the datagram to be sent is directly handed over to DMA for processing, the protocol stack only processes the memory address and data size, reducing the large amount of data copying processes that occur in the protocol stack, thereby effectively reducing the time overhead of the process. By introducing DMA as a data exchange between memory and memory, the CPU resource occupancy is reduced. It can be seen that the datagram processing method of the present invention can effectively improve the transmission rate of network datagrams between applications and network cards, and save the occupancy rate of CPU resources.
[0050] The following is a system embodiment corresponding to the above method embodiment. This embodiment can be implemented in conjunction with the above embodiment. The relevant technical details mentioned in the above embodiment are still valid in this embodiment. In order to reduce repetition, they are not repeated here. Accordingly, the relevant technical details mentioned in this embodiment can also be applied in the above embodiment.
[0051] The present invention also proposes an efficient TCP / IP datagram processing system, which includes:
[0052] When the server application sends data, the sending module is called; when the server application receives data, the receiving module is called;
[0053] The sending module is used to enable the server application to establish a Socket connection by building a TCP server process, write the datagram to be sent into the DMA, obtain the memory offset address and the written data size fed back by the DMA, and store the memory offset address and the data size into the sk_buff of the network protocol stack through the Socket interface. The network card driver maps the datagram from the DMA to the virtual address according to the memory offset address and the data size in the sk_buff. The network card driver reads the datagram in the DMA according to the virtual address to replace part of the data in the sk_buff, and encapsulates the replaced sk_buff into a MAC frame data packet, and the physical network card sends the MAC frame out;
[0054] The receiving module is used to enable the physical network card to receive the MAC frame data packet, and then the network card driver will unpack it to obtain the sk_buff data packet, cut the data part of the sk_buff data packet to the DMA, use the memory address returned by the DMA and the written data size as the data part of the sk_buff data packet, obtain the replacement data packet, and hand over the replacement data packet to the network protocol stack. The server application receives the memory address and the data size through the network protocol stack, and the server application maps the memory address to a user-mode virtual address. The server application accesses the virtual address to receive the data packet in the DMA.
[0055] In the efficient TCP / IP datagram processing system, the sending module replaces part of the data in sk_buff by replacing the offset address plus the data size of the user data in sk_buff with the datagram read from DMA.
[0056] The efficient TCP / IP datagram processing system, wherein the DMA is used to replace the network protocol stack to process the datagram to be sent by the user, and provides a read-write interface for the user-mode server application through the DMA read-write and management driver. The DMA read-write and management driver includes DMA read-write address allocation and conflict detection, and allocates a write address and determines whether the address can be written again based on whether the written data is read.
[0057] The efficient TCP / IP datagram processing system, wherein the network card driver is transplanted according to the device used to be applicable to a variety of network card devices and virtual network cards;
[0058] In the sending module, the network card driver obtains the data packet from the kernel network protocol stack, parses the data packet to obtain the offset address and data size after DMA writing, and maps the address to the network card driver, thereby obtaining the datagram of the server application, replacing the data part of sk_buff and encapsulating it into MAC frame format;
[0059] In the receiving module, the network card driver will parse the MAC frame into sk_buff, write the parsed data into DMA, and replace the corresponding data in sk_buff with the memory offset address and data size fed back from DMA, and then hand over the replaced sk_buff to the network protocol stack for processing.
[0060] The present invention also proposes a storage medium for storing a program for executing any one of the efficient TCP / IP datagram processing methods.
[0061] The present invention also provides a client, which is used for any of the above-mentioned efficient TCP / IP datagram processing systems.
Claims
1. An efficient TCP / IP datagram processing method, characterized in that: include: When the server application sends data, execute step 1; When the server application receives data, execute step 2; Step 1, the server application establishes a Socket connection by building a TCP server process, writes the datagram to be sent into the DMA, obtains the memory offset address and the written data size fed back by the DMA, and stores the memory offset address and the data size into the sk_buff of the network protocol stack through the Socket interface. The network card driver maps the datagram from the DMA to the virtual address according to the memory offset address and the data size in the sk_buff. The network card driver reads the datagram in the DMA according to the virtual address, replaces the data part in the sk_buff with the memory offset address and the data size, and encapsulates the replaced sk_buff into a MAC frame data packet. The physical network card sends the MAC frame out; Step 2. After the physical network card receives the MAC frame data packet, the network card driver will unpack it to obtain the sk_buff data packet, cut the data part of the sk_buff data packet to DMA, use the memory address returned by DMA and the written data size as the data part of the sk_buff data packet, obtain a replacement data packet, and hand the replacement data packet to the network protocol stack. The server application receives the memory address and the data size through the network protocol stack, and the server application maps the memory address to a user-mode virtual address. The server application accesses the virtual address to receive the data packet in DMA.
2. The efficient TCP / IP datagram processing method according to claim 1, characterized in that: Replacing part of the data in sk_buff in step 1 specifically includes: replacing the offset address plus the data size of the user data in sk_buff with the datagram read from DMA.
3. The efficient TCP / IP datagram processing method as claimed in claim 1, characterized in that: The DMA is used to replace the network protocol stack to process the datagrams to be sent by the user, and provides a read-write interface for the user-mode server application through the DMA read-write and management driver. The DMA read-write and management driver includes DMA read-write address allocation and conflict detection. It allocates a write address and determines whether the address can be written again based on whether the written data is read.
4. The efficient TCP / IP datagram processing method according to claim 1, characterized in that: The network card driver is transplanted according to the device used to adapt to a variety of network card devices and virtual network cards; In step 1, the network card driver obtains a data packet from the kernel network protocol stack, parses the data packet to obtain the offset address and data size after DMA write, and maps the address to the network card driver, thereby obtaining the datagram of the server application, replacing the data part of sk_buff and encapsulating it into a MAC frame format; In step 2, the network card driver will parse the MAC frame into sk_buff, write the parsed data into DMA, and replace the corresponding data in sk_buff with the memory offset address and data size fed back from DMA, and then hand over the replaced sk_buff to the network protocol stack for processing.
5. An efficient TCP / IP datagram processing system, characterized in that: include: The sending module is called when the server application sends data; The receiving module is called when the server application receives data; The sending module is used to enable the server application to establish a Socket connection by building a TCP server process, write the datagram to be sent into the DMA, obtain the memory offset address and the written data size fed back by the DMA, and store the memory offset address and the data size into the sk_buff of the network protocol stack through the Socket interface. The network card driver maps the datagram from the DMA to the virtual address according to the memory offset address and the data size in the sk_buff. The network card driver reads the datagram in the DMA according to the virtual address, replaces the data part in the sk_buff with the memory offset address and the data size, and encapsulates the replaced sk_buff into a MAC frame data packet. The physical network card sends the MAC frame out; The receiving module is used to enable the physical network card to receive the MAC frame data packet, and then the network card driver will unpack it to obtain the sk_buff data packet, cut the data part of the sk_buff data packet to the DMA, use the memory address returned by the DMA and the written data size as the data part of the sk_buff data packet, obtain the replacement data packet, and hand over the replacement data packet to the network protocol stack. The server application receives the memory address and the data size through the network protocol stack, and the server application maps the memory address to a user-mode virtual address. The server application accesses the virtual address to receive the data packet in the DMA.
6. The efficient TCP / IP datagram processing system as claimed in claim 5, characterized in that: The sending module replaces part of the data in sk_buff specifically by replacing the offset address plus the data size of the user data in sk_buff with the datagram read from DMA.
7. The efficient TCP / IP datagram processing system as claimed in claim 5, characterized in that: The DMA is used to replace the network protocol stack to process the datagrams to be sent by the user, and provides a read-write interface for the user-mode server application through the DMA read-write and management driver. The DMA read-write and management driver includes DMA read-write address allocation and conflict detection. It allocates a write address and determines whether the address can be written again based on whether the written data is read.
8. The efficient TCP / IP datagram processing system as claimed in claim 5, characterized in that: The network card driver is transplanted according to the device used to adapt to a variety of network card devices and virtual network cards; In the sending module, the network card driver obtains the data packet from the kernel network protocol stack, parses the data packet to obtain the offset address and data size after DMA writing, and maps the address to the network card driver, thereby obtaining the datagram of the server application, replacing the data part of sk_buff and encapsulating it into MAC frame format; In the receiving module, the network card driver will parse the MAC frame into sk_buff, write the parsed data into DMA, and replace the corresponding data in sk_buff with the memory offset address and data size fed back from DMA, and then hand over the replaced sk_buff to the network protocol stack for processing.
9. A storage medium for storing a program for executing any one of the efficient TCP / IP datagram processing methods according to claims 1 to 4.
10. A client, used in any one of the efficient TCP / IP datagram processing systems according to claims 5 to 8.
Citation Information
Patent Citations
Apparatus and method for realizing zero copy based on Linux operating system
CN101135980A