2 This file is part of GNUnet.
3 (C) 2011-2014 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file ats/gnunet-service-ats_plugins.c
23 * @brief ats service plugin management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet-service-ats.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_ats_plugin.h"
33 #include "gnunet-service-ats_addresses.h"
34 #include "gnunet-service-ats_performance.h"
35 #include "gnunet-service-ats_plugins.h"
36 #include "gnunet-service-ats_scheduling.h"
37 #include "gnunet-service-ats_normalization.h"
43 * Configured ATS solver
48 * Solver handle. FIXME: TYPE!?
53 * Solver functions. FIXME.
55 static struct GNUNET_ATS_PluginEnvironment env;
58 * Solver plugin name as string
64 * The preference changed for a peer, update solver.
66 * @param peer the peer
67 * @param kind the ATS kind
68 * @param pref_rel the new relative preference value
71 GAS_normalized_preference_changed (const struct GNUNET_PeerIdentity *peer,
72 enum GNUNET_ATS_PreferenceKind kind,
75 /* Tell solver about update */
76 env.sf.s_pref (solver, peer, kind, pref_rel);
81 * The relative value for a property changed
83 * @param address the peer
84 * @param type the ATS type
85 * @param prop_rel the new relative preference value
88 GAS_normalized_property_changed (struct ATS_Address *address,
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "Normalized property %s for peer `%s' changed to %.3f \n",
94 GNUNET_ATS_print_property_type (type),
95 GNUNET_i2s (&address->peer),
97 env.sf.s_address_update_property (solver,
106 * Solver information callback
108 * @param cls the closure
109 * @param op the operation
110 * @param status operation status
111 * @param add additional information
114 solver_info_cb (void *cls,
115 enum GAS_Solver_Operation op,
116 enum GAS_Solver_Status status,
117 enum GAS_Solver_Additional_Information add)
123 add_info = "GAS_INFO_NONE";
126 add_info = "GAS_INFO_MLP_FULL";
128 case GAS_INFO_UPDATED:
129 add_info = "GAS_INFO_MLP_UPDATED";
131 case GAS_INFO_PROP_ALL:
132 add_info = "GAS_INFO_PROP_ALL";
134 case GAS_INFO_PROP_SINGLE:
135 add_info = "GAS_INFO_PROP_SINGLE";
138 add_info = "INVALID";
143 case GAS_OP_SOLVE_START:
144 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
145 "Solver notifies `%s' with result `%s' `%s'\n", "GAS_OP_SOLVE_START",
146 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
148 case GAS_OP_SOLVE_STOP:
149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
150 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_STOP",
151 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL", add_info);
154 case GAS_OP_SOLVE_SETUP_START:
155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
156 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_START",
157 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
160 case GAS_OP_SOLVE_SETUP_STOP:
161 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
162 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_SETUP_STOP",
163 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
166 case GAS_OP_SOLVE_MLP_LP_START:
167 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
168 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_START",
169 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
171 case GAS_OP_SOLVE_MLP_LP_STOP:
172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
173 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_LP_STOP",
174 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
177 case GAS_OP_SOLVE_MLP_MLP_START:
178 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
179 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_START",
180 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
182 case GAS_OP_SOLVE_MLP_MLP_STOP:
183 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
184 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_MLP_STOP",
185 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
187 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
188 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
189 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
190 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
192 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
194 "Solver notifies `%s' with result `%s'\n", "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
195 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
204 * Callback for solver to notify about assignment changes
207 * @param address the address with changes
210 bandwidth_changed_cb (void *cls,
211 struct ATS_Address *address)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Bandwidth assignment changed for peer %s \n",
218 GNUNET_i2s (&address->peer));
220 /* Notify performance clients about changes to address */
221 GAS_performance_notify_all_clients (&address->peer,
228 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
229 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
231 if ( (0 == address->assigned_bw_in) &&
232 (0 == address->assigned_bw_out) )
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "Telling transport to disconnect peer `%s'\n",
236 GNUNET_i2s (&address->peer));
238 /* Notify scheduling clients about suggestion */
239 GAS_scheduling_transmit_address_suggestion (&address->peer,
241 GNUNET_BANDWIDTH_ZERO,
242 GNUNET_BANDWIDTH_ZERO);
246 /* Do bandwidth stability check */
247 diff_out = abs (address->assigned_bw_out - address->last_notified_bw_out);
248 diff_in = abs (address->assigned_bw_in - address->last_notified_bw_in);
250 if ( (diff_out < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
251 (diff_in < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
254 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
255 "Sending bandwidth update for peer `%s': %u %u\n",
256 GNUNET_i2s (&address->peer), address->assigned_bw_out,
257 address->assigned_bw_out);
259 /* *Notify scheduling clients about suggestion */
260 GAS_scheduling_transmit_address_suggestion (&address->peer,
262 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
263 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
265 address->last_notified_bw_out = address->assigned_bw_out;
266 address->last_notified_bw_in = address->assigned_bw_in;
271 * Load quotas for networks from configuration
273 * @param cfg configuration handle
274 * @param out_dest where to write outbound quotas
275 * @param in_dest where to write inbound quotas
276 * @param dest_length length of inbound and outbound arrays
277 * @return number of networks loaded
280 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
281 unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
283 char * entry_in = NULL;
284 char * entry_out = NULL;
285 char * quota_out_str;
290 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
294 GNUNET_asprintf (&entry_out,
296 GNUNET_ATS_print_network_type (c));
297 GNUNET_asprintf (&entry_in,
299 GNUNET_ATS_print_network_type (c));
303 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
307 if (0 == strcmp (quota_out_str, GNUNET_ATS_MaxBandwidthString))
309 out_dest[c] = GNUNET_ATS_MaxBandwidth;
312 if ((GNUNET_NO == res)
314 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
317 if ((GNUNET_NO == res)
319 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,
323 if (GNUNET_NO == res)
325 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
326 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
327 GNUNET_ATS_print_network_type (c),
329 GNUNET_ATS_DefaultBandwidth);
330 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
334 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
335 _("Outbound quota configure for network `%s' is %llu\n"),
336 GNUNET_ATS_print_network_type (c),
339 GNUNET_free(quota_out_str);
343 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
344 _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
345 GNUNET_ATS_print_network_type (c),
346 GNUNET_ATS_DefaultBandwidth);
347 out_dest[c] = GNUNET_ATS_DefaultBandwidth;
352 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
356 if (0 == strcmp (quota_in_str, GNUNET_ATS_MaxBandwidthString))
358 in_dest[c] = GNUNET_ATS_MaxBandwidth;
361 if ((GNUNET_NO == res)
363 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
365 if ((GNUNET_NO == res)
367 == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,
371 if (GNUNET_NO == res)
373 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
374 _("Could not load quota for network `%s': `%s', assigning default bandwidth %llu\n"),
375 GNUNET_ATS_print_network_type (c),
377 GNUNET_ATS_DefaultBandwidth);
378 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
382 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
383 _("Inbound quota configured for network `%s' is %llu\n"),
384 GNUNET_ATS_print_network_type (c),
387 GNUNET_free(quota_in_str);
391 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
392 _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
393 GNUNET_ATS_print_network_type (c),
394 GNUNET_ATS_DefaultBandwidth);
395 in_dest[c] = GNUNET_ATS_DefaultBandwidth;
397 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
398 "Loaded quota for network `%s' (in/out): %llu %llu\n",
399 GNUNET_ATS_print_network_type (c),
402 GNUNET_free(entry_out);
403 GNUNET_free(entry_in);
405 return GNUNET_ATS_NetworkTypeCount;
410 * Initialize plugins subsystem.
412 * @param cfg configuration to use
413 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
417 GAS_plugins_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
419 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
420 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
425 /* Figure out configured solution method */
427 GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
429 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
430 "No resource assignment method configured, using proportional approach\n");
431 ats_mode = MODE_PROPORTIONAL;
435 for (c = 0; c < strlen (mode_str); c++)
436 mode_str[c] = toupper (mode_str[c]);
437 if (0 == strcmp (mode_str, "PROPORTIONAL"))
438 ats_mode = MODE_PROPORTIONAL;
439 else if (0 == strcmp (mode_str, "MLP"))
443 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
444 "Assignment method `%s' configured, but GLPK is not available, please install \n",
446 ats_mode = MODE_PROPORTIONAL;
449 else if (0 == strcmp (mode_str, "RIL"))
453 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
454 "Invalid resource assignment method `%s' configured, using proportional approach\n",
456 ats_mode = MODE_PROPORTIONAL;
458 GNUNET_free(mode_str);
461 load_quotas (cfg, quotas_out, quotas_in, GNUNET_ATS_NetworkTypeCount);
462 env.info_cb = &solver_info_cb;
463 env.info_cb_cls = NULL;
464 env.bandwidth_changed_cb = &bandwidth_changed_cb;
465 env.bw_changed_cb_cls = NULL;
466 env.get_preferences = &GAS_normalization_get_preferences_by_peer;
467 env.get_preference_cls = NULL;
468 env.get_property = &GAS_normalization_get_properties;
469 env.get_property_cls = NULL;
471 env.stats = GSA_stats;
472 env.addresses = GSA_addresses;
474 env.network_count = GNUNET_ATS_NetworkTypeCount;
475 int networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
476 for (c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
478 env.networks[c] = networks[c];
479 env.out_quota[c] = quotas_out[c];
480 env.in_quota[c] = quotas_in[c];
484 case MODE_PROPORTIONAL:
485 plugin_short = "proportional";
488 plugin_short = "mlp";
491 plugin_short = "ril";
497 GNUNET_asprintf (&plugin,
498 "libgnunet_plugin_ats_%s",
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Initializing solver `%s '`%s'\n",
504 if (NULL == (solver = GNUNET_PLUGIN_load (plugin, &env)))
506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
507 _("Failed to initialize solver `%s'!\n"),
509 return GNUNET_SYSERR;
513 GNUNET_assert (NULL != env.sf.s_add);
514 GNUNET_assert (NULL != env.sf.s_address_update_property);
515 GNUNET_assert (NULL != env.sf.s_get);
516 GNUNET_assert (NULL != env.sf.s_get_stop);
517 GNUNET_assert (NULL != env.sf.s_pref);
518 GNUNET_assert (NULL != env.sf.s_feedback);
519 GNUNET_assert (NULL != env.sf.s_del);
520 GNUNET_assert (NULL != env.sf.s_bulk_start);
521 GNUNET_assert (NULL != env.sf.s_bulk_stop);
525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
526 _("Failed to initialize solver!\n"));
527 return GNUNET_SYSERR;
534 * Shutdown address subsystem.
539 GNUNET_PLUGIN_unload (plugin,
542 GNUNET_free (plugin);
548 GAS_plugin_new_address (struct ATS_Address *new_address,
549 enum GNUNET_ATS_Network_Type addr_net,
550 const struct GNUNET_ATS_Information *atsi,
553 env.sf.s_add (solver, new_address, addr_net);
554 env.sf.s_bulk_start (solver);
555 GAS_normalization_normalize_property (new_address,
558 env.sf.s_bulk_stop (solver);
563 GAS_plugin_update_address (struct ATS_Address *address,
564 const struct GNUNET_ATS_Information *atsi,
567 env.sf.s_bulk_start (solver);
568 GAS_normalization_normalize_property (address,
571 env.sf.s_bulk_stop (solver);
576 GAS_plugin_delete_address (struct ATS_Address *address)
578 env.sf.s_del (solver, address, GNUNET_NO);
583 GAS_plugin_update_preferences (void *client,
584 const struct GNUNET_PeerIdentity *peer,
585 enum GNUNET_ATS_PreferenceKind kind,
588 env.sf.s_bulk_start (solver);
589 /* Tell normalization about change, normalization will call callback if preference changed */
590 GAS_normalization_normalize_preference (client, peer, kind, score_abs);
591 env.sf.s_bulk_stop (solver);
596 GAS_plugin_preference_feedback (void *application,
597 const struct GNUNET_PeerIdentity *peer,
598 const struct GNUNET_TIME_Relative scope,
599 enum GNUNET_ATS_PreferenceKind kind,
602 env.sf.s_feedback (solver,
612 GAS_plugin_solver_lock ()
614 env.sf.s_bulk_start (solver);
619 GAS_plugin_solver_unlock ()
621 env.sf.s_bulk_start (solver);
626 GAS_plugin_request_connect_start (const struct GNUNET_PeerIdentity *pid)
628 const struct ATS_Address *aa;
630 aa = env.sf.s_get (solver, pid);
633 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
634 "Cannot suggest address for peer `%s'\n",
638 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
639 "Suggesting address %p for peer `%s'\n",
643 GAS_scheduling_transmit_address_suggestion (pid,
645 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
646 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
648 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
649 "Address %p ready for suggestion\n",
655 GAS_plugin_request_connect_stop (const struct GNUNET_PeerIdentity *pid)
657 env.sf.s_get_stop (solver, pid);
661 /* end of gnunet-service-ats_plugins.c */