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_YES
41 #define START_ARM GNUNET_YES
42 #define DEBUG_CONNECTIONS GNUNET_NO
45 * Note that this value must not significantly exceed
46 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
47 * messages may be dropped even for a reliable transport.
49 #define TOTAL_MSGS (600 * 10)
51 #define MEASUREMENT_MSG_SIZE 10240
52 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
53 #define MEASUREMENT_MIN_QUOTA 1024
54 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
57 * How long until we give up on transmitting the message?
59 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 6000)
62 * What delay do we request from the core service for transmission?
63 * Any value smaller than the CORK delay will disable CORKing, which
64 * is what we want here.
66 #define FAST_TIMEOUT GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_MAX_CORK_DELAY, 2)
70 static int is_asymmetric_send_constant;
71 static int is_asymmetric_recv_constant;
72 static unsigned long long current_quota_p1_in;
73 static unsigned long long current_quota_p1_out;
74 static unsigned long long current_quota_p2_in;
75 static unsigned long long current_quota_p2_out;
77 static unsigned long long total_bytes;
78 static unsigned long long total_bytes_sent;
79 static unsigned long long total_bytes_recv;
81 static struct GNUNET_TIME_Absolute start_time;
83 static GNUNET_SCHEDULER_TaskIdentifier err_task;
85 static GNUNET_SCHEDULER_TaskIdentifier send_task;
87 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
91 struct GNUNET_CONFIGURATION_Handle *cfg;
92 struct GNUNET_STATISTICS_Handle *stats;
93 struct GNUNET_CORE_Handle *ch;
94 struct GNUNET_PeerIdentity id;
95 struct GNUNET_TRANSPORT_Handle *th;
96 struct GNUNET_MessageHeader *hello;
100 struct GNUNET_OS_Process *arm_proc;
104 static struct PeerContext p1;
106 static struct PeerContext p2;
109 static int measurement_running;
111 struct GNUNET_CORE_TransmitHandle * ch;
114 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
116 #define OKPP do { ok++; } while (0)
121 struct GNUNET_MessageHeader header;
126 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
128 unsigned long long delta;
130 GNUNET_CORE_disconnect (p1.ch);
132 GNUNET_CORE_disconnect (p2.ch);
134 GNUNET_TRANSPORT_disconnect (p1.th);
136 GNUNET_TRANSPORT_disconnect (p2.th);
138 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
144 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 if (send_task != GNUNET_SCHEDULER_NO_TASK)
148 GNUNET_SCHEDULER_cancel (send_task);
149 GNUNET_CORE_disconnect (p1.ch);
151 GNUNET_CORE_disconnect (p2.ch);
153 GNUNET_TRANSPORT_disconnect (p1.th);
155 GNUNET_TRANSPORT_disconnect (p2.th);
160 static void connect_notify (void *cls,
161 const struct GNUNET_PeerIdentity *peer,
162 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
164 struct PeerContext *pc = cls;
165 GNUNET_assert (pc->connect_status == 0);
166 pc->connect_status = 1;
167 #if DEBUG_CONNECTIONS
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Encrypted connection established to peer `%4s'\n",
176 disconnect_notify (void *cls,
177 const struct GNUNET_PeerIdentity *peer)
179 struct PeerContext *pc = cls;
180 pc->connect_status = 0;
181 #if DEBUG_CONNECTIONS
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
189 inbound_notify (void *cls,
190 const struct GNUNET_PeerIdentity *other,
191 const struct GNUNET_MessageHeader *message,
192 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
194 total_bytes_recv += ntohs (message->size);
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Core provides inbound data from `%4s' size %u.\n", GNUNET_i2s (other), ntohs (message->size));
197 #if DEBUG_CONNECTIONS
204 outbound_notify (void *cls,
205 const struct GNUNET_PeerIdentity *other,
206 const struct GNUNET_MessageHeader *message,
207 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
209 #if DEBUG_CONNECTIONS
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Core notifies about outbound data for `%4s'.\n",
218 next_fin (void *cls, int success)
226 const char *subsystem,
227 const char *name, uint64_t value, int is_persistent)
229 fprintf(stderr, "%s %s %llu\n", subsystem, name, (long long unsigned int) value);
234 measurement_end (void *cls,
235 const struct GNUNET_SCHEDULER_TaskContext *tc)
237 struct GNUNET_TIME_Relative duration;
239 measure_task = GNUNET_SCHEDULER_NO_TASK;
240 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
243 if (err_task != GNUNET_SCHEDULER_NO_TASK)
244 GNUNET_SCHEDULER_cancel (err_task);
245 if (send_task != GNUNET_SCHEDULER_NO_TASK)
246 GNUNET_SCHEDULER_cancel (send_task);
248 GNUNET_STATISTICS_get(p1.stats,"core","# discarded CORE_SEND requests",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p1);
249 GNUNET_STATISTICS_get(p1.stats,"core","# discarded CORE_SEND requests",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p2);
250 GNUNET_STATISTICS_get(p1.stats,"core","# discarded lower priority CORE_SEND requests",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p1);
251 GNUNET_STATISTICS_get(p1.stats,"core","# discarded lower priority CORE_SEND requests",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p2);
253 GNUNET_STATISTICS_get(p1.stats,"core","# discarded CORE_SEND request bytes",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p1);
254 GNUNET_STATISTICS_get(p1.stats,"core","# discarded CORE_SEND request bytes",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p2);
255 GNUNET_STATISTICS_get(p1.stats,"core","# discarded lower priority CORE_SEND request bytes",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p1);
256 GNUNET_STATISTICS_get(p1.stats,"core","# discarded lower priority CORE_SEND request bytes",GNUNET_TIME_UNIT_SECONDS, &next_fin, &check_2, &p2);
257 measurement_running = GNUNET_NO;
258 duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260 "\nQuota compliance: \n" \
261 "Receive rate: %10llu kB/s\n"
262 "Send rate : %10llu kB/s\n" \
263 "Quota : %10llu kB/s\n",
264 (total_bytes_recv/(duration.rel_value / 1000)/1024),
265 (total_bytes_sent/(duration.rel_value / 1000)/1024),
266 current_quota_p1_in/1024);
267 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
271 transmit_ready (void *cls, size_t size, void *buf);
275 const struct GNUNET_SCHEDULER_TaskContext *tc)
277 send_task = GNUNET_SCHEDULER_NO_TASK;
279 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
283 sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
284 &transmit_ready, &p1);
289 measure (unsigned long long quota_p1, unsigned long long quota_p2)
292 if ((is_asymmetric_send_constant == GNUNET_YES) || (is_asymmetric_recv_constant == GNUNET_YES))
293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
294 "Starting core level measurement for %u seconds receiving peer quota %llu kB/s, sending peer quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p1_in / 1024, current_quota_p2_out / 1024);
296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
297 "Starting core level measurement for %u seconds, symmetric quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p2_out / 1024);
300 #if DEBUG_CONNECTIONS
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Asking core (1) for transmission to peer `%4s'\n",
303 GNUNET_i2s (&p2.id));
305 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
306 &terminate_task_error,
308 measure_task = GNUNET_SCHEDULER_add_delayed (MEASUREMENT_INTERVALL,
311 start_time = GNUNET_TIME_absolute_get ();
312 measurement_running = GNUNET_YES;
314 total_bytes_sent = 0;
315 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
319 sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
320 &transmit_ready, &p1);
327 process_mtype (void *cls,
328 const struct GNUNET_PeerIdentity *peer,
329 const struct GNUNET_MessageHeader *message,
330 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
334 const struct TestMessage *hdr;
336 hdr = (const struct TestMessage*) message;
337 s = sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE;
338 if (MTYPE != ntohs (message->type))
339 return GNUNET_SYSERR;
341 #if DEBUG_CONNECTIONS
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Got message %u of size %u\n",
345 ntohs (message->size));
348 if (0 == (n % (TOTAL_MSGS/100)))
349 fprintf (stderr, ".");
355 static struct GNUNET_CORE_MessageHandler handlers[] = {
356 {&process_mtype, MTYPE, 0},
362 transmit_ready (void *cls, size_t size, void *buf)
365 struct TestMessage hdr;
369 if (measurement_running != GNUNET_YES)
372 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
377 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
381 sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
382 &transmit_ready, &p1);
383 GNUNET_break (NULL != ch);
389 s = sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE;
391 GNUNET_assert (size >= s);
392 GNUNET_assert (buf != NULL);
396 #if DEBUG_CONNECTIONS
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Sending message %u of size %u at offset %u\n",
403 hdr.header.size = htons (s);
404 hdr.header.type = htons (MTYPE);
405 hdr.num = htonl (tr_n);
406 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
407 ret += sizeof (struct TestMessage);
408 memset (&cbuf[ret], tr_n, s - sizeof (struct TestMessage));
409 ret += s - sizeof (struct TestMessage);
411 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
412 break; /* sometimes pack buffer full, sometimes not */
414 while (size - ret >= s);
415 GNUNET_SCHEDULER_cancel (err_task);
416 err_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
417 &terminate_task_error,
421 total_bytes_sent += ret;
422 if (send_task != GNUNET_SCHEDULER_NO_TASK)
423 GNUNET_SCHEDULER_cancel(send_task);
424 send_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 20), &send_tsk, NULL);
432 init_notify (void *cls,
433 struct GNUNET_CORE_Handle *server,
434 const struct GNUNET_PeerIdentity *my_identity,
435 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
437 struct PeerContext *p = cls;
438 #if DEBUG_CONNECTIONS
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Connection to CORE service of `%4s' established\n",
441 GNUNET_i2s (my_identity));
443 GNUNET_assert (server != NULL);
444 p->id = *my_identity;
448 GNUNET_assert (ok == 2);
451 GNUNET_CORE_connect (p2.cfg, 1,
459 &outbound_notify, GNUNET_YES, handlers);
463 GNUNET_assert (ok == 3);
465 GNUNET_assert (cls == &p2);
467 measure (MEASUREMENT_MIN_QUOTA, MEASUREMENT_MIN_QUOTA);
473 process_hello (void *cls,
474 const struct GNUNET_MessageHeader *message)
476 struct PeerContext *p = cls;
478 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
479 #if DEBUG_CONNECTIONS
480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
481 "Received (my) `%s' from transport service\n",
484 GNUNET_assert (message != NULL);
485 p->hello = GNUNET_malloc (ntohs (message->size));
486 memcpy (p->hello, message, ntohs (message->size));
487 if ((p == &p1) && (p2.th != NULL))
488 GNUNET_TRANSPORT_offer_hello (p2.th, message);
489 if ((p == &p2) && (p1.th != NULL))
490 GNUNET_TRANSPORT_offer_hello (p1.th, message);
492 if ((p == &p1) && (p2.hello != NULL))
493 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
494 if ((p == &p2) && (p1.hello != NULL))
495 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
501 setup_peer (struct PeerContext *p, const char *cfgname)
503 p->cfg = GNUNET_CONFIGURATION_create ();
505 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
506 "gnunet-service-arm",
510 "-c", cfgname, NULL);
512 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
513 p->stats = GNUNET_STATISTICS_create ("core", p->cfg);
514 GNUNET_assert (p->stats != NULL);
515 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
516 GNUNET_assert (p->th != NULL);
517 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
525 const struct GNUNET_CONFIGURATION_Handle *cfg)
527 GNUNET_assert (ok == 1);
529 setup_peer (&p1, "test_core_quota_peer1.conf");
530 setup_peer (&p2, "test_core_quota_peer2.conf");
531 GNUNET_CORE_connect (p1.cfg, 1,
538 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
540 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
543 ¤t_quota_p1_in));
544 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
547 ¤t_quota_p2_in));
548 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
551 ¤t_quota_p1_out));
552 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
555 ¤t_quota_p2_out));
560 stop_arm (struct PeerContext *p)
563 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
564 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
565 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
566 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"ARM process stopped\n");
569 GNUNET_OS_process_close (p->arm_proc);
572 GNUNET_STATISTICS_destroy (p->stats, 0);
573 GNUNET_CONFIGURATION_destroy (p->cfg);
579 char *const argv[] = { "test-core-quota-compliance",
581 "test_core_api_data.conf",
587 struct GNUNET_GETOPT_CommandLineOption options[] = {
588 GNUNET_GETOPT_OPTION_END
591 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
592 argv, "test_core_quota_compliance", "nohelp", options, &run, &ok);
599 main (int argc, char *argv[])
603 GNUNET_log_setup ("test-core-quota-compliance",
611 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-peer-2");
612 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-peer-2");
617 /* end of test_core_quota_compliance.c */