1 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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: Gaurav Sathe <gaurav.sathe@tcs.com>
19  */
20 
21 #include "ns3/lte-helper.h"
22 #include "ns3/epc-helper.h"
23 #include "ns3/core-module.h"
24 #include "ns3/network-module.h"
25 #include "ns3/ipv4-global-routing-helper.h"
26 #include "ns3/internet-module.h"
27 #include "ns3/mobility-module.h"
28 #include "ns3/lte-module.h"
29 #include "ns3/applications-module.h"
30 #include "ns3/point-to-point-helper.h"
31 #include "ns3/config-store.h"
32 //#include "ns3/gtk-config-store.h"
33 
34 using namespace ns3;
35 
36 /**
37  * Sample simulation script for LTE+EPC. It instantiates one eNodeB,
38  * attaches three UE to eNodeB starts a flow for each UE to  and from a remote host.
39  * It also instantiates one dedicated bearer per UE
40  */
41 NS_LOG_COMPONENT_DEFINE ("BearerDeactivateExample");
42 int
main(int argc,char * argv[])43 main (int argc, char *argv[])
44 {
45 
46   uint16_t numberOfNodes = 1;
47   uint16_t numberOfUeNodes = 3;
48   double simTime = 1.1;
49   double distance = 60.0;
50   double interPacketInterval = 100;
51 
52   // Command line arguments
53   CommandLine cmd (__FILE__);
54   cmd.AddValue ("numberOfNodes", "Number of eNodeBs + UE pairs", numberOfNodes);
55   cmd.AddValue ("simTime", "Total duration of the simulation [s])", simTime);
56   cmd.AddValue ("distance", "Distance between eNBs [m]", distance);
57   cmd.AddValue ("interPacketInterval", "Inter packet interval [ms])", interPacketInterval);
58   cmd.Parse (argc, argv);
59 
60   Ptr<LteHelper> lteHelper = CreateObject<LteHelper> ();
61   Ptr<PointToPointEpcHelper>  epcHelper = CreateObject<PointToPointEpcHelper> ();
62   lteHelper->SetEpcHelper (epcHelper);
63 
64   ConfigStore inputConfig;
65   inputConfig.ConfigureDefaults ();
66 
67   // parse again so you can override default values from the command line
68   cmd.Parse (argc, argv);
69 
70   Ptr<Node> pgw = epcHelper->GetPgwNode ();
71 
72   // Enable Logging
73   LogLevel logLevel = (LogLevel)(LOG_PREFIX_FUNC | LOG_PREFIX_TIME | LOG_LEVEL_ALL);
74 
75   LogComponentEnable ("BearerDeactivateExample", LOG_LEVEL_ALL);
76   LogComponentEnable ("LteHelper", logLevel);
77   LogComponentEnable ("EpcHelper", logLevel);
78   LogComponentEnable ("EpcEnbApplication", logLevel);
79   LogComponentEnable ("EpcMmeApplication", logLevel);
80   LogComponentEnable ("EpcPgwApplication", logLevel);
81   LogComponentEnable ("EpcSgwApplication", logLevel);
82   LogComponentEnable ("LteEnbRrc", logLevel);
83 
84 
85   // Create a single RemoteHost
86   NodeContainer remoteHostContainer;
87   remoteHostContainer.Create (1);
88   Ptr<Node> remoteHost = remoteHostContainer.Get (0);
89   InternetStackHelper internet;
90   internet.Install (remoteHostContainer);
91 
92   // Create the Internet
93   PointToPointHelper p2ph;
94   p2ph.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Gb/s")));
95   p2ph.SetDeviceAttribute ("Mtu", UintegerValue (1500));
96   p2ph.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
97   NetDeviceContainer internetDevices = p2ph.Install (pgw, remoteHost);
98   Ipv4AddressHelper ipv4h;
99   ipv4h.SetBase ("1.0.0.0", "255.0.0.0");
100   Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign (internetDevices);
101   // interface 0 is localhost, 1 is the p2p device
102   Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress (1);
103 
104   Ipv4StaticRoutingHelper ipv4RoutingHelper;
105   Ptr<Ipv4StaticRouting> remoteHostStaticRouting = ipv4RoutingHelper.GetStaticRouting (remoteHost->GetObject<Ipv4> ());
106   remoteHostStaticRouting->AddNetworkRouteTo (Ipv4Address ("7.0.0.0"), Ipv4Mask ("255.0.0.0"), 1);
107 
108   NodeContainer ueNodes;
109   NodeContainer enbNodes;
110   enbNodes.Create (numberOfNodes);
111   ueNodes.Create (numberOfUeNodes);
112 
113   // Install Mobility Model
114   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
115   for (uint16_t i = 0; i < numberOfNodes; i++)
116     {
117       positionAlloc->Add (Vector (distance * i, 0, 0));
118     }
119   MobilityHelper mobility;
120   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
121   mobility.SetPositionAllocator (positionAlloc);
122   mobility.Install (enbNodes);
123   mobility.Install (ueNodes);
124 
125   // Install LTE Devices to the nodes
126   NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice (enbNodes);
127   NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice (ueNodes);
128 
129   // Install the IP stack on the UEs
130   internet.Install (ueNodes);
131   Ipv4InterfaceContainer ueIpIface;
132   ueIpIface = epcHelper->AssignUeIpv4Address (NetDeviceContainer (ueLteDevs));
133   // Assign IP address to UEs, and install applications
134   for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
135     {
136       Ptr<Node> ueNode = ueNodes.Get (u);
137       // Set the default gateway for the UE
138       Ptr<Ipv4StaticRouting> ueStaticRouting = ipv4RoutingHelper.GetStaticRouting (ueNode->GetObject<Ipv4> ());
139       ueStaticRouting->SetDefaultRoute (epcHelper->GetUeDefaultGatewayAddress (), 1);
140     }
141 
142   // Attach a UE to a eNB
143   lteHelper->Attach (ueLteDevs, enbLteDevs.Get (0));
144 
145   // Activate an EPS bearer on all UEs
146 
147   for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
148     {
149       Ptr<NetDevice> ueDevice = ueLteDevs.Get (u);
150       GbrQosInformation qos;
151       qos.gbrDl = 132;  // bit/s, considering IP, UDP, RLC, PDCP header size
152       qos.gbrUl = 132;
153       qos.mbrDl = qos.gbrDl;
154       qos.mbrUl = qos.gbrUl;
155 
156       enum EpsBearer::Qci q = EpsBearer::GBR_CONV_VOICE;
157       EpsBearer bearer (q, qos);
158       bearer.arp.priorityLevel = 15 - (u + 1);
159       bearer.arp.preemptionCapability = true;
160       bearer.arp.preemptionVulnerability = true;
161       lteHelper->ActivateDedicatedEpsBearer (ueDevice, bearer, EpcTft::Default ());
162     }
163 
164 
165   // Install and start applications on UEs and remote host
166   uint16_t dlPort = 1234;
167   uint16_t ulPort = 2000;
168   uint16_t otherPort = 3000;
169   ApplicationContainer clientApps;
170   ApplicationContainer serverApps;
171   for (uint32_t u = 0; u < ueNodes.GetN (); ++u)
172     {
173       ++ulPort;
174       ++otherPort;
175       PacketSinkHelper dlPacketSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), dlPort));
176       PacketSinkHelper ulPacketSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), ulPort));
177       PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), otherPort));
178       serverApps.Add (dlPacketSinkHelper.Install (ueNodes.Get (u)));
179       serverApps.Add (ulPacketSinkHelper.Install (remoteHost));
180       serverApps.Add (packetSinkHelper.Install (ueNodes.Get (u)));
181 
182       UdpClientHelper dlClient (ueIpIface.GetAddress (u), dlPort);
183       dlClient.SetAttribute ("Interval", TimeValue (MilliSeconds (interPacketInterval)));
184       dlClient.SetAttribute ("MaxPackets", UintegerValue (1000000));
185 
186       UdpClientHelper ulClient (remoteHostAddr, ulPort);
187       ulClient.SetAttribute ("Interval", TimeValue (MilliSeconds (interPacketInterval)));
188       ulClient.SetAttribute ("MaxPackets", UintegerValue (1000000));
189 
190       UdpClientHelper client (ueIpIface.GetAddress (u), otherPort);
191       client.SetAttribute ("Interval", TimeValue (MilliSeconds (interPacketInterval)));
192       client.SetAttribute ("MaxPackets", UintegerValue (1000000));
193 
194       clientApps.Add (dlClient.Install (remoteHost));
195       clientApps.Add (ulClient.Install (ueNodes.Get (u)));
196       if (u + 1 < ueNodes.GetN ())
197         {
198           clientApps.Add (client.Install (ueNodes.Get (u + 1)));
199         }
200       else
201         {
202           clientApps.Add (client.Install (ueNodes.Get (0)));
203         }
204     }
205 
206   serverApps.Start (Seconds (0.030));
207   clientApps.Start (Seconds (0.030));
208 
209   double statsStartTime = 0.04; // need to allow for RRC connection establishment + SRS
210   double statsDuration = 1.0;
211 
212   lteHelper->EnableRlcTraces ();
213   Ptr<RadioBearerStatsCalculator> rlcStats = lteHelper->GetRlcStats ();
214   rlcStats->SetAttribute ("StartTime", TimeValue (Seconds (statsStartTime)));
215   rlcStats->SetAttribute ("EpochDuration", TimeValue (Seconds (statsDuration)));
216 
217   //get ue device pointer for UE-ID 0 IMSI 1 and enb device pointer
218   Ptr<NetDevice> ueDevice = ueLteDevs.Get (0);
219   Ptr<NetDevice> enbDevice = enbLteDevs.Get (0);
220 
221   /*
222    *   Instantiate De-activation using Simulator::Schedule() method which will initiate bearer de-activation after deActivateTime
223    *   Instantiate De-activation in sequence (Time const &time, MEM mem_ptr, OBJ obj, T1 a1, T2 a2, T3 a3)
224    */
225   Time deActivateTime (Seconds (1.5));
226   Simulator::Schedule (deActivateTime, &LteHelper::DeActivateDedicatedEpsBearer, lteHelper, ueDevice, enbDevice, 2);
227 
228   //stop simulation after 3 seconds
229   Simulator::Stop (Seconds (3.0));
230 
231   Simulator::Run ();
232   /*GtkConfigStore config;
233   config.ConfigureAttributes();*/
234 
235   Simulator::Destroy ();
236   return 0;
237 
238 }
239