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 const struct GNUNET_TESTBED_Host *host;
133 struct GNUNET_TESTBED_HelperHandle *helper;
136 * The controller callback
138 GNUNET_TESTBED_ControllerCallback cc;
141 * The closure for controller callback
146 * The configuration to use while connecting to controller
148 struct GNUNET_CONFIGURATION_Handle *cfg;
151 * The client connection handle to the controller service
153 struct GNUNET_CLIENT_Connection *client;
156 * The head of the message queue
158 struct MessageQueue *mq_head;
161 * The tail of the message queue
163 struct MessageQueue *mq_tail;
166 * The head of the ControllerLink list
168 struct ControllerLink *cl_head;
171 * The tail of the ControllerLink list
173 struct ControllerLink *cl_tail;
176 * The client transmit handle
178 struct GNUNET_CLIENT_TransmitHandle *th;
181 * The host registration handle; NULL if no current registration requests are
184 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
187 * The controller event mask
192 * Did we start the receive loop yet?
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_as_registered_ (rh->host);
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 * Start a controller process using the given configuration at the
397 * @param cfg configuration to use
398 * @param host host to run the controller on, NULL for 'localhost'
399 * @param event_mask bit mask with set of events to call 'cc' for;
400 * or-ed values of "1LL" shifted by the
401 * respective 'enum GNUNET_TESTBED_EventType'
402 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
403 * @param cc controller callback to invoke on events
404 * @param cc_cls closure for cc
405 * @return handle to the controller
407 struct GNUNET_TESTBED_Controller *
408 GNUNET_TESTBED_controller_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_TESTBED_Host *host,
411 GNUNET_TESTBED_ControllerCallback cc,
414 struct GNUNET_TESTBED_Controller *controller;
415 char * const binary_argv[] = {
416 "gnunet-service-testbed",
417 "gnunet-service-testbed",
420 struct GNUNET_TESTBED_InitMessage *msg;
422 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
423 controller->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
424 if (NULL == controller->helper)
426 GNUNET_free (controller);
429 controller->host = host;
431 controller->cc_cls = cc_cls;
432 controller->event_mask = event_mask;
433 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
434 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
435 if (NULL == controller->client)
437 GNUNET_TESTBED_controller_stop (controller);
440 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
441 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
442 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
443 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
444 msg->event_mask = GNUNET_htonll (controller->event_mask);
445 queue_message (controller, (struct GNUNET_MessageHeader *) msg);
451 * Configure shared services at a controller. Using this function,
452 * you can specify that certain services (such as "resolver")
453 * should not be run for each peer but instead be shared
454 * across N peers on the specified host. This function
455 * must be called before any peers are created at the host.
457 * @param controller controller to configure
458 * @param service_name name of the service to share
459 * @param num_peers number of peers that should share one instance
460 * of the specified service (1 for no sharing is the default),
461 * use 0 to disable the service
464 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
465 const char *service_name,
468 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
469 uint16_t service_name_size;
472 service_name_size = strlen (service_name) + 1;
473 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
475 msg = GNUNET_malloc (msg_size);
476 msg->header.size = htons (msg_size);
477 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
478 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
479 msg->num_peers = htonl (num_peers);
480 memcpy (&msg[1], service_name, service_name_size);
481 queue_message (controller, (struct GNUNET_MessageHeader *) msg);
486 * Stop the given controller (also will terminate all peers and
487 * controllers dependent on this controller). This function
488 * blocks until the testbed has been fully terminated (!).
490 * @param controller handle to controller to stop
493 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_Controller *controller)
495 struct MessageQueue *mq_entry;
497 if (NULL != controller->th)
498 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
499 /* Clear the message queue */
500 while (NULL != (mq_entry = controller->mq_head))
502 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
505 GNUNET_free (mq_entry->msg);
506 GNUNET_free (mq_entry);
508 if (NULL != controller->client)
509 GNUNET_CLIENT_disconnect (controller->client);
510 GNUNET_TESTBED_host_stop_ (controller->helper);
511 GNUNET_CONFIGURATION_destroy (controller->cfg);
512 GNUNET_free (controller);
517 * Register a host with the controller
519 * @param controller the controller handle
520 * @param host the host to register
521 * @param cc the completion callback to call to inform the status of
522 * registration. After calling this callback the registration handle
523 * will be invalid. Cannot be NULL.
524 * @param cc_cls the closure for the cc
525 * @return handle to the host registration which can be used to cancel the
528 struct GNUNET_TESTBED_HostRegistrationHandle *
529 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
530 struct GNUNET_TESTBED_Host *host,
531 GNUNET_TESTBED_HostRegistrationCompletion cc,
534 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
535 struct GNUNET_TESTBED_AddHostMessage *msg;
536 const char *username;
537 const char *hostname;
539 uint16_t user_name_length;
541 if (NULL != controller->rh)
543 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
544 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host))
546 LOG (GNUNET_ERROR_TYPE_WARNING,
547 "Host hostname: %s already registered\n",
548 (NULL == hostname) ? "localhost" : hostname);
551 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
554 GNUNET_assert (NULL != cc);
558 username = GNUNET_TESTBED_host_get_username_ (host);
559 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
560 user_name_length = 0;
561 if (NULL != username)
563 user_name_length = strlen (username) + 1;
564 msg_size += user_name_length;
566 /* FIXME: what happens when hostname is NULL? localhost */
567 GNUNET_assert (NULL != hostname);
568 msg_size += strlen (hostname) + 1;
569 msg = GNUNET_malloc (msg_size);
570 msg->header.size = htons (msg_size);
571 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
572 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
573 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
574 msg->user_name_length = htons (user_name_length);
575 if (NULL != username)
576 memcpy (&msg[1], username, user_name_length);
577 strcpy (((void *) msg) + user_name_length, hostname);
578 queue_message (controller, (struct GNUNET_MessageHeader *) msg);
584 * Cancel the pending registration. Note that if the registration message is
585 * already sent to the service the cancellation has only the effect that the
586 * registration completion callback for the registration is never called.
588 * @param handle the registration handle to cancel
591 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
594 if (handle != handle->c->rh)
599 handle->c->rh = NULL;
600 GNUNET_free (handle);
605 * Create a link from a 'master' controller to a slave controller.
606 * Whenever the master controller is asked to start a peer at the
607 * given 'delegated_host', it will delegate the request to the
608 * specified slave controller. Note that the slave controller runs at
609 * the 'slave_host', which may or may not be the same host as the
610 * 'delegated_host' (for hierarchical delegations). The configuration
611 * of the slave controller is given and to be used to either create
612 * the slave controller or to connect to an existing slave controller
613 * process. 'is_subordinate' specifies if the given slave controller
614 * should be started and managed by the master controller, or if the
615 * slave already has a master and this is just a secondary master that
616 * is also allowed to use the existing slave.
618 * @param master handle to the master controller who creates the association
619 * @param delegated_host requests to which host should be delegated
620 * @param slave_host which host is used to run the slave controller
621 * @param slave_cfg configuration to use for the slave controller
622 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
623 * by the master controller; GNUNET_NO if we are just
624 * allowed to use the slave via TCP/IP
627 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
628 struct GNUNET_TESTBED_Host *delegated_host,
629 struct GNUNET_TESTBED_Host *slave_host,
630 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
633 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
640 GNUNET_assert (GNUNET_YES ==
641 GNUNET_TESTBED_is_host_registered_ (delegated_host));
642 GNUNET_assert (GNUNET_YES ==
643 GNUNET_TESTBED_is_host_registered_ (slave_host));
644 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
645 cc_size = compressBound ((uLong) config_size);
646 cconfig = GNUNET_malloc (cc_size);
647 GNUNET_assert (Z_OK ==
648 compress2 (cconfig, &cc_size,
649 (Bytef *) config, config_size, Z_BEST_SPEED));
650 GNUNET_free (config);
651 GNUNET_assert ((UINT16_MAX -
652 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
653 >= cc_size); /* Configuration doesn't fit in 1 message */
654 msg_size = cc_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
655 msg = GNUNET_realloc (cconfig, msg_size);
656 memmove (msg + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage),
658 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
659 msg->header.size = htons (msg_size);
660 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
661 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_ (slave_host));
662 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
663 queue_message (master, (struct GNUNET_MessageHeader *) msg);
668 * Ask the testbed controller to write the current overlay topology to
669 * a file. Naturally, the file will only contain a snapshot as the
670 * topology may evolve all the time.
672 * @param controller overlay controller to inspect
673 * @param filename name of the file the topology should
677 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
678 const char *filename)
685 /* end of testbed_api.c */