1 / 76

NS-2: the network simulator

NS-2: the network simulator. 15 September 2005 Network Lab., SNU Changjee Joo. Contents. Introduction Basic TCL script Getting started OTcl Linkage Basic network elements Summary. 1. Introduction. Based on NS version 2.26 Fonts Bold Courier Tcl/otcl codes or screen display Arial

hiram-case
Download Presentation

NS-2: the network simulator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NS-2: the network simulator 15 September 2005 Network Lab., SNU ChangjeeJoo

  2. Contents • Introduction • Basic TCL script • Getting started • OTcl Linkage • Basic network elements • Summary

  3. 1. Introduction • Based on NS version 2.26 • Fonts • Bold Courier • Tcl/otcl codes or screen display • Arial • C++ codes • Italic Arial • Directory path or file name

  4. otcl • Duality: C++ andOTcl • Object-Oriented • Reusability, Maintainability • Careful planning ahead • C++ • Fast to run, slower to change • Protocol implementation • Packet processing • OTcl • Slower to run, fast to change • Configuration • Manipulate existing C++ object C++

  5. Discrete Event Scheduler Event Scheduler ns-2 tclcl C++ and otcl linkage (Interpreter) Network Component otcl tcl8.0 Object-Oriented support

  6. otcl linkage • Directories tcl/otcl library example scripts Directory for core C++ codes represented as “$ns” validation test

  7. Contents • Introduction • Basic TCL script • Getting started • OTcl Linkage • Basic network elements • Summary

  8. 2. Basic TCL script • Tcl/Tk, OTcl • For ns, you do not need to know Tcl in depth • Comments – ‘#’ • Variable – ‘$’ • All data types are stored in string • Use ‘expr’ command for calculation • Use ‘global’ for global variables • Array – ‘A(B)’ • 2-dimensional – ‘A(1,2)’

  9. Control statements • if {$x < 10} {…} elseif {$x == 10} {…} else {…} • switch $x { 1 {…} 2 {…} } • for {set x 1} {$x <= 5} {incr x} {…} • foreach y $x {…$y…} • while {$x < 10} {…} • Output/Input • puts “…$x…” • puts [format “%d-th element” $x] • scan “…” “$d $f…” a b c

  10. User defined function • proc multiply { a b } { return [expr $a*$b]} • puts “[multiply 3 4]” • String commands • compare, first, index, last, length, match, range, … • append • append x “0” • List commands • set x {apple orange {banana mango}} • lappend, concat, split, llength, lindex, lsearch,…

  11. File I/O commands • open, close, puts, gets, read, eof, flush, … • Materials • 프로그램 세계 1997년 11월, 12월 • Find above commands in/at • Any Tcl/Tk book • http://hegel.ittc.ukans.edu/topics/tcltk/ – Tutorial & Free book !! • http://users.belgacom.net/bruno.champagne/tcl.html

  12. Contents • Introduction • Basic TCL script • Getting started • OTcl Linkage • Basic network elements • Summary

  13. 3. Getting started • “Hello World” example • hello_world.tcl • set ns [new Simulator]$ns at 1 “puts \“Hello World!\””$ns at 2.5 “exit”$ns run • Hello World • % ns hello_world.tclHello World!%

  14. Basic network simulation example • basic_ex.tcl • set ns [new Simulator] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  15. scheduler 1) Create simulator instance (scheduler) • set ns [new Simulator] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  16. scheduler n1 n0 10Mb 2ms 2) Create network topology • set ns [new Simulator] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  17. scheduler n1 n0 udp0 cbr0 10Mb 2ms 3) Stack up protocol (agent) and application for sender • set ns [new Simulator] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  18. scheduler n1 n0 udp0 cbr0 null0 10Mb 2ms 4) Stack up protocol (agent) for receiver and make a connection • set ns [new Simulator] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  19. scheduler at 1.0 cbr0 start n1 n0 udp0 cbr0 null0 10Mb 2ms 5) Schedule event and run the Scheduler • set ns [new Simulator] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  20. Scheduler at 1.0 cbr0 start n1 n0 udp0 cbr0 null0 10Mb 2ms • How does it work with the scheduler ? • At time 0

  21. Scheduler time 1.0cbr0 start at 1.002 forward packet n1 n0 udp0 cbr0 null0 10Mb 2ms • At time 1.0 Control or command Packet move or function call

  22. Schedulertime 1.002forward packet n1 n0 udp0 cbr0 null0 10Mb 2ms • At time 1.002

  23. Scheduler time 1.0cbr0 start at 1.002 forward packet at 1.01 cbr0 send another packet n1 n0 udp0 cbr0 null0 10Mb 2ms • Revisit time 1.0

  24. Tracing with “trace-all” • basic_ex.tcl • set ns [new Simulator]$ns trace-all [open out.tr w]$ns namtrace-all [open out.nam w] • set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 10Mb 2ms DropTail • set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0 • set null0 [new Agent/Null]$ns attach-agent $n1 $null0 • $ns connect $udp0 $null0 • $ns at 1.0 “$cbr0 start”$ns run

  25. trace-all • “out.tr”+ 1.000032 0 1 tcp 1040 ----- 0 0.0 1.0 1 1r 1.000232 0 1 tcp 40 ----- 0 0.0 1.0 0 0… • Use $ns/bin/raw2xg to see packet’s input/output to links

  26. namtrace-all • “out.nam”l –t * -s 0 –d 1 –p tcp –e 40 –c 0 –i 0 –a 0 –x {…}+ -t 1 –s 0 –d 1 –p tcp –e 40 –c 0 –i 0 –a 0 –x {…}… • To visualize nodes/links/packets

  27. What are we expecting from simulation? • Packet movements on links or network visualization • Can use trace-all or namtrace-all • Internal operation network components, such as protocols at end-hosts and routers • Require general methods to access C++ objects => OTcl linkage • Not suitable for simulation of large network • Increase simulation time significantly • Result in huge output file

  28. Contents • Introduction • Basic TCL script • Getting started • OTcl Linkage • Basic network elements • Summary

  29. 4. OTclLinkage • Important otcl linkages • Class TclClass • Class TclObject • Class Tcl • Hierarchical structure of classes • Other linkages • Class TclCommand • Class InstVar

  30. How to connect two spaces ? • basic_ex.tcl • ……… • set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0$cbr0 set rate 1000000$cbr0 set packetSize_ 1000 • ……… otcl C++ • cbr_traffic.cc • class CBR_Traffic : public TrafficGenerator {public: CBR_Traffic( );protected: double rate_; ………} HOW ?

  31. Basic structure of C++ elements Class definition Define TclClass Connection to “construction function” Class body TclObject: binding method Connection to “variables” TclObject: command method Connection to “functions”

  32. Example of C++ elements (CBR Traffic ) $ns/tools/cbr_traffic.cc class CBR_Traffic : public TrafficGenerator {public: CBR_Traffic();protected: ………} static class CBRTrafficClass : public TclClass {public: CBRTrafficClass() : TclClass(“Application/… ………} CBR_Traffic::CBR_Traffic() { ………} Class definition TclClass Class body

  33. Class TclClass • TclClass • Connect C++ and Tcl objects TclClass C++ object CBR_Traffic Tcl object Application/Traffic/CBR CBRTrafficClass Defined in Tcl space / Automatically generated Defined in C++ space

  34. C++ virtual class • Construct interpreted class to mirror compiled class • Method to instantiate new compiled objects Object name in Tcl space • $ns/tools/cbr_traffic.cc • static class CBRTrafficClass : public TclClass { public: • CBRTrafficClass() : TclClass(“Application/Traffic/CBR”) {}TclObject* create(int, const char* const) { • return (new CBR_Traffic()); • } • } class_cbr_traffic; Instantiate new TclObject Invoke the constructor

  35. Initiating new instance • Make shadow object in Tcl space • Call TclClass for constructor of c++ object • Make c++ object • Return to Tcl • basic_ex.tcl • ……… • set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0$cbr0 set rate 1000000$cbr0 set packetSize_ 1000 • ……… otcl

  36. Class TclObject • Connect variables • Between C++ and Tcl objects • Two methods – binding and command TclClass C++ object CBR_Traffic rate_ size_ maxpkts_ Tcl object Application/Traffic/CBR rate_ packetSize_ maxpkts_ CBRTrafficClass binding binding binding

  37. Variable bindings • Five data types • real, integer, bandwidth, time, boolean • Set/get value of C++ variable from Tcl

  38. Usually defined in constructor • $ns/tools/cbr_traffic.cc • CBR_Traffic::CBR_Traffic() : seqno_(0){ • bind_bw(“rate_”, &rate_);bind(“random_”, &random_);bind(“packetSize_”, &size_);bind(“maxpkts_”, &maxpkts_); • } Keyword used in the interpreter (otcl) Variable used in compiled (C++) object

  39. Variable access from Tcl • basic_ex.tcl • Application/Traffic/CBR set maxpkts_ 1000set cbr0 [new Application/Traffic/CBR]#set value of bound variables $cbr0 set rate_ 1Mb$cbr0 set random_ 1$cbr0 set packetSize_ 500 • #get value of bound variableputs “[$cbr0 set rate_]” Bound keyword in compiled (C++) object

  40. Initiation • Default variables are in $ns/tcl/lib/ns-default.tcl • set to default values when created • If not initiated, you will see a warning after compilation (but it may still work) • $ns/tcl/lib/ns-default.tcl • ………Application/Traffic/CBR set rate_ 448Kb Application/Traffic/CBR set packetSize_ 210Application/Traffic/CBR set random_ 0Application/Traffic/CBR set maxpkts_ 268435456………

  41. Ex) Periodic variable probing • proc_ex.tcl • set ns [new Simulator]………proc procedure { } { • global ns x • set now [$ns now]puts “Value from x = [$x set value_]”$ns at [expr $now + 1] “procedure” • } • $ns at 1 “procedure”$ns run

  42. Variable tracing • Automatic record when the bound value changes • Without explicit probing • Restrictions • Variable must be visible in Tcl : bound variable, Tcl instance variable • Variable must belongs to a kind of trace class: TracedVar, TracedInt, TracedDouble • Require Tracer: generic tracer, object-specific tracer

  43. How to trace • set tracer_ [new Trace/Var] • set tracefile [open FILENAME w] • $tracer_ attach $tracefile • $OBJECT trace VARNAME $tracer_ • Ex) Trace cwnd_ of TCP • TracedDouble cwnd_. . . . .bind(“cwnd_”, &cwnd); • set tracer_ [new Trace/Var]$tracer_ attach [open FILENAME w] $tcp0 trace cwnd_ $tracer_(or $tcp0 trace cwnd_)

  44. Tracer output (contents in FILENAME) • ………f t12.00128 a_0128 nVARNAME v1200203f t12.00132 a_0128 nVARNAME v1201204f t12.00135 a_0128 nVARNAME v1200042……… • Use script language to reform data: Tcl, awk

  45. Command Method • Limitation of Variable binding (or tracing) • Good for accessing variables • How to access to function (or method) of the C++ object ? • Provide more generalized access • No initialization (no default value) • With help of Class Tcl

  46. Special function “command” • Override “command” of parent class • $ns/tools/cbr_traffic.cc • int CBR_Traffic::command(int argc, const char*const* argv) { • Tcl& tcl = Tcl::instance();if (argc == 3) { • If(strcmp(argv[1], “pkt_size”) == 0) { • size_ = atoi(argv[2]); tcl.retultf(“%d”, size_);return TCL_OK; • } • } • return (TrafficGenerator::command(argc, argv)); • } ? argv[0], argv[1] Must return TCL_OK or TCL_ERROR Pass a value to interpreter If not matched, invoke parent’s command method

  47. How to use command method • command of compiled object is invoked • set “size_” to 500 • print 500 to Tcl by result() • Return to Tcl • *.tcl • set cbr0 [new Application/Traffic/CBR] puts “packet size is [$cbr0 pkt_size 500]”

  48. Summary of Class TclObject • How to Set/Get value of variable • Variable binding • Connect variables of C++ and Tcl • Variable tracing • Automatic record of bound variables • Command method • Provide generalized access to variable and function • Override command method of parent’s class

  49. Class Tcl • Predefined communication channel from C++ • Obtain a reference • Tcl& tcl = Tcl::instance(); • Invoking otclprocedures • tcl.eval(char*) • tcl.eval(const char*) • tcl.evalf() • Passing results to/from the Interpreter • tcl.result(const char*) • tcl.result()

  50. EX) invoking otcl procedure Obtain a reference • Tcl& tcl = Tcl::instance();tcl.evalc(“Simulator set NumberInterface_”);char* ni = tcl.result();if (atoi(ni) != 1) • tcl.evalc(“Simulator set NumberInterfaces_ 1”); Invoke otcl procedure Passing value from interpreter Invoke otcl procedure

More Related