-remove trailing whitespace
[oweals/gnunet.git] / src / dht / test_dht_topo.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier put_task;
75
76 /**
77  * Task to time out / regular shutdown.
78  */
79 static GNUNET_SCHEDULER_TaskIdentifier 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 /**
151  * Function called once we're done processing stats.
152  *
153  * @param cls the test context
154  * @param op the stats operation
155  * @param emsg error message on failure
156  */
157 static void
158 stats_finished (void *cls,
159                 struct GNUNET_TESTBED_Operation *op,
160                 const char *emsg)
161 {
162   struct GNUNET_DHT_TEST_Context *ctx = cls;
163   unsigned int i;
164
165   if (NULL != op)
166     GNUNET_TESTBED_operation_done (op); // needed?
167   if (NULL != emsg)
168   {
169     fprintf (stderr, _("Gathering statistics failed: %s\n"),
170              emsg);
171     GNUNET_SCHEDULER_cancel (put_task);
172     GNUNET_DHT_TEST_cleanup (ctx);
173     return;
174   }
175   for (i = 0; NULL != stats[i].name; i++)
176     FPRINTF (stderr,
177              "%6s/%60s = %12llu\n",
178              stats[i].subsystem,
179              stats[i].name,
180              stats[i].total);
181   GNUNET_SCHEDULER_cancel (put_task);
182   GNUNET_DHT_TEST_cleanup (ctx);
183 }
184
185
186 /**
187  * Function called to process statistic values from all peers.
188  *
189  * @param cls closure
190  * @param peer the peer the statistic belong to
191  * @param subsystem name of subsystem that created the statistic
192  * @param name the name of the datum
193  * @param value the current value
194  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
195  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
196  */
197 static int
198 handle_stats (void *cls,
199               const struct GNUNET_TESTBED_Peer *peer,
200               const char *subsystem,
201               const char *name,
202               uint64_t value,
203               int is_persistent)
204 {
205   unsigned int i;
206
207   for (i = 0; NULL != stats[i].name; i++)
208     if ( (0 == strcasecmp (subsystem,
209                            stats[i].subsystem)) &&
210          (0 == strcasecmp (name,
211                            stats[i].name)) )
212       stats[i].total += value;
213   return GNUNET_OK;
214 }
215
216
217 /**
218  * Task run on success or timeout to clean up.
219  * Terminates active get operations and shuts down
220  * the testbed.
221  *
222  * @param cls the 'struct GNUNET_DHT_TestContext'
223  * @param tc scheduler context
224  */
225 static void
226 shutdown_task (void *cls,
227                const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   struct GNUNET_DHT_TEST_Context *ctx = cls;
230   struct GetOperation *get_op;
231
232   while (NULL != (get_op = get_tail))
233   {
234     GNUNET_DHT_get_stop (get_op->get);
235     GNUNET_CONTAINER_DLL_remove (get_head,
236                                  get_tail,
237                                  get_op);
238     GNUNET_free (get_op);
239   }
240   (void) GNUNET_TESTBED_get_statistics (NUM_PEERS,
241                                         my_peers,
242                                         NULL, NULL,
243                                         &handle_stats,
244                                         &stats_finished,
245                                         ctx);
246 }
247
248
249 /**
250  * Iterator called on each result obtained for a DHT
251  * operation that expects a reply
252  *
253  * @param cls closure with our 'struct GetOperation'
254  * @param exp when will this value expire
255  * @param key key of the result
256  * @param get_path peers on reply path (or NULL if not recorded)
257  * @param get_path_length number of entries in get_path
258  * @param put_path peers on the PUT path (or NULL if not recorded)
259  * @param put_path_length number of entries in get_path
260  * @param type type of the result
261  * @param size number of bytes in data
262  * @param data pointer to the result data
263  */
264 static void
265 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
266                  const struct GNUNET_HashCode * key,
267                  const struct GNUNET_PeerIdentity *get_path,
268                  unsigned int get_path_length,
269                  const struct GNUNET_PeerIdentity *put_path,
270                  unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
271                  size_t size, const void *data)
272 {
273   struct GetOperation *get_op = cls;
274   struct GNUNET_HashCode want;
275   struct GNUNET_DHT_TestContext *ctx;
276
277   if (sizeof (struct GNUNET_HashCode) != size)
278   {
279     GNUNET_break (0);
280     return;
281   }
282   GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
283   if (0 != memcmp (&want, data, sizeof (want)))
284   {
285     GNUNET_break (0);
286     return;
287   }
288   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289               "Get successful\n");
290 #if 0
291   {
292     int i;
293
294     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH: (get %u, put %u)\n",
295                 get_path_length, put_path_length);
296     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  LOCAL\n");
297     for (i = get_path_length - 1; i >= 0; i--)
298       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %s\n",
299                   GNUNET_i2s (&get_path[i]));
300     for (i = put_path_length - 1; i >= 0; i--)
301       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %s\n",
302                   GNUNET_i2s (&put_path[i]));
303   }
304 #endif
305   GNUNET_DHT_get_stop (get_op->get);
306   GNUNET_CONTAINER_DLL_remove (get_head,
307                                get_tail,
308                                get_op);
309   GNUNET_free (get_op);
310   if (NULL != get_head)
311     return;
312   /* all DHT GET operations successful; terminate! */
313   ok = 0;
314   ctx = GNUNET_SCHEDULER_cancel (timeout_task);
315   timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
316 }
317
318
319 /**
320  * Task to put the id of each peer into the DHT.
321  *
322  * @param cls array with NUM_PEERS DHT handles
323  * @param tc Task context
324  */
325 static void
326 do_puts (void *cls,
327          const struct GNUNET_SCHEDULER_TaskContext *tc)
328 {
329   struct GNUNET_DHT_Handle **hs = cls;
330   struct GNUNET_HashCode key;
331   struct GNUNET_HashCode value;
332   unsigned int i;
333
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Putting values into DHT\n");
336   for (i = 0; i < NUM_PEERS; i++)
337   {
338     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
339     GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
340     GNUNET_DHT_put (hs[i], &key, 10U,
341                     GNUNET_DHT_RO_RECORD_ROUTE |
342                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
343                     GNUNET_BLOCK_TYPE_TEST,
344                     sizeof (value), &value,
345                     GNUNET_TIME_UNIT_FOREVER_ABS,
346                     GNUNET_TIME_UNIT_FOREVER_REL,
347                     NULL, NULL);
348   }
349   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY,
350                                            &do_puts, hs);
351 }
352
353
354 /**
355  * Main function of the test.
356  *
357  * @param cls closure (NULL)
358  * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
359  * @param num_peers number of peers that are running
360  * @param peers array of peers
361  * @param dhts handle to each of the DHTs of the peers
362  */
363 static void
364 run (void *cls,
365      struct GNUNET_DHT_TEST_Context *ctx,
366      unsigned int num_peers,
367      struct GNUNET_TESTBED_Peer **peers,
368      struct GNUNET_DHT_Handle **dhts)
369 {
370   unsigned int i;
371   unsigned int j;
372   struct GNUNET_HashCode key;
373   struct GetOperation *get_op;
374
375   GNUNET_assert (NUM_PEERS == num_peers);
376   my_peers = peers;
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378               "Peers setup, starting test\n");
379   put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
380   for (i=0;i<num_peers;i++)
381   {
382     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
383     for (j=0;j<num_peers;j++)
384     {
385       get_op = GNUNET_malloc (sizeof (struct GetOperation));
386       GNUNET_CONTAINER_DLL_insert (get_head,
387                                    get_tail,
388                                    get_op);
389       get_op->get = GNUNET_DHT_get_start (dhts[j],
390                                           GNUNET_BLOCK_TYPE_TEST, /* type */
391                                           &key,      /*key to search */
392                                           4U,     /* replication level */
393                                           GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
394                                           NULL,        /* xquery */
395                                           0,      /* xquery bits */
396                                           &dht_get_handler, get_op);
397     }
398   }
399   timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT,
400                                                &shutdown_task, ctx);
401 }
402
403
404 /**
405  * Main: start test
406  */
407 int
408 main (int xargc, char *xargv[])
409 {
410   const char *cfg_filename;
411   const char *test_name;
412
413   if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
414   {
415     cfg_filename = "test_dht_2dtorus.conf";
416     test_name = "test-dht-2dtorus";
417     NUM_PEERS = 16;
418   }
419   else if (NULL != strstr (xargv[0], "test_dht_line"))
420   {
421     cfg_filename = "test_dht_line.conf";
422     test_name = "test-dht-line";
423     NUM_PEERS = 5;
424   }
425   else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
426   {
427     cfg_filename = "test_dht_line.conf";
428     test_name = "test-dht-twopeer";
429     NUM_PEERS = 2;
430   }
431   else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
432   {
433     cfg_filename = "test_dht_multipeer.conf";
434     test_name = "test-dht-multipeer";
435     NUM_PEERS = 10;
436   }
437   else
438   {
439     GNUNET_break (0);
440     return 1;
441   }
442   GNUNET_DHT_TEST_run (test_name,
443                        cfg_filename,
444                        NUM_PEERS,
445                        &run, NULL);
446   return ok;
447 }
448
449 /* end of test_dht_topo.c */