A Discrete-Event Network Simulator
API
epc-sgw-pgw-application.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.cat>
19  * Nicola Baldo <nbaldo@cttc.cat>
20  */
21 
22 
24 #include "ns3/log.h"
25 #include "ns3/mac48-address.h"
26 #include "ns3/ipv4.h"
27 #include "ns3/inet-socket-address.h"
28 #include "ns3/epc-gtpu-header.h"
29 #include "ns3/abort.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("EpcSgwPgwApplication");
34 
36 // UeInfo
38 
39 
41 {
42  NS_LOG_FUNCTION (this);
43 }
44 
45 void
46 EpcSgwPgwApplication::UeInfo::AddBearer (Ptr<EpcTft> tft, uint8_t bearerId, uint32_t teid)
47 {
48  NS_LOG_FUNCTION (this << tft << teid);
49  m_teidByBearerIdMap[bearerId] = teid;
50  return m_tftClassifier.Add (tft, teid);
51 }
52 
53 void
55 {
56  NS_LOG_FUNCTION (this << bearerId);
57  m_teidByBearerIdMap.erase (bearerId);
58 }
59 
60 uint32_t
62 {
63  NS_LOG_FUNCTION (this << p);
64  // we hardcode DOWNLINK direction since the PGW is espected to
65  // classify only downlink packets (uplink packets will go to the
66  // internet without any classification).
67  return m_tftClassifier.Classify (p, EpcTft::DOWNLINK);
68 }
69 
72 {
73  return m_enbAddr;
74 }
75 
76 void
78 {
79  m_enbAddr = enbAddr;
80 }
81 
84 {
85  return m_ueAddr;
86 }
87 
88 void
90 {
91  m_ueAddr = ueAddr;
92 }
93 
95 // EpcSgwPgwApplication
97 
98 
99 TypeId
101 {
102  static TypeId tid = TypeId ("ns3::EpcSgwPgwApplication")
103  .SetParent<Object> ()
104  .SetGroupName("Lte");
105  return tid;
106 }
107 
108 void
110 {
111  NS_LOG_FUNCTION (this);
113  m_s1uSocket = 0;
114  delete (m_s11SapSgw);
115 }
116 
117 
118 
120  : m_s1uSocket (s1uSocket),
121  m_tunDevice (tunDevice),
122  m_gtpuUdpPort (2152), // fixed by the standard
123  m_teidCount (0),
124  m_s11SapMme (0)
125 {
126  NS_LOG_FUNCTION (this << tunDevice << s1uSocket);
129 }
130 
131 
133 {
134  NS_LOG_FUNCTION (this);
135 }
136 
137 
138 bool
139 EpcSgwPgwApplication::RecvFromTunDevice (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
140 {
141  NS_LOG_FUNCTION (this << source << dest << packet << packet->GetSize ());
142 
143  // get IP address of UE
144  Ptr<Packet> pCopy = packet->Copy ();
145  Ipv4Header ipv4Header;
146  pCopy->RemoveHeader (ipv4Header);
147  Ipv4Address ueAddr = ipv4Header.GetDestination ();
148  NS_LOG_LOGIC ("packet addressed to UE " << ueAddr);
149 
150  // find corresponding UeInfo address
151  std::map<Ipv4Address, Ptr<UeInfo> >::iterator it = m_ueInfoByAddrMap.find (ueAddr);
152  if (it == m_ueInfoByAddrMap.end ())
153  {
154  NS_LOG_WARN ("unknown UE address " << ueAddr);
155  }
156  else
157  {
158  Ipv4Address enbAddr = it->second->GetEnbAddr ();
159  uint32_t teid = it->second->Classify (packet);
160  if (teid == 0)
161  {
162  NS_LOG_WARN ("no matching bearer for this packet");
163  }
164  else
165  {
166  SendToS1uSocket (packet, enbAddr, teid);
167  }
168  }
169  // there is no reason why we should notify the TUN
170  // VirtualNetDevice that he failed to send the packet: if we receive
171  // any bogus packet, it will just be silently discarded.
172  const bool succeeded = true;
173  return succeeded;
174 }
175 
176 void
178 {
179  NS_LOG_FUNCTION (this << socket);
180  NS_ASSERT (socket == m_s1uSocket);
181  Ptr<Packet> packet = socket->Recv ();
182  GtpuHeader gtpu;
183  packet->RemoveHeader (gtpu);
184  uint32_t teid = gtpu.GetTeid ();
185 
188  SocketAddressTag tag;
189  packet->RemovePacketTag (tag);
190 
191  SendToTunDevice (packet, teid);
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION (this << packet << teid);
198  NS_LOG_LOGIC (" packet size: " << packet->GetSize () << " bytes");
200 }
201 
202 void
204 {
205  NS_LOG_FUNCTION (this << packet << enbAddr << teid);
206 
207  GtpuHeader gtpu;
208  gtpu.SetTeid (teid);
209  // From 3GPP TS 29.281 v10.0.0 Section 5.1
210  // Length of the payload + the non obligatory GTP-U header
211  gtpu.SetLength (packet->GetSize () + gtpu.GetSerializedSize () - 8);
212  packet->AddHeader (gtpu);
213  uint32_t flags = 0;
214  m_s1uSocket->SendTo (packet, flags, InetSocketAddress (enbAddr, m_gtpuUdpPort));
215 }
216 
217 
218 void
220 {
221  m_s11SapMme = s;
222 }
223 
224 EpcS11SapSgw*
226 {
227  return m_s11SapSgw;
228 }
229 
230 void
231 EpcSgwPgwApplication::AddEnb (uint16_t cellId, Ipv4Address enbAddr, Ipv4Address sgwAddr)
232 {
233  NS_LOG_FUNCTION (this << cellId << enbAddr << sgwAddr);
234  EnbInfo enbInfo;
235  enbInfo.enbAddr = enbAddr;
236  enbInfo.sgwAddr = sgwAddr;
237  m_enbInfoByCellId[cellId] = enbInfo;
238 }
239 
240 void
242 {
243  NS_LOG_FUNCTION (this << imsi);
244  Ptr<UeInfo> ueInfo = Create<UeInfo> ();
245  m_ueInfoByImsiMap[imsi] = ueInfo;
246 }
247 
248 void
250 {
251  NS_LOG_FUNCTION (this << imsi << ueAddr);
252  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (imsi);
253  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << imsi);
254  m_ueInfoByAddrMap[ueAddr] = ueit->second;
255  ueit->second->SetUeAddr (ueAddr);
256 }
257 
258 void
260 {
261  NS_LOG_FUNCTION (this << req.imsi);
262  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (req.imsi);
263  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << req.imsi);
264  uint16_t cellId = req.uli.gci;
265  std::map<uint16_t, EnbInfo>::iterator enbit = m_enbInfoByCellId.find (cellId);
266  NS_ASSERT_MSG (enbit != m_enbInfoByCellId.end (), "unknown CellId " << cellId);
267  Ipv4Address enbAddr = enbit->second.enbAddr;
268  ueit->second->SetEnbAddr (enbAddr);
269 
271  res.teid = req.imsi; // trick to avoid the need for allocating TEIDs on the S11 interface
272 
273  for (std::list<EpcS11SapSgw::BearerContextToBeCreated>::iterator bit = req.bearerContextsToBeCreated.begin ();
274  bit != req.bearerContextsToBeCreated.end ();
275  ++bit)
276  {
277  // simple sanity check. If you ever need more than 4M teids
278  // throughout your simulation, you'll need to implement a smarter teid
279  // management algorithm.
280  NS_ABORT_IF (m_teidCount == 0xFFFFFFFF);
281  uint32_t teid = ++m_teidCount;
282  ueit->second->AddBearer (bit->tft, bit->epsBearerId, teid);
283 
285  bearerContext.sgwFteid.teid = teid;
286  bearerContext.sgwFteid.address = enbit->second.sgwAddr;
287  bearerContext.epsBearerId = bit->epsBearerId;
288  bearerContext.bearerLevelQos = bit->bearerLevelQos;
289  bearerContext.tft = bit->tft;
290  res.bearerContextsCreated.push_back (bearerContext);
291  }
293 
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION (this << req.teid);
300  uint64_t imsi = req.teid; // trick to avoid the need for allocating TEIDs on the S11 interface
301  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (imsi);
302  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << imsi);
303  uint16_t cellId = req.uli.gci;
304  std::map<uint16_t, EnbInfo>::iterator enbit = m_enbInfoByCellId.find (cellId);
305  NS_ASSERT_MSG (enbit != m_enbInfoByCellId.end (), "unknown CellId " << cellId);
306  Ipv4Address enbAddr = enbit->second.enbAddr;
307  ueit->second->SetEnbAddr (enbAddr);
308  // no actual bearer modification: for now we just support the minimum needed for path switch request (handover)
310  res.teid = imsi; // trick to avoid the need for allocating TEIDs on the S11 interface
313 }
314 
315 void
317 {
318  NS_LOG_FUNCTION (this << req.teid);
319  uint64_t imsi = req.teid; // trick to avoid the need for allocating TEIDs on the S11 interface
320  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (imsi);
321  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << imsi);
322 
324  res.teid = imsi;
325 
326  for (std::list<EpcS11SapSgw::BearerContextToBeRemoved>::iterator bit = req.bearerContextsToBeRemoved.begin ();
327  bit != req.bearerContextsToBeRemoved.end ();
328  ++bit)
329  {
331  bearerContext.epsBearerId = bit->epsBearerId;
332  res.bearerContextsRemoved.push_back (bearerContext);
333  }
334  //schedules Delete Bearer Request towards MME
336 }
337 
338 void
340 {
341  NS_LOG_FUNCTION (this << req.teid);
342  uint64_t imsi = req.teid; // trick to avoid the need for allocating TEIDs on the S11 interface
343  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (imsi);
344  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << imsi);
345 
346  for (std::list<EpcS11SapSgw::BearerContextRemovedSgwPgw>::iterator bit = req.bearerContextsRemoved.begin ();
347  bit != req.bearerContextsRemoved.end ();
348  ++bit)
349  {
350  //Function to remove de-activated bearer contexts from S-Gw and P-Gw side
351  ueit->second->RemoveBearer (bit->epsBearerId);
352  }
353 }
354 
355 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:266
std::map< uint16_t, EnbInfo > m_enbInfoByCellId
enum ns3::EpcS11SapMme::ModifyBearerResponseMessage::Cause cause
an Inet address class
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
3GPP TS 29.274 version 8.3.1 Release 8 section 8.28
Definition: epc-s11-sap.h:80
Delete Bearer Command message, see 3GPP TS 29.274 Release 9 V9.3.0 section 7.2.17.1.
Definition: epc-s11-sap.h:194
Ipv4Address address
Definition: epc-s11-sap.h:51
MME side of the S11 Service Access Point (SAP), provides the MME methods to be called when an S11 mes...
Definition: epc-s11-sap.h:72
uint16_t m_gtpuUdpPort
UDP port to be used for GTP.
void SetS11SapMme(EpcS11SapMme *s)
Set the MME side of the S11 SAP.
Ipv4Address GetDestination(void) const
Definition: ipv4-header.cc:304
EpcS11SapMme * m_s11SapMme
MME side of the S11 SAP.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
Delete Bearer Request message, see 3GPP TS 29.274 Release 9 V9.3.0 section 7.2.9.2.
Definition: epc-s11-sap.h:114
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
std::list< BearerContextRemovedSgwPgw > bearerContextsRemoved
Definition: epc-s11-sap.h:215
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:786
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
Callback< R > MakeNullCallback(void)
Definition: callback.h:1626
Packet addressed oo us.
Definition: net-device.h:278
std::list< BearerContextToBeRemoved > bearerContextsToBeRemoved
Definition: epc-s11-sap.h:196
void SendToTunDevice(Ptr< Packet > packet, uint32_t teid)
Send a packet to the internet via the Gi interface of the SGW/PGW.
virtual void DeleteBearerRequest(DeleteBearerRequestMessage msg)=0
As per 3GPP TS 29.274 Release 9 V9.3.0, a Delete Bearer Request message shall be sent on the S11 inte...
Delete Bearer Response message, see 3GPP TS 29.274 Release 9 V9.3.0 section 7.2.10.2.
Definition: epc-s11-sap.h:213
a polymophic address class
Definition: address.h:90
Template for the implementation of the EpcS11SapSgw as a member of an owner class of type C to which ...
Definition: epc-s11-sap.h:307
Packet header for IPv4.
Definition: ipv4-header.h:31
virtual Address GetAddress(void) const
void RemoveBearer(uint8_t bearerId)
Function, deletes contexts of bearer on SGW and PGW side.
virtual uint32_t GetSerializedSize(void) const
bool RecvFromTunDevice(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
Method to be assigned to the callback of the Gi TUN VirtualNetDevice.
Ptr< VirtualNetDevice > m_tunDevice
TUN VirtualNetDevice used for tunneling/detunneling IP packets from/to the internet over GTP-U/UDP/IP...
std::map< uint64_t, Ptr< UeInfo > > m_ueInfoByImsiMap
Map telling for each IMSI the corresponding UE info.
void DoCreateSessionRequest(EpcS11SapSgw::CreateSessionRequestMessage msg)
void SetUeAddr(Ipv4Address addr)
set the address of the UE
virtual void ModifyBearerResponse(ModifyBearerResponseMessage msg)=0
send a Modify Bearer Response message
This class implements a tag that carries an address of a packet across the socket interface...
Definition: socket.h:951
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1480
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Modify Bearer Request message, see 3GPP TS 29.274 7.2.7.
Definition: epc-s11-sap.h:227
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetTeid(uint32_t m_teid)
void AddBearer(Ptr< EpcTft > tft, uint8_t epsBearerId, uint32_t teid)
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
std::list< BearerContextCreated > bearerContextsCreated
Definition: epc-s11-sap.h:95
void SetUeAddress(uint64_t imsi, Ipv4Address ueAddr)
set the address of a previously added UE
void DoDeleteBearerResponse(EpcS11SapSgw::DeleteBearerResponseMessage req)
uint32_t GetTeid() const
void AddUe(uint64_t imsi)
Let the SGW be aware of a new UE.
void SendToS1uSocket(Ptr< Packet > packet, Ipv4Address enbS1uAddress, uint32_t teid)
Send a packet to the SGW via the S1-U interface.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
Ptr< Socket > m_s1uSocket
UDP socket to send and receive GTP-U packets to and from the S1-U interface.
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
std::map< Ipv4Address, Ptr< UeInfo > > m_ueInfoByAddrMap
Map telling for each UE address the corresponding UE info.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void DoDeleteBearerCommand(EpcS11SapSgw::DeleteBearerCommandMessage req)
SGW side of the S11 Service Access Point (SAP), provides the SGW methods to be called when an S11 mes...
Definition: epc-s11-sap.h:156
std::list< BearerContextToBeCreated > bearerContextsToBeCreated
Definition: epc-s11-sap.h:176
virtual void CreateSessionResponse(CreateSessionResponseMessage msg)=0
send a Create Session Response message
void SetEnbAddr(Ipv4Address addr)
set the address of the eNB to which the UE is connected
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:829
virtual void DoDispose()
Destructor implementation.
void SetLength(uint16_t m_length)
void RecvFromS1uSocket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the S1-U socket.
virtual ~EpcSgwPgwApplication(void)
Destructor.
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
A base class which provides memory management and object aggregation.
Definition: object.h:87
Modify Bearer Response message, see 3GPP TS 29.274 7.2.7.
Definition: epc-s11-sap.h:131
void DoModifyBearerRequest(EpcS11SapSgw::ModifyBearerRequestMessage msg)
EpcS11SapSgw * m_s11SapSgw
SGW side of the S11 SAP.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
void AddEnb(uint16_t cellId, Ipv4Address enbAddr, Ipv4Address sgwAddr)
Let the SGW be aware of a new eNB.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:255
Create Session Request message, see 3GPP TS 29.274 7.2.1.
Definition: epc-s11-sap.h:172
EpcSgwPgwApplication(const Ptr< VirtualNetDevice > tunDevice, const Ptr< Socket > s1uSocket)
Constructor that binds the tap device to the callback methods.
Implementation of the GTPv1-U Release 10 as per 3Gpp TS 29.281 document.
Create Session Response message, see 3GPP TS 29.274 7.2.2.
Definition: epc-s11-sap.h:93