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 cleanup_clientctx (ctx);
365 GNUNET_free (barrier->name);
366 GNUNET_SERVER_client_drop (barrier->mc);
367 GNUNET_free (barrier);
372 * Cancels all subcontroller barrier handles
374 * @param barrier the local barrier
377 cancel_wrappers (struct Barrier *barrier)
379 struct WBarrier *wrapper;
381 while (NULL != (wrapper = barrier->whead))
383 GNUNET_TESTBED_barrier_cancel (wrapper->hbarrier);
384 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
385 GNUNET_free (wrapper);
391 * Send a status message about a barrier to the given client
393 * @param client the client to send the message to
394 * @param name the barrier name
395 * @param status the status of the barrier
396 * @param emsg the error message; should be non-NULL for
397 * status=GNUNET_TESTBED_BARRIERSTATUS_ERROR
400 send_client_status_msg (struct GNUNET_SERVER_Client *client,
402 enum GNUNET_TESTBED_BarrierStatus status,
405 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
409 GNUNET_assert ((NULL == emsg) || (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status));
410 name_len = strlen (name);
411 msize = sizeof (struct GNUNET_TESTBED_BarrierStatusMsg)
413 + ((NULL == emsg) ? 0 : (strlen (emsg) + 1));
414 msg = GNUNET_malloc (msize);
415 msg->header.size = htons (msize);
416 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
417 msg->status = htons (status);
418 msg->name_len = htons ((uint16_t) name_len);
419 (void) memcpy (msg->data, name, name_len);
421 (void) memcpy (msg->data + name_len + 1, emsg, strlen (emsg));
422 GST_queue_message (client, &msg->header);
427 * Sends a barrier failed message
429 * @param barrier the corresponding barrier
430 * @param emsg the error message; should be non-NULL for
431 * status=GNUNET_TESTBED_BARRIERSTATUS_ERROR
434 send_barrier_status_msg (struct Barrier *barrier, const char *emsg)
436 GNUNET_assert (0 != barrier->status);
437 send_client_status_msg (barrier->mc, barrier->name, barrier->status, emsg);
442 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
443 * message should come from peers or a shared helper service using the
444 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
446 * This handler is queued in the main service and will handle the messages sent
447 * either from the testbed driver or from a high level controller
450 * @param client identification of the client
451 * @param message the actual message
454 handle_barrier_wait (void *cls, struct GNUNET_SERVER_Client *client,
455 const struct GNUNET_MessageHeader *message)
457 const struct GNUNET_TESTBED_BarrierWait *msg;
458 struct Barrier *barrier;
460 struct ClientCtx *client_ctx;
461 struct GNUNET_HashCode key;
465 msize = ntohs (message->size);
466 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierWait))
469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
472 if (NULL == barrier_map)
475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
478 msg = (const struct GNUNET_TESTBED_BarrierWait *) message;
479 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierWait);
480 name = GNUNET_malloc (name_len + 1);
481 name[name_len] = '\0';
482 (void) memcpy (name, msg->name, name_len);
483 LOG_DEBUG ("Received BARRIER_WAIT for barrier `%s'\n", name);
484 GNUNET_CRYPTO_hash (name, name_len, &key);
485 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
492 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
493 if (NULL == client_ctx)
495 client_ctx = GNUNET_malloc (sizeof (struct ClientCtx));
496 client_ctx->client = client;
497 GNUNET_SERVER_client_keep (client);
498 client_ctx->barrier = barrier;
499 GNUNET_CONTAINER_DLL_insert_tail (barrier->head, barrier->tail, client_ctx);
500 GNUNET_SERVER_client_set_user_context (client, client_ctx);
503 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
504 && (LOCAL_QUORUM_REACHED (barrier)))
506 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_CROSSED;
507 send_barrier_status_msg (barrier, NULL);
509 GNUNET_SERVER_receive_done (client, GNUNET_OK);
514 * Functions with this signature are called whenever a client
515 * is disconnected on the network level.
518 * @param client identification of the client; NULL
519 * for the last call when the server is destroyed
522 disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
524 struct ClientCtx *client_ctx;
528 client_ctx = GNUNET_SERVER_client_get_user_context (client, struct ClientCtx);
529 if (NULL == client_ctx)
531 cleanup_clientctx (client_ctx);
536 * Function to initialise barrriers component
538 * @param cfg the configuration to use for initialisation
541 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
543 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
544 {&handle_barrier_wait, NULL, GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT, 0},
547 struct GNUNET_SERVER_Handle *srv;
549 barrier_map = GNUNET_CONTAINER_multihashmap_create (3, GNUNET_YES);
550 ctx = GNUNET_SERVICE_start ("testbed-barrier", cfg,
551 GNUNET_SERVICE_OPTION_MANUAL_SHUTDOWN);
552 srv = GNUNET_SERVICE_get_server (ctx);
553 GNUNET_SERVER_add_handlers (srv, message_handlers);
554 GNUNET_SERVER_disconnect_notify (srv, &disconnect_cb, NULL);
559 * Function to stop the barrier service
562 GST_barriers_destroy ()
564 GNUNET_assert (NULL != barrier_map);
565 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
566 GNUNET_assert (NULL != ctx);
567 GNUNET_SERVICE_stop (ctx);
572 * Functions of this type are to be given as callback argument to
573 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
574 * information is available for the barrier.
576 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
577 * @param name the name of the barrier
578 * @param b_ the barrier handle
579 * @param status status of the barrier; GNUNET_OK if the barrier is crossed;
580 * GNUNET_SYSERR upon error
581 * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
585 wbarrier_status_cb (void *cls, const char *name,
586 struct GNUNET_TESTBED_Barrier *b_,
587 enum GNUNET_TESTBED_BarrierStatus status,
590 struct WBarrier *wrapper = cls;
591 struct Barrier *barrier = wrapper->barrier;
593 GNUNET_assert (b_ == wrapper->hbarrier);
594 wrapper->hbarrier = NULL;
595 GNUNET_CONTAINER_DLL_remove (barrier->whead, barrier->wtail, wrapper);
596 GNUNET_free (wrapper);
599 case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
600 LOG (GNUNET_ERROR_TYPE_ERROR,
601 "Initialising barrier `%s' failed at a sub-controller: %s\n",
602 barrier->name, (NULL != emsg) ? emsg : "NULL");
603 cancel_wrappers (barrier);
605 emsg = "Initialisation failed at a sub-controller";
606 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_ERROR;
607 send_barrier_status_msg (barrier, emsg);
609 case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
610 if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED != barrier->status)
615 barrier->num_wbarriers_reached++;
616 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
617 && (LOCAL_QUORUM_REACHED (barrier)))
619 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_CROSSED;
620 send_barrier_status_msg (barrier, NULL);
623 case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
624 if (0 != barrier->status)
629 barrier->num_wbarriers_inited++;
630 if (barrier->num_wbarriers_inited == barrier->num_wbarriers)
632 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_INITIALISED;
633 send_barrier_status_msg (barrier, NULL);
641 * Function called upon timeout while waiting for a response from the
642 * subcontrollers to barrier init message
645 * @param tc scheduler task context
648 fwd_tout_barrier_init (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
650 struct Barrier *barrier = cls;
652 cancel_wrappers (barrier);
653 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_ERROR;
654 send_barrier_status_msg (barrier,
655 "Timedout while propagating barrier initialisation\n");
656 remove_barrier (barrier);
661 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
662 * message should always come from a parent controller or the testbed API if we
663 * are the root controller.
665 * This handler is queued in the main service and will handle the messages sent
666 * either from the testbed driver or from a high level controller
669 * @param client identification of the client
670 * @param message the actual message
673 GST_handle_barrier_init (void *cls, struct GNUNET_SERVER_Client *client,
674 const struct GNUNET_MessageHeader *message)
676 const struct GNUNET_TESTBED_BarrierInit *msg;
678 struct Barrier *barrier;
680 struct WBarrier *wrapper;
681 struct GNUNET_HashCode hash;
686 if (NULL == GST_context)
689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
692 if (client != GST_context->client)
695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
698 msize = ntohs (message->size);
699 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierInit))
702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
705 msg = (const struct GNUNET_TESTBED_BarrierInit *) message;
706 name_len = (size_t) msize - sizeof (struct GNUNET_TESTBED_BarrierInit);
707 name = GNUNET_malloc (name_len + 1);
708 (void) memcpy (name, msg->name, name_len);
709 GNUNET_CRYPTO_hash (name, name_len, &hash);
710 LOG_DEBUG ("Received BARRIER_INIT for barrier `%s'\n", name);
711 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
714 send_client_status_msg (client, name, GNUNET_TESTBED_BARRIERSTATUS_ERROR,
715 "A barrier with the same name already exists");
717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
720 barrier = GNUNET_malloc (sizeof (struct Barrier));
721 (void) memcpy (&barrier->hash, &hash, sizeof (struct GNUNET_HashCode));
722 barrier->quorum = msg->quorum;
723 barrier->name = name;
724 barrier->mc = client;
725 GNUNET_SERVER_client_keep (client);
726 GNUNET_assert (GNUNET_OK ==
727 GNUNET_CONTAINER_multihashmap_put (barrier_map,
730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
731 GNUNET_SERVER_receive_done (client, GNUNET_OK);
732 /* Propagate barrier init to subcontrollers */
733 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
735 if (NULL == (slave = GST_slave_list[cnt]))
737 if (NULL == slave->controller)
739 GNUNET_break (0);/* May happen when we are connecting to the controller */
742 wrapper = GNUNET_malloc (sizeof (struct WBarrier));
743 wrapper->barrier = barrier;
744 GNUNET_CONTAINER_DLL_insert_tail (barrier->whead, barrier->wtail, wrapper);
745 wrapper->hbarrier = GNUNET_TESTBED_barrier_init_ (slave->controller,
752 if (NULL == barrier->whead) /* No further propagation */
754 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_INITIALISED;
755 LOG_DEBUG ("Sending GNUNET_TESTBED_BARRIERSTATUS_INITIALISED for barrier `%s'\n",
757 send_barrier_status_msg (barrier, NULL);
759 barrier->tout_task = GNUNET_SCHEDULER_add_delayed (MESSAGE_SEND_TIMEOUT (30),
760 &fwd_tout_barrier_init,
766 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL messages. This
767 * message should always come from a parent controller or the testbed API if we
768 * are the root controller.
770 * This handler is queued in the main service and will handle the messages sent
771 * either from the testbed driver or from a high level controller
774 * @param client identification of the client
775 * @param message the actual message
778 GST_handle_barrier_cancel (void *cls, struct GNUNET_SERVER_Client *client,
779 const struct GNUNET_MessageHeader *message)
781 const struct GNUNET_TESTBED_BarrierCancel *msg;
783 struct Barrier *barrier;
784 struct GNUNET_HashCode hash;
788 if (NULL == GST_context)
791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
794 if (client != GST_context->client)
797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
800 msize = ntohs (message->size);
801 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierCancel))
804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
807 msg = (const struct GNUNET_TESTBED_BarrierCancel *) message;
808 name_len = msize - sizeof (struct GNUNET_TESTBED_BarrierCancel);
809 name = GNUNET_malloc (name_len + 1);
810 (void) memcpy (name, msg->name, name_len);
811 GNUNET_CRYPTO_hash (name, name_len, &hash);
812 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (barrier_map, &hash))
815 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
818 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &hash);
819 GNUNET_assert (NULL != barrier);
820 cancel_wrappers (barrier);
821 remove_barrier (barrier);
822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
827 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages.
828 * This handler is queued in the main service and will handle the messages sent
829 * either from the testbed driver or from a high level controller
832 * @param client identification of the client
833 * @param message the actual message
836 GST_handle_barrier_status (void *cls, struct GNUNET_SERVER_Client *client,
837 const struct GNUNET_MessageHeader *message)
839 const struct GNUNET_TESTBED_BarrierStatusMsg *msg;
840 struct Barrier *barrier;
841 struct ClientCtx *client_ctx;
843 struct GNUNET_HashCode key;
844 enum GNUNET_TESTBED_BarrierStatus status;
848 if (NULL == GST_context)
851 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
854 if (client != GST_context->client)
857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
860 msize = ntohs (message->size);
861 if (msize <= sizeof (struct GNUNET_TESTBED_BarrierStatusMsg))
864 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
867 msg = (const struct GNUNET_TESTBED_BarrierStatusMsg *) message;
868 status = ntohs (msg->status);
869 if (GNUNET_TESTBED_BARRIERSTATUS_CROSSED != status)
871 GNUNET_break_op (0); /* current we only expect BARRIER_CROSSED
872 status message this way */
873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
877 name_len = ntohs (msg->name_len);
878 if ((sizeof (struct GNUNET_TESTBED_BarrierStatusMsg) + name_len + 1) != msize)
881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
884 if ('\0' != name[name_len])
887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
890 GNUNET_CRYPTO_hash (name, name_len, &key);
891 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key);
895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
899 while (NULL != (client_ctx = barrier->head)) /* Notify peers */
901 queue_message (client_ctx, GNUNET_copy_message (message));
902 GNUNET_CONTAINER_DLL_remove (barrier->head, barrier->tail, client_ctx);
906 /* end of gnunet-service-testbed_barriers.c */