ns-3_ Day 3
无线网络仿真
examples/tutorial/third.cc
在了解了ns-3的一些重要机制后,我们结合这份较复杂的示例代码加以理解。
前两个例子中涉及到了P2P网络结构和CSMA网络结构,这个例子在second.cc的基础上加入3台WIFI节点(n5、n6、n7):
#include "ns3/core-module.h" #include "ns3/point-to-point-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/mobility-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/yans-wifi-helper.h" #include "ns3/ssid.h" // Default Network Topology // // Wifi 10.1.3.0 // AP // * * * * // | | | | 10.1.1.0 // n5 n6 n7 n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0 using namespace ns3; NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); int main (int argc, char *argv[]) { bool verbose = true; uint32_t nCsma = 3; uint32_t nWifi = 3; bool tracing = false; CommandLine cmd (__FILE__); cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi); cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.AddValue ("tracing", "Enable pcap tracing", tracing); cmd.Parse (argc, argv); if (nWifi > 18) { std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box" << std::endl; return 1; } if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); } NodeContainer p2pNodes; p2pNodes.Create (2); PointToPointHelper pointToPoint; // 设置设备的发射速率和信道延迟 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); NetDeviceContainer p2pDevices; p2pDevices = pointToPoint.Install (p2pNodes); NodeContainer csmaNodes; csmaNodes.Add (p2pNodes.Get (1)); csmaNodes.Create (nCsma); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes); NodeContainer wifiStaNodes; wifiStaNodes.Create (nWifi); NodeContainer wifiApNode = p2pNodes.Get (0); // 构建无线设备与无线节点之间的互连通道 YansWifiChannelHelper channel = YansWifiChannelHelper::Default (); YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); // 把Channel关联到物理层对象,确保所有物理层共享底层信道 phy.SetChannel (channel.Create ()); WifiHelper wifi; // AARF Adaptive Auto Rate Fallback 算法(是一种wifi速率控制算法) wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); // 配置MAC类型和基础设施网络的SSID(用于区分不同的WIFI网络) WifiMacHelper mac; Ssid ssid = Ssid ("ns-3-ssid"); mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProbing", BooleanValue (false)); // WIFI场景下的STA节点和AP节点: // 1. STA即station,指接入无线网络的设备 // 2. AccessPoint即无线网络的创建者和中心节点,常常指的是路由器 NetDeviceContainer staDevices; staDevices = wifi.Install (phy, mac, wifiStaNodes); mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid)); NetDeviceContainer apDevices; apDevices = wifi.Install (phy, mac, wifiApNode); // 加入移动模型 MobilityHelper mobility; // 使用二维网络放置最初的STA节点 mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (0.0), "MinY", DoubleValue (0.0), "DeltaX", DoubleValue (5.0), "DeltaY", DoubleValue (10.0), "GridWidth", UintegerValue (3), "LayoutType", StringValue ("RowFirst")); // 节点以随机游走的方式移动, mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); mobility.Install (wifiStaNodes); // 接入点在模拟过程中保持在固定位置 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (wifiApNode); // 安装网络协议栈 InternetStackHelper stack; stack.Install (csmaNodes); stack.Install (wifiApNode); stack.Install (wifiStaNodes); 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); address.SetBase ("10.1.3.0", "255.255.255.0"); address.Assign (staDevices); address.Assign (apDevices); UdpEchoServerHelper echoServer (9); // 在CSMA最右端放置echo服务端程序 ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); // echo客户端程序装在STA节点 UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (wifiStaNodes.Get (nWifi - 1)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); // 启用互联网的全局路由 Ipv4GlobalRoutingHelper::PopulateRoutingTables (); Simulator::Stop (Seconds (10.0)); if (tracing == true) { // 在P2P网络(骨干网络)上进行PCAP Tracing // 在WIFI网络和CSMA网络上进行混杂模式Tracing pointToPoint.EnablePcapAll ("third"); phy.EnablePcap ("third", apDevices.Get (0)); csma.EnablePcap ("third", csmaDevices.Get (0), true); } Simulator::Run (); Simulator::Destroy (); return 0; }
结合 first.cc 中对脚本流程的总结,对 third.cc中的重点步骤进行分析。
网络拓扑
从这张示意图可以看出,这个网络拓扑涉及PPP、CSMA、Wi-Fi三种网络。前两种网络在前面的示例中都已经说明,现在重点关注Wi-Fi场景。
示例代码中的无线网络由一个接入点AP和3个移动点组成,AP同时也是双模节点,安装了PPP和Wi-Fi两种网络。
WifiMac就是链路层,Wifiphy就是物理层。WifiNetDevice是NetDevice的子类,起到桥接作用。
- 设置Channel和Wifiphy。用到2个助手类YansWifiChannelHelper和YansWifiPhyHelper,分别用于配制Channel和WifiPhy。
- Channel需要配置传播延迟(propagation delay)、损耗模型(propagation loss)和移动模型。它们决定了一个分组在无线信道里的传播延迟和接收功率。
- WifiPhy需要配置误码率(error rate),一般设为默认。
- 设置WifiMac并在节点安装NetDevice。用到2个助手类:WifiMacHelper和WifiHelper。
- WifiMacHelper用于设置WifiMac,有两个重要参数:WifiMac子类(决定节点是不是AP)和服务集标识符SSID(节点所属的服务集)。
- WifiHelper用于将Wi-Fi网络设备安装到指定节点中;另外示例中还调用了SetRemoteStationManager,用于Wi-Fi的速率控制(rate control);WifiHelper默认使用802.11aWi-Fi协议。
- 一个分组在Channel中的传播延迟和接收功率是由传播延迟模型、损耗模型和移动模型共同决定的,其中传播延迟和损耗已经在1中设置,移动模型则一般在Wi-Fi网络设备安装至节点后配置。使用的助手类是MobilityHelper。
- 移动模型也需要区分AP和移动节点。
- 示例代码中使用的是ConstantPositionMobilityModel,AP位于原点。
- 移动节点的模型分为初始位置和后续移动轨迹模型。
- GridPositionAllocator按照设置好的坐标给出移动节点的初始位置
- RandomWalk2dMobilityModel在一个指定大小的长方形区域内按照随机的速度和方向移动。
路由设置
为了完成多个有线子网之间的通信,连接两个子网的节点需要具有路由功能,这样才能正确地转发分组。
示例代码中使用语句:
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
设置全局路由。全局路由通过开放式最短路径优先路由算法(OSPF)计算有线网络拓扑中每两个点的最短路径,并为每个节点生成路由表。
数据追踪
ns-3模拟脚本中,收集到的数据常常以pcap或者ASCII文本的形式保存在指定文件中。观察示例代码中的数据追踪设置:
- pointToPoint.EnablePcapAll ("third");
- pointToPoint是一个PointToPointHelper对象
- EnablePcapAll的作用是收集这个信道上虽有节点的链路层分组收发记录
- 记录文件的格式是pcap,“third”是文件前缀名,最后得到的文件名是:
前缀名-节点标号-网络设备标号.pcap
- phy.EnablePcap ("third", apDevices.Get (0));
- phy是一个YansWifiPhyHelper对象
- .EnablePcap打印AP的Wi-Fi网络设备的物理层分组收发信息。
- csma.EnablePcap ("third", csmaDevices.Get (0), true);
- csma是一个CsmaHelper对象
- EnablePcap打印一个有线节点中CSMA网络设备的分组收发信息
posted on 2023-01-01 16:37 LeewayTang 阅读(275) 评论(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搭建本