789627a56049970e7d4555408b9bbb5b063e3993
[oweals/gnunet.git] / src / include / gnunet_testbed_service.h
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009, 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 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., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file include/gnunet_testbed_service.h
23  * @brief API for writing tests and creating large-scale
24  *        emulation testbeds for GNUnet.
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_TESTBED_SERVICE_H
29 #define GNUNET_TESTBED_SERVICE_H
30
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib-new.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42
43 /**
44  * Opaque handle to a host running experiments managed by the testbed framework.
45  * The master process must be able to SSH to this host without password (via
46  * ssh-agent).
47  */
48 struct GNUNET_TESTBED_Host;
49
50 /**
51  * Opaque handle to a peer controlled by the testbed framework.  A peer runs
52  * at a particular host.
53  */ 
54 struct GNUNET_TESTBED_Peer;
55
56 /**
57  * Opaque handle to an abstract operation to be executed by the testbed framework.
58  */
59 struct GNUNET_TESTBED_Operation;
60
61 /**
62  * Handle to interact with a GNUnet testbed controller.  Each
63  * controller has at least one master handle which is created when the
64  * controller is created; this master handle interacts with the
65  * controller process, destroying it destroys the controller (by
66  * closing stdin of the controller process).  Additionally,
67  * controllers can interact with each other (in a P2P fashion); those
68  * links are established via TCP/IP on the controller's service port.
69  */
70 struct GNUNET_TESTBED_Controller;
71
72 /**
73  * Handle to a large-scale testbed that is managed at a high level.
74  */
75 struct GNUNET_TESTBED_Testbed;
76
77
78 /**
79  * Create a host to run peers and controllers on.
80  * 
81  * @param hostname name of the host, use "NULL" for localhost
82  * @param username username to use for the login; may be NULL
83  * @param port port number to use for ssh; use 0 to let ssh decide
84  * @return handle to the host, NULL on error
85  */
86 struct GNUNET_TESTBED_Host *
87 GNUNET_TESTBED_host_create (const char *hostname,
88                             const char *username,
89                             uint16_t port);
90
91
92
93 /**
94  * Create a host to run peers and controllers on.  This function is used
95  * if a peer learns about a host via IPC between controllers (and thus 
96  * some higher-level controller has already determined the unique IDs).
97  * 
98  * @param id global host ID assigned to the host; 0 is
99  *        reserved to always mean 'localhost'
100  * @param hostname name of the host, use "NULL" for localhost
101  * @param username username to use for the login; may be NULL
102  * @param port port number to use for ssh; use 0 to let ssh decide
103  * @return handle to the host, NULL on error
104  */
105 struct GNUNET_TESTBED_Host *
106 GNUNET_TESTBED_host_create_with_id (uint32_t id,
107                                     const char *hostname,
108                                     const char *username,
109                                     uint16_t port);
110
111
112 /**
113  * Load a set of hosts from a configuration file.
114  *
115  * @param filename file with the host specification
116  * @param hosts set to the hosts found in the file
117  * @return number of hosts returned in 'hosts', 0 on error
118  */
119 unsigned int
120 GNUNET_TESTBED_hosts_load_from_file (const char *filename,
121                                      struct GNUNET_TESTBED_Host **hosts);
122
123
124 /**
125  * Destroy a host handle.  Must only be called once everything
126  * running on that host has been stopped.
127  *
128  * @param host handle to destroy
129  */
130 void
131 GNUNET_TESTBED_host_destroy (struct GNUNET_TESTBED_Host *host);
132
133
134 /**
135  * Enumeration with (at most 64) possible event types that
136  * can be monitored using the testbed framework.
137  */
138 enum GNUNET_TESTBED_EventType
139 {
140   /**
141    * A peer has been started.
142    */
143   GNUNET_TESTBED_ET_PEER_START = 0,
144
145   /**
146    * A peer has been stopped.
147    */
148   GNUNET_TESTBED_ET_PEER_STOP = 1,
149
150   /**
151    * A connection between two peers was established.
152    */
153   GNUNET_TESTBED_ET_CONNECT = 2,
154
155   /**
156    * A connection between two peers was torn down.
157    */
158   GNUNET_TESTBED_ET_DISCONNECT = 3,
159
160   /**
161    * A requested testbed operation has been completed.
162    */
163   GNUNET_TESTBED_ET_OPERATION_FINISHED = 4,
164
165   /**
166    * The 'GNUNET_TESTBED_run' operation has been completed
167    */
168   GNUNET_TESTBED_ET_TESTBED_ONLINE = 5
169
170 };
171
172
173 /**
174  * Types of information that can be requested about a peer.
175  */
176 enum GNUNET_TESTBED_PeerInformationType
177 {
178
179   /**
180    * Special value (not valid for requesting information)
181    * that is used in the event struct if a 'generic' pointer
182    * is returned (for other operations not related to this
183    * enumeration).
184    */
185   GNUNET_TESTBED_PIT_GENERIC = 0,
186
187   /**
188    * What configuration is the peer using?  Returns a 'const struct
189    * GNUNET_CONFIGURATION_Handle *'.  Valid until
190    * 'GNUNET_TESTNIG_operation_done' is called.  However, the
191    * values may be inaccurate if the peer is reconfigured in
192    * the meantime.
193    */
194   GNUNET_TESTBED_PIT_CONFIGURATION,
195
196   /**
197    * What is the identity of the peer?  Returns a
198    * 'const struct GNUNET_PeerIdentity *'.  Valid until
199    * 'GNUNET_TESTNIG_operation_done' is called.
200    */
201   GNUNET_TESTBED_PIT_IDENTITY
202
203 };
204
205
206 /**
207  * Argument to GNUNET_TESTBED_ControllerCallback with details about
208  * the event.
209  */
210 struct GNUNET_TESTBED_EventInformation
211 {
212   
213   /**
214    * Type of the event.
215    */
216   enum GNUNET_TESTBED_EventType type;
217
218   /**
219    * Details about the event.
220    */
221   union
222   {
223     
224     /**
225      * Details about peer start event.
226      */ 
227     struct
228     {
229       /**
230        * Handle for the host where the peer
231        * was started.
232        */
233       struct GNUNET_TESTBED_Host *host;
234
235       /**
236        * Handle for the peer that was started.
237        */
238       struct GNUNET_TESTBED_Peer *peer;
239       
240     } peer_start;
241
242     /**
243      * Details about peer stop event.
244      */ 
245     struct
246     {
247
248       /**
249        * Handle for the peer that was started.
250        */
251       struct GNUNET_TESTBED_Peer *peer;
252       
253     } peer_stop;
254
255     /**
256      * Details about connect event.
257      */ 
258     struct
259     {
260       /**
261        * Handle for one of the connected peers.
262        */
263       struct GNUNET_TESTBED_Peer *peer1;
264
265       /**
266        * Handle for one of the connected peers.
267        */
268       struct GNUNET_TESTBED_Peer *peer2;
269
270     } peer_connect;
271
272     /**
273      * Details about disconnect event.
274      */ 
275     struct
276     {
277       /**
278        * Handle for one of the disconnected peers.
279        */
280       struct GNUNET_TESTBED_Peer *peer1;
281
282       /**
283        * Handle for one of the disconnected peers.
284        */
285       struct GNUNET_TESTBED_Peer *peer2;
286       
287     } peer_disconnect;
288
289     /**
290      * Details about an operation finished event.
291      */ 
292     struct 
293     {
294
295       /**
296        * Handle for the operation that was finished.
297        */
298       struct GNUNET_TESTBED_Operation *operation;
299
300       /**
301        * Closure that was passed in when the event was
302        * requested.
303        */
304       void *op_cls;
305
306       /**
307        * Error message for the operation, NULL on success.
308        */ 
309       const char *emsg;
310
311       /**
312        * Peer information type; captures which of the types
313        * in the 'op_result' is actually in use.
314        */
315       enum GNUNET_TESTBED_PeerInformationType pit;
316
317       /**
318        * Pointer to an operation-specific return value; NULL on error;
319        * can be NULL for certain operations.  Valid until
320        * 'GNUNET_TESTBED_operation_done' is called.
321        */
322       union
323       {
324         /**
325          * No result (NULL pointer) or generic result
326          * (whatever the GNUNET_TESTBED_ConnectAdapter returned).
327          */
328         void *generic;
329
330         /**
331          * Identity of host running the peer.
332          */
333         struct GNUNET_TESTBED_Host *host;
334
335         /**
336          * Identity of the peer.
337          */
338         const struct GNUNET_PeerIdentity *pid;
339
340         /**
341          * Configuration of the peer.
342          */
343         const struct GNUNET_CONFIGURATION_Handle *cfg;
344
345       } op_result;
346
347     } operation_finished;   
348
349
350     /**
351      * Details about an testbed run completed event.
352      */ 
353     struct 
354     {
355
356       /**
357        * Error message for the operation, NULL on success.
358        */ 
359       const char *emsg;
360
361       /**
362        * Array of peers now running (valid until
363        * 'GNUNET_TESTBED_testbed_stop' is called).  Note that it is
364        * not allowed to call 'GNUNET_TESTBED_peer_destroy' on peers
365        * from this array.
366        */
367       struct GNUNET_TESTBED_Peer **peers;
368
369       /**
370        * Size of the 'peers' array.
371        */
372       unsigned int num_peers;
373       
374     } testbed_run_finished;   
375
376   } details;
377
378 };
379
380
381 /**
382  * Signature of the event handler function called by the
383  * respective event controller.
384  *
385  * @param cls closure
386  * @param event information about the event
387  */
388 typedef void (*GNUNET_TESTBED_ControllerCallback)(void *cls,
389                                                   const struct GNUNET_TESTBED_EventInformation *event);
390
391
392 /**
393  * Opaque Handle for Controller process
394  */
395 struct GNUNET_TESTBED_ControllerProc;
396
397
398 /**
399  * Callback to signal successfull startup of the controller process
400  *
401  * @param cls the closure from GNUNET_TESTBED_controller_start()
402  * @param cfg the configuration with which the controller has been started;
403  *          NULL if status is not GNUNET_OK
404  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
405  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
406  */
407 typedef void (*GNUNET_TESTBED_ControllerStatusCallback) (void *cls, 
408                                                         const struct GNUNET_CONFIGURATION_Handle *cfg,
409                                                         int status);
410
411
412 /**
413  * Starts a controller process at the host. FIXME: add controller start callback
414  * with the configuration with which the controller is started
415  *
416  * @param controller_ip the ip address of the controller. Will be set as TRUSTED
417  *          host when starting testbed controller at host
418  * @param host the host where the controller has to be started; NULL for
419  *          localhost
420  * @param cfg template configuration to use for the remote controller; the
421  *          remote controller will be started with a slightly modified
422  *          configuration (port numbers, unix domain sockets and service home
423  *          values are changed as per TESTING library on the remote host)
424  * @param cb function called when the controller is successfully started or
425  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
426  *          called if cb is called with GNUNET_SYSERR as status. Will never be
427  *          called in the same task as 'GNUNET_TESTBED_controller_start'
428  *          (synchronous errors will be signalled by returning NULL). This
429  *          parameter cannot be NULL.
430  * @param cls closure for above callbacks
431  * @return the controller process handle, NULL on errors
432  */
433 struct GNUNET_TESTBED_ControllerProc *
434 GNUNET_TESTBED_controller_start (const char *controller_ip,
435                                  struct GNUNET_TESTBED_Host *host,
436                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
437                                  GNUNET_TESTBED_ControllerStatusCallback cb,
438                                  void *cls);
439
440
441 /**
442  * Stop the controller process (also will terminate all peers and controllers
443  * dependent on this controller).  This function blocks until the testbed has
444  * been fully terminated (!).
445  *
446  * @param cproc the controller process handle
447  */
448 void
449 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc);
450
451
452 /**
453  * Connect to a controller process using the given configuration at the
454  * given host.
455  *
456  * @param cfg configuration to use
457  * @param host host to run the controller on; This should be the same host if
458  *          the controller was previously started with
459  *          GNUNET_TESTBED_controller_start; NULL for localhost
460  * @param host host where this controller is being run;
461  * @param event_mask bit mask with set of events to call 'cc' for;
462  *                   or-ed values of "1LL" shifted by the
463  *                   respective 'enum GNUNET_TESTBED_EventType'
464  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
465  * @param cc controller callback to invoke on events
466  * @param cc_cls closure for cc
467  * @return handle to the controller
468  */
469 struct GNUNET_TESTBED_Controller *
470 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
471                                    struct GNUNET_TESTBED_Host *host,
472                                    uint64_t event_mask,
473                                    GNUNET_TESTBED_ControllerCallback cc,
474                                    void *cc_cls);
475
476
477 /**
478  * Configure shared services at a controller.  Using this function,
479  * you can specify that certain services (such as "resolver")
480  * should not be run for each peer but instead be shared
481  * across N peers on the specified host.  This function
482  * must be called before any peers are created at the host.
483  * 
484  * @param controller controller to configure
485  * @param service_name name of the service to share
486  * @param num_peers number of peers that should share one instance
487  *        of the specified service (1 for no sharing is the default),
488  *        use 0 to disable the service
489  */
490 void
491 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
492                                              const char *service_name,
493                                              uint32_t num_peers);
494
495
496 /**
497  * Stop the given controller (also will terminate all peers and
498  * controllers dependent on this controller).  This function 
499  * blocks until the testbed has been fully terminated (!).
500  *
501  * @param controller handle to controller to stop
502  */
503 void
504 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller);
505
506
507 /**
508  * Opaque handle for host registration
509  */
510 struct GNUNET_TESTBED_HostRegistrationHandle;
511
512
513 /**
514  * Callback which will be called to after a host registration succeeded or failed
515  *
516  * @param cls the closure
517  * @param emsg the error message; NULL if host registration is successful
518  */
519 typedef void (* GNUNET_TESTBED_HostRegistrationCompletion) (void *cls, 
520                                                             const char *emsg);
521
522
523 /**
524  * Register a host with the controller
525  *
526  * @param controller the controller handle
527  * @param host the host to register
528  * @param cc the completion callback to call to inform the status of
529  *          registration. After calling this callback the registration handle
530  *          will be invalid. Cannot be NULL
531  * @param cc_cls the closure for the cc
532  * @return handle to the host registration which can be used to cancel the
533  *           registration; NULL if another registration handle is present and
534  *           is not cancelled
535  */
536 struct GNUNET_TESTBED_HostRegistrationHandle *
537 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
538                               struct GNUNET_TESTBED_Host *host,
539                               GNUNET_TESTBED_HostRegistrationCompletion cc,
540                               void *cc_cls);
541
542
543 /**
544  * Cancel the pending registration. Note that the registration message will
545  * already be queued to be sent to the service, cancellation has only the
546  * effect that the registration completion callback for the registration is
547  * never called and from our perspective the host is not registered until the
548  * completion callback is called.
549  *
550  * @param handle the registration handle to cancel
551  */
552 void
553 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
554                                     *handle);
555
556
557 /**
558  * Create a link from slave controller to delegated controller. Whenever the
559  * master controller is asked to start a peer at the delegated controller the
560  * request will be routed towards slave controller (if a route exists). The
561  * slave controller will then route it to the delegated controller. The
562  * configuration of the slave controller is given and to be used to either
563  * create the slave controller or to connect to an existing slave controller
564  * process.  'is_subordinate' specifies if the given slave controller should be
565  * started and managed by the master controller, or if the slave already has a
566  * master and this is just a secondary master that is also allowed to use the
567  * existing slave.
568  *
569  * @param master handle to the master controller who creates the association
570  * @param delegated_host requests to which host should be delegated; cannot be NULL
571  * @param slave_host which host is used to run the slave controller; use NULL to
572  *          make the master controller connect to the delegated host
573  * @param slave_cfg configuration to use for the slave controller
574  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
575  *          be started by the master controller; GNUNET_NO if we are just
576  *          allowed to use the slave via TCP/IP
577  * @return the operation handle
578  */
579 struct GNUNET_TESTBED_Operation *
580 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
581                                 struct GNUNET_TESTBED_Host *delegated_host,
582                                 struct GNUNET_TESTBED_Host *slave_host,
583                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
584                                 int is_subordinate);
585
586
587 /**
588  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
589  * serialized and compressed
590  *
591  * @param master handle to the master controller who creates the association
592  * @param delegated_host requests to which host should be delegated; cannot be NULL
593  * @param slave_host which host is used to run the slave controller; use NULL to
594  *          make the master controller connect to the delegated host
595  * @param sxcfg serialized and compressed configuration
596  * @param sxcfg_size the size scfg
597  * @param scfg_size the size of uncompressed serialized configuration
598  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
599  *          be started by the master controller; GNUNET_NO if we are just
600  *          allowed to use the slave via TCP/IP
601  * @return the operation handle
602  */
603 struct GNUNET_TESTBED_Operation *
604 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
605                                   struct GNUNET_TESTBED_Host *delegated_host,
606                                   struct GNUNET_TESTBED_Host *slave_host,
607                                   const char *sxcfg,
608                                   size_t sxcfg_size,
609                                   size_t scfg_size,
610                                   int is_subordinate);
611
612
613 /**
614  * Functions of this signature are called when a peer has been successfully
615  * created
616  *
617  * @param cls the closure from GNUNET_TESTBED_peer_create()
618  * @param peer the handle for the created peer; NULL on any error during
619  *          creation
620  * @param emsg NULL if peer is not NULL; else MAY contain the error description
621  */
622 typedef void (*GNUNET_TESTBED_PeerCreateCallback) (void *cls,
623                                                    struct GNUNET_TESTBED_Peer *peer,
624                                                    const char *emsg);
625
626
627 /**
628  * Create the given peer at the specified host using the given
629  * controller.  If the given controller is not running on the target
630  * host, it should find or create a controller at the target host and
631  * delegate creating the peer.  Explicit delegation paths can be setup
632  * using 'GNUNET_TESTBED_controller_link'.  If no explicit delegation
633  * path exists, a direct link with a subordinate controller is setup
634  * for the first delegated peer to a particular host; the subordinate
635  * controller is then destroyed once the last peer that was delegated
636  * to the remote host is stopped.
637  *
638  * Creating the peer only creates the handle to manipulate and further
639  * configure the peer; use "GNUNET_TESTBED_peer_start" and
640  * "GNUNET_TESTBED_peer_stop" to actually start/stop the peer's
641  * processes.
642  *
643  * Note that the given configuration will be adjusted by the
644  * controller to avoid port/path conflicts with other peers.
645  * The "final" configuration can be obtained using
646  * 'GNUNET_TESTBED_peer_get_information'.
647  *
648  * @param controller controller process to use
649  * @param host host to run the peer on
650  * @param cfg Template configuration to use for the peer. Should exist until
651  *          operation is cancelled or GNUNET_TESTBED_operation_done() is called
652  * @param cb the callback to call when the peer has been created
653  * @param cls the closure to the above callback
654  * @return the operation handle
655  */
656 struct GNUNET_TESTBED_Operation *
657 GNUNET_TESTBED_peer_create (struct GNUNET_TESTBED_Controller *controller,
658                             struct GNUNET_TESTBED_Host *host,
659                             const struct GNUNET_CONFIGURATION_Handle *cfg,
660                             GNUNET_TESTBED_PeerCreateCallback cb,
661                             void *cls);
662
663
664 /**
665  * Start the given peer.
666  *
667  * @param peer peer to start
668  * @return handle to the operation
669  */
670 struct GNUNET_TESTBED_Operation *
671 GNUNET_TESTBED_peer_start (struct GNUNET_TESTBED_Peer *peer);
672
673
674 /**
675  * Stop the given peer.  The handle remains valid (use
676  * "GNUNET_TESTBED_peer_destroy" to fully clean up the 
677  * state of the peer).
678  *
679  * @param peer peer to stop
680  * @return handle to the operation
681  */
682 struct GNUNET_TESTBED_Operation *
683 GNUNET_TESTBED_peer_stop (struct GNUNET_TESTBED_Peer *peer);
684
685
686 /**
687  * Request information about a peer.
688  *
689  * @param peer peer to request information about
690  * @param pit desired information
691  * @return handle to the operation
692  */
693 struct GNUNET_TESTBED_Operation *
694 GNUNET_TESTBED_peer_get_information (struct GNUNET_TESTBED_Peer *peer,
695                                      enum GNUNET_TESTBED_PeerInformationType pit);
696
697
698 /**
699  * Change peer configuration.  Must only be called while the
700  * peer is stopped.  Ports and paths cannot be changed this
701  * way.
702  *
703  * @param peer peer to change configuration for
704  * @param cfg new configuration (differences to existing
705  *            configuration only)
706  * @return handle to the operation
707  */
708 struct GNUNET_TESTBED_Operation *
709 GNUNET_TESTBED_peer_update_configuration (struct GNUNET_TESTBED_Peer *peer,
710                                           const struct GNUNET_CONFIGURATION_Handle *cfg);
711
712
713 /**
714  * Destroy the given peer; the peer should have been
715  * stopped first (if it was started).
716  *
717  * @param peer peer to stop
718  * @return handle to the operation
719  */
720 struct GNUNET_TESTBED_Operation *
721 GNUNET_TESTBED_peer_destroy (struct GNUNET_TESTBED_Peer *peer);
722
723
724 /**
725  * Options for peer connections.
726  */
727 enum GNUNET_TESTBED_ConnectOption
728 {
729   /**
730    * No option (not valid as an argument).
731    */
732   GNUNET_TESTBED_CO_NONE = 0,
733   
734   /**
735    * Allow or disallow a connection between the specified peers.  
736    * Followed by GNUNET_NO (int) if a connection is disallowed
737    * or GNUNET_YES if a connection is allowed.  Note that the
738    * default (all connections allowed or disallowed) is
739    * specified in the configuration of the controller.
740    */
741   GNUNET_TESTBED_CO_ALLOW = 1,
742   
743   /**
744    * FIXME: add (and implement) options to limit connection to
745    * particular transports, force simulation of particular latencies
746    * or message loss rates, or set bandwidth limitations.
747    */
748   
749 };
750
751
752 /**
753  * Manipulate the P2P underlay topology by configuring a link
754  * between two peers.  
755  *
756  * @param op_cls closure argument to give with the operation event
757  * @param p1 first peer
758  * @param p2 second peer
759  * @param co option to change
760  * @param ap option-specific values
761  * @return handle to the operation, NULL if configuring the link at this
762  *         time is not allowed
763  */
764 struct GNUNET_TESTBED_Operation *
765 GNUNET_TESTBED_underlay_configure_link_va (void *op_cls,
766                                            struct GNUNET_TESTBED_Peer *p1,
767                                            struct GNUNET_TESTBED_Peer *p2,
768                                            enum GNUNET_TESTBED_ConnectOption co,
769                                            va_list ap);
770
771
772 /**
773  * Manipulate the P2P underlay topology by configuring a link
774  * between two peers.  
775  *
776  * @param op_cls closure argument to give with the operation event
777  * @param p1 first peer
778  * @param p2 second peer
779  * @param co option to change
780  * @param ... option-specific values
781  * @return handle to the operation, NULL if configuring the link at this
782  *         time is not allowed
783  */
784 struct GNUNET_TESTBED_Operation *
785 GNUNET_TESTBED_underlay_configure_link (void *op_cls,
786                                         struct GNUNET_TESTBED_Peer *p1,
787                                         struct GNUNET_TESTBED_Peer *p2,
788                                         enum GNUNET_TESTBED_ConnectOption co, ...);
789
790
791
792 /**
793  * Topologies supported for testbeds.
794  */
795 enum GNUNET_TESTBED_TopologyOption
796 {
797   /**
798    * A clique (everyone connected to everyone else).  No options.
799    */
800   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
801
802   /**
803    * Small-world network (2d torus plus random links).  Followed
804    * by the number of random links to add (unsigned int).
805    */
806   GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD,
807
808   /**
809    * Small-world network (ring plus random links).  Followed
810    * by the number of random links to add (unsigned int).
811    */
812   GNUNET_TESTBED_TOPOLOGY_SMALL_WORLD_RING,
813
814   /**
815    * Ring topology.  No options.
816    */
817   GNUNET_TESTBED_TOPOLOGY_RING,
818
819   /**
820    * 2-d torus.  No options.
821    */
822   GNUNET_TESTBED_TOPOLOGY_2D_TORUS,
823
824   /**
825    * Random graph.  Followed by the link density, that is the
826    * percentage of links present in relation to a clique
827    * (float).
828    */
829   GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
830
831   /**
832    * Certain percentage of peers are unable to communicate directly
833    * replicating NAT conditions.  Followed by the fraction of
834    * NAT'ed peers (float).
835    */
836   GNUNET_TESTBED_TOPOLOGY_INTERNAT,
837
838   /**
839    * Scale free topology.   FIXME: options?
840    */
841   GNUNET_TESTBED_TOPOLOGY_SCALE_FREE,
842
843   /**
844    * Straight line topology.  No options.
845    */
846   GNUNET_TESTBED_TOPOLOGY_LINE,
847
848   /**
849    * All peers are disconnected.  No options.
850    */
851   GNUNET_TESTBED_TOPOLOGY_NONE,
852
853   /**
854    * Read a topology from a given file.  Followed by the name of the file (const char *).
855    */
856   GNUNET_TESTBED_TOPOLOGY_FROM_FILE
857 };
858
859
860 /**
861  * Configure overall network topology to have a particular shape.
862  *
863  * @param op_cls closure argument to give with the operation event
864  * @param num_peers number of peers in 'peers'
865  * @param peers array of 'num_peers' with the peers to configure
866  * @param topo desired underlay topology to use
867  * @param ap topology-specific options
868  * @return handle to the operation, NULL if configuring the topology
869  *         is not allowed at this time
870  */
871 struct GNUNET_TESTBED_Operation *
872 GNUNET_TESTBED_underlay_configure_topology_va (void *op_cls,
873                                                unsigned int num_peers,
874                                                struct GNUNET_TESTBED_Peer **peers,
875                                                enum GNUNET_TESTBED_TopologyOption topo,
876                                                va_list ap);
877
878
879 /**
880  * Configure overall network topology to have a particular shape.
881  *
882  * @param op_cls closure argument to give with the operation event
883  * @param num_peers number of peers in 'peers'
884  * @param peers array of 'num_peers' with the peers to configure
885  * @param topo desired underlay topology to use
886  * @param ... topology-specific options
887  * @return handle to the operation, NULL if configuring the topology
888  *         is not allowed at this time
889  */
890 struct GNUNET_TESTBED_Operation *
891 GNUNET_TESTBED_underlay_configure_topology (void *op_cls,
892                                             unsigned int num_peers,
893                                             struct GNUNET_TESTBED_Peer **peers,
894                                             enum GNUNET_TESTBED_TopologyOption topo,
895                                             ...);
896
897
898 /**
899  * Both peers must have been started before calling this function.
900  * This function then obtains a HELLO from 'p1', gives it to 'p2'
901  * and asks 'p2' to connect to 'p1'.
902  *
903  * @param op_cls closure argument to give with the operation event
904  * @param p1 first peer
905  * @param p2 second peer
906  * @return handle to the operation, NULL if connecting these two
907  *         peers is fundamentally not possible at this time (peers
908  *         not running or underlay disallows)
909  */
910 struct GNUNET_TESTBED_Operation *
911 GNUNET_TESTBED_overlay_connect (void *op_cls,
912                                 struct GNUNET_TESTBED_Peer *p1,
913                                 struct GNUNET_TESTBED_Peer *p2);
914
915
916 /**
917  * All peers must have been started before calling this function.
918  * This function then connects the given peers in the P2P overlay
919  * using the given topology.
920  *
921  * @param op_cls closure argument to give with the operation event
922  * @param num_peers number of peers in 'peers'
923  * @param peers array of 'num_peers' with the peers to configure
924  * @param topo desired underlay topology to use
925  * @param va topology-specific options
926  * @return handle to the operation, NULL if connecting these 
927  *         peers is fundamentally not possible at this time (peers
928  *         not running or underlay disallows)
929  */
930 struct GNUNET_TESTBED_Operation *
931 GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls,
932                                               unsigned int num_peers,
933                                               struct GNUNET_TESTBED_Peer *peers,
934                                               enum GNUNET_TESTBED_TopologyOption topo,
935                                               va_list va);
936
937
938 /**
939  * All peers must have been started before calling this function.
940  * This function then connects the given peers in the P2P overlay
941  * using the given topology.
942  *
943  * @param op_cls closure argument to give with the operation event
944  * @param num_peers number of peers in 'peers'
945  * @param peers array of 'num_peers' with the peers to configure
946  * @param topo desired underlay topology to use
947  * @param ... topology-specific options
948  * @return handle to the operation, NULL if connecting these 
949  *         peers is fundamentally not possible at this time (peers
950  *         not running or underlay disallows)
951  */
952 struct GNUNET_TESTBED_Operation *
953 GNUNET_TESTBED_overlay_configure_topology (void *op_cls,
954                                            unsigned int num_peers,
955                                            struct GNUNET_TESTBED_Peer *peers,
956                                            enum GNUNET_TESTBED_TopologyOption topo,
957                                            ...);
958
959
960 /**
961  * Ask the testbed controller to write the current overlay topology to
962  * a file.  Naturally, the file will only contain a snapshot as the
963  * topology may evolve all the time.
964  *
965  * @param controller overlay controller to inspect
966  * @param filename name of the file the topology should
967  *        be written to.
968  */
969 void
970 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
971                                                const char *filename);
972
973
974 /**
975  * Adapter function called to establish a connection to
976  * a service.
977  * 
978  * @param cls closure
979  * @param cfg configuration of the peer to connect to; will be available until
980  *          GNUNET_TESTBED_operation_done() is called on the operation returned
981  *          from GNUNET_TESTBED_service_connect()
982  * @return service handle to return in 'op_result', NULL on error
983  */
984 typedef void * (*GNUNET_TESTBED_ConnectAdapter)(void *cls,
985                                                 const struct GNUNET_CONFIGURATION_Handle *cfg);
986
987
988 /**
989  * Adapter function called to destroy a connection to
990  * a service.
991  * 
992  * @param cls closure
993  * @param op_result service handle returned from the connect adapter
994  */
995 typedef void (*GNUNET_TESTBED_DisconnectAdapter)(void *cls,
996                                                  void *op_result);
997
998
999 /**
1000  * Connect to a service offered by the given peer.  Will ensure that
1001  * the request is queued to not overwhelm our ability to create and
1002  * maintain connections with other systems.  The actual service
1003  * handle is then returned via the 'op_result' member in the event
1004  * callback.  The 'ca' callback is used to create the connection
1005  * when the time is right; the 'da' callback will be used to 
1006  * destroy the connection (upon 'GNUNET_TESTBED_operation_done').
1007  * 'GNUNET_TESTBED_operation_cancel' can be used to abort this
1008  * operation until the event callback has been called.
1009  *
1010  * @param op_cls closure to pass in operation event
1011  * @param peer peer that runs the service
1012  * @param service_name name of the service to connect to
1013  * @param ca helper function to establish the connection
1014  * @param da helper function to close the connection
1015  * @param cada_cls closure for ca and da
1016  * @return handle for the operation
1017  */
1018 struct GNUNET_TESTBED_Operation *
1019 GNUNET_TESTBED_service_connect (void *op_cls,
1020                                 struct GNUNET_TESTBED_Peer *peer,
1021                                 const char *service_name,
1022                                 GNUNET_TESTBED_ConnectAdapter ca,
1023                                 GNUNET_TESTBED_DisconnectAdapter da,
1024                                 void *cada_cls);
1025
1026
1027 /**
1028  * Cancel a pending operation.  Releases all resources
1029  * of the operation and will ensure that no event
1030  * is generated for the operation.  Does NOT guarantee
1031  * that the operation will be fully undone (or that
1032  * nothing ever happened).  
1033  * 
1034  * @param operation operation to cancel
1035  */
1036 void
1037 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation);
1038
1039
1040 /**
1041  * Signal that the information from an operation has been fully
1042  * processed.  This function MUST be called for each event
1043  * of type 'operation_finished' to fully remove the operation
1044  * from the operation queue.  After calling this function, the
1045  * 'op_result' becomes invalid (!).
1046  * 
1047  * @param operation operation to signal completion for
1048  */
1049 void
1050 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation);
1051
1052
1053 /**
1054  * Configure and run a testbed using the given
1055  * master controller on 'num_hosts' starting
1056  * 'num_peers' using the given peer configuration.
1057  *
1058  * @param controller master controller for the testbed
1059  *                   (must not be destroyed until after the
1060  *                    testbed is destroyed).
1061  * @param num_hosts number of hosts in 'hosts', 0 to only
1062  *        use 'localhost'
1063  * @param hosts list of hosts to use for the testbed
1064  * @param num_peers number of peers to start
1065  * @param peer_cfg peer configuration template to use
1066  * @param underlay_topology underlay topology to create
1067  * @param va topology-specific options
1068  * @return handle to the testbed
1069  */
1070 struct GNUNET_TESTBED_Testbed *
1071 GNUNET_TESTBED_create_va (struct GNUNET_TESTBED_Controller *controller,
1072                           unsigned int num_hosts,
1073                           struct GNUNET_TESTBED_Host **hosts,
1074                           unsigned int num_peers,
1075                           const struct GNUNET_CONFIGURATION_Handle *peer_cfg,
1076                           enum GNUNET_TESTBED_TopologyOption underlay_topology,
1077                           va_list va);
1078
1079
1080 /**
1081  * Configure and run a testbed using the given
1082  * master controller on 'num_hosts' starting
1083  * 'num_peers' using the given peer configuration.
1084  *
1085  * @param controller master controller for the testbed
1086  *                   (must not be destroyed until after the
1087  *                    testbed is destroyed).
1088  * @param num_hosts number of hosts in 'hosts', 0 to only
1089  *        use 'localhost'
1090  * @param hosts list of hosts to use for the testbed
1091  * @param num_peers number of peers to start
1092  * @param peer_cfg peer configuration template to use
1093  * @param underlay_topology underlay topology to create
1094  * @param ... topology-specific options
1095  */
1096 struct GNUNET_TESTBED_Testbed *
1097 GNUNET_TESTBED_create (struct GNUNET_TESTBED_Controller *controller,
1098                        unsigned int num_hosts,
1099                        struct GNUNET_TESTBED_Host **hosts,
1100                        unsigned int num_peers,
1101                        const struct GNUNET_CONFIGURATION_Handle *peer_cfg,
1102                        enum GNUNET_TESTBED_TopologyOption underlay_topology,
1103                        ...);
1104
1105
1106 /**
1107  * Destroy a testbed.  Stops all running peers and then
1108  * destroys all peers.  Does NOT destroy the master controller.
1109  *
1110  * @param testbed testbed to destroy
1111  */
1112 void
1113 GNUNET_TESTBED_destroy (struct GNUNET_TESTBED_Testbed *testbed);
1114
1115
1116 /**
1117  * Convenience method for running a testbed with
1118  * a single call.  Underlay and overlay topology
1119  * are configured using the "UNDERLAY" and "OVERLAY"
1120  * options in the "[testbed]" section of the configuration\
1121  * (with possible options given in "UNDERLAY_XXX" and/or
1122  * "OVERLAY_XXX").
1123  *
1124  * The testbed is to be terminated using a call to
1125  * "GNUNET_SCHEDULER_shutdown".
1126  *
1127  * @param host_filename name of the file with the 'hosts', NULL
1128  *        to run everything on 'localhost'
1129  * @param cfg configuration to use (for testbed, controller and peers)
1130  * @param num_peers number of peers to start; FIXME: maybe put that ALSO into cfg?
1131  * @param event_mask bit mask with set of events to call 'cc' for;
1132  *                   or-ed values of "1LL" shifted by the
1133  *                   respective 'enum GNUNET_TESTBED_EventType'
1134  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
1135  * @param cc controller callback to invoke on events
1136  * @param cc_cls closure for cc
1137  * @param master task to run once the testbed is ready
1138  * @param master_cls closure for 'task'.
1139  */
1140 void
1141 GNUNET_TESTBED_run (const char *host_filename,
1142                     const struct GNUNET_CONFIGURATION_Handle *cfg,
1143                     unsigned int num_peers,
1144                     uint64_t event_mask,
1145                     GNUNET_TESTBED_ControllerCallback cc,
1146                     void *cc_cls,
1147                     GNUNET_SCHEDULER_Task master,
1148                     void *master_cls);
1149
1150
1151 /**
1152  * Signature of a main function for a testcase.
1153  * 
1154  * @param cls closure
1155  * @param num_peers number of peers in 'peers'
1156  * @param peers handle to peers run in the testbed
1157  */
1158 typedef void (*GNUNET_TESTBED_TestMaster)(void *cls,
1159                                           unsigned int num_peers,
1160                                           struct GNUNET_TESTBED_Peer **peers);
1161                                           
1162
1163 /**
1164  * Convenience method for running a "simple" test on the local system
1165  * with a single call from 'main'.  Underlay and overlay topology are
1166  * configured using the "UNDERLAY" and "OVERLAY" options in the
1167  * "[testbed]" section of the configuration (with possible options
1168  * given in "UNDERLAY_XXX" and/or "OVERLAY_XXX").
1169  *
1170  * The test is to be terminated using a call to
1171  * "GNUNET_SCHEDULER_shutdown".  If starting the test fails,
1172  * the program is stopped without 'master' ever being run.
1173  *
1174  * NOTE: this function should be called from 'main', NOT from
1175  * within a GNUNET_SCHEDULER-loop.  This function will initialze
1176  * the scheduler loop, the testbed and then pass control to
1177  * 'master'.
1178  *
1179  * @param testname name of the testcase (to configure logging, etc.)
1180  * @param cfg_filename configuration filename to use
1181  *              (for testbed, controller and peers)
1182  * @param num_peers number of peers to start
1183  * @param test_master task to run once the test is ready
1184  * @param test_master_cls closure for 'task'.
1185  */
1186 void
1187 GNUNET_TESTBED_test_run (const char *testname,
1188                          const char *cfg_filename,
1189                          unsigned int num_peers,
1190                          GNUNET_TESTBED_TestMaster test_master,
1191                          void *test_master_cls);
1192
1193
1194 #if 0                           /* keep Emacsens' auto-indent happy */
1195 {
1196 #endif
1197
1198
1199 #ifdef __cplusplus
1200 }
1201 #endif
1202
1203 #endif