2 This file is part of GNUnet
3 (C) 2008--2012 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 * The event mask for the controller
247 * State of this context
252 * The topology which has to be achieved with the peers started in this context
254 enum GNUNET_TESTBED_TopologyOption topology;
257 * Have we already shutdown
262 * Number of hosts in the given host file
264 unsigned int num_hosts;
267 * Number of registered hosts. Also used as a counter while checking
268 * habitabillity of hosts
270 unsigned int reg_hosts;
273 * Current peer count for an operation; Set this to 0 and increment for each
274 * successful operation on a peer
276 unsigned int peer_count;
279 * number of peers to start
281 unsigned int num_peers;
284 * Expected overlay connects. Should be zero if no topology is relavant
289 * Number of random links to established
291 unsigned int random_links;
297 * Assuming all peers have been destroyed cleanup run handle
299 * @param cls the run handle
300 * @param tc the task context from scheduler
303 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
305 struct RunContext *rc = cls;
308 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
309 GNUNET_assert (NULL == rc->reg_handle);
310 GNUNET_assert (NULL == rc->peers);
311 GNUNET_assert (NULL == rc->hc_handles);
312 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
313 GNUNET_assert (NULL == rc->dll_op_head);
315 GNUNET_TESTBED_controller_disconnect (rc->c);
316 if (NULL != rc->cproc)
317 GNUNET_TESTBED_controller_stop (rc->cproc);
319 GNUNET_TESTBED_host_destroy (rc->h);
320 for (hid = 0; hid < rc->num_hosts; hid++)
321 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
322 GNUNET_free_non_null (rc->hosts);
324 GNUNET_CONFIGURATION_destroy (rc->cfg);
325 GNUNET_free_non_null (rc->topo_file);
326 GNUNET_free_non_null (rc->trusted_ip);
332 * Stops the testbed run and releases any used resources
334 * @param cls the tesbed run handle
335 * @param tc the task context from scheduler
338 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 struct RunContext *rc = cls;
341 struct DLLOperation *dll_op;
344 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
345 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
346 GNUNET_assert (GNUNET_NO == rc->shutdown);
347 rc->shutdown = GNUNET_YES;
348 if (NULL != rc->hc_handles)
350 for (nhost = 0; nhost < rc->num_hosts; nhost++)
351 if (NULL != rc->hc_handles[nhost])
352 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
353 GNUNET_free (rc->hc_handles);
354 rc->hc_handles = NULL;
356 /* Stop register hosts task if it is running */
357 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
359 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
360 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
362 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
364 GNUNET_SCHEDULER_cancel (rc->timeout_task);
365 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
367 if (NULL != rc->reg_handle)
369 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
370 rc->reg_handle = NULL;
372 /* cancel any exiting operations */
373 if (NULL != rc->dll_op_head)
375 while (NULL != (dll_op = rc->dll_op_head))
377 GNUNET_TESTBED_operation_done (dll_op->op);
378 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
379 GNUNET_free (dll_op);
384 if (NULL != rc->peers)
386 if (NULL != rc->topology_operation)
388 GNUNET_TESTBED_operation_done (rc->topology_operation);
389 rc->topology_operation = NULL;
391 if (RC_INIT == rc->state)
392 rc->state = RC_READY; /* Even though we haven't called the master callback */
393 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
394 dll_op->op = GNUNET_TESTBED_shutdown_peers (rc->c, dll_op, NULL, NULL);
395 DEBUG ("Shutting down peers\n");
396 rc->pstart_time = GNUNET_TIME_absolute_get ();
397 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
402 rc->state = RC_PEERS_SHUTDOWN; /* No peers are present so we consider the
403 * state where all peers are SHUTDOWN */
404 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
409 * Function to shutdown now
411 * @param rc the RunContext
414 shutdown_now (struct RunContext *rc)
416 if (GNUNET_YES == rc->shutdown)
418 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
419 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
420 GNUNET_SCHEDULER_shutdown (); /* Trigger shutdown in programs using this API */
421 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
426 * Function to return the string representation of the duration between current
427 * time and `pstart_time' in `RunContext'
429 * @param rc the RunContext
430 * @return the representation string; this is NOT reentrant
433 prof_time (struct RunContext *rc)
435 struct GNUNET_TIME_Relative ptime;
437 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
438 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
443 * Task for starting peers
445 * @param cls the RunHandle
446 * @param tc the task context from scheduler
449 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
451 struct RunContext *rc = cls;
452 struct DLLOperation *dll_op;
455 DEBUG ("Starting Peers\n");
456 rc->pstart_time = GNUNET_TIME_absolute_get ();
457 for (peer = 0; peer < rc->num_peers; peer++)
459 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
460 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
461 dll_op->cls = rc->peers[peer];
462 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
469 * Functions of this signature are called when a peer has been successfully
472 * @param cls the closure from GNUNET_TESTBED_peer_create()
473 * @param peer the handle for the created peer; NULL on any error during
475 * @param emsg NULL if peer is not NULL; else MAY contain the error description
478 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
480 struct DLLOperation *dll_op = cls;
481 struct RunContext *rc;
483 GNUNET_assert (NULL != dll_op);
485 GNUNET_assert (NULL != rc);
486 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
487 GNUNET_TESTBED_operation_done (dll_op->op);
488 GNUNET_free (dll_op);
492 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
497 rc->peers[rc->peer_count] = peer;
499 if (rc->peer_count < rc->num_peers)
501 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
502 rc->state = RC_PEERS_CREATED;
503 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
508 * call test master callback
510 * @param rc the RunContext
513 call_master (struct RunContext *rc)
515 GNUNET_SCHEDULER_cancel (rc->timeout_task);
516 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
517 if (NULL != rc->test_master)
518 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers);
523 * Callbacks of this type are called when topology configuration is completed
525 * @param cls the operation closure given to
526 * GNUNET_TESTBED_overlay_configure_topology_va() and
527 * GNUNET_TESTBED_overlay_configure() calls
528 * @param nsuccess the number of successful overlay connects
529 * @param nfailures the number of overlay connects which failed
532 topology_completion_callback (void *cls, unsigned int nsuccess,
533 unsigned int nfailures)
535 struct RunContext *rc = cls;
537 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
538 GNUNET_TESTBED_operation_done (rc->topology_operation);
539 rc->topology_operation = NULL;
540 rc->state = RC_READY;
546 * Function to create peers
548 * @param rc the RunContext
551 create_peers (struct RunContext *rc)
553 struct DLLOperation *dll_op;
556 DEBUG ("Creating peers\n");
557 rc->pstart_time = GNUNET_TIME_absolute_get ();
559 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
560 GNUNET_assert (NULL != rc->c);
562 for (peer = 0; peer < rc->num_peers; peer++)
564 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
567 GNUNET_TESTBED_peer_create (rc->c,
569 rc->num_hosts) ? rc->h : rc->hosts[peer %
571 rc->cfg, peer_create_cb, dll_op);
572 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
578 * Signature of the event handler function called by the
579 * respective event controller.
582 * @param event information about the event
585 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
587 struct RunContext *rc = cls;
588 struct DLLOperation *dll_op;
590 if (RC_INIT == rc->state)
594 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
595 dll_op = event->op_cls;
596 if (NULL != event->details.operation_finished.emsg)
598 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
603 GNUNET_assert (event->op == dll_op->op);
604 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
605 GNUNET_TESTBED_operation_done (dll_op->op);
606 GNUNET_free (dll_op);
607 if (rc->reg_hosts == rc->num_hosts)
609 rc->state = RC_LINKED;
619 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
621 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
622 (event->op == dll_op->op))
627 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
628 GNUNET_TESTBED_operation_done (dll_op->op);
629 GNUNET_free (dll_op);
633 case RC_PEERS_CREATED:
635 rc->state = RC_PEERS_SHUTDOWN;
636 GNUNET_free_non_null (rc->peers);
638 DEBUG ("Peers shut down in %s\n", prof_time (rc));
639 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
647 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
648 rc->cc (rc->cc_cls, event);
649 if (GNUNET_TESTBED_ET_PEER_START != event->type)
651 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
652 if ((NULL != dll_op->cls) &&
653 (event->details.peer_start.peer == dll_op->cls))
655 if (NULL == dll_op) /* Not our operation */
657 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
658 GNUNET_TESTBED_operation_done (dll_op->op);
659 GNUNET_free (dll_op);
661 if (rc->peer_count < rc->num_peers)
663 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
664 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
666 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
667 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
668 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
670 rc->topology_operation =
671 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
672 rc->peers, &rc->num_oc,
673 &topology_completion_callback,
677 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
679 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
681 GNUNET_assert (NULL != rc->topo_file);
682 rc->topology_operation =
683 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
684 rc->peers, &rc->num_oc,
685 &topology_completion_callback,
689 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
692 rc->topology_operation =
693 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
694 rc->peers, &rc->num_oc,
695 &topology_completion_callback,
698 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
699 if (NULL == rc->topology_operation)
700 LOG (GNUNET_ERROR_TYPE_WARNING,
701 "Not generating topology. Check number of peers\n");
704 DEBUG ("Creating overlay topology\n");
705 rc->pstart_time = GNUNET_TIME_absolute_get ();
709 rc->state = RC_READY;
715 * Task to register all hosts available in the global host list
717 * @param cls the RunContext
718 * @param tc the scheduler task context
721 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
725 * Callback which will be called to after a host registration succeeded or failed
727 * @param cls the closure
728 * @param emsg the error message; NULL if host registration is successful
731 host_registration_completion (void *cls, const char *emsg)
733 struct RunContext *rc = cls;
735 rc->reg_handle = NULL;
738 LOG (GNUNET_ERROR_TYPE_WARNING,
739 _("Host registration failed for a host. Error: %s\n"), emsg);
743 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
748 * Task to register all hosts available in the global host list
750 * @param cls RunContext
751 * @param tc the scheduler task context
754 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
756 struct RunContext *rc = cls;
757 struct DLLOperation *dll_op;
760 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
761 if (rc->reg_hosts == rc->num_hosts)
763 DEBUG ("All hosts successfully registered\n");
765 for (slave = 0; slave < rc->num_hosts; slave++)
767 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
770 GNUNET_TESTBED_controller_link (dll_op, rc->c, rc->hosts[slave],
771 rc->h, rc->cfg, GNUNET_YES);
772 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
779 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
780 host_registration_completion, rc);
786 * Callback to signal successfull startup of the controller process
788 * @param cls the closure from GNUNET_TESTBED_controller_start()
789 * @param cfg the configuration with which the controller has been started;
790 * NULL if status is not GNUNET_OK
791 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
792 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
795 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
798 struct RunContext *rc = cls;
801 if (status != GNUNET_OK)
803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
804 _("Controller crash detected. Shutting down.\n"));
806 if (NULL != rc->peers)
808 GNUNET_free (rc->peers);
814 GNUNET_CONFIGURATION_destroy (rc->cfg);
815 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
816 event_mask = rc->event_mask;
817 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
818 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
819 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
820 event_mask |= GNUNET_TESTBED_ET_CONNECT;
822 GNUNET_TESTBED_controller_connect (rc->cfg, rc->h, event_mask, &event_cb,
824 if (0 < rc->num_hosts)
827 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
830 rc->state = RC_LINKED;
836 * Callback function invoked for each interface found.
839 * @param name name of the interface (can be NULL for unknown)
840 * @param isDefault is this presumably the default interface
841 * @param addr address of this interface (can be NULL for unknown or unassigned)
842 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
843 * @param netmask the network mask (can be NULL for unknown or unassigned))
844 * @param addrlen length of the address
845 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
848 netint_proc (void *cls, const char *name, int isDefault,
849 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
850 const struct sockaddr *netmask, socklen_t addrlen)
852 struct RunContext *rc = cls;
853 char hostip[NI_MAXHOST];
856 if (sizeof (struct sockaddr_in) != addrlen)
857 return GNUNET_OK; /* Only consider IPv4 for now */
859 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
860 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
861 if (NULL == rc->trusted_ip)
863 rc->trusted_ip = GNUNET_strdup (hostip);
866 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
867 GNUNET_free (rc->trusted_ip);
868 rc->trusted_ip = buf;
874 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
875 * inform whether the given host is habitable or not. The Handle returned by
876 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
879 * @param host the host whose status is being reported; will be NULL if the host
880 * given to GNUNET_TESTBED_is_host_habitable() is NULL
881 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
884 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
887 struct RunContext *rc = cls;
888 struct GNUNET_TESTBED_Host **old_hosts;
891 for (nhost = 0; nhost < rc->num_hosts; nhost++)
893 if (host == rc->hosts[nhost])
896 GNUNET_assert (nhost != rc->num_hosts);
897 rc->hc_handles[nhost] = NULL;
898 if (GNUNET_NO == status)
900 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
901 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
902 GNUNET_TESTBED_host_get_hostname (host));
904 LOG (GNUNET_ERROR_TYPE_ERROR,
905 _("Testbed cannot be started on localhost\n"));
910 if (rc->reg_hosts < rc->num_hosts)
912 GNUNET_free (rc->hc_handles);
913 rc->hc_handles = NULL;
914 rc->h = rc->hosts[0];
916 if (0 < rc->num_hosts)
918 old_hosts = rc->hosts;
920 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
921 memcpy (rc->hosts, &old_hosts[1],
922 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
923 GNUNET_free (old_hosts);
927 GNUNET_free (rc->hosts);
930 GNUNET_OS_network_interfaces_list (netint_proc, rc);
931 if (NULL == rc->trusted_ip)
932 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
934 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h, rc->cfg,
935 &controller_status_cb, rc);
936 GNUNET_free (rc->trusted_ip);
937 rc->trusted_ip = NULL;
938 if (NULL == rc->cproc)
940 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
947 * Task run upon timeout while setting up the testbed
949 * @param cls the RunContext
950 * @param tc the task context
953 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
955 struct RunContext *rc = cls;
957 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
958 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
960 if (NULL != rc->test_master)
961 rc->test_master (rc->test_master_cls, 0, NULL);
962 rc->test_master = NULL;
967 * Convenience method for running a testbed with
968 * a single call. Underlay and overlay topology
969 * are configured using the "UNDERLAY" and "OVERLAY"
970 * options in the "[testbed]" section of the configuration\
971 * (with possible options given in "UNDERLAY_XXX" and/or
974 * The testbed is to be terminated using a call to
975 * "GNUNET_SCHEDULER_shutdown".
977 * @param host_filename name of the file with the 'hosts', NULL
978 * to run everything on 'localhost'
979 * @param cfg configuration to use (for testbed, controller and peers)
980 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
981 * @param event_mask bit mask with set of events to call 'cc' for;
982 * or-ed values of "1LL" shifted by the
983 * respective 'enum GNUNET_TESTBED_EventType'
984 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
985 * @param cc controller callback to invoke on events; This callback is called
986 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
987 * set in the event_mask as this is the only way get access to the
988 * handle of each peer
989 * @param cc_cls closure for cc
990 * @param test_master this callback will be called once the test is ready
991 * @param test_master_cls closure for 'test_master'.
994 GNUNET_TESTBED_run (const char *host_filename,
995 const struct GNUNET_CONFIGURATION_Handle *cfg,
996 unsigned int num_peers, uint64_t event_mask,
997 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
998 GNUNET_TESTBED_TestMaster test_master,
999 void *test_master_cls)
1001 struct RunContext *rc;
1003 struct GNUNET_TIME_Relative timeout;
1004 unsigned long long random_links;
1008 GNUNET_assert (num_peers > 0);
1009 rc = GNUNET_malloc (sizeof (struct RunContext));
1010 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1012 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1014 if (0 == rc->num_hosts)
1016 LOG (GNUNET_ERROR_TYPE_WARNING,
1017 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1021 if (NULL != host_filename)
1024 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1026 if (0 == rc->num_hosts)
1028 LOG (GNUNET_ERROR_TYPE_WARNING,
1029 _("No hosts loaded. Need at least one host\n"));
1034 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1036 rc->num_peers = num_peers;
1037 rc->event_mask = event_mask;
1039 rc->cc_cls = cc_cls;
1040 rc->test_master = test_master;
1041 rc->test_master_cls = test_master_cls;
1042 rc->state = RC_INIT;
1043 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1045 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1046 "OVERLAY_TOPOLOGY", &topology))
1048 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1050 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1053 ("Specified topology must be supported by testbed"));
1055 GNUNET_free (topology);
1057 switch (rc->topology)
1059 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1060 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1061 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1063 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1064 "OVERLAY_RANDOM_LINKS",
1067 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1068 * option to be set to the number of random links to be established */
1069 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1070 "OVERLAY_RANDOM_LINKS");
1073 if (random_links > UINT32_MAX)
1075 GNUNET_break (0); /* Too big number */
1078 rc->random_links = (unsigned int) random_links;
1080 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1082 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1083 "OVERLAY_TOPOLOGY_FILE",
1086 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1087 "OVERLAY_TOPOLOGY_FILE");
1091 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1093 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1094 "OVERLAY_RANDOM_LINKS"))
1095 LOG (GNUNET_ERROR_TYPE_WARNING,
1096 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1099 if (0 != rc->num_hosts)
1102 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle *)
1104 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1107 (rc->hc_handles[nhost] =
1108 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1109 &host_habitable_cb, rc)))
1112 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1113 if (NULL != rc->hc_handles[nhost])
1114 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1115 GNUNET_free (rc->hc_handles);
1116 rc->hc_handles = NULL;
1123 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
1124 &controller_status_cb, rc);
1125 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1129 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1130 DEFAULT_SETUP_TIMEOUT);
1133 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1134 rc->shutdown_run_task =
1135 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_run,
1141 GNUNET_TESTBED_host_destroy (rc->h);
1142 if (NULL != rc->hosts)
1144 for (hid = 0; hid < rc->num_hosts; hid++)
1145 if (NULL != rc->hosts[hid])
1146 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1147 GNUNET_free (rc->hosts);
1149 if (NULL != rc->cfg)
1150 GNUNET_CONFIGURATION_destroy (rc->cfg);
1155 /* end of testbed_api_testbed.c */