Message rate limiting method, related equipment and storage medium
By converting the token bucket size into the number of clock cycles and performing traffic control, the existing token bucket mechanism has been solved, and efficient traffic management has been achieved.
Patent Information
- Application Number
- CN202010321606.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-04-22
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2040-04-22
AI Technical Summary
The existing token bucket mechanism is slow to respond, and the resources are wasted, which cannot meet the user's usage needs.
By converting the token bucket size into the number of clock cycles, obtaining the number of clock cycles corresponding to the number of tokens required for the message, and sending or discarding the message under certain conditions to achieve traffic control.
Improves processing efficiency, reduces resource waste, and fast response time. It is suitable for traffic control in multi-threaded environments.
Smart Images

Figure CN113542146B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the IT field, and in particular to a message rate limiting method, related equipment and storage medium. Background Art
[0002] An important role of the token bucket is to control the Quality of Service (QoS). A fixed-size token bucket can generate tokens at a constant rate. If the token is not consumed, or the rate of consumption is lower than the rate of generation, the token will continue to increase until the bucket is full. The tokens generated later will overflow from the bucket. Finally, the maximum number of tokens that can be stored in the bucket will never exceed the size of the bucket. The token bucket indicates when traffic can be sent based on whether there are tokens in the token bucket. If there are tokens in the token bucket, traffic is allowed to be sent; if there are no tokens in the token bucket, traffic is not allowed to be sent. Therefore, if the burst threshold is configured properly and there are enough tokens in the token bucket, traffic can be sent at the peak rate.
[0003] However, the existing token bucket mechanism responds very slowly, wastes a lot of resources, and cannot meet the needs of users. Summary of the invention
[0004] In order to solve the above problems, the present application provides a message rate limiting method, related equipment and storage medium, which can effectively improve processing efficiency.
[0005] In a first aspect, a method for limiting the rate of a message is provided, including:
[0006] Convert the token bucket size into the number of first clock cycles;
[0007] Obtain a first message at a first moment, and obtain a second clock cycle number corresponding to a number of tokens required for the first message;
[0008] When the second clock cycle number is not greater than the first clock cycle number, the first message is sent.
[0009] In some possible designs, a second message is obtained at a second moment, and a third clock cycle number corresponding to the number of tokens required for the second message is obtained; when the sum of the second clock cycle number and the third clock cycle number is not greater than the sum of the new clock cycle number between the first moment and the second moment and the first clock cycle number, the second message is sent.
[0010] In some possible designs, when the sum of the second clock cycle number and the third clock cycle number is greater than the sum of the newly added clock cycle number between the first moment and the second moment and the first clock cycle number, the second message is discarded.
[0011] In some possible designs, a third message is obtained at a third moment, and a fourth clock cycle number corresponding to the number of tokens required for the third message is obtained; when the sum of the second clock cycle number and the fourth clock cycle number is not greater than the sum of the new clock cycle number between the first moment and the third moment and the first clock cycle number, the third message is sent.
[0012] In some possible designs, when the second number of clock cycles is greater than the first number of clock cycles, the first message is discarded.
[0013] Some possible designs for converting the token bucket size to the number of first clock cycles include:
[0014] N=CBS*Hz / R
[0015] Wherein, N is the number of first clock cycles, CBS is the maximum number of tokens that can be accommodated in the token bucket, r=Hz / R, r is the number of clock cycles occupied by each token, Hz is the frequency of the clock source of the network processing device, and R is the guaranteed rate.
[0016] In some possible designs, the number of recorded clock cycles is set equal to the second number of clock cycles, and the number of recorded clock cycles is stored in a first memory address of a shared memory between processes.
[0017] In some possible designs, setting the number of recorded clock cycles equal to the number of second clock cycles includes:
[0018] comparing the actual value in the first memory address with the old expected value;
[0019] In the case where the actual value in the first memory address is equal to the old expected value, the actual value of the first memory address is updated to the recorded number of clock cycles.
[0020] In some possible designs, the method further includes: when the actual value in the first memory address is not equal to the old expected value, stopping updating the actual value of the first memory address to the recorded number of clock cycles.
[0021] In a second aspect, a network processing device is provided, comprising: a conversion module, an acquisition module and a sending module.
[0022] The conversion module is used to convert the token bucket size into the first clock cycle number;
[0023] The acquisition module is used to acquire a first message at a first moment, and acquire the number of second clock cycles corresponding to the number of tokens required for the first message;
[0024] The sending module is used to send the first message when the number of the second clock cycles is not greater than the number of the first clock cycles.
[0025] In some possible designs, the acquisition module is used to acquire the second message at the second moment, and acquire the third clock cycle number corresponding to the number of tokens required for the second message;
[0026] The sending module is used to send the second message when the sum of the second clock cycle number and the third clock cycle number is not greater than the sum of the newly added clock cycle number between the first moment and the second moment and the first clock cycle number.
[0027] In some possible designs, the sending module is used to discard the second message when the sum of the second clock cycle number and the third clock cycle number is greater than the sum of the newly added clock cycle number between the first moment and the second moment and the first clock cycle number.
[0028] In some possible designs, the sending module is used to discard the first message when the number of second clock cycles is greater than the number of first clock cycles.
[0029] In some possible designs, the acquisition module is used to acquire a third message at a third moment, and acquire a fourth clock cycle number corresponding to a number of tokens required for the third message.
[0030] The sending module is used to send the third message when the sum of the second clock cycle number and the fourth clock cycle number is not greater than the sum of the newly added clock cycle number between the first moment and the third moment and the first clock cycle number.
[0031] In some possible designs, the conversion module is further used for:
[0032] N=CBS*Hz / R
[0033] Wherein, N is the number of first clock cycles, CBS is the maximum number of tokens that can be accommodated in the token bucket, r=Hz / R, r is the number of clock cycles occupied by each token, Hz is the frequency of the clock source of the network processing device, and R is the guaranteed rate.
[0034] According to a third aspect, a readable storage medium is provided, comprising instructions, which, when executed on a network processing device, enable the network processing device to execute the method as described in any one of the first aspects.
[0035] According to a fourth aspect, a computer program product is provided. When the computer program product is read and executed by a network processing device, the method described in any one of the first aspects will be executed.
[0036] In the above scheme, flow control is performed by the number of clock cycles, with only one variable. Therefore, conflicts can be handled through atomic operations. Moreover, atomic operations have fast response times and consume fewer resources, which are far superior to locking operations. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background technology, the drawings required for use in the embodiments of the present application or the background technology will be described below.
[0038] Figure 1 is a schematic diagram of flow control by tokens involved in this application;
[0039] Figure 2 It is a schematic diagram of multiple threads involved in this application grabbing tokens in a token bucket;
[0040] Figure 3 It is a schematic diagram of an application scenario involved in this application;
[0041] FIG. 4A to FIG. 4B The diagrams are respectively the schematic diagrams of the change in the number of recorded clock cycles when the bucket depth does not exceed and when the bucket depth exceeds;
[0042] FIG. 5A to FIG. 5B Schematic diagrams of the change in the number of clock cycles when the first message is allowed to be sent and when the first message is prohibited to be sent at time T1;
[0043] FIG. 6A to FIG. 6B Respectively in Figure 5A A schematic diagram of a change in the number of clock cycles when the first message is allowed to be sent and when the first message is prohibited to be sent at time T2 based on FIG.
[0044] FIG. 7A to FIG. 7B Respectively in Figure 5B A schematic diagram of a change in the number of clock cycles when the first message is allowed to be sent and when the first message is prohibited to be sent at time T2 based on FIG.
[0045] Figure 8 is a schematic diagram of modifying shared memory when there is no conflict between thread 1 and thread 2 in this application;
[0046] Fig. 9 It is a schematic diagram of modifying shared memory when a conflict occurs between thread 1 and thread 2 in this application;
[0047] Fig.10 It is a structural schematic diagram of a network processing device provided by the present application;
[0048] Fig.11 It is a structural diagram of a network processing device provided by this application. DETAILED DESCRIPTION
[0049] For ease of understanding, the token bucket involved in this application is described in detail below. Figure 1 As shown, in order to control the flow, when the thread needs to send a data packet, it first needs to apply for a token from the token bucket. If the thread can obtain the token, the thread can send the data packet. On the contrary, if the thread cannot obtain the token, the thread refuses to send the data packet. Among them, the number of data packets that the thread needs to send is directly proportional to the number of tokens that need to be obtained, that is, the more data packets the thread needs to send, the more tokens need to be obtained. On the contrary, the fewer data packets the thread needs to send, the fewer tokens need to be obtained. For example, when the thread needs to send a 100 megabyte data packet, it needs to obtain 1 token from the token bucket, and when the thread needs to send a 200 megabyte data packet, it needs to obtain 2 tokens from the token bucket. It should be understood that the above example is only used for example. In actual applications, the size of the data packet and the number of tokens obtained can also be other values, which are not specifically limited here.
[0050] In programming languages, the number of tokens in a token bucket and the process of generating and consuming tokens in a token bucket can be expressed as follows, specifically:
[0051] In programming languages, the process of generating tokens in a token bucket can be expressed as follows:
[0052] (1) Convert increasing time into increasing tokens:
[0053] ΔToken=(now-t)*R;
[0054] Among them, R is the speed limit or guaranteed rate, the unit is the number of tokens / second, r is the number of cycles occupied by each token, Hz is the frequency of the clock source, now is a variable representing the current time, t is a variable representing the last time, and ΔToken is a variable representing the added token.
[0055] (2) Add the increased token to the token bucket:
[0056] If tc+ΔToken <m,tc=tc+ΔToken;
[0057] If tc+ΔToken≥m, tc=tc;
[0058] Among them, tc is the number of tokens in the token bucket, ΔToken is a variable representing the added tokens, and m is the bucket depth of the token bucket.
[0059] In programming languages, the consumption of tokens in a token bucket can be expressed as follows:
[0060] If tc>n, then tc=tc-n;
[0061] If tc≤n, then tc=tc;
[0062] Among them, tc is a variable representing the number of tokens in the token bucket, and n is a variable representing the number of tokens to be taken out.
[0063] When multiple threads share the same data link to send data packets, they need to apply for tokens from the same token bucket, and only one thread can grab the token in the token bucket at the same time and obtain the qualification to send data packets through the data link, thereby controlling the total transmission rate. Figure 2 As shown in the example, assuming that there are n CPUs in total, CPU1, CPU2, ..., CPUn, and each CPU starts a If thread 1 can preempt the token in the token bucket, thread 1 can send a data packet on the data link, and other threads enter the waiting queue to wait; if thread 2 can preempt the token in the token bucket, thread 2 can send a data packet on the data link, and other threads enter the waiting queue to wait, ..., if thread n can preempt the token in the token bucket, thread n can send a data packet on the data link, and other threads enter the waiting queue to wait. It should be understood that Figure 2 In the example shown, one thread is started in each CPU for illustration. In actual applications, each CPU can start two threads, three threads, four threads or more, which is not specifically limited here.
[0064] In order to share variables t and tc between different processes, variables t and tc can be stored in shared memory. At this time, different processes have the permission to modify variables t and tc. However, if different threads operate on variables t and tc at the same time, conflicts will occur. Therefore, in order to prevent different threads from operating on variables t and tc at the same time, variables t and tc need to be locked. Specifically, lock before adding tokens to the token bucket, and unlock after adding tokens to the token bucket; lock before consuming tokens in the token bucket, and unlock after consuming tokens in the token bucket, so as to achieve the goal of locking variables t and tc. More specifically,
[0065] In the case of locking, the token generation process in the token bucket can be expressed as follows:
[0066] (1) Lock.
[0067] (2) Convert the increasing time into increasing tokens:
[0068] ΔToken=(now-t)*R;
[0069] Among them, R is the speed limit or guaranteed rate, the unit is the number of tokens / second, r is the number of cycles occupied by each token, Hz is the frequency of the clock source, now is a variable representing the current time, t is a variable representing the last time, and ΔToken is a variable representing the added token.
[0070] (3) Add the increased token to the token bucket:
[0071] If tc+ΔToken <m,tc=tc+ΔToken;
[0072] If tc+ΔToken≥m, tc=tc;
[0073] Among them, tc is a variable representing the number of tokens in the token bucket, ΔToken is a variable representing the added tokens, and m is the bucket depth of the token bucket.
[0074] (4) Unlock.
[0075] In the case of locking, the consumption process of tokens in the token bucket can be expressed as follows:
[0076] (1) Lock.
[0077] (2) If tc>n, then tc=tc-n; if tc≤n, then tc=tc; where tc is a variable representing the number of tokens in the token bucket, and n is a variable representing the number of tokens to be taken out.
[0078] (3) Unlock.
[0079] When a thread locks variables t and tc, other threads will be blocked, so other threads will not be able to modify variables t and tc, thus avoiding conflicts. However, when one thread locks, the response is very slow and resources are wasted.
[0080] See also Figure 3 , Figure 3 1 is a schematic diagram of an application scenario involved in the present application. The application scenario is as follows: the network processing device 110 controls the total flow of the network nodes 120 under its jurisdiction. Among them, the network processor 110 can be a border router and a border server, etc. The network node 120 can be a container, a bare metal server, a virtual machine, a physical host, etc.
[0081] The following is explained with reference to a specific example. Message 1 from network address IP1 is received by network card 2 in network processing device 110 and uploaded to the thread in processing core 2. The thread in processing core 2 performs flow control according to the token bucket to determine whether message 1 needs to be sent to the corresponding network node 2. When it is determined that message 1 needs to be sent to the corresponding network node 2, the thread in processing core 2 sends message 1 to network card 2 and sends it to the corresponding network node 2 through network card 2. Message 1 from network address IP2 is received by network card 3 in network processing device 110 and uploaded to the thread in processing core 3. The thread in processing core 3 performs flow control according to the token bucket to determine whether message 2 needs to be sent to the corresponding network node 3. When it is determined that message 2 needs to be sent to the corresponding network node 3, processing core 3 sends message 2 to network card 3 and sends it to the corresponding network node 3 through network card 3. Similarly, message 3 from network node 2 is received by network card 2 in network processing device 110 and uploaded to the thread in processing core 2. The thread in the processing core 2 performs flow control according to the token bucket to determine whether the message 3 needs to be sent to the corresponding network address IP1. When it is determined that the message 3 needs to be sent to the corresponding network address IP1, the thread in the processing core 2 sends the message 3 to the network card 2, and sends it to the corresponding network address IP1 through the network card 2. The message 4 from the network node 3 is received by the network card 3 in the network processing device 110 and uploaded to the thread in the processing core 3. The thread in the processing core 3 performs flow control according to the token bucket to determine whether the message 4 needs to be sent to the corresponding network address IP2. When it is determined that the message 4 needs to be sent to the corresponding network address IP2, the thread in the processing core 3 sends the message 4 to the network card 3, and sends it to the corresponding network address IP2 through the network card 3.
[0082] Here, the downlink traffic sent from the network address to the network node and the uplink traffic sent from the network node to the network address can be controlled separately, or the downlink traffic sent from the network address to the network node and the uplink traffic sent from the network node to the network address can be generally controlled, without specific limitation here.
[0083] The following will describe how the tokens in the token bucket operate in conjunction with specific embodiments.
[0084] The size of the token bucket (i.e., the bucket depth) can be expressed in terms of the number of clock cycles. In a specific implementation, the token bucket size can be converted into the number of first clock cycles, including: N = CBS*Hz / R, where N is the number of first clock cycles, CBS is the maximum number of tokens that can be accommodated in the token bucket, r = Hz / R, r is the number of clock cycles occupied by each token, Hz is the frequency of the clock source of the network processing device, and R is the guaranteed rate.
[0085] The token bucket has two variables: the current number of clock cycles now and the recorded number of clock cycles c. Among them, the current number of clock cycles now increases naturally with the number of clock cycles of the clock source (for example, crystal oscillator) in the network processing device. For example, if the number of clock cycles of the current crystal oscillator is 74, then the current number of clock cycles of the token bucket can be 74. In the next clock cycle, if the number of clock cycles of the current crystal oscillator is 75, then the current number of clock cycles of the token bucket changes to 75 accordingly. In the above example, the ratio of the number of clock cycles of the crystal oscillator to the current number of clock cycles is 1:1. However, in other embodiments, other proportional relationships can also be adopted, which are not specifically limited here. As Figure 4A shown, when the difference between the current number of clock cycles now and the recorded number of clock cycles c is less than the first number of clock cycles, the recorded number of clock cycles c does not change. As Figure 4B shown, when the difference between the current number of clock cycles now and the recorded number of clock cycles c is greater than the first number of clock cycles, let the recorded number of clock cycles c be equal to the current number of clock cycles now minus the first number of clock cycles to keep the bucket depth unchanged.
[0086] At time T1, the current number of clock cycles of the token bucket is now1, and the recorded number of clock cycles is c1. The network device obtains the first packet from the receive queue and determines that the second number of clock cycles corresponding to the number of tokens required for the first packet is n1*r, where n1 is the number of tokens required to send the first packet, and r is the number of clock cycles occupied by each token. As Figure 5A shown, if c1 + n1*r < now1, then let c2 = c1 + n1*r and allow the first packet to be sent; as Figure 5B shown, if c1 + n1*r > now1, then let c2 = c1 and prohibit the first packet from being sent.
[0087] At time T2, the current number of clock cycles of the token bucket is now2, and the recorded number of clock cycles is c2. Among them, now2 is equal to now1 plus the newly added number of clock cycles between time T1 and time T2. The network device obtains the second packet from the receive queue and determines that the third number of clock cycles corresponding to the number of tokens required for the second packet is n2*r, where n2 is the number of tokens required to send the second packet, and r is the number of clock cycles occupied by each token. If c2 + n2*r < now2, then let c3 = c2 + n2*r and allow the second packet to be sent. If c2 + n2*r > now2, then let c3 = c2 and prohibit the second packet from being sent. The following will be specifically described in combination with the embodiments shown in Figure 5A and Figure 5B shown. Corresponding to the embodiment shown in Figure 5A shown, as Fig. 6AAs shown, if c2 + n2 * r = c1 + n1 * r + n2 * r < now2, then let c3 = c2 + n2 * r = c1 + n1 * r + n2 * r, and allow the second message to be sent; if Figure 6B As shown, if c2 + n2 * r = c1 + n1 * r + n2 * r > now2, then let c3 = c2 = c1 + n1 * r, and prohibit the second message from being sent. Corresponding to Figure 5B the embodiment shown, such as Fig. 7A As shown, if c2 + n2 * r = c1 + n2 * r < now2, then let c3 = c2 + n2 * r = c1 + n2 * r, and allow the second message to be sent; if Figure 7B As shown, if c2 + n2 * r = c1 + n2 * r > now2, then let c3 = c2 = c1, and prohibit the second message from being sent.
[0088] It can be understood that in order to enable different processes to share the recorded clock cycle count c, the recorded clock cycle count c can be stored in shared memory. At this time, different processes all have the permission to modify the recorded clock cycle count c.
[0089] Suppose there are thread 1 and thread 2 existing simultaneously, and both thread 1 and thread 2 can access the recorded clock cycle count c in shared memory. The initial value of the recorded clock cycle count in shared memory is 60, and the number of cycles r occupied by each token is 2. The following will introduce in detail the cases where there is no conflict between thread 1 and thread 2 and where there is a conflict between thread 1 and thread 2.
[0090] The first case: There is no conflict between thread 1 and thread 2.
[0091] As Figure 8 shown in (A) below, when thread 1 hopes to obtain 3 tokens from the token bucket, thread 1 needs to modify the value of the recorded clock cycle count c in shared memory from 60 to 60 + 3 * 2 = 66. At this time, for thread 1, the actual value in the shared memory address of the recorded clock cycle count c is 60, the old expected value is 60, and the new value to be modified is 66. Thread 1 compares the actual value 60 in the shared memory address of the recorded clock cycle count c with the old expected value 60, and the actual value 60 in the shared memory address of the recorded clock cycle count c is equal to the old expected value 60, so thread 1 successfully updates the actual value in the shared memory address of the recorded clock cycle count c to 66.
[0092] As Figure 8As shown in (B), after thread 1 completes the modification of the recorded clock cycle c, thread 2 hopes to obtain 4 tokens from the token bucket, and thread 2 needs to modify the value of the recorded clock cycle number c in the shared memory from 66 to 66+4*2=74. At this time, for thread 2, the actual value in the shared memory address of the recorded clock cycle number c is 66, the old expected value is 66, and the new value to be modified is 74. Thread 2 compares the actual value 66 in the shared memory address of the recorded clock cycle number c with the old expected value 66. The actual value 66 in the shared memory address of the recorded clock cycle number c is equal to the old expected value 66, so thread 2 successfully updates the actual value in the shared memory address of the recorded clock cycle number c to 74.
[0093] The second type: Thread 1 and Thread 2 conflict.
[0094] like Fig. 9 As shown in (A), thread 2 wants to obtain 4 tokens from the token bucket, and thread 2 wants to modify the value of the number of clock cycles c in the shared memory from 60 to 60+4*2=68. At this time, for thread 2, the actual value in the shared memory address recording the number of clock cycles c is 60, the old expected value is 60, and the new value to be modified is 68. Thread 2 compares the actual value 60 in the shared memory address recording the number of clock cycles c with the old expected value 60. The actual value 60 in the shared memory address recording the number of clock cycles c is equal to the old expected value 60, so thread 2 successfully updates the actual value in the shared memory address recording the number of clock cycles c to 68.
[0095] At the same time, Fig. 9 As shown in (B), thread 1 wants to obtain 3 tokens from the token bucket, so thread 1 wants to modify the value of the number of clock cycles c in the shared memory from 60 to 60+3*2=66. At this time, thread 2 has modified the actual value in the shared memory address of the number of clock cycles c to 68. Therefore, for thread 1, the actual value in the shared memory address of the number of clock cycles c is 68, the old expected value is 60, and the new value to be modified is 66. Thread 1 compares the actual value 68 in the shared memory address of the number of clock cycles c with the old expected value 60. The actual value in the shared memory address of the number of clock cycles c is not equal to the old expected value, and thread 1 fails to modify the actual value in the shared memory address of the number of clock cycles c.
[0096] So, if Fig. 9As shown in (C), thread 1 tries again, so thread 1 wants to modify the value of the number of clock cycles c recorded in the shared memory from 68 to 68+3*2=74. At this time, for thread 1, the actual value in the shared memory address recording the number of clock cycles c is 68, the old expected value is 68, and the new value to be modified is 74. Thread 1 compares the actual value 68 in the shared memory address recording the number of clock cycles c with the old expected value 68. The actual value 68 in the shared memory address recording the number of clock cycles c is equal to the old expected value 68, so thread 1 successfully updates the actual value in the shared memory address recording the number of clock cycles c to 74.
[0097] It should be understood that the above examples are only examples of thread 1 and thread 2. In actual applications, the number of threads can be greater. In addition, the initial value of the variable c in the above threads, the number of tokens that each thread hopes to obtain, the number of cycles occupied by each token, etc. are only specific examples. In actual applications, other values can also be used, and are not specifically limited here.
[0098] The present application proposes a flow chart of a method for limiting the speed of a message. The method for limiting the speed of a message in this embodiment specifically includes the following steps:
[0099] S101: A network processing device converts a token bucket size into a first clock cycle number.
[0100] In a specific embodiment of the present application, the size of the token bucket (i.e., the bucket depth) can be expressed in terms of the number of clock cycles. In a specific implementation, the token bucket size can be converted into the first clock cycle number, including: N = CBS*Hz / R, where N is the first clock cycle number, CBS is the maximum number of tokens that can be accommodated in the token bucket, r = Hz / R, r is the number of clock cycles occupied by each token, Hz is the frequency of the clock source of the network processing device, and R is the guaranteed rate.
[0101] In a specific embodiment of the present application, the token bucket has two variables: the current number of clock cycles now and the recorded number of clock cycles c. Among them, the current number of clock cycles now naturally increases with the number of clock cycles of the clock source (for example, a crystal oscillator) in the network processing device. For example, if the current number of clock cycles of the crystal oscillator is 74, then the current number of clock cycles of the token bucket can be 74. In the next clock cycle, the current number of clock cycles of the crystal oscillator is 75, then the current number of clock cycles of the token bucket changes to 75. In the above example, the relationship between the number of clock cycles of the crystal oscillator and the current number of clock cycles is 1:1. However, in other embodiments, other proportional relationships may also be used, which are not specifically limited here. For example Figure 4AAs shown, when the difference between the current number of clock cycles now and the recorded number of clock cycles c is less than the first number of clock cycles, the recorded number of clock cycles c remains unchanged, as Figure 4B shown, when the difference between the current number of clock cycles now and the recorded number of clock cycles c is greater than the first number of clock cycles, let the recorded number of clock cycles c be equal to the current number of clock cycles now minus the first number of clock cycles to keep the bucket depth unchanged.
[0102] S102: The network processing device obtains a first packet at a first moment and obtains the number of second clock cycles corresponding to the number of tokens required for the first packet.
[0103] In a specific embodiment of the present application, the network processing device may obtain the first packet from the first receive queue, where the first packet may be a packet that goes up or down through the network processing device.
[0104] In a specific embodiment of the present application, the number of second clock cycles may be calculated by n1*r, where n1 is the number of tokens required to send the first packet and r is the number of clock cycles occupied by each token.
[0105] S103: The network processing device determines whether the number of second clock cycles is greater than the first number of clock cycles.
[0106] S104: When the number of second clock cycles is not greater than the first number of clock cycles, the network processing device sends the first packet and enters step S106.
[0107] In a specific embodiment of the present application, the recorded number of clock cycles c1 is zero, the current number of clock cycles now1 is equal to the first number of clock cycles, and the number of second clock cycles is n1*r. According to if c1 + n1*r < now1, then let c2 = c1 + n1r and allow the first packet to be sent, it can be known that here, when the number of second clock cycles is less than the first number of clock cycles, the first packet is allowed to be sent.
[0108] S105: When the number of second clock cycles is greater than the first number of clock cycles, the network processing device discards the first packet.
[0109] In a specific embodiment of the present application, the recorded number of clock cycles c1 is zero, the current number of clock cycles now1 is equal to the first number of clock cycles, and the number of second clock cycles is n1*r. According to if c1 + n1*r > now1, then let c2 = c1 and do not allow the first packet to be sent, it can be known that here, when the number of second clock cycles is greater than the first number of clock cycles, the first packet is not allowed to be sent.
[0110] S106: After sending the first message, at the second moment, the network processing device obtains a second message and the number of third clock cycles corresponding to the number of tokens required to obtain the second message.
[0111] In a specific embodiment of the present application, the network processing device may obtain the second message from the second receive queue, where the second message may be a message that goes up or down through the network processing device. Here, the first receive queue and the second receive queue may be the same receive queue or different receive queues.
[0112] In a specific embodiment of the present application, the number of third clock cycles may be calculated by n2 * r, where n2 is the number of tokens required to send the second message and r is the number of clock cycles occupied by each token.
[0113] S107: The network processing device determines whether the sum of the number of second clock cycles and the number of third clock cycles is greater than the sum of the number of clock cycles newly added between the first moment and the second moment and the number of first clock cycles.
[0114] S108: When the sum of the number of second clock cycles and the number of third clock cycles is not greater than the sum of the number of clock cycles newly added between the first moment and the second moment and the number of first clock cycles, send the second message.
[0115] In a specific embodiment of the present application, the recorded number of clock cycles c2 is equal to the number of second clock cycles n1 * r, and the number of third clock cycles is n2 * r. The current number of clock cycles now2 is equal to the sum of the number of clock cycles newly added between the first moment and the second moment and the number of first clock cycles. Therefore, according to if c2 + n2 * r < now2, then let c3 = c2 + n2 * r and allow the first message to be sent, it can be known that here, when the sum of the number of second clock cycles and the number of third clock cycles is not greater than the sum of the number of clock cycles newly added between the first moment and the second moment and the number of first clock cycles, send the second message.
[0116] S109: When the sum of the number of second clock cycles and the number of third clock cycles is greater than the sum of the number of clock cycles newly added between the first moment and the second moment and the number of first clock cycles, discard the second message.
[0117] In a specific embodiment of the present application, the recorded clock cycle count c2 is equal to the second clock cycle count being n1 * r, and the third clock cycle count is n2 * r. The current clock cycle count now2 is equal to the sum of the newly added clock cycle count between the first moment and the second moment and the first clock cycle count. Therefore, according to if c2 + n2 * r > now2, then set c3 = c2 and allow the second message to be sent, it can be known that here, when the sum of the second clock cycle count and the third clock cycle count is greater than the sum of the newly added clock cycle count between the first moment and the second moment and the first clock cycle count, the second message is not allowed to be sent.
[0118] S110: After discarding the second message, the network processing device obtains a third message at a third moment, and obtains the fourth clock cycle count corresponding to the number of tokens required for obtaining the third message.
[0119] In a specific embodiment of the present application, the fourth clock cycle count can be calculated by n3 * r, where n3 is the number of tokens required to send the third message, and r is the number of clock cycles occupied by each token.
[0120] S111: The network processing device determines whether the sum of the second clock cycle count and the fourth clock cycle count is not greater than the sum of the newly added clock cycle count between the first moment and the third moment and the first clock cycle count.
[0121] S112: When the sum of the second clock cycle count and the fourth clock cycle count is not greater than the sum of the newly added clock cycle count between the first moment and the third moment and the first clock cycle count, send the third message.
[0122] In a specific embodiment of the present application, in the case where the second message is discarded, the recorded clock cycle count c3 is equal to the second clock cycle count being n2 * r, and the fourth clock cycle count is n3 * r. The current clock cycle count now3 is equal to the sum of the newly added clock cycle count between the first moment and the third moment and the first clock cycle count. Therefore, according to if c3 + n3 * r < now3, then set c3 = c2 + n2 * r and allow the third message to be sent, it can be known that here, when the sum of the second clock cycle count and the fourth clock cycle count is not greater than the sum of the newly added clock cycle count between the first moment and the second moment and the first clock cycle count, the third message is sent.
[0123] S113: When the sum of the second clock cycle count and the fourth clock cycle count is greater than the sum of the newly added clock cycle count between the first moment and the third moment and the first clock cycle count, discard the third message.
[0124] In a specific embodiment of the present application, when the second message is discarded, the number of recorded clock cycles c3 is equal to the number of second clock cycles n2*r, and the number of fourth clock cycles n3*r. The current number of clock cycles now3 is equal to the sum of the number of newly added clock cycles between the first moment and the third moment and the number of the first clock cycles. Therefore, according to if c3+n3*r>now3, then let c3=c2, and do not allow the third message to be sent, it can be known that here, when the sum of the second clock cycle and the fourth clock cycle is greater than the sum of the number of newly added clock cycles between the first moment and the third moment and the number of the first clock cycles, the third message is discarded.
[0125] See also Fig.10 , Fig.10 The network processing device of this embodiment includes: a conversion module 210 , an acquisition module 220 and a sending module 230 .
[0126] The conversion module 210 is used to convert the token bucket size into the number of first clock cycles.
[0127] In a specific embodiment of the present application, the size of the token bucket (i.e., the bucket depth) can be expressed in terms of the number of clock cycles. In a specific implementation, the token bucket size can be converted into the first clock cycle number, including: N = CBS*Hz / R, where N is the first clock cycle number, CBS is the maximum number of tokens that can be accommodated in the token bucket, r = Hz / R, r is the number of clock cycles occupied by each token, Hz is the frequency of the clock source of the network processing device, and R is the guaranteed rate.
[0128] In a specific embodiment of the present application, the token bucket has two variables: the current number of clock cycles now and the recorded number of clock cycles c. Among them, the current number of clock cycles now naturally increases with the number of clock cycles of the clock source (for example, a crystal oscillator) in the network processing device. For example, if the current number of clock cycles of the crystal oscillator is 74, then the current number of clock cycles of the token bucket can be 74. In the next clock cycle, the current number of clock cycles of the crystal oscillator is 75, then the current number of clock cycles of the token bucket changes to 75. In the above example, the relationship between the number of clock cycles of the crystal oscillator and the current number of clock cycles is 1:1. However, in other embodiments, other proportional relationships may also be used, which are not specifically limited here. For example Figure 4A As shown, when the difference between the current clock cycle number now and the recorded clock cycle number c is less than the first clock cycle number, the recorded clock cycle number c does not change, such as Figure 4BAs shown, when the difference between the current number of clock cycles now and the recorded number of clock cycles c is greater than the first number of clock cycles, the recorded number of clock cycles c is set equal to the current number of clock cycles now minus the first number of clock cycles to keep the bucket depth unchanged.
[0129] The obtaining module 220 is configured to obtain a first packet at a first moment and obtain a second number of clock cycles corresponding to the number of tokens required for the first packet.
[0130] In a specific embodiment of the present application, the network processing device may obtain the first packet from the first receiving queue, where the first packet may be a packet transmitted upstream or downstream by the network processing device.
[0131] In a specific embodiment of the present application, the second number of clock cycles may be calculated by n1 * r, where n1 is the number of tokens required to transmit the first packet and r is the number of clock cycles occupied by each token.
[0132] The sending module 230 is configured to send the first packet when the second number of clock cycles is not greater than the first number of clock cycles.
[0133] In a specific embodiment of the present application, the recorded number of clock cycles c1 is zero, the current number of clock cycles now1 is equal to the first number of clock cycles, and the second number of clock cycles is n1 * r. According to if c1 + n1 * r < now1, then set c2 = c1 + n1r and allow the first packet to be sent, it can be known that here, when the second number of clock cycles is less than the first number of clock cycles, the first packet is allowed to be sent.
[0134] For the sake of simplicity, the above cases are not introduced in detail. For specific details, please refer to Figures 3 to 10 and related descriptions, which will not be elaborated here.
[0135] See Fig.11 , Fig.11 FIG. is a schematic structural diagram of a network processing device provided by the present application. The network processing device of this embodiment includes: a processor 310, a memory 320, a transceiver 330, and a bus 340. The bus 340 is used to connect the processor 310, the memory 320, and the transceiver 330.
[0136] The processor 310 may be one or more general-purpose processors, wherein the general-purpose processor may be any type of device capable of processing electronic instructions, including a central processing unit (CPU), a microprocessor, a microcontroller, a main processor, a controller, and an application-specific integrated circuit (ASIC), etc. The processor 310 executes various types of digital storage instructions, such as software or firmware programs stored in the memory 320. In a specific embodiment, the processor 310 may be an x86 processor, etc. The processor 310 sends commands to the memory 320 through a physical interface to complete storage-related tasks. For example, the commands that the processor 310 may provide include read commands, write commands, copy commands, and erase commands, etc. The commands may specify operations related to specific pages and blocks of the memory 320. As an example, a command may request that data be written to a specific physical page, or another command may request that a specific physical block be erased. Multiple virtual machines may be virtualized in the processor 310, and a corresponding operating system may be installed on each virtual machine. The processor has multiple processing cores and may run multiple threads, wherein the multiple threads include a first thread, a second thread, ..., and an nth thread.
[0137] The memory 320 may include a random access memory (RAM), a flash memory, etc., or may be a RAM, a read-only memory (ROM), a hard disk (HDD), or a solid-state drive (SSD). The memory 320 is provided with a shared memory, and the first thread to the nth thread share the shared memory in the memory 320.
[0138] The transceiver 330 may be used to receive and send messages, and upload received messages to the processor 310 for processing.
[0139] Understandably, Fig.11 The network processing device shown can execute the above-mentioned message rate limiting method. For details, please refer to the relevant description of the above-mentioned message rate limiting method, and no specific limitation is made here.
[0140] In the above scheme, flow control is performed by the number of clock cycles, with only one variable. Therefore, conflicts can be handled through atomic operations. Moreover, atomic operations have fast response times and consume fewer resources, which are far superior to locking operations.
[0141] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium, (e.g., a floppy disk, a storage disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state storage disk Solid State Disk (SSD)), etc.
Claims
1. A message rate limiting method, characterized in that: include: Converting the token bucket size into a first clock cycle number based on the maximum number of tokens that can be accommodated in the token bucket, the number of clock cycles occupied by each token, the frequency of a clock source of the network processing device, and the guaranteed rate; Obtain a first message at a first moment, and obtain a second clock cycle number corresponding to a number of tokens required for the first message; When the second clock cycle number is not greater than the first clock cycle number, the first message is sent, wherein the first clock cycle number is conflict-handled using atomic operations.
2. The method according to claim 1, characterized in that Also includes: Obtain a second message at a second moment, and obtain a third clock cycle number corresponding to the number of tokens required for the second message; When the sum of the second clock cycle number and the third clock cycle number is not greater than the sum of the newly added clock cycle number between the first moment and the second moment and the first clock cycle number, the second message is sent.
3. The method according to claim 2, characterized in that Also includes: When the sum of the second clock cycle number and the third clock cycle number is greater than the sum of the first clock cycle number and the first clock cycle number added between the first moment and the second moment, the second message is discarded.
4. The method according to claim 3, characterized in that Also includes: Obtain a third message at a third moment, and obtain a fourth clock cycle number corresponding to the number of tokens required for the third message; When the sum of the second clock cycle number and the fourth clock cycle number is not greater than the sum of the first clock cycle number and the first clock cycle number between the first moment and the third moment, the third message is sent.
5. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: When the second clock cycle number is greater than the first clock cycle number, the first message is discarded.
6. The method according to any one of claims 1 to 4, characterized in that: Convert the token bucket size to the number of first clock cycles, including: in, is the number of first clock cycles, is the maximum number of tokens that can be contained in the token bucket. , is the number of clock cycles taken by each token, The frequency of the clock source for the network processing device. To ensure speed.
7. A network processing device, characterized in that: include: Conversion module, acquisition module and sending module, The conversion module is used to convert the token bucket size into a first clock cycle number based on the maximum number of tokens that can be accommodated in the token bucket, the number of clock cycles occupied by each token, the frequency of the clock source of the network processing device, and the guaranteed rate; The acquisition module is used to acquire a first message at a first moment, and acquire the number of second clock cycles corresponding to the number of tokens required for the first message; The sending module is used to send the first message when the second clock cycle number is not greater than the first clock cycle number, wherein the first clock cycle number is conflict-handled using atomic operations.
8. The device according to claim 7, characterized in that The acquisition module is used to acquire the second message at the second moment, and acquire the third clock cycle number corresponding to the number of tokens required for the second message; The sending module is used to send the second message when the sum of the second clock cycle number and the third clock cycle number is not greater than the sum of the newly added clock cycle number between the first moment and the second moment and the first clock cycle number.
9. The device according to claim 8, characterized in that The sending module is used to discard the second message when the sum of the second clock cycle number and the third clock cycle number is greater than the sum of the newly added clock cycle number between the first moment and the second moment and the first clock cycle number.
10. The device according to claim 9, characterized in that The acquisition module is used to acquire a third message at a third moment, and acquire a fourth clock cycle number corresponding to the number of tokens required for the third message; The sending module is used to send the third message when the sum of the second clock cycle number and the fourth clock cycle number is not greater than the sum of the newly added clock cycle number between the first moment and the third moment and the first clock cycle number.
11. The device according to any one of claims 7 to 10, characterized in that The sending module is used to discard the first message when the number of second clock cycles is greater than the number of first clock cycles.
12. The device according to any one of claims 7 to 10, characterized in that The conversion module is also used for: in, is the number of first clock cycles, is the maximum number of tokens that can be contained in the token bucket. , is the number of clock cycles taken by each token, The frequency of the clock source for the network processing device. To ensure speed.
13. A readable storage medium, characterized in that: The method comprises instructions, which, when executed on a network processing device, cause the network processing device to execute the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Token bucket limiting speed method and apparatus
CN104980367A