NS3網路模擬
阿新 • • 發佈:2019-01-04
- NS3安裝與配置
- 點對點通訊
- 交換機(ARP協議)
- 其他分析檔案
1. 安裝與配置
1.1預先安裝環境
- apt-get install gcc g++ python
- apt-get install gcc g++ python python-dev
- apt-get install mercurial python-setuptools git
- apt-get install qt4-dev-tools libqt4-dev
- apt-get install cmake libc6-dev libc6-dev-i386 g++-multilib
- apt-get install gdb valgrind
- apt-get install gsl-bin libgsl2 libgsl-dev
- apt-get install flex bison libfl-dev
- apt-get install tcpdump
- apt-get install sqlite sqlite3 libsqlite3-dev
- apt-get install libxml2 libxml2-dev
- apt-get install libgtk2.0-0 libgtk2.0-dev
- apt-get install vtun lxc
- apt-get install uncrustify
- apt-get install doxygen graphviz imagemagick
- apt-get install texlive texlive-extra-utils texlive-latex-extra texlive-font-utils texlive-lang-portuguese dvipng
- apt-get install python-sphinx dia
- apt-get install python-pygraphviz python-kiwi python-pygoocanvas libgoocanvas-dev ipython
- apt-get install libboost-signals-dev libboost-filesystem-dev
- apt-get install openmpi-bin openmpi-common openmpi-doc libopenmpi-dev
1.2下載ns-3
當執行完以上命令後,你會看到如下內容
destination directory: ns-3-allinone
requesting all changes
adding changesets
adding manifests
adding file changes
added 26 changesets with 40 changes to 7 files
7 files updated, 0 files merged, 0 files removed, 0 files unresolved
執行結束後可以在repos目錄下看到ns-3-allinone目錄,該目錄下包含下面檔案:
build.py* constants.py dist.py* download.py* README util.py
進入該目錄,執行如下命令:
- ./download.py -n ns-3-dev
執行結束後,可以看到目錄下多了一些檔案:
build.py* constants.pyc download.py* nsc/ README util.pyc constants.py dist.py* ns-3-dev/ pybindgen/ util.py
進入ns-3-dev目錄,準備build ns-3。
1.3編譯ns-3
- cd ns-3-dev
- ./build.py
執行期間會有大量編譯資訊,最後提示build finished successfully.
進入ns-3-dev目錄使用waf構建
- ./waf -d optimized configure
- ./waf -d debug configure
- ./waf
- ./waf -d debug –enable-sudo configure
1.4.測試用例
首先在ns-3-dev/examples/tutorial/下找到 myfirst.cc檔案,將他拷貝到到scratch目錄下。
然後再返回ns-3-dev目錄,執行如下命令:
- ./waf –run myfirst
1.5安裝netanim
- cd netanim
- make clean
- qmake NetAnim.pro
- make
1.6測試
在myfirst.cc檔案中新增標頭檔案
#include “ns3/netanim-module.h”
並在Simulartor::run()這句程式碼之前新增如下語句:
AnimationInterface anim(“myfirst.xml”);
儲存後執行,
- ./waf – run myfirst
可以發現有myfirst.xml檔案生成。
接下來進入netanim資料夾下,執行netanim
- ./NetAnim
然後選擇剛才生成的myfirst.xml檔案,點選執行即可。
2. 點對點通訊
2.1網路拓撲
2.2源程式分析
int main (int argc, char *argv[])
{
//設定時間單位
Time::SetResolution (Time::NS);
//兩個日誌元件生效,並且日誌級別設定為INFO
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
//宣告一個結電容器並建立兩個節點
NodeContainer nodes;
nodes.Create (2);
//設定點到點協議的延遲和資料速率
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("500bps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
//安裝點到點網路裝置
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
//安裝協議棧
InternetStackHelper stack;
stack.Install (nodes);
//設定IP地址
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);
//應用程式部分,設定udp回顯服務應用
UdpEchoServerHelper echoServer (9);
//伺服器端應用開始
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
//設定服務開始和結束時間
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
//客戶端應用開始
ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
//產生動畫
AnimationInterface anim("myFirst.xml");
//開始模擬
Simulator::Run ();
//模擬結束
Simulator::Destroy ();
return 0;
}
2.3執行結果
3.交換機(ARP協議模擬)
3.1網路拓撲
3.2實驗解釋
傳送資料,從n0到n1。
3.3原始碼分析
int main (int argc, char *argv[])
{
// 建立四個節點
NodeContainer terminals;
terminals.Create (4);
//建立網橋
NodeContainer csmaSwitch;
csmaSwitch.Create (1);
//建立拓撲結構
//設定延遲和位元率
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
// 設定鏈路,將各節點連線到網橋
NetDeviceContainer terminalDevices;
NetDeviceContainer switchDevices;
for (int i = 0; i < 4; i++)
{
NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
terminalDevices.Add (link.Get (0));
switchDevices.Add (link.Get (1));
}
//建立網橋裝置
Ptr<Node> switchNode = csmaSwitch.Get (0);
BridgeHelper bridge;
bridge.Install (switchNode, switchDevices);
// 安裝協議棧
InternetStackHelper internet;
internet.Install (terminals);
// 設定IP地址
//
NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
ipv4.Assign (terminalDevices);
//
// 建立ONoff應用基於udp
//
NS_LOG_INFO ("Create Applications.");
uint16_t port = 9; // Discard port (RFC 863)
OnOffHelper onoff ("ns3::UdpSocketFactory",
Address (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
onoff.SetConstantRate (DataRate ("500kb/s"));
ApplicationContainer app = onoff.Install (terminals.Get (0));
// 設定開始應用的時間
app.Start (Seconds (1.0));
app.Stop (Seconds (10.0));
// 設定接收的節點
PacketSinkHelper sink ("ns3::UdpSocketFactory",
Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
app = sink.Install (terminals.Get (1));
app.Start (Seconds (0.0));
// 記錄日誌
AsciiTraceHelper ascii;
csma.EnableAsciiAll (ascii.CreateFileStream ("csma-bridge.tr"));
// 建立每個節點的埠傳送接收包的情況
csma.EnablePcapAll ("csma-bridge", false);
// 產生動畫,用於NetAnim
AnimationInterface anim("csma-bridge.xml");
//開始模擬
Simulator::Run ();
Simulator::Destroy ();
}
3.4實驗結果
第一次傳送時,網橋不知道傳送給哪個埠,所以進行廣播;
之後地址表建立,便不再進行廣播。
4.其他分析檔案
4.1Pcap檔案
Wireshark:可以看到每個結點接收發送資料的資訊:
4.2 tr 跟蹤檔案
每一筆記錄的開始欄位都是封包事件發生的原因:
r表示這個封包被某個節點接受,+表示進入佇列,-表示離開佇列,d表示封包被佇列丟棄;