概述
5G网络的基本架构如下图所示
5G的网络架构相对于4G发生了较大的变化。首先是在接入网络,4G LTE的接入网络由enB和Ue组成,在5G中,接入网变成了由AAU,DU,CU组成的新的接入网NR(New Radio)。4G的核心网由MME,SGW ,PGW ,PCRF,HSS构成,而5G将原来的4G EPC分解成了5GC 和MEC。5GC基于SBA的架构,基本网元有上图组成。为了测试5G网络的基本性能,本文在NS3仿真环境中搭建5G的基本网络。
首先,我们搭建核心网络部分。
Virtual5gcHelper virtual5gcHelper; //定义一个5G核心网的Helper对象
virtual5gcHelper.SetFileType(format);
virtual5gcHelper.SetTopoFile(topoInput);
virtual5gcHelper.SetInputFile(vmInput);
Ptr<Virt5gc> virtual5gcHelper = virtual5gcHelper.GetVirt5gc();
virt5gcHelper->Read();
//配置标准设备
virt5gcHelper->DynamicLoadInit(10.0);
//配置可扩展时延
virt5gcHelper->SetAllocationDelay(0.02);
virt5gcHelper->SetMigrationRate(0.0,0.5);
//连接NR和5GC
Ptr<NrHelper> nrHelper = virt5gcHelper->GetNrHelper();
Ptr<PointToPointNgcHelper>
ngcHelper = virt5gcHelper->HetNgcHelper();
Ptr<Node> pgw = ngcHelper->GetUpfNode();
//配置远程的服务器
NodeContainer remoteHostContainer;
remoteHostContainer.Create(1);
Ptr<Node> remoteHost = remoteHostContainer.Get(0);
InternetStackHelper internet;
internet.Install(remoteHostContainer);
// 将远程服务器和pgw相连
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);
Ipv4AddressHelper ipv4h;
ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
// interface 0 is localhost, 1 is the p2p device
//Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress (1);
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
借下来,我们配置接入网络部分
NodeContainer ueNodes;
NodeContainer enbNodes;
ueNodes = virt5gcHelper->GetUeNodes();
enbNodes = virt5gcHelper->GetEnbNodes();
numberOfNodes = ueNodes.GetN();
totalNodeN = ueNodes.GetN() + enbNodes.GetN();
//安装NR设备
NetDeviceContainer enbNrDevs = virt5gcHelper->GetEnbDevs();
NetDeviceContainer uwNrDevs = virt5gcHelper->GetUwbDevs();
//安装协议栈
internet.Install (ueNodes);
Ipv4InterfaceContainer ueIpIface;
ueIpIface = ngcHelper->AssignUeIpv4Address (NetDeviceContainer (ueNrDevs));
// Assign IP address to UEs, and install applications
for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
{
Ptr<Node> ueNode = ueNodes.Get (u);
// Set the default gateway for the UE
Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ueNode->GetObject<Ipv4> ());
ueStaticRouting->SetDefaultRoute (ngcHelper->GetUeDefaultGatewayAddress (), 1);
}
//用户附着基站
nrHelper-<Attach (ueNrDevs.Get(0), enbNrDevs.Get(0));
nrHelper-<Attach (ueNrDevs.Get(1), enbNrDevs.Get(1));
//定义下行传输链路
uint16_t dlPort = 3000;
std::list<ApplicationContainer> applist;
ApplicationContainer clientApps;
ApplicationContainer serverApps;
for (uint32_t u=0; u < numberOfNodes; u++) {
PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), dlPort));
serverApps.Add (dlPacketSinkHelper.Install (ueNodes.Get(u)));
Ptr<MyApp> app = CreateObject<MyApp> ();
Ptr<Socket> ns3UdpSocket = Socket::CreateSocket (remoteHost, UdpSocketFactory::GetTypeId ());
Address sinkAddress (InetSocketAddress (ueIpIface.GetAddress (u), dlPort));
app->Setup (ns3UdpSocket, sinkAddress, 1400, 5000000, DataRate ("5Mbps"));
remoteHost->AddApplication (app);
app->SetStartTime (Seconds (0.1+u*0.2));
app->SetStopTime (Seconds (simTime));
}
完整的方针代码如下
/* -*-
Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
*
* 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
*
* Author: Jaume Nin <jaume.nin@cttc.cat>
*/
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/point-to-point-helper.h"
#include "ns3/config-store.h"
//#include "ns3/gtk-config-store.h"
#include "ns3/virt-5gc-module.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <thread>
#include <unistd.h>
using namespace ns3;
int additionalNodes = 0;
uint16_t numberOfNodes;
uint16_t totalNodeN;
bool firstCall = true;
Ptr<OutputStreamWrapper> rttStream;
Ptr<OutputStreamWrapper> cwndStream;
Ptr<OutputStreamWrapper> thputStream;
/*
static void
Rx (Ptr<OutputStreamWrapper> stream, uint16_t i, Ptr<const Packet> packet, const Address &from){
packetRxTime[i] = Simulator::Now().GetSeconds();
if (lastPacketRxTime[i] == packetRxTime[i]){
instantPacketSize[i] += packet->GetSize();
return;
}
else{
sumPacketSize[i] += instantPacketSize[i];
*stream->GetStream () << lastPacketRxTime[i] << "t" << instantPacketSize[i] << "t" << sumPacketSize[i]
<< std::endl;
lastPacketRxTime[i] =
packetRxTime[i];
instantPacketSize[i] = packet->GetSize();
}
}
*/
class MyApp : public Application
{
public:
MyApp ();
virtual ~MyApp();
void ChangeDataRate (DataRate rate);
void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
private:
virtual void StartApplication (void);
virtual void StopApplication (void);
void ScheduleTx (void);
void SendPacket (void);
Ptr<Socket>
m_socket;
Address
m_peer;
uint32_t
m_packetSize;
uint32_t
m_nPackets;
DataRate
m_dataRate;
EventId
m_sendEvent;
bool
m_running;
uint32_t
m_packetsSent;
};
MyApp::MyApp ()
: m_socket (0),
m_peer (),
m_packetSize (0),
m_nPackets (0),
m_dataRate (0),
m_sendEvent (),
m_running (false),
m_packetsSent (0)
{
}
MyApp::~MyApp()
{
m_socket = 0;
}
void
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}
void
MyApp::ChangeDataRate (DataRate rate)
{
m_dataRate = rate;
}
void
MyApp::StartApplication (void)
{
m_running = true;
m_packetsSent = 0;
m_socket->Bind ();
m_socket->Connect (m_peer);
SendPacket ();
}
void
MyApp::StopApplication (void)
{
m_running = false;
if (m_sendEvent.IsRunning ())
{
Simulator::Cancel (m_sendEvent);
}
if (m_socket)
{
m_socket->Close ();
}
}
void
MyApp::SendPacket (void)
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
MyAppTag tag (Simulator::Now ());
m_socket->Send (packet);
if (++m_packetsSent < m_nPackets)
{
ScheduleTx ();
}
}
void
MyApp::ScheduleTx (void)
{
if (m_running)
{
Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
}
}
void
Graph()
{
usleep(10000000);
system("gnuplot result_graph_tmp.gpn");
}
void
Graphh()
{
std::thread t(&Graph);
t.join();
}
void
RttTracer (std::string node, Time oldval, Time newval)
{
*rttStream->GetStream() << Simulator::Now ().GetSeconds() << ", " << node << ", " << newval.GetSeconds () << std::endl;
}
void
TraceRtt (std::string rtt_tr_file_name)
{
std::string nodeN = std::to_string(totalNodeN + additionalNodes);
std::string path = "/NodeList/" + nodeN + "/$ns3::TcpL4Protocol/SocketList/";
AsciiTraceHelper ascii;
rttStream = ascii.CreateFileStream (rtt_tr_file_name.c_str());
std::string node, tmp_path;
*rttStream->GetStream() << "# Time Node Rtt" << std::endl;
for (uint16_t i = 0; i < numberOfNodes; i++) {
node = std::to_string(i);
tmp_path = path + node + "/RTT";
//
std::cout << tmp_path << std::endl;
Config::ConnectWithoutContext(tmp_path, MakeBoundCallback (&RttTracer, node));
}
}
void
CwndTracer (std::string node, uint32_t oldval, uint32_t newval)
{
*cwndStream->GetStream() << Simulator::Now().GetSeconds() << ", " << node << ", " << newval << std::endl;
}
void
TraceCwnd (std::string cwnd_tr_file_name)
{
std::string nodeN = std::to_string(totalNodeN + additionalNodes);
std::string path = "/NodeList/" + nodeN + "/$ns3::TcpL4Protocol/SocketList/";
AsciiTraceHelper ascii;
cwndStream = ascii.CreateFileStream (cwnd_tr_file_name.c_str());
std::string node, tmp_path;
*cwndStream->GetStream() << "# Time Node Cwnd" << std::endl;
for (uint16_t i = 0; i < numberOfNodes; i++) {
node = std::to_string(i);
tmp_path = path + node + "/CongestionWindow";
Config::ConnectWithoutContext(tmp_path, MakeBoundCallback (&CwndTracer, node));
}
}
void
CalcThroughput (Ptr<Application> sinkApp, int lastTotalRx, int node)
{
Ptr<PacketSink> sink = DynamicCast<PacketSink> (sinkApp);
Time currTime = Simulator::Now();
DoubleValue doubleValue;
GlobalValue::GetValueByName ("scalingDelay", doubleValue);
double scalingDelay = doubleValue.Get();
double delay = 1;
if (scalingDelay > 0) {
Time t_delay = Time(std::to_string(scalingDelay) + "s");
TimeValue timeValue;
GlobalValue::GetValueByName ("scalingTime", timeValue);
Time scalingTime = timeValue.Get();
Time endTime = scalingTime + t_delay;
if ((currTime >= scalingTime) && (currTime < endTime)) {
Time addedDelay = (endTime - currTime);
delay += addedDelay.GetSeconds();
}
}
double throughput = ((sink->GetTotalRx() - lastTotalRx) / delay) * (double)(8/1e6);
lastTotalRx = sink->GetTotalRx();
/*
if (firstCall) {
*thputStream->GetStream() << "# Time Node Throughput(goodput)" << std::endl;
firstCall = false;
}
*/
*thputStream->GetStream() << currTime.GetSeconds() << ", " << node << ", " << throughput << std::endl;
Simulator::Schedule(MilliSeconds(1000), &CalcThroughput, sinkApp, lastTotalRx, node);
}
void setFlowWeight(void)
{
std::string filePath = "ovs_weight.txt";
std::ofstream writeFile(filePath.data());
if (writeFile.is_open()) {
writeFile << "10n";
writeFile << "10n";
writeFile.close();
}
}
int
main (int argc, char *argv[])
{
//uint16_t numberOfNodes;
double simTime = 15.0;
double distance = 30.0;
double interPacketInterval = 50;
//std::string rttFile;
// Command line arguments
CommandLine cmd;
cmd.AddValue("numberOfNodes", "Number of eNodeBs + UE pairs", numberOfNodes);
cmd.AddValue("simTime", "Total duration of the simulation [s])", simTime);
cmd.AddValue("distance", "Distance between eNBs [m]", distance);
cmd.AddValue("interPacketInterval", "Inter packet interval [ms])", interPacketInterval);
//cmd.AddValue("rtt_tr_name", "Name of output trace file", rttFile);
cmd.Parse(argc, argv);
std::string format ("Virt5gc");
std::string topoInput ("input/virt-5gc-toposample.input");
std::string vmInput ("input/virt-5gc-vminput-heavy.input");
setFlowWeight();
Virt5gcHelper virt5gcHelp;
virt5gcHelp.SetFileType (format);
virt5gcHelp.SetTopoFile (topoInput);
virt5gcHelp.SetInputFile (vmInput);
Ptr<Virt5gc> virt5gcHelper = virt5gcHelp.GetVirt5gc();
virt5gcHelper->Read();
// set standard dev
virt5gcHelper->DynamicLoadInit(10.0);
// set scaling delay
virt5gcHelper->SetAllocationDelay(0.02);
/* SetMigrationRate (scale in, scale out)
* scale in: 0 (even distribution), 1 (load maximum from first VM in a vmList)
* scale out: 0, 0.5, 1.0
*/
virt5gcHelper->SetMigrationRate(0.0, 0.5);
ConfigStore inputConfig;
inputConfig.ConfigureDefaults();
// parse again so you can override default values from the command line
cmd.Parse(argc, argv);
Ptr<NrHelper> nrHelper = virt5gcHelper->GetNrHelper();
Ptr<PointToPointNgcHelper> ngcHelper = virt5gcHelper->GetNgcHelper();
//Ptr<OvsPointToPointEpcHelper> epcHelper = virt5gcHelper->GetEpcHelper();
Ptr<Node> pgw = ngcHelper->GetUpfNode ();
// Create a single RemoteHost
NodeContainer remoteHostContainer;
remoteHostContainer.Create (1);
Ptr<Node> remoteHost = remoteHostContainer.Get (0);
InternetStackHelper internet;
internet.Install (remoteHostContainer);
// Create the Internet
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);
Ipv4AddressHelper ipv4h;
ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
// interface 0 is localhost, 1 is the p2p device
//Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress (1);
Ipv4StaticRoutingHelper ipv4RoutingHelper;
Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
NodeContainer ueNodes;
NodeContainer enbNodes;
ueNodes = virt5gcHelper->GetUeNodes();
enbNodes = virt5gcHelper->GetEnbNodes();
numberOfNodes = ueNodes.GetN();
//numberOfNodes = 3;
totalNodeN = ueNodes.GetN() + enbNodes.GetN();
// Install LTE Devices to the nodes
NetDeviceContainer enbNrDevs = virt5gcHelper->GetEnbDevs();
NetDeviceContainer ueNrDevs = virt5gcHelper->GetUeDevs();
// Install the IP stack on the UEs
internet.Install (ueNodes);
Ipv4InterfaceContainer ueIpIface;
ueIpIface = ngcHelper->AssignUeIpv4Address (NetDeviceContainer (ueNrDevs));
// Assign IP address to UEs, and install applications
for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
{
Ptr<Node> ueNode = ueNodes.Get (u);
// Set the default gateway for the UE
Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ueNode->GetObject<Ipv4> ());
ueStaticRouting->SetDefaultRoute (ngcHelper->GetUeDefaultGatewayAddress (), 1);
}
/*
for (uint32_t i = 0; i < numberOfNodes; i++) {
lteHelper->Attach (ueLteDevs.Get(i), enbLteDevs.Get(i));
}
*/
nrHelper->Attach (ueNrDevs.Get(0), enbNrDevs.Get(0));
nrHelper->Attach (ueNrDevs.Get(1), enbNrDevs.Get(0));
uint16_t dlPort = 3000;
//int start = 1.0;
std::list<ApplicationContainer> appList;
/*
for (uint32_t u = 0; u < numberOfNodes; u++)
{
Ptr<Node> ue = ueNodes.Get(u);
++dlPort;
Ipv4Address ueipaddress = ueIpIface.GetAddress(u);
BulkSendHelper source ("ns3::TcpSocketFactory", InetSocketAddress (ueipaddress, dlPort));
source.SetAttribute ("MaxBytes", UintegerValue (0));
source.SetAttribute("SendSize", UintegerValue (1024));
ApplicationContainer sourceApps = source.Install (remoteHost);
sourceApps.Start (Seconds (start + u*0.2));
sourceApps.Stop (Seconds (simTime));
PacketSinkHelper sink ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), dlPort));
ApplicationContainer sinkApps = sink.Install (ue);
sinkApps.Start (Seconds (start));
sinkApps.Stop (Seconds (simTime));
appList.push_back(sinkApps);
Ptr<EpcTft> tft = Create<EpcTft> ();
EpcTft::PacketFilter dlpf;
dlpf.localPortStart = dlPort;
wdlpf.remotePortEnd = dlPort;
tft->Add(dlpf);
EpsBearer bearer (EpsBearer::NGBR_VIDEO_TCP_OPERATOR);
lteHelper->ActivateDedicatedEpsBearer (ueLteDevs.Get(u), bearer, tft);
}
*/
ApplicationContainer clientApps;
ApplicationContainer serverApps;
for (uint32_t u=0; u < numberOfNodes; u++) {
PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), dlPort));
serverApps.Add (dlPacketSinkHelper.Install (ueNodes.Get(u)));
Ptr<MyApp> app = CreateObject<MyApp> ();
Ptr<Socket> ns3UdpSocket = Socket::CreateSocket (remoteHost, UdpSocketFactory::GetTypeId ());
Address sinkAddress (InetSocketAddress (ueIpIface.GetAddress (u), dlPort));
app->Setup (ns3UdpSocket, sinkAddress, 1400, 5000000, DataRate ("5Mbps"));
remoteHost->AddApplication (app);
app->SetStartTime (Seconds (0.1+u*0.2));
app->SetStopTime (Seconds (simTime));
}
Simulator::Stop(Seconds(simTime));
nrHelper->EnableTraces ();
std::string thput_tr_file_name = "Virt5gc-throughput.data";
AsciiTraceHelper ascii;
thputStream = ascii.CreateFileStream (thput_tr_file_name.c_str());
//std::list<ApplicationContainer>::iterator itor = appList.begin();
//LogComponentEnable ("Config", LOG_LEVEL_ALL);
Simulator::Schedule (Seconds (1 + numberOfNodes*0.2), &TraceRtt, "Virt5gc-rtt.data");
Simulator::Schedule (Seconds (1 + numberOfNodes*0.2), &TraceCwnd, "Virt5gc-cwnd.data");
for (uint32_t i = 0; i < numberOfNodes; i++) {
Simulator::Schedule (Seconds (1 + numberOfNodes*0.2), &CalcThroughput, serverApps.Get(i), 0, i);
}
*thputStream->GetStream() << "# Time Node Throughput(goodput)" << std::endl;
*thputStream->GetStream() << "0.0, 0, 0.0" << std::endl;
*thputStream->GetStream() << "0.0, 1, 0.0" << std::endl;
//
Simulator::Schedule (Seconds (1.5 +numberOfNodes*0.2), &Graphh);
//
std::thread t(&Graph);
Simulator::Run();
//
t.join();
Simulator::Destroy();
return 0;
}
最后
以上就是谦让硬币为你收集整理的ns3网络仿真_基于NS3的5G网络仿真的全部内容,希望文章能够帮你解决ns3网络仿真_基于NS3的5G网络仿真所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复