【NS-3学习】ns3-模拟基础:关键概念,日志,命令行参数

前言

本篇博客先介绍在仿真过程当中会使用到的一些关键概念,而后介绍便于调试仿真脚本的经常使用技术:日志、命令行参数。linux

关键概念

节点git


在因特网术语中,主机(终端)是指任何一台链接到网络的计算设备。ns-3并不是一个专门的因特网模拟器,而是一个网络模拟器。为此不采用术语“主机”,由于这个词太容易让人联想到因特网以及相关协议。所以,选用其余术语:节点。github

能够将节点理解为一个空的机箱,咱们能够向其中添加各类功能,将其变成咱们想要的主机或者交换机。json

节点由C++中的Node类来描述。Node类提供了用于管理仿真器网络组件表示的各类方法。例如NodeContainer类,用于管理一组节点指针。服务器

应用网络


在ns-3中,须要被仿真的用户程序被抽象为应用。应用在C++中用Application类来描述。这个类提供了管理仿真时用户层应用的各类方法。框架

信道函数


一般把网络中数据流流过的媒介称为信道。在ns-3的模拟环境中,能够把节点链接到表明数据交换信道的对象上。在这里,基本的通讯子网这一抽象的概念被称为信道,在C++中用Channel类来描述。学习

Channel类提供了管理通讯子网对象和把节点链接至他们的各类方法。信道类一样能够由开发者以面向对象的方法自定义。一个信道实例能够模拟一条简单的线缆(wire),也能够是一个复杂的巨型以太网交换机,甚至是无线网络中充满障碍物的三维空间。ui

我曾用到的ns-3仿真的两种信道模型:CsmaChannelPointToPointChannel
CsmaChannel信道模拟了一个能够用于实现载波侦听多路访问通讯子网中的媒介,这个信道具备和以太网类似的功能。
PointToPointChannel这个类表明一个简单的点对点信道,此信道没有多点通讯能力,能够最多2个点到点链接的网络设备。

网络设备


在Unix(或者linux)系统中,外围硬件被称为“设备”。设备经过驱动程序来控制,而网卡经过网卡驱动程序来控制。在Unix系统中,网卡被称为像eth0这样的名字。在ns-3中,网络设备这一抽象概念至关于硬件设备和软件驱动的总和。

ns-3仿真环境中,网络设备安装在节点上,使得节点经过信道和其余节点通讯。与真实的计算机同样,一个节点能够经过多个网络设备同时链接到多条信道上。

网络设备由C++中的NetDevice类来描述。NetDevice提供了管理链接其余节点和信道对象的各类方法,而且容许开发者以面向对象的方法来自定义。

拓扑帮助


不少模块都有帮助类来帮助咱们快速构建仿真程序,相似于帮助咱们快速配置应用程序的BulkSendHelper。要好好利用帮助类来帮咱们编写仿真脚本。

附图一张:ns-3仿真中两个节点通讯所要经历的模块。

日志

在不少大型系统中,都会提供一种基于控制台的消息记录模块,用来向用户及时反馈命令的执行状况或者系统的运行状况,ns-3采用这种机制实现了一种可选的、多等级的消息记录模块——日志系统。在ns-3中,日志系统主要用于提供程序调试的信息或者输出程序中间结果用于验证咱们的思路。

日志的级别

日志系统有7个等级,有低到高依次为:

  1. LOG_ERROR :记录错误信息,程序中使用NS_LOG_ERROR来输出信息;
  2. LOG_WARN :记录警告信息,程序中使用NS_LOG_WARN来输出信息;
  3. LOG_DEBUG :记录一些调试信息,程序中使用NS_LOG_DEBUG来输出信息;
  4. LOG_INFO :记录一些程序相关的信息,程序中使用NS_LOG_INFO来输出信息;
  5. LOG_FUNCTION : 当有函数被调用时,记录该调用信息,程序中使用NS_LOG_FUNCTION来输出信息;
  6. LOG_LOGIC :记录程序中执行流程的信息,程序中使用NS_LOG_LOGIC来输出信息;
  7. LOG_ALL :包含上述全部信息,显示任何级别的日志。

还有一种特殊的级别须要注意,NS_LOG_UNCOND,这种输出方式不会绑定到上述任何一个级别当中,只要所属组件的日志开启,不论当前显示哪一个级别,都将输出日志信息。

日志的定义和使用

想要输出日志,首先要定义一个日志组件:

NS_LOG_COMPONENT_DEFINE("MyTestLoggingComponent");

注意,日志组件的名字不要重复,通常加上前缀。

而后是启用日志,启用日志有不少方式。这里介绍一种简单的,即在代码中启用。语法以下:

LogComponentEnable("MyTestLoggingComponent", LOG_INFO);

这样就启用了MyTestLoggingComponent组件当中的INFO级别的日志。

下面是使用日志的一个示例:

#include "ns3/core-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("MyTestLoggingComponent");

int 
main (int argc, char *argv[])
{
    LogComponentEnable("MyTestLoggingComponent", LOG_INFO);
    
    NS_LOG_ERROR("test error");
    NS_LOG_WARN("test warn");
    NS_LOG_DEBUG("test debug");
    NS_LOG_INFO("test info");
    NS_LOG_FUNCTION("test function");
    NS_LOG_LOGIC("test logic");
    NS_LOG_UNCOND("test uncond");
}
sakura@sakura-pc:ns3-load-balance-origin$ ./waf --run "testlog"
Waf: Entering directory `/home/sakura/Application/ns3-load-balance-origin/build'
[ 985/2302] Compiling scratch/testlog.cc
[ 986/2302] Compiling scratch/scratch-simulator.cc
[ 987/2302] Compiling scratch/MyTest.cc
[ 988/2302] Compiling scratch/hidden-terminal.cc
[2279/2302] Linking build/scratch/testlog
[2287/2302] Linking build/scratch/scratch-simulator
[2290/2302] Linking build/scratch/hidden-terminal
[2291/2302] Linking build/scratch/MyTest
Waf: Leaving directory `/home/sakura/Application/ns3-load-balance-origin/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (23.377s)
test info
test uncond

从输出能够看出,咱们启用了LOG_INFO级别的日志,输出INFO级别的日志内容和只有开启日志就能够输出的UNCOND信息。

若是想禁用全部日志输出,能够不启用日志或者使用LOG_NONE。

LogComponentEnable("MyTestLoggingComponent", LOG_NONE);

输出:

sakura@sakura-pc:ns3-load-balance-origin$ ./waf --run "testlog"
Waf: Entering directory `/home/sakura/Application/ns3-load-balance-origin/build'
[ 986/2302] Compiling scratch/testlog.cc
[2287/2302] Linking build/scratch/testlog
Waf: Leaving directory `/home/sakura/Application/ns3-load-balance-origin/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (2.119s)
test uncond

可是须要注意,NS_LOG_UNCOND是依旧会输出信息的,除非注释掉日志启用那一行代码。

累积日志级别

以上的LOG_TYPE都是输出单一级别的日志信息,咱们还能够输出LOG_LEVEL_INFO或者LOG_LEVEL_DEBUG这样的累积日志级别。
其累计原则是,高等级的级别会使比其等级低的日志都输出。如LOG_LEVEL_INFO除了输出INFO信息,还会输出DEBUG、WARN、ERROR信息。

将启动日志的行改成:

LogComponentEnable("MyTestLoggingComponent", LOG_LEVEL_INFO);

输出:

sakura@sakura-pc:ns3-load-balance-origin$ ./waf --run "testlog"
Waf: Entering directory `/home/sakura/Application/ns3-load-balance-origin/build'
[ 985/2302] Compiling scratch/testlog.cc
[2291/2302] Linking build/scratch/testlog
Waf: Leaving directory `/home/sakura/Application/ns3-load-balance-origin/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (2.207s)
test error
test warn
test debug
test info
test uncond

日志前缀

ns-3还提供了日志前缀,来控制显示日志的来源,从而更加方便咱们进行调试。ns-3中可用的前缀有以下几种:

  • LOG_PREFIX_FUNC:输出日志所在的函数
  • LOG_PREFIX_TIME:输出日志产生的时间(注意此时间是仿真时间,若仿真没有开始则不会有任何输出)
  • LOG_PREFIX_NODE:输出日志所在的节点编号
  • LOG_PREFIX_LEVEL:输出日志的级别
  • LOG_PREFIX_ALL:输出以上全部信息

可使用LogComponentEnable和级别叠加使用,例如:

LogComponentEnable("MyTestLoggingComponent", LOG_LEVEL_INFO);
LogComponentEnable("MyTestLoggingComponent", LOG_PREFIX_LEVEL);

完整程序以下:

#include "ns3/core-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("MyTestLoggingComponent");

int 
main (int argc, char *argv[])
{
    LogComponentEnable("MyTestLoggingComponent", LOG_LEVEL_INFO);
    LogComponentEnable("MyTestLoggingComponent", LOG_PREFIX_LEVEL);
    
    NS_LOG_ERROR("test error");
    NS_LOG_WARN("test warn");
    NS_LOG_DEBUG("test debug");
    NS_LOG_INFO("test info");
    NS_LOG_FUNCTION("test function");
    NS_LOG_LOGIC("test logic");
    NS_LOG_UNCOND("test uncond");
}
sakura@sakura-pc:ns3-load-balance-origin$ ./waf --run "testlog"
Waf: Entering directory `/home/sakura/Application/ns3-load-balance-origin/build'
[ 985/2302] Compiling scratch/testlog.cc
[2281/2302] Linking build/scratch/testlog
Waf: Leaving directory `/home/sakura/Application/ns3-load-balance-origin/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (2.142s)
[ERROR] test error
[WARN ] test warn
[DEBUG] test debug
[INFO ] test info
test uncond

命令行参数

ns-3提供了一种不须要重修编辑和构建脚本就能够改变脚本运行行为的方法。这种方法就是经过命令行传递参数来改变脚本中的变量。

在使用命令行系统时,首先要声明一个命令行类的对象,而后调用函数Parse。以下:

int main(int argc, char* argv[])
{
    ...
    CommandLine cmd;
    cmd.Parse(argc, argv);
    ...
}

这两行代码表明,如今用户能够命令行来访问代码中的变量和ns-3中的属性系统

咱们在使用./waf 运行脚本使,能够加上参数--PrintHelp来显示可使用的参数。
例如,咱们运行代码中examples/tutorial/first.cc。(这里将first.cc复制到了scratch目录下)

root@iZuf6c029wyeq0gfy84vcuZ:~/Applications/ns-allinone-3.29/ns-3.29# ./waf --run "scratch/first --PrintHelp"
Waf: Entering directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Waf: Leaving directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (16.088s)
first [General Arguments]


General Arguments:
    --PrintGlobals:              Print the list of globals.
    --PrintGroups:               Print the list of groups.
    --PrintGroup=[group]:        Print all TypeIds of group.
    --PrintTypeIds:              Print all TypeIds.
    --PrintAttributes=[typeid]:  Print all attributes of typeid.
    --PrintHelp:                 Print this help message.

参数--PrintAttributes的功能就是挂载用户想要修改的属性系统的属性名
在first.cc脚本中,以下几行代码就使用到了属性系统,即配置属性系统中的一些属性的值。

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

脚本中使用到的类是PointToPoint类,经过命令行显示该类在使用中所定义的默认值,在操做过程当中是使用已经和网络设备绑定的类PointToPointNetDevice中所涉及的属性的默认值:

root@iZuf6c029wyeq0gfy84vcuZ:~/Applications/ns-allinone-3.29/ns-3.29# ./waf --run "scratch/first --PrintAttributes=ns3::PointToPointNetDevice"
Waf: Entering directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Waf: Leaving directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (16.326s)
Attributes for TypeId ns3::PointToPointNetDevice
    --ns3::PointToPointNetDevice::Address=[ff:ff:ff:ff:ff:ff]
        The MAC address of this device.
    --ns3::PointToPointNetDevice::DataRate=[32768bps]
        The default data rate for point to point links
    --ns3::PointToPointNetDevice::InterframeGap=[+0.0ns]
        The time to wait between packet (frame) transmissions
    --ns3::PointToPointNetDevice::Mtu=[1500]
        The MAC-level Maximum Transmission Unit
    --ns3::PointToPointNetDevice::ReceiveErrorModel=[0]
        The receiver error model used to simulate packet loss
    --ns3::PointToPointNetDevice::TxQueue=[0]
        A queue to use as the transmit queue in the device.

这里显示默认值为32768bps,咱们在脚本文件中定义的5Mbit/s会在实际运行时覆盖该默认值。咱们比较一下使用自定义值(5Mbit/s)和默认值的运行结果。
使用自定义值:

root@iZuf6c029wyeq0gfy84vcuZ:~/Applications/ns-allinone-3.29/ns-3.29# ./waf --run "scratch/first"
Waf: Entering directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Waf: Leaving directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (16.072s)
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

使用默认值:

root@iZuf6c029wyeq0gfy84vcuZ:~/Applications/ns-allinone-3.29/ns-3.29# ./waf --run "scratch/first"
Waf: Entering directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
[2558/2609] Compiling scratch/first.cc
[2569/2609] Linking build/scratch/first
Waf: Leaving directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (19.405s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.25932s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.25932s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.51865s client received 1024 bytes from 10.1.1.2 port 9

因为客户端应用程序的开始运行时间没有改变,所以咱们能够发现客户端发送数据的时间仍是在2s时刻。而后,因为把数据的发送速率从5Mbit/s降到了32768bit/s,因此服务器接收到数据的时间相应的推迟了一段时间。

咱们除了能够修改应用程序的数据发送速率,还能够修改其余的属性变量,好比延迟、最大分组发送数量等。
接下来咱们就看看如何经过命令行来修改这些属性值。

挂钩自定义变量

在ns-3中咱们能够添加本身的变量,而后经过挂钩将其他命令行相关联。
以first.cc中下面这句代码为例讲解:

echoClient.SetAttribute ("MaxPackets", UintegerValue (1));

这句话限制了MaxPackets属性是固定的,若是要将这句话改为在命令行编译脚本时自定义,那么作如下改动:
在first.cc开头

uint32_t nPackets = 1; //这里添加一个变量
CommandLine cmd;
cmd.AddValue("nPackets", "Number of packets to echo", nPackets);
//这上面代码使得变量nPackets在命令行中能够修改
cmd.Parse (argc, argv);

在修改代码:

echoClient.SetAttribute ("MaxPackets", UintegerValue(nPackets));

运行first.cc脚本:

root@iZuf6c029wyeq0gfy84vcuZ:~/Applications/ns-allinone-3.29/ns-3.29# ./waf --run "scratch/first --PrintHelp"
Waf: Entering directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
[2558/2609] Compiling scratch/first.cc
[2569/2609] Linking build/scratch/first
Waf: Leaving directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (19.032s)
first [Program Options] [General Arguments]


Program Options:
    --nPackets:  Number of packets to echo [1]


General Arguments:
    --PrintGlobals:              Print the list of globals.
    --PrintGroups:               Print the list of groups.
    --PrintGroup=[group]:        Print all TypeIds of group.
    --PrintTypeIds:              Print all TypeIds.
    --PrintAttributes=[typeid]:  Print all attributes of typeid.
    --PrintHelp:                 Print this help message.

尝试修改这个参数的值:

root@iZuf6c029wyeq0gfy84vcuZ:~/Applications/ns-allinone-3.29/ns-3.29# ./waf --run "scratch/first --nPackets=2"
Waf: Entering directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Waf: Leaving directory `/root/Applications/ns-allinone-3.29/ns-3.29/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (16.033s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.25932s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.25932s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.51865s client received 1024 bytes from 10.1.1.2 port 9
At time 3s client sent 1024 bytes to 10.1.1.2 port 9
At time 3.25932s server received 1024 bytes from 10.1.1.1 port 49153
At time 3.25932s server sent 1024 bytes to 10.1.1.1 port 49153
At time 3.51865s client received 1024 bytes from 10.1.1.2 port 9

与以前的结果比较能够发现,客户端发送了两次。参数修改为功!

小结

本篇文章又零零散散地继续介绍了NS-3的相关知识。重点介绍了日志系统,日志的输出对于咱们调试仿真脚本是十分重要的,要好好掌握。日志中还有一些部分没有提例如函很多天志的详细使用和启用日志组件的其余方式,这些均可以参考[2]。NS-3中还有一个对咱们分析脚本很是重要的功能,即追踪框架(可参考[3])。这个跟踪某些事件的发生,帮助咱们调试脚本。NS3的知识暂且介绍这些。一是后面没有那么多的时间让我来写这些学习笔记,二是附着的参考博客连接能够是说很是好的学习资源(我老师写的固然好( ̄^ ̄)ゞ,这令文笔拙劣的我怎么还敢写下去)。关于NS-3有任何的疑问,各位看官能够私聊我,咱们共同进步!

参考: [1]马春光.姚建盛.ns-3网络模拟器基础及应用[M].北京:人民邮电出版社,2014 [2]NS-3学习笔记(二):NS-3的日志系统.http://rainsia.github.io/2018/03/30/ns3-002/ [3]NS-3学习笔记(九):NS-3的对象框架 之 追踪框架.http://rainsia.github.io/2018/10/22/ns3-009/

相关文章
相关标签/搜索