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
39 TM_SEND = 0, TM_RECEIVE = 1, TM_BOTH = 2
44 * Struct containing information about manipulations to a specific peer
51 struct PropManipulationEntry
56 struct PropManipulationEntry *next;
61 struct PropManipulationEntry *prev;
71 uint32_t metrics[TM_BOTH];
76 * Struct containing information about manipulations to a specific peer
83 struct GNUNET_PeerIdentity peer;
85 struct PropManipulationEntry *head;
86 struct PropManipulationEntry *tail;
89 * Peer specific manipulation metrics
91 uint32_t metrics[TM_BOTH][GNUNET_ATS_QualityPropertiesCount];
94 * Task to schedule delayed sendding
96 struct GNUNET_SCHEDULER_Task * send_delay_task;
101 struct DelayQueueEntry *send_head;
104 * Send queue DLL tail
106 struct DelayQueueEntry *send_tail;
110 struct GST_ManipulationHandle
113 * Hashmap contain all peers currently manipulated
115 struct GNUNET_CONTAINER_MultiPeerMap *peers;
118 * Peer containing information for general manipulation
120 struct TM_Peer general;
124 * Entry in the delay queue for an outbound delayed message
126 struct DelayQueueEntry
131 struct DelayQueueEntry *prev;
136 struct DelayQueueEntry *next;
139 * Peer this entry is belonging to
140 * if (NULL == tmp): enqueued in generic DLL and scheduled by generic_send_delay_task
141 * else: enqueued in tmp->send_head and tmp->send_tail and scheduled by tmp->send_delay_task
148 struct GNUNET_PeerIdentity id;
151 * Absolute time when to send
153 struct GNUNET_TIME_Absolute sent_at;
168 struct GNUNET_TIME_Relative timeout;
171 * Transports send continuation
173 GST_NeighbourSendContinuation cont;
176 * Transports send continuation cls
181 struct GST_ManipulationHandle man_handle;
184 * DLL head for delayed messages based on general delay
186 struct DelayQueueEntry *generic_dqe_head;
189 * DLL tail for delayed messages based on general delay
191 struct DelayQueueEntry *generic_dqe_tail;
194 * Task to schedule delayed sending based on general delay
196 struct GNUNET_SCHEDULER_Task * generic_send_delay_task;
199 set_metric(struct TM_Peer *dest, int direction, uint32_t type, uint32_t value)
201 struct PropManipulationEntry *cur;
202 for (cur = dest->head; NULL != cur; cur = cur->next)
204 if (cur->type == type)
209 cur = GNUNET_new (struct PropManipulationEntry);
210 GNUNET_CONTAINER_DLL_insert(dest->head, dest->tail, cur);
212 cur->metrics[TM_SEND] = UINT32_MAX;
213 cur->metrics[TM_RECEIVE] = UINT32_MAX;
219 cur->metrics[TM_SEND] = value;
220 cur->metrics[TM_RECEIVE] = value;
223 cur->metrics[TM_SEND] = value;
226 cur->metrics[TM_RECEIVE] = value;
235 find_metric(struct TM_Peer *dest, uint32_t type, int direction)
237 struct PropManipulationEntry *cur;
239 for (cur = dest->head; NULL != cur; cur = cur->next)
241 if (cur->type == type)
242 return cur->metrics[direction];
249 * Clean up metrics for a peer
253 free_metric(struct TM_Peer *dest)
255 struct PropManipulationEntry *cur;
256 struct PropManipulationEntry *next;
258 for (cur = dest->head; NULL != cur; cur = next)
261 GNUNET_CONTAINER_DLL_remove(dest->head, dest->tail, cur);
267 * Set traffic metric to manipulate
270 * @param client client sending message
271 * @param message containing information
274 GST_manipulation_set_metric(void *cls, struct GNUNET_SERVER_Client *client,
275 const struct GNUNET_MessageHeader *message)
277 struct TrafficMetricMessage *tm = (struct TrafficMetricMessage *) message;
278 struct GNUNET_PeerIdentity dummy;
279 struct GNUNET_ATS_Information *ats;
287 if (0 == ntohs(tm->ats_count))
288 GNUNET_SERVER_receive_done(client, GNUNET_SYSERR);
291 switch (ntohs(tm->direction))
297 direction = TM_RECEIVE;
306 memset(&dummy, '\0', sizeof(struct GNUNET_PeerIdentity));
307 if (0 == memcmp(&tm->peer, &dummy, sizeof(struct GNUNET_PeerIdentity)))
309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
310 "Received traffic metrics for all peers \n");
312 ats = (struct GNUNET_ATS_Information *) &tm[1];
313 for (c = 0; c < ntohs(tm->ats_count); c++)
315 type = htonl(ats[c].type);
316 value = htonl(ats[c].value);
317 set_metric(&man_handle.general, direction, type, value);
322 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
323 "Received traffic metrics for peer `%s'\n", GNUNET_i2s(&tm->peer));
326 == (tmp = GNUNET_CONTAINER_multipeermap_get(man_handle.peers, &tm->peer)))
328 tmp = GNUNET_new (struct TM_Peer);
329 tmp->peer = (tm->peer);
330 for (c = 0; c < TM_BOTH; c++)
332 for (c2 = 0; c2 < GNUNET_ATS_QualityPropertiesCount; c2++)
334 tmp->metrics[c][c2] = UINT32_MAX;
337 GNUNET_CONTAINER_multipeermap_put(man_handle.peers, &tm->peer, tmp,
338 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
341 ats = (struct GNUNET_ATS_Information *) &tm[1];
342 for (c = 0; c < ntohs(tm->ats_count); c++)
344 type = htonl(ats[c].type);
345 value = htonl(ats[c].value);
346 set_metric(tmp, direction, type, value);
349 GNUNET_SERVER_receive_done(client, GNUNET_OK);
353 send_delayed(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
355 struct DelayQueueEntry *dqe = cls;
356 struct DelayQueueEntry *next;
357 struct TM_Peer *tmp = dqe->tmp;
358 struct GNUNET_TIME_Relative delay;
362 GNUNET_break(GNUNET_YES == GST_neighbours_test_connected (&dqe->id));
363 tmp->send_delay_task = NULL;
364 GNUNET_CONTAINER_DLL_remove(tmp->send_head, tmp->send_tail, dqe);
365 GST_neighbours_send(&dqe->id, dqe->msg, dqe->msg_size, dqe->timeout,
366 dqe->cont, dqe->cont_cls);
368 next = tmp->send_head;
371 /* More delayed messages */
372 delay = GNUNET_TIME_absolute_get_remaining(next->sent_at);
373 tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed(delay,
374 &send_delayed, next);
379 /* Remove from generic queue */
380 GNUNET_break(GNUNET_YES == GST_neighbours_test_connected (&dqe->id));
381 generic_send_delay_task = NULL;
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,
384 dqe->cont, dqe->cont_cls);
385 next = generic_dqe_head;
388 /* More delayed messages */
389 delay = GNUNET_TIME_absolute_get_remaining(next->sent_at);
390 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed(delay,
391 &send_delayed, next);
398 * Adapter function between transport's send function and transport plugins
400 * @param target the peer the message to send to
401 * @param msg the message received
402 * @param msg_size message size
403 * @param timeout timeout
404 * @param cont the continuation to call after sending
405 * @param cont_cls cls for continuation
408 GST_manipulation_send(const struct GNUNET_PeerIdentity *target, const void *msg,
409 size_t msg_size, struct GNUNET_TIME_Relative timeout,
410 GST_NeighbourSendContinuation cont, void *cont_cls)
413 struct DelayQueueEntry *dqe;
414 struct GNUNET_TIME_Relative delay;
417 != (tmp = GNUNET_CONTAINER_multipeermap_get(man_handle.peers, target)))
419 GNUNET_break(GNUNET_YES == GST_neighbours_test_connected(target));
420 /* Manipulate here */
422 if (UINT32_MAX != find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND))
424 /* We have a delay */
425 delay.rel_value_us = find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY,
427 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
430 dqe->sent_at = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
433 dqe->cont_cls = cont_cls;
435 dqe->msg_size = msg_size;
436 dqe->timeout = timeout;
437 memcpy(dqe->msg, msg, msg_size);
438 GNUNET_CONTAINER_DLL_insert_tail(tmp->send_head, tmp->send_tail, dqe);
439 if (NULL == tmp->send_delay_task)
440 tmp->send_delay_task = GNUNET_SCHEDULER_add_delayed(delay,
442 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
443 "Delaying %u byte message to peer `%s' with generic delay for %ms\n", msg_size, GNUNET_i2s (target), GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
448 != find_metric(&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY,
451 GNUNET_break(GNUNET_YES == GST_neighbours_test_connected(target));
452 /* We have a delay */
453 delay.rel_value_us = find_metric(&man_handle.general,
454 GNUNET_ATS_QUALITY_NET_DELAY, TM_SEND);
455 dqe = GNUNET_malloc (sizeof (struct DelayQueueEntry) + msg_size);
458 dqe->sent_at = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
461 dqe->cont_cls = cont_cls;
463 dqe->msg_size = msg_size;
464 dqe->timeout = timeout;
465 memcpy(dqe->msg, msg, msg_size);
466 GNUNET_CONTAINER_DLL_insert_tail(generic_dqe_head, generic_dqe_tail, dqe);
467 if (NULL == generic_send_delay_task)
469 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed(delay,
472 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
473 "Delaying %u byte message to peer `%s' with peer specific delay for %s\n", msg_size, GNUNET_i2s (target), GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
478 GST_neighbours_send(target, msg, msg_size, timeout, cont, cont_cls);
482 * Function that will be called to manipulate ATS information according to
483 * current manipulation settings
485 * @param peer the peer
486 * @param address binary address
487 * @param session the session
488 * @param ats the ats information
489 * @param ats_count the number of ats information
491 struct GNUNET_ATS_Information *
492 GST_manipulation_manipulate_metrics(const struct GNUNET_HELLO_Address *address,
493 struct Session *session,
494 const struct GNUNET_ATS_Information *ats,
497 const struct GNUNET_PeerIdentity *peer = &address->peer;
498 struct GNUNET_ATS_Information *ats_new;
506 ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) * ats_count);
507 tmp = GNUNET_CONTAINER_multipeermap_get (man_handle.peers, peer);
508 for (d = 0; d < ats_count; d++)
513 m_tmp = find_metric(tmp, ntohl(ats[d].type), TM_RECEIVE);
514 g_tmp = find_metric(&man_handle.general, ntohl(ats[d].type), TM_RECEIVE);
516 if (UINT32_MAX != g_tmp)
517 ats_new[d].value = htonl(g_tmp);
518 if (UINT32_MAX != m_tmp)
519 ats_new[d].value = htonl(m_tmp);
526 * Adapter function between transport plugins and transport receive function
527 * manipulation delays for next send.
529 * @param cls the closure for transport
530 * @param address the address and the peer the message was received from
531 * @param message the message received
532 * @param session the session the message was received on
533 * @return manipulated delay for next receive
535 struct GNUNET_TIME_Relative
536 GST_manipulation_recv (void *cls,
537 const struct GNUNET_HELLO_Address *address,
538 struct Session *session,
539 const struct GNUNET_MessageHeader *message)
542 uint32_t p_recv_delay;
543 uint32_t g_recv_delay;
544 struct GNUNET_TIME_Relative quota_delay;
545 struct GNUNET_TIME_Relative m_delay;
547 g_recv_delay = find_metric(&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY,
549 if ((g_recv_delay >= GNUNET_TIME_UNIT_ZERO.rel_value_us)
550 && (UINT32_MAX != g_recv_delay))
551 m_delay.rel_value_us = g_recv_delay; /* Global delay */
553 m_delay = GNUNET_TIME_UNIT_ZERO;
555 if (NULL != (tmp = GNUNET_CONTAINER_multipeermap_get(man_handle.peers, &address->peer)))
557 /* Manipulate receive delay */
558 p_recv_delay = find_metric(tmp, GNUNET_ATS_QUALITY_NET_DELAY, TM_RECEIVE);
559 if (UINT32_MAX != p_recv_delay)
560 m_delay.rel_value_us = p_recv_delay; /* Peer specific delay */
563 quota_delay = GST_receive_callback(cls, address, session, message);
565 if (quota_delay.rel_value_us > m_delay.rel_value_us)
566 m_delay = quota_delay;
568 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
569 "Delaying next receive for peer `%s' for %s\n",
570 GNUNET_i2s (&address->peer),
571 GNUNET_STRINGS_relative_time_to_string (m_delay, GNUNET_YES));
577 * Initialize traffic manipulation
579 * @param GST_cfg configuration handle
582 GST_manipulation_init(const struct GNUNET_CONFIGURATION_Handle *GST_cfg)
584 unsigned long long tmp;
585 struct GNUNET_TIME_Relative delay;
588 == GNUNET_CONFIGURATION_get_value_number(GST_cfg, "transport",
589 "MANIPULATE_DISTANCE_IN", &tmp)) && (tmp > 0))
591 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
592 "Setting inbound distance_in to %llu\n", (unsigned long long) tmp);
593 set_metric(&man_handle.general, TM_RECEIVE,
594 GNUNET_ATS_QUALITY_NET_DISTANCE, tmp);
598 == GNUNET_CONFIGURATION_get_value_number(GST_cfg, "transport",
599 "MANIPULATE_DISTANCE_OUT", &tmp)) && (tmp > 0))
601 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
602 "Setting outbound distance_in to %llu\n", (unsigned long long) tmp);
603 set_metric(&man_handle.general, TM_SEND, GNUNET_ATS_QUALITY_NET_DISTANCE,
608 == GNUNET_CONFIGURATION_get_value_time(GST_cfg, "transport",
609 "MANIPULATE_DELAY_IN", &delay)) && (delay.rel_value_us > 0))
611 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
612 "Delaying inbound traffic for %s\n", GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
613 set_metric(&man_handle.general, TM_RECEIVE, GNUNET_ATS_QUALITY_NET_DELAY,
617 == GNUNET_CONFIGURATION_get_value_time(GST_cfg, "transport",
618 "MANIPULATE_DELAY_OUT", &delay)) && (delay.rel_value_us > 0))
620 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
621 "Delaying outbound traffic for %s\n", GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
622 set_metric(&man_handle.general, TM_SEND, GNUNET_ATS_QUALITY_NET_DELAY,
625 man_handle.peers = GNUNET_CONTAINER_multipeermap_create(10, GNUNET_NO);
629 free_tmps(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
631 struct DelayQueueEntry *dqe;
632 struct DelayQueueEntry *next;
636 struct TM_Peer *tmp = (struct TM_Peer *) value;
639 != GNUNET_CONTAINER_multipeermap_remove(man_handle.peers, key, value))
642 next = tmp->send_head;
643 while (NULL != (dqe = next))
646 GNUNET_CONTAINER_DLL_remove(tmp->send_head, tmp->send_tail, dqe);
647 if (NULL != dqe->cont)
648 dqe->cont(dqe->cont_cls, GNUNET_SYSERR, dqe->msg_size, 0);
651 if (NULL != tmp->send_delay_task)
653 GNUNET_SCHEDULER_cancel(tmp->send_delay_task);
654 tmp->send_delay_task = NULL;
662 * Notify manipulation about disconnect so it can discard queued messages
664 * @param peer the disconnecting peer
667 GST_manipulation_peer_disconnect(const struct GNUNET_PeerIdentity *peer)
670 struct DelayQueueEntry *dqe;
671 struct DelayQueueEntry *next;
673 if (NULL != (tmp = GNUNET_CONTAINER_multipeermap_get(man_handle.peers, peer)))
675 next = tmp->send_head;
676 while (NULL != (dqe = next))
679 GNUNET_CONTAINER_DLL_remove(tmp->send_head, tmp->send_tail, dqe);
680 if (NULL != dqe->cont)
681 dqe->cont(dqe->cont_cls, GNUNET_SYSERR, dqe->msg_size, 0);
686 != find_metric(&man_handle.general, GNUNET_ATS_QUALITY_NET_DELAY,
689 next = generic_dqe_head;
690 while (NULL != (dqe = next))
693 if (0 == memcmp(peer, &dqe->id, sizeof(dqe->id)))
695 GNUNET_CONTAINER_DLL_remove(generic_dqe_head, generic_dqe_tail,
697 if (NULL != dqe->cont)
698 dqe->cont(dqe->cont_cls, GNUNET_SYSERR, dqe->msg_size, 0);
702 if (NULL != generic_send_delay_task)
704 GNUNET_SCHEDULER_cancel(generic_send_delay_task);
705 generic_send_delay_task = NULL;
706 if (NULL != generic_dqe_head)
707 generic_send_delay_task = GNUNET_SCHEDULER_add_delayed(
708 GNUNET_TIME_absolute_get_remaining(generic_dqe_head->sent_at),
709 &send_delayed, generic_dqe_head);
715 * Stop traffic manipulation
718 GST_manipulation_stop()
720 struct DelayQueueEntry *cur;
721 struct DelayQueueEntry *next;
722 GNUNET_CONTAINER_multipeermap_iterate(man_handle.peers, &free_tmps, NULL);
723 GNUNET_CONTAINER_multipeermap_destroy(man_handle.peers);
725 next = generic_dqe_head;
726 while (NULL != (cur = next))
729 GNUNET_CONTAINER_DLL_remove(generic_dqe_head, generic_dqe_tail, cur);
730 if (NULL != cur->cont)
731 cur->cont(cur->cont_cls, GNUNET_SYSERR, cur->msg_size, 0);
734 if (NULL != generic_send_delay_task)
736 GNUNET_SCHEDULER_cancel(generic_send_delay_task);
737 generic_send_delay_task = NULL;
740 free_metric(&man_handle.general);
741 man_handle.peers = NULL;
744 /* end of file gnunet-service-transport_manipulation.c */