2 This file is part of GNUnet
3 (C) 2008--2013 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.
22 * @file testbed/testbed_api_testbed.c
23 * @brief high-level testbed management
24 * @author Christian Grothoff
25 * @author Sree Harsha Totakura
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api.h"
32 #include "testbed_api_peers.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_api_topology.h"
37 * Generic loggins shorthand
39 #define LOG(kind,...) \
40 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
43 * Debug logging shortcut
46 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49 * The default setup timeout in seconds
51 #define DEFAULT_SETUP_TIMEOUT 300
55 * Configuration section for testbed
57 #define TESTBED_CONFIG_SECTION "testbed"
60 * Option string for the maximum number of edges a peer is permitted to have
61 * while generating scale free topology
63 #define SCALE_FREE_CAP "SCALE_FREE_TOPOLOGY_CAP"
66 * Option string for the number of edges to be established when adding a new
67 * node to the scale free network
69 #define SCALE_FREE_M "SCALE_FREE_TOPOLOGY_M"
72 * Context information for the operation we start
74 struct RunContextOperation
77 * The testbed operation handle
79 struct GNUNET_TESTBED_Operation *op;
82 * Context information for GNUNET_TESTBED_run()
84 struct GNUNET_TESTBED_RunHandle *rc;
95 * States of RunContext
105 * Controllers on given hosts started and linked
115 * The testbed run is ready and the master callback can be called now. At this
116 * time the peers are all started and if a topology is provided in the
117 * configuration the topology would have been attempted
122 /* * Peers are stopped */
124 /* RC_PEERS_STOPPED, */
127 /* * Peers are destroyed */
129 /* RC_PEERS_DESTROYED */
132 * All peers shutdown (stopped and destroyed)
139 * Context for host compability checks
141 struct CompatibilityCheckContext
146 struct GNUNET_TESTBED_RunHandle *rc;
149 * Handle for the compability check
151 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
154 * Index of the host in the run context's hosts array
163 struct GNUNET_TESTBED_RunHandle
166 * The controller handle
168 struct GNUNET_TESTBED_Controller *c;
171 * The configuration of the controller. This is based on the cfg given to the
172 * function GNUNET_TESTBED_run(). We also use this config as a template while
175 struct GNUNET_CONFIGURATION_Handle *cfg;
178 * Handle to the host on which the controller runs
180 struct GNUNET_TESTBED_Host *h;
183 * The handle to the controller process
185 struct GNUNET_TESTBED_ControllerProc *cproc;
188 * The callback to use as controller callback
190 GNUNET_TESTBED_ControllerCallback cc;
193 * The pointer to the controller callback
198 * The trusted IP string
203 * TestMaster callback to call when testbed initialization is done
205 GNUNET_TESTBED_TestMaster test_master;
208 * The closure for the TestMaster callback
210 void *test_master_cls;
213 * A hashmap for operations started by us
215 struct GNUNET_CONTAINER_MultiHashMap32 *rcop_map;
218 * An array of hosts loaded from the hostkeys file
220 struct GNUNET_TESTBED_Host **hosts;
223 * Array of compatibility check contexts
225 struct CompatibilityCheckContext *hclist;
228 * Array of peers which we create
230 struct GNUNET_TESTBED_Peer **peers;
233 * The topology generation operation. Will be null if no topology is set in
236 struct GNUNET_TESTBED_Operation *topology_operation;
239 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
244 * Host registration handle
246 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
249 * Profiling start time
251 struct GNUNET_TIME_Absolute pstart_time;
254 * Host registration task
256 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
259 * Task to be run of a timeout
261 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
264 * Task run upon shutdown interrupts
266 GNUNET_SCHEDULER_TaskIdentifier interrupt_task;
269 * The event mask for the controller
274 * State of this context
279 * The topology which has to be achieved with the peers started in this context
281 enum GNUNET_TESTBED_TopologyOption topology;
284 * Have we already shutdown
289 * Number of hosts in the given host file
291 unsigned int num_hosts;
294 * Number of registered hosts. Also used as a counter while checking
295 * habitabillity of hosts
297 unsigned int reg_hosts;
300 * Current peer count for an operation; Set this to 0 and increment for each
301 * successful operation on a peer
303 unsigned int peer_count;
306 * number of peers to start
308 unsigned int num_peers;
311 * Expected overlay connects. Should be zero if no topology is relavant
316 * Number of random links to established
318 unsigned int random_links;
321 * the number of overlay link connection attempts that succeeded
323 unsigned int links_succeeded;
326 * the number of overlay link connection attempts that failed
328 unsigned int links_failed;
334 * Return a 32-bit key from a pointer
336 * @param rcop the pointer
340 rcop_key (void *rcop)
342 return * ((uint32_t *) &rcop);
347 * Context information used for finding a pointer in the rcop_map
352 * The operation pointer to look for
354 struct GNUNET_TESTBED_Operation *query;
357 * The Run context operation which has the operation being queried
359 struct RunContextOperation *result;
364 * Iterator for searching over the elements matching a given query
366 * @param cls the SearchContext
367 * @param key the 32-bit key
368 * @param value the RunContextOperation element
369 * @return GNUNET_YES to continue iteration; GNUNET_NO to cancel it
372 search_iterator (void *cls, uint32_t key, void *value)
374 struct RunContextOperation *rcop = value;
375 struct SearchContext *sc = cls;
377 GNUNET_assert (NULL != rcop);
378 if (sc->query == rcop->op)
380 GNUNET_assert (NULL == sc->result);
389 * Initiate a search for the given operation in the rcop_map
391 * @param rc the RunContext whose rcop_map will be searched for the given
393 * @param op the given operation to search for
394 * @return the matching RunContextOperation if found; NULL if not
396 static struct RunContextOperation *
397 search_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct GNUNET_TESTBED_Operation *op)
399 struct SearchContext sc;
404 GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
409 GNUNET_assert (NULL != sc.result);
417 * Insert an RunContextOperation into the rcop_map of the given RunContext
419 * @param rc the RunContext into whose map is to be used for insertion
420 * @param rcop the RunContextOperation to insert
423 insert_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct RunContextOperation *rcop)
425 GNUNET_assert (GNUNET_OK ==
426 GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
427 rcop_key (rcop->op), rcop,
428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
433 * Remove a RunContextOperation from the rcop_map of the given RunContext
435 * @param rc the RunContext from whose map the given RunContextOperaton has to
437 * @param rcop the RunContextOperation
440 remove_rcop (struct GNUNET_TESTBED_RunHandle *rc, struct RunContextOperation *rcop)
442 GNUNET_assert (GNUNET_YES ==
443 GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
449 * Assuming all peers have been destroyed cleanup run handle
451 * @param rc the run context
454 cleanup (struct GNUNET_TESTBED_RunHandle *rc)
458 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
459 GNUNET_assert (NULL == rc->reg_handle);
460 GNUNET_assert (NULL == rc->peers);
461 GNUNET_assert (NULL == rc->hclist);
462 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
463 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
464 GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
466 GNUNET_TESTBED_controller_disconnect (rc->c);
467 if (NULL != rc->cproc)
468 GNUNET_TESTBED_controller_stop (rc->cproc);
470 GNUNET_TESTBED_host_destroy (rc->h);
471 for (hid = 0; hid < rc->num_hosts; hid++)
472 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
473 GNUNET_free_non_null (rc->hosts);
475 GNUNET_CONFIGURATION_destroy (rc->cfg);
476 GNUNET_free_non_null (rc->topo_file);
477 GNUNET_free_non_null (rc->trusted_ip);
483 * Iterator for cleaning up elements from rcop_map
485 * @param cls the RunContext
486 * @param key the 32-bit key
487 * @param value the RunContextOperation element
488 * @return always GNUNET_YES
491 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
493 struct GNUNET_TESTBED_RunHandle *rc = cls;
494 struct RunContextOperation *rcop = value;
496 GNUNET_assert (rc == rcop->rc);
497 remove_rcop (rc, rcop);
498 GNUNET_TESTBED_operation_done (rcop->op);
505 * Cancels operations and tasks which are assigned to the given run context
507 * @param rc the RunContext
510 rc_cleanup_operations (struct GNUNET_TESTBED_RunHandle *rc)
512 struct CompatibilityCheckContext *hc;
515 if (NULL != rc->hclist)
517 for (nhost = 0; nhost < rc->num_hosts; nhost++)
519 hc = &rc->hclist[nhost];
521 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
523 GNUNET_free (rc->hclist);
526 /* Stop register hosts task if it is running */
527 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
529 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
530 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
532 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
534 GNUNET_SCHEDULER_cancel (rc->timeout_task);
535 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
537 if (NULL != rc->reg_handle)
539 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
540 rc->reg_handle = NULL;
542 if (NULL != rc->topology_operation)
544 GNUNET_TESTBED_operation_done (rc->topology_operation);
545 rc->topology_operation = NULL;
547 /* cancel any exiting operations */
548 GNUNET_assert (GNUNET_SYSERR !=
549 GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
550 &rcop_cleanup_iterator,
556 * Cancels the scheduled interrupt task
558 * @param rc the run context
561 cancel_interrupt_task (struct GNUNET_TESTBED_RunHandle *rc)
563 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
564 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
569 * This callback will be called when all the operations are completed
572 * @param cls run context
575 wait_op_completion (void *cls)
577 struct GNUNET_TESTBED_RunHandle *rc = cls;
578 struct RunContextOperation *rcop;
580 if ( (NULL == rc->cproc)
582 || (GNUNET_YES == rc->shutdown) )
584 if (NULL != rc->peers)
586 GNUNET_free (rc->peers);
591 if (NULL == rc->peers)
593 rc->shutdown = GNUNET_YES;
594 rcop = GNUNET_new (struct RunContextOperation);
596 rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
597 GNUNET_assert (NULL != rcop->op);
598 DEBUG ("Shutting down peers\n");
599 rc->pstart_time = GNUNET_TIME_absolute_get ();
600 insert_rcop (rc, rcop);
604 rc->state = RC_PEERS_SHUTDOWN;
605 cancel_interrupt_task (rc);
611 * Task run upon interrupts (SIGINT, SIGTERM) and upon scheduler shutdown.
613 * @param cls the RunContext which has to be acted upon
614 * @param tc the scheduler task context
617 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
619 struct GNUNET_TESTBED_RunHandle *rc = cls;
620 struct GNUNET_TESTBED_Controller *c = rc->c;
624 rc->interrupt_task = GNUNET_SCHEDULER_add_delayed
625 (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt, rc);
626 rc_cleanup_operations (rc);
627 if ( (GNUNET_NO == rc->shutdown)
629 && (0 != (size = GNUNET_CONTAINER_multihashmap32_size (c->opc_map))))
631 LOG (GNUNET_ERROR_TYPE_WARNING, "Shutdown postponed as there are "
632 "%u operations currently active\n", size);
633 c->opcq_empty_cb = &wait_op_completion;
634 c->opcq_empty_cls = rc;
637 wait_op_completion (rc);
642 * Function to return the string representation of the duration between current
643 * time and `pstart_time' in `RunContext'
645 * @param rc the RunContext
646 * @return the representation string; this is NOT reentrant
649 prof_time (struct GNUNET_TESTBED_RunHandle *rc)
651 struct GNUNET_TIME_Relative ptime;
653 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
654 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
659 * Task for starting peers
661 * @param cls the RunHandle
662 * @param tc the task context from scheduler
665 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
667 struct GNUNET_TESTBED_RunHandle *rc = cls;
668 struct RunContextOperation *rcop;
671 DEBUG ("Starting Peers\n");
672 rc->pstart_time = GNUNET_TIME_absolute_get ();
673 for (peer = 0; peer < rc->num_peers; peer++)
675 rcop = GNUNET_new (struct RunContextOperation);
677 rcop->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
678 GNUNET_assert (NULL != rcop->op);
679 rcop->cls = rc->peers[peer];
680 insert_rcop (rc, rcop);
687 * Functions of this signature are called when a peer has been successfully
690 * @param cls the closure from GNUNET_TESTBED_peer_create()
691 * @param peer the handle for the created peer; NULL on any error during
693 * @param emsg NULL if peer is not NULL; else MAY contain the error description
696 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
698 struct RunContextOperation *rcop = cls;
699 struct GNUNET_TESTBED_RunHandle *rc;
701 GNUNET_assert (NULL != rcop);
702 GNUNET_assert (NULL != (rc = rcop->rc));
703 remove_rcop (rc, rcop);
704 GNUNET_TESTBED_operation_done (rcop->op);
709 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
711 GNUNET_SCHEDULER_shutdown ();
714 rc->peers[rc->peer_count] = peer;
716 if (rc->peer_count < rc->num_peers)
718 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
719 rc->state = RC_PEERS_CREATED;
720 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
725 * call test master callback
727 * @param rc the RunContext
730 call_master (struct GNUNET_TESTBED_RunHandle *rc)
732 GNUNET_SCHEDULER_cancel (rc->timeout_task);
733 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
734 if (NULL != rc->test_master)
735 rc->test_master (rc->test_master_cls, rc, rc->num_peers, rc->peers,
736 rc->links_succeeded, rc->links_failed);
741 * Callbacks of this type are called when topology configuration is completed
743 * @param cls the operation closure given to
744 * GNUNET_TESTBED_overlay_configure_topology_va() and
745 * GNUNET_TESTBED_overlay_configure() calls
746 * @param nsuccess the number of successful overlay connects
747 * @param nfailures the number of overlay connects which failed
750 topology_completion_callback (void *cls, unsigned int nsuccess,
751 unsigned int nfailures)
753 struct GNUNET_TESTBED_RunHandle *rc = cls;
755 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
756 GNUNET_TESTBED_operation_done (rc->topology_operation);
757 rc->topology_operation = NULL;
758 rc->links_succeeded = nsuccess;
759 rc->links_failed = nfailures;
760 rc->state = RC_READY;
766 * Function to create peers
768 * @param rc the RunContext
771 create_peers (struct GNUNET_TESTBED_RunHandle *rc)
773 struct RunContextOperation *rcop;
776 DEBUG ("Creating peers\n");
777 rc->pstart_time = GNUNET_TIME_absolute_get ();
779 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
780 GNUNET_assert (NULL != rc->c);
782 for (peer = 0; peer < rc->num_peers; peer++)
784 rcop = GNUNET_new (struct RunContextOperation);
787 GNUNET_TESTBED_peer_create (rc->c,
789 rc->num_hosts) ? rc->h : rc->hosts[peer %
791 rc->cfg, &peer_create_cb, rcop);
792 GNUNET_assert (NULL != rcop->op);
793 insert_rcop (rc, rcop);
799 * Signature of the event handler function called by the
800 * respective event controller.
803 * @param event information about the event
806 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
808 struct GNUNET_TESTBED_RunHandle *rc = cls;
809 struct RunContextOperation *rcop;
811 if (RC_INIT == rc->state)
815 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
816 rcop = event->op_cls;
817 if (NULL != event->details.operation_finished.emsg)
819 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
820 GNUNET_SCHEDULER_shutdown ();
824 GNUNET_assert (event->op == rcop->op);
825 remove_rcop (rc, rcop);
826 GNUNET_TESTBED_operation_done (rcop->op);
828 if (rc->reg_hosts == rc->num_hosts)
830 rc->state = RC_LINKED;
836 GNUNET_SCHEDULER_shutdown ();
840 if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
842 if (NULL == (rcop = search_rcop (rc, event->op)))
844 remove_rcop (rc, rcop);
845 GNUNET_TESTBED_operation_done (rcop->op);
847 if ( (GNUNET_NO == rc->shutdown)
848 && (NULL != event->details.operation_finished.emsg) )
850 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
851 event->details.operation_finished.emsg);
852 GNUNET_SCHEDULER_shutdown ();
855 GNUNET_assert (GNUNET_YES == rc->shutdown);
859 case RC_PEERS_CREATED:
861 rc->state = RC_PEERS_SHUTDOWN;
862 GNUNET_free_non_null (rc->peers);
864 DEBUG ("Peers shut down in %s\n", prof_time (rc));
865 GNUNET_SCHEDULER_shutdown ();
873 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
874 rc->cc (rc->cc_cls, event);
875 if (GNUNET_TESTBED_ET_PEER_START != event->type)
877 if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
879 remove_rcop (rc, rcop);
880 GNUNET_TESTBED_operation_done (rcop->op);
883 if (rc->peer_count < rc->num_peers)
885 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
886 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
888 switch (rc->topology)
890 case GNUNET_TESTBED_TOPOLOGY_NONE:
892 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
893 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
894 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
895 rc->topology_operation =
896 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
897 rc->peers, &rc->num_oc,
898 &topology_completion_callback,
902 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
904 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
905 GNUNET_assert (NULL != rc->topo_file);
906 rc->topology_operation =
907 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
908 rc->peers, &rc->num_oc,
909 &topology_completion_callback,
913 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
915 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
917 unsigned long long number;
919 GNUNET_assert (GNUNET_OK ==
920 GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
923 cap = (unsigned int) number;
924 GNUNET_assert (GNUNET_OK ==
925 GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
928 rc->topology_operation =
929 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
930 rc->peers, &rc->num_oc,
931 &topology_completion_callback,
935 (unsigned int) number, /* uint8_t */
936 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
940 rc->topology_operation =
941 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
942 rc->peers, &rc->num_oc,
943 &topology_completion_callback,
946 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
948 if (NULL == rc->topology_operation)
949 LOG (GNUNET_ERROR_TYPE_WARNING,
950 "Not generating a topology. Check number of peers\n");
953 DEBUG ("Creating overlay topology\n");
954 rc->pstart_time = GNUNET_TIME_absolute_get ();
958 rc->state = RC_READY;
964 * Task to register all hosts available in the global host list
966 * @param cls the RunContext
967 * @param tc the scheduler task context
970 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
974 * Callback which will be called to after a host registration succeeded or failed
976 * @param cls the closure
977 * @param emsg the error message; NULL if host registration is successful
980 host_registration_completion (void *cls, const char *emsg)
982 struct GNUNET_TESTBED_RunHandle *rc = cls;
984 rc->reg_handle = NULL;
987 LOG (GNUNET_ERROR_TYPE_WARNING,
988 _("Host registration failed for a host. Error: %s\n"), emsg);
989 GNUNET_SCHEDULER_shutdown ();
992 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
997 * Task to register all hosts available in the global host list
999 * @param cls RunContext
1000 * @param tc the scheduler task context
1003 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1005 struct GNUNET_TESTBED_RunHandle *rc = cls;
1006 struct RunContextOperation *rcop;
1009 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1010 if (rc->reg_hosts == rc->num_hosts)
1012 DEBUG ("All hosts successfully registered\n");
1014 for (slave = 0; slave < rc->num_hosts; slave++)
1016 rcop = GNUNET_new (struct RunContextOperation);
1019 GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[slave],
1021 GNUNET_assert (NULL != rcop->op);
1022 insert_rcop (rc, rcop);
1028 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
1029 host_registration_completion, rc);
1035 * Callback to signal successfull startup of the controller process
1037 * @param cls the closure from GNUNET_TESTBED_controller_start()
1038 * @param cfg the configuration with which the controller has been started;
1039 * NULL if status is not GNUNET_OK
1040 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1041 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1044 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1047 struct GNUNET_TESTBED_RunHandle *rc = cls;
1048 uint64_t event_mask;
1050 if (status != GNUNET_OK)
1053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1054 _("Controller crash detected. Shutting down.\n"));
1055 GNUNET_SCHEDULER_shutdown ();
1058 GNUNET_CONFIGURATION_destroy (rc->cfg);
1059 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1060 event_mask = rc->event_mask;
1061 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1062 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1063 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
1064 event_mask |= GNUNET_TESTBED_ET_CONNECT;
1066 GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
1067 if (0 < rc->num_hosts)
1070 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
1073 rc->state = RC_LINKED;
1079 * Callback function invoked for each interface found.
1081 * @param cls closure
1082 * @param name name of the interface (can be NULL for unknown)
1083 * @param isDefault is this presumably the default interface
1084 * @param addr address of this interface (can be NULL for unknown or unassigned)
1085 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
1086 * @param netmask the network mask (can be NULL for unknown or unassigned))
1087 * @param addrlen length of the address
1088 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1091 netint_proc (void *cls, const char *name, int isDefault,
1092 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
1093 const struct sockaddr *netmask, socklen_t addrlen)
1095 struct GNUNET_TESTBED_RunHandle *rc = cls;
1096 char hostip[NI_MAXHOST];
1099 if (sizeof (struct sockaddr_in) != addrlen)
1100 return GNUNET_OK; /* Only consider IPv4 for now */
1102 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
1103 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
1104 if (NULL == rc->trusted_ip)
1106 rc->trusted_ip = GNUNET_strdup (hostip);
1109 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
1110 GNUNET_free (rc->trusted_ip);
1111 rc->trusted_ip = buf;
1117 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1118 * inform whether the given host is habitable or not. The Handle returned by
1119 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1122 * @param host the host whose status is being reported; will be NULL if the host
1123 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1124 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1127 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
1130 struct CompatibilityCheckContext *hc = cls;
1131 struct GNUNET_TESTBED_RunHandle *rc;
1132 struct GNUNET_TESTBED_Host **old_hosts;
1135 GNUNET_assert (NULL != (rc = hc->rc));
1137 GNUNET_assert (nhost <= rc->num_hosts);
1138 GNUNET_assert (host == rc->hosts[nhost]);
1140 if (GNUNET_NO == status)
1142 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1143 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1144 GNUNET_TESTBED_host_get_hostname (host));
1146 LOG (GNUNET_ERROR_TYPE_ERROR,
1147 _("Testbed cannot be started on localhost\n"));
1148 GNUNET_SCHEDULER_shutdown ();
1152 if (rc->reg_hosts < rc->num_hosts)
1154 GNUNET_free (rc->hclist);
1156 rc->h = rc->hosts[0];
1158 if (0 < rc->num_hosts)
1160 old_hosts = rc->hosts;
1162 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1163 memcpy (rc->hosts, &old_hosts[1],
1164 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1165 GNUNET_free (old_hosts);
1169 GNUNET_free (rc->hosts);
1172 GNUNET_TESTBED_host_resolve_ (rc->h);
1173 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1174 GNUNET_TESTBED_host_resolve_ (rc->hosts[nhost]);
1175 GNUNET_OS_network_interfaces_list (netint_proc, rc);
1176 if (NULL == rc->trusted_ip)
1177 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1179 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1180 &controller_status_cb, rc);
1181 GNUNET_free (rc->trusted_ip);
1182 rc->trusted_ip = NULL;
1183 if (NULL == rc->cproc)
1185 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1186 GNUNET_SCHEDULER_shutdown ();
1192 * Task run upon timeout while setting up the testbed
1194 * @param cls the RunContext
1195 * @param tc the task context
1198 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1200 struct GNUNET_TESTBED_RunHandle *rc = cls;
1202 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1203 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1204 GNUNET_SCHEDULER_shutdown ();
1205 if (NULL != rc->test_master)
1206 rc->test_master (rc->test_master_cls, rc, 0, NULL, 0, 0);
1207 rc->test_master = NULL;
1212 * Convenience method for running a testbed with
1213 * a single call. Underlay and overlay topology
1214 * are configured using the "UNDERLAY" and "OVERLAY"
1215 * options in the "[testbed]" section of the configuration\
1216 * (with possible options given in "UNDERLAY_XXX" and/or
1219 * The testbed is to be terminated using a call to
1220 * "GNUNET_SCHEDULER_shutdown".
1222 * @param host_filename name of the file with the 'hosts', NULL
1223 * to run everything on 'localhost'
1224 * @param cfg configuration to use (for testbed, controller and peers)
1225 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1226 * @param event_mask bit mask with set of events to call 'cc' for;
1227 * or-ed values of "1LL" shifted by the
1228 * respective 'enum GNUNET_TESTBED_EventType'
1229 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1230 * @param cc controller callback to invoke on events; This callback is called
1231 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1232 * set in the event_mask as this is the only way get access to the
1233 * handle of each peer
1234 * @param cc_cls closure for cc
1235 * @param test_master this callback will be called once the test is ready
1236 * @param test_master_cls closure for 'test_master'.
1239 GNUNET_TESTBED_run (const char *host_filename,
1240 const struct GNUNET_CONFIGURATION_Handle *cfg,
1241 unsigned int num_peers, uint64_t event_mask,
1242 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1243 GNUNET_TESTBED_TestMaster test_master,
1244 void *test_master_cls)
1246 struct GNUNET_TESTBED_RunHandle *rc;
1248 struct CompatibilityCheckContext *hc;
1249 struct GNUNET_TIME_Relative timeout;
1250 unsigned long long number;
1254 GNUNET_assert (num_peers > 0);
1255 rc = GNUNET_new (struct GNUNET_TESTBED_RunHandle);
1256 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1258 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1260 if (0 == rc->num_hosts)
1262 LOG (GNUNET_ERROR_TYPE_WARNING,
1263 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1267 if (NULL != host_filename)
1270 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1272 if (0 == rc->num_hosts)
1274 LOG (GNUNET_ERROR_TYPE_WARNING,
1275 _("No hosts loaded. Need at least one host\n"));
1280 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1282 rc->num_peers = num_peers;
1283 rc->event_mask = event_mask;
1285 rc->cc_cls = cc_cls;
1286 rc->test_master = test_master;
1287 rc->test_master_cls = test_master_cls;
1288 rc->state = RC_INIT;
1289 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1291 GNUNET_CONFIGURATION_get_value_string (rc->cfg, TESTBED_CONFIG_SECTION,
1292 "OVERLAY_TOPOLOGY", &topology))
1294 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1296 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1299 ("Specified topology must be supported by testbed"));
1301 GNUNET_free (topology);
1303 switch (rc->topology)
1305 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1306 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1307 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1309 GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
1310 "OVERLAY_RANDOM_LINKS",
1313 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1314 * option to be set to the number of random links to be established */
1315 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1316 "OVERLAY_RANDOM_LINKS");
1319 if (number > UINT32_MAX)
1321 GNUNET_break (0); /* Too big number */
1324 rc->random_links = (unsigned int) number;
1326 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1328 GNUNET_CONFIGURATION_get_value_filename (rc->cfg, TESTBED_CONFIG_SECTION,
1329 "OVERLAY_TOPOLOGY_FILE",
1332 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1333 "OVERLAY_TOPOLOGY_FILE");
1337 case GNUNET_TESTBED_TOPOLOGY_SCALE_FREE:
1339 GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
1340 SCALE_FREE_CAP, &number))
1342 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1346 if (UINT16_MAX < number)
1348 LOG (GNUNET_ERROR_TYPE_ERROR,
1349 _("Maximum number of edges a peer can have in a scale free topology"
1350 " cannot be more than %u. Given `%s = %llu'"), UINT16_MAX,
1351 SCALE_FREE_CAP, number);
1355 GNUNET_CONFIGURATION_get_value_number (rc->cfg, TESTBED_CONFIG_SECTION,
1356 SCALE_FREE_M, &number))
1358 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, TESTBED_CONFIG_SECTION,
1362 if (UINT8_MAX < number)
1364 LOG (GNUNET_ERROR_TYPE_ERROR,
1365 _("The number of edges that can established when adding a new node"
1366 " to scale free topology cannot be more than %u. Given `%s = %llu'"),
1367 UINT8_MAX, SCALE_FREE_M, number);
1373 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1375 GNUNET_CONFIGURATION_have_value (rc->cfg, TESTBED_CONFIG_SECTION,
1376 "OVERLAY_RANDOM_LINKS"))
1377 LOG (GNUNET_ERROR_TYPE_WARNING,
1378 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1381 if (0 != rc->num_hosts)
1383 rc->hclist = GNUNET_malloc (sizeof (struct CompatibilityCheckContext)
1385 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1387 hc = &rc->hclist[nhost];
1390 hc->h = GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1391 &host_habitable_cb, hc);
1395 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1397 hc = &rc->hclist[nhost];
1399 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
1401 GNUNET_free (rc->hclist);
1409 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1410 &controller_status_cb, rc);
1411 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, TESTBED_CONFIG_SECTION,
1415 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1416 DEFAULT_SETUP_TIMEOUT);
1418 rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1420 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1421 rc->interrupt_task =
1422 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1428 GNUNET_TESTBED_host_destroy (rc->h);
1429 if (NULL != rc->hosts)
1431 for (hid = 0; hid < rc->num_hosts; hid++)
1432 if (NULL != rc->hosts[hid])
1433 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1434 GNUNET_free (rc->hosts);
1436 if (NULL != rc->cfg)
1437 GNUNET_CONFIGURATION_destroy (rc->cfg);
1443 * Obtain handle to the master controller from a testbed run. The handle
1444 * returned should not be disconnected.
1446 * @param h the testbed run handle
1447 * @return handle to the master controller
1449 struct GNUNET_TESTBED_Controller *
1450 GNUNET_TESTBED_run_get_controller_handle (struct GNUNET_TESTBED_RunHandle *h)
1456 /* end of testbed_api_testbed.c */