Introduction to ns-3
Simulation
September 6, 2021
Introduction to ns-3 Simulation 1
Introduction
► ns-3 is an open-source discrete event network
simulation tool
► Written in C + + . Can write scripts in C + + & python.
► Can simulate simple and complex networking scenarios.
Introduction to ns-3 Simulation 2
Installation - Requirements
► Installation instructions can be found on
https://www.nsnam.org/wiki/Installation
► Installation Prerequisites. The minimal requirement for ns-3 is to have
► Ubuntu/linux environment or a VM installed
► C + + & python3 is installed in the system.
$ sudo apt update
$ sudo apt install g++ python3 python3-dev python-dev pkg-config sqlite3 python3-setuptools git qt5-default gir1.2-
goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-
common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar openmpi-bin openmpi-common openmpi-doc
libopenmpi-dev tcpdump wireshark libxml2 libxml2-dev
Introduction to ns-3 Simulation 3
Installation of NS-3
► Download the ns-allinone
► Copy the file in home directory, extract.
► Go to ns-allinone folder : cd ns-allinone-3.34
► Build the modules
$ ./build.py --enable-examples --enable-tests
► Waf configure:
$./waf clean
$ ./waf configure --build-profile=optimized --enable-examples --enable-tests
$./waf configure --enable-sudo --enable-examples --enable-tests
► Run
./waf --run hello-simulator
Introduction to ns-3 Simulation 4
Installation - Downloading sources
► ns-3 uses a python tool called bake to download & install ns-
3
► Use git to obtain the bake tool.
git clone https://gitlab.com/nsnam/bake
► Change into the bake directory with cd bake
► Check if prerequisutes are met
./bake.py check
► If the python script fails due to missing python libraries, use pip to install them.
► pip is a python package-management system.
► Install pip with sudo apt-get install python3-pip
Introduction to ns-3 Simulation 5
Installation - Downloading sources
► To configure bake to download & install ns-3.31
run:
./bake.py configure -e ns-3.31
► Run ./bake.py show to see what bake will install & download.
► Download ns-3 along with other components
./bake.py download
► Build it
./bake.py build
Introduction to ns-3 Simulation 6
Installation - Run an example
► Once ns-3 download & build is finished, we need to run a configure option, then
build it.
cdsource/ns-3.31 #change into ns-3 directory
make configure # runs './waf configure --enable-examples --enable-tests'
./waf build
► Run an example.
./waf --run examples/tutorial/first
► All ns-3 simulations must be run using ./waf from the ns-3 root
directory.
Introduction to ns-3 Simulation 7
Somethings to know
► ns-3 C + + code must be stored in les with .cc extension. C + + header les must
have .h extension.
► ns-3 uses the g++ command to compile C + + code. You can configure ns-3 to
use another C + + compiler such as clang++. If you do that, the entire ns-3 code
must be compiled again.
CXX="clang++"./waf configure
► Simulations run from the ns-3 root directory.
► Typically, it would be called ns-3.30 (for version 3.30) or ns-3-dev for the
development version.
► If your simulation writes to les, they would be stored relative to this
directory.
Introduction to ns-3 Simulation 8
Understanding ns-3 directory structure
► The root ns-3 directory for version 3.30 is called ns-3.30
► Under the directory, there are several important folders.
► src this is the ns-3 directory where source code for modules is stores.
► examples contains simple ns-3 simulation examples.
► contrib Contributed modules are stored here. You do not need to do anything here
unless you are creating libraries for use in ns-3
► build this is the build directory for ns-3
► scratch this is where user programs are stored.
► In most directories, there's a l e named wscript, which is used by ./waf.
► Examine the examples/tutorial/wscript. You can now see why we used
first and not first.cc when we ran the example.
Introduction to ns-3 Simulation 9
Understanding ns-3 directory structure
► Whenever you run ./waf to build or run programs, all projects under scratch are
compiled. The command fails if any project has errors.
► ns-3 comes with many examples. Relative to ns-3 root directory, there are
examples under the examples directory. In addition, every module under src also
contains its own examples directory.
► To run the example ./examples/udp/udp-echo.cc run the following command
from ns-3 root directory:
./waf --run examples/udp/udp-echo
► The example does not print anything to the terminal, but it will create output les of
type .pcap and .tr
► To examine .pcap les, open them with a packet analyzer like Wireshark.
► the resulting .tr l e is an ASCII trace of the packets.
Introduction to ns-3 Simulation 10
ns-3 directory structure - Creating projects under scratch
► To create an ns-3 project, create a folder under scratch, let's say you called it
Project1.
► Under Project1, one .cc must contain a main function
► To run Project1, you need to be at the ns-3 root directory, and run
./waf --run Project1
To pass arguments, use quotation marks as follows
./waf –run "Project1 --p1=10 --p2=20"
Introduction to ns-3 Simulation 10
Major ns-3
components
Node NetDevice
MAC
DeviceList subclass of
Implemented in device or separate class
dev0 ...
dev1 PHY
Implemented in device or separate class
... ...
Uses sub classes of ns3::Channel
ApplicationList
app0
app1 subclass of ns3::Application
... + m_node: Ptr<Node>
+ StartApplication(): void
+ StopApplication():void
Extends
CustomApplication
+ StartApplication() : void
Introduction to ns-3 Simulation 11
Understanding to Code
September 20, 2021
Program Structure
Topology
Header Files
Namespace
int main (int argc, char
*argv{})
Variable
Declaration
Number of nodes-
NodeContainer
Topology
Helpers
Channel
Devices Application
The basic model
Application Application
Application Application
Sockets-like
API
Protocol Protocol
Packet(s) stack
stack
Node Node
NetDevice NetDevice
NetDevice Channel NetDevice
Channel
15
Coding Example
The first line in the file
The ns-3 simulator is licensed using the GNU General Public License.
16
Module Includes
The code proper starts with a number of include statements.
17
Ns3 Namespace
The next line in the first.cc script is a namespace declaration.
Logging
18
Main Functions
The next lines of the script you will find are,
1.NS_LOG_ERROR — Log error messages;
2.NS_LOG_WARN — Log warning messages;
3.NS_LOG_DEBUG — Log relatively rare, ad-hoc debugging messages;
4.NS_LOG_INFO — Log informational messages about program progress;
5.NS_LOG_FUNCTION — Log a message describing each function called;
6.NS_LOG_LOGIC – Log messages describing logical flow within a function;
7.NS_LOG_ALL — Log everything. 19
8.NS_LOG_UNCOND – Log the associated message unconditionally.
Topology Helpers
Create the ns-3 Node objects that will represent the computers in the simulation.
We are constructing a point to point link
20
Topology Helpers
We will need to have a list of all of the NetDevice objects that are created
21
Internet Stack Helper
Protocol stacks installed on our nodes
TCP, UDP, IP, etc.
Ipv4AddressHelper
make the association between an IP address and a device
22
Applications
Two specializations of the core ns-3 class Application called
UdpEchoServerApplication
UdpEchoClientApplication
UdpEchoServerHelper
23
Applications
UdpEchoClientHelper
24
Simulator
What we need to do at this point is to actually run the simulation.
scheduled events in the simulator at 1.0 seconds, 2.0 seconds and two events at 10.0 seconds
25
PCAP file
////////////////////////////////////////////////////////////////////////////////////////////////////
if (tracing == true)
{
pointToPoint.EnablePcapAll ("first");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
pointToPoint.EnablePcapAll ("p2p");// for point to point finding packet capture for all
// this pcap file will be opened using wireshark or tcpdump
csma.EnablePcap ("csma1", csmaDevices.Get (1), true);
csma.EnablePcap ("csma3", csmaDevices.Get (3), true);
////////////////to run//////////////////////////////////////////////////////////////////
TraceFile
Trace-metrics installation
sudo apt install default-jdk (for java installation)
Download tracemetrics and unzip to folder
/////////////////////////////////////code/////////////////////////////////
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("first.tr"));
//////////////////////////////////////////////////////////////////////////////////////////
pointToPoint.EnableAsciiAll(ascii.CreateFileStream("point2point.tr"));
csma.EnableAsciiAll(ascii.CreateFileStream("csma2.tr"));
/////////////////////////////////////////to run///////////////////////////////////////////////////
NetAnim
//////////////////////////////////////code//////////////////////
Header file
#include "ns3/netanim-module.h"
///////////////////////////////////////////////////////////////////////////
AnimationInterface anim("first.xml"); //added
anim.SetConstantPosition(nodes.Get(0),10.0,20.0);
anim.SetConstantPosition(nodes.Get(1),20.0,30.0);
//////////////////////////////////////////////////////////////////////////////////////
AnimationInterface anim("second.xml");
anim.SetConstantPosition(p2pNodes.Get(0),10.0,10.0); //either we can use p2p or csma nodes
10.0, 20.0.. are the position
anim.SetConstantPosition(p2pNodes.Get(1),20.0,20.0);
anim.SetConstantPosition(csmaNodes.Get(1),30.0,30.0);
anim.SetConstantPosition(csmaNodes.Get(2),40.0,40.0);
anim.SetConstantPosition(csmaNodes.Get(3),50.0,50.0);
NetAnim
/////////////////////////////////to run////////////////////////////////////////
Building Your Script
Drop your script into the scratch directory
Now build your first example script using waf
30
Building Your Script
You can now run the example
31
Building a Wireless Network Topology
Add the Wifi and the mobility modules
The network topology illustration follows:
32
Building a Wireless Network Topology
For enabling or disabling logging components and for changing the number of devices created
33
Building a Wireless Network Topology
Part of the Wifi network
Configure the PHY and channel helpers
Yet Another Network Simulator
M. Lacage and T. R. Henderson, “Yet Another Network Simulator,” WNS2 ns-2:
The IP Network Simulator, Italy, 2006.
http://cutebugs.net/files/wns2-yans.pdf
34
Building a Wireless Network Topology
Create a channel object and associate it to our PHY layer object manager
NqosWifiMacHelper object to set MAC parameters
rate control algorithm
AARF (Adaptive Auto-Rate Fallback ) algorithm
35
Building a Wireless Network Topology
The SSID of the infrastructure network
In Wait Beacon and Wait Probe Response, STA is gathering beacon or probe response packets from APs, resulted in a list of
candidate AP. After the respective timeout, it then tries to associate to the best AP (i.e., best SNR). STA will switch between the
two states and restart the scanning procedure if SetActiveProbing() called.
Create the wifi devices of these stations
36
Building a Wireless Network Topology
Configure the AP (access point) node
Shares the same set of PHY-level Attributes (and channel) as the stations
37
Building a Wireless Network Topology
Set some Attributes controlling the “position allocator” functionality
The number of objects layed
out on a line.
38
Building a Wireless Network Topology
Need to tell them how to move
RandomWalk2dMobilityModel
random direction
random speed
Rectangle (double _xMin, double _xMax, double _yMin, double _yMax)
39
Building a Wireless Network Topology
Want the access point to remain in a fixed position during the simulation
Protocol stacks
40
Building a Wireless Network Topology
assign IP addresses to the device interfaces
41
Building a Wireless Network Topology
Enable internetwork routing
Create just enough tracing to cover all three networks
42
Building a Wireless Network Topology
Run the simulation, clean up and then exit the program
Output
43
Codes (1/7) for 5 nodes
44
Codes (2/7)
45
Codes (3/7)
46
Codes (4/7)
47
Codes (5/7)
48
Codes (6/7)
49
Codes (7/7)
50