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
66 * Handle for ATS address component
68 struct GAS_Addresses_Suggestion_Requests
70 struct GAS_Addresses_Suggestion_Requests *next;
71 struct GAS_Addresses_Suggestion_Requests *prev;
73 struct GNUNET_PeerIdentity id;
77 * Handle for ATS address component
79 struct GAS_Addresses_Handle
82 * A multihashmap to store all addresses
84 struct GNUNET_CONTAINER_MultiHashMap *addresses;
87 * Configure WAN quota in
89 unsigned long long wan_quota_in;
92 * Configure WAN quota out
94 unsigned long long wan_quota_out;
97 * Number of active addresses
99 unsigned int active_addr_count;
102 * Is ATS addresses running
107 * Configured ATS solver
117 * Address suggestion requests DLL head
119 struct GAS_Addresses_Suggestion_Requests *r_head;
122 * Address suggestion requests DLL tail
124 struct GAS_Addresses_Suggestion_Requests *r_tail;
126 /* Solver functions */
131 GAS_solver_init s_init;
134 * Update address in solver
136 GAS_solver_address_update s_update;
139 * Get address from solver
141 GAS_solver_get_preferred_address s_get;
144 * Delete address in solver
146 GAS_solver_address_delete s_del;
149 * Change preference for quality in solver
151 GAS_solver_address_change_preference s_pref;
156 GAS_solver_done s_done;
163 struct GAS_Addresses_Handle *handle;
167 assemble_ats_information (struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
169 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
170 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
173 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
174 ats[0].value = aa->atsp_utilization_out.value__;
175 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
176 ats[1].value = aa->atsp_utilization_in.value__;
177 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
178 ats[2].value = ntohl(aa->atsp_network_type);
179 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
180 ats[3].value = ntohl(aa->atsp_latency.rel_value);
181 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
182 ats[4].value = ntohl(aa->atsp_distance);
183 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
184 ats[5].value = ntohl (aa->atsp_cost_wan);
185 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
186 ats[6].value = ntohl (aa->atsp_cost_lan);
187 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
188 ats[7].value = ntohl (aa->atsp_cost_wlan);
193 send_bw_notification (struct ATS_Address *aa)
195 struct GNUNET_ATS_Information *ats;
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
199 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
200 ntohl (aa->assigned_bw_out.value__));
201 ats_count = assemble_ats_information (aa, &ats);
203 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
204 aa->addr_len, aa->session_id,
208 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
209 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
211 ats, ats_count, aa->assigned_bw_out,
217 * Update a bandwidth assignment for a peer. This trivial method currently
218 * simply assigns the same share to all active connections.
222 * @param value the 'struct ATS_Address'
223 * @return GNUNET_OK (continue to iterate)
226 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
228 struct ATS_Address *aa = value;
230 if (GNUNET_YES != aa->active)
232 GNUNET_assert (handle->active_addr_count > 0);
236 aa->assigned_bw_in.value__ = htonl (handle->wan_quota_in / handle->active_addr_count);
237 aa->assigned_bw_out.value__ = htonl (handle->wan_quota_out / handle->active_addr_count);
239 send_bw_notification (aa);
246 * Some (significant) input changed, recalculate bandwidth assignment
250 recalculate_assigned_bw ()
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Recalculating bandwidth for all active connections\n");
254 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
256 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", handle->active_addr_count,
259 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &update_bw_simple_it, NULL);
263 * Free the given address
264 * @param addr address to destroy
267 free_address (struct ATS_Address *addr)
269 GNUNET_free_non_null (addr->ats);
270 GNUNET_free (addr->plugin);
275 * Create a ATS_address with the given information
277 * @param plugin_name plugin
278 * @param plugin_addr address
279 * @param plugin_addr_len address length
280 * @param session_id session
281 * @return the ATS_Address
283 static struct ATS_Address *
284 create_address (const struct GNUNET_PeerIdentity *peer,
285 const char *plugin_name,
286 const void *plugin_addr, size_t plugin_addr_len,
289 struct ATS_Address *aa = NULL;
291 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
293 aa->addr_len = plugin_addr_len;
295 memcpy (&aa[1], plugin_addr, plugin_addr_len);
296 aa->plugin = GNUNET_strdup (plugin_name);
297 aa->session_id = session_id;
303 * Destroy the given address.
305 * @param addr address to destroy
306 * @return GNUNET_YES if bandwidth allocations should be recalcualted
309 destroy_address (struct ATS_Address *addr)
314 GNUNET_assert (GNUNET_YES ==
315 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
316 &addr->peer.hashPubKey,
320 if (handle->ats_mode == MODE_MLP)
321 GAS_mlp_address_delete (handle->solver, handle->addresses, addr);
324 if (GNUNET_YES == addr->active)
326 handle->active_addr_count--;
327 addr->active = GNUNET_NO;
335 struct CompareAddressContext
337 const struct ATS_Address *search;
339 /* exact_address != NULL if address and session is equal */
340 struct ATS_Address *exact_address;
341 /* exact_address != NULL if address and session is 0 */
342 struct ATS_Address *base_address;
347 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
349 struct CompareAddressContext *cac = cls;
350 struct ATS_Address *aa = value;
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
354 aa->addr_len, aa->session_id,
355 cac->search->addr_len, cac->search->session_id);
357 /* Find an matching exact address:
360 * aa->addr_len == cac->search->addr_len
361 * aa->plugin == cac->search->plugin
362 * aa->addr == cac->search->addr
363 * aa->session == cac->search->session
365 * return as exact address
367 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
369 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
370 cac->exact_address = aa;
373 /* Find an matching base address:
377 * aa->session_id == 0
380 * aa->addr_len == cac->search->addr_len
381 * aa->plugin == cac->search->plugin
382 * aa->addr == cac->search->addr
384 * return as base address
386 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
388 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
389 cac->base_address = aa;
392 /* Find an matching exact address based on session:
396 * cac->search->addr_len == 0
399 * aa->plugin == cac->search->plugin
400 * aa->session_id == cac->search->session_id
402 * return as exact address
404 if (0 == cac->search->addr_len)
406 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
407 cac->exact_address = aa;
410 if (cac->exact_address == NULL)
411 return GNUNET_YES; /* Continue iteration to find exact address */
413 return GNUNET_NO; /* Stop iteration since we have an exact address */
418 * Find an existing equivalent address record.
419 * Compares by peer identity and network address OR by session ID
420 * (one of the two must match).
422 * @param peer peer to lookup addresses for
423 * @param addr existing address record
424 * @return existing address record, NULL for none
427 find_address (const struct GNUNET_PeerIdentity *peer,
428 const struct ATS_Address *addr)
430 struct CompareAddressContext cac;
432 cac.exact_address = NULL;
433 cac.base_address = NULL;
435 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
436 &compare_address_it, &cac);
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Found exact address: %s base address: %s\n",
441 (cac.exact_address != NULL) ? "YES" : "NO",
442 (cac.base_address != NULL) ? "YES" : "NO");
444 if (cac.exact_address == NULL)
445 return cac.base_address;
446 return cac.exact_address;
450 static struct ATS_Address *
451 lookup_address (const struct GNUNET_PeerIdentity *peer,
452 const char *plugin_name, const void *plugin_addr,
453 size_t plugin_addr_len, uint32_t session_id,
454 const struct GNUNET_ATS_Information *atsi,
457 struct ATS_Address *aa;
458 struct ATS_Address *old;
460 aa = create_address (peer,
462 plugin_addr, plugin_addr_len,
465 aa->mlp_information = NULL;
466 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
467 aa->ats_count = atsi_count;
468 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
470 /* Get existing address or address with session == 0 */
471 old = find_address (peer, aa);
477 else if (old->session_id != session_id)
487 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
489 struct CompareAddressContext *cac = cls;
490 struct ATS_Address *aa = value;
492 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
494 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
496 cac->exact_address = aa;
505 * Find an existing equivalent address record.
506 * Compares by peer identity and network address AND by session ID
507 * (one of the two must match).
509 * @param peer peer to lookup addresses for
510 * @param addr existing address record
511 * @return existing address record, NULL for none
513 static struct ATS_Address *
514 find_exact_address (const struct GNUNET_PeerIdentity *peer,
515 const struct ATS_Address *addr)
517 struct CompareAddressContext cac;
519 cac.exact_address = NULL;
521 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
522 &compare_address_session_it, &cac);
523 return cac.exact_address;
528 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
529 const char *plugin_name, const void *plugin_addr,
530 size_t plugin_addr_len, uint32_t session_id,
531 const struct GNUNET_ATS_Information *atsi,
534 struct ATS_Address *aa;
535 struct ATS_Address *old;
537 if (GNUNET_NO == handle->running)
540 GNUNET_assert (NULL != handle->addresses);
542 aa = create_address (peer,
544 plugin_addr, plugin_addr_len,
547 aa->mlp_information = NULL;
548 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
549 aa->ats_count = atsi_count;
550 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
552 /* Get existing address or address with session == 0 */
553 old = find_address (peer, aa);
556 /* We have a new address */
557 GNUNET_assert (GNUNET_OK ==
558 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
559 &peer->hashPubKey, aa,
560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
562 GNUNET_i2s (peer), session_id, aa);
566 if (old->session_id == 0)
568 /* We have a base address with out an session, update this address */
569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
570 "Updated existing address for peer `%s' %p with new session %u\n",
571 GNUNET_i2s (peer), old, session_id);
572 GNUNET_free_non_null (old->ats);
573 old->session_id = session_id;
577 old->ats_count = aa->ats_count;
578 GNUNET_free (aa->plugin);
583 /* This address and session is already existing */
584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
585 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
586 GNUNET_i2s (peer), plugin_name, session_id);
592 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
593 const char *plugin_name, const void *plugin_addr,
594 size_t plugin_addr_len, uint32_t session_id,
595 const struct GNUNET_ATS_Information *atsi,
598 struct ATS_Address *old;
601 if (GNUNET_NO == handle->running)
604 GNUNET_assert (NULL != handle->addresses);
606 /* Get existing address */
607 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
608 session_id, atsi, atsi_count);
611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
612 GNUNET_i2s (peer), plugin_name, session_id);
617 for (i = 0; i < atsi_count; i++)
618 switch (ntohl (atsi[i].type))
620 case GNUNET_ATS_UTILIZATION_UP:
621 old->atsp_utilization_out.value__ = atsi[i].value;
623 case GNUNET_ATS_UTILIZATION_DOWN:
624 old->atsp_utilization_in.value__ = atsi[i].value;
626 case GNUNET_ATS_QUALITY_NET_DELAY:
627 old->atsp_latency.rel_value = ntohl (atsi[i].value);
629 case GNUNET_ATS_QUALITY_NET_DISTANCE:
630 old->atsp_distance = ntohl (atsi[i].value);
632 case GNUNET_ATS_COST_WAN:
633 old->atsp_cost_wan = ntohl (atsi[i].value);
635 case GNUNET_ATS_COST_LAN:
636 old->atsp_cost_lan = ntohl (atsi[i].value);
638 case GNUNET_ATS_COST_WLAN:
639 old->atsp_cost_wlan = ntohl (atsi[i].value);
641 case GNUNET_ATS_NETWORK_TYPE:
642 old->atsp_network_type = ntohl (atsi[i].value);
646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
647 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
652 /* Tell solver about update */
653 handle->s_update (handle->solver, handle->addresses, old);
660 * If session != 0, just the session is deleted, the address itself still exists
661 * If session == 0, remove full address
662 * If session == 0 and addrlen == 0, destroy inbound address
666 * @param value the 'struct ATS_Address'
667 * @return GNUNET_OK (continue to iterate)
670 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
672 const struct ATS_Address *info = cls;
673 struct ATS_Address *aa = value;
676 memcmp (&aa->peer, &info->peer,
677 sizeof (struct GNUNET_PeerIdentity)));
678 /* session == 0, remove full address */
679 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
680 (aa->addr_len == info->addr_len) &&
681 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Deleting address for peer `%s': `%s' %u\n",
686 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
688 if (GNUNET_YES == destroy_address (aa))
689 recalculate_assigned_bw ();
692 /* session != 0, just remove session */
693 if (aa->session_id != info->session_id)
694 return GNUNET_OK; /* irrelevant */
695 if (aa->session_id != 0)
696 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Deleting session for peer `%s': `%s' %u\n",
700 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
703 if (GNUNET_YES == aa->active)
705 aa->active = GNUNET_NO;
706 handle->active_addr_count--;
707 recalculate_assigned_bw ();
710 /* session == 0 and addrlen == 0 : destroy address */
711 if (aa->addr_len == 0)
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Deleting session and address for peer `%s': `%s' %u\n",
715 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
716 (void) destroy_address (aa);
720 /* session was set to 0, update address */
722 if (handle->ats_mode == MODE_MLP)
723 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
732 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
733 const char *plugin_name, const void *plugin_addr,
734 size_t plugin_addr_len, uint32_t session_id)
736 struct ATS_Address *aa;
737 struct ATS_Address *old;
739 if (GNUNET_NO == handle->running)
742 /* Get existing address */
743 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
744 session_id, NULL, 0);
747 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
748 GNUNET_i2s (peer), plugin_name, session_id);
753 GNUNET_break (0 < strlen (plugin_name));
754 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
756 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
757 &destroy_by_session_id, aa);
764 * Find a "good" address to use for a peer. If we already have an existing
765 * address, we stick to it. Otherwise, we pick by lowest distance and then
768 * @param cls the 'struct ATS_Address**' where we store the result
770 * @param value another 'struct ATS_Address*' to consider using
771 * @return GNUNET_OK (continue to iterate)
774 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
776 struct ATS_Address **ap = cls;
777 struct ATS_Address *aa = (struct ATS_Address *) value;
778 struct ATS_Address *ab = *ap;
779 struct GNUNET_TIME_Absolute now;
781 now = GNUNET_TIME_absolute_get();
783 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
785 /* This address is blocked for suggestion */
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Address %p blocked for suggestion for %llu ms \n",
789 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
793 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
794 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
799 /* FIXME this is a hack */
804 if ((0 == strcmp (ab->plugin, "tcp")) &&
805 (0 == strcmp (aa->plugin, "tcp")))
807 if ((0 != ab->addr_len) &&
810 /* saved address was an outbound address, but we have an inbound address */
814 if (0 == ab->addr_len)
816 /* saved address was an inbound address, so do not overwrite */
821 /* FIXME end of hack */
828 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
829 (ntohl (aa->assigned_bw_in.value__) > 0))
831 /* stick to existing connection */
835 if (ab->atsp_distance > aa->atsp_distance)
837 /* user shorter distance */
841 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
843 /* user lower latency */
853 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
854 const char *plugin_name, const void *plugin_addr,
855 size_t plugin_addr_len, uint32_t session_id, int in_use)
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
860 GNUNET_i2s (peer), in_use);
863 struct ATS_Address *old;
865 if (GNUNET_NO == handle->running)
866 return GNUNET_SYSERR;
868 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
871 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
872 "Trying to set unknown address `%s', %s %u %s \n",
874 plugin_name, session_id,
875 (GNUNET_NO == in_use) ? "NO" : "YES");
877 return GNUNET_SYSERR;
879 if (old->used == in_use)
882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
883 "Address in use called multiple times for peer `%s': %s -> %s \n",
885 (GNUNET_NO == old->used) ? "NO" : "YES",
886 (GNUNET_NO == in_use) ? "NO" : "YES");
887 return GNUNET_SYSERR;
891 /* Tell solver about update */
892 handle->s_update (handle->solver, handle->addresses, old);
899 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
901 struct ATS_Address *aa;
905 /* Get preferred address from MODE_MLP */
906 struct ATS_PreferedAddress * paddr = NULL;
907 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
909 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
910 /* FIXME use bw in value */
911 paddr->bandwidth_in = paddr->bandwidth_out;
912 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
919 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
922 if (aa->active == GNUNET_NO)
924 aa->active = GNUNET_YES;
925 handle->active_addr_count++;
926 send_bw_notification (aa);
930 /* just to be sure... */
931 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
932 aa->addr_len, aa->session_id,
933 aa->ats, aa->ats_count,
942 request_address_simple (const struct GNUNET_PeerIdentity *peer)
944 struct ATS_Address *aa;
947 /* Get address with: stick to current address, lower distance, lower latency */
948 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
949 &find_address_it, &aa);
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
953 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
958 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
960 if (aa->active == GNUNET_NO)
962 aa->active = GNUNET_YES;
963 handle->active_addr_count++;
964 if (handle->ats_mode == MODE_SIMPLISTIC)
966 recalculate_assigned_bw ();
971 /* just to be sure... */
972 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
973 aa->addr_len, aa->session_id,
974 aa->ats, aa->ats_count,
982 * Cancel address suggestions for a peer
984 * @param peer the respective peer
987 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
989 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
992 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
1002 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1008 * Add an address suggestions for a peer
1010 * @param peer the respective peer
1013 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
1015 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
1017 if (GNUNET_NO == handle->running)
1021 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
1022 return; /* already suggesting */
1026 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
1028 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
1030 if (handle->ats_mode == MODE_SIMPLISTIC)
1032 request_address_simple (peer);
1034 if (handle->ats_mode == MODE_MLP)
1036 request_address_mlp(peer);
1042 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1044 struct ATS_Address *aa = value;
1046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1047 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
1049 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
1050 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
1056 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
1058 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1066 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
1067 enum GNUNET_ATS_PreferenceKind kind,
1070 if (GNUNET_NO == handle->running)
1073 /* Tell solver about update */
1074 handle->s_pref (handle->solver, peer, kind, score);
1080 * Initialize address subsystem.
1082 * @param cfg configuration to use
1083 * @param stats the statistics handle to use
1085 struct GAS_Addresses_Handle *
1086 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1087 const struct GNUNET_STATISTICS_Handle *stats)
1089 struct GAS_Addresses_Handle *ah;
1090 char *quota_wan_in_str;
1091 char *quota_wan_out_str;
1095 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1097 handle->running = GNUNET_NO;
1099 /* Initialize the system with configuration values */
1100 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
1102 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
1103 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &ah->wan_quota_in)))
1104 ah->wan_quota_in = (UINT32_MAX) /10;
1106 GNUNET_free (quota_wan_in_str);
1107 quota_wan_in_str = NULL;
1110 ah->wan_quota_in = (UINT32_MAX) /10;
1112 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
1114 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
1115 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &ah->wan_quota_out)))
1116 ah->wan_quota_out = (UINT32_MAX) /10;
1118 GNUNET_free (quota_wan_out_str);
1119 quota_wan_out_str = NULL;
1122 ah->wan_quota_out = (UINT32_MAX) /10;
1124 /* Initialize the addresses database */
1125 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1126 GNUNET_assert (NULL != ah->addresses);
1128 /* Figure out configured solution method */
1129 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1132 ah->ats_mode = MODE_SIMPLISTIC;
1136 for (c = 0; c < strlen (mode_str); c++)
1137 mode_str[c] = toupper (mode_str[c]);
1138 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1140 ah->ats_mode = MODE_SIMPLISTIC;
1142 else if (0 == strcmp (mode_str, "MLP"))
1144 ah->ats_mode = MODE_MLP;
1146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1147 ah->ats_mode = MODE_SIMPLISTIC;
1152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1153 ah->ats_mode = MODE_SIMPLISTIC;
1155 GNUNET_free (mode_str);
1157 /* Start configured solution method */
1158 switch (ah->ats_mode)
1161 /* Init the MLP solver with default values */
1163 ah->ats_mode = MODE_MLP;
1164 ah->s_init = &GAS_mlp_init;
1165 ah->s_update = &GAS_mlp_address_update;
1166 ah->s_get = &GAS_mlp_get_preferred_address;
1167 ah->s_pref = &GAS_mlp_address_change_preference;
1168 ah->s_del = &GAS_mlp_address_delete;
1169 ah->s_done = &GAS_mlp_done;
1175 case MODE_SIMPLISTIC:
1176 /* Init the simplistic solver with default values */
1177 ah->ats_mode = MODE_SIMPLISTIC;
1178 ah->s_init = &GAS_simplistic_init;
1179 ah->s_update = &GAS_simplistic_address_update;
1180 ah->s_get = &GAS_simplistic_get_preferred_address;
1181 ah->s_pref = &GAS_simplistic_address_change_preference;
1182 ah->s_del = &GAS_simplistic_address_delete;
1183 ah->s_done = &GAS_simplistic_done;
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1191 GNUNET_assert (NULL != ah->s_init);
1192 GNUNET_assert (NULL != ah->s_update);
1193 GNUNET_assert (NULL != ah->s_get);
1194 GNUNET_assert (NULL != ah->s_pref);
1195 GNUNET_assert (NULL != ah->s_del);
1196 GNUNET_assert (NULL != ah->s_done);
1198 ah->solver = ah->s_init (cfg, stats);
1199 if (NULL == ah->solver)
1201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1206 /* up and running */
1207 ah->running = GNUNET_YES;
1213 * Free memory of address.
1216 * @param key peer identity (unused)
1217 * @param value the 'struct ATS_Address' to free
1218 * @return GNUNET_OK (continue to iterate)
1221 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1223 struct ATS_Address *aa = value;
1225 destroy_address (aa);
1231 GAS_addresses_destroy_all ()
1233 if (GNUNET_NO == handle->running)
1236 if (handle->addresses != NULL)
1237 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1238 GNUNET_assert (handle->active_addr_count == 0);
1243 * Shutdown address subsystem.
1246 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1248 struct GAS_Addresses_Suggestion_Requests *cur;
1250 GNUNET_assert (NULL != handle);
1251 GAS_addresses_destroy_all ();
1252 handle->running = GNUNET_NO;
1253 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1254 handle->addresses = NULL;
1255 while (NULL != (cur = handle->r_head))
1257 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1261 GNUNET_free (handle);
1262 /* Stop configured solution method */
1266 struct PeerIteratorContext
1268 GNUNET_ATS_Peer_Iterator it;
1270 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1275 const struct GNUNET_HashCode * key,
1278 struct PeerIteratorContext *ip_ctx = cls;
1279 struct GNUNET_PeerIdentity tmp;
1281 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1283 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1284 tmp.hashPubKey = (*key);
1285 ip_ctx->it (ip_ctx->it_cls, &tmp);
1292 * Return all peers currently known to ATS
1294 * @param p_it the iterator to call for every peer, callbach with id == NULL
1296 * @param p_it_cls the closure for the iterator
1299 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1301 struct PeerIteratorContext ip_ctx;
1306 GNUNET_assert (NULL != handle->addresses);
1308 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1312 ip_ctx.it_cls = p_it_cls;
1313 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1314 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1315 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1317 p_it (p_it_cls, NULL);
1320 struct PeerInfoIteratorContext
1322 GNUNET_ATS_PeerInfo_Iterator it;
1328 peerinfo_it (void *cls,
1329 const struct GNUNET_HashCode * key,
1332 struct PeerInfoIteratorContext *pi_ctx = cls;
1333 struct ATS_Address *addr = (struct ATS_Address *) value;
1334 struct GNUNET_ATS_Information *ats;
1337 if (NULL != pi_ctx->it)
1339 ats_count = assemble_ats_information (addr, &ats);
1341 pi_ctx->it (pi_ctx->it_cls,
1344 addr->addr, addr->addr_len,
1347 addr->assigned_bw_out,
1348 addr->assigned_bw_in);
1356 * Return all peers currently known to ATS
1358 * @param peer the respective peer
1359 * @param pi_it the iterator to call for every peer
1360 * @param pi_it_cls the closure for the iterator
1363 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1365 struct PeerInfoIteratorContext pi_ctx;
1366 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1367 GNUNET_assert (NULL != peer);
1368 GNUNET_assert (NULL != handle->addresses);
1370 return; /* does not make sense without callback */
1372 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1374 pi_ctx.it_cls = pi_it_cls;
1376 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1379 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1384 /* end of gnunet-service-ats_addresses.c */