-restructuring, adding comments
[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,
488          count_addresses,
489          net->active_addresses);
490     for (aw = net->head; NULL != aw; aw = aw->next)
491     {
492       if (GNUNET_YES != aw->addr->active)
493         continue;
494
495       LOG (GNUNET_ERROR_TYPE_WARNING,
496            "Active: `%s' `%s' length %u\n",
497            GNUNET_i2s (&aw->addr->peer),
498            aw->addr->plugin,
499            aw->addr->addr_len);
500     }
501   }
502
503   LOG (GNUNET_ERROR_TYPE_INFO,
504        "Total relative preference %.3f for %u addresses in network %s\n",
505        sum_relative_peer_prefences,
506        net->active_addresses,
507        net->desc);
508
509   for (aw = net->head; NULL != aw; aw = aw->next)
510   {
511     if (GNUNET_YES == aw->addr->active)
512     {
513       peer_relative_prefs = s->env->get_preferences (s->env->cls,
514                                                      &aw->addr->peer);
515
516       cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
517       total_weight = net->active_addresses +
518           s->prop_factor * sum_relative_peer_prefences;
519       peer_weight = (1.0 + (s->prop_factor * cur_pref));
520
521       assigned_quota_in = min_bw
522           + ((peer_weight / total_weight) * remaining_quota_in);
523       assigned_quota_out = min_bw
524           + ((peer_weight / total_weight) * remaining_quota_out);
525
526       LOG (GNUNET_ERROR_TYPE_INFO,
527           "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
528           GNUNET_i2s (&aw->addr->peer),
529            peer_weight,
530            total_weight,
531           assigned_quota_in,
532            assigned_quota_out);
533     }
534     else
535     {
536       assigned_quota_in = 0;
537       assigned_quota_out = 0;
538     }
539
540     quota_in_used += assigned_quota_in;
541     quota_out_used += assigned_quota_out;
542     /* Prevent overflow due to rounding errors */
543     if (assigned_quota_in > UINT32_MAX)
544       assigned_quota_in = UINT32_MAX;
545     if (assigned_quota_out > UINT32_MAX)
546       assigned_quota_out = UINT32_MAX;
547
548     /* Store for later propagation */
549     aw->calculated_quota_in = assigned_quota_in;
550     aw->calculated_quota_out = assigned_quota_out;
551   }
552   LOG (GNUNET_ERROR_TYPE_DEBUG,
553        "Total bandwidth assigned is (in/out): %llu /%llu\n",
554        quota_in_used,
555        quota_out_used);
556   if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
557   {
558     LOG (GNUNET_ERROR_TYPE_ERROR,
559          "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
560          net->active_addresses,
561          quota_out_used,
562          net->total_quota_out);
563   }
564   if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
565   {
566     LOG (GNUNET_ERROR_TYPE_ERROR,
567          "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
568          net->active_addresses,
569          quota_in_used,
570          net->total_quota_in);
571   }
572 }
573
574
575 /**
576  * Notify ATS service of bandwidth changes to addresses.
577  *
578  * @param s solver handle
579  * @param net the network to propagate changes in
580  */
581 static void
582 propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
583                      struct Network *net)
584 {
585   struct AddressWrapper *cur;
586
587   for (cur = net->head; NULL != cur; cur = cur->next)
588   {
589     if ( (cur->addr->assigned_bw_in == cur->calculated_quota_in) &&
590          (cur->addr->assigned_bw_out == cur->calculated_quota_out) )
591       continue;
592     cur->addr->assigned_bw_in = cur->calculated_quota_in;
593     cur->addr->assigned_bw_out = cur->calculated_quota_out;
594
595     LOG (GNUNET_ERROR_TYPE_DEBUG,
596          "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
597          (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
598          cur->addr,
599          GNUNET_i2s (&cur->addr->peer),
600          cur->addr->assigned_bw_in,
601          cur->addr->assigned_bw_out);
602
603     /* Notify on change */
604     if (GNUNET_YES == cur->addr->active)
605       s->env->bandwidth_changed_cb (s->env->cls,
606                                     cur->addr);
607   }
608 }
609
610
611 /**
612  * Distribute bandwidth.  The addresses have already been selected,
613  * this is merely distributed the bandwidth among the addresses.
614  *
615  * @param s the solver handle
616  * @param n the network, can be NULL for all networks
617  */
618 static void
619 distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
620                                  struct Network *n)
621 {
622   unsigned int i;
623
624   if (GNUNET_YES == s->bulk_lock)
625   {
626     s->bulk_requests++;
627     return;
628   }
629   if (NULL != n)
630   {
631     LOG (GNUNET_ERROR_TYPE_DEBUG,
632         "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
633          GNUNET_ATS_print_network_type(n->type),
634          n->active_addresses,
635          n->total_addresses);
636     s->env->info_cb (s->env->cls,
637                      GAS_OP_SOLVE_START,
638                      GAS_STAT_SUCCESS,
639                      GAS_INFO_PROP_SINGLE);
640     distribute_bandwidth(s,
641                          n);
642     s->env->info_cb (s->env->cls,
643                      GAS_OP_SOLVE_STOP,
644                      GAS_STAT_SUCCESS,
645                      GAS_INFO_PROP_SINGLE);
646     s->env->info_cb (s->env->cls,
647                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
648                      GAS_STAT_SUCCESS,
649                      GAS_INFO_PROP_SINGLE);
650     propagate_bandwidth (s,
651                          n);
652
653     s->env->info_cb (s->env->cls,
654                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
655                      GAS_STAT_SUCCESS,
656                      GAS_INFO_PROP_SINGLE);
657   }
658   else
659   {
660     s->env->info_cb (s->env->cls,
661                      GAS_OP_SOLVE_START,
662                      GAS_STAT_SUCCESS,
663                      GAS_INFO_PROP_ALL);
664     for (i = 0; i < s->network_count; i++)
665       distribute_bandwidth (s,
666                             &s->network_entries[i]);
667     s->env->info_cb (s->env->cls,
668                      GAS_OP_SOLVE_STOP,
669                      GAS_STAT_SUCCESS,
670                      GAS_INFO_PROP_ALL);
671     s->env->info_cb (s->env->cls,
672                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
673                      GAS_STAT_SUCCESS,
674                      GAS_INFO_PROP_ALL);
675     for (i = 0; i < s->network_count; i++)
676       propagate_bandwidth (s,
677                            &s->network_entries[i]);
678     s->env->info_cb (s->env->cls,
679                      GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
680                      GAS_STAT_SUCCESS,
681                      GAS_INFO_PROP_ALL);
682   }
683 }
684
685
686 /**
687  * Context for finding the best address* Linked list of addresses in this network: head
688  */
689 struct FindBestAddressCtx
690 {
691   /**
692    * The solver handle
693    */
694   struct GAS_PROPORTIONAL_Handle *s;
695
696   /**
697    * The currently best address
698    */
699   struct ATS_Address *best;
700 };
701
702
703 /**
704  * Find index of a ATS property type in the quality properties array.
705  *
706  * @param type ATS property type
707  * @return index in the quality array, #GNUNET_SYSERR if the type
708  *         was not a quality property
709  */
710 static int
711 find_quality_property_index (enum GNUNET_ATS_Property type)
712 {
713   enum GNUNET_ATS_Property existing_types[] = GNUNET_ATS_QualityProperties;
714   unsigned int c;
715
716   for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
717     if (existing_types[c] == type)
718       return c;
719   GNUNET_break (0);
720   return GNUNET_SYSERR;
721 }
722
723
724 /**
725  * Find a "good" address to use for a peer by iterating over the
726  * addresses for this peer.  If we already have an existing address,
727  * we stick to it.  Otherwise, we pick by lowest distance and then by
728  * lowest latency.
729  *
730  * @param cls the `struct FindBestAddressCtx *' where we store the result
731  * @param key the peer we are trying to find the best address for
732  * @param value another `struct ATS_Address*` to consider using
733  * @return #GNUNET_OK (continue to iterate)
734  */
735 static int
736 find_best_address_it (void *cls,
737                       const struct GNUNET_PeerIdentity *key,
738                       void *value)
739 {
740   struct FindBestAddressCtx *ctx = cls;
741   struct ATS_Address *current = value;
742   struct AddressWrapper *asi = current->solver_information;
743   struct GNUNET_TIME_Relative active_time;
744   double best_delay;
745   double best_distance;
746   double cur_delay;
747   double cur_distance;
748   int index;
749
750   if (GNUNET_NO ==
751       is_bandwidth_available_in_network (asi->network))
752   {
753     /* There's no bandwidth available in this network,
754        so we cannot use this address. */
755     return GNUNET_OK;
756   }
757   if (GNUNET_YES == current->active)
758   {
759     active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
760     if (active_time.rel_value_us <=
761         ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) * ctx->s->stability_factor)
762     {
763       /* Keep active address for stability reasons */
764       ctx->best = current;
765       return GNUNET_NO;
766     }
767   }
768   if (NULL == ctx->best)
769   {
770     /* We so far have nothing else, so go with it! */
771     ctx->best = current;
772     return GNUNET_OK;
773   }
774
775   /* Now compare ATS information */
776   index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
777   cur_distance = current->atsin[index].norm;
778   best_distance = ctx->best->atsin[index].norm;
779   index = find_quality_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
780   cur_delay = current->atsin[index].norm;
781   best_delay = ctx->best->atsin[index].norm;
782
783   /* user shorter distance */
784   if (cur_distance < best_distance)
785   {
786     if (GNUNET_NO == ctx->best->active)
787     {
788       /* Activity doesn't influence the equation, use current */
789       ctx->best = current;
790     }
791     else if ((best_distance / cur_distance) > ctx->s->stability_factor)
792     {
793       /* Distance change is significant, switch active address! */
794       ctx->best = current;
795     }
796   }
797
798   /* User connection with less delay */
799   if (cur_delay < best_delay)
800   {
801     if (GNUNET_NO == ctx->best->active)
802     {
803       /* Activity doesn't influence the equation, use current */
804       ctx->best = current;
805     }
806     else if ((best_delay / cur_delay) > ctx->s->stability_factor)
807     {
808       /* Latency change is significant, switch active address! */
809       ctx->best = current;
810     }
811   }
812   return GNUNET_OK;
813 }
814
815
816 /**
817  * Find the currently best address for a peer from the set of
818  * addresses available or return NULL of no address is available.
819  *
820  * @param s the proportional handle
821  * @param addresses the address hashmap
822  * @param id the peer id
823  * @return the address or NULL
824  */
825 struct ATS_Address *
826 get_best_address (struct GAS_PROPORTIONAL_Handle *s,
827                   struct GNUNET_CONTAINER_MultiPeerMap *addresses,
828                   const struct GNUNET_PeerIdentity *id)
829 {
830   struct FindBestAddressCtx fba_ctx;
831
832   fba_ctx.best = NULL;
833   fba_ctx.s = s;
834   GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
835                                               id,
836                                               &find_best_address_it,
837                                               &fba_ctx);
838   return fba_ctx.best;
839 }
840
841
842 /**
843  * Decrease address count in network
844  *
845  * @param s the solver handle
846  * @param net the network type
847  */
848 static void
849 address_decrement_active (struct GAS_PROPORTIONAL_Handle *s,
850                           struct Network *net)
851 {
852   if (net->active_addresses < 1)
853   {
854     GNUNET_break (0);
855   }
856   else
857   {
858     net->active_addresses--;
859     GNUNET_STATISTICS_update (s->env->stats,
860                               net->stat_active, -1, GNUNET_NO);
861   }
862   if (s->active_addresses < 1)
863   {
864     GNUNET_break (0);
865   }
866   else
867   {
868     s->active_addresses--;
869     GNUNET_STATISTICS_update (s->env->stats,
870                               "# ATS addresses total",
871                               -1,
872                               GNUNET_NO);
873   }
874 }
875
876
877 /**
878  * Address map iterator to find current active address for peer.
879  * Asserts that only one address is active per peer.
880  *
881  * @param cls last active address
882  * @param key peer's key
883  * @param value address to check
884  * @return #GNUNET_NO on double active address else #GNUNET_YES;
885  */
886 static int
887 get_active_address_it (void *cls,
888                        const struct GNUNET_PeerIdentity *key,
889                        void *value)
890 {
891   struct ATS_Address **dest = cls;
892   struct ATS_Address *aa = value;
893
894   if (GNUNET_YES != aa->active)
895     return GNUNET_OK;
896   GNUNET_assert (NULL == (*dest));
897   (*dest) = aa;
898   return GNUNET_OK;
899 }
900
901
902 /**
903  * Find current active address for peer
904  *
905  * @param s the solver handle
906  * @param peer the peer
907  * @return active address or NULL
908  */
909 static struct ATS_Address *
910 get_active_address (struct GAS_PROPORTIONAL_Handle *s,
911                     const struct GNUNET_PeerIdentity *peer)
912 {
913   struct ATS_Address *dest;
914
915   dest = NULL;
916   GNUNET_CONTAINER_multipeermap_get_multiple (s->env->addresses,
917                                               peer,
918                                               &get_active_address_it,
919                                               &dest);
920   return dest;
921 }
922
923
924 /**
925  * Update active address for a peer:
926  * Check if active address exists and what the best address is, if addresses
927  * are different switch
928  *
929  * @param s solver handle
930  * @param peer the peer to check
931  * return the new address or NULL if no update was performed
932   */
933 static struct ATS_Address *
934 update_active_address (struct GAS_PROPORTIONAL_Handle *s,
935                        const struct GNUNET_PeerIdentity *peer)
936 {
937   struct ATS_Address *best_address;
938   struct ATS_Address *current_address;
939   struct AddressWrapper *asi;
940   struct Network *net;
941
942   LOG (GNUNET_ERROR_TYPE_INFO,
943        "Updating active address for peer `%s'\n",
944        GNUNET_i2s (peer));
945
946   /* Find active address */
947   current_address = get_active_address (s,
948                                         peer);
949
950   LOG (GNUNET_ERROR_TYPE_INFO,
951        "Peer `%s' has active address %p\n",
952        GNUNET_i2s (peer),
953        current_address);
954
955   /* Find best address */
956   best_address = get_best_address (s,
957                                    s->env->addresses,
958                                    peer);
959   LOG (GNUNET_ERROR_TYPE_INFO,
960        "Peer `%s' has best address %p\n",
961        GNUNET_i2s (peer),
962        best_address);
963
964   if (NULL != current_address)
965   {
966     GNUNET_assert (GNUNET_YES == current_address->active);
967     if ( (NULL == best_address) ||
968          ( (NULL != best_address) &&
969            (current_address != best_address) ) )
970     {
971       /* We switch to a new address (or to none),
972          mark old address as inactive */
973       LOG (GNUNET_ERROR_TYPE_INFO,
974            "Disabling previous %s address %p for peer `%s'\n",
975            (GNUNET_NO == current_address->active) ? "inactive" : "active",
976            current_address,
977            GNUNET_i2s (peer));
978
979       asi = current_address->solver_information;
980       net = asi->network;
981       asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
982       current_address->active = GNUNET_NO; /* No active any longer */
983       current_address->assigned_bw_in = 0; /* no bandwidth assigned */
984       current_address->assigned_bw_out = 0; /* no bandwidth assigned */
985       address_decrement_active (s, net);
986       distribute_bandwidth_in_network (s, net);
987     }
988     if (NULL == best_address)
989     {
990       /* We previously had an active address, but now we cannot suggest one
991        * Therefore we have to disconnect the peer */
992       LOG (GNUNET_ERROR_TYPE_INFO,
993            "Disconnecting peer `%s' with previous address %p\n",
994            GNUNET_i2s (peer),
995            current_address);
996       s->env->bandwidth_changed_cb (s->env->cls,
997                                     current_address);
998     }
999   }
1000   if (NULL == best_address)
1001   {
1002     LOG (GNUNET_ERROR_TYPE_INFO,
1003          "Cannot suggest address for peer `%s'\n",
1004          GNUNET_i2s (peer));
1005     return NULL;
1006   }
1007
1008   LOG (GNUNET_ERROR_TYPE_INFO,
1009        "Suggesting new address %p for peer `%s'\n",
1010        best_address,
1011        GNUNET_i2s (peer));
1012
1013   if ( (NULL != current_address) &&
1014        (best_address == current_address) )
1015   {
1016     GNUNET_break (GNUNET_NO != current_address->active);
1017     return best_address; /* Same same */
1018   }
1019
1020   asi = best_address->solver_information;
1021   GNUNET_assert (NULL != asi);
1022   net = asi->network;
1023
1024   /* Mark address as active */
1025   asi->activated = GNUNET_TIME_absolute_get ();
1026   best_address->active = GNUNET_YES;
1027
1028   net->active_addresses++;
1029   s->active_addresses++;
1030   GNUNET_STATISTICS_update (s->env->stats,
1031                             "# ATS active addresses total",
1032                             1,
1033                             GNUNET_NO);
1034   GNUNET_STATISTICS_update (s->env->stats,
1035                             net->stat_active,
1036                             1,
1037                             GNUNET_NO);
1038   LOG (GNUNET_ERROR_TYPE_INFO,
1039        "Address %p for peer `%s' is now active\n",
1040        best_address,
1041        GNUNET_i2s (peer));
1042   /* Distribute bandwidth */
1043   distribute_bandwidth_in_network (s, net);
1044   return best_address;
1045 }
1046
1047
1048 /**
1049  * Changes the preferences for a peer in the problem
1050  *
1051  * @param solver the solver handle
1052  * @param peer the peer to change the preference for
1053  * @param kind the kind to change the preference
1054  * @param pref_rel the normalized preference value for this kind over all clients
1055  */
1056 static void
1057 GAS_proportional_address_change_preference (void *solver,
1058                                             const struct GNUNET_PeerIdentity *peer,
1059                                             enum GNUNET_ATS_PreferenceKind kind,
1060                                             double pref_rel)
1061 {
1062   struct GAS_PROPORTIONAL_Handle *s = solver;
1063   struct ATS_Address *best_address;
1064   struct ATS_Address *active_address;
1065   struct AddressWrapper *asi;
1066
1067   if (0 ==
1068       s->env->get_connectivity (s->env->cls,
1069                                 peer))
1070     return; /* Peer is not requested */
1071
1072   /* This peer is requested, find best address */
1073   active_address = get_active_address (s,
1074                                        peer);
1075   best_address = update_active_address (s, peer);
1076
1077   if ( (NULL != best_address) &&
1078        (active_address == best_address) )
1079   {
1080     asi = best_address->solver_information;
1081     GNUNET_assert (NULL != asi);
1082
1083     /* We sticked to the same address, therefore redistribute  */
1084     distribute_bandwidth_in_network (s, asi->network);
1085   }
1086 }
1087
1088
1089 /**
1090  * Get application feedback for a peer
1091  *
1092  * @param solver the solver handle
1093  * @param application the application
1094  * @param peer the peer to change the preference for
1095  * @param scope the time interval for this feedback: [now - scope .. now]
1096  * @param kind the kind to change the preference
1097  * @param score the score
1098  */
1099 static void
1100 GAS_proportional_address_preference_feedback (void *solver,
1101                                               struct GNUNET_SERVER_Client *application,
1102                                               const struct GNUNET_PeerIdentity *peer,
1103                                               const struct GNUNET_TIME_Relative scope,
1104                                               enum GNUNET_ATS_PreferenceKind kind,
1105                                               double score)
1106 {
1107   struct GAS_PROPORTIONAL_Handle *s = solver;
1108
1109   GNUNET_assert(NULL != peer);
1110   GNUNET_assert(NULL != s);
1111 }
1112
1113
1114 /**
1115  * Get the preferred address for a specific peer
1116  *
1117  * @param solver the solver handle
1118  * @param peer the identity of the peer
1119  * @return best address
1120  */
1121 static const struct ATS_Address *
1122 GAS_proportional_get_preferred_address (void *solver,
1123                                         const struct GNUNET_PeerIdentity *peer)
1124 {
1125   struct GAS_PROPORTIONAL_Handle *s = solver;
1126   const struct ATS_Address *best_address;
1127
1128   best_address = update_active_address (s, peer);
1129   if (s->bulk_lock > 0)
1130     return NULL;
1131   return best_address;
1132 }
1133
1134
1135 /**
1136  * Stop notifying about address and bandwidth changes for this peer
1137  *
1138  * @param solver the solver handle
1139  * @param peer the peer
1140  */
1141 static void
1142 GAS_proportional_stop_get_preferred_address (void *solver,
1143                                              const struct GNUNET_PeerIdentity *peer)
1144 {
1145   struct GAS_PROPORTIONAL_Handle *s = solver;
1146   struct ATS_Address *cur;
1147   struct AddressWrapper *asi;
1148   struct Network *cur_net;
1149
1150   cur = get_active_address (s,
1151                             peer);
1152   if (NULL != cur)
1153   {
1154     LOG (GNUNET_ERROR_TYPE_INFO,
1155          "Disabling %s address %p for peer `%s'\n",
1156          (GNUNET_NO == cur->active) ? "inactive" : "active",
1157          cur,
1158          GNUNET_i2s (&cur->peer));
1159
1160     /* Disabling current address */
1161     asi = cur->solver_information;
1162     cur_net = asi->network;
1163     asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
1164     cur->active = GNUNET_NO; /* No active any longer */
1165     cur->assigned_bw_in = 0; /* no bandwidth assigned */
1166     cur->assigned_bw_out = 0; /* no bandwidth assigned */
1167
1168     address_decrement_active (s, cur_net);
1169
1170     distribute_bandwidth_in_network (s, cur_net);
1171   }
1172 }
1173
1174
1175 /**
1176  * Start a bulk operation
1177  *
1178  * @param solver the solver
1179  */
1180 static void
1181 GAS_proportional_bulk_start (void *solver)
1182 {
1183   struct GAS_PROPORTIONAL_Handle *s = solver;
1184
1185   LOG (GNUNET_ERROR_TYPE_DEBUG,
1186        "Locking solver for bulk operation ...\n");
1187   GNUNET_assert (NULL != solver);
1188   s->bulk_lock++;
1189 }
1190
1191
1192 /**
1193  * Bulk operation done.
1194  *
1195  * @param solver our `struct GAS_PROPORTIONAL_Handle *`
1196  */
1197 static void
1198 GAS_proportional_bulk_stop (void *solver)
1199 {
1200   struct GAS_PROPORTIONAL_Handle *s = solver;
1201
1202   LOG (GNUNET_ERROR_TYPE_DEBUG,
1203        "Unlocking solver from bulk operation ...\n");
1204   if (s->bulk_lock < 1)
1205   {
1206     GNUNET_break(0);
1207     return;
1208   }
1209   s->bulk_lock--;
1210   if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
1211   {
1212     LOG (GNUNET_ERROR_TYPE_INFO,
1213          "No lock pending, recalculating\n");
1214     distribute_bandwidth_in_network (s, NULL);
1215     s->bulk_requests = 0;
1216   }
1217 }
1218
1219
1220 /**
1221  * Transport properties for this address have changed
1222  *
1223  * @param solver solver handle
1224  * @param address the address
1225  * @param type the ATSI type in HBO
1226  * @param abs_value the absolute value of the property
1227  * @param rel_value the normalized value
1228  */
1229 static void
1230 GAS_proportional_address_property_changed (void *solver,
1231                                            struct ATS_Address *address,
1232                                            enum GNUNET_ATS_Property type,
1233                                            uint32_t abs_value,
1234                                            double rel_value)
1235 {
1236   struct GAS_PROPORTIONAL_Handle *s = solver;
1237   struct AddressWrapper *asi;
1238   struct ATS_Address *best_address;
1239   struct ATS_Address *active_address;
1240
1241   asi = address->solver_information;
1242   LOG (GNUNET_ERROR_TYPE_INFO,
1243        "Property `%s' for peer `%s' address %p changed to %.2f\n",
1244        GNUNET_ATS_print_property_type (type),
1245        GNUNET_i2s (&address->peer),
1246        address,
1247        rel_value);
1248
1249   if (0 ==
1250       s->env->get_connectivity (s->env->cls,
1251                                 &address->peer))
1252     return; /* Peer is not requested */
1253
1254   /* This peer is requested, find active and best address */
1255   active_address = get_active_address (s,
1256                                        &address->peer);
1257   best_address = update_active_address (s,
1258                                         &address->peer);
1259   if (active_address == best_address)
1260   {
1261     /* We stuck to the same address, therefore redistribute
1262        (NOTE: because otherwise update_active_address() already
1263        calls distribute_bandwidth_in_network() )*/
1264     distribute_bandwidth_in_network (s, asi->network);
1265   }
1266 }
1267
1268
1269 /**
1270  * Add a new single address to a network
1271  *
1272  * @param solver the solver Handle
1273  * @param address the address to add
1274  * @param network network type of this address
1275  */
1276 static void
1277 GAS_proportional_address_add (void *solver,
1278                               struct ATS_Address *address,
1279                               enum GNUNET_ATS_Network_Type network)
1280 {
1281   struct GAS_PROPORTIONAL_Handle *s = solver;
1282   struct Network *net;
1283   struct AddressWrapper *aw;
1284
1285   GNUNET_assert (network < s->env->network_count);
1286   net = &s->network_entries[network];
1287   net->total_addresses++;
1288
1289   aw = GNUNET_new (struct AddressWrapper);
1290   aw->addr = address;
1291   aw->network = net;
1292   address->solver_information = aw;
1293   GNUNET_CONTAINER_DLL_insert (net->head,
1294                                net->tail,
1295                                aw);
1296   GNUNET_STATISTICS_update (s->env->stats,
1297                             "# ATS addresses total",
1298                             1,
1299                             GNUNET_NO);
1300   GNUNET_STATISTICS_update (s->env->stats,
1301                             net->stat_total,
1302                             1,
1303                             GNUNET_NO);
1304
1305   LOG (GNUNET_ERROR_TYPE_INFO,
1306        "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
1307        address,
1308        GNUNET_i2s (&address->peer),
1309        net->total_addresses,
1310        net->active_addresses,
1311        net->desc);
1312
1313   if (0 ==
1314       s->env->get_connectivity (s->env->cls,
1315                                 &address->peer))
1316     return; /* Peer is not requested */
1317
1318   /* This peer is requested, find best address */
1319   update_active_address (s,
1320                          &address->peer);
1321 }
1322
1323
1324 /**
1325  * Remove an address from the solver. To do so, we:
1326  * - Removed it from specific network
1327  * - Decrease the number of total addresses
1328  * - If active:
1329  *   - decrease number of active addreses
1330  *   - update quotas
1331  *
1332  * @param solver the solver handle
1333  * @param address the address to remove
1334  */
1335 static void
1336 GAS_proportional_address_delete (void *solver,
1337                                  struct ATS_Address *address)
1338 {
1339   struct GAS_PROPORTIONAL_Handle *s = solver;
1340   struct AddressWrapper *aw = address->solver_information;
1341   struct Network *net = aw->network;
1342
1343   LOG (GNUNET_ERROR_TYPE_DEBUG,
1344        "Deleting %s address for peer `%s' from network `%s' (total: %u/active: %u)\n",
1345        (GNUNET_NO == address->active) ? "inactive" : "active",
1346        GNUNET_i2s (&address->peer),
1347        net->desc,
1348        net->total_addresses,
1349        net->active_addresses);
1350
1351   GNUNET_CONTAINER_DLL_remove (net->head,
1352                                net->tail,
1353                                aw);
1354   GNUNET_assert (net->total_addresses > 0);
1355   net->total_addresses--;
1356   GNUNET_STATISTICS_update (s->env->stats,
1357                             net->stat_total,
1358                             -1,
1359                             GNUNET_NO);
1360   if (GNUNET_YES == address->active)
1361   {
1362     /* Address was active, remove from network and update quotas*/
1363     address->active = GNUNET_NO;
1364     address->assigned_bw_in = 0;
1365     address->assigned_bw_out = 0;
1366     address_decrement_active (s, net);
1367     /* FIXME: this may trigger the solver unnecessarily,
1368        especially if update_active_address() succeeds! */
1369     distribute_bandwidth_in_network (s, net);
1370
1371     if (NULL ==
1372         update_active_address (s,
1373                                &address->peer))
1374     {
1375       /* No alternative address found, disconnect peer */
1376       LOG (GNUNET_ERROR_TYPE_INFO,
1377            "Disconnecting peer `%s' after deleting previous address %p\n",
1378            GNUNET_i2s (&address->peer),
1379            address);
1380       s->env->bandwidth_changed_cb (s->env->cls,
1381                                     address);
1382     }
1383   }
1384   GNUNET_free (aw);
1385   address->solver_information = NULL;
1386   LOG (GNUNET_ERROR_TYPE_DEBUG,
1387        "After deleting address now total %u and active %u addresses in network `%s'\n",
1388        net->total_addresses,
1389        net->active_addresses,
1390        net->desc);
1391 }
1392
1393
1394 /**
1395  * Function invoked when the plugin is loaded.
1396  *
1397  * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
1398  *            modified to return the API functions (ugh).
1399  * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
1400  */
1401 void *
1402 libgnunet_plugin_ats_proportional_init (void *cls)
1403 {
1404   static struct GNUNET_ATS_SolverFunctions sf;
1405   struct GNUNET_ATS_PluginEnvironment *env = cls;
1406   struct GAS_PROPORTIONAL_Handle *s;
1407   struct Network * cur;
1408   float f_tmp;
1409   unsigned int c;
1410
1411   s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
1412   s->env = env;
1413   sf.cls = s;
1414   sf.s_add = &GAS_proportional_address_add;
1415   sf.s_address_update_property = &GAS_proportional_address_property_changed;
1416   sf.s_get = &GAS_proportional_get_preferred_address;
1417   sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
1418   sf.s_pref = &GAS_proportional_address_change_preference;
1419   sf.s_feedback = &GAS_proportional_address_preference_feedback;
1420   sf.s_del = &GAS_proportional_address_delete;
1421   sf.s_bulk_start = &GAS_proportional_bulk_start;
1422   sf.s_bulk_stop = &GAS_proportional_bulk_stop;
1423   s->stability_factor = PROP_STABILITY_FACTOR;
1424   if (GNUNET_SYSERR !=
1425       GNUNET_CONFIGURATION_get_value_float (env->cfg,
1426                                             "ats",
1427                                             "PROP_STABILITY_FACTOR",
1428                                             &f_tmp))
1429   {
1430     if ((f_tmp < 1.0) || (f_tmp > 2.0))
1431     {
1432       LOG (GNUNET_ERROR_TYPE_ERROR,
1433            _("Invalid %s configuration %f \n"),
1434            "PROP_STABILITY_FACTOR",
1435            f_tmp);
1436     }
1437     else
1438     {
1439       s->stability_factor = f_tmp;
1440       LOG (GNUNET_ERROR_TYPE_INFO,
1441            "Using %s of %.3f\n",
1442            "PROP_STABILITY_FACTOR",
1443            f_tmp);
1444     }
1445   }
1446   s->prop_factor = PROPORTIONALITY_FACTOR;
1447   if (GNUNET_SYSERR !=
1448       GNUNET_CONFIGURATION_get_value_float (env->cfg,
1449                                             "ats",
1450                                             "PROP_PROPORTIONALITY_FACTOR",
1451                                             &f_tmp))
1452   {
1453     if (f_tmp < 1.0)
1454     {
1455       LOG (GNUNET_ERROR_TYPE_ERROR,
1456            _("Invalid %s configuration %f\n"),
1457            "PROP_PROPORTIONALITY_FACTOR",
1458            f_tmp);
1459     }
1460     else
1461     {
1462       s->prop_factor = f_tmp;
1463       LOG (GNUNET_ERROR_TYPE_INFO,
1464            "Using %s of %.3f\n",
1465            "PROP_PROPORTIONALITY_FACTOR",
1466            f_tmp);
1467     }
1468   }
1469
1470   s->network_entries = GNUNET_malloc (env->network_count *
1471                                       sizeof (struct Network));
1472   for (c = 0; c < env->network_count; c++)
1473   {
1474     cur = &s->network_entries[c];
1475     cur->type = c;
1476     cur->total_quota_in = env->in_quota[c];
1477     cur->total_quota_out = env->out_quota[c];
1478     cur->desc = GNUNET_ATS_print_network_type (c);
1479     GNUNET_asprintf (&cur->stat_total,
1480                      "# ATS addresses %s total",
1481                      cur->desc);
1482     GNUNET_asprintf (&cur->stat_active,
1483                      "# ATS active addresses %s total",
1484                      cur->desc);
1485     LOG (GNUNET_ERROR_TYPE_INFO,
1486          "Added network %u `%s' (%llu/%llu)\n",
1487          c,
1488          cur->desc,
1489          cur->total_quota_in,
1490          cur->total_quota_out);
1491   }
1492   return &sf;
1493 }
1494
1495
1496 /**
1497  * Function used to unload the plugin.
1498  *
1499  * @param cls return value from #libgnunet_plugin_ats_proportional_init()
1500  */
1501 void *
1502 libgnunet_plugin_ats_proportional_done (void *cls)
1503 {
1504   struct GNUNET_ATS_SolverFunctions *sf = cls;
1505   struct GAS_PROPORTIONAL_Handle *s = sf->cls;
1506   struct AddressWrapper *cur;
1507   struct AddressWrapper *next;
1508   unsigned int c;
1509
1510   for (c = 0; c < s->network_count; c++)
1511   {
1512     GNUNET_break (0 == s->network_entries[c].total_addresses);
1513     GNUNET_break (0 == s->network_entries[c].active_addresses);
1514     next = s->network_entries[c].head;
1515     while (NULL != (cur = next))
1516     {
1517       next = cur->next;
1518       GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
1519                                    s->network_entries[c].tail,
1520                                    cur);
1521       GNUNET_free_non_null (cur->addr->solver_information);
1522       GNUNET_free(cur);
1523     }
1524     GNUNET_free (s->network_entries[c].stat_total);
1525     GNUNET_free (s->network_entries[c].stat_active);
1526   }
1527   GNUNET_break (0 == s->active_addresses);
1528   GNUNET_free (s->network_entries);
1529   GNUNET_free (s);
1530   return NULL;
1531 }
1532
1533
1534 /* end of plugin_ats_proportional.c */