53
Network Simulator - 2 Source Analysis Made by Min-Soo Kim and Kang-Yong Lee Ajou University, Division of Information & Computer Engineering

Network Simulator - 2

  • Upload
    denzel

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

Network Simulator - 2. Source Analysis Made by Min-Soo Kim and Kang-Yong Lee Ajou University, Division of Information & Computer Engineering. Content. Development Environment Structure of Source Directory We Focus On Event Scheduler Layered View of NS-2 Internal Node of NS-2 - PowerPoint PPT Presentation

Citation preview

Page 1: Network Simulator - 2

Network Simulator - 2

Source AnalysisMade by Min-Soo Kim and Kang-Yong Lee

Ajou University, Division of Information & Computer Engineering

Page 2: Network Simulator - 2

Content

Development Environment Structure of Source Directory We Focus On Event Scheduler Layered View of NS-2 Internal Node of NS-2 Link of NS-2 Agent Overview of Packet Flow TcpAgent

Page 3: Network Simulator - 2

Development Environment

Operating System Unix-like System (FreeBSD, Linux, SunOS)

Program Code C++ , OTcl

Version of Our NS-2 Ns-2 2.6

Page 4: Network Simulator - 2

Structure of Source Directory (1) ns-allinone-2.26

/ns-allinone/ns-allinone

/cweb/cweb

/gt-itm/gt-itm

/nam-1.9/nam-1.9

/ns-2.26/ns-2.26

/otcl-1.0a8/otcl-1.0a8

/sgb/sgb

/tcl8.3.2/tcl8.3.2

/tclcl-1.0b13/tclcl-1.0b13

/tk8.3.2/tk8.3.2

/xgraph-12.1/xgraph-12.1

/zlib-1.1.4/zlib-1.1.4

version of WEB for documenting C,C++ (Optional)

Georgia Tech Internetwork Topology Modeler (Optional)

Network Animator (Optional)

NS Main Compoment(Required)

Otcl Library Source (Required)

Stanford GraphBase package (Optional)

Tclcl Library Source (Required)

Tcl/C++ Interface[Linkage] (Required)

Tk Library Source (Required)

X-graph Source(Optional)

Data Compression Library (Optional)

Page 5: Network Simulator - 2

Structure of Source Directory (2) ns-allinone/ns-2.26

/ns-allinone/ns-2.26/ns-allinone/ns-2.26/adc/adc

/aodv/aodv/apps/apps/asim/asim

/baytcp/baytcp/classifier/classifier/common/common

/conf/conf/diffserv/diffserv

/diffusion/diffusion/dsdv/dsdv

/dsr/dsr/emulate/emulate

/link/link/linkstate/linkstate

/mac/mac/mcast/mcast/mobile/mobile/queue/queue

/routealgo/routealgo/sensor-nets/sensor-nets

/tcp/tcp/trace/trace

Application Protocol Classes(FTP, PING..)

AODV Routing Protocol

Common Classes (node, agent, scheduler, Timer-handler, bi-

connector, packet, encapsulator, decapsulator classes)

Link related Classes

Mac Layer Protocol(Wired and Wireless)

Multicast related Classes

Various Queue Model Classes

TCP Protocol related Classes

Routing Algorithm

Trace & Result file related Classes

Page 6: Network Simulator - 2

We Focus on Inside of “/ns-2.26” Directory

Event Scheduler Basic Network Components :

Node, Link, Packet Traffic models and applications :

Web, FTP, telnet, Constant-bit rate, real audio Transport protocols :

Unicast: TCP, UDP Multicast

Routing and queueing : Wired Routing, Wireless Routing Queuing protocols : RED, drop-tail

Physical media : Wired (LANs, P-to-P), Wireless Channel, Satellite Channel

Inside of “/tclcl-1.0b13” Directory Otcl/C++ Linkage Classes

Page 7: Network Simulator - 2

Event Scheduler (1) NS-2 “event-driven” simulator Characteristics of Event Scheduler

Single-Threaded Only one event in execution at any given time Unit of time seconds Execution Policy First scheduled – First dispatched manner

Related classes & Functions of Event Scheduler In “/ns-2.26/common/scheduler.h” “/ns-2.26/common/scheduler.cc”

Class Event {} double time_ : time at which event is ready

int uid_ : unique ID of event Event* next_ : event list Handler* handler_ : handler to call when event’s scheduled time arrived

Class Handler {} virtual void handle (Event* event) : handling the event which received as parameter (dispatch)

Class Scheduler {}Next Page

Page 8: Network Simulator - 2

Event Scheduler (2) Class Scheduler {}

void schedule(Handler*, Event*, double delay) : Schedule later event void dispatch(Event*) : execute an event void dispatch(Event*, double) : execute an event at specific time void cancel (Event* ) : cancel event void insert (Event* ) : schedule event Event* deque(void) : next event (removes from queue) Event* lookup(scheduler_uid_t) : look for event

Classes derived from Scheduler {} Class ListScheduler {} : implements the scheduler using a simple linked-list structure Class HeapScheduler {} : implements the scheduler using a heap structure Class CalendarScheduler {} :implements the scheduler using a one-year calendar on which events

on the same month/day of multiple years can be recorded in one day. Class RealTimeScheduler {} : implements the scheduler using synchronization of events with real-

time

SchedulerScheduler

ListSchedulerListScheduler HeapSchedulerHeapScheduler CalendarSchedulerCalendarScheduler RealTimeSchedulerRealTimeScheduler

Page 9: Network Simulator - 2

Event Scheduler (3)

time_, uid_, next_, handler_

rescheduling

time_, uid_, next_, handler_

Handler.handle(Event *)

Scheduler.insert(Event *)

head_ ->

Scheduler.deque(void)

Network Object

Scheduler.dispatch(Event *, Double Time)

Scheduler.schedule(Handler h, Event *, Double delay)

Page 10: Network Simulator - 2

Layered View of NS-2 Internal (1)

Packet Flow

Higher Layer

Link Layer (LL)

Application::virtual send()Application::virtual send()

Transport TCPagent or UDPagentTransport TCPagent or UDPagent

Network::schedule()

Network::schedule()

A : Sender

recv()recv()

sendDown()sendDown()

Schedule()Schedule()Schedule with Delay

(1)

(3)

(2)

(4)(6)

Related Files-Higher Layer :

/ns-2.26/agent.cc

/ns-2.26/tcp.cc

/ns-2.26/userfiles

-Link Layer :

/ns-2.26/ll.cc

/ns-2.26/ll.h

-Mac Layer :

/ns-2.26/mac-802_3.cc

/ns-2.26/mac-802.3.h

-Phy Layer :

/ns-2.26/phy.cc

/ns-2.26/phy.h

/ns-2.26/channel.cc

/ns-2.26/channel.h

Page 11: Network Simulator - 2

Mac Layer

(802.3)

Physical Layer

(phy)

sendDown()sendDown()

recv()recv()From Upper Layer

recv()recv()

Physical Layer

(channel)get_pdelay()get_pdelay()

recv()recv()

Schedule()Schedule()

rrecv()rrecv()

sendUp()sendUp()

To Upper Layer

(5)

(6)

(7)

(8)

(9)

(10)

Layered View of NS-2 Internal (2)

Page 12: Network Simulator - 2

sendUp()sendUp()

recv()recv()

mac-recv()mac-recv()

recv()recv()From lower Layer

Mac Layer

(Classifier/Mac)

sendUp()sendUp()

recv()recv() Mac Layer

(802.3)

Link Layer (LL)

Schedule()Schedule()

Application::virtual recv()Application::virtual recv()

Transport TCPagent or UDPagentTransport TCPagent or UDPagent

Network::schedule()

Network::schedule()

(10)

(11)

(12)

(13)

(14)

(15)

(16)

B : Receiver

Layered View of NS-2 Internal (3)

Page 13: Network Simulator - 2

Layered View of NS-2 Internal (4) Connectivity within LAN environment

Hig

he

r L

aye

rs

Agent AgentAgent

Lin

k L

aye

r

LL (Link Layer) LLLL

Queue QueueQueue

Ma

c L

aye

r

Mac MacMac

Ph

ysic

al L

aye

r

Channel Classifer/Mac

Page 14: Network Simulator - 2

Network Components - Node (1) Node basic

NS Node is essentially a collection of classifier Unicast Node and Multicast Node

Classifier Classifier doing Packet forwarding related works When it receives a packet, it examine the packet’s field, usually its

destination address. It should then map the value to an outgoing interface object that is the next downstream recipient of this packet.

Each classifier contains a table of simulation objects indexed by slot number

The job of a classfier is to determine the slot number associated with a received packet and forward that packet to the object referenced by that particular slot.

The C++ class Classifer(/ns/classifier/classifier.cc) provieds a base class from which other classifier are derived.

Page 15: Network Simulator - 2

Network Components - Node (2) Source of Classifierclass Classifier : public NsObject {

public:

Classifier();

virtual ~Classifier();

void recv(Packet* p, Handler* h);

virtual int classify(Packet *);

virtual void clear(int slot);

virtual void install(int slot, NsObject*);

// function to set the rtg table size

void set_table_size(int nn);

protected:

void alloc(int);

NsObject** slot_; //table that maps slot number to a NsObject

int nslot_;

int maxslot_;

int offset_;// offset for NsObject *default_target_;

int nsize_; //what size of nslot_ should be

};

void Classifier::recv(Packet* p, Handler*h)

{

NsObject* node;

int cl = classify(p);

if (cl < 0 || cl >= nslot_ || (node = slot_[cl]) == 0)

{

if (default_target_)

return default_target_;

Tcl::instance().evalf("%s no-slot %ld", name(), cl);

if (cl == TWICE)

{

cl = classify(p);

if (cl < 0 || cl >= nslot_ || (node = slot_[cl])==0)

return (NULL);

}

}

node->recv(p,h);

}

Page 16: Network Simulator - 2

Network Components - Node (3) Unicast Node

Port Classifier : Distribute incomming packet to the correct agent Addr Classifier : Distribute incomming packet to the correct outgoing link

Link

Agent

Agent

Agent

Link Link

NODE

Node entry

entry_

Addr Classifier dmux_

Port Classifier

agents_

Page 17: Network Simulator - 2

Network Components - Node (4) Multicast Node

Switch : determine unicast packet or multicast packet Multicast Classifier : classfies packets according to both source and destination group address Replicator : produce n copy of packet that are deliverd to to all n objects referenced in table

Link

Agent

Agent

Agent

Link Link

MUTICASTNODE

Node entry

entry_switch_

dmux_

agents_

MuticastClassifier

classifier_

muticastclassifier_

<S1,G1>

<S2,G2>

Replicators

Page 18: Network Simulator - 2

Network Components - Link (1) Link basic

Link is built up from a sequence of connectors The Class Link is implemented entirely in Otcl. Link Class is base class of

other Link Classes. The Class SimpleLink implements simple point-to-point link with an

associated queue and delay. It is derived from the base Otcl class Link.

SimpleLink head_ : entry point to the link, it point first object in the link. queue_ : reference to the main queue element of the link. link_ : reference to the element that simulate packet delivery delays. ttl_ : reference to the element that manipulates the ttl in every packet. drophead_ : reference to an object that is the head of a queue of elements

that process link drops. enqT_, deqT_, drpT_, rcvT_ : reference to the element that traces

packets.

Page 19: Network Simulator - 2

Network Components - Link (2) Composite Construction of a Undirectional Link (SimpleLink)

Link

enqT_ queue_ deqT_ link_ rcvT_ttl_

drophead_ drpT_

head_

Page 20: Network Simulator - 2

Network Components - Link (3) Connectors

Connectors, unlike classifiers, only generate data for one recipient. either the packet is delivered to the the neighbor ( target_ ), or it is sent to

the drop-target A connector will receive a packet, perform some function, and deliver the

packet to its neighbor, or drop the packet. There are a number of different types of connectors in ns. Each connector

performs a difference function.

Different types of Connectors networkinterface : it labels packets with incomming interface identifier. DynaLink : it decides whether or not a packet should forwarded depeding

on whether the link is up or down. DelayLink : it models the link’s delay and bandwidth characteristic Queues : it models the output buffers attached to a link in a “real” router in

a network. TTLChecker : it will decrement the ttl in each packet that it receives.

Page 21: Network Simulator - 2

Agent – What is the “Agent” ?

Agent Agents represent endpoints where network-layer packets are constructed

or consumed.

Agent are used in the implementation of protocol at various layers.

Supports Packet generation and reception

The Related Source File “~ns/agent.cc” “~ns/agent.h” “~ns/tcl/lib/ns-agent.tcl”

Page 22: Network Simulator - 2

Agent – Protocol Agents There are several agents supported in the NS-2

TCP TCP/Reno TCP/Fack TCP/Vegas/RBP TCP/Sack1 TCP/FullTcp TCPSink : “One-way” TCP Connection

TCP source sends data packets and the TCP sink sends ACK packets

UDP : A basic UDP agent SRM RTP RTCP LossMonitor

Page 23: Network Simulator - 2

Agent – Agent state

Agent state Information of Simulated packet To assign various fields before packet is sent

1) here_ : node address of myself ( source address in packets)2) dst_ : destination address3) size_ : packet size in bytes4) type_ : type of packet5) fid_ : the IP flow identifier (For IPv6)6) prio_ : the IP priority (For IPv6)7) flags_ : packet flags8) defttl_ : default IP ttl value

Page 24: Network Simulator - 2

Agent – Agent Class

class Agent : public Connector {

public:virtual ~Agent();virtual void attachApp(Application* app);inline nsaddr_t& addr() { return here_.addr_; }inline nsaddr_t& port() { return here_.port_; }

inline nsaddr_t& daddr() { return dst_.addr_; } inline nsaddr_t& dport() { return dst_.port_; } protected:

Packet* allocpkt() const; Packet* allocpkt(int) const; void initpkt(Packet*) const; ns_addr_t here_; // address of this agent ns_addr_t dst_; // destination address for pkt flow int size_; // fixed packet size packet_t type_; // type to place in packet header int fid_; // for IPv6 flow id field int prio_; // for IPv6 prio field int flags_; // for experiments (see ip.h) int defttl_; // default ttl for outgoing pkts

Application *app_; // ptr to application for callback

Access Function

Agent State

Application Pointer

Page 25: Network Simulator - 2

Agent – Agent Class Methods (1)

Packet* allocpkt (void) Packet* allocpkt (int n)

Parameter : void or n Create new packet and assign its fields Create new packet with a data payload of n bytes and assign its fields

Packet* Agent::allocpkt (){

Packet* p = Packet::alloc();initpkt(p);return p;

}Packet* Agent::allocpkt (int n){

Packet* p = allocpkt ();if (n > 0)

p -> allocdata (n);

return p;}

Adding data payload

Packet Initiate

Page 26: Network Simulator - 2

void initpkt (Packet* p) Parameter : Packet struct pointer Fill in all fields of a packet

void Agent::initpkt (Packet *p){

hdr_cmn* ch = hdr_cmn::access(p);ch->ptype() = type_;ch->size() = size_;ch->timestamp() = Scheduler::instance().clock();

hdr_ip* iph = hdr_ip::access(p);iph->saddr() = here_.addr_;iph->sport() = here_.port_;iph->daddr() = dst_.addr_;iph->dport() = dst_.port_;iph->flowid() = fid_;iph->prio() = prio_;iph->ttl() = defttl_;……..

}

Agent – Agent Class Methods (2)

Agent state setting

-Address- port number- IPv6 option

Packet information

Page 27: Network Simulator - 2

void attachAPP (Application *app) Parameter : Application pointer Associate Application with Agent

void Agent::attachApp (Application *app)

{

app_ = app;

}

Agent – Agent Class Methods (3)

Associate Application-Agent

Page 28: Network Simulator - 2

0

1

n0 n1

Addr Classifier

Port Classifier

entry_

0 Agent/TCP Addr Classifier

Port Classifier

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

dst_=1.0 dst_=0.0Application/FTP

Agent – Examples : TCP, TCP Sink

Page 29: Network Simulator - 2

Agent – Examples : TCP, TCP Sink Agent

1. Creating the Agent

Otcl code

set tcp [new Agent/TCP] # Create sender Agentset sink [new Agent/TCPSink] # Create receiver Agent

$ns attach-agent $n0 $tcp # Put sender on node 0$ns attach-agent $n1 $sink # Put receiver on node 3$ns connect $tcp $sink # Establish TCP connection

set ftp [new Application/FTP] # Create an FTP source “Application”$ftp attach-agent $tcp # Associate FTP with the TCP sender$ns at 1.5 “$ftp start” # Start at time 1.5

Page 30: Network Simulator - 2

Agent – Examples : TCP, TCP Sink Agent

2. Invokes the Constructor of The Agent & TcpAgent

Agent Constructor

Agent::Agent (int pkttype){

bind (“addr_”, (int *) &addr_);bind (“dst_”, (int *) &dst_);bind (“fid_”, (int *) &fid_);bind (“prio_”, (int *) &prio_);bind (“flags_”, (int *) &flags_);

}

TcpAgent Constructor

TcpAgent::TcpAgent() : Agent(){

bind (“windowOption_”, &wnd_option_); bind (“windowConstant_”, &wnd_const_);

……… }

BindingOtcl variable

with C++ variable (Agent state)

Page 31: Network Simulator - 2

Agent – Examples : TCP, TCP Sink Agent

3. Starting the Agent

Generating Packets

TcpAgent

void TcpAgent::output (int seqno, int reason){

Packet *p = allocpkt ();hdr_tcp *tcph = (hdr_tcp*) p->access (off_tcp_);…………Connector :: send (p, 0);

}

Generating Packets

Page 32: Network Simulator - 2

Agent – Examples : TCP, TCP Sink Agent

4. Processing Input

Receiving Packets

TcpSink Agent

void TcpSink::recv (Packet* pkt, Handler*){

hdr_tcp *th = (hdr_tcp *) pkt->access (off_tcp_);ack(pkt)packet::free(pkt);

}void TcpSink::ack (Packet *opkt){

Packet* npkt = allocpkt();hdr_tcp *otcp = (hdr_tcp *) opkt -> access (off_tcp_);hdr_tcp *ntcp = (hdr_tcp *) npkt-> access (off_tcp_);………………send (npkt, 0); /* Overrides the Agent::recv() methods

* invoke Connector::send() */}

Page 33: Network Simulator - 2

Agent – Summary

allocpkt ( int size )allocpkt ( int size )

alloc ()alloc ()

initpkt ( packet )

- destination Addr- destination port No.- source Addr- source port No.- ttl- pkt Size- pkt type- timestamp

initpkt ( packet )

- destination Addr- destination port No.- source Addr- source port No.- ttl- pkt Size- pkt type- timestamp

connector::send ()connector::send ()

Sender Agent class

To Low level

classifier::recv ( packet )

classifier::recv ( packet )

recv ( packet )recv ( packet )

Receiver Agent class

Page 34: Network Simulator - 2

TcpAgent - Basic TcpAgent

One-Way TCP Sender Agent Implementation of Basic TCP Tahoe version Base class of other version of TCP Agent

RenoTcpAgent NewRenoTcpAgent VegasTcpAgent Sack1TcpAgent

The related source file “ns/tcp/tcp.cc” “ns/tcp/tcp.h”

Page 35: Network Simulator - 2

TcpAgent – Header field of TCP struct hdr_tcp (in “ns/tcp.h” )

struct hdr_tcp {double ts; //packet generated timedouble ts_echo_; //echoed timestampint seqno; //sequence numberint reason; //reason for a retransmissionint sack_area_[NSA+1][2]; //selective ack blocksint sa_length_; //indicates the number of SACKs in this packetint ackno_; //ack numberint hlen_; //header lengthint tcp_flags_; //TCP flagsint last_rtt_; //more recent RTT measurement in msstatic int offset_; //offset for this header}

Page 36: Network Simulator - 2

TcpAgent - Characteristic Characteristic of TCP in TcpAgent (Tahoe version of TCP)

Sliding Window Several Timer

Retransmission Timer Measurement of RTT (Round Trip Time) Calculation of RTO (Retransmission TimeOut) Backoff Strategy (Karn’s Algorithm)

Delay Send Timer Burst Send Timer

Congestion Control Mechanism Slow Start Congestion Avoidance Fast Retransmit Fast Recovery (added to TCP Reno version)

Page 37: Network Simulator - 2

TcpAgent – Sliding Window (1) Sliding Window

Send all packets within window without waiting for an acknowledgement. Increases efficiency As acknowledgments for segments come in, the window is moved.

… …

Transmitter Receiver

Window Advertisement

Transmitter Window Size Value of

Window Advertisement

Free space in buffer to fill

increase bigger increase

decrease smaller decrease

Stop transmissions 0 full

Page 38: Network Simulator - 2

TcpAgent – Sliding Window (2) Related Methods

void TcpAgent::send_much(int force, int reason, int maxburst) {

int win = window(); //window sizeint npackets = 0;/* Save time when first packet was sent, for newreno --Allman */if (t_seqno_ == 0)

firstsent_ = Scheduler::instance().clock();//window size 만큼 패킷을 보냄 (output 함수 호출 )while (t_seqno_ <= highest_ack_ + win && t_seqno_ <maxseq_) {

if (overhead_ == 0 || force) {output(t_seqno_, reason);npackets++; t_seqno_ ++ ;

} else if (!(delsnd_timer_.status() == TIMER_PENDING)) {delsnd_timer_.resched(Random::uniform(overhead_));return;

}win = window(); //window size…..

}}

Page 39: Network Simulator - 2

TcpAgent – Retransmission Timer (1) Retransmission Timer

TCP use retransmission timer to ensure data delivery in the absence of any feedback from the remote data receiver.

The duration of this timer is referred to as RTO (Retransmission Timeout). To compute the current RTO, a TCP sender maintain two state variable,

SRTT (smoothed round-trip time) and RTTVAR (round-trip time variation).

Measurement of RTT RTT = (α * Old_RTT) + ((1 – α) * new_RTT_sample )

0 < α < 1 α close to 1 => no change in a short time α close to 0 => RTT changes too quickly

Calculation of RTO (Timeout) DIFF = sample – old_RTT Smoothed_RTT = old_RTT + d * DIFF DEV = old_DEV + p (|DIFF| - old_DEV) Timeout = Smoothed_RTT + g * DEV Continued next page =>

Page 40: Network Simulator - 2

TcpAgent – Retransmission Timer (2) Calculation of RTO (Timeout)

DEV estimated mean deviation d, a fraction between 0 and 1 to control how quickly the new sample affects

the weighted average p, a fraction between 0 and 1 to control how quickly the new sample affects

mean deviation g, a factor controls how much deviation affects round trip timeout Research suggests: d=1/8, p=1/4 and g=4

Karn’s Algorithm (Back-off Strategy) Definition : when computing the round trip estimate, ignore samples that

correspond to retransmitted segments, but use a back-off strategy, and retain the timeout value from a retransmitted packet for subsequent packets until a valid sample is obtained.

Timer Back-off Strategy New_timeout = γ* timeout (typically, γ= 2) Each time timer expires (retransmit happens), TCP increases timeout

value.

Page 41: Network Simulator - 2

TcpAgent – Retransmission Timer (3) Related Class & Methods//Retransmission Timerclass RtxTimer : public TimerHandler {public:

RtxTimer(TcpAgent *a) : TimerHandler() { a_ = a; }protected:

virtual void expire(Event *e);TcpAgent *a_;

};//Called when timeoutvoid RtxTimer::expire(Event*) {

a_->timeout(TCP_TIMER_RTX);} //Reset retransmission timervoid TcpAgent::reset_rtx_timer(int mild, int backoff){

if (backoff)rtt_backoff(); //using karn’s algorithm

set_rtx_timer();……rtt_active_ = 0;

}//Set retransmission timervoid TcpAgent::set_rtx_timer(){

rtx_timer_.resched(rtt_timeout());}

//Karn’s algorithmvoid TcpAgent::rtt_backoff(){

if (t_backoff_ < 64)t_backoff_ <<= 1;

if (t_backoff_ > 8) {t_rttvar_ += (t_srtt_ >> T_SRTT_BITS);t_srtt_ = 0;

}}double TcpAgent::rtt_timeout() //Calculate timeout value{

double timeout;if (rfc2988_) { //use updated RFC2988 timers

if (t_rtxcur_ < minrto_)timeout = minrto_ * t_backoff_;

elsetimeout = t_rtxcur_ * t_backoff_;

} else {timeout = t_rtxcur_ * t_backoff_;if (timeout < minrto_)

timeout = minrto_;}if (timeout > maxrto_)

timeout = maxrto_;…….return (timeout);

}

Page 42: Network Simulator - 2

TcpAgent – Retransmission Timer (4) Related Methods

// This fuction measures RTT and calculates SRTT, // RTTVAR and RTO, when every ACK is receivedvoid TcpAgent::rtt_update(double tao){

double now = Scheduler::instance().clock();if (ts_option_)

t_rtt_ = int(tao /tcp_tick_ + 0.5);else {

double sendtime = now - tao;sendtime += boot_time_;double tickoff = fmod(sendtime, tcp_tick_);t_rtt_ = int((tao + tickoff) / tcp_tick_);

}if (t_rtt_ < 1)

t_rtt_ = 1// Until here measurement of RTT

// Calculation of RTO using SRTT and RTTVARt_rtxcur_ = (((t_rttvar_ << (rttvar_exp_ + (T_SRTT_BITS - T_RTTVAR_BITS))) +

t_srtt_) >> T_SRTT_BITS ) * tcp_tick_;return;

}

if (t_srtt_ != 0) {

register short delta;// d = (m - a0)delta = t_rtt_ - (t_srtt_ >> T_SRTT_BITS);// a1 = 7/8 a0 + 1/8 m if ((t_srtt_ += delta) <= 0)

t_srtt_ = 1;if (delta < 0)

delta = -delta;delta -= (t_rttvar_ >> T_RTTVAR_BITS); // var1 = 3/4 var0 + 1/4 |d|if ((t_rttvar_ += delta) <= 0)t_rttvar_ = 1;

}else {

// srtt = rttt_srtt_ = t_rtt_ << T_SRTT_BITS;// rttvar = rtt / 2 t_rttvar_ = t_rtt_ << (T_RTTVAR_BITS-1);

}// Unitil here Calculation of Smoothed RTT and// RTT variance

omitted

Page 43: Network Simulator - 2

TcpAgent – Retransmission Timer (5) Related Methods

void TcpAgent::timeout(int tno){

// retransmit timerif (tno == TCP_TIMER_RTX) {

if (cwnd_ < 1) cwnd_ = 1;recover_ = curseq_;…….if (highest_ack_ == maxseq_ && restart_bugfix_) //if there is no outstanding data, don't cut //down ssthresh_.

slowdown(CLOSE_CWND_ONE); //when connection is idleelse {

// timeout occur by congestion++nrexmit_;last_cwnd_action_ = CWND_ACTION_TIMEOUT;slowdown(CLOSE_SSTHRESH_HALF|CLOSE_CWND_RESTART);

}reset_rtx_timer(0,1);last_cwnd_action_ = CWND_ACTION_TIMEOUT;send_much(0, TCP_REASON_TIMEOUT, maxburst_);

} }

Page 44: Network Simulator - 2

TcpAgent – Retransmission Timer (6) Overview

RtxTimer::expire()RtxTimer::expire()

timeout(TCP_TIMER_RTX)timeout(TCP_TIMER_RTX)

reset_rtx_timer()reset_rtx_timer()

rtt_backoff()rtt_backoff() set_rtx_timer()set_rtx_timer()

timeout = rtt_timeout()timeout = rtt_timeout()

RtxTimer::resched(timeout)RtxTimer::resched(timeout)

slowdown()slowdown() rtt_update()rtt_update()

newack()newack()

(1)

(2)

(1) (2)

(1)

(2)

Page 45: Network Simulator - 2

TcpAgent – Slow Start (1) Slow Start

It operates by observing that the rate at which new packets should be injected into the network is the rate at which the acknowledgments are returned by the other end.

Slow Start adds another window to the sender’s TCP : the congestion window (cwnd).

The congestion window is initialized the one segment. Each time an ACK is received, the congestion window is increased by one

segment. If congestion window value over the ssthresh(slow start thresh) value, then

switch to Congestion Avoidance mode.

Page 46: Network Simulator - 2

TcpAgent – Slow Start (2) Related Methods

// Return current window sizeint TcpAgent::window(){

return (cwnd_ < wnd_ ? (int)cwnd_ : (int)wnd_);}// set initial window size “1”void TcpAgent::set_initial_window(){

if (syn_ && delay_growth_)cwnd_ = 1.0;

elsecwnd_ = initial_window();

}// This fuction called every ACK is received and increase congestion window sizevoid TcpAgent::opencwnd(){

double increment;if (cwnd_ < ssthresh_) {

// slow-start (exponential)cwnd_ += 1;

} ……

}

Page 47: Network Simulator - 2

TcpAgent – Congestion Avoidance (1) Congestion Avoidance

Congestion avoidance is a way to deal with lost packets. There are two indications of packet loss : a timeout occurring and the

receipt of duplicate ACKs. When congestion occurs TCP must slow down its transmission rate of

packets into network, and then invoke slow start to get things going again. Algorithm operates as follows

1. Initialization for a given connection sets cwnd to one segment and ssthresh to 65535 bytes.

2. The TCP output routine never sends more than the minimum of cwnd and the receiver’s advertised window.

3. When congestion occurs (indicated by a timeout or the reception of duplicate ACKs), one-half of the current window size (the minimum of cwnd and the receiver’s advertised window) is saved in ssthresh. Additionally, if the congestion is indicated by a timeout, cwnd is set to one segment

4. When new data is acknowledged by the other end, increase cwnd, but the way it increases depends on whether TCP is performing slow start or congestion avoidance. If TCP is in slow start, then cwnd is increased by one segment every time an ACK is received. Otherwise if TCP is in congestion avoidance, then cwnd be increased by segsize * segsize/cwnd each time an ACK is received.

Page 48: Network Simulator - 2

TcpAgent – Congestion Avoidance (2) Congestion Avoidance

Page 49: Network Simulator - 2

TcpAgent – Congestion Avoidance (3) Related Methodsvoid TcpAgent::opencwnd(){

double increment;if (cwnd_ < ssthresh_){

// slow-start (exponential)cwnd_ += 1;

}else {

// lineardouble f;switch (wnd_option_) { …… case 1: // This is the standard algorithm. increment = increase_num_ / cwnd_; …… cwnd_ += increment; break;}

}return;

}

// This fuction is called when timeout occurvoid TcpAgent::slowdown(int how){

double decrease; double win, halfwin, decreasewin;int slowstart = 0;++ncwndcuts_; // we are in slowstart for sure if cwnd < ssthreshif (cwnd_ < ssthresh_)

slowstart = 1;……if (how & CLOSE_SSTHRESH_HALF)

// For the first decrease, decrease by halfif (first_decrease_ == 1 || slowstart ||last_cwnd_action_ == CWND_ACTION_TIMEOUT) {

ssthresh_ = (int) halfwin;} else

ssthresh_ = (int) decreasewin;……if (how & CLOSE_CWND_ONE)

cwnd_ = 1;if (ssthresh_ < 2)

ssthresh_ = 2;……

}

Page 50: Network Simulator - 2

TcpAgent – Fast Retransmit (1) Fast Retransmit

When TCP received duplicate ACKs, TCP does not know whether a duplicate ACK is caused by a lost segment or just reordering of the segments.

It waits for a small number of duplicate ACKs to be received. It is assumed that if there is just a reordering of the segments, there will be

only one or two duplicate ACKs before the reordered segment is processed, which will then generate a new ACK.

If three or more duplicate ACKs are received in row, it is strong indication that a segment has been lost. TCP then performs a retransmission timer to expire.

Page 51: Network Simulator - 2

TcpAgent – Fast Retransmit (2) Related Methods

void TcpAgent::recv(Packet *pkt, Handler*){

……else if (tcph->seqno() == last_ack_) { …… if (++dupacks_ == numdupacks_ &&

!noFastRetrans_) {

dupack_action(); } }

……send_much(0, 0, maxburst_);

}

void TcpAgent::dupack_action(){

int recovered = (highest_ack_ > recover_);if (recovered || (!bug_fix_ && !ecn_)) {

goto tahoe_action;}……

tahoe_action:recover_ = maxseq_;last_cwnd_action_ = CWND_ACTION_DUPACK;slowdown(CLOSE_SSTHRESH_HALF|CLOSE_CWND_ONE);reset_rtx_timer(0,0);return;

}

Page 52: Network Simulator - 2

TcpAgent – Overview Flow chart

send_much()send_much()

output()output()

recv()recv()

TcpSinkTcpSink

recv_newack_helper()recv_newack_helper()

opencwnd()opencwnd()newack()newack()newtimer()newtimer()

rtt_update()rtt_update()

Receiver

dupack_action()dupack_action()

finish()finish()

Data packetACK

(1.B) (1.A)(2)

(1) (2) (3)

(1)

(2)

Page 53: Network Simulator - 2

Next Presentation Source Analysis