NS3网络仿真

  • NS3安装与配置
  • 点对点通信
  • 交换机(ARP协议)
  • 其他分析文件

1. 安装与配置

参考网址:维基百科NS3配置

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

  • cd

  • mkdir repos

  • cd repos

  • hg clone http://code.nsnam.org/ns-3-allinone

当执行完以上命令后,你会看到如下内容

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.

NS3网络仿真_第1张图片

进入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文件,点击运行即可。

NS3网络仿真_第2张图片

2. 点对点通信
2.1网络拓扑

NS3网络仿真_第3张图片

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执行结果

NS3网络仿真_第4张图片

3.交换机(ARP协议仿真)
3.1网络拓扑

NS3网络仿真_第5张图片

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 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实验结果

第一次发送时,网桥不知道发送给哪个端口,所以进行广播;
之后地址表建立,便不再进行广播。

NS3网络仿真_第6张图片

NS3网络仿真_第7张图片

4.其他分析文件

4.1Pcap文件
Wireshark:可以看到每个结点接收发送数据的信息:

NS3网络仿真_第8张图片

4.2 tr 跟踪文件

NS3网络仿真_第9张图片

每一笔记录的开始字段都是封包事件发生的原因:
r表示这个封包被某个节点接受,+表示进入队列,-表示离开队列,d表示封包被队列丢弃;

你可能感兴趣的:(计算机网络)