A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
error-model.h
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#ifndef ERROR_MODEL_H
63#define ERROR_MODEL_H
64
65#include "ns3/object.h"
66#include "ns3/random-variable-stream.h"
67
68#include <list>
69
70namespace ns3
71{
72
73class Packet;
74
75/**
76 * \ingroup network
77 * \defgroup errormodel Error Model
78 */
79/**
80 * \ingroup errormodel
81 * \brief General error model that can be used to corrupt packets
82 *
83 * This object is used to flag packets as being lost/errored or not.
84 * It is part of the Object framework and can be aggregated to
85 * other ns3 objects and handled by the Ptr class.
86 *
87 * The main method is IsCorrupt(Ptr<Packet> p) which returns true if
88 * the packet is to be corrupted according to the underlying model.
89 * Depending on the error model, the packet itself may have its packet
90 * data buffer errored or not, or side information may be returned to
91 * the client in the form of a packet tag. (Note: No such error models
92 * that actually error the bits in a packet presently exist).
93 * The object can have state (resettable by Reset()).
94 * The object can also be enabled and disabled via two public member functions.
95 *
96 * Typical code (simplified) to use an ErrorModel may look something like
97 * this:
98 * \code
99 * Ptr<ErrorModel> rem = CreateObject<RateErrorModel> ();
100 * Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
101 * rem->SetRandomVariable (uv);
102 * rem->SetRate (0.001);
103 * ...
104 * Ptr<Packet> p;
105 * if (rem->IsCorrupt (p))
106 * {
107 * dropTrace(p);
108 * } else {
109 * Forward (p);
110 * }
111 * \endcode
112 *
113 * Four practical error models, a RateErrorModel, a BurstErrorModel,
114 * a ListErrorModel, and a ReceiveListErrorModel, are currently implemented.
115 */
116class ErrorModel : public Object
117{
118 public:
119 /**
120 * \brief Get the type ID.
121 * \return the object TypeId
122 */
123 static TypeId GetTypeId();
124
125 ErrorModel();
126 ~ErrorModel() override;
127
128 /**
129 * Note: Depending on the error model, this function may or may not
130 * alter the contents of the packet upon returning true.
131 *
132 * \returns true if the Packet is to be considered as errored/corrupted
133 * \param pkt Packet to apply error model to
134 */
135 bool IsCorrupt(Ptr<Packet> pkt);
136 /**
137 * Reset any state associated with the error model
138 */
139 void Reset();
140 /**
141 * Enable the error model
142 */
143 void Enable();
144 /**
145 * Disable the error model
146 */
147 void Disable();
148 /**
149 * \return true if error model is enabled; false otherwise
150 */
151 bool IsEnabled() const;
152
153 private:
154 /**
155 * Corrupt a packet according to the specified model.
156 * \param p the packet to corrupt
157 * \returns true if the packet is corrupted
158 */
159 virtual bool DoCorrupt(Ptr<Packet> p) = 0;
160 /**
161 * Re-initialize any state
162 */
163 virtual void DoReset() = 0;
164
165 bool m_enable; //!< True if the error model is enabled
166};
167
168/**
169 * \brief Determine which packets are errored corresponding to an underlying
170 * distribution, rate, and unit.
171 *
172 * This object is used to flag packets as being lost/errored or not.
173 * The two parameters that govern the behavior are the rate (or
174 * equivalently, the mean duration/spacing between errors), and the
175 * unit (which may be per-bit, per-byte, and per-packet).
176 * Users can optionally provide a RandomVariableStream object; the default
177 * is to use a Uniform(0,1) distribution.
178
179 * Reset() on this model will do nothing
180 *
181 * IsCorrupt() will not modify the packet data buffer
182 */
184{
185 public:
186 /**
187 * \brief Get the type ID.
188 * \return the object TypeId
189 */
190 static TypeId GetTypeId();
191
193 ~RateErrorModel() override;
194
195 /**
196 * Error unit. The error model can be packet, Byte or bit based.
197 */
199 {
203 };
204
205 /**
206 * \returns the ErrorUnit being used by the underlying model
207 */
209 /**
210 * \param error_unit the ErrorUnit to be used by the underlying model
211 */
212 void SetUnit(ErrorUnit error_unit);
213
214 /**
215 * \returns the error rate being applied by the model
216 */
217 double GetRate() const;
218 /**
219 * \param rate the error rate to be used by the model
220 */
221 void SetRate(double rate);
222
223 /**
224 * \param ranvar A random variable distribution to generate random variates
225 */
227
228 /**
229 * Assign a fixed random variable stream number to the random variables
230 * used by this model. Return the number of streams (possibly zero) that
231 * have been assigned.
232 *
233 * \param stream first stream index to use
234 * \return the number of stream indices assigned by this model
235 */
236 int64_t AssignStreams(int64_t stream);
237
238 private:
239 bool DoCorrupt(Ptr<Packet> p) override;
240 /**
241 * Corrupt a packet (packet unit).
242 * \param p the packet to corrupt
243 * \returns true if the packet is corrupted
244 */
245 virtual bool DoCorruptPkt(Ptr<Packet> p);
246 /**
247 * Corrupt a packet (Byte unit).
248 * \param p the packet to corrupt
249 * \returns true if the packet is corrupted
250 */
251 virtual bool DoCorruptByte(Ptr<Packet> p);
252 /**
253 * Corrupt a packet (bit unit).
254 * \param p the packet to corrupt
255 * \returns true if the packet is corrupted
256 */
257 virtual bool DoCorruptBit(Ptr<Packet> p);
258 void DoReset() override;
259
260 ErrorUnit m_unit; //!< Error rate unit
261 double m_rate; //!< Error rate
262
264};
265
266/**
267 * \brief Determine which bursts of packets are errored corresponding to
268 * an underlying distribution, burst rate, and burst size.
269 *
270 * This object is used to flag packets as being lost/errored or not.
271 * The two parameters that govern the behavior are the burst rate (or
272 * equivalently, the mean duration/spacing between between error events),
273 * and the burst size (or equivalently, the number of packets being flagged
274 * as errored at each error event).
275 *
276 * Users can optionally provide RandomVariableStream objects;
277 * the default for the decision variable is to use a Uniform(0,1) distribution;
278 * the default for the burst size (number of packets) is to use a
279 * discrete Uniform[1,4] distribution.
280 *
281 * For every packet, the model generates a random number based on the
282 * decision variable, and compares it with the burst error rate to
283 * determine if a burst error event should occur.
284 * If a new error event occurs, the model to will generate a new burst size
285 * to determine how many packets should be dropped in this particular burst
286 * error event in addition to the current packet.
287 *
288 * When a second packet arrives, the model again determines if a new error
289 * event should occur based on a newly generated decision variable and
290 * the burst error rate. If a new error event is determined to occur,
291 * the model will restart with a new burst size. Otherwise, the model will
292 * resume the last error event and drop the packet provided that the
293 * total number of packets that has been dropped does not exceed the
294 * burst size.
295 *
296 * IsCorrupt() will not modify the packet data buffer
297 */
299{
300 public:
301 /**
302 * \brief Get the type ID.
303 * \return the object TypeId
304 */
305 static TypeId GetTypeId();
306
308 ~BurstErrorModel() override;
309
310 /**
311 * \returns the error rate being applied by the model
312 */
313 double GetBurstRate() const;
314 /**
315 * \param rate the error rate to be used by the model
316 */
317 void SetBurstRate(double rate);
318
319 /**
320 * \param ranVar A random variable distribution to generate random variates
321 */
323
324 /**
325 * \param burstSz A random variable distribution to generate random burst size
326 */
328
329 /**
330 * Assign a fixed random variable stream number to the random variables
331 * used by this model. Return the number of streams (possibly zero) that
332 * have been assigned.
333 *
334 * \param stream first stream index to use
335 * \return the number of stream indices assigned by this model
336 */
337 int64_t AssignStreams(int64_t stream);
338
339 private:
340 bool DoCorrupt(Ptr<Packet> p) override;
341 void DoReset() override;
342
343 double m_burstRate; //!< the burst error event
344 Ptr<RandomVariableStream> m_burstStart; //!< the error decision variable
345 Ptr<RandomVariableStream> m_burstSize; //!< the number of packets being flagged as errored
346
347 /**
348 * keep track of the number of packets being errored
349 * until it reaches m_burstSize
350 */
352 uint32_t m_currentBurstSz; //!< the current burst size
353};
354
355/**
356 * \brief Provide a list of Packet uids to corrupt
357 *
358 * This object is used to flag packets as being lost/errored or not.
359 * A note on performance: the list is assumed to be unordered, and
360 * in general, Packet uids received may be unordered. Therefore,
361 * each call to IsCorrupt() will result in a walk of the list with
362 * the present underlying implementation.
363 *
364 * Note also that if one wants to target multiple packets from looking
365 * at an (unerrored) trace file, the act of erroring a given packet may
366 * cause subsequent packet uids to change. For instance, suppose one wants
367 * to error packets 11 and 17 on a given device. It may be that erroring
368 * packet 11 will cause the subsequent uid stream to change and 17 may no
369 * longer correspond to the second packet that one wants to lose. Therefore,
370 * be advised that it might take some trial and error to select the
371 * right uids when multiple are provided.
372 *
373 * Reset() on this model will clear the list
374 *
375 * IsCorrupt() will not modify the packet data buffer
376 */
378{
379 public:
380 /**
381 * \brief Get the type ID.
382 * \return the object TypeId
383 */
384 static TypeId GetTypeId();
386 ~ListErrorModel() override;
387
388 /**
389 * \return a copy of the underlying list
390 */
391 std::list<uint64_t> GetList() const;
392 /**
393 * \param packetlist The list of packet uids to error.
394 *
395 * This method overwrites any previously provided list.
396 */
397 void SetList(const std::list<uint64_t>& packetlist);
398
399 private:
400 bool DoCorrupt(Ptr<Packet> p) override;
401 void DoReset() override;
402
403 /// Typedef: packet Uid list
404 typedef std::list<uint64_t> PacketList;
405 /// Typedef: packet Uid list const iterator
406 typedef std::list<uint64_t>::const_iterator PacketListCI;
407
408 PacketList m_packetList; //!< container of Uid of packets to corrupt
409};
410
411/**
412 * \brief Provide a list of Packets to corrupt
413 *
414 * This model also processes a user-generated list of packets to
415 * corrupt, except that the list corresponds to the sequence of
416 * received packets as observed by this error model, and not the
417 * Packet UID.
418 *
419 * Reset() on this model will clear the list
420 *
421 * IsCorrupt() will not modify the packet data buffer
422 */
424{
425 public:
426 /**
427 * \brief Get the type ID.
428 * \return the object TypeId
429 */
430 static TypeId GetTypeId();
432 ~ReceiveListErrorModel() override;
433
434 /**
435 * \return a copy of the underlying list
436 */
437 std::list<uint32_t> GetList() const;
438 /**
439 * \param packetlist The list of packets to error.
440 *
441 * This method overwrites any previously provided list.
442 */
443 void SetList(const std::list<uint32_t>& packetlist);
444
445 private:
446 bool DoCorrupt(Ptr<Packet> p) override;
447 void DoReset() override;
448
449 /// Typedef: packet sequence number list
450 typedef std::list<uint32_t> PacketList;
451 /// Typedef: packet sequence number list const iterator
452 typedef std::list<uint32_t>::const_iterator PacketListCI;
453
454 PacketList m_packetList; //!< container of sequence number of packets to corrupt
455 uint32_t m_timesInvoked; //!< number of times the error model has been invoked
456};
457
458/**
459 * \brief The simplest error model, corrupts even packets and does not corrupt odd ones.
460 */
462{
463 public:
464 /**
465 * \brief Get the type ID.
466 * \return the object TypeId
467 */
468 static TypeId GetTypeId();
469
471 ~BinaryErrorModel() override;
472
473 private:
474 bool DoCorrupt(Ptr<Packet> p) override;
475 void DoReset() override;
476
477 uint8_t m_counter; //!< internal state counter.
478};
479
480} // namespace ns3
481#endif
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
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
std::list< uint64_t > PacketList
Typedef: packet Uid list.
Definition: error-model.h:404
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
std::list< uint64_t >::const_iterator PacketListCI
Typedef: packet Uid list const iterator.
Definition: error-model.h:406
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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
std::list< uint32_t > PacketList
Typedef: packet sequence number list.
Definition: error-model.h:450
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
std::list< uint32_t >::const_iterator PacketListCI
Typedef: packet sequence number list const iterator.
Definition: error-model.h:452
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
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.