etm3056 - communications networks lab sheet
TRANSCRIPT
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
1/29
FACULTY OF ENGINEERING
LAB SHEET
ETM 3056 - COMMUNICATIONS NETWORKS
TRIMESTER 1 (2010/2011)
CN1 COMMUNICATION PROTOCOLS ANALYSIS
CN2 WIRELESS NETWORK SIMULATION
Note:
On-the-spot evaluation may be carried out during or at the end of the experiment. Students are advised to read through this lab sheet before doing experiment. Your performance, teamwork efford, and learning attitude will count towards the marks.
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
2/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 1 of 15
CN1 : COMMUNICATION PROTOCOL ANALYSIS
Faculty of Engineering
Multimedia University
1. OBJECTIVES
Experiment(1): NS-2: The Beginning
Objective: To understand the basic components, simulation setup and simulationanimation using NS-2
Experiment(2): Simulation Analysis Tools
Objective: To understand the simulation output tools and analysis using NS-2Experiment(3): TCP Analysis
Objective: To study different versions of TCP.
2. LIST OF EQUIPMENTS AND SOFTWARES
Computer running ns-allinone (latest release is 2.31) is a package which contains requiredcomponents and some optional components used in running NS-2. The package contains an"install" script to automatically configure, compile and install these components.Currently the package contains:
Tcl release 8.4.14 (Main component) Tk release 8.4.14 ( Main component) Otcl release 1.13 ( Main component) TclCL release 1.19 ( Main component) Ns release 2.31 (Main component) Nam (Animation purpose) Gnuplot (Plotting purpose)
External storage to save the simulation output.
3. INTRODUCTION
3.1 NS-2 SIMULATION TOOL
NS-2 is a network simulator.
NS-2 is an event-driven object-oriented simulator, written in C++, with an Otcl interpreter as afront-end. This means that most of your simulation scripts will be written in Tcl. If you want todevelop new components for NS-2 you might have to use both Tcl and C++.
NS-2 uses two languages because any network simulator, in general, has two different kinds ofthings it needs to do. On the one hand, detailed simulations of protocols requires a systemsprogramming language which can effciently manipulate bytes, packet headers, and implementalgorithms that run over large data sets. For these tasks run-time speed is important and turn-aroundtime (run simulation, find bug, fix bug, recompile, re-run) is less important. On the other hand, alarge part of network research involves slightly varying parameters or configurations, or quicklyexploring a number of scenarios. In these cases, iteration time (change the model and re-run) ismore important. Since configuration runs once (at the beginning of the simulation), run-time of thispart of the task is less important.
CN1
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
3/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 2 of 15
NS-2 meets both of these needs with two languages, C++ and Otcl. C++ is fast to run but slower tochange, making it suitable for detailed protocol implementation. Otcl runs much slower but can bechanged very quickly (and interactively), making it ideal for simulation configuration. NS-2 (viatclcl) provides glue to make objects and variables appear on both languages.
Figure 1Traces in NS-2 format
$ns trace-all [open tr.out w]
----
++ 11 00 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
-- 11 00 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
rr 11..0000223344 00 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
dd 11..0044221188 11 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
Queue Time From
Node
To
Node
Traff
Type
Pkt
Size
Flags Flow
ID
Src
Port
Dst.
Port
Seq.
Num.
Pkt ID
+,-,r,d double int int string int string int int. int int. int int int
Queue + queue, - dequeue, r received, d dropped
Time time stamp
From/To Nodes paket is between these nodes
Traffic Type what type of agent created this traffic
Packet Size The size of the packet moving from one node to the next
Flags Any flow control of congestion flags
Flow ID Helps identify which traffic path this is apart of
Source Port and Destination Port The end points of the packet
Sequence Number Used in TCP Windowing and Acking
Packet Number Unique number used to id this single packet for monitoring sake. NAM allows you to actually
track a packets movement through a network using this ID
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
4/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 3 of 15
4. EXPERIMENTS
4.1 EXPERIMENT(1): NS-2 SIMULATION
4.1.1 RUNNING KNOPPIX
For these experiments, we will be using Knoppix with embedded NS-2. Knoppix is complete Linux
distribution that can run for a single CD. Usually this kind of distribution is called a live CD whereit can load Linux system without installing it on a hard drive. The distribution used for thisexperiment come with NS-2 embedded in it. Hence, student can run the NS-2 without having toinstall it on the hard drive. Please get a Knoppix CD from the lab technician.
Start using Knoppix
After Knoppix uploaded, type the command below to run the KDE environment:
startx
Bring up theKonsole, and setup path for nam and xgraph line by line (press Enter after each
line):
PATH=$PATH:/usr/local/ns-allinone-2.29/nam-1.11export PATH
PATH=$PATH:/usr/local/ns-allinone-2.29/xgraph-12.1export PATH
To check either the paths are already set, type the commands below one by one. It shouldreturn the path to the required applications:
which ns
which namwhich xgraph
To set your working path, type
cd Desktop
From now on, do save all the documents and files on your desktop.
At the end of session, after logout from the KDE environment, press the below keyssimultaneously to eject the CD:
Ctrl+Alt+Del
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
5/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 4 of 15
4.1.2 RUNNING NS-2: THE BEGINNING
Objective: To understand the basic components, simulation setup and simulation animation usingNS-2
Step1: Login to the computer as being instructed by the technical staff.Step2: Identify the working folder for your working space where all your files will be saved.
Step3: Now, bring up the console to set your working path.Step4: To start NS-2, type inside the console:
ns
Step5: In this section you are going to write a template that you can use for all your simulationscripts. A simulation in NS-2 is described by a Tcl class Simulator. A simulation script, therefore,generally begins by creating an instance of this class. This is done with the command
set ns [new Simulator]
Step6: After creating the simulator object a simulation script usually calls various methods to create
nodes and topologies. Next we have to define what exactly we want to happen in the simulation.Since the NS-2 simulator is an event-driven simulator this is done by scheduling events: whencreating a simulator object a scheduler is created which runs by selecting the next earliest event,executing it to completion, and returning to execute the next event. An event can for example be thesending of data from one node to another node or the failure of a link. The command to schedule anevent is
$ns at
Example:
$ns at 1.0 puts \Hello World!\$ns at 1.5 exit
Step7: After scheduling all the events, we are ready to start the simulation:
$ns run
Although we have seen the basics of running a simulation we dont have a way yet to get any outputfrom the simulation. Fortunately, NS-2 provides a number of ways of collecting output or trace dataon a simulation (We will see this in Section 4.1.3).
Optionally, we can run the above scripts by inserting all the scripts inside a file (say experiment1-
1.tcl) and run the file by executing:
ns experiment1-1.tcl
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
6/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 5 of 15
4.1.3 CREATING A TCL FILE
Create a new file by the name experiment1-2.tcl. Type all the followings inside the file.
Line1: set ns [new Simulator]
The following code for example first opens the file out.nam for writing and gives it the file handle
nf. It then tells the simulator to trace each packet on every link in the topology and write the datainto the file.
Line2: set nf [open experiment1-2.nam w]Line3: $ns namtrace-all $nf
The trace data is collected in a format that can later be used by the nam program to visualize thesimulation in an animation.
The only remaining step is to make sure that all the trace data is flushed into the file at the end ofthe simulation and that the file is closed. Furthermore, we have to run the nam tool on the createdtrace file to produce the animation. We do this by writing a finish procedure that we call at the end
of the simulation and that performs all these tasks.
Line4: proc finish {} {Line5: global ns nfLine6: $ns flush-traceLine7: close $nfLine8: exec experiment1-2.nam &Line9: exit 0Line10: }
To create a node we can simply use the simulator method node. The following two lines create twonodes and assign them to the handles n0 and n1.
Line11: set n0 [$ns node]Line12: set n1 [$ns node]
We can then either use the simulator method simplex-link or the method duplex-link to connect thenodes with a link:
Line13: $ns duplex-link $n0 $n1 1Mb 10ms DropTail
This creates a bidirectional link between n0 and n1 with bandwidth 1Mbps, a propagation delay of10ms and a DropTail queue. In addition to the DropTail queue, NS-2 also supports many other
queuing policies like for example FairQueueing(FQ), Random Early Detection (RED), and Class-based queuing (CBQ).
By now you have some scripts that create topologies but there is not yet trace. Trace generation inNS-2 is based on objects of two classes, the Agent and the Application.
Agents represent endpoints where network-layer packets are constructed or consumed. Every nodein the network that needs to send or receive trace must have an agent attached to it. These agentscan be thought of as the implementation of the transport protocol.
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
7/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 6 of 15
On top of an agent runs an application. The application determines the kind of trace source that issimulated (e.g. ftp or telnet). Applications represent the application layer in an NS-2 simulation.
Creating Agents: Corresponding to the two most popular transport protocols used in the Internetthere are also two types of agents in NS-2: UDP agents and TCP agents.
The following code shows an example of attaching a UDP agent to nodes n0 and n1:
Line14: set udp0 [new Agent/UDP]Line15: $ns attach-agent $n0 $udp0Line16: set null0 [new Agent/Null]Line17: $ns attach-agent $n1 $null0Line18: $ns connect $udp0 $null0
This code first creates a UDP agent and attaches it to n0 using the attach-agent procedure. It thencreates a Null agent which will act as a traffic sink and attaches it to n1. Finally, the two agents areconnected using the simulator method connect. In the next section the UDP agent will be used by anapplication to send data.
Creating Applications: In the previous section, we have set up the agents implementing thetransport layer. We will now create applications that we attach to the transport agents and that willactually generate traffic. In NS-2 there are two basic types of applications: simulated applicationsand traffic generators.
Traffic generators generate On/Off traffic: during On-periods, packets are generated at a constantburst rate and during Off-periods no packets are generated. NS-2 provides three different classes oftraffic generators which differ in how the lengths of the On and Off-periods are modelled:
1. A traffic generator of the type Application/Traffic/Exponential takes the length of theOn and Off periods from an Exponential distribution.
2. An Application/Traffic/Pareto source generates the lengths of these periods from aPareto distribution.
3. Finally, the class Application/Traffic/CBR has no off periods and generates packets at aconstant bit rate.
The following code generates one traffic generator of each class.
set exp [new Application/Traffic/Exponential]set par [new Application/Traffic/Pareto]set cbr0 [new Application/Traffic/CBR]
See the NS-2 manual for how to configure these traffic generators.
All traffic generators run on top of a UDP agent. Therefore, we have to attach a traffic generator toa UDP agent before we can use it to send data. The following example illustrates the use of theCBR traffic generator that we created above.
Line19: set cbr0 [new Application/Traffic/CBR]Line20: $cbr0 set packetSize 500Line21: $cbr0 set interval 0.005Line22: $cbr0 attach-agent $udp0
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
8/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 7 of 15
Line23: $ns at 1.0 $cbr0 startLine24: $ns at 4.5 $cbr0 stop
The simulated applications currently implemented in NS-2 are Application/FTP andApplication/Telnet. These try to simulate the corresponding applications in the real world: FTP andTelnet. Like the real applications the NS-2 applications can run only on TCP. They therefore haveto be attached to a TCP agent.
The following line calls this procedure at the end of the simulation,
Line25: $ns at 5.0 finishLine26: $ns run
4.1.4 TRACING AND MONITORING
So far, when we wanted to see what is happening in a simulation we have used nam to visualize theevents. However, often we want more detailed trace data on a simulation which then allows us tocompute numbers like packet loss rates or per-connection throughput.
The easiest way to do this is to use the simulator method trace-all which traces all the links in thetopology (insert these 2 lines into your previous file):
set f [open out.tr w]$ns trace-all $f
When you use trace-all you also have to remember to change the finish procedure to flush the traceand close the file (as we did previously for the nam tracing).
Sometimes we are only interested in the events at one particular queue and want to avoid the over-head that comes with tracing every single link in the topology. For this case NS-2 provides us withthe trace-queue method. In addition, Section 22 of the NS-2 manual describes many other ways ofcollecting trace data.
Instead of tracing events it is often useful to trace C++ or Tcl instance variables. Below is anexample of setting up variable tracing in NS-2.
# $tcp tracing its own variable cwnd$tcp trace cwnd
# the variable ssthresh of $tcp is traced by a generic $tracerset tracer [new Trace/Var]
$tcp trace ssthresh $tracer
As a summary, a NS-2 simulation script generally includes Create the event scheduler Turn on tracing, if needed Create network topology Setup routing Create transport agent Create traffic source/sink Transmit application-level data
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
9/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 8 of 15
Figure 2
Task(1a): Now execute the file that you have created which refer to Figure 2 (experiment1-2.tcl)
Run the file by executing
ns experiment1-2.tcl
Task(1b): Describes your simulation above. Hence, construct the topology in Figure 3 and name
the file experiment1-3.tcl
Figure 3
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
10/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 9 of 15
4.2 EXPERIMENT(2): SIMULATION ANALYSIS TOOLS
Objective: To understand the simulation output tools and analysis using NS-2
Execute experiment2.tcl and see the animation
ns experiment2.tcl
Task(2a): Comment on the animation
The grep command in unix allow us to filter a file. We can create a new file which consists ofonly those lines from the original file that contain a given character sequence. For example, outputtraces in NS-2 may obtain all types packets that go through all links and we may be interested onlyin the data concerning tcp packets that went from node 0 to node 3. If lines concerning such eventscontain the string 0 3 tcp then all we have to do is type
grep 0 3 tcp experiment2.tr > experiment2-1.tr
where experiment2.tr is the original trace and experiment2-1.tr is the new file. If we wish to
obtain a file containing all lines of experiment2.tr that begin with the letter r, we should type
grep -r experiment2.tr > experiment2-1.tr
if we wish to make a file of all the lines begin with s and have later tcp 1040 we should type
grep -s experiment2.tr | grep tcp 1040 > experiment2-1.tr
Plotting with XGRAPH
We are going to use xgraph instead of gnuplot to do our analysis. To plot using xgraph, itwill only read from file consist only 2 columns of data. Hence, to extract certain columns
from the required data:awk {print $column1, $column2} outputfile.tr > newfile.tr
Example to select column 1 and column 2 from file experiment2.tr and save it into newfile plot1.tr:
awk {print $1, $2} experiment2.tr > plot1.tr
To start plotting:
xgraph plot1.tr
or to have more plotting options use:
xgraph plot1.tr t Graph Label x xlabel y ylabel
Task(2b): Plot the graph of experiment2.tr from different link and comment on the results
Task(2c): Plot the graph of experiment2.tr from different link and comment on the results. PLOT inEXCEL
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
11/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 10 of 15
4.3 EXPERIMENT(3): TCP ANALYSIS
Objective: To study different versions of TCP.
Type the below lines onto the console
ns experiment3-1.tclnam experiment3-1.nam
Task(3a)
Draw and indicate network properties of the simulated script. Identify the queuing discipline of the router used in the simulation Plot the sender's TCP window over simulation time. Determine where the TCP slow-starts ends
and where congestion avoidance begins. Calculate the throughput of the transfer.Say now that we consider the same topology but the link experiences random losses (usuallywireless link)
ns experiment3-2.tcl
nam experiment3-2.nam
Task(3b)
Plot the same sender's TCP window over simulation time. Calculate the throughput of this connection By changing the script from using TCP/Reno to TCP/Newreno, repeat the simulation and
again calculate the throughput. Is there any improvement between TCP/Reno compare to TCP/Newreno? Comment if there
is any improvement.
5. POST-EXPERIMENTAL ANALYSIS AND DISCUSSIONS
Discuss what are the limitations of conducting simulation experiment using NS-2 compareto real implementation testing?
Besides using the xgraph to plot the out graph, name and discuss other tools that can use forthe same purpose?
Discuss in brief, what would be the best implementation and assumptions for a videoconference simulation.
6. REFERENCES
[1] The Network Simulator - NS-2 (no date). Home page. [Online]. University of Michigan.http://nsnam.isi.edu/nsnam/index.php/User_Information [2007, June 11].
[2] NS Manual (no date). Home page. [Online]. University of Michigan.http://www.isi.edu/nsnam/ns/ns-documentation.html [2007, June 11].
[3] NS by Example (no date). Home page. [Online]. University of Michigan.http://nile.wpi.edu/NS/ [2007, June 11].
[4] Fred Halsall, Computer Networking and the Internet, 5th Edition, Addison Wesley; 2005
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
12/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 11 of 15
Appendix I
How to Start
Create an event schedulerset ns [new Simulator]
Open a file for trace dataset nf [open out.nam w]
$ns namtrace-all $nf
A procedure to close file and start NAMproc finish {} {
global ns nf$ns flush-traceclose $nfexec nam out.nam &exit 0
}Schedule the procedure
$ns at 5.0 "finish"Start simulation
$ns run
Topology
Nodeset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]
Link$ns duplex-link $n0 $n1 1Mb 5ms DropTail$ns duplex-link $n1 $n2 400Kb 10ms DropTail
Agent/Application
Create a UDP agent and attach it to node n0set udp [new Agent/UDP]$ns attach-agent $n0 $udp
Create a CBR traffic source and attach it to udp0set cbr [new Application/Traffic/CBR]$cbr attach-agent $udp
Create a null agent to be traffic sinkset null [new Agent/Null]$ns attach-agent $n2 $null
Connect them$ns connect $udp $null
Schedule the event$ns at 0.5 "$cbr start"$ns at 4.5 "$cbr stop"
Create a TCP agent and attach it to node n0set tcp [new Agent/TCP]$ns attach-agent $n0 $tcp
Create a FTP traffic source and attach it to udp0set ftp [new Application/FTP]$ftp attach-agent $tcp
Create a TCPSink agent to be traffic sinkset sink [new Agent/TCPSink]$ns attach-agent $n2 $sink
Connect them$ns connect $tcp $sink
Schedule the event$ns at 0.5 "$ftp start"
$ns at 4.5 "$ftp stop"
Traces
Traces in NS-2 format$ns trace-all [open tr.out w]
----
++ 11 00 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
-- 11 00 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
rr 11..0000223344 00 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
dd 11..0044221188 11 22 ccbbrr 221100 -------------- 00 00..00 33..11 00 00
Traces in NAM format$ns namtrace-all [open tr.nam w]
Turn on tracing on specific links$ns trace-queue $n0 $n1$ns namtrace-queue $n0 $n1
Event and Queuing
Schedule events$ns at : any legitimate ns/tcl commands
Links and queuing$ns duplex-link $n0 $n1
: DropTail, RED, CBQ, FQ,SFQ, DRR
Routing
Unicast$ns rtproto : Static, Session, DV, cost, multi-path
Multicast$ns multicast (right after [new Simulator])
or set ns [new Simulator multicast
on]$ns mrtproto : CtrMcast, DM, ST, BST
(centralized,dense mode, shared tree)
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
13/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 12 of 15
Traffic on top of UDP
UDPset udp [new Agent/UDP]set null [new Agent/Null]$ns attach-agent $n0 $udp$ns attach-agent $n1 $null$ns connect $udp $null
CBRset src [new Application/Traffic/CBR]
Exponential or Paretoset src [new Application/Traffic/Exponential]set src [new Application/Traffic/Pareto]
Traffic on Top of TCP
TCPset tcp [new Agent/TCP]set tcpsink [new Agent/TCPSink]$ns attach-agent $n0 $tcp$ns attach-agent $n1 $tcpsink$ns connect $tcp $tcpsink
FTPset ftp [new Application/FTP]$ftp attach-agent $tcp
Telnetset telnet [new Application/Telnet]$telnet attach-agent $tcp
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
14/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 13 of 15
Appendix II
Experiment 2 Source Code
#Create a simulator objectset ns [new Simulator]
#Open trace filesset f [open experiment2.tr w]
$ns trace-all $f
#Open the nam trace fileset nf [open experiment2.nam w]$ns namtrace-all $nf
#s1, s2 and s3 act as sources.set s1 [$ns node]set s2 [$ns node]set s3 [$ns node]
#G acts as a gateway.set G [$ns node]
#r acts as a receiver.set r [$ns node]
#Define different colors for data flows$ns color 1 red ;# the color of packets from s1$ns color 2 SeaGreen ;# the color of packets from s2$ns color 3 blue ;# the color of packets from s3
#Create links between the nodes$ns duplex-link $s1 $G 6Mb 1ms DropTail$ns duplex-link $s2 $G 6Mb 1ms DropTail$ns duplex-link $s3 $G 6Mb 1ms DropTail$ns duplex-link $G $r 3Mb 1ms DropTail
#Define the queue size for the link between node G andr$ns queue-limit $G $r 5
#Define the layout of the topology$ns duplex-link-op $s1 $G orient right-up$ns duplex-link-op $s2 $G orient right$ns duplex-link-op $s3 $G orient right-down$ns duplex-link-op $G $r orient right
#Monitor the queues for links$ns duplex-link-op $s1 $G queuePos 0.5$ns duplex-link-op $s2 $G queuePos 0.5
$ns duplex-link-op $s3 $G queuePos 0.5$ns duplex-link-op $G $r queuePos 0.5
#Create a TCP agent and attach it to node s1set tcp1 [new Agent/TCP/Reno]$ns attach-agent $s1 $tcp1$tcp1 set window_ 8$tcp1 set fid_ 1
#Create a TCP agent and attach it to node s2set tcp2 [new Agent/TCP/Reno]$ns attach-agent $s2 $tcp2$tcp2 set window_ 8$tcp2 set fid_ 2
#Create a TCP agent and attach it to node s3set tcp3 [new Agent/TCP/Reno]$ns attach-agent $s3 $tcp3$tcp3 set window_ 4$tcp3 set fid_ 3
#Create TCP sink agents and attach them to node rset sink1 [new Agent/TCPSink]set sink2 [new Agent/TCPSink]set sink3 [new Agent/TCPSink]$ns attach-agent $r $sink1$ns attach-agent $r $sink2$ns attach-agent $r $sink3
#Connect the traffic sources with the traffic sinks$ns connect $tcp1 $sink1$ns connect $tcp2 $sink2$ns connect $tcp3 $sink3
#Create FTP applications and attach them to agentsset ftp1 [new Application/FTP]$ftp1 attach-agent $tcp1set ftp2 [new Application/FTP]$ftp2 attach-agent $tcp2set ftp3 [new Application/FTP]$ftp3 attach-agent $tcp3
#Define a 'finish' procedureproc finish {} {global ns$ns flush-traceputs "running nam..."exec nam experiment2 &exit 0
}
$ns at 0.0 "$s1 label Sender1"$ns at 0.0 "$s2 label Sender2"$ns at 0.0 "$s3 label Sender3"$ns at 0.0 "$G label Gateway"$ns at 0.0 "$r label Receiver"
$ns at 0.1 "$ftp1 start"$ns at 0.1 "$ftp2 start"$ns at 0.1 "$ftp3 start"$ns at 5.0 "$ftp1 stop"$ns at 5.0 "$ftp2 stop"$ns at 5.0 "$ftp3 stop"$ns at 5.25 "finish"
$ns run
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
15/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 14 of 15
Appendix III
Experiment 3-1 Source Code
# Author: Jeff Pang edited MMU
# whether to create the nam log for visualization{yes,no}set namlog "yes"
# when to start the senderset starttime 1.0# when to stop the simulationset stoptime 10.0# where to log the sender's cwnd traceset logfilename "experiment3-1.out"
set ns [new Simulator]set cwndf [open $logfilename w]
if {$namlog == "yes"} {set namf [open experiment3-1.nam w]$ns namtrace-all $namf
}
proc finish {} {global ns namlog cwndf namf$ns flush-traceclose $cwndfif {$namlog == "yes"} {
close $namf}exit 0
}
# source nodeset src [$ns node]# desination node
set dst [$ns node]# routersset r1 [$ns node]set r2 [$ns node]
# bottleneck link$ns duplex-link $r1 $r2 1Mb 16ms DropTail# router queue$ns queue-limit $r1 $r2 5# access links$ns duplex-link $src $r1 11Mb 2ms DropTail$ns duplex-link $dst $r2 11Mb 2ms DropTail
# attach tcp agents
set tcpSender [new Agent/TCP/Reno]# configure tcp agent$ns attach-agent $src $tcpSenderset tcpReceiver [new Agent/TCPSink]$ns attach-agent $dst $tcpReceiver
# have the sender run an ftp appset ftp [new Application/FTP]$ftp attach-agent $tcpSender
# trace the sender's cwnd$tcpSender attach $cwndf
$tcpSender tracevar cwnd_
$ns connect $tcpSender $tcpReceiver
$ns at $starttime "$ftp start"
$ns at $stoptime "$ftp stop"
$ns at $stoptime "finish"
# do the simulation$ns run
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
16/29
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 15 of 15
Appendix IV
Experiment 3-2 Source Code
# Author: Jeff Pang edited MMU
# whether to create the nam log for visualization{yes,no}set namlog "yes"
# when to start the senderset starttime 1.0# when the link starts being lossyset losstime 10.0# when to stop the simulationset stoptime 40.0# where to log the sender's cwnd traceset logfilename "experiment3-2.out"
set ns [new Simulator]set cwndf [open $logfilename w]
if {$namlog == "yes"} {set namf [open experiment3-2.nam w]
$ns namtrace-all $namf}
proc finish {} {global ns namlog cwndf namf$ns flush-traceclose $cwndfif {$namlog == "yes"} {
close $namf}exit 0
}
# source node
set src [$ns node]# desination nodeset dst [$ns node]# routersset r1 [$ns node]set r2 [$ns node]
# bottleneck link$ns duplex-link $r1 $r2 1Mb 10ms DropTail# router queue$ns queue-limit $r1 $r2 10# access links$ns duplex-link $src $r1 11Mb 2ms DropTail$ns duplex-link $r2 $dst 11Mb 2ms DropTail
# pretend the receiver is on a lossy wireless link# the error model for the link --- 10% loss rateset em [new ErrorModel/Uniform 0.1 pkt]$em drop-target [new Agent/Null]# attach error model to the link after slow start$ns at $losstime "$ns lossmodel $em $r2 $dst"
# attach tcp agentsset tcpSender [new Agent/TCP/Newreno]# configure tcp agent$ns attach-agent $src $tcpSenderset tcpReceiver [new Agent/TCPSink]
$ns attach-agent $dst $tcpReceiver
# have the sender run an ftp appset ftp [new Application/FTP]$ftp attach-agent $tcpSender
# trace the sender's cwnd$tcpSender attach $cwndf$tcpSender tracevar cwnd_
$ns connect $tcpSender $tcpReceiver
$ns at $starttime "$ftp start"$ns at $stoptime "$ftp stop"
$ns at $stoptime "finish"
# do the simulation$ns run
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
17/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 1 of 13
CN2 : WIRELESS NETWORK SIMULATION
Faculty of Engineering
Multimedia University
1. OBJECTIVEIn this laboratory, you are going to learn to use the mobile wireless simulation model available in theNetwork Simulator (NS). Three experiments have been setup to simulate different topology anddifferent functionalities of the wireless capability in NS.
2. INTRODUCTIONNS is an Application Programming Interface (API). The wireless model in NS was originally designedby CMU's Monarch group's mobility extension. In this original version, it covers the internal of amobilenode, routing mechanisms and network components that are used to construct the network stackfor a mobilenode. The components that are covered briefly are Channel, Network-interface, RadioPropagation model, MAC protocols, Interface Queue, Link Layer and Address Resolution Protocolmodel (ARP). CMU trace support and Generation of node movement and traffic scenario files are alsocovered. The original CMU model allows simulation of pure wireless LANs or multihop ad-hocnetworks. Further extensions were made to this model to allow combined simulation of wired andwireless networks. MobileIP was also extended to the wireless model.
A mobilenode consists of network components like Link Layer (LL), Interface Queue (IfQ), MAClayer, the wireless channel nodes transmit and receive signals from, etc. For details about these networkcomponents refer to the NS Manual.
At the beginning of a wireless simulation, we need to define the type for each of these networkcomponents. For example, there are four different wireless ad-hoc routing protocols currentlyimplemented for mobile networking in NS that we could choose from. They are (i) DSDV (HighlyDynamic Destination-Sequenced Distance Vector routing protocol), (ii) Dynamic Source Routing(DSR), (iii) TORA (Temporally-Ordered Routing Algorithm routing protocol) and (iv) Ad hoc On-demand Distance Vector Routing (AODV). Additionally, we need to define other parameters like the
type of antenna, the radio-propagation model used by mobilenodes, etc.
See comments in the code below for a brief description of each variable defined. The array used todefine these variables, val() is not global as it used to be in the earlier wireless scripts. We will beginour tcl script with a list of these different parameters described above, as follows:
# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(ant) Antenna/OmniAntenna ;# Antenna type
set val(ll) LL ;# Link layer type
set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type
set val(ifqlen) 50 ;# max packet in ifq
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC typeset val(rp) DSDV ;# ad-hoc routing protocol
set val(nn) 2 ;# number of mobilenodes
An object god will need to be created. Quoted from CMU document on god,
"God (General Operations Director) is the object that is used to store globalinformation about the state of the environment, network or nodes that an omniscientobserver would have, but that should not be made known to any participant in thesimulation."
CN2
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
18/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 2 of 13
Currently, god object stores the total number of mobile nodes and a table of shortest number of hopsrequired to reach from one node to another. The next hop information is normally loaded into godobject from movement pattern files, before simulation begins, since calculating this on the fly duringsimulation runs can be quite time consuming. However, in order to keep this example simple we avoidusing movement pattern files and thus do not provide god with next hop information. The usage ofmovement pattern files and feeding of next hop info to god shall be shown in the example in the nextsub-section.
The procedure create-god is defined in ~ns/tcl/mobility/com.tcl, which allows only a single globalinstance of the god object to be created during a simulation. In addition to the evaluationfunctionalities, the god object is called internally by MAC objects in mobilenodes. So even though wemay not utilise god for evaluation purposes, we still need to create god.
A setdest program generates movement pattern files using the random waypoint algorithm. The node-movement files generated using setdest (like scen-3-test which we will use in Experiment 2) alreadyinclude lines like above to load the god object with the appropriate information at the appropriate time.
A program called calcdest (~ns/indep-utilities/cmu-scen-gen/setdest/calcdest) can be used to annotatemovement pattern files generated by other means with the lines of god information. calcdest makesseveral assumptions about the format of the lines in the input movement pattern file which will cause itto fail if the file is not formatted properly. If calcdest rejects a movement pattern file you have created,the easiest way to format it properly is often to load it into ad-hockey and then save it out again. If ad-hockey can read your input correctly, its output will be properly formatted for calcdest.
Both setdest and calcdest calculate the shortest number of hops between nodes based on the nominalradio range, ignoring any effects that might be introduced by the propagation model in an actualsimulation. The nominal range is either provided as an argument to the programs, or extracted from theheader in node-movement pattern files.
The path length information provided to god was used by CMU's Monarch Project to analyze the pathlength optimality of ad hoc network routing protocols, and so was printed out as part of the CMUTraceoutput for each packet.
3. SOFTWARE REQUIREDi. Network Simulator (NS)ii. Network Animator (nam)iii. Object Oriented Tool Control Language (OTcl)
4. EXPERIMENT
4.1 Experiment 1: Creating a simple wireless scenarioWe are going to simulate a very simple 2-node wireless scenario. The topology consists of twomobilenodes, node_(0) and node_(1). The mobilenodes move about within an area whose boundary isdefined in this example as 500mX500m. The nodes start out initially at two opposite ends of theboundary. Then they move towards each other in the first half of the simulation and again move awayfor the second half. A TCP connection is setup between the two mobilenodes. Packets are exchangedbetween the nodes as they come within hearing range of one another. As they move away, packets startgetting dropped.
Just as with any other ns simulation, we begin by creating a tcl script for the wireless simulation. Youneed to assign a name to this tcl file such as mywireless.tcl.
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
19/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 3 of 13
Next we go to the main part of the program and start by creating an instance of the simulator,
set ns_ [new Simulator]
Then setup trace support by opening a xxx.tr file (such as simple.tr) and call the procedure trace-all {}as follows:
set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
Also we are going to set up nam traces. To support nam trace, setup this by opening a xxx.nam file(such as wireless-out.nam).
set namtrace [open wireless-out.nam w] ;# for nam tracing
$ns_ namtrace-all-wireless $namtrace 500 500
Next create a topology object that keeps track of movements of mobilenodes within the topologicalboundary.
set topo [new Topography]
We had earlier mentioned that mobilenodes move within a topology of 500mX500m. We provide thetopography object with x and y co-ordinates of the boundary, (x=500, y=500) :
$topo load_flatgrid 500 500
The topography is broken up into grids and the default value of grid resolution is 1. A different valuecan be passed as a third parameter to load_flatgrid {} above.
Next we create the object god, as follows:
create-god $val(nn)
Then, we create mobilenodes. First, we need to configure nodes before we can create them. Nodeconfiguration API may consist of defining the type of addressing (flat/hierarchical etc), the type ofadhoc routing protocol, Link Layer, MAC layer, IfQ etc. The configuration API can be defined asfollows:
(parameter examples)
# $ns_ node-config -addressingType flat or hierarchical or expanded
# -adhocRouting DSDV or DSR or TORA
# -llType LL
# -macType Mac/802_11
# -propType "Propagation/TwoRayGround"
# -ifqType "Queue/DropTail/PriQueue"
# -ifqLen 50# -phyType "Phy/WirelessPhy"
# -antType "Antenna/OmniAntenna"
# -channelType "Channel/WirelessChannel"
# -topoInstance $topo
# -energyModel "EnergyModel"
# -initialEnergy (in Joules)
# -rxPower (in W)
# -txPower (in W)
# -agentTrace ON or OFF
# -routerTrace ON or OFF
# -macTrace ON or OFF
# -movementTrace ON or OFF
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
20/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 4 of 13
Note: All default values for these options are NULL except for addressingType = flat
We are going to use the default value of flat addressing. Also lets turn on only AgentTrace andRouterTrace. You can experiment with the traces by turning all of them on. MovementTrace, whenturned on, shows the movement of the mobilenodes.
The configuration API for creating mobilenodes looks as follows:
# Configure nodes
$ns_ node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-channelType $val(chan) \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
Next we create the two (2) mobilenodes as follows:
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
$node_($i) random-motion 0 ;# disable random motion
}
The random-motion for nodes is disabled here, as we are going to provide node position and movement(speed & direction) directives next.
Now that we have created mobilenodes, we need to give them a position to start with,
#
# Provide initial (X,Y, for now Z=0) co-ordinates for node_(0) and node_(1)
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0
Node0 has a starting position of (5,2) while Node1 starts off at location (390,385).
Next we produce some node movements:
## Node_(1) starts to move towards node_(0)
#
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"
# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0" means at time 50.0s, node1 starts to move towards thedestination (x=25,y=20) at a speed of 15m/s. This API is used to change direction and speed ofmovement of the mobilenodes.
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
21/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 5 of 13
Next setup traffic flow between the two nodes as follows:
# TCP connections between node_(0) and node_(1)
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(1) $sink
$ns_ connect $tcp $sinkset ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start"
This sets up a TCP connection between the two nodes with a TCP source on node0.
Next add the following lines for providing initial position of nodes in nam.
# Define node initial position in nam
for {set i 0} {$i < $val(nn)} {incr i} {
# 20 defines the node size in nam, must adjust it according to your
# scenario size.
# The function must be called after mobility model is defined
$ns_ initial_node_pos $node_($i) 20
}
Then we need to define stop time when the simulation ends and tell mobilenodes to reset whichactually resets their internal network components,
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at 150.0 "$node_($i) reset";
}
# ending nam and the simulation
$ns_ at $val(stop) "$ns_ nam-end-wireless $val(stop)"
$ns_ at $val(stop) "stop"$ns_ at 150.01 "puts \"end simulation\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd namtrace
$ns_ flush-trace
close $tracefd
close $namtrace
}
At time 150.0s, the simulation shall stop. The nodes are reset at that time and the "$ns_ halt" is calledat 150.0002s, a little later after resetting the nodes. The procedure stop{} is called to flush out tracesand close the trace file.
And finally the command to start the simulation,
puts "Starting Simulation..."
$ns_ run
Now that you have finished writing the code, save the file and assign it a name (such as mywireless.tcl).
Next run the simulation by typing "ns filename.tcl" such as "ns mywireless.tcl" at prompt.
At the end of the simulation run, trace-output file simple.tr and nam file wireless.nam are created. Youcan open the file to see the content of these files. As we have turned on the AgentTrace andRouterTrace we see DSDV routing messages and TCP packets being received and sent by Router and
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
22/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 6 of 13
Agent objects in node _0_ and _1_. We see TCP flow starting at 10.0s from node0. Initially both thenodes are far apart and thus TCP packets are dropped by node0 as it cannot hear from node1. Around81.0s the routing info begins to be exchanged between both the nodes and around 100.0s we see thefirst TCP pkt being received by the Agent at node1 which then sends an ACK back to node0 and theTCP connection is setup. However as node1 starts to move away from node0, the connection breaksdown again around time 116.0s. Packets start getting dropped as the nodes move away from oneanother.
To see the movement or animation, type nam filename.namor nam wireless.namat prompt. Namwindows will come up and you can see the nodes in one of them. Click on the play button to startthe animation.
4.2 Experiment 2: Using node-movement/traffic-pattern filesAs an extension to the previous experiment, we are going to simulate a simple multihop wirelessscenario consisting of 3 mobilenodes here. As before, the mobilenodes move within the boundaries of adefined topology. However the node movements for this example shall be read from a node-movementfile called scen-3-test. scen-3-test defines random node movements for the 3 mobilenodes within atopology of 670mX670m. This file is available as a part of the ns distribution and can be found, along
with other node-movement files, under directory ~ns/tcl/mobility/scene. Random node movement fileslike scen-3-test can be generated using CMU's node-movement generator "setdest".
In addition to node-movements, traffic flows that are setup between the mobilenodes, are also readfrom a traffic-pattern file called cbr-3-test. cbr-3-test is also available under ~ns/tcl/mobility/scene.Random CBR and TCP flows are setup between the 3 mobilenodes and data packets are sent,forwarded or received by nodes within hearing range of one another.
We shall make changes to the tcl script, we had created in Experiment 1 and shall assign a new nameto the resulting file (such as wireless1.tcl). In addition to the variables (LL, MAC, antenna etc) thatwere declared at the beginning of the script, we now define some more parameters like the connection-pattern and node-movement file, x and y values for the topology boundary, for convenience. They arelisted as follows:
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(x) 670 ;# X dimension of the topography
set val(y) 670 ;# Y dimension of the topography
set val(ifqlen) 50 ;# max packet in ifq
set val(seed) 0.0
set val(adhocRouting) DSR
set val(nn) 3 ;# how many nodes are simulated
set val(cp) "../mobility/scene/cbr-3-test"set val(sc) "../mobility/scene/scen-3-test"
set val(stop) 400.0 ;# simulation time
Number of mobilenodes is changed to 3. Also we use DSR (dynamic source routing) as the adhocrouting protocol in place of DSDV (Destination sequence distance vector);
After the creation of ns_, the simulator instance, open a file (such as wireless1-out.tr) for wirelesstraces. Also we are going to set up nam traces. You can assign new names for these files as to bedifferent from Experiment 1.
set tracefd [open wireless1-out.tr w] ;# for wireless traces
$ns_ trace-all $tracefd
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
23/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 7 of 13
set namtrace [open wireless1-out.nam w] ;# for nam tracing
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
Next (after the creation of mobilenodes) source node-movement and connection pattern files that weredefined earlier as val(sc) and val(cp) respectively.
## Define node movement model
#
puts "Loading connection pattern..."
source $val(cp)
#
# Define traffic model
#
puts "Loading scenario file..."
source $val(sc)
In node-movement file scen-3-test, we see node-movement commands like
$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743 \
170.519203111152 3.371785899154"
This, as described in earlier sub-section, means at time 50s, node2 starts to move towards destination(368.4,170.5) at a speed of 3.37m/s. We also see other lines like
$god_ set-dist 1 2 2
These are command lines used to load the god object with the shortest hop information. It means theshortest path between node 1 and 2 is 2 hops. By providing this information, the calculation of shortestdistance between nodes by the god object during simulation runs, which can be quite time-consuming,is prevented.
Next add informative headers for the CMUTrace file, just before the line "ns_ run" :
puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)"
puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"
The rest of the script remains unchanged.
Save the file and assign a name such as wireless1.tcl. Make sure the connection-pattern and node-movement files exist under the directories as declared above. Run the script by typing at the prompt:
ns wireless1.tcl
On completion of the run, CMUTrace output file "wireless1-out.tr" and nam output file "wireless1-out.nam" are created. Running wireless1-out.nam we see the three mobilenodes moving in namwindow.
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
24/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 8 of 13
4.3 Experiment 3: Creating a simple wired-and-wireless scenarioThe wireless simulation described earlier supports multi-hop ad-hoc networks or wireless LANs. Butwe may need to simulate a topology of multiple LANs connected through wired nodes, or in otherwords we need to create a wired-and-wireless topology.
In this section we are going to extend the simple wireless topology created earlier to create a mixedscenario consisting of a wireless and a wired domain, where data is exchanged between the mobile and
non-mobile nodes. Modifications have been made to the earlier tcl script and named as wireless2.tcl.
For the mixed scenario, we are going to have 2 wired nodes, W(0) and W(1), connected to our wirelessdomain consisting of 3 mobilenodes (nodes 0, 1 & 2) via a base-station node, BS. Base station nodesare like gateways between wireless and wired domains and allow packets to be exchanged between thetwo types of nodes. Fig1 below shows the topology for this example described above.
The Adhoc routing protocol used is DSDV. Also, we define TCP and CBR connections between thewired and wireless nodes in the script itself. So we do not need to use the connection pattern file usedin earlier simulation. Also the simulation stop time has been changed. Note here that we use array opt()
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
25/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 9 of 13
instead of val() simply to illustrate that this is no longer a global array variable and its scope is definedonly in the test script. The full script is included in the Appendix.
Run the script. The ns and nam trace files are generated at the end of simulation run. Runningwireless2-out.nam shows the movement of mobilenodes and traffic in the wired domain.
5. REFERENCEi. NS manual, http://www.isi.edu/nsnam/ns/docii. NS tutorial, http://www.isi.edu/nsnam/ns/tutorial/index.html
6. DISCUSSION/ QUESTIONSi. Discuss and analyze the simulation/animation of the above experiments. Provide
your own conclusion. (Possible keywords: TCP, packet, transmit, receive, drop,node movement)
ii. Analyze and comment the output trace file (xxx.tr) for each of the experiments.iii. Analyze and comment the output nam file (xxx.nam) for each of the experimentsiv. Comment how you would use NS in your own research.v. Name and elaborate some common criteria used in measuring the performance
of a communication network.
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
26/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 10 of 13
APPENDIX I
Below is the connection pattern file cbr-3-testused in Experiment 2:
#
# 0 connecting to 2 at time 127.93667922166023
#
set udp_(0) [new Agent/UDP]$ns_ attach-agent $node_(0) $udp_(0)
set null_(0) [new Agent/Null]
$ns_ attach-agent $node_(2) $null_(0)
set cbr_(0) [new Application/Traffic/CBR]
$cbr_(0) set packetSize_ 512
$cbr_(0) set interval_ 4.0
$cbr_(0) set random_ 1
$cbr_(0) set maxpkts_ 10000
$cbr_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $null_(0)
$ns_ at 127.93667922166023 "$cbr_(0) start"
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(1) $tcp
$ns_ attach-agent $node_(2) $sink
$ns_ connect $tcp $sinkset ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 150.00000000000000 "$ftp start"
Below is the node movement file file scen-3-testused in Experiment 2:
set god_ [God instance]
$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743 170.519203111152
3.371785899154"
$ns_ at 51.000000000000 "$node_(1) setdest 221.826585497093 80.855495003839
14.909259208114"
$ns_ at 33.000000000000 "$node_(0) setdest 89.663708107313 283.494644426442
19.153832288917"
$god_ set-dist 1 2 2
$god_ set-dist 0 2 3
$god_ set-dist 0 1 1
$node_(2) set Z_ 0.000000000000
$node_(2) set Y_ 199.373306816804
$node_(2) set X_ 591.256560093833
$node_(1) set Z_ 0.000000000000
$node_(1) set Y_ 345.357731779204
$node_(1) set X_ 257.046298323157
$node_(0) set Z_ 0.000000000000
$node_(0) set Y_ 239.438009831261
$node_(0) set X_ 83.364418416244
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
27/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 11 of 13
APPENDIX II
Below is the tcl script wireless2.tcl for Experiment 3:
# wireless2.tcl
# simulation of a wired-and-wireless scenario consisting of 2 wired nodes# connected to a wireless domain through a base-station node.
# ======================================================================
# Define options
# ======================================================================
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(nn) 3 ;# number of mobilenodes
set opt(adhocRouting) DSDV ;# routing protocol
set opt(cp) "" ;# connection pattern file
set opt(sc) "/ns/ns-allinone-2.31/ns-2.31/tcl/mobility/scene/scen-3-test" ;#
node movement file.
set opt(x) 670 ;# x coordinate of topology
set opt(y) 670 ;# y coordinate of topology
set opt(seed) 0.0 ;# seed for random number gen.
set opt(stop) 250 ;# time to stop simulation
set opt(ftp1-start) 160.0
set opt(ftp2-start) 170.0
set num_wired_nodes 2
set num_bs_nodes 1
# ============================================================================
# check for boundary parameters and random seed
if { $opt(x) == 0 || $opt(y) == 0 } {
puts "No X-Y boundary values given for wireless topology\n"
}
if {$opt(seed) > 0} {
puts "Seeding Random number generator with $opt(seed)\n"
ns-random $opt(seed)
}
# create simulator instance
set ns_ [new Simulator]
# set up for hierarchical routing
$ns_ node-config -addressType hierarchical
AddrParams set domain_num_ 2 ;# number of domains
lappend cluster_num 2 1 ;# number of clusters in each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 1 4 ;# number of nodes in each cluster
AddrParams set nodes_num_ $eilastlevel ;# of each domain
set tracefd [open wireless2-out.tr w]
set namtrace [open wireless2-out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
# Create topography object
set topo [new Topography]
# define topology
$topo load_flatgrid $opt(x) $opt(y)
# create God
create-god [expr $opt(nn) + $num_bs_nodes]
#create wired nodes
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
28/29
ETM3056 Communication Networks Wireless Network Simulation
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 12 of 13
set temp {0.0.0 0.1.0} ;# hierarchical addresses for wired domain
for {set i 0} {$i < $num_wired_nodes} {incr i} {
set W($i) [$ns_ node [lindex $temp $i]]
}
# configure for base-station node
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
#create base-station node
set temp {1.0.0 1.0.1 1.0.2 1.0.3} ;# hier address to be used for wireless
;# domain
set BS(0) [$ns_ node [lindex $temp 0]]
$BS(0) random-motion 0 ;# disable random motion
#provide some co-ord (fixed) to base station node$BS(0) set X_ 1.0
$BS(0) set Y_ 2.0
$BS(0) set Z_ 0.0
# create mobilenodes in the same domain as BS(0)
# note the position and movement of mobilenodes is as defined
# in $opt(sc)
#configure for mobilenodes
$ns_ node-config -wiredRouting OFF
for {set j 0} {$j < $opt(nn)} {incr j} {
set node_($j) [ $ns_ node [lindex $temp \
[expr $j+1]] ]
$node_($j) base-station [AddrParams addr2id \
[$BS(0) node-addr]]
}
#create links between wired and BS nodes
$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail
$ns_ duplex-link $W(1) $BS(0) 5Mb 2ms DropTail
$ns_ duplex-link-op $W(0) $W(1) orient down
$ns_ duplex-link-op $W(1) $BS(0) orient left-down
# setup TCP connections
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp1
$ns_ attach-agent $W(0) $sink1
$ns_ connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns_ at $opt(ftp1-start) "$ftp1 start"
set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
set sink2 [new Agent/TCPSink]
$ns_ attach-agent $W(1) $tcp2
$ns_ attach-agent $node_(2) $sink2
$ns_ connect $tcp2 $sink2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns_ at $opt(ftp2-start) "$ftp2 start"
-
8/9/2019 ETM3056 - Communications Networks Lab Sheet
29/29
ETM3056 Communication Networks Wireless Network Simulation
# source connection-pattern and node-movement scripts
if { $opt(cp) == "" } {
puts "*** NOTE: no connection pattern specified."
set opt(cp) "none"
} else {
puts "Loading connection pattern..."
source $opt(cp)
}
if { $opt(sc) == "" } {
puts "*** NOTE: no scenario file specified."
set opt(sc) "none"} else {
puts "Loading scenario file..."
source $opt(sc)
puts "Load complete..."
}
# Define initial node position in nam
for {set i 0} {$i < $opt(nn)} {incr i} {
# 20 defines the node size in nam, must adjust it according to your
# scenario
# The function must be called after mobility model is defined
$ns_ initial_node_pos $node_($i) 20
}
# Tell all nodes when the simulation endsfor {set i } {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop).0 "$node_($i) reset";
}
# ending nam and the simulation
$ns_ at $opt(stop).0 "$BS(0) reset";
$ns_ at $opt(stop) "$ns_ nam-end-wireless $opt(stop)"
$ns_ at $opt(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop"
proc stop {} {
global ns_ tracefd namtrace
# $ns_ flush-trace
close $tracefd
close $namtrace
}
# informative headers for CMUTracefileputs $tracefd "M 0.0 nn $opt(nn) x $opt(x) y $opt(y) rp \
$opt(adhocRouting)"
puts $tracefd "M 0.0 sc $opt(sc) cp $opt(cp) seed $opt(seed)"
puts $tracefd "M 0.0 prop $opt(prop) ant $opt(ant)"
puts "Starting Simulation..."
$ns_ run