2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
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 Affero General Public License for more details.
16 * @file core/test_core_quota_compliance.c
17 * @brief testcase for core_api.c focusing quota compliance on core level
18 * @author Christian Grothoff
21 #include "gnunet_arm_service.h"
22 #include "gnunet_core_service.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_ats_service.h"
25 #include "gnunet_transport_service.h"
26 #include "gnunet_transport_hello_service.h"
27 #include "gnunet_statistics_service.h"
31 #define ASYMMETRIC_SEND_LIMITED 1
32 #define ASYMMETRIC_RECV_LIMITED 2
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.
39 #define TOTAL_MSGS (60000 * 10)
42 * How long until we give up on transmitting the message?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
47 * What delay do we request from the core service for transmission?
49 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
52 #define MESSAGESIZE (1024 - 8)
53 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
55 static unsigned long long total_bytes_sent;
56 static unsigned long long total_bytes_recv;
58 static struct GNUNET_TIME_Absolute start_time;
60 static struct GNUNET_SCHEDULER_Task *err_task;
62 static struct GNUNET_SCHEDULER_Task *measure_task;
67 struct GNUNET_CONFIGURATION_Handle *cfg;
68 struct GNUNET_CORE_Handle *ch;
69 struct GNUNET_MQ_Handle *mq;
70 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
71 struct GNUNET_PeerIdentity id;
72 struct GNUNET_MessageHeader *hello;
73 struct GNUNET_STATISTICS_Handle *stats;
74 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
75 struct GNUNET_ATS_ConnectivityHandle *ats;
76 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
78 struct GNUNET_OS_Process *arm_proc;
81 static struct PeerContext p1;
82 static struct PeerContext p2;
84 static unsigned long long current_quota_p1_in;
85 static unsigned long long current_quota_p1_out;
86 static unsigned long long current_quota_p2_in;
87 static unsigned long long current_quota_p2_out;
97 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
99 #define OKPP do { ok++; } while (0)
104 struct GNUNET_MessageHeader header;
105 uint32_t num GNUNET_PACKED;
106 uint8_t pad[MESSAGESIZE];
111 terminate_peer (struct PeerContext *p)
115 GNUNET_CORE_disconnect (p->ch);
120 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
125 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
128 if (NULL != p->ats_sh)
130 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
135 GNUNET_ATS_connectivity_done (p->ats);
138 if (NULL != p->stats)
140 GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
143 if (NULL != p->hello)
145 GNUNET_free (p->hello);
152 shutdown_task (void *cls)
154 if (NULL != err_task)
156 GNUNET_SCHEDULER_cancel (err_task);
159 if (NULL != measure_task)
161 GNUNET_SCHEDULER_cancel (measure_task);
164 terminate_peer (&p1);
165 terminate_peer (&p2);
170 terminate_task_error (void *cls)
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 "Testcase failed (timeout)!\n");
175 GNUNET_SCHEDULER_shutdown ();
181 * Callback function to process statistic values.
184 * @param subsystem name of subsystem that created the statistic
185 * @param name the name of the datum
186 * @param value the current value
187 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
188 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
191 print_stat (void *cls,
192 const char *subsystem,
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Peer1 %50s = %12llu\n",
201 (unsigned long long) value);
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Peer2 %50s = %12llu\n",
206 (unsigned long long) value);
212 measurement_stop (void *cls)
214 unsigned long long delta;
215 unsigned long long throughput_out;
216 unsigned long long throughput_in;
217 unsigned long long max_quota_in;
218 unsigned long long max_quota_out;
219 unsigned long long quota_delta;
220 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
223 FPRINTF (stdout, "%s", "\n");
226 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
228 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
229 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
231 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
232 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
233 if (max_quota_out < max_quota_in)
234 quota_delta = max_quota_in / 3;
236 quota_delta = max_quota_out / 3;
238 if ((throughput_out > (max_quota_out + quota_delta)) ||
239 (throughput_in > (max_quota_in + quota_delta)))
243 GNUNET_STATISTICS_get (p1.stats,
245 "# discarded CORE_SEND requests",
249 GNUNET_STATISTICS_get (p1.stats,
251 "# discarded CORE_SEND request bytes",
255 GNUNET_STATISTICS_get (p1.stats,
257 "# discarded lower priority CORE_SEND requests",
261 GNUNET_STATISTICS_get (p1.stats,
263 "# discarded lower priority CORE_SEND request bytes",
267 GNUNET_STATISTICS_get (p2.stats,
269 "# discarded CORE_SEND requests",
274 GNUNET_STATISTICS_get (p2.stats,
276 "# discarded CORE_SEND request bytes",
280 GNUNET_STATISTICS_get (p2.stats,
282 "# discarded lower priority CORE_SEND requests",
286 GNUNET_STATISTICS_get (p2.stats,
288 "# discarded lower priority CORE_SEND request bytes",
294 kind = GNUNET_ERROR_TYPE_ERROR;
299 "Core quota compliance test with symmetric quotas: %s\n",
300 (0 == ok) ? "PASSED" : "FAILED");
302 case ASYMMETRIC_SEND_LIMITED:
304 "Core quota compliance test with limited sender quota: %s\n",
305 (0 == ok) ? "PASSED" : "FAILED");
307 case ASYMMETRIC_RECV_LIMITED:
309 "Core quota compliance test with limited receiver quota: %s\n",
310 (0 == ok) ? "PASSED" : "FAILED");
314 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
319 "Peer 1 send quota: %llu b/s\n",
320 current_quota_p1_out);
322 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
327 "Peer 2 receive quota: %llu b/s\n",
328 current_quota_p2_in);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
333 GNUNET_SCHEDULER_shutdown ();
338 do_transmit (void *cls)
340 struct TestMessage *hdr;
341 struct GNUNET_MQ_Envelope *env;
343 env = GNUNET_MQ_msg (hdr,
345 hdr->num = htonl (tr_n);
350 GNUNET_SCHEDULER_cancel (err_task);
352 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
353 &terminate_task_error,
355 total_bytes_sent += sizeof (struct TestMessage);
356 GNUNET_MQ_send (p1.mq,
362 connect_notify (void *cls,
363 const struct GNUNET_PeerIdentity *peer,
364 struct GNUNET_MQ_Handle *mq)
366 struct PeerContext *pc = cls;
368 if (0 == memcmp (&pc->id,
370 sizeof (struct GNUNET_PeerIdentity)))
371 return NULL; /* loopback */
372 GNUNET_assert (0 == pc->connect_status);
373 pc->connect_status = 1;
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Encrypted connection established to peer `%s'\n",
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Asking core (1) for transmission to peer `%s'\n",
382 GNUNET_i2s (&p2.id));
383 GNUNET_SCHEDULER_cancel (err_task);
385 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
386 &terminate_task_error,
388 start_time = GNUNET_TIME_absolute_get ();
389 running = GNUNET_YES;
391 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
401 disconnect_notify (void *cls,
402 const struct GNUNET_PeerIdentity *peer,
405 struct PeerContext *pc = cls;
407 if (NULL == internal_cls)
408 return; /* loopback */
409 pc->connect_status = 0;
411 if (NULL != measure_task)
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
414 "Measurement aborted due to disconnect!\n");
415 GNUNET_SCHEDULER_cancel (measure_task);
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Encrypted connection to `%s' cut\n",
426 handle_test (void *cls,
427 const struct TestMessage *hdr)
431 total_bytes_recv += sizeof (struct TestMessage);
432 if (ntohl (hdr->num) != n)
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 "Expected message %u, got message %u\n",
438 GNUNET_SCHEDULER_cancel (err_task);
439 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448 FPRINTF (stderr, "%s", ".");
450 if (GNUNET_YES == running)
456 init_notify (void *cls,
457 const struct GNUNET_PeerIdentity *my_identity)
459 struct PeerContext *p = cls;
460 struct GNUNET_MQ_MessageHandler handlers[] = {
461 GNUNET_MQ_hd_fixed_size (test,
465 GNUNET_MQ_handler_end ()
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
469 "Connection to CORE service of `%s' established\n",
470 GNUNET_i2s (my_identity));
471 GNUNET_assert (NULL != my_identity);
472 p->id = *my_identity;
475 GNUNET_assert (ok == 2);
478 p2.ch = GNUNET_CORE_connect (p2.cfg,
487 GNUNET_assert (ok == 3);
489 GNUNET_assert (cls == &p2);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Asking core (1) to connect to peer `%s' and vice-versa\n",
492 GNUNET_i2s (&p2.id));
493 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
496 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
504 offer_hello_done (void *cls)
506 struct PeerContext *p = cls;
513 process_hello (void *cls,
514 const struct GNUNET_MessageHeader *message)
516 struct PeerContext *p = cls;
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Received (my) HELLO from transport service\n");
520 GNUNET_assert (message != NULL);
521 p->hello = GNUNET_malloc (ntohs (message->size));
522 GNUNET_memcpy (p->hello, message, ntohs (message->size));
525 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
531 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
536 (NULL != p2.hello) &&
538 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
543 (NULL != p1.hello) &&
545 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
553 setup_peer (struct PeerContext *p,
558 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
559 p->cfg = GNUNET_CONFIGURATION_create ();
561 GNUNET_OS_start_process (GNUNET_YES,
562 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
565 "gnunet-service-arm",
569 GNUNET_assert (GNUNET_OK ==
570 GNUNET_CONFIGURATION_load (p->cfg,
572 p->stats = GNUNET_STATISTICS_create ("core",
574 GNUNET_assert (NULL != p->stats);
575 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
576 GNUNET_assert (NULL != p->ats);
577 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
578 GNUNET_TRANSPORT_AC_ANY,
581 GNUNET_free (binary);
589 const struct GNUNET_CONFIGURATION_Handle *cfg)
591 struct GNUNET_MQ_MessageHandler handlers[] = {
592 GNUNET_MQ_hd_fixed_size (test,
596 GNUNET_MQ_handler_end ()
599 GNUNET_assert (ok == 1);
602 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
603 &terminate_task_error,
605 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
607 if (test == SYMMETRIC)
610 "test_core_quota_peer1.conf");
612 "test_core_quota_peer2.conf");
614 else if (test == ASYMMETRIC_SEND_LIMITED)
617 "test_core_quota_asymmetric_send_limit_peer1.conf");
619 "test_core_quota_asymmetric_send_limit_peer2.conf");
621 else if (test == ASYMMETRIC_RECV_LIMITED)
624 "test_core_quota_asymmetric_recv_limited_peer1.conf");
626 "test_core_quota_asymmetric_recv_limited_peer2.conf");
629 GNUNET_assert (test != -1);
630 GNUNET_assert (GNUNET_SYSERR !=
631 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
634 ¤t_quota_p1_in));
635 GNUNET_assert (GNUNET_SYSERR !=
636 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
639 ¤t_quota_p2_in));
640 GNUNET_assert (GNUNET_SYSERR !=
641 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
644 ¤t_quota_p1_out));
645 GNUNET_assert (GNUNET_SYSERR !=
646 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
649 ¤t_quota_p2_out));
651 p1.ch = GNUNET_CORE_connect (p1.cfg,
661 stop_arm (struct PeerContext *p)
663 if (0 != GNUNET_OS_process_kill (p->arm_proc,
665 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
668 GNUNET_OS_process_wait (p->arm_proc))
669 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "ARM process %u stopped\n",
673 GNUNET_OS_process_get_pid (p->arm_proc));
674 GNUNET_OS_process_destroy (p->arm_proc);
676 GNUNET_CONFIGURATION_destroy (p->cfg);
683 char *const argv[] = {
684 "test-core-quota-compliance",
686 "test_core_api_data.conf",
689 struct GNUNET_GETOPT_CommandLineOption options[] = {
690 GNUNET_GETOPT_OPTION_END
693 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
695 "test-core-quota-compliance",
707 cleanup_directory (int test)
711 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
712 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
714 case ASYMMETRIC_SEND_LIMITED:
715 GNUNET_DISK_directory_remove
716 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
717 GNUNET_DISK_directory_remove
718 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
720 case ASYMMETRIC_RECV_LIMITED:
721 GNUNET_DISK_directory_remove
722 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
723 GNUNET_DISK_directory_remove
724 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
737 if (NULL != strstr (argv[0],
742 else if (NULL != strstr (argv[0],
745 test = ASYMMETRIC_SEND_LIMITED;
747 else if (NULL != strstr (argv[0],
750 test = ASYMMETRIC_RECV_LIMITED;
752 GNUNET_assert (test != -1);
753 cleanup_directory (test);
754 GNUNET_log_setup ("test-core-quota-compliance",
758 cleanup_directory (test);
763 /* end of test_core_quota_compliance.c */