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