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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 != GNUNET_memcmp (&rr->peer,
393 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
394 ph->reservation_tail,
396 if ( (0 == amount) ||
399 /* tell client if not cancelled */
401 rc->rcb (rc->rcb_cls,
404 GNUNET_TIME_relative_ntoh (rr->res_delay));
408 /* amount non-zero, but client cancelled, consider undo! */
409 if (GNUNET_YES != rc->undo)
412 return; /* do not try to undo failed undos or negative amounts */
415 (void) GNUNET_ATS_reserve_bandwidth (ph,
423 * We received a PeerInformationMessage. Validate it.
425 * @param cls our context with the callback
426 * @param pi the message
427 * @return #GNUNET_OK if the message was well-formed
430 check_address_list (void *cls,
431 const struct PeerInformationMessage *pi)
433 const char *plugin_address;
434 const char *plugin_name;
435 uint16_t plugin_address_length;
436 uint16_t plugin_name_length;
438 plugin_address_length = ntohs (pi->address_length);
439 plugin_name_length = ntohs (pi->plugin_name_length);
440 plugin_address = (const char *) &pi[1];
441 plugin_name = &plugin_address[plugin_address_length];
442 if ( (plugin_address_length + plugin_name_length
443 + sizeof (struct PeerInformationMessage) != ntohs (pi->header.size)) ||
444 (plugin_name[plugin_name_length - 1] != '\0') )
447 return GNUNET_SYSERR;
454 * We received a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE.
457 * @param cls our context with the callback
458 * @param pi the message
461 handle_address_list (void *cls,
462 const struct PeerInformationMessage *pi)
464 struct GNUNET_ATS_PerformanceHandle *ph = cls;
465 struct GNUNET_ATS_AddressListHandle *alh;
466 struct GNUNET_ATS_AddressListHandle *next;
467 const char *plugin_address;
468 const char *plugin_name;
469 struct GNUNET_HELLO_Address address;
470 struct GNUNET_PeerIdentity allzeros;
471 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
472 struct GNUNET_ATS_Properties prop;
473 uint16_t plugin_address_length;
474 uint16_t plugin_name_length;
479 active = ntohl (pi->address_active);
480 plugin_address_length = ntohs (pi->address_length);
481 plugin_name_length = ntohs (pi->plugin_name_length);
482 plugin_address = (const char *) &pi[1];
483 plugin_name = &plugin_address[plugin_address_length];
484 LOG (GNUNET_ERROR_TYPE_DEBUG,
485 "Received ATS_ADDRESSLIST_RESPONSE message for peer %s and plugin %s\n",
486 GNUNET_i2s (&pi->peer),
489 next = ph->addresslist_head;
490 while (NULL != (alh = next))
497 return; /* was canceled */
499 memset (&allzeros, '\0', sizeof (allzeros));
500 if ( (0 == GNUNET_is_zero (&pi->peer)) &&
501 (0 == plugin_name_length) &&
502 (0 == plugin_address_length) )
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Received last message for ATS_ADDRESSLIST_RESPONSE\n");
507 bandwidth_zero.value__ = htonl (0);
508 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
509 ph->addresslist_tail,
512 alh->cb (alh->cb_cls,
522 address.peer = pi->peer;
523 address.address = plugin_address;
524 address.address_length = plugin_address_length;
525 address.transport_name = plugin_name;
526 if ( ( (GNUNET_YES == alh->all_addresses) ||
527 (GNUNET_YES == active) ) &&
530 GNUNET_ATS_properties_ntoh (&prop,
532 alh->cb (alh->cb_cls,
543 * Generic error handler, called with the appropriate error code and
544 * the same closure specified at the creation of the message queue.
545 * Not every message queue implementation supports an error handler.
547 * @param cls closure with the `struct GNUNET_ATS_PerformanceHandle *`
548 * @param error error code
551 mq_error_handler (void *cls,
552 enum GNUNET_MQ_Error error)
554 struct GNUNET_ATS_PerformanceHandle *ph = cls;
561 * Re-establish the connection to the ATS service.
563 * @param ph handle to use to re-connect.
566 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
568 struct GNUNET_MQ_MessageHandler handlers[] = {
569 GNUNET_MQ_hd_var_size (peer_information,
570 GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION,
571 struct PeerInformationMessage,
573 GNUNET_MQ_hd_fixed_size (reservation_result,
574 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT,
575 struct ReservationResultMessage,
577 GNUNET_MQ_hd_var_size (address_list,
578 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE,
579 struct PeerInformationMessage,
581 GNUNET_MQ_handler_end ()
583 struct GNUNET_MQ_Envelope *env;
584 struct ClientStartMessage *init;
586 GNUNET_assert (NULL == ph->mq);
587 ph->mq = GNUNET_CLIENT_connect (ph->cfg,
594 env = GNUNET_MQ_msg (init,
595 GNUNET_MESSAGE_TYPE_ATS_START);
596 init->start_flag = htonl ( (NULL == ph->addr_info_cb)
597 ? START_FLAG_PERFORMANCE_NO_PIC
598 : START_FLAG_PERFORMANCE_WITH_PIC);
599 GNUNET_MQ_send (ph->mq,
605 * Get handle to access performance API of the ATS subsystem.
607 * @param cfg configuration to use
608 * @param addr_info_cb callback called when performance characteristics for
610 * @param addr_info_cb_cls closure for @a addr_info_cb
611 * @return ats performance context
613 struct GNUNET_ATS_PerformanceHandle *
614 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
615 GNUNET_ATS_AddressInformationCallback addr_info_cb,
616 void *addr_info_cb_cls)
618 struct GNUNET_ATS_PerformanceHandle *ph;
620 ph = GNUNET_new (struct GNUNET_ATS_PerformanceHandle);
622 ph->addr_info_cb = addr_info_cb;
623 ph->addr_info_cb_cls = addr_info_cb_cls;
635 * Client is done using the ATS performance subsystem, release resources.
640 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
642 struct GNUNET_ATS_ReservationContext *rc;
643 struct GNUNET_ATS_AddressListHandle *alh;
645 while (NULL != (alh = ph->addresslist_head))
647 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
648 ph->addresslist_tail,
652 while (NULL != (rc = ph->reservation_head))
654 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
655 ph->reservation_tail,
657 GNUNET_break (NULL == rc->rcb);
660 if (NULL != ph->task)
662 GNUNET_SCHEDULER_cancel (ph->task);
667 GNUNET_MQ_destroy (ph->mq);
675 * Reserve inbound bandwidth from the given peer. ATS will look at
676 * the current amount of traffic we receive from the peer and ensure
677 * that the peer could add @a amount of data to its stream.
679 * @param ph performance handle
680 * @param peer identifies the peer
681 * @param amount reserve N bytes for receiving, negative
682 * amounts can be used to undo a (recent) reservation;
683 * @param rcb function to call with the resulting reservation information
684 * @param rcb_cls closure for @a rcb
685 * @return NULL on error
686 * @deprecated will be replaced soon
688 struct GNUNET_ATS_ReservationContext *
689 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
690 const struct GNUNET_PeerIdentity *peer,
692 GNUNET_ATS_ReservationCallback rcb,
695 struct GNUNET_ATS_ReservationContext *rc;
696 struct GNUNET_MQ_Envelope *env;
697 struct ReservationRequestMessage *m;
701 rc = GNUNET_new (struct GNUNET_ATS_ReservationContext);
705 rc->rcb_cls = rcb_cls;
706 if ( (NULL != rcb) &&
708 rc->undo = GNUNET_YES;
709 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
710 ph->reservation_tail,
712 env = GNUNET_MQ_msg (m,
713 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
714 m->amount = htonl (amount);
716 GNUNET_MQ_send (ph->mq,
723 * Cancel request for reserving bandwidth.
725 * @param rc context returned by the original #GNUNET_ATS_reserve_bandwidth() call
728 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
735 * Get information about addresses known to the ATS subsystem.
737 * @param ph the performance handle to use
738 * @param peer peer idm can be NULL for all peers
739 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
740 * get only address currently used
741 * @param infocb callback to call with the addresses,
742 * will callback with address == NULL when done
743 * @param infocb_cls closure for @a infocb
744 * @return ats performance context
746 struct GNUNET_ATS_AddressListHandle*
747 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
748 const struct GNUNET_PeerIdentity *peer,
750 GNUNET_ATS_AddressInformationCallback infocb,
753 struct GNUNET_ATS_AddressListHandle *alh;
754 struct GNUNET_MQ_Envelope *env;
755 struct AddressListRequestMessage *m;
764 alh = GNUNET_new (struct GNUNET_ATS_AddressListHandle);
767 alh->cb_cls = infocb_cls;
769 alh->all_addresses = all;
772 alh->all_peers = GNUNET_YES;
776 alh->all_peers = GNUNET_NO;
779 GNUNET_CONTAINER_DLL_insert (ph->addresslist_head,
780 ph->addresslist_tail,
782 env = GNUNET_MQ_msg (m,
783 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
784 m->all = htonl (all);
785 m->id = htonl (alh->id);
788 GNUNET_MQ_send (ph->mq,
795 * Cancel a pending address listing operation
797 * @param alh the handle of the request to cancel
800 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh)
802 struct GNUNET_ATS_PerformanceHandle *ph = alh->ph;
804 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
805 ph->addresslist_tail,
812 * Convert a `enum GNUNET_ATS_PreferenceType` to a string
814 * @param type the preference type
815 * @return a string or NULL if invalid
818 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type)
820 const char *prefs[] = GNUNET_ATS_PreferenceTypeString;
822 if (type < GNUNET_ATS_PREFERENCE_END)
829 * Change preferences for the given peer. Preference changes are forgotten if peers
832 * @param ph performance handle
833 * @param peer identifies the peer
834 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
837 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
838 const struct GNUNET_PeerIdentity *peer,
841 struct GNUNET_MQ_Envelope *env;
842 struct ChangePreferenceMessage *m;
844 struct PreferenceInformation *pi;
846 enum GNUNET_ATS_PreferenceKind kind;
852 while (GNUNET_ATS_PREFERENCE_END !=
853 (kind = GNUNET_VA_ARG_ENUM (ap, GNUNET_ATS_PreferenceKind) ))
857 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
859 (void) va_arg (ap, double);
861 case GNUNET_ATS_PREFERENCE_LATENCY:
863 (void) va_arg (ap, double);
870 env = GNUNET_MQ_msg_extra (m,
871 count * sizeof(struct PreferenceInformation),
872 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
873 m->num_preferences = htonl (count);
875 pi = (struct PreferenceInformation *) &m[1];
878 while (GNUNET_ATS_PREFERENCE_END != (kind =
879 GNUNET_VA_ARG_ENUM (ap, GNUNET_ATS_PreferenceKind) ))
881 pi[count].preference_kind = htonl (kind);
884 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
885 pi[count].preference_value = (float) va_arg (ap, double);
889 case GNUNET_ATS_PREFERENCE_LATENCY:
890 pi[count].preference_value = (float) va_arg (ap, double);
899 GNUNET_MQ_send (ph->mq,
905 * Send feedback to ATS on how good a the requirements for a peer and a
906 * preference is satisfied by ATS
908 * @param ph performance handle
909 * @param scope the time interval this valid for: [now - scope .. now]
910 * @param peer identifies the peer
911 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
914 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
915 const struct GNUNET_PeerIdentity *peer,
916 const struct GNUNET_TIME_Relative scope,
919 struct GNUNET_MQ_Envelope *env;
920 struct FeedbackPreferenceMessage *m;
922 struct PreferenceInformation *pi;
924 enum GNUNET_ATS_PreferenceKind kind;
930 while (GNUNET_ATS_PREFERENCE_END !=
931 (kind = GNUNET_VA_ARG_ENUM (ap, GNUNET_ATS_PreferenceKind) ))
935 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
937 (void) va_arg (ap, double);
939 case GNUNET_ATS_PREFERENCE_LATENCY:
941 (void) va_arg (ap, double);
948 env = GNUNET_MQ_msg_extra (m,
949 count * sizeof(struct PreferenceInformation),
950 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK);
951 m->scope = GNUNET_TIME_relative_hton (scope);
952 m->num_feedback = htonl (count);
954 pi = (struct PreferenceInformation *) &m[1];
957 while (GNUNET_ATS_PREFERENCE_END != (kind =
958 GNUNET_VA_ARG_ENUM (ap, GNUNET_ATS_PreferenceKind) ))
960 pi[count].preference_kind = htonl (kind);
963 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
964 pi[count].preference_value = (float) va_arg (ap, double);
968 case GNUNET_ATS_PREFERENCE_LATENCY:
969 pi[count].preference_value = (float) va_arg (ap, double);
978 GNUNET_MQ_send (ph->mq,
982 /* end of ats_api_performance.c */