A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
epc-helper.cc
Go to the documentation of this file.
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: Jaume Nin <jnin@cttc.es>
19  * Nicola Baldo <nbaldo@cttc.es>
20  */
21 
22 #include <ns3/epc-helper.h>
23 #include <ns3/log.h>
24 #include <ns3/inet-socket-address.h>
25 #include <ns3/mac48-address.h>
26 #include <ns3/eps-bearer.h>
27 #include <ns3/ipv4-address.h>
28 #include <ns3/internet-stack-helper.h>
29 #include <ns3/point-to-point-helper.h>
30 #include <ns3/packet-socket-helper.h>
31 #include <ns3/packet-socket-address.h>
32 #include <ns3/epc-enb-application.h>
33 #include <ns3/epc-sgw-pgw-application.h>
34 
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("EpcHelper");
39 
40 NS_OBJECT_ENSURE_REGISTERED (EpcHelper);
41 
43  : m_gtpuUdpPort (2152) // fixed by the standard
44 {
45  NS_LOG_FUNCTION (this);
46 
47  // since we use point-to-point links for all S1-U links,
48  // we use a /30 subnet which can hold exactly two addresses
49  // (remember that net broadcast and null address are not valid)
50  m_s1uIpv4AddressHelper.SetBase ("10.0.0.0", "255.255.255.252");
51 
52  // we use a /8 net for all UEs
53  m_ueAddressHelper.SetBase ("7.0.0.0", "255.0.0.0");
54 
55  // create SgwPgwNode
56  m_sgwPgw = CreateObject<Node> ();
57  InternetStackHelper internet;
58  internet.Install (m_sgwPgw);
59 
60  // create S1-U socket
61  Ptr<Socket> sgwPgwS1uSocket = Socket::CreateSocket (m_sgwPgw, TypeId::LookupByName ("ns3::UdpSocketFactory"));
62  int retval = sgwPgwS1uSocket->Bind (InetSocketAddress (Ipv4Address::GetAny (), m_gtpuUdpPort));
63  NS_ASSERT (retval == 0);
64 
65  // create TUN device implementing tunneling of user data over GTP-U/UDP/IP
66  m_tunDevice = CreateObject<VirtualNetDevice> ();
67  // allow jumbo packets
68  m_tunDevice->SetAttribute ("Mtu", UintegerValue (30000));
69 
70  // yes we need this
72 
74  NetDeviceContainer tunDeviceContainer;
75  tunDeviceContainer.Add (m_tunDevice);
76 
77  // the TUN device is on the same subnet as the UEs, so when a packet
78  // addressed to an UE arrives at the intenet to the WAN interface of
79  // the PGW it will be forwarded to the TUN device.
80  Ipv4InterfaceContainer tunDeviceIpv4IfContainer = m_ueAddressHelper.Assign (tunDeviceContainer);
81 
82  // create EpcSgwPgwApplication
83  m_sgwPgwApp = CreateObject<EpcSgwPgwApplication> (m_tunDevice, sgwPgwS1uSocket);
85 
86  // connect SgwPgwApplication and virtual net device for tunneling
87  m_tunDevice->SetSendCallback (MakeCallback (&EpcSgwPgwApplication::RecvFromTunDevice, m_sgwPgwApp));
88 
89 }
90 
92 {
93  NS_LOG_FUNCTION (this);
94 }
95 
96 TypeId
98 {
99  static TypeId tid = TypeId ("ns3::EpcHelper")
100  .SetParent<Object> ()
101  .AddConstructor<EpcHelper> ()
102  .AddAttribute ("S1uLinkDataRate",
103  "The data rate to be used for the next S1-U link to be created",
104  DataRateValue (DataRate ("10Gb/s")),
105  MakeDataRateAccessor (&EpcHelper::m_s1uLinkDataRate),
106  MakeDataRateChecker ())
107  .AddAttribute ("S1uLinkDelay",
108  "The delay to be used for the next S1-U link to be created",
109  TimeValue (Seconds (0)),
110  MakeTimeAccessor (&EpcHelper::m_s1uLinkDelay),
111  MakeTimeChecker ())
112  .AddAttribute ("S1uLinkMtu",
113  "The MTU of the next S1-U link to be created. Note that, because of the additional GTP/UDP/IP tunneling overhead, you need a MTU larger than the end-to-end MTU that you want to support.",
114  UintegerValue (2000),
115  MakeUintegerAccessor (&EpcHelper::m_s1uLinkMtu),
116  MakeUintegerChecker<uint16_t> ())
117  ;
118  return tid;
119 }
120 
121 void
123 {
124  m_tunDevice->SetSendCallback (MakeNullCallback<bool, Ptr<Packet>, const Address&, const Address&, uint16_t> ());
125  m_tunDevice = 0;
126  m_sgwPgwApp = 0;
127  m_sgwPgw->Dispose ();
128 }
129 
130 
131 void
133 {
134  NS_LOG_FUNCTION (this << enb << lteEnbNetDevice);
135 
136  NS_ASSERT (enb == lteEnbNetDevice->GetNode ());
137 
138  // add an IPv4 stack to the previously created eNB
139  InternetStackHelper internet;
140  internet.Install (enb);
141  NS_LOG_LOGIC ("number of Ipv4 ifaces of the eNB after node creation: " << enb->GetObject<Ipv4> ()->GetNInterfaces ());
142 
143  // create a point to point link between the new eNB and the SGW with
144  // the corresponding new NetDevices on each side
145  NodeContainer enbSgwNodes;
146  enbSgwNodes.Add (m_sgwPgw);
147  enbSgwNodes.Add (enb);
148  PointToPointHelper p2ph;
151  p2ph.SetChannelAttribute ("Delay", TimeValue (m_s1uLinkDelay));
152  NetDeviceContainer enbSgwDevices = p2ph.Install (enb, m_sgwPgw);
153  NS_LOG_LOGIC ("number of Ipv4 ifaces of the eNB after installing p2p dev: " << enb->GetObject<Ipv4> ()->GetNInterfaces ());
154  Ptr<NetDevice> enbDev = enbSgwDevices.Get (0);
155  Ptr<NetDevice> sgwDev = enbSgwDevices.Get (1);
157  Ipv4InterfaceContainer enbSgwIpIfaces = m_s1uIpv4AddressHelper.Assign (enbSgwDevices);
158  NS_LOG_LOGIC ("number of Ipv4 ifaces of the eNB after assigning Ipv4 addr to S1 dev: " << enb->GetObject<Ipv4> ()->GetNInterfaces ());
159 
160  Ipv4Address enbAddress = enbSgwIpIfaces.GetAddress (0);
161  Ipv4Address sgwAddress = enbSgwIpIfaces.GetAddress (1);
162 
163  // create S1-U socket for the ENB
164  Ptr<Socket> enbS1uSocket = Socket::CreateSocket (enb, TypeId::LookupByName ("ns3::UdpSocketFactory"));
165  int retval = enbS1uSocket->Bind (InetSocketAddress (enbAddress, m_gtpuUdpPort));
166  NS_ASSERT (retval == 0);
167 
168 
169  // give PacketSocket powers to the eNB
170  //PacketSocketHelper packetSocket;
171  //packetSocket.Install (enb);
172 
173  // create LTE socket for the ENB
174  Ptr<Socket> enbLteSocket = Socket::CreateSocket (enb, TypeId::LookupByName ("ns3::PacketSocketFactory"));
175  PacketSocketAddress enbLteSocketBindAddress;
176  enbLteSocketBindAddress.SetSingleDevice (lteEnbNetDevice->GetIfIndex ());
177  enbLteSocketBindAddress.SetProtocol (Ipv4L3Protocol::PROT_NUMBER);
178  retval = enbLteSocket->Bind (enbLteSocketBindAddress);
179  NS_ASSERT (retval == 0);
180  PacketSocketAddress enbLteSocketConnectAddress;
181  enbLteSocketConnectAddress.SetPhysicalAddress (Mac48Address::GetBroadcast ());
182  enbLteSocketConnectAddress.SetSingleDevice (lteEnbNetDevice->GetIfIndex ());
183  enbLteSocketConnectAddress.SetProtocol (Ipv4L3Protocol::PROT_NUMBER);
184  retval = enbLteSocket->Connect (enbLteSocketConnectAddress);
185  NS_ASSERT (retval == 0);
186 
187 
188  NS_LOG_INFO ("create EpcEnbApplication");
189  Ptr<EpcEnbApplication> enbApp = CreateObject<EpcEnbApplication> (enbLteSocket, enbS1uSocket, sgwAddress);
190  enb->AddApplication (enbApp);
191  NS_ASSERT (enb->GetNApplications () == 1);
192  NS_ASSERT_MSG (enb->GetApplication (0)->GetObject<EpcEnbApplication> () != 0, "cannot retrieve EpcEnbApplication");
193  NS_LOG_LOGIC ("enb: " << enb << ", enb->GetApplication (0): " << enb->GetApplication (0));
194 
195 }
196 
197 
198 void
199 EpcHelper::ActivateEpsBearer (Ptr<NetDevice> ueLteDevice, Ptr<NetDevice> enbLteDevice, Ptr<EpcTft> tft, uint16_t rnti, uint8_t lcid)
200 {
201  Ptr<Node> ueNode = ueLteDevice->GetNode ();
202  Ptr<Ipv4> ueIpv4 = ueNode->GetObject<Ipv4> ();
203  int32_t interface = ueIpv4->GetInterfaceForDevice (ueLteDevice);
204  NS_ASSERT (interface >= 0);
205  NS_ASSERT (ueIpv4->GetNAddresses (interface) == 1);
206  Ipv4Address ueAddr = ueIpv4->GetAddress (interface, 0).GetLocal ();
207  NS_LOG_LOGIC (" UE IP address: " << ueAddr);
208 
209  // NOTE: unlike ueLteDevice, enbLteDevice is NOT an Ipv4 enabled
210  // device. In fact we are interested in the S1 device of the eNB.
211  // We find it by relying on the assumption that the S1 device is the
212  // only Ipv4 enabled device of the eNB besides the localhost interface.
213  Ptr<Node> enbNode = enbLteDevice->GetNode ();
214  NS_ASSERT (enbNode != 0);
215  Ptr<Ipv4> enbIpv4 = enbNode->GetObject<Ipv4> ();
216  NS_LOG_LOGIC ("number of Ipv4 ifaces of the eNB: " << enbIpv4->GetNInterfaces ());
217  // two ifaces total: loopback + the S1-U interface
218  NS_ASSERT (enbIpv4->GetNInterfaces () == 2);
219  NS_ASSERT (ueIpv4->GetNAddresses (1) == 1);
220  // iface index 0 is loopback, index 1 is the S1-U interface
221  Ipv4Address enbAddr = enbIpv4->GetAddress (1, 0).GetLocal ();
222  NS_LOG_LOGIC (" ENB IP address: " << enbAddr);
223 
224  // setup S1 bearer at EpcSgwPgwApplication
225  uint32_t teid = m_sgwPgwApp->ActivateS1Bearer (ueAddr, enbAddr, tft);
226 
227  // setup S1 bearer at EpcEnbApplication
228  NS_LOG_LOGIC ("enb: " << enbNode << ", enb->GetApplication (0): " << enbNode->GetApplication (0));
229  NS_ASSERT (enbNode->GetNApplications () == 1);
230  Ptr<Application> app = enbNode->GetApplication (0);
231  NS_ASSERT (app != 0);
232  Ptr<EpcEnbApplication> epcEnbApp = app->GetObject<EpcEnbApplication> ();
233  NS_ASSERT (epcEnbApp != 0);
234  epcEnbApp->ErabSetupRequest (teid, rnti, lcid);
235 
236 
237 }
238 
239 
240 Ptr<Node>
242 {
243  return m_sgwPgw;
244 }
245 
246 
249 {
250  return m_ueAddressHelper.Assign (ueDevices);
251 }
252 
253 
254 
257 {
258  // return the address of the tun device
259  return m_sgwPgw->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
260 }
261 
262 
263 } // namespace ns3