计算机网络原理 实验二 《NS3路由模拟实验》

实验二

前期准备

  1. 安装相关依赖
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 //基于GTK的配置系统

sudo apt-get install gdb valgrind //调试工具

sudo apt-get install doxygen graphviz imagemagick  //文档生成器

sudo apt-get install texlive texlive-latex-extra //文档生成器,从源代码中生成说明文档     

sudo apt-get install texinfo dia texlive-extra-utils texi2html //ns-3手册和tutorial编写查看工具

sudo apt-get install flex bison //仿真必需的词法分析器和语法分析生成器,必装

sudo apt-get install libgoocanvas-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 //Gustavo's ns-3-pyviz的可视化软件

sudo apt-get install qt4-qmake

sudo apt-get install qt4-dev-tools

  1. 问题:有部分包用apt-get搜索不到,可能是仓库里删除了,不过最后运行可视化程序也没出错
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libgoocanvas-dev
  1. 结果:大部分包安装成功

安装编译ns3

  1. 下载ns3下载程序

    在home目录下新建tarballs目录;

    用hg命令下载tar.bz文件;

    解压缩;

mkdir tarballs

cd tarballs

wget http://www.nsnam.org/release/ns-allinone-3.28.tar.bz2

tar xjf ns-allinone-3.28.tar.bz2

  1. 下载ns3主程序and编译

    进入ns3目录;

    执行下载程序;

    清除整个build目录;

    打开debug并开启例子及帮助;

cd ns-allinone-3.28

./build.py 

cd ns-3.28

./waf distclean

./waf configure --build-profile=debug --enable-examples --enable-tests

./waf build 

编程

  1. 编写cpp文件

    文件目录:ns3/scratch/ns3route.cc

//ns3routing.cc
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <cassert>
 #include "ns3/core-module.h"
 #include "ns3/network-module.h"
 #include "ns3/csma-module.h"
 #include "ns3/internet-module.h"
 #include "ns3/point-to-point-module.h"
 #include "ns3/applications-module.h"
 #include "ns3/ipv4-global-routing-helper.h"
 #include "ns3/netanim-module.h"
 using namespace ns3;
 
 NS_LOG_COMPONENT_DEFINE ("DynamicGlobalRoutingExample");
 
 int 
 main (int argc, char *argv[])
 {
   // 配置全局路由算法的参数。The below value configures the default behavior of global routing.
   // 默认情况下是禁用的,相应借口事件时候,设为true
   Config::SetDefault ("ns3::Ipv4GlobalRouting::RespondToInterfaceEvents", BooleanValue (true));
 

   CommandLine cmd;
   cmd.Parse (argc, argv);
   //创建节点
   NS_LOG_INFO ("Create nodes.");
   NodeContainer c;
   c.Create (7);
   NodeContainer n0n2 = NodeContainer (c.Get (0), c.Get (2));
   NodeContainer n1n2 = NodeContainer (c.Get (1), c.Get (2));
   NodeContainer n5n6 = NodeContainer (c.Get (5), c.Get (6));
   NodeContainer n1n6 = NodeContainer (c.Get (1), c.Get (6));
   NodeContainer n2345 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
 
   InternetStackHelper internet;
   internet.Install (c);
 
   // 创建第一次没有任何ip寻址信息的信道
   NS_LOG_INFO ("Create channels.");
   PointToPointHelper p2p;
   p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
   p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
   NetDeviceContainer d0d2 = p2p.Install (n0n2);
   NetDeviceContainer d1d6 = p2p.Install (n1n6);
 
   NetDeviceContainer d1d2 = p2p.Install (n1n2);
 
   p2p.SetDeviceAttribute ("DataRate", StringValue ("1500kbps"));
   p2p.SetChannelAttribute ("Delay", StringValue ("10ms"));
   NetDeviceContainer d5d6 = p2p.Install (n5n6);
 
   // 创建第一次没有任何ip寻址信息的信道
   CsmaHelper csma;
   csma.SetChannelAttribute ("DataRate", StringValue ("5Mbps"));
   csma.SetChannelAttribute ("Delay", StringValue ("2ms"));
   NetDeviceContainer d2345 = csma.Install (n2345);
 
   // 添加ip地址
   NS_LOG_INFO ("Assign IP Addresses.");
   Ipv4AddressHelper ipv4;
   ipv4.SetBase ("10.1.1.0", "255.255.255.0");
   ipv4.Assign (d0d2);
 
   ipv4.SetBase ("10.1.2.0", "255.255.255.0");
   ipv4.Assign (d1d2);
 
   ipv4.SetBase ("10.1.3.0", "255.255.255.0");
   Ipv4InterfaceContainer i5i6 = ipv4.Assign (d5d6);
 
   ipv4.SetBase ("10.250.1.0", "255.255.255.0");
   ipv4.Assign (d2345);
 
   ipv4.SetBase ("172.16.1.0", "255.255.255.0");
   Ipv4InterfaceContainer i1i6 = ipv4.Assign (d1d6);
 
   // 创建路由器节点,初始化路由选择数据库,设置路由表节点。
   Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
 
   // 创建时断时续的发送数据报应用。
   NS_LOG_INFO ("Create Applications.");
   uint16_t port = 9;   // Discard port (RFC 863)
   OnOffHelper onoff ("ns3::UdpSocketFactory",
                      InetSocketAddress (i5i6.GetAddress (1), port));
   onoff.SetConstantRate (DataRate ("2kbps"));
   onoff.SetAttribute ("PacketSize", UintegerValue (50));
 
   ApplicationContainer apps = onoff.Install (c.Get (1));
   apps.Start (Seconds (1.0));
   apps.Stop (Seconds (10.0));
 
   OnOffHelper onoff2 ("ns3::UdpSocketFactory",
                       InetSocketAddress (i1i6.GetAddress (1), port));
   onoff2.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
   onoff2.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
   onoff2.SetAttribute ("DataRate", StringValue ("2kbps"));
   onoff2.SetAttribute ("PacketSize", UintegerValue (50));
 
   ApplicationContainer apps2 = onoff2.Install (c.Get (1));
   apps2.Start (Seconds (11.0));
   apps2.Stop (Seconds (16.0));
 
   // 接收数据包
   PacketSinkHelper sink ("ns3::UdpSocketFactory",
                          Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
   apps = sink.Install (c.Get (6));
   apps.Start (Seconds (1.0));
   apps.Stop (Seconds (10.0));
 
   PacketSinkHelper sink2 ("ns3::UdpSocketFactory",
                           Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
   apps2 = sink2.Install (c.Get (6));
   apps2.Start (Seconds (11.0));
   apps2.Stop (Seconds (16.0));
 
 
   AsciiTraceHelper ascii;
   Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("dynamic-global-routing.tr");
   p2p.EnableAsciiAll (stream);
   csma.EnableAsciiAll (stream);
   internet.EnableAsciiIpv4All (stream);
 
   p2p.EnablePcapAll ("dynamic-global-routing");
   csma.EnablePcapAll ("dynamic-global-routing", false);
  
   Ptr<Node> n1 = c.Get (1);
   Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
   
   uint32_t ipv4ifIndex1 = 2;
 
   Simulator::Schedule (Seconds (2),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
   Simulator::Schedule (Seconds (4),&Ipv4::SetUp,ipv41, ipv4ifIndex1);
 
   Ptr<Node> n6 = c.Get (6);
   Ptr<Ipv4> ipv46 = n6->GetObject<Ipv4> ();
   
   uint32_t ipv4ifIndex6 = 2;
   Simulator::Schedule (Seconds (6),&Ipv4::SetDown,ipv46, ipv4ifIndex6);
   Simulator::Schedule (Seconds (8),&Ipv4::SetUp,ipv46, ipv4ifIndex6);
 
   Simulator::Schedule (Seconds (12),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
   Simulator::Schedule (Seconds (14),&Ipv4::SetUp,ipv41, ipv4ifIndex1);
 
   // 查探路由表
   Ipv4GlobalRoutingHelper g;
   Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("dynamic-global-routing.routes", std::ios::out);
   g.PrintRoutingTableAllAt (Seconds (12), routingStream);
   //设置生成xml文件,并确定各个节点的位置
   AnimationInterface animsd("ns3routing.xml");
   animsd.SetConstantPosition(c.Get(0),0,0);
   animsd.SetConstantPosition(c.Get(1),0,40);
   animsd.SetConstantPosition(c.Get(2),20,20);
   animsd.SetConstantPosition(c.Get(3),80,20);
   animsd.SetConstantPosition(c.Get(4),40,20);
   animsd.SetConstantPosition(c.Get(5),60,20);
   animsd.SetConstantPosition(c.Get(6),60,40);

//开始仿真
   NS_LOG_INFO ("Run Simulation.");
   Simulator::Run ();
   Simulator::Destroy ();
   NS_LOG_INFO ("Done.");
 }

  1. 编译
sheli@ubuntu:/home/tarballs/ns-allinone-3.28/ns-3.28$ sudo ./waf
Waf: Entering directory `/home/tarballs/ns-allinone-3.28/ns-3.28/build'
[ 935/2709] Compiling scratch/subdir/scratch-simulator-subdir.cc
[ 936/2709] Compiling scratch/scratch-simulator.cc
[2322/2709] Compiling scratch/ns3route.cc
[2656/2709] Linking build/scratch/ns3route
[2672/2709] Linking build/scratch/subdir/subdir
[2692/2709] Linking build/scratch/scratch-simulator

sheli@ubuntu:/home/tarballs/ns-allinone-3.28/ns-3.28$ sudo ./waf --run scratch/ns3route
Waf: Entering directory `/home/tarballs/ns-allinone-3.28/ns-3.28/build'
Waf: Leaving directory `/home/tarballs/ns-allinone-3.28/ns-3.28/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (1.739s)

  1. 执行

    打开NetAnim图形化界面

sheli@ubuntu:/home/tarballs/ns-allinone-3.28/netanim-3.108$ ./NetAnim 

选取xml文件(ns3routing.xml)
NetAnim

实验结果

实验结果

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值