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_peers.h"
32 #include "testbed_api_hosts.h"
33 #include "testbed_api_topology.h"
36 * Generic loggins shorthand
38 #define LOG(kind,...) \
39 GNUNET_log_from (kind, "testbed-api-testbed", __VA_ARGS__)
42 * Debug logging shortcut
45 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48 * The default setup timeout in seconds
50 #define DEFAULT_SETUP_TIMEOUT 300
53 * Context information for the operation we start
55 struct RunContextOperation
58 * The testbed operation handle
60 struct GNUNET_TESTBED_Operation *op;
63 * Context information for GNUNET_TESTBED_run()
65 struct RunContext *rc;
76 * States of RunContext
86 * Controllers on given hosts started and linked
96 * The testbed run is ready and the master callback can be called now. At this
97 * time the peers are all started and if a topology is provided in the
98 * configuration the topology would have been attempted
103 /* * Peers are stopped */
105 /* RC_PEERS_STOPPED, */
108 /* * Peers are destroyed */
110 /* RC_PEERS_DESTROYED */
113 * All peers shutdown (stopped and destroyed)
125 * The controller handle
127 struct GNUNET_TESTBED_Controller *c;
130 * The configuration of the controller. This is based on the cfg given to the
131 * function GNUNET_TESTBED_run(). We also use this config as a template while
134 struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Handle to the host on which the controller runs
139 struct GNUNET_TESTBED_Host *h;
142 * The handle to the controller process
144 struct GNUNET_TESTBED_ControllerProc *cproc;
147 * The callback to use as controller callback
149 GNUNET_TESTBED_ControllerCallback cc;
152 * The pointer to the controller callback
157 * The trusted IP string
162 * TestMaster callback to call when testbed initialization is done
164 GNUNET_TESTBED_TestMaster test_master;
167 * The closure for the TestMaster callback
169 void *test_master_cls;
172 * A hashmap for operations started by us
174 struct GNUNET_CONTAINER_MultiHashMap32 *rcop_map;
177 * An array of hosts loaded from the hostkeys file
179 struct GNUNET_TESTBED_Host **hosts;
182 * The handle for whether a host is habitable or not
184 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
187 * Array of peers which we create
189 struct GNUNET_TESTBED_Peer **peers;
192 * The topology generation operation. Will be null if no topology is set in
195 struct GNUNET_TESTBED_Operation *topology_operation;
198 * The file containing topology data. Only used if the topology is set to 'FROM_FILE'
203 * Host registration handle
205 struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
208 * Profiling start time
210 struct GNUNET_TIME_Absolute pstart_time;
213 * Host registration task
215 GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
218 * Task to be run while shutting down
220 GNUNET_SCHEDULER_TaskIdentifier shutdown_run_task;
223 * Task to be run of a timeout
225 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
228 * Task run upon shutdown interrupts
230 GNUNET_SCHEDULER_TaskIdentifier interrupt_task;
233 * The event mask for the controller
238 * State of this context
243 * The topology which has to be achieved with the peers started in this context
245 enum GNUNET_TESTBED_TopologyOption topology;
248 * Have we already shutdown
253 * Number of hosts in the given host file
255 unsigned int num_hosts;
258 * Number of registered hosts. Also used as a counter while checking
259 * habitabillity of hosts
261 unsigned int reg_hosts;
264 * Current peer count for an operation; Set this to 0 and increment for each
265 * successful operation on a peer
267 unsigned int peer_count;
270 * number of peers to start
272 unsigned int num_peers;
275 * Expected overlay connects. Should be zero if no topology is relavant
280 * Number of random links to established
282 unsigned int random_links;
285 * the number of overlay link connection attempts that succeeded
287 unsigned int links_succeeded;
290 * the number of overlay link connection attempts that failed
292 unsigned int links_failed;
298 * Return a 32-bit key from a pointer
300 * @param rcop the pointer
304 rcop_key (void *rcop)
306 return * ((uint32_t *) &rcop);
311 * Context information used for finding a pointer in the rcop_map
316 * The operation pointer to look for
318 struct GNUNET_TESTBED_Operation *query;
321 * The Run context operation which has the operation being queried
323 struct RunContextOperation *result;
328 * Iterator for searching over the elements matching a given query
330 * @param cls the SearchContext
331 * @param key the 32-bit key
332 * @param value the RunContextOperation element
333 * @return GNUNET_YES to continue iteration; GNUNET_NO to cancel it
336 search_iterator (void *cls, uint32_t key, void *value)
338 struct RunContextOperation *rcop = value;
339 struct SearchContext *sc = cls;
341 GNUNET_assert (NULL != rcop);
342 if (sc->query == rcop->op)
344 GNUNET_assert (NULL == sc->result);
353 * Initiate a search for the given operation in the rcop_map
355 * @param rc the RunContext whose rcop_map will be searched for the given
357 * @param op the given operation to search for
358 * @return the matching RunContextOperation if found; NULL if not
360 static struct RunContextOperation *
361 search_rcop (struct RunContext *rc, struct GNUNET_TESTBED_Operation *op)
363 struct SearchContext sc;
368 GNUNET_CONTAINER_multihashmap32_get_multiple (rc->rcop_map,
373 GNUNET_assert (NULL != sc.result);
381 * Insert an RunContextOperation into the rcop_map of the given RunContext
383 * @param rc the RunContext into whose map is to be used for insertion
384 * @param rcop the RunContextOperation to insert
387 insert_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
389 GNUNET_assert (GNUNET_OK ==
390 GNUNET_CONTAINER_multihashmap32_put (rc->rcop_map,
391 rcop_key (rcop->op), rcop,
392 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
397 * Remove a RunContextOperation from the rcop_map of the given RunContext
399 * @param rc the RunContext from whose map the given RunContextOperaton has to
401 * @param rcop the RunContextOperation
404 remove_rcop (struct RunContext *rc, struct RunContextOperation *rcop)
406 GNUNET_assert (GNUNET_YES ==
407 GNUNET_CONTAINER_multihashmap32_remove (rc->rcop_map,
413 * Assuming all peers have been destroyed cleanup run handle
415 * @param cls the run handle
416 * @param tc the task context from scheduler
419 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
421 struct RunContext *rc = cls;
424 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rc->register_hosts_task);
425 GNUNET_assert (NULL == rc->reg_handle);
426 GNUNET_assert (NULL == rc->peers);
427 GNUNET_assert (NULL == rc->hc_handles);
428 GNUNET_assert (RC_PEERS_SHUTDOWN == rc->state);
429 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (rc->rcop_map));
430 GNUNET_CONTAINER_multihashmap32_destroy (rc->rcop_map);
432 GNUNET_TESTBED_controller_disconnect (rc->c);
433 if (NULL != rc->cproc)
434 GNUNET_TESTBED_controller_stop (rc->cproc);
436 GNUNET_TESTBED_host_destroy (rc->h);
437 for (hid = 0; hid < rc->num_hosts; hid++)
438 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
439 GNUNET_free_non_null (rc->hosts);
441 GNUNET_CONFIGURATION_destroy (rc->cfg);
442 GNUNET_free_non_null (rc->topo_file);
443 GNUNET_free_non_null (rc->trusted_ip);
449 * Iterator for cleaning up elements from rcop_map
451 * @param cls the RunContext
452 * @param key the 32-bit key
453 * @param value the RunContextOperation element
454 * @return always GNUNET_YES
457 rcop_cleanup_iterator (void *cls, uint32_t key, void *value)
459 struct RunContext *rc = cls;
460 struct RunContextOperation *rcop = value;
462 GNUNET_assert (rc == rcop->rc);
463 remove_rcop (rc, rcop);
464 GNUNET_TESTBED_operation_done (rcop->op);
471 * Frees memory, closes pending operations, cancels actives tasks of the given
474 * @param rc the RunContext
477 cleanup (struct RunContext *rc)
481 if (NULL != rc->hc_handles)
483 for (nhost = 0; nhost < rc->num_hosts; nhost++)
484 if (NULL != rc->hc_handles[nhost])
485 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
486 GNUNET_free (rc->hc_handles);
487 rc->hc_handles = NULL;
489 /* Stop register hosts task if it is running */
490 if (GNUNET_SCHEDULER_NO_TASK != rc->register_hosts_task)
492 GNUNET_SCHEDULER_cancel (rc->register_hosts_task);
493 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
495 if (GNUNET_SCHEDULER_NO_TASK != rc->timeout_task)
497 GNUNET_SCHEDULER_cancel (rc->timeout_task);
498 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
500 if (GNUNET_SCHEDULER_NO_TASK != rc->interrupt_task)
502 GNUNET_SCHEDULER_cancel (rc->interrupt_task);
503 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
505 if (NULL != rc->reg_handle)
507 GNUNET_TESTBED_cancel_registration (rc->reg_handle);
508 rc->reg_handle = NULL;
510 if (NULL != rc->topology_operation)
512 GNUNET_TESTBED_operation_done (rc->topology_operation);
513 rc->topology_operation = NULL;
515 /* cancel any exiting operations */
516 GNUNET_assert (GNUNET_SYSERR !=
517 GNUNET_CONTAINER_multihashmap32_iterate (rc->rcop_map,
518 &rcop_cleanup_iterator,
524 * Stops the testbed run and releases any used resources
526 * @param cls the tesbed run handle
527 * @param tc the task context from scheduler
530 shutdown_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 struct RunContext *rc = cls;
533 struct RunContextOperation *rcop;
535 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task);
536 rc->shutdown_run_task = GNUNET_SCHEDULER_NO_TASK;
537 GNUNET_assert (GNUNET_NO == rc->shutdown);
538 rc->shutdown = GNUNET_YES;
542 if (NULL != rc->peers)
544 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
546 rcop->op = GNUNET_TESTBED_shutdown_peers (rc->c, rcop, NULL, NULL);
547 GNUNET_assert (NULL != rcop->op);
548 DEBUG ("Shutting down peers\n");
549 rc->pstart_time = GNUNET_TIME_absolute_get ();
550 insert_rcop (rc, rcop);
554 rc->state = RC_PEERS_SHUTDOWN; /* No peers are present so we consider the
555 * state where all peers are SHUTDOWN */
556 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
561 * Function to shutdown now
563 * @param rc the RunContext
566 shutdown_now (struct RunContext *rc)
568 if (GNUNET_YES == rc->shutdown)
570 if (GNUNET_SCHEDULER_NO_TASK != rc->shutdown_run_task)
571 GNUNET_SCHEDULER_cancel (rc->shutdown_run_task);
572 GNUNET_SCHEDULER_shutdown (); /* Trigger shutdown in programs using this API */
573 rc->shutdown_run_task = GNUNET_SCHEDULER_add_now (&shutdown_run, rc);
578 * Task run upon any interrupt. Common ones are SIGINT & SIGTERM.
580 * @param cls the RunContext which has to be acted upon
581 * @param tc the scheduler task context
584 interrupt (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
586 struct RunContext *rc = cls;
588 rc->interrupt_task = GNUNET_SCHEDULER_NO_TASK;
594 * Function to return the string representation of the duration between current
595 * time and `pstart_time' in `RunContext'
597 * @param rc the RunContext
598 * @return the representation string; this is NOT reentrant
601 prof_time (struct RunContext *rc)
603 struct GNUNET_TIME_Relative ptime;
605 ptime = GNUNET_TIME_absolute_get_duration (rc->pstart_time);
606 return GNUNET_STRINGS_relative_time_to_string (ptime, GNUNET_YES);
611 * Task for starting peers
613 * @param cls the RunHandle
614 * @param tc the task context from scheduler
617 start_peers_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
619 struct RunContext *rc = cls;
620 struct RunContextOperation *rcop;
623 DEBUG ("Starting Peers\n");
624 rc->pstart_time = GNUNET_TIME_absolute_get ();
625 for (peer = 0; peer < rc->num_peers; peer++)
627 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
629 rcop->op = GNUNET_TESTBED_peer_start (NULL, rc->peers[peer], NULL, NULL);
630 GNUNET_assert (NULL != rcop->op);
631 rcop->cls = rc->peers[peer];
632 insert_rcop (rc, rcop);
639 * Functions of this signature are called when a peer has been successfully
642 * @param cls the closure from GNUNET_TESTBED_peer_create()
643 * @param peer the handle for the created peer; NULL on any error during
645 * @param emsg NULL if peer is not NULL; else MAY contain the error description
648 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
650 struct RunContextOperation *rcop = cls;
651 struct RunContext *rc;
653 GNUNET_assert (NULL != rcop);
654 GNUNET_assert (NULL != (rc = rcop->rc));
655 remove_rcop (rc, rcop);
656 GNUNET_TESTBED_operation_done (rcop->op);
661 LOG (GNUNET_ERROR_TYPE_ERROR, "Error while creating a peer: %s\n",
666 rc->peers[rc->peer_count] = peer;
668 if (rc->peer_count < rc->num_peers)
670 DEBUG ("%u peers created in %s\n", rc->num_peers, prof_time (rc));
671 rc->state = RC_PEERS_CREATED;
672 GNUNET_SCHEDULER_add_now (&start_peers_task, rc);
677 * call test master callback
679 * @param rc the RunContext
682 call_master (struct RunContext *rc)
684 GNUNET_SCHEDULER_cancel (rc->timeout_task);
685 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
686 if (NULL != rc->test_master)
687 rc->test_master (rc->test_master_cls, rc->num_peers, rc->peers,
688 rc->links_succeeded, rc->links_failed);
693 * Callbacks of this type are called when topology configuration is completed
695 * @param cls the operation closure given to
696 * GNUNET_TESTBED_overlay_configure_topology_va() and
697 * GNUNET_TESTBED_overlay_configure() calls
698 * @param nsuccess the number of successful overlay connects
699 * @param nfailures the number of overlay connects which failed
702 topology_completion_callback (void *cls, unsigned int nsuccess,
703 unsigned int nfailures)
705 struct RunContext *rc = cls;
707 DEBUG ("Overlay topology generated in %s\n", prof_time (rc));
708 GNUNET_TESTBED_operation_done (rc->topology_operation);
709 rc->topology_operation = NULL;
710 rc->links_succeeded = nsuccess;
711 rc->links_failed = nfailures;
712 rc->state = RC_READY;
718 * Function to create peers
720 * @param rc the RunContext
723 create_peers (struct RunContext *rc)
725 struct RunContextOperation *rcop;
728 DEBUG ("Creating peers\n");
729 rc->pstart_time = GNUNET_TIME_absolute_get ();
731 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * rc->num_peers);
732 GNUNET_assert (NULL != rc->c);
734 for (peer = 0; peer < rc->num_peers; peer++)
736 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
739 GNUNET_TESTBED_peer_create (rc->c,
741 rc->num_hosts) ? rc->h : rc->hosts[peer %
743 rc->cfg, &peer_create_cb, rcop);
744 GNUNET_assert (NULL != rcop->op);
745 insert_rcop (rc, rcop);
751 * Signature of the event handler function called by the
752 * respective event controller.
755 * @param event information about the event
758 event_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
760 struct RunContext *rc = cls;
761 struct RunContextOperation *rcop;
763 if (RC_INIT == rc->state)
767 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
768 rcop = event->op_cls;
769 if (NULL != event->details.operation_finished.emsg)
771 LOG (GNUNET_ERROR_TYPE_ERROR, _("Linking controllers failed. Exiting"));
776 GNUNET_assert (event->op == rcop->op);
777 remove_rcop (rc, rcop);
778 GNUNET_TESTBED_operation_done (rcop->op);
780 if (rc->reg_hosts == rc->num_hosts)
782 rc->state = RC_LINKED;
792 if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
794 if (NULL == (rcop = search_rcop (rc, event->op)))
796 remove_rcop (rc, rcop);
797 GNUNET_TESTBED_operation_done (rcop->op);
799 if ( (GNUNET_NO == rc->shutdown)
800 && (NULL != event->details.operation_finished.emsg) )
802 LOG (GNUNET_ERROR_TYPE_ERROR, "A operation has failed with error: %s\n",
803 event->details.operation_finished.emsg);
807 GNUNET_assert (GNUNET_YES == rc->shutdown);
811 case RC_PEERS_CREATED:
813 rc->state = RC_PEERS_SHUTDOWN;
814 GNUNET_free_non_null (rc->peers);
816 DEBUG ("Peers shut down in %s\n", prof_time (rc));
817 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
825 if ((0 != (rc->event_mask & (1LL << event->type))) && (NULL != rc->cc))
826 rc->cc (rc->cc_cls, event);
827 if (GNUNET_TESTBED_ET_PEER_START != event->type)
829 if (NULL == (rcop = search_rcop (rc, event->op))) /* Not our operation */
831 remove_rcop (rc, rcop);
832 GNUNET_TESTBED_operation_done (rcop->op);
835 if (rc->peer_count < rc->num_peers)
837 DEBUG ("%u peers started in %s\n", rc->num_peers, prof_time (rc));
838 if (GNUNET_TESTBED_TOPOLOGY_NONE != rc->topology)
840 if ((GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI == rc->topology) ||
841 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING == rc->topology) ||
842 (GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD == rc->topology))
844 rc->topology_operation =
845 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
846 rc->peers, &rc->num_oc,
847 &topology_completion_callback,
851 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
853 else if (GNUNET_TESTBED_TOPOLOGY_FROM_FILE == rc->topology)
855 GNUNET_assert (NULL != rc->topo_file);
856 rc->topology_operation =
857 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
858 rc->peers, &rc->num_oc,
859 &topology_completion_callback,
863 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
866 rc->topology_operation =
867 GNUNET_TESTBED_overlay_configure_topology (NULL, rc->num_peers,
868 rc->peers, &rc->num_oc,
869 &topology_completion_callback,
872 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
873 if (NULL == rc->topology_operation)
874 LOG (GNUNET_ERROR_TYPE_WARNING,
875 "Not generating topology. Check number of peers\n");
878 DEBUG ("Creating overlay topology\n");
879 rc->pstart_time = GNUNET_TIME_absolute_get ();
883 rc->state = RC_READY;
889 * Task to register all hosts available in the global host list
891 * @param cls the RunContext
892 * @param tc the scheduler task context
895 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
899 * Callback which will be called to after a host registration succeeded or failed
901 * @param cls the closure
902 * @param emsg the error message; NULL if host registration is successful
905 host_registration_completion (void *cls, const char *emsg)
907 struct RunContext *rc = cls;
909 rc->reg_handle = NULL;
912 LOG (GNUNET_ERROR_TYPE_WARNING,
913 _("Host registration failed for a host. Error: %s\n"), emsg);
917 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
922 * Task to register all hosts available in the global host list
924 * @param cls RunContext
925 * @param tc the scheduler task context
928 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
930 struct RunContext *rc = cls;
931 struct RunContextOperation *rcop;
934 rc->register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
935 if (rc->reg_hosts == rc->num_hosts)
937 DEBUG ("All hosts successfully registered\n");
939 for (slave = 0; slave < rc->num_hosts; slave++)
941 rcop = GNUNET_malloc (sizeof (struct RunContextOperation));
944 GNUNET_TESTBED_controller_link (rcop, rc->c, rc->hosts[slave],
945 rc->h, rc->cfg, GNUNET_YES);
946 GNUNET_assert (NULL != rcop->op);
947 insert_rcop (rc, rcop);
953 GNUNET_TESTBED_register_host (rc->c, rc->hosts[rc->reg_hosts],
954 host_registration_completion, rc);
960 * Callback to signal successfull startup of the controller process
962 * @param cls the closure from GNUNET_TESTBED_controller_start()
963 * @param cfg the configuration with which the controller has been started;
964 * NULL if status is not GNUNET_OK
965 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
966 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
969 controller_status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
972 struct RunContext *rc = cls;
975 if (status != GNUNET_OK)
977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
978 _("Controller crash detected. Shutting down.\n"));
981 if (NULL != rc->peers)
983 GNUNET_free (rc->peers);
986 if (GNUNET_YES == rc->shutdown)
988 rc->state = RC_PEERS_SHUTDOWN;
989 GNUNET_SCHEDULER_add_now (&cleanup_task, rc);
995 GNUNET_CONFIGURATION_destroy (rc->cfg);
996 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
997 event_mask = rc->event_mask;
998 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
999 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1000 if (rc->topology < GNUNET_TESTBED_TOPOLOGY_NONE)
1001 event_mask |= GNUNET_TESTBED_ET_CONNECT;
1003 GNUNET_TESTBED_controller_connect (rc->h, event_mask, &event_cb, rc);
1004 if (0 < rc->num_hosts)
1007 rc->register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, rc);
1010 rc->state = RC_LINKED;
1016 * Callback function invoked for each interface found.
1018 * @param cls closure
1019 * @param name name of the interface (can be NULL for unknown)
1020 * @param isDefault is this presumably the default interface
1021 * @param addr address of this interface (can be NULL for unknown or unassigned)
1022 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
1023 * @param netmask the network mask (can be NULL for unknown or unassigned))
1024 * @param addrlen length of the address
1025 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
1028 netint_proc (void *cls, const char *name, int isDefault,
1029 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
1030 const struct sockaddr *netmask, socklen_t addrlen)
1032 struct RunContext *rc = cls;
1033 char hostip[NI_MAXHOST];
1036 if (sizeof (struct sockaddr_in) != addrlen)
1037 return GNUNET_OK; /* Only consider IPv4 for now */
1039 getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
1040 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "getnameinfo");
1041 if (NULL == rc->trusted_ip)
1043 rc->trusted_ip = GNUNET_strdup (hostip);
1046 (void) GNUNET_asprintf (&buf, "%s; %s", rc->trusted_ip, hostip);
1047 GNUNET_free (rc->trusted_ip);
1048 rc->trusted_ip = buf;
1054 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1055 * inform whether the given host is habitable or not. The Handle returned by
1056 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1059 * @param host the host whose status is being reported; will be NULL if the host
1060 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1061 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1064 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host,
1067 struct RunContext *rc = cls;
1068 struct GNUNET_TESTBED_Host **old_hosts;
1071 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1073 if (host == rc->hosts[nhost])
1076 GNUNET_assert (nhost != rc->num_hosts);
1077 rc->hc_handles[nhost] = NULL;
1078 if (GNUNET_NO == status)
1080 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1081 LOG (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1082 GNUNET_TESTBED_host_get_hostname (host));
1084 LOG (GNUNET_ERROR_TYPE_ERROR,
1085 _("Testbed cannot be started on localhost\n"));
1090 if (rc->reg_hosts < rc->num_hosts)
1092 GNUNET_free (rc->hc_handles);
1093 rc->hc_handles = NULL;
1094 rc->h = rc->hosts[0];
1096 if (0 < rc->num_hosts)
1098 old_hosts = rc->hosts;
1100 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts);
1101 memcpy (rc->hosts, &old_hosts[1],
1102 (sizeof (struct GNUNET_TESTBED_Host *) * rc->num_hosts));
1103 GNUNET_free (old_hosts);
1107 GNUNET_free (rc->hosts);
1110 GNUNET_OS_network_interfaces_list (netint_proc, rc);
1111 if (NULL == rc->trusted_ip)
1112 rc->trusted_ip = GNUNET_strdup ("127.0.0.1");
1114 GNUNET_TESTBED_controller_start (rc->trusted_ip, rc->h,
1115 &controller_status_cb, rc);
1116 GNUNET_free (rc->trusted_ip);
1117 rc->trusted_ip = NULL;
1118 if (NULL == rc->cproc)
1120 LOG (GNUNET_ERROR_TYPE_ERROR, _("Cannot start the master controller"));
1127 * Task run upon timeout while setting up the testbed
1129 * @param cls the RunContext
1130 * @param tc the task context
1133 timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1135 struct RunContext *rc = cls;
1137 rc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1138 LOG (GNUNET_ERROR_TYPE_ERROR, _("Shutting down testbed due to timeout while setup.\n"));
1140 if (NULL != rc->test_master)
1141 rc->test_master (rc->test_master_cls, 0, NULL, 0, 0);
1142 rc->test_master = NULL;
1147 * Convenience method for running a testbed with
1148 * a single call. Underlay and overlay topology
1149 * are configured using the "UNDERLAY" and "OVERLAY"
1150 * options in the "[testbed]" section of the configuration\
1151 * (with possible options given in "UNDERLAY_XXX" and/or
1154 * The testbed is to be terminated using a call to
1155 * "GNUNET_SCHEDULER_shutdown".
1157 * @param host_filename name of the file with the 'hosts', NULL
1158 * to run everything on 'localhost'
1159 * @param cfg configuration to use (for testbed, controller and peers)
1160 * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1161 * @param event_mask bit mask with set of events to call 'cc' for;
1162 * or-ed values of "1LL" shifted by the
1163 * respective 'enum GNUNET_TESTBED_EventType'
1164 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1165 * @param cc controller callback to invoke on events; This callback is called
1166 * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
1167 * set in the event_mask as this is the only way get access to the
1168 * handle of each peer
1169 * @param cc_cls closure for cc
1170 * @param test_master this callback will be called once the test is ready
1171 * @param test_master_cls closure for 'test_master'.
1174 GNUNET_TESTBED_run (const char *host_filename,
1175 const struct GNUNET_CONFIGURATION_Handle *cfg,
1176 unsigned int num_peers, uint64_t event_mask,
1177 GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
1178 GNUNET_TESTBED_TestMaster test_master,
1179 void *test_master_cls)
1181 struct RunContext *rc;
1183 struct GNUNET_TIME_Relative timeout;
1184 unsigned long long random_links;
1188 GNUNET_assert (num_peers > 0);
1189 rc = GNUNET_malloc (sizeof (struct RunContext));
1190 rc->cfg = GNUNET_CONFIGURATION_dup (cfg);
1192 rc->num_hosts = GNUNET_TESTBED_hosts_load_from_loadleveler (rc->cfg,
1194 if (0 == rc->num_hosts)
1196 LOG (GNUNET_ERROR_TYPE_WARNING,
1197 _("No hosts loaded from LoadLeveler. Need at least one host\n"));
1201 if (NULL != host_filename)
1204 GNUNET_TESTBED_hosts_load_from_file (host_filename, rc->cfg,
1206 if (0 == rc->num_hosts)
1208 LOG (GNUNET_ERROR_TYPE_WARNING,
1209 _("No hosts loaded. Need at least one host\n"));
1214 rc->h = GNUNET_TESTBED_host_create (NULL, NULL, rc->cfg, 0);
1216 rc->num_peers = num_peers;
1217 rc->event_mask = event_mask;
1219 rc->cc_cls = cc_cls;
1220 rc->test_master = test_master;
1221 rc->test_master_cls = test_master_cls;
1222 rc->state = RC_INIT;
1223 rc->topology = GNUNET_TESTBED_TOPOLOGY_NONE;
1225 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1226 "OVERLAY_TOPOLOGY", &topology))
1228 if (GNUNET_NO == GNUNET_TESTBED_topology_get_ (&rc->topology, topology))
1230 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "testbed",
1233 ("Specified topology must be supported by testbed"));
1235 GNUNET_free (topology);
1237 switch (rc->topology)
1239 case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
1240 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING:
1241 case GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD:
1243 GNUNET_CONFIGURATION_get_value_number (rc->cfg, "testbed",
1244 "OVERLAY_RANDOM_LINKS",
1247 /* OVERLAY option RANDOM & SMALL_WORLD_RING requires OVERLAY_RANDOM_LINKS
1248 * option to be set to the number of random links to be established */
1249 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1250 "OVERLAY_RANDOM_LINKS");
1253 if (random_links > UINT32_MAX)
1255 GNUNET_break (0); /* Too big number */
1258 rc->random_links = (unsigned int) random_links;
1260 case GNUNET_TESTBED_TOPOLOGY_FROM_FILE:
1262 GNUNET_CONFIGURATION_get_value_string (rc->cfg, "testbed",
1263 "OVERLAY_TOPOLOGY_FILE",
1266 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "testbed",
1267 "OVERLAY_TOPOLOGY_FILE");
1271 /* Warn if OVERLAY_RANDOM_LINKS is present that it will be ignored */
1273 GNUNET_CONFIGURATION_have_value (rc->cfg, "testbed",
1274 "OVERLAY_RANDOM_LINKS"))
1275 LOG (GNUNET_ERROR_TYPE_WARNING,
1276 "Ignoring value of `OVERLAY_RANDOM_LINKS' in given configuration\n");
1279 if (0 != rc->num_hosts)
1282 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostHabitableCheckHandle *)
1284 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1287 (rc->hc_handles[nhost] =
1288 GNUNET_TESTBED_is_host_habitable (rc->hosts[nhost], rc->cfg,
1289 &host_habitable_cb, rc)))
1292 for (nhost = 0; nhost < rc->num_hosts; nhost++)
1293 if (NULL != rc->hc_handles[nhost])
1294 GNUNET_TESTBED_is_host_habitable_cancel (rc->hc_handles[nhost]);
1295 GNUNET_free (rc->hc_handles);
1296 rc->hc_handles = NULL;
1303 GNUNET_TESTBED_controller_start ("127.0.0.1", rc->h,
1304 &controller_status_cb, rc);
1305 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1309 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
1310 DEFAULT_SETUP_TIMEOUT);
1312 rc->rcop_map = GNUNET_CONTAINER_multihashmap32_create (256);
1314 GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, rc);
1315 rc->interrupt_task =
1316 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &interrupt,
1322 GNUNET_TESTBED_host_destroy (rc->h);
1323 if (NULL != rc->hosts)
1325 for (hid = 0; hid < rc->num_hosts; hid++)
1326 if (NULL != rc->hosts[hid])
1327 GNUNET_TESTBED_host_destroy (rc->hosts[hid]);
1328 GNUNET_free (rc->hosts);
1330 if (NULL != rc->cfg)
1331 GNUNET_CONFIGURATION_destroy (rc->cfg);
1336 /* end of testbed_api_testbed.c */