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_testbed_service.h"
30 #include "testbed_api_peers.h"
31 #include "testbed_api_hosts.h"
32 #include "testbed_api_topology.h"
35 * Generic loggins shorthand
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
41 * Opaque handle to an abstract operation to be executed by the testing framework.
43 struct GNUNET_TESTBED_Testbed
48 struct GNUNET_TESTBED_Host **hosts;
51 * The number of hosts in the hosts array
53 unsigned int num_hosts;
56 * The controller handle
58 struct GNUNET_TESTBED_Controller *c;
68 * The testbed operation handle
70 struct GNUNET_TESTBED_Operation *op;
73 * Context information for GNUNET_TESTBED_run()
75 struct RunContext *rc;
83 * The next pointer for DLL
85 struct DLLOperation *next;
88 * The prev pointer for DLL
90 struct DLLOperation *prev;
95 * States of RunContext
105 * Controllers on given hosts started and linked
110 * The testbed run is ready and the master callback can be called now. At this
111 * time the peers are all started and if a topology is provided in the
112 * configuration the topology would have been attempted
122 * Peers are 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 * Master task to call when testbed initialization is done
168 GNUNET_SCHEDULER_Task master;
171 * The closure for the master task
176 * The head element of DLL operations
178 struct DLLOperation *dll_op_head;
181 * The tail element of DLL operations
183 struct DLLOperation *dll_op_tail;
186 * An array of hosts loaded from the hostkeys file
188 struct GNUNET_TESTBED_Host **hosts;
191 * The handle for whether a host is habitable or not
193 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
196 * Array of peers which we create
198 struct GNUNET_TESTBED_Peer **peers;
201 * The topology generation operation. Will be null if no topology is set in
204 struct GNUNET_TESTBED_Operation *topology_operation;
207 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
212 * Host registration handle
214 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
217 * Host registration task
219 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
222 * Task to be run while shutting down
224 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
227 * The event mask for the controller
232 * State of this context
237 * The topology which has to be achieved with the peers started in this context
239 enum GNUNET_TESTBED_TopologyOption topology;
242 * Number of hosts in the given host file
244 unsigned int num_hosts;
247 * Number of registered hosts. Also used as a counter while checking
248 * habitabillity of hosts
250 unsigned int reg_hosts;
253 * Current peer count for an operation; Set this to 0 and increment for each
254 * successful operation on a peer
256 unsigned int peer_count;
259 * number of peers to start
261 unsigned int num_peers;
264 * counter to count overlay connect attempts. This counter includes both
265 * successful and failed overlay connects
267 unsigned int oc_count;
270 * Expected overlay connects. Should be zero if no topology is relavant
275 * Number of random links to established
277 unsigned int random_links;
283 * Task for starting peers
285 * @param cls the RunHandle
286 * @param tc the task context from scheduler
289 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
291 struct RunContext *rc = cls;
292 struct DLLOperation *dll_op;
295 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting Peers\n");
296 for (peer = 0; peer < rc->num_peers; peer++)
298 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
299 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
300 dll_op->cls = rc->peers[peer];
301 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
308 * Functions of this signature are called when a peer has been successfully
311 * @param cls the closure from GNUNET_TESTBED_peer_create()
312 * @param peer the handle for the created peer; NULL on any error during
314 * @param emsg NULL if peer is not NULL; else MAY contain the error description
317 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
319 struct DLLOperation *dll_op = cls;
320 struct RunContext *rc;
322 GNUNET_assert (NULL != dll_op);
324 GNUNET_assert (NULL != rc);
325 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
326 GNUNET_TESTBED_operation_done (dll_op->op);
327 GNUNET_free (dll_op);
331 LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
333 /* FIXME: GNUNET_TESTBED_shutdown_run()? */
336 rc->peers[rc->peer_count] = peer;
338 if (rc->peer_count < rc->num_peers)
340 LOG (GNUNET_ERROR_TYPE_DEBUG, "Required peers created successfully\n");
341 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
346 * Assuming all peers have been destroyed cleanup run handle
348 * @param cls the run handle
349 * @param tc the task context from scheduler
352 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
354 struct RunContext *rc = cls;
355 struct DLLOperation *dll_op;
358 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
359 GNUNET_assert (NULL == rc->reg_handle);
360 GNUNET_assert (NULL == rc->peers);
361 GNUNET_assert (NULL == rc->hc_handles);
362 GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
364 GNUNET_TESTBED_controller_disconnect (rc->c);
365 if (NULL != rc->cproc)
366 GNUNET_TESTBED_controller_stop (rc->cproc);
368 GNUNET_TESTBED_host_destroy (rc->h);
369 for (hid = 0; hid < rc->num_hosts; hid++)
370 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
371 GNUNET_free_non_null (rc->hosts);
372 if (NULL != rc->dll_op_head)
374 LOG (GNUNET_ERROR_TYPE_WARNING,
375 _("Some operations are still pending. Cancelling them\n"));
376 while (NULL != (dll_op = rc->dll_op_head))
378 GNUNET_TESTBED_operation_done (dll_op->op);
379 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
380 GNUNET_free (dll_op);
384 GNUNET_CONFIGURATION_destroy (rc->cfg);
385 GNUNET_free_non_null (rc->topo_file);
391 * Stops the testbed run and releases any used resources
393 * @param cls the tesbed run handle
394 * @param tc the task context from scheduler
397 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
399 struct RunContext *rc = cls;
400 struct DLLOperation *dll_op;
404 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
405 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
406 if (NULL != rc->hc_handles)
408 for (nhost = 0; nhost < rc->num_hosts; nhost++)
409 if (NULL != rc->hc_handles[nhost])
410 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
411 GNUNET_free (rc->hc_handles);
412 rc->hc_handles = NULL;
414 /* Stop register hosts task if it is running */
415 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
417 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
418 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
420 if (NULL != rc->reg_handle)
422 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
423 rc->reg_handle = NULL;
427 if (NULL != rc->peers)
429 if (NULL != rc->topology_operation)
431 GNUNET_TESTBED_operation_done (rc->topology_operation);
432 rc->topology_operation = NULL;
434 if (RC_INIT == rc->state)
435 rc->state = RC_READY; /* Even though we haven't called the master callback */
437 /* Check if some peers are stopped */
438 for (peer = 0; peer < rc->num_peers; peer++)
440 if (PS_STOPPED != rc->peers[peer]->state)
443 if (peer == rc->num_peers)
445 /* All peers are stopped */
446 rc->state = RC_PEERS_STOPPED;
447 for (peer = 0; peer < rc->num_peers; peer++)
449 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
450 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
451 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
456 /* Some peers are stopped */
457 for (peer = 0; peer < rc->num_peers; peer++)
459 if (PS_STARTED != rc->peers[peer]->state)
464 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
465 dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
466 dll_op->cls = rc->peers[peer];
467 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
470 if (rc->peer_count != rc->num_peers)
474 rc->state = RC_PEERS_DESTROYED; /* No peers are present so we consider the
475 * state where all peers are destroyed */
476 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
481 * Task to call master task
483 * @param cls the run context
484 * @param tc the task context
487 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
489 struct RunContext *rc = cls;
491 if (NULL != rc->topology_operation)
493 GNUNET_TESTBED_operation_done (rc->topology_operation);
494 rc->topology_operation = NULL;
496 if (NULL != rc->master)
497 GNUNET_SCHEDULER_add_continuation (rc->master, rc->master_cls,
498 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
503 * Function to create peers
505 * @param rc the RunContext
508 create_peers (struct RunContext *rc)
510 struct DLLOperation *dll_op;
514 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
515 GNUNET_assert (NULL != rc->c);
517 for (peer = 0; peer < rc->num_peers; peer++)
519 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
522 GNUNET_TESTBED_peer_create (rc->c,
524 ? rc->h : rc->hosts[peer % rc->num_hosts],
526 peer_create_cb, dll_op);
527 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
533 * Signature of the event handler function called by the
534 * respective event controller.
537 * @param event information about the event
540 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
542 struct RunContext *rc = cls;
543 struct DLLOperation *dll_op;
544 unsigned int peer_id;
546 if (RC_INIT == rc->state)
550 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
551 dll_op = event->details.operation_finished.op_cls;
552 if (NULL != event->details.operation_finished.emsg)
554 LOG (GNUNET_ERROR_TYPE_ERROR,
555 _("Linking controllers failed. Exiting"));
556 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
557 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
561 GNUNET_assert (event->details.operation_finished.operation == dll_op->op);
562 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
563 GNUNET_TESTBED_operation_done (dll_op->op);
564 GNUNET_free (dll_op);
565 if (rc->reg_hosts == rc->num_hosts)
567 rc->state = RC_LINKED;
573 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
574 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
578 if (NULL != rc->topology_operation)
582 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
583 case GNUNET_TESTBED_ET_CONNECT:
588 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
589 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
592 if (rc->oc_count == rc->num_oc)
594 rc->state = RC_READY;
595 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
596 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
600 if ((RC_LINKED < rc->state) &&
601 ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) ||
602 (GNUNET_TESTBED_ET_PEER_STOP == event->type)))
604 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
606 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
607 (event->details.operation_finished.operation == dll_op->op))
609 if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
610 (event->details.peer_stop.peer == dll_op->cls))
615 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
616 GNUNET_TESTBED_operation_done (dll_op->op);
617 GNUNET_free (dll_op);
619 if (rc->peer_count < rc->num_peers)
624 rc->state = RC_PEERS_STOPPED;
626 for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
628 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
629 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
630 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
634 case RC_PEERS_STOPPED:
635 rc->state = RC_PEERS_DESTROYED;
636 GNUNET_free (rc->peers);
638 LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
639 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
648 if ((0 != (rc->event_mask && (1LL << event->type))) && (NULL != rc->cc))
649 rc->cc (rc->cc_cls, event);
650 if (GNUNET_TESTBED_ET_PEER_START != event->type)
652 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
653 if ((NULL != dll_op->cls) &&
654 (event->details.peer_start.peer == dll_op->cls))
656 GNUNET_assert (NULL != dll_op);
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 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
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,
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,
689 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
692 rc->topology_operation =
693 GNUNET_TESTBED_overlay_configure_topology (NULL,
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");
705 rc->state = RC_READY;
706 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
707 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
712 * Task to register all hosts available in the global host list
714 * @param cls the RunContext
715 * @param tc the scheduler task context
718 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
722 * Callback which will be called to after a host registration succeeded or failed
724 * @param cls the closure
725 * @param emsg the error message; NULL if host registration is successful
728 host_registration_completion (void *cls, const char *emsg)
730 struct RunContext *rc = cls;
732 rc->reg_handle = NULL;
735 LOG (GNUNET_ERROR_TYPE_WARNING,
736 _("Host registration failed for a host. Error: %s\n"), emsg);
737 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
738 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
741 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
746 * Task to register all hosts available in the global host list
748 * @param cls RunContext
749 * @param tc the scheduler task context
752 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
754 struct RunContext *rc = cls;
755 struct DLLOperation *dll_op;
758 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
759 if (rc->reg_hosts == rc->num_hosts)
761 LOG (GNUNET_ERROR_TYPE_DEBUG,
762 "All hosts successfully registered\n");
764 for (slave = 0; slave < rc->num_hosts; slave++)
766 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
768 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
774 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
779 rc->reg_handle = GNUNET_TESTBED_register_host (rc->c,
780 rc->hosts[rc->reg_hosts++],
781 host_registration_completion,
787 * Callback to signal successfull startup of the controller process
789 * @param cls the closure from GNUNET_TESTBED_controller_start()
790 * @param cfg the configuration with which the controller has been started;
791 * NULL if status is not GNUNET_OK
792 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
793 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
796 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
799 struct RunContext *rc = cls;
802 if (status != GNUNET_OK)
804 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
807 GNUNET_CONFIGURATION_destroy (rc->cfg);
808 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
809 event_mask = rc->event_mask;
810 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
811 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
812 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
813 event_mask |= GNUNET_TESTBED_ET_CONNECT;
815 GNUNET_TESTBED_controller_connect (rc->cfg, rc->h, event_mask, &event_cb, rc);
816 if (0 < rc->num_hosts)
818 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
821 rc->state = RC_LINKED;
827 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
828 * inform whether the given host is habitable or not. The Handle returned by
829 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
832 * @param host the host whose status is being reported; will be NULL if the host
833 * given to GNUNET_TESTBED_is_host_habitable() is NULL
834 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
837 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
839 struct RunContext *rc = cls;
842 for (nhost = 0; nhost < rc->num_hosts; nhost++)
844 if (host == rc->hosts[nhost])
847 GNUNET_assert (nhost != rc->num_hosts);
848 rc->hc_handles[nhost] = NULL;
849 if (GNUNET_NO == status)
851 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
852 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
853 GNUNET_TESTBED_host_get_hostname (host));
855 LOG (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
856 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
857 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
861 if (rc->reg_hosts < rc->num_hosts)
863 GNUNET_free (rc->hc_handles);
864 rc->hc_handles = NULL;
865 rc->h = rc->hosts[0];
867 if (0 < rc->num_hosts)
868 rc->hosts = &rc->hosts[1];
871 GNUNET_free (rc->hosts);
874 /* FIXME: If we are starting controller on different host 127.0.0.1 may not ab
877 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
878 &controller_status_cb, rc);
879 if (NULL == rc->cproc)
881 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
882 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
883 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
889 * Convenience method for running a testbed with
890 * a single call. Underlay and overlay topology
891 * are configured using the "UNDERLAY" and "OVERLAY"
892 * options in the "[testbed]" section of the configuration\
893 * (with possible options given in "UNDERLAY_XXX" and/or
896 * The testbed is to be terminated using a call to
897 * "GNUNET_SCHEDULER_shutdown".
899 * @param host_filename name of the file with the 'hosts', NULL
900 * to run everything on 'localhost'
901 * @param cfg configuration to use (for testbed, controller and peers)
902 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
903 * @param event_mask bit mask with set of events to call 'cc' for;
904 * or-ed values of "1LL" shifted by the
905 * respective 'enum GNUNET_TESTBED_EventType'
906 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
907 * @param cc controller callback to invoke on events; This callback is called
908 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
909 * set in the event_mask as this is the only way get access to the
910 * handle of each peer
911 * @param cc_cls closure for cc
912 * @param master task to run once the testbed is ready
916 GNUNET_TESTBED_run (const char *host_filename,
917 const struct GNUNET_CONFIGURATION_Handle *cfg,
918 unsigned int num_peers, uint64_t event_mask,
919 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
920 GNUNET_SCHEDULER_Task master, void *master_cls)
922 struct RunContext *rc;
924 unsigned long long random_links;
928 GNUNET_assert (NULL != cc);
929 GNUNET_assert (num_peers > 0);
930 host_filename = NULL;
931 rc = GNUNET_malloc (sizeof (struct RunContext));
932 if (NULL != host_filename)
935 GNUNET_TESTBED_hosts_load_from_file (host_filename, &rc->hosts);
936 if (0 == rc->num_hosts)
938 LOG (GNUNET_ERROR_TYPE_WARNING,
939 _("No hosts loaded. Need at least one host\n"));
944 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, 0);
945 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
946 rc->num_peers = num_peers;
947 rc->event_mask = event_mask;
948 rc->event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
952 rc->master_cls = master_cls;
954 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
955 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
959 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology,
962 LOG (GNUNET_ERROR_TYPE_WARNING,
963 "Unknown topology %s given in configuration\n", topology);
965 GNUNET_free (topology);
967 switch (rc->topology)
969 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
970 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
971 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
972 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
973 "OVERLAY_RANDOM_LINKS",
976 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
977 option to be set to the number of random links to be established */
981 if (random_links > UINT32_MAX)
983 GNUNET_break (0); /* Too big number */
986 rc->random_links = (unsigned int) random_links;
988 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
989 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
993 /* You need to set TOPOLOGY_FILE option to a topolog file */
1002 if (NULL != host_filename)
1004 rc->hc_handles = GNUNET_malloc (sizeof (struct
1005 GNUNET_TESTBED_HostHabitableCheckHandle *)
1007 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1009 if (NULL == (rc->hc_handles[nhost] =
1010 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1015 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1016 if (NULL != rc->hc_handles[nhost])
1017 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1018 GNUNET_free (rc->hc_handles);
1019 rc->hc_handles = NULL;
1026 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, rc->cfg,
1027 &controller_status_cb, rc);
1028 rc->shutdown_run_task =
1029 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1035 GNUNET_TESTBED_host_destroy (rc->h);
1036 if (NULL != rc->hosts)
1038 for (hid = 0; hid < rc->num_hosts; hid++)
1039 if (NULL != rc->hosts[hid])
1040 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1041 GNUNET_free (rc->hosts);
1048 * Configure and run a testbed using the given
1049 * master controller on 'num_hosts' starting
1050 * 'num_peers' using the given peer configuration.
1052 * @param controller master controller for the testbed
1053 * (must not be destroyed until after the
1054 * testbed is destroyed).
1055 * @param num_hosts number of hosts in 'hosts', 0 to only
1057 * @param hosts list of hosts to use for the testbed
1058 * @param num_peers number of peers to start
1059 * @param cfg the configuration to use as a template for peers and also for
1060 * checking the value of testbed helper binary
1061 * @param underlay_topology underlay topology to create
1062 * @param va topology-specific options
1063 * @return handle to the testbed; NULL upon error (error messaage will be printed)
1065 struct GNUNET_TESTBED_Testbed *
1066 GNUNET_TESTBED_create_va (struct GNUNET_TESTBED_Controller *controller,
1067 unsigned int num_hosts,
1068 struct GNUNET_TESTBED_Host **hosts,
1069 unsigned int num_peers,
1070 const struct GNUNET_CONFIGURATION_Handle *cfg,
1071 enum GNUNET_TESTBED_TopologyOption underlay_topology,
1074 /* unsigned int nhost; */
1076 /* GNUNET_assert (underlay_topology < GNUNET_TESTBED_TOPOLOGY_NONE); */
1077 /* if (num_hosts != 0) */
1079 /* for (nhost = 0; nhost < num_hosts; nhost++) */
1081 /* if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost], cfg)) */
1083 /* LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"), */
1084 /* GNUNET_TESTBED_host_get_hostname_ (hosts[nhost])); */
1088 /* if (num_hosts != nhost) */
1091 /* We need controller callback here to get operation done events while
1099 * Configure and run a testbed using the given
1100 * master controller on 'num_hosts' starting
1101 * 'num_peers' using the given peer configuration.
1103 * @param controller master controller for the testbed
1104 * (must not be destroyed until after the
1105 * testbed is destroyed).
1106 * @param num_hosts number of hosts in 'hosts', 0 to only
1108 * @param hosts list of hosts to use for the testbed
1109 * @param num_peers number of peers to start
1110 * @param cfg the configuration to use as a template for peers and also for
1111 * checking the value of testbed helper binary
1112 * @param underlay_topology underlay topology to create
1113 * @param ... topology-specific options
1115 struct GNUNET_TESTBED_Testbed *
1116 GNUNET_TESTBED_create (struct GNUNET_TESTBED_Controller *controller,
1117 unsigned int num_hosts,
1118 struct GNUNET_TESTBED_Host **hosts,
1119 unsigned int num_peers,
1120 const struct GNUNET_CONFIGURATION_Handle *cfg,
1121 enum GNUNET_TESTBED_TopologyOption underlay_topology,
1124 struct GNUNET_TESTBED_Testbed *testbed;
1127 va_start (vargs, underlay_topology);
1128 testbed = GNUNET_TESTBED_create_va (controller, num_hosts, hosts, num_peers,
1129 cfg, underlay_topology, vargs);
1136 * Destroy a testbed. Stops all running peers and then
1137 * destroys all peers. Does NOT destroy the master controller.
1139 * @param testbed testbed to destroy
1142 GNUNET_TESTBED_destroy (struct GNUNET_TESTBED_Testbed *testbed)
1148 /* end of testbed_api_testbed.c */