Next: Exercises: Up: ns-2 for the impatient Previous: Introduction

# A very simple scenario: A pure'' ad hoc network of 6 nodes

Let us first start by showing the result of this example scenario, then we will explain the way to go there. Go to ~ns/quick-tutorial/ and run: ns ex6sta.tcl.

You will see a graph popping up, with 3 curves on it (Figure 1): the throughputs of 3 nodes when they contend to access the channel to send UDP packets to 3 others. They start at , and respectively. Simulation stops at .

Let us have a close look at ex6sta.tcl. The nodes
(WT(1) ... WT(6)) are created then positioned as follows (Figure 2):

for {set i 1} {$i <=$opt(nn)} {incr i} {
set WT($i) [$ns_ node $i ] } proc coord_proc {a} { return [expr 10 *$a ]
}

for {set i 1} {$i <=$opt(nn)} {incr i} {
$WT($i) set X_ [coord_proc $i]$WT($i) set Y_ [coord_proc$i]
$WT($i) set Z_ 0.0
}


Like all the array variables opt() used throughout the script, opt(nn) is defined (=6) at the beginning of ex6sta.tcl. coord_proc is a procedure we define to compute the values to be assigned to X_, Y_ (coordinates) of each node WT(i). ns-2 does not take the Z_ elevation into consideration (flat 2-D topologies only).

We use CBR (constant bit rate) sources, over UDP layers, that we attach to nodes WT(1),WT(3) and WT(5):

for {set i 1} {$i <$opt(nn)} {incr i 2} {
set udp($i) [new Agent/UDP]$ns_ attach-agent $WT($i) $udp($i)

set sink($i) [new Agent/Null]$ns_ attach-agent $WT([expr$i +1]) $sink($i)
$ns_ connect$udp($i)$sink($i) set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 1000
$cbr($i) set interval_ 0.005
$cbr($i) attach-agent $udp($i)

$ns_ at [expr 20.0 *$i]  "$cbr($i) start"
$ns_ at$opt(stop) "$cbr($i) stop"
}


For a UDP source in WT(i) we place a sink'' at WT(i+1) to receive the packets. Each CBR source sends 1000-Bytes packets each 5ms, therefore it can occupy alone the total the channel capacity (2 Mbits/s in this example). We schedule the sources to start at , and respectively, and to stop at .

Lower layers of Mobile nodes are configured as follows:

$ns_ node-config -adhocRouting DumbAgent \ -llType LL \ -macType Mac/802_11 \ -ifqType Queue/DropTail/PriQueue \ -ifqLen 50 \ -antType Antenna/OmniAntenna \ -propType Propagation/FreeSpace \ -phyType Phy/WirelessPhy \ -channelType Channel/WirelessChannel \ -topoInstance$topo \


adhocRouting specifies which ad hoc routing protocol to use (DSR, DSDV, AODV). Since all nodes are in the same collision domain, no ad hoc routing protocol is needed. ifqLen is the interface queue size. It has large influence on packet delays and drop rate. We use FreeSpace as a propagation model, which assumes there is a a single line of sight between senders and receivers (no radio wave reflections). More complex propagation models include TwoRayGround and Shadowing.

We consider a topology
($topo load_flatgrid$opt(x) $opt(y)). The default communication range of 802.11 nodes in ns-2 is 240m. Therefore all 6 (=opt(nn)) nodes considered here are within receive range of each other. We define what traces should be put in the trace file (out.tr):  -agentTrace ON \ -routerTrace OFF \ -macTrace OFF \ -movementTrace OFF  agent traces are the ones written by traffic sources/destinations. Router traces are written by packet forwarders. MAC traces are written by each node's MAC. Nodes' movements can be traced by switching movementTrace ON (we don't need it for the moment). We use the basic mode (no RTS/CTS, i.e. Mac/802_11 set RTSThreshold_ 3000), and the channel capacity is set to 2Mbps (Mac/802_11 set dataRate_ 2.0e6) A god (General Operations Director) needs to be created: create-god$opt(nn)


god is an object that is used to store global information about the state of the environment, network and nodes that an omniscient observer would have (without being available to any participant in the simulation). It accelerates the computation of the network routing/connectivity prior to the simulation run. We still need to create it even though we use no routing in our simulations.

Finally, at the end of the simulation (at t=opt(stop)), we reset all the wireless nodes and we call a procedure finish. In this procedure we call some external tools to filter, compute and display the throughput graphs. Everything is based on the trace file out.tr. Have it open and displayed while you follow the explanation. out.tr looks like this:

s 20.050000000 _0_ AGT  --- 10 cbr 1000 ...
r 20.051231990 _1_ AGT  --- 9 cbr 1000 ...


That is, at second 20.05 the agent in WT(1) (_0_) sent (s) cbr packet number 10 of size 1000 (bytes). And at second 20.05123199, agent in WT(2) received cbr packet number 9, and so on.

Our goal is to compute the throughput of a given node _i_ along the simulation time. That is, we need to sum up all packets sizes (column 8) of the rows starting with r, between second and . The array of seconds'' will contain the throughput curve of node _i_.

To do this computation, the tcl script (ex6sta.tcl) calls an external command, awk [1].

exec awk -f fil$i.awk out.tr > out$i.xgr


awk filters the trace file out.tr using filter fil2.awk and directs the output to out2.xgr (to be displayed with xgraph later on). Same with fil4.awk and fil6.awk.

Have a look at fil2.awk. BEGIN{} initializes an array sec[i], which will contain the throughput at second . This is done in the following lines (between {}) which are applied to each line of the trace file.

if ($1=="r" &&$7=="cbr" && $3=="_1_") { sec[int($2)]+=$8; };  It indicates that if the value in column 1 ($1) is r (packet received) and the value in column 7 ($7) is cbr (packet type) and the value in column 3 ($3) is _1_ (WT(2)), then add the packet size ($8) to the throughput in sec[int($2)].

at the end of the trace file, awk will display (output directed to out1.xgr) the array values as:

X       Y
sec[i]  throughput[i]


in bits/s.

Last, the output files (out2.xgr, out4.xgr and out6.xgr) are displayed calling:

exec xgraph out2.xgr out4.xgr out6.xgr &


from the ex6sta.tcl script.

Subsections

Next: Exercises: Up: ns-2 for the impatient Previous: Introduction