Sunday, February 8, 2015

Working with NS2

  • First of all, you need to create a simulator object. This is done with the command
                                      set ns [new Simulator]

  •  Now we open a file for writing that is going to be used for the nam trace data.

                                     set nf [open out.nam w]

                                     $ns namtrace-all $nf

 The first line opens the file 'out.nam' for writing and gives it the file handle 'nf'. In the second line we tell the simulator object that we created above to write all simulation data that is going to be relevant for nam into this file.

  • The next step is to add a 'finish' procedure that closes the trace file and starts nam.

                                         proc finish {} {

                                         global ns nf

                                         $ns flush-trace

                                         close $nf

                                         exec nam out.nam &

                                         exit 0 }

  • The next line tells the simulator object to execute the 'finish' procedure after 5.0 seconds of simulation time
                                          $ns at 5.0 "finish"

 We probably understand what this line does just by looking at it. NS provides us with a very simple way to schedule events with the 'at' command.

  • The last line finally starts the simulation
                                           $ns run

LINKS


For transmitting packets from one node to another node there should be a link between them.

SIMPLEX LINK

 Ns provides the instance procedure simplex-link to form a unidirectional link from one node to another. The link is in the class Simplex Link. The following describes the syntax of the Simplex link.

           $ns simplex-link <node0> <node1> <bandwidth> <delay> <queue type>

 The command creates a link from <node0> to <node1>, with specified <bandwidth> and <delay> characteristics.

Queue type : The type of queue used in the link. Default value is DropTail.

Bandwidth : Link bandwidth in bits per second.

Delay : Link propagation delay in seconds.

DUPLEX LINK


  The following is the syntax for duplex link between nodes.

              $ns_ duplex-link <node1> <node2> <bw> <delay> <q type> <args>

This creates a bi-directional link between node1 and node2. This procedure essentially creates a duplex-link from two simplex links, one from node1 to node2 and the other from node2 to node1.

bw : Link bandwidth in bits per second.

delay : Link propagation delay in seconds.

qtype : The link uses a queue type of <q type> and depending on the queue type different arguments are passed through <args>. Default qtype is value is DropTail.

AGENTS


Agents represent endpoints where network-layer packets are constructed or consumed, and are used in the implementation of protocols at various layers.

There are two major types of TCP agents: one-way agents and a two- way agent. One-way agents are further subdivided into a set of TCP senders (which obey different congestion and error control techniques) and receivers (“sinks”). The two-way agent is symmetric in the sense that it represents both a sender and receiver. It is still under development.

The one-way TCP sending agents currently supported are:

• Agent/TCP - a “tahoe” TCP sender

• Agent/TCP/Reno - a “Reno” TCP sender

• Agent/TCP/Newreno - Reno with a modification

• Agent/TCP/Sack1 - TCP with selective repeat (follows RFC2018)

• Agent/TCP/Vegas - TCP Vegas

• Agent/TCP/Fack - Reno TCP with “forward acknowledgment”

• Agent/TCP/Linux - a TCP sender with SACK support that runs TCP congestion control modules from Linux kernel

The one-way TCP receiving agents currently supported are:

• Agent/TCPSink - TCP sink with one ACK per packet

• Agent/TCPSink/DelAck - TCP sink with configurable delay per ACK

• Agent/TCPSink/Sack1 - selective ACK sink (follows RFC2018)

• Agent/TCPSink/Sack1/DelAck - Sack1 with DelAck


TWO NODES, ONE LINK


Two nodes 'n0' and 'n1'.The next line connects the two nodes. In this section we are going to define a very simple topology with two nodes that are connected by a link. The following two lines define the two nodes. (Note: We have to insert the code in this section before the line '$ns run', or even better, before the line '$ns at 5.0 "finish"').

                                          set n0 [$ns node]

                                          set n1 [$ns node]


 A new node object is created with the command '$ns node'. The above code creates two nodes and assigns them

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

This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth 1Megabit, a delay of 10ms and a DropTail queue.



                                   

                                                        A simple link


 SENDING DATA


The next step is to send some data from node n0 to node n1. In ns, data is always being sent from one 'agent' to another. So the next step is to create an agent object that sends data from node n0, and another agent object that receives the data on node n1.

Create a UDP agent and attach it to node n0

                              set udp0 [new Agent/UDP]

                              $ns attach-agent $n0 $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.005

                              $cbr0 attach-agent $udp0

These lines create a UDP agent and attach it to the node n0, then attach a CBR traffic generator to the UDP agent. CBR stands for 'constant bit rate'. The packetSize is being set to 500 bytes and a packet will be sent every 0.005 seconds (i.e. 200 packets per second). The next lines create a Null agent which acts as traffic sink and attach it to node n1.

                              set null0 [new Agent/Null]

                              $ns attach-agent $n1 $null0

Now the two agents have to be connected with each other.

                              $ns connect $udp0 $null0

And now we have to tell the CBR agent when to send data and when to stop sending. Note: It's probably best to put the following lines just before the line

                              $ns at 5.0 "finish"'.

                              $ns at 0.5 "$cbr0 start"

                              $ns at 4.5 "$cbr0 stop"

Now we can save the file and start the simulation again. When we click on the 'play' button in the nam window, we will see that after 0.5 simulation seconds, node 0 starts sending data packets to node 1. We might want to slow nam down then with the 'Step' slider.


                     . 

                                       

                                     Data routing between nodes




No comments:

Post a Comment