sensor: towards profiler
[oweals/gnunet.git] / src / sensor / gnunet-sensor-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C)
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  * TODO:
23  * - Run X peers
24  * - Rewrite interval time (optional)
25  * - Run 1 dashboard
26  * - Monitor dashboard records
27  * - Prompt for anomalies when ready:
28  *  -- Cut Y peers (remove their connections to other X-Y peers but not the connections among themselves)
29  */
30
31 /**
32  * @file sensor/gnunet-sensor-profiler.c
33  * @brief Profiler for the sensor service
34  * @author Omar Tarabai
35  */
36 #include "platform.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_testbed_service.h"
39 #include "gnunet_peerstore_service.h"
40 #include "gnunet_sensor_service.h"
41 #include "gnunet_sensor_util_lib.h"
42
43 /**
44  * Information about a single peer
45  */
46 struct PeerInfo
47 {
48
49   /**
50    * Peer Identity
51    */
52   struct GNUNET_PeerIdentity peer_id;
53
54   /**
55    * Testbed peer handle
56    */
57   struct GNUNET_TESTBED_Peer *testbed_peer;
58
59 };
60
61
62 /**
63  * Name of the configuration file used
64  */
65 static const char *cfg_filename = "gnunet-sensor-profiler.conf";
66
67 /**
68  * Directory to read sensor definitions from
69  */
70 static const char *sensor_src_dir = "sensors";
71
72 /**
73  * Directory to write new sensor definitions to
74  */
75 static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler";
76
77 /**
78  * Return value of the program
79  */
80 static int ok = 1;
81
82 /**
83  * Number of peers to run (Option -p)
84  */
85 static unsigned int num_peers = 0;
86
87 /**
88  * Set sensors running interval to this value (Option -i)
89  */
90 static unsigned int sensors_interval = 0;
91
92 /**
93  * Array of peer info for all peers
94  */
95 static struct PeerInfo *all_peers_info;
96
97 /**
98  * Number of peers that we already collected and start their info
99  */
100 static int peers_known = 0;
101
102 /**
103  * TESTBED operation connecting us to peerstore service on collection point
104  */
105 static struct GNUNET_TESTBED_Operation *peerstore_op;
106
107 /**
108  * Handle to peerstore service on collection point
109  */
110 static struct GNUNET_PEERSTORE_Handle *peerstore;
111
112 /**
113  * Dashboard service on collection point started?
114  */
115 static int dashboard_service_started = GNUNET_NO;
116
117 /**
118  * Number of peers started the sensor service successfully
119  */
120 static int sensor_services_started = 0;
121
122 /**
123  * Array of sensor names to be used for watching peerstore records
124  */
125 static char **sensor_names;
126
127 /**
128  * Size of 'sensor_names' array
129  */
130 static unsigned int sensor_names_size = 0;
131
132
133 /**
134  * Copy directory recursively
135  *
136  * @param src Path to source directory
137  * @param dst Destination directory, will be created if it does not exist
138  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
139  */
140 static int
141 copy_dir (const char *src, const char *dst);
142
143
144 /**
145  * Do clean up and shutdown scheduler
146  */
147 static void
148 do_shutdown ()                  // TODO: schedule timeout shutdown
149 {
150   int i;
151
152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n");
153   if (NULL != sensor_names)
154   {
155     for (i = 0; i < sensor_names_size; i++)
156       GNUNET_free (sensor_names[i]);
157     GNUNET_array_grow (sensor_names, sensor_names_size, 0);
158   }
159   if (NULL != peerstore_op)
160   {
161     GNUNET_TESTBED_operation_done (peerstore_op);
162     peerstore_op = NULL;
163   }
164   if (NULL != all_peers_info)
165   {
166     GNUNET_free (all_peers_info);
167     all_peers_info = NULL;
168   }
169   GNUNET_SCHEDULER_shutdown ();
170 }
171
172
173 /**
174  * Function called with each file/folder inside a directory that is being copied.
175  *
176  * @param cls closure, destination directory
177  * @param filename complete filename (absolute path)
178  * @return #GNUNET_OK to continue to iterate.
179  *         #GNUNET_SYSERR to abort iteration with error
180  */
181 static int
182 copy_dir_scanner (void *cls, const char *filename)
183 {
184   char *dst_dir = cls;
185   char *dst;
186   int copy_result;
187
188   GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR,
189                    GNUNET_STRINGS_get_short_name (filename));
190   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying `%s' to `%s'.\n", filename,
191               dst);
192   if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
193     copy_result = copy_dir (filename, dst);
194   else
195   {
196     if (GNUNET_YES == GNUNET_DISK_file_test (dst))
197       GNUNET_DISK_directory_remove (dst);
198     copy_result = GNUNET_DISK_file_copy (filename, dst);
199   }
200   GNUNET_free (dst);
201   return copy_result;
202 }
203
204
205 /**
206  * Copy directory recursively
207  *
208  * @param src Path to source directory
209  * @param dst Destination directory, will be created if it does not exist
210  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
211  */
212 static int
213 copy_dir (const char *src, const char *dst)
214 {
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying directory `%s' to `%s'.\n", src,
216               dst);
217   if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES))
218     return GNUNET_SYSERR;
219   if (GNUNET_OK != GNUNET_DISK_directory_create (dst))
220     return GNUNET_SYSERR;
221   if (GNUNET_SYSERR ==
222       GNUNET_DISK_directory_scan (src, &copy_dir_scanner, (char *) dst))
223     return GNUNET_SYSERR;
224   return GNUNET_OK;
225 }
226
227
228 /**
229  * Function called with each file/folder inside source sensor directory.
230  *
231  * @param cls closure (unused)
232  * @param filename complete filename (absolute path)
233  * @return #GNUNET_OK to continue to iterate.
234  */
235 static int
236 sensor_dir_scanner (void *cls, const char *filename)
237 {
238   const char *file_basename;
239   char *dst_path;
240   struct GNUNET_CONFIGURATION_Handle *sensor_cfg;
241   char *sensor_name;
242
243   file_basename = GNUNET_STRINGS_get_short_name (filename);
244   GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR,
245                    file_basename);
246   if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO))
247   {
248     GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path));
249   }
250   else
251   {
252     sensor_name = GNUNET_strdup(file_basename);
253     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Saving sensor name `%s'.\n", sensor_name);
254     GNUNET_array_append(sensor_names, sensor_names_size, sensor_name);
255     sensor_cfg = GNUNET_CONFIGURATION_create ();
256     GNUNET_assert (GNUNET_OK ==
257                    GNUNET_CONFIGURATION_parse (sensor_cfg, filename));
258     GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename,
259                                            "COLLECTION_POINT",
260                                            GNUNET_i2s_full (&all_peers_info[0].
261                                                             peer_id));
262     if (sensors_interval > 0)
263     {
264       GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
265                                              "INTERVAL",
266                                              (unsigned long long int)
267                                              sensors_interval);
268     }
269     GNUNET_CONFIGURATION_write (sensor_cfg, dst_path);
270     GNUNET_CONFIGURATION_destroy (sensor_cfg);
271   }
272   GNUNET_free (dst_path);
273   return GNUNET_OK;
274 }
275
276
277 /**
278  * Load sensor definitions and rewrite them to tmp location.
279  * Add collection point peer id and change running interval if needed.
280  */
281 static void
282 rewrite_sensors ()
283 {
284   GNUNET_assert (GNUNET_YES ==
285                  GNUNET_DISK_directory_test (sensor_src_dir, GNUNET_YES));
286   GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (sensor_dst_dir));
287   GNUNET_DISK_directory_scan (sensor_src_dir, &sensor_dir_scanner, NULL);
288 }
289
290
291 /**
292  * Callback to be called when dashboard service is started
293  *
294  * @param cls the callback closure from functions generating an operation
295  * @param op the operation that has been finished
296  * @param emsg error message in case the operation has failed; will be NULL if
297  *          operation has executed successfully.
298  */
299 static void
300 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
301                    const char *emsg)
302 {
303   if (NULL != emsg)
304   {
305     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
306     GNUNET_assert (0);
307   }
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
309   GNUNET_TESTBED_operation_done (op);
310   dashboard_service_started = GNUNET_YES;
311   //TODO:
312 }
313
314
315 /**
316  * Function called by PEERSTORE for each matching record.
317  *
318  * @param cls closure
319  * @param record peerstore record information
320  * @param emsg error message, or NULL if no errors
321  * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop
322  */
323 static int
324 peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record,
325                       char *emsg)
326 {
327   struct PeerInfo *peer = cls;
328   struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
329
330   if (NULL != emsg)
331   {
332     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
333     GNUNET_assert (0);
334   }
335   GNUNET_assert (record->value_size ==
336                  sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
337   anomaly = record->value;
338   GNUNET_assert (0 ==
339                  GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
340                                                 record->peer));
341   printf ("Anomaly report:\n"
342            "  Peer: `%s'\n"
343            "  Sensor: `%s'\n"
344            "  Anomalous: `%d'\n"
345            "  Anomalous neighbors: %f.\n\n",
346            GNUNET_i2s (&peer->peer_id),
347            record->key, anomaly->anomalous, anomaly->anomalous_neighbors);
348   return GNUNET_YES;
349 }
350
351
352 /**
353  * Callback to be called when peerstore service connect operation is completed
354  *
355  * @param cls the callback closure from functions generating an operation
356  * @param op the operation that has been finished
357  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
358  * @param emsg error message in case the operation has failed; will be NULL if
359  *          operation has executed successfully.
360  */
361 static void
362 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
363                       void *ca_result, const char *emsg)
364 {
365   int i;
366   int j;
367   struct PeerInfo *peer;
368
369   if (NULL != emsg)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
372     GNUNET_assert (0);
373   }
374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
375   /* Watch for anomaly reports from other peers */
376   for (i = 0; i < num_peers; i++)
377   {
378     peer = &all_peers_info[i];
379     for (j = 0; j < sensor_names_size; j++)
380     {
381       GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies", &peer->peer_id,
382           sensor_names[j], &peerstore_watch_cb, peer);
383     }
384   }
385 }
386
387
388 /**
389  * Adapter function called to establish a connection to peerstore service.
390  *
391  * @param cls closure
392  * @param cfg configuration of the peer to connect to; will be available until
393  *          GNUNET_TESTBED_operation_done() is called on the operation returned
394  *          from GNUNET_TESTBED_service_connect()
395  * @return service handle to return in 'op_result', NULL on error
396  */
397 static void *
398 peerstore_connect_adapter (void *cls,
399                            const struct GNUNET_CONFIGURATION_Handle *cfg)
400 {
401   peerstore = GNUNET_PEERSTORE_connect (cfg);
402   GNUNET_assert (NULL != peerstore);
403   return peerstore;
404 }
405
406
407 /**
408  * Adapter function called to destroy a connection to peerstore service.
409  *
410  * @param cls closure
411  * @param op_result service handle returned from the connect adapter
412  */
413 static void
414 peerstore_disconnect_adapter (void *cls, void *op_result)
415 {
416   GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
417   peerstore = NULL;
418   peerstore_op = NULL;
419 }
420
421
422 /**
423  * Callback to be called when sensor service is started
424  *
425  * @param cls the callback closure from functions generating an operation
426  * @param op the operation that has been finished
427  * @param emsg error message in case the operation has failed; will be NULL if
428  *          operation has executed successfully.
429  */
430 static void
431 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
432                         const char *emsg)
433 {
434   struct PeerInfo *peer = cls;
435
436   if (NULL != emsg)
437   {
438     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
439     GNUNET_assert (0);
440   }
441   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
442               GNUNET_i2s (&peer->peer_id));
443   GNUNET_TESTBED_operation_done (op);
444   sensor_services_started ++;
445   //TODO
446 }
447
448
449 /**
450  * Callback to be called when the requested peer information is available
451  *
452  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
453  * @param op the operation this callback corresponds to
454  * @param pinfo the result; will be NULL if the operation has failed
455  * @param emsg error message if the operation has failed; will be NULL if the
456  *          operation is successfull
457  */
458 static void
459 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
460               const struct GNUNET_TESTBED_PeerInformation *pinfo,
461               const char *emsg)
462 {
463   struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
464   struct PeerInfo *peer = &all_peers_info[peers_known];
465
466   if (NULL != emsg)
467   {
468     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
469     GNUNET_assert (0);
470   }
471   peer->testbed_peer = testbed_peer;
472   GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
473   peers_known++;
474   if (1 == peers_known)         /* First peer is collection point */
475   {
476     /* Rewrite sensors */
477     rewrite_sensors ();
478     /* Start dashboard */
479     GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
480                                         &dashboard_started, NULL, 1);
481   }
482   /* Start sensor service on every peer */
483   GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensor",
484                                       &sensor_service_started, peer, 1);
485   if (num_peers == peers_known) /* Last peer */
486   {
487     /* Connect to peerstore on first peer (collection point) */
488     peerstore_op =
489         GNUNET_TESTBED_service_connect (NULL, all_peers_info[0].testbed_peer,
490                                         "peerstore", &peerstore_connect_cb,
491                                         NULL, &peerstore_connect_adapter,
492                                         &peerstore_disconnect_adapter, NULL);
493   }
494   GNUNET_TESTBED_operation_done (op);
495 }
496
497
498 /**
499  * Signature of a main function for a testcase.
500  *
501  * @param cls closure
502  * @param h the run handle
503  * @param num number of peers in 'peers'
504  * @param peers handle to peers run in the testbed.  NULL upon timeout (see
505  *          GNUNET_TESTBED_test_run()).
506  * @param links_succeeded the number of overlay link connection attempts that
507  *          succeeded
508  * @param links_failed the number of overlay link connection attempts that
509  *          failed
510  * @see GNUNET_TESTBED_test_run()
511  */
512 static void
513 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num,
514              struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
515              unsigned int links_failed)
516 {
517   int i;
518
519   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520               "%d peers started. %d links succeeded. %d links failed.\n",
521               num_peers, links_succeeded, links_failed);
522   GNUNET_assert (num == num_peers);
523   GNUNET_assert (0 == links_failed);
524   /* Collect peer information */
525   all_peers_info = GNUNET_new_array (num_peers, struct PeerInfo);
526
527   for (i = 0; i < num_peers; i++)
528   {
529     GNUNET_TESTBED_peer_get_information (peers[i],
530                                          GNUNET_TESTBED_PIT_CONFIGURATION,
531                                          &peer_info_cb, peers[i]);
532   }
533 }
534
535
536 /**
537  * Verify that the user passed correct CL args
538  *
539  * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
540  */
541 static int
542 verify_args ()
543 {
544   if (num_peers < 3)
545   {
546     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
547                 _
548                 ("Invalid or missing number of peers. Set at least 3 peers.\n"));
549     return GNUNET_SYSERR;
550   }
551   return GNUNET_OK;
552 }
553
554
555 /**
556  * Actual main function.
557  *
558  * @param cls unused
559  * @param args remaining args, unused
560  * @param cfgfile name of the configuration
561  * @param cfg configuration handle
562  */
563 static void
564 run (void *cls, char *const *args, const char *cf,
565      const struct GNUNET_CONFIGURATION_Handle *c)
566 {
567   struct GNUNET_CONFIGURATION_Handle *cfg;
568   double links;
569
570   if (GNUNET_OK != verify_args ())
571   {
572     do_shutdown ();
573     return;
574   }
575   cfg = GNUNET_CONFIGURATION_create ();
576   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
577   links = log (num_peers) * log (num_peers) * num_peers / 2;
578   GNUNET_CONFIGURATION_set_value_number ((struct GNUNET_CONFIGURATION_Handle *)
579                                          cfg, "TESTBED", "OVERLAY_RANDOM_LINKS",
580                                          (unsigned long long int) links);
581   GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
582   GNUNET_CONFIGURATION_destroy (cfg);
583 }
584
585
586 /**
587  * Main function.
588  *
589  * @return 0 on success
590  */
591 int
592 main (int argc, char *const *argv)
593 {
594   static struct GNUNET_GETOPT_CommandLineOption options[] = {
595     {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
596      &GNUNET_GETOPT_set_uint, &num_peers},
597     {'i', "sensors-interval", "INTERVAL",
598      gettext_noop ("Change the interval or running sensors to given value"),
599      GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
600     GNUNET_GETOPT_OPTION_END
601   };
602
603   return (GNUNET_OK ==
604           GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
605                               gettext_noop ("Profiler for sensor service"),
606                               options, &run, NULL)) ? ok : 1;
607 }
608
609 /* end of gnunet-sensor-profiler.c */