a5480e50e0220c514b72acded4e88a2a0b279985
[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 enum ATS_Mode
39 {
40         SIMPLE,
41         MLP
42 };
43
44 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
45
46 static unsigned long long wan_quota_in;
47
48 static unsigned long long wan_quota_out;
49
50 static unsigned int active_addr_count;
51
52 static int ats_mode;
53
54 /**
55  * Update a bandwidth assignment for a peer.  This trivial method currently
56  * simply assigns the same share to all active connections.
57  *
58  * @param cls unused
59  * @param key unused
60  * @param value the 'struct ATS_Address'
61  * @return GNUNET_OK (continue to iterate)
62  */
63 static int
64 update_bw_it (void *cls, const GNUNET_HashCode * key, void *value)
65 {
66   struct ATS_Address *aa = value;
67
68   if (GNUNET_YES != aa->active)
69     return GNUNET_OK;
70   GNUNET_assert (active_addr_count > 0);
71   aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
72   aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
73   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
74               GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
75               ntohl (aa->assigned_bw_out.value__));
76   GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
77                                               aa->addr_len, aa->session_id,
78                                               aa->ats, aa->ats_count,
79                                               aa->assigned_bw_out,
80                                               aa->assigned_bw_in);
81   GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
82   GAS_performance_notify_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
83                                   aa->ats, aa->ats_count, aa->assigned_bw_out,
84                                   aa->assigned_bw_in);
85   return GNUNET_OK;
86 }
87
88
89 /**
90  * Some (significant) input changed, recalculate bandwidth assignment
91  * for all peers.
92  */
93 static void
94 recalculate_assigned_bw ()
95 {
96   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97               "Recalculating bandwidth for all active connections\n");
98   GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
99                             1, GNUNET_NO);
100   GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
101                          GNUNET_NO);
102   GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_it, NULL);
103 }
104
105 /**
106  * Free the given address
107  * @param addr address to destroy
108  */
109 static void
110 free_address (struct ATS_Address *addr)
111 {
112   GNUNET_free_non_null (addr->ats);
113   GNUNET_free (addr->plugin);
114   GNUNET_free (addr);
115 }
116
117 /**
118  * Create a ATS_address with the given information
119  * @param peer peer
120  * @param plugin_name plugin
121  * @param plugin_addr address
122  * @param plugin_addr_len address length
123  * @param session_id session
124  * @return the ATS_Address
125  */
126 static struct ATS_Address *
127 create_address (const struct GNUNET_PeerIdentity *peer,
128                 const char *plugin_name,
129                 const void *plugin_addr, size_t plugin_addr_len,
130                 uint32_t session_id)
131 {
132   struct ATS_Address *aa = NULL;
133
134   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
135   aa->peer = *peer;
136   aa->addr_len = plugin_addr_len;
137   aa->addr = &aa[1];
138   memcpy (&aa[1], plugin_addr, plugin_addr_len);
139   aa->plugin = GNUNET_strdup (plugin_name);
140   aa->session_id = session_id;
141
142   return aa;
143 }
144
145
146 /**
147  * Destroy the given address.
148  *
149  * @param addr address to destroy
150  * @return GNUNET_YES if bandwidth allocations should be recalcualted
151  */
152 static int
153 destroy_address (struct ATS_Address *addr)
154 {
155   int ret;
156
157   ret = GNUNET_NO;
158   GNUNET_assert (GNUNET_YES ==
159                  GNUNET_CONTAINER_multihashmap_remove (addresses,
160                                                        &addr->peer.hashPubKey,
161                                                        addr));
162   if (GNUNET_YES == addr->active)
163   {
164     active_addr_count--;
165     addr->active = GNUNET_NO;
166     ret = GNUNET_YES;
167   }
168   free_address (addr);
169   return ret;
170 }
171
172
173 struct CompareAddressContext
174 {
175   const struct ATS_Address *search;
176   struct ATS_Address *result;
177 };
178
179
180 static int
181 compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
182 {
183   struct CompareAddressContext *cac = cls;
184   struct ATS_Address *aa = value;
185
186   if (((aa->addr_len != cac->search->addr_len) ||
187        (0 != strcmp (aa->plugin, cac->search->plugin)) ||
188        (0 != memcmp (aa->addr, cac->search->addr, aa->addr_len))) &&
189       ((aa->session_id != cac->search->session_id) ||
190        (cac->search->session_id == 0)))
191     return GNUNET_YES;
192   cac->result = aa;
193   return GNUNET_NO;
194 }
195
196
197 /**
198  * Find an existing equivalent address record.
199  * Compares by peer identity and network address OR by session ID
200  * (one of the two must match).
201  *
202  * @param peer peer to lookup addresses for
203  * @param addr existing address record
204  * @return existing address record, NULL for none
205  */
206 struct ATS_Address *
207 find_address (const struct GNUNET_PeerIdentity *peer,
208               const struct ATS_Address *addr)
209 {
210   struct CompareAddressContext cac;
211
212   cac.result = NULL;
213   cac.search = addr;
214   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
215                                               &compare_address_it, &cac);
216   return cac.result;
217 }
218
219
220 void
221 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
222                       const char *plugin_name, const void *plugin_addr,
223                       size_t plugin_addr_len, uint32_t session_id,
224                       const struct GNUNET_ATS_Information *atsi,
225                       uint32_t atsi_count)
226 {
227   struct ATS_Address *aa;
228   struct ATS_Address *old;
229   uint32_t i;
230
231   aa = create_address (peer,
232                        plugin_name,
233                        plugin_addr, plugin_addr_len,
234                        session_id);
235
236   aa->mlp_information = NULL;
237   aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
238   aa->ats_count = atsi_count;
239   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
240
241   old = find_address (peer, aa);
242   if (old == NULL)
243   {
244     GNUNET_assert (GNUNET_OK ==
245                    GNUNET_CONTAINER_multihashmap_put (addresses,
246                                                       &peer->hashPubKey, aa,
247                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
248     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
249                 GNUNET_i2s (peer), aa);
250     old = aa;
251   }
252   else
253   {
254     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255                 "Updated existing address for peer `%s' %X \n",
256                 GNUNET_i2s (peer), old);
257     GNUNET_free_non_null (old->ats);
258     old->session_id = session_id;
259     old->ats = NULL;
260     old->ats_count = 0;
261     old->ats = aa->ats;
262     old->ats_count = aa->ats_count;
263     GNUNET_free (aa->plugin);
264     GNUNET_free (aa);
265   }
266   for (i = 0; i < atsi_count; i++)
267     switch (ntohl (atsi[i].type))
268     {
269     case GNUNET_ATS_UTILIZATION_UP:
270       old->atsp_utilization_out.value__ = atsi[i].value;
271       break;
272     case GNUNET_ATS_UTILIZATION_DOWN:
273       old->atsp_utilization_in.value__ = atsi[i].value;
274       break;
275     case GNUNET_ATS_QUALITY_NET_DELAY:
276       old->atsp_latency.rel_value = ntohl (atsi[i].value);
277       break;
278     case GNUNET_ATS_QUALITY_NET_DISTANCE:
279       old->atsp_distance = ntohl (atsi[i].value);
280       break;
281     case GNUNET_ATS_COST_WAN:
282       old->atsp_cost_wan = ntohl (atsi[i].value);
283       break;
284     case GNUNET_ATS_COST_LAN:
285       old->atsp_cost_lan = ntohl (atsi[i].value);
286       break;
287     case GNUNET_ATS_COST_WLAN:
288       old->atsp_cost_wlan = ntohl (atsi[i].value);
289       break;
290     case GNUNET_ATS_NETWORK_TYPE:
291       old->atsp_network_type = ntohl (atsi[i].value);
292       break;
293
294     default:
295       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
296                   "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
297       GNUNET_break (0);
298       break;
299     }
300 #if HAVE_LIBGLPK
301   if (ats_mode == MLP)
302     GAS_mlp_address_update (addresses, old);
303 #endif
304 }
305
306
307 /**
308  * Delete an address
309  *
310  * If session != 0, just the session is deleted, the address itself still exists
311  * If session == 0, remove full address
312  * If session == 0 and addrlen == 0, destroy inbound address
313  *
314  * @param cls unused
315  * @param key unused
316  * @param value the 'struct ATS_Address'
317  * @return GNUNET_OK (continue to iterate)
318  */
319 static int
320 destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
321 {
322   const struct ATS_Address *info = cls;
323   struct ATS_Address *aa = value;
324
325   GNUNET_assert (0 ==
326                  memcmp (&aa->peer, &info->peer,
327                          sizeof (struct GNUNET_PeerIdentity)));
328   /* session == 0, remove full address  */
329   if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
330       (aa->addr_len == info->addr_len) &&
331       (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
332   {
333     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334                 "Deleting address for peer `%s': `%s'\n",
335                 GNUNET_i2s (&aa->peer), aa->plugin);
336     if (GNUNET_YES == destroy_address (aa))
337       recalculate_assigned_bw ();
338     return GNUNET_OK;
339   }
340   /* session != 0, just remove session */
341   if (aa->session_id != info->session_id)
342     return GNUNET_OK;           /* irrelevant */
343   if (aa->session_id != 0)
344     GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
345   /* session died */
346   aa->session_id = 0;
347
348   if (GNUNET_YES == aa->active)
349   {
350     aa->active = GNUNET_NO;
351     active_addr_count--;
352     recalculate_assigned_bw ();
353   }
354
355   /* session == 0 and addrlen == 0 : destroy address */
356   if (aa->addr_len == 0)
357     (void) destroy_address (aa);
358
359   return GNUNET_OK;
360 }
361
362
363 void
364 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
365                        const char *plugin_name, const void *plugin_addr,
366                        size_t plugin_addr_len, uint32_t session_id)
367 {
368   struct ATS_Address *aa;
369
370   GNUNET_break (0 < strlen (plugin_name));
371   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
372
373   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
374                                               &destroy_by_session_id, aa);
375
376   free_address (aa);
377 }
378
379
380 /**
381  * Find a "good" address to use for a peer.  If we already have an existing
382  * address, we stick to it.  Otherwise, we pick by lowest distance and then
383  * by lowest latency.
384  *
385  * @param cls the 'struct ATS_Address**' where we store the result
386  * @param key unused
387  * @param value another 'struct ATS_Address*' to consider using
388  * @return GNUNET_OK (continue to iterate)
389  */
390 static int
391 find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
392 {
393   struct ATS_Address **ap = cls;
394   struct ATS_Address *aa = (struct ATS_Address *) value;
395   struct ATS_Address *ab = *ap;
396
397   if (NULL == ab)
398   {
399     *ap = aa;
400     return GNUNET_OK;
401   }
402   if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
403       (ntohl (aa->assigned_bw_in.value__) > 0))
404   {
405     /* stick to existing connection */
406     *ap = aa;
407     return GNUNET_OK;
408   }
409   if (ab->atsp_distance > aa->atsp_distance)
410   {
411     /* user shorter distance */
412     *ap = aa;
413     return GNUNET_OK;
414   }
415   if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
416   {
417     /* user lower latency */
418     *ap = aa;
419     return GNUNET_OK;
420   }
421   /* don't care */
422   return GNUNET_OK;
423 }
424
425
426 void
427 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
428                       const char *plugin_name, const void *plugin_addr,
429                       size_t plugin_addr_len, uint32_t session_id, int in_use)
430 {
431 #if DEBUG_ATS
432   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433               "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
434               GNUNET_i2s (peer), in_use);
435 #endif
436
437   struct ATS_Address *aa;
438   struct ATS_Address *old;
439
440   aa = create_address(peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
441   old = find_address (peer, aa);
442   free_address (aa);
443
444   GNUNET_assert (old != NULL);
445   GNUNET_assert (in_use != old->used);
446   old->used = in_use;
447
448 #if HAVE_LIBGLPK
449   if (ats_mode == MLP)
450      GAS_mlp_address_update (addresses, old);
451 #endif
452 }
453
454 void
455 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
456 {
457   struct ATS_Address *aa;
458
459   aa = NULL;
460   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
461                                               &find_address_it, &aa);
462   if (aa == NULL)
463   {
464     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
466     return;
467   }
468   if (aa->active == GNUNET_NO)
469   {
470     aa->active = GNUNET_YES;
471     active_addr_count++;
472     recalculate_assigned_bw ();
473   }
474   else
475   {
476     /* just to be sure... */
477     GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
478                                                 aa->addr_len, aa->session_id,
479                                                 aa->ats, aa->ats_count,
480                                                 aa->assigned_bw_out,
481                                                 aa->assigned_bw_in);
482   }
483 }
484
485
486 // FIXME: this function should likely end up in the LP-subsystem and
487 // not with 'addresses' in the future...
488 void
489 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
490                                  enum GNUNET_ATS_PreferenceKind kind,
491                                  float score)
492 {
493   // do nothing for now...
494 }
495
496
497
498 /**
499  * Initialize address subsystem.
500  *
501  * @param cfg configuration to use
502  * @param stats the statistics handle to use
503  */
504 void
505 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
506                     const struct GNUNET_STATISTICS_Handle *stats)
507 {
508   GNUNET_assert (GNUNET_OK ==
509                  GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
510                                                       "WAN_QUOTA_IN",
511                                                       &wan_quota_in));
512   GNUNET_assert (GNUNET_OK ==
513                  GNUNET_CONFIGURATION_get_value_size (cfg, "ats",
514                                                       "WAN_QUOTA_OUT",
515                                                       &wan_quota_out));
516
517
518
519   switch (GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP"))
520   {
521         /* MLP = YES */
522         case GNUNET_YES:
523 #if HAVE_LIBGLPK
524           ats_mode = MLP;
525           /* Init the MLP solver with default values */
526           GAS_mlp_init (stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
527           break;
528 #else
529           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
530           ats_mode = SIMPLE;
531           break;
532 #endif
533         /* MLP = NO */
534         case GNUNET_NO:
535                 ats_mode = SIMPLE;
536                 break;
537         /* No configuration value */
538         case GNUNET_SYSERR:
539                 ats_mode = SIMPLE;
540                 break;
541         default:
542                 break;
543   }
544
545   addresses = GNUNET_CONTAINER_multihashmap_create (128);
546 }
547
548
549 /**
550  * Free memory of address.
551  *
552  * @param cls NULL
553  * @param key peer identity (unused)
554  * @param value the 'struct ATS_Address' to free
555  * @return GNUNET_OK (continue to iterate)
556  */
557 static int
558 free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
559 {
560   struct ATS_Address *aa = value;
561
562   destroy_address (aa);
563   return GNUNET_OK;
564 }
565
566
567 void
568 GAS_addresses_destroy_all ()
569 {
570   if (addresses != NULL)
571     GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
572   GNUNET_assert (active_addr_count == 0);
573 }
574
575
576 /**
577  * Shutdown address subsystem.
578  */
579 void
580 GAS_addresses_done ()
581 {
582   GAS_addresses_destroy_all ();
583   GNUNET_CONTAINER_multihashmap_destroy (addresses);
584   addresses = NULL;
585 #if HAVE_LIBGLPK
586   if (ats_mode == MLP)
587   {
588     GAS_mlp_done ();
589   }
590 #endif
591
592 }
593
594
595 /* end of gnunet-service-ats_addresses.c */