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/>.
20 * @file testbed/gnunet-service-testbed.h
21 * @brief data structures shared amongst components of TESTBED service
22 * @author Sree Harsha Totakura
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_core_service.h"
32 #include "testbed_api.h"
33 #include "testbed_api_operations.h"
34 #include "testbed_api_hosts.h"
35 #include "gnunet_testing_lib.h"
36 #include "gnunet-service-testbed_links.h"
42 #define LOG(kind,...) \
43 GNUNET_log (kind, __VA_ARGS__)
48 #define LOG_DEBUG(...) \
49 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52 * By how much should the arrays lists grow
54 #define LIST_GROW_STEP 10
68 * The destination host is reachable thru
75 * Context information for operations forwarded to subcontrollers
77 struct ForwardedOperationContext
80 * The next pointer for DLL
82 struct ForwardedOperationContext *next;
85 * The prev pointer for DLL
87 struct ForwardedOperationContext *prev;
90 * The generated operation context
92 struct OperationContext *opc;
95 * The client to which we have to reply
97 struct GNUNET_SERVICE_Client *client;
105 * Task ID for the timeout task
107 struct GNUNET_SCHEDULER_Task * timeout_task;
110 * The id of the operation that has been forwarded
112 uint64_t operation_id;
115 * The type of the operation which is forwarded
117 enum OperationType type;
123 * A DLL of host registrations to be made
125 struct HostRegistration
128 * next registration in the DLL
130 struct HostRegistration *next;
133 * previous registration in the DLL
135 struct HostRegistration *prev;
138 * The callback to call after this registration's status is available
140 GNUNET_TESTBED_HostRegistrationCompletion cb;
143 * The closure for the above callback
148 * The host that has to be registered
150 struct GNUNET_TESTBED_Host *host;
155 * Context information used while linking controllers
157 struct LinkControllersContext
160 * The client which initiated the link controller operation
162 struct GNUNET_SERVICE_Client *client;
165 * The ID of the operation
167 uint64_t operation_id;
183 * The peer handle from testing API
185 struct GNUNET_TESTING_Peer *peer;
188 * The modified (by GNUNET_TESTING_peer_configure) configuration this
189 * peer is configured with
191 struct GNUNET_CONFIGURATION_Handle *cfg;
194 * Is the peer running
203 * The slave this peer is started through
208 * The id of the remote host this peer is running on
210 uint32_t remote_host_id;
217 * Is this peer locally created?
222 * Our local reference id for this peer
227 * References to peers are using in forwarded overlay contexts and remote
228 * overlay connect contexts. A peer can only be destroyed after all such
229 * contexts are destroyed. For this, we maintain a reference counter. When we
230 * use a peer in any such context, we increment this counter. We decrement it
231 * when we are destroying these contexts
233 uint32_t reference_cnt;
236 * While destroying a peer, due to the fact that there could be references to
237 * this peer, we delay the peer destroy to a further time. We do this by using
238 * this flag to destroy the peer while destroying a context in which this peer
239 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
242 uint32_t destroy_flag;
248 * The main context information associated with the client which started us
253 * The client handle associated with this context
255 struct GNUNET_SERVICE_Client *client;
258 * The network address of the master controller
263 * The TESTING system handle for starting peers locally
265 struct GNUNET_TESTING_System *system;
268 * Our host id according to this context
275 * The structure for identifying a shared service
280 * The name of the shared service
285 * Number of shared peers per instance of the shared service
290 * Number of peers currently sharing the service
292 uint32_t num_sharing;
296 struct RegisteredHostContext;
300 * Context information to used during operations which forward the overlay
303 struct ForwardedOverlayConnectContext
306 * next ForwardedOverlayConnectContext in the DLL
308 struct ForwardedOverlayConnectContext *next;
311 * previous ForwardedOverlayConnectContext in the DLL
313 struct ForwardedOverlayConnectContext *prev;
316 * Which host does this FOCC belong to?
318 struct RegisteredHostContext *rhc;
321 * A copy of the original overlay connect message
323 struct GNUNET_MessageHeader *orig_msg;
328 struct GNUNET_SERVICE_Client *client;
331 * The id of the operation which created this context information
333 uint64_t operation_id;
346 * Id of the host where peer2 is running
348 uint32_t peer2_host_id;
353 * This context information will be created for each host that is registered at
354 * slave controllers during overlay connects.
356 struct RegisteredHostContext
359 * The host which is being registered
361 struct GNUNET_TESTBED_Host *reg_host;
364 * The host of the controller which has to connect to the above rhost
366 struct GNUNET_TESTBED_Host *host;
369 * Head of the ForwardedOverlayConnectContext DLL
371 struct ForwardedOverlayConnectContext *focc_dll_head;
374 * Tail of the ForwardedOverlayConnectContext DLL
376 struct ForwardedOverlayConnectContext *focc_dll_tail;
379 * Enumeration of states for this context
390 * State where we attempt to do the overlay connection again
399 * Context data for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
401 struct HandlerContext_ShutdownPeers
404 * The number of slave we expect to hear from since we forwarded the
405 * #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
407 unsigned int nslaves;
410 * Did we observe a timeout with respect to this operation at any of the
420 extern struct GNUNET_CONFIGURATION_Handle *GST_config;
423 * The master context; generated with the first INIT message
425 extern struct Context *GST_context;
428 * DLL head for forwarded operation contexts
430 extern struct ForwardedOperationContext *fopcq_head;
433 * DLL tail for forwarded operation contexts
435 extern struct ForwardedOperationContext *fopcq_tail;
438 * A list of peers we know about
440 extern struct Peer **GST_peer_list;
445 extern struct GNUNET_TESTBED_Host **GST_host_list;
448 * Operation queue for open file descriptors
450 extern struct OperationQueue *GST_opq_openfds;
453 * Timeout for operations which may take some time
455 const extern struct GNUNET_TIME_Relative GST_timeout;
458 * The size of the peer list
460 extern unsigned int GST_peer_list_size;
463 * The current number of peers running locally under this controller
465 extern unsigned int GST_num_local_peers;
468 * The size of the host list
470 extern unsigned int GST_host_list_size;
473 * The directory where to store load statistics data
475 extern char *GST_stats_dir;
478 * Condition to check if host id is valid
480 #define VALID_HOST_ID(id) \
481 ( ((id) < GST_host_list_size) && (NULL != GST_host_list[id]) )
484 * Condition to check if peer id is valid
486 #define VALID_PEER_ID(id) \
487 ( ((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]) )
491 * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
492 * several times we call it only once. The array is also made to grow in steps
495 * @param ptr the array pointer to grow
496 * @param size the size of array
497 * @param accommodate_size the size which the array has to accommdate; after
498 * this call the array will be big enough to accommdate sizes upto
501 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
504 unsigned int growth_size; \
505 GNUNET_assert (size <= accommodate_size); \
506 growth_size = size; \
507 while (growth_size <= accommodate_size) \
508 growth_size += LIST_GROW_STEP; \
509 GNUNET_array_grow (ptr, size, growth_size); \
510 GNUNET_assert (size > accommodate_size); \
515 * Function to destroy a peer
517 * @param peer the peer structure to destroy
520 GST_destroy_peer (struct Peer *peer);
524 * Stops and destroys all peers
527 GST_destroy_peers (void);
531 * Finds the route with directly connected host as destination through which
532 * the destination host can be reached
534 * @param host_id the id of the destination host
535 * @return the route with directly connected destination host; NULL if no route
539 GST_find_dest_route (uint32_t host_id);
543 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT messages
545 * @param cls identification of the client
546 * @param msg the actual message
549 handle_overlay_connect (void *cls,
550 const struct GNUNET_TESTBED_OverlayConnectMessage *msg);
554 * Adds a host registration's request to a slave's registration queue
556 * @param slave the slave controller at which the given host has to be
558 * @param cb the host registration completion callback
559 * @param cb_cls the closure for the host registration completion callback
560 * @param host the host which has to be registered
563 GST_queue_host_registration (struct Slave *slave,
564 GNUNET_TESTBED_HostRegistrationCompletion cb,
565 void *cb_cls, struct GNUNET_TESTBED_Host *host);
569 * Callback to relay the reply msg of a forwarded operation back to the client
571 * @param cls ForwardedOperationContext
572 * @param msg the message to relay
575 GST_forwarded_operation_reply_relay (void *cls,
576 const struct GNUNET_MessageHeader *msg);
580 * Task to free resources when forwarded operation has been timedout
582 * @param cls the ForwardedOperationContext
583 * @param tc the task context from scheduler
586 GST_forwarded_operation_timeout (void *cls);
590 * Clears the forwarded operations queue
593 GST_clear_fopcq (void);
597 * Send operation failure message to client
599 * @param client the client to which the failure message has to be sent to
600 * @param operation_id the id of the failed operation
601 * @param emsg the error message; can be NULL
604 GST_send_operation_fail_msg (struct GNUNET_SERVICE_Client *client,
605 uint64_t operation_id,
610 * Notify OC subsystem that @a client disconnected.
612 * @param client the client that disconnected
615 GST_notify_client_disconnect_oc (struct GNUNET_SERVICE_Client *client);
619 * Notify peers subsystem that @a client disconnected.
621 * @param client the client that disconnected
624 GST_notify_client_disconnect_peers (struct GNUNET_SERVICE_Client *client);
628 * Function to send generic operation success message to given client
630 * @param client the client to send the message to
631 * @param operation_id the id of the operation which was successful
634 GST_send_operation_success_msg (struct GNUNET_SERVICE_Client *client,
635 uint64_t operation_id);
639 * Check #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
641 * @param cls identification of the client
642 * @param msg the actual message
643 * @return #GNUNET_OK if @a msg is well-formed
646 check_remote_overlay_connect (void *cls,
647 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
651 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
653 * @param cls identification of the client
654 * @param msg the actual message
657 handle_remote_overlay_connect (void *cls,
658 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
662 * Check #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
664 * @param cls identification of the client
665 * @param msg the actual message
666 * @return #GNUNET_OK if @a msg is well-formed
669 check_peer_create (void *cls,
670 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
674 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
676 * @param cls identification of the client
677 * @param message the actual message
680 handle_peer_create (void *cls,
681 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
685 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
687 * @param cls identification of the client
688 * @param msg the actual message
691 handle_peer_destroy (void *cls,
692 const struct GNUNET_TESTBED_PeerDestroyMessage *msg);
696 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
698 * @param cls identification of the client
699 * @param msg the actual message
702 handle_peer_start (void *cls,
703 const struct GNUNET_TESTBED_PeerStartMessage *msg);
707 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
709 * @param cls identification of the client
710 * @param message the actual message
713 handle_peer_stop (void *cls,
714 const struct GNUNET_TESTBED_PeerStopMessage *msg);
718 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
720 * @param cls identification of the client
721 * @param msg the actual message
724 handle_peer_get_config (void *cls,
725 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg);
729 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
731 * @param cls identification of the client
732 * @param msg the actual message
735 handle_shutdown_peers (void *cls,
736 const struct GNUNET_TESTBED_ShutdownPeersMessage *msg);
740 * Check #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
742 * @param cls identification of client
743 * @param msg the actual message
744 * @return #GNUNET_OK if @a msg is well-formed
747 check_manage_peer_service (void *cls,
748 const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
752 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
754 * @param cls identification of client
755 * @param msg the actual message
758 handle_manage_peer_service (void *cls,
759 const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
765 * Check #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
767 * @param cls identification of the client
768 * @param msg the actual message
769 * @return #GNUNET_OK if @a msg is well-formed
772 check_peer_reconfigure (void *cls,
773 const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
777 * Handler for #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
778 * Should stop the peer asyncronously, destroy it and create it again with the
781 * @param cls identification of the client
782 * @param msg the actual message
785 handle_peer_reconfigure (void *cls,
786 const struct 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 */