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"
34 * Generic loggins shorthand
36 #define LOG(kind,...) \
37 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
40 * Opaque handle to an abstract operation to be executed by the testing framework.
42 struct GNUNET_TESTBED_Testbed
47 struct GNUNET_TESTBED_Host **hosts;
50 * The number of hosts in the hosts array
52 unsigned int num_hosts;
55 * The controller handle
57 struct GNUNET_TESTBED_Controller *c;
67 * The testbed operation handle
69 struct GNUNET_TESTBED_Operation *op;
72 * Context information for GNUNET_TESTBED_run()
74 struct RunContext *rc;
82 * The next pointer for DLL
84 struct DLLOperation *next;
87 * The prev pointer for DLL
89 struct DLLOperation *prev;
94 * States of RunContext
104 * The testbed run is ready and the master callback can be called now. At this
105 * time the peers are all started and if a topology is provided in the
106 * configuration the topology would have been attempted
116 * Peers are destroyed
128 * The controller handle
130 struct GNUNET_TESTBED_Controller *c;
133 * Handle to the host on which the controller runs
135 struct GNUNET_TESTBED_Host *h;
138 * The handle to the controller process
140 struct GNUNET_TESTBED_ControllerProc *cproc;
143 * The callback to use as controller callback
145 GNUNET_TESTBED_ControllerCallback cc;
148 * The pointer to the controller callback
153 * Master task to call when testbed initialization is done
155 GNUNET_SCHEDULER_Task master;
158 * The closure for the master task
163 * The head element of DLL operations
165 struct DLLOperation *dll_op_head;
168 * The tail element of DLL operations
170 struct DLLOperation *dll_op_tail;
173 * Array of peers which we create
175 struct GNUNET_TESTBED_Peer **peers;
178 * The topology generation operation. Will be null if no topology is set in
181 struct GNUNET_TESTBED_Operation *topology_operation;
184 * The event mask for the controller
189 * State of this context
194 * The topology which has to be achieved with the peers started in this context
196 enum GNUNET_TESTBED_TopologyOption topology;
199 * Current peer count for an operation; Set this to 0 and increment for each
200 * successful operation on a peer
202 unsigned int peer_count;
205 * number of peers to start
207 unsigned int num_peers;
210 * counter to count overlay connect attempts. This counter includes both
211 * successful and failed overlay connects
213 unsigned int oc_count;
216 * Expected overlay connects. Should be zero if no topology is relavant
224 * Task for starting peers
226 * @param cls the RunHandle
227 * @param tc the task context from scheduler
230 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
232 struct RunContext *rc = cls;
233 struct DLLOperation *dll_op;
236 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting Peers\n");
237 for (peer = 0; peer < rc->num_peers; peer++)
239 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
240 dll_op->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
241 dll_op->cls = rc->peers[peer];
242 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
249 * Functions of this signature are called when a peer has been successfully
252 * @param cls the closure from GNUNET_TESTBED_peer_create()
253 * @param peer the handle for the created peer; NULL on any error during
255 * @param emsg NULL if peer is not NULL; else MAY contain the error description
258 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
260 struct DLLOperation *dll_op = cls;
261 struct RunContext *rc;
263 GNUNET_assert (NULL != dll_op);
265 GNUNET_assert (NULL != rc);
266 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
267 GNUNET_TESTBED_operation_done (dll_op->op);
268 GNUNET_free (dll_op);
272 LOG (GNUNET_ERROR_TYPE_WARNING, "Error while creating a peer: %s\n",
274 /* FIXME: GNUNET_TESTBED_shutdown_run()? */
277 rc->peers[rc->peer_count] = peer;
279 if (rc->peer_count < rc->num_peers)
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "Required peers created successfully\n");
282 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
287 * Assuming all peers have been destroyed cleanup run handle
289 * @param cls the run handle
290 * @param tc the task context from scheduler
293 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
295 struct RunContext *rc = cls;
296 struct DLLOperation *dll_op;
298 GNUNET_assert (NULL == rc->peers);
299 GNUNET_assert (RC_PEERS_DESTROYED == rc->state);
301 GNUNET_TESTBED_controller_disconnect (rc->c);
302 if (NULL != rc->cproc)
303 GNUNET_TESTBED_controller_stop (rc->cproc);
305 GNUNET_TESTBED_host_destroy (rc->h);
306 if (NULL != rc->dll_op_head)
308 LOG (GNUNET_ERROR_TYPE_WARNING,
309 _("Some operations are still pending. Cancelling them\n"));
310 while (NULL != (dll_op = rc->dll_op_head))
312 GNUNET_TESTBED_operation_done (dll_op->op);
313 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
314 GNUNET_free (dll_op);
322 * Task to call master task
324 * @param cls the run context
325 * @param tc the task context
328 call_master (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
330 struct RunContext *rc = cls;
332 if (NULL != rc->topology_operation)
334 GNUNET_TESTBED_operation_done (rc->topology_operation);
335 rc->topology_operation = NULL;
337 if (NULL != rc->master)
338 GNUNET_SCHEDULER_add_continuation (rc->master, rc->master_cls,
339 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
344 * Signature of the event handler function called by the
345 * respective event controller.
348 * @param event information about the event
351 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
353 struct RunContext *rc = cls;
354 struct DLLOperation *dll_op;
355 unsigned int peer_id;
357 if (NULL != rc->topology_operation)
361 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
362 case GNUNET_TESTBED_ET_CONNECT:
368 if (rc->oc_count == rc->num_oc)
370 rc->state = RC_READY;
371 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
372 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
376 if ((RC_INIT != rc->state) &&
377 ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) ||
378 (GNUNET_TESTBED_ET_PEER_STOP == event->type)))
380 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
382 if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
383 (event->details.operation_finished.operation == dll_op->op))
385 if ((GNUNET_TESTBED_ET_PEER_STOP == event->type) &&
386 (event->details.peer_stop.peer == dll_op->cls))
391 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
392 GNUNET_TESTBED_operation_done (dll_op->op);
393 GNUNET_free (dll_op);
395 if (rc->peer_count < rc->num_peers)
400 rc->state = RC_PEERS_STOPPED;
402 for (peer_id = 0; peer_id < rc->num_peers; peer_id++)
404 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
405 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer_id]);
406 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
410 case RC_PEERS_STOPPED:
411 rc->state = RC_PEERS_DESTROYED;
412 GNUNET_free (rc->peers);
414 LOG (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully destroyed\n");
415 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
424 if ((0 != (rc->event_mask && (1LL << event->type))) && (NULL != rc->cc))
425 rc->cc (rc->cc_cls, event);
426 if (GNUNET_TESTBED_ET_PEER_START != event->type)
428 for (dll_op = rc->dll_op_head; NULL != dll_op; dll_op = dll_op->next)
429 if ((NULL != dll_op->cls) &&
430 (event->details.peer_start.peer == dll_op->cls))
432 GNUNET_assert (NULL != dll_op);
433 GNUNET_CONTAINER_DLL_remove (rc->dll_op_head, rc->dll_op_tail, dll_op);
434 GNUNET_TESTBED_operation_done (dll_op->op);
435 GNUNET_free (dll_op);
437 if (rc->peer_count < rc->num_peers)
439 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers started successfully\n");
440 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
442 if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
443 || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology))
444 rc->topology_operation =
445 GNUNET_TESTBED_overlay_configure_topology (NULL,
449 (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI
452 (rc->num_oc - rc->num_peers),
453 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
455 rc->topology_operation =
456 GNUNET_TESTBED_overlay_configure_topology (NULL,
460 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
461 if (NULL == rc->topology_operation)
462 LOG (GNUNET_ERROR_TYPE_WARNING,
463 "Not generating topology. Check number of peers\n");
467 rc->state = RC_READY;
468 GNUNET_SCHEDULER_add_continuation (&call_master, rc,
469 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
475 * Callback to signal successfull startup of the controller process
477 * @param cls the closure from GNUNET_TESTBED_controller_start()
478 * @param cfg the configuration with which the controller has been started;
479 * NULL if status is not GNUNET_OK
480 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
481 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
484 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
487 struct RunContext *rc = cls;
488 struct DLLOperation *dll_op;
492 if (status != GNUNET_OK)
494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Testbed startup failed\n");
497 event_mask = rc->event_mask;
498 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
499 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
500 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
501 event_mask |= GNUNET_TESTBED_ET_CONNECT;
503 GNUNET_TESTBED_controller_connect (cfg, rc->h, event_mask, &event_cb, rc);
505 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
506 GNUNET_assert (NULL != rc->c);
508 for (peer = 0; peer < rc->num_peers; peer++)
510 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
513 GNUNET_TESTBED_peer_create (rc->c, rc->h, cfg, peer_create_cb, dll_op);
514 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail, dll_op);
520 * Stops the testbed run and releases any used resources
522 * @param cls the tesbed run handle
523 * @param tc the task context from scheduler
526 shutdown_run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 struct RunContext *rc = cls;
529 struct DLLOperation *dll_op;
534 if (NULL != rc->peers)
536 if (NULL != rc->topology_operation)
538 GNUNET_TESTBED_operation_done (rc->topology_operation);
539 rc->topology_operation = NULL;
541 if (RC_INIT == rc->state)
542 rc->state = RC_READY; /* Even though we haven't called the master callback */
544 /* Check if some peers are stopped */
545 for (peer = 0; peer < rc->num_peers; peer++)
547 if (PS_STOPPED != rc->peers[peer]->state)
550 if (peer == rc->num_peers)
552 /* All peers are stopped */
553 rc->state = RC_PEERS_STOPPED;
554 for (peer = 0; peer < rc->num_peers; peer++)
556 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
557 dll_op->op = GNUNET_TESTBED_peer_destroy (rc->peers[peer]);
558 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
563 /* Some peers are stopped */
564 for (peer = 0; peer < rc->num_peers; peer++)
566 if (PS_STARTED != rc->peers[peer]->state)
571 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
572 dll_op->op = GNUNET_TESTBED_peer_stop (rc->peers[peer], NULL, NULL);
573 dll_op->cls = rc->peers[peer];
574 GNUNET_CONTAINER_DLL_insert_tail (rc->dll_op_head, rc->dll_op_tail,
577 if (rc->peer_count != rc->num_peers)
581 rc->state = RC_PEERS_DESTROYED; /* No peers are present so we consider the
582 * state where all peers are destroyed */
583 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
588 * Convenience method for running a testbed with
589 * a single call. Underlay and overlay topology
590 * are configured using the "UNDERLAY" and "OVERLAY"
591 * options in the "[testbed]" section of the configuration\
592 * (with possible options given in "UNDERLAY_XXX" and/or
595 * The testbed is to be terminated using a call to
596 * "GNUNET_SCHEDULER_shutdown".
598 * @param host_filename name of the file with the 'hosts', NULL
599 * to run everything on 'localhost'
600 * @param cfg configuration to use (for testbed, controller and peers)
601 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
602 * @param event_mask bit mask with set of events to call 'cc' for;
603 * or-ed values of "1LL" shifted by the
604 * respective 'enum GNUNET_TESTBED_EventType'
605 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
606 * @param cc controller callback to invoke on events; This callback is called
607 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
608 * set in the event_mask as this is the only way get access to the
609 * handle of each peer
610 * @param cc_cls closure for cc
611 * @param master task to run once the testbed is ready
615 GNUNET_TESTBED_run (const char *host_filename,
616 const struct GNUNET_CONFIGURATION_Handle *cfg,
617 unsigned int num_peers, uint64_t event_mask,
618 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
619 GNUNET_SCHEDULER_Task master, void *master_cls)
621 struct RunContext *rc;
623 unsigned long long random_links;
625 GNUNET_break (NULL == host_filename); /* Currently we do not support host
627 GNUNET_assert (NULL != cc);
628 GNUNET_assert (num_peers > 0);
629 host_filename = NULL;
630 rc = GNUNET_malloc (sizeof (struct RunContext));
631 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, 0);
632 GNUNET_assert (NULL != rc->h);
634 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h, cfg,
635 &controller_status_cb, rc);
636 GNUNET_assert (NULL != rc->cproc);
637 rc->num_peers = num_peers;
638 rc->event_mask = event_mask;
639 rc->event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
643 rc->master_cls = master_cls;
645 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
646 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
650 if (0 == strcasecmp (topology, "RANDOM"))
652 rc->topology = GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI;
654 else if (0 == strcasecmp (topology, "SMALL_WORLD_RING"))
656 rc->topology = GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING;
658 else if (0 == strcasecmp (topology, "CLIQUE"))
660 rc->topology = GNUNET_TESTBED_TOPOLOGY_CLIQUE;
661 rc->num_oc = num_peers * (num_peers - 1);
663 else if (0 == strcasecmp (topology, "LINE"))
665 rc->topology = GNUNET_TESTBED_TOPOLOGY_LINE;
666 rc->num_oc = num_peers - 1;
668 else if (0 == strcasecmp (topology, "RING"))
670 rc->topology = GNUNET_TESTBED_TOPOLOGY_RING;
671 rc->num_oc = num_peers;
674 LOG (GNUNET_ERROR_TYPE_WARNING,
675 "Unknown topology %s given in configuration\n", topology);
676 GNUNET_free (topology);
678 if ( (GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology)
679 || (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology))
681 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
682 "OVERLAY_RANDOM_LINKS",
685 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
686 option to be set to the number of random links to be established */
691 if (random_links > UINT32_MAX)
693 GNUNET_break (0); /* Too big number */
697 rc->num_oc = (unsigned int) random_links;
698 if (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology)
699 rc->num_oc += num_peers;
701 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
702 &shutdown_run_task, rc);
707 * Configure and run a testbed using the given
708 * master controller on 'num_hosts' starting
709 * 'num_peers' using the given peer configuration.
711 * @param controller master controller for the testbed
712 * (must not be destroyed until after the
713 * testbed is destroyed).
714 * @param num_hosts number of hosts in 'hosts', 0 to only
716 * @param hosts list of hosts to use for the testbed
717 * @param num_peers number of peers to start
718 * @param cfg the configuration to use as a template for peers and also for
719 * checking the value of testbed helper binary
720 * @param underlay_topology underlay topology to create
721 * @param va topology-specific options
722 * @return handle to the testbed; NULL upon error (error messaage will be printed)
724 struct GNUNET_TESTBED_Testbed *
725 GNUNET_TESTBED_create_va (struct GNUNET_TESTBED_Controller *controller,
726 unsigned int num_hosts,
727 struct GNUNET_TESTBED_Host **hosts,
728 unsigned int num_peers,
729 const struct GNUNET_CONFIGURATION_Handle *cfg,
730 enum GNUNET_TESTBED_TopologyOption underlay_topology,
735 GNUNET_assert (underlay_topology < GNUNET_TESTBED_TOPOLOGY_NONE);
738 for (nhost = 0; nhost < num_hosts; nhost++)
740 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost], cfg))
742 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
743 GNUNET_TESTBED_host_get_hostname_ (hosts[nhost]));
747 if (num_hosts != nhost)
750 /* We need controller callback here to get operation done events while
758 * Configure and run a testbed using the given
759 * master controller on 'num_hosts' starting
760 * 'num_peers' using the given peer configuration.
762 * @param controller master controller for the testbed
763 * (must not be destroyed until after the
764 * testbed is destroyed).
765 * @param num_hosts number of hosts in 'hosts', 0 to only
767 * @param hosts list of hosts to use for the testbed
768 * @param num_peers number of peers to start
769 * @param cfg the configuration to use as a template for peers and also for
770 * checking the value of testbed helper binary
771 * @param underlay_topology underlay topology to create
772 * @param ... topology-specific options
774 struct GNUNET_TESTBED_Testbed *
775 GNUNET_TESTBED_create (struct GNUNET_TESTBED_Controller *controller,
776 unsigned int num_hosts,
777 struct GNUNET_TESTBED_Host **hosts,
778 unsigned int num_peers,
779 const struct GNUNET_CONFIGURATION_Handle *cfg,
780 enum GNUNET_TESTBED_TopologyOption underlay_topology,
783 struct GNUNET_TESTBED_Testbed *testbed;
786 va_start (vargs, underlay_topology);
787 testbed = GNUNET_TESTBED_create_va (controller, num_hosts, hosts, num_peers,
788 cfg, underlay_topology, vargs);
795 * Destroy a testbed. Stops all running peers and then
796 * destroys all peers. Does NOT destroy the master controller.
798 * @param testbed testbed to destroy
801 GNUNET_TESTBED_destroy (struct GNUNET_TESTBED_Testbed *testbed)
807 /* end of testbed_api_testbed.c */