NS2 Documentation

  • Upload
    reddygj

  • View
    275

  • Download
    1

Embed Size (px)

Citation preview

  • 8/13/2019 NS2 Documentation

    1/15

    NS2 Tutorial

    Here we are providing a 'providing a 'providing a 'providing a 'template' that

    we can use it for all tcl scripts. We will introduce basics of how to create event

    scheduler, create a simple pair of nodes and make communication between the two

    nodes. Then we will introduce a standard template for all tcl scripts with simple

    examples.

    In this page we are going to introduce the concepts of:

    Part of introduction to TCP/IP modelWhere can you write scripts?Basic ns2 programming stepsTemplateGolden rulesSimple examples

    CBR over UDP CBR over TCP FTP over TCP

    Part of Introduction to TCP/IPmodel

    Application Layer CBR,FTP,telnet..Transport layer TCP,UDPNetwork layer rtprotoData link Layer

    Transmission mode Simplex mode Duplex mode

    Physical layerWhere can you write Scripts?1. vi filename.tcl

    It will open page, there you can write tcl scripts.

    2. save filePress esc-> colon (shift + semicolon) ->wq (save and quit)

    It save the file

    3. To run tcl script

    ns filename.tcl

    http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#parthttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#parthttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#wherehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#wherehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basichttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basichttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#templatehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#templatehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#goldenhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#goldenhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#samplehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#samplehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp1http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp1http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp2http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp2http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp3http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp3http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp3http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp2http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#exp1http://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#samplehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#goldenhttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#templatehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#basichttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#wherehttp://csis.bits-pilani.ac.in/faculty/murali/resources/tutorials/ns2.htm#part
  • 8/13/2019 NS2 Documentation

    2/15

    Basically NS 2 programmingcontains the following steps.

    1.Create the event scheduler

    2.Turn on tracing

    3.Creating network

    a)Computing setup routing - rtproto

    b) Creating transport connection-Agents

    c)Creating traffic-Applications

    4. Monitoring

    a) Visualization using nam

    Every ns2 script starts with creating simulator objectset ns [new Simulator]

    How to create node

    set n0 [$ns node]

    set n1 [$ns node]

    Creating link

    $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.

  • 8/13/2019 NS2 Documentation

    3/15

    How to use Trace?

    We use simulator to see results. How is it achieved? Using traceTwo types of trace

    1. generic trace

    for use with xgraph, and other things

    2. nam trace

    for use with visualization

    # open trace file

    set tracefile [open out.tr w]

    $ns trace-all $tracefile

    #Open the nam trace file

    set nf [open out.nam w]

    $ns namtrace-all $nf

    Since we have started tracing, we should end also. For this we use finish

    procedure.

    #Define a 'finish' procedure

    proc finish {}

    {

    global ns tracefile nf

    $ns flush-trace

    close $nf

    close $tracefile # close tracefile

    exec nam out.nam & #Execute nam on the trace file

    exit 0

    }Finish procedure is forced to be called at the end with the line

    $ns at 5.0 finish

    Every tcl script must contain following statement

    $ns run

  • 8/13/2019 NS2 Documentation

    4/15

    UDP communication

    In UDP communication, data is flows from UDP agent to Null agent.

    #Create a UDP agent and attach it to node n0

    set udp0 [new Agent/UDP]

    $ns attach-agent $n0 $udp0

    # create a null agent which act as traffic sink and attach it to node n1

    set null0 [new Agent/Null]

    $ns attach-agent $n1 $null0

    # connect two agents with each other

    $ns connect $udp0 $null0

    TCP Communication

    In TCP communication, data is flows from TCP agent to TCPsink agent.

    # create Tcp agent and attach it to node no

    set tcp0 [new Agent/TCP]

    $ns attach-agent $n0 $tcp0

    # create a tcpsink agent which act as traffic sink and attach it to node n1

    set tcpsink0 [new Agent/TCPSink]

    $ns attach-agent $n1 $tcpsink0

    # connect two agents with each other

    $ns connect $tcp0 $tcpsink0

    Traffic generator

    For actual data to flow, we need traffic generators.They simulate some application

    traffic.

    Simple example using CBR

    # creating CBR agent

  • 8/13/2019 NS2 Documentation

    5/15

    set cbr0 [new Application/Traffic/CBR]

    # Attach the CBR agent to some udp/tcp agent

    $cbr0 attach-agent $udp0

    Scheduling the events

    Here at place major role.

    $ns at 1.0 $cbr0 start

    $ns at 5.0 finish"

    cbr0 will start at a time of 1.0 ms and whole process will stops at 5.0ms.we can also

    stop each and traffic generator. for example

    $ns at 4.0 $cbr0 stop

    Traffic generator cbr0 will stops at 4.0

    Simple Examples

    So far we are not talking about data flow. Here we will explain with CBR protocol.Example1:( CBR over UDP)

    set ns [new Simulator] Creatingns simulator objectset tracefile [open out.tr w]$ns trace-all $tracefile Open trace fileset nf [open out.nam w]

    $ns namtrace-all $nf Open the nam trace fileproc finish {}{

    global ns tracefile nf

    $ns flush-traceclose $nf

    close $tracefile

    exec nam out.nam &

    exit 0}

    'finish' procedure

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

    $ns simplex-link $n0 $n1 1Mb 10ms DropTailCreate your topology- set n0 nodes.

    - $ns duplex-links

  • 8/13/2019 NS2 Documentation

    6/15

    set udp0 [new Agent/UDP]

    $ns attach-agent $n0 $udp0

    set cbr[new Application/Traffic/CBR]$cbr attach-agent $udp0set null0 [new Agent/Null]

    $ns attach-agent $n1 $null0

    $ns connect $udp0 $null0

    Create your agents

    -transport layer and application layers

    $ns at 1.0 "$cbr start"

    $ns at 3.0 "finish"Scheduling Events

    - $ns at 1.0 startand at 3.0 finish

    $ns run starts the simulation.ResultBefore 1.0ms

  • 8/13/2019 NS2 Documentation

    7/15

    After 1.0ms

    Example 2:(CBR over TCP)

    set ns [new Simulator] Creatingns simulator objectset tracefile [open out.tr w]$ns trace-all $tracefile Open trace fileset nf [open out.nam w]

    $ns namtrace-all $nf Open the nam trace fileproc finish {}

    {

    global ns tracefile nf$ns flush-trace

    close $nfclose $tracefileexec nam out.nam &exit 0

    }

    'finish' procedure

  • 8/13/2019 NS2 Documentation

    8/15

    set n0 [$ns node]

    set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail

    Create your topology

    - set n0 nodes.- $ns duplex-links

    set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $tcp0set tcpsink0 [new Agent/TCPSink]$ns attach-agent $n1 $tcpsink0

    $ns connect $tcp0 $tcpsink0Create your agents-transport layer and application layers

    $ns at 1.0 "$cbr start"

    $ns at 3.0 "finish"Scheduling Events

    - $ns at 1.0 start

    and at 3.0 finish$ns run starts the simulation.

    Result:

    Before 1.0ms

  • 8/13/2019 NS2 Documentation

    9/15

  • 8/13/2019 NS2 Documentation

    10/15

    set n0 [$ns node]

    set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail

    Create your topology

    - set n0 nodes.- $ns duplex-links

    set tcp0 [new Agent/TCP]

    $ns attach-agent $n0 $tcp0set ftp0 [new Application/FTP]$ftp0 attach-agent $tcp0set tcpsink0 [new Agent/TCPSink]$ns attach-agent $n1 $tcpsink0

    $ns connect $tcp0 $tcpsink0

    Create your agents

    -transport layer and application layersstuff

    $ns at 1.0 "$ftp0 start"

    $ns at 3.0 "finish"Scheduling Events- $ns at 1.0 start

    and at 3.0 finish$ns run starts the simulation.Result:

    Here we are using FTP Application agent as a traffic generator instead of CBR. The

    difference is CBR traffic generator will produce constant bit rate where as FTP traffic

    generator produces maximum available bit rate.

    We are writing code

    set ftp0 [new Application/FTP]

    $ftp0 attach-agent $tcp0

    and Similarly for telnet also

    set telneto [new Application/TELNET]

    $ftp0 attach-agent $telnet0Instead of CBR

    set cbr0 [new Application/Traffic/CBR]

    $cbr0 attach-agent $tcp0

  • 8/13/2019 NS2 Documentation

    11/15

    Before 1.0ms

    After 1.0ms

  • 8/13/2019 NS2 Documentation

    12/15

    Example4: (Wireless Scenario with three movable nodes)

    set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagation modelset val(ant) Antenna/OmniAntenna ;# Antenna type

    set val(ll) LL ;# Link layer typeset val(ifq) Queue/DropTail/PriQueue ;# Interface queue typeset val(ifqlen) 50 ;# max packet in ifq

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

    set val(mac) Mac/802_11 ;# MAC typeset val(rp) DSDV ;# ad-hoc routing protocolset val(nn) 2 ;# number of mobilenodes

    #main part of the program and start by creating an instance of the simulator,

    set ns [new Simulator]$ns color 1 Blue

    $ns color 2 Red

    #Then setup trace support by opening file simple.tr and call the procedure trace-all {} asfollows:

    set tracefd [open simple.tr w]

    $ns trace-all $tracefd

    set nf [open out.nam w]

    $ns namtrace-all $nf

    #finish procedure

    proc finish { } {global ns tracefd nf

    $ns flush-traceclose $nf

    close $tracefd

    exec nam out.nam &exit 0}

    #next create a topology object that keeps track of movements of mobilenodes within thetopological boundary.

    #We had earlier mentioned that mobilenodes move within a topology of 500mX500m.#We provide the topography object with x and y co-ordinates of the boundary, (x=500,y=500) :

    set topo [new Topography]

    $topo load_flatgrid 500 500

  • 8/13/2019 NS2 Documentation

    13/15

    #Next we create the object God, as follows:#Currently, God object stores the total number of mobilenodes and a table of shortest

    number of hops required to reach from one node to#another.create-god $val(nn)

    # First, we need to configure nodes before we can create them.# Node configuration API may consist of defining the type of addressing (flat/hierarchical

    etc),

    # the type of adhoc routing protocol, Link Layer, MAC layer, IfQ etc. The configuration APIcan be defined as follows:

    # (parameter examples)# $ns_ node-config -addressingType flat or hierarchical or expanded

    # -adhocRouting DSDV or DSR or TORA# -llType LL

    # -macType Mac/802_11# -propType "Propagation/TwoRayGround"

    # -ifqType "Queue/DropTail/PriQueue"# -ifqLen 50# -phyType "Phy/WirelessPhy"

    # -antType "Antenna/OmniAntenna"# -channelType "Channel/WirelessChannel"

    # -topoInstance $topo# -energyModel "EnergyModel"

    # -initialEnergy (in Joules)# -rxPower (in W)

    # -txPower (in W)

    # -agentTrace ON or OFF# -routerTrace ON or OFF

    # -macTrace ON or OFF# -movementTrace ON or OFF

    # 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

  • 8/13/2019 NS2 Documentation

    14/15

    # we create the 2 mobilenodes as follows:

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

    }

    # Now that we have created mobilenodes, we need to give them a position to start with,

    # Provide initial (X,Y, for now Z=0) co-ordinates for node_(0) and node_(1)

    $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

    #Node0 has a starting position of (5,2) while Node1 starts off at location (390,385).

    #Next produce some node movements,

    # Node_(1) starts to move towards node_(0)$ns at 5.0 "$node_(1) setdest 25.0 20.0 15.0"

    $ns at 1.0 "$node_(0) setdest 20.0 18.0 1.0"

    # Node_(1) then starts to move away from node_(0)$ns at 10.0 "$node_(1) setdest 490.0 480.0 15.0"

    $ns at 5.0 "$node_(1) setdest 25.0 20.0 15.0"

    #means at time 50.0s, node1 starts to move towards the destination (x=25,y=20) at aspeed of 15m/s.

    #This API is used to change direction and speed of movement of the mobilenodes.

    #Next setup traffic flow between the two nodes as follows:# TCP connections between node_(0) and node_(1)

    set tcp [new Agent/TCP]$ns attach-agent $node_(0) $tcp

    set sink [new Agent/TCPSink]

    $ns attach-agent $node_(1) $sink

    $ns connect $tcp $sink

    set ftp [new Application/FTP]$ftp attach-agent $tcp#This sets up a TCP connection betwen the two nodes with a TCP source on node0.

    $tcp set class_ 2#'set class_' allows to differenciate multiple flows.

  • 8/13/2019 NS2 Documentation

    15/15

    #Associating a different class to different flows and then, a single color to#each class is useful to easily follow the different flows in nam.

    $ns at 1 "$ftp start"

    # Tell nodes when the simulation ends

    for {set i 0} {$i < $val(nn) } {incr i} {

    $ns at 15 "$node_($i) reset";}$ns at 20 "finish"

    #$ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_ halt"

    #puts "Starting Simulation..."

    $ns run