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