Introduction to ns-3
Published:
ns-3 is a discrete-event network simulator for Internet systems, targeted primarily for research and educational use. Researchers often need to test their theories on simulators before they try out on real hardware. ns-3 is a tool for works just like that. It is open-source and easily extensible for research works. In this article we will explore how we can get started and run a simple simulation on ns-3.
ns-3 can be downloaded from their official website. After downloading an archive, we can extract it and we can see the following files in the directory.
If we move inside the ns-3.40 directory, we can see the following structure:
Now we need to configure the simulator by running the following command:
./ns3 configure
Now we need to build the simulator that will compile all the source code and make the existing codes ready for us to use.
./ns3 build
Now, we will run our first simulation script. There are a plenty of examples that are shipped with ns-3. We will run one of them. First, we need to copy the file first.cc located in examples/tutorial to the scratch folder. Then we need to run the following command:
./ns3 run scratch/first
We can see the following output in our terminal:
Now, we will check the code and try to understand what the code is doing.
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1
// point-to-point
//
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");
int
main (int argc, char *argv[])
{
CommandLine cmd (__FILE__);
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
Code explanation:
- Here, we have two nodes communicating with each other as we can see from the output. So, here we create a NodeContainer that contains all the nodes in the simulation.
NodeContainer nodes;
nodes.Create (2);
- Now, we connect the nodes directly using a PointToPoint channel and set their attributes. Here we set the Data rate to 5 Mbps and the Delay 2 ms.
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
- We get the NetDevices when we install the PointToPoint channel on the nodes. Now, we set the Internet Stack on the nodes and set IP address for the Network Devices i.e. the network interface cards.
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
- Now, we set up one node as server and another node as client. Then we set the attribute of the application.
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
- As we are configuring an EchoServer and EchoClient, a node will send a data of 1024 bytes as configured and the other node will receive it and echo back the packet.
- To run the simulation, we add the following code. After the simulation is run, we need to clean up. So, we call the Destroy method.
Simulator::Run ();
Simulator::Destroy ();
This is a very basic introduction of how ns-3 simulation scripts work and how we can run a simulation script. There are lots of other functionality that we will explore later in future posts. Thanks for continuing till the end. Hopefully you got the basic idea of ns-3.