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.
17 * @file testbed/gnunet-service-testbed.h
18 * @brief data structures shared amongst components of TESTBED service
19 * @author Sree Harsha Totakura
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
25 #include "gnunet_transport_service.h"
26 #include "gnunet_core_service.h"
29 #include "testbed_api.h"
30 #include "testbed_api_operations.h"
31 #include "testbed_api_hosts.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet-service-testbed_links.h"
39 #define LOG(kind,...) \
40 GNUNET_log (kind, __VA_ARGS__)
45 #define LOG_DEBUG(...) \
46 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49 * By how much should the arrays lists grow
51 #define LIST_GROW_STEP 10
65 * The destination host is reachable thru
72 * Context information for operations forwarded to subcontrollers
74 struct ForwardedOperationContext
77 * The next pointer for DLL
79 struct ForwardedOperationContext *next;
82 * The prev pointer for DLL
84 struct ForwardedOperationContext *prev;
87 * The generated operation context
89 struct OperationContext *opc;
92 * The client to which we have to reply
94 struct GNUNET_SERVICE_Client *client;
102 * Task ID for the timeout task
104 struct GNUNET_SCHEDULER_Task * timeout_task;
107 * The id of the operation that has been forwarded
109 uint64_t operation_id;
112 * The type of the operation which is forwarded
114 enum OperationType type;
120 * A DLL of host registrations to be made
122 struct HostRegistration
125 * next registration in the DLL
127 struct HostRegistration *next;
130 * previous registration in the DLL
132 struct HostRegistration *prev;
135 * The callback to call after this registration's status is available
137 GNUNET_TESTBED_HostRegistrationCompletion cb;
140 * The closure for the above callback
145 * The host that has to be registered
147 struct GNUNET_TESTBED_Host *host;
152 * Context information used while linking controllers
154 struct LinkControllersContext
157 * The client which initiated the link controller operation
159 struct GNUNET_SERVICE_Client *client;
162 * The ID of the operation
164 uint64_t operation_id;
180 * The peer handle from testing API
182 struct GNUNET_TESTING_Peer *peer;
185 * The modified (by GNUNET_TESTING_peer_configure) configuration this
186 * peer is configured with
188 struct GNUNET_CONFIGURATION_Handle *cfg;
191 * Is the peer running
200 * The slave this peer is started through
205 * The id of the remote host this peer is running on
207 uint32_t remote_host_id;
214 * Is this peer locally created?
219 * Our local reference id for this peer
224 * References to peers are using in forwarded overlay contexts and remote
225 * overlay connect contexts. A peer can only be destroyed after all such
226 * contexts are destroyed. For this, we maintain a reference counter. When we
227 * use a peer in any such context, we increment this counter. We decrement it
228 * when we are destroying these contexts
230 uint32_t reference_cnt;
233 * While destroying a peer, due to the fact that there could be references to
234 * this peer, we delay the peer destroy to a further time. We do this by using
235 * this flag to destroy the peer while destroying a context in which this peer
236 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
239 uint32_t destroy_flag;
245 * The main context information associated with the client which started us
250 * The client handle associated with this context
252 struct GNUNET_SERVICE_Client *client;
255 * The network address of the master controller
260 * The TESTING system handle for starting peers locally
262 struct GNUNET_TESTING_System *system;
265 * Our host id according to this context
272 * The structure for identifying a shared service
277 * The name of the shared service
282 * Number of shared peers per instance of the shared service
287 * Number of peers currently sharing the service
289 uint32_t num_sharing;
293 struct RegisteredHostContext;
297 * Context information to used during operations which forward the overlay
300 struct ForwardedOverlayConnectContext
303 * next ForwardedOverlayConnectContext in the DLL
305 struct ForwardedOverlayConnectContext *next;
308 * previous ForwardedOverlayConnectContext in the DLL
310 struct ForwardedOverlayConnectContext *prev;
313 * Which host does this FOCC belong to?
315 struct RegisteredHostContext *rhc;
318 * A copy of the original overlay connect message
320 struct GNUNET_MessageHeader *orig_msg;
325 struct GNUNET_SERVICE_Client *client;
328 * The id of the operation which created this context information
330 uint64_t operation_id;
343 * Id of the host where peer2 is running
345 uint32_t peer2_host_id;
350 * This context information will be created for each host that is registered at
351 * slave controllers during overlay connects.
353 struct RegisteredHostContext
356 * The host which is being registered
358 struct GNUNET_TESTBED_Host *reg_host;
361 * The host of the controller which has to connect to the above rhost
363 struct GNUNET_TESTBED_Host *host;
366 * Head of the ForwardedOverlayConnectContext DLL
368 struct ForwardedOverlayConnectContext *focc_dll_head;
371 * Tail of the ForwardedOverlayConnectContext DLL
373 struct ForwardedOverlayConnectContext *focc_dll_tail;
376 * Enumeration of states for this context
387 * State where we attempt to do the overlay connection again
396 * Context data for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
398 struct HandlerContext_ShutdownPeers
401 * The number of slave we expect to hear from since we forwarded the
402 * #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
404 unsigned int nslaves;
407 * Did we observe a timeout with respect to this operation at any of the
417 extern struct GNUNET_CONFIGURATION_Handle *GST_config;
420 * The master context; generated with the first INIT message
422 extern struct Context *GST_context;
425 * DLL head for forwarded operation contexts
427 extern struct ForwardedOperationContext *fopcq_head;
430 * DLL tail for forwarded operation contexts
432 extern struct ForwardedOperationContext *fopcq_tail;
435 * A list of peers we know about
437 extern struct Peer **GST_peer_list;
442 extern struct GNUNET_TESTBED_Host **GST_host_list;
445 * Operation queue for open file descriptors
447 extern struct OperationQueue *GST_opq_openfds;
450 * Timeout for operations which may take some time
452 const extern struct GNUNET_TIME_Relative GST_timeout;
455 * The size of the peer list
457 extern unsigned int GST_peer_list_size;
460 * The current number of peers running locally under this controller
462 extern unsigned int GST_num_local_peers;
465 * The size of the host list
467 extern unsigned int GST_host_list_size;
470 * The directory where to store load statistics data
472 extern char *GST_stats_dir;
475 * Condition to check if host id is valid
477 #define VALID_HOST_ID(id) \
478 ( ((id) < GST_host_list_size) && (NULL != GST_host_list[id]) )
481 * Condition to check if peer id is valid
483 #define VALID_PEER_ID(id) \
484 ( ((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]) )
488 * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
489 * several times we call it only once. The array is also made to grow in steps
492 * @param ptr the array pointer to grow
493 * @param size the size of array
494 * @param accommodate_size the size which the array has to accommdate; after
495 * this call the array will be big enough to accommdate sizes upto
498 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
501 unsigned int growth_size; \
502 GNUNET_assert (size <= accommodate_size); \
503 growth_size = size; \
504 while (growth_size <= accommodate_size) \
505 growth_size += LIST_GROW_STEP; \
506 GNUNET_array_grow (ptr, size, growth_size); \
507 GNUNET_assert (size > accommodate_size); \
512 * Function to destroy a peer
514 * @param peer the peer structure to destroy
517 GST_destroy_peer (struct Peer *peer);
521 * Stops and destroys all peers
524 GST_destroy_peers (void);
528 * Finds the route with directly connected host as destination through which
529 * the destination host can be reached
531 * @param host_id the id of the destination host
532 * @return the route with directly connected destination host; NULL if no route
536 GST_find_dest_route (uint32_t host_id);
540 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT messages
542 * @param cls identification of the client
543 * @param msg the actual message
546 handle_overlay_connect (void *cls,
547 const struct GNUNET_TESTBED_OverlayConnectMessage *msg);
551 * Adds a host registration's request to a slave's registration queue
553 * @param slave the slave controller at which the given host has to be
555 * @param cb the host registration completion callback
556 * @param cb_cls the closure for the host registration completion callback
557 * @param host the host which has to be registered
560 GST_queue_host_registration (struct Slave *slave,
561 GNUNET_TESTBED_HostRegistrationCompletion cb,
562 void *cb_cls, struct GNUNET_TESTBED_Host *host);
566 * Callback to relay the reply msg of a forwarded operation back to the client
568 * @param cls ForwardedOperationContext
569 * @param msg the message to relay
572 GST_forwarded_operation_reply_relay (void *cls,
573 const struct GNUNET_MessageHeader *msg);
577 * Task to free resources when forwarded operation has been timedout
579 * @param cls the ForwardedOperationContext
580 * @param tc the task context from scheduler
583 GST_forwarded_operation_timeout (void *cls);
587 * Clears the forwarded operations queue
590 GST_clear_fopcq (void);
594 * Send operation failure message to client
596 * @param client the client to which the failure message has to be sent to
597 * @param operation_id the id of the failed operation
598 * @param emsg the error message; can be NULL
601 GST_send_operation_fail_msg (struct GNUNET_SERVICE_Client *client,
602 uint64_t operation_id,
607 * Notify OC subsystem that @a client disconnected.
609 * @param client the client that disconnected
612 GST_notify_client_disconnect_oc (struct GNUNET_SERVICE_Client *client);
616 * Notify peers subsystem that @a client disconnected.
618 * @param client the client that disconnected
621 GST_notify_client_disconnect_peers (struct GNUNET_SERVICE_Client *client);
625 * Function to send generic operation success message to given client
627 * @param client the client to send the message to
628 * @param operation_id the id of the operation which was successful
631 GST_send_operation_success_msg (struct GNUNET_SERVICE_Client *client,
632 uint64_t operation_id);
636 * Check #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
638 * @param cls identification of the client
639 * @param msg the actual message
640 * @return #GNUNET_OK if @a msg is well-formed
643 check_remote_overlay_connect (void *cls,
644 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
648 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
650 * @param cls identification of the client
651 * @param msg the actual message
654 handle_remote_overlay_connect (void *cls,
655 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
659 * Check #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
661 * @param cls identification of the client
662 * @param msg the actual message
663 * @return #GNUNET_OK if @a msg is well-formed
666 check_peer_create (void *cls,
667 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
671 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
673 * @param cls identification of the client
674 * @param message the actual message
677 handle_peer_create (void *cls,
678 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
682 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
684 * @param cls identification of the client
685 * @param msg the actual message
688 handle_peer_destroy (void *cls,
689 const struct GNUNET_TESTBED_PeerDestroyMessage *msg);
693 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
695 * @param cls identification of the client
696 * @param msg the actual message
699 handle_peer_start (void *cls,
700 const struct GNUNET_TESTBED_PeerStartMessage *msg);
704 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
706 * @param cls identification of the client
707 * @param message the actual message
710 handle_peer_stop (void *cls,
711 const struct GNUNET_TESTBED_PeerStopMessage *msg);
715 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
717 * @param cls identification of the client
718 * @param msg the actual message
721 handle_peer_get_config (void *cls,
722 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg);
726 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
728 * @param cls identification of the client
729 * @param msg the actual message
732 handle_shutdown_peers (void *cls,
733 const struct GNUNET_TESTBED_ShutdownPeersMessage *msg);
737 * Check #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
739 * @param cls identification of client
740 * @param msg the actual message
741 * @return #GNUNET_OK if @a msg is well-formed
744 check_manage_peer_service (void *cls,
745 const struct 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,
756 const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
762 * Check #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
764 * @param cls identification of the client
765 * @param msg the actual message
766 * @return #GNUNET_OK if @a msg is well-formed
769 check_peer_reconfigure (void *cls,
770 const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
774 * Handler for #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
775 * Should stop the peer asyncronously, destroy it and create it again with the
778 * @param cls identification of the client
779 * @param msg the actual message
782 handle_peer_reconfigure (void *cls,
783 const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
787 * Frees the ManageServiceContext queue
790 GST_free_mctxq (void);
794 * Cleans up the queue used for forwarding link controllers requests
801 * Cleans up the route list
804 GST_route_list_clear (void);
808 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
810 * @param rhc the RegisteredHostContext
813 GST_process_next_focc (struct RegisteredHostContext *rhc);
817 * Cleans up ForwardedOverlayConnectContext
819 * @param focc the ForwardedOverlayConnectContext to cleanup
822 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
826 * Clears all pending overlay connect contexts in queue
829 GST_free_occq (void);
833 * Clears all pending remote overlay connect contexts in queue
836 GST_free_roccq (void);
840 * Cleans up the Peer reconfigure context list
843 GST_free_prcq (void);
847 * Initializes the cache
849 * @param size the size of the cache
852 GST_cache_init (unsigned int size);
859 GST_cache_clear (void);
863 * Looks up in the hello cache and returns the HELLO of the given peer
865 * @param peer_id the index of the peer whose HELLO has to be looked up
866 * @return the HELLO message; NULL if not found
868 const struct GNUNET_MessageHeader *
869 GST_cache_lookup_hello (const unsigned int peer_id);
873 * Caches the HELLO of the given peer. Updates the HELLO if it was already
876 * @param peer_id the peer identity of the peer whose HELLO has to be cached
877 * @param hello the HELLO message
880 GST_cache_add_hello (const unsigned int peer_id,
881 const struct GNUNET_MessageHeader *hello);
885 * Initialize logging CPU and IO statisticfs. Checks the configuration for
886 * "STATS_DIR" and logs to a file in that directory. The file is name is
887 * generated from the hostname and the process's PID.
890 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
894 * Shutdown the status calls module.
897 GST_stats_destroy (void);
899 /* End of gnunet-service-testbed.h */