3546e542cdffbf63c6fc5de258ee2d1beb1062ba
[oweals/gnunet.git] / src / core / test_core_quota_compliance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 core/test_core_quota_compliance.c
22  * @brief testcase for core_api.c focusing quota compliance on core level
23  *
24  * FIXME:
25  * - make sure connect callback is invoked properly as well!
26  */
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37
38 #define VERBOSE GNUNET_YES
39
40 #define START_ARM GNUNET_YES
41 #define DEBUG_CONNECTIONS GNUNET_NO
42
43 /**
44  * Note that this value must not significantly exceed
45  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
46  * messages may be dropped even for a reliable transport.
47  */
48 #define TOTAL_MSGS (600 * 10)
49
50 #define MEASUREMENT_MSG_SIZE 1024
51 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
52 #define MEASUREMENT_MIN_QUOTA 1024
53 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 8)
54
55 /**
56  * How long until we give up on transmitting the message?
57  */
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 6000)
59
60 /**
61  * What delay do we request from the core service for transmission?
62  * Any value smaller than the CORK delay will disable CORKing, which
63  * is what we want here.
64  */
65 #define FAST_TIMEOUT GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2)
66
67 #define MTYPE 12345
68
69 static int is_asymmetric_send_constant;
70 static int is_asymmetric_recv_constant;
71 static unsigned long long current_quota_p1_in;
72 static unsigned long long current_quota_p1_out;
73 static unsigned long long current_quota_p2_in;
74 static unsigned long long current_quota_p2_out;
75
76 static unsigned long long total_bytes;
77 static unsigned long long total_bytes_sent;
78
79 static struct GNUNET_TIME_Absolute start_time;
80
81 struct PeerContext
82 {
83   struct GNUNET_CONFIGURATION_Handle *cfg;
84   struct GNUNET_CORE_Handle *ch;
85   struct GNUNET_PeerIdentity id;   
86   struct GNUNET_TRANSPORT_Handle *th;
87   struct GNUNET_MessageHeader *hello;
88   int connect_status;
89 #if START_ARM
90   pid_t arm_pid;
91 #endif
92 };
93
94 static struct PeerContext p1;
95
96 static struct PeerContext p2;
97
98 static struct GNUNET_SCHEDULER_Handle *sched;
99
100 static int ok;
101 static int measurement_running;
102
103 struct GNUNET_CORE_TransmitHandle * ch;
104
105 #if VERBOSE
106 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
107 #else
108 #define OKPP do { ok++; } while (0)
109 #endif
110
111 struct TestMessage 
112 {
113   struct GNUNET_MessageHeader header;
114   uint32_t num;
115 };
116
117 static void
118 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 {
120   unsigned long long delta;
121
122   GNUNET_CORE_disconnect (p1.ch);
123   p1.ch = NULL;
124   GNUNET_CORE_disconnect (p2.ch);
125   p2.ch = NULL;
126   GNUNET_TRANSPORT_disconnect (p1.th);
127   p1.th = NULL;
128   GNUNET_TRANSPORT_disconnect (p2.th);
129   p2.th = NULL;
130   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
131   ok = 0;
132 }
133
134
135 static void
136 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   GNUNET_break (0);
139   GNUNET_CORE_disconnect (p1.ch);
140   p1.ch = NULL;
141   GNUNET_CORE_disconnect (p2.ch);
142   p2.ch = NULL;
143   GNUNET_TRANSPORT_disconnect (p1.th);
144   p1.th = NULL;
145   GNUNET_TRANSPORT_disconnect (p2.th);
146   p2.th = NULL;
147   ok = 42;
148 }
149
150 static void
151 connect_notify (void *cls,
152                 const struct GNUNET_PeerIdentity *peer,
153                 struct GNUNET_TIME_Relative latency,
154                 uint32_t distance)
155 {
156   struct PeerContext *pc = cls;
157   GNUNET_assert (pc->connect_status == 0);
158   pc->connect_status = 1;
159 #if DEBUG_CONNECTIONS
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161               "Encrypted connection established to peer `%4s'\n",
162               GNUNET_i2s (peer));
163 #endif
164 }
165
166
167 static void
168 disconnect_notify (void *cls,
169                    const struct GNUNET_PeerIdentity *peer)
170 {
171   struct PeerContext *pc = cls;
172   pc->connect_status = 0;
173 #if DEBUG_CONNECTIONS
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175               "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
176 #endif
177 }
178
179
180 static int
181 inbound_notify (void *cls,
182                 const struct GNUNET_PeerIdentity *other,
183                 const struct GNUNET_MessageHeader *message,
184                 struct GNUNET_TIME_Relative latency,
185                 uint32_t distance)
186 {
187 #if DEBUG_CONNECTIONS
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189               "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
190 #endif
191   return GNUNET_OK;
192 }
193
194
195 static int
196 outbound_notify (void *cls,
197                  const struct GNUNET_PeerIdentity *other,
198                  const struct GNUNET_MessageHeader *message,
199                  struct GNUNET_TIME_Relative latency,
200                  uint32_t distance)
201 {
202 #if DEBUG_CONNECTIONS
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Core notifies about outbound data for `%4s'.\n",
205               GNUNET_i2s (other));
206 #endif
207   return GNUNET_OK;
208 }
209
210
211 static GNUNET_SCHEDULER_TaskIdentifier err_task;
212
213 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
214
215
216 static void
217 measurement_end (void *cls,
218            const struct GNUNET_SCHEDULER_TaskContext *tc)
219 {
220           struct GNUNET_TIME_Relative duration;
221
222           measure_task  = GNUNET_SCHEDULER_NO_TASK;
223           if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
224                 return;
225           measurement_running = GNUNET_NO;
226
227           duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
228           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
229                           "\nQuota compliance: \n"\
230                           "Throughput   : %10llu kB/s\n"\
231                           "Quota                : %10llu kB/s\n", (total_bytes_sent/(duration.rel_value / 1000)/1024),current_quota_p1_in/1024);
232
233           if (err_task != GNUNET_SCHEDULER_NO_TASK)
234                   GNUNET_SCHEDULER_cancel (sched, err_task);
235       GNUNET_SCHEDULER_add_now (sched, &terminate_task, NULL);
236 }
237
238 static size_t
239 transmit_ready (void *cls, size_t size, void *buf);
240
241 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
242 {
243 #if VERBOSE
244   if ((is_asymmetric_send_constant == GNUNET_YES) || (is_asymmetric_recv_constant == GNUNET_YES))
245           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
246               "Starting core level measurement for %u seconds, receiving peer quota %llu kB/s, sending peer quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p1_in / 1024, current_quota_p2_out / 1024);
247   else
248           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249               "Starting core level measurement for %u seconds, symmetric quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p2_out / 1024);
250
251 #endif
252 #if DEBUG_CONNECTIONS
253   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254               "Asking core (1) for transmission to peer `%4s'\n",
255               GNUNET_i2s (&p2.id));
256 #endif
257   err_task = GNUNET_SCHEDULER_add_delayed (sched,
258                               TIMEOUT,
259                               &terminate_task_error,
260                               NULL);
261   measure_task = GNUNET_SCHEDULER_add_delayed (sched,
262                               MEASUREMENT_INTERVALL,
263                               &measurement_end,
264                               NULL);
265   start_time = GNUNET_TIME_absolute_get ();
266   measurement_running = GNUNET_YES;
267   total_bytes = 0;
268   total_bytes_sent = 0;
269   ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
270                                                0,
271                                                TIMEOUT,
272                                                &p2.id,
273                                                sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
274                                                &transmit_ready, &p1);
275 }
276
277 static int tr_n;
278
279
280 static int
281 process_mtype (void *cls,
282                const struct GNUNET_PeerIdentity *peer,
283                const struct GNUNET_MessageHeader *message,
284                struct GNUNET_TIME_Relative latency,
285                uint32_t distance)
286 {
287   static int n;
288   unsigned int s;
289   const struct TestMessage *hdr;
290
291   hdr = (const struct TestMessage*) message;
292   s = sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE;
293   if (MTYPE != ntohs (message->type))
294     return GNUNET_SYSERR;
295
296 #if DEBUG_CONNECTIONS
297   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298               "Got message %u of size %u\n",
299               ntohl (hdr->num),
300               ntohs (message->size));         
301 #endif
302   n++;
303   if (0 == (n % (TOTAL_MSGS/100)))
304     fprintf (stderr, ".");
305
306   return GNUNET_OK;
307 }
308
309
310 static struct GNUNET_CORE_MessageHandler handlers[] = {
311   {&process_mtype, MTYPE, 0},
312   {NULL, 0, 0}
313 };
314
315
316 static size_t
317 transmit_ready (void *cls, size_t size, void *buf)
318 {
319   char *cbuf = buf;
320   struct TestMessage hdr;
321   unsigned int s;
322   unsigned int ret;
323
324   if (measurement_running != GNUNET_YES)
325         return 0;
326
327   GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE); 
328   if (buf == NULL)
329     {
330       if (p1.ch != NULL)
331       {
332                 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
333                                                          0,
334                                                          FAST_TIMEOUT,
335                                                          &p2.id,
336                                                          sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
337                                                          &transmit_ready, &p1);
338                 GNUNET_break (NULL != ch);
339       }
340       return 0;
341     }
342   ret = 0;
343   ch = NULL;
344   s = sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE;
345
346   GNUNET_assert (size >= s);
347   GNUNET_assert (buf != NULL);
348   cbuf = buf;
349   do
350     {
351 #if DEBUG_CONNECTIONS
352       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353                   "Sending message %u of size %u at offset %u\n",
354                   tr_n,
355                   s,
356                   ret);
357 #endif
358       hdr.header.size = htons (s);
359       hdr.header.type = htons (MTYPE);
360       hdr.num = htonl (tr_n);
361       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
362       ret += sizeof (struct TestMessage);
363       memset (&cbuf[ret], tr_n, s - sizeof (struct TestMessage));
364       ret += s - sizeof (struct TestMessage);
365       tr_n++;
366       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
367         break; /* sometimes pack buffer full, sometimes not */
368     }
369   while (size - ret >= s);
370   GNUNET_SCHEDULER_cancel (sched, err_task);
371   err_task = GNUNET_SCHEDULER_add_delayed (sched,
372                                   TIMEOUT,
373                                   &terminate_task_error, 
374                                   NULL);
375
376   total_bytes += ret;
377   total_bytes_sent += ret;
378
379   ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
380                                                          0,
381                                                          FAST_TIMEOUT,
382                                                          &p2.id,
383                                                          sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
384                                                          &transmit_ready, &p1);
385   return ret;
386 }
387
388
389
390 static void
391 init_notify (void *cls,
392              struct GNUNET_CORE_Handle *server,
393              const struct GNUNET_PeerIdentity *my_identity,
394              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
395 {
396   struct PeerContext *p = cls;
397 #if DEBUG_CONNECTIONS
398   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399               "Connection to CORE service of `%4s' established\n",
400               GNUNET_i2s (my_identity));
401 #endif
402   GNUNET_assert (server != NULL);
403   p->id = *my_identity;
404   p->ch = server;
405   if (cls == &p1)
406     {
407       GNUNET_assert (ok == 2);
408       OKPP;
409       /* connect p2 */
410       GNUNET_CORE_connect (sched,
411                            p2.cfg,
412                            TIMEOUT,
413                            &p2,
414                            &init_notify,                         
415                            &connect_notify,
416                            &disconnect_notify,
417                            NULL,
418                            &inbound_notify,
419                            GNUNET_YES,
420                            &outbound_notify, GNUNET_YES, handlers);
421     }
422   else
423     {
424       GNUNET_assert (ok == 3);
425       OKPP;
426       GNUNET_assert (cls == &p2);
427
428       measure (MEASUREMENT_MIN_QUOTA, MEASUREMENT_MIN_QUOTA);
429     }
430 }
431
432
433 static void
434 process_hello (void *cls,
435                const struct GNUNET_MessageHeader *message)
436 {
437   struct PeerContext *p = cls;
438
439   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
440 #if DEBUG_CONNECTIONS
441   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442               "Received (my) `%s' from transport service\n",
443               "HELLO");
444 #endif
445   GNUNET_assert (message != NULL);
446   p->hello = GNUNET_malloc (ntohs (message->size));
447   memcpy (p->hello, message, ntohs (message->size));
448   if ((p == &p1) && (p2.th != NULL))
449     GNUNET_TRANSPORT_offer_hello (p2.th, message);
450   if ((p == &p2) && (p1.th != NULL))
451     GNUNET_TRANSPORT_offer_hello (p1.th, message);
452
453   if ((p == &p1) && (p2.hello != NULL))
454     GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
455   if ((p == &p2) && (p1.hello != NULL))
456     GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
457 }
458
459
460
461 static void
462 setup_peer (struct PeerContext *p, const char *cfgname)
463 {
464   p->cfg = GNUNET_CONFIGURATION_create ();
465 #if START_ARM
466   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
467                                         "gnunet-service-arm",
468 #if VERBOSE
469                                         "-L", "DEBUG",
470 #endif
471                                         "-c", cfgname, NULL);
472 #endif
473   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
474   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL, p, NULL, NULL, NULL);
475   GNUNET_assert (p->th != NULL);
476   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
477 }
478
479
480 static void
481 run (void *cls,
482      struct GNUNET_SCHEDULER_Handle *s,
483      char *const *args,
484      const char *cfgfile,
485      const struct GNUNET_CONFIGURATION_Handle *cfg)
486 {
487   GNUNET_assert (ok == 1);
488   OKPP;
489   sched = s;
490   setup_peer (&p1, "test_core_quota_peer1.conf");
491   setup_peer (&p2, "test_core_quota_peer2.conf");
492   GNUNET_CORE_connect (sched,
493                        p1.cfg,
494                        TIMEOUT,
495                        &p1,
496                        &init_notify,
497                        &connect_notify,
498                        &disconnect_notify,
499                        NULL,
500                        &inbound_notify,
501                        GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
502
503   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
504                                          "CORE",
505                                          "TOTAL_QUOTA_IN",
506                                          &current_quota_p1_in));
507   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
508                                          "CORE",
509                                          "TOTAL_QUOTA_IN",
510                                          &current_quota_p2_in));
511   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
512                                          "CORE",
513                                          "TOTAL_QUOTA_OUT",
514                                          &current_quota_p1_out));
515   GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
516                                          "CORE",
517                                          "TOTAL_QUOTA_OUT",
518                                          &current_quota_p2_out));
519 }
520
521
522 static void
523 stop_arm (struct PeerContext *p)
524 {
525 #if START_ARM
526   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
527     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
528   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
529     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
530   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531               "ARM process %u stopped\n", p->arm_pid);
532 #endif
533   GNUNET_CONFIGURATION_destroy (p->cfg);
534 }
535
536 static int
537 check ()
538 {
539   char *const argv[] = { "test-core-quota-compliance",
540     "-c",
541     "test_core_api_data.conf",
542 #if VERBOSE
543     "-L", "DEBUG",
544 #endif
545     NULL
546   };
547   struct GNUNET_GETOPT_CommandLineOption options[] = {
548     GNUNET_GETOPT_OPTION_END
549   };
550   ok = 1;
551   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
552                       argv, "test_core_quota_compliance", "nohelp", options, &run, &ok);
553   stop_arm (&p1);
554   stop_arm (&p2);
555   return ok;
556 }
557
558 int
559 main (int argc, char *argv[])
560 {
561   int ret;
562
563   GNUNET_log_setup ("test-core-api",
564 #if VERBOSE
565                     "DEBUG",
566 #else
567                     "WARNING",
568 #endif
569                     NULL);
570   ret = check ();
571   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-peer-2");
572   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-peer-2");
573
574   return ret;
575 }
576
577 /* end of test_core_quota_compliance.c */