g1050.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * g1050.c - IP network modeling, as per G.1050/TIA-921.
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2007 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #if defined(HAVE_CONFIG_H)
  26. #include "config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <inttypes.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <stdio.h>
  34. #include <fcntl.h>
  35. #if defined(HAVE_TGMATH_H)
  36. #include <tgmath.h>
  37. #endif
  38. #if defined(HAVE_MATH_H)
  39. #define GEN_CONST
  40. #include <math.h>
  41. #endif
  42. #if defined(HAVE_STDBOOL_H)
  43. #include <stdbool.h>
  44. #else
  45. #include "spandsp/stdbool.h"
  46. #endif
  47. #include "floating_fudge.h"
  48. #include "spandsp.h"
  49. #include "spandsp/g1050.h"
  50. #define PACKET_LOSS_TIME -1
  51. g1050_constants_t g1050_constants[1] =
  52. {
  53. {
  54. {
  55. { /* Side A LAN */
  56. {
  57. 0.004, /*! Probability of loss rate change low->high */
  58. 0.1 /*! Probability of loss rate change high->low */
  59. },
  60. {
  61. {
  62. 0.0, /*! Probability of an impulse */
  63. 0.0,
  64. },
  65. {
  66. 0.5,
  67. 0.0
  68. }
  69. },
  70. 1.0, /*! Impulse height, based on MTU and bit rate */
  71. 0.0, /*! Impulse decay coefficient */
  72. 0.001, /*! Probability of packet loss due to occupancy. */
  73. 0.15 /*! Probability of packet loss due to a multiple access collision. */
  74. },
  75. { /* Side A access link */
  76. {
  77. 0.0002, /*! Probability of loss rate change low->high */
  78. 0.2 /*! Probability of loss rate change high->low */
  79. },
  80. {
  81. {
  82. 0.001, /*! Probability of an impulse */
  83. 0.0,
  84. },
  85. {
  86. 0.3,
  87. 0.4
  88. }
  89. },
  90. 40.0, /*! Impulse height, based on MTU and bit rate */
  91. 0.75, /*! Impulse decay coefficient */
  92. 0.0005, /*! Probability of packet loss due to occupancy. */
  93. 0.0 /*! Probability of packet loss due to a multiple access collision. */
  94. },
  95. { /* Side B access link */
  96. {
  97. 0.0002, /*! Probability of loss rate change low->high */
  98. 0.2 /*! Probability of loss rate change high->low */
  99. },
  100. {
  101. {
  102. 0.001, /*! Probability of an impulse */
  103. 0.0,
  104. },
  105. {
  106. 0.3,
  107. 0.4
  108. }
  109. },
  110. 40.0, /*! Impulse height, based on MTU and bit rate */
  111. 0.75, /*! Impulse decay coefficient */
  112. 0.0005, /*! Probability of packet loss due to occupancy. */
  113. 0.0 /*! Probability of packet loss due to a multiple access collision. */
  114. },
  115. { /* Side B LAN */
  116. {
  117. 0.004, /*! Probability of loss rate change low->high */
  118. 0.1 /*! Probability of loss rate change high->low */
  119. },
  120. {
  121. {
  122. 0.0, /*! Probability of an impulse */
  123. 0.0,
  124. },
  125. {
  126. 0.5,
  127. 0.0
  128. }
  129. },
  130. 1.0, /*! Impulse height, based on MTU and bit rate */
  131. 0.0, /*! Impulse decay coefficient */
  132. 0.001, /*! Probability of packet loss due to occupancy. */
  133. 0.15 /*! Probability of packet loss due to a multiple access collision. */
  134. }
  135. }
  136. }
  137. };
  138. g1050_channel_speeds_t g1050_speed_patterns[168] =
  139. {
  140. { 4000000, 0, 128000, 768000, 0, 4000000, 0, 128000, 768000, 0, 0.360},
  141. { 4000000, 0, 128000, 768000, 0, 20000000, 0, 128000, 768000, 0, 0.720},
  142. { 4000000, 0, 128000, 768000, 0, 100000000, 0, 128000, 768000, 0, 0.360},
  143. { 20000000, 0, 128000, 768000, 0, 20000000, 0, 128000, 768000, 0, 0.360},
  144. { 20000000, 0, 128000, 768000, 0, 100000000, 0, 128000, 768000, 0, 0.360},
  145. {100000000, 0, 128000, 768000, 0, 100000000, 0, 128000, 768000, 0, 0.090},
  146. { 4000000, 0, 128000, 1536000, 0, 4000000, 0, 384000, 768000, 0, 0.720},
  147. { 4000000, 0, 128000, 1536000, 0, 20000000, 0, 384000, 768000, 0, 1.470},
  148. { 4000000, 0, 128000, 1536000, 0, 100000000, 0, 384000, 768000, 0, 0.840},
  149. { 20000000, 0, 128000, 1536000, 0, 20000000, 0, 384000, 768000, 0, 0.750},
  150. { 20000000, 0, 128000, 1536000, 0, 100000000, 0, 384000, 768000, 0, 0.855},
  151. {100000000, 0, 128000, 1536000, 0, 100000000, 0, 384000, 768000, 0, 0.240},
  152. { 4000000, 0, 128000, 3000000, 0, 4000000, 0, 384000, 768000, 0, 0.120},
  153. { 4000000, 0, 128000, 3000000, 0, 20000000, 0, 384000, 768000, 0, 0.420},
  154. { 4000000, 0, 128000, 3000000, 0, 100000000, 0, 384000, 768000, 0, 0.840},
  155. { 20000000, 0, 128000, 3000000, 0, 20000000, 0, 384000, 768000, 0, 0.300},
  156. { 20000000, 0, 128000, 3000000, 0, 100000000, 0, 384000, 768000, 0, 0.930},
  157. {100000000, 0, 128000, 3000000, 0, 100000000, 0, 384000, 768000, 0, 0.390},
  158. { 4000000, 0, 384000, 768000, 0, 4000000, 0, 128000, 1536000, 0, 0.720},
  159. { 4000000, 0, 384000, 768000, 0, 20000000, 0, 128000, 1536000, 0, 1.470},
  160. { 4000000, 0, 384000, 768000, 0, 100000000, 0, 128000, 1536000, 0, 0.840},
  161. { 20000000, 0, 384000, 768000, 0, 20000000, 0, 128000, 1536000, 0, 0.750},
  162. { 20000000, 0, 384000, 768000, 0, 100000000, 0, 128000, 1536000, 0, 0.855},
  163. {100000000, 0, 384000, 768000, 0, 100000000, 0, 128000, 1536000, 0, 0.240},
  164. { 4000000, 0, 384000, 1536000, 0, 4000000, 0, 384000, 1536000, 0, 1.440},
  165. { 4000000, 0, 384000, 1536000, 0, 20000000, 0, 384000, 1536000, 0, 3.000},
  166. { 4000000, 0, 384000, 1536000, 0, 100000000, 0, 384000, 1536000, 0, 1.920},
  167. { 20000000, 0, 384000, 1536000, 0, 20000000, 0, 384000, 1536000, 0, 1.563},
  168. { 20000000, 0, 384000, 1536000, 0, 100000000, 0, 384000, 1536000, 0, 2.000},
  169. {100000000, 0, 384000, 1536000, 0, 100000000, 0, 384000, 1536000, 0, 0.640},
  170. { 4000000, 0, 384000, 3000000, 0, 4000000, 0, 384000, 1536000, 0, 0.240},
  171. { 4000000, 0, 384000, 3000000, 0, 20000000, 0, 384000, 1536000, 0, 0.850},
  172. { 4000000, 0, 384000, 3000000, 0, 100000000, 0, 384000, 1536000, 0, 1.720},
  173. { 20000000, 0, 384000, 3000000, 0, 20000000, 0, 384000, 1536000, 0, 0.625},
  174. { 20000000, 0, 384000, 3000000, 0, 100000000, 0, 384000, 1536000, 0, 2.025},
  175. {100000000, 0, 384000, 3000000, 0, 100000000, 0, 384000, 1536000, 0, 1.040},
  176. { 4000000, 0, 384000, 768000, 0, 4000000, 0, 128000, 3000000, 0, 0.120},
  177. { 4000000, 0, 384000, 768000, 0, 20000000, 0, 128000, 3000000, 0, 0.420},
  178. { 4000000, 0, 384000, 768000, 0, 100000000, 0, 128000, 3000000, 0, 0.840},
  179. { 20000000, 0, 384000, 768000, 0, 20000000, 0, 128000, 3000000, 0, 0.300},
  180. { 20000000, 0, 384000, 768000, 0, 100000000, 0, 128000, 3000000, 0, 0.930},
  181. {100000000, 0, 384000, 768000, 0, 100000000, 0, 128000, 3000000, 0, 0.390},
  182. { 4000000, 0, 384000, 1536000, 0, 4000000, 0, 384000, 3000000, 0, 0.240},
  183. { 4000000, 0, 384000, 1536000, 0, 20000000, 0, 384000, 3000000, 0, 0.850},
  184. { 4000000, 0, 384000, 1536000, 0, 100000000, 0, 384000, 3000000, 0, 1.720},
  185. { 20000000, 0, 384000, 1536000, 0, 20000000, 0, 384000, 3000000, 0, 0.625},
  186. { 20000000, 0, 384000, 1536000, 0, 100000000, 0, 384000, 3000000, 0, 2.025},
  187. {100000000, 0, 384000, 1536000, 0, 100000000, 0, 384000, 3000000, 0, 1.040},
  188. { 4000000, 0, 384000, 3000000, 0, 4000000, 0, 384000, 3000000, 0, 0.040},
  189. { 4000000, 0, 384000, 3000000, 0, 20000000, 0, 384000, 3000000, 0, 0.200},
  190. { 4000000, 0, 384000, 3000000, 0, 100000000, 0, 384000, 3000000, 0, 0.520},
  191. { 20000000, 0, 384000, 3000000, 0, 20000000, 0, 384000, 3000000, 0, 0.250},
  192. { 20000000, 0, 384000, 3000000, 0, 100000000, 0, 384000, 3000000, 0, 1.300},
  193. {100000000, 0, 384000, 3000000, 0, 100000000, 0, 384000, 3000000, 0, 1.690},
  194. { 4000000, 0, 128000, 1536000, 0, 20000000, 0, 768000, 1536000, 0, 0.090},
  195. { 4000000, 0, 128000, 1536000, 0, 100000000, 0, 768000, 1536000, 0, 0.360},
  196. { 20000000, 0, 128000, 1536000, 0, 20000000, 0, 768000, 1536000, 0, 0.090},
  197. { 20000000, 0, 128000, 1536000, 0, 100000000, 0, 768000, 1536000, 0, 0.405},
  198. {100000000, 0, 128000, 1536000, 0, 100000000, 0, 768000, 1536000, 0, 0.180},
  199. { 4000000, 0, 128000, 7000000, 0, 20000000, 0, 768000, 768000, 0, 0.270},
  200. { 4000000, 0, 128000, 7000000, 0, 100000000, 0, 768000, 768000, 0, 1.080},
  201. { 20000000, 0, 128000, 7000000, 0, 20000000, 0, 768000, 768000, 0, 0.270},
  202. { 20000000, 0, 128000, 7000000, 0, 100000000, 0, 768000, 768000, 0, 1.215},
  203. {100000000, 0, 128000, 7000000, 0, 100000000, 0, 768000, 768000, 0, 0.540},
  204. { 4000000, 0, 128000, 13000000, 0, 20000000, 0, 768000, 13000000, 0, 0.030},
  205. { 4000000, 0, 128000, 13000000, 0, 100000000, 0, 768000, 13000000, 0, 0.120},
  206. { 20000000, 0, 128000, 13000000, 0, 20000000, 0, 768000, 13000000, 0, 0.030},
  207. { 20000000, 0, 128000, 13000000, 0, 100000000, 0, 768000, 13000000, 0, 0.135},
  208. {100000000, 0, 128000, 13000000, 0, 100000000, 0, 768000, 13000000, 0, 0.060},
  209. { 4000000, 0, 384000, 1536000, 0, 20000000, 0, 1536000, 1536000, 0, 0.180},
  210. { 4000000, 0, 384000, 1536000, 0, 100000000, 0, 1536000, 1536000, 0, 0.720},
  211. { 20000000, 0, 384000, 1536000, 0, 20000000, 0, 1536000, 1536000, 0, 0.188},
  212. { 20000000, 0, 384000, 1536000, 0, 100000000, 0, 1536000, 1536000, 0, 0.870},
  213. {100000000, 0, 384000, 1536000, 0, 100000000, 0, 1536000, 1536000, 0, 0.480},
  214. { 4000000, 0, 384000, 7000000, 0, 20000000, 0, 768000, 1536000, 0, 0.540},
  215. { 4000000, 0, 384000, 7000000, 0, 100000000, 0, 768000, 1536000, 0, 2.160},
  216. { 20000000, 0, 384000, 7000000, 0, 20000000, 0, 768000, 1536000, 0, 0.563},
  217. { 20000000, 0, 384000, 7000000, 0, 100000000, 0, 768000, 1536000, 0, 2.610},
  218. {100000000, 0, 384000, 7000000, 0, 100000000, 0, 768000, 1536000, 0, 1.440},
  219. { 4000000, 0, 384000, 13000000, 0, 20000000, 0, 1536000, 13000000, 0, 0.060},
  220. { 4000000, 0, 384000, 13000000, 0, 100000000, 0, 1536000, 13000000, 0, 0.240},
  221. { 20000000, 0, 384000, 13000000, 0, 20000000, 0, 1536000, 13000000, 0, 0.063},
  222. { 20000000, 0, 384000, 13000000, 0, 100000000, 0, 1536000, 13000000, 0, 0.290},
  223. {100000000, 0, 384000, 13000000, 0, 100000000, 0, 1536000, 13000000, 0, 0.160},
  224. { 4000000, 0, 384000, 1536000, 0, 20000000, 0, 1536000, 3000000, 0, 0.030},
  225. { 4000000, 0, 384000, 1536000, 0, 100000000, 0, 1536000, 3000000, 0, 0.120},
  226. { 20000000, 0, 384000, 1536000, 0, 20000000, 0, 1536000, 3000000, 0, 0.075},
  227. { 20000000, 0, 384000, 1536000, 0, 100000000, 0, 1536000, 3000000, 0, 0.495},
  228. {100000000, 0, 384000, 1536000, 0, 100000000, 0, 1536000, 3000000, 0, 0.780},
  229. { 4000000, 0, 384000, 7000000, 0, 20000000, 0, 768000, 3000000, 0, 0.090},
  230. { 4000000, 0, 384000, 7000000, 0, 100000000, 0, 768000, 3000000, 0, 0.360},
  231. { 20000000, 0, 384000, 7000000, 0, 20000000, 0, 768000, 3000000, 0, 0.225},
  232. { 20000000, 0, 384000, 7000000, 0, 100000000, 0, 768000, 3000000, 0, 1.485},
  233. {100000000, 0, 384000, 7000000, 0, 100000000, 0, 768000, 3000000, 0, 2.340},
  234. { 4000000, 0, 384000, 13000000, 0, 20000000, 0, 3000000, 13000000, 0, 0.010},
  235. { 4000000, 0, 384000, 13000000, 0, 100000000, 0, 3000000, 13000000, 0, 0.040},
  236. { 20000000, 0, 384000, 13000000, 0, 20000000, 0, 3000000, 13000000, 0, 0.025},
  237. { 20000000, 0, 384000, 13000000, 0, 100000000, 0, 3000000, 13000000, 0, 0.165},
  238. {100000000, 0, 384000, 13000000, 0, 100000000, 0, 3000000, 13000000, 0, 0.260},
  239. { 4000000, 0, 768000, 1536000, 0, 20000000, 0, 128000, 1536000, 0, 0.090},
  240. { 20000000, 0, 768000, 1536000, 0, 20000000, 0, 128000, 1536000, 0, 0.090},
  241. { 20000000, 0, 768000, 1536000, 0, 100000000, 0, 128000, 1536000, 0, 0.405},
  242. { 4000000, 0, 768000, 1536000, 0, 100000000, 0, 128000, 1536000, 0, 0.360},
  243. {100000000, 0, 768000, 1536000, 0, 100000000, 0, 128000, 1536000, 0, 0.180},
  244. { 4000000, 0, 1536000, 1536000, 0, 20000000, 0, 384000, 1536000, 0, 0.180},
  245. { 20000000, 0, 1536000, 1536000, 0, 20000000, 0, 384000, 1536000, 0, 0.188},
  246. { 20000000, 0, 1536000, 1536000, 0, 100000000, 0, 384000, 1536000, 0, 0.870},
  247. { 4000000, 0, 1536000, 1536000, 0, 100000000, 0, 384000, 1536000, 0, 0.720},
  248. {100000000, 0, 1536000, 1536000, 0, 100000000, 0, 384000, 1536000, 0, 0.480},
  249. { 4000000, 0, 1536000, 3000000, 0, 20000000, 0, 384000, 1536000, 0, 0.030},
  250. { 20000000, 0, 1536000, 3000000, 0, 20000000, 0, 384000, 1536000, 0, 0.075},
  251. { 20000000, 0, 1536000, 3000000, 0, 100000000, 0, 384000, 1536000, 0, 0.495},
  252. { 4000000, 0, 1536000, 3000000, 0, 100000000, 0, 384000, 1536000, 0, 0.120},
  253. {100000000, 0, 1536000, 3000000, 0, 100000000, 0, 384000, 1536000, 0, 0.780},
  254. { 4000000, 0, 768000, 768000, 0, 20000000, 0, 128000, 7000000, 0, 0.270},
  255. { 20000000, 0, 768000, 768000, 0, 20000000, 0, 128000, 7000000, 0, 0.270},
  256. { 20000000, 0, 768000, 768000, 0, 100000000, 0, 128000, 7000000, 0, 1.215},
  257. { 4000000, 0, 768000, 768000, 0, 100000000, 0, 128000, 7000000, 0, 1.080},
  258. {100000000, 0, 768000, 768000, 0, 100000000, 0, 128000, 7000000, 0, 0.540},
  259. { 4000000, 0, 768000, 1536000, 0, 20000000, 0, 384000, 7000000, 0, 0.540},
  260. { 20000000, 0, 768000, 1536000, 0, 20000000, 0, 384000, 7000000, 0, 0.563},
  261. { 20000000, 0, 768000, 1536000, 0, 100000000, 0, 384000, 7000000, 0, 2.610},
  262. { 4000000, 0, 768000, 1536000, 0, 100000000, 0, 384000, 7000000, 0, 2.160},
  263. {100000000, 0, 768000, 1536000, 0, 100000000, 0, 384000, 7000000, 0, 1.440},
  264. { 4000000, 0, 768000, 3000000, 0, 20000000, 0, 384000, 7000000, 0, 0.090},
  265. { 20000000, 0, 768000, 3000000, 0, 20000000, 0, 384000, 7000000, 0, 0.225},
  266. { 20000000, 0, 768000, 3000000, 0, 100000000, 0, 384000, 7000000, 0, 1.485},
  267. { 4000000, 0, 768000, 3000000, 0, 100000000, 0, 384000, 7000000, 0, 0.360},
  268. {100000000, 0, 768000, 3000000, 0, 100000000, 0, 384000, 7000000, 0, 2.340},
  269. { 4000000, 0, 768000, 13000000, 0, 20000000, 0, 128000, 13000000, 0, 0.030},
  270. { 20000000, 0, 768000, 13000000, 0, 20000000, 0, 128000, 13000000, 0, 0.030},
  271. { 20000000, 0, 768000, 13000000, 0, 100000000, 0, 128000, 13000000, 0, 0.135},
  272. { 4000000, 0, 768000, 13000000, 0, 100000000, 0, 128000, 13000000, 0, 0.120},
  273. {100000000, 0, 768000, 13000000, 0, 100000000, 0, 128000, 13000000, 0, 0.060},
  274. { 4000000, 0, 1536000, 13000000, 0, 20000000, 0, 384000, 13000000, 0, 0.060},
  275. { 20000000, 0, 1536000, 13000000, 0, 20000000, 0, 384000, 13000000, 0, 0.063},
  276. { 20000000, 0, 1536000, 13000000, 0, 100000000, 0, 384000, 13000000, 0, 0.290},
  277. { 4000000, 0, 1536000, 13000000, 0, 100000000, 0, 384000, 13000000, 0, 0.240},
  278. {100000000, 0, 1536000, 13000000, 0, 100000000, 0, 384000, 13000000, 0, 0.160},
  279. { 4000000, 0, 3000000, 13000000, 0, 20000000, 0, 384000, 13000000, 0, 0.010},
  280. { 20000000, 0, 3000000, 13000000, 0, 20000000, 0, 384000, 13000000, 0, 0.025},
  281. { 20000000, 0, 3000000, 13000000, 0, 100000000, 0, 384000, 13000000, 0, 0.165},
  282. { 4000000, 0, 3000000, 13000000, 0, 100000000, 0, 384000, 13000000, 0, 0.040},
  283. {100000000, 0, 3000000, 13000000, 0, 100000000, 0, 384000, 13000000, 0, 0.260},
  284. { 20000000, 0, 1536000, 1536000, 0, 20000000, 0, 1536000, 1536000, 0, 0.023},
  285. { 20000000, 0, 1536000, 1536000, 0, 100000000, 0, 1536000, 1536000, 0, 0.180},
  286. {100000000, 0, 1536000, 1536000, 0, 100000000, 0, 1536000, 1536000, 0, 0.360},
  287. { 20000000, 0, 1536000, 7000000, 0, 20000000, 0, 768000, 1536000, 0, 0.068},
  288. { 20000000, 0, 1536000, 7000000, 0, 100000000, 0, 768000, 1536000, 0, 0.540},
  289. {100000000, 0, 1536000, 7000000, 0, 100000000, 0, 768000, 1536000, 0, 1.080},
  290. { 20000000, 0, 1536000, 13000000, 0, 20000000, 0, 1536000, 13000000, 0, 0.015},
  291. { 20000000, 0, 1536000, 13000000, 0, 100000000, 0, 1536000, 13000000, 0, 0.120},
  292. {100000000, 0, 1536000, 13000000, 0, 100000000, 0, 1536000, 13000000, 0, 0.240},
  293. { 20000000, 0, 768000, 1536000, 0, 20000000, 0, 1536000, 7000000, 0, 0.068},
  294. { 20000000, 0, 768000, 1536000, 0, 100000000, 0, 1536000, 7000000, 0, 0.540},
  295. {100000000, 0, 768000, 1536000, 0, 100000000, 0, 1536000, 7000000, 0, 1.080},
  296. { 20000000, 0, 768000, 7000000, 0, 20000000, 0, 768000, 7000000, 0, 0.203},
  297. { 20000000, 0, 768000, 7000000, 0, 100000000, 0, 768000, 7000000, 0, 1.620},
  298. {100000000, 0, 768000, 7000000, 0, 100000000, 0, 768000, 7000000, 0, 3.240},
  299. { 20000000, 0, 768000, 13000000, 0, 20000000, 0, 7000000, 13000000, 0, 0.023},
  300. { 20000000, 0, 768000, 13000000, 0, 100000000, 0, 7000000, 13000000, 0, 0.180},
  301. {100000000, 0, 768000, 13000000, 0, 100000000, 0, 7000000, 13000000, 0, 0.360},
  302. { 20000000, 0, 7000000, 13000000, 0, 20000000, 0, 768000, 13000000, 0, 0.023},
  303. { 20000000, 0, 7000000, 13000000, 0, 100000000, 0, 768000, 13000000, 0, 0.180},
  304. {100000000, 0, 7000000, 13000000, 0, 100000000, 0, 768000, 13000000, 0, 0.360},
  305. { 20000000, 0, 13000000, 13000000, 0, 20000000, 0, 13000000, 13000000, 0, 0.003},
  306. { 20000000, 0, 13000000, 13000000, 0, 100000000, 0, 13000000, 13000000, 0, 0.020},
  307. {100000000, 0, 13000000, 13000000, 0, 100000000, 0, 13000000, 13000000, 0, 0.040}
  308. };
  309. g1050_model_t g1050_standard_models[9] =
  310. {
  311. { /* Severity 0 - no impairment */
  312. {
  313. 0, /*! Percentage likelihood of occurance in scenario A */
  314. 0, /*! Percentage likelihood of occurance in scenario B */
  315. 0, /*! Percentage likelihood of occurance in scenario C */
  316. },
  317. {
  318. 0.0, /*! Percentage occupancy */
  319. 1508, /*! MTU */
  320. 0.0 /*! Peak jitter */
  321. },
  322. {
  323. 0.0, /*! Percentage occupancy */
  324. 512, /*! MTU */
  325. 0.0 /*! Peak jitter */
  326. },
  327. {
  328. 0.0, /*! Basic delay of the regional backbone, in seconds */
  329. 0.0, /*! Basic delay of the intercontinental backbone, in seconds */
  330. 0.0, /*! Percentage packet loss of the backbone */
  331. 0.0, /*! Maximum jitter of the backbone, in seconds */
  332. 0.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  333. 0.0, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  334. 0.0, /*! The interval between link failures, in seconds */
  335. 0.0, /*! The duration of link failures, in seconds */
  336. 0.0, /*! Probability of packet loss in the backbone, in percent */
  337. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  338. },
  339. {
  340. 0.0, /*! Percentage occupancy */
  341. 512, /*! MTU */
  342. 0.0 /*! Peak jitter */
  343. },
  344. {
  345. 0.0, /*! Percentage occupancy */
  346. 1508, /*! MTU */
  347. 0.0 /*! Peak jitter */
  348. }
  349. },
  350. { /* Severity A */
  351. {
  352. 50, /*! Percentage likelihood of occurance in scenario A */
  353. 5, /*! Percentage likelihood of occurance in scenario B */
  354. 5, /*! Percentage likelihood of occurance in scenario C */
  355. },
  356. {
  357. 1.0, /*! Percentage occupancy */
  358. 1508, /*! MTU */
  359. 0.0015 /*! Peak jitter */
  360. },
  361. {
  362. 0.0, /*! Percentage occupancy */
  363. 512, /*! MTU */
  364. 0.0 /*! Peak jitter */
  365. },
  366. {
  367. 0.004, /*! Basic delay of the regional backbone, in seconds */
  368. 0.016, /*! Basic delay of the intercontinental backbone, in seconds */
  369. 0.0, /*! Percentage packet loss of the backbone */
  370. 0.005, /*! Maximum jitter of the backbone, in seconds */
  371. 0.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  372. 0.0, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  373. 0.0, /*! The interval between link failures, in seconds */
  374. 0.0, /*! The duration of link failures, in seconds */
  375. 0.0, /*! Probability of packet loss in the backbone, in percent */
  376. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  377. },
  378. {
  379. 0.0, /*! Percentage occupancy */
  380. 512, /*! MTU */
  381. 0.0 /*! Peak jitter */
  382. },
  383. {
  384. 1.0, /*! Percentage occupancy */
  385. 1508, /*! MTU */
  386. 0.0015 /*! Peak jitter */
  387. }
  388. },
  389. { /* Severity B */
  390. {
  391. 30, /*! Percentage likelihood of occurance in scenario A */
  392. 25, /*! Percentage likelihood of occurance in scenario B */
  393. 5, /*! Percentage likelihood of occurance in scenario C */
  394. },
  395. {
  396. 2.0, /*! Percentage occupancy */
  397. 1508, /*! MTU */
  398. 0.0015 /*! Peak jitter */
  399. },
  400. {
  401. 1.0, /*! Percentage occupancy */
  402. 512, /*! MTU */
  403. 0.0 /*! Peak jitter */
  404. },
  405. {
  406. 0.008, /*! Basic delay of the regional backbone, in seconds */
  407. 0.032, /*! Basic delay of the intercontinental backbone, in seconds */
  408. 0.01, /*! Percentage packet loss of the backbone */
  409. 0.01, /*! Maximum jitter of the backbone, in seconds */
  410. 3600.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  411. 0.002, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  412. 3600.0, /*! The interval between link failures, in seconds */
  413. 0.064, /*! The duration of link failures, in seconds */
  414. 0.0, /*! Probability of packet loss in the backbone, in percent */
  415. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  416. },
  417. {
  418. 1.0, /*! Percentage occupancy */
  419. 512, /*! MTU */
  420. 0.0 /*! Peak jitter */
  421. },
  422. {
  423. 2.0, /*! Percentage occupancy */
  424. 1508, /*! MTU */
  425. 0.0015 /*! Peak jitter */
  426. }
  427. },
  428. { /* Severity C */
  429. {
  430. 15, /*! Percentage likelihood of occurance in scenario A */
  431. 30, /*! Percentage likelihood of occurance in scenario B */
  432. 10, /*! Percentage likelihood of occurance in scenario C */
  433. },
  434. {
  435. 3.0, /*! Percentage occupancy */
  436. 1508, /*! MTU */
  437. 0.0015 /*! Peak jitter */
  438. },
  439. {
  440. 2.0, /*! Percentage occupancy */
  441. 1508, /*! MTU */
  442. 0.0 /*! Peak jitter */
  443. },
  444. {
  445. 0.016, /*! Basic delay of the regional backbone, in seconds */
  446. 0.064, /*! Basic delay of the intercontinental backbone, in seconds */
  447. 0.02, /*! Percentage packet loss of the backbone */
  448. 0.016, /*! Maximum jitter of the backbone, in seconds */
  449. 1800.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  450. 0.004, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  451. 1800.0, /*! The interval between link failures, in seconds */
  452. 0.128, /*! The duration of link failures, in seconds */
  453. 0.0, /*! Probability of packet loss in the backbone, in percent */
  454. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  455. },
  456. {
  457. 2.0, /*! Percentage occupancy */
  458. 1508, /*! MTU */
  459. 0.0 /*! Peak jitter */
  460. },
  461. {
  462. 3.0, /*! Percentage occupancy */
  463. 1508, /*! MTU */
  464. 0.0015 /*! Peak jitter */
  465. }
  466. },
  467. { /* Severity D */
  468. {
  469. 5, /*! Percentage likelihood of occurance in scenario A */
  470. 25, /*! Percentage likelihood of occurance in scenario B */
  471. 15, /*! Percentage likelihood of occurance in scenario C */
  472. },
  473. {
  474. 5.0, /*! Percentage occupancy */
  475. 1508, /*! MTU */
  476. 0.0015 /*! Peak jitter */
  477. },
  478. {
  479. 4.0, /*! Percentage occupancy */
  480. 1508, /*! MTU */
  481. 0.0 /*! Peak jitter */
  482. },
  483. {
  484. 0.032, /*! Basic delay of the regional backbone, in seconds */
  485. 0.128, /*! Basic delay of the intercontinental backbone, in seconds */
  486. 0.04, /*! Percentage packet loss of the backbone */
  487. 0.04, /*! Maximum jitter of the backbone, in seconds */
  488. 900.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  489. 0.008, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  490. 900.0, /*! The interval between link failures, in seconds */
  491. 0.256, /*! The duration of link failures, in seconds */
  492. 0.0, /*! Probability of packet loss in the backbone, in percent */
  493. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  494. },
  495. {
  496. 4.0, /*! Percentage occupancy */
  497. 1508, /*! MTU */
  498. 0.0 /*! Peak jitter */
  499. },
  500. {
  501. 5.0, /*! Percentage occupancy */
  502. 1508, /*! MTU */
  503. 0.0015 /*! Peak jitter */
  504. }
  505. },
  506. { /* Severity E */
  507. {
  508. 0, /*! Percentage likelihood of occurance in scenario A */
  509. 10, /*! Percentage likelihood of occurance in scenario B */
  510. 20, /*! Percentage likelihood of occurance in scenario C */
  511. },
  512. {
  513. 8.0, /*! Percentage occupancy */
  514. 1508, /*! MTU */
  515. 0.0015 /*! Peak jitter */
  516. },
  517. {
  518. 8.0, /*! Percentage occupancy */
  519. 1508, /*! MTU */
  520. 0.0 /*! Peak jitter */
  521. },
  522. {
  523. 0.064, /*! Basic delay of the regional backbone, in seconds */
  524. 0.196, /*! Basic delay of the intercontinental backbone, in seconds */
  525. 0.1, /*! Percentage packet loss of the backbone */
  526. 0.07, /*! Maximum jitter of the backbone, in seconds */
  527. 480.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  528. 0.016, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  529. 480.0, /*! The interval between link failures, in seconds */
  530. 0.4, /*! The duration of link failures, in seconds */
  531. 0.0, /*! Probability of packet loss in the backbone, in percent */
  532. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  533. },
  534. {
  535. 8.0, /*! Percentage occupancy */
  536. 1508, /*! MTU */
  537. 0.0 /*! Peak jitter */
  538. },
  539. {
  540. 8.0, /*! Percentage occupancy */
  541. 1508, /*! MTU */
  542. 0.0015 /*! Peak jitter */
  543. }
  544. },
  545. { /* Severity F */
  546. {
  547. 0, /*! Percentage likelihood of occurance in scenario A */
  548. 0, /*! Percentage likelihood of occurance in scenario B */
  549. 25, /*! Percentage likelihood of occurance in scenario C */
  550. },
  551. {
  552. 12.0, /*! Percentage occupancy */
  553. 1508, /*! MTU */
  554. 0.0015 /*! Peak jitter */
  555. },
  556. {
  557. 15.0, /*! Percentage occupancy */
  558. 1508, /*! MTU */
  559. 0.0 /*! Peak jitter */
  560. },
  561. {
  562. 0.128, /*! Basic delay of the regional backbone, in seconds */
  563. 0.256, /*! Basic delay of the intercontinental backbone, in seconds */
  564. 0.2, /*! Percentage packet loss of the backbone */
  565. 0.1, /*! Maximum jitter of the backbone, in seconds */
  566. 240.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  567. 0.032, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  568. 240.0, /*! The interval between link failures, in seconds */
  569. 0.8, /*! The duration of link failures, in seconds */
  570. 0.0, /*! Probability of packet loss in the backbone, in percent */
  571. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  572. },
  573. {
  574. 15.0, /*! Percentage occupancy */
  575. 1508, /*! MTU */
  576. 0.0 /*! Peak jitter */
  577. },
  578. {
  579. 12.0, /*! Percentage occupancy */
  580. 1508, /*! MTU */
  581. 0.0015 /*! Peak jitter */
  582. }
  583. },
  584. { /* Severity G */
  585. {
  586. 0, /*! Percentage likelihood of occurance in scenario A */
  587. 0, /*! Percentage likelihood of occurance in scenario B */
  588. 15, /*! Percentage likelihood of occurance in scenario C */
  589. },
  590. {
  591. 16.0, /*! Percentage occupancy */
  592. 1508, /*! MTU */
  593. 0.0015 /*! Peak jitter */
  594. },
  595. {
  596. 30.0, /*! Percentage occupancy */
  597. 1508, /*! MTU */
  598. 0.0 /*! Peak jitter */
  599. },
  600. {
  601. 0.256, /*! Basic delay of the regional backbone, in seconds */
  602. 0.512, /*! Basic delay of the intercontinental backbone, in seconds */
  603. 0.5, /*! Percentage packet loss of the backbone */
  604. 0.15, /*! Maximum jitter of the backbone, in seconds */
  605. 120.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  606. 0.064, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  607. 120.0, /*! The interval between link failures, in seconds */
  608. 1.6, /*! The duration of link failures, in seconds */
  609. 0.0, /*! Probability of packet loss in the backbone, in percent */
  610. 0.0 /*! Probability of a packet going out of sequence in the backbone. */
  611. },
  612. {
  613. 30.0, /*! Percentage occupancy */
  614. 1508, /*! MTU */
  615. 0.0 /*! Peak jitter */
  616. },
  617. {
  618. 16.0, /*! Percentage occupancy */
  619. 1508, /*! MTU */
  620. 0.0015 /*! Peak jitter */
  621. }
  622. },
  623. { /* Severity H */
  624. {
  625. 0, /*! Percentage likelihood of occurance in scenario A */
  626. 0, /*! Percentage likelihood of occurance in scenario B */
  627. 5, /*! Percentage likelihood of occurance in scenario C */
  628. },
  629. {
  630. 20.0, /*! Percentage occupancy */
  631. 1508, /*! MTU */
  632. 0.0015 /*! Peak jitter */
  633. },
  634. {
  635. 50.0, /*! Percentage occupancy */
  636. 1508, /*! MTU */
  637. 0.0 /*! Peak jitter */
  638. },
  639. {
  640. 0.512, /*! Basic delay of the regional backbone, in seconds */
  641. 0.768, /*! Basic delay of the intercontinental backbone, in seconds */
  642. 1.0, /*! Percentage packet loss of the backbone */
  643. 0.5, /*! Maximum jitter of the backbone, in seconds */
  644. 60.0, /*! Interval between the backbone route flapping between two paths, in seconds */
  645. 0.128, /*! The difference in backbone delay between the two routes we flap between, in seconds */
  646. 60.0, /*! The interval between link failures, in seconds */
  647. 3.0, /*! The duration of link failures, in seconds */
  648. 1.0, /*! Probability of packet loss in the backbone, in percent */
  649. 1.0 /*! Probability of a packet going out of sequence in the backbone. */
  650. },
  651. {
  652. 50.0, /*! Percentage occupancy */
  653. 1508, /*! MTU */
  654. 0.0 /*! Peak jitter */
  655. },
  656. {
  657. 20.0, /*! Percentage occupancy */
  658. 1508, /*! MTU */
  659. 0.0015 /*! Peak jitter */
  660. }
  661. }
  662. };
  663. #if defined(HAVE_DRAND48)
  664. static __inline__ void q1050_rand_init(void)
  665. {
  666. srand48(time(NULL));
  667. }
  668. /*- End of function --------------------------------------------------------*/
  669. static __inline__ double q1050_rand(void)
  670. {
  671. return drand48();
  672. }
  673. /*- End of function --------------------------------------------------------*/
  674. #else
  675. static __inline__ void q1050_rand_init(void)
  676. {
  677. srand(time(NULL));
  678. }
  679. /*- End of function --------------------------------------------------------*/
  680. static __inline__ double q1050_rand(void)
  681. {
  682. return (double) rand()/(double) RAND_MAX;
  683. }
  684. /*- End of function --------------------------------------------------------*/
  685. #endif
  686. static __inline__ double scale_probability(double prob, double scale)
  687. {
  688. /* Re-calculate probability based on a different time interval */
  689. return 1.0 - pow(1.0 - prob, scale);
  690. }
  691. /*- End of function --------------------------------------------------------*/
  692. static void g1050_segment_init(g1050_segment_state_t *s,
  693. int link_type,
  694. g1050_segment_constants_t *constants,
  695. g1050_segment_model_t *parms,
  696. int bit_rate,
  697. int multiple_access,
  698. int qos_enabled,
  699. int packet_size,
  700. int packet_rate)
  701. {
  702. double x;
  703. double packet_interval;
  704. memset(s, 0, sizeof(*s));
  705. packet_interval = 1000.0/packet_rate;
  706. /* Some calculatons are common to both LAN and access links, and those that are not. */
  707. s->link_type = link_type;
  708. s->prob_loss_rate_change[0] = scale_probability(constants->prob_loss_rate_change[0]*parms->percentage_occupancy, 1.0/packet_interval);
  709. s->serial_delay = packet_size*8.0/bit_rate;
  710. if (link_type == G1050_LAN_LINK)
  711. {
  712. s->prob_loss_rate_change[1] = scale_probability(constants->prob_loss_rate_change[1], 1.0/packet_interval);
  713. s->prob_impulse[0] = constants->prob_impulse[0][0];
  714. s->prob_impulse[1] = constants->prob_impulse[1][0];
  715. s->impulse_coeff = constants->impulse_coeff;
  716. s->impulse_height = parms->mtu*(8.0/bit_rate)*(1.0 + parms->percentage_occupancy/constants->impulse_height);
  717. }
  718. else if (link_type == G1050_ACCESS_LINK)
  719. {
  720. s->prob_loss_rate_change[1] = scale_probability(constants->prob_loss_rate_change[1]/(1.0 + parms->percentage_occupancy), 1.0/packet_interval);
  721. s->prob_impulse[0] = scale_probability(constants->prob_impulse[0][0] + (parms->percentage_occupancy/2000.0), 1.0/packet_interval);
  722. s->prob_impulse[1] = scale_probability(constants->prob_impulse[1][0] + (constants->prob_impulse[1][1]*parms->percentage_occupancy/100.0), 1.0/packet_interval);
  723. s->impulse_coeff = 1.0 - scale_probability(1.0 - constants->impulse_coeff, 1.0/packet_interval);
  724. x = (1.0 - constants->impulse_coeff)/(1.0 - s->impulse_coeff);
  725. s->impulse_height = x*parms->mtu*(8.0/bit_rate)*(1.0 + parms->percentage_occupancy/constants->impulse_height);
  726. }
  727. /* The following are calculated the same way for LAN and access links */
  728. s->prob_packet_loss = constants->prob_packet_loss*parms->percentage_occupancy;
  729. s->qos_enabled = qos_enabled;
  730. s->multiple_access = multiple_access;
  731. s->prob_packet_collision_loss = constants->prob_packet_collision_loss;
  732. s->max_jitter = parms->max_jitter;
  733. /* The following is common state information to all links. */
  734. s->high_loss = false;
  735. s->congestion_delay = 0.0;
  736. s->last_arrival_time = 0.0;
  737. /* Count of packets lost in this segment. */
  738. s->lost_packets = 0;
  739. s->lost_packets_2 = 0;
  740. }
  741. /*- End of function --------------------------------------------------------*/
  742. static void g1050_core_init(g1050_core_state_t *s, g1050_core_model_t *parms, int packet_rate)
  743. {
  744. memset(s, 0, sizeof(*s));
  745. /* Set up route flapping. */
  746. /* This is the length of the period of both the delayed duration and the non-delayed. */
  747. s->route_flap_interval = parms->route_flap_interval*G1050_TICKS_PER_SEC;
  748. /* How much additional delay is added or subtracted during route flaps. */
  749. s->route_flap_delta = parms->route_flap_delay;
  750. /* Current tick count. This is initialized so that we are part way into the first
  751. CLEAN interval before the first change occurs. This is a random portion of the
  752. period. When we reach the first flap, the flapping in both directions becomes
  753. periodic. */
  754. s->route_flap_counter = s->route_flap_interval - 99 - floor(s->route_flap_interval*q1050_rand());
  755. s->link_failure_interval_ticks = parms->link_failure_interval*G1050_TICKS_PER_SEC;
  756. /* Link failures occur when the count reaches this number of ticks. */
  757. /* Duration of a failure. */
  758. s->link_failure_duration_ticks = floor((G1050_TICKS_PER_SEC*parms->link_failure_duration));
  759. /* How far into the first CLEAN interval we are. This is like the route flap initialzation. */
  760. s->link_failure_counter = s->link_failure_interval_ticks - 99 - floor(s->link_failure_interval_ticks*q1050_rand());
  761. s->link_recovery_counter = s->link_failure_duration_ticks;
  762. s->base_delay = parms->base_regional_delay;
  763. s->max_jitter = parms->max_jitter;
  764. s->prob_packet_loss = parms->prob_packet_loss/100.0;
  765. s->prob_oos = parms->prob_oos/100.0;
  766. s->last_arrival_time = 0.0;
  767. s->delay_delta = 0;
  768. /* Count of packets lost in this segment. */
  769. s->lost_packets = 0;
  770. s->lost_packets_2 = 0;
  771. }
  772. /*- End of function --------------------------------------------------------*/
  773. static void g1050_segment_model(g1050_segment_state_t *s, double delays[], int len)
  774. {
  775. int i;
  776. bool lose;
  777. int was_high_loss;
  778. double impulse;
  779. double slice_delay;
  780. /* Compute delay and loss value for each time slice. */
  781. for (i = 0; i < len; i++)
  782. {
  783. lose = false;
  784. /* Initialize delay to the serial delay plus some jitter. */
  785. slice_delay = s->serial_delay + s->max_jitter*q1050_rand();
  786. /* If no QoS, do congestion delay and packet loss analysis. */
  787. if (!s->qos_enabled)
  788. {
  789. /* To match the logic in G.1050 we need to record the current loss state, before
  790. checking if we should change. */
  791. was_high_loss = s->high_loss;
  792. /* Toggle between the low-loss and high-loss states, based on the transition probability. */
  793. if (q1050_rand() < s->prob_loss_rate_change[was_high_loss])
  794. s->high_loss = !s->high_loss;
  795. impulse = 0.0;
  796. if (q1050_rand() < s->prob_impulse[was_high_loss])
  797. {
  798. impulse = s->impulse_height;
  799. if (!was_high_loss || s->link_type == G1050_LAN_LINK)
  800. impulse *= q1050_rand();
  801. }
  802. if (was_high_loss && q1050_rand() < s->prob_packet_loss)
  803. lose = true;
  804. /* Single pole LPF for the congestion delay impulses. */
  805. s->congestion_delay = s->congestion_delay*s->impulse_coeff + impulse*(1.0 - s->impulse_coeff);
  806. slice_delay += s->congestion_delay;
  807. }
  808. /* If duplex mismatch on LAN, packet loss based on loss probability. */
  809. if (s->multiple_access && (q1050_rand() < s->prob_packet_collision_loss))
  810. lose = true;
  811. /* Put computed delay into time slice array. */
  812. if (lose)
  813. {
  814. delays[i] = PACKET_LOSS_TIME;
  815. s->lost_packets++;
  816. }
  817. else
  818. {
  819. delays[i] = slice_delay;
  820. }
  821. }
  822. }
  823. /*- End of function --------------------------------------------------------*/
  824. static void g1050_core_model(g1050_core_state_t *s, double delays[], int len)
  825. {
  826. int32_t i;
  827. bool lose;
  828. double jitter_delay;
  829. for (i = 0; i < len; i++)
  830. {
  831. lose = false;
  832. jitter_delay = s->base_delay + s->max_jitter*q1050_rand();
  833. /* Route flapping */
  834. if (--s->route_flap_counter <= 0)
  835. {
  836. /* Route changed */
  837. s->delay_delta = s->route_flap_delta - s->delay_delta;
  838. s->route_flap_counter = s->route_flap_interval;
  839. }
  840. if (q1050_rand() < s->prob_packet_loss)
  841. lose = true;
  842. /* Link failures */
  843. if (--s->link_failure_counter <= 0)
  844. {
  845. /* We are in a link failure */
  846. lose = true;
  847. if (--s->link_recovery_counter <= 0)
  848. {
  849. /* Leave failure state. */
  850. s->link_failure_counter = s->link_failure_interval_ticks;
  851. s->link_recovery_counter = s->link_failure_duration_ticks;
  852. lose = false;
  853. }
  854. }
  855. if (lose)
  856. {
  857. delays[i] = PACKET_LOSS_TIME;
  858. s->lost_packets++;
  859. }
  860. else
  861. {
  862. delays[i] = jitter_delay + s->delay_delta;
  863. }
  864. }
  865. }
  866. /*- End of function --------------------------------------------------------*/
  867. static int g1050_segment_delay(g1050_segment_state_t *s,
  868. double base_time,
  869. double arrival_times[],
  870. double delays[],
  871. int num_packets)
  872. {
  873. int i;
  874. int32_t departure_time;
  875. int lost_packets;
  876. /* Add appropriate delays to the packets for the segments before the core. */
  877. lost_packets = 0;
  878. for (i = 0; i < num_packets; i++)
  879. {
  880. /* Apply half a millisecond of rounding, as we working in millisecond steps. */
  881. departure_time = (arrival_times[i] + 0.0005 - base_time)*G1050_TICKS_PER_SEC;
  882. if (arrival_times[i] == PACKET_LOSS_TIME)
  883. {
  884. /* Lost already */
  885. }
  886. else if (delays[departure_time] == PACKET_LOSS_TIME)
  887. {
  888. arrival_times[i] = PACKET_LOSS_TIME;
  889. lost_packets++;
  890. }
  891. else
  892. {
  893. arrival_times[i] += delays[departure_time];
  894. if (arrival_times[i] < s->last_arrival_time)
  895. arrival_times[i] = s->last_arrival_time;
  896. else
  897. s->last_arrival_time = arrival_times[i];
  898. }
  899. }
  900. return lost_packets;
  901. }
  902. /*- End of function --------------------------------------------------------*/
  903. static int g1050_segment_delay_preserve_order(g1050_segment_state_t *s,
  904. double base_time,
  905. double arrival_times_a[],
  906. double arrival_times_b[],
  907. double delays[],
  908. int num_packets)
  909. {
  910. int i;
  911. int j;
  912. int departure_time;
  913. double last_arrival_time;
  914. double last_arrival_time_temp;
  915. int lost_packets;
  916. /* Add appropriate delays to the packets for the segments after the core. */
  917. last_arrival_time = 0.0;
  918. last_arrival_time_temp = 0.0;
  919. lost_packets = 0;
  920. for (i = 0; i < num_packets; i++)
  921. {
  922. /* We need to preserve the order that came out of the core, so we
  923. use an alternate array for the results. */
  924. /* Apply half a millisecond of rounding, as we working in millisecond steps. */
  925. departure_time = (arrival_times_a[i] + 0.0005 - base_time)*G1050_TICKS_PER_SEC;
  926. if (arrival_times_a[i] == PACKET_LOSS_TIME)
  927. {
  928. /* Lost already */
  929. arrival_times_b[i] = PACKET_LOSS_TIME;
  930. }
  931. else if (delays[departure_time] == PACKET_LOSS_TIME)
  932. {
  933. arrival_times_b[i] = PACKET_LOSS_TIME;
  934. lost_packets++;
  935. }
  936. else
  937. {
  938. arrival_times_b[i] = arrival_times_a[i] + delays[departure_time];
  939. if (arrival_times_a[i] < last_arrival_time)
  940. {
  941. /* If a legitimate out of sequence packet is detected, search
  942. back a fixed amount of time to preserve order. */
  943. for (j = i - 1; j >= 0; j--)
  944. {
  945. if ((arrival_times_a[j] != PACKET_LOSS_TIME)
  946. &&
  947. (arrival_times_b[j] != PACKET_LOSS_TIME))
  948. {
  949. if ((arrival_times_a[i] - arrival_times_a[j]) > SEARCHBACK_PERIOD)
  950. break;
  951. if ((arrival_times_a[j] > arrival_times_a[i])
  952. &&
  953. (arrival_times_b[j] < arrival_times_b[i]))
  954. {
  955. arrival_times_b[j] = arrival_times_b[i];
  956. }
  957. }
  958. }
  959. }
  960. else
  961. {
  962. last_arrival_time = arrival_times_a[i];
  963. if (arrival_times_b[i] < last_arrival_time_temp)
  964. arrival_times_b[i] = last_arrival_time_temp;
  965. else
  966. last_arrival_time_temp = arrival_times_b[i];
  967. }
  968. }
  969. }
  970. return lost_packets;
  971. }
  972. /*- End of function --------------------------------------------------------*/
  973. static int g1050_core_delay(g1050_core_state_t *s,
  974. double base_time,
  975. double arrival_times[],
  976. double delays[],
  977. int num_packets)
  978. {
  979. int i;
  980. int departure_time;
  981. int lost_packets;
  982. /* This element does NOT preserve packet order. */
  983. lost_packets = 0;
  984. for (i = 0; i < num_packets; i++)
  985. {
  986. /* Apply half a millisecond of rounding, as we working in millisecond steps. */
  987. departure_time = (arrival_times[i] + 0.0005 - base_time)*G1050_TICKS_PER_SEC;
  988. if (arrival_times[i] == PACKET_LOSS_TIME)
  989. {
  990. /* Lost already */
  991. }
  992. else if (delays[departure_time] == PACKET_LOSS_TIME)
  993. {
  994. arrival_times[i] = PACKET_LOSS_TIME;
  995. lost_packets++;
  996. }
  997. else
  998. {
  999. /* Not lost. Compute arrival time. */
  1000. arrival_times[i] += delays[departure_time];
  1001. if (arrival_times[i] < s->last_arrival_time)
  1002. {
  1003. /* This packet is EARLIER than the last one. It is out of order! */
  1004. /* Do we allow it to stay out of order? */
  1005. if (q1050_rand() >= s->prob_oos)
  1006. arrival_times[i] = s->last_arrival_time;
  1007. }
  1008. else
  1009. {
  1010. /* Packet is in the correct order, relative to the last one. */
  1011. s->last_arrival_time = arrival_times[i];
  1012. }
  1013. }
  1014. }
  1015. return lost_packets;
  1016. }
  1017. /*- End of function --------------------------------------------------------*/
  1018. static void g1050_simulate_chunk(g1050_state_t *s)
  1019. {
  1020. int i;
  1021. s->base_time += 1.0;
  1022. memmove(&s->segment[0].delays[0], &s->segment[0].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[0].delays[0]));
  1023. g1050_segment_model(&s->segment[0], &s->segment[0].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
  1024. memmove(&s->segment[1].delays[0], &s->segment[1].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[1].delays[0]));
  1025. g1050_segment_model(&s->segment[1], &s->segment[1].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
  1026. memmove(&s->core.delays[0], &s->core.delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->core.delays[0]));
  1027. g1050_core_model(&s->core, &s->core.delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
  1028. memmove(&s->segment[2].delays[0], &s->segment[2].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[2].delays[0]));
  1029. g1050_segment_model(&s->segment[2], &s->segment[2].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
  1030. memmove(&s->segment[3].delays[0], &s->segment[3].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[3].delays[0]));
  1031. g1050_segment_model(&s->segment[3], &s->segment[3].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
  1032. memmove(&s->arrival_times_1[0], &s->arrival_times_1[s->packet_rate], 2*s->packet_rate*sizeof(s->arrival_times_1[0]));
  1033. memmove(&s->arrival_times_2[0], &s->arrival_times_2[s->packet_rate], 2*s->packet_rate*sizeof(s->arrival_times_2[0]));
  1034. for (i = 0; i < s->packet_rate; i++)
  1035. {
  1036. s->arrival_times_1[2*s->packet_rate + i] = s->base_time + 2.0 + (double) i/(double) s->packet_rate;
  1037. s->arrival_times_2[2*s->packet_rate + i] = 0.0;
  1038. }
  1039. s->segment[0].lost_packets_2 += g1050_segment_delay(&s->segment[0], s->base_time, s->arrival_times_1, s->segment[0].delays, s->packet_rate);
  1040. s->segment[1].lost_packets_2 += g1050_segment_delay(&s->segment[1], s->base_time, s->arrival_times_1, s->segment[1].delays, s->packet_rate);
  1041. s->core.lost_packets_2 += g1050_core_delay(&s->core, s->base_time, s->arrival_times_1, s->core.delays, s->packet_rate);
  1042. s->segment[2].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[2], s->base_time, s->arrival_times_1, s->arrival_times_2, s->segment[2].delays, s->packet_rate);
  1043. s->segment[3].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[3], s->base_time, s->arrival_times_2, s->arrival_times_1, s->segment[3].delays, s->packet_rate);
  1044. }
  1045. /*- End of function --------------------------------------------------------*/
  1046. SPAN_DECLARE(g1050_state_t *) g1050_init(int model,
  1047. int speed_pattern,
  1048. int packet_size,
  1049. int packet_rate)
  1050. {
  1051. g1050_state_t *s;
  1052. g1050_constants_t *constants;
  1053. g1050_channel_speeds_t *sp;
  1054. g1050_model_t *mo;
  1055. int i;
  1056. /* If the random generator has not been seeded it might give endless
  1057. zeroes - it depends on the platform. */
  1058. for (i = 0; i < 10; i++)
  1059. {
  1060. if (q1050_rand() != 0.0)
  1061. break;
  1062. }
  1063. if (i >= 10)
  1064. q1050_rand_init();
  1065. if ((s = (g1050_state_t *) malloc(sizeof(*s))) == NULL)
  1066. return NULL;
  1067. memset(s, 0, sizeof(*s));
  1068. constants = &g1050_constants[0];
  1069. sp = &g1050_speed_patterns[speed_pattern - 1];
  1070. mo = &g1050_standard_models[model];
  1071. memset(s, 0, sizeof(*s));
  1072. s->packet_rate = packet_rate;
  1073. s->packet_size = packet_size;
  1074. g1050_segment_init(&s->segment[0],
  1075. G1050_LAN_LINK,
  1076. &constants->segment[0],
  1077. &mo->sidea_lan,
  1078. sp->sidea_lan_bit_rate,
  1079. sp->sidea_lan_multiple_access,
  1080. false,
  1081. packet_size,
  1082. packet_rate);
  1083. g1050_segment_init(&s->segment[1],
  1084. G1050_ACCESS_LINK,
  1085. &constants->segment[1],
  1086. &mo->sidea_access_link,
  1087. sp->sidea_access_link_bit_rate_ab,
  1088. false,
  1089. sp->sidea_access_link_qos_enabled,
  1090. packet_size,
  1091. packet_rate);
  1092. g1050_core_init(&s->core, &mo->core, packet_rate);
  1093. g1050_segment_init(&s->segment[2],
  1094. G1050_ACCESS_LINK,
  1095. &constants->segment[2],
  1096. &mo->sideb_access_link,
  1097. sp->sideb_access_link_bit_rate_ba,
  1098. false,
  1099. sp->sideb_access_link_qos_enabled,
  1100. packet_size,
  1101. packet_rate);
  1102. g1050_segment_init(&s->segment[3],
  1103. G1050_LAN_LINK,
  1104. &constants->segment[3],
  1105. &mo->sideb_lan,
  1106. sp->sideb_lan_bit_rate,
  1107. sp->sideb_lan_multiple_access,
  1108. false,
  1109. packet_size,
  1110. packet_rate);
  1111. s->base_time = 0.0;
  1112. /* Start with enough of the future modelled to allow for the worst jitter.
  1113. After this we will always keep at least 2 seconds of the future modelled. */
  1114. g1050_segment_model(&s->segment[0], s->segment[0].delays, 3*G1050_TICKS_PER_SEC);
  1115. g1050_segment_model(&s->segment[1], s->segment[1].delays, 3*G1050_TICKS_PER_SEC);
  1116. g1050_core_model(&s->core, s->core.delays, 3*G1050_TICKS_PER_SEC);
  1117. g1050_segment_model(&s->segment[2], s->segment[2].delays, 3*G1050_TICKS_PER_SEC);
  1118. g1050_segment_model(&s->segment[3], s->segment[3].delays, 3*G1050_TICKS_PER_SEC);
  1119. /* Initialise the arrival times to the departure times */
  1120. for (i = 0; i < 3*s->packet_rate; i++)
  1121. {
  1122. s->arrival_times_1[i] = s->base_time + (double) i/(double)s->packet_rate;
  1123. s->arrival_times_2[i] = 0.0;
  1124. }
  1125. s->segment[0].lost_packets_2 += g1050_segment_delay(&s->segment[0], s->base_time, s->arrival_times_1, s->segment[0].delays, s->packet_rate);
  1126. s->segment[1].lost_packets_2 += g1050_segment_delay(&s->segment[1], s->base_time, s->arrival_times_1, s->segment[1].delays, s->packet_rate);
  1127. s->core.lost_packets_2 += g1050_core_delay(&s->core, s->base_time, s->arrival_times_1, s->core.delays, s->packet_rate);
  1128. s->segment[2].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[2], s->base_time, s->arrival_times_1, s->arrival_times_2, s->segment[2].delays, s->packet_rate);
  1129. s->segment[3].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[3], s->base_time, s->arrival_times_2, s->arrival_times_1, s->segment[3].delays, s->packet_rate);
  1130. s->first = NULL;
  1131. s->last = NULL;
  1132. return s;
  1133. }
  1134. /*- End of function --------------------------------------------------------*/
  1135. SPAN_DECLARE(int) g1050_free(g1050_state_t *s)
  1136. {
  1137. free(s);
  1138. return 0;
  1139. }
  1140. /*- End of function --------------------------------------------------------*/
  1141. SPAN_DECLARE(void) g1050_dump_parms(int model, int speed_pattern)
  1142. {
  1143. g1050_channel_speeds_t *sp;
  1144. g1050_model_t *mo;
  1145. sp = &g1050_speed_patterns[speed_pattern - 1];
  1146. mo = &g1050_standard_models[model];
  1147. printf("Model %d%c\n", speed_pattern, 'A' + model - 1);
  1148. printf("LOO %.6f%% %.6f%% %.6f%%\n", mo->loo[0]*sp->loo/100.0, mo->loo[1]*sp->loo/100.0, mo->loo[2]*sp->loo/100.0);
  1149. printf("Side A LAN %dbps, %.3f%% occupancy, MTU %d, %s MA\n", sp->sidea_lan_bit_rate, mo->sidea_lan.percentage_occupancy, mo->sidea_lan.mtu, (sp->sidea_lan_multiple_access) ? "" : "no");
  1150. printf("Side A access %dbps, %.3f%% occupancy, MTU %d, %s QoS\n", sp->sidea_access_link_bit_rate_ab, mo->sidea_access_link.percentage_occupancy, mo->sidea_access_link.mtu, (sp->sidea_access_link_qos_enabled) ? "" : "no");
  1151. printf("Core delay %.4fs (%.4fs), peak jitter %.4fs, prob loss %.4f%%, prob OOS %.4f%%\n", mo->core.base_regional_delay, mo->core.base_intercontinental_delay, mo->core.max_jitter, mo->core.prob_packet_loss, mo->core.prob_oos);
  1152. printf(" Route flap interval %.4fs, delay change %.4fs\n", mo->core.route_flap_interval, mo->core.route_flap_delay);
  1153. printf(" Link failure interval %.4fs, duration %.4fs\n", mo->core.link_failure_interval, mo->core.link_failure_duration);
  1154. printf("Side B access %dbps, %.3f%% occupancy, MTU %d, %s QoS\n", sp->sideb_access_link_bit_rate_ba, mo->sideb_access_link.percentage_occupancy, mo->sideb_access_link.mtu, (sp->sideb_access_link_qos_enabled) ? "" : "no");
  1155. printf("Side B LAN %dbps, %.3f%% occupancy, MTU %d, %s MA\n", sp->sideb_lan_bit_rate, mo->sideb_lan.percentage_occupancy, mo->sideb_lan.mtu, (sp->sideb_lan_multiple_access) ? "" : "no");
  1156. }
  1157. /*- End of function --------------------------------------------------------*/
  1158. SPAN_DECLARE(int) g1050_put(g1050_state_t *s, const uint8_t buf[], int len, int seq_no, double departure_time)
  1159. {
  1160. g1050_queue_element_t *element;
  1161. g1050_queue_element_t *e;
  1162. double arrival_time;
  1163. while (departure_time >= s->base_time + 1.0)
  1164. g1050_simulate_chunk(s);
  1165. arrival_time = s->arrival_times_1[(int) ((departure_time - s->base_time)*(double) s->packet_rate + 0.5)];
  1166. if (arrival_time < 0)
  1167. {
  1168. /* This packet is lost */
  1169. return 0;
  1170. }
  1171. if ((element = (g1050_queue_element_t *) malloc(sizeof(*element) + len)) == NULL)
  1172. return -1;
  1173. element->next = NULL;
  1174. element->prev = NULL;
  1175. element->seq_no = seq_no;
  1176. element->departure_time = departure_time;
  1177. element->arrival_time = arrival_time;
  1178. element->len = len;
  1179. memcpy(element->pkt, buf, len);
  1180. /* Add it to the queue, in order */
  1181. if (s->last == NULL)
  1182. {
  1183. /* The queue is empty */
  1184. s->first =
  1185. s->last = element;
  1186. }
  1187. else
  1188. {
  1189. for (e = s->last; e; e = e->prev)
  1190. {
  1191. if (e->arrival_time <= arrival_time)
  1192. break;
  1193. }
  1194. if (e)
  1195. {
  1196. element->next = e->next;
  1197. element->prev = e;
  1198. e->next = element;
  1199. }
  1200. else
  1201. {
  1202. element->next = s->first;
  1203. s->first = element;
  1204. }
  1205. if (element->next)
  1206. element->next->prev = element;
  1207. else
  1208. s->last = element;
  1209. }
  1210. //printf(">> Seq %d, departs %f, arrives %f\n", seq_no, departure_time, arrival_time);
  1211. return len;
  1212. }
  1213. /*- End of function --------------------------------------------------------*/
  1214. SPAN_DECLARE(int) g1050_get(g1050_state_t *s, uint8_t buf[], int max_len, double current_time, int *seq_no, double *departure_time, double *arrival_time)
  1215. {
  1216. int len;
  1217. g1050_queue_element_t *element;
  1218. element = s->first;
  1219. if (element == NULL)
  1220. {
  1221. if (seq_no)
  1222. *seq_no = -1;
  1223. if (departure_time)
  1224. *departure_time = -1;
  1225. if (arrival_time)
  1226. *arrival_time = -1;
  1227. return -1;
  1228. }
  1229. if (element->arrival_time > current_time)
  1230. {
  1231. if (seq_no)
  1232. *seq_no = element->seq_no;
  1233. if (departure_time)
  1234. *departure_time = element->departure_time;
  1235. if (arrival_time)
  1236. *arrival_time = element->arrival_time;
  1237. return -1;
  1238. }
  1239. /* Return the first packet in the queue */
  1240. len = element->len;
  1241. memcpy(buf, element->pkt, len);
  1242. if (seq_no)
  1243. *seq_no = element->seq_no;
  1244. if (departure_time)
  1245. *departure_time = element->departure_time;
  1246. if (arrival_time)
  1247. *arrival_time = element->arrival_time;
  1248. //printf("<< Seq %d, arrives %f (%f)\n", element->seq_no, element->arrival_time, current_time);
  1249. /* Remove it from the queue */
  1250. if (s->first == s->last)
  1251. s->last = NULL;
  1252. s->first = element->next;
  1253. if (element->next)
  1254. element->next->prev = NULL;
  1255. free(element);
  1256. return len;
  1257. }
  1258. /*- End of function --------------------------------------------------------*/
  1259. SPAN_DECLARE(void) g1050_queue_dump(g1050_state_t *s)
  1260. {
  1261. g1050_queue_element_t *e;
  1262. printf("Queue scanned forewards\n");
  1263. for (e = s->first; e; e = e->next)
  1264. printf("Seq %5d, arrival %10.4f, len %3d\n", e->seq_no, e->arrival_time, e->len);
  1265. printf("Queue scanned backwards\n");
  1266. for (e = s->last; e; e = e->prev)
  1267. printf("Seq %5d, arrival %10.4f, len %3d\n", e->seq_no, e->arrival_time, e->len);
  1268. }
  1269. /*- End of function --------------------------------------------------------*/
  1270. /*- End of file ------------------------------------------------------------*/