A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fdtbfq-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>
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/math.h>
25 
26 #include <ns3/simulator.h>
27 #include <ns3/lte-amc.h>
28 #include <ns3/fdtbfq-ff-mac-scheduler.h>
29 #include <ns3/lte-vendor-specific-parameters.h>
30 #include <ns3/boolean.h>
31 #include <ns3/integer.h>
32 #include <set>
33 #include <cfloat>
34 
35 NS_LOG_COMPONENT_DEFINE ("FdTbfqFfMacScheduler");
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 
47 NS_OBJECT_ENSURE_REGISTERED (FdTbfqFfMacScheduler);
48 
49 
50 
52 {
53 public:
55 
56  // inherited from FfMacCschedSapProvider
57  virtual void CschedCellConfigReq (const struct CschedCellConfigReqParameters& params);
58  virtual void CschedUeConfigReq (const struct CschedUeConfigReqParameters& params);
59  virtual void CschedLcConfigReq (const struct CschedLcConfigReqParameters& params);
60  virtual void CschedLcReleaseReq (const struct CschedLcReleaseReqParameters& params);
61  virtual void CschedUeReleaseReq (const struct CschedUeReleaseReqParameters& params);
62 
63 private:
66 };
67 
69 {
70 }
71 
73 {
74 }
75 
76 
77 void
79 {
81 }
82 
83 void
85 {
87 }
88 
89 
90 void
92 {
94 }
95 
96 void
98 {
100 }
101 
102 void
104 {
106 }
107 
108 
109 
110 
112 {
113 public:
115 
116  // inherited from FfMacSchedSapProvider
117  virtual void SchedDlRlcBufferReq (const struct SchedDlRlcBufferReqParameters& params);
118  virtual void SchedDlPagingBufferReq (const struct SchedDlPagingBufferReqParameters& params);
119  virtual void SchedDlMacBufferReq (const struct SchedDlMacBufferReqParameters& params);
120  virtual void SchedDlTriggerReq (const struct SchedDlTriggerReqParameters& params);
121  virtual void SchedDlRachInfoReq (const struct SchedDlRachInfoReqParameters& params);
122  virtual void SchedDlCqiInfoReq (const struct SchedDlCqiInfoReqParameters& params);
123  virtual void SchedUlTriggerReq (const struct SchedUlTriggerReqParameters& params);
124  virtual void SchedUlNoiseInterferenceReq (const struct SchedUlNoiseInterferenceReqParameters& params);
125  virtual void SchedUlSrInfoReq (const struct SchedUlSrInfoReqParameters& params);
126  virtual void SchedUlMacCtrlInfoReq (const struct SchedUlMacCtrlInfoReqParameters& params);
127  virtual void SchedUlCqiInfoReq (const struct SchedUlCqiInfoReqParameters& params);
128 
129 
130 private:
133 };
134 
135 
136 
138 {
139 }
140 
141 
143  : m_scheduler (scheduler)
144 {
145 }
146 
147 void
149 {
151 }
152 
153 void
155 {
157 }
158 
159 void
161 {
163 }
164 
165 void
167 {
169 }
170 
171 void
173 {
175 }
176 
177 void
179 {
181 }
182 
183 void
185 {
187 }
188 
189 void
191 {
193 }
194 
195 void
197 {
199 }
200 
201 void
203 {
205 }
206 
207 void
209 {
211 }
212 
213 
214 
215 
216 
218  : m_cschedSapUser (0),
219  m_schedSapUser (0),
220  m_timeWindow (99.0),
221  m_nextRntiUl (0),
222  bankSize (0)
223 {
224  m_amc = CreateObject <LteAmc> ();
227 }
228 
230 {
231  NS_LOG_FUNCTION (this);
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION (this);
239  m_dlHarqProcessesTimer.clear ();
241  m_dlInfoListBuffered.clear ();
242  m_ulHarqCurrentProcessId.clear ();
243  m_ulHarqProcessesStatus.clear ();
245  delete m_cschedSapProvider;
246  delete m_schedSapProvider;
247 }
248 
249 TypeId
251 {
252  static TypeId tid = TypeId ("ns3::FdTbfqFfMacScheduler")
254  .AddConstructor<FdTbfqFfMacScheduler> ()
255  .AddAttribute ("CqiTimerThreshold",
256  "The number of TTIs a CQI is valid (default 1000 - 1 sec.)",
257  UintegerValue (1000),
258  MakeUintegerAccessor (&FdTbfqFfMacScheduler::m_cqiTimersThreshold),
259  MakeUintegerChecker<uint32_t> ())
260  .AddAttribute ("DebtLimit",
261  "Flow debt limit (default -625000 bytes)",
262  IntegerValue (-625000),
263  MakeIntegerAccessor (&FdTbfqFfMacScheduler::m_debtLimit),
264  MakeIntegerChecker<int> ())
265  .AddAttribute ("CreditLimit",
266  "Flow credit limit (default 625000 bytes)",
267  UintegerValue (625000),
268  MakeUintegerAccessor (&FdTbfqFfMacScheduler::m_creditLimit),
269  MakeUintegerChecker<uint32_t> ())
270  .AddAttribute ("TokenPoolSize",
271  "The maximum value of flow token pool (default 1 bytes)",
272  UintegerValue (1),
273  MakeUintegerAccessor (&FdTbfqFfMacScheduler::m_tokenPoolSize),
274  MakeUintegerChecker<uint32_t> ())
275  .AddAttribute ("CreditableThreshold",
276  "Threshold of flow credit (default 0 bytes)",
277  UintegerValue (0),
278  MakeUintegerAccessor (&FdTbfqFfMacScheduler::m_creditableThreshold),
279  MakeUintegerChecker<uint32_t> ())
280 
281  .AddAttribute ("HarqEnabled",
282  "Activate/Deactivate the HARQ [by default is active].",
283  BooleanValue (true),
284  MakeBooleanAccessor (&FdTbfqFfMacScheduler::m_harqOn),
285  MakeBooleanChecker ())
286  .AddAttribute ("UlGrantMcs",
287  "The MCS of the UL grant, must be [0..15] (default 0)",
288  UintegerValue (0),
289  MakeUintegerAccessor (&FdTbfqFfMacScheduler::m_ulGrantMcs),
290  MakeUintegerChecker<uint8_t> ())
291  ;
292  return tid;
293 }
294 
295 
296 
297 void
299 {
300  m_cschedSapUser = s;
301 }
302 
303 void
305 {
306  m_schedSapUser = s;
307 }
308 
311 {
312  return m_cschedSapProvider;
313 }
314 
317 {
318  return m_schedSapProvider;
319 }
320 
321 void
323 {
324  NS_LOG_FUNCTION (this);
325  // Read the subset of parameters used
326  m_cschedCellConfig = params;
329  cnf.m_result = SUCCESS;
331  return;
332 }
333 
334 void
336 {
337  NS_LOG_FUNCTION (this << " RNTI " << params.m_rnti << " txMode " << (uint16_t)params.m_transmissionMode);
338  std::map <uint16_t,uint8_t>::iterator it = m_uesTxMode.find (params.m_rnti);
339  if (it == m_uesTxMode.end ())
340  {
341  m_uesTxMode.insert (std::pair <uint16_t, double> (params.m_rnti, params.m_transmissionMode));
342  // generate HARQ buffers
343  m_dlHarqCurrentProcessId.insert (std::pair <uint16_t,uint8_t > (params.m_rnti, 0));
344  DlHarqProcessesStatus_t dlHarqPrcStatus;
345  dlHarqPrcStatus.resize (8,0);
346  m_dlHarqProcessesStatus.insert (std::pair <uint16_t, DlHarqProcessesStatus_t> (params.m_rnti, dlHarqPrcStatus));
347  DlHarqProcessesTimer_t dlHarqProcessesTimer;
348  dlHarqProcessesTimer.resize (8,0);
349  m_dlHarqProcessesTimer.insert (std::pair <uint16_t, DlHarqProcessesTimer_t> (params.m_rnti, dlHarqProcessesTimer));
350  DlHarqProcessesDciBuffer_t dlHarqdci;
351  dlHarqdci.resize (8);
352  m_dlHarqProcessesDciBuffer.insert (std::pair <uint16_t, DlHarqProcessesDciBuffer_t> (params.m_rnti, dlHarqdci));
353  DlHarqRlcPduListBuffer_t dlHarqRlcPdu;
354  dlHarqRlcPdu.resize (2);
355  dlHarqRlcPdu.at (0).resize (8);
356  dlHarqRlcPdu.at (1).resize (8);
357  m_dlHarqProcessesRlcPduListBuffer.insert (std::pair <uint16_t, DlHarqRlcPduListBuffer_t> (params.m_rnti, dlHarqRlcPdu));
358  m_ulHarqCurrentProcessId.insert (std::pair <uint16_t,uint8_t > (params.m_rnti, 0));
359  UlHarqProcessesStatus_t ulHarqPrcStatus;
360  ulHarqPrcStatus.resize (8,0);
361  m_ulHarqProcessesStatus.insert (std::pair <uint16_t, UlHarqProcessesStatus_t> (params.m_rnti, ulHarqPrcStatus));
362  UlHarqProcessesDciBuffer_t ulHarqdci;
363  ulHarqdci.resize (8);
364  m_ulHarqProcessesDciBuffer.insert (std::pair <uint16_t, UlHarqProcessesDciBuffer_t> (params.m_rnti, ulHarqdci));
365  }
366  else
367  {
368  (*it).second = params.m_transmissionMode;
369  }
370  return;
371 }
372 
373 void
375 {
376  NS_LOG_FUNCTION (this << " New LC, rnti: " << params.m_rnti);
377 
378  std::map <uint16_t, fdtbfqsFlowPerf_t>::iterator it;
379  for (uint16_t i = 0; i < params.m_logicalChannelConfigList.size (); i++)
380  {
381  it = m_flowStatsDl.find (params.m_rnti);
382 
383  if (it == m_flowStatsDl.end ())
384  {
385  uint64_t mbrDlInBytes = params.m_logicalChannelConfigList.at (i).m_eRabMaximulBitrateDl / 8; // byte/s
386  uint64_t mbrUlInBytes = params.m_logicalChannelConfigList.at (i).m_eRabMaximulBitrateUl / 8; // byte/s
387 
388  fdtbfqsFlowPerf_t flowStatsDl;
389  flowStatsDl.flowStart = Simulator::Now ();
390  flowStatsDl.packetArrivalRate = 0;
391  flowStatsDl.tokenGenerationRate = mbrDlInBytes;
392  flowStatsDl.tokenPoolSize = 0;
393  flowStatsDl.maxTokenPoolSize = m_tokenPoolSize;
394  flowStatsDl.counter = 0;
395  flowStatsDl.burstCredit = m_creditLimit; // bytes
396  flowStatsDl.debtLimit = m_debtLimit; // bytes
398  m_flowStatsDl.insert (std::pair<uint16_t, fdtbfqsFlowPerf_t> (params.m_rnti, flowStatsDl));
399  fdtbfqsFlowPerf_t flowStatsUl;
400  flowStatsUl.flowStart = Simulator::Now ();
401  flowStatsUl.packetArrivalRate = 0;
402  flowStatsUl.tokenGenerationRate = mbrUlInBytes;
403  flowStatsUl.tokenPoolSize = 0;
404  flowStatsUl.maxTokenPoolSize = m_tokenPoolSize;
405  flowStatsUl.counter = 0;
406  flowStatsUl.burstCredit = m_creditLimit; // bytes
407  flowStatsUl.debtLimit = m_debtLimit; // bytes
409  m_flowStatsUl.insert (std::pair<uint16_t, fdtbfqsFlowPerf_t> (params.m_rnti, flowStatsUl));
410  }
411  else
412  {
413  // update MBR and GBR from UeManager::SetupDataRadioBearer ()
414  uint64_t mbrDlInBytes = params.m_logicalChannelConfigList.at (i).m_eRabMaximulBitrateDl / 8; // byte/s
415  uint64_t mbrUlInBytes = params.m_logicalChannelConfigList.at (i).m_eRabMaximulBitrateUl / 8; // byte/s
416  m_flowStatsDl[(*it).first].tokenGenerationRate = mbrDlInBytes;
417  m_flowStatsUl[(*it).first].tokenGenerationRate = mbrUlInBytes;
418 
419  }
420  }
421 
422  return;
423 }
424 
425 void
427 {
428  NS_LOG_FUNCTION (this);
429  for (uint16_t i = 0; i < params.m_logicalChannelIdentity.size (); i++)
430  {
431  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it = m_rlcBufferReq.begin ();
432  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator temp;
433  while (it!=m_rlcBufferReq.end ())
434  {
435  if (((*it).first.m_rnti == params.m_rnti) && ((*it).first.m_lcId == params.m_logicalChannelIdentity.at (i)))
436  {
437  temp = it;
438  it++;
439  m_rlcBufferReq.erase (temp);
440  }
441  else
442  {
443  it++;
444  }
445  }
446  }
447  return;
448 }
449 
450 void
452 {
453  NS_LOG_FUNCTION (this);
454 
455  m_uesTxMode.erase (params.m_rnti);
456  m_dlHarqCurrentProcessId.erase (params.m_rnti);
457  m_dlHarqProcessesStatus.erase (params.m_rnti);
458  m_dlHarqProcessesTimer.erase (params.m_rnti);
459  m_dlHarqProcessesDciBuffer.erase (params.m_rnti);
461  m_ulHarqCurrentProcessId.erase (params.m_rnti);
462  m_ulHarqProcessesStatus.erase (params.m_rnti);
463  m_ulHarqProcessesDciBuffer.erase (params.m_rnti);
464  m_flowStatsDl.erase (params.m_rnti);
465  m_flowStatsUl.erase (params.m_rnti);
466  m_ceBsrRxed.erase (params.m_rnti);
467  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it = m_rlcBufferReq.begin ();
468  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator temp;
469  while (it!=m_rlcBufferReq.end ())
470  {
471  if ((*it).first.m_rnti == params.m_rnti)
472  {
473  temp = it;
474  it++;
475  m_rlcBufferReq.erase (temp);
476  }
477  else
478  {
479  it++;
480  }
481  }
482  if (m_nextRntiUl == params.m_rnti)
483  {
484  m_nextRntiUl = 0;
485  }
486 
487  return;
488 }
489 
490 
491 void
493 {
494  NS_LOG_FUNCTION (this << params.m_rnti << (uint32_t) params.m_logicalChannelIdentity);
495  // API generated by RLC for updating RLC parameters on a LC (tx and retx queues)
496 
497  std::map <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it;
498 
499  LteFlowId_t flow (params.m_rnti, params.m_logicalChannelIdentity);
500 
501  it = m_rlcBufferReq.find (flow);
502 
503  if (it == m_rlcBufferReq.end ())
504  {
505  m_rlcBufferReq.insert (std::pair <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters> (flow, params));
506  }
507  else
508  {
509  (*it).second = params;
510  }
511 
512  return;
513 }
514 
515 void
517 {
518  NS_LOG_FUNCTION (this);
519  NS_FATAL_ERROR ("method not implemented");
520  return;
521 }
522 
523 void
525 {
526  NS_LOG_FUNCTION (this);
527  NS_FATAL_ERROR ("method not implemented");
528  return;
529 }
530 
531 int
533 {
534  for (int i = 0; i < 4; i++)
535  {
536  if (dlbandwidth < FdTbfqType0AllocationRbg[i])
537  {
538  return (i + 1);
539  }
540  }
541 
542  return (-1);
543 }
544 
545 
546 int
548 {
549  std::map <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it;
550  int lcActive = 0;
551  for (it = m_rlcBufferReq.begin (); it != m_rlcBufferReq.end (); it++)
552  {
553  if (((*it).first.m_rnti == rnti) && (((*it).second.m_rlcTransmissionQueueSize > 0)
554  || ((*it).second.m_rlcRetransmissionQueueSize > 0)
555  || ((*it).second.m_rlcStatusPduSize > 0) ))
556  {
557  lcActive++;
558  }
559  if ((*it).first.m_rnti > rnti)
560  {
561  break;
562  }
563  }
564  return (lcActive);
565 
566 }
567 
568 
569 uint8_t
571 {
572  NS_LOG_FUNCTION (this << rnti);
573 
574  std::map <uint16_t, uint8_t>::iterator it = m_dlHarqCurrentProcessId.find (rnti);
575  if (it == m_dlHarqCurrentProcessId.end ())
576  {
577  NS_FATAL_ERROR ("No Process Id found for this RNTI " << rnti);
578  }
579  std::map <uint16_t, DlHarqProcessesStatus_t>::iterator itStat = m_dlHarqProcessesStatus.find (rnti);
580  if (itStat == m_dlHarqProcessesStatus.end ())
581  {
582  NS_FATAL_ERROR ("No Process Id Statusfound for this RNTI " << rnti);
583  }
584  uint8_t i = (*it).second;
585  do
586  {
587  i = (i + 1) % HARQ_PROC_NUM;
588  }
589  while ( ((*itStat).second.at (i) != 0)&&(i != (*it).second));
590  if ((*itStat).second.at (i) == 0)
591  {
592  return (true);
593  }
594  else
595  {
596  return (false); // return a not valid harq proc id
597  }
598 }
599 
600 
601 
602 uint8_t
604 {
605  NS_LOG_FUNCTION (this << rnti);
606 
607  if (m_harqOn == false)
608  {
609  return (0);
610  }
611 
612 
613  std::map <uint16_t, uint8_t>::iterator it = m_dlHarqCurrentProcessId.find (rnti);
614  if (it == m_dlHarqCurrentProcessId.end ())
615  {
616  NS_FATAL_ERROR ("No Process Id found for this RNTI " << rnti);
617  }
618  std::map <uint16_t, DlHarqProcessesStatus_t>::iterator itStat = m_dlHarqProcessesStatus.find (rnti);
619  if (itStat == m_dlHarqProcessesStatus.end ())
620  {
621  NS_FATAL_ERROR ("No Process Id Statusfound for this RNTI " << rnti);
622  }
623  uint8_t i = (*it).second;
624  do
625  {
626  i = (i + 1) % HARQ_PROC_NUM;
627  }
628  while ( ((*itStat).second.at (i) != 0)&&(i != (*it).second));
629  if ((*itStat).second.at (i) == 0)
630  {
631  (*it).second = i;
632  (*itStat).second.at (i) = 1;
633  }
634  else
635  {
636  NS_FATAL_ERROR ("No HARQ process available for RNTI " << rnti << " check before update with HarqProcessAvailability");
637  }
638 
639  return ((*it).second);
640 }
641 
642 
643 void
645 {
646  NS_LOG_FUNCTION (this);
647 
648  std::map <uint16_t, DlHarqProcessesTimer_t>::iterator itTimers;
649  for (itTimers = m_dlHarqProcessesTimer.begin (); itTimers != m_dlHarqProcessesTimer.end (); itTimers ++)
650  {
651  for (uint16_t i = 0; i < HARQ_PROC_NUM; i++)
652  {
653  if ((*itTimers).second.at (i) == HARQ_DL_TIMEOUT)
654  {
655  // reset HARQ process
656 
657  NS_LOG_DEBUG (this << " Reset HARQ proc " << i << " for RNTI " << (*itTimers).first);
658  std::map <uint16_t, DlHarqProcessesStatus_t>::iterator itStat = m_dlHarqProcessesStatus.find ((*itTimers).first);
659  if (itStat == m_dlHarqProcessesStatus.end ())
660  {
661  NS_FATAL_ERROR ("No Process Id Status found for this RNTI " << (*itTimers).first);
662  }
663  (*itStat).second.at (i) = 0;
664  (*itTimers).second.at (i) = 0;
665  }
666  else
667  {
668  (*itTimers).second.at (i)++;
669  }
670  }
671  }
672 
673 }
674 
675 
676 void
678 {
679  NS_LOG_FUNCTION (this << " Frame no. " << (params.m_sfnSf >> 4) << " subframe no. " << (0xF & params.m_sfnSf));
680  // API generated by RLC for triggering the scheduling of a DL subframe
681 
682 
683  // evaluate the relative channel quality indicator for each UE per each RBG
684  // (since we are using allocation type 0 the small unit of allocation is RBG)
685  // Resource allocation type 0 (see sec 7.1.6.1 of 36.213)
686 
687  RefreshDlCqiMaps ();
688 
690  int rbgNum = m_cschedCellConfig.m_dlBandwidth / rbgSize;
691  std::map <uint16_t, std::vector <uint16_t> > allocationMap; // RBs map per RNTI
692  std::vector <bool> rbgMap; // global RBGs map
693  uint16_t rbgAllocatedNum = 0;
694  std::set <uint16_t> rntiAllocated;
695  rbgMap.resize (m_cschedCellConfig.m_dlBandwidth / rbgSize, false);
697 
698  // RACH Allocation
700  uint16_t rbStart = 0;
701  std::vector <struct RachListElement_s>::iterator itRach;
702  for (itRach = m_rachList.begin (); itRach != m_rachList.end (); itRach++)
703  {
704  NS_ASSERT_MSG (m_amc->GetTbSizeFromMcs (m_ulGrantMcs, m_cschedCellConfig.m_ulBandwidth) > (*itRach).m_estimatedSize, " Default UL Grant MCS does not allow to send RACH messages");
705  BuildRarListElement_s newRar;
706  newRar.m_rnti = (*itRach).m_rnti;
707  // DL-RACH Allocation
708  // Ideal: no needs of configuring m_dci
709  // UL-RACH Allocation
710  newRar.m_grant.m_rnti = newRar.m_rnti;
711  newRar.m_grant.m_mcs = m_ulGrantMcs;
712  uint16_t rbLen = 1;
713  uint16_t tbSizeBits = 0;
714  // find lowest TB size that fits UL grant estimated size
715  while ((tbSizeBits < (*itRach).m_estimatedSize) && (rbStart + rbLen < m_cschedCellConfig.m_ulBandwidth))
716  {
717  rbLen++;
718  tbSizeBits = m_amc->GetTbSizeFromMcs (m_ulGrantMcs, rbLen);
719  }
720  if (tbSizeBits < (*itRach).m_estimatedSize)
721  {
722  // no more allocation space: finish allocation
723  break;
724  }
725  newRar.m_grant.m_rbStart = rbStart;
726  newRar.m_grant.m_rbLen = rbLen;
727  newRar.m_grant.m_tbSize = tbSizeBits / 8;
728  newRar.m_grant.m_hopping = false;
729  newRar.m_grant.m_tpc = 0;
730  newRar.m_grant.m_cqiRequest = false;
731  newRar.m_grant.m_ulDelay = false;
732  NS_LOG_INFO (this << " UL grant allocated to RNTI " << (*itRach).m_rnti << " rbStart " << rbStart << " rbLen " << rbLen << " MCS " << m_ulGrantMcs << " tbSize " << newRar.m_grant.m_tbSize);
733  for (uint16_t i = rbStart; i < rbStart + rbLen; i++)
734  {
735  m_rachAllocationMap.at (i) = (*itRach).m_rnti;
736  }
737  rbStart = rbStart + rbLen;
738 
739  ret.m_buildRarList.push_back (newRar);
740  }
741  m_rachList.clear ();
742 
743 
744  // Process DL HARQ feedback
746  // retrieve past HARQ retx buffered
747  if (m_dlInfoListBuffered.size () > 0)
748  {
749  if (params.m_dlInfoList.size () > 0)
750  {
751  NS_LOG_INFO (this << " Received DL-HARQ feedback");
752  m_dlInfoListBuffered.insert (m_dlInfoListBuffered.end (), params.m_dlInfoList.begin (), params.m_dlInfoList.end ());
753  }
754  }
755  else
756  {
757  if (params.m_dlInfoList.size () > 0)
758  {
760  }
761  }
762  if (m_harqOn == false)
763  {
764  // Ignore HARQ feedback
765  m_dlInfoListBuffered.clear ();
766  }
767  std::vector <struct DlInfoListElement_s> dlInfoListUntxed;
768  for (uint16_t i = 0; i < m_dlInfoListBuffered.size (); i++)
769  {
770  std::set <uint16_t>::iterator itRnti = rntiAllocated.find (m_dlInfoListBuffered.at (i).m_rnti);
771  if (itRnti != rntiAllocated.end ())
772  {
773  // RNTI already allocated for retx
774  continue;
775  }
776  uint8_t nLayers = m_dlInfoListBuffered.at (i).m_harqStatus.size ();
777  std::vector <bool> retx;
778  NS_LOG_INFO (this << " Processing DLHARQ feedback");
779  if (nLayers == 1)
780  {
781  retx.push_back (m_dlInfoListBuffered.at (i).m_harqStatus.at (0) == DlInfoListElement_s::NACK);
782  retx.push_back (false);
783  }
784  else
785  {
786  retx.push_back (m_dlInfoListBuffered.at (i).m_harqStatus.at (0) == DlInfoListElement_s::NACK);
787  retx.push_back (m_dlInfoListBuffered.at (i).m_harqStatus.at (1) == DlInfoListElement_s::NACK);
788  }
789  if (retx.at (0) || retx.at (1))
790  {
791  // retrieve HARQ process information
792  uint16_t rnti = m_dlInfoListBuffered.at (i).m_rnti;
793  uint8_t harqId = m_dlInfoListBuffered.at (i).m_harqProcessId;
794  NS_LOG_INFO (this << " HARQ retx RNTI " << rnti << " harqId " << (uint16_t)harqId);
795  std::map <uint16_t, DlHarqProcessesDciBuffer_t>::iterator itHarq = m_dlHarqProcessesDciBuffer.find (rnti);
796  if (itHarq == m_dlHarqProcessesDciBuffer.end ())
797  {
798  NS_FATAL_ERROR ("No info find in HARQ buffer for UE " << rnti);
799  }
800 
801  DlDciListElement_s dci = (*itHarq).second.at (harqId);
802  int rv = 0;
803  if (dci.m_rv.size () == 1)
804  {
805  rv = dci.m_rv.at (0);
806  }
807  else
808  {
809  rv = (dci.m_rv.at (0) > dci.m_rv.at (1) ? dci.m_rv.at (0) : dci.m_rv.at (1));
810  }
811 
812  if (rv == 3)
813  {
814  // maximum number of retx reached -> drop process
815  NS_LOG_INFO ("Maximum number of retransmissions reached -> drop process");
816  std::map <uint16_t, DlHarqProcessesStatus_t>::iterator it = m_dlHarqProcessesStatus.find (rnti);
817  if (it == m_dlHarqProcessesStatus.end ())
818  {
819  NS_LOG_ERROR ("No info find in HARQ buffer for UE (might change eNB) " << m_dlInfoListBuffered.at (i).m_rnti);
820  }
821  (*it).second.at (harqId) = 0;
822  std::map <uint16_t, DlHarqRlcPduListBuffer_t>::iterator itRlcPdu = m_dlHarqProcessesRlcPduListBuffer.find (rnti);
823  if (itRlcPdu == m_dlHarqProcessesRlcPduListBuffer.end ())
824  {
825  NS_FATAL_ERROR ("Unable to find RlcPdcList in HARQ buffer for RNTI " << m_dlInfoListBuffered.at (i).m_rnti);
826  }
827  for (uint16_t k = 0; k < (*itRlcPdu).second.size (); k++)
828  {
829  (*itRlcPdu).second.at (k).at (harqId).clear ();
830  }
831  continue;
832  }
833  // check the feasibility of retransmitting on the same RBGs
834  // translate the DCI to Spectrum framework
835  std::vector <int> dciRbg;
836  uint32_t mask = 0x1;
837  NS_LOG_INFO ("Original RBGs " << dci.m_rbBitmap << " rnti " << dci.m_rnti);
838  for (int j = 0; j < 32; j++)
839  {
840  if (((dci.m_rbBitmap & mask) >> j) == 1)
841  {
842  dciRbg.push_back (j);
843  NS_LOG_INFO ("\t" << j);
844  }
845  mask = (mask << 1);
846  }
847  bool free = true;
848  for (uint8_t j = 0; j < dciRbg.size (); j++)
849  {
850  if (rbgMap.at (dciRbg.at (j)) == true)
851  {
852  free = false;
853  break;
854  }
855  }
856  if (free)
857  {
858  // use the same RBGs for the retx
859  // reserve RBGs
860  for (uint8_t j = 0; j < dciRbg.size (); j++)
861  {
862  rbgMap.at (dciRbg.at (j)) = true;
863  NS_LOG_INFO ("RBG " << dciRbg.at (j) << " assigned");
864  rbgAllocatedNum++;
865  }
866 
867  NS_LOG_INFO (this << " Send retx in the same RBGs");
868  }
869  else
870  {
871  // find RBGs for sending HARQ retx
872  uint8_t j = 0;
873  uint8_t rbgId = (dciRbg.at (dciRbg.size () - 1) + 1) % rbgNum;
874  uint8_t startRbg = dciRbg.at (dciRbg.size () - 1);
875  std::vector <bool> rbgMapCopy = rbgMap;
876  while ((j < dciRbg.size ())&&(startRbg != rbgId))
877  {
878  if (rbgMapCopy.at (rbgId) == false)
879  {
880  rbgMapCopy.at (rbgId) = true;
881  dciRbg.at (j) = rbgId;
882  j++;
883  }
884  rbgId++;
885  }
886  if (j == dciRbg.size ())
887  {
888  // find new RBGs -> update DCI map
889  uint32_t rbgMask = 0;
890  for (uint16_t k = 0; k < dciRbg.size (); k++)
891  {
892  rbgMask = rbgMask + (0x1 << dciRbg.at (k));
893  rbgAllocatedNum++;
894  }
895  dci.m_rbBitmap = rbgMask;
896  rbgMap = rbgMapCopy;
897  NS_LOG_INFO (this << " Move retx in RBGs " << dciRbg.size ());
898  }
899  else
900  {
901  // HARQ retx cannot be performed on this TTI -> store it
902  dlInfoListUntxed.push_back (params.m_dlInfoList.at (i));
903  NS_LOG_INFO (this << " No resource for this retx -> buffer it");
904  }
905  }
906  // retrieve RLC PDU list for retx TBsize and update DCI
908  std::map <uint16_t, DlHarqRlcPduListBuffer_t>::iterator itRlcPdu = m_dlHarqProcessesRlcPduListBuffer.find (rnti);
909  if (itRlcPdu == m_dlHarqProcessesRlcPduListBuffer.end ())
910  {
911  NS_FATAL_ERROR ("Unable to find RlcPdcList in HARQ buffer for RNTI " << rnti);
912  }
913  for (uint8_t j = 0; j < nLayers; j++)
914  {
915  if (retx.at (j))
916  {
917  if (j >= dci.m_ndi.size ())
918  {
919  // for avoiding errors in MIMO transient phases
920  dci.m_ndi.push_back (0);
921  dci.m_rv.push_back (0);
922  dci.m_mcs.push_back (0);
923  dci.m_tbsSize.push_back (0);
924  NS_LOG_INFO (this << " layer " << (uint16_t)j << " no txed (MIMO transition)");
925  }
926  else
927  {
928  dci.m_ndi.at (j) = 0;
929  dci.m_rv.at (j)++;
930  (*itHarq).second.at (harqId).m_rv.at (j)++;
931  NS_LOG_INFO (this << " layer " << (uint16_t)j << " RV " << (uint16_t)dci.m_rv.at (j));
932  }
933  }
934  else
935  {
936  // empty TB of layer j
937  dci.m_ndi.at (j) = 0;
938  dci.m_rv.at (j) = 0;
939  dci.m_mcs.at (j) = 0;
940  dci.m_tbsSize.at (j) = 0;
941  NS_LOG_INFO (this << " layer " << (uint16_t)j << " no retx");
942  }
943  }
944  for (uint16_t k = 0; k < (*itRlcPdu).second.at (0).at (dci.m_harqProcess).size (); k++)
945  {
946  std::vector <struct RlcPduListElement_s> rlcPduListPerLc;
947  for (uint8_t j = 0; j < nLayers; j++)
948  {
949  if (retx.at (j))
950  {
951  if (j < dci.m_ndi.size ())
952  {
953  rlcPduListPerLc.push_back ((*itRlcPdu).second.at (j).at (dci.m_harqProcess).at (k));
954  }
955  }
956  }
957 
958  if (rlcPduListPerLc.size () > 0)
959  {
960  newEl.m_rlcPduList.push_back (rlcPduListPerLc);
961  }
962  }
963  newEl.m_rnti = rnti;
964  newEl.m_dci = dci;
965  (*itHarq).second.at (harqId).m_rv = dci.m_rv;
966  // refresh timer
967  std::map <uint16_t, DlHarqProcessesTimer_t>::iterator itHarqTimer = m_dlHarqProcessesTimer.find (rnti);
968  if (itHarqTimer== m_dlHarqProcessesTimer.end ())
969  {
970  NS_FATAL_ERROR ("Unable to find HARQ timer for RNTI " << (uint16_t)rnti);
971  }
972  (*itHarqTimer).second.at (harqId) = 0;
973  ret.m_buildDataList.push_back (newEl);
974  rntiAllocated.insert (rnti);
975  }
976  else
977  {
978  // update HARQ process status
979  NS_LOG_INFO (this << " HARQ received ACK for UE " << m_dlInfoListBuffered.at (i).m_rnti);
980  std::map <uint16_t, DlHarqProcessesStatus_t>::iterator it = m_dlHarqProcessesStatus.find (m_dlInfoListBuffered.at (i).m_rnti);
981  if (it == m_dlHarqProcessesStatus.end ())
982  {
983  NS_FATAL_ERROR ("No info find in HARQ buffer for UE " << m_dlInfoListBuffered.at (i).m_rnti);
984  }
985  (*it).second.at (m_dlInfoListBuffered.at (i).m_harqProcessId) = 0;
986  std::map <uint16_t, DlHarqRlcPduListBuffer_t>::iterator itRlcPdu = m_dlHarqProcessesRlcPduListBuffer.find (m_dlInfoListBuffered.at (i).m_rnti);
987  if (itRlcPdu == m_dlHarqProcessesRlcPduListBuffer.end ())
988  {
989  NS_FATAL_ERROR ("Unable to find RlcPdcList in HARQ buffer for RNTI " << m_dlInfoListBuffered.at (i).m_rnti);
990  }
991  for (uint16_t k = 0; k < (*itRlcPdu).second.size (); k++)
992  {
993  (*itRlcPdu).second.at (k).at (m_dlInfoListBuffered.at (i).m_harqProcessId).clear ();
994  }
995  }
996  }
997  m_dlInfoListBuffered.clear ();
998  m_dlInfoListBuffered = dlInfoListUntxed;
999 
1000 
1001  // update token pool, counter and bank size
1002  std::map <uint16_t, fdtbfqsFlowPerf_t>::iterator itStats;
1003  for (itStats = m_flowStatsDl.begin (); itStats != m_flowStatsDl.end (); itStats++)
1004  {
1005  if ( (*itStats).second.tokenGenerationRate / 1000 + (*itStats).second.tokenPoolSize > (*itStats).second.maxTokenPoolSize )
1006  {
1007  (*itStats).second.counter += (*itStats).second.tokenGenerationRate / 1000 - ( (*itStats).second.maxTokenPoolSize - (*itStats).second.tokenPoolSize );
1008  (*itStats).second.tokenPoolSize = (*itStats).second.maxTokenPoolSize;
1009  bankSize += (*itStats).second.tokenGenerationRate / 1000 - ( (*itStats).second.maxTokenPoolSize - (*itStats).second.tokenPoolSize );
1010  }
1011  else
1012  {
1013  (*itStats).second.tokenPoolSize += (*itStats).second.tokenGenerationRate / 1000;
1014  }
1015  }
1016 
1017  std::set <uint16_t> allocatedRnti; // store UEs which are already assigned RBGs
1018  std::set <uint8_t> allocatedRbg; // store RBGs which are already allocated to UE
1019 
1020  int totalRbg = 0;
1021  while (totalRbg < rbgNum)
1022  {
1023  // select UE with largest metric
1024  std::map <uint16_t, fdtbfqsFlowPerf_t>::iterator it;
1025  std::map <uint16_t, fdtbfqsFlowPerf_t>::iterator itMax = m_flowStatsDl.end ();
1026  double metricMax = 0.0;
1027  bool firstRnti = true;
1028  for (it = m_flowStatsDl.begin (); it != m_flowStatsDl.end (); it++)
1029  {
1030  std::set <uint16_t>::iterator itRnti = rntiAllocated.find ((*it).first);
1031  if ((itRnti != rntiAllocated.end ())||(!HarqProcessAvailability ((*it).first)))
1032  {
1033  // UE already allocated for HARQ or without HARQ process available -> drop it
1034  if (itRnti != rntiAllocated.end ())
1035  {
1036  NS_LOG_DEBUG (this << " RNTI discared for HARQ tx" << (uint16_t)(*it).first);
1037  }
1038  if (!HarqProcessAvailability ((*it).first))
1039  {
1040  NS_LOG_DEBUG (this << " RNTI discared for HARQ id" << (uint16_t)(*it).first);
1041  }
1042  continue;
1043  }
1044 
1045  if (LcActivePerFlow ((*it).first) == 0)
1046  {
1047  continue;
1048  }
1049 
1050  std::set <uint16_t>::iterator rnti;
1051  rnti = allocatedRnti.find((*it).first);
1052  if (rnti != allocatedRnti.end ()) // already allocated RBGs to this UE
1053  {
1054  continue;
1055  }
1056 
1057  double metric = ( ( (double)(*it).second.counter ) / ( (double)(*it).second.tokenGenerationRate ) );
1058 
1059  if (firstRnti == true)
1060  {
1061  metricMax = metric;
1062  itMax = it;
1063  firstRnti = false;
1064  continue;
1065  }
1066  if (metric > metricMax)
1067  {
1068  metricMax = metric;
1069  itMax = it;
1070  }
1071  } // end for m_flowStatsDl
1072 
1073  if (itMax == m_flowStatsDl.end())
1074  {
1075  // all UEs are allocated RBG or all UEs already allocated for HARQ or without HARQ process available
1076  break;
1077  }
1078 
1079  // mark this UE as "allocated"
1080  allocatedRnti.insert((*itMax).first);
1081 
1082  // calculate the maximum number of byte that the scheduler can assigned to this UE
1083  uint32_t budget = 0;
1084  if ( bankSize > 0 )
1085  {
1086  budget = (*itMax).second.counter - (*itMax).second.debtLimit;
1087  if ( budget > (*itMax).second.burstCredit )
1088  budget = (*itMax).second.burstCredit;
1089  if ( budget > bankSize )
1090  budget = bankSize;
1091  }
1092  budget = budget + (*itMax).second.tokenPoolSize;
1093 
1094  // calcualte how much bytes this UE actally need
1095  if (budget == 0)
1096  {
1097  // there are no tokens for this UE
1098  continue;
1099  }
1100  else
1101  {
1102  // calculate rlc buffer size
1103  uint32_t rlcBufSize = 0;
1104  uint8_t lcid = 0;
1105  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator itRlcBuf;
1106  for (itRlcBuf = m_rlcBufferReq.begin (); itRlcBuf != m_rlcBufferReq.end (); itRlcBuf++)
1107  {
1108  if ( (*itRlcBuf).first.m_rnti == (*itMax).first )
1109  lcid = (*itRlcBuf).first.m_lcId;
1110  }
1111  LteFlowId_t flow ((*itMax).first, lcid);
1112  itRlcBuf = m_rlcBufferReq.find (flow);
1113  if (itRlcBuf!=m_rlcBufferReq.end ())
1114  rlcBufSize = (*itRlcBuf).second.m_rlcTransmissionQueueSize + (*itRlcBuf).second.m_rlcRetransmissionQueueSize + (*itRlcBuf).second.m_rlcStatusPduSize;
1115  if ( budget > rlcBufSize )
1116  budget = rlcBufSize;
1117  }
1118 
1119  // assign RBGs to this UE
1120  uint32_t bytesTxed = 0;
1121  uint32_t bytesTxedTmp = 0;
1122  int rbgIndex = 0;
1123  while ( bytesTxed <= budget )
1124  {
1125  totalRbg++;
1126 
1127  std::map <uint16_t,SbMeasResult_s>::iterator itCqi;
1128  itCqi = m_a30CqiRxed.find ((*itMax).first);
1129  std::map <uint16_t,uint8_t>::iterator itTxMode;
1130  itTxMode = m_uesTxMode.find ((*itMax).first);
1131  if (itTxMode == m_uesTxMode.end ())
1132  {
1133  NS_FATAL_ERROR ("No Transmission Mode info on user " << (*it).first);
1134  }
1135  int nLayer = TransmissionModesLayers::TxMode2LayerNum ((*itTxMode).second);
1136 
1137  // find RBG with largest achievableRate
1138  double achievableRateMax = 0.0;
1139  rbgIndex = rbgNum;
1140  for (int k = 0; k < rbgNum; k++)
1141  {
1142  std::set <uint8_t>::iterator rbg;
1143  rbg = allocatedRbg.find (k);
1144  if (rbg != allocatedRbg.end ()) // RBGs are already allocated to this UE
1145  continue;
1146 
1147  if ( rbgMap.at (k) == true) // this RBG is allocated in RACH procedure
1148  continue;
1149 
1150  std::vector <uint8_t> sbCqi;
1151  if (itCqi == m_a30CqiRxed.end ())
1152  {
1153  for (uint8_t k = 0; k < nLayer; k++)
1154  {
1155  sbCqi.push_back (1); // start with lowest value
1156  }
1157  }
1158  else
1159  {
1160  sbCqi = (*itCqi).second.m_higherLayerSelected.at (k).m_sbCqi;
1161  }
1162  uint8_t cqi1 = sbCqi.at (0);
1163  uint8_t cqi2 = 1;
1164  if (sbCqi.size () > 1)
1165  {
1166  cqi2 = sbCqi.at (1);
1167  }
1168 
1169  if ((cqi1 > 0)||(cqi2 > 0)) // CQI == 0 means "out of range" (see table 7.2.3-1 of 36.213)
1170  {
1171  if (LcActivePerFlow ((*itMax).first) > 0)
1172  {
1173  // this UE has data to transmit
1174  double achievableRate = 0.0;
1175  for (uint8_t j = 0; j < nLayer; j++)
1176  {
1177  uint8_t mcs = 0;
1178  if (sbCqi.size () > j)
1179  {
1180  mcs = m_amc->GetMcsFromCqi (sbCqi.at (j));
1181  }
1182  else
1183  {
1184  // no info on this subband -> worst MCS
1185  mcs = 0;
1186  }
1187  achievableRate += ((m_amc->GetTbSizeFromMcs (mcs, rbgSize) / 8) / 0.001); // = TB size / TTI
1188  }
1189 
1190  if ( achievableRate > achievableRateMax )
1191  {
1192  achievableRateMax = achievableRate;
1193  rbgIndex = k;
1194  }
1195  } // end of LcActivePerFlow
1196  } // end of cqi
1197  } // end of for rbgNum
1198 
1199  if ( rbgIndex == rbgNum) // impossible
1200  {
1201  // all RBGs are already assigned
1202  totalRbg = rbgNum;
1203  break;
1204  }
1205  else
1206  {
1207  // mark this UE as "allocated"
1208  allocatedRbg.insert (rbgIndex);
1209  }
1210 
1211  // assign this RBG to UE
1212  std::map <uint16_t, std::vector <uint16_t> >::iterator itMap;
1213  itMap = allocationMap.find ((*itMax).first);
1214  uint16_t RbgPerRnti;
1215  if (itMap == allocationMap.end ())
1216  {
1217  // insert new element
1218  std::vector <uint16_t> tempMap;
1219  tempMap.push_back (rbgIndex);
1220  allocationMap.insert (std::pair <uint16_t, std::vector <uint16_t> > ((*itMax).first, tempMap));
1221  itMap = allocationMap.find ((*itMax).first); // point itMap to the first RBGs assigned to this UE
1222  }
1223  else
1224  {
1225  (*itMap).second.push_back (rbgIndex);
1226  }
1227  rbgMap.at (rbgIndex) = true; // Mark this RBG as allocated
1228 
1229  RbgPerRnti = (*itMap).second.size();
1230 
1231  // calculate tb size
1232  std::vector <uint8_t> worstCqi (2, 15);
1233  if (itCqi != m_a30CqiRxed.end ())
1234  {
1235  for (uint16_t k = 0; k < (*itMap).second.size (); k++)
1236  {
1237  if ((*itCqi).second.m_higherLayerSelected.size () > (*itMap).second.at (k))
1238  {
1239  for (uint8_t j = 0; j < nLayer; j++)
1240  {
1241  if ((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.size () > j)
1242  {
1243  if (((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (j)) < worstCqi.at (j))
1244  {
1245  worstCqi.at (j) = ((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (j));
1246  }
1247  }
1248  else
1249  {
1250  // no CQI for this layer of this suband -> worst one
1251  worstCqi.at (j) = 1;
1252  }
1253  }
1254  }
1255  else
1256  {
1257  for (uint8_t j = 0; j < nLayer; j++)
1258  {
1259  worstCqi.at (j) = 1; // try with lowest MCS in RBG with no info on channel
1260  }
1261  }
1262  }
1263  }
1264  else
1265  {
1266  for (uint8_t j = 0; j < nLayer; j++)
1267  {
1268  worstCqi.at (j) = 1; // try with lowest MCS in RBG with no info on channel
1269  }
1270  }
1271 
1272  bytesTxedTmp = bytesTxed;
1273  bytesTxed = 0;
1274  for (uint8_t j = 0; j < nLayer; j++)
1275  {
1276  int tbSize = (m_amc->GetTbSizeFromMcs (m_amc->GetMcsFromCqi (worstCqi.at (j)), RbgPerRnti * rbgSize) / 8); // (size of TB in bytes according to table 7.1.7.2.1-1 of 36.213)
1277  bytesTxed += tbSize;
1278  }
1279 
1280  } // end of while()
1281 
1282  // remove and unmark last RBG assigned to UE
1283  if ( bytesTxed > budget )
1284  {
1285  std::map <uint16_t, std::vector <uint16_t> >::iterator itMap;
1286  itMap = allocationMap.find ((*itMax).first);
1287  (*itMap).second.pop_back ();
1288  allocatedRbg.erase (rbgIndex);
1289  bytesTxed = bytesTxedTmp; // recovery bytesTxed
1290  totalRbg--;
1291  rbgMap.at (rbgIndex) = false; // unmark this RBG
1292  }
1293 
1294  // update UE stats
1295  if ( bytesTxed <= (*itMax).second.tokenPoolSize )
1296  {
1297  (*itMax).second.tokenPoolSize -= bytesTxed;
1298  }
1299  else
1300  {
1301  (*itMax).second.counter = (*itMax).second.counter - ( bytesTxed - (*itMax).second.tokenPoolSize );
1302  (*itMax).second.tokenPoolSize = 0;
1303  if (bankSize <= ( bytesTxed - (*itMax).second.tokenPoolSize ))
1304  bankSize = 0;
1305  else
1306  bankSize = bankSize - ( bytesTxed - (*itMax).second.tokenPoolSize );
1307  }
1308  } // end of RBGs
1309 
1310  // generate the transmission opportunities by grouping the RBGs of the same RNTI and
1311  // creating the correspondent DCIs
1312  std::map <uint16_t, std::vector <uint16_t> >::iterator itMap = allocationMap.begin ();
1313  while (itMap != allocationMap.end ())
1314  {
1315  // create new BuildDataListElement_s for this LC
1316  BuildDataListElement_s newEl;
1317  newEl.m_rnti = (*itMap).first;
1318  // create the DlDciListElement_s
1319  DlDciListElement_s newDci;
1320  newDci.m_rnti = (*itMap).first;
1321  newDci.m_harqProcess = UpdateHarqProcessId ((*itMap).first);
1322 
1323  uint16_t lcActives = LcActivePerFlow ((*itMap).first);
1324  NS_LOG_INFO (this << "Allocate user " << newEl.m_rnti << " rbg " << lcActives);
1325  if (lcActives == 0)
1326  {
1327  // Set to max value, to avoid divide by 0 below
1328  lcActives = (uint16_t)65535; // UINT16_MAX;
1329  }
1330  uint16_t RgbPerRnti = (*itMap).second.size ();
1331  std::map <uint16_t,SbMeasResult_s>::iterator itCqi;
1332  itCqi = m_a30CqiRxed.find ((*itMap).first);
1333  std::map <uint16_t,uint8_t>::iterator itTxMode;
1334  itTxMode = m_uesTxMode.find ((*itMap).first);
1335  if (itTxMode == m_uesTxMode.end ())
1336  {
1337  NS_FATAL_ERROR ("No Transmission Mode info on user " << (*itMap).first);
1338  }
1339  int nLayer = TransmissionModesLayers::TxMode2LayerNum ((*itTxMode).second);
1340  std::vector <uint8_t> worstCqi (2, 15);
1341  if (itCqi != m_a30CqiRxed.end ())
1342  {
1343  for (uint16_t k = 0; k < (*itMap).second.size (); k++)
1344  {
1345  if ((*itCqi).second.m_higherLayerSelected.size () > (*itMap).second.at (k))
1346  {
1347  NS_LOG_INFO (this << " RBG " << (*itMap).second.at (k) << " CQI " << (uint16_t)((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (0)) );
1348  for (uint8_t j = 0; j < nLayer; j++)
1349  {
1350  if ((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.size () > j)
1351  {
1352  if (((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (j)) < worstCqi.at (j))
1353  {
1354  worstCqi.at (j) = ((*itCqi).second.m_higherLayerSelected.at ((*itMap).second.at (k)).m_sbCqi.at (j));
1355  }
1356  }
1357  else
1358  {
1359  // no CQI for this layer of this suband -> worst one
1360  worstCqi.at (j) = 1;
1361  }
1362  }
1363  }
1364  else
1365  {
1366  for (uint8_t j = 0; j < nLayer; j++)
1367  {
1368  worstCqi.at (j) = 1; // try with lowest MCS in RBG with no info on channel
1369  }
1370  }
1371  }
1372  }
1373  else
1374  {
1375  for (uint8_t j = 0; j < nLayer; j++)
1376  {
1377  worstCqi.at (j) = 1; // try with lowest MCS in RBG with no info on channel
1378  }
1379  }
1380  for (uint8_t j = 0; j < nLayer; j++)
1381  {
1382  NS_LOG_INFO (this << " Layer " << (uint16_t)j << " CQI selected " << (uint16_t)worstCqi.at (j));
1383  }
1384  uint32_t bytesTxed = 0;
1385  for (uint8_t j = 0; j < nLayer; j++)
1386  {
1387  newDci.m_mcs.push_back (m_amc->GetMcsFromCqi (worstCqi.at (j)));
1388  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)
1389  newDci.m_tbsSize.push_back (tbSize);
1390  NS_LOG_INFO (this << " Layer " << (uint16_t)j << " MCS selected" << m_amc->GetMcsFromCqi (worstCqi.at (j)));
1391  bytesTxed += tbSize;
1392  }
1393 
1394  newDci.m_resAlloc = 0; // only allocation type 0 at this stage
1395  newDci.m_rbBitmap = 0; // TBD (32 bit bitmap see 7.1.6 of 36.213)
1396  uint32_t rbgMask = 0;
1397  for (uint16_t k = 0; k < (*itMap).second.size (); k++)
1398  {
1399  rbgMask = rbgMask + (0x1 << (*itMap).second.at (k));
1400  NS_LOG_INFO (this << " Allocated RBG " << (*itMap).second.at (k));
1401  }
1402  newDci.m_rbBitmap = rbgMask; // (32 bit bitmap see 7.1.6 of 36.213)
1403 
1404  // create the rlc PDUs -> equally divide resources among actives LCs
1405  std::map <LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator itBufReq;
1406  for (itBufReq = m_rlcBufferReq.begin (); itBufReq != m_rlcBufferReq.end (); itBufReq++)
1407  {
1408  if (((*itBufReq).first.m_rnti == (*itMap).first)
1409  && (((*itBufReq).second.m_rlcTransmissionQueueSize > 0)
1410  || ((*itBufReq).second.m_rlcRetransmissionQueueSize > 0)
1411  || ((*itBufReq).second.m_rlcStatusPduSize > 0) ))
1412  {
1413  std::vector <struct RlcPduListElement_s> newRlcPduLe;
1414  for (uint8_t j = 0; j < nLayer; j++)
1415  {
1416  RlcPduListElement_s newRlcEl;
1417  newRlcEl.m_logicalChannelIdentity = (*itBufReq).first.m_lcId;
1418  newRlcEl.m_size = newDci.m_tbsSize.at (j) / lcActives;
1419  NS_LOG_INFO (this << " LCID " << (uint32_t) newRlcEl.m_logicalChannelIdentity << " size " << newRlcEl.m_size << " layer " << (uint16_t)j);
1420  newRlcPduLe.push_back (newRlcEl);
1421  UpdateDlRlcBufferInfo (newDci.m_rnti, newRlcEl.m_logicalChannelIdentity, newRlcEl.m_size);
1422  if (m_harqOn == true)
1423  {
1424  // store RLC PDU list for HARQ
1425  std::map <uint16_t, DlHarqRlcPduListBuffer_t>::iterator itRlcPdu = m_dlHarqProcessesRlcPduListBuffer.find ((*itMap).first);
1426  if (itRlcPdu == m_dlHarqProcessesRlcPduListBuffer.end ())
1427  {
1428  NS_FATAL_ERROR ("Unable to find RlcPdcList in HARQ buffer for RNTI " << (*itMap).first);
1429  }
1430  (*itRlcPdu).second.at (j).at (newDci.m_harqProcess).push_back (newRlcEl);
1431  }
1432  }
1433  newEl.m_rlcPduList.push_back (newRlcPduLe);
1434  }
1435  if ((*itBufReq).first.m_rnti > (*itMap).first)
1436  {
1437  break;
1438  }
1439  }
1440  for (uint8_t j = 0; j < nLayer; j++)
1441  {
1442  newDci.m_ndi.push_back (1);
1443  newDci.m_rv.push_back (0);
1444  }
1445 
1446  newEl.m_dci = newDci;
1447 
1448  if (m_harqOn == true)
1449  {
1450  // store DCI for HARQ
1451  std::map <uint16_t, DlHarqProcessesDciBuffer_t>::iterator itDci = m_dlHarqProcessesDciBuffer.find (newEl.m_rnti);
1452  if (itDci == m_dlHarqProcessesDciBuffer.end ())
1453  {
1454  NS_FATAL_ERROR ("Unable to find RNTI entry in DCI HARQ buffer for RNTI " << newEl.m_rnti);
1455  }
1456  (*itDci).second.at (newDci.m_harqProcess) = newDci;
1457  // refresh timer
1458  std::map <uint16_t, DlHarqProcessesTimer_t>::iterator itHarqTimer = m_dlHarqProcessesTimer.find (newEl.m_rnti);
1459  if (itHarqTimer== m_dlHarqProcessesTimer.end ())
1460  {
1461  NS_FATAL_ERROR ("Unable to find HARQ timer for RNTI " << (uint16_t)newEl.m_rnti);
1462  }
1463  (*itHarqTimer).second.at (newDci.m_harqProcess) = 0;
1464  }
1465 
1466  // ...more parameters -> ingored in this version
1467 
1468  ret.m_buildDataList.push_back (newEl);
1469 
1470  itMap++;
1471  } // end while allocation
1472  ret.m_nrOfPdcchOfdmSymbols = 1;
1473 
1475 
1476 
1477  return;
1478 }
1479 
1480 void
1482 {
1483  NS_LOG_FUNCTION (this);
1484 
1485  m_rachList = params.m_rachList;
1486 
1487  return;
1488 }
1489 
1490 void
1492 {
1493  NS_LOG_FUNCTION (this);
1494 
1495  for (unsigned int i = 0; i < params.m_cqiList.size (); i++)
1496  {
1497  if ( params.m_cqiList.at (i).m_cqiType == CqiListElement_s::P10 )
1498  {
1499  // wideband CQI reporting
1500  std::map <uint16_t,uint8_t>::iterator it;
1501  uint16_t rnti = params.m_cqiList.at (i).m_rnti;
1502  it = m_p10CqiRxed.find (rnti);
1503  if (it == m_p10CqiRxed.end ())
1504  {
1505  // create the new entry
1506  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)
1507  // generate correspondent timer
1508  m_p10CqiTimers.insert ( std::pair<uint16_t, uint32_t > (rnti, m_cqiTimersThreshold));
1509  }
1510  else
1511  {
1512  // update the CQI value and refresh correspondent timer
1513  (*it).second = params.m_cqiList.at (i).m_wbCqi.at (0);
1514  // update correspondent timer
1515  std::map <uint16_t,uint32_t>::iterator itTimers;
1516  itTimers = m_p10CqiTimers.find (rnti);
1517  (*itTimers).second = m_cqiTimersThreshold;
1518  }
1519  }
1520  else if ( params.m_cqiList.at (i).m_cqiType == CqiListElement_s::A30 )
1521  {
1522  // subband CQI reporting high layer configured
1523  std::map <uint16_t,SbMeasResult_s>::iterator it;
1524  uint16_t rnti = params.m_cqiList.at (i).m_rnti;
1525  it = m_a30CqiRxed.find (rnti);
1526  if (it == m_a30CqiRxed.end ())
1527  {
1528  // create the new entry
1529  m_a30CqiRxed.insert ( std::pair<uint16_t, SbMeasResult_s > (rnti, params.m_cqiList.at (i).m_sbMeasResult) );
1530  m_a30CqiTimers.insert ( std::pair<uint16_t, uint32_t > (rnti, m_cqiTimersThreshold));
1531  }
1532  else
1533  {
1534  // update the CQI value and refresh correspondent timer
1535  (*it).second = params.m_cqiList.at (i).m_sbMeasResult;
1536  std::map <uint16_t,uint32_t>::iterator itTimers;
1537  itTimers = m_a30CqiTimers.find (rnti);
1538  (*itTimers).second = m_cqiTimersThreshold;
1539  }
1540  }
1541  else
1542  {
1543  NS_LOG_ERROR (this << " CQI type unknown");
1544  }
1545  }
1546 
1547  return;
1548 }
1549 
1550 
1551 double
1552 FdTbfqFfMacScheduler::EstimateUlSinr (uint16_t rnti, uint16_t rb)
1553 {
1554  std::map <uint16_t, std::vector <double> >::iterator itCqi = m_ueCqi.find (rnti);
1555  if (itCqi == m_ueCqi.end ())
1556  {
1557  // no cqi info about this UE
1558  return (NO_SINR);
1559 
1560  }
1561  else
1562  {
1563  // take the average SINR value among the available
1564  double sinrSum = 0;
1565  int sinrNum = 0;
1566  for (uint32_t i = 0; i < m_cschedCellConfig.m_ulBandwidth; i++)
1567  {
1568  double sinr = (*itCqi).second.at (i);
1569  if (sinr != NO_SINR)
1570  {
1571  sinrSum += sinr;
1572  sinrNum++;
1573  }
1574  }
1575  double estimatedSinr = (sinrNum > 0) ? (sinrSum / sinrNum) : DBL_MAX;
1576  // store the value
1577  (*itCqi).second.at (rb) = estimatedSinr;
1578  return (estimatedSinr);
1579  }
1580 }
1581 
1582 void
1584 {
1585  NS_LOG_FUNCTION (this << " UL - Frame no. " << (params.m_sfnSf >> 4) << " subframe no. " << (0xF & params.m_sfnSf) << " size " << params.m_ulInfoList.size ());
1586 
1587  RefreshUlCqiMaps ();
1588 
1589  // Generate RBs map
1591  std::vector <bool> rbMap;
1592  uint16_t rbAllocatedNum = 0;
1593  std::set <uint16_t> rntiAllocated;
1594  std::vector <uint16_t> rbgAllocationMap;
1595  // update with RACH allocation map
1596  rbgAllocationMap = m_rachAllocationMap;
1597  //rbgAllocationMap.resize (m_cschedCellConfig.m_ulBandwidth, 0);
1598  m_rachAllocationMap.clear ();
1600 
1601  rbMap.resize (m_cschedCellConfig.m_ulBandwidth, false);
1602  // remove RACH allocation
1603  for (uint16_t i = 0; i < m_cschedCellConfig.m_ulBandwidth; i++)
1604  {
1605  if (rbgAllocationMap.at (i) != 0)
1606  {
1607  rbMap.at (i) = true;
1608  NS_LOG_DEBUG (this << " Allocated for RACH " << i);
1609  }
1610  }
1611 
1612 
1613  if (m_harqOn == true)
1614  {
1615  // Process UL HARQ feedback
1616  // update UL HARQ proc id
1617  std::map <uint16_t, uint8_t>::iterator itProcId;
1618  for (itProcId = m_ulHarqCurrentProcessId.begin (); itProcId != m_ulHarqCurrentProcessId.end (); itProcId++)
1619  {
1620  (*itProcId).second = ((*itProcId).second + 1) % HARQ_PROC_NUM;
1621  }
1622 
1623  for (uint16_t i = 0; i < params.m_ulInfoList.size (); i++)
1624  {
1625  if (params.m_ulInfoList.at (i).m_receptionStatus == UlInfoListElement_s::NotOk)
1626  {
1627  // retx correspondent block: retrieve the UL-DCI
1628  uint16_t rnti = params.m_ulInfoList.at (i).m_rnti;
1629  itProcId = m_ulHarqCurrentProcessId.find (rnti);
1630  if (itProcId == m_ulHarqCurrentProcessId.end ())
1631  {
1632  NS_LOG_ERROR ("No info find in HARQ buffer for UE (might change eNB) " << rnti);
1633  }
1634  uint8_t harqId = (uint8_t)((*itProcId).second - HARQ_PERIOD) % HARQ_PROC_NUM;
1635  NS_LOG_INFO (this << " UL-HARQ retx RNTI " << rnti << " harqId " << (uint16_t)harqId << " i " << i << " size " << params.m_ulInfoList.size ());
1636  std::map <uint16_t, UlHarqProcessesDciBuffer_t>::iterator itHarq = m_ulHarqProcessesDciBuffer.find (rnti);
1637  if (itHarq == m_ulHarqProcessesDciBuffer.end ())
1638  {
1639  NS_LOG_ERROR ("No info find in HARQ buffer for UE (might change eNB) " << rnti);
1640  continue;
1641  }
1642  UlDciListElement_s dci = (*itHarq).second.at (harqId);
1643  std::map <uint16_t, UlHarqProcessesStatus_t>::iterator itStat = m_ulHarqProcessesStatus.find (rnti);
1644  if (itStat == m_ulHarqProcessesStatus.end ())
1645  {
1646  NS_LOG_ERROR ("No info find in HARQ buffer for UE (might change eNB) " << rnti);
1647  }
1648  if ((*itStat).second.at (harqId) >= 3)
1649  {
1650  NS_LOG_INFO ("Max number of retransmissions reached (UL)-> drop process");
1651  continue;
1652  }
1653  bool free = true;
1654  for (int j = dci.m_rbStart; j < dci.m_rbStart + dci.m_rbLen; j++)
1655  {
1656  if (rbMap.at (j) == true)
1657  {
1658  free = false;
1659  NS_LOG_INFO (this << " BUSY " << j);
1660  }
1661  }
1662  if (free)
1663  {
1664  // retx on the same RBs
1665  for (int j = dci.m_rbStart; j < dci.m_rbStart + dci.m_rbLen; j++)
1666  {
1667  rbMap.at (j) = true;
1668  rbgAllocationMap.at (j) = dci.m_rnti;
1669  NS_LOG_INFO ("\tRB " << j);
1670  rbAllocatedNum++;
1671  }
1672  NS_LOG_INFO (this << " Send retx in the same RBs " << (uint16_t)dci.m_rbStart << " to " << dci.m_rbStart + dci.m_rbLen << " RV " << (*itStat).second.at (harqId) + 1);
1673  }
1674  else
1675  {
1676  NS_LOG_INFO ("Cannot allocate retx due to RACH allocations for UE " << rnti);
1677  continue;
1678  }
1679  dci.m_ndi = 0;
1680  // Update HARQ buffers with new HarqId
1681  (*itStat).second.at ((*itProcId).second) = (*itStat).second.at (harqId) + 1;
1682  (*itStat).second.at (harqId) = 0;
1683  (*itHarq).second.at ((*itProcId).second) = dci;
1684  ret.m_dciList.push_back (dci);
1685  rntiAllocated.insert (dci.m_rnti);
1686  }
1687  else
1688  {
1689  NS_LOG_INFO (this << " HARQ-ACK feedback from RNTI " << params.m_ulInfoList.at (i).m_rnti);
1690  }
1691  }
1692  }
1693 
1694  std::map <uint16_t,uint32_t>::iterator it;
1695  int nflows = 0;
1696 
1697  for (it = m_ceBsrRxed.begin (); it != m_ceBsrRxed.end (); it++)
1698  {
1699  std::set <uint16_t>::iterator itRnti = rntiAllocated.find ((*it).first);
1700  // select UEs with queues not empty and not yet allocated for HARQ
1701  if (((*it).second > 0)&&(itRnti == rntiAllocated.end ()))
1702  {
1703  nflows++;
1704  }
1705  }
1706 
1707  if (nflows == 0)
1708  {
1709  if (ret.m_dciList.size () > 0)
1710  {
1712  }
1713 
1714  return; // no flows to be scheduled
1715  }
1716 
1717 
1718  // Divide the remaining resources equally among the active users starting from the subsequent one served last scheduling trigger
1719  uint16_t rbPerFlow = (m_cschedCellConfig.m_ulBandwidth) / (nflows + rntiAllocated.size ());
1720  if (rbPerFlow < 3)
1721  {
1722  rbPerFlow = 3; // at least 3 rbg per flow (till available resource) to ensure TxOpportunity >= 7 bytes
1723  }
1724  int rbAllocated = 0;
1725 
1726  std::map <uint16_t, fdtbfqsFlowPerf_t>::iterator itStats;
1727  if (m_nextRntiUl != 0)
1728  {
1729  for (it = m_ceBsrRxed.begin (); it != m_ceBsrRxed.end (); it++)
1730  {
1731  if ((*it).first == m_nextRntiUl)
1732  {
1733  break;
1734  }
1735  }
1736  if (it == m_ceBsrRxed.end ())
1737  {
1738  NS_LOG_ERROR (this << " no user found");
1739  }
1740  }
1741  else
1742  {
1743  it = m_ceBsrRxed.begin ();
1744  m_nextRntiUl = (*it).first;
1745  }
1746  do
1747  {
1748  std::set <uint16_t>::iterator itRnti = rntiAllocated.find ((*it).first);
1749  if ((itRnti != rntiAllocated.end ())||((*it).second == 0))
1750  {
1751  // UE already allocated for UL-HARQ -> skip it
1752  NS_LOG_DEBUG (this << " UE already allocated in HARQ -> discared, RNTI " << (*it).first);
1753  it++;
1754  if (it == m_ceBsrRxed.end ())
1755  {
1756  // restart from the first
1757  it = m_ceBsrRxed.begin ();
1758  }
1759  continue;
1760  }
1761  if (rbAllocated + rbPerFlow - 1 > m_cschedCellConfig.m_ulBandwidth)
1762  {
1763  // limit to physical resources last resource assignment
1764  rbPerFlow = m_cschedCellConfig.m_ulBandwidth - rbAllocated;
1765  // at least 3 rbg per flow to ensure TxOpportunity >= 7 bytes
1766  if (rbPerFlow < 3)
1767  {
1768  // terminate allocation
1769  rbPerFlow = 0;
1770  }
1771  }
1772 
1773  UlDciListElement_s uldci;
1774  uldci.m_rnti = (*it).first;
1775  uldci.m_rbLen = rbPerFlow;
1776  bool allocated = false;
1777  NS_LOG_INFO (this << " RB Allocated " << rbAllocated << " rbPerFlow " << rbPerFlow << " flows " << nflows);
1778  while ((!allocated)&&((rbAllocated + rbPerFlow - 1) < m_cschedCellConfig.m_ulBandwidth) && (rbPerFlow != 0))
1779  {
1780  // check availability
1781  bool free = true;
1782  for (uint16_t j = rbAllocated; j < rbAllocated + rbPerFlow; j++)
1783  {
1784  if (rbMap.at (j) == true)
1785  {
1786  free = false;
1787  break;
1788  }
1789  }
1790  if (free)
1791  {
1792  uldci.m_rbStart = rbAllocated;
1793 
1794  for (uint16_t j = rbAllocated; j < rbAllocated + rbPerFlow; j++)
1795  {
1796  rbMap.at (j) = true;
1797  // store info on allocation for managing ul-cqi interpretation
1798  rbgAllocationMap.at (j) = (*it).first;
1799  }
1800  rbAllocated += rbPerFlow;
1801  allocated = true;
1802  break;
1803  }
1804  rbAllocated++;
1805  if (rbAllocated + rbPerFlow - 1 > m_cschedCellConfig.m_ulBandwidth)
1806  {
1807  // limit to physical resources last resource assignment
1808  rbPerFlow = m_cschedCellConfig.m_ulBandwidth - rbAllocated;
1809  // at least 3 rbg per flow to ensure TxOpportunity >= 7 bytes
1810  if (rbPerFlow < 3)
1811  {
1812  // terminate allocation
1813  rbPerFlow = 0;
1814  }
1815  }
1816  }
1817  if (!allocated)
1818  {
1819  // unable to allocate new resource: finish scheduling
1820  m_nextRntiUl = (*it).first;
1821  if (ret.m_dciList.size () > 0)
1822  {
1824  }
1825  m_allocationMaps.insert (std::pair <uint16_t, std::vector <uint16_t> > (params.m_sfnSf, rbgAllocationMap));
1826  return;
1827  }
1828 
1829 
1830 
1831  std::map <uint16_t, std::vector <double> >::iterator itCqi = m_ueCqi.find ((*it).first);
1832  int cqi = 0;
1833  if (itCqi == m_ueCqi.end ())
1834  {
1835  // no cqi info about this UE
1836  uldci.m_mcs = 0; // MCS 0 -> UL-AMC TBD
1837  }
1838  else
1839  {
1840  // take the lowest CQI value (worst RB)
1841  double minSinr = (*itCqi).second.at (uldci.m_rbStart);
1842  if (minSinr == NO_SINR)
1843  {
1844  minSinr = EstimateUlSinr ((*it).first, uldci.m_rbStart);
1845  }
1846  for (uint16_t i = uldci.m_rbStart; i < uldci.m_rbStart + uldci.m_rbLen; i++)
1847  {
1848  double sinr = (*itCqi).second.at (i);
1849  if (sinr == NO_SINR)
1850  {
1851  sinr = EstimateUlSinr ((*it).first, i);
1852  }
1853  if ((*itCqi).second.at (i) < minSinr)
1854  {
1855  minSinr = (*itCqi).second.at (i);
1856  }
1857  }
1858 
1859  // translate SINR -> cqi: WILD ACK: same as DL
1860  double s = log2 ( 1 + (
1861  std::pow (10, minSinr / 10 ) /
1862  ( (-std::log (5.0 * 0.00005 )) / 1.5) ));
1864  if (cqi == 0)
1865  {
1866  it++;
1867  if (it == m_ceBsrRxed.end ())
1868  {
1869  // restart from the first
1870  it = m_ceBsrRxed.begin ();
1871  }
1872  NS_LOG_DEBUG (this << " UE discared for CQI=0, RNTI " << uldci.m_rnti);
1873  // remove UE from allocation map
1874  for (uint16_t i = uldci.m_rbStart; i < uldci.m_rbStart + uldci.m_rbLen; i++)
1875  {
1876  rbgAllocationMap.at (i) = 0;
1877  }
1878  continue; // CQI == 0 means "out of range" (see table 7.2.3-1 of 36.213)
1879  }
1880  uldci.m_mcs = m_amc->GetMcsFromCqi (cqi);
1881  }
1882 
1883  uldci.m_tbSize = (m_amc->GetTbSizeFromMcs (uldci.m_mcs, rbPerFlow) / 8);
1884  UpdateUlRlcBufferInfo (uldci.m_rnti, uldci.m_tbSize);
1885  uldci.m_ndi = 1;
1886  uldci.m_cceIndex = 0;
1887  uldci.m_aggrLevel = 1;
1888  uldci.m_ueTxAntennaSelection = 3; // antenna selection OFF
1889  uldci.m_hopping = false;
1890  uldci.m_n2Dmrs = 0;
1891  uldci.m_tpc = 0; // no power control
1892  uldci.m_cqiRequest = false; // only period CQI at this stage
1893  uldci.m_ulIndex = 0; // TDD parameter
1894  uldci.m_dai = 1; // TDD parameter
1895  uldci.m_freqHopping = 0;
1896  uldci.m_pdcchPowerOffset = 0; // not used
1897  ret.m_dciList.push_back (uldci);
1898  // store DCI for HARQ_PERIOD
1899  uint8_t harqId = 0;
1900  if (m_harqOn == true)
1901  {
1902  std::map <uint16_t, uint8_t>::iterator itProcId;
1903  itProcId = m_ulHarqCurrentProcessId.find (uldci.m_rnti);
1904  if (itProcId == m_ulHarqCurrentProcessId.end ())
1905  {
1906  NS_FATAL_ERROR ("No info find in HARQ buffer for UE " << uldci.m_rnti);
1907  }
1908  harqId = (*itProcId).second;
1909  std::map <uint16_t, UlHarqProcessesDciBuffer_t>::iterator itDci = m_ulHarqProcessesDciBuffer.find (uldci.m_rnti);
1910  if (itDci == m_ulHarqProcessesDciBuffer.end ())
1911  {
1912  NS_FATAL_ERROR ("Unable to find RNTI entry in UL DCI HARQ buffer for RNTI " << uldci.m_rnti);
1913  }
1914  (*itDci).second.at (harqId) = uldci;
1915  }
1916 
1917  NS_LOG_INFO (this << " UE Allocation RNTI " << (*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 << " harqId " << (uint16_t)harqId);
1918 
1919  it++;
1920  if (it == m_ceBsrRxed.end ())
1921  {
1922  // restart from the first
1923  it = m_ceBsrRxed.begin ();
1924  }
1925  if ((rbAllocated == m_cschedCellConfig.m_ulBandwidth) || (rbPerFlow == 0))
1926  {
1927  // Stop allocation: no more PRBs
1928  m_nextRntiUl = (*it).first;
1929  break;
1930  }
1931  }
1932  while (((*it).first != m_nextRntiUl)&&(rbPerFlow!=0));
1933 
1934 
1935  m_allocationMaps.insert (std::pair <uint16_t, std::vector <uint16_t> > (params.m_sfnSf, rbgAllocationMap));
1937 
1938  return;
1939 }
1940 
1941 void
1943 {
1944  NS_LOG_FUNCTION (this);
1945  return;
1946 }
1947 
1948 void
1950 {
1951  NS_LOG_FUNCTION (this);
1952  return;
1953 }
1954 
1955 void
1957 {
1958  NS_LOG_FUNCTION (this);
1959 
1960  std::map <uint16_t,uint32_t>::iterator it;
1961 
1962  for (unsigned int i = 0; i < params.m_macCeList.size (); i++)
1963  {
1964  if ( params.m_macCeList.at (i).m_macCeType == MacCeListElement_s::BSR )
1965  {
1966  // buffer status report
1967  // note that this scheduler does not differentiate the
1968  // allocation according to which LCGs have more/less bytes
1969  // to send.
1970  // Hence the BSR of different LCGs are just summed up to get
1971  // a total queue size that is used for allocation purposes.
1972 
1973  uint32_t buffer = 0;
1974  for (uint8_t lcg = 0; lcg < 4; ++lcg)
1975  {
1976  uint8_t bsrId = params.m_macCeList.at (i).m_macCeValue.m_bufferStatus.at (lcg);
1977  buffer += BufferSizeLevelBsr::BsrId2BufferSize (bsrId);
1978  }
1979 
1980  uint16_t rnti = params.m_macCeList.at (i).m_rnti;
1981  NS_LOG_LOGIC (this << "RNTI=" << rnti << " buffer=" << buffer);
1982  it = m_ceBsrRxed.find (rnti);
1983  if (it == m_ceBsrRxed.end ())
1984  {
1985  // create the new entry
1986  m_ceBsrRxed.insert ( std::pair<uint16_t, uint32_t > (rnti, buffer));
1987  }
1988  else
1989  {
1990  // update the buffer size value
1991  (*it).second = buffer;
1992  }
1993  }
1994  }
1995 
1996  return;
1997 }
1998 
1999 void
2001 {
2002  NS_LOG_FUNCTION (this);
2003 // retrieve the allocation for this subframe
2004  switch (m_ulCqiFilter)
2005  {
2007  {
2008  // filter all the CQIs that are not SRS based
2009  if (params.m_ulCqi.m_type != UlCqi_s::SRS)
2010  {
2011  return;
2012  }
2013  }
2014  break;
2016  {
2017  // filter all the CQIs that are not SRS based
2018  if (params.m_ulCqi.m_type != UlCqi_s::PUSCH)
2019  {
2020  return;
2021  }
2022  }
2024  break;
2025 
2026  default:
2027  NS_FATAL_ERROR ("Unknown UL CQI type");
2028  }
2029 
2030  switch (params.m_ulCqi.m_type)
2031  {
2032  case UlCqi_s::PUSCH:
2033  {
2034  std::map <uint16_t, std::vector <uint16_t> >::iterator itMap;
2035  std::map <uint16_t, std::vector <double> >::iterator itCqi;
2036  NS_LOG_DEBUG (this << " Collect PUSCH CQIs of Frame no. " << (params.m_sfnSf >> 4) << " subframe no. " << (0xF & params.m_sfnSf));
2037  itMap = m_allocationMaps.find (params.m_sfnSf);
2038  if (itMap == m_allocationMaps.end ())
2039  {
2040  return;
2041  }
2042  for (uint32_t i = 0; i < (*itMap).second.size (); i++)
2043  {
2044  // convert from fixed point notation Sxxxxxxxxxxx.xxx to double
2045  double sinr = LteFfConverter::fpS11dot3toDouble (params.m_ulCqi.m_sinr.at (i));
2046  itCqi = m_ueCqi.find ((*itMap).second.at (i));
2047  if (itCqi == m_ueCqi.end ())
2048  {
2049  // create a new entry
2050  std::vector <double> newCqi;
2051  for (uint32_t j = 0; j < m_cschedCellConfig.m_ulBandwidth; j++)
2052  {
2053  if (i == j)
2054  {
2055  newCqi.push_back (sinr);
2056  }
2057  else
2058  {
2059  // initialize with NO_SINR value.
2060  newCqi.push_back (NO_SINR);
2061  }
2062 
2063  }
2064  m_ueCqi.insert (std::pair <uint16_t, std::vector <double> > ((*itMap).second.at (i), newCqi));
2065  // generate correspondent timer
2066  m_ueCqiTimers.insert (std::pair <uint16_t, uint32_t > ((*itMap).second.at (i), m_cqiTimersThreshold));
2067  }
2068  else
2069  {
2070  // update the value
2071  (*itCqi).second.at (i) = sinr;
2072  NS_LOG_DEBUG (this << " RNTI " << (*itMap).second.at (i) << " RB " << i << " SINR " << sinr);
2073  // update correspondent timer
2074  std::map <uint16_t, uint32_t>::iterator itTimers;
2075  itTimers = m_ueCqiTimers.find ((*itMap).second.at (i));
2076  (*itTimers).second = m_cqiTimersThreshold;
2077 
2078  }
2079 
2080  }
2081  // remove obsolete info on allocation
2082  m_allocationMaps.erase (itMap);
2083  }
2084  break;
2085  case UlCqi_s::SRS:
2086  {
2087  // get the RNTI from vendor specific parameters
2088  uint16_t rnti = 0;
2089  NS_ASSERT (params.m_vendorSpecificList.size () > 0);
2090  for (uint16_t i = 0; i < params.m_vendorSpecificList.size (); i++)
2091  {
2092  if (params.m_vendorSpecificList.at (i).m_type == SRS_CQI_RNTI_VSP)
2093  {
2094  Ptr<SrsCqiRntiVsp> vsp = DynamicCast<SrsCqiRntiVsp> (params.m_vendorSpecificList.at (i).m_value);
2095  rnti = vsp->GetRnti ();
2096  }
2097  }
2098  std::map <uint16_t, std::vector <double> >::iterator itCqi;
2099  itCqi = m_ueCqi.find (rnti);
2100  if (itCqi == m_ueCqi.end ())
2101  {
2102  // create a new entry
2103  std::vector <double> newCqi;
2104  for (uint32_t j = 0; j < m_cschedCellConfig.m_ulBandwidth; j++)
2105  {
2106  double sinr = LteFfConverter::fpS11dot3toDouble (params.m_ulCqi.m_sinr.at (j));
2107  newCqi.push_back (sinr);
2108  NS_LOG_INFO (this << " RNTI " << rnti << " new SRS-CQI for RB " << j << " value " << sinr);
2109 
2110  }
2111  m_ueCqi.insert (std::pair <uint16_t, std::vector <double> > (rnti, newCqi));
2112  // generate correspondent timer
2113  m_ueCqiTimers.insert (std::pair <uint16_t, uint32_t > (rnti, m_cqiTimersThreshold));
2114  }
2115  else
2116  {
2117  // update the values
2118  for (uint32_t j = 0; j < m_cschedCellConfig.m_ulBandwidth; j++)
2119  {
2120  double sinr = LteFfConverter::fpS11dot3toDouble (params.m_ulCqi.m_sinr.at (j));
2121  (*itCqi).second.at (j) = sinr;
2122  NS_LOG_INFO (this << " RNTI " << rnti << " update SRS-CQI for RB " << j << " value " << sinr);
2123  }
2124  // update correspondent timer
2125  std::map <uint16_t, uint32_t>::iterator itTimers;
2126  itTimers = m_ueCqiTimers.find (rnti);
2127  (*itTimers).second = m_cqiTimersThreshold;
2128 
2129  }
2130 
2131 
2132  }
2133  break;
2134  case UlCqi_s::PUCCH_1:
2135  case UlCqi_s::PUCCH_2:
2136  case UlCqi_s::PRACH:
2137  {
2138  NS_FATAL_ERROR ("FdTbfqFfMacScheduler supports only PUSCH and SRS UL-CQIs");
2139  }
2140  break;
2141  default:
2142  NS_FATAL_ERROR ("Unknown type of UL-CQI");
2143  }
2144  return;
2145 }
2146 
2147 void
2149 {
2150  // refresh DL CQI P01 Map
2151  std::map <uint16_t,uint32_t>::iterator itP10 = m_p10CqiTimers.begin ();
2152  while (itP10 != m_p10CqiTimers.end ())
2153  {
2154  NS_LOG_INFO (this << " P10-CQI for user " << (*itP10).first << " is " << (uint32_t)(*itP10).second << " thr " << (uint32_t)m_cqiTimersThreshold);
2155  if ((*itP10).second == 0)
2156  {
2157  // delete correspondent entries
2158  std::map <uint16_t,uint8_t>::iterator itMap = m_p10CqiRxed.find ((*itP10).first);
2159  NS_ASSERT_MSG (itMap != m_p10CqiRxed.end (), " Does not find CQI report for user " << (*itP10).first);
2160  NS_LOG_INFO (this << " P10-CQI expired for user " << (*itP10).first);
2161  m_p10CqiRxed.erase (itMap);
2162  std::map <uint16_t,uint32_t>::iterator temp = itP10;
2163  itP10++;
2164  m_p10CqiTimers.erase (temp);
2165  }
2166  else
2167  {
2168  (*itP10).second--;
2169  itP10++;
2170  }
2171  }
2172 
2173  // refresh DL CQI A30 Map
2174  std::map <uint16_t,uint32_t>::iterator itA30 = m_a30CqiTimers.begin ();
2175  while (itA30 != m_a30CqiTimers.end ())
2176  {
2177  NS_LOG_INFO (this << " A30-CQI for user " << (*itA30).first << " is " << (uint32_t)(*itA30).second << " thr " << (uint32_t)m_cqiTimersThreshold);
2178  if ((*itA30).second == 0)
2179  {
2180  // delete correspondent entries
2181  std::map <uint16_t,SbMeasResult_s>::iterator itMap = m_a30CqiRxed.find ((*itA30).first);
2182  NS_ASSERT_MSG (itMap != m_a30CqiRxed.end (), " Does not find CQI report for user " << (*itA30).first);
2183  NS_LOG_INFO (this << " A30-CQI expired for user " << (*itA30).first);
2184  m_a30CqiRxed.erase (itMap);
2185  std::map <uint16_t,uint32_t>::iterator temp = itA30;
2186  itA30++;
2187  m_a30CqiTimers.erase (temp);
2188  }
2189  else
2190  {
2191  (*itA30).second--;
2192  itA30++;
2193  }
2194  }
2195 
2196  return;
2197 }
2198 
2199 
2200 void
2202 {
2203  // refresh UL CQI Map
2204  std::map <uint16_t,uint32_t>::iterator itUl = m_ueCqiTimers.begin ();
2205  while (itUl != m_ueCqiTimers.end ())
2206  {
2207  NS_LOG_INFO (this << " UL-CQI for user " << (*itUl).first << " is " << (uint32_t)(*itUl).second << " thr " << (uint32_t)m_cqiTimersThreshold);
2208  if ((*itUl).second == 0)
2209  {
2210  // delete correspondent entries
2211  std::map <uint16_t, std::vector <double> >::iterator itMap = m_ueCqi.find ((*itUl).first);
2212  NS_ASSERT_MSG (itMap != m_ueCqi.end (), " Does not find CQI report for user " << (*itUl).first);
2213  NS_LOG_INFO (this << " UL-CQI exired for user " << (*itUl).first);
2214  (*itMap).second.clear ();
2215  m_ueCqi.erase (itMap);
2216  std::map <uint16_t,uint32_t>::iterator temp = itUl;
2217  itUl++;
2218  m_ueCqiTimers.erase (temp);
2219  }
2220  else
2221  {
2222  (*itUl).second--;
2223  itUl++;
2224  }
2225  }
2226 
2227  return;
2228 }
2229 
2230 void
2231 FdTbfqFfMacScheduler::UpdateDlRlcBufferInfo (uint16_t rnti, uint8_t lcid, uint16_t size)
2232 {
2233  std::map<LteFlowId_t, FfMacSchedSapProvider::SchedDlRlcBufferReqParameters>::iterator it;
2234  LteFlowId_t flow (rnti, lcid);
2235  it = m_rlcBufferReq.find (flow);
2236  if (it != m_rlcBufferReq.end ())
2237  {
2238  NS_LOG_INFO (this << " UE " << rnti << " LC " << (uint16_t)lcid << " txqueue " << (*it).second.m_rlcTransmissionQueueSize << " retxqueue " << (*it).second.m_rlcRetransmissionQueueSize << " status " << (*it).second.m_rlcStatusPduSize << " decrease " << size);
2239  // Update queues: RLC tx order Status, ReTx, Tx
2240  // Update status queue
2241  if (((*it).second.m_rlcStatusPduSize > 0) && (size >= (*it).second.m_rlcStatusPduSize))
2242  {
2243  (*it).second.m_rlcStatusPduSize = 0;
2244  }
2245  else if (((*it).second.m_rlcRetransmissionQueueSize > 0) && (size >= (*it).second.m_rlcRetransmissionQueueSize))
2246  {
2247  (*it).second.m_rlcRetransmissionQueueSize = 0;
2248  }
2249  else if ((*it).second.m_rlcTransmissionQueueSize > 0)
2250  {
2251  uint32_t rlcOverhead;
2252  if (lcid == 1)
2253  {
2254  // for SRB1 (using RLC AM) it's better to
2255  // overestimate RLC overhead rather than
2256  // underestimate it and risk unneeded
2257  // segmentation which increases delay
2258  rlcOverhead = 4;
2259  }
2260  else
2261  {
2262  // minimum RLC overhead due to header
2263  rlcOverhead = 2;
2264  }
2265  // update transmission queue
2266  if ((*it).second.m_rlcTransmissionQueueSize <= size - rlcOverhead)
2267  {
2268  (*it).second.m_rlcTransmissionQueueSize = 0;
2269  }
2270  else
2271  {
2272  (*it).second.m_rlcTransmissionQueueSize -= size - rlcOverhead;
2273  }
2274  }
2275  }
2276  else
2277  {
2278  NS_LOG_ERROR (this << " Does not find DL RLC Buffer Report of UE " << rnti);
2279  }
2280 }
2281 
2282 void
2283 FdTbfqFfMacScheduler::UpdateUlRlcBufferInfo (uint16_t rnti, uint16_t size)
2284 {
2285 
2286  size = size - 2; // remove the minimum RLC overhead
2287  std::map <uint16_t,uint32_t>::iterator it = m_ceBsrRxed.find (rnti);
2288  if (it != m_ceBsrRxed.end ())
2289  {
2290  NS_LOG_INFO (this << " UE " << rnti << " size " << size << " BSR " << (*it).second);
2291  if ((*it).second >= size)
2292  {
2293  (*it).second -= size;
2294  }
2295  else
2296  {
2297  (*it).second = 0;
2298  }
2299  }
2300  else
2301  {
2302  NS_LOG_ERROR (this << " Does not find BSR report info of UE " << rnti);
2303  }
2304 
2305 }
2306 
2307 void
2309 {
2310  NS_LOG_FUNCTION (this << " RNTI " << rnti << " txMode " << (uint16_t)txMode);
2312  params.m_rnti = rnti;
2313  params.m_transmissionMode = txMode;
2315 }
2316 
2317 
2318 }