remove speed bumps
[oweals/gnunet.git] / src / core / test_core_api_reliability.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2015, 2016 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 core/test_core_api_reliability.c
22  * @brief testcase for core_api.c focusing on reliable transmission (with TCP)
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_transport_hello_service.h"
32 #include <gauger.h>
33
34 /**
35  * Note that this value must not significantly exceed
36  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
37  * messages may be dropped even for a reliable transport.
38  */
39 #define TOTAL_MSGS (600 * 10)
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
45
46 #define MTYPE 12345
47
48
49 static unsigned long long total_bytes;
50
51 static struct GNUNET_TIME_Absolute start_time;
52
53 static struct GNUNET_SCHEDULER_Task *err_task;
54
55
56 struct PeerContext
57 {
58   struct GNUNET_CONFIGURATION_Handle *cfg;
59   struct GNUNET_CORE_Handle *ch;
60   struct GNUNET_MQ_Handle *mq;
61   struct GNUNET_PeerIdentity id;
62   struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
63   struct GNUNET_MessageHeader *hello;
64   struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
65   struct GNUNET_ATS_ConnectivityHandle *ats;
66   struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
67   int connect_status;
68   struct GNUNET_OS_Process *arm_proc;
69 };
70
71 static struct PeerContext p1;
72
73 static struct PeerContext p2;
74
75 static int ok;
76
77 static int32_t tr_n;
78
79
80 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
81
82 struct TestMessage
83 {
84   struct GNUNET_MessageHeader header;
85   uint32_t num GNUNET_PACKED;
86 };
87
88
89 static unsigned int
90 get_size (unsigned int iter)
91 {
92   unsigned int ret;
93
94   if (iter < 60000)
95     return iter + sizeof (struct TestMessage);
96   ret = (iter * iter * iter);
97   return sizeof (struct TestMessage) + (ret % 60000);
98 }
99
100
101 static void
102 terminate_peer (struct PeerContext *p)
103 {
104   if (NULL != p->ch)
105   {
106     GNUNET_CORE_disconnect (p->ch);
107     p->ch = NULL;
108   }
109   if (NULL != p->ghh)
110   {
111     GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
112     p->ghh = NULL;
113   }
114   if (NULL != p->oh)
115   {
116     GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
117     p->oh = NULL;
118   }
119   if (NULL != p->ats_sh)
120   {
121     GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
122     p->ats_sh = NULL;
123   }
124   if (NULL != p->ats)
125   {
126     GNUNET_ATS_connectivity_done (p->ats);
127     p->ats = NULL;
128   }
129 }
130
131
132 static void
133 terminate_task_error (void *cls)
134 {
135   err_task = NULL;
136   GNUNET_break (0);
137   GNUNET_SCHEDULER_shutdown ();
138   ok = 42;
139 }
140
141
142 static void
143 do_shutdown (void *cls)
144 {
145   unsigned long long delta;
146
147   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
148   FPRINTF (stderr,
149            "\nThroughput was %llu kb/s\n",
150            total_bytes * 1000000LL / 1024 / delta);
151   GAUGER ("CORE",
152           "Core throughput/s",
153           total_bytes * 1000000LL / 1024 / delta,
154           "kb/s");
155   if (NULL != err_task)
156   {
157     GNUNET_SCHEDULER_cancel (err_task);
158     err_task = NULL;
159   }
160   terminate_peer (&p1);
161   terminate_peer (&p2);
162
163 }
164
165
166 static void
167 send_message (struct GNUNET_MQ_Handle *mq,
168               int32_t num)
169 {
170   struct GNUNET_MQ_Envelope *env;
171   struct TestMessage *hdr;
172   unsigned int s;
173
174   GNUNET_assert (NULL != mq);
175   GNUNET_assert (tr_n < TOTAL_MSGS);
176   s = get_size (tr_n);
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Sending message %u of size %u\n",
179               tr_n,
180               s);
181   env = GNUNET_MQ_msg_extra (hdr,
182                              s - sizeof (struct TestMessage),
183                              MTYPE);
184   hdr->num = htonl (tr_n);
185   memset (&hdr[1],
186           tr_n,
187           s - sizeof (struct TestMessage));
188   tr_n++;
189   GNUNET_SCHEDULER_cancel (err_task);
190   err_task =
191       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
192                                     &terminate_task_error,
193                                     NULL);
194   total_bytes += s;
195   GNUNET_MQ_send (mq,
196                   env);
197 }
198
199
200 static void *
201 connect_notify (void *cls,
202                 const struct GNUNET_PeerIdentity *peer,
203                 struct GNUNET_MQ_Handle *mq)
204 {
205   struct PeerContext *pc = cls;
206
207   if (0 == memcmp (&pc->id,
208                    peer,
209                    sizeof (struct GNUNET_PeerIdentity)))
210     return (void *) peer;
211   pc->mq = mq;
212   GNUNET_assert (0 == pc->connect_status);
213   pc->connect_status = 1;
214   if (pc == &p1)
215   {
216     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217                 "Encrypted connection established to peer `%s'\n",
218                 GNUNET_i2s (peer));
219     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220                 "Asking core (1) for transmission to peer `%s'\n",
221                 GNUNET_i2s (&p2.id));
222     GNUNET_SCHEDULER_cancel (err_task);
223     err_task =
224         GNUNET_SCHEDULER_add_delayed (TIMEOUT,
225                                       &terminate_task_error,
226                                       NULL);
227     start_time = GNUNET_TIME_absolute_get ();
228     send_message (mq,
229                   0);
230   }
231   return (void *) peer;
232 }
233
234
235 static void
236 disconnect_notify (void *cls,
237                    const struct GNUNET_PeerIdentity *peer,
238                    void *internal_cls)
239 {
240   struct PeerContext *pc = cls;
241
242   if (0 == memcmp (&pc->id,
243                    peer,
244                    sizeof (struct GNUNET_PeerIdentity)))
245     return;
246   pc->mq = NULL;
247   pc->connect_status = 0;
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249               "Encrypted connection to `%s' cut\n",
250               GNUNET_i2s (peer));
251 }
252
253
254 static int
255 check_test (void *cls,
256             const struct TestMessage *hdr)
257 {
258   return GNUNET_OK; /* accept all */
259 }
260
261
262 static void
263 handle_test (void *cls,
264              const struct TestMessage *hdr)
265 {
266   static int n;
267   unsigned int s;
268
269   s = get_size (n);
270   if (ntohs (hdr->header.size) != s)
271   {
272     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273                 "Expected message %u of size %u, got %u bytes of message %u\n",
274                 n,
275                 s,
276                 ntohs (hdr->header.size),
277                 ntohl (hdr->num));
278     GNUNET_SCHEDULER_cancel (err_task);
279     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
280                                          NULL);
281     return;
282   }
283   if (ntohl (hdr->num) != n)
284   {
285     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286                 "Expected message %u of size %u, got %u bytes of message %u\n",
287                 n,
288                 s,
289                 (unsigned int) ntohs (hdr->header.size),
290                 (unsigned int) ntohl (hdr->num));
291     GNUNET_SCHEDULER_cancel (err_task);
292     err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
293                                          NULL);
294     return;
295   }
296   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297               "Got message %u of size %u\n",
298               (unsigned int) ntohl (hdr->num),
299               (unsigned int) ntohs (hdr->header.size));
300   n++;
301   if (0 == (n % (TOTAL_MSGS / 100)))
302     FPRINTF (stderr,
303              "%s",
304              ".");
305   if (n == TOTAL_MSGS)
306   {
307     ok = 0;
308     GNUNET_SCHEDULER_shutdown ();
309   }
310   else
311   {
312     if (n == tr_n)
313     {
314       send_message (p1.mq,
315                     tr_n);
316     }
317   }
318 }
319
320
321 static void
322 init_notify (void *cls,
323              const struct GNUNET_PeerIdentity *my_identity)
324 {
325   struct PeerContext *p = cls;
326   struct GNUNET_MQ_MessageHandler handlers[] = {
327     GNUNET_MQ_hd_var_size (test,
328                            MTYPE,
329                            struct TestMessage,
330                            NULL),
331     GNUNET_MQ_handler_end ()
332   };
333
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Connection to CORE service of `%s' established\n",
336               GNUNET_i2s (my_identity));
337   p->id = *my_identity;
338   if (cls == &p1)
339   {
340     GNUNET_assert (ok == 2);
341     OKPP;
342     /* connect p2 */
343     GNUNET_assert (NULL !=
344                    (p2.ch = GNUNET_CORE_connect (p2.cfg,
345                                                  &p2,
346                                                  &init_notify,
347                                                  &connect_notify,
348                                                  &disconnect_notify,
349                                                  handlers)));
350   }
351   else
352   {
353     GNUNET_assert (ok == 3);
354     OKPP;
355     GNUNET_assert (cls == &p2);
356     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357                 "Asking transport (1) to connect to peer `%s'\n",
358                 GNUNET_i2s (&p2.id));
359     p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
360                                                  &p2.id,
361                                                  1);
362   }
363 }
364
365
366 static void
367 offer_hello_done (void *cls)
368 {
369   struct PeerContext *p = cls;
370
371   p->oh = NULL;
372 }
373
374
375 static void
376 process_hello (void *cls,
377                const struct GNUNET_MessageHeader *message)
378 {
379   struct PeerContext *p = cls;
380
381   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382               "Received (my) `%s' from transport service\n", "HELLO");
383   GNUNET_assert (message != NULL);
384   GNUNET_free_non_null (p->hello);
385   p->hello = GNUNET_copy_message (message);
386   if ((p == &p1) && (NULL == p2.oh))
387     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
388                                           message,
389                                           &offer_hello_done,
390                                           &p2);
391   if ((p == &p2) && (NULL == p1.oh))
392     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
393                                           message,
394                                           &offer_hello_done,
395                                           &p1);
396
397   if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh) )
398     p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
399                                           p2.hello,
400                                           &offer_hello_done,
401                                           &p1);
402   if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
403     p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
404                                           p1.hello,
405                                           &offer_hello_done,
406                                           &p2);
407 }
408
409
410 static void
411 setup_peer (struct PeerContext *p,
412             const char *cfgname)
413 {
414   char *binary;
415
416   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
417   p->cfg = GNUNET_CONFIGURATION_create ();
418   p->arm_proc
419     = GNUNET_OS_start_process (GNUNET_YES,
420                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
421                                NULL, NULL, NULL,
422                                binary,
423                                "gnunet-service-arm",
424                                "-c",
425                                cfgname,
426                                NULL);
427   GNUNET_assert (GNUNET_OK ==
428                  GNUNET_CONFIGURATION_load (p->cfg,
429                                             cfgname));
430   p->ats = GNUNET_ATS_connectivity_init (p->cfg);
431   GNUNET_assert (NULL != p->ats);
432   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
433                                        GNUNET_TRANSPORT_AC_ANY,
434                                        &process_hello,
435                                        p);
436   GNUNET_free (binary);
437 }
438
439
440 static void
441 run (void *cls,
442      char *const *args,
443      const char *cfgfile,
444      const struct GNUNET_CONFIGURATION_Handle *cfg)
445 {
446   struct GNUNET_MQ_MessageHandler handlers[] = {
447     GNUNET_MQ_hd_fixed_size (test,
448                              MTYPE,
449                              struct TestMessage,
450                              NULL),
451     GNUNET_MQ_handler_end ()
452   };
453
454   GNUNET_assert (ok == 1);
455   OKPP;
456   setup_peer (&p1,
457               "test_core_api_peer1.conf");
458   setup_peer (&p2,
459               "test_core_api_peer2.conf");
460   err_task =
461       GNUNET_SCHEDULER_add_delayed (TIMEOUT,
462                                     &terminate_task_error,
463                                     NULL);
464   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
465                                  NULL);
466
467   GNUNET_assert (NULL !=
468                  (p1.ch = GNUNET_CORE_connect (p1.cfg,
469                                                &p1,
470                                                &init_notify,
471                                                &connect_notify,
472                                                &disconnect_notify,
473                                                handlers)));
474 }
475
476
477 static void
478 stop_arm (struct PeerContext *p)
479 {
480   if (0 != GNUNET_OS_process_kill (p->arm_proc,
481                                    GNUNET_TERM_SIG))
482     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
483                          "kill");
484   if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
485     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
486                          "waitpid");
487   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488               "ARM process %u stopped\n",
489               GNUNET_OS_process_get_pid (p->arm_proc));
490   GNUNET_OS_process_destroy (p->arm_proc);
491   p->arm_proc = NULL;
492   GNUNET_CONFIGURATION_destroy (p->cfg);
493 }
494
495
496 int
497 main (int argc,
498       char *argv1[])
499 {
500   char *const argv[] = {
501     "test-core-api-reliability",
502     "-c",
503     "test_core_api_data.conf",
504     NULL
505   };
506   struct GNUNET_GETOPT_CommandLineOption options[] = {
507     GNUNET_GETOPT_OPTION_END
508   };
509   ok = 1;
510   GNUNET_log_setup ("test-core-api-reliability",
511                     "WARNING",
512                     NULL);
513   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
514                       argv,
515                       "test-core-api-reliability",
516                       "nohelp",
517                       options,
518                       &run,
519                       &ok);
520   stop_arm (&p1);
521   stop_arm (&p2);
522   GNUNET_free_non_null (p1.hello);
523   GNUNET_free_non_null (p2.hello);
524   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
525   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
526
527   return ok;
528 }
529
530 /* end of test_core_api_reliability.c */