2 This file is part of GNUnet.
3 (C) 2010,2011 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.
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"
32 * Message in linked list we should send to the ATS service. The
33 * actual binary message follows this struct.
41 struct PendingMessage *next;
46 struct PendingMessage *prev;
49 * Size of the message.
54 * Is this the 'ATS_START' message?
61 * Linked list of pending reservations.
63 struct GNUNET_ATS_ReservationContext
69 struct GNUNET_ATS_ReservationContext *next;
74 struct GNUNET_ATS_ReservationContext *prev;
79 struct GNUNET_PeerIdentity peer;
87 * Function to call on result.
89 GNUNET_ATS_ReservationCallback rcb;
97 * Do we need to undo this reservation if it succeeded? Set to
98 * GNUNET_YES if a reservation is cancelled. (at that point, 'info'
99 * is also set to NULL; however, info will ALSO be NULL for the
100 * reservation context that is created to undo the original request,
101 * so 'info' being NULL cannot be used to check if undo is
109 * Linked list of pending reservations.
111 struct GNUNET_ATS_AddressListHandle
117 struct GNUNET_ATS_AddressListHandle *next;
122 struct GNUNET_ATS_AddressListHandle *prev;
127 struct GNUNET_ATS_PerformanceHandle *ph;
132 GNUNET_ATS_PeerInformationCallback cb;
142 struct GNUNET_PeerIdentity peer;
145 * Return all or specific peer only
150 * Return all or used address only
155 * Request multiplexing
161 * ATS Handle to obtain and/or modify performance information.
163 struct GNUNET_ATS_PerformanceHandle
169 const struct GNUNET_CONFIGURATION_Handle *cfg;
172 * Callback to invoke on performance changes.
174 GNUNET_ATS_PeerInformationCallback infocb;
177 * Closure for 'infocb'.
182 * Connection to ATS service.
184 struct GNUNET_CLIENT_Connection *client;
187 * Head of list of messages for the ATS service.
189 struct PendingMessage *pending_head;
192 * Tail of list of messages for the ATS service
194 struct PendingMessage *pending_tail;
197 * Head of linked list of pending reservation requests.
199 struct GNUNET_ATS_ReservationContext *reservation_head;
202 * Tail of linked list of pending reservation requests.
204 struct GNUNET_ATS_ReservationContext *reservation_tail;
207 * Head of linked list of pending address list requests.
209 struct GNUNET_ATS_AddressListHandle *addresslist_head;
212 * Tail of linked list of pending address list requests.
214 struct GNUNET_ATS_AddressListHandle *addresslist_tail;
217 * Current request for transmission to ATS.
219 struct GNUNET_CLIENT_TransmitHandle *th;
222 * Task to trigger reconnect.
224 GNUNET_SCHEDULER_TaskIdentifier task;
227 * Request multiplexing
234 * Re-establish the connection to the ATS service.
236 * @param ph handle to use to re-connect.
239 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
243 * Re-establish the connection to the ATS service.
245 * @param cls handle to use to re-connect.
246 * @param tc scheduler context
249 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 struct GNUNET_ATS_PerformanceHandle *ph = cls;
253 ph->task = GNUNET_SCHEDULER_NO_TASK;
259 * Transmit messages from the message queue to the service
260 * (if there are any, and if we are not already trying).
262 * @param ph handle to use
265 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
269 * We can now transmit a message to ATS. Do it.
271 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
272 * @param size number of bytes we can transmit to ATS
273 * @param buf where to copy the messages
274 * @return number of bytes copied into buf
277 transmit_message_to_ats (void *cls, size_t size, void *buf)
279 struct GNUNET_ATS_PerformanceHandle *ph = cls;
280 struct PendingMessage *p;
287 while ((NULL != (p = ph->pending_head)) && (p->size <= size))
289 memcpy (&cbuf[ret], &p[1], p->size);
292 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
301 * Transmit messages from the message queue to the service
302 * (if there are any, and if we are not already trying).
304 * @param ph handle to use
307 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
309 struct PendingMessage *p;
313 if (NULL == (p = ph->pending_head))
315 if (NULL == ph->client)
316 return; /* currently reconnecting */
318 GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
319 GNUNET_TIME_UNIT_FOREVER_REL,
320 GNUNET_YES, &transmit_message_to_ats,
326 * We received a peer information message. Validate and process it.
328 * @param ph our context with the callback
329 * @param msg the message
330 * @return GNUNET_OK if the message was well-formed
333 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
334 const struct GNUNET_MessageHeader *msg)
336 const struct PeerInformationMessage *pi;
337 const struct GNUNET_ATS_Information *atsi;
338 const char *plugin_address;
339 const char *plugin_name;
340 struct GNUNET_HELLO_Address address;
341 uint16_t plugin_address_length;
342 uint16_t plugin_name_length;
345 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
348 return GNUNET_SYSERR;
351 pi = (const struct PeerInformationMessage *) msg;
352 ats_count = ntohl (pi->ats_count);
353 plugin_address_length = ntohs (pi->address_length);
354 plugin_name_length = ntohs (pi->plugin_name_length);
355 atsi = (const struct GNUNET_ATS_Information *) &pi[1];
356 plugin_address = (const char *) &atsi[ats_count];
357 plugin_name = &plugin_address[plugin_address_length];
358 if ((plugin_address_length + plugin_name_length +
359 ats_count * sizeof (struct GNUNET_ATS_Information) +
360 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
362 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
363 || (plugin_name[plugin_name_length - 1] != '\0'))
366 return GNUNET_SYSERR;
368 if (ph->infocb == NULL)
373 address.peer = pi->peer;
374 address.address = plugin_address;
375 address.address_length = plugin_address_length;
376 address.transport_name = plugin_name;
377 ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
384 * We received a reservation result message. Validate and process it.
386 * @param ph our context with the callback
387 * @param msg the message
388 * @return GNUNET_OK if the message was well-formed
391 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
392 const struct GNUNET_MessageHeader *msg)
394 const struct ReservationResultMessage *rr;
395 struct GNUNET_ATS_ReservationContext *rc;
398 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
401 return GNUNET_SYSERR;
403 rr = (const struct ReservationResultMessage *) msg;
404 amount = ntohl (rr->amount);
405 rc = ph->reservation_head;
406 if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
409 return GNUNET_SYSERR;
411 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
412 if ((amount == 0) || (rc->rcb != NULL))
414 /* tell client if not cancelled */
416 rc->rcb (rc->rcb_cls, &rr->peer, amount,
417 GNUNET_TIME_relative_ntoh (rr->res_delay));
421 /* amount non-zero, but client cancelled, consider undo! */
422 if (GNUNET_YES != rc->undo)
425 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
428 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
434 * We received a reservation result message. Validate and process it.
436 * @param ph our context with the callback
437 * @param msg the message
438 * @return GNUNET_OK if the message was well-formed
441 process_ar_message (struct GNUNET_ATS_PerformanceHandle *ph,
442 const struct GNUNET_MessageHeader *msg)
444 const struct PeerInformationMessage *pi;
445 struct GNUNET_ATS_AddressListHandle *alh;
446 struct GNUNET_ATS_AddressListHandle *next;
447 const struct GNUNET_ATS_Information *atsi;
448 const char *plugin_address;
449 const char *plugin_name;
450 struct GNUNET_HELLO_Address address;
451 struct GNUNET_PeerIdentity allzeros;
452 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
453 uint16_t plugin_address_length;
454 uint16_t plugin_name_length;
459 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
462 return GNUNET_SYSERR;
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 _("Received %s message\n"), "ATS_ADDRESSLIST_RESPONSE");
467 pi = (const struct PeerInformationMessage *) msg;
469 ats_count = ntohl (pi->ats_count);
470 active = ntohl (pi->address_active);
471 plugin_address_length = ntohs (pi->address_length);
472 plugin_name_length = ntohs (pi->plugin_name_length);
473 atsi = (const struct GNUNET_ATS_Information *) &pi[1];
474 plugin_address = (const char *) &atsi[ats_count];
475 plugin_name = &plugin_address[plugin_address_length];
476 if ((plugin_address_length + plugin_name_length +
477 ats_count * sizeof (struct GNUNET_ATS_Information) +
478 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
480 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
481 || (plugin_name[plugin_name_length - 1] != '\0'))
484 return GNUNET_SYSERR;
487 next = ph->addresslist_head;
488 while (NULL != (alh = next))
497 return GNUNET_SYSERR;
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) &&
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
508 _("Received last message for %s \n"), "ATS_ADDRESSLIST_RESPONSE");
509 bandwidth_zero.value__ = htonl (0);
510 alh->cb (ph->infocb_cls,
512 bandwidth_zero, bandwidth_zero,
514 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail, alh);
519 address.peer = pi->peer;
520 address.address = plugin_address;
521 address.address_length = plugin_address_length;
522 address.transport_name = plugin_name;
524 if ((GNUNET_YES == alh->all_addresses) || (GNUNET_YES == active))
526 alh->cb (ph->infocb_cls,
528 pi->bandwidth_out, pi->bandwidth_in,
536 * Type of a function to call when we receive a message
539 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
540 * @param msg message received, NULL on timeout or fatal error
543 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
545 struct GNUNET_ATS_PerformanceHandle *ph = cls;
549 switch (ntohs (msg->type))
551 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
552 if (GNUNET_OK != process_pi_message (ph, msg))
555 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
556 if (GNUNET_OK != process_rr_message (ph, msg))
559 case GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE:
560 if (GNUNET_OK != process_ar_message (ph, msg))
567 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
568 GNUNET_TIME_UNIT_FOREVER_REL);
573 GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
576 GNUNET_CLIENT_disconnect (ph->client);
579 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
585 * Re-establish the connection to the ATS service.
587 * @param ph handle to use to re-connect.
590 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
592 struct PendingMessage *p;
593 struct ClientStartMessage *init;
595 GNUNET_assert (NULL == ph->client);
596 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
597 GNUNET_assert (NULL != ph->client);
598 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
599 GNUNET_TIME_UNIT_FOREVER_REL);
600 if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
602 p = GNUNET_malloc (sizeof (struct PendingMessage) +
603 sizeof (struct ClientStartMessage));
604 p->size = sizeof (struct ClientStartMessage);
605 p->is_init = GNUNET_YES;
606 init = (struct ClientStartMessage *) &p[1];
607 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
608 init->header.size = htons (sizeof (struct ClientStartMessage));
610 htonl ((ph->infocb ==
611 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
612 START_FLAG_PERFORMANCE_WITH_PIC);
613 GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
621 * Get handle to access performance API of the ATS subsystem.
623 * @param cfg configuration to use
624 * @param infocb function to call on allocation changes, can be NULL
625 * @param infocb_cls closure for infocb
626 * @return ats performance context
628 struct GNUNET_ATS_PerformanceHandle *
629 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
630 GNUNET_ATS_PeerInformationCallback infocb,
633 struct GNUNET_ATS_PerformanceHandle *ph;
635 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
638 ph->infocb_cls = infocb_cls;
646 * Client is done using the ATS performance subsystem, release resources.
651 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
653 struct PendingMessage *p;
654 struct GNUNET_ATS_ReservationContext *rc;
655 struct GNUNET_ATS_AddressListHandle *alh;
657 while (NULL != (p = ph->pending_head))
659 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
662 while (NULL != (alh = ph->addresslist_head))
664 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail,
668 while (NULL != (rc = ph->reservation_head))
670 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
672 GNUNET_break (NULL == rc->rcb);
675 if (GNUNET_SCHEDULER_NO_TASK != ph->task)
677 GNUNET_SCHEDULER_cancel (ph->task);
678 ph->task = GNUNET_SCHEDULER_NO_TASK;
680 if (NULL != ph->client)
682 GNUNET_CLIENT_disconnect (ph->client);
690 * Reserve inbound bandwidth from the given peer. ATS will look at
691 * the current amount of traffic we receive from the peer and ensure
692 * that the peer could add 'amount' of data to its stream.
694 * @param ph performance handle
695 * @param peer identifies the peer
696 * @param amount reserve N bytes for receiving, negative
697 * amounts can be used to undo a (recent) reservation;
698 * @param rcb function to call with the resulting reservation information
699 * @param rcb_cls closure for info
700 * @return NULL on error
701 * @deprecated will be replaced soon
703 struct GNUNET_ATS_ReservationContext *
704 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
705 const struct GNUNET_PeerIdentity *peer,
707 GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
709 struct GNUNET_ATS_ReservationContext *rc;
710 struct PendingMessage *p;
711 struct ReservationRequestMessage *m;
713 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
717 rc->rcb_cls = rcb_cls;
718 if ((rcb != NULL) && (amount > 0))
719 rc->undo = GNUNET_YES;
720 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
723 p = GNUNET_malloc (sizeof (struct PendingMessage) +
724 sizeof (struct ReservationRequestMessage));
725 p->size = sizeof (struct ReservationRequestMessage);
726 p->is_init = GNUNET_NO;
727 m = (struct ReservationRequestMessage *) &p[1];
728 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
729 m->header.size = htons (sizeof (struct ReservationRequestMessage));
730 m->amount = htonl (amount);
732 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
739 * Cancel request for reserving bandwidth.
741 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
744 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
750 * Get information about addresses known to the ATS subsystem.
752 * @param handle the performance handle to use
753 * @param peer peer idm can be NULL for all peers
754 * @param all GNUNET_YES to get information about all addresses or GNUNET_NO to
755 * get only address currently used
756 * @param infocb callback to call with the addresses,
757 * will callback with address == NULL when done
758 * @param infocb_cls closure for infocb
759 * @return ats performance context
761 struct GNUNET_ATS_AddressListHandle*
762 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
763 const struct GNUNET_PeerIdentity *peer,
765 GNUNET_ATS_PeerInformationCallback infocb,
768 struct GNUNET_ATS_AddressListHandle *alh;
769 struct PendingMessage *p;
770 struct AddressListRequestMessage *m;
772 GNUNET_assert (NULL != handle);
774 alh = GNUNET_malloc (sizeof (struct GNUNET_ATS_AddressListHandle));
775 alh->id = handle->id;
778 alh->cb_cls = infocb_cls;
780 alh->all_addresses = all;
782 alh->all_peers = GNUNET_YES;
785 alh->all_peers = GNUNET_NO;
789 GNUNET_CONTAINER_DLL_insert (handle->addresslist_head, handle->addresslist_tail, alh);
791 p = GNUNET_malloc (sizeof (struct PendingMessage) +
792 sizeof (struct AddressListRequestMessage));
793 p->size = sizeof (struct AddressListRequestMessage);
794 m = (struct AddressListRequestMessage *) &p[1];
795 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
796 m->header.size = htons (sizeof (struct AddressListRequestMessage));
797 m->all = htonl (all);
798 m->id = htonl (alh->id);
803 memset (&m->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
805 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail, p);
807 do_transmit (handle);
814 * Cancel a pending address listing operation
816 * @param handle the GNUNET_ATS_AddressListHandle handle to cancel
819 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle)
821 GNUNET_assert (NULL != handle);
823 GNUNET_CONTAINER_DLL_remove (handle->ph->addresslist_head, handle->ph->addresslist_tail, handle);
824 GNUNET_free (handle);
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 ... 0-terminated specification of the desired changes
837 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
838 const struct GNUNET_PeerIdentity *peer, ...)
840 struct PendingMessage *p;
841 struct ChangePreferenceMessage *m;
844 struct PreferenceInformation *pi;
846 enum GNUNET_ATS_PreferenceKind kind;
850 while (GNUNET_ATS_PREFERENCE_END !=
851 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
855 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
857 (void) va_arg (ap, double);
860 case GNUNET_ATS_PREFERENCE_LATENCY:
862 (void) va_arg (ap, double);
871 count * sizeof (struct PreferenceInformation) +
872 sizeof (struct ChangePreferenceMessage);
873 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
875 p->is_init = GNUNET_NO;
876 m = (struct ChangePreferenceMessage *) &p[1];
877 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
878 m->header.size = htons (msize);
879 m->num_preferences = htonl (count);
881 pi = (struct PreferenceInformation *) &m[1];
884 while (GNUNET_ATS_PREFERENCE_END !=
885 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
887 pi[count].preference_kind = htonl (kind);
890 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
891 pi[count].preference_value = (float) va_arg (ap, double);
895 case GNUNET_ATS_PREFERENCE_LATENCY:
896 pi[count].preference_value = (float) va_arg (ap, double);
905 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
909 /* end of ats_api_performance.c */