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"
38 #define VERBOSE GNUNET_YES
40 #define START_ARM GNUNET_YES
41 #define DEBUG_CONNECTIONS GNUNET_NO
44 * Note that this value must not significantly exceed
45 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
46 * messages may be dropped even for a reliable transport.
48 #define TOTAL_MSGS (600 * 10)
50 #define MEASUREMENT_MSG_SIZE 1024
51 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
52 #define MEASUREMENT_MIN_QUOTA 1024
53 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 8)
56 * How long until we give up on transmitting the message?
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 6000)
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)
69 static int is_asymmetric_send_constant;
70 static int is_asymmetric_recv_constant;
71 static unsigned long long current_quota_p1_in;
72 static unsigned long long current_quota_p1_out;
73 static unsigned long long current_quota_p2_in;
74 static unsigned long long current_quota_p2_out;
76 static unsigned long long total_bytes;
77 static unsigned long long total_bytes_sent;
79 static struct GNUNET_TIME_Absolute start_time;
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;
94 static struct PeerContext p1;
96 static struct PeerContext p2;
98 static struct GNUNET_SCHEDULER_Handle *sched;
101 static int measurement_running;
103 struct GNUNET_CORE_TransmitHandle * ch;
106 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
108 #define OKPP do { ok++; } while (0)
113 struct GNUNET_MessageHeader header;
118 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 unsigned long long delta;
122 GNUNET_CORE_disconnect (p1.ch);
124 GNUNET_CORE_disconnect (p2.ch);
126 GNUNET_TRANSPORT_disconnect (p1.th);
128 GNUNET_TRANSPORT_disconnect (p2.th);
130 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
136 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 GNUNET_CORE_disconnect (p1.ch);
141 GNUNET_CORE_disconnect (p2.ch);
143 GNUNET_TRANSPORT_disconnect (p1.th);
145 GNUNET_TRANSPORT_disconnect (p2.th);
151 connect_notify (void *cls,
152 const struct GNUNET_PeerIdentity *peer,
153 struct GNUNET_TIME_Relative latency,
156 struct PeerContext *pc = cls;
157 GNUNET_assert (pc->connect_status == 0);
158 pc->connect_status = 1;
159 #if DEBUG_CONNECTIONS
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Encrypted connection established to peer `%4s'\n",
168 disconnect_notify (void *cls,
169 const struct GNUNET_PeerIdentity *peer)
171 struct PeerContext *pc = cls;
172 pc->connect_status = 0;
173 #if DEBUG_CONNECTIONS
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
181 inbound_notify (void *cls,
182 const struct GNUNET_PeerIdentity *other,
183 const struct GNUNET_MessageHeader *message,
184 struct GNUNET_TIME_Relative latency,
187 #if DEBUG_CONNECTIONS
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
196 outbound_notify (void *cls,
197 const struct GNUNET_PeerIdentity *other,
198 const struct GNUNET_MessageHeader *message,
199 struct GNUNET_TIME_Relative latency,
202 #if DEBUG_CONNECTIONS
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Core notifies about outbound data for `%4s'.\n",
211 static GNUNET_SCHEDULER_TaskIdentifier err_task;
213 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
217 measurement_end (void *cls,
218 const struct GNUNET_SCHEDULER_TaskContext *tc)
220 struct GNUNET_TIME_Relative duration;
222 measure_task = GNUNET_SCHEDULER_NO_TASK;
223 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
225 measurement_running = GNUNET_NO;
227 duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
229 "\nQuota compliance: \n"\
230 "Throughput : %10llu kB/s\n"\
231 "Quota : %10llu kB/s\n", (total_bytes_sent/(duration.rel_value / 1000)/1024),current_quota_p1_in/1024);
233 if (err_task != GNUNET_SCHEDULER_NO_TASK)
234 GNUNET_SCHEDULER_cancel (sched, err_task);
235 GNUNET_SCHEDULER_add_now (sched, &terminate_task, NULL);
239 transmit_ready (void *cls, size_t size, void *buf);
241 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
244 if ((is_asymmetric_send_constant == GNUNET_YES) || (is_asymmetric_recv_constant == GNUNET_YES))
245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
246 "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);
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 "Starting core level measurement for %u seconds, symmetric quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p2_out / 1024);
252 #if DEBUG_CONNECTIONS
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Asking core (1) for transmission to peer `%4s'\n",
255 GNUNET_i2s (&p2.id));
257 err_task = GNUNET_SCHEDULER_add_delayed (sched,
259 &terminate_task_error,
261 measure_task = GNUNET_SCHEDULER_add_delayed (sched,
262 MEASUREMENT_INTERVALL,
265 start_time = GNUNET_TIME_absolute_get ();
266 measurement_running = GNUNET_YES;
268 total_bytes_sent = 0;
269 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
273 sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
274 &transmit_ready, &p1);
281 process_mtype (void *cls,
282 const struct GNUNET_PeerIdentity *peer,
283 const struct GNUNET_MessageHeader *message,
284 struct GNUNET_TIME_Relative latency,
289 const struct TestMessage *hdr;
291 hdr = (const struct TestMessage*) message;
292 s = sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE;
293 if (MTYPE != ntohs (message->type))
294 return GNUNET_SYSERR;
296 #if DEBUG_CONNECTIONS
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Got message %u of size %u\n",
300 ntohs (message->size));
303 if (0 == (n % (TOTAL_MSGS/100)))
304 fprintf (stderr, ".");
310 static struct GNUNET_CORE_MessageHandler handlers[] = {
311 {&process_mtype, MTYPE, 0},
317 transmit_ready (void *cls, size_t size, void *buf)
320 struct TestMessage hdr;
324 if (measurement_running != GNUNET_YES)
327 GNUNET_assert (size <= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE);
332 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
336 sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
337 &transmit_ready, &p1);
338 GNUNET_break (NULL != ch);
344 s = sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE;
346 GNUNET_assert (size >= s);
347 GNUNET_assert (buf != NULL);
351 #if DEBUG_CONNECTIONS
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Sending message %u of size %u at offset %u\n",
358 hdr.header.size = htons (s);
359 hdr.header.type = htons (MTYPE);
360 hdr.num = htonl (tr_n);
361 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
362 ret += sizeof (struct TestMessage);
363 memset (&cbuf[ret], tr_n, s - sizeof (struct TestMessage));
364 ret += s - sizeof (struct TestMessage);
366 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
367 break; /* sometimes pack buffer full, sometimes not */
369 while (size - ret >= s);
370 GNUNET_SCHEDULER_cancel (sched, err_task);
371 err_task = GNUNET_SCHEDULER_add_delayed (sched,
373 &terminate_task_error,
377 total_bytes_sent += ret;
379 ch = GNUNET_CORE_notify_transmit_ready (p1.ch,
383 sizeof (struct TestMessage) + MEASUREMENT_MSG_SIZE,
384 &transmit_ready, &p1);
391 init_notify (void *cls,
392 struct GNUNET_CORE_Handle *server,
393 const struct GNUNET_PeerIdentity *my_identity,
394 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
396 struct PeerContext *p = cls;
397 #if DEBUG_CONNECTIONS
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Connection to CORE service of `%4s' established\n",
400 GNUNET_i2s (my_identity));
402 GNUNET_assert (server != NULL);
403 p->id = *my_identity;
407 GNUNET_assert (ok == 2);
410 GNUNET_CORE_connect (sched,
420 &outbound_notify, GNUNET_YES, handlers);
424 GNUNET_assert (ok == 3);
426 GNUNET_assert (cls == &p2);
428 measure (MEASUREMENT_MIN_QUOTA, MEASUREMENT_MIN_QUOTA);
434 process_hello (void *cls,
435 const struct GNUNET_MessageHeader *message)
437 struct PeerContext *p = cls;
439 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
440 #if DEBUG_CONNECTIONS
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Received (my) `%s' from transport service\n",
445 GNUNET_assert (message != NULL);
446 p->hello = GNUNET_malloc (ntohs (message->size));
447 memcpy (p->hello, message, ntohs (message->size));
448 if ((p == &p1) && (p2.th != NULL))
449 GNUNET_TRANSPORT_offer_hello (p2.th, message);
450 if ((p == &p2) && (p1.th != NULL))
451 GNUNET_TRANSPORT_offer_hello (p1.th, message);
453 if ((p == &p1) && (p2.hello != NULL))
454 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
455 if ((p == &p2) && (p1.hello != NULL))
456 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
462 setup_peer (struct PeerContext *p, const char *cfgname)
464 p->cfg = GNUNET_CONFIGURATION_create ();
466 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
467 "gnunet-service-arm",
471 "-c", cfgname, NULL);
473 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
474 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL, p, NULL, NULL, NULL);
475 GNUNET_assert (p->th != NULL);
476 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
482 struct GNUNET_SCHEDULER_Handle *s,
485 const struct GNUNET_CONFIGURATION_Handle *cfg)
487 GNUNET_assert (ok == 1);
490 setup_peer (&p1, "test_core_quota_peer1.conf");
491 setup_peer (&p2, "test_core_quota_peer2.conf");
492 GNUNET_CORE_connect (sched,
501 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
503 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
506 ¤t_quota_p1_in));
507 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
510 ¤t_quota_p2_in));
511 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p1.cfg,
514 ¤t_quota_p1_out));
515 GNUNET_assert (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_number (p2.cfg,
518 ¤t_quota_p2_out));
523 stop_arm (struct PeerContext *p)
526 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
527 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
528 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
529 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
531 "ARM process %u stopped\n", p->arm_pid);
533 GNUNET_CONFIGURATION_destroy (p->cfg);
539 char *const argv[] = { "test-core-quota-compliance",
541 "test_core_api_data.conf",
547 struct GNUNET_GETOPT_CommandLineOption options[] = {
548 GNUNET_GETOPT_OPTION_END
551 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
552 argv, "test_core_quota_compliance", "nohelp", options, &run, &ok);
559 main (int argc, char *argv[])
563 GNUNET_log_setup ("test-core-api",
571 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-peer-2");
572 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-quota-peer-2");
577 /* end of test_core_quota_compliance.c */