2 This file is part of GNUnet.
3 Copyright (C) 2011, 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file ats/gnunet-service-ats-new.c
23 * @author Matthias Wachs
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_ats_plugin_new.h"
34 * What type of client is this client?
55 * Information we track per client.
60 * Preferences expressed by a client are kept in a DLL per client.
62 struct ClientPreference
67 struct ClientPreference *next;
72 struct ClientPreference *prev;
75 * Which client expressed the preference?
77 struct Client *client;
80 * Plugin's representation of the preference.
82 struct GNUNET_ATS_PreferenceHandle *ph;
85 * Details about the preference.
87 struct GNUNET_ATS_Preference pref;
92 * Information about ongoing sessions of the transport client.
94 struct GNUNET_ATS_Session
98 * Session data exposed to the plugin.
100 struct GNUNET_ATS_SessionData data;
103 * The transport client that provided the session.
105 struct Client *client;
108 * Session state in the plugin.
110 struct GNUNET_ATS_SessionHandle *sh;
113 * Unique ID for the session when talking with the client.
121 * Information we track per client.
126 * Type of the client, initially #CT_NONE.
128 enum ClientType type;
131 * Service handle of the client.
133 struct GNUNET_SERVICE_Client *client;
136 * Message queue to talk to the client.
138 struct GNUNET_MQ_Handle *mq;
141 * Details depending on @e type.
148 * Head of DLL of preferences expressed by this client.
150 struct ClientPreference *cp_head;
153 * Tail of DLL of preferences expressed by this client.
155 struct ClientPreference *cp_tail;
162 * Map from session IDs to `struct GNUNET_ATS_Session` objects.
164 struct GNUNET_CONTAINER_MultiHashMap32 *sessions;
174 * Handle for statistics.
176 static struct GNUNET_STATISTICS_Handle *stats;
181 static struct GNUNET_ATS_SolverFunctions *plugin;
184 * Solver plugin name as string
186 static char *plugin_name;
189 * The transport client (there can only be one at a time).
191 static struct Client *transport_client;
195 * Function called by the solver to prompt the transport to
196 * try out a new address.
198 * @param cls closure, NULL
199 * @param pid peer this is about
200 * @param address address the transport should try
203 suggest_cb (void *cls,
204 const struct GNUNET_PeerIdentity *pid,
207 struct GNUNET_MQ_Envelope *env;
208 size_t slen = strlen (address) + 1;
209 struct AddressSuggestionMessage *as;
211 if (NULL == transport_client)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Suggesting address `%s' of peer `%s'\n",
220 env = GNUNET_MQ_msg_extra (as,
222 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
227 GNUNET_MQ_send (transport_client->mq,
233 * Function called by the solver to tell the transpor to
234 * allocate bandwidth for the specified session.
236 * @param cls closure, NULL
237 * @param session session this is about
238 * @param peer peer this is about
239 * @param bw_in suggested bandwidth for receiving
240 * @param bw_out suggested bandwidth for transmission
243 allocate_cb (void *cls,
244 struct GNUNET_ATS_Session *session,
245 const struct GNUNET_PeerIdentity *peer,
246 struct GNUNET_BANDWIDTH_Value32NBO bw_in,
247 struct GNUNET_BANDWIDTH_Value32NBO bw_out)
249 struct GNUNET_MQ_Envelope *env;
250 struct SessionAllocationMessage *sam;
253 if ( (NULL == transport_client) ||
254 (session->client != transport_client) )
256 /* transport must have just died and solver is addressing the
257 losses of sessions (possibly of previous transport), ignore! */
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Allocating %u/%u bytes for %p of peer `%s'\n",
262 ntohl (bw_in.value__),
263 ntohl (bw_out.value__),
266 env = GNUNET_MQ_msg (sam,
267 GNUNET_MESSAGE_TYPE_ATS_SESSION_ALLOCATION);
268 sam->session_id = session->session_id;
270 sam->bandwidth_in = bw_in;
271 sam->bandwidth_out = bw_out;
272 GNUNET_MQ_send (transport_client->mq,
278 * Convert @a properties to @a prop
280 * @param properties in NBO
281 * @param prop[out] in HBO
284 prop_ntoh (const struct PropertiesNBO *properties,
285 struct GNUNET_ATS_Properties *prop)
287 prop->delay = GNUNET_TIME_relative_ntoh (properties->delay);
288 prop->goodput_out = ntohl (properties->goodput_out);
289 prop->goodput_in = ntohl (properties->goodput_in);
290 prop->utilization_out = ntohl (properties->utilization_out);
291 prop->utilization_in = ntohl (properties->utilization_in);
292 prop->distance = ntohl (properties->distance);
293 prop->mtu = ntohl (properties->mtu);
294 prop->nt = (enum GNUNET_NetworkType) ntohl (properties->nt);
295 prop->cc = (enum GNUNET_TRANSPORT_CommunicatorCharacteristics) ntohl (properties->cc);
300 * We have received a `struct ExpressPreferenceMessage` from an application client.
302 * @param cls handle to the client
303 * @param msg the start message
306 handle_suggest (void *cls,
307 const struct ExpressPreferenceMessage *msg)
309 struct Client *c = cls;
310 struct ClientPreference *cp;
312 if (CT_NONE == c->type)
313 c->type = CT_APPLICATION;
314 if (CT_APPLICATION != c->type)
317 GNUNET_SERVICE_client_drop (c->client);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Client suggested we talk to %s with preference %d at rate %u\n",
322 GNUNET_i2s (&msg->peer),
323 (int) ntohl (msg->pk),
324 (int) ntohl (msg->bw.value__));
325 cp = GNUNET_new (struct ClientPreference);
327 cp->pref.peer = msg->peer;
328 cp->pref.bw = msg->bw;
329 cp->pref.pk = (enum GNUNET_MQ_PreferenceKind) ntohl (msg->pk);
330 cp->ph = plugin->preference_add (plugin->cls,
332 GNUNET_CONTAINER_DLL_insert (c->details.application.cp_head,
333 c->details.application.cp_tail,
335 GNUNET_SERVICE_client_continue (c->client);
340 * We have received a `struct ExpressPreferenceMessage` from an application client.
342 * @param cls handle to the client
343 * @param msg the start message
346 handle_suggest_cancel (void *cls,
347 const struct ExpressPreferenceMessage *msg)
349 struct Client *c = cls;
350 struct ClientPreference *cp;
352 if (CT_NONE == c->type)
353 c->type = CT_APPLICATION;
354 if (CT_APPLICATION != c->type)
357 GNUNET_SERVICE_client_drop (c->client);
360 for (cp = c->details.application.cp_head;
363 if ( (cp->pref.pk == (enum GNUNET_MQ_PreferenceKind) ntohl (msg->pk)) &&
364 (cp->pref.bw.value__ == msg->bw.value__) &&
365 (0 == GNUNET_memcmp (&cp->pref.peer,
371 GNUNET_SERVICE_client_drop (c->client);
374 plugin->preference_del (plugin->cls,
377 GNUNET_CONTAINER_DLL_remove (c->details.application.cp_head,
378 c->details.application.cp_tail,
381 GNUNET_SERVICE_client_continue (c->client);
386 * Handle 'start' messages from transport clients.
388 * @param cls client that sent the request
389 * @param message the request message
392 handle_start (void *cls,
393 const struct GNUNET_MessageHeader *hdr)
395 struct Client *c = cls;
397 if (CT_NONE != c->type)
400 GNUNET_SERVICE_client_drop (c->client);
403 c->type = CT_TRANSPORT;
404 c->details.transport.sessions
405 = GNUNET_CONTAINER_multihashmap32_create (128);
406 if (NULL != transport_client)
408 GNUNET_SERVICE_client_drop (transport_client->client);
409 transport_client = NULL;
411 transport_client = c;
412 GNUNET_SERVICE_client_continue (c->client);
417 * Check 'session_add' message is well-formed and comes from a
420 * @param cls client that sent the request
421 * @param message the request message
422 * @return #GNUNET_OK if @a message is well-formed
425 check_session_add (void *cls,
426 const struct SessionAddMessage *message)
428 struct Client *c = cls;
430 GNUNET_MQ_check_zero_termination (message);
431 if (CT_TRANSPORT != c->type)
434 return GNUNET_SYSERR;
441 * Handle 'session add' messages from transport clients.
443 * @param cls client that sent the request
444 * @param message the request message
447 handle_session_add (void *cls,
448 const struct SessionAddMessage *message)
450 struct Client *c = cls;
451 const char *address = (const char *) &message[1];
452 struct GNUNET_ATS_Session *session;
453 int inbound_only = (GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY ==
454 ntohs (message->header.type));
456 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
457 message->session_id);
461 GNUNET_SERVICE_client_drop (c->client);
464 session = GNUNET_new (struct GNUNET_ATS_Session);
465 session->data.session = session;
467 session->session_id = message->session_id;
468 session->data.peer = message->peer;
469 prop_ntoh (&message->properties,
470 &session->data.prop);
471 session->data.inbound_only = inbound_only;
472 GNUNET_assert (GNUNET_YES ==
473 GNUNET_CONTAINER_multihashmap32_put (c->details.transport.sessions,
476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
477 session->sh = plugin->session_add (plugin->cls,
480 GNUNET_assert (NULL != session->sh);
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Transport has new session %p to %s\n",
484 GNUNET_i2s (&message->peer));
485 GNUNET_SERVICE_client_continue (c->client);
490 * Handle 'session update' messages from transport clients.
492 * @param cls client that sent the request
493 * @param msg the request message
496 handle_session_update (void *cls,
497 const struct SessionUpdateMessage *msg)
499 struct Client *c = cls;
500 struct GNUNET_ATS_Session *session;
502 if (CT_TRANSPORT != c->type)
505 GNUNET_SERVICE_client_drop (c->client);
508 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
513 GNUNET_SERVICE_client_drop (c->client);
516 prop_ntoh (&msg->properties,
517 &session->data.prop);
518 plugin->session_update (plugin->cls,
521 GNUNET_SERVICE_client_continue (c->client);
526 * Handle 'session delete' messages from transport clients.
528 * @param cls client that sent the request
529 * @param message the request message
532 handle_session_del (void *cls,
533 const struct SessionDelMessage *message)
535 struct Client *c = cls;
536 struct GNUNET_ATS_Session *session;
538 if (CT_TRANSPORT != c->type)
541 GNUNET_SERVICE_client_drop (c->client);
544 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
545 message->session_id);
549 GNUNET_SERVICE_client_drop (c->client);
552 GNUNET_assert (NULL != session->sh);
553 plugin->session_del (plugin->cls,
557 GNUNET_assert (GNUNET_YES ==
558 GNUNET_CONTAINER_multihashmap32_remove (c->details.transport.sessions,
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Transport lost session %p to %s\n",
564 GNUNET_i2s (&session->data.peer));
565 GNUNET_free (session);
566 GNUNET_SERVICE_client_continue (c->client);
571 * A client connected to us. Setup the local client
575 * @param client handle of the client
576 * @param mq message queue to talk to @a client
580 client_connect_cb (void *cls,
581 struct GNUNET_SERVICE_Client *client,
582 struct GNUNET_MQ_Handle *mq)
584 struct Client *c = GNUNET_new (struct Client);
593 * Function called on each session to release associated state
594 * on transport disconnect.
596 * @param cls the `struct Client`
597 * @param key unused (session_id)
598 * @param value a `struct GNUNET_ATS_Session`
601 free_session (void *cls,
605 struct Client *c = cls;
606 struct GNUNET_ATS_Session *session = value;
609 GNUNET_assert (c == session->client);
610 GNUNET_assert (NULL != session->sh);
611 plugin->session_del (plugin->cls,
615 GNUNET_free (session);
621 * A client disconnected from us. Tear down the local client
625 * @param client handle of the client
626 * @param app_ctx our `struct Client`
629 client_disconnect_cb (void *cls,
630 struct GNUNET_SERVICE_Client *client,
633 struct Client *c = app_ctx;
636 GNUNET_assert (c->client == client);
642 for (struct ClientPreference *cp = c->details.application.cp_head;
644 cp = c->details.application.cp_head)
646 plugin->preference_del (plugin->cls,
649 GNUNET_CONTAINER_DLL_remove (c->details.application.cp_head,
650 c->details.application.cp_tail,
656 if (transport_client == c)
657 transport_client = NULL;
658 GNUNET_CONTAINER_multihashmap32_iterate (c->details.transport.sessions,
661 GNUNET_CONTAINER_multihashmap32_destroy (c->details.transport.sessions);
669 * Task run at the end during shutdown.
674 final_cleanup (void *cls)
679 GNUNET_STATISTICS_destroy (stats,
685 GNUNET_PLUGIN_unload (plugin_name,
689 if (NULL != plugin_name)
691 GNUNET_free (plugin_name);
698 * Task run during shutdown.
703 cleanup_task (void *cls)
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
707 "ATS shutdown initiated\n");
708 GNUNET_SCHEDULER_add_now (&final_cleanup,
714 * Process template requests.
717 * @param cfg configuration to use
718 * @param service the initialized service
722 const struct GNUNET_CONFIGURATION_Handle *cfg,
723 struct GNUNET_SERVICE_Handle *service)
725 static struct GNUNET_ATS_PluginEnvironment env;
728 stats = GNUNET_STATISTICS_create ("ats",
731 GNUNET_CONFIGURATION_get_value_string (cfg,
736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
737 "No ATS solver configured, using 'simple' approach\n");
738 solver = GNUNET_strdup ("simple");
740 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
745 env.suggest_cb = &suggest_cb;
746 env.allocate_cb = &allocate_cb;
747 GNUNET_asprintf (&plugin_name,
748 "libgnunet_plugin_ats2_%s",
750 GNUNET_free (solver);
751 if (NULL == (plugin = GNUNET_PLUGIN_load (plugin_name,
754 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
755 _("Failed to initialize solver `%s'!\n"),
757 GNUNET_SCHEDULER_shutdown ();
764 * Define "main" method using service macro.
768 GNUNET_SERVICE_OPTION_NONE,
771 &client_disconnect_cb,
773 GNUNET_MQ_hd_fixed_size (suggest,
774 GNUNET_MESSAGE_TYPE_ATS_SUGGEST,
775 struct ExpressPreferenceMessage,
777 GNUNET_MQ_hd_fixed_size (suggest_cancel,
778 GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL,
779 struct ExpressPreferenceMessage,
781 GNUNET_MQ_hd_fixed_size (start,
782 GNUNET_MESSAGE_TYPE_ATS_START,
783 struct GNUNET_MessageHeader,
785 GNUNET_MQ_hd_var_size (session_add,
786 GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD,
787 struct SessionAddMessage,
789 GNUNET_MQ_hd_var_size (session_add,
790 GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY,
791 struct SessionAddMessage,
793 GNUNET_MQ_hd_fixed_size (session_update,
794 GNUNET_MESSAGE_TYPE_ATS_SESSION_UPDATE,
795 struct SessionUpdateMessage,
797 GNUNET_MQ_hd_fixed_size (session_del,
798 GNUNET_MESSAGE_TYPE_ATS_SESSION_DEL,
799 struct SessionDelMessage,
801 GNUNET_MQ_handler_end ());
804 /* end of gnunet-service-ats.c */