-improve UDP logging
[oweals/gnunet.git] / src / ats / gnunet-service-ats_plugins.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file ats/gnunet-service-ats_plugins.c
23  * @brief ats service plugin management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
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"
35
36
37 /**
38  * Solver handle.
39  */
40 static struct GNUNET_ATS_SolverFunctions *sf;
41
42 /**
43  * Solver environment.
44  */
45 static struct GNUNET_ATS_PluginEnvironment env;
46
47 /**
48  * Solver plugin name as string
49  */
50 static char *plugin;
51
52
53 /**
54  * The preference changed for a peer, update solver.
55  *
56  * @param peer the peer
57  * @param kind the ATS kind
58  * @param pref_rel the new relative preference value
59  */
60 void
61 GAS_plugin_notify_preference_changed (const struct GNUNET_PeerIdentity *peer,
62                                       enum GNUNET_ATS_PreferenceKind kind,
63                                       double pref_rel)
64 {
65   sf->s_pref (sf->cls,
66               peer,
67               kind,
68               pref_rel);
69 }
70
71
72 /**
73  * The relative value for a property changed
74  *
75  * @param address the peer
76  * @param type the ATS type
77  * @param prop_rel the new relative preference value
78  */
79 void
80 GAS_plugin_notify_property_changed (struct ATS_Address *address,
81                                     enum GNUNET_ATS_Property type,
82                                     double prop_rel)
83 {
84   sf->s_address_update_property (sf->cls,
85                                  address,
86                                  type,
87                                  0,
88                                  prop_rel);
89 }
90
91
92 /**
93  * Solver information callback
94  *
95  * @param cls the closure
96  * @param op the operation
97  * @param status operation status
98  * @param add additional information
99  */
100 static void
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)
105 {
106   const char *add_info;
107
108   switch (add) {
109     case GAS_INFO_NONE:
110       add_info = "GAS_INFO_NONE";
111       break;
112     case GAS_INFO_FULL:
113       add_info = "GAS_INFO_MLP_FULL";
114       break;
115     case GAS_INFO_UPDATED:
116       add_info = "GAS_INFO_MLP_UPDATED";
117       break;
118     case GAS_INFO_PROP_ALL:
119       add_info = "GAS_INFO_PROP_ALL";
120       break;
121     case GAS_INFO_PROP_SINGLE:
122       add_info = "GAS_INFO_PROP_SINGLE";
123       break;
124     default:
125       add_info = "INVALID";
126       break;
127   }
128   switch (op)
129   {
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",
135                 add_info);
136     return;
137   case GAS_OP_SOLVE_STOP:
138     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139                 "Solver notifies `%s' with result `%s'\n",
140                 "GAS_OP_SOLVE_STOP",
141                 (GAS_STAT_SUCCESS == status) ? "SUCCESS" : "FAIL",
142                 add_info);
143     return;
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");
149     return;
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");
155     return;
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");
161     return;
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");
167     return;
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");
173     return;
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");
179     return;
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");
185     return;
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");
191     return;
192   default:
193     GNUNET_break (0);
194     break;
195   }
196 }
197
198
199 /**
200  * Callback for solver to notify about assignment changes
201  *
202  * @param cls NULL
203  * @param address the address with changes
204  */
205 static void
206 bandwidth_changed_cb (void *cls,
207                       struct ATS_Address *address)
208 {
209   long long diff_out;
210   long long diff_in;
211
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);
217
218   /* Notify performance clients about changes to address */
219   GAS_performance_notify_all_clients (&address->peer,
220                                       address->plugin,
221                                       address->addr,
222                                       address->addr_len,
223                                       address->active,
224                                       address->atsi,
225                                       address->atsi_count,
226                                       GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
227                                       GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
228
229   if ( (0 == address->assigned_bw_in) &&
230        (0 == address->assigned_bw_out) )
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233                "Telling transport to disconnect peer `%s'\n",
234                 GNUNET_i2s (&address->peer));
235
236     /* Notify scheduling clients about suggestion */
237     GAS_scheduling_transmit_address_suggestion (&address->peer,
238                                                 address->session_id,
239                                                 GNUNET_BANDWIDTH_ZERO,
240                                                 GNUNET_BANDWIDTH_ZERO);
241     return;
242   }
243
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__)) )
251   {
252     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253                 "Bandwidth change too small, not notifying client\n");
254     return;
255   }
256
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);
262
263   /* *Notify scheduling clients about suggestion */
264   GAS_scheduling_transmit_address_suggestion (&address->peer,
265                                               address->session_id,
266                                               GNUNET_BANDWIDTH_value_init (address->assigned_bw_out),
267                                               GNUNET_BANDWIDTH_value_init (address->assigned_bw_in));
268
269   address->last_notified_bw_out = address->assigned_bw_out;
270   address->last_notified_bw_in = address->assigned_bw_in;
271 }
272
273
274 /**
275  * Convert quota from text to numeric value.
276  *
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
281  */
282 static unsigned long long
283 parse_quota (const char *quota_str,
284              const char *direction,
285              enum GNUNET_ATS_Network_Type network)
286 {
287   int res;
288   unsigned long long ret;
289
290   res = GNUNET_NO;
291   if (0 == strcmp (quota_str, GNUNET_ATS_MaxBandwidthString))
292   {
293     ret = GNUNET_ATS_MaxBandwidth;
294     res = GNUNET_YES;
295   }
296   if ((GNUNET_NO == res) &&
297       (GNUNET_OK ==
298        GNUNET_STRINGS_fancy_size_to_bytes (quota_str,
299                                            &ret)))
300     res = GNUNET_YES;
301   if ((GNUNET_NO == res) &&
302       (1 ==
303        sscanf (quota_str,
304                "%llu",
305                &ret)))
306     res = GNUNET_YES;
307   if (GNUNET_NO == res)
308   {
309     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310                 _("Could not load %s quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
311                 direction,
312                 GNUNET_ATS_print_network_type (network),
313                 quota_str,
314                 GNUNET_ATS_DefaultBandwidth);
315     ret = GNUNET_ATS_DefaultBandwidth;
316   }
317   else
318   {
319     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
320                 _("%s quota configured for network `%s' is %llu\n"),
321                 direction,
322                 GNUNET_ATS_print_network_type (network),
323                 ret);
324   }
325   return ret;
326 }
327
328
329 /**
330  * Load quota value from the configuration @a cfg for the
331  * given network @a type and @a direction.
332  *
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
337  */
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)
342 {
343   char *entry;
344   char *quota_str;
345   unsigned long long ret;
346
347   GNUNET_asprintf (&entry,
348                    "%s_QUOTA_%s",
349                    GNUNET_ATS_print_network_type (type),
350                    direction);
351   if (GNUNET_OK ==
352       GNUNET_CONFIGURATION_get_value_string (cfg,
353                                              "ats",
354                                              entry,
355                                              &quota_str))
356   {
357     ret = parse_quota (quota_str,
358                        direction,
359                        type);
360     GNUNET_free (quota_str);
361   }
362   else
363   {
364     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
365                 _("No %s-quota configured for network `%s', assigning default bandwidth %llu\n"),
366                 direction,
367                 GNUNET_ATS_print_network_type (type),
368                 GNUNET_ATS_DefaultBandwidth);
369     ret = GNUNET_ATS_DefaultBandwidth;
370   }
371   GNUNET_free (entry);
372   return ret;
373 }
374
375
376 /**
377  * Load quotas for networks from configuration
378  *
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
384  */
385 static unsigned int
386 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg,
387              unsigned long long *out_dest,
388              unsigned long long *in_dest,
389              int dest_length)
390 {
391   unsigned int c;
392
393   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
394   {
395     in_dest[c] = load_quota (cfg,
396                              c,
397                              "out");
398     out_dest[c] = load_quota (cfg,
399                               c,
400                               "in");
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),
404                 in_dest[c],
405                 out_dest[c]);
406   }
407   return c;
408 }
409
410
411 /**
412  * Initialize plugins subsystem.
413  *
414  * @param cfg configuration to use
415  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (failed to load
416  *         solver plugin)
417  */
418 int
419 GAS_plugin_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
420 {
421   char *mode_str;
422
423   /* Figure out configured solution method */
424   if (GNUNET_SYSERR ==
425       GNUNET_CONFIGURATION_get_value_string (cfg,
426                                              "ats",
427                                              "MODE",
428                                              &mode_str))
429   {
430     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
431                 "No resource assignment method configured, using proportional approach\n");
432     mode_str = GNUNET_strdup ("proportional");
433   }
434   env.cls = NULL;
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;
439   env.cfg = cfg;
440   env.stats = GSA_stats;
441   env.addresses = GSA_addresses;
442   env.network_count = GNUNET_ATS_NetworkTypeCount;
443   load_quotas (cfg,
444                env.out_quota,
445                env.in_quota,
446                GNUNET_ATS_NetworkTypeCount);
447   GNUNET_asprintf (&plugin,
448                    "libgnunet_plugin_ats_%s",
449                    mode_str);
450   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451               "Initializing solver `%s'\n",
452               mode_str);
453   GNUNET_free (mode_str);
454   if (NULL == (sf = GNUNET_PLUGIN_load (plugin, &env)))
455   {
456     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
457                 _("Failed to initialize solver `%s'!\n"),
458                 plugin);
459     return GNUNET_SYSERR;
460   }
461   return GNUNET_OK;
462 }
463
464
465 /**
466  * Shutdown address subsystem.
467  */
468 void
469 GAS_plugin_done ()
470 {
471   GNUNET_PLUGIN_unload (plugin,
472                         sf);
473   sf = NULL;
474   GNUNET_free (plugin);
475   plugin = NULL;
476 }
477
478
479 /**
480  * Tell the solver that the given address can now be used
481  * for talking to the respective peer.
482  *
483  * @param new_address the new address
484  * @param addr_net network scope the address is in
485  */
486 void
487 GAS_plugin_new_address (struct ATS_Address *new_address,
488                         enum GNUNET_ATS_Network_Type addr_net)
489 {
490   sf->s_add (sf->cls,
491              new_address,
492              addr_net);
493 }
494
495
496 /**
497  * Tell the solver that the given address is no longer valid
498  * can cannot be used any longer.
499  *
500  * @param address address that was deleted
501  */
502 void
503 GAS_plugin_delete_address (struct ATS_Address *address)
504 {
505   sf->s_del (sf->cls,
506              address);
507 }
508
509
510 /**
511  * Tell the solver that the given client has expressed its
512  * appreciation for the past performance of a given connection.
513  *
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
519  */
520 void
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,
525                             float score_abs)
526 {
527   sf->s_feedback (sf->cls,
528                   application,
529                   peer,
530                   scope,
531                   kind,
532                   score_abs);
533 }
534
535
536 /**
537  * Stop instant solving, there are many state updates
538  * happening in bulk right now.
539  */
540 void
541 GAS_plugin_solver_lock ()
542 {
543   sf->s_bulk_start (sf->cls);
544 }
545
546
547 /**
548  * Resume instant solving, we are done with the bulk state updates.
549  */
550 void
551 GAS_plugin_solver_unlock ()
552 {
553   sf->s_bulk_stop (sf->cls);
554 }
555
556
557 /**
558  * Notify the plugin that a request to connect to
559  * a particular peer was given to us.
560  *
561  * @param pid identity of peer we now care about
562  */
563 void
564 GAS_plugin_request_connect_start (const struct GNUNET_PeerIdentity *pid)
565 {
566   sf->s_get (sf->cls,
567              pid);
568 }
569
570
571 /**
572  * Notify the plugin that a request to connect to
573  * a particular peer was dropped.
574  *
575  * @param pid identity of peer we care now less about
576  */
577 void
578 GAS_plugin_request_connect_stop (const struct GNUNET_PeerIdentity *pid)
579 {
580   sf->s_get_stop (sf->cls,
581                   pid);
582 }
583
584
585 /* end of gnunet-service-ats_plugins.c */