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_Handle
71 * A multihashmap to store all addresses
73 struct GNUNET_CONTAINER_MultiHashMap *addresses;
76 * Configure WAN quota in
78 unsigned long long wan_quota_in;
81 * Configure WAN quota out
83 unsigned long long wan_quota_out;
86 * Number of active addresses
88 unsigned int active_addr_count;
91 * Is ATS addresses running
96 * Configured ATS solver
105 /* Solver functions */
110 GAS_solver_init s_init;
113 * Update address in solver
115 GAS_solver_address_update s_update;
118 * Get address from solver
120 GAS_solver_get_preferred_address s_get;
123 * Delete address in solver
125 GAS_solver_address_delete s_del;
128 * Change preference for quality in solver
130 GAS_solver_address_change_preference s_pref;
135 GAS_solver_done s_done;
142 struct GAS_Addresses_Handle *handle;
146 assemble_ats_information (struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
148 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
149 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
152 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
153 ats[0].value = aa->atsp_utilization_out.value__;
154 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
155 ats[1].value = aa->atsp_utilization_in.value__;
156 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
157 ats[2].value = ntohl(aa->atsp_network_type);
158 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
159 ats[3].value = ntohl(aa->atsp_latency.rel_value);
160 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
161 ats[4].value = ntohl(aa->atsp_distance);
162 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
163 ats[5].value = ntohl (aa->atsp_cost_wan);
164 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
165 ats[6].value = ntohl (aa->atsp_cost_lan);
166 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
167 ats[7].value = ntohl (aa->atsp_cost_wlan);
172 send_bw_notification (struct ATS_Address *aa)
174 struct GNUNET_ATS_Information *ats;
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
178 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
179 ntohl (aa->assigned_bw_out.value__));
180 ats_count = assemble_ats_information (aa, &ats);
182 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
183 aa->addr_len, aa->session_id,
187 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
188 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
190 ats, ats_count, aa->assigned_bw_out,
196 * Update a bandwidth assignment for a peer. This trivial method currently
197 * simply assigns the same share to all active connections.
201 * @param value the 'struct ATS_Address'
202 * @return GNUNET_OK (continue to iterate)
205 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
207 struct ATS_Address *aa = value;
209 if (GNUNET_YES != aa->active)
211 GNUNET_assert (handle->active_addr_count > 0);
215 aa->assigned_bw_in.value__ = htonl (handle->wan_quota_in / handle->active_addr_count);
216 aa->assigned_bw_out.value__ = htonl (handle->wan_quota_out / handle->active_addr_count);
218 send_bw_notification (aa);
225 * Some (significant) input changed, recalculate bandwidth assignment
229 recalculate_assigned_bw ()
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Recalculating bandwidth for all active connections\n");
233 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
235 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", handle->active_addr_count,
238 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &update_bw_simple_it, NULL);
242 * Free the given address
243 * @param addr address to destroy
246 free_address (struct ATS_Address *addr)
248 GNUNET_free_non_null (addr->ats);
249 GNUNET_free (addr->plugin);
254 * Create a ATS_address with the given information
256 * @param plugin_name plugin
257 * @param plugin_addr address
258 * @param plugin_addr_len address length
259 * @param session_id session
260 * @return the ATS_Address
262 static struct ATS_Address *
263 create_address (const struct GNUNET_PeerIdentity *peer,
264 const char *plugin_name,
265 const void *plugin_addr, size_t plugin_addr_len,
268 struct ATS_Address *aa = NULL;
270 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
272 aa->addr_len = plugin_addr_len;
274 memcpy (&aa[1], plugin_addr, plugin_addr_len);
275 aa->plugin = GNUNET_strdup (plugin_name);
276 aa->session_id = session_id;
282 * Destroy the given address.
284 * @param addr address to destroy
285 * @return GNUNET_YES if bandwidth allocations should be recalcualted
288 destroy_address (struct ATS_Address *addr)
293 GNUNET_assert (GNUNET_YES ==
294 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
295 &addr->peer.hashPubKey,
299 if (handle->ats_mode == MODE_MLP)
300 GAS_mlp_address_delete (handle->solver, handle->addresses, addr);
303 if (GNUNET_YES == addr->active)
305 handle->active_addr_count--;
306 addr->active = GNUNET_NO;
314 struct CompareAddressContext
316 const struct ATS_Address *search;
318 /* exact_address != NULL if address and session is equal */
319 struct ATS_Address *exact_address;
320 /* exact_address != NULL if address and session is 0 */
321 struct ATS_Address *base_address;
326 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
328 struct CompareAddressContext *cac = cls;
329 struct ATS_Address *aa = value;
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
333 aa->addr_len, aa->session_id,
334 cac->search->addr_len, cac->search->session_id);
336 /* Find an matching exact address:
339 * aa->addr_len == cac->search->addr_len
340 * aa->plugin == cac->search->plugin
341 * aa->addr == cac->search->addr
342 * aa->session == cac->search->session
344 * return as exact address
346 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
348 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
349 cac->exact_address = aa;
352 /* Find an matching base address:
356 * aa->session_id == 0
359 * aa->addr_len == cac->search->addr_len
360 * aa->plugin == cac->search->plugin
361 * aa->addr == cac->search->addr
363 * return as base address
365 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
367 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
368 cac->base_address = aa;
371 /* Find an matching exact address based on session:
375 * cac->search->addr_len == 0
378 * aa->plugin == cac->search->plugin
379 * aa->session_id == cac->search->session_id
381 * return as exact address
383 if (0 == cac->search->addr_len)
385 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
386 cac->exact_address = aa;
389 if (cac->exact_address == NULL)
390 return GNUNET_YES; /* Continue iteration to find exact address */
392 return GNUNET_NO; /* Stop iteration since we have an exact address */
397 * Find an existing equivalent address record.
398 * Compares by peer identity and network address OR by session ID
399 * (one of the two must match).
401 * @param peer peer to lookup addresses for
402 * @param addr existing address record
403 * @return existing address record, NULL for none
406 find_address (const struct GNUNET_PeerIdentity *peer,
407 const struct ATS_Address *addr)
409 struct CompareAddressContext cac;
411 cac.exact_address = NULL;
412 cac.base_address = NULL;
414 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
415 &compare_address_it, &cac);
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Found exact address: %s base address: %s\n",
420 (cac.exact_address != NULL) ? "YES" : "NO",
421 (cac.base_address != NULL) ? "YES" : "NO");
423 if (cac.exact_address == NULL)
424 return cac.base_address;
425 return cac.exact_address;
429 static struct ATS_Address *
430 lookup_address (const struct GNUNET_PeerIdentity *peer,
431 const char *plugin_name, const void *plugin_addr,
432 size_t plugin_addr_len, uint32_t session_id,
433 const struct GNUNET_ATS_Information *atsi,
436 struct ATS_Address *aa;
437 struct ATS_Address *old;
439 aa = create_address (peer,
441 plugin_addr, plugin_addr_len,
444 aa->mlp_information = NULL;
445 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
446 aa->ats_count = atsi_count;
447 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
449 /* Get existing address or address with session == 0 */
450 old = find_address (peer, aa);
456 else if (old->session_id != session_id)
466 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
468 struct CompareAddressContext *cac = cls;
469 struct ATS_Address *aa = value;
471 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
473 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
475 cac->exact_address = aa;
484 * Find an existing equivalent address record.
485 * Compares by peer identity and network address AND by session ID
486 * (one of the two must match).
488 * @param peer peer to lookup addresses for
489 * @param addr existing address record
490 * @return existing address record, NULL for none
492 static struct ATS_Address *
493 find_exact_address (const struct GNUNET_PeerIdentity *peer,
494 const struct ATS_Address *addr)
496 struct CompareAddressContext cac;
498 cac.exact_address = NULL;
500 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
501 &compare_address_session_it, &cac);
502 return cac.exact_address;
507 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
508 const char *plugin_name, const void *plugin_addr,
509 size_t plugin_addr_len, uint32_t session_id,
510 const struct GNUNET_ATS_Information *atsi,
513 struct ATS_Address *aa;
514 struct ATS_Address *old;
516 if (GNUNET_NO == handle->running)
519 GNUNET_assert (NULL != handle->addresses);
521 aa = create_address (peer,
523 plugin_addr, plugin_addr_len,
526 aa->mlp_information = NULL;
527 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
528 aa->ats_count = atsi_count;
529 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
531 /* Get existing address or address with session == 0 */
532 old = find_address (peer, aa);
535 /* We have a new address */
536 GNUNET_assert (GNUNET_OK ==
537 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
538 &peer->hashPubKey, aa,
539 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
541 GNUNET_i2s (peer), session_id, aa);
545 if (old->session_id == 0)
547 /* We have a base address with out an session, update this address */
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Updated existing address for peer `%s' %p with new session %u\n",
550 GNUNET_i2s (peer), old, session_id);
551 GNUNET_free_non_null (old->ats);
552 old->session_id = session_id;
556 old->ats_count = aa->ats_count;
557 GNUNET_free (aa->plugin);
562 /* This address and session is already existing */
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
565 GNUNET_i2s (peer), plugin_name, session_id);
571 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
572 const char *plugin_name, const void *plugin_addr,
573 size_t plugin_addr_len, uint32_t session_id,
574 const struct GNUNET_ATS_Information *atsi,
577 struct ATS_Address *old;
580 if (GNUNET_NO == handle->running)
583 GNUNET_assert (NULL != handle->addresses);
585 /* Get existing address */
586 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
587 session_id, atsi, atsi_count);
590 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
591 GNUNET_i2s (peer), plugin_name, session_id);
596 for (i = 0; i < atsi_count; i++)
597 switch (ntohl (atsi[i].type))
599 case GNUNET_ATS_UTILIZATION_UP:
600 old->atsp_utilization_out.value__ = atsi[i].value;
602 case GNUNET_ATS_UTILIZATION_DOWN:
603 old->atsp_utilization_in.value__ = atsi[i].value;
605 case GNUNET_ATS_QUALITY_NET_DELAY:
606 old->atsp_latency.rel_value = ntohl (atsi[i].value);
608 case GNUNET_ATS_QUALITY_NET_DISTANCE:
609 old->atsp_distance = ntohl (atsi[i].value);
611 case GNUNET_ATS_COST_WAN:
612 old->atsp_cost_wan = ntohl (atsi[i].value);
614 case GNUNET_ATS_COST_LAN:
615 old->atsp_cost_lan = ntohl (atsi[i].value);
617 case GNUNET_ATS_COST_WLAN:
618 old->atsp_cost_wlan = ntohl (atsi[i].value);
620 case GNUNET_ATS_NETWORK_TYPE:
621 old->atsp_network_type = ntohl (atsi[i].value);
625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
626 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
631 /* Tell solver about update */
632 handle->s_update (handle->solver, handle->addresses, old);
639 * If session != 0, just the session is deleted, the address itself still exists
640 * If session == 0, remove full address
641 * If session == 0 and addrlen == 0, destroy inbound address
645 * @param value the 'struct ATS_Address'
646 * @return GNUNET_OK (continue to iterate)
649 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
651 const struct ATS_Address *info = cls;
652 struct ATS_Address *aa = value;
655 memcmp (&aa->peer, &info->peer,
656 sizeof (struct GNUNET_PeerIdentity)));
657 /* session == 0, remove full address */
658 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
659 (aa->addr_len == info->addr_len) &&
660 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Deleting address for peer `%s': `%s' %u\n",
665 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
667 if (GNUNET_YES == destroy_address (aa))
668 recalculate_assigned_bw ();
671 /* session != 0, just remove session */
672 if (aa->session_id != info->session_id)
673 return GNUNET_OK; /* irrelevant */
674 if (aa->session_id != 0)
675 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "Deleting session for peer `%s': `%s' %u\n",
679 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
682 if (GNUNET_YES == aa->active)
684 aa->active = GNUNET_NO;
685 handle->active_addr_count--;
686 recalculate_assigned_bw ();
689 /* session == 0 and addrlen == 0 : destroy address */
690 if (aa->addr_len == 0)
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Deleting session and address for peer `%s': `%s' %u\n",
694 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
695 (void) destroy_address (aa);
699 /* session was set to 0, update address */
701 if (handle->ats_mode == MODE_MLP)
702 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
711 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
712 const char *plugin_name, const void *plugin_addr,
713 size_t plugin_addr_len, uint32_t session_id)
715 struct ATS_Address *aa;
716 struct ATS_Address *old;
718 if (GNUNET_NO == handle->running)
721 /* Get existing address */
722 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
723 session_id, NULL, 0);
726 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
727 GNUNET_i2s (peer), plugin_name, session_id);
732 GNUNET_break (0 < strlen (plugin_name));
733 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
735 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
736 &destroy_by_session_id, aa);
743 * Find a "good" address to use for a peer. If we already have an existing
744 * address, we stick to it. Otherwise, we pick by lowest distance and then
747 * @param cls the 'struct ATS_Address**' where we store the result
749 * @param value another 'struct ATS_Address*' to consider using
750 * @return GNUNET_OK (continue to iterate)
753 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
755 struct ATS_Address **ap = cls;
756 struct ATS_Address *aa = (struct ATS_Address *) value;
757 struct ATS_Address *ab = *ap;
758 struct GNUNET_TIME_Absolute now;
760 now = GNUNET_TIME_absolute_get();
762 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
764 /* This address is blocked for suggestion */
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Address %p blocked for suggestion for %llu ms \n",
768 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
772 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
773 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
778 /* FIXME this is a hack */
783 if ((0 == strcmp (ab->plugin, "tcp")) &&
784 (0 == strcmp (aa->plugin, "tcp")))
786 if ((0 != ab->addr_len) &&
789 /* saved address was an outbound address, but we have an inbound address */
793 if (0 == ab->addr_len)
795 /* saved address was an inbound address, so do not overwrite */
800 /* FIXME end of hack */
807 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
808 (ntohl (aa->assigned_bw_in.value__) > 0))
810 /* stick to existing connection */
814 if (ab->atsp_distance > aa->atsp_distance)
816 /* user shorter distance */
820 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
822 /* user lower latency */
832 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
833 const char *plugin_name, const void *plugin_addr,
834 size_t plugin_addr_len, uint32_t session_id, int in_use)
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
839 GNUNET_i2s (peer), in_use);
842 struct ATS_Address *old;
844 if (GNUNET_NO == handle->running)
845 return GNUNET_SYSERR;
847 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
850 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
851 "Trying to set unknown address `%s', %s %u %s \n",
853 plugin_name, session_id,
854 (GNUNET_NO == in_use) ? "NO" : "YES");
856 return GNUNET_SYSERR;
858 if (old->used == in_use)
861 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
862 "Address in use called multiple times for peer `%s': %s -> %s \n",
864 (GNUNET_NO == old->used) ? "NO" : "YES",
865 (GNUNET_NO == in_use) ? "NO" : "YES");
866 return GNUNET_SYSERR;
870 /* Tell solver about update */
871 handle->s_update (handle->solver, handle->addresses, old);
878 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
880 struct ATS_Address *aa;
884 /* Get preferred address from MODE_MLP */
885 struct ATS_PreferedAddress * paddr = NULL;
886 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
888 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
889 /* FIXME use bw in value */
890 paddr->bandwidth_in = paddr->bandwidth_out;
891 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
898 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
901 if (aa->active == GNUNET_NO)
903 aa->active = GNUNET_YES;
904 handle->active_addr_count++;
905 send_bw_notification (aa);
909 /* just to be sure... */
910 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
911 aa->addr_len, aa->session_id,
912 aa->ats, aa->ats_count,
921 request_address_simple (const struct GNUNET_PeerIdentity *peer)
923 struct ATS_Address *aa;
926 /* Get address with: stick to current address, lower distance, lower latency */
927 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
928 &find_address_it, &aa);
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
932 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
937 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
939 if (aa->active == GNUNET_NO)
941 aa->active = GNUNET_YES;
942 handle->active_addr_count++;
943 if (handle->ats_mode == MODE_SIMPLISTIC)
945 recalculate_assigned_bw ();
950 /* just to be sure... */
951 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
952 aa->addr_len, aa->session_id,
953 aa->ats, aa->ats_count,
961 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
963 if (GNUNET_NO == handle->running)
966 if (handle->ats_mode == MODE_SIMPLISTIC)
968 request_address_simple (peer);
970 if (handle->ats_mode == MODE_MLP)
972 request_address_mlp(peer);
978 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
980 struct ATS_Address *aa = value;
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
985 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
986 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
992 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
994 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1002 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
1003 enum GNUNET_ATS_PreferenceKind kind,
1006 if (GNUNET_NO == handle->running)
1009 /* Tell solver about update */
1010 handle->s_pref (handle->solver, peer, kind, score);
1016 * Initialize address subsystem.
1018 * @param cfg configuration to use
1019 * @param stats the statistics handle to use
1021 struct GAS_Addresses_Handle *
1022 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1023 const struct GNUNET_STATISTICS_Handle *stats)
1025 struct GAS_Addresses_Handle *ah;
1026 char *quota_wan_in_str;
1027 char *quota_wan_out_str;
1031 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1033 handle->running = GNUNET_NO;
1035 /* Initialize the system with configuration values */
1036 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
1038 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
1039 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &ah->wan_quota_in)))
1040 ah->wan_quota_in = (UINT32_MAX) /10;
1042 GNUNET_free (quota_wan_in_str);
1043 quota_wan_in_str = NULL;
1046 ah->wan_quota_in = (UINT32_MAX) /10;
1048 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
1050 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
1051 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &ah->wan_quota_out)))
1052 ah->wan_quota_out = (UINT32_MAX) /10;
1054 GNUNET_free (quota_wan_out_str);
1055 quota_wan_out_str = NULL;
1058 ah->wan_quota_out = (UINT32_MAX) /10;
1060 /* Initialize the addresses database */
1061 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1062 GNUNET_assert (NULL != ah->addresses);
1064 /* Figure out configured solution method */
1065 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1068 ah->ats_mode = MODE_SIMPLISTIC;
1072 for (c = 0; c < strlen (mode_str); c++)
1073 mode_str[c] = toupper (mode_str[c]);
1074 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1076 ah->ats_mode = MODE_SIMPLISTIC;
1078 else if (0 == strcmp (mode_str, "MLP"))
1080 ah->ats_mode = MODE_MLP;
1082 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1083 ah->ats_mode = MODE_SIMPLISTIC;
1088 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1089 ah->ats_mode = MODE_SIMPLISTIC;
1091 GNUNET_free (mode_str);
1093 /* Start configured solution method */
1094 switch (ah->ats_mode)
1097 /* Init the MLP solver with default values */
1099 ah->ats_mode = MODE_MLP;
1100 ah->s_init = &GAS_mlp_init;
1101 ah->s_update = &GAS_mlp_address_update;
1102 ah->s_get = &GAS_mlp_get_preferred_address;
1103 ah->s_pref = &GAS_mlp_address_change_preference;
1104 ah->s_del = &GAS_mlp_address_delete;
1105 ah->s_done = &GAS_mlp_done;
1111 case MODE_SIMPLISTIC:
1112 /* Init the simplistic solver with default values */
1113 ah->ats_mode = MODE_SIMPLISTIC;
1114 ah->s_init = &GAS_simplistic_init;
1115 ah->s_update = &GAS_simplistic_address_update;
1116 ah->s_get = &GAS_simplistic_get_preferred_address;
1117 ah->s_pref = &GAS_simplistic_address_change_preference;
1118 ah->s_del = &GAS_simplistic_address_delete;
1119 ah->s_done = &GAS_simplistic_done;
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1127 GNUNET_assert (NULL != ah->s_init);
1128 GNUNET_assert (NULL != ah->s_update);
1129 GNUNET_assert (NULL != ah->s_get);
1130 GNUNET_assert (NULL != ah->s_pref);
1131 GNUNET_assert (NULL != ah->s_del);
1132 GNUNET_assert (NULL != ah->s_done);
1134 ah->solver = ah->s_init (cfg, stats);
1135 if (NULL == ah->solver)
1137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1142 /* up and running */
1143 ah->running = GNUNET_YES;
1149 * Free memory of address.
1152 * @param key peer identity (unused)
1153 * @param value the 'struct ATS_Address' to free
1154 * @return GNUNET_OK (continue to iterate)
1157 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1159 struct ATS_Address *aa = value;
1161 destroy_address (aa);
1167 GAS_addresses_destroy_all ()
1169 if (GNUNET_NO == handle->running)
1172 if (handle->addresses != NULL)
1173 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1174 GNUNET_assert (handle->active_addr_count == 0);
1179 * Shutdown address subsystem.
1182 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1184 GNUNET_assert (NULL != handle);
1186 GAS_addresses_destroy_all ();
1187 handle->running = GNUNET_NO;
1188 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1189 handle->addresses = NULL;
1190 GNUNET_free (handle);
1191 /* Stop configured solution method */
1195 struct PeerIteratorContext
1197 GNUNET_ATS_Peer_Iterator it;
1199 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1204 const struct GNUNET_HashCode * key,
1207 struct PeerIteratorContext *ip_ctx = cls;
1208 struct GNUNET_PeerIdentity tmp;
1210 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1212 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1213 tmp.hashPubKey = (*key);
1214 ip_ctx->it (ip_ctx->it_cls, &tmp);
1221 * Return all peers currently known to ATS
1223 * @param p_it the iterator to call for every peer, callbach with id == NULL
1225 * @param p_it_cls the closure for the iterator
1228 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1230 struct PeerIteratorContext ip_ctx;
1235 GNUNET_assert (NULL != handle->addresses);
1237 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1241 ip_ctx.it_cls = p_it_cls;
1242 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1243 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1244 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1246 p_it (p_it_cls, NULL);
1249 struct PeerInfoIteratorContext
1251 GNUNET_ATS_PeerInfo_Iterator it;
1257 peerinfo_it (void *cls,
1258 const struct GNUNET_HashCode * key,
1261 struct PeerInfoIteratorContext *pi_ctx = cls;
1262 struct ATS_Address *addr = (struct ATS_Address *) value;
1263 struct GNUNET_ATS_Information *ats;
1266 if (NULL != pi_ctx->it)
1268 ats_count = assemble_ats_information (addr, &ats);
1270 pi_ctx->it (pi_ctx->it_cls,
1273 addr->addr, addr->addr_len,
1276 addr->assigned_bw_out,
1277 addr->assigned_bw_in);
1285 * Return all peers currently known to ATS
1287 * @param peer the respective peer
1288 * @param pi_it the iterator to call for every peer
1289 * @param pi_it_cls the closure for the iterator
1292 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1294 struct PeerInfoIteratorContext pi_ctx;
1295 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1296 GNUNET_assert (NULL != peer);
1297 GNUNET_assert (NULL != handle->addresses);
1299 return; /* does not make sense without callback */
1301 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1303 pi_ctx.it_cls = pi_it_cls;
1305 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1308 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1313 /* end of gnunet-service-ats_addresses.c */