testbed_api.h 13 KB

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