File: tcp-int.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,396 kB
  • sloc: cpp: 172,923; tcl: 107,167; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 829; awk: 525; csh: 355
file content (313 lines) | stat: -rw-r--r-- 8,888 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
/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 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 Daedalus Research
 * 	Group at the University of California Berkeley.
 * 4. Neither the name of the University nor of the Research Group 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.
 *
 * Contributed by the Daedalus Research Group, U.C.Berkeley
 * http://daedalus.cs.berkeley.edu
 *
 * $Header: /cvsroot/nsnam/ns-2/tcp/tcp-int.cc,v 1.15 2000/09/01 03:04:07 haoboy Exp $
 */


/*
 * We separate TCP functionality into two parts: that having to do with 
 * providing a reliable, ordered byte-stream service, and that having to do with
 * congestion control and loss recovery. The former is done on a per-connection
 * basis and is implemented as part of IntTcpAgent ("integrated TCP"). The 
 * latter is done in an integrated fashion across multiple TCP connections, and
 * is implemented as part of TcpSessionAgent ("TCP session"). TcpSessionAgent is
 * derived from CorresHost ("correspondent host"), which keeps track of the 
 * state of all TCP (TCP/Int) connections to a host that it is corresponding 
 * with.
 *
 * The motivation for this separation of functionality is to make an ensemble of
 * connection more well-behaved than a set of independent TCP connections.
 * The packet loss rate is cut down and the chances of losses being recovered 
 * via data-driven techniques (rather than via timeouts) is improved. At the 
 * same time, we do not introduce any unnecessary coupling between the 
 * logically-independent byte-streams that the set of connections represents. 
 * This is in contrast to the coupling that is inherent in the multiplexing at 
 * the application layer of multiple byte-streams onto a single TCP connection.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include "packet.h"
#include "ip.h"
#include "tcp.h"
#include "flags.h"
#include "nilist.h"
#include "tcp-int.h"
#include "chost.h"
#include "tcp-session.h"
#include "random.h"

Islist<TcpSessionAgent> TcpSessionAgent::sessionList_;

static class IntTcpClass : public TclClass {
public:
	IntTcpClass() : TclClass("Agent/TCP/Int") {}
	TclObject* create(int, const char*const*) {
		return (new IntTcpAgent());
	}
} class_tcp_int;

IntTcpAgent::IntTcpAgent() : TcpAgent(), slink(), 
	session_(0), closecwTS_(0), lastTS_(-1), count_(0), 
	wt_(1), wndIncSeqno_(0), num_thresh_dupack_segs_(0)
{
	bind("rightEdge_", &rightEdge_);
	bind("uniqTS_", &uniqTS_);
	bind("winInc_", &winInc_);
	bind("winMult_", &winMult_);
}

int
IntTcpAgent::command(int argc, const char*const* argv)
{
	Tcl& tcl = Tcl::instance();

	if (argc == 3) {
		if (!strcmp(argv[1], "setwt")) {
			if (!session_)
				createTcpSession();
			session_->set_weight(this,atoi(argv[2]));
			return (TCL_OK);
		}
	}
	else if (argc == 2) {
		if (!strcmp(argv[1], "session")) {
			if (!session_)
				createTcpSession();
			tcl.resultf("%s", session_->name());
			return (TCL_OK);
		}
	}
	return (TcpAgent::command(argc,argv));
}

/* 
 * Update the ack information and reset the timer. RTT update is done in 
 * TcpSessionAgent.
 */
void
IntTcpAgent::newack(Packet* pkt)
{
	hdr_tcp *tcph = hdr_tcp::access(pkt);
	last_ack_ = tcph->seqno();
	highest_ack_ = last_ack_;

	if (t_seqno_ < last_ack_ + 1)
		t_seqno_ = last_ack_ + 1;
	/* if the connection is done, call finish() */
	if ((highest_ack_ >= curseq_-1) && !closed_) {
		closed_ = 1;
		finish();
	}
}

void 
IntTcpAgent::recv(Packet *pkt, Handler *)
{
	hdr_tcp *tcph = hdr_tcp::access(pkt);
	int amt_data_acked = 0;

	if (tcph->seqno() > last_ack_) {
		amt_data_acked = tcph->seqno() - last_ack_;
		newack(pkt);
	} 
	session_->recv(this, pkt, amt_data_acked);
}

void
IntTcpAgent::createTcpSession()
{
	Tcl& tcl = Tcl::instance();

	tcl.evalf("%s set node_", name());
	tcl.evalf("%s createTcpSession %d", tcl.result(), daddr());
	Islist_iter<TcpSessionAgent> session_iter(TcpSessionAgent::sessionList_);
	TcpSessionAgent *cur;

	while ((cur = session_iter()) != NULL) {
		if (cur->addr()/256 == addr() && cur->daddr() == daddr()) {
			session_ = cur;
			break;
		}
	}
	if (!session_) {
		printf("In IntTcpAgent::createTcpSession(): failed\n");
		abort();
	}
	session_->add_agent(this, size_, winMult_, winInc_, ssthresh_);
}

void
IntTcpAgent::output(int seqno, int reason)
{
	Packet *pkt = allocpkt();
	hdr_tcp *tcph = hdr_tcp::access(pkt);
	tcph->seqno() = seqno;
	tcph->ts() = Scheduler::instance().clock();
	tcph->ts_echo() = ts_peer_;
	tcph->reason() = reason;

	session_->setflags(pkt);
	
	int bytes = hdr_cmn::access(pkt)->size();

	/* call helper function to fill in additional fields */
	output_helper(pkt);

        ++ndatapack_;
	ndatabytes_ += bytes;
	send(pkt, 0);
	if (seqno == curseq_ && seqno > maxseq_)
		idle();  // Tell application I have sent everything so far
	if (seqno > maxseq_) {
		maxseq_ = seqno;
	}
	else {
        	++nrexmitpack_;
        	nrexmitbytes_ += bytes;
	}
	if (wndIncSeqno_ == 0)
		wndIncSeqno_ = maxseq_;
}


/*
 * Unlike in other flavors of TCP, IntTcpAgent does not decide when to send 
 * packets and how many of them to send. That decision is made by 
 * TcpSessionAgent. So send_much() does little more than defer to 
 * TcpSessionAgent.
 */
void
IntTcpAgent::send_much(int force, int reason, int /*maxburst*/)
{
	if (!session_)
		createTcpSession();
	if (!force && delsnd_timer_.status() == TIMER_PENDING)
		return;
	if (overhead_ && !force) {
		delsnd_timer_.resched(Random::uniform(overhead_));
		return;
	}
	session_->send_much(this, force,reason);
}

/*
 * Send one new packet.
 */
void
IntTcpAgent::send_one(int sessionSeqno)
{
	int dst_addr = daddr();
	int dst_port = dport();
	int sport = port();

	if (!session_)
		createTcpSession();
	/* 
	 * XXX We assume that the session layer has already made sure that
	 * we have data to send.
	 */
	output(t_seqno_++); 
	session_->add_pkts(size_, t_seqno_ - 1, sessionSeqno,
			   dst_addr, dst_port, sport, lastTS_, this); 
}


/*
 * open up the congestion window
 */
void 
IntTcpAgent::opencwnd()
{
	session_->opencwnd(size_, this);
}

/*
 * close down the congestion window
 */
void 
IntTcpAgent::closecwnd(int how)
{   
	session_->closecwnd(how, this);
}

Segment *
IntTcpAgent::rxmit_last(int reason, int seqno, int sessionSeqno, double /*ts*/)
{
	session_->agent_rcov(this);
	/* 
	 * XXX kludge -- IntTcpAgent is not supposed to deal with 
	 * rtx timer 
	 */
	session_->reset_rtx_timer(1,0); 
	output(seqno, reason);
	daddr_ = daddr();
	dport_ = dport();
	sport_ = port();
	return (session_->add_pkts(size_, seqno, sessionSeqno, daddr_, 
				   dport_, sport_, lastTS_, this));
	return NULL;
}
unsigned long output_helper_count=0;
double last_clock=0;
void
IntTcpAgent::output_helper(Packet *p)
{
	double now = Scheduler::instance().clock();
	output_helper_count++;
	last_clock = now;
	hdr_tcp *tcph = hdr_tcp::access(p);

	/* This is to make sure that we get unique times for each xmission */
	while (uniqTS_ && now <= lastTS_) {
		now += 0.000001; // something arbitrarily small
	}

	lastTS_ = now;
	tcph->ts() = now;
	/* if this is a fast start pkt and not a retransmission, mark it */
	if (session_->fs_pkt() && tcph->seqno() > maxseq_)
		hdr_flags::access(p)->fs_ = 1;
	return;
}

int 
IntTcpAgent::data_left_to_send() {
	return (curseq_ > t_seqno_);
}