A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
lena-x2-handover.cc
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Manuel Requena <manuel.requena@cttc.es>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/internet-module.h"
24 #include "ns3/mobility-module.h"
25 #include "ns3/lte-module.h"
26 #include "ns3/applications-module.h"
27 #include "ns3/point-to-point-module.h"
28 #include "ns3/config-store-module.h"
29 
30 using namespace ns3;
31 
32 
33 
34 void
35 NotifyConnectionEstablishedUe (std::string context,
36  uint64_t imsi,
37  uint16_t cellid,
38  uint16_t rnti)
39 {
40  std::cout << context
41  << " UE IMSI " << imsi
42  << ": connected to CellId " << cellid
43  << " with RNTI " << rnti
44  << std::endl;
45 }
46 
47 void
48 NotifyHandoverStartUe (std::string context,
49  uint64_t imsi,
50  uint16_t cellid,
51  uint16_t rnti,
52  uint16_t targetCellId)
53 {
54  std::cout << context
55  << " UE IMSI " << imsi
56  << ": previously connected to CellId " << cellid
57  << " with RNTI " << rnti
58  << ", doing handover to CellId " << targetCellId
59  << std::endl;
60 }
61 
62 void
63 NotifyHandoverEndOkUe (std::string context,
64  uint64_t imsi,
65  uint16_t cellid,
66  uint16_t rnti)
67 {
68  std::cout << context
69  << " UE IMSI " << imsi
70  << ": successful handover to CellId " << cellid
71  << " with RNTI " << rnti
72  << std::endl;
73 }
74 
75 void
76 NotifyConnectionEstablishedEnb (std::string context,
77  uint64_t imsi,
78  uint16_t cellid,
79  uint16_t rnti)
80 {
81  std::cout << context
82  << " eNB CellId " << cellid
83  << ": successful connection of UE with IMSI " << imsi
84  << " RNTI " << rnti
85  << std::endl;
86 }
87 
88 void
89 NotifyHandoverStartEnb (std::string context,
90  uint64_t imsi,
91  uint16_t cellid,
92  uint16_t rnti,
93  uint16_t targetCellId)
94 {
95  std::cout << context
96  << " eNB CellId " << cellid
97  << ": start handover of UE with IMSI " << imsi
98  << " RNTI " << rnti
99  << " to CellId " << targetCellId
100  << std::endl;
101 }
102 
103 void
104 NotifyHandoverEndOkEnb (std::string context,
105  uint64_t imsi,
106  uint16_t cellid,
107  uint16_t rnti)
108 {
109  std::cout << context
110  << " eNB CellId " << cellid
111  << ": completed handover of UE with IMSI " << imsi
112  << " RNTI " << rnti
113  << std::endl;
114 }
115 
116 
117 
118 
124 NS_LOG_COMPONENT_DEFINE ("EpcX2HandoverExample");
125 int
126 main (int argc, char *argv[])
127 {
128  // LogLevel logLevel = (LogLevel)(LOG_PREFIX_FUNC | LOG_PREFIX_TIME | LOG_LEVEL_ALL);
129 
130  // LogComponentEnable ("LteHelper", logLevel);
131  // LogComponentEnable ("EpcHelper", logLevel);
132  // LogComponentEnable ("EpcEnbApplication", logLevel);
133  // LogComponentEnable ("EpcX2", logLevel);
134  // LogComponentEnable ("EpcSgwPgwApplication", logLevel);
135 
136  // LogComponentEnable ("LteEnbRrc", logLevel);
137  // LogComponentEnable ("LteEnbNetDevice", logLevel);
138  // LogComponentEnable ("LteUeRrc", logLevel);
139  // LogComponentEnable ("LteUeNetDevice", logLevel);
140 
141  uint16_t numberOfUes = 1;
142  uint16_t numberOfEnbs = 2;
143  uint16_t numBearersPerUe = 2;
144  double simTime = 0.300;
145  double distance = 100.0;
146 
147  // change some default attributes so that they are reasonable for
148  // this scenario, but do this before processing command line
149  // arguments, so that the user is allowed to override these settings
150  Config::SetDefault ("ns3::UdpClient::Interval", TimeValue (MilliSeconds(10)));
151  Config::SetDefault ("ns3::UdpClient::MaxPackets", UintegerValue(1000000));
152  Config::SetDefault ("ns3::LteHelper::UseIdealRrc", BooleanValue(false));
153 
154  // Command line arguments
155  CommandLine cmd;
156  cmd.AddValue("numberOfUes", "Number of UEs", numberOfUes);
157  cmd.AddValue("numberOfEnbs", "Number of eNodeBs", numberOfEnbs);
158  cmd.AddValue("simTime", "Total duration of the simulation (in seconds)",simTime);
159  cmd.Parse(argc, argv);
160 
161 
162  Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
163  Ptr<EpcHelper> epcHelper = CreateObject<EpcHelper> ();
164  lteHelper->SetEpcHelper (epcHelper);
165  lteHelper->SetSchedulerType("ns3::RrFfMacScheduler");
166 
167  Ptr<Node> pgw = epcHelper->GetPgwNode ();
168 
169  // Create a single RemoteHost
170  NodeContainer remoteHostContainer;
171  remoteHostContainer.Create (1);
172  Ptr<Node> remoteHost = remoteHostContainer.Get (0);
173  InternetStackHelper internet;
174  internet.Install (remoteHostContainer);
175 
176  // Create the Internet
177  PointToPointHelper p2ph;
178  p2ph.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Gb/s")));
179  p2ph.SetDeviceAttribute ("Mtu", UintegerValue (1500));
180  p2ph.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
181  NetDeviceContainer internetDevices = p2ph.Install (pgw, remoteHost);
182  Ipv4AddressHelper ipv4h;
183  ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
184  Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
185  Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress (1);
186 
187 
188  // Routing of the Internet Host (towards the LTE network)
189  Ipv4StaticRoutingHelper ipv4RoutingHelper;
190  Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
191  // interface 0 is localhost, 1 is the p2p device
192  remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
193 
194  NodeContainer ueNodes;
195  NodeContainer enbNodes;
196  enbNodes.Create(numberOfEnbs);
197  ueNodes.Create(numberOfUes);
198 
199  // Install Mobility Model
200  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
201  for (uint16_t i = 0; i < numberOfEnbs; i++)
202  {
203  positionAlloc->Add (Vector(distance * 2*i - distance, 0, 0));
204  }
205  for (uint16_t i = 0; i < numberOfUes; i++)
206  {
207  positionAlloc->Add (Vector(0, 0, 0));
208  }
209  MobilityHelper mobility;
210  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
211  mobility.SetPositionAllocator(positionAlloc);
212  mobility.Install(enbNodes);
213  mobility.Install(ueNodes);
214 
215  // Install LTE Devices in eNB and UEs
216  NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice (enbNodes);
217  NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice (ueNodes);
218 
219  // Install the IP stack on the UEs
220  internet.Install (ueNodes);
221  Ipv4InterfaceContainer ueIpIfaces;
222  ueIpIfaces = epcHelper->AssignUeIpv4Address (NetDeviceContainer (ueLteDevs));
223  // Assign IP address to UEs, and install applications
224  for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
225  {
226  Ptr<Node> ueNode = ueNodes.Get (u);
227  // Set the default gateway for the UE
228  Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ueNode->GetObject<Ipv4> ());
229  ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress (), 1);
230  }
231 
232 
233  // Attach all UEs to the first eNodeB
234  for (uint16_t i = 0; i < numberOfUes; i++)
235  {
236  lteHelper->Attach (ueLteDevs.Get(i), enbLteDevs.Get(0));
237  }
238 
239 
240  NS_LOG_LOGIC ("setting up applications");
241 
242  // Install and start applications on UEs and remote host
243  uint16_t dlPort = 10000;
244  uint16_t ulPort = 20000;
245 
246  // randomize a bit start times to avoid simulation artifacts
247  // (e.g., buffer overflows due to packet transmissions happening
248  // exactly at the same time)
249  Ptr<UniformRandomVariable> startTimeSeconds = CreateObject<UniformRandomVariable> ();
250  startTimeSeconds->SetAttribute ("Min", DoubleValue (0));
251  startTimeSeconds->SetAttribute ("Max", DoubleValue (0.010));
252 
253  for (uint32_t u = 0; u < numberOfUes; ++u)
254  {
255  Ptr<Node> ue = ueNodes.Get (u);
256  // Set the default gateway for the UE
257  Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ue->GetObject<Ipv4> ());
258  ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress (), 1);
259 
260  for (uint32_t b = 0; b < numBearersPerUe; ++b)
261  {
262  ++dlPort;
263  ++ulPort;
264 
265  ApplicationContainer clientApps;
266  ApplicationContainer serverApps;
267 
268  NS_LOG_LOGIC ("installing UDP DL app for UE " << u);
269  UdpClientHelper dlClientHelper (ueIpIfaces.GetAddress (u), dlPort);
270  clientApps.Add (dlClientHelper.Install (remoteHost));
271  PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory",
273  serverApps.Add (dlPacketSinkHelper.Install (ue));
274 
275  NS_LOG_LOGIC ("installing UDP UL app for UE " << u);
276  UdpClientHelper ulClientHelper (remoteHostAddr, ulPort);
277  clientApps.Add (ulClientHelper.Install (ue));
278  PacketSinkHelper ulPacketSinkHelper ("ns3::UdpSocketFactory",
280  serverApps.Add (ulPacketSinkHelper.Install (remoteHost));
281 
282  Ptr<EpcTft> tft = Create<EpcTft> ();
284  dlpf.localPortStart = dlPort;
285  dlpf.localPortEnd = dlPort;
286  tft->Add (dlpf);
288  ulpf.remotePortStart = ulPort;
289  ulpf.remotePortEnd = ulPort;
290  tft->Add (ulpf);
291  EpsBearer bearer (EpsBearer::NGBR_VIDEO_TCP_DEFAULT);
292  lteHelper->ActivateDedicatedEpsBearer (ueLteDevs.Get (u), bearer, tft);
293 
294  Time startTime = Seconds (startTimeSeconds->GetValue ());
295  serverApps.Start (startTime);
296  clientApps.Start (startTime);
297 
298  } // end for b
299  }
300 
301 
302  // Add X2 inteface
303  lteHelper->AddX2Interface (enbNodes);
304 
305  // X2-based Handover
306  lteHelper->HandoverRequest (Seconds (0.100), ueLteDevs.Get (0), enbLteDevs.Get (0), enbLteDevs.Get (1));
307 
308 
309  // Uncomment to enable PCAP tracing
310  //p2ph.EnablePcapAll("lena-x2-handover");
311 
312  lteHelper->EnableMacTraces ();
313  lteHelper->EnableRlcTraces ();
314  lteHelper->EnablePdcpTraces ();
315  Ptr<RadioBearerStatsCalculator> rlcStats = lteHelper->GetRlcStats ();
316  rlcStats->SetAttribute ("EpochDuration", TimeValue (Seconds (0.05)));
317  Ptr<RadioBearerStatsCalculator> pdcpStats = lteHelper->GetPdcpStats ();
318  pdcpStats->SetAttribute ("EpochDuration", TimeValue (Seconds (0.05)));
319 
320 
321  // connect custom trace sinks for RRC connection establishment and handover notification
322  Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/ConnectionEstablished",
323  MakeCallback (&NotifyConnectionEstablishedEnb));
324  Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/ConnectionEstablished",
325  MakeCallback (&NotifyConnectionEstablishedUe));
326  Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverStart",
327  MakeCallback (&NotifyHandoverStartEnb));
328  Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverStart",
329  MakeCallback (&NotifyHandoverStartUe));
330  Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverEndOk",
331  MakeCallback (&NotifyHandoverEndOkEnb));
332  Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverEndOk",
333  MakeCallback (&NotifyHandoverEndOkUe));
334 
335 
336  Simulator::Stop(Seconds(simTime));
337  Simulator::Run();
338 
339  // GtkConfigStore config;
340  // config.ConfigureAttributes();
341 
343  return 0;
344 
345 }
holds a vector of ns3::Application pointers.
uint8_t Add(PacketFilter f)
Definition: epc-tft.cc:157
keep track of time unit.
Definition: nstime.h:149
an Inet address class
static Ipv4Address GetAny(void)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Hold a bool native type.
Definition: boolean.h:38
NetDeviceContainer InstallEnbDevice(NodeContainer c)
Definition: lte-helper.cc:285
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void SetDefaultRoute(Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a default route to the static routing table.
void HandoverRequest(Time hoTime, Ptr< NetDevice > ueDev, Ptr< NetDevice > sourceEnbDev, Ptr< NetDevice > targetEnbDev)
Definition: lte-helper.cc:750
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
NetDeviceContainer Install(NodeContainer c)
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void EnableRlcTraces(void)
Definition: lte-helper.cc:829
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:728
void SetDeviceAttribute(std::string name, const AttributeValue &value)
a 3d vector
Definition: vector.h:31
void SetSchedulerType(std::string type)
Definition: lte-helper.cc:183
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
Class for representing data rates.
Definition: data-rate.h:71
void EnablePdcpTraces(void)
Definition: lte-helper.cc:1339
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
Create a client application which sends udp packets carrying a 32bit sequence number and a 64 bit tim...
hold objects of type ns3::Time
Definition: nstime.h:700
Hold an unsigned integer type.
Definition: uinteger.h:46
void Attach(NetDeviceContainer ueDevices, Ptr< NetDevice > enbDevice)
Definition: lte-helper.cc:562
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
#define NS_LOG_LOGIC(msg)
Definition: log.h:334
static void Destroy(void)
Definition: simulator.cc:121
Access to the Ipv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:75
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
void ActivateDedicatedEpsBearer(NetDeviceContainer ueDevices, EpsBearer bearer, Ptr< EpcTft > tft)
Definition: lte-helper.cc:629
keep track of a set of node pointers.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
void Install(std::string nodeName) const
void AddX2Interface(NodeContainer enbNodes)
Definition: lte-helper.cc:727
void SetChannelAttribute(std::string name, const AttributeValue &value)
Ptr< RadioBearerStatsCalculator > GetRlcStats(void)
Definition: lte-helper.cc:1333
NetDeviceContainer InstallUeDevice(NodeContainer c)
Definition: lte-helper.cc:300
Ptr< RadioBearerStatsCalculator > GetPdcpStats(void)
Definition: lte-helper.cc:1347
Helper class used to assign positions and mobility models to nodes.
void AddNetworkRouteTo(Ipv4Address network, Ipv4Mask networkMask, Ipv4Address nextHop, uint32_t interface, uint32_t metric=0)
Add a network route to the static routing table.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
void SetEpcHelper(Ptr< EpcHelper > h)
Definition: lte-helper.cc:176
Helper class that adds ns3::Ipv4StaticRouting objects.
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void EnableMacTraces(void)
Definition: lte-helper.cc:1204
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Hold an floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
Ptr< T > GetObject(void) const
Definition: object.h:332
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const