56
1 Roma, Ottobre 2010

 · set ns [new Simulator] # Crea un oggetto simulator set nf [open out.nam w] # Apre in scrittura il file nam $ns namtrace-all $nf set trace_file [open traccia.tr w]

  • Upload
    dokiet

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

1

Roma, Ottobre 2010

2

3

4

5

6

if {$i > 0} {

puts “i è un intero positivo”

}

set <nome_variabile> <valore>

unset <nome_variabile>

while {$i < 100}{

set n($i) [new node]

incr i

}

for {set i 0}{$i < 100}{incr i}{

set n($i) [new node]

}

7

8

set ns [new simulator]

set nf [open out.nam w]

$ns namtrace-all $nf

9

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

$ns at 5.0 “finish”

$ns run

10

set nodo1 [$ns node]

for {set i 0} {$i < N} {incr i}

{set node$i [$ns node]}

set ns [ new Simulator ]

set nodo1 [ $ns node ] set nodo2 [ $ns node ] set nodo3 [ $ns node ]

set nodo4 [ $ns node ]

11

Nodo 1

Nodo 2

Nodo 3

Nodo 4

$ns simplex-link $nodo1 $nodo2 10Mb 1ms DropTail

$ns duplex-link $nodo3 $nodo4 100Mb 50ms DropTail

$ns queue-limit $nodo1 $nodo2 10

12

Tasso d’errore sui pacchetti pari all’1%

set errmod [new ErrorModel]

$errmod unit pkt # unità di errore desiderata

$errmod set rate_ 0.01 # variabile casuale per la generazione degli errori

$errmod ranvar [new RandomVariable/Uniform]

$errmod drop-target [new Agent/Null]

$ns link-lossmodel $errmod $nodo1 $nodo2

13

Application 1

Agent 1

Nodo

2

Nodo

1

Application 2

Agent 2

14

15

set udp1 [new Agent / UDP] # crea l’agente udp1

set null2 [new Agent / Null] # crea l’agente null2

$ns attach-agent $nodo1 $udp1 # attacca udp1 al nodo1

$ns attach-agent $nodo2 $null2 # attacca null2 al nodo2

$ns connect $udp1 $null2 # connette i due agent

$udp1 set fid_ 1 # associa i pacchetti al flusso 1, tramite la variabile “flow indicator”

16

udp1 Nodo

2

Nodo

1

1 1 1

null2

set ExpOnOff [new Application/Traffic/Exponential]

set cbr1 [new Application/Traffic/CBR]

17

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize_ 500

$cbr1 set rate_ 128Kb

$cbr1 attach-agent $udp1

$ns at 2.0 “$cbr1 start”

$ns at 5.5 “$cbr1 stop”

18

agent 1

(Agent/UDP) Nodo

2

Nodo

1

1 1 1 agent 2

(Agent/Null)

set udp3 [new Agent / UDP] # crea l’agent3 UDP

set null4 [new Agent / Null] # crea l’agent4 Null

$ns attach-agent $nodo3 $udp3

$ns attach-agent $nodo4 $null4

$ns connect $udp3 $null4

$udp3 set fid_ 2

# Crea l’applicazione ftp3 e l’attacca all’agent3

set ftp3 [ new Application/FTP ]

$ftp3 attach-agent $udp3

$ns at 0.0 “$ftp3 start” # ftp3 inizia a 0.0 s

$ns at 4.5 “$ftp3 stop” # ftp3 termina a 4.5 s

19

Nodo

3

Nodo

4 udp3

2 2 2

null4

set trace_file [open traccia.tr w]

$ns trace-queue $nodo1 $nodo2 $trace_file

....

close $trace_file

set trace_file [open traccia.tr r]

20

21

set ns [new Simulator] # Crea un oggetto simulator

set nf [open out.nam w] # Apre in scrittura il file nam

$ns namtrace-all $nf

set trace_file [open traccia.tr w]

proc finish {} { # Definisce una procedura “finish”

global ns nf trace_file

$ns flush-trace

close $nf $trace_file

exec nam out.nam & # Esegue nam

exit 0

}

for {set i 1} {$i < 5} {incr i} # Crea 4 nodi

{set nodo$i [$ns node]}

$ns simplex-link $nodo1 $nodo2 10Mb 1ms DropTail # Crea i link

$ns duplex-link $nodo3 $nodo4 100Mb 50ms DropTail

$ns queue-limit $nodo1 $nodo2 10

$ns trace_queue $nodo1 $nodo2 $trace_file

22

set errmod [new ErrorModel] # Crea l’ErrorModel

$errmod unit pkt

$errmod set rate_ 0.01

$errmod ranvar [new RandomVariable/Uniform]

$errmod drop-target [new Agent/Null]

$ns link-lossmodel $errmod $nodo1 $nodo2

set udp1 [new Agent/UDP] # Crea l’agent udp1 e lo attacca a nodo1

$ns attach-agent $nodo1 $udp1

set null2 [new Agent/Null] # Crea l’agent null2 e lo attacca a nodo2

$ns attach-agent $nodo2 $null2

$ns connect $udp1 $null2 # Collega l’agent udp1 con null2

$udp1 set fid_1

set cbr1 [new Application/Traffic/CBR] # Crea una sorgente CBR

$cbr1 set packetSize_ 500

$cbr1 set rate_ 128Kb

$cbr1 attach-agent $udp0

23

set udp3 [new Agent / UDP] # Crea l’agent udp3 e lo attacca a nodo3

$ns attach-agent $nodo3 $udp3

set null4 [new Agent / Null] # Crea l’agent null4 e lo attacca a nodo4

$ns attach-agent $nodo4 $null4

$ns connect $udp3 $null4 # Collega l’agent udp3 con null4

$udp3 set fid_ 2

set ftp3 [ new Application/FTP ] # Crea una sorgente FTP

$ftp3 attach-agent $udp3

$ns at 0.0 “$cbr1 start” # Eventi della simulazione

$ns at 1.0 “$ftp3 start”

$ns at 3.5 “$cbr1 stop”

$ns at 4.5 “$ftp3 stop”

$ns at 5.0 “finish” # Richiama la procedura finish

$ns run # Parte la simulazione

24

set ns [ new Simulator ]

set f0 [open out0.tr w] # Apre i files di uscita set f1 [open out1.tr w]

set f2 [open out2.tr w]

for {set k 0} {$k < 5} {incr k} {

set n($k) [$ns node] # Crea i nodi di rete }

# Creazione dei link tra i nodi di rete $ns duplex-link $n(0) $n(3) 1Mb 100ms DropTail

$ns duplex-link $n(1) $n(3) 1Mb 100ms DropTail

$ns duplex-link $n(2) $n(3) 1Mb 100ms DropTail

$ns duplex-link $n(3) $n(4) 1Mb 100ms DropTail

25

n0 n3

n1 n2

n4

proc finish {} { # Procedura di chiusura

global f0 f1 f2

close $f0 # Chiude i file di uscita

close $f1

close $f2

# Invoca xgraph per visualizzare i risultati

exec xgraph out0.tr out1.tr out2.tr –geometry 800x400 & exit 0

}

26

proc attach-expoo-traffic {node sink size burst idle rate} {

set ns [Simulator instance]

# crea un agente UDP e lo attacca al nodo

set source [new Agent/UDP]

$ns attach-agent $node $source

# crea un agente Expoo traffic e i suoi parametri

set traffic [new Application/Traffic/Exponential]

$traffic set packetSize_ $size

$traffic set burst_time_ $burst

$traffic set idle_time_ $idle

$traffic set rate_ $rate

$traffic attach-agent $source

$ns connect $source $sink

return $traffic

}

27

• ‘attach-expoo-traffic’ ‘node, sink, size, burst,idle rate’.

• $traffic ‘size, burst, idle, rate’

• $traffic $source $source $sink

28

source sink

# Creo tre sink agenti LossMonitor, attaccati a n4

set sink0 [new Agent/LossMonitor]

set sink1 [new Agent/LossMonitor]

set sink2 [new Agent/LossMonitor]

$ns attach-agent $n4 $sink0

$ns attach-agent $n4 $sink1

$ns attach-agent $n4 $sink2

29

n0 n3

n1 n2

n4

sink0

sink1

sink2

# Creo tre sorgenti di traffico Expoo

set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]

set source1 [attach-expoo-traffic $n1 $sink1 200

2s 1s 200k]

set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]

30

n0 n3

n1 n2

n4

source1

source2

source0

# Procedura che periodicamente registra nei file la banda occupata dalle sorgenti di traffico

proc record {} {

global sink0 sink1 sink2 f0 f1 f2

set ns [Simulator instance]

# dopo 0.5s verrà richiamata questa procedura

set time 0.5

# quanti bytes sono arrivati ad ogni sink

set bw0 [$sink0 set bytes_]

set bw1 [$sink1 set bytes_]

set bw2 [$sink2 set bytes_]

# registra il tempo attuale

set now [$ns now]

… … … … … … … … … … … …

31

… … … … … … … … … … … …

# Calcola la banda istantanea (in MBit/s) e la scrive nei file di output

put $f0 “$now [expr $bw0/($time*8/10^6)]”

put $f1 “$now [expr $bw1/($time*8/10^6)]”

put $f2 “$now [expr $bw2/($time*8/10^6)]”

# Resetta la variabile “bytes_” nei sink

$sink0 set bytes_ 0

$sink1 set bytes_ 0

$sink2 set bytes_ 0

# Ri-schedula la procedura di record

$ns at [expr $now+$time] “record”

}

32

# La registrazione inizia all’istante 0.0 s $ns at 0.0 "record"

# Le sorgenti iniziano a trasmettere a 10.0s $ns at 10.0 "$source0 start" $ns at 10.0 "$source1 start"

$ns at 10.0 "$source2 start"

# A 50s le sorgenti terminano la trasmissione $ns at 50.0 "$source0 stop" $ns at 50.0 "$source1 stop"

$ns at 50.0 "$source2 stop"

# A 60s la simulazione termina $ns at 60.0 "finish"

# Parte la simulazione $ns run

33

34

35

36

37

14-11-2010 N. 38

14-11-2010 N. 39

14-11-2010 N. 40

14-11-2010 N. 41

14-11-2010 N. 42

38

set opt(nn) 1 # numero dei nodi mobili

set num_wired_nodes 2 # numero dei nodi fissi # opt() indica una variabile definita solo all’interno di questo

script. val() è una variabile globale.

# coordinate x e y della topologia di rete

set opt(x) 670

set opt(y) 670

set opt(stop) 250 # istante temporale per interrompere la simulazione

set opt(tr-ns) infra-out.tr # file di traccia

set opt(tr-nam) infra-out.nam # file NAM

14-11-2010 N. 43

39

# Settaggi dell'IEEE 802.11

Mac/802_11 set dataRate_ 2.0e6

Mac/802_11 set RTSThreshold_ 3000

set tracefd [open $opt(tr-ns) w]

set namtrace [open $opt(tr-nam) w]

$ns_ trace-all $tracefd

$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)

set topo [new Topography] # crea la topologia

$topo load_flatgrid $opt(x) $opt(y)

create-god [expr $opt(nn) + 2] # crea l’oggetto GOD (General Operations Director)

14-11-2010 N. 44

40

set ns_ [new Simulator]# crea la classe SIMULATORE

# indirizzi gerarchici per i nodi di rete

$ns_ node-config -addressType hierarchical

AddrParams set domain_num_ 3 # numero di domini

lappend cluster_num 2 1 1 # numero di clusters in ciascun dominio

14-11-2010 N. 45

41

# numero di nodi in ciascun cluster di ogni dominio

AddrParams set cluster_num_ $cluster_num

lappend eilastlevel 1 1 2 1

AddrParams set nodes_num_ $eilastlevel

14-11-2010 N. 46

41

# crea 2 nodi fissi

set W(0) [$ns_ node 0.0.0] # il nodo W(0) con indirizzo 0.0.0

set W(1) [$ns_ node 0.1.0] # il nodo W(1) con indirizzo 0.1.0

14-11-2010 N. 47

42

# Configurazione del Foreign Agent e Home Agent

$ns_ node-config - mobileIP ON \ # Mobile IP

-adhocRouting DSDV \

-llType LL \

-macType Mac/802_11 \

-ifqType Queue/DropTail/PriQueue \

-ifqLen 50 \

-antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \

-phyType Phy/WirelessPhy \

-channelType Channel/WirelessChannel \

-topoInstance $topo \

-wiredRouting ON \

-agentTrace ON \

-routerTrace OFF \

-macTrace OFF 14-11-2010 N. 48

43

# Creazione dell'Home e Foreign Agent

set HA [$ns_ node 1.0.0]

set FA [$ns_ node 2.0.0]

$HA set X_ 1.00

$HA set Y_ 2.00 # Posizione dell’HA sul NAM

$HA set Z_ 0.00

$FA set X_ 650.00

$FA set Y_ 600.00 # Posizione dell’FA sul NAM

$FA set Z_ 0.00

14-11-2010 N. 49

44

# Crea i collegamenti tra i nodi fissi e la BaseStation

$ns_ duplex-link $W(0) $W(1) 5Mb 2ms DropTail

$ns_ duplex-link $W(1) $HA 5Mb 2ms DropTail

$ns_ duplex-link $W(1) $FA 5Mb 2ms DropTail

# Istruzioni per la visualizzazione sul NAM

$ns_ duplex-link-op $W(0) $W(1) orient down

$ns_ duplex-link-op $W(1) $HA orient left-down

$ns_ duplex-link-op $W(1) $FA orient right-down

14-11-2010 N. 50

45

# Crea un nodo mobile (nel dominio dell’HA)

# che si sposta tra l’HA e il FA

# Disattiva il routing tra nodi fissi

$ns_ node-config -wiredRouting OFF

set MH [$ns_ node 1.0.1] # crea il MH con indirizzo 1.0.1

set HAaddress [AddrParams addr2id [$HA node-addr]]

[$MH set regagent_] set home_agent_ $HAaddress

$MH set X_ 2.00

$MH set Y_ 2.00

$MH set Z_ 0.00 14-11-2010 N. 51

46

# Il MH inizia a muoversi verso il FA

$ns_ at 100.00 “$MH setdest 640.00 610.00 20.00”

# Il MH torna indietro verso l'HA

$ns_ at 200.00 “$MH setdest 2.00 2.00 20.00”

# Definisce la posizione iniziale nel NAM

$ns_ initial_node_pos $MH 20

14-11-2010 N. 52

47

# Creazione degli agenti TCP e Sink

set tcp1 [new Agent/TCP]

$tcp1 set class_ 2

set sink1 [new Agent/TCPSink]

$ns_ attach-agent $W(0) $tcp1

$ns_ attach-agent $MH $sink1

$ns_ connect $tcp1 $sink1

# Creazione dell’applicazione FTP

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

$ns_ at 100.0 “$ftp1 start” 14-11-2010 N. 53

48

# Alla fine della simulazione viene resettato il MH, l’HA e il FA

$ns_ at $opt(stop).0 “$MH reset”

$ns_ at $opt(stop).0 “$HA reset”

$ns_ at $opt(stop).0 “$FA reset”

$ns_ at $opt(stop).02 “puts \”NS EXITING...\”; $ns_ halt”

$ns_ at $opt(stop).01 “finish”

14-11-2010 N. 54

49

proc finish {} {

global ns_ tracefd namtrace

close $tracefd

close $namtrace

exec rm -f out-tcp.xgr

exec awk -f fil-tcp.awk infra-out.tr > out-tcp.xgr

exec xgraph out-tcp.xgr &

exec nam infra-out.nam &

}

puts “Inizia la simulazione”

$ns_ run

14-11-2010 N. 55

50

51