A socket network communication optimization method, system and medium
Using hook technology to intercept and rewrite socket network programming interface functions, and combining the application layer packet reassembly algorithm to reorganize data packets at the application layer, the problem of low network packet sending efficiency in Linux system socket network communication is solved, and the network communication rate is improved. and throughput effects.
Patent Information
- Application Number
- CN202411465008.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-01-21
- Estimated Expiration
- 2044-10-21
AI Technical Summary
The existing Linux systems do not have excellent processing of network packets in socket network communication, resulting in low network speed and throughput, and difficulty in optimizing by modifying Nagle algorithms or network card drivers.
Use hook technology to intercept calls to socket network programming interface functions, and use the rewritten socket network programming interface functions combined with the data packet reassembly algorithm provided by the application layer to reorganize the data packets in the application layer and then send them to the kernel layer to avoid sending them to Linux immediately. In the transmission queue buffer of the kernel protocol stack.
It improves the sending rate and throughput of socket network communication, reduces the number of system calls and the time for the bottom layer of the kernel network to apply for memory, and realizes network communication optimization without modifying the Nagle algorithm, network card driver or network communication parameters.
Smart Images

Figure CN118972472B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of socket network communication optimization, and in particular to a socket network communication optimization method, system and medium. Background Art
[0002] With the development of technology, Linux system has occupied an important position in various fields, especially in the fields of industrial control, server, finance, security and other industries. Network communication provides an indispensable technology for data transmission, and the speed, low latency and high throughput of network communication are important standards for the quality of network communication. Socket is a programming interface for network communication. It provides a mechanism that enables processes (or threads) on different computers or the same computer to communicate and exchange data through the network. At present, the Linux system is not good at sending and processing network packets in socket network communication, resulting in low network rate and throughput, which affects the transmission rate of the network. At present, the Nagle algorithm provided by the bottom layer of the Linux kernel driver is used to optimize network packets, but its network communication rate and throughput are still very inefficient. Network packets refer to smaller data packets transmitted in the TCP protocol (Transmission Control Protocol). Network packets usually refer to data packets with a smaller size relative to the MTU (Maximum Transmission Unit, MTU). When the size of the data packet is small, the TCP protocol may have problems such as higher overhead, increased latency and insufficient utilization of network bandwidth.
[0003] Nagle algorithm is an algorithm used to improve network transmission efficiency, which works in TCP protocol. The goal of this algorithm is to reduce the number of small data packets on the network to improve the throughput and efficiency of the network. Nagle algorithm processes small packets at the TCP protocol layer. If you need to optimize based on Nagle algorithm, you need to modify the kernel code or adjust the system parameters provided by Nagle algorithm. This tuning method is very difficult for most Linux system users, most of whom are not familiar with network protocols and the underlying principles of Nagle algorithm. In addition, you can also adjust the buffer size of the transmission queue buffer (tx queue buff, a buffer for storing data to be sent, used to queue data packets waiting to be sent on the network) to achieve more network data packets buffered at the bottom layer, adjust the transmission rate of the network card, etc. to optimize network communication. These optimization measures bring great difficulties to those who are not familiar with Linux kernel development, network protocol stack and network card driver. Summary of the invention
[0004] Technical problem to be solved by the present invention: In view of the above-mentioned problems in the prior art, a method, system and medium for optimizing socket network communication are provided. The present invention aims to achieve the purpose of optimizing network communication without modifying the Nagle algorithm of the Linux system, the Linux network card driver, the network communication parameters and the original socket network communication application.
[0005] In order to solve the above technical problems, the technical solution adopted by the present invention is:
[0006] A method for optimizing socket network communication comprises the following steps:
[0007] S1, uses hook technology to intercept the call of socket network programming interface function;
[0008] S2, using the rewritten socket network programming interface function combined with the data packet reassembly algorithm provided by the application layer, sends the data packet to the kernel layer after reassembly at the application layer, instead of immediately sending it to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of socket network communication.
[0009] Optionally, the use of the hook technology to intercept the call of the socket network programming interface function in step S1 includes the use of the hook technology to intercept the call of the server's accept connection request function accept, the write function write, and the send data function sendto, and the rewritten socket network programming interface function in step S2 includes the accept connection request function accept, the write function write, and the send data function sendto, wherein the steps executed by the rewritten accept connection request function accept include:
[0010] S101, obtaining the file descriptor socket_fd in the call of the accept function of the server to accept the connection request;
[0011] S102, judging whether the current socket network communication protocol type is TCP protocol or UDS protocol according to the file descriptor socket_fd, and judging whether the Nagle algorithm is enabled if it is TCP protocol, and jumping to step S103 if it is Nagle algorithm is enabled; and jumping to step S103 if it is UDS protocol;
[0012] S103, insert the file descriptor socket_fd into the initialized file descriptor linked list fd_list;
[0013] The rewritten write function write is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared with the file descriptor socket_fd in the file descriptor list fd_list inserted by the accept connection request function and matches, the data packet to be written is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack to optimize the sending rate and throughput of the socket network communication.
[0014] The rewritten data sending function sendto is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared and matched with the file descriptor socket_fd in the file descriptor linked list fd_list inserted by the connection request function accept, the data packet to be sent is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
[0015] Optionally, the use of hook technology to intercept the call of the socket network programming interface function in step S1 includes using the hook technology to intercept the call of the client's connection creation request function connect, write function write and send data function sendto, and the rewritten socket network programming interface function in step S2 includes the connection creation request function connect, write function write and send data function sendto, wherein the steps executed by the rewritten connection creation request function connect include:
[0016] S201, obtaining the file descriptor socket_fd in the call of the client's connection request function connect;
[0017] S202, judging the current socket network communication protocol type as TCP or UDS protocol according to the file descriptor socket_fd, judging whether the Nagle algorithm has been enabled if it is TCP protocol, and jumping to step S103 if it is UDS protocol; jumping to step S103;
[0018] S203, insert the file descriptor socket_fd into the initialized file descriptor linked list fd_list;
[0019] The rewritten write function write is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared with the file descriptor socket_fd in the file descriptor list fd_list inserted in the connection request function connect, and they match, the data packet to be written is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
[0020] The rewritten data sending function sendto is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared and matched with the file descriptor socket_fd in the file descriptor linked list fd_list inserted in the connection request function connect, the data packet to be sent is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
[0021] Optionally, the calling format of the rewritten write function write is: write(fd, buf, count), where fd is the file descriptor, buf is the data packet buffer address, and count is the count value.
[0022] Optionally, in step S2, when the data packet is reassembled at the application layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer and then sent to the kernel layer, for the call of the write function write, the data packet is reassembled at the application layer by using the rewritten write function write in combination with the data packet reassembly algorithm provided by the application layer and then sent to the kernel layer, including:
[0023] S301, intercepting the call of the write function write by hook technology, if the call of the write function write is intercepted, jump to step S302;
[0024] S302, determine whether the file descriptor fd in the call of the write function write exists in the file descriptor linked list fd_list. If not, directly execute the original write function original_write after hook interception and renaming, end and exit; otherwise, jump to step S303; wherein, the format of the call of the original write function original_write is: original_write (fd, buf, count), wherein fd is the file descriptor, buf is the data packet buffer address, and count is the count value;
[0025] S303, determine whether the count value count of the call of the write function write is less than the preset merge length merge_len, if so, jump to step S304; otherwise, execute the original write function original_write to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S304; the timer is used to periodically check the merge buffer merge_buff, if the timer expires and it is found that there is data in the merge buffer merge_buff, then execute the original write function original_write to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S304; if the timer expires and it is found that there is no data in the merge buffer merge_buff, restart the timer;
[0026] S304, copy the memory in the data packet buffer address buf called by the write function write to the merge buffer merge_buff, and return the count value count of the call of the write function write.
[0027] Optionally, the calling format of the rewritten data sending function sendto is: sendto(fd, buf,len, flags, to, tolen), where fd is the file descriptor, buf is the data packet buffer address, len is the count value, flags is the control flag, to is the target address, and tolen is the length of the target address.
[0028] Optionally, in step S2, when the data packet is reassembled at the application layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer and then sent to the kernel layer, for the call of the data sending function sendto, the data packet is reassembled at the application layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer and then sent to the kernel layer includes:
[0029] S401, intercepting the call of the data sending function sendto through the hook technology, if the call of the data sending function sendto is intercepted, jump to step S402;
[0030] S402, determine whether the file descriptor fd called by the data sending function sendto exists in the file descriptor linked list fd_list. If not, directly call the original data sending function original_sendto that is intercepted and renamed by hook to directly send the data to the kernel layer, end and exit; otherwise, jump to step S403; wherein, the format of the original data sending function original_sendto is: original_sendto (fd, buf, len, flags, to, tolen), wherein fd is the file descriptor, buf is the data packet buffer address, len is the count value, flags is the control flag, to is the target address, and tolen is the length of the target address;
[0031] S403, determine whether the count value len of the call of the data sending function sendto is less than the preset merge length merge_len, if so, jump to step S404; otherwise, execute the original data sending function original_sendto to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S404; the timer is used to periodically check the merge buffer merge_buff, if the timer expires and it is found that there is data in the merge buffer merge_buff, then execute the original data sending function original_sendto to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S404; if the timer expires and it is found that there is no data in the merge buffer merge_buff, restart the timer;
[0032] S404, copy the memory in the data packet buffer address buf of the call of the data sending function sendto to the merge buffer merge_buff, and return the count value len of the call of the data sending function sendto.
[0033] In addition, the present invention also provides a socket network communication optimization system, comprising a microprocessor and a memory connected to each other, wherein the microprocessor is programmed or configured to execute the socket network communication optimization method.
[0034] In addition, the present invention also provides a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute the socket network communication optimization method through a processor.
[0035] In addition, the present invention also provides a computer program product, including a computer program or instructions, wherein the computer program or instructions are programmed or configured to execute the socket network communication optimization method through a processor.
[0036] Compared with the prior art, the present invention mainly has the following advantages: the present invention aims to achieve the purpose of network communication optimization without modifying the Nagle algorithm of the Linux system, the Linux network card driver, the network communication parameters, and the original socket network communication application program. The socket network communication optimization method of the present invention includes: using hook technology to intercept the call of the socket network programming interface function; using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer to reassemble the data packet at the application layer and then send it to the kernel layer, rather than immediately sending it to the transmission queue buffer of the Linux kernel protocol stack to optimize the sending rate and throughput of the socket network communication, thereby reducing the time of entering the Linux kernel layer to apply for memory each time, reducing the time of copying network data from the user layer to the memory layer each time, and also reducing the number of system calls, thereby reducing the time of system call overhead, thereby achieving the purpose of network communication optimization without modifying the Nagle algorithm of the Linux system, the Linux network card driver, the network communication parameters, and the original socket network communication application program. Specifically, it has the following advantages: 1. It is not necessary to modify the kernel and the socket communication application to improve the performance of the network. 2. The transmission rate of the network is improved, the delay is reduced, and the throughput is improved through the hook technology. 3. Use hook technology to reduce system resource usage, thereby optimizing network performance. 4. Use hook technology to reorganize network packets sent by socket applications at the application layer, reduce system calls and kernel network bottom memory application times to achieve network optimization. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 Schematic diagram of the basic flow of the method of the embodiment of the present invention.
[0038] Figure 2 The figure is a schematic diagram of the socket network communication principle between the server and the client in an embodiment of the present invention.
[0039] Figure 3 The figure is a schematic diagram of the execution flow of the write function write rewritten in the embodiment of the present invention. DETAILED DESCRIPTION
[0040] The socket network communication optimization method, system and medium of the present invention mainly provide a method for achieving the purpose of network communication optimization without modifying the Nagle algorithm of the Linux system, the Linux network card driver, adjusting the network communication parameters, and modifying the original socket network communication application. This technology uses hook technology to intercept the socket programming interface to achieve the optimization of the sending network packets of network communication packets. The following article will take the installation of the Kylin V10 desktop version system on an ARM architecture physical machine as an example to further explain in detail the socket network communication optimization method, system and medium of the present invention. Figure 1 As shown, the optimization method of socket network communication in this embodiment includes the following steps:
[0041] S1, uses hook technology (hijacking a specific function through the dlsym function and rewriting the function) to intercept the call of the socket network programming interface function;
[0042] S2, using the rewritten socket network programming interface function combined with the data packet reassembly algorithm provided by the application layer, sends the data packet to the kernel layer after reassembly at the application layer, instead of immediately sending it to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of socket network communication.
[0043] like Figure 2 As shown in the figure, when the server and the client communicate via the socket network, the client needs to initiate a connection request function connect to the server, and the server needs to pre-bind the port and listen to the connection request initiated by the client through the connection request function connect, and execute the connection request function accept to establish the connection communication. Figure 2 As shown, in step S1 of this embodiment, the use of hook technology to intercept the call of the socket network programming interface function includes using the hook technology to intercept the call of the server's accept connection request function accept, the write function write, and the send data function sendto. The rewritten socket network programming interface function in step S2 includes the accept connection request function accept, the write function write, and the send data function sendto, wherein the steps executed by the rewritten accept connection request function include:
[0044] S101, obtaining the file descriptor socket_fd in the call of the accept function of the server to accept the connection request (the file descriptor returned by the socket communication, which can be used to allow different computers or processes or threads of the same computer to exchange data);
[0045] S102, judging whether the current socket network communication protocol type is TCP protocol or UDS protocol according to the file descriptor socket_fd, and judging whether the Nagle algorithm is enabled if it is TCP protocol, and jumping to step S103 if it is Nagle algorithm is enabled; and jumping to step S103 if it is UDS protocol;
[0046] S103, insert the file descriptor socket_fd into the initialized file descriptor linked list fd_list (a linked list used to store file descriptions for TCP / UDS socket communication);
[0047] The rewritten write function write is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared with the file descriptor socket_fd in the file descriptor list fd_list inserted by the accept connection request function and matches, the data packet to be written is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack to optimize the sending rate and throughput of the socket network communication.
[0048] The rewritten data sending function sendto is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared and matched with the file descriptor socket_fd in the file descriptor list fd_list inserted by the accept connection request function, the data packet to be sent is reassembled in the application layer using the reassembly algorithm and sent to the kernel layer instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack to optimize the sending rate and throughput of the socket network communication. Among them, throughput refers to the amount of data or data flow transmitted per unit time in a computer network. It measures the speed and capacity of network transmission, indicating how much data the network can transmit per unit time.
[0049] like Figure 2 As shown, in step S1 of this embodiment, the use of hook technology to intercept the call of the socket network programming interface function includes using the hook technology to intercept the call of the client's connection creation request function connect, the write function write, and the data sending function sendto. The socket network programming interface function rewritten in step S2 includes the connection creation request function connect, the write function write, and the data sending function sendto, wherein the steps executed by the rewritten connection creation request function connect include:
[0050] S201, obtaining the file descriptor socket_fd in the call of the client's connection request function connect;
[0051] S202, judging the current socket network communication protocol type as TCP or UDS protocol according to the file descriptor socket_fd, judging whether the Nagle algorithm has been enabled if it is TCP protocol, and jumping to step S103 if it is UDS protocol; jumping to step S103;
[0052] S203, insert the file descriptor socket_fd into the initialized file descriptor linked list fd_list;
[0053] The rewritten write function write is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared with the file descriptor socket_fd in the file descriptor list fd_list inserted in the connection request function connect, and they match, the data packet to be written is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
[0054] The rewritten data sending function sendto is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared and matched with the file descriptor socket_fd in the file descriptor linked list fd_list inserted in the connection request function connect, the data packet to be sent is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
[0055] In this embodiment, the format of the call of the rewritten write function write is: write(fd,buf,count), where fd is the file descriptor, buf is the data packet buffer address, and count is the count value. Figure 3 As shown, in step S2 of this embodiment, when the data packet is reassembled at the application layer and then sent to the kernel layer by using the rewritten socket network programming interface function combined with the data packet reassembly algorithm provided by the application layer, for the call of the write function write, the rewritten write function write is used in combination with the data packet reassembly algorithm provided by the application layer to reassemble the data packet at the application layer and then send it to the kernel layer, including:
[0056] S301, intercepting the call of the write function write by hook technology, if the call of the write function write is intercepted, jump to step S302;
[0057] S302, determine whether the file descriptor fd in the call of the write function write exists in the file descriptor linked list fd_list. If not, directly execute the original write function original_write after hook interception and renaming, end and exit; otherwise, jump to step S303; wherein, the format of the call of the original write function original_write is: original_write (fd, buf, count), wherein fd is the file descriptor, buf is the data packet buffer address, and count is the count value;
[0058] S303, determine whether the count value count of the call of the write function write is less than the preset merge length merge_len, if so, jump to step S304; otherwise, execute the original write function original_write to send the merged data in the merge buffer merge_buff to the transmission queue buffer of the kernel, and clear the merge buffer merge_buff, restart the timer, and jump to step S304; the timer is used to regularly check the merge buffer merge_buff, if the timer expires and finds that there is data in the merge buffer merge_buff, then execute the original write function original_write to send the merged data in the merge buffer merge_buff to the transmission queue buffer of the kernel, and clear the merge buffer merge_buff, restart the timer, and jump to step S304; if the timer expires and finds that there is no data in the merge buffer merge_buff, restart the timer; wherein the merge buffer merge_buff is a memory space opened up by the hook technology, which is used as a queue buffer (queue buff) for reorganizing network data packets; wherein the timer is an existing technology that executes a specific function after counting a set timing time;
[0059] S304, copy the memory in the data packet buffer address buf called by the write function write to the merge buffer merge_buff, and return the count value count of the call of the write function write.
[0060] In this embodiment, the format of the call of the rewritten send data function sendto is: sendto(fd, buf,len, flags, to, tolen), where fd is a file descriptor, buf is a data packet buffer address, len is a count value, flags is a control flag, to is a target address, and tolen is the length of the target address. In step S2, when the data packet is reassembled at the application layer and then sent to the kernel layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer, for the call of the send data function sendto, the rewritten socket network programming interface function is used in combination with the data packet reassembly algorithm provided by the application layer to reassemble the data packet at the application layer and then send it to the kernel layer, including:
[0061] S401, intercepting the call of the data sending function sendto through the hook technology, if the call of the data sending function sendto is intercepted, jump to step S402;
[0062] S402, determine whether the file descriptor fd called by the data sending function sendto exists in the file descriptor linked list fd_list. If not, directly call the original data sending function original_sendto that is intercepted and renamed by hook to directly send the data to the kernel layer, end and exit; otherwise, jump to step S403; wherein, the format of the original data sending function original_sendto is: original_sendto (fd, buf, len, flags, to, tolen), wherein fd is the file descriptor, buf is the data packet buffer address, len is the count value, flags is the control flag, to is the target address, and tolen is the length of the target address;
[0063] S403, determine whether the count value len of the call of the data sending function sendto is less than the preset merge length merge_len, if so, jump to step S404; otherwise, execute the original data sending function original_sendto to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S404; the timer is used to periodically check the merge buffer merge_buff, if the timer expires and it is found that there is data in the merge buffer merge_buff, then execute the original data sending function original_sendto to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S404; if the timer expires and it is found that there is no data in the merge buffer merge_buff, restart the timer;
[0064] S404, copy the memory in the data packet buffer address buf of the call of the data sending function sendto to the merge buffer merge_buff, and return the count value len of the call of the data sending function sendto.
[0065] In order to verify the optimization method of socket network communication in this embodiment, the TCP network packet capture is performed through the tcpdump tool in this embodiment. It is found that the method of this embodiment uses the rewritten socket network programming interface function combined with the data packet reassembly algorithm provided by the application layer to reassemble the data packet at the application layer and then send it to the kernel layer, rather than immediately sending it to the transmission queue buffer of the Linux kernel protocol stack, which can successfully assemble the packet, thereby optimizing the sending rate and throughput of the socket network communication. It should be noted that the tcpdump tool is an existing packet capture tool, which does not belong to the scope of improvement of this embodiment and will not be described in detail in this embodiment.
[0066] To sum up, when starting the socket application, this embodiment rewrites the accept connection request function and the connect connection request function on the server / client side through the hook technology. Through the file descriptor socket_fd of the interface, it can be determined whether the file descriptor is TCP / UDS communication. For TCP, it is necessary to determine whether the Nagle algorithm is turned on. If the condition is met, the corresponding file descriptor socket_fd is inserted into the pre-initialized file descriptor linked list fd_list. When sending data through the send data function sendto / write data function write, the input file descriptor fd of the send data function sendto / write data function write is searched for a match in the elements in the file descriptor linked list fd_list, and then a buff memory space with a binding size of merge_len is applied for each file descriptor socket_fd inserted into the linked list. The size of merge_len should be smaller than MTU, and a timer function is initialized at the same time. On the server / client side, the hook technology is used to rewrite the data sending function sendto / data writing function write, and the number of bytes written count is judged. The specific implementation is divided into two steps: if the number of bytes count is less than the length of merge_len, it is not sent directly. First, the memory space of the merge buffer merge_buff size applied before is copied, and it is not sent directly to the bottom layer of the Linux kernel to reduce system calls, and then merge_len -= count is used to inform the merge buffer merge_buff of the remaining space size, and then the number of bytes count is directly returned to the application, and then the timer is started to inform the end of this sending. If the number of bytes count is greater than the length of merge_len, the timer is turned off first, and then the merge buffer merge_buff that has been filled before is sent to the transmission queue buffer tx queue buff in the memory, and then the merge buffer merge_buff is cleared. Then the data of the length of bytes count bytes is filled into the merge buffer merge_buff, and then merge_len -= count is used to inform the merge buffer merge_buff of the remaining space size, and then the number of bytes count is directly returned to the application to inform the end of this sending. If within the timer timeout period, the bytes continuously written by the send data function sendto / write data function write do not fill the space of the merge buffer merge_buff, and there is data to be sent in the merge buffer merge_buff, then the content of the merge buffer merge_buff is directly sent to the kernel's transmission queue buffer tx queue buff in the timer function, and then the merge buffer merge_buff is cleared.The above hook technology is to hijack and rewrite the accept and create connection request functions, connect and send data functions through the dlsym() function, and then generate a dynamic library socket_hook.so for these hijacked and rewritten functions (dynamic library that dynamically hijacks fixed functions and then rewrites them through dlsym()), set the variable LD_PRELOAD=socket_hook.so, where the variable LD_PRELOAD is an environment variable of the Linux system, which is used to specify the shared library to be preloaded when executing the program; socket_app (generally refers to the application program that uses socket communication for network data transmission) to execute the application program that uses socket communication for network data transmission, so that the specific function can be hijacked and rewritten through the dlsym() function at the application layer, to optimize the sending of socket network communication packets and improve the network rate and throughput. This embodiment intercepts the socket programming interface through hook technology to achieve the purpose of optimizing network communication. By intercepting and rewriting some interfaces of socket programming, the sent network data is first intercepted in these interfaces, and then the data packet reassembly algorithm is performed in the hook layer. After reassembly, it is sent to the kernel layer, so that it does not immediately send the network data to the transmission queue buffer tx queue buff of the Linux kernel protocol stack through the system call, and also reduces the time to apply for memory each time entering the Linux kernel layer, and also reduces the time to copy network data from the user layer to the memory layer each time, and also reduces the number of system calls, thereby reducing the time of system call overhead, thereby realizing the interception and rewriting of the data transmission interface through hook technology to achieve the improvement of network rate and throughput, reducing Linux system calls, and reducing the number of memory applications of the kernel network driver, thereby achieving the purpose of network optimization. The method of this embodiment does not need to modify the communication application of the kernel and socket to improve the performance of the network. The method of this embodiment uses hook technology to improve the transmission rate of the network, reduce delay (the time required to transmit data in a computer network is as short as possible, where delay refers to the time interval between the data sender sending data and the receiver receiving data), and improve throughput. The method of this embodiment reduces the resource usage of the system through hook technology, thereby achieving the purpose of optimizing network performance. The method of this embodiment uses hook technology to reorganize the network packets sent by the socket application at the application layer, reducing the time for system calls and kernel network bottom layer to apply for memory times to achieve the purpose of network optimization.
[0067] In addition, this embodiment also provides a socket network communication optimization system, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the socket network communication optimization method.
[0068] In addition, this embodiment also provides a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute the socket network communication optimization method through a processor.
[0069] In addition, this embodiment also provides a computer program product, including a computer program or instructions, which are programmed or configured to execute the socket network communication optimization method through a processor.
[0070] Those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application can take the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the process Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing device to work in a specific way, so that the instructions stored in the computer-readable memory produce a product including an instruction device, which implements the functions specified in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable device provide for implementing the process in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0071] The above is only a preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions under the concept of the present invention belong to the protection scope of the present invention. It should be pointed out that for ordinary technicians in this technical field, some improvements and modifications without departing from the principle of the present invention should also be regarded as the protection scope of the present invention.
Claims
1. A socket network communication optimization method, characterized in that: The steps include: S1, uses hook technology to intercept the call of socket network programming interface function; S2, using the rewritten socket network programming interface function combined with the data packet reassembly algorithm provided by the application layer to reassemble the data packet at the application layer before sending it to the kernel layer, rather than immediately sending it to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication; In step S1, the use of hook technology to intercept the call of the socket network programming interface function includes using the hook technology to intercept the call of the server's accept connection request function accept, write function write and send data function sendto. The rewritten socket network programming interface function in step S2 includes the accept connection request function accept, write function write and send data function sendto, wherein the steps executed by the rewritten accept connection request function include: S101, obtaining the file descriptor socket_fd in the call of the accept function of the server to accept the connection request; S102, judging whether the current socket network communication protocol type is TCP protocol or UDS protocol according to the file descriptor socket_fd, and judging whether the Nagle algorithm is enabled if it is TCP protocol, and jumping to step S103 if it is Nagle algorithm is enabled; and jumping to step S103 if it is UDS protocol; S103, insert the file descriptor socket_fd into the initialized file descriptor linked list fd_list; The rewritten write function write is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared with the file descriptor socket_fd in the file descriptor list fd_list inserted by the accept connection request function and matches, the data packet to be written is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack to optimize the sending rate and throughput of the socket network communication. The rewritten data sending function sendto is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared and matched with the file descriptor socket_fd in the file descriptor linked list fd_list inserted by the connection request function accept, the data packet to be sent is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
2. The method for optimizing socket network communication according to claim 1, characterized in that: In step S1, the use of hook technology to intercept the call of the socket network programming interface function includes using the hook technology to intercept the call of the client's connection creation request function connect, the write function write, and the data sending function sendto. The socket network programming interface function rewritten in step S2 includes the connection creation request function connect, the write function write, and the data sending function sendto, wherein the steps executed by the rewritten connection creation request function connect include: S201, obtaining the file descriptor socket_fd in the call of the client's connection request function connect; S202, judging the current socket network communication protocol type as TCP or UDS protocol according to the file descriptor socket_fd, judging whether the Nagle algorithm has been enabled if it is TCP protocol, and jumping to step S103 if it is UDS protocol; jumping to step S103; S203, insert the file descriptor socket_fd into the initialized file descriptor linked list fd_list; The rewritten write function write is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared with the file descriptor socket_fd in the file descriptor list fd_list inserted in the connection request function connect, and they match, the data packet to be written is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication. The rewritten data sending function sendto is used to extract the file descriptor parameter fd in the input parameter. If the file descriptor parameter fd is compared and matched with the file descriptor socket_fd in the file descriptor linked list fd_list inserted in the connection request function connect, the data packet to be sent is reassembled at the application layer using the reassembly algorithm and then sent to the kernel layer, instead of being immediately sent to the transmission queue buffer of the Linux kernel protocol stack, so as to optimize the sending rate and throughput of the socket network communication.
3. The method for optimizing socket network communication according to claim 2, characterized in that: The calling format of the rewritten write function write is: write(fd, buf, count), wherein fd is the file descriptor, buf is the data packet buffer address, and count is the count value.
4. The method for optimizing socket network communication according to claim 3, characterized in that: In step S2, when the data packet is reassembled at the application layer and then sent to the kernel layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer, for the call of the write function write, the data packet is reassembled at the application layer and then sent to the kernel layer by using the rewritten write function write in combination with the data packet reassembly algorithm provided by the application layer, including: S301, intercepting the call of the write function write by hook technology, if the call of the write function write is intercepted, jump to step S302; S302, determine whether the file descriptor fd in the call of the write function write exists in the file descriptor linked list fd_list. If not, directly execute the original write function original_write after hook interception and renaming, end and exit; otherwise, jump to step S303; wherein, the format of the call of the original write function original_write is: original_write (fd, buf, count), wherein fd is the file descriptor, buf is the data packet buffer address, and count is the count value; S303, determine whether the count value count of the call of the write function write is less than the preset merge length merge_len, if so, jump to step S304; otherwise, execute the original write function original_write to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S304; the timer is used to periodically check the merge buffer merge_buff, if the timer expires and it is found that there is data in the merge buffer merge_buff, then execute the original write function original_write to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S304; if the timer expires and it is found that there is no data in the merge buffer merge_buff, restart the timer; S304, copy the memory in the data packet buffer address buf called by the write function write to the merge buffer merge_buff, and return the count value count of the call of the write function write.
5. The method for optimizing socket network communication according to claim 2, characterized in that: The calling format of the rewritten data sending function sendto is: sendto(fd, buf, len, flags, to, tolen), where fd is the file descriptor, buf is the data packet buffer address, len is the count value, flags is the control flag, to is the target address, and tolen is the length of the target address.
6. The method for optimizing socket network communication according to claim 5, characterized in that: In step S2, when the data packet is reassembled at the application layer and then sent to the kernel layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer, for the call of the data sending function sendto, the data packet is reassembled at the application layer and then sent to the kernel layer by using the rewritten socket network programming interface function in combination with the data packet reassembly algorithm provided by the application layer, including: S401, intercepting the call of the data sending function sendto through the hook technology, if the call of the data sending function sendto is intercepted, jump to step S402; S402, determine whether the file descriptor fd called by the data sending function sendto exists in the file descriptor linked list fd_list. If not, directly call the original data sending function original_sendto that is intercepted and renamed by hook to directly send the data to the kernel layer, end and exit; otherwise, jump to step S403; wherein, the format of the original data sending function original_sendto is: original_sendto (fd, buf, len, flags, to, tolen), wherein fd is the file descriptor, buf is the data packet buffer address, len is the count value, flags is the control flag, to is the target address, and tolen is the length of the target address; S403, determine whether the count value len of the call of the data sending function sendto is less than the preset merge length merge_len, if so, jump to step S404; otherwise, execute the original data sending function original_sendto to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S404; the timer is used to periodically check the merge buffer merge_buff, if the timer expires and it is found that there is data in the merge buffer merge_buff, then execute the original data sending function original_sendto to send the merged data in the merge buffer merge_buff to the kernel's transmission queue buffer, and clear the merge buffer merge_buff, restart the timer, and jump to step S404; if the timer expires and it is found that there is no data in the merge buffer merge_buff, restart the timer; S404, copy the memory in the data packet buffer address buf of the call of the data sending function sendto to the merge buffer merge_buff, and return the count value len of the call of the data sending function sendto.
7. A socket network communication optimization system, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the socket network communication optimization method described in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program or instruction stored therein, characterized in that: The computer program or instruction is programmed or configured to execute the socket network communication optimization method described in any one of claims 1 to 6 through a processor.
9. A computer program product comprising a computer program or instructions, characterized in that The computer program or instruction is programmed or configured to execute the socket network communication optimization method described in any one of claims 1 to 6 through a processor.
Citation Information
Patent Citations
UDP data package transmission method
CN101217464A
Method for acquiring network flow data set with accurate application type identification
CN102694733A