* - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
* - partial disconnect reporting -- same as error reporting?
* - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
- * - speed requirement specification (change?) in mesh API -- API call
* - add ping message
* - relay corking down to core
* - set ttl relative to tree depth
+ * - Add data ACK count in path ACK
* TODO END
*/
#define MESH_DEBUG_DHT GNUNET_YES
#define MESH_DEBUG_CONNECTION GNUNET_NO
+#define INITIAL_WINDOW_SIZE 2
+#define ACK_THRESHOLD INITIAL_WINDOW_SIZE / 2
+
#if MESH_DEBUG_CONNECTION
#define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
#else
/** FWD declaration */
struct MeshPeerInfo;
+struct MeshClient;
/**
};
-struct MeshClient; /* FWD declaration */
-
/**
* Struct containing all information regarding a tunnel
* For an intermediate node the improtant info used will be:
MESH_TunnelNumber local_tid_dest;
/**
- * Local count ID of the last packet seen/sent.
+ * Is the speed on the tunnel limited to the slowest peer?
*/
- uint32_t pid;
+ int speed_min;
/**
- * SKIP value for this tunnel.
+ * Is the tunnel bufferless (minimum latency)?
*/
- uint32_t skip;
+ int nobuffer;
/**
- * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
+ * Packet ID of the last fwd packet seen (sent/retransmitted/received).
*/
- struct GNUNET_CONTAINER_MultiHashMap *children_fc;
+ uint32_t fwd_pid;
/**
- * Last ACK.
+ * Packet ID of the last bck packet sent (unique counter per hop).
*/
- uint32_t last_ack;
+ uint32_t bck_pid;
- /**
- * Maximum child ACK.
+ /**
+ * SKIP value for this tunnel.
*/
- uint32_t max_child_ack;
+ uint32_t skip;
/**
- * How many messages are in the queue.
+ * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
+ * Contains the Flow Control info: FWD ACK value received,
+ * last BCK ACK sent, PID and SKIP values.
*/
- unsigned int queue_n;
+ struct GNUNET_CONTAINER_MultiHashMap *children_fc;
/**
- * How many messages do we accept in the queue.
+ * Last ACK sent towards the origin (for traffic towards leaf node).
*/
- unsigned int queue_max;
+ uint32_t last_fwd_ack;
+
+ /**
+ * BCK ACK value received from the hop towards the owner of the tunnel,
+ * (previous node / owner): up to what message PID can we sent back to him.
+ */
+ uint32_t bck_ack;
/**
- * Is the speed on the tunnel limited to the slowest peer?
+ * How many messages are in the forward queue (towards leaves).
*/
- int speed_min;
+ unsigned int fwd_queue_n;
/**
- * Is the tunnel bufferless (minimum latency)?
+ * How many messages do we accept in the forward queue.
*/
- int nobuffer;
+ unsigned int fwd_queue_max;
/**
- * Flag to signal the destruction of the tunnel.
- * If this is set GNUNET_YES the tunnel will be destroyed
- * when the queue is empty.
+ * How many messages are in the backward queue (towards origin).
*/
- int destroy;
+ unsigned int bck_queue_n;
+
+ /**
+ * How many messages do we accept in the backward queue.
+ */
+ unsigned int bck_queue_max;
/**
* Last time the tunnel was used
struct MeshClient *owner;
/**
- * Clients that have been informed about the tunnel, if any
+ * Clients that have been informed about and want to stay in the tunnel.
*/
struct MeshClient **clients;
/**
- * Number of elements in clients
+ * Flow control info for each client.
+ */
+ struct MeshTunnelClientInfo *clients_fc;
+
+ /**
+ * Number of elements in clients/clients_fc
*/
unsigned int nclients;
/**
- * Clients that have requested to leave the tunnel
+ * Clients that have been informed but requested to leave the tunnel.
*/
struct MeshClient **ignore;
*/
struct MeshRegexSearchContext *regex_ctx;
- /**
- * Task to keep the used paths alive
- */
+ /**
+ * Task to keep the used paths alive
+ */
GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
- /**
- * Task to destroy the tunnel after timeout
- *
- * FIXME: merge the two? a tunnel will have either
- * a path refresh OR a timeout, never both!
- */
+ /**
+ * Task to destroy the tunnel after timeout
+ *
+ * FIXME: merge the two? a tunnel will have either
+ * a path refresh OR a timeout, never both!
+ */
GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+
+ /**
+ * Flag to signal the destruction of the tunnel.
+ * If this is set GNUNET_YES the tunnel will be destroyed
+ * when the queue is empty.
+ */
+ int destroy;
};
GNUNET_PEER_Id id;
/**
- * SKIP value
+ * SKIP value.
*/
uint32_t skip;
uint32_t pid;
/**
- * Maximum PID allowed.
+ * Maximum PID allowed (FWD ACK received).
+ */
+ uint32_t fwd_ack;
+
+ /**
+ * Last ACK sent to that child (BCK ACK).
+ */
+ uint32_t bck_ack;
+};
+
+
+/**
+ * Info about a leaf client of a tunnel, needed to perform flow control.
+ */
+struct MeshTunnelClientInfo
+{
+ /**
+ * PID of the last packet sent to the client (FWD).
+ */
+ uint32_t fwd_pid;
+
+ /**
+ * PID of the last packet received from the client (BCK).
+ */
+ uint32_t bck_pid;
+
+ /**
+ * Maximum PID allowed (FWD ACK received).
+ */
+ uint32_t fwd_ack;
+
+ /**
+ * Last ACK sent to that child (BCK ACK).
+ */
+ uint32_t bck_ack;
+};
+
+
+
+/**
+ * Info collected during iteration of child nodes in order to get the ACK value
+ * for a tunnel.
+ */
+struct MeshTunnelChildIteratorContext
+{
+ /**
+ * Tunnel whose info is being collected.
+ */
+ struct MeshTunnel *t;
+
+ /**
+ * Is this context initialized? Is the value in max_child_ack valid?
+ */
+ int init;
+
+ /**
+ * Maximum child ACK so far.
*/
- uint32_t max_pid;
+ uint32_t max_child_ack;
+
+ /**
+ * Number of children nodes
+ */
+ unsigned int nchildren;
};
+
/**
* Info needed to work with tunnel paths and peers
*/
/**
- * Get the current ack value for a tunnel, taking in account the tunnel
- * mode and the status of all children nodes.
+ * Get the current ack value for a tunnel, for data going from root to leaves,
+ * taking in account the tunnel mode and the status of all children and clients.
+ *
+ * @param t Tunnel.
+ *
+ * @return Maximum PID allowed.
+ */
+static uint32_t
+tunnel_get_fwd_ack (struct MeshTunnel *t);
+
+/**
+ * Get the current ack value for a tunnel, for data going from leaves to root,
+ * taking in account the tunnel mode and the status of all children and clients.
*
* @param t Tunnel.
*
* @return Maximum PID allowed.
*/
static uint32_t
-tunnel_get_ack (struct MeshTunnel *t);
+tunnel_get_bck_ack (struct MeshTunnel *t);
+
+/**
+ * Add a client to a tunnel, initializing all needed data structures.
+ *
+ * @param t Tunnel to which add the client.
+ * @param c Client which to add to the tunnel.
+ */
+static void
+tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
/**
/****************** GENERAL HELPER FUNCTIONS ************************/
/******************************************************************************/
-
/**
* Decrements the reference counter and frees all resources if needed
*
{
struct GNUNET_HashCode hash;
- GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
+ GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
GNUNET_break (GNUNET_YES ==
GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
&hash, t));
&hash,
t));
}
-
}
type = ntohs (payload->type);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
+ GNUNET_MESH_DEBUG_M2S (type));
memcpy (cbuf, msg, sizeof (cbuf));
switch (htons (msg->type))
tmsg.tunnel_id = htonl (t->local_tid_dest);
GNUNET_SERVER_notification_context_unicast (nc, c->handle,
&tmsg.header, GNUNET_NO);
- GNUNET_array_append (t->clients, t->nclients, c);
+ tunnel_add_client (t, c);
GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
&hash);
GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
GNUNET_SERVER_notification_context_unicast (nc, c->handle,
(struct GNUNET_MessageHeader
- *) cbuf, GNUNET_YES);
+ *) cbuf, GNUNET_NO);
}
}
return count;
/**
- * Notify a client about how many more payload packages will we accept
- * on a given tunnel.
+ * Notify a client of a tunnel about how many more
+ * payload packages will we accept on a given tunnel,
+ * distinguishing between root and leaf clients.
*
- * @param c Client.
- * @param t Tunnel.
+ * @param c Client whom to send the ACK.
+ * @param t Tunnel on which to send the ACK.
*/
static void
send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
{
struct GNUNET_MESH_LocalAck msg;
+ MESH_TunnelNumber tid;
uint32_t ack;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
- ack = tunnel_get_ack (t);
+ if (c == t->owner)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
+ ack = tunnel_get_fwd_ack (t);
+ tid = t->local_tid;
+ }
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
+ ack = tunnel_get_bck_ack (t);
+ tid = t->local_tid_dest;
+ }
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
- if (t->last_ack == ack)
+ if (t->last_fwd_ack == ack)
return;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
- t->last_ack = ack;
+ t->last_fwd_ack = ack;
msg.header.size = htons (sizeof (msg));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
- msg.tunnel_id = htonl (t->local_tid);
+ msg.tunnel_id = htonl (tid);
msg.max_pid = htonl (ack);
GNUNET_SERVER_notification_context_unicast (nc, c->handle,
struct MeshPeerInfo *neighbor;
struct MeshPeerPath *p;
size_t size;
+ uint16_t type;
// GNUNET_TRANSPORT_try_connect(); FIXME use?
info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
info->mesh_data->data = GNUNET_malloc (size);
memcpy (info->mesh_data->data, message, size);
- if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
+ type = ntohs(message->type);
+ if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
{
struct GNUNET_MESH_Unicast *m;
neighbor = peer_info_get (peer);
for (p = neighbor->path_head; NULL != p; p = p->next)
{
- if (2 == p->length)
+ if (2 >= p->length)
{
break;
}
}
if (NULL == p)
{
+#if MESH_DEBUG
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " %s IS NOT DIRECTLY CONNECTED\n",
+ GNUNET_i2s(peer));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " PATHS TO %s:\n",
+ GNUNET_i2s(peer));
+ for (p = neighbor->path_head; NULL != p; p = p->next)
+ {
+ struct GNUNET_PeerIdentity debug_id;
+ unsigned int i;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " path with %u hops through:\n",
+ p->length);
+ for (i = 0; i < p->length; i++)
+ {
+ GNUNET_PEER_resolve(p->peers[i], &debug_id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " hop %u: %s\n",
+ i, GNUNET_i2s(&debug_id));
+ }
+ }
+#endif
GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
GNUNET_free (info->mesh_data->data);
GNUNET_free (info->mesh_data);
return;
}
info->peer = neighbor;
+ if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
+ {
+ /*
+ * TODO: in this case we only need the service to retransmit
+ * the message down the path. If we pass the real type to queue_add,
+ * queue_send will try to build the message from scratch. This can
+ * probably be done by some other way instead of deleteing the type
+ * info.
+ */
+ type = 0;
+ }
queue_add (info,
- 0,
+ type,
size,
neighbor,
t);
if (t->clients[i] == c)
{
t->clients[i] = t->clients[t->nclients - 1];
+ t->clients_fc[i] = t->clients_fc[t->nclients - 1];
GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
+ t->nclients++;
+ GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
break;
}
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
}
+/**
+ * Add a client to a tunnel, initializing all needed data structures.
+ *
+ * @param t Tunnel to which add the client.
+ * @param c Client which to add to the tunnel.
+ */
+static void
+tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
+{
+ struct MeshTunnelClientInfo clinfo;
+
+ GNUNET_array_append (t->clients, t->nclients, c);
+ clinfo.fwd_ack = t->fwd_pid + 1;
+ clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
+ clinfo.fwd_pid = t->fwd_pid;
+ clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
+ t->nclients--;
+ GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
+}
+
/**
* Notifies a tunnel that a connection has broken that affects at least
{
GNUNET_SCHEDULER_cancel(*(mdata->task));
GNUNET_free (mdata->task);
- GNUNET_SERVER_receive_done(t->owner->handle, GNUNET_OK);
+ GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
}
// FIXME change order?
GNUNET_free (mdata);
}
+/**
+ * @brief Get neighbor's Flow Control information.
+ *
+ * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
+ * descendant of the local node in a certain tunnel.
+ * If the info is not yet there (recently created path), creates the data struct
+ * and inserts it into the tunnel info, initialized to the current tunnel ACK
+ * values.
+ *
+ * @param t Tunnel related.
+ * @param peer Neighbor whose Flow Control info is needed.
+ *
+ * @return Neighbor's Flow Control info.
+ */
+static struct MeshTunnelChildInfo *
+tunnel_get_neighbor_fc (const struct MeshTunnel *t,
+ const struct GNUNET_PeerIdentity *peer)
+{
+ struct MeshTunnelChildInfo *cinfo;
+ cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
+ &peer->hashPubKey);
+ if (NULL == cinfo)
+ {
+ uint32_t delta;
+
+ cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
+ cinfo->id = GNUNET_PEER_intern (peer);
+ cinfo->skip = t->fwd_pid;
+
+ delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
+ cinfo->fwd_ack = t->fwd_pid + delta;
+ cinfo->bck_ack = delta;
+
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (t->children_fc,
+ &peer->hashPubKey,
+ cinfo,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+ }
+ return cinfo;
+}
+
+
+/**
+ * Get the Flow Control info of a client.
+ *
+ * @param t Tunnel on which to look.
+ * @param c Client whose ACK to get.
+ *
+ * @return ACK value.
+ */
+static struct MeshTunnelClientInfo *
+tunnel_get_client_fc (struct MeshTunnel *t,
+ struct MeshClient *c)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->nclients; i++)
+ {
+ if (t->clients[i] != c)
+ continue;
+ return &t->clients_fc[i];
+ }
+ GNUNET_assert (0);
+ return NULL; // avoid compiler / coverity complaints
+}
+
/**
* Iterator to get the appropiate ACK value from all children nodes.
* @param id Id of the child node.
*/
static void
-tunnel_get_child_ack (void *cls,
- GNUNET_PEER_Id id)
+tunnel_get_child_fwd_ack (void *cls,
+ GNUNET_PEER_Id id)
{
struct GNUNET_PeerIdentity peer_id;
struct MeshTunnelChildInfo *cinfo;
- struct MeshTunnel *t = cls;
+ struct MeshTunnelChildIteratorContext *ctx = cls;
+ struct MeshTunnel *t = ctx->t;
uint32_t ack;
GNUNET_PEER_resolve (id, &peer_id);
- cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
- &peer_id.hashPubKey);
- if (NULL == cinfo)
- {
- cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
- cinfo->id = id;
- cinfo->pid = t->pid;
- cinfo->skip = t->pid;
- cinfo->max_pid = ack = t->pid + 1;
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CONTAINER_multihashmap_put(t->children_fc,
- &peer_id.hashPubKey,
- cinfo,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
- }
- else
+ cinfo = tunnel_get_neighbor_fc (t, &peer_id);
+ ack = cinfo->fwd_ack;
+
+ if (GNUNET_NO == ctx->init)
{
- ack = cinfo->max_pid;
+ ctx->max_child_ack = ack;
+ ctx->init = GNUNET_YES;
}
- if (0 == t->max_child_ack)
- t->max_child_ack = ack;
-
if (GNUNET_YES == t->speed_min)
{
- t->max_child_ack = t->max_child_ack > ack ? ack : t->max_child_ack;
+ ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
}
else
{
- t->max_child_ack = t->max_child_ack > ack ? t->max_child_ack : ack;
+ ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
}
}
/**
* Get the maximum PID allowed to transmit to any
- * tunnel child of the local peer.
+ * tunnel child of the local peer, depending on the tunnel
+ * buffering/speed settings.
*
* @param t Tunnel.
*
- * @return Maximum PID allowed.
+ * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
*/
-static uint32_t
-tunnel_get_children_ack (struct MeshTunnel *t)
+static int64_t
+tunnel_get_children_fwd_ack (struct MeshTunnel *t)
+{
+ struct MeshTunnelChildIteratorContext ctx;
+ ctx.t = t;
+ ctx.max_child_ack = 0;
+ ctx.nchildren = 0;
+ tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
+
+ if (0 == ctx.nchildren)
+ return -1LL;
+
+ if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
+ ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
+
+ return (int64_t) ctx.max_child_ack;
+}
+
+
+/**
+ * Set the FWD ACK value of a client in a particular tunnel.
+ *
+ * @param t Tunnel affected.
+ * @param c Client whose ACK to set.
+ * @param ack ACK value.
+ */
+static void
+tunnel_set_client_fwd_ack (struct MeshTunnel *t,
+ struct MeshClient *c,
+ uint32_t ack)
+{
+ unsigned int i;
+
+ for (i = 0; i < t->nclients; i++)
+ {
+ if (t->clients[i] != c)
+ continue;
+ t->clients_fc[i].fwd_ack = ack;
+ return;
+ }
+ GNUNET_break (0);
+}
+
+
+/**
+ * Get the highest ACK value of all clients in a particular tunnel,
+ * according to the buffering/speed settings.
+ *
+ * @param t Tunnel on which to look.
+ *
+ * @return Corresponding ACK value (max uint32_t).
+ * If no clients are suscribed, -1.
+ */
+static int64_t
+tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
{
- t->max_child_ack = 0;
- tree_iterate_children (t->tree, tunnel_get_child_ack, t);
- return t->max_child_ack;
+ unsigned int i;
+ int64_t ack;
+
+ if (0 == t->nclients)
+ return -1;
+
+ for (ack = -1, i = 0; i < t->nclients; i++)
+ {
+ if (-1 == ack ||
+ (GNUNET_YES == t->speed_min &&
+ GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
+ (GNUNET_NO == t->speed_min &&
+ GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
+ {
+ ack = t->clients_fc[i].fwd_ack;
+ }
+ }
+
+ if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
+ ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
+
+ return (uint32_t) ack;
}
/**
- * Get the current ack value for a tunnel, taking in account the tunnel
+ * Get the current fwd ack value for a tunnel, taking in account the tunnel
* mode and the status of all children nodes.
*
* @param t Tunnel.
* @return Maximum PID allowed.
*/
static uint32_t
-tunnel_get_ack (struct MeshTunnel *t)
+tunnel_get_fwd_ack (struct MeshTunnel *t)
{
uint32_t count;
uint32_t buffer_free;
- uint32_t child_ack;
+ int64_t child_ack;
+ int64_t client_ack;
uint32_t ack;
- count = t->pid - t->skip;
- buffer_free = t->queue_max - t->queue_n;
- ack = count + buffer_free;
- child_ack = tunnel_get_children_ack (t);
+ count = t->fwd_pid - t->skip;
+ buffer_free = t->fwd_queue_max - t->fwd_queue_n;
+ ack = count + buffer_free; // Might overflow 32 bits, it's ok!
+ child_ack = tunnel_get_children_fwd_ack (t);
+ client_ack = tunnel_get_clients_fwd_ack (t);
+ if (-1 == child_ack)
+ {
+ // Node has no children, child_ack AND core buffer are irrelevant.
+ GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
+ return (uint32_t) client_ack;
+ }
if (GNUNET_YES == t->speed_min)
{
- ack = child_ack > ack ? ack : child_ack;
+ ack = GMC_min_pid ((uint32_t) child_ack, ack);
+ ack = GMC_min_pid ((uint32_t) client_ack, ack);
}
else
{
- ack = child_ack > ack ? child_ack : ack;
+ ack = GMC_max_pid ((uint32_t) child_ack, ack);
+ ack = GMC_max_pid ((uint32_t) client_ack, ack);
}
+ if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
+ ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
+ count, buffer_free, child_ack, client_ack, ack);
return ack;
}
-
/**
- * Send an ACK informing the predecessor about the available buffer space.
- * If buffering is off, send only on behalf of children or self if endpoint.
- * If buffering is on, send when sent to children and buffer space is free.
- *
- * @param t Tunnel on which to send the ACK.
+ * Get the current bck ack value for a tunnel, taking in account the tunnel
+ * mode and the status of all children nodes.
+ *
+ * @param t Tunnel.
+ *
+ * @return Maximum PID allowed.
*/
-static void
-tunnel_send_ack (struct MeshTunnel *t, uint16_t type)
+static uint32_t
+tunnel_get_bck_ack (struct MeshTunnel *t)
{
- struct GNUNET_MESH_ACK msg;
- struct GNUNET_PeerIdentity id;
uint32_t ack;
- /* Is it after unicast / multicast retransmission? */
- if (GNUNET_MESSAGE_TYPE_MESH_ACK != type)
+ if (GNUNET_YES == t->nobuffer)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ACK via DATA retransmission\n");
- if (GNUNET_YES == t->nobuffer)
+ if (t->bck_ack > t->bck_pid)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
- return;
+ return t->bck_pid + 1;
}
- if (t->queue_max > t->queue_n * 2)
+ else
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
- return;
+ return t->bck_pid;
}
}
+ ack = t->bck_pid + t->bck_queue_max - t->bck_queue_n;
- /* Ok, ACK might be necessary, what PID to ACK? */
- ack = tunnel_get_ack (t);
+ // FIXME fc
+ return ack;
+}
- /* If speed_min and not all children have ack'd, dont send yet */
- if (ack == t->last_ack)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, not ready\n");
- return;
- }
+static void
+send_local_ack (struct MeshClient *c, struct MeshTunnel *t, uint32_t ack)
+{
+ struct GNUNET_MESH_LocalAck msg;
- t->last_ack = ack;
- msg.pid = htonl (ack);
+ msg.header.size = htons (sizeof (msg));
+ msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
+ msg.tunnel_id = htonl (t->local_tid_dest);
+ msg.max_pid = htonl (ack);
+ GNUNET_SERVER_notification_context_unicast(nc,
+ c->handle,
+ &msg.header,
+ GNUNET_NO);
+}
- GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
+/**
+ * Build an ACK message and send it to the given peer.
+ */
+static void
+send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
+{
+ struct GNUNET_MESH_ACK msg;
+ GNUNET_PEER_resolve (t->id.oid, &msg.oid);
msg.header.size = htons (sizeof (msg));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
+ msg.pid = htonl (ack);
msg.tid = htonl (t->id.tid);
- GNUNET_PEER_resolve(t->id.oid, &msg.oid);
- send_message (&msg.header, &id, t);
+
+ send_message (&msg.header, peer, t);
+}
+
+
+/**
+ * Send an ACK informing the predecessor about the available buffer space.
+ * In case there is no predecessor, inform the owning client.
+ * If buffering is off, send only on behalf of children or self if endpoint.
+ * If buffering is on, send when sent to children and buffer space is free.
+ * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
+ * the ACK itself goes "back" (towards root).
+ *
+ * @param t Tunnel on which to send the ACK.
+ * @param type Type of message that triggered the ACK transmission.
+ */
+static void
+tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
+{
+ struct GNUNET_PeerIdentity id;
+ uint32_t ack;
+
+ if (NULL != t->owner)
+ {
+ send_client_tunnel_ack (t->owner, t);
+ return;
+ }
+ /* Is it after unicast / multicast retransmission? */
+ switch (type)
+ {
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ACK due to FWD DATA retransmission\n");
+ if (GNUNET_YES == t->nobuffer)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
+ return;
+ }
+ if (t->fwd_queue_max > t->fwd_queue_n * 2)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
+ return;
+ }
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_ACK:
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
+ break;
+ default:
+ GNUNET_break (0);
+ }
+
+ /* Ok, ACK might be necessary, what PID to ACK? */
+ ack = tunnel_get_fwd_ack (t);
+
+ /* If speed_min and not all children have ack'd, dont send yet */
+ if (ack == t->last_fwd_ack)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
+ return;
+ }
+
+ t->last_fwd_ack = ack;
+ GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
+ send_ack (t, &id, ack);
+}
+
+
+/**
+ * Iterator to send a child node a BCK ACK to allow him to send more
+ * to_origin data.
+ *
+ * @param cls Closure (tunnel).
+ * @param id Id of the child node.
+ */
+static void
+tunnel_send_child_bck_ack (void *cls,
+ GNUNET_PEER_Id id)
+{
+ struct MeshTunnel *t = cls;
+ struct MeshTunnelChildInfo *cinfo;
+ struct GNUNET_PeerIdentity peer;
+
+ GNUNET_PEER_resolve (id, &peer);
+ cinfo = tunnel_get_neighbor_fc (t, &peer);
+
+ if (cinfo->bck_ack != cinfo->pid &&
+ GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
+ return;
+
+ cinfo->bck_ack++;
+ send_ack (t, &peer, cinfo->bck_ack);
+}
+
+
+/**
+ * @brief Send BCK ACKs to clients to allow them more to_origin traffic
+ *
+ * Iterates over all clients and sends BCK ACKs to the ones that need it.
+ *
+ * @param t Tunnel on which to send the BCK ACKs.
+ */
+static void
+tunnel_send_clients_bck_ack (struct MeshTunnel *t)
+{
+ unsigned int i;
+
+ /* Find client whom to allow to send to origin (with lowest buffer space) */
+ for (i = 0; i < t->nclients; i++)
+ {
+ struct MeshTunnelClientInfo *clinfo;
+ unsigned int delta;
+
+ clinfo = &t->clients_fc[i];
+ delta = clinfo->bck_ack - clinfo->bck_pid;
+
+ if ((GNUNET_NO == t->nobuffer && ACK_THRESHOLD > delta) ||
+ (GNUNET_YES == t->nobuffer && 0 == delta))
+ {
+ uint32_t ack;
+
+ ack = clinfo->bck_pid;
+ ack += t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ " sending ack to client %u: %u\n",
+ t->clients[i]->id, ack);
+ send_local_ack(t->clients[i], t, ack);
+ clinfo->bck_ack = ack;
+ }
+ }
+}
+
+
+/**
+ * Send an ACK informing the children nodes and destination clients about
+ * the available buffer space.
+ * If buffering is off, send only on behalf of root (can be self).
+ * If buffering is on, send when sent to predecessor and buffer space is free.
+ * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
+ * the ACK itself goes "forward" (towards children/clients).
+ *
+ * @param t Tunnel on which to send the ACK.
+ * @param type Type of message that triggered the ACK transmission.
+ */
+static void
+tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
+{
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending BCK ACK on tunnel %u [%u]\n",
+ t->id.oid, t->id.tid);
+ /* Is it after data to_origin retransmission? */
+ switch (type)
+ {
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "ACK due to BCK DATA retransmission\n");
+ if (GNUNET_YES == t->nobuffer)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
+ return;
+ }
+ if (t->bck_queue_max > t->bck_queue_n * 2)
+ {
+// GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
+// return;
+ }
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_ACK:
+ case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
+ // FIXME fc
+ break;
+ default:
+ GNUNET_break (0);
+ }
+
+ tunnel_send_clients_bck_ack (t);
+ tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
}
{
struct MeshTunnel *t;
struct GNUNET_HashCode hash;
-
+
if (n_tunnels >= max_tunnels && NULL == client)
return NULL;
t = GNUNET_malloc (sizeof (struct MeshTunnel));
t->id.oid = owner;
t->id.tid = tid;
- t->queue_max = (max_msgs_queue / max_tunnels) + 1;
+ t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
+ t->bck_queue_max = t->fwd_queue_max;
t->tree = tree_new (owner);
t->owner = client;
+ t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
+ t->bck_pid = (uint32_t) -1; // Next (expected) = 0
+ t->bck_ack = INITIAL_WINDOW_SIZE - 1;
+ t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
t->local_tid = local;
t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
n_tunnels++;
GNUNET_break (0);
tunnel_destroy (t);
if (NULL != client)
+ {
+ GNUNET_break (0);
GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
+ }
return NULL;
}
GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
{
- GNUNET_break (0);
tunnel_destroy (t);
+ GNUNET_break (0);
GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
return NULL;
}
* Resets the tunnel timeout. Starts it if no timeout was running.
*
* @param t Tunnel whose timeout to reset.
+ *
+ * TODO use heap to improve efficiency of scheduler.
*/
static void
tunnel_reset_timeout (struct MeshTunnel *t)
mc = (struct GNUNET_MESH_Multicast *) mh;
mh = (struct GNUNET_MessageHeader *) &mc[1];
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- " multicast, payload type %u\n", ntohs (mh->type));
+ " multicast, payload type %s\n",
+ GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" multicast, payload size %u\n", ntohs (mh->size));
}
else
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
- ntohs (mh->type));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
+ GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
}
}
#endif
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
t = queue->tunnel;
- t->queue_n--;
+ if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
+ {
+ t->fwd_queue_n--;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
+ }
+ else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
+ {
+ t->bck_queue_n--;
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
+ }
/* Fill buf */
switch (queue->type)
{
- case 0:
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
- data_size = send_core_data_raw (queue->cls, size, buf);
- msg = (struct GNUNET_MessageHeader *) buf;
- if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
- tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
- break;
- case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
- data_size = send_core_data_multicast(queue->cls, size, buf);
- tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
- break;
- case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
- data_size = send_core_path_create(queue->cls, size, buf);
+ case 0:
+ case GNUNET_MESSAGE_TYPE_MESH_ACK:
+ case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
+ case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "********* raw: %u\n",
+ queue->type);
+ /* Fall through */
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ data_size = send_core_data_raw (queue->cls, size, buf);
+ msg = (struct GNUNET_MessageHeader *) buf;
+ switch (ntohs (msg->type)) // Type of preconstructed message
+ {
+ case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
break;
- case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
- data_size = send_core_path_ack(queue->cls, size, buf);
+ case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
+ tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
break;
- default:
- GNUNET_break (0);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
- data_size = 0;
+ default:
+ break;
+ }
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
+ data_size = send_core_data_multicast(queue->cls, size, buf);
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
+ data_size = send_core_path_create(queue->cls, size, buf);
+ break;
+ case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
+ data_size = send_core_path_ack(queue->cls, size, buf);
+ break;
+ default:
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "********* type unknown: %u\n",
+ queue->type);
+ data_size = 0;
}
/* Free queue, but cls was freed by send_core_* */
queue_destroy (queue, GNUNET_NO);
- if (GNUNET_YES == t->destroy && 0 == t->queue_n)
+ if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
tunnel_destroy (t);
queue_add (void *cls, uint16_t type, size_t size,
struct MeshPeerInfo *dst, struct MeshTunnel *t)
{
- struct MeshPeerQueue *queue;
+ struct MeshPeerQueue *queue;
+ unsigned int *max;
+ unsigned int *n;
- if (t->queue_n >= t->queue_max)
+ n = NULL;
+ if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
+ GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
+ {
+ n = &t->fwd_queue_n;
+ max = &t->fwd_queue_max;
+ }
+ else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
+ {
+ n = &t->bck_queue_n;
+ max = &t->bck_queue_max;
+ }
+ if (NULL != n) {
+ if (*n >= *max)
{
if (NULL == t->owner)
GNUNET_break_op(0); // TODO: kill connection?
GNUNET_break(0);
return; // Drop message
}
- t->queue_n++;
- queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
- queue->cls = cls;
- queue->type = type;
- queue->size = size;
- queue->peer = dst;
- queue->tunnel = t;
- GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
- if (NULL == dst->core_transmit)
- {
- struct GNUNET_PeerIdentity id;
-
- GNUNET_PEER_resolve (dst->id, &id);
- dst->core_transmit =
- GNUNET_CORE_notify_transmit_ready(core_handle,
- 0,
- 0,
- GNUNET_TIME_UNIT_FOREVER_REL,
- &id,
- size,
- &queue_send,
- dst);
- }
+ (*n)++;
+ }
+ queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
+ queue->cls = cls;
+ queue->type = type;
+ queue->size = size;
+ queue->peer = dst;
+ queue->tunnel = t;
+ GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
+ if (NULL == dst->core_transmit)
+ {
+ struct GNUNET_PeerIdentity id;
+
+ GNUNET_PEER_resolve (dst->id, &id);
+ dst->core_transmit =
+ GNUNET_CORE_notify_transmit_ready(core_handle,
+ 0,
+ 0,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ &id,
+ size,
+ &queue_send,
+ dst);
+ }
}
GNUNET_YES : GNUNET_NO;
if (GNUNET_YES == t->nobuffer)
- t->queue_max = 1;
+ {
+ t->bck_queue_max = 1;
+ t->fwd_queue_max = 1;
+ }
+ // FIXME only assign a local tid if a local client is interested (on demand)
while (NULL != tunnel_get_incoming (next_local_tid))
next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
t->local_tid_dest = next_local_tid++;
next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
+ // FIXME end
tunnel_reset_timeout (t);
GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
/* create path: self not found in path through self */
GNUNET_break_op (0);
path_destroy (path);
- /* FIXME error. destroy tunnel? leave for timeout? */
- return 0;
+ tunnel_destroy (t);
+ return GNUNET_OK;
}
path_add_to_peers (path, GNUNET_NO);
tunnel_add_path (t, path, own_pos);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
GNUNET_i2s (peer));
+ /* Check size */
size = ntohs (message->size);
if (size <
sizeof (struct GNUNET_MESH_Unicast) +
return GNUNET_OK;
}
msg = (struct GNUNET_MESH_Unicast *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
- ntohs (msg[1].header.type));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
+ GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
+ /* Check tunnel */
t = tunnel_get (&msg->oid, ntohl (msg->tid));
if (NULL == t)
{
return GNUNET_OK;
}
pid = ntohl (msg->pid);
- if (t->pid == pid)
+ if (t->fwd_pid == pid)
{
- GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
+ GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
" Already seen pid %u, DROPPING!\n", pid);
return GNUNET_OK;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" pid %u not seen yet, forwarding\n", pid);
}
- t->skip += (pid - t->pid) - 1;
- t->pid = pid;
+ t->skip += (pid - t->fwd_pid) - 1;
+ t->fwd_pid = pid;
+ if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
+ {
+ GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
tunnel_reset_timeout (t);
dest_id = GNUNET_PEER_search (&msg->destination);
if (dest_id == myid)
" it's for us! sending to clients...\n");
GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
- tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK); // FIXME send after client processes the packet
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
return GNUNET_OK;
}
ttl = ntohl (msg->ttl);
{
GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
return GNUNET_OK;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" not for us, retransmitting...\n");
- GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
neighbor = tree_get_first_hop (t->tree, dest_id);
- cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
- &neighbor->hashPubKey);
- if (NULL == cinfo)
- {
- cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
- cinfo->id = GNUNET_PEER_intern (neighbor);
- cinfo->skip = pid;
- cinfo->max_pid = pid + t->queue_max - t->queue_n; // FIXME review
-
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CONTAINER_multihashmap_put (t->children_fc,
- &neighbor->hashPubKey,
- cinfo,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
- }
+ cinfo = tunnel_get_neighbor_fc (t, neighbor);
cinfo->pid = pid;
GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
&tunnel_add_skip,
&neighbor);
+ if (GMC_is_pid_bigger (pid, cinfo->fwd_ack))
+ {
+ GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
+ GNUNET_break_op (0);
+ return GNUNET_OK;
+ }
send_message (message, neighbor, t);
+ GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
+
return GNUNET_OK;
}
return GNUNET_OK;
}
pid = ntohl (msg->pid);
- if (t->pid == pid)
+ if (t->fwd_pid == pid)
{
/* already seen this packet, drop */
- GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" Already seen pid %u, DROPPING!\n", pid);
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
return GNUNET_OK;
}
else
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" pid %u not seen yet, forwarding\n", pid);
}
- t->skip += (pid - t->pid) - 1;
- t->pid = pid;
+ t->skip += (pid - t->fwd_pid) - 1;
+ t->fwd_pid = pid;
tunnel_reset_timeout (t);
/* Transmit to locally interested clients */
{
GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
send_subscribed_clients (message, &msg[1].header);
+ tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
if (ntohl (msg->ttl) == 0)
{
GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
return GNUNET_OK;
}
GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
return GNUNET_OK;
}
msg = (struct GNUNET_MESH_ToOrigin *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
- ntohs (msg[1].header.type));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
+ GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
t = tunnel_get (&msg->oid, ntohl (msg->tid));
if (NULL == t)
return GNUNET_OK;
}
- if (t->id.oid == myid)
+ if (NULL != t->owner)
{
char cbuf[size];
struct GNUNET_MESH_ToOrigin *copy;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" it's for us! sending to clients...\n");
- if (NULL == t->owner)
- {
- /* got data packet for ownerless tunnel */
- GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
- 1, GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
- GNUNET_break_op (0);
- return GNUNET_OK;
- }
/* TODO signature verification */
memcpy (cbuf, message, size);
copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
copy->tid = htonl (t->local_tid);
GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
- ©->header, GNUNET_YES);
+ ©->header, GNUNET_NO);
+ tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
return GNUNET_OK;
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
unsigned int atsi_count)
{
struct GNUNET_MESH_ACK *msg;
- struct MeshTunnelChildInfo *cinfo;
struct MeshTunnel *t;
uint32_t ack;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got an ACK packet from %s\n",
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
GNUNET_i2s (peer));
msg = (struct GNUNET_MESH_ACK *) message;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
- ntohs (msg[1].header.type));
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
+ GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
t = tunnel_get (&msg->oid, ntohl (msg->tid));
if (NULL == t)
return GNUNET_OK;
}
ack = ntohl (msg->pid);
- cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
- &peer->hashPubKey);
- if (NULL == cinfo)
+
+ /* Is this a forward or backward ACK? */
+ if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
{
- GNUNET_break_op (0);
- return GNUNET_OK;
+ struct MeshTunnelChildInfo *cinfo;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
+ cinfo = tunnel_get_neighbor_fc (t, peer);
+ cinfo->fwd_ack = ack;
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
}
- cinfo->max_pid = ack;
- tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
+ else
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
+ t->bck_ack = ack;
+ tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
+ }
+ // FIXME fc Unlock queues?
return GNUNET_OK;
}
msg->oid = my_full_id;
msg->tid = htonl (t->id.tid);
msg->ttl = htonl (default_ttl);
- msg->pid = htonl (t->pid + 1);
- t->pid++;
+ msg->pid = htonl (t->fwd_pid + 1);
+ t->fwd_pid++;
payload = (struct GNUNET_MessageHeader *) &msg[1];
payload->size = htons (sizeof (struct GNUNET_MessageHeader));
payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
struct GNUNET_MESH_TunnelMessage *t_msg;
struct MeshTunnel *t;
struct MeshClient *c;
+ MESH_TunnelNumber tid;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
/* Sanity check for tunnel numbering */
- if (0 == (ntohl (t_msg->tunnel_id) & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
+ tid = ntohl (t_msg->tunnel_id);
+ if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
{
GNUNET_break (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
/* Sanity check for duplicate tunnel IDs */
- if (NULL != tunnel_get_by_local_id (c, ntohl (t_msg->tunnel_id)))
+ if (NULL != tunnel_get_by_local_id (c, tid))
{
GNUNET_break (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
while (NULL != tunnel_get_by_pi (myid, next_tid))
next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
- t = tunnel_new (myid, next_tid++, c, ntohl (t_msg->tunnel_id));
+ t = tunnel_new (myid, next_tid++, c, tid);
if (NULL == t)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
+ GNUNET_break (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
t = tunnel_get_by_local_id(c, tid);
if (NULL == t)
{
- GNUNET_break (0);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
+ GNUNET_break (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
t = tunnel_get_by_local_id(c, tid);
if (NULL == t)
{
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
GNUNET_break (0);
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
t = tunnel_get_by_local_id(c, tid);
if (NULL == t)
{
- GNUNET_break (0);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
+ GNUNET_break (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
/* Message size sanity check */
if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
{
- GNUNET_break (0);
- GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
- return;
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
}
/* Tunnel exists? */
return;
}
+ /* PID should be as expected */
+ if (ntohl (data_msg->pid) != t->fwd_pid + 1)
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Unicast PID, expected %u, got %u\n",
+ t->fwd_pid + 1, ntohl (data_msg->pid));
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
/* Ok, everything is correct, send the message
* (pretend we got it from a mesh peer)
*/
{
+ /* Work around const limitation */
char buf[ntohs (message->size)] GNUNET_ALIGN;
struct GNUNET_MESH_Unicast *copy;
- /* Work around const limitation */
copy = (struct GNUNET_MESH_Unicast *) buf;
memcpy (buf, data_msg, size);
copy->oid = my_full_id;
copy->tid = htonl (t->id.tid);
copy->ttl = htonl (default_ttl);
- copy->pid = htonl (t->pid + 1);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" calling generic handler...\n");
handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
- send_client_tunnel_ack (t->owner, t);
}
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
GNUNET_SERVER_receive_done (client, GNUNET_OK);
const struct GNUNET_MessageHeader *message)
{
struct GNUNET_MESH_ToOrigin *data_msg;
- struct GNUNET_PeerIdentity id;
+ struct MeshTunnelClientInfo *clinfo;
struct MeshClient *c;
struct MeshTunnel *t;
MESH_TunnelNumber tid;
}
/* It should be sent by someone who has this as incoming tunnel. */
- if (-1 == client_knows_tunnel (c, t))
+ if (GNUNET_NO == client_knows_tunnel (c, t))
{
GNUNET_break (0);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
- GNUNET_PEER_resolve (t->id.oid, &id);
+
+ /* PID should be as expected */
+ clinfo = tunnel_get_client_fc (t, c);
+ if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "To Origin PID, expected %u, got %u\n",
+ clinfo->bck_pid + 1, ntohl (data_msg->pid));
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
/* Ok, everything is correct, send the message
* (pretend we got it from a mesh peer)
/* Work around const limitation */
copy = (struct GNUNET_MESH_ToOrigin *) buf;
memcpy (buf, data_msg, size);
- copy->oid = id;
+ GNUNET_PEER_resolve (t->id.oid, ©->oid);
copy->tid = htonl (t->id.tid);
+ copy->ttl = htonl (default_ttl);
+ GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
copy->sender = my_full_id;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" calling generic handler...\n");
return;
}
+ /* PID should be as expected */
+ if (ntohl (data_msg->pid) != t->fwd_pid + 1)
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Multicast PID, expected %u, got %u\n",
+ t->fwd_pid + 1, ntohl (data_msg->pid));
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
{
char buf[ntohs (message->size)] GNUNET_ALIGN;
struct GNUNET_MESH_Multicast *copy;
copy->oid = my_full_id;
copy->tid = htonl (t->id.tid);
copy->ttl = htonl (default_ttl);
- copy->pid = htonl (t->pid + 1);
+ GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
" calling generic handler...\n");
handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
}
+/**
+ * Handler for client's ACKs for payload traffic.
+ *
+ * @param cls Closure (unused).
+ * @param client Identification of the client.
+ * @param message The actual message.
+ */
+static void
+handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *message)
+{
+ struct GNUNET_MESH_LocalAck *msg;
+ struct MeshTunnel *t;
+ struct MeshClient *c;
+ MESH_TunnelNumber tid;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
+ /* Sanity check for client registration */
+ if (NULL == (c = client_get (client)))
+ {
+ GNUNET_break (0);
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ msg = (struct GNUNET_MESH_LocalAck *) message;
+ /* Tunnel exists? */
+ tid = ntohl (msg->tunnel_id);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
+ t = tunnel_get_by_local_id (c, tid);
+ if (NULL == t)
+ {
+ GNUNET_break (0); // FIXME fc
+ GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+ return;
+ }
+
+ /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
+ if (NULL != t->owner && t->owner->handle == client)
+ {
+ /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
+ t->bck_ack = ntohl(msg->max_pid);
+ tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
+ }
+ else
+ {
+ /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
+ tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
+ tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
+ }
+
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+}
+
+
/**
* Functions to handle messages from clients
*/
GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
{&handle_local_multicast, NULL,
GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
+ {&handle_local_ack, NULL,
+ GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
+ sizeof (struct GNUNET_MESH_LocalAck)},
{NULL, NULL, 0, 0}
};
core_init (void *cls, struct GNUNET_CORE_Handle *server,
const struct GNUNET_PeerIdentity *identity)
{
+ static int i = 0;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
core_handle = server;
if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
NULL == server)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
- GNUNET_SCHEDULER_shutdown ();
+ GNUNET_SCHEDULER_shutdown (); // Try gracefully
+ if (10 < i++)
+ GNUNET_abort(); // Try harder
}
return;
}
types = GNUNET_CONTAINER_multihashmap_create (32);
GNUNET_SERVER_add_handlers (server_handle, client_handlers);
- nc = GNUNET_SERVER_notification_context_create (server_handle,
- LOCAL_QUEUE_SIZE);
+ nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
GNUNET_SERVER_disconnect_notify (server_handle,
&handle_local_client_disconnect, NULL);