<?xml version="1.0" encoding="Big5"?>
<?xml-stylesheet type="text/css" href="document.css"?>
<document xmlns:HTML="http://www.w3.org/1999/xhtml">
	<title>
	Dynamic Routing 模擬範例
	</title>
	<content>
	<subject>→ DV Routing</subject>
	DV Routing (Distance Vector Routing) 是 Bellman Ford Algorithm 的實做, 他是一種 dynamic routing 的
	protocol (可調整的protocol). 和 Link State Routing 同為 least-cost 演算法. RIP 在 internet 
    下便是使用到了 DV routing, 他的 Distance 可表示 hop 數, queue 長度, 及延遲 (delay). 	
	因此在 Dynamic Routing 示範範例裡我們以他來做範例. 
	<HTML:BR/>
	Bellman Ford Algorithm 的定義：
	<HTML:BR/>
		<HTML:OL>
		<HTML:FONT color="red">== Definition ==</HTML:FONT>
			<HTML:OL>
				<item>S = source node</item>
				<item>w(i,j) = node i到node j的link cost</item>
				<item>w(i,i) = 0</item>
				<item>w(i,j) = ∞, 如果兩個 nodes 沒有直接地連結</item>
				<item>w(i,j) ≧ 0 , 如果兩個 nodes 有直接地連結</item>
			    <item>h = maximum number of links in a path at the current stage of the algorithm</item>
				<item>Lh(n) = cost of the least-cost path from node s to node n under the constraint of 
				no more the h links</item>
			</HTML:OL>
		<HTML:BR/>
		<HTML:FONT color="red">== Initialization ==</HTML:FONT>
			<HTML:OL>
				<item>L0(n)= ∞, for all n≠s</item>
				<item>Lh(s)=0, for all h</item>
			</HTML:OL>
		<HTML:BR/>	
		<HTML:FONT color="red">== Update ==</HTML:FONT>
	   		<HTML:OL>	
				<item>For each successive h≧0</item>
				<HTML:OL><item>Lh+1=Min(j) [Lh(j) + w(j,n)]</item></HTML:OL>
				<HTML:BR/>
				<item>
				Connect n with the predecessor node j that achieves the minimum, and eliminate any 
				connection of n with a different predecessor node formed during an earlier iteration. 
				The path from s to n terminates with the link from j to n.
				</item>
			</HTML:OL>	
		</HTML:OL>		
	<HTML:BR/>
	接下來就以一連串的圖來解釋 Bellman Ford Algorithm 的方法 : 
	<HTML:HR width="70%" align="left"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_1.PNG" width="400" height="200"/>
	h=0
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_2.PNG" width="400" height="200"/>
	h=1
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_3.PNG" width="400" height="200"/>
	h=2
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_4.PNG" width="400" height="200"/>
	h=3
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_5.PNG" width="400" height="200"/>
	h=4
	<HTML:BR/>
	<HTML:HR width="70%" align="left"/>
	<HTML:BR/>
	<HTML:BR/>
	<HTML:BR/>
	DV routing 如何做 routing 的動作?
		<item>1. 每一個 router 告知鄰近的 router 自己的 routing table</item>
		<item>2. 對於每條 network 路徑, 接收到的 router 會從鄰近的通知中挑選最低的 cost</item>
		<item>3. 然後, 將之加入自己的routing table中重新通知鄰近的router</item>
	<HTML:BR/>	
	一般可使 DV Routing 提高效能的有 split horizon, poison reverse, triggered updates, 和 holddown 這些方法
	, 在 ns-2 裡也有提供一些參數或函式來實做到這些方法.	
	</content>
	<content>
	<subject>→ 程式碼解說部分</subject>
	接下來會用 ns2 來模擬 DV routing 來解決遇到 link failure 的情況. 底下解說的程式碼可以從
	<HTML:A href="http://www.isi.edu/nsnam/ns/tutorial/examples/example3.tcl">
	http://www.isi.edu/nsnam/ns/tutorial/examples/example3.tcl
	</HTML:A> 
	來下載.
	<HTML:BR/>
	<HTML:BR/>
	程式碼部分 : 
	<HTML:HR width="70%" align="left"/>
		<HTML:OL>
		<item><HTML:FONT color="green">#開啟一個模擬物件</HTML:FONT></item>
		<item><HTML:B>set ns [new Simulator]</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#告訴 simulator 使用 dynamic routing</HTML:FONT></item>
		<item><HTML:B>$ns rtproto DV</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#開啟nam trace file</HTML:FONT></item>
		<item><HTML:B>set nf [open out.nam w]</HTML:B></item>
		<item><HTML:B>$ns namtrace-all $nf</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#定義結束的行程</HTML:FONT></item>
		<item><HTML:B>proc finish {} {</HTML:B></item>
			<HTML:OL>
			<item><HTML:B>global ns nf	</HTML:B></item>
			<item><HTML:B>$ns flush-trace </HTML:B></item>
			<item><HTML:FONT color="green">#Close the trace file</HTML:FONT></item>
			<item><HTML:B>close $nf </HTML:B></item>
			<item><HTML:FONT color="green">#Execute nam on the trace file</HTML:FONT></item>
			<item><HTML:B>exec nam out.nam &amp; </HTML:B></item>
			<item><HTML:B>exit 0</HTML:B></item>
			</HTML:OL>
		<item><HTML:B>}</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#產生七個 nodes</HTML:FONT></item>
		<item><HTML:B>for {set i 0} {$i &lt; 7} {incr i} {</HTML:B></item>
			<HTML:OL>
			<item><HTML:B>set n($i) [$ns node]</HTML:B></item>
			</HTML:OL>	
		<item><HTML:B>}</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#產生 node 間的 link</HTML:FONT></item>
		<item><HTML:B>for {set i 0} {$i &lt; 7} {incr i} {</HTML:B></item>
			<HTML:OL>
			<item><HTML:B>$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail</HTML:B></item>
			</HTML:OL>	
		<item><HTML:B>}</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#產生 UDP agent 並且在 node n(0) 綁上他</HTML:FONT></item>
		<item><HTML:B>set udp0 [new Agent/UDP]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n(0) $udp0</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 產生 CBR traffic source 並且將之綁在 udp0</HTML:FONT></item>
		<item><HTML:B>set cbr0 [new Application/Traffic/CBR]</HTML:B></item>
		<item><HTML:B>$cbr0 set packetSize_ 500</HTML:B></item>
		<item><HTML:B>$cbr0 set interval_ 0.005</HTML:B></item>
		<item><HTML:B>$cbr0 attach-agent $udp0</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#產生Null agent (a traffic sink)並且將之綁在node n(3)</HTML:FONT></item>
		<item><HTML:B>set null0 [new Agent/Null]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n(3) $null0</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#連結 traffic source 到 traffic sink </HTML:FONT></item>
		<item><HTML:B>$ns connect $udp0 $null0</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#為 CBR agent 排行程並使網路自動化</HTML:FONT></item>
		<item><HTML:B>$ns at 0.5 "$cbr0 start"</HTML:B></item>
		<item><HTML:B>$ns rtmodel-at 1.0 down $n(1) $n(2)</HTML:B></item>
		<item><HTML:B>$ns rtmodel-at 2.0 up $n(1) $n(2)</HTML:B></item>
		<item><HTML:B>$ns at 4.5 "$cbr0 stop"</HTML:B></item>
		<item><HTML:FONT color="green"># simulation time 5 秒時呼叫結束程序</HTML:FONT></item>
		<item><HTML:B>$ns at 5.0 "finish"</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">#開始跑模擬</HTML:FONT></item>
		<item><HTML:B>$ns run</HTML:B></item>
		<HTML:BR/>	
		</HTML:OL>
	</content>	
	<content>
	<subject>→ 程式執行說明 : </subject>
	執行程式, 可以看到一開始有很多小的 packets 在 network 上面跑, 如果你將 nam 
	慢到足夠的速度去按其中一個封包的話, 會發現到他們是 rtProtoDV 的封包(圖一), 他們可以交換 nodes 之間的
	routing 資訊. 0.5秒時, node0 送 packet 到 node3 (圖二). 當執行到1秒時, node1 到 node2 之間中斷 (圖三)
	, node1 會發 rtProtoDV 封包告知 node1, node1 至 node2 不通(圖四), 要更新路由, node0 即更新路由(圖五)
	, 並且轉送封包至 node6, 而 router 彼此間也會傳遞要更新路由的訊息(圖六), traffic 會繼續經過 node 
	6, 5, 4 重新路由(圖七). 到了2秒時, node1 到 node2 之間恢復, 各 router 彼此間傳遞更新路由的封包(圖八)
	, 封包再度經由 node1, 2傳遞.(圖九)	
	<HTML:BR/>
	模擬結果 : 
	<HTML:HR/>	
	<HTML:IMG src="graph/ns_routing_dynamic_6.PNG" width="400" height="300"/>圖一	
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_7.PNG" width="400" height="300"/>圖二
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_8.PNG" width="400" height="300"/>圖三
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_9.PNG" width="400" height="300"/>圖四
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_10.PNG" width="400" height="300"/>圖五
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_11.PNG" width="400" height="300"/>圖六
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_12.PNG" width="400" height="300"/>圖七
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_13.PNG" width="400" height="300"/>圖八
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_dynamic_14.PNG" width="400" height="300"/>圖九
	<HTML:BR/>
	</content>	
	
</document>

