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, 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;
234 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
235 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
237 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
238 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
239 if (max_quota_out < max_quota_in)
240 quota_delta = max_quota_in / 3;
242 quota_delta = max_quota_out / 3;
244 if ((throughput_out > (max_quota_out + quota_delta)) ||
245 (throughput_in > (max_quota_in + quota_delta)))
249 GNUNET_STATISTICS_get (p1.stats,
251 "# discarded CORE_SEND requests",
255 GNUNET_STATISTICS_get (p1.stats,
257 "# discarded CORE_SEND request bytes",
261 GNUNET_STATISTICS_get (p1.stats,
263 "# discarded lower priority CORE_SEND requests",
267 GNUNET_STATISTICS_get (p1.stats,
269 "# discarded lower priority CORE_SEND request bytes",
273 GNUNET_STATISTICS_get (p2.stats,
275 "# discarded CORE_SEND requests",
280 GNUNET_STATISTICS_get (p2.stats,
282 "# discarded CORE_SEND request bytes",
286 GNUNET_STATISTICS_get (p2.stats,
288 "# discarded lower priority CORE_SEND requests",
292 GNUNET_STATISTICS_get (p2.stats,
294 "# discarded lower priority CORE_SEND request bytes",
300 kind = GNUNET_ERROR_TYPE_ERROR;
305 "Core quota compliance test with symmetric quotas: %s\n",
306 (0 == ok) ? "PASSED" : "FAILED");
308 case ASYMMETRIC_SEND_LIMITED:
310 "Core quota compliance test with limited sender quota: %s\n",
311 (0 == ok) ? "PASSED" : "FAILED");
313 case ASYMMETRIC_RECV_LIMITED:
315 "Core quota compliance test with limited receiver quota: %s\n",
316 (0 == ok) ? "PASSED" : "FAILED");
320 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
325 "Peer 1 send quota: %llu b/s\n",
326 current_quota_p1_out);
328 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
333 "Peer 2 receive quota: %llu b/s\n",
334 current_quota_p2_in);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
339 GNUNET_SCHEDULER_shutdown ();
344 do_transmit (void *cls)
346 struct TestMessage *hdr;
347 struct GNUNET_MQ_Envelope *env;
349 env = GNUNET_MQ_msg (hdr,
351 hdr->num = htonl (tr_n);
356 GNUNET_SCHEDULER_cancel (err_task);
358 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
359 &terminate_task_error,
361 total_bytes_sent += sizeof (struct TestMessage);
362 GNUNET_MQ_send (p1.mq,
368 connect_notify (void *cls,
369 const struct GNUNET_PeerIdentity *peer,
370 struct GNUNET_MQ_Handle *mq)
372 struct PeerContext *pc = cls;
374 if (0 == memcmp (&pc->id,
376 sizeof (struct GNUNET_PeerIdentity)))
377 return NULL; /* loopback */
378 GNUNET_assert (0 == pc->connect_status);
379 pc->connect_status = 1;
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Encrypted connection established to peer `%s'\n",
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Asking core (1) for transmission to peer `%s'\n",
388 GNUNET_i2s (&p2.id));
389 GNUNET_SCHEDULER_cancel (err_task);
391 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
392 &terminate_task_error,
394 start_time = GNUNET_TIME_absolute_get ();
395 running = GNUNET_YES;
397 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
407 disconnect_notify (void *cls,
408 const struct GNUNET_PeerIdentity *peer,
411 struct PeerContext *pc = cls;
413 if (NULL == internal_cls)
414 return; /* loopback */
415 pc->connect_status = 0;
417 if (NULL != measure_task)
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "Measurement aborted due to disconnect!\n");
421 GNUNET_SCHEDULER_cancel (measure_task);
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "Encrypted connection to `%s' cut\n",
432 handle_test (void *cls,
433 const struct TestMessage *hdr)
437 total_bytes_recv += sizeof (struct TestMessage);
438 if (ntohl (hdr->num) != n)
440 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
441 "Expected message %u, got message %u\n",
444 GNUNET_SCHEDULER_cancel (err_task);
445 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 fprintf (stderr, "%s", ".");
456 if (GNUNET_YES == running)
462 init_notify (void *cls,
463 const struct GNUNET_PeerIdentity *my_identity)
465 struct PeerContext *p = cls;
466 struct GNUNET_MQ_MessageHandler handlers[] = {
467 GNUNET_MQ_hd_fixed_size (test,
471 GNUNET_MQ_handler_end ()
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Connection to CORE service of `%s' established\n",
476 GNUNET_i2s (my_identity));
477 GNUNET_assert (NULL != my_identity);
478 p->id = *my_identity;
481 GNUNET_assert (ok == 2);
484 p2.ch = GNUNET_CORE_connect (p2.cfg,
493 GNUNET_assert (ok == 3);
495 GNUNET_assert (cls == &p2);
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Asking core (1) to connect to peer `%s' and vice-versa\n",
498 GNUNET_i2s (&p2.id));
499 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
502 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
510 offer_hello_done (void *cls)
512 struct PeerContext *p = cls;
519 process_hello (void *cls,
520 const struct GNUNET_MessageHeader *message)
522 struct PeerContext *p = cls;
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Received (my) HELLO from transport service\n");
526 GNUNET_assert (message != NULL);
527 p->hello = GNUNET_malloc (ntohs (message->size));
528 GNUNET_memcpy (p->hello, message, ntohs (message->size));
531 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
537 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
542 (NULL != p2.hello) &&
544 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
549 (NULL != p1.hello) &&
551 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
559 setup_peer (struct PeerContext *p,
564 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
565 p->cfg = GNUNET_CONFIGURATION_create ();
567 GNUNET_OS_start_process (GNUNET_YES,
568 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
571 "gnunet-service-arm",
575 GNUNET_assert (GNUNET_OK ==
576 GNUNET_CONFIGURATION_load (p->cfg,
578 p->stats = GNUNET_STATISTICS_create ("core",
580 GNUNET_assert (NULL != p->stats);
581 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
582 GNUNET_assert (NULL != p->ats);
583 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
584 GNUNET_TRANSPORT_AC_ANY,
587 GNUNET_free (binary);
595 const struct GNUNET_CONFIGURATION_Handle *cfg)
597 struct GNUNET_MQ_MessageHandler handlers[] = {
598 GNUNET_MQ_hd_fixed_size (test,
602 GNUNET_MQ_handler_end ()
605 GNUNET_assert (ok == 1);
608 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
609 &terminate_task_error,
611 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
613 if (test == SYMMETRIC)
616 "test_core_quota_peer1.conf");
618 "test_core_quota_peer2.conf");
620 else if (test == ASYMMETRIC_SEND_LIMITED)
623 "test_core_quota_asymmetric_send_limit_peer1.conf");
625 "test_core_quota_asymmetric_send_limit_peer2.conf");
627 else if (test == ASYMMETRIC_RECV_LIMITED)
630 "test_core_quota_asymmetric_recv_limited_peer1.conf");
632 "test_core_quota_asymmetric_recv_limited_peer2.conf");
635 GNUNET_assert (test != -1);
636 GNUNET_assert (GNUNET_SYSERR !=
637 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
640 ¤t_quota_p1_in));
641 GNUNET_assert (GNUNET_SYSERR !=
642 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
645 ¤t_quota_p2_in));
646 GNUNET_assert (GNUNET_SYSERR !=
647 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
650 ¤t_quota_p1_out));
651 GNUNET_assert (GNUNET_SYSERR !=
652 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
655 ¤t_quota_p2_out));
657 p1.ch = GNUNET_CORE_connect (p1.cfg,
667 stop_arm (struct PeerContext *p)
669 if (0 != GNUNET_OS_process_kill (p->arm_proc,
671 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
674 GNUNET_OS_process_wait (p->arm_proc))
675 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "ARM process %u stopped\n",
679 GNUNET_OS_process_get_pid (p->arm_proc));
680 GNUNET_OS_process_destroy (p->arm_proc);
682 GNUNET_CONFIGURATION_destroy (p->cfg);
689 char *const argv[] = {
690 "test-core-quota-compliance",
692 "test_core_api_data.conf",
695 struct GNUNET_GETOPT_CommandLineOption options[] = {
696 GNUNET_GETOPT_OPTION_END
699 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
701 "test-core-quota-compliance",
713 cleanup_directory (int test)
717 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
718 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
720 case ASYMMETRIC_SEND_LIMITED:
721 GNUNET_DISK_directory_remove
722 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
723 GNUNET_DISK_directory_remove
724 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
726 case ASYMMETRIC_RECV_LIMITED:
727 GNUNET_DISK_directory_remove
728 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
729 GNUNET_DISK_directory_remove
730 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
743 if (NULL != strstr (argv[0],
748 else if (NULL != strstr (argv[0],
751 test = ASYMMETRIC_SEND_LIMITED;
753 else if (NULL != strstr (argv[0],
756 test = ASYMMETRIC_RECV_LIMITED;
758 GNUNET_assert (test != -1);
759 cleanup_directory (test);
760 GNUNET_log_setup ("test-core-quota-compliance",
764 cleanup_directory (test);
769 /* end of test_core_quota_compliance.c */