LteCommon.h Source File

Simu5G: LteCommon.h Source File
Simu5G
LteCommon.h
1//
2// Simu5G
3//
4// Copyright (C) 2012-2021 Giovanni Nardini, Giovanni Stea, Antonio Virdis et al. (University of Pisa)
5// Copyright (C) 2022-2026 Giovanni Nardini, Giovanni Stea et al. (University of Pisa)
6//
7// This file is part of a software released under the license included in file
8// "license.pdf". Please read LICENSE and README files before using it.
9// The above files and the present reference are part of the software itself,
10// and cannot be removed from it.
11//
12
13//
14// Description:
15// This file contains LTE typedefs and constants.
16// At the end of the file there are some utility functions.
17//
18
19#ifndef _LTE_LTECOMMON_H_
20#define _LTE_LTECOMMON_H_
21
22#include <algorithm>
23#include <bitset>
24#include <fstream>
25#include <iostream>
26#include <list>
27#include <map>
28#include <optional>
29#include <queue>
30#include <set>
31#include <sstream>
32#include <string>
33#include <vector>
34
35#include <inet/common/packet/Packet.h>
36#include <inet/common/Protocol.h>
37#include <inet/common/Units.h>
38#include <inet/common/geometry/Geometry_m.h>
39#include <inet/networklayer/common/L3Address.h>
40#include <inet/networklayer/contract/ipv4/Ipv4Address.h>
41
42#include "simu5g/common/LteDefs.h"
43#include "simu5g/common/LteTypes.h"
44#include "simu5g/common/LteCommonEnum_m.h"
45
46namespace inet {
47class PacketFilter;
48}
49
50using namespace omnetpp;
51
52using inet::Hz;
53using inet::GHz;
54
55namespace simu5g {
56
57class Binder;
58class CellInfo;
59class LteMacEnb;
60class LteMacBase;
61class PhyBase;
63class LteControlInfo;
64class FlowControlInfo;
65class ExtCell;
66class IBackgroundTrafficManager;
67class BackgroundScheduler;
68class TrafficGeneratorBase;
69
74{
75 public:
76 static const inet::Protocol pdcp; // Packet Data Convergence Protocol
77 static const inet::Protocol rlc; // Radio Link Control
78 static const inet::Protocol ltemac; // LTE Medium Access Control
79 static const inet::Protocol gtp; // GPRS Tunneling Protocol
80 static const inet::Protocol x2ap; // X2AP Protocol
81 static const inet::Protocol sdap; // Service Data Adaptation Protocol
82 static const inet::Protocol unstructured; // The payload of an Unstructured session: opaque data, carried as is
83};
84
85// Identity of one flow/bearer instance between two ends, as used to request bearer
86// establishment. This is the identity half of FlowControlInfo as a plain value: the
87// packet tag is constructed FROM it, not the other way around.
88struct FlowId {
89 MacNodeId sourceId = NODEID_NONE;
90 MacNodeId destId = NODEID_NONE;
91 Direction direction = DL;
92 DrbId drbId = DRBID_NONE;
93 MacNodeId d2dGroupId = NODEID_NONE;
94 MacNodeId d2dTxPeerId = NODEID_NONE;
95 MacNodeId d2dRxPeerId = NODEID_NONE;
96
97 DrbKey txDrbKey() const; // same logic as ctrlInfoToTxDrbKey (D2D groupcast keys by group)
98 DrbKey rxDrbKey() const; // same logic as ctrlInfoToRxDrbKey
99 FlowId reversed() const; // same swaps as Binder's (former) makeReverseFlowControlInfo
100};
101
102inline std::ostream& operator<<(std::ostream& os, const FlowId& e)
103{
104 os << "src=" << e.sourceId << " dst=" << e.destId << " dir=" << e.direction << " drb=" << e.drbId
105 << " d2dGroup=" << e.d2dGroupId << " d2dTx=" << e.d2dTxPeerId << " d2dRx=" << e.d2dRxPeerId;
106 return os;
107}
108
109// Identity of an IP flow as the packet classifier sees it: the fields the flow -> DRB
110// bindings of ~Ip2Nic are keyed by. Not a packet filter (yet): the fields match exactly,
111// except that the plain-LTE stack stores a wildcard direction to keep its key
112// direction-agnostic.
113struct FlowBindingKey {
114 inet::L3Address srcAddr;
115 inet::L3Address dstAddr;
116 uint16_t typeOfService = 0;
117 Direction direction = DL;
118
119 bool operator==(const FlowBindingKey& other) const {
120 return srcAddr == other.srcAddr && dstAddr == other.dstAddr &&
121 typeOfService == other.typeOfService && direction == other.direction;
122 }
123
124 // The same flow as seen from the other end: addresses swapped and direction
125 // reversed (D2D and the wildcard direction map to themselves).
126 FlowBindingKey reversed() const {
127 Direction revDir = (direction == UL) ? DL : (direction == DL) ? UL : direction;
128 return FlowBindingKey{dstAddr, srcAddr, typeOfService, revDir};
129 }
130};
131
132inline std::ostream& operator<<(std::ostream& os, const FlowBindingKey& k)
133{
134 os << k.srcAddr << "->" << k.dstAddr << " tos=" << k.typeOfService << " dir=" << k.direction;
135 return os;
136}
137
138struct FlowBindingKeyHash {
139 static std::size_t hashAddress(const inet::L3Address& addr) {
140 if (addr.getType() == inet::L3Address::IPv4)
141 return std::hash<uint32_t>{}(addr.toIpv4().getInt());
142 const uint32_t *w = addr.toIpv6().words();
143 return std::hash<uint32_t>{}(w[0] ^ w[1] ^ w[2] ^ w[3]);
144 }
145 std::size_t operator()(const FlowBindingKey& key) const {
146 std::size_t h1 = hashAddress(key.srcAddr);
147 std::size_t h2 = hashAddress(key.dstAddr);
148 std::size_t h3 = std::hash<uint16_t>{}(key.typeOfService);
149 std::size_t h4 = std::hash<uint16_t>{}(uint16_t(key.direction));
150 return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
151 }
152};
153
154// The configuration half of a bearer-establishment request: what the requester asks RRC
155// to set the bearer up as. rlcMode = UNKNOWN_RLC_MODE means the requester states nothing;
156// the bearer configurator then resolves the request from the bearer's definition entry
157// before it reaches RRC. The D2D mode-switch path passes explicit values because it
158// transports already-decided configuration.
159struct BearerRequest {
160 RlcMode rlcMode = UM;
161 Lcg lcg = Lcg(0); // logicalChannelGroup (mac-LogicalChannelConfig)
162
163 // The flow whose classification triggered this establishment, if any: RRC binds it
164 // to the bearer at both endpoints, so the reverse flow resolves to the same DRB
165 // instead of establishing a second, parallel bearer. Absent when the requester
166 // classifies by other means (SDAP maps QFIs) or establishes bearers up front
167 // (the bearer configurator's static definitions).
168 std::optional<FlowBindingKey> flowBindingKey;
169};
170
171inline std::ostream& operator<<(std::ostream& os, const BearerRequest& e)
172{
173 os << "rlcMode=" << e.rlcMode << " lcg=" << e.lcg << " hasFlowBindingKey=" << e.flowBindingKey.has_value();
174 return os;
175}
176
180#define ELEM(x) { x, #x }
181
183#define TTI 0.001
184
186#define NOW simTime()
187
189#define MAX_CODEWORDS 2
190
191// Number of QCI classes
192#define LTE_QCI_CLASSES 9
193
194
195// specifies a list of bands that can be used by a user
196typedef std::vector<Band> UsableBands;
197
198// maps a user with a set of usable bands. If a UE is not in the list, the set of usable bands comprises the whole spectrum
199typedef std::map<MacNodeId, UsableBands> UsableBandsList;
200
202typedef unsigned short Codeword;
203
205typedef unsigned short NumerologyIndex;
206
207// Attenuation vector for analogue models
208typedef std::vector<double> AttenuationVector;
209
210
211/*************************
212* Transmission Modes *
213*************************/
214
216const unsigned char DL_NUM_TXMODE = 6; //UNKNOWN_TX_MODE;
217
219const unsigned char UL_NUM_TXMODE = 6; //UNKNOWN_TX_MODE;
220
221
222const unsigned int txModeToIndex[2] = { 0, 1 };
223
224const TxMode indexToTxMode[2] = {
225 SINGLE_ANTENNA_PORT0,
226 TRANSMIT_DIVERSITY
227};
228
229typedef std::map<MacNodeId, TxMode> TxModeMap;
230
231const double cqiToByteTms[16] = { 0, 2, 3, 5, 11, 15, 20, 25, 36, 38, 49, 63, 72, 79, 89, 92 };
232
233double dBmToLinear(double dbm);
234double dBToLinear(double db);
235double linearToDBm(double lin);
236double linearToDb(double lin);
237
238
239/*****************
240* X2 Support
241*****************/
242
243class X2InformationElement;
244
249typedef std::list<X2InformationElement *> X2InformationElementsList;
250
258struct BandLimit
259{
263 std::vector<int> limit_;
264
265 BandLimit() : band_(0)
266 {
267 limit_.resize(MAX_CODEWORDS, -1);
268 }
269
270 // default "from Band" constructor
271 BandLimit(Band b) : band_(b)
272 {
273 limit_.resize(MAX_CODEWORDS, -1);
274 }
275
276 bool operator<(const BandLimit rhs) const
277 {
278 return limit_[0] > rhs.limit_[0];
279 }
280
281};
282
283typedef std::vector<BandLimit> BandLimitVector;
284
285/*****************
286* LTE Constants
287*****************/
288
289const unsigned char MAXCW = 2;
290const Cqi MAXCQI = 15;
291const Cqi NOSIGNALCQI = 0;
292const Rank NORANK = 1;
293const Tbs CQI2ITBSSIZE = 29;
294const unsigned int PDCP_HEADER_UM = 1;
295const unsigned int PDCP_HEADER_AM = 2;
296// Flat RLC header sizes used by the LTE TS 36.322 UM/AM path (and by the LTE AM STATUS
297// PDU, see LteRlcAmRxEntity::sendStatusReportLte). These are fixed approximations: the LTE
298// header/STATUS size does not vary with the SN field length, the segmentation state, or
299// the NACK count. The NR TS 38.322 SO path instead sizes each PDU exactly via
300// nrUmHeaderBytes()/nrAmHeaderBytes() below; making LTE AM equally faithful is future work.
301const unsigned int RLC_HEADER_UM = 2;
302const unsigned int RLC_HEADER_AM = 2;
303
304// NR-SO RLC header size (bytes) by segment state, so the TX and the scheduler agree on the
305// exact per-PDU header. snBits is the SN field length carried by this flow (UM 6/12, AM
306// 12/18 per TS 38.322); the byte size is computed by octet-aligning the fixed fields + SN
307// and adding the 16-bit SO on a non-first (continuation) segment.
308enum NrUmSegState { NRUM_COMPLETE = 0, NRUM_FIRST = 1, NRUM_CONTINUATION = 2 };
309
310// UM (TS 38.322 6.2.1.3): complete SDU = SI+R = 1B (no SN/SO); first segment = SI(2)+SN,
311// byte-aligned (6-bit SN -> 1B, 12-bit -> 2B); continuation = that + SO(16b) (3B / 4B).
312inline unsigned int nrUmHeaderBytes(NrUmSegState s, unsigned int snBits)
313{
314 if (s == NRUM_COMPLETE)
315 return 1u; // SI + R, no SN
316 unsigned int base = (2u + snBits + 7u) / 8u; // SI(2) + SN, octet-aligned
317 return (s == NRUM_CONTINUATION) ? base + 2u : base; // + SO(16b) on a continuation
318}
319
320
321// AM (TS 38.322 6.2.1.4): an AMD PDU always carries the SN. complete/first = D/C+P+SI(4)+SN,
322// byte-aligned (12-bit SN -> 2B, 18-bit -> 3B); continuation adds the SO(16b) (4B / 5B).
323inline unsigned int nrAmHeaderBytes(NrUmSegState s, unsigned int snBits)
324{
325 unsigned int base = (4u + snBits + 7u) / 8u; // D/C+P+SI(4) + SN, octet-aligned
326 return (s == NRUM_CONTINUATION) ? base + 2u : base; // + SO(16b) on a continuation
327}
328const unsigned int MAC_HEADER = 2;
329const unsigned int MAXGRANT = 4294967295U;
330
331/*****************
332* MAC Support
333*****************/
334
335class LteMacBuffer;
336class LteMacQueue;
337class MacControlElement;
338class LteMacPdu;
339
344typedef std::map<std::pair<MacCid, Codeword>, unsigned int> LteMacScheduleList;
345
350typedef std::map<std::pair<MacNodeId, Codeword>, inet::Packet *> MacPduList;
351
352/*
353 * Codeword list : for each node, it keeps track of allocated codewords (number)
354 */
355typedef std::map<MacNodeId, unsigned int> LteMacAllocatedCws;
356
361typedef std::list<inet::Packet *> RlcSduList;
362typedef std::list<size_t> RlcSduListSizes;
363
368typedef cPacketQueue MacSduList;
369
374typedef std::list<MacControlElement *> MacControlElementsList;
375
379typedef std::set<MacNodeId> UeSet;
380
381/*****************
382* HARQ Support
383*****************/
384
386#define HARQ_NONE 255
387
389#define HARQ_TX_INTERVAL 7 * TTI
390
391// Codeword List - returned by Harq functions
392typedef std::list<Codeword> CwList;
393
395typedef std::pair<unsigned char, CwList> UnitList;
396
397/*********************
398* Incell Interference Support
399*********************/
400struct EnbInfo
401{
402 bool init; // initialization flag
403 bool isNr; // eNodeB or gNodeB
404 EnbType type; // MICRO_ENB or MACRO_ENB
405 double txPwr;
406 TxDirectionType txDirection;
407 double txAngle;
408 MacNodeId id;
409 PhyBase *phy = nullptr;
410 LteMacEnb *mac = nullptr;
411 StochasticChannelModel *realChan = nullptr;
412 opp_component_ptr<cModule> eNodeB;
413 int x2;
414 std::string str() const;
415};
416
417struct UeInfo
418{
419 bool init; // initialization flag
420 double txPwr;
421 MacNodeId id;
422 MacNodeId cellId;
423 StochasticChannelModel *realChan = nullptr;
424 opp_component_ptr<cModule> ue;
425 PhyBase *phy = nullptr;
426 std::string str() const;
427};
428
429/**********************************
430 * Background UEs avg CQI support *
431 *********************************/
432struct BgTrafficManagerInfo
433{
434 bool init; // initialization flag
435 IBackgroundTrafficManager *bgTrafficManager = nullptr;
436 GHz carrierFrequency;
437 double allocatedRbsDl;
438 double allocatedRbsUl;
439 std::vector<double> allocatedRbsUeUl;
440 std::string str() const;
441};
442
443// uplink interference support
444struct UeAllocationInfo {
445 MacNodeId nodeId;
446 MacCellId cellId;
447 PhyBase *phy = nullptr;
448 TrafficGeneratorBase *trafficGen = nullptr;
449 Direction dir;
450};
451
452inline std::ostream& operator<<(std::ostream& os, const UeAllocationInfo& e)
453{
454 os << "node=" << e.nodeId << " cell=" << e.cellId << " dir=" << e.dir;
455 return os;
456}
457
458typedef std::vector<ExtCell *> ExtCellList;
459typedef std::vector<BackgroundScheduler *> BackgroundSchedulerList;
460
461/*****************
462* PHY Support *
463*****************/
464
465typedef std::vector<std::vector<std::vector<double>>> BlerCurves;
466
467struct SlotFormat {
468 bool tdd;
469 unsigned int numDlSymbols;
470 unsigned int numUlSymbols;
471 unsigned int numFlexSymbols;
472};
473
474inline std::ostream& operator<<(std::ostream& os, const SlotFormat& e)
475{
476 os << "tdd=" << e.tdd << " dl=" << e.numDlSymbols << " ul=" << e.numUlSymbols << " flex=" << e.numFlexSymbols;
477 return os;
478}
479
480struct CarrierInfo {
481 GHz carrierFrequency;
482 unsigned int numBands;
483 unsigned int firstBand;
484 unsigned int lastBand;
485 BandLimitVector bandLimit;
486 NumerologyIndex numerologyIndex;
487 SlotFormat slotFormat;
488};
489
490inline std::ostream& operator<<(std::ostream& os, const CarrierInfo& e)
491{
492 os << "freq=" << e.carrierFrequency << " numBands=" << e.numBands
493 << " bands=[" << e.firstBand << "," << e.lastBand << "] numerology=" << e.numerologyIndex
494 << " slot={" << e.slotFormat << "} bandLimitEntries=" << e.bandLimit.size();
495 return os;
496}
497
498typedef std::map<GHz, CarrierInfo> CarrierInfoMap;
499
500/*************************************
501* Shortcut for structures using STL
502*************************************/
503
504typedef std::vector<Cqi> CqiVector;
505typedef std::set<Band> BandSet;
506typedef std::set<Remote> RemoteSet;
507typedef std::map<MacNodeId, bool> ConnectedUesMap;
508typedef std::pair<int, simtime_t> PacketInfo;
509typedef std::set<MacNodeId> ActiveUser;
510typedef std::set<MacCid> ActiveSet;
511
518typedef std::map<std::string, cMsgPar> ParameterMap;
519
520/*********************
521* Utility functions
522*********************/
523
524const std::string dirToA(Direction dir);
527Direction directionFromBsrLcid(LogicalCid lcid, Direction fallback);
528const std::string d2dModeToA(LteD2DMode mode);
529const std::string allocationTypeToA(RbAllocationType type);
530const std::string modToA(LteMod mod);
531const std::string periodicityToA(FbPeriodicity per);
532const std::string txModeToA(TxMode tx);
533TxMode aToTxMode(std::string s);
534const std::string schedDisciplineToA(SchedDiscipline discipline);
535SchedDiscipline aToSchedDiscipline(std::string s);
536Remote aToDas(std::string s);
537const std::string dasToA(const Remote r);
538const char *nodeTypeToA(RanNodeType t);
539RanNodeType aToNodeType(std::string name);
540RanNodeType getNodeTypeById(MacNodeId id);
541bool isBaseStation(CoreNodeType nodeType);
542void verifyControlInfo(const FlowControlInfo *info);
543bool isNrUe(MacNodeId id);
544FeedbackType getFeedbackType(std::string s);
545RbAllocationType getRbAllocationType(std::string s);
546const std::string phyFrameTypeToA(const LtePhyFrameType r);
547LtePhyFrameType aToPhyFrameType(std::string s);
548const std::string rlcModeToA(RlcMode type);
549char *cStringToLower(char *str);
550RlcMode aToRlcMode(std::string s);
551const std::string sessionTypeToA(SessionType type);
552SessionType aToSessionType(std::string s);
553bool isIpSessionType(SessionType type); // IPv4, IPv6 or IPv4v6
554const std::string coreNetworkToA(CoreNetwork type);
555CoreNetwork aToCoreNetwork(std::string s);
556const std::string cellGroupToA(CellGroup group);
557CellGroup aToCellGroup(std::string s);
558
559// Configure an inet::PacketFilter from its string form: an expression written as
560// "expr(...)", or a message-name pattern. Parse errors throw here, at setup time.
561void configurePacketFilter(inet::PacketFilter& filter, const char *spec);
562
563// Take the expr() value of a policy parameter as an evaluatable expression, bound to
564// the given variable resolver (whose ownership passes to the expression). The returned
565// expression is the caller's to delete. Parameters holding anything but an expr()
566// throw here, at setup time.
567omnetpp::cDynamicExpression *getExpressionFromPar(omnetpp::cPar& par, omnetpp::cDynamicExpression::IResolver *resolver);
568const std::string planeToA(Plane p);
569MacNodeId ctrlInfoToUeId(const FlowControlInfo *info);
570DrbKey ctrlInfoToTxDrbKey(const FlowControlInfo *info); // DrbKey for TX context: key by dest (remote receiver)
571DrbKey ctrlInfoToRxDrbKey(const FlowControlInfo *info); // DrbKey for RX context: key by source (remote sender)
572const std::string DeploymentScenarioToA(DeploymentScenario type);
573DeploymentScenario aToDeploymentScenario(std::string s);
574
581inline bool isMulticastDestId(MacNodeId nodeId) {
582 return num(nodeId) >= MULTICAST_DEST_MIN_ID;
583}
584
592void getParametersFromXML(cXMLElement *xmlData, ParameterMap& outputMap);
593
606void parseStringToIntArray(std::string str, int *values, int dim, int pad);
607
617void initializeAllChannels(cModule *mod);
618
619void removeAllSimu5GTags(inet::Packet *pkt);
620
621template<typename T>
622bool checkIfHeaderType(const inet::Packet *pkt, bool checkFirst = false) {
623
624 auto pktAux = pkt->dup();
625
626 int index = 0;
627 while (pktAux->getBitLength() > 0 && pktAux->peekAtFront<inet::Chunk>()) {
628 auto chunk = pktAux->popAtFront<inet::Chunk>();
629 if (inet::dynamicPtrCast<const T>(chunk)) {
630 delete pktAux;
631 if (index != 0 && checkFirst)
632 throw cRuntimeError("The header is not the top");
633 return true;
634 }
635 index++;
636 }
637 delete pktAux;
638 return false;
639}
640
641} //namespace
642
643#endif
Definition Binder.h:48
There is one CellInfo module for each eNB (thus one for each cell). Keeps cross-layer information abo...
Definition CellInfo.h:30
Definition LteTypes.h:191
MAC Layer.
Definition LteMacBase.h:80
Buffers for MAC packets.
Definition LteMacBuffer.h:28
Queue for MAC SDU packets.
Definition LteMacQueue.h:41
Definition LteCommon.h:74
Definition StochasticChannelModel.h:74
Definition AlertReceiver.h:24
unsigned short Tbs
Transport Block Size.
Definition LteTypes.h:137
std::list< MacControlElement * > MacControlElementsList
Definition LteCommon.h:374
std::map< std::string, cMsgPar > ParameterMap
Definition LteCommon.h:518
unsigned short Band
Logical band.
Definition LteTypes.h:140
void getParametersFromXML(cXMLElement *xmlData, ParameterMap &outputMap)
unsigned short Codeword
Codeword.
Definition LteCommon.h:202
std::list< inet::Packet * > RlcSduList
Definition LteCommon.h:361
std::list< X2InformationElement * > X2InformationElementsList
Definition LteCommon.h:249
Direction directionFromBsrLcid(LogicalCid lcid, Direction fallback)
const unsigned char DL_NUM_TXMODE
Number of transmission modes in DL direction.
Definition LteCommon.h:216
void parseStringToIntArray(std::string str, int *values, int dim, int pad)
MacNodeId MacCellId
Cell node ID. It is numerically equal to eNodeB MAC node ID.
Definition LteTypes.h:66
std::map< std::pair< MacCid, Codeword >, unsigned int > LteMacScheduleList
Definition LteCommon.h:344
const unsigned char UL_NUM_TXMODE
Number of transmission modes in UL direction.
Definition LteCommon.h:219
void initializeAllChannels(cModule *mod)
unsigned short Cqi
Channel Quality Indicator.
Definition LteTypes.h:133
std::set< MacNodeId > UeSet
Definition LteCommon.h:379
unsigned short Rank
Connection Identifier: <MacNodeId,LogicalCid>
Definition LteTypes.h:130
std::pair< unsigned char, CwList > UnitList
Pair of acid, list of unit ids.
Definition LteCommon.h:395
unsigned short NumerologyIndex
Numerology Index.
Definition LteCommon.h:205
bool isMulticastDestId(MacNodeId nodeId)
Definition LteCommon.h:581
std::map< std::pair< MacNodeId, Codeword >, inet::Packet * > MacPduList
Definition LteCommon.h:350
constexpr MacNodeId NODEID_NONE
MAC node ID.
Definition LteTypes.h:54
cPacketQueue MacSduList
Definition LteCommon.h:368
Definition LteCommon.h:259
Band band_
Band which the element refers to.
Definition LteCommon.h:261
std::vector< int > limit_
Limit of bytes (per codeword) which can be requested for the current band.
Definition LteCommon.h:263