Download docx - NS2 Examples

Transcript
Page 1: NS2 Examples

EXPERIMENT – 1

AIM: Installation of Network Simulator (NS2) on Ubuntu 14.04.

1. Introduction: Network simulators are tools used to simulate discrete events in a network and which helps to predict the behaviours of a computer network. Generally the simulated networks have entities like links, switches, hubs, applications, etc. Once the simulation model is complete, it is executed to analyse the performance. Administrators can then customize the simulator to suit their needs. Network simulators typically come with support for the most popular protocols and networks in use today, such as WLAN,UDP,TCP,IP, WAN, etc.

Most simulators that are available today are based on a GUI application like the NCTUNS while some others incl. NS2 are CLI based. Simulating the network involves configuring the state elements like links, switches, hubs, terminals, etc. and also the events like packet drop rate, delivery status and so on. The most important output of the simulations are the tracfiles. Trace files log every packet, every event that occurred in the simulation and are used for analysis. Network simulators can also provide other tools to facilitate visual analysis of trends and potential trouble spots. Most of the simulation is performed in discrete time intervals where events that are in the queue are processed one after the other in an order.

Since simulation is a complex task, we cannot guarantee that all the simulators can provide exact or accurate results for all the different type of information. Examples of network simulators are: ns, NCTUNS, NetSim, etc.ns2 is a name for series of discrete event network simulators like ns-1, ns-2 and ns-3. All of them are discrete-event network simulators, primarily used in research and teaching. ns2 is free software, publicly available under the GNU GPLv2 license for research, development, and use. This post deals with the installation of "ns2" also called the "network simulator 2" in Ubuntu 14.04.

2. Download and extract NS2: The package downloaded will be named "ns-allinone-2.35.tar.gz". Copy it to the home folder. Then in a terminal use the following two commands to extract the contents of the package.:

cd ~/tar -xvzf ns-allinone-2.35.tar.gz

All the files will be extracted into a folder called "ns-allinone-2.35".

3. Building the dependencies:Ns2 requires a few packages to be pre installed. It also requires the GCC- version 4.3 to work correctly. So install all of them by using the following command:

sudo apt-get install build-essential autoconf automake libxmu-dev

One of the dependencies mentioned is the compiler GCC-4.3, which is no longer available, and thus we have to install GCC-4.4 version. The version 4.4 is the oldest we can get. To do that, use the follwoing command:

Page 2: NS2 Examples

sudo apt-get install gcc-4.4

The image below shows the output of executing both the above commands. If you have all the dependencies pre-installed, as I did, the output will look like the image below:

Once the installation is over, we have to make a change in the "ls.h" file. Use the following steps to make the changes:Navigate to the folder "linkstate", use the following command. Here it is assumed that the ns folder extracted is in the home folder of your system.

cd ~/ns-allinone-2.35/ns-2.35/linkstate

Now open the file named "ls.h" and scroll to the 137th line. In that change the word "error" to "this->error". The image below shows the line 137 (highlighted in the image below) after making the changes to the ls.h file.To open the file use the following command:

gedit ls.h

Save that file and close it.

Page 3: NS2 Examples

Now there is one more step that has to be done. We have to tell the ns which version of GCC will be used. To do so, go to your ns folder and type the following command:

Sudo gedit ns-allinone-2.34/otcl-1.13/Makefile.in

In the file, change Change CC= @CC@ to CC=gcc-4.4, as shown in the image below.

Page 4: NS2 Examples

4. Installation:Now we are ready to install ns2. To do so we first require root privileges and then we can run the install script. Use the following two commands:

sudo su cd ~/ns-allinone-2.35/./install

The following is a snap of these commands:

The image below shows how it looks upon successful execution

It took almost 6 minutes to build and install ns2 on my system. But before we run it, we need to add the build path to the environment path.

5. Setting the environment path:The final step is to tell the system, where the files for ns2 are installed or present. To do that, we have to set the environment path using the ".bashrc" file. In that file, we need to add a few lines at the bottom. The things to be added are given below. But for the path indicated below, many of those lines have "/home/akshay/ns-allinone-2.35/...." , but that is where I have my extracted folder. Make sure you replace them with your path. For example, if you have installed it in a folder "/home/abc", then replace "/home/akshay/ns-allinone-2.35/otcl-1.14" with "/home/abc/ns-allinone-2.35/otcl-1.14".Do this for all the required lines.

sudo gedit ~/.bashrc

Lines to be added:

# LD_LIBRARY_PATH

Page 5: NS2 Examples

OTCL_LIB=/home/akshay/ns-allinone-2.35/otcl-1.14

NS2_LIB=/home/akshay/ns-allinone-2.35/lib

X11_LIB=/usr/X11R6/lib

USR_LOCAL_LIB=/usr/local/lib

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$OTCL_LIB:$NS2_LIB:$X11_LIB:$USR_LOCAL_LIB

# TCL_LIBRARY

TCL_LIB=/home/akshay/ns-allinone-2.35/tcl8.5.10/library

USR_LIB=/usr/lib

export TCL_LIBRARY=$TCL_LIB:$USR_LIB

# PATH

XGRAPH=/home/akshay/ns-allinone-2.35/bin:/home/akshay/ns-allinone-2.35/tcl8.5.10/unix:/home/akshay/ns-allinone-2.35/tk8.5.10/unix

#the above two lines beginning from xgraph and ending with unix should come on the same line

NS=/home/akshay/ns-allinone-2.35/ns-2.35/

NAM=/home/akshay/ns-allinone-2.35/nam-1.15/

PATH=$PATH:$XGRAPH:$NS:$NAM

Once the changes have been made, save the file and restart the system.

6 . Running NS2:Once the system has restarted, open a terminal and start ns2 by using the following command:

ns

If the installation is correct then the terminal looks like the image below :

Page 6: NS2 Examples

EXPERIMENT- 2

AIM: PC to PC communication.

#Create a simulator objectset ns [new Simulator]

#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf

#Define a 'finish' procedureproc finish {} { global ns nf $ns flush-trace

#Close the trace file close $nf

#Execute nam on the trace file exec namout.nam& exit 0}

#Create two nodesset n0 [$ns node]set n1 [$ns node]

#Create a duplex link between the nodes$ns duplex-link $n0 $n1 1Mb 10ms DropTail

#Create a UDP agent and attach it to node n0set 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

#Create a Null agent (a traffic sink) and attach it to node n1set null0 [new Agent/Null]$ns attach-agent $n1 $null0

#Connect the traffic source with the traffic sink

Page 7: NS2 Examples

$ns connect $udp0 $null0

#Schedule events for the CBR agent$ns at 0.5 "$cbr0 start"$ns at 4.5 "$cbr0 stop"#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"

#Run the simulation$ns run

OUTPUT

Page 8: NS2 Examples

EXPERIMENT – 3AIM: Implementation of ring topology

#Create a simulator objectset ns [new Simulator]#Tell the simulator to use dynamic routing$ns rtproto DV#Open the nam trace filesetnf [open out.nam w]$ns namtrace-all $nf#Define a 'finish' procedureproc finish {} {global ns nf $ns flush-trace

#Close the trace fileclose $nf

#Executenam on the trace fileexecnamout.nam&exit 0}#Create seven nodesfor {set i 0} {$i< 7} {incri} {set n($i) [$ns node]}

#Create links between the nodesfor {set i 0} {$i< 7} {incri} { $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail}#Create a UDP agent and attach it to node n(0)set udp0 [new Agent/UDP]$ns attach-agent $n(0) $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

#Create a Null agent (a traffic sink) and attach it to node n(3)set null0 [new Agent/Null]$ns attach-agent $n(3) $null0

#Connect the traffic source with the traffic sink$ns connect $udp0 $null0

Page 9: NS2 Examples

#Schedule events for the CBR agent and the network dynamics$ns at 0.5 "$cbr0 start"$ns rtmodel-at 1.0 down $n(1) $n(2)$ns rtmodel-at 2.0 up $n(1) $n(2)$ns at 4.5 "$cbr0 stop"#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"

#Run the simulation$ns run

OUTPUT

Page 10: NS2 Examples

EXPERIMENT NO. - 4

AIM: Implementation of star topology.

#Create a simulator objectset ns [new Simulator]

#Define different colors for data flows$ns color 1 Blue$ns color 2 Red

#Open the nam trace filesetnf [open out.nam w]$ns namtrace-all $nf

#Define a 'finish' procedureproc finish {} {global ns nf $ns flush-trace

#Close the trace fileclose $nf

#Executenam on the trace fileexecnamout.nam&exit 0}

#Create four nodesset 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 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n2 1Mb 10ms SFQ

$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 the link between node 2 and node 3$ns duplex-link-op $n2 $n3 queuePos 0.5

Page 11: NS2 Examples

#Create a UDP agent and attach it to node n0set udp0 [new Agent/UDP]$udp0 set class_ 1$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

#Create a UDP agent and attach it to node n1set udp1 [new Agent/UDP]$udp1 set class_ 2$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1set cbr1 [new Application/Traffic/CBR]$cbr1 set packetSize_ 500$cbr1 set interval_ 0.005$cbr1 attach-agent $udp1

#Create a Null agent (a traffic sink) and attach it to node n3set null0 [new Agent/Null]$ns attach-agent $n3 $null0

#Connect the traffic sources with the traffic sink$ns connect $udp0 $null0 $ns connect $udp1 $null0

#Schedule events for the CBR agents$ns at 0.5 "$cbr0 start"$ns at 1.0 "$cbr1 start"$ns at 4.0 "$cbr1 stop"$ns at 4.5 "$cbr0 stop"#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"#Run the simulation$ns run

Page 12: NS2 Examples

OUTPUT:

Page 13: NS2 Examples

EXPERIMENT NO. -5

AIM: To show link failure between nodes of a given topology.

set ns [new Simulator]

$ns rtproto DV

set nf [open out.nam w]

$ns namtrace-all $nf

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

}

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

set n4 [$ns node]

set n5 [$ns node]

set n6 [$ns node]

set n7 [$ns node]

set n8 [$ns node]

set n9 [$ns node]

set n10 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

Page 14: NS2 Examples

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail

$ns duplex-link $n0 $n4 1Mb 10ms DropTail

$ns duplex-link $n4 $n5 1Mb 10ms DropTail

$ns duplex-link $n5 $n6 1Mb 10ms DropTail

$ns duplex-link $n6 $n3 1Mb 10ms DropTail

$ns duplex-link $n0 $n7 1Mb 10ms DropTail

$ns duplex-link $n7 $n8 1Mb 10ms DropTail

$ns duplex-link $n8 $n9 1Mb 10ms DropTail

$ns duplex-link $n9 $n10 1Mb 10ms DropTail

$ns duplex-link $n10 $n3 1Mb 10ms DropTail

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

set null0 [new Agent/Null]

$ns attach-agent $n3 $null0

$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"

$ns rtmodel-at 1.0 down $n1 $n2

$ns rtmodel-at 1.5 down $n5 $n6

$ns rtmodel-at 2.0 up $n5 $n6

$ns rtmodel-at 2.5 up $n1 $n2

Page 15: NS2 Examples

$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

$ns run

OUTPUT:

OUTPUT:

Page 16: NS2 Examples

EXPERIMENT NO. -6

Write a program to draw X-graph.

set ns [new Simulator]set f0 [open out0.tr w]set f1 [open out1.tr w]set f2 [open out2.tr w]

set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]

$ns duplex-link $n0 $n3 1Mb 100ms DropTail$ns duplex-link $n1 $n3 1Mb 100ms DropTail$ns duplex-link $n2 $n3 1Mb 100ms DropTail$ns duplex-link $n3 $n4 1Mb 100ms DropTailproc finish {} { global f0 f1 f2

close $f0close $f1close $f2

exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 & exit 0}

proc attach-expoo-traffic { node sink size burst idle rate } {

set ns [Simulator instance] set source [new Agent/UDP] $ns attach-agent $node $source

set traffic [new Application/Traffic/Exponential] $traffic set packetSize_ $size $traffic set burst_time_ $burst $traffic set idle_time_ $idle $traffic set rate_ $rate $traffic attach-agent $source

Page 17: NS2 Examples

$ns connect $source $sinkreturn $traffic

}

proc record {} { global sink0 sink1 sink2 f0 f1 f2

set ns [Simulator instance]

set time 0.5 set bw0 [$sink0 set bytes_] set bw1 [$sink1 set bytes_] set bw2 [$sink2 set bytes_]

set now [$ns now]

puts $f0 "$now [expr $bw0/$time*8/1000000]" puts $f1 "$now [expr $bw1/$time*8/1000000]" puts $f2 "$now [expr $bw2/$time*8/1000000]" $sink0 set bytes_ 0 $sink1 set bytes_ 0 $sink2 set bytes_ 0

$ns at [expr $now+$time] "record"}

set sink0 [new Agent/LossMonitor]set sink1 [new Agent/LossMonitor]set sink2 [new Agent/LossMonitor]$ns attach-agent $n4 $sink0$ns attach-agent $n4 $sink1$ns attach-agent $n4 $sink2

set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]$ns at 0.0 "record"

$ns at 10.0 "$source0 start"$ns at 10.0 "$source1 start"$ns at 10.0 "$source2 start"$ns at 50.0 "$source0 stop"$ns at 50.0 "$source1 stop"$ns at 50.0 "$source2 stop"$ns at 60.0 "finish"

Page 18: NS2 Examples

$ns run

OUTPUT:

Page 19: NS2 Examples

EXPERIMENT NO. – 7

Write a program to implementation of ping protocol

#Create a simulator objectset ns [new Simulator]#Open a trace fileset nf [open out.nam w]$ns namtrace-all $nf

#Define a 'finish' procedureproc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit 0}#Create three nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]#Connect the nodes with two links$ns duplex-link $n0 $n1 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail#Define a 'recv' function for the class 'Agent/Ping'Agent/Ping instproc recv {from rtt} {

$self instvar node_puts "node [$node_ id] received ping answer from \

$from with round-trip-time $rtt ms."}#Create two ping agents and attach them to the nodes n0 and n2set p0 [new Agent/Ping]$ns attach-agent $n0 $p0set p1 [new Agent/Ping]$ns attach-agent $n2 $p1#Connect the two agents$ns connect $p0 $p1

Page 20: NS2 Examples

#Schedule events$ns at 0.2 "$p0 send"$ns at 0.4 "$p1 send"$ns at 0.6 "$p0 send"$ns at 0.6 "$p1 send"$ns at 1.0 "finish"#Run the simulation$ns run

OUTPUT:

Page 21: NS2 Examples

EXPERIMENT NO. – 8

Creating simple wireless scenario and implement node movement traffic movement files for wireless simulations

tcl file

Phy/WirelessPhy set freq_ 2.472e9 Phy/WirelessPhy set RXThresh_ 2.62861e-09; #100m radiusPhy/WirelessPhy set CSThresh_ [expr 0.9*[Phy/WirelessPhy set RXThresh_]]Phy/WirelessPhy set bandwidth_ 11.0e6Mac/802_11 set dataRate_ 11MbMac/802_11 set basicRate_ 2Mb

set val(chan) Channel/WirelessChannel ;set val(prop) Propagation/TwoRayGround ;set val(netif) Phy/WirelessPhy ;set val(mac) Mac/802_11 ;set val(ifq) Queue/DropTail/PriQueue ;set val(ll) LL ;set val(ant) Antenna/OmniAntenna ;set val(ifqlen) 30 ;set val(nn) 20 ;set val(rp) AODV ;set val(x) 200 ;set val(y) 200 ;set val(stop) 50 ;

set ns [new Simulator]

set tracefd [open xpectr.tr w]

set winFile [open CwMaodv_20 w]

set namtracefd [open namwrls.nam w]

$ns trace-all $tracefd$ns use-newtrace

$ns namtrace-all-wireless $namtracefd $val(x) $val(y)

set topo [new Topography]$topo load_flatgrid $val(x) $val(y)

Page 22: NS2 Examples

create-god $val(nn)

$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 $val(chan) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace OFF \

for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns node] }

$node_(0) set X_ 95.0$node_(0) set Y_ 50.0$node_(0) set Z_ 0.0

$node_(1) set X_ 60.0$node_(1) set Y_ 50.0$node_(1) set Z_ 0.0

$node_(2) set X_ 25.0$node_(2) set Y_ 190.0$node_(2) set Z_ 0.0

$node_(3) set X_ 135.0$node_(3) set Y_ 155.0$node_(3) set Z_ 0.0

$node_(4) set X_ 105.0$node_(4) set Y_ 180.0$node_(4) set Z_ 0.0

$node_(5) set X_ 110.0$node_(5) set Y_ 200.0$node_(5) set Z_ 0.0

Page 23: NS2 Examples

$node_(6) set X_ 55.0$node_(6) set Y_ 75.0$node_(6) set Z_ 0.0

$node_(7) set X_ 1.0$node_(7) set Y_ 20.0$node_(7) set Z_ 0.0

$node_(8) set X_ 175.0$node_(8) set Y_ 90.0$node_(8) set Z_ 0.0

$node_(9) set X_ 115.0$node_(9) set Y_ 115.0$node_(9) set Z_ 0.0

$node_(10) set X_ 75.0$node_(10) set Y_ 175.0$node_(10) set Z_ 0.0

$node_(11) set X_ 150.0$node_(11) set Y_ 135.0$node_(11) set Z_ 0.0

$node_(12) set X_ 45.0$node_(12) set Y_ 90.0$node_(12) set Z_ 0.0

$node_(13) set X_ 10.0$node_(13) set Y_ 45.0$node_(13) set Z_ 0.0

$node_(14) set X_ 90.0$node_(14) set Y_ 1.0$node_(14) set Z_ 0.0

$node_(15) set X_ 110.0$node_(15) set Y_ 100.0$node_(15) set Z_ 0.0

$node_(16) set X_ 160.0$node_(16) set Y_ 120.0$node_(16) set Z_ 0.0

$node_(17) set X_ 180.0$node_(17) set Y_ 20.0

Page 24: NS2 Examples

$node_(17) set Z_ 0.0

$node_(18) set X_ 120.0$node_(18) set Y_ 10.0$node_(18) set Z_ 0.0

$node_(19) set X_ 190.0$node_(19) set Y_ 1.0$node_(19) set Z_ 0.0

$ns at 10.0 "$node_(2) setdest 135.0 65.0 8.0"$ns at 10.0 "$node_(4) setdest 140.0 70.0 8.0"$ns at 10.0 "$node_(8) setdest 125.0 100.0 8.0"$ns at 10.0 "$node_(10) setdest 190.0 160.0 8.0"$ns at 10.0 "$node_(18) setdest 70.0 120.0 8.0"$ns at 10.0 "$node_(14) setdest 10.0 170.0 8.0"

set tcp [new Agent/TCP]set sink [new Agent/TCPSink]$ns attach-agent $node_(0) $tcp$ns attach-agent $node_(2) $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp

$ns at 0.1 "$ftp start"for {set i 0} {$i < $val(nn) } {incr i} { $ns initial_node_pos $node_($i) 10 }for {set i 0} {$i < $val(nn) } {incr i} { $ns at $val(stop) "$node_($i) reset"}

$ns at $val(stop) "stop"

proc plotWindow {tcpSource file} {global nsset time 0.1set now [$ns now]set cwnd [$tcpSource set cwnd_]puts $file "$now $cwnd"$ns at [expr $now+$time] "plotWindow $tcpSource $file" }$ns at 0.1 "plotWindow $tcp $winFile"

proc stop {} {global ns tracefd namtracefd

Page 25: NS2 Examples

$ns flush-traceclose $tracefdclose $namtracefdexec nam namwrls.nam &exit 0}$ns run

awk file

BEGIN { sendLine = 0; recvLine = 0; fowardLine = 0;} $0 ~/^s.* AGT/ { sendLine ++ ;} $0 ~/^r.* AGT/ { recvLine ++ ;} $0 ~/^f.* RTR/ { fowardLine ++ ;} END { printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;}

OUTPUT:

Page 26: NS2 Examples

EXPERIMENT NO. – 9

Creating simple wireless cum wired scenario and running mobile IP in a simple wired cum wireless technology

# check for boundary parameters and random seedif { $opt(x) == 0 || $opt(y) == 0 } {

puts "No X-Y boundary values given for wireless topology\n"}if {$opt(seed) > 0} {

puts "Seeding Random number generator with $opt(seed)\n"ns-random $opt(seed)

}# create simulator instanceset ns_ [new Simulator]

# set up for hierarchical routing$ns_ node-config -addressType hierarchical

AddrParams set domain_num_ 3 ;# number of domainslappend cluster_num 2 1 1 ;# number of clusters in each domainAddrParams set cluster_num_ $cluster_numlappend eilastlevel 1 1 2 1 ;# number of nodes in each cluster AddrParams set nodes_num_ $eilastlevel ;# of each domain

set tracefd [open wireless3-out.tr w]set namtrace [open wireless3-out.nam w]$ns_ trace-all $tracefd$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)

# Create topography objectset topo [new Topography]

# define topology$topo load_flatgrid $opt(x) $opt(y)

# create God# 2 for HA and FAcreate-god [expr $opt(nn) + 2]

#create wired nodesset temp {0.0.0 0.1.0} ;# hierarchical addresses for {set i 0} {$i < $num_wired_nodes} {incr i} {

Page 27: NS2 Examples

set W($i) [$ns_ node [lindex $temp $i]] }

# Configure for ForeignAgent and HomeAgent nodes$ns_ node-config -mobileIP ON \ -adhocRouting $opt(adhocRouting) \ -llType $opt(ll) \ -macType $opt(mac) \ -ifqType $opt(ifq) \ -ifqLen $opt(ifqlen) \ -antType $opt(ant) \ -propType $opt(prop) \ -phyType $opt(netif) \ -channelType $opt(chan) \

-topoInstance $topo \ -wiredRouting ON \

-agentTrace ON \ -routerTrace OFF \ -macTrace OFF

# Create HA and FAset HA [$ns_ node 1.0.0]set FA [$ns_ node 2.0.0]$HA random-motion 0$FA random-motion 0

# Position (fixed) for base-station nodes (HA & FA).$HA set X_ 1.000000000000$HA set Y_ 2.000000000000$HA set Z_ 0.000000000000

$FA set X_ 650.000000000000$FA set Y_ 600.000000000000$FA set Z_ 0.000000000000

# create a mobilenode that would be moving between HA and FA.# note address of MH indicates its in the same domain as HA.$ns_ node-config -wiredRouting OFF

set MH [$ns_ node 1.0.1]set node_(0) $MHset HAaddress [AddrParams addr2id [$HA node-addr]][$MH set regagent_] set home_agent_ $HAaddress

Page 28: NS2 Examples

# movement of the MH$MH set Z_ 0.000000000000$MH set Y_ 2.000000000000$MH set X_ 2.000000000000

# MH starts to move towards FA$ns_ at 100.000000000000 "$MH setdest 640.000000000000 610.000000000000 20.000000000000"# goes back to HA$ns_ at 200.000000000000 "$MH setdest 2.000000000000 2.000000000000 20.000000000000"

# create links between wired and BaseStation nodes$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail$ns_ duplex-link $W(1) $HA 5Mb 2ms DropTail$ns_ duplex-link $W(1) $FA 5Mb 2ms DropTail

$ns_ duplex-link-op $W(0) $W(1) orient down$ns_ duplex-link-op $W(1) $HA orient left-down$ns_ duplex-link-op $W(1) $FA orient right-down

# setup TCP connections between a wired node and the MobileHost

set tcp1 [new Agent/TCP]$tcp1 set class_ 2set sink1 [new Agent/TCPSink]$ns_ attach-agent $W(0) $tcp1$ns_ attach-agent $MH $sink1$ns_ connect $tcp1 $sink1set ftp1 [new Application/FTP]$ftp1 attach-agent $tcp1$ns_ at $opt(ftp1-start) "$ftp1 start"

# source connection-pattern and node-movement scriptsif { $opt(cp) == "" } {

puts "*** NOTE: no connection pattern specified." set opt(cp) "none"} else {

puts "Loading connection pattern..."source $opt(cp)

}if { $opt(sc) == "" } {

puts "*** NOTE: no scenario file specified." set opt(sc) "none"} else {

puts "Loading scenario file..."

Page 29: NS2 Examples

source $opt(sc)puts "Load complete..."

}

# Define initial node position in nam

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

# 20 defines the node size in nam, must adjust it according to your # scenario # The function must be called after mobility model is defined

$ns_ initial_node_pos $node_($i) 20}

# Tell all nodes when the siulation endsfor {set i 0} {$i < $opt(nn) } {incr i} { $ns_ at $opt(stop).0 "$node_($i) reset";}$ns_ at $opt(stop).0 "$HA reset";$ns_ at $opt(stop).0 "$FA reset";

$ns_ at $opt(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"$ns_ at $opt(stop).0001 "stop"proc stop {} { global ns_ tracefd namtrace close $tracefd close $namtrace}

# some useful headers for tracefileputs $tracefd "M 0.0 nn $opt(nn) x $opt(x) y $opt(y) rp \

$opt(adhocRouting)"puts $tracefd "M 0.0 sc $opt(sc) cp $opt(cp) seed $opt(seed)"puts $tracefd "M 0.0 prop $opt(prop) ant $opt(ant)"

puts "Starting Simulation..."$ns_ run

Page 30: NS2 Examples

EXPERIMENT NO. – 10(a)

EHTHERNET LAN PROTOCOL (CSMA/CD)

Aim: To study about CSMA/CD (IEEE 802.3), which is the Ethernet LAN protocol.

Theory: 

Whenever multiple users have unregulated access to a single line, there is a danger of signals overlapping and destroying each other. Such overlaps turn the signals into unusable noise, called collisions.

As traffic increases on the multiple access links, the collision also increases. A LAN therefore needs a mechanism to co-ordinate traffic, minimize the number of collisions that occur, and maximize the number of frames that are delivered successfully.

The access method used in the Ethernet is called Carrier Sense Multiple Access with Collision Detection (CSMA/CD).

CSMA/CD is the result of an evolution from Multiple Access (MA) to Carrier Sense Multiple Access (CSMA), and finally Carrier Sense Multiple Access with Collision Detection (CSMA/CD).

If the station can determine whether a collision is taking place, then the amount of wasted bandwidth can be reduced by aborting the transmission when a collision is detected. The CSMA/CD uses this approach.

CSMA/CD is the most commonly used protocol for LANs. It was developed by Digital Equipment Corporation (DEC), Intel and Xerox. This network is called Ethernet. The IEEE 802.3 CSMA/CD standard for LAN is based on Ethernet specification.

A station with a message to send must monitor the channel to see if any other station is sending. If another station is sending, the second station must wait until the sending station has finished. Then it may send its message. If no station was sending at the time that it first listened, the station may send its message immediately. The term “carrier sense” indicates this “listening before transmitting” behavior.

All stations are attached to one path and monitor the signal on the channel through transceiver attached to the cable. CSMA/CD has totally decentralized control and is based on contention access.

Page 31: NS2 Examples

CSMA/CD supports both baseband and broadband system. CSMA/CD offers four options in terms of bit rate, signaling method and maximum electrical cable segment length. These are:• 10 BASE 5• 10 BASE 2• 1 BASE 5• 10 BROAD 36? The number at the beginning indicates the bit rate in Mbps.? The middle term indicates the type of signaling.? The number at the end indicates the cable length in multiples of 100 meters.

Base band (Digital signal): Manchester encoding.Broad band (Analog signal): Differential Phase Shift Keying encoding.

The format of MAC frames in CSMA/CD:

• Preamble: 56 bits of altering 1s and 0s.• SFD: Start Frame Delimiter. This signals the beginning of the frame.• Destination address: Contains the physical address of the packet’s destination address.• Source address: Contains the physical address of the packet’s sender’s address.• Length PDU: Indicates the number of bytes in the coming Protocol Data Unit (PDU).The data unit in the upper layer of Data Link Layer (Logical Link Control) is the PDU.• Data and Padding: It contains the information and padding is done if necessary.• CRC: Cyclic Redundancy Check. It contains the error detection information.• DSAP: Destination Service Access Point. • SSAP: Source Service Access Point.• Control:It is used for flow management. The frames are of 3 types as in HDLC: If the first bit in the control field is 0, it is I-frame.If the two bits in the control field are 10, it is S-frame.If the two bits in the control field are 11, it is U-frame.

The PDU from the LLC has no CRC, and no station address. These fields are added in the MAC layer (lower layer in the Data Link Layer).

Result: 

Thus the Ethernet LAN protocol with its frame format was studied.

Page 32: NS2 Examples

EXPERIMENT NO. - 10(b)

SIMULATION OF CSMA/CD 

Aim: To simulate the CSMA/CD protocol using ViRtSim software.

Procedure: 

1. Select program ?ViRtSim? Simulation ?CSMA ?CSMA/CD 2. Enter the program in the Algorithm column (right side), save and run it.

Algorithm: 

Step 1: Declare two frame variables and assign them the data frames to be transmitted. Step 2: Initialize the CSMA/CD function. Step 3: Send data from node B to node A and sense multiple access. Step 4: Send data from node A to node C and sense multiple access. Step 5: Check if collision occurs. Step 6: If collision occurs wait for 1000ms and then retransmit the two data from the nodes one after the other. Step 7: Stop the execution. 

Program: 

includevoid main(){Frame X,Y;X="A.R.Engg";Y="III-ECE";

CSMACD_INIT(); CSMACD_START(); CSMA_SEND(B,A,X); CSMA_SEND(A,C,Y);

R=COLLISION_OCCUR();if(R){ 

Page 33: NS2 Examples

WAIT(1000); 

RETRANSMIT(B,A); RETRANSMIT(A,C);

}}

Output:

1. Two Stations Transmit data X and Y simultaneously: 2. Occurrence of Collision:3. Retransmit the data X from Station B to Station A: 4. Retransmit the data Y from Station A to Station C: 

Result: 

Thus the CSMA/CD protocol was simulated using ViRtSim software was studied.