0% found this document useful (0 votes)
226 views5 pages

NS2 Simple Simulation Example

This document summarizes a simple NS network simulation script. The script creates a network with 4 nodes connected by links with varying bandwidths and delays. It attaches TCP and UDP agents to nodes and connects them. It schedules the start and stop of FTP and CBR traffic generators. Running the simulation configures the network, generates and transmits packets between agents, and records traces for analysis in NAM.

Uploaded by

pradeep
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)
226 views5 pages

NS2 Simple Simulation Example

This document summarizes a simple NS network simulation script. The script creates a network with 4 nodes connected by links with varying bandwidths and delays. It attaches TCP and UDP agents to nodes and connects them. It schedules the start and stop of FTP and CBR traffic generators. Running the simulation configures the network, generates and transmits packets between agents, and records traces for analysis in NAM.

Uploaded by

pradeep
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

SimpleSimulationExample

ThissectionshowsasimpleNSsimulationscriptandexplainswhateachlinedoes.Example3isan
OTclscriptthatcreatesthesimplenetworkconfigurationandrunsthesimulationscenarioinFigure4.
Torunthissimulation,download"nssimple.tcl"andtype"nsnssimple.tcl"atyourshellprompt.

Figure4.ASimpleNetworkTopologyandSimulationScenario
Thisnetworkconsistsof4nodes(n0,n1,n2,n3)asshowninabovefigure.Theduplexlinksbetween
n0andn2,andn1andn2have2Mbpsofbandwidthand10msofdelay.Theduplexlinkbetweenn2
andn3has1.7Mbpsofbandwidthand20msofdelay.EachnodeusesaDropTailqueue,ofwhichthe
maximumsizeis10.A"tcp"agentisattachedton0,andaconnectionisestablishedtoatcp"sink"
agentattachedton3.Asdefault,themaximumsizeofapacketthata"tcp"agentcangenerateis
1KByte.Atcp"sink"agentgeneratesandsendsACKpacketstothesender(tcpagent)andfreesthe
receivedpackets.A"udp"agentthatisattachedton1isconnectedtoa"null"agentattachedton3.A
"null"agentjustfreesthepacketsreceived.A"ftp"anda"cbr"trafficgeneratorareattachedto"tcp"
and"udp"agentsrespectively,andthe"cbr"isconfiguredtogenerate1KBytepacketsattherateof1
Mbps.The"cbr"issettostartat0.1secandstopat4.5sec,and"ftp"issettostartat1.0secandstop
at4.0sec.


Example3.ASimpleNSSimulationScript
Thefollowingistheexplanationofthescriptabove.Ingeneral,anNSscriptstartswithmakinga
Simulatorobjectinstance.
setns[newSimulator]:generatesanNSsimulatorobjectinstance,andassignsittovariablens
(italicsisusedforvariablesandvaluesinthissection).Whatthislinedoesisthefollowing:

Initializethepacketformat(ignorethisfornow)
Createascheduler(defaultiscalendarscheduler)
Selectthedefaultaddressformat(ignorethisfornow)

The"Simulator"objecthasmemberfunctionsthatdothefollowing:

Createcompoundobjectssuchasnodesandlinks(describedlater)
Connectnetworkcomponentobjectscreated(ex.attachagent)
Setnetworkcomponentparameters(mostlyforcompoundobjects)
Createconnectionsbetweenagents(ex.makeconnectionbetweena"tcp"and"sink")
SpecifyNAMdisplayoptions
Etc.

Mostofmemberfunctionsareforsimulationsetup(referredtoasplumbingfunctionsinthe
Overviewsection)andscheduling,howeversomeofthemarefortheNAMdisplay.The
"Simulator"objectmemberfunctionimplementationsarelocatedinthe"ns2/tcl/lib/nslib.tcl"
file.

$nscolorfidcolor:istosetcolorofthepacketsforaflowspecifiedbytheflowid(fid).This
memberfunctionof"Simulator"objectisfortheNAMdisplay,andhasnoeffectontheactual
simulation.

$nsnamtraceallfiledescriptor:Thismemberfunctiontellsthesimulatortorecordsimulation
tracesinNAMinputformat.Italsogivesthefilenamethatthetracewillbewrittentolaterby
thecommand$nsflushtrace.Similarly,thememberfunctiontraceallisforrecordingthe
simulationtraceinageneralformat.


procfinish{}:iscalledafterthissimulationisoverbythecommand$nsat5.0"finish".Inthis
function,postsimulationprocessesarespecified.

setn0[$nsnode]:Thememberfunctionnodecreatesanode.AnodeinNSiscompoundobject
madeofaddressandportclassifiers(describedinalatersection).Userscancreateanodeby
separatelycreatinganaddressandaportclassifierobjectsandconnectingthemtogether.
However,thismemberfunctionofSimulatorobjectmakesthejobeasier.Toseehowanodeis
created,lookatthefiles:"ns2/tcl/libs/nslib.tcl"and"ns2/tcl/libs/nsnode.tcl".

$nsduplexlinknode1node2bandwidthdelayqueuetype:createstwosimplexlinksof
specifiedbandwidthanddelay,andconnectsthetwospecifiednodes.InNS,theoutputqueue
ofanodeisimplementedasapartofalink,thereforeusersshouldspecifythequeuetypewhen
creatinglinks.Intheabovesimulationscript,DropTailqueueisused.Ifthereaderwantstouse
aREDqueue,simplyreplacethewordDropTailwithRED.TheNSimplementationofalinkis
showninalatersection.Likeanode,alinkisacompoundobject,anduserscancreateitssub
objectsandconnectthemandthenodes.Linksourcecodescanbefoundin"ns2/tcl/libs/ns
lib.tcl"and"ns2/tcl/libs/nslink.tcl"files.Onethingtonoteisthatyoucaninserterrormodules
inalinkcomponenttosimulatealossylink(actuallyuserscanmakeandinsertanynetwork
objects).RefertotheNSdocumentationtofindouthowtodothis.

$nsqueuelimitnode1node2number:Thislinesetsthequeuelimitofthetwosimplexlinks
thatconnectnode1andnode2tothenumberspecified.Atthispoint,theauthorsdonotknow
howmanyofthesekindsofmemberfunctionsofSimulatorobjectsareavailableandwhatthey
are.Pleasetakealookat"ns2/tcl/libs/nslib.tcl"and"ns2/tcl/libs/nslink.tcl",orNS
documentationformoreinformation.

$nsduplexlinkopnode1node2...:ThenextcoupleoflinesareusedfortheNAMdisplay.To
seetheeffectsoftheselines,userscancommenttheselinesoutandtrythesimulation.
Nowthatthebasicnetworksetupisdone,thenextthingtodoistosetuptrafficagentssuchasTCP
andUDP,trafficsourcessuchasFTPandCBR,andattachthemtonodesandagentsrespectively.
settcp[newAgent/TCP]:ThislineshowshowtocreateaTCPagent.Butingeneral,userscan
createanyagentortrafficsourcesinthisway.Agentsandtrafficsourcesareinfactbasic
objects(notcompoundobjects),mostlyimplementedinC++andlinkedtoOTcl.Therefore,
therearenospecificSimulatorobjectmemberfunctionsthatcreatetheseobjectinstances.To
createagentsortrafficsources,ausershouldknowtheclassnamestheseobjects(Agent/TCP,
Agnet/TCPSink,Application/FTPandsoon).ThisinformationcanbefoundintheNS
documentationorpartlyinthisdocumentation.Butoneshortcutistolookatthe"ns
2/tcl/libs/nsdefault.tcl"file.Thisfilecontainsthedefaultconfigurableparametervaluesettings
foravailablenetworkobjects.Therefore,itworksasagoodindicatorofwhatkindofnetwork
objectsareavailableinNSandwhataretheconfigurableparameters.

$nsattachagentnodeagent:Theattachagentmemberfunctionattachesanagentobjectcreated
toanodeobject.Actually,whatthisfunctiondoesiscalltheattachmemberfunctionof
specifiednode,whichattachesthegivenagenttoitself.Therefore,ausercandothesamething
by,forexample,$n0attach$tcp.Similarly,eachagentobjecthasamemberfunctionattach
agentthatattachesatrafficsourceobjecttoitself.

$nsconnectagent1agent2:Aftertwoagentsthatwillcommunicatewitheachotherarecreated,
thenextthingistoestablishalogicalnetworkconnectionbetweenthem.Thislineestablishesa
networkconnectionbysettingthedestinationaddresstoeachothers'networkandportaddress
pair.

Assumingthatallthenetworkconfigurationisdone,thenextthingtodoiswriteasimulationscenario
(i.e.simulationscheduling).TheSimulatorobjecthasmanyschedulingmemberfunctions.However,
theonethatismostlyusedisthefollowing:
$nsattime"string":ThismemberfunctionofaSimulatorobjectmakesthescheduler
(scheduler_isthevariablethatpointstheschedulerobjectcreatedby[newScheduler]
commandatthebeginningofthescript)toscheduletheexecutionofthespecifiedstringat
givensimulationtime.Forexample,$nsat0.1"$cbrstart"willmaketheschedulercallastart
memberfunctionoftheCBRtrafficsourceobject,whichstartstheCBRtotransmitdata.InNS,
usuallyatrafficsourcedoesnottransmitactualdata,butitnotifiestheunderlyingagentthatit
hassomeamountofdatatotransmit,andtheagent,justknowinghowmuchofthedatato
transfer,createspacketsandsendsthem.
Afterallnetworkconfiguration,schedulingandpostsimulationprocedurespecificationsaredone,the
onlythingleftistorunthesimulation.Thisisdoneby$nsrun.

You might also like