2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_manipulation.c
23 * @brief transport component manipulation traffic for simulation
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet-service-transport_blacklist.h"
29 #include "gnunet-service-transport_clients.h"
30 #include "gnunet-service-transport_hello.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_plugins.h"
33 #include "gnunet-service-transport_validation.h"
34 #include "gnunet-service-transport.h"
35 #include "transport.h"
41 enum TRAFFIC_METRIC_DIRECTION
48 struct GST_ManipulationHandle man_handle;
52 * Struct containing information about manipulations to a specific peer
59 struct PropManipulationEntry
64 struct PropManipulationEntry *next;
69 struct PropManipulationEntry *prev;
79 uint32_t metrics[TM_BOTH];
84 * Struct containing information about manipulations to a specific peer
91 struct GNUNET_PeerIdentity peer;
93 struct PropManipulationEntry *head;
94 struct PropManipulationEntry *tail;
97 * Peer specific manipulation metrics
99 uint32_t metrics [TM_BOTH][GNUNET_ATS_QualityPropertiesCount];
102 * Task to schedule delayed sendding
104 GNUNET_SCHEDULER_TaskIdentifier send_delay_task;
107 * Send queue DLL head
109 struct DelayQueueEntry *send_head;
112 * Send queue DLL tail
114 struct DelayQueueEntry *send_tail;
118 struct GST_ManipulationHandle
121 * Hashmap contain all peers currently manipulated
123 struct GNUNET_CONTAINER_MultiHashMap *peers;
126 * Peer containing information for general manipulation
128 struct TM_Peer general;
134 * Entry in the delay queue for an outbound delayed message
136 struct DelayQueueEntry
141 struct DelayQueueEntry *prev;
146 struct DelayQueueEntry *next;
149 * Peer this entry is belonging to
154 * Absolute time when to send
156 struct GNUNET_TIME_Absolute sent_at;
171 struct GNUNET_TIME_Relative timeout;
174 * Transports send continuation
176 GST_NeighbourSendContinuation cont;
179 * Transports send continuation cls
186 set_metric (struct TM_Peer *dest, int direction, uint32_t type, uint32_t value)
188 struct PropManipulationEntry *cur;
189 for (cur = dest->head; NULL != cur; cur = cur->next)
191 if (cur->type == type)
196 cur = GNUNET_malloc (sizeof (struct PropManipulationEntry));
197 GNUNET_CONTAINER_DLL_insert (dest->head, dest->tail, cur);
199 cur->metrics[TM_SEND] = UINT32_MAX;
200 cur->metrics[TM_RECEIVE] = UINT32_MAX;
206 cur->metrics[TM_SEND] = value;
207 cur->metrics[TM_RECEIVE] = value;
210 cur->metrics[TM_SEND] = value;
213 cur->metrics[TM_RECEIVE] = value;
222 find_metric (struct TM_Peer *dest, uint32_t type, int direction)
224 struct PropManipulationEntry *cur;
226 for (cur = dest->head; NULL != cur; cur = cur->next)
228 if (cur->type == type)
229 return cur->metrics[direction];
236 * Clean up metrics for a peer
240 free_metric (struct TM_Peer *dest)
242 struct PropManipulationEntry *cur;
243 struct PropManipulationEntry *next;
245 for (cur = dest->head; NULL != cur; cur = next)
248 GNUNET_CONTAINER_DLL_remove (dest->head, dest->tail, cur);
255 * Set traffic metric to manipulate
258 * @param client client sending message
259 * @param message containing information
262 GST_manipulation_set_metric (void *cls, struct GNUNET_SERVER_Client *client,
263 const struct GNUNET_MessageHeader *message)
265 struct TrafficMetricMessage *tm = (struct TrafficMetricMessage *) message;
266 struct GNUNET_PeerIdentity dummy;
267 struct GNUNET_ATS_Information *ats;
275 if (0 == ntohs (tm->ats_count))
276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
279 switch (ntohs(tm->direction)) {
284 direction = TM_RECEIVE;
293 memset (&dummy, '\0', sizeof (struct GNUNET_PeerIdentity));
294 if (0 == memcmp (&tm->peer, &dummy, sizeof (struct GNUNET_PeerIdentity)))
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received traffic metrics for all peers \n");
298 ats = (struct GNUNET_ATS_Information *) &tm[1];
299 for (c = 0; c < ntohs (tm->ats_count); c++)
301 type = htonl (ats[c].type);
302 value = htonl (ats[c].value);
303 set_metric (&man_handle.general, direction, type, value);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received traffic metrics for peer `%s'\n",
309 GNUNET_i2s(&tm->peer));
311 if (NULL == (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &tm->peer.hashPubKey)))
313 tmp = GNUNET_malloc (sizeof (struct TM_Peer));
314 tmp->peer = (tm->peer);
315 for (c = 0; c < TM_BOTH; c++)
317 for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
319 tmp->metrics[c][c2] = UINT32_MAX;
322 GNUNET_CONTAINER_multihashmap_put (man_handle.peers, &tm->peer.hashPubKey, tmp, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
325 ats = (struct GNUNET_ATS_Information *) &tm[1];
326 for (c = 0; c < ntohs (tm->ats_count); c++)
328 type = htonl (ats[c].type);
329 value = htonl (ats[c].value);
330 set_metric (tmp, direction, type, value);
333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
337 send_delayed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
339 struct DelayQueueEntry *dqe = cls;
340 struct DelayQueueEntry *next;
341 struct TM_Peer *tmp = dqe->tmp;
342 struct GNUNET_TIME_Relative delay;
343 tmp->send_delay_task = GNUNET_SCHEDULER_NO_TASK;
344 GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
345 GST_neighbours_send (&tmp->peer, dqe->msg, dqe->msg_size, dqe->timeout, dqe->cont, dqe->cont_cls);
347 next = tmp->send_head;
350 /* More delayed messages */
351 delay = GNUNET_TIME_absolute_get_remaining (next->sent_at);
352 tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
360 * Adapter function between transport's send function and transport plugins
362 * @param target the peer the message to send to
363 * @param msg the message received
364 * @param msg_size message size
365 * @param timeout timeout
366 * @param cont the continuation to call after sending
367 * @param cont_cls cls for continuation
370 GST_manipulation_send (const struct GNUNET_PeerIdentity *target, const void *msg,
371 size_t msg_size, struct GNUNET_TIME_Relative timeout,
372 GST_NeighbourSendContinuation cont, void *cont_cls)
375 struct DelayQueueEntry *dqe;
376 struct GNUNET_TIME_Relative delay;
378 if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &target->hashPubKey)))
380 /* Manipulate here */
382 if (UINT32_MAX != find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND))
384 /* We have a delay */
385 delay.rel_value = find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND);
386 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
388 dqe->sent_at = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), delay);
390 dqe->cont_cls = cont_cls;
392 dqe->msg_size = msg_size;
393 dqe->timeout = timeout;
394 memcpy (dqe->msg, msg, msg_size);
395 GNUNET_CONTAINER_DLL_insert_tail (tmp->send_head, tmp->send_tail, dqe);
396 if (GNUNET_SCHEDULER_NO_TASK == tmp->send_delay_task)
397 tmp->send_delay_task =GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
401 else if (UINT32_MAX != find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND))
403 /* We have a delay */
404 delay.rel_value = find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND);
405 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
407 dqe->sent_at = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), delay);
409 dqe->cont_cls = cont_cls;
411 dqe->msg_size = msg_size;
412 dqe->timeout = timeout;
413 memcpy (dqe->msg, msg, msg_size);
414 GNUNET_CONTAINER_DLL_insert_tail (tmp->send_head, tmp->send_tail, dqe);
415 if (GNUNET_SCHEDULER_NO_TASK == tmp->send_delay_task)
416 tmp->send_delay_task =GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
421 GST_neighbours_send (target, msg, msg_size, timeout, cont, cont_cls);
426 * Function that will be called to manipulate ATS information according to
427 * current manipulation settings
429 * @param peer the peer
430 * @param address binary address
431 * @param session the session
432 * @param ats the ats information
433 * @param ats_count the number of ats information
435 struct GNUNET_ATS_Information *
436 GST_manipulation_manipulate_metrics (const struct GNUNET_PeerIdentity *peer,
437 const struct GNUNET_HELLO_Address *address,
438 struct Session *session,
439 const struct GNUNET_ATS_Information *ats,
442 struct GNUNET_ATS_Information *ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *ats_count);
447 tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey);
449 for (d = 0; d < ats_count; d++)
455 m_tmp = find_metric (tmp, ntohl(ats[d].type), TM_RECEIVE);
456 g_tmp = find_metric (&man_handle.general, ntohl(ats[d].type), TM_RECEIVE);
458 if (UINT32_MAX != g_tmp)
459 ats_new[d].value = htonl(g_tmp);
460 if (UINT32_MAX != m_tmp)
461 ats_new[d].value = htonl(m_tmp);
469 * Adapter function between transport plugins and transport receive function
470 * manipulation delays for next send.
472 * @param cls the closure for transport
473 * @param peer the peer the message was received from
474 * @param message the message received
475 * @param session the session the message was received on
476 * @param sender_address the sender address
477 * @param sender_address_len the length of the sender address
478 * @return manipulated delay for next receive
480 struct GNUNET_TIME_Relative
481 GST_manipulation_recv (void *cls,
482 const struct GNUNET_PeerIdentity *peer,
483 const struct GNUNET_MessageHeader *message,
484 struct Session *session,
485 const char *sender_address,
486 uint16_t sender_address_len)
489 uint32_t p_recv_delay;
490 uint32_t g_recv_delay;
491 struct GNUNET_TIME_Relative quota_delay;
492 struct GNUNET_TIME_Relative m_delay;
494 g_recv_delay = find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_RECEIVE);
495 if ((g_recv_delay >= GNUNET_TIME_UNIT_ZERO.rel_value) && (UINT32_MAX != g_recv_delay))
496 m_delay.rel_value = g_recv_delay; /* Global delay */
498 m_delay = GNUNET_TIME_UNIT_ZERO;
500 if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey)))
502 /* Manipulate receive delay */
503 p_recv_delay = find_metric (tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_RECEIVE);
504 if (UINT32_MAX != p_recv_delay)
505 m_delay.rel_value = p_recv_delay; /* Peer specific delay */
508 quota_delay = GST_receive_callback (cls, peer, message,
509 session, sender_address, sender_address_len);
510 if (quota_delay.rel_value > m_delay.rel_value)
518 * Initialize traffic manipulation
520 * @param GST_cfg configuration handle
523 GST_manipulation_init (const struct GNUNET_CONFIGURATION_Handle *GST_cfg)
525 unsigned long long tmp;
527 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
528 "transport", "MANIPULATE_DISTANCE_IN", &tmp))
530 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting inbound distance_in to %u\n",
531 (unsigned long long) tmp);
532 set_metric (&man_handle.general, TM_RECEIVE, GNUNET_ATS_QUALITY_NET_DISTANCE, tmp);
535 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
536 "transport", "MANIPULATE_DISTANCE_OUT", &tmp))
538 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting outbound distance_in to %u\n",
539 (unsigned long long) tmp);
540 set_metric (&man_handle.general, TM_SEND, GNUNET_ATS_QUALITY_NET_DISTANCE, tmp);
543 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
544 "transport", "MANIPULATE_DELAY_IN", &tmp))
546 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying inbound traffic for %llu ms\n",
547 (unsigned long long) tmp);
548 set_metric (&man_handle.general, TM_RECEIVE, GNUNET_ATS_QUALITY_NET_DELAY, tmp);
552 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
553 "transport", "MANIPULATE_DELAY_OUT", &tmp))
555 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying outbound traffic for %llu ms\n",
556 (unsigned long long) tmp);
557 set_metric (&man_handle.general, TM_SEND, GNUNET_ATS_QUALITY_NET_DELAY, tmp);
560 man_handle.peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
565 free_tmps (void *cls,
566 const struct GNUNET_HashCode * key,
569 struct DelayQueueEntry *dqe;
570 struct DelayQueueEntry *next;
573 struct TM_Peer *tmp = (struct TM_Peer *) value;
574 GNUNET_CONTAINER_multihashmap_remove (man_handle.peers, key, value);
576 next = tmp->send_head;
577 while (NULL != (dqe = next))
580 GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
583 if (GNUNET_SCHEDULER_NO_TASK != tmp->send_delay_task)
585 GNUNET_SCHEDULER_cancel (tmp->send_delay_task);
586 tmp->send_delay_task = GNUNET_SCHEDULER_NO_TASK;
595 * Stop traffic manipulation
598 GST_manipulation_stop ()
600 GNUNET_CONTAINER_multihashmap_iterate (man_handle.peers, &free_tmps,NULL);
602 GNUNET_CONTAINER_multihashmap_destroy (man_handle.peers);
603 free_metric (&man_handle.general);
604 man_handle.peers = NULL;
608 /* end of file gnunet-service-transport_manipulation.c */