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");
199 * Callback for solver to notify about assignment changes
202 * @param address the address with changes
205 bandwidth_changed_cb (void *cls,
206 struct ATS_Address *address)
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Bandwidth assignment changed for peer %s \n",
213 GNUNET_i2s (&address->peer));
215 /* Notify performance clients about changes to address */
216 GAS_performance_notify_all_clients (&address->peer,
223 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
224 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
226 if ( (0 == address->assigned_bw_in) &&
227 (0 == address->assigned_bw_out) )
229 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
230 "Telling transport to disconnect peer `%s'\n",
231 GNUNET_i2s (&address->peer));
233 /* Notify scheduling clients about suggestion */
234 GAS_scheduling_transmit_address_suggestion (&address->peer,
236 GNUNET_BANDWIDTH_ZERO,
237 GNUNET_BANDWIDTH_ZERO);
241 /* Do bandwidth stability check */
242 diff_out = abs (address->assigned_bw_out - address->last_notified_bw_out);
243 diff_in = abs (address->assigned_bw_in - address->last_notified_bw_in);
245 if ( (diff_out < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
246 (diff_in < htonl(GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
249 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
250 "Sending bandwidth update for peer `%s': %u/%u\n",
251 GNUNET_i2s (&address->peer),
252 address->assigned_bw_out,
253 address->assigned_bw_out);
255 /* *Notify scheduling clients about suggestion */
256 GAS_scheduling_transmit_address_suggestion (&address->peer,
258 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
259 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
261 address->last_notified_bw_out = address->assigned_bw_out;
262 address->last_notified_bw_in = address->assigned_bw_in;
267 * Convert quota from text to numeric value.
269 * @param quota_str the value found in the configuration
270 * @param direction direction of the quota
271 * @param network network the quota applies to
272 * @return numeric quota value to use
274 static unsigned long long
275 parse_quota (const char *quota_str,
276 const char *direction,
277 enum GNUNET_ATS_Network_Type network)
280 unsigned long long ret;
283 if (0 == strcmp (quota_str, GNUNET_ATS_MaxBandwidthString))
285 ret = GNUNET_ATS_MaxBandwidth;
288 if ((GNUNET_NO == res) &&
290 GNUNET_STRINGS_fancy_size_to_bytes (quota_str,
293 if ((GNUNET_NO == res) &&
299 if (GNUNET_NO == res)
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
302 _("Could not load %s quota for network `%s': `%s', assigning default bandwidth %llu\n"),
304 GNUNET_ATS_print_network_type (network),
306 GNUNET_ATS_DefaultBandwidth);
307 ret = GNUNET_ATS_DefaultBandwidth;
311 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
312 _("%s quota configured for network `%s' is %llu\n"),
314 GNUNET_ATS_print_network_type (network),
322 * Load quota value from the configuration @a cfg for the
323 * given network @a type and @a direction.
325 * @param cfg configuration to parse
326 * @param type network type to parse for
327 * @param direction traffic direction to parse for
328 * @return quota to apply
330 static unsigned long long
331 load_quota (const struct GNUNET_CONFIGURATION_Handle *cfg,
332 enum GNUNET_ATS_Network_Type type,
333 const char *direction)
337 unsigned long long ret;
339 GNUNET_asprintf (&entry,
341 GNUNET_ATS_print_network_type (type),
344 GNUNET_CONFIGURATION_get_value_string (cfg,
349 ret = parse_quota (quota_str,
352 GNUNET_free (quota_str);
356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
357 _("No %s-quota configured for network `%s', assigning default bandwidth %llu\n"),
359 GNUNET_ATS_print_network_type (type),
360 GNUNET_ATS_DefaultBandwidth);
361 ret = GNUNET_ATS_DefaultBandwidth;
369 * Load quotas for networks from configuration
371 * @param cfg configuration handle
372 * @param out_dest where to write outbound quotas
373 * @param in_dest where to write inbound quotas
374 * @param dest_length length of inbound and outbound arrays
375 * @return number of networks loaded
378 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
379 unsigned long long *out_dest,
380 unsigned long long *in_dest,
385 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
387 in_dest[c] = load_quota (cfg,
390 out_dest[c] = load_quota (cfg,
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Loaded quota for network `%s' (in/out): %llu %llu\n",
395 GNUNET_ATS_print_network_type (c),
404 * Initialize plugins subsystem.
406 * @param cfg configuration to use
407 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
411 GAS_plugin_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
415 /* Figure out configured solution method */
417 GNUNET_CONFIGURATION_get_value_string (cfg,
422 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
423 "No resource assignment method configured, using proportional approach\n");
424 mode_str = GNUNET_strdup ("proportional");
427 env.info_cb = &solver_info_cb;
428 env.bandwidth_changed_cb = &bandwidth_changed_cb;
429 env.get_preferences = &GAS_preference_get_by_peer;
431 env.stats = GSA_stats;
432 env.addresses = GSA_addresses;
433 env.network_count = GNUNET_ATS_NetworkTypeCount;
437 GNUNET_ATS_NetworkTypeCount);
438 GNUNET_asprintf (&plugin,
439 "libgnunet_plugin_ats_%s",
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Initializing solver `%s'\n",
444 GNUNET_free (mode_str);
445 if (NULL == (sf = GNUNET_PLUGIN_load (plugin, &env)))
447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448 _("Failed to initialize solver `%s'!\n"),
450 return GNUNET_SYSERR;
457 * Shutdown address subsystem.
462 GNUNET_PLUGIN_unload (plugin,
465 GNUNET_free (plugin);
471 * Tell the solver that the given address can now be used
472 * for talking to the respective peer.
474 * @param new_address the new address
475 * @param addr_net network scope the address is in
476 * @param atsi performance data for the address
477 * @param atsi_count size of the @a atsi array
480 GAS_plugin_new_address (struct ATS_Address *new_address,
481 enum GNUNET_ATS_Network_Type addr_net,
482 const struct GNUNET_ATS_Information *atsi,
488 sf->s_bulk_start (sf->cls);
489 GAS_normalization_update_property (new_address,
492 sf->s_bulk_stop (sf->cls);
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)
512 * Tell the solver that the given client has expressed its
513 * appreciation for the past performance of a given connection.
515 * @param application client providing the feedback
516 * @param peer peer the feedback is about
517 * @param scope timeframe the feedback applies to
518 * @param kind performance property the feedback relates to
519 * @param score_abs degree of the appreciation
522 GAS_plugin_notify_feedback (struct GNUNET_SERVER_Client *application,
523 const struct GNUNET_PeerIdentity *peer,
524 const struct GNUNET_TIME_Relative scope,
525 enum GNUNET_ATS_PreferenceKind kind,
528 sf->s_feedback (sf->cls,
538 * Stop instant solving, there are many state updates
539 * happening in bulk right now.
542 GAS_plugin_solver_lock ()
544 sf->s_bulk_start (sf->cls);
549 * Resume instant solving, we are done with the bulk state updates.
552 GAS_plugin_solver_unlock ()
554 sf->s_bulk_start (sf->cls);
559 * Notify the plugin that a request to connect to
560 * a particular peer was given to us.
562 * @param pid identity of peer we now care about
565 GAS_plugin_request_connect_start (const struct GNUNET_PeerIdentity *pid)
567 const struct ATS_Address *aa;
569 aa = sf->s_get (sf->cls, pid);
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573 "Cannot suggest address for peer `%s'\n",
577 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
578 "Suggesting address %p for peer `%s'\n",
581 GAS_scheduling_transmit_address_suggestion (pid,
583 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_out),
584 GNUNET_BANDWIDTH_value_init (aa->assigned_bw_in));
589 * Notify the plugin that a request to connect to
590 * a particular peer was dropped.
592 * @param pid identity of peer we care now less about
595 GAS_plugin_request_connect_stop (const struct GNUNET_PeerIdentity *pid)
597 sf->s_get_stop (sf->cls,
602 /* end of gnunet-service-ats_plugins.c */