ns-3_ Day 1
一些概念
在Day0中,通过ns-3提供的 first.cc 我们了解了几个仿真过程中涉及的概念。ns-3的主体概念有下列5个:
- node 在网络模拟中,实际上就是“terminal/host”的概念。
- application 需要被仿真的用户程序,这也是开发的主要部分。
- channel 网络中数据流流过的媒介,连接各个节点。
- net_device node需要安装net_device才能参与网络连接
- topology_helper 建立每种网络的拓扑结构时有对应的helper辅助
除此之外,对于一个仿真系统来说,数据的收集和统计也是核心的功能,对此ns-3有以下系统:
- Logging 在 fitst.cc 中可以看到几处 LogComponent 的设置,设置后会在运行时给出log信息,帮助调试脚本和分析网络运行。
- 命令行 在 first.cc 中还可以看到 CommandLine,通过这个类可以解析命令行参数。
- Tracing 收集过程数据主要依靠的就是Tracing系统,这个机制嵌入到topology_helper中,直接使用即可
- ASCII Tracing 产生ASCII格式的文档
- PCAP Tracing 产生 .pcap 后缀的文件,可以被诸如 Wireshark 的软件解析。
另一个仿真脚本
下面是一个比 Day0 中复杂一些的脚本,位于:
examples/tutorial/second.cc
#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" #include "ns3/ipv4-global-routing-helper.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("SecondScriptExample"); int main (int argc, char *argv[]) { // 决定是否开启2个UdpApplication的Logging组件 bool verbose = true; // LAN中还有3个node uint32_t nCsma = 3; CommandLine cmd (__FILE__); 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; // 先创建网络结构中的P2P部分 NodeContainer p2pNodes; p2pNodes.Create (2); // 再创建网络结构中的CSMA部分 NodeContainer csmaNodes; csmaNodes.Add (p2pNodes.Get (1)); csmaNodes.Create (nCsma); PointToPointHelper pointToPoint; pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install (p2pNodes); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes); InternetStackHelper stack; stack.Install (p2pNodes.Get (0)); stack.Install (csmaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); Ipv4InterfaceContainer p2pInterfaces; p2pInterfaces = address.Assign (p2pDevices); address.SetBase ("10.1.2.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign (csmaDevices); // 至此网络结构搭建完成,开始安装应用 UdpEchoServerHelper echoServer (9); // 把server应用安装在CSMA网段的“最后”一个节点上 ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); // 最后的c-s过程发生在P2P网段和CSMA网段的2个node上 ApplicationContainer clientApps = echoClient.Install (p2pNodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); // 全局路由管理器根据节点产生的链路报告为每个节点建立路由表 Ipv4GlobalRoutingHelper::PopulateRoutingTables (); // 开启pcap tracing pointToPoint.EnablePcapAll ("second"); csma.EnablePcap ("second", csmaDevices.Get (1), true); Simulator::Run (); Simulator::Destroy (); return 0; }
这个脚本的工作流程大致如下:
10.1.1.0 n0 -------------- n1 n2 n3 n4 point-to-point | | | | ================ LAN 10.1.2.0
- n0发出packet,经 10.1.1.0 网段送达n1
- packet在n1的CSMA接口发出,这里需要ARP协议找到n4的MAC地址
- n1会在CSMA上广播寻找指定IP地址的设备,n4作出正面应答
- 同理,n4要想回发echo packet时(它知道n0不在CSMA上,因为初始化了全局路由),也先广播获知n1的MAC,先从CSMA发给n1再从P2P网络发送给n0。
posted on 2022-12-18 16:04 LeewayTang 阅读(74) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本