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"
37 enum TRAFFIC_METRIC_DIRECTION
44 struct GST_ManipulationHandle man_handle;
49 * Struct containing information about manipulations to a specific peer
56 struct PropManipulationEntry
61 struct PropManipulationEntry *next;
66 struct PropManipulationEntry *prev;
76 uint32_t metrics[TM_BOTH];
81 * Struct containing information about manipulations to a specific peer
88 struct GNUNET_PeerIdentity peer;
90 struct PropManipulationEntry *head;
91 struct PropManipulationEntry *tail;
94 * Peer specific manipulation metrics
96 uint32_t metrics [TM_BOTH][GNUNET_ATS_QualityPropertiesCount];
99 * Task to schedule delayed sendding
101 GNUNET_SCHEDULER_TaskIdentifier send_delay_task;
104 * Send queue DLL head
106 struct DelayQueueEntry *send_head;
109 * Send queue DLL tail
111 struct DelayQueueEntry *send_tail;
115 struct GST_ManipulationHandle
118 * Hashmap contain all peers currently manipulated
120 struct GNUNET_CONTAINER_MultiHashMap *peers;
123 * Peer containing information for general manipulation
125 struct TM_Peer general;
131 * Entry in the delay queue for an outbound delayed message
133 struct DelayQueueEntry
138 struct DelayQueueEntry *prev;
143 struct DelayQueueEntry *next;
146 * Peer this entry is belonging to
147 * if (NULL == tmp): enqueued in generic DLL and scheduled by generic_send_delay_task
148 * else: enqueued in tmp->send_head and tmp->send_tail and scheduled by tmp->send_delay_task
155 struct GNUNET_PeerIdentity id;
158 * Absolute time when to send
160 struct GNUNET_TIME_Absolute sent_at;
175 struct GNUNET_TIME_Relative timeout;
178 * Transports send continuation
180 GST_NeighbourSendContinuation cont;
183 * Transports send continuation cls
189 * DLL head for delayed messages based on general delay
191 struct DelayQueueEntry *generic_dqe_head;
194 * DLL tail for delayed messages based on general delay
196 struct DelayQueueEntry *generic_dqe_tail;
199 * Task to schedule delayed sending based on general delay
201 GNUNET_SCHEDULER_TaskIdentifier generic_send_delay_task;
204 set_metric (struct TM_Peer *dest, int direction, uint32_t type, uint32_t value)
206 struct PropManipulationEntry *cur;
207 for (cur = dest->head; NULL != cur; cur = cur->next)
209 if (cur->type == type)
214 cur = GNUNET_malloc (sizeof (struct PropManipulationEntry));
215 GNUNET_CONTAINER_DLL_insert (dest->head, dest->tail, cur);
217 cur->metrics[TM_SEND] = UINT32_MAX;
218 cur->metrics[TM_RECEIVE] = UINT32_MAX;
224 cur->metrics[TM_SEND] = value;
225 cur->metrics[TM_RECEIVE] = value;
228 cur->metrics[TM_SEND] = value;
231 cur->metrics[TM_RECEIVE] = value;
240 find_metric (struct TM_Peer *dest, uint32_t type, int direction)
242 struct PropManipulationEntry *cur;
244 for (cur = dest->head; NULL != cur; cur = cur->next)
246 if (cur->type == type)
247 return cur->metrics[direction];
254 * Clean up metrics for a peer
258 free_metric (struct TM_Peer *dest)
260 struct PropManipulationEntry *cur;
261 struct PropManipulationEntry *next;
263 for (cur = dest->head; NULL != cur; cur = next)
266 GNUNET_CONTAINER_DLL_remove (dest->head, dest->tail, cur);
273 * Set traffic metric to manipulate
276 * @param client client sending message
277 * @param message containing information
280 GST_manipulation_set_metric (void *cls, struct GNUNET_SERVER_Client *client,
281 const struct GNUNET_MessageHeader *message)
283 struct TrafficMetricMessage *tm = (struct TrafficMetricMessage *) message;
284 struct GNUNET_PeerIdentity dummy;
285 struct GNUNET_ATS_Information *ats;
293 if (0 == ntohs (tm->ats_count))
294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
297 switch (ntohs(tm->direction)) {
302 direction = TM_RECEIVE;
311 memset (&dummy, '\0', sizeof (struct GNUNET_PeerIdentity));
312 if (0 == memcmp (&tm->peer, &dummy, sizeof (struct GNUNET_PeerIdentity)))
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received traffic metrics for all peers \n");
316 ats = (struct GNUNET_ATS_Information *) &tm[1];
317 for (c = 0; c < ntohs (tm->ats_count); c++)
319 type = htonl (ats[c].type);
320 value = htonl (ats[c].value);
321 set_metric (&man_handle.general, direction, type, value);
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received traffic metrics for peer `%s'\n",
327 GNUNET_i2s(&tm->peer));
329 if (NULL == (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &tm->peer.hashPubKey)))
331 tmp = GNUNET_malloc (sizeof (struct TM_Peer));
332 tmp->peer = (tm->peer);
333 for (c = 0; c < TM_BOTH; c++)
335 for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
337 tmp->metrics[c][c2] = UINT32_MAX;
340 GNUNET_CONTAINER_multihashmap_put (man_handle.peers, &tm->peer.hashPubKey, tmp, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
343 ats = (struct GNUNET_ATS_Information *) &tm[1];
344 for (c = 0; c < ntohs (tm->ats_count); c++)
346 type = htonl (ats[c].type);
347 value = htonl (ats[c].value);
348 set_metric (tmp, direction, type, value);
351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
355 send_delayed (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
357 struct DelayQueueEntry *dqe = cls;
358 struct DelayQueueEntry *next;
359 struct TM_Peer *tmp = dqe->tmp;
360 struct GNUNET_TIME_Relative delay;
364 GNUNET_break (GNUNET_YES == GST_neighbours_test_connected (&dqe->id));
365 tmp->send_delay_task = GNUNET_SCHEDULER_NO_TASK;
366 GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
367 GST_neighbours_send (&dqe->id, dqe->msg, dqe->msg_size, dqe->timeout, dqe->cont, dqe->cont_cls);
369 next = tmp->send_head;
372 /* More delayed messages */
373 delay = GNUNET_TIME_absolute_get_remaining (next->sent_at);
374 tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, next);
379 /* Remove from generic queue */
380 GNUNET_break (GNUNET_YES == GST_neighbours_test_connected (&dqe->id));
381 generic_send_delay_task = GNUNET_SCHEDULER_NO_TASK;
382 GNUNET_CONTAINER_DLL_remove (generic_dqe_head, generic_dqe_tail, dqe);
383 GST_neighbours_send (&dqe->id, dqe->msg, dqe->msg_size, dqe->timeout, dqe->cont, dqe->cont_cls);
384 next = generic_dqe_head;
387 /* More delayed messages */
388 delay = GNUNET_TIME_absolute_get_remaining (next->sent_at);
389 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, next);
397 * Adapter function between transport's send function and transport plugins
399 * @param target the peer the message to send to
400 * @param msg the message received
401 * @param msg_size message size
402 * @param timeout timeout
403 * @param cont the continuation to call after sending
404 * @param cont_cls cls for continuation
407 GST_manipulation_send (const struct GNUNET_PeerIdentity *target, const void *msg,
408 size_t msg_size, struct GNUNET_TIME_Relative timeout,
409 GST_NeighbourSendContinuation cont, void *cont_cls)
412 struct DelayQueueEntry *dqe;
413 struct GNUNET_TIME_Relative delay;
415 if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &target->hashPubKey)))
417 GNUNET_break (GNUNET_YES == GST_neighbours_test_connected(target));
418 /* Manipulate here */
420 if (UINT32_MAX != find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND))
422 /* We have a delay */
423 delay.rel_value = find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND);
424 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
427 dqe->sent_at = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), delay);
429 dqe->cont_cls = cont_cls;
431 dqe->msg_size = msg_size;
432 dqe->timeout = timeout;
433 memcpy (dqe->msg, msg, msg_size);
434 GNUNET_CONTAINER_DLL_insert_tail (tmp->send_head, tmp->send_tail, dqe);
435 if (GNUNET_SCHEDULER_NO_TASK == tmp->send_delay_task)
436 tmp->send_delay_task =GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438 "Delaying %u byte message to peer `%s' with generic delay for %llu ms\n",
439 msg_size, GNUNET_i2s (target), (long long unsigned int) delay.rel_value);
443 else if (UINT32_MAX != find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND))
445 GNUNET_break (GNUNET_YES == GST_neighbours_test_connected(target));
446 /* We have a delay */
447 delay.rel_value = find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND);
448 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
451 dqe->sent_at = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), delay);
453 dqe->cont_cls = cont_cls;
455 dqe->msg_size = msg_size;
456 dqe->timeout = timeout;
457 memcpy (dqe->msg, msg, msg_size);
458 GNUNET_CONTAINER_DLL_insert_tail (generic_dqe_head, generic_dqe_tail, dqe);
459 if (GNUNET_SCHEDULER_NO_TASK == generic_send_delay_task)
461 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed (delay, &send_delayed, dqe);
463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
464 "Delaying %u byte message to peer `%s' with peer specific delay for %llu ms\n",
465 msg_size, GNUNET_i2s (target), (long long unsigned int) delay.rel_value);
470 GST_neighbours_send (target, msg, msg_size, timeout, cont, cont_cls);
475 * Function that will be called to manipulate ATS information according to
476 * current manipulation settings
478 * @param peer the peer
479 * @param address binary address
480 * @param session the session
481 * @param ats the ats information
482 * @param ats_count the number of ats information
484 struct GNUNET_ATS_Information *
485 GST_manipulation_manipulate_metrics (const struct GNUNET_PeerIdentity *peer,
486 const struct GNUNET_HELLO_Address *address,
487 struct Session *session,
488 const struct GNUNET_ATS_Information *ats,
491 struct GNUNET_ATS_Information *ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *ats_count);
496 tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey);
498 for (d = 0; d < ats_count; d++)
504 m_tmp = find_metric (tmp, ntohl(ats[d].type), TM_RECEIVE);
505 g_tmp = find_metric (&man_handle.general, ntohl(ats[d].type), TM_RECEIVE);
507 if (UINT32_MAX != g_tmp)
508 ats_new[d].value = htonl(g_tmp);
509 if (UINT32_MAX != m_tmp)
510 ats_new[d].value = htonl(m_tmp);
518 * Adapter function between transport plugins and transport receive function
519 * manipulation delays for next send.
521 * @param cls the closure for transport
522 * @param peer the peer the message was received from
523 * @param message the message received
524 * @param session the session the message was received on
525 * @param sender_address the sender address
526 * @param sender_address_len the length of the sender address
527 * @return manipulated delay for next receive
529 struct GNUNET_TIME_Relative
530 GST_manipulation_recv (void *cls,
531 const struct GNUNET_PeerIdentity *peer,
532 const struct GNUNET_MessageHeader *message,
533 struct Session *session,
534 const char *sender_address,
535 uint16_t sender_address_len)
538 uint32_t p_recv_delay;
539 uint32_t g_recv_delay;
540 struct GNUNET_TIME_Relative quota_delay;
541 struct GNUNET_TIME_Relative m_delay;
543 g_recv_delay = find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_RECEIVE);
544 if ((g_recv_delay >= GNUNET_TIME_UNIT_ZERO.rel_value) && (UINT32_MAX != g_recv_delay))
545 m_delay.rel_value = g_recv_delay; /* Global delay */
547 m_delay = GNUNET_TIME_UNIT_ZERO;
549 if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey)))
551 /* Manipulate receive delay */
552 p_recv_delay = find_metric (tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_RECEIVE);
553 if (UINT32_MAX != p_recv_delay)
554 m_delay.rel_value = p_recv_delay; /* Peer specific delay */
557 quota_delay = GST_receive_callback (cls, peer, message,
558 session, sender_address, sender_address_len);
560 if (quota_delay.rel_value > m_delay.rel_value)
561 m_delay = quota_delay;
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Delaying next receive for peer `%s' for %llu ms\n",
565 GNUNET_i2s (peer), (long long unsigned int) m_delay.rel_value);
572 * Initialize traffic manipulation
574 * @param GST_cfg configuration handle
577 GST_manipulation_init (const struct GNUNET_CONFIGURATION_Handle *GST_cfg)
579 unsigned long long tmp;
581 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
582 "transport", "MANIPULATE_DISTANCE_IN", &tmp) && (tmp > 0))
584 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting inbound distance_in to %u\n",
585 (unsigned long long) tmp);
586 set_metric (&man_handle.general, TM_RECEIVE, GNUNET_ATS_QUALITY_NET_DISTANCE, tmp);
589 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
590 "transport", "MANIPULATE_DISTANCE_OUT", &tmp) && (tmp > 0))
592 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting outbound distance_in to %u\n",
593 (unsigned long long) tmp);
594 set_metric (&man_handle.general, TM_SEND, GNUNET_ATS_QUALITY_NET_DISTANCE, tmp);
597 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
598 "transport", "MANIPULATE_DELAY_IN", &tmp) && (tmp > 0))
600 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying inbound traffic for %llu ms\n",
601 (unsigned long long) tmp);
602 set_metric (&man_handle.general, TM_RECEIVE, GNUNET_ATS_QUALITY_NET_DELAY, tmp);
606 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (GST_cfg,
607 "transport", "MANIPULATE_DELAY_OUT", &tmp) && (tmp > 0))
609 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Delaying outbound traffic for %llu ms\n",
610 (unsigned long long) tmp);
611 set_metric (&man_handle.general, TM_SEND, GNUNET_ATS_QUALITY_NET_DELAY, tmp);
614 man_handle.peers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
619 free_tmps (void *cls,
620 const struct GNUNET_HashCode * key,
623 struct DelayQueueEntry *dqe;
624 struct DelayQueueEntry *next;
627 struct TM_Peer *tmp = (struct TM_Peer *) value;
628 GNUNET_CONTAINER_multihashmap_remove (man_handle.peers, key, value);
630 next = tmp->send_head;
631 while (NULL != (dqe = next))
634 GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
635 if (NULL != dqe->cont)
636 dqe->cont (dqe->cont_cls, GNUNET_SYSERR, dqe->msg_size, 0);
639 if (GNUNET_SCHEDULER_NO_TASK != tmp->send_delay_task)
641 GNUNET_SCHEDULER_cancel (tmp->send_delay_task);
642 tmp->send_delay_task = GNUNET_SCHEDULER_NO_TASK;
650 * Notify manipulation about disconnect so it can discard queued messages
652 * @param peer the disconnecting peer
655 GST_manipulation_peer_disconnect (const struct GNUNET_PeerIdentity *peer)
658 struct DelayQueueEntry *dqe;
659 struct DelayQueueEntry *next;
661 if (NULL != (tmp = GNUNET_CONTAINER_multihashmap_get (man_handle.peers, &peer->hashPubKey)))
663 next = tmp->send_head;
664 while (NULL != (dqe = next))
667 GNUNET_CONTAINER_DLL_remove (tmp->send_head, tmp->send_tail, dqe);
668 if (NULL != dqe->cont)
669 dqe->cont (dqe->cont_cls, GNUNET_SYSERR, dqe->msg_size, 0);
673 else if (UINT32_MAX != find_metric (&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND))
675 next = generic_dqe_head;
676 while (NULL != (dqe = next))
679 if (0 == memcmp (peer, &dqe->id, sizeof (dqe->id)))
681 GNUNET_CONTAINER_DLL_remove (generic_dqe_head, generic_dqe_tail, dqe);
682 if (NULL != dqe->cont)
683 dqe->cont (dqe->cont_cls, GNUNET_SYSERR, dqe->msg_size, 0);
687 if (GNUNET_SCHEDULER_NO_TASK != generic_send_delay_task)
689 GNUNET_SCHEDULER_cancel (generic_send_delay_task);
690 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining(generic_dqe_head->sent_at), &send_delayed, generic_dqe_head);
697 * Stop traffic manipulation
700 GST_manipulation_stop ()
702 struct DelayQueueEntry *cur;
703 struct DelayQueueEntry *next;
704 GNUNET_CONTAINER_multihashmap_iterate (man_handle.peers, &free_tmps,NULL);
705 GNUNET_CONTAINER_multihashmap_destroy (man_handle.peers);
707 next = generic_dqe_head;
708 while (NULL != (cur = next))
711 GNUNET_CONTAINER_DLL_remove (generic_dqe_head, generic_dqe_tail, cur);
712 if (NULL != cur->cont)
713 cur->cont (cur->cont_cls, GNUNET_SYSERR, cur->msg_size, 0);
716 if (GNUNET_SCHEDULER_NO_TASK != generic_send_delay_task)
718 GNUNET_SCHEDULER_cancel (generic_send_delay_task);
719 generic_send_delay_task = GNUNET_SCHEDULER_NO_TASK;
722 free_metric (&man_handle.general);
723 man_handle.peers = NULL;
727 /* end of file gnunet-service-transport_manipulation.c */