如何在NS-3 LTE模块中实现满缓冲流量?
为NS-3 LTE仿真添加满缓冲流量的解决方案
核心修改步骤
- 添加EPC核心网组件:NS-3中LTE端到端流量依赖EPC实现IP层连通,需创建
EpcHelper并关联LteHelper。 - 配置Internet栈与IP地址:为UE、PGW及远程主机安装Internet栈,分配IP并设置路由,保证网络连通性。
- 部署满缓冲流量应用:使用
UdpClient持续发送大速率数据包,配合PacketSink接收,模拟缓冲区始终有数据的满缓冲场景。
修改后的完整代码
#include <ns3/core-module.h> #include <ns3/network-module.h> #include <ns3/mobility-module.h> #include <ns3/lte-module.h> #include <ns3/internet-module.h> #include <ns3/point-to-point-module.h> #include <ns3/applications-module.h> using namespace std; using namespace ns3; //defines for simple code #define pfff "ns3::PfFfMacScheduler" #define const_pos_model "ns3::ConstantPositionMobilityModel" #define simTime Seconds(10) // 修正宏定义,移除多余分号 int main(int argc, char *argv[]) { //creating UE and ENB (2 and 1) Ptr<LteHelper> lteHelper = CreateObject<LteHelper>(); Ptr<EpcHelper> epcHelper = CreateObject<EpcHelper>(); // 添加EPC助手 lteHelper->SetEpcHelper(epcHelper); Ptr<Node> pgw = epcHelper->GetPgwNode(); // 获取PGW节点 NodeContainer enbNodes; enbNodes.Create(1); NodeContainer ueNodes; ueNodes.Create(2); //creating class instance for using mobility MobilityHelper mobility; //setup the position of ue Ptr<ListPositionAllocator> UEposition = CreateObject<ListPositionAllocator> (); UEposition->Add(Vector (1.0,1.0, 0.0)); UEposition->Add(Vector (1.0,-1.0, 0.0)); mobility.SetPositionAllocator(UEposition); mobility.SetMobilityModel(const_pos_model); mobility.Install(ueNodes); //setup the position of enb Ptr<ListPositionAllocator> ENBposition = CreateObject<ListPositionAllocator> (); ENBposition->Add(Vector (0.0,0.0,0.0)); mobility.SetPositionAllocator(ENBposition); mobility.SetMobilityModel(const_pos_model); mobility.Install(enbNodes); //setup pfff scheduler lteHelper->SetSchedulerType(pfff); //install LTE settings on UE and ENB devices NetDeviceContainer enbDevs; enbDevs = lteHelper->InstallEnbDevice(enbNodes); NetDeviceContainer ueDevs; ueDevs = lteHelper->InstallUeDevice(ueNodes); lteHelper->Attach(ueDevs, enbDevs.Get(0)); // 改用非GBR的互联网QCI,更适合数据流量场景 enum EpsBearer::Qci q = EpsBearer::NON_GBR_INTERNET; EpsBearer bearer(q); lteHelper->ActivateDataRadioBearer(ueDevs, bearer); // -------------------------- // 配置Internet栈与IP连通性 // -------------------------- InternetStackHelper internet; internet.Install(ueNodes); internet.Install(pgw); // 创建远程主机模拟外部网络,通过点到点连接到PGW NodeContainer remoteHostContainer; remoteHostContainer.Create(1); Ptr<Node> remoteHost = remoteHostContainer.Get(0); internet.Install(remoteHost); PointToPointHelper p2ph; p2ph.SetDeviceAttribute("DataRate", DataRateValue(DataRate("100Gb/s"))); // 带宽设足够大,避免成为瓶颈 p2ph.SetDeviceAttribute("Mtu", UintegerValue(1500)); p2ph.SetChannelAttribute("Delay", TimeValue(Seconds(0.010))); NetDeviceContainer internetDevices = p2ph.Install(pgw, remoteHost); // 分配外部网络IP Ipv4AddressHelper ipv4h; ipv4h.SetBase("1.0.0.0", "255.0.0.0"); Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign(internetDevices); Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress(1); // 为UE分配IP地址 Ipv4InterfaceContainer ueIpIface = epcHelper->AssignUeIpv4Address(NetDeviceContainer(ueDevs)); // 为UE设置默认路由,指向EPC网关 Ipv4StaticRoutingHelper ipv4RoutingHelper; for (uint32_t u = 0; u < ueNodes.GetN(); ++u) { Ptr<Node> ueNode = ueNodes.Get(u); Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting(ueNode->GetObject<Ipv4>()); ueStaticRouting->AddDefaultRoute(epcHelper->GetUeDefaultGatewayAddress(), 1); } // -------------------------- // 部署满缓冲流量应用(上行方向) // -------------------------- uint16_t port = 9; // 使用RFC 863的Discard端口 // 在远程主机部署接收端 PacketSinkHelper sinkHelper("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port)); ApplicationContainer sinkApps = sinkHelper.Install(remoteHost); sinkApps.Start(Seconds(0.0)); sinkApps.Stop(simTime); // 在每个UE部署持续发送的UdpClient,模拟满缓冲 for (uint32_t u = 0; u < ueNodes.GetN(); ++u) { Ptr<Node> ueNode = ueNodes.Get(u); UdpClientHelper clientHelper(remoteHostAddr, port); // 设置发送间隔:按100Mbps速率计算,数据包大小1472字节(MTU1500减去IP+UDP头) double interval = (1472 * 8) / 100e6; // 单位:秒 clientHelper.SetAttribute("Interval", TimeValue(Seconds(interval))); clientHelper.SetAttribute("PacketSize", UintegerValue(1472)); ApplicationContainer clientApps = clientHelper.Install(ueNode); clientApps.Start(Seconds(0.1)); // 延迟启动,确保LTE附着流程完成 clientApps.Stop(simTime); } // 启用跟踪 lteHelper->EnableMacTraces(); lteHelper->EnableRlcTraces(); Simulator::Stop(simTime); Simulator::Run(); Simulator::Destroy(); return 0; }
关键说明
- EPC组件必要性:必须添加
EpcHelper才能实现LTE的端到端IP连通,否则UE无法与外部网络通信。 - 满缓冲实现逻辑:通过
UdpClient以固定间隔持续发送数据包,设置的发送速率远大于LTE空口承载能力,确保UE的RLC/MAC缓冲区始终有数据等待发送,达到满缓冲效果。 - 路由配置作用:UE的默认路由指向EPC网关,保证数据包能正确转发到外部网络的远程主机。
- QCI选择原因:改用
NON_GBR_INTERNET(QCI 9)更适合数据流量场景,避免GBR业务的速率限制影响满缓冲效果。
内容的提问来源于stack exchange,提问作者Vladimir Pugovkin
相关产品推荐
相关产品推荐

