2 This file is part of GNUnet.
3 Copyright (C) 2010, 2011, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file ats/ats_api_performance.c
22 * @brief automatic transport selection and outbound bandwidth determination
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
31 #define LOG(kind,...) GNUNET_log_from(kind, "ats-performance-api", __VA_ARGS__)
35 * Linked list of pending reservations.
37 struct GNUNET_ATS_ReservationContext
43 struct GNUNET_ATS_ReservationContext *next;
48 struct GNUNET_ATS_ReservationContext *prev;
53 struct GNUNET_PeerIdentity peer;
61 * Function to call on result.
63 GNUNET_ATS_ReservationCallback rcb;
71 * Do we need to undo this reservation if it succeeded? Set to
72 * #GNUNET_YES if a reservation is cancelled. (at that point, 'info'
73 * is also set to NULL; however, info will ALSO be NULL for the
74 * reservation context that is created to undo the original request,
75 * so 'info' being NULL cannot be used to check if undo is
83 * Linked list of pending reservations.
85 struct GNUNET_ATS_AddressListHandle
91 struct GNUNET_ATS_AddressListHandle *next;
96 struct GNUNET_ATS_AddressListHandle *prev;
101 struct GNUNET_ATS_PerformanceHandle *ph;
106 GNUNET_ATS_AddressInformationCallback cb;
109 * Callback closure for @e cb
116 struct GNUNET_PeerIdentity peer;
119 * Return all or specific peer only
124 * Return all or used address only
129 * Request multiplexing
136 * ATS Handle to obtain and/or modify performance information.
138 struct GNUNET_ATS_PerformanceHandle
144 const struct GNUNET_CONFIGURATION_Handle *cfg;
147 * Callback to invoke when an address has performance changes.
149 GNUNET_ATS_AddressInformationCallback addr_info_cb;
152 * Closure for @e addr_info_cb.
154 void *addr_info_cb_cls;
157 * Connection to ATS service.
159 struct GNUNET_MQ_Handle *mq;
162 * Head of linked list of pending reservation requests.
164 struct GNUNET_ATS_ReservationContext *reservation_head;
167 * Tail of linked list of pending reservation requests.
169 struct GNUNET_ATS_ReservationContext *reservation_tail;
172 * Head of linked list of pending address list requests.
174 struct GNUNET_ATS_AddressListHandle *addresslist_head;
177 * Tail of linked list of pending address list requests.
179 struct GNUNET_ATS_AddressListHandle *addresslist_tail;
182 * Current request for transmission to ATS.
184 struct GNUNET_CLIENT_TransmitHandle *th;
187 * Task to trigger reconnect.
189 struct GNUNET_SCHEDULER_Task *task;
192 * Reconnect backoff delay.
194 struct GNUNET_TIME_Relative backoff;
197 * Monitor request multiplexing
202 * Request multiplexing
207 * Is the receive loop active?
213 * Re-establish the connection to the ATS service.
215 * @param ph handle to use to re-connect.
218 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
222 * Re-establish the connection to the ATS service.
224 * @param cls handle to use to re-connect.
227 reconnect_task (void *cls)
229 struct GNUNET_ATS_PerformanceHandle *ph = cls;
237 * Reconnect to the ATS service, something went wrong.
239 * @param ph handle to reconnect
242 do_reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
244 struct GNUNET_ATS_ReservationContext *rc;
245 struct GNUNET_ATS_AddressListHandle *alh;
246 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
250 GNUNET_MQ_destroy (ph->mq);
253 while (NULL != (rc = ph->reservation_head))
255 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
256 ph->reservation_tail,
259 rc->rcb (rc->rcb_cls,
262 GNUNET_TIME_UNIT_FOREVER_REL);
265 bandwidth_zero.value__ = htonl (0);
266 while (NULL != (alh = ph->addresslist_head))
268 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
269 ph->addresslist_tail,
272 alh->cb (alh->cb_cls,
280 if (NULL != ph->addr_info_cb)
282 /* Indicate reconnect */
283 ph->addr_info_cb (ph->addr_info_cb_cls,
290 ph->backoff = GNUNET_TIME_STD_BACKOFF (ph->backoff);
291 ph->task = GNUNET_SCHEDULER_add_delayed (ph->backoff,
298 * We received a peer information message. Validate and process it.
300 * @param cls our context with the callback
301 * @param pi the message
302 * @return #GNUNET_OK if the message was well-formed
305 check_peer_information (void *cls,
306 const struct PeerInformationMessage *pi)
308 const char *plugin_address;
309 const char *plugin_name;
310 uint16_t plugin_address_length;
311 uint16_t plugin_name_length;
313 plugin_address_length = ntohs (pi->address_length);
314 plugin_name_length = ntohs (pi->plugin_name_length);
315 plugin_address = (const char *) &pi[1];
316 plugin_name = &plugin_address[plugin_address_length];
317 if ( (plugin_address_length + plugin_name_length
318 + sizeof(struct PeerInformationMessage) != ntohs (pi->header.size)) ||
319 (plugin_name[plugin_name_length - 1] != '\0'))
322 return GNUNET_SYSERR;
329 * We received a peer information message. Validate and process it.
331 * @param cls our context with the callback
332 * @param pi the message
333 * @return #GNUNET_OK if the message was well-formed
336 handle_peer_information (void *cls,
337 const struct PeerInformationMessage *pi)
339 struct GNUNET_ATS_PerformanceHandle *ph = cls;
340 const char *plugin_address;
341 const char *plugin_name;
342 struct GNUNET_HELLO_Address address;
343 uint16_t plugin_address_length;
345 struct GNUNET_ATS_Properties prop;
347 if (NULL == ph->addr_info_cb)
349 plugin_address_length = ntohs (pi->address_length);
350 addr_active = (int) ntohl (pi->address_active);
351 plugin_address = (const char *) &pi[1];
352 plugin_name = &plugin_address[plugin_address_length];
354 GNUNET_ATS_properties_ntoh (&prop,
356 address.peer = pi->peer;
357 address.local_info = (enum GNUNET_HELLO_AddressInfo) ntohl (pi->address_local_info);
358 address.address = plugin_address;
359 address.address_length = plugin_address_length;
360 address.transport_name = plugin_name;
361 ph->addr_info_cb (ph->addr_info_cb_cls,
371 * We received a reservation result message. Validate and process it.
373 * @param cls our context with the callback
374 * @param rr the message
377 handle_reservation_result (void *cls,
378 const struct ReservationResultMessage *rr)
380 struct GNUNET_ATS_PerformanceHandle *ph = cls;
381 struct GNUNET_ATS_ReservationContext *rc;
384 amount = ntohl (rr->amount);
385 rc = ph->reservation_head;
386 if (0 != memcmp (&rr->peer,
388 sizeof(struct GNUNET_PeerIdentity)))
394 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
395 ph->reservation_tail,
397 if ( (0 == amount) ||
400 /* tell client if not cancelled */
402 rc->rcb (rc->rcb_cls,
405 GNUNET_TIME_relative_ntoh (rr->res_delay));
409 /* amount non-zero, but client cancelled, consider undo! */
410 if (GNUNET_YES != rc->undo)
413 return; /* do not try to undo failed undos or negative amounts */
416 (void) GNUNET_ATS_reserve_bandwidth (ph,
424 * We received a PeerInformationMessage. Validate it.
426 * @param cls our context with the callback
427 * @param pi the message
428 * @return #GNUNET_OK if the message was well-formed
431 check_address_list (void *cls,
432 const struct PeerInformationMessage *pi)
434 const char *plugin_address;
435 const char *plugin_name;
436 uint16_t plugin_address_length;
437 uint16_t plugin_name_length;
439 plugin_address_length = ntohs (pi->address_length);
440 plugin_name_length = ntohs (pi->plugin_name_length);
441 plugin_address = (const char *) &pi[1];
442 plugin_name = &plugin_address[plugin_address_length];
443 if ( (plugin_address_length + plugin_name_length
444 + sizeof (struct PeerInformationMessage) != ntohs (pi->header.size)) ||
445 (plugin_name[plugin_name_length - 1] != '\0') )
448 return GNUNET_SYSERR;
455 * We received a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE.
458 * @param cls our context with the callback
459 * @param pi the message
462 handle_address_list (void *cls,
463 const struct PeerInformationMessage *pi)
465 struct GNUNET_ATS_PerformanceHandle *ph = cls;
466 struct GNUNET_ATS_AddressListHandle *alh;
467 struct GNUNET_ATS_AddressListHandle *next;
468 const char *plugin_address;
469 const char *plugin_name;
470 struct GNUNET_HELLO_Address address;
471 struct GNUNET_PeerIdentity allzeros;
472 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
473 struct GNUNET_ATS_Properties prop;
474 uint16_t plugin_address_length;
475 uint16_t plugin_name_length;
480 active = ntohl (pi->address_active);
481 plugin_address_length = ntohs (pi->address_length);
482 plugin_name_length = ntohs (pi->plugin_name_length);
483 plugin_address = (const char *) &pi[1];
484 plugin_name = &plugin_address[plugin_address_length];
485 LOG (GNUNET_ERROR_TYPE_DEBUG,
486 "Received ATS_ADDRESSLIST_RESPONSE message for peer %s and plugin %s\n",
487 GNUNET_i2s (&pi->peer),
490 next = ph->addresslist_head;
491 while (NULL != (alh = next))
498 return; /* was canceled */
500 memset (&allzeros, '\0', sizeof (allzeros));
501 if ( (0 == memcmp (&allzeros, &pi->peer, sizeof(allzeros))) &&
502 (0 == plugin_name_length) &&
503 (0 == plugin_address_length) )
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "Received last message for ATS_ADDRESSLIST_RESPONSE\n");
508 bandwidth_zero.value__ = htonl (0);
509 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
510 ph->addresslist_tail,
513 alh->cb (alh->cb_cls,
523 address.peer = pi->peer;
524 address.address = plugin_address;
525 address.address_length = plugin_address_length;
526 address.transport_name = plugin_name;
527 if ( ( (GNUNET_YES == alh->all_addresses) ||
528 (GNUNET_YES == active) ) &&
531 GNUNET_ATS_properties_ntoh (&prop,
533 alh->cb (alh->cb_cls,
544 * Generic error handler, called with the appropriate error code and
545 * the same closure specified at the creation of the message queue.
546 * Not every message queue implementation supports an error handler.
548 * @param cls closure with the `struct GNUNET_ATS_PerformanceHandle *`
549 * @param error error code
552 mq_error_handler (void *cls,
553 enum GNUNET_MQ_Error error)
555 struct GNUNET_ATS_PerformanceHandle *ph = cls;
562 * Re-establish the connection to the ATS service.
564 * @param ph handle to use to re-connect.
567 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
569 struct GNUNET_MQ_MessageHandler handlers[] = {
570 GNUNET_MQ_hd_var_size (peer_information,
571 GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION,
572 struct PeerInformationMessage,
574 GNUNET_MQ_hd_fixed_size (reservation_result,
575 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT,
576 struct ReservationResultMessage,
578 GNUNET_MQ_hd_var_size (address_list,
579 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE,
580 struct PeerInformationMessage,
582 GNUNET_MQ_handler_end ()
584 struct GNUNET_MQ_Envelope *env;
585 struct ClientStartMessage *init;
587 GNUNET_assert (NULL == ph->mq);
588 ph->mq = GNUNET_CLIENT_connect (ph->cfg,
595 env = GNUNET_MQ_msg (init,
596 GNUNET_MESSAGE_TYPE_ATS_START);
597 init->start_flag = htonl ( (NULL == ph->addr_info_cb)
598 ? START_FLAG_PERFORMANCE_NO_PIC
599 : START_FLAG_PERFORMANCE_WITH_PIC);
600 GNUNET_MQ_send (ph->mq,
606 * Get handle to access performance API of the ATS subsystem.
608 * @param cfg configuration to use
609 * @param addr_info_cb callback called when performance characteristics for
611 * @param addr_info_cb_cls closure for @a addr_info_cb
612 * @return ats performance context
614 struct GNUNET_ATS_PerformanceHandle *
615 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
616 GNUNET_ATS_AddressInformationCallback addr_info_cb,
617 void *addr_info_cb_cls)
619 struct GNUNET_ATS_PerformanceHandle *ph;
621 ph = GNUNET_new (struct GNUNET_ATS_PerformanceHandle);
623 ph->addr_info_cb = addr_info_cb;
624 ph->addr_info_cb_cls = addr_info_cb_cls;
636 * Client is done using the ATS performance subsystem, release resources.
641 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
643 struct GNUNET_ATS_ReservationContext *rc;
644 struct GNUNET_ATS_AddressListHandle *alh;
646 while (NULL != (alh = ph->addresslist_head))
648 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
649 ph->addresslist_tail,
653 while (NULL != (rc = ph->reservation_head))
655 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
656 ph->reservation_tail,
658 GNUNET_break (NULL == rc->rcb);
661 if (NULL != ph->task)
663 GNUNET_SCHEDULER_cancel (ph->task);
668 GNUNET_MQ_destroy (ph->mq);
676 * Reserve inbound bandwidth from the given peer. ATS will look at
677 * the current amount of traffic we receive from the peer and ensure
678 * that the peer could add @a amount of data to its stream.
680 * @param ph performance handle
681 * @param peer identifies the peer
682 * @param amount reserve N bytes for receiving, negative
683 * amounts can be used to undo a (recent) reservation;
684 * @param rcb function to call with the resulting reservation information
685 * @param rcb_cls closure for @a rcb
686 * @return NULL on error
687 * @deprecated will be replaced soon
689 struct GNUNET_ATS_ReservationContext *
690 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
691 const struct GNUNET_PeerIdentity *peer,
693 GNUNET_ATS_ReservationCallback rcb,
696 struct GNUNET_ATS_ReservationContext *rc;
697 struct GNUNET_MQ_Envelope *env;
698 struct ReservationRequestMessage *m;
702 rc = GNUNET_new (struct GNUNET_ATS_ReservationContext);
706 rc->rcb_cls = rcb_cls;
707 if ( (NULL != rcb) &&
709 rc->undo = GNUNET_YES;
710 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
711 ph->reservation_tail,
713 env = GNUNET_MQ_msg (m,
714 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
715 m->amount = htonl (amount);
717 GNUNET_MQ_send (ph->mq,
724 * Cancel request for reserving bandwidth.
726 * @param rc context returned by the original #GNUNET_ATS_reserve_bandwidth() call
729 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
736 * Get information about addresses known to the ATS subsystem.
738 * @param ph the performance handle to use
739 * @param peer peer idm can be NULL for all peers
740 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
741 * get only address currently used
742 * @param infocb callback to call with the addresses,
743 * will callback with address == NULL when done
744 * @param infocb_cls closure for @a infocb
745 * @return ats performance context
747 struct GNUNET_ATS_AddressListHandle*
748 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
749 const struct GNUNET_PeerIdentity *peer,
751 GNUNET_ATS_AddressInformationCallback infocb,
754 struct GNUNET_ATS_AddressListHandle *alh;
755 struct GNUNET_MQ_Envelope *env;
756 struct AddressListRequestMessage *m;
765 alh = GNUNET_new (struct GNUNET_ATS_AddressListHandle);
768 alh->cb_cls = infocb_cls;
770 alh->all_addresses = all;
773 alh->all_peers = GNUNET_YES;
777 alh->all_peers = GNUNET_NO;
780 GNUNET_CONTAINER_DLL_insert (ph->addresslist_head,
781 ph->addresslist_tail,
783 env = GNUNET_MQ_msg (m,
784 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
785 m->all = htonl (all);
786 m->id = htonl (alh->id);
789 GNUNET_MQ_send (ph->mq,
796 * Cancel a pending address listing operation
798 * @param alh the handle of the request to cancel
801 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh)
803 struct GNUNET_ATS_PerformanceHandle *ph = alh->ph;
805 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
806 ph->addresslist_tail,
813 * Convert a `enum GNUNET_ATS_PreferenceType` to a string
815 * @param type the preference type
816 * @return a string or NULL if invalid
819 GNUNET_ATS_print_preference_type (uint32_t type)
821 const char *prefs[] = GNUNET_ATS_PreferenceTypeString;
823 if (type < GNUNET_ATS_PREFERENCE_END)
830 * Change preferences for the given peer. Preference changes are forgotten if peers
833 * @param ph performance handle
834 * @param peer identifies the peer
835 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
838 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
839 const struct GNUNET_PeerIdentity *peer,
842 struct GNUNET_MQ_Envelope *env;
843 struct ChangePreferenceMessage *m;
845 struct PreferenceInformation *pi;
847 enum GNUNET_ATS_PreferenceKind kind;
853 while (GNUNET_ATS_PREFERENCE_END !=
854 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
858 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
860 (void) va_arg (ap, double);
862 case GNUNET_ATS_PREFERENCE_LATENCY:
864 (void) va_arg (ap, double);
871 env = GNUNET_MQ_msg_extra (m,
872 count * sizeof(struct PreferenceInformation),
873 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
874 m->num_preferences = htonl (count);
876 pi = (struct PreferenceInformation *) &m[1];
879 while (GNUNET_ATS_PREFERENCE_END != (kind =
880 va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
882 pi[count].preference_kind = htonl (kind);
885 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
886 pi[count].preference_value = (float) va_arg (ap, double);
890 case GNUNET_ATS_PREFERENCE_LATENCY:
891 pi[count].preference_value = (float) va_arg (ap, double);
900 GNUNET_MQ_send (ph->mq,
906 * Send feedback to ATS on how good a the requirements for a peer and a
907 * preference is satisfied by ATS
909 * @param ph performance handle
910 * @param scope the time interval this valid for: [now - scope .. now]
911 * @param peer identifies the peer
912 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
915 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
916 const struct GNUNET_PeerIdentity *peer,
917 const struct GNUNET_TIME_Relative scope,
920 struct GNUNET_MQ_Envelope *env;
921 struct FeedbackPreferenceMessage *m;
923 struct PreferenceInformation *pi;
925 enum GNUNET_ATS_PreferenceKind kind;
931 while (GNUNET_ATS_PREFERENCE_END !=
932 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
936 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
938 (void) va_arg (ap, double);
940 case GNUNET_ATS_PREFERENCE_LATENCY:
942 (void) va_arg (ap, double);
949 env = GNUNET_MQ_msg_extra (m,
950 count * sizeof(struct PreferenceInformation),
951 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK);
952 m->scope = GNUNET_TIME_relative_hton (scope);
953 m->num_feedback = htonl (count);
955 pi = (struct PreferenceInformation *) &m[1];
958 while (GNUNET_ATS_PREFERENCE_END != (kind =
959 va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
961 pi[count].preference_kind = htonl (kind);
964 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
965 pi[count].preference_value = (float) va_arg (ap, double);
969 case GNUNET_ATS_PREFERENCE_LATENCY:
970 pi[count].preference_value = (float) va_arg (ap, double);
979 GNUNET_MQ_send (ph->mq,
983 /* end of ats_api_performance.c */