NS3对于初学者的教程,高手忽略这个吧_第1页
NS3对于初学者的教程,高手忽略这个吧_第2页
NS3对于初学者的教程,高手忽略这个吧_第3页
NS3对于初学者的教程,高手忽略这个吧_第4页
NS3对于初学者的教程,高手忽略这个吧_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1. Ns3 的安装: 开发环境配置: sudo apt-get install gcc g+ python python-dev/C+ 和 python 安装,必装 sudo apt-get install mercurial /NS3代码维护使用的源码版本控制管理系统 sudo apt-get install bzr /运行 python 绑定 ns-3-dev 需要 bazaar 这个组件 sudo apt-get install libgtk2.0-0 libgtk2.0-dev / sudo apt-get install gdb valgrind /调试工具 sudo apt-ge

2、t install doxygen graphviz imagemagick / sudo apt-get install texlive texlive-latex-extra / 基于GTK的配置系统 文档生成器 文档生成器,从源代码中生成说明文档 sudo apt-get install texinfo dia texlive-extra-utils texi2html /ns-3 具 手册和 tutorial 编写查看工 sudo apt-get install flex bison/ 仿真必需的词法分析器和语法分析生成器,必装 sudo apt-get install libgooc

3、anvas-dev /部分移动场景仿真的可视化测试需要这个组件 sudo apt-get install tcpdump /读取 pcap 的 packet traces ,即包嗅探器 sudo apt-get install sqlite sqlite3 libsqlite3-dev /支持统计特性的数据库软件 sudo apt-get install libxml2 /xml的配置存储软件 sudo apt-get install python-pygraphviz python-kiwi python-pygoocanvas /Gustavos ns-3-pyviz 可视化软件 sudo

4、 apt-get install qt4-qmake sudo apt-get install qt4-dev-tools 安装完成后: 进入 ns-allinone-3.16 目录 sudo ./build.py 之后 进入 ns-allinone-3.16/ns-3.16 sudo ./waf -check 返回目录 ns-allinone-3.16 进入目录 netanim-3.103 sudo make clean sudo qmake NetA sudo make 之后 sudo ls 可以看见绿色的 NetAnim 这个东西就成功了 sudo ./NetAnim 打开

5、仿真界面 安装全部完成 23 2.ns3仿真的运行 ctrl+alt+t 调岀终端,进入 ns-allinone-3.16 目录, 4 严 r zhQU(a)ubuntu: /personail/r-aUinone-3.16 zhouubuntui$ Is cprogramexamples desktop perso naL公共的 c+program nusicUbuntu One 模板聞片下载桌面 zhouubuntu:cd personal/ zhouubuntu:/personat$ Is ns-allinone-3.16 ns-alltnone-3.16.tar zhou(ubuntu

6、:/personal$ cd ns-allinone-3.16/ zhou0ubuntu:-/personal/ns-allinone*3.16$ Is buildpyconsta门亡宜”戸丫花 ns-3 16pybtndgen-09 uttl.py constants * py netanim-3.103 nsc-0.5.3 READMEutil.pyc zhou(ubuntu:-/personal/ns-alli.none-3.16$ | 输入 cd ns-3.16/ 进入ns-3.16文件夹 * zhou)ubuntu: /personaL/ns-aUiDone-5J6/

7、ns-3.l6 zhougubuntuIs cprog ran examples.desktop personal 公共的文档 c+program mustcUbuntu Ore模板 图片TR桌面 zhougubuntu匚d personal/ zhouubuntu:-/personals Is ns* alii none *3,15 ns-allirione-3 * 1 tar .bz2 zhou电ubiintzu /p巳吕。n日 1$ cd ns-aLlinone - 3 * 16 / rhouubuntu:-/personal/ns-allinone-3.16$ Is build.pyc

8、onstants.pyc ns-3.16pyb tndgen-0.IS.0.609 util.py constants _py netanim-3 103 nsc-0.5.3 READMEuittl * pyc ihQuubuntu;/pers9nal/ns-dllinone-316$ cd ns-3.16/ zhougubuntu:/personal/ns-alllnone3.lti/ns-3.16$ Is 输入 cd examples/tutorial/ AUTHORS scratchutils bindingsLICENSE buildH53 CHANCES.html README s

9、rc test py tetpy-output uttLs.py utils py匚 VERSION docRELEASE_NOTES testpy*supp waf zhouubuntu:-/personal/ns-alitnone-3,16/ns-3.16$ | waf.bat waf-tools wscrlpt WUttl*p wutils,pyc 进入examples里面的tutorial文件夹(里面有刚刚上手专用的实例,我们就用这个来调试) * zhou)ubuntu! /peronal/h-all1none-3-16/n-3.l6 zhougubuntuIs cprogran ex

10、amples.desktop e+progran nustc zhouubuntu匚d personal/ zhougubuntu:/persondl$ Is ns-allinone*3.lfi ns-alLinon personal 公共的视類文档 Ubuntu One模板 圏片 下载 zhou电ubuntu:-/personal$ cd ns-aliinone - 3,16/ zhouubuntu:-/personal/n5-alltnone-3*16$ Is build.pycanstants.pyc ns-3.16pybtndgen-0.IS.0.809 util.py constan

11、ts-py netanim-3.103 nw匚日5勺 READMEutil * pyc ihouubuntu;-/per5QnX/n5-3lT,nQne-15 cd nsY.l/ zhouubuntu:-/personal/ns-allinome-3.16/ns-3.16$ Is examples LICENSE ns3 README RELEASE NOTES scratch src testpy testpy-output py supp tils uttls * py utils pyc VERSION waf AUTHORS bindings build CHANGES.html do

12、c_- _ zhouubuntu:-/personaI/ns-allinone-3,16/ns-31 scratc 1527/1539 cxxprogram: h/f build/scratch/first.cc.8.o Antnattonlnterfa匚e tPosttion if it is AninationInterface tPosttion if It is WARNINGNodeO stationary WARNINGrNoderl stati.onary Does not have a mobility nodel* Use SetConstan Does not have a

13、 mobtltty node!. Use SetConstan At At At At_ zhouubuntu:-/personal/ns-allinone-316/ns-316$ | time time time time to m2 port 9 2s client sent 1024 bytes 2.00369s server received 1024 bytes fran 10.1,1.1 port 49153 2*00369s server 2,09737s client received 1024 bytes fron 10*1.1.2 port 9 sent 1324 byte

14、s to 10.1,1*1 port 49153 输入Is 可以看到有个名字叫做first.xml的文件这个就是可以当作动画的文件。 再用上一节的仿真界面的使用来运行这个就行 如图: 成功的仿真了 5. .tr和.pcap文件的生成和解析 tr文件的生成:需要在 SimuIator:Run ();这个的前面加上 AsciiTraceHelper ascii;csmaHelper.EnableAII (ascii.CreateFileStream (-1.tr csmO); pcap文件的生成:需要在 Simulator:Run ();这个的前面加上csmaHelper.EnablePcapAl

15、l (-fbspfalse); 如图: 於4 侖 zhcuf3)ZHOU-P匚:/khou-personal/hctwcrk-5iiriuhtiris-altinone-3dT/h3.17/scfdtch Animattonin vert ace anin fHyxinl/test-cifla-lrXnfJ; 9Q | 91 AscitTraceHelper 92 csnallelperLriableflsciiAll (asciiXreateFileGtrean (Hytrce/test-csna-l.tr); ?3csnaHelper.EnablePcapAll (nypcap/test-

16、 csHa-ipl t false); 94 95 Stnulator:Run (; 96 Stnulator:Destroy); 7 98return 0; 9館 之后按照上面的运行方法运行。(csmaHelper是由CsmaHelper这个类(协议)实例化的对象) 如图运行完成: 册。厂 zhcuZHOU-PC: -/ihou-personal/ittwork-siimilatoris-allinane-adZ/hs-J.IT 749/1612 c)o(: scratch/te i 75a/lli ckx: scratch/te 1445B/1612 c)cn: Ecratch/2l 14

17、1/1612 : scratch/tt 1467/1612 cxx: 14S2/1G12 ckx: scratch/te 149Z/1612v- isei/1612 1504/151Z: inulator-汕 1512/1612 lbSL/1612j 1561/1612 1591/1612J 15/1612 des-1 1593/1612 h/subdir/subdtr 1594/161Z 1595/1612 1596/1612 nt-To-Point cxxprogra*!: cxxprogra; omprograTi: cjocprogra:1!: CKJtprog ra; cxxprQf

18、lra; cxxprogrF: cxprcgra; 1 /tcstR udp*ltcc * a buld/sc rstch/test* udp cc15o biitld/scratch/test - csna-1. cc.9. o - butLd/seratch/test-esna -1 bulld/scratchyolsr - htna.cc . 8- build/scratch/olsr-hna but Ld/scratch/tt.cc, 7, c - butId/scratch/tt build/5cratch/test-ptp-Anodes -1 *cc 12. - butld/scr

19、atch/test-ptp-ino build/5cretch/subdir/5cretch-stnulto-subdir.cc.L3_o - build/scra build/scratch/second,cc.6.o build/scratch/second build/scrstcih/test-oXsr-1.cc.14,o - bulld/scratch/test-olsr-1 build/sc rot ch/test-Pox nt-To-Point - liCc.ld.o - build/scratch/test-Pot 之后Is看有没有自己命名的那些文件:我的tr文件为test-c

20、sma-1.tr我的pcap文件为test-csma-1.pcap 输入vim自己的文件名即:vim test-csma-1.tr 如图: Q L zhouJZHOU-PC: -/zhou-per:ArpHeader (request source nac? so - 96 - 00: o a: e o: oe : et) t o J s6:90;Oi source ipv4: . Cest tpu4: lfl.1.2.4 J P 刊 Igd (svie=lS) ns J; lEthcrnetTratler 5:00;Q0;00:03, destinatlonff:ff;ff:

21、ff;ff;ff) ns3:ArpHEthernetTrailer (fcs-9) 4 r 2OB21 /HodeLtst/4/DviceList/6/$ns3:iCsaNetuevtce/Macftx ns3:EtherretHeader ( length /type-OxBOG, source-00:00:00:00H0G;ff:ff:ff;ff;ff) ns3;iArpHeader (req uest source nac: Qa-asoeieoteeBt):EthernetTrsiXer (fcs=0) 召 4 2,63251 /HodeList/a/DeZiceLi5t/3/5ns3

22、:CsnNetLevtcelxQueueEnqueue rsJ::EthernetHeade r ( length/type-0 x605* 5Ource-OO:OO;oe:0O;OO;O6( destnatT.on-00;e0;e0: OC: 00; 03) n: Arpll eader (reply source nac: 606000:OU;00;00;03 dest ipv; 10,1,2.1) Pa/load (slze=18) ns3:EthernetTrailer (fcs-O) 呼Wns-lW 只i幻顶端 分析 Ascii Traces 在一个极其密集的文档中有很多信息,但是需

23、要注意的第一件事是在这个文件中有很多分立的行。除非你大大 加宽你的窗口,否则是很难看清楚的。 每一行对应了一个trace事件。本例中我们在查看每个点到点设备的传输队列的trace事件。传输队列是任一个目 的地为点到点信道的数据包的必经队列。注意trace文件的每行以一个单独的字符开始(后面带有空格)。这个字 符具有如下含义: +: An en queue operati on occurred on the device queue; -:A dequeue operati on occurred on the device queue; d: A packet was dropped, ty

24、pically because the queue was full; r: A packet was received by the net device. +:设备队列中的入队操作; -:设备队列中的岀队操作; d:数据包被丢弃,通常因为队列已满; r:网络设备接收到数据包。 我们来更详细的看一下trace文件的第一行。为了看得更清晰,我把这一行分成了不同的部分,并在左边标岀序 号: 00 + 01 2 02 /NodeList/O/DeviceList/O/$ ns3:Poi ntToPoi ntNetDevice/TxQueue/E nqueue 03 ns3:PppHeader (

25、04Poin t-to-Poi nt Protocol: IP (0 x0021) 05n s3:lpv4Header ( 06 tos 0 x0 ttl 64 id 0 protocol 17 offset 0 flags none 07len gth: 1052 ) 08n s3:UdpHeader ( 09len gth: 1032 49153 9) 10Payload (size=1024) 展开后的trace事件的第一行(序号00)代表操作。+字符表示是一个传输队列的入队操作。第二行(序号01)是单位 为秒的仿真时间。 Trace的下一行(序号02)

26、告诉我们trace发送端发起这个事件 (以tracing命名空间表示)。你可以认为tracing命名空间 有点像一个文件系统命名空间。命名空间的根为NodeList。这个NodeList是NS- 3核心代码管理的一个容器,此容器包含有一个脚本中创建的所有的节点。正如一个文件系统在根下有目录 ,在NodeList下有节点数。字符串 /NodeList/0是指NodeList中第0个节点,我们通常认为是node 0.每个节点中有一个已经安装好的设备列表。这个列表是在命名空间的下一个岀现的。可以看到trace事件来自 节点中安装的第0个设备DeviceList/0。 下一个字符串,$ns3:Poin

27、tToPointNetDevice告诉我们第0个节点的设备列表的第0个位置的设备类型。回忆序号 00处的+操作表示设备的传输队列发生了入队操作,这个在trace pathTxQueue/Enqueue的最后部分反映岀来了。 Trace中剩下的几行是很直观的。序号03-04处表明数据包封装成点到点协议。序号05- 07处显示数据包IP版本,发送端IP地址,接收端IP地址为。序号08- 09岀显示数据包的UDP头,最后序号10处表明数据包数据量为1024bytes。 在trace文件中的下一行显示了这个数据包在这个节点中从传输队列中被移除。 Trace文件的第三行显

28、示了数据包正在被回显服务器所在的节点的网络设备接收。trace如下。 00 r 01 2.25732 02 /NodeList/1/DeviceList/0/$ ns3:Poi ntToPoi ntNetDevice/MacRx 03n s3:lpv4Header ( 04 tos 0 x0 ttl 64 id 0 protocol 17 offset 0 flags none 05len gth: 1052 ) 06n s3:UdpHeader ( 07len gth: 1032 49153 9) 08Payload (size=1024) 注意,trace

29、操作现在是r并且仿真时间已经增加到2.25732秒。如果你一直按照本教程来操作,你已经把网络设 备的DataRate,和信道Delay设置成默认值。 在第2行中,Trace发送端命名空间条目已经改变,来显示这个事件是来自节点1(/NodeList/1),即数据包的接收t race端 (/MacRx)。通过查看文件中其他的traces,你可以很容易的跟踪数据包。 5.3.2 PCAP 格式 Traci ng ns-3设备控制器也可以被用来创建.pcap格式的trace文件。缩写pcap(通常为小写)表示packet capture,事实上是包含有定义一个 .pcap文件格式的API。可以读取并且

30、显示这种格式的最流行的程序是Wiresha rk(以前被称为Ethereal)。然而,有很多其他的分析器也使用这个包格式。我们鼓励读者了解其他的分析pcap追 踪文件的工具。在本教程中,我们用tcpdump来查看pcap trace。 在scratch/myfirst.cc中我们刚增加的 ASCII追踪代码后面插入这行代码。注意我们刚使用的是字符串myfirst而 不是myfirst.pcap。这是因为这里传递的参数是个前缀,而不是完整的文件名。在仿真过程中,helper将为任 何一个点到点设备创建一个追踪文件。文件名将包含预设前缀,节点名,设备名,和.pcap后缀。 在我们的例子脚本中,我们

31、最终将看到名为myfirst-0-0.pcap和myfirst-1- 0.pcap。这分别是为节点 0设备0,和节点1设备0创建的pcap trace文件。 用tcpdump读取结果 此处最简单的做法就是使用tcpdump来查看pcap文件, tcpdump -nn -tt -r myfirst-0-0.pcap reading from file myfirst-0-0.pcap, link-type PPP (PPP) 2.000000 IP .49153 .9: UDP, le ngth 1024 2.514648 IP .9 10.1.1

32、.1.49153: UDP, le ngth 1024 tcpdump -nn -tt -r myfirst-1-0.pcap reading from file myfirst-1-0.pcap, link-type PPP (PPP) 2.257324 IP .49153 .9: UDP, le ngth 1024 2.257324 IP .9 .49153: UDP, le ngth 1024 在myfirst-0-0.pcap(客户端设备)文件中可以看到回显数据包在第二秒被发送。如果查看第二个文件(myfirst-1- 0

33、.pcap)可以看到此包在2.257324秒被收到。在第二个文件中可以看到在2.257324秒这个包被回应,最终,在客 户端2.514648秒回应的包被收到。 5.322用Wireshark读取结果 如果你对 Wireshark不熟悉,从/可以下载到程序和文档。 trac Wireshark是一个可以用来显示trace文件的用户图形接口。如果你安装了Wireshark,可以打开和查看每 e文件,就像这些trace是使用包嗅探工具在真正的网络上抓下来一样。 对于trace文件和pcap文件 均可以自己编写gawk脚本来查看,编写方法自己百度。 以下为

34、实例讲解: 打开first.cc文件(前面的标号是我自己的vim配置,只是方便阅读,并不是代码。) 顶部的注释栏为 GNU声明 1 /* -*- Mode:C+; c-file-style:gnu; indent-tabs-mode:nil; -*- */ 2 /* 3 * This program is free software; you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License version 2 as 5 * published by the Fr

35、ee Software Foundation; 6 * 7 * This program is distributed in the hope that it will be useful, 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the 10 * GNU Ge neral Public Lice nse for more details. 11 * 12 * You should

36、have received a copy of the GNU General Public License 13 * along with this program; if not, write to the Free Software 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15 */ 然后是头文件包含 16 16 #i nclude n s3/core-module.h 核心模型库 17 #include ns3/network-module.h 网络模型库 18 #incl

37、ude ns3/internet-module.h 因特网模型库 19 #in clude n s3/po in t-to-po in t-module.h 点到点网络模型 20 #in clude n s3/applicati on s-module.h 应用模型库 正式代码: 22 21 using n amespace n s3; 使用ns3的命名空间 24 22 NS_LOG_COMPONENT_DEFINE (FirstScriptExample); 启用日志,日志是方便在运行和运行成功后查看输岀的信息的。 26 23 int 24 main (int argc, char *arg

38、v) 25 26 LogCompo nentEn able (UdpEchoClie ntApplicatio n, LOG_LEVEL_INFO); 开启客户机应用日志,记录客户机的使用情况- 27 LogCompo nentEn able (UdpEchoServerApplicatio n, LOG_LEVEL_INFO); 开启服务器应用日志,记录服务器的使用情况- 32 28 NodeC ontainer no des; 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 实例化一个节点容器对象,里面还是空的。 nodes.Create (2); 为容器创建两个新的节点。 PointToPointHelper pointToPoint; 实例化一个点到点协议的对象。 pointToPoint.SetDeviceAttribute (DataRate, StringValue (5Mbps); 设置点到点协议的数据传输速率。 pointToPoint.SetChannelAt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论