Introduction to ns 2 Network Simulation and Simulator

  • Slides: 54
Download presentation
Introduction to ns 2 Network Simulation and Simulator Internals 潘仁義 jypan@comm. ccu. edu. tw

Introduction to ns 2 Network Simulation and Simulator Internals 潘仁義 jypan@comm. ccu. edu. tw Modified from Su Wen’s ns 2 ppt suwen@dcs. uky. edu & Padmaparna Haldar’s haldar@isi. edu

The Network Simulator - ns-2 q http: //www. isi. edu/nsnam/ns/ v The source code

The Network Simulator - ns-2 q http: //www. isi. edu/nsnam/ns/ v The source code and documentation is currently maintained by VINT project at ISI q http: //nsnam. isi. edu/nsnam/index. php/Main_Page v Sept 3, 2007: ns-2. 32 released. v Mar 10, 2007: ns-2. 31 released. v July 2, 2006: ns-3 project announced. q NS 2 is a discrete event simulator targeted at networking research q NS 2 is an object oriented simulator, written in C++, with an OTcl interpreter as a frontend 抽考: ns 2安 裝

ns-2 Overview q Collection of various protocols at multiple layers v TCP(reno, tahoe, vegas,

ns-2 Overview q Collection of various protocols at multiple layers v TCP(reno, tahoe, vegas, sack) v MAC(802. 11, 802. 3, TDMA) v Ad-hoc Routing (DSDV, DSR, AODV, TORA) v Sensor Network (diffusion, gaf) v Multicast protocols, Satellite protocols, and many others q Codes are contributed from multiple research communities v Good: Large set of simulation modules v Bad: Level of support and documentation varies q The source code and documentation is currently maintained by VINT project at ISI

Documentation q introductory: Marc Greis's tutorial q reference: Ns Manual (formerly called "ns Notes

Documentation q introductory: Marc Greis's tutorial q reference: Ns Manual (formerly called "ns Notes and Documentation") q ns by Example q Practical Programming in Tcl and Tk (http: //www. beedub. com/book/) q http: //hpds. ee. ncku. edu. tw/~smallko/ns 2/n s 2. htm

Current Status qns-2 (2. 1 b 6) Simulator Core v 100 K lines of

Current Status qns-2 (2. 1 b 6) Simulator Core v 100 K lines of C++ v 70 K lines of OTcl v 30 K lines of test suite v 20 K lines of documentation q. Other Components v. Tcl/TK 8. x, OTcl, Tcl. CL, nam-1 v. Tcl-debug, GT-ITM, xgraph, …

ns Directory Structure ns-allinone Tcl 8. 0 TK 8. 0 OTcl tclcl. . .

ns Directory Structure ns-allinone Tcl 8. 0 TK 8. 0 OTcl tclcl. . . tcl ex examples simple. tcl test validation tests ns-2 lib nam-1 C++ code mcast OTcl code . . .

Running simulations with ns q. Compile the simulator core (“ns”) q. Write a simulation

Running simulations with ns q. Compile the simulator core (“ns”) q. Write a simulation script in Otcl ve. g. my-test. tcl q. Running the simulator ve. g. ns my-test. tcl

Hello World simple. tcl set sim [new Simulator] $sim at 1 “puts “Hello World!””

Hello World simple. tcl set sim [new Simulator] $sim at 1 “puts “Hello World!”” $sim at 1. 5 “exit” $sim run arches 74% ns simple. tcl Hello World! arches 75%

Discrete Event Simulation q Model world as events v. Simulator has list of events

Discrete Event Simulation q Model world as events v. Simulator has list of events v. Process: take next one, run it, until done v. Each event happens in an instant of virtual (simulated) time, but takes an arbitrary amount of real time q Ns uses simple model: single thread of control => no locking or race conditions to worry about (very easy)

Discrete Event Examples Consider two nodes on an Ethernet: A B simple queuing model:

Discrete Event Examples Consider two nodes on an Ethernet: A B simple queuing model: t=1, A enqueues pkt on LAN t=1. 01, LAN dequeues pkt and triggers B t=1. 0: A sends pkt to NIC A’s NIC starts carrier sense detailed t=1. 005: A’s NIC concludes cs, CSMA/CD starts tx model: t=1. 006: B’s NIC begins reciving pkt t=1. 01: B’s NIC concludes pkt B’s NIC passes pkt to app

ns-2 Environment Simulation Scenario 1 2 set ns_ [new Simulator] Tcl Script set node_(0)

ns-2 Environment Simulation Scenario 1 2 set ns_ [new Simulator] Tcl Script set node_(0) [$ns_ node] set node_(1) [$ns_ node] C++ Implementation class Mobile. Node : public Node { friend class Position. Handler; public: Mobile. Node(); • • }

Why two languages? (Tcl & C++) q C++: Detailed protocol simulations require systems programming

Why two languages? (Tcl & C++) q C++: Detailed protocol simulations require systems programming language vbyte manipulation, packet processing, algorithm implementation v. Run time speed is important v. Turn around time (run simulation, find bug, fix bug, recompile, re-run) is slower q Tcl: Simulation of slightly varying parameters or configurations vquickly exploring a number of scenarios viteration time (change the model and re-run) is more important

C++ and OTcl Separation q “data” / control separation v. C++ for “data”: Ø

C++ and OTcl Separation q “data” / control separation v. C++ for “data”: Ø per packet processing, core of ns Ø fast to run, detailed, complete control v. OTcl for control: Ø Simulation scenario configurations Ø Periodic or triggered action Ø Manipulating existing C++ objects Ø fast to write and change + running vs. writing speed – Learning and debugging (two languages)

Using ns Problem Result analysis Simulation model Setup/run simulation with ns Modify ns

Using ns Problem Result analysis Simulation model Setup/run simulation with ns Modify ns

Summary: Generic Script Structure set ns [new Simulator] # [Turn on tracing] # Create

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

Basic Tcl variables: set x 10 puts “x is $x” functions and expressions: set

Basic Tcl variables: set x 10 puts “x is $x” functions and expressions: set y [pow x 2] set y [expr x*x] control flow: if {$x > 0} { return $x } else { return [expr -$x] } while { $x > 0 } { puts $x incr x – 1 } procedures: proc pow {x n} { if {$n == 1} { return $x } set part [pow x [expr $n-1]] return [expr $x*$part] } Also lists, associative arrays, etc. => can use a real programming language to build network topologies, traffic models, etc.

Basic otcl Class Person # constructor: Person instproc init {age} { $self instvar age_

Basic otcl Class Person # constructor: Person instproc init {age} { $self instvar age_ set age_ $age } # method: Person instproc greet {} { $self instvar age_ puts “$age_ years old: How are you doing? ” } Person a 45 Kid b 15 a greet b greet # subclass: Class Kid -superclass Person Kid instproc greet {} { $self instvar age_ puts “$age_ years old kid: What’s up, dude? ” } set a [new Person 45] set b [new Kid 15] $a greet $b greet # “new” in ns 2 only => can easily make variations of existing things (TCP, TCP/Reno)

Otcl and C++: The Duality C++/OTcl split objects otcl q OTcl (object variant of

Otcl and C++: The Duality C++/OTcl split objects otcl q OTcl (object variant of Tcl) and C++ share class hierarchy q Tcl. CL is glue library that makes it easy to share functions, variables, etc

C++ and OTcl Linkage q Class Tcl: instance of OTcl interpreter Tcl& tcl =

C++ and OTcl Linkage q Class Tcl: instance of OTcl interpreter Tcl& tcl = Tcl: : instance(); tcl. evalc(“puts stdout hello world”); tcl. result() and tcl. error() q Class Tcl. Object and Tcl. Class v. Variable bindings bind(“rtt_”, &t_rtt_) v. Invoking command method in shadow class $tcp advanceby 10

C++ and Otcl linkage II q. Some important objects: v. Ns. Object: has recv()

C++ and Otcl linkage II q. Some important objects: v. Ns. Object: has recv() method v. Connector: has target() and drop() v. Bi. Connector: uptarget() & downtarget() q目前悟性不夠

Tcl. Object: Hierarchy and Shadowing Tcl. Object OTcl class hierarchy Agent C++ class hierarchy

Tcl. Object: Hierarchy and Shadowing Tcl. Object OTcl class hierarchy Agent C++ class hierarchy Tcl. Object static JSTcp. Class : public Tcl. Class { public: JSTcp. Class(): Tcl. Class("Agent/TCP/JS"){} Tcl. Object* create(int, const char*const*) { return (new JSTcp. Agent()); } }; Agent JSTcp. Agent/TCP/JS _o 123 Agent/TCP/JS OTcl shadow object *tcp Agent/TCP/JS C++ object

Tcl. Object: Creation and Deletion Agent/TCP/JS constructor invoke parent constructor complete initialization Agent/TCP constructor

Tcl. Object: Creation and Deletion Agent/TCP/JS constructor invoke parent constructor complete initialization Agent/TCP constructor invoke parent constructor complete which C++ initialization object to create? Tcl. Object constructor create C++ object create OTcl shadow object – Tcl. Class Tcl. Object (C++) constructor parent (Agent) constructor JSTCPAgent constructor do nothing, return invoke parent constructor bind variables and return OTcl C++

Class Hierarchy in ns Tcl. Object Ns. Object Connector Queue Delay Agent Drop. Tail

Class Hierarchy in ns Tcl. Object Ns. Object Connector Queue Delay Agent Drop. Tail RED TCP Reno SACK Classifier Trace Addr. Classifier Mcast. Clasifier Enq Deq JS Drop

TCP Jump Start – Step 1 q New file: tcp-js. h class JSTcp. Agent

TCP Jump Start – Step 1 q New file: tcp-js. h class JSTcp. Agent : public Tcp. Agent { public: virtual void set_initial_window() { cwnd_ = MAXWIN_; } JSTcp. Agent(); private: int MAXWIN_; };

TCP Jump Start – Step 2 q New file: tcp-js. cc #include “tcp. h”

TCP Jump Start – Step 2 q New file: tcp-js. cc #include “tcp. h” #include “tcp-js. h” static JSTcp. Class : public Tcl. Class { public: JSTcp. Class() : Tcl. Class("Agent/TCP/JS") {} Tcl. Object* create(int, const char*const*) { return (new JSTcp. Agent()); } }class_jstcp; JSTcp. Agent: : JSTcp. Agent() { bind(“MAXWIN_”, &MAXWIN_); }

Tcl. Object: : bind() q Link C++ member variables to OTcl object variables q

Tcl. Object: : bind() q Link C++ member variables to OTcl object variables q C++ Tcp. Agent: : Tcp. Agent() { bind(“window_”, &wnd_); … … } vbind_time(), bind_bool(), bind_bw() (set with unit) q OTcl set tcp [new Agent/TCP] $tcp set window_ 200

Initialization of Bound Variables q. Initialization through OTcl class variables Agent/TCP set window_ 50

Initialization of Bound Variables q. Initialization through OTcl class variables Agent/TCP set window_ 50 q. Do all initialization of bound variables in ~ns/lib/ns-default. tcl v. Otherwise a warning will be issued when the shadow object is created v. After modifying, remember to make ns 2

Calling C++ functions from Otcl q OTcl set tcp [new Agent/TCP] $tcp advance 10

Calling C++ functions from Otcl q OTcl set tcp [new Agent/TCP] $tcp advance 10 q C++ int Tcp. Agent: : 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); }

Tcl. Object: : command() $tcp send OTcl space no such procedure Tcl. Object: :

Tcl. Object: : command() $tcp send OTcl space no such procedure Tcl. Object: : unknown{} $tcp cmd send C++ space Tcp. Agent: : command() Yes process and return match “send”? No Invoke parent: return Agent: : command()

Calling Otcl functions from C++ q OTcl Agent/TCP instproc advance {num} { set window_

Calling Otcl functions from C++ q OTcl Agent/TCP instproc advance {num} { set window_ [expr $window_ + $num] return $window_ } q C++ Tcl& tcl = Tcl: : instance(); char *result; tcl. evalf(“%s advance %d”, name_, size); result = tcl. result(); wnd_ = atoi(result);

Embedded. Tcl q. How it works vtcl 2 c++: provided by Tcl. CL, converts

Embedded. Tcl q. How it works vtcl 2 c++: provided by Tcl. CL, converts tcl scripts into a C++ static character array v. Makefile. in: tclsh 8. 0 bin/tcl-expand. tcl/lib/nslib. tcl | tcl 2 c++ et_ns_lib > gen/ns_tcl. cc

Summary q. Tcl. Object v. Unified interpreted (OTcl) and compiled (C++) class hierarchies v.

Summary q. Tcl. Object v. Unified interpreted (OTcl) and compiled (C++) class hierarchies v. Seamless access (procedure call and variable access) between OTcl and C++ q. Tcl. Class v. The mechanism that makes Tcl. Object work q. Tcl: primitives to access Tcl interpreter

Event Scheduler q Create event scheduler v set ns [new Simulator] q Schedule events

Event Scheduler q Create event scheduler v set ns [new Simulator] q Schedule events v $ns at <time> <event> Ø <event>: any legitimate ns/tcl commands v $ns at 0. 1 “$ftp start” v $ns at 4. 0 “$ftp stop” v $ns at 5. 0 “finish” q Start scheduler v $ns run q ns/common/scheduler. cc v Scheduler: : command() “now”, “at” q ns/link/delay. cc v Link. Delay: : recv() “s. schedule()”

Extending ns in OTcl ns-allinone Tcl 8. 0 TK 8. 0 OTcl tclcl. .

Extending ns in OTcl ns-allinone Tcl 8. 0 TK 8. 0 OTcl tclcl. . . tcl ex examples test validation tests mysrc msg. tcl ns-2 lib nam-1 C++ code mcast OTcl code . . .

Add Your Changes into ns vsource your changes in your sim scripts q. Or

Add Your Changes into ns vsource your changes in your sim scripts q. Or vadd to tcl/lib/ns-lib. tcl … source. . /mysrc/msg. tcl v. Change Makefile NS_TCL_LIB = tcl/mysrc/msg. tcl … v. Recompile

Scalability vs Flexibility q It’s tempting to write all-OTcl simulation v. Benefit: quick prototyping

Scalability vs Flexibility q It’s tempting to write all-OTcl simulation v. Benefit: quick prototyping v. Cost: memory + runtime q Solution v. Control the granularity of your split object by migrating methods from OTcl to C++ q Conventional Wisdom: v. C++ for “data” Ø Per packet action v. OTcl for control Ø Periodic or triggered action

THE Merit of OTcl high Program size, complexity low OTcl C/C++ split objects q

THE Merit of OTcl high Program size, complexity low OTcl C/C++ split objects q Smoothly adjust the granularity of scripting to balance extensibility and performance q With complete compatibility with existing simulation scripts

Object Granularity Tips q. Functionality v. Per-packet processing C++ v. Hooks, frequently changing code

Object Granularity Tips q. Functionality v. Per-packet processing C++ v. Hooks, frequently changing code OTcl q. Data management v. Complex/large data structure C++ v. One-time configuration variables OTcl

Memory Conservation Tips q. Avoid trace-all q. Use arrays for a sequence of variables

Memory Conservation Tips q. Avoid trace-all q. Use arrays for a sequence of variables v. Instead of n$i, say n($i) n$i q. Avoid OTcl temporary variables q. Use dynamic binding vdelay_bind() instead of bind() () v. See object. {h, cc}

Memory Leaks q. Purify or dmalloc, but be careful about split objects: for {set

Memory Leaks q. Purify or dmalloc, but be careful about split objects: for {set i 0} {$i < 500} {incr i} { set a [new Random. Variable/Constant] } v. It leaks memory, but can’t be detected! q. Solution v. Explicitly delete EVERY split object that was new-ed

Backup slide

Backup slide

OTcl Linkage q OTcl Linkage是c++和OTcl 的一個介面。 C++ code OLcl Linkake OTcl code 描述 名稱

OTcl Linkage q OTcl Linkage是c++和OTcl 的一個介面。 C++ code OLcl Linkake OTcl code 描述 名稱 繼承 C++ object My. Agent class Linkage My. Agent TCL CLass object CLass OTcl object Agent/My. A gent. Otcl

OTcl Linkage

OTcl Linkage

如何使用OTcl linkage q Export C++ class variables to OTcl變數 C++變數 q - bind(): real

如何使用OTcl linkage q Export C++ class variables to OTcl變數 C++變數 q - bind(): real or integer variables - bind_time(): time variable - bind_bw(): bandwidth variable - bind_bool(): boolean variable 請在ns-2/tcl/lib/ns-lib. tcl 設預設值,否則會有警告 message

如何使用OTcl linkage q Export C++ Object Control Commands to OTcl q This is done

如何使用OTcl linkage q Export C++ Object Control Commands to OTcl q This is done by defining a "command" member function of your C++ object, which works as an OTcl command interpreter. q When OTcl object( is created and the user tries to call a member function of that object (i. e. $myagent call-my-priv-func), OTcl searches for the given member function name in the OTcl object. If the given member function name cannot be found, then it invokes the "My. Agent: : command" passing the invoked OTcl member function name and arguments in argc/argv format.

如何使用OTcl linkage q Export C++ Object Control Commands to OTcl q -

如何使用OTcl linkage q Export C++ Object Control Commands to OTcl q -

如何使用OTcl linkage q Execute an OTcl command from C++. q makes an OTcl interpreter

如何使用OTcl linkage q Execute an OTcl command from C++. q makes an OTcl interpreter print out the value in "my_var 1" and "my_var 2" private member variables q To execute an OTcl command from C++, you should get a reference to "Tcl: : instance()"

如何使用OTcl linkage q Compile, run and test "My. Agent“ q put "ex-linkage. cc" file,

如何使用OTcl linkage q Compile, run and test "My. Agent“ q put "ex-linkage. cc" file, and save it under the "ns 2" directory. q Open "Makefile", add "ex-linkage. o" at the end of object file list. q Re-compile NS using the "make" command. q Run the OTcl script using command "ns exlinkage. tcl".

如何使用OTcl linkage

如何使用OTcl linkage

如何使用OTcl linkage

如何使用OTcl linkage

C++ Guidelines q. Decide position in class hierarchy vi. e. , which class to

C++ Guidelines q. Decide position in class hierarchy vi. e. , which class to derive from? q. Create new packet header (if necessary) q. Create C++ class, fill in methods q. Define OTcl linkage (if any) q. Write OTcl code (if any) q. Build (and debug)

Where to Find What?

Where to Find What?

Where to Find What? q ns-lib. tcl: The simulator class and most of its

Where to Find What? q ns-lib. tcl: The simulator class and most of its member function definitions except for LAN, Web, and Multicast related ones are located here. q ns-default. tcl: The default values for configurable parameters for various network components are located here. the parameters are actually C++ variables made available to OTcl via an OTcl linkage function q bind(C++_variable_name, OTcl_variable_name)

Where to Find What? q ns-packet. tcl: The packet header format initialization implementation is

Where to Find What? q ns-packet. tcl: The packet header format initialization implementation is located here. When you create a new packet header, you should register the header in this file to make the packet header initialization process and give you the offset of your header in the stack. q other OTcl files: Other OTcl files in this directory, The FTP application is entirely implemented in OTcl and the source code is located in "ns-source. tcl".