网络模拟器 ns-3

Posted on Thu 06 January 2022 in Journal

Overview

NS-3 is a network simulator tool

  • An open source discrete event simulator
  • Event model packet transmission , receipt , timers etc.
  • Future events maintained in sorted Event List
  • Processing events results in zero or more new events
  • Written in C++
  • Extensive use of Templates , Smart Pointers, Callbacks C++ namespace (ns3)

  • Simulation programs are C++ executables

  • Python is used to bind public APIs provided

  • NS-3 is built as a library which may be linked to a C++ main program defines the simulation topology and start the simulation.

安装 NS-3

git clone https://gitlab.com/nsnam/ns-3-allinone.git
cd ns-3-allinone
./download.py
#./build.py --enable-examples --enable-tests

推荐用 waf

cd ns-3-dev
./waf clean
./waf configure --build-profile=debug --disable-werror --enable-examples --enable-tests
./waf build
./waf --run hello-simulator

基本概念

ns3

  • Nodes: hosts, routers, servers,… 节点, 可由 NodeContainer 来管理
  • Applications: generate and consume traffic in network
  • Protocols: broker connections, access, addressing, routing,…
  • NetDevices: e.g., ethernet & wireless cards
  • Channels: transmission medium (cable, EM waves,…)
  • Packets: make up network traffic

第一个例子

ns3

// Default Network Topology
//
//       10.1.1.0
// n0 -------------- n1
//    point-to-point
//

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
  //解析命令行
  CommandLine cmd (__FILE__);
  cmd.Parse (argc, argv);

  Time::SetResolution (Time::NS);
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);



  //创建两个节点

  NodeContainer nodes;
  nodes.Create (2);



  //设置端到端拓扑网络结构,设备速率为5Mbps, 通道延迟为 2ms
  //网络拓扑中安装这些节点


  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);


  // 互联网协议栈中安装这些节点

  InternetStackHelper stack;
  stack.Install (nodes);

  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);


  // 创建一个 UdpEchoServer
  UdpEchoServerHelper echoServer (9);


  // UdpEchoServer 应用里安装 Server 节点,1秒后启动,10秒后停止
  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  // 创建一个 UdpEchoClient, 设置 MaxPackets=1, Interval=1 秒, PacketSize=1024
  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));



  // UdpEchoClient 应用里安装 Client 节点, 2 秒后启动, 10 秒后停止

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  // 模拟器运行,之后销毁
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

运行这个例子

cp examples/tutorial/first.cc scratch/myfirst.cc
./waf
./waf –run scratch/myfirst

输出如下:

At time +2s client sent 1024 bytes to 10.1.1.2 port 9
At time +2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time +2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time +2.00737s client received 1024 bytes from 10.1.1.2 port 9

为避免新版本的 gcc 编译错误,需要改一下 CXXFLAGS

 CXXFLAGS="-Wall" ./waf configure
 ./waf -vv