Network Simulator-2

 
        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. Notably, the wireless extension, derived from CMU Monarch Project [2], has 2
Assumptions simplifying the physical world:
(1) Nodes do not move significantly over the length of time they transmit or receive a packet. This assumption holds only for mobile nodes of high-rate and low-speed. Consider a node with the sending rate of 10Kbps and moving speed of 10m/s, during its receiving a packet of 1500B, the node moves 12m. Thus, the surrounding can change significantly and cause reception failure.
(2) Node velocity is insignificant compared to the speed of light. In particular, none of the provided propagation models include Doppler effects, although they could.

Why two Languages?

        NS-2 uses two languages because simulator has two different kinds of things it needs to do. On one hand, detailed simulation of protocols requires a systems programming language which can efficiently manipulate bytes, packet headers, and implement algorithms that run over large data sets. For these tasks run-time speed is important and turn-around time (run simulation, find bug, fix bug, recompile, re-run) is less important.
On the other hand, a large part of network research involves slightly varying parameters or configurations, or quickly exploring a number of scenarios. In these cases, iteration time (change the model and re-run) is more important. Since configuration runs once (at the beginning of the simulation), run-time of this part of the task is less important.
  NS-2 meets both of these needs with two languages, C++ and OTcl. C++ is fast to run but slower to change, making it suitable for detailed protocol implementation. OTcl runs much slower but can be changed very quickly (and interactively), making it ideal for simulation configuration. NS-2 (via tclcl) provides glue to make objects and variables appear on both languages.
  
NS-2 Directory Structure:
         As shown in Figure 1, 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”.
Network Components:
       Network components are Node, Link, Queue, etc. Some of them are simple components, that is, they are created from the corresponding C++ classes; The other are compound components, that is, they are composed multiple simple C++ classes, e.g. Link are composed of Delay (emulating propagation delay) and Queue. In general, in ns-2, all network components are created, plugged and configured from TCL.

NS-2 ENVIRONMENT:

Simple Programming In NS-2:


Setting up Variables:
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(ant) Antenna/OmniAntenna ;# Antenna type
set val(ll) LL ;# Link layer type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ifqlen) 50 ;# max packet in ifq
set val(netif) Phy/WirelessPhy ;# network interface type 
set val(mac) Mac/802_11 ;# MAC type
set val(rp) DSDV ;# ad-hoc routing protocol
set val(nn) 2 ;# number of mobilenodes


#Instantiate simulator object
set ns_ [new Simulator]


#Setup Trace File
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd

#Setup Nam file
set nf [open simple.nam w]
$ns namtrace-all-wireless $nf 700 200


#Create Topography
set topo [new Topography]
$topo load_flatgrid 500 500

#Create Object God
create-god $val(nn)

 
Configuring Mobilenode

# Configure nodes
$ns_ node-config -adhocRouting $val(rp) \
-llType $val(ll) \ 
 
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace OFF

for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
$node_($i) random-motion 0 ;# disable random motion
}

 
#Configure Initial Position
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0

$node_(1) set Z_ 0.0





#Create Movement
# Node_(1) starts to move towards node_(0)
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0" 

Setup traffic flow

set tcp [new Agent/TCP]
$tcp set class_ 2 set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0)
$tcp $ns_ attach-agent $node_(1)
$sink $ns_ connect $tcp
$sink set ftp [new Application/FTP]
$ftp attach-agent $tcp $ns_ at 10.0 "$ftp start"


Set Stop Time and Start Simulation
#Set Simulation Stop Time
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\" ;
$ns_ halt" proc stop {} { global ns_ tracefd close $tracefd }

#Finally, Start The Simulation
puts "Starting Simulation..."
$ns_ run

Simulation Procedure in NS-2:

The file “simple.tcl” is under <NS Root>/ns-allinone2.33/ns 2.33/simple-wireless.tcl . cd into the directory and run in terminal as “./ns filename.tcl” to run the simulation. The simulation will generate a trace file named “filename.tr”. The simulation will also generate a animation file “filename.nam” .
In order to observe the performance of the network we need to run those two files.

Running .tr and .nam file: 

Run the .nam under the directory nam-1.33 . It will show the network structure and flow of signals.

Run the .tr file under the directory Xgraph-12.1. It will show the performance graph.


CSMA-CA Model Of 802.11 MAC Protocol

Here,
DIFS: Distributed Inter-Frame Space
SIFS: Short Inter-Frame Space
p { margin-bottom: 0.08in; }
802.11 CSMA (No Collision Detection):
Sender:
  1. If the channel is idle for DIFS seconds it transmit DATA packet.
  2. If the channel is busy then it Backs off
Receiver:
If DATA packet received it sends back ACK after SIFS seconds.
Data Transfer is as follows:
Here,
DIFS: Distributed Inter-Frame Space
SIFS: Short Inter-Frame Space
CSMA/CA (Collision Avoidance) - RTS/CTS Implementation:
In this CSMA/CA method a Sender transmits an RTS packet Receiver responds with a CTS packet. RTS stands for ‘Request To Send’ and CTS stands for ‘Clear To Send’.
Purpose of CTS packet:
1. It reserves channel for sender.
2. Notify other stations.
Data Transfer takes place is as follows:
     

Implementation Of RTS-CTS-DATA-ACK Using NS-2.33





Simulation Process:
Simulation is done on NS-2. NS-2 is the mostly used software for simulation of Wireless Network model. In this simulation process of RTS-CTS-DATA-ACK model RTS-CTS-DATA-ACK.nam is generated which shows the entire RTS-CTS-DATA-ACK process.

Program:
Mac/Simple set bandwidth_ 1Mb





set MESSAGE_PORT 42


set BROADCAST_ADDR -1


#set val(chan) Channel/WirelessChannel ; #Channel Type


set val(prop) Propagation/TwoRayGround ; # radio-propagation model


set val(netif) Phy/WirelessPhy ; # network interface type


set val(mac) Mac/802_11 ;# MAC type


#set val(mac) Mac ; # MAC type


#set val(mac) Mac/Simple


set val(ifq) Queue/DropTail/PriQueue ; # interface queue type


set val(ll) LL ; # link layer type


set val(ant) Antenna/OmniAntenna ; # antenna model


set val(ifqlen) 32768 ; # max packet in ifq


set val(rp) DumbAgent





set ns [new Simulator]





set f [open rts-cts-data-ack.tr w]


$ns trace-all $f


$ns eventtrace-all
set nf [open rts-cts-data-ack.nam w]


$ns namtrace-all-wireless $nf 700 200

# set up topography object
set topo [new Topography]

$topo load_flatgrid 700 200

$ns color 3 green;
$ns color 8 red;
$ns color 1 black;
$ns color 7 purple;
$ns color 6 tan;
$ns color 2 orange;

#
# Create God
#
create-god 3


#set mac0 [new Mac/802_11]

$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace OFF \
-macTrace ON \
-movementTrace OFF

for {set i 0} {$i < 3} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
}

$node_(0) color black
$node_(1) color black
$node_(2) color black


$node_(0) set X_ 200.0
$node_(0) set Y_ 30.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 330.0
$node_(1) set Y_ 150.0
$node_(1) set Z_ 0.0

$node_(2) set X_ 60.0
$node_(2) set Y_ 30.0
$node_(2) set Z_ 0.0

$ns at 0.6 "$node_(2) setdest 330.0 30.0 10000.0"
$ns at 1.1 "$node_(2) setdest 500.0 30.0 10000.0"

# Subclass Agent/MessagePassing to make it do flooding

Class Agent/MessagePassing/Flooding -superclass Agent/MessagePassing

Agent/MessagePassing/Flooding instproc recv {source sport size data} {
$self instvar messages_seen node_
global ns 1

# extract message ID from message
set message_id [lindex [split $data ":"] 0]
puts "\nNode [$node_ node-addr] got message $message_id\n"

if {[lsearch $messages_seen $message_id] == -1} {
lappend messages_seen $message_id
$ns trace-annotate "[$node_ node-addr] received {$data} from $source"
$ns trace-annotate "[$node_ node-addr] sending message $message_id"
$self sendto $size $data 1 $sport

 
} else {
$ns trace-annotate "[$node_ node-addr] received redundant message $message_id from $source"
}
}

Agent/MessagePassing/Flooding instproc send_message {size message_id data port} {
$self instvar messages_seen node_
global ns MESSAGE_PORT 1

lappend messages_seen $message_id
$ns trace-annotate "[$node_ node-addr] sending message $message_id"
$self sendto $size "$message_id:$data" 1 $port
}

 
# Attach a new Agent/MessagePassing/Flooding to each node on port $MESSAGE_PORT
for {set i 0} {$i < 3} {incr i} {
set a($i) [new Agent/MessagePassing/Flooding]
$node_($i) attach $a($i) $MESSAGE_PORT
$a($i) set messages_seen {}
}

$ns at 0.1 "$a(0) send_message 500 1 {first_message} $MESSAGE_PORT"
$ns at 0.1 "$a(2) send_message 500 2 {second_message} $MESSAGE_PORT"

$ns at 0.8 "$a(0) send_message 500 5 {fifth_message} $MESSAGE_PORT"
$ns at 0.8 "$a(2) send_message 500 6 {sixth_message} $MESSAGE_PORT"

$ns at 1.3 "$a(2) send_message 500 15 {fifteenth_message} $MESSAGE_PORT"
$ns at 1.3 "$a(0) send_message 500 16 {sixteenth_message} $MESSAGE_PORT"

for {set i 0} {$i < 3} {incr i} {
$ns initial_node_pos $node_($i) 30
$ns at 20.0 "$node_($i) reset";
}

$ns at 20.0 "finish"
$ns at 20.1 "puts \"NS EXITING...\"; $ns halt"


proc finish {} {
global ns f nf val
$ns flush-trace
close $f
close $nf

}

puts "Starting Simulation..."

$ns run

Simulation Result:

Sender sends RTS to Receiver:

   
Receiver sends back CTS to Sender:


 Sender sends DATA to Receiver:


 Receiver sends back ACK to Sender:


DATA WAITING during data transfer from another node: