Friday, March 22, 2013

The NS-3 simulator

While the NS-2 is the most widely used simulator for research works in the literature, a newer simulator is now under rapid development. That is NS-3 simulator. Initially I thought NS-3 is a newer version of NS-2 but it is a misunderstanding. NS-3 is a complete new implementation from scratch to suit the requirements of modern network simulations.  It is said that NS-3 is more maintainable and easily extendable comparing to old NS-2 because of it's sophisticated design. Anyway in the future most probably NS-2 will go deprecated and NS-3 simulator will take the lead. Therefore it is better to have some hands on experiences in NS-3 simulator.

To try NS-3 simulator, I installed it on Ubuntu 12.04 LTS and tried some example applications. Since NS-3 is written completely on C++, it seems it is convenient to read though the source code and explore it's implementation comparing to NS-2 which is implemented using C++ and OTcl. In the following paragraphs I'm writing down the steps I followed to install and try NS-3 simulator.
Figure - 1











Download the latest release of NS-3 as a tarball file from the link - http://www.nsnam.org/releases/. Uncompress it to a new directory in your home directory and go in to it from a terminal. In the content of the uncompressed file you should see the file named as build.py which is going to be used for building our NS-3 source code.

Issue the following command to install some necessary packages before we build NS-3.

    sudo apt-get install build-essential

Then issue the following command to build NS-3.

    ./build.py --enable-examples --enable-tests

Now some processing will take place and finally it should say that 'build' finished successfully. Now move it to the ns-3.1x directory inside the uncompressed directory where you would find the file test.py. This script will be used to run some tests on NS-3 to make sure all the modules are working as expected. So, issued the following command to run those tests.

     ./test.py -c core

 After completing those tests successfully, we can try some example scripts in NS-3 to learn how to build and run scripts in NS-3. So, while staying in the  ns-3.1x directory, issue the following command to copy the hello-simulator script to a new place where we will try those examples.

    cp examples/tutorial/hello-simulator.cc scratch/myhello.cc

Now issue the following commands to build and run this script.

    ./waf

    ./waf --run myhello

So, in the terminal you should see the output of this myhello script as Hello Simulator. That is the very first example we ran on NS-3. Now issue the following command to copy another example scripts to our working place.

    cp examples/tutorial/first.cc scratch/my-point-to-point.cc

This example script creates a sever and a client in the simulation and send a packet from the client to the server. Server replies with the echo of the same packet it received from the client. If you open the file my-point-to-point.cc and it won't be hard to understand how it works.

1:  #include "ns3/core-module.h"  
2:  #include "ns3/network-module.h"  
3:  #include "ns3/internet-module.h"  
4:  #include "ns3/point-to-point-module.h"  
5:  #include "ns3/applications-module.h"  
6:  using namespace ns3;  
7:  NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");  
8:  int  
9:  main (int argc, char *argv[])  
10:  {  
11:   LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);  
12:   LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);  
13:   NodeContainer nodes;  
14:   nodes.Create (2);  
15:   PointToPointHelper pointToPoint;  
16:   pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));  
17:   pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));  
18:   NetDeviceContainer devices;  
19:   devices = pointToPoint.Install (nodes);  
20:   InternetStackHelper stack;  
21:   stack.Install (nodes);  
22:   Ipv4AddressHelper address;  
23:   address.SetBase ("10.1.1.0", "255.255.255.0");  
24:   Ipv4InterfaceContainer interfaces = address.Assign (devices);  
25:   UdpEchoServerHelper echoServer (9);  
26:   ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));  
27:   serverApps.Start (Seconds (1.0));  
28:   serverApps.Stop (Seconds (10.0));  
29:   UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);  
30:   echoClient.SetAttribute ("MaxPackets", UintegerValue (1));  
31:   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));  
32:   echoClient.SetAttribute ("PacketSize", UintegerValue (1024));  
33:   ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));  
34:   clientApps.Start (Seconds (2.0));  
35:   clientApps.Stop (Seconds (10.0));  
36:   Simulator::Run ();  
37:   Simulator::Destroy ();  
38:   return 0;  
39:  }  

This simulation creates a topology as shown in the Figure-1. You can run this application similarly to the previous example by running the following commands.

    ./waf
    ./waf --run scratch/my-point-to-point

That's it. This is the beginning and I need to learn more things in NS-3 simulator in the near future. Most importantly I need to explore how we use it to simulate wireless networks.

No comments:

Post a Comment