0% found this document useful (0 votes)
38 views18 pages

LAB1 - Introduction To ns-3

Uploaded by

Abir Abir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views18 pages

LAB1 - Introduction To ns-3

Uploaded by

Abir Abir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LAB1 - Introduction to

ns-3
Réseaux Mobiles
M2 RSD
Pr. Nabila LABRAOUI
Department of Computer Science
University of Tlemcen

2024-2025
What is NS-3?

A Discrete-event network simulator ,


targeted primarily for research and
educational
 Open Source: ns-3 is free and publicly
available for use
 Collection of C++ libraries, not a program :
ns-3 is written entirely with C++ while
Python user code wrappers are available.
 Support under Linux, FreeBSD and Cygwin
Key Terms and Abstractions

 Node - the hardware (eg. router, PC, phone)


 Network device (ND) - transmits and receives over the
channel
 Channel - transmission medium between NDs (eg. WiFi,
ethernet)
 Application - creates or receives data sent between nodes
 Helper - is a utility class that simplifies the creation,
configuration, and management of network objects (such as
nodes, devices, and applications) by abstracting the
complexity of low-level APIs.
Architecture
Architecture

 Just like TCP/IP stacks


 Applications running in a node use Protocol Stack
(TCP, UDP, IP, etc)
 Protocol Stack sends packets to NetDevices ( or
Network interfaces / adapters)
 Each NetDevice interfaces with each Channel (WiFi,
LTE, etc.)
Installing ns3 (1)
1) Download and install VirtualBox from the download page :
https://www.virtualbox.org/wiki/Downloads
2) Download an Ubuntu Image (the latest version is 24.04 LTS from :
https://ubuntu.com/download/desktop
More details on configuration in this website :
https://ubuntu.com/tutorials/how-to-run-ubuntu-desktop-on-a-virtual-ma
chine-using-virtualbox#1-overview

3) Installation of ns-3.42 on Ubuntu 24.04 LTS


 Download ns-allinone-3.42.tar.bz2 from
https://www.nsnam.org/releases/ns-allinone-3.42.tar.bz2 and store it in
the home folder, Extract the archive
 Open a new terminal
Installing ns3 (2)
 $ sudo apt update
 $ sudo apt install g++ python3 cmake ninja-build git
gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-
pygraphviz gir1.2-gtk-3.0 ipython3 tcpdump wireshark
sqlite sqlite3 libsqlite3-dev qtbase5-dev qtchooser qt5-
qmake qtbase5-dev-tools openmpi-bin openmpi-common
openmpi-doc libopenmpi-dev doxygen graphviz imagemagick
python3-sphinx dia imagemagick texlive dvipng latexmk
texlive-extra-utils texlive-latex-extra texlive-font-utils
libeigen3-dev gsl-bin libgsl-dev libgslcblas0 libxml2
libxml2-dev libgtk-3-dev lxc-utils lxc-templates vtun uml-
utilities ebtables bridge-utils libxml2 libxml2-dev
libboost-all-dev ccache
Installing ns3 (3)
 Now we have to go to the installation.
 Open a new Terminal and type the following commands.
 $ cd ns-allinone-3.42/
 $ ./build.py --enable-examples --enable-tests
 Depending on your system, the time can be anywhere between 20 minutes
to sometimes 1 hour
 To check whether they are working, use the following command
 Open a new terminal
 $ cd ns-allinone-3.42/ns-3.42/
 $ ./ns3 run hello-simulator
 If you see Hello Simulator, congratulations! You have environments ready
for running ns-3
The first Script
 Download vim editor (you can use Eclipse IDE)
 Sudo apt install vim
 vim examples/tutorial/first.cc

 This first.cc script configures a simple network with two


nodes linked by a point-to-point link. The script
implements client-server communication using the
UdpEchoServer and UdpEchoClient applications.
Script header
 Add module header files
#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"
// These instructions include the necessary NS3 modules
 Namespace
using namespace ns3; // Eliminates the need to write ns3:: before
each NS3 class or function.
 Create log components
NS LOG COMPONENT DEFINE ("FirstScriptExample");
// declare a logging component in NS-3 for your script
More headers

 Set time resolution


Time::SetResolution (Time::NS);
// Sets time resolution to the nanosecond (NS). NS3 uses different time
units (Time::NS, Time::MS, Time::S, etc.).
 Enable log components and set log levels
LogComponentEnable ("UdpEchoClientApplication", LOG LEVEL INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG LEVEL INFO);
// enables logging for a specific component in the NS-3 script
// It allows you to see informative messages related to sending packets
and establishing connections,…
Creating topology
 Create nodes
NodeContainer nodes;
nodes.Create (2);
 Create Channel
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
// Configure the point-to-point link between nodes
 Create NetDevice and bind them to Channel
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
Protocol Stack
 Create InternetStack
InternetStackHelper stack;
stack.Install (nodes);
// installation of the Internet protocol stack (TCP/IP) on nodes
 Set IP network address
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
 Assign IP to NetDevice
Ipv4InterfaceContainer interfaces = address.Assign (devices);
Building UDP Echo Server

 Create echo server


UdpEchoServerHelper echoServer (9);
 Install echo server to node 1, mark it application, and set start and stop time
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
 Create echo client and set its attributes
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
// Port number of server: 9
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
// the client will send exactly 1 UDP packet during the simulation.
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
Building UDP Echo Server

 Install echo client to node 0, mark it application, and set start


and stop time
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0));
 Run, destroy, return
Simulator::Run ();
Simulator::Destroy ();
return 0;
int main (int argc, char *argv[])
{
// Step 1: Create and configure nodes
NodeContainer nodes; nodes.Create (2);
// Step 2: Set up point-to-point link attributes
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
// Step 3: Install devices on nodes
NetDeviceContainer devices; devices = pointToPoint.Install (nodes);
// Step 4: Install Internet stack on nodes
InternetStackHelper stack; stack.Install (nodes);
// Step 5: Assign IP addresses to nodes
Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
// Step 6: Set up an application on Node 1 (server)
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
// Step 7: Set up a client application on Node 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));
// Step 8: Start the simulation
Simulator::Run ();
// Step 9: Clean up after simulation ends
Simulator::Destroy (); return 0;
}
Work to do

 Increase packet sizes 4K, 16K, 64K


 Have the client send the packets every one second
for 4 packets
 Double data link rate Double data link delay
 Increase the number of echo clients to 2, 3, 4, 5 and
have them send packets to the server

You might also like