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
156 * ATS Handle to obtain and/or modify performance information.
158 struct GNUNET_ATS_PerformanceHandle
164 const struct GNUNET_CONFIGURATION_Handle *cfg;
167 * Callback to invoke on performance changes.
169 GNUNET_ATS_PeerInformationCallback infocb;
172 * Closure for 'infocb'.
177 * Connection to ATS service.
179 struct GNUNET_CLIENT_Connection *client;
182 * Head of list of messages for the ATS service.
184 struct PendingMessage *pending_head;
187 * Tail of list of messages for the ATS service
189 struct PendingMessage *pending_tail;
192 * Head of linked list of pending reservation requests.
194 struct GNUNET_ATS_ReservationContext *reservation_head;
197 * Tail of linked list of pending reservation requests.
199 struct GNUNET_ATS_ReservationContext *reservation_tail;
202 * Head of linked list of pending address list requests.
204 struct GNUNET_ATS_AddressListHandle *addresslist_head;
207 * Tail of linked list of pending address list requests.
209 struct GNUNET_ATS_AddressListHandle *addresslist_tail;
212 * Current request for transmission to ATS.
214 struct GNUNET_CLIENT_TransmitHandle *th;
217 * Task to trigger reconnect.
219 GNUNET_SCHEDULER_TaskIdentifier task;
225 * Re-establish the connection to the ATS service.
227 * @param ph handle to use to re-connect.
230 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
234 * Re-establish the connection to the ATS service.
236 * @param cls handle to use to re-connect.
237 * @param tc scheduler context
240 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 struct GNUNET_ATS_PerformanceHandle *ph = cls;
244 ph->task = GNUNET_SCHEDULER_NO_TASK;
250 * Transmit messages from the message queue to the service
251 * (if there are any, and if we are not already trying).
253 * @param ph handle to use
256 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
260 * We can now transmit a message to ATS. Do it.
262 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
263 * @param size number of bytes we can transmit to ATS
264 * @param buf where to copy the messages
265 * @return number of bytes copied into buf
268 transmit_message_to_ats (void *cls, size_t size, void *buf)
270 struct GNUNET_ATS_PerformanceHandle *ph = cls;
271 struct PendingMessage *p;
278 while ((NULL != (p = ph->pending_head)) && (p->size <= size))
280 memcpy (&cbuf[ret], &p[1], p->size);
283 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
292 * Transmit messages from the message queue to the service
293 * (if there are any, and if we are not already trying).
295 * @param ph handle to use
298 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
300 struct PendingMessage *p;
304 if (NULL == (p = ph->pending_head))
306 if (NULL == ph->client)
307 return; /* currently reconnecting */
309 GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
310 GNUNET_TIME_UNIT_FOREVER_REL,
311 GNUNET_YES, &transmit_message_to_ats,
317 * We received a peer information message. Validate and process it.
319 * @param ph our context with the callback
320 * @param msg the message
321 * @return GNUNET_OK if the message was well-formed
324 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
325 const struct GNUNET_MessageHeader *msg)
327 const struct PeerInformationMessage *pi;
328 const struct GNUNET_ATS_Information *atsi;
329 const char *plugin_address;
330 const char *plugin_name;
331 struct GNUNET_HELLO_Address address;
332 uint16_t plugin_address_length;
333 uint16_t plugin_name_length;
336 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
339 return GNUNET_SYSERR;
341 pi = (const struct PeerInformationMessage *) msg;
342 ats_count = ntohl (pi->ats_count);
343 plugin_address_length = ntohs (pi->address_length);
344 plugin_name_length = ntohs (pi->plugin_name_length);
345 atsi = (const struct GNUNET_ATS_Information *) &pi[1];
346 plugin_address = (const char *) &atsi[ats_count];
347 plugin_name = &plugin_address[plugin_address_length];
348 if ((plugin_address_length + plugin_name_length +
349 ats_count * sizeof (struct GNUNET_ATS_Information) +
350 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
352 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
353 || (plugin_name[plugin_name_length - 1] != '\0'))
356 return GNUNET_SYSERR;
358 if (ph->infocb == NULL)
362 address.peer = pi->peer;
363 address.address = plugin_address;
364 address.address_length = plugin_address_length;
365 address.transport_name = plugin_name;
366 ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
373 * We received a reservation result message. Validate and process it.
375 * @param ph our context with the callback
376 * @param msg the message
377 * @return GNUNET_OK if the message was well-formed
380 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
381 const struct GNUNET_MessageHeader *msg)
383 const struct ReservationResultMessage *rr;
384 struct GNUNET_ATS_ReservationContext *rc;
387 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
390 return GNUNET_SYSERR;
392 rr = (const struct ReservationResultMessage *) msg;
393 amount = ntohl (rr->amount);
394 rc = ph->reservation_head;
395 if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
398 return GNUNET_SYSERR;
400 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
401 if ((amount == 0) || (rc->rcb != NULL))
403 /* tell client if not cancelled */
405 rc->rcb (rc->rcb_cls, &rr->peer, amount,
406 GNUNET_TIME_relative_ntoh (rr->res_delay));
410 /* amount non-zero, but client cancelled, consider undo! */
411 if (GNUNET_YES != rc->undo)
414 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
417 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
423 * We received a reservation result message. Validate and process it.
425 * @param ph our context with the callback
426 * @param msg the message
427 * @return GNUNET_OK if the message was well-formed
430 process_ar_message (struct GNUNET_ATS_PerformanceHandle *ph,
431 const struct GNUNET_MessageHeader *msg)
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "TO BE IMPLEMENTED\n");
436 const struct ReservationResultMessage *rr;
437 struct GNUNET_ATS_ReservationContext *rc;
440 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
443 return GNUNET_SYSERR;
445 rr = (const struct ReservationResultMessage *) msg;
446 amount = ntohl (rr->amount);
447 rc = ph->reservation_head;
448 if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
451 return GNUNET_SYSERR;
453 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
454 if ((amount == 0) || (rc->rcb != NULL))
456 /* tell client if not cancelled */
458 rc->rcb (rc->rcb_cls, &rr->peer, amount,
459 GNUNET_TIME_relative_ntoh (rr->res_delay));
463 /* amount non-zero, but client cancelled, consider undo! */
464 if (GNUNET_YES != rc->undo)
467 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
470 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
477 * Type of a function to call when we receive a message
480 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
481 * @param msg message received, NULL on timeout or fatal error
484 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
486 struct GNUNET_ATS_PerformanceHandle *ph = cls;
490 switch (ntohs (msg->type))
492 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
493 if (GNUNET_OK != process_pi_message (ph, msg))
496 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
497 if (GNUNET_OK != process_rr_message (ph, msg))
500 case GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE:
501 if (GNUNET_OK != process_ar_message (ph, msg))
508 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
509 GNUNET_TIME_UNIT_FOREVER_REL);
514 GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
517 GNUNET_CLIENT_disconnect (ph->client);
520 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
526 * Re-establish the connection to the ATS service.
528 * @param ph handle to use to re-connect.
531 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
533 struct PendingMessage *p;
534 struct ClientStartMessage *init;
536 GNUNET_assert (NULL == ph->client);
537 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
538 GNUNET_assert (NULL != ph->client);
539 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
540 GNUNET_TIME_UNIT_FOREVER_REL);
541 if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
543 p = GNUNET_malloc (sizeof (struct PendingMessage) +
544 sizeof (struct ClientStartMessage));
545 p->size = sizeof (struct ClientStartMessage);
546 p->is_init = GNUNET_YES;
547 init = (struct ClientStartMessage *) &p[1];
548 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
549 init->header.size = htons (sizeof (struct ClientStartMessage));
551 htonl ((ph->infocb ==
552 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
553 START_FLAG_PERFORMANCE_WITH_PIC);
554 GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
562 * Get handle to access performance API of the ATS subsystem.
564 * @param cfg configuration to use
565 * @param infocb function to call on allocation changes, can be NULL
566 * @param infocb_cls closure for infocb
567 * @return ats performance context
569 struct GNUNET_ATS_PerformanceHandle *
570 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
571 GNUNET_ATS_PeerInformationCallback infocb,
574 struct GNUNET_ATS_PerformanceHandle *ph;
576 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
579 ph->infocb_cls = infocb_cls;
586 * Client is done using the ATS performance subsystem, release resources.
591 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
593 struct PendingMessage *p;
594 struct GNUNET_ATS_ReservationContext *rc;
595 struct GNUNET_ATS_AddressListHandle *alh;
597 while (NULL != (p = ph->pending_head))
599 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
602 while (NULL != (alh = ph->addresslist_head))
604 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail,
608 while (NULL != (rc = ph->reservation_head))
610 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
612 GNUNET_break (NULL == rc->rcb);
615 if (GNUNET_SCHEDULER_NO_TASK != ph->task)
617 GNUNET_SCHEDULER_cancel (ph->task);
618 ph->task = GNUNET_SCHEDULER_NO_TASK;
620 if (NULL != ph->client)
622 GNUNET_CLIENT_disconnect (ph->client);
630 * Reserve inbound bandwidth from the given peer. ATS will look at
631 * the current amount of traffic we receive from the peer and ensure
632 * that the peer could add 'amount' of data to its stream.
634 * @param ph performance handle
635 * @param peer identifies the peer
636 * @param amount reserve N bytes for receiving, negative
637 * amounts can be used to undo a (recent) reservation;
638 * @param rcb function to call with the resulting reservation information
639 * @param rcb_cls closure for info
640 * @return NULL on error
641 * @deprecated will be replaced soon
643 struct GNUNET_ATS_ReservationContext *
644 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
645 const struct GNUNET_PeerIdentity *peer,
647 GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
649 struct GNUNET_ATS_ReservationContext *rc;
650 struct PendingMessage *p;
651 struct ReservationRequestMessage *m;
653 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
657 rc->rcb_cls = rcb_cls;
658 if ((rcb != NULL) && (amount > 0))
659 rc->undo = GNUNET_YES;
660 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
663 p = GNUNET_malloc (sizeof (struct PendingMessage) +
664 sizeof (struct ReservationRequestMessage));
665 p->size = sizeof (struct ReservationRequestMessage);
666 p->is_init = GNUNET_NO;
667 m = (struct ReservationRequestMessage *) &p[1];
668 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
669 m->header.size = htons (sizeof (struct ReservationRequestMessage));
670 m->amount = htonl (amount);
672 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
679 * Cancel request for reserving bandwidth.
681 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
684 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
690 * Get information about addresses known to the ATS subsystem.
692 * @param cfg configuration to use
693 * @param peer peer idm can be NULL for all peers
694 * @param all GNUNET_YES to get information about all addresses or GNUNET_NO to
695 * get only address currently used
696 * @param infocb callback to call with the addresses,
697 * will callback with address == NULL when done
698 * @param infocb_cls closure for infocb
699 * @return ats performance context
701 struct GNUNET_ATS_AddressListHandle*
702 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
703 const struct GNUNET_PeerIdentity *peer,
705 GNUNET_ATS_PeerInformationCallback infocb,
708 struct GNUNET_ATS_AddressListHandle *alh;
709 struct PendingMessage *p;
710 struct AddressListRequestMessage *m;
712 GNUNET_assert (NULL != handle);
714 alh = GNUNET_malloc (sizeof (struct GNUNET_ATS_AddressListHandle));
716 alh->cb_cls = infocb_cls;
718 alh->all_addresses = all;
720 alh->all_peers = GNUNET_YES;
723 alh->all_peers = GNUNET_NO;
727 GNUNET_CONTAINER_DLL_insert (handle->addresslist_head, handle->addresslist_tail, alh);
729 p = GNUNET_malloc (sizeof (struct PendingMessage) +
730 sizeof (struct AddressListRequestMessage));
731 p->size = sizeof (struct AddressListRequestMessage);
732 m = (struct AddressListRequestMessage *) &p[1];
733 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST);
734 m->header.size = htons (sizeof (struct AddressListRequestMessage));
735 m->all = htonl (all);
740 memset (&m->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
742 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail, p);
743 do_transmit (handle);
750 * Cancel a pending address listing operation
752 * @param handle the GNUNET_ATS_AddressListHandle handle to cancel
755 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle)
757 GNUNET_assert (NULL != handle);
759 GNUNET_CONTAINER_DLL_remove (handle->ph->addresslist_head, handle->ph->addresslist_tail, handle);
760 GNUNET_free (handle);
765 * Change preferences for the given peer. Preference changes are forgotten if peers
768 * @param ph performance handle
769 * @param peer identifies the peer
770 * @param ... 0-terminated specification of the desired changes
773 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
774 const struct GNUNET_PeerIdentity *peer, ...)
776 struct PendingMessage *p;
777 struct ChangePreferenceMessage *m;
780 struct PreferenceInformation *pi;
782 enum GNUNET_ATS_PreferenceKind kind;
786 while (GNUNET_ATS_PREFERENCE_END !=
787 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
791 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
793 (void) va_arg (ap, double);
796 case GNUNET_ATS_PREFERENCE_LATENCY:
798 (void) va_arg (ap, double);
807 count * sizeof (struct PreferenceInformation) +
808 sizeof (struct ChangePreferenceMessage);
809 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
811 p->is_init = GNUNET_NO;
812 m = (struct ChangePreferenceMessage *) &p[1];
813 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
814 m->header.size = htons (msize);
815 m->num_preferences = htonl (count);
817 pi = (struct PreferenceInformation *) &m[1];
820 while (GNUNET_ATS_PREFERENCE_END !=
821 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
823 pi[count].preference_kind = htonl (kind);
826 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
827 pi[count].preference_value = (float) va_arg (ap, double);
831 case GNUNET_ATS_PREFERENCE_LATENCY:
832 pi[count].preference_value = (float) va_arg (ap, double);
841 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
845 /* end of ats_api_performance.c */