A Discrete-Event Network Simulator
API
error-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 University of Washington
4  * Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  *
20  * This file incorporates work covered by the following copyright and
21  * permission notice:
22  *
23  * Copyright (c) 1997 Regents of the University of California.
24  * All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  * notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  * notice, this list of conditions and the following disclaimer in the
33  * documentation and/or other materials provided with the distribution.
34  * 3. Neither the name of the University nor of the Laboratory may be used
35  * to endorse or promote products derived from this software without
36  * specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  *
50  * Contributed by the Daedalus Research Group, UC Berkeley
51  * (http://daedalus.cs.berkeley.edu)
52  *
53  * This code has been ported from ns-2 (queue/errmodel.{cc,h}
54  */
55 
56 /* BurstErrorModel additions
57  *
58  * Author: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
59  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
60  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
61  */
62 
63 #include <cmath>
64 
65 #include "error-model.h"
66 
67 #include "ns3/packet.h"
68 #include "ns3/assert.h"
69 #include "ns3/log.h"
70 #include "ns3/boolean.h"
71 #include "ns3/enum.h"
72 #include "ns3/double.h"
73 #include "ns3/string.h"
74 #include "ns3/pointer.h"
75 
76 namespace ns3 {
77 
78 NS_LOG_COMPONENT_DEFINE ("ErrorModel");
79 
80 NS_OBJECT_ENSURE_REGISTERED (ErrorModel);
81 
83 {
84  static TypeId tid = TypeId ("ns3::ErrorModel")
85  .SetParent<Object> ()
86  .SetGroupName("Network")
87  .AddAttribute ("IsEnabled", "Whether this ErrorModel is enabled or not.",
88  BooleanValue (true),
91  ;
92  return tid;
93 }
94 
96  m_enable (true)
97 {
98  NS_LOG_FUNCTION (this);
99 }
100 
102 {
103  NS_LOG_FUNCTION (this);
104 }
105 
106 bool
108 {
109  NS_LOG_FUNCTION (this << p);
110  bool result;
111  // Insert any pre-conditions here
112  result = DoCorrupt (p);
113  // Insert any post-conditions here
114  return result;
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION (this);
121  DoReset ();
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this);
128  m_enable = true;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this);
135  m_enable = false;
136 }
137 
138 bool
140 {
141  NS_LOG_FUNCTION (this);
142  return m_enable;
143 }
144 
145 //
146 // RateErrorModel
147 //
148 
150 
152 {
153  static TypeId tid = TypeId ("ns3::RateErrorModel")
154  .SetParent<ErrorModel> ()
155  .SetGroupName("Network")
156  .AddConstructor<RateErrorModel> ()
157  .AddAttribute ("ErrorUnit", "The error unit",
160  MakeEnumChecker (ERROR_UNIT_BIT, "ERROR_UNIT_BIT",
161  ERROR_UNIT_BYTE, "ERROR_UNIT_BYTE",
162  ERROR_UNIT_PACKET, "ERROR_UNIT_PACKET"))
163  .AddAttribute ("ErrorRate", "The error rate.",
164  DoubleValue (0.0),
166  MakeDoubleChecker<double> ())
167  .AddAttribute ("RanVar", "The decision variable attached to this error model.",
168  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
170  MakePointerChecker<RandomVariableStream> ())
171  ;
172  return tid;
173 }
174 
175 
177 {
178  NS_LOG_FUNCTION (this);
179 }
180 
182 {
183  NS_LOG_FUNCTION (this);
184 }
185 
188 {
189  NS_LOG_FUNCTION (this);
190  return m_unit;
191 }
192 
193 void
195 {
196  NS_LOG_FUNCTION (this << error_unit);
197  m_unit = error_unit;
198 }
199 
200 double
202 {
203  NS_LOG_FUNCTION (this);
204  return m_rate;
205 }
206 
207 void
209 {
210  NS_LOG_FUNCTION (this << rate);
211  m_rate = rate;
212 }
213 
214 void
216 {
217  NS_LOG_FUNCTION (this << ranvar);
218  m_ranvar = ranvar;
219 }
220 
221 int64_t
223 {
224  NS_LOG_FUNCTION (this << stream);
225  m_ranvar->SetStream (stream);
226  return 1;
227 }
228 
229 bool
231 {
232  NS_LOG_FUNCTION (this << p);
233  if (!IsEnabled ())
234  {
235  return false;
236  }
237  switch (m_unit)
238  {
239  case ERROR_UNIT_PACKET:
240  return DoCorruptPkt (p);
241  case ERROR_UNIT_BYTE:
242  return DoCorruptByte (p);
243  case ERROR_UNIT_BIT:
244  return DoCorruptBit (p);
245  default:
246  NS_ASSERT_MSG (false, "m_unit not supported yet");
247  break;
248  }
249  return false;
250 }
251 
252 bool
254 {
255  NS_LOG_FUNCTION (this << p);
256  return (m_ranvar->GetValue () < m_rate);
257 }
258 
259 bool
261 {
262  NS_LOG_FUNCTION (this << p);
263  // compute pkt error rate, assume uniformly distributed byte error
264  double per = 1 - std::pow (1.0 - m_rate, static_cast<double> (p->GetSize ()));
265  return (m_ranvar->GetValue () < per);
266 }
267 
268 bool
270 {
271  NS_LOG_FUNCTION (this << p);
272  // compute pkt error rate, assume uniformly distributed bit error
273  double per = 1 - std::pow (1.0 - m_rate, static_cast<double> (8 * p->GetSize ()) );
274  return (m_ranvar->GetValue () < per);
275 }
276 
277 void
279 {
280  NS_LOG_FUNCTION (this);
281  /* re-initialize any state; no-op for now */
282 }
283 
284 
285 //
286 // BurstErrorModel
287 //
288 
290 
292 {
293  static TypeId tid = TypeId ("ns3::BurstErrorModel")
294  .SetParent<ErrorModel> ()
295  .SetGroupName("Network")
296  .AddConstructor<BurstErrorModel> ()
297  .AddAttribute ("ErrorRate", "The burst error event.",
298  DoubleValue (0.0),
300  MakeDoubleChecker<double> ())
301  .AddAttribute ("BurstStart", "The decision variable attached to this error model.",
302  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
304  MakePointerChecker<RandomVariableStream> ())
305  .AddAttribute ("BurstSize", "The number of packets being corrupted at one drop.",
306  StringValue ("ns3::UniformRandomVariable[Min=1|Max=4]"),
308  MakePointerChecker<RandomVariableStream> ())
309  ;
310  return tid;
311 }
312 
313 
314 BurstErrorModel::BurstErrorModel () : m_counter (0), m_currentBurstSz (0)
315 {
316 
317 }
318 
320 {
321  NS_LOG_FUNCTION (this);
322 }
323 
324 double
326 {
327  NS_LOG_FUNCTION (this);
328  return m_burstRate;
329 }
330 
331 void
333 {
334  NS_LOG_FUNCTION (this << rate);
335  m_burstRate = rate;
336 }
337 
338 void
340 {
341  NS_LOG_FUNCTION (this << ranVar);
342  m_burstStart = ranVar;
343 }
344 
345 void
347 {
348  NS_LOG_FUNCTION (this << burstSz);
349  m_burstSize = burstSz;
350 }
351 
352 int64_t
354 {
355  NS_LOG_FUNCTION (this << stream);
356  m_burstStart->SetStream (stream);
357  m_burstSize->SetStream(stream);
358  return 2;
359 }
360 
361 bool
363 {
364  NS_LOG_FUNCTION (this);
365  if (!IsEnabled ())
366  {
367  return false;
368  }
369  double ranVar = m_burstStart ->GetValue();
370 
371  if (ranVar < m_burstRate)
372  {
373  // get a new burst size for the new error event
375  NS_LOG_DEBUG ("new burst size selected: " << m_currentBurstSz);
376  if (m_currentBurstSz == 0)
377  {
378  NS_LOG_WARN ("Burst size == 0; shouldn't happen");
379  return false;
380  }
381  m_counter = 1; // start counting dropped packets
382  return true; // drop this packet
383  }
384  else
385  {
386  // not a burst error event
388  {
389  // check to see if all the packets (determined by the last
390  // generated m_currentBurstSz) have been dropped.
391  // If not, drop 1 more packet
392  m_counter++;
393  return true;
394  }
395  else
396  {
397  // all packets in the last error event have been dropped
398  // and there is no new error event, so do not drop the packet
399  return false; // no error event
400  }
401  }
402 }
403 
404 void
406 {
407  NS_LOG_FUNCTION (this);
408  m_counter = 0;
409  m_currentBurstSz = 0;
410 
411 }
412 
413 
414 //
415 // ListErrorModel
416 //
417 
419 
421 {
422  static TypeId tid = TypeId ("ns3::ListErrorModel")
423  .SetParent<ErrorModel> ()
424  .SetGroupName("Network")
425  .AddConstructor<ListErrorModel> ()
426  ;
427  return tid;
428 }
429 
431 {
432  NS_LOG_FUNCTION (this);
433 }
434 
436 {
437  NS_LOG_FUNCTION (this);
438 }
439 
440 std::list<uint32_t>
442 {
443  NS_LOG_FUNCTION (this);
444  return m_packetList;
445 }
446 
447 void
448 ListErrorModel::SetList (const std::list<uint32_t> &packetlist)
449 {
450  NS_LOG_FUNCTION (this << &packetlist);
451  m_packetList = packetlist;
452 }
453 
454 // When performance becomes a concern, the list provided could be
455 // converted to a dynamically-sized array of uint32_t to avoid
456 // list iteration below.
457 bool
459 {
460  NS_LOG_FUNCTION (this << p);
461  if (!IsEnabled ())
462  {
463  return false;
464  }
465  uint32_t uid = p->GetUid ();
466  for (PacketListCI i = m_packetList.begin ();
467  i != m_packetList.end (); i++)
468  {
469  if (uid == *i)
470  {
471  return true;
472  }
473  }
474  return false;
475 }
476 
477 void
479 {
480  NS_LOG_FUNCTION (this);
481  m_packetList.clear ();
482 }
483 
484 //
485 // ReceiveListErrorModel
486 //
487 
489 
491 {
492  static TypeId tid = TypeId ("ns3::ReceiveListErrorModel")
493  .SetParent<ErrorModel> ()
494  .SetGroupName("Network")
495  .AddConstructor<ReceiveListErrorModel> ()
496  ;
497  return tid;
498 }
499 
500 
502  m_timesInvoked (0)
503 {
504  NS_LOG_FUNCTION (this);
505 }
506 
508 {
509  NS_LOG_FUNCTION (this);
510 }
511 
512 std::list<uint32_t>
514 {
515  NS_LOG_FUNCTION (this);
516  return m_packetList;
517 }
518 
519 void
520 ReceiveListErrorModel::SetList (const std::list<uint32_t> &packetlist)
521 {
522  NS_LOG_FUNCTION (this << &packetlist);
523  m_packetList = packetlist;
524 }
525 
526 bool
528 {
529  NS_LOG_FUNCTION (this << p);
530  if (!IsEnabled ())
531  {
532  return false;
533  }
534  m_timesInvoked += 1;
535  for (PacketListCI i = m_packetList.begin ();
536  i != m_packetList.end (); i++)
537  {
538  if (m_timesInvoked - 1 == *i)
539  {
540  return true;
541  }
542  }
543  return false;
544 }
545 
546 void
548 {
549  NS_LOG_FUNCTION (this);
550  m_packetList.clear ();
551 }
552 
553 
555 
557 {
558  static TypeId tid = TypeId ("ns3::BinaryErrorModel")
559  .SetParent<ErrorModel> ()
560  .AddConstructor<BinaryErrorModel> ()
561  ;
562  return tid;
563 }
564 
566 {
567  NS_LOG_FUNCTION (this);
568  m_counter = 0;
569 }
570 
572 {
573  NS_LOG_FUNCTION (this);
574 }
575 
576 bool
578 {
579  NS_LOG_FUNCTION (this);
580  if (!IsEnabled ())
581  {
582  return false;
583  }
584  bool ret = m_counter%2;
585  m_counter++;
586  return ret;
587 }
588 
589 void
591 {
592  NS_LOG_FUNCTION (this);
593  m_counter = 0;
594 }
595 
596 
597 
598 
599 } // namespace ns3
600 
bool m_enable
True if the error model is enabled.
Definition: error-model.h:164
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
Definition: error-model.cc:577
virtual void DoReset(void)
Re-initialize any state.
Definition: error-model.cc:478
void SetRandomBurstSize(Ptr< RandomVariableStream > burstSz)
Definition: error-model.cc:346
virtual bool DoCorrupt(Ptr< Packet > p)=0
Corrupt a packet according to the specified model.
uint64_t GetUid(void) const
Returns the packet&#39;s Uid.
Definition: packet.cc:390
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
PacketList m_packetList
container of sequence number of packets to corrupt
Definition: error-model.h:457
AttributeValue implementation for Boolean.
Definition: boolean.h:36
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
double m_rate
Error rate.
Definition: error-model.h:260
Hold variables of type string.
Definition: string.h:41
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: enum.h:209
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:84
static TypeId GetTypeId(void)
Get the type ID.
Definition: error-model.cc:490
double GetRate(void) const
Definition: error-model.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
PacketList m_packetList
container of Uid of packets to corrupt
Definition: error-model.h:410
static TypeId GetTypeId(void)
Get the type ID.
Definition: error-model.cc:556
uint8_t m_counter
internal state counter.
Definition: error-model.h:481
General error model that can be used to corrupt packets.
Definition: error-model.h:115
virtual void DoReset(void)
Re-initialize any state.
Definition: error-model.cc:405
virtual ~BinaryErrorModel()
Definition: error-model.cc:571
virtual void DoReset(void)=0
Re-initialize any state.
virtual double GetValue(void)=0
Get the next random value as a double drawn from the distribution.
static TypeId GetTypeId(void)
Get the type ID.
Definition: error-model.cc:151
virtual void DoReset(void)
Re-initialize any state.
Definition: error-model.cc:590
virtual ~RateErrorModel()
Definition: error-model.cc:181
virtual uint32_t GetInteger(void)=0
Get the next random value as an integer drawn from the distribution.
std::list< uint32_t >::const_iterator PacketListCI
Typedef: packet sequence number list const iterator.
Definition: error-model.h:455
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
Hold variables of type enum.
Definition: enum.h:54
virtual bool DoCorruptBit(Ptr< Packet > p)
Corrupt a packet (bit unit).
Definition: error-model.cc:269
Ptr< RandomVariableStream > m_burstStart
the error decision variable
Definition: error-model.h:344
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: error-model.cc:353
void SetRate(double rate)
Definition: error-model.cc:208
Ptr< RandomVariableStream > m_burstSize
the number of packets being flagged as errored
Definition: error-model.h:345
Provide a list of Packet uids to corrupt.
Definition: error-model.h:379
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
Definition: error-model.cc:527
enum ErrorUnit m_unit
Error rate unit.
Definition: error-model.h:259
virtual void DoReset(void)
Re-initialize any state.
Definition: error-model.cc:278
virtual bool DoCorruptByte(Ptr< Packet > p)
Corrupt a packet (Byte unit).
Definition: error-model.cc:260
Ptr< RandomVariableStream > m_ranvar
rng stream
Definition: error-model.h:262
uint32_t m_counter
keep track of the number of packets being errored until it reaches m_burstSize
Definition: error-model.h:351
void Disable(void)
Disable the error model.
Definition: error-model.cc:132
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
Definition: error-model.cc:362
virtual ~BurstErrorModel()
Definition: error-model.cc:319
uint32_t m_timesInvoked
number of times the error model has been invoked
Definition: error-model.h:458
virtual ~ListErrorModel()
Definition: error-model.cc:435
double GetBurstRate(void) const
Definition: error-model.cc:325
static TypeId GetTypeId(void)
Get the type ID.
Definition: error-model.cc:291
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
Definition: error-model.cc:230
void SetRandomVariable(Ptr< RandomVariableStream > ranVar)
Definition: error-model.cc:339
void SetList(const std::list< uint32_t > &packetlist)
Definition: error-model.cc:520
void Reset(void)
Reset any state associated with the error model.
Definition: error-model.cc:118
std::list< uint32_t > GetList(void) const
Definition: error-model.cc:441
virtual void DoReset(void)
Re-initialize any state.
Definition: error-model.cc:547
The simplest error model, corrupts even packets and does not corrupt odd ones.
Definition: error-model.h:465
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: double.h:42
void SetUnit(enum ErrorUnit error_unit)
Definition: error-model.cc:194
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
static TypeId GetTypeId(void)
Get the type ID.
Definition: error-model.cc:420
virtual bool DoCorrupt(Ptr< Packet > p)
Corrupt a packet according to the specified model.
Definition: error-model.cc:458
virtual bool DoCorruptPkt(Ptr< Packet > p)
Corrupt a packet (packet unit).
Definition: error-model.cc:253
Determine which bursts of packets are errored corresponding to an underlying distribution, burst rate, and burst size.
Definition: error-model.h:298
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:262
uint32_t m_currentBurstSz
the current burst size
Definition: error-model.h:352
Provide a list of Packets to corrupt.
Definition: error-model.h:426
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
bool IsCorrupt(Ptr< Packet > pkt)
Note: Depending on the error model, this function may or may not alter the contents of the packet upo...
Definition: error-model.cc:107
void SetList(const std::list< uint32_t > &packetlist)
Definition: error-model.cc:448
void SetRandomVariable(Ptr< RandomVariableStream >)
Definition: error-model.cc:215
std::list< uint32_t >::const_iterator PacketListCI
Typedef: packet Uid list const iterator.
Definition: error-model.h:408
double m_burstRate
the burst error event
Definition: error-model.h:343
static TypeId GetTypeId(void)
Get the type ID.
Definition: error-model.cc:82
RateErrorModel::ErrorUnit GetUnit(void) const
Definition: error-model.cc:187
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Definition: error-model.cc:222
bool IsEnabled(void) const
Definition: error-model.cc:139
A base class which provides memory management and object aggregation.
Definition: object.h:87
Determine which packets are errored corresponding to an underlying distribution, rate, and unit.
Definition: error-model.h:182
virtual ~ErrorModel()
Definition: error-model.cc:101
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:58
void SetBurstRate(double rate)
Definition: error-model.cc:332
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void Enable(void)
Enable the error model.
Definition: error-model.cc:125
ErrorUnit
Error unit.
Definition: error-model.h:197
std::list< uint32_t > GetList(void) const
Definition: error-model.cc:513