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