0% found this document useful (0 votes)
127 views47 pages

NS-3 Simulator Basics

NS-3 is a discrete event network simulator developed for network research and education. It is written in C++ and has bindings available in Python. NS-3 uses a modular design that allows it to easily extend and add new modules. Simulation scripts are written in C++ and use the NS-3 API to configure nodes, network devices, applications and run the simulation. Key aspects include nodes, network devices like point-to-point, CSMA, WiFi, applications like UDP client/server, and routing protocols.

Uploaded by

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

NS-3 Simulator Basics

NS-3 is a discrete event network simulator developed for network research and education. It is written in C++ and has bindings available in Python. NS-3 uses a modular design that allows it to easily extend and add new modules. Simulation scripts are written in C++ and use the NS-3 API to configure nodes, network devices, applications and run the simulation. Key aspects include nodes, network devices like point-to-point, CSMA, WiFi, applications like UDP client/server, and routing protocols.

Uploaded by

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

NS-3 Simulator Basics

NS-3 is a network simulator

Developed for network research and education

Developed after ns-2

ns-3 is written in C++

Bindings in Pthon

ns-3 uses the waf !uild sstem

simulation programs are C++ e"ecuta!les or


pthon scripts

#eatures

$t it a discrete event simulator

%odular design & 'pen source

(ctivel developed )Contrast NS-2*

Developed in C+++ Pthon !inding availa!le+

,ive visuali-er

,ogging facilit for de!ugging

.racing facilit fo getting output

Can !e connected to a real network

Direct Code /"ecution )DC/*



Discrete /vent Simulator

Simulation time moves discretel from event to


event

Schedule events to occur at specific simulation


times

( simulation scheduler orders the event e"ecution

Simulator e"ecute events one ! one

Simulation stops at specific time or when events


end

$nstallation

Primar site www+nsnam+org

,atest release 3+01

date-driven schedules

3-2 months ccle

Source download link3-


https3&&www+nsnam+org&release&ns-allinone-3+01+tar+!-2

Documentation download link3-


http3&&www+nsnam+org&ns-3-01&documentation

http3&&www+nsnam+org&docs&release&3+01&tutorial&n
s-3-tutorial+pdf

$nstallation

Download the source tar file

/"tract the tar file )( tour of src tree*

$nstall c++4 pthon

#rom command line


5cd nsallinone-3+01
5+&!uild+p --ena!le-e"amples 6ena!le-tests

7af3 ,eaving director


8&path&to&workspace&ns-allinone-3+01&ns-3+01&!uil
d9 9!uild9 finished successfull ):m2;+<32s*

$nstallation

$f using =!untu or De!ian4 there is a package


named ns3+ $nstall this package+ >ou ma need
additional packages+ Serach ns3 in Snaptic
package manager and install ns34 li!ns3-34
li!ns3-dev4 ns3-doc4 pthon-ns3

'ther packages ma !e re?uired for additional


features

@un a Script
.o test the installation cop one e"ample
availa!le in the distri!ution to scratch director
and !uild and run the same using the commands
!elow3 -
5cd ns-3+01
5cp e"amples&tutorial&first+cc scratch&first0+cc
5waf 6run first0

'utput
7af3 /ntering director
A&home&a!hiBit&workspace&ns-allinone-3+01&ns-3+01&!uildC
7af3 ,eaving director
A&home&a!hiBit&workspace&ns-allinone-3+01&ns-3+01&!uildC
C!uildC finished successfull )0+<<:s*
(t time 2s client sent 0<22 !tes to 0<+0+0+2 port D
(t time 2+<<3:Ds server received 0<22 !tes from 0<+0+0+0 port
2D0;3
(t time 2+<<3:Ds server sent 0<22 !tes to 0<+0+0+0 port 2D0;3
(t time 2+<<E3Es client received 0<22 !tes from 0<+0+0+2 port D

'rgani-ation
Fe (!stractions3 -

Node

(pplication

Net device

Channel
Corresponding to these a!stractions there are
modules ).here are other modules for other
functionalities*

%odules
Different entities are implemented in different
modules+ /ach modules has C(ttri!utesC+
#unctionalities of modules are provided !
different C++ mem!er functions+ .here is helper
(P$ availa!le with each module+ .hese helper
methods makes programming easier+ ,ow level
(P$ can still !e used+

$t is eas e"tend module to add new


features&functionalities

$t is eas to add new module



Structure of NS-3 script
#or simulation4 we need to write a simulation
script which is a C++ program+ .o this program
the ns-3 li!rar is linked to !uild our simulation
e"ecuti!le+ (P$ calls are used in the program
to do the necessar simulation+ .he waf !uild
sstem is used to !uild the simulation+

Steps in writing scripts

$nclude necessar files

=se appropriate name space

Set simulation time resolution)'ptional*

/na!le logging for different modules)'ptional*

Create nodes

Create net devices with %(C and PG>

(ttach Net devices to nodes and set


interconnections

Steps in writing scripts

$nstall protocol stack in nodes

Set network address for interfaces

Setup routing

$nstall applications in nodes

Setup tracing

Set application start and stop time

Set simulation start time

@un simulation

@elease resources at end of simulation



/"ample
( simple point-to-point link connecting two
hosts+ ( client application is instaled in one and
a server application in the other+
Hinclude Ins3&core-module+hI
Hinclude Ins3&network-module+hI
Hinclude Ins3&internet-module+hI
Hinclude Ins3&point-to-point-module+hI
Hinclude Ins3&applications-module+hI
using namespace ns3J

/"ample
$nt main )int argc4 char KargvLM* N
.ime33Set@esolution ).ime33NS*J
,ogComponent/na!le )I=dp/choClient(pplicationI4
,'OP,/Q/,P$N#'*J
,ogComponent/na!le )I=dp/choServer(pplicationI4
,'OP,/Q/,P$N#'*J

/"ample
NodeContainer nodesJ
nodes+Create )2*J
Point.oPointGelper point.oPointJ
point.oPoint+SetDevice(ttri!ute )IData@ateI4
StringQalue )I;%!psI**J
point.oPoint+SetChannel(ttri!ute)IDelaI4
StringQalue )I2msI**J
NetDeviceContainer devicesJ
devices R point.oPoint+$nstall )nodes*J

/"ample
$nternetStackGelper stackJ
stack+$nstall )nodes*J
$pv2(ddressGelper addressJ
address+SetBase )I0<+0+0+<I4 I2;;+2;;+2;;+<I*J
$pv2$nterfaceContainer interfaces R
address+(ssign )devices*J

/"ample
=dp/choServerGelper echoServer )D*J
(pplicationContainer server(pps R
echoServer+$nstall )nodes+Oet )0**J
server(pps+Start )Seconds )0+<**J
server(pps+Stop )Seconds )0<+<**J

/"ample
=dp/choClientGelper echoClient )interfaces+Oet(ddress )0*4 D*J
echoClient+Set(ttri!ute )I%a"PacketsI4 =integerQalue )0**J
echoClient+Set(ttri!ute )I$ntervalI4 .imeQalue )Seconds )0+<***J
echoClient+Set(ttri!ute )IPacketSi-eI4 =integerQalue )0<22**J
(pplicationContainer client(pps R echoClient+$nstall )nodes+Oet )<**J
client(pps+Start )Seconds )2+<**J
client(pps+Stop )Seconds )0<+<**J

/"ample
Simulator33@un )*J
Simulator33Destro )*J
return <J
S

Conversion
Conversion: -
(Serialize De serialize, refcount)
StringValue IntegerValue UintegerValue
BooleanValue PointerValue TimeValue
numValue !""ressValue I#v$!""ressValue
I#v$%as&Value %ac'(!""ressValue
%ac$)!""ressValue

,ist of %odules
(odv applications !ridge click
Config-store core csma
csma-laout dsdv /mu energ
flow-monitor internet
,te mesh mo!ilit mpi
netanim network

,ist of %odules
Ni"-vector-routing ns3tcp ns3wifi
olsr openflow point-to-point
point-to-point-laout propagation
spectrum stats tap-!ridge test
tools topolog-read uan
virtual-net-device visuali-er
wifi wima"


Net Devices

(lohaNoackNetDevice

Point.oPoint

Bridge

Csma

7ifi

7i%a"

BaseStationNetDevice

Su!scri!erStation

'pen#lowSwitch

,oopBack

Simple

Net Devices

Qirtual )for tunneling*

/mu )/mulator net device4 deprecated*

#d )#ile descriptor*

.ap

=an )=nderwater (coustic*

,te ),ong .erm /volution*

#ake

NonCommunicating

/rror

(pplications
!ulk-send )sends data as fast as possi!le*
on-off )'n 'ff pattern*
packet-sink
udp-server )@eceives =DP packets*
udp-client )=DP packet with se? no and time stamp*
udp-echo-server
udp-echo-client
v2ping )sends one $C%P /CG' re?uest4 reports the @..*
ping:
radv)@outer advertisement daemon*

@outing protocols

I#v$Static*outing (unicast an" multicast)

IPv$ +#timize" ,in& State *outing (+,S*)

IPv$ !" -oc +n Deman" Distance Vector


(!+DV)

IPv$ Destination Se.uence" Distance


Vector (DSDV)

Dnamic Source @outing )DS@*



@outing protocols

#v$,ist*outing (use" to store a #rioritize"


list of routing #rotocols)

I#v$/lo0al*outing (use" to store routes


com#ute" 01 t2e glo0al route manager)

I#v$3i4Vector*outing (a more efficient


version of glo0al routing)

I#v(,ist*outing (use" to store a #rioritize"


list of routing #rotocols)

I#v(Static*outing

/"ample 2
0<+0+0+<
n< -------------- n0 n2 n3 n2
point-to-point T T T T
RRRRRRRRRRRRRRRR
,(N 0<+0+2+<

int main )int argc4 char KargvLM* N
!ool ver!ose R trueJ
uint32Pt nCsma R 3J
Command,ine cmdJ
cmd+(ddQalue )InCsmaI4 INum!er of I4 nCsma*J
cmd+(ddQalue )Iver!oseI4 I,og if trueI4 ver!ose*J
cmd+Parse )argc4argv*J
nCsma R nCsma RR < U 0 3 nCsmaJ
&& +&waf --run Isecond --nCsmaR3I

NodeContainer p2pNodesJ
p2pNodes+Create )2*J
NodeContainer csmaNodesJ
csmaNodes+(dd )p2pNodes+Oet )0**J
csmaNodes+Create )nCsma*J
Point.oPointGelper point.oPointJ
point.oPoint+SetDevice(ttri!ute
)IData@ateI4 StringQalue )I;%!psI**J
point.oPoint+SetChannel(ttri!ute
)IDelaI4 StringQalue )I2msI**J
NetDeviceContainer p2pDevicesJ
p2pDevices R point.oPoint+$nstall )p2pNodes*J

CsmaGelper csmaJ
csma+SetChannel(ttri!ute
)IData@ateI4 StringQalue )I0<<%!psI**J
csma+SetChannel(ttri!ute
)IDelaI4 .imeQalue )NanoSeconds ):;:<***J
NetDeviceContainer csmaDevicesJ
csmaDevices R csma+$nstall )csmaNodes*J
$nternetStackGelper stackJ
stack+$nstall )p2pNodes+Oet )<**J
stack+$nstall )csmaNodes*J

$pv2(ddressGelper addressJ
address+SetBase )I0<+0+0+<I4 I2;;+2;;+2;;+<I*J
$pv2$nterfaceContainer p2p$nterfacesJ
p2p$nterfaces R address+(ssign )p2pDevices*J
address+SetBase )I0<+0+2+<I4 I2;;+2;;+2;;+<I*J
$pv2$nterfaceContainer csma$nterfacesJ
csma$nterfaces R address+(ssign)csmaDevices*J

=dp/choServerGelper echoServer )D*J
(pplicationContainer server(pps R
echoServer+$nstall )csmaNodes+Oet )nCsma**J
server(pps+Start )Seconds )0+<**J
server(pps+Stop )Seconds )0<+<**J

=dp/choClientGelper echoClient
)csma$nterfaces+Oet(ddress )nCsma*4 D*J
echoClient+Set(ttri!ute
)I%a"PacketsI4 =integerQalue )0**J
echoClient+Set(ttri!ute
)I$ntervalI4 .imeQalue )Seconds )0+<***J
echoClient+Set(ttri!ute
)IPacketSi-eI4 =integerQalue )0<22**J
(pplicationContainer client(pps R
echoClient+$nstall )p2pNodes+Oet )<**J
client(pps+Start )Seconds )2+<**J
client(pps+Stop )Seconds )0<+<**J

$pv2Olo!al@outingGelper33
[email protected]!les )*J
point.oPoint+/na!lePcap(ll )IsecondI*J
csma+/na!lePcap
)IsecondI4 csmaDevices+Oet )0*4 true*J
Simulator33@un )*J
Simulator33Destro )*J
return <J
S

/"ample 3
7ifi 0<+0+3+<
(P
K K K K
T T T T 0<+0+0+<
n; n: nE n< -------------- n0 n2 n3 n2
point-to-point T T T T
RRRRRRRRRRRRRRRR
,(N 0<+0+2+<

NodeContainer p2pNodesJ
p2pNodes+Create )2*J
Point.oPointGelper point.oPointJ
point.oPoint+SetDevice(ttri!ute )IData@ateI4
StringQalue )I;%!psI**J
point.oPoint+SetChannel(ttri!ute )IDelaI4
StringQalue )I2msI**J
NetDeviceContainer p2pDevicesJ
p2pDevices R point.oPoint+$nstall )p2pNodes*J

NodeContainer csmaNodesJ
csmaNodes+(dd )p2pNodes+Oet )0**J
csmaNodes+Create )nCsma*J
CsmaGelper csmaJ
csma+SetChannel(ttri!ute
)IData@ateI4 StringQalue )I0<<%!psI**J
csma+SetChannel(ttri!ute
)IDelaI4 .imeQalue )NanoSeconds ):;:<***J
NetDeviceContainer csmaDevicesJ
csmaDevices R csma+$nstall )csmaNodes*J

NodeContainer wifiStaNodesJ
wifiStaNodes+Create )n7ifi*J
NodeContainer wifi(pNode R p2pNodes+Oet )<*J
>ans7ifiChannelGelper channel R
>ans7ifiChannelGelper33Default )*J
>ans7ifiPhGelper ph R >ans7ifiPhGelper33Default )*J
ph+SetChannel )channel+Create )**J
7ifiGelper wifi R 7ifiGelper33Default )*J
wifi+Set@emoteStation%anager )Ins333(arf7ifi%anagerI*J
N?os7ifi%acGelper mac R N?os7ifi%acGelper33Default )*J

Ssid ssid R Ssid )Ins-3-ssidI*J
mac+Set.pe )Ins333Sta7ifi%acI4
ISsidI4 SsidQalue )ssid*4
I(ctivePro!ingI4 BooleanQalue )false**J
NetDeviceContainer staDevicesJ
staDevices R wifi+$nstall )ph4 mac4 wifiStaNodes*J
mac+Set.pe )Ins333(p7ifi%acI4
ISsidI4 SsidQalue )ssid**J
NetDeviceContainer apDevicesJ
apDevices R wifi+$nstall )ph4 mac4 wifi(pNode*J


%o!ilitGelper mo!ilitJ
mo!ilit+SetPosition(llocator )Ins333OridPosition(llocatorI4
I%inVI4 Dou!leQalue )<+<*4
I%in>I4 Dou!leQalue )<+<*4
IDeltaVI4 Dou!leQalue );+<*4
IDelta>I4 Dou!leQalue )0<+<*4
IOrid7idthI4 =integerQalue )3*4
I,aout.peI4 StringQalue )I@ow#irstI**J
mo!ilit+Set%o!ilit%odel )Ins333@andom7alk2d%o!ilit%odelI4
IBoundsI4 @ectangleQalue )@ectangle )-;<4 ;<4 -;<4 ;<***J
mo!ilit+$nstall )wifiStaNodes*J
mo!ilit+Set%o!ilit%odel )Ins333ConstantPosition%o!ilit%odelI*J
mo!ilit+$nstall )wifi(pNode*J

$nternetStackGelper stackJ
stack+$nstall )csmaNodes*J stack+$nstall )wifi(pNode*J
stack+$nstall )wifiStaNodes*J
$pv2(ddressGelper addressJ
address+SetBase )I0<+0+0+<I4 I2;;+2;;+2;;+<I*J
$pv2$nterfaceContainer p2p$nterfacesJ
p2p$nterfaces R address+(ssign )p2pDevices*J
address+SetBase )I0<+0+2+<I4 I2;;+2;;+2;;+<I*J
$pv2$nterfaceContainer csma$nterfacesJ
csma$nterfaces R address+(ssign )csmaDevices*J
address+SetBase )I0<+0+3+<I4 I2;;+2;;+2;;+<I*J
address+(ssign )staDevices*J address+(ssign )apDevices*J


=dp/choServerGelper echoServer )D*J
(pplicationContainer server(pps R
echoServer+$nstall )csmaNodes+Oet )nCsma**J
server(pps+Start )Seconds )0+<**J
server(pps+Stop )Seconds )0<+<**J
=dp/choClientGelper echoClient
)csma$nterfaces+Oet(ddress )nCsma*4 D*J
echoClient+Set(ttri!ute )I%a"PacketsI4 =integerQalue )0**J
echoClient+Set(ttri!ute )I$ntervalI4 .imeQalue )Seconds )0+<***J
echoClient+Set(ttri!ute )IPacketSi-eI4 =integerQalue )0<22**J
(pplicationContainer client(pps R
echoClient+$nstall )wifiStaNodes+Oet )n7ifi - 0**J
client(pps+Start )Seconds )2+<**J client(pps+Stop )Seconds )0<+<**J

$pv2Olo!al@[email protected]!les )*J
Simulator33Stop )Seconds )0<+<**J
point.oPoint+/na!lePcap(ll )IthirdI*J
ph+/na!lePcap )IthirdI4 apDevices+Oet )<**J
csma+/na!lePcap )IthirdI4 csmaDevices+Oet )<*4 true*J
Simulator33@un )*J
Simulator33Destro )*J
return <J

Thanks
(If you are still listning)

It's Demo Time

You might also like