/* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ /* * Copyright (c) 1990, 1997 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the University of California, Lawrence Berkeley Laboratory, * Berkeley, CA. The name of the University may not be used to * endorse or promote products derived from this software without * specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ #ifndef lint static const char rcsid[] = "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/tcp/tcp-newreno.cc,v 1.56 2004/10/26 22:59:42 sfloyd Exp $ (LBL)"; #endif // // newreno-tcp: a revised reno TCP source, without sack // #include #include #include #include "packet.h" #include "ip.h" #include "tcp.h" #include "flags.h" static class NewRenoTcpClass : public TclClass { public: NewRenoTcpClass() : TclClass("Agent/TCP/Newreno") {} TclObject* create(int, const char*const*) { return (new NewRenoTcpAgent()); } } class_newreno; NewRenoTcpAgent::NewRenoTcpAgent() : newreno_changes_(0), newreno_changes1_(0), acked_(0), firstpartial_(0), partial_window_deflation_(0), exit_recovery_fix_(0), /* Added by Nadim (UofC) */ countfr(0),countto(0),countifr(0),countlossevent(0), countloss(0),counttotalloss(0),countpktacked(0),markpktacked(0), traceflowno(-1),letracestart(0.0),fplossevent(NULL) { bind("traceflowno", &traceflowno); bind("countfr", &countfr); bind("countifr", &countifr); bind("countto", &countto); bind("countlossevent",&countlossevent); bind("counttotalloss", &counttotalloss); bind("countpktacked", &countpktacked); bind("letracestart", &letracestart); /* End of addition by Nadim (UofC) */ bind("newreno_changes_", &newreno_changes_); bind("newreno_changes1_", &newreno_changes1_); bind("exit_recovery_fix_", &exit_recovery_fix_); bind("partial_window_deflation_", &partial_window_deflation_); } /* Added by Nadim (UofC) */ int NewRenoTcpAgent::delay_bind_dispatch(const char *varName, const char *localName, TclObject *tracer) { if (delay_bind(varName, localName, "traceflowno", &traceflowno, tracer)) return TCL_OK; if (delay_bind(varName, localName, "countfr", &countfr, tracer)) return TCL_OK; if (delay_bind(varName, localName, "countifr", &countifr, tracer)) return TCL_OK; if (delay_bind(varName, localName, "countto", &countto, tracer)) return TCL_OK; if (delay_bind(varName, localName, "countlossevent", &countlossevent, tracer)) return TCL_OK; if (delay_bind(varName, localName, "counttotalloss", &counttotalloss, tracer)) return TCL_OK; if (delay_bind(varName, localName, "countpktacked", &countpktacked, tracer)) return TCL_OK; if (delay_bind(varName, localName, "letracestart", &letracestart, tracer)) return TCL_OK; return TcpAgent::delay_bind_dispatch(varName, localName, tracer); } /* End of addition by Nadim (UofC) */ /* * Process a packet that acks previously unacknowleges data, but * does not take us out of Fast Retransmit. */ void NewRenoTcpAgent::partialnewack(Packet* pkt) { hdr_tcp *tcph = hdr_tcp::access(pkt); if (partial_window_deflation_) { // Do partial window deflation before resetting last_ack_ unsigned int deflate = 0; // Should initialize it?? - haoboy if (tcph->seqno() > last_ack_) // assertion deflate = tcph->seqno() - last_ack_; else printf("False call to partialnewack: deflate %u \ last_ack_ %d\n", deflate, last_ack_); if (dupwnd_ > deflate) dupwnd_ -= (deflate - 1); else { cwnd_ -= (deflate - dupwnd_); // Leave dupwnd_ > 0 to flag "fast recovery" phase dupwnd_ = 1; } if (cwnd_ < 1) {cwnd_ = 1;} } last_ack_ = tcph->seqno(); highest_ack_ = last_ack_; if (t_seqno_ < last_ack_ + 1) t_seqno_ = last_ack_ + 1; if (rtt_active_ && tcph->seqno() >= rtt_seq_) { rtt_active_ = 0; t_backoff_ = 1; } } void NewRenoTcpAgent::partialnewack_helper(Packet* pkt) { if (!newreno_changes1_ || firstpartial_ == 0) { firstpartial_ = 1; /* For newreno_changes1_, * only reset the retransmit timer for the first * partial ACK, so that, in the worst case, we * don't have to wait for one packet retransmitted * per RTT. */ newtimer(pkt); } partialnewack(pkt); output(last_ack_ + 1, 0); } /* Added by Nadim (UofC), May 13, 2005*/ void NewRenoTcpAgent::trace_lossevent(char *eventname){ double ctime; Scheduler& s = Scheduler::instance(); ctime = &s ? s.clock() : 0; if(fid_ != traceflowno) return; printf("\n%s: LE %d PL %d backoff %d recover %d count %d time %0.4lf",eventname,countlossevent,counttotalloss,(unsigned)t_backoff_,recover_,count_,ctime); printf("\n\tcwnd %0.2f dupwnd %d ssthresh %d ack %u snd %u tseq %u",(double)cwnd_,dupwnd_,(unsigned)ssthresh_, (unsigned)highest_ack_,(unsigned)maxseq_,(unsigned)t_seqno_); } /* End of addition by Nadim */ int NewRenoTcpAgent::allow_fast_retransmit(int /* last_cwnd_action_*/) { return 0; } void NewRenoTcpAgent::dupack_action() { int recovered = (highest_ack_ > recover_); int recovered1 = (highest_ack_ == recover_); int allowFastRetransmit = allow_fast_retransmit(last_cwnd_action_); if (recovered || (!bug_fix_ && !ecn_) || allowFastRetransmit) { goto reno_action; } if (bug_fix_ && less_careful_ && recovered1) { /* * For the Less Careful variant, allow a Fast Retransmit * if highest_ack_ == recover. * RFC 2582 recommends the Careful variant, not the * Less Careful one. */ goto reno_action; } if (ecn_ && last_cwnd_action_ == CWND_ACTION_ECN) { last_cwnd_action_ = CWND_ACTION_DUPACK; /* * What if there is a DUPACK action followed closely by ECN * followed closely by a DUPACK action? * The optimal thing to do would be to remember all * congestion actions from the most recent window * of data. Otherwise "bugfix" might not prevent * all unnecessary Fast Retransmits. */ reset_rtx_timer(1,0); output(last_ack_ + 1, TCP_REASON_DUPACK); dupwnd_ = numdupacks_; return; } if (bug_fix_) { if (bugfix_ts_ && tss[highest_ack_ % tss_size_] == ts_echo_) goto reno_action; else if (bugfix_ack_ && cwnd_ > 1 && highest_ack_ - prev_highest_ack_ <= numdupacks_) goto reno_action; else /* * The line below, for "bug_fix_" true, avoids * problems with multiple fast retransmits in one * window of data. */ return; } reno_action: /* Added by Nadim: Counts Loss Event and Loss Rate */ if(highest_ack_ > recover_ ){ countloss++; counttotalloss++; countfr++; countlossevent++; } // lossevent is based on window /* End of addition by Nadim */ recover_ = maxseq_; reset_rtx_timer(1,0); if (!lossQuickStart()) { trace_event("NEWRENO_FAST_RETX"); last_cwnd_action_ = CWND_ACTION_DUPACK; slowdown(CLOSE_SSTHRESH_HALF|CLOSE_CWND_HALF); output(last_ack_ + 1, TCP_REASON_DUPACK); // from top dupwnd_ = numdupacks_; } // Single line added by nadim trace_lossevent("FAST_RECOVERY"); return; } /* Added by Nadim (UofC): Count Loss Events and Loss Rate */ void NewRenoTcpAgent::timeout(int tno) { if (tno == TCP_TIMER_RTX) { if(t_backoff_== 1) { // entering to timeout first time countloss=countloss+(int) cwnd_; counttotalloss=counttotalloss+(int)window(); countto++; countlossevent++; if( highest_ack_ < recover_ ){ // was in same drop window countfr--; countifr++; } // Count loss event and loss rate: Added by Nadim (UofC) } else { // if backoff is >= 2 counttotalloss=counttotalloss+(int) cwnd_; countto++; countlossevent++; } // End of addition by Nadim dupwnd_ = 0; dupacks_ = 0; if (bug_fix_) recover_ = maxseq_; TcpAgent::timeout(tno); // Single line added by Nadim trace_lossevent("TIMEOUT"); } else { timeout_nonrtx(tno); } } /* End of addition by Nadim (UofC) */ void NewRenoTcpAgent::recv(Packet *pkt, Handler*) { hdr_tcp *tcph = hdr_tcp::access(pkt); int valid_ack = 0; // Single line added by Nadim char eventname[64]=""; /* Use first packet to calculate the RTT --contributed by Allman */ if (qs_approved_ == 1 && tcph->seqno() > last_ack_) endQuickStart(); if (qs_requested_ == 1) processQuickStart(pkt); if (++acked_ == 1) basertt_ = Scheduler::instance().clock() - firstsent_; /* Estimate ssthresh based on the calculated RTT and the estimated bandwidth (using ACKs 2 and 3). */ else if (acked_ == 2) ack2_ = Scheduler::instance().clock(); else if (acked_ == 3) { ack3_ = Scheduler::instance().clock(); new_ssthresh_ = int((basertt_ * (size_ / (ack3_ - ack2_))) / size_); if (newreno_changes_ > 0 && new_ssthresh_ < ssthresh_) ssthresh_ = new_ssthresh_; } #ifdef notdef if (pkt->type_ != PT_ACK) { fprintf(stderr, "ns: confiuration error: tcp received non-ack\n"); exit(1); } #endif /* W.N.: check if this is from a previous incarnation */ if (tcph->ts() < lastreset_) { // Remove packet and do nothing Packet::free(pkt); return; } ++nackpack_; ts_peer_ = tcph->ts(); if (hdr_flags::access(pkt)->ecnecho() && ecn_) ecn(tcph->seqno()); recv_helper(pkt); recv_frto_helper(pkt); /* Added by Nadim (UofC): Counts Loss Event and Loss Rate */ { double ctime; Scheduler& s = Scheduler::instance(); ctime = &s ? s.clock() : 0; countpktacked = highest_ack_ - markpktacked; if(ctime < letracestart) { // resets all trace vars countfr = 0; countto = 0; countifr = 0; countlossevent = 0; countloss = 0; counttotalloss = 0; countpktacked = 0; markpktacked = highest_ack_; } } /* End of addition by nadim */ if (tcph->seqno() > last_ack_) { if (tcph->seqno() >= recover_ || (last_cwnd_action_ != CWND_ACTION_DUPACK)) { if (dupwnd_ > 0) { dupwnd_ = 0; if (last_cwnd_action_ == CWND_ACTION_DUPACK) last_cwnd_action_ = CWND_ACTION_EXITED; if (exit_recovery_fix_) { int outstanding = maxseq_ - tcph->seqno() + 1; if (ssthresh_ < outstanding) cwnd_ = ssthresh_; else cwnd_ = outstanding; } } /* Count Loss Event and Loss Rate. Added by Nadim (UofC) */ if(tcph->seqno() >= recover_ && countloss > 0){ // enters here when recovery ends or timeout ends // save to disk if (countpktacked && traceflowno == fid_){ double ctime; Scheduler& s = Scheduler::instance(); ctime = &s ? s.clock() : 0; printf("NRtm %0.4f fid %1d NRer %0.4f Rer: %0.4f ",ctime,fid_,1.0*countlossevent/countpktacked,1.0*(countto+countfr)/countpktacked); printf("plr %0.4f ",1.0*counttotalloss/countpktacked); printf("NRe %5d Re %5d ",countlossevent,countto+countfr); printf("fr %4d ifr %4d to %3d",countfr,countifr,countto); printf(" pl %4d linw %1d packed %5d\n",counttotalloss,countloss, countpktacked); } countloss = 0; // reinitializing //trace_lossevent("EXIT_RECOVERY/TIMEOUT"); } /* End of addition by Nadim */ firstpartial_ = 0; recv_newack_helper(pkt); if (last_ack_ == 0 && delay_growth_) { cwnd_ = initial_window(); } // Two lines Added by Nadim (UofC): Counting Loss Events countpktacked = highest_ack_ - markpktacked; strcpy(eventname,"REGULAR_ACK"); } else { /* received new ack for a packet sent during Fast * Recovery, but sender stays in Fast Recovery */ if (partial_window_deflation_ == 0) dupwnd_ = 0; partialnewack_helper(pkt); /* Single line added by Nadim (UofC): Counting Loss Events and Loss Rate */ countloss++; counttotalloss++; countpktacked = highest_ack_ - markpktacked; strcpy(eventname,"PARTIAL_ACK"); } } else if (tcph->seqno() == last_ack_) { if (hdr_flags::access(pkt)->eln_ && eln_) { tcp_eln(pkt); return; } if (++dupacks_ == numdupacks_) { dupack_action(); if (!exitFastRetrans_) dupwnd_ = numdupacks_; } else if (dupacks_ > numdupacks_ && (!exitFastRetrans_ || last_cwnd_action_ == CWND_ACTION_DUPACK)) { trace_event("NEWRENO_FAST_RECOVERY"); ++dupwnd_; // fast recovery /* For every two duplicate ACKs we receive (in the * "fast retransmit phase"), send one entirely new * data packet "to keep the flywheel going". --Allman */ if (newreno_changes_ > 0 && (dupacks_ % 2) == 1) output (t_seqno_++,0); } else if (dupacks_ < numdupacks_ && singledup_ ) { send_one(); } // Single line added by nadim strcpy(eventname,"DUP_ACK"); } if (tcph->seqno() >= last_ack_) // Check if ACK is valid. Suggestion by Mark Allman. valid_ack = 1; Packet::free(pkt); #ifdef notyet if (trace_) plot(); #endif /* * Try to send more data */ if (valid_ack || aggressive_maxburst_) if (dupacks_ == 0) /* * Maxburst is really only needed for the first * window of data on exiting Fast Recovery. */ send_much(0, 0, maxburst_); else if (dupacks_ > numdupacks_ - 1 && newreno_changes_ == 0) send_much(0, 0, 2); // Single line added by nadim trace_lossevent(eventname); }