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
|
/* -*- 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.
*/
/* 9/96 Pittsburgh Supercomputing Center
* UpdateScoreBoard, CheckSndNxt, MarkRetran modified for fack
*/
/* A quick hack version of the scoreboard */
#include <stdlib.h>
#include <stdio.h>
#include "scoreboard.h"
#include "tcp.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 SBNI SBN[i%sbsize_]
// last_ack = TCP last ack
int ScoreBoard::UpdateScoreBoard (int last_ack, hdr_tcp* tcph)
{
int i, sack_index, sack_left, sack_right;
int retran_decr = 0;
changed_ = 0;
// Advance the left edge of the block.
if (length_ && SBN[first_%sbsize_].seq_no_ <= last_ack) {
for (i=SBN[first_%sbsize_].seq_no_; i<=last_ack; i++) {
// Advance the ACK
if (SBNI.seq_no_ <= last_ack) {
ASSERT(first_ == i);
first_ = (first_+1);
length_--;
ASSERT1(length_ >= 0);
SBNI.ack_flag_ = 1;
SBNI.sack_flag_ = 1;
if (SBNI.retran_) {
SBNI.retran_ = 0;
SBNI.snd_nxt_ = 0;
retran_decr++;
}
changed_++;
if (length_==0)
break;
}
}
}
// If there is no scoreboard, create one.
if (length_ == 0 && tcph->sa_length()) {
i = last_ack+1;
SBNI.seq_no_ = i;
SBNI.ack_flag_ = 0;
SBNI.sack_flag_ = 0;
SBNI.retran_ = 0;
SBNI.snd_nxt_ = 0;
first_ = i;
length_++;
if (length_ >= sbsize_) {
printf ("Error, scoreboard too large (increase sbsize_ for more space)\n");
exit(1);
}
changed_++;
}
for (sack_index=0; sack_index < tcph->sa_length(); sack_index++) {
sack_left = tcph->sa_left(sack_index);
sack_right = tcph->sa_right(sack_index);
// Create new entries off the right side.
if (sack_right > SBN[(first_+length_+sbsize_-1)%sbsize_].seq_no_) {
// Resize the scoreboard if it is going to overrun the length
while((sack_right - last_ack) >= sbsize_ -1 ){
resizeSB(sbsize_*2);
}
// Create new entries
for (i = SBN[(first_+length_+sbsize_-1)%sbsize_].seq_no_+1; i<sack_right; i++) {
SBNI.seq_no_ = i;
SBNI.ack_flag_ = 0;
SBNI.sack_flag_ = 0;
SBNI.retran_ = 0;
SBNI.snd_nxt_ = 0;
length_++;
if (length_ >= sbsize_) {
fprintf(stderr, "ERROR: Scoreboard got too large!!!\n");
fprintf(stderr, " SBN[first (mod) sbsize_]: %i, sack_right: %i length_: %i\n", SBN[first_%sbsize_].seq_no_,sack_right , length_);
fprintf(stderr, "last_ack: %i SBN[(first_+length_+sbsize_-1) (mod) sbsize_].seq_no_: %i, sbsize_: %i\n", last_ack, SBN[(first_+length_+sbsize_-1)%sbsize_].seq_no_ , sbsize_);
exit(1);
}
changed_++;
}
}
for (i=SBN[(first_)%sbsize_].seq_no_; i<sack_right; i++) {
// Check to see if this segment is now covered by the sack block
if (SBNI.seq_no_ >= sack_left && SBNI.seq_no_ < sack_right) {
if (! SBNI.sack_flag_) {
SBNI.sack_flag_ = 1;
changed_++;
}
if (SBNI.retran_) {
SBNI.retran_ = 0;
retran_decr++;
}
}
}
}
return (retran_decr);
}
int ScoreBoard::CheckSndNxt (hdr_tcp* tcph)
{
int i, sack_index, sack_right;
int force_timeout = 0;
for (sack_index=0; sack_index < tcph->sa_length(); sack_index++) {
sack_right = tcph->sa_right(sack_index);
for (i=SBN[(first_)%sbsize_].seq_no_; i<sack_right; i++) {
// Check to see if this segment's snd_nxt_ is now covered by the sack block
if (SBNI.retran_ && SBNI.snd_nxt_ < sack_right) {
// the packet was lost again
SBNI.retran_ = 0;
SBNI.snd_nxt_ = 0;
force_timeout = 1;
}
}
}
return (force_timeout);
}
void ScoreBoard::ClearScoreBoard()
{
length_ = 0;
}
/*
* GetNextRetran() returns "-1" if there is no packet that is
* not acked and not sacked and not retransmitted.
*/
int ScoreBoard::GetNextRetran() // Returns sequence number of next pkt...
{
int i;
if (length_) {
for (i=SBN[(first_)%sbsize_].seq_no_;
i<SBN[(first_)%sbsize_].seq_no_+length_; i++) {
if (!SBNI.ack_flag_ && !SBNI.sack_flag_ && !SBNI.retran_) {
return (i);
}
}
}
return (-1);
}
/*
* GetNextUnacked returns sequence number of next unacked pkt,
* starting with seqno.
* Returns -1 if there is no unacked packet in that range.
*/
int ScoreBoard::GetNextUnacked (int seqno)
{
int i;
if (!length_) {
return (-1);
} else if (seqno < SBN[(first_)%sbsize_].seq_no_ ||
seqno >= SBN[(first_)%sbsize_].seq_no_+length_) {
return (-1);
} else {
for (i=seqno; i<SBN[(first_)%sbsize_].seq_no_+length_; i++) {
if (!SBNI.ack_flag_ && !SBNI.sack_flag_) {
return (i);
}
}
}
return (-1);
}
void ScoreBoard::MarkRetran (int retran_seqno, int snd_nxt)
{
SBN[retran_seqno%sbsize_].retran_ = 1;
SBN[retran_seqno%sbsize_].snd_nxt_ = snd_nxt;
}
void ScoreBoard::MarkRetran (int retran_seqno)
{
SBN[retran_seqno%sbsize_].retran_ = 1;
}
void ScoreBoard::resizeSB(int sz)
{
ScoreBoardNode *newSBN = new ScoreBoardNode[sz+1];
if(!newSBN){
fprintf(stderr, "Unable to allocate new ScoreBoardNode[%i]\n", sz);
exit(1);
}
for(int i = SBN[first_%sbsize_].seq_no_;
i<=SBN[(first_)%sbsize_].seq_no_+length_; i++) {
newSBN[i%sz] = SBN[i%sbsize_];
}
delete[] SBN;
SBN = newSBN;
sbsize_ = sz;
}
void ScoreBoard::Dump()
{
int i;
printf("SB len: %d ", length_);
if (length_) {
for (i=SBN[(first_)%sbsize_].seq_no_;
i<SBN[(first_)%sbsize_].seq_no_+length_; i++) {
printf("seq: %d [ ", i);
if(SBNI.ack_flag_)
printf("A");
if(SBNI.sack_flag_)
printf("S");
if(SBNI.retran_)
printf("R");
printf(" ]");
}
}
printf("\n");
}
|