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;
231 throughput_out = total_bytes_sent * 1000000LL / delta; /* convert to bytes/s */
232 throughput_in = total_bytes_recv * 1000000LL / delta; /* convert to bytes/s */
234 max_quota_in = GNUNET_MIN (current_quota_p1_in, current_quota_p2_in);
235 max_quota_out = GNUNET_MIN (current_quota_p1_out, current_quota_p2_out);
236 if (max_quota_out < max_quota_in)
237 quota_delta = max_quota_in / 3;
239 quota_delta = max_quota_out / 3;
241 if ((throughput_out > (max_quota_out + quota_delta)) ||
242 (throughput_in > (max_quota_in + quota_delta)))
246 GNUNET_STATISTICS_get (p1.stats,
248 "# discarded CORE_SEND requests",
252 GNUNET_STATISTICS_get (p1.stats,
254 "# discarded CORE_SEND request bytes",
258 GNUNET_STATISTICS_get (p1.stats,
260 "# discarded lower priority CORE_SEND requests",
264 GNUNET_STATISTICS_get (p1.stats,
266 "# discarded lower priority CORE_SEND request bytes",
270 GNUNET_STATISTICS_get (p2.stats,
272 "# discarded CORE_SEND requests",
277 GNUNET_STATISTICS_get (p2.stats,
279 "# discarded CORE_SEND request bytes",
283 GNUNET_STATISTICS_get (p2.stats,
285 "# discarded lower priority CORE_SEND requests",
289 GNUNET_STATISTICS_get (p2.stats,
291 "# discarded lower priority CORE_SEND request bytes",
297 kind = GNUNET_ERROR_TYPE_ERROR;
302 "Core quota compliance test with symmetric quotas: %s\n",
303 (0 == ok) ? "PASSED" : "FAILED");
305 case ASYMMETRIC_SEND_LIMITED:
307 "Core quota compliance test with limited sender quota: %s\n",
308 (0 == ok) ? "PASSED" : "FAILED");
310 case ASYMMETRIC_RECV_LIMITED:
312 "Core quota compliance test with limited receiver quota: %s\n",
313 (0 == ok) ? "PASSED" : "FAILED");
317 "Peer 1 send rate: %llu b/s (%llu bytes in %llu ms)\n",
322 "Peer 1 send quota: %llu b/s\n",
323 current_quota_p1_out);
325 "Peer 2 receive rate: %llu b/s (%llu bytes in %llu ms)\n",
330 "Peer 2 receive quota: %llu b/s\n",
331 current_quota_p2_in);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu b/s\n",max_quota_in );
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu b/s\n",max_quota_out);
336 GNUNET_SCHEDULER_shutdown ();
341 do_transmit (void *cls)
343 struct TestMessage *hdr;
344 struct GNUNET_MQ_Envelope *env;
346 env = GNUNET_MQ_msg (hdr,
348 hdr->num = htonl (tr_n);
353 GNUNET_SCHEDULER_cancel (err_task);
355 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
356 &terminate_task_error,
358 total_bytes_sent += sizeof (struct TestMessage);
359 GNUNET_MQ_send (p1.mq,
365 connect_notify (void *cls,
366 const struct GNUNET_PeerIdentity *peer,
367 struct GNUNET_MQ_Handle *mq)
369 struct PeerContext *pc = cls;
371 if (0 == memcmp (&pc->id,
373 sizeof (struct GNUNET_PeerIdentity)))
374 return NULL; /* loopback */
375 GNUNET_assert (0 == pc->connect_status);
376 pc->connect_status = 1;
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Encrypted connection established to peer `%s'\n",
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Asking core (1) for transmission to peer `%s'\n",
385 GNUNET_i2s (&p2.id));
386 GNUNET_SCHEDULER_cancel (err_task);
388 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
389 &terminate_task_error,
391 start_time = GNUNET_TIME_absolute_get ();
392 running = GNUNET_YES;
394 GNUNET_SCHEDULER_add_delayed (MEASUREMENT_LENGTH,
404 disconnect_notify (void *cls,
405 const struct GNUNET_PeerIdentity *peer,
408 struct PeerContext *pc = cls;
410 if (NULL == internal_cls)
411 return; /* loopback */
412 pc->connect_status = 0;
414 if (NULL != measure_task)
416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
417 "Measurement aborted due to disconnect!\n");
418 GNUNET_SCHEDULER_cancel (measure_task);
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Encrypted connection to `%s' cut\n",
429 handle_test (void *cls,
430 const struct TestMessage *hdr)
434 total_bytes_recv += sizeof (struct TestMessage);
435 if (ntohl (hdr->num) != n)
437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
438 "Expected message %u, got message %u\n",
441 GNUNET_SCHEDULER_cancel (err_task);
442 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error,
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 FPRINTF (stderr, "%s", ".");
453 if (GNUNET_YES == running)
459 init_notify (void *cls,
460 const struct GNUNET_PeerIdentity *my_identity)
462 struct PeerContext *p = cls;
463 struct GNUNET_MQ_MessageHandler handlers[] = {
464 GNUNET_MQ_hd_fixed_size (test,
468 GNUNET_MQ_handler_end ()
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Connection to CORE service of `%s' established\n",
473 GNUNET_i2s (my_identity));
474 GNUNET_assert (NULL != my_identity);
475 p->id = *my_identity;
478 GNUNET_assert (ok == 2);
481 p2.ch = GNUNET_CORE_connect (p2.cfg,
490 GNUNET_assert (ok == 3);
492 GNUNET_assert (cls == &p2);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Asking core (1) to connect to peer `%s' and vice-versa\n",
495 GNUNET_i2s (&p2.id));
496 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
499 p2.ats_sh = GNUNET_ATS_connectivity_suggest (p2.ats,
507 offer_hello_done (void *cls)
509 struct PeerContext *p = cls;
516 process_hello (void *cls,
517 const struct GNUNET_MessageHeader *message)
519 struct PeerContext *p = cls;
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Received (my) HELLO from transport service\n");
523 GNUNET_assert (message != NULL);
524 p->hello = GNUNET_malloc (ntohs (message->size));
525 GNUNET_memcpy (p->hello, message, ntohs (message->size));
528 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
534 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg, message,
539 (NULL != p2.hello) &&
541 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
546 (NULL != p1.hello) &&
548 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg,
556 setup_peer (struct PeerContext *p,
561 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
562 p->cfg = GNUNET_CONFIGURATION_create ();
564 GNUNET_OS_start_process (GNUNET_YES,
565 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
568 "gnunet-service-arm",
572 GNUNET_assert (GNUNET_OK ==
573 GNUNET_CONFIGURATION_load (p->cfg,
575 p->stats = GNUNET_STATISTICS_create ("core",
577 GNUNET_assert (NULL != p->stats);
578 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
579 GNUNET_assert (NULL != p->ats);
580 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
581 GNUNET_TRANSPORT_AC_ANY,
584 GNUNET_free (binary);
592 const struct GNUNET_CONFIGURATION_Handle *cfg)
594 struct GNUNET_MQ_MessageHandler handlers[] = {
595 GNUNET_MQ_hd_fixed_size (test,
599 GNUNET_MQ_handler_end ()
602 GNUNET_assert (ok == 1);
605 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
606 &terminate_task_error,
608 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
610 if (test == SYMMETRIC)
613 "test_core_quota_peer1.conf");
615 "test_core_quota_peer2.conf");
617 else if (test == ASYMMETRIC_SEND_LIMITED)
620 "test_core_quota_asymmetric_send_limit_peer1.conf");
622 "test_core_quota_asymmetric_send_limit_peer2.conf");
624 else if (test == ASYMMETRIC_RECV_LIMITED)
627 "test_core_quota_asymmetric_recv_limited_peer1.conf");
629 "test_core_quota_asymmetric_recv_limited_peer2.conf");
632 GNUNET_assert (test != -1);
633 GNUNET_assert (GNUNET_SYSERR !=
634 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
637 ¤t_quota_p1_in));
638 GNUNET_assert (GNUNET_SYSERR !=
639 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
642 ¤t_quota_p2_in));
643 GNUNET_assert (GNUNET_SYSERR !=
644 GNUNET_CONFIGURATION_get_value_size (p1.cfg,
647 ¤t_quota_p1_out));
648 GNUNET_assert (GNUNET_SYSERR !=
649 GNUNET_CONFIGURATION_get_value_size (p2.cfg,
652 ¤t_quota_p2_out));
654 p1.ch = GNUNET_CORE_connect (p1.cfg,
664 stop_arm (struct PeerContext *p)
666 if (0 != GNUNET_OS_process_kill (p->arm_proc,
668 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
671 GNUNET_OS_process_wait (p->arm_proc))
672 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "ARM process %u stopped\n",
676 GNUNET_OS_process_get_pid (p->arm_proc));
677 GNUNET_OS_process_destroy (p->arm_proc);
679 GNUNET_CONFIGURATION_destroy (p->cfg);
686 char *const argv[] = {
687 "test-core-quota-compliance",
689 "test_core_api_data.conf",
692 struct GNUNET_GETOPT_CommandLineOption options[] = {
693 GNUNET_GETOPT_OPTION_END
696 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
698 "test-core-quota-compliance",
710 cleanup_directory (int test)
714 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
715 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
717 case ASYMMETRIC_SEND_LIMITED:
718 GNUNET_DISK_directory_remove
719 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
720 GNUNET_DISK_directory_remove
721 ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
723 case ASYMMETRIC_RECV_LIMITED:
724 GNUNET_DISK_directory_remove
725 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
726 GNUNET_DISK_directory_remove
727 ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
740 if (NULL != strstr (argv[0],
745 else if (NULL != strstr (argv[0],
748 test = ASYMMETRIC_SEND_LIMITED;
750 else if (NULL != strstr (argv[0],
753 test = ASYMMETRIC_RECV_LIMITED;
755 GNUNET_assert (test != -1);
756 cleanup_directory (test);
757 GNUNET_log_setup ("test-core-quota-compliance",
761 cleanup_directory (test);
766 /* end of test_core_quota_compliance.c */