Skip to main content

Network Simulator 2 - Simple Example - NS2 Tutorial 2

Introduction to Network Simulator 2

In this post

  • Introduction to Network Simulator 2 and its architecture
  • Installation of Network Simulator 2 in various Operating Systems
  • How to simulate a given network with available libraries of NS2

The network simulator 2 (ns) addresses large number of protocols, applications, agents, queues, etc. These are called as “simulated objects” either in the C++ or OTCL. This book will let the users to code the ns Tcl scripts for wired and wireless networks. This book is intended for students, researchers who need better understanding of networks.

NS is a discrete event simulator developed towards the research and development in networking. NS supports various supports on wired, wireless networks, protocols, unicast/multicast networks, satellite networks. The recent versions of ns2 support latest wireless networks.

NS evolves in 1989 as REAL network simulator. Later in the year 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI.

While ns is favorable to researchers right through the inception it is still not a finished or a polished product. Still the bugs are being discovered and corrected by the researchers.  Sometimes the simulations may be invalidated by the bugs. Users are responsible for verifying themselves to correct these.

The main purpose of this book is to provide the readers the basic knowledge and architecture on ns2. This book provides with Tcl scripts for various networks that were available as modules within ns2. The best way to start learning NS2 is through the Ns Manual [1] provided by the http://www.isi.edu/nsnam/ns. However, this book will help you to simulate these networks that were available in ns2 software.

Installation of NS2

NS2 uses two languages OTCL and C++, in which OTCL provides the front end and C++ runs in the backend. For more details, please refer Chapter 3.

OTCL is

·       Interpreted language also called as interpreted hierarchy

·       OTcl is Object Oriented Tcl

·       Preferable for beginners to simply test the protocols, wired, wireless networks, etc

·       Interpretation is fast and execution is slow (so prefer to run in the front end)

C++

·       Compiled hierarchy

·       Preferable when dealing with packet, agent, application, protocol, routing, etc.

·       Compilation is slow, but execution is faster (so prefer to run in the backend)

TclCL is

·       Provides some classes for interfacing between C++ and OTcl

·       Consists of classes with methods Tcl and members to does the mapping between the other two languages.

The fig1.1 will describe the same.

 







NS2
NS2

Fig 1.1 – Architecture of NS2

As shown in the fig1.1, the input is the Tcl script and the output is to plot the graph or animate the network.

·       Develop the .tcl file.

·       Execute the file using ns filename.tcl

·       There are two output files

·       A file with nam as extension to show the animation

·       To execute the nam file, nam filename.nam

·       A file with .tr or .xg or any other as specified to plot the graph and to identify the performance characteristics

·       To see the graph, either to use xgraph or tracegraph or gnuplot

·       The trace file can also be processed using awk script by analyzing the text information from the file and predict the performance metrics of the networks

Directory Structure of NS2

This book deals with the latest ns package with version 2.35. The NS is a free tool that can be downloaded from [3]. It can be installed on various operating systems like Linux, Unix, Mac Systems, Windows, etc.  This book unless otherwise specified, uses Linux as the Operating System for using NS2.

NS comes with two different forms,

·       All in one package

·       The current version is ns-2.35

·       The various packages comes along with the allinone package

·       nam-1.15

·       ns-2.35

·       otcl-1.14

·       tcl-8.5.10

·       tk-8.5.10

·       tclcl-1.20

·       xgraph-12.2

·       All these packages comes in a single bundle and it contains a install script that compiles all the packages using the make utility and generate the binaries for ns, nam, xgraph, etc.

·       Component wise package suite

·       In this all the packages have to be compiled separately

NS2 has the directory structure with ns-allinone-2.35 is the top level and there are subsequent levels. The fig 1.2 shows the directory structure of ns-2.35

 

NS2 Directory Structure
NS2 Directory Structure

The ns-2.35 folder contains all the simulated modules in the compiled form and there are so many other folders within the ns-2.35. If anyone is working on the networks, then they can retain themselves within this ns-2.35 directory, because that contains almost everything related to the network.

Some other folders of use are

·       Ns-2.35 /

·       aodv – that contains the code of the routing algorithm AODV

·       dsdv – contains the code for the routing DSDV

·       mac – contains all the mac layer codes , Ex. 802.11, etc

·       queue – codes that contain all the queuing nodes

·       mobile – information about the mobile parameters of the node.

·       tcl – all the simulation modules in the interpreted hierarchy

·       lib – all the TCL simulation objects

·       ex – contains the tcl scripts for various networks

Installation of NS2 on a UNIX like platform

To install the ns2 software on a UNIX like platform, the install script has to be executed using the make utility

 

Shell> ./install

To install all the required software and to create the binaries.

Shell> ./validate

This is to validate the all the required functionalities that were installed during the ./install process.

Please refer, Appendix A for detailed installation instructions for most of the Operating systems.

Starting with NS2

Starting with ns, the user need to know lot many things. Listed are some important ideas and techniques before proceeding to program ns.

·       set tcp1 [new Agent/TCP]

·       In the above statement, [new Agent/TCP] creates a shadow object from the TcpClass Agent class.

·       When this statement is dealt with, there are many parameters to be noted.

·       Where is the actual agent (in which source file the agent was written).

·       TcpClass was written in C++ and the code is located at ~ns-2.35/tcp/tcp.cc

·       When new Agent/TCP is called in OTCL, the TcpClass runs in the backend and thus creates a shadow object which returns the TcpAgent.

·       There are many parameters executes when the agent is created. TcpAgent also has many parameters initialized and modified during the simulation of network. However, not all the parameters are relevant for the research when simulating the network.

·       All those parameters default values are located in the ~ns-2.35/tcl/lib/ns-default.tcl file. These values can be overwritten within the tcl script when the user is writing.

·       For example, Agent/TCP set numdupacks_ 3 (this is the line taken from the ns-default.tcl file and specifies that the number of duplicate acknowledgment is 3 and if the user wanted to change it to 5, the code has to be included in the tcl file like this

·       $tcp0 set numdupacks_ 5 (as tcp0 is the handler for the Agent/TCP)

·       There may be more number of C++ classes running in the background when the Tcl file is interpreting and the researchers are advised to go through the source code if they are programming a given network.

The following table tells the Agent name and their corresponding file location.

Agent/TCP

TcpClass

~ns-2.35/tcp/tcp.cc

Agent/TCP/Reno

RenoTcpClass

~ns-2.35/tcp/tcp-reno.cc

Agent/UDP

UdpAgentClass

~ns-2.35/apps/udp.cc

Table 1.1 – Otcl - C++ Class names

Let us go through a simple example of a wired network that exchange packets between the nodes.

Two wired Nodes
Two wired Nodes

Listing 1.1 – TCP with FTP Traffic between two nodes

#create a new simulator object ns

set ns [new Simulator]

# creation of two nodes n0 and n1

set n0 [$ns node]

set n1 [$ns node]

#Open the files for tracing in write mode, if the file already exits, it will be overwritten, else a #new file will be created

set tracefile [open out.tr w]

$ns trace-all $tracefile

#Open the files for network animation

set namfile [open out.nam w]

$ns namtrace-all $namfile

#Create a duplex link between nodes n0 and n1

$ns duplex-link $n0 $n1 2MB 10ms DropTail

#Creates TCP Agent and attach it to node 0

set tcp0 [new Agent/TCP]

$ns attach-agent $n0 $tcp0

#creates TCP Sink Agent and attach that agent to node 1

set tcpsink0 [new Agent/TCPSink]

$ns attach-agent $n1 $tcpsink0

# Set the Traffic for FTP and attach it to TCP Agent

set ftp [new Application/FTP]

$ftp attach-agent $tcp0

#connect the source and the sink

$ns connect $tcp0 $tcpsink0

#Start the traffic at 1.0seconds.

$ns at 1.0 "$ftp start"

#call the finish procedure at 4.0 sec

$ns at 4.0 "finish"

#Procedure finish{}

proc finish {} {

            global ns tracefile namfile

            exec nam out.nam &;  #execute the animation within the procedure

            exit 0

}

#Run the simulation

$ns run

Conclusion

This chapter gives you the short glimpse of what is ns2 and how it is installed under various operating system. This chapter deals with the directory structure of ns2 with various other packages that are of important to Network simulator for its operation. Also this chapter shows the two languages used in ns2 OTcl and C++ and demonstrates a wired Tcl script with Network animation screenshot.

Comments

Popular posts from this blog

Installing ns-3.34 in Ubuntu 20.04

This post shows how to install ns 3.34 in Ubuntu 20.04 LTS Prerequisites: Fresh installation of Ubuntu Version 20.04 LTS  ns3.34 can be downloaded from here Follow the video link for complete step by step instructions on the installation.  This version fixes the compilation issues of vanet-routing-compare.cc (bug in ns3.33)  Issue the following commands after opening a terminal  $ sudo apt update $ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev Unzip or untar the ns-allinone-3.34.tar.bz2 in the home folder (in my case its /home/pradeepkumar) $ cd ns-allinone-3.34/ $ ./build.py --enable-examples --enable-tests  Once the installation is completed, you may get an output show

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb http://in.archive.ubuntu.com/ubuntu bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/Makefile.in Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/Makefile.in ~xgraph-12.2/Makefile.in ~otcl-1.14/Makefile.in Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi

Installation of ns3 in Windows 10 and Windows 11 OS using WSL (Windows Subsystem for Linux)

This post shows how to install ns-3.33 in Windows 10 through WSL (Windows Subsystem for Linux) This posts works for Windows 11 also (I have tested it on a Windows 11 ISO and it works the Same way as mentioned in the following post.) This post will work for ns-3.3x version. Prerequisites : Install Windows Subsystem for Linux with GUI: Please refer the following video  System Information: OS used: Windows 10 and WSL (Ubuntu 20.04) GUI: XServer for Windows NS3 Version: ns-3.33 See the following complete video on how to install ns3 in Windows 10 Step 0 : Open XLaunch Step 1 :  Open WSL using PowerShell and open it as Administrator Command:/  wsl $ xfce4-session The GUI of Ubuntu Opens within Windows 10 OS. Step 2 : Download ns3 from nsnam.org website through Mozilla Firefox browser Step 3: Open a Terminal  $ sudo apt update $ sudo apt install build-essential autoconf automake libxmu-dev python3-pygraphviz cvs mercurial bzr git cmake p7zip-full python3-matplotlib python-tk python3-dev qt5-q