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
53 * Context information for the operation we start
55 struct RunContextOperation
58 * The testbed operation handle
60 struct GNUNET_TESTBED_Operation *op;
63 * Context information for GNUNET_TESTBED_run()
65 struct RunContext *rc;
76 * States of RunContext
86 * Controllers on given hosts started and linked
96 * The testbed run is ready and the master callback can be called now. At this
97 * time the peers are all started and if a topology is provided in the
98 * configuration the topology would have been attempted
103 /* * Peers are stopped */
105 /* RC_PEERS_STOPPED, */
108 /* * Peers are destroyed */
110 /* RC_PEERS_DESTROYED */
113 * All peers shutdown (stopped and destroyed)
125 * The controller handle
127 struct GNUNET_TESTBED_Controller *c;
130 * The configuration of the controller. This is based on the cfg given to the
131 * function GNUNET_TESTBED_run(). We also use this config as a template while
134 struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Handle to the host on which the controller runs
139 struct GNUNET_TESTBED_Host *h;
142 * The handle to the controller process
144 struct GNUNET_TESTBED_ControllerProc *cproc;
147 * The callback to use as controller callback
149 GNUNET_TESTBED_ControllerCallback cc;
152 * The pointer to the controller callback
157 * The trusted IP string
162 * TestMaster callback to call when testbed initialization is done
164 GNUNET_TESTBED_TestMaster test_master;
167 * The closure for the TestMaster callback
169 void *test_master_cls;
172 * A hashmap for operations started by us
174 struct GNUNET_CONTAINER_MultiHashMap32 *rcop_map;
177 * An array of hosts loaded from the hostkeys file
179 struct GNUNET_TESTBED_Host **hosts;
182 * The handle for whether a host is habitable or not
184 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
187 * Array of peers which we create
189 struct GNUNET_TESTBED_Peer **peers;
192 * The topology generation operation. Will be null if no topology is set in
195 struct GNUNET_TESTBED_Operation *topology_operation;
198 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
203 * Host registration handle
205 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
208 * Profiling start time
210 struct GNUNET_TIME_Absolute pstart_time;
213 * Host registration task
215 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
218 * Task to be run while shutting down
220 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
223 * Task to be run of a timeout
225 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
228 * Task run upon shutdown interrupts
230 GNUNET_SCHEDULER_TaskIdentifier interrupt_task;
233 * The event mask for the controller
238 * State of this context
243 * The topology which has to be achieved with the peers started in this context
245 enum GNUNET_TESTBED_TopologyOption topology;
248 * Have we already shutdown
253 * Number of hosts in the given host file
255 unsigned int num_hosts;
258 * Number of registered hosts. Also used as a counter while checking
259 * habitabillity of hosts
261 unsigned int reg_hosts;
264 * Current peer count for an operation; Set this to 0 and increment for each
265 * successful operation on a peer
267 unsigned int peer_count;
270 * number of peers to start
272 unsigned int num_peers;
275 * Expected overlay connects. Should be zero if no topology is relavant
280 * Number of random links to established
282 unsigned int random_links;
285 * the number of overlay link connection attempts that succeeded
287 unsigned int links_succeeded;
290 * the number of overlay link connection attempts that failed
292 unsigned int links_failed;
297 rcop_key (void *rcop)
299 return * ((uint32_t *) &rcop);
305 struct GNUNET_TESTBED_Operation *query;
307 struct RunContextOperation *result;
311 search_iterator (void *cls, uint32_t key, void *value)
313 struct RunContextOperation *rcop = value;
314 struct SearchContext *sc = cls;
316 GNUNET_assert (NULL != rcop);
317 if (sc->query == rcop->op)
319 GNUNET_assert (NULL == sc->result);
326 static struct RunContextOperation *
327 search_rcop (struct RunContext *rc, struct GNUNET_TESTBED_Operation *op)
329 struct SearchContext sc;
334 GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
339 GNUNET_assert (NULL != sc.result);
346 insert_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
348 GNUNET_assert (GNUNET_OK ==
349 GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
350 rcop_key (rcop->op), rcop,
351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
355 remove_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
357 GNUNET_assert (GNUNET_YES ==
358 GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
364 * Assuming all peers have been destroyed cleanup run handle
366 * @param cls the run handle
367 * @param tc the task context from scheduler
370 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
372 struct RunContext *rc = cls;
375 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
376 GNUNET_assert (NULL == rc->reg_handle);
377 GNUNET_assert (NULL == rc->peers);
378 GNUNET_assert (NULL == rc->hc_handles);
379 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
380 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
381 GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
383 GNUNET_TESTBED_controller_disconnect (rc->c);
384 if (NULL != rc->cproc)
385 GNUNET_TESTBED_controller_stop (rc->cproc);
387 GNUNET_TESTBED_host_destroy (rc->h);
388 for (hid = 0; hid < rc->num_hosts; hid++)
389 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
390 GNUNET_free_non_null (rc->hosts);
392 GNUNET_CONFIGURATION_destroy (rc->cfg);
393 GNUNET_free_non_null (rc->topo_file);
394 GNUNET_free_non_null (rc->trusted_ip);
399 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
401 struct RunContext *rc = cls;
402 struct RunContextOperation *rcop = value;
404 GNUNET_assert (rc == rcop->rc);
405 remove_rcop (rc, rcop);
406 GNUNET_TESTBED_operation_done (rcop->op);
412 cleanup (struct RunContext *rc)
416 if (NULL != rc->hc_handles)
418 for (nhost = 0; nhost < rc->num_hosts; nhost++)
419 if (NULL != rc->hc_handles[nhost])
420 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
421 GNUNET_free (rc->hc_handles);
422 rc->hc_handles = NULL;
424 /* Stop register hosts task if it is running */
425 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
427 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
428 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
430 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
432 GNUNET_SCHEDULER_cancel (rc->timeout_task);
433 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
435 if (GNUNET_SCHEDULER_NO_TASK != rc->interrupt_task)
437 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
438 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
440 if (NULL != rc->reg_handle)
442 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
443 rc->reg_handle = NULL;
445 if (NULL != rc->topology_operation)
447 GNUNET_TESTBED_operation_done (rc->topology_operation);
448 rc->topology_operation = NULL;
450 /* cancel any exiting operations */
451 GNUNET_assert (GNUNET_SYSERR !=
452 GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
453 &rcop_cleanup_iterator,
459 * Stops the testbed run and releases any used resources
461 * @param cls the tesbed run handle
462 * @param tc the task context from scheduler
465 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
467 struct RunContext *rc = cls;
468 struct RunContextOperation *rcop;
470 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
471 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
472 GNUNET_assert (GNUNET_NO == rc->shutdown);
473 rc->shutdown = GNUNET_YES;
477 if (NULL != rc->peers)
479 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
481 rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
482 GNUNET_assert (NULL != rcop->op);
483 DEBUG ("Shutting down peers\n");
484 rc->pstart_time = GNUNET_TIME_absolute_get ();
485 insert_rcop (rc, rcop);
489 rc->state = RC_PEERS_SHUTDOWN; /* No peers are present so we consider the
490 * state where all peers are SHUTDOWN */
491 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
496 * Function to shutdown now
498 * @param rc the RunContext
501 shutdown_now (struct RunContext *rc)
503 if (GNUNET_YES == rc->shutdown)
505 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
506 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
507 GNUNET_SCHEDULER_shutdown (); /* Trigger shutdown in programs using this API */
508 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
513 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
515 struct RunContext *rc = cls;
517 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
523 * Function to return the string representation of the duration between current
524 * time and `pstart_time' in `RunContext'
526 * @param rc the RunContext
527 * @return the representation string; this is NOT reentrant
530 prof_time (struct RunContext *rc)
532 struct GNUNET_TIME_Relative ptime;
534 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
535 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
540 * Task for starting peers
542 * @param cls the RunHandle
543 * @param tc the task context from scheduler
546 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
548 struct RunContext *rc = cls;
549 struct RunContextOperation *rcop;
552 DEBUG ("Starting Peers\n");
553 rc->pstart_time = GNUNET_TIME_absolute_get ();
554 for (peer = 0; peer < rc->num_peers; peer++)
556 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
558 rcop->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
559 GNUNET_assert (NULL != rcop->op);
560 rcop->cls = rc->peers[peer];
561 insert_rcop (rc, rcop);
568 * Functions of this signature are called when a peer has been successfully
571 * @param cls the closure from GNUNET_TESTBED_peer_create()
572 * @param peer the handle for the created peer; NULL on any error during
574 * @param emsg NULL if peer is not NULL; else MAY contain the error description
577 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
579 struct RunContextOperation *rcop = cls;
580 struct RunContext *rc;
582 GNUNET_assert (NULL != rcop);
583 GNUNET_assert (NULL != (rc = rcop->rc));
584 remove_rcop (rc, rcop);
585 GNUNET_TESTBED_operation_done (rcop->op);
590 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
595 rc->peers[rc->peer_count] = peer;
597 if (rc->peer_count < rc->num_peers)
599 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
600 rc->state = RC_PEERS_CREATED;
601 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
606 * call test master callback
608 * @param rc the RunContext
611 call_master (struct RunContext *rc)
613 GNUNET_SCHEDULER_cancel (rc->timeout_task);
614 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
615 if (NULL != rc->test_master)
616 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers,
617 rc->links_succeeded, rc->links_failed);
622 * Callbacks of this type are called when topology configuration is completed
624 * @param cls the operation closure given to
625 * GNUNET_TESTBED_overlay_configure_topology_va() and
626 * GNUNET_TESTBED_overlay_configure() calls
627 * @param nsuccess the number of successful overlay connects
628 * @param nfailures the number of overlay connects which failed
631 topology_completion_callback (void *cls, unsigned int nsuccess,
632 unsigned int nfailures)
634 struct RunContext *rc = cls;
636 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
637 GNUNET_TESTBED_operation_done (rc->topology_operation);
638 rc->topology_operation = NULL;
639 rc->links_succeeded = nsuccess;
640 rc->links_failed = nfailures;
641 rc->state = RC_READY;
647 * Function to create peers
649 * @param rc the RunContext
652 create_peers (struct RunContext *rc)
654 struct RunContextOperation *rcop;
657 DEBUG ("Creating peers\n");
658 rc->pstart_time = GNUNET_TIME_absolute_get ();
660 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
661 GNUNET_assert (NULL != rc->c);
663 for (peer = 0; peer < rc->num_peers; peer++)
665 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
668 GNUNET_TESTBED_peer_create (rc->c,
670 rc->num_hosts) ? rc->h : rc->hosts[peer %
672 rc->cfg, &peer_create_cb, rcop);
673 GNUNET_assert (NULL != rcop->op);
674 insert_rcop (rc, rcop);
680 * Signature of the event handler function called by the
681 * respective event controller.
684 * @param event information about the event
687 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
689 struct RunContext *rc = cls;
690 struct RunContextOperation *rcop;
692 if (RC_INIT == rc->state)
696 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
697 rcop = event->op_cls;
698 if (NULL != event->details.operation_finished.emsg)
700 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
705 GNUNET_assert (event->op == rcop->op);
706 remove_rcop (rc, rcop);
707 GNUNET_TESTBED_operation_done (rcop->op);
709 if (rc->reg_hosts == rc->num_hosts)
711 rc->state = RC_LINKED;
721 if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
723 if (NULL == (rcop = search_rcop (rc, event->op)))
725 remove_rcop (rc, rcop);
726 GNUNET_TESTBED_operation_done (rcop->op);
728 if ( (GNUNET_NO == rc->shutdown)
729 && (NULL != event->details.operation_finished.emsg) )
731 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
732 event->details.operation_finished.emsg);
736 GNUNET_assert (GNUNET_YES == rc->shutdown);
740 case RC_PEERS_CREATED:
742 rc->state = RC_PEERS_SHUTDOWN;
743 GNUNET_free_non_null (rc->peers);
745 DEBUG ("Peers shut down in %s\n", prof_time (rc));
746 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
754 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
755 rc->cc (rc->cc_cls, event);
756 if (GNUNET_TESTBED_ET_PEER_START != event->type)
758 if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
760 remove_rcop (rc, rcop);
761 GNUNET_TESTBED_operation_done (rcop->op);
764 if (rc->peer_count < rc->num_peers)
766 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
767 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
769 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
770 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
771 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
773 rc->topology_operation =
774 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
775 rc->peers, &rc->num_oc,
776 &topology_completion_callback,
780 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
782 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
784 GNUNET_assert (NULL != rc->topo_file);
785 rc->topology_operation =
786 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
787 rc->peers, &rc->num_oc,
788 &topology_completion_callback,
792 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
795 rc->topology_operation =
796 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
797 rc->peers, &rc->num_oc,
798 &topology_completion_callback,
801 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
802 if (NULL == rc->topology_operation)
803 LOG (GNUNET_ERROR_TYPE_WARNING,
804 "Not generating topology. Check number of peers\n");
807 DEBUG ("Creating overlay topology\n");
808 rc->pstart_time = GNUNET_TIME_absolute_get ();
812 rc->state = RC_READY;
818 * Task to register all hosts available in the global host list
820 * @param cls the RunContext
821 * @param tc the scheduler task context
824 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
828 * Callback which will be called to after a host registration succeeded or failed
830 * @param cls the closure
831 * @param emsg the error message; NULL if host registration is successful
834 host_registration_completion (void *cls, const char *emsg)
836 struct RunContext *rc = cls;
838 rc->reg_handle = NULL;
841 LOG (GNUNET_ERROR_TYPE_WARNING,
842 _("Host registration failed for a host. Error: %s\n"), emsg);
846 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
851 * Task to register all hosts available in the global host list
853 * @param cls RunContext
854 * @param tc the scheduler task context
857 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
859 struct RunContext *rc = cls;
860 struct RunContextOperation *rcop;
863 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
864 if (rc->reg_hosts == rc->num_hosts)
866 DEBUG ("All hosts successfully registered\n");
868 for (slave = 0; slave < rc->num_hosts; slave++)
870 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
873 GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[slave],
874 rc->h, rc->cfg, GNUNET_YES);
875 GNUNET_assert (NULL != rcop->op);
876 insert_rcop (rc, rcop);
882 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
883 host_registration_completion, rc);
889 * Callback to signal successfull startup of the controller process
891 * @param cls the closure from GNUNET_TESTBED_controller_start()
892 * @param cfg the configuration with which the controller has been started;
893 * NULL if status is not GNUNET_OK
894 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
895 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
898 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
901 struct RunContext *rc = cls;
904 if (status != GNUNET_OK)
906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
907 _("Controller crash detected. Shutting down.\n"));
910 if (NULL != rc->peers)
912 GNUNET_free (rc->peers);
915 if (GNUNET_YES == rc->shutdown)
917 rc->state = RC_PEERS_SHUTDOWN;
918 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
924 GNUNET_CONFIGURATION_destroy (rc->cfg);
925 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
926 event_mask = rc->event_mask;
927 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
928 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
929 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
930 event_mask |= GNUNET_TESTBED_ET_CONNECT;
932 GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
933 if (0 < rc->num_hosts)
936 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
939 rc->state = RC_LINKED;
945 * Callback function invoked for each interface found.
948 * @param name name of the interface (can be NULL for unknown)
949 * @param isDefault is this presumably the default interface
950 * @param addr address of this interface (can be NULL for unknown or unassigned)
951 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
952 * @param netmask the network mask (can be NULL for unknown or unassigned))
953 * @param addrlen length of the address
954 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
957 netint_proc (void *cls, const char *name, int isDefault,
958 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
959 const struct sockaddr *netmask, socklen_t addrlen)
961 struct RunContext *rc = cls;
962 char hostip[NI_MAXHOST];
965 if (sizeof (struct sockaddr_in) != addrlen)
966 return GNUNET_OK; /* Only consider IPv4 for now */
968 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
969 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
970 if (NULL == rc->trusted_ip)
972 rc->trusted_ip = GNUNET_strdup (hostip);
975 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
976 GNUNET_free (rc->trusted_ip);
977 rc->trusted_ip = buf;
983 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
984 * inform whether the given host is habitable or not. The Handle returned by
985 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
988 * @param host the host whose status is being reported; will be NULL if the host
989 * given to GNUNET_TESTBED_is_host_habitable() is NULL
990 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
993 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
996 struct RunContext *rc = cls;
997 struct GNUNET_TESTBED_Host **old_hosts;
1000 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1002 if (host == rc->hosts[nhost])
1005 GNUNET_assert (nhost != rc->num_hosts);
1006 rc->hc_handles[nhost] = NULL;
1007 if (GNUNET_NO == status)
1009 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1010 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1011 GNUNET_TESTBED_host_get_hostname (host));
1013 LOG (GNUNET_ERROR_TYPE_ERROR,
1014 _("Testbed cannot be started on localhost\n"));
1019 if (rc->reg_hosts < rc->num_hosts)
1021 GNUNET_free (rc->hc_handles);
1022 rc->hc_handles = NULL;
1023 rc->h = rc->hosts[0];
1025 if (0 < rc->num_hosts)
1027 old_hosts = rc->hosts;
1029 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1030 memcpy (rc->hosts, &old_hosts[1],
1031 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1032 GNUNET_free (old_hosts);
1036 GNUNET_free (rc->hosts);
1039 GNUNET_OS_network_interfaces_list (netint_proc, rc);
1040 if (NULL == rc->trusted_ip)
1041 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1043 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1044 &controller_status_cb, rc);
1045 GNUNET_free (rc->trusted_ip);
1046 rc->trusted_ip = NULL;
1047 if (NULL == rc->cproc)
1049 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1056 * Task run upon timeout while setting up the testbed
1058 * @param cls the RunContext
1059 * @param tc the task context
1062 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1064 struct RunContext *rc = cls;
1066 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1067 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1069 if (NULL != rc->test_master)
1070 rc->test_master (rc->test_master_cls, 0, NULL, 0, 0);
1071 rc->test_master = NULL;
1076 * Convenience method for running a testbed with
1077 * a single call. Underlay and overlay topology
1078 * are configured using the "UNDERLAY" and "OVERLAY"
1079 * options in the "[testbed]" section of the configuration\
1080 * (with possible options given in "UNDERLAY_XXX" and/or
1083 * The testbed is to be terminated using a call to
1084 * "GNUNET_SCHEDULER_shutdown".
1086 * @param host_filename name of the file with the 'hosts', NULL
1087 * to run everything on 'localhost'
1088 * @param cfg configuration to use (for testbed, controller and peers)
1089 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1090 * @param event_mask bit mask with set of events to call 'cc' for;
1091 * or-ed values of "1LL" shifted by the
1092 * respective 'enum GNUNET_TESTBED_EventType'
1093 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1094 * @param cc controller callback to invoke on events; This callback is called
1095 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1096 * set in the event_mask as this is the only way get access to the
1097 * handle of each peer
1098 * @param cc_cls closure for cc
1099 * @param test_master this callback will be called once the test is ready
1100 * @param test_master_cls closure for 'test_master'.
1103 GNUNET_TESTBED_run (const char *host_filename,
1104 const struct GNUNET_CONFIGURATION_Handle *cfg,
1105 unsigned int num_peers, uint64_t event_mask,
1106 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1107 GNUNET_TESTBED_TestMaster test_master,
1108 void *test_master_cls)
1110 struct RunContext *rc;
1112 struct GNUNET_TIME_Relative timeout;
1113 unsigned long long random_links;
1117 GNUNET_assert (num_peers > 0);
1118 rc = GNUNET_malloc (sizeof (struct RunContext));
1119 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1121 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1123 if (0 == rc->num_hosts)
1125 LOG (GNUNET_ERROR_TYPE_WARNING,
1126 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1130 if (NULL != host_filename)
1133 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1135 if (0 == rc->num_hosts)
1137 LOG (GNUNET_ERROR_TYPE_WARNING,
1138 _("No hosts loaded. Need at least one host\n"));
1143 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1145 rc->num_peers = num_peers;
1146 rc->event_mask = event_mask;
1148 rc->cc_cls = cc_cls;
1149 rc->test_master = test_master;
1150 rc->test_master_cls = test_master_cls;
1151 rc->state = RC_INIT;
1152 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1154 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1155 "OVERLAY_TOPOLOGY", &topology))
1157 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1159 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1162 ("Specified topology must be supported by testbed"));
1164 GNUNET_free (topology);
1166 switch (rc->topology)
1168 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1169 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1170 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1172 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1173 "OVERLAY_RANDOM_LINKS",
1176 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1177 * option to be set to the number of random links to be established */
1178 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1179 "OVERLAY_RANDOM_LINKS");
1182 if (random_links > UINT32_MAX)
1184 GNUNET_break (0); /* Too big number */
1187 rc->random_links = (unsigned int) random_links;
1189 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1191 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1192 "OVERLAY_TOPOLOGY_FILE",
1195 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1196 "OVERLAY_TOPOLOGY_FILE");
1200 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1202 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1203 "OVERLAY_RANDOM_LINKS"))
1204 LOG (GNUNET_ERROR_TYPE_WARNING,
1205 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1208 if (0 != rc->num_hosts)
1211 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle *)
1213 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1216 (rc->hc_handles[nhost] =
1217 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1218 &host_habitable_cb, rc)))
1221 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1222 if (NULL != rc->hc_handles[nhost])
1223 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1224 GNUNET_free (rc->hc_handles);
1225 rc->hc_handles = NULL;
1232 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1233 &controller_status_cb, rc);
1234 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1238 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1239 DEFAULT_SETUP_TIMEOUT);
1241 rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1243 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1244 rc->interrupt_task =
1245 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1251 GNUNET_TESTBED_host_destroy (rc->h);
1252 if (NULL != rc->hosts)
1254 for (hid = 0; hid < rc->num_hosts; hid++)
1255 if (NULL != rc->hosts[hid])
1256 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1257 GNUNET_free (rc->hosts);
1259 if (NULL != rc->cfg)
1260 GNUNET_CONFIGURATION_destroy (rc->cfg);
1265 /* end of testbed_api_testbed.c */