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