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