Sunday, June 12, 2016

Simulation using NS2

Let's discuss how we can simulate TCP using NS2 (Network Simulator) in Ubuntu environment.

First you need to install NS2 and nam on your machine. Use following terminal commands to install them.

Install NS2
 sudo apt-get install ns2
Install nam
 sudo apt-get install nam

Sometimes installing nam using this method leads to segmentation fault (figure 1). You can use following command list instead of using apt-get.
                       
1. sudo apt-get purge nam
2. 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"
3. unzip nam_1.14_amd64.zip
4. sudo dpkg -i nam_1.14_amd64.deb
5. sudo apt-mark hold nam


Figure 1
You can see the full code in Figure 2 below. Let's go through this .tcl code.

Create Node Structure

- Create ns object
- Give colors to 2 different data flows - This is for easiness of understanding data, flawing from different TCP nodes. Here I have given Red and Blue.
- Open nam trace file - set <File Name> [open <File to be created>.nam <read/write>]
- Create nodes - In this example I have created 6 nodes since I have used 2 as routers and 4 as nodes.
- Create links between nodes - $<ns object> <link type> <node> <node> <data rate> <latency> <data dropping type>
- Queue limit - Give a size to the queue
- Organize links - This gives locations to nodes in the graph.
- Create TCP object and attach it to node created in ns object
- Create sink object and attach it to node created in ns object
- Creating TCP object and attaching it to a node is not enough.Need to create a object which can send data(here i have used ftp). Then attach it to node attached with TCP.

Record Process

If we need to plot recorded data rate over time we can use a graph. For this I'm using "xgraph"
Install xgraph using following command.
        sudo apt-get install xgraph

Here we record all data received by sink1 and write them to f0 file with time. Then give that file to xgraph to plot. "record" function in below code do this.

Simulation

- Start process - Start ftp. Then they will start sending data - $<ns object> at <starting time> "ftp1 start"
- Finish process by executing nam <created namfile>
- finish function executes simulation as well as plotting the graph.

$ns at 0.0 "record" - start record function at 0.0ms time
$ns at 60.0 "finish" - start finish process st 60ms time
$ns run - run the code



figure