d73b9584ca41ac41a8100e3fbaedab25665d1a7d
[oweals/gnunet.git] / src / transport / test_quota_compliance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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 transport level
25  */
26 #include "platform.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_ats_service.h"
29 #include "gauger.h"
30 #include "transport-testing.h"
31
32 /**
33  * Testcase timeout
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
36
37 /**
38  * How long until we give up on transmitting the message?
39  */
40 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
41
42 #define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
43
44 static char *test_source;
45
46 static char *test_plugin;
47
48 static char *test_name;
49
50 static GNUNET_SCHEDULER_TaskIdentifier die_task;
51
52 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
53
54 struct PeerContext *p1;
55
56 struct PeerContext *p2;
57
58 struct PeerContext *sender;
59
60 struct PeerContext *receiver;
61
62 struct GNUNET_TRANSPORT_TransmitHandle *th;
63
64 char *cfg_file_p1;
65 char *gen_cfg_p2;
66 unsigned long long quota_in_p1;
67 unsigned long long quota_out_p1;
68
69 char *cfg_file_p2;
70 char *gen_cfg_p1;
71 unsigned long long quota_in_p2;
72 unsigned long long quota_out_p2;
73
74 struct GNUNET_TRANSPORT_TESTING_handle *tth;
75
76 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
77
78
79 /*
80  * Testcase specific declarations
81  */
82
83 /**
84  * Note that this value must not significantly exceed
85  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
86  * messages may be dropped even for a reliable transport.
87  */
88 #define TOTAL_MSGS (1024 * 2)
89
90 #define MTYPE 12345
91
92 GNUNET_NETWORK_STRUCT_BEGIN
93
94 struct TestMessage
95 {
96   struct GNUNET_MessageHeader header;
97   uint32_t num;
98 };
99 GNUNET_NETWORK_STRUCT_END
100
101 static int msg_scheduled;
102 static int msg_sent;
103
104 static int test_failed;
105 static int test_connected;
106
107 static unsigned long long total_bytes_sent;
108
109 static struct GNUNET_TIME_Absolute start_time;
110
111 /*
112  * END Testcase specific declarations
113  */
114
115 #if VERBOSE
116 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
117 #else
118 #define OKPP do { ok++; } while (0)
119 #endif
120
121
122 static void
123 end ()
124 {
125   unsigned long long delta;
126   unsigned long long datarate;
127
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
129
130   delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
131   datarate = (total_bytes_sent * 1000 * 1000) / delta;
132
133   FPRINTF (stderr, "Throughput was %llu b/s\n", datarate);
134
135   test_failed = GNUNET_NO;
136   if (datarate > quota_in_p2)
137   {
138     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
139                 "Datarate of %llu b/s higher than allowed inbound quota of %llu b/s\n",
140                 datarate, quota_in_p2);
141     test_failed = GNUNET_YES;
142   }
143   if (datarate > quota_out_p1)
144   {
145     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146                 "Datarate of %llu b/s higher than allowed outbound quota of %llu b/s\n",
147                 datarate, quota_out_p1);
148     test_failed = GNUNET_YES;
149   }
150   if (test_failed == GNUNET_NO)
151   {
152     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153                 "Datarate of %llu b/s complied to allowed outbound quota of %llu b/s and inbound quota of %llu b/s\n",
154                 datarate, quota_out_p1, quota_in_p2);
155   }
156
157   if (die_task != GNUNET_SCHEDULER_NO_TASK)
158     GNUNET_SCHEDULER_cancel (die_task);
159
160   if (th != NULL)
161     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
162   th = NULL;
163
164   if (cc != NULL)
165     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
166
167   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
168   GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
169
170 }
171
172 static void
173 end_badly ()
174 {
175   die_task = GNUNET_SCHEDULER_NO_TASK;
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
177
178   if (measure_task != GNUNET_SCHEDULER_NO_TASK)
179     GNUNET_SCHEDULER_cancel (measure_task);
180
181   if (test_connected == GNUNET_YES)
182     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
183   else
184     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
185
186   if (th != NULL)
187     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
188   th = NULL;
189
190   if (cc != NULL)
191     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
192
193   if (p1 != NULL)
194     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
195   if (p2 != NULL)
196     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
197
198   test_failed = GNUNET_YES;
199 }
200
201
202 static unsigned int
203 get_size (unsigned int iter)
204 {
205   unsigned int ret;
206
207   ret = (iter * iter * iter);
208   return sizeof (struct TestMessage) + (ret % 60000);
209 }
210
211
212 static void
213 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
214                 const struct GNUNET_MessageHeader *message)
215 {
216   const struct TestMessage *hdr;
217
218   hdr = (const struct TestMessage *) message;
219   if (MTYPE != ntohs (message->type))
220     return;
221
222   struct PeerContext *p = cls;
223   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
224
225   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
226               "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
227               p->no, ps, ntohl (hdr->num), ntohs (message->size),
228               GNUNET_i2s (peer));
229   GNUNET_free (ps);
230 }
231
232
233 static size_t
234 notify_ready (void *cls, size_t size, void *buf)
235 {
236   static int n;
237   char *cbuf = buf;
238   struct TestMessage hdr;
239   unsigned int s;
240   unsigned int ret;
241
242   th = NULL;
243   if (buf == NULL)
244   {
245     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246                 "Timeout occurred while waiting for transmit_ready for message %u of %u\n",
247                 msg_scheduled, TOTAL_MSGS);
248     if (GNUNET_SCHEDULER_NO_TASK != die_task)
249       GNUNET_SCHEDULER_cancel (die_task);
250     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
251     test_failed = 1;
252     return 0;
253   }
254
255   ret = 0;
256   s = get_size (n);
257   GNUNET_assert (size >= s);
258   GNUNET_assert (buf != NULL);
259   cbuf = buf;
260   do
261   {
262     hdr.header.size = htons (s);
263     hdr.header.type = htons (MTYPE);
264     hdr.num = htonl (n);
265     msg_sent = n;
266     memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
267     ret += sizeof (struct TestMessage);
268     memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
269     ret += s - sizeof (struct TestMessage);
270 #if VERBOSE
271     if (n % 5000 == 0)
272     {
273 #endif
274       char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
275
276       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
277                   "Sending message %u of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
278                   n, s, sender->no, GNUNET_i2s (&sender->id), receiver->no,
279                   receiver_s);
280       GNUNET_free (receiver_s);
281 #if 0
282     }
283 #endif
284     n++;
285     s = get_size (n);
286     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
287       break;                    /* sometimes pack buffer full, sometimes not */
288   }
289   while (size - ret >= s);
290   if (n < TOTAL_MSGS)
291   {
292     if (th == NULL)
293       th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s,
294                                                    TIMEOUT_TRANSMIT,
295                                                    &notify_ready, NULL);
296     msg_scheduled = n;
297   }
298   if (n % 5000 == 0)
299   {
300     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301                 "Returning total message block of size %u\n", ret);
302   }
303   total_bytes_sent += ret;
304   if (n == TOTAL_MSGS)
305   {
306     FPRINTF (stderr, "%s",  "\n");
307     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
308   }
309   return ret;
310 }
311
312
313 static void
314 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
315 {
316
317   struct PeerContext *p = cls;
318
319   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
320               p->no, GNUNET_i2s (peer));
321 }
322
323
324 static void
325 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
326 {
327   struct PeerContext *p = cls;
328
329   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
330               GNUNET_i2s (peer));
331   if (th != NULL)
332     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
333   th = NULL;
334
335 }
336
337 static void
338 sendtask ()
339 {
340   start_time = GNUNET_TIME_absolute_get ();
341   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0),
342                                                TIMEOUT_TRANSMIT, &notify_ready,
343                                                NULL);
344 }
345
346
347 static void
348 measure (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
349 {
350   static int counter;
351
352   measure_task = GNUNET_SCHEDULER_NO_TASK;
353
354   counter++;
355   if ((DURATION.rel_value_us / 1000 / 1000LL) < counter)
356   {
357     FPRINTF (stderr, "%s",  ".\n");
358     GNUNET_SCHEDULER_add_now (&end, NULL);
359   }
360   else
361   {
362     FPRINTF (stderr, "%s",  ".");
363     measure_task =
364         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &measure, NULL);
365   }
366 }
367
368
369 static void
370 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
371 {
372   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
373
374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
375               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
376   GNUNET_free (p1_c);
377
378   cc = NULL;
379   test_connected = GNUNET_YES;
380
381   measure_task =
382       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &measure, NULL);
383   GNUNET_SCHEDULER_add_now (&sendtask, NULL);
384
385 }
386
387 void
388 start_cb (struct PeerContext *p, void *cls)
389 {
390   static int started;
391
392   started++;
393
394   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
395               GNUNET_i2s (&p->id));
396
397   if (started != 2)
398     return;
399
400   test_connected = GNUNET_NO;
401
402   sender = p2;
403   receiver = p1;
404
405   char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407               "Test tries to send from %u (%s) -> peer %u (%s)\n", sender->no,
408               sender_c, receiver->no, GNUNET_i2s (&receiver->id));
409   GNUNET_free (sender_c);
410   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
411                                                NULL);
412
413 }
414
415 static char *
416 generate_config (char *cfg_file, unsigned long long quota_in,
417                  unsigned long long quota_out)
418 {
419   char *networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
420   char *in_name;
421   char *out_name;
422   char *fname = NULL;
423   struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
424   int c;
425
426   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_file));
427   GNUNET_asprintf (&fname, "q_in_%llu_q_out_%llu_%s", quota_in, quota_out,
428                    cfg_file);
429
430   GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", fname);
431
432   for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
433   {
434       GNUNET_asprintf (&in_name, "%s_QUOTA_IN", networks[c]);
435       GNUNET_asprintf (&out_name, "%s_QUOTA_OUT", networks[c]);
436       GNUNET_CONFIGURATION_set_value_number (cfg, "ats", in_name, quota_in);
437       GNUNET_CONFIGURATION_set_value_number (cfg, "ats", out_name, quota_out);
438       GNUNET_free (in_name);
439       GNUNET_free (out_name);
440   }
441   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write (cfg, fname));
442   GNUNET_CONFIGURATION_destroy (cfg);
443   return fname;
444 }
445
446 static void
447 run_measurement (unsigned long long p1_quota_in,
448                  unsigned long long p1_quota_out,
449                  unsigned long long p2_quota_in,
450                  unsigned long long p2_quota_out)
451 {
452   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
453
454   /* setting ATS quota */
455   quota_out_p1 = p1_quota_out;
456   gen_cfg_p1 = generate_config (cfg_file_p1, p1_quota_in, p1_quota_out);
457   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generated config file `%s'\n",
458               gen_cfg_p1);
459
460   quota_in_p2 = p2_quota_in;
461   gen_cfg_p2 = generate_config (cfg_file_p2, p2_quota_in, p2_quota_out);
462   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generated config file `%s'\n",
463               gen_cfg_p2);
464
465   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p1, 1, &notify_receive,
466                                             &notify_connect, &notify_disconnect,
467                                             &start_cb, NULL);
468
469   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p2, 2, &notify_receive,
470                                             &notify_connect, &notify_disconnect,
471                                             &start_cb, NULL);
472
473   if ((p1 == NULL) || (p2 == NULL))
474   {
475     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
476     if (die_task != GNUNET_SCHEDULER_NO_TASK)
477       GNUNET_SCHEDULER_cancel (die_task);
478     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
479     return;
480   }
481 }
482
483 static void
484 run (void *cls, char *const *args, const char *cfgfile,
485      const struct GNUNET_CONFIGURATION_Handle *cfg)
486 {
487   unsigned long long p1_quota_in = 10000;
488   unsigned long long p1_quota_out = 10000;
489   unsigned long long p2_quota_in = 10000;
490   unsigned long long p2_quota_out = 10000;
491
492   if (NULL != strstr (test_name, "asymmetric"))
493   {
494     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495                 "Running asymmetric test with sending peer unlimited, receiving peer (in/out): %llu/%llu b/s \n",
496                 p2_quota_in, p2_quota_out);
497     p1_quota_out = 1024 * 1024 * 1024;
498     p1_quota_in = 1024 * 1024 * 1024;
499   }
500   else
501   {
502     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503                 "Running symmetric test with (in/out) %llu/%llu b/s \n",
504                 p2_quota_in, p2_quota_out);
505   }
506   run_measurement (p1_quota_in, p1_quota_out, p2_quota_in, p2_quota_out);
507 }
508
509 static int
510 check ()
511 {
512   static char *argv[] = { "test_transport-quota-compliance",
513     "-c",
514     "test_quota_compliance_data.conf",
515     NULL
516   };
517   static struct GNUNET_GETOPT_CommandLineOption options[] = {
518     GNUNET_GETOPT_OPTION_END
519   };
520
521   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
522                       "nohelp", options, &run, NULL);
523   return test_failed;
524 }
525
526 int
527 main (int argc, char *argv[])
528 {
529   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
530
531   GNUNET_log_setup (test_name,
532                     "WARNING",
533                     NULL);
534
535   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
536   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
537                                                  &test_plugin);
538
539   tth = GNUNET_TRANSPORT_TESTING_init ();
540
541   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
542   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
543
544   check ();
545
546   GNUNET_free (cfg_file_p1);
547   GNUNET_free (cfg_file_p2);
548
549   if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p1))
550   {
551     GNUNET_DISK_directory_remove (gen_cfg_p1);
552     GNUNET_free (gen_cfg_p1);
553   }
554
555   if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p2))
556   {
557     GNUNET_DISK_directory_remove (gen_cfg_p2);
558     GNUNET_free (gen_cfg_p2);
559   }
560
561   GNUNET_free (test_source);
562   GNUNET_free (test_plugin);
563   GNUNET_free (test_name);
564
565   GNUNET_TRANSPORT_TESTING_done (tth);
566
567   return test_failed;
568 }
569
570
571 /* end of test_quota_compliance.c */