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;
163 * Re-establish the connection to the ATS service.
165 * @param sh handle to use to re-connect.
168 reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
172 * Transmit messages from the message queue to the service
173 * (if there are any, and if we are not already trying).
175 * @param sh handle to use
178 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph);
182 * We can now transmit a message to ATS. Do it.
184 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
185 * @param size number of bytes we can transmit to ATS
186 * @param buf where to copy the messages
187 * @return number of bytes copied into buf
190 transmit_message_to_ats (void *cls,
194 struct GNUNET_ATS_PerformanceHandle *ph = cls;
195 struct PendingMessage *p;
202 while ( (NULL != (p = ph->pending_head)) &&
205 memcpy (&cbuf[ret], &p[1], p->size);
208 GNUNET_CONTAINER_DLL_remove (ph->pending_head,
219 * Transmit messages from the message queue to the service
220 * (if there are any, and if we are not already trying).
222 * @param ph handle to use
225 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
227 struct PendingMessage *p;
231 if (NULL == (p = ph->pending_head))
233 ph->th = GNUNET_CLIENT_notify_transmit_ready (ph->client,
235 GNUNET_TIME_UNIT_FOREVER_REL,
237 &transmit_message_to_ats, ph);
242 * We received a peer information message. Validate and process it.
244 * @param ph our context with the callback
245 * @param msg the message
246 * @return GNUNET_OK if the message was well-formed
249 process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
250 const struct GNUNET_MessageHeader *msg)
252 const struct PeerInformationMessage *pi;
253 const struct GNUNET_TRANSPORT_ATS_Information *atsi;
255 const char *plugin_name;
256 uint16_t address_length;
257 uint16_t plugin_name_length;
260 if (ph->infocb == NULL)
263 return GNUNET_SYSERR;
265 if (ntohs (msg->size) < sizeof (struct PeerInformationMessage))
268 return GNUNET_SYSERR;
270 pi = (const struct PeerInformationMessage*) msg;
271 ats_count = ntohl (pi->ats_count);
272 address_length = ntohs (pi->address_length);
273 plugin_name_length = ntohs (pi->plugin_name_length);
274 atsi = (const struct GNUNET_TRANSPORT_ATS_Information*) &pi[1];
275 address = (const char*) &atsi[ats_count];
276 plugin_name = &address[address_length];
277 if ( (address_length +
279 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
280 sizeof (struct PeerInformationMessage) != ntohs (msg->size)) ||
281 (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
282 (plugin_name[plugin_name_length - 1] != '\0') )
285 return GNUNET_SYSERR;
287 ph->infocb (ph->infocb_cls,
290 address, address_length,
300 * We received a reservation result message. Validate and process it.
302 * @param ph our context with the callback
303 * @param msg the message
304 * @return GNUNET_OK if the message was well-formed
307 process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
308 const struct GNUNET_MessageHeader *msg)
310 const struct ReservationResultMessage *rr;
311 struct GNUNET_ATS_ReservationContext *rc;
314 if (ph->infocb == NULL)
317 return GNUNET_SYSERR;
319 if (ntohs (msg->size) < sizeof (struct ReservationResultMessage))
322 return GNUNET_SYSERR;
324 rr = (const struct ReservationResultMessage*) msg;
325 amount = ntohl (rr->amount);
326 rc = ph->reservation_head;
327 if (0 != memcmp (&rr->peer,
329 sizeof (struct GNUNET_PeerIdentity)))
332 return GNUNET_SYSERR;
334 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
335 ph->reservation_tail,
337 if ( (amount == 0) ||
340 /* tell client if not cancelled */
342 rc->rcb (rc->rcb_cls,
345 GNUNET_TIME_relative_ntoh (rr->res_delay));
350 /* amount non-zero, but client cancelled, consider undo! */
351 if (GNUNET_YES != rc->undo)
352 return GNUNET_OK; /* do not try to undo failed undos or negative amounts */
353 (void) GNUNET_ATS_reserve_bandwidth (ph, &rr->peer, -amount, NULL, NULL);
359 * Type of a function to call when we receive a message
362 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
363 * @param msg message received, NULL on timeout or fatal error
366 process_ats_message (void *cls,
367 const struct GNUNET_MessageHeader *msg)
369 struct GNUNET_ATS_PerformanceHandle *ph = cls;
373 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
378 switch (ntohs (msg->type))
380 case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
381 if (GNUNET_OK != process_pi_message (ph, msg))
383 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
389 case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
390 if (GNUNET_OK != process_rr_message (ph, msg))
392 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
400 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
405 GNUNET_CLIENT_receive (ph->client,
406 &process_ats_message, ph,
407 GNUNET_TIME_UNIT_FOREVER_REL);
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,
426 &process_ats_message, ph,
427 GNUNET_TIME_UNIT_FOREVER_REL);
428 if ( (NULL == (p = ph->pending_head)) ||
429 (GNUNET_YES != p->is_init) )
431 p = GNUNET_malloc (sizeof (struct PendingMessage) +
432 sizeof (struct ClientStartMessage));
433 p->size = sizeof (struct ClientStartMessage);
434 p->is_init = GNUNET_YES;
435 init = (struct ClientStartMessage *) &p[1];
436 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
437 init->header.size = htons (sizeof (struct ClientStartMessage));
438 init->start_flag = htonl ((ph->infocb == NULL)
439 ? START_FLAG_PERFORMANCE_NO_PIC
440 : START_FLAG_PERFORMANCE_WITH_PIC);
441 GNUNET_CONTAINER_DLL_insert (ph->pending_head,
451 * Get handle to access performance API of the ATS subsystem.
453 * @param cfg configuration to use
454 * @param infocb function to call on allocation changes, can be NULL
455 * @param infocb_cls closure for infocb
456 * @return ats performance context
458 struct GNUNET_ATS_PerformanceHandle *
459 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
460 GNUNET_ATS_PeerInformationCallback infocb,
463 struct GNUNET_ATS_PerformanceHandle *ph;
465 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
468 ph->infocb_cls = infocb_cls;
475 * Client is done using the ATS performance subsystem, release resources.
480 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
482 struct PendingMessage *p;
483 struct GNUNET_ATS_ReservationContext *rc;
485 while (NULL != (p = ph->pending_head))
487 GNUNET_CONTAINER_DLL_remove (ph->pending_head,
492 while (NULL != (rc = ph->reservation_head))
494 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
495 ph->reservation_tail,
497 GNUNET_break (NULL == rc->rcb);
500 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
506 * Reserve inbound bandwidth from the given peer. ATS will look at
507 * the current amount of traffic we receive from the peer and ensure
508 * that the peer could add 'amount' of data to its stream.
510 * @param ph performance handle
511 * @param peer identifies the peer
512 * @param amount reserve N bytes for receiving, negative
513 * amounts can be used to undo a (recent) reservation;
514 * @param rcb function to call with the resulting reservation information
515 * @param rcb_cls closure for info
516 * @return NULL on error
517 * @deprecated will be replaced soon
519 struct GNUNET_ATS_ReservationContext *
520 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
521 const struct GNUNET_PeerIdentity *peer,
523 GNUNET_ATS_ReservationCallback rcb,
526 struct GNUNET_ATS_ReservationContext *rc;
527 struct PendingMessage *p;
528 struct ReservationRequestMessage *m;
530 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
534 rc->rcb_cls = rcb_cls;
535 if ( (rc != NULL) && (amount > 0) )
536 rc->undo = GNUNET_YES;
537 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
538 ph->reservation_tail,
541 p = GNUNET_malloc (sizeof (struct PendingMessage) +
542 sizeof (struct ReservationRequestMessage));
543 p->size = sizeof (struct ReservationRequestMessage);
544 p->is_init = GNUNET_NO;
545 m = (struct ReservationRequestMessage*) &p[1];
546 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
547 m->header.size = htons (sizeof (struct ReservationRequestMessage));
548 m->amount = htonl (amount);
550 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
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
564 GNUNET_ATS_ReservationContext *rc)
571 * Change preferences for the given peer. Preference changes are forgotten if peers
574 * @param ph performance handle
575 * @param peer identifies the peer
576 * @param ... 0-terminated specification of the desired changes
579 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
580 const struct GNUNET_PeerIdentity *peer,
583 struct PendingMessage *p;
584 struct ChangePreferenceMessage *m;
587 struct PreferenceInformation *pi;
589 enum GNUNET_ATS_PreferenceKind kind;
593 while (GNUNET_ATS_PREFERENCE_END != (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
597 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
599 (void) va_arg (ap, double);
601 case GNUNET_ATS_PREFERENCE_LATENCY:
603 (void) va_arg (ap, double);
610 msize = count * sizeof (struct PreferenceInformation) +
611 sizeof (struct ChangePreferenceMessage);
612 p = GNUNET_malloc (sizeof (struct PendingMessage) +
615 p->is_init = GNUNET_NO;
616 m = (struct ChangePreferenceMessage*) &p[1];
617 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
618 m->header.size = htons (msize);
619 m->num_preferences = htonl (count);
621 pi = (struct PreferenceInformation*) &m[1];
624 while (GNUNET_ATS_PREFERENCE_END != (kind = va_arg (ap, enum GNUNET_ATS_PreferenceKind)))
626 pi[count].preference_kind = htonl (kind);
629 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
630 pi[count].preference_value = (float) va_arg (ap, double);
633 case GNUNET_ATS_PREFERENCE_LATENCY:
634 pi[count].preference_value = (float) va_arg (ap, double);
642 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
647 /* end of ats_api_performance.c */