📄 simple_ns.html
字号:
<html><head><title>Simple Simulation Example</title></head><body background="parchment.gif" tppabs="http://nile.wpi.edu/NS/Icons/parchment.gif" text="#000000" link="#0000FF"vlink="#000080" alink="#FF0000"><br><center><h2>Simple Simulation Example</h2></center><br><br><dl><p>This section shows a simple NS simulation script and explains what each line does. Example 3 is an OTcl script that creates the simple network configuration and runs the simulation scenario in Figure 4. To run this simulation, download "<a href="ns-simple.tcl" tppabs="http://nile.wpi.edu/NS/Example/ns-simple.tcl">ns-simple.tcl</a>" and type "<font color=GREEN>ns ns-simple.tcl</font>" at your shell prompt.</p><a name="fig4"></a><p align="center"><img src="fig4.gif" tppabs="http://nile.wpi.edu/NS/Figure/fig4.gif" border=1><br><br><b>Figure 4.</b> A Simple Network Topology and Simulation Scenario</p><p>This network consists of 4 nodes (n0, n1, n2, n3) as shown in abovefigure. The duplex links between n0 and n2, and n1 and n2 have 2 Mbpsof bandwidth and 10 ms of delay. The duplex link between n2 and n3 has1.7 Mbps of bandwidth and 20 ms of delay. Each node uses a DropTailqueue, of which the maximum size is 10. A "tcp" agent is attached ton0, and a connection is established to a tcp "sink" agent attached ton3. As default, the maximum size of a packet that a "tcp" agent cangenerate is 1KByte. A tcp "sink" agent generates and sends ACK packetsto the sender (tcp agent) and frees the received packets. A "udp"agent that is attached to n1 is connected to a "null" agent attachedto n3. A "null" agent just frees the packets received. A "ftp" and a"cbr" traffic generator are attached to "tcp" and "udp" agentsrespectively, and the "cbr" is configured to generate 1 KByte packetsat the rate of 1 Mbps. The "cbr" is set to start at 0.1 sec and stopat 4.5 sec, and "ftp" is set to start at 1.0 sec and stop at 4.0sec.</p><p align="center"><a href="ns-simple.tcl" tppabs="http://nile.wpi.edu/NS/Example/ns-simple.tcl"><img src="ex3.gif" tppabs="http://nile.wpi.edu/NS/Figure/ex3.gif" border=1 alt="ns-simple.tcl"></a><br><br><b>Example 3.</b> A Simple NS Simulation Script</p></dl><p>The following is the explanation of the script above. In general,an NS script starts with making a Simulator object instance.</p><ul><li><font color=GREEN>set <i>ns</i> [new Simulator]</font>: generatesan NS simulator object instance, and assigns it to variable <i>ns</i>(italics is used for variables and values in this section). What thisline does is the following:<br> </li> <ul> <li>Initialize the packet format (ignore this for now)</li> <li>Create a scheduler (default is calendar scheduler)</li> <li>Select the default address format (ignore this for now)<br> </li> </ul>The "Simulator" object has member functions that do the following:<br> <ul> <li>Create compound objects such as nodes and links (described later)</li> <li>Connect network component objects created (ex. attach-agent)</li> <li>Set network component parameters (mostly for compound objects)</li> <li>Create connections between agents (ex. make connection between a "tcp" and "sink")</li> <li>Specify NAM display options</li> <li>Etc.<br> </li> </ul>Most of member functions are for simulation setup (referred to asplumbing functions in the Overview section) and scheduling, howeversome of them are for the NAM display. The "Simulator" object memberfunction implementations are located in the "<fontcolor=RED>ns-2/tcl/lib/ns-lib.tcl</font>" file.<br> <li><font color=GREEN><i>$ns</i> color <i>fid color</i></font>: is toset color of the packets for a flow specified by the flow id(fid). This member function of "Simulator" object is for the NAMdisplay, and has no effect on the actual simulation. <br> </li><li><font color=GREEN><i>$ns</i> namtrace-all<i>file-descriptor</i></font>: This member function tells thesimulator to record simulation traces in NAM input format. It alsogives the file name that the trace will be written to later by thecommand <font color=GREEN><i>$ns</i> flush-trace</font>. Similarly,the member function <font color=GREEN>trace-all</font> is forrecording the simulation trace in a general format.<br> </li><li><font color=GREEN>proc <i>finish</i> {}</font>: is called afterthis simulation is over by the command <font color=GREEN><i>$ns</i> at5.0 "<i>finish</i>"</font>. In this function, post-simulationprocesses are specified.<br> </li><li><font color=GREEN>set <i>n0</i> [<i>$ns</i> node]</font>: Themember function <font color=GREEN>node</font> creates a node. A nodein NS is compound object made of address and port classifiers(described in a later section). Users can create a node by separatelycreating an address and a port classifier objects and connecting themtogether. However, this member function of Simulator object makes thejob easier. To see how a node is created, look at the files: "<fontcolor=RED>ns-2/tcl/libs/ns-lib.tcl</font>" and "<fontcolor=RED>ns-2/tcl/libs/ns-node.tcl</font>".<br> </li><li><font color=GREEN><i>$ns</i> duplex-link <i>node1 node2 bandwidthdelay queue-type</i></font>: creates two simplex links of specifiedbandwidth and delay, and connects the two specified nodes. In NS, theoutput queue of a node is implemented as a part of a link, thereforeusers should specify the queue-type when creating links. In the abovesimulation script, DropTail queue is used. If the reader wants to usea RED queue, simply replace the word DropTail with RED. The NSimplementation of a link is shown in a later section. Like a node, alink is a compound object, and users can create its sub-objects andconnect them and the nodes. Link source codes can be found in "<fontcolor=RED>ns-2/tcl/libs/ns-lib.tcl</font>" and "<fontcolor=RED>ns-2/tcl/libs/ns-link.tcl</font>" files. One thing to noteis that you can insert error modules in a link component to simulate alossy link (actually users can make and insert any networkobjects). Refer to the NS documentation to find out how to dothis.<br> </li><li><font color=GREEN><i>$ns</i> queue-limit <i>node1 node2number</i></font>: This line sets the queue limit of the two simplexlinks that connect node1 and node2 to the number specified. At thispoint, the authors do not know how many of these kinds of memberfunctions of Simulator objects are available and what they are. Pleasetake a look at "<font color=RED>ns-2/tcl/libs/ns-lib.tcl</font>" and"<font color=RED>ns-2/tcl/libs/ns-link.tcl</font>", or NSdocumentation for more information.<br> </li><li><font color=GREEN><i>$ns</i> duplex-link-op <i>node1 node2...</i></font>: The next couple of lines are used for the NAMdisplay. To see the effects of these lines, users can comment theselines out and try the simulation.</li></ul><p>Now that the basic network setup is done, the next thing to do isto setup traffic agents such as TCP and UDP, traffic sources such asFTP and CBR, and attach them to nodes and agents respectively.</p><ul><li><font color=GREEN>set <i>tcp</i> [new <i>Agent/TCP</i>]</font>:This line shows how to create a TCP agent. But in general, users cancreate any agent or traffic sources in this way. Agents and trafficsources are in fact basic objects (not compound objects), mostlyimplemented in C++ and linked to OTcl. Therefore, there are nospecific Simulator object member functions that create these objectinstances. To create agents or traffic sources, a user should know theclass names these objects (Agent/TCP, Agnet/TCPSink, Application/FTPand so on). This information can be found in the NS documentation orpartly in this documentation. But one shortcut is to look at the"<font color=RED>ns-2/tcl/libs/ns-default.tcl</font>" file. This filecontains the default configurable parameter value settings foravailable network objects. Therefore, it works as a good indicator ofwhat kind of network objects are available in NS and what are theconfigurable parameters.<br> </li><li><font color=GREEN><i>$ns</i> attach-agent <i>nodeagent</i></font>: The <font color=GREEN>attach-agent</font> memberfunction attaches an agent object created to a node object. Actually,what this function does is call the <font color=GREEN>attach</font>member function of specified node, which attaches the given agent toitself. Therefore, a user can do the same thing by, for example, <fontcolor=GREEN>$n0 attach $tcp</font>. Similarly, each agent object has amember function <font color=GREEN>attach-agent</font> that attaches atraffic source object to itself.<br> </li><li><font color=GREEN><i>$ns</i> connect <i>agent1 agent2</i></font>:After two agents that will communicate with each other are created,the next thing is to establish a logical network connection betweenthem. This line establishes a network connection by setting thedestination address to each others' network and port addresspair.</li></ul><p>Assuming that all the network configuration is done, the next thingto do is write a simulation scenario (i.e. simulation scheduling). TheSimulator object has many scheduling member functions. However, theone that is mostly used is the following:</p><ul><li><font color=GREEN><i>$ns</i> at <i>time "string"</i></font>: Thismember function of a Simulator object makes the scheduler (scheduler_is the variable that points the scheduler object created by [newScheduler] command at the beginning of the script) to schedule theexecution of the specified string at given simulation time. Forexample, <font color=GREEN><i>$ns</i> at <i>0.1 "$cbrstart"</i></font> will make the scheduler call a <fontcolor=GREEN>start</font> member function of the CBR traffic sourceobject, which starts the CBR to transmit data. In NS, usually atraffic source does not transmit actual data, but it notifies theunderlying agent that it has some amount of data to transmit, and theagent, just knowing how much of the data to transfer, creates packetsand sends them.</li></ul><p>After all network configuration, scheduling and post-simulationprocedure specifications are done, the only thing left is to run thesimulation. This is done by <font color=GREEN><i>$ns</i>run</font>.<p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -