c9036d2c192d60713979c711267f737605eba4d2
[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                                         &handle_stats,
243                                         &stats_finished,
244                                         ctx);
245 }
246
247
248 /**
249  * Iterator called on each result obtained for a DHT
250  * operation that expects a reply
251  *
252  * @param cls closure with our 'struct GetOperation'
253  * @param exp when will this value expire
254  * @param key key of the result
255  * @param get_path peers on reply path (or NULL if not recorded)
256  * @param get_path_length number of entries in get_path
257  * @param put_path peers on the PUT path (or NULL if not recorded)
258  * @param put_path_length number of entries in get_path
259  * @param type type of the result
260  * @param size number of bytes in data
261  * @param data pointer to the result data
262  */
263 static void
264 dht_get_handler (void *cls, struct GNUNET_TIME_Absolute exp,
265                  const struct GNUNET_HashCode * key,
266                  const struct GNUNET_PeerIdentity *get_path,
267                  unsigned int get_path_length,
268                  const struct GNUNET_PeerIdentity *put_path,
269                  unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
270                  size_t size, const void *data)
271 {
272   struct GetOperation *get_op = cls;
273   struct GNUNET_HashCode want;
274   struct GNUNET_DHT_TestContext *ctx;
275
276   if (sizeof (struct GNUNET_HashCode) != size)
277   {
278     GNUNET_break (0);
279     return;
280   }
281   GNUNET_CRYPTO_hash (key, sizeof (*key), &want);
282   if (0 != memcmp (&want, data, sizeof (want)))
283   {
284     GNUNET_break (0);
285     return;
286   }
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
288               "Get successful\n");
289 #if 0
290   {
291     int i;
292
293     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH: (get %u, put %u)\n",
294                 get_path_length, put_path_length);
295     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  LOCAL\n");
296     for (i = get_path_length - 1; i >= 0; i--)
297       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %s\n",
298                   GNUNET_i2s (&get_path[i]));
299     for (i = put_path_length - 1; i >= 0; i--)
300       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %s\n",
301                   GNUNET_i2s (&put_path[i]));
302   }
303 #endif
304   GNUNET_DHT_get_stop (get_op->get);
305   GNUNET_CONTAINER_DLL_remove (get_head,
306                                get_tail,
307                                get_op);
308   GNUNET_free (get_op);
309   if (NULL != get_head)
310     return;
311   /* all DHT GET operations successful; terminate! */
312   ok = 0;
313   ctx = GNUNET_SCHEDULER_cancel (timeout_task);
314   timeout_task = GNUNET_SCHEDULER_add_now (&shutdown_task, ctx);
315 }
316
317
318 /**
319  * Task to put the id of each peer into the DHT.
320  * 
321  * @param cls array with NUM_PEERS DHT handles
322  * @param tc Task context
323  */
324 static void
325 do_puts (void *cls,
326          const struct GNUNET_SCHEDULER_TaskContext *tc)
327 {
328   struct GNUNET_DHT_Handle **hs = cls;
329   struct GNUNET_HashCode key;
330   struct GNUNET_HashCode value;
331   unsigned int i;
332
333   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
334               "Putting values into DHT\n");
335   for (i = 0; i < NUM_PEERS; i++)
336   {
337     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
338     GNUNET_CRYPTO_hash (&key, sizeof (key), &value);
339     GNUNET_DHT_put (hs[i], &key, 10U,
340                     GNUNET_DHT_RO_RECORD_ROUTE |
341                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
342                     GNUNET_BLOCK_TYPE_TEST, 
343                     sizeof (value), &value, 
344                     GNUNET_TIME_UNIT_FOREVER_ABS,
345                     GNUNET_TIME_UNIT_FOREVER_REL, 
346                     NULL, NULL);
347   }
348   put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, 
349                                            &do_puts, hs);
350 }
351
352
353 /**
354  * Main function of the test.
355  *
356  * @param cls closure (NULL)
357  * @param ctx argument to give to GNUNET_DHT_TEST_cleanup on test end
358  * @param num_peers number of peers that are running
359  * @param peers array of peers
360  * @param dhts handle to each of the DHTs of the peers
361  */
362 static void
363 run (void *cls,
364      struct GNUNET_DHT_TEST_Context *ctx,
365      unsigned int num_peers,
366      struct GNUNET_TESTBED_Peer **peers,
367      struct GNUNET_DHT_Handle **dhts)
368 {
369   unsigned int i;
370   unsigned int j;
371   struct GNUNET_HashCode key;
372   struct GetOperation *get_op;
373
374   GNUNET_assert (NUM_PEERS == num_peers);
375   my_peers = peers;
376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
377               "Peers setup, starting test\n");
378   put_task = GNUNET_SCHEDULER_add_now (&do_puts, dhts);
379   for (i=0;i<num_peers;i++)
380   {
381     GNUNET_CRYPTO_hash (&i, sizeof (i), &key);
382     for (j=0;j<num_peers;j++)
383     {
384       get_op = GNUNET_malloc (sizeof (struct GetOperation));
385       GNUNET_CONTAINER_DLL_insert (get_head,
386                                    get_tail,
387                                    get_op);
388       get_op->get = GNUNET_DHT_get_start (dhts[j], 
389                                           GNUNET_BLOCK_TYPE_TEST, /* type */
390                                           &key,      /*key to search */
391                                           4U,     /* replication level */
392                                           GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
393                                           NULL,        /* xquery */
394                                           0,      /* xquery bits */
395                                           &dht_get_handler, get_op);
396     }
397   }
398   timeout_task = GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, 
399                                                &shutdown_task, ctx);
400 }
401
402
403 /**
404  * Main: start test
405  */
406 int
407 main (int xargc, char *xargv[])
408 {
409   const char *cfg_filename;
410   const char *test_name;
411   
412   if (NULL != strstr (xargv[0], "test_dht_2dtorus"))
413   {
414     cfg_filename = "test_dht_2dtorus.conf";
415     test_name = "test-dht-2dtorus";
416     NUM_PEERS = 16;
417   }
418   else if (NULL != strstr (xargv[0], "test_dht_line"))
419   {
420     cfg_filename = "test_dht_line.conf"; 
421     test_name = "test-dht-line";
422     NUM_PEERS = 5;
423   }
424   else if (NULL != strstr (xargv[0], "test_dht_twopeer"))
425   {
426     cfg_filename = "test_dht_line.conf"; 
427     test_name = "test-dht-twopeer";
428     NUM_PEERS = 2;
429   }
430   else if (NULL != strstr (xargv[0], "test_dht_multipeer"))
431   {
432     cfg_filename = "test_dht_multipeer.conf"; 
433     test_name = "test-dht-multipeer";
434     NUM_PEERS = 10;
435   }
436   else
437   {
438     GNUNET_break (0);
439     return 1;
440   }
441   GNUNET_DHT_TEST_run (test_name,
442                        cfg_filename,
443                        NUM_PEERS,
444                        &run, NULL);
445   return ok;
446 }
447
448 /* end of test_dht_topo.c */