A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
lena-x2-handover.cc
Go to the documentation of this file.
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);
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",
324  Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/ConnectionEstablished",
326  Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverStart",
328  Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverStart",
330  Config::Connect ("/NodeList/*/DeviceList/*/LteEnbRrc/HandoverEndOk",
332  Config::Connect ("/NodeList/*/DeviceList/*/LteUeRrc/HandoverEndOk",
334 
335 
336  Simulator::Stop(Seconds(simTime));
337  Simulator::Run();
338 
339  // GtkConfigStore config;
340  // config.ConfigureAttributes();
341 
343  return 0;
344 
345 }