Traffic Shaping
This is a mechanism to control the amount and the rate of the traffic sent to the network.
Two techniques can shape traffic:
- Token Bucket
- Leaky Bucket
Leaky Bucket
Suppose we have a bucket in which we are pouring water in a random order but we have to get water in a fixed rate , for this we will make a hole at the bottom of the bucket. It will ensure that water coming out is in a fixed rate . And also if bucket is full we will stop pouring in it.
- If the bucket (buffer) overflows then packets are discarded.
- Input rate can vary but the output rate remains constant.
class LeakyBucketLimiter {
public:
bool addDropToBucket(int timestamp) {
if (timeOfLastLeak != -1) {
int delta = timestamp - timeOfLastLeak;
int numToLeak = delta * DROP_RATE;
numOfDrops = max(0, numOfDrops - numToLeak);
timeOfLastLeak = timestamp;
}
if (numOfDrops < BUCKET_SIZE) {
numOfDrops++;
return true; // drop added
}
return false; // overflow
}
private:
int numOfDrops = 0;
int timeOfLastLeak = -1;
int BUCKET_SIZE = 100;
int DROP_RATE = 20;
};
Token Bucket
- A token is added at every ∆t time.
- The bucket can hold at most b-tokens. If a token arrive when bucket is full it is discarded, not the packet.
- When a packet of m bytes arrived m tokens are removed from the bucket and the packet is sent to the network.
- If less than n tokens are available no tokens are removed from the buckets and the packet may be enqueued for subsequent transmission when sufficient token have been accumulated in the bucket.
If C is the maximum capacity of bucket and ρ is the arrival rate and M is the maximum output rate then Burst Length S can be calculated as
Pros and Cons
Main advantage of token Bucket over leaky bucket
- If bucket is full in token Bucket , token are discard not packets. While in leaky bucket , packets are discarded.
- Token Bucket can send Large bursts can faster rate while leaky bucket always sends packets at constant rate.
file:///Users/xiaojiang/Downloads/trafficshapingpresentation-140126060026-phpapp01.pdf