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