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
65 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
67 static unsigned long long wan_quota_in;
69 static unsigned long long wan_quota_out;
71 static unsigned int active_addr_count;
79 struct GAS_Addresses_Handle
85 /* Solver functions */
86 GAS_solver_init s_init;
87 GAS_solver_done s_done;
88 GAS_solver_address_delete s_del;
89 GAS_solver_address_change_preference s_pref;
94 assemble_ats_information (struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
96 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
97 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
100 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
101 ats[0].value = aa->atsp_utilization_out.value__;
102 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
103 ats[1].value = aa->atsp_utilization_in.value__;
104 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
105 ats[2].value = ntohl(aa->atsp_network_type);
106 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
107 ats[3].value = ntohl(aa->atsp_latency.rel_value);
108 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
109 ats[4].value = ntohl(aa->atsp_distance);
110 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
111 ats[5].value = ntohl (aa->atsp_cost_wan);
112 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
113 ats[6].value = ntohl (aa->atsp_cost_lan);
114 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
115 ats[7].value = ntohl (aa->atsp_cost_wlan);
120 send_bw_notification (struct ATS_Address *aa)
122 struct GNUNET_ATS_Information *ats;
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
126 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
127 ntohl (aa->assigned_bw_out.value__));
128 ats_count = assemble_ats_information (aa, &ats);
130 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
131 aa->addr_len, aa->session_id,
135 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
136 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
138 ats, ats_count, aa->assigned_bw_out,
144 * Update a bandwidth assignment for a peer. This trivial method currently
145 * simply assigns the same share to all active connections.
149 * @param value the 'struct ATS_Address'
150 * @return GNUNET_OK (continue to iterate)
153 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
155 struct ATS_Address *aa = value;
157 if (GNUNET_YES != aa->active)
159 GNUNET_assert (active_addr_count > 0);
163 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
164 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
166 send_bw_notification (aa);
173 * Some (significant) input changed, recalculate bandwidth assignment
177 recalculate_assigned_bw ()
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Recalculating bandwidth for all active connections\n");
181 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
183 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
186 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
190 * Free the given address
191 * @param addr address to destroy
194 free_address (struct ATS_Address *addr)
196 GNUNET_free_non_null (addr->ats);
197 GNUNET_free (addr->plugin);
202 * Create a ATS_address with the given information
204 * @param plugin_name plugin
205 * @param plugin_addr address
206 * @param plugin_addr_len address length
207 * @param session_id session
208 * @return the ATS_Address
210 static struct ATS_Address *
211 create_address (const struct GNUNET_PeerIdentity *peer,
212 const char *plugin_name,
213 const void *plugin_addr, size_t plugin_addr_len,
216 struct ATS_Address *aa = NULL;
218 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
220 aa->addr_len = plugin_addr_len;
222 memcpy (&aa[1], plugin_addr, plugin_addr_len);
223 aa->plugin = GNUNET_strdup (plugin_name);
224 aa->session_id = session_id;
230 * Destroy the given address.
232 * @param addr address to destroy
233 * @return GNUNET_YES if bandwidth allocations should be recalcualted
236 destroy_address (struct ATS_Address *addr)
241 GNUNET_assert (GNUNET_YES ==
242 GNUNET_CONTAINER_multihashmap_remove (addresses,
243 &addr->peer.hashPubKey,
247 if (ats_mode == MODE_MLP)
248 GAS_mlp_address_delete (solver, addresses, addr);
251 if (GNUNET_YES == addr->active)
254 addr->active = GNUNET_NO;
262 struct CompareAddressContext
264 const struct ATS_Address *search;
266 /* exact_address != NULL if address and session is equal */
267 struct ATS_Address *exact_address;
268 /* exact_address != NULL if address and session is 0 */
269 struct ATS_Address *base_address;
274 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
276 struct CompareAddressContext *cac = cls;
277 struct ATS_Address *aa = value;
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
281 aa->addr_len, aa->session_id,
282 cac->search->addr_len, cac->search->session_id);
284 /* Find an matching exact address:
287 * aa->addr_len == cac->search->addr_len
288 * aa->plugin == cac->search->plugin
289 * aa->addr == cac->search->addr
290 * aa->session == cac->search->session
292 * return as exact address
294 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
296 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
297 cac->exact_address = aa;
300 /* Find an matching base address:
304 * aa->session_id == 0
307 * aa->addr_len == cac->search->addr_len
308 * aa->plugin == cac->search->plugin
309 * aa->addr == cac->search->addr
311 * return as base address
313 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
315 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
316 cac->base_address = aa;
319 /* Find an matching exact address based on session:
323 * cac->search->addr_len == 0
326 * aa->plugin == cac->search->plugin
327 * aa->session_id == cac->search->session_id
329 * return as exact address
331 if (0 == cac->search->addr_len)
333 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
334 cac->exact_address = aa;
337 if (cac->exact_address == NULL)
338 return GNUNET_YES; /* Continue iteration to find exact address */
340 return GNUNET_NO; /* Stop iteration since we have an exact address */
345 * Find an existing equivalent address record.
346 * Compares by peer identity and network address OR by session ID
347 * (one of the two must match).
349 * @param peer peer to lookup addresses for
350 * @param addr existing address record
351 * @return existing address record, NULL for none
354 find_address (const struct GNUNET_PeerIdentity *peer,
355 const struct ATS_Address *addr)
357 struct CompareAddressContext cac;
359 cac.exact_address = NULL;
360 cac.base_address = NULL;
362 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
363 &compare_address_it, &cac);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Found exact address: %s base address: %s\n",
368 (cac.exact_address != NULL) ? "YES" : "NO",
369 (cac.base_address != NULL) ? "YES" : "NO");
371 if (cac.exact_address == NULL)
372 return cac.base_address;
373 return cac.exact_address;
377 static struct ATS_Address *
378 lookup_address (const struct GNUNET_PeerIdentity *peer,
379 const char *plugin_name, const void *plugin_addr,
380 size_t plugin_addr_len, uint32_t session_id,
381 const struct GNUNET_ATS_Information *atsi,
384 struct ATS_Address *aa;
385 struct ATS_Address *old;
387 aa = create_address (peer,
389 plugin_addr, plugin_addr_len,
392 aa->mlp_information = NULL;
393 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
394 aa->ats_count = atsi_count;
395 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
397 /* Get existing address or address with session == 0 */
398 old = find_address (peer, aa);
404 else if (old->session_id != session_id)
414 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
416 struct CompareAddressContext *cac = cls;
417 struct ATS_Address *aa = value;
419 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
421 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
423 cac->exact_address = aa;
432 * Find an existing equivalent address record.
433 * Compares by peer identity and network address AND by session ID
434 * (one of the two must match).
436 * @param peer peer to lookup addresses for
437 * @param addr existing address record
438 * @return existing address record, NULL for none
440 static struct ATS_Address *
441 find_exact_address (const struct GNUNET_PeerIdentity *peer,
442 const struct ATS_Address *addr)
444 struct CompareAddressContext cac;
446 cac.exact_address = NULL;
448 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
449 &compare_address_session_it, &cac);
450 return cac.exact_address;
456 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
457 const char *plugin_name, const void *plugin_addr,
458 size_t plugin_addr_len, uint32_t session_id,
459 const struct GNUNET_ATS_Information *atsi,
462 struct ATS_Address *aa;
463 struct ATS_Address *old;
465 if (GNUNET_NO == running)
468 GNUNET_assert (NULL != addresses);
470 aa = create_address (peer,
472 plugin_addr, plugin_addr_len,
475 aa->mlp_information = NULL;
476 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
477 aa->ats_count = atsi_count;
478 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
480 /* Get existing address or address with session == 0 */
481 old = find_address (peer, aa);
484 /* We have a new address */
485 GNUNET_assert (GNUNET_OK ==
486 GNUNET_CONTAINER_multihashmap_put (addresses,
487 &peer->hashPubKey, aa,
488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
490 GNUNET_i2s (peer), session_id, aa);
494 if (old->session_id == 0)
496 /* We have a base address with out an session, update this address */
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Updated existing address for peer `%s' %p with new session %u\n",
499 GNUNET_i2s (peer), old, session_id);
500 GNUNET_free_non_null (old->ats);
501 old->session_id = session_id;
505 old->ats_count = aa->ats_count;
506 GNUNET_free (aa->plugin);
511 /* This address and session is already existing */
512 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
513 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
514 GNUNET_i2s (peer), plugin_name, session_id);
520 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
521 const char *plugin_name, const void *plugin_addr,
522 size_t plugin_addr_len, uint32_t session_id,
523 const struct GNUNET_ATS_Information *atsi,
526 struct ATS_Address *old;
529 if (GNUNET_NO == running)
532 GNUNET_assert (NULL != addresses);
534 /* Get existing address */
535 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
536 session_id, atsi, atsi_count);
539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
540 GNUNET_i2s (peer), plugin_name, session_id);
545 for (i = 0; i < atsi_count; i++)
546 switch (ntohl (atsi[i].type))
548 case GNUNET_ATS_UTILIZATION_UP:
549 old->atsp_utilization_out.value__ = atsi[i].value;
551 case GNUNET_ATS_UTILIZATION_DOWN:
552 old->atsp_utilization_in.value__ = atsi[i].value;
554 case GNUNET_ATS_QUALITY_NET_DELAY:
555 old->atsp_latency.rel_value = ntohl (atsi[i].value);
557 case GNUNET_ATS_QUALITY_NET_DISTANCE:
558 old->atsp_distance = ntohl (atsi[i].value);
560 case GNUNET_ATS_COST_WAN:
561 old->atsp_cost_wan = ntohl (atsi[i].value);
563 case GNUNET_ATS_COST_LAN:
564 old->atsp_cost_lan = ntohl (atsi[i].value);
566 case GNUNET_ATS_COST_WLAN:
567 old->atsp_cost_wlan = ntohl (atsi[i].value);
569 case GNUNET_ATS_NETWORK_TYPE:
570 old->atsp_network_type = ntohl (atsi[i].value);
574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
575 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
580 /* Tell solver about update */
584 GAS_mlp_address_update (solver, addresses, old);
586 case MODE_SIMPLISTIC:
587 GAS_simplistic_address_update (solver, addresses, old);
599 * If session != 0, just the session is deleted, the address itself still exists
600 * If session == 0, remove full address
601 * If session == 0 and addrlen == 0, destroy inbound address
605 * @param value the 'struct ATS_Address'
606 * @return GNUNET_OK (continue to iterate)
609 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
611 const struct ATS_Address *info = cls;
612 struct ATS_Address *aa = value;
615 memcmp (&aa->peer, &info->peer,
616 sizeof (struct GNUNET_PeerIdentity)));
617 /* session == 0, remove full address */
618 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
619 (aa->addr_len == info->addr_len) &&
620 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "Deleting address for peer `%s': `%s' %u\n",
625 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
627 if (GNUNET_YES == destroy_address (aa))
628 recalculate_assigned_bw ();
631 /* session != 0, just remove session */
632 if (aa->session_id != info->session_id)
633 return GNUNET_OK; /* irrelevant */
634 if (aa->session_id != 0)
635 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638 "Deleting session for peer `%s': `%s' %u\n",
639 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
642 if (GNUNET_YES == aa->active)
644 aa->active = GNUNET_NO;
646 recalculate_assigned_bw ();
649 /* session == 0 and addrlen == 0 : destroy address */
650 if (aa->addr_len == 0)
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Deleting session and address for peer `%s': `%s' %u\n",
654 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
655 (void) destroy_address (aa);
659 /* session was set to 0, update address */
661 if (ats_mode == MODE_MLP)
662 GAS_mlp_address_update (solver, addresses, aa);
671 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
672 const char *plugin_name, const void *plugin_addr,
673 size_t plugin_addr_len, uint32_t session_id)
675 struct ATS_Address *aa;
676 struct ATS_Address *old;
678 if (GNUNET_NO == running)
681 /* Get existing address */
682 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
683 session_id, NULL, 0);
686 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
687 GNUNET_i2s (peer), plugin_name, session_id);
692 GNUNET_break (0 < strlen (plugin_name));
693 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
695 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
696 &destroy_by_session_id, aa);
703 * Find a "good" address to use for a peer. If we already have an existing
704 * address, we stick to it. Otherwise, we pick by lowest distance and then
707 * @param cls the 'struct ATS_Address**' where we store the result
709 * @param value another 'struct ATS_Address*' to consider using
710 * @return GNUNET_OK (continue to iterate)
713 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
715 struct ATS_Address **ap = cls;
716 struct ATS_Address *aa = (struct ATS_Address *) value;
717 struct ATS_Address *ab = *ap;
718 struct GNUNET_TIME_Absolute now;
720 now = GNUNET_TIME_absolute_get();
722 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
724 /* This address is blocked for suggestion */
725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726 "Address %p blocked for suggestion for %llu ms \n",
728 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
732 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
733 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
738 /* FIXME this is a hack */
743 if ((0 == strcmp (ab->plugin, "tcp")) &&
744 (0 == strcmp (aa->plugin, "tcp")))
746 if ((0 != ab->addr_len) &&
749 /* saved address was an outbound address, but we have an inbound address */
753 if (0 == ab->addr_len)
755 /* saved address was an inbound address, so do not overwrite */
760 /* FIXME end of hack */
767 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
768 (ntohl (aa->assigned_bw_in.value__) > 0))
770 /* stick to existing connection */
774 if (ab->atsp_distance > aa->atsp_distance)
776 /* user shorter distance */
780 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
782 /* user lower latency */
792 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
793 const char *plugin_name, const void *plugin_addr,
794 size_t plugin_addr_len, uint32_t session_id, int in_use)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
799 GNUNET_i2s (peer), in_use);
802 struct ATS_Address *old;
804 if (GNUNET_NO == running)
805 return GNUNET_SYSERR;
807 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
811 "Trying to set unknown address `%s', %s %u %s \n",
813 plugin_name, session_id,
814 (GNUNET_NO == in_use) ? "NO" : "YES");
816 return GNUNET_SYSERR;
818 if (old->used == in_use)
821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
822 "Address in use called multiple times for peer `%s': %s -> %s \n",
824 (GNUNET_NO == old->used) ? "NO" : "YES",
825 (GNUNET_NO == in_use) ? "NO" : "YES");
826 return GNUNET_SYSERR;
830 /* Tell solver about update */
834 GAS_mlp_address_update (solver, addresses, old);
836 case MODE_SIMPLISTIC:
837 GAS_simplistic_address_update (solver, addresses, old);
848 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
850 struct ATS_Address *aa;
854 /* Get preferred address from MODE_MLP */
855 struct ATS_PreferedAddress * paddr = NULL;
856 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
858 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
859 /* FIXME use bw in value */
860 paddr->bandwidth_in = paddr->bandwidth_out;
861 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
868 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
871 if (aa->active == GNUNET_NO)
873 aa->active = GNUNET_YES;
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 (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;
914 if (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 == running)
937 if (ats_mode == MODE_SIMPLISTIC)
939 request_address_simple (peer);
941 if (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 (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 == running)
984 /* Tell solver about update */
988 GAS_mlp_address_change_preference (solver, peer, kind, score);
990 case MODE_SIMPLISTIC:
991 GAS_simplistic_address_change_preference (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;
1013 char *quota_wan_in_str;
1014 char *quota_wan_out_str;
1016 running = GNUNET_NO;
1018 /* Initialize the system with configuration values */
1019 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
1021 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
1022 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
1023 wan_quota_in = (UINT32_MAX) /10;
1025 GNUNET_free (quota_wan_in_str);
1026 quota_wan_in_str = NULL;
1030 wan_quota_in = (UINT32_MAX) /10;
1033 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
1035 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
1036 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
1037 wan_quota_out = (UINT32_MAX) /10;
1039 GNUNET_free (quota_wan_out_str);
1040 quota_wan_out_str = NULL;
1044 wan_quota_out = (UINT32_MAX) /10;
1047 /* Initialize the addresses database */
1048 addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1049 GNUNET_assert (NULL != addresses);
1051 /* Figure out configured solution method */
1052 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1055 ats_mode = MODE_SIMPLISTIC;
1059 for (c = 0; c < strlen (mode_str); c++)
1060 mode_str[c] = toupper (mode_str[c]);
1061 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1063 ats_mode = MODE_SIMPLISTIC;
1065 else if (0 == strcmp (mode_str, "MLP"))
1067 ats_mode = MODE_MLP;
1069 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1070 ats_mode = MODE_SIMPLISTIC;
1075 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1076 ats_mode = MODE_SIMPLISTIC;
1081 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1083 /* Start configured solution method */
1087 /* Init the MLP solver with default values */
1089 ah->ats_mode = MODE_MLP;
1090 ah->s_init = &GAS_mlp_init;
1091 ah->s_pref = &GAS_mlp_address_change_preference;
1092 ah->s_del = &GAS_mlp_address_delete;
1093 ah->s_done = &GAS_mlp_done;
1099 case MODE_SIMPLISTIC:
1100 /* Init the simplistic solver with default values */
1101 ah->ats_mode = MODE_SIMPLISTIC;
1102 ah->s_init = &GAS_simplistic_init;
1103 ah->s_pref = &GAS_simplistic_address_change_preference;
1104 ah->s_del = &GAS_simplistic_address_delete;
1105 ah->s_done = &GAS_simplistic_done;
1106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1113 GNUNET_assert (NULL != ah->s_init);
1114 GNUNET_assert (NULL != ah->s_pref);
1115 GNUNET_assert (NULL != ah->s_del);
1116 GNUNET_assert (NULL != ah->s_done);
1118 ah->solver = ah->s_init (cfg, stats);
1119 /* REMOVE */ solver = ah->solver;
1120 if (NULL == ah->solver)
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1127 /* up and running */
1128 running = GNUNET_YES;
1134 * Free memory of address.
1137 * @param key peer identity (unused)
1138 * @param value the 'struct ATS_Address' to free
1139 * @return GNUNET_OK (continue to iterate)
1142 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1144 struct ATS_Address *aa = value;
1146 destroy_address (aa);
1152 GAS_addresses_destroy_all ()
1154 if (GNUNET_NO == running)
1157 if (addresses != NULL)
1158 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
1159 GNUNET_assert (active_addr_count == 0);
1164 * Shutdown address subsystem.
1167 GAS_addresses_done ()
1169 GAS_addresses_destroy_all ();
1170 running = GNUNET_NO;
1171 GNUNET_CONTAINER_multihashmap_destroy (addresses);
1174 /* Stop configured solution method */
1178 /* Init the MLP solver with default values */
1179 GAS_mlp_done (solver);
1181 case MODE_SIMPLISTIC:
1182 /* Init the simplistic solver with default values */
1183 GAS_simplistic_done (solver);
1190 struct PeerIteratorContext
1192 GNUNET_ATS_Peer_Iterator it;
1194 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1199 const struct GNUNET_HashCode * key,
1202 struct PeerIteratorContext *ip_ctx = cls;
1203 struct GNUNET_PeerIdentity tmp;
1205 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1207 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1208 tmp.hashPubKey = (*key);
1209 ip_ctx->it (ip_ctx->it_cls, &tmp);
1216 * Return all peers currently known to ATS
1218 * @param p_it the iterator to call for every peer, callbach with id == NULL
1220 * @param p_it_cls the closure for the iterator
1223 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1225 struct PeerIteratorContext ip_ctx;
1230 GNUNET_assert (NULL != addresses);
1232 size = GNUNET_CONTAINER_multihashmap_size(addresses);
1236 ip_ctx.it_cls = p_it_cls;
1237 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1238 GNUNET_CONTAINER_multihashmap_iterate (addresses, &peer_it, &ip_ctx);
1239 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1241 p_it (p_it_cls, NULL);
1244 struct PeerInfoIteratorContext
1246 GNUNET_ATS_PeerInfo_Iterator it;
1252 peerinfo_it (void *cls,
1253 const struct GNUNET_HashCode * key,
1256 struct PeerInfoIteratorContext *pi_ctx = cls;
1257 struct ATS_Address *addr = (struct ATS_Address *) value;
1258 struct GNUNET_ATS_Information *ats;
1261 if (NULL != pi_ctx->it)
1263 ats_count = assemble_ats_information (addr, &ats);
1265 pi_ctx->it (pi_ctx->it_cls,
1268 addr->addr, addr->addr_len,
1271 addr->assigned_bw_out,
1272 addr->assigned_bw_in);
1280 * Return all peers currently known to ATS
1282 * @param peer the respective peer
1283 * @param pi_it the iterator to call for every peer
1284 * @param pi_it_cls the closure for the iterator
1287 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1289 struct PeerInfoIteratorContext pi_ctx;
1290 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1291 GNUNET_assert (NULL != peer);
1292 GNUNET_assert (NULL != addresses);
1294 return; /* does not make sense without callback */
1296 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1298 pi_ctx.it_cls = pi_it_cls;
1300 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1303 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1308 /* end of gnunet-service-ats_addresses.c */