Background technology
In existing data network,, usually can cause network congestion because message flow is sudden.A kind of method that solves network congestion is, limits in the speed of receiving terminal to message, if the speed of message is lower than the speed of regulation, then normally receives message; If the speed of message surpasses the speed of regulation,, perhaps reduce priority of messages etc. then with packet loss.
The method that generally adopts is to utilize token bucket that message is carried out speed limit at present.As shown in Figure 1, system's speed is in accordance with regulations constantly filled token in token bucket, till token bucket is filled.When message arrives, follow the token number in the token bucket to compare the length of message, if enough tokens are arranged in the token bucket, then message allows to pass through, and deducts the pairing token number of message length simultaneously from token bucket; If the token in the token bucket is not enough,, perhaps reduce the transmission priority of message then with packet loss.
In above-mentioned existing method, the fill rate of token has limited the speed that receives message, and the capacity of token bucket then provides certain property held to the burst flow of message.In this method, the filling of token is regularly, promptly adds certain token every the regular hour in token bucket.This time is fixed, and can have contradiction on size is selected.If it is less that this time interval is selected, then token need be filled continually by system, and system loading is heavier, and for the slower message of speed, speed limit efficient is very low; If it is bigger that the time interval is selected, then, might lose the effect of speed limit for speed message faster.
The Chinese application number of the patent of Huawei Tech Co., Ltd " adopting Token Bucket to carry out the method for message flow limitation ": 03109091.5 application has proposed a kind of new token filling mode, fills token with message to being used as trigger condition.Time interval when this method need be calculated current message and the arrival of a last message according to the timer of internal system.If the maximum magnitude interval greater than the internal system timer between two messages, the time interval that then comes out is inaccurate.In other words, this method has requirement on the scope to the time interval between two messages, has certain limitation.
In addition, above-mentioned two class methods are all just carried out the speed limit of message after receiving a complete message.If the speed of message has surpassed the speed of regulation, and length is very long, could be but still will receive whole message with packet loss, then be unfavorable for reducing in the system message forwarding time of delay.
Therefore, also there is certain defective in prior art, and awaits improving and development.
Summary of the invention
The object of the present invention is to provide a kind of method of the message rate-limiting based on token bucket, at inefficiency in the existing message rate-limiting technology, and be unfavorable for reducing defectives such as message forward delay, a kind of improved message rate-limiting method is proposed, message is carried out segmentation by regular length, is that unit carries out speed limit to message with the data segment; Simultaneously, use a dual port RAM (random access storage device: Random Access Memory) come the time difference between continuous two data segments in the accounting message, being used for the filling token number of computational token bucket, message forwarding time of delay in the minimizing system.
Technical scheme of the present invention is as follows:
A kind of message rate-limiting method based on token bucket, it comprises the steps:
A, when receiving the data segment of a certain message, trigger the operation once fill token, in described token bucket, fill the token of some;
B, judge that whether the token in the token bucket surpasses the length of message data section, if token number more than or equal to data segment, length, then allows this data segment to pass through, and deducts the pairing token number of message data segment length from token bucket;
If the C token number is less than the length of message data section, then abandon this data segment, whole message under this data segment all is dropped, and the data segment follow-up to this message no longer carries out the judgement of token number, and the data segment corresponding token number that has passed through of this message is returned to described token bucket.
Described method, wherein, described method also is provided with a memory, and described steps A also comprises:
A1, judge that whether the current data section is first data segment of the message that token bucket is handled in the system, if, change steps A 4, otherwise, steps A 2 changeed;
A2, from described memory, read the time of reception of previous data segment,, calculate the time difference between two data segments according to the time in the current system timer;
A3, according to the time difference between two message data sections and the fill rate of token, calculate current token number of need filling, carry out the operation of filling token;
A4, current system time is write into described memory, cover original data segment time of reception record.
Described method, wherein, described data segment is a regular length, the minimum length of the message of being supported in the employing system.
Described method wherein, also is provided with a timer, is used for system time is counted, and all messages are all shared a system timer.
Described method, wherein, comprise also regularly described memory scanned that the token bucket required time that fills up a sky with the token fill rate of minimum is the time difference maximum, for the difference of those and current system time the peaked memory cell of overtime difference identified.
Described method, wherein, described memory adopts dual port RAM, is used for the calculating of time difference flatly, is used for the scanning of time difference flatly.
The method of a kind of message rate-limiting based on token bucket provided by the present invention, because the token number when employing is carried out message rate-limiting to message fragment relatively, can identify above the message of schedule speed in the short period of time and abandoned, reduce in the system message forwarding time of delay; In addition, the inventive method adopts the time of reception of a dual port RAM recorded message data segment and regularly scans, and can accurately calculate the time difference between the adjacent two data section in the message, thereby improve the accuracy of message rate-limiting in the system.
Embodiment
Below in conjunction with accompanying drawing, will be described in detail the method for the invention.
The method of the message rate-limiting based on token bucket of the present invention specifically, when whenever receiving the data segment of regular length in the message, promptly triggers the operation of once filling token, fills the token of some in token bucket.Judge that then whether the token in the token bucket surpasses the length of message data section, if token number more than or equal to data segment, length, then allows this data segment to pass through, and deducts the pairing token number of message data segment length from token bucket; If token number less than the length of message data section, then abandons this data segment, the whole message under this data segment all will be dropped simultaneously, and the follow-up data segment of this message no longer carries out the judgement of token number.That is to say, have only all data segments of message all allow by the time, this message could pass through, and has so just reduced and has judged the waste of whole data segment to resource, has improved treatment effeciency and speed.
The key step of the method for the invention comprises as shown in Figure 2:
(1) the message data section of reception regular length;
(2) judge whether the current data section is first data segment that token bucket is handled in the system, if, change step (6), otherwise, step (3) changeed.
(3) from RAM, read the time of reception of previous message data section;
(4), calculate the time difference between two message data sections according to the time in the current system timer;
(5) according to the time difference between two message data sections and the fill rate of token, calculate the current token number that needs filling, be filled in the middle of the token bucket then;
(6) current system time is write into RAM, cover original message data section time of reception;
(7) judge that token in the token bucket whether greater than the length of current data section, if token number more than or equal to the length of data segment, then receives the current data section, deducts data segment corresponding token number from token bucket, and the data length that passed through of accounting message; If token number less than the length of data segment, then abandons the current data section, simultaneously with whole packet loss, the recovery of board of playing drinking games of going forward side by side is about to the pairing token number of data length that message passed through and is added back in the token bucket.In other words, the message that is dropped does not consume the token in the token bucket.
After current message data section was passed through, the data segment, length that needs the affiliated message of statistics current data section to pass through judged simultaneously whether current message data section is last data segment of message.If not last data segment, then proceed the rate limit of next message data section; If last data segment, then finish the rate limit of current message, carry out the rate limit of next message.Have only after all data segments of message all pass through, this message just passes through.In case the message data section that receives is the control by token bucket not, then whole message is dropped, the board of playing drinking games of going forward side by side recovers, the pairing token number of data segment of this message that has passed through before is added back in the token bucket, finish the rate limit of current message then, carry out the rate limit of next message.
After system finished initialization, token bucket was in full state, so first data segment does not need to add token in token bucket when arriving.
In the inventive method, the length of data segment is fixed in the message, the minimum length of the message of being supported in the suggestion employing system.If the length of last data segment of message is less than L, then the physical length with data segment compares with the token in the token bucket.
The inventive method needs in the token bucket quantity of filling token equal the token fill rate and multiply by time difference between a current message data section and the last message data section when receiving the data segment of a message.Because the capacity of token bucket is limited, so fill the capacity that the quantity of token the most very much not can surpass token bucket at every turn.With the needed time of token bucket that the token fill rate of minimum fills up a sky, be the maximum of time difference.That is to say that if the actual time between two message data sections, difference surpassed above-mentioned time difference maximum, then can calculate with the maximum of time difference needs the token number of filling.
When the time difference of adding up between two message data sections, need a timer, system time is counted.The scope of timer is limited, generally adopts the maximum of above-mentioned time difference.In data network, for the needs of service quality (QoS:Quality of Service) are provided, at first message to be classified, dissimilar messages has different rate limit.Have a large amount of type of messages in the General System, it is unpractical on hardware is realized that every type message all adopts a timer, and therefore, in the inventive method, all messages are all shared a system timer.When certain message data section arrives, the numerical value of this moment system timer is kept in the internal storage, when next message data section arrives, utilize the numerical value in the current time system timer, calculate the time difference between two message data sections, simultaneously the numerical value in the current time system timer is kept in the internal storage, covers original numerical value, be used to calculate the next time difference.
Because timer is a cycle count, and the time difference between two data segments is arbitrarily, therefore, must regularly scan internal storage, for the difference of those and current system time the peaked memory cell of overtime difference identified.Concerning memory, the calculating of time difference and the scanning of time difference are two independently processes, and memory adopts dual port RAM among the present invention, are used for the calculating of time difference flatly, are used for the scanning of time difference flatly.
Be the schematic flow sheet that the time difference calculates among the present invention as shown in Figure 3.
Suppose that it is T that token fill rate with minimum fills up a needed time of empty token bucket
Max, the binary number of corresponding n bit wide.Surpass T when the time interval of two message data sections
MaxThe time, the time difference Δ T of required calculating is T
Max
The form that is kept at the time value among the RAM is schematically as follows:
Title |
The position |
Explanation |
The overflow indicator position |
N+1 |
Time difference overflow indicator position is obtained by time difference scanning.After overflowing, Δ T=T
Max |
Carry flag bit |
n |
The carry flag of system timer. |
When the message data section arrives |
(n-1 |
The number of system timer when receiving the message data section |
Whenever receiving length when being the message data section of L, just trigger the computational process of a time difference.Value T with current system timer
2With the last message data section moment T that is kept among the RAM
1Compare, draw two time difference Δ T between the message data section.
T in reading RAM
1After the value, if T
1The overflow indicator position be 1, expression elapsed time difference scanning, this time difference is the maximum of overtime difference, so Δ T=T
Max
If T
1Still do not overflow, then compare T
1And T
2Carry flag.If T
1And T
2Carry flag identical, and T
2>T
1, then represent from T
1Constantly begin not circulation of system timer (the timer meter is to maximum reset-to-zero reclocking afterwards), then Δ T=T to current time
2-T
1If T
1And T
2Carry flag identical, and T
2≤ T
1, then represent from T
1Constantly begin to current time, system's meter has circulated twice, then Δ T=T
Max
If T
1And T
2The carry flag difference, then represent from T
1Constantly begin to current time, system timer is circulated throughout once.If T
2>T
1, Δ T=T then
MaxIf T
2≤ T
1, Δ T=T then
Max+ T
2-T
1
Be the process schematic diagram of time difference scanning among the present invention as shown in Figure 4.
The scanning process of time difference and the compute classes of time difference seemingly but trigger different constantly.In the inventive method, in the time difference maximum of T
MaxIn cycle, need carry out single pass successively to all unit of RAM, judge that the moment of preserving in each unit compares constantly with its scanning, whether the time difference has exceeded maximum of T
MaxIf the time difference has exceeded maximum, then with corresponding overflow indicator position 1, expression Δ T=T
Max
During scanning, with current time T
3With the last message data section moment T that is kept among the RAM
1Compare.If T
1Overflow indicator be 1, during time difference scanning before then being illustrated in, the time difference of this unit overflows, any operation is not done in current scanning, carries out the scanning of next ram cell.
If T
1Overflow indicator be 0, then the express time difference is not also overflowed.Compare T
1And T
3Carry flag.If T
1And T
3Carry flag identical, and T
3>T
1, then represent from T
1Constantly begin to current time, system timer is circulation not, and then the time difference does not overflow, and finishes the scanning of current ram cell, carries out the scanning of next unit.If T
1And T
3Carry flag identical, and T
3≤ T
1, then represent from T
1Constantly begin to current time, system's meter has circulated twice, and then the time difference overflows, and with overflow indicator position 1, carries out the scanning of next ram cell then.
If T
1And T
3The carry flag difference, then represent from T
1Constantly begin to current time, system timer is circulated throughout once.If T
3>T
1, then the time difference overflows, and with overflow indicator position 1, carries out the scanning of next ram cell then.If T
3≤ T
1, then the time difference does not overflow as yet, finishes the scanning of current ram cell, carries out the scanning of next unit.
Concerning each ram cell, the time interval of twice scanning is T
Max, so just guaranteed to be no more than 2 * T according to a carry flag
MaxTime in, whether in time judge the pairing time difference of time that this ram cell preserves overflows.
The calculating of time difference is constantly relevant with the arrival of message data section, fix the sweep time of time difference, so might carry out time difference calculating and time difference scanning simultaneously to the same unit in the memory RAM, promptly the same address of dual port RAM be carried out read-write operation simultaneously.In fact, time difference scanning also is to calculate a kind of time difference, therefore, for fear of the same unit of memory is operated simultaneously, if the time difference calculates and time difference scanning takes place simultaneously, then only carries out the time difference and calculates, and ignores time difference scanning.
The method of the message rate-limiting based on token bucket of the present invention, since adopted when message fragment carried out message rate-limiting token number relatively, identify the message that surpasses schedule speed in the short period of time and abandoned, reduced in the system message forwarding time of delay; The inventive method also adopts the time of reception of a dual port RAM recorded message data segment and regularly scans simultaneously, can accurately calculate the time difference between the adjacent two data section in the message, thereby improved the accuracy of message rate-limiting in the system, computational efficiency and speed have been improved, the used resources conservation of computing.
Should be understood that above-mentioned description at specific embodiment is comparatively detailed, can not therefore think the restriction to scope of patent protection of the present invention, scope of patent protection of the present invention should be as the criterion with claims.