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 #define FIND_PEER_THRESHOLD 1
73 /* If more than this many peers are added, slow down sending */
74 #define MAX_FIND_PEER_CUTOFF 2000
76 /* If less than this many peers are added, speed up sending */
77 #define MIN_FIND_PEER_CUTOFF 500
79 /* How often (in seconds) to print out connection information */
80 #define CONN_UPDATE_DURATION 10
82 #define DEFAULT_MAX_OUTSTANDING_PUTS 10
84 #define DEFAULT_MAX_OUTSTANDING_FIND_PEERS 64
86 #define DEFAULT_FIND_PEER_OFFSET GNUNET_TIME_relative_divide (DEFAULT_FIND_PEER_DELAY, DEFAULT_MAX_OUTSTANDING_FIND_PEERS)
88 #define DEFAULT_MAX_OUTSTANDING_GETS 10
90 #define DEFAULT_CONNECT_TIMEOUT 60
92 #define DEFAULT_TOPOLOGY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 8)
94 #define DEFAULT_RECONNECT_ATTEMPTS 8
97 * Default frequency for sending malicious get messages
99 #define DEFAULT_MALICIOUS_GET_FREQUENCY GNUNET_TIME_UNIT_SECONDS
102 * Default frequency for sending malicious put messages
104 #define DEFAULT_MALICIOUS_PUT_FREQUENCY GNUNET_TIME_UNIT_SECONDS
108 struct MaliciousContext
111 * Handle to DHT service (via the API)
113 struct GNUNET_DHT_Handle *dht_handle;
116 * Handle to the peer daemon
118 struct GNUNET_TESTING_Daemon *daemon;
121 * Task for disconnecting DHT handles
123 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
126 * What type of malicious to set this peer to.
133 /* This is a linked list */
134 struct TestFindPeer *next;
136 /* Handle to the bigger context */
137 struct FindPeerContext *find_peer_context;
140 * Handle to the peer's DHT service (via the API)
142 struct GNUNET_DHT_Handle *dht_handle;
145 * Handle to the peer daemon
147 struct GNUNET_TESTING_Daemon *daemon;
150 * Task for disconnecting DHT handles
152 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
155 struct TestPutContext
157 /* This is a linked list */
158 struct TestPutContext *next;
161 * Handle to the first peers DHT service (via the API)
163 struct GNUNET_DHT_Handle *dht_handle;
166 * Handle to the PUT peer daemon
168 struct GNUNET_TESTING_Daemon *daemon;
171 * Identifier for this PUT
176 * Task for disconnecting DHT handles
178 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
181 struct TestGetContext
183 /* This is a linked list */
184 struct TestGetContext *next;
187 * Handle to the first peers DHT service (via the API)
189 struct GNUNET_DHT_Handle *dht_handle;
192 * Handle for the DHT get request
194 struct GNUNET_DHT_GetHandle *get_handle;
197 * Handle to the GET peer daemon
199 struct GNUNET_TESTING_Daemon *daemon;
202 * Identifier for this GET
207 * Task for disconnecting DHT handles (and stopping GET)
209 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
212 * Whether or not this request has been fulfilled already.
218 * Simple struct to keep track of progress, and print a
219 * nice little percentage meter for long running tasks.
229 unsigned int completed;
233 char *startup_string;
237 * Linked list of information for populating statistics
238 * before ending trial.
240 struct StatisticsIteratorContext
242 const struct GNUNET_PeerIdentity *peer;
243 unsigned int stat_routes;
244 unsigned int stat_route_forwards;
245 unsigned int stat_results;
246 unsigned int stat_results_to_client;
247 unsigned int stat_result_forwards;
248 unsigned int stat_gets;
249 unsigned int stat_puts;
250 unsigned int stat_puts_inserted;
251 unsigned int stat_find_peer;
252 unsigned int stat_find_peer_start;
253 unsigned int stat_get_start;
254 unsigned int stat_put_start;
255 unsigned int stat_find_peer_reply;
256 unsigned int stat_get_reply;
257 unsigned int stat_find_peer_answer;
258 unsigned int stat_get_response_start;
262 * Context for getting a topology, logging it, and continuing
263 * on with some next operation.
265 struct TopologyIteratorContext
267 unsigned int total_iterations;
268 unsigned int current_iteration;
269 unsigned int total_connections;
270 unsigned int total_peers;
271 struct GNUNET_CONTAINER_MultiHashMap *peers_seen;
272 struct GNUNET_PeerIdentity *peer;
273 GNUNET_SCHEDULER_Task cont;
275 struct GNUNET_TIME_Relative timeout;
281 /** Node in the heap */
282 struct GNUNET_CONTAINER_HeapNode *heap_node;
284 /** Peer the count refers to */
285 struct GNUNET_PeerIdentity peer_id;
287 /** Count of connections this peer has */
292 * Context for sending out find peer requests.
294 struct FindPeerContext
297 * How long to send find peer requests, once the settle time
298 * is over don't send any more out!
300 struct GNUNET_TIME_Absolute endtime;
303 * Number of connections in the current topology
304 * (after this round of find peer requests has ended).
306 unsigned int current_peers;
309 * Number of connections in the current topology
310 * (before this round of find peer requests started).
312 unsigned int previous_peers;
315 * Number of find peer requests we have currently
318 unsigned int outstanding;
321 * Number of find peer requests to send in this round.
326 * Number of find peer requests sent last time around.
328 unsigned int last_sent;
331 * Hashmap of peers in the current topology, value
332 * is a PeerCount, with the number of connections
335 struct GNUNET_CONTAINER_MultiHashMap *peer_hash;
338 * Min heap which orders values in the peer_hash for
341 struct GNUNET_CONTAINER_Heap *peer_min_heap;
344 * Callback for counting the peers in the current topology.
346 GNUNET_TESTING_NotifyTopology count_peers_cb;
352 * Next full round (puts + gets).
357 * Next round of gets.
362 * Next round of puts.
367 * Next round of churn.
377 * How long to try to connect two peers.
379 struct GNUNET_TIME_Relative connect_timeout;
382 * How many times to re-attempt connecting two peers.
384 static unsigned long long connect_attempts;
387 * Timeout to let all GET requests happen.
389 static struct GNUNET_TIME_Relative all_get_timeout;
394 static struct GNUNET_TIME_Relative get_timeout;
397 * Time to allow for GET requests to be sent to service.
399 static struct GNUNET_TIME_Relative get_delay;
402 * Time to allow for PUT requests to be sent to service.
404 static struct GNUNET_TIME_Relative put_delay;
407 * Delay between sending find peer requests (if
408 * handled by the driver, no effect if sent by service).
410 static struct GNUNET_TIME_Relative find_peer_delay;
413 * Time between find peer requests
414 * (find_peer_delay / max_outstanding_find_peer)
416 static struct GNUNET_TIME_Relative find_peer_offset;
419 * How many seconds to allow each peer to start.
421 static struct GNUNET_TIME_Relative seconds_per_peer_start;
424 * At what time did we start the connection process.
426 static struct GNUNET_TIME_Absolute connect_start_time;
429 * What was the last time we updated connection/second information.
431 static struct GNUNET_TIME_Absolute connect_last_time;
434 * Boolean value, should the driver issue find peer requests
435 * (GNUNET_YES) or should it be left to the service (GNUNET_NO)
437 static unsigned int do_find_peer;
441 * Are we currently trying to connect two peers repeatedly?
443 static unsigned int repeat_connect_mode;
446 * Task for repeating connects.
448 GNUNET_SCHEDULER_TaskIdentifier repeat_connect_task;
450 struct GNUNET_TESTING_Daemon *repeat_connect_peer1;
451 struct GNUNET_TESTING_Daemon *repeat_connect_peer2;
455 * Boolean value, should replication be done by the dht
456 * service (GNUNET_YES) or by the driver (GNUNET_NO)
458 static unsigned int in_dht_replication;
461 * Size of test data to insert/retrieve during testing.
463 static unsigned long long test_data_size = DEFAULT_TEST_DATA_SIZE;
466 * Maximum number of concurrent connections to peers.
468 static unsigned long long max_outstanding_connections;
471 * Maximum number of concurrent ssh instances to peers.
473 static unsigned long long max_concurrent_ssh;
476 * Maximum number of concurrent PUT requests.
478 static unsigned long long max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
481 * Maximum number of concurrent GET requests.
483 static unsigned long long max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
486 * Number of nodes issuing malicious GET messages.
488 static unsigned long long malicious_getters;
491 * Maximum number of concurrent find peer messages being sent.
493 static unsigned long long max_outstanding_find_peers;
496 * Number of nodes issuing malicious PUT messages.
498 static unsigned long long malicious_putters;
501 * Time (in seconds) to delay between rounds.
503 static unsigned long long round_delay;
506 * How many malicious droppers to seed in the network.
508 static unsigned long long malicious_droppers;
511 * Bloom filter to restrict malicious nodes chosen.
513 struct GNUNET_CONTAINER_BloomFilter *malicious_bloom;
516 * Whether malicious droppers should be chosen based on proximity to a key.
518 static int malicious_sybil;
521 * Target for the malicious sybil nodes (choose the closest to this key).
523 static GNUNET_HashCode sybil_target;
526 * How often to send malicious GET messages.
528 static struct GNUNET_TIME_Relative malicious_get_frequency;
531 * How often to send malicious PUT messages.
533 static struct GNUNET_TIME_Relative malicious_put_frequency;
536 * How long to send find peer requests.
538 static unsigned long long settle_time;
541 * Handle to the dhtlog service.
543 static struct GNUNET_DHTLOG_Handle *dhtlog_handle;
546 * Replication value for GET requests.
548 static unsigned long long get_replication;
551 * Replication value for PUT requests.
553 static unsigned long long put_replication;
556 * If GNUNET_YES, insert data at the same peers every time.
557 * Otherwise, choose a new random peer to insert at each time.
559 static unsigned int replicate_same;
562 * If GNUNET_YES, issue GET requests at the same peers every time.
563 * Otherwise, choose a new random peer/data combination to search
566 static unsigned int get_from_same;
569 * Should malicious peers be set after allowing for settle time?
570 * Default is to set them malicious after initial connection setup.
572 static unsigned int malicious_after_settle;
575 * Number of rounds for testing (PUTS + GETS)
577 static unsigned long long total_rounds;
580 * Target number of connections (will stop sending find peer
581 * messages when this number is exceeded)
583 static unsigned long long target_total_connections;
586 * Number of rounds already run
588 static unsigned int rounds_finished;
591 * Number of rounds of churn to read from the file (first line, should be a single number).
593 static unsigned int churn_rounds;
596 * Current round we are in for churn, tells us how many peers to connect/disconnect.
598 static unsigned int current_churn_round;
601 * Number of times to churn per round
603 static unsigned long long churns_per_round;
606 * Array of churn values.
608 static unsigned int *churn_array;
611 * Hash map of stats contexts.
613 static struct GNUNET_CONTAINER_MultiHashMap *stats_map;
616 * LL of malicious settings.
618 struct MaliciousContext *all_malicious;
621 * List of GETS to perform
623 struct TestGetContext *all_gets;
626 * List of PUTS to perform
628 struct TestPutContext *all_puts;
631 * Directory to store temporary data in, defined in config file
633 static char *test_directory;
636 * Variable used to store the number of connections we should wait for.
638 static unsigned int expected_connections;
641 * Variable used to keep track of how many peers aren't yet started.
643 static unsigned long long peers_left;
646 * Handle to the set of all peers run for this test.
648 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 long long malicious_completed;
718 * Global used to count how many connections we have currently
719 * been notified about (how many times has topology_callback been called
722 static unsigned int total_connections;
725 * Previous connections, for counting new connections during some duration.
727 static unsigned int previous_connections;
730 * For counting failed connections during some duration.
732 static unsigned int previous_failed_connections;
735 * Global used to count how many failed connections we have
736 * been notified about (how many times has topology_callback
737 * been called with failure?)
739 static unsigned int failed_connections;
742 * If GNUNET_YES, only log PUT/GET round data to mysql, otherwise
743 * log everything (including each dht service logging).
745 static unsigned int dhtlog_minimal;
747 /* Task handle to use to schedule shutdown if something goes wrong */
748 GNUNET_SCHEDULER_TaskIdentifier die_task;
750 static char *blacklist_transports;
752 static enum GNUNET_TESTING_Topology topology;
754 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
756 static enum GNUNET_TESTING_Topology connect_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
758 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
760 static double connect_topology_option_modifier = 0.0;
762 static struct ProgressMeter *hostkey_meter;
764 static struct ProgressMeter *peer_start_meter;
766 static struct ProgressMeter *peer_connect_meter;
768 static struct ProgressMeter *put_meter;
770 static struct ProgressMeter *get_meter;
772 static GNUNET_HashCode *known_keys;
774 /* Global return value (0 for success, anything else for failure) */
778 * Create a meter to keep track of the progress of some task.
780 * @param total the total number of items to complete
781 * @param start_string a string to prefix the meter with (if printing)
782 * @param print GNUNET_YES to print the meter, GNUNET_NO to count
785 * @return the progress meter
787 static struct ProgressMeter *
788 create_meter(unsigned int total, char * start_string, int print)
790 struct ProgressMeter *ret;
791 ret = GNUNET_malloc(sizeof(struct ProgressMeter));
794 ret->modnum = total / 4;
795 ret->dotnum = (total / 50) + 1;
796 if (start_string != NULL)
797 ret->startup_string = GNUNET_strdup(start_string);
799 ret->startup_string = GNUNET_strdup("");
805 * Update progress meter (increment by one).
807 * @param meter the meter to update and print info for
809 * @return GNUNET_YES if called the total requested,
810 * GNUNET_NO if more items expected
813 update_meter(struct ProgressMeter *meter)
815 if (meter->print == GNUNET_YES)
817 if (meter->completed % meter->modnum == 0)
819 if (meter->completed == 0)
821 fprintf(stdout, "%sProgress: [0%%", meter->startup_string);
824 fprintf(stdout, "%d%%", (int)(((float)meter->completed / meter->total) * 100));
826 else if (meter->completed % meter->dotnum == 0)
827 fprintf(stdout, ".");
829 if (meter->completed + 1 == meter->total)
830 fprintf(stdout, "%d%%]\n", 100);
835 if (meter->completed == meter->total)
841 * Reset progress meter.
843 * @param meter the meter to reset
845 * @return GNUNET_YES if meter reset,
846 * GNUNET_SYSERR on error
849 reset_meter(struct ProgressMeter *meter)
852 return GNUNET_SYSERR;
854 meter->completed = 0;
859 * Release resources for meter
861 * @param meter the meter to free
864 free_meter(struct ProgressMeter *meter)
866 GNUNET_free_non_null (meter->startup_string);
871 * Check whether peers successfully shut down.
874 shutdown_callback (void *cls,
885 * Task to release DHT handles for PUT
888 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
890 struct TestPutContext *test_put = cls;
891 test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
892 GNUNET_DHT_disconnect(test_put->dht_handle);
893 test_put->dht_handle = NULL;
894 if (replicate_same == GNUNET_NO)
895 test_put->daemon = GNUNET_TESTING_daemon_get(pg, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers));
899 * Function scheduled to be run on the successful completion of this
903 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ending test normally!\n", (char *)cls);
906 GNUNET_assert (pg != NULL);
907 struct TestPutContext *test_put = all_puts;
908 struct TestGetContext *test_get = all_gets;
910 while (test_put != NULL)
912 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
913 GNUNET_SCHEDULER_cancel(test_put->disconnect_task);
914 if (test_put->dht_handle != NULL)
915 GNUNET_DHT_disconnect(test_put->dht_handle);
916 test_put = test_put->next;
919 while (test_get != NULL)
921 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
922 GNUNET_SCHEDULER_cancel(test_get->disconnect_task);
923 if (test_get->get_handle != NULL)
924 GNUNET_DHT_get_stop(test_get->get_handle);
925 if (test_get->dht_handle != NULL)
926 GNUNET_DHT_disconnect(test_get->dht_handle);
927 test_get = test_get->next;
930 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
932 if (dhtlog_handle != NULL)
934 fprintf(stderr, "Update trial endtime\n");
935 dhtlog_handle->update_trial (gets_completed);
936 GNUNET_DHTLOG_disconnect(dhtlog_handle);
937 dhtlog_handle = NULL;
940 if (hostkey_meter != NULL)
941 free_meter(hostkey_meter);
942 if (peer_start_meter != NULL)
943 free_meter(peer_start_meter);
944 if (peer_connect_meter != NULL)
945 free_meter(peer_connect_meter);
946 if (put_meter != NULL)
947 free_meter(put_meter);
948 if (get_meter != NULL)
949 free_meter(get_meter);
955 * Callback for iterating over all the peer connections of a peer group.
958 log_topology_cb (void *cls,
959 const struct GNUNET_PeerIdentity *first,
960 const struct GNUNET_PeerIdentity *second,
963 struct TopologyIteratorContext *topo_ctx = cls;
964 if ((first != NULL) && (second != NULL))
966 if ((topo_ctx->peers_seen != NULL) && (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(topo_ctx->peers_seen, &first->hashPubKey)))
968 GNUNET_CONTAINER_multihashmap_put(topo_ctx->peers_seen, &first->hashPubKey, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
969 topo_ctx->total_peers++;
971 topo_ctx->total_connections++;
972 if ((GNUNET_NO == dhtlog_minimal) && (dhtlog_handle != NULL))
973 dhtlog_handle->insert_extended_topology(first, second);
977 GNUNET_assert(dhtlog_handle != NULL);
978 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Topology iteration (%u/%u) finished (%u connections, %u peers)\n", topo_ctx->current_iteration, topo_ctx->total_iterations, topo_ctx->total_connections, topo_ctx->total_peers);
979 dhtlog_handle->update_topology(topo_ctx->total_connections);
980 if (topo_ctx->cont != NULL)
981 GNUNET_SCHEDULER_add_now (topo_ctx->cont, topo_ctx->cls);
982 if (topo_ctx->peers_seen != NULL)
983 GNUNET_CONTAINER_multihashmap_destroy(topo_ctx->peers_seen);
984 GNUNET_free(topo_ctx);
989 * Iterator over hash map entries.
991 * @param cls closure - always NULL
992 * @param key current key code
993 * @param value value in the hash map, a stats context
994 * @return GNUNET_YES if we should continue to
998 static int stats_iterate (void *cls,
999 const GNUNET_HashCode * key,
1002 struct StatisticsIteratorContext *stats_ctx;
1006 dhtlog_handle->insert_stat(stats_ctx->peer, stats_ctx->stat_routes, stats_ctx->stat_route_forwards, stats_ctx->stat_results,
1007 stats_ctx->stat_results_to_client, stats_ctx->stat_result_forwards, stats_ctx->stat_gets,
1008 stats_ctx->stat_puts, stats_ctx->stat_puts_inserted, stats_ctx->stat_find_peer,
1009 stats_ctx->stat_find_peer_start, stats_ctx->stat_get_start, stats_ctx->stat_put_start,
1010 stats_ctx->stat_find_peer_reply, stats_ctx->stat_get_reply, stats_ctx->stat_find_peer_answer,
1011 stats_ctx->stat_get_response_start);
1012 GNUNET_free(stats_ctx);
1017 stats_finished (void *cls, int result)
1019 fprintf(stderr, "Finished getting all peers statistics, iterating!\n");
1020 GNUNET_CONTAINER_multihashmap_iterate(stats_map, &stats_iterate, NULL);
1021 GNUNET_CONTAINER_multihashmap_destroy(stats_map);
1022 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
1026 * Callback function to process statistic values.
1028 * @param cls closure
1029 * @param peer the peer the statistics belong to
1030 * @param subsystem name of subsystem that created the statistic
1031 * @param name the name of the datum
1032 * @param value the current value
1033 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
1034 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
1036 static int stats_handle (void *cls,
1037 const struct GNUNET_PeerIdentity *peer,
1038 const char *subsystem,
1043 struct StatisticsIteratorContext *stats_ctx;
1045 if (dhtlog_handle != NULL)
1046 dhtlog_handle->add_generic_stat(peer, name, subsystem, value);
1047 if (GNUNET_CONTAINER_multihashmap_contains(stats_map, &peer->hashPubKey))
1049 stats_ctx = GNUNET_CONTAINER_multihashmap_get(stats_map, &peer->hashPubKey);
1053 stats_ctx = GNUNET_malloc(sizeof(struct StatisticsIteratorContext));
1054 stats_ctx->peer = peer;
1055 GNUNET_CONTAINER_multihashmap_put(stats_map, &peer->hashPubKey, stats_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1057 GNUNET_assert(stats_ctx != NULL);
1059 if (strcmp(name, STAT_ROUTES) == 0)
1060 stats_ctx->stat_routes = value;
1061 else if (strcmp(name, STAT_ROUTE_FORWARDS) == 0)
1062 stats_ctx->stat_route_forwards = value;
1063 else if (strcmp(name, STAT_RESULTS) == 0)
1064 stats_ctx->stat_results = value;
1065 else if (strcmp(name, STAT_RESULTS_TO_CLIENT) == 0)
1066 stats_ctx->stat_results_to_client = value;
1067 else if (strcmp(name, STAT_RESULT_FORWARDS) == 0)
1068 stats_ctx->stat_result_forwards = value;
1069 else if (strcmp(name, STAT_GETS) == 0)
1070 stats_ctx->stat_gets = value;
1071 else if (strcmp(name, STAT_PUTS) == 0)
1072 stats_ctx->stat_puts = value;
1073 else if (strcmp(name, STAT_PUTS_INSERTED) == 0)
1074 stats_ctx->stat_puts_inserted = value;
1075 else if (strcmp(name, STAT_FIND_PEER) == 0)
1076 stats_ctx->stat_find_peer = value;
1077 else if (strcmp(name, STAT_FIND_PEER_START) == 0)
1078 stats_ctx->stat_find_peer_start = value;
1079 else if (strcmp(name, STAT_GET_START) == 0)
1080 stats_ctx->stat_get_start = value;
1081 else if (strcmp(name, STAT_PUT_START) == 0)
1082 stats_ctx->stat_put_start = value;
1083 else if (strcmp(name, STAT_FIND_PEER_REPLY) == 0)
1084 stats_ctx->stat_find_peer_reply = value;
1085 else if (strcmp(name, STAT_GET_REPLY) == 0)
1086 stats_ctx->stat_get_reply = value;
1087 else if (strcmp(name, STAT_FIND_PEER_ANSWER) == 0)
1088 stats_ctx->stat_find_peer_answer = value;
1089 else if (strcmp(name, STAT_GET_RESPONSE_START) == 0)
1090 stats_ctx->stat_get_response_start = value;
1096 * Connect to statistics service for each peer and get the appropriate
1097 * dht statistics for safe keeping.
1100 log_dht_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1102 stats_map = GNUNET_CONTAINER_multihashmap_create(num_peers);
1103 fprintf(stderr, "Starting statistics logging\n");
1104 GNUNET_TESTING_get_statistics(pg, &stats_finished, &stats_handle, NULL);
1109 * Connect to all peers in the peer group and iterate over their
1113 capture_current_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1115 struct TopologyIteratorContext *topo_ctx = cls;
1116 dhtlog_handle->insert_topology(0);
1117 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Called capture_current_topology\n");
1118 GNUNET_TESTING_get_topology (pg, &log_topology_cb, topo_ctx);
1123 * Check if the get_handle is being used, if so stop the request. Either
1124 * way, schedule the end_badly_cont function which actually shuts down the
1128 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failing test with error: `%s'!\n", (char *)cls);
1132 struct TestPutContext *test_put = all_puts;
1133 struct TestGetContext *test_get = all_gets;
1135 while (test_put != NULL)
1137 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
1138 GNUNET_SCHEDULER_cancel(test_put->disconnect_task);
1139 if (test_put->dht_handle != NULL)
1140 GNUNET_DHT_disconnect(test_put->dht_handle);
1141 test_put = test_put->next;
1144 while (test_get != NULL)
1146 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
1147 GNUNET_SCHEDULER_cancel(test_get->disconnect_task);
1148 if (test_get->get_handle != NULL)
1149 GNUNET_DHT_get_stop(test_get->get_handle);
1150 if (test_get->dht_handle != NULL)
1151 GNUNET_DHT_disconnect(test_get->dht_handle);
1152 test_get = test_get->next;
1155 GNUNET_TESTING_daemons_stop (pg, DEFAULT_TIMEOUT, &shutdown_callback, NULL);
1157 if (dhtlog_handle != NULL)
1159 fprintf(stderr, "Update trial endtime\n");
1160 dhtlog_handle->update_trial (gets_completed);
1161 GNUNET_DHTLOG_disconnect(dhtlog_handle);
1162 dhtlog_handle = NULL;
1165 if (hostkey_meter != NULL)
1166 free_meter(hostkey_meter);
1167 if (peer_start_meter != NULL)
1168 free_meter(peer_start_meter);
1169 if (peer_connect_meter != NULL)
1170 free_meter(peer_connect_meter);
1171 if (put_meter != NULL)
1172 free_meter(put_meter);
1173 if (get_meter != NULL)
1174 free_meter(get_meter);
1180 * Forward declaration.
1183 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1186 * Forward declaration.
1189 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1192 * Iterator over hash map entries.
1194 * @param cls closure
1195 * @param key current key code
1196 * @param value value in the hash map
1197 * @return GNUNET_YES if we should continue to
1201 static int remove_peer_count (void *cls,
1202 const GNUNET_HashCode * key,
1205 struct FindPeerContext *find_peer_ctx = cls;
1206 struct PeerCount *peer_count = value;
1207 GNUNET_CONTAINER_heap_remove_node(find_peer_ctx->peer_min_heap, peer_count->heap_node);
1208 GNUNET_free(peer_count);
1214 * Connect to all peers in the peer group and iterate over their
1218 count_new_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1220 struct FindPeerContext *find_peer_context = cls;
1221 find_peer_context->previous_peers = find_peer_context->current_peers;
1222 find_peer_context->current_peers = 0;
1223 GNUNET_TESTING_get_topology (pg, find_peer_context->count_peers_cb, find_peer_context);
1227 decrement_find_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1229 struct TestFindPeer *test_find_peer = cls;
1230 GNUNET_assert(test_find_peer->find_peer_context->outstanding > 0);
1231 test_find_peer->find_peer_context->outstanding--;
1232 test_find_peer->find_peer_context->total--;
1233 if (0 == test_find_peer->find_peer_context->total)
1235 GNUNET_SCHEDULER_add_now(&count_new_peers, test_find_peer->find_peer_context);
1237 GNUNET_free(test_find_peer);
1241 * A find peer request has been sent to the server, now we will schedule a task
1242 * to wait the appropriate time to allow the request to go out and back.
1244 * @param cls closure - a TestFindPeer struct
1245 * @param tc context the task is being called with
1248 handle_find_peer_sent (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1250 struct TestFindPeer *test_find_peer = cls;
1252 GNUNET_DHT_disconnect(test_find_peer->dht_handle);
1253 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_divide(find_peer_delay, 2), &decrement_find_peers, test_find_peer);
1258 send_find_peer_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1260 struct TestFindPeer *test_find_peer = cls;
1262 if (test_find_peer->find_peer_context->outstanding > max_outstanding_find_peers)
1264 GNUNET_SCHEDULER_add_delayed(find_peer_offset, &send_find_peer_request, test_find_peer);
1268 test_find_peer->find_peer_context->outstanding++;
1269 if (GNUNET_TIME_absolute_get_remaining(test_find_peer->find_peer_context->endtime).rel_value == 0)
1271 GNUNET_SCHEDULER_add_now(&decrement_find_peers, test_find_peer);
1275 test_find_peer->dht_handle = GNUNET_DHT_connect(test_find_peer->daemon->cfg, 1);
1276 GNUNET_assert(test_find_peer->dht_handle != NULL);
1277 GNUNET_DHT_find_peers (test_find_peer->dht_handle,
1278 &handle_find_peer_sent, test_find_peer);
1283 * Add a connection to the find_peer_context given. This may
1284 * be complete overkill, but allows us to choose the peers with
1285 * the least connections to initiate find peer requests from.
1287 static void add_new_connection(struct FindPeerContext *find_peer_context,
1288 const struct GNUNET_PeerIdentity *first,
1289 const struct GNUNET_PeerIdentity *second)
1291 struct PeerCount *first_count;
1292 struct PeerCount *second_count;
1294 if (GNUNET_CONTAINER_multihashmap_contains(find_peer_context->peer_hash, &first->hashPubKey))
1296 first_count = GNUNET_CONTAINER_multihashmap_get(find_peer_context->peer_hash, &first->hashPubKey);
1297 GNUNET_assert(first_count != NULL);
1298 first_count->count++;
1299 GNUNET_CONTAINER_heap_update_cost(find_peer_context->peer_min_heap, first_count->heap_node, first_count->count);
1303 first_count = GNUNET_malloc(sizeof(struct PeerCount));
1304 first_count->count = 1;
1305 memcpy(&first_count->peer_id, first, sizeof(struct GNUNET_PeerIdentity));
1306 first_count->heap_node = GNUNET_CONTAINER_heap_insert(find_peer_context->peer_min_heap, first_count, first_count->count);
1307 GNUNET_CONTAINER_multihashmap_put(find_peer_context->peer_hash, &first->hashPubKey, first_count, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1310 if (GNUNET_CONTAINER_multihashmap_contains(find_peer_context->peer_hash, &second->hashPubKey))
1312 second_count = GNUNET_CONTAINER_multihashmap_get(find_peer_context->peer_hash, &second->hashPubKey);
1313 GNUNET_assert(second_count != NULL);
1314 second_count->count++;
1315 GNUNET_CONTAINER_heap_update_cost(find_peer_context->peer_min_heap, second_count->heap_node, second_count->count);
1319 second_count = GNUNET_malloc(sizeof(struct PeerCount));
1320 second_count->count = 1;
1321 memcpy(&second_count->peer_id, second, sizeof(struct GNUNET_PeerIdentity));
1322 second_count->heap_node = GNUNET_CONTAINER_heap_insert(find_peer_context->peer_min_heap, second_count, second_count->count);
1323 GNUNET_CONTAINER_multihashmap_put(find_peer_context->peer_hash, &second->hashPubKey, second_count, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1329 * Iterate over min heap of connections per peer. For any
1330 * peer that has 0 connections, attempt to connect them to
1333 * @param cls closure a struct FindPeerContext
1334 * @param node internal node of the heap
1335 * @param element value stored, a struct PeerCount
1336 * @param cost cost associated with the node
1337 * @return GNUNET_YES if we should continue to iterate,
1340 static int iterate_min_heap_peers (void *cls,
1341 struct GNUNET_CONTAINER_HeapNode *node,
1343 GNUNET_CONTAINER_HeapCostType cost)
1345 struct FindPeerContext *find_peer_context = cls;
1346 struct PeerCount *peer_count = element;
1347 struct GNUNET_TESTING_Daemon *d1;
1348 struct GNUNET_TESTING_Daemon *d2;
1349 struct GNUNET_TIME_Relative timeout;
1352 d1 = GNUNET_TESTING_daemon_get_by_id (pg, &peer_count->peer_id);
1353 GNUNET_assert(d1 != NULL);
1355 while ((d2 == d1) || (GNUNET_YES != GNUNET_TESTING_daemon_running(d2)))
1357 d2 = GNUNET_TESTING_daemon_get(pg, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers));
1358 GNUNET_assert(d2 != NULL);
1361 /** Just try to connect the peers, don't worry about callbacks, etc. **/
1362 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer %s has 0 connections. Trying to connect to %s...\n", GNUNET_i2s(&peer_count->peer_id), d2->shortname);
1363 timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, DEFAULT_CONNECT_TIMEOUT);
1364 if (GNUNET_TIME_relative_to_absolute(timeout).abs_value > find_peer_context->endtime.abs_value)
1366 timeout = GNUNET_TIME_absolute_get_remaining(find_peer_context->endtime);
1368 GNUNET_TESTING_daemons_connect(d1, d2, timeout, DEFAULT_RECONNECT_ATTEMPTS, GNUNET_YES, NULL, NULL);
1370 if (GNUNET_TIME_absolute_get_remaining(find_peer_context->endtime).rel_value > 0)
1377 * Forward declaration.
1380 schedule_churn_find_peer_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
1383 * Callback for iterating over all the peer connections of a peer group.
1384 * Used after we have churned on some peers to find which ones have zero
1385 * connections so we can make them issue find peer requests.
1388 count_peers_churn_cb (void *cls,
1389 const struct GNUNET_PeerIdentity *first,
1390 const struct GNUNET_PeerIdentity *second,
1393 struct FindPeerContext *find_peer_context = cls;
1394 struct TopologyIteratorContext *topo_ctx;
1395 struct PeerCount *peer_count;
1397 if ((first != NULL) && (second != NULL))
1399 add_new_connection(find_peer_context, first, second);
1400 find_peer_context->current_peers++;
1404 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer count finished (%u connections)\n",
1405 find_peer_context->current_peers);
1406 peer_count = GNUNET_CONTAINER_heap_peek(find_peer_context->peer_min_heap);
1407 GNUNET_assert(peer_count != NULL);
1408 /* 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. */
1409 /* NO they won't, because we have disabled peerinfo writing to disk (remember?) so we WILL have to give them new connections */
1410 /* Best course of action: have DHT automatically try to add peers from peerinfo on startup. This way IF peerinfo writes to file
1411 * then some peers will end up connected.
1413 * Also, find any peers that have zero connections here and set up a task to choose at random another peer in the network to
1414 * 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
1415 * until they get a peer.
1417 /* However, they won't automatically be connected to any of their previous peers... How can we handle that? */
1418 /* So now we have choices: do we want them to come back with all their connections? Probably not, but it solves this mess. */
1420 /* Second problem, which is still a problem, is that a FIND_PEER request won't work when a peer has no connections */
1423 * Okay, so here's how this *should* work now.
1425 * 1. We check the min heap for any peers that have 0 connections.
1426 * a. If any are found, we iterate over the heap and just randomly
1427 * choose another peer and ask testing to please connect the two.
1428 * This takes care of the case that a peer just randomly joins the
1429 * network. However, if there are strict topology restrictions
1430 * (imagine a ring) choosing randomly most likely won't help.
1431 * We make sure the connection attempt doesn't take longer than
1432 * the total timeout, but don't care too much about the result.
1433 * b. After that, we still schedule the find peer requests (concurrently
1434 * with the connect attempts most likely). This handles the case
1435 * that the DHT iterates over peerinfo and just needs to try to send
1436 * a message to get connected. This should handle the case that the
1437 * topology is very strict.
1439 * 2. If all peers have > 0 connections, we still send find peer requests
1440 * as long as possible (until timeout is reached) to help out those
1441 * peers that were newly churned and need more connections. This is because
1442 * once all new peers have established a single connection, they won't be
1445 * 3. Once we reach the timeout, we can do no more. We must schedule the
1446 * next iteration of get requests regardless of connections that peers
1447 * may or may not have.
1449 * Caveat: it would be nice to get peers to take data offline with them and
1450 * come back with it (or not) based on the testing framework. The
1451 * same goes for remembering previous connections, but putting either
1452 * into the general testing churn options seems like overkill because
1453 * these are very specialized cases.
1455 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Out of %u peers, fewest connections is %d\n", GNUNET_CONTAINER_heap_get_size(find_peer_context->peer_min_heap), peer_count->count);
1456 if ((peer_count->count == 0) && (GNUNET_TIME_absolute_get_remaining(find_peer_context->endtime).rel_value > 0))
1458 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found peer with no connections, will choose some peer(s) at random to connect to!\n");
1459 GNUNET_CONTAINER_heap_iterate (find_peer_context->peer_min_heap, &iterate_min_heap_peers, find_peer_context);
1460 GNUNET_SCHEDULER_add_now(&schedule_churn_find_peer_requests, find_peer_context);
1462 else if ((GNUNET_TIME_absolute_get_remaining(find_peer_context->endtime).rel_value > 0) && (find_peer_context->last_sent != 0))
1464 GNUNET_SCHEDULER_add_now(&schedule_churn_find_peer_requests, find_peer_context);
1468 GNUNET_CONTAINER_multihashmap_iterate(find_peer_context->peer_hash, &remove_peer_count, find_peer_context);
1469 GNUNET_CONTAINER_multihashmap_destroy(find_peer_context->peer_hash);
1470 GNUNET_CONTAINER_heap_destroy(find_peer_context->peer_min_heap);
1471 GNUNET_free(find_peer_context);
1472 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Churn round %u of %llu finished, scheduling next GET round.\n", current_churn_round, churn_rounds);
1473 if (dhtlog_handle != NULL)
1475 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1476 topo_ctx->cont = &do_get;
1477 topo_ctx->cls = all_gets;
1478 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1479 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
1480 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add(GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout), DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1481 &end_badly, "from do gets (count_peers_churn_cb)");
1482 GNUNET_SCHEDULER_add_now(&capture_current_topology, topo_ctx);
1486 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add(GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout), DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1487 &end_badly, "from do gets (count_peers_churn_cb)");
1488 GNUNET_SCHEDULER_add_now(&do_get, all_gets);
1495 * Set up a single find peer request for each peer in the topology. Do this
1496 * until the settle time is over, limited by the number of outstanding requests
1497 * and the time allowed for each one!
1500 schedule_churn_find_peer_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1502 struct FindPeerContext *find_peer_ctx = cls;
1503 struct TestFindPeer *test_find_peer;
1504 struct PeerCount *peer_count;
1507 if (find_peer_ctx->previous_peers == 0) /* First time, go slowly */
1508 find_peer_ctx->total = 1;
1509 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers < MIN_FIND_PEER_CUTOFF)
1510 find_peer_ctx->total = find_peer_ctx->total / 2;
1511 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers > MAX_FIND_PEER_CUTOFF) /* Found LOTS of peers, still go slowly */
1512 find_peer_ctx->total = find_peer_ctx->last_sent - (find_peer_ctx->last_sent / 4);
1514 find_peer_ctx->total = find_peer_ctx->last_sent * 4;
1516 if (find_peer_ctx->total > max_outstanding_find_peers)
1517 find_peer_ctx->total = max_outstanding_find_peers;
1519 find_peer_ctx->last_sent = find_peer_ctx->total;
1520 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sending %u find peer messages (after churn)\n", find_peer_ctx->total);
1522 if (find_peer_ctx->total > 0)
1523 find_peer_offset = GNUNET_TIME_relative_divide(find_peer_delay, find_peer_ctx->total);
1526 find_peer_ctx->previous_peers = find_peer_ctx->current_peers;
1527 find_peer_ctx->current_peers = 0;
1528 GNUNET_TESTING_get_topology (pg, &count_peers_churn_cb, find_peer_ctx);
1532 for (i = 0; i < find_peer_ctx->total; i++)
1534 test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
1535 /* If we have sent requests, choose peers with a low number of connections to send requests from */
1536 peer_count = GNUNET_CONTAINER_heap_remove_root(find_peer_ctx->peer_min_heap);
1537 GNUNET_assert(peer_count != NULL);
1538 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sending find peer request from peer with %u connections\n", peer_count->count);
1539 GNUNET_CONTAINER_multihashmap_remove(find_peer_ctx->peer_hash, &peer_count->peer_id.hashPubKey, peer_count);
1540 test_find_peer->daemon = GNUNET_TESTING_daemon_get_by_id(pg, &peer_count->peer_id);
1541 GNUNET_assert(test_find_peer->daemon != NULL);
1542 test_find_peer->find_peer_context = find_peer_ctx;
1543 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(find_peer_offset, i), &send_find_peer_request, test_find_peer);
1546 if ((find_peer_ctx->peer_hash == NULL) && (find_peer_ctx->peer_min_heap == NULL))
1548 find_peer_ctx->peer_hash = GNUNET_CONTAINER_multihashmap_create(num_peers);
1549 find_peer_ctx->peer_min_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
1553 GNUNET_CONTAINER_multihashmap_iterate(find_peer_ctx->peer_hash, &remove_peer_count, find_peer_ctx);
1554 GNUNET_CONTAINER_multihashmap_destroy(find_peer_ctx->peer_hash);
1555 find_peer_ctx->peer_hash = GNUNET_CONTAINER_multihashmap_create(num_peers);
1558 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap_size(find_peer_ctx->peer_hash));
1559 GNUNET_assert(0 == GNUNET_CONTAINER_heap_get_size(find_peer_ctx->peer_min_heap));
1562 static void schedule_churn_get_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1564 struct FindPeerContext *find_peer_context = cls;
1565 GNUNET_TESTING_get_topology (pg, &count_peers_churn_cb, find_peer_context);
1569 * Called when churning of the topology has finished.
1571 * @param cls closure unused
1572 * @param emsg NULL on success, or a printable error on failure
1574 static void churn_complete (void *cls, const char *emsg)
1576 struct FindPeerContext *find_peer_context = cls;
1577 struct PeerCount *peer_count;
1579 struct GNUNET_TESTING_Daemon *temp_daemon;
1580 struct TopologyIteratorContext *topo_ctx;
1581 struct GNUNET_TIME_Relative calc_timeout;
1586 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Ending test, churning of peers failed with error `%s'", emsg);
1587 GNUNET_SCHEDULER_add_now(&end_badly, (void *)emsg);
1592 * If we switched any peers on, we have to somehow force connect the new peer to
1593 * SOME bootstrap peer in the network. First schedule a task to find all peers
1594 * with no connections, then choose a random peer for each and connect them.
1596 if (find_peer_context != NULL)
1598 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "We have churned on some peers, so we must schedule find peer requests for them!\n");
1600 for (i = 0; i < num_peers; i ++)
1602 temp_daemon = GNUNET_TESTING_daemon_get(pg, i);
1603 if (GNUNET_YES == GNUNET_TESTING_daemon_running(temp_daemon))
1605 peer_count = GNUNET_malloc (sizeof(struct PeerCount));
1606 memcpy(&peer_count->peer_id, &temp_daemon->id, sizeof(struct GNUNET_PeerIdentity));
1607 GNUNET_assert(peer_count->count == 0);
1608 peer_count->heap_node = GNUNET_CONTAINER_heap_insert(find_peer_context->peer_min_heap, peer_count, peer_count->count);
1609 GNUNET_CONTAINER_multihashmap_put(find_peer_context->peer_hash, &temp_daemon->id.hashPubKey, peer_count, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1613 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Added %d peers to heap, total size %d\n", count_added, GNUNET_CONTAINER_heap_get_size(find_peer_context->peer_min_heap));
1614 GNUNET_SCHEDULER_add_delayed(DEFAULT_PEER_DISCONNECT_TIMEOUT, &schedule_churn_get_topology, find_peer_context);
1618 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Only churned off peers, no find peer requests, scheduling more gets (after allowing time for peers to disconnect properly!)...\n");
1619 if (dhtlog_handle != NULL)
1621 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1622 topo_ctx->cont = &do_get;
1623 topo_ctx->cls = all_gets;
1624 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1625 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
1626 calc_timeout = GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout);
1627 calc_timeout = GNUNET_TIME_relative_add(calc_timeout, DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT);
1628 calc_timeout = GNUNET_TIME_relative_add(calc_timeout, DEFAULT_PEER_DISCONNECT_TIMEOUT);
1629 die_task = GNUNET_SCHEDULER_add_delayed (calc_timeout,
1630 &end_badly, "from do gets (churn_complete)");
1631 GNUNET_SCHEDULER_add_delayed(DEFAULT_PEER_DISCONNECT_TIMEOUT, &capture_current_topology, topo_ctx);
1632 dhtlog_handle->insert_round(DHT_ROUND_GET, rounds_finished);
1636 calc_timeout = GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout);
1637 calc_timeout = GNUNET_TIME_relative_add(calc_timeout, DEFAULT_PEER_DISCONNECT_TIMEOUT);
1638 die_task = GNUNET_SCHEDULER_add_delayed (calc_timeout,
1639 &end_badly, "from do gets (churn_complete)");
1640 GNUNET_SCHEDULER_add_delayed(DEFAULT_PEER_DISCONNECT_TIMEOUT, &do_get, all_gets);
1646 * Decide how many peers to turn on or off in this round, make sure the
1647 * numbers actually make sense, then do so. This function sets in motion
1648 * churn, find peer requests for newly joined peers, and issuing get
1649 * requests once the new peers have done so.
1651 * @param cls closure (unused)
1652 * @param tc task context (unused)
1655 churn_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1657 unsigned int count_running;
1658 unsigned int churn_up;
1659 unsigned int churn_down;
1660 struct GNUNET_TIME_Relative timeout;
1661 struct FindPeerContext *find_peer_context;
1663 churn_up = churn_down = 0;
1664 count_running = GNUNET_TESTING_daemons_running(pg);
1665 if (count_running > churn_array[current_churn_round])
1666 churn_down = count_running - churn_array[current_churn_round];
1667 else if (count_running < churn_array[current_churn_round])
1668 churn_up = churn_array[current_churn_round] - count_running;
1670 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Not churning any peers, topology unchanged.\n");
1672 if (churn_up > num_peers - count_running)
1674 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Churn file specified %u peers (up); only have %u!", churn_array[current_churn_round], num_peers);
1675 churn_up = num_peers - count_running;
1677 else if (churn_down > count_running)
1679 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Churn file specified %u peers (down); only have %u!", churn_array[current_churn_round], count_running);
1680 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "This will leave NO peers running (mistake in churn configuration?)!");
1681 churn_down = count_running;
1683 //timeout = GNUNET_TIME_relative_multiply(seconds_per_peer_start, churn_up > 0 ? churn_up : churn_down);
1684 //timeout = GNUNET_TIME_relative_multiply (seconds_per_peer_start, churn_up > 0 ? churn_up : churn_down);
1685 timeout = GNUNET_TIME_relative_multiply(DEFAULT_TIMEOUT, 2); /* FIXME: Lack of intelligent choice here */
1686 find_peer_context = NULL;
1687 if (churn_up > 0) /* Only need to do find peer requests if we turned new peers on */
1689 find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
1690 find_peer_context->count_peers_cb = &count_peers_churn_cb;
1691 find_peer_context->previous_peers = 0;
1692 find_peer_context->current_peers = 0;
1693 find_peer_context->endtime = GNUNET_TIME_relative_to_absolute(timeout);
1694 find_peer_context->peer_hash = GNUNET_CONTAINER_multihashmap_create(num_peers);
1695 find_peer_context->peer_min_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
1697 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "churn_peers: want %u total, %u running, starting %u, stopping %u\n",
1698 churn_array[current_churn_round], count_running, churn_up, churn_down);
1699 GNUNET_TESTING_daemons_churn (pg, churn_down, churn_up, timeout, &churn_complete, find_peer_context);
1700 current_churn_round++;
1704 * Task to release DHT handle associated with GET request.
1707 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1709 struct TestGetContext *test_get = cls;
1710 struct TopologyIteratorContext *topo_ctx;
1712 /* The dht_handle may be null if this get was scheduled from a down peer */
1713 if (test_get->dht_handle != NULL)
1715 GNUNET_DHT_disconnect(test_get->dht_handle);
1716 outstanding_gets--; /* GET is really finished */
1717 test_get->dht_handle = NULL;
1720 /* Reset the uid (which item to search for) and the daemon (which peer to search from) for later get request iterations */
1721 if (get_from_same == GNUNET_NO)
1723 test_get->uid = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
1724 test_get->daemon = GNUNET_TESTING_daemon_get(pg, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers));
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n", gets_completed, gets_failed);
1730 update_meter(get_meter);
1731 if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0))
1733 fprintf(stderr, "Canceling die task (get_stop_finished) %llu gets completed, %llu gets failed\n", gets_completed, gets_failed);
1734 if ((GNUNET_YES == dhtlog_minimal) && (NULL != dhtlog_handle))
1735 dhtlog_handle->insert_round_details(DHT_ROUND_GET, rounds_finished, num_gets, gets_completed);
1736 GNUNET_SCHEDULER_cancel(die_task);
1737 reset_meter(put_meter);
1738 reset_meter(get_meter);
1741 * 1) Testing is completely finished, call the topology iteration dealy and die
1742 * 2) Testing is not finished, churn the network and do gets again (current_churn_round < churn_rounds)
1743 * 3) Testing is not finished, reschedule all the PUTS *and* GETS again (num_rounds > 1)
1745 if (rounds_finished == total_rounds - 1) /* Everything is finished, end testing */
1747 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
1749 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1750 topo_ctx->cont = &log_dht_statistics;
1751 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
1752 GNUNET_SCHEDULER_add_now(&capture_current_topology, topo_ctx);
1755 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
1757 else if (current_churn_round < churns_per_round * (rounds_finished + 1)) /* Do next round of churn */
1759 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Current churn round %u, real round %u, scheduling next round of churn.\n", current_churn_round, rounds_finished + 1);
1763 if (dhtlog_handle != NULL)
1764 dhtlog_handle->insert_round(DHT_ROUND_CHURN, rounds_finished);
1766 GNUNET_SCHEDULER_add_now(&churn_peers, NULL);
1768 else if (rounds_finished < total_rounds - 1) /* Start a new complete round */
1773 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Round %u of %llu finished, scheduling next round.\n", rounds_finished, total_rounds);
1775 /** We reset the peer daemon for puts and gets on each disconnect, so all we need to do is start another round! */
1776 if (GNUNET_YES == in_dht_replication) /* Replication done in DHT, don't redo puts! */
1778 if (dhtlog_handle != NULL)
1779 dhtlog_handle->insert_round(DHT_ROUND_GET, rounds_finished);
1781 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add(GNUNET_TIME_relative_add(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, round_delay), all_get_timeout), DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1782 &end_badly, "from do gets (next round)");
1783 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, round_delay), &do_get, all_gets);
1787 if (dhtlog_handle != NULL)
1788 dhtlog_handle->insert_round(DHT_ROUND_NORMAL, rounds_finished);
1789 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, round_delay), GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, num_puts * 2)),
1790 &end_badly, "from do puts");
1791 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, round_delay), &do_put, all_puts);
1798 * Task to release get handle.
1801 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1803 struct TestGetContext *test_get = cls;
1805 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
1808 cumulative_successful_gets++;
1810 GNUNET_assert(test_get->get_handle != NULL);
1811 GNUNET_DHT_get_stop(test_get->get_handle);
1812 test_get->get_handle = NULL;
1813 test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
1814 GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
1818 * Iterator called if the GET request initiated returns a response.
1820 * @param cls closure
1821 * @param exp when will this value expire
1822 * @param key key of the result
1823 * @param get_path NULL-terminated array of pointers
1824 * to the peers on reverse GET path (or NULL if not recorded)
1825 * @param put_path NULL-terminated array of pointers
1826 * to the peers on the PUT path (or NULL if not recorded)
1827 * @param type type of the result
1828 * @param size number of bytes in data
1829 * @param data pointer to the result data
1832 get_result_iterator (void *cls,
1833 struct GNUNET_TIME_Absolute exp,
1834 const GNUNET_HashCode * key,
1835 const struct GNUNET_PeerIdentity * const *get_path,
1836 const struct GNUNET_PeerIdentity * const *put_path,
1837 enum GNUNET_BLOCK_Type type,
1841 struct TestGetContext *test_get = cls;
1843 if (test_get->succeeded == GNUNET_YES)
1844 return; /* Get has already been successful, probably ending now */
1846 if (0 != memcmp(&known_keys[test_get->uid], key, sizeof (GNUNET_HashCode))) /* || (0 != memcmp(original_data, data, sizeof(original_data))))*/
1849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Key or data is not the same as was inserted!\n");
1854 test_get->succeeded = GNUNET_YES;
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
1859 GNUNET_SCHEDULER_cancel(test_get->disconnect_task);
1860 GNUNET_SCHEDULER_add_continuation(&get_stop_task, test_get, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1866 * Set up some data, and call API PUT function
1869 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1871 struct TestGetContext *test_get = cls;
1875 GNUNET_SCHEDULER_cancel(die_task);
1876 GNUNET_SCHEDULER_add_now(&finish_testing, NULL);
1879 if (test_get == NULL)
1880 return; /* End of the list */
1882 /* Set this here in case we are re-running gets */
1883 test_get->succeeded = GNUNET_NO;
1885 if (GNUNET_YES != GNUNET_TESTING_daemon_running(test_get->daemon)) /* If the peer has been churned off, don't try issuing request from it! */
1887 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer we should issue get request from is down, skipping.\n");
1889 GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
1890 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
1894 /* Check if more gets are outstanding than should be */
1895 if (outstanding_gets > max_outstanding_gets)
1897 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200), &do_get, test_get);
1901 /* Connect to the first peer's DHT */
1902 test_get->dht_handle = GNUNET_DHT_connect(test_get->daemon->cfg, 10);
1903 GNUNET_assert(test_get->dht_handle != NULL);
1906 cumulative_num_gets++;
1907 /* Insert the data at the first peer */
1908 test_get->get_handle = GNUNET_DHT_get_start(test_get->dht_handle,
1910 GNUNET_BLOCK_TYPE_TEST,
1911 &known_keys[test_get->uid],
1916 &get_result_iterator,
1920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
1922 test_get->daemon->shortname);
1924 test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed(get_timeout, &get_stop_task, test_get);
1926 /* Schedule the next request in the linked list of get requests */
1927 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
1931 * Called when the PUT request has been transmitted to the DHT service.
1932 * Schedule the GET request for some time in the future.
1935 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1937 struct TestPutContext *test_put = cls;
1938 struct TopologyIteratorContext *topo_ctx;
1942 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
1943 fprintf(stderr, "PUT Request failed!\n");
1945 /* Reset the daemon (which peer to insert at) for later put request iterations */
1946 if (replicate_same == GNUNET_NO)
1947 test_put->daemon = GNUNET_TESTING_daemon_get(pg, GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers));
1949 GNUNET_SCHEDULER_cancel(test_put->disconnect_task);
1950 test_put->disconnect_task = GNUNET_SCHEDULER_add_now(&put_disconnect_task, test_put);
1951 if (GNUNET_YES == update_meter(put_meter))
1953 GNUNET_assert(outstanding_puts == 0);
1954 GNUNET_SCHEDULER_cancel (die_task);
1955 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
1957 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
1958 topo_ctx->cont = &do_get;
1959 topo_ctx->cls = all_gets;
1960 topo_ctx->timeout = DEFAULT_GET_TIMEOUT;
1961 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
1962 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add(GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout), DEFAULT_TOPOLOGY_CAPTURE_TIMEOUT),
1963 &end_badly, "from do gets (put finished)");
1964 GNUNET_SCHEDULER_add_now(&capture_current_topology, topo_ctx);
1968 fprintf(stderr, "Scheduling die task (put finished)\n");
1969 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_add(DEFAULT_GET_TIMEOUT, all_get_timeout),
1970 &end_badly, "from do gets (put finished)");
1971 GNUNET_SCHEDULER_add_delayed(DEFAULT_GET_TIMEOUT, &do_get, all_gets);
1978 * Set up some data, and call API PUT function
1981 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
1983 struct TestPutContext *test_put = cls;
1984 char data[test_data_size]; /* Made up data to store */
1988 if (test_put == NULL)
1989 return; /* End of list */
1991 if (GNUNET_YES != GNUNET_TESTING_daemon_running(test_put->daemon)) /* If the peer has been churned off, don't try issuing request from it! */
1993 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer we should issue put request at is down, skipping.\n");
1994 update_meter(put_meter);
1995 GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
1999 for (i = 0; i < sizeof(data); i++)
2001 memset(&data[i], GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX), 1);
2004 if (outstanding_puts > max_outstanding_puts)
2006 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200), &do_put, test_put);
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
2013 test_put->daemon->shortname);
2015 test_put->dht_handle = GNUNET_DHT_connect(test_put->daemon->cfg, 10);
2017 GNUNET_assert(test_put->dht_handle != NULL);
2019 GNUNET_DHT_put(test_put->dht_handle,
2020 &known_keys[test_put->uid],
2023 GNUNET_BLOCK_TYPE_TEST,
2025 GNUNET_TIME_UNIT_FOREVER_ABS,
2027 &put_finished, test_put);
2028 test_put->disconnect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_get_forever(), &put_disconnect_task, test_put);
2029 rand = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 2);
2030 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, rand), &do_put, test_put->next);
2034 schedule_find_peer_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
2037 setup_malicious_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
2040 * Given a number of total peers and a bucket size, estimate the number of
2041 * connections in a perfect kademlia topology.
2043 static unsigned int connection_estimate(unsigned int peer_count, unsigned int bucket_size)
2046 unsigned int filled;
2050 while (i >= bucket_size)
2055 filled++; /* Add one filled bucket to account for one "half full" and some miscellaneous */
2056 return filled * bucket_size * peer_count;
2062 * Callback for iterating over all the peer connections of a peer group.
2065 count_peers_cb (void *cls,
2066 const struct GNUNET_PeerIdentity *first,
2067 const struct GNUNET_PeerIdentity *second,
2070 struct FindPeerContext *find_peer_context = cls;
2071 if ((first != NULL) && (second != NULL))
2073 add_new_connection(find_peer_context, first, second);
2074 find_peer_context->current_peers++;
2078 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer count finished (%u connections), %u new peers, connection estimate %u (target %u)\n",
2079 find_peer_context->current_peers,
2080 find_peer_context->current_peers - find_peer_context->previous_peers,
2081 connection_estimate(num_peers, DEFAULT_BUCKET_SIZE),
2082 target_total_connections);
2084 if ((find_peer_context->last_sent < 8) ||
2085 ((find_peer_context->current_peers - find_peer_context->previous_peers > FIND_PEER_THRESHOLD) &&
2086 (find_peer_context->current_peers < 2 * connection_estimate(num_peers, DEFAULT_BUCKET_SIZE)) &&
2087 (GNUNET_TIME_absolute_get_remaining(find_peer_context->endtime).rel_value > 0) &&
2088 (find_peer_context->current_peers < target_total_connections)))
2090 GNUNET_SCHEDULER_add_now(&schedule_find_peer_requests, find_peer_context);
2094 GNUNET_CONTAINER_multihashmap_iterate(find_peer_context->peer_hash, &remove_peer_count, find_peer_context);
2095 GNUNET_CONTAINER_multihashmap_destroy(find_peer_context->peer_hash);
2096 GNUNET_CONTAINER_heap_destroy(find_peer_context->peer_min_heap);
2097 GNUNET_free(find_peer_context);
2098 fprintf(stderr, "Not sending any more find peer requests.\n");
2101 if (GNUNET_YES == malicious_after_settle)
2103 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "calling setup_malicious_peers\n");
2104 GNUNET_SCHEDULER_add_now(&setup_malicious_peers, NULL);
2113 * Set up a single find peer request for each peer in the topology. Do this
2114 * until the settle time is over, limited by the number of outstanding requests
2115 * and the time allowed for each one!
2118 schedule_find_peer_requests (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2120 struct FindPeerContext *find_peer_ctx = cls;
2121 struct TestFindPeer *test_find_peer;
2122 struct PeerCount *peer_count;
2126 if (find_peer_ctx->previous_peers == 0) /* First time, go slowly */
2127 find_peer_ctx->total = 1;
2128 else if (find_peer_ctx->current_peers - find_peer_ctx->previous_peers > MAX_FIND_PEER_CUTOFF) /* Found LOTS of peers, still go slowly */
2129 find_peer_ctx->total = find_peer_ctx->last_sent - (find_peer_ctx->last_sent / 8);
2131 find_peer_ctx->total = find_peer_ctx->last_sent * 2;
2133 if (find_peer_ctx->total > max_outstanding_find_peers)
2134 find_peer_ctx->total = max_outstanding_find_peers;
2136 if (find_peer_ctx->total > num_peers) /* Don't try to send more messages than we have peers! */
2137 find_peer_ctx->total = num_peers;
2139 find_peer_ctx->last_sent = find_peer_ctx->total;
2140 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Sending %u find peer messages (goal at least %u connections)\n", find_peer_ctx->total, target_total_connections);
2142 find_peer_offset = GNUNET_TIME_relative_divide(find_peer_delay, find_peer_ctx->total);
2143 for (i = 0; i < find_peer_ctx->total; i++)
2145 test_find_peer = GNUNET_malloc(sizeof(struct TestFindPeer));
2146 /* If we haven't sent any requests yet, choose random peers */
2147 /* Also choose random in _half_ of all cases, so we don't
2148 * get stuck choosing topologically restricted peers with
2149 * few connections that will never be able to find any new
2151 if ((find_peer_ctx->previous_peers == 0) || (i % 2 == 0))
2154 * Attempt to spread find peer requests across even sections of the peer address
2155 * space. Choose basically 1 peer in every num_peers / max_outstanding_requests
2156 * each time, then offset it by a randomish value.
2158 * For instance, if num_peers is 100 and max_outstanding is 10, first chosen peer
2159 * will be between 0 - 10, second between 10 - 20, etc.
2161 random = (num_peers / find_peer_ctx->total) * i;
2162 random = random + GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, (num_peers / find_peer_ctx->total));
2163 if (random >= num_peers)
2165 random = random - num_peers;
2168 random = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2170 test_find_peer->daemon = GNUNET_TESTING_daemon_get(pg, random);
2172 else /* If we have sent requests, choose peers with a low number of connections to send requests from */
2174 peer_count = GNUNET_CONTAINER_heap_remove_root(find_peer_ctx->peer_min_heap);
2175 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(find_peer_ctx->peer_hash, &peer_count->peer_id.hashPubKey, peer_count));
2176 test_find_peer->daemon = GNUNET_TESTING_daemon_get_by_id(pg, &peer_count->peer_id);
2177 GNUNET_assert(test_find_peer->daemon != NULL);
2180 test_find_peer->find_peer_context = find_peer_ctx;
2181 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(find_peer_offset, i), &send_find_peer_request, test_find_peer);
2184 if ((find_peer_ctx->peer_hash == NULL) && (find_peer_ctx->peer_min_heap == NULL))
2186 find_peer_ctx->peer_hash = GNUNET_CONTAINER_multihashmap_create(num_peers);
2187 find_peer_ctx->peer_min_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
2191 GNUNET_CONTAINER_multihashmap_iterate(find_peer_ctx->peer_hash, &remove_peer_count, find_peer_ctx);
2192 GNUNET_CONTAINER_multihashmap_destroy(find_peer_ctx->peer_hash);
2193 find_peer_ctx->peer_hash = GNUNET_CONTAINER_multihashmap_create(num_peers);
2196 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap_size(find_peer_ctx->peer_hash));
2197 GNUNET_assert(0 == GNUNET_CONTAINER_heap_get_size(find_peer_ctx->peer_min_heap));
2202 * Convert unique ID to hash code.
2204 * @param uid unique ID to convert
2205 * @param hash set to uid (extended with zeros)
2208 hash_from_uid (uint32_t uid, GNUNET_HashCode *hash)
2210 memset (hash, 0, sizeof (GNUNET_HashCode));
2211 *((uint32_t *) hash) = uid;
2216 * Set up all of the put and get operations we want to do
2217 * in the current round. Allocate data structure for each,
2218 * add to list, then schedule the actual PUT operations.
2221 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2224 struct TestPutContext *test_put;
2225 struct TestGetContext *test_get;
2227 GNUNET_HashCode uid_hash;
2230 int remember[num_puts][num_peers];
2231 memset(&remember, 0, sizeof(int) * num_puts * num_peers);
2233 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "in setup_puts_and_gets\n");
2234 known_keys = GNUNET_malloc(sizeof(GNUNET_HashCode) * num_puts);
2235 for (i = 0; i < num_puts; i++)
2237 test_put = GNUNET_malloc(sizeof(struct TestPutContext));
2239 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &known_keys[i]);
2240 /* Set first X bits to match the chosen sybil location if we want to do the sybil attack! */
2241 if (GNUNET_YES == malicious_sybil)
2243 memcpy(&known_keys[i], &sybil_target, sizeof(GNUNET_HashCode) / 2);
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Distance between sybil location and key is %d\n", GNUNET_CRYPTO_hash_matching_bits(&known_keys[i], &sybil_target));
2246 temp_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2247 test_put->daemon = GNUNET_TESTING_daemon_get(pg, temp_peer);
2248 /* Don't start PUTs at malicious peers! */
2249 if (malicious_bloom != NULL)
2252 hash_from_uid(temp_peer, &uid_hash);
2253 while ((GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(malicious_bloom, &uid_hash)) && (count < num_peers))
2255 temp_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2256 hash_from_uid(temp_peer, &uid_hash);
2257 test_put->daemon = GNUNET_TESTING_daemon_get(pg, temp_peer);
2260 if (count == num_peers)
2261 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't find peer not in malicious bloom to select!\n");
2264 test_put->next = all_puts;
2265 all_puts = test_put;
2268 for (i = 0; i < num_gets; i++)
2270 test_get = GNUNET_malloc(sizeof(struct TestGetContext));
2271 test_get->uid = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
2273 while (remember[test_get->uid][temp_daemon] == 1)
2274 temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2275 remember[test_get->uid][temp_daemon] = 1;
2277 temp_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2278 test_get->daemon = GNUNET_TESTING_daemon_get(pg, temp_peer);
2279 /* Don't start GETs at malicious peers! */
2280 if (malicious_bloom != NULL)
2282 hash_from_uid(temp_peer, &uid_hash);
2284 while ((GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test(malicious_bloom, &uid_hash)) && (count < num_peers))
2286 temp_peer = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2287 hash_from_uid(temp_peer, &uid_hash);
2288 test_get->daemon = GNUNET_TESTING_daemon_get(pg, temp_peer);
2291 if (count == num_peers)
2292 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't find peer not in malicious bloom to select!\n");
2294 test_get->next = all_gets;
2295 all_gets = test_get;
2298 /*GNUNET_SCHEDULER_cancel (die_task);*/
2299 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, num_puts * 2),
2300 &end_badly, "from do puts");
2301 GNUNET_SCHEDULER_add_now (&do_put, all_puts);
2306 * Set up some all of the put and get operations we want
2307 * to do. Allocate data structure for each, add to list,
2308 * then call actual insert functions.
2311 continue_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2315 struct TopologyIteratorContext *topo_ctx;
2316 struct FindPeerContext *find_peer_context;
2317 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "In continue_puts_and_gets\n");
2318 if ((dhtlog_handle != NULL) && (GNUNET_NO == dhtlog_minimal))
2320 if (settle_time >= 180 * 2)
2321 max = (settle_time / 180) - 2;
2324 for (i = 1; i < max; i++)
2326 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2327 topo_ctx->current_iteration = i;
2328 topo_ctx->total_iterations = max;
2329 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
2330 //fprintf(stderr, "scheduled topology iteration in %d minutes\n", i);
2331 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, i * 3), &capture_current_topology, topo_ctx);
2333 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2334 topo_ctx->cont = &setup_puts_and_gets;
2335 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
2336 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "setting setup_puts_and_gets for %d seconds in the future\n", settle_time + 10);
2337 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, (settle_time + 10)), &capture_current_topology, topo_ctx);
2340 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, (settle_time + 10)), &setup_puts_and_gets, NULL);
2342 if (dhtlog_handle != NULL)
2343 dhtlog_handle->insert_round(DHT_ROUND_NORMAL, rounds_finished);
2346 if ((GNUNET_YES != malicious_after_settle) || (settle_time == 0))
2348 GNUNET_SCHEDULER_add_now(&setup_malicious_peers, NULL);
2352 if ((GNUNET_YES == do_find_peer) && (settle_time > 0))
2354 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Scheduling find peer requests during \"settle\" time.\n");
2355 find_peer_context = GNUNET_malloc(sizeof(struct FindPeerContext));
2356 find_peer_context->count_peers_cb = &count_peers_cb;
2357 find_peer_context->endtime = GNUNET_TIME_relative_to_absolute(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, settle_time));
2358 GNUNET_SCHEDULER_add_now(&schedule_find_peer_requests, find_peer_context);
2362 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Assuming automatic DHT find peer requests.\n");
2367 * Task to release DHT handles
2370 malicious_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2372 struct MaliciousContext *ctx = cls;
2373 outstanding_malicious--;
2374 malicious_completed++;
2375 ctx->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
2376 GNUNET_DHT_disconnect(ctx->dht_handle);
2377 ctx->dht_handle = NULL;
2380 if (malicious_completed == malicious_getters + malicious_putters + malicious_droppers)
2382 fprintf(stderr, "Finished setting all malicious peers up!\n");
2388 * Task to release DHT handles
2391 malicious_done_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2393 struct MaliciousContext *ctx = cls;
2394 GNUNET_SCHEDULER_cancel(ctx->disconnect_task);
2395 GNUNET_SCHEDULER_add_now(&malicious_disconnect_task, ctx);
2399 * Set up some data, and call API PUT function
2402 set_malicious (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2404 struct MaliciousContext *ctx = cls;
2406 if (outstanding_malicious > DEFAULT_MAX_OUTSTANDING_GETS)
2408 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &set_malicious, ctx);
2412 if (ctx->dht_handle == NULL)
2414 ctx->dht_handle = GNUNET_DHT_connect(ctx->daemon->cfg, 1);
2415 outstanding_malicious++;
2418 GNUNET_assert(ctx->dht_handle != NULL);
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting peer %s malicious type %d\n",
2423 ctx->daemon->shortname, ctx->malicious_type);
2426 switch (ctx->malicious_type)
2428 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET:
2429 GNUNET_DHT_set_malicious_getter(ctx->dht_handle, malicious_get_frequency);
2430 GNUNET_SCHEDULER_add_now (&malicious_done_task, ctx);
2432 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT:
2433 GNUNET_DHT_set_malicious_putter(ctx->dht_handle, malicious_put_frequency);
2434 GNUNET_SCHEDULER_add_now (&malicious_done_task, ctx);
2436 case GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP:
2437 GNUNET_DHT_set_malicious_dropper(ctx->dht_handle);
2438 GNUNET_SCHEDULER_add_now (&malicious_done_task, ctx);
2444 ctx->disconnect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL,
2445 &malicious_disconnect_task, ctx);
2451 * Choose the next peer from the peer group to set as malicious.
2452 * If we are doing a sybil attack, find the nearest peer to the
2453 * sybil location that has not already been set malicious. Otherwise
2454 * just choose a random not already chosen peer.
2456 * @param pg the peer group
2457 * @param bloom the bloomfilter which contains all peer already
2458 * chosen to be malicious
2461 choose_next_malicious (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_CONTAINER_BloomFilter *bloom)
2468 struct GNUNET_TESTING_Daemon *temp_daemon;
2469 GNUNET_HashCode uid_hash;
2473 GNUNET_assert (bloom != NULL);
2475 if (GNUNET_YES == malicious_sybil)
2477 for (i = 0; i < num_peers; i++)
2479 temp_daemon = GNUNET_TESTING_daemon_get(pg, i);
2480 hash_from_uid(i, &uid_hash);
2481 /* Check if this peer matches the bloomfilter */
2482 if ((GNUNET_NO == GNUNET_TESTING_daemon_running(temp_daemon)) || (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &uid_hash)))
2485 bits_match = GNUNET_CRYPTO_hash_matching_bits (&temp_daemon->id.hashPubKey, &sybil_target);
2486 if (bits_match >= curr_distance)
2488 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);
2490 curr_distance = bits_match;
2496 nearest = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2497 hash_from_uid(nearest, &uid_hash);
2499 while ((GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bloom, &uid_hash)) && (count < num_peers))
2501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %d already in bloom (tried %d times)\n", nearest, count);
2502 nearest = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
2503 hash_from_uid(nearest, &uid_hash);
2506 if (count == num_peers)
2507 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Tried %d times to find a peer, selecting %d at random!!\n", count, nearest);
2514 * Select randomly from set of known peers,
2515 * set the desired number of peers to the
2516 * proper malicious types.
2519 setup_malicious_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2521 struct MaliciousContext *ctx;
2523 uint32_t temp_daemon;
2524 GNUNET_HashCode uid_hash;
2526 for (i = 0; i < malicious_getters; i++)
2528 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2529 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2530 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2531 hash_from_uid(temp_daemon, &uid_hash);
2532 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2533 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_GET;
2534 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2538 for (i = 0; i < malicious_putters; i++)
2540 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2541 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2542 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2543 hash_from_uid(temp_daemon, &uid_hash);
2544 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2545 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_PUT;
2546 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2550 for (i = 0; i < malicious_droppers; i++)
2552 ctx = GNUNET_malloc(sizeof(struct MaliciousContext));
2553 temp_daemon = choose_next_malicious(pg, malicious_bloom);
2554 ctx->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
2555 hash_from_uid(temp_daemon, &uid_hash);
2556 GNUNET_CONTAINER_bloomfilter_add(malicious_bloom, &uid_hash);
2557 ctx->malicious_type = GNUNET_MESSAGE_TYPE_DHT_MALICIOUS_DROP;
2558 GNUNET_SCHEDULER_add_now (&set_malicious, ctx);
2564 /* Forward declaration */
2566 topology_callback (void *cls,
2567 const struct GNUNET_PeerIdentity *first,
2568 const struct GNUNET_PeerIdentity *second,
2570 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2571 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2572 struct GNUNET_TESTING_Daemon *first_daemon,
2573 struct GNUNET_TESTING_Daemon *second_daemon,
2577 * Retry connecting two specific peers until they connect,
2578 * at a specific interval. These two peers previously failed
2579 * to connect, and we hope they continue to so that we can
2580 * debug the reason they are having issues.
2583 repeat_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
2586 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Repeating connect attempt between %s and %s.\n", repeat_connect_peer1->shortname, repeat_connect_peer2->shortname);
2587 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer 1 configuration `%s'\n", repeat_connect_peer1->cfgfile);
2588 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peer 2 configuration `%s'\n", repeat_connect_peer2->cfgfile);
2590 repeat_connect_task = GNUNET_SCHEDULER_NO_TASK;
2591 GNUNET_TESTING_daemons_connect(repeat_connect_peer1,
2592 repeat_connect_peer2,
2593 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
2594 2, &topology_callback, NULL);
2599 * This function is called whenever a connection attempt is finished between two of
2600 * the started peers (started with GNUNET_TESTING_daemons_start). The total
2601 * number of times this function is called should equal the number returned
2602 * from the GNUNET_TESTING_connect_topology call.
2604 * The emsg variable is NULL on success (peers connected), and non-NULL on
2605 * failure (peers failed to connect).
2608 topology_callback (void *cls,
2609 const struct GNUNET_PeerIdentity *first,
2610 const struct GNUNET_PeerIdentity *second,
2612 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
2613 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
2614 struct GNUNET_TESTING_Daemon *first_daemon,
2615 struct GNUNET_TESTING_Daemon *second_daemon,
2618 struct TopologyIteratorContext *topo_ctx;
2619 unsigned long long duration;
2620 unsigned long long total_duration;
2621 unsigned int new_connections;
2622 unsigned int new_failed_connections;
2623 double conns_per_sec_recent;
2624 double conns_per_sec_total;
2625 double failed_conns_per_sec_recent;
2626 double failed_conns_per_sec_total;
2629 if (repeat_connect_mode == GNUNET_YES)
2631 if ((first_daemon == repeat_connect_peer1) &&
2632 (second_daemon == repeat_connect_peer2))
2634 if (emsg != NULL) /* Peers failed to connect again! */
2636 GNUNET_assert(repeat_connect_task == GNUNET_SCHEDULER_NO_TASK);
2637 repeat_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &repeat_connect, NULL);
2640 else /* Repeat peers actually connected! */
2642 if (repeat_connect_task != GNUNET_SCHEDULER_NO_TASK)
2643 GNUNET_SCHEDULER_cancel(repeat_connect_task);
2644 repeat_connect_peer1 = NULL;
2645 repeat_connect_peer2 = NULL;
2646 repeat_connect_mode = GNUNET_NO;
2647 GNUNET_TESTING_resume_connections(pg);
2648 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Resuming normal connection mode, debug connection was successful!\n");
2654 if (GNUNET_TIME_absolute_get_difference (connect_last_time,
2655 GNUNET_TIME_absolute_get()).rel_value > GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CONN_UPDATE_DURATION).rel_value)
2657 /* Get number of new connections */
2658 new_connections = total_connections - previous_connections;
2660 /* Get number of new FAILED connections */
2661 new_failed_connections = failed_connections - previous_failed_connections;
2663 /* Get duration in seconds */
2664 duration = GNUNET_TIME_absolute_get_difference (connect_last_time,
2665 GNUNET_TIME_absolute_get()).rel_value / 1000;
2666 total_duration = GNUNET_TIME_absolute_get_difference (connect_start_time,
2667 GNUNET_TIME_absolute_get()).rel_value / 1000;
2669 failed_conns_per_sec_recent = (double)new_failed_connections / duration;
2670 failed_conns_per_sec_total = (double)failed_connections / total_duration;
2671 conns_per_sec_recent = (double)new_connections / duration;
2672 conns_per_sec_total = (double)total_connections / total_duration;
2673 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Recent: %.2f/s, Total: %.2f/s, Recent failed: %.2f/s, total failed %.2f/s\n",
2674 conns_per_sec_recent, CONN_UPDATE_DURATION, conns_per_sec_total,
2675 failed_conns_per_sec_recent, failed_conns_per_sec_total);
2676 connect_last_time = GNUNET_TIME_absolute_get();
2677 previous_connections = total_connections;
2678 previous_failed_connections = failed_connections;
2679 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "have %u total_connections, %u failed\n", total_connections, failed_connections);
2681 /* These conditions likely mean we've entered the death spiral of doom */
2682 if ((total_connections > 20000) &&
2683 (conns_per_sec_recent < 5.0) &&
2684 (conns_per_sec_total > 10.0) &&
2686 (repeat_connect_mode == GNUNET_NO))
2688 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Entering repeat connection attempt mode!\n");
2689 repeat_connect_peer1 = first_daemon;
2690 repeat_connect_peer2 = second_daemon;
2691 repeat_connect_mode = GNUNET_YES;
2692 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping NEW connections from being scheduled!\n");
2693 GNUNET_TESTING_stop_connections(pg);
2694 repeat_connect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60), &repeat_connect, NULL);
2701 total_connections++;
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
2704 first_daemon->shortname,
2705 second_daemon->shortname,
2711 failed_connections++;
2713 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to connect peer %s to peer %s with error :\n%s\n",
2714 first_daemon->shortname,
2715 second_daemon->shortname, emsg);
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
2718 first_daemon->shortname,
2719 second_daemon->shortname, emsg);
2724 if ((repeat_connect_mode == GNUNET_YES) )
2728 GNUNET_assert(peer_connect_meter != NULL);
2729 if (GNUNET_YES == update_meter(peer_connect_meter))
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2733 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
2736 if (failed_connections > 0)
2737 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "While connecting, had %u failed connections.\n", failed_connections);
2738 if (dhtlog_handle != NULL)
2740 dhtlog_handle->update_connections (total_connections);
2741 dhtlog_handle->insert_topology(expected_connections);
2745 total_duration = GNUNET_TIME_absolute_get_difference (connect_start_time,
2746 GNUNET_TIME_absolute_get()).rel_value / 1000;
2747 failed_conns_per_sec_total = (double)failed_connections / total_duration;
2748 conns_per_sec_total = (double)total_connections / total_duration;
2749 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Overall connection info --- Total: %u, Total Failed %u/s\n",
2750 total_connections, failed_connections);
2751 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Overall connection info --- Total: %.2f/s, Total Failed %.2f/s\n",
2752 conns_per_sec_total, failed_conns_per_sec_total);
2753 /** Comment out until gauger is ready */
2755 GNUNET_asprintf(&temp_conn_string, "dht_peer_connection_speed");
2756 GNUNET_asprintf(&temp_failed_conn_string, "dht_peer_failed_connection_speed");
2758 GAUGER_COUNTER(temp_conn_string, conns_per_sec_total, trial_to_run, DATE);
2759 GAUGER_COUNTER(temp_failed_conn_string, failed_conns_per_sec_total, trial_to_run, DATE);
2761 GNUNET_free(temp_conn_string);
2762 GNUNET_free(temp_failed_conn_string);
2765 GNUNET_SCHEDULER_cancel (die_task);
2767 if ((GNUNET_YES == dhtlog_minimal) && (NULL != dhtlog_handle))
2769 topo_ctx = GNUNET_malloc(sizeof(struct TopologyIteratorContext));
2770 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Setting continue gets and puts as topo_cont\n");
2771 topo_ctx->cont = &continue_puts_and_gets;
2772 topo_ctx->peers_seen = GNUNET_CONTAINER_multihashmap_create(num_peers);
2773 GNUNET_SCHEDULER_add_now(&capture_current_topology, topo_ctx);
2777 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "For some reason, NOT scheduling final topology capture (settle_time %d, dhtlog_handle %s)!\n", settle_time, dhtlog_handle);
2778 GNUNET_SCHEDULER_add_now(&continue_puts_and_gets, NULL);
2781 else if (total_connections + failed_connections == expected_connections)
2783 GNUNET_SCHEDULER_cancel (die_task);
2784 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from topology_callback (too many failed connections)");
2789 peers_started_callback (void *cls,
2790 const struct GNUNET_PeerIdentity *id,
2791 const struct GNUNET_CONFIGURATION_Handle *cfg,
2792 struct GNUNET_TESTING_Daemon *d, const char *emsg)
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
2800 GNUNET_assert (id != NULL);
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
2804 (num_peers - peers_left) + 1, num_peers);
2809 if (GNUNET_YES == update_meter(peer_start_meter))
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2813 "All %d daemons started, now connecting peers!\n",
2816 GNUNET_SCHEDULER_cancel (die_task);
2818 expected_connections = UINT_MAX;
2819 if ((pg != NULL) && (peers_left == 0))
2821 connect_start_time = GNUNET_TIME_absolute_get();
2822 expected_connections = GNUNET_TESTING_connect_topology(pg,
2823 connect_topology, connect_topology_option,
2824 connect_topology_option_modifier,
2825 connect_timeout, connect_attempts,
2828 peer_connect_meter = create_meter(expected_connections, "Peer connection ", GNUNET_YES);
2829 fprintf(stderr, "Have %d expected connections\n", expected_connections);
2832 if (expected_connections == GNUNET_SYSERR)
2834 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from connect topology (bad return)");
2837 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, DEFAULT_CONNECT_TIMEOUT * expected_connections),
2838 &end_badly, "from connect topology (timeout)");
2847 peers_left = num_peers; /* Reset counter */
2848 if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
2850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2851 "Topology set up, now starting peers!\n");
2852 GNUNET_TESTING_daemons_continue_startup(pg);
2856 GNUNET_SCHEDULER_cancel (die_task);
2857 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from create topology (bad return)");
2859 GNUNET_free_non_null(blacklist_transports);
2860 GNUNET_SCHEDULER_cancel (die_task);
2861 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
2862 &end_badly, "from continue startup (timeout)");
2866 * Callback indicating that the hostkey was created for a peer.
2869 * @param id the peer identity
2870 * @param d the daemon handle (pretty useless at this point, remove?)
2871 * @param emsg non-null on failure
2874 hostkey_callback (void *cls,
2875 const struct GNUNET_PeerIdentity *id,
2876 struct GNUNET_TESTING_Daemon *d,
2881 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2886 "Hostkey (%d/%d) created for peer `%s'\n",
2887 num_peers - peers_left, num_peers, GNUNET_i2s(id));
2891 if (GNUNET_YES == update_meter(hostkey_meter))
2893 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2894 "All %d hostkeys created, now creating topology!\n",
2896 GNUNET_SCHEDULER_cancel (die_task);
2897 /* Set up task in case topology creation doesn't finish
2898 * within a reasonable amount of time */
2899 die_task = GNUNET_SCHEDULER_add_delayed (DEFAULT_TOPOLOGY_TIMEOUT,
2900 &end_badly, "from create_topology");
2901 GNUNET_SCHEDULER_add_now(&create_topology, NULL);
2910 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
2913 struct GNUNET_DHTLOG_TrialInfo trial_info;
2914 struct GNUNET_TESTING_Host *hosts;
2915 struct GNUNET_TESTING_Host *temphost;
2916 struct GNUNET_TESTING_Host *tempnext;
2918 char *connect_topology_str;
2919 char *blacklist_topology_str;
2920 char *connect_topology_option_str;
2921 char *connect_topology_option_modifier_string;
2923 char *topology_percentage_str;
2924 float topology_percentage;
2925 char *topology_probability_str;
2927 float topology_probability;
2928 unsigned long long temp_config_number;
2929 unsigned long long trial_to_run;
2932 int strict_kademlia;
2936 char *churn_filename;
2943 rounds_finished = 0;
2944 memset(&trial_info, 0, sizeof(struct GNUNET_DHTLOG_TrialInfo));
2945 /* Get path from configuration file */
2946 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
2952 /* Get number of peers to start from configuration */
2953 if (GNUNET_SYSERR ==
2954 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
2957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2958 "Number of peers must be specified in section %s option %s\n", "TESTING", "NUM_PEERS");
2960 GNUNET_assert(num_peers > 0 && num_peers < ULONG_MAX);
2963 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_timeout",
2964 &temp_config_number))
2966 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_config_number);
2969 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_timeout");
2975 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "connect_attempts",
2978 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "connect_attempts");
2983 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "max_outstanding_connections",
2984 &max_outstanding_connections))
2986 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "max_outstanding_connections");
2991 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "max_concurrent_ssh",
2992 &max_concurrent_ssh))
2994 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Must provide option %s:%s!\n", "testing", "max_concurrent_ssh");
2999 * Get DHT specific testing options.
3001 if ((GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging")) ||
3002 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging_extended")) ||
3003 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging_minimal")))
3005 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht_testing", "mysql_logging_minimal"))
3006 dhtlog_minimal = GNUNET_YES;
3008 dhtlog_handle = GNUNET_DHTLOG_connect(cfg);
3009 if (dhtlog_handle == NULL)
3011 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3012 "Could not connect to mysql server for logging, will NOT log dht operations!");
3018 stop_closest = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "stop_on_closest");
3019 if (stop_closest == GNUNET_SYSERR)
3020 stop_closest = GNUNET_NO;
3022 stop_found = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "stop_found");
3023 if (stop_found == GNUNET_SYSERR)
3024 stop_found = GNUNET_NO;
3026 strict_kademlia = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "strict_kademlia");
3027 if (strict_kademlia == GNUNET_SYSERR)
3028 strict_kademlia = GNUNET_NO;
3031 GNUNET_CONFIGURATION_get_value_string (cfg, "dht_testing", "comment",
3033 trialmessage = NULL;
3036 /** Check for a churn file to do churny simulation */
3038 GNUNET_CONFIGURATION_get_value_string(cfg, "dht_testing", "churn_file",
3041 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Reading churn data from %s\n", churn_filename);
3042 if (GNUNET_OK != GNUNET_DISK_file_test (churn_filename))
3044 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Error reading churn file!\n");
3045 GNUNET_free_non_null(trialmessage);
3046 GNUNET_free(churn_filename);
3049 if ((0 != STAT (churn_filename, &frstat)) || (frstat.st_size == 0))
3051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3052 "Could not open file specified for churn data, ending test!");
3054 GNUNET_free_non_null(trialmessage);
3055 GNUNET_free(churn_filename);
3059 churn_data = GNUNET_malloc_large (frstat.st_size);
3060 GNUNET_assert(churn_data != NULL);
3061 if (frstat.st_size !=
3062 GNUNET_DISK_fn_read (churn_filename, churn_data, frstat.st_size))
3064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3065 "Could not read file %s specified for churn, ending test!", churn_filename);
3066 GNUNET_free (churn_filename);
3067 GNUNET_free (churn_data);
3068 GNUNET_free_non_null(trialmessage);
3072 GNUNET_free_non_null(churn_filename);
3076 /* Read the first line */
3077 while (count < frstat.st_size)
3080 if (((churn_data[count] == '\n')) && (buf != &churn_data[count]))
3082 churn_data[count] = '\0';
3083 if (1 != sscanf(buf, "%u", &churn_rounds))
3085 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to read number of rounds from churn file, ending test!\n");
3087 GNUNET_free_non_null(trialmessage);
3088 GNUNET_free_non_null(churn_data);
3091 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u rounds from churn file\n", churn_rounds);
3092 buf = &churn_data[count + 1];
3093 churn_array = GNUNET_malloc(sizeof(unsigned int) * churn_rounds);
3094 break; /* Done with this part */
3098 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg, "dht_testing", "churns_per_round", &churns_per_round))
3100 churns_per_round = (unsigned long long)churn_rounds;
3104 while ((count < frstat.st_size) && (line_number < churn_rounds))
3107 if (((churn_data[count] == '\n')) && (buf != &churn_data[count]))
3109 churn_data[count] = '\0';
3111 ret = sscanf(buf, "%u", &churn_array[line_number]);
3114 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u peers in round %u\n", churn_array[line_number], line_number);
3119 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Error reading line `%s' in hostfile\n", buf);
3120 buf = &churn_data[count + 1];
3123 buf = &churn_data[count + 1];
3125 else if (churn_data[count] == '\n') /* Blank line */
3126 buf = &churn_data[count + 1];
3129 GNUNET_free_non_null(churn_data);
3131 /* Check for a hostfile containing user@host:port triples */
3133 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
3140 if (hostfile != NULL)
3142 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
3143 GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
3144 | GNUNET_DISK_PERM_USER_WRITE);
3145 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
3147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3148 "Could not open file specified for host list, ending test!");
3150 GNUNET_free_non_null(trialmessage);
3151 GNUNET_free(hostfile);
3155 data = GNUNET_malloc_large (frstat.st_size);
3156 GNUNET_assert(data != NULL);
3157 if (frstat.st_size !=
3158 GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
3160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3161 "Could not read file %s specified for host list, ending test!", hostfile);
3162 GNUNET_free (hostfile);
3164 GNUNET_free_non_null(trialmessage);
3168 GNUNET_free_non_null(hostfile);
3172 while (count < frstat.st_size - 1)
3175 /* if (((data[count] == '\n') || (data[count] == '\0')) && (buf != &data[count]))*/
3176 if (((data[count] == '\n')) && (buf != &data[count]))
3179 temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
3180 ret = sscanf(buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd", &temphost->username, &temphost->hostname, &temphost->port);
3183 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Successfully read host %s, port %d and user %s from file\n", temphost->hostname, temphost->port, temphost->username);
3187 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Error reading line `%s' in hostfile\n", buf);
3188 GNUNET_free(temphost);
3189 buf = &data[count + 1];
3192 /* temphost->hostname = buf; */
3193 temphost->next = hosts;
3195 buf = &data[count + 1];
3197 else if ((data[count] == '\n') || (data[count] == '\0'))
3198 buf = &data[count + 1];
3201 GNUNET_free_non_null(data);
3203 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_getters",
3204 &malicious_getters))
3205 malicious_getters = 0;
3208 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_putters",
3209 &malicious_putters))
3210 malicious_putters = 0;
3213 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "malicious_droppers",
3214 &malicious_droppers))
3215 malicious_droppers = 0;
3218 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "settle_time",
3222 if (GNUNET_SYSERR ==
3223 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_puts",
3225 num_puts = num_peers;
3227 if (GNUNET_SYSERR ==
3228 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "put_replication",
3230 put_replication = DEFAULT_PUT_REPLICATION;
3232 if (GNUNET_SYSERR ==
3233 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_gets",
3235 num_gets = num_peers;
3237 if (GNUNET_SYSERR ==
3238 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_replication",
3240 get_replication = DEFAULT_GET_REPLICATION;
3243 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "find_peer_delay",
3244 &temp_config_number))
3245 find_peer_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3247 find_peer_delay = DEFAULT_FIND_PEER_DELAY;
3250 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_find_peers",
3251 &temp_config_number))
3252 max_outstanding_find_peers = temp_config_number;
3254 max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
3257 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_timeout",
3258 &temp_config_number))
3259 get_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3261 get_timeout = DEFAULT_GET_TIMEOUT;
3264 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_puts",
3265 &temp_config_number))
3266 max_outstanding_puts = temp_config_number;
3268 max_outstanding_puts = DEFAULT_MAX_OUTSTANDING_PUTS;
3271 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "concurrent_gets",
3272 &temp_config_number))
3273 max_outstanding_gets = temp_config_number;
3275 max_outstanding_gets = DEFAULT_MAX_OUTSTANDING_GETS;
3278 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "timeout",
3279 &temp_config_number))
3280 all_get_timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3282 all_get_timeout.rel_value = get_timeout.rel_value * num_gets;
3285 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "get_delay",
3286 &temp_config_number))
3287 get_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3289 get_delay = DEFAULT_GET_DELAY;
3292 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "put_delay",
3293 &temp_config_number))
3294 put_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3296 put_delay = DEFAULT_PUT_DELAY;
3299 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "peer_start_timeout",
3300 &temp_config_number))
3301 seconds_per_peer_start = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3303 seconds_per_peer_start = DEFAULT_SECONDS_PER_PEER_START;
3306 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "data_size",
3307 &temp_config_number))
3308 test_data_size = temp_config_number;
3310 test_data_size = DEFAULT_TEST_DATA_SIZE;
3313 * Get testing related options.
3315 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "DHT_TESTING", "REPLICATE_SAME"))
3316 replicate_same = GNUNET_YES;
3319 * Get testing related options.
3321 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "DHT_TESTING", "GET_FROM_SAME"))
3322 get_from_same = GNUNET_YES;
3325 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_time (cfg, "DHT_TESTING",
3326 "MALICIOUS_GET_FREQUENCY",
3327 &malicious_get_frequency))
3328 malicious_get_frequency = DEFAULT_MALICIOUS_GET_FREQUENCY;
3331 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_time (cfg, "DHT_TESTING",
3332 "MALICIOUS_PUT_FREQUENCY",
3333 &malicious_put_frequency))
3334 malicious_put_frequency = DEFAULT_MALICIOUS_PUT_FREQUENCY;
3336 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "DHT_TESTING", "MALICIOUS_AFTER_SETTLE"))
3337 malicious_after_settle = GNUNET_YES;
3339 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "DHT_TESTING", "MALICIOUS_SYBIL"))
3341 /* Set up the malicious target at random for this round */
3342 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &sybil_target);
3343 malicious_sybil = GNUNET_YES;
3346 /* Create the bloomfilter for choosing which peers to set malicious */
3348 /* Bloomfilter size must be 2^k for some integer k */
3350 while (1 << k < malicious_droppers)
3352 if (malicious_droppers > 0)
3353 malicious_bloom = GNUNET_CONTAINER_bloomfilter_init (NULL, 1 << k, DHT_BLOOM_K);
3355 /* The normal behavior of the DHT is to do find peer requests
3356 * on its own. Only if this is explicitly turned off should
3357 * the testing driver issue find peer requests (even though
3358 * this is likely the default when testing).
3361 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
3364 do_find_peer = GNUNET_YES;
3368 GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht",
3371 in_dht_replication = GNUNET_YES;
3374 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3381 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3383 &temp_config_number))
3385 find_peer_delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, temp_config_number);
3388 find_peer_delay = DEFAULT_FIND_PEER_DELAY;
3390 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_number(cfg, "DHT_TESTING", "ROUND_DELAY", &round_delay))
3393 if (GNUNET_NO == GNUNET_CONFIGURATION_get_value_number (cfg, "DHT_TESTING",
3394 "OUTSTANDING_FIND_PEERS",
3395 &max_outstanding_find_peers))
3396 max_outstanding_find_peers = DEFAULT_MAX_OUTSTANDING_FIND_PEERS;
3398 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(cfg, "dht", "strict_kademlia"))
3399 max_outstanding_find_peers = max_outstanding_find_peers * 1;
3401 find_peer_offset = GNUNET_TIME_relative_divide (find_peer_delay, max_outstanding_find_peers);
3403 if (GNUNET_SYSERR ==
3404 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_rounds",
3410 if ((GNUNET_SYSERR ==
3411 GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "target_total_connections",
3412 &target_total_connections)) ||
3413 (target_total_connections == 0))
3415 target_total_connections = connection_estimate(num_peers, DEFAULT_BUCKET_SIZE);
3418 topology_str = NULL;
3420 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
3421 &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
3423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3424 "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
3425 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
3429 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "percentage",
3430 &topology_percentage_str))
3431 topology_percentage = 0.5;
3434 topology_percentage = atof (topology_percentage_str);
3435 GNUNET_free(topology_percentage_str);
3439 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "probability",
3440 &topology_probability_str))
3441 topology_probability = 0.5;
3444 topology_probability = atof (topology_probability_str);
3445 GNUNET_free(topology_probability_str);
3449 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
3450 &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connect_topology, connect_topology_str)))
3452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3453 "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
3455 GNUNET_free_non_null(connect_topology_str);
3458 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
3459 &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
3461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3462 "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
3463 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
3465 GNUNET_free_non_null(connect_topology_option_str);
3468 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
3469 &connect_topology_option_modifier_string))
3471 if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
3473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3474 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
3475 connect_topology_option_modifier_string,
3476 "connect_topology_option_modifier",
3479 GNUNET_free (connect_topology_option_modifier_string);
3482 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
3483 &blacklist_transports))
3484 blacklist_transports = NULL;
3487 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
3488 &blacklist_topology_str)) &&
3489 (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
3491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3492 "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
3494 GNUNET_free_non_null(topology_str);
3495 GNUNET_free_non_null(blacklist_topology_str);
3497 /* Set peers_left so we know when all peers started */
3498 peers_left = num_peers;
3501 /* Set up a task to end testing if peer start fails */
3502 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
3503 &end_badly, "didn't generate all hostkeys within allowed startup time!");
3505 if (dhtlog_handle == NULL)
3506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3507 "dhtlog_handle is NULL!");
3509 trial_info.other_identifier = (unsigned int)trial_to_run;
3510 trial_info.num_nodes = peers_left;
3511 trial_info.topology = topology;
3512 trial_info.blacklist_topology = blacklist_topology;
3513 trial_info.connect_topology = connect_topology;
3514 trial_info.connect_topology_option = connect_topology_option;
3515 trial_info.connect_topology_option_modifier = connect_topology_option_modifier;
3516 trial_info.topology_percentage = topology_percentage;
3517 trial_info.topology_probability = topology_probability;
3518 trial_info.puts = num_puts;
3519 trial_info.gets = num_gets;
3520 trial_info.concurrent = max_outstanding_gets;
3521 trial_info.settle_time = settle_time;
3522 trial_info.num_rounds = total_rounds;
3523 trial_info.malicious_getters = malicious_getters;
3524 trial_info.malicious_putters = malicious_putters;
3525 trial_info.malicious_droppers = malicious_droppers;
3526 trial_info.malicious_get_frequency = malicious_get_frequency.rel_value;
3527 trial_info.malicious_put_frequency = malicious_put_frequency.rel_value;
3528 trial_info.stop_closest = stop_closest;
3529 trial_info.stop_found = stop_found;
3530 trial_info.strict_kademlia = strict_kademlia;
3532 if (trialmessage != NULL)
3533 trial_info.message = trialmessage;
3535 trial_info.message = "";
3537 if (dhtlog_handle != NULL)
3538 dhtlog_handle->insert_trial(&trial_info);
3540 GNUNET_free_non_null(trialmessage);
3542 hostkey_meter = create_meter(peers_left, "Hostkeys created ", GNUNET_YES);
3543 peer_start_meter = create_meter(peers_left, "Peers started ", GNUNET_YES);
3545 put_meter = create_meter(num_puts, "Puts completed ", GNUNET_YES);
3546 get_meter = create_meter(num_gets, "Gets completed ", GNUNET_YES);
3547 pg = GNUNET_TESTING_daemons_start (cfg,
3549 max_outstanding_connections,
3551 GNUNET_TIME_relative_multiply(seconds_per_peer_start, num_peers),
3552 &hostkey_callback, NULL,
3553 &peers_started_callback, NULL,
3554 &topology_callback, NULL,
3557 while (temphost != NULL)
3559 tempnext = temphost->next;
3560 GNUNET_free (temphost->username);
3561 GNUNET_free (temphost->hostname);
3562 GNUNET_free (temphost);
3563 temphost = tempnext;
3568 main (int argc, char *argv[])
3571 struct GNUNET_GETOPT_CommandLineOption options[] = {
3572 GNUNET_GETOPT_OPTION_END
3575 ret = GNUNET_PROGRAM_run (argc,
3576 argv, "gnunet-dht-driver", "nohelp",
3577 options, &run, &ok);
3579 if (malicious_bloom != NULL)
3580 GNUNET_CONTAINER_bloomfilter_free (malicious_bloom);
3582 if (ret != GNUNET_OK)
3584 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`gnunet-dht-driver': Failed with error code %d\n", ret);
3588 * Need to remove base directory, subdirectories taken care
3589 * of by the testing framework.
3591 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
3593 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
3598 /* end of gnunet-dht-driver.c */