2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier put_task;
77 * Task to time out / regular shutdown.
79 static GNUNET_SCHEDULER_TaskIdentifier 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},
151 * Function called once we're done processing stats.
153 * @param cls the test context
154 * @param op the stats operation
155 * @param emsg error message on failure
158 stats_finished (void *cls,
159 struct GNUNET_TESTBED_Operation *op,
162 struct GNUNET_DHT_TEST_Context *ctx = cls;
166 GNUNET_TESTBED_operation_done (op); // needed?
169 fprintf (stderr, _("Gathering statistics failed: %s\n"),
171 GNUNET_SCHEDULER_cancel (put_task);
172 GNUNET_DHT_TEST_cleanup (ctx);
175 for (i = 0; NULL != stats[i].name; i++)
177 "%6s/%60s = %12llu\n",
181 GNUNET_SCHEDULER_cancel (put_task);
182 GNUNET_DHT_TEST_cleanup (ctx);
187 * Function called to process statistic values from all peers.
190 * @param peer the peer the statistic belong to
191 * @param subsystem name of subsystem that created the statistic
192 * @param name the name of the datum
193 * @param value the current value
194 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
195 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
198 handle_stats (void *cls,
199 const struct GNUNET_TESTBED_Peer *peer,
200 const char *subsystem,
207 for (i = 0; NULL != stats[i].name; i++)
208 if ( (0 == strcasecmp (subsystem,
209 stats[i].subsystem)) &&
210 (0 == strcasecmp (name,
212 stats[i].total += value;
218 * Task run on success or timeout to clean up.
219 * Terminates active get operations and shuts down
222 * @param cls the 'struct GNUNET_DHT_TestContext'
223 * @param tc scheduler context
226 shutdown_task (void *cls,
227 const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct GNUNET_DHT_TEST_Context *ctx = cls;
230 struct GetOperation *get_op;
232 while (NULL != (get_op = get_tail))
234 GNUNET_DHT_get_stop (get_op->get);
235 GNUNET_CONTAINER_DLL_remove (get_head,
238 GNUNET_free (get_op);
240 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
250 * Iterator called on each result obtained for a DHT
251 * operation that expects a reply
253 * @param cls closure with our 'struct GetOperation'
254 * @param exp when will this value expire
255 * @param key key of the result
256 * @param get_path peers on reply path (or NULL if not recorded)
257 * @param get_path_length number of entries in get_path
258 * @param put_path peers on the PUT path (or NULL if not recorded)
259 * @param put_path_length number of entries in get_path
260 * @param type type of the result
261 * @param size number of bytes in data
262 * @param data pointer to the result data
265 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
266 const struct GNUNET_HashCode * key,
267 const struct GNUNET_PeerIdentity *get_path,
268 unsigned int get_path_length,
269 const struct GNUNET_PeerIdentity *put_path,
270 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
271 size_t size, const void *data)
273 struct GetOperation *get_op = cls;
274 struct GNUNET_HashCode want;
275 struct GNUNET_DHT_TestContext *ctx;
277 if (sizeof (struct GNUNET_HashCode) != size)
282 GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
283 if (0 != memcmp (&want, data, sizeof (want)))
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH: (get %u, put %u)\n",
295 get_path_length, put_path_length);
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " LOCAL\n");
297 for (i = get_path_length - 1; i >= 0; i--)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
299 GNUNET_i2s (&get_path[i]));
300 for (i = put_path_length - 1; i >= 0; i--)
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
302 GNUNET_i2s (&put_path[i]));
305 GNUNET_DHT_get_stop (get_op->get);
306 GNUNET_CONTAINER_DLL_remove (get_head,
309 GNUNET_free (get_op);
310 if (NULL != get_head)
312 /* all DHT GET operations successful; terminate! */
314 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
315 timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
320 * Task to put the id of each peer into the DHT.
322 * @param cls array with NUM_PEERS DHT handles
323 * @param tc Task context
327 const struct GNUNET_SCHEDULER_TaskContext *tc)
329 struct GNUNET_DHT_Handle **hs = cls;
330 struct GNUNET_HashCode key;
331 struct GNUNET_HashCode value;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Putting values into DHT\n");
336 for (i = 0; i < NUM_PEERS; i++)
338 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
339 GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
340 GNUNET_DHT_put (hs[i], &key, 10U,
341 GNUNET_DHT_RO_RECORD_ROUTE |
342 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
343 GNUNET_BLOCK_TYPE_TEST,
344 sizeof (value), &value,
345 GNUNET_TIME_UNIT_FOREVER_ABS,
346 GNUNET_TIME_UNIT_FOREVER_REL,
349 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
355 * Main function of the test.
357 * @param cls closure (NULL)
358 * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
359 * @param num_peers number of peers that are running
360 * @param peers array of peers
361 * @param dhts handle to each of the DHTs of the peers
365 struct GNUNET_DHT_TEST_Context *ctx,
366 unsigned int num_peers,
367 struct GNUNET_TESTBED_Peer **peers,
368 struct GNUNET_DHT_Handle **dhts)
372 struct GNUNET_HashCode key;
373 struct GetOperation *get_op;
375 GNUNET_assert (NUM_PEERS == num_peers);
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Peers setup, starting test\n");
379 put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
380 for (i=0;i<num_peers;i++)
382 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
383 for (j=0;j<num_peers;j++)
385 get_op = GNUNET_malloc (sizeof (struct GetOperation));
386 GNUNET_CONTAINER_DLL_insert (get_head,
389 get_op->get = GNUNET_DHT_get_start (dhts[j],
390 GNUNET_BLOCK_TYPE_TEST, /* type */
391 &key, /*key to search */
392 4U, /* replication level */
393 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
396 &dht_get_handler, get_op);
399 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
400 &shutdown_task, ctx);
408 main (int xargc, char *xargv[])
410 const char *cfg_filename;
411 const char *test_name;
413 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
415 cfg_filename = "test_dht_2dtorus.conf";
416 test_name = "test-dht-2dtorus";
419 else if (NULL != strstr (xargv[0], "test_dht_line"))
421 cfg_filename = "test_dht_line.conf";
422 test_name = "test-dht-line";
425 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
427 cfg_filename = "test_dht_line.conf";
428 test_name = "test-dht-twopeer";
431 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
433 cfg_filename = "test_dht_multipeer.conf";
434 test_name = "test-dht-multipeer";
442 GNUNET_DHT_TEST_run (test_name,
449 /* end of test_dht_topo.c */