- add underlay api implementation
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_channel.c
index ec73d67e7b2bd90335c11eed2c8f8ffa0592d4e0..2e52a2ea5da6898985240fe92b830533c0e543ef 100644 (file)
@@ -34,7 +34,8 @@
 
 #define LOG(level, ...) GNUNET_log_from(level,"mesh-chn",__VA_ARGS__)
 
-#define MESH_RETRANSMIT_TIME    GNUNET_TIME_UNIT_SECONDS
+#define MESH_RETRANSMIT_TIME    GNUNET_TIME_relative_multiply(\
+                                    GNUNET_TIME_UNIT_MILLISECONDS, 250)
 #define MESH_RETRANSMIT_MARGIN  4
 
 
@@ -315,7 +316,7 @@ extern GNUNET_PEER_Id myid;
  *                    is skewed by the retransmission, count only for the
  *                    retransmitted message.
  */
-static void
+static int
 rel_message_free (struct MeshReliableMessage *copy, int update_time);
 
 /**
@@ -337,6 +338,23 @@ send_ack (struct MeshChannel *ch, int fwd);
 
 
 
+/**
+ * Test if the channel is loopback: both root and dest are on the local peer.
+ *
+ * @param ch Channel to test.
+ *
+ * @return #GNUNET_YES if channel is loopback, #GNUNET_NO otherwise.
+ */
+static int
+is_loopback (const struct MeshChannel *ch)
+{
+  if (NULL != ch->t)
+    return GMT_is_loopback (ch->t);
+
+  return (NULL != ch->root && NULL != ch->dest);
+}
+
+
 /**
  * We have received a message out of order, or the client is not ready.
  * Buffer it until we receive an ACK from the client or the missing
@@ -385,6 +403,7 @@ add_buffered_data (const struct GNUNET_MESH_Data *msg,
     LOG (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n");
 }
 
+
 /**
  * Add a destination client to a channel, initializing all data structures
  * in the channel and the client.
@@ -411,6 +430,7 @@ add_destination (struct MeshChannel *ch, struct MeshClient *c)
   ch->dest_rel = GNUNET_new (struct MeshChannelReliability);
   ch->dest_rel->ch = ch;
   ch->dest_rel->expected_delay.rel_value_us = 0;
+  ch->dest_rel->retry_timer = MESH_RETRANSMIT_TIME;
 
   ch->dest = c;
 }
@@ -709,27 +729,38 @@ ch_message_sent (void *cls,
   switch (ch_q->type)
   {
     case GNUNET_MESSAGE_TYPE_MESH_DATA:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %u %s (c: %p, q: %p)\n",
-           copy->mid, GM_m2s (type), copy, copy->q);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT DATA MID %u\n", copy->mid);
       GNUNET_assert (ch_q == copy->q);
       copy->timestamp = GNUNET_TIME_absolute_get ();
       rel = copy->rel;
       if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task)
       {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! scheduling retry in %s\n",
+             GNUNET_STRINGS_relative_time_to_string (rel->expected_delay,
+                                                     GNUNET_YES));
         if (0 != rel->expected_delay.rel_value_us)
         {
+          LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay != 0\n");
           rel->retry_timer =
           GNUNET_TIME_relative_multiply (rel->expected_delay,
                                          MESH_RETRANSMIT_MARGIN);
         }
         else
         {
+          LOG (GNUNET_ERROR_TYPE_DEBUG, "!! delay reset\n");
           rel->retry_timer = MESH_RETRANSMIT_TIME;
         }
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! using delay %s\n",
+             GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
+                                                     GNUNET_NO));
         rel->retry_task =
             GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
                                           &channel_retransmit_message, rel);
       }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "!! retry task %u\n", rel->retry_task);
+      }
       copy->q = NULL;
       break;
 
@@ -737,7 +768,7 @@ ch_message_sent (void *cls,
     case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GM_m2s (type));
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! SENT %s\n", GM_m2s (ch_q->type));
       rel = ch_q->rel;
       GNUNET_assert (rel->uniq == ch_q);
       rel->uniq = NULL;
@@ -746,6 +777,9 @@ ch_message_sent (void *cls,
           && GNUNET_MESSAGE_TYPE_MESH_DATA_ACK != type)
       {
         GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rel->retry_task);
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! STD BACKOFF %s\n",
+             GNUNET_STRINGS_relative_time_to_string (rel->retry_timer,
+                                                     GNUNET_NO));
         rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer);
         rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer,
                                                         &channel_recreate, rel);
@@ -782,7 +816,7 @@ send_create (struct MeshChannel *ch)
 
 
 /**
- * Confirm we got a channel create, FWD ack.
+ * Confirm we got a channel create or FWD ack.
  *
  * @param ch The channel to confirm.
  * @param fwd Should we send a FWD ACK? (going dest->root)
@@ -803,6 +837,24 @@ send_ack (struct MeshChannel *ch, int fwd)
 }
 
 
+/**
+ * Send a message and don't keep any info about it: we won't need to cancel it
+ * or resend it.
+ *
+ * @param msg Header of the message to fire away.
+ * @param ch Channel on which the message should go.
+ * @param force Is this a forced (undroppable) message?
+ */
+static void
+fire_and_forget (const struct GNUNET_MessageHeader *msg,
+                 struct MeshChannel *ch,
+                 int force)
+{
+  GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, force,
+                                                   NULL, NULL));
+}
+
+
 /**
  * Notify that a channel create didn't succeed.
  *
@@ -877,6 +929,7 @@ channel_rel_free_all (struct MeshChannelReliability *rel)
     next = copy->next;
     GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy);
     LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH RECV %p\n", copy);
+    GNUNET_break (NULL == copy->q);
     GNUNET_free (copy);
   }
   for (copy = rel->head_sent; NULL != copy; copy = next)
@@ -884,14 +937,31 @@ channel_rel_free_all (struct MeshChannelReliability *rel)
     next = copy->next;
     GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
     LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE BATCH %p\n", copy);
+    if (NULL != copy->q)
+    {
+      if (NULL != copy->q->q)
+      {
+        GMT_cancel (copy->q->q);
+        /* ch_message_sent will free copy->q */
+      }
+      else
+      {
+        GNUNET_free (copy->q);
+        GNUNET_break (0);
+      }
+    }
     GNUNET_free (copy);
   }
+  if (NULL != rel->uniq && NULL != rel->uniq->q)
+  {
+    GMT_cancel (rel->uniq->q);
+    /* ch_message_sent is called freeing uniq */
+  }
   if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
   {
     GNUNET_SCHEDULER_cancel (rel->retry_task);
+    rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != rel->uniq)
-    GMT_cancel (rel->uniq->q);
   GNUNET_free (rel);
 }
 
@@ -960,7 +1030,7 @@ channel_rel_free_sent (struct MeshChannelReliability *rel,
 
     /* Now copy->mid == target, free it */
     next = copy->next;
-    rel_message_free (copy, GNUNET_YES);
+    GNUNET_break (GNUNET_YES != rel_message_free (copy, GNUNET_YES));
     copy = next;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n");
@@ -977,8 +1047,11 @@ channel_rel_free_sent (struct MeshChannelReliability *rel,
  *                    If this message is ACK in a batch the timing information
  *                    is skewed by the retransmission, count only for the
  *                    retransmitted message.
+ *
+ * @return #GNUNET_YES if channel was destroyed as a result of the call,
+ *         #GNUNET_NO otherwise.
  */
-static void
+static int
 rel_message_free (struct MeshReliableMessage *copy, int update_time)
 {
   struct MeshChannelReliability *rel;
@@ -1009,19 +1082,23 @@ rel_message_free (struct MeshReliableMessage *copy, int update_time)
     LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! batch free, ignoring timing\n");
   }
   rel->ch->pending_messages--;
-  if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
-  {
-    struct MeshTunnel3 *t = rel->ch->t;
-    GMCH_destroy (rel->ch);
-    GMT_destroy_if_empty (t);
-  }
   if (NULL != copy->q)
   {
     GMT_cancel (copy->q->q);
+    /* copy->q is set to NULL by ch_message_sent */
   }
   GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy);
   LOG (GNUNET_ERROR_TYPE_DEBUG, " COPYFREE %p\n", copy);
   GNUNET_free (copy);
+
+  if (GNUNET_NO != rel->ch->destroy && 0 == rel->ch->pending_messages)
+  {
+    struct MeshTunnel3 *t = rel->ch->t;
+    GMCH_destroy (rel->ch);
+    GMT_destroy_if_empty (t);
+    return GNUNET_YES;
+  }
+  return GNUNET_NO;
 }
 
 
@@ -1035,16 +1112,22 @@ static void
 channel_confirm (struct MeshChannel *ch, int fwd)
 {
   struct MeshChannelReliability *rel;
+  enum MeshChannelState oldstate;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-              "  channel confirm %s %s:%X\n",
-              GM_f2s (fwd), GMT_2s (ch->t), ch->gid);
+              "  channel confirm %s %s\n",
+              GM_f2s (fwd), GMCH_2s (ch));
+  oldstate = ch->state;
   ch->state = MESH_CHANNEL_READY;
 
   rel = fwd ? ch->root_rel : ch->dest_rel;
   rel->client_ready = GNUNET_YES;
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "  !! retry timer confirm %s\n",
+       GNUNET_STRINGS_relative_time_to_string (rel->retry_timer, GNUNET_NO));
   rel->expected_delay = rel->retry_timer;
-  send_client_ack (ch, fwd);
+  if (GMT_get_connections_buffer (ch->t) > 0 || GMT_is_loopback (ch->t))
+    send_client_ack (ch, fwd);
 
   if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
   {
@@ -1054,12 +1137,15 @@ channel_confirm (struct MeshChannel *ch, int fwd)
   else if (NULL != rel->uniq)
   {
     GMT_cancel (rel->uniq->q);
-    /* ch_sent_message will free and NULL uniq */
+    /* ch_message_sent will free and NULL uniq */
   }
   else
   {
-    /* We SHOULD have been trying to retransmit this! */
-    GNUNET_break (0);
+    if (GNUNET_NO == is_loopback (ch))
+    {
+      /* We SHOULD have been trying to retransmit this! */
+      GNUNET_break (oldstate == MESH_CHANNEL_READY);
+    }
   }
 
   /* In case of a FWD ACK (SYNACK) send a BCK ACK (ACK). */
@@ -1141,23 +1227,6 @@ channel_new (struct MeshTunnel3 *t,
 }
 
 
-/**
- * Test if the channel is loopback: both root and dest are on the local peer.
- *
- * @param ch Channel to test.
- *
- * @return #GNUNET_YES if channel is loopback, #NGUNET_NO otherwise.
- */
-static int
-is_loopback (const struct MeshChannel *ch)
-{
-  if (NULL != ch->t)
-    return GMT_is_loopback (ch->t);
-
-  return (NULL != ch->root && NULL != ch->dest);
-}
-
-
 /**
  * Handle a loopback message: call the appropriate handler for the message type.
  *
@@ -1235,6 +1304,9 @@ GMCH_destroy (struct MeshChannel *ch)
 
   if (NULL == ch)
     return;
+  if (2 == ch->destroy)
+    return; /* recursive call */
+  ch->destroy = 2;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "destroying channel %s:%u\n",
               GMT_2s (ch->t), ch->gid);
@@ -1471,10 +1543,20 @@ GMCH_allow_client (struct MeshChannel *ch, int fwd)
       GNUNET_break (GNUNET_NO != ch->destroy);
       return;
     }
-    if (NULL != rel->head_sent && 64 <= rel->mid_send - rel->head_sent->mid)
+    if (NULL != rel->head_sent)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
-      return;
+      if (64 <= rel->mid_send - rel->head_sent->mid)
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, " too big MID gap! Wait for ACK.\n");
+        return;
+      }
+      else
+        LOG (GNUNET_ERROR_TYPE_DEBUG, " gap ok: %u - %u\n",
+             rel->head_sent->mid, rel->mid_send);
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_DEBUG, " head sent is NULL\n");
     }
   }
 
@@ -1624,14 +1706,14 @@ GMCH_handle_local_data (struct MeshChannel *ch,
   if (is_loopback (ch))
   {
     if (GMCH_get_buffer (ch, fwd) > 0)
-      send_client_ack (ch, fwd);
+      GMCH_allow_client (ch, fwd);
 
     return GNUNET_OK;
   }
 
   if (GMT_get_connections_buffer (ch->t) > 0)
   {
-    send_client_ack (ch, fwd);
+    GMCH_allow_client (ch, fwd);
   }
 
   return GNUNET_OK;
@@ -1735,7 +1817,7 @@ GMCH_handle_local_create (struct MeshClient *c,
   /* In unreliable channels, we'll use the DLL to buffer BCK data */
   ch->root_rel = GNUNET_new (struct MeshChannelReliability);
   ch->root_rel->ch = ch;
-  ch->root_rel->retry_timer = GNUNET_TIME_UNIT_SECONDS;
+  ch->root_rel->retry_timer = MESH_RETRANSMIT_TIME;
   ch->root_rel->expected_delay.rel_value_us = 0;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "CREATED CHANNEL %s\n", GMCH_2s (ch));
@@ -1787,6 +1869,23 @@ GMCH_handle_data (struct MeshChannel *ch,
     return;
   }
 
+  if (MESH_CHANNEL_READY != ch->state)
+  {
+    if (GNUNET_NO == fwd)
+    {
+      /* If we are the root, this means the other peer has sent traffic before
+       * receiving our ACK. Even if the SYNACK goes missing, no traffic should
+       * be sent before the ACK.
+       */
+      GNUNET_break_op (0);
+      return;
+    }
+    /* If we are the dest, this means that the SYNACK got to the root but
+     * the ACK went missing. Treat this as an ACK.
+     */
+    channel_confirm (ch, GNUNET_NO);
+  }
+
   GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO);
 
   mid = ntohl (msg->mid);
@@ -1896,7 +1995,8 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
     work = GNUNET_YES;
     LOG (GNUNET_ERROR_TYPE_DEBUG, " !!  id %u\n", copy->mid);
     next = copy->next;
-    rel_message_free (copy, GNUNET_YES);
+    if (GNUNET_YES == rel_message_free (copy, GNUNET_YES))
+      return;
   }
 
   /* ACK client if needed */
@@ -1908,6 +2008,7 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
     if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task)
     {
       GNUNET_SCHEDULER_cancel (rel->retry_task);
+      rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
       if (NULL != rel->head_sent && NULL == rel->head_sent->q)
       {
         struct GNUNET_TIME_Absolute new_target;
@@ -1923,10 +2024,6 @@ GMCH_handle_data_ack (struct MeshChannel *ch,
                                           &channel_retransmit_message,
                                           rel);
       }
-      else /* either no more traffic to ack or traffic has just been queued */
-      {
-        rel->retry_task = GNUNET_SCHEDULER_NO_TASK;
-      }
     }
     else /* work was done but no task was pending? shouldn't happen! */
     {
@@ -1951,50 +2048,63 @@ GMCH_handle_create (struct MeshTunnel3 *t,
   MESH_ChannelNumber chid;
   struct MeshChannel *ch;
   struct MeshClient *c;
+  int new_channel;
 
   chid = ntohl (msg->chid);
-
   ch = GMT_get_channel (t, chid);
   if (NULL == ch)
   {
     /* Create channel */
     ch = channel_new (t, NULL, 0);
     ch->gid = chid;
+    channel_set_options (ch, ntohl (msg->opt));
+    new_channel = GNUNET_YES;
+  }
+  else
+  {
+    new_channel = GNUNET_NO;
   }
-  channel_set_options (ch, ntohl (msg->opt));
 
-  /* Find a destination client */
-  ch->port = ntohl (msg->port);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "   port %u\n", ch->port);
-  c = GML_client_get_by_port (ch->port);
-  if (NULL == c)
+  if (GNUNET_YES == new_channel || GMT_is_loopback (t))
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  no client has port registered\n");
-    if (is_loopback (ch))
+    /* Find a destination client */
+    ch->port = ntohl (msg->port);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "   port %u\n", ch->port);
+    c = GML_client_get_by_port (ch->port);
+    if (NULL == c)
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  loopback: destroy on handler\n");
-      send_nack (ch);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  no client has port registered\n");
+      if (is_loopback (ch))
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "  loopback: destroy on handler\n");
+        send_nack (ch);
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_DEBUG, "  not loopback: destroy now\n");
+        send_nack (ch);
+        GMCH_destroy (ch);
+      }
+      return NULL;
     }
     else
     {
-      LOG (GNUNET_ERROR_TYPE_DEBUG, "  not loopback: destroy now\n");
-      send_nack (ch);
-      GMCH_destroy (ch);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "  client %p has port registered\n", c);
     }
-    return NULL;
+
+    add_destination (ch, c);
+    if (GNUNET_YES == ch->reliable)
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
+    else
+      LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
+
+    send_client_create (ch);
+    ch->state =  MESH_CHANNEL_SENT;
   }
   else
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "  client %p has port registered\n", c);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "  duplicate create channel\n");
   }
-
-  add_destination (ch, c);
-  if (GNUNET_YES == ch->reliable)
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n");
-  else
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "!!! Not Reliable\n");
-
-  send_client_create (ch);
   send_ack (ch, GNUNET_YES);
 
   return ch;
@@ -2090,17 +2200,6 @@ GMCH_handle_destroy (struct MeshChannel *ch,
 }
 
 
-void
-fire_and_forget (struct GNUNET_MessageHeader *msg,
-                 struct MeshChannel *ch,
-                 int force)
-{
-  GNUNET_break (NULL == GMT_send_prebuilt_message (msg, ch->t, ch,
-                                                   GNUNET_YES, force,
-                                                   NULL, NULL));
-}
-
-
 /**
  * Sends an already built message on a channel.
  *
@@ -2172,10 +2271,11 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
         }
         LOG (GNUNET_ERROR_TYPE_DEBUG, "  new q: %p\n", q);
         q->copy->q = q;
-        q->q = GMT_send_prebuilt_message (message, ch->t, ch,
-                                          fwd, NULL != existing_copy,
+        q->q = GMT_send_prebuilt_message (message, ch->t,
+                                          NULL != existing_copy,
                                           &ch_message_sent, q);
         /* q itself is stored in copy */
+        GNUNET_assert (NULL != q->q || GNUNET_NO != ch->destroy);
       }
       else
       {
@@ -2209,8 +2309,7 @@ GMCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
           GNUNET_free (q->rel->uniq);
         }
       }
-      q->q = GMT_send_prebuilt_message (message, ch->t, ch,
-                                        fwd, GNUNET_YES,
+      q->q = GMT_send_prebuilt_message (message, ch->t, GNUNET_YES,
                                         &ch_message_sent, q);
       q->rel->uniq = q;
       break;