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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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, \
58 #define MESSAGESIZE (1024 - 8)
59 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply ( \
60 GNUNET_TIME_UNIT_SECONDS, 30)
62 static unsigned long long total_bytes_sent;
63 static unsigned long long total_bytes_recv;
65 static struct GNUNET_TIME_Absolute start_time;
67 static struct GNUNET_SCHEDULER_Task *err_task;
69 static struct GNUNET_SCHEDULER_Task *measure_task;
74 struct GNUNET_CONFIGURATION_Handle *cfg;
75 struct GNUNET_CORE_Handle *ch;
76 struct GNUNET_MQ_Handle *mq;
77 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
78 struct GNUNET_PeerIdentity id;
79 struct GNUNET_MessageHeader *hello;
80 struct GNUNET_STATISTICS_Handle *stats;
81 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
82 struct GNUNET_ATS_ConnectivityHandle *ats;
83 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
85 struct GNUNET_OS_Process *arm_proc;
88 static struct PeerContext p1;
89 static struct PeerContext p2;
91 static unsigned long long current_quota_p1_in;
92 static unsigned long long current_quota_p1_out;
93 static unsigned long long current_quota_p2_in;
94 static unsigned long long current_quota_p2_out;
104 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, \
105 "Now at stage %u at %s:%u\n", ok, __FILE__, \
106 __LINE__); } while (0)
108 #define OKPP do { ok++; } while (0)
113 struct GNUNET_MessageHeader header;
114 uint32_t num GNUNET_PACKED;
115 uint8_t pad[MESSAGESIZE];
120 terminate_peer (struct PeerContext *p)
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;
238 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
239 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
241 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
242 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
243 if (max_quota_out < max_quota_in)
244 quota_delta = max_quota_in / 3;
246 quota_delta = max_quota_out / 3;
248 if ((throughput_out > (max_quota_out + quota_delta)) ||
249 (throughput_in > (max_quota_in + quota_delta)))
253 GNUNET_STATISTICS_get (p1.stats,
255 "# discarded CORE_SEND requests",
259 GNUNET_STATISTICS_get (p1.stats,
261 "# discarded CORE_SEND request bytes",
265 GNUNET_STATISTICS_get (p1.stats,
267 "# discarded lower priority CORE_SEND requests",
271 GNUNET_STATISTICS_get (p1.stats,
273 "# discarded lower priority CORE_SEND request bytes",
277 GNUNET_STATISTICS_get (p2.stats,
279 "# discarded CORE_SEND requests",
284 GNUNET_STATISTICS_get (p2.stats,
286 "# discarded CORE_SEND request bytes",
290 GNUNET_STATISTICS_get (p2.stats,
292 "# discarded lower priority CORE_SEND requests",
296 GNUNET_STATISTICS_get (p2.stats,
298 "# discarded lower priority CORE_SEND request bytes",
304 kind = GNUNET_ERROR_TYPE_ERROR;
309 "Core quota compliance test with symmetric quotas: %s\n",
310 (0 == ok) ? "PASSED" : "FAILED");
313 case ASYMMETRIC_SEND_LIMITED:
315 "Core quota compliance test with limited sender quota: %s\n",
316 (0 == ok) ? "PASSED" : "FAILED");
319 case ASYMMETRIC_RECV_LIMITED:
321 "Core quota compliance test with limited receiver quota: %s\n",
322 (0 == ok) ? "PASSED" : "FAILED");
327 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
332 "Peer 1 send quota: %llu b/s\n",
333 current_quota_p1_out);
335 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
340 "Peer 2 receive quota: %llu b/s\n",
341 current_quota_p2_in);
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
346 GNUNET_SCHEDULER_shutdown ();
351 do_transmit (void *cls)
353 struct TestMessage *hdr;
354 struct GNUNET_MQ_Envelope *env;
356 env = GNUNET_MQ_msg (hdr,
358 hdr->num = htonl (tr_n);
363 GNUNET_SCHEDULER_cancel (err_task);
365 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
366 &terminate_task_error,
368 total_bytes_sent += sizeof(struct TestMessage);
369 GNUNET_MQ_send (p1.mq,
375 connect_notify (void *cls,
376 const struct GNUNET_PeerIdentity *peer,
377 struct GNUNET_MQ_Handle *mq)
379 struct PeerContext *pc = cls;
381 if (0 == memcmp (&pc->id,
383 sizeof(struct GNUNET_PeerIdentity)))
384 return NULL; /* loopback */
385 GNUNET_assert (0 == pc->connect_status);
386 pc->connect_status = 1;
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Encrypted connection established to peer `%s'\n",
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Asking core (1) for transmission to peer `%s'\n",
395 GNUNET_i2s (&p2.id));
396 GNUNET_SCHEDULER_cancel (err_task);
398 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
399 &terminate_task_error,
401 start_time = GNUNET_TIME_absolute_get ();
402 running = GNUNET_YES;
404 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
414 disconnect_notify (void *cls,
415 const struct GNUNET_PeerIdentity *peer,
418 struct PeerContext *pc = cls;
420 if (NULL == internal_cls)
421 return; /* loopback */
422 pc->connect_status = 0;
424 if (NULL != measure_task)
426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
427 "Measurement aborted due to disconnect!\n");
428 GNUNET_SCHEDULER_cancel (measure_task);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Encrypted connection to `%s' cut\n",
438 handle_test (void *cls,
439 const struct TestMessage *hdr)
443 total_bytes_recv += sizeof(struct TestMessage);
444 if (ntohl (hdr->num) != n)
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
447 "Expected message %u, got message %u\n",
450 GNUNET_SCHEDULER_cancel (err_task);
451 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 fprintf (stderr, "%s", ".");
462 if (GNUNET_YES == running)
468 init_notify (void *cls,
469 const struct GNUNET_PeerIdentity *my_identity)
471 struct PeerContext *p = cls;
472 struct GNUNET_MQ_MessageHandler handlers[] = {
473 GNUNET_MQ_hd_fixed_size (test,
477 GNUNET_MQ_handler_end ()
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Connection to CORE service of `%s' established\n",
482 GNUNET_i2s (my_identity));
483 GNUNET_assert (NULL != my_identity);
484 p->id = *my_identity;
487 GNUNET_assert (ok == 2);
490 p2.ch = GNUNET_CORE_connect (p2.cfg,
499 GNUNET_assert (ok == 3);
501 GNUNET_assert (cls == &p2);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Asking core (1) to connect to peer `%s' and vice-versa\n",
504 GNUNET_i2s (&p2.id));
505 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
508 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
516 offer_hello_done (void *cls)
518 struct PeerContext *p = cls;
525 process_hello (void *cls,
526 const struct GNUNET_MessageHeader *message)
528 struct PeerContext *p = cls;
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "Received (my) HELLO from transport service\n");
532 GNUNET_assert (message != NULL);
533 p->hello = GNUNET_malloc (ntohs (message->size));
534 GNUNET_memcpy (p->hello, message, ntohs (message->size));
537 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
543 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
548 (NULL != p2.hello) &&
550 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
555 (NULL != p1.hello) &&
557 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
565 setup_peer (struct PeerContext *p,
570 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
571 p->cfg = GNUNET_CONFIGURATION_create ();
573 GNUNET_OS_start_process (GNUNET_YES,
574 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
577 "gnunet-service-arm",
581 GNUNET_assert (GNUNET_OK ==
582 GNUNET_CONFIGURATION_load (p->cfg,
584 p->stats = GNUNET_STATISTICS_create ("core",
586 GNUNET_assert (NULL != p->stats);
587 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
588 GNUNET_assert (NULL != p->ats);
589 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
590 GNUNET_TRANSPORT_AC_ANY,
593 GNUNET_free (binary);
601 const struct GNUNET_CONFIGURATION_Handle *cfg)
603 struct GNUNET_MQ_MessageHandler handlers[] = {
604 GNUNET_MQ_hd_fixed_size (test,
608 GNUNET_MQ_handler_end ()
611 GNUNET_assert (ok == 1);
614 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
615 &terminate_task_error,
617 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
619 if (test == SYMMETRIC)
622 "test_core_quota_peer1.conf");
624 "test_core_quota_peer2.conf");
626 else if (test == ASYMMETRIC_SEND_LIMITED)
629 "test_core_quota_asymmetric_send_limit_peer1.conf");
631 "test_core_quota_asymmetric_send_limit_peer2.conf");
633 else if (test == ASYMMETRIC_RECV_LIMITED)
636 "test_core_quota_asymmetric_recv_limited_peer1.conf");
638 "test_core_quota_asymmetric_recv_limited_peer2.conf");
641 GNUNET_assert (test != -1);
642 GNUNET_assert (GNUNET_SYSERR !=
643 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
646 ¤t_quota_p1_in));
647 GNUNET_assert (GNUNET_SYSERR !=
648 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
651 ¤t_quota_p2_in));
652 GNUNET_assert (GNUNET_SYSERR !=
653 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
656 ¤t_quota_p1_out));
657 GNUNET_assert (GNUNET_SYSERR !=
658 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
661 ¤t_quota_p2_out));
663 p1.ch = GNUNET_CORE_connect (p1.cfg,
673 stop_arm (struct PeerContext *p)
675 if (0 != GNUNET_OS_process_kill (p->arm_proc,
677 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
680 GNUNET_OS_process_wait (p->arm_proc))
681 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
684 "ARM process %u stopped\n",
685 GNUNET_OS_process_get_pid (p->arm_proc));
686 GNUNET_OS_process_destroy (p->arm_proc);
688 GNUNET_CONFIGURATION_destroy (p->cfg);
695 char *const argv[] = {
696 "test-core-quota-compliance",
698 "test_core_api_data.conf",
701 struct GNUNET_GETOPT_CommandLineOption options[] = {
702 GNUNET_GETOPT_OPTION_END
706 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
708 "test-core-quota-compliance",
720 cleanup_directory (int test)
725 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
726 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
729 case ASYMMETRIC_SEND_LIMITED:
730 GNUNET_DISK_directory_remove
731 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
732 GNUNET_DISK_directory_remove
733 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
736 case ASYMMETRIC_RECV_LIMITED:
737 GNUNET_DISK_directory_remove
738 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
739 GNUNET_DISK_directory_remove
740 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
753 if (NULL != strstr (argv[0],
758 else if (NULL != strstr (argv[0],
761 test = ASYMMETRIC_SEND_LIMITED;
763 else if (NULL != strstr (argv[0],
766 test = ASYMMETRIC_RECV_LIMITED;
768 GNUNET_assert (test != -1);
769 cleanup_directory (test);
770 GNUNET_log_setup ("test-core-quota-compliance",
774 cleanup_directory (test);
779 /* end of test_core_quota_compliance.c */