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