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_SERVICE_client_continue (c->client);
471 * Handle 'session update' messages from transport clients.
473 * @param cls client that sent the request
474 * @param msg the request message
477 handle_session_update (void *cls,
478 const struct SessionUpdateMessage *msg)
480 struct Client *c = cls;
481 struct GNUNET_ATS_Session *session;
483 if (CT_TRANSPORT != c->type)
486 GNUNET_SERVICE_client_drop (c->client);
489 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
494 GNUNET_SERVICE_client_drop (c->client);
497 prop_ntoh (&msg->properties,
498 &session->data.prop);
499 plugin->session_update (plugin->cls,
502 GNUNET_SERVICE_client_continue (c->client);
507 * Handle 'session delete' messages from transport clients.
509 * @param cls client that sent the request
510 * @param message the request message
513 handle_session_del (void *cls,
514 const struct SessionDelMessage *message)
516 struct Client *c = cls;
517 struct GNUNET_ATS_Session *session;
519 if (CT_TRANSPORT != c->type)
522 GNUNET_SERVICE_client_drop (c->client);
525 session = GNUNET_CONTAINER_multihashmap32_get (c->details.transport.sessions,
526 message->session_id);
530 GNUNET_SERVICE_client_drop (c->client);
533 plugin->session_del (plugin->cls,
536 GNUNET_assert (GNUNET_YES ==
537 GNUNET_CONTAINER_multihashmap32_remove (c->details.transport.sessions,
540 GNUNET_free (session);
541 GNUNET_SERVICE_client_continue (c->client);
546 * A client connected to us. Setup the local client
550 * @param client handle of the client
551 * @param mq message queue to talk to @a client
555 client_connect_cb (void *cls,
556 struct GNUNET_SERVICE_Client *client,
557 struct GNUNET_MQ_Handle *mq)
559 struct Client *c = GNUNET_new (struct Client);
568 * Function called on each session to release associated state
569 * on transport disconnect.
571 * @param cls the `struct Client`
572 * @param key unused (session_id)
573 * @param value a `struct GNUNET_ATS_Session`
576 free_session (void *cls,
580 struct Client *c = cls;
581 struct GNUNET_ATS_Session *session = value;
584 GNUNET_assert (c == session->client);
585 plugin->session_del (plugin->cls,
588 GNUNET_free (session);
594 * A client disconnected from us. Tear down the local client
598 * @param client handle of the client
599 * @param app_ctx our `struct Client`
602 client_disconnect_cb (void *cls,
603 struct GNUNET_SERVICE_Client *client,
606 struct Client *c = app_ctx;
609 GNUNET_assert (c->client == client);
615 for (struct ClientPreference *cp = c->details.application.cp_head;
617 cp = c->details.application.cp_head)
619 plugin->preference_del (plugin->cls,
622 GNUNET_CONTAINER_DLL_remove (c->details.application.cp_head,
623 c->details.application.cp_tail,
629 if (transport_client == c)
630 transport_client = NULL;
631 GNUNET_CONTAINER_multihashmap32_iterate (c->details.transport.sessions,
634 GNUNET_CONTAINER_multihashmap32_destroy (c->details.transport.sessions);
642 * Task run at the end during shutdown.
647 final_cleanup (void *cls)
652 GNUNET_STATISTICS_destroy (stats,
658 GNUNET_PLUGIN_unload (plugin_name,
662 if (NULL != plugin_name)
664 GNUNET_free (plugin_name);
671 * Task run during shutdown.
676 cleanup_task (void *cls)
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680 "ATS shutdown initiated\n");
681 GNUNET_SCHEDULER_add_now (&final_cleanup,
687 * Process template requests.
690 * @param cfg configuration to use
691 * @param service the initialized service
695 const struct GNUNET_CONFIGURATION_Handle *cfg,
696 struct GNUNET_SERVICE_Handle *service)
698 static struct GNUNET_ATS_PluginEnvironment env;
701 stats = GNUNET_STATISTICS_create ("ats",
704 GNUNET_CONFIGURATION_get_value_string (cfg,
709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
710 "No ATS solver configured, using 'simple' approach\n");
711 solver = GNUNET_strdup ("simple");
713 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
718 env.suggest_cb = &suggest_cb;
719 env.allocate_cb = &allocate_cb;
720 GNUNET_asprintf (&plugin_name,
721 "libgnunet_plugin_ats2_%s",
723 GNUNET_free (solver);
724 if (NULL == (plugin = GNUNET_PLUGIN_load (plugin_name,
727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728 _("Failed to initialize solver `%s'!\n"),
730 GNUNET_SCHEDULER_shutdown ();
737 * Define "main" method using service macro.
741 GNUNET_SERVICE_OPTION_NONE,
744 &client_disconnect_cb,
746 GNUNET_MQ_hd_fixed_size (suggest,
747 GNUNET_MESSAGE_TYPE_ATS_SUGGEST,
748 struct ExpressPreferenceMessage,
750 GNUNET_MQ_hd_fixed_size (suggest_cancel,
751 GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL,
752 struct ExpressPreferenceMessage,
754 GNUNET_MQ_hd_fixed_size (start,
755 GNUNET_MESSAGE_TYPE_ATS_START,
756 struct GNUNET_MessageHeader,
758 GNUNET_MQ_hd_var_size (session_add,
759 GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD,
760 struct SessionAddMessage,
762 GNUNET_MQ_hd_var_size (session_add,
763 GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY,
764 struct SessionAddMessage,
766 GNUNET_MQ_hd_fixed_size (session_update,
767 GNUNET_MESSAGE_TYPE_ATS_SESSION_UPDATE,
768 struct SessionUpdateMessage,
770 GNUNET_MQ_hd_fixed_size (session_del,
771 GNUNET_MESSAGE_TYPE_ATS_SESSION_DEL,
772 struct SessionDelMessage,
774 GNUNET_MQ_handler_end ());
777 /* end of gnunet-service-ats.c */