网络模拟器教程ns-3。第四章


第1.2
章第3章


4概念概述
4.1关键抽象
4.1.1节点
4.1.2应用
4.1.3通道
4.1.4网络设备
4.1.5拓扑助手
4.2第一个脚本ns-3
4.2.1样板代码
4.2.2插件
4.2.3 ns3名称空间
4.2.4记录
4.2.5主要功能
4.2.6使用拓扑助手
4.2.7使用应用程序
4.2.8模拟器
4.2.9构建脚本
4.3 ns-3源代码


第四章


概念概述


, , ns‑3 — . , , , , , .


4.1


, , ns‑3.


4.1.1 Node ()


- , , . , ns‑3 — , , , . , , — (node).


ns-3 . C++ Node. NodeNode () .


Node , . , , , . ns-3.


4.1.2 Application ()


, . , , , . . . , . , , .


, . ns‑3 . , , . « » , ns‑3 ns‑3 .


ns‑3 , , . C++ Application (). Application . , Application - . Application, UdpEchoClientApplication UdpEchoServerApplication. , / ‑ .


4.1.3 Channel ()


. , , . Ethernet , Ethernet. ns‑3 , . C++ Channel ().


ChannelChannel . - . - . Ethernet- , .


CsmaChannelCsmaChannel, PointToPointChannelPointToPointChannel WifiChannelWifiChannel. CsmaChannel, , . Ethernet- .


4.1.4 Net Device ( )


, , , ( ) , . , . , .


, . Unix ( Linux), device. (device drivers), (NIC) (network device drivers) (net devices). Unix Linux , eth0.


ns‑3 , . «» , . , NetDevices.


C++ NetDevice. NetDevice Node Channel; - . NetDevice CsmaNetDevice, PointToPointNetDevice WifiNetDevice. , Ethernet Ethernet, CsmaNetDevice CsmaChannel, PointToPointNetDevice PointToPointChannel, WifiNetDeviceWifiChannel.


4.1.5


- ( ) . ns‑3 , NetDevices. Node, NetDevice Channel.


NetDevices , NetDevices , IP- .. ns‑3 , , . , NetDevice ns‑3, MAC-, Node, , NetDevice Channel. , , (Internetworks). , .


4.2 ns-3


, , ns‑3 repos . release


, ns‑3 , :
$ ./waf configure --build-profile=release --out=build/release,
$ ./waf build

:


AUTHORS       examples      scratch       utils       waf.bat*
bindings      LICENSE       src           utils.py    waf-tools
build         ns3           test.py*      utils.pyc   wscript
CHANGES.html  README        testpy-output VERSION     wutils.py
doc           RELEASE_NOTES testpy.supp   waf*        wutils.pyc

examples/tutorial. first.cc. , - . , first.cc .


4.2.1 Boilerplate
emacs. emacs ( ), .


/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */

, , . ns‑3, , , . , ns‑3, doc/codingstd.txt - : https://www.nsnam.org/develop/contributing-code/coding-style/.


ns‑3 , . . emacs, , , emacs.


ns‑3 GNU General Public License. GNU ns‑3. ns‑3 GPL , .


/* 
* This program is free software; you can redistribute it and/or modify 
* it under the terms of the GNU General Public License version 2 as 
* published by the Free Software Foundation; 
*
* This program is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details. 
* 
* You should have received a copy of the GNU General Public License 
* along with this program; if not, write to the Free Software 
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

4.2.2


(include).


#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

, , . , , . , , , , , . , , , .


ns‑3 ns3 ( ), . ns3/core-module.h ns‑3, src/core . . , Waf ns3 build/debug


, ns‑3 , :
$ ./waf configure --build-profile=debug --out=build/debug
$ ./waf build

$ ./waf configure --build-profile=optimized --out=build/optimized
$ ./waf build

build/optimized, . Waf . , , ,


$ ./waf -d debug --enable-examples --enable-tests configure

, .


$ ./waf

. , ../../build/debug/ns3, , , , . , .


4.2.3 ns3


first.cc — .


using namespace ns3;

ns‑3 C++, ns3. ns‑3- , , , . C++ ns‑3 () . , , ns3 :: scope ns‑3, . , C++ ns3 std using namespace std; cout .


4.2.4


,


NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

Doxygen. - ns‑3, «» (Documentation) . , . « », ns‑3. «API Documentation», API ns‑3.


. — «» Modules ns‑3 ns‑3. Modules, ns‑3. , . ( , ) ns‑3 , , , Core, Debugging tools, Logging. Logging.


Doxygen Logging. NS_LOG_COMPONENT_DEFINE. , « » , . «More...» .


, , NS_LOG_COMPONENT_DEFINE. , , , FirstScriptExample, .


4.2.5


,


int 
main (int argc, char *argv[])
{ 

(). C++, , . . ns‑3 C++ . 1 , :


Time::SetResolution (Time::NS);

— , ( ). . , , , , , , . ( , , .)


, EchoClient EchoServer:


LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);

Logging, , /, . INFO - . .


. , .


4.2.6


Node ns‑3, .


NodeContainer nodes;
nodes.Create (2);

, NodeContainer. Classes Doxygen. Doxygen, Classes. , . ns‑3. , ns3 :: NodeContainer. , , .


, . , , , . NodeContainer , Node, . NodeContainer, nodes. Create nodes . Doxygen, ns‑3 Node .


, . . , , — . .


PointToPointHelper


, , , . , NetDevice Channel. . , , , , Ethernet . PointToPointHelper ns‑3 PointToPointNetDevice PointToPointChannel. :


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

,


PointToPointHelper pointToPoint;

PointToPointHelper. ,


pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));

PointToPointHelper «5 /» ( ) «DataRate».


, «DataRate» , PointToPointNetDevice. Doxygen ns3::PointToPointNetDevice GetTypeId , . «DataRate». ns‑3 . , .


«DataRate» PointToPointNetDevice, «Delay», PointToPointChannel. ,


pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

PointToPointHelper «2 » ( ) -, .


NetDeviceContainer


NodeContainer, . PointToPointHelper, PointToPointNetDevices PointToPointChannel. , NodeContainer, PointToPointHelper , , . NetDevice, NetDeviceContainer , NodeContainer . ,


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

. , , . Install PointToPointHelper NodeContainer . NetDeviceContainer NodeContainer ( - ) PointToPointNetDevice . PointToPointChannel , PointToPointNetDevices. , PointToPointHelper, .


pointToPoint.Install (nodes) , «-» «-» . .


InternetStackHelper


, . .


InternetStackHelper stack;
stack.Install (nodes);

InternetStackHelper — -, PointToPointHelper . Install NodeContainer . - (TCP, UDP, IP . .) .


Ipv4AddressHelper


IP-. IP-. API — IP- ( ). first.cc,


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

, IP- 10.1.1.0, 255.255.255.0. , , , 10.1.1.1, 10.1.1.2 .. , ns‑3 IP- , (, ).


,


Ipv4InterfaceContainer interfaces = address.Assign (devices);

. ns‑3 IP- , Ipv4Interface. , , , Ipv4Interface. Ipv4InterfaceContainer .


-, IP-. .


4.2.7 Application


ns‑3 — Application (). Application ns‑3 UdpEchoServerApplication UdpEchoClientApplication. , . UdpEchoServerHelper UdpEchoClientHelper , .


UdpEchoServerHelper


first.cc, UDP , .


UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoServerHelper. , , , . — (). , , . . , , SetAttribute . , , SetAttribute «».


, UdpEchoServerHelper Install. , , - . , Install NodeContainter , Install , .


C++, , node.Get (1) ( — Ptr ) NodeContainer, Install. C++ , , .

, echoServer.Install UdpEchoServerApplication NodeContainer, , 1. Install , ( , NodeContainer, ) .


«start» , «stop». . ApplicationContainer Start Stop. Time. C++, C++ double 1.0 ns‑3 Time, Seconds . , , C++ , , , . ,


serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

, - ( ) () . , ( ), , .


UdpEchoClientHelper


echo , . UdpEchoClientApplication,
UdpEchoClientHelper.


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));;

- . UdpEchoClientHelper. , ( ) «RemoteAddress» «RemotePort» , .


, Ipv4InterfaceContainer IP-, . IP- . IP- . , () , IP-, , . , .


«MaxPackets» , . «Interval» , , «PacketSize» , . 1024- .


-, - Start Stop, ( ).


4.2.8


. Simulator::Run.


Simulator::Run ();

,


serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
... 
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

1,0 , 2,0 10,0 . Simulator::Run, . 1,0 , - ( , , ). , t = 2,0 , -. , . - , .


, - , .


, (, MaxPackets ), - , . , Stop . , Simulator::Run . .


. Simulator::Destroy. ( ns‑3), , , . - — , Simulator::Destroy . ns‑3 . ns‑3 , first.cc, :


Simulator::Destroy ();
return 0;
}

?


ns‑3 — (DE). , . (, , ).


, , IPv6 , .. .. , , . , , . , Stop. Stop Simulator::Stop ( ).


, Simulator::Stop : . ( ) — , . , . , , :


• FlowMonitor — ;


• RIPng — ;


• ..


Simulator::Stop . , ns‑3 , RealtimeSimulator , , Simulator::Stop .


Simulator::Stop , . Simulator::Stop. , 11 :


+ Simulator::Stop (Seconds (11.0));
  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

, 10 . 11 1 , , , ( 2 ).


Simulator::Stop Simulator::Run; Simulator::Run !


4.2.9


. , , scratch, , Waf. . examples/tutorial/first.cc scratch


$ cd ../.. 
$ cp examples/tutorial/first.cc scratch/myfirst.cc

, waf:


$ ./waf

, .


Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
[614/708] cxx: scratch/myfirst.cc -> build/debug/scratch/myfirst_3.o
[706/708] cxx_link: build/debug/scratch/myfirst_3.o -> build/debug/scratch/myfirst
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
'build' finished successfully (2.357s)

( , scratch, scratch):


$ ./waf --run scratch/myfirst

:


Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build'
'build' finished successfully (0.418s) Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
Received 1024 bytes from 10.1.1.2

, , , . - , 1024- - 10.1.1.2. . -, , 1024 10.1.1.1. - , -, .


4.3 ns-3


, ns‑3, , . - : https://gitlab.com/nsnam/ns-3-dev.git. Mercurial ns‑3. ,


summary | shortlog | changelog | graph | tags | files

. :


drwxr-xr-x                               [up]
drwxr-xr-x                               bindings python  files
drwxr-xr-x                               doc              files
drwxr-xr-x                               examples         files
drwxr-xr-x                               ns3              files
drwxr-xr-x                               scratch          files
drwxr-xr-x                               src              files
drwxr-xr-x                               utils            files
-rw-r--r-- 2009-07-01 12:47 +0200 560    .hgignore        file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 1886   .hgtags          file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 1276   AUTHORS          file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 30961  CHANGES.html     file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 17987  LICENSE          file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 3742   README           file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 16171  RELEASE_NOTES    file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 6      VERSION          file | revisions | annotate
-rwxr-xr-x 2009-07-01 12:47 +0200 88110  waf              file | revisions | annotate
-rwxr-xr-x 2009-07-01 12:47 +0200 28     waf.bat          file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 35395  wscript          file | revisions | annotate
-rw-r--r-- 2009-07-01 12:47 +0200 7673   wutils.py        file | revisions | annotate

examples. , . tutorial — first.cc. first.cc , .


src. , . src, src. core, . , ( ) — abort.h. abort.h, abort.h, , . , , src/Applications/helper随意浏览目录树,看看在哪里可以确定ns‑3程序的样式。


All Articles