File: scoreboard1.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,864 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 818; awk: 525; csh: 355
file content (471 lines) | stat: -rw-r--r-- 15,337 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1996 The 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 Network Research
 * 	Group at Lawrence Berkeley National 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.
 */

/* 
 * TCP-Linux module for NS2 
 *
 * May 2006
 *
 * Author: Xiaoliang (David) Wei  (DavidWei@acm.org)
 *
 * NetLab, the California Institute of Technology 
 * http://netlab.caltech.edu
 *
 * Module: scoreboard1.cc
 *      This is the Scoreboard implementation for TCP-Linux in NS-2.
 *      We loosely follow the Linux implementation (clearn_rtx_queue and sacktag_write_queue in tcp_input.c) in Scoreboard Update.
 *
 */

#include <stdlib.h>
#include <stdio.h>

#include "scoreboard1.h"
#include "tcp.h"
#include "linux/ns-linux-util.h"

#define ASSERT(x) if (!(x)) {printf ("Assert SB failed\n"); exit(1);}
#define ASSERT1(x) if (!(x)) {printf ("Assert1 SB (length)\n"); exit(1);}
#define ASSERT2(x,y...) if (!(x)) {printf(y); exit(1); }

bool ScoreBoard1::CleanRtxQueue(int last_ack, unsigned char* flag) 
{
	int cmp;
	bool changed_acked_rtx = false;
	//clean the acked packets
	while ((head_) && ((cmp=head_->ShouldClean(last_ack))!=0)) {
		int pkt_num = (cmp<0) ? head_->GetLength(): cmp;
		switch (head_->GetFlag()) {
		case SKB_FLAG_SACKED: 
			sack_out_ -= pkt_num;
			ASSERT2((sack_out_>=0), "SACK_OUT %d get negative when advancing edge, last_ack %d\n", sack_out_, last_ack);
			break;
		case SKB_FLAG_LOST:
			fack_out_ -= pkt_num;
			ASSERT2((fack_out_>=0), "fack_out %d get negative when advancing edge, last_ack %d\n", fack_out_, last_ack);
			break;
		}
		//printf("%d: %d %d\n", last_ack, cmp, head_->GetFlag());

		if (cmp<0) {
			ScoreBoardNode1* tmp=head_;
			head_=head_->GetNext();
			if (tmp->GetFlag()==SKB_FLAG_RETRANSMITTED) {
				if (tmp->GetRtx()>acked_rtx_id_) {
					changed_acked_rtx = true;
					acked_rtx_id_ = tmp->GetRtx();
				}
				(*flag) |= FLAG_UNSURE_TSTAMP;
			}
			if (tmp == nxt_to_retrx_) nxt_to_retrx_ = head_;
			delete tmp;
		} else {
			head_->SetBegin(last_ack+1);
			break;
		}
	}
	return changed_acked_rtx;
}
// last_ack = TCP last ack
int ScoreBoard1::UpdateScoreBoard (int last_ack, hdr_tcp* tcph, int dupack_threshold)
{
	// This one pass process is not completely accurate:
	//
	// There is some case such that the acked_rtx_id_ is increased in the later part of the process,
	// and this change may mark some earlier packet in the queue to be lost 
	// One example:
	// 1 (retran=1) 2-5 (sacked) 6(retran=5) 
	// if this sack block has 6 in its sack, 1 won't be marked lost in the round.
	// However, 1 will be marked as lost when the next SACK arrives
	//
	// Also, if there is some new packet loss happens after the right most seq of this SACK, this
	// pass will not find the loss since it stops at the right most seq of this SACK.
	// However, the lost packets will be marked when there is new SACK come back. 
	//printf("clean rtx\n");
	unsigned char flag = 0;
	bool thorough = CleanRtxQueue(last_ack, &flag);

	if (head_==NULL) {
		ClearScoreBoard();
		thorough=false;
		// if there is no outstanding lost packets, we might not need to do the rest
	}

	if ((!thorough) && (tcph->sa_length()==0)) return (flag);

	// SACK pre-processing
	// we filter D-SACK and sort the SACKs below
	int sa_length = 0;
	int left[NSA+1];
	int right[NSA+1];
	for (int i=0; i < tcph->sa_length(); i++) {
		int sack_left = tcph->sa_left(i);
		int sack_right = tcph->sa_right(i);
		if (sack_left >= last_ack) {	//here we filter the D-SACK
			int j = sa_length;
			sa_length++;
			while ((j>0) && (left[j-1] > sack_left)) { 
				left[j] = left[j-1]; 
				right[j] = right[j-1]; 
				j--;
			}
			//insert to the right place to keep increasing order
			left[j] = sack_left;
			right[j] = sack_right;
		}
	}
	if ((!thorough)&&(sa_length == 0)) return (flag);	//same reason as the first return 0

	if ((sa_length > 0) && (fack_ < (right[sa_length-1]-1))) {
	// advance fack_. right[sa_length-1]-1 is the right most of all the sacks in the packet. 
		fack_ = right[sa_length-1]-1;
		thorough = true;
	}

	//  If there is no scoreboard, create one.
	if (head_==NULL) {
		head_ = new ScoreBoardNode1(last_ack+1, fack_,0);
		//initially, create a block from snd_una to facked, 
		//  with flag==0 meaning that packets are in flight
	};

	// From now, we process the score board
	nxt_to_retrx_ = NULL;


//	printf("start traverse\n");
	int lost_seq_threshold = fack_ - (dupack_threshold+1);
	ScoreBoardNode1* now = head_;	// the block we are processing
	ScoreBoardNode1* prev = NULL;
	int sack_index = 0;	//the sack that is currently focused 
	while ((now != NULL) && ((sack_index<sa_length)||thorough)) {
		if (now->GetFlag()!=SKB_FLAG_SACKED) {
			while ((sack_index<sa_length) && (right[sack_index]<=now->GetStart()) )
				sack_index++;
			//find the sack that match "now"
			if (sack_index<sa_length) {
//printf("Sack block: [%d %d], now block: %d[%d %d]\n", left[sack_index], right[sack_index], now->GetFlag(), now->GetStart(), now->GetEnd());
				if (left[sack_index]<=now->GetEnd()) {
				// We share some intersection between this SACK and this "now"
					if (left[sack_index]>now->GetStart()) {
						//printf("split to make some sackes\n");
						// the first part of "now" is not SACKED
						now->Split(left[sack_index]);
					} else {
						if (right[sack_index]<=now->GetEnd()) {
							//partially sacked
							//printf("going to split\n");
							now->Split(right[sack_index]);
							//printf("done split\n");
						};
						int pktnum = now->GetLength();
						switch (now->GetFlag()) {
						case SKB_FLAG_LOST:
							fack_out_ -= pktnum;
							break;
						case SKB_FLAG_RETRANSMITTED:
							acked_rtx_id_ = now->GetRtx();
							flag |= FLAG_UNSURE_TSTAMP;
							thorough = true;
							break;			
						}
						sack_out_ += pktnum;
						flag |= FLAG_DATA_SACKED;
						now->SetFlag(SKB_FLAG_SACKED);
					}
				};
			}
			if ((sack_index>=sa_length)|| (left[sack_index]>now->GetEnd())) {
//printf("now block: %d[%d %d]\n", now->GetFlag(),now->GetStart(), now->GetEnd());

				switch (now->GetFlag()) {
				case SKB_FLAG_RETRANSMITTED:
					// check if it is lost again
					if (((now->GetSndNxt() <= fack_)
					||((now->GetRtx() + dupack_threshold+1) < acked_rtx_id_))) {
						now->SetFlag(SKB_FLAG_LOST);
						fack_out_++;
						flag |= FLAG_DATA_LOST;
						if (!nxt_to_retrx_) nxt_to_retrx_ = now;
					} 
					break;
				case SKB_FLAG_INFLIGHT:
					//check if it is lost
					if (now->GetStart() <= lost_seq_threshold) {
					//some packets might be lost
						if (lost_seq_threshold<now->GetEnd()) {
							//partial loss
							now->Split(lost_seq_threshold+1);
						}
						now->SetFlag(SKB_FLAG_LOST);
						fack_out_ += now->GetLength();
						flag |= FLAG_DATA_LOST;
						if (!nxt_to_retrx_) nxt_to_retrx_ = now;
					}
					break;
				case SKB_FLAG_LOST:
					if (!nxt_to_retrx_) nxt_to_retrx_ = now;
					break;
				}
			}
		}
//printf("now block: %d[%d %d]\n", now->GetFlag(),now->GetStart(), now->GetEnd());

		if ((prev) && (prev->Mergable())) {
			//Try to merge with previous node
			//printf("Merging %d [%d,%d] + %d [%d, %d]\n", prev->GetFlag(), prev->GetStart(), prev->GetEnd(), now->GetFlag(), now->GetStart(), now->GetEnd());
			prev->Merge();
		} else {
			prev = now;
		}

		if ((prev->GetNext()==NULL) && (prev->GetEnd()<fack_)) {
			//printf("appending %d %d\n",prev->GetEnd()+1, fack_);
			prev->Append(new ScoreBoardNode1(prev->GetEnd()+1, fack_,0));
			//extend the scoreboard
		}
		now = prev -> GetNext();
	};

	return (flag);

}

void ScoreBoard1::MarkLoss(int snd_una, int snd_nxt) {
	nxt_to_retrx_ = NULL;
	ScoreBoardNode1* now = head_;
	ScoreBoardNode1* prev = NULL;
	bool last_lost = false;
	while (now) {
		if (!(now->GetFlag() & (SKB_FLAG_SACKED))) {
			if (! (now->GetFlag() & SKB_FLAG_LOST)) 
				fack_out_ += now->GetEnd() - now->GetStart() + 1;
			now->SetFlag(SKB_FLAG_LOST);
			if (last_lost) {
				prev->Merge();
				now=prev;
			} else {
				if (nxt_to_retrx_==NULL) nxt_to_retrx_ = now;
			}
			last_lost = true;
		} else {
			last_lost = false;
		}
		prev = now;
		now = now->GetNext();
		if ((now==NULL) && ((prev->GetEnd()+1) < snd_nxt)) {
			//all the packets that we sent have lost
			now = new ScoreBoardNode1(prev->GetEnd()+1, snd_nxt-1, SKB_FLAG_INFLIGHT);
			prev->Append(now);
		}
	}
	if ((prev==NULL) && (snd_una<snd_nxt)) {
		//the score board is empty
		head_ = new ScoreBoardNode1(snd_una, snd_nxt-1, SKB_FLAG_LOST);
		nxt_to_retrx_ = head_;
		fack_out_ += snd_nxt - snd_una;
	}
	rtx_id_ = 0;
	acked_rtx_id_ = -1;	
}

void ScoreBoard1::ClearScoreBoard()
{
	rtx_id_ = 0;
	acked_rtx_id_ = -1;
        fack_out_ = 0;
        sack_out_ = 0;
	fack_ = 0;
	nxt_to_retrx_ = NULL;
	while (head_) {
		ScoreBoardNode1* temp = head_;
		head_ = head_->GetNext();
		delete temp;
	}
}

/*
 * GetNextRetran() returns "-1" if there is no packet that is
 *   not acked and not sacked and not retransmitted.
 */
int ScoreBoard1::GetNextRetran()
{
	while ((nxt_to_retrx_) && (nxt_to_retrx_->GetFlag() != SKB_FLAG_LOST))
		nxt_to_retrx_ = nxt_to_retrx_->GetNext();
	if (nxt_to_retrx_) {
		return nxt_to_retrx_->GetStart();
	} else
		return -1;
}

void ScoreBoard1::MarkRetran (int retran_seqno, int snd_nxt)
{
	ASSERT2(((nxt_to_retrx_) && (nxt_to_retrx_->Inside(retran_seqno))), "Trying to mark a retransmission outside the recommended one: %p %d, snd_nxt: %d\n", nxt_to_retrx_, retran_seqno, snd_nxt);
	fack_out_ --;
	ASSERT2((fack_out_>=0), "fack_out gets negative in Mark Retran, %d, snd_nxt: %d, rtx_seq:%d\n", fack_out_, snd_nxt, retran_seqno);
	nxt_to_retrx_->MarkRetran(snd_nxt, rtx_id_);
	rtx_id_++;
	last_rtx_seq_=retran_seqno;
}


void ScoreBoard1::Dump()
{
       if (head_ == NULL ) { printf("SB: No entry\n"); return; };

       printf("SB len:%d fack:%d sack_out:%d fack_out:%d acked_rtx_id:%d next_rtx_id:%d\n", 
		fack_-head_->GetStart(), 
		fack_, 
		sack_out_,
		fack_out_,
		acked_rtx_id_,
		rtx_id_
	);

	ScoreBoardNode1* now=head_;
	while (now) {
		if (now->GetFlag()==SKB_FLAG_RETRANSMITTED) 
			printf("seq:%d (%d snd_nxt:%d rtxid:%d)\n", now->GetStart(), now->GetFlag(), now->GetSndNxt(), now->GetRtx());
		else
			printf("seq:[%d,%d] (%d)\n", now->GetStart(), now->GetEnd(), now->GetFlag());
		now=now->GetNext();
       }
       printf("\n");
}

void ScoreBoard1::test() 
{
	ClearScoreBoard();
	hdr_tcp test;
	test.sa_length_ =  1;
	test.sack_area_[0][0]= 4;
	test.sack_area_[0][1]= 5;
	
	printf(" 1 x x 4  \n");
	UpdateScoreBoard(1, &test); Dump();
        test.sack_area_[0][0]= 7;
        test.sack_area_[0][1]= 8;
	
	printf(" 1 x x 4 x x 7 \n");
	UpdateScoreBoard(1, &test); Dump();

	printf(" 1 R x 4 x x 7\n");
	MarkRetran(GetNextRetran(),8); Dump();
	
	test.sack_area_[0][0]= 9;
	test.sack_area_[0][1]= 10;
	printf(" 1 x x 4 x x 7 x 9 \n");
	UpdateScoreBoard(1, &test); Dump();

	test.sack_area_[0][0]= 7;
        test.sack_area_[0][1]= 9;
        printf(" 1 x x 4 x x 7 8 9\n");
        UpdateScoreBoard(1, &test); Dump();

	printf(" 1 R x 4 x x 7 8 9\n");
        MarkRetran(GetNextRetran(),60); Dump();

	printf(" 1 R R 4 x x 7 8 9\n");
        MarkRetran(GetNextRetran(),60); Dump();

	test.sack_area_[0][0]= 3;
        test.sack_area_[0][1]= 4;
        printf(" 1 R 3 4 x x 7 8 9\n");
        UpdateScoreBoard(1, &test); Dump();

	test.sack_area_[0][0]= 15;
        test.sack_area_[0][1]= 19;
        printf(" 1 R 3 4 x x 7 8 9 x x x x x 15 - 19\n");
        UpdateScoreBoard(1, &test); Dump();

	printf(" 1 R R 4 R x 7 8 9 x x x x x 15 - 19\n");
        MarkRetran(GetNextRetran(),60); Dump();

	printf(" 1 R 3 4 R R 7 8 9 x x x x x 15 - 19\n");
        MarkRetran(GetNextRetran(),60); Dump();

	printf(" 1 R R 4 R R 7 8 9 R x x x x 15 - 19\n");
        MarkRetran(GetNextRetran(),60); Dump();

        printf(" 1 R 3 4 R R 7 8 9 R R x x x 15 16\n");
        MarkRetran(GetNextRetran(),60); Dump();

        printf(" 1 R 3 4 R R 7 8 9 R R R x x 15 16\n");
        MarkRetran(GetNextRetran(),60); Dump();

        printf(" 1 R 3 4 R R 7 8 9 R R R R x 15 16\n");
        MarkRetran(GetNextRetran(),60); Dump();

	printf(" 1 R 3 4 R R 7 8 9 R R R R R 15 16\n");
        MarkRetran(GetNextRetran(),60); Dump();

        test.sack_area_[0][0]= 5;
        test.sack_area_[0][1]= 11;
        printf(" 1 R 3 4 5 6 7 8 9 10 R R R R 15 16\n");
        UpdateScoreBoard(1, &test); Dump();

	test.sack_area_[0][0]= 25;
        test.sack_area_[0][1]= 40;
        printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 25-40\n");
        UpdateScoreBoard(1, &test); Dump();

	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 R\n");
        MarkRetran(GetNextRetran(),90); Dump();
	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 R R\n");
        MarkRetran(GetNextRetran(),20); Dump();
	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 R R R\n");
        MarkRetran(GetNextRetran(),20); Dump();
	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 R R R R\n");
        MarkRetran(GetNextRetran(),20); Dump();
	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 R R R R R\n");
        MarkRetran(GetNextRetran(),20); Dump();
	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16 R R R R R R\n");
        MarkRetran(GetNextRetran(),20); Dump();

        test.sack_area_[0][0]= 11;
        test.sack_area_[0][1]= 16;
        printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 14 15 16 25-40\n");
        UpdateScoreBoard(1, &test); Dump();

	test.sack_area_[0][0]= 11;
        test.sack_area_[0][1]= 16;
        printf(" 1 x 3 4 5 6 7 8 9 10 11 12 13 14 15 16 25-40\n");
        UpdateScoreBoard(1, &test); Dump();


	printf(" 1 R 3 4 5 6 7 8 9 10 11 12 13 R 15 16\n");
	MarkRetran(GetNextRetran(),20); Dump();

	UpdateScoreBoard(3, &test); Dump();
}