Traces, AWK and Xgraph
Presented by,
Amritha Sampath
Department of Computer Science
RSET
Trace Files for Wired Networks
1. Type Identifier: Type of Event
“+”: a packet enqueue event
“-”: a packet dequeue event
“r”: a packet reception event
“d”: a packet drop event
“c”: a packet collision at the MAC level
2. Time: at which the packet tracing string is created.
3. Source Node : denote the IDs of the source node of the
tracing object.
4. Destination Node : denote the IDs of the destination
2 NS2 Workshop October 25, 2021
5. Packet Name: Name of the packet type
6. Packet Size: Size of the packet in bytes.
7. Flags: A 7-digit flag string
“-”: disable
1st = “E”: ECN (Explicit Congestion Notification) echo is
enabled.
2nd = “P”: the priority in the IP header is enabled.
3rd : Not in use
4th = “A”: Congestion action
5th = “E”: Congestion has occurred.
6th = “F”: The TCP fast start is used.
7th = “N”: Explicit Congestion Notification (ECN) is on.
8. Flow ID
9. Source Address :the format of these two fields is “a.b”, where
“a” is the address and “b” is the port.
10. Destination Address
11. Sequence Number
12. Packet Unique ID
3 NS2 Workshop October 25, 2021
AWK file
Language that scans through a file line by line
Data driven
Access any column in the current line by using special
variables $1, $2, $3, etc.
BEGIN { print "START" }
{ print }
END { print "STOP" }
4 NS2 Workshop October 25, 2021
Eg. 1
5 NS2 Workshop October 25, 2021
For TCP
#!/usr/bin/awk -f
BEGIN{
data=0
}
{
if ( $1=="r" && $4=="5" && $5=="tcp" )
{
data += $6
print $2,data*8.0/$2/1000000
}
}
END{
#print "Completed"
}
6 NS2 Workshop October 25, 2021
For UDP
#!/usr/bin/awk -f
BEGIN{
data=0
}
{
if ( $1=="r" && $4=="4" && $5=="cbr" )
{
data += $6
print $2,data*8.0/$2/1000000
}
}
END{
#print "Completed"
}
7 NS2 Workshop October 25, 2021
To run awk script
awk -f <filename.awk> <input_file> > <output_file>
8 NS2 Workshop October 25, 2021
Xgraph
Plotting purposes.
Comes together with NS2 installation package.
Running Xgraph
xgraph <inputfile1>...<inputfilen > -bg <color> -t
<graph_title> -x <xtitle> -y <ytitle>
9 NS2 Workshop October 25, 2021
10 NS2 Workshop October 25, 2021
Wireless Networks
Adhoc Networks
Ad-hoc networks are wireless networks where nodes
communicate with each other using multi-hop links.
No stationary infrastructure or base station for
communication.
Each node itself acts as a router for forwarding and
receiving packets to/from other nodes.
12 NS2 Workshop October 25, 2021
Basic setup
Create simulator instance
Eg: set ns [new Simulator]
Open trace file and activate it
Eg: set nt [open 3nodes.tr w]
$ns trace-all $nt
Open namtrace file and activate it
$<simulator_name> namtrace-all-wireless $<namfile_name> <size_X> <size_Y>
Eg: set nm [open 3nodes.nam w]
$ns namtrace-all-wireless $nm 200 200
13 NS2 Workshop October 25, 2021
Create topography :
set <topography_name> [new Topography]
$<topography_name> load_flatgrid <size_x> <size_y>
Eg: set topo [new Topography]
$topo load_flatgrid 200 200
Create the GOD object (General Operations Director):
create-god <number_of_nodes>
Eg: create-god $val(nn)
14 NS2 Workshop October 25, 2021
Simulation of Adhoc Networks
Specify components of the mobile node:
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
15 NS2 Workshop October 25, 2021
Configuration of the mobile nodes
$<simulator_name> node-config –<option_1> <value_1>
\
–<option_2> <value_2> \
...
...
...
–<option_n> <value_n>
16 NS2 Workshop October 25, 2021
Eg:
$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 \
-channel $chan \
-agentTrace ON\
-routerTrace ON\
-macTrace ON\
-movementTrace ON
17 NS2 Workshop October 25, 2021
Finish Procedure
Eg:
proc <proc_name> { } { proc finish {} {
global <vaiable_list> <file_list> global nt nm ns
$<simulator_name> flush-trace $ns flush-trace
close $<file_name> close $nt
exit 0 close $nm
} exit 0
}
18 NS2 Workshop October 25, 2021
Creating the nodes
After setting the configuration options, the nodes are
created
set <node_name> [$<simulator_name> node]
Eg: set n0 [$ns node ]
Enable/disable random motion
$<node_name> random-motion <value>
Value=0 to disable random motion
=1 to enable random motion
Eg: $n0 random-motion 0
19 NS2 Workshop October 25, 2021
Setting node size
$<simulator_name> initial_node_pos $<node_name> <size>
Eg: $ns initial_node_pos $n0 10
Setting initial position
$<node_name> set X_ <x_location>
$<node_name> set Y_ <y_location>
$<node_name> set Z_ 0
Eg: $n0 set X_ 0.0
$n0 set Y_ 0.0
$n0 set Z_ 0.0
20 NS2 Workshop October 25, 2021
Setting node positions
$<simulator_name> at <time> "$<node_name> setdest
<X_destination> <Y_destination> <speed>"
Eg: $ns at 0.0 “$n1 setdest 195.0 195.0 200.0”
21 NS2 Workshop October 25, 2021
Eg. 1
Create a scenario with 4 nodes with random motion
Set up TCP flow from node 0 to node 3
Set up UDP flow from node 1 to node 3
22 NS2 Workshop October 25, 2021
STEP 1: Define all mobile node
modules
set val(chan1) Channel/WirelessChannel
set val(prop1) 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(ifqlen) 50
set val(nn) 4
set val(rp) DSDV
23 NS2 Workshop October 25, 2021
STEP 2: Create simulator object
set ns [new Simulator]
24 NS2 Workshop October 25, 2021
STEP 3: Setup Trace File
set nt [open throughput.tr w]
$ns trace-all $nt
set nm [open throughput.nam w]
$ns namtrace-all-wireless $nm 300 300
25 NS2 Workshop October 25, 2021
STEP 4: Create Topography
set topo [new Topography]
$topo load_flatgrid 300 300
26 NS2 Workshop October 25, 2021
STEP 5: Create Object God
create-god $val(nn)
27 NS2 Workshop October 25, 2021
STEP 6: 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(prop1)\
-phyType $val(netif)\
-channelType $val(chan1)\
-topoInstance $topo\
-agentTrace ON\
-routerTrace ON\
-macTrace ON\
-movementTrace ON
28 NS2 Workshop October 25, 2021
STEP 7: Write a finish procedure
proc finish {} {
global nt nm ns
$ns flush-trace
close $nt
close $nm
exec nam throughput.nam
exit 0
}
29 NS2 Workshop October 25, 2021
STEP 8: Create nodes and start random
motion
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
$n0 random-motion 1
$n1 random-motion 1
$n2 random-motion 1
$n3 random-motion 1
$n0 start
$n1 start
$n2 start
$n3 start
$ns initial_node_pos $n0 10
$ns initial_node_pos $n1 10
$ns initial_node_pos $n2 10
$ns initial_node_pos $n3 10
30 NS2 Workshop October 25, 2021
STEP 9: Setup traffic flow
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n3 $sink1
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns connect $tcp0 $sink1
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set sink2 [new Agent/Null]
$ns attach-agent $n3 $sink2
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$ns connect $udp1 $sink2
31 NS2 Workshop October 25, 2021
STEP 10: Set Stop Time and Start
Simulation
$ns at 1.0 "$ftp0 start"
$ns at 1.0 "$cbr1 start"
$ns at 175.0 "$ftp0 stop"
$ns at 175.0 "$cbr1 stop"
$ns at 180.1 "finish"
$ns run
32 NS2 Workshop October 25, 2021
Eg. 2
Node Time Position
Node0 0 05 05
Node1 0 195 195
Node2 0 195 05
Node1 40 30 30
Node1 80 195 195
Node2 90 175 50
33 NS2 Workshop October 25, 2021
Procedure
Define all mobile node modules
Create simulator object
Setup Trace File
Create Topography of size 200x200.
Create Object God
Configure nodes
Write a finish procedure to close the trace and animator files
Create nodes and configure Initial Position as the positions
in Node Movement Table at time 0.
Setup traffic flow between n0 and n2.
Create Movement as defined in Node Movement Table.
Set Stop Time and Start Simulation.
34 NS2 Workshop October 25, 2021
Configure Initial Position
$n0 set X_ 5.0
$n0 set Y_ 5.0
$n0 set Z_ 0.0
$n1 set X_ 195.0
$n1 set Y_ 195.0
$n1 set Z_ 0.0
$n2 set X_ 195.0
$n2 set Y_ 5.0
$n2 set Z_ 0.0
35 NS2 Workshop October 25, 2021
Node movement
$ns at 0.0 "$n0 setdest 5.0 295.0 200.0"
$ns at 0.0 "$n1 setdest 5.0 5.0 200.0"
$ns at 0.0 "$n2 setdest 150.0 150.0 200.0"
$ns at 0.0 "$n3 setdest 295.0 150.0 200.0"
$ns at 1.0 "$ftp0 start"
$ns at 1.0 "$cbr1 start"
$ns at 40.0 "$n2 setdest 150.0 5.0 15.0"
$ns at 80.0 "$n2 setdest 150.0 295.0 15.0"
$ns at 120.0 "$n2 setdest 150.0 150.0 15.0"
$ns at 170.0 "$n0 reset"
$ns at 170.0 "$n1 reset"
$ns at 170.0 "$n2 reset"
$ns at 170.0 "$n3 reset"
$ns at 175.0 "$ftp0 stop"
$ns at 175.0 "$cbr1 stop"
$ns at 180.1 "finish"
36 NS2 Workshop October 25, 2021
Trace file for Wireless Networks
1. ACTION: [s|r|D|f]:
1. s - sent
2. r - received
3. D - dropped
4. f - forwarded
2. WHEN: the time when the action happened
3. WHERE: the node where the action happened
4. LAYER:
1. AGT - application
2. RTR - routing
3. LL - link layer
4. IFQ - outgoing packet queue (between link and mac layer)
5. MAC - mac
6. PHY - physical
37 NS2 Workshop October 25, 2021
5. flags:
6. SEQNO: the sequence number of the packet
7. TYPE: the packet type
1. cbr -- CBR data stream packet
2. DSR -- DSR routing packet (control packet generated by
routing)
3. RTS -- RTS packet generated by MAC 802.11
4. ARP -- link layer ARP(Address Resolution Protocol) packet
8. SIZE: the size of packet
9. [a b c d]:
1. a -- the packet duration in mac layer header
2. b -- the mac address of destination
3. c -- the mac address of source
4. d -- the mac type of the packet body
38 NS2 Workshop October 25, 2021
10. flags:
11. [......]: [
1. source node ip : port_number
2. destination node ip (-1 means broadcast) :
port_number
3. ip header ttl
4. ip of next hop (0 means node 0 or broadcast) ]
39 NS2 Workshop October 25, 2021
Eg. 3
Node Time Position
Node0 0 05 295
Node1 0 05 05
Node2 0 150 150
Node3 0 295 150
Node2 40 150 05
Node2 80 150 295
Node2 120 150 150
40 NS2 Workshop October 25, 2021
Awk script for Throughput Calculation
#!/usr/bin/awk -f
BEGIN{
#print "Throughput calculation"
data=0
}
{
if ( $1=="r" && $3=="_3_" && $7=="tcp" && $4=="MAC")
{
data += $8
print $2,data*8.0/$2/1000000
}
}
END{
#print "Completed"
}
41 NS2 Workshop October 25, 2021
Xgraph
42 NS2 Workshop October 25, 2021
Conclusion
Discussed Trace file formats for wired and wireless
networks
Discussed AWK files and Xgraph
Generated graph for throughput calculation for wired
networks
TCL script for wireless networks
Generated graph for throughput calculation for wired
networks
43 NS2 Workshop October 25, 2021
44 NS2 Workshop October 25, 2021