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, 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,
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "PATH: (get %u, put %u)\n",
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 for (int i = get_path_length - 1; i >= 0; i--)
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 GNUNET_i2s (&get_path[i]));
345 for (int i = put_path_length - 1; i >= 0; i--)
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 GNUNET_i2s (&put_path[i]));
351 GNUNET_DHT_get_stop (get_op->get);
352 GNUNET_CONTAINER_DLL_remove (get_head,
355 GNUNET_free (get_op);
356 if (NULL != get_head)
358 /* all DHT GET operations successful; get stats! */
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
360 "All DHT operations successful. Obtaining stats!\n");
363 GNUNET_assert (NULL != ctx);
364 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
374 * Task to put the id of each peer into the DHT.
376 * @param cls array with NUM_PEERS DHT handles
377 * @param tc Task context
382 struct GNUNET_DHT_Handle **hs = cls;
383 struct GNUNET_HashCode key;
384 struct GNUNET_HashCode value;
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Putting values into DHT\n");
389 for (unsigned int i = 0; i < NUM_PEERS; i++)
391 GNUNET_CRYPTO_hash (&i,
394 GNUNET_CRYPTO_hash (&key,
397 GNUNET_DHT_put (hs[i],
400 GNUNET_DHT_RO_RECORD_ROUTE |
401 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
402 GNUNET_BLOCK_TYPE_TEST,
405 GNUNET_TIME_UNIT_FOREVER_ABS,
409 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
416 * Start GET operations.
419 start_get (void *cls)
421 struct GNUNET_DHT_Handle **dhts = cls;
424 struct GNUNET_HashCode key;
425 struct GetOperation *get_op;
428 for (i=0;i<NUM_PEERS;i++)
430 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
431 for (j=0;j<NUM_PEERS;j++)
433 get_op = GNUNET_new (struct GetOperation);
434 GNUNET_CONTAINER_DLL_insert (get_head,
437 get_op->get = GNUNET_DHT_get_start (dhts[j],
438 GNUNET_BLOCK_TYPE_TEST, /* type */
439 &key, /*key to search */
440 4U, /* replication level */
441 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
452 * Main function of the test.
454 * @param cls closure (NULL)
455 * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
456 * @param num_peers number of @a peers that are running
457 * @param peers array of peers
458 * @param dhts handle to each of the DHTs of the peers
462 struct GNUNET_DHT_TEST_Context *ctx,
463 unsigned int num_peers,
464 struct GNUNET_TESTBED_Peer **peers,
465 struct GNUNET_DHT_Handle **dhts)
467 GNUNET_assert (NUM_PEERS == num_peers);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Peers setup, starting test\n");
471 put_task = GNUNET_SCHEDULER_add_now (&do_puts,
473 get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
476 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
479 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
488 main (int xargc, char *xargv[])
490 const char *cfg_filename;
491 const char *test_name;
493 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
495 cfg_filename = "test_dht_2dtorus.conf";
496 test_name = "test-dht-2dtorus";
499 else if (NULL != strstr (xargv[0], "test_dht_line"))
501 cfg_filename = "test_dht_line.conf";
502 test_name = "test-dht-line";
505 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
507 cfg_filename = "test_dht_line.conf";
508 test_name = "test-dht-twopeer";
511 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
513 cfg_filename = "test_dht_multipeer.conf";
514 test_name = "test-dht-multipeer";
522 GNUNET_DHT_TEST_run (test_name,
529 /* end of test_dht_topo.c */