- cleanup
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.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_addresses.c
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_performance.h"
32 #include "gnunet-service-ats_scheduling.h"
33 #include "gnunet-service-ats_reservations.h"
34 #if HAVE_LIBGLPK
35 #include "gnunet-service-ats_addresses_mlp.h"
36 #endif
37
38 #define VERBOSE GNUNET_NO
39
40 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
41
42 enum ATS_Mode
43 {
44   /*
45    * Assign each peer an equal amount of bandwidth (bw)
46    *
47    * bw_per_peer = bw_total / #active addresses
48    */
49   SIMPLE,
50
51   /*
52    * Use MLP solver to assign bandwidth
53    */
54   MLP
55 };
56
57 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
58
59 #if HAVE_LIBGLPK
60 static struct GAS_MLP_Handle *mlp;
61 #endif
62
63 static unsigned long long wan_quota_in;
64
65 static unsigned long long wan_quota_out;
66
67 static unsigned int active_addr_count;
68
69 static int ats_mode;
70
71 static int running;
72
73
74 static void
75 send_bw_notification (struct ATS_Address *aa)
76 {
77   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
78               GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
79               ntohl (aa->assigned_bw_out.value__));
80   GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
81                                               aa->addr_len, aa->session_id,
82                                               aa->ats, aa->ats_count,
83                                               aa->assigned_bw_out,
84                                               aa->assigned_bw_in);
85   GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
86   GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
87                                   aa->ats, aa->ats_count, aa->assigned_bw_out,
88                                   aa->assigned_bw_in);
89 }
90
91 /**
92  * Update a bandwidth assignment for a peer.  This trivial method currently
93  * simply assigns the same share to all active connections.
94  *
95  * @param cls unused
96  * @param key unused
97  * @param value the 'struct ATS_Address'
98  * @return GNUNET_OK (continue to iterate)
99  */
100 static int
101 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
102 {
103   struct ATS_Address *aa = value;
104
105   if (GNUNET_YES != aa->active)
106     return GNUNET_OK;
107   GNUNET_assert (active_addr_count > 0);
108
109
110   /* Simple method */
111   aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
112   aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
113
114   send_bw_notification (aa);
115
116   return GNUNET_OK;
117 }
118
119
120 /**
121  * Some (significant) input changed, recalculate bandwidth assignment
122  * for all peers.
123  */
124 static void
125 recalculate_assigned_bw ()
126 {
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Recalculating bandwidth for all active connections\n");
129   GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
130                             1, GNUNET_NO);
131   GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
132                          GNUNET_NO);
133
134   GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
135 }
136
137 /**
138  * Free the given address
139  * @param addr address to destroy
140  */
141 static void
142 free_address (struct ATS_Address *addr)
143 {
144   GNUNET_free_non_null (addr->ats);
145   GNUNET_free (addr->plugin);
146   GNUNET_free (addr);
147 }
148
149 /**
150  * Create a ATS_address with the given information
151  * @param peer peer
152  * @param plugin_name plugin
153  * @param plugin_addr address
154  * @param plugin_addr_len address length
155  * @param session_id session
156  * @return the ATS_Address
157  */
158 static struct ATS_Address *
159 create_address (const struct GNUNET_PeerIdentity *peer,
160                 const char *plugin_name,
161                 const void *plugin_addr, size_t plugin_addr_len,
162                 uint32_t session_id)
163 {
164   struct ATS_Address *aa = NULL;
165
166   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
167   aa->peer = *peer;
168   aa->addr_len = plugin_addr_len;
169   aa->addr = &aa[1];
170   memcpy (&aa[1], plugin_addr, plugin_addr_len);
171   aa->plugin = GNUNET_strdup (plugin_name);
172   aa->session_id = session_id;
173   return aa;
174 }
175
176
177 /**
178  * Destroy the given address.
179  *
180  * @param addr address to destroy
181  * @return GNUNET_YES if bandwidth allocations should be recalcualted
182  */
183 static int
184 destroy_address (struct ATS_Address *addr)
185 {
186   int ret;
187
188   ret = GNUNET_NO;
189   GNUNET_assert (GNUNET_YES ==
190                  GNUNET_CONTAINER_multihashmap_remove (addresses,
191                                                        &addr->peer.hashPubKey,
192                                                        addr));
193
194 #if HAVE_LIBGLPK
195   if (ats_mode == MLP)
196     GAS_mlp_address_delete (mlp, addresses, addr);
197 #endif
198
199   if (GNUNET_YES == addr->active)
200   {
201     active_addr_count--;
202     addr->active = GNUNET_NO;
203     ret = GNUNET_YES;
204   }
205   free_address (addr);
206   return ret;
207 }
208
209
210 struct CompareAddressContext
211 {
212   const struct ATS_Address *search;
213
214   /* exact_address != NULL if address and session is equal */
215   struct ATS_Address *exact_address;
216   /* exact_address != NULL if address and session is 0 */
217   struct ATS_Address *base_address;
218 };
219
220
221 static int
222 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
223 {
224   struct CompareAddressContext *cac = cls;
225   struct ATS_Address *aa = value;
226 /*
227   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228               "Comparing to: %s %s %u session %u\n",
229               GNUNET_i2s (&aa->peer), aa->plugin, aa->addr_len, aa->session_id);
230
231 */
232   /* find an exact matching address: aa->addr == cac->search->addr && aa->session == cac->search->session */
233   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
234   {
235       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
236       {
237         cac->exact_address = aa;
238       }
239   }
240
241   /* find an matching address: aa->addr == cac->search->addr && aa->session == 0 */
242   /* this address can be used to be updated */
243   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
244   {
245       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
246       {
247         cac->base_address = aa;
248       }
249   }
250
251   if (cac->exact_address == NULL)
252     return GNUNET_YES;
253   else
254     return GNUNET_NO;
255 }
256
257
258 /**
259  * Find an existing equivalent address record.
260  * Compares by peer identity and network address OR by session ID
261  * (one of the two must match).
262  *
263  * @param peer peer to lookup addresses for
264  * @param addr existing address record
265  * @return existing address record, NULL for none
266  */
267 struct ATS_Address *
268 find_address (const struct GNUNET_PeerIdentity *peer,
269               const struct ATS_Address *addr)
270 {
271   struct CompareAddressContext cac;
272
273   cac.exact_address = NULL;
274   cac.base_address = NULL;
275   cac.search = addr;
276   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
277                                               &compare_address_it, &cac);
278
279 #if 0
280   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281               "exact address: %s           base address: %s\n",
282               (cac.exact_address != NULL) ? "YES" : "NO",
283               (cac.base_address != NULL) ? "YES" : "NO");
284 #endif
285   if (cac.exact_address == NULL)
286     return cac.base_address;
287   return cac.exact_address;
288 }
289
290
291 static struct ATS_Address *
292 lookup_address (const struct GNUNET_PeerIdentity *peer,
293                 const char *plugin_name, const void *plugin_addr,
294                 size_t plugin_addr_len, uint32_t session_id,
295                 const struct GNUNET_ATS_Information *atsi,
296                 uint32_t atsi_count)
297 {
298   struct ATS_Address *aa;
299   struct ATS_Address *old;
300
301   aa = create_address (peer,
302                        plugin_name,
303                        plugin_addr, plugin_addr_len,
304                        session_id);
305
306   aa->mlp_information = NULL;
307   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
308   aa->ats_count = atsi_count;
309   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
310
311   /* Get existing address or address with session == 0 */
312   old = find_address (peer, aa);
313   if (old == NULL)
314   {
315     GNUNET_free (aa);
316     return NULL;
317   }
318   else if (old->session_id != session_id)
319   {
320     GNUNET_free (aa);
321     GNUNET_break (0);
322     return NULL;
323   }
324
325   return old;
326 }
327
328 static int
329 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
330 {
331   struct CompareAddressContext *cac = cls;
332   struct ATS_Address *aa = value;
333
334   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
335   {
336       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
337       {
338         cac->exact_address = aa;
339         return GNUNET_NO;
340       }
341   }
342   return GNUNET_YES;
343 }
344
345
346 /**
347  * Find an existing equivalent address record.
348  * Compares by peer identity and network address AND by session ID
349  * (one of the two must match).
350  *
351  * @param peer peer to lookup addresses for
352  * @param addr existing address record
353  * @return existing address record, NULL for none
354  */
355 struct ATS_Address *
356 find_exact_address (const struct GNUNET_PeerIdentity *peer,
357               const struct ATS_Address *addr)
358 {
359   struct CompareAddressContext cac;
360
361   cac.exact_address = NULL;
362   cac.search = addr;
363   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
364                                               &compare_address_session_it, &cac);
365   return cac.exact_address;
366 }
367
368
369 void
370 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
371                       const char *plugin_name, const void *plugin_addr,
372                       size_t plugin_addr_len, uint32_t session_id,
373                       const struct GNUNET_ATS_Information *atsi,
374                       uint32_t atsi_count)
375 {
376   struct ATS_Address *aa;
377   struct ATS_Address *old;
378
379   if (GNUNET_NO == running)
380     return;
381
382   GNUNET_assert (NULL != addresses);
383
384   aa = create_address (peer,
385                        plugin_name,
386                        plugin_addr, plugin_addr_len,
387                        session_id);
388
389   aa->mlp_information = NULL;
390   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
391   aa->ats_count = atsi_count;
392   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
393
394   /* Get existing address or address with session == 0 */
395   old = find_address (peer, aa);
396   if (old == NULL)
397   {
398     /* We have a new address */
399     GNUNET_assert (GNUNET_OK ==
400                    GNUNET_CONTAINER_multihashmap_put (addresses,
401                                                       &peer->hashPubKey, aa,
402                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
403     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
404                 GNUNET_i2s (peer), session_id, aa);
405     return;
406   }
407
408   if (old->session_id == 0)
409   {
410     /* We have a base address with out an session, update this address */
411     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412               "Updated existing address for peer `%s' %p with new session %u\n",
413               GNUNET_i2s (peer), old, session_id);
414     GNUNET_free_non_null (old->ats);
415     old->session_id = session_id;
416     old->ats = NULL;
417     old->ats_count = 0;
418     old->ats = aa->ats;
419     old->ats_count = aa->ats_count;
420     GNUNET_free (aa->plugin);
421     GNUNET_free (aa);
422     return;
423   }
424
425   /* This address and session is already existing */
426   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
427             "Added already existing address for peer `%s' `%s' %p with new session %u\n",
428             GNUNET_i2s (peer), plugin_name, session_id);
429   GNUNET_break (0);
430 }
431
432
433 void
434 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
435                       const char *plugin_name, const void *plugin_addr,
436                       size_t plugin_addr_len, uint32_t session_id,
437                       const struct GNUNET_ATS_Information *atsi,
438                       uint32_t atsi_count)
439 {
440   struct ATS_Address *old;
441   uint32_t i;
442
443   if (GNUNET_NO == running)
444     return;
445
446   GNUNET_assert (NULL != addresses);
447
448   /* Get existing address */
449   old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
450                        session_id, atsi, atsi_count);
451   if (old == NULL)
452   {
453     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
454                 GNUNET_i2s (peer), plugin_name, session_id);
455     GNUNET_break (0);
456     return;
457   }
458
459   for (i = 0; i < atsi_count; i++)
460     switch (ntohl (atsi[i].type))
461     {
462     case GNUNET_ATS_UTILIZATION_UP:
463       old->atsp_utilization_out.value__ = atsi[i].value;
464       break;
465     case GNUNET_ATS_UTILIZATION_DOWN:
466       old->atsp_utilization_in.value__ = atsi[i].value;
467       break;
468     case GNUNET_ATS_QUALITY_NET_DELAY:
469       old->atsp_latency.rel_value = ntohl (atsi[i].value);
470       break;
471     case GNUNET_ATS_QUALITY_NET_DISTANCE:
472       old->atsp_distance = ntohl (atsi[i].value);
473       break;
474     case GNUNET_ATS_COST_WAN:
475       old->atsp_cost_wan = ntohl (atsi[i].value);
476       break;
477     case GNUNET_ATS_COST_LAN:
478       old->atsp_cost_lan = ntohl (atsi[i].value);
479       break;
480     case GNUNET_ATS_COST_WLAN:
481       old->atsp_cost_wlan = ntohl (atsi[i].value);
482       break;
483     case GNUNET_ATS_NETWORK_TYPE:
484       old->atsp_network_type = ntohl (atsi[i].value);
485       break;
486
487     default:
488       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
489                   "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
490       GNUNET_break (0);
491       break;
492     }
493 #if HAVE_LIBGLPK
494   if (ats_mode == MLP)
495     GAS_mlp_address_update (mlp, addresses, old);
496 #endif
497 }
498
499
500 /**
501  * Delete an address
502  *
503  * If session != 0, just the session is deleted, the address itself still exists
504  * If session == 0, remove full address
505  * If session == 0 and addrlen == 0, destroy inbound address
506  *
507  * @param cls unused
508  * @param key unused
509  * @param value the 'struct ATS_Address'
510  * @return GNUNET_OK (continue to iterate)
511  */
512 static int
513 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
514 {
515   const struct ATS_Address *info = cls;
516   struct ATS_Address *aa = value;
517
518   GNUNET_assert (0 ==
519                  memcmp (&aa->peer, &info->peer,
520                          sizeof (struct GNUNET_PeerIdentity)));
521   /* session == 0, remove full address  */
522   if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
523       (aa->addr_len == info->addr_len) &&
524       (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
525   {
526
527     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528                 "Deleting address for peer `%s': `%s' %u\n",
529                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
530
531     if (GNUNET_YES == destroy_address (aa))
532       recalculate_assigned_bw ();
533     return GNUNET_OK;
534   }
535   /* session != 0, just remove session */
536   if (aa->session_id != info->session_id)
537     return GNUNET_OK;           /* irrelevant */
538   if (aa->session_id != 0)
539     GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
540   /* session died */
541 #if VERBOSE
542   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543               "Deleting session for peer `%s': `%s' %u\n",
544               GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
545 #endif
546   aa->session_id = 0;
547
548   if (GNUNET_YES == aa->active)
549   {
550     aa->active = GNUNET_NO;
551     active_addr_count--;
552     recalculate_assigned_bw ();
553   }
554
555   /* session == 0 and addrlen == 0 : destroy address */
556   if (aa->addr_len == 0)
557   {
558 #if VERBOSE
559     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560                 "Deleting session and address for peer `%s': `%s' %u\n",
561                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
562 #endif
563     (void) destroy_address (aa);
564   }
565   else
566   {
567     /* session was set to 0, update address */
568 #if HAVE_LIBGLPK
569   if (ats_mode == MLP)
570     GAS_mlp_address_update (mlp, addresses, aa);
571 #endif
572   }
573
574   return GNUNET_OK;
575 }
576
577 void
578 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
579                        const char *plugin_name, const void *plugin_addr,
580                        size_t plugin_addr_len, uint32_t session_id)
581 {
582   struct ATS_Address *aa;
583   struct ATS_Address *old;
584
585   if (GNUNET_NO == running)
586     return;
587
588   /* Get existing address */
589   old = lookup_address(peer, plugin_name, plugin_addr, plugin_addr_len,
590                        session_id, NULL, 0);
591   if (old == NULL)
592   {
593     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
594                 GNUNET_i2s (peer), plugin_name, session_id);
595     GNUNET_break (0);
596     return;
597   }
598
599
600   GNUNET_break (0 < strlen (plugin_name));
601   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
602
603   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
604                                               &destroy_by_session_id, aa);
605
606   free_address (aa);
607 }
608
609
610 /**
611  * Find a "good" address to use for a peer.  If we already have an existing
612  * address, we stick to it.  Otherwise, we pick by lowest distance and then
613  * by lowest latency.
614  *
615  * @param cls the 'struct ATS_Address**' where we store the result
616  * @param key unused
617  * @param value another 'struct ATS_Address*' to consider using
618  * @return GNUNET_OK (continue to iterate)
619  */
620 static int
621 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
622 {
623   struct ATS_Address **ap = cls;
624   struct ATS_Address *aa = (struct ATS_Address *) value;
625   struct ATS_Address *ab = *ap;
626   struct GNUNET_TIME_Absolute now;
627
628   now = GNUNET_TIME_absolute_get();
629
630   if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
631   {
632     /* This address is blocked for suggestion */
633     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634                 "Address %p blocked for suggestion for %llu ms \n",
635                 aa,
636                 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
637     return GNUNET_OK;
638   }
639
640   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
641   aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
642
643   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644               "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
645
646   /* FIXME this is a hack */
647
648
649   if (NULL != ab)
650   {
651     if ((0 == strcmp (ab->plugin, "tcp")) &&
652         (0 == strcmp (aa->plugin, "tcp")))
653     {
654       if ((0 != ab->addr_len) &&
655           (0 == aa->addr_len))
656       {
657         /* saved address was an outbound address, but we have an inbound address */
658         *ap = aa;
659         return GNUNET_OK;
660       }
661       if (0 == ab->addr_len)
662       {
663         /* saved address was an inbound address, so do not overwrite */
664         return GNUNET_OK;
665       }
666     }
667   }
668   /* FIXME end of hack */
669
670   if (NULL == ab)
671   {
672     *ap = aa;
673     return GNUNET_OK;
674   }
675   if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
676       (ntohl (aa->assigned_bw_in.value__) > 0))
677   {
678     /* stick to existing connection */
679     *ap = aa;
680     return GNUNET_OK;
681   }
682   if (ab->atsp_distance > aa->atsp_distance)
683   {
684     /* user shorter distance */
685     *ap = aa;
686     return GNUNET_OK;
687   }
688   if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
689   {
690     /* user lower latency */
691     *ap = aa;
692     return GNUNET_OK;
693   }
694   /* don't care */
695   return GNUNET_OK;
696 }
697
698
699 int
700 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
701                       const char *plugin_name, const void *plugin_addr,
702                       size_t plugin_addr_len, uint32_t session_id, int in_use)
703 {
704 #if DEBUG_ATS
705   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706               "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
707               GNUNET_i2s (peer), in_use);
708 #endif
709
710   struct ATS_Address *old;
711
712   if (GNUNET_NO == running)
713     return GNUNET_SYSERR;
714
715   old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
716   if (NULL == old)
717   {
718     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
719                 "Trying to set unknown address `%s', %s %u %s \n",
720                 GNUNET_i2s (peer),
721                 plugin_name, session_id,
722                 (GNUNET_NO == in_use) ? "NO" : "YES");
723     GNUNET_break (0);
724     return GNUNET_SYSERR;
725   }
726   if (old->used == in_use)
727   {
728     GNUNET_break (0);
729     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
730                 "Address in use called multiple times for peer `%s': %s -> %s \n",
731                 GNUNET_i2s (peer),
732                 (GNUNET_NO == old->used) ? "NO" : "YES",
733                 (GNUNET_NO == in_use) ? "NO" : "YES");
734     return GNUNET_SYSERR;
735   }
736   old->used = in_use;
737 #if HAVE_LIBGLPK
738   if (ats_mode == MLP)
739      GAS_mlp_address_update (mlp, addresses, old);
740 #endif
741   return GNUNET_OK;
742 }
743
744
745 void request_address_mlp (const struct GNUNET_PeerIdentity *peer)
746 {
747   struct ATS_Address *aa;
748   aa = NULL;
749
750 #if HAVE_GLPK
751   /* Get preferred address from MLP */
752   struct ATS_PreferedAddress * paddr = NULL;
753   paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
754   aa = paddr->address;
755   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
756   /* FIXME use bw in value */
757   paddr->bandwidth_in = paddr->bandwidth_out;
758   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
759   GNUNET_free (paddr);
760 #endif
761
762   if (aa == NULL)
763   {
764     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
765                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
766     return;
767   }
768   if (aa->active == GNUNET_NO)
769   {
770     aa->active = GNUNET_YES;
771     active_addr_count++;
772
773     send_bw_notification (aa);
774   }
775   else
776   {
777     /* just to be sure... */
778     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
779                                                 aa->addr_len, aa->session_id,
780                                                 aa->ats, aa->ats_count,
781                                                 aa->assigned_bw_out,
782                                                 aa->assigned_bw_in);
783   }
784
785 }
786
787 void request_address_simple (const struct GNUNET_PeerIdentity *peer)
788 {
789   struct ATS_Address *aa;
790   aa = NULL;
791
792   /* Get address with: stick to current address, lower distance, lower latency */
793   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
794                                               &find_address_it, &aa);
795   if (aa == NULL)
796   {
797     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
798                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
799     return;
800   }
801
802   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
803               "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
804
805   if (aa->active == GNUNET_NO)
806   {
807     aa->active = GNUNET_YES;
808     active_addr_count++;
809     if (ats_mode == SIMPLE)
810     {
811       recalculate_assigned_bw ();
812     }
813   }
814   else
815   {
816     /* just to be sure... */
817     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
818                                                 aa->addr_len, aa->session_id,
819                                                 aa->ats, aa->ats_count,
820                                                 aa->assigned_bw_out,
821                                                 aa->assigned_bw_in);
822   }
823 }
824
825
826 void
827 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
828 {
829   if (GNUNET_NO == running)
830     return;
831
832   if (ats_mode == SIMPLE)
833   {
834     request_address_simple (peer);
835   }
836   if (ats_mode == MLP)
837   {
838     request_address_mlp(peer);
839   }
840 }
841
842
843 static int
844 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
845 {
846   struct ATS_Address *aa = value;
847
848   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849               "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
850
851   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
852   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
853   return GNUNET_OK;
854 }
855
856 void
857 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
858 {
859   GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
860                                               &peer->hashPubKey,
861                                               &reset_address_it,
862                                               NULL));
863 }
864
865
866
867 // FIXME: this function should likely end up in the LP-subsystem and
868 // not with 'addresses' in the future...
869 void
870 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
871                                  enum GNUNET_ATS_PreferenceKind kind,
872                                  float score)
873 {
874   if (GNUNET_NO == running)
875     return;
876 #if HAVE_LIBGLPK
877   if (ats_mode == MLP)
878     GAS_mlp_address_change_preference (mlp, peer, kind, score);
879 #endif
880 }
881
882
883
884 /**
885  * Initialize address subsystem.
886  *
887  * @param cfg configuration to use
888  * @param stats the statistics handle to use
889  */
890 void
891 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
892                     const struct GNUNET_STATISTICS_Handle *stats)
893 {
894   int mode;
895
896   char *quota_wan_in_str;
897   char *quota_wan_out_str;
898
899   running = GNUNET_NO;
900
901   addresses = GNUNET_CONTAINER_multihashmap_create (128);
902   GNUNET_assert (NULL != addresses);
903
904   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_wan_in_str))
905   {
906     if (0 == strcmp(quota_wan_in_str, "unlimited") ||
907         (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
908       wan_quota_in = (UINT32_MAX) /10;
909
910     GNUNET_free (quota_wan_in_str);
911     quota_wan_in_str = NULL;
912   }
913   else
914   {
915     wan_quota_in = (UINT32_MAX) /10;
916   }
917
918   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_wan_out_str))
919   {
920     if (0 == strcmp(quota_wan_out_str, "unlimited") ||
921         (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
922       wan_quota_out = (UINT32_MAX) /10;
923
924     GNUNET_free (quota_wan_out_str);
925     quota_wan_out_str = NULL;
926   }
927   else
928   {
929     wan_quota_out = (UINT32_MAX) /10;
930   }
931
932   mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
933   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
934   switch (mode)
935   {
936     /* MLP = YES */
937     case GNUNET_YES:
938 #if HAVE_LIBGLPK
939       ats_mode = MLP;
940       /* Init the MLP solver with default values */
941       mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
942       if (NULL == mlp)
943       {
944         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
945         GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
946         break;
947       }
948       else
949       {
950         GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
951         break;
952       }
953 #else
954       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
955       GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
956       ats_mode = SIMPLE;
957       break;
958 #endif
959     /* MLP = NO */
960     case GNUNET_NO:
961       GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
962       ats_mode = SIMPLE;
963       break;
964     /* No configuration value */
965     case GNUNET_SYSERR:
966       GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
967       ats_mode = SIMPLE;
968       break;
969     default:
970       break;
971   }
972   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
973   running = GNUNET_YES;
974 }
975
976
977 /**
978  * Free memory of address.
979  *
980  * @param cls NULL
981  * @param key peer identity (unused)
982  * @param value the 'struct ATS_Address' to free
983  * @return GNUNET_OK (continue to iterate)
984  */
985 static int
986 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
987 {
988   struct ATS_Address *aa = value;
989
990   destroy_address (aa);
991   return GNUNET_OK;
992 }
993
994
995 void
996 GAS_addresses_destroy_all ()
997 {
998   if (GNUNET_NO == running)
999     return;
1000
1001   if (addresses != NULL)
1002     GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
1003   GNUNET_assert (active_addr_count == 0);
1004 }
1005
1006
1007 /**
1008  * Shutdown address subsystem.
1009  */
1010 void
1011 GAS_addresses_done ()
1012 {
1013   GAS_addresses_destroy_all ();
1014   running = GNUNET_NO;
1015   GNUNET_CONTAINER_multihashmap_destroy (addresses);
1016   addresses = NULL;
1017 #if HAVE_LIBGLPK
1018   if (ats_mode == MLP)
1019   {
1020     GAS_mlp_done (mlp);
1021   }
1022 #endif
1023
1024 }
1025
1026
1027 /* end of gnunet-service-ats_addresses.c */