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);
137 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
147 check_lowlevel_connections (int port, int protocol)
175 GNUNET_asprintf(&cmdline, "ss %s \\( sport = :%u or dport = :%u \\)", proto, port, port);
177 if (system ("ss > /dev/null 2> /dev/null"))
178 if (system ("ss > /dev/null 2> /dev/null") == 0)
179 f = popen (cmdline, "r");
183 f = popen (cmdline, "r");
186 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "ss");
187 GNUNET_free (cmdline);
191 while (NULL != fgets (line, sizeof (line), f))
195 //printf ("%s", line);
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%i TCP connections established with port %u\n",
203 GNUNET_free (cmdline);
208 int stats_check_cb (void *cls, const char *subsystem,
209 const char *name, uint64_t value,
220 if (STATS_VALUES == counter)
222 int fail = GNUNET_NO;
223 int low_level_connections_tcp = check_lowlevel_connections (2086, tcp);
224 int low_level_connections_udp = check_lowlevel_connections (2086, udp);
226 if (transport_connections != core_connections)
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 "Transport connections are inconsistent:\n %u transport notifications <-> %u core notifications\n",
230 transport_connections, core_connections);
234 if (transport_connections != statistics_transport_connections)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Transport connections are inconsistent:\n %u transport notifications <-> %u in statistics (peers connected)\n",
238 transport_connections, statistics_transport_connections);
241 if (core_connections != statistics_core_entries_session_map)
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 "Transport connections are inconsistent:\n %u core notifications <-> %u in statistics (entries session map)\n",
245 core_connections, statistics_core_entries_session_map);
249 if (core_connections != statistics_core_neighbour_entries)
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 "Transport connections are inconsistent:\n %u core notifications <-> %u in statistics (neighbour entries allocated)\n",
253 core_connections, statistics_core_neighbour_entries);
257 if (GNUNET_NO == fail)
258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
259 "Statistics consistency check successful : (%u transport / %u core) connections established\n", transport_connections, core_connections);
261 /* This is only an issue when transport_connections > statistics_transport_tcp_connections */
262 if ((low_level_connections_tcp != -1) && (statistics_transport_tcp_connections > low_level_connections_tcp))
264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265 "Lowlevel connections are inconsistent:\n %u transport tcp sessions <-> %i established tcp connections\n",
266 statistics_transport_tcp_connections, low_level_connections_tcp);
269 else if (low_level_connections_tcp != -1)
271 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
272 "%u TCP connections, %u UDP connections \n",
273 low_level_connections_tcp, low_level_connections_udp);
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 "Error obtaining TCP connections\n");
282 if (transport_connections > statistics_transport_tcp_connections)
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
285 "Transport connections are inconsistent: %u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
286 transport_connections, statistics_transport_tcp_connections);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Transport connections are inconsistent: %u transport notifications <-> %u in statistics (statistics_transport_tcp_connections)\n",
293 transport_connections, statistics_transport_tcp_connections);
296 if (GNUNET_SCHEDULER_NO_TASK == statistics_task)
297 statistics_task = GNUNET_SCHEDULER_add_delayed(REPEATED_STATS_DELAY, &stats_check, NULL);
299 stat_check_running = GNUNET_NO;
308 stats_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
310 statistics_task = GNUNET_SCHEDULER_NO_TASK;
312 if (GNUNET_YES == stat_check_running)
314 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
317 stat_check_running = GNUNET_YES;
319 statistics_transport_connections = 0 ;
320 statistics_core_entries_session_map = 0;
321 statistics_core_neighbour_entries = 0;
323 GNUNET_STATISTICS_get (stats, "transport", "# peers connected", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_connections);
324 GNUNET_STATISTICS_get (stats, "transport", "# TCP sessions active", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_transport_tcp_connections);
325 GNUNET_STATISTICS_get (stats, "core", "# neighbour entries allocated", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_neighbour_entries);
326 GNUNET_STATISTICS_get (stats, "core", "# entries in session map", GNUNET_TIME_UNIT_MINUTES, NULL, &stats_check_cb, &statistics_core_entries_session_map);
331 map_connect (const struct GNUNET_PeerIdentity *peer, void * source)
333 struct PeerContainer * pc;
334 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
336 pc = GNUNET_malloc (sizeof (struct PeerContainer));
338 pc->core_connected = GNUNET_NO;
339 pc->transport_connected = GNUNET_NO;
340 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(peers, &peer->hashPubKey, pc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
343 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
346 if (GNUNET_NO == pc->transport_connected)
348 pc->transport_connected = GNUNET_YES;
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 "%s notified multiple times about for peers `%s' (%s : %s)\n",
355 GNUNET_i2s (&pc->id),
356 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
362 if (GNUNET_NO == pc->core_connected)
364 pc->core_connected = GNUNET_YES;
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 "%s notified multiple times about for peers `%s' (%s : %s)\n",
371 GNUNET_i2s (&pc->id),
372 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
376 if (GNUNET_SCHEDULER_NO_TASK != check_task)
377 GNUNET_SCHEDULER_cancel(check_task);
378 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
380 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
381 GNUNET_SCHEDULER_cancel(statistics_task);
382 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
387 map_disconnect (const struct GNUNET_PeerIdentity * peer, void * source)
390 struct PeerContainer * pc;
391 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(peers, &peer->hashPubKey))
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396 "%s disconnect notification for unknown peer `%s'\n",
397 "TRANSPORT", GNUNET_i2s (peer));
403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
404 "%s disconnect notification for unknown peer `%s'\n",
405 "CORE", GNUNET_i2s (peer));
410 pc = GNUNET_CONTAINER_multihashmap_get(peers, &peer->hashPubKey);
413 if (GNUNET_YES == pc->transport_connected)
415 pc->transport_connected = GNUNET_NO;
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "%s notified for not connected peer `%s' (%s : %s)\n",
422 GNUNET_i2s (&pc->id),
423 "CORE", (pc->core_connected == GNUNET_YES) ? "yes" : "no");
429 if (GNUNET_YES == pc->core_connected)
431 pc->core_connected = GNUNET_NO;
435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
436 "%s notified for not connected peer `%s' (%s : %s)\n",
438 GNUNET_i2s (&pc->id),
439 "TRANSPORT", (pc->transport_connected == GNUNET_YES) ? "yes" : "no");
444 if ((GNUNET_NO == pc->core_connected) && (GNUNET_NO == pc->transport_connected))
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Removing peer `%s'\n", GNUNET_i2s (&pc->id));
447 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (peers, &peer->hashPubKey, pc));
451 if (GNUNET_SCHEDULER_NO_TASK != check_task)
452 GNUNET_SCHEDULER_cancel(check_task);
453 check_task = GNUNET_SCHEDULER_add_delayed(CHECK_DELAY, &map_check, NULL);
455 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
456 GNUNET_SCHEDULER_cancel(statistics_task);
457 statistics_task = GNUNET_SCHEDULER_add_delayed(STATS_DELAY, &stats_check, NULL);
462 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
466 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from transport service\n");
467 GNUNET_TRANSPORT_disconnect (th);
472 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Disconnecting from core service\n");
473 GNUNET_CORE_disconnect (ch);
477 if (GNUNET_SCHEDULER_NO_TASK != statistics_task)
479 GNUNET_SCHEDULER_cancel(statistics_task);
480 statistics_task = GNUNET_SCHEDULER_NO_TASK;
483 if (GNUNET_SCHEDULER_NO_TASK != check_task)
485 GNUNET_SCHEDULER_cancel(check_task);
486 check_task = GNUNET_SCHEDULER_NO_TASK;
488 check_task = GNUNET_SCHEDULER_add_now (&map_check, &map_cleanup);
492 transport_notify_connect_cb (void *cls,
493 const struct GNUNET_PeerIdentity
496 GNUNET_ATS_Information * ats,
499 transport_connections ++;
500 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT connect for peer `%s' (%u total)\n",
501 GNUNET_i2s (peer), transport_connections);
502 map_connect (peer, th);
506 * Function called to notify transport users that another
507 * peer disconnected from us.
510 * @param peer the peer that disconnected
513 transport_notify_disconnect_cb (void *cls,
515 GNUNET_PeerIdentity * peer)
517 GNUNET_assert (transport_connections > 0);
518 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "TRANSPORT disconnect for peer `%s' (%u total)\n",
519 GNUNET_i2s (peer), transport_connections) ;
520 map_disconnect (peer, th);
521 transport_connections --;
527 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
528 const struct GNUNET_ATS_Information *atsi,
529 unsigned int atsi_count)
531 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
534 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect for peer `%s' (%u total)\n",
535 GNUNET_i2s (peer), core_connections);
536 map_connect (peer, ch);
540 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE connect for myself `%s' (%u total)\n",
541 GNUNET_i2s (peer), core_connections);
546 core_disconnect_cb (void *cls,
548 GNUNET_PeerIdentity * peer)
550 if (0 != memcmp (peer, &my_peer_id, sizeof (struct GNUNET_PeerIdentity)))
552 GNUNET_assert (core_connections >= 0);
553 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect for peer `%s' (%u total)\n",
554 GNUNET_i2s (peer), core_connections);
555 map_disconnect (peer, ch);
560 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CORE disconnect for myself `%s' (%u total)\n",
561 GNUNET_i2s (peer), core_connections);
567 core_init_cb (void *cls, struct GNUNET_CORE_Handle *server,
568 const struct GNUNET_PeerIdentity *my_identity)
570 my_peer_id = *my_identity;
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connected to core service\n");
575 * Main function that will be run by the scheduler.
578 * @param args remaining command-line arguments
579 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
580 * @param cfg configuration
583 run (void *cls, char *const *args, const char *cfgfile,
584 const struct GNUNET_CONFIGURATION_Handle *cfg)
586 transport_connections = 0;
587 core_connections = 0;
590 stats = GNUNET_STATISTICS_create ("watchdog", cfg);
591 peers = GNUNET_CONTAINER_multihashmap_create (20);
593 th = GNUNET_TRANSPORT_connect(cfg, NULL, NULL, NULL,
594 &transport_notify_connect_cb,
595 &transport_notify_disconnect_cb);
596 GNUNET_assert (th != NULL);
597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connected to transport service\n");
598 ch = GNUNET_CORE_connect (cfg, 1, NULL,
605 GNUNET_assert (ch != NULL);
607 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, NULL);
615 * @param argc number of arguments from the command line
616 * @param argv command line arguments
617 * @return 0 ok, 1 on error
620 main (int argc, char *const *argv)
622 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
623 /* FIMXE: add options here */
624 GNUNET_GETOPT_OPTION_END
627 GNUNET_PROGRAM_run (argc, argv, "cn",
628 gettext_noop ("help text"), options, &run,
632 /* end of connection_watchdog.c */