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