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_peers.h"
32 #include "testbed_api_hosts.h"
33 #include "testbed_api_topology.h"
36 * Generic loggins shorthand
38 #define LOG(kind,...) \
39 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
42 * Debug logging shortcut
45 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48 * The default setup timeout in seconds
50 #define DEFAULT_SETUP_TIMEOUT 300
58 * Context information for the operation we start
60 struct RunContextOperation
63 * The testbed operation handle
65 struct GNUNET_TESTBED_Operation *op;
68 * Context information for GNUNET_TESTBED_run()
70 struct RunContext *rc;
81 * States of RunContext
92 * Island level controllers are started and linked
98 * Controllers on given hosts started and linked
108 * The testbed run is ready and the master callback can be called now. At this
109 * time the peers are all started and if a topology is provided in the
110 * configuration the topology would have been attempted
115 /* * Peers are stopped */
117 /* RC_PEERS_STOPPED, */
120 /* * Peers are destroyed */
122 /* RC_PEERS_DESTROYED */
125 * All peers shutdown (stopped and destroyed)
132 * Context for host compability checks
134 struct CompatibilityCheckContext
139 struct RunContext *rc;
142 * Handle for the compability check
144 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
147 * Index of the host in the run context's hosts array
154 * Structure to represent an island of SuperMUC's nodes
159 * Array of nodes in this island
161 struct GNUNET_TESTBED_Host **hosts;
164 * Number of nodes in the above array
176 * The controller handle
178 struct GNUNET_TESTBED_Controller *c;
181 * The configuration of the controller. This is based on the cfg given to the
182 * function GNUNET_TESTBED_run(). We also use this config as a template while
185 struct GNUNET_CONFIGURATION_Handle *cfg;
188 * Handle to the host on which the controller runs
190 struct GNUNET_TESTBED_Host *h;
193 * The handle to the controller process
195 struct GNUNET_TESTBED_ControllerProc *cproc;
198 * The callback to use as controller callback
200 GNUNET_TESTBED_ControllerCallback cc;
203 * The pointer to the controller callback
208 * The trusted IP string
213 * TestMaster callback to call when testbed initialization is done
215 GNUNET_TESTBED_TestMaster test_master;
218 * The closure for the TestMaster callback
220 void *test_master_cls;
223 * A hashmap for operations started by us
225 struct GNUNET_CONTAINER_MultiHashMap32 *rcop_map;
228 * An array of hosts loaded from the hostkeys file
230 struct GNUNET_TESTBED_Host **hosts;
234 * Array of SuperMUC islands
236 struct Island **islands;
240 * Array of compatibility check contexts
242 struct CompatibilityCheckContext *hclist;
245 * Array of peers which we create
247 struct GNUNET_TESTBED_Peer **peers;
250 * The topology generation operation. Will be null if no topology is set in
253 struct GNUNET_TESTBED_Operation *topology_operation;
256 * The file containing topology data. Only used if the topology is set to
262 * Host registration handle
264 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
267 * Profiling start time
269 struct GNUNET_TIME_Absolute pstart_time;
272 * Host registration task
274 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
277 * Task to be run while shutting down
279 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
282 * Task to be run of a timeout
284 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
287 * Task run upon shutdown interrupts
289 GNUNET_SCHEDULER_TaskIdentifier interrupt_task;
292 * The event mask for the controller
297 * State of this context
302 * The topology which has to be achieved with the peers started in this context
304 enum GNUNET_TESTBED_TopologyOption topology;
307 * Have we already shutdown
312 * Number of hosts in the given host file
314 unsigned int num_hosts;
317 * Number of registered hosts. Also used as a counter while checking
318 * habitabillity of hosts
320 unsigned int reg_hosts;
323 * Current peer count for an operation; Set this to 0 and increment for each
324 * successful operation on a peer
326 unsigned int peer_count;
329 * number of peers to start
331 unsigned int num_peers;
334 * Expected overlay connects. Should be zero if no topology is relavant
339 * Number of random links to established
341 unsigned int random_links;
344 * the number of overlay link connection attempts that succeeded
346 unsigned int links_succeeded;
349 * the number of overlay link connection attempts that failed
351 unsigned int links_failed;
355 * Number of SuperMUC islands we are running on
357 unsigned int nislands;
363 * Return a 32-bit key from a pointer
365 * @param rcop the pointer
369 rcop_key (void *rcop)
371 return * ((uint32_t *) &rcop);
376 * Context information used for finding a pointer in the rcop_map
381 * The operation pointer to look for
383 struct GNUNET_TESTBED_Operation *query;
386 * The Run context operation which has the operation being queried
388 struct RunContextOperation *result;
393 * Iterator for searching over the elements matching a given query
395 * @param cls the SearchContext
396 * @param key the 32-bit key
397 * @param value the RunContextOperation element
398 * @return GNUNET_YES to continue iteration; GNUNET_NO to cancel it
401 search_iterator (void *cls, uint32_t key, void *value)
403 struct RunContextOperation *rcop = value;
404 struct SearchContext *sc = cls;
406 GNUNET_assert (NULL != rcop);
407 if (sc->query == rcop->op)
409 GNUNET_assert (NULL == sc->result);
418 * Initiate a search for the given operation in the rcop_map
420 * @param rc the RunContext whose rcop_map will be searched for the given
422 * @param op the given operation to search for
423 * @return the matching RunContextOperation if found; NULL if not
425 static struct RunContextOperation *
426 search_rcop (struct RunContext *rc, struct GNUNET_TESTBED_Operation *op)
428 struct SearchContext sc;
433 GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
438 GNUNET_assert (NULL != sc.result);
446 * Insert an RunContextOperation into the rcop_map of the given RunContext
448 * @param rc the RunContext into whose map is to be used for insertion
449 * @param rcop the RunContextOperation to insert
452 insert_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
454 GNUNET_assert (GNUNET_OK ==
455 GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
456 rcop_key (rcop->op), rcop,
457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
462 * Remove a RunContextOperation from the rcop_map of the given RunContext
464 * @param rc the RunContext from whose map the given RunContextOperaton has to
466 * @param rcop the RunContextOperation
469 remove_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
471 GNUNET_assert (GNUNET_YES ==
472 GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
479 cleanup_islands (struct RunContext *rc)
481 struct Island *island;
484 GNUNET_assert (NULL != rc->islands);
485 for (cnt = 0; cnt < rc->nislands; cnt++)
487 island = rc->islands[cnt];
488 GNUNET_free (island->hosts);
489 GNUNET_free (island);
491 GNUNET_free (rc->islands);
497 * Assuming all peers have been destroyed cleanup run handle
499 * @param cls the run handle
500 * @param tc the task context from scheduler
503 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
505 struct RunContext *rc = cls;
508 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
509 GNUNET_assert (NULL == rc->reg_handle);
510 GNUNET_assert (NULL == rc->peers);
511 GNUNET_assert (NULL == rc->hclist);
512 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
513 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
514 GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
516 GNUNET_TESTBED_controller_disconnect (rc->c);
517 if (NULL != rc->cproc)
518 GNUNET_TESTBED_controller_stop (rc->cproc);
520 GNUNET_TESTBED_host_destroy (rc->h);
522 if (NULL != rc->islands)
523 cleanup_islands (rc);
525 for (cnt = 0; cnt < rc->num_hosts; cnt++)
526 GNUNET_TESTBED_host_destroy (rc->hosts[cnt]);
527 GNUNET_free_non_null (rc->hosts);
529 GNUNET_CONFIGURATION_destroy (rc->cfg);
530 GNUNET_free_non_null (rc->topo_file);
531 GNUNET_free_non_null (rc->trusted_ip);
537 * Iterator for cleaning up elements from rcop_map
539 * @param cls the RunContext
540 * @param key the 32-bit key
541 * @param value the RunContextOperation element
542 * @return always GNUNET_YES
545 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
547 struct RunContext *rc = cls;
548 struct RunContextOperation *rcop = value;
550 GNUNET_assert (rc == rcop->rc);
551 remove_rcop (rc, rcop);
552 GNUNET_TESTBED_operation_done (rcop->op);
559 * Frees memory, closes pending operations, cancels actives tasks of the given
562 * @param rc the RunContext
565 cleanup (struct RunContext *rc)
567 struct CompatibilityCheckContext *hc;
570 if (NULL != rc->hclist)
572 for (nhost = 0; nhost < rc->num_hosts; nhost++)
574 hc = &rc->hclist[nhost];
576 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
578 GNUNET_free (rc->hclist);
581 /* Stop register hosts task if it is running */
582 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
584 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
585 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
587 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
589 GNUNET_SCHEDULER_cancel (rc->timeout_task);
590 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
592 if (GNUNET_SCHEDULER_NO_TASK != rc->interrupt_task)
594 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
595 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
597 if (NULL != rc->reg_handle)
599 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
600 rc->reg_handle = NULL;
602 if (NULL != rc->topology_operation)
604 GNUNET_TESTBED_operation_done (rc->topology_operation);
605 rc->topology_operation = NULL;
607 /* cancel any exiting operations */
608 GNUNET_assert (GNUNET_SYSERR !=
609 GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
610 &rcop_cleanup_iterator,
616 * Stops the testbed run and releases any used resources
618 * @param cls the tesbed run handle
619 * @param tc the task context from scheduler
622 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
624 struct RunContext *rc = cls;
625 struct RunContextOperation *rcop;
627 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
628 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
629 GNUNET_assert (GNUNET_NO == rc->shutdown);
630 rc->shutdown = GNUNET_YES;
634 if (NULL != rc->peers)
636 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
638 rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
639 GNUNET_assert (NULL != rcop->op);
640 DEBUG ("Shutting down peers\n");
641 rc->pstart_time = GNUNET_TIME_absolute_get ();
642 insert_rcop (rc, rcop);
646 rc->state = RC_PEERS_SHUTDOWN; /* No peers are present so we consider the
647 * state where all peers are SHUTDOWN */
648 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
653 * Function to shutdown now
655 * @param rc the RunContext
658 shutdown_now (struct RunContext *rc)
660 if (GNUNET_YES == rc->shutdown)
662 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
663 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
664 GNUNET_SCHEDULER_shutdown (); /* Trigger shutdown in programs using this API */
665 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
670 * Task run upon any interrupt. Common ones are SIGINT & SIGTERM.
672 * @param cls the RunContext which has to be acted upon
673 * @param tc the scheduler task context
676 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
678 struct RunContext *rc = cls;
680 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
686 * Function to return the string representation of the duration between current
687 * time and `pstart_time' in `RunContext'
689 * @param rc the RunContext
690 * @return the representation string; this is NOT reentrant
693 prof_time (struct RunContext *rc)
695 struct GNUNET_TIME_Relative ptime;
697 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
698 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
703 * Task for starting peers
705 * @param cls the RunHandle
706 * @param tc the task context from scheduler
709 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
711 struct RunContext *rc = cls;
712 struct RunContextOperation *rcop;
715 DEBUG ("Starting Peers\n");
716 rc->pstart_time = GNUNET_TIME_absolute_get ();
717 for (peer = 0; peer < rc->num_peers; peer++)
719 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
721 rcop->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
722 GNUNET_assert (NULL != rcop->op);
723 rcop->cls = rc->peers[peer];
724 insert_rcop (rc, rcop);
731 * Functions of this signature are called when a peer has been successfully
734 * @param cls the closure from GNUNET_TESTBED_peer_create()
735 * @param peer the handle for the created peer; NULL on any error during
737 * @param emsg NULL if peer is not NULL; else MAY contain the error description
740 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
742 struct RunContextOperation *rcop = cls;
743 struct RunContext *rc;
745 GNUNET_assert (NULL != rcop);
746 GNUNET_assert (NULL != (rc = rcop->rc));
747 remove_rcop (rc, rcop);
748 GNUNET_TESTBED_operation_done (rcop->op);
753 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
758 rc->peers[rc->peer_count] = peer;
760 if (rc->peer_count < rc->num_peers)
762 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
763 rc->state = RC_PEERS_CREATED;
764 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
769 * call test master callback
771 * @param rc the RunContext
774 call_master (struct RunContext *rc)
776 GNUNET_SCHEDULER_cancel (rc->timeout_task);
777 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
778 if (NULL != rc->test_master)
779 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers,
780 rc->links_succeeded, rc->links_failed);
785 * Callbacks of this type are called when topology configuration is completed
787 * @param cls the operation closure given to
788 * GNUNET_TESTBED_overlay_configure_topology_va() and
789 * GNUNET_TESTBED_overlay_configure() calls
790 * @param nsuccess the number of successful overlay connects
791 * @param nfailures the number of overlay connects which failed
794 topology_completion_callback (void *cls, unsigned int nsuccess,
795 unsigned int nfailures)
797 struct RunContext *rc = cls;
799 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
800 GNUNET_TESTBED_operation_done (rc->topology_operation);
801 rc->topology_operation = NULL;
802 rc->links_succeeded = nsuccess;
803 rc->links_failed = nfailures;
804 rc->state = RC_READY;
810 * Function to create peers
812 * @param rc the RunContext
815 create_peers (struct RunContext *rc)
817 struct RunContextOperation *rcop;
818 struct GNUNET_TESTBED_Host *host;
821 struct Island *island;
829 DEBUG ("Creating peers\n");
830 rc->pstart_time = GNUNET_TIME_absolute_get ();
832 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
833 GNUNET_assert (NULL != rc->c);
835 for (peer = 0; peer < rc->num_peers; peer++)
837 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
840 if (0 != rc->nislands)
842 island = rc->islands[icnt];
843 if (hcnt == island->nhosts)
846 if (icnt == rc->nislands)
848 island = rc->islands[icnt];
851 GNUNET_assert (icnt < rc->nislands);
852 GNUNET_assert (hcnt < island->nhosts);
853 GNUNET_assert (NULL != island->hosts[hcnt]);
854 host = island->hosts[hcnt];
860 host = (0 == rc->num_hosts) ? rc->h : rc->hosts[peer % rc->num_hosts];
862 rcop->op = GNUNET_TESTBED_peer_create (rc->c, host, rc->cfg,
863 &peer_create_cb, rcop);
864 GNUNET_assert (NULL != rcop->op);
865 insert_rcop (rc, rcop);
871 * Signature of the event handler function called by the
872 * respective event controller.
875 * @param event information about the event
878 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
880 struct RunContext *rc = cls;
881 struct RunContextOperation *rcop;
883 if (RC_INIT == rc->state)
887 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
888 rcop = event->op_cls;
889 if (NULL != event->details.operation_finished.emsg)
891 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
896 GNUNET_assert (event->op == rcop->op);
897 remove_rcop (rc, rcop);
898 GNUNET_TESTBED_operation_done (rcop->op);
901 if (rc->reg_hosts != rc->num_hosts)
903 rc->state = RC_LINKED;
906 if (rc->reg_hosts != rc->nislands)
908 struct Island *island;
909 struct GNUNET_TESTBED_Host *host;
912 rc->state = RC_ISLANDS_LINKED;
914 for (cnt = 0; cnt < rc->nislands; cnt++)
916 island = rc->islands[cnt];
917 for (cnt2 = 1; cnt2 < island->nhosts; cnt2++)
919 host = island->hosts[cnt2];
920 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
923 GNUNET_TESTBED_controller_link (rcop, rc->c, host,
924 island->hosts[0], GNUNET_YES);
925 GNUNET_assert (NULL != rcop->op);
926 insert_rcop (rc, rcop);
930 if (0 != rc->reg_hosts)
932 rc->state = RC_LINKED;
943 if (RC_ISLANDS_LINKED == rc->state)
947 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
948 rcop = event->op_cls;
949 if (NULL != event->details.operation_finished.emsg)
951 LOG (GNUNET_ERROR_TYPE_ERROR,
952 "Linking 2nd level controllers failed. Exiting");
957 GNUNET_assert (event->op == rcop->op);
958 remove_rcop (rc, rcop);
959 GNUNET_TESTBED_operation_done (rcop->op);
961 if (0 != rc->reg_hosts)
963 rc->state = RC_LINKED;
973 if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
975 if (NULL == (rcop = search_rcop (rc, event->op)))
977 remove_rcop (rc, rcop);
978 GNUNET_TESTBED_operation_done (rcop->op);
980 if ( (GNUNET_NO == rc->shutdown)
981 && (NULL != event->details.operation_finished.emsg) )
983 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
984 event->details.operation_finished.emsg);
988 GNUNET_assert (GNUNET_YES == rc->shutdown);
992 case RC_PEERS_CREATED:
994 rc->state = RC_PEERS_SHUTDOWN;
995 GNUNET_free_non_null (rc->peers);
997 DEBUG ("Peers shut down in %s\n", prof_time (rc));
998 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
1006 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
1007 rc->cc (rc->cc_cls, event);
1008 if (GNUNET_TESTBED_ET_PEER_START != event->type)
1010 if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
1012 remove_rcop (rc, rcop);
1013 GNUNET_TESTBED_operation_done (rcop->op);
1016 if (rc->peer_count < rc->num_peers)
1018 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
1019 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
1021 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
1022 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
1023 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
1025 rc->topology_operation =
1026 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
1027 rc->peers, &rc->num_oc,
1028 &topology_completion_callback,
1032 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1034 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
1036 GNUNET_assert (NULL != rc->topo_file);
1037 rc->topology_operation =
1038 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
1039 rc->peers, &rc->num_oc,
1040 &topology_completion_callback,
1044 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1047 rc->topology_operation =
1048 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
1049 rc->peers, &rc->num_oc,
1050 &topology_completion_callback,
1053 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1054 if (NULL == rc->topology_operation)
1055 LOG (GNUNET_ERROR_TYPE_WARNING,
1056 "Not generating topology. Check number of peers\n");
1059 DEBUG ("Creating overlay topology\n");
1060 rc->pstart_time = GNUNET_TIME_absolute_get ();
1064 rc->state = RC_READY;
1070 * Task to register all hosts available in the global host list
1072 * @param cls the RunContext
1073 * @param tc the scheduler task context
1076 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1080 * Callback which will be called to after a host registration succeeded or failed
1082 * @param cls the closure
1083 * @param emsg the error message; NULL if host registration is successful
1086 host_registration_completion (void *cls, const char *emsg)
1088 struct RunContext *rc = cls;
1090 rc->reg_handle = NULL;
1093 LOG (GNUNET_ERROR_TYPE_WARNING,
1094 _("Host registration failed for a host. Error: %s\n"), emsg);
1098 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
1103 * Task to register all hosts available in the global host list
1105 * @param cls RunContext
1106 * @param tc the scheduler task context
1109 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1111 struct RunContext *rc = cls;
1112 struct RunContextOperation *rcop;
1115 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1116 if (rc->reg_hosts == rc->num_hosts)
1118 DEBUG ("All hosts successfully registered\n");
1121 for (cnt = 0; cnt < rc->num_hosts; cnt++)
1123 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
1126 GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[cnt],
1128 GNUNET_assert (NULL != rcop->op);
1129 insert_rcop (rc, rcop);
1132 struct Island *island;
1133 for (cnt = 0; cnt < rc->nislands; cnt++)
1135 island = rc->islands[cnt];
1136 GNUNET_assert (0 < island->nhosts);
1137 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
1140 GNUNET_TESTBED_controller_link (rcop, rc->c, island->hosts[0],
1142 GNUNET_assert (NULL != rcop->op);
1143 insert_rcop (rc, rcop);
1150 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
1151 host_registration_completion, rc);
1157 * Callback to signal successfull startup of the controller process
1159 * @param cls the closure from GNUNET_TESTBED_controller_start()
1160 * @param cfg the configuration with which the controller has been started;
1161 * NULL if status is not GNUNET_OK
1162 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1163 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1166 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1169 struct RunContext *rc = cls;
1170 uint64_t event_mask;
1172 if (status != GNUNET_OK)
1174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1175 _("Controller crash detected. Shutting down.\n"));
1178 if (NULL != rc->peers)
1180 GNUNET_free (rc->peers);
1183 if (GNUNET_YES == rc->shutdown)
1185 rc->state = RC_PEERS_SHUTDOWN;
1186 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
1192 GNUNET_CONFIGURATION_destroy (rc->cfg);
1193 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1194 event_mask = rc->event_mask;
1195 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1196 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1197 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
1198 event_mask |= GNUNET_TESTBED_ET_CONNECT;
1200 GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
1201 if (0 < rc->num_hosts)
1204 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
1207 rc->state = RC_LINKED;
1213 #define ISLANDNAME_SIZE 4
1215 parse_islands (struct RunContext *rc)
1217 char island_id[ISLANDNAME_SIZE];
1218 struct GNUNET_TESTBED_Host *host;
1219 struct Island *island;
1220 const char *hostname;
1223 DEBUG ("Parsing for islands\n");
1224 memset (island_id, 0, ISLANDNAME_SIZE);
1226 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1228 host = rc->hosts[nhost];
1229 hostname = GNUNET_TESTBED_host_get_hostname (host);
1230 GNUNET_assert (NULL != hostname);
1233 strncpy (island_id, hostname, ISLANDNAME_SIZE - 1);
1234 island = GNUNET_malloc (sizeof (struct Island));
1236 if (0 == strncmp (island_id, hostname, ISLANDNAME_SIZE - 1))
1238 GNUNET_array_append (island->hosts, island->nhosts, host);
1241 DEBUG ("Adding island `%s' with %u hosts\n", island_id, island->nhosts);
1242 GNUNET_array_append (rc->islands, rc->nislands, island);
1247 DEBUG ("Adding island `%s' with %u hosts\n", island_id, island->nhosts);
1248 GNUNET_array_append (rc->islands, rc->nislands, island);
1250 DEBUG ("Total islands parsed: %u\n", rc->nislands);
1256 * Callback function invoked for each interface found.
1258 * @param cls closure
1259 * @param name name of the interface (can be NULL for unknown)
1260 * @param isDefault is this presumably the default interface
1261 * @param addr address of this interface (can be NULL for unknown or unassigned)
1262 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
1263 * @param netmask the network mask (can be NULL for unknown or unassigned))
1264 * @param addrlen length of the address
1265 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1268 netint_proc (void *cls, const char *name, int isDefault,
1269 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
1270 const struct sockaddr *netmask, socklen_t addrlen)
1272 struct RunContext *rc = cls;
1273 char hostip[NI_MAXHOST];
1276 if (sizeof (struct sockaddr_in) != addrlen)
1277 return GNUNET_OK; /* Only consider IPv4 for now */
1279 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
1280 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
1281 if (NULL == rc->trusted_ip)
1283 rc->trusted_ip = GNUNET_strdup (hostip);
1286 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
1287 GNUNET_free (rc->trusted_ip);
1288 rc->trusted_ip = buf;
1294 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1295 * inform whether the given host is habitable or not. The Handle returned by
1296 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1299 * @param host the host whose status is being reported; will be NULL if the host
1300 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1301 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1304 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
1307 struct CompatibilityCheckContext *hc = cls;
1308 struct RunContext *rc;
1309 struct GNUNET_TESTBED_Host **old_hosts;
1312 GNUNET_assert (NULL != (rc = hc->rc));
1314 GNUNET_assert (nhost <= rc->num_hosts);
1315 GNUNET_assert (host == rc->hosts[nhost]);
1317 if (GNUNET_NO == status)
1319 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1320 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1321 GNUNET_TESTBED_host_get_hostname (host));
1323 LOG (GNUNET_ERROR_TYPE_ERROR,
1324 _("Testbed cannot be started on localhost\n"));
1329 if (rc->reg_hosts < rc->num_hosts)
1331 GNUNET_free (rc->hclist);
1333 rc->h = rc->hosts[0];
1335 if (0 < rc->num_hosts)
1337 old_hosts = rc->hosts;
1339 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1340 memcpy (rc->hosts, &old_hosts[1],
1341 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1342 GNUNET_free (old_hosts);
1346 GNUNET_free (rc->hosts);
1352 GNUNET_OS_network_interfaces_list (netint_proc, rc);
1353 if (NULL == rc->trusted_ip)
1354 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1356 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1357 &controller_status_cb, rc);
1358 GNUNET_free (rc->trusted_ip);
1359 rc->trusted_ip = NULL;
1360 if (NULL == rc->cproc)
1362 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1369 * Task run upon timeout while setting up the testbed
1371 * @param cls the RunContext
1372 * @param tc the task context
1375 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1377 struct RunContext *rc = cls;
1379 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1380 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1382 if (NULL != rc->test_master)
1383 rc->test_master (rc->test_master_cls, 0, NULL, 0, 0);
1384 rc->test_master = NULL;
1389 * Convenience method for running a testbed with
1390 * a single call. Underlay and overlay topology
1391 * are configured using the "UNDERLAY" and "OVERLAY"
1392 * options in the "[testbed]" section of the configuration\
1393 * (with possible options given in "UNDERLAY_XXX" and/or
1396 * The testbed is to be terminated using a call to
1397 * "GNUNET_SCHEDULER_shutdown".
1399 * @param host_filename name of the file with the 'hosts', NULL
1400 * to run everything on 'localhost'
1401 * @param cfg configuration to use (for testbed, controller and peers)
1402 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1403 * @param event_mask bit mask with set of events to call 'cc' for;
1404 * or-ed values of "1LL" shifted by the
1405 * respective 'enum GNUNET_TESTBED_EventType'
1406 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1407 * @param cc controller callback to invoke on events; This callback is called
1408 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1409 * set in the event_mask as this is the only way get access to the
1410 * handle of each peer
1411 * @param cc_cls closure for cc
1412 * @param test_master this callback will be called once the test is ready
1413 * @param test_master_cls closure for 'test_master'.
1416 GNUNET_TESTBED_run (const char *host_filename,
1417 const struct GNUNET_CONFIGURATION_Handle *cfg,
1418 unsigned int num_peers, uint64_t event_mask,
1419 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1420 GNUNET_TESTBED_TestMaster test_master,
1421 void *test_master_cls)
1423 struct RunContext *rc;
1425 struct CompatibilityCheckContext *hc;
1426 struct GNUNET_TIME_Relative timeout;
1427 unsigned long long random_links;
1431 GNUNET_assert (num_peers > 0);
1432 rc = GNUNET_malloc (sizeof (struct RunContext));
1433 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1435 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1437 if (0 == rc->num_hosts)
1439 LOG (GNUNET_ERROR_TYPE_WARNING,
1440 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1444 if (NULL != host_filename)
1447 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1449 if (0 == rc->num_hosts)
1451 LOG (GNUNET_ERROR_TYPE_WARNING,
1452 _("No hosts loaded. Need at least one host\n"));
1457 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1459 rc->num_peers = num_peers;
1460 rc->event_mask = event_mask;
1462 rc->cc_cls = cc_cls;
1463 rc->test_master = test_master;
1464 rc->test_master_cls = test_master_cls;
1465 rc->state = RC_INIT;
1466 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1468 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1469 "OVERLAY_TOPOLOGY", &topology))
1471 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1473 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1476 ("Specified topology must be supported by testbed"));
1478 GNUNET_free (topology);
1480 switch (rc->topology)
1482 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1483 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1484 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1486 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1487 "OVERLAY_RANDOM_LINKS",
1490 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1491 * option to be set to the number of random links to be established */
1492 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1493 "OVERLAY_RANDOM_LINKS");
1496 if (random_links > UINT32_MAX)
1498 GNUNET_break (0); /* Too big number */
1501 rc->random_links = (unsigned int) random_links;
1503 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1505 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1506 "OVERLAY_TOPOLOGY_FILE",
1509 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1510 "OVERLAY_TOPOLOGY_FILE");
1514 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1516 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1517 "OVERLAY_RANDOM_LINKS"))
1518 LOG (GNUNET_ERROR_TYPE_WARNING,
1519 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1522 if (0 != rc->num_hosts)
1524 rc->hclist = GNUNET_malloc (sizeof (struct CompatibilityCheckContext)
1526 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1528 hc = &rc->hclist[nhost];
1531 hc->h = GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1532 &host_habitable_cb, hc);
1536 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1538 hc = &rc->hclist[nhost];
1540 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
1542 GNUNET_free (rc->hclist);
1550 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1551 &controller_status_cb, rc);
1552 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1556 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1557 DEFAULT_SETUP_TIMEOUT);
1559 rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1561 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1562 rc->interrupt_task =
1563 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1569 GNUNET_TESTBED_host_destroy (rc->h);
1570 if (NULL != rc->hosts)
1572 for (hid = 0; hid < rc->num_hosts; hid++)
1573 if (NULL != rc->hosts[hid])
1574 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1575 GNUNET_free (rc->hosts);
1577 if (NULL != rc->cfg)
1578 GNUNET_CONFIGURATION_destroy (rc->cfg);
1583 /* end of testbed_api_testbed.c */