tcl/ns2 basicsilenia/course/01-tcl.pdfns2 architecture ns is an object-oriented tcl(otcl) script...

30
TCL/NS2 Basics TCL/NS2 Basics Daniele Messina, Ilenia Tinnirello

Upload: others

Post on 24-Jul-2020

36 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

TCL/NS2 Basics TCL/NS2 Basics

Daniele Messina, Ilenia Tinnirello

Page 2: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

NS2 ArchitectureNS2 Architecture

�NS is an Object-oriented Tcl (OTcl) script interpreter written in C++.

�It has a simulation event scheduler, network component object libraries, and network set-up module libraries.

Daniele Messina, Ilenia Tinnirello

OTcl Script

OTcl: Tcl interpreter with Object Oriented extension

Event scheduler ObjectsNetwork component Objects

Network setup helping Modules

Simulationresults (.tr, .nam)

Analysis(Awk, Perl, Tcl)

Visualization(NAM)

SCENARIO

PROCESSING

STATISTICSOUTPUT

Page 3: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

InstallationInstallation(for windows)(for windows)

�3 componets:

�ns2 simulator

�Tcl (script interpreter for scenario definitions)

�Nam (visual trace interpreter)

Daniele Messina, Ilenia Tinnirello

�Nam (visual trace interpreter)

�available from:�http://www.usq.edu.au/users/leis/notes/software/ns2win.html

� just copy the .exe files! (and updates path if you want)

Page 4: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Starting nsStarting ns

�You start ns with the command 'ns <tclscript>' (assuming that you are in the directory with the ns executable, or that your path points to that directory), where '<tclscript>' is the name of a Tcl script file which defines the simulation scenario (i.e. the topology and the events).

Daniele Messina, Ilenia Tinnirello

Tcl script file which defines the simulation scenario (i.e. the topology and the events).

�You could also just start ns without any arguments and enter the Tcl commands in the Tcl shell, but that is definitely less comfortable

Page 5: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Tcl commandsTcl commands

�A Tcl script consists of one or more commands. Commands are separated by newlines and semi-colons. For example:set a 24

set b 15

�The same script could be written on a single line using a semi-colon separator:

Daniele Messina, Ilenia Tinnirello

The same script could be written on a single line using a semi-colon separator:set a 24; set b 15

�Each command consists of one or more words, where the first word is the name of a command and additional words are arguments to that command. Words are separated by spaces and tabs.

Page 6: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Tcl commandsTcl commands

set var1 5

words

Daniele Messina, Ilenia Tinnirello

set var1 5

name of the command

arguments

Page 7: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The set commandThe set command

� Remember that the Tcl parser doesn’t apply any meaning to the words of a command, meanings are applied by individual command procedures. For example, consider the following C program:

x = 4;

y = x+10;

� Now consider a similar-looking program written in Tcl:

Daniele Messina, Ilenia Tinnirello

� Now consider a similar-looking program written in Tcl:

set x 4

set y x+10

� The second command simply takes the string “x+10” and stores it as the new value for y. At the end of the script y has the string value “x+10”, not the integer value 14.

set x 4

set y [expr $x+10]

Page 8: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The set commandThe set command

�note that “set 2 a” is valid too: it creates a variable whose name is “2” and whose value is “a”.

�in any case this commands returns the new value of the variable, i.e. it

Daniele Messina, Ilenia Tinnirello

�in any case this commands returns the new value of the variable, i.e. it can be used for reading the value

Page 9: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Variable substitutionVariable substitution

�Variable substitution is triggered by a dollar-sign character and causes the value of a Tcl variable to be inserted into a word

Daniele Messina, Ilenia Tinnirello

set a 20

set b [expr $a*2]

�Command substitution is invoked by enclosing a nested command in brackets:

Page 10: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Variable substitutionVariable substitution

set name tset $name foo

�What varable is being set?

Daniele Messina, Ilenia Tinnirello

Page 11: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The expr commandThe expr command�expr concatenates its arguments into a single string and evaluates the string as an arithmetic expression.

�The expr command supports an expression syntax similar to that of expressions in ANSI C, including the same precedence rules and most of the C operators.

Daniele Messina, Ilenia Tinnirello

the C operators.

�examples:

% expr 3 << 2

12

% expr 14.1*6

84.6

% expr (3 > 4) || (6 <= 7)

1

Page 12: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The incr commandThe incr command

� Adds increment to the value of variable varName. Increment and the old value of varName must both be integer strings (decimal, hexadecimal, or octal). If increment is omitted then it defaults to 1. The new value is stored in varName as a decimal string and returned as the result of the command.

Daniele Messina, Ilenia Tinnirello

incr varName ?increment?

set a 5

set b 2

incr a 2

incr a $b

Page 13: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The if commandThe if command�The if command evaluates an expression, tests its

result, and conditionally executes a script based on the result.

if {$x < 0} {

...

} elseif {$x == 0} {

...

} elseif {$x == 1} {

Daniele Messina, Ilenia Tinnirello

} elseif {$x == 1} {

...

} else {

...

}

�The result of the command will be the result of whichever script is executed. If an if command has no else clause and none of its tests succeeds then it returns an empty string.

Page 14: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

ProceduresProcedures

�A Tcl procedure is a command that is implemented with a Tcl script.

�Procedures are created with the proc command, as in the following example:

proc plus {a b} {expr $a+$b}

Daniele Messina, Ilenia Tinnirello

proc plus {a b} {expr $a+$b}

�The first argument to proc is the name of the procedure to be created, plus in this case. The second argument is a list of names of arguments to the procedure ( a and b in the example). The third argument to proc is a Tcl script that forms the body of the new procedure.

Page 15: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Variable scopeVariable scope

�When referring to variables inside a procedure, Tcl will (by default) only recognize variables that were also defined in that procedure.

�A procedure can reference global variables with the global command. For example, the

Daniele Messina, Ilenia Tinnirello

with the global command. For example, the following command makes the global variables x and y accessible inside a procedure:

global x y

Page 16: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Examples: argsExamples: args

� If the last argument in the argument list is the special value args, then the procedure may be called with varying numbers of arguments.

� The procedure’s local variable args will be set to a list whose elements are all of the extra arguments. If there are no extra arguments then args will be set to an empty string.

Daniele Messina, Ilenia Tinnirello

proc sum args {

set s 0

foreach i $args {

incr s $i

}

return $s

}

sum 1 2 3 4 5

15

Page 17: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Accessing filesAccessing files

�The open command is used for opening a file.

set f [open “trace.tr” r]

�open returns a string that identifies the open file. This file identifier is used when invoking other commands to manipulate

Daniele Messina, Ilenia Tinnirello

open file. This file identifier is used when invoking other commands to manipulate the open file, such as gets, puts, and close.

close $f

Page 18: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The puts commandThe puts command

puts ?-nonewline? ?fileId? string

�Writes string to fileId, appending a newline character unless -nonewline is specified. FileId defaults to stdout.

�Returns an empty string.�The puts command uses buffering. Output will be

Daniele Messina, Ilenia Tinnirello

�The puts command uses buffering. Output will be saved in the application's memory, until a large amount of data has accumulated for the file.

�The flush command forces any buffered data to be written to the file.

flush $f

Page 19: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

The new commandThe new command

�new is used for creating objects. It returns a “reference” to the created object.set fred [new Father]

$fred age 40

Daniele Messina, Ilenia Tinnirello

$fred age 40

�destroy is used for destroying objects:$fred destroy

john destroy

Page 20: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Scripts, eventsScripts, events

� An OTcl script will do the following:

�Initiates an event scheduler.

� The event scheduler fires the events in the queue.

Daniele Messina, Ilenia Tinnirello

� An event is an object with an associated Id, a scheduled time, and a pointer to an object that handles the event

Page 21: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

SimulatorSimulator

� set ns [new Simulator]: generates an NS simulator object instance. This line:

� initializes the packet format

� creates a scheduler: now, run, at, halt.

� The "Simulator" object has member functions that:

Daniele Messina, Ilenia Tinnirello

� The "Simulator" object has member functions that:

� create compound objects such as nodes and links

� connect network component objects created (ex. attach-agent)

� set network component parameters (mostly for compound objects)

� create connections between agents (ex. make connection between a "tcp" and "sink")

� specify NAM display options

� etc.

Page 22: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Random CommandsRandom Commands

� set rng [new RNG]� $rng seed <n>� $rng next-random� $rng uniform <a> <b>� $rng exponential� $rng integer <k>� set rv [new Randomvariable/<type>]

$rv use-rng <rng>

Daniele Messina, Ilenia Tinnirello

� $rv use-rng <rng>

Page 23: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

SimulatorSimulator

� set ns [new Simulator]: generates an NS simulator object instance. This line:

� initializes the packet format

� creates a scheduler: now, run, at, halt.

� The "Simulator" object has member functions that:

Daniele Messina, Ilenia Tinnirello

� The "Simulator" object has member functions that:

� create compound objects such as nodes and links

� connect network component objects created (ex. attach-agent)

� set network component parameters (mostly for compound objects)

� create connections between agents (ex. make connection between a "tcp" and "sink")

� specify NAM display options

� etc.

Page 24: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

M/M/1 SimulationM/M/1 Simulation

Daniele Messina, Ilenia Tinnirello

Page 25: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

InitializationInitialization

#generation simulator object, parameters and state variables

set ns [new Simulator]

set tf [open out.tr w]

set lambda 1.0; set mu 2.0;

set N 0; set alpha 0; set delta 0; set queue 0

Daniele Messina, Ilenia Tinnirello

# generate random interarrival times and packet sizes

set InterArrivalTime [new RandomVariable/Exponential]

$InterArrivalTime set avg_ [expr 1/$lambda]

set ServiceTime [new RandomVariable/Exponential]

$ServiceTime set avg_ [expr 1.0/$mu]

Page 26: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Event SchedulingEvent Scheduling

Main events:Customer arrival Customer departure

The first arrival has to be set in the tcl script, all the others are recursively computes

Daniele Messina, Ilenia Tinnirello

time

Tcl script: puts an arrival event

Arrival proc: pick a random

interval for the next arrival; if the

queue is empty, choose a

random service time

departure proc:

If the queue is not

empty, schedule

next departure,

otherwise update N

and queue only

Page 27: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

ProceduresProcedures

proc new_arrival {} {

global time N alpha InterArrivalTime ns ServiceTime queue

incr N

incr alpha

if { $N > 1} {

incr queue

} else {

Daniele Messina, Ilenia Tinnirello

} else {

set time_service [expr $time + [$ServiceTime value] ]

$ns at $time_service "new_departure"

}

#Schedule next arrival

set time_arrival [expr $time + [$InterArrivalTime value] ]

$ns at $time_arrival "new_arrival"

}

Page 28: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

ProceduresProcedures

proc new_departure {} {

global N delta ServiceTime ns queue

incr N -1

incr delta

if {$queue > 0} {

incr queue -1

Daniele Messina, Ilenia Tinnirello

incr queue -1

#Schedule next service

set time_service [expr $time + [$ServiceTime value] ]

$ns at $time_service "new_departure"

}

}

Page 29: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

Event initializationEvent initialization

$ns at 0.0001 "new_arrival"

$ns at 30.0 "exit"

$ns run

Daniele Messina, Ilenia Tinnirello

Page 30: TCL/NS2 Basicsilenia/course/01-TCL.pdfNS2 Architecture NS is an Object-oriented Tcl(OTcl) script interpreter written in C++. It has a simulation event scheduler, network component

ExercisesExercises1. Analize the simulation traces for different lamba and mu

parameters.

2. Generalize previous script to the M server case.

3. Generalize previous script by assuming that queue capacity isfinite and equal to 10 customers.

Daniele Messina, Ilenia Tinnirello