A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lena-x2-handover.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2018 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Manuel Requena <manuel.requena@cttc.es>
7 */
8
9#include "ns3/applications-module.h"
10#include "ns3/core-module.h"
11#include "ns3/internet-module.h"
12#include "ns3/lte-module.h"
13#include "ns3/mobility-module.h"
14#include "ns3/network-module.h"
15#include "ns3/point-to-point-module.h"
16// #include "ns3/gtk-config-store.h"
17
18using namespace ns3;
19
20NS_LOG_COMPONENT_DEFINE("LenaX2HandoverExample");
21
22/**
23 * UE Connection established notification.
24 *
25 * \param context The context.
26 * \param imsi The IMSI of the connected terminal.
27 * \param cellid The Cell ID.
28 * \param rnti The RNTI.
29 */
30void
31NotifyConnectionEstablishedUe(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
32{
33 std::cout << Simulator::Now().As(Time::S) << " " << context << " UE IMSI " << imsi
34 << ": connected to CellId " << cellid << " with RNTI " << rnti << std::endl;
35}
36
37/**
38 * UE Start Handover notification.
39 *
40 * \param context The context.
41 * \param imsi The IMSI of the connected terminal.
42 * \param cellid The actual Cell ID.
43 * \param rnti The RNTI.
44 * \param targetCellId The target Cell ID.
45 */
46void
47NotifyHandoverStartUe(std::string context,
48 uint64_t imsi,
49 uint16_t cellid,
50 uint16_t rnti,
51 uint16_t targetCellId)
52{
53 std::cout << Simulator::Now().As(Time::S) << " " << context << " UE IMSI " << imsi
54 << ": previously connected to CellId " << cellid << " with RNTI " << rnti
55 << ", doing handover to CellId " << targetCellId << std::endl;
56}
57
58/**
59 * UE Handover end successful notification.
60 *
61 * \param context The context.
62 * \param imsi The IMSI of the connected terminal.
63 * \param cellid The Cell ID.
64 * \param rnti The RNTI.
65 */
66void
67NotifyHandoverEndOkUe(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
68{
69 std::cout << Simulator::Now().As(Time::S) << " " << context << " UE IMSI " << imsi
70 << ": successful handover to CellId " << cellid << " with RNTI " << rnti << std::endl;
71}
72
73/**
74 * eNB Connection established notification.
75 *
76 * \param context The context.
77 * \param imsi The IMSI of the connected terminal.
78 * \param cellid The Cell ID.
79 * \param rnti The RNTI.
80 */
81void
82NotifyConnectionEstablishedEnb(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
83{
84 std::cout << Simulator::Now().As(Time::S) << " " << context << " eNB CellId " << cellid
85 << ": successful connection of UE with IMSI " << imsi << " RNTI " << rnti
86 << std::endl;
87}
88
89/**
90 * eNB Start Handover notification.
91 *
92 * \param context The context.
93 * \param imsi The IMSI of the connected terminal.
94 * \param cellid The actual Cell ID.
95 * \param rnti The RNTI.
96 * \param targetCellId The target Cell ID.
97 */
98void
99NotifyHandoverStartEnb(std::string context,
100 uint64_t imsi,
101 uint16_t cellid,
102 uint16_t rnti,
103 uint16_t targetCellId)
104{
105 std::cout << Simulator::Now().As(Time::S) << " " << context << " eNB CellId " << cellid
106 << ": start handover of UE with IMSI " << imsi << " RNTI " << rnti << " to CellId "
107 << targetCellId << std::endl;
108}
109
110/**
111 * eNB Handover end successful notification.
112 *
113 * \param context The context.
114 * \param imsi The IMSI of the connected terminal.
115 * \param cellid The Cell ID.
116 * \param rnti The RNTI.
117 */
118void
119NotifyHandoverEndOkEnb(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
120{
121 std::cout << Simulator::Now().As(Time::S) << " " << context << " eNB CellId " << cellid
122 << ": completed handover of UE with IMSI " << imsi << " RNTI " << rnti << std::endl;
123}
124
125/**
126 * Handover failure notification
127 *
128 * \param context The context.
129 * \param imsi The IMSI of the connected terminal.
130 * \param cellid The Cell ID.
131 * \param rnti The RNTI.
132 */
133void
134NotifyHandoverFailure(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
135{
136 std::cout << Simulator::Now().As(Time::S) << " " << context << " eNB CellId " << cellid
137 << " IMSI " << imsi << " RNTI " << rnti << " handover failure" << std::endl;
138}
139
140/**
141 * Sample simulation script for a X2-based handover.
142 * It instantiates two eNodeB, attaches one UE to the 'source' eNB and
143 * triggers a handover of the UE towards the 'target' eNB.
144 */
145int
146main(int argc, char* argv[])
147{
148 // LogLevel logLevel = (LogLevel)(LOG_PREFIX_FUNC | LOG_PREFIX_TIME | LOG_LEVEL_ALL);
149
150 // LogComponentEnable ("LteHelper", logLevel);
151 // LogComponentEnable ("EpcHelper", logLevel);
152 // LogComponentEnable ("EpcEnbApplication", logLevel);
153 // LogComponentEnable ("EpcMmeApplication", logLevel);
154 // LogComponentEnable ("EpcPgwApplication", logLevel);
155 // LogComponentEnable ("EpcSgwApplication", logLevel);
156 // LogComponentEnable ("EpcX2", logLevel);
157
158 // LogComponentEnable ("LteEnbRrc", logLevel);
159 // LogComponentEnable ("LteEnbNetDevice", logLevel);
160 // LogComponentEnable ("LteUeRrc", logLevel);
161 // LogComponentEnable ("LteUeNetDevice", logLevel);
162
163 uint16_t numberOfUes = 1;
164 uint16_t numberOfEnbs = 2;
165 uint16_t numBearersPerUe = 2;
166 Time simTime = MilliSeconds(490);
167 double distance = 100.0;
168 bool disableDl = false;
169 bool disableUl = false;
170
171 // change some default attributes so that they are reasonable for
172 // this scenario, but do this before processing command line
173 // arguments, so that the user is allowed to override these settings
174 Config::SetDefault("ns3::UdpClient::Interval", TimeValue(MilliSeconds(10)));
175 Config::SetDefault("ns3::UdpClient::MaxPackets", UintegerValue(1000000));
176 Config::SetDefault("ns3::LteHelper::UseIdealRrc", BooleanValue(false));
177
178 // Command line arguments
179 CommandLine cmd(__FILE__);
180 cmd.AddValue("numberOfUes", "Number of UEs", numberOfUes);
181 cmd.AddValue("numberOfEnbs", "Number of eNodeBs", numberOfEnbs);
182 cmd.AddValue("simTime", "Total duration of the simulation", simTime);
183 cmd.AddValue("disableDl", "Disable downlink data flows", disableDl);
184 cmd.AddValue("disableUl", "Disable uplink data flows", disableUl);
185 cmd.Parse(argc, argv);
186
189 lteHelper->SetEpcHelper(epcHelper);
190 lteHelper->SetSchedulerType("ns3::RrFfMacScheduler");
191 lteHelper->SetHandoverAlgorithmType("ns3::NoOpHandoverAlgorithm"); // disable automatic handover
192
193 Ptr<Node> pgw = epcHelper->GetPgwNode();
194
195 // Create a single RemoteHost
196 NodeContainer remoteHostContainer;
197 remoteHostContainer.Create(1);
198 Ptr<Node> remoteHost = remoteHostContainer.Get(0);
200 internet.Install(remoteHostContainer);
201
202 // Create the Internet
204 p2ph.SetDeviceAttribute("DataRate", DataRateValue(DataRate("100Gb/s")));
205 p2ph.SetDeviceAttribute("Mtu", UintegerValue(1500));
206 p2ph.SetChannelAttribute("Delay", TimeValue(Seconds(0.010)));
207 NetDeviceContainer internetDevices = p2ph.Install(pgw, remoteHost);
208 Ipv4AddressHelper ipv4h;
209 ipv4h.SetBase("1.0.0.0", "255.0.0.0");
210 Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign(internetDevices);
211 Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress(1);
212
213 // Routing of the Internet Host (towards the LTE network)
214 Ipv4StaticRoutingHelper ipv4RoutingHelper;
215 Ptr<Ipv4StaticRouting> remoteHostStaticRouting =
216 ipv4RoutingHelper.GetStaticRouting(remoteHost->GetObject<Ipv4>());
217 // interface 0 is localhost, 1 is the p2p device
218 remoteHostStaticRouting->AddNetworkRouteTo(Ipv4Address("7.0.0.0"), Ipv4Mask("255.0.0.0"), 1);
219
220 NodeContainer ueNodes;
221 NodeContainer enbNodes;
222 enbNodes.Create(numberOfEnbs);
223 ueNodes.Create(numberOfUes);
224
225 // Install Mobility Model
227 for (uint16_t i = 0; i < numberOfEnbs; i++)
228 {
229 positionAlloc->Add(Vector(distance * 2 * i - distance, 0, 0));
230 }
231 for (uint16_t i = 0; i < numberOfUes; i++)
232 {
233 positionAlloc->Add(Vector(0, 0, 0));
234 }
236 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
237 mobility.SetPositionAllocator(positionAlloc);
238 mobility.Install(enbNodes);
239 mobility.Install(ueNodes);
240
241 // Install LTE Devices in eNB and UEs
242 NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice(enbNodes);
243 NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice(ueNodes);
244
245 // Install the IP stack on the UEs
246 internet.Install(ueNodes);
247 Ipv4InterfaceContainer ueIpIfaces;
248 ueIpIfaces = epcHelper->AssignUeIpv4Address(NetDeviceContainer(ueLteDevs));
249
250 // Attach all UEs to the first eNodeB
251 for (uint16_t i = 0; i < numberOfUes; i++)
252 {
253 lteHelper->Attach(ueLteDevs.Get(i), enbLteDevs.Get(0));
254 }
255
256 NS_LOG_LOGIC("setting up applications");
257
258 // Install and start applications on UEs and remote host
259 uint16_t dlPort = 10000;
260 uint16_t ulPort = 20000;
261
262 // randomize a bit start times to avoid simulation artifacts
263 // (e.g., buffer overflows due to packet transmissions happening
264 // exactly at the same time)
266 startTimeSeconds->SetAttribute("Min", DoubleValue(0.05));
267 startTimeSeconds->SetAttribute("Max", DoubleValue(0.06));
268
269 for (uint32_t u = 0; u < numberOfUes; ++u)
270 {
271 Ptr<Node> ue = ueNodes.Get(u);
272 // Set the default gateway for the UE
273 Ptr<Ipv4StaticRouting> ueStaticRouting =
274 ipv4RoutingHelper.GetStaticRouting(ue->GetObject<Ipv4>());
275 ueStaticRouting->SetDefaultRoute(epcHelper->GetUeDefaultGatewayAddress(), 1);
276
277 for (uint32_t b = 0; b < numBearersPerUe; ++b)
278 {
282
283 if (!disableDl)
284 {
285 ++dlPort;
286
287 NS_LOG_LOGIC("installing UDP DL app for UE " << u);
288 UdpClientHelper dlClientHelper(ueIpIfaces.GetAddress(u), dlPort);
289 clientApps.Add(dlClientHelper.Install(remoteHost));
290 PacketSinkHelper dlPacketSinkHelper(
291 "ns3::UdpSocketFactory",
293 serverApps.Add(dlPacketSinkHelper.Install(ue));
294
296 dlpf.localPortStart = dlPort;
297 dlpf.localPortEnd = dlPort;
298 tft->Add(dlpf);
299 }
300
301 if (!disableUl)
302 {
303 ++ulPort;
304
305 NS_LOG_LOGIC("installing UDP UL app for UE " << u);
306 UdpClientHelper ulClientHelper(remoteHostAddr, ulPort);
307 clientApps.Add(ulClientHelper.Install(ue));
308 PacketSinkHelper ulPacketSinkHelper(
309 "ns3::UdpSocketFactory",
311 serverApps.Add(ulPacketSinkHelper.Install(remoteHost));
312
314 ulpf.remotePortStart = ulPort;
315 ulpf.remotePortEnd = ulPort;
316 tft->Add(ulpf);
317 }
318
320 lteHelper->ActivateDedicatedEpsBearer(ueLteDevs.Get(u), bearer, tft);
321
322 Time startTime = Seconds(startTimeSeconds->GetValue());
323 serverApps.Start(startTime);
324 clientApps.Start(startTime);
325 clientApps.Stop(simTime);
326
327 } // end for b
328 }
329
330 // Add X2 interface
331 lteHelper->AddX2Interface(enbNodes);
332
333 // X2-based Handover
334 lteHelper->HandoverRequest(MilliSeconds(300),
335 ueLteDevs.Get(0),
336 enbLteDevs.Get(0),
337 enbLteDevs.Get(1));
338
339 // Uncomment to enable PCAP tracing
340 // p2ph.EnablePcapAll("lena-x2-handover");
341
342 lteHelper->EnablePhyTraces();
343 lteHelper->EnableMacTraces();
344 lteHelper->EnableRlcTraces();
345 lteHelper->EnablePdcpTraces();
346 Ptr<RadioBearerStatsCalculator> rlcStats = lteHelper->GetRlcStats();
347 rlcStats->SetAttribute("EpochDuration", TimeValue(Seconds(0.05)));
348 Ptr<RadioBearerStatsCalculator> pdcpStats = lteHelper->GetPdcpStats();
349 pdcpStats->SetAttribute("EpochDuration", TimeValue(Seconds(0.05)));
350
351 // connect custom trace sinks for RRC connection establishment and handover notification
352 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/ConnectionEstablished",
354 Config::Connect("/NodeList/*/DeviceList/*/LteUeRrc/ConnectionEstablished",
356 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverStart",
358 Config::Connect("/NodeList/*/DeviceList/*/LteUeRrc/HandoverStart",
360 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverEndOk",
362 Config::Connect("/NodeList/*/DeviceList/*/LteUeRrc/HandoverEndOk",
364
365 // Hook a trace sink (the same one) to the four handover failure traces
366 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverFailureNoPreamble",
368 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverFailureMaxRach",
370 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverFailureLeaving",
372 Config::Connect("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverFailureJoining",
374
375 Simulator::Stop(simTime + MilliSeconds(20));
377
378 // GtkConfigStore config;
379 // config.ConfigureAttributes ();
380
382 return 0;
383}
holds a vector of ns3::Application pointers.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
This class contains the specification of EPS Bearers.
Definition eps-bearer.h:80
@ NGBR_VIDEO_TCP_DEFAULT
Non-GBR TCP-based Video (Buffered Streaming, e.g., www, e-mail...)
Definition eps-bearer.h:115
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
a class to represent an Ipv4 address mask
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Hold an unsigned integer type.
Definition uinteger.h:34
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
void NotifyHandoverFailure(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
Handover failure notification.
void NotifyHandoverEndOkUe(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
UE Handover end successful notification.
void NotifyConnectionEstablishedUe(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
UE Connection established notification.
void NotifyHandoverStartUe(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti, uint16_t targetCellId)
UE Start Handover notification.
void NotifyHandoverStartEnb(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti, uint16_t targetCellId)
eNB Start Handover notification.
void NotifyConnectionEstablishedEnb(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
eNB Connection established notification.
void NotifyHandoverEndOkEnb(std::string context, uint64_t imsi, uint16_t cellid, uint16_t rnti)
eNB Handover end successful notification.
serverApps
Definition first.py:43
clientApps
Definition first.py:53
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
mobility
Definition third.py:92
Implement the data structure representing a TrafficFlowTemplate Packet Filter.
Definition epc-tft.h:60
uint16_t localPortEnd
end of the port number range of the UE
Definition epc-tft.h:121
uint16_t remotePortEnd
end of the port number range of the remote host
Definition epc-tft.h:119
uint16_t remotePortStart
start of the port number range of the remote host
Definition epc-tft.h:118
uint16_t localPortStart
start of the port number range of the UE
Definition epc-tft.h:120