indentation fixes
[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
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.
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      General Public License for more details.
14
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.
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     int i;
336
337     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338                 "PATH: (get %u, put %u)\n",
339                 get_path_length,
340                 put_path_length);
341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342                 "  LOCAL\n");
343     for (i = get_path_length - 1; i >= 0; i--)
344       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345                   "  %s\n",
346                   GNUNET_i2s (&get_path[i]));
347     for (i = put_path_length - 1; i >= 0; i--)
348       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349                   "  %s\n",
350                   GNUNET_i2s (&put_path[i]));
351   }
352 #endif
353   GNUNET_DHT_get_stop (get_op->get);
354   GNUNET_CONTAINER_DLL_remove (get_head,
355                                get_tail,
356                                get_op);
357   GNUNET_free (get_op);
358   if (NULL != get_head)
359     return;
360   /* all DHT GET operations successful; get stats! */
361   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
362               "All DHT operations successful. Obtaining stats!\n");
363   ok = 0;
364   ctx = stop_ops ();
365   GNUNET_assert (NULL != ctx);
366   (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
367                                         my_peers,
368                                         NULL, NULL,
369                                         &handle_stats,
370                                         &stats_finished,
371                                         ctx);
372 }
373
374
375 /**
376  * Task to put the id of each peer into the DHT.
377  *
378  * @param cls array with NUM_PEERS DHT handles
379  * @param tc Task context
380  */
381 static void
382 do_puts (void *cls)
383 {
384   struct GNUNET_DHT_Handle **hs = cls;
385   struct GNUNET_HashCode key;
386   struct GNUNET_HashCode value;
387   unsigned int i;
388
389   put_task = NULL;
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391               "Putting values into DHT\n");
392   for (i = 0; i < NUM_PEERS; i++)
393   {
394     GNUNET_CRYPTO_hash (&i,
395                         sizeof (i),
396                         &key);
397     GNUNET_CRYPTO_hash (&key,
398                         sizeof (key),
399                         &value);
400     GNUNET_DHT_put (hs[i],
401                     &key,
402                     10U,
403                     GNUNET_DHT_RO_RECORD_ROUTE |
404                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
405                     GNUNET_BLOCK_TYPE_TEST,
406                     sizeof (value),
407                     &value,
408                     GNUNET_TIME_UNIT_FOREVER_ABS,
409                     NULL,
410                     NULL);
411   }
412   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
413                                            &do_puts,
414                                            hs);
415 }
416
417
418 /**
419  * Start GET operations.
420  */
421 static void
422 start_get (void *cls)
423 {
424   struct GNUNET_DHT_Handle **dhts = cls;
425   unsigned int i;
426   unsigned int j;
427   struct GNUNET_HashCode key;
428   struct GetOperation *get_op;
429
430   get_task = NULL;
431   for (i=0;i<NUM_PEERS;i++)
432   {
433     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
434     for (j=0;j<NUM_PEERS;j++)
435     {
436       get_op = GNUNET_new (struct GetOperation);
437       GNUNET_CONTAINER_DLL_insert (get_head,
438                                    get_tail,
439                                    get_op);
440       get_op->get = GNUNET_DHT_get_start (dhts[j],
441                                           GNUNET_BLOCK_TYPE_TEST, /* type */
442                                           &key,      /*key to search */
443                                           4U,     /* replication level */
444                                           GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
445                                           NULL,        /* xquery */
446                                           0,      /* xquery bits */
447                                           &dht_get_handler,
448                                           get_op);
449     }
450   }
451 }
452
453
454 /**
455  * Main function of the test.
456  *
457  * @param cls closure (NULL)
458  * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
459  * @param num_peers number of @a peers that are running
460  * @param peers array of peers
461  * @param dhts handle to each of the DHTs of the peers
462  */
463 static void
464 run (void *cls,
465      struct GNUNET_DHT_TEST_Context *ctx,
466      unsigned int num_peers,
467      struct GNUNET_TESTBED_Peer **peers,
468      struct GNUNET_DHT_Handle **dhts)
469 {
470   GNUNET_assert (NUM_PEERS == num_peers);
471   my_peers = peers;
472   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473               "Peers setup, starting test\n");
474   put_task = GNUNET_SCHEDULER_add_now (&do_puts,
475                                        dhts);
476   get_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
477                                            &start_get,
478                                            dhts);
479   timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
480                                                &timeout_cb,
481                                                ctx);
482   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
483                                  ctx);
484 }
485
486
487 /**
488  * Main: start test
489  */
490 int
491 main (int xargc, char *xargv[])
492 {
493   const char *cfg_filename;
494   const char *test_name;
495
496   if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
497   {
498     cfg_filename = "test_dht_2dtorus.conf";
499     test_name = "test-dht-2dtorus";
500     NUM_PEERS = 16;
501   }
502   else if (NULL != strstr (xargv[0], "test_dht_line"))
503   {
504     cfg_filename = "test_dht_line.conf";
505     test_name = "test-dht-line";
506     NUM_PEERS = 5;
507   }
508   else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
509   {
510     cfg_filename = "test_dht_line.conf";
511     test_name = "test-dht-twopeer";
512     NUM_PEERS = 2;
513   }
514   else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
515   {
516     cfg_filename = "test_dht_multipeer.conf";
517     test_name = "test-dht-multipeer";
518     NUM_PEERS = 10;
519   }
520   else
521   {
522     GNUNET_break (0);
523     return 1;
524   }
525   GNUNET_DHT_TEST_run (test_name,
526                        cfg_filename,
527                        NUM_PEERS,
528                        &run, NULL);
529   return ok;
530 }
531
532 /* end of test_dht_topo.c */