660 likes | 694 Views
Learn about NS-2 directory structure, simulation scenarios, protocols supported, and how to run and write scenarios. NS-2 is a discrete event simulator for networking research.
E N D
Network Simulator (NS-2) 2009/3/31 I-Wei Ting (丁義偉)
Outline • Part 1: Introduction • Part 2: NS2 directory structure • Part 3: Network Scenario
A: What is network simulator? • NS is a discrete event simulator targeted at networking research. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks. • NS began as the REAL network simulator in 1989 and has evolved over the past few years. • In 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI. • In 1996, the first version of ns2 was release.
Version • ns-2.34 pending • ns-2.33 released on March 31, 2008 • .. • .. • ns-2.30 released on Sept 26, 2006 • .. • ns-2.1b9 released on Sat Apr 13 16:26:33 PDT 2002 • .. • ns-2.0a1 Wed Nov 6 13:57:31 PST 1996 • v1.0a1 Mon Jul 31 16:05:08 PDT 1995 http://www.isi.edu/nsnam/ns/CHANGES.html
Why we use it? • New protocol is not easily implemented in real. • (development, money, time, device, people) • New protocol is not easily verified in the world. • (mobile IP, ad hoc…etc, more nodes or network topology) • New protocol is not easily compared others • (How to convince other people?) • Network Simulator simulates protocol layers • (physical, datalink, network, transport, applicatoin)
B: Where do I get NS2? http://www.isi.edu/nsnam/ns
C: What platforms does NS2 run on and how to install NS2? • Operation systems • (FreeBSD, Linux, SunOS, Solaris, Windows) • Setup for Linux-based system • If the file is put at /root • Step 1: cd /root • Step 2: tar zxvf ns-allinone-2.30.tar.gz • Step 3: cd /root/ns-allinone-2.30 • Step 4: ./install • Setup for win9x/2000/XP • 中文教學網站-柯志亨(Chih-Heng, Ke) • http://140.116.72.80/~smallko/ns2/ns2.htm • 安裝流程 • http://140.116.72.80/~smallko/ns2/setup.htm 註:在裝cygwin時,要自已勾選全部的套件,以免lost套件,無法compiler過去。
D: Where can I find documentation for NS2? download http://www.isi.edu/nsnam/ns/ns-documentation.html
E: Where are some tutorial I can start from? http://www.isi.edu/nsnam/ns/tutorial/index.html
F: What protocols does NS2 support? • Application layer: Traffic models and applications • Web, FTP, telnet, constant-bit rate, real audio • Translation layer: Transport protocols • Unicast: TCP(Reno,Vegas,etc.), UDP • Multicast: SRM • Network layer: Routing and queueing • Wired routing: Dijkstra,DV • Wireless routing: mobile IP • Ad hoc routing :DSR,AODV,TORA,DSDV • Queueing protocols: RED, drop-tail, SFQ etc • Data link layer • Ethernet, 802.11 • Physical layer • Wired(point-to-point, LANs), wireless (multiple propagation models)…
Part 2: NS2 structure • A: Why use two programming language? • B: Overview for NS2 structure • C: Directory structure • D: How to run a scenario in NS2
A: Why using two programming language to implement? • 第一:需要一種程式設計語言,能夠有效率的處理位元組(Byte),封包標頭(Packet Header)等資訊,需要應用合適的演算法在大量的資料上。因此,程式內部的運行速度(run-time speed)就非常重要。(c++) • 第二: 許多網路中的研究工作都圍繞著網路元件和環境參數的設置和改變而進行的,需要在短時間內快速的開發和模擬出所需要的網路環境(scenarios),並且方便修改和發現、修復。在這種情形下,(run-around time)就顯得很重要了,因為模擬環境的建立和參數資訊的配置只需要運行一次。(Otcl) • NS2 is an object-oriented, discrete event driven network simulator developed at UC Berkely written in C++ and OTcl. • C++ for “data” • Per packet action • OTcl for “control” • Periodic or triggered action
Event-driven simulator Event 1 Simulation Time
C: Directory structure (After install) ns-allinone-2.30 ….. tcl8.4 tk8.4 bin ns-2.30 nam-1.12 Execute files Source node (C++)
D: How to run a scenario in NS2 (linux-based) • Step 1: cd /root/ns-allinone-2.30 • Step 2: cd bin • Step 3: ./ns my_scenario.tcl
Part 3: Network Scenario • A: What is network scenario? • B: How to write a simple scenario • C: How to run a scenario in NS2 (linux-based) • D: How to get the trace file?
A: What is network scenario? (人、事、時、地、物) http://www.isi.edu/nsnam/ns/tutorial/index.html
B: How to write a simple scenario? • 1.建立一個模擬器物件 (Must) set ns [new Simulator] • 2.開啟一個nam trace 檔案 (Option) set nf [open out.nam w] $ns namtrace-all $nf • 3.宣告一個finish程序(Must) proc finish {} { global ns nf $ns flush-trace # Close the trace file close $nf # Execute nam on the trace file exec nam out.nam & exit 0 }
(simulation environment) • 1.建立節點及其屬性 • Wired node • Wireless node • wireless mobile IP • ad-hoc networks • 2.建立節點之間的實體連線及其性質 • 3.建立傳輸的應用程式及其傳輸速率
1、建立有線的節點 (Created wired nodes) 2、建立實體連線 (Created physical link) • set my_node0 [$ns node] • set my_node1 [$ns node] • $ns duplex-link $n0 $n2 2Mb 10ms SFQ • $ns simple-link $n1 $n2 3Mb 2ms RED • 雙向、單向,頻寬,延遲時間,佇列的種類 (DropTail, FQ, SFQ, RED, CBQ) 3、設定佇列的大小 (set queue length) • $ns queue-limit $my_node0 $my_node1 50 4、設定節點及連線的label、顏色(color) (Option) $ns duplex-link-op $n0 $n1 color "green” $ns duplex-link-op $n0 $n1 label“line1”
5、設定連線的成本 (link cost) 6、設定繞路(routing)屬性 • $ns cost $my_node1 $my_node2 10 • $ns cost $my_node0 $my_node2 5 • 預設 :static routing • 靜態 routing:$ns rtproto Static • 動態 routing :$ns rtproto DV • $ns rtproto DV$n1 $n2 $n3
7、建立UTP連線 set udp0 [new Agent/UDP]#建立Agent $ns attach-agent $n0 $udp0#此Agent連於n0節點上 set cbr0 [new Application/Traffic/CBR]#應用程式 $cbr0 set packetSize_ 400#封包大小(bytes) $cbr0 set interval_ 0.25#設定傳送的間隔 $cbr0 attach-agent $udp0 set sink [new Agent/LossMonitor]#建立接受端 $ns attach-agent $n3 $sink#接受端節點 $ns connect $udp0 $sink#將傳送端及接受端連在一起
8、建立FTP連線 set tcp [new Agent/TCP] #建立TCP連線 $ns attach-agent $n0 $tcp #將此TCP連至n0節點 set ftp [new Application/FTP] #建立一個FTP應用程式 $ftp attach-agent $tcp #將此FTP連至TCP物件 set sink [new Agent/TCPSink] #建立TCP的接受端 $ns attach-agent $n3 $sink #將接受端連至n3節點 $ns connect $tcp $sink #將傳送端及接受端連在一起
(time schedular, event occur) • 此部份要安排事件發生的前後順序 例如: • 第2秒, A節點開始傳輸UTP封包到B節點 第3秒, C節點開始傳輸TCP封包到D節點 第17秒, A節點結束傳送 第16秒, C節點結束傳送 $ns at 2 "$cbr0 start" $ns at 17 “$cbr0 stop” $ns at 3 "$ftp start" $ns at 16 “$ftp stop” $ns at 20 "finish" • 設定某連線中斷及修復 $ns rtmodel-at 1.0 down $my_node1 $my_node $ns rtmodel-at 2.0 up $my_node1 $my_node2
(Any Editor: notepad,ultra-editor…) 1.set ns [new Simulator] 2. set nf [open out.nam w] 3. $ns namtrace-all $nf 4. proc finish {} { 5. global ns nf 6. $ns flush-trace 7. close $nf 8. exec ./nam out.nam & 9. exit 0 10 } 11. set n0 [$ns node] 12. set n1 [$ns node] 13. set n2 [$ns node] 14. set n3 [$ns node] 15. $ns duplex-link $n0 $n2 2Mb 10ms DropTail 16. $ns duplex-link $n1 $n2 2Mb 10ms DropTail 17. $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail 18. $ns queue-limit $n2 $n3 10 (Option) (Option)
19. set tcp [new Agent/TCP] 20. $ns attach-agent $n0 $tcp 21. set sink [new Agent/TCPSink] 22. $ns attach-agent $n3 $sink 23. $ns connect $tcp $sink 24. set ftp [new Application/FTP] 25. $ftp attach-agent $tcp 26. set udp [new Agent/UDP] 27. $ns attach-agent $n1 $udp 28. set null [new Agent/LossMonitor] 29. $ns attach-agent $n3 $null 30. $ns connect $udp $null 31. set cbr [new Application/Traffic/CBR] 32. $cbr attach-agent $udp 33. $cbr set packet_size_ 1000 34. $cbr set interval_ 0.25
35. $ns at 0.1 "$cbr start" 36. $ns at 1.0 "$ftp start" 37. $ns at 4.0 "$ftp stop" 38. $ns at 4.5 "$cbr stop" 39. $ns at 5.0 "finish" 40. $ns run
C: How to run a scenario in NS2 (linux-based) • Step 1: cd /root/ns-allinone-2.30 • Step 2: cd bin • Step 3: ./ns my_scenario.tcl
Useful additional program • NAM: Viewing network simulation traces and real world packet traces
C: How to get the trace file? set my_trace [open demo1.tr w] $ns trace-all $my_trace
每個欄位所代表的意義如下: • 1.代表事件的類別 • r:代表目的端收到packet • +:代表 packet 放入 queue 中 • -:代表 packet 從 queue 中取出 • d:代表 queue 已經滿了,這個 packet 被 drop 掉 • 2.代表事件發生的時間 • 3.代表 packet 的 source node • 4.代表 packet 的 destination node • 5.代表 packet 的類別 • 6.代表 packet 的大小 (encoded in IP header) • 7.代表 packet 的 flags • 8.代表 connection(flow) 的 id • 9.代表 source address ( node.port ) • 10.代表 destinations address ( node.port ) • 11.代表 packet 的 sequence number ( network layer protocol's ) • 12.代表 packet 的 id ( unique )
Set f1 [open demo1.record w] • proc record { } { global null f1 set ns [Simulator instance] set time 0.1 set now [$ns now] set bw [$null set bytes_ ] puts $f1 “$now $bw” $null set bytes_ 0 $ns at [expr $now+$time] “record” } • $ns at 0.0 “record” • LossMonitor • nlost_ :Number of packets lost • npkts_ :Number of packets received • bytes_ :Number of bytes received • lastPktTime_ :Time at which the last packet was received • expected_ :The expected sequence number of the next packet
Network Setting • Sender:Node 1, 2, 3 • Receiver:Node 7 • Using CBR • Packet Size:1000(k) • Time Interval:1 (s) • Simulation Time:15 (s)
Tcl Code #Create a simulator object set ns [new Simulator] #Define different colors for data flows $ns color 1 Blue $ns color 2 Red $ns color 3 Green #Open the nam trace file set nf [open out.nam w] $ns namtrace-all $nf #Open the trace file set nd [open out.tr w] $ns trace-all $nd
Tcl Code #Define a 'finish' procedure proc finish {} { global ns nd nf $ns flush-trace #Close the trace file close $nd close $nf #Execute nam on the trace file exec nam out.nam & exit0 }
Tcl Code #Create four nodes set s0 [$ns node] set s1 [$ns node] set s2 [$ns node] set r0 [$ns node] set r1 [$ns node] set r2 [$ns node] set d [$ns node] #Create links between the nodes $ns duplex-link $s0 $r0 10Mb 10ms DropTail $ns duplex-link $s1 $r0 10Mb 10ms DropTail $ns duplex-link $s2 $r1 10Mb 10ms DropTail $ns duplex-link $r0 $r2 10Mb 10ms DropTail $ns duplex-link $r1 $r2 10Mb 10ms DropTail $ns duplex-link $r2 $d 10Mb 10ms DropTail s0 r0 s1 r2 d r1 s2
Tcl Code #Set position of each node $ns duplex-link-op $s0 $r0 orient right-down $ns duplex-link-op $s1 $r0 orient right-up $ns duplex-link-op $s2 $r1 orient right-up $ns duplex-link-op $r0 $r2 orient right-down $ns duplex-link-op $r1 $r2 orient right-up $ns duplex-link-op $r2 $d orient right #Monitor the queue for the link $ns duplex-link-op $r0 $r2 queuePos 0.5 $ns duplex-link-op $r1 $r2 queuePos 0.5 $ns duplex-link-op $r2 $d queuePos 0.5
Tcl Code #Set flow ID $udp0 set fid_ 1 $udp1 set fid_ 2 $udp2 set fid_ 3 #Schedule events for the CBR agents $ns at 0.0 "$cbr0 start" $ns at 0.0 "$cbr1 start" $ns at 0.0 "$cbr2 start" $ns at 15.0 "$cbr0 stop" $ns at 15.0 "$cbr1 stop" $ns at 15.0 "$cbr2 stop“ #Call the finish procedure after 5 seconds of simulation time $ns at 15.0 "finish" #Run the simulation $ns run
Analyze Result • out.tr + 0 1 3 cbr 1000 ------- 2 1.0 6.1 0 1 - 0 1 3 cbr 1000 ------- 2 1.0 6.1 0 1 + 0 1 3 cbr 1000 ------- 2 1.0 6.1 1 2 + 0 1 3 cbr 1000 ------- 2 1.0 6.1 2 3 + 0 1 3 cbr 1000 ------- 2 1.0 6.1 3 4 + 0 1 3 cbr 1000 ------- 2 1.0 6.1 4 5 + 0 2 4 cbr 1000 ------- 3 2.0 6.1 0 6 - 0 2 4 cbr 1000 ------- 3 2.0 6.1 0 6 + 0 2 4 cbr 1000 ------- 3 2.0 6.1 1 7 ...
Analyze Result event r:receive(at to_node) +:enqueue(at queue) -:dequeue(at queue) d:drop (at queue) Src addr:(node).(port) Dst addr:(node).(port)
awk – measure delay BEGIN { #程式初始化,設定一變數以記錄目前最高處理封包的ID。 highest_packet_id = 0; } { action = $1; time = $2; from = $3; to = $4; type = $5; pktsize = $6; flow_id = $8; src = $9; dst = $10; seq_no = $11; packet_id = $12;
awk – measure delay #記錄目前最高的packet ID if ( packet_id > highest_packet_id ) highest_packet_id = packet_id; #記錄封包的傳送時間 if ( start_time[packet_id] == 0 ) start_time[packet_id] = time; #記錄CBR (flow_id=1) 的接收時間 if ( flow_id == 3 && action != "d" ) { if ( action == "r" ) { end_time[packet_id] = time; } } else { #把不是flow_id=2的封包或者是flow_id=2但此封包被drop的時間設為-1 end_time[packet_id] = -1; } }
awk – measure delay END { #當資料列全部讀取完後,開始計算有效封包的端點到端點延遲時間 for ( packet_id = 0; packet_id <= highest_packet_id; packet_id++ ) { start = start_time[packet_id]; end = end_time[packet_id]; packet_duration = end - start; #只把接收時間大於傳送時間的記錄列出來 if ( start < end ) printf("%f %f\n", start, packet_duration); }