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 * ATS Handle to obtain and/or modify performance information.
111 struct GNUNET_ATS_PerformanceHandle
117 const struct GNUNET_CONFIGURATION_Handle *cfg;
120 * Callback to invoke on performance changes.
122 GNUNET_ATS_PeerInformationCallback infocb;
125 * Closure for 'infocb'.
130 * Connection to ATS service.
132 struct GNUNET_CLIENT_Connection *client;
135 * Head of list of messages for the ATS service.
137 struct PendingMessage *pending_head;
140 * Tail of list of messages for the ATS service
142 struct PendingMessage *pending_tail;
145 * Head of linked list of pending reservation requests.
147 struct GNUNET_ATS_ReservationContext *reservation_head;
150 * Tail of linked list of pending reservation requests.
152 struct GNUNET_ATS_ReservationContext *reservation_tail;
155 * Current request for transmission to ATS.
157 struct GNUNET_CLIENT_TransmitHandle *th;
160 * Task to trigger reconnect.
162 GNUNET_SCHEDULER_TaskIdentifier task;
168 * Re-establish the connection to the ATS service.
170 * @param ph handle to use to re-connect.
173 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
177 * Re-establish the connection to the ATS service.
179 * @param cls handle to use to re-connect.
180 * @param tc scheduler context
183 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
185 struct GNUNET_ATS_PerformanceHandle *ph = cls;
187 ph->task = GNUNET_SCHEDULER_NO_TASK;
193 * Transmit messages from the message queue to the service
194 * (if there are any, and if we are not already trying).
196 * @param ph handle to use
199 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
203 * We can now transmit a message to ATS. Do it.
205 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
206 * @param size number of bytes we can transmit to ATS
207 * @param buf where to copy the messages
208 * @return number of bytes copied into buf
211 transmit_message_to_ats (void *cls, size_t size, void *buf)
213 struct GNUNET_ATS_PerformanceHandle *ph = cls;
214 struct PendingMessage *p;
221 while ((NULL != (p = ph->pending_head)) && (p->size <= size))
223 memcpy (&cbuf[ret], &p[1], p->size);
226 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
235 * Transmit messages from the message queue to the service
236 * (if there are any, and if we are not already trying).
238 * @param ph handle to use
241 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
243 struct PendingMessage *p;
247 if (NULL == (p = ph->pending_head))
249 if (NULL == ph->client)
250 return; /* currently reconnecting */
252 GNUNET_CLIENT_notify_transmit_ready (ph->client, p->size,
253 GNUNET_TIME_UNIT_FOREVER_REL,
254 GNUNET_YES, &transmit_message_to_ats,
260 * We received a peer information message. Validate and process it.
262 * @param ph our context with the callback
263 * @param msg the message
264 * @return GNUNET_OK if the message was well-formed
267 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
268 const struct GNUNET_MessageHeader *msg)
270 const struct PeerInformationMessage *pi;
271 const struct GNUNET_ATS_Information *atsi;
272 const char *plugin_address;
273 const char *plugin_name;
274 struct GNUNET_HELLO_Address address;
275 uint16_t plugin_address_length;
276 uint16_t plugin_name_length;
279 if (ph->infocb == NULL)
282 return GNUNET_SYSERR;
284 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
287 return GNUNET_SYSERR;
289 pi = (const struct PeerInformationMessage *) msg;
290 ats_count = ntohl (pi->ats_count);
291 plugin_address_length = ntohs (pi->address_length);
292 plugin_name_length = ntohs (pi->plugin_name_length);
293 atsi = (const struct GNUNET_ATS_Information *) &pi[1];
294 plugin_address = (const char *) &atsi[ats_count];
295 plugin_name = &plugin_address[plugin_address_length];
296 if ((plugin_address_length + plugin_name_length +
297 ats_count * sizeof (struct GNUNET_ATS_Information) +
298 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
300 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
301 || (plugin_name[plugin_name_length - 1] != '\0'))
304 return GNUNET_SYSERR;
306 address.peer = pi->peer;
307 address.address = plugin_address;
308 address.address_length = plugin_address_length;
309 address.transport_name = plugin_name;
310 ph->infocb (ph->infocb_cls, &address, pi->bandwidth_out, pi->bandwidth_in,
317 * We received a reservation result 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_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
325 const struct GNUNET_MessageHeader *msg)
327 const struct ReservationResultMessage *rr;
328 struct GNUNET_ATS_ReservationContext *rc;
331 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
334 return GNUNET_SYSERR;
336 rr = (const struct ReservationResultMessage *) msg;
337 amount = ntohl (rr->amount);
338 rc = ph->reservation_head;
339 if (0 != memcmp (&rr->peer, &rc->peer, sizeof (struct GNUNET_PeerIdentity)))
342 return GNUNET_SYSERR;
344 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail, rc);
345 if ((amount == 0) || (rc->rcb != NULL))
347 /* tell client if not cancelled */
349 rc->rcb (rc->rcb_cls, &rr->peer, amount,
350 GNUNET_TIME_relative_ntoh (rr->res_delay));
354 /* amount non-zero, but client cancelled, consider undo! */
355 if (GNUNET_YES != rc->undo)
358 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
361 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
367 * Type of a function to call when we receive a message
370 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
371 * @param msg message received, NULL on timeout or fatal error
374 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
376 struct GNUNET_ATS_PerformanceHandle *ph = cls;
380 switch (ntohs (msg->type))
382 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
383 if (GNUNET_OK != process_pi_message (ph, msg))
386 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
387 if (GNUNET_OK != process_rr_message (ph, msg))
394 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
395 GNUNET_TIME_UNIT_FOREVER_REL);
400 GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
403 GNUNET_CLIENT_disconnect (ph->client);
406 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
412 * Re-establish the connection to the ATS service.
414 * @param ph handle to use to re-connect.
417 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
419 struct PendingMessage *p;
420 struct ClientStartMessage *init;
422 GNUNET_assert (NULL == ph->client);
423 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
424 GNUNET_assert (NULL != ph->client);
425 GNUNET_CLIENT_receive (ph->client, &process_ats_message, ph,
426 GNUNET_TIME_UNIT_FOREVER_REL);
427 if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
429 p = GNUNET_malloc (sizeof (struct PendingMessage) +
430 sizeof (struct ClientStartMessage));
431 p->size = sizeof (struct ClientStartMessage);
432 p->is_init = GNUNET_YES;
433 init = (struct ClientStartMessage *) &p[1];
434 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
435 init->header.size = htons (sizeof (struct ClientStartMessage));
437 htonl ((ph->infocb ==
438 NULL) ? START_FLAG_PERFORMANCE_NO_PIC :
439 START_FLAG_PERFORMANCE_WITH_PIC);
440 GNUNET_CONTAINER_DLL_insert (ph->pending_head, ph->pending_tail, p);
448 * Get handle to access performance API of the ATS subsystem.
450 * @param cfg configuration to use
451 * @param infocb function to call on allocation changes, can be NULL
452 * @param infocb_cls closure for infocb
453 * @return ats performance context
455 struct GNUNET_ATS_PerformanceHandle *
456 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
457 GNUNET_ATS_PeerInformationCallback infocb,
460 struct GNUNET_ATS_PerformanceHandle *ph;
462 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
465 ph->infocb_cls = infocb_cls;
472 * Client is done using the ATS performance subsystem, release resources.
477 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
479 struct PendingMessage *p;
480 struct GNUNET_ATS_ReservationContext *rc;
482 while (NULL != (p = ph->pending_head))
484 GNUNET_CONTAINER_DLL_remove (ph->pending_head, ph->pending_tail, p);
487 while (NULL != (rc = ph->reservation_head))
489 GNUNET_CONTAINER_DLL_remove (ph->reservation_head, ph->reservation_tail,
491 GNUNET_break (NULL == rc->rcb);
494 if (GNUNET_SCHEDULER_NO_TASK != ph->task)
496 GNUNET_SCHEDULER_cancel (ph->task);
497 ph->task = GNUNET_SCHEDULER_NO_TASK;
499 if (NULL != ph->client)
501 GNUNET_CLIENT_disconnect (ph->client);
509 * Reserve inbound bandwidth from the given peer. ATS will look at
510 * the current amount of traffic we receive from the peer and ensure
511 * that the peer could add 'amount' of data to its stream.
513 * @param ph performance handle
514 * @param peer identifies the peer
515 * @param amount reserve N bytes for receiving, negative
516 * amounts can be used to undo a (recent) reservation;
517 * @param rcb function to call with the resulting reservation information
518 * @param rcb_cls closure for info
519 * @return NULL on error
520 * @deprecated will be replaced soon
522 struct GNUNET_ATS_ReservationContext *
523 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
524 const struct GNUNET_PeerIdentity *peer,
526 GNUNET_ATS_ReservationCallback rcb, void *rcb_cls)
528 struct GNUNET_ATS_ReservationContext *rc;
529 struct PendingMessage *p;
530 struct ReservationRequestMessage *m;
532 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
536 rc->rcb_cls = rcb_cls;
537 if ((rcb != NULL) && (amount > 0))
538 rc->undo = GNUNET_YES;
539 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head, ph->reservation_tail,
542 p = GNUNET_malloc (sizeof (struct PendingMessage) +
543 sizeof (struct ReservationRequestMessage));
544 p->size = sizeof (struct ReservationRequestMessage);
545 p->is_init = GNUNET_NO;
546 m = (struct ReservationRequestMessage *) &p[1];
547 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST);
548 m->header.size = htons (sizeof (struct ReservationRequestMessage));
549 m->amount = htonl (amount);
551 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
558 * Cancel request for reserving bandwidth.
560 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
563 GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc)
570 * Change preferences for the given peer. Preference changes are forgotten if peers
573 * @param ph performance handle
574 * @param peer identifies the peer
575 * @param ... 0-terminated specification of the desired changes
578 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
579 const struct GNUNET_PeerIdentity *peer, ...)
581 struct PendingMessage *p;
582 struct ChangePreferenceMessage *m;
585 struct PreferenceInformation *pi;
587 enum GNUNET_ATS_PreferenceKind kind;
591 while (GNUNET_ATS_PREFERENCE_END !=
592 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
596 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
598 (void) va_arg (ap, double);
601 case GNUNET_ATS_PREFERENCE_LATENCY:
603 (void) va_arg (ap, double);
612 count * sizeof (struct PreferenceInformation) +
613 sizeof (struct ChangePreferenceMessage);
614 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
616 p->is_init = GNUNET_NO;
617 m = (struct ChangePreferenceMessage *) &p[1];
618 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE);
619 m->header.size = htons (msize);
620 m->num_preferences = htonl (count);
622 pi = (struct PreferenceInformation *) &m[1];
625 while (GNUNET_ATS_PREFERENCE_END !=
626 (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
628 pi[count].preference_kind = htonl (kind);
631 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
632 pi[count].preference_value = (float) va_arg (ap, double);
636 case GNUNET_ATS_PREFERENCE_LATENCY:
637 pi[count].preference_value = (float) va_arg (ap, double);
646 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head, ph->pending_tail, p);
650 /* end of ats_api_performance.c */