aboutsummaryrefslogtreecommitdiff
path: root/src/service/dht/test_dht_topo.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dht/test_dht_topo.c')
-rw-r--r--src/service/dht/test_dht_topo.c637
1 files changed, 637 insertions, 0 deletions
diff --git a/src/service/dht/test_dht_topo.c b/src/service/dht/test_dht_topo.c
new file mode 100644
index 000000000..a8294c65d
--- /dev/null
+++ b/src/service/dht/test_dht_topo.c
@@ -0,0 +1,637 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
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.
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_dht_service.h"
30#include "dht_test_lib.h"
31
32/**
33 * How long until we give up on fetching the data?
34 */
35#define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
36 120)
37
38/**
39 * How frequently do we execute the PUTs?
40 */
41#define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
42 5)
43
44
45/**
46 * Information we keep for each GET operation.
47 */
48struct GetOperation
49{
50 /**
51 * DLL.
52 */
53 struct GetOperation *next;
54
55 /**
56 * DLL.
57 */
58 struct GetOperation *prev;
59
60 /**
61 * Operation to fetch @a me.
62 */
63 struct GNUNET_TESTBED_Operation *to;
64
65 /**
66 * Handle for the operation.
67 */
68 struct GNUNET_DHT_GetHandle *get;
69
70 /**
71 * DHT used by this operation.
72 */
73 struct GNUNET_DHT_Handle *dht;
74
75 /**
76 * Key we are looking up.
77 */
78 struct GNUNET_HashCode key;
79
80 /**
81 * At which peer is this operation being performed?
82 */
83 struct GNUNET_PeerIdentity me;
84};
85
86
87/**
88 * Result of the test.
89 */
90static int ok;
91
92/**
93 * Task to do DHT_puts
94 */
95static struct GNUNET_SCHEDULER_Task *put_task;
96
97/**
98 * Task to do DHT_gets
99 */
100static struct GNUNET_SCHEDULER_Task *get_task;
101
102/**
103 * Task to time out / regular shutdown.
104 */
105static struct GNUNET_SCHEDULER_Task *timeout_task;
106
107/**
108 * Head of list of active GET operations.
109 */
110static struct GetOperation *get_head;
111
112/**
113 * Tail of list of active GET operations.
114 */
115static struct GetOperation *get_tail;
116
117/**
118 * Array of the testbed's peers.
119 */
120static struct GNUNET_TESTBED_Peer **my_peers;
121
122/**
123 * Number of peers to run.
124 */
125static unsigned int NUM_PEERS;
126
127
128/**
129 * Statistics we print out.
130 */
131static struct
132{
133 const char *subsystem;
134 const char *name;
135 unsigned long long total;
136} stats[] = {
137 { "core", "# bytes decrypted", 0 },
138 { "core", "# bytes encrypted", 0 },
139 { "core", "# type maps received", 0 },
140 { "core", "# session keys confirmed via PONG", 0 },
141 { "core", "# peers connected", 0 },
142 { "core", "# key exchanges initiated", 0 },
143 { "core", "# send requests dropped (disconnected)", 0 },
144 { "core", "# transmissions delayed due to corking", 0 },
145 { "core", "# messages discarded (expired prior to transmission)", 0 },
146 { "core", "# messages discarded (disconnected)", 0 },
147 { "core", "# discarded CORE_SEND requests", 0 },
148 { "core", "# discarded lower priority CORE_SEND requests", 0 },
149 { "transport", "# bytes received via TCP", 0 },
150 { "transport", "# bytes transmitted via TCP", 0 },
151 { "dht", "# PUT messages queued for transmission", 0 },
152 { "dht", "# P2P PUT requests received", 0 },
153 { "dht", "# GET messages queued for transmission", 0 },
154 { "dht", "# P2P GET requests received", 0 },
155 { "dht", "# RESULT messages queued for transmission", 0 },
156 { "dht", "# P2P RESULTS received", 0 },
157 { "dht", "# Queued messages discarded (peer disconnected)", 0 },
158 { "dht", "# Peers excluded from routing due to Bloomfilter", 0 },
159 { "dht", "# Peer selection failed", 0 },
160 { "dht", "# FIND PEER requests ignored due to Bloomfilter", 0 },
161 { "dht", "# FIND PEER requests ignored due to lack of HELLO", 0 },
162 { "dht", "# P2P FIND PEER requests processed", 0 },
163 { "dht", "# P2P GET requests ONLY routed", 0 },
164 { "dht", "# Preference updates given to core", 0 },
165 { "dht", "# REPLIES ignored for CLIENTS (no match)", 0 },
166 { "dht", "# GET requests from clients injected", 0 },
167 { "dht", "# GET requests received from clients", 0 },
168 { "dht", "# GET STOP requests received from clients", 0 },
169 { "dht", "# ITEMS stored in datacache", 0 },
170 { "dht", "# Good RESULTS found in datacache", 0 },
171 { "dht", "# GET requests given to datacache", 0 },
172 { NULL, NULL, 0 }
173};
174
175
176static struct GNUNET_DHT_TEST_Context *
177stop_ops (void)
178{
179 struct GetOperation *get_op;
180 struct GNUNET_DHT_TEST_Context *ctx = NULL;
181
182 if (NULL != timeout_task)
183 {
184 ctx = GNUNET_SCHEDULER_cancel (timeout_task);
185 timeout_task = NULL;
186 }
187 if (NULL != put_task)
188 {
189 GNUNET_SCHEDULER_cancel (put_task);
190 put_task = NULL;
191 }
192 if (NULL != get_task)
193 {
194 GNUNET_SCHEDULER_cancel (get_task);
195 get_task = NULL;
196 }
197 while (NULL != (get_op = get_tail))
198 {
199 if (NULL != get_op->to)
200 {
201 GNUNET_TESTBED_operation_done (get_op->to);
202 get_op->to = NULL;
203 }
204 if (NULL != get_op->get)
205 {
206 GNUNET_DHT_get_stop (get_op->get);
207 get_op->get = NULL;
208 }
209 GNUNET_CONTAINER_DLL_remove (get_head,
210 get_tail,
211 get_op);
212 GNUNET_free (get_op);
213 }
214 return ctx;
215}
216
217
218/**
219 * Function called once we're done processing stats.
220 *
221 * @param cls the test context
222 * @param op the stats operation
223 * @param emsg error message on failure
224 */
225static void
226stats_finished (void *cls,
227 struct GNUNET_TESTBED_Operation *op,
228 const char *emsg)
229{
230 struct GNUNET_DHT_TEST_Context *ctx = cls;
231
232 if (NULL != op)
233 GNUNET_TESTBED_operation_done (op);
234 if (NULL != emsg)
235 {
236 fprintf (stderr,
237 _ ("Gathering statistics failed: %s\n"),
238 emsg);
239 GNUNET_SCHEDULER_cancel (put_task);
240 GNUNET_DHT_TEST_cleanup (ctx);
241 return;
242 }
243 for (unsigned int i = 0; NULL != stats[i].name; i++)
244 fprintf (stderr,
245 "%6s/%60s = %12llu\n",
246 stats[i].subsystem,
247 stats[i].name,
248 stats[i].total);
249 GNUNET_DHT_TEST_cleanup (ctx);
250 GNUNET_SCHEDULER_shutdown ();
251}
252
253
254/**
255 * Function called to process statistic values from all peers.
256 *
257 * @param cls closure
258 * @param peer the peer the statistic belong to
259 * @param subsystem name of subsystem that created the statistic
260 * @param name the name of the datum
261 * @param value the current value
262 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
263 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
264 */
265static enum GNUNET_GenericReturnValue
266handle_stats (void *cls,
267 const struct GNUNET_TESTBED_Peer *peer,
268 const char *subsystem,
269 const char *name,
270 uint64_t value,
271 int is_persistent)
272{
273 for (unsigned int i = 0; NULL != stats[i].name; i++)
274 if ((0 == strcasecmp (subsystem,
275 stats[i].subsystem)) &&
276 (0 == strcasecmp (name,
277 stats[i].name)))
278 stats[i].total += value;
279 return GNUNET_OK;
280}
281
282
283/**
284 * Task run on shutdown to clean up. Terminates active get operations
285 * and shuts down the testbed.
286 *
287 * @param cls the 'struct GNUNET_DHT_TestContext'
288 */
289static void
290shutdown_task (void *cls)
291{
292 struct GNUNET_DHT_TEST_Context *ctx;
293
294 (void) cls;
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Performing shutdown\n");
297 ctx = stop_ops ();
298 if (NULL != ctx)
299 GNUNET_DHT_TEST_cleanup (ctx);
300}
301
302
303/**
304 * Task run on timeout to clean up. Terminates active get operations
305 * and shuts down the testbed.
306 *
307 * @param cls the `struct GNUNET_DHT_TestContext`
308 */
309static void
310timeout_cb (void *cls)
311{
312 struct GNUNET_DHT_TEST_Context *ctx = cls;
313
314 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
315 &timeout_cb,
316 ctx);
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
318 "Timeout\n");
319 GNUNET_SCHEDULER_shutdown ();
320}
321
322
323/**
324 * Iterator called on each result obtained for a DHT
325 * operation that expects a reply
326 *
327 * @param cls closure with our 'struct GetOperation'
328 * @param exp when will this value expire
329 * @param query query hash
330 * @param trunc_peer peer the path was truncated at, or NULL
331 * @param get_path peers on reply path (or NULL if not recorded)
332 * @param get_path_length number of entries in @a get_path
333 * @param put_path peers on the PUT path (or NULL if not recorded)
334 * @param put_path_length number of entries in @a put_path
335 * @param type type of the result
336 * @param size number of bytes in @a data
337 * @param data pointer to the result data
338 */
339static void
340dht_get_handler (void *cls,
341 struct GNUNET_TIME_Absolute exp,
342 const struct GNUNET_HashCode *query,
343 const struct GNUNET_PeerIdentity *trunc_peer,
344 const struct GNUNET_DHT_PathElement *get_path,
345 unsigned int get_path_length,
346 const struct GNUNET_DHT_PathElement *put_path,
347 unsigned int put_path_length,
348 enum GNUNET_BLOCK_Type type,
349 size_t size,
350 const void *data)
351{
352 struct GetOperation *get_op = cls;
353 struct GNUNET_HashCode want;
354 struct GNUNET_DHT_TEST_Context *ctx;
355
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "GET HANDLER called on PID %s\n",
358 GNUNET_i2s (&get_op->me));
359 if (sizeof(struct GNUNET_HashCode) != size)
360 {
361 GNUNET_break (0);
362 return;
363 }
364 if (0 != GNUNET_memcmp (query,
365 &get_op->key))
366 {
367 /* exact search should only yield exact results */
368 GNUNET_break (0);
369 return;
370 }
371 GNUNET_CRYPTO_hash (query,
372 sizeof(*query),
373 &want);
374 if (0 != memcmp (&want,
375 data,
376 sizeof(want)))
377 {
378 GNUNET_break (0);
379 return;
380 }
381 if (0 !=
382 GNUNET_DHT_verify_path (data,
383 size,
384 exp,
385 trunc_peer,
386 put_path,
387 put_path_length,
388 get_path,
389 get_path_length,
390 &get_op->me))
391 {
392 GNUNET_break (0);
393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
394 "Path signature (%u/%u) verification failed for peer %s!\n",
395 get_path_length,
396 put_path_length,
397 GNUNET_i2s (&get_op->me));
398 }
399 else
400 {
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Get successful\n");
403 ok--;
404 }
405 GNUNET_DHT_get_stop (get_op->get);
406 GNUNET_CONTAINER_DLL_remove (get_head,
407 get_tail,
408 get_op);
409 GNUNET_free (get_op);
410 if (NULL != get_head)
411 return;
412 /* all DHT GET operations successful; get stats! */
413 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
414 "All DHT operations successful. Obtaining stats!\n");
415 ctx = stop_ops ();
416 GNUNET_assert (NULL != ctx);
417 (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
418 my_peers,
419 NULL, NULL,
420 &handle_stats,
421 &stats_finished,
422 ctx);
423}
424
425
426/**
427 * Task to put the id of each peer into the DHT.
428 *
429 * @param cls array with NUM_PEERS DHT handles
430 * @param tc Task context
431 */
432static void
433do_puts (void *cls)
434{
435 struct GNUNET_DHT_Handle **hs = cls;
436 struct GNUNET_HashCode key;
437 struct GNUNET_HashCode value;
438
439 put_task = NULL;
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Putting %u values into DHT\n",
442 NUM_PEERS);
443 for (unsigned int i = 0; i < NUM_PEERS; i++)
444 {
445 GNUNET_CRYPTO_hash (&i,
446 sizeof(i),
447 &key);
448 GNUNET_CRYPTO_hash (&key,
449 sizeof(key),
450 &value);
451 GNUNET_DHT_put (hs[i],
452 &key,
453 10U,
454 GNUNET_DHT_RO_RECORD_ROUTE
455 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
456 GNUNET_BLOCK_TYPE_TEST,
457 sizeof(value),
458 &value,
459 GNUNET_TIME_UNIT_FOREVER_ABS,
460 NULL,
461 NULL);
462 }
463 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
464 &do_puts,
465 hs);
466}
467
468
469/**
470 * Callback to be called when the requested peer information is available
471 * The peer information in the callback is valid until the operation 'op' is canceled.
472 *
473 * @param cls a `struct GetOperation *`
474 * @param op the operation this callback corresponds to
475 * @param pinfo the result; will be NULL if the operation has failed
476 * @param emsg error message if the operation has failed; will be NULL if the
477 * operation is successful
478 */
479static void
480pid_cb (void *cls,
481 struct GNUNET_TESTBED_Operation *op,
482 const struct GNUNET_TESTBED_PeerInformation *pinfo,
483 const char *emsg)
484{
485 struct GetOperation *get_op = cls;
486
487 if (NULL != emsg)
488 {
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490 "Testbed failure: %s\n",
491 emsg);
492 GNUNET_TESTBED_operation_done (get_op->to);
493 get_op->to = NULL;
494 GNUNET_SCHEDULER_shutdown ();
495 return;
496 }
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Testbed provided PID %s\n",
499 GNUNET_i2s (pinfo->result.id));
500 get_op->me = *pinfo->result.id;
501 GNUNET_TESTBED_operation_done (get_op->to);
502 get_op->to = NULL;
503 get_op->get = GNUNET_DHT_get_start (get_op->dht,
504 GNUNET_BLOCK_TYPE_TEST,
505 &get_op->key,
506 4U, /* replication level */
507 GNUNET_DHT_RO_RECORD_ROUTE
508 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
509 NULL, /* xquery */
510 0, /* xquery bits */
511 &dht_get_handler,
512 get_op);
513}
514
515
516/**
517 * Start GET operations.
518 */
519static void
520start_get (void *cls)
521{
522 struct GNUNET_DHT_Handle **dhts = cls;
523
524 get_task = NULL;
525 for (unsigned int i = 0; i < NUM_PEERS; i++)
526 {
527 struct GNUNET_HashCode key;
528
529 GNUNET_CRYPTO_hash (&i,
530 sizeof(i),
531 &key);
532 for (unsigned int j = 0; j < NUM_PEERS; j++)
533 {
534 struct GetOperation *get_op;
535
536 get_op = GNUNET_new (struct GetOperation);
537 ok++;
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Starting GET %p\n",
540 get_op);
541 get_op->key = key;
542 get_op->dht = dhts[j];
543 get_op->to = GNUNET_TESTBED_peer_get_information (my_peers[j],
544 GNUNET_TESTBED_PIT_IDENTITY,
545 &pid_cb,
546 get_op);
547 GNUNET_CONTAINER_DLL_insert (get_head,
548 get_tail,
549 get_op);
550 }
551 }
552}
553
554
555/**
556 * Main function of the test.
557 *
558 * @param cls closure (NULL)
559 * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
560 * @param num_peers number of @a peers that are running
561 * @param peers array of peers
562 * @param dhts handle to each of the DHTs of the peers
563 */
564static void
565run (void *cls,
566 struct GNUNET_DHT_TEST_Context *ctx,
567 unsigned int num_peers,
568 struct GNUNET_TESTBED_Peer **peers,
569 struct GNUNET_DHT_Handle **dhts)
570{
571 GNUNET_assert (NUM_PEERS == num_peers);
572 my_peers = peers;
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Peers setup, starting test\n");
575 put_task = GNUNET_SCHEDULER_add_now (&do_puts,
576 dhts);
577 get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
578 &start_get,
579 dhts);
580 timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
581 &timeout_cb,
582 ctx);
583 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
584 ctx);
585}
586
587
588/**
589 * Main: start test
590 */
591int
592main (int xargc, char *xargv[])
593{
594 const char *cfg_filename;
595 const char *test_name;
596
597 unsetenv ("XDG_DATA_HOME");
598 unsetenv ("XDG_CONFIG_HOME");
599 unsetenv ("XDG_CACHE_HOME");
600 if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
601 {
602 cfg_filename = "test_dht_2dtorus.conf";
603 test_name = "test-dht-2dtorus";
604 NUM_PEERS = 16;
605 }
606 else if (NULL != strstr (xargv[0], "test_dht_line"))
607 {
608 cfg_filename = "test_dht_line.conf";
609 test_name = "test-dht-line";
610 NUM_PEERS = 5;
611 }
612 else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
613 {
614 cfg_filename = "test_dht_line.conf";
615 test_name = "test-dht-twopeer";
616 NUM_PEERS = 2;
617 }
618 else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
619 {
620 cfg_filename = "test_dht_multipeer.conf";
621 test_name = "test-dht-multipeer";
622 NUM_PEERS = 10;
623 }
624 else
625 {
626 GNUNET_break (0);
627 return 1;
628 }
629 GNUNET_DHT_TEST_run (test_name,
630 cfg_filename,
631 NUM_PEERS,
632 &run, NULL);
633 return ok;
634}
635
636
637/* end of test_dht_topo.c */