2 This file is part of GNUnet.
3 Copyright (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_ats_plugin.h"
29 #include "gnunet-service-ats_connectivity.h"
30 #include "gnunet-service-ats_performance.h"
31 #include "gnunet-service-ats_preferences.h"
32 #include "gnunet-service-ats_plugins.h"
33 #include "gnunet-service-ats_scheduling.h"
34 #include "gnunet-service-ats_normalization.h"
40 static struct GNUNET_ATS_SolverFunctions *sf;
45 static struct GNUNET_ATS_PluginEnvironment env;
48 * Solver plugin name as string
54 * The preference changed for a peer, update solver.
56 * @param peer the peer
57 * @param kind the ATS kind
58 * @param pref_rel the new relative preference value
61 GAS_plugin_notify_preference_changed (const struct GNUNET_PeerIdentity *peer,
62 enum GNUNET_ATS_PreferenceKind kind,
73 * The relative value for a property changed
75 * @param address the peer
76 * @param type the ATS type
77 * @param prop_rel the new relative preference value
80 GAS_plugin_notify_property_changed (struct ATS_Address *address,
81 enum GNUNET_ATS_Property type,
84 sf->s_address_update_property (sf->cls,
93 * Solver information callback
95 * @param cls the closure
96 * @param op the operation
97 * @param status operation status
98 * @param add additional information
101 solver_info_cb (void *cls,
102 enum GAS_Solver_Operation op,
103 enum GAS_Solver_Status status,
104 enum GAS_Solver_Additional_Information add)
106 const char *add_info;
110 add_info = "GAS_INFO_NONE";
113 add_info = "GAS_INFO_MLP_FULL";
115 case GAS_INFO_UPDATED:
116 add_info = "GAS_INFO_MLP_UPDATED";
118 case GAS_INFO_PROP_ALL:
119 add_info = "GAS_INFO_PROP_ALL";
121 case GAS_INFO_PROP_SINGLE:
122 add_info = "GAS_INFO_PROP_SINGLE";
125 add_info = "INVALID";
130 case GAS_OP_SOLVE_START:
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Solver notifies `%s' with result `%s' `%s'\n",
133 "GAS_OP_SOLVE_START",
134 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL",
137 case GAS_OP_SOLVE_STOP:
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "Solver notifies `%s' with result `%s'\n",
141 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL",
144 case GAS_OP_SOLVE_SETUP_START:
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Solver notifies `%s' with result `%s'\n",
147 "GAS_OP_SOLVE_SETUP_START",
148 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
150 case GAS_OP_SOLVE_SETUP_STOP:
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Solver notifies `%s' with result `%s'\n",
153 "GAS_OP_SOLVE_SETUP_STOP",
154 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
156 case GAS_OP_SOLVE_MLP_LP_START:
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Solver notifies `%s' with result `%s'\n",
159 "GAS_OP_SOLVE_LP_START",
160 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
162 case GAS_OP_SOLVE_MLP_LP_STOP:
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Solver notifies `%s' with result `%s'\n",
165 "GAS_OP_SOLVE_LP_STOP",
166 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
168 case GAS_OP_SOLVE_MLP_MLP_START:
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Solver notifies `%s' with result `%s'\n",
171 "GAS_OP_SOLVE_MLP_START",
172 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
174 case GAS_OP_SOLVE_MLP_MLP_STOP:
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
176 "Solver notifies `%s' with result `%s'\n",
177 "GAS_OP_SOLVE_MLP_STOP",
178 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
180 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
182 "Solver notifies `%s' with result `%s'\n",
183 "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
184 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
186 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
188 "Solver notifies `%s' with result `%s'\n",
189 "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
190 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
200 * Callback for solver to notify about assignment changes
203 * @param address the address with changes
206 bandwidth_changed_cb (void *cls,
207 struct ATS_Address *address)
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Bandwidth assignment changed for peer %s to %u/%u\n",
214 GNUNET_i2s (&address->peer),
215 (unsigned int) address->assigned_bw_in,
216 (unsigned int) address->assigned_bw_out);
218 /* Notify performance clients about changes to address */
219 GAS_performance_notify_all_clients (&address->peer,
226 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
227 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
229 if ( (0 == address->assigned_bw_in) &&
230 (0 == address->assigned_bw_out) )
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Telling transport to disconnect peer `%s'\n",
234 GNUNET_i2s (&address->peer));
236 /* Notify scheduling clients about suggestion */
237 GAS_scheduling_transmit_address_suggestion (&address->peer,
239 GNUNET_BANDWIDTH_ZERO,
240 GNUNET_BANDWIDTH_ZERO);
244 /* Do bandwidth stability check */
245 diff_out = llabs ((long long) address->assigned_bw_out -
246 (long long) address->last_notified_bw_out);
247 diff_in = llabs ((long long) address->assigned_bw_in -
248 (long long) address->last_notified_bw_in);
249 if ( (diff_out < htonl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
250 (diff_in < htonl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Bandwidth change too small, not notifying client\n");
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Sending bandwidth update for peer `%s': %u/%u\n",
259 GNUNET_i2s (&address->peer),
260 address->assigned_bw_out,
261 address->assigned_bw_out);
263 /* *Notify scheduling clients about suggestion */
264 GAS_scheduling_transmit_address_suggestion (&address->peer,
266 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
267 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
269 address->last_notified_bw_out = address->assigned_bw_out;
270 address->last_notified_bw_in = address->assigned_bw_in;
275 * Convert quota from text to numeric value.
277 * @param quota_str the value found in the configuration
278 * @param direction direction of the quota
279 * @param network network the quota applies to
280 * @return numeric quota value to use
282 static unsigned long long
283 parse_quota (const char *quota_str,
284 const char *direction,
285 enum GNUNET_ATS_Network_Type network)
288 unsigned long long ret;
291 if (0 == strcmp (quota_str, GNUNET_ATS_MaxBandwidthString))
293 ret = GNUNET_ATS_MaxBandwidth;
296 if ((GNUNET_NO == res) &&
298 GNUNET_STRINGS_fancy_size_to_bytes (quota_str,
301 if ((GNUNET_NO == res) &&
307 if (GNUNET_NO == res)
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 _("Could not load %s quota for network `%s': `%s', assigning default bandwidth %llu\n"),
312 GNUNET_ATS_print_network_type (network),
314 GNUNET_ATS_DefaultBandwidth);
315 ret = GNUNET_ATS_DefaultBandwidth;
319 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
320 _("%s quota configured for network `%s' is %llu\n"),
322 GNUNET_ATS_print_network_type (network),
330 * Load quota value from the configuration @a cfg for the
331 * given network @a type and @a direction.
333 * @param cfg configuration to parse
334 * @param type network type to parse for
335 * @param direction traffic direction to parse for
336 * @return quota to apply
338 static unsigned long long
339 load_quota (const struct GNUNET_CONFIGURATION_Handle *cfg,
340 enum GNUNET_ATS_Network_Type type,
341 const char *direction)
345 unsigned long long ret;
347 GNUNET_asprintf (&entry,
349 GNUNET_ATS_print_network_type (type),
352 GNUNET_CONFIGURATION_get_value_string (cfg,
357 ret = parse_quota (quota_str,
360 GNUNET_free (quota_str);
364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
365 _("No %s-quota configured for network `%s', assigning default bandwidth %llu\n"),
367 GNUNET_ATS_print_network_type (type),
368 GNUNET_ATS_DefaultBandwidth);
369 ret = GNUNET_ATS_DefaultBandwidth;
377 * Load quotas for networks from configuration
379 * @param cfg configuration handle
380 * @param out_dest where to write outbound quotas
381 * @param in_dest where to write inbound quotas
382 * @param dest_length length of inbound and outbound arrays
383 * @return number of networks loaded
386 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
387 unsigned long long *out_dest,
388 unsigned long long *in_dest,
393 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
395 in_dest[c] = load_quota (cfg,
398 out_dest[c] = load_quota (cfg,
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Loaded quota for network `%s' (in/out): %llu %llu\n",
403 GNUNET_ATS_print_network_type (c),
412 * Initialize plugins subsystem.
414 * @param cfg configuration to use
415 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
419 GAS_plugin_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
423 /* Figure out configured solution method */
425 GNUNET_CONFIGURATION_get_value_string (cfg,
430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
431 "No resource assignment method configured, using proportional approach\n");
432 mode_str = GNUNET_strdup ("proportional");
435 env.info_cb = &solver_info_cb;
436 env.bandwidth_changed_cb = &bandwidth_changed_cb;
437 env.get_preferences = &GAS_preference_get_by_peer;
438 env.get_connectivity = &GAS_connectivity_has_peer;
440 env.stats = GSA_stats;
441 env.addresses = GSA_addresses;
442 env.network_count = GNUNET_ATS_NetworkTypeCount;
446 GNUNET_ATS_NetworkTypeCount);
447 GNUNET_asprintf (&plugin,
448 "libgnunet_plugin_ats_%s",
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Initializing solver `%s'\n",
453 GNUNET_free (mode_str);
454 if (NULL == (sf = GNUNET_PLUGIN_load (plugin, &env)))
456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
457 _("Failed to initialize solver `%s'!\n"),
459 return GNUNET_SYSERR;
466 * Shutdown address subsystem.
471 GNUNET_PLUGIN_unload (plugin,
474 GNUNET_free (plugin);
480 * Tell the solver that the given address can now be used
481 * for talking to the respective peer.
483 * @param new_address the new address
484 * @param addr_net network scope the address is in
487 GAS_plugin_new_address (struct ATS_Address *new_address,
488 enum GNUNET_ATS_Network_Type addr_net)
497 * Tell the solver that the given address is no longer valid
498 * can cannot be used any longer.
500 * @param address address that was deleted
503 GAS_plugin_delete_address (struct ATS_Address *address)
511 * Tell the solver that the given client has expressed its
512 * appreciation for the past performance of a given connection.
514 * @param application client providing the feedback
515 * @param peer peer the feedback is about
516 * @param scope timeframe the feedback applies to
517 * @param kind performance property the feedback relates to
518 * @param score_abs degree of the appreciation
521 GAS_plugin_notify_feedback (struct GNUNET_SERVER_Client *application,
522 const struct GNUNET_PeerIdentity *peer,
523 const struct GNUNET_TIME_Relative scope,
524 enum GNUNET_ATS_PreferenceKind kind,
527 sf->s_feedback (sf->cls,
537 * Stop instant solving, there are many state updates
538 * happening in bulk right now.
541 GAS_plugin_solver_lock ()
543 sf->s_bulk_start (sf->cls);
548 * Resume instant solving, we are done with the bulk state updates.
551 GAS_plugin_solver_unlock ()
553 sf->s_bulk_stop (sf->cls);
558 * Notify the plugin that a request to connect to
559 * a particular peer was given to us.
561 * @param pid identity of peer we now care about
564 GAS_plugin_request_connect_start (const struct GNUNET_PeerIdentity *pid)
572 * Notify the plugin that a request to connect to
573 * a particular peer was dropped.
575 * @param pid identity of peer we care now less about
578 GAS_plugin_request_connect_stop (const struct GNUNET_PeerIdentity *pid)
580 sf->s_get_stop (sf->cls,
585 /* end of gnunet-service-ats_plugins.c */