Date post: | 19-Dec-2015 |
Category: |
Documents |
Upload: | lionel-bond |
View: | 227 times |
Download: | 0 times |
Network Simulator 2Tejas Vasavada
Contents
Simulation of wired networksSimulation of wireless networks • Traffic and movement-pattern files• Trace files• Analyzing trace files using 'awk' script• 'gnuplot' – utility to plot graphs
Contents (contd.)
DSR – A protocol for MANETs Source code of DSR in NS2 How to change DSR (with examples)
• Printing packet header• Configuring nodes to drop packets• Adding a new function in DSR • Calling previous function from Tcl• Passing arguments from Tcl to DSR• Configuring nodes to drop packets• Use of timers
Contents
How to add new package in NS Simulation of Wired-cum-Wireless Networks
Introduction
• Simulation comes before implementation.
• A newly designed protocol is simulated first to test the performance.
Simulation Tools
• OmNet++, NS2, Qualnet, Opnet, GlomoSim etc.
• NS2 - an open source tool, widely used in academics for research purpose.
How to use NS2
• Front end – Tcl (Tool Command Langauage)
• Back end – C++
• Today we will learn Tcl.
Wired Network
Tcl script
• set ns [new Simulator] creates an instance of ‘Simulator’ class.
• Output is stored in .nam files (Network AniMator)
set nf [open out.nam w]$ns namtrace-all $nf
Tcl script (contd.)
• proc finish {} {global ns nf$ns flush-traceclose $nfexec nam out.nam &exit 0}
‘finish’ will be called at the end of simulation.
Tcl Script (contd.)
• $ns at 5.0 "finish“finish() is called at 5th second. • $ns run Starts simulation.
Tcl script (contd.)
• set n0 [$ns node] set n1 [$ns node]Creates two nodes - n0 and n1.
• $ns duplex-link $n0 $n1 1Mb 10ms DropTailConnects both the nodes.
Tcl script (contd.)• #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 udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0
Tcl script (contd.)
• set null0 [new Agent/Null] $ns attach-agent $n1 $null0Creates a null agent and attaches it to n1.
• $ns connect $udp0 $null0Two agents are connected togother.
Tcl script (contd.)
• $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop“Node n0 will start sending packets at time0.5 sec and will stop at 4.5 sec.
Exercise 1
• Read example1.tcl provided to you.
• Run it using following commandns example1.tcl
• Read & Run Example2.tcl
Example2.tcl
• $ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
Creates star shape topology.
Example2.tcl (contd.)
Example2.tcl (contd.)
• Nodes n0 and n1 connect to n3 via n2.
• Bandwidth n2-n3 < (bandwidth n0-n2+
bandwidth n1-n2)• Node n2 drops packets.
Example2.tcl
• To see what is happening at n2, different flows should be coloured differently.
• $udp0 set class_ 1 $udp1 set class_ 2
• $ns color 1 Blue$ns color 2 Red
Example2.tcl (contd.)
Example3.tcl
• for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }
Creates 7 nodes.• for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail}
Example3.tcl (contd.)
Example3.tcl (contd.)
• Node n0 is connected to n3. Packets will pass through n1 and n2 (shortest path).
• Break n1-n2 link using following commands.$ns rtmodel-at 1.0 down $n(1) $n(2)$ns rtmodel-at 2.0 up $n(1) $n(2)
Example3.tcl (contd.)
Example3.tcl (contd.)
• Use following command to use alternate path.
$ns rtproto DV
Example3.tcl (contd.)
Wireless Network
Simple-wireless.tcl
• Components of a mobile node…Link Layer,Interface Queue between LL and Network
Layer,MAC LayerRouting Layer
• Parameters of a mobile nodeType of antenna,Radio propagation model
Simple-wireless.tcl (contd.)
set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagation modelset val(ant) Antenna/OmniAntenna ;# Antenna typeset val(ll) LL ;# Link layer typeset val(ifq) Queue/DropTail/PriQueue ;# Interface queue typeset val(ifqlen) 50 ;# max packet in ifqset val(netif) Phy/WirelessPhy ;# network interface typeset val(mac) Mac/802_11 ;# MAC typeset val(rp) DSDV ;# ad-hoc routing protocolset val(nn) 2 ;# number of mobilenodes
Simple-wireless.tcl (contd.)
• set ns_ [new Simulator]Creates new instance of ‘Simulator’.
• set tracefd [open simple.tr w]$ns_ trace-all $tracefd
Creates new instance of trace file.
Simple-wireless.tcl (contd.)
• set topo [new Topography]Creates new topology.
• $topo load_flatgrid 500 500Sets resolution of topology to 500 x 500.
Simple-wireless.tcl (contd.)• # 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
Simple-wireless.tcl (contd.)
• for {set i 0} {$i < $val(nn) } {incr i} {set node_($i) [$ns_ node ]$node_($i) random-motion 0 }
Random motion is disabled. We will provide initial position and movement parameters.
Simple-wireless.tcl (contd.)
• $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
Simple-wireless.tcl (contd.)
• # 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"
Simple-wireless.tcl (contd.)• # TCP connections between node_(0) and node_(1) set tcp [new Agent/TCP]
$tcp set class_ 2set sink [new Agent/TCPSink]$ns_ attach-agent $node_(0) $tcp$ns_ attach-agent $node_(1) $sink$ns_ connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns_ at 10.0 "$ftp start"
Simple-wireless.tcl (contd.)# Tell nodes when the simulation ends
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_ tracefdclose $tracefd}
Simple-wireless.tcl (contd.)
• puts "Starting Simulation..."$ns_ run
Prints the message and starts simulation.
Traffic-pattern fileand
movement-pattern file
Wireless-1.tcl
• set val(cp) “/root/cbr-3-test"set val(sc) “/root/scen-3-test“
cbr-3-test is connection-pattern file.scen-3-test is movement-pattern file.
Wireless-1.tcl (contd.)
• set val(x) 670 set val(y) 670
• set namtrace [open wireless1-out.nam w] $ns_ namtrace-all-wireless $namtrace $val(x)
$val(y)
Wireless-1.tcl (contd.)
• puts "Loading connection pattern..."source $val(cp)
Loads connection-pattern file.
• puts "Loading scenario file..."source $val(sc)
Loads movement-pattern(scenario) file.
Wireless-1.tcl (contd.)
• for {set i 0} {$i < $val(nn)} {incr i} {$ns_ initial_node_pos $node_($i) 20}
Defines size of node in nam
Traffic pattern file• ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed]
[-mc connections] [-rate rate] > [filenm]
Generates a traffic pattern. It is saved in file ‘filenm’.-type : type of connection cbr or tcp-nn : no. of nodes-seed : seed value used by random number generator-mc : no. of connections-rate : rate at with packets are sent
Traffic pattern file (contd.)
• ns cbrgen.tcl -type cbr -nn 10 -seed 1.0 -mc 8 -rate 4.0 > cbr-10-test
Generates traffic-pattern and saves in file cbr-10-test.
Create traffice-pattern file for tcp traffic and check how it differs from cbr-10-test
Movement-pattern file
• ‘setdest’ utility available at: ns-x.xx/indep-utils/cmu-scen-gen/setdest
• /setdest –v 1 –n <nodes> -p <pause time> -M <max speed> -t <sim time> -x <maxx> -y <maxy>
Movement-pattern file (contd.)
• ./setdest –v 2 –n <nodes> -s <speed type> -m <min speed> -M <max speed> -t <sim time> -P <pause type> -p <pause time> -x <maxx> -y <maxy>
Speed type: 1 - uniform speed 2 - normal speed
• Pause type: 1 - constant 2 – uniform [ 0, 2 x pause ]
For e.g../setdest –v 2 -n 50 -s 1 -m 1 -M 20 -t 200 -P 1 -p
0 -x 200 -y 200 > /root/Desktop/scen-3-test
Trace file format
Trace files
• Output of simulation is in trace format.• Trace file has two formats: old and new• To use new format add following line in tcl
script before calling trace-all .
$ns use-newtrace
Trace file format
• Event type s sendd dropr receivef forward
• Time -t
Trace file format (contd.)
• Node property tag-Ni Node id-Nx Node’s x-coordinate-Ny Node’s y-coordinate-Nz Node’s z-coordinate-Ne Node’s energy level-Nl Trace level (AGT, RTR, MAC)
Trace file format (contd.)
• -Is source address.source port number-Id dest address.dest port number-It packet type-Il packet size-If flow id-Ii unique id-Iv ttl value
Trace file format (contd.)
• Next hop information
-Hs id for this node-Hd id of next node towards destination
Awk script
Awk script
• ‘Awk’ is a scripting language used to process text files.
• Processes the file row by row.• Columns are identified like $1,$2 etc.• awk –f <.awk file> <.tr> file.• Use the awk file provided to calculate
throughput from tracefile.
Sample Script - dr.awk
• BEGIN– {– sent=0– recd=0– }
• {– sent=0– recd=0
dr.awk
– event=$1– level=$19– if(event == “s” && level == “AGT”)
• sent++– if(event == “r” && level == “AGT”)
• recd++• }
dr.awk
• END {
– printf("sent %g, recd %g, fwd %g, del. %g\n",sent,recd,fwd,recd/sent);
• }
GnuPlot
'gnuplot'
• Syntax:– gnuplot <file.gnu>
• Read dr.gnu file to understand use of 'gnuplot' to plot graphs.
Adding new Files in NS
• Find ns2_ricean_dist folder available with this presentation.
• Follow the procedure in “README” file.