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/>.
19 * @file core/test_core_quota_compliance.c
20 * @brief testcase for core_api.c focusing quota compliance on core level
21 * @author Christian Grothoff
24 #include "gnunet_arm_service.h"
25 #include "gnunet_core_service.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_transport_hello_service.h"
30 #include "gnunet_statistics_service.h"
34 #define ASYMMETRIC_SEND_LIMITED 1
35 #define ASYMMETRIC_RECV_LIMITED 2
38 * Note that this value must not significantly exceed
39 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
40 * messages may be dropped even for a reliable transport.
42 #define TOTAL_MSGS (60000 * 10)
45 * How long until we give up on transmitting the message?
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
50 * What delay do we request from the core service for transmission?
52 #define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
55 #define MESSAGESIZE (1024 - 8)
56 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
58 static unsigned long long total_bytes_sent;
59 static unsigned long long total_bytes_recv;
61 static struct GNUNET_TIME_Absolute start_time;
63 static struct GNUNET_SCHEDULER_Task *err_task;
65 static struct GNUNET_SCHEDULER_Task *measure_task;
70 struct GNUNET_CONFIGURATION_Handle *cfg;
71 struct GNUNET_CORE_Handle *ch;
72 struct GNUNET_MQ_Handle *mq;
73 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
74 struct GNUNET_PeerIdentity id;
75 struct GNUNET_MessageHeader *hello;
76 struct GNUNET_STATISTICS_Handle *stats;
77 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
78 struct GNUNET_ATS_ConnectivityHandle *ats;
79 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
81 struct GNUNET_OS_Process *arm_proc;
84 static struct PeerContext p1;
85 static struct PeerContext p2;
87 static unsigned long long current_quota_p1_in;
88 static unsigned long long current_quota_p1_out;
89 static unsigned long long current_quota_p2_in;
90 static unsigned long long current_quota_p2_out;
100 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
102 #define OKPP do { ok++; } while (0)
107 struct GNUNET_MessageHeader header;
108 uint32_t num GNUNET_PACKED;
109 uint8_t pad[MESSAGESIZE];
114 terminate_peer (struct PeerContext *p)
118 GNUNET_CORE_disconnect (p->ch);
123 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
128 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
131 if (NULL != p->ats_sh)
133 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
138 GNUNET_ATS_connectivity_done (p->ats);
141 if (NULL != p->stats)
143 GNUNET_STATISTICS_destroy (p->stats, GNUNET_NO);
146 if (NULL != p->hello)
148 GNUNET_free (p->hello);
155 shutdown_task (void *cls)
157 if (NULL != err_task)
159 GNUNET_SCHEDULER_cancel (err_task);
162 if (NULL != measure_task)
164 GNUNET_SCHEDULER_cancel (measure_task);
167 terminate_peer (&p1);
168 terminate_peer (&p2);
173 terminate_task_error (void *cls)
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 "Testcase failed (timeout)!\n");
178 GNUNET_SCHEDULER_shutdown ();
184 * Callback function to process statistic values.
187 * @param subsystem name of subsystem that created the statistic
188 * @param name the name of the datum
189 * @param value the current value
190 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
191 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
194 print_stat (void *cls,
195 const char *subsystem,
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Peer1 %50s = %12llu\n",
204 (unsigned long long) value);
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Peer2 %50s = %12llu\n",
209 (unsigned long long) value);
215 measurement_stop (void *cls)
217 unsigned long long delta;
218 unsigned long long throughput_out;
219 unsigned long long throughput_in;
220 unsigned long long max_quota_in;
221 unsigned long long max_quota_out;
222 unsigned long long quota_delta;
223 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
226 FPRINTF (stdout, "%s", "\n");
229 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
232 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
233 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
235 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
236 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
237 if (max_quota_out < max_quota_in)
238 quota_delta = max_quota_in / 3;
240 quota_delta = max_quota_out / 3;
242 if ((throughput_out > (max_quota_out + quota_delta)) ||
243 (throughput_in > (max_quota_in + quota_delta)))
247 GNUNET_STATISTICS_get (p1.stats,
249 "# discarded CORE_SEND requests",
253 GNUNET_STATISTICS_get (p1.stats,
255 "# discarded CORE_SEND request bytes",
259 GNUNET_STATISTICS_get (p1.stats,
261 "# discarded lower priority CORE_SEND requests",
265 GNUNET_STATISTICS_get (p1.stats,
267 "# discarded lower priority CORE_SEND request bytes",
271 GNUNET_STATISTICS_get (p2.stats,
273 "# discarded CORE_SEND requests",
278 GNUNET_STATISTICS_get (p2.stats,
280 "# discarded CORE_SEND request bytes",
284 GNUNET_STATISTICS_get (p2.stats,
286 "# discarded lower priority CORE_SEND requests",
290 GNUNET_STATISTICS_get (p2.stats,
292 "# discarded lower priority CORE_SEND request bytes",
298 kind = GNUNET_ERROR_TYPE_ERROR;
303 "Core quota compliance test with symmetric quotas: %s\n",
304 (0 == ok) ? "PASSED" : "FAILED");
306 case ASYMMETRIC_SEND_LIMITED:
308 "Core quota compliance test with limited sender quota: %s\n",
309 (0 == ok) ? "PASSED" : "FAILED");
311 case ASYMMETRIC_RECV_LIMITED:
313 "Core quota compliance test with limited receiver quota: %s\n",
314 (0 == ok) ? "PASSED" : "FAILED");
318 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
323 "Peer 1 send quota: %llu b/s\n",
324 current_quota_p1_out);
326 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
331 "Peer 2 receive quota: %llu b/s\n",
332 current_quota_p2_in);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
337 GNUNET_SCHEDULER_shutdown ();
342 do_transmit (void *cls)
344 struct TestMessage *hdr;
345 struct GNUNET_MQ_Envelope *env;
347 env = GNUNET_MQ_msg (hdr,
349 hdr->num = htonl (tr_n);
354 GNUNET_SCHEDULER_cancel (err_task);
356 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
357 &terminate_task_error,
359 total_bytes_sent += sizeof (struct TestMessage);
360 GNUNET_MQ_send (p1.mq,
366 connect_notify (void *cls,
367 const struct GNUNET_PeerIdentity *peer,
368 struct GNUNET_MQ_Handle *mq)
370 struct PeerContext *pc = cls;
372 if (0 == memcmp (&pc->id,
374 sizeof (struct GNUNET_PeerIdentity)))
375 return NULL; /* loopback */
376 GNUNET_assert (0 == pc->connect_status);
377 pc->connect_status = 1;
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Encrypted connection established to peer `%s'\n",
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Asking core (1) for transmission to peer `%s'\n",
386 GNUNET_i2s (&p2.id));
387 GNUNET_SCHEDULER_cancel (err_task);
389 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
390 &terminate_task_error,
392 start_time = GNUNET_TIME_absolute_get ();
393 running = GNUNET_YES;
395 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
405 disconnect_notify (void *cls,
406 const struct GNUNET_PeerIdentity *peer,
409 struct PeerContext *pc = cls;
411 if (NULL == internal_cls)
412 return; /* loopback */
413 pc->connect_status = 0;
415 if (NULL != measure_task)
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 "Measurement aborted due to disconnect!\n");
419 GNUNET_SCHEDULER_cancel (measure_task);
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Encrypted connection to `%s' cut\n",
430 handle_test (void *cls,
431 const struct TestMessage *hdr)
435 total_bytes_recv += sizeof (struct TestMessage);
436 if (ntohl (hdr->num) != n)
438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
439 "Expected message %u, got message %u\n",
442 GNUNET_SCHEDULER_cancel (err_task);
443 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 FPRINTF (stderr, "%s", ".");
454 if (GNUNET_YES == running)
460 init_notify (void *cls,
461 const struct GNUNET_PeerIdentity *my_identity)
463 struct PeerContext *p = cls;
464 struct GNUNET_MQ_MessageHandler handlers[] = {
465 GNUNET_MQ_hd_fixed_size (test,
469 GNUNET_MQ_handler_end ()
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Connection to CORE service of `%s' established\n",
474 GNUNET_i2s (my_identity));
475 GNUNET_assert (NULL != my_identity);
476 p->id = *my_identity;
479 GNUNET_assert (ok == 2);
482 p2.ch = GNUNET_CORE_connect (p2.cfg,
491 GNUNET_assert (ok == 3);
493 GNUNET_assert (cls == &p2);
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Asking core (1) to connect to peer `%s' and vice-versa\n",
496 GNUNET_i2s (&p2.id));
497 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
500 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
508 offer_hello_done (void *cls)
510 struct PeerContext *p = cls;
517 process_hello (void *cls,
518 const struct GNUNET_MessageHeader *message)
520 struct PeerContext *p = cls;
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Received (my) HELLO from transport service\n");
524 GNUNET_assert (message != NULL);
525 p->hello = GNUNET_malloc (ntohs (message->size));
526 GNUNET_memcpy (p->hello, message, ntohs (message->size));
529 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
535 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
540 (NULL != p2.hello) &&
542 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
547 (NULL != p1.hello) &&
549 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
557 setup_peer (struct PeerContext *p,
562 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
563 p->cfg = GNUNET_CONFIGURATION_create ();
565 GNUNET_OS_start_process (GNUNET_YES,
566 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
569 "gnunet-service-arm",
573 GNUNET_assert (GNUNET_OK ==
574 GNUNET_CONFIGURATION_load (p->cfg,
576 p->stats = GNUNET_STATISTICS_create ("core",
578 GNUNET_assert (NULL != p->stats);
579 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
580 GNUNET_assert (NULL != p->ats);
581 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
582 GNUNET_TRANSPORT_AC_ANY,
585 GNUNET_free (binary);
593 const struct GNUNET_CONFIGURATION_Handle *cfg)
595 struct GNUNET_MQ_MessageHandler handlers[] = {
596 GNUNET_MQ_hd_fixed_size (test,
600 GNUNET_MQ_handler_end ()
603 GNUNET_assert (ok == 1);
606 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
607 &terminate_task_error,
609 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
611 if (test == SYMMETRIC)
614 "test_core_quota_peer1.conf");
616 "test_core_quota_peer2.conf");
618 else if (test == ASYMMETRIC_SEND_LIMITED)
621 "test_core_quota_asymmetric_send_limit_peer1.conf");
623 "test_core_quota_asymmetric_send_limit_peer2.conf");
625 else if (test == ASYMMETRIC_RECV_LIMITED)
628 "test_core_quota_asymmetric_recv_limited_peer1.conf");
630 "test_core_quota_asymmetric_recv_limited_peer2.conf");
633 GNUNET_assert (test != -1);
634 GNUNET_assert (GNUNET_SYSERR !=
635 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
638 ¤t_quota_p1_in));
639 GNUNET_assert (GNUNET_SYSERR !=
640 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
643 ¤t_quota_p2_in));
644 GNUNET_assert (GNUNET_SYSERR !=
645 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
648 ¤t_quota_p1_out));
649 GNUNET_assert (GNUNET_SYSERR !=
650 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
653 ¤t_quota_p2_out));
655 p1.ch = GNUNET_CORE_connect (p1.cfg,
665 stop_arm (struct PeerContext *p)
667 if (0 != GNUNET_OS_process_kill (p->arm_proc,
669 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
672 GNUNET_OS_process_wait (p->arm_proc))
673 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "ARM process %u stopped\n",
677 GNUNET_OS_process_get_pid (p->arm_proc));
678 GNUNET_OS_process_destroy (p->arm_proc);
680 GNUNET_CONFIGURATION_destroy (p->cfg);
687 char *const argv[] = {
688 "test-core-quota-compliance",
690 "test_core_api_data.conf",
693 struct GNUNET_GETOPT_CommandLineOption options[] = {
694 GNUNET_GETOPT_OPTION_END
697 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
699 "test-core-quota-compliance",
711 cleanup_directory (int test)
715 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
716 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
718 case ASYMMETRIC_SEND_LIMITED:
719 GNUNET_DISK_directory_remove
720 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
721 GNUNET_DISK_directory_remove
722 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
724 case ASYMMETRIC_RECV_LIMITED:
725 GNUNET_DISK_directory_remove
726 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
727 GNUNET_DISK_directory_remove
728 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
741 if (NULL != strstr (argv[0],
746 else if (NULL != strstr (argv[0],
749 test = ASYMMETRIC_SEND_LIMITED;
751 else if (NULL != strstr (argv[0],
754 test = ASYMMETRIC_RECV_LIMITED;
756 GNUNET_assert (test != -1);
757 cleanup_directory (test);
758 GNUNET_log_setup ("test-core-quota-compliance",
762 cleanup_directory (test);
767 /* end of test_core_quota_compliance.c */