Download presentation
1
Wireless Communication : LAB 3
Background of Wireless Communication Wireless Communication Technology Wireless Networking and Mobile IP Wireless Local Area Networks Student Presentations and Projects Introduction to NS2 Programming
2
Outline NS overview NS structure and basics
Steps to create a typical NS script Summary: Generic Script Structure Vis Tools Useful pointers NS by Example ( Where is documentation and Tutorials:
3
What is NS Discrete event simulator targeted for networking research
Discrete event simulation: representing a system by a collection of states and a set of events that describe state changes. Discrete-Event/Continuous-Time An event in NS a packet ID that is unique for a packet with scheduled time and the pointer to an network object that handles the packet
4
What can ns simulate? Topology: wired, wireless
Queue Scheduling Algorithms: RED, DropTail, … Transport Protocols: TCP (all flavors), UDP, … Routing: Static and dynamic routing, MPLS, … Application: FTP, HTTP, Telnet, Traffic generators, … Multicast Various error models for link failures Open source
5
NS Structure NS is written in oTcl and C++;
Efficiency and simplicity oTcl is an extension to oTcl (object Tcl): Have to deal with objects NS uses C++ for per-packet action e.g. scheduler, TCP implementation oTcl for control Topology Network objects "Tool Control Language," a flexible interpreter designed primarily by John Ousterhout at UC Berkeley. Extensible and powerful, it is a viable competitor of the perl language. Tk is the X11 toolkit extension to Tcl.
6
User interface-Interactive Mode
./ns #> set ns [new Simulator] _o4 #> $ns at 1 "puts \"Hello World!\"" 1 #> $ns at 1.5 "exit" 2 #> $ns run Hello World!
7
User interface-Batch Mode
simple.tcl set ns [new Simulator] $ns at 1 "puts \"Hello World!\"" $ns at 1.5 "exit" $ns run ns simple.tcl Hello World!
8
Basic Tcl proc test{ } { set a 43 set b 27 set c [expr $a+$b]
set d [expr [expr $a - $b] * $c] for {set k 0} {$k<10} {incr k} if {$k<5} puts "k<5, pow=[expr pow($d, $k)]" } else puts "k>=5, mod=[expr $d % $k]" test
9
Basic oTcl Class vehicle vehicle instproc characterize{} {
$self instvar NumberOfWheels_ puts "$NumberOfWheels_ -wheel vehicle is a vehicle“ } Class car -superclass vehicle car instproc characterize{} $self instvar NumberOfWheels_ puts "$NumberOfWheels_ -wheel vehicle is a car" set a [new vehicle] set b [new car] $a set NumberOfWheels_ 3 $b set NumberOfWheels_ 4 $a characterize $b characterize
10
Tcl/oTcl Common Problem
Generally useless debugging information: no object (_o24 cmd line 1) invoked from within “_o24 cmd drop-target { }” “catch “$self cmd $args” ret” (procedure “_o24” line 2) Fortunately, you do NOT need to be a Tcl expert for using NS
11
Structure of a typical ns script
Creating event scheduler Opening trace files Creating topology Creating the transport layer “connection” Creating application to generate traffic Start and stop the traffic flows
12
Structure of a typical ns script
Creating event scheduler Opening trace files Creating topology Creating the transport layer “connection” Creating application to generate traffic Start and stop the traffic flows
13
Create Event Scheduler
Create scheduler set ns [new Simulator] Schedule event $ns at <time> <event> <event> is any legitimate ns/tcl commands Start scheduler $ns run
14
Structure of a typical ns script
Creating event scheduler Opening trace files Creating topology Creating the transport layer “connection” Creating application to generate traffic Start and stop the traffic flows
15
Open trace files Ex.1 Trace packets on all links
set fp [open test.out w] $ns trace-all $fp trace format: <event> <time> <from> <to> <pkt_type> <size> <flag><flow_id><src> <dst><seq_no> <ack_seq_no> r tcp tcp tcp d tcp
16
Open trace files Ex.2 #Open the Trace file set tf [open out.tr w]
$ns trace-all $tf
17
Create Topology Create nodes Create links and queues set n0 [$ns node]
$ns <link-type> $n0de1 $node2 <bandwidth> <delay> <queue-type> <queue-type>: DropTail, RED, WFQ, SFQ, … <link-type>: duplex-link, simplex-link $ns duplex-link $n0 $n1 1Mb 10ms DropTail
18
Create Flow: UDP UDP Create agents - set udp [new Agent/UDP]
- set null [new Agent/NULL] Attach to the nodes - $ns attach-agent $n0 $udp - $ns attach-agent $n1 $null Establish the “connection” - $ns connect $udp $null
19
Create Connection: TCP
Create agents - set tcp [new Agent/TCP] - set tcpsink [new Agent/TCPSink] Attach to the nodes - $ns attach-agent $n0 $tcp - $ns attach-agent $n1 $tcpsink Establish the “connection” - $ns connect $tcp $tcpsink
20
Create Application On Top of UDP: CBR On Top of TCP: FTP
set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $udp On Top of TCP: FTP set ftp [new Application/FTP] $ftp attach-agent $tcp
21
Start & Stop the traffic
Schedule to start the traffic $ns at 0.0 “$cbr0 start” Schedule to stop the traffic $ns at 5.0 “$cbr0 stop” $ns at 5.0 “finish” proc finish { } { global ns fp $ns flush-trace; close $fp exec …. & }
22
Summary: Generic Script Structure
set ns [new Simulator] # [Turn on tracing] # Create topology # Setup packet loss, link dynamics # Create routing agents # Create: # - multicast groups # - protocol agents # - application and/or setup traffic sources # Post-processing procs # Start simulation
23
Example 1 - TCP Simple scenario with TCP and UDP connections n0 TCP n5
recvr 5Mb 2ms n1 n2 1.5Mb 10ms 5Mb 2ms n4 UDP n3 TCPSink
24
TCP : Step 1 Scheduler & tracing #Create scheduler
Set ns [new Simulator] #Turn on tracing set f [open out.tr w] $ns trace-all $f Set nf [open out.nam w] $ns namtrace-all $nf
25
TCP : Step 2 Create topology #create nodes set n0 [$ns node]
26
TCP : Step 3 #create links $ns duplex-link $n0 $n1 5Mb 2ms DropTail
$ns queue-limit $n1 $n2 25 $ns queue-limit $n2 $n1 25
27
TCP : Step 4 Create TCP agents set tcp [new Agent/TCP]
set sink [new Agent/TCPSink] $ns attach-agent $n0 $tcp $ns attach-agent $n3 $sink $ns connect $tcp $sink
28
TCP : Step 5 #start application traffic $ns at 1.1 “$ftp start”
Attach traffic set ftp [new Application/FTP] $ftp attach-agent $tcp #start application traffic $ns at 1.1 “$ftp start”
29
TCP : Step 6 End of simulation wrapper (as usual) $ns at 2.0 “finish”
Proc finish {} { global ns f nf close $f close $nf puts “Running nam…” exec nam out.nam & exit 0 } $ns run
30
Example 2
31
Example 2 #Create a simulator object set ns [new Simulator]
#Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf
32
Example 2 #Define a 'finish' procedure proc finish {} { global ns nf
$ns flush-trace #Close the NAM trace file close $nf #Execute NAM on the trace file exec nam out.nam & exit 0 }
33
Example 2 #Create four nodes set n0 [$ns node] set n1 [$ns node]
#Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10
34
Example 2 #Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right #Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5 #Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1
35
Example 2 #Setup a FTP over TCP connection
set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n3 $null $ns connect $udp $null $udp set fid_ 2
36
Example 2 #Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false #Schedule events for the CBR and FTP agents $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop"
37
Example 2 #Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish" #Print CBR packet size and interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #Run the simulation $ns run
38
Tracing cat out.tr | grep " 2 3 cbr " | grep ^r | column 1 10 | awk '{dif = $2 - old2; if(dif==0) dif = 1; if(dif > 0) {printf("%d\t%f\n", $2, ($1 - old1) / dif); old1 = $1; old2 = $2}}' > jitter.txt Give a .tr file the user must be able to extract the useful information. A Simple unix shell script using text filters like grep and awk can extract and write to a file the required information. This can later used by Xgraph orGNUPlot etc. This shell command selects the "CBR packet receive" event at n3, selects time (column 1) and sequence number (column 10), and calculates the difference from last packet receive time divided by difference in sequence number (for loss packets) for each sequence number. The following is the corresponding jitter graph that is generated using gnuplot. The X axis show the packet sequence number and the Y axis shows simulation time in seconds.
39
Viz Tools Nam-1 (Network AniMator Version 1) Packet-level animation
Well-supported by ns Xgraph Convert trace output into xgraph format
40
NAM NAM has a nice graphical user interface similar to that of a CD player (play, fast forward, rewind, pause and so on), and also has a display speed controller.It can graphically present information such as throughput and number of packet drops at each link, although the graphical information cannot be used for accurate simulation analysis. For wireless simuations, however, only node movements can currently be seen in nam . Dumping of traffic data and thus visualization of data pkt movements in nam for wireless scenarios is still not supported (future work).
41
Ns-nam Interface Color Node manipulation Link manipulation
Topology layout Protocol state Misc
42
Nam Interface: Color Color mapping Color flow id association
$ns color 40 red $ns color 41 blue $ns color 42 chocolate Color flow id association $tcp0 set fid_ 40 ;# red packets $tcp1 set fid_ 41 ;# blue packets
43
Nam Interface: Nodes Color Shape (can’t be changed after sim starts)
$node color red Shape (can’t be changed after sim starts) $node shape box ;# circle, box, hexagon Marks (concentric “shapes”) $ns at 1.0 “$n0 add-mark m0 blue box” $ns at 2.0 “$n0 delete-mark m0” Label (single string) $ns at 1.1 “$n0 label \”web cache 0\””
44
Nam Interface: Links Color Label Dynamics (automatically handled)
$ns duplex-link-op $n0 $n1 color "green" Label $ns duplex-link-op $n0 $n1 label "abced" Dynamics (automatically handled) $ns rtmodel Deterministic { } $n0 $n1 Asymmetric links not allowed
45
Nam Interface: Topo Layout
“Manual” layout: specify everything $ns duplex-link-op $n(0) $n(1) orient right $ns duplex-link-op $n(1) $n(2) orient right-up $ns duplex-link-op $n(2) $n(3) orient down $ns duplex-link-op $n(3) $n(4) orient 60deg If anything missing automatic layout
46
Nam Interface: Protocol State
Monitor values of agent variables $ns add-agent-trace $srm0 srm_agent0 $ns monitor-agent-trace $srm0 $srm0 tracevar C1_ $srm0 tracevar C2_ # … … $ns delete-agent-trace $tcp1
47
Nam Interface: Misc Annotation Set animation rate
Add textual explanation to your sim $ns at 3.5 "$ns trace-annotate \“packet drop\"“ Set animation rate $ns at 0.0 "$ns set-animation-rate 0.1ms"
48
Useful Pointers Directory structure:
/ns-2.../ contains C++ code /ns-2.../tcl/lib/ contains oTcl source code /ns-2.../tcl/ex/ examples simulation scripts /ns-2.../tcl/test/ validation test scripts RED Queue Monitor Example
49
Xgraph
50
Other Utilities in Ns Nam editor Tcl debugger Topology generator
Available as part of nam-1 Tcl debugger For source and documentation, see Topology generator Scenario generator
51
Resources Ns distribution download Installation problems and bug-fix
Installation problems and bug-fix Ns-users mailing list See Archives from above URL
52
Resources (contd..) Marc Greis’ tutorial Ns-users archive Ns-manual
Ns-users archive Ns-manual Tcl (Tool Command Language) Practical programming in Tcl and Tk, Brent Welch Otcl (MIT Object Tcl) ~otcl/doc/tutorial.html (in distribution)
53
Resources (contd..) NS Frequently Asked Questions: Lloyd Wood - introducing ns:
54
QUESTIONS ???
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.