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