remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / dht / test_dht_topo.c
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  */
48 struct GetOperation
49 {
50   /**
51    * DLL.
52    */
53   struct GetOperation *next;
54
55   /**
56    * DLL.
57    */
58   struct GetOperation *prev;
59
60   /**
61    * Handle for the operation.
62    */
63   struct GNUNET_DHT_GetHandle *get;
64 };
65
66
67 /**
68  * Result of the test.
69  */
70 static int ok = 1;
71
72 /**
73  * Task to do DHT_puts
74  */
75 static struct GNUNET_SCHEDULER_Task *put_task;
76
77 /**
78  * Task to do DHT_gets
79  */
80 static struct GNUNET_SCHEDULER_Task *get_task;
81
82 /**
83  * Task to time out / regular shutdown.
84  */
85 static struct GNUNET_SCHEDULER_Task *timeout_task;
86
87 /**
88  * Head of list of active GET operations.
89  */
90 static struct GetOperation *get_head;
91
92 /**
93  * Tail of list of active GET operations.
94  */
95 static struct GetOperation *get_tail;
96
97 /**
98  * Array of the testbed's peers.
99  */
100 static struct GNUNET_TESTBED_Peer **my_peers;
101
102 /**
103  * Number of peers to run.
104  */
105 static unsigned int NUM_PEERS;
106
107
108 /**
109  * Statistics we print out.
110  */
111 static struct
112 {
113   const char *subsystem;
114   const char *name;
115   unsigned long long total;
116 } stats[] = {
117   { "core", "# bytes decrypted", 0 },
118   { "core", "# bytes encrypted", 0 },
119   { "core", "# type maps received", 0 },
120   { "core", "# session keys confirmed via PONG", 0 },
121   { "core", "# peers connected", 0 },
122   { "core", "# key exchanges initiated", 0 },
123   { "core", "# send requests dropped (disconnected)", 0 },
124   { "core", "# transmissions delayed due to corking", 0 },
125   { "core", "# messages discarded (expired prior to transmission)", 0 },
126   { "core", "# messages discarded (disconnected)", 0 },
127   { "core", "# discarded CORE_SEND requests", 0 },
128   { "core", "# discarded lower priority CORE_SEND requests", 0 },
129   { "transport", "# bytes received via TCP", 0 },
130   { "transport", "# bytes transmitted via TCP", 0 },
131   { "dht", "# PUT messages queued for transmission", 0 },
132   { "dht", "# P2P PUT requests received", 0 },
133   { "dht", "# GET messages queued for transmission", 0 },
134   { "dht", "# P2P GET requests received", 0 },
135   { "dht", "# RESULT messages queued for transmission", 0 },
136   { "dht", "# P2P RESULTS received", 0 },
137   { "dht", "# Queued messages discarded (peer disconnected)", 0 },
138   { "dht", "# Peers excluded from routing due to Bloomfilter", 0 },
139   { "dht", "# Peer selection failed", 0 },
140   { "dht", "# FIND PEER requests ignored due to Bloomfilter", 0 },
141   { "dht", "# FIND PEER requests ignored due to lack of HELLO", 0 },
142   { "dht", "# P2P FIND PEER requests processed", 0 },
143   { "dht", "# P2P GET requests ONLY routed", 0 },
144   { "dht", "# Preference updates given to core", 0 },
145   { "dht", "# REPLIES ignored for CLIENTS (no match)", 0 },
146   { "dht", "# GET requests from clients injected", 0 },
147   { "dht", "# GET requests received from clients", 0 },
148   { "dht", "# GET STOP requests received from clients", 0 },
149   { "dht", "# ITEMS stored in datacache", 0 },
150   { "dht", "# Good RESULTS found in datacache", 0 },
151   { "dht", "# GET requests given to datacache", 0 },
152   { NULL, NULL, 0 }
153 };
154
155
156 static struct GNUNET_DHT_TEST_Context *
157 stop_ops ()
158 {
159   struct GetOperation *get_op;
160   struct GNUNET_DHT_TEST_Context *ctx = NULL;
161
162   if (NULL != timeout_task)
163   {
164     ctx = GNUNET_SCHEDULER_cancel (timeout_task);
165     timeout_task = NULL;
166   }
167   if (NULL != put_task)
168   {
169     GNUNET_SCHEDULER_cancel (put_task);
170     put_task = NULL;
171   }
172   if (NULL != get_task)
173   {
174     GNUNET_SCHEDULER_cancel (get_task);
175     get_task = NULL;
176   }
177   while (NULL != (get_op = get_tail))
178   {
179     GNUNET_DHT_get_stop (get_op->get);
180     GNUNET_CONTAINER_DLL_remove (get_head,
181                                  get_tail,
182                                  get_op);
183     GNUNET_free (get_op);
184   }
185   return ctx;
186 }
187
188
189 /**
190  * Function called once we're done processing stats.
191  *
192  * @param cls the test context
193  * @param op the stats operation
194  * @param emsg error message on failure
195  */
196 static void
197 stats_finished (void *cls,
198                 struct GNUNET_TESTBED_Operation *op,
199                 const char *emsg)
200 {
201   struct GNUNET_DHT_TEST_Context *ctx = cls;
202   unsigned int i;
203
204   if (NULL != op)
205     GNUNET_TESTBED_operation_done (op);
206   if (NULL != emsg)
207   {
208     fprintf (stderr,
209              _ ("Gathering statistics failed: %s\n"),
210              emsg);
211     GNUNET_SCHEDULER_cancel (put_task);
212     GNUNET_DHT_TEST_cleanup (ctx);
213     return;
214   }
215   for (i = 0; NULL != stats[i].name; i++)
216     fprintf (stderr,
217              "%6s/%60s = %12llu\n",
218              stats[i].subsystem,
219              stats[i].name,
220              stats[i].total);
221   GNUNET_DHT_TEST_cleanup (ctx);
222   GNUNET_SCHEDULER_shutdown ();
223 }
224
225
226 /**
227  * Function called to process statistic values from all peers.
228  *
229  * @param cls closure
230  * @param peer the peer the statistic belong to
231  * @param subsystem name of subsystem that created the statistic
232  * @param name the name of the datum
233  * @param value the current value
234  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
235  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
236  */
237 static int
238 handle_stats (void *cls,
239               const struct GNUNET_TESTBED_Peer *peer,
240               const char *subsystem,
241               const char *name,
242               uint64_t value,
243               int is_persistent)
244 {
245   unsigned int i;
246
247   for (i = 0; NULL != stats[i].name; i++)
248     if ((0 == strcasecmp (subsystem,
249                           stats[i].subsystem)) &&
250         (0 == strcasecmp (name,
251                           stats[i].name)))
252       stats[i].total += value;
253   return GNUNET_OK;
254 }
255
256
257 /**
258  * Task run on shutdown to clean up.  Terminates active get operations
259  * and shuts down the testbed.
260  *
261  * @param cls the 'struct GNUNET_DHT_TestContext'
262  */
263 static void
264 shutdown_task (void *cls)
265 {
266   (void) stop_ops ();
267 }
268
269
270 /**
271  * Task run on timeout to clean up.  Terminates active get operations
272  * and shuts down the testbed.
273  *
274  * @param cls the `struct GNUNET_DHT_TestContext`
275  */
276 static void
277 timeout_cb (void *cls)
278 {
279   timeout_task = NULL;
280   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281               "Timeout\n");
282   GNUNET_SCHEDULER_shutdown ();
283 }
284
285
286 /**
287  * Iterator called on each result obtained for a DHT
288  * operation that expects a reply
289  *
290  * @param cls closure with our 'struct GetOperation'
291  * @param exp when will this value expire
292  * @param key key of the result
293  * @param get_path peers on reply path (or NULL if not recorded)
294  * @param get_path_length number of entries in @a get_path
295  * @param put_path peers on the PUT path (or NULL if not recorded)
296  * @param put_path_length number of entries in @a put_path
297  * @param type type of the result
298  * @param size number of bytes in @a data
299  * @param data pointer to the result data
300  */
301 static void
302 dht_get_handler (void *cls,
303                  struct GNUNET_TIME_Absolute exp,
304                  const struct GNUNET_HashCode *key,
305                  const struct GNUNET_PeerIdentity *get_path,
306                  unsigned int get_path_length,
307                  const struct GNUNET_PeerIdentity *put_path,
308                  unsigned int put_path_length,
309                  enum GNUNET_BLOCK_Type type,
310                  size_t size,
311                  const void *data)
312 {
313   struct GetOperation *get_op = cls;
314   struct GNUNET_HashCode want;
315   struct GNUNET_DHT_TEST_Context *ctx;
316
317   if (sizeof(struct GNUNET_HashCode) != size)
318   {
319     GNUNET_break (0);
320     return;
321   }
322   GNUNET_CRYPTO_hash (key,
323                       sizeof(*key),
324                       &want);
325   if (0 != memcmp (&want,
326                    data,
327                    sizeof(want)))
328   {
329     GNUNET_break (0);
330     return;
331   }
332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333               "Get successful\n");
334 #if 0
335   {
336     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337                 "PATH: (get %u, put %u)\n",
338                 get_path_length,
339                 put_path_length);
340     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                 "  LOCAL\n");
342     for (int i = get_path_length - 1; i >= 0; i--)
343       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344                   "  %s\n",
345                   GNUNET_i2s (&get_path[i]));
346     for (int i = put_path_length - 1; i >= 0; i--)
347       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348                   "  %s\n",
349                   GNUNET_i2s (&put_path[i]));
350   }
351 #endif
352   GNUNET_DHT_get_stop (get_op->get);
353   GNUNET_CONTAINER_DLL_remove (get_head,
354                                get_tail,
355                                get_op);
356   GNUNET_free (get_op);
357   if (NULL != get_head)
358     return;
359   /* all DHT GET operations successful; get stats! */
360   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
361               "All DHT operations successful. Obtaining stats!\n");
362   ok = 0;
363   ctx = stop_ops ();
364   GNUNET_assert (NULL != ctx);
365   (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
366                                         my_peers,
367                                         NULL, NULL,
368                                         &handle_stats,
369                                         &stats_finished,
370                                         ctx);
371 }
372
373
374 /**
375  * Task to put the id of each peer into the DHT.
376  *
377  * @param cls array with NUM_PEERS DHT handles
378  * @param tc Task context
379  */
380 static void
381 do_puts (void *cls)
382 {
383   struct GNUNET_DHT_Handle **hs = cls;
384   struct GNUNET_HashCode key;
385   struct GNUNET_HashCode value;
386
387   put_task = NULL;
388   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389               "Putting values into DHT\n");
390   for (unsigned int i = 0; i < NUM_PEERS; i++)
391   {
392     GNUNET_CRYPTO_hash (&i,
393                         sizeof(i),
394                         &key);
395     GNUNET_CRYPTO_hash (&key,
396                         sizeof(key),
397                         &value);
398     GNUNET_DHT_put (hs[i],
399                     &key,
400                     10U,
401                     GNUNET_DHT_RO_RECORD_ROUTE
402                     | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
403                     GNUNET_BLOCK_TYPE_TEST,
404                     sizeof(value),
405                     &value,
406                     GNUNET_TIME_UNIT_FOREVER_ABS,
407                     NULL,
408                     NULL);
409   }
410   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
411                                            &do_puts,
412                                            hs);
413 }
414
415
416 /**
417  * Start GET operations.
418  */
419 static void
420 start_get (void *cls)
421 {
422   struct GNUNET_DHT_Handle **dhts = cls;
423   unsigned int i;
424   unsigned int j;
425   struct GNUNET_HashCode key;
426   struct GetOperation *get_op;
427
428   get_task = NULL;
429   for (i = 0; i < NUM_PEERS; i++)
430   {
431     GNUNET_CRYPTO_hash (&i, sizeof(i), &key);
432     for (j = 0; j < NUM_PEERS; j++)
433     {
434       get_op = GNUNET_new (struct GetOperation);
435       GNUNET_CONTAINER_DLL_insert (get_head,
436                                    get_tail,
437                                    get_op);
438       get_op->get = GNUNET_DHT_get_start (dhts[j],
439                                           GNUNET_BLOCK_TYPE_TEST,    /* type */
440                                           &key,      /*key to search */
441                                           4U,     /* replication level */
442                                           GNUNET_DHT_RO_RECORD_ROUTE
443                                           | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
444                                           NULL,        /* xquery */
445                                           0,      /* xquery bits */
446                                           &dht_get_handler,
447                                           get_op);
448     }
449   }
450 }
451
452
453 /**
454  * Main function of the test.
455  *
456  * @param cls closure (NULL)
457  * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
458  * @param num_peers number of @a peers that are running
459  * @param peers array of peers
460  * @param dhts handle to each of the DHTs of the peers
461  */
462 static void
463 run (void *cls,
464      struct GNUNET_DHT_TEST_Context *ctx,
465      unsigned int num_peers,
466      struct GNUNET_TESTBED_Peer **peers,
467      struct GNUNET_DHT_Handle **dhts)
468 {
469   GNUNET_assert (NUM_PEERS == num_peers);
470   my_peers = peers;
471   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472               "Peers setup, starting test\n");
473   put_task = GNUNET_SCHEDULER_add_now (&do_puts,
474                                        dhts);
475   get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
476                                            &start_get,
477                                            dhts);
478   timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
479                                                &timeout_cb,
480                                                ctx);
481   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
482                                  ctx);
483 }
484
485
486 /**
487  * Main: start test
488  */
489 int
490 main (int xargc, char *xargv[])
491 {
492   const char *cfg_filename;
493   const char *test_name;
494
495   if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
496   {
497     cfg_filename = "test_dht_2dtorus.conf";
498     test_name = "test-dht-2dtorus";
499     NUM_PEERS = 16;
500   }
501   else if (NULL != strstr (xargv[0], "test_dht_line"))
502   {
503     cfg_filename = "test_dht_line.conf";
504     test_name = "test-dht-line";
505     NUM_PEERS = 5;
506   }
507   else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
508   {
509     cfg_filename = "test_dht_line.conf";
510     test_name = "test-dht-twopeer";
511     NUM_PEERS = 2;
512   }
513   else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
514   {
515     cfg_filename = "test_dht_multipeer.conf";
516     test_name = "test-dht-multipeer";
517     NUM_PEERS = 10;
518   }
519   else
520   {
521     GNUNET_break (0);
522     return 1;
523   }
524   GNUNET_DHT_TEST_run (test_name,
525                        cfg_filename,
526                        NUM_PEERS,
527                        &run, NULL);
528   return ok;
529 }
530
531
532 /* end of test_dht_topo.c */