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 env = GNUNET_MQ_msg_extra (as,
218 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
223 GNUNET_MQ_send (transport_client->mq,
229 * Function called by the solver to tell the transpor to
230 * allocate bandwidth for the specified session.
232 * @param cls closure, NULL
233 * @param session session this is about
234 * @param peer peer this is about
235 * @param bw_in suggested bandwidth for receiving
236 * @param bw_out suggested bandwidth for transmission
239 allocate_cb (void *cls,
240 struct GNUNET_ATS_Session *session,
241 const struct GNUNET_PeerIdentity *peer,
242 struct GNUNET_BANDWIDTH_Value32NBO bw_in,
243 struct GNUNET_BANDWIDTH_Value32NBO bw_out)
245 struct GNUNET_MQ_Envelope *env;
246 struct SessionAllocationMessage *sam;
249 if ( (NULL == transport_client) ||
250 (session->client != transport_client) )
252 /* transport must have just died and solver is addressing the
253 losses of sessions (possibly of previous transport), ignore! */
256 env = GNUNET_MQ_msg (sam,
257 GNUNET_MESSAGE_TYPE_ATS_SESSION_ALLOCATION);
258 sam->session_id = session->session_id;
260 sam->bandwidth_in = bw_in;
261 sam->bandwidth_out = bw_out;
262 GNUNET_MQ_send (transport_client->mq,
268 * Convert @a properties to @a prop
270 * @param properties in NBO
271 * @param prop[out] in HBO
274 prop_ntoh (const struct PropertiesNBO *properties,
275 struct GNUNET_ATS_Properties *prop)
277 prop->delay = GNUNET_TIME_relative_ntoh (properties->delay);
278 prop->goodput_out = ntohl (properties->goodput_out);
279 prop->goodput_in = ntohl (properties->goodput_in);
280 prop->utilization_out = ntohl (properties->utilization_out);
281 prop->utilization_in = ntohl (properties->utilization_in);
282 prop->distance = ntohl (properties->distance);
283 prop->mtu = ntohl (properties->mtu);
284 prop->nt = (enum GNUNET_NetworkType) ntohl (properties->nt);
285 prop->cc = (enum GNUNET_TRANSPORT_CommunicatorCharacteristics) ntohl (properties->cc);
290 * We have received a `struct ExpressPreferenceMessage` from an application client.
292 * @param cls handle to the client
293 * @param msg the start message
296 handle_suggest (void *cls,
297 const struct ExpressPreferenceMessage *msg)
299 struct Client *c = cls;
300 struct ClientPreference *cp;
302 if (CT_NONE == c->type)
303 c->type = CT_APPLICATION;
304 if (CT_APPLICATION != c->type)
307 GNUNET_SERVICE_client_drop (c->client);
310 cp = GNUNET_new (struct ClientPreference);
312 cp->pref.peer = msg->peer;
313 cp->pref.bw = msg->bw;
314 cp->pref.pk = (enum GNUNET_MQ_PreferenceKind) ntohl (msg->pk);
315 cp->ph = plugin->preference_add (plugin->cls,
317 GNUNET_CONTAINER_DLL_insert (c->details.application.cp_head,
318 c->details.application.cp_tail,
320 GNUNET_SERVICE_client_continue (c->client);
325 * We have received a `struct ExpressPreferenceMessage` from an application client.
327 * @param cls handle to the client
328 * @param msg the start message
331 handle_suggest_cancel (void *cls,
332 const struct ExpressPreferenceMessage *msg)
334 struct Client *c = cls;
335 struct ClientPreference *cp;
337 if (CT_NONE == c->type)
338 c->type = CT_APPLICATION;
339 if (CT_APPLICATION != c->type)
342 GNUNET_SERVICE_client_drop (c->client);
345 for (cp = c->details.application.cp_head;
348 if ( (cp->pref.pk == (enum GNUNET_MQ_PreferenceKind) ntohl (msg->pk)) &&
349 (cp->pref.bw.value__ == msg->bw.value__) &&
350 (0 == memcmp (&cp->pref.peer,
352 sizeof (struct GNUNET_PeerIdentity))) )
357 GNUNET_SERVICE_client_drop (c->client);
360 plugin->preference_del (plugin->cls,
363 GNUNET_CONTAINER_DLL_remove (c->details.application.cp_head,
364 c->details.application.cp_tail,
367 GNUNET_SERVICE_client_continue (c->client);
372 * Handle 'start' messages from transport clients.
374 * @param cls client that sent the request
375 * @param message the request message
378 handle_start (void *cls,
379 const struct GNUNET_MessageHeader *hdr)
381 struct Client *c = cls;
383 if (CT_NONE != c->type)
386 GNUNET_SERVICE_client_drop (c->client);
389 c->type = CT_TRANSPORT;
390 c->details.transport.sessions
391 = GNUNET_CONTAINER_multihashmap32_create (128);
392 if (NULL != transport_client)
394 GNUNET_SERVICE_client_drop (transport_client->client);
395 transport_client = NULL;
397 transport_client = c;
398 GNUNET_SERVICE_client_continue (c->client);
403 * Check 'session_add' message is well-formed and comes from a
406 * @param cls client that sent the request
407 * @param message the request message
408 * @return #GNUNET_OK if @a message is well-formed
411 check_session_add (void *cls,
412 const struct SessionAddMessage *message)
414 struct Client *c = cls;
416 GNUNET_MQ_check_zero_termination (message);
417 if (CT_TRANSPORT != c->type)
420 return GNUNET_SYSERR;
427 * Handle 'session add' messages from transport clients.
429 * @param cls client that sent the request
430 * @param message the request message
433 handle_session_add (void *cls,
434 const struct SessionAddMessage *message)
436 struct Client *c = cls;
437 const char *address = (const char *) &message[1];
438 struct GNUNET_ATS_Session *session;
439 int inbound_only = (GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY ==
440 ntohs (message->header.type));
442 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
443 message->session_id);
447 GNUNET_SERVICE_client_drop (c->client);
450 session = GNUNET_new (struct GNUNET_ATS_Session);
451 session->data.session = session;
453 session->session_id = message->session_id;
454 session->data.peer = message->peer;
455 prop_ntoh (&message->properties,
456 &session->data.prop);
457 session->data.inbound_only = inbound_only;
458 GNUNET_assert (GNUNET_YES ==
459 GNUNET_CONTAINER_multihashmap32_put (c->details.transport.sessions,
462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
463 session->sh = plugin->session_add (plugin->cls,
466 GNUNET_assert (NULL != session->sh);
467 GNUNET_SERVICE_client_continue (c->client);
472 * Handle 'session update' messages from transport clients.
474 * @param cls client that sent the request
475 * @param msg the request message
478 handle_session_update (void *cls,
479 const struct SessionUpdateMessage *msg)
481 struct Client *c = cls;
482 struct GNUNET_ATS_Session *session;
484 if (CT_TRANSPORT != c->type)
487 GNUNET_SERVICE_client_drop (c->client);
490 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
495 GNUNET_SERVICE_client_drop (c->client);
498 prop_ntoh (&msg->properties,
499 &session->data.prop);
500 plugin->session_update (plugin->cls,
503 GNUNET_SERVICE_client_continue (c->client);
508 * Handle 'session delete' messages from transport clients.
510 * @param cls client that sent the request
511 * @param message the request message
514 handle_session_del (void *cls,
515 const struct SessionDelMessage *message)
517 struct Client *c = cls;
518 struct GNUNET_ATS_Session *session;
520 if (CT_TRANSPORT != c->type)
523 GNUNET_SERVICE_client_drop (c->client);
526 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
527 message->session_id);
531 GNUNET_SERVICE_client_drop (c->client);
534 GNUNET_assert (NULL != session->sh);
535 plugin->session_del (plugin->cls,
539 GNUNET_assert (GNUNET_YES ==
540 GNUNET_CONTAINER_multihashmap32_remove (c->details.transport.sessions,
543 GNUNET_free (session);
544 GNUNET_SERVICE_client_continue (c->client);
549 * A client connected to us. Setup the local client
553 * @param client handle of the client
554 * @param mq message queue to talk to @a client
558 client_connect_cb (void *cls,
559 struct GNUNET_SERVICE_Client *client,
560 struct GNUNET_MQ_Handle *mq)
562 struct Client *c = GNUNET_new (struct Client);
571 * Function called on each session to release associated state
572 * on transport disconnect.
574 * @param cls the `struct Client`
575 * @param key unused (session_id)
576 * @param value a `struct GNUNET_ATS_Session`
579 free_session (void *cls,
583 struct Client *c = cls;
584 struct GNUNET_ATS_Session *session = value;
587 GNUNET_assert (c == session->client);
588 GNUNET_assert (NULL != session->sh);
589 plugin->session_del (plugin->cls,
593 GNUNET_free (session);
599 * A client disconnected from us. Tear down the local client
603 * @param client handle of the client
604 * @param app_ctx our `struct Client`
607 client_disconnect_cb (void *cls,
608 struct GNUNET_SERVICE_Client *client,
611 struct Client *c = app_ctx;
614 GNUNET_assert (c->client == client);
620 for (struct ClientPreference *cp = c->details.application.cp_head;
622 cp = c->details.application.cp_head)
624 plugin->preference_del (plugin->cls,
627 GNUNET_CONTAINER_DLL_remove (c->details.application.cp_head,
628 c->details.application.cp_tail,
634 if (transport_client == c)
635 transport_client = NULL;
636 GNUNET_CONTAINER_multihashmap32_iterate (c->details.transport.sessions,
639 GNUNET_CONTAINER_multihashmap32_destroy (c->details.transport.sessions);
647 * Task run at the end during shutdown.
652 final_cleanup (void *cls)
657 GNUNET_STATISTICS_destroy (stats,
663 GNUNET_PLUGIN_unload (plugin_name,
667 if (NULL != plugin_name)
669 GNUNET_free (plugin_name);
676 * Task run during shutdown.
681 cleanup_task (void *cls)
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "ATS shutdown initiated\n");
686 GNUNET_SCHEDULER_add_now (&final_cleanup,
692 * Process template requests.
695 * @param cfg configuration to use
696 * @param service the initialized service
700 const struct GNUNET_CONFIGURATION_Handle *cfg,
701 struct GNUNET_SERVICE_Handle *service)
703 static struct GNUNET_ATS_PluginEnvironment env;
706 stats = GNUNET_STATISTICS_create ("ats",
709 GNUNET_CONFIGURATION_get_value_string (cfg,
714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
715 "No ATS solver configured, using 'simple' approach\n");
716 solver = GNUNET_strdup ("simple");
718 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
723 env.suggest_cb = &suggest_cb;
724 env.allocate_cb = &allocate_cb;
725 GNUNET_asprintf (&plugin_name,
726 "libgnunet_plugin_ats2_%s",
728 GNUNET_free (solver);
729 if (NULL == (plugin = GNUNET_PLUGIN_load (plugin_name,
732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
733 _("Failed to initialize solver `%s'!\n"),
735 GNUNET_SCHEDULER_shutdown ();
742 * Define "main" method using service macro.
746 GNUNET_SERVICE_OPTION_NONE,
749 &client_disconnect_cb,
751 GNUNET_MQ_hd_fixed_size (suggest,
752 GNUNET_MESSAGE_TYPE_ATS_SUGGEST,
753 struct ExpressPreferenceMessage,
755 GNUNET_MQ_hd_fixed_size (suggest_cancel,
756 GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL,
757 struct ExpressPreferenceMessage,
759 GNUNET_MQ_hd_fixed_size (start,
760 GNUNET_MESSAGE_TYPE_ATS_START,
761 struct GNUNET_MessageHeader,
763 GNUNET_MQ_hd_var_size (session_add,
764 GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD,
765 struct SessionAddMessage,
767 GNUNET_MQ_hd_var_size (session_add,
768 GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY,
769 struct SessionAddMessage,
771 GNUNET_MQ_hd_fixed_size (session_update,
772 GNUNET_MESSAGE_TYPE_ATS_SESSION_UPDATE,
773 struct SessionUpdateMessage,
775 GNUNET_MQ_hd_fixed_size (session_del,
776 GNUNET_MESSAGE_TYPE_ATS_SESSION_DEL,
777 struct SessionDelMessage,
779 GNUNET_MQ_handler_end ());
782 /* end of gnunet-service-ats.c */