A Discrete-Event Network Simulator
API
lena-simple-epc-emu.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 2013 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Jaume Nin <jaume.nin@cttc.cat>
18  * Nicola Baldo <nbaldo@cttc.cat>
19  */
20 
21 #include "ns3/applications-module.h"
22 #include "ns3/config-store.h"
23 #include "ns3/core-module.h"
24 #include "ns3/epc-helper.h"
25 #include "ns3/internet-module.h"
26 #include "ns3/ipv4-global-routing-helper.h"
27 #include "ns3/lte-helper.h"
28 #include "ns3/lte-module.h"
29 #include "ns3/mobility-module.h"
30 #include "ns3/network-module.h"
31 #include "ns3/point-to-point-helper.h"
32 
33 using namespace ns3;
34 
35 /*
36  * Simple simulation program using the emulated EPC.
37  * For the LTE radio part, it simulates a simple linear topology with
38  * a fixed number of eNBs spaced at equal distance, and a fixed number
39  * of UEs per each eNB, located at the same position of the eNB.
40  * For the EPC, it uses EmuEpcHelper to realize the S1-U connection
41  * via a real link.
42  */
43 
44 NS_LOG_COMPONENT_DEFINE("EpcFirstExample");
45 
46 int
47 main(int argc, char* argv[])
48 {
49  uint16_t nEnbs = 1;
50  uint16_t nUesPerEnb = 1;
51  double simTime = 10.1;
52  double distance = 1000.0;
53  double interPacketInterval = 1000;
54 
55  // Command line arguments
56  CommandLine cmd(__FILE__);
57  cmd.AddValue("nEnbs", "Number of eNBs", nEnbs);
58  cmd.AddValue("nUesPerEnb", "Number of UEs per eNB", nUesPerEnb);
59  cmd.AddValue("simTime", "Total duration of the simulation [s])", simTime);
60  cmd.AddValue("distance", "Distance between eNBs [m]", distance);
61  cmd.AddValue("interPacketInterval", "Inter packet interval [ms])", interPacketInterval);
62  cmd.Parse(argc, argv);
63 
64  // let's go in real time
65  // NOTE: if you go in real time I strongly advise to use
66  // --ns3::RealtimeSimulatorImpl::SynchronizationMode=HardLimit
67  // I've seen that if BestEffort is used things can break
68  // (even simple stuff such as ARP)
69  // GlobalValue::Bind ("SimulatorImplementationType",
70  // StringValue ("ns3::RealtimeSimulatorImpl"));
71 
72  // let's speed things up, we don't need these details for this scenario
73  Config::SetDefault("ns3::LteSpectrumPhy::CtrlErrorModelEnabled", BooleanValue(false));
74  Config::SetDefault("ns3::LteSpectrumPhy::DataErrorModelEnabled", BooleanValue(false));
75 
76  GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
77 
78  ConfigStore inputConfig;
79  inputConfig.ConfigureDefaults();
80 
81  // parse again so you can override default values from the command line
82  cmd.Parse(argc, argv);
83 
84  Ptr<LteHelper> lteHelper = CreateObject<LteHelper>();
85  Ptr<EmuEpcHelper> epcHelper = CreateObject<EmuEpcHelper>();
86  lteHelper->SetEpcHelper(epcHelper);
87  epcHelper->Initialize();
88 
89  Ptr<Node> pgw = epcHelper->GetPgwNode();
90 
91  // Create a single RemoteHost
92  NodeContainer remoteHostContainer;
93  remoteHostContainer.Create(1);
94  Ptr<Node> remoteHost = remoteHostContainer.Get(0);
96  internet.Install(remoteHostContainer);
97 
98  // Create the Internet
99  PointToPointHelper p2ph;
100  p2ph.SetDeviceAttribute("DataRate", DataRateValue(DataRate("100Gb/s")));
101  p2ph.SetDeviceAttribute("Mtu", UintegerValue(1500));
102  p2ph.SetChannelAttribute("Delay", TimeValue(Seconds(0.010)));
103  NetDeviceContainer internetDevices = p2ph.Install(pgw, remoteHost);
104  Ipv4AddressHelper ipv4h;
105  ipv4h.SetBase("1.0.0.0", "255.0.0.0");
106  Ipv4InterfaceContainer internetIpIfaces = ipv4h.Assign(internetDevices);
107  // interface 0 is localhost, 1 is the p2p device
108  Ipv4Address remoteHostAddr = internetIpIfaces.GetAddress(1);
109 
110  Ipv4StaticRoutingHelper ipv4RoutingHelper;
111  Ptr<Ipv4StaticRouting> remoteHostStaticRouting =
112  ipv4RoutingHelper.GetStaticRouting(remoteHost->GetObject<Ipv4>());
113  remoteHostStaticRouting->AddNetworkRouteTo(Ipv4Address("7.0.0.0"), Ipv4Mask("255.0.0.0"), 1);
114 
115  NodeContainer ueNodes;
116  NodeContainer enbNodes;
117  enbNodes.Create(nEnbs);
118  ueNodes.Create(nEnbs * nUesPerEnb);
119 
120  // Install Mobility Model
121  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
122  for (uint16_t i = 0; i < nEnbs; i++)
123  {
124  positionAlloc->Add(Vector(distance * i, 0, 0));
125  }
127  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
128  mobility.SetPositionAllocator(positionAlloc);
129  mobility.Install(enbNodes);
130  mobility.Install(ueNodes);
131 
132  // Install LTE Devices to the nodes
133  NetDeviceContainer enbLteDevs = lteHelper->InstallEnbDevice(enbNodes);
134  NetDeviceContainer ueLteDevs = lteHelper->InstallUeDevice(ueNodes);
135 
136  // Install the IP stack on the UEs
137  internet.Install(ueNodes);
138  Ipv4InterfaceContainer ueIpIface;
139  ueIpIface = epcHelper->AssignUeIpv4Address(NetDeviceContainer(ueLteDevs));
140  // Assign IP address to UEs, and install applications
141  for (uint32_t u = 0; u < ueNodes.GetN(); ++u)
142  {
143  Ptr<Node> ueNode = ueNodes.Get(u);
144  // Set the default gateway for the UE
145  Ptr<Ipv4StaticRouting> ueStaticRouting =
146  ipv4RoutingHelper.GetStaticRouting(ueNode->GetObject<Ipv4>());
147  ueStaticRouting->SetDefaultRoute(epcHelper->GetUeDefaultGatewayAddress(), 1);
148  }
149 
150  lteHelper->Attach(ueLteDevs);
151  // side effects: 1) use idle mode cell selection, 2) activate default EPS bearer
152 
153  // randomize a bit start times to avoid simulation artifacts
154  // (e.g., buffer overflows due to packet transmissions happening
155  // exactly at the same time)
156  Ptr<UniformRandomVariable> startTimeSeconds = CreateObject<UniformRandomVariable>();
157  startTimeSeconds->SetAttribute("Min", DoubleValue(0));
158  startTimeSeconds->SetAttribute("Max", DoubleValue(interPacketInterval / 1000.0));
159 
160  // Install and start applications on UEs and remote host
161  uint16_t dlPort = 1234;
162  uint16_t ulPort = 2000;
163  for (uint32_t u = 0; u < ueNodes.GetN(); ++u)
164  {
165  ++ulPort;
168 
169  PacketSinkHelper dlPacketSinkHelper("ns3::UdpSocketFactory",
171  PacketSinkHelper ulPacketSinkHelper("ns3::UdpSocketFactory",
173  serverApps.Add(dlPacketSinkHelper.Install(ueNodes.Get(u)));
174  serverApps.Add(ulPacketSinkHelper.Install(remoteHost));
175 
176  UdpClientHelper dlClient(ueIpIface.GetAddress(u), dlPort);
177  dlClient.SetAttribute("Interval", TimeValue(MilliSeconds(interPacketInterval)));
178  dlClient.SetAttribute("MaxPackets", UintegerValue(1000000));
179 
180  UdpClientHelper ulClient(remoteHostAddr, ulPort);
181  ulClient.SetAttribute("Interval", TimeValue(MilliSeconds(interPacketInterval)));
182  ulClient.SetAttribute("MaxPackets", UintegerValue(1000000));
183 
184  clientApps.Add(dlClient.Install(remoteHost));
185  clientApps.Add(ulClient.Install(ueNodes.Get(u)));
186 
187  serverApps.Start(Seconds(startTimeSeconds->GetValue()));
188  clientApps.Start(Seconds(startTimeSeconds->GetValue()));
189  }
190 
191  Simulator::Stop(Seconds(simTime));
192  Simulator::Run();
193 
195  return 0;
196 }
holds a vector of ns3::Application pointers.
Parse command-line arguments.
Definition: command-line.h:232
void ConfigureDefaults()
Configure the default values.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
static void Bind(std::string name, const AttributeValue &value)
Iterate over the set of GlobalValues until a matching name is found and then set its value with Globa...
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.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
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
Definition: ipv4-address.h:257
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...
void SetEpcHelper(Ptr< EpcHelper > h)
Set the EpcHelper to be used to setup the EPC network in conjunction with the setup of the LTE radio ...
Definition: lte-helper.cc:285
NetDeviceContainer InstallEnbDevice(NodeContainer c)
Create a set of eNodeB devices.
Definition: lte-helper.cc:485
void Attach(NetDeviceContainer ueDevices)
Enables automatic attachment of a set of UE devices to a suitable cell using Idle mode initial cell s...
Definition: lte-helper.cc:1039
NetDeviceContainer InstallUeDevice(NodeContainer c)
Create a set of UE devices.
Definition: lte-helper.cc:500
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
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.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:204
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
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)
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:45
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
serverApps
Definition: first.py:54
clientApps
Definition: first.py:64
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
mobility
Definition: third.py:105