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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, \
39 * How frequently do we execute the PUTs?
41 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
46 * Information we keep for each GET operation.
53 struct GetOperation *next;
58 struct GetOperation *prev;
61 * Handle for the operation.
63 struct GNUNET_DHT_GetHandle *get;
75 static struct GNUNET_SCHEDULER_Task *put_task;
80 static struct GNUNET_SCHEDULER_Task *get_task;
83 * Task to time out / regular shutdown.
85 static struct GNUNET_SCHEDULER_Task *timeout_task;
88 * Head of list of active GET operations.
90 static struct GetOperation *get_head;
93 * Tail of list of active GET operations.
95 static struct GetOperation *get_tail;
98 * Array of the testbed's peers.
100 static struct GNUNET_TESTBED_Peer **my_peers;
103 * Number of peers to run.
105 static unsigned int NUM_PEERS;
109 * Statistics we print out.
113 const char *subsystem;
115 unsigned long long total;
117 { "core", "# bytes decrypted", 0 },
118 { "core", "# bytes encrypted", 0 },
119 { "core", "# type maps received", 0 },
120 { "core", "# session keys confirmed via PONG", 0 },
121 { "core", "# peers connected", 0 },
122 { "core", "# key exchanges initiated", 0 },
123 { "core", "# send requests dropped (disconnected)", 0 },
124 { "core", "# transmissions delayed due to corking", 0 },
125 { "core", "# messages discarded (expired prior to transmission)", 0 },
126 { "core", "# messages discarded (disconnected)", 0 },
127 { "core", "# discarded CORE_SEND requests", 0 },
128 { "core", "# discarded lower priority CORE_SEND requests", 0 },
129 { "transport", "# bytes received via TCP", 0 },
130 { "transport", "# bytes transmitted via TCP", 0 },
131 { "dht", "# PUT messages queued for transmission", 0 },
132 { "dht", "# P2P PUT requests received", 0 },
133 { "dht", "# GET messages queued for transmission", 0 },
134 { "dht", "# P2P GET requests received", 0 },
135 { "dht", "# RESULT messages queued for transmission", 0 },
136 { "dht", "# P2P RESULTS received", 0 },
137 { "dht", "# Queued messages discarded (peer disconnected)", 0 },
138 { "dht", "# Peers excluded from routing due to Bloomfilter", 0 },
139 { "dht", "# Peer selection failed", 0 },
140 { "dht", "# FIND PEER requests ignored due to Bloomfilter", 0 },
141 { "dht", "# FIND PEER requests ignored due to lack of HELLO", 0 },
142 { "dht", "# P2P FIND PEER requests processed", 0 },
143 { "dht", "# P2P GET requests ONLY routed", 0 },
144 { "dht", "# Preference updates given to core", 0 },
145 { "dht", "# REPLIES ignored for CLIENTS (no match)", 0 },
146 { "dht", "# GET requests from clients injected", 0 },
147 { "dht", "# GET requests received from clients", 0 },
148 { "dht", "# GET STOP requests received from clients", 0 },
149 { "dht", "# ITEMS stored in datacache", 0 },
150 { "dht", "# Good RESULTS found in datacache", 0 },
151 { "dht", "# GET requests given to datacache", 0 },
156 static struct GNUNET_DHT_TEST_Context *
159 struct GetOperation *get_op;
160 struct GNUNET_DHT_TEST_Context *ctx = NULL;
162 if (NULL != timeout_task)
164 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
167 if (NULL != put_task)
169 GNUNET_SCHEDULER_cancel (put_task);
172 if (NULL != get_task)
174 GNUNET_SCHEDULER_cancel (get_task);
177 while (NULL != (get_op = get_tail))
179 GNUNET_DHT_get_stop (get_op->get);
180 GNUNET_CONTAINER_DLL_remove (get_head,
183 GNUNET_free (get_op);
190 * Function called once we're done processing stats.
192 * @param cls the test context
193 * @param op the stats operation
194 * @param emsg error message on failure
197 stats_finished (void *cls,
198 struct GNUNET_TESTBED_Operation *op,
201 struct GNUNET_DHT_TEST_Context *ctx = cls;
205 GNUNET_TESTBED_operation_done (op);
209 _ ("Gathering statistics failed: %s\n"),
211 GNUNET_SCHEDULER_cancel (put_task);
212 GNUNET_DHT_TEST_cleanup (ctx);
215 for (i = 0; NULL != stats[i].name; i++)
217 "%6s/%60s = %12llu\n",
221 GNUNET_DHT_TEST_cleanup (ctx);
222 GNUNET_SCHEDULER_shutdown ();
227 * Function called to process statistic values from all peers.
230 * @param peer the peer the statistic belong to
231 * @param subsystem name of subsystem that created the statistic
232 * @param name the name of the datum
233 * @param value the current value
234 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
235 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
238 handle_stats (void *cls,
239 const struct GNUNET_TESTBED_Peer *peer,
240 const char *subsystem,
247 for (i = 0; NULL != stats[i].name; i++)
248 if ((0 == strcasecmp (subsystem,
249 stats[i].subsystem)) &&
250 (0 == strcasecmp (name,
252 stats[i].total += value;
258 * Task run on shutdown to clean up. Terminates active get operations
259 * and shuts down the testbed.
261 * @param cls the 'struct GNUNET_DHT_TestContext'
264 shutdown_task (void *cls)
271 * Task run on timeout to clean up. Terminates active get operations
272 * and shuts down the testbed.
274 * @param cls the `struct GNUNET_DHT_TestContext`
277 timeout_cb (void *cls)
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 GNUNET_SCHEDULER_shutdown ();
287 * Iterator called on each result obtained for a DHT
288 * operation that expects a reply
290 * @param cls closure with our 'struct GetOperation'
291 * @param exp when will this value expire
292 * @param key key of the result
293 * @param get_path peers on reply path (or NULL if not recorded)
294 * @param get_path_length number of entries in @a get_path
295 * @param put_path peers on the PUT path (or NULL if not recorded)
296 * @param put_path_length number of entries in @a put_path
297 * @param type type of the result
298 * @param size number of bytes in @a data
299 * @param data pointer to the result data
302 dht_get_handler (void *cls,
303 struct GNUNET_TIME_Absolute exp,
304 const struct GNUNET_HashCode *key,
305 const struct GNUNET_PeerIdentity *get_path,
306 unsigned int get_path_length,
307 const struct GNUNET_PeerIdentity *put_path,
308 unsigned int put_path_length,
309 enum GNUNET_BLOCK_Type type,
313 struct GetOperation *get_op = cls;
314 struct GNUNET_HashCode want;
315 struct GNUNET_DHT_TEST_Context *ctx;
317 if (sizeof(struct GNUNET_HashCode) != size)
322 GNUNET_CRYPTO_hash (key,
325 if (0 != memcmp (&want,
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "PATH: (get %u, put %u)\n",
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 for (int i = get_path_length - 1; i >= 0; i--)
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 GNUNET_i2s (&get_path[i]));
346 for (int i = put_path_length - 1; i >= 0; i--)
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 GNUNET_i2s (&put_path[i]));
352 GNUNET_DHT_get_stop (get_op->get);
353 GNUNET_CONTAINER_DLL_remove (get_head,
356 GNUNET_free (get_op);
357 if (NULL != get_head)
359 /* all DHT GET operations successful; get stats! */
360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361 "All DHT operations successful. Obtaining stats!\n");
364 GNUNET_assert (NULL != ctx);
365 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
375 * Task to put the id of each peer into the DHT.
377 * @param cls array with NUM_PEERS DHT handles
378 * @param tc Task context
383 struct GNUNET_DHT_Handle **hs = cls;
384 struct GNUNET_HashCode key;
385 struct GNUNET_HashCode value;
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Putting values into DHT\n");
390 for (unsigned int i = 0; i < NUM_PEERS; i++)
392 GNUNET_CRYPTO_hash (&i,
395 GNUNET_CRYPTO_hash (&key,
398 GNUNET_DHT_put (hs[i],
401 GNUNET_DHT_RO_RECORD_ROUTE
402 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
403 GNUNET_BLOCK_TYPE_TEST,
406 GNUNET_TIME_UNIT_FOREVER_ABS,
410 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
417 * Start GET operations.
420 start_get (void *cls)
422 struct GNUNET_DHT_Handle **dhts = cls;
425 struct GNUNET_HashCode key;
426 struct GetOperation *get_op;
429 for (i = 0; i < NUM_PEERS; i++)
431 GNUNET_CRYPTO_hash (&i, sizeof(i), &key);
432 for (j = 0; j < NUM_PEERS; j++)
434 get_op = GNUNET_new (struct GetOperation);
435 GNUNET_CONTAINER_DLL_insert (get_head,
438 get_op->get = GNUNET_DHT_get_start (dhts[j],
439 GNUNET_BLOCK_TYPE_TEST, /* type */
440 &key, /*key to search */
441 4U, /* replication level */
442 GNUNET_DHT_RO_RECORD_ROUTE
443 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
454 * Main function of the test.
456 * @param cls closure (NULL)
457 * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
458 * @param num_peers number of @a peers that are running
459 * @param peers array of peers
460 * @param dhts handle to each of the DHTs of the peers
464 struct GNUNET_DHT_TEST_Context *ctx,
465 unsigned int num_peers,
466 struct GNUNET_TESTBED_Peer **peers,
467 struct GNUNET_DHT_Handle **dhts)
469 GNUNET_assert (NUM_PEERS == num_peers);
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Peers setup, starting test\n");
473 put_task = GNUNET_SCHEDULER_add_now (&do_puts,
475 get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
478 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
481 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
490 main (int xargc, char *xargv[])
492 const char *cfg_filename;
493 const char *test_name;
495 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
497 cfg_filename = "test_dht_2dtorus.conf";
498 test_name = "test-dht-2dtorus";
501 else if (NULL != strstr (xargv[0], "test_dht_line"))
503 cfg_filename = "test_dht_line.conf";
504 test_name = "test-dht-line";
507 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
509 cfg_filename = "test_dht_line.conf";
510 test_name = "test-dht-twopeer";
513 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
515 cfg_filename = "test_dht_multipeer.conf";
516 test_name = "test-dht-multipeer";
524 GNUNET_DHT_TEST_run (test_name,
532 /* end of test_dht_topo.c */