test_fragmentation_parallel.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 500
  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 5
  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 *frags[NUM_MSGS];
  45. static struct GNUNET_SCHEDULER_Task *shutdown_task;
  46. static void
  47. do_shutdown (void *cls)
  48. {
  49. unsigned int i;
  50. ret = 0;
  51. shutdown_task = NULL;
  52. GNUNET_DEFRAGMENT_context_destroy (defrag);
  53. defrag = NULL;
  54. for (i = 0; i < NUM_MSGS; i++)
  55. {
  56. if (frags[i] == NULL)
  57. continue;
  58. GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
  59. frags[i] = NULL;
  60. }
  61. }
  62. static void
  63. proc_msgs (void *cls, const struct GNUNET_MessageHeader *hdr)
  64. {
  65. static unsigned int total;
  66. unsigned int i;
  67. const char *buf;
  68. #if DETAILS
  69. FPRINTF (stderr, "%s", "!"); /* message complete, good! */
  70. #endif
  71. buf = (const char *) hdr;
  72. for (i = sizeof (struct GNUNET_MessageHeader); i < ntohs (hdr->size); i++)
  73. GNUNET_assert (buf[i] == (char) i);
  74. total++;
  75. #if ! DETAILS
  76. if (0 == (total % (NUM_MSGS / 100)))
  77. FPRINTF (stderr, "%s", ".");
  78. #endif
  79. /* tolerate 10% loss, i.e. due to duplicate fragment IDs */
  80. if ((total >= NUM_MSGS - (NUM_MSGS / 10)) && (ret != 0))
  81. {
  82. if (NULL == shutdown_task)
  83. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  84. }
  85. }
  86. /**
  87. * Process ACK (by passing to fragmenter)
  88. */
  89. static void
  90. proc_acks (void *cls, uint32_t msg_id, const struct GNUNET_MessageHeader *hdr)
  91. {
  92. unsigned int i;
  93. int ret;
  94. if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
  95. {
  96. ack_drops++;
  97. return; /* random drop */
  98. }
  99. for (i = 0; i < NUM_MSGS; i++)
  100. {
  101. if (frags[i] == NULL)
  102. continue;
  103. ret = GNUNET_FRAGMENT_process_ack (frags[i], hdr);
  104. if (ret == GNUNET_OK)
  105. {
  106. #if DETAILS
  107. FPRINTF (stderr, "%s", "@"); /* good ACK */
  108. #endif
  109. GNUNET_FRAGMENT_context_destroy (frags[i], NULL, NULL);
  110. frags[i] = NULL;
  111. acks++;
  112. return;
  113. }
  114. if (ret == GNUNET_NO)
  115. {
  116. #if DETAILS
  117. FPRINTF (stderr, "%s", "@"); /* good ACK */
  118. #endif
  119. acks++;
  120. return;
  121. }
  122. }
  123. #if DETAILS
  124. FPRINTF (stderr, "%s", "_"); /* BAD: ack that nobody feels responsible for... */
  125. #endif
  126. }
  127. /**
  128. * Process fragment (by passing to defrag).
  129. */
  130. static void
  131. proc_frac (void *cls, const struct GNUNET_MessageHeader *hdr)
  132. {
  133. struct GNUNET_FRAGMENT_Context **fc = cls;
  134. int ret;
  135. GNUNET_FRAGMENT_context_transmission_done (*fc);
  136. if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, DROPRATE))
  137. {
  138. frag_drops++;
  139. return; /* random drop */
  140. }
  141. if (NULL == defrag)
  142. {
  143. FPRINTF (stderr, "%s", "E"); /* Error: frag after shutdown!? */
  144. return;
  145. }
  146. ret = GNUNET_DEFRAGMENT_process_fragment (defrag, hdr);
  147. if (ret == GNUNET_NO)
  148. {
  149. #if DETAILS
  150. FPRINTF (stderr, "%s", "?"); /* duplicate fragment */
  151. #endif
  152. dups++;
  153. }
  154. else if (ret == GNUNET_OK)
  155. {
  156. #if DETAILS
  157. FPRINTF (stderr, "%s", "."); /* good fragment */
  158. #endif
  159. fragc++;
  160. }
  161. }
  162. /**
  163. * Main function run with scheduler.
  164. */
  165. static void
  166. run (void *cls, char *const *args, const char *cfgfile,
  167. const struct GNUNET_CONFIGURATION_Handle *cfg)
  168. {
  169. unsigned int i;
  170. struct GNUNET_MessageHeader *msg;
  171. char buf[MTU + 32 * 1024];
  172. defrag = GNUNET_DEFRAGMENT_context_create (NULL, MTU, NUM_MSGS /* enough space for all */
  173. , NULL, &proc_msgs, &proc_acks);
  174. for (i = 0; i < sizeof (buf); i++)
  175. buf[i] = (char) i;
  176. msg = (struct GNUNET_MessageHeader *) buf;
  177. for (i = 0; i < NUM_MSGS; i++)
  178. {
  179. msg->type = htons ((uint16_t) i);
  180. msg->size =
  181. htons (sizeof (struct GNUNET_MessageHeader) + (17 * i) % (32 * 1024));
  182. frags[i] = GNUNET_FRAGMENT_context_create (NULL /* no stats */ ,
  183. MTU, &trackers[i],
  184. GNUNET_TIME_UNIT_MILLISECONDS,
  185. GNUNET_TIME_UNIT_SECONDS,
  186. msg,
  187. &proc_frac, &frags[i]);
  188. }
  189. }
  190. int
  191. main (int argc, char *argv[])
  192. {
  193. struct GNUNET_GETOPT_CommandLineOption options[] = {
  194. GNUNET_GETOPT_OPTION_END
  195. };
  196. char *const argv_prog[] = {
  197. "test-fragmentation",
  198. "-c",
  199. "test_fragmentation_data.conf",
  200. "-L",
  201. "WARNING",
  202. NULL
  203. };
  204. unsigned int i;
  205. GNUNET_log_setup ("test-fragmentation",
  206. "WARNING",
  207. NULL);
  208. for (i = 0; i < NUM_MSGS; i++)
  209. GNUNET_BANDWIDTH_tracker_init (&trackers[i], NULL, NULL,
  210. GNUNET_BANDWIDTH_value_init ((i + 1) * 1024),
  211. 100);
  212. GNUNET_PROGRAM_run (5, argv_prog, "test-fragmentation", "nohelp", options,
  213. &run, NULL);
  214. FPRINTF (stderr,
  215. "\nHad %u good fragments, %u duplicate fragments, %u acks and %u simulated drops of acks\n",
  216. fragc, dups, acks, ack_drops);
  217. return ret;
  218. }