2 This file is part of GNUnet.
3 Copyright (C) 2008--2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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.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 * Context to be associated with each client
69 * The barrier this client is waiting for
71 struct Barrier *barrier;
76 struct ClientCtx *next;
81 struct ClientCtx *prev;
86 struct GNUNET_SERVICE_Client *client;
91 * Wrapper around Barrier handle
98 struct WBarrier *next;
103 struct WBarrier *prev;
106 * The local barrier associated with the creation of this wrapper
108 struct Barrier *barrier;
111 * Handle to the slave controller where this wrapper creates a barrier
113 struct GNUNET_TESTBED_Controller *controller;
116 * The barrier handle from API
118 struct GNUNET_TESTBED_Barrier *hbarrier;
121 * Has this barrier been crossed?
133 * The hashcode of the barrier name
135 struct GNUNET_HashCode hash;
138 * The client handle to the master controller
140 struct GNUNET_SERVICE_Client *mc;
143 * The name of the barrier
148 * DLL head for the list of clients waiting for this barrier
150 struct ClientCtx *head;
153 * DLL tail for the list of clients waiting for this barrier
155 struct ClientCtx *tail;
158 * DLL head for the list of barrier handles
160 struct WBarrier *whead;
163 * DLL tail for the list of barrier handles
165 struct WBarrier *wtail;
168 * Identifier for the timeout task
170 struct GNUNET_SCHEDULER_Task *tout_task;
173 * The status of this barrier
175 enum GNUNET_TESTBED_BarrierStatus status;
178 * Number of barriers wrapped in the above DLL
180 unsigned int num_wbarriers;
183 * Number of wrapped barriers reached so far
185 unsigned int num_wbarriers_reached;
188 * Number of wrapped barrier initialised so far
190 unsigned int num_wbarriers_inited;
193 * Number of peers which have reached this barrier
195 unsigned int nreached;
198 * Number of slaves we have initialised this barrier
200 unsigned int nslaves;
203 * Quorum percentage to be reached
210 * Hashtable handle for storing initialised barriers
212 static struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
217 static struct GNUNET_SERVICE_Handle *ctx;
221 * Function to remove a barrier from the barrier map and cleanup resources
222 * occupied by a barrier
224 * @param barrier the barrier handle
227 remove_barrier (struct Barrier *barrier)
229 struct ClientCtx *ctx;
231 GNUNET_assert (GNUNET_YES ==
232 GNUNET_CONTAINER_multihashmap_remove (barrier_map,
235 while (NULL != (ctx = barrier->head))
237 GNUNET_CONTAINER_DLL_remove (barrier->head,
242 GNUNET_free (barrier->name);
243 GNUNET_free (barrier);
248 * Cancels all subcontroller barrier handles
250 * @param barrier the local barrier
253 cancel_wrappers (struct Barrier *barrier)
255 struct WBarrier *wrapper;
257 while (NULL != (wrapper = barrier->whead))
259 GNUNET_TESTBED_barrier_cancel (wrapper->hbarrier);
260 GNUNET_CONTAINER_DLL_remove (barrier->whead,
263 GNUNET_free (wrapper);
269 * Send a status message about a barrier to the given client
271 * @param client the client to send the message to
272 * @param name the barrier name
273 * @param status the status of the barrier
274 * @param emsg the error message; should be non-NULL for
275 * status=GNUNET_TESTBED_BARRIERSTATUS_ERROR
278 send_client_status_msg (struct GNUNET_SERVICE_Client *client,
280 enum GNUNET_TESTBED_BarrierStatus status,
283 struct GNUNET_MQ_Envelope *env;
284 struct GNUNET_TESTBED_BarrierStatusMsg *msg;
288 GNUNET_assert ((NULL == emsg) ||
289 (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status));
290 name_len = strlen (name) + 1;
291 err_len = ((NULL == emsg) ? 0 : (strlen (emsg) + 1));
292 env = GNUNET_MQ_msg_extra (msg,
294 GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS);
295 msg->status = htons (status);
296 msg->name_len = htons ((uint16_t) name_len - 1);
297 GNUNET_memcpy (msg->data,
300 GNUNET_memcpy (msg->data + name_len,
303 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
309 * Sends a barrier failed message
311 * @param barrier the corresponding barrier
312 * @param emsg the error message; should be non-NULL for
313 * status=GNUNET_TESTBED_BARRIERSTATUS_ERROR
316 send_barrier_status_msg (struct Barrier *barrier,
319 GNUNET_assert (0 != barrier->status);
320 send_client_status_msg (barrier->mc,
328 * Check #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages.
330 * @param cls identification of the client
331 * @param message the actual message
334 check_barrier_wait (void *cls,
335 const struct GNUNET_TESTBED_BarrierWait *msg)
337 return GNUNET_OK; /* always well-formed */
342 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT messages. This
343 * message should come from peers or a shared helper service using the
344 * testbed-barrier client API (@see gnunet_testbed_barrier_service.h)
346 * This handler is queued in the main service and will handle the messages sent
347 * either from the testbed driver or from a high level controller
349 * @param cls identification of the client
350 * @param message the actual message
353 handle_barrier_wait (void *cls,
354 const struct GNUNET_TESTBED_BarrierWait *msg)
356 struct ClientCtx *client_ctx = cls;
357 struct Barrier *barrier;
359 struct GNUNET_HashCode key;
363 msize = ntohs (msg->header.size);
364 if (NULL == barrier_map)
367 GNUNET_SERVICE_client_drop (client_ctx->client);
370 name_len = msize - sizeof(struct GNUNET_TESTBED_BarrierWait);
371 name = GNUNET_malloc (name_len + 1);
372 name[name_len] = '\0';
376 LOG_DEBUG ("Received BARRIER_WAIT for barrier `%s'\n",
378 GNUNET_CRYPTO_hash (name,
382 if (NULL == (barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map, &key)))
385 GNUNET_SERVICE_client_drop (client_ctx->client);
388 if (NULL != client_ctx->barrier)
391 GNUNET_SERVICE_client_drop (client_ctx->client);
394 client_ctx->barrier = barrier;
395 GNUNET_CONTAINER_DLL_insert_tail (barrier->head,
399 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers) &&
400 (LOCAL_QUORUM_REACHED (barrier)))
402 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_CROSSED;
403 send_barrier_status_msg (barrier,
406 GNUNET_SERVICE_client_continue (client_ctx->client);
411 * Function called when a client connects to the testbed-barrier service.
414 * @param client the connecting client
415 * @param mq queue to talk to @a client
416 * @return our `struct ClientCtx`
419 connect_cb (void *cls,
420 struct GNUNET_SERVICE_Client *client,
421 struct GNUNET_MQ_Handle *mq)
423 struct ClientCtx *client_ctx;
425 LOG_DEBUG ("Client connected to testbed-barrier service\n");
426 client_ctx = GNUNET_new (struct ClientCtx);
427 client_ctx->client = client;
433 * Functions with this signature are called whenever a client
434 * is disconnected on the network level.
437 * @param client identification of the client; NULL
438 * for the last call when the server is destroyed
441 disconnect_cb (void *cls,
442 struct GNUNET_SERVICE_Client *client,
445 struct ClientCtx *client_ctx = app_ctx;
446 struct Barrier *barrier = client_ctx->barrier;
450 GNUNET_CONTAINER_DLL_remove (barrier->head,
453 client_ctx->barrier = NULL;
455 GNUNET_free (client_ctx);
456 LOG_DEBUG ("Client disconnected from testbed-barrier service\n");
461 * Function to initialise barrriers component
463 * @param cfg the configuration to use for initialisation
466 GST_barriers_init (struct GNUNET_CONFIGURATION_Handle *cfg)
468 struct GNUNET_MQ_MessageHandler message_handlers[] = {
469 GNUNET_MQ_hd_var_size (barrier_wait,
470 GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_WAIT,
471 struct GNUNET_TESTBED_BarrierWait,
473 GNUNET_MQ_handler_end ()
476 LOG_DEBUG ("Launching testbed-barrier service\n");
477 barrier_map = GNUNET_CONTAINER_multihashmap_create (3,
479 ctx = GNUNET_SERVICE_start ("testbed-barrier",
489 * Iterator over hash map entries.
492 * @param key current key code
493 * @param value value in the hash map
494 * @return #GNUNET_YES if we should continue to
499 barrier_destroy_iterator (void *cls,
500 const struct GNUNET_HashCode *key,
503 struct Barrier *barrier = value;
505 GNUNET_assert (NULL != barrier);
506 cancel_wrappers (barrier);
507 remove_barrier (barrier);
513 * Function to stop the barrier service
516 GST_barriers_destroy ()
518 GNUNET_assert (NULL != barrier_map);
519 GNUNET_assert (GNUNET_SYSERR !=
520 GNUNET_CONTAINER_multihashmap_iterate (barrier_map,
522 barrier_destroy_iterator,
524 GNUNET_CONTAINER_multihashmap_destroy (barrier_map);
525 GNUNET_assert (NULL != ctx);
526 GNUNET_SERVICE_stop (ctx);
531 * Functions of this type are to be given as callback argument to
532 * GNUNET_TESTBED_barrier_init(). The callback will be called when status
533 * information is available for the barrier.
535 * @param cls the closure given to GNUNET_TESTBED_barrier_init()
536 * @param name the name of the barrier
537 * @param b_ the barrier handle
538 * @param status status of the barrier; #GNUNET_OK if the barrier is crossed;
539 * #GNUNET_SYSERR upon error
540 * @param emsg if the status were to be #GNUNET_SYSERR, this parameter has the
544 wbarrier_status_cb (void *cls,
546 struct GNUNET_TESTBED_Barrier *b_,
547 enum GNUNET_TESTBED_BarrierStatus status,
550 struct WBarrier *wrapper = cls;
551 struct Barrier *barrier = wrapper->barrier;
553 GNUNET_assert (b_ == wrapper->hbarrier);
556 case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
557 LOG (GNUNET_ERROR_TYPE_ERROR,
558 "Initialising barrier `%s' failed at a sub-controller: %s\n",
560 (NULL != emsg) ? emsg : "NULL");
561 cancel_wrappers (barrier);
563 emsg = "Initialisation failed at a sub-controller";
564 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_ERROR;
565 send_barrier_status_msg (barrier, emsg);
568 case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
569 if (GNUNET_TESTBED_BARRIERSTATUS_INITIALISED != barrier->status)
574 barrier->num_wbarriers_reached++;
575 if ((barrier->num_wbarriers_reached == barrier->num_wbarriers)
576 && (LOCAL_QUORUM_REACHED (barrier)))
578 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_CROSSED;
579 send_barrier_status_msg (barrier, NULL);
583 case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
584 if (0 != barrier->status)
589 barrier->num_wbarriers_inited++;
590 if (barrier->num_wbarriers_inited == barrier->num_wbarriers)
592 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_INITIALISED;
593 send_barrier_status_msg (barrier, NULL);
601 * Function called upon timeout while waiting for a response from the
602 * subcontrollers to barrier init message
607 fwd_tout_barrier_init (void *cls)
609 struct Barrier *barrier = cls;
611 cancel_wrappers (barrier);
612 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_ERROR;
613 send_barrier_status_msg (barrier,
614 "Timedout while propagating barrier initialisation\n");
615 remove_barrier (barrier);
620 * Check #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages.
622 * @param cls identification of the client
623 * @param msg the actual message
624 * @return #GNUNET_OK if @a msg is well-formed
627 check_barrier_init (void *cls,
628 const struct GNUNET_TESTBED_BarrierInit *msg)
630 return GNUNET_OK; /* always well-formed */
635 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_INIT messages. This
636 * message should always come from a parent controller or the testbed API if we
637 * are the root controller.
639 * This handler is queued in the main service and will handle the messages sent
640 * either from the testbed driver or from a high level controller
642 * @param cls identification of the client
643 * @param msg the actual message
646 handle_barrier_init (void *cls,
647 const struct GNUNET_TESTBED_BarrierInit *msg)
649 struct GNUNET_SERVICE_Client *client = cls;
651 struct Barrier *barrier;
653 struct WBarrier *wrapper;
654 struct GNUNET_HashCode hash;
659 if (NULL == GST_context)
662 GNUNET_SERVICE_client_drop (client);
665 if (client != GST_context->client)
668 GNUNET_SERVICE_client_drop (client);
671 msize = ntohs (msg->header.size);
672 name_len = (size_t) msize - sizeof(struct GNUNET_TESTBED_BarrierInit);
673 name = GNUNET_malloc (name_len + 1);
674 GNUNET_memcpy (name, msg->name, name_len);
675 GNUNET_CRYPTO_hash (name, name_len, &hash);
676 LOG_DEBUG ("Received BARRIER_INIT for barrier `%s'\n",
679 GNUNET_CONTAINER_multihashmap_contains (barrier_map,
682 send_client_status_msg (client,
684 GNUNET_TESTBED_BARRIERSTATUS_ERROR,
685 "A barrier with the same name already exists");
687 GNUNET_SERVICE_client_continue (client);
690 barrier = GNUNET_new (struct Barrier);
691 barrier->hash = hash;
692 barrier->quorum = msg->quorum;
693 barrier->name = name;
694 barrier->mc = client;
695 GNUNET_assert (GNUNET_OK ==
696 GNUNET_CONTAINER_multihashmap_put (barrier_map,
699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
700 GNUNET_SERVICE_client_continue (client);
701 /* Propagate barrier init to subcontrollers */
702 for (cnt = 0; cnt < GST_slave_list_size; cnt++)
704 if (NULL == (slave = GST_slave_list[cnt]))
706 if (NULL == slave->controller)
708 GNUNET_break (0); /* May happen when we are connecting to the controller */
711 wrapper = GNUNET_new (struct WBarrier);
712 wrapper->barrier = barrier;
713 wrapper->controller = slave->controller;
714 GNUNET_CONTAINER_DLL_insert_tail (barrier->whead,
717 barrier->num_wbarriers++;
718 wrapper->hbarrier = GNUNET_TESTBED_barrier_init_ (wrapper->controller,
725 if (NULL == barrier->whead) /* No further propagation */
727 barrier->status = GNUNET_TESTBED_BARRIERSTATUS_INITIALISED;
729 "Sending GNUNET_TESTBED_BARRIERSTATUS_INITIALISED for barrier `%s'\n",
731 send_barrier_status_msg (barrier, NULL);
734 barrier->tout_task = GNUNET_SCHEDULER_add_delayed (MESSAGE_SEND_TIMEOUT (
736 &fwd_tout_barrier_init,
742 * Check #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL messages.
744 * @param cls identification of the client
745 * @param msg the actual message
746 * @return #GNUNET_OK if @a msg is well-formed
749 check_barrier_cancel (void *cls,
750 const struct GNUNET_TESTBED_BarrierCancel *msg)
752 return GNUNET_OK; /* all are well-formed */
757 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_CANCEL messages. This
758 * message should always come from a parent controller or the testbed API if we
759 * are the root controller.
761 * This handler is queued in the main service and will handle the messages sent
762 * either from the testbed driver or from a high level controller
764 * @param cls identification of the client
765 * @param msg the actual message
768 handle_barrier_cancel (void *cls,
769 const struct GNUNET_TESTBED_BarrierCancel *msg)
771 struct GNUNET_SERVICE_Client *client = cls;
773 struct Barrier *barrier;
774 struct GNUNET_HashCode hash;
778 if (NULL == GST_context)
781 GNUNET_SERVICE_client_drop (client);
784 if (client != GST_context->client)
787 GNUNET_SERVICE_client_drop (client);
790 msize = ntohs (msg->header.size);
791 name_len = msize - sizeof(struct GNUNET_TESTBED_BarrierCancel);
792 name = GNUNET_malloc (name_len + 1);
796 LOG_DEBUG ("Received BARRIER_CANCEL for barrier `%s'\n",
798 GNUNET_CRYPTO_hash (name,
802 GNUNET_CONTAINER_multihashmap_contains (barrier_map,
806 GNUNET_SERVICE_client_drop (client);
809 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map,
811 GNUNET_assert (NULL != barrier);
812 cancel_wrappers (barrier);
813 remove_barrier (barrier);
814 GNUNET_SERVICE_client_continue (client);
819 * Check #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages.
821 * @param cls identification of the client
822 * @param msg the actual message
823 * @return #GNUNET_OK if @a msg is well-formed
826 check_barrier_status (void *cls,
827 const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
832 enum GNUNET_TESTBED_BarrierStatus status;
834 msize = ntohs (msg->header.size) - sizeof(*msg);
835 status = ntohs (msg->status);
836 if (GNUNET_TESTBED_BARRIERSTATUS_CROSSED != status)
838 GNUNET_break_op (0); /* current we only expect BARRIER_CROSSED
839 status message this way */
840 return GNUNET_SYSERR;
843 name_len = ntohs (msg->name_len);
844 if ((name_len + 1) != msize)
847 return GNUNET_SYSERR;
849 if ('\0' != name[name_len])
852 return GNUNET_SYSERR;
859 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_BARRIER_STATUS messages.
860 * This handler is queued in the main service and will handle the messages sent
861 * either from the testbed driver or from a high level controller
863 * @param cls identification of the client
864 * @param msg the actual message
867 handle_barrier_status (void *cls,
868 const struct GNUNET_TESTBED_BarrierStatusMsg *msg)
870 struct GNUNET_SERVICE_Client *client = cls;
871 struct Barrier *barrier;
872 struct ClientCtx *client_ctx;
873 struct WBarrier *wrapper;
875 struct GNUNET_HashCode key;
877 struct GNUNET_MQ_Envelope *env;
879 if (NULL == GST_context)
882 GNUNET_SERVICE_client_drop (client);
885 if (client != GST_context->client)
888 GNUNET_SERVICE_client_drop (client);
892 name_len = ntohs (msg->name_len);
893 LOG_DEBUG ("Received BARRIER_STATUS for barrier `%s'\n",
895 GNUNET_CRYPTO_hash (name,
898 barrier = GNUNET_CONTAINER_multihashmap_get (barrier_map,
903 GNUNET_SERVICE_client_drop (client);
906 GNUNET_SERVICE_client_continue (client);
907 for (client_ctx = barrier->head; NULL != client_ctx; client_ctx =
908 client_ctx->next) /* Notify peers */
910 env = GNUNET_MQ_msg_copy (&msg->header);
911 GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client_ctx->client),
915 * The wrapper barriers do not echo the barrier status, so we have to do it
918 for (wrapper = barrier->whead; NULL != wrapper; wrapper = wrapper->next)
920 GNUNET_TESTBED_queue_message_ (wrapper->controller,
921 GNUNET_copy_message (&msg->header));
926 /* end of gnunet-service-testbed_barriers.c */