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/gnunet-service-testbed_barriers.c
23 * @brief barrier handling at the testbed controller
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
27 #include "gnunet-service-testbed.h"
28 #include "gnunet-service-testbed_barriers.h"
29 #include "testbed_api_barriers.h"
33 * timeout for outgoing message transmissions in seconds
35 #define MESSAGE_SEND_TIMEOUT(s) \
36 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, s)
40 * Test to see if local peers have reached the required quorum of a barrier
42 #define LOCAL_QUORUM_REACHED(barrier) \
43 ((barrier->quorum * GST_num_local_peers) <= (barrier->nreached * 100))
53 #define LOG(kind,...) \
54 GNUNET_log_from (kind, "testbed-barriers", __VA_ARGS__)
64 * Message queue for transmitting messages
69 * next pointer for DLL
71 struct MessageQueue *next;
74 * prev pointer for DLL
76 struct MessageQueue *prev;
79 * The message to be sent
81 struct GNUNET_MessageHeader *msg;
86 * Context to be associated with each client
91 * The barrier this client is waiting for
93 struct Barrier *barrier;
98 struct ClientCtx *next;
103 struct ClientCtx *prev;
108 struct GNUNET_SERVER_Client *client;
111 * the transmission handle
113 struct GNUNET_SERVER_TransmitHandle *tx;
118 struct MessageQueue *mq_head;
123 struct MessageQueue *mq_tail;
128 * Wrapper around Barrier handle
135 struct WBarrier *next;
140 struct WBarrier *prev;
143 * The local barrier associated with the creation of this wrapper
145 struct Barrier *barrier;
148 * The barrier handle from API
150 struct GNUNET_TESTBED_Barrier *hbarrier;
153 * Has this barrier been crossed?
165 * The hashcode of the barrier name
167 struct GNUNET_HashCode hash;
170 * The client handle to the master controller
172 struct GNUNET_SERVER_Client *mc;
175 * The name of the barrier
180 * DLL head for the list of clients waiting for this barrier
182 struct ClientCtx *head;
185 * DLL tail for the list of clients waiting for this barrier
187 struct ClientCtx *tail;
190 * DLL head for the list of barrier handles
192 struct WBarrier *whead;
195 * DLL tail for the list of barrier handles
197 struct WBarrier *wtail;
200 * Identifier for the timeout task
202 GNUNET_SCHEDULER_TaskIdentifier tout_task;
205 * The status of this barrier
207 enum GNUNET_TESTBED_BarrierStatus status;
210 * Number of barriers wrapped in the above DLL
212 unsigned int num_wbarriers;
215 * Number of wrapped barriers reached so far
217 unsigned int num_wbarriers_reached;
220 * Number of wrapped barrier initialised so far
222 unsigned int num_wbarriers_inited;
225 * Number of peers which have reached this barrier
227 unsigned int nreached;
230 * Number of slaves we have initialised this barrier
232 unsigned int nslaves;
235 * Quorum percentage to be reached
243 * Hashtable handle for storing initialised barriers
245 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
250 static struct GNUNET_SERVICE_Context *ctx;
254 * Function called to notify a client about the connection
255 * begin ready to queue more data. "buf" will be
256 * NULL and "size" zero if the connection was closed for
257 * writing in the meantime.
259 * @param cls client context
260 * @param size number of bytes available in buf
261 * @param buf where the callee should write the message
262 * @return number of bytes written to buf
265 transmit_ready_cb (void *cls, size_t size, void *buf)
267 struct ClientCtx *ctx = cls;
268 struct GNUNET_SERVER_Client *client = ctx->client;
269 struct MessageQueue *mq;
270 struct GNUNET_MessageHeader *msg;
275 if ((0 == size) || (NULL == buf))
277 GNUNET_assert (NULL != ctx->client);
278 GNUNET_SERVER_client_drop (ctx->client);
284 wrote = ntohs (msg->size);
285 GNUNET_assert (size >= wrote);
286 (void) memcpy (buf, msg, wrote);
287 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
288 GNUNET_free (mq->msg);
290 if (NULL != (mq = ctx->mq_head))
291 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
292 MESSAGE_SEND_TIMEOUT (30),
293 &transmit_ready_cb, ctx);
299 * Queue a message into a clients message queue
301 * @param ctx the context associated with the client
302 * @param msg the message to queue. Will be consumed
305 queue_message (struct ClientCtx *ctx, struct GNUNET_MessageHeader *msg)
307 struct MessageQueue *mq;
308 struct GNUNET_SERVER_Client *client = ctx->client;
310 mq = GNUNET_malloc (sizeof (struct MessageQueue));
312 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n",
313 ntohs (msg->type), ntohs (msg->size));
314 GNUNET_CONTAINER_DLL_insert_tail (ctx->mq_head, ctx->mq_tail, mq);
316 ctx->tx = GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
317 MESSAGE_SEND_TIMEOUT (30),
318 &transmit_ready_cb, ctx);
323 * Function to cleanup client context data structure
325 * @param ctx the client context data structure
328 cleanup_clientctx (struct ClientCtx *ctx)
330 struct MessageQueue *mq;
332 if (NULL != ctx->client)
333 GNUNET_SERVER_client_drop (ctx->client);
335 GNUNET_SERVER_notify_transmit_ready_cancel (ctx->tx);
336 if (NULL != (mq = ctx->mq_head))
338 GNUNET_CONTAINER_DLL_remove (ctx->mq_head, ctx->mq_tail, mq);
339 GNUNET_free (mq->msg);
347 * Function to remove a barrier from the barrier map and cleanup resources
348 * occupied by a barrier
350 * @param barrier the barrier handle
353 remove_barrier (struct Barrier *barrier)
355 struct ClientCtx *ctx;
357 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (barrier_map,
360 while (NULL != (ctx = barrier->head))
362 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, ctx);
363 GNUNET_SERVER_client_drop (ctx->client);
367 GNUNET_SERVER_notify_transmit_ready_cancel (ctx->tx);
371 GNUNET_free (barrier->name);
372 GNUNET_SERVER_client_drop (barrier->mc);
373 GNUNET_free (barrier);
378 * Cancels all subcontroller barrier handles
380 * @param barrier the local barrier
383 cancel_wrappers (struct Barrier *barrier)
385 struct WBarrier *wrapper;
387 while (NULL != (wrapper = barrier->whead))
389 GNUNET_TESTBED_barrier_cancel (wrapper->hbarrier);
390 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
391 GNUNET_free (wrapper);
397 * Send a status message about a barrier to the given client
399 * @param client the client to send the message to
400 * @param name the barrier name
401 * @param status the status of the barrier
402 * @param emsg the error message; should be non-NULL for
403 * status=BARRIER_STATUS_ERROR
406 send_client_status_msg (struct GNUNET_SERVER_Client *client,
408 enum GNUNET_TESTBED_BarrierStatus status,
411 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
415 GNUNET_assert ((NULL == emsg) || (BARRIER_STATUS_ERROR == status));
416 name_len = strlen (name);
417 msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg)
419 + ((NULL == emsg) ? 0 : (strlen (emsg) + 1));
420 msg = GNUNET_malloc (msize);
421 msg->header.size = htons (msize);
422 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
423 msg->status = htons (status);
424 msg->name_len = htons ((uint16_t) name_len);
425 (void) memcpy (msg->data, name, name_len);
427 (void) memcpy (msg->data + name_len + 1, emsg, strlen (emsg));
428 GST_queue_message (client, &msg->header);
433 * Sends a barrier failed message
435 * @param barrier the corresponding barrier
436 * @param emsg the error message; should be non-NULL for
437 * status=BARRIER_STATUS_ERROR
440 send_barrier_status_msg (struct Barrier *barrier, const char *emsg)
442 GNUNET_assert (0 != barrier->status);
443 send_client_status_msg (barrier->mc, barrier->name, barrier->status, emsg);
448 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
449 * message should come from peers or a shared helper service using the
450 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
452 * This handler is queued in the main service and will handle the messages sent
453 * either from the testbed driver or from a high level controller
456 * @param client identification of the client
457 * @param message the actual message
460 handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
461 const struct GNUNET_MessageHeader *message)
463 const struct GNUNET_TESTBED_BarrierWait *msg;
464 struct Barrier *barrier;
466 struct ClientCtx *client_ctx;
467 struct GNUNET_HashCode key;
471 msize = ntohs (message->size);
472 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
478 if (NULL == barrier_map)
481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
484 msg = (const struct GNUNET_TESTBED_BarrierWait *) message;
485 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierWait);
486 name = GNUNET_malloc (name_len + 1);
487 name[name_len] = '\0';
488 (void) memcpy (name, msg->name, name_len);
489 LOG_DEBUG ("Received BARRIER_WAIT for barrier `%s'\n", name);
490 GNUNET_CRYPTO_hash (name, name_len, &key);
491 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
498 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
499 if (NULL == client_ctx)
501 client_ctx = GNUNET_malloc (sizeof (struct ClientCtx));
502 client_ctx->client = client;
503 GNUNET_SERVER_client_keep (client);
504 client_ctx->barrier = barrier;
505 GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
506 GNUNET_SERVER_client_set_user_context (client, client_ctx);
509 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
510 && (LOCAL_QUORUM_REACHED (barrier)))
512 barrier->status = BARRIER_STATUS_CROSSED;
513 send_barrier_status_msg (barrier, NULL);
515 GNUNET_SERVER_receive_done (client, GNUNET_OK);
520 * Functions with this signature are called whenever a client
521 * is disconnected on the network level.
524 * @param client identification of the client; NULL
525 * for the last call when the server is destroyed
528 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
530 struct ClientCtx *client_ctx;
534 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
535 cleanup_clientctx (client_ctx);
540 * Function to initialise barrriers component
542 * @param cfg the configuration to use for initialisation
545 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
547 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
548 {&handle_barrier_wait, NULL, GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT, 0},
551 struct GNUNET_SERVER_Handle *srv;
553 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
554 ctx = GNUNET_SERVICE_start ("testbed-barrier", cfg,
555 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
556 srv = GNUNET_SERVICE_get_server (ctx);
557 GNUNET_SERVER_add_handlers (srv, message_handlers);
558 GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
563 * Function to stop the barrier service
566 GST_barriers_destroy ()
568 GNUNET_assert (NULL != barrier_map);
569 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
570 GNUNET_assert (NULL != ctx);
571 GNUNET_SERVICE_stop (ctx);
576 * Functions of this type are to be given as callback argument to
577 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
578 * information is available for the barrier.
580 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
581 * @param name the name of the barrier
582 * @param b_ the barrier handle
583 * @param status status of the barrier; GNUNET_OK if the barrier is crossed;
584 * GNUNET_SYSERR upon error
585 * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
589 wbarrier_status_cb (void *cls, const char *name,
590 struct GNUNET_TESTBED_Barrier *b_,
591 enum GNUNET_TESTBED_BarrierStatus status,
594 struct WBarrier *wrapper = cls;
595 struct Barrier *barrier = wrapper->barrier;
597 GNUNET_assert (b_ == wrapper->hbarrier);
598 wrapper->hbarrier = NULL;
599 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
600 GNUNET_free (wrapper);
603 case BARRIER_STATUS_ERROR:
604 LOG (GNUNET_ERROR_TYPE_ERROR,
605 "Initialising barrier `%s' failed at a sub-controller: %s\n",
606 barrier->name, (NULL != emsg) ? emsg : "NULL");
607 cancel_wrappers (barrier);
609 emsg = "Initialisation failed at a sub-controller";
610 barrier->status = BARRIER_STATUS_ERROR;
611 send_barrier_status_msg (barrier, emsg);
613 case BARRIER_STATUS_CROSSED:
614 if (BARRIER_STATUS_INITIALISED != barrier->status)
619 barrier->num_wbarriers_reached++;
620 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
621 && (LOCAL_QUORUM_REACHED (barrier)))
623 barrier->status = BARRIER_STATUS_CROSSED;
624 send_barrier_status_msg (barrier, NULL);
627 case BARRIER_STATUS_INITIALISED:
628 if (0 != barrier->status)
633 barrier->num_wbarriers_inited++;
634 if (barrier->num_wbarriers_inited == barrier->num_wbarriers)
636 barrier->status = BARRIER_STATUS_INITIALISED;
637 send_barrier_status_msg (barrier, NULL);
645 * Function called upon timeout while waiting for a response from the
646 * subcontrollers to barrier init message
649 * @param tc scheduler task context
652 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
654 struct Barrier *barrier = cls;
656 cancel_wrappers (barrier);
657 barrier->status = BARRIER_STATUS_ERROR;
658 send_barrier_status_msg (barrier,
659 "Timedout while propagating barrier initialisation\n");
660 remove_barrier (barrier);
665 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
666 * message should always come from a parent controller or the testbed API if we
667 * are the root controller.
669 * This handler is queued in the main service and will handle the messages sent
670 * either from the testbed driver or from a high level controller
673 * @param client identification of the client
674 * @param message the actual message
677 GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
678 const struct GNUNET_MessageHeader *message)
680 const struct GNUNET_TESTBED_BarrierInit *msg;
682 struct Barrier *barrier;
684 struct WBarrier *wrapper;
685 struct GNUNET_HashCode hash;
690 if (NULL == GST_context)
693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
696 if (client != GST_context->client)
699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
702 msize = ntohs (message->size);
703 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierInit))
706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
709 msg = (const struct GNUNET_TESTBED_BarrierInit *) message;
710 name_len = (size_t) msize - sizeof (struct GNUNET_TESTBED_BarrierInit);
711 name = GNUNET_malloc (name_len + 1);
712 (void) memcpy (name, msg->name, name_len);
713 GNUNET_CRYPTO_hash (name, name_len, &hash);
714 LOG_DEBUG ("Received BARRIER_INIT for barrier `%s'\n", name);
715 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
718 send_client_status_msg (client, name, BARRIER_STATUS_ERROR,
719 "A barrier with the same name already exists");
721 GNUNET_SERVER_receive_done (client, GNUNET_OK);
724 barrier = GNUNET_malloc (sizeof (struct Barrier));
725 (void) memcpy (&barrier->hash, &hash, sizeof (struct GNUNET_HashCode));
726 barrier->quorum = msg->quorum;
727 barrier->name = name;
728 barrier->mc = client;
729 GNUNET_SERVER_client_keep (client);
730 GNUNET_assert (GNUNET_OK ==
731 GNUNET_CONTAINER_multihashmap_put (barrier_map,
734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
735 GNUNET_SERVER_receive_done (client, GNUNET_OK);
736 /* Propagate barrier init to subcontrollers */
737 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
739 if (NULL == (slave = GST_slave_list[cnt]))
741 if (NULL == slave->controller)
743 GNUNET_break (0);/* May happen when we are connecting to the controller */
746 wrapper = GNUNET_malloc (sizeof (struct WBarrier));
747 wrapper->barrier = barrier;
748 GNUNET_CONTAINER_DLL_insert_tail (barrier->whead, barrier->wtail, wrapper);
749 wrapper->hbarrier = GNUNET_TESTBED_barrier_init_ (slave->controller,
756 if (NULL == barrier->whead) /* No further propagation */
758 barrier->status = BARRIER_STATUS_INITIALISED;
759 LOG_DEBUG ("Sending BARRIER_STATUS_INITIALISED for barrier `%s'\n",
761 send_barrier_status_msg (barrier, NULL);
763 barrier->tout_task = GNUNET_SCHEDULER_add_delayed (MESSAGE_SEND_TIMEOUT (30),
764 &fwd_tout_barrier_init,
770 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL messages. This
771 * message should always come from a parent controller or the testbed API if we
772 * are the root controller.
774 * This handler is queued in the main service and will handle the messages sent
775 * either from the testbed driver or from a high level controller
778 * @param client identification of the client
779 * @param message the actual message
782 GST_handle_barrier_cancel (void *cls, struct GNUNET_SERVER_Client *client,
783 const struct GNUNET_MessageHeader *message)
785 const struct GNUNET_TESTBED_BarrierCancel *msg;
787 struct Barrier *barrier;
788 struct GNUNET_HashCode hash;
792 if (NULL == GST_context)
795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
798 if (client != GST_context->client)
801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
804 msize = ntohs (message->size);
805 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierCancel))
808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
811 msg = (const struct GNUNET_TESTBED_BarrierCancel *) message;
812 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierCancel);
813 name = GNUNET_malloc (name_len + 1);
814 (void) memcpy (name, msg->name, name_len);
815 GNUNET_CRYPTO_hash (name, name_len, &hash);
816 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
822 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &hash);
823 GNUNET_assert (NULL != barrier);
824 cancel_wrappers (barrier);
825 remove_barrier (barrier);
826 GNUNET_SERVER_receive_done (client, GNUNET_OK);
831 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages.
832 * This handler is queued in the main service and will handle the messages sent
833 * either from the testbed driver or from a high level controller
836 * @param client identification of the client
837 * @param message the actual message
840 GST_handle_barrier_status (void *cls, struct GNUNET_SERVER_Client *client,
841 const struct GNUNET_MessageHeader *message)
843 const struct GNUNET_TESTBED_BarrierStatusMsg *msg;
844 struct Barrier *barrier;
845 struct ClientCtx *client_ctx;
847 struct GNUNET_HashCode key;
848 enum GNUNET_TESTBED_BarrierStatus status;
852 if (NULL == GST_context)
855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
858 if (client != GST_context->client)
861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
864 msize = ntohs (message->size);
865 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierStatusMsg))
868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
871 msg = (const struct GNUNET_TESTBED_BarrierStatusMsg *) message;
872 status = ntohs (msg->status);
873 if (BARRIER_STATUS_CROSSED != status)
875 GNUNET_break_op (0); /* current we only expect BARRIER_CROSSED
876 status message this way */
877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
881 name_len = ntohs (msg->name_len);
882 if ((sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1) != msize)
885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
888 if ('\0' != name[name_len])
891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
894 GNUNET_CRYPTO_hash (name, name_len, &key);
895 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key);
899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
902 GNUNET_SERVER_receive_done (client, GNUNET_OK);
903 while (NULL != (client_ctx = barrier->head)) /* Notify peers */
905 queue_message (client_ctx, GNUNET_copy_message (message));
906 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
910 /* end of gnunet-service-testbed_barriers.c */