`

NS by Example 笔记(2)Simple Simulation Example

阅读更多

介绍了如何建立一个简单的网络模拟 

 



 

 

网络中有4个节点(n0, n1, n2, n3) , n0 与 n2, n1 与 n2 相连(双工)并且有 2 Mbps 带宽和 10 ms 的延迟。 n2 与 n3 相连, 带宽 1.7 Mbps 并且延迟 20 ms。 每个节点使用 DropTail 队列, 且队列的最大的大小为10。 一个"tcp" 代理附加到 n0, 并且连接到一个 tcp "sink" 代理 附加在n3。 默认情况下, "tcp" 代理生成的packet 包的大小是1KByte。 "sink" 代理生成并发送ACK包给发送者(tcp agent)然后释放收到的包。 n1 附加上一个 "udp" 代理, 连接到一个 "null" 代理附属在 n3. "null" 代理释放收到的所有包.  "ftp" 和 "cbr" 2个流量生成器分别附到 "tcp" and "udp" 代理上, 且"cbr" 以1 Mbps的速率生成1 KByte包. 设置"cbr" 开始在0.1 sec 然后停止于4.5 sec, 而 "ftp" 在1.0 sec 开始然后在4.0 sec停止。

 

#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
        #Close the NAM trace file
        close $nf
        #Execute NAM on the trace file
        exec nam out.nam &
        exit 0
}

#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 10
$ns queue-limit $n2 $n3 10

#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 sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$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 null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$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


#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"

#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time
$ns at 5.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

 

 

 

在ns脚本中,一般首先建立模拟对象的实例

 

set ns [new Simulator]: 生成模拟对象的实例, 赋值到变量ns中 。代码执行一下步骤:

  • 初始化包的格式 
  • 建立调度程序scheduler (默认是calendar scheduler)
  • 选择默认的地址格式 

"Simulator" 对象中的成员函数的作用:

  • 建立compound(复合)对象, 比如nodes 和 links
  • 建立连到网络的组件对象(component objects)(例如 attach-agent)
  • 设定网络组件的参数
  • 建立代理之间的连接 (例如 "tcp" 和 "sink"的连接)
  • 指定NAM的显示选项
  • 其他
     

大多数函数用来建立模拟和调度,有些用于NAM的显示。 "Simulator" 类中函数的实现可以参考 "ns-2/tcl/lib/ns-lib.tcl" 文件.

 

$ns color fid color: 设定传输的包的颜色,颜色由 flow id (fid) 定义. 这个 "Simulator" 对象的函数用于NAM的显示, 并且对实际的模拟没有影响. 

 

$ns namtrace-all file-descriptor: 函数告知模拟器记录模拟的轨迹到NAM输入格式,之后用$ns flush-trace命令写入到名为file-descriptor文件。 相似的, 如果用 trace-all 就以普通格式保存模拟的轨迹。

 

proc finish {}:$ns at 5.0 "finish"(结束模拟)运行后调用。这个函数指定了模拟完成后的操作。 

 

set n0 [$ns node]: 成员函数 node 建立一个节点. 在NS中节点node是由地址和端口分级器组成的compound对象。建立node的具体过程见 "ns-2/tcl/libs/ns-lib.tcl" 和 "ns-2/tcl/libs/ns-node.tcl"文件。

  

$ns duplex-link node1 node2 bandwidth delay queue-type: 建立连接两个节点并指明了带宽和延迟的链接。NS中, 节点的输出队列是链接的一部分, 所以需要在建立链接时指明队列类型queue-type。代码中用到DropTail queue这个队列类型。 如果想用RED queue, 只需用RED替换DropTail。 link也是一种compound对象,并且可以建立他的子对象并把他们连接到节点。Link 的 source codes位于"ns-2/tcl/libs/ns-lib.tcl" 和 "ns-2/tcl/libs/ns-link.tcl" 文件. 特别注意,我们可以在link组件中插入error modules去模拟lossy link (事实上可以建立并插入任何网络对象), 详见NS的文档。

 

$ns queue-limit node1 node2 number: 指定相连node1和node2的连接的队列限度为number。具体参考 "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl", 或者 NS 文档。
 

$ns duplex-link-op node1 node2 ...: 之后的几行代码都是用于 NAM 的显示. 注释掉这几行然后再模拟,就能发现这几行的效果。

 

 

 

到这里,网络的基本设置已经完成, 下面我们需要去设定流量代理比如 TCP 和 UDP, 流量源traffic source 比如 FTP 和 CBR, 并附加到相应的节点上。

 

set tcp [new Agent/TCP]: 建立TCP代理。也可以用这个方法建立任意的代理或流量源。 Agents 和 traffic sources 是基本对象而不是 compound 对象, 大多数用C++实现并连接到OTcl. 所以没有明确的模拟对象的成员函数去建立这些对象的实例。 建立他们时需要知道类名(比如 Agent/TCP, Agnet/TCPSink, Application/FTP 等等), 可以查阅 NS 文档, 但更快的方式是查看"ns-2/tcl/libs/ns-default.tcl"文件。 这个文件包含了可用网络对象的默认并可配置的参数值, 因此他很好的说明了哪类网络对象在NS中可用和他们对应的参数。 

 

$ns attach-agent node agent: attach-agent 函数把建立好的代理对象附加到节点对象上。其实这个函数调用了节点中的attach函数, attach函数把代理附给节点本身。 因此,$n0 attach $tcp得到同样的效果。 同样的,每个代理也有 attach-agent 函数去附加 traffic source 流量源对象给他本身。
 

$ns connect agent1 agent2: 在2个代理之间建立逻辑上的连接。通过设置目的地地址和端口地址到相互的网络去建立连接。

 

 

 

网络配置完成之后,我们就需要定义模拟的场景(模拟行程 simulation scheduling)。 模拟对象有许多安排函数,下面是一个最常用到的:

 

$ns at time "string": 这个函数让调度程序去安排指定的命令"string"的执行时间,代码起始处的[new Scheduler]命令建立了scheduler对象并赋在变量 scheduler_ 中。 例如, $ns at 0.1 "$cbr start" 让调度程序去调用CBR traffic source 对象的 start 函数, 然后 CBR 开始传输数据。 NS中,  traffic source 通常并非传输实际的数据, 而是通知底层的代理他有一定数量数据要传输, 并且代理只知道有多少数据要发送,然后建立包并发送他们。

 

在配置完所有的网络,行程安排和模拟后执行的程序之后,加入代码 $ns run 在脚本最后去运行模拟。

  • 大小: 4.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics