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 * Boolean value, should the driver issue find peer requests
430 * (GNUNET_YES) or should it be left to the service (GNUNET_NO)
432 static unsigned int do_find_peer;
436 * Are we currently trying to connect two peers repeatedly?
438 static unsigned int repeat_connect_mode;
441 * Task for repeating connects.
443 GNUNET_SCHEDULER_TaskIdentifier repeat_connect_task;
445 struct GNUNET_TESTING_Daemon *repeat_connect_peer1;
446 struct GNUNET_TESTING_Daemon *repeat_connect_peer2;
450 * Boolean value, should replication be done by the dht
451 * service (GNUNET_YES) or by the driver (GNUNET_NO)
453 static unsigned int in_dht_replication;
456 * Size of test data to insert/retrieve during testing.
458 static unsigned long long test_data_size = DEFAULT_TEST_DATA_SIZE;
461 * Maximum number of concurrent connections to peers.
463 static unsigned long long max_outstanding_connections;
466 * Maximum number of concurrent ssh instances to peers.
468 static unsigned long long max_concurrent_ssh;
471 * Maximum number of concurrent PUT requests.
473 static unsigned long long max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
476 * Maximum number of concurrent GET requests.
478 static unsigned long long max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
481 * Number of nodes issuing malicious GET messages.
483 static unsigned long long malicious_getters;
486 * Maximum number of concurrent find peer messages being sent.
488 static unsigned long long max_outstanding_find_peers;
491 * Number of nodes issuing malicious PUT messages.
493 static unsigned long long malicious_putters;
496 * Time (in seconds) to delay between rounds.
498 static unsigned long long round_delay;
501 * The identifier for this trial (if we have one)
502 * for external data collection.
504 static unsigned long long trial_to_run;
507 * How many malicious droppers to seed in the network.
509 static unsigned long long malicious_droppers;
512 * Bloom filter to restrict malicious nodes chosen.
514 struct GNUNET_CONTAINER_BloomFilter *malicious_bloom;
517 * Whether malicious droppers should be chosen based on proximity to a key.
519 static int malicious_sybil;
522 * Target for the malicious sybil nodes (choose the closest to this key).
524 static GNUNET_HashCode sybil_target;
527 * How often to send malicious GET messages.
529 static struct GNUNET_TIME_Relative malicious_get_frequency;
532 * How often to send malicious PUT messages.
534 static struct GNUNET_TIME_Relative malicious_put_frequency;
537 * How long to send find peer requests.
539 static unsigned long long settle_time;
542 * Handle to the dhtlog service.
544 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
547 * Replication value for GET requests.
549 static unsigned long long get_replication;
552 * Replication value for PUT requests.
554 static unsigned long long put_replication;
557 * If GNUNET_YES, insert data at the same peers every time.
558 * Otherwise, choose a new random peer to insert at each time.
560 static unsigned int replicate_same;
563 * If GNUNET_YES, issue GET requests at the same peers every time.
564 * Otherwise, choose a new random peer/data combination to search
567 static unsigned int get_from_same;
570 * Should malicious peers be set after allowing for settle time?
571 * Default is to set them malicious after initial connection setup.
573 static unsigned int malicious_after_settle;
576 * Number of rounds for testing (PUTS + GETS)
578 static unsigned long long total_rounds;
581 * Target number of connections (will stop sending find peer
582 * messages when this number is exceeded)
584 static unsigned long long target_total_connections;
587 * Number of rounds already run
589 static unsigned int rounds_finished;
592 * Number of rounds of churn to read from the file (first line, should be a single number).
594 static unsigned int churn_rounds;
597 * Current round we are in for churn, tells us how many peers to connect/disconnect.
599 static unsigned int current_churn_round;
602 * Number of times to churn per round
604 static unsigned long long churns_per_round;
607 * Array of churn values.
609 static unsigned int *churn_array;
612 * Hash map of stats contexts.
614 static struct GNUNET_CONTAINER_MultiHashMap *stats_map;
617 * LL of malicious settings.
619 struct MaliciousContext *all_malicious;
622 * List of GETS to perform
624 struct TestGetContext *all_gets;
627 * List of PUTS to perform
629 struct TestPutContext *all_puts;
632 * Directory to store temporary data in, defined in config file
634 static char *test_directory;
637 * Variable used to store the number of connections we should wait for.
639 static unsigned int expected_connections;
642 * Variable used to keep track of how many peers aren't yet started.
644 static unsigned long long peers_left;
647 * Handle to the set of all peers run for this test.
649 static struct GNUNET_TESTING_PeerGroup *pg;
652 * Global config handle.
654 static const struct GNUNET_CONFIGURATION_Handle *config;
657 * Total number of peers to run, set based on config file.
659 static unsigned long long num_peers;
662 * Total number of items to insert.
664 static unsigned long long num_puts;
667 * How many puts do we currently have in flight?
669 static unsigned long long outstanding_puts;
672 * How many puts are done?
674 static unsigned long long puts_completed;
677 * Total number of items to attempt to get.
679 static unsigned long long num_gets;
682 * How many puts do we currently have in flight?
684 static unsigned long long outstanding_gets;
687 * How many gets are done?
689 static unsigned long long gets_completed;
692 * Total number of items to attempt to get.
694 static unsigned long long cumulative_num_gets;
697 * How many gets are done?
699 static unsigned long long cumulative_successful_gets;
702 * How many gets failed?
704 static unsigned long long gets_failed;
707 * How many malicious control messages do
708 * we currently have in flight?
710 static unsigned long long outstanding_malicious;
713 * How many set malicious peers are done?
715 static unsigned int malicious_completed;
718 * For gauger logging, what specific identifier (svn revision)
721 static unsigned long long revision;
724 * Global used to count how many connections we have currently
725 * been notified about (how many times has topology_callback been called
728 static unsigned int total_connections;
731 * Previous connections, for counting new connections during some duration.
733 static unsigned int previous_connections;
736 * For counting failed connections during some duration.
738 static unsigned int previous_failed_connections;
741 * Global used to count how many failed connections we have
742 * been notified about (how many times has topology_callback
743 * been called with failure?)
745 static unsigned int failed_connections;
748 * If GNUNET_YES, only log PUT/GET round data to mysql, otherwise
749 * log everything (including each dht service logging).
751 static unsigned int dhtlog_minimal;
753 /* Task handle to use to schedule shutdown if something goes wrong */
754 GNUNET_SCHEDULER_TaskIdentifier die_task;
756 static char *blacklist_transports;
758 static enum GNUNET_TESTING_Topology topology;
760 static enum GNUNET_TESTING_Topology blacklist_topology =
761 GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
763 static enum GNUNET_TESTING_Topology connect_topology =
764 GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
766 static enum GNUNET_TESTING_TopologyOption connect_topology_option =
767 GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
769 static double connect_topology_option_modifier = 0.0;
771 static struct ProgressMeter *hostkey_meter;
773 static struct ProgressMeter *peer_start_meter;
775 static struct ProgressMeter *peer_connect_meter;
777 static struct ProgressMeter *put_meter;
779 static struct ProgressMeter *get_meter;
781 static GNUNET_HashCode *known_keys;
783 /* Global return value (0 for success, anything else for failure) */
787 * Create a meter to keep track of the progress of some task.
789 * @param total the total number of items to complete
790 * @param start_string a string to prefix the meter with (if printing)
791 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
794 * @return the progress meter
796 static struct ProgressMeter *
797 create_meter(unsigned int total, char * start_string, int print)
799 struct ProgressMeter *ret;
800 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
803 ret->modnum = total / 4;
804 ret->dotnum = (total / 50) + 1;
805 if (start_string != NULL)
806 ret->startup_string = GNUNET_strdup(start_string);
808 ret->startup_string = GNUNET_strdup("");
814 * Update progress meter (increment by one).
816 * @param meter the meter to update and print info for
818 * @return GNUNET_YES if called the total requested,
819 * GNUNET_NO if more items expected
822 update_meter(struct ProgressMeter *meter)
824 if (meter->print == GNUNET_YES)
826 if (meter->completed % meter->modnum == 0)
828 if (meter->completed == 0)
830 fprintf (stdout, "%sProgress: [0%%", meter->startup_string);
833 fprintf (stdout, "%d%%", (int) (((float) meter->completed
834 / meter->total) * 100));
836 else if (meter->completed % meter->dotnum == 0)
837 fprintf (stdout, ".");
839 if (meter->completed + 1 == meter->total)
840 fprintf (stdout, "%d%%]\n", 100);
845 if (meter->completed == meter->total)
851 * Reset progress meter.
853 * @param meter the meter to reset
855 * @return GNUNET_YES if meter reset,
856 * GNUNET_SYSERR on error
859 reset_meter(struct ProgressMeter *meter)
862 return GNUNET_SYSERR;
864 meter->completed = 0;
869 * Release resources for meter
871 * @param meter the meter to free
874 free_meter(struct ProgressMeter *meter)
876 GNUNET_free_non_null (meter->startup_string);
881 * Check whether peers successfully shut down.
884 shutdown_callback(void *cls, const char *emsg)
894 * Task to release DHT handles for PUT
897 put_disconnect_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
899 struct TestPutContext *test_put = cls;
900 test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
901 GNUNET_DHT_disconnect (test_put->dht_handle);
902 test_put->dht_handle = NULL;
903 if (replicate_same == GNUNET_NO)
905 = GNUNET_TESTING_daemon_get (
907 GNUNET_CRYPTO_random_u32 (
908 GNUNET_CRYPTO_QUALITY_WEAK,
913 * Function scheduled to be run on the successful completion of this
917 finish_testing(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending test normally!\n",
921 GNUNET_assert (pg != NULL);
922 struct TestPutContext *test_put = all_puts;
923 struct TestGetContext *test_get = all_gets;
924 char *temp_get_string;
927 while (test_put != NULL)
929 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
930 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
931 if (test_put->dht_handle != NULL)
932 GNUNET_DHT_disconnect (test_put->dht_handle);
933 test_put = test_put->next;
936 while (test_get != NULL)
938 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
939 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
940 if (test_get->get_handle != NULL)
941 GNUNET_DHT_get_stop (test_get->get_handle);
942 if (test_get->dht_handle != NULL)
943 GNUNET_DHT_disconnect (test_get->dht_handle);
944 test_get = test_get->next;
947 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
949 if (dhtlog_handle != NULL)
951 fprintf (stderr, "Update trial endtime\n");
952 dhtlog_handle->update_trial (cumulative_successful_gets);
953 GNUNET_DHTLOG_disconnect (dhtlog_handle);
954 dhtlog_handle = NULL;
957 if (hostkey_meter != NULL)
958 free_meter (hostkey_meter);
959 if (peer_start_meter != NULL)
960 free_meter (peer_start_meter);
961 if (peer_connect_meter != NULL)
962 free_meter (peer_connect_meter);
963 if (put_meter != NULL)
964 free_meter (put_meter);
965 if (get_meter != NULL)
966 free_meter (get_meter);
968 GNUNET_asprintf (&temp_get_string,
969 "DHT Successful GETs (trial %d)", trial_to_run);
970 GNUNET_asprintf (&revision_str, "%llu", revision);
971 GAUGER_ID("DHT_TESTING", temp_get_string, cumulative_successful_gets / (double)cumulative_num_gets, "percent successful", revision_str);
974 "Finished trial, had %llu successful gets out of %llu total, %.2f percent succeeded\n",
975 cumulative_successful_gets, cumulative_num_gets,
976 cumulative_successful_gets / (double) cumulative_num_gets);
977 GNUNET_free(temp_get_string);
983 * Callback for iterating over all the peer connections of a peer group.
986 log_topology_cb(void *cls, const struct GNUNET_PeerIdentity *first,
987 const struct GNUNET_PeerIdentity *second, const char *emsg)
989 struct TopologyIteratorContext *topo_ctx = cls;
990 if ((first != NULL) && (second != NULL))
992 if ((topo_ctx->peers_seen != NULL) && (GNUNET_NO
993 == GNUNET_CONTAINER_multihashmap_contains (topo_ctx->peers_seen,
994 &first->hashPubKey)))
996 GNUNET_CONTAINER_multihashmap_put (topo_ctx->peers_seen,
997 &first->hashPubKey, NULL,
998 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
999 topo_ctx->total_peers++;
1001 topo_ctx->total_connections++;
1002 if ((GNUNET_NO == dhtlog_minimal) && (dhtlog_handle != NULL))
1003 dhtlog_handle->insert_extended_topology (first, second);
1007 GNUNET_assert(dhtlog_handle != NULL);
1009 GNUNET_ERROR_TYPE_WARNING,
1010 "Topology iteration (%u/%u) finished (%u connections, %u peers)\n",
1011 topo_ctx->current_iteration, topo_ctx->total_iterations,
1012 topo_ctx->total_connections, topo_ctx->total_peers);
1013 dhtlog_handle->update_topology (topo_ctx->total_connections);
1014 if (topo_ctx->cont != NULL)
1015 GNUNET_SCHEDULER_add_now (topo_ctx->cont, topo_ctx->cls);
1016 if (topo_ctx->peers_seen != NULL)
1017 GNUNET_CONTAINER_multihashmap_destroy (topo_ctx->peers_seen);
1018 GNUNET_free(topo_ctx);
1023 * Iterator over hash map entries.
1025 * @param cls closure - always NULL
1026 * @param key current key code
1027 * @param value value in the hash map, a stats context
1028 * @return GNUNET_YES if we should continue to
1033 stats_iterate(void *cls, const GNUNET_HashCode * key, void *value)
1035 struct StatisticsIteratorContext *stats_ctx;
1039 dhtlog_handle->insert_stat (stats_ctx->peer, stats_ctx->stat_routes,
1040 stats_ctx->stat_route_forwards,
1041 stats_ctx->stat_results,
1042 stats_ctx->stat_results_to_client,
1043 stats_ctx->stat_result_forwards,
1044 stats_ctx->stat_gets, stats_ctx->stat_puts,
1045 stats_ctx->stat_puts_inserted,
1046 stats_ctx->stat_find_peer,
1047 stats_ctx->stat_find_peer_start,
1048 stats_ctx->stat_get_start,
1049 stats_ctx->stat_put_start,
1050 stats_ctx->stat_find_peer_reply,
1051 stats_ctx->stat_get_reply,
1052 stats_ctx->stat_find_peer_answer,
1053 stats_ctx->stat_get_response_start);
1054 GNUNET_free(stats_ctx);
1059 stats_finished(void *cls, int result)
1061 fprintf (stderr, "Finished getting all peers statistics, iterating!\n");
1062 GNUNET_CONTAINER_multihashmap_iterate (stats_map, &stats_iterate, NULL);
1063 GNUNET_CONTAINER_multihashmap_destroy (stats_map);
1064 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
1068 * Callback function to process statistic values.
1070 * @param cls closure
1071 * @param peer the peer the statistics belong to
1072 * @param subsystem name of subsystem that created the statistic
1073 * @param name the name of the datum
1074 * @param value the current value
1075 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1076 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1079 stats_handle(void *cls, const struct GNUNET_PeerIdentity *peer,
1080 const char *subsystem, const char *name, uint64_t value, int is_persistent)
1082 struct StatisticsIteratorContext *stats_ctx;
1084 if (dhtlog_handle != NULL)
1085 dhtlog_handle->add_generic_stat (peer, name, subsystem, value);
1086 if (GNUNET_CONTAINER_multihashmap_contains (stats_map, &peer->hashPubKey))
1088 stats_ctx = GNUNET_CONTAINER_multihashmap_get (stats_map,
1093 stats_ctx = GNUNET_malloc(sizeof(struct StatisticsIteratorContext));
1094 stats_ctx->peer = peer;
1095 GNUNET_CONTAINER_multihashmap_put (stats_map, &peer->hashPubKey,
1097 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1099 GNUNET_assert(stats_ctx != NULL);
1101 if (strcmp (name, STAT_ROUTES) == 0)
1102 stats_ctx->stat_routes = value;
1103 else if (strcmp (name, STAT_ROUTE_FORWARDS) == 0)
1104 stats_ctx->stat_route_forwards = value;
1105 else if (strcmp (name, STAT_RESULTS) == 0)
1106 stats_ctx->stat_results = value;
1107 else if (strcmp (name, STAT_RESULTS_TO_CLIENT) == 0)
1108 stats_ctx->stat_results_to_client = value;
1109 else if (strcmp (name, STAT_RESULT_FORWARDS) == 0)
1110 stats_ctx->stat_result_forwards = value;
1111 else if (strcmp (name, STAT_GETS) == 0)
1112 stats_ctx->stat_gets = value;
1113 else if (strcmp (name, STAT_PUTS) == 0)
1114 stats_ctx->stat_puts = value;
1115 else if (strcmp (name, STAT_PUTS_INSERTED) == 0)
1116 stats_ctx->stat_puts_inserted = value;
1117 else if (strcmp (name, STAT_FIND_PEER) == 0)
1118 stats_ctx->stat_find_peer = value;
1119 else if (strcmp (name, STAT_FIND_PEER_START) == 0)
1120 stats_ctx->stat_find_peer_start = value;
1121 else if (strcmp (name, STAT_GET_START) == 0)
1122 stats_ctx->stat_get_start = value;
1123 else if (strcmp (name, STAT_PUT_START) == 0)
1124 stats_ctx->stat_put_start = value;
1125 else if (strcmp (name, STAT_FIND_PEER_REPLY) == 0)
1126 stats_ctx->stat_find_peer_reply = value;
1127 else if (strcmp (name, STAT_GET_REPLY) == 0)
1128 stats_ctx->stat_get_reply = value;
1129 else if (strcmp (name, STAT_FIND_PEER_ANSWER) == 0)
1130 stats_ctx->stat_find_peer_answer = value;
1131 else if (strcmp (name, STAT_GET_RESPONSE_START) == 0)
1132 stats_ctx->stat_get_response_start = value;
1138 * Connect to statistics service for each peer and get the appropriate
1139 * dht statistics for safe keeping.
1142 log_dht_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1144 stats_map = GNUNET_CONTAINER_multihashmap_create (num_peers);
1145 fprintf (stderr, "Starting statistics logging\n");
1146 GNUNET_TESTING_get_statistics (pg, &stats_finished, &stats_handle, NULL);
1150 * Connect to all peers in the peer group and iterate over their
1154 capture_current_topology(void *cls,
1155 const struct GNUNET_SCHEDULER_TaskContext * tc)
1157 struct TopologyIteratorContext *topo_ctx = cls;
1158 dhtlog_handle->insert_topology (0);
1159 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Called capture_current_topology\n");
1160 GNUNET_TESTING_get_topology (pg, &log_topology_cb, topo_ctx);
1164 * Check if the get_handle is being used, if so stop the request. Either
1165 * way, schedule the end_badly_cont function which actually shuts down the
1169 end_badly(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failing test with error: `%s'!\n",
1174 struct TestPutContext *test_put = all_puts;
1175 struct TestGetContext *test_get = all_gets;
1177 while (test_put != NULL)
1179 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
1180 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
1181 if (test_put->dht_handle != NULL)
1182 GNUNET_DHT_disconnect (test_put->dht_handle);
1183 test_put = test_put->next;
1186 while (test_get != NULL)
1188 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
1189 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
1190 if (test_get->get_handle != NULL)
1191 GNUNET_DHT_get_stop (test_get->get_handle);
1192 if (test_get->dht_handle != NULL)
1193 GNUNET_DHT_disconnect (test_get->dht_handle);
1194 test_get = test_get->next;
1197 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
1199 if (dhtlog_handle != NULL)
1201 fprintf (stderr, "Update trial endtime\n");
1202 dhtlog_handle->update_trial (gets_completed);
1203 GNUNET_DHTLOG_disconnect (dhtlog_handle);
1204 dhtlog_handle = NULL;
1207 if (hostkey_meter != NULL)
1208 free_meter (hostkey_meter);
1209 if (peer_start_meter != NULL)
1210 free_meter (peer_start_meter);
1211 if (peer_connect_meter != NULL)
1212 free_meter (peer_connect_meter);
1213 if (put_meter != NULL)
1214 free_meter (put_meter);
1215 if (get_meter != NULL)
1216 free_meter (get_meter);
1222 * Forward declaration.
1225 do_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1228 * Forward declaration.
1231 do_get(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1234 * Iterator over hash map entries.
1236 * @param cls closure
1237 * @param key current key code
1238 * @param value value in the hash map
1239 * @return GNUNET_YES if we should continue to
1244 remove_peer_count(void *cls, const GNUNET_HashCode * key, void *value)
1246 struct PeerCount *peer_count = value;
1247 GNUNET_CONTAINER_heap_remove_node (peer_count->heap_node);
1248 GNUNET_free(peer_count);
1254 * Connect to all peers in the peer group and iterate over their
1258 count_new_peers(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1260 struct FindPeerContext *find_peer_context = cls;
1261 find_peer_context->previous_peers = find_peer_context->current_peers;
1262 find_peer_context->current_peers = 0;
1263 GNUNET_TESTING_get_topology (pg, find_peer_context->count_peers_cb,
1268 decrement_find_peers(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1270 struct TestFindPeer *test_find_peer = cls;
1271 GNUNET_assert(test_find_peer->find_peer_context->outstanding > 0);
1272 test_find_peer->find_peer_context->outstanding--;
1273 test_find_peer->find_peer_context->total--;
1274 if (0 == test_find_peer->find_peer_context->total)
1276 GNUNET_SCHEDULER_add_now (&count_new_peers,
1277 test_find_peer->find_peer_context);
1279 GNUNET_free(test_find_peer);
1283 * A find peer request has been sent to the server, now we will schedule a task
1284 * to wait the appropriate time to allow the request to go out and back.
1286 * @param cls closure - a TestFindPeer struct
1287 * @param tc context the task is being called with
1290 handle_find_peer_sent(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1292 struct TestFindPeer *test_find_peer = cls;
1294 GNUNET_DHT_disconnect (test_find_peer->dht_handle);
1295 GNUNET_SCHEDULER_add_delayed (
1296 GNUNET_TIME_relative_divide (find_peer_delay, 2),
1297 &decrement_find_peers, test_find_peer);
1301 send_find_peer_request(void *cls,
1302 const struct GNUNET_SCHEDULER_TaskContext * tc)
1304 struct TestFindPeer *test_find_peer = cls;
1306 if (test_find_peer->find_peer_context->outstanding
1307 > max_outstanding_find_peers)
1309 GNUNET_SCHEDULER_add_delayed (find_peer_offset, &send_find_peer_request,
1314 test_find_peer->find_peer_context->outstanding++;
1315 if (GNUNET_TIME_absolute_get_remaining (
1316 test_find_peer->find_peer_context->endtime).rel_value
1319 GNUNET_SCHEDULER_add_now (&decrement_find_peers, test_find_peer);
1323 test_find_peer->dht_handle = GNUNET_DHT_connect (test_find_peer->daemon->cfg,
1325 GNUNET_assert(test_find_peer->dht_handle != NULL);
1326 GNUNET_DHT_find_peers (test_find_peer->dht_handle, &handle_find_peer_sent,
1331 * Add a connection to the find_peer_context given. This may
1332 * be complete overkill, but allows us to choose the peers with
1333 * the least connections to initiate find peer requests from.
1336 add_new_connection(struct FindPeerContext *find_peer_context,
1337 const struct GNUNET_PeerIdentity *first,
1338 const struct GNUNET_PeerIdentity *second)
1340 struct PeerCount *first_count;
1341 struct PeerCount *second_count;
1343 if (GNUNET_CONTAINER_multihashmap_contains (find_peer_context->peer_hash,
1344 &first->hashPubKey))
1347 = GNUNET_CONTAINER_multihashmap_get (find_peer_context->peer_hash,
1348 &first->hashPubKey);
1349 GNUNET_assert(first_count != NULL);
1350 first_count->count++;
1351 GNUNET_CONTAINER_heap_update_cost (find_peer_context->peer_min_heap,
1352 first_count->heap_node,
1353 first_count->count);
1357 first_count = GNUNET_malloc(sizeof(struct PeerCount));
1358 first_count->count = 1;
1359 memcpy (&first_count->peer_id, first, sizeof(struct GNUNET_PeerIdentity));
1360 first_count->heap_node
1361 = GNUNET_CONTAINER_heap_insert (find_peer_context->peer_min_heap,
1362 first_count, first_count->count);
1363 GNUNET_CONTAINER_multihashmap_put (find_peer_context->peer_hash,
1364 &first->hashPubKey, first_count,
1365 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1368 if (GNUNET_CONTAINER_multihashmap_contains (find_peer_context->peer_hash,
1369 &second->hashPubKey))
1372 = GNUNET_CONTAINER_multihashmap_get (find_peer_context->peer_hash,
1373 &second->hashPubKey);
1374 GNUNET_assert(second_count != NULL);
1375 second_count->count++;
1376 GNUNET_CONTAINER_heap_update_cost (find_peer_context->peer_min_heap,
1377 second_count->heap_node,
1378 second_count->count);
1382 second_count = GNUNET_malloc(sizeof(struct PeerCount));
1383 second_count->count = 1;
1384 memcpy (&second_count->peer_id, second,
1385 sizeof(struct GNUNET_PeerIdentity));
1386 second_count->heap_node
1387 = GNUNET_CONTAINER_heap_insert (find_peer_context->peer_min_heap,
1388 second_count, second_count->count);
1389 GNUNET_CONTAINER_multihashmap_put (find_peer_context->peer_hash,
1390 &second->hashPubKey, second_count,
1391 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1396 * Iterate over min heap of connections per peer. For any
1397 * peer that has 0 connections, attempt to connect them to
1400 * @param cls closure a struct FindPeerContext
1401 * @param node internal node of the heap
1402 * @param element value stored, a struct PeerCount
1403 * @param cost cost associated with the node
1404 * @return GNUNET_YES if we should continue to iterate,
1408 iterate_min_heap_peers(void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1409 void *element, GNUNET_CONTAINER_HeapCostType cost)
1411 struct FindPeerContext *find_peer_context = cls;
1412 struct PeerCount *peer_count = element;
1413 struct GNUNET_TESTING_Daemon *d1;
1414 struct GNUNET_TESTING_Daemon *d2;
1415 struct GNUNET_TIME_Relative timeout;
1418 d1 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
1419 GNUNET_assert(d1 != NULL);
1421 while ((d2 == d1) || (GNUNET_YES != GNUNET_TESTING_daemon_running (d2)))
1424 = GNUNET_TESTING_daemon_get (
1426 GNUNET_CRYPTO_random_u32 (
1427 GNUNET_CRYPTO_QUALITY_WEAK,
1429 GNUNET_assert(d2 != NULL);
1432 /** Just try to connect the peers, don't worry about callbacks, etc. **/
1433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1434 "Peer %s has 0 connections. Trying to connect to %s...\n",
1435 GNUNET_i2s (&peer_count->peer_id), d2->shortname);
1436 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1437 DEFAULT_CONNECT_TIMEOUT);
1438 if (GNUNET_TIME_relative_to_absolute (timeout).abs_value
1439 > find_peer_context->endtime.abs_value)
1442 = GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime);
1444 GNUNET_TESTING_daemons_connect (d1, d2, timeout,
1445 DEFAULT_RECONNECT_ATTEMPTS, GNUNET_YES,
1448 if (GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime).rel_value
1456 * Forward declaration.
1459 schedule_churn_find_peer_requests(void *cls,
1460 const struct GNUNET_SCHEDULER_TaskContext * tc);
1463 * Callback for iterating over all the peer connections of a peer group.
1464 * Used after we have churned on some peers to find which ones have zero
1465 * connections so we can make them issue find peer requests.
1468 count_peers_churn_cb(void *cls, const struct GNUNET_PeerIdentity *first,
1469 const struct GNUNET_PeerIdentity *second, const char *emsg)
1471 struct FindPeerContext *find_peer_context = cls;
1472 struct TopologyIteratorContext *topo_ctx;
1473 struct PeerCount *peer_count;
1475 if ((first != NULL) && (second != NULL))
1477 add_new_connection (find_peer_context, first, second);
1478 find_peer_context->current_peers++;
1482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1483 "Peer count finished (%u connections)\n",
1484 find_peer_context->current_peers);
1486 = GNUNET_CONTAINER_heap_peek (find_peer_context->peer_min_heap);
1487 GNUNET_assert(peer_count != NULL);
1488 /* 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. */
1489 /* NO they won't, because we have disabled peerinfo writing to disk (remember?) so we WILL have to give them new connections */
1490 /* Best course of action: have DHT automatically try to add peers from peerinfo on startup. This way IF peerinfo writes to file
1491 * then some peers will end up connected.
1493 * Also, find any peers that have zero connections here and set up a task to choose at random another peer in the network to
1494 * 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
1495 * until they get a peer.
1497 /* However, they won't automatically be connected to any of their previous peers... How can we handle that? */
1498 /* So now we have choices: do we want them to come back with all their connections? Probably not, but it solves this mess. */
1500 /* Second problem, which is still a problem, is that a FIND_PEER request won't work when a peer has no connections */
1503 * Okay, so here's how this *should* work now.
1505 * 1. We check the min heap for any peers that have 0 connections.
1506 * a. If any are found, we iterate over the heap and just randomly
1507 * choose another peer and ask testing to please connect the two.
1508 * This takes care of the case that a peer just randomly joins the
1509 * network. However, if there are strict topology restrictions
1510 * (imagine a ring) choosing randomly most likely won't help.
1511 * We make sure the connection attempt doesn't take longer than
1512 * the total timeout, but don't care too much about the result.
1513 * b. After that, we still schedule the find peer requests (concurrently
1514 * with the connect attempts most likely). This handles the case
1515 * that the DHT iterates over peerinfo and just needs to try to send
1516 * a message to get connected. This should handle the case that the
1517 * topology is very strict.
1519 * 2. If all peers have > 0 connections, we still send find peer requests
1520 * as long as possible (until timeout is reached) to help out those
1521 * peers that were newly churned and need more connections. This is because
1522 * once all new peers have established a single connection, they won't be
1525 * 3. Once we reach the timeout, we can do no more. We must schedule the
1526 * next iteration of get requests regardless of connections that peers
1527 * may or may not have.
1529 * Caveat: it would be nice to get peers to take data offline with them and
1530 * come back with it (or not) based on the testing framework. The
1531 * same goes for remembering previous connections, but putting either
1532 * into the general testing churn options seems like overkill because
1533 * these are very specialized cases.
1536 GNUNET_ERROR_TYPE_WARNING,
1537 "Out of %u peers, fewest connections is %d\n",
1538 GNUNET_CONTAINER_heap_get_size (
1539 find_peer_context->peer_min_heap),
1541 if ((peer_count->count == 0)
1542 && (GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime).rel_value
1546 GNUNET_ERROR_TYPE_WARNING,
1547 "Found peer with no connections, will choose some peer(s) at random to connect to!\n");
1548 GNUNET_CONTAINER_heap_iterate (find_peer_context->peer_min_heap,
1549 &iterate_min_heap_peers,
1551 GNUNET_SCHEDULER_add_now (&schedule_churn_find_peer_requests,
1554 else if ((GNUNET_TIME_absolute_get_remaining (find_peer_context->endtime).rel_value
1555 > 0) && (find_peer_context->last_sent != 0))
1557 GNUNET_SCHEDULER_add_now (&schedule_churn_find_peer_requests,
1562 GNUNET_CONTAINER_multihashmap_iterate (find_peer_context->peer_hash,
1565 GNUNET_CONTAINER_multihashmap_destroy (find_peer_context->peer_hash);
1566 GNUNET_CONTAINER_heap_destroy (find_peer_context->peer_min_heap);
1567 GNUNET_free(find_peer_context);
1569 GNUNET_ERROR_TYPE_WARNING,
1570 "Churn round %u of %llu finished, scheduling next GET round.\n",
1571 current_churn_round, churn_rounds);
1572 if (dhtlog_handle != NULL)
1574 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1575 topo_ctx->cont = &do_get;
1576 topo_ctx->cls = all_gets;
1577 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1578 topo_ctx->peers_seen
1579 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1581 = GNUNET_SCHEDULER_add_delayed (
1582 GNUNET_TIME_relative_add (
1583 GNUNET_TIME_relative_add (
1584 DEFAULT_GET_TIMEOUT,
1586 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1588 "from do gets (count_peers_churn_cb)");
1589 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
1594 = GNUNET_SCHEDULER_add_delayed (
1595 GNUNET_TIME_relative_add (
1596 GNUNET_TIME_relative_add (
1597 DEFAULT_GET_TIMEOUT,
1599 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1601 "from do gets (count_peers_churn_cb)");
1602 GNUNET_SCHEDULER_add_now (&do_get, all_gets);
1609 * Set up a single find peer request for each peer in the topology. Do this
1610 * until the settle time is over, limited by the number of outstanding requests
1611 * and the time allowed for each one!
1614 schedule_churn_find_peer_requests(void *cls,
1615 const struct GNUNET_SCHEDULER_TaskContext * tc)
1617 struct FindPeerContext *find_peer_ctx = cls;
1618 struct TestFindPeer *test_find_peer;
1619 struct PeerCount *peer_count;
1622 if (find_peer_ctx->previous_peers == 0) /* First time, go slowly */
1623 find_peer_ctx->total = 1;
1624 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers
1625 < MIN_FIND_PEER_CUTOFF)
1626 find_peer_ctx->total = find_peer_ctx->total / 2;
1627 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers
1628 > MAX_FIND_PEER_CUTOFF) /* Found LOTS of peers, still go slowly */
1629 find_peer_ctx->total = find_peer_ctx->last_sent - (find_peer_ctx->last_sent
1632 find_peer_ctx->total = find_peer_ctx->last_sent * 4;
1634 if (find_peer_ctx->total > max_outstanding_find_peers)
1635 find_peer_ctx->total = max_outstanding_find_peers;
1637 find_peer_ctx->last_sent = find_peer_ctx->total;
1638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1639 "Sending %u find peer messages (after churn)\n",
1640 find_peer_ctx->total);
1642 if (find_peer_ctx->total > 0)
1643 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay,
1644 find_peer_ctx->total);
1647 find_peer_ctx->previous_peers = find_peer_ctx->current_peers;
1648 find_peer_ctx->current_peers = 0;
1649 GNUNET_TESTING_get_topology (pg, &count_peers_churn_cb, find_peer_ctx);
1652 for (i = 0; i < find_peer_ctx->total; i++)
1654 test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
1655 /* If we have sent requests, choose peers with a low number of connections to send requests from */
1657 = GNUNET_CONTAINER_heap_remove_root (find_peer_ctx->peer_min_heap);
1658 GNUNET_assert(peer_count != NULL);
1659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1660 "Sending find peer request from peer with %u connections\n",
1662 GNUNET_CONTAINER_multihashmap_remove (find_peer_ctx->peer_hash,
1663 &peer_count->peer_id.hashPubKey,
1665 test_find_peer->daemon
1666 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
1667 GNUNET_assert(test_find_peer->daemon != NULL);
1668 test_find_peer->find_peer_context = find_peer_ctx;
1669 GNUNET_SCHEDULER_add_delayed (
1670 GNUNET_TIME_relative_multiply (
1673 &send_find_peer_request, test_find_peer);
1676 if ((find_peer_ctx->peer_hash == NULL) && (find_peer_ctx->peer_min_heap
1679 find_peer_ctx->peer_hash
1680 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1681 find_peer_ctx->peer_min_heap
1682 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1686 GNUNET_CONTAINER_multihashmap_iterate (find_peer_ctx->peer_hash,
1687 &remove_peer_count, find_peer_ctx);
1688 GNUNET_CONTAINER_multihashmap_destroy (find_peer_ctx->peer_hash);
1689 find_peer_ctx->peer_hash
1690 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1693 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap_size(find_peer_ctx->peer_hash));
1694 GNUNET_assert(0 == GNUNET_CONTAINER_heap_get_size(find_peer_ctx->peer_min_heap));
1698 schedule_churn_get_topology(void *cls,
1699 const struct GNUNET_SCHEDULER_TaskContext * tc)
1701 struct FindPeerContext *find_peer_context = cls;
1702 GNUNET_TESTING_get_topology (pg, &count_peers_churn_cb, find_peer_context);
1706 * Called when churning of the topology has finished.
1708 * @param cls closure unused
1709 * @param emsg NULL on success, or a printable error on failure
1712 churn_complete(void *cls, const char *emsg)
1714 struct FindPeerContext *find_peer_context = cls;
1715 struct PeerCount *peer_count;
1717 struct GNUNET_TESTING_Daemon *temp_daemon;
1718 struct TopologyIteratorContext *topo_ctx;
1719 struct GNUNET_TIME_Relative calc_timeout;
1724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1725 "Ending test, churning of peers failed with error `%s'", emsg);
1726 GNUNET_SCHEDULER_add_now (&end_badly, (void *) emsg);
1731 * If we switched any peers on, we have to somehow force connect the new peer to
1732 * SOME bootstrap peer in the network. First schedule a task to find all peers
1733 * with no connections, then choose a random peer for each and connect them.
1735 if (find_peer_context != NULL)
1738 GNUNET_ERROR_TYPE_WARNING,
1739 "We have churned on some peers, so we must schedule find peer requests for them!\n");
1741 for (i = 0; i < num_peers; i++)
1743 temp_daemon = GNUNET_TESTING_daemon_get (pg, i);
1744 if (GNUNET_YES == GNUNET_TESTING_daemon_running (temp_daemon))
1746 peer_count = GNUNET_malloc (sizeof(struct PeerCount));
1747 memcpy (&peer_count->peer_id, &temp_daemon->id,
1748 sizeof(struct GNUNET_PeerIdentity));
1749 GNUNET_assert(peer_count->count == 0);
1750 peer_count->heap_node
1751 = GNUNET_CONTAINER_heap_insert (
1752 find_peer_context->peer_min_heap,
1753 peer_count, peer_count->count);
1754 GNUNET_CONTAINER_multihashmap_put (find_peer_context->peer_hash,
1755 &temp_daemon->id.hashPubKey,
1757 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1762 GNUNET_ERROR_TYPE_WARNING,
1763 "Added %d peers to heap, total size %d\n",
1765 GNUNET_CONTAINER_heap_get_size (
1766 find_peer_context->peer_min_heap));
1767 GNUNET_SCHEDULER_add_delayed (DEFAULT_PEER_DISCONNECT_TIMEOUT,
1768 &schedule_churn_get_topology,
1774 GNUNET_ERROR_TYPE_WARNING,
1775 "Only churned off peers, no find peer requests, scheduling more gets (after allowing time for peers to disconnect properly!)...\n");
1776 if (dhtlog_handle != NULL)
1778 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1779 topo_ctx->cont = &do_get;
1780 topo_ctx->cls = all_gets;
1781 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1782 topo_ctx->peers_seen
1783 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1784 calc_timeout = GNUNET_TIME_relative_add (DEFAULT_GET_TIMEOUT,
1787 = GNUNET_TIME_relative_add (calc_timeout,
1788 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT);
1790 = GNUNET_TIME_relative_add (calc_timeout,
1791 DEFAULT_PEER_DISCONNECT_TIMEOUT);
1793 = GNUNET_SCHEDULER_add_delayed (calc_timeout, &end_badly,
1794 "from do gets (churn_complete)");
1795 GNUNET_SCHEDULER_add_delayed (DEFAULT_PEER_DISCONNECT_TIMEOUT,
1796 &capture_current_topology, topo_ctx);
1797 dhtlog_handle->insert_round (DHT_ROUND_GET, rounds_finished);
1801 calc_timeout = GNUNET_TIME_relative_add (DEFAULT_GET_TIMEOUT,
1804 = GNUNET_TIME_relative_add (calc_timeout,
1805 DEFAULT_PEER_DISCONNECT_TIMEOUT);
1807 = GNUNET_SCHEDULER_add_delayed (calc_timeout, &end_badly,
1808 "from do gets (churn_complete)");
1809 GNUNET_SCHEDULER_add_delayed (DEFAULT_PEER_DISCONNECT_TIMEOUT,
1816 * Decide how many peers to turn on or off in this round, make sure the
1817 * numbers actually make sense, then do so. This function sets in motion
1818 * churn, find peer requests for newly joined peers, and issuing get
1819 * requests once the new peers have done so.
1821 * @param cls closure (unused)
1822 * @param tc task context (unused)
1825 churn_peers(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1827 unsigned int count_running;
1828 unsigned int churn_up;
1829 unsigned int churn_down;
1830 struct GNUNET_TIME_Relative timeout;
1831 struct FindPeerContext *find_peer_context;
1833 churn_up = churn_down = 0;
1834 count_running = GNUNET_TESTING_daemons_running (pg);
1835 if (count_running > churn_array[current_churn_round])
1836 churn_down = count_running - churn_array[current_churn_round];
1837 else if (count_running < churn_array[current_churn_round])
1838 churn_up = churn_array[current_churn_round] - count_running;
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Not churning any peers, topology unchanged.\n");
1843 if (churn_up > num_peers - count_running)
1845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1846 "Churn file specified %u peers (up); only have %u!",
1847 churn_array[current_churn_round], num_peers);
1848 churn_up = num_peers - count_running;
1850 else if (churn_down > count_running)
1852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1853 "Churn file specified %u peers (down); only have %u!",
1854 churn_array[current_churn_round], count_running);
1855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1856 "This will leave NO peers running (mistake in churn configuration?)!");
1857 churn_down = count_running;
1859 //timeout = GNUNET_TIME_relative_multiply(seconds_per_peer_start, churn_up > 0 ? churn_up : churn_down);
1860 //timeout = GNUNET_TIME_relative_multiply (seconds_per_peer_start, churn_up > 0 ? churn_up : churn_down);
1861 timeout = GNUNET_TIME_relative_multiply (DEFAULT_TIMEOUT, 2); /* FIXME: Lack of intelligent choice here */
1862 find_peer_context = NULL;
1863 if (churn_up > 0) /* Only need to do find peer requests if we turned new peers on */
1865 find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
1866 find_peer_context->count_peers_cb = &count_peers_churn_cb;
1867 find_peer_context->previous_peers = 0;
1868 find_peer_context->current_peers = 0;
1869 find_peer_context->endtime = GNUNET_TIME_relative_to_absolute (timeout);
1870 find_peer_context->peer_hash
1871 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1872 find_peer_context->peer_min_heap
1873 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1876 GNUNET_ERROR_TYPE_WARNING,
1877 "churn_peers: want %u total, %u running, starting %u, stopping %u\n",
1878 churn_array[current_churn_round], count_running, churn_up,
1880 GNUNET_TESTING_daemons_churn (pg, churn_down, churn_up, timeout,
1881 &churn_complete, find_peer_context);
1882 current_churn_round++;
1886 * Task to release DHT handle associated with GET request.
1889 get_stop_finished(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1891 struct TestGetContext *test_get = cls;
1892 struct TopologyIteratorContext *topo_ctx;
1894 /* The dht_handle may be null if this get was scheduled from a down peer */
1895 if (test_get->dht_handle != NULL)
1897 GNUNET_DHT_disconnect (test_get->dht_handle);
1898 outstanding_gets--; /* GET is really finished */
1899 test_get->dht_handle = NULL;
1902 /* Reset the uid (which item to search for) and the daemon (which peer to search from) for later get request iterations */
1903 if (get_from_same == GNUNET_NO)
1905 test_get->uid = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1908 = GNUNET_TESTING_daemon_get (
1910 GNUNET_CRYPTO_random_u32 (
1911 GNUNET_CRYPTO_QUALITY_WEAK,
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n", gets_completed, gets_failed);
1918 update_meter (get_meter);
1919 if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0))
1923 "Canceling die task (get_stop_finished) %llu gets completed, %llu gets failed\n",
1924 gets_completed, gets_failed);
1925 if ((GNUNET_YES == dhtlog_minimal) && (NULL != dhtlog_handle))
1926 dhtlog_handle->insert_round_details (DHT_ROUND_GET, rounds_finished,
1927 num_gets, gets_completed);
1928 GNUNET_SCHEDULER_cancel (die_task);
1929 reset_meter (put_meter);
1930 reset_meter (get_meter);
1933 * 1) Testing is completely finished, call the topology iteration dealy and die
1934 * 2) Testing is not finished, churn the network and do gets again (current_churn_round < churn_rounds)
1935 * 3) Testing is not finished, reschedule all the PUTS *and* GETS again (num_rounds > 1)
1937 if (rounds_finished == total_rounds - 1) /* Everything is finished, end testing */
1939 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
1941 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1942 topo_ctx->cont = &log_dht_statistics;
1943 topo_ctx->peers_seen
1944 = GNUNET_CONTAINER_multihashmap_create (num_peers);
1945 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
1948 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
1950 else if (current_churn_round < churns_per_round * (rounds_finished + 1)) /* Do next round of churn */
1953 GNUNET_ERROR_TYPE_WARNING,
1954 "Current churn round %u, real round %u, scheduling next round of churn.\n",
1955 current_churn_round, rounds_finished + 1);
1959 if (dhtlog_handle != NULL)
1960 dhtlog_handle->insert_round (DHT_ROUND_CHURN, rounds_finished);
1962 GNUNET_SCHEDULER_add_now (&churn_peers, NULL);
1964 else if (rounds_finished < total_rounds - 1) /* Start a new complete round */
1969 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1970 "Round %u of %llu finished, scheduling next round.\n",
1971 rounds_finished, total_rounds);
1973 /** We reset the peer daemon for puts and gets on each disconnect, so all we need to do is start another round! */
1974 if (GNUNET_YES == in_dht_replication) /* Replication done in DHT, don't redo puts! */
1976 if (dhtlog_handle != NULL)
1977 dhtlog_handle->insert_round (DHT_ROUND_GET, rounds_finished);
1980 = GNUNET_SCHEDULER_add_delayed (
1981 GNUNET_TIME_relative_add (
1982 GNUNET_TIME_relative_add (
1983 GNUNET_TIME_relative_multiply (
1984 GNUNET_TIME_UNIT_SECONDS,
1987 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1989 "from do gets (next round)");
1990 GNUNET_SCHEDULER_add_delayed (
1991 GNUNET_TIME_relative_multiply (
1992 GNUNET_TIME_UNIT_SECONDS,
1998 if (dhtlog_handle != NULL)
1999 dhtlog_handle->insert_round (DHT_ROUND_NORMAL, rounds_finished);
2001 = GNUNET_SCHEDULER_add_delayed (
2002 GNUNET_TIME_relative_add (
2003 GNUNET_TIME_relative_multiply (
2004 GNUNET_TIME_UNIT_SECONDS,
2006 GNUNET_TIME_relative_multiply (
2007 GNUNET_TIME_UNIT_SECONDS,
2010 &end_badly, "from do puts");
2011 GNUNET_SCHEDULER_add_delayed (
2012 GNUNET_TIME_relative_multiply (
2013 GNUNET_TIME_UNIT_SECONDS,
2022 * Task to release get handle.
2025 get_stop_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2027 struct TestGetContext *test_get = cls;
2029 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
2032 cumulative_successful_gets++;
2034 GNUNET_assert(test_get->get_handle != NULL);
2035 GNUNET_DHT_get_stop (test_get->get_handle);
2036 test_get->get_handle = NULL;
2037 test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
2038 GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
2042 * Iterator called if the GET request initiated returns a response.
2044 * @param cls closure
2045 * @param exp when will this value expire
2046 * @param key key of the result
2047 * @param get_path NULL-terminated array of pointers
2048 * to the peers on reverse GET path (or NULL if not recorded)
2049 * @param put_path NULL-terminated array of pointers
2050 * to the peers on the PUT path (or NULL if not recorded)
2051 * @param type type of the result
2052 * @param size number of bytes in data
2053 * @param data pointer to the result data
2056 get_result_iterator(void *cls, struct GNUNET_TIME_Absolute exp,
2057 const GNUNET_HashCode * key,
2058 const struct GNUNET_PeerIdentity * const *get_path,
2059 const struct GNUNET_PeerIdentity * const *put_path,
2060 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
2062 struct TestGetContext *test_get = cls;
2064 if (test_get->succeeded == GNUNET_YES)
2065 return; /* Get has already been successful, probably ending now */
2067 if (0 != memcmp (&known_keys[test_get->uid], key, sizeof(GNUNET_HashCode))) /* || (0 != memcmp(original_data, data, sizeof(original_data))))*/
2070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2071 "Key or data is not the same as was inserted!\n");
2076 test_get->succeeded = GNUNET_YES;
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
2081 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
2082 GNUNET_SCHEDULER_add_continuation (&get_stop_task, test_get,
2083 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
2087 * Set up some data, and call API PUT function
2090 do_get(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2092 struct TestGetContext *test_get = cls;
2096 GNUNET_SCHEDULER_cancel (die_task);
2097 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
2100 if (test_get == NULL)
2101 return; /* End of the list */
2103 /* Set this here in case we are re-running gets */
2104 test_get->succeeded = GNUNET_NO;
2106 if (GNUNET_YES != GNUNET_TESTING_daemon_running (test_get->daemon)) /* If the peer has been churned off, don't try issuing request from it! */
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 "Peer we should issue get request from is down, skipping.\n");
2111 GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
2112 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
2116 /* Check if more gets are outstanding than should be */
2117 if (outstanding_gets > max_outstanding_gets)
2119 GNUNET_SCHEDULER_add_delayed (
2120 GNUNET_TIME_relative_multiply (
2121 GNUNET_TIME_UNIT_MILLISECONDS,
2127 /* Connect to the first peer's DHT */
2128 test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
2129 GNUNET_assert(test_get->dht_handle != NULL);
2132 cumulative_num_gets++;
2133 /* Insert the data at the first peer */
2134 test_get->get_handle = GNUNET_DHT_get_start (test_get->dht_handle, get_delay,
2135 GNUNET_BLOCK_TYPE_TEST,
2136 &known_keys[test_get->uid],
2138 GNUNET_DHT_RO_NONE, NULL, 0,
2139 NULL, 0, &get_result_iterator,
2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
2145 test_get->daemon->shortname);
2147 test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed (get_timeout,
2151 /* Schedule the next request in the linked list of get requests */
2152 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
2156 * Called when the PUT request has been transmitted to the DHT service.
2157 * Schedule the GET request for some time in the future.
2160 put_finished(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2162 struct TestPutContext *test_put = cls;
2163 struct TopologyIteratorContext *topo_ctx;
2167 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
2168 fprintf (stderr, "PUT Request failed!\n");
2170 /* Reset the daemon (which peer to insert at) for later put request iterations */
2171 if (replicate_same == GNUNET_NO)
2173 = GNUNET_TESTING_daemon_get (
2175 GNUNET_CRYPTO_random_u32 (
2176 GNUNET_CRYPTO_QUALITY_WEAK,
2179 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
2180 test_put->disconnect_task = GNUNET_SCHEDULER_add_now (&put_disconnect_task,
2182 if (GNUNET_YES == update_meter (put_meter))
2184 GNUNET_assert(outstanding_puts == 0);
2185 GNUNET_SCHEDULER_cancel (die_task);
2186 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
2188 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2189 topo_ctx->cont = &do_get;
2190 topo_ctx->cls = all_gets;
2191 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
2192 topo_ctx->peers_seen
2193 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2195 = GNUNET_SCHEDULER_add_delayed (
2196 GNUNET_TIME_relative_add (
2197 GNUNET_TIME_relative_add (
2198 DEFAULT_GET_TIMEOUT,
2200 DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
2202 "from do gets (put finished)");
2203 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
2207 fprintf (stderr, "Scheduling die task (put finished)\n");
2209 = GNUNET_SCHEDULER_add_delayed (
2210 GNUNET_TIME_relative_add (
2211 DEFAULT_GET_TIMEOUT,
2214 "from do gets (put finished)");
2215 GNUNET_SCHEDULER_add_delayed (DEFAULT_GET_TIMEOUT, &do_get, all_gets);
2222 * Set up some data, and call API PUT function
2225 do_put(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2227 struct TestPutContext *test_put = cls;
2228 char data[test_data_size]; /* Made up data to store */
2232 if (test_put == NULL)
2233 return; /* End of list */
2235 if (GNUNET_YES != GNUNET_TESTING_daemon_running (test_put->daemon)) /* If the peer has been churned off, don't try issuing request from it! */
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2238 "Peer we should issue put request at is down, skipping.\n");
2239 update_meter (put_meter);
2240 GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
2244 for (i = 0; i < sizeof(data); i++)
2246 memset (&data[i], GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2250 if (outstanding_puts > max_outstanding_puts)
2252 GNUNET_SCHEDULER_add_delayed (
2253 GNUNET_TIME_relative_multiply (
2254 GNUNET_TIME_UNIT_MILLISECONDS,
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
2263 test_put->daemon->shortname);
2265 test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
2267 GNUNET_assert(test_put->dht_handle != NULL);
2269 GNUNET_DHT_put (test_put->dht_handle, &known_keys[test_put->uid],
2270 put_replication, GNUNET_DHT_RO_NONE, GNUNET_BLOCK_TYPE_TEST,
2271 sizeof(data), data, GNUNET_TIME_UNIT_FOREVER_ABS, put_delay,
2272 &put_finished, test_put);
2273 test_put->disconnect_task
2274 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
2275 &put_disconnect_task, test_put);
2276 rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 2);
2277 GNUNET_SCHEDULER_add_delayed (
2278 GNUNET_TIME_relative_multiply (
2279 GNUNET_TIME_UNIT_SECONDS,
2285 schedule_find_peer_requests(void *cls,
2286 const struct GNUNET_SCHEDULER_TaskContext * tc);
2289 setup_malicious_peers(void *cls,
2290 const struct GNUNET_SCHEDULER_TaskContext * tc);
2293 * Given a number of total peers and a bucket size, estimate the number of
2294 * connections in a perfect kademlia topology.
2297 connection_estimate(unsigned int peer_count, unsigned int bucket_size)
2300 unsigned int filled;
2304 while (i >= bucket_size)
2309 filled++; /* Add one filled bucket to account for one "half full" and some miscellaneous */
2310 return filled * bucket_size * peer_count;
2315 * Callback for iterating over all the peer connections of a peer group.
2318 count_peers_cb(void *cls, const struct GNUNET_PeerIdentity *first,
2319 const struct GNUNET_PeerIdentity *second, const char *emsg)
2321 struct FindPeerContext *find_peer_context = cls;
2322 if ((first != NULL) && (second != NULL))
2324 add_new_connection (find_peer_context, first, second);
2325 find_peer_context->current_peers++;
2330 GNUNET_ERROR_TYPE_WARNING,
2331 "Peer count finished (%u connections), %u new peers, connection estimate %u (target %u)\n",
2332 find_peer_context->current_peers,
2333 find_peer_context->current_peers
2334 - find_peer_context->previous_peers,
2335 connection_estimate (num_peers, DEFAULT_BUCKET_SIZE),
2336 target_total_connections);
2338 if ((find_peer_context->last_sent < 8)
2339 || ((find_peer_context->current_peers < 2
2340 * connection_estimate (num_peers, DEFAULT_BUCKET_SIZE))
2341 && (GNUNET_TIME_absolute_get_remaining (
2342 find_peer_context->endtime).rel_value
2343 > 0) && (find_peer_context->current_peers
2344 < target_total_connections)))
2346 GNUNET_SCHEDULER_add_now (&schedule_find_peer_requests,
2351 GNUNET_CONTAINER_multihashmap_iterate (find_peer_context->peer_hash,
2354 GNUNET_CONTAINER_multihashmap_destroy (find_peer_context->peer_hash);
2355 GNUNET_CONTAINER_heap_destroy (find_peer_context->peer_min_heap);
2356 GNUNET_free(find_peer_context);
2357 fprintf (stderr, "Not sending any more find peer requests.\n");
2360 if (GNUNET_YES == malicious_after_settle)
2362 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "calling setup_malicious_peers\n");
2363 GNUNET_SCHEDULER_add_now(&setup_malicious_peers, NULL);
2371 * Set up a single find peer request for each peer in the topology. Do this
2372 * until the settle time is over, limited by the number of outstanding requests
2373 * and the time allowed for each one!
2376 schedule_find_peer_requests(void *cls,
2377 const struct GNUNET_SCHEDULER_TaskContext * tc)
2379 struct FindPeerContext *find_peer_ctx = cls;
2380 struct TestFindPeer *test_find_peer;
2381 struct PeerCount *peer_count;
2385 if (find_peer_ctx->previous_peers == 0) /* First time, go slowly */
2386 find_peer_ctx->total = 1;
2387 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers
2388 > MAX_FIND_PEER_CUTOFF) /* Found LOTS of peers, still go slowly */
2389 find_peer_ctx->total = find_peer_ctx->last_sent - (find_peer_ctx->last_sent
2392 find_peer_ctx->total = find_peer_ctx->last_sent * 2;
2394 if (find_peer_ctx->total > max_outstanding_find_peers)
2395 find_peer_ctx->total = max_outstanding_find_peers;
2397 if (find_peer_ctx->total > num_peers) /* Don't try to send more messages than we have peers! */
2398 find_peer_ctx->total = num_peers;
2400 find_peer_ctx->last_sent = find_peer_ctx->total;
2401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2402 "Sending %u find peer messages (goal at least %u connections)\n",
2403 find_peer_ctx->total, target_total_connections);
2405 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay,
2406 find_peer_ctx->total);
2407 for (i = 0; i < find_peer_ctx->total; i++)
2409 test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
2410 /* If we haven't sent any requests yet, choose random peers */
2411 /* Also choose random in _half_ of all cases, so we don't
2412 * get stuck choosing topologically restricted peers with
2413 * few connections that will never be able to find any new
2415 if ((find_peer_ctx->previous_peers == 0) || (i % 2 == 0))
2418 * Attempt to spread find peer requests across even sections of the peer address
2419 * space. Choose basically 1 peer in every num_peers / max_outstanding_requests
2420 * each time, then offset it by a randomish value.
2422 * For instance, if num_peers is 100 and max_outstanding is 10, first chosen peer
2423 * will be between 0 - 10, second between 10 - 20, etc.
2425 random = (num_peers / find_peer_ctx->total) * i;
2427 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2428 (num_peers / find_peer_ctx->total));
2429 if (random >= num_peers)
2431 random = random - num_peers;
2434 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2436 test_find_peer->daemon = GNUNET_TESTING_daemon_get (pg, random);
2438 else /* If we have sent requests, choose peers with a low number of connections to send requests from */
2441 = GNUNET_CONTAINER_heap_remove_root (find_peer_ctx->peer_min_heap);
2442 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(find_peer_ctx->peer_hash, &peer_count->peer_id.hashPubKey, peer_count));
2443 test_find_peer->daemon
2444 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
2445 GNUNET_assert(test_find_peer->daemon != NULL);
2448 test_find_peer->find_peer_context = find_peer_ctx;
2449 GNUNET_SCHEDULER_add_delayed (
2450 GNUNET_TIME_relative_multiply (
2453 &send_find_peer_request, test_find_peer);
2456 if ((find_peer_ctx->peer_hash == NULL) && (find_peer_ctx->peer_min_heap
2459 find_peer_ctx->peer_hash
2460 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2461 find_peer_ctx->peer_min_heap
2462 = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2466 GNUNET_CONTAINER_multihashmap_iterate (find_peer_ctx->peer_hash,
2467 &remove_peer_count, find_peer_ctx);
2468 GNUNET_CONTAINER_multihashmap_destroy (find_peer_ctx->peer_hash);
2469 find_peer_ctx->peer_hash
2470 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2473 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap_size(find_peer_ctx->peer_hash));
2474 GNUNET_assert(0 == GNUNET_CONTAINER_heap_get_size(find_peer_ctx->peer_min_heap));
2479 * Convert unique ID to hash code.
2481 * @param uid unique ID to convert
2482 * @param hash set to uid (extended with zeros)
2485 hash_from_uid(uint32_t uid, GNUNET_HashCode *hash)
2487 memset (hash, 0, sizeof(GNUNET_HashCode));
2488 *((uint32_t *) hash) = uid;
2492 * Set up all of the put and get operations we want to do
2493 * in the current round. Allocate data structure for each,
2494 * add to list, then schedule the actual PUT operations.
2497 setup_puts_and_gets(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2500 struct TestPutContext *test_put;
2501 struct TestGetContext *test_get;
2503 GNUNET_HashCode uid_hash;
2506 int remember[num_puts][num_peers];
2507 memset(&remember, 0, sizeof(int) * num_puts * num_peers);
2509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "in setup_puts_and_gets\n");
2510 known_keys = GNUNET_malloc(sizeof(GNUNET_HashCode) * num_puts);
2511 for (i = 0; i < num_puts; i++)
2513 test_put = GNUNET_malloc(sizeof(struct TestPutContext));
2515 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
2517 /* Set first X bits to match the chosen sybil location if we want to do the sybil attack! */
2518 if (GNUNET_YES == malicious_sybil)
2520 memcpy (&known_keys[i], &sybil_target, sizeof(GNUNET_HashCode) / 2);
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 "Distance between sybil location and key is %d\n",
2523 GNUNET_CRYPTO_hash_matching_bits (&known_keys[i],
2526 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2528 test_put->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2529 /* Don't start PUTs at malicious peers! */
2530 if (malicious_bloom != NULL)
2533 hash_from_uid (temp_peer, &uid_hash);
2535 == GNUNET_CONTAINER_bloomfilter_test (malicious_bloom, &uid_hash))
2536 && (count < num_peers))
2538 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2540 hash_from_uid (temp_peer, &uid_hash);
2541 test_put->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2544 if (count == num_peers)
2545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2546 "Couldn't find peer not in malicious bloom to select!\n");
2549 test_put->next = all_puts;
2550 all_puts = test_put;
2553 for (i = 0; i < num_gets; i++)
2555 test_get = GNUNET_malloc(sizeof(struct TestGetContext));
2556 test_get->uid = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2559 while (remember[test_get->uid][temp_daemon] == 1)
2560 temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2561 remember[test_get->uid][temp_daemon] = 1;
2563 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2565 test_get->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2566 /* Don't start GETs at malicious peers! */
2567 if (malicious_bloom != NULL)
2569 hash_from_uid (temp_peer, &uid_hash);
2572 == GNUNET_CONTAINER_bloomfilter_test (malicious_bloom, &uid_hash))
2573 && (count < num_peers))
2575 temp_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
2577 hash_from_uid (temp_peer, &uid_hash);
2578 test_get->daemon = GNUNET_TESTING_daemon_get (pg, temp_peer);
2581 if (count == num_peers)
2582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2583 "Couldn't find peer not in malicious bloom to select!\n");
2585 test_get->next = all_gets;
2586 all_gets = test_get;
2589 /*GNUNET_SCHEDULER_cancel (die_task);*/
2591 = GNUNET_SCHEDULER_add_delayed (
2592 GNUNET_TIME_relative_multiply (
2593 GNUNET_TIME_UNIT_SECONDS,
2596 &end_badly, "from do puts");
2597 GNUNET_SCHEDULER_add_now (&do_put, all_puts);
2602 * Set up some all of the put and get operations we want
2603 * to do. Allocate data structure for each, add to list,
2604 * then call actual insert functions.
2607 continue_puts_and_gets(void *cls,
2608 const struct GNUNET_SCHEDULER_TaskContext * tc)
2612 struct TopologyIteratorContext *topo_ctx;
2613 struct FindPeerContext *find_peer_context;
2614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "In continue_puts_and_gets\n");
2615 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
2617 if (settle_time >= 180 * 2)
2618 max = (settle_time / 180) - 2;
2621 for (i = 1; i < max; i++)
2623 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2624 topo_ctx->current_iteration = i;
2625 topo_ctx->total_iterations = max;
2626 topo_ctx->peers_seen
2627 = GNUNET_CONTAINER_multihashmap_create (num_peers);
2628 //fprintf(stderr, "scheduled topology iteration in %d minutes\n", i);
2629 GNUNET_SCHEDULER_add_delayed (
2630 GNUNET_TIME_relative_multiply (
2631 GNUNET_TIME_UNIT_MINUTES,
2633 &capture_current_topology, topo_ctx);
2635 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2636 topo_ctx->cont = &setup_puts_and_gets;
2637 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create (num_peers);
2638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2639 "setting setup_puts_and_gets for %d seconds in the future\n",
2641 GNUNET_SCHEDULER_add_delayed (
2642 GNUNET_TIME_relative_multiply (
2643 GNUNET_TIME_UNIT_SECONDS,
2646 &capture_current_topology, topo_ctx);
2649 GNUNET_SCHEDULER_add_delayed (
2650 GNUNET_TIME_relative_multiply (
2651 GNUNET_TIME_UNIT_SECONDS,
2654 &setup_puts_and_gets, NULL);
2656 if (dhtlog_handle != NULL)
2657 dhtlog_handle->insert_round (DHT_ROUND_NORMAL, rounds_finished);
2660 if ((GNUNET_YES != malicious_after_settle) || (settle_time == 0))
2662 GNUNET_SCHEDULER_add_now(&setup_malicious_peers, NULL);
2666 if ((GNUNET_YES == do_find_peer) && (settle_time > 0))
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2669 "Scheduling find peer requests during \"settle\" time.\n");
2670 find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
2671 find_peer_context->count_peers_cb = &count_peers_cb;
2672 find_peer_context->endtime
2673 = GNUNET_TIME_relative_to_absolute (
2674 GNUNET_TIME_relative_multiply (
2675 GNUNET_TIME_UNIT_SECONDS,
2677 GNUNET_SCHEDULER_add_now (&schedule_find_peer_requests, find_peer_context);
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 "Assuming automatic DHT find peer requests.\n");
2687 * Task to release DHT handles
2690 malicious_disconnect_task(void *cls,
2691 const struct GNUNET_SCHEDULER_TaskContext * tc)
2693 struct MaliciousContext *ctx = cls;
2694 outstanding_malicious--;
2695 malicious_completed++;
2696 ctx->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
2697 GNUNET_DHT_disconnect (ctx->dht_handle);
2698 ctx->dht_handle = NULL;
2701 if (malicious_completed == malicious_getters + malicious_putters
2702 + malicious_droppers)
2704 fprintf (stderr, "Finished setting all malicious peers up!\n");
2709 * Task to release DHT handles
2712 malicious_done_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2714 struct MaliciousContext *ctx = cls;
2715 GNUNET_SCHEDULER_cancel (ctx->disconnect_task);
2716 GNUNET_SCHEDULER_add_now (&malicious_disconnect_task, ctx);
2720 * Set up some data, and call API PUT function
2723 set_malicious(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2725 struct MaliciousContext *ctx = cls;
2727 if (outstanding_malicious > DEFAULT_MAX_OUTSTANDING_GETS)
2729 GNUNET_SCHEDULER_add_delayed (
2730 GNUNET_TIME_relative_multiply (
2731 GNUNET_TIME_UNIT_MILLISECONDS,
2733 &set_malicious, ctx);
2737 if (ctx->dht_handle == NULL)
2739 ctx->dht_handle = GNUNET_DHT_connect (ctx->daemon->cfg, 1);
2740 outstanding_malicious++;
2743 GNUNET_assert(ctx->dht_handle != NULL);
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting peer %s malicious type %d\n",
2747 ctx->daemon->shortname, ctx->malicious_type);
2750 switch (ctx->malicious_type)
2752 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
2753 GNUNET_DHT_set_malicious_getter (ctx->dht_handle, malicious_get_frequency, &malicious_done_task, ctx);
2755 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
2756 GNUNET_DHT_set_malicious_putter (ctx->dht_handle, malicious_put_frequency, &malicious_done_task, ctx);
2758 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
2759 GNUNET_DHT_set_malicious_dropper (ctx->dht_handle, &malicious_done_task, ctx);
2765 ctx->disconnect_task
2766 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2767 &malicious_disconnect_task, ctx);
2772 * Choose the next peer from the peer group to set as malicious.
2773 * If we are doing a sybil attack, find the nearest peer to the
2774 * sybil location that has not already been set malicious. Otherwise
2775 * just choose a random not already chosen peer.
2777 * @param pg the peer group
2778 * @param bloom the bloomfilter which contains all peer already
2779 * chosen to be malicious
2782 choose_next_malicious (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_CONTAINER_BloomFilter *bloom)
2789 struct GNUNET_TESTING_Daemon *temp_daemon;
2790 GNUNET_HashCode uid_hash;
2794 GNUNET_assert (bloom != NULL);
2796 if (GNUNET_YES == malicious_sybil)
2798 for (i = 0; i < num_peers; i++)
2800 temp_daemon = GNUNET_TESTING_daemon_get(pg, i);
2801 hash_from_uid(i, &uid_hash);
2802 /* Check if this peer matches the bloomfilter */
2803 if ((GNUNET_NO == GNUNET_TESTING_daemon_running(temp_daemon)) || (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &uid_hash)))
2806 bits_match = GNUNET_CRYPTO_hash_matching_bits (&temp_daemon->id.hashPubKey, &sybil_target);
2807 if (bits_match >= curr_distance)
2809 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);
2811 curr_distance = bits_match;
2817 nearest = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2818 hash_from_uid(nearest, &uid_hash);
2820 while ((GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &uid_hash)) && (count < num_peers))
2822 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %d already in bloom (tried %d times)\n", nearest, count);
2823 nearest = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2824 hash_from_uid(nearest, &uid_hash);
2827 if (count == num_peers)
2828 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Tried %d times to find a peer, selecting %d at random!!\n", count, nearest);
2835 * Select randomly from set of known peers,
2836 * set the desired number of peers to the
2837 * proper malicious types.
2840 setup_malicious_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2842 struct MaliciousContext *ctx;
2844 uint32_t temp_daemon;
2845 GNUNET_HashCode uid_hash;
2847 for (i = 0; i < malicious_getters; i++)
2849 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2850 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2851 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2852 hash_from_uid(temp_daemon, &uid_hash);
2853 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2854 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET;
2855 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2859 for (i = 0; i < malicious_putters; i++)
2861 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2862 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2863 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2864 hash_from_uid(temp_daemon, &uid_hash);
2865 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2866 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT;
2867 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2871 for (i = 0; i < malicious_droppers; i++)
2873 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2874 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2875 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2876 hash_from_uid(temp_daemon, &uid_hash);
2877 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2878 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP;
2879 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2885 /* Forward declaration */
2887 topology_callback (void *cls,
2888 const struct GNUNET_PeerIdentity *first,
2889 const struct GNUNET_PeerIdentity *second,
2891 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2892 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2893 struct GNUNET_TESTING_Daemon *first_daemon,
2894 struct GNUNET_TESTING_Daemon *second_daemon,
2898 * Retry connecting two specific peers until they connect,
2899 * at a specific interval. These two peers previously failed
2900 * to connect, and we hope they continue to so that we can
2901 * debug the reason they are having issues.
2904 repeat_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2907 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Repeating connect attempt between %s and %s.\n", repeat_connect_peer1->shortname, repeat_connect_peer2->shortname);
2908 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer 1 configuration `%s'\n", repeat_connect_peer1->cfgfile);
2909 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer 2 configuration `%s'\n", repeat_connect_peer2->cfgfile);
2911 repeat_connect_task = GNUNET_SCHEDULER_NO_TASK;
2912 GNUNET_TESTING_daemons_connect(repeat_connect_peer1,
2913 repeat_connect_peer2,
2914 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
2915 2, &topology_callback, NULL);
2920 * This function is called whenever a connection attempt is finished between two of
2921 * the started peers (started with GNUNET_TESTING_daemons_start). The total
2922 * number of times this function is called should equal the number returned
2923 * from the GNUNET_TESTING_connect_topology call.
2925 * The emsg variable is NULL on success (peers connected), and non-NULL on
2926 * failure (peers failed to connect).
2929 topology_callback(void *cls, const struct GNUNET_PeerIdentity *first,
2930 const struct GNUNET_PeerIdentity *second, uint32_t distance,
2931 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2932 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2933 struct GNUNET_TESTING_Daemon *first_daemon,
2934 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
2936 struct TopologyIteratorContext *topo_ctx;
2937 unsigned long long duration;
2938 unsigned long long total_duration;
2939 unsigned int new_connections;
2940 unsigned int new_failed_connections;
2941 double conns_per_sec_recent;
2942 double conns_per_sec_total;
2943 double failed_conns_per_sec_recent;
2944 double failed_conns_per_sec_total;
2945 char *temp_conn_string;
2946 char *temp_conn_failed_string;
2950 if (repeat_connect_mode == GNUNET_YES)
2952 if ((first_daemon == repeat_connect_peer1) &&
2953 (second_daemon == repeat_connect_peer2))
2955 if (emsg != NULL) /* Peers failed to connect again! */
2957 GNUNET_assert(repeat_connect_task == GNUNET_SCHEDULER_NO_TASK);
2958 repeat_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &repeat_connect, NULL);
2961 else /* Repeat peers actually connected! */
2963 if (repeat_connect_task != GNUNET_SCHEDULER_NO_TASK)
2964 GNUNET_SCHEDULER_cancel(repeat_connect_task);
2965 repeat_connect_peer1 = NULL;
2966 repeat_connect_peer2 = NULL;
2967 repeat_connect_mode = GNUNET_NO;
2968 GNUNET_TESTING_resume_connections(pg);
2969 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Resuming normal connection mode, debug connection was successful!\n");
2975 if (GNUNET_TIME_absolute_get_difference (connect_last_time,
2976 GNUNET_TIME_absolute_get ()).rel_value
2977 > GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
2978 CONN_UPDATE_DURATION).rel_value)
2980 /* Get number of new connections */
2981 new_connections = total_connections - previous_connections;
2983 /* Get number of new FAILED connections */
2984 new_failed_connections = failed_connections - previous_failed_connections;
2986 /* Get duration in seconds */
2988 = GNUNET_TIME_absolute_get_difference (connect_last_time,
2989 GNUNET_TIME_absolute_get ()).rel_value
2992 = GNUNET_TIME_absolute_get_difference (connect_start_time,
2993 GNUNET_TIME_absolute_get ()).rel_value
2996 failed_conns_per_sec_recent = (double) new_failed_connections / duration;
2997 failed_conns_per_sec_total = (double) failed_connections / total_duration;
2998 conns_per_sec_recent = (double) new_connections / duration;
2999 conns_per_sec_total = (double) total_connections / total_duration;
3001 GNUNET_ERROR_TYPE_WARNING,
3002 "Recent: %.2f/s, Total: %.2f/s, Recent failed: %.2f/s, total failed %.2f/s\n",
3003 conns_per_sec_recent, CONN_UPDATE_DURATION,
3004 conns_per_sec_total, failed_conns_per_sec_recent,
3005 failed_conns_per_sec_total);
3006 connect_last_time = GNUNET_TIME_absolute_get ();
3007 previous_connections = total_connections;
3008 previous_failed_connections = failed_connections;
3009 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3010 "have %u total_connections, %u failed\n", total_connections,
3011 failed_connections);
3013 /* These conditions likely mean we've entered the death spiral of doom */
3014 if ((total_connections > 20000) &&
3015 (conns_per_sec_recent < 5.0) &&
3016 (conns_per_sec_total > 10.0) &&
3018 (repeat_connect_mode == GNUNET_NO))
3020 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Entering repeat connection attempt mode!\n");
3021 repeat_connect_peer1 = first_daemon;
3022 repeat_connect_peer2 = second_daemon;
3023 repeat_connect_mode = GNUNET_YES;
3024 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping NEW connections from being scheduled!\n");
3025 GNUNET_TESTING_stop_connections(pg);
3026 repeat_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &repeat_connect, NULL);
3033 total_connections++;
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
3036 first_daemon->shortname,
3037 second_daemon->shortname,
3043 failed_connections++;
3045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to connect peer %s to peer %s with error :\n%s\n",
3046 first_daemon->shortname,
3047 second_daemon->shortname, emsg);
3049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
3050 first_daemon->shortname,
3051 second_daemon->shortname, emsg);
3056 if ((repeat_connect_mode == GNUNET_YES) )
3060 GNUNET_assert(peer_connect_meter != NULL);
3061 if (GNUNET_YES == update_meter (peer_connect_meter))
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3065 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
3068 if (failed_connections > 0)
3069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3070 "While connecting, had %u failed connections.\n",
3071 failed_connections);
3072 if (dhtlog_handle != NULL)
3074 dhtlog_handle->update_connections (total_connections);
3075 dhtlog_handle->insert_topology (expected_connections);
3079 = GNUNET_TIME_absolute_get_difference (connect_start_time,
3080 GNUNET_TIME_absolute_get ()).rel_value
3082 failed_conns_per_sec_total = (double) failed_connections / total_duration;
3083 conns_per_sec_total = (double) total_connections / total_duration;
3084 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3085 "Overall connection info --- Total: %u, Total Failed %u/s\n",
3086 total_connections, failed_connections);
3088 GNUNET_ERROR_TYPE_WARNING,
3089 "Overall connection info --- Total: %.2f/s, Total Failed %.2f/s\n",
3090 conns_per_sec_total, failed_conns_per_sec_total);
3092 GNUNET_asprintf (&temp_conn_string,
3093 "DHT Profiler Connection (trial %d)",
3095 GNUNET_asprintf (&temp_conn_failed_string,
3096 "DHT Profiler Connection failed (trial %d)",
3098 GNUNET_asprintf (&revision_str, "%llu", revision);
3099 GAUGER_ID("DHT_TESTING", temp_conn_string, conns_per_sec_total, "conns/s", revision_str);
3100 GAUGER_ID("DHT_TESTING", temp_conn_failed_string, failed_conns_per_sec_total, "failed_conns", revision_str);
3101 GNUNET_free(temp_conn_string);
3102 GNUNET_free(temp_conn_failed_string);
3103 GNUNET_asprintf (&temp_conn_string,
3104 "DHT Profiler Total Connections (trial %d)",
3107 &temp_conn_failed_string,
3108 "DHT Profiler Total Connections failed (trial %d)",
3110 GAUGER_ID("DHT_TESTING", temp_conn_string, total_connections, "conns", revision_str);
3111 GAUGER_ID("DHT_TESTING", temp_conn_failed_string, failed_connections, "failed conns", revision_str);
3112 GNUNET_free(temp_conn_string);
3113 GNUNET_free(temp_conn_failed_string);
3114 GNUNET_free(revision_str);
3116 GNUNET_SCHEDULER_cancel (die_task);
3118 if ((GNUNET_YES == dhtlog_minimal) && (NULL != dhtlog_handle))
3120 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
3121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3122 "Setting continue gets and puts as topo_cont\n");
3123 topo_ctx->cont = &continue_puts_and_gets;
3124 topo_ctx->peers_seen
3125 = GNUNET_CONTAINER_multihashmap_create (num_peers);
3126 GNUNET_SCHEDULER_add_now (&capture_current_topology, topo_ctx);
3131 GNUNET_ERROR_TYPE_WARNING,
3132 "For some reason, NOT scheduling final topology capture (settle_time %d, dhtlog_handle %s)!\n",
3133 settle_time, dhtlog_handle);
3134 GNUNET_SCHEDULER_add_now (&continue_puts_and_gets, NULL);
3137 else if (total_connections + failed_connections == expected_connections)
3139 GNUNET_SCHEDULER_cancel (die_task);
3141 = GNUNET_SCHEDULER_add_now (&end_badly,
3142 "from topology_callback (too many failed connections)");
3147 peers_started_callback(void *cls, const struct GNUNET_PeerIdentity *id,
3148 const struct GNUNET_CONFIGURATION_Handle *cfg,
3149 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3154 "Failed to start daemon with error: `%s'\n", emsg);
3157 GNUNET_assert (id != NULL);
3160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
3161 (num_peers - peers_left) + 1, num_peers);
3166 if (GNUNET_YES == update_meter (peer_start_meter))
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3170 "All %d daemons started, now connecting peers!\n",
3173 GNUNET_SCHEDULER_cancel (die_task);
3175 expected_connections = UINT_MAX;
3176 if ((pg != NULL) && (peers_left == 0))
3178 connect_start_time = GNUNET_TIME_absolute_get ();
3179 expected_connections
3180 = GNUNET_TESTING_connect_topology (
3183 connect_topology_option,
3184 connect_topology_option_modifier,
3186 connect_attempts, NULL, NULL);
3188 peer_connect_meter = create_meter (expected_connections,
3189 "Peer connection ", GNUNET_YES);
3190 fprintf (stderr, "Have %d expected connections\n",
3191 expected_connections);
3194 if (expected_connections == GNUNET_SYSERR)
3197 = GNUNET_SCHEDULER_add_now (&end_badly,
3198 "from connect topology (bad return)");
3202 = GNUNET_SCHEDULER_add_delayed (
3203 GNUNET_TIME_relative_multiply (
3204 GNUNET_TIME_UNIT_SECONDS,
3205 DEFAULT_CONNECT_TIMEOUT
3206 * expected_connections),
3208 "from connect topology (timeout)");
3217 peers_left = num_peers; /* Reset counter */
3218 if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology,
3219 blacklist_transports) != GNUNET_SYSERR)
3221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3222 "Topology set up, now starting peers!\n");
3223 GNUNET_TESTING_daemons_continue_startup (pg);
3227 GNUNET_SCHEDULER_cancel (die_task);
3228 die_task = GNUNET_SCHEDULER_add_now (&end_badly,
3229 "from create topology (bad return)");
3231 GNUNET_free_non_null(blacklist_transports);
3232 GNUNET_SCHEDULER_cancel (die_task);
3234 = GNUNET_SCHEDULER_add_delayed (
3235 GNUNET_TIME_relative_multiply (
3236 seconds_per_peer_start,
3239 "from continue startup (timeout)");
3243 * Callback indicating that the hostkey was created for a peer.
3246 * @param id the peer identity
3247 * @param d the daemon handle (pretty useless at this point, remove?)
3248 * @param emsg non-null on failure
3251 hostkey_callback(void *cls, const struct GNUNET_PeerIdentity *id,
3252 struct GNUNET_TESTING_Daemon *d, const char *emsg)
3256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3257 "Hostkey callback received error: %s\n", emsg);
3261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3262 "Hostkey (%d/%d) created for peer `%s'\n",
3263 num_peers - peers_left, num_peers, GNUNET_i2s(id));
3267 if (GNUNET_YES == update_meter (hostkey_meter))
3269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3270 "All %d hostkeys created, now creating topology!\n",
3272 GNUNET_SCHEDULER_cancel (die_task);
3273 /* Set up task in case topology creation doesn't finish
3274 * within a reasonable amount of time */
3275 die_task = GNUNET_SCHEDULER_add_delayed (DEFAULT_TOPOLOGY_TIMEOUT,
3277 "from create_topology");
3278 GNUNET_SCHEDULER_add_now (&create_topology, NULL);
3284 run(void *cls, char * const *args, const char *cfgfile,
3285 const struct GNUNET_CONFIGURATION_Handle *cfg)
3288 struct GNUNET_DHTLOG_TrialInfo trial_info;
3289 struct GNUNET_TESTING_Host *hosts;
3290 struct GNUNET_TESTING_Host *temphost;
3291 struct GNUNET_TESTING_Host *tempnext;
3293 char *connect_topology_str;
3294 char *blacklist_topology_str;
3295 char *connect_topology_option_str;
3296 char *connect_topology_option_modifier_string;
3298 char *topology_percentage_str;
3299 float topology_percentage;
3300 char *topology_probability_str;
3302 float topology_probability;
3303 unsigned long long temp_config_number;
3306 int strict_kademlia;
3310 char *churn_filename;
3317 rounds_finished = 0;
3318 memset (&trial_info, 0, sizeof(struct GNUNET_DHTLOG_TrialInfo));
3319 /* Get path from configuration file */
3320 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "paths",
3328 /* Get number of peers to start from configuration */
3329 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3334 GNUNET_ERROR_TYPE_WARNING,
3335 "Number of peers must be specified in section %s option %s\n",
3336 "TESTING", "NUM_PEERS");
3338 GNUNET_assert(num_peers > 0 && num_peers < ULONG_MAX);
3340 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3342 &temp_config_number))
3343 connect_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3344 temp_config_number);
3347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3348 "testing", "connect_timeout");
3352 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3357 "testing", "connect_attempts");
3362 != GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3363 "max_outstanding_connections",
3364 &max_outstanding_connections))
3366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3367 "testing", "max_outstanding_connections");
3371 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
3372 "max_concurrent_ssh",
3373 &max_concurrent_ssh))
3375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n",
3376 "testing", "max_concurrent_ssh");
3381 * Get DHT specific testing options.
3383 if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3386 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3387 "mysql_logging_extended"))
3389 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3390 "mysql_logging_minimal")))
3393 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht_testing",
3394 "mysql_logging_minimal"))
3395 dhtlog_minimal = GNUNET_YES;
3397 dhtlog_handle = GNUNET_DHTLOG_connect (cfg);
3398 if (dhtlog_handle == NULL)
3400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3401 "Could not connect to mysql server for logging, will NOT log dht operations!");
3407 stop_closest = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3409 if (stop_closest == GNUNET_SYSERR)
3410 stop_closest = GNUNET_NO;
3412 stop_found = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht", "stop_found");
3413 if (stop_found == GNUNET_SYSERR)
3414 stop_found = GNUNET_NO;
3416 strict_kademlia = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3418 if (strict_kademlia == GNUNET_SYSERR)
3419 strict_kademlia = GNUNET_NO;
3421 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing",
3424 trialmessage = NULL;
3427 /** Check for a churn file to do churny simulation */
3428 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing",
3432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Reading churn data from %s\n",
3434 if (GNUNET_OK != GNUNET_DISK_file_test (churn_filename))
3436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Error reading churn file!\n");
3437 GNUNET_free_non_null(trialmessage);
3438 GNUNET_free(churn_filename);
3441 if ((0 != STAT (churn_filename, &frstat)) || (frstat.st_size == 0))
3443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3444 "Could not open file specified for churn data, ending test!");
3446 GNUNET_free_non_null(trialmessage);
3447 GNUNET_free(churn_filename);
3451 churn_data = GNUNET_malloc_large (frstat.st_size);
3452 GNUNET_assert(churn_data != NULL);
3453 if (frstat.st_size != GNUNET_DISK_fn_read (churn_filename, churn_data,
3457 GNUNET_ERROR_TYPE_ERROR,
3458 "Could not read file %s specified for churn, ending test!",
3460 GNUNET_free (churn_filename);
3461 GNUNET_free (churn_data);
3462 GNUNET_free_non_null(trialmessage);
3466 GNUNET_free_non_null(churn_filename);
3470 /* Read the first line */
3471 while (count < frstat.st_size)
3474 if (((churn_data[count] == '\n')) && (buf != &churn_data[count]))
3476 churn_data[count] = '\0';
3477 if (1 != sscanf (buf, "%u", &churn_rounds))
3479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3480 "Failed to read number of rounds from churn file, ending test!\n");
3482 GNUNET_free_non_null(trialmessage);
3483 GNUNET_free_non_null(churn_data);
3486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3487 "Read %u rounds from churn file\n", churn_rounds);
3488 buf = &churn_data[count + 1];
3489 churn_array = GNUNET_malloc(sizeof(unsigned int) * churn_rounds);
3490 break; /* Done with this part */
3495 != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3499 churns_per_round = (unsigned long long) churn_rounds;
3503 while ((count < frstat.st_size) && (line_number < churn_rounds))
3506 if (((churn_data[count] == '\n')) && (buf != &churn_data[count]))
3508 churn_data[count] = '\0';
3510 ret = sscanf (buf, "%u", &churn_array[line_number]);
3513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3514 "Read %u peers in round %u\n",
3515 churn_array[line_number], line_number);
3520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3521 "Error reading line `%s' in hostfile\n", buf);
3522 buf = &churn_data[count + 1];
3525 buf = &churn_data[count + 1];
3527 else if (churn_data[count] == '\n') /* Blank line */
3528 buf = &churn_data[count + 1];
3531 GNUNET_free_non_null(churn_data);
3533 /* Check for a hostfile containing user@host:port triples */
3535 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
3542 if (hostfile != NULL)
3544 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
3545 GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
3546 | GNUNET_DISK_PERM_USER_WRITE);
3547 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
3549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3550 "Could not open file specified for host list, ending test!");
3552 GNUNET_free_non_null(trialmessage);
3553 GNUNET_free(hostfile);
3557 data = GNUNET_malloc_large (frstat.st_size);
3558 GNUNET_assert(data != NULL);
3560 != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
3563 GNUNET_ERROR_TYPE_ERROR,
3564 "Could not read file %s specified for host list, ending test!",
3566 GNUNET_free (hostfile);
3568 GNUNET_free_non_null(trialmessage);
3572 GNUNET_free_non_null(hostfile);
3576 while (count < frstat.st_size - 1)
3579 if (((data[count] == '\n')) && (buf != &data[count]))
3582 temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
3583 ret = sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
3584 &temphost->username, &temphost->hostname,
3589 GNUNET_ERROR_TYPE_DEBUG,
3590 "Successfully read host %s, port %d and user %s from file\n",
3591 temphost->hostname, temphost->port,
3592 temphost->username);
3596 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3597 "Error reading line `%s' in hostfile\n", buf);
3598 GNUNET_free(temphost);
3599 buf = &data[count + 1];
3602 temphost->next = hosts;
3604 buf = &data[count + 1];
3606 else if ((data[count] == '\n') || (data[count] == '\0'))
3607 buf = &data[count + 1];
3610 GNUNET_free_non_null(data);
3611 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3612 "malicious_getters",
3613 &malicious_getters))
3614 malicious_getters = 0;
3616 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3617 "malicious_putters",
3618 &malicious_putters))
3619 malicious_putters = 0;
3621 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3622 "malicious_droppers",
3623 &malicious_droppers))
3624 malicious_droppers = 0;
3626 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3631 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
3635 num_puts = num_peers;
3638 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3641 put_replication = DEFAULT_PUT_REPLICATION;
3643 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
3647 num_gets = num_peers;
3650 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3653 get_replication = DEFAULT_GET_REPLICATION;
3655 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3657 &temp_config_number))
3658 find_peer_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3659 temp_config_number);
3661 find_peer_delay = DEFAULT_FIND_PEER_DELAY;
3664 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3665 "concurrent_find_peers",
3666 &temp_config_number))
3667 max_outstanding_find_peers = temp_config_number;
3669 max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
3671 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3673 &temp_config_number))
3674 get_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3675 temp_config_number);
3677 get_timeout = DEFAULT_GET_TIMEOUT;
3679 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3681 &temp_config_number))
3682 max_outstanding_puts = temp_config_number;
3684 max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
3686 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3688 &temp_config_number))
3689 max_outstanding_gets = temp_config_number;
3691 max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
3693 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3695 &temp_config_number))
3696 all_get_timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3697 temp_config_number);
3699 all_get_timeout.rel_value = get_timeout.rel_value * num_gets;
3701 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3703 &temp_config_number))
3704 get_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3705 temp_config_number);
3707 get_delay = DEFAULT_GET_DELAY;
3709 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3711 &temp_config_number))
3712 put_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3713 temp_config_number);
3715 put_delay = DEFAULT_PUT_DELAY;
3717 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3718 "peer_start_timeout",
3719 &temp_config_number))
3720 seconds_per_peer_start
3721 = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3722 temp_config_number);
3724 seconds_per_peer_start = DEFAULT_SECONDS_PER_PEER_START;
3726 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3728 &temp_config_number))
3729 test_data_size = temp_config_number;
3731 test_data_size = DEFAULT_TEST_DATA_SIZE;
3734 * Get testing related options.
3736 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3738 replicate_same = GNUNET_YES;
3741 * Get testing related options.
3743 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3745 get_from_same = GNUNET_YES;
3748 == GNUNET_CONFIGURATION_get_value_time (cfg, "DHT_TESTING",
3749 "MALICIOUS_GET_FREQUENCY",
3750 &malicious_get_frequency))
3751 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
3754 == GNUNET_CONFIGURATION_get_value_time (cfg, "DHT_TESTING",
3755 "MALICIOUS_PUT_FREQUENCY",
3756 &malicious_put_frequency))
3757 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
3760 == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3761 "MALICIOUS_AFTER_SETTLE"))
3762 malicious_after_settle = GNUNET_YES;
3764 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT_TESTING",
3767 /* Set up the malicious target at random for this round */
3768 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
3770 malicious_sybil = GNUNET_YES;
3773 /* Create the bloomfilter for choosing which peers to set malicious */
3775 /* Bloomfilter size must be 2^k for some integer k */
3777 while (1 << k < malicious_droppers)
3779 if (malicious_droppers > 0)
3780 malicious_bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, 1 << k,
3783 /* The normal behavior of the DHT is to do find peer requests
3784 * on its own. Only if this is explicitly turned off should
3785 * the testing driver issue find peer requests (even though
3786 * this is likely the default when testing).
3788 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3790 do_find_peer = GNUNET_YES;
3792 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3794 in_dht_replication = GNUNET_YES;
3796 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3801 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3806 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3808 &temp_config_number))
3809 find_peer_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3810 temp_config_number);
3812 find_peer_delay = DEFAULT_FIND_PEER_DELAY;
3814 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3820 == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3821 "OUTSTANDING_FIND_PEERS",
3822 &max_outstanding_find_peers))
3823 max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
3825 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg, "dht",
3827 max_outstanding_find_peers = max_outstanding_find_peers * 1;
3829 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay,
3830 max_outstanding_find_peers);
3832 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (cfg,
3839 == GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing",
3840 "target_total_connections",
3841 &target_total_connections))
3842 || (target_total_connections == 0))
3843 target_total_connections = connection_estimate (num_peers,
3844 DEFAULT_BUCKET_SIZE);
3846 topology_str = NULL;
3847 if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3850 && (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
3852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3853 "Invalid topology `%s' given for section %s option %s\n",
3854 topology_str, "TESTING", "TOPOLOGY");
3855 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
3859 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "percentage",
3860 &topology_percentage_str))
3861 topology_percentage = 0.5;
3864 topology_percentage = atof (topology_percentage_str);
3865 GNUNET_free(topology_percentage_str);
3869 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "probability",
3870 &topology_probability_str))
3871 topology_probability = 0.5;
3874 topology_probability = atof (topology_probability_str);
3875 GNUNET_free(topology_probability_str);
3879 == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3881 &connect_topology_str))
3882 && (GNUNET_NO == GNUNET_TESTING_topology_get (&connect_topology,
3883 connect_topology_str)))
3886 GNUNET_ERROR_TYPE_WARNING,
3887 "Invalid connect topology `%s' given for section %s option %s\n",
3888 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
3890 GNUNET_free_non_null(connect_topology_str);
3893 == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3894 "connect_topology_option",
3895 &connect_topology_option_str))
3897 == GNUNET_TESTING_topology_option_get (&connect_topology_option,
3898 connect_topology_option_str)))
3901 GNUNET_ERROR_TYPE_WARNING,
3902 "Invalid connect topology option `%s' given for section %s option %s\n",
3903 connect_topology_option_str, "TESTING",
3904 "CONNECT_TOPOLOGY_OPTION");
3905 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
3907 GNUNET_free_non_null(connect_topology_option_str);
3910 == GNUNET_CONFIGURATION_get_value_string (
3913 "connect_topology_option_modifier",
3914 &connect_topology_option_modifier_string))
3916 if (sscanf (connect_topology_option_modifier_string, "%lf",
3917 &connect_topology_option_modifier) != 1)
3920 GNUNET_ERROR_TYPE_WARNING,
3921 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
3922 connect_topology_option_modifier_string,
3923 "connect_topology_option_modifier", "TESTING");
3925 GNUNET_free (connect_topology_option_modifier_string);
3929 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3930 "blacklist_transports",
3931 &blacklist_transports))
3932 blacklist_transports = NULL;
3935 == GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
3936 "blacklist_topology",
3937 &blacklist_topology_str))
3938 && (GNUNET_NO == GNUNET_TESTING_topology_get (&blacklist_topology,
3939 blacklist_topology_str)))
3941 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3942 "Invalid topology `%s' given for section %s option %s\n",
3943 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
3945 GNUNET_free_non_null(topology_str);
3946 GNUNET_free_non_null(blacklist_topology_str);
3948 /* Set peers_left so we know when all peers started */
3949 peers_left = num_peers;
3951 /* Set up a task to end testing if peer start fails */
3953 = GNUNET_SCHEDULER_add_delayed (
3954 GNUNET_TIME_relative_multiply (
3955 seconds_per_peer_start,
3958 "didn't generate all hostkeys within allowed startup time!");
3960 if (dhtlog_handle == NULL)
3961 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "dhtlog_handle is NULL!");
3963 trial_info.other_identifier = (unsigned int) trial_to_run;
3964 trial_info.num_nodes = peers_left;
3965 trial_info.topology = topology;
3966 trial_info.blacklist_topology = blacklist_topology;
3967 trial_info.connect_topology = connect_topology;
3968 trial_info.connect_topology_option = connect_topology_option;
3969 trial_info.connect_topology_option_modifier
3970 = connect_topology_option_modifier;
3971 trial_info.topology_percentage = topology_percentage;
3972 trial_info.topology_probability = topology_probability;
3973 trial_info.puts = num_puts;
3974 trial_info.gets = num_gets;
3975 trial_info.concurrent = max_outstanding_gets;
3976 trial_info.settle_time = settle_time;
3977 trial_info.num_rounds = total_rounds;
3978 trial_info.malicious_getters = malicious_getters;
3979 trial_info.malicious_putters = malicious_putters;
3980 trial_info.malicious_droppers = malicious_droppers;
3981 trial_info.malicious_get_frequency = malicious_get_frequency.rel_value;
3982 trial_info.malicious_put_frequency = malicious_put_frequency.rel_value;
3983 trial_info.stop_closest = stop_closest;
3984 trial_info.stop_found = stop_found;
3985 trial_info.strict_kademlia = strict_kademlia;
3987 if (trialmessage != NULL)
3988 trial_info.message = trialmessage;
3990 trial_info.message = "";
3992 if (dhtlog_handle != NULL)
3993 dhtlog_handle->insert_trial (&trial_info);
3995 GNUNET_free_non_null(trialmessage);
3997 hostkey_meter = create_meter (peers_left, "Hostkeys created ", GNUNET_YES);
3998 peer_start_meter = create_meter (peers_left, "Peers started ", GNUNET_YES);
4000 put_meter = create_meter (num_puts, "Puts completed ", GNUNET_YES);
4001 get_meter = create_meter (num_gets, "Gets completed ", GNUNET_YES);
4003 = GNUNET_TESTING_daemons_start (
4006 max_outstanding_connections,
4008 GNUNET_TIME_relative_multiply (
4009 seconds_per_peer_start,
4011 &hostkey_callback, NULL,
4012 &peers_started_callback, NULL,
4013 &topology_callback, NULL, hosts);
4015 while (temphost != NULL)
4017 tempnext = temphost->next;
4018 GNUNET_free (temphost->username);
4019 GNUNET_free (temphost->hostname);
4020 GNUNET_free (temphost);
4021 temphost = tempnext;
4026 main(int argc, char *argv[])
4029 struct GNUNET_GETOPT_CommandLineOption options[] =
4031 GNUNET_GETOPT_OPTION_END };
4033 ret = GNUNET_PROGRAM_run (argc, argv, "gnunet-dht-driver", "nohelp", options,
4036 if (malicious_bloom != NULL)
4037 GNUNET_CONTAINER_bloomfilter_free (malicious_bloom);
4039 if (ret != GNUNET_OK)
4041 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4042 "`gnunet-dht-driver': Failed with error code %d\n", ret);
4046 * Need to remove base directory, subdirectories taken care
4047 * of by the testing framework.
4049 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
4051 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4052 "Failed to remove testing directory %s\n", test_directory);
4057 /* end of gnunet-dht-driver.c */