-fixing source port randomization for DNS service
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses_mlp.c
index fa4d3a106c08688114bc93b4d366671b073d38a1..602925e1f5ab3b0a954d243b4455bae9207bead0 100644 (file)
@@ -147,6 +147,52 @@ mlp_status_to_string (int retcode)
   return "unknown error";
 }
 
+/**
+ * Translate ATS properties to text
+ * Just intended for debugging
+ *
+ * @param retcode return code
+ * @return string with result
+ */
+const char *
+mlp_ats_to_string (int ats_index)
+{
+  switch (ats_index) {
+    case GNUNET_ATS_ARRAY_TERMINATOR:
+      return "GNUNET_ATS_ARRAY_TERMINATOR";
+      break;
+    case GNUNET_ATS_UTILIZATION_UP:
+      return "GNUNET_ATS_UTILIZATION_UP";
+      break;
+    case GNUNET_ATS_UTILIZATION_DOWN:
+      return "GNUNET_ATS_UTILIZATION_DOWN";
+      break;
+    case GNUNET_ATS_COST_LAN:
+      return "GNUNET_ATS_COST_LAN";
+      break;
+    case GNUNET_ATS_COST_WAN:
+      return "GNUNET_ATS_COST_LAN";
+      break;
+    case GNUNET_ATS_COST_WLAN:
+      return "GNUNET_ATS_COST_WLAN";
+      break;
+    case GNUNET_ATS_NETWORK_TYPE:
+      return "GNUNET_ATS_NETWORK_TYPE";
+      break;
+    case GNUNET_ATS_QUALITY_NET_DELAY:
+      return "GNUNET_ATS_QUALITY_NET_DELAY";
+      break;
+    case GNUNET_ATS_QUALITY_NET_DISTANCE:
+      return "GNUNET_ATS_QUALITY_NET_DISTANCE";
+      break;
+    default:
+      return "unknown";
+      break;
+  }
+  GNUNET_break (0);
+  return "unknown error";
+}
+
 /**
  * Find a peer in the DLL
  * @param the peer to find
@@ -302,6 +348,34 @@ create_constraint_it (void *cls, const GNUNET_HashCode * key, void *value)
   return GNUNET_OK;
 }
 
+/**
+ * Find the required ATS information for an address
+ *
+ * @param addr the address
+ * @param ats_index the desired ATS index
+ *
+ * @return the index on success, otherwise GNUNET_SYSERR
+ */
+
+static int
+mlp_lookup_ats (struct ATS_Address *addr, int ats_index)
+{
+  struct GNUNET_ATS_Information * ats = addr->ats;
+  int c;
+  int found = GNUNET_NO;
+  for (c = 0; c < addr->ats_count; c++)
+  {
+    if (ats[c].type == ats_index)
+    {
+      found = GNUNET_YES;
+      break;
+    }
+  }
+  if (found == GNUNET_YES)
+    return c;
+  else
+    return GNUNET_SYSERR;
+}
 
 /**
  * Adds the problem constraints for all addresses
@@ -520,6 +594,26 @@ mlp_add_constraints_all_addresses (struct GAS_MLP_Handle *mlp, struct GNUNET_CON
       while (addr != NULL)
       {
         mlpi = addr->mlp_information;
+        /* lookup ATS information */
+        int index = mlp_lookup_ats(addr, mlp->q[c]);
+
+        if (index != GNUNET_SYSERR)
+        {
+          value = (double) addr->ats[index].value;
+#if DEBUG_ATS
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Quality %i with ATS property `%s' has index %i in addresses ats information has value %f\n", c,  mlp_ats_to_string(mlp->q[c]), index, (double) addr->ats[index].value);
+#endif
+        }
+#if DEBUG_ATS
+        else
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Quality %i with ATS property `%s' not existing\n", c,  mlp_ats_to_string(mlp->q[c]), index);
+#endif
+        mlpi = addr->mlp_information;
+
+        mlpi->r_q[c] = mlp->r_q[c];
+        mlpi->c_q[c] = mlpi->c_b;
+        mlpi->q[c] = value;
+
         ia[mlp->ci] = mlp->r_q[c];
         ja[mlp->ci] = mlpi->c_b;
         ar[mlp->ci] = p->f * value;
@@ -835,7 +929,7 @@ mlp_solve_mlp_problem (struct GAS_MLP_Handle *mlp)
   return GNUNET_OK;
 }
 
-int mlp_solve_problem (struct GAS_MLP_Handle *mlp);
+int GAS_mlp_solve_problem (struct GAS_MLP_Handle *mlp);
 
 static void
 mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -851,7 +945,7 @@ mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Scheduled problem solving\n");
 #endif
   if (mlp->addr_in_problem != 0)
-    mlp_solve_problem(mlp);
+    GAS_mlp_solve_problem(mlp);
 }
 
 
@@ -862,7 +956,7 @@ mlp_scheduler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return GNUNET_OK if could be solved, GNUNET_SYSERR on failure
  */
 int
-mlp_solve_problem (struct GAS_MLP_Handle *mlp)
+GAS_mlp_solve_problem (struct GAS_MLP_Handle *mlp)
 {
   int res;
   mlp->last_execution = GNUNET_TIME_absolute_get ();
@@ -1039,6 +1133,7 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   mlp->stats = (struct GNUNET_STATISTICS_Handle *) stats;
   mlp->max_iterations = max_iterations;
   mlp->max_exec_duration = max_duration;
+  mlp->auto_solve = GNUNET_YES;
 
   /* Redirect GLPK output to GNUnet logging */
   glp_error_hook((void *) mlp, &mlp_term_hook);
@@ -1076,6 +1171,7 @@ GAS_mlp_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
   return mlp;
 }
 
+
 /**
  * Updates a single address in the MLP problem
  *
@@ -1095,7 +1191,6 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
 {
   int new;
   struct MLP_information *mlpi;
-  int c;
 
   GNUNET_STATISTICS_update (mlp->stats,"# LP address updates", 1, GNUNET_NO);
 
@@ -1109,6 +1204,15 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
   if (new == GNUNET_YES)
   {
     mlpi = GNUNET_malloc (sizeof (struct MLP_information));
+
+    int c;
+    for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
+    {
+      mlpi->c_q[c] = 0;
+      mlpi->r_q[c] = 0;
+      mlpi->q[c] = 0.0;
+    }
+
     address->mlp_information = mlpi;
     mlp->addr_in_problem ++;
 
@@ -1117,12 +1221,14 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
     if (peer == NULL)
     {
 #if DEBUG_ATS
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding new peer `%s'\n", GNUNET_i2s (&address->peer));
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding new peer `%s'\n",
+          GNUNET_i2s (&address->peer));
 #endif
       peer = GNUNET_malloc (sizeof (struct ATS_Peer));
       peer->head = NULL;
       peer->tail = NULL;
 
+      int c;
       for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
       {
         peer->f_q[c] = 1.0;
@@ -1139,7 +1245,8 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
     else
     {
 #if DEBUG_ATS
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding address to peer `%s'\n", GNUNET_i2s (&address->peer));
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding address to peer `%s'\n",
+          GNUNET_i2s (&address->peer));
 #endif
       GNUNET_CONTAINER_DLL_insert (peer->head, peer->tail, address);
     }
@@ -1147,10 +1254,71 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
   else
   {
 #if DEBUG_ATS
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating existing address to peer `%s'\n", GNUNET_i2s (&address->peer));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating existing address to peer `%s'\n",
+        GNUNET_i2s (&address->peer));
 #endif
     mlpi = address->mlp_information;
+    int c;
+    for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
+    {
+      int index = mlp_lookup_ats(address, mlp->q[c]);
+      if ((index != GNUNET_SYSERR) && (mlpi->c_q[c] != 0) && (mlpi->r_q[c] != 0))
+      {
+        if (mlpi->q[c] == (double) address->ats[index].value)
+          break;
+#if DEBUG_ATS
+        GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Updating address for peer `%s' value `%s'from %f to %f\n",
+            GNUNET_i2s (&address->peer),
+            mlp_ats_to_string(mlp->q[c]),
+            mlpi->q[c],
+            (double) address->ats[index].value);
+#endif
+        switch (mlp->q[c])
+        {
+          case GNUNET_ATS_QUALITY_NET_DELAY:
+            mlpi->q[c] = (double) address->ats[index].value;
+            break;
+          case GNUNET_ATS_QUALITY_NET_DISTANCE:
+            mlpi->q[c] = (double) address->ats[index].value;
+            break;
+          default:
+            break;
+        }
+
+        /* Get current number of columns */
+        int cols = glp_get_num_cols(mlp->prob);
+        int *ind = GNUNET_malloc (cols * sizeof (int));
+        double *val = GNUNET_malloc (cols * sizeof (double));
+
+        /* Get the matrix row of quality */
+        cols = glp_get_mat_row(mlp->prob, mlp->r_q[c], ind, val);
+
+        int c2;
+        /* Get the index if matrix row of quality */
+        for (c2 = 1; c2 <= cols; c2++ )
+        {
+#if DEBUG_ATS
+          GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Existing element column %i : %f\n",
+            ind[c2], val[c2]);
+#endif
+          if ((mlpi->c_b == ind[c2]) && (val[c2] != mlpi->q[c]))
+          {
+            /* Update the value */
+            val[c2] = mlpi->q[c];
+#if DEBUG_ATS
+            GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "New element column %i : %f\n",
+                ind[c2], val[c2]);
+#endif
+        }
+      }
 
+      /* Get the index if matrix row of quality */
+      glp_set_mat_row (mlp->prob, mlpi->r_q[c], cols, ind, val);
+
+      GNUNET_free (ind);
+      GNUNET_free (val);
+      }
+    }
   }
 
   /* Recalculate */
@@ -1160,7 +1328,8 @@ GAS_mlp_address_update (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
     mlp_create_problem (mlp, addresses);
     mlp->presolver_required = GNUNET_YES;
   }
-  mlp_solve_problem (mlp);
+  if (mlp->auto_solve == GNUNET_YES)
+    GAS_mlp_solve_problem (mlp);
 }
 
 /**
@@ -1213,7 +1382,8 @@ GAS_mlp_address_delete (struct GAS_MLP_Handle *mlp, struct GNUNET_CONTAINER_Mult
 
     /* Recalculate */
     mlp->presolver_required = GNUNET_YES;
-    mlp_solve_problem (mlp);
+    if (mlp->auto_solve == GNUNET_YES)
+      GAS_mlp_solve_problem (mlp);
   }
 }
 
@@ -1248,6 +1418,8 @@ GAS_mlp_done (struct GAS_MLP_Handle *mlp)
   struct ATS_Peer * peer;
   struct ATS_Peer * tmp;
 
+  GNUNET_assert (mlp != NULL);
+
   if (mlp->mlp_task != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_SCHEDULER_cancel(mlp->mlp_task);
@@ -1255,16 +1427,13 @@ GAS_mlp_done (struct GAS_MLP_Handle *mlp)
   }
 
   /* clean up peer list */
-  if (mlp != NULL)
+  peer = mlp->peer_head;
+  while (peer != NULL)
   {
-    peer = mlp->peer_head;
-    while (peer != NULL)
-    {
-      GNUNET_CONTAINER_DLL_remove(mlp->peer_head, mlp->peer_tail, peer);
-      tmp = peer->next;
-      GNUNET_free (peer);
-      peer = tmp;
-    }
+    GNUNET_CONTAINER_DLL_remove(mlp->peer_head, mlp->peer_tail, peer);
+    tmp = peer->next;
+    GNUNET_free (peer);
+    peer = tmp;
   }
   mlp_delete_problem (mlp);