A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  ;
35 
36 
38 // UeInfo
40 
41 
43 {
44  NS_LOG_FUNCTION (this);
45 }
46 
47 void
48 EpcSgwPgwApplication::UeInfo::AddBearer (Ptr<EpcTft> tft, uint8_t bearerId, uint32_t teid)
49 {
50  NS_LOG_FUNCTION (this << tft << teid);
51  m_teidByBearerIdMap[bearerId] = teid;
52  return m_tftClassifier.Add (tft, teid);
53 }
54 
55 uint32_t
57 {
58  NS_LOG_FUNCTION (this << p);
59  // we hardcode DOWNLINK direction since the PGW is espected to
60  // classify only downlink packets (uplink packets will go to the
61  // internet without any classification).
62  return m_tftClassifier.Classify (p, EpcTft::DOWNLINK);
63 }
64 
67 {
68  return m_enbAddr;
69 }
70 
71 void
73 {
74  m_enbAddr = enbAddr;
75 }
76 
79 {
80  return m_ueAddr;
81 }
82 
83 void
85 {
86  m_ueAddr = ueAddr;
87 }
88 
90 // EpcSgwPgwApplication
92 
93 
94 TypeId
96 {
97  static TypeId tid = TypeId ("ns3::EpcSgwPgwApplication")
98  .SetParent<Object> ();
99  return tid;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this);
107  m_s1uSocket = 0;
108  delete (m_s11SapSgw);
109 }
110 
111 
112 
114  : m_s1uSocket (s1uSocket),
115  m_tunDevice (tunDevice),
116  m_gtpuUdpPort (2152), // fixed by the standard
117  m_teidCount (0),
118  m_s11SapMme (0)
119 {
120  NS_LOG_FUNCTION (this << tunDevice << s1uSocket);
123 }
124 
125 
127 {
128  NS_LOG_FUNCTION (this);
129 }
130 
131 
132 bool
133 EpcSgwPgwApplication::RecvFromTunDevice (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
134 {
135  NS_LOG_FUNCTION (this << source << dest << packet << packet->GetSize ());
136 
137  // get IP address of UE
138  Ptr<Packet> pCopy = packet->Copy ();
139  Ipv4Header ipv4Header;
140  pCopy->RemoveHeader (ipv4Header);
141  Ipv4Address ueAddr = ipv4Header.GetDestination ();
142  NS_LOG_LOGIC ("packet addressed to UE " << ueAddr);
143 
144  // find corresponding UeInfo address
145  std::map<Ipv4Address, Ptr<UeInfo> >::iterator it = m_ueInfoByAddrMap.find (ueAddr);
146  if (it == m_ueInfoByAddrMap.end ())
147  {
148  NS_LOG_WARN ("unknown UE address " << ueAddr) ;
149  }
150  else
151  {
152  Ipv4Address enbAddr = it->second->GetEnbAddr ();
153  uint32_t teid = it->second->Classify (packet);
154  if (teid == 0)
155  {
156  NS_LOG_WARN ("no matching bearer for this packet");
157  }
158  else
159  {
160  SendToS1uSocket (packet, enbAddr, teid);
161  }
162  }
163  // there is no reason why we should notify the TUN
164  // VirtualNetDevice that he failed to send the packet: if we receive
165  // any bogus packet, it will just be silently discarded.
166  const bool succeeded = true;
167  return succeeded;
168 }
169 
170 void
172 {
173  NS_LOG_FUNCTION (this << socket);
174  NS_ASSERT (socket == m_s1uSocket);
175  Ptr<Packet> packet = socket->Recv ();
176  GtpuHeader gtpu;
177  packet->RemoveHeader (gtpu);
178  uint32_t teid = gtpu.GetTeid ();
179 
182  SocketAddressTag tag;
183  packet->RemovePacketTag (tag);
184 
185  SendToTunDevice (packet, teid);
186 }
187 
188 void
190 {
191  NS_LOG_FUNCTION (this << packet << teid);
192  NS_LOG_LOGIC (" packet size: " << packet->GetSize () << " bytes");
194 }
195 
196 void
198 {
199  NS_LOG_FUNCTION (this << packet << enbAddr << teid);
200 
201  GtpuHeader gtpu;
202  gtpu.SetTeid (teid);
203  // From 3GPP TS 29.281 v10.0.0 Section 5.1
204  // Length of the payload + the non obligatory GTP-U header
205  gtpu.SetLength (packet->GetSize () + gtpu.GetSerializedSize () - 8);
206  packet->AddHeader (gtpu);
207  uint32_t flags = 0;
208  m_s1uSocket->SendTo (packet, flags, InetSocketAddress(enbAddr, m_gtpuUdpPort));
209 }
210 
211 
212 void
214 {
215  m_s11SapMme = s;
216 }
217 
218 EpcS11SapSgw*
220 {
221  return m_s11SapSgw;
222 }
223 
224 void
225 EpcSgwPgwApplication::AddEnb (uint16_t cellId, Ipv4Address enbAddr, Ipv4Address sgwAddr)
226 {
227  NS_LOG_FUNCTION (this << cellId << enbAddr << sgwAddr);
228  EnbInfo enbInfo;
229  enbInfo.enbAddr = enbAddr;
230  enbInfo.sgwAddr = sgwAddr;
231  m_enbInfoByCellId[cellId] = enbInfo;
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION (this << imsi);
238  Ptr<UeInfo> ueInfo = Create<UeInfo> ();
239  m_ueInfoByImsiMap[imsi] = ueInfo;
240 }
241 
242 void
244 {
245  NS_LOG_FUNCTION (this << imsi << ueAddr);
246  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (imsi);
247  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << imsi);
248  m_ueInfoByAddrMap[ueAddr] = ueit->second;
249  ueit->second->SetUeAddr (ueAddr);
250 }
251 
252 void
254 {
255  NS_LOG_FUNCTION (this << req.imsi);
256  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (req.imsi);
257  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << req.imsi);
258  uint16_t cellId = req.uli.gci;
259  std::map<uint16_t, EnbInfo>::iterator enbit = m_enbInfoByCellId.find (cellId);
260  NS_ASSERT_MSG (enbit != m_enbInfoByCellId.end (), "unknown CellId " << cellId);
261  Ipv4Address enbAddr = enbit->second.enbAddr;
262  ueit->second->SetEnbAddr (enbAddr);
263 
265  res.teid = req.imsi; // trick to avoid the need for allocating TEIDs on the S11 interface
266 
267  for (std::list<EpcS11SapSgw::BearerContextToBeCreated>::iterator bit = req.bearerContextsToBeCreated.begin ();
268  bit != req.bearerContextsToBeCreated.end ();
269  ++bit)
270  {
271  // simple sanity check. If you ever need more than 4M teids
272  // throughout your simulation, you'll need to implement a smarter teid
273  // management algorithm.
274  NS_ABORT_IF (m_teidCount == 0xFFFFFFFF);
275  uint32_t teid = ++m_teidCount;
276  ueit->second->AddBearer (bit->tft, bit->epsBearerId, teid);
277 
279  bearerContext.sgwFteid.teid = teid;
280  bearerContext.sgwFteid.address = enbit->second.sgwAddr;
281  bearerContext.epsBearerId = bit->epsBearerId;
282  bearerContext.bearerLevelQos = bit->bearerLevelQos;
283  bearerContext.tft = bit->tft;
284  res.bearerContextsCreated.push_back (bearerContext);
285  }
287 
288 }
289 
290 void
292 {
293  NS_LOG_FUNCTION (this << req.teid);
294  uint64_t imsi = req.teid; // trick to avoid the need for allocating TEIDs on the S11 interface
295  std::map<uint64_t, Ptr<UeInfo> >::iterator ueit = m_ueInfoByImsiMap.find (imsi);
296  NS_ASSERT_MSG (ueit != m_ueInfoByImsiMap.end (), "unknown IMSI " << imsi);
297  uint16_t cellId = req.uli.gci;
298  std::map<uint16_t, EnbInfo>::iterator enbit = m_enbInfoByCellId.find (cellId);
299  NS_ASSERT_MSG (enbit != m_enbInfoByCellId.end (), "unknown CellId " << cellId);
300  Ipv4Address enbAddr = enbit->second.enbAddr;
301  ueit->second->SetEnbAddr (enbAddr);
302  // no actual bearer modification: for now we just support the minimum needed for path switch request (handover)
304  res.teid = imsi; // trick to avoid the need for allocating TEIDs on the S11 interface
307 }
308 
309 }; // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
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:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
3GPP TS 29.274 version 8.3.1 Release 8 section 8.28
Definition: epc-s11-sap.h:80
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.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
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)
Definition: assert.h:64
uint32_t GetSize(void) const
Definition: packet.h:650
bool Receive(Ptr< Packet > packet, uint16_t protocol, const Address &source, const Address &destination, PacketType packetType)
Callback< R > MakeNullCallback(void)
Definition: callback.h:1395
void SendToTunDevice(Ptr< Packet > packet, uint32_t teid)
Send a packet to the internet via the Gi interface of the SGW/PGW.
a polymophic address class
Definition: address.h:86
Template for the implementation of the EpcS11SapSgw as a member of an owner class of type C to which ...
Definition: epc-s11-sap.h:242
Packet header for IPv4.
Definition: ipv4-header.h:31
virtual Address GetAddress(void) const
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< SampleEmitter > s
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:948
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
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)
Definition: log.h:368
#define NS_ABORT_IF(cond)
Abnormal program termination if cond is true.
Definition: abort.h:69
Modify Bearer Request message, see 3GPP TS 29.274 7.2.7.
Definition: epc-s11-sap.h:169
Ptr< Packet > Copy(void) const
Definition: packet.cc:122
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
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)
Definition: assert.h:86
Ptr< Socket > m_s1uSocket
UDP socket to send and receive GTP-U packets to and from the S1-U interface.
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:38
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:135
std::list< BearerContextToBeCreated > bearerContextsToBeCreated
Definition: epc-s11-sap.h:155
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
Packet addressed oo us.
Definition: net-device.h:272
#define NS_LOG_WARN(msg)
Definition: log.h:280
bool RemovePacketTag(Tag &tag)
Remove a packet tag.
Definition: packet.cc:848
virtual void DoDispose()
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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:63
Modify Bearer Response message, see 3GPP TS 29.274 7.2.7.
Definition: epc-s11-sap.h:110
void DoModifyBearerRequest(EpcS11SapSgw::ModifyBearerRequestMessage msg)
EpcS11SapSgw * m_s11SapSgw
SGW side of the S11 SAP.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
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:253
Create Session Request message, see 3GPP TS 29.274 7.2.1.
Definition: epc-s11-sap.h:151
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