2 This file is part of GNUnet
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api_testbed.c
23 * @brief high-level testbed management
24 * @author Christian Grothoff
25 * @author Sree Harsha Totakura
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31 #include "testbed_api.h"
32 #include "testbed_api_peers.h"
33 #include "testbed_api_hosts.h"
34 #include "testbed_api_topology.h"
37 * Generic loggins shorthand
39 #define LOG(kind,...) \
40 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
43 * Debug logging shortcut
46 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49 * The default setup timeout in seconds
51 #define DEFAULT_SETUP_TIMEOUT 300
59 * Context information for the operation we start
61 struct RunContextOperation
64 * The testbed operation handle
66 struct GNUNET_TESTBED_Operation *op;
69 * Context information for GNUNET_TESTBED_run()
71 struct RunContext *rc;
82 * States of RunContext
92 * Controllers on given hosts started and linked
102 * The testbed run is ready and the master callback can be called now. At this
103 * time the peers are all started and if a topology is provided in the
104 * configuration the topology would have been attempted
109 /* * Peers are stopped */
111 /* RC_PEERS_STOPPED, */
114 /* * Peers are destroyed */
116 /* RC_PEERS_DESTROYED */
119 * All peers shutdown (stopped and destroyed)
126 * Context for host compability checks
128 struct CompatibilityCheckContext
133 struct RunContext *rc;
136 * Handle for the compability check
138 struct GNUNET_TESTBED_HostHabitableCheckHandle *h;
141 * Index of the host in the run context's hosts array
153 * The controller handle
155 struct GNUNET_TESTBED_Controller *c;
158 * The configuration of the controller. This is based on the cfg given to the
159 * function GNUNET_TESTBED_run(). We also use this config as a template while
162 struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Handle to the host on which the controller runs
167 struct GNUNET_TESTBED_Host *h;
170 * The handle to the controller process
172 struct GNUNET_TESTBED_ControllerProc *cproc;
175 * The callback to use as controller callback
177 GNUNET_TESTBED_ControllerCallback cc;
180 * The pointer to the controller callback
185 * The trusted IP string
190 * TestMaster callback to call when testbed initialization is done
192 GNUNET_TESTBED_TestMaster test_master;
195 * The closure for the TestMaster callback
197 void *test_master_cls;
200 * A hashmap for operations started by us
202 struct GNUNET_CONTAINER_MultiHashMap32 *rcop_map;
205 * An array of hosts loaded from the hostkeys file
207 struct GNUNET_TESTBED_Host **hosts;
210 * Array of compatibility check contexts
212 struct CompatibilityCheckContext *hclist;
215 * Array of peers which we create
217 struct GNUNET_TESTBED_Peer **peers;
220 * The topology generation operation. Will be null if no topology is set in
223 struct GNUNET_TESTBED_Operation *topology_operation;
226 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
231 * Host registration handle
233 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
236 * Profiling start time
238 struct GNUNET_TIME_Absolute pstart_time;
241 * Host registration task
243 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
246 * Task to be run of a timeout
248 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
251 * Task run upon shutdown interrupts
253 GNUNET_SCHEDULER_TaskIdentifier interrupt_task;
256 * The event mask for the controller
261 * State of this context
266 * The topology which has to be achieved with the peers started in this context
268 enum GNUNET_TESTBED_TopologyOption topology;
271 * Have we already shutdown
276 * Number of hosts in the given host file
278 unsigned int num_hosts;
281 * Number of registered hosts. Also used as a counter while checking
282 * habitabillity of hosts
284 unsigned int reg_hosts;
287 * Current peer count for an operation; Set this to 0 and increment for each
288 * successful operation on a peer
290 unsigned int peer_count;
293 * number of peers to start
295 unsigned int num_peers;
298 * Expected overlay connects. Should be zero if no topology is relavant
303 * Number of random links to established
305 unsigned int random_links;
308 * the number of overlay link connection attempts that succeeded
310 unsigned int links_succeeded;
313 * the number of overlay link connection attempts that failed
315 unsigned int links_failed;
321 * Return a 32-bit key from a pointer
323 * @param rcop the pointer
327 rcop_key (void *rcop)
329 return * ((uint32_t *) &rcop);
334 * Context information used for finding a pointer in the rcop_map
339 * The operation pointer to look for
341 struct GNUNET_TESTBED_Operation *query;
344 * The Run context operation which has the operation being queried
346 struct RunContextOperation *result;
351 * Iterator for searching over the elements matching a given query
353 * @param cls the SearchContext
354 * @param key the 32-bit key
355 * @param value the RunContextOperation element
356 * @return GNUNET_YES to continue iteration; GNUNET_NO to cancel it
359 search_iterator (void *cls, uint32_t key, void *value)
361 struct RunContextOperation *rcop = value;
362 struct SearchContext *sc = cls;
364 GNUNET_assert (NULL != rcop);
365 if (sc->query == rcop->op)
367 GNUNET_assert (NULL == sc->result);
376 * Initiate a search for the given operation in the rcop_map
378 * @param rc the RunContext whose rcop_map will be searched for the given
380 * @param op the given operation to search for
381 * @return the matching RunContextOperation if found; NULL if not
383 static struct RunContextOperation *
384 search_rcop (struct RunContext *rc, struct GNUNET_TESTBED_Operation *op)
386 struct SearchContext sc;
391 GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
396 GNUNET_assert (NULL != sc.result);
404 * Insert an RunContextOperation into the rcop_map of the given RunContext
406 * @param rc the RunContext into whose map is to be used for insertion
407 * @param rcop the RunContextOperation to insert
410 insert_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
412 GNUNET_assert (GNUNET_OK ==
413 GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
414 rcop_key (rcop->op), rcop,
415 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
420 * Remove a RunContextOperation from the rcop_map of the given RunContext
422 * @param rc the RunContext from whose map the given RunContextOperaton has to
424 * @param rcop the RunContextOperation
427 remove_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
429 GNUNET_assert (GNUNET_YES ==
430 GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
436 * Assuming all peers have been destroyed cleanup run handle
438 * @param cls the run handle
439 * @param tc the task context from scheduler
444 struct RunContext *rc = cls;
447 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
448 GNUNET_assert (NULL == rc->reg_handle);
449 GNUNET_assert (NULL == rc->peers);
450 GNUNET_assert (NULL == rc->hclist);
451 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
452 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
453 GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
455 GNUNET_TESTBED_controller_disconnect (rc->c);
456 if (NULL != rc->cproc)
457 GNUNET_TESTBED_controller_stop (rc->cproc);
459 GNUNET_TESTBED_host_destroy (rc->h);
460 for (hid = 0; hid < rc->num_hosts; hid++)
461 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
462 GNUNET_free_non_null (rc->hosts);
464 GNUNET_CONFIGURATION_destroy (rc->cfg);
465 GNUNET_free_non_null (rc->topo_file);
466 GNUNET_free_non_null (rc->trusted_ip);
472 * Iterator for cleaning up elements from rcop_map
474 * @param cls the RunContext
475 * @param key the 32-bit key
476 * @param value the RunContextOperation element
477 * @return always GNUNET_YES
480 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
482 struct RunContext *rc = cls;
483 struct RunContextOperation *rcop = value;
485 GNUNET_assert (rc == rcop->rc);
486 remove_rcop (rc, rcop);
487 GNUNET_TESTBED_operation_done (rcop->op);
494 * Cancels operations and tasks which are assigned to the given run context
496 * @param rc the RunContext
499 rc_cleanup_operations (struct RunContext *rc)
501 struct CompatibilityCheckContext *hc;
504 if (NULL != rc->hclist)
506 for (nhost = 0; nhost < rc->num_hosts; nhost++)
508 hc = &rc->hclist[nhost];
510 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
512 GNUNET_free (rc->hclist);
515 /* Stop register hosts task if it is running */
516 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
518 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
519 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
521 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
523 GNUNET_SCHEDULER_cancel (rc->timeout_task);
524 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
526 if (NULL != rc->reg_handle)
528 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
529 rc->reg_handle = NULL;
531 if (NULL != rc->topology_operation)
533 GNUNET_TESTBED_operation_done (rc->topology_operation);
534 rc->topology_operation = NULL;
536 /* cancel any exiting operations */
537 GNUNET_assert (GNUNET_SYSERR !=
538 GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
539 &rcop_cleanup_iterator,
545 * Cancels the scheduled interrupt task
547 * @param rc the run context
550 cancel_interrupt_task (struct RunContext *rc)
552 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
553 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
558 * This callback will be called when all the operations are completed
561 * @param cls run context
562 * @param tc scheduler task context
565 wait_op_completion (void *cls)
567 struct RunContext *rc = cls;
568 struct RunContextOperation *rcop;
570 if ( (NULL == rc->cproc)
572 || (GNUNET_YES == rc->shutdown) )
574 if (NULL != rc->peers)
576 GNUNET_free (rc->peers);
581 if (NULL == rc->peers)
583 rc->shutdown = GNUNET_YES;
584 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
586 rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
587 GNUNET_assert (NULL != rcop->op);
588 DEBUG ("Shutting down peers\n");
589 rc->pstart_time = GNUNET_TIME_absolute_get ();
590 insert_rcop (rc, rcop);
594 rc->state = RC_PEERS_SHUTDOWN;
595 cancel_interrupt_task (rc);
601 * Task run upon interrupts (SIGINT, SIGTERM) and upon scheduler shutdown.
603 * @param cls the RunContext which has to be acted upon
604 * @param tc the scheduler task context
607 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
609 struct RunContext *rc = cls;
610 struct GNUNET_TESTBED_Controller *c = rc->c;
614 rc->interrupt_task = GNUNET_SCHEDULER_add_delayed
615 (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt, rc);
616 rc_cleanup_operations (rc);
617 if ( (GNUNET_NO == rc->shutdown)
619 && (0 != (size = GNUNET_CONTAINER_multihashmap32_size (c->opc_map))))
621 LOG (GNUNET_ERROR_TYPE_WARNING, "Shutdown postponed as there are "
622 "%u operations currently active\n", size);
623 c->opcq_empty_cb = &wait_op_completion;
624 c->opcq_empty_cls = rc;
627 wait_op_completion (rc);
632 * Function to return the string representation of the duration between current
633 * time and `pstart_time' in `RunContext'
635 * @param rc the RunContext
636 * @return the representation string; this is NOT reentrant
639 prof_time (struct RunContext *rc)
641 struct GNUNET_TIME_Relative ptime;
643 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
644 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
649 * Task for starting peers
651 * @param cls the RunHandle
652 * @param tc the task context from scheduler
655 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
657 struct RunContext *rc = cls;
658 struct RunContextOperation *rcop;
661 DEBUG ("Starting Peers\n");
662 rc->pstart_time = GNUNET_TIME_absolute_get ();
663 for (peer = 0; peer < rc->num_peers; peer++)
665 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
667 rcop->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
668 GNUNET_assert (NULL != rcop->op);
669 rcop->cls = rc->peers[peer];
670 insert_rcop (rc, rcop);
677 * Functions of this signature are called when a peer has been successfully
680 * @param cls the closure from GNUNET_TESTBED_peer_create()
681 * @param peer the handle for the created peer; NULL on any error during
683 * @param emsg NULL if peer is not NULL; else MAY contain the error description
686 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
688 struct RunContextOperation *rcop = cls;
689 struct RunContext *rc;
691 GNUNET_assert (NULL != rcop);
692 GNUNET_assert (NULL != (rc = rcop->rc));
693 remove_rcop (rc, rcop);
694 GNUNET_TESTBED_operation_done (rcop->op);
699 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
701 GNUNET_SCHEDULER_shutdown ();
704 rc->peers[rc->peer_count] = peer;
706 if (rc->peer_count < rc->num_peers)
708 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
709 rc->state = RC_PEERS_CREATED;
710 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
715 * call test master callback
717 * @param rc the RunContext
720 call_master (struct RunContext *rc)
722 GNUNET_SCHEDULER_cancel (rc->timeout_task);
723 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
724 if (NULL != rc->test_master)
725 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers,
726 rc->links_succeeded, rc->links_failed);
731 * Callbacks of this type are called when topology configuration is completed
733 * @param cls the operation closure given to
734 * GNUNET_TESTBED_overlay_configure_topology_va() and
735 * GNUNET_TESTBED_overlay_configure() calls
736 * @param nsuccess the number of successful overlay connects
737 * @param nfailures the number of overlay connects which failed
740 topology_completion_callback (void *cls, unsigned int nsuccess,
741 unsigned int nfailures)
743 struct RunContext *rc = cls;
745 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
746 GNUNET_TESTBED_operation_done (rc->topology_operation);
747 rc->topology_operation = NULL;
748 rc->links_succeeded = nsuccess;
749 rc->links_failed = nfailures;
750 rc->state = RC_READY;
756 * Function to create peers
758 * @param rc the RunContext
761 create_peers (struct RunContext *rc)
763 struct RunContextOperation *rcop;
766 DEBUG ("Creating peers\n");
767 rc->pstart_time = GNUNET_TIME_absolute_get ();
769 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
770 GNUNET_assert (NULL != rc->c);
772 for (peer = 0; peer < rc->num_peers; peer++)
774 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
777 GNUNET_TESTBED_peer_create (rc->c,
779 rc->num_hosts) ? rc->h : rc->hosts[peer %
781 rc->cfg, &peer_create_cb, rcop);
782 GNUNET_assert (NULL != rcop->op);
783 insert_rcop (rc, rcop);
789 * Signature of the event handler function called by the
790 * respective event controller.
793 * @param event information about the event
796 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
798 struct RunContext *rc = cls;
799 struct RunContextOperation *rcop;
801 if (RC_INIT == rc->state)
805 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
806 rcop = event->op_cls;
807 if (NULL != event->details.operation_finished.emsg)
809 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
810 GNUNET_SCHEDULER_shutdown ();
814 GNUNET_assert (event->op == rcop->op);
815 remove_rcop (rc, rcop);
816 GNUNET_TESTBED_operation_done (rcop->op);
818 if (rc->reg_hosts == rc->num_hosts)
820 rc->state = RC_LINKED;
826 GNUNET_SCHEDULER_shutdown ();
830 if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
832 if (NULL == (rcop = search_rcop (rc, event->op)))
834 remove_rcop (rc, rcop);
835 GNUNET_TESTBED_operation_done (rcop->op);
837 if ( (GNUNET_NO == rc->shutdown)
838 && (NULL != event->details.operation_finished.emsg) )
840 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
841 event->details.operation_finished.emsg);
842 GNUNET_SCHEDULER_shutdown ();
845 GNUNET_assert (GNUNET_YES == rc->shutdown);
849 case RC_PEERS_CREATED:
851 rc->state = RC_PEERS_SHUTDOWN;
852 GNUNET_free_non_null (rc->peers);
854 DEBUG ("Peers shut down in %s\n", prof_time (rc));
855 GNUNET_SCHEDULER_shutdown ();
863 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
864 rc->cc (rc->cc_cls, event);
865 if (GNUNET_TESTBED_ET_PEER_START != event->type)
867 if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
869 remove_rcop (rc, rcop);
870 GNUNET_TESTBED_operation_done (rcop->op);
873 if (rc->peer_count < rc->num_peers)
875 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
876 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
878 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
879 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
880 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
882 rc->topology_operation =
883 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
884 rc->peers, &rc->num_oc,
885 &topology_completion_callback,
889 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
891 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
893 GNUNET_assert (NULL != rc->topo_file);
894 rc->topology_operation =
895 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
896 rc->peers, &rc->num_oc,
897 &topology_completion_callback,
901 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
904 rc->topology_operation =
905 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
906 rc->peers, &rc->num_oc,
907 &topology_completion_callback,
910 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
911 if (NULL == rc->topology_operation)
912 LOG (GNUNET_ERROR_TYPE_WARNING,
913 "Not generating topology. Check number of peers\n");
916 DEBUG ("Creating overlay topology\n");
917 rc->pstart_time = GNUNET_TIME_absolute_get ();
921 rc->state = RC_READY;
927 * Task to register all hosts available in the global host list
929 * @param cls the RunContext
930 * @param tc the scheduler task context
933 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
937 * Callback which will be called to after a host registration succeeded or failed
939 * @param cls the closure
940 * @param emsg the error message; NULL if host registration is successful
943 host_registration_completion (void *cls, const char *emsg)
945 struct RunContext *rc = cls;
947 rc->reg_handle = NULL;
950 LOG (GNUNET_ERROR_TYPE_WARNING,
951 _("Host registration failed for a host. Error: %s\n"), emsg);
952 GNUNET_SCHEDULER_shutdown ();
955 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
960 * Task to register all hosts available in the global host list
962 * @param cls RunContext
963 * @param tc the scheduler task context
966 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
968 struct RunContext *rc = cls;
969 struct RunContextOperation *rcop;
972 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
973 if (rc->reg_hosts == rc->num_hosts)
975 DEBUG ("All hosts successfully registered\n");
977 for (slave = 0; slave < rc->num_hosts; slave++)
979 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
982 GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[slave],
984 GNUNET_assert (NULL != rcop->op);
985 insert_rcop (rc, rcop);
991 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
992 host_registration_completion, rc);
998 * Callback to signal successfull startup of the controller process
1000 * @param cls the closure from GNUNET_TESTBED_controller_start()
1001 * @param cfg the configuration with which the controller has been started;
1002 * NULL if status is not GNUNET_OK
1003 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1004 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1007 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1010 struct RunContext *rc = cls;
1011 uint64_t event_mask;
1013 if (status != GNUNET_OK)
1015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1016 _("Controller crash detected. Shutting down.\n"));
1017 GNUNET_SCHEDULER_shutdown ();
1020 GNUNET_CONFIGURATION_destroy (rc->cfg);
1021 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1022 event_mask = rc->event_mask;
1023 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1024 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1025 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
1026 event_mask |= GNUNET_TESTBED_ET_CONNECT;
1028 GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
1029 if (0 < rc->num_hosts)
1032 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
1035 rc->state = RC_LINKED;
1041 * Callback function invoked for each interface found.
1043 * @param cls closure
1044 * @param name name of the interface (can be NULL for unknown)
1045 * @param isDefault is this presumably the default interface
1046 * @param addr address of this interface (can be NULL for unknown or unassigned)
1047 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
1048 * @param netmask the network mask (can be NULL for unknown or unassigned))
1049 * @param addrlen length of the address
1050 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1053 netint_proc (void *cls, const char *name, int isDefault,
1054 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
1055 const struct sockaddr *netmask, socklen_t addrlen)
1057 struct RunContext *rc = cls;
1058 char hostip[NI_MAXHOST];
1061 if (sizeof (struct sockaddr_in) != addrlen)
1062 return GNUNET_OK; /* Only consider IPv4 for now */
1064 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
1065 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
1066 if (NULL == rc->trusted_ip)
1068 rc->trusted_ip = GNUNET_strdup (hostip);
1071 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
1072 GNUNET_free (rc->trusted_ip);
1073 rc->trusted_ip = buf;
1079 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1080 * inform whether the given host is habitable or not. The Handle returned by
1081 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1084 * @param host the host whose status is being reported; will be NULL if the host
1085 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1086 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1089 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
1092 struct CompatibilityCheckContext *hc = cls;
1093 struct RunContext *rc;
1094 struct GNUNET_TESTBED_Host **old_hosts;
1097 GNUNET_assert (NULL != (rc = hc->rc));
1099 GNUNET_assert (nhost <= rc->num_hosts);
1100 GNUNET_assert (host == rc->hosts[nhost]);
1102 if (GNUNET_NO == status)
1104 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1105 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1106 GNUNET_TESTBED_host_get_hostname (host));
1108 LOG (GNUNET_ERROR_TYPE_ERROR,
1109 _("Testbed cannot be started on localhost\n"));
1110 GNUNET_SCHEDULER_shutdown ();
1114 if (rc->reg_hosts < rc->num_hosts)
1116 GNUNET_free (rc->hclist);
1118 rc->h = rc->hosts[0];
1120 if (0 < rc->num_hosts)
1122 old_hosts = rc->hosts;
1124 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1125 memcpy (rc->hosts, &old_hosts[1],
1126 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1127 GNUNET_free (old_hosts);
1131 GNUNET_free (rc->hosts);
1134 GNUNET_TESTBED_host_resolve_ (rc->h);
1135 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1136 GNUNET_TESTBED_host_resolve_ (rc->hosts[nhost]);
1137 GNUNET_OS_network_interfaces_list (netint_proc, rc);
1138 if (NULL == rc->trusted_ip)
1139 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1141 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1142 &controller_status_cb, rc);
1143 GNUNET_free (rc->trusted_ip);
1144 rc->trusted_ip = NULL;
1145 if (NULL == rc->cproc)
1147 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1148 GNUNET_SCHEDULER_shutdown ();
1154 * Task run upon timeout while setting up the testbed
1156 * @param cls the RunContext
1157 * @param tc the task context
1160 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1162 struct RunContext *rc = cls;
1164 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1165 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1166 GNUNET_SCHEDULER_shutdown ();
1167 if (NULL != rc->test_master)
1168 rc->test_master (rc->test_master_cls, 0, NULL, 0, 0);
1169 rc->test_master = NULL;
1174 * Convenience method for running a testbed with
1175 * a single call. Underlay and overlay topology
1176 * are configured using the "UNDERLAY" and "OVERLAY"
1177 * options in the "[testbed]" section of the configuration\
1178 * (with possible options given in "UNDERLAY_XXX" and/or
1181 * The testbed is to be terminated using a call to
1182 * "GNUNET_SCHEDULER_shutdown".
1184 * @param host_filename name of the file with the 'hosts', NULL
1185 * to run everything on 'localhost'
1186 * @param cfg configuration to use (for testbed, controller and peers)
1187 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1188 * @param event_mask bit mask with set of events to call 'cc' for;
1189 * or-ed values of "1LL" shifted by the
1190 * respective 'enum GNUNET_TESTBED_EventType'
1191 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1192 * @param cc controller callback to invoke on events; This callback is called
1193 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1194 * set in the event_mask as this is the only way get access to the
1195 * handle of each peer
1196 * @param cc_cls closure for cc
1197 * @param test_master this callback will be called once the test is ready
1198 * @param test_master_cls closure for 'test_master'.
1201 GNUNET_TESTBED_run (const char *host_filename,
1202 const struct GNUNET_CONFIGURATION_Handle *cfg,
1203 unsigned int num_peers, uint64_t event_mask,
1204 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1205 GNUNET_TESTBED_TestMaster test_master,
1206 void *test_master_cls)
1208 struct RunContext *rc;
1210 struct CompatibilityCheckContext *hc;
1211 struct GNUNET_TIME_Relative timeout;
1212 unsigned long long random_links;
1216 GNUNET_assert (num_peers > 0);
1217 rc = GNUNET_malloc (sizeof (struct RunContext));
1218 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1220 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1222 if (0 == rc->num_hosts)
1224 LOG (GNUNET_ERROR_TYPE_WARNING,
1225 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1229 if (NULL != host_filename)
1232 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1234 if (0 == rc->num_hosts)
1236 LOG (GNUNET_ERROR_TYPE_WARNING,
1237 _("No hosts loaded. Need at least one host\n"));
1242 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1244 rc->num_peers = num_peers;
1245 rc->event_mask = event_mask;
1247 rc->cc_cls = cc_cls;
1248 rc->test_master = test_master;
1249 rc->test_master_cls = test_master_cls;
1250 rc->state = RC_INIT;
1251 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1253 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1254 "OVERLAY_TOPOLOGY", &topology))
1256 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1258 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1261 ("Specified topology must be supported by testbed"));
1263 GNUNET_free (topology);
1265 switch (rc->topology)
1267 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1268 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1269 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1271 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1272 "OVERLAY_RANDOM_LINKS",
1275 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1276 * option to be set to the number of random links to be established */
1277 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1278 "OVERLAY_RANDOM_LINKS");
1281 if (random_links > UINT32_MAX)
1283 GNUNET_break (0); /* Too big number */
1286 rc->random_links = (unsigned int) random_links;
1288 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1290 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1291 "OVERLAY_TOPOLOGY_FILE",
1294 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1295 "OVERLAY_TOPOLOGY_FILE");
1300 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1302 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1303 "OVERLAY_RANDOM_LINKS"))
1304 LOG (GNUNET_ERROR_TYPE_WARNING,
1305 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1308 if (0 != rc->num_hosts)
1310 rc->hclist = GNUNET_malloc (sizeof (struct CompatibilityCheckContext)
1312 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1314 hc = &rc->hclist[nhost];
1317 hc->h = GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1318 &host_habitable_cb, hc);
1322 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1324 hc = &rc->hclist[nhost];
1326 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
1328 GNUNET_free (rc->hclist);
1336 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1337 &controller_status_cb, rc);
1338 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1342 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1343 DEFAULT_SETUP_TIMEOUT);
1345 rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1347 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1348 rc->interrupt_task =
1349 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1355 GNUNET_TESTBED_host_destroy (rc->h);
1356 if (NULL != rc->hosts)
1358 for (hid = 0; hid < rc->num_hosts; hid++)
1359 if (NULL != rc->hosts[hid])
1360 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1361 GNUNET_free (rc->hosts);
1363 if (NULL != rc->cfg)
1364 GNUNET_CONFIGURATION_destroy (rc->cfg);
1369 /* end of testbed_api_testbed.c */