2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file dht/test_dht_topo.c
22 * @author Christian Grothoff
23 * @brief Test for the dht service: store and retrieve in various topologies.
24 * Each peer stores a value from the DHT and then each peer tries to get each
25 * value from each other peer.
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "dht_test_lib.h"
33 * How long until we give up on fetching the data?
35 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
38 * How frequently do we execute the PUTs?
40 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
44 * Information we keep for each GET operation.
51 struct GetOperation *next;
56 struct GetOperation *prev;
59 * Handle for the operation.
61 struct GNUNET_DHT_GetHandle *get;
74 static struct GNUNET_SCHEDULER_Task *put_task;
79 static struct GNUNET_SCHEDULER_Task *get_task;
82 * Task to time out / regular shutdown.
84 static struct GNUNET_SCHEDULER_Task *timeout_task;
87 * Head of list of active GET operations.
89 static struct GetOperation *get_head;
92 * Tail of list of active GET operations.
94 static struct GetOperation *get_tail;
97 * Array of the testbed's peers.
99 static struct GNUNET_TESTBED_Peer **my_peers;
102 * Number of peers to run.
104 static unsigned int NUM_PEERS;
108 * Statistics we print out.
112 const char *subsystem;
114 unsigned long long total;
116 {"core", "# bytes decrypted", 0},
117 {"core", "# bytes encrypted", 0},
118 {"core", "# type maps received", 0},
119 {"core", "# session keys confirmed via PONG", 0},
120 {"core", "# peers connected", 0},
121 {"core", "# key exchanges initiated", 0},
122 {"core", "# send requests dropped (disconnected)", 0},
123 {"core", "# transmissions delayed due to corking", 0},
124 {"core", "# messages discarded (expired prior to transmission)", 0},
125 {"core", "# messages discarded (disconnected)", 0},
126 {"core", "# discarded CORE_SEND requests", 0},
127 {"core", "# discarded lower priority CORE_SEND requests", 0},
128 {"transport", "# bytes received via TCP", 0},
129 {"transport", "# bytes transmitted via TCP", 0},
130 {"dht", "# PUT messages queued for transmission", 0},
131 {"dht", "# P2P PUT requests received", 0},
132 {"dht", "# GET messages queued for transmission", 0},
133 {"dht", "# P2P GET requests received", 0},
134 {"dht", "# RESULT messages queued for transmission", 0},
135 {"dht", "# P2P RESULTS received", 0},
136 {"dht", "# Queued messages discarded (peer disconnected)", 0},
137 {"dht", "# Peers excluded from routing due to Bloomfilter", 0},
138 {"dht", "# Peer selection failed", 0},
139 {"dht", "# FIND PEER requests ignored due to Bloomfilter", 0},
140 {"dht", "# FIND PEER requests ignored due to lack of HELLO", 0},
141 {"dht", "# P2P FIND PEER requests processed", 0},
142 {"dht", "# P2P GET requests ONLY routed", 0},
143 {"dht", "# Preference updates given to core", 0},
144 {"dht", "# REPLIES ignored for CLIENTS (no match)", 0},
145 {"dht", "# GET requests from clients injected", 0},
146 {"dht", "# GET requests received from clients", 0},
147 {"dht", "# GET STOP requests received from clients", 0},
148 {"dht", "# ITEMS stored in datacache", 0},
149 {"dht", "# Good RESULTS found in datacache", 0},
150 {"dht", "# GET requests given to datacache", 0},
155 static struct GNUNET_DHT_TEST_Context *
158 struct GetOperation *get_op;
159 struct GNUNET_DHT_TEST_Context *ctx = NULL;
161 if (NULL != timeout_task)
163 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
166 if (NULL != put_task)
168 GNUNET_SCHEDULER_cancel (put_task);
171 if (NULL != get_task)
173 GNUNET_SCHEDULER_cancel (get_task);
176 while (NULL != (get_op = get_tail))
178 GNUNET_DHT_get_stop (get_op->get);
179 GNUNET_CONTAINER_DLL_remove (get_head,
182 GNUNET_free (get_op);
189 * Function called once we're done processing stats.
191 * @param cls the test context
192 * @param op the stats operation
193 * @param emsg error message on failure
196 stats_finished (void *cls,
197 struct GNUNET_TESTBED_Operation *op,
200 struct GNUNET_DHT_TEST_Context *ctx = cls;
204 GNUNET_TESTBED_operation_done (op);
208 _("Gathering statistics failed: %s\n"),
210 GNUNET_SCHEDULER_cancel (put_task);
211 GNUNET_DHT_TEST_cleanup (ctx);
214 for (i = 0; NULL != stats[i].name; i++)
216 "%6s/%60s = %12llu\n",
220 GNUNET_DHT_TEST_cleanup (ctx);
221 GNUNET_SCHEDULER_shutdown ();
226 * Function called to process statistic values from all peers.
229 * @param peer the peer the statistic belong to
230 * @param subsystem name of subsystem that created the statistic
231 * @param name the name of the datum
232 * @param value the current value
233 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
234 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
237 handle_stats (void *cls,
238 const struct GNUNET_TESTBED_Peer *peer,
239 const char *subsystem,
246 for (i = 0; NULL != stats[i].name; i++)
247 if ( (0 == strcasecmp (subsystem,
248 stats[i].subsystem)) &&
249 (0 == strcasecmp (name,
251 stats[i].total += value;
257 * Task run on shutdown to clean up. Terminates active get operations
258 * and shuts down the testbed.
260 * @param cls the 'struct GNUNET_DHT_TestContext'
263 shutdown_task (void *cls)
270 * Task run on timeout to clean up. Terminates active get operations
271 * and shuts down the testbed.
273 * @param cls the `struct GNUNET_DHT_TestContext`
276 timeout_cb (void *cls)
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281 GNUNET_SCHEDULER_shutdown ();
286 * Iterator called on each result obtained for a DHT
287 * operation that expects a reply
289 * @param cls closure with our 'struct GetOperation'
290 * @param exp when will this value expire
291 * @param key key of the result
292 * @param get_path peers on reply path (or NULL if not recorded)
293 * @param get_path_length number of entries in @a get_path
294 * @param put_path peers on the PUT path (or NULL if not recorded)
295 * @param put_path_length number of entries in @a put_path
296 * @param type type of the result
297 * @param size number of bytes in @a data
298 * @param data pointer to the result data
301 dht_get_handler (void *cls,
302 struct GNUNET_TIME_Absolute exp,
303 const struct GNUNET_HashCode *key,
304 const struct GNUNET_PeerIdentity *get_path,
305 unsigned int get_path_length,
306 const struct GNUNET_PeerIdentity *put_path,
307 unsigned int put_path_length,
308 enum GNUNET_BLOCK_Type type,
312 struct GetOperation *get_op = cls;
313 struct GNUNET_HashCode want;
314 struct GNUNET_DHT_TEST_Context *ctx;
316 if (sizeof (struct GNUNET_HashCode) != size)
321 GNUNET_CRYPTO_hash (key,
324 if (0 != memcmp (&want,
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "PATH: (get %u, put %u)\n",
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 for (i = get_path_length - 1; i >= 0; i--)
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 GNUNET_i2s (&get_path[i]));
347 for (i = put_path_length - 1; i >= 0; i--)
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 GNUNET_i2s (&put_path[i]));
353 GNUNET_DHT_get_stop (get_op->get);
354 GNUNET_CONTAINER_DLL_remove (get_head,
357 GNUNET_free (get_op);
358 if (NULL != get_head)
360 /* all DHT GET operations successful; get stats! */
361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
362 "All DHT operations successful. Obtaining stats!\n");
365 GNUNET_assert (NULL != ctx);
366 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
376 * Task to put the id of each peer into the DHT.
378 * @param cls array with NUM_PEERS DHT handles
379 * @param tc Task context
384 struct GNUNET_DHT_Handle **hs = cls;
385 struct GNUNET_HashCode key;
386 struct GNUNET_HashCode value;
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Putting values into DHT\n");
392 for (i = 0; i < NUM_PEERS; i++)
394 GNUNET_CRYPTO_hash (&i,
397 GNUNET_CRYPTO_hash (&key,
400 GNUNET_DHT_put (hs[i],
403 GNUNET_DHT_RO_RECORD_ROUTE |
404 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
405 GNUNET_BLOCK_TYPE_TEST,
408 GNUNET_TIME_UNIT_FOREVER_ABS,
412 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
419 * Start GET operations.
422 start_get (void *cls)
424 struct GNUNET_DHT_Handle **dhts = cls;
427 struct GNUNET_HashCode key;
428 struct GetOperation *get_op;
431 for (i=0;i<NUM_PEERS;i++)
433 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
434 for (j=0;j<NUM_PEERS;j++)
436 get_op = GNUNET_new (struct GetOperation);
437 GNUNET_CONTAINER_DLL_insert (get_head,
440 get_op->get = GNUNET_DHT_get_start (dhts[j],
441 GNUNET_BLOCK_TYPE_TEST, /* type */
442 &key, /*key to search */
443 4U, /* replication level */
444 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
455 * Main function of the test.
457 * @param cls closure (NULL)
458 * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
459 * @param num_peers number of @a peers that are running
460 * @param peers array of peers
461 * @param dhts handle to each of the DHTs of the peers
465 struct GNUNET_DHT_TEST_Context *ctx,
466 unsigned int num_peers,
467 struct GNUNET_TESTBED_Peer **peers,
468 struct GNUNET_DHT_Handle **dhts)
470 GNUNET_assert (NUM_PEERS == num_peers);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Peers setup, starting test\n");
474 put_task = GNUNET_SCHEDULER_add_now (&do_puts,
476 get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
479 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
482 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
491 main (int xargc, char *xargv[])
493 const char *cfg_filename;
494 const char *test_name;
496 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
498 cfg_filename = "test_dht_2dtorus.conf";
499 test_name = "test-dht-2dtorus";
502 else if (NULL != strstr (xargv[0], "test_dht_line"))
504 cfg_filename = "test_dht_line.conf";
505 test_name = "test-dht-line";
508 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
510 cfg_filename = "test_dht_line.conf";
511 test_name = "test-dht-twopeer";
514 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
516 cfg_filename = "test_dht_multipeer.conf";
517 test_name = "test-dht-multipeer";
525 GNUNET_DHT_TEST_run (test_name,
532 /* end of test_dht_topo.c */