(no commit message)
[oweals/gnunet.git] / src / transport / test_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 transport/test_quota_compliance.c
22  * @brief base test case for transport implementations
23  *
24  * This test case tests quota compliance both on core and transport level
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
36
37 #define VERBOSE GNUNET_YES
38
39 #define VERBOSE_ARM GNUNET_NO
40
41 #define START_ARM GNUNET_YES
42 #define DEBUG_MEASUREMENT GNUNET_NO
43 #define DEBUG_CONNECTIONS GNUNET_NO
44
45 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
46 #define MEASUREMENT_MSG_SIZE 10000
47 #define MEASUREMENT_MSG_SIZE_BIG 32768
48 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
49 #define MEASUREMENT_MIN_QUOTA 1024 * 10
50 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
51 /**
52  * Testcase timeout
53  */
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
55
56
57
58 #define MTYPE 11111
59
60 struct PeerContext
61 {
62   struct GNUNET_CONFIGURATION_Handle *cfg;
63   struct GNUNET_TRANSPORT_Handle *th;
64   struct GNUNET_PeerIdentity id;
65 #if START_ARM
66   pid_t arm_pid;
67 #endif
68 };
69
70 /**
71  * Handle for a transmission-ready request.
72  */
73 struct GNUNET_TRANSPORT_TransmitHandle
74 {
75
76   /**
77    * Neighbour for this handle, NULL for control-traffic.
78    */
79   struct NeighbourList *neighbour;
80
81   /**
82    * Function to call when notify_size bytes are available
83    * for transmission.
84    */
85   GNUNET_CONNECTION_TransmitReadyNotify notify;
86
87   /**
88    * Closure for notify.
89    */
90   void *notify_cls;
91
92   /**
93    * transmit_ready task Id.  The task is used to introduce the
94    * artificial delay that may be required to maintain the bandwidth
95    * limits.  Later, this will be the ID of the "transmit_timeout"
96    * task which is used to signal a timeout if the transmission could
97    * not be done in a timely fashion.
98    */
99   GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
100
101   /**
102    * Timeout for this request.
103    */
104   struct GNUNET_TIME_Absolute timeout;
105
106   /**
107    * How many bytes is our notify callback waiting for?
108    */
109   size_t notify_size;
110
111   /**
112    * How important is this message?
113    */
114   unsigned int priority;
115
116 };
117
118 static struct PeerContext p1;
119
120 static struct PeerContext p2;
121
122 static struct GNUNET_SCHEDULER_Handle *sched;
123
124 static int ok;
125
126 static int connected;
127 static int measurement_running;
128 static int send_running;
129 static int recv_running;
130
131 static unsigned long long total_bytes;
132 static unsigned long long current_quota_p1;
133 static unsigned long long current_quota_p2;
134
135 static struct GNUNET_TIME_Absolute start_time;
136
137 static GNUNET_SCHEDULER_TaskIdentifier die_task;
138 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
139 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
140
141 struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
142
143 #if VERBOSE
144 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
145 #else
146 #define OKPP do { ok++; } while (0)
147 #endif
148
149
150
151 static void
152 end_send ()
153 {
154
155 }
156
157 static void
158 end ()
159 {
160   GNUNET_SCHEDULER_cancel (sched, die_task);
161   die_task = GNUNET_SCHEDULER_NO_TASK;
162
163   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
164   {
165             GNUNET_SCHEDULER_cancel (sched, measurement_task);
166             measurement_task = GNUNET_SCHEDULER_NO_TASK;
167   }
168   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
169   {
170             GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
171             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
172   }
173   GNUNET_SCHEDULER_shutdown (sched);
174 #if DEBUG_CONNECTIONS
175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
176 #endif
177   GNUNET_TRANSPORT_disconnect (p1.th);
178   GNUNET_TRANSPORT_disconnect (p2.th);
179 #if DEBUG_CONNECTIONS
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181               "Transports disconnected, returning success!\n");
182 #endif
183   GNUNET_SCHEDULER_shutdown (sched);
184 }
185
186
187
188 static void
189 stop_arm (struct PeerContext *p)
190 {
191 #if START_ARM
192   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
193     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
194   GNUNET_OS_process_wait (p->arm_pid);
195 #endif
196   GNUNET_CONFIGURATION_destroy (p->cfg);
197 }
198
199
200 static void
201 end_badly (void *cls,
202            const struct GNUNET_SCHEDULER_TaskContext *tc)
203 {
204   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
205   {
206             GNUNET_SCHEDULER_cancel (sched, measurement_task);
207             measurement_task = GNUNET_SCHEDULER_NO_TASK;
208   }
209   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
210   {
211             GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
212             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
213   }
214   GNUNET_break (0);
215   if (p1.th != NULL)
216           GNUNET_TRANSPORT_disconnect (p1.th);
217   if (p2.th != NULL)
218           GNUNET_TRANSPORT_disconnect (p2.th);
219   ok = 1;
220 }
221
222 struct TestMessage
223 {
224   struct GNUNET_MessageHeader header;
225   uint32_t num;
226 };
227
228 static unsigned int
229 get_size (unsigned int iter)
230 {
231   return MEASUREMENT_MSG_SIZE + sizeof (struct TestMessage);
232 }
233
234 static void
235 notify_receive_new (void *cls,
236                 const struct GNUNET_PeerIdentity *peer,
237                 const struct GNUNET_MessageHeader *message,
238                 struct GNUNET_TIME_Relative latency,
239                 uint32_t distance)
240 {
241   static int n;
242   unsigned int s;
243   const struct TestMessage *hdr;
244
245   hdr = (const struct TestMessage*) message;
246   s = get_size (n);
247   if (measurement_running == GNUNET_NO)
248           return;
249   if (MTYPE != ntohs (message->type))
250     return;
251 #if DEBUG_MEASUREMENT
252   if (ntohl(hdr->num) % 5000 == 0)
253     {
254       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255                   "Got message %u of size %u\n",
256                   ntohl (hdr->num),
257                   ntohs (message->size));
258     }
259 #endif
260   n++;
261 }
262
263 static size_t
264 notify_ready_new (void *cls, size_t size, void *buf)
265 {
266   static int n;
267   char *cbuf = buf;
268   struct TestMessage hdr;
269   unsigned int s;
270   unsigned int ret;
271
272   transmit_handle = NULL;
273
274   if (measurement_task == GNUNET_SCHEDULER_NO_TASK)
275           return 0;
276
277   if (buf == NULL)
278     {
279       ok = 42;
280       return 0;
281     }
282
283   if (measurement_running != GNUNET_YES)
284   {
285           send_running = GNUNET_NO;
286           end_send();
287           return 0;
288   }
289
290   send_running = GNUNET_YES;
291   ret = 0;
292   s = get_size (n);
293   GNUNET_assert (size >= s);
294   GNUNET_assert (buf != NULL);
295   cbuf = buf;
296   do
297     {
298       hdr.header.size = htons (s);
299       hdr.header.type = htons (MTYPE);
300       hdr.num = htonl (n);
301       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
302       ret += sizeof (struct TestMessage);
303       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
304       ret += s - sizeof (struct TestMessage);
305 #if DEBUG_MEASUREMENT
306       if (n % 5000 == 0)
307        {
308           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309                       "Sending message %u\n",n);
310        }
311 #endif
312       n++;
313       s = get_size (n);
314       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
315         break; /* sometimes pack buffer full, sometimes not */
316     }
317   while (size - ret >= s);
318   transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
319                                             &p1.id,
320                                             s, 0, SEND_TIMEOUT,
321                                             &notify_ready_new,
322                                             NULL);
323   total_bytes += s;
324   return ret;
325 }
326
327 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 );
328
329 static void measurement_counter
330  (void *cls,
331            const struct GNUNET_SCHEDULER_TaskContext *tc)
332 {
333   measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
334
335   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
336         return;
337
338 #if VERBOSE
339   fprintf(stderr,".");
340 #endif
341   measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
342                                                            GNUNET_TIME_UNIT_SECONDS,
343                                                            &measurement_counter,
344                                                            NULL);
345 }
346
347 static void
348 measurement_end (void *cls,
349            const struct GNUNET_SCHEDULER_TaskContext *tc)
350 {
351   measurement_task  = GNUNET_SCHEDULER_NO_TASK;
352   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
353         return;
354
355   measurement_running = GNUNET_NO;
356   struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
357
358
359   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
360   {
361     GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
362     measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
363   }
364 #if VERBOSE
365   fprintf(stderr,"\n");
366 #endif
367   /*
368   if (transmit_handle != NULL)
369   {
370           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
371           transmit_handle = NULL;
372   }
373   */
374   if ((total_bytes/(duration.rel_value / 1000)) > (current_quota_p1 + (current_quota_p1 / 10)))
375   {
376           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377                           "\nQuota compliance failed: \n"\
378                           "Quota allowed: %10llu kB/s\n"\
379                           "Throughput   : %10llu kB/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.rel_value / 1000)/1024));
380           ok = 1;
381 /*        end();
382           return;*/
383   }
384   else
385   {
386
387           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
388                           "\nQuota compliance ok: \n"\
389                           "Quota allowed: %10llu kB/s\n"\
390                           "Throughput   : %10llu kB/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.rel_value / 1000)/1024));
391           ok = 0;
392   }
393
394   if (current_quota_p1 < MEASUREMENT_MIN_QUOTA)
395   {
396           end();
397           return;
398   }
399   else
400   {
401 #if VERBOSE
402   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403               "Scheduling next measurement\n");
404 #endif
405         measure (current_quota_p1 / 10, current_quota_p2 / 10);
406   }
407 }
408
409 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
410 {
411           current_quota_p1 = quota_p1;
412           current_quota_p2 = quota_p2;
413 #if VERBOSE
414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415               "Starting transport level measurement for %u seconds and quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p1 / 1024);
416 #endif
417                 GNUNET_TRANSPORT_set_quota (p1.th,
418                           &p2.id,
419                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
420                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
421                           GNUNET_TIME_UNIT_FOREVER_REL,
422                           NULL, NULL);
423                 GNUNET_TRANSPORT_set_quota (p2.th,
424                           &p1.id,
425                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
426                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
427                           GNUNET_TIME_UNIT_FOREVER_REL,
428                           NULL, NULL);
429
430                 GNUNET_SCHEDULER_cancel (sched, die_task);
431                 die_task = GNUNET_SCHEDULER_add_delayed (sched,
432                                                    TIMEOUT,
433                                                    &end_badly,
434                                                    NULL);
435                 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
436                   GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
437                 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
438                                                                    GNUNET_TIME_UNIT_SECONDS,
439                                                                    &measurement_counter,
440                                                                    NULL);
441                 measurement_task = GNUNET_SCHEDULER_add_delayed (sched,
442                                                    MEASUREMENT_INTERVALL,
443                                                    &measurement_end,
444                                                    NULL);
445                 total_bytes = 0;
446                 measurement_running = GNUNET_YES;
447                 start_time = GNUNET_TIME_absolute_get ();
448
449                 if (transmit_handle != NULL)
450                           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
451                 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
452                                                                                           &p1.id,
453                                                                                           get_size (0), 0, SEND_TIMEOUT,
454                                                                                           &notify_ready_new,
455                                                                                           NULL);
456 }
457
458 static void
459 notify_connect (void *cls,
460                 const struct GNUNET_PeerIdentity *peer,
461                 struct GNUNET_TIME_Relative latency,
462                 uint32_t distance)
463 {
464   if (cls == &p1)
465     {
466 #if DEBUG_CONNECTIONS
467   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468               "Peer 1 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
469 #endif
470           connected++;
471     }
472   else
473     {
474 #if DEBUG_CONNECTIONS
475   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476               "Peer 2 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
477 #endif
478       connected++;
479     }
480   if (connected == 2)
481     {
482           measure(MEASUREMENT_MAX_QUOTA,MEASUREMENT_MAX_QUOTA);
483     }
484 }
485
486
487 static void
488 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
489 {
490 #if DEBUG_CONNECTIONS
491   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492               "Peer `%4s' disconnected (%p)!\n",
493               GNUNET_i2s (peer), cls);
494 #endif
495 }
496
497
498 static void
499 setup_peer (struct PeerContext *p, const char *cfgname)
500 {
501   p->cfg = GNUNET_CONFIGURATION_create ();
502 #if START_ARM
503   p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
504                                         "gnunet-service-arm",
505                                         "gnunet-service-arm",
506 #if VERBOSE_ARM
507                                         "-L", "DEBUG",
508 #endif
509                                         "-c", cfgname, NULL);
510 #endif
511
512   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
513   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
514                                     p,
515                                     &notify_receive_new,
516                                     &notify_connect,
517                                     &notify_disconnect);
518   GNUNET_assert (p->th != NULL);
519 }
520
521
522 static void
523 exchange_hello_last (void *cls,
524                      const struct GNUNET_MessageHeader *message)
525 {
526   struct PeerContext *me = cls;
527
528   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
529
530   GNUNET_assert (ok >= 3);
531   OKPP;
532   GNUNET_assert (message != NULL);
533   GNUNET_assert (GNUNET_OK ==
534                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
535                                       message, &me->id));
536   /* both HELLOs exchanged, get ready to test transmission! */
537 }
538
539
540 static void
541 exchange_hello (void *cls,
542                 const struct GNUNET_MessageHeader *message)
543 {
544   struct PeerContext *me = cls;
545
546   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
547   GNUNET_assert (ok >= 2);
548   OKPP;
549   GNUNET_assert (message != NULL);
550   GNUNET_assert (GNUNET_OK ==
551                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
552                                       message, &me->id));
553   GNUNET_TRANSPORT_offer_hello (p2.th, message);
554   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
555 }
556
557 static void
558 run (void *cls,
559      struct GNUNET_SCHEDULER_Handle *s,
560      char *const *args,
561      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
562 {
563   GNUNET_assert (ok == 1);
564   OKPP;
565   sched = s;
566
567   die_task = GNUNET_SCHEDULER_add_delayed (sched,
568                                            TIMEOUT,
569                                            &end_badly,
570                                            NULL);
571   measurement_running = GNUNET_NO;
572   send_running = GNUNET_NO;
573   recv_running = GNUNET_NO;
574
575   setup_peer (&p1, "test_quota_compliance_peer1.conf");
576   setup_peer (&p2, "test_quota_compliance_peer2.conf");
577
578   GNUNET_assert(p1.th != NULL);
579   GNUNET_assert(p2.th != NULL);
580   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
581 }
582
583 int
584 main (int argc, char *argv[])
585 {
586   int ret = 0;
587 #ifdef MINGW
588   return GNUNET_SYSERR;
589 #endif
590   GNUNET_log_setup ("test-quota-compliance",
591 #if VERBOSE
592                     "DEBUG",
593 #else
594                     "WARNING",
595 #endif
596                     NULL);
597   char *const argv1[] = { "test-quota-compliance",
598     "-c",
599     "test_quota_compliance_data.conf",
600 #if VERBOSE
601     "-L", "DEBUG",
602 #endif
603     NULL
604   };
605   struct GNUNET_GETOPT_CommandLineOption options[] = {
606     GNUNET_GETOPT_OPTION_END
607   };
608
609   ok = 1;
610   GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
611                       argv1, "test-quota-compliance", "nohelp",
612                       options, &run, &ok);
613   ret = ok;
614   stop_arm (&p1);
615   stop_arm (&p2);
616   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
617   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
618   return ret;
619 }
620
621 /* end of test_quota_compliance.c */