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 #include "gnunet_common.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_transport_service.h"
34 #include "gnunet_statistics_service.h"
36 #define VERBOSE GNUNET_NO
37 #define DEBUG_TRANSMISSION GNUNET_NO
40 #define ASYMMETRIC_SEND_LIMITED 1
41 #define ASYMMETRIC_RECV_LIMITED 2
43 #define START_ARM GNUNET_YES
46 * Note that this value must not significantly exceed
47 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
48 * messages may be dropped even for a reliable transport.
50 #define TOTAL_MSGS (60000 * 10)
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
58 * What delay do we request from the core service for transmission?
59 * Any value smaller than the CORK delay will disable CORKing, which
60 * is what we want here.
62 #define FAST_TIMEOUT GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2)
65 #define MESSAGESIZE 1024
66 #define MEASUREMENT_LENGTH GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
68 static unsigned long long total_bytes_sent;
69 static unsigned long long total_bytes_recv;
71 static struct GNUNET_TIME_Absolute start_time;
73 static GNUNET_SCHEDULER_TaskIdentifier err_task;
75 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
80 struct GNUNET_CONFIGURATION_Handle *cfg;
81 struct GNUNET_CORE_Handle *ch;
82 struct GNUNET_PeerIdentity id;
83 struct GNUNET_TRANSPORT_Handle *th;
84 struct GNUNET_MessageHeader *hello;
85 struct GNUNET_STATISTICS_Handle *stats;
88 struct GNUNET_OS_Process *arm_proc;
92 static struct PeerContext p1;
93 static struct PeerContext p2;
95 static unsigned long long current_quota_p1_in;
96 static unsigned long long current_quota_p1_out;
97 static unsigned long long current_quota_p2_in;
98 static unsigned long long current_quota_p2_out;
108 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
110 #define OKPP do { ok++; } while (0)
115 struct GNUNET_MessageHeader header;
120 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
122 struct GNUNET_CORE_Handle *ch;
126 GNUNET_CORE_disconnect (ch);
129 GNUNET_CORE_disconnect (ch);
130 GNUNET_TRANSPORT_disconnect (p1.th);
132 GNUNET_TRANSPORT_disconnect (p2.th);
138 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
140 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase failed!\n");
144 if (measure_task != GNUNET_SCHEDULER_NO_TASK)
145 GNUNET_SCHEDULER_cancel(measure_task);
147 GNUNET_CORE_disconnect (p1.ch);
149 GNUNET_CORE_disconnect (p2.ch);
151 GNUNET_TRANSPORT_disconnect (p1.th);
153 GNUNET_TRANSPORT_disconnect (p2.th);
160 * Callback function to process statistic values.
163 * @param subsystem name of subsystem that created the statistic
164 * @param name the name of the datum
165 * @param value the current value
166 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
167 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
170 print_stat (void *cls,
171 const char *subsystem,
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Peer1 %50s = %12llu\n",
180 (unsigned long long) value);
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Peer2 %50s = %12llu\n",
185 (unsigned long long) value);
190 measurement_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
192 unsigned long long int delta;
193 unsigned long long int throughput_out;
194 unsigned long long int throughput_in;
195 unsigned long long int max_quota_in;
196 unsigned long long int max_quota_out;
197 unsigned long long int quota_delta;
199 measure_task = GNUNET_SCHEDULER_NO_TASK;
200 fprintf(stdout,"\n");
203 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
205 throughput_out = total_bytes_sent * 1000 / 1024 / delta;
206 throughput_in = total_bytes_recv * 1000 / 1024 / delta;
208 if (current_quota_p1_in < current_quota_p2_in)
209 max_quota_in = current_quota_p1_in;
211 max_quota_in = current_quota_p2_in;
212 if (current_quota_p1_out < current_quota_p2_out)
213 max_quota_out = current_quota_p1_out;
215 max_quota_out = current_quota_p2_out;
217 if (max_quota_out < max_quota_in)
218 quota_delta = max_quota_in / 10;
220 quota_delta = max_quota_out / 10;
222 if ((throughput_out > (max_quota_out+quota_delta)/1024) || (throughput_in > (max_quota_in+quota_delta)/1024))
227 GNUNET_STATISTICS_get (p1.stats,
229 "# discarded CORE_SEND requests",
230 GNUNET_TIME_UNIT_FOREVER_REL,
234 GNUNET_STATISTICS_get (p1.stats,
236 "# discarded CORE_SEND request bytes",
237 GNUNET_TIME_UNIT_FOREVER_REL,
240 GNUNET_STATISTICS_get (p1.stats,
242 "# discarded lower priority CORE_SEND requests",
243 GNUNET_TIME_UNIT_FOREVER_REL,
246 GNUNET_STATISTICS_get (p1.stats,
248 "# discarded lower priority CORE_SEND request bytes",
249 GNUNET_TIME_UNIT_FOREVER_REL,
252 GNUNET_STATISTICS_get (p2.stats,
254 "# discarded CORE_SEND requests",
255 GNUNET_TIME_UNIT_FOREVER_REL,
259 GNUNET_STATISTICS_get (p2.stats,
261 "# discarded CORE_SEND request bytes",
262 GNUNET_TIME_UNIT_FOREVER_REL,
265 GNUNET_STATISTICS_get (p2.stats,
267 "# discarded lower priority CORE_SEND requests",
268 GNUNET_TIME_UNIT_FOREVER_REL,
271 GNUNET_STATISTICS_get (p2.stats,
273 "# discarded lower priority CORE_SEND request bytes",
274 GNUNET_TIME_UNIT_FOREVER_REL,
278 enum GNUNET_ErrorType kind = GNUNET_ERROR_TYPE_DEBUG;
281 kind = GNUNET_ERROR_TYPE_ERROR;
286 GNUNET_log (kind,"Core quota compliance test with symmetric quotas: %s\n", (ok==0)?"PASSED":"FAILED");
288 case ASYMMETRIC_SEND_LIMITED:
289 GNUNET_log (kind,"Core quota compliance test with limited sender quota: %s\n", (ok==0)?"PASSED":"FAILED");
291 case ASYMMETRIC_RECV_LIMITED:
292 GNUNET_log (kind,"Core quota compliance test with limited receiver quota: %s\n", (ok==0)?"PASSED":"FAILED");
295 GNUNET_log (kind,"Peer 1 send rate: %llu kB/s (%llu Bytes in %u sec.)\n",throughput_out,total_bytes_sent, delta/1000);
296 GNUNET_log (kind,"Peer 1 send quota: %llu kB/s\n",current_quota_p1_out / 1024);
297 GNUNET_log (kind,"Peer 2 receive rate: %llu kB/s (%llu Bytes in %u sec.)\n",throughput_in,total_bytes_recv, delta/1000);
298 GNUNET_log (kind,"Peer 2 receive quota: %llu kB/s\n",current_quota_p2_in / 1024);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. inbound quota allowed: %llu kB/s\n",max_quota_in /1024);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Max. outbound quota allowed: %llu kB/s\n",max_quota_out/1024);
303 GNUNET_SCHEDULER_cancel (err_task);
304 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
309 transmit_ready (void *cls, size_t size, void *buf)
312 struct TestMessage hdr;
315 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
318 if ( (p1.ch != NULL) &&
319 (p1.connect_status == 1) )
320 GNUNET_break (NULL !=
321 GNUNET_CORE_notify_transmit_ready (p1.ch,
327 &transmit_ready, &p1));
330 GNUNET_assert (tr_n < TOTAL_MSGS);
332 GNUNET_assert (size >= MESSAGESIZE);
333 GNUNET_assert (buf != NULL);
337 #if DEBUG_TRANSMISSION
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Sending message %u of size %u at offset %u\n",
344 hdr.header.size = htons (MESSAGESIZE);
345 hdr.header.type = htons (MTYPE);
346 hdr.num = htonl (tr_n);
347 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
348 ret += sizeof (struct TestMessage);
349 memset (&cbuf[ret], tr_n, MESSAGESIZE - sizeof (struct TestMessage));
350 ret += MESSAGESIZE - sizeof (struct TestMessage);
352 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
353 break; /* sometimes pack buffer full, sometimes not */
355 while (size - ret >= MESSAGESIZE);
356 GNUNET_SCHEDULER_cancel (err_task);
357 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
358 &terminate_task_error,
361 total_bytes_sent += ret;
368 connect_notify (void *cls,
369 const struct GNUNET_PeerIdentity *peer,
370 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
372 struct PeerContext *pc = cls;
374 if (0 == memcmp (&pc->id,
376 sizeof (struct GNUNET_PeerIdentity)))
378 GNUNET_assert (pc->connect_status == 0);
379 pc->connect_status = 1;
382 #if DEBUG_TRANSMISSION
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Encrypted connection established to peer `%4s'\n",
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Asking core (1) for transmission to peer `%4s'\n",
388 GNUNET_i2s (&p2.id));
390 if (err_task != GNUNET_SCHEDULER_NO_TASK) GNUNET_SCHEDULER_cancel (err_task);
391 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
392 &terminate_task_error,
394 start_time = GNUNET_TIME_absolute_get ();
395 running = GNUNET_YES;
396 measure_task = GNUNET_SCHEDULER_add_delayed(MEASUREMENT_LENGTH, &measurement_stop, NULL);
398 GNUNET_break (NULL != GNUNET_CORE_notify_transmit_ready (p1.ch,
404 &transmit_ready, &p1));
410 disconnect_notify (void *cls,
411 const struct GNUNET_PeerIdentity *peer)
413 struct PeerContext *pc = cls;
415 if (0 == memcmp (&pc->id,
417 sizeof (struct GNUNET_PeerIdentity)))
419 pc->connect_status = 0;
420 #if DEBUG_TRANSMISSION
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
428 inbound_notify (void *cls,
429 const struct GNUNET_PeerIdentity *other,
430 const struct GNUNET_MessageHeader *message,
431 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
433 #if DEBUG_TRANSMISSION
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Core provides inbound data from `%4s' %llu.\n", GNUNET_i2s (other), ntohs(message->size));
437 total_bytes_recv += ntohs(message->size);
443 outbound_notify (void *cls,
444 const struct GNUNET_PeerIdentity *other,
445 const struct GNUNET_MessageHeader *message,
446 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
448 #if DEBUG_TRANSMISSION
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 "Core notifies about outbound data for `%4s'.\n",
458 transmit_ready (void *cls, size_t size, void *buf);
461 process_mtype (void *cls,
462 const struct GNUNET_PeerIdentity *peer,
463 const struct GNUNET_MessageHeader *message,
464 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
467 const struct TestMessage *hdr;
469 hdr = (const struct TestMessage*) message;
470 if (MTYPE != ntohs (message->type))
471 return GNUNET_SYSERR;
472 if (ntohs (message->size) != MESSAGESIZE)
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 "Expected message %u of size %u, got %u bytes of message %u\n",
477 ntohs (message->size),
479 GNUNET_SCHEDULER_cancel (err_task);
480 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
481 return GNUNET_SYSERR;
483 if (ntohl (hdr->num) != n)
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 "Expected message %u of size %u, got %u bytes of message %u\n",
488 ntohs (message->size),
490 GNUNET_SCHEDULER_cancel (err_task);
491 err_task = GNUNET_SCHEDULER_add_now (&terminate_task_error, NULL);
492 return GNUNET_SYSERR;
494 #if DEBUG_TRANSMISSION
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Got message %u of size %u\n",
498 ntohs (message->size));
502 fprintf (stderr, ".");
505 if (running == GNUNET_YES)
506 GNUNET_break (NULL !=
507 GNUNET_CORE_notify_transmit_ready (p1.ch,
513 &transmit_ready, &p1));
518 static struct GNUNET_CORE_MessageHandler handlers[] = {
519 {&process_mtype, MTYPE, 0},
526 init_notify (void *cls,
527 struct GNUNET_CORE_Handle *server,
528 const struct GNUNET_PeerIdentity *my_identity,
529 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
531 struct PeerContext *p = cls;
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "Connection to CORE service of `%4s' established\n",
535 GNUNET_i2s (my_identity));
536 GNUNET_assert (server != NULL);
537 p->id = *my_identity;
541 GNUNET_assert (ok == 2);
544 GNUNET_CORE_connect (p2.cfg, 1,
552 &outbound_notify, GNUNET_YES, handlers);
556 GNUNET_assert (ok == 3);
558 GNUNET_assert (cls == &p2);
559 #if DEBUG_TRANSMISSION
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Asking core (1) to connect to peer `%4s'\n",
562 GNUNET_i2s (&p2.id));
564 GNUNET_CORE_peer_request_connect (p1.ch,
565 GNUNET_TIME_UNIT_SECONDS,
573 process_hello (void *cls,
574 const struct GNUNET_MessageHeader *message)
576 struct PeerContext *p = cls;
578 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
580 #if DEBUG_TRANSMISSION
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Received (my) `%s' from transport service\n",
585 GNUNET_assert (message != NULL);
586 p->hello = GNUNET_malloc (ntohs (message->size));
587 memcpy (p->hello, message, ntohs (message->size));
588 if ((p == &p1) && (p2.th != NULL))
589 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
590 if ((p == &p2) && (p1.th != NULL))
591 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
593 if ((p == &p1) && (p2.hello != NULL))
594 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
595 if ((p == &p2) && (p1.hello != NULL))
596 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
602 setup_peer (struct PeerContext *p, const char *cfgname)
604 p->cfg = GNUNET_CONFIGURATION_create ();
606 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
607 "gnunet-service-arm",
611 "-c", cfgname, NULL);
613 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
614 p->stats = GNUNET_STATISTICS_create ("core",p->cfg);
615 GNUNET_assert (p->stats!=NULL);
616 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
617 GNUNET_assert (p->th != NULL);
618 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
626 const struct GNUNET_CONFIGURATION_Handle *cfg)
628 GNUNET_assert (ok == 1);
630 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
631 &terminate_task_error,
633 if (test == SYMMETRIC)
635 setup_peer (&p1, "test_core_quota_peer1.conf");
636 setup_peer (&p2, "test_core_quota_peer2.conf");
638 else if (test == ASYMMETRIC_SEND_LIMITED)
640 setup_peer (&p1, "test_core_quota_asymmetric_send_limit_peer1.conf");
641 setup_peer (&p2, "test_core_quota_asymmetric_send_limit_peer2.conf");
643 else if (test == ASYMMETRIC_RECV_LIMITED)
645 setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
646 setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
649 GNUNET_assert (test != -1);
650 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
653 ¤t_quota_p1_in));
654 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
657 ¤t_quota_p2_in));
658 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
661 ¤t_quota_p1_out));
662 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
665 ¤t_quota_p2_out));
667 GNUNET_CORE_connect (p1.cfg, 1,
674 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
679 stop_arm (struct PeerContext *p)
682 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
683 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
684 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
685 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
688 GNUNET_OS_process_close (p->arm_proc);
691 GNUNET_CONFIGURATION_destroy (p->cfg);
699 char *const argv[] = { "test-core-quota-compliance",
701 "test_core_api_data.conf",
707 struct GNUNET_GETOPT_CommandLineOption options[] = {
708 GNUNET_GETOPT_OPTION_END
711 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
712 argv, "test-core-quota-compliance", "nohelp", options, &run, &ok);
719 main (int argc, char *argv[])
724 if (strstr(argv[0], "_symmetric") != NULL)
728 else if (strstr(argv[0], "_asymmetric_send") != NULL)
730 test = ASYMMETRIC_SEND_LIMITED;
732 else if (strstr(argv[0], "_asymmetric_recv") != NULL)
734 test = ASYMMETRIC_RECV_LIMITED;
736 GNUNET_assert (test != -1);
737 if (test == SYMMETRIC)
739 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
740 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
742 else if (test == ASYMMETRIC_SEND_LIMITED)
744 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
745 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
747 else if (test == ASYMMETRIC_RECV_LIMITED)
749 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
750 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
753 GNUNET_log_setup ("test-core-quota-compliance",
761 if (test == SYMMETRIC)
763 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-1/");
764 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-sym-peer-2/");
766 else if (test == ASYMMETRIC_SEND_LIMITED)
768 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-1/");
769 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-send-lim-peer-2/");
771 else if (test == ASYMMETRIC_RECV_LIMITED)
773 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-1/");
774 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-asym-recv-lim-peer-2/");
781 /* end of test_core_api_reliability.c */