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 info;
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);
207 GNUNET_CONTAINER_DLL_remove (ph->pending_head,
218 * Transmit messages from the message queue to the service
219 * (if there are any, and if we are not already trying).
221 * @param ph handle to use
224 do_transmit (struct GNUNET_ATS_PerformanceHandle *ph)
226 struct PendingMessage *p;
230 if (NULL == (p = ph->pending_head))
232 ph->th = GNUNET_CLIENT_notify_transmit_ready (ph->client,
234 GNUNET_TIME_UNIT_FOREVER_REL,
236 &transmit_message_to_ats, ph);
241 * Type of a function to call when we receive a message
244 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
245 * @param msg message received, NULL on timeout or fatal error
248 process_ats_message (void *cls,
249 const struct GNUNET_MessageHeader *msg)
251 struct GNUNET_ATS_PerformanceHandle *ph = cls;
255 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
260 switch (ntohs (msg->type))
265 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
270 GNUNET_CLIENT_receive (ph->client,
271 &process_ats_message, ph,
272 GNUNET_TIME_UNIT_FOREVER_REL);
277 * Re-establish the connection to the ATS service.
279 * @param ph handle to use to re-connect.
282 reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
284 struct PendingMessage *p;
285 struct ClientStartMessage *init;
287 GNUNET_assert (NULL == ph->client);
288 ph->client = GNUNET_CLIENT_connect ("ats", ph->cfg);
289 GNUNET_assert (NULL != ph->client);
290 GNUNET_CLIENT_receive (ph->client,
291 &process_ats_message, ph,
292 GNUNET_TIME_UNIT_FOREVER_REL);
293 if ( (NULL == (p = ph->pending_head)) ||
294 (GNUNET_YES != p->is_init) )
296 p = GNUNET_malloc (sizeof (struct PendingMessage) +
297 sizeof (struct ClientStartMessage));
298 p->size = sizeof (struct ClientStartMessage);
299 p->is_init = GNUNET_YES;
300 init = (struct ClientStartMessage *) &p[1];
301 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
302 init->header.size = htons (sizeof (struct ClientStartMessage));
303 init->start_flag = htonl ((ph->infocb == NULL)
304 ? START_FLAG_PERFORMANCE_NO_PIC
305 : START_FLAG_PERFORMANCE_WITH_PIC);
306 GNUNET_CONTAINER_DLL_insert (ph->pending_head,
316 * Get handle to access performance API of the ATS subsystem.
318 * @param cfg configuration to use
319 * @param infocb function to call on allocation changes, can be NULL
320 * @param infocb_cls closure for infocb
321 * @return ats performance context
323 struct GNUNET_ATS_PerformanceHandle *
324 GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
325 GNUNET_ATS_PeerInformationCallback infocb,
328 struct GNUNET_ATS_PerformanceHandle *ph;
330 ph = GNUNET_malloc (sizeof (struct GNUNET_ATS_PerformanceHandle));
333 ph->infocb_cls = infocb_cls;
340 * Client is done using the ATS performance subsystem, release resources.
345 GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph)
347 struct PendingMessage *p;
348 struct GNUNET_ATS_ReservationContext *rc;
350 while (NULL != (p = ph->pending_head))
352 GNUNET_CONTAINER_DLL_remove (ph->pending_head,
357 while (NULL != (rc = ph->reservation_head))
359 GNUNET_CONTAINER_DLL_remove (ph->reservation_head,
360 ph->reservation_tail,
362 GNUNET_break (NULL == rc->info);
365 GNUNET_CLIENT_disconnect (ph->client, GNUNET_NO);
371 * Reserve inbound bandwidth from the given peer. ATS will look at
372 * the current amount of traffic we receive from the peer and ensure
373 * that the peer could add 'amount' of data to its stream.
375 * @param ph performance handle
376 * @param peer identifies the peer
377 * @param amount reserve N bytes for receiving, negative
378 * amounts can be used to undo a (recent) reservation;
379 * @param info function to call with the resulting reservation information
380 * @param info_cls closure for info
381 * @return NULL on error
382 * @deprecated will be replaced soon
384 struct GNUNET_ATS_ReservationContext *
385 GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
386 const struct GNUNET_PeerIdentity *peer,
388 GNUNET_ATS_ReservationCallback info,
391 struct GNUNET_ATS_ReservationContext *rc;
392 struct PendingMessage *p;
393 struct ReservationRequestMessage *m;
395 rc = GNUNET_malloc (sizeof (struct GNUNET_ATS_ReservationContext));
399 rc->info_cls = info_cls;
400 GNUNET_CONTAINER_DLL_insert_tail (ph->reservation_head,
401 ph->reservation_tail,
404 p = GNUNET_malloc (sizeof (struct PendingMessage) +
405 sizeof (struct ReservationRequestMessage));
406 p->size = sizeof (struct ReservationRequestMessage);
407 p->is_init = GNUNET_NO;
408 m = (struct ReservationRequestMessage*) &p[1];
409 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
410 m->header.size = htons (sizeof (struct ReservationRequestMessage));
411 m->amount = htonl (amount);
413 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
421 * Cancel request for reserving bandwidth.
423 * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
426 GNUNET_ATS_reserve_bandwidth_cancel (struct
427 GNUNET_ATS_ReservationContext *rc)
434 * Change preferences for the given peer. Preference changes are forgotten if peers
437 * @param ph performance handle
438 * @param peer identifies the peer
439 * @param ... 0-terminated specification of the desired changes
442 GNUNET_ATS_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
443 const struct GNUNET_PeerIdentity *peer,
446 struct PendingMessage *p;
447 struct ChangePreferenceMessage *m;
450 struct PreferenceInformation *pi;
452 // FIXME: set 'count'
453 p = GNUNET_malloc (sizeof (struct PendingMessage) +
454 sizeof (struct ChangePreferenceMessage) +
455 count * sizeof (struct PreferenceInformation));
457 p->is_init = GNUNET_NO;
458 m = (struct ReservationRequestMessage*) &p[1];
459 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
460 m->header.size = htons (msize);
461 m->num_preferences = htonl (count);
463 pi = (struct PreferenceInformation*) &m[1];
464 // FIXME: fill in 'pi'
466 GNUNET_CONTAINER_DLL_insert_tail (ph->pending_head,
471 /* end of ats_api_performance.c */