2 This file is part of GNUnet.
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_SERVER_Client *client;
107 * Task ID for the timeout task
109 GNUNET_SCHEDULER_TaskIdentifier 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_SERVER_Client *client;
167 * The ID of the operation
169 uint64_t operation_id;
186 * The peer handle from testing API
188 struct GNUNET_TESTING_Peer *peer;
191 * The modified (by GNUNET_TESTING_peer_configure) configuration this
192 * peer is configured with
194 struct GNUNET_CONFIGURATION_Handle *cfg;
197 * Is the peer running
206 * The slave this peer is started through
211 * The id of the remote host this peer is running on
213 uint32_t remote_host_id;
220 * Is this peer locally created?
225 * Our local reference id for this peer
230 * References to peers are using in forwarded overlay contexts and remote
231 * overlay connect contexts. A peer can only be destroyed after all such
232 * contexts are destroyed. For this, we maintain a reference counter. When we
233 * use a peer in any such context, we increment this counter. We decrement it
234 * when we are destroying these contexts
236 uint32_t reference_cnt;
239 * While destroying a peer, due to the fact that there could be references to
240 * this peer, we delay the peer destroy to a further time. We do this by using
241 * this flag to destroy the peer while destroying a context in which this peer
242 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
245 uint32_t destroy_flag;
251 * The main context information associated with the client which started us
256 * The client handle associated with this context
258 struct GNUNET_SERVER_Client *client;
261 * The network address of the master controller
266 * The TESTING system handle for starting peers locally
268 struct GNUNET_TESTING_System *system;
271 * Our host id according to this context
278 * The structure for identifying a shared service
283 * The name of the shared service
288 * Number of shared peers per instance of the shared service
293 * Number of peers currently sharing the service
295 uint32_t num_sharing;
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 * A copy of the original overlay connect message
318 struct GNUNET_MessageHeader *orig_msg;
323 struct GNUNET_SERVER_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
385 * State where we attempt to do the overlay connection again
394 * Context data for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
396 struct HandlerContext_ShutdownPeers
399 * The number of slave we expect to hear from since we forwarded the
400 * GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
402 unsigned int nslaves;
405 * Did we observe a timeout with respect to this operation at any of the
415 struct GNUNET_CONFIGURATION_Handle *our_config;
418 * The master context; generated with the first INIT message
420 extern struct Context *GST_context;
423 * DLL head for forwarded operation contexts
425 extern struct ForwardedOperationContext *fopcq_head;
428 * DLL tail for forwarded operation contexts
430 extern struct ForwardedOperationContext *fopcq_tail;
433 * A list of peers we know about
435 extern struct Peer **GST_peer_list;
440 extern struct GNUNET_TESTBED_Host **GST_host_list;
443 * Operation queue for open file descriptors
445 extern struct OperationQueue *GST_opq_openfds;
448 * Timeout for operations which may take some time
450 const extern struct GNUNET_TIME_Relative GST_timeout;
453 * The size of the peer list
455 extern unsigned int GST_peer_list_size;
458 * The size of the host list
460 extern unsigned int GST_host_list_size;
463 * The directory where to store load statistics data
465 extern char *GST_stats_dir;
468 * Condition to check if host id is valid
470 #define VALID_HOST_ID(id) \
471 ( ((id) < GST_host_list_size) && (NULL != GST_host_list[id]) )
474 * Condition to check if peer id is valid
476 #define VALID_PEER_ID(id) \
477 ( ((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]) )
481 * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
482 * several times we call it only once. The array is also made to grow in steps
485 * @param ptr the array pointer to grow
486 * @param size the size of array
487 * @param accommodate_size the size which the array has to accommdate; after
488 * this call the array will be big enough to accommdate sizes upto
491 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
494 unsigned int growth_size; \
495 GNUNET_assert (size <= accommodate_size); \
496 growth_size = size; \
497 while (growth_size <= accommodate_size) \
498 growth_size += LIST_GROW_STEP; \
499 GNUNET_array_grow (ptr, size, growth_size); \
500 GNUNET_assert (size > accommodate_size); \
505 * Queues a message in send queue for sending to the service
507 * @param client the client to whom the queued message has to be sent
508 * @param msg the message to queue
511 GST_queue_message (struct GNUNET_SERVER_Client *client,
512 struct GNUNET_MessageHeader *msg);
516 * Function to destroy a peer
518 * @param peer the peer structure to destroy
521 GST_destroy_peer (struct Peer *peer);
525 * Stops and destroys all peers
528 GST_destroy_peers ();
532 * Finds the route with directly connected host as destination through which
533 * the destination host can be reached
535 * @param host_id the id of the destination host
536 * @return the route with directly connected destination host; NULL if no route
540 GST_find_dest_route (uint32_t host_id);
544 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
547 * @param client identification of the client
548 * @param message the actual message
551 GST_handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
552 const struct GNUNET_MessageHeader *message);
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,
589 const struct GNUNET_SCHEDULER_TaskContext *tc);
593 * Clears the forwarded operations queue
600 * Send operation failure message to client
602 * @param client the client to which the failure message has to be sent to
603 * @param operation_id the id of the failed operation
604 * @param emsg the error message; can be NULL
607 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
608 uint64_t operation_id, const char *emsg);
612 * Function to send generic operation success message to given client
614 * @param client the client to send the message to
615 * @param operation_id the id of the operation which was successful
618 GST_send_operation_success_msg (struct GNUNET_SERVER_Client *client,
619 uint64_t operation_id);
623 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
626 * @param client identification of the client
627 * @param message the actual message
630 GST_handle_remote_overlay_connect (void *cls,
631 struct GNUNET_SERVER_Client *client,
632 const struct GNUNET_MessageHeader *message);
636 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
639 * @param client identification of the client
640 * @param message the actual message
643 GST_handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
644 const struct GNUNET_MessageHeader *message);
648 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
651 * @param client identification of the client
652 * @param message the actual message
655 GST_handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
656 const struct GNUNET_MessageHeader *message);
660 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
663 * @param client identification of the client
664 * @param message the actual message
667 GST_handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
668 const struct GNUNET_MessageHeader *message);
672 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
675 * @param client identification of the client
676 * @param message the actual message
679 GST_handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
680 const struct GNUNET_MessageHeader *message);
684 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
687 * @param client identification of the client
688 * @param message the actual message
691 GST_handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
692 const struct GNUNET_MessageHeader *message);
696 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
699 * @param client identification of the client
700 * @param message the actual message
703 GST_handle_shutdown_peers (void *cls, struct GNUNET_SERVER_Client *client,
704 const struct GNUNET_MessageHeader *message);
708 * Handler for GNUNET_TESTBED_ManagePeerServiceMessage message
711 * @param client identification of client
712 * @param message the actual message
715 GST_handle_manage_peer_service (void *cls, struct GNUNET_SERVER_Client *client,
716 const struct GNUNET_MessageHeader *message);
720 * Frees the ManageServiceContext queue
727 * Cleans up the queue used for forwarding link controllers requests
734 * Cleans up the route list
737 GST_route_list_clear ();
741 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
743 * @param rhc the RegisteredHostContext
746 GST_process_next_focc (struct RegisteredHostContext *rhc);
750 * Cleans up ForwardedOverlayConnectContext
752 * @param focc the ForwardedOverlayConnectContext to cleanup
755 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
759 * Clears all pending overlay connect contexts in queue
766 * Clears all pending remote overlay connect contexts in queue
773 * Initializes the cache
775 * @param size the size of the cache
778 GST_cache_init (unsigned int size);
789 * Looks up in the hello cache and returns the HELLO of the given peer
791 * @param peer_id the index of the peer whose HELLO has to be looked up
792 * @return the HELLO message; NULL if not found
794 const struct GNUNET_MessageHeader *
795 GST_cache_lookup_hello (const unsigned int peer_id);
799 * Caches the HELLO of the given peer. Updates the HELLO if it was already
802 * @param peer_id the peer identity of the peer whose HELLO has to be cached
803 * @param hello the HELLO message
806 GST_cache_add_hello (const unsigned int peer_id,
807 const struct GNUNET_MessageHeader *hello);
811 * Functions of this type are called when the needed handle is available for
812 * usage. These functions are to be registered with either of the functions
813 * GST_cache_get_handle_transport() or GST_cache_get_handle_core(). The
814 * corresponding handles will be set and if they are not, then it signals an
815 * error while opening the handles.
817 * @param cls the closure passed to GST_cache_get_handle_transport() or
818 * GST_cache_get_handle_core()
819 * @param ch the handle to CORE. Can be NULL if it is not requested
820 * @param th the handle to TRANSPORT. Can be NULL if it is not requested
821 * @param peer_id the identity of the peer. Will be NULL if ch is NULL. In other
822 * cases, its value being NULL means that CORE connection has failed.
824 typedef void (*GST_cache_handle_ready_cb) (void *cls,
825 struct GNUNET_CORE_Handle * ch,
826 struct GNUNET_TRANSPORT_Handle * th,
827 const struct GNUNET_PeerIdentity *
832 * Callback to notify when the target peer given to
833 * GST_cache_get_handle_transport() is connected. Note that this callback may
834 * not be called if the target peer is already connected. Use
835 * GNUNET_TRANSPORT_check_neighbour_connected() to check if the target peer is
836 * already connected or not. This callback will be called only once or never (in
837 * case the target cannot be connected).
839 * @param cls the closure given to GST_cache_get_handle_done() for this callback
840 * @param target the peer identity of the target peer. The pointer should be
841 * valid until GST_cache_get_handle_done() is called.
843 typedef void (*GST_cache_peer_connect_notify) (void *cls,
844 const struct GNUNET_PeerIdentity
849 * Get a transport handle with the given configuration. If the handle is already
850 * cached before, it will be retured in the given callback; the peer_id is used to lookup in the
851 * cache. If not a new operation is started to open the transport handle and
852 * will be given in the callback when it is available.
854 * @param peer_id the index of the peer
855 * @param cfg the configuration with which the transport handle has to be
856 * created if it was not present in the cache
857 * @param cb the callback to notify when the transport handle is available
858 * @param cb_cls the closure for the above callback
859 * @param target the peer identify of the peer whose connection to our TRANSPORT
860 * subsystem will be notified through the connect_notify_cb. Can be NULL
861 * @param connect_notify_cb the callback to call when the given target peer is
862 * connected. This callback will only be called once or never again (in
863 * case the target peer cannot be connected). Can be NULL
864 * @param connect_notify_cb_cls the closure for the above callback
865 * @return the handle which can be used cancel or mark that the handle is no
868 struct GSTCacheGetHandle *
869 GST_cache_get_handle_transport (unsigned int peer_id,
870 const struct GNUNET_CONFIGURATION_Handle *cfg,
871 GST_cache_handle_ready_cb cb, void *cb_cls,
872 const struct GNUNET_PeerIdentity *target,
873 GST_cache_peer_connect_notify connect_notify_cb,
874 void *connect_notify_cb_cls);
878 * Get a CORE handle with the given configuration. If the handle is already
879 * cached before, it will be retured in the given callback; the peer_id is used
880 * to lookup in the cache. If the handle is not cached before, a new operation
881 * is started to open the CORE handle and will be given in the callback when it
882 * is available along with the peer identity
884 * @param peer_id the index of the peer
885 * @param cfg the configuration with which the transport handle has to be
886 * created if it was not present in the cache
887 * @param cb the callback to notify when the transport handle is available
888 * @param cb_cls the closure for the above callback
889 * @param target the peer identify of the peer whose connection to our CORE
890 * subsystem will be notified through the connect_notify_cb. Can be NULL
891 * @param connect_notify_cb the callback to call when the given target peer is
892 * connected. This callback will only be called once or never again (in
893 * case the target peer cannot be connected). Can be NULL
894 * @param connect_notify_cb_cls the closure for the above callback
895 * @return the handle which can be used cancel or mark that the handle is no
898 struct GSTCacheGetHandle *
899 GST_cache_get_handle_core (unsigned int peer_id,
900 const struct GNUNET_CONFIGURATION_Handle *cfg,
901 GST_cache_handle_ready_cb cb, void *cb_cls,
902 const struct GNUNET_PeerIdentity *target,
903 GST_cache_peer_connect_notify connect_notify_cb,
904 void *connect_notify_cb_cls);
908 * Mark the GetCacheHandle as being done if a handle has been provided already
909 * or as being cancelled if the callback for the handle hasn't been called.
911 * @param cgh the CacheGetHandle handle
914 GST_cache_get_handle_done (struct GSTCacheGetHandle *cgh);
918 * Initialize logging CPU and IO statisticfs. Checks the configuration for
919 * "STATS_DIR" and logs to a file in that directory. The file is name is
920 * generated from the hostname and the process's PID.
923 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
927 * Shutdown the status calls module.
930 GST_stats_destroy ();
932 /* End of gnunet-service-testbed.h */