My Ns2 Tutorial Final-Color

Embed Size (px)

Citation preview

  • 8/20/2019 My Ns2 Tutorial Final-Color

    1/79

    Meivazhi Salai Andavargal

     

    1

     

  • 8/20/2019 My Ns2 Tutorial Final-Color

    2/79

    The ns2 Tutorial

    Prepared by 

     

    2

     Assistant Professor & HOD

    Department of Electronics & Communication EngineeringSriram Engineering College, Perumalpattu – 602 024

  • 8/20/2019 My Ns2 Tutorial Final-Color

    3/79

    Procedure (default) to open x-desktop for ns2 simulation

    Select ‘Startà

    Cygwinà

    Cygwin bash shell’

    Type at cygwin bash shell ‘startx’ and press to open x-desktop

    Type at x bash shell ‘CD/usr/local/ns*’ and

    Press to move to ns2 installation folder

    3

  • 8/20/2019 My Ns2 Tutorial Final-Color

    4/79

    Procedure (default) to create and save your TCL simulation scripts

    Open ‘notepad’

    Write your TCL simulation scripts

    Select ‘Save as…’ from ‘File’ menu

    Select ‘All Files’ for ‘Save as type’ from pop-up dialog

    4

    Select ‘ANSI’ for ‘Encoding’

    Type a name for the script file with ‘.tcl’ extension

    Select ‘Save’

  • 8/20/2019 My Ns2 Tutorial Final-Color

    5/79

    Command to run a simulation scripts

    #Command to run a simulation script

    ns .tcl

    scriptfilename Name of the simulation script file

    5

  • 8/20/2019 My Ns2 Tutorial Final-Color

    6/79

    Steps to write a simulation script for ns2

    @Create a simulator object

    @ [Set nam or other tracing]

    @Add nodes@ [Set node parameters]

    @Add links

    @ [Set link parameters]

    @Add agents

    @ [Set agent parameters]

     

    6

     @ [Set connection parameters]

    @Add traffics

    @ [Set traffic parameters]

    @ [Insert errors, network dynamics, routing]

    @Set up event scheduling

    @ [Create control loops or procedures]@Run simulation

  • 8/20/2019 My Ns2 Tutorial Final-Color

    7/79

    Steps to write a simulation script for ns2 (cont’d)

    Please remember, in a TCL script,

    ´ Anything that follows the symbol ‘#’ in a line is a comment

    ´ Anything that is written in this color is a keyword

    ´ Anything that is written in this color is a user-defined pointer variable

    7

  • 8/20/2019 My Ns2 Tutorial Final-Color

    8/79

    Create a simulator object

    #Create a simulator object

    set nsptr [new Simulator]

    nsptr Pointer to simulator object

    8

  • 8/20/2019 My Ns2 Tutorial Final-Color

    9/79

    Related commands

    $nsptr version - Returns ns version

    $nsptr now - Returns present time

    $nsptr halt - Stop/pause simulation

    $nsptr cancel - Cancels event event

     

    9

    nsp r us - race – us es a race u ers n respec ve race es

    $nsptr after  – Schedule after a delay

    $nsptr is-started - Returns true if simulator has started to run

    nsptr Pointer to simulator object

  • 8/20/2019 My Ns2 Tutorial Final-Color

    10/79

    Set nam or other tracing

    #Set nam tracing

    #Open nam trace file

    set ntfptr [open ntfname.nam ]#Set trace type

    $nsptr $ntfptr

    ntfptr Pointer to nam trace file ntfname created

    ntfname Name of nam trace file

    10

    r Read Only

    w Read & Write

    namtrace-all Trace all traffic

    namtrace-queue $src $dst

    Trace traffic between src & dst

    nodes; hence declare only afterdeclaring link between src & dst

  • 8/20/2019 My Ns2 Tutorial Final-Color

    11/79

    Set nam or other tracing (cont’d)

    #Set other tracing

    #Open other trace file

    set tfptr [open tfname.tr ]#Set trace type

    $nsptr $tfptr

    tfptr Pointer to trace file tfname created

    tfname Name of trace file

    11

    r Read Only

    w Read & Write

    trace-all Trace all traffic

    trace-queue $src $dst

    Trace traffic between src & dst nodes;

    hence declare only after declaring linkbetween src & dst

  • 8/20/2019 My Ns2 Tutorial Final-Color

    12/79

    Add nodes

    #Add nodes

    set ndh [$nsptr node]

    ndh Handle to node created

    nsptr Pointer to simulator object

    12

  • 8/20/2019 My Ns2 Tutorial Final-Color

    13/79

    Set node parameters

    #Set node parameters

    $ndh

    color Color of node

    shape Shape of node

    “red”

    “blue” And more for color

    ndh Handle to node

    13

    green

    “circle”

    “box”

    “hexagon”

    And more for shape

  • 8/20/2019 My Ns2 Tutorial Final-Color

    14/79

    Set node parameters (cont’d)

    #Set node parameters

    #Add label from specified time

    $nsptr at “$ndh label \”\””

    Schedule time in secs

    Any text as label

    nsptr Pointer to simulator object

    ndh Handle to node

    14

  • 8/20/2019 My Ns2 Tutorial Final-Color

    15/79

    Add links

    #Add links

    $nsptr $fnd $snd

    simplex-link

    duplex-link

    Link type: simplex-link, unidirectional link;

    duplex-link, bidirectional link

    Link bandwidth in bps: add Mb for Mbps

    nsptr Pointer to simulator object

    fnd, snd Handles to pair of nodes to be linked

    15

    Link delay in secs: add ms for milli secs

    DropTail

    FQ 

    SFQ 

    DRR

    CBQ 

    RED

    Queue type: Drop Tail, Fair Queue,

    Stochastic Fair Queue, Deficit Round

    Robin, CBQ, Random Early Discard

  • 8/20/2019 My Ns2 Tutorial Final-Color

    16/79

    Related commands

    #Return instance variable head_ of link betweenfnd and snd

    [$nsptr link $fnd $snd] head

    #Return instance variable link_ of link between fnd and snd

    [$nsptr link $fnd $snd] link

    #Return instance variable queue_ of link between fnd and snd

    [$nsptr link $fnd $snd] queue

    16

    nsptr Pointer to simulator object

    fnd, snd Handles to link nodes

  • 8/20/2019 My Ns2 Tutorial Final-Color

    17/79

    Set link parameters

    #Set link parameters

    $nsptr -op $fnd $snd

    simplex-link

    duplex-link

    Link type: simplex-link, unidirectional link;

    duplex-link, bidirectional link

    color Color of link

    nsptr Pointer to simulator object

    fnd, snd Handles to link nodes

    17

    label Label for link

    queuePos Queue position

    “red”

    “blue”

    “green”

    And more for color

    “anytext” Any text as label

    Number for queuePos

  • 8/20/2019 My Ns2 Tutorial Final-Color

    18/79

    Set link parameters (cont’d)

    orient Setting node position for animation

    right

    Left

    up

    down

    right-up

    right-down

    Orient second node to right of first node

    Orient second node to left of first node

    Orient second node straight up first node

    Orient second node straight down first node

    Orient second node to right of first node and up

    Orient second node to right of first node and down

    18

    left-upleft-down

    up-right

    down-right

    up-left

    down-left

    Orient second node to left of first node and upOrient second node to left of first node and down

    Orient second node up first node and right

    Orient second node down first node and right

    Orient second node up first node and left

    Orient second node down first node and left

    Orient second node at deg_double to left of first node

  • 8/20/2019 My Ns2 Tutorial Final-Color

    19/79

    Set link parameters (cont’d)

    #Set queue size of link

    $nsptr queue-limit $fnd $snd

    Queue size in packets

    nsptr Pointer to simulator object

    fnd, snd Handles to link nodes

    19

  • 8/20/2019 My Ns2 Tutorial Final-Color

    20/79

    Set link parameters (cont’d)

    #Set queue parameters of link

    set qptr [[$nsptr link $fnd $snd] queue]

    $qptr set

    nsptr Pointer to simulator object

    fnd, snd Handles to link nodes

    qptr Pointer to queue of link between fnd & snd

    For all queue types

    20

    limit_

    blocked_

    unblock_on_resume_

    Queue size in packets

    Enabling/disabling queue block

    Enabling/disabling automatic queue unblock

    true, false

    true, false

    Number of packets

    If true, queue is blocked; unable to sent packet to link

    If true, queue is unblocked after last pkt is txed.

  • 8/20/2019 My Ns2 Tutorial Final-Color

    21/79

    Set link parameters (cont’d)

    secsPerByte_ Seconds per byte

    For Fair Queue (FQ)

    maxqueue_

    buckets_

    Maximum queue in packets

    Number of buckets used for hashing of flow

    For Stochastic Fair Queue (SFQ)

    21

     _

    buckets_

    blimit_

    quantum_

    mask_

    Number of buckets used for hashing of flow

    Shared buffer size in bytes

    Flow rate in bytes

    If true, flow consists of packets with same node id

    true, false

    For Deficit Round Robin (DRR)

  • 8/20/2019 My Ns2 Tutorial Final-Color

    22/79

    Set link parameters (cont’d)

    bytes_

    queue-in-bytes_thresh_

    maxthresh_

    mean_pktsize_

    q_weight_

    wait_

    linterm_

    If true, byte-mode RED is enabled

    If true, average queue size is measured in bytesMinimum threshold for average queue size in packets

    Maximum threshold for average queue size in packets

    Rough estimate of average packet size in bytes

    Queue weight, used for calculating average queue size

    If true, interval between dropped pkts is maintained

    Packet dropping probability varies bet 0 and (1/linterm)

    For Random Early Discard (RED)

    22

     _drop-tail_

    ,If true, drop-tail is used when queue overflows

    true, false

    true, false

    true, false

    true, false

    true, false

  • 8/20/2019 My Ns2 Tutorial Final-Color

    23/79

    Add agents

    #Add agents

    #Create desired agent

    set agptr [new Agent/]#Attach agent to desired node

    $nsptr attach-agent $ndh $agptr

    agptr Pointer to agent created

    nsptr Pointer to simulator object

    23

    TCP

    TCP/Reno

    TCP/Newreno

    TCP/Sack1

    TCP/Fack

    TCP senders

    n Han e to no e to e attac e wit agent

  • 8/20/2019 My Ns2 Tutorial Final-Color

    24/79

    Add agents (cont’d)

    TCP/FullTcp

    TCP/Vegas

    TCP/Vegas/RBPTCP/Reno/RBP

    TCP/Asym

    TCP/Reno/Asym

    TCP/Newreno/Asym

    TCP senders

    TCPSink

    24

    TCPSink/Asym

    TCPSink/Sack1

    TCPSink/Sack1/DelAck

    TCP receivers

    UDP UDP sender

    Null Null receiver that simply discards received packets

    RTP RTP sender/receiver

  • 8/20/2019 My Ns2 Tutorial Final-Color

    25/79

    Add agents (cont’d)

    LossMonitor Packet sink which checks for losses

    IVS/Source IVS senderIVS/Receiver IVS receiver

    CrtMcast/Encap Centralized multicast encapsulator

    CrtMcast/Decap Centralized multicast de-encapsulator

    Message Protocol to carry text messages

    Message/Prune Processes multicast prune messages

    25

    SRM SRM agent with non-adaptive timers

    SRM/Adaptive SRM agent with adaptive timers

    Tap Agent to interface simulator to live network

    rtProto/DV Distance vector routing protocol agent

  • 8/20/2019 My Ns2 Tutorial Final-Color

    26/79

    Add agents (cont’d)

    26

  • 8/20/2019 My Ns2 Tutorial Final-Color

    27/79

    Add agents (cont’d)

    27

  • 8/20/2019 My Ns2 Tutorial Final-Color

    28/79

    Add agents (cont’d)

    28

  • 8/20/2019 My Ns2 Tutorial Final-Color

    29/79

    Set agent parameters

    #Set agent parameters

    #For configurable parameters

    $agptr set #For read only parameters

    set pptr $agptr

    agptr Pointer to agent created

    pptr Pointer to parameter read

    < arameter> Confi uration arameters common for all a ents

    29

    fid_

    prio_

    agent_addr_

    agent_port_

    dst_addr_

    dst_port_

    An integer as flow ID

    An integer as priority

    An integer as address of this agent

    Port no of this agent in integer

    Destination address of this agent in integer

    Destination port of this agent in integer

     

    packetSize_ Packet size in bytes

    Configuration parameters common for all source agents

  • 8/20/2019 My Ns2 Tutorial Final-Color

    30/79

    Set agent parameters (cont’d)

    window_

    maxcwnd_windowInit_

    windowOption_

    maxburst_

    MSW

    Upper bound on advertised window for TCP connection in packets

    Upper bound on congestion window in packets-Set to 0 to ignoreInitial congestion window size in packets

    Algorithm to handle congestion window (0 to 8)

    Maximum no of packets to send in response to single ACK

    Maximum window size in packets for TCP-window_

  • 8/20/2019 My Ns2 Tutorial Final-Color

    31/79

    Set agent parameters (cont’d)

    nlost_

    npkts_bytes_

    lastPktTime_

    expected_

    Number of packets lost

    Number of packets receivedNumber of bytes received

    Time at which last packet was received

    Expected sequence number of the next packet

    State variables (read only) for only LossMonitor agent

    #Set flow color for agents

    $nsptr color

    31

    Flow ID of agent considered

    red

    blue

    green

    And more as flow color

    nsptr Pointer to simulator object

  • 8/20/2019 My Ns2 Tutorial Final-Color

    32/79

    Create connection

    #Create connection

    $nsptr connect $srcag $dstag

    nsptr Pointer to simulator object

    srcag Pointer to desired source agent

    dstag Pointer to desired destination agent

    32

  • 8/20/2019 My Ns2 Tutorial Final-Color

    33/79

    Add agents & Create connection-alternate

    #Create desired agent, attach it to desired src node, connect it to desired dst

    #node and returns ptr to src node createdset agptr [$nsptr create-connection $srcndh $dstndh

    ]

    nsptr Pointer to simulator object created

    Source agent

    Destination a ent

    33

    srcndh Pointer to source node

    dstndh Pointer to destination node

    agptr Pointer to source agent created

  • 8/20/2019 My Ns2 Tutorial Final-Color

    34/79

    Add agents & Create connection-alternate (cont’d)

    #Create desired agent, attach it to desired src node, connect it to desired dst

    #node and returns list of ptrs to src & dst nodes created

    set agptrl [$nsptr create-connection-list $srcndh $dstndh

    ]

    set sagptr [lindex $agptrl 0]

    set dagptr [lindex $agptrl 1]

    nsptr Pointer to simulator object

    34

    Source agent

    Destination agent

    srcndh Pointer to source node

    dstndh Pointer to destination node

    agptrl List of pointers to source and destination agents created

    sagptr Pointer to source agent created

    dagptr Pointer to destination agent created

  • 8/20/2019 My Ns2 Tutorial Final-Color

    35/79

    Add traffics

    #Add traffic

    #Create traffic

    set trptr [new Application/]#Attach it to desired agent

    $trptr attach-agent $agptr

    trptr Pointer to traffic created

    agptr Pointer to desired agent

    35

    FTP

    Telnet

    FTP traffic for TCP agent

    Telnet traffic for TCP agent

    Traffic/CBR

    Traffic/Exponential

    Traffic/Pareto

    CBR traffic for UDP agent

    Exponential traffic for UDP agent

    Pareto traffic for UDP agent

  • 8/20/2019 My Ns2 Tutorial Final-Color

    36/79

    Set traffic parameters

    #Set traffic parameters

    $trptr set

    packetSize_

    rate_

    Packet size in bytes

    Traffic rate in bps

    trptr Pointer to traffic created

    For all traffic

    36

    random_

    maxpkts_

    If true, random noise is introduced in traffic

    Maximum number of packets

    true, false

    For CBR traffic

  • 8/20/2019 My Ns2 Tutorial Final-Color

    37/79

    Set traffic parameters (cont’d)

    burst_time_

    idle_time_

    Length of burst (on-time) in secs

    Length of off-time in secs

    For Exponential traffic

    burst_time_ Length of burst (on-time) in secs

     

    For Pareto traffic

    37

     _ _shape_

      -Pareto shape parameter

  • 8/20/2019 My Ns2 Tutorial Final-Color

    38/79

    Set up event scheduling

    #Set up event scheduling

    $nsptr at “”

    nsptr Pointer to simulator object

    Schedule time in secs

    Any valid TCL command

    38

  • 8/20/2019 My Ns2 Tutorial Final-Color

    39/79

    Set up event scheduling (cont’d)

    #Selecting event scheduler before any event schedule

    $nsptr use-scheduler

    List

    Heap

    Calendar

    ReatTime

    Scheduler using a simple linked-list structure

    Scheduler using a heap structure

    Scheduler using a data structure analogous to year desk calendar

    Scheduler using real time

    nsptr Pointer to simulator object

    39

  • 8/20/2019 My Ns2 Tutorial Final-Color

    40/79

    Set up event scheduling (cont’d)

    #Some important event schedules

    #Start traffic trptr at time

    $nsptr at “$trptr start”#Stop traffic trptr at time

    $nsptr at “$trptr stop”

    nsptr Pointer to simulator object

    trptr Pointer to desired traffic

    40

    Schedule time in secs

  • 8/20/2019 My Ns2 Tutorial Final-Color

    41/79

    Set up event scheduling (cont’d)

    #Annotating

    $nsptr at “$nsptr trace-annotate \”\””

    #Set nam animation rate$nsptr at “$nsptr set-animation-rate ”

    #Finishing simulation

    $nsptr at “finish”

    nsptr Pointer to simulator object

    41

    inis User- e ine inis proce ure; Re er to inis proce ure

    Schedule time in secs

    Any text as annotation

    Animation rate in secs

  • 8/20/2019 My Ns2 Tutorial Final-Color

    42/79

    Run simulation

    #Run simulation

    $nsptr run

    nsptr Pointer to simulator object

    42

  • 8/20/2019 My Ns2 Tutorial Final-Color

    43/79

    Creating procedures

    #Creating procedures

    proc pname {parg1 parg2 …} {pbody}

    pname Procedure name by which it is called

    parg1, parg2 Procedure arguments

    pbody Procedure body i.e., list of TCL commands to be executed

    43

  • 8/20/2019 My Ns2 Tutorial Final-Color

    44/79

    Creating procedures (cont’d)

    #’finish’ procedure-a custom way to finish your simulation

    proc finish {} {

    #declaring nsptr, ntfptr as global variablesglobal nsptr ntfptr

    #flush trace buffers to respective files

    $nsptr flush-trace

    #close all files

    close $ntfptr

     

    44

    open an ma on or s s mu a onexec nam .nam &

    #exit procedure normally

    exit 0

    }

    nsptr Pointer to simulator object

    ntfptr Pointer to nam trace file ntfname

    ntfname Name of nam trace file

  • 8/20/2019 My Ns2 Tutorial Final-Color

    45/79

    Insert error model

    #Create error model

    set errmod [new ]

    #Set errmodel parameters$errmod set

    #Set target for droped packets

    $errmod drop-target [new Agent/Null]

    errmod Pointer to error model created

     

    45

    ErrorModelErrorModel/Periodic

    ErrorModel/Uniform

    SelectErrorModel

    ErrorModel/List

    Simple error modelModeling periodic packet drop

    Uniform error model

    For selective packet drop

    Dropping specified list of packets

  • 8/20/2019 My Ns2 Tutorial Final-Color

    46/79

    Insert error model (cont’d)

    enable_

    rate_

    True if this error module is turned on

    Uniform error rate in pkt or byte

     

    For ErrorModel

    unit_ Error unit in pkts, bytes or time

    pkts,

    bytes,

    time

    For all error models

    46

    delay_bandwidth_

    markecn_

    delay_pkt_

    Time to delay packetBandwidth of the link

    If true, ecn is marked instead of dropping on corruption

    If true, packet is delayed instead of dropping

    true or false

    true or false

    true or false

  • 8/20/2019 My Ns2 Tutorial Final-Color

    47/79

    Insert error model (cont’d)

    period_

    offset_

    burstlen_

    default_drop_

    Drop period in secs

    Drop offset in secs

    Drop length in secs

    All pkts are dropped except last in period_ if true (1)

     

    For ErrorModel/Periodic

    47

    rue or a se

  • 8/20/2019 My Ns2 Tutorial Final-Color

    48/79

    Insert error model (cont’d)

    enable_

    rate_delay_

    bandwidth_

    markecn_

    delay_pkt_

    pkt_type_

    drop_cycle_

    True if this error module is turned on

    Uniform error rate in pkt or byteTime to delay packet

    Bandwidth of the link

    Mark ecn instead of dropping on corruption?

    Delay packet instead of dropping?

    0,1 or 2 (TCP, UDP or CBR packet)

    Drop pkts after every drop_cycle_

     

    For SelectErrorModel

    48

    drop_offset_ No of pkts to be dropped

    true, false

    true, false

    true, false

    0, 1 or 2

  • 8/20/2019 My Ns2 Tutorial Final-Color

    49/79

    Insert error model (cont’d)

    cnt_

    droplist_

    dropcnt_

    cur_

    Count of pkts/bytes we've seen

    Array of pkt/byte numbers to affect

    Number of entries in droplist_ total

    Current index into droplist_

    For ErrorModel/List

    49

  • 8/20/2019 My Ns2 Tutorial Final-Color

    50/79

    Insert error model (cont’d)

    #Optional: Set random variable before setting drop-target

    $errmod ranvar [new RandomVariable/]

    errmod Pointer to error model created

    ranvar Pointer to random variable created

    Uniform, Exponential, Pareto,

    ParetoII, Constant,

    HyperExponential, Empirical,

    Random variable type for random

    generator

    50

    orma , og orma , e u

  • 8/20/2019 My Ns2 Tutorial Final-Color

    51/79

    Insert error model (cont’d)

    #Place error model on simple link just before the queue

    $nsptr lossmodel $errmod $srcndh $dstndh

    or#Place error model on simple link just after the queue

    $nsptr link-lossmodel $errmod $srcndh $dstndh

    errmod Pointer to error model created

    nsptr Pointer to simulator object created

     

    51

    srcn , s n o e an es o es re no e pa r

  • 8/20/2019 My Ns2 Tutorial Final-Color

    52/79

    Insert error model (cont’d)

    #Generate random variable using desired distribution with desired parameters

    set rng [new RNG]

    $rng seed set ranvar [new RandomVariable/]

    $ranvar set

    $ranvar use-rng $rng

    rng Pointer to random generator created

    52

    ranvar Pointer to random variable created

    Seed value for random generator

    Uniform, Exponential, Pareto,

    ParetoII, Constant,

    HyperExponential, Empirical,

    Normal, LogNormal, Weibull

    Random variable type for random

    generator

  • 8/20/2019 My Ns2 Tutorial Final-Color

    53/79

    Insert error model (cont’d)

    min_

    max_

    Start value for distribution

    End value for distribution

    & Configurable parameters & their values for Uniform

    avg_ Average value for distribution

    & Configurable parameters & their values for Exponential

    av Avera e value for distribution

    & Configurable parameters & their values for Pareto & ParetoII

    53

     _

    shape_

     _

     

    Shape value for distribution

    val_ Value for distribution

    & Configurable parameters & their values for Constant

    avg_

    cov_

    Average value for distribution

    Covariance value for distribution

    & Configurable parameters & their values for HyperExponential

  • 8/20/2019 My Ns2 Tutorial Final-Color

    54/79

    Insert error model (cont’d)

    avg_

    std_

    Average value for distribution

    Standard deviation value for distribution

    & Configurable parameters & their values for Normal &

    LogNormal

    minCDF_

    maxCDF_

    interpolation_

    maxEntr

    0, 1 or 2

    Min CDF value

    Max CDF value

    Interpolation method;

    Size of CDF table

    & Configurable parameters & their values for Empirical

    54

     _  _  

    shape_

    scale_

    Shape for distribution

    Scale for distribution

    & Configurable parameters & their values for Weibull

  • 8/20/2019 My Ns2 Tutorial Final-Color

    55/79

    Insert network dynamics

    #Set network dynamics

    $nsptr rtmodel $src $dst

    or$nsptr rtmodel-at $src $dst

    Trace Up/down the link as per a trace file tracefile

     

    nsptr Pointer to simulator object

    src, dst Handles to src & dst nodes of link

    55

    ExponentialDeterministic

    Manual

    Up/down the link exponentially as per Up/down the link deterministically as per

    Up/down the link manually

    $tracefile

    { }

    { }

    Trace file already opened

    Time schedule, all in secs

    Time schedule, all in secs

    Schedule time in secs and link status

  • 8/20/2019 My Ns2 Tutorial Final-Color

    56/79

    Insert network dynamics (cont’d)

    , ,

    ,

    stt-start time; upp-link-up interval; dnp-link-

    down interval; fin-stop time

    Schedule time in secs

    up or down Up or down the link

    56

  • 8/20/2019 My Ns2 Tutorial Final-Color

    57/79

    Insert network dynamics (cont’d)

    #Simply upping and downing a link e.g., link srcà dst

    #Getting link_ for link of interest

    set lin [$nsptr link $src $dst]#Setting link srcà dst as dynamic

    $lin dynamic

    #Scheduling link dynamics for link srcà dst

    $nsptr at “$lin ”

    lin  

    57

     

    nsptr Pointer to simulator object

    src, dst Handles to nodes on link srcà dst

    up or down Up or down the link

    Schedule time in secs

  • 8/20/2019 My Ns2 Tutorial Final-Color

    58/79

    Insert routing

    #Select routing protocol

    $nsptr rtproto

    Static

    Session

    DV

    LS

    Static or Shortest Path routing algorithm

    Session routing algorithm

    Distance Vector routing algorithm

    Link State routing algorithm

    nsptr Pointer to simulator object

    58

    Manualhierarchical

     

    ManualHierarchical routing algorithm

    $n0 $n1 ...

    Selected routing algorithm applied to selected list

    of nodes; if not specified, selected algorithm

    applies to all nodes

  • 8/20/2019 My Ns2 Tutorial Final-Color

    59/79

    Insert routing (cont’d)

    #Steps for manual routing

    #Enable manual routing with rtproto

    $nsptr rtproto Manual #Set default routes for all pairs of src and dst that are linked in given

    #topology, if srcà dst is simplex-link, as

    [$src get-module “Manual”] add-route-to-adj-node -default $dst

    #or if srcà dst is duplex-link, add default route for reverse link also as

    [$dst get-module “Manual”] add-route-to-adj-node -default $src

    59

    nsptr Pointer to simulator object

    src, dst Pair of nodes that are linked in given topology

    $n0 $n1 ...

    Selected routing algorithm applied to selected list

    of nodes; if not specified, selected algorithm

    applies to all nodes

  • 8/20/2019 My Ns2 Tutorial Final-Color

    60/79

    Insert routing (cont’d)

    #Set actual route for a pair of src and dst if desired path between them is

    #e.g., srcà imn1 and imn1à dst, if srcà imn1 & imn1à dst are simplex-link, as

    [$src get-module “Manual”] add-route-to-adj-node $imn1[$imn1 get-module “Manual”] add-route-to-adj-node $dst

    #or if srcà imn1 & imn1à dst are duplex-link, set reverse path

    #dstà imn1 & imn1à src also as

    [$dst get-module “Manual”] add-route-to-adj-node $imn1

    [$imn1 get-module “Manual”] add-route-to-adj-node $src

    60

    src, dst, imn1 Nodes that are linked in given topology

  • 8/20/2019 My Ns2 Tutorial Final-Color

    61/79

    Creating LAN

    #Steps to create a LAN

    #Step 1: Create new simulator object

    #Step 2: Set NAM tracing

    #Step 3: Add nodes

    #Step 4: Add links if required

    61

    #Step 5: Create LAN including all or some nodes created

    #Step 6: Add agents & Add traffics

    #Step 7: Set event scheduling

    #Step 8: Run simulation

  • 8/20/2019 My Ns2 Tutorial Final-Color

    62/79

    Creating LAN (cont’d)

    #Step 5: Creating LAN and adding all or some nodes created

    $nsptr make-lan

    orset lanptr [$nsptr newLan

    ]

    “   ”  

    nsptr Pointer to simulator object

    62

      …  

    Bandwidth in bps

    Delay in secs

    LL (Data) Link Layer

    Queue/DropTail Interface queue type

    Mac/Csma/Cd

    Mac/Csma/Ca

    CSMA/CD LAN protocol

    CSMA/CA LAN protocol

  • 8/20/2019 My Ns2 Tutorial Final-Color

    63/79

    Creating LAN (cont’d)

    Mac/802_3

    Mac/802_11

    10Mbps LAN connectivity

    Wireless LAN protocol

    Channel

    WirelessChannel

    Channel type for wired LAN

    Channel type for wireless LAN

    Phy/WiredPhy

    Phy/WirelessPhy

    Network interface for wired LAN

    Network interface for wireless LAN

    63

  • 8/20/2019 My Ns2 Tutorial Final-Color

    64/79

    Creating mobile network

    #Steps to create a mobile network

    #Step 1: Set mobile net options

    #Step 2: Set parameters for selected mobile net options#Step 3: Create new simulator object

    #Step 4: Set nam and other tracings

    #Step 5: Create topography and GOD for mobile net

    #Step 6: Set mobile node configurations

    #Step 7: Create nodes as many as nmn

     

    64

    ep : e n a pos ons an s zes or mo e no es#Step 9: Set scheduled or random movements for mobile nodes

    #Step 10: Add agents, Connect them and Add traffics

    #Step 11: Set event scheduling for traffics

    #Step 12: Reset nodes when simulation ends

    #Step 13: Stop nam when simulation ends

    #Step 14: Call ‘finish’ to finish

  • 8/20/2019 My Ns2 Tutorial Final-Color

    65/79

    Creating mobile network (cont’d)

    #Step 1: Define mobile net options

    set opt(chan)

    set opt(prop) set opt(phy)

    set opt(mac)

    set opt(ifq)

    set opt(ll)

    set opt(ant)

     

    65

    se op q en < q en>set opt(nmn)

    set opt(rtp)

    set opt(x)

    set opt(y)

    set opt(seed)

    set opt(stp)

  • 8/20/2019 My Ns2 Tutorial Final-Color

    66/79

    Creating mobile network (cont’d)

    Channel/WirelessChannel Channel type Propagation/TwoRayGround Radio-propagation model

    Phy/WirelessPhy Network interface type

    Mac/802_11 Mac type

    Queue/DropTail/PriQueue Interface queue type for dsdv

     

    opt() Array to options

    66

     

    LL Link layer type

    Antenna/OmniAntenna Antenna type

    Maximum queue length in packets

    Number of mobile nodes

  • 8/20/2019 My Ns2 Tutorial Final-Color

    67/79

    Creating mobile network (cont’d)

    DSDV

    DSR

    TORAAODV

    Destination Sequence Distance Vector

    Dynamic Source Routing

    Temporally ordered Routing AlgorithmAdhoc On-demand Distance Vector

    X dimension of topography

    Y dimension of topography

    Seed for random num generation

     

    67

    mu a on en me n secs

  • 8/20/2019 My Ns2 Tutorial Final-Color

    68/79

    Creating mobile network (cont’d)

    #Step 2: Set Phy/WirelessPhy parameters

    Phy/WirelessPhy set

    CPThresh_

    CSThresh_

    RXThresh_

    Pt_

    freq_

    L_

    Capture threshold in dB

    Carrier sense threshold in watts

    Receive power threshold in watts

    Transmitted signal power in watts

    Frequency in Hz

    System loss factor

    68

  • 8/20/2019 My Ns2 Tutorial Final-Color

    69/79

    Creating mobile network (cont’d)

    #Step 2: Set LL parameters

    LL set

    mindelay_

    delay_

    bandwith_

    Minimum delay in secs

    Delay in secs

    Bandwith in bps

    69

     _

  • 8/20/2019 My Ns2 Tutorial Final-Color

    70/79

    Creating mobile network (cont’d)

    #Step 2: Set Antenna/OmniAntenna parameters

    Antenna/OmniAntenna set

    X_

    Y_

    Z_

    Gt_

    Gr_

    X position of antenna

    Y position of antenna

    Z position of antenna

    Transmitting gain of antenna in dB

    Receiving gain of antenna in dB

    70

     _

  • 8/20/2019 My Ns2 Tutorial Final-Color

    71/79

    Creating mobile network (cont’d)

    #Step 4: Set nam & other tracings

    set ntfptr [open ntfname.tr ]

    $nsptr namtrace-all-wireless $ntfptr $opt(x) $opt(y)

    ntfptr Pointer to nam trace file ntfname

    ntfname Name of nam trace file

    nsptr Pointer to simulator object

     

    71

     

    r

    w

    Read only

    Read/write or create

  • 8/20/2019 My Ns2 Tutorial Final-Color

    72/79

    Creating mobile network (cont’d)

    #Step 5: Create topography & GOD for mobile net

    set topo [new Topography]

    $topo load-flatgrid $opt(x) $opt(y)

    create-god $opt(nmn)

    topo Pointer to new topography created

    opt(val) Respective opt array element

    72

  • 8/20/2019 My Ns2 Tutorial Final-Color

    73/79

    Creating mobile network (cont’d)

    #Step 6: Set mobile node configurations

    $nsptr node-config $

    -addressingType

    -adhocRouting

    -llType

    -macType

    -propType

    -if T e

    usually flat or hierarchical used for wireless topologies

    Adhoc rotuing protocol like DSDV, DSR, TORA, AODV etc

    LinkLayer

    MAC type like Mac/802_11

    Propagation model like Propagation/TwoRayGround

    Interface ueue t e like ueue Dro Tail Pri ueue

    73

    -ifqLen-phyType

    -antType

    -channelType

    -topoInstance

    -wiredRouting

    -mobileIP

    -energyModel

     

    Interface queue length like 50Network inteface type like Phy/WirelessPhy

    Antenna type like Antenna/OmniAntenna

    Channel type like Channel/WirelessChannel

    Topography instance

    Turning wired routing ON or OFF

    Setting the flag for mobileIP ON or OFF

    EnergyModel type

    opt(val) Respective opt array element already defined

  • 8/20/2019 My Ns2 Tutorial Final-Color

    74/79

    Creating mobile network (cont’d)

    initialEnergy

    rxPower

    txPower

    agentTrace

    routerTrace

    macTrace

    movementTrace

    Specified in Joules

    Specified in W

    Specified in W

    Tracing at agent level turned ON or OFF

    Tracing at router level turned ON or OFF

    Tracing at mac level turned ON or OFF

    Mobilenode movement logging turned ON or OFF

    opt(val) Respective opt array element already defined

    74

     

  • 8/20/2019 My Ns2 Tutorial Final-Color

    75/79

    Creating mobile network (cont’d)

    #Step 8: Set initial positions and sizes for mobile nodes

    $ndh set

    $nsptr initial_node_pos $ndh

    ndh Handle to node

    nsptr Pointer to simulator object

    X_ X coordinate

     

    75

     _

    Z_

     

    Z coordinate

  • 8/20/2019 My Ns2 Tutorial Final-Color

    76/79

    Creating mobile network (cont’d)

    #Step 9: Set scheduled or random movements for mobile nodes

    #For scheduled movements

    #Set random-motion false for scheduled motions to take place

    $ndh random-motion 0

    $nsptr at “$ndh setdest ”

    Schedule time in secs

    < > X and Y coordinates for new osition

    76

     _  

    Speed of movement in m/s

    #For random movements

    $ndh random-motion

    $nsptr at “$ndh start”

    Schedule time in secs

    true or false

    0 or 1Random motion of node ndh is enabled if true or 1

  • 8/20/2019 My Ns2 Tutorial Final-Color

    77/79

    Creating mobile network (cont’d)

    #Step 12: Reset nodes when simulation ends

    $nsptr at $opt(stp) “$ndh reset”nsptr Pointer to simulator object

    ndh Handle to node

    opt(val) Respective opt array element

    77

  • 8/20/2019 My Ns2 Tutorial Final-Color

    78/79

    Creating mobile network (cont’d)

    #Step 13: Stop nam when simulation ends

    $nsptr at $opt(stp) “$ndh nam-end-wireless $opt(stp)”

    nsptr Pointer to simulator object

    ndh Handle to node

    opt(val) Respective opt array element

    78

  • 8/20/2019 My Ns2 Tutorial Final-Color

    79/79

    Creating mobile network (cont’d)

    # Step 14: Call ‘finish’ to finish

    $nsptr at “finish”

    proc finish {} {

    #declaring nsptr, ntfptr as global variables

    global nsptr ntfptr

    #flush trace buffers to respective files

    $nsptr flush-trace

     

    79

     

    close $ntfptr

    #stop simulation-not necessary if step 13 is used

    $nsptr halt

    #open animation for this simulation

    exec nam .nam &

    exit 0

    }