2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file ats/gnunet-service-ats_addresses.c
23 * @brief ats service address management
24 * @author Matthias Wachs
25 * @author Christian Grothoff
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"
35 #include "gnunet-service-ats_addresses_mlp.h"
37 #include "gnunet-service-ats_addresses_simplistic.h"
39 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
43 * Available ressource assignment modes
50 * Assign each peer an equal amount of bandwidth (bw)
52 * bw_per_peer = bw_total / #active addresses
59 * Solve ressource assignment as an optimization problem
60 * Uses an mixed integer programming solver
70 struct GAS_Addresses_Handle
72 struct GNUNET_CONTAINER_MultiHashMap *addresses;
74 unsigned long long wan_quota_in;
76 unsigned long long wan_quota_out;
78 unsigned int active_addr_count;
87 /* Solver functions */
88 GAS_solver_init s_init;
89 GAS_solver_done s_done;
90 GAS_solver_address_delete s_del;
91 GAS_solver_address_change_preference s_pref;
94 struct GAS_Addresses_Handle *handle;
97 assemble_ats_information (struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
99 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
100 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
103 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
104 ats[0].value = aa->atsp_utilization_out.value__;
105 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
106 ats[1].value = aa->atsp_utilization_in.value__;
107 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
108 ats[2].value = ntohl(aa->atsp_network_type);
109 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
110 ats[3].value = ntohl(aa->atsp_latency.rel_value);
111 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
112 ats[4].value = ntohl(aa->atsp_distance);
113 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
114 ats[5].value = ntohl (aa->atsp_cost_wan);
115 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
116 ats[6].value = ntohl (aa->atsp_cost_lan);
117 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
118 ats[7].value = ntohl (aa->atsp_cost_wlan);
123 send_bw_notification (struct ATS_Address *aa)
125 struct GNUNET_ATS_Information *ats;
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
129 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
130 ntohl (aa->assigned_bw_out.value__));
131 ats_count = assemble_ats_information (aa, &ats);
133 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
134 aa->addr_len, aa->session_id,
138 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
139 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
141 ats, ats_count, aa->assigned_bw_out,
147 * Update a bandwidth assignment for a peer. This trivial method currently
148 * simply assigns the same share to all active connections.
152 * @param value the 'struct ATS_Address'
153 * @return GNUNET_OK (continue to iterate)
156 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
158 struct ATS_Address *aa = value;
160 if (GNUNET_YES != aa->active)
162 GNUNET_assert (handle->active_addr_count > 0);
166 aa->assigned_bw_in.value__ = htonl (handle->wan_quota_in / handle->active_addr_count);
167 aa->assigned_bw_out.value__ = htonl (handle->wan_quota_out / handle->active_addr_count);
169 send_bw_notification (aa);
176 * Some (significant) input changed, recalculate bandwidth assignment
180 recalculate_assigned_bw ()
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Recalculating bandwidth for all active connections\n");
184 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
186 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", handle->active_addr_count,
189 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &update_bw_simple_it, NULL);
193 * Free the given address
194 * @param addr address to destroy
197 free_address (struct ATS_Address *addr)
199 GNUNET_free_non_null (addr->ats);
200 GNUNET_free (addr->plugin);
205 * Create a ATS_address with the given information
207 * @param plugin_name plugin
208 * @param plugin_addr address
209 * @param plugin_addr_len address length
210 * @param session_id session
211 * @return the ATS_Address
213 static struct ATS_Address *
214 create_address (const struct GNUNET_PeerIdentity *peer,
215 const char *plugin_name,
216 const void *plugin_addr, size_t plugin_addr_len,
219 struct ATS_Address *aa = NULL;
221 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
223 aa->addr_len = plugin_addr_len;
225 memcpy (&aa[1], plugin_addr, plugin_addr_len);
226 aa->plugin = GNUNET_strdup (plugin_name);
227 aa->session_id = session_id;
233 * Destroy the given address.
235 * @param addr address to destroy
236 * @return GNUNET_YES if bandwidth allocations should be recalcualted
239 destroy_address (struct ATS_Address *addr)
244 GNUNET_assert (GNUNET_YES ==
245 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
246 &addr->peer.hashPubKey,
250 if (handle->ats_mode == MODE_MLP)
251 GAS_mlp_address_delete (handle->solver, handle->addresses, addr);
254 if (GNUNET_YES == addr->active)
256 handle->active_addr_count--;
257 addr->active = GNUNET_NO;
265 struct CompareAddressContext
267 const struct ATS_Address *search;
269 /* exact_address != NULL if address and session is equal */
270 struct ATS_Address *exact_address;
271 /* exact_address != NULL if address and session is 0 */
272 struct ATS_Address *base_address;
277 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
279 struct CompareAddressContext *cac = cls;
280 struct ATS_Address *aa = value;
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
284 aa->addr_len, aa->session_id,
285 cac->search->addr_len, cac->search->session_id);
287 /* Find an matching exact address:
290 * aa->addr_len == cac->search->addr_len
291 * aa->plugin == cac->search->plugin
292 * aa->addr == cac->search->addr
293 * aa->session == cac->search->session
295 * return as exact address
297 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
299 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
300 cac->exact_address = aa;
303 /* Find an matching base address:
307 * aa->session_id == 0
310 * aa->addr_len == cac->search->addr_len
311 * aa->plugin == cac->search->plugin
312 * aa->addr == cac->search->addr
314 * return as base address
316 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
318 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
319 cac->base_address = aa;
322 /* Find an matching exact address based on session:
326 * cac->search->addr_len == 0
329 * aa->plugin == cac->search->plugin
330 * aa->session_id == cac->search->session_id
332 * return as exact address
334 if (0 == cac->search->addr_len)
336 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
337 cac->exact_address = aa;
340 if (cac->exact_address == NULL)
341 return GNUNET_YES; /* Continue iteration to find exact address */
343 return GNUNET_NO; /* Stop iteration since we have an exact address */
348 * Find an existing equivalent address record.
349 * Compares by peer identity and network address OR by session ID
350 * (one of the two must match).
352 * @param peer peer to lookup addresses for
353 * @param addr existing address record
354 * @return existing address record, NULL for none
357 find_address (const struct GNUNET_PeerIdentity *peer,
358 const struct ATS_Address *addr)
360 struct CompareAddressContext cac;
362 cac.exact_address = NULL;
363 cac.base_address = NULL;
365 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
366 &compare_address_it, &cac);
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Found exact address: %s base address: %s\n",
371 (cac.exact_address != NULL) ? "YES" : "NO",
372 (cac.base_address != NULL) ? "YES" : "NO");
374 if (cac.exact_address == NULL)
375 return cac.base_address;
376 return cac.exact_address;
380 static struct ATS_Address *
381 lookup_address (const struct GNUNET_PeerIdentity *peer,
382 const char *plugin_name, const void *plugin_addr,
383 size_t plugin_addr_len, uint32_t session_id,
384 const struct GNUNET_ATS_Information *atsi,
387 struct ATS_Address *aa;
388 struct ATS_Address *old;
390 aa = create_address (peer,
392 plugin_addr, plugin_addr_len,
395 aa->mlp_information = NULL;
396 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
397 aa->ats_count = atsi_count;
398 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
400 /* Get existing address or address with session == 0 */
401 old = find_address (peer, aa);
407 else if (old->session_id != session_id)
416 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
418 struct CompareAddressContext *cac = cls;
419 struct ATS_Address *aa = value;
421 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
423 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
425 cac->exact_address = aa;
434 * Find an existing equivalent address record.
435 * Compares by peer identity and network address AND by session ID
436 * (one of the two must match).
438 * @param peer peer to lookup addresses for
439 * @param addr existing address record
440 * @return existing address record, NULL for none
442 static struct ATS_Address *
443 find_exact_address (const struct GNUNET_PeerIdentity *peer,
444 const struct ATS_Address *addr)
446 struct CompareAddressContext cac;
448 cac.exact_address = NULL;
450 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
451 &compare_address_session_it, &cac);
452 return cac.exact_address;
457 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
458 const char *plugin_name, const void *plugin_addr,
459 size_t plugin_addr_len, uint32_t session_id,
460 const struct GNUNET_ATS_Information *atsi,
463 struct ATS_Address *aa;
464 struct ATS_Address *old;
466 if (GNUNET_NO == handle->running)
469 GNUNET_assert (NULL != handle->addresses);
471 aa = create_address (peer,
473 plugin_addr, plugin_addr_len,
476 aa->mlp_information = NULL;
477 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
478 aa->ats_count = atsi_count;
479 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
481 /* Get existing address or address with session == 0 */
482 old = find_address (peer, aa);
485 /* We have a new address */
486 GNUNET_assert (GNUNET_OK ==
487 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
488 &peer->hashPubKey, aa,
489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
491 GNUNET_i2s (peer), session_id, aa);
495 if (old->session_id == 0)
497 /* We have a base address with out an session, update this address */
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Updated existing address for peer `%s' %p with new session %u\n",
500 GNUNET_i2s (peer), old, session_id);
501 GNUNET_free_non_null (old->ats);
502 old->session_id = session_id;
506 old->ats_count = aa->ats_count;
507 GNUNET_free (aa->plugin);
512 /* This address and session is already existing */
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
515 GNUNET_i2s (peer), plugin_name, session_id);
521 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
522 const char *plugin_name, const void *plugin_addr,
523 size_t plugin_addr_len, uint32_t session_id,
524 const struct GNUNET_ATS_Information *atsi,
527 struct ATS_Address *old;
530 if (GNUNET_NO == handle->running)
533 GNUNET_assert (NULL != handle->addresses);
535 /* Get existing address */
536 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
537 session_id, atsi, atsi_count);
540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
541 GNUNET_i2s (peer), plugin_name, session_id);
546 for (i = 0; i < atsi_count; i++)
547 switch (ntohl (atsi[i].type))
549 case GNUNET_ATS_UTILIZATION_UP:
550 old->atsp_utilization_out.value__ = atsi[i].value;
552 case GNUNET_ATS_UTILIZATION_DOWN:
553 old->atsp_utilization_in.value__ = atsi[i].value;
555 case GNUNET_ATS_QUALITY_NET_DELAY:
556 old->atsp_latency.rel_value = ntohl (atsi[i].value);
558 case GNUNET_ATS_QUALITY_NET_DISTANCE:
559 old->atsp_distance = ntohl (atsi[i].value);
561 case GNUNET_ATS_COST_WAN:
562 old->atsp_cost_wan = ntohl (atsi[i].value);
564 case GNUNET_ATS_COST_LAN:
565 old->atsp_cost_lan = ntohl (atsi[i].value);
567 case GNUNET_ATS_COST_WLAN:
568 old->atsp_cost_wlan = ntohl (atsi[i].value);
570 case GNUNET_ATS_NETWORK_TYPE:
571 old->atsp_network_type = ntohl (atsi[i].value);
575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
576 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
581 /* Tell solver about update */
582 switch (handle->ats_mode)
585 GAS_mlp_address_update (handle->solver, handle->addresses, old);
587 case MODE_SIMPLISTIC:
588 GAS_simplistic_address_update (handle->solver, handle->addresses, old);
600 * If session != 0, just the session is deleted, the address itself still exists
601 * If session == 0, remove full address
602 * If session == 0 and addrlen == 0, destroy inbound address
606 * @param value the 'struct ATS_Address'
607 * @return GNUNET_OK (continue to iterate)
610 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
612 const struct ATS_Address *info = cls;
613 struct ATS_Address *aa = value;
616 memcmp (&aa->peer, &info->peer,
617 sizeof (struct GNUNET_PeerIdentity)));
618 /* session == 0, remove full address */
619 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
620 (aa->addr_len == info->addr_len) &&
621 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Deleting address for peer `%s': `%s' %u\n",
626 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
628 if (GNUNET_YES == destroy_address (aa))
629 recalculate_assigned_bw ();
632 /* session != 0, just remove session */
633 if (aa->session_id != info->session_id)
634 return GNUNET_OK; /* irrelevant */
635 if (aa->session_id != 0)
636 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "Deleting session for peer `%s': `%s' %u\n",
640 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
643 if (GNUNET_YES == aa->active)
645 aa->active = GNUNET_NO;
646 handle->active_addr_count--;
647 recalculate_assigned_bw ();
650 /* session == 0 and addrlen == 0 : destroy address */
651 if (aa->addr_len == 0)
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Deleting session and address for peer `%s': `%s' %u\n",
655 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
656 (void) destroy_address (aa);
660 /* session was set to 0, update address */
662 if (handle->ats_mode == MODE_MLP)
663 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
672 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
673 const char *plugin_name, const void *plugin_addr,
674 size_t plugin_addr_len, uint32_t session_id)
676 struct ATS_Address *aa;
677 struct ATS_Address *old;
679 if (GNUNET_NO == handle->running)
682 /* Get existing address */
683 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
684 session_id, NULL, 0);
687 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
688 GNUNET_i2s (peer), plugin_name, session_id);
693 GNUNET_break (0 < strlen (plugin_name));
694 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
696 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
697 &destroy_by_session_id, aa);
704 * Find a "good" address to use for a peer. If we already have an existing
705 * address, we stick to it. Otherwise, we pick by lowest distance and then
708 * @param cls the 'struct ATS_Address**' where we store the result
710 * @param value another 'struct ATS_Address*' to consider using
711 * @return GNUNET_OK (continue to iterate)
714 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
716 struct ATS_Address **ap = cls;
717 struct ATS_Address *aa = (struct ATS_Address *) value;
718 struct ATS_Address *ab = *ap;
719 struct GNUNET_TIME_Absolute now;
721 now = GNUNET_TIME_absolute_get();
723 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
725 /* This address is blocked for suggestion */
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Address %p blocked for suggestion for %llu ms \n",
729 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
733 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
734 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
739 /* FIXME this is a hack */
744 if ((0 == strcmp (ab->plugin, "tcp")) &&
745 (0 == strcmp (aa->plugin, "tcp")))
747 if ((0 != ab->addr_len) &&
750 /* saved address was an outbound address, but we have an inbound address */
754 if (0 == ab->addr_len)
756 /* saved address was an inbound address, so do not overwrite */
761 /* FIXME end of hack */
768 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
769 (ntohl (aa->assigned_bw_in.value__) > 0))
771 /* stick to existing connection */
775 if (ab->atsp_distance > aa->atsp_distance)
777 /* user shorter distance */
781 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
783 /* user lower latency */
793 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
794 const char *plugin_name, const void *plugin_addr,
795 size_t plugin_addr_len, uint32_t session_id, int in_use)
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
800 GNUNET_i2s (peer), in_use);
803 struct ATS_Address *old;
805 if (GNUNET_NO == handle->running)
806 return GNUNET_SYSERR;
808 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
812 "Trying to set unknown address `%s', %s %u %s \n",
814 plugin_name, session_id,
815 (GNUNET_NO == in_use) ? "NO" : "YES");
817 return GNUNET_SYSERR;
819 if (old->used == in_use)
822 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
823 "Address in use called multiple times for peer `%s': %s -> %s \n",
825 (GNUNET_NO == old->used) ? "NO" : "YES",
826 (GNUNET_NO == in_use) ? "NO" : "YES");
827 return GNUNET_SYSERR;
831 /* Tell solver about update */
832 switch (handle->ats_mode)
835 GAS_mlp_address_update (handle->solver, handle->addresses, old);
837 case MODE_SIMPLISTIC:
838 GAS_simplistic_address_update (handle->solver, handle->addresses, old);
849 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
851 struct ATS_Address *aa;
855 /* Get preferred address from MODE_MLP */
856 struct ATS_PreferedAddress * paddr = NULL;
857 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
859 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
860 /* FIXME use bw in value */
861 paddr->bandwidth_in = paddr->bandwidth_out;
862 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
869 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
872 if (aa->active == GNUNET_NO)
874 aa->active = GNUNET_YES;
875 handle->active_addr_count++;
876 send_bw_notification (aa);
880 /* just to be sure... */
881 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
882 aa->addr_len, aa->session_id,
883 aa->ats, aa->ats_count,
892 request_address_simple (const struct GNUNET_PeerIdentity *peer)
894 struct ATS_Address *aa;
897 /* Get address with: stick to current address, lower distance, lower latency */
898 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
899 &find_address_it, &aa);
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
903 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
908 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
910 if (aa->active == GNUNET_NO)
912 aa->active = GNUNET_YES;
913 handle->active_addr_count++;
914 if (handle->ats_mode == MODE_SIMPLISTIC)
916 recalculate_assigned_bw ();
921 /* just to be sure... */
922 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
923 aa->addr_len, aa->session_id,
924 aa->ats, aa->ats_count,
932 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
934 if (GNUNET_NO == handle->running)
937 if (handle->ats_mode == MODE_SIMPLISTIC)
939 request_address_simple (peer);
941 if (handle->ats_mode == MODE_MLP)
943 request_address_mlp(peer);
949 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
951 struct ATS_Address *aa = value;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
956 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
957 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
963 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
965 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
973 // FIXME: this function should likely end up in the LP-subsystem and
974 // not with 'addresses' in the future...
976 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
977 enum GNUNET_ATS_PreferenceKind kind,
980 if (GNUNET_NO == handle->running)
984 /* Tell solver about update */
985 switch (handle->ats_mode)
988 GAS_mlp_address_change_preference (handle->solver, peer, kind, score);
990 case MODE_SIMPLISTIC:
991 GAS_simplistic_address_change_preference (handle->solver, peer, kind, score);
1002 * Initialize address subsystem.
1004 * @param cfg configuration to use
1005 * @param stats the statistics handle to use
1007 struct GAS_Addresses_Handle *
1008 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1009 const struct GNUNET_STATISTICS_Handle *stats)
1011 struct GAS_Addresses_Handle *ah;
1012 char *quota_wan_in_str;
1013 char *quota_wan_out_str;
1017 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1019 handle->running = GNUNET_NO;
1021 /* Initialize the system with configuration values */
1022 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
1024 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
1025 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &ah->wan_quota_in)))
1026 ah->wan_quota_in = (UINT32_MAX) /10;
1028 GNUNET_free (quota_wan_in_str);
1029 quota_wan_in_str = NULL;
1032 ah->wan_quota_in = (UINT32_MAX) /10;
1034 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
1036 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
1037 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &ah->wan_quota_out)))
1038 ah->wan_quota_out = (UINT32_MAX) /10;
1040 GNUNET_free (quota_wan_out_str);
1041 quota_wan_out_str = NULL;
1044 ah->wan_quota_out = (UINT32_MAX) /10;
1046 /* Initialize the addresses database */
1047 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1048 GNUNET_assert (NULL != ah->addresses);
1050 /* Figure out configured solution method */
1051 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1053 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1054 ah->ats_mode = MODE_SIMPLISTIC;
1058 for (c = 0; c < strlen (mode_str); c++)
1059 mode_str[c] = toupper (mode_str[c]);
1060 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1062 ah->ats_mode = MODE_SIMPLISTIC;
1064 else if (0 == strcmp (mode_str, "MLP"))
1066 ah->ats_mode = MODE_MLP;
1068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1069 ah->ats_mode = MODE_SIMPLISTIC;
1074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1075 ah->ats_mode = MODE_SIMPLISTIC;
1078 /* Start configured solution method */
1079 switch (ah->ats_mode)
1082 /* Init the MLP solver with default values */
1084 ah->ats_mode = MODE_MLP;
1085 ah->s_init = &GAS_mlp_init;
1086 ah->s_pref = &GAS_mlp_address_change_preference;
1087 ah->s_del = &GAS_mlp_address_delete;
1088 ah->s_done = &GAS_mlp_done;
1094 case MODE_SIMPLISTIC:
1095 /* Init the simplistic solver with default values */
1096 ah->ats_mode = MODE_SIMPLISTIC;
1097 ah->s_init = &GAS_simplistic_init;
1098 ah->s_pref = &GAS_simplistic_address_change_preference;
1099 ah->s_del = &GAS_simplistic_address_delete;
1100 ah->s_done = &GAS_simplistic_done;
1101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1108 GNUNET_assert (NULL != ah->s_init);
1109 GNUNET_assert (NULL != ah->s_pref);
1110 GNUNET_assert (NULL != ah->s_del);
1111 GNUNET_assert (NULL != ah->s_done);
1113 ah->solver = ah->s_init (cfg, stats);
1114 if (NULL == ah->solver)
1116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1121 /* up and running */
1122 ah->running = GNUNET_YES;
1128 * Free memory of address.
1131 * @param key peer identity (unused)
1132 * @param value the 'struct ATS_Address' to free
1133 * @return GNUNET_OK (continue to iterate)
1136 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1138 struct ATS_Address *aa = value;
1140 destroy_address (aa);
1146 GAS_addresses_destroy_all ()
1148 if (GNUNET_NO == handle->running)
1151 if (handle->addresses != NULL)
1152 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1153 GNUNET_assert (handle->active_addr_count == 0);
1158 * Shutdown address subsystem.
1161 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1163 GNUNET_assert (NULL != handle);
1165 GAS_addresses_destroy_all ();
1166 handle->running = GNUNET_NO;
1167 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1168 handle->addresses = NULL;
1169 GNUNET_free (handle);
1170 /* Stop configured solution method */
1174 struct PeerIteratorContext
1176 GNUNET_ATS_Peer_Iterator it;
1178 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1183 const struct GNUNET_HashCode * key,
1186 struct PeerIteratorContext *ip_ctx = cls;
1187 struct GNUNET_PeerIdentity tmp;
1189 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1191 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1192 tmp.hashPubKey = (*key);
1193 ip_ctx->it (ip_ctx->it_cls, &tmp);
1200 * Return all peers currently known to ATS
1202 * @param p_it the iterator to call for every peer, callbach with id == NULL
1204 * @param p_it_cls the closure for the iterator
1207 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1209 struct PeerIteratorContext ip_ctx;
1214 GNUNET_assert (NULL != handle->addresses);
1216 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1220 ip_ctx.it_cls = p_it_cls;
1221 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1222 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1223 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1225 p_it (p_it_cls, NULL);
1228 struct PeerInfoIteratorContext
1230 GNUNET_ATS_PeerInfo_Iterator it;
1236 peerinfo_it (void *cls,
1237 const struct GNUNET_HashCode * key,
1240 struct PeerInfoIteratorContext *pi_ctx = cls;
1241 struct ATS_Address *addr = (struct ATS_Address *) value;
1242 struct GNUNET_ATS_Information *ats;
1245 if (NULL != pi_ctx->it)
1247 ats_count = assemble_ats_information (addr, &ats);
1249 pi_ctx->it (pi_ctx->it_cls,
1252 addr->addr, addr->addr_len,
1255 addr->assigned_bw_out,
1256 addr->assigned_bw_in);
1264 * Return all peers currently known to ATS
1266 * @param peer the respective peer
1267 * @param pi_it the iterator to call for every peer
1268 * @param pi_it_cls the closure for the iterator
1271 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1273 struct PeerInfoIteratorContext pi_ctx;
1274 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1275 GNUNET_assert (NULL != peer);
1276 GNUNET_assert (NULL != handle->addresses);
1278 return; /* does not make sense without callback */
1280 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1282 pi_ctx.it_cls = pi_it_cls;
1284 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1287 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1292 /* end of gnunet-service-ats_addresses.c */