A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bs-scheduler-simple.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008 INRIA
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: Jahanzeb Farooq <jahanzeb.farooq@sophia.inria.fr>
19  */
20 
21 #include "bs-scheduler-simple.h"
22 #include "ns3/simulator.h"
23 #include "bs-net-device.h"
24 #include "ns3/packet-burst.h"
25 #include "cid.h"
26 #include "wimax-mac-header.h"
27 #include "ss-record.h"
28 #include "wimax-mac-queue.h"
29 #include "ns3/log.h"
30 #include "burst-profile-manager.h"
31 #include "wimax-connection.h"
32 #include "connection-manager.h"
33 #include "ss-manager.h"
34 #include "service-flow.h"
35 #include "service-flow-record.h"
36 #include "service-flow-manager.h"
37 
38 NS_LOG_COMPONENT_DEFINE ("BSSchedulerSimple");
39 
40 namespace ns3 {
41 
42 NS_OBJECT_ENSURE_REGISTERED (BSSchedulerSimple);
43 
44 TypeId BSSchedulerSimple::GetTypeId (void)
45 {
46  static TypeId tid = TypeId ("ns3::BSSchedulerSimple").SetParent<Object> ().AddConstructor<BSSchedulerSimple> ();
47  return tid;
48 }
49 
50 BSSchedulerSimple::BSSchedulerSimple ()
51  : m_downlinkBursts (new std::list<std::pair<OfdmDlMapIe*, Ptr<PacketBurst> > > ())
52 {
53  SetBs (0);
54 }
55 
56 BSSchedulerSimple::BSSchedulerSimple (Ptr<BaseStationNetDevice> bs)
57  : m_downlinkBursts (new std::list<std::pair<OfdmDlMapIe*, Ptr<PacketBurst> > > ())
58 {
59  // m_downlinkBursts is filled by AddDownlinkBurst and emptied by
60  // wimax-bs-net-device::sendBurst and wimax-ss-net-device::sendBurst
61  SetBs (bs);
62 }
63 
64 BSSchedulerSimple::~BSSchedulerSimple (void)
65 {
66  std::list<std::pair<OfdmDlMapIe*, Ptr<PacketBurst> > > *downlinkBursts = m_downlinkBursts;
67  std::pair<OfdmDlMapIe*, Ptr<PacketBurst> > pair;
68  while (downlinkBursts->size ())
69  {
70  pair = downlinkBursts->front ();
71  pair.second = 0;
72  delete pair.first;
73  }
74  SetBs (0);
75  delete m_downlinkBursts;
76  m_downlinkBursts = 0;
77 }
78 
79 std::list<std::pair<OfdmDlMapIe*, Ptr<PacketBurst> > >*
80 BSSchedulerSimple::GetDownlinkBursts (void) const
81 {
82  return m_downlinkBursts;
83 }
84 
85 void BSSchedulerSimple::AddDownlinkBurst (Ptr<const WimaxConnection> connection,
86  uint8_t diuc,
87  WimaxPhy::ModulationType modulationType,
88  Ptr<PacketBurst> burst)
89 {
90  OfdmDlMapIe *dlMapIe = new OfdmDlMapIe ();
91  dlMapIe->SetCid (connection->GetCid ());
92  dlMapIe->SetDiuc (diuc);
93 
94  NS_LOG_INFO ("BS scheduler, burst size: " << burst->GetSize () << " bytes" << ", pkts: " << burst->GetNPackets ()
95  << ", connection: " << connection->GetTypeStr () << ", CID: " << connection->GetCid ());
96  if (connection->GetType () == Cid::TRANSPORT)
97  {
98  NS_LOG_INFO (", SFID: " << connection->GetServiceFlow ()->GetSfid () << ", service: "
99  << connection->GetServiceFlow ()->GetSchedulingTypeStr ());
100  }
101  NS_LOG_INFO (", modulation: " << modulationType << ", DIUC: " << (uint32_t) diuc);
102 
103  m_downlinkBursts->push_back (std::make_pair (dlMapIe, burst));
104 }
105 
106 void BSSchedulerSimple::Schedule (void)
107 {
108  Ptr<WimaxConnection> connection;
109  WimaxPhy::ModulationType modulationType = WimaxPhy::MODULATION_TYPE_BPSK_12;
110  uint8_t diuc = OfdmDlBurstProfile::DIUC_BURST_PROFILE_1;
111  uint32_t nrSymbolsRequired = 0;
112  GenericMacHeader hdr;
113  Ptr<Packet> packet;
114  Ptr<PacketBurst> burst;
115  ServiceFlow::SchedulingType schedulingType = ServiceFlow::SF_TYPE_NONE;
116  uint32_t availableSymbols = GetBs ()->GetNrDlSymbols ();
117 
118  while (SelectConnection (connection))
119  {
120  if (connection != GetBs ()->GetInitialRangingConnection () && connection != GetBs ()->GetBroadcastConnection ())
121  {
122  /* determines modulation/DIUC only once per burst as it is always same for a particular CID */
123  if (connection->GetType () == Cid::MULTICAST)
124  {
125  modulationType = connection->GetServiceFlow ()->GetModulation ();
126  }
127  else
128  {
129  modulationType = GetBs ()->GetSSManager ()->GetSSRecord (connection->GetCid ())->GetModulationType ();
130  }
131  diuc = GetBs ()->GetBurstProfileManager ()->GetBurstProfile (modulationType,
132  WimaxNetDevice::DIRECTION_DOWNLINK);
133  }
134  else if (connection == GetBs ()->GetInitialRangingConnection () || connection
135  == GetBs ()->GetBroadcastConnection ())
136  {
137 
138  modulationType = WimaxPhy::MODULATION_TYPE_BPSK_12;
139  diuc = OfdmDlBurstProfile::DIUC_BURST_PROFILE_1;
140  }
141 
142  if (connection->GetType () == Cid::TRANSPORT || connection->GetType () == Cid::MULTICAST)
143  {
144  schedulingType = (ServiceFlow::SchedulingType) connection->GetSchedulingType ();
145  }
146 
147  if (schedulingType == ServiceFlow::SF_TYPE_UGS)
148  {
149  nrSymbolsRequired = connection->GetServiceFlow ()->GetRecord ()->GetGrantSize ();
150  if (nrSymbolsRequired < availableSymbols)
151  {
152  burst = CreateUgsBurst (connection->GetServiceFlow (), modulationType, nrSymbolsRequired);
153  }
154  else
155  {
156  burst = CreateUgsBurst (connection->GetServiceFlow (), modulationType, availableSymbols);
157  }
158  if (burst->GetNPackets () != 0)
159  {
160  uint32_t BurstSizeSymbols = GetBs ()->GetPhy ()->GetNrSymbols (burst->GetSize (), modulationType);
161  AddDownlinkBurst (connection, diuc, modulationType, burst);
162 
163  if (availableSymbols <= BurstSizeSymbols)
164  {
165  availableSymbols -= BurstSizeSymbols; // XXX: Overflows but don't know how to fix
166  break;
167  }
168  }
169  }
170  else
171  {
172  burst = Create<PacketBurst> ();
173  while (connection->HasPackets () == true)
174  {
175  uint32_t FirstPacketSize = connection->GetQueue ()->GetFirstPacketRequiredByte (MacHeaderType::HEADER_TYPE_GENERIC);
176  nrSymbolsRequired = GetBs ()->GetPhy ()->GetNrSymbols (FirstPacketSize, modulationType);
177  if (availableSymbols < nrSymbolsRequired && CheckForFragmentation (connection,
178  availableSymbols,
179  modulationType))
180  {
181  uint32_t availableByte = GetBs ()->GetPhy ()->GetNrBytes (availableSymbols, modulationType);
182  packet = connection->Dequeue (MacHeaderType::HEADER_TYPE_GENERIC, availableByte);
183  availableSymbols = 0;
184  }
185  else if (availableSymbols >= nrSymbolsRequired)
186  {
187  packet = connection->Dequeue ();
188  availableSymbols -= nrSymbolsRequired;
189  }
190  else
191  {
192  break;
193  }
194  burst->AddPacket (packet);
195  }
196  AddDownlinkBurst (connection, diuc, modulationType, burst);
197  }
198  if (availableSymbols == 0)
199  {
200  break;
201  }
202  }
203 
204 
205  if (m_downlinkBursts->size ())
206  {
207  NS_LOG_DEBUG ("BS scheduler, number of bursts: " << m_downlinkBursts->size () << ", symbols left: "
208  << availableSymbols << std::endl << "BS scheduler, queues:" << " IR "
209  << GetBs ()->GetInitialRangingConnection ()->GetQueue ()->GetSize () << " broadcast "
210  << GetBs ()->GetBroadcastConnection ()->GetQueue ()->GetSize () << " basic "
211  << GetBs ()->GetConnectionManager ()->GetNPackets (Cid::BASIC, ServiceFlow::SF_TYPE_NONE) << " primary "
212  << GetBs ()->GetConnectionManager ()->GetNPackets (Cid::PRIMARY, ServiceFlow::SF_TYPE_NONE) << " transport "
213  << GetBs ()->GetConnectionManager ()->GetNPackets (Cid::TRANSPORT, ServiceFlow::SF_TYPE_ALL));
214  }
215 }
216 
217 bool BSSchedulerSimple::SelectConnection (Ptr<WimaxConnection> &connection)
218 {
219  connection = 0;
220  Time currentTime = Simulator::Now ();
221  std::vector<Ptr<WimaxConnection> >::const_iterator iter1;
222  std::vector<ServiceFlow*>::iterator iter2;
223  ServiceFlowRecord *serviceFlowRecord;
224  NS_LOG_INFO ("BS Scheduler: Selecting connection...");
225  if (GetBs ()->GetBroadcastConnection ()->HasPackets ())
226  {
227  NS_LOG_INFO ("Return GetBroadcastConnection");
228  connection = GetBs ()->GetBroadcastConnection ();
229  return true;
230  }
231  else if (GetBs ()->GetInitialRangingConnection ()->HasPackets ())
232  {
233  NS_LOG_INFO ("Return GetInitialRangingConnection");
234  connection = GetBs ()->GetInitialRangingConnection ();
235  return true;
236  }
237  else
238  {
239  std::vector<Ptr<WimaxConnection> > connections;
240  std::vector<ServiceFlow*> serviceFlows;
241 
242  connections = GetBs ()->GetConnectionManager ()->GetConnections (Cid::BASIC);
243  for (iter1 = connections.begin (); iter1 != connections.end (); ++iter1)
244  {
245  if ((*iter1)->HasPackets ())
246  {
247  NS_LOG_INFO ("Return Basic");
248  connection = *iter1;
249  return true;
250  }
251  }
252 
253  connections = GetBs ()->GetConnectionManager ()->GetConnections (Cid::PRIMARY);
254  for (iter1 = connections.begin (); iter1 != connections.end (); ++iter1)
255  {
256  if ((*iter1)->HasPackets ())
257  {
258  NS_LOG_INFO ("Return Primary");
259  connection = *iter1;
260  return true;
261  }
262  }
263 
264  serviceFlows = GetBs ()->GetServiceFlowManager ()->GetServiceFlows (ServiceFlow::SF_TYPE_UGS);
265  for (iter2 = serviceFlows.begin (); iter2 != serviceFlows.end (); ++iter2)
266  {
267  serviceFlowRecord = (*iter2)->GetRecord ();
268  NS_LOG_INFO ("processing UGS: HAS PACKET=" << (*iter2)->HasPackets () << "max Latency = "
269  << MilliSeconds ((*iter2)->GetMaximumLatency ()) << "Delay = " << ((currentTime
270  - serviceFlowRecord->GetDlTimeStamp ()) + GetBs ()->GetPhy ()->GetFrameDuration ()));
271  // if latency would exceed in case grant is allocated in next frame then allocate in current frame
272  if ((*iter2)->HasPackets () && ((currentTime - serviceFlowRecord->GetDlTimeStamp ())
273  + GetBs ()->GetPhy ()->GetFrameDuration ()) > MilliSeconds ((*iter2)->GetMaximumLatency ()))
274  {
275  serviceFlowRecord->SetDlTimeStamp (currentTime);
276  connection = (*iter2)->GetConnection ();
277  NS_LOG_INFO ("Return UGS SF: CID = " << (*iter2)->GetCid () << "SFID = " << (*iter2)->GetSfid ());
278  return true;
279  }
280  }
281 
282  serviceFlows = GetBs ()->GetServiceFlowManager ()->GetServiceFlows (ServiceFlow::SF_TYPE_RTPS);
283  for (iter2 = serviceFlows.begin (); iter2 != serviceFlows.end (); ++iter2)
284  {
285  serviceFlowRecord = (*iter2)->GetRecord ();
286  // if latency would exceed in case poll is allocated in next frame then allocate in current frame
287  if ((*iter2)->HasPackets () && ((currentTime - serviceFlowRecord->GetDlTimeStamp ())
288  + GetBs ()->GetPhy ()->GetFrameDuration ()) > MilliSeconds ((*iter2)->GetMaximumLatency ()))
289  {
290  serviceFlowRecord->SetDlTimeStamp (currentTime);
291  connection = (*iter2)->GetConnection ();
292  NS_LOG_INFO ("Return RTPS SF: CID = " << (*iter2)->GetCid () << "SFID = " << (*iter2)->GetSfid ());
293  return true;
294  }
295  }
296 
297  serviceFlows = GetBs ()->GetServiceFlowManager ()->GetServiceFlows (ServiceFlow::SF_TYPE_NRTPS);
298  for (iter2 = serviceFlows.begin (); iter2 != serviceFlows.end (); ++iter2)
299  {
300  serviceFlowRecord = (*iter2)->GetRecord ();
301  if ((*iter2)->HasPackets ())
302  {
303  NS_LOG_INFO ("Return NRTPS SF: CID = " << (*iter2)->GetCid () << "SFID = " << (*iter2)->GetSfid ());
304  connection = (*iter2)->GetConnection ();
305  return true;
306  }
307  }
308 
309  serviceFlows = GetBs ()->GetServiceFlowManager ()->GetServiceFlows (ServiceFlow::SF_TYPE_BE);
310  for (iter2 = serviceFlows.begin (); iter2 != serviceFlows.end (); ++iter2)
311  {
312  serviceFlowRecord = (*iter2)->GetRecord ();
313  if ((*iter2)->HasPackets ())
314  {
315  NS_LOG_INFO ("Return BE SF: CID = " << (*iter2)->GetCid () << "SFID = " << (*iter2)->GetSfid ());
316  connection = (*iter2)->GetConnection ();
317  return true;
318  }
319  }
320  }
321  NS_LOG_INFO ("NO connection is selected!");
322  return false;
323 }
324 
325 Ptr<PacketBurst> BSSchedulerSimple::CreateUgsBurst (ServiceFlow *serviceFlow,
326  WimaxPhy::ModulationType modulationType,
327  uint32_t availableSymbols)
328 {
329  Time timeStamp;
330  GenericMacHeader hdr;
331  Ptr<Packet> packet;
332  Ptr<PacketBurst> burst = Create<PacketBurst> ();
333  uint32_t nrSymbolsRequired = 0;
334 
335  // serviceFlow->CleanUpQueue ();
336  Ptr<WimaxConnection> connection = serviceFlow->GetConnection ();
337  while (serviceFlow->HasPackets ())
338  {
339  uint32_t FirstPacketSize = connection->GetQueue ()->GetFirstPacketRequiredByte (MacHeaderType::HEADER_TYPE_GENERIC);
340  nrSymbolsRequired = GetBs ()->GetPhy ()->GetNrSymbols (FirstPacketSize,modulationType);
341  if (availableSymbols < nrSymbolsRequired && CheckForFragmentation (connection,
342  availableSymbols,
343  modulationType))
344  {
345  uint32_t availableByte = GetBs ()->GetPhy ()->GetNrBytes (availableSymbols, modulationType);
346  packet = connection->Dequeue (MacHeaderType::HEADER_TYPE_GENERIC, availableByte);
347  availableSymbols = 0;
348  }
349  else
350  {
351  packet = connection->Dequeue ();
352  availableSymbols -= nrSymbolsRequired;
353  }
354  burst->AddPacket (packet);
355  if (availableSymbols <= 0)
356  {
357  break;
358  }
359  }
360  return burst;
361 }
362 
363 }
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
#define NS_LOG_INFO(msg)
Definition: log.h:264
static Time Now(void)
Definition: simulator.cc:179
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601