test_transport_api_reliability.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2016 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 transport/test_transport_api_reliability.c
  18. * @brief base test case for transport implementations
  19. *
  20. * This test case serves ensures that messages are reliably sent between peers
  21. *
  22. * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
  23. * and ensures that all message were received.
  24. */
  25. #include "platform.h"
  26. #include "gnunet_transport_service.h"
  27. #include "gauger.h"
  28. #include "transport-testing.h"
  29. /**
  30. * Allow making the problem "bigger".
  31. */
  32. #define FACTOR 1
  33. /**
  34. * Total number of messages to send
  35. *
  36. * Note that this value must not significantly exceed
  37. * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
  38. * messages may be dropped even for a reliable transport.
  39. */
  40. #define TOTAL_MSGS (1024 * 3 * FACTOR)
  41. /**
  42. * Testcase timeout
  43. */
  44. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 * FACTOR)
  45. /**
  46. * If we are in an "xhdr" test, the factor by which we divide
  47. * #TOTAL_MSGS for a more sane test duration.
  48. */
  49. static unsigned int xhdr = 1;
  50. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  51. /**
  52. * Total amount of bytes sent
  53. */
  54. static unsigned long long total_bytes;
  55. /**
  56. * Time of start
  57. */
  58. static struct GNUNET_TIME_Absolute start_time;
  59. /**
  60. * No. of last message received
  61. */
  62. static unsigned int msg_recv;
  63. /**
  64. * Bitmap storing which messages were received
  65. */
  66. static char bitmap[TOTAL_MSGS / 8];
  67. /**
  68. * Get the desired message size for message number @a iter.
  69. */
  70. static size_t
  71. get_size (unsigned int iter)
  72. {
  73. size_t ret;
  74. ret = (iter * iter * iter);
  75. #ifndef LINUX
  76. /* FreeBSD/OSX etc. Unix DGRAMs do not work
  77. * with large messages */
  78. if (0 == strcmp ("unix", ccc->test_plugin))
  79. ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 1024);
  80. #endif
  81. ret = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
  82. return ret;
  83. }
  84. /**
  85. * Implementation of the callback for obtaining the
  86. * size of messages for transmission. Counts the total
  87. * number of bytes sent as a side-effect.
  88. *
  89. * @param cnt_down count down from `TOTAL_MSGS - 1`
  90. * @return message size of the message
  91. */
  92. static size_t
  93. get_size_cnt (unsigned int cnt_down)
  94. {
  95. size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
  96. total_bytes += ret;
  97. return ret;
  98. }
  99. /**
  100. * Sets a bit active in the bitmap.
  101. *
  102. * @param bitIdx which bit to set
  103. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  104. */
  105. static int
  106. set_bit (unsigned int bitIdx)
  107. {
  108. size_t arraySlot;
  109. unsigned int targetBit;
  110. if (bitIdx >= sizeof (bitmap) * 8)
  111. {
  112. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  113. "tried to set bit %u of %u(!?!?)\n",
  114. bitIdx,
  115. (unsigned int) sizeof (bitmap) * 8);
  116. return GNUNET_SYSERR;
  117. }
  118. arraySlot = bitIdx / 8;
  119. targetBit = (1L << (bitIdx % 8));
  120. bitmap[arraySlot] |= targetBit;
  121. return GNUNET_OK;
  122. }
  123. /**
  124. * Obtain a bit from bitmap.
  125. * @param map the bitmap
  126. * @param bit index from bitmap
  127. *
  128. * @return Bit @a bit from @a map
  129. */
  130. static int
  131. get_bit (const char *map,
  132. unsigned int bit)
  133. {
  134. if (bit > TOTAL_MSGS)
  135. {
  136. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  137. "get bit %u of %u(!?!?)\n",
  138. bit,
  139. (unsigned int) sizeof (bitmap) * 8);
  140. return 0;
  141. }
  142. return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
  143. }
  144. static void
  145. custom_shutdown (void *cls)
  146. {
  147. unsigned long long delta;
  148. unsigned long long rate;
  149. int ok;
  150. /* Calculcate statistics */
  151. delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
  152. if (0 == delta)
  153. delta = 1;
  154. rate = (1000LL* 1000ll * total_bytes) / (1024 * delta);
  155. FPRINTF (stderr,
  156. "\nThroughput was %llu KiBytes/s\n",
  157. rate);
  158. {
  159. char *value_name;
  160. GNUNET_asprintf (&value_name,
  161. "unreliable_%s",
  162. ccc->test_plugin);
  163. GAUGER ("TRANSPORT",
  164. value_name,
  165. (int) rate,
  166. "kb/s");
  167. GNUNET_free (value_name);
  168. }
  169. ok = 0;
  170. for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
  171. {
  172. if (get_bit (bitmap, i) == 0)
  173. {
  174. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  175. "Did not receive message %d\n",
  176. i);
  177. ok = -1;
  178. }
  179. }
  180. if (0 != ok)
  181. ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
  182. }
  183. static void
  184. notify_receive (void *cls,
  185. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  186. const struct GNUNET_PeerIdentity *sender,
  187. const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
  188. {
  189. static int n;
  190. unsigned int s;
  191. char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
  192. if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
  193. return;
  194. msg_recv = ntohl (hdr->num);
  195. s = get_size (ntohl (hdr->num));
  196. if (ntohs (hdr->header.size) != s)
  197. {
  198. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  199. "Expected message %u of size %u, got %u bytes of message %u\n",
  200. (uint32_t) ntohl (hdr->num),
  201. s,
  202. ntohs (hdr->header.size),
  203. (uint32_t) ntohl (hdr->num));
  204. ccc->global_ret = GNUNET_SYSERR;
  205. GNUNET_SCHEDULER_shutdown ();
  206. return;
  207. }
  208. memset (cbuf,
  209. ntohl (hdr->num),
  210. s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage));
  211. if (0 !=
  212. memcmp (cbuf,
  213. &hdr[1],
  214. s - sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)))
  215. {
  216. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  217. "Expected message %u with bits %u, but body did not match\n",
  218. (uint32_t) ntohl (hdr->num),
  219. (unsigned char) ntohl (hdr->num));
  220. ccc->global_ret = GNUNET_SYSERR;
  221. GNUNET_SCHEDULER_shutdown ();
  222. return;
  223. }
  224. #if VERBOSE
  225. if (0 == ntohl (hdr->num) % 5)
  226. {
  227. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  228. "Got message %u of size %u\n",
  229. (uint32_t) ntohl (hdr->num),
  230. ntohs (hdr->header.size));
  231. }
  232. #endif
  233. n++;
  234. if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
  235. {
  236. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  237. "Message id %u is bigger than maxmimum number of messages %u expected\n",
  238. (uint32_t) ntohl (hdr->num),
  239. TOTAL_MSGS / xhdr);
  240. }
  241. if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
  242. {
  243. FPRINTF (stderr, "%s", ".");
  244. }
  245. if (n == TOTAL_MSGS / xhdr)
  246. {
  247. /* end testcase with success */
  248. ccc->global_ret = GNUNET_OK;
  249. GNUNET_SCHEDULER_shutdown ();
  250. }
  251. }
  252. int
  253. main (int argc, char *argv[])
  254. {
  255. if (0 == strstr (argv[0], "xhdr"))
  256. xhdr = 30;
  257. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  258. .num_messages = TOTAL_MSGS / xhdr,
  259. .get_size_cb = &get_size_cnt
  260. };
  261. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  262. .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
  263. .connect_continuation_cls = &sc,
  264. .config_file = "test_transport_api_data.conf",
  265. .rec = &notify_receive,
  266. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  267. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  268. .shutdown_task = &custom_shutdown,
  269. .timeout = TIMEOUT,
  270. .global_ret = GNUNET_SYSERR
  271. };
  272. ccc = &my_ccc;
  273. sc.ccc = ccc;
  274. start_time = GNUNET_TIME_absolute_get ();
  275. if (GNUNET_OK !=
  276. GNUNET_TRANSPORT_TESTING_main (2,
  277. &GNUNET_TRANSPORT_TESTING_connect_check,
  278. ccc))
  279. return 1;
  280. return 0;
  281. }
  282. /* end of test_transport_api_reliability.c */