S-38.148

Ns2 simulation exercise

1.Introduction

2.Theoretical background

2.1.Overview of TCP’s congestion control

2.1.1.Slow start and congestion avoidance

2.1.2.Fast Retransmit

2.1.3.Fast Recovery

2.2.Modelling TCP’s performance

2.2.1.Simple model

2.2.2.Complex model

3.Ns2

3.1.Installing and using ns2

3.2.General description

3.3.Otcl basics

3.3.1.Assigning values to variables

3.3.2.Procedures

3.3.3.Files and lists

3.3.4.Calling subprocesses

3.4.Creating the topology

3.4.1.Nodes

3.4.2.Agents, applications and traffic sources

3.4.3.Traffic Sinks

3.4.4.Links

3.5.Tracing and monitoring

3.5.1.Traces

3.5.2.Monitors

3.6.Controlling the simulation

3.7.Modifying the C++ code

3.8.Simple ns2 example

4.Exercise

4.1.Task

4.2.Simulation parameters

4.3.Results

4.4.Handout requirements

1.Introduction

This exercise is about comparing the analytical models for TCP’s steady-state throughput with simulated results. The purpose is to make you familiar with TCP’s congestion control mechanisms and performance as well as the ns2 simulator. Notice that you are supposed to carry out this exercise independently, group work is not allowed.

2.Theoretical background

2.1.Overview of TCP’s congestion control

TCP implements a window based flow control mechanism, as explained in [APS99]. Roughly speaking, a window based protocol means that the so called current window size defines a strict upper bound on the amount of unacknowledged data that can be in transit between a given sender-receiver pair. Originally TCP’s flow control was governed simply by the maximum allowed window size advertised by the receiver and the policy that allowed the sender to send new packets only after receiving the acknowledgement for the previous packet.

After the occurrence of the so called congestion collapse in the Internet in the late 80’s it was realised, however, that special congestion control algorithms would be required to prevent the TCP senders from overrunning the resources of the network. In 1988, Tahoe TCP was released including three congestion control algorithms: slow start, congestion avoidance and fast retransmit. In 1990 Reno TCP, providing one more algorithm called fast recovery, was released.

Besides the receiver’s advertised window, awnd, TCP’s congestion control introduced two new variables for the connection: the congestion window, cwnd, and the slowstart threshold, ssthresh. The window size of the sender, w, was defined to be

w = min(cwnd, awnd),

instead of being equal to awnd. The congestion window can be thought of as being a counterpart to advertised window. Whereas awnd is used to prevent the sender from overrunning the resources of the receiver, the purpose of cwnd is to prevent the sender from sending more data than the network can accommodate in the current load conditions.

The idea is to modify cwnd adaptively to reflect the current load of the network. In practice, this is done through detection of lost packets. A packet loss can basically be detected either via a time-out mechanism or via duplicate ACKs.

Timeouts:

Associated with each packet is a timer. If it expires, timeout occurs, and the packet is retransmitted. The value of the timer, denoted by RTO, should ideally be of the order of an RTT. However, as the value of RTT is not known in practice, it is measured by the TCP connection by using, e.g, the so called Karn algorithm. In this exercise, you will also need to measure the value of RTO, explained later in chapter 3.7.

Duplicate ACKs:

If a packet has been lost, the receiver keeps sending acknowledgements but does not modify the sequence number field in the ACK packets. When the sender observes several ACKs acknowledging the same packet, it concludes that a packet has been lost.

2.1.1.Slow start and congestion avoidance

In slow start, when a connection is established, the value of cwnd is first set to 1 and after each received ACK the value is updated to

cwnd = cwnd + 1

implying doubling of cwnd for each RTT.

The exponential growth of cwnd continues until a packet loss is observed, causing the value of ssthresh to be updated to

ssthresh = cwnd/2.

After the packet loss, the connection starts from slow start again with cwnd = 1, and the window is increased exponentially until it equals ssthresh, the estimate for the available bandwidth in the network. At this point, the connection goes to congestion avoidance phase where the value of cwnd is increased less aggressively with the pattern

cwnd = cwnd + 1/cwnd,

implying linear instead of exponential growth. This linear increase will continue until a packet loss is detected.

2.1.2.Fast Retransmit

Duplicate ACKs that were mentioned to be one way of detecting lost packets, can also be caused by reordered packets. When receiving one duplicate ACK the sender can not yet know whether the packet has been lost or just gotten out of order but after receiving several duplicate ACKs it is reasonable to assume that a packet loss has occurred. The purpose of fast retransmit mechanism is to speed up the retransmission process by allowing the sender to retransmit a packet as soon as it has enough evidence that a packet has been lost. This means that instead of waiting for the retransmit timer to expire, the sender can retransmit a packet immediately after receiving three duplicate ACKs.

2.1.3.Fast Recovery

In Tahoe TCP the connection always goes to slow start after a packet loss. However, if the window size is large and packet losses are rare, it would be better for the connection to continue from the congestion avoidance phase, since it will take a while to increase the window size from 1 to ssthresh. The purpose of the fast recovery algorithm in Reno TCP is to achieve this behaviour.

In a connection with fast retransmit, the source can use the flow of duplicate ACKs to clock the transmission of packets. When a possibly lost packet is retransmitted, the values of ssthresh and cwnd will be set to

ssthresh = cwnd/2

and

cwnd = ssthresh

meaning that the connection will continue from the congestion avoidance phase and increases its window size linearly.

2.2.Modelling TCP’s performance

The traditional methods for examining the performance of TCP have been simulation, implementations and measurements. However, efforts have also been made to analytically characterize the throughput of TCP as a function of parameters such as packet drop rate and round trip time.

2.2.1.Simple model

The simple model presented in [F99] provides an upper bound on TCP’s average sending rate that applies to any conformant tcp. A conformant TCP is defined in [F99] as a TCP connection where the TCP sender adheres to the two essential components of TCP’s congestion control: First, whenever a packet drop occurs in a window of data, the TCP sender interpretes this as a signal of congestion and responds by cutting the congestion window at least in half. Second, in the congestion avoidance phase where there is currently no congestion, the TCP sender increases the congestion window by at most one packet per window of data. Thus, this behaviour corresponds to TCP Reno in the presence of only triple duplicate loss indications.

In [F99] a steady-state model is assumed. It is also assumed for the purpose of the analysis that a packet is dropped from a TCP connection if and only if the congestion window has increased to W packets. Because of the steady-state model the average packet drop rate, p, is assumed to be nonbursty.

The TCP sender follows the two components of TCP’s congestion control as mentioned above. When a packet is dropped, the congestion window is halved. After the drop, the TCP sender increases linearly its congestion window until the congestion window has reached its old value W and another packet drop occurs. The development of TCP’s congestion window under these assumptions is depicted in Figure 1.

Figure 1 Development of TCP's congestion window

If a TCP sender with packets of B bytes and a reasonably constant roundtrip time of R seconds is considered, it is clear that with the assumptions of the model the TCP sender transmits at least

(1)

packets per a dropped packet. Thus the packet drop rate p is bounded by

(2).

From (2), the upper bound for W is:

(3).

In the steady-state model the average congestion window is 0.75W over a single cycle. Thus the maximum sending rate for the TCP connection over this cycle in bytes is

(4).

Substituting the upper limit for W, we get

(5) ,

where B is the packet size, R is the round trip delay and p is the steady-state packet drop rate.

This model should give reasonably reliable results with small packet losses (< 2%), but with higher loss rates it can considerably overestimate TCP’s throughput. Also, the equations derived do not take into account the effect of retransmit timers. Basically, TCP can detect packet loss either by receiving “triple-duplicate” acknowledgements (four ACKs having the same sequence number), or via time-outs. In this model it is assumed that packet loss is observed solely by triple duplicate ACKs.

2.2.2.Complex model

A good model for predicting TCP throughput should capture both the time-out and “triple-duplicate” ACK loss indications and provide fairly accurate estimates also with higher packet losses. A more complex model presented in [PFTK98] takes time-outs into account and is applicable for broader range of loss rates. In [PFTK98] the following approximation of TCP’s throughput, B(p):

(6),

where Wmax is the receiver’s advertised window and thus the upper bound for the congestion window, RTT is the round trip time, p the loss indication rate, T0 TCP’s average retransmission time-out value and b the number of packets that are acknowledged by each ACK. In the denominator, the first term is due to triple-duplicate acks, and the second term models the timeouts. With larger loss rates the second term dominates.

3.Ns2

3.1.Installing and using ns2

Ns2 can be built and run both under Unix and Windows. Instructions on how to install ns2 on Windows can be found at: However, the installation may be smoother under Unix.

In this exercise you should use the faarao machines (xerxes, imhotep, etc…) in computer class Maari-a. In order to be able to use ns2, you first have to go through the following steps:

Preparations:

  • First give a command ‘mkscratch –l’ in your home directory. The command creates a scratch directory into your home directory, where it is possible to store temporarily large amounts of data.
  • Copy the package s-38.148_ns2.tar.gzip from the directory /p/edu/s-38.180/148 into your scratch directory. Then extract the files of the package by giving the commands:

gunzip s-38.148_ns2.tar.gzip

tar -xvf s-38.148_ns2.tar

  • In order to be able to compile the package, you have to modify your compiler settings. First create a directory ‘bin/ix86’ into your home directory (NOT the scratch directory). Then create two symbolic links with the commands:

ln –s /p/bin/g++-2.95.3 g++

ln –s /p/bin/gcc-2.95.3 gcc

  • Next, move to the ns-2.1b9a directory, which has been created into your scratch directory, and give a command ‘make’. If everything went well, you can now start using ns2. If some problems occurred during compiling, just give a command ‘make clean’ and try again.

Using ns2:

Before using ns2 you will have to do the following:

  • In the ns-2.1b9a directory there should be two files, ‘usens2.csh’ and ‘usens2.sh’. These files contain the required settings for environmental variables. In order to initialize these settings, give a command ‘source usens2.csh’ in this directory (assuming that you are using tcsh) each time you start an ns2 session in a shell.

Then, to run your simulation script “myscript.tcl”, just write:

ns myscript.tcl

Modifying ns2

If you have made some changes in the C++ code, run make in the ns-2.1b9a directory.

3.2.General description

Ns2 is an event driven, object oriented network simulator enabling the simulation of a variety of local and wide area networks. It implements different network protocols (TCP, UDP), traffic sources (FTP, web, CBR, Exponential on/off), queue management mechanisms (RED, DropTail), routing protocols (Dijkstra) etc. Ns2 is written in C++ and Otcl to separate the control and data path implementations. The simulator supports a class hierarchy in C++ (the compiled hierarchy) and a corresponding hierarchy within the Otcl interpreter (interpreted hierarchy).

The reason why ns2 uses two languages is that different tasks have different requirements: For example simulation of protocols requires efficient manipulation of bytes and packet headers making the run-time speed very important. On the other hand, in network studies where the aim is to vary some parameters and to quickly examine a number of scenarios the time to change the model and run it again is more important.

In ns2, C++ is used for detailed protocol implementation and in general for such cases where every packet of a flow has to be processed. For instance, if you want to implement a new queuing discipline, then C++ is the language of choice. Otcl, on the other hand, is suitable for configuration and setup. Otcl runs quite slowly, but it can be changed very quickly making the construction of simulations easier. In ns2, the compiled C++ objects can be made available to the Otcl interpreter. In this way, the ready-made C++ objects can be controlled from the OTcl level.

There are quite many understandable tutorials available for new ns-users. By going through, for example, the following tutorials should give you a rather good view of how to create simple simulation scenarios with ns2:

The next chapters will summarise and explain the key features of tcl and ns2, but in case you need more detailed information, the ns-manual and a class hierarchy by Antoine Clerget are worth reading:

Other useful ns2 related links, such as archives of ns2 mailing lists, can be found from ns2 homepage:

3.3.Otcl basics

This chapter introduces the syntax and the basic commands of the Otcl language used by ns2. It is important that you understand how Otcl works before moving to the chapters handling the creation of the actual simulation scenario.

3.3.1.Assigning values to variables

In tcl, values can be stored to variables and these values can be further used in commands:

set a 5

set b [expr $a/5]

In the first line, the variable a is assigned the value “5”. In the second line, the result of the command [expr $a/5], which equals 1, is then used as an argument to another command, which in turn assigns a value to the variable b. The “$” sign is used to obtain a value contained in a variable and square brackets are an indication of a command substitution.

3.3.2.Procedures

You can define new procedures with the proc command. The first argument to proc is the name of the procedure and the second argument contains the list of the argument names to that procedure. For instance a procedure that calculates the sum of two numbers can be defined as follows:

proc sum {a b} {

expr $a + $b

}

The next procedure calculates the factorial of a number:

proc factorial a {

if {$a <= 1} {

return 1

}

#here the procedure is called again

expr $x * [factorial [expr $x-1]]}

It is also possible to give an empty string as an argument list. However, in this case the variables that are used by the procedure have to be defined as global. For instance:

proc sum {} {

global a b

expr $a + $b

}

3.3.3.Files and lists

In tcl, a file can be opened for reading with the command:

set testfile [open test.dat r]

The first line of the file can be stored to a list with a command:

gets $testfile list

Now it is possible to obtain the elements of the list with commands (numbering of elements starts from 0) :

set first [lindex $list 0]

set second [lindex $list 1]

Similarly, a file can be written with a puts command:

set testfile [open test.dat w]

puts $testfile “testi”

3.3.4.Calling subprocesses

The command exec creates a subprocess and waits for it to complete. The use of exec is similar to giving a command line to a shell program. For instance, to remove a file:

exec rm $testfile

The exec command is particularly useful when one wants to call a tcl-script from within another tcl-script. For instance, in order to run the tcl-script example.tcl multiple times with the value of the parameter “test” ranging from 1 to 10, one can type the following lines to another tcl-script:

for {set ind 1} {$ind <= 10} {incr ind} {

set test $ind

exec ns example.tcl test

}

3.4.Creating the topology

To be able to run a simulation scenario, a network topology must first be created. In ns2, the topology consists of a collection of nodes and links.

Before the topology can be set up, a new simulator object must be created at the beginning of the script with the command:

set ns [new Simulator]

The simulator object has member functions that enable creating the nodes and the links, connecting agents etc. All these basic functions can be found from the class Simulator. When using functions belonging to this class, the command begins with “$ns”, since ns was defined to be a handle to the Simulator object.

3.4.1.Nodes

New node objects can be created with the command

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

The member function of the Simulator class, called “node” creates four nodes and assigns them to the handles n0, n1, n2 and n3. These handles can later be used when referring to the nodes. If the node is not a router but an end system, traffic agents (TCP, UDP etc.) and traffic sources (FTP, CBR etc.) must be set up, i.e, sources need to be attached to the agents and the agents to the nodes, respectively.

3.4.2.Agents, applications and traffic sources

The most common agents used in ns2 are UDP and TCP agents. In case of a TCP agent, several types are available. The most common agent types are:

  • Agent/TCP – a Tahoe TCP sender
  • Agent/TCP/Reno – a Reno TCP sender
  • Agent/TCP/Sack1 – TCP with selective acknowledgement

The most common applications and traffic sources provided by ns2 are: