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  .SetGroupName("Lte");
67  return tid;
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION (this);
74  m_lteSocket = 0;
75  m_s1uSocket = 0;
76  delete m_s1SapProvider;
77  delete m_s1apSapEnb;
78 }
79 
80 
81 EpcEnbApplication::EpcEnbApplication (Ptr<Socket> lteSocket, Ptr<Socket> s1uSocket, Ipv4Address enbS1uAddress, Ipv4Address sgwS1uAddress, uint16_t cellId)
82  : m_lteSocket (lteSocket),
83  m_s1uSocket (s1uSocket),
84  m_enbS1uAddress (enbS1uAddress),
85  m_sgwS1uAddress (sgwS1uAddress),
86  m_gtpuUdpPort (2152), // fixed by the standard
87  m_s1SapUser (0),
88  m_s1apSapMme (0),
89  m_cellId (cellId)
90 {
91  NS_LOG_FUNCTION (this << lteSocket << s1uSocket << sgwS1uAddress);
96 }
97 
98 
100 {
101  NS_LOG_FUNCTION (this);
102 }
103 
104 
105 void
107 {
108  m_s1SapUser = s;
109 }
110 
111 
114 {
115  return m_s1SapProvider;
116 }
117 
118 void
120 {
121  m_s1apSapMme = s;
122 }
123 
124 
127 {
128  return m_s1apSapEnb;
129 }
130 
131 void
132 EpcEnbApplication::DoInitialUeMessage (uint64_t imsi, uint16_t rnti)
133 {
134  NS_LOG_FUNCTION (this);
135  // side effect: create entry if not exist
136  m_imsiRntiMap[imsi] = rnti;
137  m_s1apSapMme->InitialUeMessage (imsi, rnti, imsi, m_cellId);
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this);
144  uint16_t enbUeS1Id = params.rnti;
145  uint64_t mmeUeS1Id = params.mmeUeS1Id;
146  uint64_t imsi = mmeUeS1Id;
147  // side effect: create entry if not exist
148  m_imsiRntiMap[imsi] = params.rnti;
149 
150  uint16_t gci = params.cellId;
151  std::list<EpcS1apSapMme::ErabSwitchedInDownlinkItem> erabToBeSwitchedInDownlinkList;
152  for (std::list<EpcEnbS1SapProvider::BearerToBeSwitched>::iterator bit = params.bearersToBeSwitched.begin ();
153  bit != params.bearersToBeSwitched.end ();
154  ++bit)
155  {
156  EpsFlowId_t flowId;
157  flowId.m_rnti = params.rnti;
158  flowId.m_bid = bit->epsBearerId;
159  uint32_t teid = bit->teid;
160 
161  EpsFlowId_t rbid (params.rnti, bit->epsBearerId);
162  // side effect: create entries if not exist
163  m_rbidTeidMap[params.rnti][bit->epsBearerId] = teid;
164  m_teidRbidMap[teid] = rbid;
165 
167  erab.erabId = bit->epsBearerId;
169  erab.enbTeid = bit->teid;
170 
171  erabToBeSwitchedInDownlinkList.push_back (erab);
172  }
173  m_s1apSapMme->PathSwitchRequest (enbUeS1Id, mmeUeS1Id, gci, erabToBeSwitchedInDownlinkList);
174 }
175 
176 void
178 {
179  NS_LOG_FUNCTION (this << rnti);
180  std::map<uint16_t, std::map<uint8_t, uint32_t> >::iterator rntiIt = m_rbidTeidMap.find (rnti);
181  if (rntiIt != m_rbidTeidMap.end ())
182  {
183  for (std::map<uint8_t, uint32_t>::iterator bidIt = rntiIt->second.begin ();
184  bidIt != rntiIt->second.end ();
185  ++bidIt)
186  {
187  uint32_t teid = bidIt->second;
188  m_teidRbidMap.erase (teid);
189  }
190  m_rbidTeidMap.erase (rntiIt);
191  }
192 }
193 
194 void
195 EpcEnbApplication::DoInitialContextSetupRequest (uint64_t mmeUeS1Id, uint16_t enbUeS1Id, std::list<EpcS1apSapEnb::ErabToBeSetupItem> erabToBeSetupList)
196 {
197  NS_LOG_FUNCTION (this);
198 
199  for (std::list<EpcS1apSapEnb::ErabToBeSetupItem>::iterator erabIt = erabToBeSetupList.begin ();
200  erabIt != erabToBeSetupList.end ();
201  ++erabIt)
202  {
203  // request the RRC to setup a radio bearer
204 
205  uint64_t imsi = mmeUeS1Id;
206  std::map<uint64_t, uint16_t>::iterator imsiIt = m_imsiRntiMap.find (imsi);
207  NS_ASSERT_MSG (imsiIt != m_imsiRntiMap.end (), "unknown IMSI");
208  uint16_t rnti = imsiIt->second;
209 
211  params.rnti = rnti;
212  params.bearer = erabIt->erabLevelQosParameters;
213  params.bearerId = erabIt->erabId;
214  params.gtpTeid = erabIt->sgwTeid;
216 
217  EpsFlowId_t rbid (rnti, erabIt->erabId);
218  // side effect: create entries if not exist
219  m_rbidTeidMap[rnti][erabIt->erabId] = params.gtpTeid;
220  m_teidRbidMap[params.gtpTeid] = rbid;
221 
222  }
223 }
224 
225 void
226 EpcEnbApplication::DoPathSwitchRequestAcknowledge (uint64_t enbUeS1Id, uint64_t mmeUeS1Id, uint16_t gci, std::list<EpcS1apSapEnb::ErabSwitchedInUplinkItem> erabToBeSwitchedInUplinkList)
227 {
228  NS_LOG_FUNCTION (this);
229 
230  uint64_t imsi = mmeUeS1Id;
231  std::map<uint64_t, uint16_t>::iterator imsiIt = m_imsiRntiMap.find (imsi);
232  NS_ASSERT_MSG (imsiIt != m_imsiRntiMap.end (), "unknown IMSI");
233  uint16_t rnti = imsiIt->second;
235  params.rnti = rnti;
237 }
238 
239 void
241 {
242  NS_LOG_FUNCTION (this);
243  NS_ASSERT (socket == m_lteSocket);
244  Ptr<Packet> packet = socket->Recv ();
245 
246  EpsBearerTag tag;
247  bool found = packet->RemovePacketTag (tag);
248  NS_ASSERT (found);
249  uint16_t rnti = tag.GetRnti ();
250  uint8_t bid = tag.GetBid ();
251  NS_LOG_LOGIC ("received packet with RNTI=" << (uint32_t) rnti << ", BID=" << (uint32_t) bid);
252  std::map<uint16_t, std::map<uint8_t, uint32_t> >::iterator rntiIt = m_rbidTeidMap.find (rnti);
253  if (rntiIt == m_rbidTeidMap.end ())
254  {
255  NS_LOG_WARN ("UE context not found, discarding packet");
256  }
257  else
258  {
259  std::map<uint8_t, uint32_t>::iterator bidIt = rntiIt->second.find (bid);
260  NS_ASSERT (bidIt != rntiIt->second.end ());
261  uint32_t teid = bidIt->second;
262  SendToS1uSocket (packet, teid);
263  }
264 }
265 
266 void
268 {
269  NS_LOG_FUNCTION (this << socket);
270  NS_ASSERT (socket == m_s1uSocket);
271  Ptr<Packet> packet = socket->Recv ();
272  GtpuHeader gtpu;
273  packet->RemoveHeader (gtpu);
274  uint32_t teid = gtpu.GetTeid ();
275  std::map<uint32_t, EpsFlowId_t>::iterator it = m_teidRbidMap.find (teid);
276  NS_ASSERT (it != m_teidRbidMap.end ());
277 
278  SendToLteSocket (packet, it->second.m_rnti, it->second.m_bid);
279 }
280 
281 void
282 EpcEnbApplication::SendToLteSocket (Ptr<Packet> packet, uint16_t rnti, uint8_t bid)
283 {
284  NS_LOG_FUNCTION (this << packet << rnti << (uint16_t) bid << packet->GetSize ());
285  EpsBearerTag tag (rnti, bid);
286  packet->AddPacketTag (tag);
287  int sentBytes = m_lteSocket->Send (packet);
288  NS_ASSERT (sentBytes > 0);
289 }
290 
291 
292 void
294 {
295  NS_LOG_FUNCTION (this << packet << teid << packet->GetSize ());
296  GtpuHeader gtpu;
297  gtpu.SetTeid (teid);
298  // From 3GPP TS 29.281 v10.0.0 Section 5.1
299  // Length of the payload + the non obligatory GTP-U header
300  gtpu.SetLength (packet->GetSize () + gtpu.GetSerializedSize () - 8);
301  packet->AddHeader (gtpu);
302  uint32_t flags = 0;
304 }
305 
306 void
307 EpcEnbApplication::DoReleaseIndication (uint64_t imsi, uint16_t rnti, uint8_t bearerId)
308 {
309  NS_LOG_FUNCTION (this << bearerId );
310  std::list<EpcS1apSapMme::ErabToBeReleasedIndication> erabToBeReleaseIndication;
312  erab.erabId = bearerId;
313  erabToBeReleaseIndication.push_back (erab);
314  //From 3GPP TS 23401-950 Section 5.4.4.2, enB sends EPS bearer Identity in Bearer Release Indication message to MME
315  m_s1apSapMme->ErabReleaseIndication (imsi, rnti, erabToBeReleaseIndication);
316 }
317 } // 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:824
#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
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:792
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.
void DoInitialUeMessage(uint64_t imsi, uint16_t rnti)
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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:128
#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:356
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:90
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:831
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:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
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:257
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.