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 struct GAS_Addresses_Handle
68 struct GNUNET_CONTAINER_MultiHashMap *addresses;
70 unsigned long long wan_quota_in;
72 unsigned long long wan_quota_out;
74 unsigned int active_addr_count;
83 /* Solver functions */
84 GAS_solver_init s_init;
85 GAS_solver_address_update s_update;
86 GAS_solver_get_preferred_address s_get;
87 GAS_solver_address_delete s_del;
88 GAS_solver_address_change_preference s_pref;
89 GAS_solver_done s_done;
92 struct GAS_Addresses_Handle *handle;
95 assemble_ats_information (struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
97 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
98 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
101 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
102 ats[0].value = aa->atsp_utilization_out.value__;
103 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
104 ats[1].value = aa->atsp_utilization_in.value__;
105 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
106 ats[2].value = ntohl(aa->atsp_network_type);
107 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
108 ats[3].value = ntohl(aa->atsp_latency.rel_value);
109 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
110 ats[4].value = ntohl(aa->atsp_distance);
111 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
112 ats[5].value = ntohl (aa->atsp_cost_wan);
113 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
114 ats[6].value = ntohl (aa->atsp_cost_lan);
115 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
116 ats[7].value = ntohl (aa->atsp_cost_wlan);
121 send_bw_notification (struct ATS_Address *aa)
123 struct GNUNET_ATS_Information *ats;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New bandwidth for peer %s is %u/%u\n",
127 GNUNET_i2s (&aa->peer), ntohl (aa->assigned_bw_in.value__),
128 ntohl (aa->assigned_bw_out.value__));
129 ats_count = assemble_ats_information (aa, &ats);
131 GAS_scheduling_transmit_address_suggestion (&aa->peer, aa->plugin, aa->addr,
132 aa->addr_len, aa->session_id,
136 GAS_reservations_set_bandwidth (&aa->peer, aa->assigned_bw_in);
137 GAS_performance_notify_all_clients (&aa->peer, aa->plugin, aa->addr, aa->addr_len,
139 ats, ats_count, aa->assigned_bw_out,
145 * Update a bandwidth assignment for a peer. This trivial method currently
146 * simply assigns the same share to all active connections.
150 * @param value the 'struct ATS_Address'
151 * @return GNUNET_OK (continue to iterate)
154 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
156 struct ATS_Address *aa = value;
158 if (GNUNET_YES != aa->active)
160 GNUNET_assert (handle->active_addr_count > 0);
164 aa->assigned_bw_in.value__ = htonl (handle->wan_quota_in / handle->active_addr_count);
165 aa->assigned_bw_out.value__ = htonl (handle->wan_quota_out / handle->active_addr_count);
167 send_bw_notification (aa);
174 * Some (significant) input changed, recalculate bandwidth assignment
178 recalculate_assigned_bw ()
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Recalculating bandwidth for all active connections\n");
182 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
184 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", handle->active_addr_count,
187 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &update_bw_simple_it, NULL);
191 * Free the given address
192 * @param addr address to destroy
195 free_address (struct ATS_Address *addr)
197 GNUNET_free_non_null (addr->ats);
198 GNUNET_free (addr->plugin);
203 * Create a ATS_address with the given information
205 * @param plugin_name plugin
206 * @param plugin_addr address
207 * @param plugin_addr_len address length
208 * @param session_id session
209 * @return the ATS_Address
211 static struct ATS_Address *
212 create_address (const struct GNUNET_PeerIdentity *peer,
213 const char *plugin_name,
214 const void *plugin_addr, size_t plugin_addr_len,
217 struct ATS_Address *aa = NULL;
219 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
221 aa->addr_len = plugin_addr_len;
223 memcpy (&aa[1], plugin_addr, plugin_addr_len);
224 aa->plugin = GNUNET_strdup (plugin_name);
225 aa->session_id = session_id;
231 * Destroy the given address.
233 * @param addr address to destroy
234 * @return GNUNET_YES if bandwidth allocations should be recalcualted
237 destroy_address (struct ATS_Address *addr)
242 GNUNET_assert (GNUNET_YES ==
243 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
244 &addr->peer.hashPubKey,
248 if (handle->ats_mode == MODE_MLP)
249 GAS_mlp_address_delete (handle->solver, handle->addresses, addr);
252 if (GNUNET_YES == addr->active)
254 handle->active_addr_count--;
255 addr->active = GNUNET_NO;
263 struct CompareAddressContext
265 const struct ATS_Address *search;
267 /* exact_address != NULL if address and session is equal */
268 struct ATS_Address *exact_address;
269 /* exact_address != NULL if address and session is 0 */
270 struct ATS_Address *base_address;
275 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
277 struct CompareAddressContext *cac = cls;
278 struct ATS_Address *aa = value;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
282 aa->addr_len, aa->session_id,
283 cac->search->addr_len, cac->search->session_id);
285 /* Find an matching exact address:
288 * aa->addr_len == cac->search->addr_len
289 * aa->plugin == cac->search->plugin
290 * aa->addr == cac->search->addr
291 * aa->session == cac->search->session
293 * return as exact address
295 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
297 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
298 cac->exact_address = aa;
301 /* Find an matching base address:
305 * aa->session_id == 0
308 * aa->addr_len == cac->search->addr_len
309 * aa->plugin == cac->search->plugin
310 * aa->addr == cac->search->addr
312 * return as base address
314 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
316 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
317 cac->base_address = aa;
320 /* Find an matching exact address based on session:
324 * cac->search->addr_len == 0
327 * aa->plugin == cac->search->plugin
328 * aa->session_id == cac->search->session_id
330 * return as exact address
332 if (0 == cac->search->addr_len)
334 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
335 cac->exact_address = aa;
338 if (cac->exact_address == NULL)
339 return GNUNET_YES; /* Continue iteration to find exact address */
341 return GNUNET_NO; /* Stop iteration since we have an exact address */
346 * Find an existing equivalent address record.
347 * Compares by peer identity and network address OR by session ID
348 * (one of the two must match).
350 * @param peer peer to lookup addresses for
351 * @param addr existing address record
352 * @return existing address record, NULL for none
355 find_address (const struct GNUNET_PeerIdentity *peer,
356 const struct ATS_Address *addr)
358 struct CompareAddressContext cac;
360 cac.exact_address = NULL;
361 cac.base_address = NULL;
363 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
364 &compare_address_it, &cac);
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Found exact address: %s base address: %s\n",
369 (cac.exact_address != NULL) ? "YES" : "NO",
370 (cac.base_address != NULL) ? "YES" : "NO");
372 if (cac.exact_address == NULL)
373 return cac.base_address;
374 return cac.exact_address;
378 static struct ATS_Address *
379 lookup_address (const struct GNUNET_PeerIdentity *peer,
380 const char *plugin_name, const void *plugin_addr,
381 size_t plugin_addr_len, uint32_t session_id,
382 const struct GNUNET_ATS_Information *atsi,
385 struct ATS_Address *aa;
386 struct ATS_Address *old;
388 aa = create_address (peer,
390 plugin_addr, plugin_addr_len,
393 aa->mlp_information = NULL;
394 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
395 aa->ats_count = atsi_count;
396 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
398 /* Get existing address or address with session == 0 */
399 old = find_address (peer, aa);
405 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 (handle->addresses, &peer->hashPubKey,
449 &compare_address_session_it, &cac);
450 return cac.exact_address;
455 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
456 const char *plugin_name, const void *plugin_addr,
457 size_t plugin_addr_len, uint32_t session_id,
458 const struct GNUNET_ATS_Information *atsi,
461 struct ATS_Address *aa;
462 struct ATS_Address *old;
464 if (GNUNET_NO == handle->running)
467 GNUNET_assert (NULL != handle->addresses);
469 aa = create_address (peer,
471 plugin_addr, plugin_addr_len,
474 aa->mlp_information = NULL;
475 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
476 aa->ats_count = atsi_count;
477 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
479 /* Get existing address or address with session == 0 */
480 old = find_address (peer, aa);
483 /* We have a new address */
484 GNUNET_assert (GNUNET_OK ==
485 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
486 &peer->hashPubKey, aa,
487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
489 GNUNET_i2s (peer), session_id, aa);
493 if (old->session_id == 0)
495 /* We have a base address with out an session, update this address */
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Updated existing address for peer `%s' %p with new session %u\n",
498 GNUNET_i2s (peer), old, session_id);
499 GNUNET_free_non_null (old->ats);
500 old->session_id = session_id;
504 old->ats_count = aa->ats_count;
505 GNUNET_free (aa->plugin);
510 /* This address and session is already existing */
511 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
512 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
513 GNUNET_i2s (peer), plugin_name, session_id);
519 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
520 const char *plugin_name, const void *plugin_addr,
521 size_t plugin_addr_len, uint32_t session_id,
522 const struct GNUNET_ATS_Information *atsi,
525 struct ATS_Address *old;
528 if (GNUNET_NO == handle->running)
531 GNUNET_assert (NULL != handle->addresses);
533 /* Get existing address */
534 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
535 session_id, atsi, atsi_count);
538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
539 GNUNET_i2s (peer), plugin_name, session_id);
544 for (i = 0; i < atsi_count; i++)
545 switch (ntohl (atsi[i].type))
547 case GNUNET_ATS_UTILIZATION_UP:
548 old->atsp_utilization_out.value__ = atsi[i].value;
550 case GNUNET_ATS_UTILIZATION_DOWN:
551 old->atsp_utilization_in.value__ = atsi[i].value;
553 case GNUNET_ATS_QUALITY_NET_DELAY:
554 old->atsp_latency.rel_value = ntohl (atsi[i].value);
556 case GNUNET_ATS_QUALITY_NET_DISTANCE:
557 old->atsp_distance = ntohl (atsi[i].value);
559 case GNUNET_ATS_COST_WAN:
560 old->atsp_cost_wan = ntohl (atsi[i].value);
562 case GNUNET_ATS_COST_LAN:
563 old->atsp_cost_lan = ntohl (atsi[i].value);
565 case GNUNET_ATS_COST_WLAN:
566 old->atsp_cost_wlan = ntohl (atsi[i].value);
568 case GNUNET_ATS_NETWORK_TYPE:
569 old->atsp_network_type = ntohl (atsi[i].value);
573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
574 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
579 /* Tell solver about update */
580 handle->s_update (handle->solver, handle->addresses, old);
587 * If session != 0, just the session is deleted, the address itself still exists
588 * If session == 0, remove full address
589 * If session == 0 and addrlen == 0, destroy inbound address
593 * @param value the 'struct ATS_Address'
594 * @return GNUNET_OK (continue to iterate)
597 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
599 const struct ATS_Address *info = cls;
600 struct ATS_Address *aa = value;
603 memcmp (&aa->peer, &info->peer,
604 sizeof (struct GNUNET_PeerIdentity)));
605 /* session == 0, remove full address */
606 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
607 (aa->addr_len == info->addr_len) &&
608 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "Deleting address for peer `%s': `%s' %u\n",
613 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
615 if (GNUNET_YES == destroy_address (aa))
616 recalculate_assigned_bw ();
619 /* session != 0, just remove session */
620 if (aa->session_id != info->session_id)
621 return GNUNET_OK; /* irrelevant */
622 if (aa->session_id != 0)
623 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Deleting session for peer `%s': `%s' %u\n",
627 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
630 if (GNUNET_YES == aa->active)
632 aa->active = GNUNET_NO;
633 handle->active_addr_count--;
634 recalculate_assigned_bw ();
637 /* session == 0 and addrlen == 0 : destroy address */
638 if (aa->addr_len == 0)
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Deleting session and address for peer `%s': `%s' %u\n",
642 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
643 (void) destroy_address (aa);
647 /* session was set to 0, update address */
649 if (handle->ats_mode == MODE_MLP)
650 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
659 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
660 const char *plugin_name, const void *plugin_addr,
661 size_t plugin_addr_len, uint32_t session_id)
663 struct ATS_Address *aa;
664 struct ATS_Address *old;
666 if (GNUNET_NO == handle->running)
669 /* Get existing address */
670 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
671 session_id, NULL, 0);
674 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
675 GNUNET_i2s (peer), plugin_name, session_id);
680 GNUNET_break (0 < strlen (plugin_name));
681 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
683 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
684 &destroy_by_session_id, aa);
691 * Find a "good" address to use for a peer. If we already have an existing
692 * address, we stick to it. Otherwise, we pick by lowest distance and then
695 * @param cls the 'struct ATS_Address**' where we store the result
697 * @param value another 'struct ATS_Address*' to consider using
698 * @return GNUNET_OK (continue to iterate)
701 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
703 struct ATS_Address **ap = cls;
704 struct ATS_Address *aa = (struct ATS_Address *) value;
705 struct ATS_Address *ab = *ap;
706 struct GNUNET_TIME_Absolute now;
708 now = GNUNET_TIME_absolute_get();
710 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
712 /* This address is blocked for suggestion */
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Address %p blocked for suggestion for %llu ms \n",
716 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
720 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
721 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
726 /* FIXME this is a hack */
731 if ((0 == strcmp (ab->plugin, "tcp")) &&
732 (0 == strcmp (aa->plugin, "tcp")))
734 if ((0 != ab->addr_len) &&
737 /* saved address was an outbound address, but we have an inbound address */
741 if (0 == ab->addr_len)
743 /* saved address was an inbound address, so do not overwrite */
748 /* FIXME end of hack */
755 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
756 (ntohl (aa->assigned_bw_in.value__) > 0))
758 /* stick to existing connection */
762 if (ab->atsp_distance > aa->atsp_distance)
764 /* user shorter distance */
768 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
770 /* user lower latency */
780 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
781 const char *plugin_name, const void *plugin_addr,
782 size_t plugin_addr_len, uint32_t session_id, int in_use)
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
787 GNUNET_i2s (peer), in_use);
790 struct ATS_Address *old;
792 if (GNUNET_NO == handle->running)
793 return GNUNET_SYSERR;
795 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
799 "Trying to set unknown address `%s', %s %u %s \n",
801 plugin_name, session_id,
802 (GNUNET_NO == in_use) ? "NO" : "YES");
804 return GNUNET_SYSERR;
806 if (old->used == in_use)
809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
810 "Address in use called multiple times for peer `%s': %s -> %s \n",
812 (GNUNET_NO == old->used) ? "NO" : "YES",
813 (GNUNET_NO == in_use) ? "NO" : "YES");
814 return GNUNET_SYSERR;
818 /* Tell solver about update */
819 handle->s_update (handle->solver, handle->addresses, old);
826 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
828 struct ATS_Address *aa;
832 /* Get preferred address from MODE_MLP */
833 struct ATS_PreferedAddress * paddr = NULL;
834 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
836 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
837 /* FIXME use bw in value */
838 paddr->bandwidth_in = paddr->bandwidth_out;
839 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
846 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
849 if (aa->active == GNUNET_NO)
851 aa->active = GNUNET_YES;
852 handle->active_addr_count++;
853 send_bw_notification (aa);
857 /* just to be sure... */
858 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
859 aa->addr_len, aa->session_id,
860 aa->ats, aa->ats_count,
869 request_address_simple (const struct GNUNET_PeerIdentity *peer)
871 struct ATS_Address *aa;
874 /* Get address with: stick to current address, lower distance, lower latency */
875 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
876 &find_address_it, &aa);
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
880 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
885 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
887 if (aa->active == GNUNET_NO)
889 aa->active = GNUNET_YES;
890 handle->active_addr_count++;
891 if (handle->ats_mode == MODE_SIMPLISTIC)
893 recalculate_assigned_bw ();
898 /* just to be sure... */
899 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
900 aa->addr_len, aa->session_id,
901 aa->ats, aa->ats_count,
909 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
911 if (GNUNET_NO == handle->running)
914 if (handle->ats_mode == MODE_SIMPLISTIC)
916 request_address_simple (peer);
918 if (handle->ats_mode == MODE_MLP)
920 request_address_mlp(peer);
926 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
928 struct ATS_Address *aa = value;
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
931 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
933 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
934 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
940 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
942 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
950 // FIXME: this function should likely end up in the LP-subsystem and
951 // not with 'addresses' in the future...
953 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
954 enum GNUNET_ATS_PreferenceKind kind,
957 if (GNUNET_NO == handle->running)
960 /* Tell solver about update */
961 handle->s_pref (handle->solver, peer, kind, score);
967 * Initialize address subsystem.
969 * @param cfg configuration to use
970 * @param stats the statistics handle to use
972 struct GAS_Addresses_Handle *
973 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
974 const struct GNUNET_STATISTICS_Handle *stats)
976 struct GAS_Addresses_Handle *ah;
977 char *quota_wan_in_str;
978 char *quota_wan_out_str;
982 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
984 handle->running = GNUNET_NO;
986 /* Initialize the system with configuration values */
987 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
989 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
990 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &ah->wan_quota_in)))
991 ah->wan_quota_in = (UINT32_MAX) /10;
993 GNUNET_free (quota_wan_in_str);
994 quota_wan_in_str = NULL;
997 ah->wan_quota_in = (UINT32_MAX) /10;
999 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
1001 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
1002 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &ah->wan_quota_out)))
1003 ah->wan_quota_out = (UINT32_MAX) /10;
1005 GNUNET_free (quota_wan_out_str);
1006 quota_wan_out_str = NULL;
1009 ah->wan_quota_out = (UINT32_MAX) /10;
1011 /* Initialize the addresses database */
1012 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1013 GNUNET_assert (NULL != ah->addresses);
1015 /* Figure out configured solution method */
1016 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1018 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1019 ah->ats_mode = MODE_SIMPLISTIC;
1023 for (c = 0; c < strlen (mode_str); c++)
1024 mode_str[c] = toupper (mode_str[c]);
1025 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1027 ah->ats_mode = MODE_SIMPLISTIC;
1029 else if (0 == strcmp (mode_str, "MLP"))
1031 ah->ats_mode = MODE_MLP;
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1034 ah->ats_mode = MODE_SIMPLISTIC;
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1040 ah->ats_mode = MODE_SIMPLISTIC;
1042 GNUNET_free (mode_str);
1044 /* Start configured solution method */
1045 switch (ah->ats_mode)
1048 /* Init the MLP solver with default values */
1050 ah->ats_mode = MODE_MLP;
1051 ah->s_init = &GAS_mlp_init;
1052 ah->s_update = &GAS_mlp_address_update;
1053 ah->s_get = &GAS_mlp_get_preferred_address;
1054 ah->s_pref = &GAS_mlp_address_change_preference;
1055 ah->s_del = &GAS_mlp_address_delete;
1056 ah->s_done = &GAS_mlp_done;
1062 case MODE_SIMPLISTIC:
1063 /* Init the simplistic solver with default values */
1064 ah->ats_mode = MODE_SIMPLISTIC;
1065 ah->s_init = &GAS_simplistic_init;
1066 ah->s_update = &GAS_simplistic_address_update;
1067 ah->s_get = &GAS_simplistic_get_preferred_address;
1068 ah->s_pref = &GAS_simplistic_address_change_preference;
1069 ah->s_del = &GAS_simplistic_address_delete;
1070 ah->s_done = &GAS_simplistic_done;
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1078 GNUNET_assert (NULL != ah->s_init);
1079 GNUNET_assert (NULL != ah->s_update);
1080 GNUNET_assert (NULL != ah->s_get);
1081 GNUNET_assert (NULL != ah->s_pref);
1082 GNUNET_assert (NULL != ah->s_del);
1083 GNUNET_assert (NULL != ah->s_done);
1085 ah->solver = ah->s_init (cfg, stats);
1086 if (NULL == ah->solver)
1088 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1093 /* up and running */
1094 ah->running = GNUNET_YES;
1100 * Free memory of address.
1103 * @param key peer identity (unused)
1104 * @param value the 'struct ATS_Address' to free
1105 * @return GNUNET_OK (continue to iterate)
1108 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1110 struct ATS_Address *aa = value;
1112 destroy_address (aa);
1118 GAS_addresses_destroy_all ()
1120 if (GNUNET_NO == handle->running)
1123 if (handle->addresses != NULL)
1124 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1125 GNUNET_assert (handle->active_addr_count == 0);
1130 * Shutdown address subsystem.
1133 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1135 GNUNET_assert (NULL != handle);
1137 GAS_addresses_destroy_all ();
1138 handle->running = GNUNET_NO;
1139 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1140 handle->addresses = NULL;
1141 GNUNET_free (handle);
1142 /* Stop configured solution method */
1146 struct PeerIteratorContext
1148 GNUNET_ATS_Peer_Iterator it;
1150 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1155 const struct GNUNET_HashCode * key,
1158 struct PeerIteratorContext *ip_ctx = cls;
1159 struct GNUNET_PeerIdentity tmp;
1161 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1163 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1164 tmp.hashPubKey = (*key);
1165 ip_ctx->it (ip_ctx->it_cls, &tmp);
1172 * Return all peers currently known to ATS
1174 * @param p_it the iterator to call for every peer, callbach with id == NULL
1176 * @param p_it_cls the closure for the iterator
1179 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1181 struct PeerIteratorContext ip_ctx;
1186 GNUNET_assert (NULL != handle->addresses);
1188 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1192 ip_ctx.it_cls = p_it_cls;
1193 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1194 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1195 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1197 p_it (p_it_cls, NULL);
1200 struct PeerInfoIteratorContext
1202 GNUNET_ATS_PeerInfo_Iterator it;
1208 peerinfo_it (void *cls,
1209 const struct GNUNET_HashCode * key,
1212 struct PeerInfoIteratorContext *pi_ctx = cls;
1213 struct ATS_Address *addr = (struct ATS_Address *) value;
1214 struct GNUNET_ATS_Information *ats;
1217 if (NULL != pi_ctx->it)
1219 ats_count = assemble_ats_information (addr, &ats);
1221 pi_ctx->it (pi_ctx->it_cls,
1224 addr->addr, addr->addr_len,
1227 addr->assigned_bw_out,
1228 addr->assigned_bw_in);
1236 * Return all peers currently known to ATS
1238 * @param peer the respective peer
1239 * @param pi_it the iterator to call for every peer
1240 * @param pi_it_cls the closure for the iterator
1243 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1245 struct PeerInfoIteratorContext pi_ctx;
1246 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1247 GNUNET_assert (NULL != peer);
1248 GNUNET_assert (NULL != handle->addresses);
1250 return; /* does not make sense without callback */
1252 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1254 pi_ctx.it_cls = pi_it_cls;
1256 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1259 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1264 /* end of gnunet-service-ats_addresses.c */