700 likes | 821 Views
ns - 2 seminar. Network Simulator 2 - Introduction for Beginners - 2008. 03. 18 CNLAB. Scope. Basic Requirements for Using ns-2 1) C++ 2) Tcl & OTcl 3) Object Orientation concepts 4) ns-2 Mechanisms ( ns-2 Core & Utilities)
E N D
ns-2 seminar Network Simulator 2 - Introduction for Beginners - 2008. 03. 18 CNLAB
Scope Basic Requirements for Using ns-2 1) C++ 2) Tcl & OTcl 3) Object Orientation concepts 4) ns-2 Mechanisms (ns-2 Core & Utilities) 5) Application-specific Implementation Architectures . Wired ~ to Wireless world . Lower Layer ~ to Higher Layer . Variety of architectural specifics
Scope Introduction Some Basic Concepts - Architectural Views - Plumbing & Duality - Tcl basics OTcl Linkage Architecture Object Orientation in OTcl Elemental Procedures in ns-2 programming - Network creation - Agents & others
Introduction Ns has began as a variant of the REAL network simulator in 1989, and has evolved substantially in the past few years. In 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI. - VINT : Virtual InterNet Testbed http://www.isi.edu/nsnam/ns/ The latest version :ns-2.32 [2007] : hundreds * 1000 LOC
Introduction Related Research area - functionality & application - Network Layer . Routing : unicast, multicast, MPLS . QoS : IntServ / DiffServ . Queuing Disciplines - Transport . TCP . Reliable multicast . Congestion control - Application . Web Caching . Multimedia . Traffic Sources - Wireless world . Mobile IP . Ad hoc routing . Sensor MAC
Introduction NS-2 Directory ns-allinone Tcl8.x TK8.x OTcl tclcl ns-2 nam-1 . . . tcl C++ code ... ex test mcast lib examples validation tests OTcl code
Introduction What is NS-2 ? - Discrete Event Simulator -Packet-level - Link layer and up - Wired and Wireless worlds Languages - Tcl (Tool Command Language) - OTcl (MIT Object-oriented Tcl) - C++ - Some others
Introduction Tcl Interpreter with Extents - OTcl : Object-oriented support, extending Tcl/Tk - TclCL : C++ and OTcl Linkage (interface) - Discrete Event Scheduler (ordered list of events) - Data Network (the Internet) Components Event Scheduler ns-2 tclcl Network Component Users Otcl tcl8.x
Basic Concepts – Architectural View Object-oriented (C++, OTcl) • - Reusability • - Maintenance Scalability + Extensibility - Control / “Data” separation . OTcl for control (scenarios) . C++ for “data” - Split OTcl / C++ objects Duality Needs Linkage Modular approach - Fine-grained Object composition
Basic Concepts – Architectural View Simplified User’s View of NS - NS-2 is basically the Tcl & OTcl Interpreter with network simulation object libraries.
Basic Concepts – Architectural View Layered View of the NS [an example] - OTcl : Application, Scenario, or Upper Layer - C++ : Lower Layer or Protocol/Packet Layer
Basic Concepts – Architectural View Components domain Relationship View of the NS - NS-2 is written in C++ with OTcl Interpreter as the Front-End. - Simulator objects are implemented in the Compiled hierarchy (C++ domain), but instantiated through the Interpreter.
Basic Concepts - Architecture Nodes
n0 n1 Port Classifier Multicast Node dmux_ Addr Classifier classifier_ Node entry Node entry dmux_ entry_ entry_ Multicast Classifier classifier_ multiclassifier_ Basic Concepts - Architecture Nodes Unicast Node Classifier : Address, Multicast, Multipath
n0 n1 duplex link n1 entry_ head_ enqT_ queue_ deqT_ link_ ttl_ drophead_ drpT_ tracing simplex link Basic Concepts - Architecture Links
n0 n1 Port Classifier Addr Classifier n1 entry_ 0 Node entry head_ dmux_ enqT_ queue_ deqT_ link_ ttl_ 1 entry_ classifier_ drophead_ drpT_ Basic Concepts - Architecture Nodes & Links
n0 n1 Port Classifier Port Classifier Addr Classifier Addr Classifier 0 1 dmux_ dmux_ 1 0 entry_ entry_ classifier_ classifier_ Link n1-n0 Basic Concepts - Architecture Routing Link n0-n1
dst_=0.0 dst_=1.0 Agent/TCP Agent/TCPSink agents_ agents_ 0 1 1 0 Basic Concepts - Architecture Transport n0 n1 Port Classifier Port Classifier Addr Classifier Addr Classifier 0 0 dmux_ dmux_ Link n0-n1 entry_ entry_ classifier_ classifier_ Link n1-n0
Application/FTP dst_=0.0 dst_=1.0 0 1 1 0 Basic Concepts - Architecture Application : Traffic Generator n0 n1 Port Classifier Port Classifier Agent/TCPSink Addr Classifier Addr Classifier Agent/TCP 0 0 agents_ agents_ dmux_ dmux_ Link n0-n1 entry_ entry_ classifier_ classifier_ Link n1-n0
Basic Concepts - Architecture Object Hierarchy TclObject NsObject other objects Classifier Connector Queue Delay Agent Trace AddrClassifier McastClasifier DropTail RED TCP Enq Deq Drop ... Reno SACK
Basic Concepts - Architecture Some differences between NS-2 & Real network - Node : no queue, above network layer, no lower layer on the basic Node (not in mobile node) - Link : Compound object with Queues - No physical media, but simulated environment - No real data or payload transmissions, except in special applications (web), which uses special transport. - Normally, the ultimate Packet Transmissions are done by calling the next object’s method (recv( )).
Basic Concepts - Plumbing Network Topology Setupusing - Network Objects : Nodes & Links *cf) NsObject - Plumbing Library functions Plumbing : for Network Setup • - Plumbing Data Paths among the Network Objects • by setting the “Neighbor” pointers of an Object to • the Address of an appropriate object. • - Plumbing modules are implemented as member • functions of the base Simulator object. • - neighbor_,target_ variables are key to plumbing • transmission : target_recv( )
Basic Concepts - Duality TclCL linkage Pure C++ objects Pure OTcl objects C++ / OTcl split objects C++ OTcl ns OTcl & C++ Share Class Hierarchy
Basic Concepts - Duality C++ / OTcl Separation - C++ for “Data” . Per Packet action : Packet Headers accessed - OTcl for Control . Periodic or Triggered action (~ Scenario scheduling) . Network(Topology) Creation : Application procedure - Tradeoff between Composability and Speed (+) - Learning/debugging are more difficult (-) Separation on Dual planes need Linkage to compose
Basic Concepts - Duality OTcl : Interpreted hierarchy - Create Network - Create Connection (wired) - Create Traffic C++ : Compiled hierarchy - Packet processing methods - OTcl command Interpreter . object::command( ) method : one of the linkage
Basic Concepts - Duality Otcl domain set tcp [new Agent/TCP] $tcpadvance 10 C++ domain int TcpAgent::command(int argc, const char*const* argv) { if (argc == 3) { if (strcmp(argv[1], “advance”) == 0) { int newseq = atoi(argv[2]); …… return(TCL_OK); } } return (Agent::command(argc, argv); }
Basic Concepts – Tcl/OTcl Tcl / OTcl Basics - proc command : define a global procedure . proc create_topology { - set command : assign a value to a variable . set count 10 . $self set count 10 - Substitutions . Command substitution : brackets as command - set cnt [ $self set count ] cnt = 10 - set cnt [ expr $cnt + $cnt ] set cnt to 20 . Variable substitution : ex) $count 10 $ns at [ $ns now ] “$selfsend $class $rate"
C++ / OTcl Linkage - snapshot view Export C++ class to Otcl [Create C++ object from OTcl] •Creating a new network object class in C++, & also an instance of this object in Otcl, needs a Linkage object derived from the TclClass (C++ domain). Export C++ class variables to OTcl •Thenew network object class in C++ may have some parameter variables, also to be configured from Otcl. Export C++ Object Control Commands to OTcl •Giving the control of new C++ object to Otcl, via the Otcl command interpreter: command() function. Execute an OTcl command from C++ •Execute an OTcl command from the new C++ object.
C++ / OTcl Linkage - TclObject TclObject : base class - Basic hierarchy in ns-2 for split objects - Mirrored in both C++ & OTcl - NsObject : the superclass of all network component objects that handle packets Connector, Classifier subclasses - Connector : having only one output data path - Classifier : having multiple output data path (Router) - Node : Compoundobject ~ Node Entry, Classifier objects - Link : ’’ ~ Queue, Delay, TTL objects..
C++ / OTcl Linkage - TclObject TclObject : Hierarchy and Shadowing C++ class hierarchy OTcl class hierarchy TclObject TclObject Agent static TcpClass : public TclClass { public: TcpClass():TclClass(“Agent/Tcp"){} TclObject* create(int,const char*const*) { return (new TcpAgent());} }; Agent TcpAgent Agent/TCP _o123 *tcp Agent/TCP OTcl shadow object Agent/TCP C++ object
C++/OTcl Linkage - TclObject TclObject Creation & Deletion - Global Procedures : new{ }, delete{ } - Example : set tcp [ new Agent/TCP ] … delete $tcp
Agent/TCP constructor parent constructor TclObject constructor invoke parent constructor create C++ object which C++ obj to create? – TclClass complete initialization complete initialization create OTcl shadow object parent (Agent) constructor AgentTCP constructor TclObject (C++) constructor invoke parent constructor invoke parent constructor do nothing, return bind variables and return bind variables and return C++ / OTcl Linkage – TclObject set tcp [newAgent/TCP] invoke parent constructor OTcl C++
C++/OTcl Linkage - TclObject::bind() Linking C++ member variable to OTcl object variable • Bound variables can be configured from Both C++/OTcl. C++ domain TcpAgent::TcpAgent( ) { // Constructor bind(“window_”, &wnd); … … } // other type : bind_time(), bind_bool(), bind_bw() OTcl domain set tcp [new Agent/TCP] $tcp set window_ 200
C++ / OTcl Linkage - TclObject::command() Export C++ Object Control Commands to OTcl int MyAgent::command(int argc, const char*const* argv ) { if (argc == 2) { if (strcmp(argv[1], “call-my-func”) == 0) { MyPrivFunc( )); return (TCL_OK); } } return (Agent::command(argc, argv)); } - Effect : Implement OTcl methods in C++ - Trap point : OTcl method cmd{ }
C++ / OTcl Linkage - TclObject::command() OTcl space no such procedure $tcp send TclObject::unknown{} $tcp cmd send C++ space TcpAgent::command() match “send”? Yes No Invoke parent: return Agent::command() process and return
mirroring OTcl C++ TclObject TclObject NsObject ?? Agent Agent TcpAgent Agent/TCP C++ / OTcl Linkage – TclClass • Static class TcpClass : public TclClass { • public: • TcpClass() : TclClass(“Agent/TCP”) { } • TclObject* create(int, const char*const*) { • return (new TcpAgent( ) ); • } • } class_tcp;
C++ / OTcl Linkage – TclClass Class TclClass ( inC++ ) - This compiled class is pure virtual class : only 2 functions •Construct the interpreted class hierarchy to mirror the compiled class hierarchy. •Method toCreate instances of the associated TclObject. - For the static variableclass_tcp, ns-2 will execute the constructor of TcpClass. The constructor specifies the interpreted class explicitly as “Agent/TCP”. - This sets up the related methods and the interpreted class hierarchy in the Otcl space. Next slide - This class isassociated with the class TcpAgent, and when the user issues “new …”, TcpClass::create( ) is invoked. TcpAgent is created.
C++ / OTcl Linkage – TclClass Interpreted Class Hierarchy construction (more) - When the object constructor is executed, the TclClass constructor stores the name of the class, and inserts this object into Linked List of TclClass objects. - During initialization, TclClass::bind() is invoked. - For each object in the List, bind() invokes register{… } with the class name as its argument. - register{ } establishes the class hierarchy, creating the class required. class TcpAgent : public Agent { public: TcpAgent( ); // Constructor protected: int command(int argc, const char* const* argv); int xxx_; };
C++ / OTcl Linkage – Tcl Singleton class with a handle to Tcl interpreter Usage - Invoke OTcl procedure from C++ : 4 methods - Obtain OTcl evaluation results : tcl.result( ) - Pass a result string to OTcl : tcl.result(*s), tcl.resultf(*s)
C++ / OTcl Linkage – Tcl Execute an OTcl command from C++ Tcl& tcl = Tcl::instance(); tcl.eval (“puts \ “Message from MyPrivFunc\””); or char wrk[128]; sprintf(wrk, “$s recv %d”, name(), result); tcl.eval (wrk); tcl.evalc (“puts \ “Message from MyPrivFunc\””); tcl.evalf (“puts \ “ my_var1 = %d\””, my_var1); tcl.evalf (“puts \ “ my_var2 = %d\””, my_var2);
C++ / OTcl Linkage - summary TclObject - Unifies Interpreted (OTcl) and Compiled (C++) class hierarchies - Seamless access (Method call and Variable access) between OTcl and C++ TclClass - The mechanism that makes TclObject work - Interpreted hierarchy & shadowing methods Tcl - Primitives ( in C++ ) to access Tcl interpreter
Object Orientation in OTcl Some Differences between C++ & OTcl - OTcl Class vs. C++ Class architecture . OTcl attaches methods to Object or Class (evolving) . C++ has single Class Declaration - C++/OTcl Split Objects . Implement methods in either language . Define instance variables in either C++ or Otcl Through in different Syntax & Forms
Object Orientation in OTcl Class structures comparison Class Link Link instproc init { src dst } { $self next $self instvar fromNode_ toNode_ … set fromNode_ $src set toNode_ $dst ………… } Link instproc head { } { $self instvar head_ return $head_ } class PingAgent : public Agent { public: PingAgent( ); int command(int argc, const char* const* argv); void recv (Packet*, Handler*); protected: int off_ping_; };
Object Orientation in OTcl Some Differences between C++ / OTcl - instvar : to define/access member variable - instproc : to define member function (procedure) - OTcl init{ } vs. C++ Constructor( ) - $self this - Syntax checking in Tcl/OTcl . No new line from “{”+ body, but only after “{” . if {, } elseif {, …
OTcl Basics Class Mom Mom instproc greet {} { $selfinstvar age_ puts “$age_ years old mom: How are you doing?” } Class Kid -superclass Mom Kid instproc greet {} { $selfinstvar age_ puts “$age_ years old kid: What’s up, dude?” } set mom [new Mom] $mom set age_ 45 set kid [new Kid] $kid set age_ 15 $mom greet $kid greet 45 years old mom: How are you doing? 15 years old kid: What's up, dude?
Elemental Tcl Procedures in ns-2 programming - Creating the Event Scheduler - [Turn on tracing] - Creating network - Setup routing - Insert errors - Creating connection(Agents) - Creating traffic - Configuring Algorithmic Scenario specific to this App …. - Start ns simulation
Example Procedures # Application code Tcl/OTcl Script. set end 200.0 set warmup 20.0 # For Stable data timing set linkrate 5Mb # May be Adjusted. set ns [new Simulator] # ns Created : First line of App create_topology # Creating topology... create_agents # Creating agents... create_sources # Creating traffic sources... create_conditioners # Creating conditioners... configure_scheduler # Configuring Queue Scheduler... $ns at $end "end" $ns run # Starting simulation...
Creating Event Scheduler Create event scheduler set ns [new Simulator] Scheduling Events $ns at <time> “<event>” <event> : any legitimate ns/tcl commands Start scheduler $ns run
Register Event Deque Object B Object A Dispatch Dispatch Event Scheduling Event Queue Target object Event (Packet) Time Scheduler