File: rate-limit-strategy.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,780 kB
  • ctags: 27,490
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (271 lines) | stat: -rw-r--r-- 7,595 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 2000  International Computer Science Institute
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by ACIRI, the AT&T 
 *      Center for Internet Research at ICSI (the International Computer
 *      Science Institute).
 * 4. Neither the name of ACIRI nor of ICSI may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ICSI AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ICSI OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include "rate-limit-strategy.h"

RateLimitStrategy::RateLimitStrategy(double rate, int ptype, double share, double estimate) {
  
  target_rate_ = rate;
  reset_time_ = Scheduler::instance().clock();
  
  ptype_ = ptype;
  ptype_share_ = share;
  
//  if (debug_)
//    printf("TB: Starting a token bucket at %g with rate %g bps\n", Scheduler::instance().clock(), rate);
  rateEstimator_ = new RateEstimator(estimate);
  rateLimiter_ = new TokenBucketRateLimiter();
  
  ptypeRateEstimator_ = new RateEstimator();
  ptypeRateLimiter_ = new TokenBucketRateLimiter();
  
  ptypeLog_ = new PacketTypeLog(this);
}

double
RateLimitStrategy::process(Packet *p, int mine, int lowDemand) {
  
  rateEstimator_->estimateRate(p);
  ptypeLog_->log(p);
  
  hdr_cmn* hdr = HDR_CMN(p);
    
  int dropped = 0;
  if (hdr->ptype() == (u_int)ptype_) {
    ptypeRateEstimator_->estimateRate(p);
    dropped = ptypeRateLimiter_->rateLimit(p, ptypeRateEstimator_->estRate_, 
					   target_rate_*ptype_share_, mine, lowDemand);
  }

  if (dropped) return 1;
  
  dropped = rateLimiter_->rateLimit(p, rateEstimator_->estRate_, target_rate_, mine, lowDemand);
  return dropped;
}

void
RateLimitStrategy::restrictPacketType(int type, double share, double actual) {
  
  if (type == ptype_) {
    return;
  }
  
  printf("RLSt: Restricting type %d to %g hogging=%g at %g \n",  type, share, actual, 
	 Scheduler::instance().clock());
  ptype_ = type;
  ptype_share_ = share;
  ptypeRateEstimator_->estRate_ = rateEstimator_->estRate_*actual;
}

double 
RateLimitStrategy::getDropRate() {
  double inRate = rateEstimator_->estRate_;
  double dropRate = 0;
  if (inRate > 0) {
	dropRate = (inRate - target_rate_)/inRate;
  }  
  if (dropRate < 0) dropRate=0;
  return dropRate;
}

double 
RateLimitStrategy::getArrivalRate() {
  return rateEstimator_->estRate_;
}

void
RateLimitStrategy::reset() {
  rateLimiter_->reset();
  ptypeRateLimiter_->reset();
  rateEstimator_->reset(); 
  ptypeRateEstimator_->reset();
}

RateLimitStrategy::~RateLimitStrategy() {
  delete(rateLimiter_);
  delete(ptypeRateLimiter_);
  delete(rateEstimator_);
  delete(ptypeRateEstimator_);
  delete(ptypeLog_);
}

//########################### PacketTypeLog Methods #####################

PacketTypeLog::PacketTypeLog(RateLimitStrategy * rlst) {
  count_=0;
  for (int i=0; i<MAX_PACKET_TYPES; i++) {
    typeCount_[i]=0;
  }
  rlStrategy_ = rlst;
  
  resched(PACKET_TYPE_TIMER);
}

void
PacketTypeLog::log(Packet *p) {
  hdr_cmn* hdr = HDR_CMN(p);
  int type = hdr->ptype();
  
  int index = mapTypeToIndex(type);
  //count packets instead of bytes
  typeCount_[index]++;
  count_++;
}

void
PacketTypeLog::expire(Event * ) {

  //printf("PTTimer Expiry at %g\n", Scheduler::instance().clock());

  if (!count_) {
    resched(PACKET_TYPE_TIMER);
    return;
  }
    
  for (int i=0; i<MAX_PACKET_TYPES; i++) {
    if (typeCount_[i]!=0) {
      double actualShare = ((double)typeCount_[i])/count_;
      int type = mapIndexToType(i);
      double maxShare = mapTypeToShare(type);
      if (actualShare>maxShare) {
	//right now only one type can be restricted.
	rlStrategy_->restrictPacketType(type, maxShare, actualShare);
	resched(PACKET_TYPE_TIMER);
	return;
      }
    }
  }
  
  //stop restricting if you were.
  rlStrategy_->restrictPacketType(-1, 1, 1);
  resched(PACKET_TYPE_TIMER);
  return;
}
  
int 
PacketTypeLog::mapTypeToIndex(int type) {
  
  switch (type) {
  case PT_PING: return 0;
  case PT_UDP: return 1;
  case PT_CBR: return 2;
  default: return MAX_PACKET_TYPES-1;
  }
}

int 
PacketTypeLog::mapIndexToType(int index) {
  switch (index) {
  case 0: return PT_PING;
  case 1: return PT_UDP;
  case 2: return PT_CBR;
  case MAX_PACKET_TYPES-1: return -1;
  default: printf("PTLog: invalid index\n"); exit(-1);
  }
}

//returning 1.0 means don't limit traffic of that type.
double
PacketTypeLog::mapTypeToShare(int type) {
  switch (type) {
  case PT_PING: return 1.0;
  case PT_UDP: return 1.0;
  case PT_CBR: return 1.0;
  default: return 1;
  }
}

PacketTypeLog::~PacketTypeLog() {
  cancel();
}

//########################### TokenBucketRateLimiter Methods ###################

TokenBucketRateLimiter::TokenBucketRateLimiter() {
  
  bucket_depth_ = DEFAULT_BUCKET_DEPTH;
  tbucket_ = bucket_depth_;
  total_passed_ = 0.0;
  total_dropped_ = 0.0;
  time_last_token_ = Scheduler::instance().clock();
}

int 
TokenBucketRateLimiter::rateLimit(Packet * p, double , double targetRate, int mine, int lowDemand) {

  hdr_cmn* hdr = HDR_CMN(p);
  double now = Scheduler::instance().clock();
  double time_elapsed = now - time_last_token_;
  
  //printf("TB: now = %g last_sent = %g elapsed = %g\n", now, time_last_token_, time_elapsed);
  tbucket_ += (time_elapsed * targetRate)/8.0;
  time_last_token_ = now;

  if (tbucket_ > bucket_depth_)
    tbucket_ = bucket_depth_;      /* never overflow */

  //printf("TB: tbucket_ = %g pktSize = %g\n", tbucket_, (double)hdr->size_);
  //printf("TB: in = %g out = %g\n", total_passed_, total_dropped_);

  if ((double)hdr->size_ < tbucket_ || (mine && lowDemand) ) {
    tbucket_ -= hdr->size_;
    total_passed_ += (double) hdr->size_;
    //printf("Passed Packet in Rate Limiter\n");
    return 0;
  } 
  else {
    total_dropped_ += (double) hdr->size_;
    //    printf("Dropped Packet in Rate Limiter\n");
    return 1;
  }
}

// double 
// TokenBucketRateLimiter::getDropRate() { 
//   if (getArrivals() == 0) 
//     return 0; 
//   else 
//     return total_dropped_/(total_passed_+total_dropped_);
// }
  
void
TokenBucketRateLimiter::reset() {
  total_dropped_ = 0;
  total_passed_ = 0;
  tbucket_ = 0;
  time_last_token_ = Scheduler::instance().clock();
}