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 * Type of a function to call when we receive a message
426 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
427 * @param msg message received, NULL on timeout or fatal error
430 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
432 struct GNUNET_ATS_PerformanceHandle *ph = cls;
436 switch (ntohs (msg->type))
438 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
439 if (GNUNET_OK != process_pi_message (ph, msg))
442 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
443 if (GNUNET_OK != process_rr_message (ph, msg))
450 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
451 GNUNET_TIME_UNIT_FOREVER_REL);
456 GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
459 GNUNET_CLIENT_disconnect (ph->client);
462 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
468 * Re-establish the connection to the ATS service.
470 * @param ph handle to use to re-connect.
473 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
475 struct PendingMessage *p;
476 struct ClientStartMessage *init;
478 GNUNET_assert (NULL == ph->client);
479 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
480 GNUNET_assert (NULL != ph->client);
481 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
482 GNUNET_TIME_UNIT_FOREVER_REL);
483 if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
485 p = GNUNET_malloc (sizeof (struct PendingMessage) +
486 sizeof (struct ClientStartMessage));
487 p->size = sizeof (struct ClientStartMessage);
488 p->is_init = GNUNET_YES;
489 init = (struct ClientStartMessage *) &p[1];
490 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
491 init->header.size = htons (sizeof (struct ClientStartMessage));
493 htonl ((ph->infocb ==
494 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
495 START_FLAG_PERFORMANCE_WITH_PIC);
496 GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
504 * Get handle to access performance API of the ATS subsystem.
506 * @param cfg configuration to use
507 * @param infocb function to call on allocation changes, can be NULL
508 * @param infocb_cls closure for infocb
509 * @return ats performance context
511 struct GNUNET_ATS_PerformanceHandle *
512 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
513 GNUNET_ATS_PeerInformationCallback infocb,
516 struct GNUNET_ATS_PerformanceHandle *ph;
518 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
521 ph->infocb_cls = infocb_cls;
528 * Client is done using the ATS performance subsystem, release resources.
533 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
535 struct PendingMessage *p;
536 struct GNUNET_ATS_ReservationContext *rc;
537 struct GNUNET_ATS_AddressListHandle *alh;
539 while (NULL != (p = ph->pending_head))
541 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
544 while (NULL != (alh = ph->addresslist_head))
546 GNUNET_CONTAINER_DLL_remove (ph->addresslist_head, ph->addresslist_tail,
550 while (NULL != (rc = ph->reservation_head))
552 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
554 GNUNET_break (NULL == rc->rcb);
557 if (GNUNET_SCHEDULER_NO_TASK != ph->task)
559 GNUNET_SCHEDULER_cancel (ph->task);
560 ph->task = GNUNET_SCHEDULER_NO_TASK;
562 if (NULL != ph->client)
564 GNUNET_CLIENT_disconnect (ph->client);
572 * Reserve inbound bandwidth from the given peer. ATS will look at
573 * the current amount of traffic we receive from the peer and ensure
574 * that the peer could add 'amount' of data to its stream.
576 * @param ph performance handle
577 * @param peer identifies the peer
578 * @param amount reserve N bytes for receiving, negative
579 * amounts can be used to undo a (recent) reservation;
580 * @param rcb function to call with the resulting reservation information
581 * @param rcb_cls closure for info
582 * @return NULL on error
583 * @deprecated will be replaced soon
585 struct GNUNET_ATS_ReservationContext *
586 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
587 const struct GNUNET_PeerIdentity *peer,
589 GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
591 struct GNUNET_ATS_ReservationContext *rc;
592 struct PendingMessage *p;
593 struct ReservationRequestMessage *m;
595 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
599 rc->rcb_cls = rcb_cls;
600 if ((rcb != NULL) && (amount > 0))
601 rc->undo = GNUNET_YES;
602 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
605 p = GNUNET_malloc (sizeof (struct PendingMessage) +
606 sizeof (struct ReservationRequestMessage));
607 p->size = sizeof (struct ReservationRequestMessage);
608 p->is_init = GNUNET_NO;
609 m = (struct ReservationRequestMessage *) &p[1];
610 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
611 m->header.size = htons (sizeof (struct ReservationRequestMessage));
612 m->amount = htonl (amount);
614 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
621 * Cancel request for reserving bandwidth.
623 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
626 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
632 * Get information about addresses known to the ATS subsystem.
634 * @param cfg configuration to use
635 * @param peer peer idm can be NULL for all peers
636 * @param all GNUNET_YES to get information about all addresses or GNUNET_NO to
637 * get only address currently used
638 * @param infocb callback to call with the addresses,
639 * will callback with address == NULL when done
640 * @param infocb_cls closure for infocb
641 * @return ats performance context
643 struct GNUNET_ATS_AddressListHandle*
644 GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *handle,
645 const struct GNUNET_PeerIdentity *peer,
647 GNUNET_ATS_PeerInformationCallback infocb,
650 struct GNUNET_ATS_AddressListHandle *alh;
652 GNUNET_assert (NULL != handle);
654 alh = GNUNET_malloc (sizeof (struct GNUNET_ATS_AddressListHandle));
656 alh->cb_cls = infocb_cls;
658 alh->all_addresses = all;
660 alh->all_peers = GNUNET_YES;
663 alh->all_peers = GNUNET_NO;
667 GNUNET_CONTAINER_DLL_insert (handle->addresslist_head, handle->addresslist_tail, alh);
670 FPRINTF (stderr, "TBD");
677 * Cancel a pending address listing operation
679 * @param handle the GNUNET_ATS_AddressListHandle handle to cancel
682 GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *handle)
684 GNUNET_assert (NULL != handle);
686 GNUNET_CONTAINER_DLL_remove (handle->ph->addresslist_head, handle->ph->addresslist_tail, handle);
687 GNUNET_free (handle);
692 * Change preferences for the given peer. Preference changes are forgotten if peers
695 * @param ph performance handle
696 * @param peer identifies the peer
697 * @param ... 0-terminated specification of the desired changes
700 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
701 const struct GNUNET_PeerIdentity *peer, ...)
703 struct PendingMessage *p;
704 struct ChangePreferenceMessage *m;
707 struct PreferenceInformation *pi;
709 enum GNUNET_ATS_PreferenceKind kind;
713 while (GNUNET_ATS_PREFERENCE_END !=
714 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
718 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
720 (void) va_arg (ap, double);
723 case GNUNET_ATS_PREFERENCE_LATENCY:
725 (void) va_arg (ap, double);
734 count * sizeof (struct PreferenceInformation) +
735 sizeof (struct ChangePreferenceMessage);
736 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
738 p->is_init = GNUNET_NO;
739 m = (struct ChangePreferenceMessage *) &p[1];
740 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
741 m->header.size = htons (msize);
742 m->num_preferences = htonl (count);
744 pi = (struct PreferenceInformation *) &m[1];
747 while (GNUNET_ATS_PREFERENCE_END !=
748 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
750 pi[count].preference_kind = htonl (kind);
753 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
754 pi[count].preference_value = (float) va_arg (ap, double);
758 case GNUNET_ATS_PREFERENCE_LATENCY:
759 pi[count].preference_value = (float) va_arg (ap, double);
768 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
772 /* end of ats_api_performance.c */