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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Manuel Requena <manuel.requena@cttc.es>
7 */
8
10
11#include "epc-gtpu-header.h"
12
13#include "ns3/log.h"
14
15#include <map>
16
17namespace ns3
18{
19
20NS_LOG_COMPONENT_DEFINE("EpcSgwApplication");
21
22NS_OBJECT_ENSURE_REGISTERED(EpcSgwApplication);
23
25 Ipv4Address s5Addr,
26 const Ptr<Socket> s5uSocket,
27 const Ptr<Socket> s5cSocket)
28 : m_s5Addr(s5Addr),
29 m_s5uSocket(s5uSocket),
30 m_s5cSocket(s5cSocket),
31 m_s1uSocket(s1uSocket),
32 m_gtpuUdpPort(2152), // fixed by the standard
33 m_gtpcUdpPort(2123), // fixed by the standard
34 m_teidCount(0)
35{
36 NS_LOG_FUNCTION(this << s1uSocket << s5Addr << s5uSocket << s5cSocket);
40}
41
46
47void
58
61{
62 static TypeId tid = TypeId("ns3::EpcSgwApplication").SetParent<Object>().SetGroupName("Lte");
63 return tid;
64}
65
66void
68{
69 NS_LOG_FUNCTION(this << mmeS11Addr << s11Socket);
70 m_mmeS11Addr = mmeS11Addr;
71 m_s11Socket = s11Socket;
73}
74
75void
77{
78 NS_LOG_FUNCTION(this << pgwAddr);
79 m_pgwAddr = pgwAddr;
80}
81
82void
83EpcSgwApplication::AddEnb(uint16_t cellId, Ipv4Address enbAddr, Ipv4Address sgwAddr)
84{
85 NS_LOG_FUNCTION(this << cellId << enbAddr << sgwAddr);
86 EnbInfo enbInfo;
87 enbInfo.enbAddr = enbAddr;
88 enbInfo.sgwAddr = sgwAddr;
89 m_enbInfoByCellId[cellId] = enbInfo;
90}
91
92void
94{
95 NS_LOG_FUNCTION(this << socket);
96 NS_ASSERT(socket == m_s11Socket);
97 Ptr<Packet> packet = socket->Recv();
98 GtpcHeader header;
99 packet->PeekHeader(header);
100 uint16_t msgType = header.GetMessageType();
101
102 switch (msgType)
103 {
106 break;
107
110 break;
111
114 break;
115
118 break;
119
120 default:
121 NS_FATAL_ERROR("GTP-C message not supported");
122 break;
123 }
124}
125
126void
128{
129 NS_LOG_FUNCTION(this << socket);
130 NS_ASSERT(socket == m_s5uSocket);
131 Ptr<Packet> packet = socket->Recv();
132 GtpuHeader gtpu;
133 packet->RemoveHeader(gtpu);
134 uint32_t teid = gtpu.GetTeid();
135
136 Ipv4Address enbAddr = m_enbByTeidMap[teid];
137 NS_LOG_DEBUG("eNB " << enbAddr << " TEID " << teid);
138 SendToS1uSocket(packet, enbAddr, teid);
139}
140
141void
143{
144 NS_LOG_FUNCTION(this << socket);
145 NS_ASSERT(socket == m_s5cSocket);
146 Ptr<Packet> packet = socket->Recv();
147 GtpcHeader header;
148 packet->PeekHeader(header);
149 uint16_t msgType = header.GetMessageType();
150
151 switch (msgType)
152 {
155 break;
156
159 break;
160
163 break;
164
165 default:
166 NS_FATAL_ERROR("GTP-C message not supported");
167 break;
168 }
169}
170
171void
173{
174 NS_LOG_FUNCTION(this << socket);
175 NS_ASSERT(socket == m_s1uSocket);
176 Ptr<Packet> packet = socket->Recv();
177 GtpuHeader gtpu;
178 packet->RemoveHeader(gtpu);
179 uint32_t teid = gtpu.GetTeid();
180
181 SendToS5uSocket(packet, m_pgwAddr, teid);
182}
183
184void
186{
187 NS_LOG_FUNCTION(this << packet << enbAddr << teid);
188
189 GtpuHeader gtpu;
190 gtpu.SetTeid(teid);
191 // From 3GPP TS 29.281 v10.0.0 Section 5.1
192 // Length of the payload + the non obligatory GTP-U header
193 gtpu.SetLength(packet->GetSize() + gtpu.GetSerializedSize() - 8);
194 packet->AddHeader(gtpu);
195 m_s1uSocket->SendTo(packet, 0, InetSocketAddress(enbAddr, m_gtpuUdpPort));
196}
197
198void
200{
201 NS_LOG_FUNCTION(this << packet << pgwAddr << teid);
202
203 GtpuHeader gtpu;
204 gtpu.SetTeid(teid);
205 // From 3GPP TS 29.281 v10.0.0 Section 5.1
206 // Length of the payload + the non obligatory GTP-U header
207 gtpu.SetLength(packet->GetSize() + gtpu.GetSerializedSize() - 8);
208 packet->AddHeader(gtpu);
209 m_s5uSocket->SendTo(packet, 0, InetSocketAddress(pgwAddr, m_gtpuUdpPort));
210}
211
212///////////////////////////////////
213// Process messages from the MME
214///////////////////////////////////
215
216void
218{
219 NS_LOG_FUNCTION(this);
220
222 packet->RemoveHeader(msg);
223 uint64_t imsi = msg.GetImsi();
224 uint16_t cellId = msg.GetUliEcgi();
225 NS_LOG_DEBUG("cellId " << cellId << " IMSI " << imsi);
226
227 auto enbit = m_enbInfoByCellId.find(cellId);
228 NS_ASSERT_MSG(enbit != m_enbInfoByCellId.end(), "unknown CellId " << cellId);
229 Ipv4Address enbAddr = enbit->second.enbAddr;
230 NS_LOG_DEBUG("eNB " << enbAddr);
231
232 GtpcHeader::Fteid_t mmeS11Fteid = msg.GetSenderCpFteid();
233 NS_ASSERT_MSG(mmeS11Fteid.interfaceType == GtpcHeader::S11_MME_GTPC, "wrong interface type");
234
236 msgOut.SetImsi(imsi);
237 msgOut.SetUliEcgi(cellId);
238
239 GtpcHeader::Fteid_t sgwS5cFteid;
241 sgwS5cFteid.teid = imsi;
242 m_mmeS11FteidBySgwS5cTeid[sgwS5cFteid.teid] = mmeS11Fteid;
243 sgwS5cFteid.addr = m_s5Addr;
244 msgOut.SetSenderCpFteid(sgwS5cFteid); // S5 SGW GTP-C TEID
245
246 std::list<GtpcCreateSessionRequestMessage::BearerContextToBeCreated> bearerContexts =
248 NS_LOG_DEBUG("BearerContextToBeCreated size = " << bearerContexts.size());
249 std::list<GtpcCreateSessionRequestMessage::BearerContextToBeCreated> bearerContextsOut;
250 for (auto& bearerContext : bearerContexts)
251 {
252 // simple sanity check. If you ever need more than 4M teids
253 // throughout your simulation, you'll need to implement a smarter teid
254 // management algorithm.
255 NS_ABORT_IF(m_teidCount == 0xFFFFFFFF);
256 uint32_t teid = ++m_teidCount;
257
258 NS_LOG_DEBUG(" TEID " << teid);
259 m_enbByTeidMap[teid] = enbAddr;
260
263 bearerContextOut.sgwS5uFteid.teid = teid; // S5U SGW FTEID
264 bearerContextOut.sgwS5uFteid.addr = enbit->second.sgwAddr;
265 bearerContextOut.epsBearerId = bearerContext.epsBearerId;
266 bearerContextOut.bearerLevelQos = bearerContext.bearerLevelQos;
267 bearerContextOut.tft = bearerContext.tft;
268 bearerContextsOut.push_back(bearerContextOut);
269 }
270
271 msgOut.SetBearerContextsToBeCreated(bearerContextsOut);
272
273 msgOut.SetTeid(0);
274 msgOut.ComputeMessageLength();
275
276 Ptr<Packet> packetOut = Create<Packet>();
277 packetOut->AddHeader(msgOut);
278 NS_LOG_DEBUG("Send CreateSessionRequest to PGW " << m_pgwAddr);
280}
281
282void
284{
285 NS_LOG_FUNCTION(this);
286
288 packet->RemoveHeader(msg);
289 uint64_t imsi = msg.GetImsi();
290 uint16_t cellId = msg.GetUliEcgi();
291 NS_LOG_DEBUG("cellId " << cellId << " IMSI " << imsi);
292
293 auto enbit = m_enbInfoByCellId.find(cellId);
294 NS_ASSERT_MSG(enbit != m_enbInfoByCellId.end(), "unknown CellId " << cellId);
295 Ipv4Address enbAddr = enbit->second.enbAddr;
296 NS_LOG_DEBUG("eNB " << enbAddr);
297
299 msgOut.SetImsi(imsi);
300 msgOut.SetUliEcgi(cellId);
301
302 std::list<GtpcModifyBearerRequestMessage::BearerContextToBeModified> bearerContextsOut;
303 std::list<GtpcModifyBearerRequestMessage::BearerContextToBeModified> bearerContexts =
305 NS_LOG_DEBUG("BearerContextsToBeModified size = " << bearerContexts.size());
306 for (auto& bearerContext : bearerContexts)
307 {
308 NS_ASSERT_MSG(bearerContext.fteid.interfaceType == GtpcHeader::S1U_ENB_GTPU,
309 "Wrong FTEID in ModifyBearerRequest msg");
310 uint32_t teid = bearerContext.fteid.teid;
311 Ipv4Address enbAddr = bearerContext.fteid.addr;
312 NS_LOG_DEBUG("bearerId " << (uint16_t)bearerContext.epsBearerId << " TEID " << teid);
313 auto addrit = m_enbByTeidMap.find(teid);
314 NS_ASSERT_MSG(addrit != m_enbByTeidMap.end(), "unknown TEID " << teid);
315 addrit->second = enbAddr;
317 bearerContextOut.epsBearerId = bearerContext.epsBearerId;
319 bearerContextOut.fteid.addr = m_s5Addr;
320 bearerContextOut.fteid.teid = bearerContext.fteid.teid;
321
322 bearerContextsOut.push_back(bearerContextOut);
323 }
324
325 msgOut.SetTeid(imsi);
326 msgOut.ComputeMessageLength();
327
328 Ptr<Packet> packetOut = Create<Packet>();
329 packetOut->AddHeader(msgOut);
330 NS_LOG_DEBUG("Send ModifyBearerRequest to PGW " << m_pgwAddr);
332}
333
334void
336{
337 NS_LOG_FUNCTION(this);
338
340 packet->RemoveHeader(msg);
341
342 std::list<GtpcDeleteBearerCommandMessage::BearerContext> bearerContextsOut;
343 for (auto& bearerContext : msg.GetBearerContexts())
344 {
345 NS_LOG_DEBUG("ebid " << (uint16_t)bearerContext.m_epsBearerId);
347 bearerContextOut.m_epsBearerId = bearerContext.m_epsBearerId;
348 bearerContextsOut.push_back(bearerContextOut);
349 }
350
352 msgOut.SetBearerContexts(bearerContextsOut);
353 msgOut.SetTeid(msg.GetTeid());
354 msgOut.ComputeMessageLength();
355
356 Ptr<Packet> packetOut = Create<Packet>();
357 packetOut->AddHeader(msgOut);
358 NS_LOG_DEBUG("Send DeleteBearerCommand to PGW " << m_pgwAddr);
360}
361
362void
364{
365 NS_LOG_FUNCTION(this);
366
368 packet->RemoveHeader(msg);
370 msgOut.SetEpsBearerIds(msg.GetEpsBearerIds());
371 msgOut.SetTeid(msg.GetTeid());
372 msgOut.ComputeMessageLength();
373
374 Ptr<Packet> packetOut = Create<Packet>();
375 packetOut->AddHeader(msgOut);
376 NS_LOG_DEBUG("Send DeleteBearerResponse to PGW " << m_pgwAddr);
378}
379
380////////////////////////////////////////////
381// Process messages received from the PGW
382////////////////////////////////////////////
383
384void
386{
387 NS_LOG_FUNCTION(this);
388
390 packet->RemoveHeader(msg);
391
392 GtpcHeader::Fteid_t pgwS5cFteid = msg.GetSenderCpFteid();
393 NS_ASSERT_MSG(pgwS5cFteid.interfaceType == GtpcHeader::S5_PGW_GTPC, "wrong interface type");
394
397
398 uint32_t teid = msg.GetTeid();
400
401 std::list<GtpcCreateSessionResponseMessage::BearerContextCreated> bearerContexts =
403 NS_LOG_DEBUG("BearerContextsCreated size = " << bearerContexts.size());
404 std::list<GtpcCreateSessionResponseMessage::BearerContextCreated> bearerContextsOut;
405 for (auto& bearerContext : bearerContexts)
406 {
409 bearerContextOut.fteid.teid = bearerContext.fteid.teid;
410 bearerContextOut.fteid.addr = m_s5Addr;
411 bearerContextOut.epsBearerId = bearerContext.epsBearerId;
412 bearerContextOut.bearerLevelQos = bearerContext.bearerLevelQos;
413 bearerContextOut.tft = bearerContext.tft;
414 bearerContextsOut.push_back(bearerContext);
415 }
416 msgOut.SetBearerContextsCreated(bearerContextsOut);
417
418 msgOut.SetTeid(mmeS11Fteid.teid);
419 msgOut.ComputeMessageLength();
420
421 Ptr<Packet> packetOut = Create<Packet>();
422 packetOut->AddHeader(msgOut);
423 NS_LOG_DEBUG("Send CreateSessionResponse to MME " << mmeS11Fteid.addr);
424 m_s11Socket->SendTo(packetOut, 0, InetSocketAddress(mmeS11Fteid.addr, m_gtpcUdpPort));
425}
426
427void
429{
430 NS_LOG_FUNCTION(this);
431
433 packet->RemoveHeader(msg);
434
437 msgOut.SetTeid(msg.GetTeid());
438 msgOut.ComputeMessageLength();
439
440 Ptr<Packet> packetOut = Create<Packet>();
441 packetOut->AddHeader(msgOut);
442 NS_LOG_DEBUG("Send ModifyBearerResponse to MME " << m_mmeS11Addr);
444}
445
446void
448{
449 NS_LOG_FUNCTION(this);
450
452 packet->RemoveHeader(msg);
453
455 msgOut.SetEpsBearerIds(msg.GetEpsBearerIds());
456 msgOut.SetTeid(msg.GetTeid());
457 msgOut.ComputeMessageLength();
458
459 Ptr<Packet> packetOut = Create<Packet>();
460 packetOut->AddHeader(msgOut);
461 NS_LOG_DEBUG("Send DeleteBearerRequest to MME " << m_mmeS11Addr);
463}
464
465} // 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.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition socket.cc:117
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:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
Callback< R, Args... > MakeNullCallback()
Definition callback.h:727
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition abort.h:65
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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:684
Ptr< EpcTft > tft
Bearer traffic flow template.
Ipv4Address addr
IPv4 address.
InterfaceType_t interfaceType
Interface type.