#include "platform.h"
#include "gnunet_arm_service.h"
#include "gnunet_testing_lib.h"
+#include "gnunet_core_service.h"
#define VERBOSE_TESTING GNUNET_NO
+#define VERBOSE_TOPOLOGY GNUNET_YES
+
#define DEBUG_CHURN GNUNET_NO
/**
*/
#define HIGH_PORT 56000
-#define MAX_OUTSTANDING_CONNECTIONS 50
+#define MAX_OUTSTANDING_CONNECTIONS 40
+
+#define MAX_CONCURRENT_HOSTKEYS 10
-#define MAX_CONCURRENT_HOSTKEYS 16
+#define MAX_CONCURRENT_STARTING 10
-#define MAX_CONCURRENT_STARTING 50
+#define MAX_CONCURRENT_SHUTDOWN 10
#define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
* Prototype of a function called whenever two peers would be connected
* in a certain topology.
*/
-typedef int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
+typedef unsigned int (*GNUNET_TESTING_ConnectionProcessor)(struct GNUNET_TESTING_PeerGroup *pg,
unsigned int first,
unsigned int second);
/**
* Total peers to wait for
*/
- int total_peers;
+ unsigned int total_peers;
/**
* Number of peers successfully shut down
*/
- int peers_down;
+ unsigned int peers_down;
/**
* Number of peers failed to shut down
*/
- int peers_failed;
+ unsigned int peers_failed;
+
+ /**
+ * Number of peers we have started shutting
+ * down. If too many, wait on them.
+ */
+ unsigned int outstanding;
+
+ /**
+ * Timeout for shutdown.
+ */
+ struct GNUNET_TIME_Relative timeout;
/**
* Callback to call when all peers either
void *cb_cls;
};
+/**
+ * Individual shutdown context for a particular peer.
+ */
+struct PeerShutdownContext
+{
+ /**
+ * Pointer to the high level shutdown context.
+ */
+ struct ShutdownContext *shutdown_ctx;
+
+ /**
+ * The daemon handle for the peer to shut down.
+ */
+ struct GNUNET_TESTING_Daemon *daemon;
+};
+
+/**
+ * Individual shutdown context for a particular peer.
+ */
+struct PeerRestartContext
+{
+ /**
+ * Pointer to the high level restart context.
+ */
+ struct ChurnRestartContext *churn_restart_ctx;
+
+ /**
+ * The daemon handle for the peer to shut down.
+ */
+ struct GNUNET_TESTING_Daemon *daemon;
+};
+
+
struct CreateTopologyContext
{
* Hostname, where to start the peer
*/
const char *hostname;
+
+ /**
+ * Username to use when connecting to the
+ * host via ssh.
+ */
+ const char *username;
+
+ /**
+ * Port to use for ssh.
+ */
+ uint16_t sshport;
+
+};
+
+struct ChurnRestartContext
+{
+ /**
+ * Number of restarts currently in flight.
+ */
+ unsigned int outstanding;
+
+ /**
+ * Handle to the underlying churn context.
+ */
+ struct ChurnContext *churn_ctx;
+
+ /**
+ * How long to allow the operation to take.
+ */
+ struct GNUNET_TIME_Relative timeout;
};
/**
/**
- * Data we keep per host.
+ * Linked list of per-host data.
*/
struct HostData
{
*/
char *hostname;
+ /**
+ * SSH username to use when connecting to this host.
+ */
+ char *username;
+
+ /**
+ * SSH port to use when connecting to this host.
+ */
+ uint16_t sshport;
+
/**
* Lowest port that we have not yet used
* for GNUnet.
uint16_t minport;
};
+struct TopologyIterateContext
+{
+ /**
+ * Callback for notifying of two connected peers.
+ */
+ GNUNET_TESTING_NotifyTopology topology_cb;
+
+ /**
+ * Closure for topology_cb
+ */
+ void *cls;
+
+ /**
+ * Number of peers currently connected to.
+ */
+ unsigned int connected;
+
+ /**
+ * Number of peers we have finished iterating.
+ */
+ unsigned int completed;
+
+ /**
+ * Number of peers total.
+ */
+ unsigned int total;
+};
+
+struct StatsIterateContext
+{
+ /**
+ * Continuation to call once all stats information has been retrieved.
+ */
+ GNUNET_STATISTICS_Callback cont;
+
+ /**
+ * Proc function to call on each value received.
+ */
+ GNUNET_TESTING_STATISTICS_Iterator proc;
+
+ /**
+ * Closure for topology_cb
+ */
+ void *cls;
+
+ /**
+ * Number of peers currently connected to.
+ */
+ unsigned int connected;
+
+ /**
+ * Number of peers we have finished iterating.
+ */
+ unsigned int completed;
+
+ /**
+ * Number of peers total.
+ */
+ unsigned int total;
+};
+
+struct CoreContext
+{
+ void *iter_context;
+ struct GNUNET_TESTING_Daemon *daemon;
+};
+
+struct StatsCoreContext
+{
+ void *iter_context;
+ struct GNUNET_TESTING_Daemon *daemon;
+ /**
+ * Handle to the statistics service.
+ */
+ struct GNUNET_STATISTICS_Handle *stats_handle;
+
+ /**
+ * Handle for getting statistics.
+ */
+ struct GNUNET_STATISTICS_GetHandle *stats_get_handle;
+};
/**
* Handle to a group of GNUnet peers.
void *notify_connection_cls;
/**
- * NULL-terminated array of information about
- * hosts.
+ * Array of information about hosts.
*/
struct HostData *hosts;
+ /**
+ * Number of hosts (size of HostData)
+ */
+ unsigned int num_hosts;
+
/**
* Array of "total" peers.
*/
struct UpdateContext
{
struct GNUNET_CONFIGURATION_Handle *ret;
+ const struct GNUNET_CONFIGURATION_Handle *orig;
const char *hostname;
unsigned int nport;
unsigned int upnum;
+ unsigned int fdnum;
};
+struct ConnectTopologyContext
+{
+ /**
+ * How many connections are left to create.
+ */
+ unsigned int remaining_connections;
+
+ /**
+ * Handle to group of peers.
+ */
+ struct GNUNET_TESTING_PeerGroup *pg;
+
+ /**
+ * Temp value set for each iteration.
+ */
+ struct PeerData *first;
+
+ /**
+ * Notification that all peers are connected.
+ */
+ GNUNET_TESTING_NotifyCompletion notify_connections_done;
+
+ /**
+ * Closure for notify.
+ */
+ void *notify_cls;
+};
struct ConnectContext
{
+ /**
+ * Peer to connect second to.
+ */
struct GNUNET_TESTING_Daemon *first;
+ /**
+ * Peer to connect first to.
+ */
struct GNUNET_TESTING_Daemon *second;
- struct GNUNET_TESTING_PeerGroup *pg;
+ /**
+ * Higher level topology connection context.
+ */
+ struct ConnectTopologyContext *ct_ctx;
};
/**
* known topology, GNUNET_NO if not
*/
int
-GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, char * topology_string)
+GNUNET_TESTING_topology_get(enum GNUNET_TESTING_Topology *topology, const char * topology_string)
{
/**
* Strings representing topologies in enum
*/
int
GNUNET_TESTING_topology_option_get (enum GNUNET_TESTING_TopologyOption *topology_option,
- char * topology_string)
+ const char * topology_string)
{
/**
* Options for connecting a topology as strings.
*/
"CONNECT_DFS",
+ /**
+ * Find the N closest peers to each allowed peer in the
+ * topology and make sure a connection to those peers
+ * exists in the connect topology.
+ */
+ "CONNECT_CLOSEST",
+
/**
* No options specified.
*/
unsigned int ival;
char cval[12];
char uval[128];
+ char *single_variable;
+ char *per_host_variable;
+ unsigned long long num_per_host;
if ((0 == strcmp (option, "PORT")) && (1 == sscanf (value, "%u", &ival)))
{
- if (ival != 0)
+ GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
+ if ((ival != 0) && (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable)))
{
GNUNET_snprintf (cval, sizeof (cval), "%u", ctx->nport++);
value = cval;
}
+
+ GNUNET_free(single_variable);
}
if (0 == strcmp (option, "UNIXPATH"))
{
- GNUNET_snprintf (uval,
- sizeof (uval),
- "/tmp/test-service-%s-%u",
- section,
- ctx->upnum++);
- value = uval;
+ GNUNET_asprintf(&single_variable, "single_%s_per_host", section);
+ GNUNET_asprintf(&per_host_variable, "num_%s_per_host", section);
+ if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(ctx->orig, "testing", single_variable))
+ {
+ GNUNET_snprintf (uval,
+ sizeof (uval),
+ "/tmp/test-service-%s-%u",
+ section,
+ ctx->upnum++);
+ value = uval;
+ }
+ else if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_number(ctx->orig, "testing", per_host_variable, &num_per_host))
+ {
+ GNUNET_snprintf (uval,
+ sizeof (uval),
+ "/tmp/test-service-%s-%u",
+ section,
+ ctx->fdnum % num_per_host);
+ value = uval;
+ }
+ GNUNET_free(single_variable);
+ GNUNET_free(per_host_variable);
+
}
if ((0 == strcmp (option, "HOSTNAME")) && (ctx->hostname != NULL))
* port numbers that were used
* @param upnum number to make unix domain socket names unique
* @param hostname hostname of the controlling host, to allow control connections from
+ * @param fdnum number used to offset the unix domain socket for grouped processes
+ * (such as statistics or peerinfo, which can be shared among others)
*
* @return new configuration, NULL on error
*/
make_config (const struct GNUNET_CONFIGURATION_Handle *cfg,
uint16_t * port,
uint32_t * upnum,
- const char *hostname)
+ const char *hostname, uint32_t * fdnum)
{
struct UpdateContext uc;
uint16_t orig;
orig = *port;
uc.nport = *port;
uc.upnum = *upnum;
+ uc.fdnum = *fdnum;
uc.ret = GNUNET_CONFIGURATION_create ();
uc.hostname = hostname;
+ uc.orig = cfg;
GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
if (uc.nport >= HIGH_PORT)
if (GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "control_host", &control_host) == GNUNET_OK)
{
- GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
+ if (hostname != NULL)
+ GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1; %s;", control_host, hostname);
+ else
+ GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", control_host);
+
GNUNET_CONFIGURATION_set_value_string(uc.ret, "core", "ACCEPT_FROM", allowed_hosts);
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport", "ACCEPT_FROM", allowed_hosts);
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "dht", "ACCEPT_FROM", allowed_hosts);
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "statistics", "ACCEPT_FROM", allowed_hosts);
GNUNET_free_non_null(control_host);
GNUNET_free(allowed_hosts);
}
if (hostname != NULL)
{
GNUNET_asprintf(&allowed_hosts, "%s; 127.0.0.1;", hostname);
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", hostname);
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", hostname);
GNUNET_CONFIGURATION_set_value_string(uc.ret, "arm", "ACCEPT_FROM", allowed_hosts);
GNUNET_free(allowed_hosts);
}
+ else
+ {
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-tcp", "BINDTO", "127.0.0.1");
+ GNUNET_CONFIGURATION_set_value_string(uc.ret, "transport-udp", "BINDTO", "127.0.0.1");
+ }
*port = (uint16_t) uc.nport;
*upnum = uc.upnum;
+ uc.fdnum++;
+ *fdnum = uc.fdnum;
return uc.ret;
}
* technically should only be 0 or 2
*
*/
-static int
+static unsigned int
add_actual_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
{
int added;
* for being sure doesn't bother me!
*
*/
-static int
+static unsigned int
add_allowed_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
{
int added;
* @return the number of connections added (can be 0, 1 or 2)
*
*/
-static int
+static unsigned int
blacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
{
int added;
* @return the number of connections removed (can be 0, 1 or 2)
*
*/
-static int
+static unsigned int
unblacklist_connections(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second)
{
int removed;
*
* @return the number of connections created
*/
-static int
+static unsigned int
create_scale_free (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
* @return the number of connections that were set up
*
*/
-int
+static unsigned int
create_small_world_ring(struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int i, j;
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_nated_internet (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int outer_count, inner_count;
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_small_world (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int i, j, k;
unsigned int distance;
double probability, random, percentage;
unsigned int smallWorldConnections;
+ unsigned int small_world_it;
char *p_string;
int connect_attempts;
square = floor (sqrt (pg->total));
"TESTING");
GNUNET_free (p_string);
}
+ if (percentage < 0.0)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Invalid value `%s' for option `%s' in section `%s': got %f, needed value greater than 0\n"),
+ "PERCENTAGE", "TESTING", percentage);
+ percentage = 0.5;
+ }
probability = 0.5; /* FIXME: default percentage? */
if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(pg->cfg,
"TESTING",
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Total connections added thus far: %u!\n"), connect_attempts);
#endif
smallWorldConnections = 0;
- for (i = 0; i < (int) (natLog * percentage); i++)
+ small_world_it = (unsigned int)(natLog * percentage);
+ if (small_world_it < 1)
+ small_world_it = 1;
+ GNUNET_assert(small_world_it > 0 && small_world_it < (unsigned int)-1);
+ for (i = 0; i < small_world_it; i++)
{
for (j = 0; j < pg->total; j++)
{
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_erdos_renyi (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
double temp_rand;
/**
* Create a topology given a peer group (set of running peers)
- * and a connection processor.
+ * and a connection processor. This particular function creates
+ * the connections for a 2d-torus, plus additional "closest"
+ * connections per peer.
*
* @param pg the peergroup to create the topology on
* @param proc the connection processor to call to actually set
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_2d_torus (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int i;
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_clique (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int outer_count;
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_line (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int count;
* @return the number of connections that were set up
*
*/
-static int
+static unsigned int
create_ring (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_ConnectionProcessor proc)
{
unsigned int count;
FILE *temp_friend_handle;
unsigned int pg_iter;
char *temp_service_path;
- pid_t *pidarr;
+ struct GNUNET_OS_Process **procarr;
char *arg;
char * mytemp;
enum GNUNET_OS_ProcessStatusType type;
int ret;
int max_wait = 10;
- pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
+ procarr = GNUNET_malloc(sizeof(struct GNUNET_OS_Process *) * pg->total);
for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
{
mytemp = GNUNET_DISK_mktemp("friends");
if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
{
GNUNET_asprintf (&arg, "%s/friends", temp_service_path);
- pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
+ procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
"mv", mytemp, arg, NULL);
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
GNUNET_asprintf (&arg, "%s@%s:%s/friends", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
else
GNUNET_asprintf (&arg, "%s:%s/friends", pg->peers[pg_iter].daemon->hostname, temp_service_path);
- pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
+ procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
"scp", mytemp, arg, NULL);
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Checking copy status of file %d\n"), pg_iter);
#endif
- if (pidarr[pg_iter] != 0) /* Check for already completed! */
+ if (procarr[pg_iter] != NULL) /* Check for already completed! */
{
- if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
+ if (GNUNET_OS_process_status(procarr[pg_iter], &type, &return_code) != GNUNET_OK)
{
ret = GNUNET_SYSERR;
}
}
else
{
- pidarr[pg_iter] = 0;
+ GNUNET_OS_process_close (procarr[pg_iter]);
+ procarr[pg_iter] = NULL;
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("File %d copied\n"), pg_iter);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Finished copying all friend files!\n"));
#endif
- GNUNET_free(pidarr);
+ GNUNET_free(procarr);
return ret;
}
* @param transports space delimited list of transports to blacklist
*/
static int
-create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
+create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, const char *transports)
{
FILE *temp_file_handle;
static struct BlacklistContext blacklist_ctx;
unsigned int pg_iter;
char *temp_service_path;
- pid_t *pidarr;
+ struct GNUNET_OS_Process **procarr;
char *arg;
char *mytemp;
enum GNUNET_OS_ProcessStatusType type;
char *pos;
char *temp_transports;
- pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
+ procarr = GNUNET_malloc(sizeof(struct GNUNET_OS_Process *) * pg->total);
for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
{
mytemp = GNUNET_DISK_mktemp("blacklist");
if (pg->peers[pg_iter].daemon->hostname == NULL) /* Local, just copy the file */
{
GNUNET_asprintf (&arg, "%s/blacklist", temp_service_path);
- pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
+ procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "mv",
"mv", mytemp, arg, NULL);
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
GNUNET_asprintf (&arg, "%s@%s:%s/blacklist", pg->peers[pg_iter].daemon->username, pg->peers[pg_iter].daemon->hostname, temp_service_path);
else
GNUNET_asprintf (&arg, "%s:%s/blacklist", pg->peers[pg_iter].daemon->hostname, temp_service_path);
- pidarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
+ procarr[pg_iter] = GNUNET_OS_start_process (NULL, NULL, "scp",
"scp", mytemp, arg, NULL);
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Checking copy status of file %d\n"), pg_iter);
#endif
- if (pidarr[pg_iter] != 0) /* Check for already completed! */
+ if (procarr[pg_iter] != NULL) /* Check for already completed! */
{
- if (GNUNET_OS_process_status(pidarr[pg_iter], &type, &return_code) != GNUNET_OK)
+ if (GNUNET_OS_process_status(procarr[pg_iter], &type, &return_code) != GNUNET_OK)
{
ret = GNUNET_SYSERR;
}
}
else
{
- pidarr[pg_iter] = 0;
+ GNUNET_OS_process_close (procarr[pg_iter]);
+ procarr[pg_iter] = NULL;
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("File %d copied\n"), pg_iter);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Finished copying all blacklist files!\n"));
#endif
- GNUNET_free(pidarr);
+ GNUNET_free(procarr);
return ret;
}
struct GNUNET_TESTING_Daemon *second_daemon,
const char *emsg)
{
- struct GNUNET_TESTING_PeerGroup *pg = cls;
+ struct ConnectTopologyContext *ct_ctx = cls;
+ struct GNUNET_TESTING_PeerGroup *pg = ct_ctx->pg;
outstanding_connects--;
+ ct_ctx->remaining_connections--;
+ if (ct_ctx->remaining_connections == 0)
+ {
+ if (ct_ctx->notify_connections_done != NULL)
+ ct_ctx->notify_connections_done(ct_ctx->notify_cls, NULL);
+ GNUNET_free(ct_ctx);
+ }
- pg->notify_connection(pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
+ if (pg->notify_connection != NULL)
+ pg->notify_connection (pg->notify_connection_cls, first, second, distance, first_cfg, second_cfg, first_daemon, second_daemon, emsg);
}
+/**
+ * Either delay a connection (because there are too many outstanding)
+ * or schedule it for right now.
+ *
+ * @param cls a connection context
+ * @param tc the task runtime context
+ */
static void schedule_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct ConnectContext *connect_context = cls;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Delaying connect, we have too many outstanding connections!\n"));
#endif
- GNUNET_SCHEDULER_add_delayed(connect_context->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
+ GNUNET_SCHEDULER_add_delayed(connect_context->ct_ctx->pg->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_connect, connect_context);
}
else
{
CONNECT_TIMEOUT,
CONNECT_ATTEMPTS,
&internal_connect_notify,
- connect_context->pg);
+ connect_context->ct_ctx);
GNUNET_free(connect_context);
}
}
const GNUNET_HashCode * key,
void *value)
{
- struct PeerData *first = cls;
+ struct ConnectTopologyContext *ct_ctx = cls;
+ struct PeerData *first = ct_ctx->first;
struct GNUNET_TESTING_Daemon *second = value;
struct ConnectContext *connect_context;
connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
- connect_context->pg = first->pg;
connect_context->first = first->daemon;
connect_context->second = second;
+ connect_context->ct_ctx = ct_ctx;
GNUNET_SCHEDULER_add_now(first->pg->sched, &schedule_connect, connect_context);
return GNUNET_YES;
* @return the number of connections that will be attempted
*/
static int
-connect_topology (struct GNUNET_TESTING_PeerGroup *pg)
+connect_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion notify_callback, void *notify_cls)
{
unsigned int pg_iter;
int ret;
- int total;
+ unsigned int total;
+ struct ConnectTopologyContext *ct_ctx;
#if OLD
struct PeerConnection *connection_iter;
struct ConnectContext *connect_context;
#endif
total = 0;
+ ct_ctx = GNUNET_malloc(sizeof(struct ConnectTopologyContext));
+ ct_ctx->notify_connections_done = notify_callback;
+ ct_ctx->notify_cls = notify_cls;
+ ct_ctx->pg = pg;
+
for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
{
- ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, &pg->peers[pg_iter]);
- if (GNUNET_SYSERR == ret)
- return GNUNET_SYSERR;
+ total += GNUNET_CONTAINER_multihashmap_size(pg->peers[pg_iter].connect_peers);
+ }
+
+ if (total == 0)
+ {
+ GNUNET_free(ct_ctx);
+ return total;
+ }
+ ct_ctx->remaining_connections = total;
+ total = 0;
+ for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
+ {
+ ct_ctx->first = &pg->peers[pg_iter];
+ ret = GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].connect_peers, &connect_iterator, ct_ctx);
+ GNUNET_assert(GNUNET_SYSERR != ret && ret >= 0);
total = total + ret;
#if OLD
- connection_iter = ;
+ connection_iter = FIXME;
while (connection_iter != NULL)
{
connect_context = GNUNET_malloc(sizeof(struct ConnectContext));
connect_context->pg = pg;
- connect_context->first = ;
+ connect_context->first = FIXME;
connect_context->second = connection_iter->daemon;
GNUNET_SCHEDULER_add_now(pg->sched, &schedule_connect, connect_context);
connection_iter = connection_iter->next;
* @return the maximum number of connections were all allowed peers
* connected to each other
*/
-int
+unsigned int
GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
enum GNUNET_TESTING_Topology topology,
enum GNUNET_TESTING_Topology restrict_topology,
- char *restrict_transports)
+ const char *restrict_transports)
{
int ret;
- int num_connections;
+ unsigned int num_connections;
int unblacklisted_connections;
- GNUNET_assert (pg->notify_connection != NULL);
- ret = GNUNET_OK;
-
switch (topology)
{
case GNUNET_TESTING_TOPOLOGY_CLIQUE:
/* Use the create clique method to initially set all connections as blacklisted. */
if (restrict_topology != GNUNET_TESTING_TOPOLOGY_NONE)
create_clique (pg, &blacklist_connections);
+
unblacklisted_connections = 0;
/* Un-blacklist connections as per the topology specified */
switch (restrict_topology)
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Failed during blacklist file copying!\n"));
#endif
- return GNUNET_SYSERR;
+ return 0;
}
else
{
*/
static int
random_connect_iterator (void *cls,
- const GNUNET_HashCode * key,
- void *value)
+ const GNUNET_HashCode * key,
+ void *value)
{
struct RandomContext *random_ctx = cls;
double random_number;
return count;
}
+
+struct FindClosestContext
+{
+ /**
+ * The currently known closest peer.
+ */
+ struct GNUNET_TESTING_Daemon *closest;
+
+ /**
+ * The info for the peer we are adding connections for.
+ */
+ struct PeerData *curr_peer;
+
+ /**
+ * The distance (bits) between the current
+ * peer and the currently known closest.
+ */
+ unsigned int closest_dist;
+
+ /**
+ * The offset of the closest known peer in
+ * the peer group.
+ */
+ unsigned int closest_num;
+};
+
+/**
+ * Iterator over hash map entries of the allowed
+ * peer connections. Find the closest, not already
+ * connected peer and return it.
+ *
+ * @param cls closure (struct FindClosestContext)
+ * @param key current key code (hash of offset in pg)
+ * @param value value in the hash map - a GNUNET_TESTING_Daemon
+ * @return GNUNET_YES if we should continue to
+ * iterate,
+ * GNUNET_NO if not.
+ */
+static
+int find_closest_peers (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct FindClosestContext *closest_ctx = cls;
+ struct GNUNET_TESTING_Daemon *daemon = value;
+
+ if (((closest_ctx->closest == NULL) ||
+ (GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey) > closest_ctx->closest_dist))
+ && (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(closest_ctx->curr_peer->connect_peers, key)))
+ {
+ closest_ctx->closest_dist = GNUNET_CRYPTO_hash_matching_bits(&daemon->id.hashPubKey, &closest_ctx->curr_peer->daemon->id.hashPubKey);
+ closest_ctx->closest = daemon;
+ uid_from_hash(key, &closest_ctx->closest_num);
+ }
+ return GNUNET_YES;
+}
+
+/**
+ * From the set of connections possible, choose at num connections per
+ * peer based on depth which are closest out of those allowed. Guaranteed
+ * to add num peers to connect to, provided there are that many peers
+ * in the underlay topology to connect to.
+ *
+ * @param pg the peergroup we are dealing with
+ * @param num how many connections at least should each peer have (if possible)?
+ * @param proc processor to actually add the connections
+ */
+void
+add_closest (struct GNUNET_TESTING_PeerGroup *pg, unsigned int num, GNUNET_TESTING_ConnectionProcessor proc)
+{
+ struct FindClosestContext closest_ctx;
+ uint32_t pg_iter;
+ uint32_t i;
+
+ for (i = 0; i < num; i++) /* Each time find a closest peer (from those available) */
+ {
+ for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
+ {
+ closest_ctx.curr_peer = &pg->peers[pg_iter];
+ closest_ctx.closest = NULL;
+ closest_ctx.closest_dist = 0;
+ closest_ctx.closest_num = 0;
+ GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].allowed_peers, &find_closest_peers, &closest_ctx);
+ if (closest_ctx.closest != NULL)
+ {
+ GNUNET_assert((0 <= closest_ctx.closest_num) && (closest_ctx.closest_num < pg->total));
+ proc(pg, pg_iter, closest_ctx.closest_num);
+ }
+ }
+ }
+}
+
/**
* From the set of connections possible, choose at least num connections per
* peer based on depth first traversal of peer connections. If DFS leaves
}
/**
- * There are many ways to connect peers that are supported by this function.
- * To connect peers in the same topology that was created via the
- * GNUNET_TESTING_create_topology, the topology variable must be set to
+ * Internal callback for topology information for a particular peer.
+ */
+static void
+internal_topology_callback(void *cls,
+ const struct GNUNET_PeerIdentity *peer,
+ struct GNUNET_TIME_Relative latency, uint32_t distance)
+{
+ struct CoreContext *core_ctx = cls;
+ struct TopologyIterateContext *iter_ctx = core_ctx->iter_context;
+
+ if (peer == NULL) /* Either finished, or something went wrong */
+ {
+ iter_ctx->completed++;
+ iter_ctx->connected--;
+ /* One core context allocated per iteration, must free! */
+ GNUNET_free(core_ctx);
+ }
+ else
+ {
+ iter_ctx->topology_cb(iter_ctx->cls, &core_ctx->daemon->id, peer, latency, distance, NULL);
+ }
+
+ if (iter_ctx->completed == iter_ctx->total)
+ {
+ iter_ctx->topology_cb(iter_ctx->cls, NULL, NULL, GNUNET_TIME_relative_get_zero(), 0, NULL);
+ /* Once all are done, free the iteration context */
+ GNUNET_free(iter_ctx);
+ }
+}
+
+
+/**
+ * Check running topology iteration tasks, if below max start a new one, otherwise
+ * schedule for some time in the future.
+ */
+static void
+schedule_get_topology(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct CoreContext *core_context = cls;
+ struct TopologyIterateContext *topology_context = (struct TopologyIterateContext *)core_context->iter_context;
+ if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
+ return;
+
+ if (topology_context->connected > MAX_OUTSTANDING_CONNECTIONS)
+ {
+#if VERBOSE_TESTING > 2
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ _("Delaying connect, we have too many outstanding connections!\n"));
+#endif
+ GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_topology, core_context);
+ }
+ else
+ {
+#if VERBOSE_TESTING > 2
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
+#endif
+ topology_context->connected++;
+ if (GNUNET_OK != GNUNET_CORE_iterate_peers (core_context->daemon->sched, core_context->daemon->cfg, &internal_topology_callback, core_context))
+ internal_topology_callback(core_context, NULL, GNUNET_TIME_relative_get_zero(), 0);
+
+ }
+}
+
+/**
+ * Iterate over all (running) peers in the peer group, retrieve
+ * all connections that each currently has.
+ */
+void
+GNUNET_TESTING_get_topology (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyTopology cb, void *cls)
+{
+ struct TopologyIterateContext *topology_context;
+ struct CoreContext *core_ctx;
+ unsigned int i;
+ unsigned int total_count;
+
+ /* Allocate a single topology iteration context */
+ topology_context = GNUNET_malloc(sizeof(struct TopologyIterateContext));
+ topology_context->topology_cb = cb;
+ topology_context->cls = cls;
+ total_count = 0;
+ for (i = 0; i < pg->total; i++)
+ {
+ if (pg->peers[i].daemon->running == GNUNET_YES)
+ {
+ /* Allocate one core context per core we need to connect to */
+ core_ctx = GNUNET_malloc(sizeof(struct CoreContext));
+ core_ctx->daemon = pg->peers[i].daemon;
+ /* Set back pointer to topology iteration context */
+ core_ctx->iter_context = topology_context;
+ GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_topology, core_ctx);
+ total_count++;
+ }
+ }
+ topology_context->total = total_count;
+ return;
+}
+
+/**
+ * Callback function to process statistic values.
+ * This handler is here only really to insert a peer
+ * identity (or daemon) so the statistics can be uniquely
+ * tied to a single running peer.
+ *
+ * @param cls closure
+ * @param subsystem name of subsystem that created the statistic
+ * @param name the name of the datum
+ * @param value the current value
+ * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
+ * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
+ */
+static int internal_stats_callback (void *cls,
+ const char *subsystem,
+ const char *name,
+ uint64_t value,
+ int is_persistent)
+{
+ struct StatsCoreContext *core_context = cls;
+ struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
+
+ return stats_context->proc(stats_context->cls, &core_context->daemon->id, subsystem, name, value, is_persistent);
+}
+
+/**
+ * Internal continuation call for statistics iteration.
+ *
+ * @param cls closure, the CoreContext for this iteration
+ * @param success whether or not the statistics iterations
+ * was canceled or not (we don't care)
+ */
+static void internal_stats_cont (void *cls, int success)
+{
+ struct StatsCoreContext *core_context = cls;
+ struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
+
+ stats_context->connected--;
+ stats_context->completed++;
+
+ if (stats_context->completed == stats_context->total)
+ {
+ stats_context->cont(stats_context->cls, GNUNET_YES);
+ GNUNET_free(stats_context);
+ }
+
+ if (core_context->stats_handle != NULL)
+ GNUNET_STATISTICS_destroy(core_context->stats_handle, GNUNET_NO);
+
+ GNUNET_free(core_context);
+}
+
+/**
+ * Check running topology iteration tasks, if below max start a new one, otherwise
+ * schedule for some time in the future.
+ */
+static void
+schedule_get_statistics(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct StatsCoreContext *core_context = cls;
+ struct StatsIterateContext *stats_context = (struct StatsIterateContext *)core_context->iter_context;
+
+ if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
+ return;
+
+ if (stats_context->connected > MAX_OUTSTANDING_CONNECTIONS)
+ {
+#if VERBOSE_TESTING > 2
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ _("Delaying connect, we have too many outstanding connections!\n"));
+#endif
+ GNUNET_SCHEDULER_add_delayed(core_context->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_get_statistics, core_context);
+ }
+ else
+ {
+#if VERBOSE_TESTING > 2
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ _("Creating connection, outstanding_connections is %d\n"), outstanding_connects);
+#endif
+
+ stats_context->connected++;
+ core_context->stats_handle = GNUNET_STATISTICS_create(core_context->daemon->sched, "testing", core_context->daemon->cfg);
+ if (core_context->stats_handle == NULL)
+ {
+ internal_stats_cont (core_context, GNUNET_NO);
+ return;
+ }
+
+ core_context->stats_get_handle = GNUNET_STATISTICS_get(core_context->stats_handle, NULL, NULL, GNUNET_TIME_relative_get_forever(), &internal_stats_cont, &internal_stats_callback, core_context);
+ if (core_context->stats_get_handle == NULL)
+ internal_stats_cont (core_context, GNUNET_NO);
+
+ }
+}
+
+struct DuplicateStats
+{
+ /**
+ * Next item in the list
+ */
+ struct DuplicateStats *next;
+
+ /**
+ * Nasty string, concatenation of relevant information.
+ */
+ char *unique_string;
+};
+
+/**
+ * Check whether the combination of port/host/unix domain socket
+ * already exists in the list of peers being checked for statistics.
+ *
+ * @param pg the peergroup in question
+ * @param specific_peer the peer we're concerned with
+ * @param stats_list the list to return to the caller
+ *
+ * @return GNUNET_YES if the statistics instance has been seen already,
+ * GNUNET_NO if not (and we may have added it to the list)
+ */
+static int
+stats_check_existing(struct GNUNET_TESTING_PeerGroup *pg, struct PeerData *specific_peer, struct DuplicateStats **stats_list)
+{
+ struct DuplicateStats *pos;
+ char *unix_domain_socket;
+ unsigned long long port;
+ char *to_match;
+ if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_yesno(pg->cfg, "testing", "single_statistics_per_host"))
+ return GNUNET_NO; /* Each peer has its own statistics instance, do nothing! */
+
+ pos = *stats_list;
+ if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(specific_peer->cfg, "statistics", "unixpath", &unix_domain_socket))
+ return GNUNET_NO;
+
+ GNUNET_CONFIGURATION_get_value_number(specific_peer->cfg, "statistics", "port", &port);
+
+ if (specific_peer->daemon->hostname != NULL)
+ GNUNET_asprintf(&to_match, "%s%s%llu", specific_peer->daemon->hostname, unix_domain_socket, port);
+ else
+ GNUNET_asprintf(&to_match, "%s%llu", unix_domain_socket, port);
+
+ while (pos != NULL)
+ {
+ if (0 == strcmp(to_match, pos->unique_string))
+ {
+ GNUNET_free(to_match);
+ return GNUNET_YES;
+ }
+ pos = pos->next;
+ }
+ pos = GNUNET_malloc(sizeof(struct DuplicateStats));
+ pos->unique_string = to_match;
+ pos->next = *stats_list;
+ *stats_list = pos;
+ return GNUNET_NO;
+}
+
+/**
+ * Iterate over all (running) peers in the peer group, retrieve
+ * all statistics from each.
+ */
+void
+GNUNET_TESTING_get_statistics (struct GNUNET_TESTING_PeerGroup *pg,
+ GNUNET_STATISTICS_Callback cont,
+ GNUNET_TESTING_STATISTICS_Iterator proc, void *cls)
+{
+ struct StatsIterateContext *stats_context;
+ struct StatsCoreContext *core_ctx;
+ unsigned int i;
+ unsigned int total_count;
+ struct DuplicateStats *stats_list;
+ struct DuplicateStats *pos;
+ stats_list = NULL;
+
+ /* Allocate a single stats iteration context */
+ stats_context = GNUNET_malloc(sizeof(struct StatsIterateContext));
+ stats_context->cont = cont;
+ stats_context->proc = proc;
+ stats_context->cls = cls;
+ total_count = 0;
+
+ for (i = 0; i < pg->total; i++)
+ {
+ if ((pg->peers[i].daemon->running == GNUNET_YES) && (GNUNET_NO == stats_check_existing(pg, &pg->peers[i], &stats_list)))
+ {
+ /* Allocate one core context per core we need to connect to */
+ core_ctx = GNUNET_malloc(sizeof(struct StatsCoreContext));
+ core_ctx->daemon = pg->peers[i].daemon;
+ /* Set back pointer to topology iteration context */
+ core_ctx->iter_context = stats_context;
+ GNUNET_SCHEDULER_add_now(pg->sched, &schedule_get_statistics, core_ctx);
+ total_count++;
+ }
+ }
+
+ GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Retrieving stats from %u total instances.\n", total_count);
+ stats_context->total = total_count;
+ if (stats_list != NULL)
+ {
+ pos = stats_list;
+ while(pos != NULL)
+ {
+ GNUNET_free(pos->unique_string);
+ stats_list = pos->next;
+ GNUNET_free(pos);
+ pos = stats_list->next;
+ }
+ }
+ return;
+}
+
+/**
+ * There are many ways to connect peers that are supported by this function.
+ * To connect peers in the same topology that was created via the
+ * GNUNET_TESTING_create_topology, the topology variable must be set to
* GNUNET_TESTING_TOPOLOGY_NONE. If the topology variable is specified,
* a new instance of that topology will be generated and attempted to be
* connected. This could result in some connections being impossible,
* @param topology which topology to connect the peers in
* @param options options for connecting the topology
* @param option_modifier modifier for options that take a parameter
+ * @param notify_callback notification to be called once all connections completed
+ * @param notify_cls closure for notification callback
+ *
* @return the number of connections that will be attempted, GNUNET_SYSERR on error
*/
int
GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
enum GNUNET_TESTING_Topology topology,
enum GNUNET_TESTING_TopologyOption options,
- double option_modifier)
+ double option_modifier,
+ GNUNET_TESTING_NotifyCompletion notify_callback,
+ void *notify_cls)
{
switch (topology)
{
#endif
perform_dfs(pg, (int)option_modifier);
break;
+ case GNUNET_TESTING_TOPOLOGY_OPTION_ADD_CLOSEST:
+#if VERBOSE_TOPOLOGY
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("Finding additional %u closest peers each (if possible)\n"), (unsigned int)option_modifier);
+#endif
+ add_closest(pg, (unsigned int)option_modifier, &add_actual_connections);
+ break;
case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
break;
case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
break;
}
- return connect_topology(pg);
+ return connect_topology(pg, notify_callback, notify_cls);
}
/**
}
}
+
+/**
+ * Callback for informing us about a successful
+ * or unsuccessful churn start call.
+ *
+ * @param cls a ChurnContext
+ * @param id the peer identity of the started peer
+ * @param cfg the handle to the configuration of the peer
+ * @param d handle to the daemon for the peer
+ * @param emsg NULL on success, non-NULL on failure
+ *
+ */
+void
+churn_start_callback (void *cls,
+ const struct GNUNET_PeerIdentity *id,
+ const struct GNUNET_CONFIGURATION_Handle *cfg,
+ struct GNUNET_TESTING_Daemon *d,
+ const char *emsg)
+{
+ struct ChurnRestartContext *startup_ctx = cls;
+ struct ChurnContext *churn_ctx = startup_ctx->churn_ctx;
+
+ unsigned int total_left;
+ char *error_message;
+
+ error_message = NULL;
+ if (emsg != NULL)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Churn stop callback failed with error `%s'\n",
+ emsg);
+ churn_ctx->num_failed_start++;
+ }
+ else
+ {
+ churn_ctx->num_to_start--;
+ }
+
+#if DEBUG_CHURN
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+ "Started peer, %d left.\n",
+ churn_ctx->num_to_start);
+#endif
+
+ total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
+
+ if (total_left == 0)
+ {
+ if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
+ GNUNET_asprintf(&error_message,
+ "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
+ churn_ctx->num_failed_start,
+ churn_ctx->num_failed_stop);
+ churn_ctx->cb(churn_ctx->cb_cls, error_message);
+ GNUNET_free_non_null(error_message);
+ GNUNET_free(churn_ctx);
+ GNUNET_free(startup_ctx);
+ }
+}
+
+
+static void schedule_churn_restart(void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
+{
+ struct PeerRestartContext *peer_restart_ctx = cls;
+ struct ChurnRestartContext *startup_ctx = peer_restart_ctx->churn_restart_ctx;
+
+ if (startup_ctx->outstanding > MAX_CONCURRENT_STARTING)
+ GNUNET_SCHEDULER_add_delayed(peer_restart_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_restart, peer_restart_ctx);
+ else
+ {
+ GNUNET_TESTING_daemon_start_stopped(peer_restart_ctx->daemon,
+ startup_ctx->timeout,
+ &churn_start_callback,
+ startup_ctx);
+ GNUNET_free(peer_restart_ctx);
+ }
+}
+
static void
internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
{
internal_context->peer->cfg,
internal_context->timeout,
internal_context->hostname,
+ internal_context->username,
+ internal_context->sshport,
&internal_hostkey_callback,
internal_context,
&internal_startup_callback,
* @param cb_cls closure for cb
* @param connect_callback function to call each time two hosts are connected
* @param connect_callback_cls closure for connect_callback
- * @param hostnames space-separated list of hostnames to use; can be NULL (to run
- * everything on localhost).
+ * @param hostnames linked list of hosts to use to start peers on (NULL to run on localhost only)
+ *
* @return NULL on error, otherwise handle to control peer group
*/
struct GNUNET_TESTING_PeerGroup *
void *cb_cls,
GNUNET_TESTING_NotifyConnection
connect_callback, void *connect_callback_cls,
- const char *hostnames)
+ const struct GNUNET_TESTING_Host *hostnames)
{
struct GNUNET_TESTING_PeerGroup *pg;
- const char *rpos;
+ const struct GNUNET_TESTING_Host *hostpos;
+#if 0
char *pos;
+ const char *rpos;
char *start;
+#endif
const char *hostname;
+ const char *username;
char *baseservicehome;
char *newservicehome;
char *tmpdir;
unsigned int off;
unsigned int hostcnt;
uint16_t minport;
+ uint16_t sshport;
uint32_t upnum;
+ uint32_t fdnum;
if (0 == total)
{
return NULL;
}
upnum = 0;
+ fdnum = 0;
pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup));
pg->sched = sched;
pg->cfg = cfg;
pg->peers = GNUNET_malloc (total * sizeof (struct PeerData));
if (NULL != hostnames)
{
+ off = 0;
+ hostpos = hostnames;
+ while (hostpos != NULL)
+ {
+ hostpos = hostpos->next;
+ off++;
+ }
+ pg->hosts = GNUNET_malloc (off * sizeof (struct HostData));
+ off = 0;
+
+ hostpos = hostnames;
+ while (hostpos != NULL)
+ {
+ pg->hosts[off].minport = LOW_PORT;
+ pg->hosts[off].hostname = GNUNET_strdup(hostpos->hostname);
+ if (hostpos->username != NULL)
+ pg->hosts[off].username = GNUNET_strdup(hostpos->username);
+ pg->hosts[off].sshport = hostpos->port;
+ hostpos = hostpos->next;
+ off++;
+ }
+
+ if (off == 0)
+ {
+ pg->hosts = NULL;
+ }
+ hostcnt = off;
+ minport = 0;
+ pg->num_hosts = off;
+
+#if NO_LL
off = 2;
/* skip leading spaces */
while ((0 != *hostnames) && (isspace ( (unsigned char) *hostnames)))
}
hostcnt = off;
minport = 0; /* make gcc happy */
+#endif
}
else
{
if (hostcnt > 0)
{
hostname = pg->hosts[off % hostcnt].hostname;
+ username = pg->hosts[off % hostcnt].username;
+ sshport = pg->hosts[off % hostcnt].sshport;
pcfg = make_config (cfg,
&pg->hosts[off % hostcnt].minport,
&upnum,
- hostname);
+ hostname, &fdnum);
}
else
{
hostname = NULL;
+ username = NULL;
+ sshport = 0;
pcfg = make_config (cfg,
&minport,
&upnum,
- hostname);
+ hostname, &fdnum);
}
if (NULL == pcfg)
pg->peers[off].internal_context.peer = &pg->peers[off];
pg->peers[off].internal_context.timeout = timeout;
pg->peers[off].internal_context.hostname = hostname;
+ pg->peers[off].internal_context.username = username;
+ pg->peers[off].internal_context.sshport = sshport;
pg->peers[off].internal_context.hostkey_callback = hostkey_callback;
pg->peers[off].internal_context.hostkey_cls = hostkey_cls;
pg->peers[off].internal_context.start_cb = cb;
return NULL;
}
+/*
+ * Get a daemon by peer identity, so callers can
+ * retrieve the daemon without knowing it's offset.
+ *
+ * @param pg the peer group to retrieve the daemon from
+ * @param peer_id the peer identity of the daemon to retrieve
+ *
+ * @return the daemon on success, or NULL if no such peer identity is found
+ */
+struct GNUNET_TESTING_Daemon *
+GNUNET_TESTING_daemon_get_by_id (struct GNUNET_TESTING_PeerGroup *pg, struct GNUNET_PeerIdentity *peer_id)
+{
+ unsigned int i;
+
+ for (i = 0; i < pg->total; i ++)
+ {
+ if (0 == memcmp(&pg->peers[i].daemon->id, peer_id, sizeof(struct GNUNET_PeerIdentity)))
+ return pg->peers[i].daemon;
+ }
+
+ return NULL;
+}
+
/**
* Prototype of a function that will be called when a
* particular operation was completed the testing library.
void
churn_stop_callback (void *cls, const char *emsg)
{
- struct ChurnContext *churn_ctx = cls;
+ struct ShutdownContext *shutdown_ctx = cls;
+ struct ChurnContext *churn_ctx = shutdown_ctx->cb_cls;
unsigned int total_left;
char *error_message;
error_message = NULL;
+ shutdown_ctx->outstanding--;
+
if (emsg != NULL)
{
GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
churn_ctx->cb(churn_ctx->cb_cls, error_message);
GNUNET_free_non_null(error_message);
GNUNET_free(churn_ctx);
+ GNUNET_free(shutdown_ctx);
}
}
/**
- * Callback for informing us about a successful
- * or unsuccessful churn start call.
+ * Count the number of running peers.
*
- * @param cls a ChurnContext
- * @param id the peer identity of the started peer
- * @param cfg the handle to the configuration of the peer
- * @param d handle to the daemon for the peer
- * @param emsg NULL on success, non-NULL on failure
+ * @param pg handle for the peer group
*
+ * @return the number of currently running peers in the peer group
*/
-void
-churn_start_callback (void *cls,
- const struct GNUNET_PeerIdentity *id,
- const struct GNUNET_CONFIGURATION_Handle *cfg,
- struct GNUNET_TESTING_Daemon *d,
- const char *emsg)
+unsigned int
+GNUNET_TESTING_daemons_running (struct GNUNET_TESTING_PeerGroup *pg)
{
- struct ChurnContext *churn_ctx = cls;
- unsigned int total_left;
- char *error_message;
-
- error_message = NULL;
- if (emsg != NULL)
+ unsigned int i;
+ unsigned int running = 0;
+ for (i = 0; i < pg->total; i++)
+ {
+ if (pg->peers[i].daemon->running == GNUNET_YES)
{
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "Churn stop callback failed with error `%s'\n",
- emsg);
- churn_ctx->num_failed_start++;
+ GNUNET_assert(running != -1);
+ running++;
}
+ }
+ return running;
+}
+
+/**
+ * Task to rate limit the number of outstanding peer shutdown
+ * requests. This is necessary for making sure we don't do
+ * too many ssh connections at once, but is generally nicer
+ * to any system as well (graduated task starts, as opposed
+ * to calling gnunet-arm N times all at once).
+ */
+static void
+schedule_churn_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
+{
+ struct PeerShutdownContext *peer_shutdown_ctx = cls;
+ struct ShutdownContext *shutdown_ctx;
+
+ GNUNET_assert(peer_shutdown_ctx != NULL);
+ shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
+ GNUNET_assert(shutdown_ctx != NULL);
+
+ if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
+ GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_churn_shutdown_task, peer_shutdown_ctx);
else
{
- churn_ctx->num_to_start--;
+ shutdown_ctx->outstanding++;
+ GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, shutdown_ctx->cb, shutdown_ctx, GNUNET_NO, GNUNET_YES);
+ GNUNET_free(peer_shutdown_ctx);
}
-
-#if DEBUG_CHURN
- GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
- "Started peer, %d left.\n",
- churn_ctx->num_to_start);
-#endif
-
- total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
-
- if (total_left == 0)
- {
- if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
- GNUNET_asprintf(&error_message,
- "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!",
- churn_ctx->num_failed_start,
- churn_ctx->num_failed_stop);
- churn_ctx->cb(churn_ctx->cb_cls, error_message);
- GNUNET_free_non_null(error_message);
- GNUNET_free(churn_ctx);
- }
}
-
/**
* Simulate churn by stopping some peers (and possibly
* re-starting others if churn is called multiple times). This
void *cb_cls)
{
struct ChurnContext *churn_ctx;
+ struct ShutdownContext *shutdown_ctx;
+ struct PeerShutdownContext *peer_shutdown_ctx;
+ struct PeerRestartContext *peer_restart_ctx;
+ struct ChurnRestartContext *churn_startup_ctx;
+
unsigned int running;
unsigned int stopped;
unsigned int total_running;
}
}
+ GNUNET_assert(running >= voff);
+ if (voff > 0)
+ {
+ shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
+ shutdown_ctx->cb = &churn_stop_callback;
+ shutdown_ctx->cb_cls = churn_ctx;
+ shutdown_ctx->total_peers = voff;
+ shutdown_ctx->timeout = timeout;
+ }
+
for (i = 0; i < voff; i++)
{
#if DEBUG_CHURN
GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
#endif
+ GNUNET_assert(running_arr != NULL);
+ peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
+ peer_shutdown_ctx->daemon = pg->peers[running_arr[running_permute[i]]].daemon;
+ peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
+ GNUNET_SCHEDULER_add_now(peer_shutdown_ctx->daemon->sched, &schedule_churn_shutdown_task, peer_shutdown_ctx);
+
+ /*
GNUNET_TESTING_daemon_stop (pg->peers[running_arr[running_permute[i]]].daemon,
timeout,
&churn_stop_callback, churn_ctx,
- GNUNET_NO, GNUNET_YES);
+ GNUNET_NO, GNUNET_YES); */
}
+ GNUNET_assert(stopped >= von);
+ if (von > 0)
+ {
+ churn_startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
+ churn_startup_ctx->churn_ctx = churn_ctx;
+ churn_startup_ctx->timeout = timeout;
+ }
for (i = 0; i < von; i++)
{
#if DEBUG_CHURN
GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
#endif
+ GNUNET_assert(stopped_arr != NULL);
+ peer_restart_ctx = GNUNET_malloc(sizeof(struct PeerRestartContext));
+ peer_restart_ctx->churn_restart_ctx = churn_startup_ctx;
+ peer_restart_ctx->daemon = pg->peers[stopped_arr[stopped_permute[i]]].daemon;
+ GNUNET_SCHEDULER_add_now(peer_restart_ctx->daemon->sched, &schedule_churn_restart, peer_restart_ctx);
+ /*
GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon,
- timeout, &churn_start_callback, churn_ctx);
+ timeout, &churn_start_callback, churn_ctx);*/
}
GNUNET_free_non_null(running_arr);
* @param callback_cls closure for the callback function
*/
void
-GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg, GNUNET_TESTING_NotifyCompletion callback, void *callback_cls)
+GNUNET_TESTING_daemons_restart (struct GNUNET_TESTING_PeerGroup *pg,
+ GNUNET_TESTING_NotifyCompletion callback,
+ void *callback_cls)
{
struct RestartContext *restart_context;
unsigned int off;
GNUNET_TESTING_NotifyCompletion cb,
void *cb_cls)
{
+ struct ShutdownContext *shutdown_ctx;
+ struct ChurnRestartContext *startup_ctx;
struct ChurnContext *churn_ctx;
if (GNUNET_NO == desired_status)
{
if (NULL != pg->peers[offset].daemon)
{
+ shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
churn_ctx->num_to_start = 0;
churn_ctx->num_to_stop = 1;
churn_ctx->cb = cb;
- churn_ctx->cb_cls = cb_cls;
+ churn_ctx->cb_cls = cb_cls;
+ shutdown_ctx->cb_cls = churn_ctx;
GNUNET_TESTING_daemon_stop(pg->peers[offset].daemon,
- timeout, &churn_stop_callback, churn_ctx,
+ timeout, &churn_stop_callback, shutdown_ctx,
GNUNET_NO, GNUNET_YES);
}
}
{
if (NULL == pg->peers[offset].daemon)
{
+ startup_ctx = GNUNET_malloc(sizeof(struct ChurnRestartContext));
churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
churn_ctx->num_to_start = 1;
churn_ctx->num_to_stop = 0;
churn_ctx->cb = cb;
churn_ctx->cb_cls = cb_cls;
+ startup_ctx->churn_ctx = churn_ctx;
GNUNET_TESTING_daemon_start_stopped(pg->peers[offset].daemon,
- timeout, &churn_start_callback, churn_ctx);
+ timeout, &churn_start_callback, startup_ctx);
}
}
else
* @param emsg NULL on success
*/
void internal_shutdown_callback (void *cls,
- const char *emsg)
+ const char *emsg)
{
struct ShutdownContext *shutdown_ctx = cls;
+ shutdown_ctx->outstanding--;
if (emsg == NULL)
{
shutdown_ctx->peers_down++;
}
}
+
+/**
+ * Task to rate limit the number of outstanding peer shutdown
+ * requests. This is necessary for making sure we don't do
+ * too many ssh connections at once, but is generally nicer
+ * to any system as well (graduated task starts, as opposed
+ * to calling gnunet-arm N times all at once).
+ */
+static void
+schedule_shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
+{
+ struct PeerShutdownContext *peer_shutdown_ctx = cls;
+ struct ShutdownContext *shutdown_ctx;
+
+ GNUNET_assert(peer_shutdown_ctx != NULL);
+ shutdown_ctx = peer_shutdown_ctx->shutdown_ctx;
+ GNUNET_assert(shutdown_ctx != NULL);
+
+ if (shutdown_ctx->outstanding > MAX_CONCURRENT_SHUTDOWN)
+ GNUNET_SCHEDULER_add_delayed(peer_shutdown_ctx->daemon->sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100), &schedule_shutdown_task, peer_shutdown_ctx);
+ else
+ {
+ shutdown_ctx->outstanding++;
+ GNUNET_TESTING_daemon_stop (peer_shutdown_ctx->daemon, shutdown_ctx->timeout, &internal_shutdown_callback, shutdown_ctx, GNUNET_YES, GNUNET_NO);
+ GNUNET_free(peer_shutdown_ctx);
+ }
+}
+
/**
* Shutdown all peers started in the given group.
*
{
unsigned int off;
struct ShutdownContext *shutdown_ctx;
- GNUNET_TESTING_NotifyCompletion shutdown_cb;
- void *shutdown_cb_cls;
+ struct PeerShutdownContext *peer_shutdown_ctx;
GNUNET_assert(pg->total > 0);
- shutdown_cb = NULL;
- shutdown_ctx = NULL;
-
- if (cb != NULL)
- {
- shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
- shutdown_ctx->cb = cb;
- shutdown_ctx->cb_cls = cb_cls;
- shutdown_ctx->total_peers = pg->total;
- shutdown_cb = &internal_shutdown_callback;
- shutdown_cb_cls = cb_cls;
- }
+ shutdown_ctx = GNUNET_malloc(sizeof(struct ShutdownContext));
+ shutdown_ctx->cb = cb;
+ shutdown_ctx->cb_cls = cb_cls;
+ shutdown_ctx->total_peers = pg->total;
+ shutdown_ctx->timeout = timeout;
+ /* shtudown_ctx->outstanding = 0; */
for (off = 0; off < pg->total; off++)
{
GNUNET_assert(NULL != pg->peers[off].daemon);
- GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
+ peer_shutdown_ctx = GNUNET_malloc(sizeof(struct PeerShutdownContext));
+ peer_shutdown_ctx->daemon = pg->peers[off].daemon;
+ peer_shutdown_ctx->shutdown_ctx = shutdown_ctx;
+ GNUNET_SCHEDULER_add_now(pg->peers[off].daemon->sched, &schedule_shutdown_task, peer_shutdown_ctx);
+ //GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, shutdown_cb, shutdown_ctx, GNUNET_YES, GNUNET_NO);
if (NULL != pg->peers[off].cfg)
GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);
if (pg->peers[off].allowed_peers != NULL)
GNUNET_CONTAINER_multihashmap_destroy(pg->peers[off].blacklisted_peers);
}
GNUNET_free (pg->peers);
- if (NULL != pg->hosts)
+ for (off = 0; off < pg->num_hosts; off++)
{
- GNUNET_free (pg->hosts[0].hostname);
- GNUNET_free (pg->hosts);
+ GNUNET_free (pg->hosts[off].hostname);
+ GNUNET_free_non_null (pg->hosts[off].username);
}
+ GNUNET_free_non_null (pg->hosts);
GNUNET_free (pg);
}