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;
142 * Should we reconnect to ATS due to some serious error?
149 * Re-establish the connection to the ATS service.
151 * @param sh handle to use to re-connect.
154 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
158 * Re-establish the connection to the ATS service.
160 * @param cls handle to use to re-connect.
161 * @param tc scheduler context
164 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 struct GNUNET_ATS_SchedulingHandle *sh = cls;
168 sh->task = GNUNET_SCHEDULER_NO_TASK;
174 * Disconnect from ATS and then reconnect.
176 * @param sh our handle
179 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
181 sh->reconnect = GNUNET_NO;
182 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
191 * Transmit messages from the message queue to the service
192 * (if there are any, and if we are not already trying).
194 * @param sh handle to use
197 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
201 * Type of a function to call when we receive a message
204 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
205 * @param msg message received, NULL on timeout or fatal error
208 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
212 * We can now transmit a message to ATS. Do it.
214 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
215 * @param size number of bytes we can transmit to ATS
216 * @param buf where to copy the messages
217 * @return number of bytes copied into buf
220 transmit_message_to_ats (void *cls, size_t size, void *buf)
222 struct GNUNET_ATS_SchedulingHandle *sh = cls;
223 struct PendingMessage *p;
228 if ((size == 0) || (buf == NULL))
230 force_reconnect (sh);
235 while ((NULL != (p = sh->pending_head)) && (p->size <= size))
237 memcpy (&cbuf[ret], &p[1], p->size);
240 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
241 if (GNUNET_YES == p->is_init)
242 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
243 GNUNET_TIME_UNIT_FOREVER_REL);
252 * Transmit messages from the message queue to the service
253 * (if there are any, and if we are not already trying).
255 * @param sh handle to use
258 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
260 struct PendingMessage *p;
264 if (NULL == (p = sh->pending_head))
266 if (NULL == sh->client)
267 return; /* currently reconnecting */
269 GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
270 GNUNET_TIME_UNIT_FOREVER_REL,
271 GNUNET_NO, &transmit_message_to_ats,
277 * Find the session object corresponding to the given session ID.
279 * @param sh our handle
280 * @param session_id current session ID
281 * @param peer peer the session belongs to
282 * @return the session object (or NULL)
284 static struct Session *
285 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
286 const struct GNUNET_PeerIdentity *peer)
288 if (session_id >= sh->session_array_size)
295 /* Check if this session was:
296 * removed by remove_session (transport service)
297 * released by release_session (ATS)
299 if (sh->session_array[session_id].session == NULL)
302 memcmp (peer, &sh->session_array[session_id].peer,
303 sizeof (struct GNUNET_PeerIdentity)));
308 memcmp (peer, &sh->session_array[session_id].peer,
309 sizeof (struct GNUNET_PeerIdentity)))
312 sh->reconnect = GNUNET_YES;
315 return sh->session_array[session_id].session;
320 * Get the ID for the given session object. If we do not have an ID for
321 * the given session object, allocate one.
323 * @param sh our handle
324 * @param session session object
325 * @param peer peer the session belongs to
326 * @return the session id
329 get_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
330 const struct GNUNET_PeerIdentity *peer)
338 for (i = 1; i < sh->session_array_size; i++)
340 if (session == sh->session_array[i].session)
343 memcmp (peer, &sh->session_array[i].peer,
344 sizeof (struct GNUNET_PeerIdentity)));
347 if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
352 f = sh->session_array_size;
353 GNUNET_array_grow (sh->session_array, sh->session_array_size,
354 sh->session_array_size * 2);
356 GNUNET_assert (f > 0);
357 sh->session_array[f].session = session;
358 sh->session_array[f].peer = *peer;
359 sh->session_array[f].slot_used = GNUNET_YES;
365 * Remove the session of the given session ID from the session
366 * table (it is no longer valid).
368 * @param sh our handle
369 * @param session_id identifies session that is no longer valid
370 * @param peer peer the session belongs to
373 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
374 const struct GNUNET_PeerIdentity *peer)
378 GNUNET_assert (session_id < sh->session_array_size);
379 GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
381 memcmp (peer, &sh->session_array[session_id].peer,
382 sizeof (struct GNUNET_PeerIdentity)));
383 sh->session_array[session_id].session = NULL;
388 * Release the session slot from the session table (ATS service is
389 * also done using it).
391 * @param sh our handle
392 * @param session_id identifies session that is no longer valid
393 * @param peer peer the session belongs to
396 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
397 const struct GNUNET_PeerIdentity *peer)
399 if (session_id >= sh->session_array_size)
402 sh->reconnect = GNUNET_YES;
406 /* this slot should have been removed from remove_session before */
407 GNUNET_assert (sh->session_array[session_id].session == NULL);
410 memcmp (peer, &sh->session_array[session_id].peer,
411 sizeof (struct GNUNET_PeerIdentity)))
414 sh->reconnect = GNUNET_YES;
418 sh->session_array[session_id].slot_used = GNUNET_NO;
419 memset (&sh->session_array[session_id].peer, 0,
420 sizeof (struct GNUNET_PeerIdentity));
425 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
426 const struct SessionReleaseMessage *srm)
428 release_session (sh, ntohl (srm->session_id), &srm->peer);
433 * Type of a function to call when we receive a message
436 * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
437 * @param msg message received, NULL on timeout or fatal error
440 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
442 struct GNUNET_ATS_SchedulingHandle *sh = cls;
443 const struct AddressSuggestionMessage *m;
444 const struct GNUNET_ATS_Information *atsi;
445 const char *plugin_address;
446 const char *plugin_name;
447 uint16_t plugin_address_length;
448 uint16_t plugin_name_length;
450 struct GNUNET_HELLO_Address address;
454 force_reconnect (sh);
457 if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
458 (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
460 process_release_message (sh, (const struct SessionReleaseMessage *) msg);
461 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
462 GNUNET_TIME_UNIT_FOREVER_REL);
463 if (GNUNET_YES == sh->reconnect)
464 force_reconnect (sh);
467 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
468 (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
471 force_reconnect (sh);
474 m = (const struct AddressSuggestionMessage *) msg;
475 ats_count = ntohl (m->ats_count);
476 plugin_address_length = ntohs (m->address_length);
477 atsi = (const struct GNUNET_ATS_Information *) &m[1];
478 plugin_address = (const char *) &atsi[ats_count];
479 plugin_name = &plugin_address[plugin_address_length];
480 plugin_name_length = ntohs (m->plugin_name_length);
481 if ((plugin_address_length + plugin_name_length +
482 ats_count * sizeof (struct GNUNET_ATS_Information) +
483 sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
485 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
486 || (plugin_name[plugin_name_length - 1] != '\0'))
489 force_reconnect (sh);
492 uint32_t session_id = ntohl (m->session_id);
494 struct Session * s = NULL;
499 s = find_session (sh, session_id, &m->peer);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS tries to use outdated session `%s'\n", GNUNET_i2s(&m->peer));
507 address.peer = m->peer;
508 address.address = plugin_address;
509 address.address_length = plugin_address_length;
510 address.transport_name = plugin_name;
511 sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
512 m->bandwidth_in, atsi, ats_count);
517 GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
518 GNUNET_TIME_UNIT_FOREVER_REL);
519 if (GNUNET_YES == sh->reconnect)
520 force_reconnect (sh);
525 * Re-establish the connection to the ATS service.
527 * @param sh handle to use to re-connect.
530 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
532 struct PendingMessage *p;
533 struct ClientStartMessage *init;
535 GNUNET_assert (NULL == sh->client);
536 sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
537 GNUNET_assert (NULL != sh->client);
538 if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
540 p = GNUNET_malloc (sizeof (struct PendingMessage) +
541 sizeof (struct ClientStartMessage));
542 p->size = sizeof (struct ClientStartMessage);
543 p->is_init = GNUNET_YES;
544 init = (struct ClientStartMessage *) &p[1];
545 init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
546 init->header.size = htons (sizeof (struct ClientStartMessage));
547 init->start_flag = htonl (START_FLAG_SCHEDULING);
548 GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
555 * Initialize the ATS subsystem.
557 * @param cfg configuration to use
558 * @param suggest_cb notification to call whenever the suggestation changed
559 * @param suggest_cb_cls closure for 'suggest_cb'
560 * @return ats context
562 struct GNUNET_ATS_SchedulingHandle *
563 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
564 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
565 void *suggest_cb_cls)
567 struct GNUNET_ATS_SchedulingHandle *sh;
569 sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
571 sh->suggest_cb = suggest_cb;
572 sh->suggest_cb_cls = suggest_cb_cls;
573 GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
580 * Client is done with ATS scheduling, release resources.
582 * @param sh handle to release
585 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
587 struct PendingMessage *p;
589 while (NULL != (p = sh->pending_head))
591 GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
594 if (NULL != sh->client)
596 GNUNET_CLIENT_disconnect (sh->client, GNUNET_NO);
599 if (GNUNET_SCHEDULER_NO_TASK != sh->task)
601 GNUNET_SCHEDULER_cancel (sh->task);
602 sh->task = GNUNET_SCHEDULER_NO_TASK;
604 GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
610 * We would like to establish a new connection with a peer. ATS
611 * should suggest a good address to begin with.
614 * @param peer identity of the peer we need an address for
617 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
618 const struct GNUNET_PeerIdentity *peer)
620 struct PendingMessage *p;
621 struct RequestAddressMessage *m;
623 p = GNUNET_malloc (sizeof (struct PendingMessage) +
624 sizeof (struct RequestAddressMessage));
625 p->size = sizeof (struct RequestAddressMessage);
626 p->is_init = GNUNET_NO;
627 m = (struct RequestAddressMessage *) &p[1];
628 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
629 m->header.size = htons (sizeof (struct RequestAddressMessage));
630 m->reserved = htonl (0);
632 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
638 * We would like to stop receiving address updates for this peer
641 * @param peer identity of the peer
644 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
645 const struct GNUNET_PeerIdentity *peer)
647 struct PendingMessage *p;
648 struct RequestAddressMessage *m;
650 p = GNUNET_malloc (sizeof (struct PendingMessage) +
651 sizeof (struct RequestAddressMessage));
652 p->size = sizeof (struct RequestAddressMessage);
653 p->is_init = GNUNET_NO;
654 m = (struct RequestAddressMessage *) &p[1];
655 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
656 m->header.size = htons (sizeof (struct RequestAddressMessage));
657 m->reserved = htonl (0);
659 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
664 * We have updated performance statistics for a given address. Note
665 * that this function can be called for addresses that are currently
666 * in use as well as addresses that are valid but not actively in use.
667 * Furthermore, the peer may not even be connected to us right now (in
668 * which case the call may be ignored or the information may be stored
669 * for later use). Update bandwidth assignments.
672 * @param address the address
673 * @param session session handle (if available)
674 * @param ats performance data for the address
675 * @param ats_count number of performance records in 'ats'
678 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
679 const struct GNUNET_HELLO_Address *address,
680 struct Session *session,
681 const struct GNUNET_ATS_Information *ats,
684 struct PendingMessage *p;
685 struct AddressUpdateMessage *m;
686 struct GNUNET_ATS_Information *am;
691 namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1;
693 sizeof (struct AddressUpdateMessage) + address->address_length +
694 ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
695 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
696 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
697 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
699 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
705 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
707 p->is_init = GNUNET_NO;
708 m = (struct AddressUpdateMessage *) &p[1];
709 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
710 m->header.size = htons (msize);
711 m->ats_count = htonl (ats_count);
712 m->peer = address->peer;
713 m->address_length = htons (address->address_length);
714 m->plugin_name_length = htons (namelen);
715 m->session_id = htonl (get_session_id (sh, session, &address->peer));
716 am = (struct GNUNET_ATS_Information *) &m[1];
717 memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
718 pm = (char *) &am[ats_count];
719 memcpy (pm, address->address, address->address_length);
720 memcpy (&pm[address->address_length], address->transport_name, namelen);
721 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
727 * An address is now in use or not used any more.
730 * @param address the address
731 * @param session session handle
732 * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
733 * if address is not used any more
736 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
737 const struct GNUNET_HELLO_Address *address,
738 struct Session *session,
741 struct PendingMessage *p;
742 struct AddressUseMessage *m;
747 GNUNET_assert (NULL != address);
748 namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1;
749 msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
750 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
751 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
752 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
758 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
760 p->is_init = GNUNET_NO;
761 m = (struct AddressUseMessage *) &p[1];
762 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
763 m->header.size = htons (msize);
764 m->peer = address->peer;
765 m->in_use = htons (in_use);
766 m->address_length = htons (address->address_length);
767 m->plugin_name_length = htons (namelen);
768 m->session_id = htonl (get_session_id (sh, session, &address->peer));
770 memcpy (pm, address->address, address->address_length);
771 memcpy (&pm[address->address_length], address->transport_name, namelen);
772 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
778 * A session got destroyed, stop including it as a valid address.
781 * @param address the address
782 * @param session session handle that is no longer valid
785 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
786 const struct GNUNET_HELLO_Address *address,
787 struct Session *session)
789 struct PendingMessage *p;
790 struct AddressDestroyedMessage *m;
796 namelen = (address->transport_name == NULL) ? 0 : strlen (address->transport_name) + 1;
797 msize = sizeof (struct AddressDestroyedMessage) + address->address_length + namelen;
798 if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
799 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
800 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
807 p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
809 p->is_init = GNUNET_NO;
810 m = (struct AddressDestroyedMessage *) &p[1];
811 m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
812 m->header.size = htons (msize);
813 m->reserved = htonl (0);
814 m->peer = address->peer;
815 m->address_length = htons (address->address_length);
816 m->plugin_name_length = htons (namelen);
817 session_id = get_session_id (sh, session, &address->peer);
818 m->session_id = htonl (session_id);
820 memcpy (pm, address->address, address->address_length);
821 memcpy (&pm[address->address_length], address->transport_name, namelen);
822 GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
824 remove_session (sh, session_id, &address->peer);
827 /* end of ats_api_scheduling.c */