eliminate super-lazy functions
[oweals/gnunet.git] / src / ats / plugin_ats_proportional.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2011-2015 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/plugin_ats_proportional.c
23  * @brief ATS proportional solver
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_statistics_service.h"
29 #include "gnunet_ats_plugin.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats_addresses.h"
32
33 #define PROP_STABILITY_FACTOR 1.25
34
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
37
38
39 /**
40  *
41  * NOTE: Do not change this documentation. This documentation is based
42  * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
43  * use build_txt.sh to generate plaintext output
44  *
45  * ATS addresses : proportional solver
46  *
47  *    The proportional solver ("proportional") distributes the available
48  *    bandwidth fair over all the addresses influenced by the
49  *    preference values. For each available network type an in- and
50  *    outbound quota is configured and the bandwidth available in
51  *    these networks is distributed over the addresses.  The solver
52  *    first assigns every addresses the minimum amount of bandwidth
53  *    #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
54  *    remaining bandwidth available according to the preference
55  *    values. For each peer only a single address gets bandwidth
56  *    assigned and only one address marked as active.  The most
57  *    important functionality for the solver is implemented in: *
58  *    find_address_it is an hashmap iterator returning the prefered
59  *    address for an peer * update_quota_per_network distributes
60  *    available bandwidth for a network over active addresses
61  *
62  *    Changes to addresses automatically have an impact on the the
63  *    bandwidth assigned to other addresses in the same network since
64  *    the solver distributes the remaining bandwidth over the
65  *    addresses in the network.  When changes to the addresses occur,
66  *    the solver first performs the changes, like adding or deleting
67  *    addresses, and then updates bandwidth assignment for the
68  *    affected network. Bandwidth assignment is only recalculated on
69  *    demand when an address is requested by a client for a peer or
70  *    when the addresses available have changed or an address changed
71  *    the network it is located in. When the bandwidth assignment has
72  *    changed the callback is called with the new bandwidth
73  *    assignments. The bandwidth distribution for a network is
74  *    recalculated due to: * address suggestion requests * address
75  *    deletions * address switching networks during address update *
76  *    preference changes
77  *
78  *     3.1 Data structures used
79  *
80  *    For each ATS network (e.g. WAN, LAN, loopback) a struct Network
81  *    is used to specify network related information as total adresses
82  *    and active addresses in this network and the configured in- and
83  *    outbound quota. Each network also contains a list of addresses
84  *    added to the solver located in this network. The proportional
85  *    solver uses the addresses' solver_information field to store the
86  *    proportional network it belongs to for each address.
87  *
88  *     3.2 Initializing
89  *
90  *    When the proportional solver is initialized the solver creates a
91  *    new solver handle and initializes the network structures with
92  *    the quotas passed from addresses and returns the handle solver.
93  *
94  *     3.3 Adding an address
95  *
96  *    When a new address is added to the solver using s_add, a lookup
97  *    for the network for this address is done and the address is
98  *    enqueued in in the linked list of the network.
99  *
100  *     3.4 Updating an address
101  *
102  *    The main purpose of address updates is to update the ATS
103  *    information for addresse selection. Important for the proportional
104  *    solver is when an address switches network it is located
105  *    in. This is common because addresses added by transport's
106  *    validation mechanism are commonly located in
107  *    #GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
108  *    in this network type and only if a connection is successful on
109  *    return of payload data transport switches to the real network
110  *    the address is located in.  When an address changes networks it
111  *    is first of all removed from the old network using the solver
112  *    API function #GAS_proportional_address_delete() and the network in
113  *    the address struct is updated. A lookup for the respective new
114  *    proportional network is done and stored in the addresse's
115  *    solver_information field. Next the address is re-added to the
116  *    solver using the solver API function
117  *    #GAS_proportional_address_add(). If the address was marked as in
118  *    active, the solver checks if bandwidth is available in the
119  *    network and if yes sets the address to active and updates the
120  *    bandwidth distribution in this network. If no bandwidth is
121  *    available it sets the bandwidth for this address to 0 and tries
122  *    to suggest an alternative address. If an alternative address was
123  *    found, addresses' callback is called for this address.
124  *
125  *     3.5 Deleting an address
126  *
127  *    When an address is removed from the solver, it removes the
128  *    respective address from the network and if the address was
129  *    marked as active, it updates the bandwidth distribution for this
130  *    network.
131  *
132  *     3.6 Requesting addresses
133  *
134  *    When an address is requested for a peer the solver performs a
135  *    lookup for the peer entry in addresses address hashmap and
136  *    selects the best address.  The selection of the most suitable
137  *    address is done in the find_address_it hashmap iterator
138  *    described in detail in section 3.7. If no address is returned,
139  *    no address can be suggested at the moment. If the address
140  *    returned is marked as active, the solver can return this
141  *    address. If the address is not marked as active, the solver
142  *    checks if another address belongign to this peer is marked as
143  *    active and marks the address as inactive, updates the bandwidth
144  *    for this address to 0, call the bandwidth changed callback for
145  *    this address due to the change and updates quota assignment for
146  *    the addresse's network. the now in-active address is belonging
147  *    to. The solver marks the new address as active and updates the
148  *    bandwidth assignment for this network.
149  *
150  *     3.7 Choosing addresses
151  *
152  *    Choosing the best possible address for suggestion is done by
153  *    iterating over all addresses of a peer stored in addresses'
154  *    hashmap and using the hashmap iterator find_address_it to select
155  *    the best available address.  Several checks are done when an
156  *    address is selected. First if this address is currently blocked
157  *    by addresses from being suggested. An address is blocked for the
158  *    duration of #ATS_BLOCKING_DELTA when it is suggested to
159  *    transport. Next it is checked if at least
160  *    #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
161  *    in the addresse's network, because suggesting an address without
162  *    bandwidth does not make sense. This also ensures that all active
163  *    addresses in this network get at least the minimum amount of
164  *    bandwidth assigned. In the next step the solver ensures that for
165  *    tcp connections inbound connections are prefered over outbound
166  *    connections. In the next stet the solver ensures that
167  *    connections are prefered in the following order: * connections
168  *    are already established and have bandwidth assigned *
169  *    connections with a shorter distance * connectes have a shorter
170  *    latency
171  *
172  *     3.8 Changing preferences
173  *
174  *     3.9 Shutdown
175  *
176  *    During shutdown all network entries and aging processes are
177  *    destroyed and freed.
178  *
179  *
180  * OLD DOCUMENTATION
181  *
182  * This solver assigns in and outbound bandwidth equally for all
183  * addresses in specific network type (WAN, LAN) based on configured
184  * in and outbound quota for this network.
185  *
186  * The solver is notified by addresses about changes to the addresses
187  * and recalculates the bandwith assigned if required. The solver
188  * notifies addresses by calling the GAS_bandwidth_changed_cb
189  * callback.
190  *
191  * - Initialization
192  *
193  *
194  *
195  *
196  * For each peer only a single is selected and marked as "active" in the address
197  * struct.
198  *
199  * E.g.:
200  *
201  * You have the networks WAN and LAN and quotas
202  * WAN_TOTAL_IN, WAN_TOTAL_OUT
203  * LAN_TOTAL_IN, LAN_TOTAL_OUT
204  *
205  * If you have x addresses in the network segment LAN, the quotas are
206  * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
207  *
208  * Quotas are automatically recalculated and reported back when addresses are
209  * - requested
210  *
211  */
212
213 /**
214  * Default value to assume for the proportionality factor,
215  * if none is given in the configuration.
216  */
217 #define PROPORTIONALITY_FACTOR 2.0
218
219
220 /**
221  * Address information stored for the proportional solver in the
222  * `solver_information` member of `struct GNUNET_ATS_Address`.
223  *
224  * They are also stored in the respective `struct Network`'s linked
225  * list.
226  */
227 struct AddressWrapper
228 {
229   /**
230    * Next in DLL
231    */
232   struct AddressWrapper *next;
233
234   /**
235    * Previous in DLL
236    */
237   struct AddressWrapper *prev;
238
239   /**
240    * The address
241    */
242   struct ATS_Address *addr;
243
244     /**
245    * Network scope this address is in
246    */
247   struct Network *network;
248
249   /**
250    * Inbound quota
251    */
252   uint32_t calculated_quota_in;
253
254   /**
255    * Outbound quota
256    */
257   uint32_t calculated_quota_out;
258
259   /**
260    * When was this address activated
261    */
262   struct GNUNET_TIME_Absolute activated;
263
264 };
265
266
267 /**
268  * Representation of a network
269  */
270 struct Network
271 {
272   /**
273    * Network description
274    */
275   const char *desc;
276
277   /**
278    * String for statistics total addresses
279    */
280   char *stat_total;
281
282   /**
283    * String for statistics active addresses
284    */
285   char *stat_active;
286
287   /**
288    * Linked list of addresses in this network: head
289    */
290   struct AddressWrapper *head;
291
292   /**
293    * Linked list of addresses in this network: tail
294    */
295   struct AddressWrapper *tail;
296
297   /**
298    * Total inbound quota
299    */
300   unsigned long long total_quota_in;
301
302   /**
303    * Total outbound quota
304    */
305   unsigned long long total_quota_out;
306
307   /**
308    * ATS network type
309    */
310   enum GNUNET_ATS_Network_Type type;
311
312   /**
313    * Number of active addresses for this network
314    */
315   unsigned int active_addresses;
316
317   /**
318    * Number of total addresses for this network
319    */
320   unsigned int total_addresses;
321
322 };
323
324
325 /**
326  * A handle for the proportional solver
327  */
328 struct GAS_PROPORTIONAL_Handle
329 {
330
331   /**
332    * Our execution environment.
333    */
334   struct GNUNET_ATS_PluginEnvironment *env;
335
336   /**
337    * Networks array
338    */
339   struct Network *network_entries;
340
341   /**
342    * Proportionality factor
343    */
344   double prop_factor;
345
346   /**
347    * Stability factor
348    */
349   double stability_factor;
350
351   /**
352    * Bulk lock counter. If zero, we are not locked.
353    */
354   unsigned int bulk_lock;
355
356   /**
357    * Number of changes made while solver was locked.  We really only
358    * use 0/non-zero to check on unlock if we have to run the update.
359    */
360   unsigned int bulk_requests;
361
362   /**
363    * Number of active addresses for solver
364    */
365   unsigned int active_addresses;
366
367   /**
368    * Number of networks in @a network_entries
369    */
370   unsigned int network_count;
371 };
372
373
374 /**
375  * Test if bandwidth is available in this network to add an additional address
376  *
377  * @param net the network type to update
378  * @return #GNUNET_YES or #GNUNET_NO
379  */
380 static int
381 is_bandwidth_available_in_network (struct Network *net)
382 {
383   unsigned int na = net->active_addresses + 1;
384   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
385
386   if ( ((net->total_quota_in / na) > min_bw) &&
387        ((net->total_quota_out / na) > min_bw) )
388   {
389     LOG (GNUNET_ERROR_TYPE_DEBUG,
390          "Enough bandwidth available for %u active addresses in network `%s'\n",
391          na,
392          net->desc);
393
394     return GNUNET_YES;
395   }
396   LOG (GNUNET_ERROR_TYPE_DEBUG,
397        "Not enough bandwidth available for %u active addresses in network `%s'\n",
398        na,
399        net->desc);
400   return GNUNET_NO;
401 }
402
403
404 /**
405  * Update bandwidth assigned to peers in this network
406  *
407  * @param s the solver handle
408  * @param net the network type to update
409  * this address
410  */
411 static void
412 distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
413                       struct Network *net)
414 {
415   struct AddressWrapper *aw;
416   unsigned long long remaining_quota_in = 0;
417   unsigned long long quota_out_used = 0;
418   unsigned long long remaining_quota_out = 0;
419   unsigned long long quota_in_used = 0;
420   int count_addresses;
421   uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
422   double relative_peer_prefence;
423   double sum_relative_peer_prefences; /* Important: has to be double not float due to precision */
424   double cur_pref; /* Important: has to be double not float due to precision */
425   double peer_weight;
426   double total_weight;
427   const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */
428
429   uint32_t assigned_quota_in = 0;
430   uint32_t assigned_quota_out = 0;
431
432
433   LOG (GNUNET_ERROR_TYPE_INFO,
434        "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
435        net->desc,
436        net->active_addresses,
437        net->total_quota_in,
438        net->total_quota_in);
439
440   if (net->active_addresses == 0)
441   {
442     return; /* no addresses to update */
443   }
444
445   /* Idea
446    * Assign every peer in network minimum Bandwidth
447    * Distribute bandwidth left according to preference
448    */
449
450   if ((net->active_addresses * min_bw) > net->total_quota_in)
451   {
452     GNUNET_break(0);
453     return;
454   }
455   if ((net->active_addresses * min_bw) > net->total_quota_out)
456   {
457     GNUNET_break(0);
458     return;
459   }
460
461   remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
462   remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
463   LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
464       remaining_quota_in, remaining_quota_out);
465   sum_relative_peer_prefences = 0.0;
466
467   /* Calculate sum of relative preference for active addresses in this network */
468   count_addresses = 0;
469   for (aw = net->head; NULL != aw; aw = aw->next)
470   {
471     if (GNUNET_YES != aw->addr->active)
472       continue;
473
474     peer_relative_prefs = s->env->get_preferences (s->env->cls,
475                                                    &aw->addr->peer);
476     relative_peer_prefence = 0.0;
477     relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
478     sum_relative_peer_prefences += relative_peer_prefence;
479     count_addresses ++;
480   }
481
482   if (count_addresses != net->active_addresses)
483   {
484     GNUNET_break (0);
485     LOG (GNUNET_ERROR_TYPE_WARNING,
486          "%s: Counted %u active addresses, but network says to have %u active addresses\n",
487          net->desc, count_addresses, net->active_addresses);
488     for (aw = net->head; NULL != aw; aw = aw->next)
489     {
490       if (GNUNET_YES != aw->addr->active)
491         continue;
492
493       LOG (GNUNET_ERROR_TYPE_WARNING,
494            "Active: `%s' `%s' length %u\n",
495            GNUNET_i2s (&aw->addr->peer),
496            aw->addr->plugin,
497            aw->addr->addr_len);
498     }
499   }
500
501   LOG (GNUNET_ERROR_TYPE_INFO,
502       "Total relative preference %.3f for %u addresses in network %s\n",
503       sum_relative_peer_prefences, net->active_addresses, net->desc);
504
505   for (aw = net->head; NULL != aw; aw = aw->next)
506   {
507     if (GNUNET_YES == aw->addr->active)
508     {
509       peer_relative_prefs = s->env->get_preferences (s->env->cls,
510                                                      &aw->addr->peer);
511
512       cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
513       total_weight = net->active_addresses +
514           s->prop_factor * sum_relative_peer_prefences;
515       peer_weight = (1.0 + (s->prop_factor * cur_pref));
516
517       assigned_quota_in = min_bw
518           + ((peer_weight / total_weight) * remaining_quota_in);
519       assigned_quota_out = min_bw
520           + ((peer_weight / total_weight) * remaining_quota_out);
521
522       LOG (GNUNET_ERROR_TYPE_INFO,
523           "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
524           GNUNET_i2s (&aw->addr->peer), peer_weight, total_weight,
525           assigned_quota_in, assigned_quota_out);
526     }
527     else
528     {
529       assigned_quota_in = 0;
530       assigned_quota_out = 0;
531     }
532
533     quota_in_used += assigned_quota_in;
534     quota_out_used += assigned_quota_out;
535     /* Prevent overflow due to rounding errors */
536     if (assigned_quota_in > UINT32_MAX)
537       assigned_quota_in = UINT32_MAX;
538     if (assigned_quota_out > UINT32_MAX)
539       assigned_quota_out = UINT32_MAX;
540
541     /* Compare to current bandwidth assigned */
542     aw->calculated_quota_in = assigned_quota_in;
543     aw->calculated_quota_out = assigned_quota_out;
544   }
545   LOG(GNUNET_ERROR_TYPE_DEBUG,
546       "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
547       quota_out_used);
548   if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
549   {
550     LOG(GNUNET_ERROR_TYPE_ERROR,
551         "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
552         net->active_addresses, quota_out_used, net->total_quota_out);
553   }
554   if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
555   {
556     LOG(GNUNET_ERROR_TYPE_ERROR,
557         "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
558         net->active_addresses, quota_in_used, net->total_quota_in);
559   }
560 }
561
562
563 /**
564  * Context for finding the best address* Linked list of addresses in this network: head
565  */
566 struct FindBestAddressCtx
567 {
568   /**
569    * The solver handle
570    */
571   struct GAS_PROPORTIONAL_Handle *s;
572
573   /**
574    * The currently best address
575    */
576   struct ATS_Address *best;
577 };
578
579
580 /**
581  * Find index of a ATS property type in the quality properties array.
582  *
583  * @param type ATS property type
584  * @return index in the quality array, #GNUNET_SYSERR if the type
585  *         was not a quality property
586  */
587 static int
588 find_quality_property_index (enum GNUNET_ATS_Property type)
589 {
590   enum GNUNET_ATS_Property existing_types[] = GNUNET_ATS_QualityProperties;
591   unsigned int c;
592
593   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
594     if (existing_types[c] == type)
595       return c;
596   GNUNET_break (0);
597   return GNUNET_SYSERR;
598 }
599
600
601 /**
602  * Find a "good" address to use for a peer by iterating over the addresses for this peer.
603  * If we already have an existing address, we stick to it.
604  * Otherwise, we pick by lowest distance and then by lowest latency.
605  *
606  * @param cls the `struct FindBestAddressCtx *' where we store the result
607  * @param key unused
608  * @param value another `struct ATS_Address*` to consider using
609  * @return #GNUNET_OK (continue to iterate)
610  */
611 static int
612 find_best_address_it (void *cls,
613                       const struct GNUNET_PeerIdentity *key,
614                       void *value)
615 {
616   struct FindBestAddressCtx *ctx = cls;
617   struct ATS_Address *current = value;
618   struct ATS_Address *current_best = current;
619   struct AddressWrapper *asi;
620   struct GNUNET_TIME_Relative active_time;
621   struct GNUNET_TIME_Relative min_active_time;
622   double best_delay;
623   double best_distance;
624   double cur_delay;
625   double cur_distance;
626   int index;
627
628   current_best = NULL;
629   asi = current->solver_information;
630   if (NULL == asi)
631   {
632     GNUNET_break (0);
633     return GNUNET_OK;
634   }
635
636   if (GNUNET_NO == is_bandwidth_available_in_network (asi->network))
637   {
638     return GNUNET_OK; /* There's no bandwidth available in this network */
639   }
640
641   if (NULL != ctx->best)
642   {
643     /* Compare current addresses with denominated 'best' address */
644     current_best = ctx->best;
645   }
646   else
647   {
648     /* We do not have a 'best' address so take this address */
649     LOG (GNUNET_ERROR_TYPE_DEBUG,
650          "Setting initial address %p\n",
651          current);
652     current_best = current;
653     goto end;
654   }
655
656   if (GNUNET_YES == current->active)
657   {
658     GNUNET_assert (asi->activated.abs_value_us != GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us);
659     active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
660     min_active_time.rel_value_us =  ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) *
661         ctx->s->stability_factor;
662     if (active_time.rel_value_us <= min_active_time.rel_value_us)
663     {
664       /* Keep active address for stability reasons */
665       ctx->best = current;
666       return GNUNET_NO;
667     }
668   }
669
670   /* Now compare ATS information */
671   index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
672   cur_distance = current->atsin[index].norm;
673   best_distance = ctx->best->atsin[index].norm;
674   index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
675   cur_delay = current->atsin[index].norm;
676   best_delay = ctx->best->atsin[index].norm;
677
678   /* user shorter distance */
679   if (cur_distance < best_distance)
680   {
681     if (GNUNET_NO == ctx->best->active)
682     {
683       current_best = current; /* Use current */
684     }
685     else if ((best_distance / cur_distance) > ctx->s->stability_factor)
686     {
687       /* Best and active address performs worse  */
688       current_best = current;
689     }
690   }
691   else
692   {
693     /* Use current best */
694     current_best = ctx->best;
695   }
696
697   /* User connection with less delay */
698   if (cur_delay < best_delay)
699   {
700
701     if (GNUNET_NO == ctx->best->active)
702     {
703       current_best = current; /* Use current */
704     }
705     else if ((best_delay / cur_delay) > ctx->s->stability_factor)
706     {
707       /* Best and active address performs worse  */
708       current_best = current;
709     }
710   }
711   else
712   {
713     /* Use current best */
714     current_best = ctx->best;
715   }
716
717 end:
718   ctx->best = current_best;
719   return GNUNET_OK;
720 }
721
722
723 /**
724  * Find the currently best address for a peer from the set of addresses available
725  * or return NULL of no address is available
726  *
727  * @param s the proportional handle
728  * @param addresses the address hashmap
729  * @param id the peer id
730  * @return the address or NULL
731  */
732 struct ATS_Address *
733 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
734                   struct GNUNET_CONTAINER_MultiPeerMap *addresses,
735                   const struct GNUNET_PeerIdentity *id)
736 {
737   struct FindBestAddressCtx fba_ctx;
738
739   fba_ctx.best = NULL;
740   fba_ctx.s = s;
741   GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
742                                               id,
743                                               &find_best_address_it,
744                                               &fba_ctx);
745   return fba_ctx.best;
746 }
747
748
749 /**
750  * Address map iterator to find current active address for peer.
751  * Asserts that only one address is active per peer.
752  *
753  * @param cls last active address
754  * @param key peer's key
755  * @param value address to check
756  * @return #GNUNET_NO on double active address else #GNUNET_YES;
757  */
758 static int
759 get_active_address_it (void *cls,
760                        const struct GNUNET_PeerIdentity *key,
761                        void *value)
762 {
763   struct ATS_Address **dest = cls;
764   struct ATS_Address *aa = value;
765
766   if (GNUNET_YES != aa->active)
767     return GNUNET_OK;
768   GNUNET_assert (NULL == (*dest));
769   (*dest) = aa;
770   return GNUNET_OK;
771 }
772
773
774 /**
775  * Find current active address for peer
776  *
777  * @param s the solver handle
778  * @param peer the peer
779  * @return active address or NULL
780  */
781 static struct ATS_Address *
782 get_active_address (struct GAS_PROPORTIONAL_Handle *s,
783                     const struct GNUNET_PeerIdentity *peer)
784 {
785   struct ATS_Address *dest;
786
787   dest = NULL;
788   GNUNET_CONTAINER_multipeermap_get_multiple (s->env->addresses,
789                                               peer,
790                                               &get_active_address_it,
791                                               &dest);
792   return dest;
793 }
794
795
796 /**
797  * Decrease address count in network
798  *
799  * @param s the solver handle
800  * @param net the network type
801  */
802 static void
803 address_decrement_active (struct GAS_PROPORTIONAL_Handle *s,
804                           struct Network *net)
805 {
806   if (net->active_addresses < 1)
807   {
808     GNUNET_break (0);
809   }
810   else
811   {
812     net->active_addresses--;
813     GNUNET_STATISTICS_update (s->env->stats,
814                               net->stat_active, -1, GNUNET_NO);
815   }
816   if (s->active_addresses < 1)
817   {
818     GNUNET_break (0);
819   }
820   else
821   {
822     s->active_addresses--;
823     GNUNET_STATISTICS_update (s->env->stats,
824                               "# ATS addresses total",
825                               -1,
826                               GNUNET_NO);
827   }
828 }
829
830
831 /**
832  * Notify bandwidth changes to addresses
833  *
834  * @param s solver handle
835  * @param net the network to propagate changes in
836  */
837 static void
838 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
839                      struct Network *net)
840 {
841   struct AddressWrapper *cur;
842
843   for (cur = net->head; NULL != cur; cur = cur->next)
844   {
845     if ( (cur->addr->assigned_bw_in == cur->calculated_quota_in) &&
846          (cur->addr->assigned_bw_out == cur->calculated_quota_out) )
847       continue;
848     cur->addr->assigned_bw_in = cur->calculated_quota_in;
849     cur->addr->assigned_bw_out = cur->calculated_quota_out;
850
851     /* Reset for next iteration */
852     cur->calculated_quota_in = 0;
853     cur->calculated_quota_out = 0;
854     LOG (GNUNET_ERROR_TYPE_DEBUG,
855          "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
856          (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
857          cur->addr,
858          GNUNET_i2s (&cur->addr->peer),
859          cur->addr->assigned_bw_in,
860          cur->addr->assigned_bw_out);
861
862     /* Notify on change */
863     if (GNUNET_YES == cur->addr->active)
864       s->env->bandwidth_changed_cb (s->env->cls,
865                                     cur->addr);
866   }
867 }
868
869
870 /**
871  * Distribibute bandwidth
872  *
873  * @param s the solver handle
874  * @param n the network, can be NULL for all network
875  */
876 static void
877 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
878                                  struct Network *n)
879 {
880   if (GNUNET_YES == s->bulk_lock)
881   {
882     s->bulk_requests++;
883     return;
884   }
885
886   if (NULL != n)
887   {
888     LOG (GNUNET_ERROR_TYPE_INFO,
889         "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
890          GNUNET_ATS_print_network_type(n->type),
891          n->active_addresses,
892          n->total_addresses);
893
894     s->env->info_cb (s->env->cls,
895                      GAS_OP_SOLVE_START,
896                      GAS_STAT_SUCCESS,
897                      GAS_INFO_PROP_SINGLE);
898
899     /* Distribute  */
900     distribute_bandwidth(s, n);
901
902     s->env->info_cb (s->env->cls,
903                      GAS_OP_SOLVE_STOP,
904                      GAS_STAT_SUCCESS,
905                      GAS_INFO_PROP_SINGLE);
906     s->env->info_cb (s->env->cls,
907                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
908                      GAS_STAT_SUCCESS,
909                      GAS_INFO_PROP_SINGLE);
910
911     /* Do propagation */
912     propagate_bandwidth (s, n);
913
914     s->env->info_cb (s->env->cls,
915                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
916                      GAS_STAT_SUCCESS,
917                      GAS_INFO_PROP_SINGLE);
918   }
919   else
920   {
921     int i;
922     s->env->info_cb (s->env->cls,
923                      GAS_OP_SOLVE_START,
924                      GAS_STAT_SUCCESS,
925                      GAS_INFO_PROP_ALL);
926     for (i = 0; i < s->network_count; i++)
927     {
928       /* Distribute */
929       distribute_bandwidth(s, &s->network_entries[i]);
930     }
931
932     s->env->info_cb (s->env->cls,
933                      GAS_OP_SOLVE_STOP,
934                      GAS_STAT_SUCCESS,
935                      GAS_INFO_PROP_ALL);
936     s->env->info_cb (s->env->cls,
937                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
938                      GAS_STAT_SUCCESS,
939                      GAS_INFO_PROP_ALL);
940     for (i = 0; i < s->network_count; i++)
941     {
942       /* Do propagation */
943       propagate_bandwidth(s, &s->network_entries[i]);
944     }
945     s->env->info_cb (s->env->cls,
946                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
947                      GAS_STAT_SUCCESS,
948                      GAS_INFO_PROP_ALL);
949   }
950 }
951
952
953 /**
954  * Update active address for a peer:
955  * Check if active address exists and what the best address is, if addresses
956  * are different switch
957  *
958  * @param s solver handle
959  * @param peer the peer to check
960  * return the new address or NULL if no update was performed
961   */
962 static struct ATS_Address *
963 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
964                        const struct GNUNET_PeerIdentity *peer)
965 {
966   struct ATS_Address *best_address;
967   struct ATS_Address *current_address;
968   struct AddressWrapper *asi;
969   struct Network *net;
970
971   LOG (GNUNET_ERROR_TYPE_INFO,
972        "Updating active address for peer `%s'\n",
973        GNUNET_i2s (peer));
974
975   /* Find active address */
976   current_address = get_active_address (s,
977                                         peer);
978
979   LOG (GNUNET_ERROR_TYPE_INFO,
980        "Peer `%s' has active address %p\n",
981        GNUNET_i2s (peer),
982        current_address);
983
984   /* Find best address */
985   best_address = get_best_address (s,
986                                    s->env->addresses,
987                                    peer);
988   LOG (GNUNET_ERROR_TYPE_INFO,
989        "Peer `%s' has best address %p\n",
990        GNUNET_i2s (peer),
991        best_address);
992
993   if (NULL != current_address)
994   {
995     GNUNET_assert (GNUNET_YES == current_address->active);
996     if ( (NULL == best_address) ||
997          ( (NULL != best_address) &&
998            (current_address != best_address) ) )
999     {
1000       /* We switch to a new address (or to none),
1001          mark old address as inactive */
1002       LOG (GNUNET_ERROR_TYPE_INFO,
1003            "Disabling previous %s address %p for peer `%s'\n",
1004            (GNUNET_NO == current_address->active) ? "inactive" : "active",
1005            current_address,
1006            GNUNET_i2s (peer));
1007
1008       asi = current_address->solver_information;
1009       net = asi->network;
1010       asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1011       current_address->active = GNUNET_NO; /* No active any longer */
1012       current_address->assigned_bw_in = 0; /* no bandwidth assigned */
1013       current_address->assigned_bw_out = 0; /* no bandwidth assigned */
1014       address_decrement_active (s, net);
1015       distribute_bandwidth_in_network (s, net);
1016     }
1017     if (NULL == best_address)
1018     {
1019       /* We previously had an active address, but now we cannot suggest one
1020        * Therefore we have to disconnect the peer */
1021       LOG (GNUNET_ERROR_TYPE_INFO,
1022            "Disconnecting peer `%s' with previous address %p\n",
1023            GNUNET_i2s (peer),
1024            current_address);
1025       s->env->bandwidth_changed_cb (s->env->cls,
1026                                     current_address);
1027     }
1028   }
1029   if (NULL == best_address)
1030   {
1031     LOG (GNUNET_ERROR_TYPE_INFO,
1032          "Cannot suggest address for peer `%s'\n",
1033          GNUNET_i2s (peer));
1034     return NULL;
1035   }
1036
1037   LOG (GNUNET_ERROR_TYPE_INFO,
1038        "Suggesting new address %p for peer `%s'\n",
1039        best_address,
1040        GNUNET_i2s (peer));
1041
1042   if ( (NULL != current_address) &&
1043        (best_address == current_address) )
1044   {
1045     GNUNET_break (GNUNET_NO != current_address->active);
1046     return best_address; /* Same same */
1047   }
1048
1049   asi = best_address->solver_information;
1050   GNUNET_assert (NULL != asi);
1051   net = asi->network;
1052
1053   /* Mark address as active */
1054   asi->activated = GNUNET_TIME_absolute_get ();
1055   best_address->active = GNUNET_YES;
1056
1057   net->active_addresses++;
1058   s->active_addresses++;
1059   GNUNET_STATISTICS_update (s->env->stats,
1060                             "# ATS active addresses total",
1061                             1,
1062                             GNUNET_NO);
1063   GNUNET_STATISTICS_update (s->env->stats,
1064                             net->stat_active,
1065                             1,
1066                             GNUNET_NO);
1067   LOG (GNUNET_ERROR_TYPE_INFO,
1068        "Address %p for peer `%s' is now active\n",
1069        best_address,
1070        GNUNET_i2s (peer));
1071   /* Distribute bandwidth */
1072   distribute_bandwidth_in_network (s, net);
1073   return best_address;
1074 }
1075
1076
1077 /**
1078  * Changes the preferences for a peer in the problem
1079  *
1080  * @param solver the solver handle
1081  * @param peer the peer to change the preference for
1082  * @param kind the kind to change the preference
1083  * @param pref_rel the normalized preference value for this kind over all clients
1084  */
1085 static void
1086 GAS_proportional_address_change_preference (void *solver,
1087                                             const struct GNUNET_PeerIdentity *peer,
1088                                             enum GNUNET_ATS_PreferenceKind kind,
1089                                             double pref_rel)
1090 {
1091   struct GAS_PROPORTIONAL_Handle *s = solver;
1092   struct ATS_Address *best_address;
1093   struct ATS_Address *active_address;
1094   struct AddressWrapper *asi;
1095
1096   if (0 ==
1097       s->env->get_connectivity (s->env->cls,
1098                                 peer))
1099     return; /* Peer is not requested */
1100
1101   /* This peer is requested, find best address */
1102   active_address = get_active_address (s,
1103                                        peer);
1104   best_address = update_active_address (s, peer);
1105
1106   if ( (NULL != best_address) &&
1107        (active_address == best_address) )
1108   {
1109     asi = best_address->solver_information;
1110     GNUNET_assert (NULL != asi);
1111
1112     /* We sticked to the same address, therefore redistribute  */
1113     distribute_bandwidth_in_network (s, asi->network);
1114   }
1115 }
1116
1117
1118 /**
1119  * Get application feedback for a peer
1120  *
1121  * @param solver the solver handle
1122  * @param application the application
1123  * @param peer the peer to change the preference for
1124  * @param scope the time interval for this feedback: [now - scope .. now]
1125  * @param kind the kind to change the preference
1126  * @param score the score
1127  */
1128 static void
1129 GAS_proportional_address_preference_feedback (void *solver,
1130                                               struct GNUNET_SERVER_Client *application,
1131                                               const struct GNUNET_PeerIdentity *peer,
1132                                               const struct GNUNET_TIME_Relative scope,
1133                                               enum GNUNET_ATS_PreferenceKind kind,
1134                                               double score)
1135 {
1136   struct GAS_PROPORTIONAL_Handle *s = solver;
1137
1138   GNUNET_assert(NULL != peer);
1139   GNUNET_assert(NULL != s);
1140 }
1141
1142
1143 /**
1144  * Get the preferred address for a specific peer
1145  *
1146  * @param solver the solver handle
1147  * @param peer the identity of the peer
1148  * @return best address
1149  */
1150 static const struct ATS_Address *
1151 GAS_proportional_get_preferred_address (void *solver,
1152                                         const struct GNUNET_PeerIdentity *peer)
1153 {
1154   struct GAS_PROPORTIONAL_Handle *s = solver;
1155   const struct ATS_Address *best_address;
1156
1157   best_address = update_active_address (s, peer);
1158   if (s->bulk_lock > 0)
1159     return NULL;
1160   return best_address;
1161 }
1162
1163
1164 /**
1165  * Stop notifying about address and bandwidth changes for this peer
1166  *
1167  * @param solver the solver handle
1168  * @param peer the peer
1169  */
1170 static void
1171 GAS_proportional_stop_get_preferred_address (void *solver,
1172                                              const struct GNUNET_PeerIdentity *peer)
1173 {
1174   struct GAS_PROPORTIONAL_Handle *s = solver;
1175   struct ATS_Address *cur;
1176   struct AddressWrapper *asi;
1177   struct Network *cur_net;
1178
1179   cur = get_active_address (s,
1180                             peer);
1181   if (NULL != cur)
1182   {
1183     LOG (GNUNET_ERROR_TYPE_INFO,
1184          "Disabling %s address %p for peer `%s'\n",
1185          (GNUNET_NO == cur->active) ? "inactive" : "active",
1186          cur,
1187          GNUNET_i2s (&cur->peer));
1188
1189     /* Disabling current address */
1190     asi = cur->solver_information;
1191     cur_net = asi->network;
1192     asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1193     cur->active = GNUNET_NO; /* No active any longer */
1194     cur->assigned_bw_in = 0; /* no bandwidth assigned */
1195     cur->assigned_bw_out = 0; /* no bandwidth assigned */
1196
1197     address_decrement_active (s, cur_net);
1198
1199     distribute_bandwidth_in_network (s, cur_net);
1200   }
1201 }
1202
1203
1204 /**
1205  * Start a bulk operation
1206  *
1207  * @param solver the solver
1208  */
1209 static void
1210 GAS_proportional_bulk_start (void *solver)
1211 {
1212   struct GAS_PROPORTIONAL_Handle *s = solver;
1213
1214   LOG (GNUNET_ERROR_TYPE_DEBUG,
1215        "Locking solver for bulk operation ...\n");
1216   GNUNET_assert (NULL != solver);
1217   s->bulk_lock++;
1218 }
1219
1220
1221 /**
1222  * Bulk operation done.
1223  *
1224  * @param solver our `struct GAS_PROPORTIONAL_Handle *`
1225  */
1226 static void
1227 GAS_proportional_bulk_stop (void *solver)
1228 {
1229   struct GAS_PROPORTIONAL_Handle *s = solver;
1230
1231   LOG (GNUNET_ERROR_TYPE_DEBUG,
1232        "Unlocking solver from bulk operation ...\n");
1233   if (s->bulk_lock < 1)
1234   {
1235     GNUNET_break(0);
1236     return;
1237   }
1238   s->bulk_lock--;
1239   if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1240   {
1241     LOG (GNUNET_ERROR_TYPE_INFO,
1242          "No lock pending, recalculating\n");
1243     distribute_bandwidth_in_network (s, NULL);
1244     s->bulk_requests = 0;
1245   }
1246 }
1247
1248
1249 /**
1250  * Transport properties for this address have changed
1251  *
1252  * @param solver solver handle
1253  * @param address the address
1254  * @param type the ATSI type in HBO
1255  * @param abs_value the absolute value of the property
1256  * @param rel_value the normalized value
1257  */
1258 static void
1259 GAS_proportional_address_property_changed (void *solver,
1260                                            struct ATS_Address *address,
1261                                            enum GNUNET_ATS_Property type,
1262                                            uint32_t abs_value,
1263                                            double rel_value)
1264 {
1265   struct GAS_PROPORTIONAL_Handle *s = solver;
1266   struct AddressWrapper *asi;
1267   struct ATS_Address *best_address;
1268   struct ATS_Address *active_address;
1269
1270   asi = address->solver_information;
1271   LOG (GNUNET_ERROR_TYPE_INFO,
1272        "Property `%s' for peer `%s' address %p changed to %.2f\n",
1273        GNUNET_ATS_print_property_type (type),
1274        GNUNET_i2s (&address->peer),
1275        address,
1276        rel_value);
1277
1278   if (0 ==
1279       s->env->get_connectivity (s->env->cls,
1280                                 &address->peer))
1281     return; /* Peer is not requested */
1282
1283   /* This peer is requested, find active and best address */
1284   active_address = get_active_address (s,
1285                                        &address->peer);
1286   best_address = update_active_address (s,
1287                                         &address->peer);
1288   if (active_address == best_address)
1289   {
1290     /* We stuck to the same address, therefore redistribute
1291        (NOTE: because otherwise update_active_address() already
1292        calls distribute_bandwidth_in_network() )*/
1293     distribute_bandwidth_in_network (s, asi->network);
1294   }
1295 }
1296
1297
1298 /**
1299  * Add a new single address to a network
1300  *
1301  * @param solver the solver Handle
1302  * @param address the address to add
1303  * @param network network type of this address
1304  */
1305 static void
1306 GAS_proportional_address_add (void *solver,
1307                               struct ATS_Address *address,
1308                               enum GNUNET_ATS_Network_Type network)
1309 {
1310   struct GAS_PROPORTIONAL_Handle *s = solver;
1311   struct Network *net;
1312   struct AddressWrapper *aw;
1313
1314   GNUNET_assert (network < s->env->network_count);
1315   net = &s->network_entries[network];
1316   net->total_addresses++;
1317
1318   aw = GNUNET_new (struct AddressWrapper);
1319   aw->addr = address;
1320   aw->network = net;
1321   address->solver_information = aw;
1322   GNUNET_CONTAINER_DLL_insert (net->head,
1323                                net->tail,
1324                                aw);
1325   GNUNET_STATISTICS_update (s->env->stats,
1326                             "# ATS addresses total",
1327                             1,
1328                             GNUNET_NO);
1329   GNUNET_STATISTICS_update (s->env->stats,
1330                             net->stat_total,
1331                             1,
1332                             GNUNET_NO);
1333
1334   LOG (GNUNET_ERROR_TYPE_INFO,
1335        "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1336        address,
1337        GNUNET_i2s (&address->peer),
1338        net->total_addresses,
1339        net->active_addresses,
1340        net->desc);
1341
1342   if (0 ==
1343       s->env->get_connectivity (s->env->cls,
1344                                 &address->peer))
1345     return; /* Peer is not requested */
1346
1347   /* This peer is requested, find best address */
1348   update_active_address (s,
1349                          &address->peer);
1350 }
1351
1352
1353 /**
1354  * Remove an address from the solver. To do so, we:
1355  * - Removed it from specific network
1356  * - Decrease the number of total addresses
1357  * - If active:
1358  *   - decrease number of active addreses
1359  *   - update quotas
1360  *
1361  * @param solver the solver handle
1362  * @param address the address to remove
1363  */
1364 static void
1365 GAS_proportional_address_delete (void *solver,
1366                                  struct ATS_Address *address)
1367 {
1368   struct GAS_PROPORTIONAL_Handle *s = solver;
1369   struct AddressWrapper *aw = address->solver_information;
1370   struct Network *net = aw->network;
1371
1372   LOG (GNUNET_ERROR_TYPE_DEBUG,
1373        "Deleting %s address for peer `%s' from network `%s' (total: %u/active: %u)\n",
1374        (GNUNET_NO == address->active) ? "inactive" : "active",
1375        GNUNET_i2s (&address->peer),
1376        net->desc,
1377        net->total_addresses,
1378        net->active_addresses);
1379
1380   GNUNET_CONTAINER_DLL_remove (net->head,
1381                                net->tail,
1382                                aw);
1383   GNUNET_assert (net->total_addresses > 0);
1384   net->total_addresses--;
1385   GNUNET_STATISTICS_update (s->env->stats,
1386                             net->stat_total,
1387                             -1,
1388                             GNUNET_NO);
1389   if (GNUNET_YES == address->active)
1390   {
1391     /* Address was active, remove from network and update quotas*/
1392     address->active = GNUNET_NO;
1393     address->assigned_bw_in = 0;
1394     address->assigned_bw_out = 0;
1395     address_decrement_active (s, net);
1396     /* FIXME: this may trigger the solver unnecessarily,
1397        especially if update_active_address() succeeds! */
1398     distribute_bandwidth_in_network (s, net);
1399
1400     if (NULL ==
1401         update_active_address (s,
1402                                &address->peer))
1403     {
1404       /* No alternative address found, disconnect peer */
1405       LOG (GNUNET_ERROR_TYPE_INFO,
1406            "Disconnecting peer `%s' after deleting previous address %p\n",
1407            GNUNET_i2s (&address->peer),
1408            address);
1409       s->env->bandwidth_changed_cb (s->env->cls,
1410                                     address);
1411     }
1412   }
1413   GNUNET_free (aw);
1414   address->solver_information = NULL;
1415   LOG (GNUNET_ERROR_TYPE_DEBUG,
1416        "After deleting address now total %u and active %u addresses in network `%s'\n",
1417        net->total_addresses,
1418        net->active_addresses,
1419        net->desc);
1420 }
1421
1422
1423 /**
1424  * Function invoked when the plugin is loaded.
1425  *
1426  * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
1427  *            modified to return the API functions (ugh).
1428  * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
1429  */
1430 void *
1431 libgnunet_plugin_ats_proportional_init (void *cls)
1432 {
1433   static struct GNUNET_ATS_SolverFunctions sf;
1434   struct GNUNET_ATS_PluginEnvironment *env = cls;
1435   struct GAS_PROPORTIONAL_Handle *s;
1436   struct Network * cur;
1437   float f_tmp;
1438   unsigned int c;
1439
1440   s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
1441   s->env = env;
1442   sf.cls = s;
1443   sf.s_add = &GAS_proportional_address_add;
1444   sf.s_address_update_property = &GAS_proportional_address_property_changed;
1445   sf.s_get = &GAS_proportional_get_preferred_address;
1446   sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
1447   sf.s_pref = &GAS_proportional_address_change_preference;
1448   sf.s_feedback = &GAS_proportional_address_preference_feedback;
1449   sf.s_del = &GAS_proportional_address_delete;
1450   sf.s_bulk_start = &GAS_proportional_bulk_start;
1451   sf.s_bulk_stop = &GAS_proportional_bulk_stop;
1452   s->stability_factor = PROP_STABILITY_FACTOR;
1453   if (GNUNET_SYSERR !=
1454       GNUNET_CONFIGURATION_get_value_float (env->cfg,
1455                                             "ats",
1456                                             "PROP_STABILITY_FACTOR",
1457                                             &f_tmp))
1458   {
1459     if ((f_tmp < 1.0) || (f_tmp > 2.0))
1460     {
1461       LOG (GNUNET_ERROR_TYPE_ERROR,
1462            _("Invalid %s configuration %f \n"),
1463            "PROP_STABILITY_FACTOR",
1464            f_tmp);
1465     }
1466     else
1467     {
1468       s->stability_factor = f_tmp;
1469       LOG (GNUNET_ERROR_TYPE_INFO,
1470            "Using %s of %.3f\n",
1471            "PROP_STABILITY_FACTOR",
1472            f_tmp);
1473     }
1474   }
1475   s->prop_factor = PROPORTIONALITY_FACTOR;
1476   if (GNUNET_SYSERR !=
1477       GNUNET_CONFIGURATION_get_value_float (env->cfg,
1478                                             "ats",
1479                                             "PROP_PROPORTIONALITY_FACTOR",
1480                                             &f_tmp))
1481   {
1482     if (f_tmp < 1.0)
1483     {
1484       LOG (GNUNET_ERROR_TYPE_ERROR,
1485            _("Invalid %s configuration %f\n"),
1486            "PROP_PROPORTIONALITY_FACTOR",
1487            f_tmp);
1488     }
1489     else
1490     {
1491       s->prop_factor = f_tmp;
1492       LOG (GNUNET_ERROR_TYPE_INFO,
1493            "Using %s of %.3f\n",
1494            "PROP_PROPORTIONALITY_FACTOR",
1495            f_tmp);
1496     }
1497   }
1498
1499   s->network_entries = GNUNET_malloc (env->network_count *
1500                                       sizeof (struct Network));
1501   for (c = 0; c < env->network_count; c++)
1502   {
1503     cur = &s->network_entries[c];
1504     cur->type = c;
1505     cur->total_quota_in = env->in_quota[c];
1506     cur->total_quota_out = env->out_quota[c];
1507     cur->desc = GNUNET_ATS_print_network_type (c);
1508     GNUNET_asprintf (&cur->stat_total,
1509                      "# ATS addresses %s total",
1510                      cur->desc);
1511     GNUNET_asprintf (&cur->stat_active,
1512                      "# ATS active addresses %s total",
1513                      cur->desc);
1514     LOG (GNUNET_ERROR_TYPE_INFO,
1515          "Added network %u `%s' (%llu/%llu)\n",
1516          c,
1517          cur->desc,
1518          cur->total_quota_in,
1519          cur->total_quota_out);
1520   }
1521   return &sf;
1522 }
1523
1524
1525 /**
1526  * Function used to unload the plugin.
1527  *
1528  * @param cls return value from #libgnunet_plugin_ats_proportional_init()
1529  */
1530 void *
1531 libgnunet_plugin_ats_proportional_done (void *cls)
1532 {
1533   struct GNUNET_ATS_SolverFunctions *sf = cls;
1534   struct GAS_PROPORTIONAL_Handle *s = sf->cls;
1535   struct AddressWrapper *cur;
1536   struct AddressWrapper *next;
1537   unsigned int c;
1538
1539   for (c = 0; c < s->network_count; c++)
1540   {
1541     GNUNET_break (0 == s->network_entries[c].total_addresses);
1542     GNUNET_break (0 == s->network_entries[c].active_addresses);
1543     next = s->network_entries[c].head;
1544     while (NULL != (cur = next))
1545     {
1546       next = cur->next;
1547       GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1548                                    s->network_entries[c].tail,
1549                                    cur);
1550       GNUNET_free_non_null (cur->addr->solver_information);
1551       GNUNET_free(cur);
1552     }
1553     GNUNET_free (s->network_entries[c].stat_total);
1554     GNUNET_free (s->network_entries[c].stat_active);
1555   }
1556   GNUNET_break (0 == s->active_addresses);
1557   GNUNET_free (s->network_entries);
1558   GNUNET_free (s);
1559   return NULL;
1560 }
1561
1562
1563 /* end of plugin_ats_proportional.c */