removed apps
[luayats.git] / src / user / leakybucket.c
blob16737335dabf8ab15694f914faf84027c474be66
1 #include "leakybucket.h"
2 #include <math.h> // for ceil
5 /////////////////////////////////////////////////////////////////////////////
6 // Dual Leaky Bucket
7 /////////////////////////////////////////////////////////////////////////////
9 // Issue: we need to change the code to be according to the ATMF GCRA
10 // and to be with integer logic!
12 // Issue: what is implemented now is a dual token bucket similar to the
13 // "Two-Rate Three Color Marker" defined in RFC2698
14 // the counter, however, counts up for each packet (in RFC2698 it counts down)
17 LeakyBucket::LeakyBucket(void)
19 value = 0;
20 max = 0;
21 last_update = 0;
22 } // LeakyBucket::LeakyBucket(void) - constructor
24 LeakyBucket::~LeakyBucket(void)
28 void LeakyBucket::SetParam(double rate, int burstsize)
30 decrement_per_slot = rate / 8.0 * SlotLength;
31 max = burstsize;
33 //printf("LeakyBucket::SetParam : decrement_per_slot=%f max=%f\n",
34 // decrement_per_slot, max);
36 } // LeakyBucket::SetParam(double rate, int burstsize)
38 /////////////////////////////////////////////////////////
39 // LeakyBucket::PacketArrivalConformance
40 // A packet of with framelength "bytes" has arrived, is
41 // this packet conforming?
42 // The buckets are increased
43 // Return value:
44 // 0 - packet is not conforming
45 // 1 - packet is conforming
46 /////////////////////////////////////////////////////////
47 int LeakyBucket::PacketArrivalConformance(int bytes)
49 update();
51 value += bytes;
53 //printf("value=%f\n",value);
55 if(value > max)
57 value -= bytes;
58 return 0; // not conforming
60 else
61 return 1; // conforming
63 }; // LeakyBucket::PacketArrivalConformance(int bytes)
65 /////////////////////////////////////////////////////////
66 // LeakyBucket::Update
67 // Update the LeakyBucket counter to the current time
68 // we decrement the counter by n*decrement_per_slot
69 // decrement_per_slot determines the data rate
70 /////////////////////////////////////////////////////////
71 void LeakyBucket::update(void)
73 int n;
74 n = SimTime - last_update;
75 if(n >= 0)
77 value -= (double) (n * decrement_per_slot);
78 if(value < 0)
79 value = 0;
80 } // decrement only
82 last_update = SimTime;
84 } // LeakyBucket::update(void)
87 /////////////////////////////////////////////////////////////////////////////
88 // Dual Leaky Bucket
89 /////////////////////////////////////////////////////////////////////////////
90 DualLeakyBucket::DualLeakyBucket(root* owner)
92 pOwner = owner;
93 pLbCir = NULL;
94 pLbPir = NULL;
95 CHECK(pLbCir = new LeakyBucket());
96 CHECK(pLbPir = new LeakyBucket());
99 DualLeakyBucket::~DualLeakyBucket(void)
101 if (pLbCir)
102 delete pLbCir;
103 if (pLbPir)
104 delete pLbPir;
107 /////////////////////////////////////////////////////////
108 // DualLeakyBucket::SetParam
109 // Set the parameters for a dual leaky bucket
110 /////////////////////////////////////////////////////////
111 void DualLeakyBucket::SetParam(double cir, int cbs, double pir, int pbs)
113 pLbCir->SetParam(cir, cbs);
114 pLbPir->SetParam(pir, pbs);
117 /////////////////////////////////////////////////////////
118 // DualLeakyBucket::PacketArrivalConformance(bytes)
119 // A packet of with framelength "bytes" has arrived, is
120 // this packet conforming?
121 // The buckets are increased
122 // Return value:
123 // TRUE if packet is conforming and need not be dropped
124 // FALSE if packet is not conforming and must be dropped
125 // *PDropPrec = 0 (green), 1 (yellow) or 2 (red)
126 /////////////////////////////////////////////////////////
127 int DualLeakyBucket::PacketArrivalConformance(int bytes,int *pDropPrec)
130 // issue: we need to add the color-aware mode
131 // issue: in the data processing spec I have defined separate handling
132 // for PIR and CIR, here it is combined
134 int confCir, confPir;
136 confCir = pLbCir->PacketArrivalConformance(bytes);
137 confPir = pLbPir->PacketArrivalConformance(bytes);
139 if(action == 0)
140 return TRUE; //packet always conforming
141 else if(action == 1) // mark - impelement 2-Rate 3color marker (RFC2698)
143 if(!confPir) // PIR bucket not conforming
144 *pDropPrec = 2; // red
145 else if(!confCir) // CIR bucket not conforming
146 *pDropPrec = 1; // yellow
147 else
148 *pDropPrec = 0; // green
150 return TRUE; // packet is conforming and not dropped
152 else if (action == 2) // drop if CIR bucket not conforming
154 if(!confCir) // not conforming to CIR
155 return FALSE;
157 else if (action == 3) // drop if PIR bucket not conforming
159 if(!confPir) // PIR bucket not conforming
160 return FALSE; // drop packet
162 else
164 errm2s2d("%s: (file %s, line %d) Internal error: policing action %d shall be applied but no rule exists",
165 pOwner->name, __FILE__, __LINE__, action);
168 return TRUE;
170 } // DualLeakyBucket::PacketArrivalConformance(int bytes,int *pDropPrec)