PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/klenkovic/ns-3-dev-logging-fixes
C Header | 417 lines | 104 code | 49 blank | 264 comment | 0 complexity | 4a75a7005fb8c3905e5897e7e72a9fb6 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. * Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation;
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. *
  20. * This file incorporates work covered by the following copyright and
  21. * permission notice:
  22. *
  23. * Copyright (c) 1997 Regents of the University of California.
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. * 1. Redistributions of source code must retain the above copyright
  30. * notice, this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright
  32. * notice, this list of conditions and the following disclaimer in the
  33. * documentation and/or other materials provided with the distribution.
  34. * 3. Neither the name of the University nor of the Laboratory may be used
  35. * to endorse or promote products derived from this software without
  36. * specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  39. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. *
  50. * Contributed by the Daedalus Research Group, UC Berkeley
  51. * (http://daedalus.cs.berkeley.edu)
  52. *
  53. * This code has been ported from ns-2 (queue/errmodel.{cc,h}
  54. */
  55. /* BurstErrorModel additions
  56. *
  57. * Author: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
  58. * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
  59. * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
  60. */
  61. #ifndef ERROR_MODEL_H
  62. #define ERROR_MODEL_H
  63. #include <list>
  64. #include "ns3/object.h"
  65. #include "ns3/random-variable-stream.h"
  66. namespace ns3 {
  67. class Packet;
  68. /**
  69. * \ingroup network
  70. * \defgroup errormodel Error Model
  71. */
  72. /**
  73. * \ingroup errormodel
  74. * \brief General error model that can be used to corrupt packets
  75. *
  76. * This object is used to flag packets as being lost/errored or not.
  77. * It is part of the Object framework and can be aggregated to
  78. * other ns3 objects and handled by the Ptr class.
  79. *
  80. * The main method is IsCorrupt(Ptr<Packet> p) which returns true if
  81. * the packet is to be corrupted according to the underlying model.
  82. * Depending on the error model, the packet itself may have its packet
  83. * data buffer errored or not, or side information may be returned to
  84. * the client in the form of a packet tag. (Note: No such error models
  85. * that actually error the bits in a packet presently exist).
  86. * The object can have state (resettable by Reset()).
  87. * The object can also be enabled and disabled via two public member functions.
  88. *
  89. * Typical code (simplified) to use an ErrorModel may look something like
  90. * this:
  91. * \code
  92. * Ptr<ErrorModel> rem = CreateObject<RateErrorModel> ();
  93. * Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
  94. * rem->SetRandomVariable (uv);
  95. * rem->SetRate (0.001);
  96. * ...
  97. * Ptr<Packet> p;
  98. * if (rem->IsCorrupt (p))
  99. * {
  100. * dropTrace(p);
  101. * } else {
  102. * Forward (p);
  103. * }
  104. * \endcode
  105. *
  106. * Four practical error models, a RateErrorModel, a BurstErrorModel,
  107. * a ListErrorModel, and a ReceiveListErrorModel, are currently implemented.
  108. */
  109. class ErrorModel : public Object
  110. {
  111. public:
  112. static TypeId GetTypeId (void);
  113. ErrorModel ();
  114. virtual ~ErrorModel ();
  115. /**
  116. * Note: Depending on the error model, this function may or may not
  117. * alter the contents of the packet upon returning true.
  118. *
  119. * \returns true if the Packet is to be considered as errored/corrupted
  120. * \param pkt Packet to apply error model to
  121. */
  122. bool IsCorrupt (Ptr<Packet> pkt);
  123. /**
  124. * Reset any state associated with the error model
  125. */
  126. void Reset (void);
  127. /**
  128. * Enable the error model
  129. */
  130. void Enable (void);
  131. /**
  132. * Disable the error model
  133. */
  134. void Disable (void);
  135. /**
  136. * \return true if error model is enabled; false otherwise
  137. */
  138. bool IsEnabled (void) const;
  139. private:
  140. /*
  141. * These methods must be implemented by subclasses
  142. */
  143. virtual bool DoCorrupt (Ptr<Packet>) = 0;
  144. virtual void DoReset (void) = 0;
  145. bool m_enable;
  146. };
  147. /**
  148. * \brief Determine which packets are errored corresponding to an underlying
  149. * distribution, rate, and unit.
  150. *
  151. * This object is used to flag packets as being lost/errored or not.
  152. * The two parameters that govern the behavior are the rate (or
  153. * equivalently, the mean duration/spacing between errors), and the
  154. * unit (which may be per-bit, per-byte, and per-packet).
  155. * Users can optionally provide a RandomVariableStream object; the default
  156. * is to use a Uniform(0,1) distribution.
  157. * Reset() on this model will do nothing
  158. *
  159. * IsCorrupt() will not modify the packet data buffer
  160. */
  161. class RateErrorModel : public ErrorModel
  162. {
  163. public:
  164. static TypeId GetTypeId (void);
  165. RateErrorModel ();
  166. virtual ~RateErrorModel ();
  167. enum ErrorUnit
  168. {
  169. ERROR_UNIT_BIT,
  170. ERROR_UNIT_BYTE,
  171. ERROR_UNIT_PACKET
  172. };
  173. /**
  174. * \returns the ErrorUnit being used by the underlying model
  175. */
  176. RateErrorModel::ErrorUnit GetUnit (void) const;
  177. /**
  178. * \param error_unit the ErrorUnit to be used by the underlying model
  179. */
  180. void SetUnit (enum ErrorUnit error_unit);
  181. /**
  182. * \returns the error rate being applied by the model
  183. */
  184. double GetRate (void) const;
  185. /**
  186. * \param rate the error rate to be used by the model
  187. */
  188. void SetRate (double rate);
  189. /**
  190. * \param ranvar A random variable distribution to generate random variates
  191. */
  192. void SetRandomVariable (Ptr<RandomVariableStream>);
  193. /**
  194. * Assign a fixed random variable stream number to the random variables
  195. * used by this model. Return the number of streams (possibly zero) that
  196. * have been assigned.
  197. *
  198. * \param stream first stream index to use
  199. * \return the number of stream indices assigned by this model
  200. */
  201. int64_t AssignStreams (int64_t stream);
  202. private:
  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. enum ErrorUnit m_unit;
  209. double m_rate;
  210. Ptr<RandomVariableStream> m_ranvar;
  211. };
  212. /**
  213. * \brief Determine which bursts of packets are errored corresponding to
  214. * an underlying distribution, burst rate, and burst size.
  215. *
  216. * This object is used to flag packets as being lost/errored or not.
  217. * The two parameters that govern the behavior are the burst rate (or
  218. * equivalently, the mean duration/spacing between between error events),
  219. * and the burst size (or equivalently, the number of packets being flagged
  220. * as errored at each error event).
  221. *
  222. * Users can optionally provide RandomVariableStream objects;
  223. * the default for the decision variable is to use a Uniform(0,1) distribution;
  224. * the default for the burst size (number of packets) is to use a
  225. * discrete Uniform[1,4] distribution.
  226. *
  227. * For every packet, the model generates a random number based on the
  228. * decision variable, and compares it with the burst error rate to
  229. * determine if a burst error event should occur.
  230. * If a new error event occurs, the model to will generate a new burst size
  231. * to determine how many packets should be dropped in this particular burst
  232. * error event in addition to the current packet.
  233. *
  234. * When a second packet arrives, the model again determines if a new error
  235. * event should occur based on a newly generated decision variable and
  236. * the burst error rate. If a new error event is determined to occur,
  237. * the model will restart with a new burst size. Otherwise, the model will
  238. * resume the last error event and drop the packet provided that the
  239. * total number of packets that has been dropped does not exceed the
  240. * burst size.
  241. *
  242. * IsCorrupt() will not modify the packet data buffer
  243. */
  244. class BurstErrorModel : public ErrorModel
  245. {
  246. public:
  247. static TypeId GetTypeId (void);
  248. BurstErrorModel ();
  249. virtual ~BurstErrorModel ();
  250. /**
  251. * \returns the error rate being applied by the model
  252. */
  253. double GetBurstRate (void) const;
  254. /**
  255. * \param burstRate the error rate to be used by the model
  256. */
  257. void SetBurstRate (double rate);
  258. /**
  259. * \param ranVariable A random variable distribution to generate random variates
  260. */
  261. void SetRandomVariable (Ptr<RandomVariableStream>);
  262. /**
  263. * \param burstSize A random variable distribution to generate random burst size
  264. */
  265. void SetRandomBurstSize (Ptr<RandomVariableStream>);
  266. /**
  267. * Assign a fixed random variable stream number to the random variables
  268. * used by this model. Return the number of streams (possibly zero) that
  269. * have been assigned.
  270. *
  271. * \param stream first stream index to use
  272. * \return the number of stream indices assigned by this model
  273. */
  274. int64_t AssignStreams (int64_t stream);
  275. private:
  276. virtual bool DoCorrupt (Ptr<Packet> p);
  277. virtual void DoReset (void);
  278. double m_burstRate; //the burst error event
  279. Ptr<RandomVariableStream> m_burstStart; //the error decision variable
  280. Ptr<RandomVariableStream> m_burstSize; //the number of packets being flagged as errored
  281. uint32_t m_counter; //keep track of the number of packets being errored
  282. //until it reaches m_burstSize
  283. uint32_t m_currentBurstSz; //the current burst size
  284. };
  285. /**
  286. * \brief Provide a list of Packet uids to corrupt
  287. *
  288. * This object is used to flag packets as being lost/errored or not.
  289. * A note on performance: the list is assumed to be unordered, and
  290. * in general, Packet uids received may be unordered. Therefore,
  291. * each call to IsCorrupt() will result in a walk of the list with
  292. * the present underlying implementation.
  293. *
  294. * Note also that if one wants to target multiple packets from looking
  295. * at an (unerrored) trace file, the act of erroring a given packet may
  296. * cause subsequent packet uids to change. For instance, suppose one wants
  297. * to error packets 11 and 17 on a given device. It may be that erroring
  298. * packet 11 will cause the subsequent uid stream to change and 17 may no
  299. * longer correspond to the second packet that one wants to lose. Therefore,
  300. * be advised that it might take some trial and error to select the
  301. * right uids when multiple are provided.
  302. *
  303. * Reset() on this model will clear the list
  304. *
  305. * IsCorrupt() will not modify the packet data buffer
  306. */
  307. class ListErrorModel : public ErrorModel
  308. {
  309. public:
  310. static TypeId GetTypeId (void);
  311. ListErrorModel ();
  312. virtual ~ListErrorModel ();
  313. /**
  314. * \return a copy of the underlying list
  315. */
  316. std::list<uint32_t> GetList (void) const;
  317. /**
  318. * \param packetlist The list of packet uids to error.
  319. *
  320. * This method overwrites any previously provided list.
  321. */
  322. void SetList (const std::list<uint32_t> &packetlist);
  323. private:
  324. virtual bool DoCorrupt (Ptr<Packet> p);
  325. virtual void DoReset (void);
  326. typedef std::list<uint32_t> PacketList;
  327. typedef std::list<uint32_t>::const_iterator PacketListCI;
  328. PacketList m_packetList;
  329. };
  330. /**
  331. * \brief Provide a list of Packets to corrupt
  332. *
  333. * This model also processes a user-generated list of packets to
  334. * corrupt, except that the list corresponds to the sequence of
  335. * received packets as observed by this error model, and not the
  336. * Packet UID.
  337. *
  338. * Reset() on this model will clear the list
  339. *
  340. * IsCorrupt() will not modify the packet data buffer
  341. */
  342. class ReceiveListErrorModel : public ErrorModel
  343. {
  344. public:
  345. static TypeId GetTypeId (void);
  346. ReceiveListErrorModel ();
  347. virtual ~ReceiveListErrorModel ();
  348. /**
  349. * \return a copy of the underlying list
  350. */
  351. std::list<uint32_t> GetList (void) const;
  352. /**
  353. * \param packetlist The list of packets to error.
  354. *
  355. * This method overwrites any previously provided list.
  356. */
  357. void SetList (const std::list<uint32_t> &packetlist);
  358. private:
  359. virtual bool DoCorrupt (Ptr<Packet> p);
  360. virtual void DoReset (void);
  361. typedef std::list<uint32_t> PacketList;
  362. typedef std::list<uint32_t>::const_iterator PacketListCI;
  363. PacketList m_packetList;
  364. uint32_t m_timesInvoked;
  365. };
  366. } // namespace ns3
  367. #endif