GtpTunnel.h Source File

Simu5G: GtpTunnel.h Source File
Simu5G
GtpTunnel.h
1//
2// Simu5G
3//
4// Copyright (C) 2026 Andras Varga (OpenSim Ltd)
5//
6// This file is part of a software released under the license included in file
7// "license.pdf". Please read LICENSE and README files before using it.
8// The above files and the present reference are part of the software itself,
9// and cannot be removed from it.
10//
11
12#ifndef _GTP_TUNNEL_H_
13#define _GTP_TUNNEL_H_
14
15#include <map>
16#include <ostream>
17
18#include <inet/common/packet/Packet.h>
19#include <inet/networklayer/common/L3Address.h>
20
21#include "simu5g/common/LteTypes.h"
22#include "simu5g/common/SessionTag_m.h"
23#include "simu5g/corenetwork/gtp/GtpUserMsg_m.h"
24
25namespace simu5g {
26
32struct FTeid
33{
34 inet::L3Address address;
35 Teid teid = TEID_NONE;
36
37 bool isSet() const { return teid != TEID_NONE; }
38};
39
40inline std::ostream& operator<<(std::ostream& os, const FTeid& fteid)
41{
42 return os << fteid.address.str() << "/teid=" << fteid.teid;
43}
44
51{
52 MacNodeId lteNodeId = NODEID_NONE;
53 MacNodeId nrNodeId = NODEID_NONE;
54 SessionId id = SessionId(0);
55};
56
57inline bool operator<(const SessionRef& a, const SessionRef& b)
58{
59 if (a.lteNodeId != b.lteNodeId)
60 return a.lteNodeId < b.lteNodeId;
61 if (a.nrNodeId != b.nrNodeId)
62 return a.nrNodeId < b.nrNodeId;
63 return a.id < b.id;
64}
65
66inline bool operator==(const SessionRef& a, const SessionRef& b)
67{
68 return a.lteNodeId == b.lteNodeId && a.nrNodeId == b.nrNodeId && a.id == b.id;
69}
70
71inline std::ostream& operator<<(std::ostream& os, const SessionRef& session)
72{
73 return os << "session " << session.id << " of UE " << session.lteNodeId << "/" << session.nrNodeId;
74}
75
80struct AddressPrefix
81{
82 inet::L3Address prefix;
83 int length = 0;
84
85 AddressPrefix() {}
86 AddressPrefix(const inet::L3Address& prefix, int length) : prefix(prefix), length(length) {}
87 bool matches(const inet::L3Address& address) const { return address.getType() == prefix.getType() && address.matches(prefix, length); }
88};
89
90inline std::ostream& operator<<(std::ostream& os, const AddressPrefix& p)
91{
92 return os << p.prefix.str() << "/" << p.length;
93}
94
100{
101 SessionRef ref;
102 SessionType type = IP_V4;
103};
104
109inline void attachSessionTag(inet::Packet *datagram, const TunnelSession& session)
110{
111 auto tag = datagram->addTag<SessionTag>();
112 tag->setSessionId(session.ref.id);
113 tag->setLteNodeId(session.ref.lteNodeId);
114 tag->setNrNodeId(session.ref.nrNodeId);
115 tag->setSessionType(session.type);
116}
117
126{
127 inet::L3Address sessionAddress;
128 int localPort = -1;
129 inet::L3Address serverAddress;
130 int serverPort = -1;
131};
132
133inline std::ostream& operator<<(std::ostream& os, const N6Tunnel& tunnel)
134{
135 return os << "[" << tunnel.sessionAddress.str() << "]:" << tunnel.localPort << " <-> [" << tunnel.serverAddress.str() << "]:" << tunnel.serverPort;
136}
137
144{
145 FTeid anchor;
146 std::map<inet::L3Address, Teid> mecHosts;
147 bool toUpf = false; // the anchor is a UPF (5GC): the G-PDUs carry a PDU Session Container
148};
149
157{
158 SessionRef ref;
159 SessionType type = IP_V4;
160 UplinkTunnels uplink;
161 FTeid dl;
162};
163
169inline inet::Ptr<GtpUserMsg> makeGtpUserHeader(Teid teid, Qfi qfi, PduSessionContainerType container, inet::B tpduLength)
170{
171 auto header = inet::makeShared<GtpUserMsg>();
172 header->setTeid(teid);
173 header->setQfi(qfi);
174 header->setPduSessionContainer(container);
175 inet::B headerLength = gtpUserHeaderLength(container);
176 header->setChunkLength(headerLength);
177 header->setLengthField((headerLength - inet::B(8) + tpduLength).get());
178 return header;
179}
180
181} //namespace
182
183#endif
Definition AlertReceiver.h:24
constexpr Teid TEID_NONE
GTP-U Tunnel Endpoint Identifier (TS 29.281), chosen by the receiving end of the tunnel.
Definition LteTypes.h:87
void attachSessionTag(inet::Packet *datagram, const TunnelSession &session)
Definition GtpTunnel.h:109
inet::Ptr< GtpUserMsg > makeGtpUserHeader(Teid teid, Qfi qfi, PduSessionContainerType container, inet::B tpduLength)
Definition GtpTunnel.h:169
constexpr MacNodeId NODEID_NONE
MAC node ID.
Definition LteTypes.h:54
Definition GtpTunnel.h:81
Definition GtpTunnel.h:33
Definition GtpTunnel.h:126
Definition GtpTunnel.h:51
Definition GtpTunnel.h:157
Definition GtpTunnel.h:100
Definition GtpTunnel.h:144