-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / testbed / testbed_api.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file testbed/testbed_api.h
21  * @brief Interface for functions internally exported from testbed_api.c
22  * @author Sree Harsha Totakura
23  */
24
25 #ifndef TESTBED_API_H
26 #define TESTBED_API_H
27
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "testbed.h"
31 #include "testbed_helper.h"
32
33 /**
34  * Testbed Helper binary name
35  */
36 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
37
38
39 /**
40  * Enumeration of operations
41  */
42 enum OperationType
43 {
44     /**
45      * Peer create operation
46      */
47   OP_PEER_CREATE,
48
49     /**
50      * Peer start operation
51      */
52   OP_PEER_START,
53
54     /**
55      * Peer stop operation
56      */
57   OP_PEER_STOP,
58
59     /**
60      * Peer destroy operation
61      */
62   OP_PEER_DESTROY,
63
64     /**
65      * Get peer information operation
66      */
67   OP_PEER_INFO,
68
69   /**
70    * Reconfigure a peer
71    */
72   OP_PEER_RECONFIGURE,
73
74     /**
75      * Overlay connection operation
76      */
77   OP_OVERLAY_CONNECT,
78
79     /**
80      * Forwarded operation
81      */
82   OP_FORWARDED,
83
84     /**
85      * Link controllers operation
86      */
87   OP_LINK_CONTROLLERS,
88
89   /**
90    * Get slave config operation
91    */
92   OP_GET_SLAVE_CONFIG,
93
94   /**
95    * Stop and destroy all peers
96    */
97   OP_SHUTDOWN_PEERS,
98
99   /**
100    * Start/stop service at a peer
101    */
102   OP_MANAGE_SERVICE
103 };
104
105
106
107 /**
108  * Enumeration of states of OperationContext
109  */
110 enum OperationContextState
111 {
112   /**
113    * The initial state where the associated operation has just been created
114    * and is waiting in the operation queues to be started
115    */
116   OPC_STATE_INIT = 0,
117
118   /**
119    * The operation has been started. It may occupy some resources which are to
120    * be freed if cancelled.
121    */
122   OPC_STATE_STARTED,
123
124   /**
125    * The operation has finished. The end results of this operation may occupy
126    * some resources which are to be freed by operation_done
127    */
128   OPC_STATE_FINISHED
129 };
130
131
132 /**
133  * Context information for GNUNET_TESTBED_Operation
134  */
135 struct OperationContext
136 {
137   /**
138    * The controller to which this operation context belongs to
139    */
140   struct GNUNET_TESTBED_Controller *c;
141
142   /**
143    * The operation
144    */
145   struct GNUNET_TESTBED_Operation *op;
146
147   /**
148    * The operation closure
149    */
150   void *op_cls;
151
152   /**
153    * Data relevant to the operation
154    */
155   void *data;
156
157   /**
158    * The id of the opearation
159    */
160   uint64_t id;
161
162   /**
163    * The type of operation
164    */
165   enum OperationType type;
166
167   /**
168    * The state of the operation
169    */
170   enum OperationContextState state;
171 };
172
173
174 /**
175  * Operation empty callback
176  *
177  * @param cls closure
178  */
179 typedef void
180 (*TESTBED_opcq_empty_cb) (void *cls);
181
182
183 /**
184  * Handle to interact with a GNUnet testbed controller.  Each
185  * controller has at least one master handle which is created when the
186  * controller is created; this master handle interacts with the
187  * controller process, destroying it destroys the controller (by
188  * closing stdin of the controller process).  Additionally,
189  * controllers can interact with each other (in a P2P fashion); those
190  * links are established via TCP/IP on the controller's service port.
191  */
192 struct GNUNET_TESTBED_Controller
193 {
194   /**
195    * The host where the controller is running
196    */
197   struct GNUNET_TESTBED_Host *host;
198
199   /**
200    * The controller callback
201    */
202   GNUNET_TESTBED_ControllerCallback cc;
203
204   /**
205    * The closure for controller callback
206    */
207   void *cc_cls;
208
209   /**
210    * The configuration to use while connecting to controller
211    */
212   struct GNUNET_CONFIGURATION_Handle *cfg;
213
214   /**
215    * The message queue to the controller service
216    */
217   struct GNUNET_MQ_Handle *mq;
218
219   /**
220    * The host registration handle; NULL if no current registration requests are
221    * present
222    */
223   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
224
225   /**
226    * The map of active operation contexts
227    */
228   struct GNUNET_CONTAINER_MultiHashMap32 *opc_map;
229
230   /**
231    * If this callback is not NULL, schedule it as a task when opc_map gets empty
232    */
233   TESTBED_opcq_empty_cb opcq_empty_cb;
234
235   /**
236    * Closure for the above task
237    */
238   void *opcq_empty_cls;
239
240   /**
241    * Operation queue for simultaneous operations
242    */
243   struct OperationQueue *opq_parallel_operations;
244
245   /**
246    * Operation queue for simultaneous service connections
247    */
248   struct OperationQueue *opq_parallel_service_connections;
249
250   /**
251    * Operation queue for simultaneous topology configuration operations
252    */
253   struct OperationQueue *opq_parallel_topology_config_operations;
254
255   /**
256    * handle for hashtable of barrier handles, values are
257    * of type `struct GNUNET_TESTBED_Barrier`.
258    */
259   struct GNUNET_CONTAINER_MultiHashMap *barrier_map;
260
261   /**
262    * The controller event mask
263    */
264   uint64_t event_mask;
265
266   /**
267    * The operation id counter. use current value and increment
268    */
269   uint32_t operation_counter;
270
271 };
272
273
274 /**
275  * Handle for barrier
276  */
277 struct GNUNET_TESTBED_Barrier
278 {
279   /**
280    * hashcode identifying this barrier in the hashmap
281    */
282   struct GNUNET_HashCode key;
283
284   /**
285    * The controller handle given while initiliasing this barrier
286    */
287   struct GNUNET_TESTBED_Controller *c;
288
289   /**
290    * The name of the barrier
291    */
292   char *name;
293
294   /**
295    * The continuation callback to call when we have a status update on this
296    */
297   GNUNET_TESTBED_barrier_status_cb cb;
298
299   /**
300    * the closure for the above callback
301    */
302   void *cls;
303
304   /**
305    * Should the barrier crossed status message be echoed back to the controller?
306    */
307   int echo;
308 };
309
310
311
312 /**
313  * Queues a message in send queue for sending to the service
314  *
315  * @param controller the handle to the controller
316  * @param msg the message to queue
317  * @deprecated
318  */
319 void
320 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
321                                struct GNUNET_MessageHeader *msg);
322
323
324 /**
325  * Inserts the given operation context into the operation context map of the
326  * given controller.  Creates the operation context map if one does not exist
327  * for the controller
328  *
329  * @param c the controller
330  * @param opc the operation context to be inserted
331  */
332 void
333 GNUNET_TESTBED_insert_opc_ (struct GNUNET_TESTBED_Controller *c,
334                             struct OperationContext *opc);
335
336
337 /**
338  * Removes the given operation context from the operation context map of the
339  * given controller
340  *
341  * @param c the controller
342  * @param opc the operation context to remove
343  */
344 void
345 GNUNET_TESTBED_remove_opc_ (const struct GNUNET_TESTBED_Controller *c,
346                             struct OperationContext *opc);
347
348
349 /**
350  * Compresses given configuration using zlib compress
351  *
352  * @param config the serialized configuration
353  * @param size the size of config
354  * @param xconfig will be set to the compressed configuration (memory is fresly
355  *          allocated)
356  * @return the size of the xconfig
357  */
358 size_t
359 GNUNET_TESTBED_compress_config_ (const char *config,
360                                  size_t size,
361                                  char **xconfig);
362
363
364 /**
365  * Function to serialize and compress using zlib a configuration through a
366  * configuration handle
367  *
368  * @param cfg the configuration
369  * @param size the size of configuration when serialize.  Will be set on success.
370  * @param xsize the sizeo of the compressed configuration.  Will be set on success.
371  * @return the serialized and compressed configuration
372  */
373 char *
374 GNUNET_TESTBED_compress_cfg_ (const struct GNUNET_CONFIGURATION_Handle *cfg,
375                               size_t *size,
376                               size_t *xsize);
377
378
379 /**
380  * Creates a helper initialization message. This function is here because we
381  * want to use this in testing
382  *
383  * @param trusted_ip the ip address of the controller which will be set as TRUSTED
384  *          HOST(all connections form this ip are permitted by the testbed) when
385  *          starting testbed controller at host. This can either be a single ip
386  *          address or a network address in CIDR notation.
387  * @param hostname the hostname of the destination this message is intended for
388  * @param cfg the configuration that has to used to start the testbed service
389  *          thru helper
390  * @return the initialization message
391  */
392 struct GNUNET_TESTBED_HelperInit *
393 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
394                                         const char *hostname,
395                                         const struct GNUNET_CONFIGURATION_Handle *cfg);
396
397
398 /**
399  * Sends the given message as an operation. The given callback is called when a
400  * reply for the operation is available.  Call
401  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
402  * operation context if the cc hasn't been called
403  *
404  * @param controller the controller to which the message has to be sent
405  * @param operation_id the operation id of the message
406  * @param msg the message to send
407  * @param cc the callback to call when reply is available
408  * @param cc_cls the closure for the above callback
409  * @return the operation context which can be used to cancel the forwarded
410  *           operation
411  */
412 struct OperationContext *
413 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller *controller,
414                                        uint64_t operation_id,
415                                        const struct GNUNET_MessageHeader *msg,
416                                        GNUNET_MQ_MessageCallback cc,
417                                        void *cc_cls);
418
419 /**
420  * Function to cancel an operation created by simply forwarding an operation
421  * message.
422  *
423  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
424  */
425 void
426 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc);
427
428
429 /**
430  * Generates configuration by uncompressing configuration in given message. The
431  * given message should be of the following types:
432  * #GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
433  * #GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
434  *
435  * @param msg the message containing compressed configuration
436  * @return handle to the parsed configuration
437  */
438 struct GNUNET_CONFIGURATION_Handle *
439 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg);
440
441
442 /**
443  * Checks the integrity of the OpeationFailureEventMessage and if good returns
444  * the error message it contains.
445  *
446  * @param msg the OperationFailureEventMessage
447  * @return the error message
448  */
449 const char *
450 GNUNET_TESTBED_parse_error_string_ (const struct GNUNET_TESTBED_OperationFailureEventMessage *msg);
451
452
453 /**
454  * Function to return the operation id for a controller. The operation id is
455  * created from the controllers host id and its internal operation counter.
456  *
457  * @param controller the handle to the controller whose operation id has to be incremented
458  * @return the incremented operation id.
459  */
460 uint64_t
461 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller);
462
463
464 /**
465  * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
466  * check. Another difference is that this function takes the id of the slave
467  * host.
468  *
469  * @param op_cls the closure for the operation
470  * @param master the handle to master controller
471  * @param slave_host_id id of the host where the slave controller is running to
472  *          the slave_host should remain valid until this operation is cancelled
473  *          or marked as finished
474  * @return the operation handle;
475  */
476 struct GNUNET_TESTBED_Operation *
477 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
478                                   struct GNUNET_TESTBED_Controller *master,
479                                   uint32_t slave_host_id);
480
481
482
483 /**
484  * Initialise a barrier and call the given callback when the required percentage
485  * of peers (quorum) reach the barrier OR upon error.
486  *
487  * @param controller the handle to the controller
488  * @param name identification name of the barrier
489  * @param quorum the percentage of peers that is required to reach the barrier.
490  *   Peers signal reaching a barrier by calling
491  *   GNUNET_TESTBED_barrier_reached().
492  * @param cb the callback to call when the barrier is reached or upon error.
493  *   Cannot be NULL.
494  * @param cls closure for the above callback
495  * @param echo #GNUNET_YES to echo the barrier crossed status message back to the
496  *   controller
497  * @return barrier handle; NULL upon error
498  */
499 struct GNUNET_TESTBED_Barrier *
500 GNUNET_TESTBED_barrier_init_ (struct GNUNET_TESTBED_Controller *controller,
501                               const char *name,
502                               unsigned int quorum,
503                               GNUNET_TESTBED_barrier_status_cb cb,
504                               void *cls,
505                               int echo);
506
507
508 /**
509  * Remove a barrier and it was the last one in the barrier hash map, destroy the
510  * hash map
511  *
512  * @param barrier the barrier to remove
513  */
514 void
515 GNUNET_TESTBED_barrier_remove_ (struct GNUNET_TESTBED_Barrier *barrier);
516
517
518
519 #endif
520 /* end of testbed_api.h */