- verboser log, faster start
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.h
1 /*
2   This file is part of GNUnet.
3   (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file testbed/gnunet-service-testbed.h
23  * @brief data structures shared amongst components of TESTBED service
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32
33 #include "testbed.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
39
40 /**
41  * Generic logging
42  */
43 #define LOG(kind,...)                           \
44   GNUNET_log (kind, __VA_ARGS__)
45
46 /**
47  * Debug logging
48  */
49 #define LOG_DEBUG(...)                          \
50   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
51
52 /**
53  * By how much should the arrays lists grow
54  */
55 #define LIST_GROW_STEP 10
56
57
58 /**
59  * A routing entry
60  */
61 struct Route
62 {
63   /**
64    * destination host
65    */
66   uint32_t dest;
67
68   /**
69    * The destination host is reachable thru
70    */
71   uint32_t thru;
72 };
73
74
75 /**
76  * Context information for operations forwarded to subcontrollers
77  */
78 struct ForwardedOperationContext
79 {
80   /**
81    * The next pointer for DLL
82    */
83   struct ForwardedOperationContext *next;
84
85   /**
86    * The prev pointer for DLL
87    */
88   struct ForwardedOperationContext *prev;
89
90   /**
91    * The generated operation context
92    */
93   struct OperationContext *opc;
94
95   /**
96    * The client to which we have to reply
97    */
98   struct GNUNET_SERVER_Client *client;
99
100   /**
101    * Closure pointer
102    */
103   void *cls;
104
105   /**
106    * Task ID for the timeout task
107    */
108   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
109
110   /**
111    * The id of the operation that has been forwarded
112    */
113   uint64_t operation_id;
114
115   /**
116    * The type of the operation which is forwarded
117    */
118   enum OperationType type;
119
120 };
121
122
123 /**
124  * A DLL of host registrations to be made
125  */
126 struct HostRegistration
127 {
128   /**
129    * next registration in the DLL
130    */
131   struct HostRegistration *next;
132
133   /**
134    * previous registration in the DLL
135    */
136   struct HostRegistration *prev;
137
138   /**
139    * The callback to call after this registration's status is available
140    */
141   GNUNET_TESTBED_HostRegistrationCompletion cb;
142
143   /**
144    * The closure for the above callback
145    */
146   void *cb_cls;
147
148   /**
149    * The host that has to be registered
150    */
151   struct GNUNET_TESTBED_Host *host;
152 };
153
154
155 /**
156  * Context information used while linking controllers
157  */
158 struct LinkControllersContext
159 {
160   /**
161    * The client which initiated the link controller operation
162    */
163   struct GNUNET_SERVER_Client *client;
164
165   /**
166    * The ID of the operation
167    */
168   uint64_t operation_id;
169
170 };
171
172
173 /**
174  * Structure representing a connected(directly-linked) controller
175  */
176 struct Slave
177 {
178   /**
179    * The controller process handle if we had started the controller
180    */
181   struct GNUNET_TESTBED_ControllerProc *controller_proc;
182
183   /**
184    * The controller handle
185    */
186   struct GNUNET_TESTBED_Controller *controller;
187
188   /**
189    * The configuration of the slave. Cannot be NULL
190    */
191   struct GNUNET_CONFIGURATION_Handle *cfg;
192
193   /**
194    * handle to lcc which is associated with this slave startup. Should be set to
195    * NULL when the slave has successfully started up
196    */
197   struct LinkControllersContext *lcc;
198
199   /**
200    * Head of the host registration DLL
201    */
202   struct HostRegistration *hr_dll_head;
203
204   /**
205    * Tail of the host registration DLL
206    */
207   struct HostRegistration *hr_dll_tail;
208
209   /**
210    * The current host registration handle
211    */
212   struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
213
214   /**
215    * Hashmap to hold Registered host contexts
216    */
217   struct GNUNET_CONTAINER_MultiHashMap *reghost_map;
218
219   /**
220    * The id of the host this controller is running on
221    */
222   uint32_t host_id;
223
224 };
225
226
227 /**
228  * A peer
229  */
230
231 struct Peer
232 {
233
234   union
235   {
236     struct
237     {
238       /**
239        * The peer handle from testing API
240        */
241       struct GNUNET_TESTING_Peer *peer;
242
243       /**
244        * The modified (by GNUNET_TESTING_peer_configure) configuration this
245        * peer is configured with
246        */
247       struct GNUNET_CONFIGURATION_Handle *cfg;
248
249       /**
250        * Is the peer running
251        */
252       int is_running;
253
254     } local;
255
256     struct
257     {
258       /**
259        * The slave this peer is started through
260        */
261       struct Slave *slave;
262
263       /**
264        * The id of the remote host this peer is running on
265        */
266       uint32_t remote_host_id;
267
268     } remote;
269
270   } details;
271
272   /**
273    * Is this peer locally created?
274    */
275   int is_remote;
276
277   /**
278    * Our local reference id for this peer
279    */
280   uint32_t id;
281
282   /**
283    * References to peers are using in forwarded overlay contexts and remote
284    * overlay connect contexts. A peer can only be destroyed after all such
285    * contexts are destroyed. For this, we maintain a reference counter. When we
286    * use a peer in any such context, we increment this counter. We decrement it
287    * when we are destroying these contexts
288    */
289   uint32_t reference_cnt;
290
291   /**
292    * While destroying a peer, due to the fact that there could be references to
293    * this peer, we delay the peer destroy to a further time. We do this by using
294    * this flag to destroy the peer while destroying a context in which this peer
295    * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
296    * the peer
297    */
298   uint32_t destroy_flag;
299
300 };
301
302
303 /**
304  * The main context information associated with the client which started us
305  */
306 struct Context
307 {
308   /**
309    * The client handle associated with this context
310    */
311   struct GNUNET_SERVER_Client *client;
312
313   /**
314    * The network address of the master controller
315    */
316   char *master_ip;
317
318   /**
319    * The TESTING system handle for starting peers locally
320    */
321   struct GNUNET_TESTING_System *system;
322
323   /**
324    * Our host id according to this context
325    */
326   uint32_t host_id;
327 };
328
329
330 /**
331  * The structure for identifying a shared service
332  */
333 struct SharedService
334 {
335   /**
336    * The name of the shared service
337    */
338   char *name;
339
340   /**
341    * Number of shared peers per instance of the shared service
342    */
343   uint32_t num_shared;
344
345   /**
346    * Number of peers currently sharing the service
347    */
348   uint32_t num_sharing;
349 };
350
351
352 /**
353  * Context information to used during operations which forward the overlay
354  * connect message
355  */
356 struct ForwardedOverlayConnectContext
357 {
358   /**
359    * next ForwardedOverlayConnectContext in the DLL
360    */
361   struct ForwardedOverlayConnectContext *next;
362
363   /**
364    * previous ForwardedOverlayConnectContext in the DLL
365    */
366   struct ForwardedOverlayConnectContext *prev;
367
368   /**
369    * A copy of the original overlay connect message
370    */
371   struct GNUNET_MessageHeader *orig_msg;
372
373   /**
374    * The id of the operation which created this context information
375    */
376   uint64_t operation_id;
377
378   /**
379    * the id of peer 1
380    */
381   uint32_t peer1;
382
383   /**
384    * The id of peer 2
385    */
386   uint32_t peer2;
387
388   /**
389    * Id of the host where peer2 is running
390    */
391   uint32_t peer2_host_id;
392 };
393
394
395 /**
396  * This context information will be created for each host that is registered at
397  * slave controllers during overlay connects.
398  */
399 struct RegisteredHostContext
400 {
401   /**
402    * The host which is being registered
403    */
404   struct GNUNET_TESTBED_Host *reg_host;
405
406   /**
407    * The host of the controller which has to connect to the above rhost
408    */
409   struct GNUNET_TESTBED_Host *host;
410
411   /**
412    * The gateway to which this operation is forwarded to
413    */
414   struct Slave *gateway;
415
416   /**
417    * The gateway through which peer2's controller can be reached
418    */
419   struct Slave *gateway2;
420
421   /**
422    * Handle for sub-operations
423    */
424   struct GNUNET_TESTBED_Operation *sub_op;
425
426   /**
427    * The client which initiated the link controller operation
428    */
429   struct GNUNET_SERVER_Client *client;
430
431   /**
432    * Head of the ForwardedOverlayConnectContext DLL
433    */
434   struct ForwardedOverlayConnectContext *focc_dll_head;
435
436   /**
437    * Tail of the ForwardedOverlayConnectContext DLL
438    */
439   struct ForwardedOverlayConnectContext *focc_dll_tail;
440
441   /**
442    * Enumeration of states for this context
443    */
444   enum RHCState
445   {
446
447     /**
448      * The initial state
449      */
450     RHC_INIT = 0,
451
452     /**
453      * State where we attempt to get peer2's controller configuration
454      */
455     RHC_GET_CFG,
456
457     /**
458      * State where we attempt to link the controller of peer 1 to the controller
459      * of peer2
460      */
461     RHC_LINK,
462
463     /**
464      * State where we attempt to do the overlay connection again
465      */
466     RHC_OL_CONNECT
467   } state;
468
469 };
470
471
472 /**
473  * States of LCFContext
474  */
475 enum LCFContextState
476 {
477   /**
478    * The Context has been initialized; Nothing has been done on it
479    */
480   INIT,
481
482   /**
483    * Delegated host has been registered at the forwarding controller
484    */
485   DELEGATED_HOST_REGISTERED,
486
487   /**
488    * The slave host has been registred at the forwarding controller
489    */
490   SLAVE_HOST_REGISTERED,
491
492   /**
493    * The context has been finished (may have error)
494    */
495   FINISHED
496 };
497
498
499 /**
500  * Link controllers request forwarding context
501  */
502 struct LCFContext
503 {
504   /**
505    * The gateway which will pass the link message to delegated host
506    */
507   struct Slave *gateway;
508
509   /**
510    * The controller link message that has to be forwarded to
511    */
512   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
513
514   /**
515    * The client which has asked to perform this operation
516    */
517   struct GNUNET_SERVER_Client *client;
518
519   /**
520    * Handle for operations which are forwarded while linking controllers
521    */
522   struct ForwardedOperationContext *fopc;
523
524   /**
525    * The id of the operation which created this context
526    */
527   uint64_t operation_id;
528
529   /**
530    * The state of this context
531    */
532   enum LCFContextState state;
533
534   /**
535    * The delegated host
536    */
537   uint32_t delegated_host_id;
538
539   /**
540    * The slave host
541    */
542   uint32_t slave_host_id;
543
544 };
545
546
547 /**
548  * Structure of a queue entry in LCFContext request queue
549  */
550 struct LCFContextQueue
551 {
552   /**
553    * The LCFContext
554    */
555   struct LCFContext *lcf;
556
557   /**
558    * Head prt for DLL
559    */
560   struct LCFContextQueue *next;
561
562   /**
563    * Tail ptr for DLL
564    */
565   struct LCFContextQueue *prev;
566 };
567
568 /**
569  * Our configuration
570  */
571 struct GNUNET_CONFIGURATION_Handle *our_config;
572
573 /**
574  * The master context; generated with the first INIT message
575  */
576 extern struct Context *GST_context;
577
578 /**
579  * DLL head for forwarded operation contexts
580  */
581 extern struct ForwardedOperationContext *fopcq_head;
582
583 /**
584  * DLL tail for forwarded operation contexts
585  */
586 extern struct ForwardedOperationContext *fopcq_tail;
587
588 /**
589  * A list of peers we know about
590  */
591 extern struct Peer **GST_peer_list;
592
593 /**
594  * Array of hosts
595  */
596 extern struct GNUNET_TESTBED_Host **GST_host_list;
597
598 /**
599  * A list of directly linked neighbours
600  */
601 extern struct Slave **GST_slave_list;
602
603 /**
604  * Operation queue for open file descriptors
605  */
606 extern struct OperationQueue *GST_opq_openfds;
607
608 /**
609  * Timeout for operations which may take some time
610  */
611 const extern struct GNUNET_TIME_Relative GST_timeout;
612
613 /**
614  * The size of the peer list
615  */
616 extern unsigned int GST_peer_list_size;
617
618 /**
619  * The size of the host list
620  */
621 extern unsigned int GST_host_list_size;
622
623 /**
624  * The size of directly linked neighbours list
625  */
626 extern unsigned int GST_slave_list_size;
627
628
629 /**
630  * Queues a message in send queue for sending to the service
631  *
632  * @param client the client to whom the queued message has to be sent
633  * @param msg the message to queue
634  */
635 void
636 GST_queue_message (struct GNUNET_SERVER_Client *client,
637                    struct GNUNET_MessageHeader *msg);
638
639
640 /**
641  * Function to destroy a peer
642  *
643  * @param peer the peer structure to destroy
644  */
645 void
646 GST_destroy_peer (struct Peer *peer);
647
648
649 /**
650  * Finds the route with directly connected host as destination through which
651  * the destination host can be reached
652  *
653  * @param host_id the id of the destination host
654  * @return the route with directly connected destination host; NULL if no route
655  *           is found
656  */
657 struct Route *
658 GST_find_dest_route (uint32_t host_id);
659
660
661 /**
662  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
663  *
664  * @param cls NULL
665  * @param client identification of the client
666  * @param message the actual message
667  */
668 void
669 GST_handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
670                             const struct GNUNET_MessageHeader *message);
671
672
673 /**
674  * Adds a host registration's request to a slave's registration queue
675  *
676  * @param slave the slave controller at which the given host has to be
677  *          registered
678  * @param cb the host registration completion callback
679  * @param cb_cls the closure for the host registration completion callback
680  * @param host the host which has to be registered
681  */
682 void
683 GST_queue_host_registration (struct Slave *slave,
684                              GNUNET_TESTBED_HostRegistrationCompletion cb,
685                              void *cb_cls, struct GNUNET_TESTBED_Host *host);
686
687
688 /**
689  * Callback to relay the reply msg of a forwarded operation back to the client
690  *
691  * @param cls ForwardedOperationContext
692  * @param msg the message to relay
693  */
694 void
695 GST_forwarded_operation_reply_relay (void *cls,
696                                      const struct GNUNET_MessageHeader *msg);
697
698
699 /**
700  * Task to free resources when forwarded operation has been timedout
701  *
702  * @param cls the ForwardedOperationContext
703  * @param tc the task context from scheduler
704  */
705 void
706 GST_forwarded_operation_timeout (void *cls,
707                                  const struct GNUNET_SCHEDULER_TaskContext *tc);
708
709
710 /**
711  * Send operation failure message to client
712  *
713  * @param client the client to which the failure message has to be sent to
714  * @param operation_id the id of the failed operation
715  * @param emsg the error message; can be NULL
716  */
717 void
718 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
719                              uint64_t operation_id, const char *emsg);
720
721
722 /**
723  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
724  *
725  * @param cls NULL
726  * @param client identification of the client
727  * @param message the actual message
728  */
729 void
730 GST_handle_remote_overlay_connect (void *cls,
731                                    struct GNUNET_SERVER_Client *client,
732                                    const struct GNUNET_MessageHeader *message);
733
734
735 /**
736  * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
737  *
738  * @param rhc the RegisteredHostContext
739  */
740 void
741 GST_process_next_focc (struct RegisteredHostContext *rhc);
742
743
744 /**
745  * Cleans up ForwardedOverlayConnectContext
746  *
747  * @param focc the ForwardedOverlayConnectContext to cleanup
748  */
749 void
750 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
751
752
753 /**
754  * Clears all pending overlay connect contexts in queue
755  */
756 void
757 GST_free_occq ();
758
759
760 /**
761  * Clears all pending remote overlay connect contexts in queue
762  */
763 void
764 GST_free_roccq ();
765
766
767 /**
768  * Initializes the cache
769  *
770  * @param size the size of the cache
771  */
772 void
773 GST_cache_init (unsigned int size);
774
775
776 /**
777  * Clear cache
778  */
779 void
780 GST_cache_clear ();
781
782
783 /**
784  * Looks up in the hello cache and returns the HELLO of the given peer
785  *
786  * @param peer_id the index of the peer whose HELLO has to be looked up
787  * @return the HELLO message; NULL if not found
788  */
789 const struct GNUNET_MessageHeader *
790 GST_cache_lookup_hello (const unsigned int peer_id);
791
792
793 /**
794  * Caches the HELLO of the given peer. Updates the HELLO if it was already
795  * cached before
796  *
797  * @param id the peer identity of the peer whose HELLO has to be cached
798  * @param hello the HELLO message
799  */
800 void
801 GST_cache_add_hello (const unsigned int peer_id,
802                      const struct GNUNET_MessageHeader *hello);
803
804
805 /**
806  * Functions of this type are called when the needed handle is available for
807  * usage. These functions are to be registered with either of the functions
808  * GST_cache_get_handle_transport() or GST_cache_get_handle_core(). The
809  * corresponding handles will be set and if they are not, then it signals an
810  * error while opening the handles.
811  *
812  * @param cls the closure passed to GST_cache_get_handle_transport() or
813  *          GST_cache_get_handle_core()
814  * @param ch the handle to CORE. Can be NULL if it is not requested
815  * @param th the handle to TRANSPORT. Can be NULL if it is not requested
816  * @param peer_id the identity of the peer. Will be NULL if ch is NULL. In other
817  *          cases, its value being NULL means that CORE connection has failed.
818  */
819 typedef void (*GST_cache_handle_ready_cb) (void *cls,
820                                            struct GNUNET_CORE_Handle * ch,
821                                            struct GNUNET_TRANSPORT_Handle * th,
822                                            const struct GNUNET_PeerIdentity *
823                                            peer_id);
824
825
826 /**
827  * Callback to notify when the target peer given to
828  * GST_cache_get_handle_transport() is connected. Note that this callback may
829  * not be called if the target peer is already connected. Use
830  * GNUNET_TRANSPORT_check_neighbour_connected() to check if the target peer is
831  * already connected or not. This callback will be called only once or never (in
832  * case the target cannot be connected).
833  *
834  * @param cls the closure given to GST_cache_get_handle_done() for this callback
835  * @param target the peer identity of the target peer. The pointer should be
836  *          valid until GST_cache_get_handle_done() is called.
837  */
838 typedef void (*GST_cache_peer_connect_notify) (void *cls,
839                                                const struct GNUNET_PeerIdentity
840                                                * target);
841
842
843 /**
844  * Get a transport handle with the given configuration. If the handle is already
845  * cached before, it will be retured in the given callback; the peer_id is used to lookup in the
846  * cache. If not a new operation is started to open the transport handle and
847  * will be given in the callback when it is available.
848  *
849  * @param peer_id the index of the peer
850  * @param cfg the configuration with which the transport handle has to be
851  *          created if it was not present in the cache
852  * @param cb the callback to notify when the transport handle is available
853  * @param cb_cls the closure for the above callback
854  * @param target the peer identify of the peer whose connection to our TRANSPORT
855  *          subsystem will be notified through the connect_notify_cb. Can be NULL
856  * @param connect_notify_cb the callback to call when the given target peer is
857  *          connected. This callback will only be called once or never again (in
858  *          case the target peer cannot be connected). Can be NULL
859  * @param connect_notify_cb_cls the closure for the above callback
860  * @return the handle which can be used cancel or mark that the handle is no
861  *           longer being used
862  */
863 struct GSTCacheGetHandle *
864 GST_cache_get_handle_transport (unsigned int peer_id,
865                                 const struct GNUNET_CONFIGURATION_Handle *cfg,
866                                 GST_cache_handle_ready_cb cb, void *cb_cls,
867                                 const struct GNUNET_PeerIdentity *target,
868                                 GST_cache_peer_connect_notify connect_notify_cb,
869                                 void *connect_notify_cb_cls);
870
871
872 /**
873  * Get a CORE handle with the given configuration. If the handle is already
874  * cached before, it will be retured in the given callback; the peer_id is used
875  * to lookup in the cache. If the handle is not cached before, a new operation
876  * is started to open the CORE handle and will be given in the callback when it
877  * is available along with the peer identity
878  *
879  * @param peer_id the index of the peer
880  * @param cfg the configuration with which the transport handle has to be
881  *          created if it was not present in the cache
882  * @param cb the callback to notify when the transport handle is available
883  * @param cb_cls the closure for the above callback
884  * @param target the peer identify of the peer whose connection to our CORE
885  *          subsystem will be notified through the connect_notify_cb. Can be NULL
886  * @param connect_notify_cb the callback to call when the given target peer is
887  *          connected. This callback will only be called once or never again (in
888  *          case the target peer cannot be connected). Can be NULL
889  * @param connect_notify_cb_cls the closure for the above callback
890  * @return the handle which can be used cancel or mark that the handle is no
891  *           longer being used
892  */
893 struct GSTCacheGetHandle *
894 GST_cache_get_handle_core (unsigned int peer_id,
895                            const struct GNUNET_CONFIGURATION_Handle *cfg,
896                            GST_cache_handle_ready_cb cb, void *cb_cls,
897                            const struct GNUNET_PeerIdentity *target,
898                            GST_cache_peer_connect_notify connect_notify_cb,
899                            void *connect_notify_cb_cls);
900
901
902 /**
903  * Mark the GetCacheHandle as being done if a handle has been provided already
904  * or as being cancelled if the callback for the handle hasn't been called.
905  *
906  * @param cgh the CacheGetHandle handle
907  */
908 void
909 GST_cache_get_handle_done (struct GSTCacheGetHandle *cgh);
910
911 /* End of gnunet-service-testbed.h */