- distribute peers equally among island nodes on SuperMUC
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
index ea1a1a5704b91ff47827396db02eae61a6cd28e4..99625cba05b55f51865193d20d669191d8a806f0 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2012 Christian Grothoff (and other contributing authors)
+      (C) 2008--2013 Christian Grothoff (and other contributing authors)
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
 #define LOG_DEBUG(...)                          \
   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
 
+/**
+ * Prints API violation message
+ */
+#define API_VIOLATION(cond,errstr)              \
+  do {                                          \
+    if (cond)                                   \
+      break;                                    \
+    LOG (GNUNET_ERROR_TYPE_ERROR, "API violation detected: %s\n", errstr); \
+    GNUNET_assert (0);                                                  \
+  } while (0)
+
+/**
+ * Log an error message at log-level 'level' that indicates a failure of the
+ * command 'cmd' with the message given by gai_strerror(rc).
+ */
+#define LOG_GAI(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gai_strerror(rc)); } while(0)
+
 /**
  * Number of extra elements we create space for when we grow host list
  */
@@ -109,16 +126,6 @@ struct TimeSlot
 struct GNUNET_TESTBED_Host
 {
 
-  /**
-   * The next pointer for DLL
-   */
-  struct GNUNET_TESTBED_Host *next;
-
-  /**
-   * The prev pointer for DLL
-   */
-  struct GNUNET_TESTBED_Host *prev;
-
   /**
    * The hostname of the host; NULL for localhost
    */
@@ -132,7 +139,8 @@ struct GNUNET_TESTBED_Host
   /**
    * the configuration to use as a template while starting a controller on this
    * host.  Operation queue size specific to a host are also read from this
-   * configuration handle
+   * configuration handle.  After starting the controller, it points to the actual
+   * configuration with which the controller is running
    */
   struct GNUNET_CONFIGURATION_Handle *cfg;
 
@@ -175,10 +183,15 @@ struct GNUNET_TESTBED_Host
   unsigned int tslots_filled;
 
   /**
-   * Is a controller started on this host?
+   * Is a controller started on this host? FIXME: Is this needed?
    */
   int controller_started;
 
+  /**
+   * Is this host locked by GNUNET_TESTBED_controller_start()?
+   */
+  int locked;
+
   /**
    * Global ID we use to refer to a host on the network
    */
@@ -319,6 +332,21 @@ GNUNET_TESTBED_host_get_cfg_ (const struct GNUNET_TESTBED_Host *host)
 }
 
 
+/**
+ * Function to replace host's configuration
+ *
+ * @param host the host handle
+ * @param new_cfg the new configuration to replace the old one
+ */
+void
+GNUNET_TESTBED_host_replace_cfg_ (struct GNUNET_TESTBED_Host *host,
+                                  const struct GNUNET_CONFIGURATION_Handle *new_cfg)
+{
+  GNUNET_CONFIGURATION_destroy (host->cfg);
+  host->cfg = GNUNET_CONFIGURATION_dup (new_cfg);
+}
+
+
 /**
  * Create a host to run peers and controllers on.
  *
@@ -458,7 +486,7 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
     {
       data[offset] = '\0';
       ret =
-          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
+          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.-]:%5hd", username,
                   hostname, &port);
       if (3 == ret)
       {
@@ -492,6 +520,201 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
 }
 
 
+/**
+ * Resolves a hostname using getaddrinfo
+ *
+ * @param host the hostname
+ * @return the string representing the IPv4 address of the given host; NULL upon error
+ */
+const char *
+simple_resolve (const char *host)
+{
+  struct addrinfo *res;
+  const struct sockaddr_in *in_addr; 
+  char *hostip;
+  struct addrinfo hint;
+  unsigned int rc;
+
+  hint.ai_family = AF_INET;    /* IPv4 */
+  hint.ai_socktype = 0;
+  hint.ai_protocol = 0;
+  hint.ai_addrlen = 0;
+  hint.ai_addr = NULL;
+  hint.ai_canonname = NULL;
+  hint.ai_next = NULL;
+  hint.ai_flags = AI_NUMERICSERV;
+  res = NULL;
+  LOG_DEBUG ("Resolving [%s]\n", host);
+  if (0 != (rc = getaddrinfo (host, "22", &hint, &res)))
+  {
+    LOG_GAI (GNUNET_ERROR_TYPE_ERROR, "getaddrinfo", rc);
+    return NULL;
+  }
+  GNUNET_assert (NULL != res);
+  GNUNET_assert (NULL != res->ai_addr);
+  GNUNET_assert (sizeof (struct sockaddr_in) == res->ai_addrlen);
+  in_addr = (const struct sockaddr_in *) res->ai_addr;
+  hostip = inet_ntoa (in_addr->sin_addr);
+  GNUNET_assert (NULL != hostip);
+  freeaddrinfo (res);
+  LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
+  return hostip;
+}
+
+#if ENABLE_LL
+static int
+cmpstringp(const void *p1, const void *p2)
+{
+  /* The actual arguments to this function are "pointers to
+     pointers to char", but strcmp(3) arguments are "pointers
+     to char", hence the following cast plus dereference */
+  
+  return strcmp(* (char * const *) p1, * (char * const *) p2);
+}
+#endif
+
+/**
+ * Loads the set of host allocated by the LoadLeveler Job Scheduler.  This
+ * function is only available when compiled with support for LoadLeveler and is
+ * used for running on the SuperMUC
+ *
+ * @param cfg the configuration to use as a template while starting a controller
+ *          on any of the loaded hosts.  Operation queue sizes specific to a host
+ *          are also read from this configuration handle
+ * @param hosts set to the hosts found in the file; caller must free this if
+ *          number of hosts returned is greater than 0
+ * @return number of hosts returned in 'hosts', 0 on error
+ */
+unsigned int
+GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
+                                            GNUNET_CONFIGURATION_Handle *cfg,
+                                            struct GNUNET_TESTBED_Host ***hosts)
+{
+#if !ENABLE_LL
+  LOG (GNUNET_ERROR_TYPE_ERROR, 
+       _("The function %s is only available when compiled with (--with-ll)\n"),
+       __func__);
+  GNUNET_assert (0);
+#else
+  const char *hostfile;
+  char *buf;
+  char *hostname;
+  char **hostnames;
+  struct GNUNET_TESTBED_Host **host_list;
+  ssize_t rsize;
+  uint64_t size;
+  uint64_t offset;
+  enum {
+    SCAN,
+    SKIP,
+    TRIM,
+    READHOST
+  } pstep;
+  unsigned int host;
+  unsigned int nhosts;
+  
+  if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
+  {
+    GNUNET_break (0);
+    return 0;
+  }
+  if (GNUNET_SYSERR == GNUNET_DISK_file_size (hostfile, &size, GNUNET_YES,
+                                              GNUNET_YES))
+  {
+    GNUNET_break (0);
+    return 0;
+  }
+  if (0 == size)
+  {
+    GNUNET_break (0);
+    return 0;
+  }
+  buf = GNUNET_malloc (size + 1);
+  rsize = GNUNET_DISK_fn_read (hostfile, buf, (size_t) size);
+  if ( (GNUNET_SYSERR == rsize) || ((ssize_t) size != rsize) )
+  {
+    GNUNET_free (buf);
+    GNUNET_break (0);
+    return 0;
+  }
+  size++;
+  offset = 0;
+  pstep = SCAN;
+  hostname = NULL;
+  hostnames = NULL;
+  nhosts = 0;
+  while (offset < size)
+  {
+    switch (pstep)
+    {
+    case SCAN:
+      if ('!' == buf[offset])
+        pstep = SKIP;
+      else 
+        pstep = TRIM;
+      break;
+    case SKIP:
+      if ('\n' == buf[offset])
+        pstep = SCAN;
+      break;
+    case TRIM:
+      if ('!' == buf[offset])
+      {
+        pstep = SKIP;
+        break;
+      }
+      if ( (' ' == buf[offset]) 
+           || ('\t' == buf[offset])
+           || ('\r' == buf[offset]) )
+        pstep = TRIM;
+      else
+      {
+        pstep = READHOST;
+        hostname = &buf[offset];        
+      }
+      break;
+    case READHOST:
+      if (isspace (buf[offset]))
+      {
+        buf[offset] = '\0';
+        for (host = 0; host < nhosts; host++)
+          if (0 == strcmp (hostnames[host], hostname))
+            break;
+        if (host == nhosts)
+        {
+          LOG_DEBUG ("Adding host [%s]\n", hostname);
+          hostname = GNUNET_strdup (hostname);
+          GNUNET_array_append (hostnames, nhosts, hostname);
+        }
+        else
+          LOG_DEBUG ("Not adding host [%s] as it is already included\n", hostname);
+        hostname = NULL;
+        pstep = SCAN;
+      }
+      break;
+    }
+    offset++;
+  }
+  GNUNET_free_non_null (buf);
+  if (NULL == hostnames)
+    return 0;
+  if (NULL == hosts)
+    goto cleanup;
+  qsort (hostnames, nhosts, sizeof (hostnames[0]), cmpstringp);
+  host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhosts);
+  for (host = 0; host < nhosts; host++)
+    host_list[host] = GNUNET_TESTBED_host_create (hostnames[host], NULL, cfg, 0);
+  *hosts = host_list;
+
+ cleanup:
+  for (host = 0; host < nhosts; host++)
+    GNUNET_free (hostnames[host]);
+  GNUNET_free(hostnames);
+  return nhosts;
+#endif
+}
+
+
 /**
  * Destroy a host handle.  Must only be called once everything
  * running on that host has been stopped.
@@ -566,6 +789,32 @@ GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
 }
 
 
+/**
+ * Unmarks a host registered at a controller
+ *
+ * @param host the host to unmark
+ * @param controller the controller at which this host has to be unmarked
+ */
+void
+GNUNET_TESTBED_deregister_host_at_ (struct GNUNET_TESTBED_Host *host,
+                                    const struct GNUNET_TESTBED_Controller
+                                    *const controller)
+{
+  struct RegisteredController *rc;
+
+  for (rc = host->rc_head; NULL != rc; rc=rc->next)
+    if (controller == rc->controller)
+      break;
+  if (NULL == rc)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
+  GNUNET_free (rc);
+}
+
+
 /**
  * Checks whether a host has been registered
  *
@@ -631,11 +880,6 @@ struct GNUNET_TESTBED_ControllerProc
    */
   struct GNUNET_MessageHeader *msg;
 
-  /**
-   * The configuration of the running testbed service
-   */
-  struct GNUNET_CONFIGURATION_Handle *cfg;
-
 };
 
 
@@ -818,6 +1062,7 @@ helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
   struct GNUNET_TESTBED_ControllerProc *cp = cls;
   const struct GNUNET_TESTBED_HelperReply *msg;
   const char *hostname;
+  const char *hostip;
   char *config;
   uLongf config_size;
   uLongf xconfig_size;
@@ -835,18 +1080,25 @@ helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
   GNUNET_assert (Z_OK ==
                  uncompress ((Bytef *) config, &config_size,
                              (const Bytef *) &msg[1], xconfig_size));
-  GNUNET_assert (NULL == cp->cfg);
-  cp->cfg = GNUNET_CONFIGURATION_create ();
+  /* Replace the configuration template present in the host with the
+     controller's running configuration */
+  GNUNET_CONFIGURATION_destroy (cp->host->cfg);
+  cp->host->cfg = GNUNET_CONFIGURATION_create ();
   GNUNET_assert (GNUNET_CONFIGURATION_deserialize
-                 (cp->cfg, config, config_size, GNUNET_NO));
+                 (cp->host->cfg, config, config_size, GNUNET_NO));
   GNUNET_free (config);
   if ((NULL == cp->host) ||
       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname (cp->host))))
     hostname = "localhost";
+  hostip = simple_resolve (hostname);
+  if (NULL == hostip)
+    hostip = "127.0.0.1";  
   /* Change the hostname so that we can connect to it */
-  GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
-                                         hostname);
-  cp->cb (cp->cls, cp->cfg, GNUNET_OK);
+  GNUNET_CONFIGURATION_set_value_string (cp->host->cfg, "testbed", "hostname",
+                                         hostip);
+  cp->host->locked = GNUNET_NO;
+  cp->host->controller_started = GNUNET_YES;
+  cp->cb (cp->cls, cp->host->cfg, GNUNET_OK);
   return GNUNET_OK;
 }
 
@@ -893,18 +1145,19 @@ helper_exp_cb (void *cls)
 
 
 /**
- * Starts a controller process at the given host
+ * Starts a controller process at the given host.  The given host's configration
+ * is used as a Template configuration to use for the remote controller; the
+ * remote controller will be started with a slightly modified configuration
+ * (port numbers, unix domain sockets and service home values are changed as per
+ * TESTING library on the remote host).  The modified configuration replaces the
+ * host's existing configuration before signalling success through the
+ * GNUNET_TESTBED_ControllerStatusCallback()
  *
  * @param trusted_ip the ip address of the controller which will be set as TRUSTED
  *          HOST(all connections form this ip are permitted by the testbed) when
  *          starting testbed controller at host. This can either be a single ip
  *          address or a network address in CIDR notation.
- * @param host the host where the controller has to be started; NULL for
- *          localhost
- * @param cfg template configuration to use for the remote controller; the
- *          remote controller will be started with a slightly modified
- *          configuration (port numbers, unix domain sockets and service home
- *          values are changed as per TESTING library on the remote host)
+ * @param host the host where the controller has to be started.  CANNOT be NULL.
  * @param cb function called when the controller is successfully started or
  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
  *          called if cb is called with GNUNET_SYSERR as status. Will never be
@@ -917,21 +1170,27 @@ helper_exp_cb (void *cls)
 struct GNUNET_TESTBED_ControllerProc *
 GNUNET_TESTBED_controller_start (const char *trusted_ip,
                                  struct GNUNET_TESTBED_Host *host,
-                                 const struct GNUNET_CONFIGURATION_Handle *cfg,
                                  GNUNET_TESTBED_ControllerStatusCallback cb,
                                  void *cls)
 {
   struct GNUNET_TESTBED_ControllerProc *cp;
   struct GNUNET_TESTBED_HelperInit *msg;
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
   const char *hostname;
-
   static char *const binary_argv[] = {
     HELPER_TESTBED_BINARY, NULL
   };
-
-  hostname = NULL;  
+  
+  GNUNET_assert (NULL != host);
+  GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host)));
+  hostname = NULL;
+  API_VIOLATION (GNUNET_NO == host->locked,
+                 "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()");
+  host->locked = GNUNET_YES;
+  API_VIOLATION (GNUNET_NO == host->controller_started,
+                 "Attempting to start a controller on a host which is already started a controller");
   cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
-  if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
+  if (0 == GNUNET_TESTBED_host_get_id_ (host))
   {
     cp->helper =
         GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
@@ -1000,28 +1259,57 @@ GNUNET_TESTBED_controller_start (const char *trusted_ip,
 
 
 /**
- * Stop the controller process (also will terminate all peers and controllers
- * dependent on this controller).  This function blocks until the testbed has
- * been fully terminated (!). The controller status cb from
- * GNUNET_TESTBED_controller_start() will not be called.
+ * Sends termination signal to the controller's helper process
  *
- * @param cproc the controller process handle
+ * @param cproc the handle to the controller's helper process
  */
 void
-GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
+GNUNET_TESTBED_controller_kill_ (struct GNUNET_TESTBED_ControllerProc *cproc)
 {
   if (NULL != cproc->shandle)
     GNUNET_HELPER_send_cancel (cproc->shandle);
   if (NULL != cproc->helper)
-    GNUNET_HELPER_soft_stop (cproc->helper);
-  if (NULL != cproc->cfg)
-    GNUNET_CONFIGURATION_destroy (cproc->cfg);
+    GNUNET_HELPER_kill (cproc->helper, GNUNET_YES);
+}
+
+
+/**
+ * Cleans-up the controller's helper process handle
+ *
+ * @param cproc the handle to the controller's helper process
+ */
+void
+GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
+{
+  if (NULL != cproc->helper)
+  {
+    GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper));
+    GNUNET_HELPER_destroy (cproc->helper);
+  }
   if (NULL != cproc->helper_argv)
     free_argv (cproc->helper_argv);
+  cproc->host->controller_started = GNUNET_NO;
+  cproc->host->locked = GNUNET_NO;
   GNUNET_free (cproc);
 }
 
 
+/**
+ * Stop the controller process (also will terminate all peers and controllers
+ * dependent on this controller).  This function blocks until the testbed has
+ * been fully terminated (!). The controller status cb from
+ * GNUNET_TESTBED_controller_start() will not be called.
+ *
+ * @param cproc the controller process handle
+ */
+void
+GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
+{
+  GNUNET_TESTBED_controller_kill_ (cproc);
+  GNUNET_TESTBED_controller_destroy_ (cproc);
+}
+
+
 /**
  * The handle for whether a host is habitable or not
  */
@@ -1032,11 +1320,6 @@ struct GNUNET_TESTBED_HostHabitableCheckHandle
    */
   const struct GNUNET_TESTBED_Host *host;
 
-  /* /\** */
-  /*  * the configuration handle to lookup the path of the testbed helper */
-  /*  *\/ */
-  /* const struct GNUNET_CONFIGURATION_Handle *cfg; */
-
   /**
    * The callback to call once we have the status
    */
@@ -1113,6 +1396,7 @@ call_cb:
   cb = h->cb;
   cb_cls = h->cb_cls;
   host = h->host;
+  free_argv (h->helper_argv);
   GNUNET_free (h);
   if (NULL != cb)
     cb (cb_cls, host, ret);
@@ -1169,10 +1453,11 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
   stat_args[0] = "stat";
   stat_args[2] = NULL;
   rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
+  GNUNET_free (stat_args[1]);
   h->helper_argv = join_argv ((const char **) rsh_args,
                               (const char **) rsh_suffix_args);
-  GNUNET_free (rsh_suffix_args);
-  GNUNET_free (rsh_args);
+  free_argv (rsh_suffix_args);
+  free_argv (rsh_args);
   h->auxp =
       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
                                    NULL, h->helper_argv[0], h->helper_argv);