Presentation is loading. Please wait.

Presentation is loading. Please wait.

NS-2 Tutorial. Motivation What’s NS-2 Network simulator –Discrete event simulator It covers multiple layers –Application layer, transport layer, network.

Similar presentations


Presentation on theme: "NS-2 Tutorial. Motivation What’s NS-2 Network simulator –Discrete event simulator It covers multiple layers –Application layer, transport layer, network."— Presentation transcript:

1 NS-2 Tutorial

2 Motivation

3 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

4 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 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) Discrete Event Simulation

5 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

6 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.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

7 Why Two Languages Simulator had two distinct requirementsSimulator 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 changeC++ is fast to run but slower to change Otcl runs much slower but can be changed quicklyOtcl runs much slower but can be changed quickly

8 OTcl and C++: The Duality C++ OTcl Pure C++ objects Pure OTcl objects C++/OTcl split objects ns Split Object: Object created in Otcl has a corresponding object in C++.

9 ns Directory Structure TK8.0OTcltclclTcl8.0ns-2nam-1 tcl extest lib... examples validation tests C++ code OTcl code ns-allinone mcast

10 Network Topology: Node n0n1 Addr Classifier Port Classifier classifier_ dmux_ entry_ Node entry Unicast Node Multicast Classifier classifier_ dmux_ entry_ Node entry Multicast Node multiclassifier_ Classifier : Address, Multicast, Multipath, Hash

11 Network Topology: Link n0n1 enqT_queue_deqT_ drophead_ drpT_ link_ttl_ n1 entry_ head_ tracing simplex link duplex link

12 Routing n0n1 Addr Classifier Port Classifier classifier_ dmux_ entry_ Node entry 0 1 enqT_queue_deqT_ drophead_drpT_ link_ttl_ n1 entry _ head_

13 Routing (con’t) n0n1 Addr Classifier Port Classifier classifier_ dmux_ entry_ 0 1 Addr Classifier Port Classifier classifier_ dmux_ entry_ 1 0 Link n0-n1 Link n1-n0

14 Transport 0 1 n0n1 Addr Classifier Port Classifier classifier_ dmux_ entry_ 0 Agent/TCP agents_ Addr Classifier Port Classifier classifier_ dmux_ entry_ 1 0 Link n0-n1 Link n1-n0 0 Agent/TCPSink agents_ dst_=1.0 dst_=0.0

15 Application: Traffic Generator 0 1 n0n1 Addr Classifier Port Classifier classifier_ dmux_ entry_ 0 Agent/TCP agents_ Addr Classifier Port Classifier classifier_ dmux_ entry_ 1 0 Link n0-n1 Link n1-n0 0 Agent/TCPSink agents_ dst_=1.0 dst_=0.0 Application/FTP

16 Plumbing: Packet Flow 0 1 n0n1 Addr Classifier Port Classifier entry_ 0 Agent/TCP Addr Classifier Port Classifier entry_ 1 0 Link n0-n1 Link n1-n0 0 Agent/TCPSink dst_=1.0 dst_=0.0 Application/FTP

17 Running NS-2 Program

18 Resources http://www.isi.edu/nsnam/ns Tcl (Tool Command Language) –http://dev.scriptics.com/scriptinghttp://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

19 Outline What’s NS-2 NS-2 Structures TCL(Tool Command Language) Basics OTCL Basics Examples

20 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.

21 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

22 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

23 More examples set var 5 –=> 5 set b $var –=> 5 The set command is used to assign a value to a variable

24 Variable Substitution Variable substitution –i: the character ‘i’. –$i: the variable i. –set v1 6 –set v2 $v1 (variable substitution)

25 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 + 6 wrong number of arguments –set i {5 + 6} 5 + 6 –set i [5 + 6] invalid command –set i [expr 5 + 6] 11

26 Math Expressions –expr 8 / 2 => 4 –set len [expr [string length foobar] + 7] =>13

27 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 –puts stdout “ 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]

28 More on Tcl Control Structures –if {condition} then {…….} –for {set i 0} {$i < 10} {incr i 2} {……} Procedures –proc proc_name {arg1 arg2…} { ……}

29 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

30 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 = 4

31 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 –$self instvar variable_name (inside a class method)

32 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]

33 Examples Class Mom Mom instproc greet {} { $self instvar age_ puts “$age_ years old mom: How are you doing?” } Class Kid -superclass Mom Kid instproc greet {} { $self instvar 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?

34 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:

35 (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

36 (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

37 (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

38 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

39 Topology

40 Step 1: Creating Event Scheduler Create event scheduler –set ns [new Simulator] Schedule events –$ns at – : any legitimate ns/tcl commands Start scheduler –$ns run

41 Step 2: Tracing Trace packets on all links –$ns trace-all [open test.out w] -- -- + 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

42 Tracing Trace all –set nf [open out.ns w] –$ns trace-all $nf Turn on tracing on specific links

43 Step 3: Creating Network Nodes –set n0 [$ns node] –set n1 [$ns node] Links and queuing –$ns duplex-link $n0 $n1 – : DropTail, RED, CBQ, FQ, SFQ, DRR

44 #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

45 Step 4: Creat transport conneciton TCP 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

46 Step 4: Creat transport conneciton UDP 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

47 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

48 Creating Traffic: (On Top of TCP) FTP set ftp [new Application/FTP] $ftp attach-agent $src_tcp $ftp set packetSize_ 512

49 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


Download ppt "NS-2 Tutorial. Motivation What’s NS-2 Network simulator –Discrete event simulator It covers multiple layers –Application layer, transport layer, network."

Similar presentations


Ads by Google