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 transport/test_quota_compliance.c
22 * @brief base test case for transport implementations
24 * This test case tests quota compliance both on core and transport level
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.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_server_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
37 #define VERBOSE GNUNET_YES
39 #define VERBOSE_ARM GNUNET_NO
41 #define START_ARM GNUNET_YES
42 #define DEBUG_MEASUREMENT GNUNET_NO
43 #define DEBUG_CONNECTIONS GNUNET_NO
45 #define MEASUREMENT_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
46 #define MEASUREMENT_MSG_SIZE 10000
47 #define MEASUREMENT_MSG_SIZE_BIG 32768
48 #define MEASUREMENT_MAX_QUOTA 1024 * 1024 * 1024
49 #define MEASUREMENT_MIN_QUOTA 1024 * 10
50 #define SEND_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 35)
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 200)
62 struct GNUNET_CONFIGURATION_Handle *cfg;
63 struct GNUNET_TRANSPORT_Handle *th;
64 struct GNUNET_PeerIdentity id;
71 * Handle for a transmission-ready request.
73 struct GNUNET_TRANSPORT_TransmitHandle
77 * Neighbour for this handle, NULL for control-traffic.
79 struct NeighbourList *neighbour;
82 * Function to call when notify_size bytes are available
85 GNUNET_CONNECTION_TransmitReadyNotify notify;
93 * transmit_ready task Id. The task is used to introduce the
94 * artificial delay that may be required to maintain the bandwidth
95 * limits. Later, this will be the ID of the "transmit_timeout"
96 * task which is used to signal a timeout if the transmission could
97 * not be done in a timely fashion.
99 GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
102 * Timeout for this request.
104 struct GNUNET_TIME_Absolute timeout;
107 * How many bytes is our notify callback waiting for?
112 * How important is this message?
114 unsigned int priority;
118 static struct PeerContext p1;
120 static struct PeerContext p2;
122 static struct GNUNET_SCHEDULER_Handle *sched;
126 static int connected;
127 static int measurement_running;
128 static int send_running;
129 static int recv_running;
131 static unsigned long long total_bytes;
132 static unsigned long long current_quota_p1;
133 static unsigned long long current_quota_p2;
136 static int is_tcp_nat;
141 static struct GNUNET_TIME_Absolute start_time;
143 static GNUNET_SCHEDULER_TaskIdentifier die_task;
144 static GNUNET_SCHEDULER_TaskIdentifier measurement_task;
145 static GNUNET_SCHEDULER_TaskIdentifier measurement_counter_task;
147 struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
150 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
152 #define OKPP do { ok++; } while (0)
166 GNUNET_SCHEDULER_cancel (sched, die_task);
167 die_task = GNUNET_SCHEDULER_NO_TASK;
169 if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
171 GNUNET_SCHEDULER_cancel (sched, measurement_task);
172 measurement_task = GNUNET_SCHEDULER_NO_TASK;
174 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
176 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
177 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
179 GNUNET_SCHEDULER_shutdown (sched);
180 #if DEBUG_CONNECTIONS
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
183 GNUNET_TRANSPORT_disconnect (p1.th);
184 GNUNET_TRANSPORT_disconnect (p2.th);
185 #if DEBUG_CONNECTIONS
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Transports disconnected, returning success!\n");
189 GNUNET_SCHEDULER_shutdown (sched);
195 stop_arm (struct PeerContext *p)
198 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
199 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
200 GNUNET_OS_process_wait (p->arm_pid);
202 GNUNET_CONFIGURATION_destroy (p->cfg);
207 end_badly (void *cls,
208 const struct GNUNET_SCHEDULER_TaskContext *tc)
210 if (measurement_task != GNUNET_SCHEDULER_NO_TASK)
212 GNUNET_SCHEDULER_cancel (sched, measurement_task);
213 measurement_task = GNUNET_SCHEDULER_NO_TASK;
215 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
217 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
218 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
222 GNUNET_TRANSPORT_disconnect (p1.th);
224 GNUNET_TRANSPORT_disconnect (p2.th);
230 struct GNUNET_MessageHeader header;
235 get_size (unsigned int iter)
237 return MEASUREMENT_MSG_SIZE + sizeof (struct TestMessage);
241 notify_receive_new (void *cls,
242 const struct GNUNET_PeerIdentity *peer,
243 const struct GNUNET_MessageHeader *message,
244 struct GNUNET_TIME_Relative latency,
249 const struct TestMessage *hdr;
251 hdr = (const struct TestMessage*) message;
253 if (measurement_running == GNUNET_NO)
255 if (MTYPE != ntohs (message->type))
257 #if DEBUG_MEASUREMENT
258 if (ntohl(hdr->num) % 5000 == 0)
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Got message %u of size %u\n",
263 ntohs (message->size));
270 notify_ready_new (void *cls, size_t size, void *buf)
274 struct TestMessage hdr;
278 transmit_handle = NULL;
280 if (measurement_task == GNUNET_SCHEDULER_NO_TASK)
289 if (measurement_running != GNUNET_YES)
291 send_running = GNUNET_NO;
296 send_running = GNUNET_YES;
299 GNUNET_assert (size >= s);
300 GNUNET_assert (buf != NULL);
304 hdr.header.size = htons (s);
305 hdr.header.type = htons (MTYPE);
307 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
308 ret += sizeof (struct TestMessage);
309 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
310 ret += s - sizeof (struct TestMessage);
311 #if DEBUG_MEASUREMENT
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Sending message %u\n",n);
320 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
321 break; /* sometimes pack buffer full, sometimes not */
323 while (size - ret >= s);
324 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
333 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 );
335 static void measurement_counter
337 const struct GNUNET_SCHEDULER_TaskContext *tc)
339 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
341 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
347 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
348 GNUNET_TIME_UNIT_SECONDS,
349 &measurement_counter,
354 measurement_end (void *cls,
355 const struct GNUNET_SCHEDULER_TaskContext *tc)
357 measurement_task = GNUNET_SCHEDULER_NO_TASK;
358 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
361 measurement_running = GNUNET_NO;
362 struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_difference(start_time, GNUNET_TIME_absolute_get());
365 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
367 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
368 measurement_counter_task = GNUNET_SCHEDULER_NO_TASK;
371 fprintf(stderr,"\n");
374 if (transmit_handle != NULL)
376 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
377 transmit_handle = NULL;
380 if ((total_bytes/(duration.rel_value / 1000)) > (current_quota_p1 + (current_quota_p1 / 10)))
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
383 "\nQuota compliance failed: \n"\
384 "Quota allowed: %10llu kB/s\n"\
385 "Throughput : %10llu kB/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.rel_value / 1000)/1024));
393 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
394 "\nQuota compliance ok: \n"\
395 "Quota allowed: %10llu kB/s\n"\
396 "Throughput : %10llu kB/s\n", (current_quota_p1 / (1024)) , (total_bytes/(duration.rel_value / 1000)/1024));
400 if (current_quota_p1 < MEASUREMENT_MIN_QUOTA)
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Scheduling next measurement\n");
411 measure (current_quota_p1 / 10, current_quota_p2 / 10);
415 static void measure (unsigned long long quota_p1, unsigned long long quota_p2 )
417 current_quota_p1 = quota_p1;
418 current_quota_p2 = quota_p2;
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Starting transport level measurement for %u seconds and quota %llu kB/s\n", MEASUREMENT_INTERVALL.rel_value / 1000 , current_quota_p1 / 1024);
423 GNUNET_TRANSPORT_set_quota (p1.th,
425 GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
426 GNUNET_BANDWIDTH_value_init (current_quota_p1 ),
427 GNUNET_TIME_UNIT_FOREVER_REL,
429 GNUNET_TRANSPORT_set_quota (p2.th,
431 GNUNET_BANDWIDTH_value_init (current_quota_p2),
432 GNUNET_BANDWIDTH_value_init (current_quota_p2),
433 GNUNET_TIME_UNIT_FOREVER_REL,
436 GNUNET_SCHEDULER_cancel (sched, die_task);
437 die_task = GNUNET_SCHEDULER_add_delayed (sched,
441 if (measurement_counter_task != GNUNET_SCHEDULER_NO_TASK)
442 GNUNET_SCHEDULER_cancel (sched, measurement_counter_task);
443 measurement_counter_task = GNUNET_SCHEDULER_add_delayed (sched,
444 GNUNET_TIME_UNIT_SECONDS,
445 &measurement_counter,
447 measurement_task = GNUNET_SCHEDULER_add_delayed (sched,
448 MEASUREMENT_INTERVALL,
452 measurement_running = GNUNET_YES;
453 start_time = GNUNET_TIME_absolute_get ();
455 if (transmit_handle != NULL)
456 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
457 transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
459 get_size (0), 0, SEND_TIMEOUT,
465 notify_connect (void *cls,
466 const struct GNUNET_PeerIdentity *peer,
467 struct GNUNET_TIME_Relative latency,
472 #if DEBUG_CONNECTIONS
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Peer 1 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
480 #if DEBUG_CONNECTIONS
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Peer 2 `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
488 measure(MEASUREMENT_MAX_QUOTA,MEASUREMENT_MAX_QUOTA);
494 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
496 #if DEBUG_CONNECTIONS
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Peer `%4s' disconnected (%p)!\n",
499 GNUNET_i2s (peer), cls);
505 setup_peer (struct PeerContext *p, const char *cfgname)
507 p->cfg = GNUNET_CONFIGURATION_create ();
509 p->arm_pid = GNUNET_OS_start_process (NULL, NULL,
510 "gnunet-service-arm",
511 "gnunet-service-arm",
515 "-c", cfgname, NULL);
518 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
519 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, NULL,
524 GNUNET_assert (p->th != NULL);
529 exchange_hello_last (void *cls,
530 const struct GNUNET_MessageHeader *message)
532 struct PeerContext *me = cls;
534 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
536 GNUNET_assert (ok >= 3);
538 GNUNET_assert (message != NULL);
539 GNUNET_assert (GNUNET_OK ==
540 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
542 /* both HELLOs exchanged, get ready to test transmission! */
547 exchange_hello (void *cls,
548 const struct GNUNET_MessageHeader *message)
550 struct PeerContext *me = cls;
552 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
553 GNUNET_assert (ok >= 2);
555 GNUNET_assert (message != NULL);
556 GNUNET_assert (GNUNET_OK ==
557 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
559 GNUNET_TRANSPORT_offer_hello (p2.th, message);
560 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
565 struct GNUNET_SCHEDULER_Handle *s,
567 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
569 GNUNET_assert (ok == 1);
573 die_task = GNUNET_SCHEDULER_add_delayed (sched,
577 measurement_running = GNUNET_NO;
578 send_running = GNUNET_NO;
579 recv_running = GNUNET_NO;
583 setup_peer (&p1, "test_quota_compliance_tcp_peer1.conf");
584 setup_peer (&p2, "test_quota_compliance_tcp_peer2.conf");
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 setup_peer (&p1, "test_quota_compliance_http_peer1.conf");
592 setup_peer (&p2, "test_quota_compliance_http_peer2.conf");
596 setup_peer (&p1, "test_quota_compliance_tcp_peer1.conf");
597 setup_peer (&p2, "test_quota_compliance_tcp_peer2.conf");
601 setup_peer (&p1, "test_quota_compliance_tcp_peer1.conf");
602 setup_peer (&p2, "test_quota_compliance_tcp_peer2.conf");
606 setup_peer (&p1, "test_quota_compliance_tcp_peer1.conf");
607 setup_peer (&p2, "test_quota_compliance_tcp_peer2.conf");
612 GNUNET_assert(p1.th != NULL);
613 GNUNET_assert(p2.th != NULL);
614 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
618 main (int argc, char *argv[])
622 return GNUNET_SYSERR;
624 if (strstr(argv[0], "tcp_nat") != NULL)
626 is_tcp_nat = GNUNET_YES;
628 else if (strstr(argv[0], "tcp") != NULL)
632 else if (strstr(argv[0], "https") != NULL)
634 is_https = GNUNET_YES;
636 else if (strstr(argv[0], "http") != NULL)
638 is_http = GNUNET_YES;
640 else if (strstr(argv[0], "udp") != NULL)
644 GNUNET_log_setup ("test-quota-compliance",
651 char *const argv1[] = { "test-quota-compliance",
653 "test_quota_compliance_data.conf",
659 struct GNUNET_GETOPT_CommandLineOption options[] = {
660 GNUNET_GETOPT_OPTION_END
664 GNUNET_PROGRAM_run ((sizeof (argv1) / sizeof (char *)) - 1,
665 argv1, "test-quota-compliance", "nohelp",
670 GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer1");
671 GNUNET_DISK_directory_remove ("/tmp/test_quota_compliance_peer2");
675 /* end of test_quota_compliance.c */