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;
133 * Slave host registration handles
135 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
139 * Handle to global configuration
141 static struct GNUNET_CONFIGURATION_Handle *cfg;
144 * Structure for storing host handles
149 * IP address of this host.
156 struct GNUNET_TESTBED_Host *host;
159 * Registration state of this host.
166 struct GNUNET_TESTBED_Operation *op;
171 enum SetupState state;
177 static struct Host slaves[NUM_HOSTS] = { {"192.168.1.33", NULL, 0, NULL, INIT},
178 {"192.168.1.34", NULL, 0, NULL, INIT}
182 * Structure for holding peer's handles.
187 * Handle to testbed peer.
189 struct GNUNET_TESTBED_Peer *peer;
192 * Peer's mesh handle.
194 struct GNUNET_MESH_Handle *mesh_handle;
197 * The service connect operation to stream
199 struct GNUNET_TESTBED_Operation *op;
204 enum SetupState state;
209 struct GNUNET_PeerIdentity our_id;
215 struct PeerData peers[TOTAL_PEERS];
220 * Close sockets and stop testing deamons nicely
223 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
228 GNUNET_SCHEDULER_cancel (abort_task);
230 for (i = 0; i < TOTAL_PEERS; i++)
232 if (NULL != peers[i].mesh_handle)
233 GNUNET_MESH_disconnect (peers[i].mesh_handle);
234 if (NULL != peers[i].op)
235 GNUNET_TESTBED_operation_done (peers[i].op);
238 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
243 * Something went wrong and timed out. Kill everything and set error flag.
246 * @param tc task context.
249 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
252 result = GNUNET_SYSERR;
258 * Method called whenever another peer has added us to a tunnel
259 * the other peer initiated.
260 * Only called (once) upon reception of data with a message type which was
261 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
262 * causes te tunnel to be ignored and no further notifications are sent about
266 * @param tunnel new handle to the tunnel
267 * @param initiator peer that started the tunnel
268 * @param atsi performance information for the tunnel
269 * @return initial tunnel context for the tunnel
270 * (can be NULL -- that's not an error)
273 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
274 const struct GNUNET_PeerIdentity *initiator,
275 const struct GNUNET_ATS_Information *atsi)
282 * Function called whenever an inbound tunnel is destroyed. Should clean up
283 * any associated state. This function is NOT called if the client has
284 * explicitly asked for the tunnel to be destroyed using
285 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
288 * @param cls closure (set from GNUNET_MESH_connect)
289 * @param tunnel connection to the other end (henceforth invalid)
290 * @param tunnel_ctx place where local state associated
291 * with the tunnel is stored
294 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
302 * Mesh connect callback.
304 * @param cls internal peer id.
305 * @param op operation handle.
306 * @param ca_result connect adapter result.
307 * @param emsg error message.
310 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
311 void *ca_result, const char *emsg)
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect callback for peer %i\n",
327 * Mesh connect adapter.
329 * @param cls not used.
330 * @param cfg configuration handle.
335 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
337 struct PeerData *peer = (struct PeerData *) cls;
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect adapter\n");
341 static struct GNUNET_MESH_MessageHandler handlers[] = {
345 static GNUNET_MESH_ApplicationType apptypes[] = {
346 GNUNET_APPLICATION_TYPE_END
350 GNUNET_MESH_connect (cfg, cls, &mesh_inbound_tunnel_handler,
351 &mesh_tunnel_end_handler, handlers, apptypes);
358 * Adapter function called to destroy a connection to
362 * @param op_result service handle returned from the connect adapter
365 mesh_da (void *cls, void *op_result)
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh disconnect adapter\n");
372 * Functions of this signature are called when a peer has been successfully
373 * started or stopped.
375 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
376 * @param emsg NULL on success; otherwise an error description
379 peer_start_cb (void *cls, const char *emsg)
384 GNUNET_TESTBED_operation_done (op[i]);
386 // FIXME create and start rest of PEERS_PER_HOST
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u peer(s) started\n", peers_started);
389 if (TOTAL_PEERS == peers_started)
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started.\n");
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Linking slave controllers\n");
394 for (cnt = 0; cnt < NUM_HOSTS - 1; cnt++)
396 state[cnt] = LINKING_SLAVES;
398 GNUNET_TESTBED_get_slave_config ((void *) (long) cnt, master_ctrl,
399 slaves[cnt + 1].host);
406 * Functions of this signature are called when a peer has been successfully
409 * @param cls the closure from GNUNET_TESTBED_peer_create()
410 * @param peer the handle for the created peer; NULL on any error during
412 * @param emsg NULL if peer is not NULL; else MAY contain the error description
415 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
420 // GNUNET_TESTBED_operation_done(op[i]);
421 peer_id = i; // FIXME A * i + B
422 peers[peer_id].peer = peer;
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Peer %i created\n", peer_id);
424 op[i] = GNUNET_TESTBED_peer_start (NULL, peer, peer_start_cb, (void *) i);
429 * Signature of the event handler function called by the
430 * respective event controller.
433 * @param event information about the event
436 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
442 case GNUNET_TESTBED_ET_PEER_START:
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Peer started\n");
444 /* event->details.peer_start.peer; */
445 /* event->details.peer_start.host; */
448 case GNUNET_TESTBED_ET_PEER_STOP:
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer stopped\n");
451 case GNUNET_TESTBED_ET_CONNECT:
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Overlay Connected\n");
453 for (i = 0; i < TOTAL_PEERS; i++)
455 GNUNET_TESTBED_service_connect (NULL, peers[i].peer, "mesh",
456 &mesh_connect_cb, (void *) i, &mesh_ca,
460 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
461 if (NULL != event->details.operation_finished.emsg)
463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testbed error: %s\n",
464 event->details.operation_finished.emsg);
468 i = (long) event->details.operation_finished.op_cls;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Init: %u\n", i);
474 GNUNET_TESTBED_operation_done (event->details.
475 operation_finished.operation);
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
482 GNUNET_assert (NULL != slaves[i].op);
484 GNUNET_TESTBED_operation_done (slaves[i].op);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Linked host %i\n", i);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating peer...\n");
491 state[i] = CREATING_PEER;
493 GNUNET_TESTBED_peer_create (master_ctrl, slaves[i].host, cfg,
494 peer_create_cb, (void *) i);
499 GNUNET_TESTBED_operation_done (event->details.
500 operation_finished.operation);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Peer create\n");
508 struct GNUNET_CONFIGURATION_Handle *slave_cfg;
510 GNUNET_assert (NULL != event->details.operation_finished.generic);
512 GNUNET_CONFIGURATION_dup ((struct GNUNET_CONFIGURATION_Handle *)
513 event->details.operation_finished.generic);
514 GNUNET_TESTBED_operation_done (event->details.
515 operation_finished.operation);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
518 state[i] = LINKING_SLAVES_SUCCESS;
520 GNUNET_TESTBED_controller_link ((void *) (long) i, master_ctrl,
521 slaves[i + 1].host, slaves[i].host,
522 slave_cfg, GNUNET_NO);
523 GNUNET_CONFIGURATION_destroy (slave_cfg);
526 case LINKING_SLAVES_SUCCESS:
528 unsigned int peer_cnt;
529 struct GNUNET_TESTBED_Peer *peer_handles[TOTAL_PEERS];
531 GNUNET_TESTBED_operation_done (event->details.
532 operation_finished.operation);
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Linking slave %i succeeded\n", i);
536 state[0] = CONNECTING_PEERS;
538 for (peer_cnt = 0; peer_cnt < TOTAL_PEERS; peer_cnt++)
540 peer_handles[peer_cnt] = peers[peer_cnt].peer;
543 GNUNET_TESTBED_overlay_configure_topology (NULL, TOTAL_PEERS,
545 GNUNET_TESTBED_TOPOLOGY_LINE);
546 GNUNET_assert (NULL != op[0]);
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting peers...\n");
550 case CONNECTING_PEERS:
552 GNUNET_TESTBED_operation_done (event->details.
553 operation_finished.operation);
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Operation %u finished\n", i);
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
570 * Callback which will be called to after a host registration succeeded or
571 * failed. Registration of all slave hosts is continued and linking of the hosts
574 * @param cls not used.
575 * @param emsg the error message; NULL if host registration is successful.
578 registration_cont (void *cls, const char *emsg)
580 struct Host *slave = (struct Host *) cls;
582 if (NULL != emsg || NULL == slave)
584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
588 state[host_registered] = LINKING;
589 slave->state = LINKING;
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Linking host %u\n", host_registered);
593 GNUNET_TESTBED_controller_link ((void *) (long) host_registered,
594 master_ctrl, slave->host, NULL, cfg,
597 if (NUM_HOSTS != host_registered)
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Registering host %u with ip %s\n",
600 host_registered, slaves[host_registered].ip);
601 rh = GNUNET_TESTBED_register_host (master_ctrl,
602 slaves[host_registered].host,
604 &slaves[host_registered]);
611 * Callback to signal successfull startup of the controller process. If the
612 * startup was successfull the master controller and all slave hosts are
613 * created. Registering the slave hosts is started and continued in
616 * @param cls not used.
617 * @param config the configuration with which the controller has been started;
618 * NULL if status is not GNUNET_OK
619 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
620 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
623 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
628 if (NULL == config || GNUNET_OK != status)
632 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
633 event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
634 event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
635 event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to master controller\n");
638 GNUNET_TESTBED_controller_connect (config, master_host, event_mask,
639 &controller_cb, NULL);
640 GNUNET_assert (NULL != master_ctrl);
642 for (i = 0; i < NUM_HOSTS; i++)
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating host %u with ip %s\n", i,
646 slaves[i].host = GNUNET_TESTBED_host_create (slaves[i].ip, NULL, 0);
647 GNUNET_assert (NULL != slaves[i].host);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Registering host %u with ip %s\n",
651 host_registered, slaves[0].ip);
652 rh = GNUNET_TESTBED_register_host (master_ctrl, slaves[0].host,
653 ®istration_cont, &slaves[0]);
654 GNUNET_assert (NULL != rh);
661 * @param cls not used.
662 * @param args arguments passed to GNUNET_PROGRAM_run
663 * @param cfgfile the path to configuration file
664 * @param config the configuration file handle
667 run (void *cls, char *const *args, const char *cfgfile,
668 const struct GNUNET_CONFIGURATION_Handle *config)
670 master_host = GNUNET_TESTBED_host_create ("192.168.1.33", NULL, 0);
671 GNUNET_assert (NULL != master_host);
672 cfg = GNUNET_CONFIGURATION_dup (config);
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting master controller\n");
675 GNUNET_TESTBED_controller_start ("192.168.1.33", master_host, cfg,
678 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
679 (GNUNET_TIME_UNIT_MINUTES, 60), &do_abort,
685 * Main function for profiling the regex/mesh implementation. Checks if all ssh
686 * connections to each of the hosts in 'slave_ips' is possible before setting up
689 * @param argc argument count.
690 * @param argv argument values.
692 * @return 0 on success.
695 main (int argc, char **argv)
701 struct GNUNET_GETOPT_CommandLineOption options[] = {
702 GNUNET_GETOPT_OPTION_END
704 char *const argv2[] = { "gnunet-regex-profiler",
705 "-c", "regex_profiler_test.conf",
709 test_hosts = GNUNET_OK;
710 for (i = 0; i < NUM_HOSTS; i++)
712 char *const remote_args[] = {
713 "ssh", "-o", "BatchMode=yes", slaves[i].ip,
714 "gnunet-helper-testbed --help > /dev/null", NULL
716 struct GNUNET_OS_Process *auxp;
717 enum GNUNET_OS_ProcessStatusType type;
720 fprintf (stderr, "Testing host %i\n", i);
722 GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
723 NULL, "ssh", remote_args);
724 GNUNET_assert (NULL != auxp);
727 ret = GNUNET_OS_process_status (auxp, &type, &code);
728 GNUNET_assert (GNUNET_SYSERR != ret);
731 while (GNUNET_NO == ret);
732 (void) GNUNET_OS_process_wait (auxp);
733 GNUNET_OS_process_destroy (auxp);
737 "Unable to run the test as this system is not configured "
738 "to use password less SSH logins to host %s.\n", slaves[i].ip);
739 test_hosts = GNUNET_SYSERR;
742 if (test_hosts != GNUNET_OK)
744 fprintf (stderr, "Some hosts have failed the ssh check. Exiting.\n");
747 fprintf (stderr, "START.\n");
749 result = GNUNET_SYSERR;
752 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
753 "gnunet-regex-profiler", "nohelp", options, &run,
756 fprintf (stderr, "END.\n");
758 if (GNUNET_SYSERR == result || GNUNET_OK != ret)