NS3重要概念
节点
可以理解为英特网中的主机,但因为NS3不是专门模拟英特网的,所以用结点代替。C++用Node类描述
应用
ns-3仿真环境中的应用程序在节点上运行来驱动模拟过程。在ns-3中,需要被仿真的用户程序被抽象为应用。应用在C++中用
Application类来描述。这个类提供了管理仿真时用户层应用的各种方
法。
信道
你可以把节点连接到代表数据交换信道的对象上。在这
里,基本的通信子网这一抽象概念被称为信道,在C++中用channel类
来描述。
PointToPointChannel 这个类代表一个简单的点对点信道,此通道上
没有多点通信能力,可以有最多2个点至点连接的网络设备。在通道中
有2种“线”。第一个连接的设备获取0号线上的传输,第二个设备得
到1号线上的传输。每根线有空闲或传输状态。Wi-FiChannel此无线通
道实现描述为“又一网络模型”传播模型,在默认情况下,如果没有
传播模型需要设置,则是调用者使用通道设置
网络设备
设备通过驱动程序来控制,而网卡通过网卡驱动程序来控制。在UNIX和Linux系统中,网卡被称为像eth0这样的名字。
在 ns-3中,网络设备这一抽象概念相当于硬件设备和软件驱动的
总和。ns3仿真环境中,网络设备安装在节点上,使得节点通过信道和
其他节点通信。像真实的计算机一样,一个节点可以通过多个网络设
备同时连接到多条信道上。
拓扑帮助
既然把网络设备连接到节点、信道、配置IP地址等在ns-3是很普
遍的任务,那么干脆提供 “拓扑生成器”来使这个工作变得尽可能的
容易。
第二个样例
拓扑结构,n0和n1实现点到点通信(这部分在上一章的例子中己经学会,相似内容不再介绍),n1~n4是个有线局域网络。网络实现 n0经由 n1
和 n4通信。
// Default Network Topology
//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0
main函数第一部分是准备工作
bool verbose = true;////定义变量,用于诀定是否开启2个UdpApplication的Logging组件;默认true开启
uint32_t nCsma = 3;//LAN中有三个node。LAN(local area network)
CommandLine cmd;//命令行部分
cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse (argc,argv);
if (verbose)
{
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
nCsma = nCsma == 0 ? 1 : nCsma;
接下来是网络拓扑部分
NodeContainer p2pNodes;//创建p2p即点对点部分的两个节点。
p2pNodes.Create (2);
NodeContainer csmaNodes;//创建另一个NodeContainer类对象,用于总线(CSMA)网络
csmaNodes.Add (p2pNodes.Get (1));//将之前P2P的NodeContianer的第二个节点(索引1)添加到CSMA的NodeContainer,以获得CSMA device;这个node将会有2个device
csmaNodes.Create (nCsma);//创建其余三个节点
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//设置信道传输速率和延迟
NetDeviceContainer p2pDevices;//安装P2P网卡设备到P2P网络节点,同first.cc
p2pDevices = pointToPoint.Install (p2pNodes);
CsmaHelper csma;//类似于P2PHelper,CsmaHelper帮助创建和连接CSMA设备及信道
csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));//数据率由Channel属性指定,而非Device属性;
//因为CSMA不允许同一信道上有多个不同数据率的设备!
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install (csmaNodes);
InternetStackHelper stack;//安装网络协议
stack.Install (p2pNodes.Get (0));
stack.Install (csmaNodes);//P2P链路中的第二个节点包含在csmaNodes中
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");//安排P2P网段的地址
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign (p2pDevices);//分配给p2p
address.SetBase ("10.1.2.0", "255.255.255.0");//安排CSMA网段地址
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign (csmaDevices);
之后是网络应用层安装
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));//将Server服务安装在CSMA网段的最后一个节点上,nCsma是可变的,所以不能用3
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));//参照first.cc
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0));//p2p左边节点
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));
最后调用全局路由Helper 帮助建立网络路由
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();//全局路由管理器根据节点产生的链路通告为每个节点建立路由
表
pointToPoint.EnablePcapAll ("second");
csma.EnablePcap ("second", csmaDevices.Get (1), true);//开启pcap跟踪
网友评论