Advertisment

Simulate a Network Using NS-2

author-image
PCQ Bureau
New Update


Advertisment

- Arijit Roy & Pushpendu Kar, IIT Kharagpur

Network Simulator Ver 2 or NS-2 is a popular, flexible, fast, accurate, free, and open source discrete-event and object oriented network simulator. It's mainly used for research and education, but could also be used by network designers before they create their network architectures. Work on NS-2 began in 1989, and in 1995 it was supported by the Defense Advanced Research Projects Agency (DARPA), USA. The latest version of NS-2, NS-2.35 was released on Nov 4, 2011 under open source licensing based on GNU GPL compatibility. NS-2 can run on several kinds of UNIX (FreeBSD, Linux, SunOS, and Solaris). It's written in C++ and OTcl (an object oriented extension of Tcl). C++ is used to write the core of NS-2 and OTcl is used to write the simulation script. The simulation software is well documented with tutorials and manuals.

Advertisment

Snapshot

Price: Free (Open Source, GNU GPL License)

Applies to: Network Researchers



USP:Simulation of Protocols and Functionality for a variety of Networks.

Primary Links: http://www.isi.edu/nsnam/ns/


Search engine keywords: NS-2

Getting Started

To start using NS-2, it should be downloaded from the primary link http://www.isi.edu/nsnam/ns/, installed and tested on a computer. Basic installation of NS-2 requires about 320 MB disk space.

Advertisment



Installation

The installation can be done by any one of the two ways described below:



A. By all-in-one Installation

Advertisment

For quick installation, ns-allinone package can be used as it contains all components required for NS-2. Download copy of ns-allinone- .tr.gz in a directory, extract ns-allinone-.tr.gz and then go to the directory by using the command cd ns-allinone-. For installation type the command ./install. Installation will take a few minutes to complete.



B. By individual Installation

For individual installation download the following component: Tcl, Tk, OTcl, Tclcl, Ns-2, and Nam. These components can be downloaded from the primary link. Make a new folder named as â??ns2â??. Extract all the downloaded components into this folder. For extracting the above component individually the following command can be used:

Advertisment

tar -xzvf

After extracting all components, they should be installed one by one as follows.



a. Tcl

Advertisment

Go to the directory tcl-/unix and install using following commands:

1. cd tcl/unix

2. ./configure

3. Make

4. sudo make install



b. Tk

Advertisment

Go to the directory tk-/unix and install it by using following commands:

1. cd tk

2. cd unix

3. ./configure

4. make

5. sudo make install





c. OTcl

Go to directory otcl- and at the time of configuration give the path of the location where Tcl has been installed. For installation of OTcl following commands should be used:

1. cd otcl-

2. ./configure --with-tcl=../tcl

3. make

4. sudo make install



d. Tclcl

Go to directory tclcl- and during configuration give the path of the location where Tcl has been installed. For installation of Tclcl following commands should be used:

1. cd tclcl-

2. ./configure --with-tcl=../tcl 3. make

4. sudo make install



e. Ns

This is the main component of the network simulator and for installing this go to the directory ns- and install it by using following commands:

1 .cd ns-

2. ./configure --with-tcl=../tcl

3. make

4. sudo make install



f. Nam

This is the component used for visual representation of simulation. Go to directory nam- and install it by using following commands:

1. cd nam-

2. ./configure --with-tcl=../tcl

3. make

4. sudo make install

During the installation process, some errors may arise. For successful installation these errors should be corrected with the solutions given on various websites. Type the command ns on your Linux terminal to know if the installation is successful or not. If it returns a percentage symbol (%), ns is installed successfully.



Writing the Simulation Script

For writing a basic simulation script, the following structure should be followed:

1. Create new simulator object

2. Turn on tracing

3. Create Network (Physical Layer)

4. Create link and queue (Datalink layer)

5. Define routing protocol

6. Create transport connection (Transport Layer)

7. Create Traffic (Application Layer)

8. If it is needed create C++ module for new protocol



Sample Script:

Let there be a small network of six nodes, n0, n1, n2, n3, n4, n5. In this scenario node n0 (TCP source) transmits packets to node n4 (TCP sink) through nodes n5 and n3. There is another traffic node n1 transmitting UDP packets to node n2 via node n5.

To simulate this scenario, one should write the script in a file named 'abc.tcl' using 'gedit' editor by typing the command gedit abc.tcl &, in Linux terminal. After writing the following script, save it by clicking on 'save' button in the tool bar or by using 'Ctrl + s' shortcut key.

# create new simulator, ns is an object and Simulator

is class


set ns

# defining the color for data flow, Color1 is â??greenâ?? for TCP packets and Color2 is â??blueâ?? for UDP packets.

$ns color 1 Green

$ns color 2 Red

# createing trace file, opening the trace file named â??out.tr in write mode

set tracefile

$ns trace-all $tracefile

# createing nam file, opening the trace file named â??out.nam in write mode

set namfile

$ns namtrace-all $namfile

# defining finish procedure, to close trace and nam file and execute nam file

proc finish {} {

global ns tracefile namfile

$ns flush-trace

close $tracefile

close $namfile

exec nam out.nam &

exit 0

}



# creating nodes

set n0 <$ns node>

set n1 <$ns node>

set n2 <$ns node>

set n3 <$ns node>

set n4 <$ns node>

set n5 <$ns node>

# creating links between the nodes and setting its properties. Here â??DropTailâ?? means the packets will be drop when queue is full

$ns duplex-link $n0 $n5 2Mb 20ms DropTail

$ns duplex-link $n1 $n5 2Mb 10ms DropTail

$ns duplex-link $n5 $n3 2Mb 20ms DropTail

$ns duplex-link $n5 $n2 2Mb 10ms DropTail

$ns duplex-link $n4 $n3 2Mb 20ms DropTail

# creating TCP Agent

set tcp

# attaching agent with node n0

$ns attach-agent $n0 $tcp

# creating Sink

set sink

# attaching agent with n4

$ns attach-agent $n4 $sink

# connecting TCP agent with sink

$ns connect $tcp $sink

# setting TCP flow id

$tcp set fid_ 1

# setting FTP application

set ftp

# attaching application with agent

$ftp attach-agent $tcp

# creating UDP Agent

set udp

# attaching agent with node n1

$ns attach-agent $n1 $udp

# creating Sink

set null

# attaching agent with n2

$ns attach-agent $n2 $null

# connecting UDP agent with sink

$ns connect $udp $null

# setting UDP flow id

$udp set fid_ 2

# setup CBR over UDP

set cbr

# attaching cbr application with udp

$cbr attach-agent $udp

# setting packet size

$cbr set packetSize_ 1000

#setting bit rate

$cbr set rate_ 0.01Mb

# setting random false means no noise

$cbr set random_ false

# scheduling the events

$ns at 0.5 “$cbr start”

$ns at 2.0 “$ftp start”

$ns at 7.0 “$ftp stop”

$ns at 7.5 “$cbr stop

$ns at 10.0 “finish”

$ns run

Now, go to the directory where this file is located and execute using following command

ns abc.tcl

Trace File Analysis

From the generated trace file of the above simulation two lines have been taken. Each field of the trace file is described as follows:



Trace file:

+ 0.5 1 5 cbr 1000 ------- 2 1.0 2.0 0 0

- 0.5 1 5 cbr 1000 ------- 2 1.0 2.0 0 0

Conclusion

To avoid the real installation of a network due to its cost and complexity during education and research, NS-2 plays a vital role to simulate real scenarios for developing and testing existing or new protocols. The simulations executed through this software are well accepted in the research community. As NS-2 is free and open source software, it is gaining popularity among researcher and academicians.

Advertisment