實驗名稱:比較tcp和udp的丟包行為
試驗目的:
1. 熟練用ns2做網絡仿真試驗的整個流程;
2. 練習寫tcl腳本,了解怎么應用http和rtp;
3. 練習用awk處理trace數據,了解怎么計算丟包率;
4. 練習用gnuplot繪制曲線圖,熟練gnuplot的使用。
實驗步驟:
1。確定網絡拓撲。
一個簡單的三個節點的拓撲,兩個運行cbr(const-bitrate)應用的發送結點,一個接收結點。一條鏈路使用tcp鏈接,一條鏈路使用udp連接。如圖。

2。寫tcl腳本。
# jiqing 2007-6-5 # this script is to compare the loss rates of http and rtp. set ns [new Simulator] #open a nam trace file set nf [open out.nam w] $ns namtrace-all $nf #open a trace file set tf [open out.tr w] $ns trace-all $tf #finish procedure proc finish {} { global ns nf tf $ns flush-trace close $nf close $tf exec ./nam out.nam & exit 0 } #create nodes set node(http) [$ns node] set node(rtp) [$ns node] set node(recv) [$ns node] #create links $ns duplex-link $node(http) $node(recv) 0.9Mb 10ms DropTail $ns duplex-link $node(rtp) $node(recv) 0.9Mb 10ms DropTail #set queue size $ns queue-limit $node(http) $node(recv) 10 $ns queue-limit $node(rtp) $node(recv) 10 #relayout nodes $ns duplex-link-op $node(http) $node(recv) orient right-down $ns duplex-link-op $node(rtp) $node(recv) orient right-up #set colors $ns color 1 blue $ns color 2 red #set a tcp connection set tcp [new Agent/TCP] $ns attach-agent $node(http) $tcp set sink [new Agent/TCPSink] $ns attach-agent $node(recv) $sink $ns connect $tcp $sink $tcp set fid_ 1 #set a cbr above tcp connection set cbr(http) [new Application/Traffic/CBR] $cbr(http) attach-agent $tcp $cbr(http) set type_ CBR $cbr(http) set packet_size_ 1000 $cbr(http) set rate_ 1mb $cbr(http) set random_ false #set a rtp connection set rtp [new Agent/UDP] $ns attach-agent $node(rtp) $rtp set null [new Agent/Null] $ns attach-agent $node(recv) $null $ns connect $rtp $null $rtp set fid_ 2 #set a cbr above tcp connection set cbr(rtp) [new Application/Traffic/CBR] $cbr(rtp) attach-agent $rtp $cbr(rtp) set type_ CBR $cbr(rtp) set packet_size_ 1000 $cbr(rtp) set rate_ 1mb $cbr(rtp) set random_ false #schedule $ns at 0.1 "$cbr(http) start" $ns at 0.1 "$cbr(rtp) start" $ns at 4.0 "$cbr(http) stop" $ns at 4.0 "$cbr(rtp) stop" $ns at 4.1 "finish" $ns run |
3。仿真。在命令提示符下輸入ns http_vs_rtp.tcl,回車。仿真結束,會調用nam演示動畫。
4。用awk處理trace數據。
awk的語法和c很像,不同的是awk中使用變量不用事先聲明。一個awk程序的結構如下面所示:
BEGIN{
...
}
{
...
}
END{
...
}
可見,程序分為三塊,BEGIN只在程序進入時執行一次,END只在程序退出前執行一次。而中間的程序塊會在處理每行數據時都執行一次。
這里用awk計算tcp和udp連接的丟包率,具體程序如下:
BEGIN { tcp_droped = 0; udp_droped = 0; last_tcp = -1; last_udp = -1; total_tcp = 0; total_udp = 0; i = 0; } { action = $1; time = $2; type = $5; seq_no = $11; if(action == "+" && type == "tcp"){ total_tcp = total_tcp + 1; } if(action == "+" && type == "cbr"){ total_udp = total_udp + 1; } if(action=="r"){ if(type=="tcp"){ if(seq_no != last_tcp + 1){ tcp_droped = tcp_droped + 1; } last_tcp = seq_no; } if(type=="cbr"){ if(seq_no != last_udp + 1){ udp_droped = udp_droped + 1; } last_udp = seq_no; } time_point[i] = time; if(total_tcp > 0) tcp_loss[i] = tcp_droped / total_tcp * 100; else tcp_loss[i] = 0; if(total_udp > 0) udp_loss[i] = udp_droped / total_udp * 100; else udp_loss[i] = 0; i = i + 1; } } END { printf("%.2f\t%.2f\t%.2f\n",0,0,0); for(j=0;j < i; i++){ printf("%.2f\t%.2f\t%.2f\n",time_point[j], tcp_loss[j], udp_loss[j]); } } |
awk程序寫好后,在命令行下輸入awk -f measure_loss.awk > http_rtp.txt,將把printf輸出的結果寫入http_rtp.txt。參見http_rtp.txt。
。。。。。。。 0.91 6.45 0.00 0.91 6.45 0.00 0.92 6.38 0.00 0.92 6.38 0.00 0.92 6.38 0.00 0.93 6.32 0.96 0.93 6.32 0.96 0.93 6.32 0.95 0.94 6.25 0.95 0.94 6.25 0.94 0.94 6.25 0.94 0.95 6.19 0.94 0.95 6.19 0.93 0.95 6.19 0.93 0.95 6.12 0.93 0.96 6.12 0.93 。。。。。。。 |
5。用gnuplot畫圖。
怎么進入和退出gnuplot?
開啟xserver(運行startxwin.bat)后,輸入gnuplot,將進入交互式的畫圖。如果要退出,輸入quit。
http_rtp.txt中有三列數據,分別是時間、tcp丟包率、udp丟包率。
怎么控制gnuplot選擇其中兩列作為x,y軸的數據呢?
plot "filename" using 2將會把第二列作為y軸的數據,第一列默認是x軸的數據。
plot "filename" using 2:3將把第二列作為x軸的數據,第三列作為y軸的數據。
怎么在一張圖中繪制多條線?
多個數據源之間用逗號隔開就可以了,如:
plot "filename1" using 2, "filename2" using 2:3, ...
怎么把圖輸出為圖片?
set terminal gif //設置輸出格式
set output "http_rtp.gif" //設置輸出文件名
replot //輸出
怎么為圖片增加說明?
set title "title" //設置標題
set xlabel "xlabel" //設置x軸說明
set ylabel "ylabel" //設置y軸說明

實驗中遇到的困難及解決辦法:
1。問題:不知道怎么使用rtp agent。
解決:找不到關於這方面的資料,先用udp代替。
2。問題:不知道怎么使用http
解決:關於http的使用ns-2手冊 39.7web cache一節,可供借鑒。但是建立一個http應用比較麻煩,需要http/server, http/client, http/cache。這里用cbr來代替。
實驗結果:
1。當鏈路帶寬設為1mb,tcp和udp速率都為1000時,tcp有丟包現象,udp沒有丟包現象,這大概是因為tcp有確認報文,而udp沒有的緣故。當把鏈路帶寬都設定為0.9Mb,tcp和udp都有嚴重的丟包現象。可見雖然tcp有重傳機制,但在帶寬太低的情況下,仍然會丟包。
2。最終的曲線圖說明,tcp的擁塞控制機制發揮了作用,丟包率逐步下降。而udp沒有擁塞控制機制,當緩沖隊列滿后,丟包率迅速上升。
參考文獻:
1. The ns Manual.
2. ja2 chung, mark claypool. ns by example.
3. 柯志亨的個人站點:http://140.116.72.80/~smallko/。