Sei sulla pagina 1di 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.
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

CN1 : COMMUNICATION PROTOCOL ANALYSIS


Faculty of Engineering
Multimedia University
CN1
1. OBJECTIVES

Experiment(1): NS-2: The Beginning


Objective: To understand the basic components, simulation setup and simulation
animation using NS-2
Experiment(2): Simulation Analysis Tools
Objective: To understand the simulation output tools and analysis using NS-2
Experiment(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 required
components 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 a


front-end. This means that most of your simulation scripts will be written in Tcl. If you want to
develop 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 of
things it needs to do. On the one hand, detailed simulations of protocols requires a systems
programming language which can effciently manipulate bytes, packet headers, and implement
algorithms that run over large data sets. For these tasks run-time speed is important and turn-around
time (run simulation, find bug, fix bug, recompile, re-run) is less important. On the other hand, a
large part of network research involves slightly varying parameters or configurations, or quickly
exploring a number of scenarios. In these cases, iteration time (change the model and re-run) is
more important. Since configuration runs once (at the beginning of the simulation), run-time of this
part of the task is less important.
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 1 of 15
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

NS-2 meets both of these needs with two languages, C++ and Otcl. C++ is fast to run but slower to
change, making it suitable for detailed protocol implementation. Otcl runs much slower but can be
changed very quickly (and interactively), making it ideal for simulation configuration. NS-2 (via
tclcl) provides glue to make objects and variables appear on both languages.

Figure 1
Traces in NS-2 format
$ns trace-all [open tr.out w]

<event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst> <seq> <attr>
+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0
d 1.04218 1 2 cbr 210 ------- 0 0.0 3.1 0 0

Queue Time From To Traff Pkt Flags Flow Src Dst. Seq. Pkt ID
Node Node Type Size ID Port Port Num.
+,-,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

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 2 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

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 where
it can load Linux system without installing it on a hard drive. The distribution used for this
experiment come with NS-2 embedded in it. Hence, student can run the NS-2 without having to
install 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 the Konsole, 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.11”
export PATH

PATH=”$PATH:/usr/local/ns-allinone-2.29/xgraph-12.1”
export PATH

To check either the paths are already set, type the commands below one by one. It should
return the path to the required applications:

which ns
which nam
which 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 keys
simultaneously to eject the CD:

Ctrl+Alt+Del

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 3 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

4.1.2 RUNNING NS-2: THE BEGINNING

Objective: To understand the basic components, simulation setup and simulation animation using
NS-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 simulation
scripts. 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: when
creating 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 the
sending of data from one node to another node or the failure of a link. The command to schedule an
event is

$ns at <time in sec> <event>

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 don’t have a way yet to get any output
from the simulation. Fortunately, NS-2 provides a number of ways of collecting output or trace data
on 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

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 4 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

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 data
into 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 the
simulation in an animation.

The only remaining step is to make sure that all the trace data is flushed into the file at the end of
the simulation and that the file is closed. Furthermore, we have to run the nam tool on the created
trace 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 nf
Line6: $ns flush-trace
Line7: close $nf
Line8: exec experiment1-2.nam &
Line9: exit 0
Line10: }

To create a node we can simply use the simulator method node. The following two lines create two
nodes 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 the
nodes 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 of
10ms 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 in
NS-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 node
in the network that needs to send or receive trace must have an agent attached to it. These agents
can be thought of as the implementation of the transport protocol.

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 5 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

On top of an agent runs an application. The application determines the kind of trace source that is
simulated (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 Internet
there 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 $udp0
Line16: set null0 [new Agent/Null]
Line17: $ns attach-agent $n1 $null0
Line18: $ns connect $udp0 $null0

This code first creates a UDP agent and attaches it to n0 using the attach-agent procedure. It then
creates a Null agent which will act as a traffic sink and attaches it to n1. Finally, the two agents are
connected using the simulator method connect. In the next section the UDP agent will be used by an
application to send data.

Creating Applications: In the previous section, we have set up the agents implementing the
transport layer. We will now create applications that we attach to the transport agents and that will
actually generate traffic. In NS-2 there are two basic types of applications: simulated applications
and traffic generators.

Traffic generators generate On/Off traffic: during On-periods, packets are generated at a constant
burst rate and during Off-periods no packets are generated. NS-2 provides three different classes of
traffic 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 the


On and Off periods from an Exponential distribution.
2. An Application/Traffic/Pareto source generates the lengths of these periods from a
Pareto distribution.
3. Finally, the class Application/Traffic/CBR has no off periods and generates packets at a
constant 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 to
a UDP agent before we can use it to send data. The following example illustrates the use of the
CBR traffic generator that we created above.

Line19: set cbr0 [new Application/Traffic/CBR]


Line20: $cbr0 set packetSize 500
Line21: $cbr0 set interval 0.005
Line22: $cbr0 attach-agent $udp0

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 6 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

Line23: $ns at 1.0 ‘‘$cbr0 start’’


Line24: $ns at 4.5 “$cbr0 stop”

The simulated applications currently implemented in NS-2 are Application/FTP and


Application/Telnet. These try to simulate the corresponding applications in the real world: FTP and
Telnet. Like the real applications the NS-2 applications can run only on TCP. They therefore have
to be attached to a TCP agent.

The following line calls this procedure at the end of the simulation,

Line25: $ns at 5.0 ‘‘finish’’


Line26: $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 the
events. However, often we want more detailed trace data on a simulation which then allows us to
compute 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 the
topology (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 trace
and 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 with
the trace-queue method. In addition, Section 22 of the NS-2 manual describes many other ways of
collecting trace data.

Instead of tracing events it is often useful to trace C++ or Tcl instance variables. Below is an
example 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 $tracer


set 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
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 7 of 15
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

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

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 8 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

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 of
only those lines from the original file that contain a given character sequence. For example, output
traces in NS-2 may obtain all types packets that go through all links and we may be interested only
in the data concerning tcp packets that went from node 0 to node 3. If lines concerning such events
contain 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, it
will 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 new
file ‘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 in
EXCEL

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 9 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

4.3 EXPERIMENT(3): TCP ANALYSIS

Objective: To study different versions of TCP.

Type the below lines onto the console

ns experiment3-1.tcl
nam 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 (usually
wireless 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 compare
to real implementation testing?
 Besides using the xgraph to plot the out graph, name and discuss other tools that can use for
the same purpose?
 Discuss in brief, what would be the best implementation and assumptions for a video
conference 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

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 10 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

Appendix I

How to Start Create a TCPSink agent to be traffic sink


Create an event scheduler set sink [new Agent/TCPSink]
set ns [new Simulator] $ns attach-agent $n2 $sink

Open a file for trace data Connect them


set nf [open out.nam w] $ns connect $tcp $sink
$ns namtrace-all $nf
Schedule the event
A procedure to close file and start NAM $ns at 0.5 "$ftp start"
proc finish {} { $ns at 4.5 "$ftp stop"
global ns nf
$ns flush-trace Traces
close $nf Traces in NS-2 format
exec nam out.nam & $ns trace-all [open tr.out w]
exit 0
} <event> <time> <from> <to> <pkt> <size> --
Schedule the procedure <fid> <src> <dst> <seq> <attr>
$ns at 5.0 "finish" + 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
Start simulation - 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
$ns run r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0
d 1.04218 1 2 cbr 210 ------- 0 0.0 3.1 0 0

Topology Traces in NAM format


Node $ns namtrace-all [open tr.nam w]
set n0 [$ns node]
set n1 [$ns node] Turn on tracing on specific links
set n2 [$ns node] $ns trace-queue $n0 $n1
$ns namtrace-queue $n0 $n1
Link
$ns duplex-link $n0 $n1 1Mb 5ms DropTail
$ns duplex-link $n1 $n2 400Kb 10ms DropTail Event and Queuing
Schedule events
$ns at <time> <event>
Agent/Application <event>: any legitimate ns/tcl commands
Create a UDP agent and attach it to node n0 Links and queuing
set udp [new Agent/UDP] $ns duplex-link $n0 $n1 <bandwidth>
$ns attach-agent $n0 $udp <delay> <queue_type>
<queue_type>: DropTail, RED, CBQ, FQ,
Create a CBR traffic source and attach it to udp0 SFQ, DRR
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
Routing
Create a null agent to be traffic sink Unicast
set null [new Agent/Null] $ns rtproto <type>
$ns attach-agent $n2 $null <type>: Static, Session, DV, cost, multi-path
Connect them Multicast
$ns connect $udp $null $ns multicast (right after [new Simulator])
or set ns [new Simulator –multicast
Schedule the event on]
$ns at 0.5 "$cbr start" $ns mrtproto <type>
$ns at 4.5 "$cbr stop" <type>: CtrMcast, DM, ST, BST
(centralized,dense mode, shared tree)
Create a TCP agent and attach it to node n0
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

Create a FTP traffic source and attach it to udp0


set ftp [new Application/FTP]
$ftp attach-agent $tcp

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 11 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

Traffic on top of UDP Traffic on Top of TCP


UDP TCP
set udp [new Agent/UDP] set tcp [new Agent/TCP]
set null [new Agent/Null] set tcpsink [new Agent/TCPSink]
$ns attach-agent $n0 $udp $ns attach-agent $n0 $tcp
$ns attach-agent $n1 $null $ns attach-agent $n1 $tcpsink
$ns connect $udp $null $ns connect $tcp $tcpsink
CBR FTP
set src [new Application/Traffic/CBR] set ftp [new Application/FTP]
Exponential or Pareto $ftp attach-agent $tcp
set src [new Application/Traffic/Exponential] Telnet
set src [new Application/Traffic/Pareto] set telnet [new Application/Telnet]
$telnet attach-agent $tcp

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 12 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

Appendix II
Experiment 2 – Source Code

#Create a simulator object #Create a TCP agent and attach it to node s2


set ns [new Simulator] set tcp2 [new Agent/TCP/Reno]
$ns attach-agent $s2 $tcp2
#Open trace files $tcp2 set window_ 8
set f [open experiment2.tr w] $tcp2 set fid_ 2
$ns trace-all $f
#Create a TCP agent and attach it to node s3
#Open the nam trace file set tcp3 [new Agent/TCP/Reno]
set nf [open experiment2.nam w] $ns attach-agent $s3 $tcp3
$ns namtrace-all $nf $tcp3 set window_ 4
$tcp3 set fid_ 3
#s1, s2 and s3 act as sources.
set s1 [$ns node] #Create TCP sink agents and attach them to node r
set s2 [$ns node] set sink1 [new Agent/TCPSink]
set s3 [$ns node] set sink2 [new Agent/TCPSink]
set sink3 [new Agent/TCPSink]
#G acts as a gateway. $ns attach-agent $r $sink1
set G [$ns node] $ns attach-agent $r $sink2
$ns attach-agent $r $sink3
#r acts as a receiver.
set r [$ns node] #Connect the traffic sources with the traffic sinks
$ns connect $tcp1 $sink1
#Define different colors for data flows $ns connect $tcp2 $sink2
$ns color 1 red ;# the color of packets from s1 $ns connect $tcp3 $sink3
$ns color 2 SeaGreen ;# the color of packets from s2
$ns color 3 blue ;# the color of packets from s3 #Create FTP applications and attach them to agents
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
#Create links between the nodes set ftp2 [new Application/FTP]
$ns duplex-link $s1 $G 6Mb 1ms DropTail $ftp2 attach-agent $tcp2
$ns duplex-link $s2 $G 6Mb 1ms DropTail set ftp3 [new Application/FTP]
$ns duplex-link $s3 $G 6Mb 1ms DropTail $ftp3 attach-agent $tcp3
$ns duplex-link $G $r 3Mb 1ms DropTail
#Define a 'finish' procedure
#Define the queue size for the link between node G and proc finish {} {
r global ns
$ns queue-limit $G $r 5 $ns flush-trace
puts "running nam..."
#Define the layout of the topology exec nam experiment2 &
$ns duplex-link-op $s1 $G orient right-up exit 0
$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 $ns at 0.0 "$s1 label Sender1"
$ns at 0.0 "$s2 label Sender2"
#Monitor the queues for links $ns at 0.0 "$s3 label Sender3"
$ns duplex-link-op $s1 $G queuePos 0.5 $ns at 0.0 "$G label Gateway"
$ns duplex-link-op $s2 $G queuePos 0.5 $ns at 0.0 "$r label Receiver"
$ns duplex-link-op $s3 $G queuePos 0.5 $ns at 0.1 "$ftp1 start"
$ns duplex-link-op $G $r queuePos 0.5 $ns at 0.1 "$ftp2 start"
$ns at 0.1 "$ftp3 start"
#Create a TCP agent and attach it to node s1 $ns at 5.0 "$ftp1 stop"
set tcp1 [new Agent/TCP/Reno] $ns at 5.0 "$ftp2 stop"
$ns attach-agent $s1 $tcp1 $ns at 5.0 "$ftp3 stop"
$tcp1 set window_ 8 $ns at 5.25 "finish"
$tcp1 set fid_ 1
$ns run

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 13 of 15


ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

Appendix III
Experiment 3-1 – Source Code

# Author: Jeff Pang edited MMU $tcpSender tracevar cwnd_

# whether to create the nam log for visualization $ns connect $tcpSender $tcpReceiver
{yes,no}
set namlog "yes" $ns at $starttime "$ftp start"
# when to start the sender $ns at $stoptime "$ftp stop"
set starttime 1.0
# when to stop the simulation $ns at $stoptime "finish"
set stoptime 10.0
# where to log the sender's cwnd trace # do the simulation
set logfilename "experiment3-1.out" $ns run

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-trace
close $cwndf
if {$namlog == "yes"} {
close $namf
}
exit 0
}

# source node
set src [$ns node]
# desination node
set dst [$ns node]
# routers
set 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 $tcpSender
set tcpReceiver [new Agent/TCPSink]
$ns attach-agent $dst $tcpReceiver

# have the sender run an ftp app


set ftp [new Application/FTP]
$ftp attach-agent $tcpSender

# trace the sender's cwnd


$tcpSender attach $cwndf
Khairil/Shamini/Muslim Updated 06-2010 (CN1) 14 of 15
ETM3056 : Communications. Networks CN1: Communication Protocol Analysis

Appendix IV
Experiment 3-2 – Source Code

# Author: Jeff Pang edited MMU # attach tcp agents


set tcpSender [new Agent/TCP/Newreno]
# whether to create the nam log for visualization # configure tcp agent
{yes,no} $ns attach-agent $src $tcpSender
set namlog "yes" set tcpReceiver [new Agent/TCPSink]
# when to start the sender $ns attach-agent $dst $tcpReceiver
set starttime 1.0
# when the link starts being lossy # have the sender run an ftp app
set losstime 10.0 set ftp [new Application/FTP]
# when to stop the simulation $ftp attach-agent $tcpSender
set stoptime 40.0
# where to log the sender's cwnd trace # trace the sender's cwnd
set logfilename "experiment3-2.out" $tcpSender attach $cwndf
$tcpSender tracevar cwnd_
set ns [new Simulator]
set cwndf [open $logfilename w] $ns connect $tcpSender $tcpReceiver

if {$namlog == "yes"} { $ns at $starttime "$ftp start"


set namf [open experiment3-2.nam w] $ns at $stoptime "$ftp stop"
$ns namtrace-all $namf
} $ns at $stoptime "finish"

proc finish {} { # do the simulation


global ns namlog cwndf namf $ns run
$ns flush-trace
close $cwndf
if {$namlog == "yes"} {
close $namf
}
exit 0
}

# source node
set src [$ns node]
# desination node
set dst [$ns node]
# routers
set 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 rate
set 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"

Khairil/Shamini/Muslim Updated 06-2010 (CN1) 15 of 15


ETM3056 Communication Networks Wireless Network Simulation

CN2 : WIRELESS NETWORK SIMULATION


Faculty of Engineering

1. OBJECTIVE
Multimedia University
CN2
In this laboratory, you are going to learn to use the mobile wireless simulation model available in the
Network Simulator (NS). Three experiments have been setup to simulate different topology and
different functionalities of the wireless capability in NS.

2. INTRODUCTION

NS is an Application Programming Interface (API). The wireless model in NS was originally designed
by CMU's Monarch group's mobility extension. In this original version, it covers the internal of a
mobilenode, routing mechanisms and network components that are used to construct the network stack
for a mobilenode. The components that are covered briefly are Channel, Network-interface, Radio
Propagation model, MAC protocols, Interface Queue, Link Layer and Address Resolution Protocol
model (ARP). CMU trace support and Generation of node movement and traffic scenario files are also
covered. The original CMU model allows simulation of pure wireless LANs or multihop ad-hoc
networks. Further extensions were made to this model to allow combined simulation of wired and
wireless networks. MobileIP was also extended to the wireless model.

A mobilenode consists of network components like Link Layer (LL), Interface Queue (IfQ), MAC
layer, the wireless channel nodes transmit and receive signals from, etc. For details about these network
components refer to the NS Manual.

At the beginning of a wireless simulation, we need to define the type for each of these network
components. For example, there are four different wireless ad-hoc routing protocols currently
implemented for mobile networking in NS that we could choose from. They are (i) DSDV (Highly
Dynamic 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 to
define these variables, val() is not global as it used to be in the earlier wireless scripts. We will begin
our 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 type
set 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 global
information about the state of the environment, network or nodes that an omniscient
observer would have, but that should not be made known to any participant in the
simulation."
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 1 of 13
ETM3056 Communication Networks Wireless Network Simulation

Currently, god object stores the total number of mobile nodes and a table of shortest number of hops
required to reach from one node to another. The next hop information is normally loaded into god
object from movement pattern files, before simulation begins, since calculating this on the fly during
simulation runs can be quite time consuming. However, in order to keep this example simple we avoid
using movement pattern files and thus do not provide god with next hop information. The usage of
movement pattern files and feeding of next hop info to god shall be shown in the example in the next
sub-section.

The procedure create-god is defined in ~ns/tcl/mobility/com.tcl, which allows only a single global
instance of the god object to be created during a simulation. In addition to the evaluation
functionalities, the god object is called internally by MAC objects in mobilenodes. So even though we
may 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) already
include 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 annotate


movement pattern files generated by other means with the lines of god information. calcdest makes
several assumptions about the format of the lines in the input movement pattern file which will cause it
to 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 nominal
radio range, ignoring any effects that might be introduced by the propagation model in an actual
simulation. The nominal range is either provided as an argument to the programs, or extracted from the
header in node-movement pattern files.

The path length information provided to god was used by CMU's Monarch Project to analyze the path
length optimality of ad hoc network routing protocols, and so was printed out as part of the CMUTrace
output for each packet.

3. SOFTWARE REQUIRED

i. Network Simulator (NS)


ii. Network Animator (nam)
iii. Object Oriented Tool Control Language (OTcl)

4. EXPERIMENT

4.1 Experiment 1: Creating a simple wireless scenario

We are going to simulate a very simple 2-node wireless scenario. The topology consists of two
mobilenodes, node_(0) and node_(1). The mobilenodes move about within an area whose boundary is
defined in this example as 500mX500m. The nodes start out initially at two opposite ends of the
boundary. Then they move towards each other in the first half of the simulation and again move away
for the second half. A TCP connection is setup between the two mobilenodes. Packets are exchanged
between the nodes as they come within hearing range of one another. As they move away, packets start
getting dropped.

Just as with any other ns simulation, we begin by creating a tcl script for the wireless simulation. You
need to assign a name to this tcl file such as mywireless.tcl.

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 2 of 13


ETM3056 Communication Networks Wireless Network Simulation

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 topological
boundary.

set topo [new Topography]

We had earlier mentioned that mobilenodes move within a topology of 500mX500m. We provide the
topography 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 value
can 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. Node
configuration API may consist of defining the type of addressing (flat/hierarchical etc), the type of
adhoc routing protocol, Link Layer, MAC layer, IfQ etc. The configuration API can be defined as
follows:

(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

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 3 of 13


ETM3056 Communication Networks Wireless Network Simulation

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 let’s turn on only AgentTrace and
RouterTrace. You can experiment with the traces by turning all of them on. MovementTrace, when
turned 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 the
destination (x=25,y=20) at a speed of 15m/s. This API is used to change direction and speed of
movement of the mobilenodes.

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 4 of 13


ETM3056 Communication Networks Wireless Network Simulation

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 $sink
set 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 which
actually 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 called
at 150.0002s, a little later after resetting the nodes. The procedure stop{} is called to flush out traces
and 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. You
can open the file to see the content of these files. As we have turned on the AgentTrace and
RouterTrace we see DSDV routing messages and TCP packets being received and sent by Router and
Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 5 of 13
ETM3056 Communication Networks Wireless Network Simulation

Agent objects in node _0_ and _1_. We see TCP flow starting at 10.0s from node0. Initially both the
nodes are far apart and thus TCP packets are dropped by node0 as it cannot hear from node1. Around
81.0s the routing info begins to be exchanged between both the nodes and around 100.0s we see the
first TCP pkt being received by the Agent at node1 which then sends an ACK back to node0 and the
TCP connection is setup. However as node1 starts to move away from node0, the connection breaks
down again around time 116.0s. Packets start getting dropped as the nodes move away from one
another.

To see the movement or animation, type “nam filename.nam” or “nam wireless.nam” at prompt. Nam
windows will come up and you can see the nodes in one of them. Click on the “play” button to start
the animation.

4.2 Experiment 2: Using node-movement/traffic-pattern files

As an extension to the previous experiment, we are going to simulate a simple multihop wireless
scenario consisting of 3 mobilenodes here. As before, the mobilenodes move within the boundaries of a
defined topology. However the node movements for this example shall be read from a node-movement
file called scen-3-test. scen-3-test defines random node movements for the 3 mobilenodes within a
topology 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 files
like 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 read
from 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 name
to the resulting file (such as wireless1.tcl). In addition to the variables (LL, MAC, antenna etc) that
were 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 are
listed 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 adhoc
routing 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 wireless
traces. Also we are going to set up nam traces. You can assign new names for these files as to be
different from Experiment 1.

set tracefd [open wireless1-out.tr w] ;# for wireless traces


$ns_ trace-all $tracefd

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 6 of 13


ETM3056 Communication Networks Wireless Network Simulation

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 were
defined 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 the
shortest path between node 1 and 2 is 2 hops. By providing this information, the calculation of shortest
distance 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 nam
window.

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 7 of 13


ETM3056 Communication Networks Wireless Network Simulation

4.3 Experiment 3: Creating a simple wired-and-wireless scenario

The wireless simulation described earlier supports multi-hop ad-hoc networks or wireless LANs. But
we may need to simulate a topology of multiple LANs connected through wired nodes, or in other
words 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 mixed
scenario 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 wireless
domain consisting of 3 mobilenodes (nodes 0, 1 & 2) via a base-station node, BS. Base station nodes
are like gateways between wireless and wired domains and allow packets to be exchanged between the
two 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 the
wired and wireless nodes in the script itself. So we do not need to use the connection pattern file used
in earlier simulation. Also the simulation stop time has been changed. Note here that we use array opt()

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 8 of 13


ETM3056 Communication Networks Wireless Network Simulation

instead of val() simply to illustrate that this is no longer a global array variable and its scope is defined
only 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. Running
wireless2-out.nam shows the movement of mobilenodes and traffic in the wired domain.

5. REFERENCE

i. NS manual, http://www.isi.edu/nsnam/ns/doc
ii. NS tutorial, http://www.isi.edu/nsnam/ns/tutorial/index.html

6. DISCUSSION/ QUESTIONS

i. 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 experiments
iv. 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.

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 9 of 13


ETM3056 Communication Networks Wireless Network Simulation

APPENDIX I

Below is the connection pattern file cbr-3-test used 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 $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 150.00000000000000 "$ftp start"

Below is the node movement file file scen-3-test used 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

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 10 of 13


ETM3056 Communication Networks Wireless Network Simulation

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

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 11 of 13


ETM3056 Communication Networks Wireless Network Simulation

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"

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 12 of 13


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 ends


for {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 CMUTracefile


puts $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

Authors: Zalifah/Khairil/Nazeri/05-2007 (T1 Session 2010/2011) Page 13 of 13

Potrebbero piacerti anche