boolverbose=true;uint32_tnCsma=3;CommandLinecmd;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;
At time +2s client sent 1024 bytes to 10.1.2.4 port 9
At time +2.0078s server received 1024 bytes from 10.1.1.1 port 49153
At time +2.0078s server sent 1024 bytes to 10.1.1.1 port 49153
At time +2.01761s client received 1024 bytes from 10.1.2.4 port 9
回想一下,第一条消息“Sent 1024 bytes to 10.1.2.4”是UDP客户端将数据包发送到服务器。在这种情况下,服务器位于另一个网络(10.1.2.0)上。
第二条消息“Received 1024 bytes from 10.1.1.1”来自UDP服务器,在接收到回显数据包时生成。
最后一条消息“Received 1024 bytes from 10.1.2.4”来自客户端,表示它已经从服务器那里收到了回显。
At time +2s client sent 1024 bytes to 10.1.2.5 port 9
At time +2.0118s server received 1024 bytes from 10.1.1.1 port 49153
At time +2.0118s server sent 1024 bytes to 10.1.1.1 port 49153
At time +2.02461s client received 1024 bytes from 10.1.2.5 port 9
/* * 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/applications-module.h"#include"ns3/core-module.h"#include"ns3/csma-module.h"#include"ns3/internet-module.h"#include"ns3/ipv4-global-routing-helper.h"#include"ns3/network-module.h"#include"ns3/point-to-point-module.h"// Default Network Topology//// 10.1.1.0// n0 -------------- n1 n2 n3 n4// point-to-point | | | |// ================// LAN 10.1.2.0usingnamespacens3;NS_LOG_COMPONENT_DEFINE("SecondScriptExample");intmain(intargc,char*argv[]){boolverbose=true;// 确定是否启用UdpEchoClientApplication和UdpEchoServerApplication日志记录uint32_tnCsma=3;// 允许动态通过命令行参数更改CSMA网络上的设备数量CommandLinecmd(__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;// 确保我们的拓扑中至少有一个“额外”的节点NodeContainerp2pNodes;// 建立2个我们将通过点对点链接连接的节点p2pNodes.Create(2);// 节点容器called:p2pNodes,里面装有两个节点NodeContainercsmaNodes;// 建立另一个NodeContainer来保存将成为总线(CSMA)网络一部分的节点csmaNodes.Add(p2pNodes.Get(1));// 从点对点节点容器中获取第一个节点(索引为1)// 并将其添加到将获取CSMA设备的节点容器中csmaNodes.Create(nCsma);// 创建合理点数的“CSMA容器”PointToPointHelperpointToPoint;// 建立一个“节点连接助手”,called:pointTopointpointToPoint.SetDeviceAttribute("DataRate",StringValue("5Mbps"));// 该助手配置:每秒5兆比特的发送器pointToPoint.SetChannelAttribute("Delay",StringValue("2ms"));// 由该助手配置的通道上:有2毫秒的延迟NetDeviceContainerp2pDevices;// 建立一个“网络设备创建助手”p2pDevices=pointToPoint.Install(p2pNodes);// 在“点对点链接的节点”上安装设备CsmaHelpercsma;// 建立一个“CSMA搭建助手”,called:csmacsma.SetChannelAttribute("DataRate",StringValue("100Mbps"));// csma设备之间“通道对”的带宽是100Mbpscsma.SetChannelAttribute("Delay",TimeValue(NanoSeconds(6560)));// 通道的光速延迟设置为6560nsNetDeviceContainercsmaDevices;// 建立一个“网络设备创建助手”csmaDevices=csma.Install(csmaNodes);// 在“CSMA节点”上安装设备InternetStackHelperstack;// 建立一个“网络栈搭建助手”stack.Install(p2pNodes.Get(0));// 将“点对点连接的节点容器”内所有“剩余”的节点安排上协议栈stack.Install(csmaNodes);// 将“CSMA节点容器”内所有的节点均安排上协议栈Ipv4AddressHelperaddress;// 建立一个“IP地址分配助手”,旨在为设备接口分配IP地址address.SetBase("10.1.1.0","255.255.255.0");// 助手内含:使用网络10.1.1.0,创建“点对点设备”地址Ipv4InterfaceContainerp2pInterfaces;// 建立一个“接口容器”,将创建的接口保存在容器中p2pInterfaces=address.Assign(p2pDevices);// 将“点对点链接容器内的节点”接口赋上地址address.SetBase("10.1.2.0","255.255.255.0");// 需要为CSMA设备接口分配IP地址(使用网络10.1.2.0)Ipv4InterfaceContainercsmaInterfaces;// 建立一个“接口容器”,将创建的接口保存在容器中csmaInterfaces=address.Assign(csmaDevices);// 将“CSMA容器内节点”接口赋上地址UdpEchoServerHelperechoServer(9);// 建立一个“服务器端搭建助手”ApplicationContainerserverApps=echoServer.Install(csmaNodes.Get(nCsma));// 在CSMA容器内的最后一// 个节点上配置服务器serverApps.Start(Seconds(1.0));// 服务器的开始时刻是t=1sserverApps.Stop(Seconds(10.0));// 服务器的关闭时刻是t=10sUdpEchoClientHelperechoClient(csmaInterfaces.GetAddress(nCsma),9);// 客户端的远程地址是“CSMA容器”// 配置好服务器的节点,即:// 上述index=nCsma的那个节点echoClient.SetAttribute("MaxPackets",UintegerValue(1));// 规范:客户端发送数据包的最大数量echoClient.SetAttribute("Interval",TimeValue(Seconds(1.0)));// 规范:发送的时间间隔echoClient.SetAttribute("PacketSize",UintegerValue(1024));// 规范:所发送数据包的最大容量ApplicationContainerclientApps=echoClient.Install(p2pNodes.Get(0));// 建立一个“应用安装助手”clientApps.Start(Seconds(2.0));// 客户端应用的开始时刻是t=2sclientApps.Stop(Seconds(10.0));// 客户端应用的关闭时刻是t=10sIpv4GlobalRoutingHelper::PopulateRoutingTables();// 建立一个“设置全局路由表”的助手// 该管理器使用此全局信息构造每个节点的路由表pointToPoint.EnablePcapAll("second");// 启用“点对点”助手中的pcap跟踪csma.EnablePcap("second",csmaDevices.Get(1),true);// 启用CSMA助手中的pcap跟踪Simulator::Run();// 代码运行Simulator::Destroy();// 清理仿真return0;}117,1Bot
boolverbose=true;uint32_tnCsma=3;uint32_tnWifi=3;CommandLinecmd;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.Parse(argc,argv);if(verbose){LogComponentEnable("UdpEchoClientApplication",LOG_LEVEL_INFO);LogComponentEnable("UdpEchoServerApplication",LOG_LEVEL_INFO);}
voidCourseChange(std::stringcontext,Ptr<constMobilityModel>model){Vectorposition=model->GetPosition();NS_LOG_UNCOND(context<<" x = "<<position.x<<", y = "<<position.y);}
/* * 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/applications-module.h"#include"ns3/core-module.h"#include"ns3/csma-module.h"#include"ns3/internet-module.h"#include"ns3/mobility-module.h"#include"ns3/network-module.h"#include"ns3/point-to-point-module.h"#include"ns3/ssid.h"#include"ns3/yans-wifi-helper.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.0usingnamespacens3;NS_LOG_COMPONENT_DEFINE("ThirdScriptExample");intmain(intargc,char*argv[]){boolverbose=true;uint32_tnCsma=3;uint32_tnWifi=3;booltracing=false;CommandLinecmd(__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);// The underlying restriction of 18 is due to the grid position// allocator's configuration; the grid layout will exceed the// bounding box if more than 18 nodes are provided.if(nWifi>18){std::cout<<"nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"<<std::endl;return1;}if(verbose){LogComponentEnable("UdpEchoClientApplication",LOG_LEVEL_INFO);LogComponentEnable("UdpEchoServerApplication",LOG_LEVEL_INFO);}NodeContainerp2pNodes;p2pNodes.Create(2);PointToPointHelperpointToPoint;pointToPoint.SetDeviceAttribute("DataRate",StringValue("5Mbps"));pointToPoint.SetChannelAttribute("Delay",StringValue("2ms"));NetDeviceContainerp2pDevices;p2pDevices=pointToPoint.Install(p2pNodes);NodeContainercsmaNodes;csmaNodes.Add(p2pNodes.Get(1));csmaNodes.Create(nCsma);CsmaHelpercsma;csma.SetChannelAttribute("DataRate",StringValue("100Mbps"));csma.SetChannelAttribute("Delay",TimeValue(NanoSeconds(6560)));NetDeviceContainercsmaDevices;csmaDevices=csma.Install(csmaNodes);NodeContainerwifiStaNodes;wifiStaNodes.Create(nWifi);NodeContainerwifiApNode=p2pNodes.Get(0);// 构建了Wi-Fi设备和这些Wi-Fi节点之间的互连通道:YansWifiChannelHelperchannel=YansWifiChannelHelper::Default();// 建立了默认的通道模型“搭建助手”YansWifiPhyHelperphy;// 建立了默认的PHY模型“搭建助手”phy.SetChannel(channel.Create());// 建立一个“通道对象”,并将其与我们的PHY层对象管理器关联,以确保由YansWifiPhyHelper创建的所有PHY层对象共享相同的基础通道,即它们共享相同的无线介质,可以通信和干扰// PHY助手配置已经完成,现在我们可以专注于MAC层:WifiMacHelpermac;// 建立一个WifiMacHelper对象,用于设置MAC参数Ssidssid=Ssid("ns-3-ssid");// 建立一个将用于设置MAC层实现的“Ssid”属性值的802.11SSID对象// 现在,我们准备在节点上安装Wi-Fi模型:(我们将依赖于四个helper)WifiHelperwifi;NetDeviceContainerstaDevices;mac.SetType("ns3::StaWifiMac","Ssid",SsidValue(ssid),"ActiveProbing",BooleanValue(false));// 这里四个参数,实现所有与站点相关的参数在MAC和PHY层都配置好// 第一个是TypeId值,指定了将由助手创建的具体类型的MAC层// 最后一个“ActiveProbing”属性被设置为false,这意味着由该助手创建的MAC将不会发送探测请求,并且站点将监听AP的信标。staDevices=wifi.Install(phy,mac,wifiStaNodes);// 调用熟悉的Install方法来创建这些站点的Wi-Fi设备// 我们已经为所有STA节点配置了Wi-Fi,现在我们需要配置AP(接入点)节点:NetDeviceContainerapDevices;mac.SetType("ns3::ApWifiMac","Ssid",SsidValue(ssid));// WifiMacHelper将创建“ns3::ApWifiMac”类型的MAC层,后者指定将创建一个配置为AP的MAC实例apDevices=wifi.Install(phy,mac,wifiApNode);// 创建单个AP,它与站点共享相同的PHY层属性(和信道)MobilityHelpermobility;// 建立一个MobilityHelper对象(移动搭建助手)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"));// 使用二维网格来最初放置STA节点mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel","Bounds",RectangleValue(Rectangle(-50,50,-50,50)));// 我们选择RandomWalk2dMobilityModel告诉这些点如何移动mobility.Install(wifiStaNodes);// 告诉MobilityHelper在STA节点上安装移动模型mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");// 希望仿真期间让接入点AP保持固定位置mobility.Install(wifiApNode);//通过将此节点的移动模型设置为ns3::ConstantPositionMobilityModel来实现// 现在,我们已经创建了我们的节点、设备和信道,并为Wi-Fi节点选择了移动模型,但我们还没有协议栈:InternetStackHelperstack;stack.Install(csmaNodes);stack.Install(wifiApNode);stack.Install(wifiStaNodes);Ipv4AddressHelperaddress;// 建立一个IPv4地址分配助手// 使用Ipv4AddressHelper为我们的设备接口分配IP地址:address.SetBase("10.1.1.0","255.255.255.0");Ipv4InterfaceContainerp2pInterfaces;p2pInterfaces=address.Assign(p2pDevices);// 使用网络10.1.1.0创建两个点对点设备所需的地址address.SetBase("10.1.2.0","255.255.255.0");Ipv4InterfaceContainercsmaInterfaces;csmaInterfaces=address.Assign(csmaDevices);// 使用网络10.1.2.0为CSMA网络分配地址address.SetBase("10.1.3.0","255.255.255.0");address.Assign(staDevices);address.Assign(apDevices);// 然后我们从网络10.1.3.0为无线网络上的STA设备和AP分配地址UdpEchoServerHelperechoServer(9);// 将回声服务器放在文件开头插图中的“最右侧”节点上:ApplicationContainerserverApps=echoServer.Install(csmaNodes.Get(nCsma));serverApps.Start(Seconds(1.0));serverApps.Stop(Seconds(10.0));UdpEchoClientHelperechoClient(csmaInterfaces.GetAddress(nCsma),9);echoClient.SetAttribute("MaxPackets",UintegerValue(1));echoClient.SetAttribute("Interval",TimeValue(Seconds(1.0)));echoClient.SetAttribute("PacketSize",UintegerValue(1024));// 将回声客户端放在我们创建的最后一个STA节点上,将其指向CSMA网络上的服务器。// 我们以前也进行过类似的操作。ApplicationContainerclientApps=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)// 创建足够的跟踪来覆盖所有(三个)网络{phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);pointToPoint.EnablePcapAll("third");phy.EnablePcap("third",apDevices.Get(0));csma.EnablePcap("third",csmaDevices.Get(0),true);}Simulator::Run();Simulator::Destroy();return0;}
目前,支持流控制的NetDevice支持BQL。通过使用ns-3模拟和实际实验进行的关于设备队列大小对队列调度效果的影响的分析在以下文献中报告:P. Imputato 和 S. Avallone. An analysis of the impact of network device buffers on packet schedulers through experiments and simulations. Simulation Modelling Practice and Theory, 80(Supplement C):1–18, January 2018. DOI: 10.1016/j.simpat.2017.09.008