RSS
 

ns2 Simulator On Ubuntu

04 Apr

ns‘ is a discrete event simulator targeted at networking research. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks. ns-2 is a packet-level simulator and essentially a centric discrete event scheduler to schedule the events such as packet and timer expiration. Centric event scheduler cannot accurately emulate “events handled at the same time” in real world, that is, events are handled one by one. However, this is not a serious problem in most network simulations, because the events here are often transitory. Beyond the event scheduler, ns-2 implements a variety of network components and protocols.

Setting it up

Setting up ‘ns’ in your Ubuntu system is really easy with the PPA given below :

sudo add-apt-repository ppa:wouterh/ppa
sudo apt-get update
sudo apt-get install ns2 nam xgraph

and we are almost there. Test Working of this simulator :-

nam

If a window showing ‘The Network Animator’ shows up, then we are good to go! For further details on the package, please see this link.

Time for Experiments

Just fire up anyone of the following and start working on them.

  • ns
  • nam
  • tclcl
  • otcl

For e.g. – First, we typed ‘ns’ and were presented with a prompt where all the programming oft he network is to be done which is about to be simulated. After that we type ‘nam’ in the terminal and the window will appear showing NAM (The Network Animator). This tool was primarily built for animated packet trace data. This trace is typically derived as the output of any simulator say ‘ns’ or from real network measurements e.g. using ‘tcpdump’.

ns2 Directory structure –>> The C++ classes of ns-2 network components or protocols are implemented in the subdirectory “ns-2”, and the TCL library (corresponding to configurations of these C++ instances) in the subdirectory of “tcl”.

‘nam’ –>> Type ‘nam’ in the terminal. This will open a window and click ‘new’. This will bring up the ‘nam’ UI where you are supposed to design everything. The design instructions are as follows :-

  • Create nodes using ‘Add Nodes’ tool.
  • Connect them using ‘Add Link’ tool.
  • Attach an ‘Agent’ to a node.
  • Connect agents by using Link tool.
  • Set options by double-clicking or right-clicking on each object.
  • Run the scenario using File menu command, run ‘ns’.

Take a look at the ‘nam’ editor and its components :

 

Working with ‘ns’ –>> Type ‘ns’ in the terminal to get started. Now follow the instructions to set up a TCL connection.  Save this file in any text editor as ‘test1.tcl’ being this the first tcl script of this tutorial.

#Create a simulator object
set ns [new Simulator]

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a ‘finish’ procedure
proc finish {} {
global ns nf
$ns flush-trace

#Close the trace file
close $nf

#Execute nam on the trace file
exec nam out.nam &
exit 0
}

# Insert your own code for topology creation and agent definitions, etc. here
#Call the finish procedure after 5 seconds simulation time

$ns at 5.0 “finish”

#Run the simulation
$ns run

done. You can run this script by typing in ‘ns test1.tcl’ but apparently we haven’t defined any objects (nodes, links, etc.) or events, so we are going to get an error message like ‘nam: empty trace file out.nam’. We are going to define the objects now :

In this section we are going to define a very simple topology with two nodes that are connected by a link. The following two lines define the two nodes. (Note: You have to insert the code in this section before the line ‘$ns run’, or even better, before the line ‘$ns at 5.0 “finish”‘).

set n0 [$ns node]
set n1 [$ns node]

A new node object is created with the command ‘$ns node’. The above code creates two nodes and assigns them to the handles ‘n0′ and ‘n1′. The next line connects the two nodes.

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue. Now you can save your file and start the script with ‘ns example1.tcl’. nam will be started automatically. Below you can see the output of the work we have done so far. Easy isnt it, but it will be understandable to you only when you are having knowledge of computer-networks, protocols and particularly the OSI model.

Lets test another one - This time we will be straight forward to the concept, this time we are going to ‘send data’ between two nodes. In ns, data is always being sent from one ‘agent’ to another. So the next step is to create an agent object that sends data from node n0, and another agent object that receives the data on node n1. Considering the above example, add agent ‘UDP’ and set other preferences as listed below :

# Insert your own code for topology creation and agent definitions, etc. here. Call the finish procedure after 5 seconds simulation time. Create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0

#Now the two agents have to be connected with each other.

$ns connect $udp0 $null0

$ns at 0.5 “$cbr0 start”
$ns at 4.5 “$cbr0 stop”

Add these lines carefully to the file before the line ‘$ns at 5.0 “finish” ‘ and save it. Run it again as test1.tcl and after a few seconds you will see a flow of data from one node to another. It looks really fascinating to see the first simulation working :)

Here, take a look :-

Thats it for now. You should test more things with nam & TCL scripts like packer monitoring etc. The ns2 documentation will make things more clear to you. The following links may also prove useful in understanding the concepts – ns-tut , wireless simulation , ns-by-example.

For any queries regarding Mint/Ubuntu can be asked here anytime.

Njoy Network Simulation !!!

 
Author :  
Geekier Than You....!!! A PC Addict Who Believes In Winning n Practicality. Free Tech Consultant | Blogger | Entrepreneur | FOSS Newbie | Critic | Visionary | Loading...

Tags: , , , , , , , , , , , , , , , , , ,

  • Ysangavi

    thanks a lot gaurav. it was very useful as i’m a beginner trying to work in ns2

  • Adnan_kuleta

    Dear! 
    I have problems with ns2.
    if i type ns on terminal nothing comes out, it stands for a while a ‘%’ on it.
    nam works perfect, but a iwat to test some Tcl files. can i do it on nam or only on ns??

  • Namita

    hello gaurav!!!
    i tried running tcl scripts by typing ns simple.tcl in the terminal.but an error as following is displayed:
    error while calling class oldsim:simple.tcl.
    m using ns-2.34 on ubuntu 10.04..
    plz help me

  • http://www.linoob.com Linoob.com’s Admin

    Here is something I found
    Looks like firstly you are entering into ns (typing ns & hitting enter) and then trying to run example2.tcl over there, DON’T DO THAT.

    instead direct the operation of cd installation path /……./ examples, and then run “ns example2.tcl” by the terminal itself…

    $ ns
    % ns examplename.tcl <~~~~ DON'T

    $ ns examplename.tcl <~~~~ DO

  • Dvk Chaitanya

    hello gaurav. Instead of downloading ns all in one package, I have installed ns,nam and xgraphs in ubuntu using ubuntu software center. My question is where do we need to place our C++ programs to run TCL scripts ?  Why Iam asking is after using software center to install, I dont have any ns all in one directory structure .. waiting for ur reply.. thanks.

  • Noopur Relan

    wats d meaning of “instead direct the operation of cd installation path /……./ examples,”????

  • sathy

    hi,how to import a new protocol definition in ns2??
    plz guide me

  • Cherif3132

    hello ! you can give me un exepmle for olsr et mpolsr ! 

  • guest

    hi there … i an using ns2.34 … all goes well but when i use xgraph command i get error problems found with input data but nam runs well additionally all trace files are emptty  … any suggestions

  • jain

    do we have to write the entire code using tcl in ubuntu?

  • jeeva

    hello gaurav i tried running tcl scripts by typing ns simple.tcl in the terminal.but an error as following is displayed:
    error while calling class oldsim:simple.tcl.
    m using ns-2.34 on ubuntu 10.10
    plz help me

  • K chhabra

    hello i have installed ns2 using software center of ubuntu 11.10 tcl scripts are working properly now how to change the c++ coding do hoew to access the ns2 directory for making changes in c++ coding