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
23 * @author Christian Grothoff
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_transport_hello_service.h"
32 #include "gnunet_statistics_service.h"
36 #define ASYMMETRIC_SEND_LIMITED 1
37 #define ASYMMETRIC_RECV_LIMITED 2
40 * Note that this value must not significantly exceed
41 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
42 * messages may be dropped even for a reliable transport.
44 #define TOTAL_MSGS (60000 * 10)
47 * How long until we give up on transmitting the message?
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
52 * What delay do we request from the core service for transmission?
54 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
57 #define MESSAGESIZE 1024
58 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
60 static unsigned long long total_bytes_sent;
61 static unsigned long long total_bytes_recv;
63 static struct GNUNET_TIME_Absolute start_time;
65 static struct GNUNET_SCHEDULER_Task *err_task;
67 static struct GNUNET_SCHEDULER_Task *measure_task;
72 struct GNUNET_CONFIGURATION_Handle *cfg;
73 struct GNUNET_CORE_Handle *ch;
74 struct GNUNET_CORE_TransmitHandle *nth;
75 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
76 struct GNUNET_PeerIdentity id;
77 struct GNUNET_MessageHeader *hello;
78 struct GNUNET_STATISTICS_Handle *stats;
79 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
80 struct GNUNET_ATS_ConnectivityHandle *ats;
81 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
83 struct GNUNET_OS_Process *arm_proc;
86 static struct PeerContext p1;
87 static struct PeerContext p2;
89 static unsigned long long current_quota_p1_in;
90 static unsigned long long current_quota_p1_out;
91 static unsigned long long current_quota_p2_in;
92 static unsigned long long current_quota_p2_out;
102 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
104 #define OKPP do { ok++; } while (0)
109 struct GNUNET_MessageHeader header;
115 terminate_peer (struct PeerContext *p)
119 GNUNET_CORE_notify_transmit_ready_cancel (p->nth);
124 GNUNET_CORE_disconnect (p->ch);
129 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
134 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
137 if (NULL != p->ats_sh)
139 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
144 GNUNET_ATS_connectivity_done (p->ats);
147 if (NULL != p->stats)
149 GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
152 if (NULL != p->hello)
154 GNUNET_free (p->hello);
161 shutdown_task (void *cls)
163 if (NULL != err_task)
165 GNUNET_SCHEDULER_cancel (err_task);
168 if (NULL != measure_task)
170 GNUNET_SCHEDULER_cancel (measure_task);
173 terminate_peer (&p1);
174 terminate_peer (&p2);
179 terminate_task_error (void *cls)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "Testcase failed (timeout)!\n");
184 GNUNET_SCHEDULER_shutdown ();
190 * Callback function to process statistic values.
193 * @param subsystem name of subsystem that created the statistic
194 * @param name the name of the datum
195 * @param value the current value
196 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
197 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
200 print_stat (void *cls,
201 const char *subsystem,
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Peer1 %50s = %12llu\n",
210 (unsigned long long) value);
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Peer2 %50s = %12llu\n",
215 (unsigned long long) value);
221 measurement_stop (void *cls)
223 unsigned long long delta;
224 unsigned long long throughput_out;
225 unsigned long long throughput_in;
226 unsigned long long max_quota_in;
227 unsigned long long max_quota_out;
228 unsigned long long quota_delta;
229 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
232 FPRINTF (stdout, "%s", "\n");
235 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
237 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
238 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
240 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
241 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
242 if (max_quota_out < max_quota_in)
243 quota_delta = max_quota_in / 3;
245 quota_delta = max_quota_out / 3;
247 if ((throughput_out > (max_quota_out + quota_delta)) ||
248 (throughput_in > (max_quota_in + quota_delta)))
252 GNUNET_STATISTICS_get (p1.stats,
254 "# discarded CORE_SEND requests",
258 GNUNET_STATISTICS_get (p1.stats,
260 "# discarded CORE_SEND request bytes",
264 GNUNET_STATISTICS_get (p1.stats,
266 "# discarded lower priority CORE_SEND requests",
270 GNUNET_STATISTICS_get (p1.stats,
272 "# discarded lower priority CORE_SEND request bytes",
276 GNUNET_STATISTICS_get (p2.stats,
278 "# discarded CORE_SEND requests",
283 GNUNET_STATISTICS_get (p2.stats,
285 "# discarded CORE_SEND request bytes",
289 GNUNET_STATISTICS_get (p2.stats,
291 "# discarded lower priority CORE_SEND requests",
295 GNUNET_STATISTICS_get (p2.stats,
297 "# discarded lower priority CORE_SEND request bytes",
303 kind = GNUNET_ERROR_TYPE_ERROR;
308 "Core quota compliance test with symmetric quotas: %s\n",
309 (0 == ok) ? "PASSED" : "FAILED");
311 case ASYMMETRIC_SEND_LIMITED:
313 "Core quota compliance test with limited sender quota: %s\n",
314 (0 == ok) ? "PASSED" : "FAILED");
316 case ASYMMETRIC_RECV_LIMITED:
318 "Core quota compliance test with limited receiver quota: %s\n",
319 (0 == ok) ? "PASSED" : "FAILED");
323 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
328 "Peer 1 send quota: %llu b/s\n",
329 current_quota_p1_out);
331 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
336 "Peer 2 receive quota: %llu b/s\n",
337 current_quota_p2_in);
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
342 GNUNET_SCHEDULER_shutdown ();
347 transmit_ready (void *cls,
352 struct TestMessage hdr;
356 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
359 if ( (NULL != p1.ch) &&
360 (1 == p1.connect_status) )
361 GNUNET_break (NULL !=
363 GNUNET_CORE_notify_transmit_ready (p1.ch,
365 GNUNET_CORE_PRIO_BEST_EFFORT,
373 GNUNET_assert (tr_n < TOTAL_MSGS);
375 GNUNET_assert (size >= MESSAGESIZE);
376 GNUNET_assert (NULL != buf);
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Sending message %u of size %u at offset %u\n",
385 hdr.header.size = htons (MESSAGESIZE);
386 hdr.header.type = htons (MTYPE);
387 hdr.num = htonl (tr_n);
388 GNUNET_memcpy (&cbuf[ret],
390 sizeof (struct TestMessage));
391 ret += sizeof (struct TestMessage);
394 MESSAGESIZE - sizeof (struct TestMessage));
395 ret += MESSAGESIZE - sizeof (struct TestMessage);
397 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
399 break; /* sometimes pack buffer full, sometimes not */
401 while (size - ret >= MESSAGESIZE);
402 GNUNET_SCHEDULER_cancel (err_task);
404 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
405 &terminate_task_error,
408 total_bytes_sent += ret;
414 connect_notify (void *cls,
415 const struct GNUNET_PeerIdentity *peer)
417 struct PeerContext *pc = cls;
419 if (0 == memcmp (&pc->id,
421 sizeof (struct GNUNET_PeerIdentity)))
422 return; /* loopback */
423 GNUNET_assert (0 == pc->connect_status);
424 pc->connect_status = 1;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Encrypted connection established to peer `%s'\n",
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Asking core (1) for transmission to peer `%s'\n",
432 GNUNET_i2s (&p2.id));
433 GNUNET_SCHEDULER_cancel (err_task);
435 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
436 &terminate_task_error,
438 start_time = GNUNET_TIME_absolute_get ();
439 running = GNUNET_YES;
441 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
445 GNUNET_break (NULL !=
447 GNUNET_CORE_notify_transmit_ready (p1.ch,
449 GNUNET_CORE_PRIO_BEST_EFFORT,
460 disconnect_notify (void *cls,
461 const struct GNUNET_PeerIdentity *peer)
463 struct PeerContext *pc = cls;
465 if (0 == memcmp (&pc->id,
467 sizeof (struct GNUNET_PeerIdentity)))
468 return; /* loopback */
469 pc->connect_status = 0;
470 if (NULL != measure_task)
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "Measurement aborted due to disconnect!\n");
474 GNUNET_SCHEDULER_cancel (measure_task);
479 GNUNET_CORE_notify_transmit_ready_cancel (pc->nth);
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "Encrypted connection to `%s' cut\n",
489 inbound_notify (void *cls,
490 const struct GNUNET_PeerIdentity *other,
491 const struct GNUNET_MessageHeader *message)
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Core provides inbound data from `%s' %u.\n",
496 (unsigned int) ntohs (message->size));
497 total_bytes_recv += ntohs (message->size);
503 outbound_notify (void *cls,
504 const struct GNUNET_PeerIdentity *other,
505 const struct GNUNET_MessageHeader *message)
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 "Core notifies about outbound data for `%s'.\n",
515 transmit_ready (void *cls, size_t size, void *buf);
519 process_mtype (void *cls,
520 const struct GNUNET_PeerIdentity *peer,
521 const struct GNUNET_MessageHeader *message)
524 const struct TestMessage *hdr;
526 hdr = (const struct TestMessage *) message;
527 if (MTYPE != ntohs (message->type))
528 return GNUNET_SYSERR;
529 if (ntohs (message->size) != MESSAGESIZE)
531 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
532 "Expected message %u of size %u, got %u bytes of message %u\n",
535 ntohs (message->size),
537 GNUNET_SCHEDULER_cancel (err_task);
538 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
540 return GNUNET_SYSERR;
542 if (ntohl (hdr->num) != n)
544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
545 "Expected message %u of size %u, got %u bytes of message %u\n",
548 ntohs (message->size),
550 GNUNET_SCHEDULER_cancel (err_task);
551 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
553 return GNUNET_SYSERR;
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Got message %u of size %u\n",
558 ntohs (message->size));
561 FPRINTF (stderr, "%s", ".");
563 if (GNUNET_YES == running)
564 GNUNET_break (NULL !=
565 (p1.nth = GNUNET_CORE_notify_transmit_ready (p1.ch,
567 GNUNET_CORE_PRIO_BEST_EFFORT,
577 static struct GNUNET_CORE_MessageHandler handlers[] = {
578 {&process_mtype, MTYPE, 0},
585 init_notify (void *cls,
586 const struct GNUNET_PeerIdentity *my_identity)
588 struct PeerContext *p = cls;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Connection to CORE service of `%4s' established\n",
592 GNUNET_i2s (my_identity));
593 GNUNET_assert (NULL != my_identity);
594 p->id = *my_identity;
597 GNUNET_assert (ok == 2);
601 GNUNET_CORE_connect (p2.cfg,
614 GNUNET_assert (ok == 3);
616 GNUNET_assert (cls == &p2);
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Asking core (1) to connect to peer `%s' and vice-versa\n",
619 GNUNET_i2s (&p2.id));
620 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
623 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
631 offer_hello_done (void *cls)
633 struct PeerContext *p = cls;
640 process_hello (void *cls,
641 const struct GNUNET_MessageHeader *message)
643 struct PeerContext *p = cls;
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Received (my) HELLO from transport service\n");
647 GNUNET_assert (message != NULL);
648 p->hello = GNUNET_malloc (ntohs (message->size));
649 GNUNET_memcpy (p->hello, message, ntohs (message->size));
650 if ((p == &p1) && (NULL == p2.oh))
651 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
655 if ((p == &p2) && (NULL == p1.oh))
656 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
660 if ((p == &p1) && (p2.hello != NULL) && (NULL == p1.oh))
661 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
665 if ((p == &p2) && (p1.hello != NULL) && (NULL == p2.oh) )
666 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
674 setup_peer (struct PeerContext *p,
679 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
680 p->cfg = GNUNET_CONFIGURATION_create ();
682 GNUNET_OS_start_process (GNUNET_YES,
683 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
686 "gnunet-service-arm",
690 GNUNET_assert (GNUNET_OK ==
691 GNUNET_CONFIGURATION_load (p->cfg,
693 p->stats = GNUNET_STATISTICS_create ("core",
695 GNUNET_assert (NULL != p->stats);
696 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
697 GNUNET_assert (NULL != p->ats);
698 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
699 GNUNET_TRANSPORT_AC_ANY,
702 GNUNET_free (binary);
710 const struct GNUNET_CONFIGURATION_Handle *cfg)
712 GNUNET_assert (ok == 1);
715 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
716 &terminate_task_error,
718 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
720 if (test == SYMMETRIC)
723 "test_core_quota_peer1.conf");
725 "test_core_quota_peer2.conf");
727 else if (test == ASYMMETRIC_SEND_LIMITED)
730 "test_core_quota_asymmetric_send_limit_peer1.conf");
732 "test_core_quota_asymmetric_send_limit_peer2.conf");
734 else if (test == ASYMMETRIC_RECV_LIMITED)
737 "test_core_quota_asymmetric_recv_limited_peer1.conf");
739 "test_core_quota_asymmetric_recv_limited_peer2.conf");
742 GNUNET_assert (test != -1);
743 GNUNET_assert (GNUNET_SYSERR !=
744 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
747 ¤t_quota_p1_in));
748 GNUNET_assert (GNUNET_SYSERR !=
749 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
752 ¤t_quota_p2_in));
753 GNUNET_assert (GNUNET_SYSERR !=
754 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
757 ¤t_quota_p1_out));
758 GNUNET_assert (GNUNET_SYSERR !=
759 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
762 ¤t_quota_p2_out));
765 GNUNET_CORE_connect (p1.cfg,
779 stop_arm (struct PeerContext *p)
781 if (0 != GNUNET_OS_process_kill (p->arm_proc,
783 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
786 GNUNET_OS_process_wait (p->arm_proc))
787 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "ARM process %u stopped\n",
791 GNUNET_OS_process_get_pid (p->arm_proc));
792 GNUNET_OS_process_destroy (p->arm_proc);
794 GNUNET_CONFIGURATION_destroy (p->cfg);
801 char *const argv[] = {
802 "test-core-quota-compliance",
804 "test_core_api_data.conf",
807 struct GNUNET_GETOPT_CommandLineOption options[] = {
808 GNUNET_GETOPT_OPTION_END
811 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
813 "test-core-quota-compliance",
825 cleanup_directory (int test)
829 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
830 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
832 case ASYMMETRIC_SEND_LIMITED:
833 GNUNET_DISK_directory_remove
834 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
835 GNUNET_DISK_directory_remove
836 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
838 case ASYMMETRIC_RECV_LIMITED:
839 GNUNET_DISK_directory_remove
840 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
841 GNUNET_DISK_directory_remove
842 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
855 if (NULL != strstr (argv[0],
860 else if (NULL != strstr (argv[0],
863 test = ASYMMETRIC_SEND_LIMITED;
865 else if (NULL != strstr (argv[0],
868 test = ASYMMETRIC_RECV_LIMITED;
870 GNUNET_assert (test != -1);
871 cleanup_directory (test);
872 GNUNET_log_setup ("test-core-quota-compliance",
876 cleanup_directory (test);
881 /* end of test_core_quota_compliance.c */