NS3-Installation and Coding
NS3-Installation and Coding
Simulation
September 6, 2021
$ 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
./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
► Run an example.
► 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
► 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
dev0 ...
dev1 PHY
Implemented in device or separate class
... ...
Uses sub classes of ns3::Channel
ApplicationList
app0
app1 subclass of ns3::Application
+ StartApplication(): void
+ StopApplication():void
Extends
CustomApplication
+ StartApplication() : void
Header Files
Namespace
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 ns-3 simulator is licensed using the GNU General Public License.
16
Module Includes
17
Ns3 Namespace
Logging
18
Main Functions
Create the ns-3 Node objects that will represent the computers in the simulation.
20
Topology Helpers
We will need to have a list of all of the NetDevice objects that are created
21
Internet Stack Helper
22
Applications
UdpEchoServerHelper
23
Applications
UdpEchoClientHelper
24
Simulator
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
30
Building Your Script
31
Building a Wireless Network Topology
Add the Wifi and the mobility modules
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
34
Building a Wireless Network Topology
Create a channel object and associate it to our PHY layer object manager
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.
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
38
Building a Wireless Network Topology
Need to tell them how to move
RandomWalk2dMobilityModel
random direction
random speed
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
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