2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file core/test_core_quota_compliance.c
22 * @brief testcase for core_api.c focusing quota compliance on core level
25 * - make sure connect callback is invoked properly as well!
28 #include "gnunet_common.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "gnunet_statistics_service.h"
39 #define VERBOSE GNUNET_NO
40 #define DEBUG_TRANSMISSION GNUNET_NO
43 #define ASYMMETRIC_SEND_LIMITED 1
44 #define ASYMMETRIC_RECV_LIMITED 2
46 #define START_ARM GNUNET_YES
49 * Note that this value must not significantly exceed
50 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
51 * messages may be dropped even for a reliable transport.
53 #define TOTAL_MSGS (60000 * 10)
56 * How long until we give up on transmitting the message?
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
61 * What delay do we request from the core service for transmission?
62 * Any value smaller than the CORK delay will disable CORKing, which
63 * is what we want here.
65 #define FAST_TIMEOUT GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2)
68 #define MESSAGESIZE 1024
69 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
71 static unsigned long long total_bytes_sent;
72 static unsigned long long total_bytes_recv;
74 static struct GNUNET_TIME_Absolute start_time;
76 static GNUNET_SCHEDULER_TaskIdentifier err_task;
78 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
83 struct GNUNET_CONFIGURATION_Handle *cfg;
84 struct GNUNET_CORE_Handle *ch;
85 struct GNUNET_PeerIdentity id;
86 struct GNUNET_TRANSPORT_Handle *th;
87 struct GNUNET_MessageHeader *hello;
88 struct GNUNET_STATISTICS_Handle *stats;
91 struct GNUNET_OS_Process *arm_proc;
95 static struct PeerContext p1;
96 static struct PeerContext p2;
98 static unsigned long long current_quota_p1_in;
99 static unsigned long long current_quota_p1_out;
100 static unsigned long long current_quota_p2_in;
101 static unsigned long long current_quota_p2_out;
111 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
113 #define OKPP do { ok++; } while (0)
118 struct GNUNET_MessageHeader header;
123 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
125 GNUNET_CORE_disconnect (p1.ch);
127 GNUNET_CORE_disconnect (p2.ch);
129 GNUNET_TRANSPORT_disconnect (p1.th);
131 GNUNET_TRANSPORT_disconnect (p2.th);
137 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\n");
143 if (measure_task != GNUNET_SCHEDULER_NO_TASK)
144 GNUNET_SCHEDULER_cancel(measure_task);
146 GNUNET_CORE_disconnect (p1.ch);
148 GNUNET_CORE_disconnect (p2.ch);
150 GNUNET_TRANSPORT_disconnect (p1.th);
152 GNUNET_TRANSPORT_disconnect (p2.th);
159 * Callback function to process statistic values.
162 * @param subsystem name of subsystem that created the statistic
163 * @param name the name of the datum
164 * @param value the current value
165 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
166 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
169 print_stat (void *cls,
170 const char *subsystem,
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Peer1 %50s = %12llu\n",
179 (unsigned long long) value);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182 "Peer2 %50s = %12llu\n",
184 (unsigned long long) value);
189 measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 unsigned long long int delta;
192 unsigned long long int throughput_out;
193 unsigned long long int throughput_in;
194 unsigned long long int max_quota_in;
195 unsigned long long int max_quota_out;
196 unsigned long long int quota_delta;
198 measure_task = GNUNET_SCHEDULER_NO_TASK;
199 fprintf(stdout,"\n");
202 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
204 throughput_out = total_bytes_sent * 1000 / 1024 / delta;
205 throughput_in = total_bytes_recv * 1000 / 1024 / delta;
207 if (current_quota_p1_in < current_quota_p2_in)
208 max_quota_in = current_quota_p1_in;
210 max_quota_in = current_quota_p2_in;
211 if (current_quota_p1_out < current_quota_p2_out)
212 max_quota_out = current_quota_p1_out;
214 max_quota_out = current_quota_p2_out;
216 if (max_quota_out < max_quota_in)
217 quota_delta = max_quota_in / 10;
219 quota_delta = max_quota_out / 10;
221 if ((throughput_out > (max_quota_out+quota_delta)/1024) || (throughput_in > (max_quota_in+quota_delta)/1024))
226 GNUNET_STATISTICS_get (p1.stats,
228 "# discarded CORE_SEND requests",
229 GNUNET_TIME_UNIT_FOREVER_REL,
233 GNUNET_STATISTICS_get (p1.stats,
235 "# discarded CORE_SEND request bytes",
236 GNUNET_TIME_UNIT_FOREVER_REL,
239 GNUNET_STATISTICS_get (p1.stats,
241 "# discarded lower priority CORE_SEND requests",
242 GNUNET_TIME_UNIT_FOREVER_REL,
245 GNUNET_STATISTICS_get (p1.stats,
247 "# discarded lower priority CORE_SEND request bytes",
248 GNUNET_TIME_UNIT_FOREVER_REL,
251 GNUNET_STATISTICS_get (p2.stats,
253 "# discarded CORE_SEND requests",
254 GNUNET_TIME_UNIT_FOREVER_REL,
258 GNUNET_STATISTICS_get (p2.stats,
260 "# discarded CORE_SEND request bytes",
261 GNUNET_TIME_UNIT_FOREVER_REL,
264 GNUNET_STATISTICS_get (p2.stats,
266 "# discarded lower priority CORE_SEND requests",
267 GNUNET_TIME_UNIT_FOREVER_REL,
270 GNUNET_STATISTICS_get (p2.stats,
272 "# discarded lower priority CORE_SEND request bytes",
273 GNUNET_TIME_UNIT_FOREVER_REL,
277 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
280 kind = GNUNET_ERROR_TYPE_ERROR;
285 GNUNET_log (kind,"Core quota compliance test with symmetric quotas: %s\n", (ok==0)?"PASSED":"FAILED");
287 case ASYMMETRIC_SEND_LIMITED:
288 GNUNET_log (kind,"Core quota compliance test with limited sender quota: %s\n", (ok==0)?"PASSED":"FAILED");
290 case ASYMMETRIC_RECV_LIMITED:
291 GNUNET_log (kind,"Core quota compliance test with limited receiver quota: %s\n", (ok==0)?"PASSED":"FAILED");
294 GNUNET_log (kind,"Peer 1 send rate: %llu kB/s (%llu Bytes in %u sec.)\n",throughput_out,total_bytes_sent, delta/1000);
295 GNUNET_log (kind,"Peer 1 send quota: %llu kB/s\n",current_quota_p1_out / 1024);
296 GNUNET_log (kind,"Peer 2 receive rate: %llu kB/s (%llu Bytes in %u sec.)\n",throughput_in,total_bytes_recv, delta/1000);
297 GNUNET_log (kind,"Peer 2 receive quota: %llu kB/s\n",current_quota_p2_in / 1024);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu kB/s\n",max_quota_in /1024);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu kB/s\n",max_quota_out/1024);
302 GNUNET_SCHEDULER_cancel (err_task);
303 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
308 transmit_ready (void *cls, size_t size, void *buf)
311 struct TestMessage hdr;
314 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
318 GNUNET_break (NULL !=
319 GNUNET_CORE_notify_transmit_ready (p1.ch,
324 &transmit_ready, &p1));
327 GNUNET_assert (tr_n < TOTAL_MSGS);
329 GNUNET_assert (size >= MESSAGESIZE);
330 GNUNET_assert (buf != NULL);
334 #if DEBUG_TRANSMISSION
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Sending message %u of size %u at offset %u\n",
341 hdr.header.size = htons (MESSAGESIZE);
342 hdr.header.type = htons (MTYPE);
343 hdr.num = htonl (tr_n);
344 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
345 ret += sizeof (struct TestMessage);
346 memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
347 ret += MESSAGESIZE - sizeof (struct TestMessage);
349 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
350 break; /* sometimes pack buffer full, sometimes not */
352 while (size - ret >= MESSAGESIZE);
353 GNUNET_SCHEDULER_cancel (err_task);
354 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
355 &terminate_task_error,
358 total_bytes_sent += ret;
365 connect_notify (void *cls,
366 const struct GNUNET_PeerIdentity *peer,
367 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
369 struct PeerContext *pc = cls;
371 GNUNET_assert (pc->connect_status == 0);
372 pc->connect_status = 1;
375 #if DEBUG_TRANSMISSION
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Encrypted connection established to peer `%4s'\n",
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Asking core (1) for transmission to peer `%4s'\n",
381 GNUNET_i2s (&p2.id));
383 if (err_task != GNUNET_SCHEDULER_NO_TASK) GNUNET_SCHEDULER_cancel (err_task);
384 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
385 &terminate_task_error,
387 start_time = GNUNET_TIME_absolute_get ();
388 running = GNUNET_YES;
389 measure_task = GNUNET_SCHEDULER_add_delayed(MEASUREMENT_LENGTH, &measurement_stop, NULL);
391 GNUNET_break (NULL != GNUNET_CORE_notify_transmit_ready (p1.ch,
396 &transmit_ready, &p1));
402 disconnect_notify (void *cls,
403 const struct GNUNET_PeerIdentity *peer)
405 struct PeerContext *pc = cls;
406 pc->connect_status = 0;
407 #if DEBUG_TRANSMISSION
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
415 inbound_notify (void *cls,
416 const struct GNUNET_PeerIdentity *other,
417 const struct GNUNET_MessageHeader *message,
418 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
420 #if DEBUG_TRANSMISSION
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Core provides inbound data from `%4s' %llu.\n", GNUNET_i2s (other), ntohs(message->size));
424 total_bytes_recv += ntohs(message->size);
430 outbound_notify (void *cls,
431 const struct GNUNET_PeerIdentity *other,
432 const struct GNUNET_MessageHeader *message,
433 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
435 #if DEBUG_TRANSMISSION
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Core notifies about outbound data for `%4s'.\n",
445 transmit_ready (void *cls, size_t size, void *buf);
448 process_mtype (void *cls,
449 const struct GNUNET_PeerIdentity *peer,
450 const struct GNUNET_MessageHeader *message,
451 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
454 const struct TestMessage *hdr;
456 hdr = (const struct TestMessage*) message;
457 if (MTYPE != ntohs (message->type))
458 return GNUNET_SYSERR;
459 if (ntohs (message->size) != MESSAGESIZE)
461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
462 "Expected message %u of size %u, got %u bytes of message %u\n",
464 ntohs (message->size),
466 GNUNET_SCHEDULER_cancel (err_task);
467 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
468 return GNUNET_SYSERR;
470 if (ntohl (hdr->num) != n)
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "Expected message %u of size %u, got %u bytes of message %u\n",
475 ntohs (message->size),
477 GNUNET_SCHEDULER_cancel (err_task);
478 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
479 return GNUNET_SYSERR;
481 #if DEBUG_TRANSMISSION
482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
483 "Got message %u of size %u\n",
485 ntohs (message->size));
489 fprintf (stderr, ".");
492 if (running == GNUNET_YES)
493 GNUNET_break (NULL !=
494 GNUNET_CORE_notify_transmit_ready (p1.ch,
499 &transmit_ready, &p1));
504 static struct GNUNET_CORE_MessageHandler handlers[] = {
505 {&process_mtype, MTYPE, 0},
512 init_notify (void *cls,
513 struct GNUNET_CORE_Handle *server,
514 const struct GNUNET_PeerIdentity *my_identity,
515 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
517 struct PeerContext *p = cls;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Connection to CORE service of `%4s' established\n",
521 GNUNET_i2s (my_identity));
522 GNUNET_assert (server != NULL);
523 p->id = *my_identity;
527 GNUNET_assert (ok == 2);
530 GNUNET_CORE_connect (p2.cfg, 1,
538 &outbound_notify, GNUNET_YES, handlers);
542 GNUNET_assert (ok == 3);
544 GNUNET_assert (cls == &p2);
545 #if DEBUG_TRANSMISSION
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Asking core (1) to connect to peer `%4s'\n",
548 GNUNET_i2s (&p2.id));
550 GNUNET_CORE_peer_request_connect (p1.ch,
551 GNUNET_TIME_UNIT_SECONDS,
559 process_hello (void *cls,
560 const struct GNUNET_MessageHeader *message)
562 struct PeerContext *p = cls;
564 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
566 #if DEBUG_TRANSMISSION
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Received (my) `%s' from transport service\n",
571 GNUNET_assert (message != NULL);
572 p->hello = GNUNET_malloc (ntohs (message->size));
573 memcpy (p->hello, message, ntohs (message->size));
574 if ((p == &p1) && (p2.th != NULL))
575 GNUNET_TRANSPORT_offer_hello (p2.th, message);
576 if ((p == &p2) && (p1.th != NULL))
577 GNUNET_TRANSPORT_offer_hello (p1.th, message);
579 if ((p == &p1) && (p2.hello != NULL))
580 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
581 if ((p == &p2) && (p1.hello != NULL))
582 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
588 setup_peer (struct PeerContext *p, const char *cfgname)
590 p->cfg = GNUNET_CONFIGURATION_create ();
592 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
593 "gnunet-service-arm",
597 "-c", cfgname, NULL);
599 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
600 p->stats = GNUNET_STATISTICS_create ("core",p->cfg);
601 GNUNET_assert (p->stats!=NULL);
602 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
603 GNUNET_assert (p->th != NULL);
604 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
612 const struct GNUNET_CONFIGURATION_Handle *cfg)
614 GNUNET_assert (ok == 1);
616 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
617 &terminate_task_error,
619 if (test == SYMMETRIC)
621 setup_peer (&p1, "test_core_quota_peer1.conf");
622 setup_peer (&p2, "test_core_quota_peer2.conf");
624 else if (test == ASYMMETRIC_SEND_LIMITED)
626 setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
627 setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
629 else if (test == ASYMMETRIC_RECV_LIMITED)
631 setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
632 setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
635 GNUNET_assert (test != -1);
636 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
639 ¤t_quota_p1_in));
640 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
643 ¤t_quota_p2_in));
644 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
647 ¤t_quota_p1_out));
648 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
651 ¤t_quota_p2_out));
653 GNUNET_CORE_connect (p1.cfg, 1,
660 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
665 stop_arm (struct PeerContext *p)
668 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
669 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
670 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
671 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
674 GNUNET_OS_process_close (p->arm_proc);
677 GNUNET_CONFIGURATION_destroy (p->cfg);
685 char *const argv[] = { "test-core-quota-compliance",
687 "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,
698 argv, "test-core-quota-compliance", "nohelp", options, &run, &ok);
705 main (int argc, char *argv[])
710 if (strstr(argv[0], "_symmetric") != NULL)
714 else if (strstr(argv[0], "_asymmetric_send") != NULL)
716 test = ASYMMETRIC_SEND_LIMITED;
718 else if (strstr(argv[0], "_asymmetric_recv") != NULL)
720 test = ASYMMETRIC_RECV_LIMITED;
722 GNUNET_assert (test != -1);
723 if (test == SYMMETRIC)
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/");
728 else if (test == ASYMMETRIC_SEND_LIMITED)
730 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
731 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
733 else if (test == ASYMMETRIC_RECV_LIMITED)
735 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
736 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
739 GNUNET_log_setup ("test-core-quota-compliance",
747 if (test == SYMMETRIC)
749 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
750 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
752 else if (test == ASYMMETRIC_SEND_LIMITED)
754 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
755 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
757 else if (test == ASYMMETRIC_RECV_LIMITED)
759 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
760 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
767 /* end of test_core_api_reliability.c */