You are on page 1of 11

Ex no

:
Date:
Simulation of TCP using NS-2
Aim:
To simulate a network consisting of TCP (Tahoe, Reno and new Reno) and to
find the congestion window size.
Description:
Flow control Algorithm:
In a connection between a client and a server, the client tells the server the
number of bytes it is willing to receive at one time from the server; this is the client's
receive window, which becomes the server's send window. Likewise, the server tells the
client how many bytes of data it is willing to take from the client at one time; this is the
server's receive window and the client's send window. Since the window size can be used
in this manner to manage the rate at which data flows between the devices at the ends of
the connection, it is the method by which TCP implements flow control, one of the
“classical” jobs of the transport layer. Flow control is vitally important to TCP, as it is the
method by which devices communicate their status to each other. By reducing or
increasing window size, the server and client each ensure that the other device sends data
just as fast as the recipient can deal with it.

Flow control is a technique whose primary purpose is to properly match the
transmission rate of sender to that of the receiver and the network. It is important for the
transmission to be at a high enough rate to ensure good performance, but also to protect
against overwhelming the network or receiving host. Congestion control is primarily
concerned with a sustained overload of network intermediate devices such as IP routers.

TCP uses the window field, briefly described previously, as the primary means for
flow control. During the data transfer phase, the window field is used to adjust the rate of
flow of the byte stream between communicating TCPs.

the window is increased to four segments. then sixteen segments and so on. the Slow Start algorithm initializes a congestion window to one segment. For example. the sender can transmit the minimum of the congestion window and the advertised window of the receiver. which is the maximum segment size (MSS) initialized by the receiver during the connection establishment phase. doubling from there on out up to the maximum window size advertised by the receiver or until congestion finally does occur. This is accomplished through the return rate of acknowledgements from the receiver. Congestion Control TCP congestion control and Internet traffic management issues in general is an active area of research and experimentation. the congestion window increases by one segment for each acknowledgement returned. Thus. the rate of acknowledgements returned by the receiver determine the rate at which the sender can transmit data. When a TCP connection first begins. a requirement for TCP software implementations is a mechanism used by the sender to control the transmission rate. otherwise known as sender-based flow control. This final section is a very brief summary of the standard congestion control algorithms widely used in TCP implementations today. In other words. the window "slides" as bytes in the stream are sent and acknowledged. the first successful transmission and acknowledgement of a TCP segment increases the window to two segments. which is simply called the transmission window. . Slow Start Slow Start. When acknowledgements are returned by the receiver. In this simple example. Moving from left to right. Then eight segments. there is a 4-byte sliding window.7 The size of the window and how fast to increase or decrease the window size is an area of great research. After successful transmission of these two segments and acknowledgements completes. Slow Start is actually not very slow when the network is not congested and network response time is good.

This halfway point was recorded earlier as the new transmission window. However. If this happens. In the Congestion Avoidance algorithm a retransmission timer expiring or the reception of duplicate ACKs can implicitly signal the sender that a network congestion situation is occurring. Congestion Avoidance is used to slow the transmission rate. as it will approach the point where congestion had previously been detected. However. The sender immediately sets its transmission window to one half of the current window size (the minimum of the congestion window and the receiver's advertised window size). Slow Start is only used up to the halfway point where congestion originally occurred. it is a strong indication that at least one segment has been lost. it should not be long before the receiver sends the latest expected acknowledgement. but to at least two segments. As data is received during Congestion Avoidance. the congestion window is reset to one segment. the sender does not know if it is because a TCP segment was lost or simply that a segment was delayed and received out of order at the receiver. Typically no more than one or two duplicate ACKs should be received when simple out of order conditions exist. If congestion was indicated by a timeout. If however more than two duplicate ACKs are received by the sender. Fast Retransmit When a duplicate ACK is received. the congestion window is increased. This mechanism will force the sender to more slowly grow its transmission rate. However. If the receiver can re-order segments.Congestion Avoidance During the initial data transfer phase of a TCP connection the Slow Start algorithm is used. Slow Start is used in conjunction with Congestion Avoidance as the means to get the data transfer going again so it doesn't slow down and stay slow. which automatically puts the sender into Slow Start mode. there may be a point during Slow Start that the network is forced to drop one or more packets due to overload or congestion. The TCP sender will assume enough time has lapsed for . the congestion window is increased by one segment for all segments in the transmission window that are acknowledged. After this halfway point.

the sender resumes transmission with a larger window.all segments to be properly re-ordered by the fact that the receiver had enough time to send three duplicate ACKs. where the current congestion window is .Rather than start at a window of one segment as in Slow Start mode. When three or more duplicate ACKs are received. incrementing as if in Congestion Avoidance mode. it reduces sets the congestion window to half the current window and resets ssthresh_ to match this value.inflated. Two applications are commonly used for TCP: FTP and Telnet. This is a strong indication that serious network congestion may not exist and that the lost segment was a rare event. TCP Agents: The TCP agent does not generate any application data on its own. the sender does not even wait for a retransmission timer to expire before retransmitting the segment (as indicated by the position of the duplicate ACK in the byte stream). except it also includes fast recovery. . the TCP sender has implicit knowledge that there is data still flowing to the receiver. This process is called the Fast Retransmit algorithm Immediately following Fast Retransmit is the Fast Recovery algorithm. The various types of TCP are: • Reno TCP: The Reno TCP agent is very similar to the Tahoe TCP agent. by the number of duplicate ACKs the TCP sender has received before receiving a new ACK. Instead. the simulation user can connect any trafc generation module to the TCP agent to generate data. Rather. The Reno TCP agent does not return to slow-start during a fast retransmit. So instead of reducing the flow of data abruptly by going all the way into Slow Start. Fast Recovery Since the Fast Retransmit algorithm is used when duplicate ACKs are being received. Why? The reason is because duplicate ACKs can only be generated when a segment is received. the sender only enters Congestion Avoidance mode.

Reno TCP with .TCP sink with congurable delay per ACK • Agent/TCPSink/Sack1 .Sack1 with DelAck The two-way experimental sender currently supports only a Reno form of TCP: • Agent/TCP/FullTcp • Note: Still under development Procedure: • create the simulator object • create the nodes and link the nodes together • setup the application and Transport agent • Connect the Transport agent of the sender with the Application agent • Connect the Agents to the node.a “Reno” TCP sender • Agent/TCP/Newreno .TCP with selective repeat (follows RFC2018) • Agent/TCP/Vegas . the sender must receive an ACK for the highest sequence number sent. In order to exit fast recovery. • Newreno TCP This agent is based on the Reno TCP agent. It was contributed by Ted Kuo. but which modifies the action taken when receiving new ACKS. Thus. new partial ACKs. The one-way TCP sending agents currently supported are: • Agent/TCP .selective ACK sink (follows RFC2018) • Agent/TCPSink/Sack1/DelAck .Reno with a modication • Agent/TCP/Sack1 . (those which represent new ACKs but do not represent an ACK for all outstanding data) do not deflate the window (and possibly lead to a stall. • Vegas TCP This agent implements “Vegas” TCP. Similarly connect the Transport agent of the receiver with the Receiver node .forward acknowledgment. The one-way TCP receiving agents currently supported are: • Agent/TCPSink . characteristic of Reno).TCP sink with one ACK per packet • Agent/TCPSink/DelAck .TCP Vegas • Agent/TCP/Fack .a “tahoe” TCP sender • Agent/TCP/Reno .

005$ftp2 attach-agent $tcp2set sink2 [new Agent/TCPSink]$ns attach-agent $n7 $sink2$ns connect $tcp2 $sink2 #Setting color for the nodes$tcp0 set fid_ 1$tcp1 set fid_ 2$tcp2 set fid_ 3proc record {} { global tcp0 tcp1 tcp2 f0 f1 f2 set ns [Simulator instance] set time 0.1 "record"$ns at 0.tr w]set f1 [open tahoe1.tr tahoe2.005$ftp attach- agent $tcp1set sink [new Agent/TCPSink]$ns attach-agent $n5 $sink$ns connect $tcp1 $sink set tcp2 [new Agent/TCP]$ns attach-agent $n6 $tcp2set ftp2 [new Application/FTP]$ftp2 set packetSize_ 500$ftp2 set interval_ 0.9 "$ftp2 start"$ns at 4.tr w]$ns trace-all $nr set nf [open taho.5 "$ftp0 start"$ns at 1.005$ftp0 attach-agent $tcp0set sink0 [new Agent/TCPSink]$ns attach-agent $n4 $sink0$ns connect $tcp0 $sink0 #Connection of node1 with node4set tcp1 [new Agent/TCP]$ns attach-agent $n0 $tcp1set ftp [new Application/FTP]$ftp set packetSize_ 500$ftp set interval_ 0.tr -geometry 800 400 & exit 0 }set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]set n5 [$ns node]set n6 [$ns node]set n7 [$ns node]#Creation of Links:$ns duplex-link $n0 $n2 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n2 $n3 3Mb 10ms DropTail$ns duplex-link $n3 $n4 2Mb 10ms DropTail$ns duplex-link $n3 $n5 2Mb 10ms DropTail$ns duplex-link $n6 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n7 2Mb 10ms DropTail #Orientation of nodes:$ns duplex-link-op $n0 $n2 orient right-up$ns duplex-link-op $n2 $n3 orient middle$ns duplex-link-op $n1 $n2 orient right-down$ns duplex-link-op $n3 $n4 orient right-up$ns duplex-link-op $n3 $n5 orient right-down$ns duplex-link-op $n2 $n6 orient left$ns duplex-link-op $n3 $n7 orient right#Connection of node1 with node5set tcp0 [new Agent/TCP]$ns attach-agent $n1 $tcp0set ftp0 [new Application/FTP]$ftp0 set packetSize_ 500$ftp0 set interval_ 0.nam & exec xgraph tahoe0.4 "$ftp2 stop"$ns at 3.0 "finish"$ns run .tr w]set nr [open taho.01 set wnd0 [$tcp0 set cwnd_] set wnd1 [$tcp1 set cwnd_] set wnd2 [$tcp2 set cwnd_] set now [$ns now] puts $f0 "$now $wnd0" puts $f1 "$now $wnd1" puts $f2 "$now $wnd2" $tcp0 set bytes_ 0 $tcp1 set bytes_ 0 $tcp2 set bytes_ 0 $ns at [expr $now+$time] "record"}$ns color 1 Red$ns color 2 Green$ns color 3 Brown#Schedule events for the CBR and FTP agents$ns at 0.1 "$ftp stop"$ns at 4.4 "$ftp start"$ns at 1.nam w]$ns namtrace-all $nf proc finish { } { global ns f0 f1 nr nf f2 $ns flush-trace close $f0 close $f1 close $f2 close $nf exec nam taho.tr w]set f2 [open tahoe2. • Connect the Sender and the Receiver Agents • schedule the start of the simulation • schedule the end of the simulation • run Program: Case (I) Two Flows Start at 10.tr tahoe1.0 sec and Stops at 210.sec: Tahoe TCP: #Simulation of TCPset ns [new Simulator] set f0 [open tahoe0.5 "$ftp0 stop"$ns at 5.

005$ftp0 attach-agent $tcp0set sink0 [new Agent/TCPSink]$ns attach-agent $n4 $sink0$ns connect $tcp0 $sink0 #Connection of node1 with node4set tcp1 [new Agent/TCP/Reno]$ns attach-agent $n0 $tcp1set ftp [new Application/FTP]$ftp set packetSize_ 500$ftp set interval_ 0.005$ftp1 attach-agent $tcp2set sink1 [new Agent/TCPSink]$ns attach-agent $n7 $sink1$ns connect $tcp2 $sink1 Output: Simulation Output: .005$ftp1 attach-agent $tcp2set sink1 [new Agent/TCPSink]$ns attach-agent $n7 $sink1$ns connect $tcp2 $sink1 New Reno TCP: #Connection of node1 with node5set tcp0 [new Agent/TCP/Newreno]$ns attach-agent $n1 $tcp0set ftp0 [new Application/FTP]$ftp0 set packetSize_ 500$ftp0 set interval_ 0.005$ftp attach-agent $tcp1set sink [new Agent/TCPSink]$ns attach-agent $n5 $sink$ns connect $tcp1 $sink set tcp2 [new Agent/TCP/Reno]$ns attach-agent $n6 $tcp2set ftp1 [new Application/FTP]$ftp1 set packetSize_ 500$ftp1 set interval_ 0.005$ftp attach-agent $tcp1set sink [new Agent/TCPSink]$ns attach-agent $n5 $sink$ns connect $tcp1 $sink set tcp2 [new Agent/TCP/Vegas]$ns attach-agent $n6 $tcp2set ftp1 [new Application/FTP]$ftp1 set packetSize_ 500$ftp1 set interval_ 0.005$ftp0 attach-agent $tcp0set sink0 [new Agent/TCPSink]$ns attach-agent $n4 $sink0$ns connect $tcp0 $sink0 #Connection of node1 with node4set tcp1 [new Agent/TCP/Newreno]$ns attach-agent $n0 $tcp1set ftp [new Application/FTP]$ftp set packetSize_ 500$ftp set interval_ 0.005$ftp attach-agent $tcp1set sink [new Agent/TCPSink]$ns attach-agent $n5 $sink$ns connect $tcp1 $sink set tcp2 [new Agent/TCP/Newreno]$ns attach-agent $n6 $tcp2set ftp1 [new Application/FTP]$ftp1 set packetSize_ 500$ftp1 set interval_ 0.005$ftp0 attach-agent $tcp0set sink0 [new Agent/TCPSink]$ns attach-agent $n4 $sink0$ns connect $tcp0 $sink0 #Connection of node1 with node4set tcp1 [new Agent/TCP/Vegas]$ns attach-agent $n0 $tcp1set ftp [new Application/FTP]$ftp set packetSize_ 500$ftp set interval_ 0.Reno Tcp: #Connection of node1 with node5set tcp0 [new Agent/TCP/Reno]$ns attach-agent $n1 $tcp0set ftp0 [new Application/FTP]$ftp0 set packetSize_ 500$ftp0 set interval_ 0.005$ftp1 attach-agent $tcp2set sink1 [new Agent/TCPSink]$ns attach-agent $n7 $sink1$ns connect $tcp2 $sink1 Vegas TCP: #Connection of node1 with node5set tcp0 [new Agent/TCP/Vegas]$ns attach-agent $n1 $tcp0set ftp0 [new Application/FTP]$ftp0 set packetSize_ 500$ftp0 set interval_ 0.

X graph: Tahoe TCP: .

Reno TCP: New Reno TCP: .

Vegas TCP: .

.Result: Thus the program for simulating a TCP was written and the output was Verified.