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"
38 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
43 * Assign each peer an equal amount of bandwidth (bw)
45 * bw_per_peer = bw_total / #active addresses
50 * Use MLP solver to assign bandwidth
55 static struct GNUNET_CONTAINER_MultiHashMap *addresses;
58 static struct GAS_MLP_Handle *mlp;
61 static unsigned long long wan_quota_in;
63 static unsigned long long wan_quota_out;
65 static unsigned int active_addr_count;
73 assemble_ats_information (struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
75 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
76 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
79 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
80 ats[0].value = aa->atsp_utilization_out.value__;
81 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
82 ats[1].value = aa->atsp_utilization_in.value__;
83 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
84 ats[2].value = ntohl(aa->atsp_network_type);
85 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
86 ats[3].value = ntohl(aa->atsp_latency.rel_value);
87 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
88 ats[4].value = ntohl(aa->atsp_distance);
89 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
90 ats[5].value = ntohl (aa->atsp_cost_wan);
91 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
92 ats[6].value = ntohl (aa->atsp_cost_lan);
93 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
94 ats[7].value = ntohl (aa->atsp_cost_wlan);
99 send_bw_notification (struct ATS_Address *aa)
101 struct GNUNET_ATS_Information *ats;
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
105 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
106 ntohl (aa->assigned_bw_out.value__));
107 ats_count = assemble_ats_information (aa, &ats);
109 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
110 aa->addr_len, aa->session_id,
114 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
115 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
117 ats, ats_count, aa->assigned_bw_out,
123 * Update a bandwidth assignment for a peer. This trivial method currently
124 * simply assigns the same share to all active connections.
128 * @param value the 'struct ATS_Address'
129 * @return GNUNET_OK (continue to iterate)
132 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
134 struct ATS_Address *aa = value;
136 if (GNUNET_YES != aa->active)
138 GNUNET_assert (active_addr_count > 0);
142 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
143 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
145 send_bw_notification (aa);
152 * Some (significant) input changed, recalculate bandwidth assignment
156 recalculate_assigned_bw ()
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Recalculating bandwidth for all active connections\n");
160 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
162 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
165 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
169 * Free the given address
170 * @param addr address to destroy
173 free_address (struct ATS_Address *addr)
175 GNUNET_free_non_null (addr->ats);
176 GNUNET_free (addr->plugin);
181 * Create a ATS_address with the given information
183 * @param plugin_name plugin
184 * @param plugin_addr address
185 * @param plugin_addr_len address length
186 * @param session_id session
187 * @return the ATS_Address
189 static struct ATS_Address *
190 create_address (const struct GNUNET_PeerIdentity *peer,
191 const char *plugin_name,
192 const void *plugin_addr, size_t plugin_addr_len,
195 struct ATS_Address *aa = NULL;
197 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
199 aa->addr_len = plugin_addr_len;
201 memcpy (&aa[1], plugin_addr, plugin_addr_len);
202 aa->plugin = GNUNET_strdup (plugin_name);
203 aa->session_id = session_id;
209 * Destroy the given address.
211 * @param addr address to destroy
212 * @return GNUNET_YES if bandwidth allocations should be recalcualted
215 destroy_address (struct ATS_Address *addr)
220 GNUNET_assert (GNUNET_YES ==
221 GNUNET_CONTAINER_multihashmap_remove (addresses,
222 &addr->peer.hashPubKey,
227 GAS_mlp_address_delete (mlp, addresses, addr);
230 if (GNUNET_YES == addr->active)
233 addr->active = GNUNET_NO;
241 struct CompareAddressContext
243 const struct ATS_Address *search;
245 /* exact_address != NULL if address and session is equal */
246 struct ATS_Address *exact_address;
247 /* exact_address != NULL if address and session is 0 */
248 struct ATS_Address *base_address;
253 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
255 struct CompareAddressContext *cac = cls;
256 struct ATS_Address *aa = value;
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
260 aa->addr_len, aa->session_id,
261 cac->search->addr_len, cac->search->session_id);
263 /* Find an matching exact address:
266 * aa->addr_len == cac->search->addr_len
267 * aa->plugin == cac->search->plugin
268 * aa->addr == cac->search->addr
269 * aa->session == cac->search->session
271 * return as exact address
273 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
275 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
276 cac->exact_address = aa;
279 /* Find an matching base address:
283 * aa->session_id == 0
286 * aa->addr_len == cac->search->addr_len
287 * aa->plugin == cac->search->plugin
288 * aa->addr == cac->search->addr
290 * return as base address
292 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
294 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
295 cac->base_address = aa;
298 /* Find an matching exact address based on session:
302 * cac->search->addr_len == 0
305 * aa->plugin == cac->search->plugin
306 * aa->session_id == cac->search->session_id
308 * return as exact address
310 if (0 == cac->search->addr_len)
312 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
313 cac->exact_address = aa;
316 if (cac->exact_address == NULL)
317 return GNUNET_YES; /* Continue iteration to find exact address */
319 return GNUNET_NO; /* Stop iteration since we have an exact address */
324 * Find an existing equivalent address record.
325 * Compares by peer identity and network address OR by session ID
326 * (one of the two must match).
328 * @param peer peer to lookup addresses for
329 * @param addr existing address record
330 * @return existing address record, NULL for none
333 find_address (const struct GNUNET_PeerIdentity *peer,
334 const struct ATS_Address *addr)
336 struct CompareAddressContext cac;
338 cac.exact_address = NULL;
339 cac.base_address = NULL;
341 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
342 &compare_address_it, &cac);
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Found exact address: %s base address: %s\n",
347 (cac.exact_address != NULL) ? "YES" : "NO",
348 (cac.base_address != NULL) ? "YES" : "NO");
350 if (cac.exact_address == NULL)
351 return cac.base_address;
352 return cac.exact_address;
356 static struct ATS_Address *
357 lookup_address (const struct GNUNET_PeerIdentity *peer,
358 const char *plugin_name, const void *plugin_addr,
359 size_t plugin_addr_len, uint32_t session_id,
360 const struct GNUNET_ATS_Information *atsi,
363 struct ATS_Address *aa;
364 struct ATS_Address *old;
366 aa = create_address (peer,
368 plugin_addr, plugin_addr_len,
371 aa->mlp_information = NULL;
372 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
373 aa->ats_count = atsi_count;
374 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
376 /* Get existing address or address with session == 0 */
377 old = find_address (peer, aa);
383 else if (old->session_id != session_id)
393 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
395 struct CompareAddressContext *cac = cls;
396 struct ATS_Address *aa = value;
398 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
400 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
402 cac->exact_address = aa;
411 * Find an existing equivalent address record.
412 * Compares by peer identity and network address AND by session ID
413 * (one of the two must match).
415 * @param peer peer to lookup addresses for
416 * @param addr existing address record
417 * @return existing address record, NULL for none
419 static struct ATS_Address *
420 find_exact_address (const struct GNUNET_PeerIdentity *peer,
421 const struct ATS_Address *addr)
423 struct CompareAddressContext cac;
425 cac.exact_address = NULL;
427 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
428 &compare_address_session_it, &cac);
429 return cac.exact_address;
435 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
436 const char *plugin_name, const void *plugin_addr,
437 size_t plugin_addr_len, uint32_t session_id,
438 const struct GNUNET_ATS_Information *atsi,
441 struct ATS_Address *aa;
442 struct ATS_Address *old;
444 if (GNUNET_NO == running)
447 GNUNET_assert (NULL != addresses);
449 aa = create_address (peer,
451 plugin_addr, plugin_addr_len,
454 aa->mlp_information = NULL;
455 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
456 aa->ats_count = atsi_count;
457 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
459 /* Get existing address or address with session == 0 */
460 old = find_address (peer, aa);
463 /* We have a new address */
464 GNUNET_assert (GNUNET_OK ==
465 GNUNET_CONTAINER_multihashmap_put (addresses,
466 &peer->hashPubKey, aa,
467 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
469 GNUNET_i2s (peer), session_id, aa);
473 if (old->session_id == 0)
475 /* We have a base address with out an session, update this address */
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Updated existing address for peer `%s' %p with new session %u\n",
478 GNUNET_i2s (peer), old, session_id);
479 GNUNET_free_non_null (old->ats);
480 old->session_id = session_id;
484 old->ats_count = aa->ats_count;
485 GNUNET_free (aa->plugin);
490 /* This address and session is already existing */
491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
492 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
493 GNUNET_i2s (peer), plugin_name, session_id);
499 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
500 const char *plugin_name, const void *plugin_addr,
501 size_t plugin_addr_len, uint32_t session_id,
502 const struct GNUNET_ATS_Information *atsi,
505 struct ATS_Address *old;
508 if (GNUNET_NO == running)
511 GNUNET_assert (NULL != addresses);
513 /* Get existing address */
514 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
515 session_id, atsi, atsi_count);
518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
519 GNUNET_i2s (peer), plugin_name, session_id);
524 for (i = 0; i < atsi_count; i++)
525 switch (ntohl (atsi[i].type))
527 case GNUNET_ATS_UTILIZATION_UP:
528 old->atsp_utilization_out.value__ = atsi[i].value;
530 case GNUNET_ATS_UTILIZATION_DOWN:
531 old->atsp_utilization_in.value__ = atsi[i].value;
533 case GNUNET_ATS_QUALITY_NET_DELAY:
534 old->atsp_latency.rel_value = ntohl (atsi[i].value);
536 case GNUNET_ATS_QUALITY_NET_DISTANCE:
537 old->atsp_distance = ntohl (atsi[i].value);
539 case GNUNET_ATS_COST_WAN:
540 old->atsp_cost_wan = ntohl (atsi[i].value);
542 case GNUNET_ATS_COST_LAN:
543 old->atsp_cost_lan = ntohl (atsi[i].value);
545 case GNUNET_ATS_COST_WLAN:
546 old->atsp_cost_wlan = ntohl (atsi[i].value);
548 case GNUNET_ATS_NETWORK_TYPE:
549 old->atsp_network_type = ntohl (atsi[i].value);
553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
554 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
560 GAS_mlp_address_update (mlp, addresses, old);
568 * If session != 0, just the session is deleted, the address itself still exists
569 * If session == 0, remove full address
570 * If session == 0 and addrlen == 0, destroy inbound address
574 * @param value the 'struct ATS_Address'
575 * @return GNUNET_OK (continue to iterate)
578 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
580 const struct ATS_Address *info = cls;
581 struct ATS_Address *aa = value;
584 memcmp (&aa->peer, &info->peer,
585 sizeof (struct GNUNET_PeerIdentity)));
586 /* session == 0, remove full address */
587 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
588 (aa->addr_len == info->addr_len) &&
589 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Deleting address for peer `%s': `%s' %u\n",
594 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
596 if (GNUNET_YES == destroy_address (aa))
597 recalculate_assigned_bw ();
600 /* session != 0, just remove session */
601 if (aa->session_id != info->session_id)
602 return GNUNET_OK; /* irrelevant */
603 if (aa->session_id != 0)
604 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Deleting session for peer `%s': `%s' %u\n",
608 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
611 if (GNUNET_YES == aa->active)
613 aa->active = GNUNET_NO;
615 recalculate_assigned_bw ();
618 /* session == 0 and addrlen == 0 : destroy address */
619 if (aa->addr_len == 0)
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Deleting session and address for peer `%s': `%s' %u\n",
623 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
624 (void) destroy_address (aa);
628 /* session was set to 0, update address */
631 GAS_mlp_address_update (mlp, addresses, aa);
640 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
641 const char *plugin_name, const void *plugin_addr,
642 size_t plugin_addr_len, uint32_t session_id)
644 struct ATS_Address *aa;
645 struct ATS_Address *old;
647 if (GNUNET_NO == running)
650 /* Get existing address */
651 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
652 session_id, NULL, 0);
655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
656 GNUNET_i2s (peer), plugin_name, session_id);
661 GNUNET_break (0 < strlen (plugin_name));
662 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
664 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
665 &destroy_by_session_id, aa);
672 * Find a "good" address to use for a peer. If we already have an existing
673 * address, we stick to it. Otherwise, we pick by lowest distance and then
676 * @param cls the 'struct ATS_Address**' where we store the result
678 * @param value another 'struct ATS_Address*' to consider using
679 * @return GNUNET_OK (continue to iterate)
682 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
684 struct ATS_Address **ap = cls;
685 struct ATS_Address *aa = (struct ATS_Address *) value;
686 struct ATS_Address *ab = *ap;
687 struct GNUNET_TIME_Absolute now;
689 now = GNUNET_TIME_absolute_get();
691 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
693 /* This address is blocked for suggestion */
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Address %p blocked for suggestion for %llu ms \n",
697 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
701 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
702 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
707 /* FIXME this is a hack */
712 if ((0 == strcmp (ab->plugin, "tcp")) &&
713 (0 == strcmp (aa->plugin, "tcp")))
715 if ((0 != ab->addr_len) &&
718 /* saved address was an outbound address, but we have an inbound address */
722 if (0 == ab->addr_len)
724 /* saved address was an inbound address, so do not overwrite */
729 /* FIXME end of hack */
736 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
737 (ntohl (aa->assigned_bw_in.value__) > 0))
739 /* stick to existing connection */
743 if (ab->atsp_distance > aa->atsp_distance)
745 /* user shorter distance */
749 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
751 /* user lower latency */
761 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
762 const char *plugin_name, const void *plugin_addr,
763 size_t plugin_addr_len, uint32_t session_id, int in_use)
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
768 GNUNET_i2s (peer), in_use);
771 struct ATS_Address *old;
773 if (GNUNET_NO == running)
774 return GNUNET_SYSERR;
776 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
780 "Trying to set unknown address `%s', %s %u %s \n",
782 plugin_name, session_id,
783 (GNUNET_NO == in_use) ? "NO" : "YES");
785 return GNUNET_SYSERR;
787 if (old->used == in_use)
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 "Address in use called multiple times for peer `%s': %s -> %s \n",
793 (GNUNET_NO == old->used) ? "NO" : "YES",
794 (GNUNET_NO == in_use) ? "NO" : "YES");
795 return GNUNET_SYSERR;
800 GAS_mlp_address_update (mlp, addresses, old);
807 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
809 struct ATS_Address *aa;
813 /* Get preferred address from MLP */
814 struct ATS_PreferedAddress * paddr = NULL;
815 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
817 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
818 /* FIXME use bw in value */
819 paddr->bandwidth_in = paddr->bandwidth_out;
820 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
827 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
830 if (aa->active == GNUNET_NO)
832 aa->active = GNUNET_YES;
835 send_bw_notification (aa);
839 /* just to be sure... */
840 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
841 aa->addr_len, aa->session_id,
842 aa->ats, aa->ats_count,
851 request_address_simple (const struct GNUNET_PeerIdentity *peer)
853 struct ATS_Address *aa;
856 /* Get address with: stick to current address, lower distance, lower latency */
857 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
858 &find_address_it, &aa);
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
862 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
867 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
869 if (aa->active == GNUNET_NO)
871 aa->active = GNUNET_YES;
873 if (ats_mode == SIMPLE)
875 recalculate_assigned_bw ();
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,
891 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
893 if (GNUNET_NO == running)
896 if (ats_mode == SIMPLE)
898 request_address_simple (peer);
902 request_address_mlp(peer);
908 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
910 struct ATS_Address *aa = value;
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
915 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
916 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
922 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
924 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
932 // FIXME: this function should likely end up in the LP-subsystem and
933 // not with 'addresses' in the future...
935 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
936 enum GNUNET_ATS_PreferenceKind kind,
939 if (GNUNET_NO == running)
943 GAS_mlp_address_change_preference (mlp, peer, kind, score);
950 * Initialize address subsystem.
952 * @param cfg configuration to use
953 * @param stats the statistics handle to use
956 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
957 const struct GNUNET_STATISTICS_Handle *stats)
961 char *quota_wan_in_str;
962 char *quota_wan_out_str;
966 addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
967 GNUNET_assert (NULL != addresses);
969 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
971 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
972 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
973 wan_quota_in = (UINT32_MAX) /10;
975 GNUNET_free (quota_wan_in_str);
976 quota_wan_in_str = NULL;
980 wan_quota_in = (UINT32_MAX) /10;
983 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
985 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
986 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
987 wan_quota_out = (UINT32_MAX) /10;
989 GNUNET_free (quota_wan_out_str);
990 quota_wan_out_str = NULL;
994 wan_quota_out = (UINT32_MAX) /10;
997 mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
1005 /* Init the MLP solver with default values */
1006 mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
1009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
1010 GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
1015 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
1020 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1026 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1029 /* No configuration value */
1031 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
1038 running = GNUNET_YES;
1043 * Free memory of address.
1046 * @param key peer identity (unused)
1047 * @param value the 'struct ATS_Address' to free
1048 * @return GNUNET_OK (continue to iterate)
1051 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1053 struct ATS_Address *aa = value;
1055 destroy_address (aa);
1061 GAS_addresses_destroy_all ()
1063 if (GNUNET_NO == running)
1066 if (addresses != NULL)
1067 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
1068 GNUNET_assert (active_addr_count == 0);
1073 * Shutdown address subsystem.
1076 GAS_addresses_done ()
1078 GAS_addresses_destroy_all ();
1079 running = GNUNET_NO;
1080 GNUNET_CONTAINER_multihashmap_destroy (addresses);
1083 if (ats_mode == MLP)
1090 struct PeerIteratorContext
1092 GNUNET_ATS_Peer_Iterator it;
1094 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1099 const struct GNUNET_HashCode * key,
1102 struct PeerIteratorContext *ip_ctx = cls;
1103 struct GNUNET_PeerIdentity tmp;
1105 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1107 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1108 tmp.hashPubKey = (*key);
1109 ip_ctx->it (ip_ctx->it_cls, &tmp);
1116 * Return all peers currently known to ATS
1118 * @param p_it the iterator to call for every peer, callbach with id == NULL
1120 * @param p_it_cls the closure for the iterator
1123 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1125 struct PeerIteratorContext ip_ctx;
1130 GNUNET_assert (NULL != addresses);
1132 size = GNUNET_CONTAINER_multihashmap_size(addresses);
1136 ip_ctx.it_cls = p_it_cls;
1137 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1138 GNUNET_CONTAINER_multihashmap_iterate (addresses, &peer_it, &ip_ctx);
1139 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1141 p_it (p_it_cls, NULL);
1144 struct PeerInfoIteratorContext
1146 GNUNET_ATS_PeerInfo_Iterator it;
1152 peerinfo_it (void *cls,
1153 const struct GNUNET_HashCode * key,
1156 struct PeerInfoIteratorContext *pi_ctx = cls;
1157 struct ATS_Address *addr = (struct ATS_Address *) value;
1158 struct GNUNET_ATS_Information *ats;
1161 if (NULL != pi_ctx->it)
1163 ats_count = assemble_ats_information (addr, &ats);
1165 pi_ctx->it (pi_ctx->it_cls,
1168 addr->addr, addr->addr_len,
1171 addr->assigned_bw_out,
1172 addr->assigned_bw_in);
1180 * Return all peers currently known to ATS
1182 * @param peer the respective peer
1183 * @param pi_it the iterator to call for every peer
1184 * @param pi_it_cls the closure for the iterator
1187 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1189 struct PeerInfoIteratorContext pi_ctx;
1190 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1191 GNUNET_assert (NULL != peer);
1192 GNUNET_assert (NULL != addresses);
1194 return; /* does not make sense without callback */
1196 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1198 pi_ctx.it_cls = pi_it_cls;
1200 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1203 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1208 /* end of gnunet-service-ats_addresses.c */