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_reservations.h"
34 #include "gnunet-service-ats_scheduling.h"
35 #include "gnunet-service-ats_normalization.h"
41 static struct GNUNET_ATS_SolverFunctions *sf;
46 static struct GNUNET_ATS_PluginEnvironment env;
49 * Solver plugin name as string
55 * The preference changed for a peer, update solver.
57 * @param peer the peer
58 * @param kind the ATS kind
59 * @param pref_rel the new relative preference value
62 GAS_plugin_notify_preference_changed (const struct GNUNET_PeerIdentity *peer,
63 enum GNUNET_ATS_PreferenceKind kind,
74 * The relative value for a property changed.
76 * @param address the peer for which a property changed
79 GAS_plugin_notify_property_changed (struct ATS_Address *address)
81 sf->s_address_update_property (sf->cls,
87 * Solver information callback
89 * @param cls the closure
90 * @param op the operation
91 * @param status operation status
92 * @param add additional information
95 solver_info_cb (void *cls,
96 enum GAS_Solver_Operation op,
97 enum GAS_Solver_Status status,
98 enum GAS_Solver_Additional_Information add)
100 const char *add_info;
104 add_info = "GAS_INFO_NONE";
107 add_info = "GAS_INFO_MLP_FULL";
109 case GAS_INFO_UPDATED:
110 add_info = "GAS_INFO_MLP_UPDATED";
112 case GAS_INFO_PROP_ALL:
113 add_info = "GAS_INFO_PROP_ALL";
115 case GAS_INFO_PROP_SINGLE:
116 add_info = "GAS_INFO_PROP_SINGLE";
119 add_info = "INVALID";
124 case GAS_OP_SOLVE_START:
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Solver notifies `%s' with result `%s' `%s'\n",
127 "GAS_OP_SOLVE_START",
128 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL",
131 case GAS_OP_SOLVE_STOP:
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Solver notifies `%s' with result `%s'\n",
135 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL",
138 case GAS_OP_SOLVE_SETUP_START:
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "Solver notifies `%s' with result `%s'\n",
141 "GAS_OP_SOLVE_SETUP_START",
142 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
144 case GAS_OP_SOLVE_SETUP_STOP:
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Solver notifies `%s' with result `%s'\n",
147 "GAS_OP_SOLVE_SETUP_STOP",
148 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
150 case GAS_OP_SOLVE_MLP_LP_START:
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Solver notifies `%s' with result `%s'\n",
153 "GAS_OP_SOLVE_LP_START",
154 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
156 case GAS_OP_SOLVE_MLP_LP_STOP:
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Solver notifies `%s' with result `%s'\n",
159 "GAS_OP_SOLVE_LP_STOP",
160 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
162 case GAS_OP_SOLVE_MLP_MLP_START:
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Solver notifies `%s' with result `%s'\n",
165 "GAS_OP_SOLVE_MLP_START",
166 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
168 case GAS_OP_SOLVE_MLP_MLP_STOP:
169 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
170 "Solver notifies `%s' with result `%s'\n",
171 "GAS_OP_SOLVE_MLP_STOP",
172 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
174 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_START:
175 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
176 "Solver notifies `%s' with result `%s'\n",
177 "GAS_OP_SOLVE_UPDATE_NOTIFICATION_START",
178 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
180 case GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP:
181 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
182 "Solver notifies `%s' with result `%s'\n",
183 "GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP",
184 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL");
194 * Callback for solver to notify about assignment changes
197 * @param address the address with changes
200 bandwidth_changed_cb (void *cls,
201 struct ATS_Address *address)
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Bandwidth assignment changed for peer %s to %u/%u\n",
208 GNUNET_i2s (&address->peer),
209 (unsigned int) address->assigned_bw_in,
210 (unsigned int) address->assigned_bw_out);
211 GAS_reservations_set_bandwidth (&address->peer,
212 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
213 /* Notify performance clients about changes to address */
214 GAS_performance_notify_all_clients (&address->peer,
219 &address->properties,
220 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
221 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
223 if ( (0 == address->assigned_bw_in) &&
224 (0 == address->assigned_bw_out) )
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Telling transport to disconnect peer `%s'\n",
228 GNUNET_i2s (&address->peer));
230 /* Notify scheduling clients about suggestion */
231 GAS_scheduling_transmit_address_suggestion (&address->peer,
233 GNUNET_BANDWIDTH_ZERO,
234 GNUNET_BANDWIDTH_ZERO);
238 /* Do bandwidth stability check */
239 diff_out = llabs ((long long) address->assigned_bw_out -
240 (long long) address->last_notified_bw_out);
241 diff_in = llabs ((long long) address->assigned_bw_in -
242 (long long) address->last_notified_bw_in);
243 if ( (diff_out < htonl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) &&
244 (diff_in < htonl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__)) )
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Bandwidth change too small, not notifying client\n");
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Sending bandwidth update for peer `%s': %u/%u\n",
253 GNUNET_i2s (&address->peer),
254 address->assigned_bw_out,
255 address->assigned_bw_out);
257 /* *Notify scheduling clients about suggestion */
258 GAS_scheduling_transmit_address_suggestion (&address->peer,
260 GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
261 GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
263 address->last_notified_bw_out = address->assigned_bw_out;
264 address->last_notified_bw_in = address->assigned_bw_in;
269 * Convert quota from text to numeric value.
271 * @param quota_str the value found in the configuration
272 * @param direction direction of the quota
273 * @param network network the quota applies to
274 * @return numeric quota value to use
276 static unsigned long long
277 parse_quota (const char *quota_str,
278 const char *direction,
279 enum GNUNET_ATS_Network_Type network)
282 unsigned long long ret;
285 if (0 == strcmp (quota_str, GNUNET_ATS_MaxBandwidthString))
287 ret = GNUNET_ATS_MaxBandwidth;
290 if ((GNUNET_NO == res) &&
292 GNUNET_STRINGS_fancy_size_to_bytes (quota_str,
295 if ((GNUNET_NO == res) &&
301 if (GNUNET_NO == res)
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
304 _("Could not load %s quota for network `%s': `%s', assigning default bandwidth %llu\n"),
306 GNUNET_ATS_print_network_type (network),
308 GNUNET_ATS_DefaultBandwidth);
309 ret = GNUNET_ATS_DefaultBandwidth;
313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
314 _("%s quota configured for network `%s' is %llu\n"),
316 GNUNET_ATS_print_network_type (network),
324 * Load quota value from the configuration @a cfg for the
325 * given network @a type and @a direction.
327 * @param cfg configuration to parse
328 * @param type network type to parse for
329 * @param direction traffic direction to parse for
330 * @return quota to apply
332 static unsigned long long
333 load_quota (const struct GNUNET_CONFIGURATION_Handle *cfg,
334 enum GNUNET_ATS_Network_Type type,
335 const char *direction)
339 unsigned long long ret;
341 GNUNET_asprintf (&entry,
343 GNUNET_ATS_print_network_type (type),
346 GNUNET_CONFIGURATION_get_value_string (cfg,
351 ret = parse_quota (quota_str,
354 GNUNET_free (quota_str);
358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
359 _("No %s-quota configured for network `%s', assigning default bandwidth %llu\n"),
361 GNUNET_ATS_print_network_type (type),
362 GNUNET_ATS_DefaultBandwidth);
363 ret = GNUNET_ATS_DefaultBandwidth;
371 * Load quotas for networks from configuration
373 * @param cfg configuration handle
374 * @param out_dest where to write outbound quotas
375 * @param in_dest where to write inbound quotas
376 * @param dest_length length of inbound and outbound arrays
377 * @return number of networks loaded
380 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
381 unsigned long long *out_dest,
382 unsigned long long *in_dest,
387 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
389 in_dest[c] = load_quota (cfg,
392 out_dest[c] = load_quota (cfg,
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Loaded quota for network `%s' (in/out): %llu %llu\n",
397 GNUNET_ATS_print_network_type (c),
406 * Initialize plugins subsystem.
408 * @param cfg configuration to use
409 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
413 GAS_plugin_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
417 /* Figure out configured solution method */
419 GNUNET_CONFIGURATION_get_value_string (cfg,
424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
425 "No resource assignment method configured, using proportional approach\n");
426 mode_str = GNUNET_strdup ("proportional");
429 env.info_cb = &solver_info_cb;
430 env.bandwidth_changed_cb = &bandwidth_changed_cb;
431 env.get_preferences = &GAS_preference_get_by_peer;
432 env.get_connectivity = &GAS_connectivity_has_peer;
434 env.stats = GSA_stats;
435 env.addresses = GSA_addresses;
436 env.network_count = GNUNET_ATS_NetworkTypeCount;
440 GNUNET_ATS_NetworkTypeCount);
441 GNUNET_asprintf (&plugin,
442 "libgnunet_plugin_ats_%s",
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Initializing solver `%s'\n",
447 GNUNET_free (mode_str);
448 if (NULL == (sf = GNUNET_PLUGIN_load (plugin, &env)))
450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451 _("Failed to initialize solver `%s'!\n"),
453 return GNUNET_SYSERR;
460 * Shutdown address subsystem.
465 GNUNET_PLUGIN_unload (plugin,
468 GNUNET_free (plugin);
474 * Tell the solver that the given address can now be used
475 * for talking to the respective peer.
477 * @param new_address the new address
480 GAS_plugin_new_address (struct ATS_Address *new_address)
484 new_address->properties.scope); /* FIXME: remove 3rd arg here! */
489 * Tell the solver that the given address is no longer valid
490 * can cannot be used any longer.
492 * @param address address that was deleted
495 GAS_plugin_delete_address (struct ATS_Address *address)
503 * Tell the solver that the given client has expressed its
504 * appreciation for the past performance of a given connection.
506 * @param application client providing the feedback
507 * @param peer peer the feedback is about
508 * @param scope timeframe the feedback applies to
509 * @param kind performance property the feedback relates to
510 * @param score_abs degree of the appreciation
513 GAS_plugin_notify_feedback (struct GNUNET_SERVER_Client *application,
514 const struct GNUNET_PeerIdentity *peer,
515 const struct GNUNET_TIME_Relative scope,
516 enum GNUNET_ATS_PreferenceKind kind,
519 sf->s_feedback (sf->cls,
529 * Stop instant solving, there are many state updates
530 * happening in bulk right now.
533 GAS_plugin_solver_lock ()
535 sf->s_bulk_start (sf->cls);
540 * Resume instant solving, we are done with the bulk state updates.
543 GAS_plugin_solver_unlock ()
545 sf->s_bulk_stop (sf->cls);
550 * Notify the plugin that a request to connect to
551 * a particular peer was given to us.
553 * @param pid identity of peer we now care about
556 GAS_plugin_request_connect_start (const struct GNUNET_PeerIdentity *pid)
564 * Notify the plugin that a request to connect to
565 * a particular peer was dropped.
567 * @param pid identity of peer we care now less about
570 GAS_plugin_request_connect_stop (const struct GNUNET_PeerIdentity *pid)
572 sf->s_get_stop (sf->cls,
577 /* end of gnunet-service-ats_plugins.c */