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
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file core/test_core_quota_compliance.c
22 * @brief testcase for core_api.c focusing quota compliance on core level
25 #include "gnunet_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_transport_hello_service.h"
31 #include "gnunet_statistics_service.h"
35 #define ASYMMETRIC_SEND_LIMITED 1
36 #define ASYMMETRIC_RECV_LIMITED 2
39 * Note that this value must not significantly exceed
40 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
41 * messages may be dropped even for a reliable transport.
43 #define TOTAL_MSGS (60000 * 10)
46 * How long until we give up on transmitting the message?
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
51 * What delay do we request from the core service for transmission?
53 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
56 #define MESSAGESIZE 1024
57 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
59 static unsigned long long total_bytes_sent;
60 static unsigned long long total_bytes_recv;
62 static struct GNUNET_TIME_Absolute start_time;
64 static struct GNUNET_SCHEDULER_Task *err_task;
66 static struct GNUNET_SCHEDULER_Task *measure_task;
71 struct GNUNET_CONFIGURATION_Handle *cfg;
72 struct GNUNET_CORE_Handle *ch;
73 struct GNUNET_CORE_TransmitHandle *nth;
74 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
75 struct GNUNET_PeerIdentity id;
76 struct GNUNET_MessageHeader *hello;
77 struct GNUNET_STATISTICS_Handle *stats;
78 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
79 struct GNUNET_ATS_ConnectivityHandle *ats;
80 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
82 struct GNUNET_OS_Process *arm_proc;
85 static struct PeerContext p1;
86 static struct PeerContext p2;
88 static unsigned long long current_quota_p1_in;
89 static unsigned long long current_quota_p1_out;
90 static unsigned long long current_quota_p2_in;
91 static unsigned long long current_quota_p2_out;
101 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
103 #define OKPP do { ok++; } while (0)
108 struct GNUNET_MessageHeader header;
114 terminate_peer (struct PeerContext *p)
118 GNUNET_CORE_notify_transmit_ready_cancel (p->nth);
123 GNUNET_CORE_disconnect (p->ch);
128 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
133 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
136 if (NULL != p->ats_sh)
138 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
143 GNUNET_ATS_connectivity_done (p->ats);
146 if (NULL != p->stats)
148 GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
151 if (NULL != p->hello)
153 GNUNET_free (p->hello);
160 shutdown_task (void *cls)
162 if (NULL != err_task)
164 GNUNET_SCHEDULER_cancel (err_task);
167 if (NULL != measure_task)
169 GNUNET_SCHEDULER_cancel (measure_task);
172 terminate_peer (&p1);
173 terminate_peer (&p2);
178 terminate_task_error (void *cls)
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Testcase failed (timeout)!\n");
183 GNUNET_SCHEDULER_shutdown ();
189 * Callback function to process statistic values.
192 * @param subsystem name of subsystem that created the statistic
193 * @param name the name of the datum
194 * @param value the current value
195 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
196 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
199 print_stat (void *cls,
200 const char *subsystem,
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Peer1 %50s = %12llu\n",
209 (unsigned long long) value);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Peer2 %50s = %12llu\n",
214 (unsigned long long) value);
220 measurement_stop (void *cls)
222 unsigned long long delta;
223 unsigned long long throughput_out;
224 unsigned long long throughput_in;
225 unsigned long long max_quota_in;
226 unsigned long long max_quota_out;
227 unsigned long long quota_delta;
228 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
231 FPRINTF (stdout, "%s", "\n");
234 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
236 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
237 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
239 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
240 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
241 if (max_quota_out < max_quota_in)
242 quota_delta = max_quota_in / 3;
244 quota_delta = max_quota_out / 3;
246 if ((throughput_out > (max_quota_out + quota_delta)) ||
247 (throughput_in > (max_quota_in + quota_delta)))
251 GNUNET_STATISTICS_get (p1.stats,
253 "# discarded CORE_SEND requests",
257 GNUNET_STATISTICS_get (p1.stats,
259 "# discarded CORE_SEND request bytes",
263 GNUNET_STATISTICS_get (p1.stats,
265 "# discarded lower priority CORE_SEND requests",
269 GNUNET_STATISTICS_get (p1.stats,
271 "# discarded lower priority CORE_SEND request bytes",
275 GNUNET_STATISTICS_get (p2.stats,
277 "# discarded CORE_SEND requests",
282 GNUNET_STATISTICS_get (p2.stats,
284 "# discarded CORE_SEND request bytes",
288 GNUNET_STATISTICS_get (p2.stats,
290 "# discarded lower priority CORE_SEND requests",
294 GNUNET_STATISTICS_get (p2.stats,
296 "# discarded lower priority CORE_SEND request bytes",
302 kind = GNUNET_ERROR_TYPE_ERROR;
307 "Core quota compliance test with symmetric quotas: %s\n",
308 (0 == ok) ? "PASSED" : "FAILED");
310 case ASYMMETRIC_SEND_LIMITED:
312 "Core quota compliance test with limited sender quota: %s\n",
313 (0 == ok) ? "PASSED" : "FAILED");
315 case ASYMMETRIC_RECV_LIMITED:
317 "Core quota compliance test with limited receiver quota: %s\n",
318 (0 == ok) ? "PASSED" : "FAILED");
322 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
327 "Peer 1 send quota: %llu b/s\n",
328 current_quota_p1_out);
330 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
335 "Peer 2 receive quota: %llu b/s\n",
336 current_quota_p2_in);
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
341 GNUNET_SCHEDULER_shutdown ();
346 transmit_ready (void *cls,
351 struct TestMessage hdr;
355 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
358 if ( (NULL != p1.ch) &&
359 (1 == p1.connect_status) )
360 GNUNET_break (NULL !=
362 GNUNET_CORE_notify_transmit_ready (p1.ch,
364 GNUNET_CORE_PRIO_BEST_EFFORT,
372 GNUNET_assert (tr_n < TOTAL_MSGS);
374 GNUNET_assert (size >= MESSAGESIZE);
375 GNUNET_assert (NULL != buf);
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Sending message %u of size %u at offset %u\n",
384 hdr.header.size = htons (MESSAGESIZE);
385 hdr.header.type = htons (MTYPE);
386 hdr.num = htonl (tr_n);
387 GNUNET_memcpy (&cbuf[ret],
389 sizeof (struct TestMessage));
390 ret += sizeof (struct TestMessage);
393 MESSAGESIZE - sizeof (struct TestMessage));
394 ret += MESSAGESIZE - sizeof (struct TestMessage);
396 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
398 break; /* sometimes pack buffer full, sometimes not */
400 while (size - ret >= MESSAGESIZE);
401 GNUNET_SCHEDULER_cancel (err_task);
403 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
404 &terminate_task_error,
407 total_bytes_sent += ret;
413 connect_notify (void *cls,
414 const struct GNUNET_PeerIdentity *peer)
416 struct PeerContext *pc = cls;
418 if (0 == memcmp (&pc->id,
420 sizeof (struct GNUNET_PeerIdentity)))
421 return; /* loopback */
422 GNUNET_assert (0 == pc->connect_status);
423 pc->connect_status = 1;
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Encrypted connection established to peer `%s'\n",
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Asking core (1) for transmission to peer `%s'\n",
431 GNUNET_i2s (&p2.id));
432 GNUNET_SCHEDULER_cancel (err_task);
434 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
435 &terminate_task_error,
437 start_time = GNUNET_TIME_absolute_get ();
438 running = GNUNET_YES;
440 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
444 GNUNET_break (NULL !=
446 GNUNET_CORE_notify_transmit_ready (p1.ch,
448 GNUNET_CORE_PRIO_BEST_EFFORT,
459 disconnect_notify (void *cls,
460 const struct GNUNET_PeerIdentity *peer)
462 struct PeerContext *pc = cls;
464 if (0 == memcmp (&pc->id,
466 sizeof (struct GNUNET_PeerIdentity)))
467 return; /* loopback */
468 pc->connect_status = 0;
469 if (NULL != measure_task)
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472 "Measurement aborted due to disconnect!\n");
473 GNUNET_SCHEDULER_cancel (measure_task);
478 GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Encrypted connection to `%s' cut\n",
488 inbound_notify (void *cls,
489 const struct GNUNET_PeerIdentity *other,
490 const struct GNUNET_MessageHeader *message)
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Core provides inbound data from `%s' %u.\n",
495 (unsigned int) ntohs (message->size));
496 total_bytes_recv += ntohs (message->size);
502 outbound_notify (void *cls,
503 const struct GNUNET_PeerIdentity *other,
504 const struct GNUNET_MessageHeader *message)
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Core notifies about outbound data for `%s'.\n",
514 transmit_ready (void *cls, size_t size, void *buf);
518 process_mtype (void *cls,
519 const struct GNUNET_PeerIdentity *peer,
520 const struct GNUNET_MessageHeader *message)
523 const struct TestMessage *hdr;
525 hdr = (const struct TestMessage *) message;
526 if (MTYPE != ntohs (message->type))
527 return GNUNET_SYSERR;
528 if (ntohs (message->size) != MESSAGESIZE)
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
531 "Expected message %u of size %u, got %u bytes of message %u\n",
534 ntohs (message->size),
536 GNUNET_SCHEDULER_cancel (err_task);
537 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
539 return GNUNET_SYSERR;
541 if (ntohl (hdr->num) != n)
543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
544 "Expected message %u of size %u, got %u bytes of message %u\n",
547 ntohs (message->size),
549 GNUNET_SCHEDULER_cancel (err_task);
550 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
552 return GNUNET_SYSERR;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Got message %u of size %u\n",
557 ntohs (message->size));
560 FPRINTF (stderr, "%s", ".");
562 if (GNUNET_YES == running)
563 GNUNET_break (NULL !=
564 (p1.nth = GNUNET_CORE_notify_transmit_ready (p1.ch,
566 GNUNET_CORE_PRIO_BEST_EFFORT,
576 static struct GNUNET_CORE_MessageHandler handlers[] = {
577 {&process_mtype, MTYPE, 0},
584 init_notify (void *cls,
585 const struct GNUNET_PeerIdentity *my_identity)
587 struct PeerContext *p = cls;
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Connection to CORE service of `%4s' established\n",
591 GNUNET_i2s (my_identity));
592 GNUNET_assert (NULL != my_identity);
593 p->id = *my_identity;
596 GNUNET_assert (ok == 2);
600 GNUNET_CORE_connect (p2.cfg,
613 GNUNET_assert (ok == 3);
615 GNUNET_assert (cls == &p2);
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Asking core (1) to connect to peer `%s' and vice-versa\n",
618 GNUNET_i2s (&p2.id));
619 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
622 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
630 offer_hello_done (void *cls)
632 struct PeerContext *p = cls;
639 process_hello (void *cls,
640 const struct GNUNET_MessageHeader *message)
642 struct PeerContext *p = cls;
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Received (my) HELLO from transport service\n");
646 GNUNET_assert (message != NULL);
647 p->hello = GNUNET_malloc (ntohs (message->size));
648 GNUNET_memcpy (p->hello, message, ntohs (message->size));
649 if ((p == &p1) && (NULL == p2.oh))
650 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
654 if ((p == &p2) && (NULL == p1.oh))
655 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
659 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh))
660 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
664 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
665 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
673 setup_peer (struct PeerContext *p,
678 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
679 p->cfg = GNUNET_CONFIGURATION_create ();
681 GNUNET_OS_start_process (GNUNET_YES,
682 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
685 "gnunet-service-arm",
689 GNUNET_assert (GNUNET_OK ==
690 GNUNET_CONFIGURATION_load (p->cfg,
692 p->stats = GNUNET_STATISTICS_create ("core",
694 GNUNET_assert (NULL != p->stats);
695 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
696 GNUNET_assert (NULL != p->ats);
697 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
698 GNUNET_TRANSPORT_AC_ANY,
701 GNUNET_free (binary);
709 const struct GNUNET_CONFIGURATION_Handle *cfg)
711 GNUNET_assert (ok == 1);
714 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
715 &terminate_task_error,
717 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
719 if (test == SYMMETRIC)
722 "test_core_quota_peer1.conf");
724 "test_core_quota_peer2.conf");
726 else if (test == ASYMMETRIC_SEND_LIMITED)
729 "test_core_quota_asymmetric_send_limit_peer1.conf");
731 "test_core_quota_asymmetric_send_limit_peer2.conf");
733 else if (test == ASYMMETRIC_RECV_LIMITED)
736 "test_core_quota_asymmetric_recv_limited_peer1.conf");
738 "test_core_quota_asymmetric_recv_limited_peer2.conf");
741 GNUNET_assert (test != -1);
742 GNUNET_assert (GNUNET_SYSERR !=
743 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
746 ¤t_quota_p1_in));
747 GNUNET_assert (GNUNET_SYSERR !=
748 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
751 ¤t_quota_p2_in));
752 GNUNET_assert (GNUNET_SYSERR !=
753 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
756 ¤t_quota_p1_out));
757 GNUNET_assert (GNUNET_SYSERR !=
758 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
761 ¤t_quota_p2_out));
764 GNUNET_CORE_connect (p1.cfg,
778 stop_arm (struct PeerContext *p)
780 if (0 != GNUNET_OS_process_kill (p->arm_proc,
782 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
785 GNUNET_OS_process_wait (p->arm_proc))
786 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "ARM process %u stopped\n",
790 GNUNET_OS_process_get_pid (p->arm_proc));
791 GNUNET_OS_process_destroy (p->arm_proc);
793 GNUNET_CONFIGURATION_destroy (p->cfg);
800 char *const argv[] = {
801 "test-core-quota-compliance",
803 "test_core_api_data.conf",
806 struct GNUNET_GETOPT_CommandLineOption options[] = {
807 GNUNET_GETOPT_OPTION_END
810 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
812 "test-core-quota-compliance",
830 if (strstr (argv[0], "_symmetric") != NULL)
834 else if (strstr (argv[0], "_asymmetric_send") != NULL)
836 test = ASYMMETRIC_SEND_LIMITED;
838 else if (strstr (argv[0], "_asymmetric_recv") != NULL)
840 test = ASYMMETRIC_RECV_LIMITED;
842 GNUNET_assert (test != -1);
843 if (test == SYMMETRIC)
845 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
846 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
848 else if (test == ASYMMETRIC_SEND_LIMITED)
850 GNUNET_DISK_directory_remove
851 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
852 GNUNET_DISK_directory_remove
853 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
855 else if (test == ASYMMETRIC_RECV_LIMITED)
857 GNUNET_DISK_directory_remove
858 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
859 GNUNET_DISK_directory_remove
860 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
863 GNUNET_log_setup ("test-core-quota-compliance",
867 if (test == SYMMETRIC)
869 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
870 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
872 else if (test == ASYMMETRIC_SEND_LIMITED)
874 GNUNET_DISK_directory_remove
875 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
876 GNUNET_DISK_directory_remove
877 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
879 else if (test == ASYMMETRIC_RECV_LIMITED)
881 GNUNET_DISK_directory_remove
882 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
883 GNUNET_DISK_directory_remove
884 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
889 /* end of test_core_quota_compliance.c */