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 * The testbed operation handle
60 struct GNUNET_TESTBED_Operation *op;
63 * Context information for GNUNET_TESTBED_run()
65 struct RunContext *rc;
73 * The next pointer for DLL
75 struct DLLOperation *next;
78 * The prev pointer for DLL
80 struct DLLOperation *prev;
85 * States of RunContext
95 * Controllers on given hosts started and linked
105 * The testbed run is ready and the master callback can be called now. At this
106 * time the peers are all started and if a topology is provided in the
107 * configuration the topology would have been attempted
112 /* * Peers are stopped */
114 /* RC_PEERS_STOPPED, */
117 /* * Peers are destroyed */
119 /* RC_PEERS_DESTROYED */
122 * All peers shutdown (stopped and destroyed)
134 * The controller handle
136 struct GNUNET_TESTBED_Controller *c;
139 * The configuration of the controller. This is based on the cfg given to the
140 * function GNUNET_TESTBED_run(). We also use this config as a template while
143 struct GNUNET_CONFIGURATION_Handle *cfg;
146 * Handle to the host on which the controller runs
148 struct GNUNET_TESTBED_Host *h;
151 * The handle to the controller process
153 struct GNUNET_TESTBED_ControllerProc *cproc;
156 * The callback to use as controller callback
158 GNUNET_TESTBED_ControllerCallback cc;
161 * The pointer to the controller callback
166 * The trusted IP string
171 * TestMaster callback to call when testbed initialization is done
173 GNUNET_TESTBED_TestMaster test_master;
176 * The closure for the TestMaster callback
178 void *test_master_cls;
181 * The head element of DLL operations
183 struct DLLOperation *dll_op_head;
186 * The tail element of DLL operations
188 struct DLLOperation *dll_op_tail;
191 * An array of hosts loaded from the hostkeys file
193 struct GNUNET_TESTBED_Host **hosts;
196 * The handle for whether a host is habitable or not
198 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
201 * Array of peers which we create
203 struct GNUNET_TESTBED_Peer **peers;
206 * The topology generation operation. Will be null if no topology is set in
209 struct GNUNET_TESTBED_Operation *topology_operation;
212 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
217 * Host registration handle
219 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
222 * Profiling start time
224 struct GNUNET_TIME_Absolute pstart_time;
227 * Host registration task
229 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
232 * Task to be run while shutting down
234 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
237 * Task to be run of a timeout
239 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
242 * Task run upon shutdown interrupts
244 GNUNET_SCHEDULER_TaskIdentifier interrupt_task;
247 * The event mask for the controller
252 * State of this context
257 * The topology which has to be achieved with the peers started in this context
259 enum GNUNET_TESTBED_TopologyOption topology;
262 * Have we already shutdown
267 * Number of hosts in the given host file
269 unsigned int num_hosts;
272 * Number of registered hosts. Also used as a counter while checking
273 * habitabillity of hosts
275 unsigned int reg_hosts;
278 * Current peer count for an operation; Set this to 0 and increment for each
279 * successful operation on a peer
281 unsigned int peer_count;
284 * number of peers to start
286 unsigned int num_peers;
289 * Expected overlay connects. Should be zero if no topology is relavant
294 * Number of random links to established
296 unsigned int random_links;
299 * the number of overlay link connection attempts that succeeded
301 unsigned int links_succeeded;
304 * the number of overlay link connection attempts that failed
306 unsigned int links_failed;
312 * Assuming all peers have been destroyed cleanup run handle
314 * @param cls the run handle
315 * @param tc the task context from scheduler
318 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
320 struct RunContext *rc = cls;
323 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
324 GNUNET_assert (NULL == rc->reg_handle);
325 GNUNET_assert (NULL == rc->peers);
326 GNUNET_assert (NULL == rc->hc_handles);
327 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
328 GNUNET_assert (NULL == rc->dll_op_head);
330 GNUNET_TESTBED_controller_disconnect (rc->c);
331 if (NULL != rc->cproc)
332 GNUNET_TESTBED_controller_stop (rc->cproc);
334 GNUNET_TESTBED_host_destroy (rc->h);
335 for (hid = 0; hid < rc->num_hosts; hid++)
336 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
337 GNUNET_free_non_null (rc->hosts);
339 GNUNET_CONFIGURATION_destroy (rc->cfg);
340 GNUNET_free_non_null (rc->topo_file);
341 GNUNET_free_non_null (rc->trusted_ip);
346 cleanup (struct RunContext *rc)
348 struct DLLOperation *dll_op;
351 if (NULL != rc->hc_handles)
353 for (nhost = 0; nhost < rc->num_hosts; nhost++)
354 if (NULL != rc->hc_handles[nhost])
355 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
356 GNUNET_free (rc->hc_handles);
357 rc->hc_handles = NULL;
359 /* Stop register hosts task if it is running */
360 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
362 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
363 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
365 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
367 GNUNET_SCHEDULER_cancel (rc->timeout_task);
368 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
370 if (GNUNET_SCHEDULER_NO_TASK != rc->interrupt_task)
372 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
373 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
375 if (NULL != rc->reg_handle)
377 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
378 rc->reg_handle = NULL;
380 if (NULL != rc->topology_operation)
382 GNUNET_TESTBED_operation_done (rc->topology_operation);
383 rc->topology_operation = NULL;
385 /* cancel any exiting operations */
386 while (NULL != (dll_op = rc->dll_op_head))
388 GNUNET_TESTBED_operation_done (dll_op->op);
389 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
390 GNUNET_free (dll_op);
396 * Stops the testbed run and releases any used resources
398 * @param cls the tesbed run handle
399 * @param tc the task context from scheduler
402 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
404 struct RunContext *rc = cls;
405 struct DLLOperation *dll_op;
407 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
408 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
409 GNUNET_assert (GNUNET_NO == rc->shutdown);
410 rc->shutdown = GNUNET_YES;
414 if (NULL != rc->peers)
416 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
417 dll_op->op = GNUNET_TESTBED_shutdown_peers (rc->c, dll_op, NULL, NULL);
418 GNUNET_assert (NULL != dll_op->op);
419 DEBUG ("Shutting down peers\n");
420 rc->pstart_time = GNUNET_TIME_absolute_get ();
421 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
426 rc->state = RC_PEERS_SHUTDOWN; /* No peers are present so we consider the
427 * state where all peers are SHUTDOWN */
428 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
433 * Function to shutdown now
435 * @param rc the RunContext
438 shutdown_now (struct RunContext *rc)
440 if (GNUNET_YES == rc->shutdown)
442 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
443 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
444 GNUNET_SCHEDULER_shutdown (); /* Trigger shutdown in programs using this API */
445 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
450 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
452 struct RunContext *rc = cls;
454 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
460 * Function to return the string representation of the duration between current
461 * time and `pstart_time' in `RunContext'
463 * @param rc the RunContext
464 * @return the representation string; this is NOT reentrant
467 prof_time (struct RunContext *rc)
469 struct GNUNET_TIME_Relative ptime;
471 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
472 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
477 * Task for starting peers
479 * @param cls the RunHandle
480 * @param tc the task context from scheduler
483 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
485 struct RunContext *rc = cls;
486 struct DLLOperation *dll_op;
489 DEBUG ("Starting Peers\n");
490 rc->pstart_time = GNUNET_TIME_absolute_get ();
491 for (peer = 0; peer < rc->num_peers; peer++)
493 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
494 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
495 GNUNET_assert (NULL != dll_op->op);
496 dll_op->cls = rc->peers[peer];
497 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
504 * Functions of this signature are called when a peer has been successfully
507 * @param cls the closure from GNUNET_TESTBED_peer_create()
508 * @param peer the handle for the created peer; NULL on any error during
510 * @param emsg NULL if peer is not NULL; else MAY contain the error description
513 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
515 struct DLLOperation *dll_op = cls;
516 struct RunContext *rc;
518 GNUNET_assert (NULL != dll_op);
520 GNUNET_assert (NULL != rc);
521 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
522 GNUNET_TESTBED_operation_done (dll_op->op);
523 GNUNET_free (dll_op);
527 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
532 rc->peers[rc->peer_count] = peer;
534 if (rc->peer_count < rc->num_peers)
536 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
537 rc->state = RC_PEERS_CREATED;
538 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
543 * call test master callback
545 * @param rc the RunContext
548 call_master (struct RunContext *rc)
550 GNUNET_SCHEDULER_cancel (rc->timeout_task);
551 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
552 if (NULL != rc->test_master)
553 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers,
554 rc->links_succeeded, rc->links_failed);
559 * Callbacks of this type are called when topology configuration is completed
561 * @param cls the operation closure given to
562 * GNUNET_TESTBED_overlay_configure_topology_va() and
563 * GNUNET_TESTBED_overlay_configure() calls
564 * @param nsuccess the number of successful overlay connects
565 * @param nfailures the number of overlay connects which failed
568 topology_completion_callback (void *cls, unsigned int nsuccess,
569 unsigned int nfailures)
571 struct RunContext *rc = cls;
573 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
574 GNUNET_TESTBED_operation_done (rc->topology_operation);
575 rc->topology_operation = NULL;
576 rc->links_succeeded = nsuccess;
577 rc->links_failed = nfailures;
578 rc->state = RC_READY;
584 * Function to create peers
586 * @param rc the RunContext
589 create_peers (struct RunContext *rc)
591 struct DLLOperation *dll_op;
594 DEBUG ("Creating peers\n");
595 rc->pstart_time = GNUNET_TIME_absolute_get ();
597 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
598 GNUNET_assert (NULL != rc->c);
600 for (peer = 0; peer < rc->num_peers; peer++)
602 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
605 GNUNET_TESTBED_peer_create (rc->c,
607 rc->num_hosts) ? rc->h : rc->hosts[peer %
609 rc->cfg, peer_create_cb, dll_op);
610 GNUNET_assert (NULL != dll_op->op);
611 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
617 * Signature of the event handler function called by the
618 * respective event controller.
621 * @param event information about the event
624 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
626 struct RunContext *rc = cls;
627 struct DLLOperation *dll_op;
629 if (RC_INIT == rc->state)
633 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
634 dll_op = event->op_cls;
635 if (NULL != event->details.operation_finished.emsg)
637 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
642 GNUNET_assert (event->op == dll_op->op);
643 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
644 GNUNET_TESTBED_operation_done (dll_op->op);
645 GNUNET_free (dll_op);
646 if (rc->reg_hosts == rc->num_hosts)
648 rc->state = RC_LINKED;
658 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
660 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
661 (event->op == dll_op->op))
666 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
667 GNUNET_TESTBED_operation_done (dll_op->op);
668 GNUNET_free (dll_op);
669 if ( (GNUNET_NO == rc->shutdown)
670 && (NULL != event->details.operation_finished.emsg) )
672 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
673 event->details.operation_finished.emsg);
677 GNUNET_assert (GNUNET_YES == rc->shutdown);
681 case RC_PEERS_CREATED:
683 rc->state = RC_PEERS_SHUTDOWN;
684 GNUNET_free_non_null (rc->peers);
686 DEBUG ("Peers shut down in %s\n", prof_time (rc));
687 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
695 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
696 rc->cc (rc->cc_cls, event);
697 if (GNUNET_TESTBED_ET_PEER_START != event->type)
699 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
700 if ((NULL != dll_op->cls) &&
701 (event->details.peer_start.peer == dll_op->cls))
703 if (NULL == dll_op) /* Not our operation */
705 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
706 GNUNET_TESTBED_operation_done (dll_op->op);
707 GNUNET_free (dll_op);
709 if (rc->peer_count < rc->num_peers)
711 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
712 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
714 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
715 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
716 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
718 rc->topology_operation =
719 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
720 rc->peers, &rc->num_oc,
721 &topology_completion_callback,
725 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
727 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
729 GNUNET_assert (NULL != rc->topo_file);
730 rc->topology_operation =
731 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
732 rc->peers, &rc->num_oc,
733 &topology_completion_callback,
737 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
740 rc->topology_operation =
741 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
742 rc->peers, &rc->num_oc,
743 &topology_completion_callback,
746 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
747 if (NULL == rc->topology_operation)
748 LOG (GNUNET_ERROR_TYPE_WARNING,
749 "Not generating topology. Check number of peers\n");
752 DEBUG ("Creating overlay topology\n");
753 rc->pstart_time = GNUNET_TIME_absolute_get ();
757 rc->state = RC_READY;
763 * Task to register all hosts available in the global host list
765 * @param cls the RunContext
766 * @param tc the scheduler task context
769 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
773 * Callback which will be called to after a host registration succeeded or failed
775 * @param cls the closure
776 * @param emsg the error message; NULL if host registration is successful
779 host_registration_completion (void *cls, const char *emsg)
781 struct RunContext *rc = cls;
783 rc->reg_handle = NULL;
786 LOG (GNUNET_ERROR_TYPE_WARNING,
787 _("Host registration failed for a host. Error: %s\n"), emsg);
791 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
796 * Task to register all hosts available in the global host list
798 * @param cls RunContext
799 * @param tc the scheduler task context
802 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
804 struct RunContext *rc = cls;
805 struct DLLOperation *dll_op;
808 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
809 if (rc->reg_hosts == rc->num_hosts)
811 DEBUG ("All hosts successfully registered\n");
813 for (slave = 0; slave < rc->num_hosts; slave++)
815 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
818 GNUNET_TESTBED_controller_link (dll_op, rc->c, rc->hosts[slave],
819 rc->h, rc->cfg, GNUNET_YES);
820 GNUNET_assert (NULL != dll_op->op);
821 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
828 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
829 host_registration_completion, rc);
835 * Callback to signal successfull startup of the controller process
837 * @param cls the closure from GNUNET_TESTBED_controller_start()
838 * @param cfg the configuration with which the controller has been started;
839 * NULL if status is not GNUNET_OK
840 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
841 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
844 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
847 struct RunContext *rc = cls;
850 if (status != GNUNET_OK)
852 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
853 _("Controller crash detected. Shutting down.\n"));
856 if (NULL != rc->peers)
858 GNUNET_free (rc->peers);
861 if (GNUNET_YES == rc->shutdown)
863 rc->state = RC_PEERS_SHUTDOWN;
864 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
870 GNUNET_CONFIGURATION_destroy (rc->cfg);
871 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
872 event_mask = rc->event_mask;
873 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
874 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
875 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
876 event_mask |= GNUNET_TESTBED_ET_CONNECT;
878 GNUNET_TESTBED_controller_connect (rc->cfg, rc->h, event_mask, &event_cb,
880 if (0 < rc->num_hosts)
883 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
886 rc->state = RC_LINKED;
892 * Callback function invoked for each interface found.
895 * @param name name of the interface (can be NULL for unknown)
896 * @param isDefault is this presumably the default interface
897 * @param addr address of this interface (can be NULL for unknown or unassigned)
898 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
899 * @param netmask the network mask (can be NULL for unknown or unassigned))
900 * @param addrlen length of the address
901 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
904 netint_proc (void *cls, const char *name, int isDefault,
905 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
906 const struct sockaddr *netmask, socklen_t addrlen)
908 struct RunContext *rc = cls;
909 char hostip[NI_MAXHOST];
912 if (sizeof (struct sockaddr_in) != addrlen)
913 return GNUNET_OK; /* Only consider IPv4 for now */
915 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
916 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
917 if (NULL == rc->trusted_ip)
919 rc->trusted_ip = GNUNET_strdup (hostip);
922 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
923 GNUNET_free (rc->trusted_ip);
924 rc->trusted_ip = buf;
930 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
931 * inform whether the given host is habitable or not. The Handle returned by
932 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
935 * @param host the host whose status is being reported; will be NULL if the host
936 * given to GNUNET_TESTBED_is_host_habitable() is NULL
937 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
940 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
943 struct RunContext *rc = cls;
944 struct GNUNET_TESTBED_Host **old_hosts;
947 for (nhost = 0; nhost < rc->num_hosts; nhost++)
949 if (host == rc->hosts[nhost])
952 GNUNET_assert (nhost != rc->num_hosts);
953 rc->hc_handles[nhost] = NULL;
954 if (GNUNET_NO == status)
956 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
957 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
958 GNUNET_TESTBED_host_get_hostname (host));
960 LOG (GNUNET_ERROR_TYPE_ERROR,
961 _("Testbed cannot be started on localhost\n"));
966 if (rc->reg_hosts < rc->num_hosts)
968 GNUNET_free (rc->hc_handles);
969 rc->hc_handles = NULL;
970 rc->h = rc->hosts[0];
972 if (0 < rc->num_hosts)
974 old_hosts = rc->hosts;
976 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
977 memcpy (rc->hosts, &old_hosts[1],
978 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
979 GNUNET_free (old_hosts);
983 GNUNET_free (rc->hosts);
986 GNUNET_OS_network_interfaces_list (netint_proc, rc);
987 if (NULL == rc->trusted_ip)
988 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
990 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h, rc->cfg,
991 &controller_status_cb, rc);
992 GNUNET_free (rc->trusted_ip);
993 rc->trusted_ip = NULL;
994 if (NULL == rc->cproc)
996 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1003 * Task run upon timeout while setting up the testbed
1005 * @param cls the RunContext
1006 * @param tc the task context
1009 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1011 struct RunContext *rc = cls;
1013 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1014 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1016 if (NULL != rc->test_master)
1017 rc->test_master (rc->test_master_cls, 0, NULL, 0, 0);
1018 rc->test_master = NULL;
1023 * Convenience method for running a testbed with
1024 * a single call. Underlay and overlay topology
1025 * are configured using the "UNDERLAY" and "OVERLAY"
1026 * options in the "[testbed]" section of the configuration\
1027 * (with possible options given in "UNDERLAY_XXX" and/or
1030 * The testbed is to be terminated using a call to
1031 * "GNUNET_SCHEDULER_shutdown".
1033 * @param host_filename name of the file with the 'hosts', NULL
1034 * to run everything on 'localhost'
1035 * @param cfg configuration to use (for testbed, controller and peers)
1036 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1037 * @param event_mask bit mask with set of events to call 'cc' for;
1038 * or-ed values of "1LL" shifted by the
1039 * respective 'enum GNUNET_TESTBED_EventType'
1040 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1041 * @param cc controller callback to invoke on events; This callback is called
1042 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1043 * set in the event_mask as this is the only way get access to the
1044 * handle of each peer
1045 * @param cc_cls closure for cc
1046 * @param test_master this callback will be called once the test is ready
1047 * @param test_master_cls closure for 'test_master'.
1050 GNUNET_TESTBED_run (const char *host_filename,
1051 const struct GNUNET_CONFIGURATION_Handle *cfg,
1052 unsigned int num_peers, uint64_t event_mask,
1053 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1054 GNUNET_TESTBED_TestMaster test_master,
1055 void *test_master_cls)
1057 struct RunContext *rc;
1059 struct GNUNET_TIME_Relative timeout;
1060 unsigned long long random_links;
1064 GNUNET_assert (num_peers > 0);
1065 rc = GNUNET_malloc (sizeof (struct RunContext));
1066 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1068 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1070 if (0 == rc->num_hosts)
1072 LOG (GNUNET_ERROR_TYPE_WARNING,
1073 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1077 if (NULL != host_filename)
1080 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1082 if (0 == rc->num_hosts)
1084 LOG (GNUNET_ERROR_TYPE_WARNING,
1085 _("No hosts loaded. Need at least one host\n"));
1090 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1092 rc->num_peers = num_peers;
1093 rc->event_mask = event_mask;
1095 rc->cc_cls = cc_cls;
1096 rc->test_master = test_master;
1097 rc->test_master_cls = test_master_cls;
1098 rc->state = RC_INIT;
1099 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1101 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1102 "OVERLAY_TOPOLOGY", &topology))
1104 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1106 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1109 ("Specified topology must be supported by testbed"));
1111 GNUNET_free (topology);
1113 switch (rc->topology)
1115 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1116 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1117 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1119 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1120 "OVERLAY_RANDOM_LINKS",
1123 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1124 * option to be set to the number of random links to be established */
1125 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1126 "OVERLAY_RANDOM_LINKS");
1129 if (random_links > UINT32_MAX)
1131 GNUNET_break (0); /* Too big number */
1134 rc->random_links = (unsigned int) random_links;
1136 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1138 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1139 "OVERLAY_TOPOLOGY_FILE",
1142 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1143 "OVERLAY_TOPOLOGY_FILE");
1147 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1149 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1150 "OVERLAY_RANDOM_LINKS"))
1151 LOG (GNUNET_ERROR_TYPE_WARNING,
1152 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1155 if (0 != rc->num_hosts)
1158 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle *)
1160 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1163 (rc->hc_handles[nhost] =
1164 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1165 &host_habitable_cb, rc)))
1168 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1169 if (NULL != rc->hc_handles[nhost])
1170 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1171 GNUNET_free (rc->hc_handles);
1172 rc->hc_handles = NULL;
1179 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
1180 &controller_status_cb, rc);
1181 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1185 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1186 DEFAULT_SETUP_TIMEOUT);
1189 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1190 rc->interrupt_task =
1191 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1197 GNUNET_TESTBED_host_destroy (rc->h);
1198 if (NULL != rc->hosts)
1200 for (hid = 0; hid < rc->num_hosts; hid++)
1201 if (NULL != rc->hosts[hid])
1202 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1203 GNUNET_free (rc->hosts);
1205 if (NULL != rc->cfg)
1206 GNUNET_CONFIGURATION_destroy (rc->cfg);
1211 /* end of testbed_api_testbed.c */