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