A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
epc-sgw-application.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017-2018 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Manuel Requena <manuel.requena@cttc.es>
18 */
19
20#include "epc-sgw-application.h"
21
22#include "epc-gtpu-header.h"
23
24#include "ns3/log.h"
25
26#include <map>
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("EpcSgwApplication");
32
33NS_OBJECT_ENSURE_REGISTERED(EpcSgwApplication);
34
36 Ipv4Address s5Addr,
37 const Ptr<Socket> s5uSocket,
38 const Ptr<Socket> s5cSocket)
39 : m_s5Addr(s5Addr),
40 m_s5uSocket(s5uSocket),
41 m_s5cSocket(s5cSocket),
42 m_s1uSocket(s1uSocket),
43 m_gtpuUdpPort(2152), // fixed by the standard
44 m_gtpcUdpPort(2123), // fixed by the standard
45 m_teidCount(0)
46{
47 NS_LOG_FUNCTION(this << s1uSocket << s5Addr << s5uSocket << s5cSocket);
51}
52
54{
55 NS_LOG_FUNCTION(this);
56}
57
58void
60{
61 NS_LOG_FUNCTION(this);
63 m_s1uSocket = nullptr;
65 m_s5uSocket = nullptr;
67 m_s5cSocket = nullptr;
68}
69
72{
73 static TypeId tid = TypeId("ns3::EpcSgwApplication").SetParent<Object>().SetGroupName("Lte");
74 return tid;
75}
76
77void
79{
80 NS_LOG_FUNCTION(this << mmeS11Addr << s11Socket);
81 m_mmeS11Addr = mmeS11Addr;
82 m_s11Socket = s11Socket;
84}
85
86void
88{
89 NS_LOG_FUNCTION(this << pgwAddr);
90 m_pgwAddr = pgwAddr;
91}
92
93void
94EpcSgwApplication::AddEnb(uint16_t cellId, Ipv4Address enbAddr, Ipv4Address sgwAddr)
95{
96 NS_LOG_FUNCTION(this << cellId << enbAddr << sgwAddr);
97 EnbInfo enbInfo;
98 enbInfo.enbAddr = enbAddr;
99 enbInfo.sgwAddr = sgwAddr;
100 m_enbInfoByCellId[cellId] = enbInfo;
101}
102
103void
105{
106 NS_LOG_FUNCTION(this << socket);
107 NS_ASSERT(socket == m_s11Socket);
108 Ptr<Packet> packet = socket->Recv();
109 GtpcHeader header;
110 packet->PeekHeader(header);
111 uint16_t msgType = header.GetMessageType();
112
113 switch (msgType)
114 {
117 break;
118
121 break;
122
125 break;
126
129 break;
130
131 default:
132 NS_FATAL_ERROR("GTP-C message not supported");
133 break;
134 }
135}
136
137void
139{
140 NS_LOG_FUNCTION(this << socket);
141 NS_ASSERT(socket == m_s5uSocket);
142 Ptr<Packet> packet = socket->Recv();
143 GtpuHeader gtpu;
144 packet->RemoveHeader(gtpu);
145 uint32_t teid = gtpu.GetTeid();
146
147 Ipv4Address enbAddr = m_enbByTeidMap[teid];
148 NS_LOG_DEBUG("eNB " << enbAddr << " TEID " << teid);
149 SendToS1uSocket(packet, enbAddr, teid);
150}
151
152void
154{
155 NS_LOG_FUNCTION(this << socket);
156 NS_ASSERT(socket == m_s5cSocket);
157 Ptr<Packet> packet = socket->Recv();
158 GtpcHeader header;
159 packet->PeekHeader(header);
160 uint16_t msgType = header.GetMessageType();
161
162 switch (msgType)
163 {
166 break;
167
170 break;
171
174 break;
175
176 default:
177 NS_FATAL_ERROR("GTP-C message not supported");
178 break;
179 }
180}
181
182void
184{
185 NS_LOG_FUNCTION(this << socket);
186 NS_ASSERT(socket == m_s1uSocket);
187 Ptr<Packet> packet = socket->Recv();
188 GtpuHeader gtpu;
189 packet->RemoveHeader(gtpu);
190 uint32_t teid = gtpu.GetTeid();
191
192 SendToS5uSocket(packet, m_pgwAddr, teid);
193}
194
195void
197{
198 NS_LOG_FUNCTION(this << packet << enbAddr << teid);
199
200 GtpuHeader gtpu;
201 gtpu.SetTeid(teid);
202 // From 3GPP TS 29.281 v10.0.0 Section 5.1
203 // Length of the payload + the non obligatory GTP-U header
204 gtpu.SetLength(packet->GetSize() + gtpu.GetSerializedSize() - 8);
205 packet->AddHeader(gtpu);
206 m_s1uSocket->SendTo(packet, 0, InetSocketAddress(enbAddr, m_gtpuUdpPort));
207}
208
209void
211{
212 NS_LOG_FUNCTION(this << packet << pgwAddr << teid);
213
214 GtpuHeader gtpu;
215 gtpu.SetTeid(teid);
216 // From 3GPP TS 29.281 v10.0.0 Section 5.1
217 // Length of the payload + the non obligatory GTP-U header
218 gtpu.SetLength(packet->GetSize() + gtpu.GetSerializedSize() - 8);
219 packet->AddHeader(gtpu);
220 m_s5uSocket->SendTo(packet, 0, InetSocketAddress(pgwAddr, m_gtpuUdpPort));
221}
222
224// Process messages from the MME
226
227void
229{
230 NS_LOG_FUNCTION(this);
231
233 packet->RemoveHeader(msg);
234 uint64_t imsi = msg.GetImsi();
235 uint16_t cellId = msg.GetUliEcgi();
236 NS_LOG_DEBUG("cellId " << cellId << " IMSI " << imsi);
237
238 std::map<uint16_t, EnbInfo>::iterator enbit = m_enbInfoByCellId.find(cellId);
239 NS_ASSERT_MSG(enbit != m_enbInfoByCellId.end(), "unknown CellId " << cellId);
240 Ipv4Address enbAddr = enbit->second.enbAddr;
241 NS_LOG_DEBUG("eNB " << enbAddr);
242
243 GtpcHeader::Fteid_t mmeS11Fteid = msg.GetSenderCpFteid();
244 NS_ASSERT_MSG(mmeS11Fteid.interfaceType == GtpcHeader::S11_MME_GTPC, "wrong interface type");
245
247 msgOut.SetImsi(imsi);
248 msgOut.SetUliEcgi(cellId);
249
250 GtpcHeader::Fteid_t sgwS5cFteid;
252 sgwS5cFteid.teid = imsi;
253 m_mmeS11FteidBySgwS5cTeid[sgwS5cFteid.teid] = mmeS11Fteid;
254 sgwS5cFteid.addr = m_s5Addr;
255 msgOut.SetSenderCpFteid(sgwS5cFteid); // S5 SGW GTP-C TEID
256
257 std::list<GtpcCreateSessionRequestMessage::BearerContextToBeCreated> bearerContexts =
259 NS_LOG_DEBUG("BearerContextToBeCreated size = " << bearerContexts.size());
260 std::list<GtpcCreateSessionRequestMessage::BearerContextToBeCreated> bearerContextsOut;
261 for (auto& bearerContext : bearerContexts)
262 {
263 // simple sanity check. If you ever need more than 4M teids
264 // throughout your simulation, you'll need to implement a smarter teid
265 // management algorithm.
266 NS_ABORT_IF(m_teidCount == 0xFFFFFFFF);
267 uint32_t teid = ++m_teidCount;
268
269 NS_LOG_DEBUG(" TEID " << teid);
270 m_enbByTeidMap[teid] = enbAddr;
271
274 bearerContextOut.sgwS5uFteid.teid = teid; // S5U SGW FTEID
275 bearerContextOut.sgwS5uFteid.addr = enbit->second.sgwAddr;
276 bearerContextOut.epsBearerId = bearerContext.epsBearerId;
277 bearerContextOut.bearerLevelQos = bearerContext.bearerLevelQos;
278 bearerContextOut.tft = bearerContext.tft;
279 bearerContextsOut.push_back(bearerContextOut);
280 }
281
282 msgOut.SetBearerContextsToBeCreated(bearerContextsOut);
283
284 msgOut.SetTeid(0);
285 msgOut.ComputeMessageLength();
286
287 Ptr<Packet> packetOut = Create<Packet>();
288 packetOut->AddHeader(msgOut);
289 NS_LOG_DEBUG("Send CreateSessionRequest to PGW " << m_pgwAddr);
291}
292
293void
295{
296 NS_LOG_FUNCTION(this);
297
299 packet->RemoveHeader(msg);
300 uint64_t imsi = msg.GetImsi();
301 uint16_t cellId = msg.GetUliEcgi();
302 NS_LOG_DEBUG("cellId " << cellId << " IMSI " << imsi);
303
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 NS_LOG_DEBUG("eNB " << enbAddr);
308
310 msgOut.SetImsi(imsi);
311 msgOut.SetUliEcgi(cellId);
312
313 std::list<GtpcModifyBearerRequestMessage::BearerContextToBeModified> bearerContextsOut;
314 std::list<GtpcModifyBearerRequestMessage::BearerContextToBeModified> bearerContexts =
316 NS_LOG_DEBUG("BearerContextsToBeModified size = " << bearerContexts.size());
317 for (auto& bearerContext : bearerContexts)
318 {
319 NS_ASSERT_MSG(bearerContext.fteid.interfaceType == GtpcHeader::S1U_ENB_GTPU,
320 "Wrong FTEID in ModifyBearerRequest msg");
321 uint32_t teid = bearerContext.fteid.teid;
322 Ipv4Address enbAddr = bearerContext.fteid.addr;
323 NS_LOG_DEBUG("bearerId " << (uint16_t)bearerContext.epsBearerId << " TEID " << teid);
324 std::map<uint32_t, Ipv4Address>::iterator addrit = m_enbByTeidMap.find(teid);
325 NS_ASSERT_MSG(addrit != m_enbByTeidMap.end(), "unknown TEID " << teid);
326 addrit->second = enbAddr;
328 bearerContextOut.epsBearerId = bearerContext.epsBearerId;
330 bearerContextOut.fteid.addr = m_s5Addr;
331 bearerContextOut.fteid.teid = bearerContext.fteid.teid;
332
333 bearerContextsOut.push_back(bearerContextOut);
334 }
335
336 msgOut.SetTeid(imsi);
337 msgOut.ComputeMessageLength();
338
339 Ptr<Packet> packetOut = Create<Packet>();
340 packetOut->AddHeader(msgOut);
341 NS_LOG_DEBUG("Send ModifyBearerRequest to PGW " << m_pgwAddr);
343}
344
345void
347{
348 NS_LOG_FUNCTION(this);
349
351 packet->RemoveHeader(msg);
352
353 std::list<GtpcDeleteBearerCommandMessage::BearerContext> bearerContextsOut;
354 for (auto& bearerContext : msg.GetBearerContexts())
355 {
356 NS_LOG_DEBUG("ebid " << (uint16_t)bearerContext.m_epsBearerId);
358 bearerContextOut.m_epsBearerId = bearerContext.m_epsBearerId;
359 bearerContextsOut.push_back(bearerContextOut);
360 }
361
363 msgOut.SetBearerContexts(bearerContextsOut);
364 msgOut.SetTeid(msg.GetTeid());
365 msgOut.ComputeMessageLength();
366
367 Ptr<Packet> packetOut = Create<Packet>();
368 packetOut->AddHeader(msgOut);
369 NS_LOG_DEBUG("Send DeleteBearerCommand to PGW " << m_pgwAddr);
371}
372
373void
375{
376 NS_LOG_FUNCTION(this);
377
379 packet->RemoveHeader(msg);
381 msgOut.SetEpsBearerIds(msg.GetEpsBearerIds());
382 msgOut.SetTeid(msg.GetTeid());
383 msgOut.ComputeMessageLength();
384
385 Ptr<Packet> packetOut = Create<Packet>();
386 packetOut->AddHeader(msgOut);
387 NS_LOG_DEBUG("Send DeleteBearerResponse to PGW " << m_pgwAddr);
389}
390
392// Process messages received from the PGW
394
395void
397{
398 NS_LOG_FUNCTION(this);
399
401 packet->RemoveHeader(msg);
402
403 GtpcHeader::Fteid_t pgwS5cFteid = msg.GetSenderCpFteid();
404 NS_ASSERT_MSG(pgwS5cFteid.interfaceType == GtpcHeader::S5_PGW_GTPC, "wrong interface type");
405
408
409 uint32_t teid = msg.GetTeid();
411
412 std::list<GtpcCreateSessionResponseMessage::BearerContextCreated> bearerContexts =
414 NS_LOG_DEBUG("BearerContextsCreated size = " << bearerContexts.size());
415 std::list<GtpcCreateSessionResponseMessage::BearerContextCreated> bearerContextsOut;
416 for (auto& bearerContext : bearerContexts)
417 {
420 bearerContextOut.fteid.teid = bearerContext.fteid.teid;
421 bearerContextOut.fteid.addr = m_s5Addr;
422 bearerContextOut.epsBearerId = bearerContext.epsBearerId;
423 bearerContextOut.bearerLevelQos = bearerContext.bearerLevelQos;
424 bearerContextOut.tft = bearerContext.tft;
425 bearerContextsOut.push_back(bearerContext);
426 }
427 msgOut.SetBearerContextsCreated(bearerContextsOut);
428
429 msgOut.SetTeid(mmeS11Fteid.teid);
430 msgOut.ComputeMessageLength();
431
432 Ptr<Packet> packetOut = Create<Packet>();
433 packetOut->AddHeader(msgOut);
434 NS_LOG_DEBUG("Send CreateSessionResponse to MME " << mmeS11Fteid.addr);
435 m_s11Socket->SendTo(packetOut, 0, InetSocketAddress(mmeS11Fteid.addr, m_gtpcUdpPort));
436}
437
438void
440{
441 NS_LOG_FUNCTION(this);
442
444 packet->RemoveHeader(msg);
445
448 msgOut.SetTeid(msg.GetTeid());
449 msgOut.ComputeMessageLength();
450
451 Ptr<Packet> packetOut = Create<Packet>();
452 packetOut->AddHeader(msgOut);
453 NS_LOG_DEBUG("Send ModifyBearerResponse to MME " << m_mmeS11Addr);
455}
456
457void
459{
460 NS_LOG_FUNCTION(this);
461
463 packet->RemoveHeader(msg);
464
466 msgOut.SetEpsBearerIds(msg.GetEpsBearerIds());
467 msgOut.SetTeid(msg.GetTeid());
468 msgOut.ComputeMessageLength();
469
470 Ptr<Packet> packetOut = Create<Packet>();
471 packetOut->AddHeader(msgOut);
472 NS_LOG_DEBUG("Send DeleteBearerRequest to MME " << m_mmeS11Addr);
474}
475
476} // namespace ns3
Ptr< Socket > m_s5cSocket
UDP socket to send/receive GTP-C packets to/from the S5 interface.
void SendToS1uSocket(Ptr< Packet > packet, Ipv4Address enbS1uAddress, uint32_t teid)
Send a data packet to an eNB via the S1-U interface.
uint16_t m_gtpuUdpPort
UDP port to be used for GTP-U.
std::map< uint32_t, GtpcHeader::Fteid_t > m_mmeS11FteidBySgwS5cTeid
MME S11 FTEID by SGW S5C TEID.
void DoRecvModifyBearerRequest(Ptr< Packet > packet)
Process GTP-C Modify Bearer Request message.
void DoRecvCreateSessionResponse(Ptr< Packet > packet)
Process GTP-C Create Session Response message.
Ipv4Address m_mmeS11Addr
MME address in the S11 interface.
void DoRecvDeleteBearerRequest(Ptr< Packet > packet)
Process GTP-C Delete Bearer Request message.
void AddMme(Ipv4Address mmeS11Addr, Ptr< Socket > s11Socket)
Let the SGW be aware of an MME.
void DoRecvDeleteBearerCommand(Ptr< Packet > packet)
Process GTP-C Delete Bearer Command message.
void RecvFromS5cSocket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the S5-C socket.
~EpcSgwApplication() override
Destructor.
void SendToS5uSocket(Ptr< Packet > packet, Ipv4Address pgwAddr, uint32_t teid)
Send a data packet to the PGW via the S5 interface.
void DoRecvCreateSessionRequest(Ptr< Packet > packet)
Process GTP-C Create Session Request message.
EpcSgwApplication(const Ptr< Socket > s1uSocket, Ipv4Address s5Addr, const Ptr< Socket > s5uSocket, const Ptr< Socket > s5cSocket)
Constructor that binds callback methods of sockets.
void AddPgw(Ipv4Address pgwAddr)
Let the SGW be aware of a PGW.
void RecvFromS5uSocket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the S5-U socket.
std::map< uint32_t, Ipv4Address > m_enbByTeidMap
Map for eNB address by TEID.
Ptr< Socket > m_s1uSocket
UDP socket to send/receive GTP-U packets to/from the S1-U interface.
Ptr< Socket > m_s11Socket
UDP socket to send/receive control messages to/from the S11 interface.
std::map< uint16_t, EnbInfo > m_enbInfoByCellId
Map for eNB info by cell ID.
uint32_t m_teidCount
TEID count.
void RecvFromS11Socket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the S11 socket.
Ipv4Address m_s5Addr
SGW address in the S5 interface.
uint16_t m_gtpcUdpPort
UDP port to be used for GTP-C.
void RecvFromS1uSocket(Ptr< Socket > socket)
Method to be assigned to the recv callback of the S1-U socket.
void DoDispose() override
Destructor implementation.
void DoRecvDeleteBearerResponse(Ptr< Packet > packet)
Process GTP-C Delete Bearer Response message.
Ptr< Socket > m_s5uSocket
UDP socket to send/receive GTP-U packets to/from the S5 interface.
Ipv4Address m_pgwAddr
PGW address in the S5 interface.
void DoRecvModifyBearerResponse(Ptr< Packet > packet)
Process GTP-C Modify Bearer Response message.
void AddEnb(uint16_t cellId, Ipv4Address enbAddr, Ipv4Address sgwAddr)
Let the SGW be aware of a new eNB.
static TypeId GetTypeId()
Get the type ID.
GTP-C Create Session Request Message.
uint64_t GetImsi() const
Get the IMSI.
void SetUliEcgi(uint32_t uliEcgi)
Set the UliEcgi.
GtpcHeader::Fteid_t GetSenderCpFteid() const
Get the Sender CpFteid.
uint32_t GetUliEcgi() const
Get the UliEcgi.
void SetBearerContextsToBeCreated(std::list< BearerContextToBeCreated > bearerContexts)
Set the Bearer Contexts.
std::list< BearerContextToBeCreated > GetBearerContextsToBeCreated() const
Get the Bearer Contexts.
void SetImsi(uint64_t imsi)
Set the IMSI.
void SetSenderCpFteid(GtpcHeader::Fteid_t fteid)
Set the Sender CpFteid.
GTP-C Create Session Response Message.
void SetCause(Cause_t cause)
Set the Cause.
void SetBearerContextsCreated(std::list< BearerContextCreated > bearerContexts)
Set the Bearer Contexts.
std::list< BearerContextCreated > GetBearerContextsCreated() const
Get the Container of Bearer Contexts.
GtpcHeader::Fteid_t GetSenderCpFteid() const
Get the Sender CpFteid.
GTP-C Delete Bearer Command Message.
std::list< BearerContext > GetBearerContexts() const
Get the Bearer contexts.
void SetBearerContexts(std::list< BearerContext > bearerContexts)
Set the Bearer contexts.
GTP-C Delete Bearer Request Message.
std::list< uint8_t > GetEpsBearerIds() const
Get the Bearers IDs.
void SetEpsBearerIds(std::list< uint8_t > epsBearerIds)
Set the Bearers IDs.
GTP-C Delete Bearer Response Message.
std::list< uint8_t > GetEpsBearerIds() const
Get the Bearers IDs.
void SetEpsBearerIds(std::list< uint8_t > epsBearerIds)
Set the Bearers IDs.
Header of the GTPv2-C protocol.
uint8_t GetMessageType() const
Get message type.
void ComputeMessageLength()
Compute the message length according to the message type.
void SetTeid(uint32_t teid)
Set TEID.
uint32_t GetTeid() const
Get TEID.
GTP-C Modify Bearer Request Message.
uint64_t GetImsi() const
Get the IMSI.
uint32_t GetUliEcgi() const
Get the UliEcgi.
void SetUliEcgi(uint32_t uliEcgi)
Set the UliEcgi.
std::list< BearerContextToBeModified > GetBearerContextsToBeModified() const
Get the Bearer Contexts.
void SetImsi(uint64_t imsi)
Set the IMSI.
GTP-C Modify Bearer Response Message.
void SetCause(Cause_t cause)
Set the Cause.
Implementation of the GPRS Tunnelling Protocol header according to GTPv1-U Release 10 as per 3Gpp TS ...
void SetTeid(uint32_t teid)
Set TEID function.
uint32_t GetSerializedSize() const override
uint32_t GetTeid() const
Get a tunnel endpoint identificator (TEID)
void SetLength(uint16_t length)
Set the length in octets of the payload.
an Inet address class
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
virtual int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress)=0
Send data to a specified peer.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:745
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:76
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:702
Ptr< EpcTft > tft
Bearer traffic flow template.
Ipv4Address addr
IPv4 address.
InterfaceType_t interfaceType
Interface type.