2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file regex/gnunet-regex-profiler.c
23 * @brief Regex profiler for testing distributed regex use.
31 #include "gnunet_applications.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_mesh_service.h"
34 #include "gnunet_stream_lib.h"
35 #include "gnunet_testbed_service.h"
39 * Total number of hosts.
44 * Number of peers per host.
46 #define PEER_PER_HOST 1
49 * Total number of peers.
51 #define TOTAL_PEERS (NUM_HOSTS * PEER_PER_HOST)
55 * Different states in test setup
65 * Connecting to slave controller
71 LINKING_SLAVES_SUCCESS,
82 * Event Mask for operation callbacks
87 * Testbed operation handle
89 static struct GNUNET_TESTBED_Operation *op[NUM_HOSTS];
94 static enum SetupState state[NUM_HOSTS];
99 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
107 * Hosts successfully registered
109 static unsigned int host_registered;
112 * Peers successfully started
114 static unsigned int peers_started;
117 * The master controller host
119 struct GNUNET_TESTBED_Host *master_host;
122 * The master controller process
124 static struct GNUNET_TESTBED_ControllerProc *master_proc;
127 * Handle to master controller
129 static struct GNUNET_TESTBED_Controller *master_ctrl;
132 * Slave host IP addresses
135 static char *slave_ips[NUM_HOSTS] = { "192.168.1.33", "192.168.1.34" };
140 struct GNUNET_TESTBED_Host *slave_hosts[NUM_HOSTS];
143 * Slave host registration handles
145 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
149 * Handle to global configuration
151 static struct GNUNET_CONFIGURATION_Handle *cfg;
154 * Structure for holding peer's handles.
159 * Handle to testbed peer.
161 struct GNUNET_TESTBED_Peer *peer;
164 * Peer's mesh handle.
166 struct GNUNET_MESH_Handle *mesh_handle;
169 * The service connect operation to stream
171 struct GNUNET_TESTBED_Operation *op;
176 enum SetupState state;
181 struct GNUNET_PeerIdentity our_id;
187 struct PeerData peers[TOTAL_PEERS];
192 * Close sockets and stop testing deamons nicely
195 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
199 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
200 GNUNET_SCHEDULER_cancel (abort_task);
202 for (i = 0; i < TOTAL_PEERS; i++)
204 if (NULL != peers[i].mesh_handle)
205 GNUNET_MESH_disconnect (peers[i].mesh_handle);
206 if (NULL != peers[i].op)
207 GNUNET_TESTBED_operation_done (peers[i].op);
210 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
215 * Something went wrong and timed out. Kill everything and set error flag.
218 * @param tc task context.
221 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
224 result = GNUNET_SYSERR;
230 * Method called whenever another peer has added us to a tunnel
231 * the other peer initiated.
232 * Only called (once) upon reception of data with a message type which was
233 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
234 * causes te tunnel to be ignored and no further notifications are sent about
238 * @param tunnel new handle to the tunnel
239 * @param initiator peer that started the tunnel
240 * @param atsi performance information for the tunnel
241 * @return initial tunnel context for the tunnel
242 * (can be NULL -- that's not an error)
245 mesh_inbound_tunnel_handler (void *cls,
246 struct GNUNET_MESH_Tunnel * tunnel,
247 const struct GNUNET_PeerIdentity * initiator,
248 const struct GNUNET_ATS_Information * atsi)
255 * Function called whenever an inbound tunnel is destroyed. Should clean up
256 * any associated state. This function is NOT called if the client has
257 * explicitly asked for the tunnel to be destroyed using
258 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
261 * @param cls closure (set from GNUNET_MESH_connect)
262 * @param tunnel connection to the other end (henceforth invalid)
263 * @param tunnel_ctx place where local state associated
264 * with the tunnel is stored
267 mesh_tunnel_end_handler (void *cls,
268 const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
275 * Mesh connect callback.
277 * @param cls internal peer id.
278 * @param op operation handle.
279 * @param ca_result connect adapter result.
280 * @param emsg error message.
283 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
284 void *ca_result, const char *emsg)
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect callback for peer %i\n",
300 * Mesh connect adapter.
302 * @param cls not used.
303 * @param cfg configuration handle.
308 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
310 struct PeerData *peer = (struct PeerData *)cls;
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect adapter\n");
314 static struct GNUNET_MESH_MessageHandler handlers[] = {
318 static GNUNET_MESH_ApplicationType apptypes[] = {
319 GNUNET_APPLICATION_TYPE_END
322 peer->mesh_handle = GNUNET_MESH_connect (cfg, cls,
323 &mesh_inbound_tunnel_handler,
324 &mesh_tunnel_end_handler, handlers, apptypes);
331 * Adapter function called to destroy a connection to
335 * @param op_result service handle returned from the connect adapter
338 mesh_da (void *cls, void *op_result)
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh disconnect adapter\n");
345 * Functions of this signature are called when a peer has been successfully
346 * started or stopped.
348 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
349 * @param emsg NULL on success; otherwise an error description
352 peer_start_cb (void *cls, const char *emsg)
357 GNUNET_TESTBED_operation_done (op[i]);
359 // FIXME create and start rest of PEERS_PER_HOST
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u peer(s) started\n", peers_started);
362 if (TOTAL_PEERS == peers_started)
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started.\n");
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Linking slave controllers\n");
367 for (cnt = 0; cnt < NUM_HOSTS - 1; cnt++)
369 state[cnt] = LINKING_SLAVES;
371 GNUNET_TESTBED_get_slave_config ((void *) (long) cnt, master_ctrl,
372 slave_hosts[cnt + 1]);
379 * Functions of this signature are called when a peer has been successfully
382 * @param cls the closure from GNUNET_TESTBED_peer_create()
383 * @param peer the handle for the created peer; NULL on any error during
385 * @param emsg NULL if peer is not NULL; else MAY contain the error description
388 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
393 // GNUNET_TESTBED_operation_done(op[i]);
394 peer_id = i; // FIXME A * i + B
395 peers[peer_id].peer = peer;
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Peer %i created\n", peer_id);
397 op[i] = GNUNET_TESTBED_peer_start (NULL, peer, peer_start_cb, (void *) i);
402 * Signature of the event handler function called by the
403 * respective event controller.
406 * @param event information about the event
409 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
415 case GNUNET_TESTBED_ET_PEER_START:
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Peer started\n");
417 /* event->details.peer_start.peer; */
418 /* event->details.peer_start.host; */
421 case GNUNET_TESTBED_ET_PEER_STOP:
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer stopped\n");
424 case GNUNET_TESTBED_ET_CONNECT:
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Overlay Connected\n");
426 for (i = 0; i < TOTAL_PEERS; i++)
428 GNUNET_TESTBED_service_connect (NULL, peers[i].peer, "mesh", &mesh_connect_cb,
429 (void *) i, &mesh_ca, &mesh_da, NULL);
432 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
433 if (NULL != event->details.operation_finished.emsg)
435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testbed error: %s\n",
436 event->details.operation_finished.emsg);
440 i = (long) event->details.operation_finished.op_cls;
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Init: %u\n", i);
445 GNUNET_TESTBED_operation_done (event->details.
446 operation_finished.operation);
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
451 GNUNET_TESTBED_operation_done (event->details.
452 operation_finished.operation);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Linked host %i\n", i);
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating peer...\n");
458 state[i] = CREATING_PEER;
460 GNUNET_TESTBED_peer_create (master_ctrl, slave_hosts[i], cfg,
461 peer_create_cb, (void *) i);
465 GNUNET_TESTBED_operation_done (event->details.
466 operation_finished.operation);
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Peer create\n");
474 struct GNUNET_CONFIGURATION_Handle *slave_cfg;
476 GNUNET_assert (NULL != event->details.operation_finished.generic);
478 GNUNET_CONFIGURATION_dup ((struct GNUNET_CONFIGURATION_Handle *)
479 event->details.operation_finished.generic);
480 GNUNET_TESTBED_operation_done (event->details.
481 operation_finished.operation);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
484 state[i] = LINKING_SLAVES_SUCCESS;
486 GNUNET_TESTBED_controller_link ((void *) (long) i, master_ctrl,
487 slave_hosts[i + 1], slave_hosts[i],
488 slave_cfg, GNUNET_NO);
489 GNUNET_CONFIGURATION_destroy (slave_cfg);
492 case LINKING_SLAVES_SUCCESS:
494 unsigned int peer_cnt;
495 struct GNUNET_TESTBED_Peer *peer_handles[TOTAL_PEERS];
497 GNUNET_TESTBED_operation_done (event->details.
498 operation_finished.operation);
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Linking slave %i succeeded\n", i);
502 state[0] = CONNECTING_PEERS;
504 for (peer_cnt = 0; peer_cnt < TOTAL_PEERS; peer_cnt++)
506 peer_handles[peer_cnt] = peers[peer_cnt].peer;
509 GNUNET_TESTBED_overlay_configure_topology (NULL, TOTAL_PEERS, peer_handles,
510 GNUNET_TESTBED_TOPOLOGY_LINE);
511 GNUNET_assert (NULL != op[0]);
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peers...\n");
515 case CONNECTING_PEERS:
517 GNUNET_TESTBED_operation_done (event->details.
518 operation_finished.operation);
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
535 * Callback which will be called to after a host registration succeeded or
536 * failed. Registration of all slave hosts is continued and linking of the hosts
539 * @param cls not used.
540 * @param emsg the error message; NULL if host registration is successful.
543 registration_cont (void *cls, const char *emsg)
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
550 state[host_registered] = LINKING;
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Linking host %u\n", host_registered);
552 op[host_registered] =
553 GNUNET_TESTBED_controller_link ((void *) (long) host_registered,
554 master_ctrl, slave_hosts[host_registered],
555 NULL, cfg, GNUNET_YES);
557 if (NUM_HOSTS != host_registered)
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Registering host %u\n",
561 rh = GNUNET_TESTBED_register_host (master_ctrl,
562 slave_hosts[host_registered],
563 ®istration_cont, NULL);
570 * Callback to signal successfull startup of the controller process. If the
571 * startup was successfull the master controller and all slave hosts are
572 * created. Registering the slave hosts is started and continued in
575 * @param cls not used.
576 * @param config the configuration with which the controller has been started;
577 * NULL if status is not GNUNET_OK
578 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
579 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
582 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
587 if (NULL == config || GNUNET_OK != status)
591 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
592 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
593 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
594 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to master controller\n");
597 GNUNET_TESTBED_controller_connect (config, master_host, event_mask,
598 &controller_cb, NULL);
599 GNUNET_assert (NULL != master_ctrl);
601 for (i = 0; i < NUM_HOSTS; i++)
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating host %u\n", i);
604 slave_hosts[i] = GNUNET_TESTBED_host_create (slave_ips[i], NULL, 0);
605 GNUNET_assert (NULL != slave_hosts[i]);
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Registering host %u\n",
610 rh = GNUNET_TESTBED_register_host (master_ctrl, slave_hosts[0],
611 ®istration_cont, NULL);
612 GNUNET_assert (NULL != rh);
619 * @param cls not used.
620 * @param args arguments passed to GNUNET_PROGRAM_run
621 * @param cfgfile the path to configuration file
622 * @param config the configuration file handle
625 run (void *cls, char *const *args, const char *cfgfile,
626 const struct GNUNET_CONFIGURATION_Handle *config)
628 master_host = GNUNET_TESTBED_host_create ("192.168.1.33", NULL, 0);
629 GNUNET_assert (NULL != master_host);
630 cfg = GNUNET_CONFIGURATION_dup (config);
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting master controller\n");
633 GNUNET_TESTBED_controller_start ("192.168.1.33", master_host, cfg,
636 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
637 (GNUNET_TIME_UNIT_MINUTES, 60), &do_abort,
643 * Main function for profiling the regex/mesh implementation. Checks if all ssh
644 * connections to each of the hosts in 'slave_ips' is possible before setting up
647 * @param argc argument count.
648 * @param argv argument values.
650 * @return 0 on success.
653 main (int argc, char **argv)
659 struct GNUNET_GETOPT_CommandLineOption options[] = {
660 GNUNET_GETOPT_OPTION_END
662 char *const argv2[] = { "gnunet-regex-profiler",
663 "-c", "regex_profiler_test.conf",
667 test_hosts = GNUNET_OK;
668 for (i = 0; i < NUM_HOSTS; i++)
670 char *const remote_args[] = {
671 "ssh", "-o", "BatchMode=yes", slave_ips[i],
672 "gnunet-helper-testbed --help > /dev/null", NULL
674 struct GNUNET_OS_Process *auxp;
675 enum GNUNET_OS_ProcessStatusType type;
678 fprintf (stderr, "Testing host %i\n", i);
680 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
681 NULL, "ssh", remote_args);
682 GNUNET_assert (NULL != auxp);
685 ret = GNUNET_OS_process_status (auxp, &type, &code);
686 GNUNET_assert (GNUNET_SYSERR != ret);
689 while (GNUNET_NO == ret);
690 (void) GNUNET_OS_process_wait (auxp);
691 GNUNET_OS_process_destroy (auxp);
695 "Unable to run the test as this system is not configured "
696 "to use password less SSH logins to host %s.\n", slave_ips[i]);
697 test_hosts = GNUNET_SYSERR;
700 if (test_hosts != GNUNET_OK)
702 fprintf (stderr, "Some hosts have failed the ssh check. Exiting.\n");
705 fprintf (stderr, "START.\n");
707 result = GNUNET_SYSERR;
710 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
711 "gnunet-regex-profiler", "nohelp", options, &run, NULL);
713 fprintf (stderr, "END.\n");
715 if (GNUNET_SYSERR == result || GNUNET_OK != ret)