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


第1.2
章第3
章第4章


5设置
5.1使用日志记录模块5.1.1 日志记录
概述
5.1.2启用日志记录
5.1.3在代码中添加日志记录
5.2使用命令行参数
5.2.1覆盖默认属性值
5.2.2捕获自己的命令
5.3使用跟踪系统
5.3。 1 ASCII跟踪
解析ASCII跟踪
5.3.2 PCAP跟踪


第五章


客制化


5.1使用日志记录模块


我们已经通过查看first.cc脚本简要回顾了ns‑ 3日志记录模块在本章中,我们将仔细研究使用日志子系统的可能选项。


5.1.1日志概述


- , ns‑3 . , « » ( stderr Unix) . , , . , , «» .


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


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


() .


  • LOG_ERROR — ( : NS_LOG_ERROR);
  • LOG_WARN — ( : NS_LOG_WARN);
  • LOG_DEBUG — ( : NS_LOG_DEBUG);
  • LOG_INFO — ( : NS_LOG_INFO);
  • LOG_FUNCTION — , ( : NS_LOG_FUNCTION, -, NS_LOG_FUNCTION_NOARGS, );
  • LOG_LOGIC — , ( : NS_LOG_LOGIC);
  • LOG_ALL — ( ).
    (LOG_TYPE) LOG_LEVEL_TYPE, , , . ( , LOG_ERROR LOG_LEVEL_ERROR, LOG_ALL LOG_LEVEL_ALL .) , LOG_INFO NS_LOG_INFO, LOG_LEVEL_INFO , NS_LOG_DEBUG, NS_LOG_WARN NS_LOG_ERROR.

, , .


  • NS_LOG_UNCOND — ( ).

. sh- NS_LOG . , Doxygen- , .


, , , scratch/myfirst.cc, .


5.1.2


NS_LOG, , , , , , ,


$ ./waf --run scratch/myfirst

- ns‑3


$ 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.413s)
Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
Received 1024 bytes from 10.1.1.2

, «» «» , , UdpEchoClientApplication UdpEchoServerApplication. , , , NS_LOG.


, sh- , «VARIABLE = value». csh- , « setenv» .


, UDP - scratch/myfirst.cc,


LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);

LOG_LEVEL_INFO. , . NS_LOG_INFO, NS_LOG_DEBUG, NS_LOG_WARN NS_LOG_ERROR. , , NS_LOG :


$ export NS_LOG=UdpEchoClientApplication=level_all

NS_LOG sh-,


UdpEchoClientApplication=level_all

— , , — , . . NS_LOG, ns‑3 :


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.404s)
UdpEchoClientApplication:UdpEchoClient()
UdpEchoClientApplication:SetDataSize(1024)
UdpEchoClientApplication:StartApplication()
UdpEchoClientApplication:ScheduleTransmit()
UdpEchoClientApplication:Send()
Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
UdpEchoClientApplication:HandleRead(0x6241e0, 0x624a20)
Received 1024 bytes from 10.1.1.2
UdpEchoClientApplication:StopApplication()
UdpEchoClientApplication:DoDispose()
UdpEchoClientApplication:~UdpEchoClient()

, , NS_LOG_FUNCTION. . , - ( )NS_LOG_FUNCTION (this). NS_LOG_FUNCTION_NOARGS ()
. , , ns‑3 - . , , . - .


, . , UdpEchoClientApplication , , C++ (: :). .


, . , , , , . .


, , . , , «Received 1024 bytes from 10.1.1.2». , prefix_func NS_LOG. ,


$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func'

, , , , Unix. , , , , .


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.417s)
UdpEchoClientApplication:UdpEchoClient()
UdpEchoClientApplication:SetDataSize(1024)
UdpEchoClientApplication:StartApplication()
UdpEchoClientApplication:ScheduleTransmit()
UdpEchoClientApplication:Send()
UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
UdpEchoClientApplication:HandleRead(0x6241e0, 0x624a20)
UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2
UdpEchoClientApplication:StopApplication()
UdpEchoClientApplication:DoDispose()
UdpEchoClientApplication:~UdpEchoClient()

, , UDP -, . «Received 1024 bytes from 10.1.1.2» -. UDP -. , , , NS_LOG.


$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func:
               UdpEchoServerApplication=level_all|prefix_func'

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


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.406s)
UdpEchoServerApplication:UdpEchoServer()
UdpEchoClientApplication:UdpEchoClient()
UdpEchoClientApplication:SetDataSize(1024)
UdpEchoServerApplication:StartApplication()
UdpEchoClientApplication:StartApplication()
UdpEchoClientApplication:ScheduleTransmit()
UdpEchoClientApplication:Send()
UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2
UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1
UdpEchoServerApplication:HandleRead(): Echoing packet
UdpEchoClientApplication:HandleRead(0x624920, 0x625160)
UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2
UdpEchoServerApplication:StopApplication()
UdpEchoClientApplication:StopApplication()
UdpEchoClientApplication:DoDispose()
UdpEchoServerApplication:DoDispose()
UdpEchoClientApplication:~UdpEchoClient()
UdpEchoServerApplication:~UdpEchoServer()

, . prefix_time:


$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func|prefix_time: UdpEchoServerApplication=level_all|prefix_func|prefix_time'

, . , :


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)
0s UdpEchoServerApplication:UdpEchoServer()
0s UdpEchoClientApplication:UdpEchoClient()
0s UdpEchoClientApplication:SetDataSize(1024)
1s UdpEchoServerApplication:StartApplication()
2s UdpEchoClientApplication:StartApplication()
2s UdpEchoClientApplication:ScheduleTransmit()
2s UdpEchoClientApplication:Send()
2s UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2
2.00369s UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1
2.00369s UdpEchoServerApplication:HandleRead(): Echoing packet
2.00737s UdpEchoClientApplication:HandleRead(0x624290, 0x624ad0)
2.00737s UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2
10s UdpEchoServerApplication:StopApplication()
10s UdpEchoClientApplication:StopApplication()
UdpEchoClientApplication:DoDispose()
UdpEchoServerApplication:DoDispose()
UdpEchoClientApplication:~UdpEchoClient()
UdpEchoServerApplication:~UdpEchoServer()

, UdpEchoServer 0 . , . UdpEchoClient.


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)
0s UdpEchoServerApplication:UdpEchoServer()
0s UdpEchoClientApplication:UdpEchoClient()
0s UdpEchoClientApplication:SetDataSize(1024)
1s UdpEchoServerApplication:StartApplication()
2s UdpEchoClientApplication:StartApplication()
2s UdpEchoClientApplication:ScheduleTransmit()
2s UdpEchoClientApplication:Send()
2s UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2
2.00369s UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1
2.00369s UdpEchoServerApplication:HandleRead(): Echoing packet
2.00737s UdpEchoClientApplication:HandleRead(0x624290, 0x624ad0)
2.00737s UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2
10s UdpEchoServerApplication:StopApplication()
10s UdpEchoClientApplication:StopApplication()
UdpEchoClientApplication:DoDispose()
UdpEchoServerApplication:DoDispose()
UdpEchoClientApplication:~UdpEchoClient()
UdpEchoServerApplication:~UdpEchoServer()

, scratch/first.cc - . , StartApplication . , - , .


ScheduleTransmit , Send HandleRead -. , 3,69 . , - , , , , , - - HandleRead.


. , . NS_LOG ,


$ export 'NS_LOG=*=level_all|prefix_func|prefix_time'

. . ( 1265 -), ,


$ ./waf --run scratch/myfirst > log.out 2>&1

, , , . , . , , , , . , , . , - . , . .


5.1.3


, log . myfirst.cc, «» . , :


NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

, , NS_LOG . . , — NS_LOG_INFO. ( , ), , ("Creating Topology"). ,
scratch/myfirst.cc ,
NS_LOG_INFO ("Creating Topology");
,


NodeContainer nodes;
nodes.Create (2);

, waf, NS_LOG, , :


$ ./waf
$ export NS_LOG=
,    ,
$ ./waf --run scratch/myfirst

, (FirstScriptExample) . , FirstScriptExample NS_LOG_INFO. , ,


$ export NS_LOG=FirstScriptExample=info

, «Creating Topology»,


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.404s)
Creating Topology
Sent 1024 bytes to 10.1.1.2
Received 1024 bytes from 10.1.1.1
Received 1024 bytes from 10.1.1.2

5.2


5.2.1


ns‑3 — . .


. ( ), ,


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

. ns‑3 . scratch/myfirst.cc. , , ,


$ ./waf --run "scratch/myfirst --PrintHelp"

Waf scratch/myfirst --PrintHelp. , , . --PrintHelp ,


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.413s)
TcpL4Protocol:TcpStateMachine()
CommandLine:HandleArgument(): Handle arg name=PrintHelp value=
--PrintHelp: Print this help message.
--PrintGroups: Print the list of groups.
--PrintTypeIds: Print all TypeIds.
--PrintGroup=[group]: Print all TypeIds of group.
--PrintAttributes=[typeid]: Print all attributes of typeid.
--PrintGlobals: Print the list of globals.

--PrintAttributes. ns‑3, first.cc. ,


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

, DataRate PointToPointNetDevice. PointToPointNetDevice. , TypeId. , . ns3::PointToPointNetDevice. , ,


$ ./waf --run "scratch/myfirst --PrintAttributes=ns3::PointToPointNetDevice"

. , ,


--ns3::PointToPointNetDevice::DataRate=[32768bps]:
The default data rate for point to point links

, PointToPointNetDevice. Attribute PointToPointHelper . - . SetDeviceAttribute SetChannelAttribute myfirst.cc, .


PointToPointHelper , ,


...
NodeContainer nodes;
nodes.Create (2);
PointToPointHelper pointToPoint;
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
...

Waf (./waf) UDP - .


$ export 'NS_LOG=UdpEchoServerApplication=level_all|prefix_time'

, :


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.405s)
0s UdpEchoServerApplication:UdpEchoServer()
1s UdpEchoServerApplication:StartApplication()
Sent 1024 bytes to 10.1.1.2
2.25732s Received 1024 bytes from 10.1.1.1
2.25732s Echoing packet
Received 1024 bytes from 10.1.1.2
10s UdpEchoServerApplication:StopApplication()
UdpEchoServerApplication:DoDispose()
UdpEchoServerApplication:~UdpEchoServer()

, , , -, 2,00369 .


2.00369s UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1

2.25732 . , PointToPointNetDevice , 32768 . DataRate , . , , :


$ ./waf --run "scratch/myfirst --ns3::PointToPointNetDevice::DataRate=5Mbps"

DataRate . ? , , . , :


$ ./waf --run "scratch/myfirst --PrintAttributes=ns3::PointToPointChannel"

, :


--ns3::PointToPointChannel::Delay=[0ns]:
Transmission delay through the channel

,


$ ./waf --run "scratch/myfirst
--ns3::PointToPointNetDevice::DataRate=5Mbps
--ns3::PointToPointChannel::Delay=2ms"

, , DataRate Delay :


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.417s)
0s UdpEchoServerApplication:UdpEchoServer()
1s UdpEchoServerApplication:StartApplication()
Sent 1024 bytes to 10.1.1.2
2.00369s Received 1024 bytes from 10.1.1.1
2.00369s Echoing packet
Received 1024 bytes from 10.1.1.2
10s UdpEchoServerApplication:StopApplication()
UdpEchoServerApplication:DoDispose()
UdpEchoServerApplication:~UdpEchoServer()

, 2,00369 . . , MaxPackets UdpEchoClient.


? . , , MaxPackets . . . , . , :


$ ./waf --run "scratch/myfirst
--ns3::PointToPointNetDevice::DataRate=5Mbps
--ns3::PointToPointChannel::Delay=2ms
--ns3::UdpEchoClient::MaxPackets=2"

, , , . , . , :


$ ./waf --run "scratch/myfirst --PrintHelp"
myfirst [Program Arguments] [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.

«PrintGroups», TypeId. ( ). , . , «-»:


./waf --run "scratch/myfirst --PrintGroup=PointToPoint"
TypeIds in group PointToPoint:
ns3::PointToPointChannel
ns3::PointToPointNetDevice
ns3::PointToPointRemoteChannel
ns3::PppHeader

TypeId , ,
--PrintAttributes = ns3 :: PointToPointChannel, .


— Doxygen ns‑3. , .


5.2.2


. AddValue.
, , , . nPackets main. , . , , . , AddValue. scratch/myfirst.cc , ,


int
main (int argc, char *argv[])
{
uint32_t nPackets = 1;
CommandLine cmd;
cmd.AddValue("nPackets", "Number of packets to echo", nPackets);
cmd.Parse (argc, argv);
...

, MaxPackets , nPackets 1, .


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

, --PrintHelp, . . ,


$ ./waf --run "scratch/myfirst --PrintHelp"
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.403s)
--PrintHelp: Print this help message.
--PrintGroups: Print the list of groups.
--PrintTypeIds: Print all TypeIds.
--PrintGroup=[group]: Print all TypeIds of group.
--PrintAttributes=[typeid]: Print all attributes of typeid.
--PrintGlobals: Print the list of globals.
User Arguments:
--nPackets: Number of packets to echo

, , - -nPackets,


$ ./waf --run "scratch/myfirst --nPackets=2"


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.404s)
0s UdpEchoServerApplication:UdpEchoServer()
1s UdpEchoServerApplication:StartApplication()
Sent 1024 bytes to 10.1.1.2
2.25732s Received 1024 bytes from 10.1.1.1
2.25732s Echoing packet
Received 1024 bytes from 10.1.1.2
Sent 1024 bytes to 10.1.1.2
3.25732s Received 1024 bytes from 10.1.1.1
3.25732s Echoing packet
Received 1024 bytes from 10.1.1.2
10s UdpEchoServerApplication:StopApplication()
UdpEchoServerApplication:DoDispose()
UdpEchoServerApplication:~UdpEchoServer()

. , ?
, ns‑3, . , , . , .


5.3


, ns‑3 . ns‑3 — C++, C++ :


#include <iostream>
...
int main ()
{
...
std::cout << "The value of x is " << x << std::endl;
...
}

, . .


ns‑3:


  • , ;


  • , ;


  • . ns‑3 , .



ns-3 , . , , . , , .


, «» , , , - . , . , , ( ) .


, . , , , .


, . . ns‑3 , .


5.3.1 ASCII


ns‑3 , , , . , ASCII. , ns-2, out.tr, .


ASCII scratch/myfirst.cc. Simulator :: Run (), :
AsciiTraceHelper ascii;


pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("myfirst.tr"));

ns‑3, ASCII. . «» CreateFileStream () ( ) . , , , , , myfirst.tr ns‑3. ns‑3, , , () C++.


EnableAsciiAll() , ASCII - , , () ASCII.


, ns-2, , «+», «-», «d» «r».
:


$ ./waf --run scratch/myfirst

, Waf, “‘build’ finished successfully” ( ) .


, myfirst.tr. - Waf, , . , , Waf --cwd. , ASCII myfirst.tr , .


ASCII


, , , , . , .


. , - . — , -. , ( ). :


+: ;
-: ;
d: , , , ;
r: .


. ( ) :


0 +
1 2
2 /NodeList/0/DeviceList/0/$ns3::PointToPointNetDevice/TxQueue/Enqueue
3 ns3::PppHeader (
4   Point-to-Point Protocol: IP (0x0021))
6   ns3::Ipv4Header (
7     tos 0x0 ttl 64 id 0 protocol 17 offset 0 flags [none]
8     length: 1052 10.1.1.1 > 10.1.1.2)
9     ns3::UdpHeader (
10      length: 1032 49153 > 9)
11      Payload (size=1024)

( 0) — . +, . ( 1) — , . , UdpEchoClientApplication . , .


( 2) , ( ). , . NodeList. , ns‑3. , . , , NodeList . , /NodeList/0 NodeList, « 0». , . . , DeviceList/0, .


, $ ns3 :: PointToPointNetDevice, , : . , +, 0, , . « »: TxQueue/Enqueue.


. 3-4 , -. 5-7 , IP4 IP- 10.1.1.1 10.1.1.2. 8-9 , UDP , , 10 , — 1024 .


, .


, -. .


0 r
1 2.25732
2 /NodeList/1/DeviceList/0/$ns3::PointToPointNetDevice/MacRx
3   ns3::Ipv4Header (
4     tos 0x0 ttl 64 id 0 protocol 17 offset 0 flags [none]
5     length: 1052 10.1.1.1 > 10.1.1.2)
6     ns3::UdpHeader (
7       length: 1032 49153 > 9)
8       Payload (size=1024)

, r, 2,25732 . , , DataRate . , .


( 2) , , 1 (/NodeList/1) (/MacRx). , .


5.3.2 PCAP


ns‑3 .pcap. pcap ( ) API, .pcap. , , Wireshark ( Ethereal). , , . , pcap . pcap tcpdump.


pcap , .


pointToPoint.EnablePcapAll ("myfirst");

ASCII, scratch/myfirst.cc. , «myfirst», «myfirst.pcap» - . , , . -. , , «.pcap».


«myfirst-0-0.pcap» «myfirst-1-0.pcap», pcap 0- 0 1- 0 . , pcap , :


$ ./waf --run scratch/myfirst

, : ASCII myfirst.tr, , myfirst-0-0.pcap myfirst-1-0.pcap — pcap , .


tcpdump


pcap tcpdump.


$ tcpdump -nn -tt -r myfirst-0-0.pcap
reading from file myfirst-0-0.pcap, link-type PPP (PPP)
2.000000 IP 10.1.1.1.49153 > 10.1.1.2.9: UDP, length 1024
2.514648 IP 10.1.1.2.9 > 10.1.1.1.49153: UDP, length 1024
tcpdump -nn -tt -r myfirst-1-0.pcap
reading from file myfirst-1-0.pcap, link-type PPP (PPP)
2.257324 IP 10.1.1.1.49153 > 10.1.1.2.9: UDP, length 1024
2.257324 IP 10.1.1.2.9 > 10.1.1.1.49153: UDP, length 1024

myfirst-0-0.pcap ( ) , - 2 . (myfirst-1-0.pcap), , 2,257324 . , 2.257324 , , , 2.514648 .


Wireshark


如果您不熟悉Wireshark,则可以从以下网站下载程序和文档:http : //www.wireshark.org/Wireshark是一个图形用户界面,可用于显示这些跟踪文件。如果您有Wireshark,则可以打开任何跟踪文件并显示内容,就像使用数据包分析器捕获数据包一样。


All Articles