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