- minor changes, TODO, debug message
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.h
1 /*
2   This file is part of GNUnet.
3   Copyright (C) 2008--2013 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18   Boston, MA 02110-1301, 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 #include "gnunet-service-testbed_links.h"
39
40
41 /**
42  * Generic logging
43  */
44 #define LOG(kind,...)                           \
45   GNUNET_log (kind, __VA_ARGS__)
46
47 /**
48  * Debug logging
49  */
50 #define LOG_DEBUG(...)                          \
51   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52
53 /**
54  * By how much should the arrays lists grow
55  */
56 #define LIST_GROW_STEP 10
57
58
59 /**
60  * A routing entry
61  */
62 struct Route
63 {
64   /**
65    * destination host
66    */
67   uint32_t dest;
68
69   /**
70    * The destination host is reachable thru
71    */
72   uint32_t thru;
73 };
74
75
76 /**
77  * Context information for operations forwarded to subcontrollers
78  */
79 struct ForwardedOperationContext
80 {
81   /**
82    * The next pointer for DLL
83    */
84   struct ForwardedOperationContext *next;
85
86   /**
87    * The prev pointer for DLL
88    */
89   struct ForwardedOperationContext *prev;
90
91   /**
92    * The generated operation context
93    */
94   struct OperationContext *opc;
95
96   /**
97    * The client to which we have to reply
98    */
99   struct GNUNET_SERVICE_Client *client;
100
101   /**
102    * Closure pointer
103    */
104   void *cls;
105
106   /**
107    * Task ID for the timeout task
108    */
109   struct GNUNET_SCHEDULER_Task * timeout_task;
110
111   /**
112    * The id of the operation that has been forwarded
113    */
114   uint64_t operation_id;
115
116   /**
117    * The type of the operation which is forwarded
118    */
119   enum OperationType type;
120
121 };
122
123
124 /**
125  * A DLL of host registrations to be made
126  */
127 struct HostRegistration
128 {
129   /**
130    * next registration in the DLL
131    */
132   struct HostRegistration *next;
133
134   /**
135    * previous registration in the DLL
136    */
137   struct HostRegistration *prev;
138
139   /**
140    * The callback to call after this registration's status is available
141    */
142   GNUNET_TESTBED_HostRegistrationCompletion cb;
143
144   /**
145    * The closure for the above callback
146    */
147   void *cb_cls;
148
149   /**
150    * The host that has to be registered
151    */
152   struct GNUNET_TESTBED_Host *host;
153 };
154
155
156 /**
157  * Context information used while linking controllers
158  */
159 struct LinkControllersContext
160 {
161   /**
162    * The client which initiated the link controller operation
163    */
164   struct GNUNET_SERVICE_Client *client;
165
166   /**
167    * The ID of the operation
168    */
169   uint64_t operation_id;
170
171 };
172
173
174 /**
175  * A peer
176  */
177 struct Peer
178 {
179
180   union
181   {
182     struct
183     {
184       /**
185        * The peer handle from testing API
186        */
187       struct GNUNET_TESTING_Peer *peer;
188
189       /**
190        * The modified (by GNUNET_TESTING_peer_configure) configuration this
191        * peer is configured with
192        */
193       struct GNUNET_CONFIGURATION_Handle *cfg;
194
195       /**
196        * Is the peer running
197        */
198       int is_running;
199
200     } local;
201
202     struct
203     {
204       /**
205        * The slave this peer is started through
206        */
207       struct Slave *slave;
208
209       /**
210        * The id of the remote host this peer is running on
211        */
212       uint32_t remote_host_id;
213
214     } remote;
215
216   } details;
217
218   /**
219    * Is this peer locally created?
220    */
221   int is_remote;
222
223   /**
224    * Our local reference id for this peer
225    */
226   uint32_t id;
227
228   /**
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
234    */
235   uint32_t reference_cnt;
236
237   /**
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
242    * the peer
243    */
244   uint32_t destroy_flag;
245
246 };
247
248
249 /**
250  * The main context information associated with the client which started us
251  */
252 struct Context
253 {
254   /**
255    * The client handle associated with this context
256    */
257   struct GNUNET_SERVICE_Client *client;
258
259   /**
260    * The network address of the master controller
261    */
262   char *master_ip;
263
264   /**
265    * The TESTING system handle for starting peers locally
266    */
267   struct GNUNET_TESTING_System *system;
268
269   /**
270    * Our host id according to this context
271    */
272   uint32_t host_id;
273 };
274
275
276 /**
277  * The structure for identifying a shared service
278  */
279 struct SharedService
280 {
281   /**
282    * The name of the shared service
283    */
284   char *name;
285
286   /**
287    * Number of shared peers per instance of the shared service
288    */
289   uint32_t num_shared;
290
291   /**
292    * Number of peers currently sharing the service
293    */
294   uint32_t num_sharing;
295 };
296
297
298 struct RegisteredHostContext;
299
300
301 /**
302  * Context information to used during operations which forward the overlay
303  * connect message
304  */
305 struct ForwardedOverlayConnectContext
306 {
307   /**
308    * next ForwardedOverlayConnectContext in the DLL
309    */
310   struct ForwardedOverlayConnectContext *next;
311
312   /**
313    * previous ForwardedOverlayConnectContext in the DLL
314    */
315   struct ForwardedOverlayConnectContext *prev;
316
317   /**
318    * Which host does this FOCC belong to?
319    */
320   struct RegisteredHostContext *rhc;
321
322   /**
323    * A copy of the original overlay connect message
324    */
325   struct GNUNET_MessageHeader *orig_msg;
326
327   /**
328    * The client handle
329    */
330   struct GNUNET_SERVICE_Client *client;
331
332   /**
333    * The id of the operation which created this context information
334    */
335   uint64_t operation_id;
336
337   /**
338    * the id of peer 1
339    */
340   uint32_t peer1;
341
342   /**
343    * The id of peer 2
344    */
345   uint32_t peer2;
346
347   /**
348    * Id of the host where peer2 is running
349    */
350   uint32_t peer2_host_id;
351 };
352
353
354 /**
355  * This context information will be created for each host that is registered at
356  * slave controllers during overlay connects.
357  */
358 struct RegisteredHostContext
359 {
360   /**
361    * The host which is being registered
362    */
363   struct GNUNET_TESTBED_Host *reg_host;
364
365   /**
366    * The host of the controller which has to connect to the above rhost
367    */
368   struct GNUNET_TESTBED_Host *host;
369
370   /**
371    * Head of the ForwardedOverlayConnectContext DLL
372    */
373   struct ForwardedOverlayConnectContext *focc_dll_head;
374
375   /**
376    * Tail of the ForwardedOverlayConnectContext DLL
377    */
378   struct ForwardedOverlayConnectContext *focc_dll_tail;
379
380   /**
381    * Enumeration of states for this context
382    */
383   enum RHCState
384   {
385
386     /**
387      * The initial state
388      */
389     RHC_INIT = 0,
390
391     /**
392      * State where we attempt to do the overlay connection again
393      */
394     RHC_DONE
395   } state;
396
397 };
398
399
400 /**
401  * Context data for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
402  */
403 struct HandlerContext_ShutdownPeers
404 {
405   /**
406    * The number of slave we expect to hear from since we forwarded the
407    * #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
408    */
409   unsigned int nslaves;
410
411   /**
412    * Did we observe a timeout with respect to this operation at any of the
413    * slaves
414    */
415   int timeout;
416 };
417
418
419 /**
420  * Our configuration
421  */
422 extern struct GNUNET_CONFIGURATION_Handle *GST_config;
423
424 /**
425  * The master context; generated with the first INIT message
426  */
427 extern struct Context *GST_context;
428
429 /**
430  * DLL head for forwarded operation contexts
431  */
432 extern struct ForwardedOperationContext *fopcq_head;
433
434 /**
435  * DLL tail for forwarded operation contexts
436  */
437 extern struct ForwardedOperationContext *fopcq_tail;
438
439 /**
440  * A list of peers we know about
441  */
442 extern struct Peer **GST_peer_list;
443
444 /**
445  * Array of hosts
446  */
447 extern struct GNUNET_TESTBED_Host **GST_host_list;
448
449 /**
450  * Operation queue for open file descriptors
451  */
452 extern struct OperationQueue *GST_opq_openfds;
453
454 /**
455  * Timeout for operations which may take some time
456  */
457 const extern struct GNUNET_TIME_Relative GST_timeout;
458
459 /**
460  * The size of the peer list
461  */
462 extern unsigned int GST_peer_list_size;
463
464 /**
465  * The current number of peers running locally under this controller
466  */
467 extern unsigned int GST_num_local_peers;
468
469 /**
470  * The size of the host list
471  */
472 extern unsigned int GST_host_list_size;
473
474 /**
475  * The directory where to store load statistics data
476  */
477 extern char *GST_stats_dir;
478
479 /**
480  * Condition to check if host id is valid
481  */
482 #define VALID_HOST_ID(id) \
483   ( ((id) < GST_host_list_size) && (NULL != GST_host_list[id]) )
484
485 /**
486  * Condition to check if peer id is valid
487  */
488 #define VALID_PEER_ID(id) \
489   ( ((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]) )
490
491
492 /**
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
495  * of LIST_GROW_STEP.
496  *
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
501  *          accommodate_size
502  */
503 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
504   do                                                                    \
505   {                                                                     \
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);                            \
513   } while (0)
514
515
516 /**
517  * Function to destroy a peer
518  *
519  * @param peer the peer structure to destroy
520  */
521 void
522 GST_destroy_peer (struct Peer *peer);
523
524
525 /**
526  * Stops and destroys all peers
527  */
528 void
529 GST_destroy_peers (void);
530
531
532 /**
533  * Finds the route with directly connected host as destination through which
534  * the destination host can be reached
535  *
536  * @param host_id the id of the destination host
537  * @return the route with directly connected destination host; NULL if no route
538  *           is found
539  */
540 struct Route *
541 GST_find_dest_route (uint32_t host_id);
542
543
544 /**
545  * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT messages
546  *
547  * @param cls identification of the client
548  * @param msg the actual message
549  */
550 void
551 handle_overlay_connect (void *cls,
552                         const struct GNUNET_TESTBED_OverlayConnectMessage *msg);
553
554
555 /**
556  * Adds a host registration's request to a slave's registration queue
557  *
558  * @param slave the slave controller at which the given host has to be
559  *          registered
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
563  */
564 void
565 GST_queue_host_registration (struct Slave *slave,
566                              GNUNET_TESTBED_HostRegistrationCompletion cb,
567                              void *cb_cls, struct GNUNET_TESTBED_Host *host);
568
569
570 /**
571  * Callback to relay the reply msg of a forwarded operation back to the client
572  *
573  * @param cls ForwardedOperationContext
574  * @param msg the message to relay
575  */
576 void
577 GST_forwarded_operation_reply_relay (void *cls,
578                                      const struct GNUNET_MessageHeader *msg);
579
580
581 /**
582  * Task to free resources when forwarded operation has been timedout
583  *
584  * @param cls the ForwardedOperationContext
585  * @param tc the task context from scheduler
586  */
587 void
588 GST_forwarded_operation_timeout (void *cls);
589
590
591 /**
592  * Clears the forwarded operations queue
593  */
594 void
595 GST_clear_fopcq (void);
596
597
598 /**
599  * Send operation failure message to client
600  *
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
604  */
605 void
606 GST_send_operation_fail_msg (struct GNUNET_SERVICE_Client *client,
607                              uint64_t operation_id,
608                              const char *emsg);
609
610
611 /**
612  * Notify OC subsystem that @a client disconnected.
613  *
614  * @param client the client that disconnected
615  */
616 void
617 GST_notify_client_disconnect_oc (struct GNUNET_SERVICE_Client *client);
618
619
620 /**
621  * Notify peers subsystem that @a client disconnected.
622  *
623  * @param client the client that disconnected
624  */
625 void
626 GST_notify_client_disconnect_peers (struct GNUNET_SERVICE_Client *client);
627
628
629 /**
630  * Function to send generic operation success message to given client
631  *
632  * @param client the client to send the message to
633  * @param operation_id the id of the operation which was successful
634  */
635 void
636 GST_send_operation_success_msg (struct GNUNET_SERVICE_Client *client,
637                                 uint64_t operation_id);
638
639
640 /**
641  * Check #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
642  *
643  * @param cls identification of the client
644  * @param msg the actual message
645  * @return #GNUNET_OK if @a msg is well-formed
646  */
647 int
648 check_remote_overlay_connect (void *cls,
649                               const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
650
651
652 /**
653  * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT messages
654  *
655  * @param cls identification of the client
656  * @param msg the actual message
657  */
658 void
659 handle_remote_overlay_connect (void *cls,
660                                const struct GNUNET_TESTBED_RemoteOverlayConnectMessage *msg);
661
662
663 /**
664  * Check #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
665  *
666  * @param cls identification of the client
667  * @param msg the actual message
668  * @return #GNUNET_OK if @a msg is well-formed
669  */
670 int
671 check_peer_create (void *cls,
672                    const struct GNUNET_TESTBED_PeerCreateMessage *msg);
673
674
675 /**
676  * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
677  *
678  * @param cls identification of the client
679  * @param message the actual message
680  */
681 void
682 handle_peer_create (void *cls,
683                     const struct GNUNET_TESTBED_PeerCreateMessage *msg);
684
685
686 /**
687  * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
688  *
689  * @param cls identification of the client
690  * @param msg the actual message
691  */
692 void
693 handle_peer_destroy (void *cls,
694                      const struct GNUNET_TESTBED_PeerDestroyMessage *msg);
695
696
697 /**
698  * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
699  *
700  * @param cls identification of the client
701  * @param msg the actual message
702  */
703 void
704 handle_peer_start (void *cls,
705                    const struct GNUNET_TESTBED_PeerStartMessage *msg);
706
707
708 /**
709  * Message handler for #GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
710  *
711  * @param cls identification of the client
712  * @param message the actual message
713  */
714 void
715 handle_peer_stop (void *cls,
716                   const struct GNUNET_TESTBED_PeerStopMessage *msg);
717
718
719 /**
720  * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
721  *
722  * @param cls identification of the client
723  * @param msg the actual message
724  */
725 void
726 handle_peer_get_config (void *cls,
727                         const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg);
728
729
730 /**
731  * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
732  *
733  * @param cls identification of the client
734  * @param msg the actual message
735  */
736 void
737 handle_shutdown_peers (void *cls,
738                        const struct GNUNET_TESTBED_ShutdownPeersMessage *msg);
739
740
741 /**
742  * Check #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
743  *
744  * @param cls identification of client
745  * @param msg the actual message
746  * @return #GNUNET_OK if @a msg is well-formed
747  */
748 int
749 check_manage_peer_service (void *cls,
750                            const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
751
752
753 /**
754  * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE message
755  *
756  * @param cls identification of client
757  * @param msg the actual message
758  */
759 void
760 handle_manage_peer_service (void *cls,
761                             const struct GNUNET_TESTBED_ManagePeerServiceMessage *msg);
762
763
764
765
766 /**
767  * Check #GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
768  *
769  * @param cls identification of the client
770  * @param msg the actual message
771  * @return #GNUNET_OK if @a msg is well-formed
772  */
773 int
774 check_peer_reconfigure (void *cls,
775                         const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
776
777
778 /**
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
781  * new configuration.
782  *
783  * @param cls identification of the client
784  * @param msg the actual message
785  */
786 void
787 handle_peer_reconfigure (void *cls,
788                          const struct GNUNET_TESTBED_PeerReconfigureMessage *msg);
789
790
791 /**
792  * Frees the ManageServiceContext queue
793  */
794 void
795 GST_free_mctxq (void);
796
797
798 /**
799  * Cleans up the queue used for forwarding link controllers requests
800  */
801 void
802 GST_free_lcf (void);
803
804
805 /**
806  * Cleans up the route list
807  */
808 void
809 GST_route_list_clear (void);
810
811
812 /**
813  * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
814  *
815  * @param rhc the RegisteredHostContext
816  */
817 void
818 GST_process_next_focc (struct RegisteredHostContext *rhc);
819
820
821 /**
822  * Cleans up ForwardedOverlayConnectContext
823  *
824  * @param focc the ForwardedOverlayConnectContext to cleanup
825  */
826 void
827 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
828
829
830 /**
831  * Clears all pending overlay connect contexts in queue
832  */
833 void
834 GST_free_occq (void);
835
836
837 /**
838  * Clears all pending remote overlay connect contexts in queue
839  */
840 void
841 GST_free_roccq (void);
842
843
844 /**
845  * Cleans up the Peer reconfigure context list
846  */
847 void
848 GST_free_prcq (void);
849
850
851 /**
852  * Initializes the cache
853  *
854  * @param size the size of the cache
855  */
856 void
857 GST_cache_init (unsigned int size);
858
859
860 /**
861  * Clear cache
862  */
863 void
864 GST_cache_clear (void);
865
866
867 /**
868  * Looks up in the hello cache and returns the HELLO of the given peer
869  *
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
872  */
873 const struct GNUNET_MessageHeader *
874 GST_cache_lookup_hello (const unsigned int peer_id);
875
876
877 /**
878  * Caches the HELLO of the given peer. Updates the HELLO if it was already
879  * cached before
880  *
881  * @param peer_id the peer identity of the peer whose HELLO has to be cached
882  * @param hello the HELLO message
883  */
884 void
885 GST_cache_add_hello (const unsigned int peer_id,
886                      const struct GNUNET_MessageHeader *hello);
887
888
889 /**
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.
893  */
894 void
895 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
896
897
898 /**
899  * Shutdown the status calls module.
900  */
901 void
902 GST_stats_destroy (void);
903
904 /* End of gnunet-service-testbed.h */