490 likes | 661 Views
NS-2 Tutorial. Motivation. What’s NS-2. Network simulator Discrete event simulator It covers multiple layers Application layer, transport layer, network layer and link layer. Supports the simulation of
E N D
What’s NS-2 • Network simulator • Discrete event simulator • It covers multiple layers • Application layer, transport layer, network layer and link layer. • Supports the simulation of • intserv/diffserv, Multicast, Transport, Applications, Wireless (fixed, mobile, satellite) • Packet level
Discrete Event Simulation Model world as events – simulator has list of events ordered by simulation time – process: take next one, run it, until done – each event happens in an instant of virtual (simulated) time, but takes an arbitrary amount of real time NS uses simple model: single thread of control => no locking or race conditions to worry about (very easy)
History and Status • REAL variant (1989)DARPA (LBL, Xerox PARC, UCB, and USC/ISI) (1995) • Currently; DARPA; NSF; collaboration with researchers: ACIRI, UCB Daedelus, CMU, Sun Microsystems, NORTEL, Cisco • ns-1 • ns-2 • 100K lines of C++ • 70K lines of OTcl • 30K lines of test suite • 20K lines of documentation • Current version is 2.28
ns-2 Programming Languages • NS-2 is an object oriented simulator, written in C++, with an OTcl (Object Tool Command Language) interpreter as a front-end. • Back-end C++ • Defining new agents, protocols and framework. • Manipulations at the byte/bit levels. • if you have to change the behaviour of an existing C++ class in ways that weren't anticipated • Front-end Otcl Topologies, scenarios, simulations, … • Script language (easy topology modifications) • if you can do what you want by manipulating existing C++ objects
Why Two Languages • Simulator had two distinct requirements • Detailed simulation of Protocol(Run-time speed) • Varying parameters or configuration(Change model & rerun) • C++ is fast to run but slower to change • Otcl runs much slower but can be changed quickly
OTcl and C++: The Duality • Split Object: • Object created in Otcl has a corresponding object in C++. Pure OTcl objects Pure C++ objects C++/OTcl split objects C++ OTcl ns
ns Directory Structure ns-allinone Tcl8.0 TK8.0 OTcl tclcl ns-2 nam-1 C++ code ... tcl ex test mcast lib ... examples validation tests OTcl code
n0 n1 Port Classifier Multicast Node dmux_ Addr Classifier classifier_ Node entry Node entry dmux_ entry_ entry_ Multicast Classifier classifier_ multiclassifier_ Network Topology: Node Unicast Node Classifier : Address, Multicast, Multipath, Hash
n0 n1 duplex link n1 entry_ head_ enqT_ queue_ deqT_ link_ ttl_ drophead_ drpT_ tracing simplex link Network Topology: Link
n0 n1 Port Classifier Addr Classifier n1 entry_ 0 Node entry head_ dmux_ enqT_ queue_ deqT_ link_ ttl_ 1 entry_ classifier_ drophead_ drpT_ Routing
n0 n1 Port Classifier Port Classifier Addr Classifier Addr Classifier 0 1 dmux_ dmux_ 1 0 entry_ entry_ classifier_ classifier_ Link n1-n0 Routing (con’t) Link n0-n1
dst_=0.0 dst_=1.0 Agent/TCP Agent/TCPSink agents_ agents_ 0 1 1 0 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 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
dst_=0.0 dst_=1.0 0 1 1 0 Plumbing: Packet Flow n0 n1 Application/FTP Port Classifier Port Classifier Agent/TCPSink Addr Classifier Addr Classifier Agent/TCP 0 0 Link n0-n1 entry_ entry_ Link n1-n0
Resources • http://www.isi.edu/nsnam/ns • Tcl (Tool Command Language) • http://dev.scriptics.com/scripting • OTcl (MIT Object Tcl) • ~otcl/doc/tutorial.html (in distribution) • ns manual • Included in distribution: ~ns/doc • http://www.isi.edu/nsnam/ns/ns-documentation.html
Outline • What’s NS-2 • NS-2 Structures • TCL(Tool Command Language) Basics • OTCL Basics • Examples
Basic Syntax Tcl commands are evaluated in two steps: First the Tcl interpreter parses the commands into words, performing substitution along the way. Then a command procedure process the words to produce a result string. Each command has a separate command procedure.
Tcl Basics • Basic syntax • command arg1 arg2 arg3 … • command is either the name of a built-in command or a Tcl procedure • Three basic steps • Argument grouping • variable substitution, command substitution • Command invocation
First example: hello world! • Hello world! • puts stdout {Hello World!} • Two points to emphasize • Arguments are interpreted by the command • Curly braces are used to group words together into a single argument
More examples • set var 5 • => 5 • set b $var • => 5 • The set command is used to assign a value to a variable
Variable Substitution • Variable substitution • i: the character ‘i’. • $i: the variable i. • set v1 6 • set v2 $v1 (variable substitution)
Command Substitution • Command Substitution • set value [expr $v1+ $v2] • => set value 12 • Rewirte the outer command by using the result of the nested command • Operation substitution • set i 5 + 6wrong number of arguments • set i {5 + 6}5 + 6 • set i [5 + 6]invalid command • set i [expr 5 + 6]11
Math Expressions • Math Expressions • expr 8 / 2 • => 4 • set len [expr [string length foobar] + 7] • =>13
Grouping • Grouping: group words (argument) together into one argument • Grouping with curly braces • Curly braces prevent substitution in the group • Grouping with double quotes • Double quotes allow substitution to occur in the group • Example: • set a hello => hello • putsstdout “ The length of $a is [string length $a] ." • => The length of hello is 5 • puts stdout { The length of $a is [string length $a] .} • =>The length of $a is [string length $a]
More on Tcl • Control Structures • if {condition} then {…….} • for {set i 0} {$i < 10} {incr i 2} {……} • Procedures • proc proc_name {arg1 arg2…} { ……}
Example set a 43 set b 27 proc test { a b } { 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 43 27
Results k < 5, pow = 1.0 k < 5, pow = 1120.0 k < 5, pow = 1254400.0 k < 5, pow = 1404928000.0 k < 5, pow = 1573519360000.0 k >= 5, mod = 0 k >= 5, mod = 4 k >= 5, mod = 0 k >= 5, mod = 0 k >= 5, mod = 4
OTcl Basics • Creating a class • Class class_name • Class class_name –superclass Base_class • Defining instance procedures • class_name instproc proc_name {args} {…..} • Defining instance variables • $selfinstvar variable_name (inside a class method)
OTcl Basics • Creating an instance • set new_inst [new class_name] • Calling an instance procedure • $new_inst proc_name {args} • Using an instance value • $new_inst set v1 10 • set v2 [$new_inst set v1]
Examples 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?
NS2, and all of it's peer programs & dependencies are installed in/usr/ns/ on the Fedora machines in the Main Undergraduate Lab in Math Sciences. It is also available on the compute servers csc, csd, and cse. Some notes:
(1) You *must* put /usr/ns/otcl-1.9 and /usr/ns/lib into your LD_LIBRARY_PATH environment variable. • If you are using csh or tcsh, you can set it like: setenv LD_LIBRARY_PATH /usr/ns/otcl-1.9:/usr/ns/lib • If you are using sh or bash, you can set it like: export LD_LIBRARY_PATH=/usr/ns/otcl-1.9:/usr/ns/lib
(2) You *must* put /usr/ns/tcl8.4.5/library into your TCL_LIBRARY environment variable. Otherwise ns/nam will complain during startup. • If you are using csh or tcsh, you can set it like: setenv TCL_LIBRARY /usr/ns/tcl8.4.5/library • If you are using sh or bash, you can set it like: export TCL_LIBRARY=/usr/ns/tcl8.4.5/library
(3)You *should* put /usr/ns/bin into your PATH environment variable. Otherwise you will have to type the full path to ns/nam when you want to run them. • If you are using csh or tcsh, you can set it like: setenv PATH /usr/ns/bin:$PATH • If you are using sh or bash, you can set it like: export PATH=/usr/ns/bin:$PATH
Elements of ns-2 Simulation • Step 1: Create the event scheduler • Step 2: Turn on tracing • Step 3:Create network topology • Step 4: Create transport connection • Step 5: Create traffic on top of transport connection
Step 1: Creating Event Scheduler • Create event scheduler • set ns [new Simulator] • Schedule events • $ns at <time> <event> • <event>: any legitimate ns/tcl commands • Start scheduler • $ns run
Step 2: Tracing • Trace packets on all links • $ns trace-all [open test.out w] <event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst> <seq> <attr> + 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0 - 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0 r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0 • Trace packets on all links in nam format • $ns namtrace-all [open test.nam w] • Must appear immediately after creating scheduler
Tracing • Trace all • set nf [open out.ns w] • $ns trace-all $nf • Turn on tracing on specific links
Step 3: Creating Network • Nodes • set n0 [$ns node] • set n1 [$ns node] • Links and queuing • $ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type> • <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR
#Create 6 nodes for {set i 0} {$i < 6} {incr i} { set n($i) [$ns node] } #Create a duplex link between the nodes $ns duplex-link $n(0) $n(4) 10Mb 10ms DropTail $ns duplex-link $n(1) $n(4) 10Mb 10ms DropTail $ns duplex-link $n(4) $n(5) 1.5Mb 10ms DropTail $ns duplex-link $n(5) $n(3) 10Mb 10ms DropTail $ns duplex-link $n(5) $n(2) 10Mb 10ms DropTail $ns queue-limit $n(4) $n(5) 20
Step 4: Creat transport connecitonTCP • TCP #Create a TCP agent and attach it to node n(0) #set up connection between node 0 and node 3 set src_tcp [new Agent/TCP/Reno] $ns attach-agent $n(0) $src_tcp set dst_tcp [new Agent/TCPSink/DelAck] $ns attach-agent $n(3) $dst_tcp $ns connect $src_tcp $dst_tcp
Step 4: Creat transport connecitonUDP • UDP # UDP flow between n1 and n7 set src_udp [new Agent/UDP] $ns attach-agent $n(1) $src_udp set dst_udp [new Agent/LossMonitor] $ns attach-agent $n(2) $dst_udp $ns connect $src_udp $dst_udp
Step 5: Creating Traffic (On Top of UDP) • CBR #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $src_udp $cbr set type_ CBR $cbr set packetSize_ 512 $cbr set rate_ 0.8Mb
Creating Traffic: (On Top of TCP) • FTP set ftp [new Application/FTP] $ftp attach-agent $src_tcp $ftp set packetSize_ 512
Things to Remember • Topology, agents, sources, start • Connect the agents • Slot not found error • Start/Stop the sources • In procedures, declare global variables before use • “$ns run” – last line