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 sh 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,
184 const struct GNUNET_SCHEDULER_TaskContext *tc)
186 struct GNUNET_ATS_PerformanceHandle *ph = cls;
188 ph->task = GNUNET_SCHEDULER_NO_TASK;
194 * Transmit messages from the message queue to the service
195 * (if there are any, and if we are not already trying).
197 * @param sh handle to use
200 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
204 * We can now transmit a message to ATS. Do it.
206 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
207 * @param size number of bytes we can transmit to ATS
208 * @param buf where to copy the messages
209 * @return number of bytes copied into buf
212 transmit_message_to_ats (void *cls,
216 struct GNUNET_ATS_PerformanceHandle *ph = cls;
217 struct PendingMessage *p;
224 while ( (NULL != (p = ph->pending_head)) &&
227 memcpy (&cbuf[ret], &p[1], p->size);
230 GNUNET_CONTAINER_DLL_remove (ph->pending_head,
241 * Transmit messages from the message queue to the service
242 * (if there are any, and if we are not already trying).
244 * @param ph handle to use
247 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
249 struct PendingMessage *p;
253 if (NULL == (p = ph->pending_head))
255 ph->th = GNUNET_CLIENT_notify_transmit_ready (ph->client,
257 GNUNET_TIME_UNIT_FOREVER_REL,
259 &transmit_message_to_ats, ph);
264 * We received a peer information message. Validate and process it.
266 * @param ph our context with the callback
267 * @param msg the message
268 * @return GNUNET_OK if the message was well-formed
271 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
272 const struct GNUNET_MessageHeader *msg)
274 const struct PeerInformationMessage *pi;
275 const struct GNUNET_ATS_Information *atsi;
277 const char *plugin_name;
278 uint16_t address_length;
279 uint16_t plugin_name_length;
282 if (ph->infocb == NULL)
285 return GNUNET_SYSERR;
287 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
290 return GNUNET_SYSERR;
292 pi = (const struct PeerInformationMessage*) msg;
293 ats_count = ntohl (pi->ats_count);
294 address_length = ntohs (pi->address_length);
295 plugin_name_length = ntohs (pi->plugin_name_length);
296 atsi = (const struct GNUNET_ATS_Information*) &pi[1];
297 address = (const char*) &atsi[ats_count];
298 plugin_name = &address[address_length];
299 if ( (address_length +
301 ats_count * sizeof (struct GNUNET_ATS_Information) +
302 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
303 (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
304 (plugin_name[plugin_name_length - 1] != '\0') )
307 return GNUNET_SYSERR;
309 ph->infocb (ph->infocb_cls,
312 address, address_length,
322 * We received a reservation result message. Validate and process it.
324 * @param ph our context with the callback
325 * @param msg the message
326 * @return GNUNET_OK if the message was well-formed
329 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
330 const struct GNUNET_MessageHeader *msg)
332 const struct ReservationResultMessage *rr;
333 struct GNUNET_ATS_ReservationContext *rc;
336 if (ph->infocb == NULL)
339 return GNUNET_SYSERR;
341 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
344 return GNUNET_SYSERR;
346 rr = (const struct ReservationResultMessage*) msg;
347 amount = ntohl (rr->amount);
348 rc = ph->reservation_head;
349 if (0 != memcmp (&rr->peer,
351 sizeof (struct GNUNET_PeerIdentity)))
354 return GNUNET_SYSERR;
356 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
357 ph->reservation_tail,
359 if ( (amount == 0) ||
362 /* tell client if not cancelled */
364 rc->rcb (rc->rcb_cls,
367 GNUNET_TIME_relative_ntoh (rr->res_delay));
371 /* amount non-zero, but client cancelled, consider undo! */
372 if (GNUNET_YES != rc->undo)
375 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
378 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
384 * Type of a function to call when we receive a message
387 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
388 * @param msg message received, NULL on timeout or fatal error
391 process_ats_message (void *cls,
392 const struct GNUNET_MessageHeader *msg)
394 struct GNUNET_ATS_PerformanceHandle *ph = cls;
398 switch (ntohs (msg->type))
400 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
401 if (GNUNET_OK != process_pi_message (ph, msg))
404 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
405 if (GNUNET_OK != process_rr_message (ph, msg))
413 GNUNET_CLIENT_receive (ph->client,
414 &process_ats_message, ph,
415 GNUNET_TIME_UNIT_FOREVER_REL);
418 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
420 ph->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
421 &reconnect_task, ph);
426 * Re-establish the connection to the ATS service.
428 * @param ph handle to use to re-connect.
431 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
433 struct PendingMessage *p;
434 struct ClientStartMessage *init;
436 GNUNET_assert (NULL == ph->client);
437 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
438 GNUNET_assert (NULL != ph->client);
439 GNUNET_CLIENT_receive (ph->client,
440 &process_ats_message, ph,
441 GNUNET_TIME_UNIT_FOREVER_REL);
442 if ( (NULL == (p = ph->pending_head)) ||
443 (GNUNET_YES != p->is_init) )
445 p = GNUNET_malloc (sizeof (struct PendingMessage) +
446 sizeof (struct ClientStartMessage));
447 p->size = sizeof (struct ClientStartMessage);
448 p->is_init = GNUNET_YES;
449 init = (struct ClientStartMessage *) &p[1];
450 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
451 init->header.size = htons (sizeof (struct ClientStartMessage));
452 init->start_flag = htonl ((ph->infocb == NULL)
453 ? START_FLAG_PERFORMANCE_NO_PIC
454 : START_FLAG_PERFORMANCE_WITH_PIC);
455 GNUNET_CONTAINER_DLL_insert (ph->pending_head,
465 * Get handle to access performance API of the ATS subsystem.
467 * @param cfg configuration to use
468 * @param infocb function to call on allocation changes, can be NULL
469 * @param infocb_cls closure for infocb
470 * @return ats performance context
472 struct GNUNET_ATS_PerformanceHandle *
473 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
474 GNUNET_ATS_PeerInformationCallback infocb,
477 struct GNUNET_ATS_PerformanceHandle *ph;
479 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
482 ph->infocb_cls = infocb_cls;
489 * Client is done using the ATS performance subsystem, release resources.
494 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
496 struct PendingMessage *p;
497 struct GNUNET_ATS_ReservationContext *rc;
499 while (NULL != (p = ph->pending_head))
501 GNUNET_CONTAINER_DLL_remove (ph->pending_head,
506 while (NULL != (rc = ph->reservation_head))
508 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
509 ph->reservation_tail,
511 GNUNET_break (NULL == rc->rcb);
514 if (GNUNET_SCHEDULER_NO_TASK != ph->task)
516 GNUNET_SCHEDULER_cancel (ph->task);
517 ph->task = GNUNET_SCHEDULER_NO_TASK;
519 if (NULL != ph->client)
521 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
529 * Reserve inbound bandwidth from the given peer. ATS will look at
530 * the current amount of traffic we receive from the peer and ensure
531 * that the peer could add 'amount' of data to its stream.
533 * @param ph performance handle
534 * @param peer identifies the peer
535 * @param amount reserve N bytes for receiving, negative
536 * amounts can be used to undo a (recent) reservation;
537 * @param rcb function to call with the resulting reservation information
538 * @param rcb_cls closure for info
539 * @return NULL on error
540 * @deprecated will be replaced soon
542 struct GNUNET_ATS_ReservationContext *
543 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
544 const struct GNUNET_PeerIdentity *peer,
546 GNUNET_ATS_ReservationCallback rcb,
549 struct GNUNET_ATS_ReservationContext *rc;
550 struct PendingMessage *p;
551 struct ReservationRequestMessage *m;
553 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
557 rc->rcb_cls = rcb_cls;
558 if ( (rcb != NULL) && (amount > 0) )
559 rc->undo = GNUNET_YES;
560 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
561 ph->reservation_tail,
564 p = GNUNET_malloc (sizeof (struct PendingMessage) +
565 sizeof (struct ReservationRequestMessage));
566 p->size = sizeof (struct ReservationRequestMessage);
567 p->is_init = GNUNET_NO;
568 m = (struct ReservationRequestMessage*) &p[1];
569 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
570 m->header.size = htons (sizeof (struct ReservationRequestMessage));
571 m->amount = htonl (amount);
573 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
581 * Cancel request for reserving bandwidth.
583 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
586 GNUNET_ATS_reserve_bandwidth_cancel (struct
587 GNUNET_ATS_ReservationContext *rc)
594 * Change preferences for the given peer. Preference changes are forgotten if peers
597 * @param ph performance handle
598 * @param peer identifies the peer
599 * @param ... 0-terminated specification of the desired changes
602 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
603 const struct GNUNET_PeerIdentity *peer,
606 struct PendingMessage *p;
607 struct ChangePreferenceMessage *m;
610 struct PreferenceInformation *pi;
612 enum GNUNET_ATS_PreferenceKind kind;
616 while (GNUNET_ATS_PREFERENCE_END != (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
620 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
622 (void) va_arg (ap, double);
624 case GNUNET_ATS_PREFERENCE_LATENCY:
626 (void) va_arg (ap, double);
633 msize = count * sizeof (struct PreferenceInformation) +
634 sizeof (struct ChangePreferenceMessage);
635 p = GNUNET_malloc (sizeof (struct PendingMessage) +
638 p->is_init = GNUNET_NO;
639 m = (struct ChangePreferenceMessage*) &p[1];
640 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
641 m->header.size = htons (msize);
642 m->num_preferences = htonl (count);
644 pi = (struct PreferenceInformation*) &m[1];
647 while (GNUNET_ATS_PREFERENCE_END != (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
649 pi[count].preference_kind = htonl (kind);
652 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
653 pi[count].preference_value = (float) va_arg (ap, double);
656 case GNUNET_ATS_PREFERENCE_LATENCY:
657 pi[count].preference_value = (float) va_arg (ap, double);
665 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
670 /* end of ats_api_performance.c */