A Discrete-Event Network Simulator
API
epc-enb-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 
23 #include "epc-enb-application.h"
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/uinteger.h"
29 
30 #include "epc-gtpu-header.h"
31 #include "eps-bearer-tag.h"
32 
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("EpcEnbApplication");
37 
39 {
40 }
41 
42 EpcEnbApplication::EpsFlowId_t::EpsFlowId_t (const uint16_t a, const uint8_t b)
43  : m_rnti (a),
44  m_bid (b)
45 {
46 }
47 
48 bool
50 {
51  return ( (a.m_rnti == b.m_rnti) && (a.m_bid == b.m_bid) );
52 }
53 
54 bool
56 {
57  return ( (a.m_rnti < b.m_rnti) || ( (a.m_rnti == b.m_rnti) && (a.m_bid < b.m_bid) ) );
58 }
59 
60 
61 TypeId
63 {
64  static TypeId tid = TypeId ("ns3::EpcEnbApplication")
65  .SetParent<Object> ();
66  return tid;
67 }
68 
69 void
71 {
72  NS_LOG_FUNCTION (this);
73  m_lteSocket = 0;
74  m_s1uSocket = 0;
75  delete m_s1SapProvider;
76  delete m_s1apSapEnb;
77 }
78 
79 
80 EpcEnbApplication::EpcEnbApplication (Ptr<Socket> lteSocket, Ptr<Socket> s1uSocket, Ipv4Address enbS1uAddress, Ipv4Address sgwS1uAddress, uint16_t cellId)
81  : m_lteSocket (lteSocket),
82  m_s1uSocket (s1uSocket),
83  m_enbS1uAddress (enbS1uAddress),
84  m_sgwS1uAddress (sgwS1uAddress),
85  m_gtpuUdpPort (2152), // fixed by the standard
86  m_s1SapUser (0),
87  m_s1apSapMme (0),
88  m_cellId (cellId)
89 {
90  NS_LOG_FUNCTION (this << lteSocket << s1uSocket << sgwS1uAddress);
95 }
96 
97 
99 {
100  NS_LOG_FUNCTION (this);
101 }
102 
103 
104 void
106 {
107  m_s1SapUser = s;
108 }
109 
110 
113 {
114  return m_s1SapProvider;
115 }
116 
117 void
119 {
120  m_s1apSapMme = s;
121 }
122 
123 
126 {
127  return m_s1apSapEnb;
128 }
129 
130 void
131 EpcEnbApplication::DoInitialUeMessage (uint64_t imsi, uint16_t rnti)
132 {
133  NS_LOG_FUNCTION (this);
134  // side effect: create entry if not exist
135  m_imsiRntiMap[imsi] = rnti;
136  m_s1apSapMme->InitialUeMessage (imsi, rnti, imsi, m_cellId);
137 }
138 
139 void
141 {
142  NS_LOG_FUNCTION (this);
143  uint16_t enbUeS1Id = params.rnti;
144  uint64_t mmeUeS1Id = params.mmeUeS1Id;
145  uint64_t imsi = mmeUeS1Id;
146  // side effect: create entry if not exist
147  m_imsiRntiMap[imsi] = params.rnti;
148 
149  uint16_t gci = params.cellId;
150  std::list<EpcS1apSapMme::ErabSwitchedInDownlinkItem> erabToBeSwitchedInDownlinkList;
151  for (std::list<EpcEnbS1SapProvider::BearerToBeSwitched>::iterator bit = params.bearersToBeSwitched.begin ();
152  bit != params.bearersToBeSwitched.end ();
153  ++bit)
154  {
155  EpsFlowId_t flowId;
156  flowId.m_rnti = params.rnti;
157  flowId.m_bid = bit->epsBearerId;
158  uint32_t teid = bit->teid;
159 
160  EpsFlowId_t rbid (params.rnti, bit->epsBearerId);
161  // side effect: create entries if not exist
162  m_rbidTeidMap[params.rnti][bit->epsBearerId] = teid;
163  m_teidRbidMap[teid] = rbid;
164 
166  erab.erabId = bit->epsBearerId;
168  erab.enbTeid = bit->teid;
169 
170  erabToBeSwitchedInDownlinkList.push_back (erab);
171  }
172  m_s1apSapMme->PathSwitchRequest (enbUeS1Id, mmeUeS1Id, gci, erabToBeSwitchedInDownlinkList);
173 }
174 
175 void
177 {
178  NS_LOG_FUNCTION (this << rnti);
179  std::map<uint16_t, std::map<uint8_t, uint32_t> >::iterator rntiIt = m_rbidTeidMap.find (rnti);
180  if (rntiIt != m_rbidTeidMap.end ())
181  {
182  for (std::map<uint8_t, uint32_t>::iterator bidIt = rntiIt->second.begin ();
183  bidIt != rntiIt->second.end ();
184  ++bidIt)
185  {
186  uint32_t teid = bidIt->second;
187  m_teidRbidMap.erase (teid);
188  }
189  m_rbidTeidMap.erase (rntiIt);
190  }
191 }
192 
193 void
194 EpcEnbApplication::DoInitialContextSetupRequest (uint64_t mmeUeS1Id, uint16_t enbUeS1Id, std::list<EpcS1apSapEnb::ErabToBeSetupItem> erabToBeSetupList)
195 {
196  NS_LOG_FUNCTION (this);
197 
198  for (std::list<EpcS1apSapEnb::ErabToBeSetupItem>::iterator erabIt = erabToBeSetupList.begin ();
199  erabIt != erabToBeSetupList.end ();
200  ++erabIt)
201  {
202  // request the RRC to setup a radio bearer
203 
204  uint64_t imsi = mmeUeS1Id;
205  std::map<uint64_t, uint16_t>::iterator imsiIt = m_imsiRntiMap.find (imsi);
206  NS_ASSERT_MSG (imsiIt != m_imsiRntiMap.end (), "unknown IMSI");
207  uint16_t rnti = imsiIt->second;
208 
210  params.rnti = rnti;
211  params.bearer = erabIt->erabLevelQosParameters;
212  params.bearerId = erabIt->erabId;
213  params.gtpTeid = erabIt->sgwTeid;
215 
216  EpsFlowId_t rbid (rnti, erabIt->erabId);
217  // side effect: create entries if not exist
218  m_rbidTeidMap[rnti][erabIt->erabId] = params.gtpTeid;
219  m_teidRbidMap[params.gtpTeid] = rbid;
220 
221  }
222 }
223 
224 void
225 EpcEnbApplication::DoPathSwitchRequestAcknowledge (uint64_t enbUeS1Id, uint64_t mmeUeS1Id, uint16_t gci, std::list<EpcS1apSapEnb::ErabSwitchedInUplinkItem> erabToBeSwitchedInUplinkList)
226 {
227  NS_LOG_FUNCTION (this);
228 
229  uint64_t imsi = mmeUeS1Id;
230  std::map<uint64_t, uint16_t>::iterator imsiIt = m_imsiRntiMap.find (imsi);
231  NS_ASSERT_MSG (imsiIt != m_imsiRntiMap.end (), "unknown IMSI");
232  uint16_t rnti = imsiIt->second;
234  params.rnti = rnti;
236 }
237 
238 void
240 {
241  NS_LOG_FUNCTION (this);
242  NS_ASSERT (socket == m_lteSocket);
243  Ptr<Packet> packet = socket->Recv ();
244 
247  SocketAddressTag satag;
248  packet->RemovePacketTag (satag);
249 
250  EpsBearerTag tag;
251  bool found = packet->RemovePacketTag (tag);
252  NS_ASSERT (found);
253  uint16_t rnti = tag.GetRnti ();
254  uint8_t bid = tag.GetBid ();
255  NS_LOG_LOGIC ("received packet with RNTI=" << (uint32_t) rnti << ", BID=" << (uint32_t) bid);
256  std::map<uint16_t, std::map<uint8_t, uint32_t> >::iterator rntiIt = m_rbidTeidMap.find (rnti);
257  if (rntiIt == m_rbidTeidMap.end ())
258  {
259  NS_LOG_WARN ("UE context not found, discarding packet");
260  }
261  else
262  {
263  std::map<uint8_t, uint32_t>::iterator bidIt = rntiIt->second.find (bid);
264  NS_ASSERT (bidIt != rntiIt->second.end ());
265  uint32_t teid = bidIt->second;
266  SendToS1uSocket (packet, teid);
267  }
268 }
269 
270 void
272 {
273  NS_LOG_FUNCTION (this << socket);
274  NS_ASSERT (socket == m_s1uSocket);
275  Ptr<Packet> packet = socket->Recv ();
276  GtpuHeader gtpu;
277  packet->RemoveHeader (gtpu);
278  uint32_t teid = gtpu.GetTeid ();
279  std::map<uint32_t, EpsFlowId_t>::iterator it = m_teidRbidMap.find (teid);
280  NS_ASSERT (it != m_teidRbidMap.end ());
281 
284  SocketAddressTag tag;
285  packet->RemovePacketTag (tag);
286 
287  SendToLteSocket (packet, it->second.m_rnti, it->second.m_bid);
288 }
289 
290 void
291 EpcEnbApplication::SendToLteSocket (Ptr<Packet> packet, uint16_t rnti, uint8_t bid)
292 {
293  NS_LOG_FUNCTION (this << packet << rnti << (uint16_t) bid << packet->GetSize ());
294  EpsBearerTag tag (rnti, bid);
295  packet->AddPacketTag (tag);
296  int sentBytes = m_lteSocket->Send (packet);
297  NS_ASSERT (sentBytes > 0);
298 }
299 
300 
301 void
303 {
304  NS_LOG_FUNCTION (this << packet << teid << packet->GetSize ());
305  GtpuHeader gtpu;
306  gtpu.SetTeid (teid);
307  // From 3GPP TS 29.281 v10.0.0 Section 5.1
308  // Length of the payload + the non obligatory GTP-U header
309  gtpu.SetLength (packet->GetSize () + gtpu.GetSerializedSize () - 8);
310  packet->AddHeader (gtpu);
311  uint32_t flags = 0;
313 }
314 
315 void
316 EpcEnbApplication::DoReleaseIndication (uint64_t imsi, uint16_t rnti, uint8_t bearerId)
317 {
318  NS_LOG_FUNCTION (this << bearerId );
319  std::list<EpcS1apSapMme::ErabToBeReleasedIndication> erabToBeReleaseIndication;
321  erab.erabId = bearerId;
322  erabToBeReleaseIndication.push_back (erab);
323  //From 3GPP TS 23401-950 Section 5.4.4.2, enB sends EPS bearer Identity in Bearer Release Indication message to MME
324  m_s1apSapMme->ErabReleaseIndication (imsi, rnti, erabToBeReleaseIndication);
325 }
326 } // namespace ns3
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Parameters passed to DataRadioBearerSetupRequest ()
This class implements the Service Access Point (SAP) between the LteEnbRrc and the EpcEnbApplication...
E-RAB Release Indication Item IEs, 3GPP TS 36.413 version 9.8.0 section 9.1.3.7.
Definition: epc-s1ap-sap.h:67
an Inet address class
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
MME side of the S1-AP Service Access Point (SAP), provides the MME methods to be called when an S1-AP...
Definition: epc-s1ap-sap.h:48
EpcEnbApplication(Ptr< Socket > lteSocket, Ptr< Socket > s1uSocket, Ipv4Address enbS1uAddress, Ipv4Address sgwS1uAddress, uint16_t cellId)
Constructor.
This class implements the Service Access Point (SAP) between the LteEnbRrc and the EpcEnbApplication...
void DoPathSwitchRequest(EpcEnbS1SapProvider::PathSwitchRequestParameters params)
virtual void DataRadioBearerSetupRequest(DataRadioBearerSetupRequestParameters params)=0
request the setup of a DataRadioBearer
Ipv4Address m_enbS1uAddress
address of the eNB for S1-U communications
uint16_t rnti
the RNTI identifying the UE for which the DataRadioBearer is to be created
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:836
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
void DoDispose(void)
Destructor implementation.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Ipv4Address m_sgwS1uAddress
address of the SGW which terminates all S1-U tunnels
std::list< BearerToBeSwitched > bearersToBeSwitched
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
Tag used to define the RNTI and EPS bearer ID for packets interchanged between the EpcEnbApplication ...
EpsBearer bearer
the characteristics of the bearer to be set up
Ptr< Socket > m_lteSocket
raw packet socket to send and receive the packets to and from the LTE radio interface ...
uint8_t GetBid(void) const
virtual void ErabReleaseIndication(uint64_t mmeUeS1Id, uint16_t enbUeS1Id, std::list< ErabToBeReleasedIndication > erabToBeReleaseIndication)=0
As per 3GPP TS 23.401 Release 9 V9.5.0 Figure 5.4.4.2-1 eNB sends indication of Bearer Release to MME...
uint16_t GetRnti(void) const
void SendToLteSocket(Ptr< Packet > packet, uint16_t rnti, uint8_t bid)
Send a packet to the UE via the LTE radio interface of the eNB.
EpcS1apSapEnb * GetS1apSapEnb()
void SendToS1uSocket(Ptr< Packet > packet, uint32_t teid)
Send a packet to the SGW via the S1-U interface.
void SetS1SapUser(EpcEnbS1SapUser *s)
Set the S1 SAP User.
eNB side of the S1-AP Service Access Point (SAP), provides the eNB methods to be called when an S1-AP...
Definition: epc-s1ap-sap.h:132
uint16_t m_gtpuUdpPort
UDP port to be used for GTP.
EpcS1apSapMme * m_s1apSapMme
MME side of the S1-AP SAP.
Ptr< SampleEmitter > s
void DoInitialUeMessage(uint64_t imsi, uint16_t rnti)
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:1290
virtual void PathSwitchRequest(uint64_t enbUeS1Id, uint64_t mmeUeS1Id, uint16_t gci, std::list< ErabSwitchedInDownlinkItem > erabToBeSwitchedInDownlinkList)=0
PATH SWITCH REQUEST message, see 3GPP TS 36.413 9.1.5.8.
uint32_t gtpTeid
S1-bearer GTP tunnel endpoint identifier, see 36.423 9.2.1.
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
bool operator<(const int64x64_t &lhs, const int64x64_t &rhs)
Less than operator.
Definition: int64x64-128.h:350
virtual ~EpcEnbApplication(void)
Destructor.
void RecvFromLteSocket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the LTE socket.
void DoInitialContextSetupRequest(uint64_t mmeUeS1Id, uint16_t enbUeS1Id, std::list< EpcS1apSapEnb::ErabToBeSetupItem > erabToBeSetupList)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetTeid(uint32_t m_teid)
static TypeId GetTypeId(void)
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Template for the implementation of the EpcEnbS1SapProvider as a member of an owner class of type C to...
EpcEnbS1SapProvider * GetS1SapProvider()
uint32_t GetTeid() const
void RecvFromS1uSocket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the S1-U socket.
EpcEnbS1SapUser * m_s1SapUser
User for the S1 SAP.
void DoUeContextRelease(uint16_t rnti)
std::map< uint32_t, EpsFlowId_t > m_teidRbidMap
map telling for each S1-U TEID the corresponding RNTI,BID
#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:84
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual void PathSwitchRequestAcknowledge(PathSwitchRequestAcknowledgeParameters params)=0
#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:843
void DoPathSwitchRequestAcknowledge(uint64_t enbUeS1Id, uint64_t mmeUeS1Id, uint16_t cgi, std::list< EpcS1apSapEnb::ErabSwitchedInUplinkItem > erabToBeSwitchedInUplinkList)
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:95
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
virtual void InitialUeMessage(uint64_t mmeUeS1Id, uint16_t enbUeS1Id, uint64_t stmsi, uint16_t ecgi)=0
A base class which provides memory management and object aggregation.
Definition: object.h:87
EpcS1apSapEnb * m_s1apSapEnb
ENB side of the S1-AP SAP.
void SetS1apSapMme(EpcS1apSapMme *s)
Set the MME side of the S1-AP SAP.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void DoReleaseIndication(uint64_t imsi, uint16_t rnti, uint8_t bearerId)
This function accepts bearer id corresponding to a particular UE and schedules indication of bearer r...
std::map< uint16_t, std::map< uint8_t, uint32_t > > m_rbidTeidMap
map of maps telling for each RNTI and BID the corresponding S1-U TEID
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
std::map< uint64_t, uint16_t > m_imsiRntiMap
UE context info.
Ptr< Socket > m_s1uSocket
UDP socket to send and receive GTP-U the packets to and from the S1-U interface.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
Template for the implementation of the EpcS1apSapEnb as a member of an owner class of type C to which...
Definition: epc-s1ap-sap.h:253
Implementation of the GTPv1-U Release 10 as per 3Gpp TS 29.281 document.
EpcEnbS1SapProvider * m_s1SapProvider
Provider for the S1 SAP.