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