NS3学习
NS3的用途:网络模拟
模拟内容:
- Node
- Applications
- Protocol
- NetDevices
- Channal(在Nodes间传递Packet)
主要用到的NS3模块
core
- Events
- Scheduler
- Time arithmetic
network
- Packets
- Node Class
文件组成
-
src/[core network…]/ -
models/
-
examples/
-
tests/
-
bindings/
-
doc/
-
helper
-
wscript
-
自己写的程序放在scratch文件夹中
常用到的学习目录:examples/tutorial/
模块构成
Node
一个Node可以有多个网卡
NetDevice and Channel
Ethernet NIC —– CSMA Channel
Wifi Net Device —– Wifi Channel
Application(数据收发的方式)
Bulk-Send:一次性全部发出去
On-Off:间歇发送
Packet Sink:收到即接收
Udp-Server——Udp-Client
Udp-echo-client———Udp-echo-server(收到什么,返回什么)
Internet Stack
IPv4/IPv6
入门NS3
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* 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
*/
#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"
using namespace ns3; //用了ns3的名字空间
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); //定义自己的日志名称
int
main (int argc, char *argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);
Time::SetResolution (Time::NS);
//UdpEchoClientApplication是系统已经定义好的模块,启动该日志,级别为INFO
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
//Container就是个数组,存储需要的对象
//Helper是用来调用对象的封装
NS_LOG_UNCOND ("Welcome! Ns3 Simulator! "); //添加无条件记录相关消息
NS_LOG_INFO ("Creating Topology"); //记录程序进展信息
//需要当命令行设置export NS_LOG=
//FirstScriptExample=info才会显示
NodeContainer nodes; //节点数组
nodes.Create (2); //建立两个node,node0和node1
PointToPointHelper pointToPoint; //点对点信道操作
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); //设置带宽
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); //设置时延
NetDeviceContainer devices; //网卡数组
devices = pointToPoint.Install (nodes); //点对点的信道中添加两个node,得到两个网卡,放在devices中
InternetStackHelper stack; //协议栈操作
stack.Install (nodes); //在两个节点上安装协议栈
Ipv4AddressHelper address; //IPv4地址操作
address.SetBase ("10.1.1.0", "255.255.255.0"); //设置地址网段和掩码
Ipv4InterfaceContainer interfaces = address.Assign (devices); //给两个网卡分配IP地址(10.1.1.1和10.1.1.2)
UdpEchoServerHelper echoServer (9); //UdpEcho服务器操作
//使用端口号为9
ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); //在Node1安装UdpEcho服务器(端口9)
serverApps.Start (Seconds (1.0)); //服务开始时间
serverApps.Stop (Seconds (10.0)); //服务结束时间
UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9); //UdpEcho客户端操作
//目的服务器是IP为10.1.1.2: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)); //在Node0上面安装echoClient
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
Simulator::Run (); //开始模拟
Simulator::Destroy ();//结束
return 0;
}
将该文件存入/scratch/myfirst.cc,./waf编译后运行
root@ubuntu:/.../ns-3.30# ./waf --run scratch/myfirst #运行该程序
Waf: Entering directory `/home/distiny/Desktop/tarballs/ns-allinone-3.30/ns-3.30/build'
Waf: Leaving directory `/home/distiny/Desktop/tarballs/ns-allinone-3.30/ns-3.30/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (1.751s)
Welcome! Ns3 Simulator!
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@ubuntu:/home/distiny/Desktop/tarballs/ns-allinone-3.30/ns-3.30# vim scratch/myfirst.cc
添加语句pointToPoint.EnablePcapAll (“myfirst”);于Simulator::Run ();后
再次运行,可以得到pcap文件,使用tcpdump查看pcap文件
$ 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.007372 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.003686 IP 10.1.1.1.49153 > 10.1.1.2.9: UDP, length 1024
2.003686 IP 10.1.1.2.9 > 10.1.1.1.49153: UDP, length 1024
思考
解释数据报传递用时数值是如何得到的?
答:本程序中,设置带宽5Mbps,时延2ms,通过wireshark分析pcap文件,传输数据1024字节,UDP头部8个字节,IP数据报头部20字节,点对点传输协议头部2字节,一共有1054字节,即8432比特。
传输用时t=8432b/5Mbps=0.001686s
加上时延,共0.003686s,和抓包的结果一致。
其他拓展
NS_LOG_ERROR——Log error messages. 记录错误信息
NS_LOG_WARN——Log warning messgaes. 记录警告信息
NS_LOG_DEBUG——Log relatively rare, ad-hoc debugging messages. 记录相对不常见ad-hoc的调试信息
NS_LOG_INFO——Log informational messages about program progress. 记录程序进展信息
NS_LOG_FUNCTION——Log a message describing each function called. 记录描述每个调用函数信息
NS_LOG_LOGIC——Log messages describing logical flow within a function. 记录一个函数内描述逻辑流程的信息
NS_LOG_ALL——Log everything mentioned above. 记录所有信息
此外,还有一种一直被使用的无条件日志级别,与日志级别或者组件选择无关。
NS_LOG_UNCOND——Log theassociated message unconditionally. 无条件记录相关消息
修改代码
int
main (int argc, char *argv[])
{
uint32_t nPackets = 1;
CommandLine cmd;
cmd.AddValue("nPackets", "Number of packets to echo", nPackets);
//设置变量nPackets,名为“nPackets”,解释为“Number of packets to echo”
cmd.Parse (argc, argv);
...
// echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("MaxPackets", UintegerValue (nPackets));
...
$ ./waf --run "scratch/myfirst --PrintHelp"
Waf: Entering directory `/home/distiny/Desktop/tarballs/ns-allinone-3.30/ns-3.30/build'
Waf: Leaving directory `/home/distiny/Desktop/tarballs/ns-allinone-3.30/ns-3.30/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (2.071s)
myfirst [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.
可以看到该变量及定义和值
也可以在运行前修改该变量的值
$ ./waf --run "scratch/myfirst --nPackets=2"