2 This file is part of GNUnet.
3 (C) 2009 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 dht/gnunet-dht-driver.c
22 * @brief Driver for setting up a group of gnunet peers and
23 * then issuing GETS and PUTS on the DHT. Coarse results
24 * are reported, fine grained results (if requested) are
25 * logged to a (mysql) database, or to file.
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_dht_service.h"
34 #define VERBOSE GNUNET_NO
36 /* Timeout for entire driver to run */
37 #define DEFAULT_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
39 /* Timeout for waiting for (individual) replies to get requests */
40 #define DEFAULT_GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
42 /* Timeout for waiting for gets to be sent to the service */
43 #define DEFAULT_GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
45 /* Timeout for waiting for puts to be sent to the service */
46 #define DEFAULT_PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
48 #define DEFAULT_SECONDS_PER_PEER_START GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 45)
50 #define DEFAULT_TEST_DATA_SIZE 8
52 #define DEFAULT_MAX_OUTSTANDING_PUTS 10
54 #define DEFAULT_MAX_OUTSTANDING_GETS 10
56 #define DEFAULT_CONNECT_TIMEOUT 60
58 #define DEFAULT_TOPOLOGY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
64 /* This is a linked list */
65 struct TestPutContext *next;
68 * Handle to the first peers DHT service (via the API)
70 struct GNUNET_DHT_Handle *dht_handle;
73 * Handle to the PUT peer daemon
75 struct GNUNET_TESTING_Daemon *daemon;
78 * Identifier for this PUT
83 * Task for disconnecting DHT handles
85 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
90 /* This is a linked list */
91 struct TestGetContext *next;
94 * Handle to the first peers DHT service (via the API)
96 struct GNUNET_DHT_Handle *dht_handle;
99 * Handle for the DHT get request
101 struct GNUNET_DHT_GetHandle *get_handle;
104 * Handle to the GET peer daemon
106 struct GNUNET_TESTING_Daemon *daemon;
109 * Identifier for this GET
114 * Task for disconnecting DHT handles (and stopping GET)
116 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
119 * Whether or not this request has been fulfilled already.
125 * Simple struct to keep track of progress, and print a
126 * nice little percentage meter for long running tasks.
136 unsigned int completed;
140 char *startup_string;
146 * Timeout to let all get requests happen.
148 static struct GNUNET_TIME_Relative all_get_timeout;
153 static struct GNUNET_TIME_Relative get_timeout;
155 static struct GNUNET_TIME_Relative get_delay;
157 static struct GNUNET_TIME_Relative put_delay;
159 static struct GNUNET_TIME_Relative seconds_per_peer_start;
161 static unsigned long long test_data_size = DEFAULT_TEST_DATA_SIZE;
163 static unsigned long long max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
165 static unsigned long long max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
167 static unsigned long long malicious_getters;
169 static unsigned long long malicious_putters;
171 static unsigned long long malicious_droppers;
173 static unsigned long long settle_time;
175 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
177 static unsigned long long trialuid;
180 * List of GETS to perform
182 struct TestGetContext *all_gets;
185 * List of PUTS to perform
187 struct TestPutContext *all_puts;
190 * Directory to store temporary data in, defined in config file
192 static char *test_directory;
195 * Variable used to store the number of connections we should wait for.
197 static unsigned int expected_connections;
200 * Variable used to keep track of how many peers aren't yet started.
202 static unsigned long long peers_left;
205 * Handle to the set of all peers run for this test.
207 static struct GNUNET_TESTING_PeerGroup *pg;
210 * Global scheduler, used for all GNUNET_SCHEDULER_* functions.
212 static struct GNUNET_SCHEDULER_Handle *sched;
215 * Total number of peers to run, set based on config file.
217 static unsigned long long num_peers;
220 * Total number of items to insert.
222 static unsigned long long num_puts;
225 * Total number of items to attempt to get.
227 static unsigned long long num_gets;
230 * How many puts do we currently have in flight?
232 static unsigned long long outstanding_puts;
235 * How many puts are done?
237 static unsigned long long puts_completed;
240 * How many puts do we currently have in flight?
242 static unsigned long long outstanding_gets;
245 * How many gets are done?
247 static unsigned long long gets_completed;
250 * How many gets failed?
252 static unsigned long long gets_failed;
255 * Global used to count how many connections we have currently
256 * been notified about (how many times has topology_callback been called
259 static unsigned int total_connections;
262 * Global used to count how many failed connections we have
263 * been notified about (how many times has topology_callback
264 * been called with failure?)
266 static unsigned int failed_connections;
268 /* Task handle to use to schedule shutdown if something goes wrong */
269 GNUNET_SCHEDULER_TaskIdentifier die_task;
271 static char *blacklist_transports;
273 static enum GNUNET_TESTING_Topology topology;
275 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
277 static enum GNUNET_TESTING_Topology connect_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
279 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
281 static double connect_topology_option_modifier = 0.0;
283 static struct ProgressMeter *hostkey_meter;
285 static struct ProgressMeter *peer_start_meter;
287 static struct ProgressMeter *peer_connect_meter;
289 static struct ProgressMeter *put_meter;
291 static struct ProgressMeter *get_meter;
293 /* Global return value (0 for success, anything else for failure) */
297 * Create a meter to keep track of the progress of some task.
299 * @param total the total number of items to complete
300 * @param start_string a string to prefix the meter with (if printing)
301 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
304 * @return the progress meter
306 static struct ProgressMeter *
307 create_meter(unsigned int total, char * start_string, int print)
309 struct ProgressMeter *ret;
310 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
313 ret->modnum = total / 4;
314 ret->dotnum = (total / 50) + 1;
315 if (start_string != NULL)
316 ret->startup_string = GNUNET_strdup(start_string);
318 ret->startup_string = GNUNET_strdup("");
324 * Update progress meter (increment by one).
326 * @param meter the meter to update and print info for
328 * @return GNUNET_YES if called the total requested,
329 * GNUNET_NO if more items expected
332 update_meter(struct ProgressMeter *meter)
334 if (meter->print == GNUNET_YES)
336 if (meter->completed % meter->modnum == 0)
338 if (meter->completed == 0)
340 fprintf(stdout, "%sProgress: [0%%", meter->startup_string);
343 fprintf(stdout, "%d%%", (int)(((float)meter->completed / meter->total) * 100));
345 else if (meter->completed % meter->dotnum == 0)
346 fprintf(stdout, ".");
348 if (meter->completed + 1 == meter->total)
349 fprintf(stdout, "%d%%]\n", 100);
354 if (meter->completed == meter->total)
360 * Release resources for meter
362 * @param meter the meter to free
365 free_meter(struct ProgressMeter *meter)
367 GNUNET_free_non_null(meter->startup_string);
368 GNUNET_free_non_null(meter);
372 * Check whether peers successfully shut down.
374 void shutdown_callback (void *cls,
385 * Task to release DHT handles for PUT
388 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
390 struct TestPutContext *test_put = cls;
391 test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
392 GNUNET_DHT_disconnect(test_put->dht_handle);
393 test_put->dht_handle = NULL;
397 * Function scheduled to be run on the successful completion of this
401 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending test normally!\n", (char *)cls);
404 GNUNET_assert (pg != NULL);
405 struct TestPutContext *test_put = all_puts;
406 struct TestGetContext *test_get = all_gets;
408 while (test_put != NULL)
410 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
411 GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
412 if (test_put->dht_handle != NULL)
413 GNUNET_DHT_disconnect(test_put->dht_handle);
414 test_put = test_put->next;
417 while (test_get != NULL)
419 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
420 GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
421 if (test_get->get_handle != NULL)
422 GNUNET_DHT_get_stop(test_get->get_handle, NULL, NULL);
423 if (test_get->dht_handle != NULL)
424 GNUNET_DHT_disconnect(test_get->dht_handle);
425 test_get = test_get->next;
428 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
430 /* FIXME: optionally get stats for dropped messages, etc. */
431 if (dhtlog_handle != NULL)
433 fprintf(stderr, "Update trial endtime\n");
434 dhtlog_handle->update_trial (trialuid, 0, 0, 0);
437 if (hostkey_meter != NULL)
438 free_meter(hostkey_meter);
439 if (peer_start_meter != NULL)
440 free_meter(peer_start_meter);
441 if (peer_connect_meter != NULL)
442 free_meter(peer_connect_meter);
443 if (put_meter != NULL)
444 free_meter(put_meter);
445 if (get_meter != NULL)
446 free_meter(get_meter);
453 * Check if the get_handle is being used, if so stop the request. Either
454 * way, schedule the end_badly_cont function which actually shuts down the
458 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failing test with error: `%s'!\n", (char *)cls);
462 struct TestPutContext *test_put = all_puts;
463 struct TestGetContext *test_get = all_gets;
465 while (test_put != NULL)
467 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
468 GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
469 if (test_put->dht_handle != NULL)
470 GNUNET_DHT_disconnect(test_put->dht_handle);
471 test_put = test_put->next;
474 while (test_get != NULL)
476 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
477 GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
478 if (test_get->get_handle != NULL)
479 GNUNET_DHT_get_stop(test_get->get_handle, NULL, NULL);
480 if (test_get->dht_handle != NULL)
481 GNUNET_DHT_disconnect(test_get->dht_handle);
482 test_get = test_get->next;
485 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
487 /* FIXME: optionally get stats for dropped messages, etc. */
488 if (dhtlog_handle != NULL)
490 fprintf(stderr, "Update trial endtime\n");
491 dhtlog_handle->update_trial (trialuid, 0, 0, 0);
494 if (hostkey_meter != NULL)
495 free_meter(hostkey_meter);
496 if (peer_start_meter != NULL)
497 free_meter(peer_start_meter);
498 if (peer_connect_meter != NULL)
499 free_meter(peer_connect_meter);
500 if (put_meter != NULL)
501 free_meter(put_meter);
502 if (get_meter != NULL)
503 free_meter(get_meter);
509 * Task to release DHT handle associated with GET request.
512 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
514 struct TestGetContext *test_get = cls;
515 outstanding_gets--; /* GET is really finished */
516 GNUNET_DHT_disconnect(test_get->dht_handle);
517 test_get->dht_handle = NULL;
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n", gets_completed, gets_failed);
522 update_meter(get_meter);
523 if ((gets_completed == num_gets) && (outstanding_gets == 0))/* All gets successful */
525 GNUNET_SCHEDULER_cancel(sched, die_task);
526 GNUNET_SCHEDULER_add_now(sched, &finish_testing, NULL);
528 else if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0)) /* Had some failures */
530 GNUNET_SCHEDULER_cancel(sched, die_task);
531 GNUNET_SCHEDULER_add_now(sched, &finish_testing, NULL);
536 * Task to release get handle.
539 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
541 struct TestGetContext *test_get = cls;
543 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
545 GNUNET_assert(test_get->get_handle != NULL);
546 GNUNET_DHT_get_stop(test_get->get_handle, &get_stop_finished, test_get);
547 test_get->get_handle = NULL;
548 test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
552 * Iterator called if the GET request initiated returns a response.
555 * @param exp when will this value expire
556 * @param key key of the result
557 * @param type type of the result
558 * @param size number of bytes in data
559 * @param data pointer to the result data
561 void get_result_iterator (void *cls,
562 struct GNUNET_TIME_Absolute exp,
563 const GNUNET_HashCode * key,
568 struct TestGetContext *test_get = cls;
569 GNUNET_HashCode search_key; /* Key stored under */
570 char original_data[test_data_size]; /* Made up data to store */
572 memset(original_data, test_get->uid, sizeof(original_data));
573 GNUNET_CRYPTO_hash(original_data, test_data_size, &search_key);
575 if (test_get->succeeded == GNUNET_YES)
576 return; /* Get has already been successful, probably ending now */
578 if ((0 != memcmp(&search_key, key, sizeof (GNUNET_HashCode))) || (0 != memcmp(original_data, data, sizeof(original_data))))
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key or data is not the same as was inserted!\n");
585 test_get->succeeded = GNUNET_YES;
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
590 GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
591 GNUNET_SCHEDULER_add_continuation(sched, &get_stop_task, test_get, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
595 * Continuation telling us GET request was sent.
598 get_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
600 // Is there something to be done here?
601 if (tc->reason != GNUNET_SCHEDULER_REASON_PREREQ_DONE)
606 * Set up some data, and call API PUT function
609 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
611 struct TestGetContext *test_get = cls;
612 GNUNET_HashCode key; /* Made up key to store data under */
613 char data[test_data_size]; /* Made up data to store */
617 GNUNET_SCHEDULER_cancel(sched, die_task);
618 GNUNET_SCHEDULER_add_now(sched, &finish_testing, NULL);
620 if (test_get == NULL)
621 return; /* End of the list */
623 memset(data, test_get->uid, sizeof(data));
624 GNUNET_CRYPTO_hash(data, test_data_size, &key);
626 if (outstanding_gets > max_outstanding_gets)
628 GNUNET_SCHEDULER_add_delayed (sched, get_delay, &do_get, test_get);
632 test_get->dht_handle = GNUNET_DHT_connect(sched, test_get->daemon->cfg, 10);
633 /* Insert the data at the first peer */
634 GNUNET_assert(test_get->dht_handle != NULL);
636 test_get->get_handle = GNUNET_DHT_get_start(test_get->dht_handle,
637 GNUNET_TIME_relative_get_forever(),
640 &get_result_iterator,
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
647 test_get->daemon->shortname);
649 test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, get_timeout, &get_stop_task, test_get);
650 GNUNET_SCHEDULER_add_now (sched, &do_get, test_get->next);
654 * Called when the PUT request has been transmitted to the DHT service.
655 * Schedule the GET request for some time in the future.
658 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
660 struct TestPutContext *test_put = cls;
664 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
665 fprintf(stderr, "PUT Request failed!\n");
667 GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
668 test_put->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &put_disconnect_task, test_put);
669 if (GNUNET_YES == update_meter(put_meter))
671 GNUNET_assert(outstanding_puts == 0);
672 GNUNET_SCHEDULER_cancel (sched, die_task);
673 die_task = GNUNET_SCHEDULER_add_delayed (sched, all_get_timeout,
674 &end_badly, "from do gets");
675 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time), &do_get, all_gets);
681 * Set up some data, and call API PUT function
684 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
686 struct TestPutContext *test_put = cls;
687 GNUNET_HashCode key; /* Made up key to store data under */
688 char data[test_data_size]; /* Made up data to store */
691 if (test_put == NULL)
692 return; /* End of list */
694 memset(data, test_put->uid, sizeof(data));
695 GNUNET_CRYPTO_hash(data, test_data_size, &key);
697 if (outstanding_puts > max_outstanding_puts)
699 GNUNET_SCHEDULER_add_delayed (sched, put_delay, &do_put, test_put);
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
706 test_put->daemon->shortname);
708 test_put->dht_handle = GNUNET_DHT_connect(sched, test_put->daemon->cfg, 10);
710 GNUNET_assert(test_put->dht_handle != NULL);
712 GNUNET_DHT_put(test_put->dht_handle,
716 GNUNET_TIME_absolute_get_forever(),
717 GNUNET_TIME_relative_get_forever(),
718 &put_finished, test_put);
719 test_put->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_get_forever(), &put_disconnect_task, test_put);
720 rand = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
721 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, rand), &do_put, test_put->next);
726 * Set up some all of the put and get operations we want
727 * to do. Allocate data structure for each, add to list,
728 * then call actual insert functions.
731 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
734 uint32_t temp_daemon;
735 struct TestPutContext *test_put;
736 struct TestGetContext *test_get;
737 int remember[num_puts][num_peers];
739 for (i = 0; i < num_puts; i++)
741 test_put = GNUNET_malloc(sizeof(struct TestPutContext));
743 temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
744 test_put->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
745 test_put->next = all_puts;
749 for (i = 0; i < num_gets; i++)
751 test_get = GNUNET_malloc(sizeof(struct TestGetContext));
752 test_get->uid = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
753 temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
754 while (remember[test_get->uid][temp_daemon] == 1)
755 temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
756 test_get->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
757 remember[test_get->uid][temp_daemon] = 1;
758 test_get->next = all_gets;
762 /*GNUNET_SCHEDULER_cancel (sched, die_task);*/
763 die_task = GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, num_puts * 2),
764 &end_badly, "from do puts");
765 GNUNET_SCHEDULER_add_now (sched, &do_put, all_puts);
768 * This function is called whenever a connection attempt is finished between two of
769 * the started peers (started with GNUNET_TESTING_daemons_start). The total
770 * number of times this function is called should equal the number returned
771 * from the GNUNET_TESTING_connect_topology call.
773 * The emsg variable is NULL on success (peers connected), and non-NULL on
774 * failure (peers failed to connect).
777 topology_callback (void *cls,
778 const struct GNUNET_PeerIdentity *first,
779 const struct GNUNET_PeerIdentity *second,
781 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
782 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
783 struct GNUNET_TESTING_Daemon *first_daemon,
784 struct GNUNET_TESTING_Daemon *second_daemon,
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
792 first_daemon->shortname,
793 second_daemon->shortname,
800 failed_connections++;
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
802 first_daemon->shortname,
803 second_daemon->shortname, emsg);
806 GNUNET_assert(peer_connect_meter != NULL);
807 if (GNUNET_YES == update_meter(peer_connect_meter))
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
814 if (dhtlog_handle != NULL)
815 dhtlog_handle->update_connections (trialuid, total_connections);
817 GNUNET_SCHEDULER_cancel (sched, die_task);
818 /*die_task = GNUNET_SCHEDULER_add_delayed (sched, DEFAULT_TIMEOUT,
819 &end_badly, "from setup puts/gets");*/
821 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time), &setup_puts_and_gets, NULL);
823 else if (total_connections + failed_connections == expected_connections)
825 GNUNET_SCHEDULER_cancel (sched, die_task);
826 die_task = GNUNET_SCHEDULER_add_now (sched,
827 &end_badly, "from topology_callback (too many failed connections)");
832 peers_started_callback (void *cls,
833 const struct GNUNET_PeerIdentity *id,
834 const struct GNUNET_CONFIGURATION_Handle *cfg,
835 struct GNUNET_TESTING_Daemon *d, const char *emsg)
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
843 GNUNET_assert (id != NULL);
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
847 (num_peers - peers_left) + 1, num_peers);
852 if (GNUNET_YES == update_meter(peer_start_meter))
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "All %d daemons started, now connecting peers!\n",
859 GNUNET_SCHEDULER_cancel (sched, die_task);
861 expected_connections = -1;
862 if ((pg != NULL) && (peers_left == 0))
864 expected_connections = GNUNET_TESTING_connect_topology (pg, connect_topology, connect_topology_option, connect_topology_option_modifier);
865 peer_connect_meter = create_meter(expected_connections, "Peer connection ", GNUNET_YES);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Have %d expected connections\n", expected_connections);
872 if (expected_connections == GNUNET_SYSERR)
874 die_task = GNUNET_SCHEDULER_add_now (sched,
875 &end_badly, "from connect topology (bad return)");
878 die_task = GNUNET_SCHEDULER_add_delayed (sched,
879 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, DEFAULT_CONNECT_TIMEOUT * expected_connections),
880 &end_badly, "from connect topology (timeout)");
889 peers_left = num_peers; /* Reset counter */
890 if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Topology set up, now starting peers!\n");
896 GNUNET_TESTING_daemons_continue_startup(pg);
900 GNUNET_SCHEDULER_cancel (sched, die_task);
901 die_task = GNUNET_SCHEDULER_add_now (sched,
902 &end_badly, "from create topology (bad return)");
904 GNUNET_SCHEDULER_cancel (sched, die_task);
905 die_task = GNUNET_SCHEDULER_add_delayed (sched,
906 GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
907 &end_badly, "from continue startup (timeout)");
911 * Callback indicating that the hostkey was created for a peer.
914 * @param id the peer identity
915 * @param d the daemon handle (pretty useless at this point, remove?)
916 * @param emsg non-null on failure
918 void hostkey_callback (void *cls,
919 const struct GNUNET_PeerIdentity *id,
920 struct GNUNET_TESTING_Daemon *d,
925 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930 "Hostkey (%d/%d) created for peer `%s'\n",
931 num_peers - peers_left, num_peers, GNUNET_i2s(id));
935 if (GNUNET_YES == update_meter(hostkey_meter))
938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939 "All %d hostkeys created, now creating topology!\n",
942 GNUNET_SCHEDULER_cancel (sched, die_task);
943 /* Set up task in case topology creation doesn't finish
944 * within a reasonable amount of time */
945 die_task = GNUNET_SCHEDULER_add_delayed (sched,
946 DEFAULT_TOPOLOGY_TIMEOUT,
947 &end_badly, "from create_topology");
948 GNUNET_SCHEDULER_add_now(sched, &create_topology, NULL);
956 struct GNUNET_SCHEDULER_Handle *s,
958 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
960 struct GNUNET_TESTING_Host *hosts;
961 struct GNUNET_TESTING_Host *temphost;
963 char * connect_topology_str;
964 char * blacklist_topology_str;
965 char * connect_topology_option_str;
966 char * connect_topology_option_modifier_string;
968 char * topology_percentage_str;
969 float topology_percentage;
970 char * topology_probability_str;
972 float topology_probability;
973 unsigned long long temp_config_number;
982 /* Get path from configuration file */
983 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
990 * Get DHT specific testing options.
992 if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging"))||
993 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging_extended")))
995 dhtlog_handle = GNUNET_DHTLOG_connect(cfg);
996 if (dhtlog_handle == NULL)
998 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
999 "Could not connect to mysql server for logging, will NOT log dht operations!");
1006 GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing", "comment",
1008 trialmessage = NULL;
1011 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
1016 if (hostfile != NULL)
1018 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
1019 GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
1020 | GNUNET_DISK_PERM_USER_WRITE);
1021 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
1023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1024 "Could not open file specified for host list, ending test!");
1026 GNUNET_free_non_null(trialmessage);
1027 GNUNET_free(hostfile);
1031 data = GNUNET_malloc_large (frstat.st_size);
1032 GNUNET_assert(data != NULL);
1033 if (frstat.st_size !=
1034 GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
1036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1037 "Could not read file %s specified for host list, ending test!", hostfile);
1038 GNUNET_free (hostfile);
1045 while (count < frstat.st_size)
1048 if (((data[count] == '\n') || (data[count] == '\0')) && (buf != &data[count]))
1051 temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
1052 temphost->hostname = buf;
1053 temphost->next = hosts;
1055 buf = &data[count + 1];
1057 else if ((data[count] == '\n') || (data[count] == '\0'))
1058 buf = &data[count + 1];
1063 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_getters",
1064 &malicious_getters))
1065 malicious_getters = 0;
1068 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_putters",
1069 &malicious_putters))
1070 malicious_putters = 0;
1073 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_droppers",
1074 &malicious_droppers))
1075 malicious_droppers = 0;
1078 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "settle_time",
1082 if (GNUNET_SYSERR ==
1083 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_puts",
1085 num_puts = num_peers;
1087 if (GNUNET_SYSERR ==
1088 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_gets",
1090 num_gets = num_peers;
1093 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_timeout",
1094 &temp_config_number))
1095 get_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1097 get_timeout = DEFAULT_GET_TIMEOUT;
1100 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_puts",
1101 &temp_config_number))
1102 max_outstanding_puts = temp_config_number;
1104 max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
1107 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_gets",
1108 &temp_config_number))
1109 max_outstanding_gets = temp_config_number;
1111 max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
1114 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "timeout",
1115 &temp_config_number))
1116 all_get_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1118 all_get_timeout.value = get_timeout.value * ((num_gets / max_outstanding_gets) + 1);
1121 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_delay",
1122 &temp_config_number))
1123 get_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1125 get_delay = DEFAULT_GET_DELAY;
1128 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "put_delay",
1129 &temp_config_number))
1130 put_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1132 put_delay = DEFAULT_PUT_DELAY;
1135 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "peer_start_timeout",
1136 &temp_config_number))
1137 seconds_per_peer_start = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
1139 seconds_per_peer_start = DEFAULT_SECONDS_PER_PEER_START;
1142 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "data_size",
1143 &temp_config_number))
1144 test_data_size = temp_config_number;
1146 test_data_size = DEFAULT_TEST_DATA_SIZE;
1149 * Get testing related options.
1152 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
1153 &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
1155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1156 "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
1157 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1161 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "percentage",
1162 &topology_percentage_str))
1163 topology_percentage = 0.5;
1166 topology_percentage = atof (topology_percentage_str);
1170 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "probability",
1171 &topology_probability_str))
1172 topology_probability = 0.5;
1175 topology_probability = atof (topology_probability_str);
1179 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
1180 &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connect_topology, connect_topology_str)))
1182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1183 "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1185 GNUNET_free_non_null(connect_topology_str);
1187 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
1188 &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
1190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1191 "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
1192 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1194 GNUNET_free_non_null(connect_topology_option_str);
1196 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
1197 &connect_topology_option_modifier_string))
1199 if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
1201 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1202 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1203 connect_topology_option_modifier_string,
1204 "connect_topology_option_modifier",
1207 GNUNET_free (connect_topology_option_modifier_string);
1210 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
1211 &blacklist_transports))
1212 blacklist_transports = NULL;
1215 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
1216 &blacklist_topology_str)) &&
1217 (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
1219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1220 "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1222 GNUNET_free_non_null(topology_str);
1223 GNUNET_free_non_null(blacklist_topology_str);
1225 /* Get number of peers to start from configuration */
1226 if (GNUNET_SYSERR ==
1227 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1231 "Number of peers must be specified in section %s option %s\n", topology_str, "TESTING", "NUM_PEERS");
1234 /* Set peers_left so we know when all peers started */
1235 peers_left = num_peers;
1237 /* Set up a task to end testing if peer start fails */
1238 die_task = GNUNET_SCHEDULER_add_delayed (sched,
1239 GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
1240 &end_badly, "didn't generate all hostkeys within allowed startup time!");
1242 if (dhtlog_handle == NULL)
1243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1244 "dhtlog_handle is NULL!");
1246 if ((trialmessage != NULL) && (dhtlog_handle != NULL))
1248 dhtlog_handle->insert_trial (&trialuid, peers_left, topology,
1249 blacklist_topology, connect_topology,
1250 connect_topology_option,
1251 connect_topology_option_modifier, topology_percentage,
1252 topology_probability, num_puts, num_gets,
1253 max_outstanding_gets, settle_time, 1,
1254 malicious_getters, malicious_putters,
1255 malicious_droppers, trialmessage);
1257 else if (dhtlog_handle != NULL)
1259 dhtlog_handle->insert_trial (&trialuid, peers_left, topology,
1260 blacklist_topology, connect_topology,
1261 connect_topology_option,
1262 connect_topology_option_modifier, topology_percentage,
1263 topology_probability, num_puts, num_gets,
1264 max_outstanding_gets, settle_time, 1,
1265 malicious_getters, malicious_putters,
1266 malicious_droppers, "");
1269 hostkey_meter = create_meter(peers_left, "Hostkeys created ", GNUNET_YES);
1270 peer_start_meter = create_meter(peers_left, "Peers started ", GNUNET_YES);
1272 put_meter = create_meter(num_puts, "Puts completed ", GNUNET_YES);
1273 get_meter = create_meter(num_gets, "Gets completed ", GNUNET_YES);
1274 pg = GNUNET_TESTING_daemons_start (sched, cfg,
1276 GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
1277 &hostkey_callback, NULL,
1278 &peers_started_callback, NULL,
1279 &topology_callback, NULL,
1286 main (int argc, char *argv[])
1289 struct GNUNET_GETOPT_CommandLineOption options[] = {
1290 GNUNET_GETOPT_OPTION_END
1293 ret = GNUNET_PROGRAM_run (argc,
1294 argv, "gnunet-dht-driver", "nohelp",
1295 options, &run, &ok);
1297 if (ret != GNUNET_OK)
1299 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`gnunet-dht-driver': Failed with error code %d\n", ret);
1303 * Need to remove base directory, subdirectories taken care
1304 * of by the testing framework.
1306 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
1308 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
1313 /* end of test_dht_twopeer_put_get.c */