2 This file is part of GNUnet.
3 Copyright (C) 2010-2015, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file ats/ats_api2_transport.c
20 * @brief address suggestions and bandwidth allocation
21 * @author Christian Grothoff
22 * @author Matthias Wachs
25 #include "gnunet_ats_transport_service.h"
28 #define LOG(kind,...) GNUNET_log_from(kind, "ats-transport-api", __VA_ARGS__)
32 * Information we track per session, incoming or outgoing. It also
33 * doesn't matter if we have a session, any session that ATS is
34 * allowed to suggest right now should be tracked.
36 struct GNUNET_ATS_SessionRecord
40 * Transport handle this session record belongs to.
42 struct GNUNET_ATS_TransportHandle *ath;
50 * Session handle, NULL if inbound-only (also implies we cannot
51 * actually control inbound traffic via transport!). So if
52 * @e session is NULL, the @e properties are informative for
53 * ATS (connection exists, utilization) but ATS cannot directly
54 * influence it (and should thus not call the
55 * #GNUNET_ATS_AllocationCallback for this @e session, which is
56 * obvious as NULL is not a meaningful session to allocation
59 struct GNUNET_ATS_Session *session;
62 * Identity of the peer reached at @e address.
64 struct GNUNET_PeerIdentity pid;
67 * Performance data about the @e session.
69 struct GNUNET_ATS_Properties properties;
72 * Unique ID to identify this session at this @a pid in IPC
81 * Handle to the ATS subsystem for bandwidth/transport transport information.
83 struct GNUNET_ATS_TransportHandle
89 const struct GNUNET_CONFIGURATION_Handle *cfg;
92 * Callback to invoke on suggestions.
94 GNUNET_ATS_SuggestionCallback suggest_cb;
97 * Closure for @e suggest_cb.
102 * Callback to invoke on allocations.
104 GNUNET_ATS_AllocationCallback alloc_cb;
107 * Closure for @e alloc_cb.
112 * Message queue for sending requests to the ATS service.
114 struct GNUNET_MQ_Handle *mq;
117 * Task to trigger reconnect.
119 struct GNUNET_SCHEDULER_Task *task;
122 * Hash map mapping PIDs to session records.
124 struct GNUNET_CONTAINER_MultiPeerMap *records;
127 * Reconnect backoff delay.
129 struct GNUNET_TIME_Relative backoff;
136 * Convert ATS properties from host to network byte order.
138 * @param nbo[OUT] value written
139 * @param hbo value read
142 properties_hton (struct PropertiesNBO *nbo,
143 const struct GNUNET_ATS_Properties *hbo)
145 nbo->delay = GNUNET_TIME_relative_hton (hbo->delay);
146 nbo->goodput_out = htonl (hbo->goodput_out);
147 nbo->goodput_in = htonl (hbo->goodput_in);
148 nbo->utilization_out = htonl (hbo->utilization_out);
149 nbo->utilization_in = htonl (hbo->utilization_in);
150 nbo->distance = htonl (hbo->distance);
151 nbo->mtu = htonl (hbo->mtu);
152 nbo->nt = htonl ((uint32_t) hbo->nt);
153 nbo->cc = htonl ((uint32_t) hbo->cc);
158 * Re-establish the connection to the ATS service.
160 * @param sh handle to use to re-connect.
163 reconnect (struct GNUNET_ATS_TransportHandle *ath);
167 * Re-establish the connection to the ATS service.
169 * @param cls handle to use to re-connect.
172 reconnect_task (void *cls)
174 struct GNUNET_ATS_TransportHandle *ath = cls;
182 * Disconnect from ATS and then reconnect.
184 * @param ath our handle
187 force_reconnect (struct GNUNET_ATS_TransportHandle *ath)
191 GNUNET_MQ_destroy (ath->mq);
194 /* FIXME: do we tell transport service about disconnect events? CON:
195 initially ATS will have a really screwed picture of the world and
196 the rapid change would be bad. PRO: if we don't, ATS and
197 transport may disagree about the allocation for a while...
198 For now: lazy: do nothing. */
199 ath->backoff = GNUNET_TIME_STD_BACKOFF (ath->backoff);
200 ath->task = GNUNET_SCHEDULER_add_delayed (ath->backoff,
207 * Check format of address suggestion message from the service.
209 * @param cls the `struct GNUNET_ATS_TransportHandle`
210 * @param m message received
213 check_ats_address_suggestion (void *cls,
214 const struct AddressSuggestionMessage *m)
217 GNUNET_MQ_check_zero_termination (m);
218 return GNUNET_SYSERR;
223 * We received an address suggestion message from the service.
225 * @param cls the `struct GNUNET_ATS_TransportHandle`
226 * @param m message received
229 handle_ats_address_suggestion (void *cls,
230 const struct AddressSuggestionMessage *m)
232 struct GNUNET_ATS_TransportHandle *ath = cls;
233 const char *address = (const char *) &m[1];
235 ath->suggest_cb (ath->suggest_cb_cls,
242 * Closure for #match_session_cb.
252 * Where to store the result.
254 struct GNUNET_ATS_SessionRecord *sr;
259 * Finds matching session record.
261 * @param cls a `struct FindContext`
262 * @param pid peer identity (unused)
263 * @param value a `struct GNUNET_ATS_SessionRecord`
264 * @return #GNUNET_NO if match found, #GNUNET_YES to continue searching
267 match_session_cb (void *cls,
268 const struct GNUNET_PeerIdentity *pid,
271 struct FindContext *fc = cls;
272 struct GNUNET_ATS_SessionRecord *sr = value;
275 if (fc->session_id == sr->slot)
286 * Find session record for peer @a pid and session @a session_id
288 * @param ath transport handle to search
289 * @param session_id session ID to match
290 * @param pid peer to search under
291 * @return NULL if no such record exists
293 static struct GNUNET_ATS_SessionRecord *
294 find_session (struct GNUNET_ATS_TransportHandle *ath,
296 const struct GNUNET_PeerIdentity *pid)
298 struct FindContext fc = {
299 .session_id = session_id,
302 GNUNET_CONTAINER_multipeermap_get_multiple (ath->records,
311 * We received a session allocation message from the service.
313 * @param cls the `struct GNUNET_ATS_TransportHandle`
314 * @param m message received
317 handle_ats_session_allocation (void *cls,
318 const struct SessionAllocationMessage *m)
320 struct GNUNET_ATS_TransportHandle *ath = cls;
321 struct GNUNET_ATS_SessionRecord *ar;
324 session_id = ntohl (m->session_id);
325 ar = find_session (ath,
330 /* this can (rarely) happen if ATS changes an sessiones allocation
331 just when the transport service deleted it */
332 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
333 "Allocation ignored, session unknown\n");
336 ath->backoff = GNUNET_TIME_UNIT_ZERO;
337 LOG (GNUNET_ERROR_TYPE_DEBUG,
338 "ATS allocates bandwidth for peer `%s' using address %s\n",
339 GNUNET_i2s (&ar->pid),
341 ath->alloc_cb (ath->alloc_cb_cls,
349 * We encountered an error handling the MQ to the ATS service.
352 * @param cls the `struct GNUNET_ATS_TransportHandle`
353 * @param error details about the error
356 error_handler (void *cls,
357 enum GNUNET_MQ_Error error)
359 struct GNUNET_ATS_TransportHandle *ath = cls;
361 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "ATS connection died (code %d), reconnecting\n",
364 force_reconnect (ath);
369 * Generate and transmit the `struct SessionAddMessage` for the given
372 * @param ar the session to inform the ATS service about
375 send_add_session_message (const struct GNUNET_ATS_SessionRecord *ar)
377 struct GNUNET_ATS_TransportHandle *ath = ar->ath;
378 struct GNUNET_MQ_Envelope *ev;
379 struct SessionAddMessage *m;
383 return; /* disconnected, skip for now */
384 alen = strlen (ar->address) + 1;
385 ev = GNUNET_MQ_msg_extra (m,
387 (NULL == ar->session)
388 ? GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY
389 : GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD);
391 m->session_id = htonl (ar->slot);
392 properties_hton (&m->properties,
394 GNUNET_memcpy (&m[1],
398 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 "Adding address `%s' for peer `%s'\n",
401 GNUNET_i2s (&ar->pid));
402 GNUNET_MQ_send (ath->mq,
408 * Send ATS information about the session record.
410 * @param cls our `struct GNUNET_ATS_TransportHandle *`, unused
412 * @param value the `struct GNUNET_ATS_SessionRecord *` to add
416 send_add_session_cb (void *cls,
417 const struct GNUNET_PeerIdentity *pid,
420 struct GNUNET_ATS_SessionRecord *ar = value;
424 send_add_session_message (ar);
430 * Re-establish the connection to the ATS service.
432 * @param ath handle to use to re-connect.
435 reconnect (struct GNUNET_ATS_TransportHandle *ath)
437 struct GNUNET_MQ_MessageHandler handlers[] = {
438 GNUNET_MQ_hd_var_size (ats_address_suggestion,
439 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION,
440 struct AddressSuggestionMessage,
442 GNUNET_MQ_hd_fixed_size (ats_session_allocation,
443 GNUNET_MESSAGE_TYPE_ATS_SESSION_ALLOCATION,
444 struct SessionAllocationMessage,
446 GNUNET_MQ_handler_end ()
448 struct GNUNET_MQ_Envelope *ev;
449 struct GNUNET_MessageHeader *init;
451 GNUNET_assert (NULL == ath->mq);
452 ath->mq = GNUNET_CLIENT_connect (ath->cfg,
460 force_reconnect (ath);
463 ev = GNUNET_MQ_msg (init,
464 GNUNET_MESSAGE_TYPE_ATS_START);
465 GNUNET_MQ_send (ath->mq,
469 GNUNET_CONTAINER_multipeermap_iterate (ath->records,
470 &send_add_session_cb,
476 * Initialize the ATS subsystem.
478 * @param cfg configuration to use
479 * @param alloc_cb notification to call whenever the allocation changed
480 * @param alloc_cb_cls closure for @a alloc_cb
481 * @param suggest_cb notification to call whenever the suggestation is made
482 * @param suggest_cb_cls closure for @a suggest_cb
483 * @return ats context
485 struct GNUNET_ATS_TransportHandle *
486 GNUNET_ATS_transport_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
487 GNUNET_ATS_AllocationCallback alloc_cb,
489 GNUNET_ATS_SuggestionCallback suggest_cb,
490 void *suggest_cb_cls)
492 struct GNUNET_ATS_TransportHandle *ath;
494 ath = GNUNET_new (struct GNUNET_ATS_TransportHandle);
496 ath->suggest_cb = suggest_cb;
497 ath->suggest_cb_cls = suggest_cb_cls;
498 ath->alloc_cb = alloc_cb;
499 ath->alloc_cb_cls = alloc_cb_cls;
500 ath->records = GNUNET_CONTAINER_multipeermap_create (128,
508 * Release memory associated with the session record.
512 * @param value a `struct GNUNET_ATS_SessionRecord`
516 free_record (void *cls,
517 const struct GNUNET_PeerIdentity *pid,
520 struct GNUNET_ATS_SessionRecord *ar = value;
530 * Client is done with ATS transport, release resources.
532 * @param ath handle to release
535 GNUNET_ATS_transport_done (struct GNUNET_ATS_TransportHandle *ath)
539 GNUNET_MQ_destroy (ath->mq);
542 if (NULL != ath->task)
544 GNUNET_SCHEDULER_cancel (ath->task);
547 GNUNET_CONTAINER_multipeermap_iterate (ath->records,
550 GNUNET_CONTAINER_multipeermap_destroy (ath->records);
556 * We have a new session ATS should know. Sessiones have to be added
557 * with this function before they can be: updated, set in use and
561 * @param pid peer we connected to
562 * @param address the address (human readable version)
563 * @param session transport-internal handle for the session/queue, NULL if
564 * the session is inbound-only
565 * @param prop performance data for the session
566 * @return handle to the session representation inside ATS, NULL
567 * on error (i.e. ATS knows this exact session already)
569 struct GNUNET_ATS_SessionRecord *
570 GNUNET_ATS_session_add (struct GNUNET_ATS_TransportHandle *ath,
571 const struct GNUNET_PeerIdentity *pid,
573 struct GNUNET_ATS_Session *session,
574 const struct GNUNET_ATS_Properties *prop)
576 struct GNUNET_ATS_SessionRecord *ar;
582 /* we need a valid address */
586 alen = strlen (address) + 1;
587 if ( (alen + sizeof (struct SessionAddMessage) >= GNUNET_MAX_MESSAGE_SIZE) ||
588 (alen >= GNUNET_MAX_MESSAGE_SIZE) )
590 /* address too large for us, this should not happen */
595 /* Spin 's' until we find an unused session ID for this pid */
596 for (s = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
598 NULL != find_session (ath,
603 alen = strlen (address) + 1;
604 ar = GNUNET_malloc (sizeof (struct GNUNET_ATS_SessionRecord) + alen);
607 ar->session = session;
608 ar->address = (const char *) &ar[1];
610 ar->properties = *prop;
614 (void) GNUNET_CONTAINER_multipeermap_put (ath->records,
617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
618 send_add_session_message (ar);
624 * We have updated performance statistics for a given session. Note
625 * that this function can be called for sessiones that are currently
626 * in use as well as sessiones that are valid but not actively in use.
627 * Furthermore, the peer may not even be connected to us right now (in
628 * which case the call may be ignored or the information may be stored
629 * for later use). Update bandwidth assignments.
631 * @param ar session record to update information for
632 * @param prop performance data for the session
635 GNUNET_ATS_session_update (struct GNUNET_ATS_SessionRecord *ar,
636 const struct GNUNET_ATS_Properties *prop)
638 struct GNUNET_ATS_TransportHandle *ath = ar->ath;
639 struct GNUNET_MQ_Envelope *ev;
640 struct SessionUpdateMessage *m;
642 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 "Updating address `%s' for peer `%s'\n",
645 GNUNET_i2s (&ar->pid));
646 ar->properties = *prop;
648 return; /* disconnected, skip for now */
649 ev = GNUNET_MQ_msg (m,
650 GNUNET_MESSAGE_TYPE_ATS_SESSION_UPDATE);
651 m->session_id = htonl (ar->slot);
653 properties_hton (&m->properties,
655 GNUNET_MQ_send (ath->mq,
661 * A session was destroyed, ATS should now schedule and
662 * allocate under the assumption that this @a ar is no
665 * @param ar session record to drop
668 GNUNET_ATS_session_del (struct GNUNET_ATS_SessionRecord *ar)
670 struct GNUNET_ATS_TransportHandle *ath = ar->ath;
671 struct GNUNET_MQ_Envelope *ev;
672 struct SessionDelMessage *m;
674 LOG (GNUNET_ERROR_TYPE_DEBUG,
675 "Deleting address `%s' for peer `%s'\n",
677 GNUNET_i2s (&ar->pid));
680 ev = GNUNET_MQ_msg (m,
681 GNUNET_MESSAGE_TYPE_ATS_SESSION_DEL);
682 m->session_id = htonl (ar->slot);
684 GNUNET_MQ_send (ath->mq,
689 /* end of ats_api2_transport.c */