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 - 8)
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_MQ_Handle *mq;
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;
110 uint32_t num GNUNET_PACKED;
111 uint8_t pad[MESSAGESIZE];
116 terminate_peer (struct PeerContext *p)
120 GNUNET_CORE_disconnecT (p->ch);
125 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
130 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
133 if (NULL != p->ats_sh)
135 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
140 GNUNET_ATS_connectivity_done (p->ats);
143 if (NULL != p->stats)
145 GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
148 if (NULL != p->hello)
150 GNUNET_free (p->hello);
157 shutdown_task (void *cls)
159 if (NULL != err_task)
161 GNUNET_SCHEDULER_cancel (err_task);
164 if (NULL != measure_task)
166 GNUNET_SCHEDULER_cancel (measure_task);
169 terminate_peer (&p1);
170 terminate_peer (&p2);
175 terminate_task_error (void *cls)
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "Testcase failed (timeout)!\n");
180 GNUNET_SCHEDULER_shutdown ();
186 * Callback function to process statistic values.
189 * @param subsystem name of subsystem that created the statistic
190 * @param name the name of the datum
191 * @param value the current value
192 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
193 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
196 print_stat (void *cls,
197 const char *subsystem,
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Peer1 %50s = %12llu\n",
206 (unsigned long long) value);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
209 "Peer2 %50s = %12llu\n",
211 (unsigned long long) value);
217 measurement_stop (void *cls)
219 unsigned long long delta;
220 unsigned long long throughput_out;
221 unsigned long long throughput_in;
222 unsigned long long max_quota_in;
223 unsigned long long max_quota_out;
224 unsigned long long quota_delta;
225 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
228 FPRINTF (stdout, "%s", "\n");
231 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
233 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
234 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
236 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
237 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
238 if (max_quota_out < max_quota_in)
239 quota_delta = max_quota_in / 3;
241 quota_delta = max_quota_out / 3;
243 if ((throughput_out > (max_quota_out + quota_delta)) ||
244 (throughput_in > (max_quota_in + quota_delta)))
248 GNUNET_STATISTICS_get (p1.stats,
250 "# discarded CORE_SEND requests",
254 GNUNET_STATISTICS_get (p1.stats,
256 "# discarded CORE_SEND request bytes",
260 GNUNET_STATISTICS_get (p1.stats,
262 "# discarded lower priority CORE_SEND requests",
266 GNUNET_STATISTICS_get (p1.stats,
268 "# discarded lower priority CORE_SEND request bytes",
272 GNUNET_STATISTICS_get (p2.stats,
274 "# discarded CORE_SEND requests",
279 GNUNET_STATISTICS_get (p2.stats,
281 "# discarded CORE_SEND request bytes",
285 GNUNET_STATISTICS_get (p2.stats,
287 "# discarded lower priority CORE_SEND requests",
291 GNUNET_STATISTICS_get (p2.stats,
293 "# discarded lower priority CORE_SEND request bytes",
299 kind = GNUNET_ERROR_TYPE_ERROR;
304 "Core quota compliance test with symmetric quotas: %s\n",
305 (0 == ok) ? "PASSED" : "FAILED");
307 case ASYMMETRIC_SEND_LIMITED:
309 "Core quota compliance test with limited sender quota: %s\n",
310 (0 == ok) ? "PASSED" : "FAILED");
312 case ASYMMETRIC_RECV_LIMITED:
314 "Core quota compliance test with limited receiver quota: %s\n",
315 (0 == ok) ? "PASSED" : "FAILED");
319 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
324 "Peer 1 send quota: %llu b/s\n",
325 current_quota_p1_out);
327 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
332 "Peer 2 receive quota: %llu b/s\n",
333 current_quota_p2_in);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
338 GNUNET_SCHEDULER_shutdown ();
343 do_transmit (void *cls)
345 struct TestMessage *hdr;
346 struct GNUNET_MQ_Envelope *env;
348 env = GNUNET_MQ_msg (hdr,
350 hdr->num = htonl (tr_n);
355 GNUNET_SCHEDULER_cancel (err_task);
357 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
358 &terminate_task_error,
360 total_bytes_sent += sizeof (struct TestMessage);
361 GNUNET_MQ_send (p1.mq,
367 connect_notify (void *cls,
368 const struct GNUNET_PeerIdentity *peer,
369 struct GNUNET_MQ_Handle *mq)
371 struct PeerContext *pc = cls;
373 if (0 == memcmp (&pc->id,
375 sizeof (struct GNUNET_PeerIdentity)))
376 return NULL; /* loopback */
377 GNUNET_assert (0 == pc->connect_status);
378 pc->connect_status = 1;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Encrypted connection established to peer `%s'\n",
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Asking core (1) for transmission to peer `%s'\n",
387 GNUNET_i2s (&p2.id));
388 GNUNET_SCHEDULER_cancel (err_task);
390 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
391 &terminate_task_error,
393 start_time = GNUNET_TIME_absolute_get ();
394 running = GNUNET_YES;
396 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
406 disconnect_notify (void *cls,
407 const struct GNUNET_PeerIdentity *peer,
410 struct PeerContext *pc = cls;
412 if (NULL == internal_cls)
413 return; /* loopback */
414 pc->connect_status = 0;
416 if (NULL != measure_task)
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
419 "Measurement aborted due to disconnect!\n");
420 GNUNET_SCHEDULER_cancel (measure_task);
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Encrypted connection to `%s' cut\n",
431 handle_test (void *cls,
432 const struct TestMessage *hdr)
436 total_bytes_recv += sizeof (struct TestMessage);
437 if (ntohl (hdr->num) != n)
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 "Expected message %u, got message %u\n",
443 GNUNET_SCHEDULER_cancel (err_task);
444 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 FPRINTF (stderr, "%s", ".");
455 if (GNUNET_YES == running)
461 init_notify (void *cls,
462 const struct GNUNET_PeerIdentity *my_identity)
464 struct PeerContext *p = cls;
465 struct GNUNET_MQ_MessageHandler handlers[] = {
466 GNUNET_MQ_hd_fixed_size (test,
470 GNUNET_MQ_handler_end ()
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Connection to CORE service of `%s' established\n",
475 GNUNET_i2s (my_identity));
476 GNUNET_assert (NULL != my_identity);
477 p->id = *my_identity;
480 GNUNET_assert (ok == 2);
483 p2.ch = GNUNET_CORE_connecT (p2.cfg,
492 GNUNET_assert (ok == 3);
494 GNUNET_assert (cls == &p2);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Asking core (1) to connect to peer `%s' and vice-versa\n",
497 GNUNET_i2s (&p2.id));
498 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
501 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
509 offer_hello_done (void *cls)
511 struct PeerContext *p = cls;
518 process_hello (void *cls,
519 const struct GNUNET_MessageHeader *message)
521 struct PeerContext *p = cls;
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Received (my) HELLO from transport service\n");
525 GNUNET_assert (message != NULL);
526 p->hello = GNUNET_malloc (ntohs (message->size));
527 GNUNET_memcpy (p->hello, message, ntohs (message->size));
530 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
536 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
541 (NULL != p2.hello) &&
543 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
548 (NULL != p1.hello) &&
550 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
558 setup_peer (struct PeerContext *p,
563 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
564 p->cfg = GNUNET_CONFIGURATION_create ();
566 GNUNET_OS_start_process (GNUNET_YES,
567 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
570 "gnunet-service-arm",
574 GNUNET_assert (GNUNET_OK ==
575 GNUNET_CONFIGURATION_load (p->cfg,
577 p->stats = GNUNET_STATISTICS_create ("core",
579 GNUNET_assert (NULL != p->stats);
580 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
581 GNUNET_assert (NULL != p->ats);
582 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
583 GNUNET_TRANSPORT_AC_ANY,
586 GNUNET_free (binary);
594 const struct GNUNET_CONFIGURATION_Handle *cfg)
596 struct GNUNET_MQ_MessageHandler handlers[] = {
597 GNUNET_MQ_hd_fixed_size (test,
601 GNUNET_MQ_handler_end ()
604 GNUNET_assert (ok == 1);
607 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
608 &terminate_task_error,
610 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
612 if (test == SYMMETRIC)
615 "test_core_quota_peer1.conf");
617 "test_core_quota_peer2.conf");
619 else if (test == ASYMMETRIC_SEND_LIMITED)
622 "test_core_quota_asymmetric_send_limit_peer1.conf");
624 "test_core_quota_asymmetric_send_limit_peer2.conf");
626 else if (test == ASYMMETRIC_RECV_LIMITED)
629 "test_core_quota_asymmetric_recv_limited_peer1.conf");
631 "test_core_quota_asymmetric_recv_limited_peer2.conf");
634 GNUNET_assert (test != -1);
635 GNUNET_assert (GNUNET_SYSERR !=
636 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
639 ¤t_quota_p1_in));
640 GNUNET_assert (GNUNET_SYSERR !=
641 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
644 ¤t_quota_p2_in));
645 GNUNET_assert (GNUNET_SYSERR !=
646 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
649 ¤t_quota_p1_out));
650 GNUNET_assert (GNUNET_SYSERR !=
651 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
654 ¤t_quota_p2_out));
656 p1.ch = GNUNET_CORE_connecT (p1.cfg,
666 stop_arm (struct PeerContext *p)
668 if (0 != GNUNET_OS_process_kill (p->arm_proc,
670 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
673 GNUNET_OS_process_wait (p->arm_proc))
674 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "ARM process %u stopped\n",
678 GNUNET_OS_process_get_pid (p->arm_proc));
679 GNUNET_OS_process_destroy (p->arm_proc);
681 GNUNET_CONFIGURATION_destroy (p->cfg);
688 char *const argv[] = {
689 "test-core-quota-compliance",
691 "test_core_api_data.conf",
694 struct GNUNET_GETOPT_CommandLineOption options[] = {
695 GNUNET_GETOPT_OPTION_END
698 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
700 "test-core-quota-compliance",
712 cleanup_directory (int test)
716 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
717 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
719 case ASYMMETRIC_SEND_LIMITED:
720 GNUNET_DISK_directory_remove
721 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
722 GNUNET_DISK_directory_remove
723 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
725 case ASYMMETRIC_RECV_LIMITED:
726 GNUNET_DISK_directory_remove
727 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
728 GNUNET_DISK_directory_remove
729 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
742 if (NULL != strstr (argv[0],
747 else if (NULL != strstr (argv[0],
750 test = ASYMMETRIC_SEND_LIMITED;
752 else if (NULL != strstr (argv[0],
755 test = ASYMMETRIC_RECV_LIMITED;
757 GNUNET_assert (test != -1);
758 cleanup_directory (test);
759 GNUNET_log_setup ("test-core-quota-compliance",
763 cleanup_directory (test);
768 /* end of test_core_quota_compliance.c */