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