Basics of NS2 and Otcl/tcl script

Simulation is the process of learning by doing. Whenever there is something new in the world, we try to analyse it first by examining it and in the process get to learn a lot of things. This entire course is called as Simulation.

Correlating to this process, in order to understand all the complexities one need to model the entire role-play in form of computer simulation, the need is to build artificial objects and assign them roles dynamically.

Computer simulation is the designing of a theoretical physical system on a digital computer with emphasis on model designing, execution and analysis. After creation of the mathematical model the most important step is to create a computer program for updating the state and event variables through time (by time slicing or event scheduling). If this simulation is carried out successively in parallel computers, it is called Parallel or Distributed simulation.

Network simulation (NS) is one of the types of simulation, which is used to simulate the networks such as in MANETs, VANETs etc. It provides simulation for routing and multicast protocols for both wired and wireless networks. NS is licensed for use under version 2 of the GNU (General Public License) and is popularly known as NS2. It is an object-oriented, discrete event-driven simulator written in C++ and Otcl/tcl.
NS-2 can be used to implement network protocols such as TCP and UPD, traffic source behavior such as FTP, Telnet, Web, CBR and VBR, router queue management mechanism such as Drop Tail, RED and CBQ, routing algorithms and many more. In ns2, C++ is used for detailed protocol implementation and Otcl is used for the setup. The compiled C++ objects are made available to the Otcl interpreter and in this way, the ready-made C++ objects can be controlled from the OTcl level.
 
Install NS-2 using this command :

sudo apt-get install ns2

Nam is also needed to install. Nam (Network Animator) is an animation tool to graphically represent the network and packet traces. Use this command :



sudo apt-get install nam

Some basic Otcl script syntax :

  • Basic Command :
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set a 8
    set b [expr $a/8]

    chevron_right

    
    

    Explanation : In the first line, the variable a is assigned the value 8. In the second line, the result of the command [expr $a/8], which equals 1, is then used as an argument to another command, which in turn assigns a value to the variable b. The “$” sign is used to obtain a value contained in a variable and square brackets are an indication of a command substitution.

  •  

  • Define new procedures with proc command
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    proc factorial fact {
      
        if {$fact <= 1} {
      
            return 1
        }
    expr $fact * [factorial [expr $fact-1]]
      
    }

    chevron_right

    
    

  •  

  • To open a file for reading :
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set testfile [open hello.dat r]

    chevron_right

    
    

    Similarly, put command is used to write data into the file

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set testfile [open hello.dat w]
      
    puts $testfile “hello1”

    chevron_right

    
    

  •  

  • To call subprocesses within another process, exec is used, which creates a subprocess and waits for it to complete.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    exec rm $testfile

    chevron_right

    
    

     

  • To be able to run a simulation scenario, a network topology must first be created. In ns2, the topology consists of a collection of nodes and links.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set ns [new Simulator]

    chevron_right

    
    

  •  

  • The simulator object has member functions which enables to create the nodes and define the links between them. The class simulator contains all the basic functions. Since ns was defined to handle the Simulator object, the command $ns is used for using the functions belonging to the simulator class.

    In the network topology nodes can be added in the following manner :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  •  

  • Traffic agents (TCP, UDP etc.) and traffic sources (FTP, CBR etc.) must be set up if the node is not a router. It enables to create CBR traffic source using UDP as transport protocol or an FTP traffic source using TCP as a transport protocol.

    CBR traffic source using UDP :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set udp0 [new Agent/UDP]
      
    $ns attach-agent $n0 $udp0
    set cbr0 [new Application/Traffic/CBR]
    $cbr0 attach-agent $udp0
    $cbr0 set packet_size_ 512

    chevron_right

    
    

     
    FTP traffic source using TCP :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set tcp0 [new Agent/TCP]
    $ns attach-agent $n0 $tcp0
    set ftp0 [new Application/FTP]
    $ftp0 attach-agent $tcp0
    $tcp0 set packet_size_ 512

    chevron_right

    
    

Below is the implementation of creating links between the source and destination using both ftp and tcp :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Create a simulator object
set ns [new Simulator]
  
# Define different colors 
# for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
  
# 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 NAM trace file
    close $nf
      
    # Execute NAM on the trace file
    exec nam out.nam &
    exit 0
}
  
# Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
  
# Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
  
# Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
  
# Give node position (for NAM)
$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
  
# Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
  
  
# Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
  
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
  
# Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
  
  
# Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
  
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
  
# Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false
  
  
# Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
  
# Detach tcp and sink agents
# (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
  
# Call the finish procedure after
# 5 seconds of simulation time
$ns at 5.0 "finish"
  
# Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
  
# Run the simulation
$ns run

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.