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 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 *
8 * This file incorporates work covered by the following copyright and
9 * permission notice:
10 *
11 * Copyright (c) 1997 Regents of the University of California.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor of the Laboratory may be used
23 * to endorse or promote products derived from this software without
24 * specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * Contributed by the Daedalus Research Group, UC Berkeley
39 * (http://daedalus.cs.berkeley.edu)
40 *
41 * This code has been ported from ns-2 (queue/errmodel.{cc,h}
42 */
43
44/* BurstErrorModel additions
45 *
46 * Author: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
47 * ResiliNets Research Group https://resilinets.org/
48 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
49 */
50
51#ifndef ERROR_MODEL_H
52#define ERROR_MODEL_H
53
54#include "ns3/object.h"
55#include "ns3/random-variable-stream.h"
56
57#include <list>
58
59namespace ns3
60{
61
62class Packet;
63
64/**
65 * \ingroup network
66 * \defgroup errormodel Error Model
67 */
68/**
69 * \ingroup errormodel
70 * \brief General error model that can be used to corrupt packets
71 *
72 * This object is used to flag packets as being lost/errored or not.
73 * It is part of the Object framework and can be aggregated to
74 * other ns3 objects and handled by the Ptr class.
75 *
76 * The main method is IsCorrupt(Ptr<Packet> p) which returns true if
77 * the packet is to be corrupted according to the underlying model.
78 * Depending on the error model, the packet itself may have its packet
79 * data buffer errored or not, or side information may be returned to
80 * the client in the form of a packet tag. (Note: No such error models
81 * that actually error the bits in a packet presently exist).
82 * The object can have state (resettable by Reset()).
83 * The object can also be enabled and disabled via two public member functions.
84 *
85 * Typical code (simplified) to use an ErrorModel may look something like
86 * this:
87 * \code
88 * Ptr<ErrorModel> rem = CreateObject<RateErrorModel> ();
89 * Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
90 * rem->SetRandomVariable (uv);
91 * rem->SetRate (0.001);
92 * ...
93 * Ptr<Packet> p;
94 * if (rem->IsCorrupt (p))
95 * {
96 * dropTrace(p);
97 * } else {
98 * Forward (p);
99 * }
100 * \endcode
101 *
102 * Four practical error models, a RateErrorModel, a BurstErrorModel,
103 * a ListErrorModel, and a ReceiveListErrorModel, are currently implemented.
104 */
105class ErrorModel : public Object
106{
107 public:
108 /**
109 * \brief Get the type ID.
110 * \return the object TypeId
111 */
112 static TypeId GetTypeId();
113
114 ErrorModel();
115 ~ErrorModel() override;
116
117 /**
118 * Note: Depending on the error model, this function may or may not
119 * alter the contents of the packet upon returning true.
120 *
121 * \returns true if the Packet is to be considered as errored/corrupted
122 * \param pkt Packet to apply error model to
123 */
124 bool IsCorrupt(Ptr<Packet> pkt);
125 /**
126 * Reset any state associated with the error model
127 */
128 void Reset();
129 /**
130 * Enable the error model
131 */
132 void Enable();
133 /**
134 * Disable the error model
135 */
136 void Disable();
137 /**
138 * \return true if error model is enabled; false otherwise
139 */
140 bool IsEnabled() const;
141
142 private:
143 /**
144 * Corrupt a packet according to the specified model.
145 * \param p the packet to corrupt
146 * \returns true if the packet is corrupted
147 */
148 virtual bool DoCorrupt(Ptr<Packet> p) = 0;
149 /**
150 * Re-initialize any state
151 */
152 virtual void DoReset() = 0;
153
154 bool m_enable; //!< True if the error model is enabled
155};
156
157/**
158 * \brief Determine which packets are errored corresponding to an underlying
159 * distribution, rate, and unit.
160 *
161 * This object is used to flag packets as being lost/errored or not.
162 * The two parameters that govern the behavior are the rate (or
163 * equivalently, the mean duration/spacing between errors), and the
164 * unit (which may be per-bit, per-byte, and per-packet).
165 * Users can optionally provide a RandomVariableStream object; the default
166 * is to use a Uniform(0,1) distribution.
167
168 * Reset() on this model will do nothing
169 *
170 * IsCorrupt() will not modify the packet data buffer
171 */
173{
174 public:
175 /**
176 * \brief Get the type ID.
177 * \return the object TypeId
178 */
179 static TypeId GetTypeId();
180
182 ~RateErrorModel() override;
183
184 /**
185 * Error unit. The error model can be packet, Byte or bit based.
186 */
193
194 /**
195 * \returns the ErrorUnit being used by the underlying model
196 */
198 /**
199 * \param error_unit the ErrorUnit to be used by the underlying model
200 */
201 void SetUnit(ErrorUnit error_unit);
202
203 /**
204 * \returns the error rate being applied by the model
205 */
206 double GetRate() const;
207 /**
208 * \param rate the error rate to be used by the model
209 */
210 void SetRate(double rate);
211
212 /**
213 * \param ranvar A random variable distribution to generate random variates
214 */
216
217 /**
218 * Assign a fixed random variable stream number to the random variables
219 * used by this model. Return the number of streams (possibly zero) that
220 * have been assigned.
221 *
222 * \param stream first stream index to use
223 * \return the number of stream indices assigned by this model
224 */
225 int64_t AssignStreams(int64_t stream);
226
227 private:
228 bool DoCorrupt(Ptr<Packet> p) override;
229 /**
230 * Corrupt a packet (packet unit).
231 * \param p the packet to corrupt
232 * \returns true if the packet is corrupted
233 */
234 virtual bool DoCorruptPkt(Ptr<Packet> p);
235 /**
236 * Corrupt a packet (Byte unit).
237 * \param p the packet to corrupt
238 * \returns true if the packet is corrupted
239 */
240 virtual bool DoCorruptByte(Ptr<Packet> p);
241 /**
242 * Corrupt a packet (bit unit).
243 * \param p the packet to corrupt
244 * \returns true if the packet is corrupted
245 */
246 virtual bool DoCorruptBit(Ptr<Packet> p);
247 void DoReset() override;
248
249 ErrorUnit m_unit; //!< Error rate unit
250 double m_rate; //!< Error rate
251
253};
254
255/**
256 * \brief Determine which bursts of packets are errored corresponding to
257 * an underlying distribution, burst rate, and burst size.
258 *
259 * This object is used to flag packets as being lost/errored or not.
260 * The two parameters that govern the behavior are the burst rate (or
261 * equivalently, the mean duration/spacing between between error events),
262 * and the burst size (or equivalently, the number of packets being flagged
263 * as errored at each error event).
264 *
265 * Users can optionally provide RandomVariableStream objects;
266 * the default for the decision variable is to use a Uniform(0,1) distribution;
267 * the default for the burst size (number of packets) is to use a
268 * discrete Uniform[1,4] distribution.
269 *
270 * For every packet, the model generates a random number based on the
271 * decision variable, and compares it with the burst error rate to
272 * determine if a burst error event should occur.
273 * If a new error event occurs, the model to will generate a new burst size
274 * to determine how many packets should be dropped in this particular burst
275 * error event in addition to the current packet.
276 *
277 * When a second packet arrives, the model again determines if a new error
278 * event should occur based on a newly generated decision variable and
279 * the burst error rate. If a new error event is determined to occur,
280 * the model will restart with a new burst size. Otherwise, the model will
281 * resume the last error event and drop the packet provided that the
282 * total number of packets that has been dropped does not exceed the
283 * burst size.
284 *
285 * IsCorrupt() will not modify the packet data buffer
286 */
288{
289 public:
290 /**
291 * \brief Get the type ID.
292 * \return the object TypeId
293 */
294 static TypeId GetTypeId();
295
297 ~BurstErrorModel() override;
298
299 /**
300 * \returns the error rate being applied by the model
301 */
302 double GetBurstRate() const;
303 /**
304 * \param rate the error rate to be used by the model
305 */
306 void SetBurstRate(double rate);
307
308 /**
309 * \param ranVar A random variable distribution to generate random variates
310 */
312
313 /**
314 * \param burstSz A random variable distribution to generate random burst size
315 */
317
318 /**
319 * Assign a fixed random variable stream number to the random variables
320 * used by this model. Return the number of streams (possibly zero) that
321 * have been assigned.
322 *
323 * \param stream first stream index to use
324 * \return the number of stream indices assigned by this model
325 */
326 int64_t AssignStreams(int64_t stream);
327
328 private:
329 bool DoCorrupt(Ptr<Packet> p) override;
330 void DoReset() override;
331
332 double m_burstRate; //!< the burst error event
333 Ptr<RandomVariableStream> m_burstStart; //!< the error decision variable
334 Ptr<RandomVariableStream> m_burstSize; //!< the number of packets being flagged as errored
335
336 /**
337 * keep track of the number of packets being errored
338 * until it reaches m_burstSize
339 */
341 uint32_t m_currentBurstSz; //!< the current burst size
342};
343
344/**
345 * \brief Provide a list of Packet uids to corrupt
346 *
347 * This object is used to flag packets as being lost/errored or not.
348 * A note on performance: the list is assumed to be unordered, and
349 * in general, Packet uids received may be unordered. Therefore,
350 * each call to IsCorrupt() will result in a walk of the list with
351 * the present underlying implementation.
352 *
353 * Note also that if one wants to target multiple packets from looking
354 * at an (unerrored) trace file, the act of erroring a given packet may
355 * cause subsequent packet uids to change. For instance, suppose one wants
356 * to error packets 11 and 17 on a given device. It may be that erroring
357 * packet 11 will cause the subsequent uid stream to change and 17 may no
358 * longer correspond to the second packet that one wants to lose. Therefore,
359 * be advised that it might take some trial and error to select the
360 * right uids when multiple are provided.
361 *
362 * Reset() on this model will clear the list
363 *
364 * IsCorrupt() will not modify the packet data buffer
365 */
367{
368 public:
369 /**
370 * \brief Get the type ID.
371 * \return the object TypeId
372 */
373 static TypeId GetTypeId();
375 ~ListErrorModel() override;
376
377 /**
378 * \return a copy of the underlying list
379 */
380 std::list<uint64_t> GetList() const;
381 /**
382 * \param packetlist The list of packet uids to error.
383 *
384 * This method overwrites any previously provided list.
385 */
386 void SetList(const std::list<uint64_t>& packetlist);
387
388 private:
389 bool DoCorrupt(Ptr<Packet> p) override;
390 void DoReset() override;
391
392 /// Typedef: packet Uid list
393 typedef std::list<uint64_t> PacketList;
394 /// Typedef: packet Uid list const iterator
395 typedef std::list<uint64_t>::const_iterator PacketListCI;
396
397 PacketList m_packetList; //!< container of Uid of packets to corrupt
398};
399
400/**
401 * \brief Provide a list of Packets to corrupt
402 *
403 * This model also processes a user-generated list of packets to
404 * corrupt, except that the list corresponds to the sequence of
405 * received packets as observed by this error model, and not the
406 * Packet UID.
407 *
408 * Reset() on this model will clear the list
409 *
410 * IsCorrupt() will not modify the packet data buffer
411 */
413{
414 public:
415 /**
416 * \brief Get the type ID.
417 * \return the object TypeId
418 */
419 static TypeId GetTypeId();
421 ~ReceiveListErrorModel() override;
422
423 /**
424 * \return a copy of the underlying list
425 */
426 std::list<uint32_t> GetList() const;
427 /**
428 * \param packetlist The list of packets to error.
429 *
430 * This method overwrites any previously provided list.
431 */
432 void SetList(const std::list<uint32_t>& packetlist);
433
434 private:
435 bool DoCorrupt(Ptr<Packet> p) override;
436 void DoReset() override;
437
438 /// Typedef: packet sequence number list
439 typedef std::list<uint32_t> PacketList;
440 /// Typedef: packet sequence number list const iterator
441 typedef std::list<uint32_t>::const_iterator PacketListCI;
442
443 PacketList m_packetList; //!< container of sequence number of packets to corrupt
444 uint32_t m_timesInvoked; //!< number of times the error model has been invoked
445};
446
447/**
448 * \brief The simplest error model, corrupts even packets and does not corrupt odd ones.
449 */
451{
452 public:
453 /**
454 * \brief Get the type ID.
455 * \return the object TypeId
456 */
457 static TypeId GetTypeId();
458
460 ~BinaryErrorModel() override;
461
462 private:
463 bool DoCorrupt(Ptr<Packet> p) override;
464 void DoReset() override;
465
466 uint8_t m_counter; //!< internal state counter.
467};
468
469} // namespace ns3
470#endif
The simplest error model, corrupts even packets and does not corrupt odd ones.
uint8_t m_counter
internal state counter.
void DoReset() override
Re-initialize any state.
~BinaryErrorModel() override
static TypeId GetTypeId()
Get the type ID.
bool DoCorrupt(Ptr< Packet > p) override
Corrupt a packet according to the specified model.
Determine which bursts of packets are errored corresponding to an underlying distribution,...
Ptr< RandomVariableStream > m_burstStart
the error decision variable
void SetRandomVariable(Ptr< RandomVariableStream > ranVar)
bool DoCorrupt(Ptr< Packet > p) override
Corrupt a packet according to the specified model.
Ptr< RandomVariableStream > m_burstSize
the number of packets being flagged as errored
uint32_t m_currentBurstSz
the current burst size
double GetBurstRate() const
void DoReset() override
Re-initialize any state.
static TypeId GetTypeId()
Get the type ID.
double m_burstRate
the burst error event
void SetBurstRate(double rate)
uint32_t m_counter
keep track of the number of packets being errored until it reaches m_burstSize
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
~BurstErrorModel() override
void SetRandomBurstSize(Ptr< RandomVariableStream > burstSz)
General error model that can be used to corrupt packets.
bool m_enable
True if the error model is enabled.
void Reset()
Reset any state associated with the error model.
void Enable()
Enable the error model.
bool IsEnabled() const
~ErrorModel() override
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.
void Disable()
Disable the error model.
bool IsCorrupt(Ptr< Packet > pkt)
Note: Depending on the error model, this function may or may not alter the contents of the packet upo...
Provide a list of Packet uids to corrupt.
bool DoCorrupt(Ptr< Packet > p) override
Corrupt a packet according to the specified model.
~ListErrorModel() override
void DoReset() override
Re-initialize any state.
std::list< uint64_t > PacketList
Typedef: packet Uid list.
void SetList(const std::list< uint64_t > &packetlist)
std::list< uint64_t > GetList() const
static TypeId GetTypeId()
Get the type ID.
PacketList m_packetList
container of Uid of packets to corrupt
std::list< uint64_t >::const_iterator PacketListCI
Typedef: packet Uid list const iterator.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Determine which packets are errored corresponding to an underlying distribution, rate,...
double m_rate
Error rate.
virtual bool DoCorruptByte(Ptr< Packet > p)
Corrupt a packet (Byte unit).
ErrorUnit m_unit
Error rate unit.
void SetRate(double rate)
void DoReset() override
Re-initialize any state.
virtual bool DoCorruptBit(Ptr< Packet > p)
Corrupt a packet (bit unit).
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
void SetUnit(ErrorUnit error_unit)
static TypeId GetTypeId()
Get the type ID.
~RateErrorModel() override
bool DoCorrupt(Ptr< Packet > p) override
Corrupt a packet according to the specified model.
Ptr< RandomVariableStream > m_ranvar
rng stream
virtual bool DoCorruptPkt(Ptr< Packet > p)
Corrupt a packet (packet unit).
void SetRandomVariable(Ptr< RandomVariableStream >)
RateErrorModel::ErrorUnit GetUnit() const
double GetRate() const
Provide a list of Packets to corrupt.
std::list< uint32_t > PacketList
Typedef: packet sequence number list.
void DoReset() override
Re-initialize any state.
bool DoCorrupt(Ptr< Packet > p) override
Corrupt a packet according to the specified model.
std::list< uint32_t >::const_iterator PacketListCI
Typedef: packet sequence number list const iterator.
static TypeId GetTypeId()
Get the type ID.
uint32_t m_timesInvoked
number of times the error model has been invoked
std::list< uint32_t > GetList() const
PacketList m_packetList
container of sequence number of packets to corrupt
void SetList(const std::list< uint32_t > &packetlist)
a unique identifier for an interface.
Definition type-id.h:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.