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_scheduling.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 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
63 struct GNUNET_ATS_SchedulingHandle
69 const struct GNUNET_CONFIGURATION_Handle *cfg;
72 * Callback to invoke on suggestions.
74 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
77 * Closure for 'suggest_cb'.
82 * Connection to ATS service.
84 struct GNUNET_CLIENT_Connection *client;
87 * Head of list of messages for the ATS service.
89 struct PendingMessage *pending_head;
92 * Tail of list of messages for the ATS service
94 struct PendingMessage *pending_tail;
97 * Current request for transmission to ATS.
99 struct GNUNET_CLIENT_TransmitHandle *th;
102 * Array of session objects (we need to translate them to numbers and back
103 * for the protocol; the offset in the array is the session number on the
104 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
105 * Unused entries are also NULL.
107 struct Session **session_array;
110 * Task to trigger reconnect.
112 GNUNET_SCHEDULER_TaskIdentifier task;
115 * Size of the session array.
117 unsigned int session_array_size;
123 * Re-establish the connection to the ATS service.
125 * @param sh handle to use to re-connect.
128 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
133 * Re-establish the connection to the ATS service.
135 * @param cls handle to use to re-connect.
136 * @param tc scheduler context
139 reconnect_task (void *cls,
140 const struct GNUNET_SCHEDULER_TaskContext *tc)
142 struct GNUNET_ATS_SchedulingHandle *sh = cls;
144 sh->task = GNUNET_SCHEDULER_NO_TASK;
150 * Transmit messages from the message queue to the service
151 * (if there are any, and if we are not already trying).
153 * @param sh handle to use
156 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
160 * Type of a function to call when we receive a message
163 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
164 * @param msg message received, NULL on timeout or fatal error
167 process_ats_message (void *cls,
168 const struct GNUNET_MessageHeader *msg);
172 * We can now transmit a message to ATS. Do it.
174 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
175 * @param size number of bytes we can transmit to ATS
176 * @param buf where to copy the messages
177 * @return number of bytes copied into buf
180 transmit_message_to_ats (void *cls,
184 struct GNUNET_ATS_SchedulingHandle *sh = cls;
185 struct PendingMessage *p;
192 while ( (NULL != (p = sh->pending_head)) &&
195 memcpy (&cbuf[ret], &p[1], p->size);
198 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
201 if (GNUNET_YES == p->is_init)
202 GNUNET_CLIENT_receive (sh->client,
203 &process_ats_message, sh,
204 GNUNET_TIME_UNIT_FOREVER_REL);
213 * Transmit messages from the message queue to the service
214 * (if there are any, and if we are not already trying).
216 * @param sh handle to use
219 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
221 struct PendingMessage *p;
225 if (NULL == (p = sh->pending_head))
227 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
229 GNUNET_TIME_UNIT_FOREVER_REL,
231 &transmit_message_to_ats, sh);
236 * Find the session object corresponding to the given session ID.
238 * @param sh our handle
239 * @param session_id current session ID
240 * @return the session object (or NULL)
242 static struct Session*
243 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
246 if (session_id >= sh->session_array_size)
251 return sh->session_array[session_id];
256 * Get the ID for the given session object. If we do not have an ID for
257 * the given session object, allocate one.
259 * @param sh our handle
260 * @param session session object
261 * @return the session id
264 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
265 struct Session *session)
271 for (i=1;i<sh->session_array_size;i++)
273 if (session == sh->session_array[i])
276 (sh->session_array[i] == NULL) )
281 f = sh->session_array_size;
282 GNUNET_array_grow (sh->session_array,
283 sh->session_array_size,
284 sh->session_array_size * 2);
286 sh->session_array[f] = session;
292 * Remove the session of the given session ID from the session
293 * table (it is no longer valid).
295 * @param sh our handle
296 * @param session_id identifies session that is no longer valid
299 remove_session (struct GNUNET_ATS_SchedulingHandle *sh,
302 GNUNET_assert (session_id < sh->session_array_size);
303 sh->session_array[session_id] = NULL;
308 * Type of a function to call when we receive a message
311 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
312 * @param msg message received, NULL on timeout or fatal error
315 process_ats_message (void *cls,
316 const struct GNUNET_MessageHeader *msg)
318 struct GNUNET_ATS_SchedulingHandle *sh = cls;
319 const struct AddressSuggestionMessage *m;
320 const struct GNUNET_TRANSPORT_ATS_Information *atsi;
322 const char *plugin_name;
323 uint16_t address_length;
324 uint16_t plugin_name_length;
329 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
331 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
332 &reconnect_task, sh);
335 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
336 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)) )
339 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
341 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
342 &reconnect_task, sh);
345 m = (const struct AddressSuggestionMessage*) msg;
346 ats_count = ntohl (m->ats_count);
347 address_length = ntohs (m->address_length);
348 atsi = (const struct GNUNET_TRANSPORT_ATS_Information*) &m[1];
349 address = (const char*) &atsi[ats_count];
350 plugin_name = &address[address_length];
351 plugin_name_length = ntohs (m->plugin_name_length);
352 if ( (address_length +
354 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
355 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
356 (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
357 (plugin_name[plugin_name_length - 1] != '\0') )
360 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
362 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
363 &reconnect_task, sh);
366 sh->suggest_cb (sh->suggest_cb_cls,
369 address, address_length,
370 find_session (sh, ntohl (m->session_id)),
375 GNUNET_CLIENT_receive (sh->client,
376 &process_ats_message, sh,
377 GNUNET_TIME_UNIT_FOREVER_REL);
382 * Re-establish the connection to the ATS service.
384 * @param sh handle to use to re-connect.
387 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
389 struct PendingMessage *p;
390 struct ClientStartMessage *init;
392 GNUNET_assert (NULL == sh->client);
393 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
394 GNUNET_assert (NULL != sh->client);
395 if ( (NULL == (p = sh->pending_head)) ||
396 (GNUNET_YES != p->is_init) )
398 p = GNUNET_malloc (sizeof (struct PendingMessage) +
399 sizeof (struct ClientStartMessage));
400 p->size = sizeof (struct ClientStartMessage);
401 p->is_init = GNUNET_YES;
402 init = (struct ClientStartMessage *) &p[1];
403 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
404 init->header.size = htons (sizeof (struct ClientStartMessage));
405 init->start_flag = htonl (START_FLAG_SCHEDULING);
406 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
415 * Initialize the ATS subsystem.
417 * @param cfg configuration to use
418 * @param suggest_cb notification to call whenever the suggestation changed
419 * @param suggest_cb_cls closure for 'suggest_cb'
420 * @return ats context
422 struct GNUNET_ATS_SchedulingHandle *
423 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
424 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
425 void *suggest_cb_cls)
427 struct GNUNET_ATS_SchedulingHandle *sh;
429 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
431 sh->suggest_cb = suggest_cb;
432 sh->suggest_cb_cls = suggest_cb_cls;
433 GNUNET_array_grow (sh->session_array,
434 sh->session_array_size,
442 * Client is done with ATS scheduling, release resources.
444 * @param sh handle to release
447 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
449 struct PendingMessage *p;
451 while (NULL != (p = sh->pending_head))
453 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
458 if (NULL != sh->client)
460 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
463 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
465 GNUNET_SCHEDULER_cancel (sh->task);
466 sh->task = GNUNET_SCHEDULER_NO_TASK;
468 GNUNET_array_grow (sh->session_array,
469 sh->session_array_size,
476 * We would like to establish a new connection with a peer. ATS
477 * should suggest a good address to begin with.
480 * @param peer identity of the peer we need an address for
483 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
484 const struct GNUNET_PeerIdentity *peer)
486 struct PendingMessage *p;
487 struct RequestAddressMessage *m;
489 p = GNUNET_malloc (sizeof (struct PendingMessage) +
490 sizeof (struct RequestAddressMessage));
491 p->size = sizeof (struct RequestAddressMessage);
492 p->is_init = GNUNET_NO;
493 m = (struct RequestAddressMessage*) &p[1];
494 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
495 m->header.size = htons (sizeof (struct RequestAddressMessage));
496 m->reserved = htonl (0);
498 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
506 * We have updated performance statistics for a given address. Note
507 * that this function can be called for addresses that are currently
508 * in use as well as addresses that are valid but not actively in use.
509 * Furthermore, the peer may not even be connected to us right now (in
510 * which case the call may be ignored or the information may be stored
511 * for later use). Update bandwidth assignments.
514 * @param peer identity of the new peer
515 * @param plugin_name name of the transport plugin
516 * @param plugin_addr address (if available)
517 * @param plugin_addr_len number of bytes in plugin_addr
518 * @param session session handle (if available)
519 * @param ats performance data for the address
520 * @param ats_count number of performance records in 'ats'
523 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
524 const struct GNUNET_PeerIdentity *peer,
525 const char *plugin_name,
526 const void *plugin_addr, size_t plugin_addr_len,
527 struct Session *session,
528 const struct GNUNET_TRANSPORT_ATS_Information *ats,
531 struct PendingMessage *p;
532 struct AddressUpdateMessage *m;
533 struct GNUNET_TRANSPORT_ATS_Information *am;
538 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
539 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
540 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) + namelen;
541 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
542 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
543 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
544 (ats_count >= GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
549 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
551 p->is_init = GNUNET_NO;
552 m = (struct AddressUpdateMessage*) &p[1];
553 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
554 m->header.size = htons (msize);
555 m->ats_count = htonl (ats_count);
557 m->address_length = htons (plugin_addr_len);
558 m->plugin_name_length = htons (namelen);
559 m->session_id = htonl (get_session_id (sh, session));
560 am = (struct GNUNET_TRANSPORT_ATS_Information*) &m[1];
561 memcpy (am, ats, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
562 pm = (char *) &am[ats_count];
563 memcpy (pm, plugin_addr, plugin_addr_len);
564 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
565 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
573 * A session got destroyed, stop including it as a valid address.
576 * @param peer identity of the peer
577 * @param plugin_name name of the transport plugin
578 * @param plugin_addr address (if available)
579 * @param plugin_addr_len number of bytes in plugin_addr
580 * @param session session handle that is no longer valid
583 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
584 const struct GNUNET_PeerIdentity *peer,
585 const char *plugin_name,
586 const void *plugin_addr,
587 size_t plugin_addr_len,
588 struct Session *session)
590 struct PendingMessage *p;
591 struct AddressDestroyedMessage *m;
597 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
598 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
600 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
601 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
602 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
607 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
609 p->is_init = GNUNET_NO;
610 m = (struct AddressDestroyedMessage*) &p[1];
611 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
612 m->header.size = htons (msize);
613 m->reserved = htonl (0);
615 m->address_length = htons (plugin_addr_len);
616 m->plugin_name_length = htons (namelen);
617 m->session_id = htonl (session_id = get_session_id (sh, session));
619 memcpy (pm, plugin_addr, plugin_addr_len);
620 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
621 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
625 remove_session (sh, session_id);
628 /* end of ats_api_scheduling.c */