File: qsagent.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 (262 lines) | stat: -rw-r--r-- 7,448 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

/*
 * qsagent.cc
 * Copyright (C) 2001 by the University of Southern California
 * $Id: qsagent.cc,v 1.9 2010/03/08 05:54:53 tom_henderson Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 * The copyright of this module includes the following
 * linking-with-specific-other-licenses addition:
 *
 * In addition, as a special exception, the copyright holders of
 * this module give you permission to combine (via static or
 * dynamic linking) this module with free software programs or
 * libraries that are released under the GNU LGPL and with code
 * included in the standard release of ns-2 under the Apache 2.0
 * license or under otherwise-compatible licenses with advertising
 * requirements (or modified versions of such code, with unchanged
 * license).  You may copy and distribute such a system following the
 * terms of the GNU GPL for this module and the licenses of the
 * other code concerned, provided that you include the source code of
 * that other code when and as the GNU GPL requires distribution of
 * source code.
 *
 * Note that people who make modified versions of this module
 * are not obligated to grant this special exception for their
 * modified versions; it is their choice whether to do so.  The GNU
 * General Public License gives permission to release a modified
 * version without this exception; this exception also makes it
 * possible to release a modified version which carries forward this
 * exception.
 *
 */

/*
 * Quick Start for TCP and IP.
 * A scheme for transport protocols to dynamically determine initial 
 * congestion window size.
 *
 * http://www.ietf.org/internet-drafts/draft-amit-quick-start-02.ps
 *
 * This implements the Quick Start Agent at each of network element "Agent/QSAgent"
 * qsagent.cc
 *
 * Srikanth Sundarrajan, 2002
 * sundarra@usc.edu
 */

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <signal.h>
#include <float.h>

#include "object.h"
#include "agent.h"
#include "packet.h"
#include "ip.h"
#include "classifier.h"
#include "connector.h"
#include "delay.h"
#include "queue.h"
#include "scheduler.h"
#include "random.h"

#include "hdr_qs.h"
#include "qsagent.h"
#include <fstream>

static class QSAgentClass : public TclClass {
public:
	QSAgentClass() : TclClass("Agent/QSAgent") {}
	TclObject* create(int, const char*const*) {
		return (new QSAgent);
	}
} class_QSAgent;

int QSAgent::rate_function_ = 1;

QSAgent::QSAgent():Agent(PT_TCP), old_classifier_(NULL), qs_enabled_(1), 
    qs_timer_(this) 
{

	prev_int_aggr_ = 0;
	aggr_approval_ = 0;

	bind("qs_enabled_", &qs_enabled_);
	bind("old_classifier_", &old_classifier_);
	bind("state_delay_", &state_delay_);
	bind("alloc_rate_", &alloc_rate_);
	bind("threshold_", &threshold_);
	bind("max_rate_", &max_rate_);
	bind("mss_", &mss_);
	bind("rate_function_", &rate_function_);
	bind("algorithm_", &algorithm_);

	qs_timer_.resched(state_delay_);
  
}

QSAgent::~QSAgent()
{
}

int QSAgent::command(int argc, const char*const* argv)
{
	return (Agent::command(argc,argv));
}

void QSAgent::recv(Packet* packet, Handler*)
{
	double app_rate;
	//double avail_bw, util;
	Classifier * pkt_target;
	Tcl& tcl = Tcl::instance();
	char qname[64], lname[64];

	hdr_qs *qsh =  hdr_qs::access(packet);
	hdr_ip *iph = hdr_ip::access(packet);

	assert (old_classifier_ != 0);

	pkt_target = (Classifier *)TclObject::lookup(old_classifier_->name());

	if (qs_enabled_) {
		if (qsh->flag() == QS_REQUEST && qsh->rate() > 0 && iph->daddr() != addr()) {
			sprintf (qname, "[Simulator instance] get-queue %d %d", addr(), iph->daddr()); 
			tcl.evalc (qname);
			Queue * queue = (Queue *) TclObject::lookup(tcl.result());

			sprintf (lname, "[Simulator instance] get-link %d %d", addr(), iph->daddr()); 
			tcl.evalc (lname);
			LinkDelay * link = (LinkDelay *) TclObject::lookup(tcl.result());

			if (link != NULL && queue != NULL) {
				app_rate = process(link, queue, hdr_qs::rate_to_Bps(qsh->rate()));
				if (app_rate > 0) {    
					qsh->ttl() -= 1;
					qsh->rate() = hdr_qs::Bps_to_rate(app_rate); //update rate
				}
				else {
					qsh->rate() = 0; //disable quick start, not enough bandwidth
					qsh->flag() = QS_DISABLE;
				}
			}
		}
	}
	
	pkt_target->recv(packet, 0);

	return;
  
}


double QSAgent::process(LinkDelay *link, Queue *queue, double ratereq)
{
	double util, avail_bw, app_rate, util_bw;

	// PS: avail_bw is in units of bytes per sec.
	if (algorithm_ == 1) {
		/*
		 */
		util = queue->utilization();
		avail_bw = link->bandwidth() / 8 * (1 - util);
		avail_bw -= (prev_int_aggr_ + aggr_approval_);
		avail_bw *= alloc_rate_;
		app_rate = (avail_bw < ratereq) ? (int) avail_bw : ratereq;
		app_rate = (app_rate < (max_rate_ * 1024)) ?
			app_rate : (max_rate_ * 1024);
		if (app_rate > 0) {
			// add approved to current bucket
			aggr_approval_ += app_rate;
		}
	} else if (algorithm_ == 2) {
		/*
		 * Algorithm 2 checks if the utilized bandwidth is
		 *  less than some fraction (threshold_) of
		 *  the total bandwidth.  If so, the approved rate
		 *  is at most some fraction (alloc_rate_) of the
		 *  link bandwidth.
		 */
		util = queue->utilization();
		util_bw = link->bandwidth() / 8 * util;
		util_bw += (prev_int_aggr_ + aggr_approval_);
		if (util_bw < threshold_ * link->bandwidth() / 8) {
			app_rate = alloc_rate_ * link->bandwidth() / 8;
			if (ratereq < app_rate)
				app_rate = ratereq;
		} else {
			app_rate = 0;
		}
		aggr_approval_ += app_rate;
	} else if (algorithm_ == 3) {
		/*
		 * Algorithm 3 checks if the utilized bandwidth is
		 *  less than some fraction (threshold_) of
		 *  the total bandwidth.  If so, the approved rate
		 *  is at most the allowed allocated bandwidth minus
		 *  the utilized bandwidth.
		 *
		 * Algorithm 3 used queue->peak_utilization() instead of
		 *   queue->utilization().  This looks at the peak
		 *   utilization measures over a sub-interval of a
		 *   larger interval.
		 */
		util = queue->peak_utilization();
		util_bw = link->bandwidth() / 8 * util;
		util_bw += (prev_int_aggr_ + aggr_approval_);
		if (util_bw < threshold_ * link->bandwidth() / 8) {
			app_rate = alloc_rate_ * link->bandwidth() / 8
				     - util_bw;
			if (ratereq < app_rate)
				app_rate = ratereq;
			if (app_rate <  0) 
				app_rate = 0;
		} else {
			app_rate = 0;
		}
		aggr_approval_ += app_rate;
	} else if (algorithm_ == 4) {
		// a broken router: yes to all QS requests
		app_rate = ratereq;
	} else {
		app_rate = 0;
	}

#ifdef QS_DEBUG
		printf("%d: requested = %f KBps, available = %f KBps, approved = %f KBps\n", addr(), ratereq/1024, free_bw/1024, app_rate/1024);
#endif

	return app_rate;
}


void QSTimer::expire(Event *) {
	
	qs_handle_->prev_int_aggr_ = qs_handle_->aggr_approval_;
	qs_handle_->aggr_approval_ = 0;

	this->resched(qs_handle_->state_delay_);

}


/*
Local Variables:
c-basic-offset: 8
End:
*/