2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 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.h
23 * @brief data structures shared amongst components of TESTBED service
24 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
34 #include "testbed_api.h"
35 #include "testbed_api_operations.h"
36 #include "testbed_api_hosts.h"
37 #include "gnunet_testing_lib.h"
38 #include "gnunet-service-testbed_links.h"
44 #define LOG(kind, ...) \
45 GNUNET_log (kind, __VA_ARGS__)
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
54 * By how much should the arrays lists grow
56 #define LIST_GROW_STEP 10
70 * The destination host is reachable thru
77 * Context information for operations forwarded to subcontrollers
79 struct ForwardedOperationContext
82 * The next pointer for DLL
84 struct ForwardedOperationContext *next;
87 * The prev pointer for DLL
89 struct ForwardedOperationContext *prev;
92 * The generated operation context
94 struct OperationContext *opc;
97 * The client to which we have to reply
99 struct GNUNET_SERVICE_Client *client;
107 * Task ID for the timeout task
109 struct GNUNET_SCHEDULER_Task *timeout_task;
112 * The id of the operation that has been forwarded
114 uint64_t operation_id;
117 * The type of the operation which is forwarded
119 enum OperationType type;
124 * A DLL of host registrations to be made
126 struct HostRegistration
129 * next registration in the DLL
131 struct HostRegistration *next;
134 * previous registration in the DLL
136 struct HostRegistration *prev;
139 * The callback to call after this registration's status is available
141 GNUNET_TESTBED_HostRegistrationCompletion cb;
144 * The closure for the above callback
149 * The host that has to be registered
151 struct GNUNET_TESTBED_Host *host;
156 * Context information used while linking controllers
158 struct LinkControllersContext
161 * The client which initiated the link controller operation
163 struct GNUNET_SERVICE_Client *client;
166 * The ID of the operation
168 uint64_t operation_id;
182 * The peer handle from testing API
184 struct GNUNET_TESTING_Peer *peer;
187 * The modified (by GNUNET_TESTING_peer_configure) configuration this
188 * peer is configured with
190 struct GNUNET_CONFIGURATION_Handle *cfg;
193 * Is the peer running
201 * The slave this peer is started through
206 * The id of the remote host this peer is running on
208 uint32_t remote_host_id;
213 * Is this peer locally created?
218 * Our local reference id for this peer
223 * References to peers are using in forwarded overlay contexts and remote
224 * overlay connect contexts. A peer can only be destroyed after all such
225 * contexts are destroyed. For this, we maintain a reference counter. When we
226 * use a peer in any such context, we increment this counter. We decrement it
227 * when we are destroying these contexts
229 uint32_t reference_cnt;
232 * While destroying a peer, due to the fact that there could be references to
233 * this peer, we delay the peer destroy to a further time. We do this by using
234 * this flag to destroy the peer while destroying a context in which this peer
235 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
238 uint32_t destroy_flag;
243 * The main context information associated with the client which started us
248 * The client handle associated with this context
250 struct GNUNET_SERVICE_Client *client;
253 * The network address of the master controller
258 * The TESTING system handle for starting peers locally
260 struct GNUNET_TESTING_System *system;
263 * Our host id according to this context
270 * The structure for identifying a shared service
275 * The name of the shared service
280 * Number of shared peers per instance of the shared service
285 * Number of peers currently sharing the service
287 uint32_t num_sharing;
291 struct RegisteredHostContext;
295 * Context information to used during operations which forward the overlay
298 struct ForwardedOverlayConnectContext
301 * next ForwardedOverlayConnectContext in the DLL
303 struct ForwardedOverlayConnectContext *next;
306 * previous ForwardedOverlayConnectContext in the DLL
308 struct ForwardedOverlayConnectContext *prev;
311 * Which host does this FOCC belong to?
313 struct RegisteredHostContext *rhc;
316 * A copy of the original overlay connect message
318 struct GNUNET_MessageHeader *orig_msg;
323 struct GNUNET_SERVICE_Client *client;
326 * The id of the operation which created this context information
328 uint64_t operation_id;
341 * Id of the host where peer2 is running
343 uint32_t peer2_host_id;
348 * This context information will be created for each host that is registered at
349 * slave controllers during overlay connects.
351 struct RegisteredHostContext
354 * The host which is being registered
356 struct GNUNET_TESTBED_Host *reg_host;
359 * The host of the controller which has to connect to the above rhost
361 struct GNUNET_TESTBED_Host *host;
364 * Head of the ForwardedOverlayConnectContext DLL
366 struct ForwardedOverlayConnectContext *focc_dll_head;
369 * Tail of the ForwardedOverlayConnectContext DLL
371 struct ForwardedOverlayConnectContext *focc_dll_tail;
374 * Enumeration of states for this context
384 * State where we attempt to do the overlay connection again
392 * Context data for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
394 struct HandlerContext_ShutdownPeers
397 * The number of slave we expect to hear from since we forwarded the
398 * #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
400 unsigned int nslaves;
403 * Did we observe a timeout with respect to this operation at any of the
413 extern struct GNUNET_CONFIGURATION_Handle *GST_config;
416 * The master context; generated with the first INIT message
418 extern struct Context *GST_context;
421 * DLL head for forwarded operation contexts
423 extern struct ForwardedOperationContext *fopcq_head;
426 * DLL tail for forwarded operation contexts
428 extern struct ForwardedOperationContext *fopcq_tail;
431 * A list of peers we know about
433 extern struct Peer **GST_peer_list;
438 extern struct GNUNET_TESTBED_Host **GST_host_list;
441 * Operation queue for open file descriptors
443 extern struct OperationQueue *GST_opq_openfds;
446 * Timeout for operations which may take some time
448 extern struct GNUNET_TIME_Relative GST_timeout;
451 * The size of the peer list
453 extern unsigned int GST_peer_list_size;
456 * The current number of peers running locally under this controller
458 extern unsigned int GST_num_local_peers;
461 * The size of the host list
463 extern unsigned int GST_host_list_size;
466 * The directory where to store load statistics data
468 extern char *GST_stats_dir;
471 * Condition to check if host id is valid
473 #define VALID_HOST_ID(id) \
474 (((id) < GST_host_list_size) && (NULL != GST_host_list[id]))
477 * Condition to check if peer id is valid
479 #define VALID_PEER_ID(id) \
480 (((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]))
484 * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
485 * several times we call it only once. The array is also made to grow in steps
488 * @param ptr the array pointer to grow
489 * @param size the size of array
490 * @param accommodate_size the size which the array has to accommdate; after
491 * this call the array will be big enough to accommdate sizes upto
494 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
497 unsigned int growth_size; \
498 GNUNET_assert (size <= accommodate_size); \
499 growth_size = size; \
500 while (growth_size <= accommodate_size) \
501 growth_size += LIST_GROW_STEP; \
502 GNUNET_array_grow (ptr, size, growth_size); \
503 GNUNET_assert (size > accommodate_size); \
508 * Function to destroy a peer
510 * @param peer the peer structure to destroy
513 GST_destroy_peer (struct Peer *peer);
517 * Stops and destroys all peers
520 GST_destroy_peers (void);
524 * Finds the route with directly connected host as destination through which
525 * the destination host can be reached
527 * @param host_id the id of the destination host
528 * @return the route with directly connected destination host; NULL if no route
532 GST_find_dest_route (uint32_t host_id);
536 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT messages
538 * @param cls identification of the client
539 * @param msg the actual message
542 handle_overlay_connect (void *cls,
543 const struct GNUNET_TESTBED_OverlayConnectMessage *msg);
547 * Adds a host registration's request to a slave's registration queue
549 * @param slave the slave controller at which the given host has to be
551 * @param cb the host registration completion callback
552 * @param cb_cls the closure for the host registration completion callback
553 * @param host the host which has to be registered
556 GST_queue_host_registration (struct Slave *slave,
557 GNUNET_TESTBED_HostRegistrationCompletion cb,
558 void *cb_cls, struct GNUNET_TESTBED_Host *host);
562 * Callback to relay the reply msg of a forwarded operation back to the client
564 * @param cls ForwardedOperationContext
565 * @param msg the message to relay
568 GST_forwarded_operation_reply_relay (void *cls,
569 const struct GNUNET_MessageHeader *msg);
573 * Task to free resources when forwarded operation has been timedout
575 * @param cls the ForwardedOperationContext
576 * @param tc the task context from scheduler
579 GST_forwarded_operation_timeout (void *cls);
583 * Clears the forwarded operations queue
586 GST_clear_fopcq (void);
590 * Send operation failure message to client
592 * @param client the client to which the failure message has to be sent to
593 * @param operation_id the id of the failed operation
594 * @param emsg the error message; can be NULL
597 GST_send_operation_fail_msg (struct GNUNET_SERVICE_Client *client,
598 uint64_t operation_id,
603 * Notify OC subsystem that @a client disconnected.
605 * @param client the client that disconnected
608 GST_notify_client_disconnect_oc (struct GNUNET_SERVICE_Client *client);
612 * Notify peers subsystem that @a client disconnected.
614 * @param client the client that disconnected
617 GST_notify_client_disconnect_peers (struct GNUNET_SERVICE_Client *client);
621 * Function to send generic operation success message to given client
623 * @param client the client to send the message to
624 * @param operation_id the id of the operation which was successful
627 GST_send_operation_success_msg (struct GNUNET_SERVICE_Client *client,
628 uint64_t operation_id);
632 * Check #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
634 * @param cls identification of the client
635 * @param msg the actual message
636 * @return #GNUNET_OK if @a msg is well-formed
639 check_remote_overlay_connect (void *cls,
641 GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
645 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
647 * @param cls identification of the client
648 * @param msg the actual message
651 handle_remote_overlay_connect (void *cls,
653 GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
657 * Check #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
659 * @param cls identification of the client
660 * @param msg the actual message
661 * @return #GNUNET_OK if @a msg is well-formed
664 check_peer_create (void *cls,
665 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
669 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
671 * @param cls identification of the client
672 * @param message the actual message
675 handle_peer_create (void *cls,
676 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
680 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
682 * @param cls identification of the client
683 * @param msg the actual message
686 handle_peer_destroy (void *cls,
687 const struct GNUNET_TESTBED_PeerDestroyMessage *msg);
691 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
693 * @param cls identification of the client
694 * @param msg the actual message
697 handle_peer_start (void *cls,
698 const struct GNUNET_TESTBED_PeerStartMessage *msg);
702 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
704 * @param cls identification of the client
705 * @param message the actual message
708 handle_peer_stop (void *cls,
709 const struct GNUNET_TESTBED_PeerStopMessage *msg);
713 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
715 * @param cls identification of the client
716 * @param msg the actual message
719 handle_peer_get_config (void *cls,
721 GNUNET_TESTBED_PeerGetConfigurationMessage *msg);
725 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
727 * @param cls identification of the client
728 * @param msg the actual message
731 handle_shutdown_peers (void *cls,
732 const struct GNUNET_TESTBED_ShutdownPeersMessage *msg);
736 * Check #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
738 * @param cls identification of client
739 * @param msg the actual message
740 * @return #GNUNET_OK if @a msg is well-formed
743 check_manage_peer_service (void *cls,
745 GNUNET_TESTBED_ManagePeerServiceMessage *msg);
749 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
751 * @param cls identification of client
752 * @param msg the actual message
755 handle_manage_peer_service (void *cls,
757 GNUNET_TESTBED_ManagePeerServiceMessage *msg);
763 * Check #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
765 * @param cls identification of the client
766 * @param msg the actual message
767 * @return #GNUNET_OK if @a msg is well-formed
770 check_peer_reconfigure (void *cls,
772 GNUNET_TESTBED_PeerReconfigureMessage *msg);
776 * Handler for #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
777 * Should stop the peer asyncronously, destroy it and create it again with the
780 * @param cls identification of the client
781 * @param msg the actual message
784 handle_peer_reconfigure (void *cls,
786 GNUNET_TESTBED_PeerReconfigureMessage *msg);
790 * Frees the ManageServiceContext queue
793 GST_free_mctxq (void);
797 * Cleans up the queue used for forwarding link controllers requests
804 * Cleans up the route list
807 GST_route_list_clear (void);
811 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
813 * @param rhc the RegisteredHostContext
816 GST_process_next_focc (struct RegisteredHostContext *rhc);
820 * Cleans up ForwardedOverlayConnectContext
822 * @param focc the ForwardedOverlayConnectContext to cleanup
825 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
829 * Clears all pending overlay connect contexts in queue
832 GST_free_occq (void);
836 * Clears all pending remote overlay connect contexts in queue
839 GST_free_roccq (void);
843 * Cleans up the Peer reconfigure context list
846 GST_free_prcq (void);
850 * Initializes the cache
852 * @param size the size of the cache
855 GST_cache_init (unsigned int size);
862 GST_cache_clear (void);
866 * Looks up in the hello cache and returns the HELLO of the given peer
868 * @param peer_id the index of the peer whose HELLO has to be looked up
869 * @return the HELLO message; NULL if not found
871 const struct GNUNET_MessageHeader *
872 GST_cache_lookup_hello (const unsigned int peer_id);
876 * Caches the HELLO of the given peer. Updates the HELLO if it was already
879 * @param peer_id the peer identity of the peer whose HELLO has to be cached
880 * @param hello the HELLO message
883 GST_cache_add_hello (const unsigned int peer_id,
884 const struct GNUNET_MessageHeader *hello);
888 * Initialize logging CPU and IO statisticfs. Checks the configuration for
889 * "STATS_DIR" and logs to a file in that directory. The file is name is
890 * generated from the hostname and the process's PID.
893 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
897 * Shutdown the status calls module.
900 GST_stats_destroy (void);
902 /* End of gnunet-service-testbed.h */