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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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;
125 * A DLL of host registrations to be made
127 struct HostRegistration
130 * next registration in the DLL
132 struct HostRegistration *next;
135 * previous registration in the DLL
137 struct HostRegistration *prev;
140 * The callback to call after this registration's status is available
142 GNUNET_TESTBED_HostRegistrationCompletion cb;
145 * The closure for the above callback
150 * The host that has to be registered
152 struct GNUNET_TESTBED_Host *host;
157 * Context information used while linking controllers
159 struct LinkControllersContext
162 * The client which initiated the link controller operation
164 struct GNUNET_SERVICE_Client *client;
167 * The ID of the operation
169 uint64_t operation_id;
185 * The peer handle from testing API
187 struct GNUNET_TESTING_Peer *peer;
190 * The modified (by GNUNET_TESTING_peer_configure) configuration this
191 * peer is configured with
193 struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Is the peer running
205 * The slave this peer is started through
210 * The id of the remote host this peer is running on
212 uint32_t remote_host_id;
219 * Is this peer locally created?
224 * Our local reference id for this peer
229 * References to peers are using in forwarded overlay contexts and remote
230 * overlay connect contexts. A peer can only be destroyed after all such
231 * contexts are destroyed. For this, we maintain a reference counter. When we
232 * use a peer in any such context, we increment this counter. We decrement it
233 * when we are destroying these contexts
235 uint32_t reference_cnt;
238 * While destroying a peer, due to the fact that there could be references to
239 * this peer, we delay the peer destroy to a further time. We do this by using
240 * this flag to destroy the peer while destroying a context in which this peer
241 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
244 uint32_t destroy_flag;
250 * The main context information associated with the client which started us
255 * The client handle associated with this context
257 struct GNUNET_SERVICE_Client *client;
260 * The network address of the master controller
265 * The TESTING system handle for starting peers locally
267 struct GNUNET_TESTING_System *system;
270 * Our host id according to this context
277 * The structure for identifying a shared service
282 * The name of the shared service
287 * Number of shared peers per instance of the shared service
292 * Number of peers currently sharing the service
294 uint32_t num_sharing;
298 struct RegisteredHostContext;
302 * Context information to used during operations which forward the overlay
305 struct ForwardedOverlayConnectContext
308 * next ForwardedOverlayConnectContext in the DLL
310 struct ForwardedOverlayConnectContext *next;
313 * previous ForwardedOverlayConnectContext in the DLL
315 struct ForwardedOverlayConnectContext *prev;
318 * Which host does this FOCC belong to?
320 struct RegisteredHostContext *rhc;
323 * A copy of the original overlay connect message
325 struct GNUNET_MessageHeader *orig_msg;
330 struct GNUNET_SERVICE_Client *client;
333 * The id of the operation which created this context information
335 uint64_t operation_id;
348 * Id of the host where peer2 is running
350 uint32_t peer2_host_id;
355 * This context information will be created for each host that is registered at
356 * slave controllers during overlay connects.
358 struct RegisteredHostContext
361 * The host which is being registered
363 struct GNUNET_TESTBED_Host *reg_host;
366 * The host of the controller which has to connect to the above rhost
368 struct GNUNET_TESTBED_Host *host;
371 * Head of the ForwardedOverlayConnectContext DLL
373 struct ForwardedOverlayConnectContext *focc_dll_head;
376 * Tail of the ForwardedOverlayConnectContext DLL
378 struct ForwardedOverlayConnectContext *focc_dll_tail;
381 * Enumeration of states for this context
392 * State where we attempt to do the overlay connection again
401 * Context data for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
403 struct HandlerContext_ShutdownPeers
406 * The number of slave we expect to hear from since we forwarded the
407 * #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
409 unsigned int nslaves;
412 * Did we observe a timeout with respect to this operation at any of the
422 extern struct GNUNET_CONFIGURATION_Handle *GST_config;
425 * The master context; generated with the first INIT message
427 extern struct Context *GST_context;
430 * DLL head for forwarded operation contexts
432 extern struct ForwardedOperationContext *fopcq_head;
435 * DLL tail for forwarded operation contexts
437 extern struct ForwardedOperationContext *fopcq_tail;
440 * A list of peers we know about
442 extern struct Peer **GST_peer_list;
447 extern struct GNUNET_TESTBED_Host **GST_host_list;
450 * Operation queue for open file descriptors
452 extern struct OperationQueue *GST_opq_openfds;
455 * Timeout for operations which may take some time
457 const extern struct GNUNET_TIME_Relative GST_timeout;
460 * The size of the peer list
462 extern unsigned int GST_peer_list_size;
465 * The current number of peers running locally under this controller
467 extern unsigned int GST_num_local_peers;
470 * The size of the host list
472 extern unsigned int GST_host_list_size;
475 * The directory where to store load statistics data
477 extern char *GST_stats_dir;
480 * Condition to check if host id is valid
482 #define VALID_HOST_ID(id) \
483 ( ((id) < GST_host_list_size) && (NULL != GST_host_list[id]) )
486 * Condition to check if peer id is valid
488 #define VALID_PEER_ID(id) \
489 ( ((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]) )
493 * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
494 * several times we call it only once. The array is also made to grow in steps
497 * @param ptr the array pointer to grow
498 * @param size the size of array
499 * @param accommodate_size the size which the array has to accommdate; after
500 * this call the array will be big enough to accommdate sizes upto
503 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
506 unsigned int growth_size; \
507 GNUNET_assert (size <= accommodate_size); \
508 growth_size = size; \
509 while (growth_size <= accommodate_size) \
510 growth_size += LIST_GROW_STEP; \
511 GNUNET_array_grow (ptr, size, growth_size); \
512 GNUNET_assert (size > accommodate_size); \
517 * Function to destroy a peer
519 * @param peer the peer structure to destroy
522 GST_destroy_peer (struct Peer *peer);
526 * Stops and destroys all peers
529 GST_destroy_peers (void);
533 * Finds the route with directly connected host as destination through which
534 * the destination host can be reached
536 * @param host_id the id of the destination host
537 * @return the route with directly connected destination host; NULL if no route
541 GST_find_dest_route (uint32_t host_id);
545 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT messages
547 * @param cls identification of the client
548 * @param msg the actual message
551 handle_overlay_connect (void *cls,
552 const struct GNUNET_TESTBED_OverlayConnectMessage *msg);
556 * Adds a host registration's request to a slave's registration queue
558 * @param slave the slave controller at which the given host has to be
560 * @param cb the host registration completion callback
561 * @param cb_cls the closure for the host registration completion callback
562 * @param host the host which has to be registered
565 GST_queue_host_registration (struct Slave *slave,
566 GNUNET_TESTBED_HostRegistrationCompletion cb,
567 void *cb_cls, struct GNUNET_TESTBED_Host *host);
571 * Callback to relay the reply msg of a forwarded operation back to the client
573 * @param cls ForwardedOperationContext
574 * @param msg the message to relay
577 GST_forwarded_operation_reply_relay (void *cls,
578 const struct GNUNET_MessageHeader *msg);
582 * Task to free resources when forwarded operation has been timedout
584 * @param cls the ForwardedOperationContext
585 * @param tc the task context from scheduler
588 GST_forwarded_operation_timeout (void *cls);
592 * Clears the forwarded operations queue
595 GST_clear_fopcq (void);
599 * Send operation failure message to client
601 * @param client the client to which the failure message has to be sent to
602 * @param operation_id the id of the failed operation
603 * @param emsg the error message; can be NULL
606 GST_send_operation_fail_msg (struct GNUNET_SERVICE_Client *client,
607 uint64_t operation_id,
612 * Notify OC subsystem that @a client disconnected.
614 * @param client the client that disconnected
617 GST_notify_client_disconnect_oc (struct GNUNET_SERVICE_Client *client);
621 * Notify peers subsystem that @a client disconnected.
623 * @param client the client that disconnected
626 GST_notify_client_disconnect_peers (struct GNUNET_SERVICE_Client *client);
630 * Function to send generic operation success message to given client
632 * @param client the client to send the message to
633 * @param operation_id the id of the operation which was successful
636 GST_send_operation_success_msg (struct GNUNET_SERVICE_Client *client,
637 uint64_t operation_id);
641 * Check #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
643 * @param cls identification of the client
644 * @param msg the actual message
645 * @return #GNUNET_OK if @a msg is well-formed
648 check_remote_overlay_connect (void *cls,
649 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
653 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
655 * @param cls identification of the client
656 * @param msg the actual message
659 handle_remote_overlay_connect (void *cls,
660 const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
664 * Check #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
666 * @param cls identification of the client
667 * @param msg the actual message
668 * @return #GNUNET_OK if @a msg is well-formed
671 check_peer_create (void *cls,
672 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
676 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
678 * @param cls identification of the client
679 * @param message the actual message
682 handle_peer_create (void *cls,
683 const struct GNUNET_TESTBED_PeerCreateMessage *msg);
687 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
689 * @param cls identification of the client
690 * @param msg the actual message
693 handle_peer_destroy (void *cls,
694 const struct GNUNET_TESTBED_PeerDestroyMessage *msg);
698 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
700 * @param cls identification of the client
701 * @param msg the actual message
704 handle_peer_start (void *cls,
705 const struct GNUNET_TESTBED_PeerStartMessage *msg);
709 * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
711 * @param cls identification of the client
712 * @param message the actual message
715 handle_peer_stop (void *cls,
716 const struct GNUNET_TESTBED_PeerStopMessage *msg);
720 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
722 * @param cls identification of the client
723 * @param msg the actual message
726 handle_peer_get_config (void *cls,
727 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg);
731 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
733 * @param cls identification of the client
734 * @param msg the actual message
737 handle_shutdown_peers (void *cls,
738 const struct GNUNET_TESTBED_ShutdownPeersMessage *msg);
742 * Check #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
744 * @param cls identification of client
745 * @param msg the actual message
746 * @return #GNUNET_OK if @a msg is well-formed
749 check_manage_peer_service (void *cls,
750 const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
754 * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
756 * @param cls identification of client
757 * @param msg the actual message
760 handle_manage_peer_service (void *cls,
761 const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
767 * Check #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
769 * @param cls identification of the client
770 * @param msg the actual message
771 * @return #GNUNET_OK if @a msg is well-formed
774 check_peer_reconfigure (void *cls,
775 const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
779 * Handler for #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
780 * Should stop the peer asyncronously, destroy it and create it again with the
783 * @param cls identification of the client
784 * @param msg the actual message
787 handle_peer_reconfigure (void *cls,
788 const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
792 * Frees the ManageServiceContext queue
795 GST_free_mctxq (void);
799 * Cleans up the queue used for forwarding link controllers requests
806 * Cleans up the route list
809 GST_route_list_clear (void);
813 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
815 * @param rhc the RegisteredHostContext
818 GST_process_next_focc (struct RegisteredHostContext *rhc);
822 * Cleans up ForwardedOverlayConnectContext
824 * @param focc the ForwardedOverlayConnectContext to cleanup
827 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
831 * Clears all pending overlay connect contexts in queue
834 GST_free_occq (void);
838 * Clears all pending remote overlay connect contexts in queue
841 GST_free_roccq (void);
845 * Cleans up the Peer reconfigure context list
848 GST_free_prcq (void);
852 * Initializes the cache
854 * @param size the size of the cache
857 GST_cache_init (unsigned int size);
864 GST_cache_clear (void);
868 * Looks up in the hello cache and returns the HELLO of the given peer
870 * @param peer_id the index of the peer whose HELLO has to be looked up
871 * @return the HELLO message; NULL if not found
873 const struct GNUNET_MessageHeader *
874 GST_cache_lookup_hello (const unsigned int peer_id);
878 * Caches the HELLO of the given peer. Updates the HELLO if it was already
881 * @param peer_id the peer identity of the peer whose HELLO has to be cached
882 * @param hello the HELLO message
885 GST_cache_add_hello (const unsigned int peer_id,
886 const struct GNUNET_MessageHeader *hello);
890 * Initialize logging CPU and IO statisticfs. Checks the configuration for
891 * "STATS_DIR" and logs to a file in that directory. The file is name is
892 * generated from the hostname and the process's PID.
895 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
899 * Shutdown the status calls module.
902 GST_stats_destroy (void);
904 /* End of gnunet-service-testbed.h */