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