A multi-core dynamic rate adjustment method to achieve lock-free speed limiting
By independently allocating rate-limiting tokens and calculating the token bucket rate of each core in a multi-core environment, the problem of performance degradation caused by speed-limiting and locking of virtual network elements is solved, linear increase in service core and performance is achieved, and deadlock is avoided. Improved performance.
Patent Information
- Application Number
- CN202311703006.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-12
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2043-12-12
AI Technical Summary
In a multi-core environment, the service performance of virtual network elements cannot increase linearly, but may decrease. This is because speed limit locking causes performance degradation. Especially when the number of cores increases, the performance improves even worse, affecting usage.
Design a lock-free rate limiting method for multi-core dynamic rate adjustment. Hash table entries are generated through IP address hashing, and global rate limiting tokens are independently assigned to each service core so that each core has an independent rate limiting configuration. The speed-regulating core calculates the token bucket rate of each core to achieve lock-free speed limiting.
The multi-core service speed limit is realized without locking, and the service core and performance increase linearly, eliminating the performance degradation and deadlock possibility caused by locking, and greatly improving the performance.
Smart Images

Figure CN117880208B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of data communication, and in particular relates to a method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores. Background Art
[0002] As cloud services grow, the traffic on the resource pool increases, and the service pressure on virtual network elements increases. Network elements need to be expanded or their performance specifications need to be increased, and additional service cores need to be added. However, as network element service cores increase, service performance does not increase linearly, and may even decrease. The reason is that services are concurrently distributed to multiple cores, which causes speed limit and lock performance degradation.
[0003] The commonly used rate limiting method in virtual network elements is the token bucket. Since the service is multi-core, the token bucket needs to be global and needs to be locked in the case of multi-core. Locking has a great impact on performance in the case of multi-core. The more cores there are, the worse the performance will be or even decrease, affecting usage. Therefore, improvements are needed. Summary of the invention
[0004] The purpose of the present invention is to provide a multi-core dynamic rate adjustment method for realizing lock-free speed limiting, which is used to solve the technical problems raised in the existing background technology.
[0005] In order to achieve the above-mentioned purpose, the present invention adopts the following technical scheme: a multi-core dynamic rate adjustment method for realizing lock-free speed limiting, comprising the following steps: S01, designing a speed limiting instance data structure; performing hashing using an IP address as a key to generate a hash table entry, and traversing a linked list to find a service instance entry corresponding to the IP address;
[0006] S02. Design the business core speed limit processing flow; distribute the global speed limit token to each business core so that each business core has an independent speed limit configuration; S03. Design the speed regulation process of the speed regulation core; take a single core for speed regulation, traverse all business instances, and calculate the independent token bucket rate of each core.
[0007] Preferably, the method for generating the hash table entry is as follows: select a suitable hash function to convert the IP address into an integer hash value; calculate the hash value of the IP address, divide the IP address into four parts, then convert each part into an integer, and combine them into an integer hash value according to certain rules; use the calculated hash value as a key and store the business instance entry related to the IP address as a value in the hash table; when it is necessary to find a business instance entry corresponding to a given IP address, first calculate the hash value of the IP address, and then find the entry corresponding to the hash value in the hash table; if a matching entry is found, return the value of the entry; otherwise, return a not found result.
[0008] Preferably, the hash table entry stores independent token buckets and traffic statistics information of all service cores.
[0009] Preferably, the processing flow of business core speed limit includes the following steps: the business core receives the message; obtains the destination IP address dip in the message as the key value of the hash table, uses the hash function to convert dip into a unique integer hash value, traverses the hash linked list, and uses dip to match the instance business ip. If it fails, the packet is dropped, and if it succeeds, the instance entry is hit; obtains the id of the current business core, and the id of the current business core is the subscript index of the corresponding business core array in the instance entry array, and each core is configured independently; according to the message length and index, the traffic statistics of the current core in the curt_stat of the entry are updated; according to index, the token bucket of the current core is obtained, and then the token bucket algorithm is used to determine whether the speed limit is to be dropped. If the speed limit is, the packet is dropped, and if the speed limit is not, the packet is sent normally.
[0010] Preferably, the method of using a hash function to convert dip into a unique integer hash value is as follows: the dip is divided into four parts, each part is then converted into an integer, and combined into an integer hash value according to a set rule.
[0011] Preferably, the traffic statistics of the current core in curt_stat of entry are updated according to the message length and index, and the specific steps are as follows: obtain the length and index value of the current message; search the traffic statistics of the current core in entry according to index and obtain the curt_stat value; update the relevant fields in curt_stat according to the current message length; write the updated curt_stat value back to the entry array of the core corresponding to entry.
[0012] Preferably, the method for determining whether speed limit packet loss occurs by using a token bucket algorithm is as follows: setting the capacity of the token bucket and the number of tokens generated per second; when the sender needs to send data, it obtains a token from the token bucket, and if there is no token in the token bucket, it waits until a token is available; after obtaining the token, the sender sends the data; after the sender sends the data, it puts the token back into the token bucket, and if the token bucket is full, it discards the excess tokens; if within a set time, the number of tokens in the token bucket does not reach a set threshold, it is determined that speed limit packet loss has occurred.
[0013] Preferably, the steps of speed regulation process are as follows: traverse the instance list to obtain each business instance entry; accumulate the traffic statistics of all cores in curt_stat in the entry to obtain tot_stat; calculate the traffic percentage based on the traffic statistics of each core in curt_stat on the entry divided by tot_stat; based on to l_token on the entry and the calculated traffic percentage of each core percent, calculate to l_token×percent for each core and update it to work_token to obtain the independent token bucket rate of each core.
[0014] Preferably, the speed regulation process calculates curt_stat and to l_token through a calculation formula, and allocates the flow rate of each core to work_token.
[0015] Preferably, the calculation formula is as follows:
[0016]
[0017] Where Ptn is the token bucket rate of core n calculated this time, Btot is the cumulative sum of B1 to Bn, Bn is the statistical traffic of core n, where n is 1, 2, 3...i, and tol_token is the total token bucket configuration on the current entry.
[0018] In summary, due to the adoption of the above technical solution, the beneficial effects of the present invention are:
[0019] 1. The present invention uses a separate speed regulation core to distribute the global token bucket rate of the business instance to the unique token bucket rate of each core through the traffic percentage of each core, thereby achieving multi-core business speed limit without locking, and the business cores and performance both increase linearly.
[0020] 2. The present invention eliminates the process of locking business traffic, improves the performance of a single core, and mainly eliminates the possibility of deadlock caused by locking, achieves lock-free speed limiting, greatly improves performance, and increases business cores linearly. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0022] Figure 1 A flow chart of the method of the present invention is shown;
[0023] Figure 2 The hash table item structure diagram of the present invention is shown;
[0024] Figure 3 The flow chart of the service core speed limit processing of the present invention is shown;
[0025] Figure 4 The speed regulation core speed regulation flow chart of the present invention is shown;
[0026] Figure 5 A schematic diagram of a speed regulation calculation formula of the present invention is shown;
[0027] Figure 6 A schematic diagram of the single-core performance test results of the present invention is shown;
[0028] Figure 7 A schematic diagram of the 3-core performance test results of the present invention is shown. DETAILED DESCRIPTION
[0029] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0030] Example 1
[0031] Reference Figure 1 As shown, a method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores includes the following steps:
[0032] S01. Design speed limit instance data structure;
[0033] Use the IP address as the key to hash and generate a hash table entry. Then traverse the linked list to find the business instance entry corresponding to the IP address. The specific steps are as follows:
[0034] (1) Select a suitable hash function to convert the IP address into an integer hash value. Commonly used hash functions include MurmurHash, CityHash, etc.
[0035] (2) Calculate the hash value of the IP address by dividing the IP address into four parts (such as 192.168.1.1), then converting each part into an integer and combining them into an integer hash value according to certain rules; for example, the first part can be shifted left by 24 bits, the second part can be shifted left by 16 bits, the third part can be shifted left by 8 bits, and the fourth part can remain unchanged, and then these four integers can be added together to obtain the hash value;
[0036] (3) Use the calculated hash value as the key and the business instance entry related to the IP address as the value to store in the hash table; a dictionary or a hash table can be used to implement this step;
[0037] (4) When it is necessary to find the business instance entry corresponding to a given IP address, first calculate the hash value of the IP address, and then search the hash table for the entry corresponding to the hash value; if a matching entry is found, return the value of the entry; otherwise, return a not found result;
[0038] The hash table entry stores independent token buckets and traffic statistics for all service cores;
[0039] The hash table entry structure design is as follows Figure 2 As shown, the specific parameters of the table structure are described as follows:
[0040] struct list_head node: the business instance adopts a linked list structure;
[0041] ui nt32_t i nstance_i p: the key unique identifier of the business instance, also used for hashing in linked lists to solve fast search;
[0042] token_t to l_token: the total rate limit token bucket configuration of the business instance;
[0043] token_t token
[256] : business core speed limit token bucket array. Each business core has its own independent token bucket for speed limit. The id of the business core is the index of the array. A maximum of 256 cores are supported.
[0044] work_flow_stat_t curt_stat
[256] : business core traffic statistics array. Each business core has its own independent traffic statistics. It is used by the speed regulation core to summarize the traffic statistics of all business cores, and then redistribute the total token bucket rate to each business core token according to the percentage. The id of the business core is the index of the array. A maximum of 256 cores are supported.
[0045] S02. Design business core speed limit processing flow;
[0046] The business core is a multi-core processor. Each core has a full configuration and can independently process all business instances. However, the rate limit configuration of an instance needs to be global, so the normal solution needs to be locked.
[0047] Furthermore, in order to solve the global locking problem, the global speed limit token is assigned to each business core in the design, so that each business core has an independent speed limit configuration and does not need to be locked. The sum of the independent speed limit configurations of all business cores is equal to the total speed limit configuration.
[0048] Specifically, refer to Figure 3 As shown in the figure, the process of processing the service core speed limit includes the following steps:
[0049] (1) Business core receives the message;
[0050] (2) Obtain the destination IP address dip in the message as the key value of the hash table, use the hash function to convert dip into a unique integer hash value, traverse the hash linked list, and search for an entry that matches the calculated hash value. If a matching entry is found, proceed to the next step; otherwise, it means that no matching instance service IP is found, and packet loss processing is required; after finding a matching entry, it is necessary to determine whether the entry is an instance entry; this can be determined by comparing the service IP address in the instance entry with the dip in the message; if the two are equal, it means that the instance entry is hit, and subsequent processing can be performed; otherwise, it means that the instance entry is not hit, and packet loss processing is required; if the instance entry is hit, the message can be processed, such as parsing the message content, updating the database, etc.; if the instance entry is not hit, packet loss processing is performed, such as logging, sending an error report, etc.;
[0051] Specifically, the method of using a hash function to convert dip into a unique integer hash value is as follows:
[0052] Divide the dip into four parts (such as 192.168.1.1), then convert each part into an integer and combine them into an integer hash value according to certain rules. For example, the first part can be shifted left 24 bits, the second part can be shifted left 16 bits, the third part can be shifted left 8 bits, and the fourth part can remain unchanged. Then add these four integers to get the hash value.
[0053] (3) Get the ID of the current business core. This ID is the index of the corresponding business core array in the instance entry array. Each core is configured independently.
[0054] (4) Update the traffic statistics of the current core in curt_stat of entry according to the message length and index. The specific steps are as follows:
[0055] Get the length and index value of the current message, find the traffic statistics of the current core in entry according to index, and get the curt_stat value; update the relevant fields in curt_stat according to the current message length, such as the cumulative number of inbound packets, the cumulative number of inbound bytes, the cumulative number of outbound packets, etc.; write the updated curt_stat value back to the entry array of the core corresponding to entry, where the curt_stat corresponding to different cores needs to be stored in a specific order;
[0056] (5) Obtain the token bucket of the current core based on index, and then use the token bucket algorithm to determine whether the speed limit causes packet loss. If the speed limit is set, the packet is lost; if the speed limit is not set, the packet is sent normally.
[0057] Specifically, the method of determining whether to limit packet loss through the token bucket algorithm is as follows:
[0058] Set the capacity of the token bucket and the number of tokens generated per second;
[0059] When the sender needs to send data, it gets a token from the token bucket. If there is no token in the token bucket, it waits until a token is available.
[0060] After obtaining the token, the sender can send a certain amount of data, and the amount of data can be set according to actual needs;
[0061] After the sender sends the data, it puts the token back into the token bucket. If the token bucket is full, it discards the extra tokens.
[0062] If the number of tokens in the token bucket does not reach the set threshold (the threshold is set according to the actual situation) within a certain period of time (such as 1 second, 2 seconds), it is considered that rate limit packet loss has occurred;
[0063] Specifically, suppose we set the capacity of the token bucket to 10 tokens and generate 2 tokens per second. When the sender needs to send data, it first gets a token from the token bucket. If there is a token in the token bucket, it is allowed to send data. If there is no token in the token bucket, it waits until a token is available.
[0064] Assume that the sender successfully obtains 2 tokens within 1 second and sends the corresponding amount of data. At this time, the number of remaining tokens in the token bucket is 8. In the next second, since only 2 tokens are generated per second, the number of tokens in the token bucket does not increase. Therefore, in the next second, if the sender tries to obtain tokens again, it will wait because there are no tokens in the token bucket;
[0065] If the number of tokens in the token bucket does not reach the set threshold (2*time) within 3 consecutive seconds (3 time intervals in total), it is considered that rate limit packet loss has occurred. In this case, the rate limit policy can be optimized by adjusting the capacity of the token bucket or the number of tokens generated per second to avoid packet loss.
[0066] S03. Design speed regulation and speed regulation process;
[0067] Specifically, the speed regulation takes a single core, traverses all business instances, and then calculates the percentage of each core through traffic statistics on the business core. This percentage and the total token bucket rate are used to obtain the independent token bucket rate of each core.
[0068] Further, see Figure 4 As shown, the steps of the speed regulation process are as follows:
[0069] (1) Traverse the instance list and obtain each business instance entry;
[0070] (2) Accumulate the traffic statistics of all cores in curt_stat in entry tot_stat;
[0071] (3) Calculate the traffic percentage by dividing the traffic statistics of each core in curt_stat on the entry by tot_stat;
[0072] (4) Based on the tol_token on the entry and the calculated traffic percentage of each core, each core calculates tol_token*percent and updates it to work_token to obtain the independent token bucket rate of each core;
[0073] Specifically, for the above steps, specific examples are given as follows:
[0074] Reference Figure 5 As shown in the figure, the speed control thread allocates the flow rate of each core to work_token by calculating curt_stat and to l_token. The calculation formula is explained using 32 business cores as an example.
[0075] B1 is the statistical flow of nuclear No. 1;
[0076] Btot is the cumulative sum of B1 to B32, which is the total traffic of all service cores under this entry;
[0077] to l_token is the total token bucket configuration on the current entry;
[0078] Pt1 is the token bucket rate of core 1 in this calculation;
[0079]
[0080] Where Ptn is the token bucket rate of core n in this calculation, Bn is the statistical traffic of core n, where n is 1, 2, 3...i.
[0081] In summary, the present invention performs traffic statistics on each core in a business instance, and then calculates a core cycle separately. The traffic statistics on each core for each business instance can be converted into the traffic percentage of each core of this business instance, and then the total token bucket rate of this business instance is distributed to each core according to the traffic percentage, so that the global token bucket becomes a unique token bucket for each core, which solves the problem of locking, and increases the business cores linearly and the performance linearly.
[0082] Example 2
[0083] In the Tianyi Cloud 4.0 cloud network base, the speed-limiting network elements all use the method in Example 1 to achieve lock-free speed limiting, which is used to limit the speed of eip, natgw, peer i ng, lb, vpn and other services.
[0084] Forwarding performance: For a 10G network card, the forwarding performance of a single CPU core can reach 450w+pps, and the forwarding performance of three cores can reach 1044w+pps (full bandwidth 10G). The performance increases linearly with the increase of service cores. For a 10G physical network card interface, only three CPU cores are needed for full bandwidth line-speed forwarding.
[0085] The single-core performance test results are as follows Figure 6 As shown;
[0086] The 3-core performance test results are as attached Figure 7 shown.
[0087] To sum up, the present invention uses a speed regulation core to distribute the speed limit token bucket to each business core according to the traffic ratio, thereby realizing lock-free speed limit of the business core. It is actually a method of allocating global services to each core independently through business rules, achieving the effect of no locking, so that the business cores increase linearly and the performance can also increase linearly.
[0088] The above description is only a preferred specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical scheme and inventive concept of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.
[0089] The preferred embodiments of the present invention disclosed above are only used to help explain the present invention. The preferred embodiments do not describe all the details in detail, nor do they limit the invention to only specific implementation methods. Obviously, many modifications and changes can be made according to the content of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the present invention, so that those skilled in the art can understand and use the present invention well. The present invention is limited only by the claims and their full scope and equivalents.
Claims
1. A multi-core dynamic rate adjustment method for achieving lock-free rate limiting, characterized in that: The steps include: S01. Design speed limit instance data structure; Use the IP address as the key to perform hashing, generate a hash table entry, and traverse the linked list to find the business instance entry corresponding to the IP address; The method for generating the hash table entry is as follows: Choose a suitable hash function to convert the IP address into an integer hash value; Calculate the hash value of the IP address, split the IP address into four parts, then convert each part into an integer and combine them into an integer hash value according to certain rules; The calculated hash value is used as the key, and the business instance entry related to the IP address is stored in the hash table as the value; When you need to find the business instance entry corresponding to a given IP address, first calculate the hash value of the IP address, and then find the entry corresponding to the hash value in the hash table; if a matching entry is found, return the value of the entry; otherwise, return a not found result; The hash table entry stores independent token buckets and traffic statistics information of all service cores; S02. Design business core speed limit processing flow; Allocate the global speed limit token to each service core, so that each service core has an independent speed limit configuration; The process of processing the service core rate limit includes the following steps: The business core receives the message; Get the destination IP address dip in the message as the key value of the hash table, use the hash function to convert dip into a unique integer hash value, traverse the hash table, and use dip to match the instance service IP. If it fails, the packet is dropped. If it succeeds, the instance entry is hit. Get the id of the current business core. The id of the current business core is the subscript index of the corresponding business core array in the instance entry array. Each core is configured independently. Update the traffic statistics of the current core in curt_stat of entry according to the message length and index; Get the token bucket of the current core according to the index, and then use the token bucket algorithm to determine whether the speed limit is dropped. If the speed is limited, the packet is dropped. If the speed is not limited, the packet is sent normally. S03. Design speed regulation and speed regulation process; Speed regulation takes a single core, traverses all business instances, and calculates the independent token bucket rate of each core.
2. A method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores as claimed in claim 1, characterized in that: The method of using a hash function to convert dip into a unique integer hash value is as follows: Divide the dip into four parts, then convert each part into an integer and combine them into an integer hash value according to the set rules.
3. A method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores as claimed in claim 2, characterized in that: According to the message length and index, update the traffic statistics of the current core in the entry's curt_stat. The specific steps are as follows: Get the length and index value of the current message; Search the traffic statistics of the current core in the entry according to the index and obtain the curt_stat value; According to the current message length, update the relevant fields in curt_stat; Write the updated curt_stat value back to the entry array of the core corresponding to entry.
4. A method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores as claimed in claim 3, characterized in that: The method of determining whether to limit the rate of packet loss through the token bucket algorithm is as follows: Set the capacity of the token bucket and the number of tokens generated per second; When the sender needs to send data, it gets a token from the token bucket. If there is no token in the token bucket, it waits until a token is available. After obtaining the token, the sender sends the data; After the sender sends the data, it puts the token back into the token bucket. If the token bucket is full, it discards the extra tokens. If the number of tokens in the token bucket does not reach the set threshold within the set time, it is determined that rate limit packet loss has occurred.
5. A method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores as claimed in claim 4, characterized in that: The steps of speed regulation process are as follows: Traverse the instance list and obtain each business instance entry; Accumulate the traffic statistics of all cores in curt_stat in entry to get tot_stat; The traffic percentage is calculated by dividing the traffic statistics of each core in curt_stat on the entry by tot_stat; According to the tol_token on the entry and the calculated per-core traffic percentage percent, each core calculates tol_token×percent and updates it to work_token to obtain the independent token bucket rate for each core.
6. A method for realizing lock-free speed limiting by dynamically adjusting the speed of multiple cores as claimed in claim 5, characterized in that: The speed regulation process calculates curt_stat and tol_token through a calculation formula and allocates the flow rate of each core to work_token; The calculation formula is as follows: Where Ptn is the token bucket rate of core n calculated this time, Btot is the cumulative sum of B1 to Bn, Bn is the statistical traffic of core n, where n is 1, 2, 3...i, and tol_token is the total token bucket configuration on the current entry.
