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 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
951 enum GNUNET_ATS_PreferenceKind kind,
954 if (GNUNET_NO == handle->running)
957 /* Tell solver about update */
958 handle->s_pref (handle->solver, peer, kind, score);
964 * Initialize address subsystem.
966 * @param cfg configuration to use
967 * @param stats the statistics handle to use
969 struct GAS_Addresses_Handle *
970 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
971 const struct GNUNET_STATISTICS_Handle *stats)
973 struct GAS_Addresses_Handle *ah;
974 char *quota_wan_in_str;
975 char *quota_wan_out_str;
979 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
981 handle->running = GNUNET_NO;
983 /* Initialize the system with configuration values */
984 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
986 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
987 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &ah->wan_quota_in)))
988 ah->wan_quota_in = (UINT32_MAX) /10;
990 GNUNET_free (quota_wan_in_str);
991 quota_wan_in_str = NULL;
994 ah->wan_quota_in = (UINT32_MAX) /10;
996 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
998 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
999 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &ah->wan_quota_out)))
1000 ah->wan_quota_out = (UINT32_MAX) /10;
1002 GNUNET_free (quota_wan_out_str);
1003 quota_wan_out_str = NULL;
1006 ah->wan_quota_out = (UINT32_MAX) /10;
1008 /* Initialize the addresses database */
1009 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1010 GNUNET_assert (NULL != ah->addresses);
1012 /* Figure out configured solution method */
1013 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1015 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1016 ah->ats_mode = MODE_SIMPLISTIC;
1020 for (c = 0; c < strlen (mode_str); c++)
1021 mode_str[c] = toupper (mode_str[c]);
1022 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1024 ah->ats_mode = MODE_SIMPLISTIC;
1026 else if (0 == strcmp (mode_str, "MLP"))
1028 ah->ats_mode = MODE_MLP;
1030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1031 ah->ats_mode = MODE_SIMPLISTIC;
1036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1037 ah->ats_mode = MODE_SIMPLISTIC;
1039 GNUNET_free (mode_str);
1041 /* Start configured solution method */
1042 switch (ah->ats_mode)
1045 /* Init the MLP solver with default values */
1047 ah->ats_mode = MODE_MLP;
1048 ah->s_init = &GAS_mlp_init;
1049 ah->s_update = &GAS_mlp_address_update;
1050 ah->s_get = &GAS_mlp_get_preferred_address;
1051 ah->s_pref = &GAS_mlp_address_change_preference;
1052 ah->s_del = &GAS_mlp_address_delete;
1053 ah->s_done = &GAS_mlp_done;
1059 case MODE_SIMPLISTIC:
1060 /* Init the simplistic solver with default values */
1061 ah->ats_mode = MODE_SIMPLISTIC;
1062 ah->s_init = &GAS_simplistic_init;
1063 ah->s_update = &GAS_simplistic_address_update;
1064 ah->s_get = &GAS_simplistic_get_preferred_address;
1065 ah->s_pref = &GAS_simplistic_address_change_preference;
1066 ah->s_del = &GAS_simplistic_address_delete;
1067 ah->s_done = &GAS_simplistic_done;
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1075 GNUNET_assert (NULL != ah->s_init);
1076 GNUNET_assert (NULL != ah->s_update);
1077 GNUNET_assert (NULL != ah->s_get);
1078 GNUNET_assert (NULL != ah->s_pref);
1079 GNUNET_assert (NULL != ah->s_del);
1080 GNUNET_assert (NULL != ah->s_done);
1082 ah->solver = ah->s_init (cfg, stats);
1083 if (NULL == ah->solver)
1085 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize MLP solver!\n");
1090 /* up and running */
1091 ah->running = GNUNET_YES;
1097 * Free memory of address.
1100 * @param key peer identity (unused)
1101 * @param value the 'struct ATS_Address' to free
1102 * @return GNUNET_OK (continue to iterate)
1105 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1107 struct ATS_Address *aa = value;
1109 destroy_address (aa);
1115 GAS_addresses_destroy_all ()
1117 if (GNUNET_NO == handle->running)
1120 if (handle->addresses != NULL)
1121 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1122 GNUNET_assert (handle->active_addr_count == 0);
1127 * Shutdown address subsystem.
1130 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1132 GNUNET_assert (NULL != handle);
1134 GAS_addresses_destroy_all ();
1135 handle->running = GNUNET_NO;
1136 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1137 handle->addresses = NULL;
1138 GNUNET_free (handle);
1139 /* Stop configured solution method */
1143 struct PeerIteratorContext
1145 GNUNET_ATS_Peer_Iterator it;
1147 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1152 const struct GNUNET_HashCode * key,
1155 struct PeerIteratorContext *ip_ctx = cls;
1156 struct GNUNET_PeerIdentity tmp;
1158 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1160 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1161 tmp.hashPubKey = (*key);
1162 ip_ctx->it (ip_ctx->it_cls, &tmp);
1169 * Return all peers currently known to ATS
1171 * @param p_it the iterator to call for every peer, callbach with id == NULL
1173 * @param p_it_cls the closure for the iterator
1176 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1178 struct PeerIteratorContext ip_ctx;
1183 GNUNET_assert (NULL != handle->addresses);
1185 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1189 ip_ctx.it_cls = p_it_cls;
1190 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1191 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1192 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1194 p_it (p_it_cls, NULL);
1197 struct PeerInfoIteratorContext
1199 GNUNET_ATS_PeerInfo_Iterator it;
1205 peerinfo_it (void *cls,
1206 const struct GNUNET_HashCode * key,
1209 struct PeerInfoIteratorContext *pi_ctx = cls;
1210 struct ATS_Address *addr = (struct ATS_Address *) value;
1211 struct GNUNET_ATS_Information *ats;
1214 if (NULL != pi_ctx->it)
1216 ats_count = assemble_ats_information (addr, &ats);
1218 pi_ctx->it (pi_ctx->it_cls,
1221 addr->addr, addr->addr_len,
1224 addr->assigned_bw_out,
1225 addr->assigned_bw_in);
1233 * Return all peers currently known to ATS
1235 * @param peer the respective peer
1236 * @param pi_it the iterator to call for every peer
1237 * @param pi_it_cls the closure for the iterator
1240 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1242 struct PeerInfoIteratorContext pi_ctx;
1243 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1244 GNUNET_assert (NULL != peer);
1245 GNUNET_assert (NULL != handle->addresses);
1247 return; /* does not make sense without callback */
1249 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1251 pi_ctx.it_cls = pi_it_cls;
1253 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1256 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1261 /* end of gnunet-service-ats_addresses.c */