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 if (NULL == sh->client)
228 return; /* currently reconnecting */
229 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
231 GNUNET_TIME_UNIT_FOREVER_REL,
233 &transmit_message_to_ats, sh);
238 * Find the session object corresponding to the given session ID.
240 * @param sh our handle
241 * @param session_id current session ID
242 * @return the session object (or NULL)
244 static struct Session*
245 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
248 if (session_id >= sh->session_array_size)
253 return sh->session_array[session_id];
258 * Get the ID for the given session object. If we do not have an ID for
259 * the given session object, allocate one.
261 * @param sh our handle
262 * @param session session object
263 * @return the session id
266 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
267 struct Session *session)
273 for (i=1;i<sh->session_array_size;i++)
275 if (session == sh->session_array[i])
278 (sh->session_array[i] == NULL) )
283 f = sh->session_array_size;
284 GNUNET_array_grow (sh->session_array,
285 sh->session_array_size,
286 sh->session_array_size * 2);
288 sh->session_array[f] = session;
294 * Remove the session of the given session ID from the session
295 * table (it is no longer valid).
297 * @param sh our handle
298 * @param session_id identifies session that is no longer valid
301 remove_session (struct GNUNET_ATS_SchedulingHandle *sh,
304 GNUNET_assert (session_id < sh->session_array_size);
305 sh->session_array[session_id] = NULL;
310 * Type of a function to call when we receive a message
313 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
314 * @param msg message received, NULL on timeout or fatal error
317 process_ats_message (void *cls,
318 const struct GNUNET_MessageHeader *msg)
320 struct GNUNET_ATS_SchedulingHandle *sh = cls;
321 const struct AddressSuggestionMessage *m;
322 const struct GNUNET_ATS_Information *atsi;
324 const char *plugin_name;
325 uint16_t address_length;
326 uint16_t plugin_name_length;
331 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
333 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
334 &reconnect_task, sh);
337 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
338 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)) )
341 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
343 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
344 &reconnect_task, sh);
347 m = (const struct AddressSuggestionMessage*) msg;
348 ats_count = ntohl (m->ats_count);
349 address_length = ntohs (m->address_length);
350 atsi = (const struct GNUNET_ATS_Information*) &m[1];
351 address = (const char*) &atsi[ats_count];
352 plugin_name = &address[address_length];
353 plugin_name_length = ntohs (m->plugin_name_length);
354 if ( (address_length +
356 ats_count * sizeof (struct GNUNET_ATS_Information) +
357 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
358 (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
359 (plugin_name[plugin_name_length - 1] != '\0') )
362 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
364 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
365 &reconnect_task, sh);
368 sh->suggest_cb (sh->suggest_cb_cls,
371 address, address_length,
372 find_session (sh, ntohl (m->session_id)),
377 GNUNET_CLIENT_receive (sh->client,
378 &process_ats_message, sh,
379 GNUNET_TIME_UNIT_FOREVER_REL);
384 * Re-establish the connection to the ATS service.
386 * @param sh handle to use to re-connect.
389 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
391 struct PendingMessage *p;
392 struct ClientStartMessage *init;
394 GNUNET_assert (NULL == sh->client);
395 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
396 GNUNET_assert (NULL != sh->client);
397 if ( (NULL == (p = sh->pending_head)) ||
398 (GNUNET_YES != p->is_init) )
400 p = GNUNET_malloc (sizeof (struct PendingMessage) +
401 sizeof (struct ClientStartMessage));
402 p->size = sizeof (struct ClientStartMessage);
403 p->is_init = GNUNET_YES;
404 init = (struct ClientStartMessage *) &p[1];
405 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
406 init->header.size = htons (sizeof (struct ClientStartMessage));
407 init->start_flag = htonl (START_FLAG_SCHEDULING);
408 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
417 * Initialize the ATS subsystem.
419 * @param cfg configuration to use
420 * @param suggest_cb notification to call whenever the suggestation changed
421 * @param suggest_cb_cls closure for 'suggest_cb'
422 * @return ats context
424 struct GNUNET_ATS_SchedulingHandle *
425 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
426 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
427 void *suggest_cb_cls)
429 struct GNUNET_ATS_SchedulingHandle *sh;
431 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
433 sh->suggest_cb = suggest_cb;
434 sh->suggest_cb_cls = suggest_cb_cls;
435 GNUNET_array_grow (sh->session_array,
436 sh->session_array_size,
444 * Client is done with ATS scheduling, release resources.
446 * @param sh handle to release
449 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
451 struct PendingMessage *p;
453 while (NULL != (p = sh->pending_head))
455 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
460 if (NULL != sh->client)
462 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
465 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
467 GNUNET_SCHEDULER_cancel (sh->task);
468 sh->task = GNUNET_SCHEDULER_NO_TASK;
470 GNUNET_array_grow (sh->session_array,
471 sh->session_array_size,
478 * We would like to establish a new connection with a peer. ATS
479 * should suggest a good address to begin with.
482 * @param peer identity of the peer we need an address for
485 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
486 const struct GNUNET_PeerIdentity *peer)
488 struct PendingMessage *p;
489 struct RequestAddressMessage *m;
491 p = GNUNET_malloc (sizeof (struct PendingMessage) +
492 sizeof (struct RequestAddressMessage));
493 p->size = sizeof (struct RequestAddressMessage);
494 p->is_init = GNUNET_NO;
495 m = (struct RequestAddressMessage*) &p[1];
496 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
497 m->header.size = htons (sizeof (struct RequestAddressMessage));
498 m->reserved = htonl (0);
500 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
508 * We have updated performance statistics for a given address. Note
509 * that this function can be called for addresses that are currently
510 * in use as well as addresses that are valid but not actively in use.
511 * Furthermore, the peer may not even be connected to us right now (in
512 * which case the call may be ignored or the information may be stored
513 * for later use). Update bandwidth assignments.
516 * @param peer identity of the new peer
517 * @param plugin_name name of the transport plugin
518 * @param plugin_addr address (if available)
519 * @param plugin_addr_len number of bytes in plugin_addr
520 * @param session session handle (if available)
521 * @param ats performance data for the address
522 * @param ats_count number of performance records in 'ats'
525 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
526 const struct GNUNET_PeerIdentity *peer,
527 const char *plugin_name,
528 const void *plugin_addr, size_t plugin_addr_len,
529 struct Session *session,
530 const struct GNUNET_ATS_Information *ats,
533 struct PendingMessage *p;
534 struct AddressUpdateMessage *m;
535 struct GNUNET_ATS_Information *am;
540 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
541 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
542 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
543 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
544 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
545 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
546 (ats_count >= GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) )
551 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
553 p->is_init = GNUNET_NO;
554 m = (struct AddressUpdateMessage*) &p[1];
555 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
556 m->header.size = htons (msize);
557 m->ats_count = htonl (ats_count);
559 m->address_length = htons (plugin_addr_len);
560 m->plugin_name_length = htons (namelen);
561 m->session_id = htonl (get_session_id (sh, session));
562 am = (struct GNUNET_ATS_Information*) &m[1];
563 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
564 pm = (char *) &am[ats_count];
565 memcpy (pm, plugin_addr, plugin_addr_len);
566 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
567 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
575 * A session got destroyed, stop including it as a valid address.
578 * @param peer identity of the peer
579 * @param plugin_name name of the transport plugin
580 * @param plugin_addr address (if available)
581 * @param plugin_addr_len number of bytes in plugin_addr
582 * @param session session handle that is no longer valid
585 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
586 const struct GNUNET_PeerIdentity *peer,
587 const char *plugin_name,
588 const void *plugin_addr,
589 size_t plugin_addr_len,
590 struct Session *session)
592 struct PendingMessage *p;
593 struct AddressDestroyedMessage *m;
599 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
600 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
602 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
603 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
604 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
609 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
611 p->is_init = GNUNET_NO;
612 m = (struct AddressDestroyedMessage*) &p[1];
613 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
614 m->header.size = htons (msize);
615 m->reserved = htonl (0);
617 m->address_length = htons (plugin_addr_len);
618 m->plugin_name_length = htons (namelen);
619 m->session_id = htonl (session_id = get_session_id (sh, session));
621 memcpy (pm, plugin_addr, plugin_addr_len);
622 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
623 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
627 remove_session (sh, session_id);
630 /* end of ats_api_scheduling.c */