Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63980
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2017-04-22 16:11
文章分类
文章存档

2017年(46)

我的朋友

分类: 系统运维

2017-05-16 14:58:04

ns是网络离散事件模拟器。所有分布在网络中的事件都被全序化,塞入一个事件队列里。并由管理该事件队列的event scheduler,维护一个全局的时间。event scheduler会根据每个当前事件,激发相应的动作action。这就牵涉到分布式系统建模的基本问题之一:action的原子性,像 recv,send,drop等可观测的外部活动一般设定为原子的,而很多不可见的内部动作则建模成某种latency,它们是非原子的。比如说信道消息 传输的延迟就是send和correponding recv之间的时间间隔,switcher的延迟也是recv和correponding send之间的时间间隔。大量send和recv动作之间是可以arbitrarily interleave。分布式系统建模的另外两个基本问题是:封闭的分布式系统状态的建模、分布系统实时性的建模。分布式系统中时间很重要,不仅因为网络 中有很多对实时性有要求的应用(e.g. time based schedule),更本质的是因为Agents间的可靠通信协议都是基于超时机制的,或多或少对系统中的synchronous有某种假设。 purely asynchronous system只是有理论上的研究价值。

下面的语句展示了,如何创建和侦听(跟踪)ns的离散事件模拟器。

set ns [new Simulator]

set tracefile [open out.tr w]
$ns trace-all $tracefile

set namfile [open out.nam w]
$ns namtrace-all $namfile

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

$ns at 125.0 "finish"
$ns run

# Simulator instproc now   # return scheduler's notion of current time
# Simulator instproc at args   # schedule execution of code at specified time
# Simulator instproc at-now args   # schedule execution of code at now
# Simulator instproc after n args   # schedule execution of code after n secs
# Simulator instproc run args   # start scheduler
# Simulator instproc halt   # stop (pause) scheduler


定义网络拓扑
在ns的模型里面,wired network有两部分组成:无状态的网络节点、有状态的traffic和Agent。网络拓扑就是相互关联着的网络节点。网络组件分为节点(Node) 和信道(Channel或Link)。其中信道都是点到点的,用channel with limited buffer模拟节点间的异步通信。而广播、多播是依靠Multicast Node来模拟的。

# create a node
set n0 [$ns node]

# create bidirectional link from n0 to n2
$ns duplex-link $n0 $n2 10Mb 10ms DropTail

# create directional link from n0 to n2
$ns simple-link $n0 $n2 10Mb 10ms DropTail

# strategies handling buffer overflow of channel/link:
# DropTail/RED/FQ/DRR/SFQ/CBQ

# set buffer size of link
$ns queue-limit $n0 $n2 20

Packet与网络状态

struct Packet {
Header header;
data; //optional: for app-level protocols, or define a new header as an alternative
}
struct Header {
cmn header;
ip header;
tcp header;
rtp header;
trace header;
}
struct cmn header {
uid_ : unique id
ptype_ : pkt type
size_ : simulated packet size
ts_ : time stamp
...
}

定义Agents和Traffic flow

set src [new Agent/TCP]
set snk [new Agent/TCPSink]
# set src [new Agent/UDP]
# set src [new Agent/Null]      #UDP traffic sink

# protocol layers
$ns attach-agent $n0 $src
$ns attach-agent $n1 $snk

# establish the connection
$ns connect $src $snk

# configure tcp packet header at the source of traffic
$src set fid_ 1
$src set packetSize_ 512

# define a Pareto on-off traffic source
set source [new Application/Traffic/Pareto]
$source set packetSize_ 500
$source set burst_time_ 200ms
$source set idle_time_ 400ms
$source set rate_ 100k
$source set shape_ 1.5

# or define a trace driven source instead
# set tracefile [new TraceFile]
# $tracefile filename XXXX
# set source [new Application/Traffic/Trace]
# $source attach-tracefile $tracefile

$source attach-agent $src

# schedule the traffic
$ns at 1.0 "$source start"
$ns at 124.0 "$source stop"

基于Nam的可视化

$ns color 1 Blue
# 设定标号为1的网络流量颜色
# $node color red
# 设定 node 颜色
# $node shape square (circle, square, and hexagon)
# 设定 node 形状(circle as default choice)
# $node label "Text“
# 设定 node 的标签
# $node label-color blue
# 设定 node 标签颜色
# $ns duplex-link-op $n1 $n2 color green
# 设定 Link 颜色
# $ns duplex-link-op $n1 $n2 label "Text"
# 设定 Link 的标签
# $ns duplex-link-op $n1 $n2 label-color blue
# 设定 Link 标签的颜色
# $ns duplex-link-op $n1 $n2 orient right-up
# 设定 Link 的方向

资料

一个简明扼要的slides: 
ns主页上面的资料最经典和权威
Class Hierarchy of NS2 http://www-sop.inria.fr/planete/software/ns-doc/ns-current/
Marc Greis' ns tutorial
NS by example
NS2's trace format
NS maillist
NS2中文手册
可以参考一下别人的blog
http://blog.csdn.net/mitkook/archive/2006/12/22/1452846.aspx

论坛 http://gcsn.csie.ntust.edu.tw/Discuz/index.php
阅读(695) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~