plugin datastore mysql
[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_SERVER_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_SERVER_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
178 struct Peer
179 {
180
181   union
182   {
183     struct
184     {
185       /**
186        * The peer handle from testing API
187        */
188       struct GNUNET_TESTING_Peer *peer;
189
190       /**
191        * The modified (by GNUNET_TESTING_peer_configure) configuration this
192        * peer is configured with
193        */
194       struct GNUNET_CONFIGURATION_Handle *cfg;
195
196       /**
197        * Is the peer running
198        */
199       int is_running;
200
201     } local;
202
203     struct
204     {
205       /**
206        * The slave this peer is started through
207        */
208       struct Slave *slave;
209
210       /**
211        * The id of the remote host this peer is running on
212        */
213       uint32_t remote_host_id;
214
215     } remote;
216
217   } details;
218
219   /**
220    * Is this peer locally created?
221    */
222   int is_remote;
223
224   /**
225    * Our local reference id for this peer
226    */
227   uint32_t id;
228
229   /**
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
235    */
236   uint32_t reference_cnt;
237
238   /**
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
243    * the peer
244    */
245   uint32_t destroy_flag;
246
247 };
248
249
250 /**
251  * The main context information associated with the client which started us
252  */
253 struct Context
254 {
255   /**
256    * The client handle associated with this context
257    */
258   struct GNUNET_SERVER_Client *client;
259
260   /**
261    * The network address of the master controller
262    */
263   char *master_ip;
264
265   /**
266    * The TESTING system handle for starting peers locally
267    */
268   struct GNUNET_TESTING_System *system;
269
270   /**
271    * Our host id according to this context
272    */
273   uint32_t host_id;
274 };
275
276
277 /**
278  * The structure for identifying a shared service
279  */
280 struct SharedService
281 {
282   /**
283    * The name of the shared service
284    */
285   char *name;
286
287   /**
288    * Number of shared peers per instance of the shared service
289    */
290   uint32_t num_shared;
291
292   /**
293    * Number of peers currently sharing the service
294    */
295   uint32_t num_sharing;
296 };
297
298
299 /**
300  * Context information to used during operations which forward the overlay
301  * connect message
302  */
303 struct ForwardedOverlayConnectContext
304 {
305   /**
306    * next ForwardedOverlayConnectContext in the DLL
307    */
308   struct ForwardedOverlayConnectContext *next;
309
310   /**
311    * previous ForwardedOverlayConnectContext in the DLL
312    */
313   struct ForwardedOverlayConnectContext *prev;
314
315   /**
316    * A copy of the original overlay connect message
317    */
318   struct GNUNET_MessageHeader *orig_msg;
319
320   /**
321    * The client handle
322    */
323   struct GNUNET_SERVER_Client *client;
324
325   /**
326    * The id of the operation which created this context information
327    */
328   uint64_t operation_id;
329
330   /**
331    * the id of peer 1
332    */
333   uint32_t peer1;
334
335   /**
336    * The id of peer 2
337    */
338   uint32_t peer2;
339
340   /**
341    * Id of the host where peer2 is running
342    */
343   uint32_t peer2_host_id;
344 };
345
346
347 /**
348  * This context information will be created for each host that is registered at
349  * slave controllers during overlay connects.
350  */
351 struct RegisteredHostContext
352 {
353   /**
354    * The host which is being registered
355    */
356   struct GNUNET_TESTBED_Host *reg_host;
357
358   /**
359    * The host of the controller which has to connect to the above rhost
360    */
361   struct GNUNET_TESTBED_Host *host;
362
363   /**
364    * Head of the ForwardedOverlayConnectContext DLL
365    */
366   struct ForwardedOverlayConnectContext *focc_dll_head;
367
368   /**
369    * Tail of the ForwardedOverlayConnectContext DLL
370    */
371   struct ForwardedOverlayConnectContext *focc_dll_tail;
372
373   /**
374    * Enumeration of states for this context
375    */
376   enum RHCState
377   {
378
379     /**
380      * The initial state
381      */
382     RHC_INIT = 0,
383
384     /**
385      * State where we attempt to do the overlay connection again
386      */
387     RHC_DONE
388   } state;
389
390 };
391
392
393 /**
394  * Context data for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS handler
395  */
396 struct HandlerContext_ShutdownPeers
397 {
398   /**
399    * The number of slave we expect to hear from since we forwarded the
400    * GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS message to them
401    */
402   unsigned int nslaves;
403
404   /**
405    * Did we observe a timeout with respect to this operation at any of the
406    * slaves
407    */
408   int timeout;
409 };
410
411
412 /**
413  * Our configuration
414  */
415 extern struct GNUNET_CONFIGURATION_Handle *GST_config;
416
417 /**
418  * The master context; generated with the first INIT message
419  */
420 extern struct Context *GST_context;
421
422 /**
423  * DLL head for forwarded operation contexts
424  */
425 extern struct ForwardedOperationContext *fopcq_head;
426
427 /**
428  * DLL tail for forwarded operation contexts
429  */
430 extern struct ForwardedOperationContext *fopcq_tail;
431
432 /**
433  * A list of peers we know about
434  */
435 extern struct Peer **GST_peer_list;
436
437 /**
438  * Array of hosts
439  */
440 extern struct GNUNET_TESTBED_Host **GST_host_list;
441
442 /**
443  * Operation queue for open file descriptors
444  */
445 extern struct OperationQueue *GST_opq_openfds;
446
447 /**
448  * Timeout for operations which may take some time
449  */
450 const extern struct GNUNET_TIME_Relative GST_timeout;
451
452 /**
453  * The size of the peer list
454  */
455 extern unsigned int GST_peer_list_size;
456
457 /**
458  * The current number of peers running locally under this controller
459  */
460 extern unsigned int GST_num_local_peers;
461
462 /**
463  * The size of the host list
464  */
465 extern unsigned int GST_host_list_size;
466
467 /**
468  * The directory where to store load statistics data
469  */
470 extern char *GST_stats_dir;
471
472 /**
473  * Condition to check if host id is valid
474  */
475 #define VALID_HOST_ID(id) \
476   ( ((id) < GST_host_list_size) && (NULL != GST_host_list[id]) )
477
478 /**
479  * Condition to check if peer id is valid
480  */
481 #define VALID_PEER_ID(id) \
482   ( ((id) < GST_peer_list_size) && (NULL != GST_peer_list[id]) )
483
484
485 /**
486  * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
487  * several times we call it only once. The array is also made to grow in steps
488  * of LIST_GROW_STEP.
489  *
490  * @param ptr the array pointer to grow
491  * @param size the size of array
492  * @param accommodate_size the size which the array has to accommdate; after
493  *          this call the array will be big enough to accommdate sizes upto
494  *          accommodate_size
495  */
496 #define GST_array_grow_large_enough(ptr, size, accommodate_size) \
497   do                                                                    \
498   {                                                                     \
499     unsigned int growth_size;                                           \
500     GNUNET_assert (size <= accommodate_size);                            \
501     growth_size = size;                                                 \
502     while (growth_size <= accommodate_size)                             \
503       growth_size += LIST_GROW_STEP;                                    \
504     GNUNET_array_grow (ptr, size, growth_size);                         \
505     GNUNET_assert (size > accommodate_size);                            \
506   } while (0)
507
508
509 /**
510  * Queues a message in send queue for sending to the service
511  *
512  * @param client the client to whom the queued message has to be sent
513  * @param msg the message to queue
514  */
515 void
516 GST_queue_message (struct GNUNET_SERVER_Client *client,
517                    struct GNUNET_MessageHeader *msg);
518
519
520 /**
521  * Function to destroy a peer
522  *
523  * @param peer the peer structure to destroy
524  */
525 void
526 GST_destroy_peer (struct Peer *peer);
527
528
529 /**
530  * Stops and destroys all peers
531  */
532 void
533 GST_destroy_peers ();
534
535
536 /**
537  * Finds the route with directly connected host as destination through which
538  * the destination host can be reached
539  *
540  * @param host_id the id of the destination host
541  * @return the route with directly connected destination host; NULL if no route
542  *           is found
543  */
544 struct Route *
545 GST_find_dest_route (uint32_t host_id);
546
547
548 /**
549  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
550  *
551  * @param cls NULL
552  * @param client identification of the client
553  * @param message the actual message
554  */
555 void
556 GST_handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
557                             const struct GNUNET_MessageHeader *message);
558
559
560 /**
561  * Adds a host registration's request to a slave's registration queue
562  *
563  * @param slave the slave controller at which the given host has to be
564  *          registered
565  * @param cb the host registration completion callback
566  * @param cb_cls the closure for the host registration completion callback
567  * @param host the host which has to be registered
568  */
569 void
570 GST_queue_host_registration (struct Slave *slave,
571                              GNUNET_TESTBED_HostRegistrationCompletion cb,
572                              void *cb_cls, struct GNUNET_TESTBED_Host *host);
573
574
575 /**
576  * Callback to relay the reply msg of a forwarded operation back to the client
577  *
578  * @param cls ForwardedOperationContext
579  * @param msg the message to relay
580  */
581 void
582 GST_forwarded_operation_reply_relay (void *cls,
583                                      const struct GNUNET_MessageHeader *msg);
584
585
586 /**
587  * Task to free resources when forwarded operation has been timedout
588  *
589  * @param cls the ForwardedOperationContext
590  * @param tc the task context from scheduler
591  */
592 void
593 GST_forwarded_operation_timeout (void *cls);
594
595
596 /**
597  * Clears the forwarded operations queue
598  */
599 void
600 GST_clear_fopcq ();
601
602
603 /**
604  * Send operation failure message to client
605  *
606  * @param client the client to which the failure message has to be sent to
607  * @param operation_id the id of the failed operation
608  * @param emsg the error message; can be NULL
609  */
610 void
611 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
612                              uint64_t operation_id, const char *emsg);
613
614
615 /**
616  * Function to send generic operation success message to given client
617  *
618  * @param client the client to send the message to
619  * @param operation_id the id of the operation which was successful
620  */
621 void
622 GST_send_operation_success_msg (struct GNUNET_SERVER_Client *client,
623                                 uint64_t operation_id);
624
625
626 /**
627  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
628  *
629  * @param cls NULL
630  * @param client identification of the client
631  * @param message the actual message
632  */
633 void
634 GST_handle_remote_overlay_connect (void *cls,
635                                    struct GNUNET_SERVER_Client *client,
636                                    const struct GNUNET_MessageHeader *message);
637
638
639 /**
640  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
641  *
642  * @param cls NULL
643  * @param client identification of the client
644  * @param message the actual message
645  */
646 void
647 GST_handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
648                         const struct GNUNET_MessageHeader *message);
649
650
651 /**
652  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
653  *
654  * @param cls NULL
655  * @param client identification of the client
656  * @param message the actual message
657  */
658 void
659 GST_handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
660                          const struct GNUNET_MessageHeader *message);
661
662
663 /**
664  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
665  *
666  * @param cls NULL
667  * @param client identification of the client
668  * @param message the actual message
669  */
670 void
671 GST_handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
672                        const struct GNUNET_MessageHeader *message);
673
674
675 /**
676  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
677  *
678  * @param cls NULL
679  * @param client identification of the client
680  * @param message the actual message
681  */
682 void
683 GST_handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
684                       const struct GNUNET_MessageHeader *message);
685
686
687 /**
688  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
689  *
690  * @param cls NULL
691  * @param client identification of the client
692  * @param message the actual message
693  */
694 void
695 GST_handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
696                             const struct GNUNET_MessageHeader *message);
697
698
699 /**
700  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS messages
701  *
702  * @param cls NULL
703  * @param client identification of the client
704  * @param message the actual message
705  */
706 void
707 GST_handle_shutdown_peers (void *cls, struct GNUNET_SERVER_Client *client,
708                            const struct GNUNET_MessageHeader *message);
709
710
711 /**
712  * Handler for GNUNET_TESTBED_ManagePeerServiceMessage message
713  *
714  * @param cls NULL
715  * @param client identification of client
716  * @param message the actual message
717  */
718 void
719 GST_handle_manage_peer_service (void *cls, struct GNUNET_SERVER_Client *client,
720                                 const struct GNUNET_MessageHeader *message);
721
722
723 /**
724  * Handler for GNUNET_MESSAGE_TYPDE_TESTBED_RECONFIGURE_PEER type messages.
725  * Should stop the peer asyncronously, destroy it and create it again with the
726  * new configuration.
727  *
728  * @param cls NULL
729  * @param client identification of the client
730  * @param message the actual message
731  */
732 void
733 GST_handle_peer_reconfigure (void *cls, struct GNUNET_SERVER_Client *client,
734                              const struct GNUNET_MessageHeader *message);
735
736
737 /**
738  * Frees the ManageServiceContext queue
739  */
740 void
741 GST_free_mctxq ();
742
743
744 /**
745  * Cleans up the queue used for forwarding link controllers requests
746  */
747 void
748 GST_free_lcfq ();
749
750
751 /**
752  * Cleans up the route list
753  */
754 void
755 GST_route_list_clear ();
756
757
758 /**
759  * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
760  *
761  * @param rhc the RegisteredHostContext
762  */
763 void
764 GST_process_next_focc (struct RegisteredHostContext *rhc);
765
766
767 /**
768  * Cleans up ForwardedOverlayConnectContext
769  *
770  * @param focc the ForwardedOverlayConnectContext to cleanup
771  */
772 void
773 GST_cleanup_focc (struct ForwardedOverlayConnectContext *focc);
774
775
776 /**
777  * Clears all pending overlay connect contexts in queue
778  */
779 void
780 GST_free_occq ();
781
782
783 /**
784  * Clears all pending remote overlay connect contexts in queue
785  */
786 void
787 GST_free_roccq ();
788
789
790 /**
791  * Cleans up the Peer reconfigure context list
792  */
793 void
794 GST_free_prcq ();
795
796
797 /**
798  * Initializes the cache
799  *
800  * @param size the size of the cache
801  */
802 void
803 GST_cache_init (unsigned int size);
804
805
806 /**
807  * Clear cache
808  */
809 void
810 GST_cache_clear ();
811
812
813 /**
814  * Looks up in the hello cache and returns the HELLO of the given peer
815  *
816  * @param peer_id the index of the peer whose HELLO has to be looked up
817  * @return the HELLO message; NULL if not found
818  */
819 const struct GNUNET_MessageHeader *
820 GST_cache_lookup_hello (const unsigned int peer_id);
821
822
823 /**
824  * Caches the HELLO of the given peer. Updates the HELLO if it was already
825  * cached before
826  *
827  * @param peer_id the peer identity of the peer whose HELLO has to be cached
828  * @param hello the HELLO message
829  */
830 void
831 GST_cache_add_hello (const unsigned int peer_id,
832                      const struct GNUNET_MessageHeader *hello);
833
834
835 /**
836  * Initialize logging CPU and IO statisticfs.  Checks the configuration for
837  * "STATS_DIR" and logs to a file in that directory.  The file is name is
838  * generated from the hostname and the process's PID.
839  */
840 void
841 GST_stats_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
842
843
844 /**
845  * Shutdown the status calls module.
846  */
847 void
848 GST_stats_destroy ();
849
850 /* End of gnunet-service-testbed.h */