File: vq.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 (362 lines) | stat: -rw-r--r-- 11,805 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
/* -*-  Mode:C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1994 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.
 */

/* Marking scheme proposed by Kunniyur and Srikant in "Decentralized Adaptive
*    ECN Algorithms" published in the Proceedings of Infocom2000, Tel-Aviv, 
*    Israel, March 2000.
* 
* Basic Idea of the AVQ (Adaptive Virtual Queue) Algorithm
* --------------------------------------------------------
* 
* The scheme involves having a virtual queue at each link with the same
* arrival rate as the original queue, but with the service rate scaled
* down by some factor (ecnlim_). The buffer capacity remains the same,
* but if needed we can also scale the buffer capacity by come constant
* (buflim_). When a packet is dropped from the virtual queue, mark a
* packet at the front of the real queue. Although in this implemetation,
* a "drop-tail" function is used in the VIRTUAL QUEUE to mark packets in
* the original queue, any algorithm (like RED) can be used in the
* virtual queue to signal congestion. The capacity of the virtual queue
* is adapted periodically to guarantee loss-free service. 
* 
* The update of the virtual capacity (capacity of the virtual queue) is
* done using a token-bucket. For implentation details, please refer to
* the Sigcomm 2001 paper titled "Analysis and Design of an Adaptive
* Virtual Queue (AVQ) algorithm for Active Queue Management (AQM). 
* 
* The virtual queue length can be taken in packets or in bytes. If the
* packet sizes are not fixed, then it is recommended to use bytes
* instead of packets as units of length. If the variable (qib_) is set,
* then the virtual queue is measured in bytes, else it is measured in
* packets and the mean packet size is set to 1000. 
* 
*                          -Srisankar
* 
* ********************************************************************  
* Options in the AVQ algorithm code:
* ----------------------------------
* 
* * ecnlim_ -> This parameter specifies the capacity of the virtual 
*              queue as a fraction of the capcity of the original queue.
* 
* * buflim_ -> This parameter specifies the buffer capacity of the
*              virtual queue as a fraction of the buffer capacity of the
*              original queue.
* 
* * queue_in_bytes_ -> Indicates whether you want to measure the queue
*                      in bytes or packets. 
* 
* * markpkts_ -> Set to 1 if router wants to emply marking for that
*                link. Set to 0, if dropping is preferred. 
* 
* * markfront_ -> Set to 1 if mark from front policy is employed. Usually
*                 set to zero.   
* 
* * mean_pktsize- -> The mean packet size is set to 1000.
* 
* * gamma -> This parameter specifies the desired arrival rate at the link.
* 
* ********************************************************************  
*/

#ifndef lint
static const char rcsid[] =
    "@(#) $Header: /cvsroot/nsnam/ns-2/queue/vq.cc,v 1.7 2010/03/08 05:54:53 tom_henderson Exp $ (LBL)";
#endif
#include "flags.h"
#include "delay.h"
#include "vq.h"
#include "math.h"

static class VqClass : public TclClass {
public:
	VqClass() : TclClass("Queue/Vq") {}
	TclObject* create(int argc, const char*const* argv) {
		if (argc==5) 
			return (new Vq(argv[4]));
		else
			return (new Vq("Drop"));
	}
} class_vq;

Vq::Vq(const char * ) : link_(NULL), EDTrace(NULL), tchan_(0){
	q_ = new PacketQueue;
	pq_ = q_;
	bind_bool("drop_front_", &drop_front_);
	bind("ecnlim_", &ecnlim_); //  = ctilde/c ; Initial value set to 0.8
	bind("buflim_", &buflim_); /* Fraction of the original buffer that the VQ buffer has. Default is 1.0 */
	bind_bool("queue_in_bytes_", &qib_);	 // boolean: q in bytes?
	bind("gamma_", &gamma_); // Defines the utilization. Default is 0.98
	bind_bool("markpkts_", &markpkts_); /* Whether to mark or drop?  Default is drop */
	bind_bool("markfront_", &markfront_); /* Mark from front?  Deafult is false */
	bind("mean_pktsize_", &mean_pktsize_);  // avg pkt size
	bind("curq_", &curq_);          // current queue size
	
	vq_len = 0.0;
	prev_time = 0.0;
	vqprev_time = 0.0;
	alpha2 = 0.15; // Determines how fast we adapt at the link
	Pktdrp = 0; /* Useful if we are dropping pkts instead of marking */
	firstpkt = 1;
	qlength = 0; /* Tracks the queue length */
}

int Vq::command(int argc, const char*const* argv) {
	Tcl& tcl = Tcl::instance();
  if (argc == 3) {
	  if (strcmp(argv[1], "link") == 0) {
		  LinkDelay* del = (LinkDelay*)TclObject::lookup(argv[2]);
		  if (del == 0) {
			  return(TCL_ERROR);
		  }
		  // set capacity now
			link_ = del;
		  c_ = del->bandwidth();
		  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);
		  }
	  }
		// 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("Vq: trace: can't attach %s for writing", id);
				return (TCL_ERROR);
			}
			return (TCL_OK);
		}
  }
  return Queue::command(argc, argv);
}

void Vq::enque(Packet* p)
{
	q_->enque(p);
	hdr_cmn* ch = hdr_cmn::access(p);
	qlength = qlength + (qib_ * ch->size()) + (1 - qib_);
	
	if(firstpkt){
		/* Changing c_ so that it is measured in packets per second */
		/* Assuming packets are fixed size with 1000 bytes */
		
		if(qib_) c_ = c_ / (8.0);
		else c_ = c_ / (8.0 * mean_pktsize_);
		firstpkt = 0;
		ctilde = c_ * ecnlim_;
		prev_time = Scheduler::instance().clock();
		vqprev_time = Scheduler::instance().clock();
	}

	/* Update the Virtual Queue length */ 	
	curr_time = Scheduler::instance().clock();
	vq_len = vq_len - (ctilde * (curr_time - vqprev_time));
	vqprev_time = curr_time;
	if(vq_len < 0.0) vq_len = 0.0;
	vq_len = vq_len + qib_ * ch->size() + (1 - qib_);
	
	/* checkPacketForECN() returns 1 if we need to mark or drop a packet*/
	
	if(checkPacketForECN()){
		if(markpkts_)
			markPacketForECN(p);
		else
			dropPacketForECN(p);
	}
	
	/* Adaptation of the virtual capacity, tilde(C) */
	/* Use the token bucket system  */
	/* Scale alpha appropriately if qib_ is set */
	
	if(Pktdrp == 0){ // Do the adaptation
		/* Pktdrp = 0 always when marking */
		ctilde = ctilde + alpha2*gamma_*c_*(curr_time - prev_time) - alpha2*(1.0 - qib_) - alpha2*qib_*ch->size();
		if(ctilde > c_) ctilde = c_;
		if(ctilde <0.0) ctilde = 0.0;
		prev_time = curr_time;
	}
	else{ // No adaptation and reset Pktdrp
		Pktdrp = 0;
	}
	
	if (qlength > qlim_*( 1 - qib_ + qib_*mean_pktsize_)) {
		if (drop_front_) { /* remove from head of queue */
			if(q_->length() > 0){
				Packet *pp = q_->head();
				qlength = qlength - qib_ * ch->size() - (1 - qib_);
				q_->remove(pp); 
				drop(pp);
			}
		} 
		else {
			q_->remove(p);
			qlength = qlength - qib_ * ch->size() - (1 - qib_);
			drop(p);
		}
	}
	curq_ = qlength;
}

/* This is a simple DropTail on the VQ. However, one Can add 
   any AQM scheme on VQ here */
int Vq::checkPacketForECN(){
	if(vq_len > (buflim_ * qlim_ * ( 1 - qib_ + qib_*mean_pktsize_))){
		return 1;
	}
	else{
		return 0;
	}
}

/* Implements a simple mark-tail/mark-front here. If needed other
   mechanism (like mark-random ) can also be implemented */ 
void  Vq::markPacketForECN(Packet* pkt)
{
	/* Update the VQ length */
	hdr_cmn* ch = hdr_cmn::access(pkt);
	vq_len = vq_len - qib_ * ch->size() - (1.0 - qib_);
	if(vq_len < 0.0) vq_len = 0.0;
	
	if(markfront_){ 
		Packet *pp = q_->head();
		hdr_flags* hf = hdr_flags::access(pp);
		if(hf->ect() == 1)  // ECN capable flow
			hf->ce() = 1; // Mark the TCP Flow;
	}
	else{ 
		/* Mark the current packet and forget about it */
		hdr_flags* hdr = hdr_flags::access(pkt);
		if(hdr->ect() == 1)  // ECN capable flow
			hdr->ce() = 1; // For TCP Flows
	}
}	

/* Implements a simple drop-tail/drop-front here. If needed other
   mechanism (like drop-random ) can also be implemented */ 
void Vq::dropPacketForECN(Packet* pkt) 
{
	/* Update the VQ length */
	hdr_cmn* ch = hdr_cmn::access(pkt);
	vq_len = vq_len - qib_ * ch->size() - (1.0 - qib_);
	if(vq_len < 0.0) vq_len = 0.0;

	if(drop_front_){
		/* If drop from front is enabled, then deque a 
		   packet from the front of the queue and drop it ...
		   Usually not recommended */ 
		if(q_->length() > 0 ){
			Packet *pp = q_->head();
			qlength = qlength - qib_ * ch->size() - (1 - qib_);
			q_->remove(pp); /* The queue length is taken care of in
										 in the deque program */
			drop(pp);

		}
	}
	else{
		q_->remove(pkt);
		qlength = qlength - qib_ * ch->size() - (1 - qib_);
		drop(pkt);
	}
	
	/* If one is dropping packets, one needs to be careful about 
	   measuring the total arrival rate to calculate the virtual
	   capacity, tilde(C). In this case, the arrival rate that is
	   taken into the adaptation algorithm is the accepted rate and
	   not the offered rate. */
	Pktdrp = 1; // Do not update tilde(C)
	
}	

Packet* Vq::deque()
{
	if((q_->length() > 0)){
		Packet *ppp = q_->deque();
		hdr_cmn* ch = hdr_cmn::access(ppp);
		qlength = qlength - qib_ * ch->size() - (1 - qib_);
		curq_ = qlength;
		return ppp;
	}
	else return q_->deque();
}

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

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

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

	if (tchan_) {
		int n;
		double t = Scheduler::instance().clock();
		// XXX: be compatible with nsv1 RED trace entries
		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; 
}