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 * Information we track per session.
66 * Identity of the peer (just needed for error checking).
68 struct GNUNET_PeerIdentity peer;
73 struct Session *session;
76 * Set to GNUNET_YES if the slot is used.
83 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
85 struct GNUNET_ATS_SchedulingHandle
91 const struct GNUNET_CONFIGURATION_Handle *cfg;
94 * Callback to invoke on suggestions.
96 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
99 * Closure for 'suggest_cb'.
101 void *suggest_cb_cls;
104 * Connection to ATS service.
106 struct GNUNET_CLIENT_Connection *client;
109 * Head of list of messages for the ATS service.
111 struct PendingMessage *pending_head;
114 * Tail of list of messages for the ATS service
116 struct PendingMessage *pending_tail;
119 * Current request for transmission to ATS.
121 struct GNUNET_CLIENT_TransmitHandle *th;
124 * Array of session objects (we need to translate them to numbers and back
125 * for the protocol; the offset in the array is the session number on the
126 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
127 * Unused entries are also NULL.
129 struct SessionRecord *session_array;
132 * Task to trigger reconnect.
134 GNUNET_SCHEDULER_TaskIdentifier task;
137 * Size of the session array.
139 unsigned int session_array_size;
145 * Re-establish the connection to the ATS service.
147 * @param sh handle to use to re-connect.
150 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
155 * Re-establish the connection to the ATS service.
157 * @param cls handle to use to re-connect.
158 * @param tc scheduler context
161 reconnect_task (void *cls,
162 const struct GNUNET_SCHEDULER_TaskContext *tc)
164 struct GNUNET_ATS_SchedulingHandle *sh = cls;
166 sh->task = GNUNET_SCHEDULER_NO_TASK;
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_SchedulingHandle *sh);
182 * Type of a function to call when we receive a message
185 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
186 * @param msg message received, NULL on timeout or fatal error
189 process_ats_message (void *cls,
190 const struct GNUNET_MessageHeader *msg);
194 * We can now transmit a message to ATS. Do it.
196 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
197 * @param size number of bytes we can transmit to ATS
198 * @param buf where to copy the messages
199 * @return number of bytes copied into buf
202 transmit_message_to_ats (void *cls,
206 struct GNUNET_ATS_SchedulingHandle *sh = cls;
207 struct PendingMessage *p;
214 while ( (NULL != (p = sh->pending_head)) &&
217 memcpy (&cbuf[ret], &p[1], p->size);
220 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
223 if (GNUNET_YES == p->is_init)
224 GNUNET_CLIENT_receive (sh->client,
225 &process_ats_message, sh,
226 GNUNET_TIME_UNIT_FOREVER_REL);
235 * Transmit messages from the message queue to the service
236 * (if there are any, and if we are not already trying).
238 * @param sh handle to use
241 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
243 struct PendingMessage *p;
247 if (NULL == (p = sh->pending_head))
249 if (NULL == sh->client)
250 return; /* currently reconnecting */
251 sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
253 GNUNET_TIME_UNIT_FOREVER_REL,
255 &transmit_message_to_ats, sh);
260 * Find the session object corresponding to the given session ID.
262 * @param sh our handle
263 * @param session_id current session ID
264 * @param peer peer the session belongs to
265 * @return the session object (or NULL)
267 static struct Session*
268 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
270 const struct GNUNET_PeerIdentity *peer)
272 if (session_id >= sh->session_array_size)
279 GNUNET_assert (0 == memcmp (peer,
280 &sh->session_array[session_id].peer,
281 sizeof (struct GNUNET_PeerIdentity)));
282 return sh->session_array[session_id].session;
287 * Get the ID for the given session object. If we do not have an ID for
288 * the given session object, allocate one.
290 * @param sh our handle
291 * @param session session object
292 * @param peer peer the session belongs to
293 * @return the session id
296 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
297 struct Session *session,
298 const struct GNUNET_PeerIdentity *peer)
306 for (i=1;i<sh->session_array_size;i++)
308 if (session == sh->session_array[i].session)
310 GNUNET_assert (0 == memcmp (peer,
311 &sh->session_array[i].peer,
312 sizeof (struct GNUNET_PeerIdentity)));
316 (sh->session_array[i].slot_used == GNUNET_NO) )
321 f = sh->session_array_size;
322 GNUNET_array_grow (sh->session_array,
323 sh->session_array_size,
324 sh->session_array_size * 2);
326 GNUNET_assert (f > 0);
327 sh->session_array[f].session = session;
328 sh->session_array[f].peer = *peer;
329 sh->session_array[f].slot_used = GNUNET_YES;
335 * Remove the session of the given session ID from the session
336 * table (it is no longer valid).
338 * @param sh our handle
339 * @param session_id identifies session that is no longer valid
340 * @param peer peer the session belongs to
343 remove_session (struct GNUNET_ATS_SchedulingHandle *sh,
345 const struct GNUNET_PeerIdentity *peer)
349 GNUNET_assert (session_id < sh->session_array_size);
350 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
351 GNUNET_assert (0 == memcmp (peer,
352 &sh->session_array[session_id].peer,
353 sizeof (struct GNUNET_PeerIdentity)));
354 sh->session_array[session_id].session = NULL;
359 * Release the session slot from the session table (ATS service is
360 * also done using it).
362 * @param sh our handle
363 * @param session_id identifies session that is no longer valid
364 * @param peer peer the session belongs to
367 release_session (struct GNUNET_ATS_SchedulingHandle *sh,
369 const struct GNUNET_PeerIdentity *peer)
371 GNUNET_assert (session_id < sh->session_array_size);
372 GNUNET_assert (0 == memcmp (peer,
373 &sh->session_array[session_id].peer,
374 sizeof (struct GNUNET_PeerIdentity)));
375 sh->session_array[session_id].slot_used = GNUNET_NO;
376 memset (&sh->session_array[session_id].peer,
378 sizeof (struct GNUNET_PeerIdentity));
383 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
384 const struct SessionReleaseMessage *srm)
387 ntohl (srm->session_id),
393 * Type of a function to call when we receive a message
396 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
397 * @param msg message received, NULL on timeout or fatal error
400 process_ats_message (void *cls,
401 const struct GNUNET_MessageHeader *msg)
403 struct GNUNET_ATS_SchedulingHandle *sh = cls;
404 const struct AddressSuggestionMessage *m;
405 const struct GNUNET_ATS_Information *atsi;
407 const char *plugin_name;
408 uint16_t address_length;
409 uint16_t plugin_name_length;
414 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
416 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
417 &reconnect_task, sh);
420 if ( (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
421 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)) )
423 process_release_message (sh,
424 (const struct SessionReleaseMessage*) msg);
425 GNUNET_CLIENT_receive (sh->client,
426 &process_ats_message, sh,
427 GNUNET_TIME_UNIT_FOREVER_REL);
430 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
431 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)) )
434 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
436 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
437 &reconnect_task, sh);
440 m = (const struct AddressSuggestionMessage*) msg;
441 ats_count = ntohl (m->ats_count);
442 address_length = ntohs (m->address_length);
443 atsi = (const struct GNUNET_ATS_Information*) &m[1];
444 address = (const char*) &atsi[ats_count];
445 plugin_name = &address[address_length];
446 plugin_name_length = ntohs (m->plugin_name_length);
447 if ( (address_length +
449 ats_count * sizeof (struct GNUNET_ATS_Information) +
450 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
451 (ats_count > GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) ||
452 (plugin_name[plugin_name_length - 1] != '\0') )
455 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
457 sh->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
458 &reconnect_task, sh);
461 sh->suggest_cb (sh->suggest_cb_cls,
464 address, address_length,
465 find_session (sh, ntohl (m->session_id), &m->peer),
470 GNUNET_CLIENT_receive (sh->client,
471 &process_ats_message, sh,
472 GNUNET_TIME_UNIT_FOREVER_REL);
477 * Re-establish the connection to the ATS service.
479 * @param sh handle to use to re-connect.
482 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
484 struct PendingMessage *p;
485 struct ClientStartMessage *init;
487 GNUNET_assert (NULL == sh->client);
488 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
489 GNUNET_assert (NULL != sh->client);
490 if ( (NULL == (p = sh->pending_head)) ||
491 (GNUNET_YES != p->is_init) )
493 p = GNUNET_malloc (sizeof (struct PendingMessage) +
494 sizeof (struct ClientStartMessage));
495 p->size = sizeof (struct ClientStartMessage);
496 p->is_init = GNUNET_YES;
497 init = (struct ClientStartMessage *) &p[1];
498 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
499 init->header.size = htons (sizeof (struct ClientStartMessage));
500 init->start_flag = htonl (START_FLAG_SCHEDULING);
501 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
510 * Initialize the ATS subsystem.
512 * @param cfg configuration to use
513 * @param suggest_cb notification to call whenever the suggestation changed
514 * @param suggest_cb_cls closure for 'suggest_cb'
515 * @return ats context
517 struct GNUNET_ATS_SchedulingHandle *
518 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
519 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
520 void *suggest_cb_cls)
522 struct GNUNET_ATS_SchedulingHandle *sh;
524 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
526 sh->suggest_cb = suggest_cb;
527 sh->suggest_cb_cls = suggest_cb_cls;
528 GNUNET_array_grow (sh->session_array,
529 sh->session_array_size,
537 * Client is done with ATS scheduling, release resources.
539 * @param sh handle to release
542 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
544 struct PendingMessage *p;
546 while (NULL != (p = sh->pending_head))
548 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
553 if (NULL != sh->client)
555 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
558 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
560 GNUNET_SCHEDULER_cancel (sh->task);
561 sh->task = GNUNET_SCHEDULER_NO_TASK;
563 GNUNET_array_grow (sh->session_array,
564 sh->session_array_size,
571 * We would like to establish a new connection with a peer. ATS
572 * should suggest a good address to begin with.
575 * @param peer identity of the peer we need an address for
578 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
579 const struct GNUNET_PeerIdentity *peer)
581 struct PendingMessage *p;
582 struct RequestAddressMessage *m;
584 p = GNUNET_malloc (sizeof (struct PendingMessage) +
585 sizeof (struct RequestAddressMessage));
586 p->size = sizeof (struct RequestAddressMessage);
587 p->is_init = GNUNET_NO;
588 m = (struct RequestAddressMessage*) &p[1];
589 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
590 m->header.size = htons (sizeof (struct RequestAddressMessage));
591 m->reserved = htonl (0);
593 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
601 * We have updated performance statistics for a given address. Note
602 * that this function can be called for addresses that are currently
603 * in use as well as addresses that are valid but not actively in use.
604 * Furthermore, the peer may not even be connected to us right now (in
605 * which case the call may be ignored or the information may be stored
606 * for later use). Update bandwidth assignments.
609 * @param peer identity of the new peer
610 * @param plugin_name name of the transport plugin
611 * @param plugin_addr address (if available)
612 * @param plugin_addr_len number of bytes in plugin_addr
613 * @param session session handle (if available)
614 * @param ats performance data for the address
615 * @param ats_count number of performance records in 'ats'
618 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
619 const struct GNUNET_PeerIdentity *peer,
620 const char *plugin_name,
621 const void *plugin_addr, size_t plugin_addr_len,
622 struct Session *session,
623 const struct GNUNET_ATS_Information *ats,
626 struct PendingMessage *p;
627 struct AddressUpdateMessage *m;
628 struct GNUNET_ATS_Information *am;
633 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
634 msize = sizeof (struct AddressUpdateMessage) + plugin_addr_len +
635 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
636 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
637 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
638 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
639 (ats_count >= GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)) )
644 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
646 p->is_init = GNUNET_NO;
647 m = (struct AddressUpdateMessage*) &p[1];
648 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
649 m->header.size = htons (msize);
650 m->ats_count = htonl (ats_count);
652 m->address_length = htons (plugin_addr_len);
653 m->plugin_name_length = htons (namelen);
654 m->session_id = htonl (get_session_id (sh, session, peer));
655 am = (struct GNUNET_ATS_Information*) &m[1];
656 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
657 pm = (char *) &am[ats_count];
658 memcpy (pm, plugin_addr, plugin_addr_len);
659 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
660 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
668 * A session got destroyed, stop including it as a valid address.
671 * @param peer identity of the peer
672 * @param plugin_name name of the transport plugin
673 * @param plugin_addr address (if available)
674 * @param plugin_addr_len number of bytes in plugin_addr
675 * @param session session handle that is no longer valid
678 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
679 const struct GNUNET_PeerIdentity *peer,
680 const char *plugin_name,
681 const void *plugin_addr,
682 size_t plugin_addr_len,
683 struct Session *session)
685 struct PendingMessage *p;
686 struct AddressDestroyedMessage *m;
692 namelen = (plugin_name == NULL) ? 0 : strlen (plugin_name) + 1;
693 msize = sizeof (struct AddressDestroyedMessage) + plugin_addr_len +
695 if ( (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
696 (plugin_addr_len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
697 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
702 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
704 p->is_init = GNUNET_NO;
705 m = (struct AddressDestroyedMessage*) &p[1];
706 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
707 m->header.size = htons (msize);
708 m->reserved = htonl (0);
710 m->address_length = htons (plugin_addr_len);
711 m->plugin_name_length = htons (namelen);
712 m->session_id = htonl (session_id = get_session_id (sh, session, peer));
714 memcpy (pm, plugin_addr, plugin_addr_len);
715 memcpy (&pm[plugin_addr_len], plugin_name, namelen);
716 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head,
720 remove_session (sh, session_id, peer);
723 /* end of ats_api_scheduling.c */