2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 * - we could avoid a linear scan over the
28 * active addresses in some cases, so if
29 * there is need, we can still optimize here
30 * - we might want to split off the logic to
31 * determine LAN vs. WAN, as it has nothing
32 * to do with accessing the ATS service.
35 #include "gnunet_ats_service.h"
39 * How frequently do we scan the interfaces for changes to the addresses?
41 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
43 #define LOG(kind,...) GNUNET_log_from(kind, "ats-scheduling-api", __VA_ARGS__)
46 * Session ID we use if there is no session / slot.
52 * Information we track per address, incoming or outgoing. It also
53 * doesn't matter if we have a session, any address that ATS is
54 * allowed to suggest right now should be tracked.
56 struct GNUNET_ATS_AddressRecord
60 * Scheduling handle this address record belongs to.
62 struct GNUNET_ATS_SchedulingHandle *sh;
67 struct GNUNET_HELLO_Address *address;
70 * Session handle. NULL if we have an address but no
71 * active session for this address.
73 struct GNUNET_ATS_Session *session;
76 * Performance data about the address.
78 struct GNUNET_ATS_PropertiesNBO properties;
81 * Which slot (index) in the session array does
82 * this record correspond to?
83 * FIXME: a linear search on this is really crappy!
84 * Maybe switch to a 64-bit global counter and be
85 * done with it? Or does that then cause too much
86 * trouble on the ATS-service side?
91 * We're about to destroy this address record, just ATS does
92 * not know this yet. Once ATS confirms its destruction,
100 * Handle to the ATS subsystem for bandwidth/transport scheduling information.
102 struct GNUNET_ATS_SchedulingHandle
108 const struct GNUNET_CONFIGURATION_Handle *cfg;
111 * Callback to invoke on suggestions.
113 GNUNET_ATS_AddressSuggestionCallback suggest_cb;
116 * Closure for @e suggest_cb.
118 void *suggest_cb_cls;
121 * Connection to ATS service.
123 struct GNUNET_CLIENT_Connection *client;
126 * Message queue for sending requests to the ATS service.
128 struct GNUNET_MQ_Handle *mq;
131 * Array of session objects (we need to translate them to numbers and back
132 * for the protocol; the offset in the array is the session number on the
133 * network). Index 0 is always NULL and reserved to represent the NULL pointer.
134 * Unused entries are also NULL.
136 struct GNUNET_ATS_AddressRecord **session_array;
139 * Task to trigger reconnect.
141 struct GNUNET_SCHEDULER_Task *task;
144 * Reconnect backoff delay.
146 struct GNUNET_TIME_Relative backoff;
149 * Size of the @e session_array.
151 unsigned int session_array_size;
157 * Re-establish the connection to the ATS service.
159 * @param sh handle to use to re-connect.
162 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
166 * Re-establish the connection to the ATS service.
168 * @param cls handle to use to re-connect.
171 reconnect_task (void *cls)
173 struct GNUNET_ATS_SchedulingHandle *sh = cls;
181 * Disconnect from ATS and then reconnect.
183 * @param sh our handle
186 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
190 GNUNET_MQ_destroy (sh->mq);
193 if (NULL != sh->client)
195 GNUNET_CLIENT_disconnect (sh->client);
198 sh->suggest_cb (sh->suggest_cb_cls,
200 GNUNET_BANDWIDTH_ZERO,
201 GNUNET_BANDWIDTH_ZERO);
202 sh->backoff = GNUNET_TIME_STD_BACKOFF (sh->backoff);
203 sh->task = GNUNET_SCHEDULER_add_delayed (sh->backoff,
210 * Find the session object corresponding to the given session ID.
212 * @param sh our handle
213 * @param session_id current session ID
214 * @param peer peer the session belongs to
215 * @return the session object (or NULL)
217 static struct GNUNET_ATS_AddressRecord *
218 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
220 const struct GNUNET_PeerIdentity *peer)
222 struct GNUNET_ATS_AddressRecord *ar;
224 if (session_id >= sh->session_array_size)
231 ar = sh->session_array[session_id];
237 if (NULL == ar->address)
239 /* address was destroyed in the meantime, this can happen
240 as we communicate asynchronously with the ATS service. */
243 if (0 != memcmp (peer,
245 sizeof (struct GNUNET_PeerIdentity)))
255 * Get an available session ID.
257 * @param sh our handle
258 * @return an unused slot, but never NOT_FOUND (0)
261 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh)
267 while ( ( (NOT_FOUND == off) ||
268 (NULL != sh->session_array[off % sh->session_array_size]) ) &&
269 (i < sh->session_array_size) )
274 if ( (NOT_FOUND != off % sh->session_array_size) &&
275 (NULL == sh->session_array[off % sh->session_array_size]) )
277 i = sh->session_array_size;
278 GNUNET_array_grow (sh->session_array,
279 sh->session_array_size,
280 sh->session_array_size * 2);
286 * Get the ID for the given session object.
288 * @param sh our handle
289 * @param session session object
290 * @param address the address we are looking for
291 * @return the session id or NOT_FOUND for error
294 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
295 struct GNUNET_ATS_Session *session,
296 const struct GNUNET_HELLO_Address *address)
305 for (i = 1; i < sh->session_array_size; i++)
306 if ( (NULL != sh->session_array[i]) &&
307 (GNUNET_NO == sh->session_array[i]->in_destroy) &&
308 ( (session == sh->session_array[i]->session) ||
309 (NULL == sh->session_array[i]->session) ) &&
310 (0 == memcmp (&address->peer,
311 &sh->session_array[i]->address->peer,
312 sizeof (struct GNUNET_PeerIdentity))) &&
313 (0 == GNUNET_HELLO_address_cmp (address,
314 sh->session_array[i]->address)) )
321 * Release the session slot from the session table (ATS service is
322 * also done using it).
324 * @param sh our handle
325 * @param session_id identifies session that is no longer valid
328 release_session (struct GNUNET_ATS_SchedulingHandle *sh,
331 struct GNUNET_ATS_AddressRecord *ar;
333 if (NOT_FOUND == session_id)
335 if (session_id >= sh->session_array_size)
338 force_reconnect (sh);
341 /* this slot should have been removed from remove_session before */
342 ar = sh->session_array[session_id];
343 if (NULL != ar->session)
346 force_reconnect (sh);
349 GNUNET_HELLO_address_free (ar->address);
351 sh->session_array[session_id] = NULL;
356 * Type of a function to call when we receive a session release
357 * message from the service.
359 * @param cls the `struct GNUNET_ATS_SchedulingHandle`
360 * @param msg message received, NULL on timeout or fatal error
363 process_ats_session_release_message (void *cls,
364 const struct GNUNET_MessageHeader *msg)
366 struct GNUNET_ATS_SchedulingHandle *sh = cls;
367 const struct GNUNET_ATS_SessionReleaseMessage *srm;
369 srm = (const struct GNUNET_ATS_SessionReleaseMessage *) msg;
370 /* Note: peer field in srm not necessary right now,
371 but might be good to have in the future */
373 ntohl (srm->session_id));
378 * Type of a function to call when we receive a address suggestion
379 * message from the service.
381 * @param cls the `struct GNUNET_ATS_SchedulingHandle`
382 * @param msg message received, NULL on timeout or fatal error
385 process_ats_address_suggestion_message (void *cls,
386 const struct GNUNET_MessageHeader *msg)
388 struct GNUNET_ATS_SchedulingHandle *sh = cls;
389 const struct AddressSuggestionMessage *m;
390 struct GNUNET_ATS_AddressRecord *ar;
393 m = (const struct AddressSuggestionMessage *) msg;
394 session_id = ntohl (m->session_id);
398 force_reconnect (sh);
401 ar = find_session (sh,
407 force_reconnect (sh);
410 if (NULL == sh->suggest_cb)
412 if (GNUNET_YES == ar->in_destroy)
414 /* ignore suggestion, as this address is dying, unless BW is 0,
415 in that case signal 'disconnect' via BW 0 */
416 if ( (0 == ntohl (m->bandwidth_out.value__)) &&
417 (0 == ntohl (m->bandwidth_in.value__)) )
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "ATS suggests disconnect from peer `%s' with BW %u/%u\n",
421 GNUNET_i2s (&ar->address->peer),
422 (unsigned int) ntohl (m->bandwidth_out.value__),
423 (unsigned int) ntohl (m->bandwidth_in.value__));
424 sh->suggest_cb (sh->suggest_cb_cls,
433 if ( (NULL == ar->session) &&
434 (GNUNET_HELLO_address_check_option (ar->address,
435 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) )
440 sh->backoff = GNUNET_TIME_UNIT_ZERO;
441 LOG (GNUNET_ERROR_TYPE_DEBUG,
442 "ATS suggests address slot %u for peer `%s' using plugin %s\n",
444 GNUNET_i2s (&ar->address->peer),
445 ar->address->transport_name);
446 sh->suggest_cb (sh->suggest_cb_cls,
456 * We encountered an error handling the MQ to the
457 * ATS service. Reconnect.
459 * @param cls the `struct GNUNET_ATS_SchedulingHandle`
460 * @param error details about the error
463 error_handler (void *cls,
464 enum GNUNET_MQ_Error error)
466 struct GNUNET_ATS_SchedulingHandle *sh = cls;
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "ATS connection died (code %d), reconnecting\n",
471 force_reconnect (sh);
476 * Generate and transmit the `struct AddressAddMessage` for the given
479 * @param sh the scheduling handle to use for transmission
480 * @param ar the address to inform the ATS service about
483 send_add_address_message (struct GNUNET_ATS_SchedulingHandle *sh,
484 const struct GNUNET_ATS_AddressRecord *ar)
486 struct GNUNET_MQ_Envelope *ev;
487 struct AddressAddMessage *m;
493 return; /* disconnected, skip for now */
494 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != ar->properties.scope);
495 namelen = strlen (ar->address->transport_name) + 1;
496 msize = ar->address->address_length + namelen;
497 ev = GNUNET_MQ_msg_extra (m, msize, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
498 m->peer = ar->address->peer;
499 m->address_length = htons (ar->address->address_length);
500 m->address_local_info = htonl ((uint32_t) ar->address->local_info);
501 m->plugin_name_length = htons (namelen);
502 m->session_id = htonl (ar->slot);
503 m->properties = ar->properties;
505 LOG (GNUNET_ERROR_TYPE_DEBUG,
506 "Adding address for peer `%s', plugin `%s', session %p slot %u\n",
507 GNUNET_i2s (&ar->address->peer),
508 ar->address->transport_name,
513 ar->address->address,
514 ar->address->address_length);
515 if (NULL != ar->address->transport_name)
516 memcpy (&pm[ar->address->address_length],
517 ar->address->transport_name,
519 GNUNET_MQ_send (sh->mq, ev);
524 * Re-establish the connection to the ATS service.
526 * @param sh handle to use to re-connect.
529 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
531 static const struct GNUNET_MQ_MessageHandler handlers[] =
532 { { &process_ats_session_release_message,
533 GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE,
534 sizeof (struct GNUNET_ATS_SessionReleaseMessage) },
535 { &process_ats_address_suggestion_message,
536 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION,
537 sizeof (struct AddressSuggestionMessage) },
539 struct GNUNET_MQ_Envelope *ev;
540 struct ClientStartMessage *init;
542 struct GNUNET_ATS_AddressRecord *ar;
544 GNUNET_assert (NULL == sh->client);
545 sh->client = GNUNET_CLIENT_connect ("ats",
547 if (NULL == sh->client)
550 force_reconnect (sh);
553 sh->mq = GNUNET_MQ_queue_for_connection_client (sh->client,
557 ev = GNUNET_MQ_msg (init,
558 GNUNET_MESSAGE_TYPE_ATS_START);
559 init->start_flag = htonl (START_FLAG_SCHEDULING);
560 GNUNET_MQ_send (sh->mq, ev);
563 for (i=0;i<sh->session_array_size;i++)
565 ar = sh->session_array[i];
568 send_add_address_message (sh, ar);
576 * Initialize the ATS subsystem.
578 * @param cfg configuration to use
579 * @param suggest_cb notification to call whenever the suggestation changed
580 * @param suggest_cb_cls closure for @a suggest_cb
581 * @return ats context
583 struct GNUNET_ATS_SchedulingHandle *
584 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
585 GNUNET_ATS_AddressSuggestionCallback suggest_cb,
586 void *suggest_cb_cls)
588 struct GNUNET_ATS_SchedulingHandle *sh;
590 sh = GNUNET_new (struct GNUNET_ATS_SchedulingHandle);
592 sh->suggest_cb = suggest_cb;
593 sh->suggest_cb_cls = suggest_cb_cls;
594 GNUNET_array_grow (sh->session_array,
595 sh->session_array_size,
603 * Client is done with ATS scheduling, release resources.
605 * @param sh handle to release
608 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
610 struct GNUNET_ATS_AddressRecord *ar;
615 GNUNET_MQ_destroy (sh->mq);
618 if (NULL != sh->client)
620 GNUNET_CLIENT_disconnect (sh->client);
623 if (NULL != sh->task)
625 GNUNET_SCHEDULER_cancel (sh->task);
628 for (i=0;i<sh->session_array_size;i++)
630 if (NULL != (ar = sh->session_array[i]))
632 GNUNET_HELLO_address_free (ar->address);
634 sh->session_array[i] = NULL;
637 GNUNET_array_grow (sh->session_array,
638 sh->session_array_size,
645 * We have a new address ATS should know. Addresses have to be added
646 * with this function before they can be: updated, set in use and
650 * @param address the address
651 * @param session session handle, can be NULL
652 * @param prop performance data for the address
653 * @return handle to the address representation inside ATS, NULL
654 * on error (i.e. ATS knows this exact address already)
656 struct GNUNET_ATS_AddressRecord *
657 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
658 const struct GNUNET_HELLO_Address *address,
659 struct GNUNET_ATS_Session *session,
660 const struct GNUNET_ATS_Properties *prop)
662 struct GNUNET_ATS_AddressRecord *ar;
669 /* we need a valid address */
673 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
674 namelen = strlen (address->transport_name) + 1;
675 msize = address->address_length + namelen;
676 if ((msize + sizeof (struct AddressUpdateMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
677 (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
678 (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
680 /* address too large for us, this should not happen */
690 /* Already existing, nothing todo, but this should not happen */
694 s = find_empty_session_slot (sh);
695 ar = GNUNET_new (struct GNUNET_ATS_AddressRecord);
698 ar->session = session;
699 ar->address = GNUNET_HELLO_address_copy (address);
700 GNUNET_ATS_properties_hton (&ar->properties,
702 sh->session_array[s] = ar;
703 send_add_address_message (sh, ar);
709 * An address was used to initiate a session.
711 * @param ar address record to update information for
712 * @param session session handle
715 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
716 struct GNUNET_ATS_Session *session)
718 GNUNET_break (NULL == ar->session);
719 ar->session = session;
724 * A session was destroyed, disassociate it from the
725 * given address record. If this was an incoming
726 * addess, destroy the address as well.
728 * @param ar address record to update information for
729 * @param session session handle
730 * @return #GNUNET_YES if the @a ar was destroyed because
731 * it was an incoming address,
732 * #GNUNET_NO if the @ar was kept because we can
733 * use it still to establish a new session
736 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
737 struct GNUNET_ATS_Session *session)
739 GNUNET_assert (session == ar->session);
741 if (GNUNET_HELLO_address_check_option (ar->address,
742 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
744 GNUNET_ATS_address_destroy (ar);
752 * We have updated performance statistics for a given address. Note
753 * that this function can be called for addresses that are currently
754 * in use as well as addresses that are valid but not actively in use.
755 * Furthermore, the peer may not even be connected to us right now (in
756 * which case the call may be ignored or the information may be stored
757 * for later use). Update bandwidth assignments.
759 * @param ar address record to update information for
760 * @param prop performance data for the address
763 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
764 const struct GNUNET_ATS_Properties *prop)
766 struct GNUNET_ATS_SchedulingHandle *sh = ar->sh;
767 struct GNUNET_MQ_Envelope *ev;
768 struct AddressUpdateMessage *m;
770 LOG (GNUNET_ERROR_TYPE_DEBUG,
771 "Updating address for peer `%s', plugin `%s', session %p slot %u\n",
772 GNUNET_i2s (&ar->address->peer),
773 ar->address->transport_name,
776 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
777 GNUNET_ATS_properties_hton (&ar->properties,
780 return; /* disconnected, skip for now */
781 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
782 m->session_id = htonl (ar->slot);
783 m->peer = ar->address->peer;
784 m->properties = ar->properties;
785 GNUNET_MQ_send (sh->mq,
791 * An address got destroyed, stop using it as a valid address.
793 * @param ar address to destroy
796 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar)
798 struct GNUNET_ATS_SchedulingHandle *sh = ar->sh;
799 struct GNUNET_MQ_Envelope *ev;
800 struct AddressDestroyedMessage *m;
802 LOG (GNUNET_ERROR_TYPE_DEBUG,
803 "Deleting address for peer `%s', plugin `%s', slot %u session %p\n",
804 GNUNET_i2s (&ar->address->peer),
805 ar->address->transport_name,
808 GNUNET_break (NULL == ar->session);
810 ar->in_destroy = GNUNET_YES;
813 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
814 m->session_id = htonl (ar->slot);
815 m->peer = ar->address->peer;
816 GNUNET_MQ_send (sh->mq, ev);
820 /* end of ats_api_scheduling.c */