2 This file is part of GNUnet.
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.
24 * - Rewrite interval time (optional)
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)
32 * @file sensor/gnunet-sensor-profiler.c
33 * @brief Profiler for the sensor service
34 * @author Omar Tarabai
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"
44 * Information about a single peer
52 struct GNUNET_PeerIdentity peer_id;
57 struct GNUNET_TESTBED_Peer *testbed_peer;
63 * Name of the configuration file used
65 static const char *cfg_filename = "gnunet-sensor-profiler.conf";
68 * Directory to read sensor definitions from
70 static const char *sensor_src_dir = "sensors";
73 * Directory to write new sensor definitions to
75 static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler";
78 * Return value of the program
83 * Number of peers to run (Option -p)
85 static unsigned int num_peers = 0;
88 * Set sensors running interval to this value (Option -i)
90 static unsigned int sensors_interval = 0;
93 * Path to topology file (Option -t)
95 static char *topology_file;
98 * Array of peer info for all peers
100 static struct PeerInfo *all_peers_info;
103 * Number of peers that we already collected and start their info
105 static int peers_known = 0;
108 * TESTBED operation connecting us to peerstore service on collection point
110 static struct GNUNET_TESTBED_Operation *peerstore_op;
113 * Handle to peerstore service on collection point
115 static struct GNUNET_PEERSTORE_Handle *peerstore;
118 * Dashboard service on collection point started?
120 static int dashboard_service_started = GNUNET_NO;
123 * Number of peers started the sensor service successfully
125 static int sensor_services_started = 0;
128 * Array of sensor names to be used for watching peerstore records
130 static char **sensor_names;
133 * Size of 'sensor_names' array
135 static unsigned int sensor_names_size = 0;
139 * Copy directory recursively
141 * @param src Path to source directory
142 * @param dst Destination directory, will be created if it does not exist
143 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
146 copy_dir (const char *src, const char *dst);
150 * Do clean up and shutdown scheduler
153 do_shutdown () // TODO: schedule timeout shutdown
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n");
158 if (NULL != sensor_names)
160 for (i = 0; i < sensor_names_size; i++)
161 GNUNET_free (sensor_names[i]);
162 GNUNET_array_grow (sensor_names, sensor_names_size, 0);
164 if (NULL != peerstore_op)
166 GNUNET_TESTBED_operation_done (peerstore_op);
169 if (NULL != all_peers_info)
171 GNUNET_free (all_peers_info);
172 all_peers_info = NULL;
174 GNUNET_SCHEDULER_shutdown ();
179 * Function called with each file/folder inside a directory that is being copied.
181 * @param cls closure, destination directory
182 * @param filename complete filename (absolute path)
183 * @return #GNUNET_OK to continue to iterate.
184 * #GNUNET_SYSERR to abort iteration with error
187 copy_dir_scanner (void *cls, const char *filename)
193 GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR,
194 GNUNET_STRINGS_get_short_name (filename));
195 if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
196 copy_result = copy_dir (filename, dst);
199 if (GNUNET_YES == GNUNET_DISK_file_test (dst))
200 GNUNET_DISK_directory_remove (dst);
201 copy_result = GNUNET_DISK_file_copy (filename, dst);
209 * Copy directory recursively
211 * @param src Path to source directory
212 * @param dst Destination directory, will be created if it does not exist
213 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
216 copy_dir (const char *src, const char *dst)
218 if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES))
219 return GNUNET_SYSERR;
220 if (GNUNET_OK != GNUNET_DISK_directory_create (dst))
221 return GNUNET_SYSERR;
223 GNUNET_DISK_directory_scan (src, ©_dir_scanner, (char *) dst))
224 return GNUNET_SYSERR;
230 * Function called with each file/folder inside source sensor directory.
232 * @param cls closure (unused)
233 * @param filename complete filename (absolute path)
234 * @return #GNUNET_OK to continue to iterate.
237 sensor_dir_scanner (void *cls, const char *filename)
239 const char *file_basename;
241 struct GNUNET_CONFIGURATION_Handle *sensor_cfg;
244 file_basename = GNUNET_STRINGS_get_short_name (filename);
245 GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR,
247 if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO))
249 GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path));
253 sensor_name = GNUNET_strdup (file_basename);
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,
260 GNUNET_i2s_full (&all_peers_info[0].
262 if (sensors_interval > 0)
264 GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
266 (unsigned long long int)
269 GNUNET_CONFIGURATION_write (sensor_cfg, dst_path);
270 GNUNET_CONFIGURATION_destroy (sensor_cfg);
272 GNUNET_free (dst_path);
278 * Load sensor definitions and rewrite them to tmp location.
279 * Add collection point peer id and change running interval if needed.
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);
292 * Callback to be called when dashboard service is started
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.
300 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
309 GNUNET_TESTBED_operation_done (op);
310 dashboard_service_started = GNUNET_YES;
316 * Function called by PEERSTORE for each matching record.
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
324 peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record,
327 struct PeerInfo *peer = cls;
328 struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
335 GNUNET_assert (record->value_size ==
336 sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
337 anomaly = record->value;
339 GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
341 printf ("Anomaly report:\n" " Peer: `%s'\n" " Sensor: `%s'\n"
342 " Anomalous: `%d'\n" " Anomalous neighbors: %f.\n\n",
343 GNUNET_i2s (&peer->peer_id), record->key, anomaly->anomalous,
344 anomaly->anomalous_neighbors);
350 * Callback to be called when peerstore service connect operation is completed
352 * @param cls the callback closure from functions generating an operation
353 * @param op the operation that has been finished
354 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
355 * @param emsg error message in case the operation has failed; will be NULL if
356 * operation has executed successfully.
359 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
360 void *ca_result, const char *emsg)
364 struct PeerInfo *peer;
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
372 /* Watch for anomaly reports from other peers */
373 for (i = 0; i < num_peers; i++)
375 peer = &all_peers_info[i];
376 for (j = 0; j < sensor_names_size; j++)
378 GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies",
379 &peer->peer_id, sensor_names[j],
380 &peerstore_watch_cb, peer);
387 * Adapter function called to establish a connection to peerstore service.
390 * @param cfg configuration of the peer to connect to; will be available until
391 * GNUNET_TESTBED_operation_done() is called on the operation returned
392 * from GNUNET_TESTBED_service_connect()
393 * @return service handle to return in 'op_result', NULL on error
396 peerstore_connect_adapter (void *cls,
397 const struct GNUNET_CONFIGURATION_Handle *cfg)
399 peerstore = GNUNET_PEERSTORE_connect (cfg);
400 GNUNET_assert (NULL != peerstore);
406 * Adapter function called to destroy a connection to peerstore service.
409 * @param op_result service handle returned from the connect adapter
412 peerstore_disconnect_adapter (void *cls, void *op_result)
414 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
421 * Callback to be called when sensor service is started
423 * @param cls the callback closure from functions generating an operation
424 * @param op the operation that has been finished
425 * @param emsg error message in case the operation has failed; will be NULL if
426 * operation has executed successfully.
429 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
432 struct PeerInfo *peer = cls;
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
440 GNUNET_i2s (&peer->peer_id));
441 GNUNET_TESTBED_operation_done (op);
442 sensor_services_started++;
443 if (sensor_services_started == num_peers) //TODO: remove
450 * Callback to be called when the requested peer information is available
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
459 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
460 const struct GNUNET_TESTBED_PeerInformation *pinfo,
463 struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
464 struct PeerInfo *peer = &all_peers_info[peers_known];
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
471 peer->testbed_peer = testbed_peer;
472 GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
474 if (1 == peers_known) /* First peer is collection point */
476 /* Rewrite sensors */
478 /* Start dashboard */
479 GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
480 &dashboard_started, NULL, 1);
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 */
487 /* Connect to peerstore on first peer (collection point) */
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);
494 GNUNET_TESTBED_operation_done (op);
499 * Signature of a main function for a testcase.
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
508 * @param links_failed the number of overlay link connection attempts that
510 * @see GNUNET_TESTBED_test_run()
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)
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);
527 for (i = 0; i < num_peers; i++)
529 GNUNET_TESTBED_peer_get_information (peers[i],
530 GNUNET_TESTBED_PIT_CONFIGURATION,
531 &peer_info_cb, peers[i]);
537 * Verify that the user passed correct CL args
539 * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
548 ("Invalid or missing number of peers. Set at least 3 peers.\n"));
549 return GNUNET_SYSERR;
551 if (NULL == topology_file ||
552 GNUNET_YES != GNUNET_DISK_file_test (topology_file))
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 _("Missing or invalid topology file.\n"));
556 return GNUNET_SYSERR;
563 * Actual main function.
566 * @param args remaining args, unused
567 * @param cfgfile name of the configuration
568 * @param cfg configuration handle
571 run (void *cls, char *const *args, const char *cf,
572 const struct GNUNET_CONFIGURATION_Handle *c)
574 struct GNUNET_CONFIGURATION_Handle *cfg;
576 if (GNUNET_OK != verify_args ())
581 cfg = GNUNET_CONFIGURATION_create ();
582 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
583 GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *)
585 "OVERLAY_TOPOLOGY_FILE",
587 GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
588 GNUNET_CONFIGURATION_destroy (cfg);
595 * @return 0 on success
598 main (int argc, char *const *argv)
600 static struct GNUNET_GETOPT_CommandLineOption options[] = {
601 {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
602 &GNUNET_GETOPT_set_uint, &num_peers},
603 {'t', "topology-file", "FILEPATH", gettext_noop ("Path to topology file"),
604 GNUNET_YES, &GNUNET_GETOPT_set_filename, &topology_file},
605 {'i', "sensors-interval", "INTERVAL",
606 gettext_noop ("Change the interval or running sensors to given value"),
607 GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
608 GNUNET_GETOPT_OPTION_END
612 GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
613 gettext_noop ("Profiler for sensor service"),
614 options, &run, NULL)) ? ok : 1;
617 /* end of gnunet-sensor-profiler.c */