A load balancing method and device based on lock-free session
By using the client port and worker thread matching formula in the Xinchuang load balancing product, a lock-free session table is implemented, which solves the performance issues in FullNAT and transparent transmission scenarios and improves device performance and deployment flexibility.
Patent Information
- Application Number
- CN202411587812.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-08
- Publication Date
- 2025-09-09
- Estimated Expiration
- 2044-11-08
AI Technical Summary
Xinchuang's load balancing products need to adapt to both FullNAT and transparent transmission scenarios, but existing technologies require lock protection when different CPUs process data packets, resulting in reduced performance.
Through initialization based on source port, destination port and worker thread, the client port is used to ensure a lock-free session table, ensuring that client and application server requests are processed through the same worker thread and session, avoiding cross-CPU access, and using the worker thread matching formula to determine the target worker thread.
It implements lock-free sessions, improves server load balancing performance and deployment flexibility, meets the requirements of FullNAT and transparent transmission scenarios, and reduces lock overhead.
Smart Images

Figure CN119766816B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of Internet technology, and more specifically, to a load balancing method and device based on lock-free sessions. Background Art
[0002] In the era of the rapidly developing Internet and Internet of Things, server load balancing devices serve as traffic entry points, requiring high performance, high reliability, and high scalability. Server load balancing devices are generally categorized as either Layer 4 or Layer 7 load balancing, depending on the layer of operation. Layer 4 load balancing operates at the transport layer, while Layer 7 load balancing operates at the application layer. Both Layer 4 and Layer 7 load balancing require reliable and rapid forwarding of user requests to backend application servers.
[0003] Common forwarding modes include transparent mode and FullNAT (Full Network Address Translation, which refers to the simultaneous implementation of SNAT and DNAT, where SNAT stands for Source Network Address Translation and DNAT stands for Destionation Network Address Translation). Transparent mode does not modify the source address of a packet, but only changes the destination IP address and port to the application server's IP address and service port. This mode preserves the client's source IP address, but requires the server load balancer to function as a gateway; otherwise, the application server's response packets will not reach the client. In FullNAT mode, the server load balancer modifies the source IP address and port of the client's request packet. This forwarding mode has low network requirements (it does not require the server load balancer to function as a gateway), but client IP information is lost.
[0004] A session records how packets are forwarded on each connection. It typically includes a five-tuple of information about the client and application server: protocol type, source IP address, destination IP address, source port, and destination port. The session table is a core data structure in load balancing products. Here, we focus on how to build an efficient session table and adapt it to both transparent transmission and FullNAT scenarios.
[0005] Xinchuang Load Balancing refers to a load balancing product that uses domestically produced CPUs and domestically produced operating systems. For domestically produced CPUs, we use the Hygon 7380 CPU, which has four NUMA nodes, each with eight cores. NUMA is a memory architecture design used in multi-processor systems, particularly high-performance computing and server platforms. In a NUMA architecture, each CPU core has its own local memory. When a core needs to access the local memory of another CPU, a higher latency is incurred. This access is called remote memory access. In contrast, accessing one's own local memory has a lower latency, which is called local memory access.
[0006] In a multi-core system, we usually split the session table to each working CPU, so that the data packets of each session will be processed on a fixed CPU, thus avoiding the loss caused by cross-CPU or NUMA. The existing technology generally uses the RSS mechanism of the network card, which is a network card traffic diversion technology that aims to improve network performance by distributing data packets to multiple receive queues.
[0007] However, this RSS splitting only applies to FullNAT forwarding mode because it ensures that RSS(cip,vip,cport,vport) == RSS(rip,snat_ip,rport,snat_port) by selecting the appropriate source network address translation IP address (SNAT_IP) and source network address translation port (SNAT_port). This ensures that both the request and reply packets arrive in the same queue on the network card and, consequently, on the same CPU. If the corresponding session happens to be on that CPU, the packets can then be quickly forwarded and processed based on the session. (RSS stands for Receive Side Scaling; cip is the client IP address; vip is the virtual IP address; cport is the client port; vport is the virtual port; rip is the real server IP address; snat_ip is the source network address translation IP address; rport is the real server port; and snat_port is the source network address translation port.)
[0008] At present, Xinchuang load balancing products need to adapt to both FullNAT scenarios and transparent transmission scenarios. That is, when forwarding client data packets to the application server, only VIP / Vport is changed to RIP / Rport. At this time, according to the RSS diversion algorithm, the request data packet from the client will be allocated to the RSS (cip, vip, cport, vport) CPU, and the response data packet from the application server will be allocated to the RSS (rip, cip, rport, cport) CPU. In most cases, these two values are not equal, which means that the data stream from the client and the response data stream from the application server need to be processed on different CPUs. The two CPUs need to be locked when accessing the same public data structure, which seriously reduces the processing performance of the device.
[0009] Based on this, it is necessary to introduce a new method and device to ensure a lock-free session table by using the client port, and to meet the requirements of both FullNAT scenarios and transparent transmission scenarios. Summary of the Invention
[0010] In response to the technical problems mentioned above, the present invention provides a load balancing method and device based on lock-free sessions. Based on the defined source port, destination port, working thread and session table, the lock-free session table is guaranteed by using the client port. The client request and the application server request are forwarded and processed through the same working thread and the same session, and the corresponding data packets are forwarded to the same CPU. This solves the technical problem in the prior art that different CPUs process data packet processing requests, resulting in lock protection and reduced device performance. It not only meets the application requirements of FullNAT scenarios and transparent transmission scenarios at the same time, but also ensures that each working thread has its own independent session table, and does not need to access other session tables across CPUs or NUMA (non-uniform memory access), thereby realizing lock-free sessions, thereby reducing lock overhead, improving server load balancing performance and deployment flexibility.
[0011] The present invention provides a load balancing method based on lock-free sessions, the method comprising:
[0012] S1, port initialization: Based on the server load balancing device, the source port, destination port and worker thread are initialized, and the number of times the source port and the destination port are referenced by the virtual port or application server port is counted in real time according to the user-side configuration, and a session table is constructed based on the five-tuple data structure and the session, and the number of worker threads is counted;
[0013] S2, responding to a data processing request: receiving a data packet processing request in real time based on a scenario type, determining the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determining a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads;
[0014] S3, data packet forwarding: creating a request processing session based on the target working thread according to the source port, the destination port, and the session table, and using the request processing session to forward the data packet corresponding to the data processing request;
[0015] Among them, the five-tuple data structure includes the source IP address, source port, destination IP address, destination port and transport layer protocol. The five-tuple data structure in the session table has a one-to-one correspondence with the session; the number of the working thread also has a one-to-one correspondence with the session; the scenario types include: transparent transmission scenario and complete network address translation scenario; the data packet processing request includes: client request and application server request.
[0016] Preferably, the step S1 includes:
[0017] S11, based on the server load balancing device, defining a source port reference count array and a destination port reference count array respectively;
[0018] S12, configuring the source port and the destination port of the server load balancing device according to the client configuration, and setting the initial value of the number of times the source port of the server load balancing device is referenced and the initial value of the number of times the destination port of the server load balancing device is referenced to zero based on the source port reference count array and the destination port reference count array, and then initializing the number of times the source port of the server load balancing device is referenced and the number of times the destination port of the server load balancing device is referenced according to the configuration information of the virtual port and the application server port;
[0019] S13, based on the server load balancing device and the working CPU, and the five-tuple data structure, construct the session table, initialize the working thread, add a number to the working thread, and count the number of working threads of the working thread; wherein, one working thread corresponds to one working CPU.
[0020] Preferably, the step S2 includes the step of client request processing, specifically:
[0021] When the scenario type is a transparent transmission scenario or a complete network address translation scenario, and the data packet processing request is a client request,
[0022] S211, receiving a client request based on a server load balancing device, and obtaining a client port and a virtual port corresponding to the client request according to the client request, and determining the source port and the destination port, wherein the source port is the client port corresponding to the client request, and the destination port is the virtual port corresponding to the client request;
[0023] S212, obtaining and determining the value of the source port and the value of the destination port according to the number of times the client port corresponding to the client request is referenced and the number of times the virtual port is referenced;
[0024] S213: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
[0025] Preferably, the step S2 further includes the step of applying the server-side request processing in the transparent transmission scenario, specifically:
[0026] When the scenario type is a transparent transmission scenario and the data packet processing request is an application server-side request, S221, based on the server load balancing device, receives the application server-side request, and according to the transparent transmission scenario and the application server-side request, obtains the application server port and client port corresponding to the application server-side request, and determines the source port and the destination port, wherein the source port is the application server port corresponding to the application server-side request, and the destination port is the client port corresponding to the application server-side request; S222, obtains and determines the value of the source port and the value of the destination port based on the number of times the application server port corresponding to the application server-side request is referenced, and the number of times the client port is referenced; S223, according to the value of the source port, the value of the destination port and the number of working threads of the working thread, uses the working thread matching formula to calculate the number of the target working thread that processes the data packet processing request.
[0027] Preferably, the step S2 further includes the step of applying server-side request processing in a complete network address translation scenario, specifically:
[0028] When the scenario type is a complete network address translation scenario and the data packet processing request is an application server-side request, S221, based on the server load balancing device, receives the application server-side request, and according to the complete network address translation scenario and the application server-side request, obtains the application server port and source network address translation port corresponding to the application server-side request, and determines the source port and the destination port, wherein the source port is the application server port corresponding to the application server-side request, and the destination port is the source network address translation port corresponding to the application server-side request; S222, obtains and determines the value of the source port and the value of the destination port based on the number of references of the application server port corresponding to the application server-side request and the number of references of the source network address translation port; S223, according to the value of the source port, the value of the destination port and the number of working threads of the working thread, uses the working thread matching formula to calculate the number of the target working thread that processes the data packet processing request.
[0029] Preferably, in step S2, the step of determining a target worker thread for processing the data packet processing request from the worker threads based on the number of times the source port is referenced and the number of times the destination port is referenced, a worker thread matching formula, and the number of worker threads is specifically as follows:
[0030] 1) Determine the value of the source port and the value of the destination port according to the number of times the source port is referenced and the number of times the destination port is referenced, wherein:
[0031] If the number of times the source port is cited is zero, recording the source port;
[0032] If the number of times the source port is cited is greater than zero, the source port is recorded as zero;
[0033] If the number of times the destination port is referenced is zero, then record the destination port;
[0034] If the number of times the destination port is referenced is greater than zero, the destination port is recorded as zero;
[0035] 2) according to the value of the source port, the value of the destination port and the number of the working threads of the working thread, the number of the target working thread for processing the data packet processing request is calculated using the working thread matching formula; wherein,
[0036] The working thread matching formula is: N = (Count Sport +Count Vport )%W Thread , where N is the number of the worker thread, Count Sportis the value of the source port, Count Vport is the value of the destination port, W Thread The number of worker threads for the worker thread.
[0037] Preferably, the S3 includes the steps of data packet forwarding processing in the transparent transmission scenario, specifically:
[0038] When the scenario type is a transparent transmission scenario and the data packet processing request is a client request, based on the target working thread, select five-tuple data corresponding to the source port and the destination port from the session table according to the source port and the destination port; if the five-tuple data corresponding to the source port and the destination port and the session exist in the session table, forward the data packet requested by the client to the application server using the five-tuple data corresponding to the source port and the destination port based on the session; if the five-tuple data corresponding to the source port and the destination port does not exist in the session table, modify the destination IP address corresponding to the client request to the application server IP address, modify the destination port to the application server port, generate new five-tuple data based on the source port, the source IP address, the application server IP address, the application server port and the transport layer protocol, create a new session, use the new session and the new five-tuple data to forward the data packet requested by the client to the application server, and update the new five-tuple data and the new session to the session table;
[0039] When the scenario type is a transparent transmission scenario and the data packet processing request is an application server-side request, the session and five-tuple data for processing the application server-side request are determined according to the number of the target working thread and the session table, the source IP address in the five-tuple data corresponding to the application server-side request is modified to the virtual IP address of the server load balancing device, and the source port is modified to the virtual port of the server load balancing device, and the modified five-tuple data is generated. The session corresponding to the application server-side request and the modified five-tuple data are used to forward the data packet corresponding to the application server-side request to the client; wherein the five-tuple data includes the source IP address, source port, destination IP address, destination port and transport layer protocol.
[0040] Preferably, the S3 further includes the steps of data packet forwarding processing in a complete network address translation scenario, specifically:
[0041] When the scenario type is a complete network address translation scenario and the data packet processing request is a client request, based on the target working thread, the five-tuple data corresponding to the source port and the destination port is selected from the session table according to the source port and the destination port. If the five-tuple data corresponding to the source port and the destination port and the session exist in the session table, the data packet requested by the client is forwarded to the application server using the five-tuple data corresponding to the source port and the destination port based on the session. If the five-tuple data corresponding to the source port and the destination port does not exist in the session table, the source port corresponding to the client request is modified to the target source port. The network address translation port is modified, the source IP address is modified to the target source network address translation IP address, and the destination IP address corresponding to the client request is modified to the application server IP address, and the destination port is modified to the application server port. A new five-tuple data is generated based on the target source network address translation IP address and the target source network address translation port, as well as the modified destination IP address, destination port, and transport layer protocol corresponding to the client request, and a new session is created. The data packet of the client request is forwarded to the application server using the new session and the new five-tuple data, and the new five-tuple data and the new session are updated in the session table.
[0042] When the scenario type is a complete network address translation scenario and the data packet processing request is an application server-side request, the session and five-tuple data for processing the application server-side request are determined according to the number of the target working thread and the session table, the source IP address in the five-tuple data corresponding to the application server-side request is modified to the virtual IP address of the server load balancing device, and the source port is modified to the virtual port of the server load balancing device, and the modified five-tuple data is generated. The session corresponding to the application server-side request and the modified five-tuple data are used to forward the data packet corresponding to the application server-side request to the client; wherein the five-tuple data includes the source IP address, source port, destination IP address, destination port and transport layer protocol.
[0043] Preferably, if the session table does not contain five-tuple data corresponding to the source port and the destination port, before modifying the source port corresponding to the client request to the target source network address translation port and the source IP address to the target source network address translation IP address, the method further includes a target source network address translation port matching process, specifically:
[0044] When the number of times the source network address translation port is referenced is equal to zero, the value of the source network address translation port is recorded; when the number of times the source network address translation port is referenced is greater than zero, the value of the source network address translation port is modified to zero;
[0045] Based on the working thread matching formula, the target source network address translation port is selected according to the number of the target working thread and the value of the source network address translation port. If the value of the source network address translation port% the number of working threads is equal to the number of the target working thread, and the value of the source network address translation port is less than 65536, then the source network address translation port is the target source network address translation port, and the source network address translation IP address corresponding to the target source network address translation port is the target source network address translation IP address.
[0046] Accordingly, the present invention also provides a device for implementing the load balancing method based on lock-free sessions, the device comprising a port initialization unit, a response data processing request unit, and a data packet forwarding unit;
[0047] The port initialization unit is used for port initialization: based on the server load balancing device, the source port, the destination port and the working thread are initialized, and according to the user terminal configuration, the number of times the source port and the destination port are referenced by the virtual port or the application server port is counted in real time, and a session table is constructed according to the five-tuple data structure and the session, and the number of working threads of the working thread is counted;
[0048] The data processing request response unit is used to respond to the data processing request: receive a data packet processing request in real time based on a scenario type, determine the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determine a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads;
[0049] The data packet forwarding unit is configured to: create a request processing session based on the target working thread according to the source port, the destination port, and the session table, and forward the data packet corresponding to the data processing request using the request processing session;
[0050] The five-tuple data structure includes the source IP address, source port, destination IP address, destination port, and transport layer protocol. The five-tuple data structure in the session table corresponds one-to-one to each session. The worker thread number also corresponds one-to-one to each session. The scenario types include transparent transmission and full network address translation. The packet processing requests include client requests and application server requests.
[0051] By applying the above technical solution, the present invention realizes a lock-free session table based on a defined source port, destination port, working thread and session table. By using the client port, the client request and the application server request are forwarded and processed through the same working thread and the same session, and the corresponding data packets are transferred to the same CPU. This solves the technical problem in the prior art that different CPUs process data packet processing requests, resulting in lock protection and reduced device performance. It not only meets the application requirements of the FullNAT scenario and the transparent transmission scenario at the same time, but also ensures that each working thread has its own independent session table, and does not need to access other session tables across CPUs or NUMA (non-uniform memory access), thereby realizing lock-free sessions, thereby reducing lock overhead, and improving the performance of server load balancing and deployment flexibility. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0053] Figure 1 A schematic diagram showing a flow chart of a load balancing method based on lock-free sessions proposed in an embodiment of the present invention is shown;
[0054] Figure 2 A schematic diagram of a session processing flow of a load balancing method based on lock-free sessions proposed in an embodiment of the present invention is shown;
[0055] Figure 3 A schematic diagram of a port reference count processing flow of a load balancing method based on lock-free sessions proposed in an embodiment of the present invention is shown;
[0056] Figure 4 A schematic diagram of a data packet processing request process in a transparent transmission scenario of a load balancing method based on a lock-free session proposed in an embodiment of the present invention is shown;
[0057] Figure 5 A schematic diagram of a complete network address translation data packet processing request process of a load balancing method based on lock-free sessions proposed in an embodiment of the present invention is shown;
[0058] Figure 6 A structural diagram of a load balancing device based on lock-free sessions proposed in an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0059] The following will be combined with the drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0060] The present invention provides a load balancing method based on lock-free session, such as Figure 1 As shown, the method includes the following steps:
[0061] S1, port initialization: Based on the server load balancing device, the source port, destination port and working thread are initialized, and the number of times the source port and the destination port are referenced by the virtual port or application server port is counted in real time according to the user-side configuration, and a session table is constructed based on the five-tuple data structure and the session, and the number of working threads of the working thread is counted.
[0062] In this embodiment, the step S1 includes:
[0063] S11, based on the server load balancing device, defining a source port reference count array and a destination port reference count array respectively;
[0064] S12, configuring the source port and the destination port of the server load balancing device according to the client configuration, and setting the initial value of the number of times the source port of the server load balancing device is referenced and the initial value of the number of times the destination port of the server load balancing device is referenced to zero based on the source port reference count array and the destination port reference count array, and then initializing the number of times the source port of the server load balancing device is referenced and the number of times the destination port of the server load balancing device is referenced according to the configuration information of the virtual port and the application server port;
[0065] S13, based on the server load balancing device and the working CPU, and the five-tuple data structure, construct the session table, initialize the working thread, add a number to the working thread, and count the number of working threads of the working thread;
[0066] Wherein, one working thread corresponds to one working CPU.
[0067] S2, responding to data processing requests: receiving data packet processing requests in real time based on the scenario type, determining the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determining a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads.
[0068] In this embodiment, the step S2 includes the steps of client request processing, specifically:
[0069] When the scenario type is a transparent transmission scenario or a complete network address translation scenario, and the data packet processing request is a client request,
[0070] S211, receiving a client request based on a server load balancing device, and obtaining a client port and a virtual port corresponding to the client request according to the client request, and determining the source port and the destination port, wherein the source port is the client port corresponding to the client request, and the destination port is the virtual port corresponding to the client request;
[0071] S212, obtaining and determining the value of the source port and the value of the destination port according to the number of times the client port corresponding to the client request is referenced and the number of times the virtual port is referenced;
[0072] S213: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
[0073] In this embodiment, the step S2 also includes the step of applying the server-side request processing in the transparent transmission scenario, specifically:
[0074] When the scenario type is a transparent transmission scenario and the data packet processing request is an application server request,
[0075] S221, based on the server load balancing device receiving the application server request, and according to the transparent transmission scenario and the application server request, obtaining the application server port and the client port corresponding to the application server request, and determining the source port and the destination port, wherein the source port is the application server port corresponding to the application server request, and the destination port is the client port corresponding to the application server request;
[0076] S222, obtaining and determining the value of the source port and the value of the destination port according to the number of times the application server port corresponding to the application server request is referenced and the number of times the client port is referenced;
[0077] S223: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
[0078] In this embodiment, the step S2 also includes the steps of applying server-side request processing in a complete network address translation scenario, specifically:
[0079] When the scenario type is a complete network address translation scenario and the data packet processing request is an application server request,
[0080] S221, based on the server load balancing device receiving the application server request, and according to the complete network address translation scenario and the application server request, obtaining the application server port and the source network address translation port corresponding to the application server request, and determining the source port and the destination port, wherein the source port is the application server port corresponding to the application server request, and the destination port is the source network address translation port corresponding to the application server request;
[0081] S222, obtaining and determining the value of the source port and the value of the destination port according to the number of times the application server port corresponding to the application server request is referenced and the number of times the source network address translation port is referenced;
[0082] S223: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
[0083] In this embodiment, in step S2, the step of determining the target worker thread for processing the data packet processing request from the worker threads based on the number of references of the source port and the number of references of the destination port, a worker thread matching formula, and the number of worker threads is specifically as follows:
[0084] 1) Determine the value of the source port and the value of the destination port according to the number of times the source port is referenced and the number of times the destination port is referenced, wherein:
[0085] If the number of times the source port is cited is zero, recording the source port;
[0086] If the number of times the source port is cited is greater than zero, the source port is recorded as zero;
[0087] If the number of times the destination port is referenced is zero, then record the destination port;
[0088] If the number of times the destination port is referenced is greater than zero, the destination port is recorded as zero;
[0089] 2) according to the value of the source port, the value of the destination port and the number of the working threads of the working thread, using the working thread matching formula to calculate the number of the target working thread for processing the data packet processing request;
[0090] in,
[0091] The working thread matching formula is: N = (Count Sport +Count Vport )%W Thread , where N is the number of the worker thread, Count Sport is the value of the source port, Count Vport is the value of the destination port, W Thread The number of worker threads for the worker thread.
[0092] S3, data packet forwarding: based on the target working thread, creating a request processing session according to the source port, the destination port and the session table, and using the request processing session to forward the data packet corresponding to the data processing request.
[0093] Among them, the five-tuple data structure includes the source IP address, source port, destination IP address, destination port and transport layer protocol. The five-tuple data structure in the session table has a one-to-one correspondence with the session; the number of the working thread also has a one-to-one correspondence with the session; the scenario types include: transparent transmission scenario and complete network address translation scenario; the data packet processing request includes: client request and application server request.
[0094] In this embodiment, S3 includes the steps of data packet forwarding processing in the transparent transmission scenario, specifically:
[0095] When the scenario type is a transparent transmission scenario and the data packet processing request is a client request,
[0096] Based on the target working thread, selecting five-tuple data corresponding to the source port and the destination port from the session table according to the source port and the destination port,
[0097] If the session table contains five-tuple data corresponding to the source port and the destination port and a session, forwarding the data packet requested by the client to the application server based on the session using the five-tuple data corresponding to the source port and the destination port,
[0098] If there is no five-tuple data corresponding to the source port and the destination port in the session table, modify the destination IP address corresponding to the client request to the application server IP address, modify the destination port to the application server port, generate new five-tuple data based on the source port, the source IP address, the application server IP address, the application server port, and the transport layer protocol, create a new session, use the new session and the new five-tuple data to forward the data packet requested by the client to the application server, and update the new five-tuple data and the new session in the session table;
[0099] When the scenario type is a transparent transmission scenario and the data packet processing request is an application server request, determining the session and five-tuple data for processing the application server request according to the number of the target working thread and the session table, modifying the source IP address in the five-tuple data corresponding to the application server request to the virtual IP address of the server load balancing device, and modifying the source port to the virtual port of the server load balancing device, generating modified five-tuple data, and forwarding the data packet corresponding to the application server request to the client using the session corresponding to the application server request and the modified five-tuple data;
[0100] The five-tuple data includes the source IP address, source port, destination IP address, destination port and transport layer protocol.
[0101] In this embodiment, S3 also includes the steps of data packet forwarding processing in a complete network address translation scenario, specifically:
[0102] When the scenario type is a complete network address translation scenario and the packet processing request is a client request,
[0103] Based on the target working thread, selecting five-tuple data corresponding to the source port and the destination port from the session table according to the source port and the destination port,
[0104] If the session table contains five-tuple data corresponding to the source port and the destination port and a session, forwarding the data packet requested by the client to the application server based on the session using the five-tuple data corresponding to the source port and the destination port,
[0105] If there is no five-tuple data corresponding to the source port and the destination port in the session table, the source port corresponding to the client request is modified to the target source network address translation port, the source IP address is modified to the target source network address translation IP address, and the destination IP address corresponding to the client request is modified to the application server IP address, the destination port is modified to the application server port, new five-tuple data is generated based on the target source network address translation IP address and the target source network address translation port, and the modified destination IP address, destination port, and transport layer protocol corresponding to the client request, a new session is created, the new session and the new five-tuple data are used to forward the data packet requested by the client to the application server, and the new five-tuple data and the new session are updated in the session table;
[0106] When the scenario type is a complete network address translation scenario and the data packet processing request is an application server request,
[0107] Determining, based on the number of the target working thread and the session table, a session and five-tuple data for processing the application server request, modifying the source IP address in the five-tuple data corresponding to the application server request to a virtual IP address of the server load balancing device, and modifying the source port to a virtual port of the server load balancing device, generating modified five-tuple data, and forwarding a data packet corresponding to the application server request to the client using the session corresponding to the application server request and the modified five-tuple data;
[0108] The five-tuple data includes the source IP address, source port, destination IP address, destination port and transport layer protocol.
[0109] In this embodiment, if the session table does not contain five-tuple data corresponding to the source port and the destination port, before modifying the source port corresponding to the client request to the target source network address translation port and the source IP address to the target source network address translation IP address, the step of target source network address translation port matching is further included, specifically:
[0110] When the number of times the source network address translation port is referenced is equal to zero, the value of the source network address translation port is recorded; when the number of times the source network address translation port is referenced is greater than zero, the value of the source network address translation port is modified to zero;
[0111] Based on the working thread matching formula, the target source network address translation port is selected according to the number of the target working thread and the value of the source network address translation port. If the value of the source network address translation port% the number of working threads is equal to the number of the target working thread, and the value of the source network address translation port is less than 65536, then the source network address translation port is the target source network address translation port, and the source network address translation IP address corresponding to the target source network address translation port is the target source network address translation IP address.
[0112] By applying the above technical solutions, a lock-free session table based on defined source ports, destination ports, working threads and session tables is realized. By using the client port to ensure the lock-free session table, the client request and the application server request are forwarded and processed through the same working thread and the same session, and the corresponding data packets are transferred to the same CPU, which solves the technical problems in the prior art of different CPUs processing data packet processing requests resulting in lock protection and reduced device performance. It not only meets the application requirements of FullNAT scenarios and transparent transmission scenarios at the same time, but also ensures that each working thread has its own independent session table, and does not need to access other session tables across CPUs or NUMA (non-uniform memory access), thereby realizing lock-free sessions, thereby reducing lock overhead, and improving server load balancing performance and deployment flexibility.
[0113] In order to help those skilled in the art better understand the technical solution provided by this application, the above steps are further explained with examples. The specific contents are as follows:
[0114]
[0115] like Figures 1 to 5 As shown, a load balancing method based on lock-free sessions includes the following steps:
[0116] S1, port initialization: Based on the server load balancing device, the source port, destination port and working thread are initialized, and the number of times the source port and the destination port are referenced by the virtual port or application server port is counted in real time according to the user-side configuration, and a session table is constructed based on the five-tuple data structure and the session, and the number of working threads of the working thread is counted.
[0117] Among them, in order to better count the number of times the source port and the destination port are referenced by the virtual port or the application server port in real time according to the user-side configuration, a port reference count array is defined to track the number of times the port is referenced by vport or rport, and the initialization value is 0, indicating that the port is not occupied.
[0118] #define MAX_PORT_NO 65535
[0119] uint16_t tml_app_port_refcnt[MAX_PORT_NO+1]={0};
[0120] Initialize the port reference count array based on the user's configuration. For example, if a virtual service is configured with vport 80, and two application servers have service ports 8080, then the reference count for port 80 is 1, i.e., tml_app_port_refcnt
[80] == 1; the reference count for port 8080 is 2, i.e., tml_app_port_refcnt
[8080] == 2.
[0121] For the Hygon C86 7380 CPU, there are a total of 32 cores, and 16 worker threads are used here.
[0122] S2, responding to data processing requests: receiving data packet processing requests in real time based on the scenario type, determining the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determining a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads.
[0123] S3, data packet forwarding: based on the target working thread, creating a request processing session according to the source port, the destination port and the session table, and using the request processing session to forward the data packet corresponding to the data processing request.
[0124] Among them, the five-tuple data structure includes the source IP address, source port, destination IP address, destination port and transport layer protocol. The five-tuple data structure in the session table has a one-to-one correspondence with the session; the number of the working thread also has a one-to-one correspondence with the session; the scenario types include: transparent transmission scenario and complete network address translation scenario; the data packet processing request includes: client request and application server request.
[0125] (1) Data packet processing request processing process in the transparent transmission scenario
[0126] Step 1:
[0127] A data packet arrives from the client, and its source and destination ports are cport (client port, assumed to be 12345) and vport (virtual port, here is 80). According to the port reference count array, for the source port, tml_app_port_refcnt[cport] is 0, indicating that the port has not been registered by the service, so the source port remains unchanged, still cport; for the destination port, tml_app_port_refcnt[vport]>0, indicating that the port has been registered by the service, so the destination port becomes 0. After the change, (source port + destination port) = (cport + 0) = cport, and the selected working thread is (cport% number of working threads) = N, indicating that the Nth working thread will process the session. If cport = 12345 and the number of working threads is 16, then (cport% number of working threads) = 9, which means that the 9th working thread will process the session.
[0128] Step 2:
[0129] The worker thread first searches the local session table based on the five-tuple. If found, it forwards the packet based on the session. If no corresponding session is found, it selects a suitable application server and creates a session. For transparent transmission mode, the source IP address and port of the packet do not need to be modified. Instead, the destination IP address and port of the packet are modified to the application server IP address and rport before forwarding it to the application server.
[0130] Step 3:
[0131] A data packet arrives from the application server, with source and destination ports Rport (here 8080) and cport (client port, here 12345). According to the port reference count array, for the source port, tml_app_port_refcnt[Rport] is 2, and tml_app_port_refcnt[vport]>0, indicating that the port is registered by the service, so the source port becomes 0; for the destination port, tml_app_port_refcnt[cport]==0, indicating that the port is not registered by the service, so the destination port cport remains unchanged. After the change, (source port + destination port)=(0+cport)=cport, and the selected working thread is (cport%number of working threads)=N, indicating that the Nth working thread will handle the session. If cport=12345 and the number of working threads is 16, then (cport%number of working threads)=9, which means that the 9th working thread will handle the session.
[0132] Step 4:
[0133] The worker thread finds the corresponding session in the local session table (created in step 2), then modifies the source IP address and source port of the data packet to VIP and Vport, and forwards it to the client.
[0134] In steps 1 and 3, the data flow in the client direction and the data flow in the application server direction arrive at the same worker thread, which ensures that the same session is processed only on the same CPU.
[0135] (2) Packet processing request process in FullNAT scenario
[0136] Step 1:
[0137] A data packet arrives from the client, and its source and destination ports are cport (client port, assumed to be 12345) and vport (virtual port, here is 80). According to the port reference count array, for the source port, tml_app_port_refcnt[cport] is 0, indicating that the port has not been registered by the service, so the source port remains unchanged, still cport; for the destination port, tml_app_port_refcnt[vport]>0, indicating that the port has been registered by the service, so the destination port becomes 0. Now (source port + destination port) = (cport + 0) = cport, and the selected working thread is (cport% number of working threads) = N, indicating that the Nth working thread will handle the session. If cport = 12345 and the number of working threads is 16, then (cport% number of working threads) = 9, which means that the 9th working thread will handle the session.
[0138] Step 2:
[0139] The worker thread first searches the local session table based on the five-tuple. If found, it forwards the packet based on the session. If no corresponding session is found, it selects a suitable application server and creates a session. For FullNAT mode, the source IP address and source port of the packet need to be modified. Here, we focus on the selection and allocation of the source port SnatPort. The following conditions must be met:
[0140] 1)(cport% number of working threads after change) ==(SnatPort% number of working threads after change).
[0141] 2) The value of SnatPort is less than 65536 (the reasonable port range).
[0142] The changed port means that if tml_app_port_refcnt[port]>0, the port value becomes 0; otherwise it remains unchanged and is still the original port value.
[0143] At the same time, the destination IP address and port of the data packet are modified to the application server IP address and rport and then forwarded to the application server.
[0144] Step 3:
[0145] A data packet arrives from the application server, with source and destination ports Rport (here 8080) and SnatPort. According to the port reference count array, for the source port, tml_app_port_refcnt[Rport] is 2, and tml_app_port_refcnt[vport]>0, indicating that the port is registered by the service, so the source port becomes 0; for the destination port, according to the requirements of step 2, (the changed source port is 0 + the changed SnatPort) = (the changed SnatPort), and the selected working thread is (the changed SnatPort% number of working threads) = (the changed cport% number of working threads) = N, indicating that the Nth working thread will process the session. If the SnatPort selected in step 2 is (12345+16) and the number of working threads is 16, then (SnatPort% number of working threads) = 9, which means that the 9th working thread will process the session.
[0146] Step 4:
[0147] The worker thread finds the corresponding session in the local session table (created in step 2), then modifies the source IP address and source port of the data packet to VIP and Vport, and the destination IP address and destination port to Cip and Cport, and forwards it to the client.
[0148] In steps 1 and 3, the data flow in the client direction and the data flow in the application server direction arrive at the same worker thread, which ensures that the same session is processed only on the same CPU.
[0149] Based on the above steps, the client source port is used to forward data flows between the client and the application server to the same worker thread for processing. This ensures that each worker thread has its own independent session table, eliminating the need to cross CPU or NUMA nodes to access other session tables. This reduces locking overhead and improves server load balancing performance. This solution is applicable not only to SNAT scenarios but also to transparent transmission scenarios, providing customers with more deployment options.
[0150] Corresponding to the load balancing method based on lock-free session in one embodiment of the present invention, the present invention also discloses a load balancing device based on lock-free session, such as Figure 6 As shown, the device includes a port initialization unit, a response data processing request unit and a data packet forwarding unit;
[0151] The port initialization unit is used for port initialization: based on the server load balancing device, the source port, the destination port and the working thread are initialized, and according to the user terminal configuration, the number of times the source port and the destination port are referenced by the virtual port or the application server port is counted in real time, and a session table is constructed according to the five-tuple data structure and the session, and the number of working threads of the working thread is counted;
[0152] The data processing request response unit is used to respond to the data processing request: receive a data packet processing request in real time based on a scenario type, determine the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determine a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads;
[0153] The data packet forwarding unit is configured to: create a request processing session based on the target working thread according to the source port, the destination port, and the session table, and forward the data packet corresponding to the data processing request using the request processing session;
[0154] in,
[0155] The five-tuple data structure includes the source IP address, source port, destination IP address, destination port, and transport layer protocol. The five-tuple data structure in the session table has a one-to-one correspondence with the session. The worker thread number also has a one-to-one correspondence with the session.
[0156] The scenario types include: transparent transmission scenario and complete network address translation scenario;
[0157] The data packet processing request includes: a client request and an application server request.
[0158] Each embodiment in this specification is described in a related manner. The same or similar parts between the embodiments can be referred to each other. Each embodiment focuses on the differences from other embodiments.
[0159] The above description is only a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention are included in the scope of protection of the present invention.
Claims
1. A load balancing method based on lock-free sessions, characterized in that: The method comprises: S1, port initialization: Based on the server load balancing device, the source port, destination port and worker thread are initialized, and the number of times the source port and the destination port are referenced by the virtual port or application server port is counted in real time according to the user-side configuration, and a session table is constructed based on the five-tuple data structure and the session, and the number of worker threads is counted; S2, responding to a data processing request: receiving a data packet processing request in real time based on a scenario type, determining the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determining a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads; S3, data packet forwarding: creating a request processing session based on the target working thread according to the source port, the destination port, and the session table, and using the request processing session to forward the data packet corresponding to the data processing request; in, The five-tuple data structure includes a source IP address, a source port, a destination IP address, a destination port, and a transport layer protocol. The five-tuple data structure in the session table has a one-to-one correspondence with the session; the worker thread number also has a one-to-one correspondence with the session. The scenario types include: transparent transmission scenario and complete network address translation scenario; The data packet processing request includes: a client request and an application server request.
2. The method according to claim 1, wherein The steps of S1 include: S11, based on the server load balancing device, defining a source port reference count array and a destination port reference count array respectively; S12, respectively configuring the source port and destination port of the server load balancing device according to the client configuration, and based on the source port reference count array and the destination port reference count array, setting the initial value of the number of times the source port of the server load balancing device is referenced and the initial value of the number of times the destination port is referenced to zero, and then initializing the number of times the source port of the server load balancing device is referenced and the number of times the destination port is referenced according to the configuration information of the virtual port and the application server port; S13, constructing the session table based on the server load balancing device and the working CPU, and the five-tuple data structure, initializing the working thread, adding a number to the working thread, and counting the number of working threads of the working thread; Wherein, one working thread corresponds to one working CPU.
3. The method according to claim 1, wherein The step S2 includes the steps of client request processing, specifically: When the scenario type is a transparent transmission scenario or a complete network address translation scenario, and the data packet processing request is a client request, S211, receiving a client request based on a server load balancing device, and obtaining a client port and a virtual port corresponding to the client request according to the client request, and determining the source port and the destination port, wherein the source port is the client port corresponding to the client request, and the destination port is the virtual port corresponding to the client request; S212, obtaining and determining the value of the source port and the value of the destination port according to the number of times the client port corresponding to the client request is referenced and the number of times the virtual port is referenced; S213: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
4. The method according to claim 1, wherein The step S2 also includes the step of applying the server-side request processing in the transparent transmission scenario, specifically: When the scenario type is a transparent transmission scenario and the data packet processing request is an application server request, S221, based on the server load balancing device receiving the application server request, and according to the transparent transmission scenario and the application server request, obtaining the application server port and the client port corresponding to the application server request, and determining the source port and the destination port, wherein the source port is the application server port corresponding to the application server request, and the destination port is the client port corresponding to the application server request; S222, obtaining and determining the value of the source port and the value of the destination port according to the number of times the application server port corresponding to the application server request is referenced and the number of times the client port is referenced; S223: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
5. The method according to claim 1, wherein The step S2 also includes the steps of applying server-side request processing in a complete network address translation scenario, specifically: When the scenario type is a complete network address translation scenario and the data packet processing request is an application server request, S221, based on the server load balancing device receiving the application server request, and according to the complete network address translation scenario and the application server request, obtaining the application server port and the source network address translation port corresponding to the application server request, and determining the source port and the destination port, wherein the source port is the application server port corresponding to the application server request, and the destination port is the source network address translation port corresponding to the application server request; S222, obtaining and determining the value of the source port and the value of the destination port according to the number of times the application server port corresponding to the application server request is referenced and the number of times the source network address translation port is referenced; S223: According to the value of the source port, the value of the destination port, and the number of the working threads of the working thread, use the working thread matching formula to calculate and obtain the number of the target working thread for processing the data packet processing request.
6. The method according to claim 1, wherein In step S2, the step of determining a target worker thread for processing the data packet processing request from the worker threads based on the number of references of the source port and the number of references of the destination port, a worker thread matching formula, and the number of worker threads is specifically as follows: 1) Determine the value of the source port and the value of the destination port according to the number of times the source port is referenced and the number of times the destination port is referenced, wherein: If the number of times the source port is cited is zero, recording the source port; If the number of times the source port is cited is greater than zero, the source port is recorded as zero; If the number of times the destination port is referenced is zero, then record the destination port; If the number of times the destination port is referenced is greater than zero, the destination port is recorded as zero; 2) according to the value of the source port, the value of the destination port and the number of the working threads of the working thread, using the working thread matching formula to calculate the number of the target working thread for processing the data packet processing request; in, The working thread matching formula is: N = (Count Sport +Count Vport )%W Thread , where N is the number of the worker thread, Count Sport is the value of the source port, Count Vport is the value of the destination port, W Thread The number of worker threads for the worker thread.
7. The method according to claim 1, wherein S3 includes the steps of data packet forwarding processing in the transparent transmission scenario, specifically: When the scenario type is a transparent transmission scenario and the data packet processing request is a client request, Based on the target working thread, selecting five-tuple data corresponding to the source port and the destination port from the session table according to the source port and the destination port, If the session table contains five-tuple data corresponding to the source port and the destination port and a session, forwarding the data packet requested by the client to the application server based on the session using the five-tuple data corresponding to the source port and the destination port, If there is no five-tuple data corresponding to the source port and the destination port in the session table, modify the destination IP address corresponding to the client request to the application server IP address, modify the destination port to the application server port, generate new five-tuple data based on the source port, the source IP address, the application server IP address, the application server port, and the transport layer protocol, create a new session, use the new session and the new five-tuple data to forward the data packet requested by the client to the application server, and update the new five-tuple data and the new session in the session table; When the scenario type is a transparent transmission scenario and the data packet processing request is an application server request, Determining, based on the number of the target working thread and the session table, a session and five-tuple data for processing the application server request, modifying the source IP address in the five-tuple data corresponding to the application server request to a virtual IP address of the server load balancing device, and modifying the source port to a virtual port of the server load balancing device, generating modified five-tuple data, and forwarding a data packet corresponding to the application server request to the client using the session corresponding to the application server request and the modified five-tuple data; The five-tuple data includes the source IP address, source port, destination IP address, destination port and transport layer protocol.
8. The method according to claim 1, wherein S3 also includes the steps of packet forwarding processing in a complete network address translation scenario, specifically: When the scenario type is a complete network address translation scenario and the packet processing request is a client request, Based on the target working thread, selecting five-tuple data corresponding to the source port and the destination port from the session table according to the source port and the destination port, If the session table contains five-tuple data corresponding to the source port and the destination port and a session, forwarding the data packet requested by the client to the application server based on the session using the five-tuple data corresponding to the source port and the destination port, If there is no five-tuple data corresponding to the source port and the destination port in the session table, the source port corresponding to the client request is modified to the target source network address translation port, the source IP address is modified to the target source network address translation IP address, and the destination IP address corresponding to the client request is modified to the application server IP address, the destination port is modified to the application server port, new five-tuple data is generated based on the target source network address translation IP address and the target source network address translation port, and the modified destination IP address, destination port, and transport layer protocol corresponding to the client request, a new session is created, the new session and the new five-tuple data are used to forward the data packet requested by the client to the application server, and the new five-tuple data and the new session are updated in the session table; When the scenario type is a complete network address translation scenario and the data packet processing request is an application server request, Determining, based on the number of the target working thread and the session table, a session and five-tuple data for processing the application server request, modifying the source IP address in the five-tuple data corresponding to the application server request to a virtual IP address of the server load balancing device, and modifying the source port to a virtual port of the server load balancing device, generating modified five-tuple data, and forwarding a data packet corresponding to the application server request to the client using the session corresponding to the application server request and the modified five-tuple data; The five-tuple data includes the source IP address, source port, destination IP address, destination port and transport layer protocol.
9. The method according to claim 8, wherein If the session table does not contain five-tuple data corresponding to the source port and the destination port, before modifying the source port corresponding to the client request to the target source network address translation port and the source IP address to the target source network address translation IP address, the step of matching the target source network address translation port is further included, specifically: When the number of times the source network address translation port is referenced is equal to zero, the value of the source network address translation port is recorded; when the number of times the source network address translation port is referenced is greater than zero, the value of the source network address translation port is modified to zero; based on the working thread matching formula, the target source network address translation port is selected according to the number of the target working thread and the value of the source network address translation port; if the value of the source network address translation port% the number of working threads is equal to the number of the target working thread, and the value of the source network address translation port is less than 65536, then the source network address translation port is the target source network address translation port, and the source network address translation IP address corresponding to the target source network address translation port is the target source network address translation IP address.
10. A device for implementing the load balancing method based on lock-free session according to claim 1, characterized in that: The device includes a port initialization unit, a response data processing request unit and a data packet forwarding unit; The port initialization unit is used for port initialization: based on the server load balancing device, the source port, the destination port and the working thread are initialized, and according to the user terminal configuration, the number of times the source port and the destination port are referenced by the virtual port or the application server port is counted in real time, and a session table is constructed according to the five-tuple data structure and the session, and the number of working threads of the working thread is counted; The data processing request response unit is used to respond to the data processing request: receive a data packet processing request in real time based on a scenario type, determine the source port and the destination port, as well as the number of times the source port is referenced and the number of times the destination port is referenced according to the data packet processing request, and determine a target working thread for processing the data packet processing request from the working threads based on the number of times the source port is referenced and the number of times the destination port is referenced, as well as a working thread matching formula and the number of working threads; The data packet forwarding unit is configured to: create a request processing session based on the target working thread according to the source port, the destination port, and the session table, and forward the data packet corresponding to the data processing request using the request processing session; in, The five-tuple data structure includes a source IP address, a source port, a destination IP address, a destination port, and a transport layer protocol. The five-tuple data structure in the session table has a one-to-one correspondence with the session; the worker thread number also has a one-to-one correspondence with the session. The scenario types include: transparent transmission scenario and complete network address translation scenario; The data packet processing request includes: a client request and an application server request.
Citation Information
Patent Citations
UDP (User Datagram Protocol) conversation multiplexing method and load balancing equipment
CN102761608A
Message forwarding method and device
CN106534345A