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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file dht/test_dht_topo.c
17 * @author Christian Grothoff
18 * @brief Test for the dht service: store and retrieve in various topologies.
19 * Each peer stores a value from the DHT and then each peer tries to get each
20 * value from each other peer.
23 #include "gnunet_util_lib.h"
24 #include "gnunet_dht_service.h"
25 #include "dht_test_lib.h"
28 * How long until we give up on fetching the data?
30 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
33 * How frequently do we execute the PUTs?
35 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39 * Information we keep for each GET operation.
46 struct GetOperation *next;
51 struct GetOperation *prev;
54 * Handle for the operation.
56 struct GNUNET_DHT_GetHandle *get;
69 static struct GNUNET_SCHEDULER_Task *put_task;
74 static struct GNUNET_SCHEDULER_Task *get_task;
77 * Task to time out / regular shutdown.
79 static struct GNUNET_SCHEDULER_Task *timeout_task;
82 * Head of list of active GET operations.
84 static struct GetOperation *get_head;
87 * Tail of list of active GET operations.
89 static struct GetOperation *get_tail;
92 * Array of the testbed's peers.
94 static struct GNUNET_TESTBED_Peer **my_peers;
97 * Number of peers to run.
99 static unsigned int NUM_PEERS;
103 * Statistics we print out.
107 const char *subsystem;
109 unsigned long long total;
111 {"core", "# bytes decrypted", 0},
112 {"core", "# bytes encrypted", 0},
113 {"core", "# type maps received", 0},
114 {"core", "# session keys confirmed via PONG", 0},
115 {"core", "# peers connected", 0},
116 {"core", "# key exchanges initiated", 0},
117 {"core", "# send requests dropped (disconnected)", 0},
118 {"core", "# transmissions delayed due to corking", 0},
119 {"core", "# messages discarded (expired prior to transmission)", 0},
120 {"core", "# messages discarded (disconnected)", 0},
121 {"core", "# discarded CORE_SEND requests", 0},
122 {"core", "# discarded lower priority CORE_SEND requests", 0},
123 {"transport", "# bytes received via TCP", 0},
124 {"transport", "# bytes transmitted via TCP", 0},
125 {"dht", "# PUT messages queued for transmission", 0},
126 {"dht", "# P2P PUT requests received", 0},
127 {"dht", "# GET messages queued for transmission", 0},
128 {"dht", "# P2P GET requests received", 0},
129 {"dht", "# RESULT messages queued for transmission", 0},
130 {"dht", "# P2P RESULTS received", 0},
131 {"dht", "# Queued messages discarded (peer disconnected)", 0},
132 {"dht", "# Peers excluded from routing due to Bloomfilter", 0},
133 {"dht", "# Peer selection failed", 0},
134 {"dht", "# FIND PEER requests ignored due to Bloomfilter", 0},
135 {"dht", "# FIND PEER requests ignored due to lack of HELLO", 0},
136 {"dht", "# P2P FIND PEER requests processed", 0},
137 {"dht", "# P2P GET requests ONLY routed", 0},
138 {"dht", "# Preference updates given to core", 0},
139 {"dht", "# REPLIES ignored for CLIENTS (no match)", 0},
140 {"dht", "# GET requests from clients injected", 0},
141 {"dht", "# GET requests received from clients", 0},
142 {"dht", "# GET STOP requests received from clients", 0},
143 {"dht", "# ITEMS stored in datacache", 0},
144 {"dht", "# Good RESULTS found in datacache", 0},
145 {"dht", "# GET requests given to datacache", 0},
150 static struct GNUNET_DHT_TEST_Context *
153 struct GetOperation *get_op;
154 struct GNUNET_DHT_TEST_Context *ctx = NULL;
156 if (NULL != timeout_task)
158 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
161 if (NULL != put_task)
163 GNUNET_SCHEDULER_cancel (put_task);
166 if (NULL != get_task)
168 GNUNET_SCHEDULER_cancel (get_task);
171 while (NULL != (get_op = get_tail))
173 GNUNET_DHT_get_stop (get_op->get);
174 GNUNET_CONTAINER_DLL_remove (get_head,
177 GNUNET_free (get_op);
184 * Function called once we're done processing stats.
186 * @param cls the test context
187 * @param op the stats operation
188 * @param emsg error message on failure
191 stats_finished (void *cls,
192 struct GNUNET_TESTBED_Operation *op,
195 struct GNUNET_DHT_TEST_Context *ctx = cls;
199 GNUNET_TESTBED_operation_done (op);
203 _("Gathering statistics failed: %s\n"),
205 GNUNET_SCHEDULER_cancel (put_task);
206 GNUNET_DHT_TEST_cleanup (ctx);
209 for (i = 0; NULL != stats[i].name; i++)
211 "%6s/%60s = %12llu\n",
215 GNUNET_DHT_TEST_cleanup (ctx);
216 GNUNET_SCHEDULER_shutdown ();
221 * Function called to process statistic values from all peers.
224 * @param peer the peer the statistic belong to
225 * @param subsystem name of subsystem that created the statistic
226 * @param name the name of the datum
227 * @param value the current value
228 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
229 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
232 handle_stats (void *cls,
233 const struct GNUNET_TESTBED_Peer *peer,
234 const char *subsystem,
241 for (i = 0; NULL != stats[i].name; i++)
242 if ( (0 == strcasecmp (subsystem,
243 stats[i].subsystem)) &&
244 (0 == strcasecmp (name,
246 stats[i].total += value;
252 * Task run on shutdown to clean up. Terminates active get operations
253 * and shuts down the testbed.
255 * @param cls the 'struct GNUNET_DHT_TestContext'
258 shutdown_task (void *cls)
265 * Task run on timeout to clean up. Terminates active get operations
266 * and shuts down the testbed.
268 * @param cls the `struct GNUNET_DHT_TestContext`
271 timeout_cb (void *cls)
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 GNUNET_SCHEDULER_shutdown ();
281 * Iterator called on each result obtained for a DHT
282 * operation that expects a reply
284 * @param cls closure with our 'struct GetOperation'
285 * @param exp when will this value expire
286 * @param key key of the result
287 * @param get_path peers on reply path (or NULL if not recorded)
288 * @param get_path_length number of entries in @a get_path
289 * @param put_path peers on the PUT path (or NULL if not recorded)
290 * @param put_path_length number of entries in @a put_path
291 * @param type type of the result
292 * @param size number of bytes in @a data
293 * @param data pointer to the result data
296 dht_get_handler (void *cls,
297 struct GNUNET_TIME_Absolute exp,
298 const struct GNUNET_HashCode *key,
299 const struct GNUNET_PeerIdentity *get_path,
300 unsigned int get_path_length,
301 const struct GNUNET_PeerIdentity *put_path,
302 unsigned int put_path_length,
303 enum GNUNET_BLOCK_Type type,
307 struct GetOperation *get_op = cls;
308 struct GNUNET_HashCode want;
309 struct GNUNET_DHT_TEST_Context *ctx;
311 if (sizeof (struct GNUNET_HashCode) != size)
316 GNUNET_CRYPTO_hash (key,
319 if (0 != memcmp (&want,
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "PATH: (get %u, put %u)\n",
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 for (int i = get_path_length - 1; i >= 0; i--)
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 GNUNET_i2s (&get_path[i]));
340 for (int i = put_path_length - 1; i >= 0; i--)
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 GNUNET_i2s (&put_path[i]));
346 GNUNET_DHT_get_stop (get_op->get);
347 GNUNET_CONTAINER_DLL_remove (get_head,
350 GNUNET_free (get_op);
351 if (NULL != get_head)
353 /* all DHT GET operations successful; get stats! */
354 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
355 "All DHT operations successful. Obtaining stats!\n");
358 GNUNET_assert (NULL != ctx);
359 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
369 * Task to put the id of each peer into the DHT.
371 * @param cls array with NUM_PEERS DHT handles
372 * @param tc Task context
377 struct GNUNET_DHT_Handle **hs = cls;
378 struct GNUNET_HashCode key;
379 struct GNUNET_HashCode value;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Putting values into DHT\n");
384 for (unsigned int i = 0; i < NUM_PEERS; i++)
386 GNUNET_CRYPTO_hash (&i,
389 GNUNET_CRYPTO_hash (&key,
392 GNUNET_DHT_put (hs[i],
395 GNUNET_DHT_RO_RECORD_ROUTE |
396 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
397 GNUNET_BLOCK_TYPE_TEST,
400 GNUNET_TIME_UNIT_FOREVER_ABS,
404 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
411 * Start GET operations.
414 start_get (void *cls)
416 struct GNUNET_DHT_Handle **dhts = cls;
419 struct GNUNET_HashCode key;
420 struct GetOperation *get_op;
423 for (i=0;i<NUM_PEERS;i++)
425 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
426 for (j=0;j<NUM_PEERS;j++)
428 get_op = GNUNET_new (struct GetOperation);
429 GNUNET_CONTAINER_DLL_insert (get_head,
432 get_op->get = GNUNET_DHT_get_start (dhts[j],
433 GNUNET_BLOCK_TYPE_TEST, /* type */
434 &key, /*key to search */
435 4U, /* replication level */
436 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
447 * Main function of the test.
449 * @param cls closure (NULL)
450 * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
451 * @param num_peers number of @a peers that are running
452 * @param peers array of peers
453 * @param dhts handle to each of the DHTs of the peers
457 struct GNUNET_DHT_TEST_Context *ctx,
458 unsigned int num_peers,
459 struct GNUNET_TESTBED_Peer **peers,
460 struct GNUNET_DHT_Handle **dhts)
462 GNUNET_assert (NUM_PEERS == num_peers);
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Peers setup, starting test\n");
466 put_task = GNUNET_SCHEDULER_add_now (&do_puts,
468 get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
471 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
474 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
483 main (int xargc, char *xargv[])
485 const char *cfg_filename;
486 const char *test_name;
488 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
490 cfg_filename = "test_dht_2dtorus.conf";
491 test_name = "test-dht-2dtorus";
494 else if (NULL != strstr (xargv[0], "test_dht_line"))
496 cfg_filename = "test_dht_line.conf";
497 test_name = "test-dht-line";
500 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
502 cfg_filename = "test_dht_line.conf";
503 test_name = "test-dht-twopeer";
506 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
508 cfg_filename = "test_dht_multipeer.conf";
509 test_name = "test-dht-multipeer";
517 GNUNET_DHT_TEST_run (test_name,
524 /* end of test_dht_topo.c */