A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tdtbfq-ff-mac-scheduler.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Marco Miozzo <marco.miozzo@cttc.es> // original version
19  * Modification: Dizhi Zhou <dizhi.zhou@gmail.com> // modify codes related to downlink scheduler
20  */
21 
22 #include <ns3/log.h>
23 #include <ns3/pointer.h>
24 #include <ns3/integer.h>
25 
26 #include <ns3/simulator.h>
27 #include <ns3/lte-amc.h>
28 #include <ns3/tdtbfq-ff-mac-scheduler.h>
29 #include <ns3/lte-vendor-specific-parameters.h>
30 
31 NS_LOG_COMPONENT_DEFINE ("TdTbfqFfMacScheduler");
32 
33 // value for SINR outside the range defined by LTE, used to indicate that there
34 // is no CQI for this element
35 #define NO_SINR -5000
36 
37 namespace ns3 {
38 
40  10, // RGB size 1
41  26, // RGB size 2
42  63, // RGB size 3
43  110 // RGB size 4
44 }; // see table 7.1.6.1-1 of 36.213
45 
46 NS_OBJECT_ENSURE_REGISTERED (TdTbfqFfMacScheduler);
47 
49 {
50 public:
52 
53  // inherited from FfMacCschedSapProvider
54  virtual void CschedCellConfigReq (const struct CschedCellConfigReqParameters& params);
55  virtual void CschedUeConfigReq (const struct CschedUeConfigReqParameters& params);
56  virtual void CschedLcConfigReq (const struct CschedLcConfigReqParameters& params);
57  virtual void CschedLcReleaseReq (const struct CschedLcReleaseReqParameters& params);
58  virtual void CschedUeReleaseReq (const struct CschedUeReleaseReqParameters& params);
59 
60 private:
63 };
64 
66 {
67 }
68 
70 {
71 }
72 
73 
74 void
76 {
78 }
79 
80 void
82 {
84 }
85 
86 
87 void
89 {
91 }
92 
93 void
95 {
97 }
98 
99 void
101 {
103 }
104 
105 
106 
107 
109 {
110 public:
112 
113  // inherited from FfMacSchedSapProvider
114  virtual void SchedDlRlcBufferReq (const struct SchedDlRlcBufferReqParameters& params);
115  virtual void SchedDlPagingBufferReq (const struct SchedDlPagingBufferReqParameters& params);
116  virtual void SchedDlMacBufferReq (const struct SchedDlMacBufferReqParameters& params);
117  virtual void SchedDlTriggerReq (const struct SchedDlTriggerReqParameters& params);
118  virtual void SchedDlRachInfoReq (const struct SchedDlRachInfoReqParameters& params);
119  virtual void SchedDlCqiInfoReq (const struct SchedDlCqiInfoReqParameters& params);
120  virtual void SchedUlTriggerReq (const struct SchedUlTriggerReqParameters& params);
121  virtual void SchedUlNoiseInterferenceReq (const struct SchedUlNoiseInterferenceReqParameters& params);
122  virtual void SchedUlSrInfoReq (const struct SchedUlSrInfoReqParameters& params);
123  virtual void SchedUlMacCtrlInfoReq (const struct SchedUlMacCtrlInfoReqParameters& params);
124  virtual void SchedUlCqiInfoReq (const struct SchedUlCqiInfoReqParameters& params);
125 
126 
127 private:
130 };
131 
132 
133 
135 {
136 }
137 
138 
140  : m_scheduler (scheduler)
141 {
142 }
143 
144 void
146 {
148 }
149 
150 void
152 {
154 }
155 
156 void
158 {
160 }
161 
162 void
164 {
166 }
167 
168 void
170 {
172 }
173 
174 void
176 {
178 }
179 
180 void
182 {
184 }
185 
186 void
188 {
190 }
191 
192 void
194 {
196 }
197 
198 void
200 {
202 }
203 
204 void
206 {
208 }
209 
210 
211 
212 
213 
215  : m_cschedSapUser (0),
216  m_schedSapUser (0),
217  m_timeWindow (99.0),
218  m_nextRntiUl (0),
219  bankSize (0)
220 {
221  m_amc = CreateObject <LteAmc> ();
224 }
225 
227 {
228  NS_LOG_FUNCTION (this);
229 }
230 
231 void
233 {
234  NS_LOG_FUNCTION (this);
235  delete m_cschedSapProvider;
236  delete m_schedSapProvider;
237 }
238 
239 TypeId
241 {
242  static TypeId tid = TypeId ("ns3::TdTbfqFfMacScheduler")
244  .AddConstructor<TdTbfqFfMacScheduler> ()
245  .AddAttribute ("CqiTimerThreshold",
246  "The number of TTIs a CQI is valid (default 1000 - 1 sec.)",
247  UintegerValue (1000),
248  MakeUintegerAccessor (&TdTbfqFfMacScheduler::m_cqiTimersThreshold),
249  MakeUintegerChecker<uint32_t> ())
250  .AddAttribute ("DebtLimit",
251  "Flow debt limit (default -625000 bytes)",
252  IntegerValue (-625000),
253  MakeIntegerAccessor (&TdTbfqFfMacScheduler::m_debtLimit),
254  MakeIntegerChecker<int> ())
255  .AddAttribute ("CreditLimit",
256  "Flow credit limit (default 625000 bytes)",
257  UintegerValue (625000),
258  MakeUintegerAccessor (&TdTbfqFfMacScheduler::m_creditLimit),
259  MakeUintegerChecker<uint32_t> ())
260  .AddAttribute ("TokenPoolSize",
261  "The maximum value of flow token pool (default 1 bytes)",
262  UintegerValue (1),
263  MakeUintegerAccessor (&TdTbfqFfMacScheduler::m_tokenPoolSize),
264  MakeUintegerChecker<uint32_t> ())
265  .AddAttribute ("CreditableThreshold",
266  "Threshold of flow credit (default 0 bytes)",
267  UintegerValue (0),
268  MakeUintegerAccessor (&TdTbfqFfMacScheduler::m_creditableThreshold),
269  MakeUintegerChecker<uint32_t> ())
270  ;
271  return tid;
272 }
273 
274 
275 
276 void
278 {
279  m_cschedSapUser = s;
280 }
281 
282 void
284 {
285  m_schedSapUser = s;
286 }
287 
290 {
291  return m_cschedSapProvider;
292 }
293 
296 {
297  return m_schedSapProvider;
298 }
299 
300 void
302 {
303  NS_LOG_FUNCTION (this);
304  // Read the subset of parameters used
305  m_cschedCellConfig = params;
307  cnf.m_result = SUCCESS;
309  return;
310 }
311 
312 void
314 {
315  NS_LOG_FUNCTION (this << " RNTI " << params.m_rnti << " txMode " << (uint16_t)params.m_transmissionMode);
316  std::map <uint16_t,uint8_t>::iterator it = m_uesTxMode.find (params.m_rnti);
317  if (it == m_uesTxMode.end ())
318  {
319  m_uesTxMode.insert (std::pair <uint16_t, double> (params.m_rnti, params.m_transmissionMode));
320  }
321  else
322  {
323  (*it).second = params.m_transmissionMode;
324  }
325  return;
326 }
327 
328 void
330 {
331  NS_LOG_FUNCTION (this << " New LC, rnti: " << params.m_rnti);
332 
333  std::map <uint16_t, tdtbfqsFlowPerf_t>::iterator it;
334  for (uint16_t i = 0; i < params.m_logicalChannelConfigList.size (); i++)
335  {
336  it = m_flowStatsDl.find (params.m_rnti);
337 
338  if (it == m_flowStatsDl.end ())
339  {
340  uint64_t mbrDlInBytes = params.m_logicalChannelConfigList.at (i).m_eRabMaximulBitrateDl / 8; // byte/s
341  uint64_t mbrUlInBytes = params.m_logicalChannelConfigList.at (i).m_eRabMaximulBitrateUl / 8; // byte/s
342 
343  tdtbfqsFlowPerf_t flowStatsDl;
344  flowStatsDl.flowStart = Simulator::Now ();
345  flowStatsDl.packetArrivalRate = 0;
346  flowStatsDl.tokenGenerationRate = mbrDlInBytes;
347  flowStatsDl.tokenPoolSize = 0;
348  flowStatsDl.maxTokenPoolSize = m_tokenPoolSize;
349  flowStatsDl.counter = 0;
350  flowStatsDl.burstCredit = m_creditLimit; // bytes
351  flowStatsDl.debtLimit = m_debtLimit; // bytes
353  m_flowStatsDl.insert (std::pair<uint16_t, tdtbfqsFlowPerf_t> (params.m_rnti, flowStatsDl));
354  tdtbfqsFlowPerf_t flowStatsUl;
355  flowStatsUl.flowStart = Simulator::Now ();
356  flowStatsUl.packetArrivalRate = 0;
357  flowStatsUl.tokenGenerationRate = mbrUlInBytes;
358  flowStatsUl.tokenPoolSize = 0;
359  flowStatsUl.maxTokenPoolSize = m_tokenPoolSize;
360  flowStatsUl.counter = 0;
361  flowStatsUl.burstCredit = m_creditLimit; // bytes
362  flowStatsUl.debtLimit = m_debtLimit; // bytes
364  m_flowStatsUl.insert (std::pair<uint16_t, tdtbfqsFlowPerf_t> (params.m_rnti, flowStatsUl));
365  }
366  else
367  {
368  NS_LOG_ERROR ("RNTI already exists");
369  }
370  }
371 
372  return;
373 }
374 
375 
376 void
378 {
379  NS_FATAL_ERROR ("unimplemented");
380  return;
381 }
382 
383 void
385 {
386  NS_FATAL_ERROR ("unimplemented");
387  return;
388 }
389 
390 
391 void
393 {
394  NS_LOG_FUNCTION (this << params.m_rnti << (uint32_t) params.m_logicalChannelIdentity);
395  // API generated by RLC for updating RLC parameters on a LC (tx and retx queues)
396 
397  std::map <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it;
398 
399  LteFlowId_t flow (params.m_rnti, params.m_logicalChannelIdentity);
400 
401  it = m_rlcBufferReq.find (flow);
402 
403  if (it == m_rlcBufferReq.end ())
404  {
405  m_rlcBufferReq.insert (std::pair <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters> (flow, params));
406  }
407  else
408  {
409  (*it).second = params;
410  }
411 
412  return;
413 }
414 
415 void
417 {
418  NS_FATAL_ERROR ("unimplemented");
419  return;
420 }
421 
422 void
424 {
425  NS_FATAL_ERROR ("unimplemented");
426  return;
427 }
428 
429 int
431 {
432  for (int i = 0; i < 4; i++)
433  {
434  if (dlbandwidth < TdTbfqType0AllocationRbg[i])
435  {
436  return (i + 1);
437  }
438  }
439 
440  return (-1);
441 }
442 
443 int
445 {
446  std::map <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it;
447  int lcActive = 0;
448  for (it = m_rlcBufferReq.begin (); it != m_rlcBufferReq.end (); it++)
449  {
450  if (((*it).first.m_rnti == rnti) && (((*it).second.m_rlcTransmissionQueueSize > 0)
451  || ((*it).second.m_rlcRetransmissionQueueSize > 0)
452  || ((*it).second.m_rlcStatusPduSize > 0) ))
453  {
454  lcActive++;
455  }
456  if ((*it).first.m_rnti > rnti)
457  {
458  break;
459  }
460  }
461  return (lcActive);
462 
463 }
464 
465 void
467 {
468  NS_LOG_FUNCTION (this << " Frame no. " << (params.m_sfnSf >> 4) << " subframe no. " << (0xF & params.m_sfnSf));
469  // API generated by RLC for triggering the scheduling of a DL subframe
470 
471  // evaluate the relative channel quality indicator for each UE per each RBG
472  // (since we are using allocation type 0 the small unit of allocation is RBG)
473  // Resource allocation type 0 (see sec 7.1.6.1 of 36.213)
474  RefreshDlCqiMaps ();
475 
476  // update token pool, counter and bank size
477  std::map <uint16_t, tdtbfqsFlowPerf_t>::iterator itStats;
478  for (itStats = m_flowStatsDl.begin (); itStats != m_flowStatsDl.end (); itStats++)
479  {
480  if ( (*itStats).second.tokenGenerationRate / 1000 + (*itStats).second.tokenPoolSize > (*itStats).second.maxTokenPoolSize )
481  {
482  (*itStats).second.counter += (*itStats).second.tokenGenerationRate / 1000 - ( (*itStats).second.maxTokenPoolSize - (*itStats).second.tokenPoolSize );
483  (*itStats).second.tokenPoolSize = (*itStats).second.maxTokenPoolSize;
484  bankSize += (*itStats).second.tokenGenerationRate / 1000 - ( (*itStats).second.maxTokenPoolSize - (*itStats).second.tokenPoolSize );
485  }
486  else
487  {
488  (*itStats).second.tokenPoolSize += (*itStats).second.tokenGenerationRate / 1000;
489  }
490  }
491 
493  int rbgNum = m_cschedCellConfig.m_dlBandwidth / rbgSize;
494  std::map <uint16_t, std::vector <uint16_t> > allocationMap;
495 
496  // select UE with largest metric
497  std::map <uint16_t, tdtbfqsFlowPerf_t>::iterator it;
498  std::map <uint16_t, tdtbfqsFlowPerf_t>::iterator itMax = m_flowStatsDl.end ();
499  double metricMax = 0.0;
500  bool firstRnti = true;
501  for (it = m_flowStatsDl.begin (); it != m_flowStatsDl.end (); it++)
502  {
503  if (LcActivePerFlow ((*it).first) == 0)
504  {
505  continue;
506  }
507 
508  double metric = ( ( (double)(*it).second.counter ) / ( (double)(*it).second.tokenGenerationRate ) );
509 
510  if (firstRnti == true)
511  {
512  metricMax = metric;
513  itMax = it;
514  firstRnti = false;
515  continue;
516  }
517  if (metric > metricMax)
518  {
519  metricMax = metric;
520  itMax = it;
521  }
522  } // end for m_flowStatsDl
523 
524  if (itMax == m_flowStatsDl.end ())
525  {
526  // no UE available for downlink
527  return;
528  }
529  else
530  {
531  // assign all RBGs to this UE
532  std::vector <uint16_t> tempMap;
533  for (int i = 0; i < rbgNum; i++)
534  {
535  tempMap.push_back (i);
536  }
537  allocationMap.insert (std::pair <uint16_t, std::vector <uint16_t> > ((*itMax).first, tempMap));
538  }
539 
540  // generate the transmission opportunities by grouping the RBGs of the same RNTI and
541  // creating the correspondent DCIs
543  std::map <uint16_t, std::vector <uint16_t> >::iterator itMap = allocationMap.begin ();
544 
545  while (itMap != allocationMap.end ())
546  {
547  // create new BuildDataListElement_s for this LC
549  newEl.m_rnti = (*itMap).first;
550  // create the DlDciListElement_s
551  DlDciListElement_s newDci;
552  std::vector <struct RlcPduListElement_s> newRlcPduLe;
553  newDci.m_rnti = (*itMap).first;
554 
555  uint16_t lcActives = LcActivePerFlow ((*itMap).first);
556  uint16_t RgbPerRnti = (*itMap).second.size ();
557  std::map <uint16_t,SbMeasResult_s>::iterator itCqi;
558  itCqi = m_a30CqiRxed.find ((*itMap).first);
559  std::map <uint16_t,uint8_t>::iterator itTxMode;
560  itTxMode = m_uesTxMode.find ((*itMap).first);
561  if (itTxMode == m_uesTxMode.end ())
562  {
563  NS_FATAL_ERROR ("No Transmission Mode info on user " << (*itMap).first);
564  }
565  int nLayer = TransmissionModesLayers::TxMode2LayerNum ((*itTxMode).second);
566  std::vector <uint8_t> worstCqi (2, 15);
567  if (itCqi != m_a30CqiRxed.end ())
568  {
569  for (uint16_t k = 0; k < (*itMap).second.size (); k++)
570  {
571  if ((*itCqi).second.m_higherLayerSelected.size () > (*itMap).second.at (k))
572  {
573  for (uint8_t j = 0; j < nLayer; j++)
574  {
575  if ((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.size () > j)
576  {
577  if (((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (j)) < worstCqi.at (j))
578  {
579  worstCqi.at (j) = ((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (j));
580  }
581  }
582  else
583  {
584  // no CQI for this layer of this suband -> worst one
585  worstCqi.at (j) = 1;
586  }
587  }
588  }
589  else
590  {
591  for (uint8_t j = 0; j < nLayer; j++)
592  {
593  worstCqi.at (j) = 1; // try with lowest MCS in RBG with no info on channel
594  }
595  }
596  }
597  }
598  else
599  {
600  for (uint8_t j = 0; j < nLayer; j++)
601  {
602  worstCqi.at (j) = 1; // try with lowest MCS in RBG with no info on channel
603  }
604  }
605  uint32_t bytesTxed = 0;
606  for (uint8_t j = 0; j < nLayer; j++)
607  {
608  newDci.m_mcs.push_back (m_amc->GetMcsFromCqi (worstCqi.at (j)));
609  int tbSize = (m_amc->GetTbSizeFromMcs (newDci.m_mcs.at (j), RgbPerRnti * rbgSize) / 8); // (size of TB in bytes according to table 7.1.7.2.1-1 of 36.213)
610  newDci.m_tbsSize.push_back (tbSize);
611  bytesTxed += tbSize;
612  }
613 
614  newDci.m_resAlloc = 0; // only allocation type 0 at this stage
615  newDci.m_rbBitmap = 0; // TBD (32 bit bitmap see 7.1.6 of 36.213)
616  uint32_t rbgMask = 0;
617  for (uint16_t k = 0; k < (*itMap).second.size (); k++)
618  {
619  rbgMask = rbgMask + (0x1 << (*itMap).second.at (k));
620  }
621  newDci.m_rbBitmap = rbgMask; // (32 bit bitmap see 7.1.6 of 36.213)
622 
623  // create the rlc PDUs -> equally divide resources among actives LCs
624  std::map <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator itBufReq;
625  for (itBufReq = m_rlcBufferReq.begin (); itBufReq != m_rlcBufferReq.end (); itBufReq++)
626  {
627  if (((*itBufReq).first.m_rnti == (*itMap).first)
628  && (((*itBufReq).second.m_rlcTransmissionQueueSize > 0)
629  || ((*itBufReq).second.m_rlcRetransmissionQueueSize > 0)
630  || ((*itBufReq).second.m_rlcStatusPduSize > 0) ))
631  {
632  for (uint8_t j = 0; j < nLayer; j++)
633  {
634  RlcPduListElement_s newRlcEl;
635  newRlcEl.m_logicalChannelIdentity = (*itBufReq).first.m_lcId;
636  newRlcEl.m_size = newDci.m_tbsSize.at (j) / lcActives;
637  newRlcPduLe.push_back (newRlcEl);
638  UpdateDlRlcBufferInfo (newDci.m_rnti, newRlcEl.m_logicalChannelIdentity, newRlcEl.m_size);
639  }
640  }
641  if ((*itBufReq).first.m_rnti > (*itMap).first)
642  {
643  break;
644  }
645  }
646  newDci.m_ndi.push_back (1); // TBD (new data indicator)
647  newDci.m_rv.push_back (0); // TBD (redundancy version)
648 
649  newEl.m_dci = newDci;
650  // ...more parameters -> ingored in this version
651 
652  newEl.m_rlcPduList.push_back (newRlcPduLe);
653  ret.m_buildDataList.push_back (newEl);
654 
655  // update UE stats
656  std::map <uint16_t, tdtbfqsFlowPerf_t>::iterator it;
657  it = m_flowStatsDl.find ((*itMap).first);
658  if (it != m_flowStatsDl.end ())
659  {
660  if ( bytesTxed <= (*it).second.tokenPoolSize )
661  {
662  (*it).second.tokenPoolSize -= bytesTxed;
663  }
664  else
665  {
666  (*it).second.counter = (*it).second.counter - ( bytesTxed - (*it).second.tokenPoolSize );
667  (*it).second.tokenPoolSize = 0;
668  if (bankSize <= ( bytesTxed - (*it).second.tokenPoolSize ))
669  bankSize = 0;
670  else
671  bankSize = bankSize - ( bytesTxed - (*it).second.tokenPoolSize );
672  }
673  }
674  else
675  {
676  NS_LOG_DEBUG (this << " No Stats for this allocated UE");
677  }
678 
679  itMap++;
680  } // end while allocation
681  ret.m_nrOfPdcchOfdmSymbols = 1; // TODO: check correct value according the DCIs txed
682 
684 
685 
686  return;
687 }
688 
689 void
691 {
692  NS_FATAL_ERROR ("unimplemented");
693  return;
694 }
695 
696 void
698 {
699  NS_LOG_FUNCTION (this);
700 
701  for (unsigned int i = 0; i < params.m_cqiList.size (); i++)
702  {
703  if ( params.m_cqiList.at (i).m_cqiType == CqiListElement_s::P10 )
704  {
705  // wideband CQI reporting
706  std::map <uint16_t,uint8_t>::iterator it;
707  uint16_t rnti = params.m_cqiList.at (i).m_rnti;
708  it = m_p10CqiRxed.find (rnti);
709  if (it == m_p10CqiRxed.end ())
710  {
711  // create the new entry
712  m_p10CqiRxed.insert ( std::pair<uint16_t, uint8_t > (rnti, params.m_cqiList.at (i).m_wbCqi.at (0)) ); // only codeword 0 at this stage (SISO)
713  // generate correspondent timer
714  m_p10CqiTimers.insert ( std::pair<uint16_t, uint32_t > (rnti, m_cqiTimersThreshold));
715  }
716  else
717  {
718  // update the CQI value and refresh correspondent timer
719  (*it).second = params.m_cqiList.at (i).m_wbCqi.at (0);
720  // update correspondent timer
721  std::map <uint16_t,uint32_t>::iterator itTimers;
722  itTimers = m_p10CqiTimers.find (rnti);
723  (*itTimers).second = m_cqiTimersThreshold;
724  }
725  }
726  else if ( params.m_cqiList.at (i).m_cqiType == CqiListElement_s::A30 )
727  {
728  // subband CQI reporting high layer configured
729  std::map <uint16_t,SbMeasResult_s>::iterator it;
730  uint16_t rnti = params.m_cqiList.at (i).m_rnti;
731  it = m_a30CqiRxed.find (rnti);
732  if (it == m_a30CqiRxed.end ())
733  {
734  // create the new entry
735  m_a30CqiRxed.insert ( std::pair<uint16_t, SbMeasResult_s > (rnti, params.m_cqiList.at (i).m_sbMeasResult) );
736  m_a30CqiTimers.insert ( std::pair<uint16_t, uint32_t > (rnti, m_cqiTimersThreshold));
737  }
738  else
739  {
740  // update the CQI value and refresh correspondent timer
741  (*it).second = params.m_cqiList.at (i).m_sbMeasResult;
742  std::map <uint16_t,uint32_t>::iterator itTimers;
743  itTimers = m_a30CqiTimers.find (rnti);
744  (*itTimers).second = m_cqiTimersThreshold;
745  }
746  }
747  else
748  {
749  NS_LOG_ERROR (this << " CQI type unknown");
750  }
751  }
752 
753  return;
754 }
755 
756 
757 double
758 TdTbfqFfMacScheduler::EstimateUlSinr (uint16_t rnti, uint16_t rb)
759 {
760  std::map <uint16_t, std::vector <double> >::iterator itCqi = m_ueCqi.find (rnti);
761  if (itCqi == m_ueCqi.end ())
762  {
763  // no cqi info about this UE
764  return (NO_SINR);
765 
766  }
767  else
768  {
769  // take the average SINR value among the available
770  double sinrSum = 0;
771  int sinrNum = 0;
772  for (uint32_t i = 0; i < m_cschedCellConfig.m_ulBandwidth; i++)
773  {
774  double sinr = (*itCqi).second.at (i);
775  if (sinr != NO_SINR)
776  {
777  sinrSum += sinr;
778  sinrNum++;
779  }
780  }
781  double estimatedSinr = sinrSum / (double)sinrNum;
782  // store the value
783  (*itCqi).second.at (rb) = estimatedSinr;
784  return (estimatedSinr);
785  }
786 }
787 
788 void
790 {
791  NS_LOG_FUNCTION (this << " UL - Frame no. " << (params.m_sfnSf >> 4) << " subframe no. " << (0xF & params.m_sfnSf));
792 
793  RefreshUlCqiMaps ();
794 
795  std::map <uint16_t,uint32_t>::iterator it;
796  int nflows = 0;
797 
798  for (it = m_ceBsrRxed.begin (); it != m_ceBsrRxed.end (); it++)
799  {
800  // remove old entries of this UE-LC
801  if ((*it).second > 0)
802  {
803  nflows++;
804  }
805  }
806 
807  if (nflows == 0)
808  {
809  return ; // no flows to be scheduled
810  }
811 
812 
813  // Divide the resource equally among the active users
814  int rbPerFlow = m_cschedCellConfig.m_ulBandwidth / nflows;
815  if (rbPerFlow == 0)
816  {
817  rbPerFlow = 1; // at least 1 rbg per flow (till available resource)
818  }
819  int rbAllocated = 0;
820 
822  std::vector <uint16_t> rbgAllocationMap;
823  if (m_nextRntiUl != 0)
824  {
825  for (it = m_ceBsrRxed.begin (); it != m_ceBsrRxed.end (); it++)
826  {
827  if ((*it).first == m_nextRntiUl)
828  {
829  break;
830  }
831  }
832  if (it == m_ceBsrRxed.end ())
833  {
834  NS_LOG_ERROR (this << " no user found");
835  }
836  }
837  else
838  {
839  it = m_ceBsrRxed.begin ();
840  m_nextRntiUl = (*it).first;
841  }
842  do
843  {
844  if (rbAllocated + rbPerFlow > m_cschedCellConfig.m_ulBandwidth)
845  {
846  // limit to physical resources last resource assignment
847  rbPerFlow = m_cschedCellConfig.m_ulBandwidth - rbAllocated;
848  }
849 
850  UlDciListElement_s uldci;
851  uldci.m_rnti = (*it).first;
852  uldci.m_rbStart = rbAllocated;
853  uldci.m_rbLen = rbPerFlow;
854  std::map <uint16_t, std::vector <double> >::iterator itCqi = m_ueCqi.find ((*it).first);
855  int cqi = 0;
856  if (itCqi == m_ueCqi.end ())
857  {
858  // no cqi info about this UE
859  uldci.m_mcs = 0; // MCS 0 -> UL-AMC TBD
860  }
861  else
862  {
863  // take the lowest CQI value (worst RB)
864  double minSinr = (*itCqi).second.at (uldci.m_rbStart);
865  if (minSinr == NO_SINR)
866  {
867  minSinr = EstimateUlSinr ((*it).first, uldci.m_rbStart);
868  }
869  for (uint16_t i = uldci.m_rbStart; i < uldci.m_rbStart + uldci.m_rbLen; i++)
870  {
871  double sinr = (*itCqi).second.at (i);
872  if (sinr == NO_SINR)
873  {
874  sinr = EstimateUlSinr ((*it).first, i);
875  }
876  if ((*itCqi).second.at (i) < minSinr)
877  {
878  minSinr = (*itCqi).second.at (i);
879  }
880  }
881 
882  // translate SINR -> cqi: WILD ACK: same as DL
883  double s = log2 ( 1 + (
884  std::pow (10, minSinr / 10 ) /
885  ( (-std::log (5.0 * 0.00005 )) / 1.5) ));
887  if (cqi == 0)
888  {
889  it++;
890  if (it == m_ceBsrRxed.end ())
891  {
892  // restart from the first
893  it = m_ceBsrRxed.begin ();
894  }
895  continue; // CQI == 0 means "out of range" (see table 7.2.3-1 of 36.213)
896  }
897  uldci.m_mcs = m_amc->GetMcsFromCqi (cqi);
898 
899  }
900 
901  rbAllocated += rbPerFlow;
902  // store info on allocation for managing ul-cqi interpretation
903  for (int i = 0; i < rbPerFlow; i++)
904  {
905  rbgAllocationMap.push_back ((*it).first);
906  }
907  uldci.m_tbSize = (m_amc->GetTbSizeFromMcs (uldci.m_mcs, rbPerFlow) / 8);
908  NS_LOG_DEBUG (this << " UE " << (*it).first << " startPRB " << (uint32_t)uldci.m_rbStart << " nPRB " << (uint32_t)uldci.m_rbLen << " CQI " << cqi << " MCS " << (uint32_t)uldci.m_mcs << " TBsize " << uldci.m_tbSize << " RbAlloc " << rbAllocated);
909  UpdateUlRlcBufferInfo (uldci.m_rnti, uldci.m_tbSize);
910  uldci.m_ndi = 1;
911  uldci.m_cceIndex = 0;
912  uldci.m_aggrLevel = 1;
913  uldci.m_ueTxAntennaSelection = 3; // antenna selection OFF
914  uldci.m_hopping = false;
915  uldci.m_n2Dmrs = 0;
916  uldci.m_tpc = 0; // no power control
917  uldci.m_cqiRequest = false; // only period CQI at this stage
918  uldci.m_ulIndex = 0; // TDD parameter
919  uldci.m_dai = 1; // TDD parameter
920  uldci.m_freqHopping = 0;
921  uldci.m_pdcchPowerOffset = 0; // not used
922  ret.m_dciList.push_back (uldci);
923 
924 
925  it++;
926  if (it == m_ceBsrRxed.end ())
927  {
928  // restart from the first
929  it = m_ceBsrRxed.begin ();
930  }
931  if (rbAllocated == m_cschedCellConfig.m_ulBandwidth)
932  {
933  // Stop allocation: no more PRBs
934  m_nextRntiUl = (*it).first;
935  break;
936  }
937  }
938  while ((*it).first != m_nextRntiUl);
939 
940  m_allocationMaps.insert (std::pair <uint16_t, std::vector <uint16_t> > (params.m_sfnSf, rbgAllocationMap));
942  return;
943 }
944 
945 void
947 {
948  NS_FATAL_ERROR ("unimplemented");
949  return;
950 }
951 
952 void
954 {
955  NS_FATAL_ERROR ("unimplemented");
956  return;
957 }
958 
959 void
961 {
962  NS_LOG_FUNCTION (this);
963 
964  std::map <uint16_t,uint32_t>::iterator it;
965 
966  for (unsigned int i = 0; i < params.m_macCeList.size (); i++)
967  {
968  if ( params.m_macCeList.at (i).m_macCeType == MacCeListElement_s::BSR )
969  {
970  // buffer status report
971  // note that we only consider LCG 0, the other three LCGs are neglected
972  // this is consistent with the assumption in LteUeMac that the first LCG gathers all LCs
973  uint16_t rnti = params.m_macCeList.at (i).m_rnti;
974  it = m_ceBsrRxed.find (rnti);
975  if (it == m_ceBsrRxed.end ())
976  {
977  // create the new entry
978  uint8_t bsrId = params.m_macCeList.at (i).m_macCeValue.m_bufferStatus.at (0);
979  int buffer = BufferSizeLevelBsr::BsrId2BufferSize (bsrId);
980  m_ceBsrRxed.insert ( std::pair<uint16_t, uint32_t > (rnti, buffer));
981  }
982  else
983  {
984  // update the buffer size value
985  (*it).second = BufferSizeLevelBsr::BsrId2BufferSize (params.m_macCeList.at (i).m_macCeValue.m_bufferStatus.at (0));
986  }
987  }
988  }
989 
990  return;
991 }
992 
993 void
995 {
996  NS_LOG_FUNCTION (this);
997  // retrieve the allocation for this subframe
998  switch (m_ulCqiFilter)
999  {
1001  {
1002  // filter all the CQIs that are not SRS based
1003  if (params.m_ulCqi.m_type!=UlCqi_s::SRS)
1004  {
1005  return;
1006  }
1007  }
1008  break;
1010  {
1011  // filter all the CQIs that are not SRS based
1012  if (params.m_ulCqi.m_type!=UlCqi_s::PUSCH)
1013  {
1014  return;
1015  }
1016  }
1018  break;
1019 
1020  default:
1021  NS_FATAL_ERROR ("Unknown UL CQI type");
1022  }
1023 
1024  switch (params.m_ulCqi.m_type)
1025  {
1026  case UlCqi_s::PUSCH:
1027  {
1028  std::map <uint16_t, std::vector <uint16_t> >::iterator itMap;
1029  std::map <uint16_t, std::vector <double> >::iterator itCqi;
1030  itMap = m_allocationMaps.find (params.m_sfnSf);
1031  if (itMap == m_allocationMaps.end ())
1032  {
1033  NS_LOG_DEBUG (this << " Does not find info on allocation, size : " << m_allocationMaps.size ());
1034  return;
1035  }
1036  for (uint32_t i = 0; i < (*itMap).second.size (); i++)
1037  {
1038  // convert from fixed point notation Sxxxxxxxxxxx.xxx to double
1039  // NS_LOG_INFO (this << " i " << i << " size " << params.m_ulCqi.m_sinr.size () << " mapSIze " << (*itMap).second.size ());
1040  double sinr = LteFfConverter::fpS11dot3toDouble (params.m_ulCqi.m_sinr.at (i));
1041  itCqi = m_ueCqi.find ((*itMap).second.at (i));
1042  if (itCqi == m_ueCqi.end ())
1043  {
1044  // create a new entry
1045  std::vector <double> newCqi;
1046  for (uint32_t j = 0; j < m_cschedCellConfig.m_ulBandwidth; j++)
1047  {
1048  if (i == j)
1049  {
1050  newCqi.push_back (sinr);
1051  }
1052  else
1053  {
1054  // initialize with NO_SINR value.
1055  newCqi.push_back (NO_SINR);
1056  }
1057 
1058  }
1059  m_ueCqi.insert (std::pair <uint16_t, std::vector <double> > ((*itMap).second.at (i), newCqi));
1060  // generate correspondent timer
1061  m_ueCqiTimers.insert (std::pair <uint16_t, uint32_t > ((*itMap).second.at (i), m_cqiTimersThreshold));
1062  }
1063  else
1064  {
1065  // update the value
1066  (*itCqi).second.at (i) = sinr;
1067  // update correspondent timer
1068  std::map <uint16_t, uint32_t>::iterator itTimers;
1069  itTimers = m_ueCqiTimers.find ((*itMap).second.at (i));
1070  (*itTimers).second = m_cqiTimersThreshold;
1071 
1072  }
1073 
1074  }
1075  // remove obsolete info on allocation
1076  m_allocationMaps.erase (itMap);
1077  }
1078  break;
1079  case UlCqi_s::SRS:
1080  {
1081  // get the RNTI from vendor specific parameters
1082  uint16_t rnti = 0;
1083  NS_ASSERT (params.m_vendorSpecificList.size () > 0);
1084  for (uint16_t i = 0; i < params.m_vendorSpecificList.size (); i++)
1085  {
1086  if (params.m_vendorSpecificList.at (i).m_type == SRS_CQI_RNTI_VSP)
1087  {
1088  Ptr<SrsCqiRntiVsp> vsp = DynamicCast<SrsCqiRntiVsp> (params.m_vendorSpecificList.at (i).m_value);
1089  rnti = vsp->GetRnti ();
1090  }
1091  }
1092  std::map <uint16_t, std::vector <double> >::iterator itCqi;
1093  itCqi = m_ueCqi.find (rnti);
1094  if (itCqi == m_ueCqi.end ())
1095  {
1096  // create a new entry
1097  std::vector <double> newCqi;
1098  for (uint32_t j = 0; j < m_cschedCellConfig.m_ulBandwidth; j++)
1099  {
1100  double sinr = LteFfConverter::fpS11dot3toDouble (params.m_ulCqi.m_sinr.at (j));
1101  newCqi.push_back (sinr);
1102  NS_LOG_DEBUG (this << " RNTI " << rnti << " new SRS-CQI for RB " << j << " value " << sinr);
1103 
1104  }
1105  m_ueCqi.insert (std::pair <uint16_t, std::vector <double> > (rnti, newCqi));
1106  // generate correspondent timer
1107  m_ueCqiTimers.insert (std::pair <uint16_t, uint32_t > (rnti, m_cqiTimersThreshold));
1108  }
1109  else
1110  {
1111  // update the values
1112  for (uint32_t j = 0; j < m_cschedCellConfig.m_ulBandwidth; j++)
1113  {
1114  double sinr = LteFfConverter::fpS11dot3toDouble (params.m_ulCqi.m_sinr.at (j));
1115  (*itCqi).second.at (j) = sinr;
1116  NS_LOG_DEBUG (this << " RNTI " << rnti << " update SRS-CQI for RB " << j << " value " << sinr);
1117  }
1118  // update correspondent timer
1119  std::map <uint16_t, uint32_t>::iterator itTimers;
1120  itTimers = m_ueCqiTimers.find (rnti);
1121  (*itTimers).second = m_cqiTimersThreshold;
1122 
1123  }
1124 
1125 
1126  }
1127  break;
1128  case UlCqi_s::PUCCH_1:
1129  case UlCqi_s::PUCCH_2:
1130  case UlCqi_s::PRACH:
1131  {
1132  NS_FATAL_ERROR ("TdTbfqFfMacScheduler supports only PUSCH and SRS UL-CQIs");
1133  }
1134  break;
1135  default:
1136  NS_FATAL_ERROR ("Unknown type of UL-CQI");
1137  }
1138  return;
1139 }
1140 
1141 void
1143 {
1144  // refresh DL CQI P01 Map
1145  std::map <uint16_t,uint32_t>::iterator itP10 = m_p10CqiTimers.begin ();
1146  while (itP10!=m_p10CqiTimers.end ())
1147  {
1148 // NS_LOG_INFO (this << " P10-CQI for user " << (*itP10).first << " is " << (uint32_t)(*itP10).second << " thr " << (uint32_t)m_cqiTimersThreshold);
1149  if ((*itP10).second == 0)
1150  {
1151  // delete correspondent entries
1152  std::map <uint16_t,uint8_t>::iterator itMap = m_p10CqiRxed.find ((*itP10).first);
1153  NS_ASSERT_MSG (itMap != m_p10CqiRxed.end (), " Does not find CQI report for user " << (*itP10).first);
1154  NS_LOG_INFO (this << " P10-CQI exired for user " << (*itP10).first);
1155  m_p10CqiRxed.erase (itMap);
1156  std::map <uint16_t,uint32_t>::iterator temp = itP10;
1157  itP10++;
1158  m_p10CqiTimers.erase (temp);
1159  }
1160  else
1161  {
1162  (*itP10).second--;
1163  itP10++;
1164  }
1165  }
1166 
1167  // refresh DL CQI A30 Map
1168  std::map <uint16_t,uint32_t>::iterator itA30 = m_a30CqiTimers.begin ();
1169  while (itA30!=m_a30CqiTimers.end ())
1170  {
1171 // NS_LOG_INFO (this << " A30-CQI for user " << (*itA30).first << " is " << (uint32_t)(*itA30).second << " thr " << (uint32_t)m_cqiTimersThreshold);
1172  if ((*itA30).second == 0)
1173  {
1174  // delete correspondent entries
1175  std::map <uint16_t,SbMeasResult_s>::iterator itMap = m_a30CqiRxed.find ((*itA30).first);
1176  NS_ASSERT_MSG (itMap != m_a30CqiRxed.end (), " Does not find CQI report for user " << (*itA30).first);
1177  NS_LOG_INFO (this << " A30-CQI exired for user " << (*itA30).first);
1178  m_a30CqiRxed.erase (itMap);
1179  std::map <uint16_t,uint32_t>::iterator temp = itA30;
1180  itA30++;
1181  m_a30CqiTimers.erase (temp);
1182  }
1183  else
1184  {
1185  (*itA30).second--;
1186  itA30++;
1187  }
1188  }
1189 
1190  return;
1191 }
1192 
1193 
1194 void
1196 {
1197  // refresh UL CQI Map
1198  std::map <uint16_t,uint32_t>::iterator itUl = m_ueCqiTimers.begin ();
1199  while (itUl!=m_ueCqiTimers.end ())
1200  {
1201 // NS_LOG_INFO (this << " UL-CQI for user " << (*itUl).first << " is " << (uint32_t)(*itUl).second << " thr " << (uint32_t)m_cqiTimersThreshold);
1202  if ((*itUl).second == 0)
1203  {
1204  // delete correspondent entries
1205  std::map <uint16_t, std::vector <double> >::iterator itMap = m_ueCqi.find ((*itUl).first);
1206  NS_ASSERT_MSG (itMap != m_ueCqi.end (), " Does not find CQI report for user " << (*itUl).first);
1207  NS_LOG_INFO (this << " UL-CQI exired for user " << (*itUl).first);
1208  (*itMap).second.clear ();
1209  m_ueCqi.erase (itMap);
1210  std::map <uint16_t,uint32_t>::iterator temp = itUl;
1211  itUl++;
1212  m_ueCqiTimers.erase (temp);
1213  }
1214  else
1215  {
1216  (*itUl).second--;
1217  itUl++;
1218  }
1219  }
1220 
1221  return;
1222 }
1223 
1224 void
1225 TdTbfqFfMacScheduler::UpdateDlRlcBufferInfo (uint16_t rnti, uint8_t lcid, uint16_t size)
1226 {
1227  size = size - 2; // remove the minimum RLC overhead
1228  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it;
1229  LteFlowId_t flow (rnti, lcid);
1230  it = m_rlcBufferReq.find (flow);
1231  if (it!=m_rlcBufferReq.end ())
1232  {
1233  // Update queues: RLC tx order Status, ReTx, Tx
1234  // Update status queue
1235  if ((*it).second.m_rlcStatusPduSize <= size)
1236  {
1237  size -= (*it).second.m_rlcStatusPduSize;
1238  (*it).second.m_rlcStatusPduSize = 0;
1239  }
1240  else
1241  {
1242  (*it).second.m_rlcStatusPduSize -= size;
1243  return;
1244  }
1245  // update retransmission queue
1246  if ((*it).second.m_rlcRetransmissionQueueSize <= size)
1247  {
1248  size -= (*it).second.m_rlcRetransmissionQueueSize;
1249  (*it).second.m_rlcRetransmissionQueueSize = 0;
1250  }
1251  else
1252  {
1253  (*it).second.m_rlcRetransmissionQueueSize -= size;
1254  return;
1255  }
1256  // update transmission queue
1257  if ((*it).second.m_rlcTransmissionQueueSize <= size)
1258  {
1259  size -= (*it).second.m_rlcTransmissionQueueSize;
1260  (*it).second.m_rlcTransmissionQueueSize = 0;
1261  }
1262  else
1263  {
1264  (*it).second.m_rlcTransmissionQueueSize -= size;
1265  return;
1266  }
1267  }
1268  else
1269  {
1270  NS_LOG_ERROR (this << " Does not find DL RLC Buffer Report of UE " << rnti);
1271  }
1272 }
1273 
1274 void
1275 TdTbfqFfMacScheduler::UpdateUlRlcBufferInfo (uint16_t rnti, uint16_t size)
1276 {
1277 
1278  size = size - 2; // remove the minimum RLC overhead
1279  std::map <uint16_t,uint32_t>::iterator it = m_ceBsrRxed.find (rnti);
1280  if (it!=m_ceBsrRxed.end ())
1281  {
1282  if ((*it).second >= size)
1283  {
1284  (*it).second -= size;
1285  }
1286  else
1287  {
1288  (*it).second = 0;
1289  }
1290  }
1291  else
1292  {
1293  NS_LOG_ERROR (this << " Does not find BSR report info of UE " << rnti);
1294  }
1295 
1296 }
1297 
1298 void
1300 {
1301  NS_LOG_FUNCTION (this << " RNTI " << rnti << " txMode " << (uint16_t)txMode);
1303  params.m_rnti = rnti;
1304  params.m_transmissionMode = txMode;
1306 }
1307 
1308 
1309 }