2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * put_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},
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'
225 shutdown_task (void *cls)
227 struct GNUNET_DHT_TEST_Context *ctx = cls;
228 struct GetOperation *get_op;
230 while (NULL != (get_op = get_tail))
232 GNUNET_DHT_get_stop (get_op->get);
233 GNUNET_CONTAINER_DLL_remove (get_head,
236 GNUNET_free (get_op);
238 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
248 * Iterator called on each result obtained for a DHT
249 * operation that expects a reply
251 * @param cls closure with our 'struct GetOperation'
252 * @param exp when will this value expire
253 * @param key key of the result
254 * @param get_path peers on reply path (or NULL if not recorded)
255 * @param get_path_length number of entries in get_path
256 * @param put_path peers on the PUT path (or NULL if not recorded)
257 * @param put_path_length number of entries in get_path
258 * @param type type of the result
259 * @param size number of bytes in data
260 * @param data pointer to the result data
263 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
264 const struct GNUNET_HashCode * key,
265 const struct GNUNET_PeerIdentity *get_path,
266 unsigned int get_path_length,
267 const struct GNUNET_PeerIdentity *put_path,
268 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
269 size_t size, const void *data)
271 struct GetOperation *get_op = cls;
272 struct GNUNET_HashCode want;
273 struct GNUNET_DHT_TestContext *ctx;
275 if (sizeof (struct GNUNET_HashCode) != size)
280 GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
281 if (0 != memcmp (&want, data, sizeof (want)))
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH: (get %u, put %u)\n",
293 get_path_length, put_path_length);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " LOCAL\n");
295 for (i = get_path_length - 1; i >= 0; i--)
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
297 GNUNET_i2s (&get_path[i]));
298 for (i = put_path_length - 1; i >= 0; i--)
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
300 GNUNET_i2s (&put_path[i]));
303 GNUNET_DHT_get_stop (get_op->get);
304 GNUNET_CONTAINER_DLL_remove (get_head,
307 GNUNET_free (get_op);
308 if (NULL != get_head)
310 /* all DHT GET operations successful; terminate! */
312 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
313 timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
318 * Task to put the id of each peer into the DHT.
320 * @param cls array with NUM_PEERS DHT handles
321 * @param tc Task context
326 struct GNUNET_DHT_Handle **hs = cls;
327 struct GNUNET_HashCode key;
328 struct GNUNET_HashCode value;
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Putting values into DHT\n");
333 for (i = 0; i < NUM_PEERS; i++)
335 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
336 GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
337 GNUNET_DHT_put (hs[i], &key, 10U,
338 GNUNET_DHT_RO_RECORD_ROUTE |
339 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
340 GNUNET_BLOCK_TYPE_TEST,
341 sizeof (value), &value,
342 GNUNET_TIME_UNIT_FOREVER_ABS,
343 GNUNET_TIME_UNIT_FOREVER_REL,
346 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
352 * Main function of the test.
354 * @param cls closure (NULL)
355 * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
356 * @param num_peers number of peers that are running
357 * @param peers array of peers
358 * @param dhts handle to each of the DHTs of the peers
362 struct GNUNET_DHT_TEST_Context *ctx,
363 unsigned int num_peers,
364 struct GNUNET_TESTBED_Peer **peers,
365 struct GNUNET_DHT_Handle **dhts)
369 struct GNUNET_HashCode key;
370 struct GetOperation *get_op;
372 GNUNET_assert (NUM_PEERS == num_peers);
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Peers setup, starting test\n");
376 put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
377 for (i=0;i<num_peers;i++)
379 GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
380 for (j=0;j<num_peers;j++)
382 get_op = GNUNET_new (struct GetOperation);
383 GNUNET_CONTAINER_DLL_insert (get_head,
386 get_op->get = GNUNET_DHT_get_start (dhts[j],
387 GNUNET_BLOCK_TYPE_TEST, /* type */
388 &key, /*key to search */
389 4U, /* replication level */
390 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
393 &dht_get_handler, get_op);
396 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
397 &shutdown_task, ctx);
405 main (int xargc, char *xargv[])
407 const char *cfg_filename;
408 const char *test_name;
410 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
412 cfg_filename = "test_dht_2dtorus.conf";
413 test_name = "test-dht-2dtorus";
416 else if (NULL != strstr (xargv[0], "test_dht_line"))
418 cfg_filename = "test_dht_line.conf";
419 test_name = "test-dht-line";
422 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
424 cfg_filename = "test_dht_line.conf";
425 test_name = "test-dht-twopeer";
428 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
430 cfg_filename = "test_dht_multipeer.conf";
431 test_name = "test-dht-multipeer";
439 GNUNET_DHT_TEST_run (test_name,
446 /* end of test_dht_topo.c */