test_fragmentation.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2004, 2009 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fragmentation/test_fragmentation.c
  18. * @brief test for fragmentation.c
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_fragmentation_lib.h"
  23. #define DETAILS GNUNET_NO
  24. /**
  25. * Number of messages to transmit (note: each uses ~32k memory!)
  26. */
  27. #define NUM_MSGS 1000
  28. /**
  29. * MTU to force on fragmentation (must be > 1k + 12)
  30. */
  31. #define MTU 1111
  32. /**
  33. * Simulate dropping of 1 out of how many messages? (must be > 1)
  34. */
  35. #define DROPRATE 15
  36. static int ret = 1;
  37. static unsigned int dups;
  38. static unsigned int fragc;
  39. static unsigned int frag_drops;
  40. static unsigned int acks;
  41. static unsigned int ack_drops;
  42. static struct GNUNET_DEFRAGMENT_Context *defrag;
  43. static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
  44. static struct GNUNET_FRAGMENT_Context *frag;
  45. static struct GNUNET_SCHEDULER_Task *shutdown_task;
  46. static struct GNUNET_TIME_Relative msg_delay;
  47. static struct GNUNET_TIME_Relative ack_delay;
  48. static void
  49. do_shutdown (void *cls)
  50. {
  51. ret = 0;
  52. shutdown_task = NULL;
  53. GNUNET_DEFRAGMENT_context_destroy (defrag);
  54. defrag = NULL;
  55. if (NULL != frag)
  56. {
  57. GNUNET_FRAGMENT_context_destroy (frag, &msg_delay, &ack_delay);
  58. frag = NULL;
  59. }
  60. fprintf (stderr,
  61. "\nFinal message-delay: %s\n",
  62. GNUNET_STRINGS_relative_time_to_string (msg_delay,
  63. GNUNET_YES));
  64. fprintf (stderr,
  65. "Final ack-delay: %s\n",
  66. GNUNET_STRINGS_relative_time_to_string (ack_delay,
  67. GNUNET_YES));
  68. }
  69. static void
  70. proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
  71. {
  72. static unsigned int total;
  73. unsigned int i;
  74. const char *buf;
  75. #if DETAILS
  76. fprintf (stderr, "%s", "M! "); /* message complete, good! */
  77. #endif
  78. buf = (const char *) hdr;
  79. for (i = sizeof(struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
  80. GNUNET_assert (buf[i] == (char) i);
  81. total++;
  82. #if ! DETAILS
  83. if (0 == (total % (NUM_MSGS / 100)))
  84. fprintf (stderr, "%s", ".");
  85. #endif
  86. /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
  87. if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
  88. {
  89. if (NULL == shutdown_task)
  90. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  91. }
  92. }
  93. /**
  94. * Process fragment (by passing to defrag).
  95. */
  96. static void
  97. proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
  98. {
  99. struct GNUNET_FRAGMENT_Context **fc = cls;
  100. int ret;
  101. GNUNET_FRAGMENT_context_transmission_done (*fc);
  102. if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
  103. {
  104. frag_drops++;
  105. #if DETAILS
  106. fprintf (stderr, "%s", "DF "); /* dropped Frag */
  107. #endif
  108. return; /* random drop */
  109. }
  110. if (NULL == defrag)
  111. {
  112. fprintf (stderr, "%s", "?E "); /* Error: frag after shutdown!? */
  113. return;
  114. }
  115. ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
  116. if (ret == GNUNET_NO)
  117. {
  118. #if DETAILS
  119. fprintf (stderr, "%s", "FF "); /* duplicate fragment */
  120. #endif
  121. dups++;
  122. }
  123. else if (ret == GNUNET_OK)
  124. {
  125. #if DETAILS
  126. fprintf (stderr, "%s", "F! "); /* good fragment */
  127. #endif
  128. fragc++;
  129. }
  130. }
  131. static void
  132. next_transmission ()
  133. {
  134. static unsigned int i;
  135. struct GNUNET_MessageHeader *msg;
  136. static char buf[MTU + 32 * 1024];
  137. unsigned int j;
  138. if (0 == i)
  139. {
  140. for (j = 0; j < sizeof(buf); j++)
  141. buf[j] = (char) j;
  142. }
  143. else
  144. {
  145. GNUNET_FRAGMENT_context_destroy (frag,
  146. &msg_delay,
  147. &ack_delay);
  148. frag = NULL;
  149. }
  150. if (i == NUM_MSGS)
  151. return;
  152. #if DETAILS
  153. fprintf (stderr, "%s", "T! "); /* sending message */
  154. #endif
  155. msg = (struct GNUNET_MessageHeader *) buf;
  156. msg->type = htons ((uint16_t) i);
  157. msg->size =
  158. htons (sizeof(struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
  159. frag = GNUNET_FRAGMENT_context_create (NULL /* no stats */,
  160. MTU, &trackers[i],
  161. msg_delay,
  162. ack_delay,
  163. msg,
  164. &proc_frac, &frag);
  165. i++;
  166. }
  167. /**
  168. * Process ACK (by passing to fragmenter)
  169. */
  170. static void
  171. proc_acks (void *cls,
  172. uint32_t msg_id,
  173. const struct GNUNET_MessageHeader *hdr)
  174. {
  175. unsigned int i;
  176. int ret;
  177. if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
  178. {
  179. ack_drops++;
  180. #if DETAILS
  181. fprintf (stderr, "%s", "DA "); /* dropped ACK */
  182. #endif
  183. return; /* random drop */
  184. }
  185. for (i = 0; i < NUM_MSGS; i++)
  186. {
  187. if (NULL == frag)
  188. continue;
  189. ret = GNUNET_FRAGMENT_process_ack (frag, hdr);
  190. if (ret == GNUNET_OK)
  191. {
  192. #if DETAILS
  193. fprintf (stderr, "%s", "GA "); /* good ACK */
  194. #endif
  195. next_transmission ();
  196. acks++;
  197. return;
  198. }
  199. if (ret == GNUNET_NO)
  200. {
  201. #if DETAILS
  202. fprintf (stderr, "%s", "AA "); /* duplicate ACK */
  203. #endif
  204. acks++;
  205. return;
  206. }
  207. }
  208. #if DETAILS
  209. fprintf (stderr, "%s", "?A "); /* BAD: ack that nobody feels responsible for... */
  210. #endif
  211. }
  212. /**
  213. * Main function run with scheduler.
  214. */
  215. static void
  216. run (void *cls,
  217. char *const *args,
  218. const char *cfgfile,
  219. const struct GNUNET_CONFIGURATION_Handle *cfg)
  220. {
  221. defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU,
  222. 3,
  223. NULL,
  224. &proc_msgs,
  225. &proc_acks);
  226. next_transmission ();
  227. }
  228. int
  229. main (int argc, char *argv[])
  230. {
  231. struct GNUNET_GETOPT_CommandLineOption options[] = {
  232. GNUNET_GETOPT_OPTION_END
  233. };
  234. char *const argv_prog[] = {
  235. "test-fragmentation",
  236. "-c",
  237. "test_fragmentation_data.conf",
  238. "-L",
  239. "WARNING",
  240. NULL
  241. };
  242. unsigned int i;
  243. msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
  244. ack_delay = GNUNET_TIME_UNIT_SECONDS;
  245. GNUNET_log_setup ("test-fragmentation",
  246. "WARNING",
  247. NULL);
  248. for (i = 0; i < NUM_MSGS; i++)
  249. GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
  250. GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
  251. 100);
  252. GNUNET_PROGRAM_run (5,
  253. argv_prog,
  254. "test-fragmentation", "nohelp",
  255. options,
  256. &run, NULL);
  257. fprintf (stderr,
  258. "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
  259. fragc,
  260. dups,
  261. acks,
  262. ack_drops);
  263. return ret;
  264. }