1a511eb3d85b9dbe3eb6a36157271324676d4358
[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 #include "gnunet-service-ats_addresses_simplistic.h"
38
39 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
40
41
42 /**
43  * Available ressource assignment modes
44  */
45 enum ATS_Mode
46 {
47   /*
48    * Simplistic mode:
49    *
50    * Assign each peer an equal amount of bandwidth (bw)
51    *
52    * bw_per_peer = bw_total / #active addresses
53    */
54   MODE_SIMPLISTIC,
55
56   /*
57    * MLP mode:
58    *
59    * Solve ressource assignment as an optimization problem
60    * Uses an mixed integer programming solver
61    */
62   MODE_MLP
63 };
64
65 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
66
67 static void *solver;
68
69 static unsigned long long wan_quota_in;
70
71 static unsigned long long wan_quota_out;
72
73 static unsigned int active_addr_count;
74
75 static int ats_mode;
76
77 static int running;
78
79
80 static unsigned int
81 assemble_ats_information (struct ATS_Address *aa,  struct GNUNET_ATS_Information **dest)
82 {
83   unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
84   struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
85   (*dest) = ats;
86
87   ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
88   ats[0].value = aa->atsp_utilization_out.value__;
89   ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
90   ats[1].value = aa->atsp_utilization_in.value__;
91   ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
92   ats[2].value = ntohl(aa->atsp_network_type);
93   ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
94   ats[3].value = ntohl(aa->atsp_latency.rel_value);
95   ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
96   ats[4].value = ntohl(aa->atsp_distance);
97   ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
98   ats[5].value = ntohl (aa->atsp_cost_wan);
99   ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
100   ats[6].value = ntohl (aa->atsp_cost_lan);
101   ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
102   ats[7].value = ntohl (aa->atsp_cost_wlan);
103   return ats_count;
104 }
105
106 static void
107 send_bw_notification (struct ATS_Address *aa)
108 {
109   struct GNUNET_ATS_Information *ats;
110   uint32_t ats_count;
111
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
113               GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
114               ntohl (aa->assigned_bw_out.value__));
115   ats_count = assemble_ats_information (aa, &ats);
116
117   GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
118                                               aa->addr_len, aa->session_id,
119                                               ats, ats_count,
120                                               aa->assigned_bw_out,
121                                               aa->assigned_bw_in);
122   GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
123   GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
124                                   aa->active,
125                                   ats, ats_count, aa->assigned_bw_out,
126                                   aa->assigned_bw_in);
127   GNUNET_free (ats);
128 }
129
130 /**
131  * Update a bandwidth assignment for a peer.  This trivial method currently
132  * simply assigns the same share to all active connections.
133  *
134  * @param cls unused
135  * @param key unused
136  * @param value the 'struct ATS_Address'
137  * @return GNUNET_OK (continue to iterate)
138  */
139 static int
140 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
141 {
142   struct ATS_Address *aa = value;
143
144   if (GNUNET_YES != aa->active)
145     return GNUNET_OK;
146   GNUNET_assert (active_addr_count > 0);
147
148
149   /* Simple method */
150   aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
151   aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
152
153   send_bw_notification (aa);
154
155   return GNUNET_OK;
156 }
157
158
159 /**
160  * Some (significant) input changed, recalculate bandwidth assignment
161  * for all peers.
162  */
163 static void
164 recalculate_assigned_bw ()
165 {
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167               "Recalculating bandwidth for all active connections\n");
168   GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
169                             1, GNUNET_NO);
170   GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
171                          GNUNET_NO);
172
173   GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
174 }
175
176 /**
177  * Free the given address
178  * @param addr address to destroy
179  */
180 static void
181 free_address (struct ATS_Address *addr)
182 {
183   GNUNET_free_non_null (addr->ats);
184   GNUNET_free (addr->plugin);
185   GNUNET_free (addr);
186 }
187
188 /**
189  * Create a ATS_address with the given information
190  * @param peer peer
191  * @param plugin_name plugin
192  * @param plugin_addr address
193  * @param plugin_addr_len address length
194  * @param session_id session
195  * @return the ATS_Address
196  */
197 static struct ATS_Address *
198 create_address (const struct GNUNET_PeerIdentity *peer,
199                 const char *plugin_name,
200                 const void *plugin_addr, size_t plugin_addr_len,
201                 uint32_t session_id)
202 {
203   struct ATS_Address *aa = NULL;
204
205   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
206   aa->peer = *peer;
207   aa->addr_len = plugin_addr_len;
208   aa->addr = &aa[1];
209   memcpy (&aa[1], plugin_addr, plugin_addr_len);
210   aa->plugin = GNUNET_strdup (plugin_name);
211   aa->session_id = session_id;
212   return aa;
213 }
214
215
216 /**
217  * Destroy the given address.
218  *
219  * @param addr address to destroy
220  * @return GNUNET_YES if bandwidth allocations should be recalcualted
221  */
222 static int
223 destroy_address (struct ATS_Address *addr)
224 {
225   int ret;
226
227   ret = GNUNET_NO;
228   GNUNET_assert (GNUNET_YES ==
229                  GNUNET_CONTAINER_multihashmap_remove (addresses,
230                                                        &addr->peer.hashPubKey,
231                                                        addr));
232
233 #if HAVE_LIBGLPK
234   if (ats_mode == MODE_MLP)
235     GAS_mlp_address_delete (solver, addresses, addr);
236 #endif
237
238   if (GNUNET_YES == addr->active)
239   {
240     active_addr_count--;
241     addr->active = GNUNET_NO;
242     ret = GNUNET_YES;
243   }
244   free_address (addr);
245   return ret;
246 }
247
248
249 struct CompareAddressContext
250 {
251   const struct ATS_Address *search;
252
253   /* exact_address != NULL if address and session is equal */
254   struct ATS_Address *exact_address;
255   /* exact_address != NULL if address and session is 0 */
256   struct ATS_Address *base_address;
257 };
258
259
260 static int
261 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
262 {
263   struct CompareAddressContext *cac = cls;
264   struct ATS_Address *aa = value;
265
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
267       GNUNET_h2s (key),
268       aa->addr_len, aa->session_id,
269       cac->search->addr_len, cac->search->session_id);
270
271   /* Find an matching exact address:
272    *
273    * Compare by:
274    * aa->addr_len == cac->search->addr_len
275    * aa->plugin == cac->search->plugin
276    * aa->addr == cac->search->addr
277    * aa->session == cac->search->session
278    *
279    * return as exact address
280    */
281   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
282   {
283       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
284         cac->exact_address = aa;
285   }
286
287   /* Find an matching base address:
288    *
289    * Properties:
290    *
291    * aa->session_id == 0
292    *
293    * Compare by:
294    * aa->addr_len == cac->search->addr_len
295    * aa->plugin == cac->search->plugin
296    * aa->addr == cac->search->addr
297    *
298    * return as base address
299    */
300   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
301   {
302       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
303         cac->base_address = aa;
304   }
305
306   /* Find an matching exact address based on session:
307    *
308    * Properties:
309    *
310    * cac->search->addr_len == 0
311    *
312    * Compare by:
313    * aa->plugin == cac->search->plugin
314    * aa->session_id == cac->search->session_id
315    *
316    * return as exact address
317    */
318   if (0 == cac->search->addr_len)
319   {
320       if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
321         cac->exact_address = aa;
322   }
323
324   if (cac->exact_address == NULL)
325     return GNUNET_YES; /* Continue iteration to find exact address */
326   else
327     return GNUNET_NO; /* Stop iteration since we have an exact address */
328 }
329
330
331 /**
332  * Find an existing equivalent address record.
333  * Compares by peer identity and network address OR by session ID
334  * (one of the two must match).
335  *
336  * @param peer peer to lookup addresses for
337  * @param addr existing address record
338  * @return existing address record, NULL for none
339  */
340 struct ATS_Address *
341 find_address (const struct GNUNET_PeerIdentity *peer,
342               const struct ATS_Address *addr)
343 {
344   struct CompareAddressContext cac;
345
346   cac.exact_address = NULL;
347   cac.base_address = NULL;
348   cac.search = addr;
349   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
350                                               &compare_address_it, &cac);
351
352 #if 0
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354               "Found exact address: %s           base address: %s\n",
355               (cac.exact_address != NULL) ? "YES" : "NO",
356               (cac.base_address != NULL) ? "YES" : "NO");
357 #endif
358   if (cac.exact_address == NULL)
359     return cac.base_address;
360   return cac.exact_address;
361 }
362
363
364 static struct ATS_Address *
365 lookup_address (const struct GNUNET_PeerIdentity *peer,
366                 const char *plugin_name, const void *plugin_addr,
367                 size_t plugin_addr_len, uint32_t session_id,
368                 const struct GNUNET_ATS_Information *atsi,
369                 uint32_t atsi_count)
370 {
371   struct ATS_Address *aa;
372   struct ATS_Address *old;
373
374   aa = create_address (peer,
375                        plugin_name,
376                        plugin_addr, plugin_addr_len,
377                        session_id);
378
379   aa->mlp_information = NULL;
380   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
381   aa->ats_count = atsi_count;
382   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
383
384   /* Get existing address or address with session == 0 */
385   old = find_address (peer, aa);
386   free_address (aa);
387   if (old == NULL)
388   {
389     return NULL;
390   }
391   else if (old->session_id != session_id)
392   {
393     return NULL;
394   }
395   return old;
396 }
397
398
399 #if DEADCODE
400 static int
401 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
402 {
403   struct CompareAddressContext *cac = cls;
404   struct ATS_Address *aa = value;
405
406   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
407   {
408       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
409       {
410         cac->exact_address = aa;
411         return GNUNET_NO;
412       }
413   }
414   return GNUNET_YES;
415 }
416
417
418 /**
419  * Find an existing equivalent address record.
420  * Compares by peer identity and network address AND by session ID
421  * (one of the two must match).
422  *
423  * @param peer peer to lookup addresses for
424  * @param addr existing address record
425  * @return existing address record, NULL for none
426  */
427 static struct ATS_Address *
428 find_exact_address (const struct GNUNET_PeerIdentity *peer,
429               const struct ATS_Address *addr)
430 {
431   struct CompareAddressContext cac;
432
433   cac.exact_address = NULL;
434   cac.search = addr;
435   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
436                                               &compare_address_session_it, &cac);
437   return cac.exact_address;
438 }
439 #endif
440
441
442 void
443 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
444                       const char *plugin_name, const void *plugin_addr,
445                       size_t plugin_addr_len, uint32_t session_id,
446                       const struct GNUNET_ATS_Information *atsi,
447                       uint32_t atsi_count)
448 {
449   struct ATS_Address *aa;
450   struct ATS_Address *old;
451
452   if (GNUNET_NO == running)
453     return;
454
455   GNUNET_assert (NULL != addresses);
456
457   aa = create_address (peer,
458                        plugin_name,
459                        plugin_addr, plugin_addr_len,
460                        session_id);
461
462   aa->mlp_information = NULL;
463   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
464   aa->ats_count = atsi_count;
465   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
466
467   /* Get existing address or address with session == 0 */
468   old = find_address (peer, aa);
469   if (old == NULL)
470   {
471     /* We have a new address */
472     GNUNET_assert (GNUNET_OK ==
473                    GNUNET_CONTAINER_multihashmap_put (addresses,
474                                                       &peer->hashPubKey, aa,
475                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
476     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
477                 GNUNET_i2s (peer), session_id, aa);
478     return;
479   }
480
481   if (old->session_id == 0)
482   {
483     /* We have a base address with out an session, update this address */
484     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485               "Updated existing address for peer `%s' %p with new session %u\n",
486               GNUNET_i2s (peer), old, session_id);
487     GNUNET_free_non_null (old->ats);
488     old->session_id = session_id;
489     old->ats = NULL;
490     old->ats_count = 0;
491     old->ats = aa->ats;
492     old->ats_count = aa->ats_count;
493     GNUNET_free (aa->plugin);
494     GNUNET_free (aa);
495     return;
496   }
497
498   /* This address and session is already existing */
499   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500             "Added already existing address for peer `%s' `%s' %p with new session %u\n",
501             GNUNET_i2s (peer), plugin_name, session_id);
502   GNUNET_break (0);
503 }
504
505
506 void
507 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
508                       const char *plugin_name, const void *plugin_addr,
509                       size_t plugin_addr_len, uint32_t session_id,
510                       const struct GNUNET_ATS_Information *atsi,
511                       uint32_t atsi_count)
512 {
513   struct ATS_Address *old;
514   uint32_t i;
515
516   if (GNUNET_NO == running)
517     return;
518
519   GNUNET_assert (NULL != addresses);
520
521   /* Get existing address */
522   old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
523                        session_id, atsi, atsi_count);
524   if (old == NULL)
525   {
526     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
527                 GNUNET_i2s (peer), plugin_name, session_id);
528     GNUNET_break (0);
529     return;
530   }
531
532   for (i = 0; i < atsi_count; i++)
533     switch (ntohl (atsi[i].type))
534     {
535     case GNUNET_ATS_UTILIZATION_UP:
536       old->atsp_utilization_out.value__ = atsi[i].value;
537       break;
538     case GNUNET_ATS_UTILIZATION_DOWN:
539       old->atsp_utilization_in.value__ = atsi[i].value;
540       break;
541     case GNUNET_ATS_QUALITY_NET_DELAY:
542       old->atsp_latency.rel_value = ntohl (atsi[i].value);
543       break;
544     case GNUNET_ATS_QUALITY_NET_DISTANCE:
545       old->atsp_distance = ntohl (atsi[i].value);
546       break;
547     case GNUNET_ATS_COST_WAN:
548       old->atsp_cost_wan = ntohl (atsi[i].value);
549       break;
550     case GNUNET_ATS_COST_LAN:
551       old->atsp_cost_lan = ntohl (atsi[i].value);
552       break;
553     case GNUNET_ATS_COST_WLAN:
554       old->atsp_cost_wlan = ntohl (atsi[i].value);
555       break;
556     case GNUNET_ATS_NETWORK_TYPE:
557       old->atsp_network_type = ntohl (atsi[i].value);
558       break;
559
560     default:
561       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
562                   "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
563       GNUNET_break (0);
564       break;
565     }
566
567   /* Tell solver about update */
568   switch (ats_mode)
569   {
570     case MODE_MLP:
571       GAS_mlp_address_update (solver, addresses, old);
572       break;
573     case MODE_SIMPLISTIC:
574       GAS_simplistic_address_update (solver, addresses, old);
575       break;
576     default:
577       GNUNET_break (0);
578       break;
579   }
580 }
581
582
583 /**
584  * Delete an address
585  *
586  * If session != 0, just the session is deleted, the address itself still exists
587  * If session == 0, remove full address
588  * If session == 0 and addrlen == 0, destroy inbound address
589  *
590  * @param cls unused
591  * @param key unused
592  * @param value the 'struct ATS_Address'
593  * @return GNUNET_OK (continue to iterate)
594  */
595 static int
596 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
597 {
598   const struct ATS_Address *info = cls;
599   struct ATS_Address *aa = value;
600
601   GNUNET_assert (0 ==
602                  memcmp (&aa->peer, &info->peer,
603                          sizeof (struct GNUNET_PeerIdentity)));
604   /* session == 0, remove full address  */
605   if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
606       (aa->addr_len == info->addr_len) &&
607       (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
608   {
609
610     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611                 "Deleting address for peer `%s': `%s' %u\n",
612                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
613
614     if (GNUNET_YES == destroy_address (aa))
615       recalculate_assigned_bw ();
616     return GNUNET_OK;
617   }
618   /* session != 0, just remove session */
619   if (aa->session_id != info->session_id)
620     return GNUNET_OK;           /* irrelevant */
621   if (aa->session_id != 0)
622     GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
623   /* session died */
624   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625               "Deleting session for peer `%s': `%s' %u\n",
626               GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
627   aa->session_id = 0;
628
629   if (GNUNET_YES == aa->active)
630   {
631     aa->active = GNUNET_NO;
632     active_addr_count--;
633     recalculate_assigned_bw ();
634   }
635
636   /* session == 0 and addrlen == 0 : destroy address */
637   if (aa->addr_len == 0)
638   {
639     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640                 "Deleting session and address for peer `%s': `%s' %u\n",
641                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
642     (void) destroy_address (aa);
643   }
644   else
645   {
646     /* session was set to 0, update address */
647 #if HAVE_LIBGLPK
648   if (ats_mode == MODE_MLP)
649     GAS_mlp_address_update (solver, addresses, aa);
650 #endif
651   }
652
653   return GNUNET_OK;
654 }
655
656
657 void
658 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
659                        const char *plugin_name, const void *plugin_addr,
660                        size_t plugin_addr_len, uint32_t session_id)
661 {
662   struct ATS_Address *aa;
663   struct ATS_Address *old;
664
665   if (GNUNET_NO == running)
666     return;
667
668   /* Get existing address */
669   old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
670                        session_id, NULL, 0);
671   if (old == NULL)
672   {
673     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
674                 GNUNET_i2s (peer), plugin_name, session_id);
675     return;
676   }
677
678
679   GNUNET_break (0 < strlen (plugin_name));
680   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
681
682   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
683                                               &destroy_by_session_id, aa);
684
685   free_address (aa);
686 }
687
688
689 /**
690  * Find a "good" address to use for a peer.  If we already have an existing
691  * address, we stick to it.  Otherwise, we pick by lowest distance and then
692  * by lowest latency.
693  *
694  * @param cls the 'struct ATS_Address**' where we store the result
695  * @param key unused
696  * @param value another 'struct ATS_Address*' to consider using
697  * @return GNUNET_OK (continue to iterate)
698  */
699 static int
700 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
701 {
702   struct ATS_Address **ap = cls;
703   struct ATS_Address *aa = (struct ATS_Address *) value;
704   struct ATS_Address *ab = *ap;
705   struct GNUNET_TIME_Absolute now;
706
707   now = GNUNET_TIME_absolute_get();
708
709   if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
710   {
711     /* This address is blocked for suggestion */
712     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
713                 "Address %p blocked for suggestion for %llu ms \n",
714                 aa,
715                 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
716     return GNUNET_OK;
717   }
718
719   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
720   aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
721
722   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723               "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
724
725   /* FIXME this is a hack */
726
727
728   if (NULL != ab)
729   {
730     if ((0 == strcmp (ab->plugin, "tcp")) &&
731         (0 == strcmp (aa->plugin, "tcp")))
732     {
733       if ((0 != ab->addr_len) &&
734           (0 == aa->addr_len))
735       {
736         /* saved address was an outbound address, but we have an inbound address */
737         *ap = aa;
738         return GNUNET_OK;
739       }
740       if (0 == ab->addr_len)
741       {
742         /* saved address was an inbound address, so do not overwrite */
743         return GNUNET_OK;
744       }
745     }
746   }
747   /* FIXME end of hack */
748
749   if (NULL == ab)
750   {
751     *ap = aa;
752     return GNUNET_OK;
753   }
754   if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
755       (ntohl (aa->assigned_bw_in.value__) > 0))
756   {
757     /* stick to existing connection */
758     *ap = aa;
759     return GNUNET_OK;
760   }
761   if (ab->atsp_distance > aa->atsp_distance)
762   {
763     /* user shorter distance */
764     *ap = aa;
765     return GNUNET_OK;
766   }
767   if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
768   {
769     /* user lower latency */
770     *ap = aa;
771     return GNUNET_OK;
772   }
773   /* don't care */
774   return GNUNET_OK;
775 }
776
777
778 int
779 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
780                       const char *plugin_name, const void *plugin_addr,
781                       size_t plugin_addr_len, uint32_t session_id, int in_use)
782 {
783 #if DEBUG_ATS
784   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785               "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
786               GNUNET_i2s (peer), in_use);
787 #endif
788
789   struct ATS_Address *old;
790
791   if (GNUNET_NO == running)
792     return GNUNET_SYSERR;
793
794   old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
795   if (NULL == old)
796   {
797     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
798                 "Trying to set unknown address `%s', %s %u %s \n",
799                 GNUNET_i2s (peer),
800                 plugin_name, session_id,
801                 (GNUNET_NO == in_use) ? "NO" : "YES");
802     GNUNET_break (0);
803     return GNUNET_SYSERR;
804   }
805   if (old->used == in_use)
806   {
807     GNUNET_break (0);
808     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
809                 "Address in use called multiple times for peer `%s': %s -> %s \n",
810                 GNUNET_i2s (peer),
811                 (GNUNET_NO == old->used) ? "NO" : "YES",
812                 (GNUNET_NO == in_use) ? "NO" : "YES");
813     return GNUNET_SYSERR;
814   }
815   old->used = in_use;
816
817   /* Tell solver about update */
818   switch (ats_mode)
819   {
820     case MODE_MLP:
821       GAS_mlp_address_update (solver, addresses, old);
822       break;
823     case MODE_SIMPLISTIC:
824       GAS_simplistic_address_update (solver, addresses, old);
825       break;
826     default:
827       GNUNET_break (0);
828       break;
829   }
830   return GNUNET_OK;
831 }
832
833
834 static void 
835 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
836 {
837   struct ATS_Address *aa;
838   aa = NULL;
839
840 #if HAVE_GLPK
841   /* Get preferred address from MODE_MLP */
842   struct ATS_PreferedAddress * paddr = NULL;
843   paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
844   aa = paddr->address;
845   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
846   /* FIXME use bw in value */
847   paddr->bandwidth_in = paddr->bandwidth_out;
848   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
849   GNUNET_free (paddr);
850 #endif
851
852   if (aa == NULL)
853   {
854     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
855                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
856     return;
857   }
858   if (aa->active == GNUNET_NO)
859   {
860     aa->active = GNUNET_YES;
861     active_addr_count++;
862
863     send_bw_notification (aa);
864   }
865   else
866   {
867     /* just to be sure... */
868     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
869                                                 aa->addr_len, aa->session_id,
870                                                 aa->ats, aa->ats_count,
871                                                 aa->assigned_bw_out,
872                                                 aa->assigned_bw_in);
873   }
874
875 }
876
877
878 static void 
879 request_address_simple (const struct GNUNET_PeerIdentity *peer)
880 {
881   struct ATS_Address *aa;
882   aa = NULL;
883
884   /* Get address with: stick to current address, lower distance, lower latency */
885   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
886                                               &find_address_it, &aa);
887   if (aa == NULL)
888   {
889     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
890                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
891     return;
892   }
893
894   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
895               "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
896
897   if (aa->active == GNUNET_NO)
898   {
899     aa->active = GNUNET_YES;
900     active_addr_count++;
901     if (ats_mode == MODE_SIMPLISTIC)
902     {
903       recalculate_assigned_bw ();
904     }
905   }
906   else
907   {
908     /* just to be sure... */
909     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
910                                                 aa->addr_len, aa->session_id,
911                                                 aa->ats, aa->ats_count,
912                                                 aa->assigned_bw_out,
913                                                 aa->assigned_bw_in);
914   }
915 }
916
917
918 void
919 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
920 {
921   if (GNUNET_NO == running)
922     return;
923
924   if (ats_mode == MODE_SIMPLISTIC)
925   {
926     request_address_simple (peer);
927   }
928   if (ats_mode == MODE_MLP)
929   {
930     request_address_mlp(peer);
931   }
932 }
933
934
935 static int
936 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
937 {
938   struct ATS_Address *aa = value;
939
940   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941               "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
942
943   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
944   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
945   return GNUNET_OK;
946 }
947
948
949 void
950 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
951 {
952   GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
953                                               &peer->hashPubKey,
954                                               &reset_address_it,
955                                               NULL));
956 }
957
958
959
960 // FIXME: this function should likely end up in the LP-subsystem and
961 // not with 'addresses' in the future...
962 void
963 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
964                                  enum GNUNET_ATS_PreferenceKind kind,
965                                  float score)
966 {
967   if (GNUNET_NO == running)
968     return;
969
970
971   /* Tell solver about update */
972   switch (ats_mode)
973   {
974     case MODE_MLP:
975       GAS_mlp_address_change_preference (solver, peer, kind, score);
976       break;
977     case MODE_SIMPLISTIC:
978       GAS_simplistic_address_change_preference (solver, peer, kind, score);
979       break;
980     default:
981       GNUNET_break (0);
982       break;
983   }
984 }
985
986
987
988 /**
989  * Initialize address subsystem.
990  *
991  * @param cfg configuration to use
992  * @param stats the statistics handle to use
993  */
994 void
995 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
996                     const struct GNUNET_STATISTICS_Handle *stats)
997 {
998   int c;
999   char *quota_wan_in_str;
1000   char *quota_wan_out_str;
1001   char *mode_str;
1002   running = GNUNET_NO;
1003
1004   /* Initialize the system with configuration values */
1005   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_wan_in_str))
1006   {
1007     if (0 == strcmp(quota_wan_in_str, "unlimited") ||
1008         (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
1009       wan_quota_in = (UINT32_MAX) /10;
1010
1011     GNUNET_free (quota_wan_in_str);
1012     quota_wan_in_str = NULL;
1013   }
1014   else
1015   {
1016     wan_quota_in = (UINT32_MAX) /10;
1017   }
1018
1019   if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_wan_out_str))
1020   {
1021     if (0 == strcmp(quota_wan_out_str, "unlimited") ||
1022         (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
1023       wan_quota_out = (UINT32_MAX) /10;
1024
1025     GNUNET_free (quota_wan_out_str);
1026     quota_wan_out_str = NULL;
1027   }
1028   else
1029   {
1030     wan_quota_out = (UINT32_MAX) /10;
1031   }
1032
1033   /* Initialize the addresses database */
1034   addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1035   GNUNET_assert (NULL != addresses);
1036
1037   /* Figure out configured solution method */
1038   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1039   {
1040       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1041       ats_mode = MODE_SIMPLISTIC;
1042   }
1043   else
1044   {
1045       for (c = 0; c < strlen (mode_str); c++)
1046         mode_str[c] = toupper (mode_str[c]);
1047       if (0 == strcmp (mode_str, "SIMPLISTIC"))
1048       {
1049           ats_mode = MODE_SIMPLISTIC;
1050       }
1051       else if (0 == strcmp (mode_str, "MLP"))
1052       {
1053           ats_mode = MODE_MLP;
1054 #if !HAVE_LIBGLPK
1055           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1056           ats_mode = MODE_SIMPLISTIC;
1057 #endif
1058       }
1059       else
1060       {
1061           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1062           ats_mode = MODE_SIMPLISTIC;
1063       }
1064   }
1065
1066   /* Start configured solution method */
1067   switch (ats_mode)
1068   {
1069     case MODE_MLP:
1070       /* Init the MLP solver with default values */
1071       solver = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
1072       if (NULL != solver)
1073       {
1074           ats_mode = MODE_MLP;
1075           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "MLP");
1076           break;
1077       }
1078       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1079     case MODE_SIMPLISTIC:
1080       /* Init the simplistic solver with default values */
1081       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1082       solver = GAS_simplistic_init (cfg, stats);
1083       if (NULL != solver)
1084       {
1085           ats_mode = MODE_SIMPLISTIC;
1086           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1087           break;
1088       }
1089       else
1090       {
1091         GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize simplistic solver!\n");
1092         return;
1093       }
1094       break;
1095     default:
1096       GNUNET_break (0);
1097       break;
1098   }
1099   /* up and running */
1100   running = GNUNET_YES;
1101 }
1102
1103
1104 /**
1105  * Free memory of address.
1106  *
1107  * @param cls NULL
1108  * @param key peer identity (unused)
1109  * @param value the 'struct ATS_Address' to free
1110  * @return GNUNET_OK (continue to iterate)
1111  */
1112 static int
1113 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1114 {
1115   struct ATS_Address *aa = value;
1116
1117   destroy_address (aa);
1118   return GNUNET_OK;
1119 }
1120
1121
1122 void
1123 GAS_addresses_destroy_all ()
1124 {
1125   if (GNUNET_NO == running)
1126     return;
1127
1128   if (addresses != NULL)
1129     GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
1130   GNUNET_assert (active_addr_count == 0);
1131 }
1132
1133
1134 /**
1135  * Shutdown address subsystem.
1136  */
1137 void
1138 GAS_addresses_done ()
1139 {
1140   GAS_addresses_destroy_all ();
1141   running = GNUNET_NO;
1142   GNUNET_CONTAINER_multihashmap_destroy (addresses);
1143   addresses = NULL;
1144
1145   /* Stop configured solution method */
1146   switch (ats_mode)
1147   {
1148     case MODE_MLP:
1149       /* Init the MLP solver with default values */
1150       GAS_mlp_done (solver);
1151       break;
1152     case MODE_SIMPLISTIC:
1153       /* Init the simplistic solver with default values */
1154       GAS_simplistic_done (solver);
1155       break;
1156     default:
1157       break;
1158   }
1159 }
1160
1161 struct PeerIteratorContext
1162 {
1163   GNUNET_ATS_Peer_Iterator it;
1164   void *it_cls;
1165   struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1166 };
1167
1168 static int
1169 peer_it (void *cls,
1170          const struct GNUNET_HashCode * key,
1171          void *value)
1172 {
1173   struct PeerIteratorContext *ip_ctx = cls;
1174   struct GNUNET_PeerIdentity tmp;
1175
1176   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1177   {
1178       GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1179       tmp.hashPubKey = (*key);
1180       ip_ctx->it (ip_ctx->it_cls, &tmp);
1181   }
1182
1183   return GNUNET_OK;
1184 }
1185
1186 /**
1187  * Return all peers currently known to ATS
1188  *
1189  * @param p_it the iterator to call for every peer, callbach with id == NULL
1190  *        when done
1191  * @param p_it_cls the closure for the iterator
1192  */
1193 void
1194 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1195 {
1196   struct PeerIteratorContext ip_ctx;
1197   unsigned int size;
1198
1199   if (NULL == p_it)
1200       return;
1201   GNUNET_assert (NULL != addresses);
1202
1203   size = GNUNET_CONTAINER_multihashmap_size(addresses);
1204   if (0 != size)
1205   {
1206     ip_ctx.it = p_it;
1207     ip_ctx.it_cls = p_it_cls;
1208     ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1209     GNUNET_CONTAINER_multihashmap_iterate (addresses, &peer_it, &ip_ctx);
1210     GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1211   }
1212   p_it (p_it_cls, NULL);
1213 }
1214
1215 struct PeerInfoIteratorContext
1216 {
1217   GNUNET_ATS_PeerInfo_Iterator it;
1218   void *it_cls;
1219 };
1220
1221
1222 static int 
1223 peerinfo_it (void *cls,
1224              const struct GNUNET_HashCode * key,
1225              void *value)
1226 {
1227   struct PeerInfoIteratorContext *pi_ctx = cls;
1228   struct ATS_Address *addr = (struct ATS_Address *)  value;
1229   struct GNUNET_ATS_Information *ats;
1230   uint32_t ats_count;
1231
1232   if (NULL != pi_ctx->it)
1233   {
1234     ats_count = assemble_ats_information (addr, &ats);
1235
1236     pi_ctx->it (pi_ctx->it_cls,
1237                 &addr->peer,
1238                 addr->plugin,
1239                 addr->addr, addr->addr_len,
1240                 addr->active,
1241                 ats, ats_count,
1242                 addr->assigned_bw_out,
1243                 addr->assigned_bw_in);
1244     GNUNET_free (ats);
1245   }
1246   return GNUNET_YES;
1247 }
1248
1249
1250 /**
1251  * Return all peers currently known to ATS
1252  *
1253  * @param peer the respective peer
1254  * @param pi_it the iterator to call for every peer
1255  * @param pi_it_cls the closure for the iterator
1256  */
1257 void
1258 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1259 {
1260   struct PeerInfoIteratorContext pi_ctx;
1261   struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1262   GNUNET_assert (NULL != peer);
1263   GNUNET_assert (NULL != addresses);
1264   if (NULL == pi_it)
1265     return; /* does not make sense without callback */
1266
1267   zero_bw = GNUNET_BANDWIDTH_value_init (0);
1268   pi_ctx.it = pi_it;
1269   pi_ctx.it_cls = pi_it_cls;
1270
1271   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1272
1273   if (NULL != pi_it)
1274     pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1275
1276 }
1277
1278
1279 /* end of gnunet-service-ats_addresses.c */