250 likes | 381 Vues
ns-2 Tutorial. Deepak Bansal bansal@mit.edu http://nms.lcs.mit.edu/~bansal 15 September, 2000 (Modified from Polly Huang’s and last year’s original). Why ns?. Ubiquity of Tcl, C, C++ languages Access to source code Discrete event simulator Protocols TCP, UDP, FTP, HTTP, DSR,
E N D
ns-2 Tutorial Deepak Bansal bansal@mit.edu http://nms.lcs.mit.edu/~bansal 15 September, 2000 (Modified from Polly Huang’s and last year’s original)
Why ns? • Ubiquity of Tcl, C, C++ languages • Access to source code • Discrete event simulator • Protocols • TCP, UDP, FTP, HTTP, DSR, • Wired and wireless media • Topology generation • Nam visualization tool • Standard in research community
What is ns? • ns is a language • object-oriented tcl (otcl) + extensions for building network simulations. • Otcl: object oriented support • tclcl: C++ and otcl linkage objects otcl C++ objects Base language Tcl tclcl ns
ns on Athena Sun workstations • Accessing the course locker: • add 6.899 • type “ns” or “nam” at your prompt • /mit/6.899/software/ns-allinone-2.1b6/bin • runs on Linux platforms only (not on Sun!) • on Sun, older version of ns exists in sipbnet. • For more information, see course web page.
Tutorial Roadmap • Programming in Tcl and Otcl • Creating simulations in ns • Adding new primitives to ns • Tracing
Basic tcl % proc occur {value list} { set count 0 foreach el $list { if $el == $value { incr count } return $count } % set nblues [occur blue $colors] % puts “I have $nblues blues” I have 1 blues athena% ns % set a 24 % set b 15 % set c [expr $a + $b] % set colors {red green blue purple}
% Class Bagel .% set abagel [new Bagel] _o11 % $abagel info class Bagel % Bagel info instances _o11 % Bagel instproc init {args} { $self instvar toasted_ set toasted_ 0 eval $self next $args } % Bagel instproc toast {} { $self instvar toasted_ incr toasted_ if {$toasted>1} then { error "something's burning!" } return {} } % $abagel toast % $abagel toast something's burning! Basic otcl
Creating Event Scheduler • Create scheduler • set ns [new Simulator] • Schedule event • $ns at <time> <event> • <event>: any legitimate ns/tcl commands • Start scheduler • $ns run
Creating a Network • Nodes • set n0 [$ns node] • set n1 [$ns node] • Links & Queuing • $ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type> • <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR
FTP TCP Creating a simple simulation # Create a topology Set ns [new Simulator] set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 1Mb 10ms DropTail # Add agents set tcp [$ns create-connection Agent/TCP $n0 Agent/TCPSink $n1 0] set ftp [$tcp attach-app FTP] # Create a schedule $ns at 0.5 "$ftp start" $ns at 4.5 "$ftp stop" #Run the simulation $ns run FTP TCP 1Mb n0 n1 10ms
n0 n1 Port Classifier Port Classifier Addr Classifier Addr Classifier 0 1 dmux_ dmux_ 1 0 entry_ entry_ classifier_ classifier_ Link n1-n0 Routing Link n0-n1
dst_=0.0 dst_=1.0 Agent/TCPSink Agent/TCP agents_ 0 1 1 0 Transport n0 n1 Port Classifier Port Classifier Addr Classifier Addr Classifier 0 0 agents_ dmux_ dmux_ Link n0-n1 entry_ entry_ classifier_ classifier_ Link n1-n0
Application/FTP dst_=0.0 dst_=1.0 0 1 1 0 Application n0 n1 Port Classifier Port Classifier Addr Classifier Addr Classifier Agent/TCPSink Agent/TCP 0 0 agents_ agents_ dmux_ dmux_ Link n0-n1 entry_ entry_ classifier_ classifier_ Link n1-n0
dst_=0.0 dst_=1.0 0 1 1 0 Packet Flow n0 n1 Port Classifier Application/FTP Port Classifier Addr Classifier Addr Classifier Agent/TCPSink Agent/TCP 0 0 Link n0-n1 entry_ entry_ Link n1-n0
otcl and C++: The Duality • C++ for data • per packet action • otcl for control • periodic or triggered action C++ otcl
New Class bind() TclClass() command() variables procedures New Class Adding New Classes variables procedures otcl C++
mirroring C++ OTcl TclObject TclObject NsObject ?? Agent Agent FBIAgent Agent/FBI TclClass • Static class FBIClass : public TclClass { • public: • FBIClass() : TclClass(“Agent/FBI”) {} • TclObject* create(int, const char*const*) { • return (new FBIAgent()); • } • } class_fbi;
TclObject: bind() • C++ FBIAgent::FBIAgent() { bind(“age_”, age_); … } • otcl Agent/FBI set age_ 10
TclObject: command() • C++ Int FBIAgent::command(int argc, const char*const* argv) { if (argc == 3) { if (strcomp(argv[1], “mature”) == 0) { age_ += atoi(argv[2]); … return(TCL_OK); } } return (Agent::command(argc, argv); } • otcl set mulder [new Agent/FBI] $mulder mature 20
Tracing • Trace all packets on all links • $ns trace-all [open out.w w] • <event> <time> <from> <to> <pkt> <size> - - <flowid> <src> <dst> <seqno> <aseqno> • Trace all packets on all links in nam-1 format • $ns namtrace-all [open out.w w]