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 * 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
117 * Peers are destroyed
129 * The controller handle
131 struct GNUNET_TESTBED_Controller *c;
134 * Handle to the host on which the controller runs
136 struct GNUNET_TESTBED_Host *h;
139 * The handle to the controller process
141 struct GNUNET_TESTBED_ControllerProc *cproc;
144 * The callback to use as controller callback
146 GNUNET_TESTBED_ControllerCallback cc;
149 * The pointer to the controller callback
154 * Master task to call when testbed initialization is done
156 GNUNET_SCHEDULER_Task master;
159 * The closure for the master task
164 * The head element of DLL operations
166 struct DLLOperation *dll_op_head;
169 * The tail element of DLL operations
171 struct DLLOperation *dll_op_tail;
174 * Array of peers which we create
176 struct GNUNET_TESTBED_Peer **peers;
179 * The topology generation operation. Will be null if no topology is set in
182 struct GNUNET_TESTBED_Operation *topology_operation;
185 * The event mask for the controller
190 * State of this context
195 * The topology which has to be achieved with the peers started in this context
197 enum GNUNET_TESTBED_TopologyOption topology;
200 * Current peer count for an operation; Set this to 0 and increment for each
201 * successful operation on a peer
203 unsigned int peer_count;
206 * number of peers to start
208 unsigned int num_peers;
211 * counter to count overlay connect attempts. This counter includes both
212 * successful and failed overlay connects
214 unsigned int oc_count;
217 * Expected overlay connects. Should be zero if no topology is relavant
225 * Task for starting peers
227 * @param cls the RunHandle
228 * @param tc the task context from scheduler
231 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
233 struct RunContext *rc = cls;
234 struct DLLOperation *dll_op;
237 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting Peers\n");
238 for (peer = 0; peer < rc->num_peers; peer++)
240 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
241 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
242 dll_op->cls = rc->peers[peer];
243 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
250 * Functions of this signature are called when a peer has been successfully
253 * @param cls the closure from GNUNET_TESTBED_peer_create()
254 * @param peer the handle for the created peer; NULL on any error during
256 * @param emsg NULL if peer is not NULL; else MAY contain the error description
259 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
261 struct DLLOperation *dll_op = cls;
262 struct RunContext *rc;
264 GNUNET_assert (NULL != dll_op);
266 GNUNET_assert (NULL != rc);
267 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
268 GNUNET_TESTBED_operation_done (dll_op->op);
269 GNUNET_free (dll_op);
273 LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
275 /* FIXME: GNUNET_TESTBED_shutdown_run()? */
278 rc->peers[rc->peer_count] = peer;
280 if (rc->peer_count < rc->num_peers)
282 LOG (GNUNET_ERROR_TYPE_DEBUG, "Required peers created successfully\n");
283 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
288 * Assuming all peers have been destroyed cleanup run handle
290 * @param cls the run handle
291 * @param tc the task context from scheduler
294 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
296 struct RunContext *rc = cls;
297 struct DLLOperation *dll_op;
299 GNUNET_assert (NULL == rc->peers);
300 GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
302 GNUNET_TESTBED_controller_disconnect (rc->c);
303 if (NULL != rc->cproc)
304 GNUNET_TESTBED_controller_stop (rc->cproc);
306 GNUNET_TESTBED_host_destroy (rc->h);
307 if (NULL != rc->dll_op_head)
309 LOG (GNUNET_ERROR_TYPE_WARNING,
310 _("Some operations are still pending. Cancelling them\n"));
311 while (NULL != (dll_op = rc->dll_op_head))
313 GNUNET_TESTBED_operation_done (dll_op->op);
314 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
315 GNUNET_free (dll_op);
323 * Task to call master task
325 * @param cls the run context
326 * @param tc the task context
329 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
331 struct RunContext *rc = cls;
333 if (NULL != rc->topology_operation)
335 GNUNET_TESTBED_operation_done (rc->topology_operation);
336 rc->topology_operation = NULL;
338 if (NULL != rc->master)
339 GNUNET_SCHEDULER_add_continuation (rc->master, rc->master_cls,
340 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
345 * Signature of the event handler function called by the
346 * respective event controller.
349 * @param event information about the event
352 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
354 struct RunContext *rc = cls;
355 struct DLLOperation *dll_op;
356 unsigned int peer_id;
358 if (NULL != rc->topology_operation)
362 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
363 case GNUNET_TESTBED_ET_CONNECT:
369 if (rc->oc_count == rc->num_oc)
371 rc->state = RC_READY;
372 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
373 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
377 if ((RC_INIT != rc->state) &&
378 ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) ||
379 (GNUNET_TESTBED_ET_PEER_STOP == event->type)))
381 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
383 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
384 (event->details.operation_finished.operation == dll_op->op))
386 if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
387 (event->details.peer_stop.peer == dll_op->cls))
392 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
393 GNUNET_TESTBED_operation_done (dll_op->op);
394 GNUNET_free (dll_op);
396 if (rc->peer_count < rc->num_peers)
401 rc->state = RC_PEERS_STOPPED;
403 for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
405 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
406 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
407 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
411 case RC_PEERS_STOPPED:
412 rc->state = RC_PEERS_DESTROYED;
413 GNUNET_free (rc->peers);
415 LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
416 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
425 if ((0 != (rc->event_mask && (1LL << event->type))) && (NULL != rc->cc))
426 rc->cc (rc->cc_cls, event);
427 if (GNUNET_TESTBED_ET_PEER_START != event->type)
429 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
430 if ((NULL != dll_op->cls) &&
431 (event->details.peer_start.peer == dll_op->cls))
433 GNUNET_assert (NULL != dll_op);
434 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
435 GNUNET_TESTBED_operation_done (dll_op->op);
436 GNUNET_free (dll_op);
438 if (rc->peer_count < rc->num_peers)
440 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
441 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
443 if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
444 || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology))
445 rc->topology_operation =
446 GNUNET_TESTBED_overlay_configure_topology (NULL,
450 (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI
453 (rc->num_oc - rc->num_peers),
454 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
456 rc->topology_operation =
457 GNUNET_TESTBED_overlay_configure_topology (NULL,
461 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
462 if (NULL == rc->topology_operation)
463 LOG (GNUNET_ERROR_TYPE_WARNING,
464 "Not generating topology. Check number of peers\n");
468 rc->state = RC_READY;
469 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
470 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
476 * Callback to signal successfull startup of the controller process
478 * @param cls the closure from GNUNET_TESTBED_controller_start()
479 * @param cfg the configuration with which the controller has been started;
480 * NULL if status is not GNUNET_OK
481 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
482 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
485 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
488 struct RunContext *rc = cls;
489 struct DLLOperation *dll_op;
493 if (status != GNUNET_OK)
495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
498 event_mask = rc->event_mask;
499 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
500 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
501 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
502 event_mask |= GNUNET_TESTBED_ET_CONNECT;
504 GNUNET_TESTBED_controller_connect (cfg, rc->h, event_mask, &event_cb, rc);
506 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
507 GNUNET_assert (NULL != rc->c);
509 for (peer = 0; peer < rc->num_peers; peer++)
511 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
514 GNUNET_TESTBED_peer_create (rc->c, rc->h, cfg, peer_create_cb, dll_op);
515 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
521 * Stops the testbed run and releases any used resources
523 * @param cls the tesbed run handle
524 * @param tc the task context from scheduler
527 shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
529 struct RunContext *rc = cls;
530 struct DLLOperation *dll_op;
535 if (NULL != rc->peers)
537 if (NULL != rc->topology_operation)
539 GNUNET_TESTBED_operation_done (rc->topology_operation);
540 rc->topology_operation = NULL;
542 if (RC_INIT == rc->state)
543 rc->state = RC_READY; /* Even though we haven't called the master callback */
545 /* Check if some peers are stopped */
546 for (peer = 0; peer < rc->num_peers; peer++)
548 if (PS_STOPPED != rc->peers[peer]->state)
551 if (peer == rc->num_peers)
553 /* All peers are stopped */
554 rc->state = RC_PEERS_STOPPED;
555 for (peer = 0; peer < rc->num_peers; peer++)
557 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
558 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
559 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
564 /* Some peers are stopped */
565 for (peer = 0; peer < rc->num_peers; peer++)
567 if (PS_STARTED != rc->peers[peer]->state)
572 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
573 dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
574 dll_op->cls = rc->peers[peer];
575 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
578 if (rc->peer_count != rc->num_peers)
582 rc->state = RC_PEERS_DESTROYED; /* No peers are present so we consider the
583 * state where all peers are destroyed */
584 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
589 * Convenience method for running a testbed with
590 * a single call. Underlay and overlay topology
591 * are configured using the "UNDERLAY" and "OVERLAY"
592 * options in the "[testbed]" section of the configuration\
593 * (with possible options given in "UNDERLAY_XXX" and/or
596 * The testbed is to be terminated using a call to
597 * "GNUNET_SCHEDULER_shutdown".
599 * @param host_filename name of the file with the 'hosts', NULL
600 * to run everything on 'localhost'
601 * @param cfg configuration to use (for testbed, controller and peers)
602 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
603 * @param event_mask bit mask with set of events to call 'cc' for;
604 * or-ed values of "1LL" shifted by the
605 * respective 'enum GNUNET_TESTBED_EventType'
606 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
607 * @param cc controller callback to invoke on events; This callback is called
608 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
609 * set in the event_mask as this is the only way get access to the
610 * handle of each peer
611 * @param cc_cls closure for cc
612 * @param master task to run once the testbed is ready
616 GNUNET_TESTBED_run (const char *host_filename,
617 const struct GNUNET_CONFIGURATION_Handle *cfg,
618 unsigned int num_peers, uint64_t event_mask,
619 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
620 GNUNET_SCHEDULER_Task master, void *master_cls)
622 struct RunContext *rc;
624 unsigned long long random_links;
626 GNUNET_break (NULL == host_filename); /* Currently we do not support host
628 GNUNET_assert (NULL != cc);
629 GNUNET_assert (num_peers > 0);
630 host_filename = NULL;
631 rc = GNUNET_malloc (sizeof (struct RunContext));
632 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, 0);
633 GNUNET_assert (NULL != rc->h);
635 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, cfg,
636 &controller_status_cb, rc);
637 GNUNET_assert (NULL != rc->cproc);
638 rc->num_peers = num_peers;
639 rc->event_mask = event_mask;
640 rc->event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
644 rc->master_cls = master_cls;
646 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
647 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
651 if (0 == strcasecmp (topology, "RANDOM"))
653 rc->topology = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
655 else if (0 == strcasecmp (topology, "SMALL_WORLD_RING"))
657 rc->topology = GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING;
659 else if (0 == strcasecmp (topology, "CLIQUE"))
661 rc->topology = GNUNET_TESTBED_TOPOLOGY_CLIQUE;
662 rc->num_oc = num_peers * (num_peers - 1);
664 else if (0 == strcasecmp (topology, "LINE"))
666 rc->topology = GNUNET_TESTBED_TOPOLOGY_LINE;
667 rc->num_oc = num_peers - 1;
669 else if (0 == strcasecmp (topology, "RING"))
671 rc->topology = GNUNET_TESTBED_TOPOLOGY_RING;
672 rc->num_oc = num_peers;
674 else if (0 == strcasecmp (topology, "2D_TORUS"))
676 rc->topology = GNUNET_TESTBED_TOPOLOGY_2D_TORUS;
677 rc->num_oc = GNUNET_TESTBED_2dtorus_calc_links (num_peers, NULL, NULL);
680 LOG (GNUNET_ERROR_TYPE_WARNING,
681 "Unknown topology %s given in configuration\n", topology);
682 GNUNET_free (topology);
684 if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
685 || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology))
687 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
688 "OVERLAY_RANDOM_LINKS",
691 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
692 option to be set to the number of random links to be established */
697 if (random_links > UINT32_MAX)
699 GNUNET_break (0); /* Too big number */
703 rc->num_oc = (unsigned int) random_links;
704 if (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology)
705 rc->num_oc += num_peers;
707 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
708 &shutdown_run_task, rc);
713 * Configure and run a testbed using the given
714 * master controller on 'num_hosts' starting
715 * 'num_peers' using the given peer configuration.
717 * @param controller master controller for the testbed
718 * (must not be destroyed until after the
719 * testbed is destroyed).
720 * @param num_hosts number of hosts in 'hosts', 0 to only
722 * @param hosts list of hosts to use for the testbed
723 * @param num_peers number of peers to start
724 * @param cfg the configuration to use as a template for peers and also for
725 * checking the value of testbed helper binary
726 * @param underlay_topology underlay topology to create
727 * @param va topology-specific options
728 * @return handle to the testbed; NULL upon error (error messaage will be printed)
730 struct GNUNET_TESTBED_Testbed *
731 GNUNET_TESTBED_create_va (struct GNUNET_TESTBED_Controller *controller,
732 unsigned int num_hosts,
733 struct GNUNET_TESTBED_Host **hosts,
734 unsigned int num_peers,
735 const struct GNUNET_CONFIGURATION_Handle *cfg,
736 enum GNUNET_TESTBED_TopologyOption underlay_topology,
741 GNUNET_assert (underlay_topology < GNUNET_TESTBED_TOPOLOGY_NONE);
744 for (nhost = 0; nhost < num_hosts; nhost++)
746 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost], cfg))
748 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
749 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
753 if (num_hosts != nhost)
756 /* We need controller callback here to get operation done events while
764 * Configure and run a testbed using the given
765 * master controller on 'num_hosts' starting
766 * 'num_peers' using the given peer configuration.
768 * @param controller master controller for the testbed
769 * (must not be destroyed until after the
770 * testbed is destroyed).
771 * @param num_hosts number of hosts in 'hosts', 0 to only
773 * @param hosts list of hosts to use for the testbed
774 * @param num_peers number of peers to start
775 * @param cfg the configuration to use as a template for peers and also for
776 * checking the value of testbed helper binary
777 * @param underlay_topology underlay topology to create
778 * @param ... topology-specific options
780 struct GNUNET_TESTBED_Testbed *
781 GNUNET_TESTBED_create (struct GNUNET_TESTBED_Controller *controller,
782 unsigned int num_hosts,
783 struct GNUNET_TESTBED_Host **hosts,
784 unsigned int num_peers,
785 const struct GNUNET_CONFIGURATION_Handle *cfg,
786 enum GNUNET_TESTBED_TopologyOption underlay_topology,
789 struct GNUNET_TESTBED_Testbed *testbed;
792 va_start (vargs, underlay_topology);
793 testbed = GNUNET_TESTBED_create_va (controller, num_hosts, hosts, num_peers,
794 cfg, underlay_topology, vargs);
801 * Destroy a testbed. Stops all running peers and then
802 * destroys all peers. Does NOT destroy the master controller.
804 * @param testbed testbed to destroy
807 GNUNET_TESTBED_destroy (struct GNUNET_TESTBED_Testbed *testbed)
813 /* end of testbed_api_testbed.c */