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
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_hello_lib.h"
37 #include "testbed_api_hosts.h"
40 * Generic logging shorthand
42 #define LOG(kind, ...) \
43 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
48 #define LOG_DEBUG(...) \
49 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
53 * The message queue for sending messages to the controller service
58 * The message to be sent
60 struct GNUNET_MessageHeader *msg;
63 * next pointer for DLL
65 struct MessageQueue *next;
68 * prev pointer for DLL
70 struct MessageQueue *prev;
75 * Structure for a controller link
80 * The next ptr for DLL
82 struct ControllerLink *next;
85 * The prev ptr for DLL
87 struct ControllerLink *prev;
90 * The host which will be referred in the peer start request. This is the
91 * host where the peer should be started
93 struct GNUNET_TESTBED_Host *delegated_host;
96 * The host which will contacted to delegate the peer start request
98 struct GNUNET_TESTBED_Host *slave_host;
101 * The configuration to be used to connect to slave host
103 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
106 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
107 * if we are just allowed to use the slave via TCP/IP
114 * Handle to interact with a GNUnet testbed controller. Each
115 * controller has at least one master handle which is created when the
116 * controller is created; this master handle interacts with the
117 * controller process, destroying it destroys the controller (by
118 * closing stdin of the controller process). Additionally,
119 * controllers can interact with each other (in a P2P fashion); those
120 * links are established via TCP/IP on the controller's service port.
122 struct GNUNET_TESTBED_Controller
126 * The host where the controller is running
128 struct GNUNET_TESTBED_Host *host;
131 * The controller callback
133 GNUNET_TESTBED_ControllerCallback cc;
136 * The closure for controller callback
141 * The configuration to use while connecting to controller
143 struct GNUNET_CONFIGURATION_Handle *cfg;
146 * The client connection handle to the controller service
148 struct GNUNET_CLIENT_Connection *client;
151 * The head of the message queue
153 struct MessageQueue *mq_head;
156 * The tail of the message queue
158 struct MessageQueue *mq_tail;
161 * The head of the ControllerLink list
163 struct ControllerLink *cl_head;
166 * The tail of the ControllerLink list
168 struct ControllerLink *cl_tail;
171 * The client transmit handle
173 struct GNUNET_CLIENT_TransmitHandle *th;
176 * The host registration handle; NULL if no current registration requests are
179 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
182 * The controller event mask
187 * Did we start the receive loop yet?
192 * Did we create the host for this?
199 * handle for host registration
201 struct GNUNET_TESTBED_HostRegistrationHandle
204 * The host being registered
206 struct GNUNET_TESTBED_Host *host;
209 * The controller at which this host is being registered
211 struct GNUNET_TESTBED_Controller *c;
214 * The Registartion completion callback
216 GNUNET_TESTBED_HostRegistrationCompletion cc;
219 * The closure for above callback
226 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
227 * controller (testbed service)
229 * @param c the controller handler
230 * @param msg message received
231 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
235 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
236 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
238 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
247 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
249 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
250 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
254 msg_size = ntohs (msg->header.size);
255 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
257 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
258 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
259 rh->cc (rh->cc_cls, NULL);
263 /* We have an error message */
264 emsg = (char *) &msg[1];
265 if ('\0' != emsg[msg_size -
266 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
272 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
273 ntohl (msg->host_id), emsg);
274 rh->cc (rh->cc_cls, emsg);
281 * Handler for messages from controller (testbed service)
283 * @param cls the controller handler
284 * @param msg message received, NULL on timeout or fatal error
287 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
289 struct GNUNET_TESTBED_Controller *c = cls;
292 /* FIXME: Add checks for message integrity */
295 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
299 switch (ntohs (msg->type))
301 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
303 handle_addhostconfirm (c, (const struct
304 GNUNET_TESTBED_HostConfirmedMessage *) msg);
309 if (GNUNET_OK == status)
310 GNUNET_CLIENT_receive (c->client, &message_handler, c,
311 GNUNET_TIME_UNIT_FOREVER_REL);
316 * Function called to notify a client about the connection begin ready to queue
317 * more data. "buf" will be NULL and "size" zero if the connection was closed
318 * for writing in the meantime.
321 * @param size number of bytes available in buf
322 * @param buf where the callee should write the message
323 * @return number of bytes written to buf
326 transmit_ready_notify (void *cls, size_t size, void *buf)
328 struct GNUNET_TESTBED_Controller *c = cls;
329 struct MessageQueue *mq_entry;
332 mq_entry = c->mq_head;
333 GNUNET_assert (NULL != mq_entry);
334 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
335 size = ntohs (mq_entry->msg->size);
336 memcpy (buf, mq_entry->msg, size);
337 GNUNET_free (mq_entry->msg);
338 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
339 GNUNET_free (mq_entry);
340 mq_entry = c->mq_head;
341 if (NULL != mq_entry)
343 GNUNET_CLIENT_notify_transmit_ready (c->client,
344 ntohs (mq_entry->msg->size),
345 GNUNET_TIME_UNIT_FOREVER_REL,
346 GNUNET_NO, &transmit_ready_notify,
348 if ( (GNUNET_NO == c->in_receive) &&
351 c->in_receive = GNUNET_YES;
352 GNUNET_CLIENT_receive (c->client, &message_handler, c,
353 GNUNET_TIME_UNIT_FOREVER_REL);
360 * Queues a message in send queue for sending to the service
362 * @param controller the handle to the controller
363 * @param msg the message to queue
366 queue_message (struct GNUNET_TESTBED_Controller *controller,
367 struct GNUNET_MessageHeader *msg)
369 struct MessageQueue *mq_entry;
373 type = ntohs (msg->type);
374 size = ntohs (msg->size);
375 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
376 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
377 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
379 LOG (GNUNET_ERROR_TYPE_DEBUG,
380 "Queueing message of type %u, size %u for sending\n", type,
382 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
384 if (NULL == controller->th)
386 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
387 GNUNET_TIME_UNIT_FOREVER_REL,
388 GNUNET_NO, &transmit_ready_notify,
394 * Handle for controller process
396 struct GNUNET_TESTBED_ControllerProc
401 struct GNUNET_TESTBED_HelperHandle *helper;
407 * Starts a controller process at the host
409 * @param host the host where the controller has to be started; NULL for localhost
410 * @return the controller process handle
412 struct GNUNET_TESTBED_ControllerProc *
413 GNUNET_TESTBED_controller_start (struct GNUNET_TESTBED_Host *host)
415 struct GNUNET_TESTBED_ControllerProc *cproc;
416 char * const binary_argv[] = {
417 "gnunet-service-testbed",
418 "gnunet-service-testbed",
422 cproc = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
423 cproc->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
424 if (NULL == cproc->helper)
434 * Stop the controller process (also will terminate all peers and controllers
435 * dependent on this controller). This function blocks until the testbed has
436 * been fully terminated (!).
438 * @param cproc the controller process handle
441 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
443 GNUNET_TESTBED_host_stop_ (cproc->helper);
449 * Start a controller process using the given configuration at the
452 * @param cfg configuration to use
453 * @param host host to run the controller on; This should be the same host if
454 * the controller was previously started with
455 * GNUNET_TESTBED_controller_start; NULL for localhost
456 * @param event_mask bit mask with set of events to call 'cc' for;
457 * or-ed values of "1LL" shifted by the
458 * respective 'enum GNUNET_TESTBED_EventType'
459 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
460 * @param cc controller callback to invoke on events
461 * @param cc_cls closure for cc
462 * @return handle to the controller
464 struct GNUNET_TESTBED_Controller *
465 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
466 struct GNUNET_TESTBED_Host *host,
468 GNUNET_TESTBED_ControllerCallback cc,
471 struct GNUNET_TESTBED_Controller *controller;
472 struct GNUNET_TESTBED_InitMessage *msg;
474 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
476 controller->cc_cls = cc_cls;
477 controller->event_mask = event_mask;
478 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
479 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
480 if (NULL == controller->client)
482 GNUNET_TESTBED_controller_disconnect (controller);
487 host = GNUNET_TESTBED_host_create_by_id_ (0);
490 LOG (GNUNET_ERROR_TYPE_WARNING,
491 "Treating NULL host as localhost. Multiple references to localhost. "
492 " May break when localhost freed before calling disconnect \n");
493 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
497 controller->aux_host = GNUNET_YES;
500 GNUNET_assert (NULL != host);
501 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
502 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
503 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
504 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
505 msg->event_mask = GNUNET_htonll (controller->event_mask);
506 queue_message (controller, (struct GNUNET_MessageHeader *) msg);
512 * Configure shared services at a controller. Using this function,
513 * you can specify that certain services (such as "resolver")
514 * should not be run for each peer but instead be shared
515 * across N peers on the specified host. This function
516 * must be called before any peers are created at the host.
518 * @param controller controller to configure
519 * @param service_name name of the service to share
520 * @param num_peers number of peers that should share one instance
521 * of the specified service (1 for no sharing is the default),
522 * use 0 to disable the service
525 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
526 const char *service_name,
529 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
530 uint16_t service_name_size;
533 service_name_size = strlen (service_name) + 1;
534 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
536 msg = GNUNET_malloc (msg_size);
537 msg->header.size = htons (msg_size);
538 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
539 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
540 msg->num_peers = htonl (num_peers);
541 memcpy (&msg[1], service_name, service_name_size);
542 queue_message (controller, (struct GNUNET_MessageHeader *) msg);
547 * disconnects from the controller.
549 * @param controller handle to controller to stop
552 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
554 struct MessageQueue *mq_entry;
556 if (NULL != controller->th)
557 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
558 /* Clear the message queue */
559 while (NULL != (mq_entry = controller->mq_head))
561 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
564 GNUNET_free (mq_entry->msg);
565 GNUNET_free (mq_entry);
567 if (NULL != controller->client)
568 GNUNET_CLIENT_disconnect (controller->client);
569 GNUNET_CONFIGURATION_destroy (controller->cfg);
570 if (GNUNET_YES == controller->aux_host)
571 GNUNET_TESTBED_host_destroy (controller->host);
572 GNUNET_free (controller);
577 * Register a host with the controller
579 * @param controller the controller handle
580 * @param host the host to register
581 * @param cc the completion callback to call to inform the status of
582 * registration. After calling this callback the registration handle
583 * will be invalid. Cannot be NULL.
584 * @param cc_cls the closure for the cc
585 * @return handle to the host registration which can be used to cancel the
588 struct GNUNET_TESTBED_HostRegistrationHandle *
589 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
590 struct GNUNET_TESTBED_Host *host,
591 GNUNET_TESTBED_HostRegistrationCompletion cc,
594 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
595 struct GNUNET_TESTBED_AddHostMessage *msg;
596 const char *username;
597 const char *hostname;
599 uint16_t user_name_length;
601 if (NULL != controller->rh)
603 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
604 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
606 LOG (GNUNET_ERROR_TYPE_WARNING,
607 "Host hostname: %s already registered\n",
608 (NULL == hostname) ? "localhost" : hostname);
611 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
614 GNUNET_assert (NULL != cc);
618 username = GNUNET_TESTBED_host_get_username_ (host);
619 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
620 user_name_length = 0;
621 if (NULL != username)
623 user_name_length = strlen (username) + 1;
624 msg_size += user_name_length;
626 /* FIXME: what happens when hostname is NULL? localhost */
627 GNUNET_assert (NULL != hostname);
628 msg_size += strlen (hostname) + 1;
629 msg = GNUNET_malloc (msg_size);
630 msg->header.size = htons (msg_size);
631 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
632 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
633 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
634 msg->user_name_length = htons (user_name_length);
635 if (NULL != username)
636 memcpy (&msg[1], username, user_name_length);
637 strcpy (((void *) msg) + user_name_length, hostname);
638 queue_message (controller, (struct GNUNET_MessageHeader *) msg);
644 * Cancel the pending registration. Note that if the registration message is
645 * already sent to the service the cancellation has only the effect that the
646 * registration completion callback for the registration is never called.
648 * @param handle the registration handle to cancel
651 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
654 if (handle != handle->c->rh)
659 handle->c->rh = NULL;
660 GNUNET_free (handle);
665 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
666 * serialized and compressed
668 * @param master handle to the master controller who creates the association
669 * @param delegated_host requests to which host should be delegated; cannot be NULL
670 * @param slave_host which host is used to run the slave controller; use NULL to
671 * make the master controller connect to the delegated host
672 * @param sxcfg serialized and compressed configuration
673 * @param sxcfg_size the size scfg
674 * @param scfg_size the size of uncompressed serialized configuration
675 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
676 * be started by the master controller; GNUNET_NO if we are just
677 * allowed to use the slave via TCP/IP
680 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
681 struct GNUNET_TESTBED_Host *delegated_host,
682 struct GNUNET_TESTBED_Host *slave_host,
688 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
691 GNUNET_assert (GNUNET_YES ==
692 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
693 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
694 GNUNET_assert (GNUNET_YES ==
695 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
696 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
697 msg = GNUNET_malloc (msg_size);
698 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
699 msg->header.size = htons (msg_size);
700 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
701 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
702 ((NULL != slave_host) ? slave_host : master->host));
703 msg->config_size = htons ((uint16_t) scfg_size);
704 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
705 memcpy (&msg[1], sxcfg, sxcfg_size);
706 queue_message (master, (struct GNUNET_MessageHeader *) msg);
711 * Create a link from slave controller to delegated controller. Whenever the
712 * master controller is asked to start a peer at the delegated controller the
713 * request will be routed towards slave controller (if a route exists). The
714 * slave controller will then route it to the delegated controller. The
715 * configuration of the slave controller is given and to be used to either
716 * create the slave controller or to connect to an existing slave controller
717 * process. 'is_subordinate' specifies if the given slave controller should be
718 * started and managed by the master controller, or if the slave already has a
719 * master and this is just a secondary master that is also allowed to use the
722 * @param master handle to the master controller who creates the association
723 * @param delegated_host requests to which host should be delegated
724 * @param slave_host which host is used to run the slave controller
725 * @param slave_cfg configuration to use for the slave controller
726 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
727 * by the master controller; GNUNET_NO if we are just
728 * allowed to use the slave via TCP/IP
731 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
732 struct GNUNET_TESTBED_Host *delegated_host,
733 struct GNUNET_TESTBED_Host *slave_host,
734 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
742 GNUNET_assert (GNUNET_YES ==
743 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
744 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
745 GNUNET_assert (GNUNET_YES ==
746 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
747 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
748 cc_size = compressBound ((uLong) config_size);
749 cconfig = GNUNET_malloc (cc_size);
750 GNUNET_assert (Z_OK ==
751 compress2 (cconfig, &cc_size,
752 (Bytef *) config, config_size, Z_BEST_SPEED));
753 GNUNET_free (config);
754 GNUNET_assert ((UINT16_MAX -
755 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
756 >= cc_size); /* Configuration doesn't fit in 1 message */
757 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
758 (const char *) cconfig,
759 cc_size, config_size, is_subordinate);
760 GNUNET_free (cconfig);
765 * Ask the testbed controller to write the current overlay topology to
766 * a file. Naturally, the file will only contain a snapshot as the
767 * topology may evolve all the time.
769 * @param controller overlay controller to inspect
770 * @param filename name of the file the topology should
774 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
775 const char *filename)
782 /* end of testbed_api.c */