Skip to content

A Simple Wired Network Simulation with Link Failiure

The scope of this mini project is to simulate a wired network with10Gb link capacity and visualize the network behavior on re-route discovery at the event of link failure.

The TCL Script written on NS2 for Network Simulation

#Create a simulator object

set ns [new Simulator]
$ns rtproto DV
#Define different colors for data flows
$ns color 1 Blue
$ns color 2 Red
$ns color 3 Green

#Open the nam trace file
set nf [open ofc1.nam w]
$ns namtrace-all $nf

set fa [open "sender1_throughput" "w"]
set fb [open "sender2_throughput" "w"]
set fc [open "sender3_throughput" "w"]
set fd [open "total_throughput" "w"]

set f1 [open "Bandwidth1" "w"] 
set f2 [open "Bandwidth2" "w"]
set f3 [open "Bandwidth3" "w"]
set f4 [open "TotalBandwidth" "w"]



#Define a 'finish' procedure
proc finish {} {
 global ns nf nam_tracefd fa fb fc fd f1 f2 f3 f4 
 $ns flush-trace
 close $nf

 close $fa 
 close $fb 
 close $fc 
 close $fd 
 close $f1 
 close $f2 
 close $f3 
 close $f4 

 exec xgraph sender1_throughput sender2_throughput sender3_throughput total_throughput &
 exec xgraph Bandwidth1 &
 exec xgraph Bandwidth2 &
 exec xgraph Bandwidth3 &
 exec xgraph TotalBandwidth &

 puts "running nam..." 
 #Execute nam on the trace file
 exec nam ofc1.nam &
 exit 0
}


proc record {} {
 global null0 null1 null2 t fa fb fc fd f1 f2 f3 f4 
 global cbr0 cbr1 cbr2

 set ns [Simulator instance]
 set time 0.1
 set now [$ns now]
 
 set bw0 [$null0 set bytes_]
 set bw1 [$null1 set bytes_]
 set bw2 [$null2 set bytes_]

 set totbw [expr $bw0 + $bw1 + $bw2]
 puts $fd "$now [expr $totbw/$time*8/1000000]"

 puts $fa "$now [expr $bw0/$time*8/1000000]"
 puts $fb "$now [expr $bw1/$time*8/1000000]"
 puts $fc "$now [expr $bw2/$time*8/1000000]"

 puts $f1 "$now [expr $bw0]"
 puts $f2 "$now [expr $bw1]"
 puts $f3 "$now [expr $bw2]"
 puts $f4 "$now [expr $totbw]"

 $null0 set bytes_ 0
 $null1 set bytes_ 0
 $null2 set bytes_ 0

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

#Create Twelve nodes
for {set i 0} {$i < 13} {incr i} {
 set n($i) [$ns node]
}


#Create links between the nodes

$ns duplex-link $n(0) $n(1) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(2) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(3) 1Mb 10ms DropTail
$ns duplex-link $n(2) $n(5) 1Mb 10ms DropTail
$ns duplex-link $n(5) $n(6) 1Mb 10ms DropTail
$ns duplex-link $n(5) $n(7) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(4) 1Mb 10ms DropTail
$ns duplex-link $n(3) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(8) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(8) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(7) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(5) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(12) 1Mb 10ms DropTail

$ns duplex-link-op $n(5) $n(8) orient down
$ns duplex-link-op $n(0) $n(1) orient right
$ns duplex-link-op $n(1) $n(2) orient right-up
$ns duplex-link-op $n(1) $n(3) orient right-down
$ns duplex-link-op $n(2) $n(5) orient right
$ns duplex-link-op $n(5) $n(6) orient up
$ns duplex-link-op $n(5) $n(7) orient right
$ns duplex-link-op $n(3) $n(4) orient down
$ns duplex-link-op $n(3) $n(8) orient right
$ns duplex-link-op $n(8) $n(9) orient down
$ns duplex-link-op $n(8) $n(10) orient right-up
$ns duplex-link-op $n(7) $n(10) orient right-down
$ns duplex-link-op $n(10) $n(11) orient right
$ns duplex-link-op $n(10) $n(12) orient down



#Monitor the queue for the link between node 2 and node 3
#$ns duplex-link-op $n(2) $n(3) queuePos 0.5

#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n(0) $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.01
$cbr0 attach-agent $udp0

#Create a UDP agent and attach it to node n6
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n(6) $udp1

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

#Create a UDP agent and attach it to node n6
set udp2 [new Agent/UDP]
$udp2 set class_ 3
$ns attach-agent $n(6) $udp2

# Create a CBR traffic source and attach it to udp2
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize_ 500
$cbr2 set interval_ 0.01
$cbr2 attach-agent $udp2

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

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

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

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

$ns at 0.55 "record"
#Schedule events for the CBR agents
$ns at 0.5 "$n(0) color \"blue\""
$ns at 0.5 "$n(11) color \"blue\""
$ns at 0.5 "$cbr0 start"
$ns at 0.5 "$ns trace-annotate \"Starting CBR0 node0 to node11\"" 


$ns at 0.6 "$n(12) color \"red\""
$ns at 0.6 "$cbr1 start"
$ns at 0.6 "$ns trace-annotate \"Starting CBR1 node6 to node12\"" 

$ns at 0.7 "$n(4) color \"green\""
$ns at 0.7 "$cbr2 start"
$ns at 0.7 "$ns trace-annotate \"Starting CBR2 node6 to node4\"" 

$ns rtmodel-at 2.5 down $n(5) $n(8)
$ns at 2.5 "$ns trace-annotate \"Simulating Link Failure at node5 to node8\"" 
$ns at 2.7 "$ns trace-annotate \"Using Alternate path\"" 
$ns at 3.5 "$ns trace-annotate \"Link Active at node5 to node8\"" 
$ns rtmodel-at 3.5 up $n(5) $n(8)
$ns at 4.9 "$cbr1 stop"
$ns at 4.9 "$cbr0 stop"
$ns at 4.9 "$cbr2 stop"

#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run

The Sample Network Setup

Node 0 Staring transmission to node 11


Node 0 Resuming transmission to node 11 and Node 6 Staring transmission to node 11


Node 0 Resuming transmission to node 11 and Node 6 Resuming transmission to node 11 and Node 6 Starting transmission to node 4

The Bandwidth usage of Nodes.

Conclusion

In this project, a wired network has been simulated using the existing protocols and agents of ns2. A 10Gb  Back-Bone was simulated in network simulator (ns2) and the re-route discovery on link failure has been visualized.

 

WhatsApp Discuss Through WhatsApp