Lets learn "How to write a simple NS2 program and analyse the packets"

 

Before preceding , NS2 and NAM must be installed on your PC .


If you dont have use the below commands to install ns2 and nam

sudo apt-get install ns2
sudo apt-get purge nam
wget --user-agent="Mozilla/5.0 (Windows NT 5.2; rv:2.0.1) Gecko/20100101
Firefox/4.0.1" "http://technobytz.com/wp-content/uploads/2015/11/nam_1.14_amd64.zip"
unzip nam_1.14_amd64.zip
sudo dpkg -i nam_1.14_amd64.deb
sudo apt-mark hold nam


After installing the above tools let us move to the coding part ...

#Create a new Simulation Instance set ns [new Simulator] set bandwidth 1.75Mb #Turn on the Trace and the animation files set f [open out.tr w] set nf [open out.nam w] $ns trace-all $f $ns namtrace-all $nf #Define the finish procedure to perform at the end of the simulation proc finish {} { global f nf ns $ns flush-trace close $f close $nf exec nam out.nam & #exec awk -f 1.awk out.tr & exit 0 } #Create the nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] #Label the nodes $n0 label "TCP Source" $n1 label "UDP Source" $n2 label "Sink" #Set the color $ns color 1 red $ns color 2 yellow #Create the Topology $ns duplex-link $n0 $n1 $bandwidth 10ms DropTail $ns duplex-link $n1 $n2 1.75Mb 20ms DropTail #Attach a Queue of size N Packets between the nodes n1 n2 $ns queue-limit $n1 $n2 10 #Make the Link Orientation $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right #Create a UDP Agent and attach to the node n1 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 #Create a CBR Traffic source and attach to the UDP Agent set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $udp0 #Specify the Packet Size and interval $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 #Create a Null Agent and attach to the node n2 set null0 [new Agent/Null] $ns attach-agent $n2 $null0 #Connect the CBR Traffic source to the Null agent $ns connect $udp0 $null0 #Create a TCP agent and attach to the node n0 set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0 #Create a FTP source and attach to the TCP agent set ftp0 [new Application/FTP] #Attach the FTP source to the TCP Agent $ftp0 attach-agent $tcp0 #Create a TCPSink agent and attach to the node n2 set sink [new Agent/TCPSink] $ns attach-agent $n2 $sink #Specify the Max file Size in Bytes $ftp0 set maxPkts_ 1000 #Connect the TCP Agent with the TCPSink $ns connect $tcp0 $sink $udp0 set class_ 1 $tcp0 set class_ 2 #Schedule the Events $ns at 0.1 "$cbr0 start" $ns at 1.0 "$ftp0 start" $ns at 4.0 "$ftp0 stop" $ns at 4.5 "$cbr0 stop" $ns at 5.0 "finish" $ns run

Now run the code using

ns filename.tcl

you will get something like this : 




Also , a trace file has been generated which is out.tr . This trace file contains data related to packets sent and packets recieved and about packet dropped .

Part of trace file is shown below :





1. Event
This field defines the type of event :
+ :a packet enque event
-  :a packet deque event
r  :a packet reception event
d :a packet drop (e.g., sent to dropHead_) event
c :a packet collision at the MAC level

2. Time
This field defines the time at which packet tracing string is created .

3. From
This defines the source node of packet .

4. To
This defines the destination node of packet .

5. Packet Type
This field defines the transport layer protocol agent used (cbr,ftp) .

6. Packet Size
This field defines the size of packet in bytes .

7. Flags
This field consist of 7 digit string whose values defines the following info :
“-”: disable
1st = “E”: ECN (Explicit Congestion Notification) echo is enabled.
2nd = “P”: the priority in the IP header is enabled.
3rd : Not in use
4th = “A”: Congestion action
5th = “E”: Congestion has occurred.
6th = “F”: The TCP fast start is used.
7th = “N”: Explicit Congestion Notification (ECN) is on. 

8. FID
(Flow Id)

9. Source Address
This field defines the source address of the packet(actual origin of the packet) . This is defined in the form of a.b (a defines the address and b defines the port number) .

10. Destination Address
This field defines the destination address of the packet(final destination of the packet) . This is also define d in the form of a.b (a defines the address and b.defines the port address).

11. Seq number
This field defines the sequence number of the packet .

12. Packet ID
This field defines the packet id (usually same as sequence number).


Now we get the basic format of the trace file generated . Now we required to find the number of dropped packets to find the throughput .

This can be done by using any programming language because we are only required to read the trace file and need to count the number of packets (either sent, or received , or dropped)

But here we are using the perl script to do so ...

#!/usr/bin/perl use :strict; if($#ARGV<0){ printf("Usage: <trace-file>\n"); exit 1; } # to open the given trace file open(Trace, $ARGV[0]) or die "Cannot open the trace file"; my $sc = 0; # sending counter my $rc = 0; # receiving counter my $rp = 0; my $mc =0; my $d_udp = 0; my $d_tcp = 0; my %pkt_fc = (); #packet forwarding counter while(<Trace>){ # read one line in from the file my @line = split; #split the line with delimin as space if($line[0] eq "d" && $line[4] eq "cbr"){ $d_udp++; } if($line[0] eq "d" && $line[4] eq "tcp"){ $d_tcp++; } printf("Dropped tcp length %f\n",$d_tcp); printf("Dropped udp length %f\n",$d_udp);
#Command use to run perl script from terminal #perl filename.p tracefilename.tr


Now to plot the graph (dropped packets vs bandwidth) , you need to install the xgraph (a tool that takes the coordinates as input and plot the corrosponding graph)


To insall xgraph

sudo apt-get install ns2 nam xgraph


Now create a file graph.xg




Now run the file using command 

xgraph graph.xg

and you will get the graph as shown below ..


Comments

Popular posts from this blog

Lets learn "About kube proxy in iptables mode"

Lets learn "System design for paste bin (or any text sharing website)"

Lets learn "What is CDN?"