Open In App

TCL script to simulate link state routing in ns2

Last Updated : 13 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will know how to write a TCL script for the simulation of one of the routing protocols link state routing (also called dijkstra’s algorithm) using the Network Simulator. To understand more about the basics of TCL scripts you can check out this article https://www.geeksforgeeks.org/basics-of-ns2-and-otcltcl-script/

TCL script to simulate link state routing in ns2 :
To implement TCL script to simulate link state routing in ns2, we will go through the various steps in simulating LS (Link State) routing, each with several lines of code as follows.

Step-1: Initializing the network :
The first step is to initialize the network simulator, and we do so by creating a network simulator object. After that, we initialize rtproto (routing protocol) to Link State (LS).

set ns [new Simulator]
$ns rtproto LS

Step-2: Creating number of nodes :
We next create a random number of nodes, let’s say 7. We use the node instance to create these nodes as follows.

set node1 [$ns node]
set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
set node5 [$ns node]
set node6 [$ns node]
set node7 [$ns node]

Step-3: Creating the trace file :
Our next step is to create the trace file and nam file. The nam file is used to view simulator output whereas the trace file traces all the routing information in the process. For this we create trace file and nam file objects and then open the files in write mode. The trace-all instance is used to trace all routing information into the trace file and similarly namtrace-all for the nam file.

set tf [open out.tr w]
$ns trace-all $tf
set nf [open out.nam w]
$ns namtrace-all $nf

Step-4: Labeling the nodes :
In the next step we can label the nodes if we wish to. Here we are labeling them from node 0 to node 6. We can also customize the labels by assigning different colors to them and thus viewing the simulation much more clearly. Here we will use red and blue.

$node1 label "node 1"
$node1 label "node 2"
$node1 label "node 3"
$node1 label "node 4"
$node1 label "node 5"
$node1 label "node 6"
$node1 label "node 7"
$node1 label-color blue
$node2 label-color red
$node3 label-color red
$node4 label-color blue
$node5 label-color blue
$node6 label-color blue
$node7 label-color blue

Step-5: Creating duplex links :
The next step is to create duplex links between the nodes forming a ring in the end. This can be achieved by using the duplex-link instance along with specifying three parameters: data rate (1.5Mb), delay (10ms) and kind of queue (DropTail).

$ns duplex-link $node1 $node2 1.5Mb 10ms DropTail
$ns duplex-link $node2 $node3 1.5Mb 10ms DropTail
$ns duplex-link $node3 $node4 1.5Mb 10ms DropTail
$ns duplex-link $node4 $node5 1.5Mb 10ms DropTail
$ns duplex-link $node5 $node6 1.5Mb 10ms DropTail
$ns duplex-link $node6 $node7 1.5Mb 10ms DropTail
$ns duplex-link $node7 $node1 1.5Mb 10ms DropTail

Step-6: Orient the links between the nodes :
Now we need to orient the links between the nodes appropriately to obtain proper alignment. The duplex-link-op instance is used for the same.

$ns duplex-link-op $node1 $node2 orient left-down
$ns duplex-link-op $node2 $node3 orient left-down
$ns duplex-link-op $node3 $node4 orient right-down
$ns duplex-link-op $node4 $node5 orient right
$ns duplex-link-op $node5 $node6 orient right-up
$ns duplex-link-op $node6 $node7 orient left-up
$ns duplex-link-op $node7 $node1 orient left-up

Step-7: Attaching TCP agents :
The next step is to attach TCP agents (using attach-agent) at two nodes let’s say node 1 and node 4. We can do this creating the source and sink objects and connecting them using connect instance.

set tcp2 [new Agent/TCP]
$ns attach-agent $node1 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $node4 $sink2
$ns connect $tcp2 $sink2

Step-8: Creating FTP traffic :
Our next step is to create FTP traffic and attach to TCP source. The traffic then flows across node 1 and node 4. We can do this by creating an FTP agent and attaching it to tcp2.

set traffic_ftp2 [new Application/FTP]
$traffic_ftp2 attach-agent $tcp2

Step-9: Adding a finish procedure :
The next step is to add a finish procedure to flush all data into trace file and then and then run the nam file.

proc finish{} {

global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
 
}

Step-10: Scheduling the FTP :
The final step is to schedule the FTP traffic at the required time intervals. We can also disable the link between any pair of nodes at a certain timestamp using rtmodel-at instance and then enable it after a certain time. This is majorly done for testing purposes. Here we have disabled the link between nodes 2 and 3. The program ends with the run command.

$ns at 0.5 "traffic_ftp2 start"
$ns rtmodel-at 1.0 down $node2 $node3
$ns rtmodel-at 2.0 up $node2 $node3
$ns at 3.0 "traffic_ftp2 start"
$ns at 4.0 "traffic_ftp2 stop"
$ns at 5.0 "finish"
$ns run

Output :
The final output can be visualized as follows.

Simulation before disabling the link

As shown in the figure above there is a normal flow of packets from node 1 to node 4. We now disable the link between nodes 2 and 3 at time 1 and the simulations changes as shown below. The disabled link is shown in red and the packet flow this time changes direction rather than the original route to node 4.

Simulation when link between node2 and node3 is disabled


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads