File: rem.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,796 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (370 lines) | stat: -rw-r--r-- 8,919 bytes parent folder | download | duplicates (8)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1990-1997 Regents of the University of California.
 * 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 the Computer Systems
 *	Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS 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.
 *
 *   
 * ns-2 code written by: Sanjeewa Athuraliya (sanjeewa@caltech.edu)
 *                       Caltech, Pasadena,
 *			 CA 91125
 *                       Victor Li
 *                       University of Melbourne, Parkville
 *                       Vic., Australia.
 *
 * Ref: Active Queue Management (REM), IEEE Network, May/June 2001.
 *      http://netlab.caltech.edu
 */

#include <math.h>
#include <sys/types.h>
#include "config.h"
#include "template.h"
#include "random.h"
#include "flags.h"
#include "delay.h"
#include "rem.h"
#include <iostream>

static class REMClass : public TclClass {
public:
	REMClass() : TclClass("Queue/REM") {}
	TclObject* create(int, const char*const*) {
		return (new REMQueue);
	}
} class_rem;


void REMQueue :: set_update_timer()
{
	rem_timer_.resched(remp_.p_updtime);
}

void REMQueue :: timeout()
{
	//do price update
	run_updaterule();
	set_update_timer();
}


void REMTimer :: expire (Event *) {
	 a_->timeout();
}



REMQueue::REMQueue() : link_(NULL), tchan_(0), rem_timer_(this) 
{
	bind("gamma_", &remp_.p_gamma);
	bind("phi_", &remp_.p_phi);
	bind("inw_", &remp_.p_inw);
	bind("mean_pktsize_", &remp_.p_pktsize); 
	bind("pupdtime_", &remp_.p_updtime); 
	bind("pbo_", &remp_.p_bo); 
	bind("prob_", &remv_.v_prob);		    // dropping probability
	bind("curq_", &curq_);			    // current queue size
	bind("pmark_", &pmark_);      	//number of packets being marked 
	bind_bool("markpkts_", &markpkts_); /* Whether to mark or drop?  Default is drop */
	bind_bool("qib_", &qib_); /* queue in bytes? */ 

	q_ = new PacketQueue();			    // underlying queue
	pq_ = q_;
	reset();

#ifdef notdef
print_remp();
print_remv();
#endif

}

void REMQueue::reset()
{
	/*
	 * Compute the "packet time constant" if we know the
	 * link bandwidth.  The ptc is the max number of 
	 * pkts per second which can be placed on the link.
	 * The link bw is given in bits/sec, so scale psize
	 * accordingly.
	 */
	 
	if (link_)
		remp_.p_ptc = link_->bandwidth() / (8.0 * remp_.p_pktsize);

	remv_.v_pl = 0.0;
	remv_.v_prob = 0.0;
	remv_.v_in = 0.0;
 	remv_.v_ave = 0.0;
	remv_.v_count = 0.0;

	remv_.v_pl1 = 0.0;
	remv_.v_pl2 = 0.0;
	remv_.v_in1 = 0.0;
	remv_.v_in2 = 0.0;
	pmark_ = 0.0;
	bcount_ = 0; 
	
	//Queue::reset();
	set_update_timer();
}

/*
 * Compute the average input rate, the price and the marking prob..
 */
void REMQueue::run_updaterule()
{

	double in, in_avg, nqueued, pl, pr;

        // link price, the congestion measure

	pl = remv_.v_pl;

	// in is the number of bytes (if qib_ is true) or packets (otherwise)
	// arriving at the link (input rate) during one update time interval

	in = remv_.v_count;
       
	// in_avg is the low pss filtered input rate
	// which is in bytes if qib_ is true and in packets otherwise.

	in_avg = remv_.v_ave;
  
	in_avg *= (1.0 - remp_.p_inw);
	
	if (qib_) {
		in_avg += remp_.p_inw*in/remp_.p_pktsize; 
		nqueued = bcount_/remp_.p_pktsize; 
        }
	else {
		in_avg += remp_.p_inw*in;
		nqueued = q_ -> length();
	}


	// c measures the maximum number of packets that 
	// could be sent during one update interval.

	double c  = remp_.p_updtime*remp_.p_ptc;

	pl = pl + remp_.p_gamma*( in_avg + 0.1*(nqueued-remp_.p_bo) - c );

	if ( pl < 0.0) {
	    pl = 0.0;
	}

	double pow1 = pow (remp_.p_phi, -pl);
	pr = 1.0-pow1;


	remv_.v_count = 0.0;
	remv_.v_ave = in_avg;
	remv_.v_pl = pl;
	remv_.v_prob = pr;
}


/*
 * Return the next packet in the queue for transmission.
 */
Packet* REMQueue::deque() 
{
	Packet *p = q_->deque();
	if (p != 0) {
		bcount_ -= hdr_cmn::access(p)->size ();
	}
	if (markpkts_) {
		double u = Random::uniform();
		if (p!=0) {
			double pro = remv_.v_prob;
			if (qib_) {
				int size = hdr_cmn::access(p)->size ();
				pro = remv_.v_prob*size/remp_.p_pktsize; 
			}
   		if ( u <= pro ) {
				hdr_flags* hf = hdr_flags::access(p);
				if(hf->ect() == 1) { 
					hf->ce() = 1; 
					pmark_++;
				}
			}
		}
	}
	double qlen = qib_ ? bcount_ : q_->length();
	curq_ = (int) qlen;
	return (p);
}

/*
 * Receive a new packet arriving at the queue.
 * The packet is dropped if the maximum queue size is exceeded.
 */

void REMQueue::enque(Packet* pkt)
{
	hdr_cmn* ch = hdr_cmn::access(pkt);
	double qlen; 

	if (qib_) {
		remv_.v_count += ch->size();
	}
	else {
		++remv_.v_count;
	}

	double qlim = qib_ ? (qlim_*remp_.p_pktsize) : qlim_ ;

	q_ -> enque(pkt);
	bcount_ += ch->size();

	qlen = qib_ ? bcount_ : q_->length();

	if (qlen >= qlim) {
		q_->remove(pkt);
		bcount_ -= ch->size();
		drop(pkt);
	}
	else  {
		if (!markpkts_) {
			double u = Random::uniform(); 
			double pro = remv_.v_prob;
			if (qib_) {
				pro = remv_.v_prob*ch->size()/remp_.p_pktsize; 
			}
			if ( u <= pro ) {
				q_->remove(pkt);
				bcount_ -= ch->size();
				drop(pkt);
			}		    
		}
	}
	qlen = qib_ ? bcount_ : q_->length();
	curq_ = (int) qlen;
}


int REMQueue::command(int argc, const char*const* argv)
{

	Tcl& tcl = Tcl::instance();
	if (argc == 2) {
		if (strcmp(argv[1], "reset") == 0) {
			reset();
			return (TCL_OK);
		}
	} else if (argc == 3) {
		// attach a file for variable tracing
		if (strcmp(argv[1], "attach") == 0) {
			int mode;
			const char* id = argv[2];
			tchan_ = Tcl_GetChannel(tcl.interp(), (char*)id, &mode);
			if (tchan_ == 0) {
				tcl.resultf("REM: trace: can't attach %s for writing", id);
				return (TCL_ERROR);
			}
			return (TCL_OK);
		}
		// tell REM about link stats
		if (strcmp(argv[1], "link") == 0) {
			LinkDelay* del = (LinkDelay*)TclObject::lookup(argv[2]);
			if (del == 0) {
				tcl.resultf("REM: no LinkDelay object %s",
					argv[2]);
				return(TCL_ERROR);
			}
			// set ptc now
			link_ = del;
			remp_.p_ptc = link_->bandwidth() /
				(8. * remp_.p_pktsize);

			return (TCL_OK);
		}
		if (!strcmp(argv[1], "packetqueue-attach")) {
			delete q_;
			if (!(q_ = (PacketQueue*) TclObject::lookup(argv[2])))
				return (TCL_ERROR);
			else {
				pq_ = q_;
				return (TCL_OK);
			}
		}
	}
	return (Queue::command(argc, argv));
}

/*
 * Routine called by TracedVar facility when variables change values.
 * Currently used to trace values of avg queue size, drop probability,
 * and the instantaneous queue size seen by arriving packets.
 * Note that the tracing of each var must be enabled in tcl to work.
 */

void
REMQueue::trace(TracedVar* v)
{
	char wrk[500];
	const char *p;

	if (((p = strstr(v->name(), "ave")) == NULL) &&
	    ((p = strstr(v->name(), "prob")) == NULL) &&
	    ((p = strstr(v->name(), "curq")) == NULL)) {
		fprintf(stderr, "REM:unknown trace var %s\n",
			v->name());
		return;
	}

	if (tchan_) {
		int n;
		double t = Scheduler::instance().clock();
		if (*p == 'c') {
			sprintf(wrk, "Q %g %d", t, int(*((TracedInt*) v)));
		} else {
			sprintf(wrk, "%c %g %g", *p, t,
				double(*((TracedDouble*) v)));
		}
		n = strlen(wrk);
		wrk[n] = '\n'; 
		wrk[n+1] = 0;
		(void)Tcl_Write(tchan_, wrk, n+1);
	}
	return; 
}

/* for debugging help */
void REMQueue::print_remp()
{
	printf("=========\n");
}

void REMQueue::print_remv()
{
	printf("=========\n");
}