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.
26 * @author Nathan Evans (who to blame)
29 #ifndef HAVE_MALICIOUS
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_dht_service.h"
39 /* Specific DEBUG hack, do not use normally (may leak memory, segfault, or eat children.) */
40 #define ONLY_TESTING GNUNET_NO
43 #define VERBOSE GNUNET_NO
45 /* Timeout for entire driver to run */
46 #define DEFAULT_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
48 /* Timeout for waiting for (individual) replies to get requests */
49 #define DEFAULT_GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
51 #define DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
53 /* Timeout for waiting for gets to be sent to the service */
54 #define DEFAULT_GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
56 /* Timeout for waiting for puts to be sent to the service */
57 #define DEFAULT_PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10)
59 /* Time to allow a find peer request to take */
60 #define DEFAULT_FIND_PEER_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 40)
62 /* Time to wait for all peers disconnected due to to churn to actually be removed from system */
63 #define DEFAULT_PEER_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
65 #define DEFAULT_SECONDS_PER_PEER_START GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 45)
67 #define DEFAULT_TEST_DATA_SIZE 8
69 #define DEFAULT_BUCKET_SIZE 4
71 /* If more than this many peers are added, slow down sending */
72 #define MAX_FIND_PEER_CUTOFF 2000
74 /* If less than this many peers are added, speed up sending */
75 #define MIN_FIND_PEER_CUTOFF 500
77 /* How often (in seconds) to print out connection information */
78 #define CONN_UPDATE_DURATION 10
80 #define DEFAULT_MAX_OUTSTANDING_PUTS 10
82 #define DEFAULT_MAX_OUTSTANDING_FIND_PEERS 64
84 #define DEFAULT_FIND_PEER_OFFSET GNUNET_TIME_relative_divide (DEFAULT_FIND_PEER_DELAY, DEFAULT_MAX_OUTSTANDING_FIND_PEERS)
86 #define DEFAULT_MAX_OUTSTANDING_GETS 10
88 #define DEFAULT_CONNECT_TIMEOUT 60
90 #define DEFAULT_TOPOLOGY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
92 #define DEFAULT_RECONNECT_ATTEMPTS 8
95 * Default frequency for sending malicious get messages
97 #define DEFAULT_MALICIOUS_GET_FREQUENCY GNUNET_TIME_UNIT_SECONDS
100 * Default frequency for sending malicious put messages
102 #define DEFAULT_MALICIOUS_PUT_FREQUENCY GNUNET_TIME_UNIT_SECONDS
106 struct MaliciousContext
109 * Handle to DHT service (via the API)
111 struct GNUNET_DHT_Handle *dht_handle;
114 * Handle to the peer daemon
116 struct GNUNET_TESTING_Daemon *daemon;
119 * Task for disconnecting DHT handles
121 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
124 * What type of malicious to set this peer to.
131 /* This is a linked list */
132 struct TestFindPeer *next;
134 /* Handle to the bigger context */
135 struct FindPeerContext *find_peer_context;
138 * Handle to the peer's DHT service (via the API)
140 struct GNUNET_DHT_Handle *dht_handle;
143 * Handle to the peer daemon
145 struct GNUNET_TESTING_Daemon *daemon;
148 * Task for disconnecting DHT handles
150 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
153 struct TestPutContext
155 /* This is a linked list */
156 struct TestPutContext *next;
159 * Handle to the first peers DHT service (via the API)
161 struct GNUNET_DHT_Handle *dht_handle;
164 * Handle to the PUT peer daemon
166 struct GNUNET_TESTING_Daemon *daemon;
169 * Identifier for this PUT
174 * Task for disconnecting DHT handles
176 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
179 struct TestGetContext
181 /* This is a linked list */
182 struct TestGetContext *next;
185 * Handle to the first peers DHT service (via the API)
187 struct GNUNET_DHT_Handle *dht_handle;
190 * Handle for the DHT get request
192 struct GNUNET_DHT_GetHandle *get_handle;
195 * Handle to the GET peer daemon
197 struct GNUNET_TESTING_Daemon *daemon;
200 * Identifier for this GET
205 * Task for disconnecting DHT handles (and stopping GET)
207 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
210 * Whether or not this request has been fulfilled already.
216 * Simple struct to keep track of progress, and print a
217 * nice little percentage meter for long running tasks.
227 unsigned int completed;
231 char *startup_string;
235 * Linked list of information for populating statistics
236 * before ending trial.
238 struct StatisticsIteratorContext
240 const struct GNUNET_PeerIdentity *peer;
241 unsigned int stat_routes;
242 unsigned int stat_route_forwards;
243 unsigned int stat_results;
244 unsigned int stat_results_to_client;
245 unsigned int stat_result_forwards;
246 unsigned int stat_gets;
247 unsigned int stat_puts;
248 unsigned int stat_puts_inserted;
249 unsigned int stat_find_peer;
250 unsigned int stat_find_peer_start;
251 unsigned int stat_get_start;
252 unsigned int stat_put_start;
253 unsigned int stat_find_peer_reply;
254 unsigned int stat_get_reply;
255 unsigned int stat_find_peer_answer;
256 unsigned int stat_get_response_start;
260 * Context for getting a topology, logging it, and continuing
261 * on with some next operation.
263 struct TopologyIteratorContext
265 unsigned int total_iterations;
266 unsigned int current_iteration;
267 unsigned int total_connections;
268 unsigned int total_peers;
269 struct GNUNET_CONTAINER_MultiHashMap *peers_seen;
270 struct GNUNET_PeerIdentity *peer;
271 GNUNET_SCHEDULER_Task cont;
273 struct GNUNET_TIME_Relative timeout;
278 /** Node in the heap */
279 struct GNUNET_CONTAINER_HeapNode *heap_node;
281 /** Peer the count refers to */
282 struct GNUNET_PeerIdentity peer_id;
284 /** Count of connections this peer has */
289 * Context for sending out find peer requests.
291 struct FindPeerContext
294 * How long to send find peer requests, once the settle time
295 * is over don't send any more out!
297 struct GNUNET_TIME_Absolute endtime;
300 * Number of connections in the current topology
301 * (after this round of find peer requests has ended).
303 unsigned int current_peers;
306 * Number of connections in the current topology
307 * (before this round of find peer requests started).
309 unsigned int previous_peers;
312 * Number of find peer requests we have currently
315 unsigned int outstanding;
318 * Number of find peer requests to send in this round.
323 * Number of find peer requests sent last time around.
325 unsigned int last_sent;
328 * Hashmap of peers in the current topology, value
329 * is a PeerCount, with the number of connections
332 struct GNUNET_CONTAINER_MultiHashMap *peer_hash;
335 * Min heap which orders values in the peer_hash for
338 struct GNUNET_CONTAINER_Heap *peer_min_heap;
341 * Callback for counting the peers in the current topology.
343 GNUNET_TESTING_NotifyTopology count_peers_cb;
349 * Next full round (puts + gets).
354 * Next round of gets.
359 * Next round of puts.
364 * Next round of churn.
372 * How long to try to connect two peers.
374 struct GNUNET_TIME_Relative connect_timeout;
377 * How many times to re-attempt connecting two peers.
379 static unsigned long long connect_attempts;
382 * Timeout to let all GET requests happen.
384 static struct GNUNET_TIME_Relative all_get_timeout;
389 static struct GNUNET_TIME_Relative get_timeout;
392 * Time to allow for GET requests to be sent to service.
394 static struct GNUNET_TIME_Relative get_delay;
397 * Time to allow for PUT requests to be sent to service.
399 static struct GNUNET_TIME_Relative put_delay;
402 * Delay between sending find peer requests (if
403 * handled by the driver, no effect if sent by service).
405 static struct GNUNET_TIME_Relative find_peer_delay;
408 * Time between find peer requests
409 * (find_peer_delay / max_outstanding_find_peer)
411 static struct GNUNET_TIME_Relative find_peer_offset;
414 * How many seconds to allow each peer to start.
416 static struct GNUNET_TIME_Relative seconds_per_peer_start;
419 * At what time did we start the connection process.
421 static struct GNUNET_TIME_Absolute connect_start_time;
424 * What was the last time we updated connection/second information.
426 static struct GNUNET_TIME_Absolute connect_last_time;
429 * At what time did we start the hostkey creation process.
431 static struct GNUNET_TIME_Absolute hostkey_start_time;
434 * At what time did we start the peer startup process.
436 static struct GNUNET_TIME_Absolute peer_start_time;
439 * Boolean value, should the driver issue find peer requests
440 * (GNUNET_YES) or should it be left to the service (GNUNET_NO)
442 static unsigned int do_find_peer;
445 * Whether or not to insert gauger data.
447 static unsigned int insert_gauger_data;
451 * Are we currently trying to connect two peers repeatedly?
453 static unsigned int repeat_connect_mode;
456 * Task for repeating connects.
458 GNUNET_SCHEDULER_TaskIdentifier repeat_connect_task;
460 struct GNUNET_TESTING_Daemon *repeat_connect_peer1;
461 struct GNUNET_TESTING_Daemon *repeat_connect_peer2;
465 * Boolean value, should replication be done by the dht
466 * service (GNUNET_YES) or by the driver (GNUNET_NO)
468 static unsigned int in_dht_replication;
471 * Size of test data to insert/retrieve during testing.
473 static unsigned long long test_data_size = DEFAULT_TEST_DATA_SIZE;
476 * Maximum number of concurrent connections to peers.
478 static unsigned long long max_outstanding_connections;
481 * Maximum number of concurrent ssh instances to peers.
483 static unsigned long long max_concurrent_ssh;
486 * Maximum number of concurrent PUT requests.
488 static unsigned long long max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
491 * Maximum number of concurrent GET requests.
493 static unsigned long long max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
496 * Number of nodes issuing malicious GET messages.
498 static unsigned long long malicious_getters;
501 * Maximum number of concurrent find peer messages being sent.
503 static unsigned long long max_outstanding_find_peers;
506 * Number of nodes issuing malicious PUT messages.
508 static unsigned long long malicious_putters;
511 * Time (in seconds) to delay between rounds.
513 static unsigned long long round_delay;
516 * The identifier for this trial (if we have one)
517 * for external data collection.
519 static unsigned long long trial_to_run;
522 * How many malicious droppers to seed in the network.
524 static unsigned long long malicious_droppers;
527 * Bloom filter to restrict malicious nodes chosen.
529 struct GNUNET_CONTAINER_BloomFilter *malicious_bloom;
532 * Whether malicious droppers should be chosen based on proximity to a key.
534 static int malicious_sybil;
537 * Target for the malicious sybil nodes (choose the closest to this key).
539 static GNUNET_HashCode sybil_target;
542 * How often to send malicious GET messages.
544 static struct GNUNET_TIME_Relative malicious_get_frequency;
547 * How often to send malicious PUT messages.
549 static struct GNUNET_TIME_Relative malicious_put_frequency;
552 * How long to send find peer requests.
554 static unsigned long long settle_time;
557 * Handle to the dhtlog service.
559 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
562 * Replication value for GET requests.
564 static unsigned long long get_replication;
567 * Replication value for PUT requests.
569 static unsigned long long put_replication;
572 * If GNUNET_YES, insert data at the same peers every time.
573 * Otherwise, choose a new random peer to insert at each time.
575 static unsigned int replicate_same;
578 * If GNUNET_YES, issue GET requests at the same peers every time.
579 * Otherwise, choose a new random peer/data combination to search
582 static unsigned int get_from_same;
585 * Should malicious peers be set after allowing for settle time?
586 * Default is to set them malicious after initial connection setup.
588 static unsigned int malicious_after_settle;
591 * Number of rounds for testing (PUTS + GETS)
593 static unsigned long long total_rounds;
596 * Target number of connections (will stop sending find peer
597 * messages when this number is exceeded)
599 static unsigned long long target_total_connections;
602 * Number of rounds already run
604 static unsigned int rounds_finished;
607 * Number of rounds of churn to read from the file (first line, should be a single number).
609 static unsigned int churn_rounds;
612 * Current round we are in for churn, tells us how many peers to connect/disconnect.
614 static unsigned int current_churn_round;
617 * Number of times to churn per round
619 static unsigned long long churns_per_round;
622 * Array of churn values.
624 static unsigned int *churn_array;
627 * Hash map of stats contexts.
629 static struct GNUNET_CONTAINER_MultiHashMap *stats_map;
632 * LL of malicious settings.
634 struct MaliciousContext *all_malicious;
637 * List of GETS to perform
639 struct TestGetContext *all_gets;
642 * List of PUTS to perform
644 struct TestPutContext *all_puts;
647 * Directory to store temporary data in, defined in config file
649 static char *test_directory;
652 * Variable used to store the number of connections we should wait for.
654 static unsigned int expected_connections;
657 * Variable used to keep track of how many peers aren't yet started.
659 static unsigned long long peers_left;
662 * Handle to the set of all peers run for this test.
664 static struct GNUNET_TESTING_PeerGroup *pg;
667 * Global config handle.
669 static const struct GNUNET_CONFIGURATION_Handle *config;
672 * Total number of peers to run, set based on config file.
674 static unsigned long long num_peers;
677 * Total number of items to insert.
679 static unsigned long long num_puts;
682 * How many puts do we currently have in flight?
684 static unsigned long long outstanding_puts;
687 * How many puts are done?
689 static unsigned long long puts_completed;
692 * Total number of items to attempt to get.
694 static unsigned long long num_gets;
697 * How many puts do we currently have in flight?
699 static unsigned long long outstanding_gets;
702 * How many gets are done?
704 static unsigned long long gets_completed;
707 * Total number of items to attempt to get.
709 static unsigned long long cumulative_num_gets;
712 * How many gets are done?
714 static unsigned long long cumulative_successful_gets;
717 * How many gets failed?
719 static unsigned long long gets_failed;
722 * How many malicious control messages do
723 * we currently have in flight?
725 static unsigned long long outstanding_malicious;
728 * How many set malicious peers are done?
730 static unsigned int malicious_completed;
733 * For gauger logging, what specific identifier (svn revision)
736 static unsigned long long revision;
739 * Global used to count how many connections we have currently
740 * been notified about (how many times has topology_callback been called
743 static unsigned int total_connections;
746 * Previous connections, for counting new connections during some duration.
748 static unsigned int previous_connections;
751 * For counting failed connections during some duration.
753 static unsigned int previous_failed_connections;
756 * Global used to count how many failed connections we have
757 * been notified about (how many times has topology_callback
758 * been called with failure?)
760 static unsigned int failed_connections;
763 * If GNUNET_YES, only log PUT/GET round data to mysql, otherwise
764 * log everything (including each dht service logging).
766 static unsigned int dhtlog_minimal;
768 /* Task handle to use to schedule shutdown if something goes wrong */
769 GNUNET_SCHEDULER_TaskIdentifier die_task;
771 static char *blacklist_transports;
773 static enum GNUNET_TESTING_Topology topology;
775 static enum GNUNET_TESTING_Topology blacklist_topology =
776 GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
778 static enum GNUNET_TESTING_Topology connect_topology =
779 GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
781 static enum GNUNET_TESTING_TopologyOption connect_topology_option =
782 GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
784 static double connect_topology_option_modifier = 0.0;
786 static struct ProgressMeter *hostkey_meter;
788 static struct ProgressMeter *peer_start_meter;
790 static struct ProgressMeter *peer_connect_meter;
792 static struct ProgressMeter *put_meter;
794 static struct ProgressMeter *get_meter;
796 static GNUNET_HashCode *known_keys;
798 /* Global return value (0 for success, anything else for failure) */
802 * Create a meter to keep track of the progress of some task.
804 * @param total the total number of items to complete
805 * @param start_string a string to prefix the meter with (if printing)
806 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
809 * @return the progress meter
811 static struct ProgressMeter *
812 create_meter(unsigned int total, char * start_string, int print)
814 struct ProgressMeter *ret;
815 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
818 ret->modnum = total / 4;
819 ret->dotnum = (total / 50) + 1;
820 if (start_string != NULL)
821 ret->startup_string = GNUNET_strdup(start_string);
823 ret->startup_string = GNUNET_strdup("");
829 * Update progress meter (increment by one).
831 * @param meter the meter to update and print info for
833 * @return GNUNET_YES if called the total requested,
834 * GNUNET_NO if more items expected
837 update_meter(struct ProgressMeter *meter)
839 if (meter->print == GNUNET_YES)
841 if (meter->completed % meter->modnum == 0)
843 if (meter->completed == 0)
845 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
848 fprintf (stdout, "%d%%", (int) (((float) meter->completed
849 / meter->total) * 100));
851 else if (meter->completed % meter->dotnum == 0)
852 fprintf (stdout, ".");
854 if (meter->completed + 1 == meter->total)
855 fprintf (stdout, "%d%%]\n", 100);
860 if (meter->completed == meter->total)
866 * Reset progress meter.
868 * @param meter the meter to reset
870 * @return GNUNET_YES if meter reset,
871 * GNUNET_SYSERR on error
874 reset_meter(struct ProgressMeter *meter)
877 return GNUNET_SYSERR;
879 meter->completed = 0;
884 * Release resources for meter
886 * @param meter the meter to free
889 free_meter(struct ProgressMeter *meter)
891 GNUNET_free_non_null (meter->startup_string);
896 * Check whether peers successfully shut down.
899 shutdown_callback(void *cls, const char *emsg)
909 * Task to release DHT handles for PUT
912 put_disconnect_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
914 struct TestPutContext *test_put = cls;
915 test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
916 GNUNET_DHT_disconnect (test_put->dht_handle);
917 test_put->dht_handle = NULL;
918 if (replicate_same == GNUNET_NO)
920 = GNUNET_TESTING_daemon_get (
922 GNUNET_CRYPTO_random_u32 (
923 GNUNET_CRYPTO_QUALITY_WEAK,
928 * Function scheduled to be run on the successful completion of this
932 finish_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
934 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending test normally!\n",
936 GNUNET_assert (pg != NULL);
937 struct TestPutContext *test_put = all_puts;
938 struct TestGetContext *test_get = all_gets;
939 char *temp_get_string;
942 while (test_put != NULL)
944 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
945 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
946 if (test_put->dht_handle != NULL)
947 GNUNET_DHT_disconnect (test_put->dht_handle);
948 test_put = test_put->next;
951 while (test_get != NULL)
953 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
954 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
955 if (test_get->get_handle != NULL)
956 GNUNET_DHT_get_stop (test_get->get_handle);
957 if (test_get->dht_handle != NULL)
958 GNUNET_DHT_disconnect (test_get->dht_handle);
959 test_get = test_get->next;
962 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
964 if (dhtlog_handle != NULL)
966 fprintf (stderr, "Update trial endtime\n");
967 dhtlog_handle->update_trial (cumulative_successful_gets);
968 GNUNET_DHTLOG_disconnect (dhtlog_handle);
969 dhtlog_handle = NULL;
972 if (hostkey_meter != NULL)
973 free_meter (hostkey_meter);
974 if (peer_start_meter != NULL)
975 free_meter (peer_start_meter);
976 if (peer_connect_meter != NULL)
977 free_meter (peer_connect_meter);
978 if (put_meter != NULL)
979 free_meter (put_meter);
980 if (get_meter != NULL)
981 free_meter (get_meter);
983 GNUNET_asprintf (&temp_get_string,
984 "DHT Successful GETs (trial %d)", trial_to_run);
985 GNUNET_asprintf (&revision_str, "%llu", revision);
986 if (GNUNET_YES == insert_gauger_data)
987 GAUGER_ID("DHT_TESTING", temp_get_string, cumulative_successful_gets / (double)cumulative_num_gets, "percent successful", revision_str);
990 "Finished trial, had %llu successful gets out of %llu total, %.2f percent succeeded\n",
991 cumulative_successful_gets, cumulative_num_gets,
992 cumulative_successful_gets / (double) cumulative_num_gets);
993 GNUNET_free(temp_get_string);
999 * Callback for iterating over all the peer connections of a peer group.
1002 log_topology_cb(void *cls, const struct GNUNET_PeerIdentity *first,
1003 const struct GNUNET_PeerIdentity *second, const char *emsg)
1005 struct TopologyIteratorContext *topo_ctx = cls;
1006 if ((first != NULL) && (second != NULL))
1008 if ((topo_ctx->peers_seen != NULL) && (GNUNET_NO
1009 == GNUNET_CONTAINER_multihashmap_contains (topo_ctx->peers_seen,
1010 &first->hashPubKey)))
1012 GNUNET_CONTAINER_multihashmap_put (topo_ctx->peers_seen,
1013 &first->hashPubKey, NULL,
1014 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1015 topo_ctx->total_peers++;
1017 topo_ctx->total_connections++;
1018 if ((GNUNET_NO == dhtlog_minimal) && (dhtlog_handle != NULL))
1019 dhtlog_handle->insert_extended_topology (first, second);
1023 GNUNET_assert(dhtlog_handle != NULL);
1025 GNUNET_ERROR_TYPE_WARNING,
1026 "Topology iteration (%u/%u) finished (%u connections, %u peers)\n",
1027 topo_ctx->current_iteration, topo_ctx->total_iterations,
1028 topo_ctx->total_connections, topo_ctx->total_peers);
1029 dhtlog_handle->update_topology (topo_ctx->total_connections);
1030 if (topo_ctx->cont != NULL)
1031 GNUNET_SCHEDULER_add_now (topo_ctx->cont, topo_ctx->cls);
1032 if (topo_ctx->peers_seen != NULL)
1033 GNUNET_CONTAINER_multihashmap_destroy (topo_ctx->peers_seen);
1034 GNUNET_free(topo_ctx);
1039 * Iterator over hash map entries.
1041 * @param cls closure - always NULL
1042 * @param key current key code
1043 * @param value value in the hash map, a stats context
1044 * @return GNUNET_YES if we should continue to
1049 stats_iterate(void *cls, const GNUNET_HashCode * key, void *value)
1051 struct StatisticsIteratorContext *stats_ctx;
1055 dhtlog_handle->insert_stat (stats_ctx->peer, stats_ctx->stat_routes,
1056 stats_ctx->stat_route_forwards,
1057 stats_ctx->stat_results,
1058 stats_ctx->stat_results_to_client,
1059 stats_ctx->stat_result_forwards,
1060 stats_ctx->stat_gets, stats_ctx->stat_puts,
1061 stats_ctx->stat_puts_inserted,
1062 stats_ctx->stat_find_peer,
1063 stats_ctx->stat_find_peer_start,
1064 stats_ctx->stat_get_start,
1065 stats_ctx->stat_put_start,
1066 stats_ctx->stat_find_peer_reply,
1067 stats_ctx->stat_get_reply,
1068 stats_ctx->stat_find_peer_answer,
1069 stats_ctx->stat_get_response_start);
1070 GNUNET_free(stats_ctx);
1075 stats_finished(void *cls, int result)
1077 fprintf (stderr, "Finished getting all peers statistics, iterating!\n");
1078 GNUNET_CONTAINER_multihashmap_iterate (stats_map, &stats_iterate, NULL);
1079 GNUNET_CONTAINER_multihashmap_destroy (stats_map);
1080 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
1084 * Callback function to process statistic values.
1086 * @param cls closure
1087 * @param peer the peer the statistics belong to
1088 * @param subsystem name of subsystem that created the statistic
1089 * @param name the name of the datum
1090 * @param value the current value
1091 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1092 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1095 stats_handle(void *cls, const struct GNUNET_PeerIdentity *peer,
1096 const char *subsystem, const char *name, uint64_t value, int is_persistent)
1098 struct StatisticsIteratorContext *stats_ctx;
1100 if (dhtlog_handle != NULL)
1101 dhtlog_handle->add_generic_stat (peer, name, subsystem, value);
1102 if (GNUNET_CONTAINER_multihashmap_contains (stats_map, &peer->hashPubKey))
1104 stats_ctx = GNUNET_CONTAINER_multihashmap_get (stats_map,
1109 stats_ctx = GNUNET_malloc(sizeof(struct StatisticsIteratorContext));
1110 stats_ctx->peer = peer;
1111 GNUNET_CONTAINER_multihashmap_put (stats_map, &peer->hashPubKey,
1113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1115 GNUNET_assert(stats_ctx != NULL);
1117 if (strcmp (name, STAT_ROUTES) == 0)
1118 stats_ctx->stat_routes = value;
1119 else if (strcmp (name, STAT_ROUTE_FORWARDS) == 0)
1120 stats_ctx->stat_route_forwards = value;
1121 else if (strcmp (name, STAT_RESULTS) == 0)
1122 stats_ctx->stat_results = value;
1123 else if (strcmp (name, STAT_RESULTS_TO_CLIENT) == 0)
1124 stats_ctx->stat_results_to_client = value;
1125 else if (strcmp (name, STAT_RESULT_FORWARDS) == 0)
1126 stats_ctx->stat_result_forwards = value;
1127 else if (strcmp (name, STAT_GETS) == 0)
1128 stats_ctx->stat_gets = value;
1129 else if (strcmp (name, STAT_PUTS) == 0)
1130 stats_ctx->stat_puts = value;
1131 else if (strcmp (name, STAT_PUTS_INSERTED) == 0)
1132 stats_ctx->stat_puts_inserted = value;
1133 else if (strcmp (name, STAT_FIND_PEER) == 0)
1134 stats_ctx->stat_find_peer = value;
1135 else if (strcmp (name, STAT_FIND_PEER_START) == 0)
1136 stats_ctx->stat_find_peer_start = value;
1137 else if (strcmp (name, STAT_GET_START) == 0)
1138 stats_ctx->stat_get_start = value;
1139 else if (strcmp (name, STAT_PUT_START) == 0)
1140 stats_ctx->stat_put_start = value;
1141 else if (strcmp (name, STAT_FIND_PEER_REPLY) == 0)
1142 stats_ctx->stat_find_peer_reply = value;
1143 else if (strcmp (name, STAT_GET_REPLY) == 0)
1144 stats_ctx->stat_get_reply = value;
1145 else if (strcmp (name, STAT_FIND_PEER_ANSWER) == 0)
1146 stats_ctx->stat_find_peer_answer = value;
1147 else if (strcmp (name, STAT_GET_RESPONSE_START) == 0)
1148 stats_ctx->stat_get_response_start = value;
1154 * Connect to statistics service for each peer and get the appropriate
1155 * dht statistics for safe keeping.
1158 log_dht_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1160 stats_map = GNUNET_CONTAINER_multihashmap_create (num_peers);
1161 fprintf (stderr, "Starting statistics logging\n");
1162 GNUNET_TESTING_get_statistics (pg, &stats_finished, &stats_handle, NULL);
1166 * Connect to all peers in the peer group and iterate over their
1170 capture_current_topology(void *cls,
1171 const struct GNUNET_SCHEDULER_TaskContext * tc)
1173 struct TopologyIteratorContext *topo_ctx = cls;
1174 dhtlog_handle->insert_topology (0);
1175 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Called capture_current_topology\n");
1176 GNUNET_TESTING_get_topology (pg, &log_topology_cb, topo_ctx);
1180 * Check if the get_handle is being used, if so stop the request. Either
1181 * way, schedule the end_badly_cont function which actually shuts down the
1185 end_badly(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failing test with error: `%s'!\n",
1190 struct TestPutContext *test_put = all_puts;
1191 struct TestGetContext *test_get = all_gets;
1193 while (test_put != NULL)
1195 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
1196 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
1197 if (test_put->dht_handle != NULL)
1198 GNUNET_DHT_disconnect (test_put->dht_handle);
1199 test_put = test_put->next;
1202 while (test_get != NULL)
1204 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
1205 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
1206 if (test_get->get_handle != NULL)
1207 GNUNET_DHT_get_stop (test_get->get_handle);
1208 if (test_get->dht_handle != NULL)
1209 GNUNET_DHT_disconnect (test_get->dht_handle);
1210 test_get = test_get->next;
1213 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
1215 if (dhtlog_handle != NULL)
1217 fprintf (stderr, "Update trial endtime\n");
1218 dhtlog_handle->update_trial (gets_completed);
1219 GNUNET_DHTLOG_disconnect (dhtlog_handle);
1220 dhtlog_handle = NULL;
1223 if (hostkey_meter != NULL)
1224 free_meter (hostkey_meter);
1225 if (peer_start_meter != NULL)
1226 free_meter (peer_start_meter);
1227 if (peer_connect_meter != NULL)
1228 free_meter (peer_connect_meter);
1229 if (put_meter != NULL)
1230 free_meter (put_meter);
1231 if (get_meter != NULL)
1232 free_meter (get_meter);
1238 * Forward declaration.
1241 do_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1244 * Forward declaration.
1247 do_get(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1250 * Iterator over hash map entries.
1252 * @param cls closure
1253 * @param key current key code
1254 * @param value value in the hash map
1255 * @return GNUNET_YES if we should continue to
1260 remove_peer_count(void *cls, const GNUNET_HashCode * key, void *value)
1262 struct PeerCount *peer_count = value;
1263 GNUNET_CONTAINER_heap_remove_node (peer_count->heap_node);
1264 GNUNET_free(peer_count);
1270 * Connect to all peers in the peer group and iterate over their
1274 count_new_peers(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1276 struct FindPeerContext *find_peer_context = cls;
1277 find_peer_context->previous_peers = find_peer_context->current_peers;
1278 find_peer_context->current_peers = 0;
1279 GNUNET_TESTING_get_topology (pg, find_peer_context->count_peers_cb,
1284 decrement_find_peers(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1286 struct TestFindPeer *test_find_peer = cls;
1287 GNUNET_assert(test_find_peer->find_peer_context->outstanding > 0);
1288 test_find_peer->find_peer_context->outstanding--;
1289 test_find_peer->find_peer_context->total--;
1290 if (0 == test_find_peer->find_peer_context->total)
1292 GNUNET_SCHEDULER_add_now (&count_new_peers,
1293 test_find_peer->find_peer_context);
1295 GNUNET_free(test_find_peer);
1299 * A find peer request has been sent to the server, now we will schedule a task
1300 * to wait the appropriate time to allow the request to go out and back.
1302 * @param cls closure - a TestFindPeer struct
1303 * @param tc context the task is being called with
1306 handle_find_peer_sent(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1308 struct TestFindPeer *test_find_peer = cls;
1310 GNUNET_DHT_disconnect (test_find_peer->dht_handle);
1311 GNUNET_SCHEDULER_add_delayed (
1312 GNUNET_TIME_relative_divide (find_peer_delay, 2),
1313 &decrement_find_peers, test_find_peer);
1317 send_find_peer_request(void *cls,
1318 const struct GNUNET_SCHEDULER_TaskContext * tc)
1320 struct TestFindPeer *test_find_peer = cls;
1322 if (test_find_peer->find_peer_context->outstanding
1323 > max_outstanding_find_peers)
1325 GNUNET_SCHEDULER_add_delayed (find_peer_offset, &send_find_peer_request,
1330 test_find_peer->find_peer_context->outstanding++;
1331 if (GNUNET_TIME_absolute_get_remaining (
1332 test_find_peer->find_peer_context->endtime).rel_value
1335 GNUNET_SCHEDULER_add_now (&decrement_find_peers, test_find_peer);
1339 test_find_peer->dht_handle = GNUNET_DHT_connect (test_find_peer->daemon->cfg,
1341 GNUNET_assert(test_find_peer->dht_handle != NULL);
1342 GNUNET_DHT_find_peers (test_find_peer->dht_handle, &handle_find_peer_sent,
1347 * Add a connection to the find_peer_context given. This may
1348 * be complete overkill, but allows us to choose the peers with
1349 * the least connections to initiate find peer requests from.
1352 add_new_connection(struct FindPeerContext *find_peer_context,
1353 const struct GNUNET_PeerIdentity *first,
1354 const struct GNUNET_PeerIdentity *second)
1356 struct PeerCount *first_count;
1357 struct PeerCount *second_count;
1359 if (GNUNET_CONTAINER_multihashmap_contains (find_peer_context->peer_hash,
1360 &first->hashPubKey))
1363 = GNUNET_CONTAINER_multihashmap_get (find_peer_context->peer_hash,
1364 &first->hashPubKey);
1365 GNUNET_assert(first_count != NULL);
1366 first_count->count++;
1367 GNUNET_CONTAINER_heap_update_cost (find_peer_context->peer_min_heap,
1368 first_count->heap_node,
1369 first_count->count);
1373 first_count = GNUNET_malloc(sizeof(struct PeerCount));
1374 first_count->count = 1;
1375 memcpy (&first_count->peer_id, first, sizeof(struct GNUNET_PeerIdentity));
1376 first_count->heap_node
1377 = GNUNET_CONTAINER_heap_insert (find_peer_context->peer_min_heap,
1378 first_count, first_count->count);
1379 GNUNET_CONTAINER_multihashmap_put (find_peer_context->peer_hash,
1380 &first->hashPubKey, first_count,
1381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1384 if (GNUNET_CONTAINER_multihashmap_contains (find_peer_context->peer_hash,
1385 &second->hashPubKey))
1388 = GNUNET_CONTAINER_multihashmap_get (find_peer_context->peer_hash,
1389 &second->hashPubKey);
1390 GNUNET_assert(second_count != NULL);
1391 second_count->count++;
1392 GNUNET_CONTAINER_heap_update_cost (find_peer_context->peer_min_heap,
1393 second_count->heap_node,
1394 second_count->count);
1398 second_count = GNUNET_malloc(sizeof(struct PeerCount));
1399 second_count->count = 1;
1400 memcpy (&second_count->peer_id, second,
1401 sizeof(struct GNUNET_PeerIdentity));
1402 second_count->heap_node
1403 = GNUNET_CONTAINER_heap_insert (find_peer_context->peer_min_heap,
1404 second_count, second_count->count);
1405 GNUNET_CONTAINER_multihashmap_put (find_peer_context->peer_hash,
1406 &second->hashPubKey, second_count,
1407 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1412 * Iterate over min heap of connections per peer. For any
1413 * peer that has 0 connections, attempt to connect them to
1416 * @param cls closure a struct FindPeerContext
1417 * @param node internal node of the heap
1418 * @param element value stored, a struct PeerCount
1419 * @param cost cost associated with the node
1420 * @return GNUNET_YES if we should continue to iterate,
1424 iterate_min_heap_peers(void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1425 void *element, GNUNET_CONTAINER_HeapCostType cost)
1427 struct FindPeerContext *find_peer_context = cls;
1428 struct PeerCount *peer_count = element;
1429 struct GNUNET_TESTING_Daemon *d1;
1430 struct GNUNET_TESTING_Daemon *d2;
1431 struct GNUNET_TIME_Relative timeout;
1434 d1 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
1435 GNUNET_assert(d1 != NULL);
1437 while ((d2 == d1) || (GNUNET_YES != GNUNET_TESTING_daemon_running (d2)))
1440 = GNUNET_TESTING_daemon_get (
1442 GNUNET_CRYPTO_random_u32 (
1443 GNUNET_CRYPTO_QUALITY_WEAK,
1445 GNUNET_assert(d2 != NULL);
1448 /** Just try to connect the peers, don't worry about callbacks, etc. **/
1449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1450 "Peer %s has 0 connections. Trying to connect to %s...\n",
1451 GNUNET_i2s (&peer_count->peer_id), d2->shortname);
1452 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1453 DEFAULT_CONNECT_TIMEOUT);
1454 if (GNUNET_TIME_relative_to_absolute (timeout).abs_value
1455 > find_peer_context->endtime.abs_value)
1458 = GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime);
1460 GNUNET_TESTING_daemons_connect (d1, d2, timeout,
1461 DEFAULT_RECONNECT_ATTEMPTS, GNUNET_YES,
1464 if (GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime).rel_value
1472 * Forward declaration.
1475 schedule_churn_find_peer_requests(void *cls,
1476 const struct GNUNET_SCHEDULER_TaskContext * tc);
1479 * Callback for iterating over all the peer connections of a peer group.
1480 * Used after we have churned on some peers to find which ones have zero
1481 * connections so we can make them issue find peer requests.
1484 count_peers_churn_cb(void *cls, const struct GNUNET_PeerIdentity *first,
1485 const struct GNUNET_PeerIdentity *second, const char *emsg)
1487 struct FindPeerContext *find_peer_context = cls;
1488 struct TopologyIteratorContext *topo_ctx;
1489 struct PeerCount *peer_count;
1491 if ((first != NULL) && (second != NULL))
1493 add_new_connection (find_peer_context, first, second);
1494 find_peer_context->current_peers++;
1498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1499 "Peer count finished (%u connections)\n",
1500 find_peer_context->current_peers);
1502 = GNUNET_CONTAINER_heap_peek (find_peer_context->peer_min_heap);
1503 GNUNET_assert(peer_count != NULL);
1504 /* WAIT. When peers are churned they will come back with their peers (at least in peerinfo), because the HOSTS file doesn't likely get removed. CRAP. */
1505 /* NO they won't, because we have disabled peerinfo writing to disk (remember?) so we WILL have to give them new connections */
1506 /* Best course of action: have DHT automatically try to add peers from peerinfo on startup. This way IF peerinfo writes to file
1507 * then some peers will end up connected.
1509 * Also, find any peers that have zero connections here and set up a task to choose at random another peer in the network to
1510 * connect to. Of course, if they are blacklisted from that peer they won't be able to connect, so we will have to keep trying
1511 * until they get a peer.
1513 /* However, they won't automatically be connected to any of their previous peers... How can we handle that? */
1514 /* So now we have choices: do we want them to come back with all their connections? Probably not, but it solves this mess. */
1516 /* Second problem, which is still a problem, is that a FIND_PEER request won't work when a peer has no connections */
1519 * Okay, so here's how this *should* work now.
1521 * 1. We check the min heap for any peers that have 0 connections.
1522 * a. If any are found, we iterate over the heap and just randomly
1523 * choose another peer and ask testing to please connect the two.
1524 * This takes care of the case that a peer just randomly joins the
1525 * network. However, if there are strict topology restrictions
1526 * (imagine a ring) choosing randomly most likely won't help.
1527 * We make sure the connection attempt doesn't take longer than
1528 * the total timeout, but don't care too much about the result.
1529 * b. After that, we still schedule the find peer requests (concurrently
1530 * with the connect attempts most likely). This handles the case
1531 * that the DHT iterates over peerinfo and just needs to try to send
1532 * a message to get connected. This should handle the case that the
1533 * topology is very strict.
1535 * 2. If all peers have > 0 connections, we still send find peer requests
1536 * as long as possible (until timeout is reached) to help out those
1537 * peers that were newly churned and need more connections. This is because
1538 * once all new peers have established a single connection, they won't be
1541 * 3. Once we reach the timeout, we can do no more. We must schedule the
1542 * next iteration of get requests regardless of connections that peers
1543 * may or may not have.
1545 * Caveat: it would be nice to get peers to take data offline with them and
1546 * come back with it (or not) based on the testing framework. The
1547 * same goes for remembering previous connections, but putting either
1548 * into the general testing churn options seems like overkill because
1549 * these are very specialized cases.
1552 GNUNET_ERROR_TYPE_WARNING,
1553 "Out of %u peers, fewest connections is %d\n",
1554 GNUNET_CONTAINER_heap_get_size (
1555 find_peer_context->peer_min_heap),
1557 if ((peer_count->count == 0)
1558 && (GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime).rel_value
1562 GNUNET_ERROR_TYPE_WARNING,
1563 "Found peer with no connections, will choose some peer(s) at random to connect to!\n");
1564 GNUNET_CONTAINER_heap_iterate (find_peer_context->peer_min_heap,
1565 &iterate_min_heap_peers,
1567 GNUNET_SCHEDULER_add_now (&schedule_churn_find_peer_requests,
1570 else if ((GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime).rel_value
1571 > 0) && (find_peer_context->last_sent != 0))
1573 GNUNET_SCHEDULER_add_now (&schedule_churn_find_peer_requests,
1578 GNUNET_CONTAINER_multihashmap_iterate (find_peer_context->peer_hash,
1581 GNUNET_CONTAINER_multihashmap_destroy (find_peer_context->peer_hash);
1582 GNUNET_CONTAINER_heap_destroy (find_peer_context->peer_min_heap);
1583 GNUNET_free(find_peer_context);
1585 GNUNET_ERROR_TYPE_WARNING,
1586 "Churn round %u of %llu finished, scheduling next GET round.\n",
1587 current_churn_round, churn_rounds);
1588 if (dhtlog_handle != NULL)
1590 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1591 topo_ctx->cont = &do_get;
1592 topo_ctx->cls = all_gets;
1593 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1594 topo_ctx->peers_seen
1595 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1597 = GNUNET_SCHEDULER_add_delayed (
1598 GNUNET_TIME_relative_add (
1599 GNUNET_TIME_relative_add (
1600 DEFAULT_GET_TIMEOUT,
1602 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1604 "from do gets (count_peers_churn_cb)");
1605 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
1610 = GNUNET_SCHEDULER_add_delayed (
1611 GNUNET_TIME_relative_add (
1612 GNUNET_TIME_relative_add (
1613 DEFAULT_GET_TIMEOUT,
1615 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1617 "from do gets (count_peers_churn_cb)");
1618 GNUNET_SCHEDULER_add_now (&do_get, all_gets);
1625 * Set up a single find peer request for each peer in the topology. Do this
1626 * until the settle time is over, limited by the number of outstanding requests
1627 * and the time allowed for each one!
1630 schedule_churn_find_peer_requests(void *cls,
1631 const struct GNUNET_SCHEDULER_TaskContext * tc)
1633 struct FindPeerContext *find_peer_ctx = cls;
1634 struct TestFindPeer *test_find_peer;
1635 struct PeerCount *peer_count;
1638 if (find_peer_ctx->previous_peers == 0) /* First time, go slowly */
1639 find_peer_ctx->total = 1;
1640 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers
1641 < MIN_FIND_PEER_CUTOFF)
1642 find_peer_ctx->total = find_peer_ctx->total / 2;
1643 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers
1644 > MAX_FIND_PEER_CUTOFF) /* Found LOTS of peers, still go slowly */
1645 find_peer_ctx->total = find_peer_ctx->last_sent - (find_peer_ctx->last_sent
1648 find_peer_ctx->total = find_peer_ctx->last_sent * 4;
1650 if (find_peer_ctx->total > max_outstanding_find_peers)
1651 find_peer_ctx->total = max_outstanding_find_peers;
1653 find_peer_ctx->last_sent = find_peer_ctx->total;
1654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1655 "Sending %u find peer messages (after churn)\n",
1656 find_peer_ctx->total);
1658 if (find_peer_ctx->total > 0)
1659 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay,
1660 find_peer_ctx->total);
1663 find_peer_ctx->previous_peers = find_peer_ctx->current_peers;
1664 find_peer_ctx->current_peers = 0;
1665 GNUNET_TESTING_get_topology (pg, &count_peers_churn_cb, find_peer_ctx);
1668 for (i = 0; i < find_peer_ctx->total; i++)
1670 test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
1671 /* If we have sent requests, choose peers with a low number of connections to send requests from */
1673 = GNUNET_CONTAINER_heap_remove_root (find_peer_ctx->peer_min_heap);
1674 GNUNET_assert(peer_count != NULL);
1675 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1676 "Sending find peer request from peer with %u connections\n",
1678 GNUNET_CONTAINER_multihashmap_remove (find_peer_ctx->peer_hash,
1679 &peer_count->peer_id.hashPubKey,
1681 test_find_peer->daemon
1682 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
1683 GNUNET_assert(test_find_peer->daemon != NULL);
1684 test_find_peer->find_peer_context = find_peer_ctx;
1685 GNUNET_SCHEDULER_add_delayed (
1686 GNUNET_TIME_relative_multiply (
1689 &send_find_peer_request, test_find_peer);
1692 if ((find_peer_ctx->peer_hash == NULL) && (find_peer_ctx->peer_min_heap
1695 find_peer_ctx->peer_hash
1696 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1697 find_peer_ctx->peer_min_heap
1698 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1702 GNUNET_CONTAINER_multihashmap_iterate (find_peer_ctx->peer_hash,
1703 &remove_peer_count, find_peer_ctx);
1704 GNUNET_CONTAINER_multihashmap_destroy (find_peer_ctx->peer_hash);
1705 find_peer_ctx->peer_hash
1706 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1709 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap_size(find_peer_ctx->peer_hash));
1710 GNUNET_assert(0 == GNUNET_CONTAINER_heap_get_size(find_peer_ctx->peer_min_heap));
1714 schedule_churn_get_topology(void *cls,
1715 const struct GNUNET_SCHEDULER_TaskContext * tc)
1717 struct FindPeerContext *find_peer_context = cls;
1718 GNUNET_TESTING_get_topology (pg, &count_peers_churn_cb, find_peer_context);
1722 * Called when churning of the topology has finished.
1724 * @param cls closure unused
1725 * @param emsg NULL on success, or a printable error on failure
1728 churn_complete(void *cls, const char *emsg)
1730 struct FindPeerContext *find_peer_context = cls;
1731 struct PeerCount *peer_count;
1733 struct GNUNET_TESTING_Daemon *temp_daemon;
1734 struct TopologyIteratorContext *topo_ctx;
1735 struct GNUNET_TIME_Relative calc_timeout;
1740 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1741 "Ending test, churning of peers failed with error `%s'", emsg);
1742 GNUNET_SCHEDULER_add_now (&end_badly, (void *) emsg);
1747 * If we switched any peers on, we have to somehow force connect the new peer to
1748 * SOME bootstrap peer in the network. First schedule a task to find all peers
1749 * with no connections, then choose a random peer for each and connect them.
1751 if (find_peer_context != NULL)
1754 GNUNET_ERROR_TYPE_WARNING,
1755 "We have churned on some peers, so we must schedule find peer requests for them!\n");
1757 for (i = 0; i < num_peers; i++)
1759 temp_daemon = GNUNET_TESTING_daemon_get (pg, i);
1760 if (GNUNET_YES == GNUNET_TESTING_daemon_running (temp_daemon))
1762 peer_count = GNUNET_malloc (sizeof(struct PeerCount));
1763 memcpy (&peer_count->peer_id, &temp_daemon->id,
1764 sizeof(struct GNUNET_PeerIdentity));
1765 GNUNET_assert(peer_count->count == 0);
1766 peer_count->heap_node
1767 = GNUNET_CONTAINER_heap_insert (
1768 find_peer_context->peer_min_heap,
1769 peer_count, peer_count->count);
1770 GNUNET_CONTAINER_multihashmap_put (find_peer_context->peer_hash,
1771 &temp_daemon->id.hashPubKey,
1773 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1778 GNUNET_ERROR_TYPE_WARNING,
1779 "Added %d peers to heap, total size %d\n",
1781 GNUNET_CONTAINER_heap_get_size (
1782 find_peer_context->peer_min_heap));
1783 GNUNET_SCHEDULER_add_delayed (DEFAULT_PEER_DISCONNECT_TIMEOUT,
1784 &schedule_churn_get_topology,
1790 GNUNET_ERROR_TYPE_WARNING,
1791 "Only churned off peers, no find peer requests, scheduling more gets (after allowing time for peers to disconnect properly!)...\n");
1792 if (dhtlog_handle != NULL)
1794 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1795 topo_ctx->cont = &do_get;
1796 topo_ctx->cls = all_gets;
1797 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1798 topo_ctx->peers_seen
1799 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1800 calc_timeout = GNUNET_TIME_relative_add (DEFAULT_GET_TIMEOUT,
1803 = GNUNET_TIME_relative_add (calc_timeout,
1804 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT);
1806 = GNUNET_TIME_relative_add (calc_timeout,
1807 DEFAULT_PEER_DISCONNECT_TIMEOUT);
1809 = GNUNET_SCHEDULER_add_delayed (calc_timeout, &end_badly,
1810 "from do gets (churn_complete)");
1811 GNUNET_SCHEDULER_add_delayed (DEFAULT_PEER_DISCONNECT_TIMEOUT,
1812 &capture_current_topology, topo_ctx);
1813 dhtlog_handle->insert_round (DHT_ROUND_GET, rounds_finished);
1817 calc_timeout = GNUNET_TIME_relative_add (DEFAULT_GET_TIMEOUT,
1820 = GNUNET_TIME_relative_add (calc_timeout,
1821 DEFAULT_PEER_DISCONNECT_TIMEOUT);
1823 = GNUNET_SCHEDULER_add_delayed (calc_timeout, &end_badly,
1824 "from do gets (churn_complete)");
1825 GNUNET_SCHEDULER_add_delayed (DEFAULT_PEER_DISCONNECT_TIMEOUT,
1832 * Decide how many peers to turn on or off in this round, make sure the
1833 * numbers actually make sense, then do so. This function sets in motion
1834 * churn, find peer requests for newly joined peers, and issuing get
1835 * requests once the new peers have done so.
1837 * @param cls closure (unused)
1838 * @param tc task context (unused)
1841 churn_peers(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1843 unsigned int count_running;
1844 unsigned int churn_up;
1845 unsigned int churn_down;
1846 struct GNUNET_TIME_Relative timeout;
1847 struct FindPeerContext *find_peer_context;
1849 churn_up = churn_down = 0;
1850 count_running = GNUNET_TESTING_daemons_running (pg);
1851 if (count_running > churn_array[current_churn_round])
1852 churn_down = count_running - churn_array[current_churn_round];
1853 else if (count_running < churn_array[current_churn_round])
1854 churn_up = churn_array[current_churn_round] - count_running;
1856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1857 "Not churning any peers, topology unchanged.\n");
1859 if (churn_up > num_peers - count_running)
1861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1862 "Churn file specified %u peers (up); only have %u!",
1863 churn_array[current_churn_round], num_peers);
1864 churn_up = num_peers - count_running;
1866 else if (churn_down > count_running)
1868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1869 "Churn file specified %u peers (down); only have %u!",
1870 churn_array[current_churn_round], count_running);
1871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1872 "This will leave NO peers running (mistake in churn configuration?)!");
1873 churn_down = count_running;
1875 //timeout = GNUNET_TIME_relative_multiply(seconds_per_peer_start, churn_up > 0 ? churn_up : churn_down);
1876 //timeout = GNUNET_TIME_relative_multiply (seconds_per_peer_start, churn_up > 0 ? churn_up : churn_down);
1877 timeout = GNUNET_TIME_relative_multiply (DEFAULT_TIMEOUT, 2); /* FIXME: Lack of intelligent choice here */
1878 find_peer_context = NULL;
1879 if (churn_up > 0) /* Only need to do find peer requests if we turned new peers on */
1881 find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
1882 find_peer_context->count_peers_cb = &count_peers_churn_cb;
1883 find_peer_context->previous_peers = 0;
1884 find_peer_context->current_peers = 0;
1885 find_peer_context->endtime = GNUNET_TIME_relative_to_absolute (timeout);
1886 find_peer_context->peer_hash
1887 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1888 find_peer_context->peer_min_heap
1889 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1892 GNUNET_ERROR_TYPE_WARNING,
1893 "churn_peers: want %u total, %u running, starting %u, stopping %u\n",
1894 churn_array[current_churn_round], count_running, churn_up,
1896 GNUNET_TESTING_daemons_churn (pg, churn_down, churn_up, timeout,
1897 &churn_complete, find_peer_context);
1898 current_churn_round++;
1902 * Task to release DHT handle associated with GET request.
1905 get_stop_finished(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1907 struct TestGetContext *test_get = cls;
1908 struct TopologyIteratorContext *topo_ctx;
1910 /* The dht_handle may be null if this get was scheduled from a down peer */
1911 if (test_get->dht_handle != NULL)
1913 GNUNET_DHT_disconnect (test_get->dht_handle);
1914 outstanding_gets--; /* GET is really finished */
1915 test_get->dht_handle = NULL;
1918 /* Reset the uid (which item to search for) and the daemon (which peer to search from) for later get request iterations */
1919 if (get_from_same == GNUNET_NO)
1921 test_get->uid = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1924 = GNUNET_TESTING_daemon_get (
1926 GNUNET_CRYPTO_random_u32 (
1927 GNUNET_CRYPTO_QUALITY_WEAK,
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n", gets_completed, gets_failed);
1934 update_meter (get_meter);
1935 if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0))
1939 "Canceling die task (get_stop_finished) %llu gets completed, %llu gets failed\n",
1940 gets_completed, gets_failed);
1941 if ((GNUNET_YES == dhtlog_minimal) && (NULL != dhtlog_handle))
1942 dhtlog_handle->insert_round_details (DHT_ROUND_GET, rounds_finished,
1943 num_gets, gets_completed);
1944 GNUNET_SCHEDULER_cancel (die_task);
1945 reset_meter (put_meter);
1946 reset_meter (get_meter);
1949 * 1) Testing is completely finished, call the topology iteration dealy and die
1950 * 2) Testing is not finished, churn the network and do gets again (current_churn_round < churn_rounds)
1951 * 3) Testing is not finished, reschedule all the PUTS *and* GETS again (num_rounds > 1)
1953 if (rounds_finished == total_rounds - 1) /* Everything is finished, end testing */
1955 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
1957 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1958 topo_ctx->cont = &log_dht_statistics;
1959 topo_ctx->peers_seen
1960 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1961 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
1964 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
1966 else if (current_churn_round < churns_per_round * (rounds_finished + 1)) /* Do next round of churn */
1969 GNUNET_ERROR_TYPE_WARNING,
1970 "Current churn round %u, real round %u, scheduling next round of churn.\n",
1971 current_churn_round, rounds_finished + 1);
1975 if (dhtlog_handle != NULL)
1976 dhtlog_handle->insert_round (DHT_ROUND_CHURN, rounds_finished);
1978 GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
1980 else if (rounds_finished < total_rounds - 1) /* Start a new complete round */
1985 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1986 "Round %u of %llu finished, scheduling next round.\n",
1987 rounds_finished, total_rounds);
1989 /** We reset the peer daemon for puts and gets on each disconnect, so all we need to do is start another round! */
1990 if (GNUNET_YES == in_dht_replication) /* Replication done in DHT, don't redo puts! */
1992 if (dhtlog_handle != NULL)
1993 dhtlog_handle->insert_round (DHT_ROUND_GET, rounds_finished);
1996 = GNUNET_SCHEDULER_add_delayed (
1997 GNUNET_TIME_relative_add (
1998 GNUNET_TIME_relative_add (
1999 GNUNET_TIME_relative_multiply (
2000 GNUNET_TIME_UNIT_SECONDS,
2003 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
2005 "from do gets (next round)");
2006 GNUNET_SCHEDULER_add_delayed (
2007 GNUNET_TIME_relative_multiply (
2008 GNUNET_TIME_UNIT_SECONDS,
2014 if (dhtlog_handle != NULL)
2015 dhtlog_handle->insert_round (DHT_ROUND_NORMAL, rounds_finished);
2017 = GNUNET_SCHEDULER_add_delayed (
2018 GNUNET_TIME_relative_add (
2019 GNUNET_TIME_relative_multiply (
2020 GNUNET_TIME_UNIT_SECONDS,
2022 GNUNET_TIME_relative_multiply (
2023 GNUNET_TIME_UNIT_SECONDS,
2026 &end_badly, "from do puts");
2027 GNUNET_SCHEDULER_add_delayed (
2028 GNUNET_TIME_relative_multiply (
2029 GNUNET_TIME_UNIT_SECONDS,
2038 * Task to release get handle.
2041 get_stop_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2043 struct TestGetContext *test_get = cls;
2045 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
2048 cumulative_successful_gets++;
2050 GNUNET_assert(test_get->get_handle != NULL);
2051 GNUNET_DHT_get_stop (test_get->get_handle);
2052 test_get->get_handle = NULL;
2053 test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
2054 GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
2058 * Iterator called if the GET request initiated returns a response.
2060 * @param cls closure
2061 * @param exp when will this value expire
2062 * @param key key of the result
2063 * @param get_path NULL-terminated array of pointers
2064 * to the peers on reverse GET path (or NULL if not recorded)
2065 * @param put_path NULL-terminated array of pointers
2066 * to the peers on the PUT path (or NULL if not recorded)
2067 * @param type type of the result
2068 * @param size number of bytes in data
2069 * @param data pointer to the result data
2072 get_result_iterator(void *cls, struct GNUNET_TIME_Absolute exp,
2073 const GNUNET_HashCode * key,
2074 const struct GNUNET_PeerIdentity * const *get_path,
2075 const struct GNUNET_PeerIdentity * const *put_path,
2076 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2078 struct TestGetContext *test_get = cls;
2080 if (test_get->succeeded == GNUNET_YES)
2081 return; /* Get has already been successful, probably ending now */
2083 if (0 != memcmp (&known_keys[test_get->uid], key, sizeof(GNUNET_HashCode))) /* || (0 != memcmp(original_data, data, sizeof(original_data))))*/
2086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2087 "Key or data is not the same as was inserted!\n");
2092 test_get->succeeded = GNUNET_YES;
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
2097 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
2098 GNUNET_SCHEDULER_add_continuation (&get_stop_task, test_get,
2099 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
2103 * Set up some data, and call API PUT function
2106 do_get(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2108 struct TestGetContext *test_get = cls;
2112 GNUNET_SCHEDULER_cancel (die_task);
2113 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
2116 if (test_get == NULL)
2117 return; /* End of the list */
2119 /* Set this here in case we are re-running gets */
2120 test_get->succeeded = GNUNET_NO;
2122 if (GNUNET_YES != GNUNET_TESTING_daemon_running (test_get->daemon)) /* If the peer has been churned off, don't try issuing request from it! */
2124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2125 "Peer we should issue get request from is down, skipping.\n");
2127 GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
2128 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
2132 /* Check if more gets are outstanding than should be */
2133 if (outstanding_gets > max_outstanding_gets)
2135 GNUNET_SCHEDULER_add_delayed (
2136 GNUNET_TIME_relative_multiply (
2137 GNUNET_TIME_UNIT_MILLISECONDS,
2143 /* Connect to the first peer's DHT */
2144 test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
2145 GNUNET_assert(test_get->dht_handle != NULL);
2148 cumulative_num_gets++;
2149 /* Insert the data at the first peer */
2150 test_get->get_handle = GNUNET_DHT_get_start (test_get->dht_handle, get_delay,
2151 GNUNET_BLOCK_TYPE_TEST,
2152 &known_keys[test_get->uid],
2154 GNUNET_DHT_RO_NONE, NULL, 0,
2155 NULL, 0, &get_result_iterator,
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
2161 test_get->daemon->shortname);
2163 test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed (get_timeout,
2167 /* Schedule the next request in the linked list of get requests */
2168 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
2172 * Called when the PUT request has been transmitted to the DHT service.
2173 * Schedule the GET request for some time in the future.
2176 put_finished(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2178 struct TestPutContext *test_put = cls;
2179 struct TopologyIteratorContext *topo_ctx;
2183 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
2184 fprintf (stderr, "PUT Request failed!\n");
2186 /* Reset the daemon (which peer to insert at) for later put request iterations */
2187 if (replicate_same == GNUNET_NO)
2189 = GNUNET_TESTING_daemon_get (
2191 GNUNET_CRYPTO_random_u32 (
2192 GNUNET_CRYPTO_QUALITY_WEAK,
2195 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
2196 test_put->disconnect_task = GNUNET_SCHEDULER_add_now (&put_disconnect_task,
2198 if (GNUNET_YES == update_meter (put_meter))
2200 GNUNET_assert(outstanding_puts == 0);
2201 GNUNET_SCHEDULER_cancel (die_task);
2202 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
2204 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2205 topo_ctx->cont = &do_get;
2206 topo_ctx->cls = all_gets;
2207 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
2208 topo_ctx->peers_seen
2209 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2211 = GNUNET_SCHEDULER_add_delayed (
2212 GNUNET_TIME_relative_add (
2213 GNUNET_TIME_relative_add (
2214 DEFAULT_GET_TIMEOUT,
2216 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
2218 "from do gets (put finished)");
2219 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
2223 fprintf (stderr, "Scheduling die task (put finished)\n");
2225 = GNUNET_SCHEDULER_add_delayed (
2226 GNUNET_TIME_relative_add (
2227 DEFAULT_GET_TIMEOUT,
2230 "from do gets (put finished)");
2231 GNUNET_SCHEDULER_add_delayed (DEFAULT_GET_TIMEOUT, &do_get, all_gets);
2238 * Set up some data, and call API PUT function
2241 do_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2243 struct TestPutContext *test_put = cls;
2244 char data[test_data_size]; /* Made up data to store */
2248 if (test_put == NULL)
2249 return; /* End of list */
2251 if (GNUNET_YES != GNUNET_TESTING_daemon_running (test_put->daemon)) /* If the peer has been churned off, don't try issuing request from it! */
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2254 "Peer we should issue put request at is down, skipping.\n");
2255 update_meter (put_meter);
2256 GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
2260 for (i = 0; i < sizeof(data); i++)
2262 memset (&data[i], GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2266 if (outstanding_puts > max_outstanding_puts)
2268 GNUNET_SCHEDULER_add_delayed (
2269 GNUNET_TIME_relative_multiply (
2270 GNUNET_TIME_UNIT_MILLISECONDS,
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
2279 test_put->daemon->shortname);
2281 test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
2283 GNUNET_assert(test_put->dht_handle != NULL);
2285 GNUNET_DHT_put (test_put->dht_handle, &known_keys[test_put->uid],
2286 put_replication, GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_TEST,
2287 sizeof(data), data, GNUNET_TIME_UNIT_FOREVER_ABS, put_delay,
2288 &put_finished, test_put);
2289 test_put->disconnect_task
2290 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
2291 &put_disconnect_task, test_put);
2292 rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
2293 GNUNET_SCHEDULER_add_delayed (
2294 GNUNET_TIME_relative_multiply (
2295 GNUNET_TIME_UNIT_SECONDS,
2301 schedule_find_peer_requests(void *cls,
2302 const struct GNUNET_SCHEDULER_TaskContext * tc);
2305 setup_malicious_peers(void *cls,
2306 const struct GNUNET_SCHEDULER_TaskContext * tc);
2309 * Given a number of total peers and a bucket size, estimate the number of
2310 * connections in a perfect kademlia topology.
2313 connection_estimate(unsigned int peer_count, unsigned int bucket_size)
2316 unsigned int filled;
2320 while (i >= bucket_size)
2325 filled++; /* Add one filled bucket to account for one "half full" and some miscellaneous */
2326 return filled * bucket_size * peer_count;
2331 * Callback for iterating over all the peer connections of a peer group.
2334 count_peers_cb(void *cls, const struct GNUNET_PeerIdentity *first,
2335 const struct GNUNET_PeerIdentity *second, const char *emsg)
2337 struct FindPeerContext *find_peer_context = cls;
2338 if ((first != NULL) && (second != NULL))
2340 add_new_connection (find_peer_context, first, second);
2341 find_peer_context->current_peers++;
2346 GNUNET_ERROR_TYPE_WARNING,
2347 "Peer count finished (%u connections), %u new peers, connection estimate %u (target %u)\n",
2348 find_peer_context->current_peers,
2349 find_peer_context->current_peers
2350 - find_peer_context->previous_peers,
2351 connection_estimate (num_peers, DEFAULT_BUCKET_SIZE),
2352 target_total_connections);
2354 if ((find_peer_context->last_sent < 8)
2355 || ((find_peer_context->current_peers < 2
2356 * connection_estimate (num_peers, DEFAULT_BUCKET_SIZE))
2357 && (GNUNET_TIME_absolute_get_remaining (
2358 find_peer_context->endtime).rel_value
2359 > 0) && (find_peer_context->current_peers
2360 < target_total_connections)))
2362 GNUNET_SCHEDULER_add_now (&schedule_find_peer_requests,
2367 GNUNET_CONTAINER_multihashmap_iterate (find_peer_context->peer_hash,
2370 GNUNET_CONTAINER_multihashmap_destroy (find_peer_context->peer_hash);
2371 GNUNET_CONTAINER_heap_destroy (find_peer_context->peer_min_heap);
2372 GNUNET_free(find_peer_context);
2373 fprintf (stderr, "Not sending any more find peer requests.\n");
2376 if (GNUNET_YES == malicious_after_settle)
2378 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "calling setup_malicious_peers\n");
2379 GNUNET_SCHEDULER_add_now(&setup_malicious_peers, NULL);
2387 * Set up a single find peer request for each peer in the topology. Do this
2388 * until the settle time is over, limited by the number of outstanding requests
2389 * and the time allowed for each one!
2392 schedule_find_peer_requests(void *cls,
2393 const struct GNUNET_SCHEDULER_TaskContext * tc)
2395 struct FindPeerContext *find_peer_ctx = cls;
2396 struct TestFindPeer *test_find_peer;
2397 struct PeerCount *peer_count;
2401 if (find_peer_ctx->previous_peers == 0) /* First time, go slowly */
2402 find_peer_ctx->total = 1;
2403 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers
2404 > MAX_FIND_PEER_CUTOFF) /* Found LOTS of peers, still go slowly */
2405 find_peer_ctx->total = find_peer_ctx->last_sent - (find_peer_ctx->last_sent
2408 find_peer_ctx->total = find_peer_ctx->last_sent * 2;
2410 if (find_peer_ctx->total > max_outstanding_find_peers)
2411 find_peer_ctx->total = max_outstanding_find_peers;
2413 if (find_peer_ctx->total > num_peers) /* Don't try to send more messages than we have peers! */
2414 find_peer_ctx->total = num_peers;
2416 find_peer_ctx->last_sent = find_peer_ctx->total;
2417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2418 "Sending %u find peer messages (goal at least %u connections)\n",
2419 find_peer_ctx->total, target_total_connections);
2421 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay,
2422 find_peer_ctx->total);
2423 for (i = 0; i < find_peer_ctx->total; i++)
2425 test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
2426 /* If we haven't sent any requests yet, choose random peers */
2427 /* Also choose random in _half_ of all cases, so we don't
2428 * get stuck choosing topologically restricted peers with
2429 * few connections that will never be able to find any new
2431 if ((find_peer_ctx->previous_peers == 0) || (i % 2 == 0))
2434 * Attempt to spread find peer requests across even sections of the peer address
2435 * space. Choose basically 1 peer in every num_peers / max_outstanding_requests
2436 * each time, then offset it by a randomish value.
2438 * For instance, if num_peers is 100 and max_outstanding is 10, first chosen peer
2439 * will be between 0 - 10, second between 10 - 20, etc.
2441 random = (num_peers / find_peer_ctx->total) * i;
2443 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2444 (num_peers / find_peer_ctx->total));
2445 if (random >= num_peers)
2447 random = random - num_peers;
2450 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2452 test_find_peer->daemon = GNUNET_TESTING_daemon_get (pg, random);
2454 else /* If we have sent requests, choose peers with a low number of connections to send requests from */
2457 = GNUNET_CONTAINER_heap_remove_root (find_peer_ctx->peer_min_heap);
2458 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(find_peer_ctx->peer_hash, &peer_count->peer_id.hashPubKey, peer_count));
2459 test_find_peer->daemon
2460 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
2461 GNUNET_assert(test_find_peer->daemon != NULL);
2464 test_find_peer->find_peer_context = find_peer_ctx;
2465 GNUNET_SCHEDULER_add_delayed (
2466 GNUNET_TIME_relative_multiply (
2469 &send_find_peer_request, test_find_peer);
2472 if ((find_peer_ctx->peer_hash == NULL) && (find_peer_ctx->peer_min_heap
2475 find_peer_ctx->peer_hash
2476 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2477 find_peer_ctx->peer_min_heap
2478 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2482 GNUNET_CONTAINER_multihashmap_iterate (find_peer_ctx->peer_hash,
2483 &remove_peer_count, find_peer_ctx);
2484 GNUNET_CONTAINER_multihashmap_destroy (find_peer_ctx->peer_hash);
2485 find_peer_ctx->peer_hash
2486 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2489 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap_size(find_peer_ctx->peer_hash));
2490 GNUNET_assert(0 == GNUNET_CONTAINER_heap_get_size(find_peer_ctx->peer_min_heap));
2495 * Convert unique ID to hash code.
2497 * @param uid unique ID to convert
2498 * @param hash set to uid (extended with zeros)
2501 hash_from_uid(uint32_t uid, GNUNET_HashCode *hash)
2503 memset (hash, 0, sizeof(GNUNET_HashCode));
2504 *((uint32_t *) hash) = uid;
2508 * Set up all of the put and get operations we want to do
2509 * in the current round. Allocate data structure for each,
2510 * add to list, then schedule the actual PUT operations.
2513 setup_puts_and_gets(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2516 struct TestPutContext *test_put;
2517 struct TestGetContext *test_get;
2519 GNUNET_HashCode uid_hash;
2522 int remember[num_puts][num_peers];
2523 memset(&remember, 0, sizeof(int) * num_puts * num_peers);
2525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "in setup_puts_and_gets\n");
2526 known_keys = GNUNET_malloc(sizeof(GNUNET_HashCode) * num_puts);
2527 for (i = 0; i < num_puts; i++)
2529 test_put = GNUNET_malloc(sizeof(struct TestPutContext));
2531 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
2533 /* Set first X bits to match the chosen sybil location if we want to do the sybil attack! */
2534 if (GNUNET_YES == malicious_sybil)
2536 memcpy (&known_keys[i], &sybil_target, sizeof(GNUNET_HashCode) / 2);
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2538 "Distance between sybil location and key is %d\n",
2539 GNUNET_CRYPTO_hash_matching_bits (&known_keys[i],
2542 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2544 test_put->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2545 /* Don't start PUTs at malicious peers! */
2546 if (malicious_bloom != NULL)
2549 hash_from_uid (temp_peer, &uid_hash);
2551 == GNUNET_CONTAINER_bloomfilter_test (malicious_bloom, &uid_hash))
2552 && (count < num_peers))
2554 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2556 hash_from_uid (temp_peer, &uid_hash);
2557 test_put->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2560 if (count == num_peers)
2561 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2562 "Couldn't find peer not in malicious bloom to select!\n");
2565 test_put->next = all_puts;
2566 all_puts = test_put;
2569 for (i = 0; i < num_gets; i++)
2571 test_get = GNUNET_malloc(sizeof(struct TestGetContext));
2572 test_get->uid = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2575 while (remember[test_get->uid][temp_daemon] == 1)
2576 temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2577 remember[test_get->uid][temp_daemon] = 1;
2579 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2581 test_get->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2582 /* Don't start GETs at malicious peers! */
2583 if (malicious_bloom != NULL)
2585 hash_from_uid (temp_peer, &uid_hash);
2588 == GNUNET_CONTAINER_bloomfilter_test (malicious_bloom, &uid_hash))
2589 && (count < num_peers))
2591 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2593 hash_from_uid (temp_peer, &uid_hash);
2594 test_get->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2597 if (count == num_peers)
2598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2599 "Couldn't find peer not in malicious bloom to select!\n");
2601 test_get->next = all_gets;
2602 all_gets = test_get;
2605 /*GNUNET_SCHEDULER_cancel (die_task);*/
2607 = GNUNET_SCHEDULER_add_delayed (
2608 GNUNET_TIME_relative_multiply (
2609 GNUNET_TIME_UNIT_SECONDS,
2612 &end_badly, "from do puts");
2613 GNUNET_SCHEDULER_add_now (&do_put, all_puts);
2618 * Set up some all of the put and get operations we want
2619 * to do. Allocate data structure for each, add to list,
2620 * then call actual insert functions.
2623 continue_puts_and_gets(void *cls,
2624 const struct GNUNET_SCHEDULER_TaskContext * tc)
2628 struct TopologyIteratorContext *topo_ctx;
2629 struct FindPeerContext *find_peer_context;
2630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "In continue_puts_and_gets\n");
2631 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
2633 if (settle_time >= 180 * 2)
2634 max = (settle_time / 180) - 2;
2637 for (i = 1; i < max; i++)
2639 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2640 topo_ctx->current_iteration = i;
2641 topo_ctx->total_iterations = max;
2642 topo_ctx->peers_seen
2643 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2644 //fprintf(stderr, "scheduled topology iteration in %d minutes\n", i);
2645 GNUNET_SCHEDULER_add_delayed (
2646 GNUNET_TIME_relative_multiply (
2647 GNUNET_TIME_UNIT_MINUTES,
2649 &capture_current_topology, topo_ctx);
2651 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2652 topo_ctx->cont = &setup_puts_and_gets;
2653 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create (num_peers);
2654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2655 "setting setup_puts_and_gets for %d seconds in the future\n",
2657 GNUNET_SCHEDULER_add_delayed (
2658 GNUNET_TIME_relative_multiply (
2659 GNUNET_TIME_UNIT_SECONDS,
2662 &capture_current_topology, topo_ctx);
2665 GNUNET_SCHEDULER_add_delayed (
2666 GNUNET_TIME_relative_multiply (
2667 GNUNET_TIME_UNIT_SECONDS,
2670 &setup_puts_and_gets, NULL);
2672 if (dhtlog_handle != NULL)
2673 dhtlog_handle->insert_round (DHT_ROUND_NORMAL, rounds_finished);
2676 if ((GNUNET_YES != malicious_after_settle) || (settle_time == 0))
2678 GNUNET_SCHEDULER_add_now(&setup_malicious_peers, NULL);
2682 if ((GNUNET_YES == do_find_peer) && (settle_time > 0))
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 "Scheduling find peer requests during \"settle\" time.\n");
2686 find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
2687 find_peer_context->count_peers_cb = &count_peers_cb;
2688 find_peer_context->endtime
2689 = GNUNET_TIME_relative_to_absolute (
2690 GNUNET_TIME_relative_multiply (
2691 GNUNET_TIME_UNIT_SECONDS,
2693 GNUNET_SCHEDULER_add_now (&schedule_find_peer_requests, find_peer_context);
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Assuming automatic DHT find peer requests.\n");
2703 * Task to release DHT handles
2706 malicious_disconnect_task(void *cls,
2707 const struct GNUNET_SCHEDULER_TaskContext * tc)
2709 struct MaliciousContext *ctx = cls;
2710 outstanding_malicious--;
2711 malicious_completed++;
2712 ctx->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
2713 GNUNET_DHT_disconnect (ctx->dht_handle);
2714 ctx->dht_handle = NULL;
2717 if (malicious_completed == malicious_getters + malicious_putters
2718 + malicious_droppers)
2720 fprintf (stderr, "Finished setting all malicious peers up!\n");
2725 * Task to release DHT handles
2728 malicious_done_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2730 struct MaliciousContext *ctx = cls;
2731 GNUNET_SCHEDULER_cancel (ctx->disconnect_task);
2732 GNUNET_SCHEDULER_add_now (&malicious_disconnect_task, ctx);
2736 * Set up some data, and call API PUT function
2739 set_malicious(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2741 struct MaliciousContext *ctx = cls;
2743 if (outstanding_malicious > DEFAULT_MAX_OUTSTANDING_GETS)
2745 GNUNET_SCHEDULER_add_delayed (
2746 GNUNET_TIME_relative_multiply (
2747 GNUNET_TIME_UNIT_MILLISECONDS,
2749 &set_malicious, ctx);
2753 if (ctx->dht_handle == NULL)
2755 ctx->dht_handle = GNUNET_DHT_connect (ctx->daemon->cfg, 1);
2756 outstanding_malicious++;
2759 GNUNET_assert(ctx->dht_handle != NULL);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting peer %s malicious type %d\n",
2763 ctx->daemon->shortname, ctx->malicious_type);
2766 switch (ctx->malicious_type)
2768 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
2769 GNUNET_DHT_set_malicious_getter (ctx->dht_handle, malicious_get_frequency, &malicious_done_task, ctx);
2771 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
2772 GNUNET_DHT_set_malicious_putter (ctx->dht_handle, malicious_put_frequency, &malicious_done_task, ctx);
2774 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
2775 GNUNET_DHT_set_malicious_dropper (ctx->dht_handle, &malicious_done_task, ctx);
2781 ctx->disconnect_task
2782 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2783 &malicious_disconnect_task, ctx);
2788 * Choose the next peer from the peer group to set as malicious.
2789 * If we are doing a sybil attack, find the nearest peer to the
2790 * sybil location that has not already been set malicious. Otherwise
2791 * just choose a random not already chosen peer.
2793 * @param pg the peer group
2794 * @param bloom the bloomfilter which contains all peer already
2795 * chosen to be malicious
2798 choose_next_malicious (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_CONTAINER_BloomFilter *bloom)
2805 struct GNUNET_TESTING_Daemon *temp_daemon;
2806 GNUNET_HashCode uid_hash;
2810 GNUNET_assert (bloom != NULL);
2812 if (GNUNET_YES == malicious_sybil)
2814 for (i = 0; i < num_peers; i++)
2816 temp_daemon = GNUNET_TESTING_daemon_get(pg, i);
2817 hash_from_uid(i, &uid_hash);
2818 /* Check if this peer matches the bloomfilter */
2819 if ((GNUNET_NO == GNUNET_TESTING_daemon_running(temp_daemon)) || (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &uid_hash)))
2822 bits_match = GNUNET_CRYPTO_hash_matching_bits (&temp_daemon->id.hashPubKey, &sybil_target);
2823 if (bits_match >= curr_distance)
2825 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Found nearer peer %s to %s, old matching bits %d, new %d\n", GNUNET_i2s(&temp_daemon->id), GNUNET_h2s(&sybil_target), curr_distance, bits_match);
2827 curr_distance = bits_match;
2833 nearest = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2834 hash_from_uid(nearest, &uid_hash);
2836 while ((GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &uid_hash)) && (count < num_peers))
2838 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %d already in bloom (tried %d times)\n", nearest, count);
2839 nearest = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2840 hash_from_uid(nearest, &uid_hash);
2843 if (count == num_peers)
2844 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Tried %d times to find a peer, selecting %d at random!!\n", count, nearest);
2851 * Select randomly from set of known peers,
2852 * set the desired number of peers to the
2853 * proper malicious types.
2856 setup_malicious_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2858 struct MaliciousContext *ctx;
2860 uint32_t temp_daemon;
2861 GNUNET_HashCode uid_hash;
2863 for (i = 0; i < malicious_getters; i++)
2865 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2866 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2867 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2868 hash_from_uid(temp_daemon, &uid_hash);
2869 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2870 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET;
2871 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2875 for (i = 0; i < malicious_putters; i++)
2877 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2878 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2879 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2880 hash_from_uid(temp_daemon, &uid_hash);
2881 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2882 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT;
2883 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2887 for (i = 0; i < malicious_droppers; i++)
2889 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2890 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2891 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2892 hash_from_uid(temp_daemon, &uid_hash);
2893 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2894 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP;
2895 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2901 /* Forward declaration */
2903 topology_callback (void *cls,
2904 const struct GNUNET_PeerIdentity *first,
2905 const struct GNUNET_PeerIdentity *second,
2907 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2908 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2909 struct GNUNET_TESTING_Daemon *first_daemon,
2910 struct GNUNET_TESTING_Daemon *second_daemon,
2914 * Retry connecting two specific peers until they connect,
2915 * at a specific interval. These two peers previously failed
2916 * to connect, and we hope they continue to so that we can
2917 * debug the reason they are having issues.
2920 repeat_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2923 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Repeating connect attempt between %s and %s.\n", repeat_connect_peer1->shortname, repeat_connect_peer2->shortname);
2924 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer 1 configuration `%s'\n", repeat_connect_peer1->cfgfile);
2925 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer 2 configuration `%s'\n", repeat_connect_peer2->cfgfile);
2927 repeat_connect_task = GNUNET_SCHEDULER_NO_TASK;
2928 GNUNET_TESTING_daemons_connect(repeat_connect_peer1,
2929 repeat_connect_peer2,
2930 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
2931 2, &topology_callback, NULL);
2936 * This function is called whenever a connection attempt is finished between two of
2937 * the started peers (started with GNUNET_TESTING_daemons_start). The total
2938 * number of times this function is called should equal the number returned
2939 * from the GNUNET_TESTING_connect_topology call.
2941 * The emsg variable is NULL on success (peers connected), and non-NULL on
2942 * failure (peers failed to connect).
2945 topology_callback(void *cls, const struct GNUNET_PeerIdentity *first,
2946 const struct GNUNET_PeerIdentity *second, uint32_t distance,
2947 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2948 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2949 struct GNUNET_TESTING_Daemon *first_daemon,
2950 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
2952 struct TopologyIteratorContext *topo_ctx;
2953 unsigned long long duration;
2954 unsigned long long total_duration;
2955 unsigned int new_connections;
2956 unsigned int new_failed_connections;
2957 double conns_per_sec_recent;
2958 double conns_per_sec_total;
2959 double failed_conns_per_sec_recent;
2960 double failed_conns_per_sec_total;
2961 char *temp_conn_string;
2962 char *temp_conn_failed_string;
2966 if (repeat_connect_mode == GNUNET_YES)
2968 if ((first_daemon == repeat_connect_peer1) &&
2969 (second_daemon == repeat_connect_peer2))
2971 if (emsg != NULL) /* Peers failed to connect again! */
2973 GNUNET_assert(repeat_connect_task == GNUNET_SCHEDULER_NO_TASK);
2974 repeat_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &repeat_connect, NULL);
2977 else /* Repeat peers actually connected! */
2979 if (repeat_connect_task != GNUNET_SCHEDULER_NO_TASK)
2980 GNUNET_SCHEDULER_cancel(repeat_connect_task);
2981 repeat_connect_peer1 = NULL;
2982 repeat_connect_peer2 = NULL;
2983 repeat_connect_mode = GNUNET_NO;
2984 GNUNET_TESTING_resume_connections(pg);
2985 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Resuming normal connection mode, debug connection was successful!\n");
2991 if (GNUNET_TIME_absolute_get_difference (connect_last_time,
2992 GNUNET_TIME_absolute_get ()).rel_value
2993 > GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2994 CONN_UPDATE_DURATION).rel_value)
2996 /* Get number of new connections */
2997 new_connections = total_connections - previous_connections;
2999 /* Get number of new FAILED connections */
3000 new_failed_connections = failed_connections - previous_failed_connections;
3002 /* Get duration in seconds */
3004 = GNUNET_TIME_absolute_get_difference (connect_last_time,
3005 GNUNET_TIME_absolute_get ()).rel_value
3008 = GNUNET_TIME_absolute_get_difference (connect_start_time,
3009 GNUNET_TIME_absolute_get ()).rel_value
3012 failed_conns_per_sec_recent = (double) new_failed_connections / duration;
3013 failed_conns_per_sec_total = (double) failed_connections / total_duration;
3014 conns_per_sec_recent = (double) new_connections / duration;
3015 conns_per_sec_total = (double) total_connections / total_duration;
3017 GNUNET_ERROR_TYPE_WARNING,
3018 "Recent: %.2f/s, Total: %.2f/s, Recent failed: %.2f/s, total failed %.2f/s\n",
3019 conns_per_sec_recent, CONN_UPDATE_DURATION,
3020 conns_per_sec_total, failed_conns_per_sec_recent,
3021 failed_conns_per_sec_total);
3022 connect_last_time = GNUNET_TIME_absolute_get ();
3023 previous_connections = total_connections;
3024 previous_failed_connections = failed_connections;
3025 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3026 "have %u total_connections, %u failed\n", total_connections,
3027 failed_connections);
3029 /* These conditions likely mean we've entered the death spiral of doom */
3030 if ((total_connections > 20000) &&
3031 (conns_per_sec_recent < 5.0) &&
3032 (conns_per_sec_total > 10.0) &&
3034 (repeat_connect_mode == GNUNET_NO))
3036 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Entering repeat connection attempt mode!\n");
3037 repeat_connect_peer1 = first_daemon;
3038 repeat_connect_peer2 = second_daemon;
3039 repeat_connect_mode = GNUNET_YES;
3040 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping NEW connections from being scheduled!\n");
3041 GNUNET_TESTING_stop_connections(pg);
3042 repeat_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &repeat_connect, NULL);
3049 total_connections++;
3051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
3052 first_daemon->shortname,
3053 second_daemon->shortname,
3059 failed_connections++;
3061 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to connect peer %s to peer %s with error :\n%s\n",
3062 first_daemon->shortname,
3063 second_daemon->shortname, emsg);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
3066 first_daemon->shortname,
3067 second_daemon->shortname, emsg);
3072 if ((repeat_connect_mode == GNUNET_YES) )
3076 GNUNET_assert(peer_connect_meter != NULL);
3077 if (GNUNET_YES == update_meter (peer_connect_meter))
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3081 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
3084 if (failed_connections > 0)
3085 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3086 "While connecting, had %u failed connections.\n",
3087 failed_connections);
3088 if (dhtlog_handle != NULL)
3090 dhtlog_handle->update_connections (total_connections);
3091 dhtlog_handle->insert_topology (expected_connections);
3095 = GNUNET_TIME_absolute_get_difference (connect_start_time,
3096 GNUNET_TIME_absolute_get ()).rel_value
3098 failed_conns_per_sec_total = (double) failed_connections / total_duration;
3099 conns_per_sec_total = (double) total_connections / total_duration;
3100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3101 "Overall connection info --- Total: %u, Total Failed %u/s\n",
3102 total_connections, failed_connections);
3104 GNUNET_ERROR_TYPE_WARNING,
3105 "Overall connection info --- Total: %.2f/s, Total Failed %.2f/s\n",
3106 conns_per_sec_total, failed_conns_per_sec_total);
3108 GNUNET_asprintf (&temp_conn_string,
3109 "DHT Profiler Connection (trial %d)",
3111 GNUNET_asprintf (&temp_conn_failed_string,
3112 "DHT Profiler Connection failed (trial %d)",
3114 GNUNET_asprintf (&revision_str, "%llu", revision);
3116 if (GNUNET_YES == insert_gauger_data)
3117 GAUGER_ID("DHT_TESTING", temp_conn_string, conns_per_sec_total, "conns/s", revision_str);
3118 if (GNUNET_YES == insert_gauger_data)
3119 GAUGER_ID("DHT_TESTING", temp_conn_failed_string, failed_conns_per_sec_total, "failed_conns", revision_str);
3121 GNUNET_free(temp_conn_string);
3122 GNUNET_free(temp_conn_failed_string);
3123 GNUNET_asprintf (&temp_conn_string,
3124 "DHT Profiler Total Connections (trial %d)",
3127 &temp_conn_failed_string,
3128 "DHT Profiler Total Connections failed (trial %d)",
3130 if (GNUNET_YES == insert_gauger_data)
3131 GAUGER_ID("DHT_TESTING", temp_conn_string, total_connections, "conns", revision_str);
3132 if (GNUNET_YES == insert_gauger_data)
3133 GAUGER_ID("DHT_TESTING", temp_conn_failed_string, failed_connections, "failed conns", revision_str);
3134 GNUNET_free(temp_conn_string);
3135 GNUNET_free(temp_conn_failed_string);
3136 GNUNET_free(revision_str);
3138 GNUNET_SCHEDULER_cancel (die_task);
3140 if ((GNUNET_YES == dhtlog_minimal) && (NULL != dhtlog_handle))
3142 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
3143 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3144 "Setting continue gets and puts as topo_cont\n");
3145 topo_ctx->cont = &continue_puts_and_gets;
3146 topo_ctx->peers_seen
3147 = GNUNET_CONTAINER_multihashmap_create (num_peers);
3148 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
3153 GNUNET_ERROR_TYPE_WARNING,
3154 "For some reason, NOT scheduling final topology capture (settle_time %d, dhtlog_handle %s)!\n",
3155 settle_time, dhtlog_handle);
3156 GNUNET_SCHEDULER_add_now (&continue_puts_and_gets, NULL);
3159 else if (total_connections + failed_connections == expected_connections)
3161 GNUNET_SCHEDULER_cancel (die_task);
3163 = GNUNET_SCHEDULER_add_now (&end_badly,
3164 "from topology_callback (too many failed connections)");
3169 peers_started_callback(void *cls, const struct GNUNET_PeerIdentity *id,
3170 const struct GNUNET_CONFIGURATION_Handle *cfg,
3171 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3176 "Failed to start daemon with error: `%s'\n", emsg);
3179 GNUNET_assert (id != NULL);
3182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
3183 (num_peers - peers_left) + 1, num_peers);
3188 if (GNUNET_YES == update_meter (peer_start_meter))
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 "All %d daemons started, now connecting peers!\n",
3195 GNUNET_SCHEDULER_cancel (die_task);
3197 expected_connections = UINT_MAX;
3198 if ((pg != NULL) && (peers_left == 0))
3200 connect_start_time = GNUNET_TIME_absolute_get ();
3201 expected_connections
3202 = GNUNET_TESTING_connect_topology (
3205 connect_topology_option,
3206 connect_topology_option_modifier,
3208 connect_attempts, NULL, NULL);
3210 peer_connect_meter = create_meter (expected_connections,
3211 "Peer connection ", GNUNET_YES);
3212 fprintf (stderr, "Have %d expected connections\n",
3213 expected_connections);
3216 if (expected_connections == 0)
3219 = GNUNET_SCHEDULER_add_now (&end_badly,
3220 "from connect topology (bad return)");
3224 = GNUNET_SCHEDULER_add_delayed (
3225 GNUNET_TIME_relative_multiply (
3226 GNUNET_TIME_UNIT_SECONDS,
3227 DEFAULT_CONNECT_TIMEOUT
3228 * expected_connections),
3230 "from connect topology (timeout)");
3239 unsigned int create_expected_connections;
3240 peers_left = num_peers; /* Reset counter */
3241 create_expected_connections = GNUNET_TESTING_create_topology (pg, topology, blacklist_topology,
3242 blacklist_transports);
3243 if (create_expected_connections > 0)
3245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3246 "Topology set up, have %u expected connections, now starting peers!\n", create_expected_connections);
3247 GNUNET_TESTING_daemons_continue_startup (pg);
3251 GNUNET_SCHEDULER_cancel (die_task);
3252 die_task = GNUNET_SCHEDULER_add_now (&end_badly,
3253 "from create topology (bad return)");
3255 GNUNET_free_non_null(blacklist_transports);
3256 GNUNET_SCHEDULER_cancel (die_task);
3258 = GNUNET_SCHEDULER_add_delayed (
3259 GNUNET_TIME_relative_multiply (
3260 seconds_per_peer_start,
3263 "from continue startup (timeout)");
3267 * Callback indicating that the hostkey was created for a peer.
3270 * @param id the peer identity
3271 * @param d the daemon handle (pretty useless at this point, remove?)
3272 * @param emsg non-null on failure
3275 hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
3276 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3278 char * revision_str;
3281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3282 "Hostkey callback received error: %s\n", emsg);
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3287 "Hostkey (%d/%d) created for peer `%s'\n",
3288 num_peers - peers_left, num_peers, GNUNET_i2s(id));
3292 if (GNUNET_YES == update_meter (hostkey_meter))
3294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3295 "All %d hostkeys created, now creating topology!\n",
3298 GNUNET_asprintf (&revision_str, "%llu", revision);
3299 if (GNUNET_YES == insert_gauger_data)
3300 GAUGER_ID("DHT_TESTING",
3301 "HOSTKEY_GENERATION",
3302 GNUNET_TIME_absolute_get_duration(hostkey_start_time).rel_value / (double)num_peers,
3303 "ms/hostkey", revision_str);
3304 GNUNET_free(revision_str);
3306 GNUNET_SCHEDULER_cancel (die_task);
3307 /* Set up task in case topology creation doesn't finish
3308 * within a reasonable amount of time */
3309 die_task = GNUNET_SCHEDULER_add_delayed (DEFAULT_TOPOLOGY_TIMEOUT,
3311 "from create_topology");
3312 GNUNET_SCHEDULER_add_now (&create_topology, NULL);
3318 run(void *cls, char * const *args, const char *cfgfile,
3319 const struct GNUNET_CONFIGURATION_Handle *cfg)
3322 struct GNUNET_DHTLOG_TrialInfo trial_info;
3323 struct GNUNET_TESTING_Host *hosts;
3324 struct GNUNET_TESTING_Host *temphost;
3325 struct GNUNET_TESTING_Host *tempnext;
3327 char *connect_topology_str;
3328 char *blacklist_topology_str;
3329 char *connect_topology_option_str;
3330 char *connect_topology_option_modifier_string;
3332 char *topology_percentage_str;
3333 float topology_percentage;
3334 char *topology_probability_str;
3336 float topology_probability;
3337 unsigned long long temp_config_number;
3340 int strict_kademlia;
3344 char *churn_filename;
3351 rounds_finished = 0;
3352 memset (&trial_info, 0, sizeof(struct GNUNET_DHTLOG_TrialInfo));
3353 /* Get path from configuration file */
3354 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "paths",
3362 /* Get number of peers to start from configuration */
3363 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3368 GNUNET_ERROR_TYPE_WARNING,
3369 "Number of peers must be specified in section %s option %s\n",
3370 "TESTING", "NUM_PEERS");
3372 GNUNET_assert(num_peers > 0 && num_peers < ULONG_MAX);
3374 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3376 &temp_config_number))
3377 connect_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3378 temp_config_number);
3381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3382 "testing", "connect_timeout");
3386 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3391 "testing", "connect_attempts");
3396 != GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3397 "max_outstanding_connections",
3398 &max_outstanding_connections))
3400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3401 "testing", "max_outstanding_connections");
3405 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3406 "max_concurrent_ssh",
3407 &max_concurrent_ssh))
3409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3410 "testing", "max_concurrent_ssh");
3415 * Get DHT specific testing options.
3417 if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3420 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3421 "mysql_logging_extended"))
3423 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3424 "mysql_logging_minimal")))
3427 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3428 "mysql_logging_minimal"))
3429 dhtlog_minimal = GNUNET_YES;
3431 dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
3432 if (dhtlog_handle == NULL)
3434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3435 "Could not connect to mysql server for logging, will NOT log dht operations!");
3441 stop_closest = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3443 if (stop_closest == GNUNET_SYSERR)
3444 stop_closest = GNUNET_NO;
3446 stop_found = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found");
3447 if (stop_found == GNUNET_SYSERR)
3448 stop_found = GNUNET_NO;
3450 strict_kademlia = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3452 if (strict_kademlia == GNUNET_SYSERR)
3453 strict_kademlia = GNUNET_NO;
3455 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing",
3458 trialmessage = NULL;
3461 /** Check for a churn file to do churny simulation */
3462 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing",
3466 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Reading churn data from %s\n",
3468 if (GNUNET_OK != GNUNET_DISK_file_test (churn_filename))
3470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Error reading churn file!\n");
3471 GNUNET_free_non_null(trialmessage);
3472 GNUNET_free(churn_filename);
3475 if ((0 != STAT (churn_filename, &frstat)) || (frstat.st_size == 0))
3477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3478 "Could not open file specified for churn data, ending test!");
3480 GNUNET_free_non_null(trialmessage);
3481 GNUNET_free(churn_filename);
3485 churn_data = GNUNET_malloc_large (frstat.st_size);
3486 GNUNET_assert(churn_data != NULL);
3487 if (frstat.st_size != GNUNET_DISK_fn_read (churn_filename, churn_data,
3491 GNUNET_ERROR_TYPE_ERROR,
3492 "Could not read file %s specified for churn, ending test!",
3494 GNUNET_free (churn_filename);
3495 GNUNET_free (churn_data);
3496 GNUNET_free_non_null(trialmessage);
3500 GNUNET_free_non_null(churn_filename);
3504 /* Read the first line */
3505 while (count < frstat.st_size)
3508 if (((churn_data[count] == '\n')) && (buf != &churn_data[count]))
3510 churn_data[count] = '\0';
3511 if (1 != sscanf (buf, "%u", &churn_rounds))
3513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3514 "Failed to read number of rounds from churn file, ending test!\n");
3516 GNUNET_free_non_null(trialmessage);
3517 GNUNET_free_non_null(churn_data);
3520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3521 "Read %u rounds from churn file\n", churn_rounds);
3522 buf = &churn_data[count + 1];
3523 churn_array = GNUNET_malloc(sizeof(unsigned int) * churn_rounds);
3524 break; /* Done with this part */
3529 != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3533 churns_per_round = (unsigned long long) churn_rounds;
3537 while ((count < frstat.st_size) && (line_number < churn_rounds))
3540 if (((churn_data[count] == '\n')) && (buf != &churn_data[count]))
3542 churn_data[count] = '\0';
3544 ret = sscanf (buf, "%u", &churn_array[line_number]);
3547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3548 "Read %u peers in round %u\n",
3549 churn_array[line_number], line_number);
3554 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3555 "Error reading line `%s' in hostfile\n", buf);
3556 buf = &churn_data[count + 1];
3559 buf = &churn_data[count + 1];
3561 else if (churn_data[count] == '\n') /* Blank line */
3562 buf = &churn_data[count + 1];
3565 GNUNET_free_non_null(churn_data);
3567 /* Check for a hostfile containing user@host:port triples */
3569 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
3576 if (hostfile != NULL)
3578 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
3579 GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
3580 | GNUNET_DISK_PERM_USER_WRITE);
3581 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
3583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3584 "Could not open file specified for host list, ending test!");
3586 GNUNET_free_non_null(trialmessage);
3587 GNUNET_free(hostfile);
3591 data = GNUNET_malloc_large (frstat.st_size);
3592 GNUNET_assert(data != NULL);
3594 != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
3597 GNUNET_ERROR_TYPE_ERROR,
3598 "Could not read file %s specified for host list, ending test!",
3600 GNUNET_free (hostfile);
3602 GNUNET_free_non_null(trialmessage);
3606 GNUNET_free_non_null(hostfile);
3610 while (count < frstat.st_size - 1)
3613 if (((data[count] == '\n')) && (buf != &data[count]))
3616 temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
3617 ret = sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
3618 &temphost->username, &temphost->hostname,
3623 GNUNET_ERROR_TYPE_DEBUG,
3624 "Successfully read host %s, port %d and user %s from file\n",
3625 temphost->hostname, temphost->port,
3626 temphost->username);
3630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3631 "Error reading line `%s' in hostfile\n", buf);
3632 GNUNET_free(temphost);
3633 buf = &data[count + 1];
3636 temphost->next = hosts;
3638 buf = &data[count + 1];
3640 else if ((data[count] == '\n') || (data[count] == '\0'))
3641 buf = &data[count + 1];
3644 GNUNET_free_non_null(data);
3645 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3646 "malicious_getters",
3647 &malicious_getters))
3648 malicious_getters = 0;
3650 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3651 "malicious_putters",
3652 &malicious_putters))
3653 malicious_putters = 0;
3655 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3656 "malicious_droppers",
3657 &malicious_droppers))
3658 malicious_droppers = 0;
3660 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3665 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
3669 num_puts = num_peers;
3672 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3675 put_replication = DEFAULT_PUT_REPLICATION;
3677 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
3681 num_gets = num_peers;
3684 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3687 get_replication = DEFAULT_GET_REPLICATION;
3689 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3691 &temp_config_number))
3692 find_peer_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3693 temp_config_number);
3695 find_peer_delay = DEFAULT_FIND_PEER_DELAY;
3698 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3699 "concurrent_find_peers",
3700 &temp_config_number))
3701 max_outstanding_find_peers = temp_config_number;
3703 max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
3705 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3707 &temp_config_number))
3708 get_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3709 temp_config_number);
3711 get_timeout = DEFAULT_GET_TIMEOUT;
3713 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3715 &temp_config_number))
3716 max_outstanding_puts = temp_config_number;
3718 max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
3720 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3722 &temp_config_number))
3723 max_outstanding_gets = temp_config_number;
3725 max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
3727 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3729 &temp_config_number))
3730 all_get_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3731 temp_config_number);
3733 all_get_timeout.rel_value = get_timeout.rel_value * num_gets;
3735 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3737 &temp_config_number))
3738 get_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3739 temp_config_number);
3741 get_delay = DEFAULT_GET_DELAY;
3743 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3745 &temp_config_number))
3746 put_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3747 temp_config_number);
3749 put_delay = DEFAULT_PUT_DELAY;
3751 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3752 "peer_start_timeout",
3753 &temp_config_number))
3754 seconds_per_peer_start
3755 = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3756 temp_config_number);
3758 seconds_per_peer_start = DEFAULT_SECONDS_PER_PEER_START;
3760 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3762 &temp_config_number))
3763 test_data_size = temp_config_number;
3765 test_data_size = DEFAULT_TEST_DATA_SIZE;
3768 * Get testing related options.
3770 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3772 replicate_same = GNUNET_YES;
3775 * Get testing related options.
3777 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3779 get_from_same = GNUNET_YES;
3782 == GNUNET_CONFIGURATION_get_value_time (cfg, "DHT_TESTING",
3783 "MALICIOUS_GET_FREQUENCY",
3784 &malicious_get_frequency))
3785 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
3788 == GNUNET_CONFIGURATION_get_value_time (cfg, "DHT_TESTING",
3789 "MALICIOUS_PUT_FREQUENCY",
3790 &malicious_put_frequency))
3791 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
3794 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3795 "MALICIOUS_AFTER_SETTLE"))
3796 malicious_after_settle = GNUNET_YES;
3798 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3801 /* Set up the malicious target at random for this round */
3802 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
3804 malicious_sybil = GNUNET_YES;
3807 /* Create the bloomfilter for choosing which peers to set malicious */
3809 /* Bloomfilter size must be 2^k for some integer k */
3811 while (1 << k < malicious_droppers)
3813 if (malicious_droppers > 0)
3814 malicious_bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, 1 << k,
3817 /* The normal behavior of the DHT is to do find peer requests
3818 * on its own. Only if this is explicitly turned off should
3819 * the testing driver issue find peer requests (even though
3820 * this is likely the default when testing).
3822 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3824 do_find_peer = GNUNET_YES;
3826 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3827 "insert_gauger_data"))
3828 insert_gauger_data = GNUNET_YES;
3830 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3832 in_dht_replication = GNUNET_YES;
3834 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3839 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3844 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3846 &temp_config_number))
3847 find_peer_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3848 temp_config_number);
3850 find_peer_delay = DEFAULT_FIND_PEER_DELAY;
3852 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3858 == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3859 "OUTSTANDING_FIND_PEERS",
3860 &max_outstanding_find_peers))
3861 max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
3863 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3865 max_outstanding_find_peers = max_outstanding_find_peers * 1;
3867 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay,
3868 max_outstanding_find_peers);
3870 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
3877 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3878 "target_total_connections",
3879 &target_total_connections))
3880 || (target_total_connections == 0))
3881 target_total_connections = connection_estimate (num_peers,
3882 DEFAULT_BUCKET_SIZE);
3884 topology_str = NULL;
3885 if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3888 && (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
3890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3891 "Invalid topology `%s' given for section %s option %s\n",
3892 topology_str, "TESTING", "TOPOLOGY");
3893 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
3897 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "percentage",
3898 &topology_percentage_str))
3899 topology_percentage = 0.5;
3902 topology_percentage = atof (topology_percentage_str);
3903 GNUNET_free(topology_percentage_str);
3907 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "probability",
3908 &topology_probability_str))
3909 topology_probability = 0.5;
3912 topology_probability = atof (topology_probability_str);
3913 GNUNET_free(topology_probability_str);
3917 == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3919 &connect_topology_str))
3920 && (GNUNET_NO == GNUNET_TESTING_topology_get (&connect_topology,
3921 connect_topology_str)))
3924 GNUNET_ERROR_TYPE_WARNING,
3925 "Invalid connect topology `%s' given for section %s option %s\n",
3926 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
3928 GNUNET_free_non_null(connect_topology_str);
3931 == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3932 "connect_topology_option",
3933 &connect_topology_option_str))
3935 == GNUNET_TESTING_topology_option_get (&connect_topology_option,
3936 connect_topology_option_str)))
3939 GNUNET_ERROR_TYPE_WARNING,
3940 "Invalid connect topology option `%s' given for section %s option %s\n",
3941 connect_topology_option_str, "TESTING",
3942 "CONNECT_TOPOLOGY_OPTION");
3943 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
3945 GNUNET_free_non_null(connect_topology_option_str);
3948 == GNUNET_CONFIGURATION_get_value_string (
3951 "connect_topology_option_modifier",
3952 &connect_topology_option_modifier_string))
3954 if (sscanf (connect_topology_option_modifier_string, "%lf",
3955 &connect_topology_option_modifier) != 1)
3958 GNUNET_ERROR_TYPE_WARNING,
3959 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
3960 connect_topology_option_modifier_string,
3961 "connect_topology_option_modifier", "TESTING");
3963 GNUNET_free (connect_topology_option_modifier_string);
3967 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3968 "blacklist_transports",
3969 &blacklist_transports))
3970 blacklist_transports = NULL;
3973 == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3974 "blacklist_topology",
3975 &blacklist_topology_str))
3976 && (GNUNET_NO == GNUNET_TESTING_topology_get (&blacklist_topology,
3977 blacklist_topology_str)))
3979 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3980 "Invalid topology `%s' given for section %s option %s\n",
3981 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
3983 GNUNET_free_non_null(topology_str);
3984 GNUNET_free_non_null(blacklist_topology_str);
3986 /* Set peers_left so we know when all peers started */
3987 peers_left = num_peers;
3989 /* Set up a task to end testing if peer start fails */
3991 = GNUNET_SCHEDULER_add_delayed (
3992 GNUNET_TIME_relative_multiply (
3993 seconds_per_peer_start,
3996 "didn't generate all hostkeys within allowed startup time!");
3998 if (dhtlog_handle == NULL)
3999 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "dhtlog_handle is NULL!");
4001 trial_info.other_identifier = (unsigned int) trial_to_run;
4002 trial_info.num_nodes = peers_left;
4003 trial_info.topology = topology;
4004 trial_info.blacklist_topology = blacklist_topology;
4005 trial_info.connect_topology = connect_topology;
4006 trial_info.connect_topology_option = connect_topology_option;
4007 trial_info.connect_topology_option_modifier
4008 = connect_topology_option_modifier;
4009 trial_info.topology_percentage = topology_percentage;
4010 trial_info.topology_probability = topology_probability;
4011 trial_info.puts = num_puts;
4012 trial_info.gets = num_gets;
4013 trial_info.concurrent = max_outstanding_gets;
4014 trial_info.settle_time = settle_time;
4015 trial_info.num_rounds = total_rounds;
4016 trial_info.malicious_getters = malicious_getters;
4017 trial_info.malicious_putters = malicious_putters;
4018 trial_info.malicious_droppers = malicious_droppers;
4019 trial_info.malicious_get_frequency = malicious_get_frequency.rel_value;
4020 trial_info.malicious_put_frequency = malicious_put_frequency.rel_value;
4021 trial_info.stop_closest = stop_closest;
4022 trial_info.stop_found = stop_found;
4023 trial_info.strict_kademlia = strict_kademlia;
4025 if (trialmessage != NULL)
4026 trial_info.message = trialmessage;
4028 trial_info.message = "";
4030 if (dhtlog_handle != NULL)
4031 dhtlog_handle->insert_trial (&trial_info);
4033 GNUNET_free_non_null(trialmessage);
4035 hostkey_meter = create_meter (peers_left, "Hostkeys created ", GNUNET_YES);
4036 peer_start_meter = create_meter (peers_left, "Peers started ", GNUNET_YES);
4038 put_meter = create_meter (num_puts, "Puts completed ", GNUNET_YES);
4039 get_meter = create_meter (num_gets, "Gets completed ", GNUNET_YES);
4040 hostkey_start_time = GNUNET_TIME_absolute_get();
4042 = GNUNET_TESTING_daemons_start (
4045 max_outstanding_connections,
4047 GNUNET_TIME_relative_multiply (
4048 seconds_per_peer_start,
4050 &hostkey_callback, NULL,
4051 &peers_started_callback, NULL,
4052 &topology_callback, NULL, hosts);
4054 while (temphost != NULL)
4056 tempnext = temphost->next;
4057 GNUNET_free (temphost->username);
4058 GNUNET_free (temphost->hostname);
4059 GNUNET_free (temphost);
4060 temphost = tempnext;
4065 main(int argc, char *argv[])
4068 struct GNUNET_GETOPT_CommandLineOption options[] =
4070 GNUNET_GETOPT_OPTION_END };
4072 ret = GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-driver", "nohelp", options,
4075 if (malicious_bloom != NULL)
4076 GNUNET_CONTAINER_bloomfilter_free (malicious_bloom);
4078 if (ret != GNUNET_OK)
4080 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4081 "`gnunet-dht-driver': Failed with error code %d\n", ret);
4085 * Need to remove base directory, subdirectories taken care
4086 * of by the testing framework.
4088 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
4090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4091 "Failed to remove testing directory %s\n", test_directory);
4096 /* end of gnunet-dht-driver.c */