Docsity

Experimental Networking (ECSE 4963) Lab 2: Network Simulator ns2, Lab Reports of Electrical and Electronics Engineering

Partial preview of the text

Download Experimental Networking (ECSE 4963) Lab 2: Network Simulator ns2 and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity!

Experimental Networking (ECSE 4963)

Lab 2, Network Simulator ns

Shiv Kalyanaraman^ Yong Xia (TA)^ Satish Raghunath [email protected]

http://www.ecse.rpi.edu/Homepages/shivkuma

Sept 5, 2002

Ns2 Tutorial

ƒ^ Languages ƒ^ Ns2 ƒ^ Nam ƒ^ An example project ƒ^ Preview of the next lab

Languages

ƒ^ System language: C, C++, Java^ -^ Build data structures and algorithms from scratch^ -^ Strongly typed to manage complexity^ -^ Compiled, high efficiency, 10~20x faster ƒ^ Scripting language: Perl, Tcl(OTcl), Unix shell^ -^ Rapid high level programming: to “glue” applicatons^ -^ Typeless to simplify connections btwn components^ -^ Interpreted, less efficient^ -^ Sacrifice execution speed for development speed(5~10x faster than system language for gluing dev)

Ns

• NS is a “Network Simulator”^ – Can setup network topologies^ – Generate packet traffic similar to Internetand measure various parameters • NS is needed because:^ – Need to verify utility / feasibility of newalgorithms / architectures^ – Actual topologies Expensive / Error prone /Time consuming to setup

Ns

•^ Most of the NS2 source code is in C++^ – http://www.cplusplus.com/doc/tutorial/^ – http://www.isi.edu/nsnam/ns^ – http://www.isi.edu/nsnam/ns/ns-documentation.html •^ Tcl is a simple scripting language used in conjunction withOtcl to setup simulation topologies and scenarios.^ – http://dev.scriptics.com/man/tcl8.2.3/ •^ OTcl adds Object orientation to Tcl^ – http://bmrc.berkeley.edu/research/cmt/cmtdoc/otcl/tu^ torial.html •^ NAM – Network Animator is used to visualize simulations^ – http://www.isi.edu/nsnam/nam

Tcl

•^ expr 20 + 10 •^ set x 32 •^ set cmd expr; set x 11; $cmd $x$x •^ set a 44; set b [expr $a4] •^ set x 24; set y 18; set z "$x + $y is [expr $x + $y]" •^ set x 24; set y 18; set z {$x + $y is [expr $x + $y]} •^ proc power {base p} {^ set result 1^ while {$p > 0} {

set result [expr $result * $base] set p [expr $p - 1] } return $result

•^ Further: http://www.beedub.com/book/2nd/tclintro.doc.html

[email protected]

A Simple Tcl Script (contd.)

ƒ^ You can run the Tcl script using the program “tclsh” as: ~>^ tclsh^ simple.tcl Executing simple^ tcl

script Read^ line:

Writing^ a^ sentence

into^ file

~> ƒ^ Let us observe the syntax:^ -^ Lines beginning with “#” are treated as comments^ -^ The symbol “$” is used to obtain the

contents^

of a variable

-^ The “set” method is used to assign values to variables. Note that the “$”does not appear when something is being assigned to the variable -^ The effect of parentheses in math is obtained by using [] – e.g., [open …]indicates that the code in the brackets is evaluated first and then assignedto f1 -^ “puts”, “gets”, “open” are all Tcl commands. “puts $f1 …” indicates thatwe are pas

sing contents of f1 as a parameter to puts

ƒ^ Simple.tcl thus opens a file called “try”, writes a sentence andreads from it

[email protected]

Exercise 1- Loops and Lists ƒ This exercise will introduce you to

loops and lists

in Tcl.

ƒ^ A file contains information about path in a network. The path is specifiedas a list of

numbers: 1 15 7 25 3 25 2 10 5 … to indicate that node 1 is

connected to node 7 with a link of 15Mbps, node 7 to node 3 with 25Mbpsand so on. Write a tcl script to read this file and interpret its contents.Your output should look like: Link 1: Node 1 to Node 7; Bandwidth: 15M Link 2: Node 7 to Node 3; Bandwidth: 25M ƒ You might want to use the following functions: # Create an empty list in l1 set l1 [list] # Concatenate a list/string with another and assign to l2 set l2 [concat $l1 $s1] # Access^ an

element i from the list l

set el1 [lindex $l2 $i] # Execute a statement n times for {set i 0} { $i < n } {incr i} { ... }

[email protected]

Otcl Examples

ƒ^ A “class” is like a struct with facilities for privateand public variables, member functions andinheritance ƒ^ Lets examine the topology class: Class^ Topology Topology^

instproc^

init^ {^ }^

$self^ instvar

nodes^ ns set^ ns^ [Simulator

instance] set^ nodes(1)

[$ns^ node] set^ nodes(2)

[$ns^ node]

[email protected]

Otcl Examples (contd.)

ƒ^ To understand all aspects in the above example you have toknow the basics of Object oriented programming. I will assumethat you know at least the meaning of these terms: memberfunctions, static functions/variables, instances, constructors ƒ^ The first line is the declaration of “Topology” as a class ƒ^ The function “init” is the equivalent of constructor in C++ - it isthe first function called when an instance of this class is createdby “new” operator. ƒ^ $self is equivalent to the “this” pointer in C++. It refers to thepresent instance within which the function is executing – that isit refers to “itself” ƒ^ “instvar” is used to declare a member variable and similarly“instproc” is used to declare a member function. The syntax of aprocedure is similar to that in Tcl except that the class namehas to come first and the “proc” keyword is replaced byinstproc. The empty braces (“{}”) indicate that the proceduretakes no parameters.

OTcl

ƒ^ Stop here, let students do OTcl program

How ns2 works

An example: how to start

ƒ^ Create a event scheduler^ -^

set ns [new Simulator]

ƒ^ Open a file for trace data^ -^

set nf [open out.nam w] - $ns namtrace-all $nf

An example: how to start

ƒ^ A procedure to close file and start NAM^ -^

proc finish {} {^ global ns nf^ $ns flush-trace^ close $nf^ exec nam out.nam &^ exit 0^ }

ƒ^ Schedule the procedure^ -^

$ns at 5.0 "finish"

ƒ^ Start simulation^ -^

$ns run