2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api.c
23 * @brief API for accessing the GNUnet testing service.
24 * This library is supposed to make it easier to write
25 * testcases and script large-scale benchmarks.
26 * @author Christian Grothoff
27 * @author Sree Harsha Totakura
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_hello_lib.h"
40 #include "testbed_api.h"
41 #include "testbed_api_hosts.h"
44 * Generic logging shorthand
46 #define LOG(kind, ...) \
47 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
52 #define LOG_DEBUG(...) \
53 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
56 * Relative time seconds shorthand
58 #define TIME_REL_SECS(sec) \
59 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
63 * Default server message sending retry timeout
65 #define TIMEOUT_REL TIME_REL_SECS(1)
69 * The message queue for sending messages to the controller service
74 * The message to be sent
76 struct GNUNET_MessageHeader *msg;
79 * next pointer for DLL
81 struct MessageQueue *next;
84 * prev pointer for DLL
86 struct MessageQueue *prev;
91 * Structure for a controller link
96 * The next ptr for DLL
98 struct ControllerLink *next;
101 * The prev ptr for DLL
103 struct ControllerLink *prev;
106 * The host which will be referred in the peer start request. This is the
107 * host where the peer should be started
109 struct GNUNET_TESTBED_Host *delegated_host;
112 * The host which will contacted to delegate the peer start request
114 struct GNUNET_TESTBED_Host *slave_host;
117 * The configuration to be used to connect to slave host
119 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
122 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
123 * if we are just allowed to use the slave via TCP/IP
130 * Handle to interact with a GNUnet testbed controller. Each
131 * controller has at least one master handle which is created when the
132 * controller is created; this master handle interacts with the
133 * controller process, destroying it destroys the controller (by
134 * closing stdin of the controller process). Additionally,
135 * controllers can interact with each other (in a P2P fashion); those
136 * links are established via TCP/IP on the controller's service port.
138 struct GNUNET_TESTBED_Controller
142 * The host where the controller is running
144 struct GNUNET_TESTBED_Host *host;
147 * The controller callback
149 GNUNET_TESTBED_ControllerCallback cc;
152 * The closure for controller callback
157 * The configuration to use while connecting to controller
159 struct GNUNET_CONFIGURATION_Handle *cfg;
162 * The client connection handle to the controller service
164 struct GNUNET_CLIENT_Connection *client;
167 * The head of the message queue
169 struct MessageQueue *mq_head;
172 * The tail of the message queue
174 struct MessageQueue *mq_tail;
177 * The head of the ControllerLink list
179 struct ControllerLink *cl_head;
182 * The tail of the ControllerLink list
184 struct ControllerLink *cl_tail;
187 * The client transmit handle
189 struct GNUNET_CLIENT_TransmitHandle *th;
192 * The host registration handle; NULL if no current registration requests are
195 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
198 * The controller event mask
203 * Did we start the receive loop yet?
208 * Did we create the host for this?
215 * handle for host registration
217 struct GNUNET_TESTBED_HostRegistrationHandle
220 * The host being registered
222 struct GNUNET_TESTBED_Host *host;
225 * The controller at which this host is being registered
227 struct GNUNET_TESTBED_Controller *c;
230 * The Registartion completion callback
232 GNUNET_TESTBED_HostRegistrationCompletion cc;
235 * The closure for above callback
242 * The global operation id counter
244 uint64_t GNUNET_TESTBED_operation_id;
247 * The head of the operation queue
249 struct GNUNET_TESTBED_Operation *op_head;
252 * The tail of the operation queue
254 struct GNUNET_TESTBED_Operation *op_tail;
258 * Adds an operation to the queue of operations
260 * @param op the operation to add
263 GNUNET_TESTBED_operation_add (struct GNUNET_TESTBED_Operation *op)
265 GNUNET_CONTAINER_DLL_insert_tail (op_head, op_tail, op);
270 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
271 * controller (testbed service)
273 * @param c the controller handler
274 * @param msg message received
275 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
279 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
280 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
282 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
291 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
293 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
294 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
298 msg_size = ntohs (msg->header.size);
299 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
301 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
302 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
303 rh->cc (rh->cc_cls, NULL);
307 /* We have an error message */
308 emsg = (char *) &msg[1];
309 if ('\0' != emsg[msg_size -
310 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
316 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
317 ntohl (msg->host_id), emsg);
318 rh->cc (rh->cc_cls, emsg);
325 * Handler for messages from controller (testbed service)
327 * @param cls the controller handler
328 * @param msg message received, NULL on timeout or fatal error
331 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
333 struct GNUNET_TESTBED_Controller *c = cls;
336 c->in_receive = GNUNET_NO;
337 /* FIXME: Add checks for message integrity */
340 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
344 switch (ntohs (msg->type))
346 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
348 handle_addhostconfirm (c, (const struct
349 GNUNET_TESTBED_HostConfirmedMessage *) msg);
354 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
356 c->in_receive = GNUNET_YES;
357 GNUNET_CLIENT_receive (c->client, &message_handler, c,
358 GNUNET_TIME_UNIT_FOREVER_REL);
363 * Function called to notify a client about the connection begin ready to queue
364 * more data. "buf" will be NULL and "size" zero if the connection was closed
365 * for writing in the meantime.
368 * @param size number of bytes available in buf
369 * @param buf where the callee should write the message
370 * @return number of bytes written to buf
373 transmit_ready_notify (void *cls, size_t size, void *buf)
375 struct GNUNET_TESTBED_Controller *c = cls;
376 struct MessageQueue *mq_entry;
379 mq_entry = c->mq_head;
380 GNUNET_assert (NULL != mq_entry);
381 if ((0 == size) && (NULL == buf)) /* Timeout */
383 LOG_DEBUG ("Message sending timed out -- retrying\n");
385 GNUNET_CLIENT_notify_transmit_ready (c->client,
386 ntohs (mq_entry->msg->size),
388 GNUNET_YES, &transmit_ready_notify,
392 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
393 size = ntohs (mq_entry->msg->size);
394 memcpy (buf, mq_entry->msg, size);
395 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
396 ntohs (mq_entry->msg->type), size);
397 GNUNET_free (mq_entry->msg);
398 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
399 GNUNET_free (mq_entry);
400 mq_entry = c->mq_head;
401 if (NULL != mq_entry)
403 GNUNET_CLIENT_notify_transmit_ready (c->client,
404 ntohs (mq_entry->msg->size),
406 GNUNET_YES, &transmit_ready_notify,
408 if (GNUNET_NO == c->in_receive)
410 c->in_receive = GNUNET_YES;
411 GNUNET_CLIENT_receive (c->client, &message_handler, c,
412 GNUNET_TIME_UNIT_FOREVER_REL);
419 * Queues a message in send queue for sending to the service
421 * @param controller the handle to the controller
422 * @param msg the message to queue
425 GNUNET_TESTBED_queue_message (struct GNUNET_TESTBED_Controller *controller,
426 struct GNUNET_MessageHeader *msg)
428 struct MessageQueue *mq_entry;
432 type = ntohs (msg->type);
433 size = ntohs (msg->size);
434 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
435 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
436 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
438 LOG (GNUNET_ERROR_TYPE_DEBUG,
439 "Queueing message of type %u, size %u for sending\n", type,
441 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
443 if (NULL == controller->th)
445 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
447 GNUNET_YES, &transmit_ready_notify,
453 * Handle for controller process
455 struct GNUNET_TESTBED_ControllerProc
460 struct GNUNET_TESTBED_HelperHandle *helper;
466 * Starts a controller process at the host
468 * @param host the host where the controller has to be started; NULL for localhost
469 * @return the controller process handle
471 struct GNUNET_TESTBED_ControllerProc *
472 GNUNET_TESTBED_controller_start (struct GNUNET_TESTBED_Host *host)
474 struct GNUNET_TESTBED_ControllerProc *cproc;
475 char * const binary_argv[] = {
476 "gnunet-service-testbed",
477 "gnunet-service-testbed",
481 cproc = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
482 cproc->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
483 if (NULL == cproc->helper)
493 * Stop the controller process (also will terminate all peers and controllers
494 * dependent on this controller). This function blocks until the testbed has
495 * been fully terminated (!).
497 * @param cproc the controller process handle
500 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
502 GNUNET_TESTBED_host_stop_ (cproc->helper);
508 * Start a controller process using the given configuration at the
511 * @param cfg configuration to use
512 * @param host host to run the controller on; This should be the same host if
513 * the controller was previously started with
514 * GNUNET_TESTBED_controller_start; NULL for localhost
515 * @param event_mask bit mask with set of events to call 'cc' for;
516 * or-ed values of "1LL" shifted by the
517 * respective 'enum GNUNET_TESTBED_EventType'
518 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
519 * @param cc controller callback to invoke on events
520 * @param cc_cls closure for cc
521 * @return handle to the controller
523 struct GNUNET_TESTBED_Controller *
524 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
525 struct GNUNET_TESTBED_Host *host,
527 GNUNET_TESTBED_ControllerCallback cc,
530 struct GNUNET_TESTBED_Controller *controller;
531 struct GNUNET_TESTBED_InitMessage *msg;
533 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
535 controller->cc_cls = cc_cls;
536 controller->event_mask = event_mask;
537 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
538 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
539 if (NULL == controller->client)
541 GNUNET_TESTBED_controller_disconnect (controller);
546 host = GNUNET_TESTBED_host_create_by_id_ (0);
549 LOG (GNUNET_ERROR_TYPE_WARNING,
550 "Treating NULL host as localhost. Multiple references to localhost. "
551 " May break when localhost freed before calling disconnect \n");
552 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
556 controller->aux_host = GNUNET_YES;
559 GNUNET_assert (NULL != host);
560 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
561 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
562 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
563 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
564 msg->event_mask = GNUNET_htonll (controller->event_mask);
565 GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
571 * Configure shared services at a controller. Using this function,
572 * you can specify that certain services (such as "resolver")
573 * should not be run for each peer but instead be shared
574 * across N peers on the specified host. This function
575 * must be called before any peers are created at the host.
577 * @param controller controller to configure
578 * @param service_name name of the service to share
579 * @param num_peers number of peers that should share one instance
580 * of the specified service (1 for no sharing is the default),
581 * use 0 to disable the service
584 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
585 const char *service_name,
588 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
589 uint16_t service_name_size;
592 service_name_size = strlen (service_name) + 1;
593 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
595 msg = GNUNET_malloc (msg_size);
596 msg->header.size = htons (msg_size);
597 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
598 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
599 msg->num_peers = htonl (num_peers);
600 memcpy (&msg[1], service_name, service_name_size);
601 GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
606 * disconnects from the controller.
608 * @param controller handle to controller to stop
611 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
613 struct MessageQueue *mq_entry;
615 if (NULL != controller->th)
616 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
617 /* Clear the message queue */
618 while (NULL != (mq_entry = controller->mq_head))
620 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
623 GNUNET_free (mq_entry->msg);
624 GNUNET_free (mq_entry);
626 if (NULL != controller->client)
627 GNUNET_CLIENT_disconnect (controller->client);
628 GNUNET_CONFIGURATION_destroy (controller->cfg);
629 if (GNUNET_YES == controller->aux_host)
630 GNUNET_TESTBED_host_destroy (controller->host);
631 GNUNET_free (controller);
636 * Register a host with the controller
638 * @param controller the controller handle
639 * @param host the host to register
640 * @param cc the completion callback to call to inform the status of
641 * registration. After calling this callback the registration handle
642 * will be invalid. Cannot be NULL.
643 * @param cc_cls the closure for the cc
644 * @return handle to the host registration which can be used to cancel the
647 struct GNUNET_TESTBED_HostRegistrationHandle *
648 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
649 struct GNUNET_TESTBED_Host *host,
650 GNUNET_TESTBED_HostRegistrationCompletion cc,
653 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
654 struct GNUNET_TESTBED_AddHostMessage *msg;
655 const char *username;
656 const char *hostname;
658 uint16_t user_name_length;
660 if (NULL != controller->rh)
662 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
663 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
665 LOG (GNUNET_ERROR_TYPE_WARNING,
666 "Host hostname: %s already registered\n",
667 (NULL == hostname) ? "localhost" : hostname);
670 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
673 GNUNET_assert (NULL != cc);
677 username = GNUNET_TESTBED_host_get_username_ (host);
678 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
679 user_name_length = 0;
680 if (NULL != username)
682 user_name_length = strlen (username) + 1;
683 msg_size += user_name_length;
685 /* FIXME: what happens when hostname is NULL? localhost */
686 GNUNET_assert (NULL != hostname);
687 msg_size += strlen (hostname) + 1;
688 msg = GNUNET_malloc (msg_size);
689 msg->header.size = htons (msg_size);
690 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
691 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
692 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
693 msg->user_name_length = htons (user_name_length);
694 if (NULL != username)
695 memcpy (&msg[1], username, user_name_length);
696 strcpy (((void *) &msg[1]) + user_name_length, hostname);
697 GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
703 * Cancel the pending registration. Note that if the registration message is
704 * already sent to the service the cancellation has only the effect that the
705 * registration completion callback for the registration is never called.
707 * @param handle the registration handle to cancel
710 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
713 if (handle != handle->c->rh)
718 handle->c->rh = NULL;
719 GNUNET_free (handle);
724 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
725 * serialized and compressed
727 * @param master handle to the master controller who creates the association
728 * @param delegated_host requests to which host should be delegated; cannot be NULL
729 * @param slave_host which host is used to run the slave controller; use NULL to
730 * make the master controller connect to the delegated host
731 * @param sxcfg serialized and compressed configuration
732 * @param sxcfg_size the size scfg
733 * @param scfg_size the size of uncompressed serialized configuration
734 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
735 * be started by the master controller; GNUNET_NO if we are just
736 * allowed to use the slave via TCP/IP
739 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
740 struct GNUNET_TESTBED_Host *delegated_host,
741 struct GNUNET_TESTBED_Host *slave_host,
747 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
750 GNUNET_assert (GNUNET_YES ==
751 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
752 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
753 GNUNET_assert (GNUNET_YES ==
754 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
755 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
756 msg = GNUNET_malloc (msg_size);
757 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
758 msg->header.size = htons (msg_size);
759 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
760 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
761 ((NULL != slave_host) ? slave_host : master->host));
762 msg->config_size = htons ((uint16_t) scfg_size);
763 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
764 memcpy (&msg[1], sxcfg, sxcfg_size);
765 GNUNET_TESTBED_queue_message (master, (struct GNUNET_MessageHeader *) msg);
770 * Compresses given configuration using zlib compress
772 * @param config the serialized configuration
773 * @param size the size of config
774 * @param xconfig will be set to the compressed configuration (memory is fresly
776 * @return the size of the xconfig
779 GNUNET_TESTBED_compress_config (const char *config, size_t size,
784 xsize = compressBound ((uLong) size);
785 *xconfig = GNUNET_malloc (xsize);
786 GNUNET_assert (Z_OK ==
787 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
788 (const Bytef *) config, (uLongf) size,
795 * Create a link from slave controller to delegated controller. Whenever the
796 * master controller is asked to start a peer at the delegated controller the
797 * request will be routed towards slave controller (if a route exists). The
798 * slave controller will then route it to the delegated controller. The
799 * configuration of the slave controller is given and to be used to either
800 * create the slave controller or to connect to an existing slave controller
801 * process. 'is_subordinate' specifies if the given slave controller should be
802 * started and managed by the master controller, or if the slave already has a
803 * master and this is just a secondary master that is also allowed to use the
806 * @param master handle to the master controller who creates the association
807 * @param delegated_host requests to which host should be delegated
808 * @param slave_host which host is used to run the slave controller
809 * @param slave_cfg configuration to use for the slave controller
810 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
811 * by the master controller; GNUNET_NO if we are just
812 * allowed to use the slave via TCP/IP
815 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
816 struct GNUNET_TESTBED_Host *delegated_host,
817 struct GNUNET_TESTBED_Host *slave_host,
818 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
826 GNUNET_assert (GNUNET_YES ==
827 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
828 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
829 GNUNET_assert (GNUNET_YES ==
830 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
831 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
832 cc_size = GNUNET_TESTBED_compress_config (config, config_size, &cconfig);
833 GNUNET_free (config);
834 GNUNET_assert ((UINT16_MAX -
835 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
836 >= cc_size); /* Configuration doesn't fit in 1 message */
837 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
838 (const char *) cconfig,
839 cc_size, config_size, is_subordinate);
840 GNUNET_free (cconfig);
845 * Ask the testbed controller to write the current overlay topology to
846 * a file. Naturally, the file will only contain a snapshot as the
847 * topology may evolve all the time.
849 * @param controller overlay controller to inspect
850 * @param filename name of the file the topology should
854 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
855 const char *filename)
862 /* end of testbed_api.c */