test_fragmentation.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. This file is part of GNUnet
  3. (C) 2004, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fragmentation/test_fragmentation.c
  19. * @brief test for fragmentation.c
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_fragmentation_lib.h"
  24. #define DETAILS GNUNET_NO
  25. /**
  26. * Number of messages to transmit (note: each uses ~32k memory!)
  27. */
  28. #define NUM_MSGS 500
  29. /**
  30. * MTU to force on fragmentation (must be > 1k + 12)
  31. */
  32. #define MTU 1111
  33. /**
  34. * Simulate dropping of 1 out of how many messages? (must be > 1)
  35. */
  36. #define DROPRATE 5
  37. static int ret = 1;
  38. static unsigned int dups;
  39. static unsigned int fragc;
  40. static unsigned int frag_drops;
  41. static unsigned int acks;
  42. static unsigned int ack_drops;
  43. static struct GNUNET_DEFRAGMENT_Context *defrag;
  44. static struct GNUNET_BANDWIDTH_Tracker trackers[NUM_MSGS];
  45. static struct GNUNET_FRAGMENT_Context *frags[NUM_MSGS];
  46. static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
  47. static void
  48. do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  49. {
  50. unsigned int i;
  51. ret = 0;
  52. shutdown_task = GNUNET_SCHEDULER_NO_TASK;
  53. GNUNET_DEFRAGMENT_context_destroy (defrag);
  54. defrag = NULL;
  55. for (i = 0; i < NUM_MSGS; i++)
  56. {
  57. if (frags[i] == NULL)
  58. continue;
  59. GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
  60. frags[i] = NULL;
  61. }
  62. }
  63. static void
  64. proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
  65. {
  66. static unsigned int total;
  67. unsigned int i;
  68. const char *buf;
  69. #if DETAILS
  70. FPRINTF (stderr, "%s", "!"); /* message complete, good! */
  71. #endif
  72. buf = (const char *) hdr;
  73. for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
  74. GNUNET_assert (buf[i] == (char) i);
  75. total++;
  76. #if ! DETAILS
  77. if (0 == (total % (NUM_MSGS / 100)))
  78. FPRINTF (stderr, "%s", ".");
  79. #endif
  80. /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
  81. if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
  82. {
  83. if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
  84. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  85. }
  86. }
  87. /**
  88. * Process ACK (by passing to fragmenter)
  89. */
  90. static void
  91. proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
  92. {
  93. unsigned int i;
  94. int ret;
  95. if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
  96. {
  97. ack_drops++;
  98. return; /* random drop */
  99. }
  100. for (i = 0; i < NUM_MSGS; i++)
  101. {
  102. if (frags[i] == NULL)
  103. continue;
  104. ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
  105. if (ret == GNUNET_OK)
  106. {
  107. #if DETAILS
  108. FPRINTF (stderr, "%s", "@"); /* good ACK */
  109. #endif
  110. GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
  111. frags[i] = NULL;
  112. acks++;
  113. return;
  114. }
  115. if (ret == GNUNET_NO)
  116. {
  117. #if DETAILS
  118. FPRINTF (stderr, "%s", "@"); /* good ACK */
  119. #endif
  120. acks++;
  121. return;
  122. }
  123. }
  124. #if DETAILS
  125. FPRINTF (stderr, "%s", "_"); /* BAD: ack that nobody feels responsible for... */
  126. #endif
  127. }
  128. /**
  129. * Process fragment (by passing to defrag).
  130. */
  131. static void
  132. proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
  133. {
  134. struct GNUNET_FRAGMENT_Context **fc = cls;
  135. int ret;
  136. GNUNET_FRAGMENT_context_transmission_done (*fc);
  137. if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
  138. {
  139. frag_drops++;
  140. return; /* random drop */
  141. }
  142. if (NULL == defrag)
  143. {
  144. FPRINTF (stderr, "%s", "E"); /* Error: frag after shutdown!? */
  145. return;
  146. }
  147. ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
  148. if (ret == GNUNET_NO)
  149. {
  150. #if DETAILS
  151. FPRINTF (stderr, "%s", "?"); /* duplicate fragment */
  152. #endif
  153. dups++;
  154. }
  155. else if (ret == GNUNET_OK)
  156. {
  157. #if DETAILS
  158. FPRINTF (stderr, "%s", "."); /* good fragment */
  159. #endif
  160. fragc++;
  161. }
  162. }
  163. /**
  164. * Main function run with scheduler.
  165. */
  166. static void
  167. run (void *cls, char *const *args, const char *cfgfile,
  168. const struct GNUNET_CONFIGURATION_Handle *cfg)
  169. {
  170. unsigned int i;
  171. struct GNUNET_MessageHeader *msg;
  172. char buf[MTU + 32 * 1024];
  173. defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
  174. , NULL, &proc_msgs, &proc_acks);
  175. for (i = 0; i < sizeof (buf); i++)
  176. buf[i] = (char) i;
  177. msg = (struct GNUNET_MessageHeader *) buf;
  178. for (i = 0; i < NUM_MSGS; i++)
  179. {
  180. msg->type = htons ((uint16_t) i);
  181. msg->size =
  182. htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
  183. frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
  184. MTU, &trackers[i],
  185. GNUNET_TIME_UNIT_MILLISECONDS,
  186. GNUNET_TIME_UNIT_SECONDS,
  187. msg,
  188. &proc_frac, &frags[i]);
  189. }
  190. }
  191. int
  192. main (int argc, char *argv[])
  193. {
  194. struct GNUNET_GETOPT_CommandLineOption options[] = {
  195. GNUNET_GETOPT_OPTION_END
  196. };
  197. char *const argv_prog[] = {
  198. "test-fragmentation",
  199. "-c",
  200. "test_fragmentation_data.conf",
  201. "-L",
  202. "WARNING",
  203. NULL
  204. };
  205. unsigned int i;
  206. GNUNET_log_setup ("test-fragmentation",
  207. "WARNING",
  208. NULL);
  209. for (i = 0; i < NUM_MSGS; i++)
  210. GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
  211. GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
  212. 100);
  213. GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
  214. &run, NULL);
  215. FPRINTF (stderr,
  216. "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
  217. fragc, dups, acks, ack_drops);
  218. return ret;
  219. }