doxygen
[oweals/gnunet.git] / src / regex / gnunet-regex-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file regex/gnunet-regex-profiler.c
23  * @brief Regex profiler for testing distributed regex use.
24  * @author Bart Polot
25  * @author Max Szengel
26  */
27
28 #include <string.h>
29
30 #include "platform.h"
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"
36
37
38 /**
39  * Total number of hosts.
40  */
41 #define NUM_HOSTS 2
42
43 /**
44  * Number of peers per host.
45  */
46 #define PEER_PER_HOST 1
47
48 /**
49  * Total number of peers.
50  */
51 #define TOTAL_PEERS (NUM_HOSTS * PEER_PER_HOST)
52
53
54 /**
55  * Different states in test setup
56  */
57 enum SetupState
58 {
59   /**
60    * The initial state
61    */
62   INIT,
63
64   /**
65    * Connecting to slave controller
66    */
67   LINKING,
68
69   LINKING_SLAVES,
70
71   LINKING_SLAVES_SUCCESS,
72
73   CONNECTING_PEERS,
74
75   CREATING_PEER,
76
77   STARTING_PEER
78 };
79
80
81 /**
82  * Event Mask for operation callbacks
83  */
84 uint64_t event_mask;
85
86 /**
87  * Testbed operation handle
88  */
89 static struct GNUNET_TESTBED_Operation *op[NUM_HOSTS];
90
91 /**
92  * Setup state.
93  */
94 static enum SetupState state[NUM_HOSTS];
95
96 /**
97  * Abort task.
98  */
99 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
100
101 /**
102  * Global test result
103  */
104 static int result;
105
106 /**
107  * Hosts successfully registered
108  */
109 static unsigned int host_registered;
110
111 /**
112  * Peers successfully started
113  */
114 static unsigned int peers_started;
115
116 /**
117  * The master controller host
118  */
119 struct GNUNET_TESTBED_Host *master_host;
120
121 /**
122  * The master controller process
123  */
124 static struct GNUNET_TESTBED_ControllerProc *master_proc;
125
126 /**
127  * Handle to master controller
128  */
129 static struct GNUNET_TESTBED_Controller *master_ctrl;
130
131 /**
132  * Slave host IP addresses
133  */
134
135 static char *slave_ips[NUM_HOSTS] = { "192.168.1.33", "192.168.1.34" };
136
137 /**
138  * The slave hosts
139  */
140 struct GNUNET_TESTBED_Host *slave_hosts[NUM_HOSTS];
141
142 /**
143  * Slave host registration handles
144  */
145 static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
146
147
148 /**
149  * Handle to global configuration
150  */
151 static struct GNUNET_CONFIGURATION_Handle *cfg;
152
153 /**
154  * Structure for holding peer's handles.
155  */
156 struct PeerData
157 {
158   /**
159    * Handle to testbed peer.
160    */
161   struct GNUNET_TESTBED_Peer *peer;
162
163   /**
164    * Peer's mesh handle.
165    */
166   struct GNUNET_MESH_Handle *mesh_handle;
167
168   /**
169    * The service connect operation to stream
170    */
171   struct GNUNET_TESTBED_Operation *op;
172
173   /**
174    * Peer setup state.
175    */
176   enum SetupState state;
177
178   /**
179    * Our Peer id
180    */
181   struct GNUNET_PeerIdentity our_id;
182 };
183
184 /**
185  * The peers
186  */
187 struct PeerData peers[TOTAL_PEERS];
188
189
190
191 /**
192  * Close sockets and stop testing deamons nicely
193  */
194 void
195 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 {
197   unsigned int i;
198
199   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
200     GNUNET_SCHEDULER_cancel (abort_task);
201
202   for (i = 0; i < TOTAL_PEERS; i++)
203   {
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);
208   }
209
210   GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
211 }
212
213
214 /**
215  * Something went wrong and timed out. Kill everything and set error flag.
216  *
217  * @param cls close.
218  * @param tc task context.
219  */
220 static void
221 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 {
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
224   result = GNUNET_SYSERR;
225   abort_task = 0;
226 }
227
228
229 /**
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
235  * the same tunnel.
236  *
237  * @param cls closure
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)
243  */
244 void *
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)
249 {
250   return NULL;
251 }
252
253
254 /**
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
259  * the tunnel.
260  *
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
265  */
266 void
267 mesh_tunnel_end_handler (void *cls,
268                          const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
269 {
270
271 }
272
273
274 /**
275  * Mesh connect callback.
276  *
277  * @param cls internal peer id.
278  * @param op operation handle.
279  * @param ca_result connect adapter result.
280  * @param emsg error message.
281  */
282 void
283 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
284                  void *ca_result, const char *emsg)
285 {
286   long i = (long) cls;
287
288   if (NULL != emsg)
289   {
290     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
291     GNUNET_assert (0);
292   }
293
294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect callback for peer %i\n",
295               i);
296 }
297
298
299 /**
300  * Mesh connect adapter.
301  *
302  * @param cls not used.
303  * @param cfg configuration handle.
304  *
305  * @return
306  */
307 void *
308 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
309 {
310   struct PeerData *peer = (struct PeerData *)cls;
311
312   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect adapter\n");
313
314   static struct GNUNET_MESH_MessageHandler handlers[] = {
315     {NULL, 0, 0}
316   };
317
318   static GNUNET_MESH_ApplicationType apptypes[] = {
319     GNUNET_APPLICATION_TYPE_END
320   };
321
322   peer->mesh_handle = GNUNET_MESH_connect (cfg, cls,
323                                            &mesh_inbound_tunnel_handler,
324                                            &mesh_tunnel_end_handler, handlers, apptypes);
325
326   return NULL;
327 }
328
329
330 /**
331  * Adapter function called to destroy a connection to
332  * the mesh service
333  *
334  * @param cls closure
335  * @param op_result service handle returned from the connect adapter
336  */
337 void
338 mesh_da (void *cls, void *op_result)
339 {
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh disconnect adapter\n");
341 }
342
343
344 /**
345  * Functions of this signature are called when a peer has been successfully
346  * started or stopped.
347  *
348  * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
349  * @param emsg NULL on success; otherwise an error description
350  */
351 static void
352 peer_start_cb (void *cls, const char *emsg)
353 {
354   unsigned int cnt;
355   long i = (long) cls;
356
357   GNUNET_TESTBED_operation_done (op[i]);
358   peers_started++;
359   // FIXME create and start rest of PEERS_PER_HOST
360   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u peer(s) started\n", peers_started);
361
362   if (TOTAL_PEERS == peers_started)
363   {
364     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers started.\n");
365     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Linking slave controllers\n");
366
367     for (cnt = 0; cnt < NUM_HOSTS - 1; cnt++)
368     {
369       state[cnt] = LINKING_SLAVES;
370       op[cnt] =
371           GNUNET_TESTBED_get_slave_config ((void *) (long) cnt, master_ctrl,
372                                            slave_hosts[cnt + 1]);
373     }
374   }
375 }
376
377
378 /**
379  * Functions of this signature are called when a peer has been successfully
380  * created
381  *
382  * @param cls the closure from GNUNET_TESTBED_peer_create()
383  * @param peer the handle for the created peer; NULL on any error during
384  *          creation
385  * @param emsg NULL if peer is not NULL; else MAY contain the error description
386  */
387 static void
388 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
389 {
390   long i = (long) cls;
391   long peer_id;
392
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);
398 }
399
400
401 /**
402  * Signature of the event handler function called by the
403  * respective event controller.
404  *
405  * @param cls closure
406  * @param event information about the event
407  */
408 static void
409 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
410 {
411   long i;
412
413   switch (event->type)
414   {
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; */
419
420     break;
421   case GNUNET_TESTBED_ET_PEER_STOP:
422     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer stopped\n");
423     break;
424   case GNUNET_TESTBED_ET_CONNECT:
425     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Overlay Connected\n");
426     for (i = 0; i < TOTAL_PEERS; i++)
427     {
428       GNUNET_TESTBED_service_connect (NULL, peers[i].peer, "mesh", &mesh_connect_cb,
429                                       (void *) i, &mesh_ca, &mesh_da, NULL);
430     }
431     break;
432   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
433     if (NULL != event->details.operation_finished.emsg)
434     {
435       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testbed error: %s\n",
436                   event->details.operation_finished.emsg);
437       GNUNET_assert (0);
438     }
439
440     i = (long) event->details.operation_finished.op_cls;
441     switch (state[i])
442     {
443     case INIT:
444       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Init: %u\n", i);
445       GNUNET_TESTBED_operation_done (event->details.
446                                      operation_finished.operation);
447       op[i] = NULL;
448       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Operation %u finished\n", i);
449       break;
450     case LINKING:
451       GNUNET_TESTBED_operation_done (event->details.
452                                      operation_finished.operation);
453       op[i] = NULL;
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");
457
458       state[i] = CREATING_PEER;
459       op[i] =
460           GNUNET_TESTBED_peer_create (master_ctrl, slave_hosts[i], cfg,
461                                       peer_create_cb, (void *) i);
462       break;
463     case CREATING_PEER:
464     {
465       GNUNET_TESTBED_operation_done (event->details.
466                                      operation_finished.operation);
467       op[i] = NULL;
468       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Operation %u finished\n", i);
469       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Peer create\n");
470       break;
471     }
472     case LINKING_SLAVES:
473     {
474       struct GNUNET_CONFIGURATION_Handle *slave_cfg;
475
476       GNUNET_assert (NULL != event->details.operation_finished.generic);
477       slave_cfg =
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);
482       op[i] = NULL;
483       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Operation %u finished\n", i);
484       state[i] = LINKING_SLAVES_SUCCESS;
485       op[i] =
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);
490       break;
491     }
492     case LINKING_SLAVES_SUCCESS:
493     {
494       unsigned int peer_cnt;
495       struct GNUNET_TESTBED_Peer *peer_handles[TOTAL_PEERS];
496
497       GNUNET_TESTBED_operation_done (event->details.
498                                      operation_finished.operation);
499       op[i] = NULL;
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;
503
504       for (peer_cnt = 0; peer_cnt < TOTAL_PEERS; peer_cnt++)
505       {
506         peer_handles[peer_cnt] = peers[peer_cnt].peer;
507       }
508       op[0] =
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");
513       break;
514     }
515     case CONNECTING_PEERS:
516     {
517       GNUNET_TESTBED_operation_done (event->details.
518                                      operation_finished.operation);
519       op[i] = NULL;
520       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  Operation %u finished\n", i);
521       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
522       break;
523     }
524     default:
525       GNUNET_break (0);
526     }
527     break;
528   default:
529     GNUNET_break (0);
530   }
531 }
532
533
534 /**
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
537  * is started.
538  *
539  * @param cls not used.
540  * @param emsg the error message; NULL if host registration is successful.
541  */
542 static void
543 registration_cont (void *cls, const char *emsg)
544 {
545   if (NULL != emsg)
546   {
547     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", emsg);
548     GNUNET_assert (0);
549   }
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);
556   host_registered++;
557   if (NUM_HOSTS != host_registered)
558   {
559     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Registering host %u\n",
560                 host_registered);
561     rh = GNUNET_TESTBED_register_host (master_ctrl,
562                                        slave_hosts[host_registered],
563                                        &registration_cont, NULL);
564     return;
565   }
566 }
567
568
569 /**
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
573  * registration_cont.
574  *
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
580  */
581 static void
582 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
583            int status)
584 {
585   unsigned int i;
586
587   if (NULL == config || GNUNET_OK != status)
588     return;
589
590   event_mask = 0;
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");
596   master_ctrl =
597       GNUNET_TESTBED_controller_connect (config, master_host, event_mask,
598                                          &controller_cb, NULL);
599   GNUNET_assert (NULL != master_ctrl);
600
601   for (i = 0; i < NUM_HOSTS; i++)
602   {
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]);
606   }
607   host_registered = 0;
608   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Registering host %u\n",
609               host_registered);
610   rh = GNUNET_TESTBED_register_host (master_ctrl, slave_hosts[0],
611                                      &registration_cont, NULL);
612   GNUNET_assert (NULL != rh);
613 }
614
615
616 /**
617  * Main run function.
618  *
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
623  */
624 static void
625 run (void *cls, char *const *args, const char *cfgfile,
626      const struct GNUNET_CONFIGURATION_Handle *config)
627 {
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");
632   master_proc =
633       GNUNET_TESTBED_controller_start ("192.168.1.33", master_host, cfg,
634                                        status_cb, NULL);
635   abort_task =
636       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
637                                     (GNUNET_TIME_UNIT_MINUTES, 60), &do_abort,
638                                     NULL);
639 }
640
641
642 /**
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
645  * the testbed.
646  *
647  * @param argc argument count.
648  * @param argv argument values.
649  *
650  * @return 0 on success.
651  */
652 int
653 main (int argc, char **argv)
654 {
655   int ret;
656   int test_hosts;
657   unsigned int i;
658
659   struct GNUNET_GETOPT_CommandLineOption options[] = {
660     GNUNET_GETOPT_OPTION_END
661   };
662   char *const argv2[] = { "gnunet-regex-profiler",
663     "-c", "regex_profiler_test.conf",
664     NULL
665   };
666
667   test_hosts = GNUNET_OK;
668   for (i = 0; i < NUM_HOSTS; i++)
669   {
670     char *const remote_args[] = {
671       "ssh", "-o", "BatchMode=yes", slave_ips[i],
672       "gnunet-helper-testbed --help > /dev/null", NULL
673     };
674     struct GNUNET_OS_Process *auxp;
675     enum GNUNET_OS_ProcessStatusType type;
676     unsigned long code;
677
678     fprintf (stderr, "Testing host %i\n", i);
679     auxp =
680         GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
681                                      NULL, "ssh", remote_args);
682     GNUNET_assert (NULL != auxp);
683     do
684     {
685       ret = GNUNET_OS_process_status (auxp, &type, &code);
686       GNUNET_assert (GNUNET_SYSERR != ret);
687       (void) usleep (300);
688     }
689     while (GNUNET_NO == ret);
690     (void) GNUNET_OS_process_wait (auxp);
691     GNUNET_OS_process_destroy (auxp);
692     if (0 != code)
693     {
694       fprintf (stderr,
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;
698     }
699   }
700   if (test_hosts != GNUNET_OK)
701   {
702     fprintf (stderr, "Some hosts have failed the ssh check. Exiting.\n");
703     return 1;
704   }
705   fprintf (stderr, "START.\n");
706
707   result = GNUNET_SYSERR;
708
709   ret =
710       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
711                           "gnunet-regex-profiler", "nohelp", options, &run, NULL);
712
713   fprintf (stderr, "END.\n");
714
715   if (GNUNET_SYSERR == result || GNUNET_OK != ret)
716     return 1;
717   return 0;
718 }