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
443 struct RunContext *rc = cls;
446 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
447 GNUNET_assert (NULL == rc->reg_handle);
448 GNUNET_assert (NULL == rc->peers);
449 GNUNET_assert (NULL == rc->hclist);
450 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
451 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
452 GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
454 GNUNET_TESTBED_controller_disconnect (rc->c);
455 if (NULL != rc->cproc)
456 GNUNET_TESTBED_controller_stop (rc->cproc);
458 GNUNET_TESTBED_host_destroy (rc->h);
459 for (hid = 0; hid < rc->num_hosts; hid++)
460 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
461 GNUNET_free_non_null (rc->hosts);
463 GNUNET_CONFIGURATION_destroy (rc->cfg);
464 GNUNET_free_non_null (rc->topo_file);
465 GNUNET_free_non_null (rc->trusted_ip);
471 * Iterator for cleaning up elements from rcop_map
473 * @param cls the RunContext
474 * @param key the 32-bit key
475 * @param value the RunContextOperation element
476 * @return always GNUNET_YES
479 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
481 struct RunContext *rc = cls;
482 struct RunContextOperation *rcop = value;
484 GNUNET_assert (rc == rcop->rc);
485 remove_rcop (rc, rcop);
486 GNUNET_TESTBED_operation_done (rcop->op);
493 * Cancels operations and tasks which are assigned to the given run context
495 * @param rc the RunContext
498 rc_cleanup_operations (struct RunContext *rc)
500 struct CompatibilityCheckContext *hc;
503 if (NULL != rc->hclist)
505 for (nhost = 0; nhost < rc->num_hosts; nhost++)
507 hc = &rc->hclist[nhost];
509 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
511 GNUNET_free (rc->hclist);
514 /* Stop register hosts task if it is running */
515 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
517 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
518 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
520 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
522 GNUNET_SCHEDULER_cancel (rc->timeout_task);
523 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
525 if (NULL != rc->reg_handle)
527 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
528 rc->reg_handle = NULL;
530 if (NULL != rc->topology_operation)
532 GNUNET_TESTBED_operation_done (rc->topology_operation);
533 rc->topology_operation = NULL;
535 /* cancel any exiting operations */
536 GNUNET_assert (GNUNET_SYSERR !=
537 GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
538 &rcop_cleanup_iterator,
544 * Cancels the scheduled interrupt task
546 * @param rc the run context
549 cancel_interrupt_task (struct RunContext *rc)
551 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
552 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
557 * This callback will be called when all the operations are completed
560 * @param cls run context
563 wait_op_completion (void *cls)
565 struct RunContext *rc = cls;
566 struct RunContextOperation *rcop;
568 if ( (NULL == rc->cproc)
570 || (GNUNET_YES == rc->shutdown) )
572 if (NULL != rc->peers)
574 GNUNET_free (rc->peers);
579 if (NULL == rc->peers)
581 rc->shutdown = GNUNET_YES;
582 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
584 rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
585 GNUNET_assert (NULL != rcop->op);
586 DEBUG ("Shutting down peers\n");
587 rc->pstart_time = GNUNET_TIME_absolute_get ();
588 insert_rcop (rc, rcop);
592 rc->state = RC_PEERS_SHUTDOWN;
593 cancel_interrupt_task (rc);
599 * Task run upon interrupts (SIGINT, SIGTERM) and upon scheduler shutdown.
601 * @param cls the RunContext which has to be acted upon
602 * @param tc the scheduler task context
605 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
607 struct RunContext *rc = cls;
608 struct GNUNET_TESTBED_Controller *c = rc->c;
612 rc->interrupt_task = GNUNET_SCHEDULER_add_delayed
613 (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt, rc);
614 rc_cleanup_operations (rc);
615 if ( (GNUNET_NO == rc->shutdown)
617 && (0 != (size = GNUNET_CONTAINER_multihashmap32_size (c->opc_map))))
619 LOG (GNUNET_ERROR_TYPE_WARNING, "Shutdown postponed as there are "
620 "%u operations currently active\n", size);
621 c->opcq_empty_cb = &wait_op_completion;
622 c->opcq_empty_cls = rc;
625 wait_op_completion (rc);
630 * Function to return the string representation of the duration between current
631 * time and `pstart_time' in `RunContext'
633 * @param rc the RunContext
634 * @return the representation string; this is NOT reentrant
637 prof_time (struct RunContext *rc)
639 struct GNUNET_TIME_Relative ptime;
641 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
642 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
647 * Task for starting peers
649 * @param cls the RunHandle
650 * @param tc the task context from scheduler
653 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
655 struct RunContext *rc = cls;
656 struct RunContextOperation *rcop;
659 DEBUG ("Starting Peers\n");
660 rc->pstart_time = GNUNET_TIME_absolute_get ();
661 for (peer = 0; peer < rc->num_peers; peer++)
663 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
665 rcop->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
666 GNUNET_assert (NULL != rcop->op);
667 rcop->cls = rc->peers[peer];
668 insert_rcop (rc, rcop);
675 * Functions of this signature are called when a peer has been successfully
678 * @param cls the closure from GNUNET_TESTBED_peer_create()
679 * @param peer the handle for the created peer; NULL on any error during
681 * @param emsg NULL if peer is not NULL; else MAY contain the error description
684 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
686 struct RunContextOperation *rcop = cls;
687 struct RunContext *rc;
689 GNUNET_assert (NULL != rcop);
690 GNUNET_assert (NULL != (rc = rcop->rc));
691 remove_rcop (rc, rcop);
692 GNUNET_TESTBED_operation_done (rcop->op);
697 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
699 GNUNET_SCHEDULER_shutdown ();
702 rc->peers[rc->peer_count] = peer;
704 if (rc->peer_count < rc->num_peers)
706 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
707 rc->state = RC_PEERS_CREATED;
708 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
713 * call test master callback
715 * @param rc the RunContext
718 call_master (struct RunContext *rc)
720 GNUNET_SCHEDULER_cancel (rc->timeout_task);
721 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
722 if (NULL != rc->test_master)
723 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers,
724 rc->links_succeeded, rc->links_failed);
729 * Callbacks of this type are called when topology configuration is completed
731 * @param cls the operation closure given to
732 * GNUNET_TESTBED_overlay_configure_topology_va() and
733 * GNUNET_TESTBED_overlay_configure() calls
734 * @param nsuccess the number of successful overlay connects
735 * @param nfailures the number of overlay connects which failed
738 topology_completion_callback (void *cls, unsigned int nsuccess,
739 unsigned int nfailures)
741 struct RunContext *rc = cls;
743 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
744 GNUNET_TESTBED_operation_done (rc->topology_operation);
745 rc->topology_operation = NULL;
746 rc->links_succeeded = nsuccess;
747 rc->links_failed = nfailures;
748 rc->state = RC_READY;
754 * Function to create peers
756 * @param rc the RunContext
759 create_peers (struct RunContext *rc)
761 struct RunContextOperation *rcop;
764 DEBUG ("Creating peers\n");
765 rc->pstart_time = GNUNET_TIME_absolute_get ();
767 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
768 GNUNET_assert (NULL != rc->c);
770 for (peer = 0; peer < rc->num_peers; peer++)
772 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
775 GNUNET_TESTBED_peer_create (rc->c,
777 rc->num_hosts) ? rc->h : rc->hosts[peer %
779 rc->cfg, &peer_create_cb, rcop);
780 GNUNET_assert (NULL != rcop->op);
781 insert_rcop (rc, rcop);
787 * Signature of the event handler function called by the
788 * respective event controller.
791 * @param event information about the event
794 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
796 struct RunContext *rc = cls;
797 struct RunContextOperation *rcop;
799 if (RC_INIT == rc->state)
803 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
804 rcop = event->op_cls;
805 if (NULL != event->details.operation_finished.emsg)
807 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
808 GNUNET_SCHEDULER_shutdown ();
812 GNUNET_assert (event->op == rcop->op);
813 remove_rcop (rc, rcop);
814 GNUNET_TESTBED_operation_done (rcop->op);
816 if (rc->reg_hosts == rc->num_hosts)
818 rc->state = RC_LINKED;
824 GNUNET_SCHEDULER_shutdown ();
828 if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
830 if (NULL == (rcop = search_rcop (rc, event->op)))
832 remove_rcop (rc, rcop);
833 GNUNET_TESTBED_operation_done (rcop->op);
835 if ( (GNUNET_NO == rc->shutdown)
836 && (NULL != event->details.operation_finished.emsg) )
838 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
839 event->details.operation_finished.emsg);
840 GNUNET_SCHEDULER_shutdown ();
843 GNUNET_assert (GNUNET_YES == rc->shutdown);
847 case RC_PEERS_CREATED:
849 rc->state = RC_PEERS_SHUTDOWN;
850 GNUNET_free_non_null (rc->peers);
852 DEBUG ("Peers shut down in %s\n", prof_time (rc));
853 GNUNET_SCHEDULER_shutdown ();
861 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
862 rc->cc (rc->cc_cls, event);
863 if (GNUNET_TESTBED_ET_PEER_START != event->type)
865 if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
867 remove_rcop (rc, rcop);
868 GNUNET_TESTBED_operation_done (rcop->op);
871 if (rc->peer_count < rc->num_peers)
873 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
874 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
876 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
877 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
878 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
880 rc->topology_operation =
881 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
882 rc->peers, &rc->num_oc,
883 &topology_completion_callback,
887 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
889 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
891 GNUNET_assert (NULL != rc->topo_file);
892 rc->topology_operation =
893 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
894 rc->peers, &rc->num_oc,
895 &topology_completion_callback,
899 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
902 rc->topology_operation =
903 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
904 rc->peers, &rc->num_oc,
905 &topology_completion_callback,
908 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
909 if (NULL == rc->topology_operation)
910 LOG (GNUNET_ERROR_TYPE_WARNING,
911 "Not generating topology. Check number of peers\n");
914 DEBUG ("Creating overlay topology\n");
915 rc->pstart_time = GNUNET_TIME_absolute_get ();
919 rc->state = RC_READY;
925 * Task to register all hosts available in the global host list
927 * @param cls the RunContext
928 * @param tc the scheduler task context
931 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
935 * Callback which will be called to after a host registration succeeded or failed
937 * @param cls the closure
938 * @param emsg the error message; NULL if host registration is successful
941 host_registration_completion (void *cls, const char *emsg)
943 struct RunContext *rc = cls;
945 rc->reg_handle = NULL;
948 LOG (GNUNET_ERROR_TYPE_WARNING,
949 _("Host registration failed for a host. Error: %s\n"), emsg);
950 GNUNET_SCHEDULER_shutdown ();
953 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
958 * Task to register all hosts available in the global host list
960 * @param cls RunContext
961 * @param tc the scheduler task context
964 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
966 struct RunContext *rc = cls;
967 struct RunContextOperation *rcop;
970 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
971 if (rc->reg_hosts == rc->num_hosts)
973 DEBUG ("All hosts successfully registered\n");
975 for (slave = 0; slave < rc->num_hosts; slave++)
977 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
980 GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[slave],
982 GNUNET_assert (NULL != rcop->op);
983 insert_rcop (rc, rcop);
989 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
990 host_registration_completion, rc);
996 * Callback to signal successfull startup of the controller process
998 * @param cls the closure from GNUNET_TESTBED_controller_start()
999 * @param cfg the configuration with which the controller has been started;
1000 * NULL if status is not GNUNET_OK
1001 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1002 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1005 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1008 struct RunContext *rc = cls;
1009 uint64_t event_mask;
1011 if (status != GNUNET_OK)
1013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1014 _("Controller crash detected. Shutting down.\n"));
1015 GNUNET_SCHEDULER_shutdown ();
1018 GNUNET_CONFIGURATION_destroy (rc->cfg);
1019 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1020 event_mask = rc->event_mask;
1021 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1022 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1023 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
1024 event_mask |= GNUNET_TESTBED_ET_CONNECT;
1026 GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
1027 if (0 < rc->num_hosts)
1030 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
1033 rc->state = RC_LINKED;
1039 * Callback function invoked for each interface found.
1041 * @param cls closure
1042 * @param name name of the interface (can be NULL for unknown)
1043 * @param isDefault is this presumably the default interface
1044 * @param addr address of this interface (can be NULL for unknown or unassigned)
1045 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
1046 * @param netmask the network mask (can be NULL for unknown or unassigned))
1047 * @param addrlen length of the address
1048 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1051 netint_proc (void *cls, const char *name, int isDefault,
1052 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
1053 const struct sockaddr *netmask, socklen_t addrlen)
1055 struct RunContext *rc = cls;
1056 char hostip[NI_MAXHOST];
1059 if (sizeof (struct sockaddr_in) != addrlen)
1060 return GNUNET_OK; /* Only consider IPv4 for now */
1062 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
1063 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
1064 if (NULL == rc->trusted_ip)
1066 rc->trusted_ip = GNUNET_strdup (hostip);
1069 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
1070 GNUNET_free (rc->trusted_ip);
1071 rc->trusted_ip = buf;
1077 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1078 * inform whether the given host is habitable or not. The Handle returned by
1079 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1082 * @param host the host whose status is being reported; will be NULL if the host
1083 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1084 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1087 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
1090 struct CompatibilityCheckContext *hc = cls;
1091 struct RunContext *rc;
1092 struct GNUNET_TESTBED_Host **old_hosts;
1095 GNUNET_assert (NULL != (rc = hc->rc));
1097 GNUNET_assert (nhost <= rc->num_hosts);
1098 GNUNET_assert (host == rc->hosts[nhost]);
1100 if (GNUNET_NO == status)
1102 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1103 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1104 GNUNET_TESTBED_host_get_hostname (host));
1106 LOG (GNUNET_ERROR_TYPE_ERROR,
1107 _("Testbed cannot be started on localhost\n"));
1108 GNUNET_SCHEDULER_shutdown ();
1112 if (rc->reg_hosts < rc->num_hosts)
1114 GNUNET_free (rc->hclist);
1116 rc->h = rc->hosts[0];
1118 if (0 < rc->num_hosts)
1120 old_hosts = rc->hosts;
1122 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1123 memcpy (rc->hosts, &old_hosts[1],
1124 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1125 GNUNET_free (old_hosts);
1129 GNUNET_free (rc->hosts);
1132 GNUNET_TESTBED_host_resolve_ (rc->h);
1133 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1134 GNUNET_TESTBED_host_resolve_ (rc->hosts[nhost]);
1135 GNUNET_OS_network_interfaces_list (netint_proc, rc);
1136 if (NULL == rc->trusted_ip)
1137 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1139 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1140 &controller_status_cb, rc);
1141 GNUNET_free (rc->trusted_ip);
1142 rc->trusted_ip = NULL;
1143 if (NULL == rc->cproc)
1145 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1146 GNUNET_SCHEDULER_shutdown ();
1152 * Task run upon timeout while setting up the testbed
1154 * @param cls the RunContext
1155 * @param tc the task context
1158 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1160 struct RunContext *rc = cls;
1162 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1163 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1164 GNUNET_SCHEDULER_shutdown ();
1165 if (NULL != rc->test_master)
1166 rc->test_master (rc->test_master_cls, 0, NULL, 0, 0);
1167 rc->test_master = NULL;
1172 * Convenience method for running a testbed with
1173 * a single call. Underlay and overlay topology
1174 * are configured using the "UNDERLAY" and "OVERLAY"
1175 * options in the "[testbed]" section of the configuration\
1176 * (with possible options given in "UNDERLAY_XXX" and/or
1179 * The testbed is to be terminated using a call to
1180 * "GNUNET_SCHEDULER_shutdown".
1182 * @param host_filename name of the file with the 'hosts', NULL
1183 * to run everything on 'localhost'
1184 * @param cfg configuration to use (for testbed, controller and peers)
1185 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1186 * @param event_mask bit mask with set of events to call 'cc' for;
1187 * or-ed values of "1LL" shifted by the
1188 * respective 'enum GNUNET_TESTBED_EventType'
1189 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1190 * @param cc controller callback to invoke on events; This callback is called
1191 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1192 * set in the event_mask as this is the only way get access to the
1193 * handle of each peer
1194 * @param cc_cls closure for cc
1195 * @param test_master this callback will be called once the test is ready
1196 * @param test_master_cls closure for 'test_master'.
1199 GNUNET_TESTBED_run (const char *host_filename,
1200 const struct GNUNET_CONFIGURATION_Handle *cfg,
1201 unsigned int num_peers, uint64_t event_mask,
1202 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1203 GNUNET_TESTBED_TestMaster test_master,
1204 void *test_master_cls)
1206 struct RunContext *rc;
1208 struct CompatibilityCheckContext *hc;
1209 struct GNUNET_TIME_Relative timeout;
1210 unsigned long long random_links;
1214 GNUNET_assert (num_peers > 0);
1215 rc = GNUNET_malloc (sizeof (struct RunContext));
1216 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1218 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1220 if (0 == rc->num_hosts)
1222 LOG (GNUNET_ERROR_TYPE_WARNING,
1223 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1227 if (NULL != host_filename)
1230 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1232 if (0 == rc->num_hosts)
1234 LOG (GNUNET_ERROR_TYPE_WARNING,
1235 _("No hosts loaded. Need at least one host\n"));
1240 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1242 rc->num_peers = num_peers;
1243 rc->event_mask = event_mask;
1245 rc->cc_cls = cc_cls;
1246 rc->test_master = test_master;
1247 rc->test_master_cls = test_master_cls;
1248 rc->state = RC_INIT;
1249 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1251 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1252 "OVERLAY_TOPOLOGY", &topology))
1254 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1256 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1259 ("Specified topology must be supported by testbed"));
1261 GNUNET_free (topology);
1263 switch (rc->topology)
1265 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1266 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1267 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1269 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1270 "OVERLAY_RANDOM_LINKS",
1273 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1274 * option to be set to the number of random links to be established */
1275 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1276 "OVERLAY_RANDOM_LINKS");
1279 if (random_links > UINT32_MAX)
1281 GNUNET_break (0); /* Too big number */
1284 rc->random_links = (unsigned int) random_links;
1286 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1288 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1289 "OVERLAY_TOPOLOGY_FILE",
1292 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1293 "OVERLAY_TOPOLOGY_FILE");
1298 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1300 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1301 "OVERLAY_RANDOM_LINKS"))
1302 LOG (GNUNET_ERROR_TYPE_WARNING,
1303 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1306 if (0 != rc->num_hosts)
1308 rc->hclist = GNUNET_malloc (sizeof (struct CompatibilityCheckContext)
1310 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1312 hc = &rc->hclist[nhost];
1315 hc->h = GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1316 &host_habitable_cb, hc);
1320 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1322 hc = &rc->hclist[nhost];
1324 GNUNET_TESTBED_is_host_habitable_cancel (hc->h);
1326 GNUNET_free (rc->hclist);
1334 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1335 &controller_status_cb, rc);
1336 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1340 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1341 DEFAULT_SETUP_TIMEOUT);
1343 rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1345 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1346 rc->interrupt_task =
1347 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1353 GNUNET_TESTBED_host_destroy (rc->h);
1354 if (NULL != rc->hosts)
1356 for (hid = 0; hid < rc->num_hosts; hid++)
1357 if (NULL != rc->hosts[hid])
1358 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1359 GNUNET_free (rc->hosts);
1361 if (NULL != rc->cfg)
1362 GNUNET_CONFIGURATION_destroy (rc->cfg);
1367 /* end of testbed_api_testbed.c */