This is an old revision of the document!


Laboratorul 1

Jucăm

Wireless Explorer doar câteva nivele LOL.

Citim tutoriale și rulăm exemple. Textul de mai jos este bazat pe ghidul NS by example de J. Chung și M. Claypool.

Introducere în ns2

NS-2 este un simulator de rețea bazat pe evenimente dezvoltat la UC Berkely scris in C++ si OTcl. NS-2 este în primul rând util pentru simularea rețelelor LAN și WAN. Cu toate ca NS este destul de ușor de utilizat pentru avansați, este destul de dificil la prima utilizare, deoarece există puține manuale user-friendly. Chiar dacă există o mulțime de documente scrise de către dezvoltatori, care conțin o explicație aprofundată a simulatorului, acestea sunt scrise pentru utilizatorii NS-2 avansați. Această secțiune oferă o idee de bază a modului în care funcționează simulatorul, cum se pregătește o simulare, unde se găsesc componentele interne ale simulatorului, cum se configurează componentele de rețea, etc. În principal se prezintă exemple simple și explicații bazate pe experimente simple, familiare unui student care a luat un curs introductiv de rețele.

Limbajul OTcl

NS-2 este în esență un interpretor de OTcl cu biblioteci de obiecte specializate în simularea rețelelor. Este necesar să se cunoască sumar programarea în OTcl pentru a utiliza NS-2. Această secțiune prezintă un exemplu OTcl care ilustrează ideea de bază a programării în OTcl. Acest exemplu este importat din manualul oficial NS-2. Această secțiune și secțiunile următoare presupune că cititorul ainstalat NS-2, și este familiarizat cu C++.

Primul exemplu este un script general OTcl, care arată modul de a declara o procedură și a o apela, modul de a atribui valori variabilelor, și modul de a implementa o buclă. OTcl este de fapt extensia orientată obiect a TCL(pronunțat “tickle”), relația dintre Tcl și Otcl este la fel ca și cea dintre C și C++, dar în acest laborator rareori sunt folosite obiectele în OTcl. Pentru a rula acest script rulați “ns ex-tcl.tcl” la promptul shell - comanda “ns” execută NS-2 (interpretorul OTcl). Veți obține aceleași rezultate dacă tastați “tclsh ex-tcl.tcl”, în cazul în care tcl8.0 este instalat pe mașina dumneavoastră.

ex-tcl.tcl
# Writing a procedure called "test"
proc test {} {
    set a 43
    set b 27
    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]"
	}
    }
}
 
# Calling the "test" procedure created above
test

In Tcl, the keyword proc is used to define a procedure, followed by an procedure name and arguments in curly brackets. The keyword set is used to assign a value to a variable. [expr …] is to make the interpreter calculate the value of expression within the bracket after the keyword. One thing to note is that to get the value assigned to a variable, $ is used with the variable name. The keyword puts prints out the following string within double quotation marks. The following shows the result of Example 1.

În Tcl, cuvântul cheie ''proc''  este folosit pentru a defini o procedură, urmat de un nume de procedură și argumentele în acolade. Cuvântul cheie ''set'' este folosit pentru a atribui o valoare unei variabile. ''[Expr ...]'' este pentru a calcula valoarea expresiei între paranteze drepte, după cuvântul cheie. Un lucru de remarcat este faptul că pentru a obține valoarea atribuită unei variabile, ''$ este utilizat cu numele variabilei. Cuvântul cheie puts imprimă șirul de caractere la consolă. Rulănd exemplul de mai sus, se obține: <code> 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 </code> == Simple Simulation Example == Această secțiune prezintă un script simplu NS-2 si explica ce face fiecare linie. Exemplul este un script OTcl care creează o configurație de rețea simplă și rulează scenariul de simulare din figura de mai jos. Pentru a rula această simulare, descărcați “ns-simple.tcl” și rulați “ns ns-simple.tcl” în shell. Această rețea este formată din 4 noduri (n0, n1, n2, n3), așa cum se arată în figura de mai sus. Link-urile duplex între n0 și n2, și n1 și n2 au capacitatea 2Mbps[Megabit/s] și 10ms de întârziere. Link-ul duplex între n2-n3 are capacitatea de 1.7Mbps și 20ms de întârziere. Fiecare nod folosește la ieșire o coadă DropTail, a cărei mărime este de 10 pachete. Un agent “TCP” este atașat la n0, și este stabilită o conexiune la un sink TCP atașat la n3. În mod implicit, dimensiunea maximă a unui pachet pe care un agent de TCP poate genera este 1KByte. Un sink TCP generează și trimite pachete ACK către expeditor (agent TCP) și livrează la aplicație pachetele primite. Un agent UDP, care este atașat la n1 este conectat la un agent sink (LossMonitor) atașat la n3. Un agent LossMonitor doar contorizează pachetele primite. Aplicații de tio FTP și generator de trafic CBR sunt atașate la agenții TCP șiUDP respectivi, iar CBR este configurat pentru a genera pachete de 1000 bytes la rata de 1Mbps. Aplicația CBR este setată să pornească de la 0.1s, și să se oprească la 14,5 sec, iar FTP pornește de la 1.0s și se oprește la 14.0s. <file tcl ns-simple.tcl> #Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #Set Queue Size of link (n2-n3) to 5 $ns queue-limit $n2 $n3 5 #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right #Monitor the queue for link n2-n3 (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5 #Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sinkt [new Agent/TCPSink] $ns attach-agent $n3 $sinkt $ns connect $tcp $sinkt $tcp set fid_ 1 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set usink [new Agent/LossMonitor] $ns attach-agent $n3 $usink $ns connect $udp $usink $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false #Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf #Define a 'finish' procedure proc finish {} { global ns nf fout $ns flush-trace #Close the NAM trace file close $nf #Execute NAM on the trace file exec nam out.nam & exit 0 } #Schedule events for the CBR and FTP agents $ns at 0.1 “$cbr start” $ns at 1.0 “$ftp start” $ns at 14.0 “$ftp stop” $ns at 14.5 “$cbr stop” #Call the finish procedure after 15 seconds of simulation time $ns at 15.0 “finish” #Print CBR packet size and interval puts “CBR packet size = [$cbr set packet_size_]” puts “CBR interval = [$cbr set interval_]” #Run the simulation $ns run </file> În continuare, descriem script-ul de mai sus. În general, un script NS-2 începe cu o instanță a obiectului simulator: * set ns [new Simulator] generează un obiect de tip simulator și îl atribuie variabilei ns (caracterele cursive sunt folosite pentru variabile și valori). Această linie: * inițializează formatul pachetelor (ignorați acest lucru deocamdată) * crează un planificator * selectează formatul adreselor (ignorați acest lucru deocamdată) * Obiectul “simulator” are funcții de membru care: * crează obiecte compuse, cum ar fi nodurile și legăturile (descrise mai târziu) * conectează componentele de rețea create (de ex., attach-agent) * setează parametrii componentelor de rețea (mai ales pentru obiectele compuse) * crează conexiuni între agenți (de ex., Face legătura între un TCP și sink) * specifică opțiunile NAM (network animator) Cele mai multe dintre funcțiile sunt pentru configurarea simularii și pentru planificare, iar unele dintre ele sunt pentru animatorulul NAM. Alte setăriu ale simulatorului pot fi consultate în fișierul ns-2 / TCL / lib / ns-lib.tcl. * $ns color fid color'' is to set color of the packets for a flow specified by the flow id (fid). This member function of "Simulator" object is for the NAM display, and has no effect on the actual simulation. 
 
  * ''$ns namtrace-all file-descriptor This member function tells the simulator to record simulation traces in NAM input format. It also gives the file name that the trace will be written to later by the command $ns flush-trace. Similarly, the member function trace-all is for recording the simulation trace in a general format. * proc finish {} is called after this simulation is over by the command $ns at 5.0 “finish”. In this function, post-simulation processes are specified. * set n0 [$ns node]'' The member function node creates a node. A node in NS is compound object made of address and port classifiers (described in a later section). Users can create a node by separately creating an address and a port classifier objects and connecting them together. However, this member function of Simulator object makes the job easier. To see how a node is created, look at the files: "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-node.tcl".
 
  * ''$ns duplex-link node1 node2 bandwidth delay queue-type creates two simplex links of specified bandwidth and delay, and connects the two specified nodes. In NS, the output queue of a node is implemented as a part of a link, therefore users should specify the queue-type when creating links. In the above simulation script, DropTail queue is used. If the reader wants to use a RED queue, simply replace the word DropTail with RED. The NS implementation of a link is shown in a later section. Like a node, a link is a compound object, and users can create its sub-objects and connect them and the nodes. Link source codes can be found in “ns-2/tcl/libs/ns-lib.tcl” and “ns-2/tcl/libs/ns-link.tcl” files. One thing to note is that you can insert error modules in a link component to simulate a lossy link (actually users can make and insert any network objects). Refer to the NS documentation to find out how to do this. * $ns queue-limit node1 node2 number'' This line sets the queue limit of the two simplex links that connect node1 and node2 to the number specified. At this point, the authors do not know how many of these kinds of member functions of Simulator objects are available and what they are. Please take a look at "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl", or NS documentation for more information.
 
  * ''$ns duplex-link-op node1 node2 … The next couple of lines are used for the NAM display. To see the effects of these lines, users can comment these lines out and try the simulation. Now that the basic network setup is done, the next thing to do is to setup traffic agents such as TCP and UDP, traffic sources such as FTP and CBR, and attach them to nodes and agents respectively. * set tcp [new Agent/TCP] This line shows how to create a TCP agent. But in general, users can create any agent or traffic sources in this way. Agents and traffic sources are in fact basic objects (not compound objects), mostly implemented in C++ and linked to OTcl. Therefore, there are no specific Simulator object member functions that create these object instances. To create agents or traffic sources, a user should know the class names these objects (Agent/TCP, Agnet/TCPSink, Application/FTP and so on). This information can be found in the NS documentation or partly in this documentation. But one shortcut is to look at the “ns-2/tcl/libs/ns-default.tcl” file. This file contains the default configurable parameter value settings for available network objects. Therefore, it works as a good indicator of what kind of network objects are available in NS and what are the configurable parameters. * $ns attach-agent node agent'' The attach-agent member function attaches an agent object created to a node object. Actually, what this function does is call the attach member function of specified node, which attaches the given agent to itself. Therefore, a user can do the same thing by, for example, $n0 attach $tcp. Similarly, each agent object has a member function attach-agent that attaches a traffic source object to itself.
 
  * ''$ns connect agent1 agent2 After two agents that will communicate with each other are created, the next thing is to establish a logical network connection between them. This line establishes a network connection by setting the destination address to each others' network and port address pair. Assuming that all the network configuration is done, the next thing to do is write a simulation scenario (i.e. simulation scheduling). The Simulator object has many scheduling member functions. However, the one that is mostly used is the following: * $ns at time "string"'' This member function of a Simulator object makes the scheduler (scheduler_ is the variable that points the scheduler object created by [new Scheduler] command at the beginning of the script) to schedule the execution of the specified string at given simulation time. For example, $ns at 0.1 “$cbr start" will make the scheduler call a start member function of the CBR traffic source object, which starts the CBR to transmit data. In NS, usually a traffic source does not transmit actual data, but it notifies the underlying agent that it has some amount of data to transmit, and the agent, just knowing how much of the data to transfer, creates packets and sends them.
After all network configuration, scheduling and post-simulation procedure specifications are done, the only thing left is to run the simulation. This is done by ''$ns run. * La rularea cu ns ./ns-simple.tcl , se execută scriptul care generează “filmul simulării” out.name, și se lansează animatorul nam. Rulați slide-ul în animator pentru a accelera filmul, observați transferul pachetelor și comportarea cozii din nodul 2 == Trasarea unui grafic == ns2 permite implementarea cu ușurință a procedurilor specializate de generare de loguri. Aceste proceduri sunt apelate periodic în timpul simulării și permit adresarea tuturor datelor specifice nodurilor, fluxurilor, cozilor, și ale celorlalte entități din rețea. În partea de final a scriptului de mai sus, inserați codul următor. Apelată periodic, această procedură contorizează numărul de octe ți primiți de destinația UDP, și numărul de octeți confirmați la sursa TCP. În acest mod, se poate calcula debitul obținut de cele două fluxuri pe intervale fixe de timp. Cele două valori sunt stocate periodic într-un fișier text out.tr. <file tcl> #Open a trace file set fout [open out.tr w] #save running byte counters set tbytes 0 set ubytes 0 proc record {} { global tcp usink fout ubytes tbytes set ns [Simulator instance] #Set the time after which the procedure should be called again set time 0.25 #How many bytes have been received/acked? set tbytes1 [expr [$tcp set ack_]*[$tcp set packetSize_]] set ubytes1 [expr [$usink set bytes_]] set now [$ns now] #Calculate the bandwidth (in MBit/s) and write it to the log puts $fout “$now [expr ($tbytes1 - $tbytes)/$time*8/1000000] \ [expr ($ubytes1 - $ubytes)/$time*8/1000000]” #Reset the bytes_ values on the traffic sinks set tbytes $tbytes1 set ubytes $ubytes1 #Re-schedule the procedure $ns at [expr $now+$time] “record” } </file> Procedura finish trebuie actualizată pentru a include închiderea fișierului de trace, și pentru a plota conținutul său: <file tcl> #Close the trace file close $fout #Call gnuplot to display the results exec echo “plot 'out.tr' using 1:2 t 'TCP' w l, using 1:3 t 'UDP' w l” | gnuplot -persist </file> În plus, înainte de a demara simularea, trebuie să armăm procedura record cu $ns at 0.0 “record”. După execuția scriptului, se vor lansa automat atât fereastra animatorului, cât și o fereastră gnuplot care afișează conținutul fișierului trace out.tr

  1. examinați cu editorul de text conținutul fișierelor out.nam și out.tr
  2. Ce reprezintă axele x, y? Explicați comportarea graficelor.
  3. Măriți coada de la link-ul bottleneck la 100. Cum explicați noua comportare?
  4. Coada aruncă pachete TCP în mod disproporționat. De ce? Folosiți o coadă SFQ pentru a remedia situația.
isrm/laboratoare/01.1475082966.txt.gz · Last modified: 2016/09/28 20:16 by dragos.niculescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0