test_dht_topo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012, 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 dht/test_dht_topo.c
  18. * @author Christian Grothoff
  19. * @brief Test for the dht service: store and retrieve in various topologies.
  20. * Each peer stores a value from the DHT and then each peer tries to get each
  21. * value from each other peer.
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_dht_service.h"
  26. #include "dht_test_lib.h"
  27. /**
  28. * How long until we give up on fetching the data?
  29. */
  30. #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
  31. 120)
  32. /**
  33. * How frequently do we execute the PUTs?
  34. */
  35. #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
  36. 5)
  37. /**
  38. * Information we keep for each GET operation.
  39. */
  40. struct GetOperation
  41. {
  42. /**
  43. * DLL.
  44. */
  45. struct GetOperation *next;
  46. /**
  47. * DLL.
  48. */
  49. struct GetOperation *prev;
  50. /**
  51. * Handle for the operation.
  52. */
  53. struct GNUNET_DHT_GetHandle *get;
  54. };
  55. /**
  56. * Result of the test.
  57. */
  58. static int ok = 1;
  59. /**
  60. * Task to do DHT_puts
  61. */
  62. static struct GNUNET_SCHEDULER_Task *put_task;
  63. /**
  64. * Task to do DHT_gets
  65. */
  66. static struct GNUNET_SCHEDULER_Task *get_task;
  67. /**
  68. * Task to time out / regular shutdown.
  69. */
  70. static struct GNUNET_SCHEDULER_Task *timeout_task;
  71. /**
  72. * Head of list of active GET operations.
  73. */
  74. static struct GetOperation *get_head;
  75. /**
  76. * Tail of list of active GET operations.
  77. */
  78. static struct GetOperation *get_tail;
  79. /**
  80. * Array of the testbed's peers.
  81. */
  82. static struct GNUNET_TESTBED_Peer **my_peers;
  83. /**
  84. * Number of peers to run.
  85. */
  86. static unsigned int NUM_PEERS;
  87. /**
  88. * Statistics we print out.
  89. */
  90. static struct
  91. {
  92. const char *subsystem;
  93. const char *name;
  94. unsigned long long total;
  95. } stats[] = {
  96. { "core", "# bytes decrypted", 0 },
  97. { "core", "# bytes encrypted", 0 },
  98. { "core", "# type maps received", 0 },
  99. { "core", "# session keys confirmed via PONG", 0 },
  100. { "core", "# peers connected", 0 },
  101. { "core", "# key exchanges initiated", 0 },
  102. { "core", "# send requests dropped (disconnected)", 0 },
  103. { "core", "# transmissions delayed due to corking", 0 },
  104. { "core", "# messages discarded (expired prior to transmission)", 0 },
  105. { "core", "# messages discarded (disconnected)", 0 },
  106. { "core", "# discarded CORE_SEND requests", 0 },
  107. { "core", "# discarded lower priority CORE_SEND requests", 0 },
  108. { "transport", "# bytes received via TCP", 0 },
  109. { "transport", "# bytes transmitted via TCP", 0 },
  110. { "dht", "# PUT messages queued for transmission", 0 },
  111. { "dht", "# P2P PUT requests received", 0 },
  112. { "dht", "# GET messages queued for transmission", 0 },
  113. { "dht", "# P2P GET requests received", 0 },
  114. { "dht", "# RESULT messages queued for transmission", 0 },
  115. { "dht", "# P2P RESULTS received", 0 },
  116. { "dht", "# Queued messages discarded (peer disconnected)", 0 },
  117. { "dht", "# Peers excluded from routing due to Bloomfilter", 0 },
  118. { "dht", "# Peer selection failed", 0 },
  119. { "dht", "# FIND PEER requests ignored due to Bloomfilter", 0 },
  120. { "dht", "# FIND PEER requests ignored due to lack of HELLO", 0 },
  121. { "dht", "# P2P FIND PEER requests processed", 0 },
  122. { "dht", "# P2P GET requests ONLY routed", 0 },
  123. { "dht", "# Preference updates given to core", 0 },
  124. { "dht", "# REPLIES ignored for CLIENTS (no match)", 0 },
  125. { "dht", "# GET requests from clients injected", 0 },
  126. { "dht", "# GET requests received from clients", 0 },
  127. { "dht", "# GET STOP requests received from clients", 0 },
  128. { "dht", "# ITEMS stored in datacache", 0 },
  129. { "dht", "# Good RESULTS found in datacache", 0 },
  130. { "dht", "# GET requests given to datacache", 0 },
  131. { NULL, NULL, 0 }
  132. };
  133. static struct GNUNET_DHT_TEST_Context *
  134. stop_ops ()
  135. {
  136. struct GetOperation *get_op;
  137. struct GNUNET_DHT_TEST_Context *ctx = NULL;
  138. if (NULL != timeout_task)
  139. {
  140. ctx = GNUNET_SCHEDULER_cancel (timeout_task);
  141. timeout_task = NULL;
  142. }
  143. if (NULL != put_task)
  144. {
  145. GNUNET_SCHEDULER_cancel (put_task);
  146. put_task = NULL;
  147. }
  148. if (NULL != get_task)
  149. {
  150. GNUNET_SCHEDULER_cancel (get_task);
  151. get_task = NULL;
  152. }
  153. while (NULL != (get_op = get_tail))
  154. {
  155. GNUNET_DHT_get_stop (get_op->get);
  156. GNUNET_CONTAINER_DLL_remove (get_head,
  157. get_tail,
  158. get_op);
  159. GNUNET_free (get_op);
  160. }
  161. return ctx;
  162. }
  163. /**
  164. * Function called once we're done processing stats.
  165. *
  166. * @param cls the test context
  167. * @param op the stats operation
  168. * @param emsg error message on failure
  169. */
  170. static void
  171. stats_finished (void *cls,
  172. struct GNUNET_TESTBED_Operation *op,
  173. const char *emsg)
  174. {
  175. struct GNUNET_DHT_TEST_Context *ctx = cls;
  176. unsigned int i;
  177. if (NULL != op)
  178. GNUNET_TESTBED_operation_done (op);
  179. if (NULL != emsg)
  180. {
  181. fprintf (stderr,
  182. _ ("Gathering statistics failed: %s\n"),
  183. emsg);
  184. GNUNET_SCHEDULER_cancel (put_task);
  185. GNUNET_DHT_TEST_cleanup (ctx);
  186. return;
  187. }
  188. for (i = 0; NULL != stats[i].name; i++)
  189. fprintf (stderr,
  190. "%6s/%60s = %12llu\n",
  191. stats[i].subsystem,
  192. stats[i].name,
  193. stats[i].total);
  194. GNUNET_DHT_TEST_cleanup (ctx);
  195. GNUNET_SCHEDULER_shutdown ();
  196. }
  197. /**
  198. * Function called to process statistic values from all peers.
  199. *
  200. * @param cls closure
  201. * @param peer the peer the statistic belong to
  202. * @param subsystem name of subsystem that created the statistic
  203. * @param name the name of the datum
  204. * @param value the current value
  205. * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
  206. * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
  207. */
  208. static int
  209. handle_stats (void *cls,
  210. const struct GNUNET_TESTBED_Peer *peer,
  211. const char *subsystem,
  212. const char *name,
  213. uint64_t value,
  214. int is_persistent)
  215. {
  216. unsigned int i;
  217. for (i = 0; NULL != stats[i].name; i++)
  218. if ((0 == strcasecmp (subsystem,
  219. stats[i].subsystem)) &&
  220. (0 == strcasecmp (name,
  221. stats[i].name)))
  222. stats[i].total += value;
  223. return GNUNET_OK;
  224. }
  225. /**
  226. * Task run on shutdown to clean up. Terminates active get operations
  227. * and shuts down the testbed.
  228. *
  229. * @param cls the 'struct GNUNET_DHT_TestContext'
  230. */
  231. static void
  232. shutdown_task (void *cls)
  233. {
  234. (void) stop_ops ();
  235. }
  236. /**
  237. * Task run on timeout to clean up. Terminates active get operations
  238. * and shuts down the testbed.
  239. *
  240. * @param cls the `struct GNUNET_DHT_TestContext`
  241. */
  242. static void
  243. timeout_cb (void *cls)
  244. {
  245. timeout_task = NULL;
  246. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  247. "Timeout\n");
  248. GNUNET_SCHEDULER_shutdown ();
  249. }
  250. /**
  251. * Iterator called on each result obtained for a DHT
  252. * operation that expects a reply
  253. *
  254. * @param cls closure with our 'struct GetOperation'
  255. * @param exp when will this value expire
  256. * @param key key of the result
  257. * @param get_path peers on reply path (or NULL if not recorded)
  258. * @param get_path_length number of entries in @a get_path
  259. * @param put_path peers on the PUT path (or NULL if not recorded)
  260. * @param put_path_length number of entries in @a put_path
  261. * @param type type of the result
  262. * @param size number of bytes in @a data
  263. * @param data pointer to the result data
  264. */
  265. static void
  266. dht_get_handler (void *cls,
  267. struct GNUNET_TIME_Absolute exp,
  268. const struct GNUNET_HashCode *key,
  269. const struct GNUNET_PeerIdentity *get_path,
  270. unsigned int get_path_length,
  271. const struct GNUNET_PeerIdentity *put_path,
  272. unsigned int put_path_length,
  273. enum GNUNET_BLOCK_Type type,
  274. size_t size,
  275. const void *data)
  276. {
  277. struct GetOperation *get_op = cls;
  278. struct GNUNET_HashCode want;
  279. struct GNUNET_DHT_TEST_Context *ctx;
  280. if (sizeof(struct GNUNET_HashCode) != size)
  281. {
  282. GNUNET_break (0);
  283. return;
  284. }
  285. GNUNET_CRYPTO_hash (key,
  286. sizeof(*key),
  287. &want);
  288. if (0 != memcmp (&want,
  289. data,
  290. sizeof(want)))
  291. {
  292. GNUNET_break (0);
  293. return;
  294. }
  295. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  296. "Get successful\n");
  297. #if 0
  298. {
  299. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  300. "PATH: (get %u, put %u)\n",
  301. get_path_length,
  302. put_path_length);
  303. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  304. " LOCAL\n");
  305. for (int i = get_path_length - 1; i >= 0; i--)
  306. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  307. " %s\n",
  308. GNUNET_i2s (&get_path[i]));
  309. for (int i = put_path_length - 1; i >= 0; i--)
  310. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  311. " %s\n",
  312. GNUNET_i2s (&put_path[i]));
  313. }
  314. #endif
  315. GNUNET_DHT_get_stop (get_op->get);
  316. GNUNET_CONTAINER_DLL_remove (get_head,
  317. get_tail,
  318. get_op);
  319. GNUNET_free (get_op);
  320. if (NULL != get_head)
  321. return;
  322. /* all DHT GET operations successful; get stats! */
  323. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  324. "All DHT operations successful. Obtaining stats!\n");
  325. ok = 0;
  326. ctx = stop_ops ();
  327. GNUNET_assert (NULL != ctx);
  328. (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
  329. my_peers,
  330. NULL, NULL,
  331. &handle_stats,
  332. &stats_finished,
  333. ctx);
  334. }
  335. /**
  336. * Task to put the id of each peer into the DHT.
  337. *
  338. * @param cls array with NUM_PEERS DHT handles
  339. * @param tc Task context
  340. */
  341. static void
  342. do_puts (void *cls)
  343. {
  344. struct GNUNET_DHT_Handle **hs = cls;
  345. struct GNUNET_HashCode key;
  346. struct GNUNET_HashCode value;
  347. put_task = NULL;
  348. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  349. "Putting values into DHT\n");
  350. for (unsigned int i = 0; i < NUM_PEERS; i++)
  351. {
  352. GNUNET_CRYPTO_hash (&i,
  353. sizeof(i),
  354. &key);
  355. GNUNET_CRYPTO_hash (&key,
  356. sizeof(key),
  357. &value);
  358. GNUNET_DHT_put (hs[i],
  359. &key,
  360. 10U,
  361. GNUNET_DHT_RO_RECORD_ROUTE
  362. | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
  363. GNUNET_BLOCK_TYPE_TEST,
  364. sizeof(value),
  365. &value,
  366. GNUNET_TIME_UNIT_FOREVER_ABS,
  367. NULL,
  368. NULL);
  369. }
  370. put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
  371. &do_puts,
  372. hs);
  373. }
  374. /**
  375. * Start GET operations.
  376. */
  377. static void
  378. start_get (void *cls)
  379. {
  380. struct GNUNET_DHT_Handle **dhts = cls;
  381. unsigned int i;
  382. unsigned int j;
  383. struct GNUNET_HashCode key;
  384. struct GetOperation *get_op;
  385. get_task = NULL;
  386. for (i = 0; i < NUM_PEERS; i++)
  387. {
  388. GNUNET_CRYPTO_hash (&i, sizeof(i), &key);
  389. for (j = 0; j < NUM_PEERS; j++)
  390. {
  391. get_op = GNUNET_new (struct GetOperation);
  392. GNUNET_CONTAINER_DLL_insert (get_head,
  393. get_tail,
  394. get_op);
  395. get_op->get = GNUNET_DHT_get_start (dhts[j],
  396. GNUNET_BLOCK_TYPE_TEST, /* type */
  397. &key, /*key to search */
  398. 4U, /* replication level */
  399. GNUNET_DHT_RO_RECORD_ROUTE
  400. | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
  401. NULL, /* xquery */
  402. 0, /* xquery bits */
  403. &dht_get_handler,
  404. get_op);
  405. }
  406. }
  407. }
  408. /**
  409. * Main function of the test.
  410. *
  411. * @param cls closure (NULL)
  412. * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
  413. * @param num_peers number of @a peers that are running
  414. * @param peers array of peers
  415. * @param dhts handle to each of the DHTs of the peers
  416. */
  417. static void
  418. run (void *cls,
  419. struct GNUNET_DHT_TEST_Context *ctx,
  420. unsigned int num_peers,
  421. struct GNUNET_TESTBED_Peer **peers,
  422. struct GNUNET_DHT_Handle **dhts)
  423. {
  424. GNUNET_assert (NUM_PEERS == num_peers);
  425. my_peers = peers;
  426. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  427. "Peers setup, starting test\n");
  428. put_task = GNUNET_SCHEDULER_add_now (&do_puts,
  429. dhts);
  430. get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  431. &start_get,
  432. dhts);
  433. timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
  434. &timeout_cb,
  435. ctx);
  436. GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
  437. ctx);
  438. }
  439. /**
  440. * Main: start test
  441. */
  442. int
  443. main (int xargc, char *xargv[])
  444. {
  445. const char *cfg_filename;
  446. const char *test_name;
  447. if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
  448. {
  449. cfg_filename = "test_dht_2dtorus.conf";
  450. test_name = "test-dht-2dtorus";
  451. NUM_PEERS = 16;
  452. }
  453. else if (NULL != strstr (xargv[0], "test_dht_line"))
  454. {
  455. cfg_filename = "test_dht_line.conf";
  456. test_name = "test-dht-line";
  457. NUM_PEERS = 5;
  458. }
  459. else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
  460. {
  461. cfg_filename = "test_dht_line.conf";
  462. test_name = "test-dht-twopeer";
  463. NUM_PEERS = 2;
  464. }
  465. else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
  466. {
  467. cfg_filename = "test_dht_multipeer.conf";
  468. test_name = "test-dht-multipeer";
  469. NUM_PEERS = 10;
  470. }
  471. else
  472. {
  473. GNUNET_break (0);
  474. return 1;
  475. }
  476. GNUNET_DHT_TEST_run (test_name,
  477. cfg_filename,
  478. NUM_PEERS,
  479. &run, NULL);
  480. return ok;
  481. }
  482. /* end of test_dht_topo.c */