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