uncrustify as demanded.
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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    * DLL.
49    */
50   struct GetOperation *next;
51
52   /**
53    * DLL.
54    */
55   struct GetOperation *prev;
56
57   /**
58    * Handle for the operation.
59    */
60   struct GNUNET_DHT_GetHandle *get;
61 };
62
63
64 /**
65  * Result of the test.
66  */
67 static int ok = 1;
68
69 /**
70  * Task to do DHT_puts
71  */
72 static struct GNUNET_SCHEDULER_Task *put_task;
73
74 /**
75  * Task to do DHT_gets
76  */
77 static struct GNUNET_SCHEDULER_Task *get_task;
78
79 /**
80  * Task to time out / regular shutdown.
81  */
82 static struct GNUNET_SCHEDULER_Task *timeout_task;
83
84 /**
85  * Head of list of active GET operations.
86  */
87 static struct GetOperation *get_head;
88
89 /**
90  * Tail of list of active GET operations.
91  */
92 static struct GetOperation *get_tail;
93
94 /**
95  * Array of the testbed's peers.
96  */
97 static struct GNUNET_TESTBED_Peer **my_peers;
98
99 /**
100  * Number of peers to run.
101  */
102 static unsigned int NUM_PEERS;
103
104
105 /**
106  * Statistics we print out.
107  */
108 static struct {
109   const char *subsystem;
110   const char *name;
111   unsigned long long total;
112 } stats[] = {
113   { "core", "# bytes decrypted", 0 },
114   { "core", "# bytes encrypted", 0 },
115   { "core", "# type maps received", 0 },
116   { "core", "# session keys confirmed via PONG", 0 },
117   { "core", "# peers connected", 0 },
118   { "core", "# key exchanges initiated", 0 },
119   { "core", "# send requests dropped (disconnected)", 0 },
120   { "core", "# transmissions delayed due to corking", 0 },
121   { "core", "# messages discarded (expired prior to transmission)", 0 },
122   { "core", "# messages discarded (disconnected)", 0 },
123   { "core", "# discarded CORE_SEND requests", 0 },
124   { "core", "# discarded lower priority CORE_SEND requests", 0 },
125   { "transport", "# bytes received via TCP", 0 },
126   { "transport", "# bytes transmitted via TCP", 0 },
127   { "dht", "# PUT messages queued for transmission", 0 },
128   { "dht", "# P2P PUT requests received", 0 },
129   { "dht", "# GET messages queued for transmission", 0 },
130   { "dht", "# P2P GET requests received", 0 },
131   { "dht", "# RESULT messages queued for transmission", 0 },
132   { "dht", "# P2P RESULTS received", 0 },
133   { "dht", "# Queued messages discarded (peer disconnected)", 0 },
134   { "dht", "# Peers excluded from routing due to Bloomfilter", 0 },
135   { "dht", "# Peer selection failed", 0 },
136   { "dht", "# FIND PEER requests ignored due to Bloomfilter", 0 },
137   { "dht", "# FIND PEER requests ignored due to lack of HELLO", 0 },
138   { "dht", "# P2P FIND PEER requests processed", 0 },
139   { "dht", "# P2P GET requests ONLY routed", 0 },
140   { "dht", "# Preference updates given to core", 0 },
141   { "dht", "# REPLIES ignored for CLIENTS (no match)", 0 },
142   { "dht", "# GET requests from clients injected", 0 },
143   { "dht", "# GET requests received from clients", 0 },
144   { "dht", "# GET STOP requests received from clients", 0 },
145   { "dht", "# ITEMS stored in datacache", 0 },
146   { "dht", "# Good RESULTS found in datacache", 0 },
147   { "dht", "# GET requests given to datacache", 0 },
148   { NULL, NULL, 0 }
149 };
150
151
152 static struct GNUNET_DHT_TEST_Context *
153 stop_ops()
154 {
155   struct GetOperation *get_op;
156   struct GNUNET_DHT_TEST_Context *ctx = NULL;
157
158   if (NULL != timeout_task)
159     {
160       ctx = GNUNET_SCHEDULER_cancel(timeout_task);
161       timeout_task = NULL;
162     }
163   if (NULL != put_task)
164     {
165       GNUNET_SCHEDULER_cancel(put_task);
166       put_task = NULL;
167     }
168   if (NULL != get_task)
169     {
170       GNUNET_SCHEDULER_cancel(get_task);
171       get_task = NULL;
172     }
173   while (NULL != (get_op = get_tail))
174     {
175       GNUNET_DHT_get_stop(get_op->get);
176       GNUNET_CONTAINER_DLL_remove(get_head,
177                                   get_tail,
178                                   get_op);
179       GNUNET_free(get_op);
180     }
181   return ctx;
182 }
183
184
185 /**
186  * Function called once we're done processing stats.
187  *
188  * @param cls the test context
189  * @param op the stats operation
190  * @param emsg error message on failure
191  */
192 static void
193 stats_finished(void *cls,
194                struct GNUNET_TESTBED_Operation *op,
195                const char *emsg)
196 {
197   struct GNUNET_DHT_TEST_Context *ctx = cls;
198   unsigned int i;
199
200   if (NULL != op)
201     GNUNET_TESTBED_operation_done(op);
202   if (NULL != emsg)
203     {
204       fprintf(stderr,
205               _("Gathering statistics failed: %s\n"),
206               emsg);
207       GNUNET_SCHEDULER_cancel(put_task);
208       GNUNET_DHT_TEST_cleanup(ctx);
209       return;
210     }
211   for (i = 0; NULL != stats[i].name; i++)
212     fprintf(stderr,
213             "%6s/%60s = %12llu\n",
214             stats[i].subsystem,
215             stats[i].name,
216             stats[i].total);
217   GNUNET_DHT_TEST_cleanup(ctx);
218   GNUNET_SCHEDULER_shutdown();
219 }
220
221
222 /**
223  * Function called to process statistic values from all peers.
224  *
225  * @param cls closure
226  * @param peer the peer the statistic belong to
227  * @param subsystem name of subsystem that created the statistic
228  * @param name the name of the datum
229  * @param value the current value
230  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
231  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
232  */
233 static int
234 handle_stats(void *cls,
235              const struct GNUNET_TESTBED_Peer *peer,
236              const char *subsystem,
237              const char *name,
238              uint64_t value,
239              int is_persistent)
240 {
241   unsigned int i;
242
243   for (i = 0; NULL != stats[i].name; i++)
244     if ((0 == strcasecmp(subsystem,
245                          stats[i].subsystem)) &&
246         (0 == strcasecmp(name,
247                          stats[i].name)))
248       stats[i].total += value;
249   return GNUNET_OK;
250 }
251
252
253 /**
254  * Task run on shutdown to clean up.  Terminates active get operations
255  * and shuts down the testbed.
256  *
257  * @param cls the 'struct GNUNET_DHT_TestContext'
258  */
259 static void
260 shutdown_task(void *cls)
261 {
262   (void)stop_ops();
263 }
264
265
266 /**
267  * Task run on timeout to clean up.  Terminates active get operations
268  * and shuts down the testbed.
269  *
270  * @param cls the `struct GNUNET_DHT_TestContext`
271  */
272 static void
273 timeout_cb(void *cls)
274 {
275   timeout_task = NULL;
276   GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
277              "Timeout\n");
278   GNUNET_SCHEDULER_shutdown();
279 }
280
281
282 /**
283  * Iterator called on each result obtained for a DHT
284  * operation that expects a reply
285  *
286  * @param cls closure with our 'struct GetOperation'
287  * @param exp when will this value expire
288  * @param key key of the result
289  * @param get_path peers on reply path (or NULL if not recorded)
290  * @param get_path_length number of entries in @a get_path
291  * @param put_path peers on the PUT path (or NULL if not recorded)
292  * @param put_path_length number of entries in @a put_path
293  * @param type type of the result
294  * @param size number of bytes in @a data
295  * @param data pointer to the result data
296  */
297 static void
298 dht_get_handler(void *cls,
299                 struct GNUNET_TIME_Absolute exp,
300                 const struct GNUNET_HashCode *key,
301                 const struct GNUNET_PeerIdentity *get_path,
302                 unsigned int get_path_length,
303                 const struct GNUNET_PeerIdentity *put_path,
304                 unsigned int put_path_length,
305                 enum GNUNET_BLOCK_Type type,
306                 size_t size,
307                 const void *data)
308 {
309   struct GetOperation *get_op = cls;
310   struct GNUNET_HashCode want;
311   struct GNUNET_DHT_TEST_Context *ctx;
312
313   if (sizeof(struct GNUNET_HashCode) != size)
314     {
315       GNUNET_break(0);
316       return;
317     }
318   GNUNET_CRYPTO_hash(key,
319                      sizeof(*key),
320                      &want);
321   if (0 != memcmp(&want,
322                   data,
323                   sizeof(want)))
324     {
325       GNUNET_break(0);
326       return;
327     }
328   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
329              "Get successful\n");
330 #if 0
331   {
332     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
333                "PATH: (get %u, put %u)\n",
334                get_path_length,
335                put_path_length);
336     GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
337                "  LOCAL\n");
338     for (int i = get_path_length - 1; i >= 0; i--)
339       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
340                  "  %s\n",
341                  GNUNET_i2s(&get_path[i]));
342     for (int i = put_path_length - 1; i >= 0; i--)
343       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
344                  "  %s\n",
345                  GNUNET_i2s(&put_path[i]));
346   }
347 #endif
348   GNUNET_DHT_get_stop(get_op->get);
349   GNUNET_CONTAINER_DLL_remove(get_head,
350                               get_tail,
351                               get_op);
352   GNUNET_free(get_op);
353   if (NULL != get_head)
354     return;
355   /* all DHT GET operations successful; get stats! */
356   GNUNET_log(GNUNET_ERROR_TYPE_INFO,
357              "All DHT operations successful. Obtaining stats!\n");
358   ok = 0;
359   ctx = stop_ops();
360   GNUNET_assert(NULL != ctx);
361   (void)GNUNET_TESTBED_get_statistics(NUM_PEERS,
362                                       my_peers,
363                                       NULL, NULL,
364                                       &handle_stats,
365                                       &stats_finished,
366                                       ctx);
367 }
368
369
370 /**
371  * Task to put the id of each peer into the DHT.
372  *
373  * @param cls array with NUM_PEERS DHT handles
374  * @param tc Task context
375  */
376 static void
377 do_puts(void *cls)
378 {
379   struct GNUNET_DHT_Handle **hs = cls;
380   struct GNUNET_HashCode key;
381   struct GNUNET_HashCode value;
382
383   put_task = NULL;
384   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
385              "Putting values into DHT\n");
386   for (unsigned int i = 0; i < NUM_PEERS; i++)
387     {
388       GNUNET_CRYPTO_hash(&i,
389                          sizeof(i),
390                          &key);
391       GNUNET_CRYPTO_hash(&key,
392                          sizeof(key),
393                          &value);
394       GNUNET_DHT_put(hs[i],
395                      &key,
396                      10U,
397                      GNUNET_DHT_RO_RECORD_ROUTE |
398                      GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
399                      GNUNET_BLOCK_TYPE_TEST,
400                      sizeof(value),
401                      &value,
402                      GNUNET_TIME_UNIT_FOREVER_ABS,
403                      NULL,
404                      NULL);
405     }
406   put_task = GNUNET_SCHEDULER_add_delayed(PUT_FREQUENCY,
407                                           &do_puts,
408                                           hs);
409 }
410
411
412 /**
413  * Start GET operations.
414  */
415 static void
416 start_get(void *cls)
417 {
418   struct GNUNET_DHT_Handle **dhts = cls;
419   unsigned int i;
420   unsigned int j;
421   struct GNUNET_HashCode key;
422   struct GetOperation *get_op;
423
424   get_task = NULL;
425   for (i = 0; i < NUM_PEERS; i++)
426     {
427       GNUNET_CRYPTO_hash(&i, sizeof(i), &key);
428       for (j = 0; j < NUM_PEERS; j++)
429         {
430           get_op = GNUNET_new(struct GetOperation);
431           GNUNET_CONTAINER_DLL_insert(get_head,
432                                       get_tail,
433                                       get_op);
434           get_op->get = GNUNET_DHT_get_start(dhts[j],
435                                              GNUNET_BLOCK_TYPE_TEST, /* type */
436                                              &key,   /*key to search */
437                                              4U,  /* replication level */
438                                              GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
439                                              NULL,     /* xquery */
440                                              0,   /* xquery bits */
441                                              &dht_get_handler,
442                                              get_op);
443         }
444     }
445 }
446
447
448 /**
449  * Main function of the test.
450  *
451  * @param cls closure (NULL)
452  * @param ctx argument to give to #GNUNET_DHT_TEST_cleanup on test end
453  * @param num_peers number of @a peers that are running
454  * @param peers array of peers
455  * @param dhts handle to each of the DHTs of the peers
456  */
457 static void
458 run(void *cls,
459     struct GNUNET_DHT_TEST_Context *ctx,
460     unsigned int num_peers,
461     struct GNUNET_TESTBED_Peer **peers,
462     struct GNUNET_DHT_Handle **dhts)
463 {
464   GNUNET_assert(NUM_PEERS == num_peers);
465   my_peers = peers;
466   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
467              "Peers setup, starting test\n");
468   put_task = GNUNET_SCHEDULER_add_now(&do_puts,
469                                       dhts);
470   get_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
471                                           &start_get,
472                                           dhts);
473   timeout_task = GNUNET_SCHEDULER_add_delayed(GET_TIMEOUT,
474                                               &timeout_cb,
475                                               ctx);
476   GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
477                                 ctx);
478 }
479
480
481 /**
482  * Main: start test
483  */
484 int
485 main(int xargc, char *xargv[])
486 {
487   const char *cfg_filename;
488   const char *test_name;
489
490   if (NULL != strstr(xargv[0], "test_dht_2dtorus"))
491     {
492       cfg_filename = "test_dht_2dtorus.conf";
493       test_name = "test-dht-2dtorus";
494       NUM_PEERS = 16;
495     }
496   else if (NULL != strstr(xargv[0], "test_dht_line"))
497     {
498       cfg_filename = "test_dht_line.conf";
499       test_name = "test-dht-line";
500       NUM_PEERS = 5;
501     }
502   else if (NULL != strstr(xargv[0], "test_dht_twopeer"))
503     {
504       cfg_filename = "test_dht_line.conf";
505       test_name = "test-dht-twopeer";
506       NUM_PEERS = 2;
507     }
508   else if (NULL != strstr(xargv[0], "test_dht_multipeer"))
509     {
510       cfg_filename = "test_dht_multipeer.conf";
511       test_name = "test-dht-multipeer";
512       NUM_PEERS = 10;
513     }
514   else
515     {
516       GNUNET_break(0);
517       return 1;
518     }
519   GNUNET_DHT_TEST_run(test_name,
520                       cfg_filename,
521                       NUM_PEERS,
522                       &run, NULL);
523   return ok;
524 }
525
526 /* end of test_dht_topo.c */