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.
50 struct GetOperation *next;
55 struct GetOperation *prev;
58 * Handle for the operation.
60 struct GNUNET_DHT_GetHandle *get;
72 static struct GNUNET_SCHEDULER_Task *put_task;
77 static struct GNUNET_SCHEDULER_Task *get_task;
80 * Task to time out / regular shutdown.
82 static struct GNUNET_SCHEDULER_Task *timeout_task;
85 * Head of list of active GET operations.
87 static struct GetOperation *get_head;
90 * Tail of list of active GET operations.
92 static struct GetOperation *get_tail;
95 * Array of the testbed's peers.
97 static struct GNUNET_TESTBED_Peer **my_peers;
100 * Number of peers to run.
102 static unsigned int NUM_PEERS;
106 * Statistics we print out.
109 const char *subsystem;
111 unsigned long long total;
113 { "core", "# bytes decrypted", 0 },
114 { "core", "# bytes encrypted", 0 },
115 { "core", "# type maps received", 0 },
116 { "core", "# session keys confirmed via PONG", 0 },
117 { "core", "# peers connected", 0 },
118 { "core", "# key exchanges initiated", 0 },
119 { "core", "# send requests dropped (disconnected)", 0 },
120 { "core", "# transmissions delayed due to corking", 0 },
121 { "core", "# messages discarded (expired prior to transmission)", 0 },
122 { "core", "# messages discarded (disconnected)", 0 },
123 { "core", "# discarded CORE_SEND requests", 0 },
124 { "core", "# discarded lower priority CORE_SEND requests", 0 },
125 { "transport", "# bytes received via TCP", 0 },
126 { "transport", "# bytes transmitted via TCP", 0 },
127 { "dht", "# PUT messages queued for transmission", 0 },
128 { "dht", "# P2P PUT requests received", 0 },
129 { "dht", "# GET messages queued for transmission", 0 },
130 { "dht", "# P2P GET requests received", 0 },
131 { "dht", "# RESULT messages queued for transmission", 0 },
132 { "dht", "# P2P RESULTS received", 0 },
133 { "dht", "# Queued messages discarded (peer disconnected)", 0 },
134 { "dht", "# Peers excluded from routing due to Bloomfilter", 0 },
135 { "dht", "# Peer selection failed", 0 },
136 { "dht", "# FIND PEER requests ignored due to Bloomfilter", 0 },
137 { "dht", "# FIND PEER requests ignored due to lack of HELLO", 0 },
138 { "dht", "# P2P FIND PEER requests processed", 0 },
139 { "dht", "# P2P GET requests ONLY routed", 0 },
140 { "dht", "# Preference updates given to core", 0 },
141 { "dht", "# REPLIES ignored for CLIENTS (no match)", 0 },
142 { "dht", "# GET requests from clients injected", 0 },
143 { "dht", "# GET requests received from clients", 0 },
144 { "dht", "# GET STOP requests received from clients", 0 },
145 { "dht", "# ITEMS stored in datacache", 0 },
146 { "dht", "# Good RESULTS found in datacache", 0 },
147 { "dht", "# GET requests given to datacache", 0 },
152 static struct GNUNET_DHT_TEST_Context *
155 struct GetOperation *get_op;
156 struct GNUNET_DHT_TEST_Context *ctx = NULL;
158 if (NULL != timeout_task)
160 ctx = GNUNET_SCHEDULER_cancel(timeout_task);
163 if (NULL != put_task)
165 GNUNET_SCHEDULER_cancel(put_task);
168 if (NULL != get_task)
170 GNUNET_SCHEDULER_cancel(get_task);
173 while (NULL != (get_op = get_tail))
175 GNUNET_DHT_get_stop(get_op->get);
176 GNUNET_CONTAINER_DLL_remove(get_head,
186 * Function called once we're done processing stats.
188 * @param cls the test context
189 * @param op the stats operation
190 * @param emsg error message on failure
193 stats_finished(void *cls,
194 struct GNUNET_TESTBED_Operation *op,
197 struct GNUNET_DHT_TEST_Context *ctx = cls;
201 GNUNET_TESTBED_operation_done(op);
205 _("Gathering statistics failed: %s\n"),
207 GNUNET_SCHEDULER_cancel(put_task);
208 GNUNET_DHT_TEST_cleanup(ctx);
211 for (i = 0; NULL != stats[i].name; i++)
213 "%6s/%60s = %12llu\n",
217 GNUNET_DHT_TEST_cleanup(ctx);
218 GNUNET_SCHEDULER_shutdown();
223 * Function called to process statistic values from all peers.
226 * @param peer the peer the statistic belong to
227 * @param subsystem name of subsystem that created the statistic
228 * @param name the name of the datum
229 * @param value the current value
230 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
231 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
234 handle_stats(void *cls,
235 const struct GNUNET_TESTBED_Peer *peer,
236 const char *subsystem,
243 for (i = 0; NULL != stats[i].name; i++)
244 if ((0 == strcasecmp(subsystem,
245 stats[i].subsystem)) &&
246 (0 == strcasecmp(name,
248 stats[i].total += value;
254 * Task run on shutdown to clean up. Terminates active get operations
255 * and shuts down the testbed.
257 * @param cls the 'struct GNUNET_DHT_TestContext'
260 shutdown_task(void *cls)
267 * Task run on timeout to clean up. Terminates active get operations
268 * and shuts down the testbed.
270 * @param cls the `struct GNUNET_DHT_TestContext`
273 timeout_cb(void *cls)
276 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
278 GNUNET_SCHEDULER_shutdown();
283 * Iterator called on each result obtained for a DHT
284 * operation that expects a reply
286 * @param cls closure with our 'struct GetOperation'
287 * @param exp when will this value expire
288 * @param key key of the result
289 * @param get_path peers on reply path (or NULL if not recorded)
290 * @param get_path_length number of entries in @a get_path
291 * @param put_path peers on the PUT path (or NULL if not recorded)
292 * @param put_path_length number of entries in @a put_path
293 * @param type type of the result
294 * @param size number of bytes in @a data
295 * @param data pointer to the result data
298 dht_get_handler(void *cls,
299 struct GNUNET_TIME_Absolute exp,
300 const struct GNUNET_HashCode *key,
301 const struct GNUNET_PeerIdentity *get_path,
302 unsigned int get_path_length,
303 const struct GNUNET_PeerIdentity *put_path,
304 unsigned int put_path_length,
305 enum GNUNET_BLOCK_Type type,
309 struct GetOperation *get_op = cls;
310 struct GNUNET_HashCode want;
311 struct GNUNET_DHT_TEST_Context *ctx;
313 if (sizeof(struct GNUNET_HashCode) != size)
318 GNUNET_CRYPTO_hash(key,
321 if (0 != memcmp(&want,
328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
332 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
333 "PATH: (get %u, put %u)\n",
336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
338 for (int i = get_path_length - 1; i >= 0; i--)
339 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
341 GNUNET_i2s(&get_path[i]));
342 for (int i = put_path_length - 1; i >= 0; i--)
343 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
345 GNUNET_i2s(&put_path[i]));
348 GNUNET_DHT_get_stop(get_op->get);
349 GNUNET_CONTAINER_DLL_remove(get_head,
353 if (NULL != get_head)
355 /* all DHT GET operations successful; get stats! */
356 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
357 "All DHT operations successful. Obtaining stats!\n");
360 GNUNET_assert(NULL != ctx);
361 (void)GNUNET_TESTBED_get_statistics(NUM_PEERS,
371 * Task to put the id of each peer into the DHT.
373 * @param cls array with NUM_PEERS DHT handles
374 * @param tc Task context
379 struct GNUNET_DHT_Handle **hs = cls;
380 struct GNUNET_HashCode key;
381 struct GNUNET_HashCode value;
384 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
385 "Putting values into DHT\n");
386 for (unsigned int i = 0; i < NUM_PEERS; i++)
388 GNUNET_CRYPTO_hash(&i,
391 GNUNET_CRYPTO_hash(&key,
394 GNUNET_DHT_put(hs[i],
397 GNUNET_DHT_RO_RECORD_ROUTE |
398 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
399 GNUNET_BLOCK_TYPE_TEST,
402 GNUNET_TIME_UNIT_FOREVER_ABS,
406 put_task = GNUNET_SCHEDULER_add_delayed(PUT_FREQUENCY,
413 * Start GET operations.
418 struct GNUNET_DHT_Handle **dhts = cls;
421 struct GNUNET_HashCode key;
422 struct GetOperation *get_op;
425 for (i = 0; i < NUM_PEERS; i++)
427 GNUNET_CRYPTO_hash(&i, sizeof(i), &key);
428 for (j = 0; j < NUM_PEERS; j++)
430 get_op = GNUNET_new(struct GetOperation);
431 GNUNET_CONTAINER_DLL_insert(get_head,
434 get_op->get = GNUNET_DHT_get_start(dhts[j],
435 GNUNET_BLOCK_TYPE_TEST, /* type */
436 &key, /*key to search */
437 4U, /* replication level */
438 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
449 * Main function of the test.
451 * @param cls closure (NULL)
452 * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
453 * @param num_peers number of @a peers that are running
454 * @param peers array of peers
455 * @param dhts handle to each of the DHTs of the peers
459 struct GNUNET_DHT_TEST_Context *ctx,
460 unsigned int num_peers,
461 struct GNUNET_TESTBED_Peer **peers,
462 struct GNUNET_DHT_Handle **dhts)
464 GNUNET_assert(NUM_PEERS == num_peers);
466 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
467 "Peers setup, starting test\n");
468 put_task = GNUNET_SCHEDULER_add_now(&do_puts,
470 get_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
473 timeout_task = GNUNET_SCHEDULER_add_delayed(GET_TIMEOUT,
476 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
485 main(int xargc, char *xargv[])
487 const char *cfg_filename;
488 const char *test_name;
490 if (NULL != strstr(xargv[0], "test_dht_2dtorus"))
492 cfg_filename = "test_dht_2dtorus.conf";
493 test_name = "test-dht-2dtorus";
496 else if (NULL != strstr(xargv[0], "test_dht_line"))
498 cfg_filename = "test_dht_line.conf";
499 test_name = "test-dht-line";
502 else if (NULL != strstr(xargv[0], "test_dht_twopeer"))
504 cfg_filename = "test_dht_line.conf";
505 test_name = "test-dht-twopeer";
508 else if (NULL != strstr(xargv[0], "test_dht_multipeer"))
510 cfg_filename = "test_dht_multipeer.conf";
511 test_name = "test-dht-multipeer";
519 GNUNET_DHT_TEST_run(test_name,
526 /* end of test_dht_topo.c */