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