PageRenderTime 5ms CodeModel.GetById 8ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/network/utils/error-model.h

https://github.com/scarletttu/ns-3-codel-dev
C++ Header | 310 lines | 83 code | 32 blank | 195 comment | 0 complexity | e5435c789f90c8bd8a91f6d29b1e4c32 MD5 | raw file
  1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2/*
  3 * Copyright (c) 2007 University of Washington
  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#ifndef ERROR_MODEL_H
 56#define ERROR_MODEL_H
 57
 58#include <list>
 59#include "ns3/object.h"
 60#include "ns3/random-variable.h"
 61
 62namespace ns3 {
 63
 64class Packet;
 65
 66/**
 67 * \ingroup network
 68 * \defgroup errormodel Error Model
 69 */
 70/**
 71 * \ingroup errormodel
 72 * \brief General error model that can be used to corrupt packets
 73 *
 74 * This object is used to flag packets as being lost/errored or not.
 75 * It is part of the Object framework and can be aggregated to 
 76 * other ns3 objects and handled by the Ptr class.
 77 *
 78 * The main method is IsCorrupt(Ptr<Packet> p) which returns true if 
 79 * the packet is to be corrupted according to the underlying model.
 80 * Depending on the error model, the packet itself may have its packet
 81 * data buffer errored or not, or side information may be returned to
 82 * the client in the form of a packet tag.  (Note:  No such error models
 83 * that actually error the bits in a packet presently exist).
 84 * The object can have state (resettable by Reset()).
 85 * The object can also be enabled and disabled via two public member functions.
 86 * 
 87 * Typical code (simplified) to use an ErrorModel may look something like 
 88 * this:
 89 * \code 
 90 * Ptr<ErrorModel> rem = CreateObject<RateErrorModel> ();
 91 * rem->SetRandomVariable (UniformVariable ());
 92 * rem->SetRate (0.001);
 93 * ...
 94 * Ptr<Packet> p;
 95 * if (rem->IsCorrupt (p))
 96 *   {
 97 *     dropTrace(p);
 98 *   } else {
 99 *     Forward (p);
100 *   }
101 * \endcode
102 *
103 * Two practical error models, a ListErrorModel and a RateErrorModel,
104 * are currently implemented. 
105 */
106class ErrorModel : public Object
107{
108public:
109  static TypeId GetTypeId (void);
110
111  ErrorModel ();
112  virtual ~ErrorModel ();
113
114  /**
115   * Note:  Depending on the error model, this function may or may not
116   * alter the contents of the packet upon returning true.
117   *
118   * \returns true if the Packet is to be considered as errored/corrupted
119   * \param pkt Packet to apply error model to
120   */
121  bool IsCorrupt (Ptr<Packet> pkt);
122  /**
123   * Reset any state associated with the error model
124   */
125  void Reset (void);
126  /**
127   * Enable the error model
128   */
129  void Enable (void);
130  /**
131   * Disable the error model
132   */
133  void Disable (void);
134  /**
135   * \return true if error model is enabled; false otherwise
136   */
137  bool IsEnabled (void) const;
138
139private:
140  /*
141   * These methods must be implemented by subclasses
142   */
143  virtual bool DoCorrupt (Ptr<Packet>) = 0;
144  virtual void DoReset (void) = 0;
145
146  bool m_enable;
147};
148
149/**
150 * \brief Determine which packets are errored corresponding to an underlying
151 * distribution, rate, and unit.
152 *
153 * This object is used to flag packets as being lost/errored or not.
154 * The two parameters that govern the behavior are the rate (or
155 * equivalently, the mean duration/spacing between errors), and the
156 * unit (which may be per-bit, per-byte, and per-packet).
157 * Users can optionally provide a RandomVariable object; the default
158 * is to use a Uniform(0,1) distribution.
159
160 * Reset() on this model will do nothing
161 *
162 * IsCorrupt() will not modify the packet data buffer
163 */
164class RateErrorModel : public ErrorModel
165{
166public:
167  static TypeId GetTypeId (void);
168
169  RateErrorModel ();
170  virtual ~RateErrorModel ();
171
172  enum ErrorUnit
173  {
174    ERROR_UNIT_BIT,
175    ERROR_UNIT_BYTE,
176    ERROR_UNIT_PACKET
177  };
178
179  /**
180   * \returns the ErrorUnit being used by the underlying model
181   */ 
182  RateErrorModel::ErrorUnit GetUnit (void) const;
183  /**
184   * \param error_unit the ErrorUnit to be used by the underlying model
185   */ 
186  void SetUnit (enum ErrorUnit error_unit);
187
188  /**
189   * \returns the error rate being applied by the model
190   */ 
191  double GetRate (void) const;
192  /**
193   * \param rate the error rate to be used by the model
194   */ 
195  void SetRate (double rate);
196
197  /**
198   * \param ranvar A random variable distribution to generate random variates
199   */ 
200  void SetRandomVariable (const RandomVariable &ranvar);
201
202private:
203  virtual bool DoCorrupt (Ptr<Packet> p);
204  virtual bool DoCorruptPkt (Ptr<Packet> p);
205  virtual bool DoCorruptByte (Ptr<Packet> p);
206  virtual bool DoCorruptBit (Ptr<Packet> p);
207  virtual void DoReset (void);
208
209  enum ErrorUnit m_unit;
210  double m_rate;
211
212  RandomVariable m_ranvar;
213};
214
215/**
216 * \brief Provide a list of Packet uids to corrupt
217 *
218 * This object is used to flag packets as being lost/errored or not.
219 * A note on performance:  the list is assumed to be unordered, and
220 * in general, Packet uids received may be unordered.  Therefore,
221 * each call to IsCorrupt() will result in a walk of the list with
222 * the present underlying implementation.
223 * 
224 * Note also that if one wants to target multiple packets from looking
225 * at an (unerrored) trace file, the act of erroring a given packet may
226 * cause subsequent packet uids to change.  For instance, suppose one wants 
227 * to error packets 11 and 17 on a given device.  It may be that erroring
228 * packet 11 will cause the subsequent uid stream to change and 17 may no
229 * longer correspond to the second packet that one wants to lose.  Therefore,
230 * be advised that it might take some trial and error to select the
231 * right uids when multiple are provided.
232 * 
233 * Reset() on this model will clear the list
234 *
235 * IsCorrupt() will not modify the packet data buffer
236 */
237class ListErrorModel : public ErrorModel
238{
239public:
240  static TypeId GetTypeId (void);
241  ListErrorModel ();
242  virtual ~ListErrorModel ();
243
244  /**
245   * \return a copy of the underlying list
246   */
247  std::list<uint32_t> GetList (void) const;
248  /**
249   * \param packetlist The list of packet uids to error.
250   *
251   * This method overwrites any previously provided list.
252   */
253  void SetList (const std::list<uint32_t> &packetlist);
254
255private:
256  virtual bool DoCorrupt (Ptr<Packet> p);
257  virtual void DoReset (void);
258
259  typedef std::list<uint32_t> PacketList;
260  typedef std::list<uint32_t>::const_iterator PacketListCI;
261
262  PacketList m_packetList;
263
264};
265
266/**
267 * \brief Provide a list of Packets to corrupt
268 *
269 * This model also processes a user-generated list of packets to
270 * corrupt, except that the list corresponds to the sequence of
271 * received packets as observed by this error model, and not the
272 * Packet UID.
273 * 
274 * Reset() on this model will clear the list
275 *
276 * IsCorrupt() will not modify the packet data buffer
277 */
278class ReceiveListErrorModel : public ErrorModel
279{
280public:
281  static TypeId GetTypeId (void);
282  ReceiveListErrorModel ();
283  virtual ~ReceiveListErrorModel ();
284
285  /**
286   * \return a copy of the underlying list
287   */
288  std::list<uint32_t> GetList (void) const;
289  /**
290   * \param packetlist The list of packets to error.
291   *
292   * This method overwrites any previously provided list.
293   */
294  void SetList (const std::list<uint32_t> &packetlist);
295
296private:
297  virtual bool DoCorrupt (Ptr<Packet> p);
298  virtual void DoReset (void);
299
300  typedef std::list<uint32_t> PacketList;
301  typedef std::list<uint32_t>::const_iterator PacketListCI;
302
303  PacketList m_packetList;
304  uint32_t m_timesInvoked;
305
306};
307
308
309} // namespace ns3
310#endif