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 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.
16 * @file ats/ats_api_performance.c
17 * @brief automatic transport selection and outbound bandwidth determination
18 * @author Christian Grothoff
19 * @author Matthias Wachs
22 #include "gnunet_ats_service.h"
26 #define LOG(kind,...) GNUNET_log_from(kind, "ats-performance-api", __VA_ARGS__)
30 * Linked list of pending reservations.
32 struct GNUNET_ATS_ReservationContext
38 struct GNUNET_ATS_ReservationContext *next;
43 struct GNUNET_ATS_ReservationContext *prev;
48 struct GNUNET_PeerIdentity peer;
56 * Function to call on result.
58 GNUNET_ATS_ReservationCallback rcb;
66 * Do we need to undo this reservation if it succeeded? Set to
67 * #GNUNET_YES if a reservation is cancelled. (at that point, 'info'
68 * is also set to NULL; however, info will ALSO be NULL for the
69 * reservation context that is created to undo the original request,
70 * so 'info' being NULL cannot be used to check if undo is
78 * Linked list of pending reservations.
80 struct GNUNET_ATS_AddressListHandle
86 struct GNUNET_ATS_AddressListHandle *next;
91 struct GNUNET_ATS_AddressListHandle *prev;
96 struct GNUNET_ATS_PerformanceHandle *ph;
101 GNUNET_ATS_AddressInformationCallback cb;
104 * Callback closure for @e cb
111 struct GNUNET_PeerIdentity peer;
114 * Return all or specific peer only
119 * Return all or used address only
124 * Request multiplexing
131 * ATS Handle to obtain and/or modify performance information.
133 struct GNUNET_ATS_PerformanceHandle
139 const struct GNUNET_CONFIGURATION_Handle *cfg;
142 * Callback to invoke when an address has performance changes.
144 GNUNET_ATS_AddressInformationCallback addr_info_cb;
147 * Closure for @e addr_info_cb.
149 void *addr_info_cb_cls;
152 * Connection to ATS service.
154 struct GNUNET_MQ_Handle *mq;
157 * Head of linked list of pending reservation requests.
159 struct GNUNET_ATS_ReservationContext *reservation_head;
162 * Tail of linked list of pending reservation requests.
164 struct GNUNET_ATS_ReservationContext *reservation_tail;
167 * Head of linked list of pending address list requests.
169 struct GNUNET_ATS_AddressListHandle *addresslist_head;
172 * Tail of linked list of pending address list requests.
174 struct GNUNET_ATS_AddressListHandle *addresslist_tail;
177 * Current request for transmission to ATS.
179 struct GNUNET_CLIENT_TransmitHandle *th;
182 * Task to trigger reconnect.
184 struct GNUNET_SCHEDULER_Task *task;
187 * Reconnect backoff delay.
189 struct GNUNET_TIME_Relative backoff;
192 * Monitor request multiplexing
197 * Request multiplexing
202 * Is the receive loop active?
208 * Re-establish the connection to the ATS service.
210 * @param ph handle to use to re-connect.
213 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
217 * Re-establish the connection to the ATS service.
219 * @param cls handle to use to re-connect.
222 reconnect_task (void *cls)
224 struct GNUNET_ATS_PerformanceHandle *ph = cls;
232 * Reconnect to the ATS service, something went wrong.
234 * @param ph handle to reconnect
237 do_reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
239 struct GNUNET_ATS_ReservationContext *rc;
240 struct GNUNET_ATS_AddressListHandle *alh;
241 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
245 GNUNET_MQ_destroy (ph->mq);
248 while (NULL != (rc = ph->reservation_head))
250 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
251 ph->reservation_tail,
254 rc->rcb (rc->rcb_cls,
257 GNUNET_TIME_UNIT_FOREVER_REL);
260 bandwidth_zero.value__ = htonl (0);
261 while (NULL != (alh = ph->addresslist_head))
263 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
264 ph->addresslist_tail,
267 alh->cb (alh->cb_cls,
275 if (NULL != ph->addr_info_cb)
277 /* Indicate reconnect */
278 ph->addr_info_cb (ph->addr_info_cb_cls,
285 ph->backoff = GNUNET_TIME_STD_BACKOFF (ph->backoff);
286 ph->task = GNUNET_SCHEDULER_add_delayed (ph->backoff,
293 * We received a peer information message. Validate and process it.
295 * @param cls our context with the callback
296 * @param pi the message
297 * @return #GNUNET_OK if the message was well-formed
300 check_peer_information (void *cls,
301 const struct PeerInformationMessage *pi)
303 const char *plugin_address;
304 const char *plugin_name;
305 uint16_t plugin_address_length;
306 uint16_t plugin_name_length;
308 plugin_address_length = ntohs (pi->address_length);
309 plugin_name_length = ntohs (pi->plugin_name_length);
310 plugin_address = (const char *) &pi[1];
311 plugin_name = &plugin_address[plugin_address_length];
312 if ( (plugin_address_length + plugin_name_length
313 + sizeof(struct PeerInformationMessage) != ntohs (pi->header.size)) ||
314 (plugin_name[plugin_name_length - 1] != '\0'))
317 return GNUNET_SYSERR;
324 * We received a peer information message. Validate and process it.
326 * @param cls our context with the callback
327 * @param pi the message
328 * @return #GNUNET_OK if the message was well-formed
331 handle_peer_information (void *cls,
332 const struct PeerInformationMessage *pi)
334 struct GNUNET_ATS_PerformanceHandle *ph = cls;
335 const char *plugin_address;
336 const char *plugin_name;
337 struct GNUNET_HELLO_Address address;
338 uint16_t plugin_address_length;
340 struct GNUNET_ATS_Properties prop;
342 if (NULL == ph->addr_info_cb)
344 plugin_address_length = ntohs (pi->address_length);
345 addr_active = (int) ntohl (pi->address_active);
346 plugin_address = (const char *) &pi[1];
347 plugin_name = &plugin_address[plugin_address_length];
349 GNUNET_ATS_properties_ntoh (&prop,
351 address.peer = pi->peer;
352 address.local_info = (enum GNUNET_HELLO_AddressInfo) ntohl (pi->address_local_info);
353 address.address = plugin_address;
354 address.address_length = plugin_address_length;
355 address.transport_name = plugin_name;
356 ph->addr_info_cb (ph->addr_info_cb_cls,
366 * We received a reservation result message. Validate and process it.
368 * @param cls our context with the callback
369 * @param rr the message
372 handle_reservation_result (void *cls,
373 const struct ReservationResultMessage *rr)
375 struct GNUNET_ATS_PerformanceHandle *ph = cls;
376 struct GNUNET_ATS_ReservationContext *rc;
379 amount = ntohl (rr->amount);
380 rc = ph->reservation_head;
381 if (0 != memcmp (&rr->peer,
383 sizeof(struct GNUNET_PeerIdentity)))
389 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
390 ph->reservation_tail,
392 if ( (0 == amount) ||
395 /* tell client if not cancelled */
397 rc->rcb (rc->rcb_cls,
400 GNUNET_TIME_relative_ntoh (rr->res_delay));
404 /* amount non-zero, but client cancelled, consider undo! */
405 if (GNUNET_YES != rc->undo)
408 return; /* do not try to undo failed undos or negative amounts */
411 (void) GNUNET_ATS_reserve_bandwidth (ph,
419 * We received a PeerInformationMessage. Validate it.
421 * @param cls our context with the callback
422 * @param pi the message
423 * @return #GNUNET_OK if the message was well-formed
426 check_address_list (void *cls,
427 const struct PeerInformationMessage *pi)
429 const char *plugin_address;
430 const char *plugin_name;
431 uint16_t plugin_address_length;
432 uint16_t plugin_name_length;
434 plugin_address_length = ntohs (pi->address_length);
435 plugin_name_length = ntohs (pi->plugin_name_length);
436 plugin_address = (const char *) &pi[1];
437 plugin_name = &plugin_address[plugin_address_length];
438 if ( (plugin_address_length + plugin_name_length
439 + sizeof (struct PeerInformationMessage) != ntohs (pi->header.size)) ||
440 (plugin_name[plugin_name_length - 1] != '\0') )
443 return GNUNET_SYSERR;
450 * We received a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE.
453 * @param cls our context with the callback
454 * @param pi the message
457 handle_address_list (void *cls,
458 const struct PeerInformationMessage *pi)
460 struct GNUNET_ATS_PerformanceHandle *ph = cls;
461 struct GNUNET_ATS_AddressListHandle *alh;
462 struct GNUNET_ATS_AddressListHandle *next;
463 const char *plugin_address;
464 const char *plugin_name;
465 struct GNUNET_HELLO_Address address;
466 struct GNUNET_PeerIdentity allzeros;
467 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
468 struct GNUNET_ATS_Properties prop;
469 uint16_t plugin_address_length;
470 uint16_t plugin_name_length;
475 active = ntohl (pi->address_active);
476 plugin_address_length = ntohs (pi->address_length);
477 plugin_name_length = ntohs (pi->plugin_name_length);
478 plugin_address = (const char *) &pi[1];
479 plugin_name = &plugin_address[plugin_address_length];
480 LOG (GNUNET_ERROR_TYPE_DEBUG,
481 "Received ATS_ADDRESSLIST_RESPONSE message for peer %s and plugin %s\n",
482 GNUNET_i2s (&pi->peer),
485 next = ph->addresslist_head;
486 while (NULL != (alh = next))
493 return; /* was canceled */
495 memset (&allzeros, '\0', sizeof (allzeros));
496 if ( (0 == memcmp (&allzeros, &pi->peer, sizeof(allzeros))) &&
497 (0 == plugin_name_length) &&
498 (0 == plugin_address_length) )
501 LOG (GNUNET_ERROR_TYPE_DEBUG,
502 "Received last message for ATS_ADDRESSLIST_RESPONSE\n");
503 bandwidth_zero.value__ = htonl (0);
504 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
505 ph->addresslist_tail,
508 alh->cb (alh->cb_cls,
518 address.peer = pi->peer;
519 address.address = plugin_address;
520 address.address_length = plugin_address_length;
521 address.transport_name = plugin_name;
522 if ( ( (GNUNET_YES == alh->all_addresses) ||
523 (GNUNET_YES == active) ) &&
526 GNUNET_ATS_properties_ntoh (&prop,
528 alh->cb (alh->cb_cls,
539 * Generic error handler, called with the appropriate error code and
540 * the same closure specified at the creation of the message queue.
541 * Not every message queue implementation supports an error handler.
543 * @param cls closure with the `struct GNUNET_ATS_PerformanceHandle *`
544 * @param error error code
547 mq_error_handler (void *cls,
548 enum GNUNET_MQ_Error error)
550 struct GNUNET_ATS_PerformanceHandle *ph = cls;
557 * Re-establish the connection to the ATS service.
559 * @param ph handle to use to re-connect.
562 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
564 struct GNUNET_MQ_MessageHandler handlers[] = {
565 GNUNET_MQ_hd_var_size (peer_information,
566 GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION,
567 struct PeerInformationMessage,
569 GNUNET_MQ_hd_fixed_size (reservation_result,
570 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT,
571 struct ReservationResultMessage,
573 GNUNET_MQ_hd_var_size (address_list,
574 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE,
575 struct PeerInformationMessage,
577 GNUNET_MQ_handler_end ()
579 struct GNUNET_MQ_Envelope *env;
580 struct ClientStartMessage *init;
582 GNUNET_assert (NULL == ph->mq);
583 ph->mq = GNUNET_CLIENT_connect (ph->cfg,
590 env = GNUNET_MQ_msg (init,
591 GNUNET_MESSAGE_TYPE_ATS_START);
592 init->start_flag = htonl ( (NULL == ph->addr_info_cb)
593 ? START_FLAG_PERFORMANCE_NO_PIC
594 : START_FLAG_PERFORMANCE_WITH_PIC);
595 GNUNET_MQ_send (ph->mq,
601 * Get handle to access performance API of the ATS subsystem.
603 * @param cfg configuration to use
604 * @param addr_info_cb callback called when performance characteristics for
606 * @param addr_info_cb_cls closure for @a addr_info_cb
607 * @return ats performance context
609 struct GNUNET_ATS_PerformanceHandle *
610 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
611 GNUNET_ATS_AddressInformationCallback addr_info_cb,
612 void *addr_info_cb_cls)
614 struct GNUNET_ATS_PerformanceHandle *ph;
616 ph = GNUNET_new (struct GNUNET_ATS_PerformanceHandle);
618 ph->addr_info_cb = addr_info_cb;
619 ph->addr_info_cb_cls = addr_info_cb_cls;
631 * Client is done using the ATS performance subsystem, release resources.
636 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
638 struct GNUNET_ATS_ReservationContext *rc;
639 struct GNUNET_ATS_AddressListHandle *alh;
641 while (NULL != (alh = ph->addresslist_head))
643 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
644 ph->addresslist_tail,
648 while (NULL != (rc = ph->reservation_head))
650 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
651 ph->reservation_tail,
653 GNUNET_break (NULL == rc->rcb);
656 if (NULL != ph->task)
658 GNUNET_SCHEDULER_cancel (ph->task);
663 GNUNET_MQ_destroy (ph->mq);
671 * Reserve inbound bandwidth from the given peer. ATS will look at
672 * the current amount of traffic we receive from the peer and ensure
673 * that the peer could add @a amount of data to its stream.
675 * @param ph performance handle
676 * @param peer identifies the peer
677 * @param amount reserve N bytes for receiving, negative
678 * amounts can be used to undo a (recent) reservation;
679 * @param rcb function to call with the resulting reservation information
680 * @param rcb_cls closure for @a rcb
681 * @return NULL on error
682 * @deprecated will be replaced soon
684 struct GNUNET_ATS_ReservationContext *
685 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
686 const struct GNUNET_PeerIdentity *peer,
688 GNUNET_ATS_ReservationCallback rcb,
691 struct GNUNET_ATS_ReservationContext *rc;
692 struct GNUNET_MQ_Envelope *env;
693 struct ReservationRequestMessage *m;
697 rc = GNUNET_new (struct GNUNET_ATS_ReservationContext);
701 rc->rcb_cls = rcb_cls;
702 if ( (NULL != rcb) &&
704 rc->undo = GNUNET_YES;
705 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
706 ph->reservation_tail,
708 env = GNUNET_MQ_msg (m,
709 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
710 m->amount = htonl (amount);
712 GNUNET_MQ_send (ph->mq,
719 * Cancel request for reserving bandwidth.
721 * @param rc context returned by the original #GNUNET_ATS_reserve_bandwidth() call
724 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
731 * Get information about addresses known to the ATS subsystem.
733 * @param ph the performance handle to use
734 * @param peer peer idm can be NULL for all peers
735 * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
736 * get only address currently used
737 * @param infocb callback to call with the addresses,
738 * will callback with address == NULL when done
739 * @param infocb_cls closure for @a infocb
740 * @return ats performance context
742 struct GNUNET_ATS_AddressListHandle*
743 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
744 const struct GNUNET_PeerIdentity *peer,
746 GNUNET_ATS_AddressInformationCallback infocb,
749 struct GNUNET_ATS_AddressListHandle *alh;
750 struct GNUNET_MQ_Envelope *env;
751 struct AddressListRequestMessage *m;
760 alh = GNUNET_new (struct GNUNET_ATS_AddressListHandle);
763 alh->cb_cls = infocb_cls;
765 alh->all_addresses = all;
768 alh->all_peers = GNUNET_YES;
772 alh->all_peers = GNUNET_NO;
775 GNUNET_CONTAINER_DLL_insert (ph->addresslist_head,
776 ph->addresslist_tail,
778 env = GNUNET_MQ_msg (m,
779 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
780 m->all = htonl (all);
781 m->id = htonl (alh->id);
784 GNUNET_MQ_send (ph->mq,
791 * Cancel a pending address listing operation
793 * @param alh the handle of the request to cancel
796 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh)
798 struct GNUNET_ATS_PerformanceHandle *ph = alh->ph;
800 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head,
801 ph->addresslist_tail,
808 * Convert a `enum GNUNET_ATS_PreferenceType` to a string
810 * @param type the preference type
811 * @return a string or NULL if invalid
814 GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type)
816 const char *prefs[] = GNUNET_ATS_PreferenceTypeString;
818 if (type < GNUNET_ATS_PREFERENCE_END)
825 * Change preferences for the given peer. Preference changes are forgotten if peers
828 * @param ph performance handle
829 * @param peer identifies the peer
830 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
833 GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
834 const struct GNUNET_PeerIdentity *peer,
837 struct GNUNET_MQ_Envelope *env;
838 struct ChangePreferenceMessage *m;
840 struct PreferenceInformation *pi;
842 enum GNUNET_ATS_PreferenceKind kind;
848 while (GNUNET_ATS_PREFERENCE_END !=
849 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
853 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
855 (void) va_arg (ap, double);
857 case GNUNET_ATS_PREFERENCE_LATENCY:
859 (void) va_arg (ap, double);
866 env = GNUNET_MQ_msg_extra (m,
867 count * sizeof(struct PreferenceInformation),
868 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
869 m->num_preferences = htonl (count);
871 pi = (struct PreferenceInformation *) &m[1];
874 while (GNUNET_ATS_PREFERENCE_END != (kind =
875 va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
877 pi[count].preference_kind = htonl (kind);
880 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
881 pi[count].preference_value = (float) va_arg (ap, double);
885 case GNUNET_ATS_PREFERENCE_LATENCY:
886 pi[count].preference_value = (float) va_arg (ap, double);
895 GNUNET_MQ_send (ph->mq,
901 * Send feedback to ATS on how good a the requirements for a peer and a
902 * preference is satisfied by ATS
904 * @param ph performance handle
905 * @param scope the time interval this valid for: [now - scope .. now]
906 * @param peer identifies the peer
907 * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
910 GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
911 const struct GNUNET_PeerIdentity *peer,
912 const struct GNUNET_TIME_Relative scope,
915 struct GNUNET_MQ_Envelope *env;
916 struct FeedbackPreferenceMessage *m;
918 struct PreferenceInformation *pi;
920 enum GNUNET_ATS_PreferenceKind kind;
926 while (GNUNET_ATS_PREFERENCE_END !=
927 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
931 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
933 (void) va_arg (ap, double);
935 case GNUNET_ATS_PREFERENCE_LATENCY:
937 (void) va_arg (ap, double);
944 env = GNUNET_MQ_msg_extra (m,
945 count * sizeof(struct PreferenceInformation),
946 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK);
947 m->scope = GNUNET_TIME_relative_hton (scope);
948 m->num_feedback = htonl (count);
950 pi = (struct PreferenceInformation *) &m[1];
953 while (GNUNET_ATS_PREFERENCE_END != (kind =
954 va_arg (ap, enum GNUNET_ATS_PreferenceKind) ))
956 pi[count].preference_kind = htonl (kind);
959 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
960 pi[count].preference_value = (float) va_arg (ap, double);
964 case GNUNET_ATS_PREFERENCE_LATENCY:
965 pi[count].preference_value = (float) va_arg (ap, double);
974 GNUNET_MQ_send (ph->mq,
978 /* end of ats_api_performance.c */