-
Notifications
You must be signed in to change notification settings - Fork 87
/
Binder.cc
1357 lines (1117 loc) · 47.7 KB
/
Binder.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Simu5G
//
// Authors: Giovanni Nardini, Giovanni Stea, Antonio Virdis (University of Pisa)
//
// This file is part of a software released under the license included in file
// "license.pdf". Please read LICENSE and README files before using it.
// The above files and the present reference are part of the software itself,
// and cannot be removed from it.
//
#include "common/binder/Binder.h"
#include "inet/networklayer/common/L3AddressResolver.h"
#include <cctype>
#include <algorithm>
#include "stack/mac/layer/LteMacUe.h"
#include "stack/phy/layer/LtePhyUe.h"
#include "corenetwork/statsCollector/BaseStationStatsCollector.h"
#include "corenetwork/statsCollector/UeStatsCollector.h"
namespace simu5g {
using namespace std;
using namespace inet;
Define_Module(Binder);
void Binder::registerCarrier(double carrierFrequency, unsigned int carrierNumBands, unsigned int numerologyIndex, bool useTdd, unsigned int tddNumSymbolsDl, unsigned int tddNumSymbolsUl)
{
CarrierInfoMap::iterator it = componentCarriers_.find(carrierFrequency);
if (it != componentCarriers_.end() && carrierNumBands <= componentCarriers_[carrierFrequency].numBands)
{
EV << "Binder::registerCarrier - Carrier @ " << carrierFrequency << "GHz already registered" << endl;
}
else
{
CarrierInfo cInfo;
cInfo.carrierFrequency = carrierFrequency;
cInfo.numBands = carrierNumBands;
cInfo.numerologyIndex = numerologyIndex;
cInfo.slotFormat = computeSlotFormat(useTdd,tddNumSymbolsDl,tddNumSymbolsUl);
componentCarriers_[carrierFrequency] = cInfo;
// update total number of bands in the system
totalBands_ += carrierNumBands;
EV << "Binder::registerCarrier - Registered component carrier @ " << carrierFrequency << "GHz" << endl;
carrierFreqToNumerologyIndex_[carrierFrequency] = numerologyIndex;
// add new (empty) entry to carrierUeMap
std::set<MacNodeId> tempSet;
carrierUeMap_[carrierFrequency] = tempSet;
}
}
void Binder::registerCarrierUe(double carrierFrequency, unsigned int numerologyIndex, MacNodeId ueId)
{
// check if carrier exists in the system
CarrierUeMap::iterator it = carrierUeMap_.find(carrierFrequency);
if (it == carrierUeMap_.end())
throw cRuntimeError("Binder::registerCarrierUe - Carrier [%fGHz] not found", carrierFrequency);
carrierUeMap_[carrierFrequency].insert(ueId);
if (ueNumerologyIndex_.find(ueId) == ueNumerologyIndex_.end())
{
std::set<NumerologyIndex> numerologySet;
ueNumerologyIndex_[ueId] = numerologySet;
}
ueNumerologyIndex_[ueId].insert(numerologyIndex);
if (ueMaxNumerologyIndex_.size() <= ueId)
{
ueMaxNumerologyIndex_.resize(ueId + 1);
ueMaxNumerologyIndex_[ueId] = numerologyIndex;
}
else
ueMaxNumerologyIndex_[ueId] = (numerologyIndex > ueMaxNumerologyIndex_[ueId]) ? numerologyIndex : ueMaxNumerologyIndex_[ueId];
}
const UeSet& Binder::getCarrierUeSet(double carrierFrequency)
{
CarrierUeMap::iterator it = carrierUeMap_.find(carrierFrequency);
if (it == carrierUeMap_.end())
throw cRuntimeError("Binder::getCarrierUeSet - Carrier [%fGHz] not found", carrierFrequency);
return carrierUeMap_[carrierFrequency];
}
NumerologyIndex Binder::getUeMaxNumerologyIndex(MacNodeId ueId)
{
return ueMaxNumerologyIndex_[ueId];
}
const std::set<NumerologyIndex>* Binder::getUeNumerologyIndex(MacNodeId ueId)
{
if (ueNumerologyIndex_.find(ueId) == ueNumerologyIndex_.end())
return NULL;
return &ueNumerologyIndex_[ueId];
}
SlotFormat Binder::computeSlotFormat(bool useTdd, unsigned int tddNumSymbolsDl, unsigned int tddNumSymbolsUl)
{
SlotFormat sf;
if (!useTdd)
{
sf.tdd = false;
// these values are not used when tdd is false
sf.numDlSymbols = 0;
sf.numUlSymbols = 0;
sf.numFlexSymbols = 0;
}
else
{
unsigned int rbxDl = 7; // TODO replace with the parameter obtained from NED file once you moved the function to the Component Carrier
sf.tdd = true;
unsigned int numSymbols = rbxDl * 2;
if (tddNumSymbolsDl+tddNumSymbolsUl > numSymbols)
throw cRuntimeError("Binder::computeSlotFormat - Number of symbols not valid - DL[%d] UL[%d]", tddNumSymbolsDl,tddNumSymbolsUl);
sf.numDlSymbols = tddNumSymbolsDl;
sf.numUlSymbols = tddNumSymbolsUl;
sf.numFlexSymbols = numSymbols - tddNumSymbolsDl - tddNumSymbolsUl;
}
return sf;
}
SlotFormat Binder::getSlotFormat(double carrierFrequency)
{
CarrierInfoMap::iterator it = componentCarriers_.find(carrierFrequency);
if (it == componentCarriers_.end())
throw cRuntimeError("Binder::getSlotFormat - Carrier [%fGHz] not found", carrierFrequency);
return it->second.slotFormat;
}
void Binder::unregisterNode(MacNodeId id)
{
EV << NOW << " Binder::unregisterNode - unregistering node " << id << endl;
std::map<Ipv4Address, MacNodeId>::iterator it;
for(it = macNodeIdToIPAddress_.begin(); it != macNodeIdToIPAddress_.end(); )
{
if(it->second == id)
{
macNodeIdToIPAddress_.erase(it++);
}
else
{
it++;
}
}
// iterate all nodeIds and find HarqRx buffers dependent on 'id'
std::map<int, OmnetId>::iterator idIter;
for (idIter = nodeIds_.begin(); idIter != nodeIds_.end(); idIter++){
LteMacBase* mac = getMacFromMacNodeId(idIter->first);
mac->unregisterHarqBufferRx(id);
}
// remove 'id' from LteMacBase* cache but do not delete pointer.
if(macNodeIdToModule_.erase(id) != 1){
EV_ERROR << "Cannot unregister node - node id \"" << id << "\" - not found";
}
// remove 'id' from MacNodeId mapping
if(nodeIds_.erase(id) != 1){
EV_ERROR << "Cannot unregister node - node id \"" << id << "\" - not found";
}
// remove 'id' from ulTransmissionMap_ if currently scheduled
for(auto &carrier : ulTransmissionMap_){ // all carrier frequency
for(auto &bands : carrier.second){ // all RB's for current and last TTI (vector<vector<vector<UeAllocationInfo>>>)
for(auto &ues : bands){ // all Ue's in each block
auto itr = ues.begin();
while(itr != ues.end()){
if (itr->nodeId == id){
itr = ues.erase(itr);
} else {
itr++;
}
}
}
}
}
}
MacNodeId Binder::registerNode(cModule *module, RanNodeType type, MacNodeId masterId, bool registerNr)
{
Enter_Method_Silent("registerNode");
MacNodeId macNodeId = -1;
if (type == UE)
{
if (!registerNr)
macNodeId = macNodeIdCounter_[1]++;
else
macNodeId = macNodeIdCounter_[2]++;
}
else if (type == ENODEB || type == GNODEB)
{
macNodeId = macNodeIdCounter_[0]++;
}
EV << "Binder : Assigning to module " << module->getName()
<< " with OmnetId: " << module->getId() << " and MacNodeId " << macNodeId
<< "\n";
// registering new node to Binder
nodeIds_[macNodeId] = module->getId();
if (!registerNr)
module->par("macNodeId") = macNodeId;
else
module->par("nrMacNodeId") = macNodeId;
if (type == UE)
{
registerNextHop(masterId, macNodeId);
}
else if (type == ENODEB || type == GNODEB)
{
module->par("macCellId") = macNodeId;
registerNextHop(macNodeId, macNodeId);
registerMasterNode(masterId, macNodeId);
}
return macNodeId;
}
void Binder::registerNextHop(MacNodeId masterId, MacNodeId slaveId)
{
Enter_Method_Silent("registerNextHop");
EV << "Binder : Registering slave " << slaveId << " to master "
<< masterId << "\n";
if (masterId != slaveId)
{
dMap_[masterId][slaveId] = true;
}
if (nextHop_.size() <= slaveId)
nextHop_.resize(slaveId + 1);
nextHop_[slaveId] = masterId;
}
void Binder::registerMasterNode(MacNodeId masterId, MacNodeId slaveId)
{
Enter_Method_Silent("registerMasterNode");
EV << "Binder : Registering slave " << slaveId << " to master "
<< masterId << "\n";
if (secondaryNodeToMasterNode_.size() <= slaveId)
secondaryNodeToMasterNode_.resize(slaveId + 1);
if (masterId == 0) // this node is a master itself
masterId = slaveId;
secondaryNodeToMasterNode_[slaveId] = masterId;
}
void Binder::initialize(int stage)
{
if (stage == inet::INITSTAGE_LOCAL)
{
phyPisaData.setBlerShift(par("blerShift"));
networkName_ = std::string(getSystemModule()->getName());
}
if (stage == inet::INITSTAGE_LAST)
{
maxDataRatePerRb_ = par("maxDataRatePerRb");
// if avg interference enabled, compute CQIs
computeAverageCqiForBackgroundUes();
}
}
void Binder::finish()
{
if (par("printTrafficGeneratorConfig").boolValue())
{
// build filename
std::stringstream outputFilenameStr;
outputFilenameStr << "config";
cConfigurationEx* configEx = getEnvir()->getConfigEx();
const char* itervars = configEx->getVariable(CFGVAR_ITERATIONVARSF);
outputFilenameStr << "-" << itervars << "repetition=" << configEx->getVariable("repetition") << ".ini";
std::string outputFilename = outputFilenameStr.str();
// open output file
std::ofstream out(outputFilename);
std::string toPrint;
std::vector<UeInfo*>::iterator it = ueList_.begin(), et = ueList_.end();
for ( ; it != et; ++it)
{
std::stringstream ss;
UeInfo* info = *it;
if (info->id < NR_UE_MIN_ID)
continue;
int cellId = info->cellId;
int ueIndex = info->ue->getIndex();
// get HARQ error rate
LteMacBase* macUe = check_and_cast<LteMacBase*>(info->ue->getSubmodule("cellularNic")->getSubmodule("nrMac"));
double harqErrorRateDl = macUe->getHarqErrorRate(DL);
double harqErrorRateUl = macUe->getHarqErrorRate(UL);
// get average CQI
LtePhyUe* phyUe = check_and_cast<LtePhyUe*>(info->phy);
double cqiDl = phyUe->getAverageCqi(DL);
double cqiUl = phyUe->getAverageCqi(UL);
double cqiVarianceDl = phyUe->getVarianceCqi(DL);
double cqiVarianceUl = phyUe->getVarianceCqi(UL);
if (info->cellId == 1)
{
// the UE belongs to the central cell
ss << "*.gnb.cellularNic.bgTrafficGenerator[0].bgUE[" << ueIndex << "].generator.rtxRateDl = " << harqErrorRateDl << "\n";
ss << "*.gnb.cellularNic.bgTrafficGenerator[0].bgUE[" << ueIndex << "].generator.rtxRateUl = " << harqErrorRateUl << "\n";
ss << "*.gnb.cellularNic.bgTrafficGenerator[0].bgUE[" << ueIndex << "].generator.cqiMeanDl = " << cqiDl << "\n";
ss << "*.gnb.cellularNic.bgTrafficGenerator[0].bgUE[" << ueIndex << "].generator.cqiStddevDl = " << sqrt(cqiVarianceDl) << "\n";
ss << "*.gnb.cellularNic.bgTrafficGenerator[0].bgUE[" << ueIndex << "].generator.cqiMeanUl = " << cqiUl << "\n";
ss << "*.gnb.cellularNic.bgTrafficGenerator[0].bgUE[" << ueIndex << "].generator.cqiStddevUl = " << sqrt(cqiVarianceUl) << "\n";
toPrint = ss.str();
ss.clear();
}
else
{
int bgCellId = cellId - 2;
// the UE belongs to a background cell
ss << "*.bgCell[" << bgCellId << "].bgTrafficGenerator.bgUE[" << ueIndex << "].generator.rtxRateDl = " << harqErrorRateDl << "\n";
ss << "*.bgCell[" << bgCellId << "].bgTrafficGenerator.bgUE[" << ueIndex << "].generator.rtxRateUl = " << harqErrorRateUl << "\n";
ss << "*.bgCell[" << bgCellId << "].bgTrafficGenerator.bgUE[" << ueIndex << "].generator.cqiMeanDl = " << cqiDl << "\n";
ss << "*.bgCell[" << bgCellId << "].bgTrafficGenerator.bgUE[" << ueIndex << "].generator.cqiStddevDl = " << sqrt(cqiVarianceDl) << "\n";
ss << "*.bgCell[" << bgCellId << "].bgTrafficGenerator.bgUE[" << ueIndex << "].generator.cqiMeanUl = " << cqiUl << "\n";
ss << "*.bgCell[" << bgCellId << "].bgTrafficGenerator.bgUE[" << ueIndex << "].generator.cqiStddevUl = " << sqrt(cqiVarianceUl) << "\n";
toPrint = ss.str();
ss.clear();
}
out << toPrint;
}
out.close();
}
}
void Binder::unregisterNextHop(MacNodeId masterId, MacNodeId slaveId)
{
Enter_Method_Silent("unregisterNextHop");
EV << "Binder : Unregistering slave " << slaveId << " from master "
<< masterId << "\n";
dMap_[masterId][slaveId] = false;
if (nextHop_.size() <= slaveId)
return;
nextHop_[slaveId] = 0;
}
OmnetId Binder::getOmnetId(MacNodeId nodeId)
{
std::map<int, OmnetId>::iterator it = nodeIds_.find(nodeId);
if(it != nodeIds_.end())
return it->second;
return 0;
}
std::map<int, OmnetId>::const_iterator Binder::getNodeIdListBegin()
{
return nodeIds_.begin();
}
std::map<int, OmnetId>::const_iterator Binder::getNodeIdListEnd()
{
return nodeIds_.end();
}
MacNodeId Binder::getMacNodeIdFromOmnetId(OmnetId id){
std::map<int, OmnetId>::iterator it;
for (it = nodeIds_.begin(); it != nodeIds_.end(); ++it )
if (it->second == id)
return it->first;
return 0;
}
LteMacBase* Binder::getMacFromMacNodeId(MacNodeId id)
{
if (id == 0)
return nullptr;
LteMacBase* mac;
if (macNodeIdToModule_.find(id) == macNodeIdToModule_.end())
{
mac = check_and_cast<LteMacBase*>(getMacByMacNodeId(id));
macNodeIdToModule_[id] = mac;
}
else
{
mac = macNodeIdToModule_[id];
}
return mac;
}
MacNodeId Binder::getNextHop(MacNodeId slaveId)
{
Enter_Method_Silent("getNextHop");
if (slaveId >= nextHop_.size())
throw cRuntimeError("Binder::getNextHop(): bad slave id %d", slaveId);
return nextHop_[slaveId];
}
MacNodeId Binder::getMasterNode(MacNodeId slaveId)
{
Enter_Method_Silent("getMasterNode");
if (slaveId >= secondaryNodeToMasterNode_.size())
throw cRuntimeError("Binder::getMasterNode(): bad slave id %d", slaveId);
return secondaryNodeToMasterNode_[slaveId];
}
void Binder::registerMecHost(const inet::L3Address& mecHostAddress)
{
mecHostAddress_.insert(mecHostAddress);
}
void Binder::registerMecHostUpfAddress(const inet::L3Address& mecHostAddress, const inet::L3Address& gtpAddress)
{
mecHostToUpfAddress_[mecHostAddress] = gtpAddress;
}
bool Binder::isMecHost(const inet::L3Address& mecHostAddress)
{
if (mecHostAddress_.find(mecHostAddress) == mecHostAddress_.end())
return false;
return true;
}
const inet::L3Address& Binder::getUpfFromMecHost(const inet::L3Address& mecHostAddress)
{
if (mecHostToUpfAddress_.find(mecHostAddress) == mecHostToUpfAddress_.end())
throw cRuntimeError("Binder::getUpfFromMecHost - address not found");
return mecHostToUpfAddress_[mecHostAddress];
}
void Binder::registerName(MacNodeId nodeId, const char* moduleName)
{
int len = strlen(moduleName);
macNodeIdToModuleName_[nodeId] = new char[len+1];
strcpy(macNodeIdToModuleName_[nodeId], moduleName);
}
void Binder::registerModule(MacNodeId nodeId, cModule* module)
{
macNodeIdToModuleRef_[nodeId] = module;
}
const char* Binder::getModuleNameByMacNodeId(MacNodeId nodeId)
{
if (macNodeIdToModuleName_.find(nodeId) == macNodeIdToModuleName_.end())
throw cRuntimeError("Binder::getModuleNameByMacNodeId - node ID not found");
return macNodeIdToModuleName_[nodeId];
}
cModule* Binder::getModuleByMacNodeId(MacNodeId nodeId)
{
if (macNodeIdToModuleRef_.find(nodeId) == macNodeIdToModuleRef_.end())
throw cRuntimeError("Binder::getModuleByMacNodeId - node ID not found");
return macNodeIdToModuleRef_[nodeId];
}
ConnectedUesMap Binder::getDeployedUes(MacNodeId localId, Direction dir)
{
Enter_Method_Silent("getDeployedUes");
return dMap_[localId];
}
simtime_t Binder::getLastUpdateUlTransmissionInfo()
{
return lastUpdateUplinkTransmissionInfo_;
}
void Binder::initAndResetUlTransmissionInfo()
{
if (lastUplinkTransmission_ < NOW - 2*TTI)
{
// data structures have not been used in the last 2 time slots,
// so they do not need to be updated.
return;
}
UplinkTransmissionMap::iterator it = ulTransmissionMap_.begin();
for (; it != ulTransmissionMap_.end(); ++it)
{
// the second element (i.e. referring to the old time slot) becomes the first element
if (!(it->second.empty()))
it->second.erase(it->second.begin());
}
lastUpdateUplinkTransmissionInfo_ = NOW;
}
void Binder::storeUlTransmissionMap(double carrierFreq, Remote antenna, RbMap& rbMap, MacNodeId nodeId, MacCellId cellId, LtePhyBase* phy, Direction dir)
{
UeAllocationInfo info;
info.nodeId = nodeId;
info.cellId = cellId;
info.phy = phy;
info.dir = dir;
info.trafficGen = nullptr;
if (ulTransmissionMap_.find(carrierFreq) == ulTransmissionMap_.end() || ulTransmissionMap_[carrierFreq].size() == 0)
{
int numCarrierBands = componentCarriers_[carrierFreq].numBands;
ulTransmissionMap_[carrierFreq].resize(2);
ulTransmissionMap_[carrierFreq][PREV_TTI].resize(numCarrierBands);
ulTransmissionMap_[carrierFreq][CURR_TTI].resize(numCarrierBands);
}
else if (ulTransmissionMap_[carrierFreq].size() == 1)
{
int numCarrierBands = componentCarriers_[carrierFreq].numBands;
ulTransmissionMap_[carrierFreq].push_back(std::vector<std::vector<UeAllocationInfo> >());
ulTransmissionMap_[carrierFreq][CURR_TTI].resize(numCarrierBands);
}
// for each allocated band, store the UE info
std::map<Band, unsigned int>::iterator it = rbMap[antenna].begin(), et = rbMap[antenna].end();
for ( ; it != et; ++it)
{
Band b = it->first;
if (it->second > 0)
ulTransmissionMap_[carrierFreq][CURR_TTI][b].push_back(info);
}
lastUplinkTransmission_ = NOW;
}
void Binder::storeUlTransmissionMap(double carrierFreq, Remote antenna, RbMap& rbMap, MacNodeId nodeId, MacCellId cellId, TrafficGeneratorBase* trafficGen, Direction dir)
{
UeAllocationInfo info;
info.nodeId = nodeId;
info.cellId = cellId;
info.phy = nullptr;
info.dir = dir;
info.trafficGen = trafficGen;
if (ulTransmissionMap_.find(carrierFreq) == ulTransmissionMap_.end() || ulTransmissionMap_[carrierFreq].size() == 0)
{
int numCarrierBands = componentCarriers_[carrierFreq].numBands;
ulTransmissionMap_[carrierFreq].resize(2);
ulTransmissionMap_[carrierFreq][PREV_TTI].resize(numCarrierBands);
ulTransmissionMap_[carrierFreq][CURR_TTI].resize(numCarrierBands);
}
else if (ulTransmissionMap_[carrierFreq].size() == 1)
{
int numCarrierBands = componentCarriers_[carrierFreq].numBands;
ulTransmissionMap_[carrierFreq].push_back(std::vector<std::vector<UeAllocationInfo> >());
ulTransmissionMap_[carrierFreq][CURR_TTI].resize(numCarrierBands);
}
// for each allocated band, store the UE info
std::map<Band, unsigned int>::iterator it = rbMap[antenna].begin(), et = rbMap[antenna].end();
for ( ; it != et; ++it)
{
Band b = it->first;
if (it->second > 0)
ulTransmissionMap_[carrierFreq][CURR_TTI][b].push_back(info);
}
lastUplinkTransmission_ = NOW;
}
const std::vector<std::vector<UeAllocationInfo> >* Binder::getUlTransmissionMap(double carrierFreq, UlTransmissionMapTTI t)
{
if (ulTransmissionMap_.find(carrierFreq) == ulTransmissionMap_.end() || t >= ulTransmissionMap_[carrierFreq].size()){
return NULL;
}
return &(ulTransmissionMap_[carrierFreq].at(t));
}
void Binder::registerX2Port(X2NodeId nodeId, int port)
{
if (x2ListeningPorts_.find(nodeId) == x2ListeningPorts_.end() )
{
// no port has yet been registered
std::list<int> ports;
ports.push_back(port);
x2ListeningPorts_[nodeId] = ports;
}
else
{
x2ListeningPorts_[nodeId].push_back(port);
}
}
int Binder::getX2Port(X2NodeId nodeId)
{
if (x2ListeningPorts_.find(nodeId) == x2ListeningPorts_.end() )
throw cRuntimeError("Binder::getX2Port - No ports available on node %d", nodeId);
int port = x2ListeningPorts_[nodeId].front();
x2ListeningPorts_[nodeId].pop_front();
return port;
}
Cqi Binder::meanCqi(std::vector<Cqi> bandCqi,MacNodeId id,Direction dir)
{
std::vector<Cqi>::iterator it;
Cqi mean=0;
for (it=bandCqi.begin();it!=bandCqi.end();++it)
{
mean+=*it;
}
mean/=bandCqi.size();
if(mean==0)
mean = 1;
return mean;
}
Cqi Binder::medianCqi(std::vector<Cqi> bandCqi,MacNodeId id,Direction dir)
{
std::sort(bandCqi.begin(),bandCqi.end());
int medianPoint = bandCqi.size()/2;
EV << "Binder::medianCqi - median point is " << bandCqi.size() << "/2 = " << medianPoint << ". MedianCqi = " << bandCqi[medianPoint] << endl;
return bandCqi[medianPoint];
}
bool Binder::checkD2DCapability(MacNodeId src, MacNodeId dst)
{
if (src < UE_MIN_ID || (src >= macNodeIdCounter_[1] && src < NR_UE_MIN_ID) || src >= macNodeIdCounter_[2]
|| dst < UE_MIN_ID || (dst >= macNodeIdCounter_[1] && dst < NR_UE_MIN_ID) || dst >= macNodeIdCounter_[2])
throw cRuntimeError("Binder::checkD2DCapability - Node Id not valid. Src %d Dst %d", src, dst);
// if the entry is missing, check if the receiver is D2D capable and update the map
if (d2dPeeringMap_.find(src) == d2dPeeringMap_.end() || d2dPeeringMap_[src].find(dst) == d2dPeeringMap_[src].end())
{
LteMacBase* dstMac = getMacFromMacNodeId(dst);
if (dstMac->isD2DCapable())
{
// set the initial mode
if (nextHop_[src] == nextHop_[dst])
{
// if served by the same cell, then the mode is selected according to the corresponding parameter
LteMacBase* srcMac = getMacFromMacNodeId(src);
bool d2dInitialMode = srcMac->getAncestorPar("d2dInitialMode").boolValue();
d2dPeeringMap_[src][dst] = (d2dInitialMode) ? DM : IM;
}
else
{
// if served by different cells, then the mode can be IM only
d2dPeeringMap_[src][dst] = IM;
}
EV << "Binder::checkD2DCapability - UE " << src << " may transmit to UE " << dst << " using D2D (current mode " << ((d2dPeeringMap_[src][dst] == DM) ? "DM)" : "IM)") << endl;
// this is a D2D-capable flow
return true;
}
else
{
EV << "Binder::checkD2DCapability - UE " << src << " may not transmit to UE " << dst << " using D2D (UE " << dst << " is not D2D capable)" << endl;
// this is not a D2D-capable flow
return false;
}
}
// an entry is present, hence this is a D2D-capable flow
return true;
}
bool Binder::getD2DCapability(MacNodeId src, MacNodeId dst)
{
if (src < UE_MIN_ID || (src >= macNodeIdCounter_[1] && src < NR_UE_MIN_ID) || src >= macNodeIdCounter_[2]
|| dst < UE_MIN_ID || (dst >= macNodeIdCounter_[1] && dst < NR_UE_MIN_ID) || dst >= macNodeIdCounter_[2])
throw cRuntimeError("Binder::getD2DCapability - Node Id not valid. Src %d Dst %d", src, dst);
// if the entry is missing, returns false
if (d2dPeeringMap_.find(src) == d2dPeeringMap_.end() || d2dPeeringMap_[src].find(dst) == d2dPeeringMap_[src].end())
return false;
// the entry exists, no matter if it is DM or IM
return true;
}
std::map<MacNodeId, std::map<MacNodeId, LteD2DMode> >* Binder::getD2DPeeringMap()
{
return &d2dPeeringMap_;
}
LteD2DMode Binder::getD2DMode(MacNodeId src, MacNodeId dst)
{
if (src < UE_MIN_ID || (src >= macNodeIdCounter_[1] && src < NR_UE_MIN_ID) || src >= macNodeIdCounter_[2]
|| dst < UE_MIN_ID || (dst >= macNodeIdCounter_[1] && dst < NR_UE_MIN_ID) || dst >= macNodeIdCounter_[2])
throw cRuntimeError("Binder::getD2DMode - Node Id not valid. Src %d Dst %d", src, dst);
return d2dPeeringMap_[src][dst];
}
bool Binder::isFrequencyReuseEnabled(MacNodeId nodeId)
{
// a d2d-enabled UE can use frequency reuse if it can communicate using DM with all its peers
// in fact, the scheduler does not know to which UE it will communicate when it grants some RBs
if (d2dPeeringMap_.find(nodeId) == d2dPeeringMap_.end())
return false;
std::map<MacNodeId, LteD2DMode>::iterator it = d2dPeeringMap_[nodeId].begin();
if (it == d2dPeeringMap_[nodeId].end())
return false;
for (; it != d2dPeeringMap_[nodeId].end(); ++it)
{
if (it->second == IM)
return false;
}
return true;
}
void Binder::registerMulticastGroup(MacNodeId nodeId, int32_t groupId)
{
if (multicastGroupMap_.find(nodeId) == multicastGroupMap_.end())
{
MulticastGroupIdSet newSet;
newSet.insert(groupId);
multicastGroupMap_[nodeId] = newSet;
}
else
{
multicastGroupMap_[nodeId].insert(groupId);
}
}
bool Binder::isInMulticastGroup(MacNodeId nodeId, int32_t groupId)
{
if (multicastGroupMap_.find(nodeId) == multicastGroupMap_.end())
return false; // the node is not enrolled in any group
if (multicastGroupMap_[nodeId].find(groupId) == multicastGroupMap_[nodeId].end())
return false; // the node is not enrolled in the given group
return true;
}
void Binder::addD2DMulticastTransmitter(MacNodeId nodeId)
{
multicastTransmitterSet_.insert(nodeId);
}
std::set<MacNodeId>& Binder::getD2DMulticastTransmitters()
{
return multicastTransmitterSet_;
}
void Binder::updateUeInfoCellId(MacNodeId id, MacCellId newCellId)
{
std::vector<UeInfo*>::iterator it = ueList_.begin();
for (; it != ueList_.end(); ++it)
{
if ((*it)->id == id)
{
(*it)->cellId = newCellId;
return;
}
}
}
void Binder::addUeHandoverTriggered(MacNodeId nodeId)
{
ueHandoverTriggered_.insert(nodeId);
}
bool Binder::hasUeHandoverTriggered(MacNodeId nodeId)
{
if (ueHandoverTriggered_.find(nodeId) == ueHandoverTriggered_.end())
return false;
return true;
}
void Binder::removeUeHandoverTriggered(MacNodeId nodeId)
{
ueHandoverTriggered_.erase(nodeId);
}
void Binder::addHandoverTriggered(MacNodeId nodeId, MacNodeId srcId, MacNodeId destId)
{
std::pair<MacNodeId, MacNodeId> p(srcId, destId);
handoverTriggered_[nodeId] = p;
}
const std::pair<MacNodeId, MacNodeId>* Binder::getHandoverTriggered(MacNodeId nodeId)
{
if (handoverTriggered_.find(nodeId) == handoverTriggered_.end())
return NULL;
return &handoverTriggered_[nodeId];
}
void Binder::removeHandoverTriggered(MacNodeId nodeId)
{
std::map<MacNodeId, std::pair<MacNodeId, MacNodeId> >::iterator it = handoverTriggered_.find(nodeId);
if (it!=handoverTriggered_.end())
handoverTriggered_.erase(it);
}
void Binder::computeAverageCqiForBackgroundUes()
{
EV << " ===== Binder::computeAverageCqiForBackgroundUes - START =====" << endl;
// initialize interference matrix
for (unsigned int i=0; i<bgTrafficManagerList_.size(); i++)
{
std::map<unsigned int, double> tmp;
for (unsigned int j=0; j<bgTrafficManagerList_.size(); j++)
tmp[j] = 0.0;
bgCellsInterferenceMatrix_[i] = tmp;
}
// update interference until "condition" becomes true
// condition = at least one value of interference is above the threshold and
// we did not reach the maximum number of iteration
bool condition = true;
const int MAX_INTERFERENCE_CHECK = 10;
// interference check needs to be done at least 2 times (1 setup)
unsigned int countInterferenceCheck = 0;
/*
* Compute SINR for each user and interference between cells (for DL) and UEs (for UL)
* This will be done in steps:
* 1) compute SINR without interference and update block usage
* 2) while average interference variation between 2 consecutives steps is above a certain threshold
* - Update interference
* - Compute SINR with interference
* - Update cell block usage according to connect UEs
*/
while(condition)
{
countInterferenceCheck++;
EV << " * ITERATION " << countInterferenceCheck << " *" << endl;
// --- MAIN Interference Check Cycle --- //
// loop through the BackgroundTrafficManagers (one per cell)
for (unsigned int bgTrafficManagerId = 0; bgTrafficManagerId < bgTrafficManagerList_.size(); bgTrafficManagerId++)
{
BgTrafficManagerInfo* info = bgTrafficManagerList_.at(bgTrafficManagerId);
if (!(info->init))
continue;
BackgroundTrafficManager* bgTrafficManager = info->bgTrafficManager;
unsigned int numBands = bgTrafficManager->getNumBands();
//---------------------------------------------------------------------
// STEP 1: update mutual interference
// for iterations after the first one, update the interference before analyzing a whole cell
// Note that it makes no sense computing this at the first iteration when the cell is allocating
// zero blocks still
if (countInterferenceCheck>1)
{
updateMutualInterference(bgTrafficManagerId, numBands, DL);
updateMutualInterference(bgTrafficManagerId, numBands, UL);
}
double cellRbsDl = 0;
double cellRbsUl = 0;
// Compute the SINR for each UE within the cell
auto bgUes_it = bgTrafficManager->getBgUesBegin();
auto bgUes_et = bgTrafficManager->getBgUesEnd();
while (bgUes_it != bgUes_et)
{
TrafficGeneratorBase* bgUe = *bgUes_it;
//---------------------------------------------------------------------
// STEP 2: compute SINR
bool losStatus = false;
inet::Coord bsCoord = bgTrafficManager->getBsCoord();
inet::Coord bgUeCoord = bgUe->getCoord();
int bgUeId = bgUe->getId();
double bsTxPower = bgTrafficManager->getBsTxPower();
double bgUeTxPower = bgUe->getTxPwr();
double sinrDl = computeSinr(bgTrafficManagerId, bgUeId, bsTxPower, bsCoord, bgUeCoord, DL, losStatus);
double sinrUl = computeSinr(bgTrafficManagerId, bgUeId, bgUeTxPower, bgUeCoord, bsCoord, UL, losStatus);
// update CQI for the bg UE
bgUe->setCqiFromSinr(sinrDl, DL);
bgUe->setCqiFromSinr(sinrUl, UL);
//---------------------------------------------------------------------
// STEP 3: update block allocation
// obtain UE load request (in bits/second)
double ueLoadDl = bgUe->getAvgLoad(DL) * 8;
double ueLoadUl = bgUe->getAvgLoad(UL) * 8;
// convert the UE load request to rbs based on SINR
double ueRbsDl = computeRequestedRbsFromSinr(sinrDl,ueLoadDl);
double ueRbsUl = computeRequestedRbsFromSinr(sinrUl,ueLoadUl);
if(ueRbsDl<0 || ueRbsUl<0)
{
EV << "Error! Computed negative requested rbs DL[" << ueRbsDl << "] UL[" << ueRbsUl << "]" << endl;
throw cRuntimeError("Binder::computeAverageCqiForBackgroundUes - Error! Computed negative requested rbs\n");
}
// check if there is room for ueRbs
ueRbsDl = (ueRbsDl > numBands) ? numBands : ueRbsDl;
ueRbsUl = (ueRbsUl > numBands) ? numBands : ueRbsUl;
// update cell load
cellRbsDl += ueRbsDl;
cellRbsUl += ueRbsUl;
// update allocation for the UL
info->allocatedRbsUeUl.at(bgUeId) = ueRbsUl;
// update allocation for the DL only at the first iteration
if (countInterferenceCheck == 1)
{
info->allocatedRbsDl = (cellRbsDl > numBands) ? numBands : cellRbsDl;
info->allocatedRbsUl = (cellRbsUl > numBands) ? numBands : cellRbsUl;
}
++bgUes_it;
}
// update allocation elem for this background traffic manager
info->allocatedRbsDl = (cellRbsDl > numBands) ? numBands : cellRbsDl;
info->allocatedRbsUl = (cellRbsUl > numBands) ? numBands : cellRbsUl;
// if the total cellRbsUl is higher than numBands, then scale the allocation for all UEs
if (cellRbsUl > numBands)
{
double scaleFactor = (double)numBands / cellRbsUl;
for (unsigned int i=0; i < info->allocatedRbsUeUl.size(); i++)
info->allocatedRbsUeUl[i] *= scaleFactor;
}
}
EV << "* END ITERATION " << countInterferenceCheck << endl;
if ( countInterferenceCheck > MAX_INTERFERENCE_CHECK )
condition = false;
}
// // DEBUG
// // loop through the BackgroundTrafficManagers (one per cell)
// for (unsigned int bgTrafficManagerId = 0; bgTrafficManagerId < bgTrafficManagerList_.size(); bgTrafficManagerId++)
// {
// BgTrafficManagerInfo* info = bgTrafficManagerList_.at(bgTrafficManagerId);
// if (!(info->init))
// continue;
//
// BackgroundTrafficManager* bgTrafficManager = info->bgTrafficManager;
//
// // Compute the SINR for each UE within the cell
// auto bgUes_it = bgTrafficManager->getBgUesBegin();
// auto bgUes_et = bgTrafficManager->getBgUesEnd();
// int cont = 0;
// while (bgUes_it != bgUes_et)
// {
// TrafficGeneratorBase* bgUe = *bgUes_it;
// Cqi cqiDl = bgUe->getCqi(DL);
// Cqi cqiUl = bgUe->getCqi(UL);
//
// std::cout << "BgTrafficManager " << bgTrafficManagerId << " - UE[" << cont << "] cqiDl[" << cqiDl << "] cqiUl[" << cqiUl << "] "<< endl;
// ++bgUes_it;
// ++cont;
// }
// }
EV << " ===== Binder::computeAverageCqiForBackgroundUes - END =====" << endl;
}