2 This file is part of GNUnet.
3 (C) 2009, 2010 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.
21 * @file integration-tests/connection_watchdog.c
22 * @brief tool to monitor core and transport connections for consistency
23 * @author Matthias Wachs
26 #include "gnunet_common.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_core_service.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_statistics_service.h"
38 #define CHECK_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
39 #define STATS_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
40 #define REPEATED_STATS_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
41 #define STATS_VALUES 4
48 static struct GNUNET_TRANSPORT_Handle *th;
49 static struct GNUNET_CORE_Handle *ch;
50 static struct GNUNET_PeerIdentity my_peer_id;
51 static const struct GNUNET_CONFIGURATION_Handle *mycfg;
52 static struct GNUNET_STATISTICS_Handle *stats;
55 static unsigned int transport_connections;
56 static unsigned int core_connections;
58 static GNUNET_SCHEDULER_TaskIdentifier check_task;
59 static GNUNET_SCHEDULER_TaskIdentifier statistics_task;
61 static uint64_t statistics_transport_connections;
62 static uint64_t statistics_transport_tcp_connections;
63 static uint64_t statistics_core_neighbour_entries;
64 static uint64_t statistics_core_entries_session_map;
66 int stat_check_running;
68 static struct GNUNET_CONTAINER_MultiHashMap *peers;
72 struct GNUNET_PeerIdentity id;
73 int transport_connected;
78 int map_check_it (void *cls,
79 const GNUNET_HashCode * key,
83 struct PeerContainer *pc = value;
84 if (pc->core_connected != pc->transport_connected)
86 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
87 "Inconsistend peer `%s': TRANSPORT %s <-> CORE %s\n",
89 (GNUNET_YES == pc->transport_connected) ? "YES" : "NO",
90 (GNUNET_YES == pc->core_connected) ? "YES" : "NO");
98 int map_cleanup_it (void *cls,
99 const GNUNET_HashCode * key,
102 struct PeerContainer *pc = value;
103 GNUNET_CONTAINER_multihashmap_remove(peers, key, value);
109 map_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_cleanup_it, NULL);
112 GNUNET_CONTAINER_multihashmap_destroy(peers);
116 map_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 check_task = GNUNET_SCHEDULER_NO_TASK;
120 GNUNET_CONTAINER_multihashmap_iterate (peers, &map_check_it, &fail);
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
123 "Inconsistent peers after connection consistency check: %u\n", fail);
125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
126 "Inconsistent peers after connection consistency check: %u\n", fail);
131 GNUNET_SCHEDULER_add_now (cls, NULL);
136 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
138 int stats_check_cb (void *cls, const char *subsystem,
139 const char *name, uint64_t value,
149 if (STATS_VALUES == counter)
151 int fail = GNUNET_NO;
152 if (transport_connections != core_connections)
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 "Transport connections are inconsistent: %u transport notifications <-> %u core notifications\n",
156 transport_connections, core_connections);
160 if (transport_connections != statistics_transport_connections)
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 "Transport connections are inconsistent: %u transport notifications <-> %u in statistics (peers connected)\n",
164 transport_connections, statistics_transport_connections);
167 if (core_connections != statistics_core_entries_session_map)
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
170 "Transport connections are inconsistent: %u core notifications <-> %u in statistics (entries session map)\n",
171 core_connections, statistics_core_entries_session_map);
175 if (core_connections != statistics_core_neighbour_entries)
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 "Transport connections are inconsistent: %u core notifications <-> %u in statistics (neighbour entries allocated)\n",
179 core_connections, statistics_core_neighbour_entries);
183 if (GNUNET_NO == fail)
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185 "Statistics consistency check successful : (%u transport / %u core) connections established\n", transport_connections, core_connections);
187 /* This is only an issue when transport_connections > statistics_transport_tcp_connections */
188 if (transport_connections > statistics_transport_tcp_connections)
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Transport connections are inconsistent: %u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
192 transport_connections, statistics_transport_tcp_connections);
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Transport connections are inconsistent: %u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
199 transport_connections, statistics_transport_tcp_connections);
202 if (GNUNET_SCHEDULER_NO_TASK == statistics_task)
203 statistics_task = GNUNET_SCHEDULER_add_delayed(REPEATED_STATS_DELAY, &stats_check, NULL);
205 stat_check_running = GNUNET_NO;
213 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 statistics_task = GNUNET_SCHEDULER_NO_TASK;
217 if (GNUNET_YES == stat_check_running)
219 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
222 stat_check_running = GNUNET_YES;
224 statistics_transport_connections = 0 ;
225 statistics_core_entries_session_map = 0;
226 statistics_core_neighbour_entries = 0;
228 GNUNET_STATISTICS_get (stats, "transport", "# peers connected", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_connections);
229 GNUNET_STATISTICS_get (stats, "transport", "# TCP sessions active", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_tcp_connections);
230 GNUNET_STATISTICS_get (stats, "core", "# neighbour entries allocated", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_neighbour_entries);
231 GNUNET_STATISTICS_get (stats, "core", "# entries in session map", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_entries_session_map);
236 map_connect (const struct GNUNET_PeerIdentity *peer, void * source)
238 struct PeerContainer * pc;
239 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
241 pc = GNUNET_malloc (sizeof (struct PeerContainer));
243 pc->core_connected = GNUNET_NO;
244 pc->transport_connected = GNUNET_NO;
245 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(peers, &peer->hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
248 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
251 if (GNUNET_NO == pc->transport_connected)
253 pc->transport_connected = GNUNET_YES;
257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
258 "%s notified multiple times about for peers `%s' (%s : %s)\n",
260 GNUNET_i2s (&pc->id),
261 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
267 if (GNUNET_NO == pc->core_connected)
269 pc->core_connected = GNUNET_YES;
273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
274 "%s notified multiple times about for peers `%s' (%s : %s)\n",
276 GNUNET_i2s (&pc->id),
277 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
281 if (GNUNET_SCHEDULER_NO_TASK != check_task)
282 GNUNET_SCHEDULER_cancel(check_task);
283 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
285 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
286 GNUNET_SCHEDULER_cancel(statistics_task);
287 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
292 map_disconnect (const struct GNUNET_PeerIdentity * peer, void * source)
295 struct PeerContainer * pc;
296 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
301 "%s disconnect notification for unknown peer `%s'\n",
302 "TRANSPORT", GNUNET_i2s (peer));
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 "%s disconnect notification for unknown peer `%s'\n",
310 "CORE", GNUNET_i2s (peer));
315 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
318 if (GNUNET_YES == pc->transport_connected)
320 pc->transport_connected = GNUNET_NO;
324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325 "%s notified for not connected peer `%s' (%s : %s)\n",
327 GNUNET_i2s (&pc->id),
328 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
334 if (GNUNET_YES == pc->core_connected)
336 pc->core_connected = GNUNET_NO;
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 "%s notified for not connected peer `%s' (%s : %s)\n",
343 GNUNET_i2s (&pc->id),
344 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
349 if ((GNUNET_NO == pc->core_connected) && (GNUNET_NO == pc->transport_connected))
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing peer `%s'\n", GNUNET_i2s (&pc->id));
352 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (peers, &peer->hashPubKey, pc));
356 if (GNUNET_SCHEDULER_NO_TASK != check_task)
357 GNUNET_SCHEDULER_cancel(check_task);
358 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
360 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
361 GNUNET_SCHEDULER_cancel(statistics_task);
362 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
367 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
371 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from transport service\n");
372 GNUNET_TRANSPORT_disconnect (th);
377 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from core service\n");
378 GNUNET_CORE_disconnect (ch);
382 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
384 GNUNET_SCHEDULER_cancel(statistics_task);
385 statistics_task = GNUNET_SCHEDULER_NO_TASK;
388 if (GNUNET_SCHEDULER_NO_TASK != check_task)
390 GNUNET_SCHEDULER_cancel(check_task);
391 check_task = GNUNET_SCHEDULER_NO_TASK;
393 check_task = GNUNET_SCHEDULER_add_now (&map_check, &map_cleanup);
397 transport_notify_connect_cb (void *cls,
398 const struct GNUNET_PeerIdentity
401 GNUNET_ATS_Information * ats,
404 transport_connections ++;
405 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT connect notification for peer `%s' (%u total)\n",
406 GNUNET_i2s (peer), transport_connections);
407 map_connect (peer, th);
411 * Function called to notify transport users that another
412 * peer disconnected from us.
415 * @param peer the peer that disconnected
418 transport_notify_disconnect_cb (void *cls,
420 GNUNET_PeerIdentity * peer)
422 GNUNET_assert (transport_connections > 0);
423 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT disconnect notification for peer `%s' (%u total)\n",
424 GNUNET_i2s (peer), transport_connections) ;
425 map_disconnect (peer, th);
426 transport_connections --;
432 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
433 const struct GNUNET_ATS_Information *atsi,
434 unsigned int atsi_count)
436 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
439 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect notification for peer `%s' (%u total)\n",
440 GNUNET_i2s (peer), core_connections);
441 map_connect (peer, ch);
445 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect notification for myself `%s' (%u total)\n",
446 GNUNET_i2s (peer), core_connections);
451 core_disconnect_cb (void *cls,
453 GNUNET_PeerIdentity * peer)
455 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
457 GNUNET_assert (core_connections >= 0);
458 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect notification for peer `%s' (%u total)\n",
459 GNUNET_i2s (peer), core_connections);
460 map_disconnect (peer, ch);
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect notification for myself `%s' (%u total)\n",
466 GNUNET_i2s (peer), core_connections);
472 core_init_cb (void *cls, struct GNUNET_CORE_Handle *server,
473 const struct GNUNET_PeerIdentity *my_identity)
475 my_peer_id = *my_identity;
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connected to core service\n");
480 * Main function that will be run by the scheduler.
483 * @param args remaining command-line arguments
484 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
485 * @param cfg configuration
488 run (void *cls, char *const *args, const char *cfgfile,
489 const struct GNUNET_CONFIGURATION_Handle *cfg)
491 transport_connections = 0;
492 core_connections = 0;
494 stats = GNUNET_STATISTICS_create ("watchdog", cfg);
495 peers = GNUNET_CONTAINER_multihashmap_create (20);
497 th = GNUNET_TRANSPORT_connect(cfg, NULL, NULL, NULL,
498 &transport_notify_connect_cb,
499 &transport_notify_disconnect_cb);
500 GNUNET_assert (th != NULL);
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connected to transport service\n");
502 ch = GNUNET_CORE_connect (cfg, 1, NULL,
509 GNUNET_assert (ch != NULL);
511 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, NULL);
518 * @param argc number of arguments from the command line
519 * @param argv command line arguments
520 * @return 0 ok, 1 on error
523 main (int argc, char *const *argv)
525 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
526 /* FIMXE: add options here */
527 GNUNET_GETOPT_OPTION_END
530 GNUNET_PROGRAM_run (argc, argv, "connection-watchdog",
531 gettext_noop ("help text"), options, &run,
535 /* end of connection_watchdog.c */