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