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