5a41f20ca0ca9f86429549b0cd916e4f0042c088
[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, 35)
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 int is_tcp;
136 static int is_tcp_nat;
137 static int is_http;
138 static int is_https;
139 static int is_udp;
140
141 static struct GNUNET_TIME_Absolute start_time;
142
143 static GNUNET_SCHEDULER_TaskIdentifier die_task;
144 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
145 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
146
147 struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
148
149 #if VERBOSE
150 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
151 #else
152 #define OKPP do { ok++; } while (0)
153 #endif
154
155
156
157 static void
158 end_send ()
159 {
160
161 }
162
163 static void
164 end ()
165 {
166   GNUNET_SCHEDULER_cancel (sched, die_task);
167   die_task = GNUNET_SCHEDULER_NO_TASK;
168
169   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
170   {
171             GNUNET_SCHEDULER_cancel (sched, measurement_task);
172             measurement_task = GNUNET_SCHEDULER_NO_TASK;
173   }
174   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
175   {
176             GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
177             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
178   }
179   GNUNET_SCHEDULER_shutdown (sched);
180 #if DEBUG_CONNECTIONS
181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
182 #endif
183   GNUNET_TRANSPORT_disconnect (p1.th);
184   GNUNET_TRANSPORT_disconnect (p2.th);
185 #if DEBUG_CONNECTIONS
186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187               "Transports disconnected, returning success!\n");
188 #endif
189   GNUNET_SCHEDULER_shutdown (sched);
190 }
191
192
193
194 static void
195 stop_arm (struct PeerContext *p)
196 {
197 #if START_ARM
198   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
199     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
200   GNUNET_OS_process_wait (p->arm_pid);
201 #endif
202   GNUNET_CONFIGURATION_destroy (p->cfg);
203 }
204
205
206 static void
207 end_badly (void *cls,
208            const struct GNUNET_SCHEDULER_TaskContext *tc)
209 {
210   if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
211   {
212             GNUNET_SCHEDULER_cancel (sched, measurement_task);
213             measurement_task = GNUNET_SCHEDULER_NO_TASK;
214   }
215   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
216   {
217             GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
218             measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
219   }
220   GNUNET_break (0);
221   if (p1.th != NULL)
222           GNUNET_TRANSPORT_disconnect (p1.th);
223   if (p2.th != NULL)
224           GNUNET_TRANSPORT_disconnect (p2.th);
225   ok = 1;
226 }
227
228 struct TestMessage
229 {
230   struct GNUNET_MessageHeader header;
231   uint32_t num;
232 };
233
234 static unsigned int
235 get_size (unsigned int iter)
236 {
237   return MEASUREMENT_MSG_SIZE + sizeof (struct TestMessage);
238 }
239
240 static void
241 notify_receive_new (void *cls,
242                 const struct GNUNET_PeerIdentity *peer,
243                 const struct GNUNET_MessageHeader *message,
244                 struct GNUNET_TIME_Relative latency,
245                 uint32_t distance)
246 {
247   static int n;
248   unsigned int s;
249   const struct TestMessage *hdr;
250
251   hdr = (const struct TestMessage*) message;
252   s = get_size (n);
253   if (measurement_running == GNUNET_NO)
254           return;
255   if (MTYPE != ntohs (message->type))
256     return;
257 #if DEBUG_MEASUREMENT
258   if (ntohl(hdr->num) % 5000 == 0)
259     {
260       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261                   "Got message %u of size %u\n",
262                   ntohl (hdr->num),
263                   ntohs (message->size));
264     }
265 #endif
266   n++;
267 }
268
269 static size_t
270 notify_ready_new (void *cls, size_t size, void *buf)
271 {
272   static int n;
273   char *cbuf = buf;
274   struct TestMessage hdr;
275   unsigned int s;
276   unsigned int ret;
277
278   transmit_handle = NULL;
279
280   if (measurement_task == GNUNET_SCHEDULER_NO_TASK)
281           return 0;
282
283   if (buf == NULL)
284     {
285       ok = 42;
286       return 0;
287     }
288
289   if (measurement_running != GNUNET_YES)
290   {
291           send_running = GNUNET_NO;
292           end_send();
293           return 0;
294   }
295
296   send_running = GNUNET_YES;
297   ret = 0;
298   s = get_size (n);
299   GNUNET_assert (size >= s);
300   GNUNET_assert (buf != NULL);
301   cbuf = buf;
302   do
303     {
304       hdr.header.size = htons (s);
305       hdr.header.type = htons (MTYPE);
306       hdr.num = htonl (n);
307       memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
308       ret += sizeof (struct TestMessage);
309       memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
310       ret += s - sizeof (struct TestMessage);
311 #if DEBUG_MEASUREMENT
312       if (n % 5000 == 0)
313        {
314           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315                       "Sending message %u\n",n);
316        }
317 #endif
318       n++;
319       s = get_size (n);
320       if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
321         break; /* sometimes pack buffer full, sometimes not */
322     }
323   while (size - ret >= s);
324   transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
325                                             &p1.id,
326                                             s, 0, SEND_TIMEOUT,
327                                             &notify_ready_new,
328                                             NULL);
329   total_bytes += s;
330   return ret;
331 }
332
333 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 );
334
335 static void measurement_counter
336  (void *cls,
337            const struct GNUNET_SCHEDULER_TaskContext *tc)
338 {
339   measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
340
341   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
342         return;
343
344 #if VERBOSE
345   fprintf(stderr,".");
346 #endif
347   measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
348                                                            GNUNET_TIME_UNIT_SECONDS,
349                                                            &measurement_counter,
350                                                            NULL);
351 }
352
353 static void
354 measurement_end (void *cls,
355            const struct GNUNET_SCHEDULER_TaskContext *tc)
356 {
357   measurement_task  = GNUNET_SCHEDULER_NO_TASK;
358   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
359         return;
360
361   measurement_running = GNUNET_NO;
362   struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
363
364
365   if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
366   {
367     GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
368     measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
369   }
370 #if VERBOSE
371   fprintf(stderr,"\n");
372 #endif
373   /*
374   if (transmit_handle != NULL)
375   {
376           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
377           transmit_handle = NULL;
378   }
379   */
380   if ((total_bytes/(duration.rel_value / 1000)) > (current_quota_p1 + (current_quota_p1 / 10)))
381   {
382           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383                           "\nQuota compliance failed: \n"\
384                           "Quota allowed: %10llu kB/s\n"\
385                           "Throughput   : %10llu kB/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.rel_value / 1000)/1024));
386           ok = 1;
387 /*        end();
388           return;*/
389   }
390   else
391   {
392
393           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
394                           "\nQuota compliance ok: \n"\
395                           "Quota allowed: %10llu kB/s\n"\
396                           "Throughput   : %10llu kB/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.rel_value / 1000)/1024));
397           ok = 0;
398   }
399
400   if (current_quota_p1 < MEASUREMENT_MIN_QUOTA)
401   {
402           end();
403           return;
404   }
405   else
406   {
407 #if VERBOSE
408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409               "Scheduling next measurement\n");
410 #endif
411         measure (current_quota_p1 / 10, current_quota_p2 / 10);
412   }
413 }
414
415 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
416 {
417           current_quota_p1 = quota_p1;
418           current_quota_p2 = quota_p2;
419 #if VERBOSE
420   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421               "Starting transport level measurement for %u seconds and quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p1 / 1024);
422 #endif
423                 GNUNET_TRANSPORT_set_quota (p1.th,
424                           &p2.id,
425                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
426                           GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
427                           GNUNET_TIME_UNIT_FOREVER_REL,
428                           NULL, NULL);
429                 GNUNET_TRANSPORT_set_quota (p2.th,
430                           &p1.id,
431                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
432                           GNUNET_BANDWIDTH_value_init (current_quota_p2),
433                           GNUNET_TIME_UNIT_FOREVER_REL,
434                           NULL, NULL);
435
436                 GNUNET_SCHEDULER_cancel (sched, die_task);
437                 die_task = GNUNET_SCHEDULER_add_delayed (sched,
438                                                    TIMEOUT,
439                                                    &end_badly,
440                                                    NULL);
441                 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
442                   GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
443                 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
444                                                                    GNUNET_TIME_UNIT_SECONDS,
445                                                                    &measurement_counter,
446                                                                    NULL);
447                 measurement_task = GNUNET_SCHEDULER_add_delayed (sched,
448                                                    MEASUREMENT_INTERVALL,
449                                                    &measurement_end,
450                                                    NULL);
451                 total_bytes = 0;
452                 measurement_running = GNUNET_YES;
453                 start_time = GNUNET_TIME_absolute_get ();
454
455                 if (transmit_handle != NULL)
456                           GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
457                 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
458                                                                                           &p1.id,
459                                                                                           get_size (0), 0, SEND_TIMEOUT,
460                                                                                           &notify_ready_new,
461                                                                                           NULL);
462 }
463
464 static void
465 notify_connect (void *cls,
466                 const struct GNUNET_PeerIdentity *peer,
467                 struct GNUNET_TIME_Relative latency,
468                 uint32_t distance)
469 {
470   if (cls == &p1)
471     {
472 #if DEBUG_CONNECTIONS
473   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474               "Peer 1 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
475 #endif
476           connected++;
477     }
478   else
479     {
480 #if DEBUG_CONNECTIONS
481   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482               "Peer 2 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
483 #endif
484       connected++;
485     }
486   if (connected == 2)
487     {
488           measure(MEASUREMENT_MAX_QUOTA,MEASUREMENT_MAX_QUOTA);
489     }
490 }
491
492
493 static void
494 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
495 {
496 #if DEBUG_CONNECTIONS
497   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498               "Peer `%4s' disconnected (%p)!\n",
499               GNUNET_i2s (peer), cls);
500 #endif
501 }
502
503
504 static void
505 setup_peer (struct PeerContext *p, const char *cfgname)
506 {
507   p->cfg = GNUNET_CONFIGURATION_create ();
508 #if START_ARM
509   p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
510                                         "gnunet-service-arm",
511                                         "gnunet-service-arm",
512 #if VERBOSE_ARM
513                                         "-L", "DEBUG",
514 #endif
515                                         "-c", cfgname, NULL);
516 #endif
517
518   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
519   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
520                                     p,
521                                     &notify_receive_new,
522                                     &notify_connect,
523                                     &notify_disconnect);
524   GNUNET_assert (p->th != NULL);
525 }
526
527
528 static void
529 exchange_hello_last (void *cls,
530                      const struct GNUNET_MessageHeader *message)
531 {
532   struct PeerContext *me = cls;
533
534   GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
535
536   GNUNET_assert (ok >= 3);
537   OKPP;
538   GNUNET_assert (message != NULL);
539   GNUNET_assert (GNUNET_OK ==
540                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
541                                       message, &me->id));
542   /* both HELLOs exchanged, get ready to test transmission! */
543 }
544
545
546 static void
547 exchange_hello (void *cls,
548                 const struct GNUNET_MessageHeader *message)
549 {
550   struct PeerContext *me = cls;
551
552   GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
553   GNUNET_assert (ok >= 2);
554   OKPP;
555   GNUNET_assert (message != NULL);
556   GNUNET_assert (GNUNET_OK ==
557                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
558                                       message, &me->id));
559   GNUNET_TRANSPORT_offer_hello (p2.th, message);
560   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
561 }
562
563 static void
564 run (void *cls,
565      struct GNUNET_SCHEDULER_Handle *s,
566      char *const *args,
567      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
568 {
569   GNUNET_assert (ok == 1);
570   OKPP;
571   sched = s;
572
573   die_task = GNUNET_SCHEDULER_add_delayed (sched,
574                                            TIMEOUT,
575                                            &end_badly,
576                                            NULL);
577   measurement_running = GNUNET_NO;
578   send_running = GNUNET_NO;
579   recv_running = GNUNET_NO;
580
581
582   if (is_tcp)
583     {
584       setup_peer (&p1, "test_quota_compliance_peer1.conf");
585       setup_peer (&p2, "test_quota_compliance_peer2.conf");
586     }
587   else if (is_http)
588     {
589       setup_peer (&p1, "test_quota_compliance_peer1.conf");
590       setup_peer (&p2, "test_quota_compliance_peer2.conf");
591     }
592   else if (is_https)
593     {
594       setup_peer (&p1, "test_quota_compliance_peer1.conf");
595       setup_peer (&p2, "test_quota_compliance_peer2.conf");
596     }
597   else if (is_udp)
598     {
599       setup_peer (&p1, "test_quota_compliance_peer1.conf");
600       setup_peer (&p2, "test_quota_compliance_peer2.conf");
601     }
602   else if (is_tcp_nat)
603     {
604       setup_peer (&p1, "test_quota_compliance_peer1.conf");
605       setup_peer (&p2, "test_quota_compliance_peer2.conf");
606     }
607   else
608     GNUNET_assert (0);
609
610   GNUNET_assert(p1.th != NULL);
611   GNUNET_assert(p2.th != NULL);
612   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
613 }
614
615 int
616 main (int argc, char *argv[])
617 {
618   int ret = 0;
619 #ifdef MINGW
620   return GNUNET_SYSERR;
621 #endif
622   if (strstr(argv[0], "tcp_nat") != NULL)
623     {
624       is_tcp_nat = GNUNET_YES;
625     }
626   else if (strstr(argv[0], "tcp") != NULL)
627     {
628       is_tcp = GNUNET_YES;
629     }
630   else if (strstr(argv[0], "https") != NULL)
631     {
632       is_https = GNUNET_YES;
633     }
634   else if (strstr(argv[0], "http") != NULL)
635     {
636       is_http = GNUNET_YES;
637     }
638   else if (strstr(argv[0], "udp") != NULL)
639     {
640       is_udp = GNUNET_YES;
641     }
642   GNUNET_log_setup ("test-quota-compliance",
643 #if VERBOSE
644                     "DEBUG",
645 #else
646                     "WARNING",
647 #endif
648                     NULL);
649   char *const argv1[] = { "test-quota-compliance",
650     "-c",
651     "test_quota_compliance_data.conf",
652 #if VERBOSE
653     "-L", "DEBUG",
654 #endif
655     NULL
656   };
657   struct GNUNET_GETOPT_CommandLineOption options[] = {
658     GNUNET_GETOPT_OPTION_END
659   };
660
661   ok = 1;
662   GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
663                       argv1, "test-quota-compliance", "nohelp",
664                       options, &run, &ok);
665   ret = ok;
666   stop_arm (&p1);
667   stop_arm (&p2);
668   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
669   GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
670   return ret;
671 }
672
673 /* end of test_quota_compliance.c */