satellite-fwd-link-scheduler.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd
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: Sami Rantanen <sami.rantanen@magister.fi>
19  */
20 
22 
23 #include "satellite-enums.h"
24 #include "satellite-mac-tag.h"
26 
27 #include <ns3/boolean.h>
28 #include <ns3/double.h>
29 #include <ns3/enum.h>
30 #include <ns3/log.h>
31 #include <ns3/mac48-address.h>
32 #include <ns3/nstime.h>
33 #include <ns3/pointer.h>
34 #include <ns3/simulator.h>
35 #include <ns3/trace-source-accessor.h>
36 #include <ns3/uinteger.h>
37 
38 #include <algorithm>
39 #include <iostream>
40 #include <stdint.h>
41 #include <string>
42 #include <utility>
43 #include <vector>
44 
45 NS_LOG_COMPONENT_DEFINE("SatFwdLinkScheduler");
46 
47 namespace ns3
48 {
49 
50 NS_OBJECT_ENSURE_REGISTERED(SatFwdLinkScheduler);
51 
52 // #define SAT_FWD_LINK_SCHEDULER_PRINT_SORT_RESULT
53 
54 #ifdef SAT_FWD_LINK_SCHEDULER_PRINT_SORT_RESULT
55 static void
56 PrintSoContent(std::string context, std::vector<Ptr<SatSchedulingObject>>& so)
57 {
58  std::cout << context << std::endl;
59 
60  for (std::vector<Ptr<SatSchedulingObject>>::const_iterator it = so.begin(); it != so.end();
61  it++)
62  {
63  std::cout << "So-Content (ptr, priority, load, hol): " << (*it) << ", "
64  << (*it)->GetPriority() << ", " << (*it)->GetBufferedBytes() << ", "
65  << (*it)->GetHolDelay() << std::endl;
66  }
67 
68  std::cout << std::endl;
69 }
70 #endif
71 
72 bool
73 SatFwdLinkScheduler::CompareSoFlowId(Ptr<SatSchedulingObject> obj1, Ptr<SatSchedulingObject> obj2)
74 {
75  return (bool)(obj1->GetFlowId() < obj2->GetFlowId());
76 }
77 
78 bool
79 SatFwdLinkScheduler::CompareSoPriorityLoad(Ptr<SatSchedulingObject> obj1,
80  Ptr<SatSchedulingObject> obj2)
81 {
82  bool result = CompareSoFlowId(obj1, obj2);
83 
84  if (obj1->GetFlowId() == obj2->GetFlowId())
85  {
86  result = (bool)(obj1->GetBufferedBytes() > obj2->GetBufferedBytes());
87  }
88 
89  return result;
90 }
91 
92 bool
93 SatFwdLinkScheduler::CompareSoPriorityHol(Ptr<SatSchedulingObject> obj1,
94  Ptr<SatSchedulingObject> obj2)
95 {
96  bool result = CompareSoFlowId(obj1, obj2);
97 
98  if (obj1->GetFlowId() == obj2->GetFlowId())
99  {
100  result = (bool)(obj1->GetHolDelay() > obj2->GetHolDelay());
101  }
102 
103  return result;
104 }
105 
106 TypeId
108 {
109  static TypeId tid =
110  TypeId("ns3::SatFwdLinkScheduler")
111  .SetParent<Object>()
112  .AddConstructor<SatFwdLinkScheduler>()
113  .AddAttribute("Interval",
114  "The time for periodic scheduling",
115  TimeValue(MilliSeconds(20)),
116  MakeTimeAccessor(&SatFwdLinkScheduler::m_periodicInterval),
117  MakeTimeChecker())
118  .AddAttribute("BBFrameConf",
119  "BB Frame configuration for this scheduler.",
120  PointerValue(),
121  MakePointerAccessor(&SatFwdLinkScheduler::m_bbFrameConf),
122  MakePointerChecker<SatBbFrameConf>())
123  .AddAttribute("DummyFrameSendingEnabled",
124  "Flag to tell, if dummy frames are sent or not.",
125  BooleanValue(false),
127  MakeBooleanChecker())
128  .AddAttribute("AdditionalSortCriteria",
129  "Sorting criteria after priority for scheduling objects from LLC.",
130  EnumValue(SatFwdLinkScheduler::NO_SORT),
131  MakeEnumAccessor<SatFwdLinkScheduler::ScheduleSortingCriteria_t>(
133  MakeEnumChecker(SatFwdLinkScheduler::NO_SORT,
134  "NoSorting",
136  "DelaySort",
138  "LoadSort"))
139  .AddAttribute("CnoEstimationMode",
140  "Mode of the C/N0 estimator",
141  EnumValue(SatCnoEstimator::LAST),
142  MakeEnumAccessor<SatCnoEstimator::EstimationMode_t>(
144  MakeEnumChecker(SatCnoEstimator::LAST,
145  "LastValueInWindow",
147  "MinValueInWindow",
149  "AverageValueInWindow"))
150  .AddAttribute("CnoEstimationWindow",
151  "Time window for C/N0 estimation.",
152  TimeValue(Seconds(5000)),
154  MakeTimeChecker())
155  .AddTraceSource(
156  "SymbolRate",
157  "Scheduler symbol rate for a given packet",
158  MakeTraceSourceAccessor(&SatFwdLinkScheduler::m_schedulingSymbolRateTrace),
159  "ns3::SatTypedefs::FwdLinkSchedulerSymbolRateCallback")
160 
161  ;
162  return tid;
163 }
164 
165 TypeId
167 {
168  NS_LOG_FUNCTION(this);
169 
170  return GetTypeId();
171 }
172 
174  : m_additionalSortCriteria(SatFwdLinkScheduler::NO_SORT),
175  m_cnoEstimatorMode(SatCnoEstimator::LAST),
176  m_carrierBandwidthInHz(0.0)
177 {
178  NS_LOG_FUNCTION(this);
179  NS_FATAL_ERROR("Default constructor for SatFwdLinkScheduler not supported");
180 }
181 
183  Mac48Address address,
184  double carrierBandwidthInHz)
185  : m_macAddress(address),
186  m_bbFrameConf(conf),
187  m_additionalSortCriteria(SatFwdLinkScheduler::NO_SORT),
188  m_cnoEstimatorMode(SatCnoEstimator::LAST),
189  m_carrierBandwidthInHz(carrierBandwidthInHz)
190 {
191  NS_LOG_FUNCTION(this);
192 
193  ObjectBase::ConstructSelf(AttributeConstructionList());
194 
195  // Random variable used in scheduling
196  m_random = CreateObject<UniformRandomVariable>();
197 }
198 
200 {
201  NS_LOG_FUNCTION(this);
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION(this);
208  m_schedContextCallback.Nullify();
209  m_txOpportunityCallback.Nullify();
210  m_sendControlMsgCallback.Nullify();
211  m_cnoEstimatorContainer.clear();
212 }
213 
214 void
216 {
217  NS_LOG_FUNCTION(this << &cb);
219 }
220 
221 void
223 {
224  NS_LOG_FUNCTION(this << &cb);
226 }
227 
228 void
230 {
231  NS_LOG_FUNCTION(this << &cb);
233 }
234 
235 bool
236 SatFwdLinkScheduler::SendControlMsg(Ptr<SatControlMessage> message, const Address& dest) const
237 {
238  NS_LOG_FUNCTION(this << message << dest);
239  return m_sendControlMsgCallback(message, dest);
240 }
241 
242 std::pair<Ptr<SatBbFrame>, const Time>
244 {
245  NS_FATAL_ERROR("SatFwdLinkScheduler::GetNextFrame: should not be here");
246 
247  Ptr<SatBbFrame> f = nullptr;
248 
249  return std::make_pair(f, m_bbFrameConf->GetDummyBbFrameDuration());
250 }
251 
252 void
253 SatFwdLinkScheduler::CnoInfoUpdated(Mac48Address utAddress, double cnoEstimate)
254 {
255  NS_LOG_FUNCTION(this << utAddress << cnoEstimate);
256 
257  CnoEstimatorMap_t::const_iterator it = m_cnoEstimatorContainer.find(utAddress);
258 
259  if (it == m_cnoEstimatorContainer.end())
260  {
261  Ptr<SatCnoEstimator> estimator = CreateCnoEstimator();
262 
263  std::pair<CnoEstimatorMap_t::const_iterator, bool> result =
264  m_cnoEstimatorContainer.insert(std::make_pair(utAddress, estimator));
265  it = result.first;
266 
267  if (result.second == false)
268  {
269  NS_FATAL_ERROR("Estimator cannot be added to container!!!");
270  }
271  }
272 
273  it->second->AddSample(cnoEstimate);
274 }
275 
276 Time
278 {
279  NS_LOG_FUNCTION(this);
280 
281  return m_bbFrameConf->GetBbFrameDuration(m_bbFrameConf->GetDefaultModCod(),
283 }
284 
285 void
287 {
288  m_dummyFrameSendingEnabled = dummyFrameSendingEnabled;
289 }
290 
291 void
293 {
294  NS_LOG_FUNCTION(this);
295 
296  NS_FATAL_ERROR("Must use subclasses");
297 }
298 
299 void
301 {
302  NS_FATAL_ERROR("SatFwdLinkScheduler::ScheduleBbFrames: should not be here");
303 }
304 
305 void
307 {
308  NS_FATAL_ERROR("SatFwdLinkScheduler::SendAndClearSymbolsSentStat: should not be here");
309 }
310 
311 void
313 {
314  NS_FATAL_ERROR("SatFwdLinkScheduler::ScheduleBbFrames: should not be here");
315 }
316 
317 void
318 SatFwdLinkScheduler::GetSchedulingObjects(std::vector<Ptr<SatSchedulingObject>>& output)
319 {
320  NS_FATAL_ERROR("SatFwdLinkScheduler::GetSchedulingObjects: should not be here");
321 }
322 
323 void
324 SatFwdLinkScheduler::SortSchedulingObjects(std::vector<Ptr<SatSchedulingObject>>& so)
325 {
326  NS_LOG_FUNCTION(this);
327 
328  // sort only if there is need to sort
329  if ((so.empty() == false) && (so.size() > 1))
330  {
331 #ifdef SAT_FWD_LINK_SCHEDULER_PRINT_SORT_RESULT
332  PrintSoContent("Before sort", so);
333 #endif
334 
335  switch (m_additionalSortCriteria)
336  {
338  std::sort(so.begin(), so.end(), CompareSoFlowId);
339  break;
340 
342  std::sort(so.begin(), so.end(), CompareSoPriorityHol);
343  break;
344 
346  std::sort(so.begin(), so.end(), CompareSoPriorityLoad);
347  break;
348 
349  default:
350  NS_FATAL_ERROR("Not supported sorting criteria!!!");
351  break;
352  }
353 
354 #ifdef SAT_FWD_LINK_SCHEDULER_PRINT_SORT_RESULT
355  PrintSoContent("After sort", so);
356 #endif
357  }
358 }
359 
360 bool
361 SatFwdLinkScheduler::CnoMatchWithFrame(double cno, Ptr<SatBbFrame> frame) const
362 {
363  NS_LOG_FUNCTION(this << cno << frame);
364 
365  bool match = false;
366 
367  SatEnums::SatModcod_t modCod = m_bbFrameConf->GetBestModcod(cno, frame->GetFrameType());
368 
369  if (modCod >= frame->GetModcod())
370  {
371  match = true;
372  }
373 
374  return match;
375 }
376 
377 double
379 {
380  NS_LOG_FUNCTION(this << ob);
381 
382  double cno = NAN;
383 
384  CnoEstimatorMap_t::const_iterator it = m_cnoEstimatorContainer.find(ob->GetMacAddress());
385 
386  if (it != m_cnoEstimatorContainer.end())
387  {
388  cno = it->second->GetCnoEstimation();
389  }
390 
391  return cno;
392 }
393 
394 Ptr<SatCnoEstimator>
396 {
397  NS_LOG_FUNCTION(this);
398 
399  Ptr<SatCnoEstimator> estimator = nullptr;
400 
401  switch (m_cnoEstimatorMode)
402  {
406  estimator = Create<SatBasicCnoEstimator>(m_cnoEstimatorMode, m_cnoEstimationWindow);
407  break;
408 
409  default:
410  NS_FATAL_ERROR("Not supported C/N0 estimation mode!!!");
411  break;
412  }
413 
414  return estimator;
415 }
416 
417 } // namespace ns3
SatCnoEstimator class defines interface for C/N0 estimators.
@ MINIMUM
Minimum value in the given window returned.
@ LAST
Last value in the given window returned.
@ AVERAGE
Average value in the given window returned.
SatModcod_t
Modulation scheme and coding rate for DVB-S2.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.