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,
116 ats, ats_count, aa->assigned_bw_out,
122 * Update a bandwidth assignment for a peer. This trivial method currently
123 * simply assigns the same share to all active connections.
127 * @param value the 'struct ATS_Address'
128 * @return GNUNET_OK (continue to iterate)
131 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
133 struct ATS_Address *aa = value;
135 if (GNUNET_YES != aa->active)
137 GNUNET_assert (active_addr_count > 0);
141 aa->assigned_bw_in.value__ = htonl (wan_quota_in / active_addr_count);
142 aa->assigned_bw_out.value__ = htonl (wan_quota_out / active_addr_count);
144 send_bw_notification (aa);
151 * Some (significant) input changed, recalculate bandwidth assignment
155 recalculate_assigned_bw ()
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Recalculating bandwidth for all active connections\n");
159 GNUNET_STATISTICS_update (GSA_stats, "# bandwidth recalculations performed",
161 GNUNET_STATISTICS_set (GSA_stats, "# active addresses", active_addr_count,
164 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, NULL);
168 * Free the given address
169 * @param addr address to destroy
172 free_address (struct ATS_Address *addr)
174 GNUNET_free_non_null (addr->ats);
175 GNUNET_free (addr->plugin);
180 * Create a ATS_address with the given information
182 * @param plugin_name plugin
183 * @param plugin_addr address
184 * @param plugin_addr_len address length
185 * @param session_id session
186 * @return the ATS_Address
188 static struct ATS_Address *
189 create_address (const struct GNUNET_PeerIdentity *peer,
190 const char *plugin_name,
191 const void *plugin_addr, size_t plugin_addr_len,
194 struct ATS_Address *aa = NULL;
196 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
198 aa->addr_len = plugin_addr_len;
200 memcpy (&aa[1], plugin_addr, plugin_addr_len);
201 aa->plugin = GNUNET_strdup (plugin_name);
202 aa->session_id = session_id;
208 * Destroy the given address.
210 * @param addr address to destroy
211 * @return GNUNET_YES if bandwidth allocations should be recalcualted
214 destroy_address (struct ATS_Address *addr)
219 GNUNET_assert (GNUNET_YES ==
220 GNUNET_CONTAINER_multihashmap_remove (addresses,
221 &addr->peer.hashPubKey,
226 GAS_mlp_address_delete (mlp, addresses, addr);
229 if (GNUNET_YES == addr->active)
232 addr->active = GNUNET_NO;
240 struct CompareAddressContext
242 const struct ATS_Address *search;
244 /* exact_address != NULL if address and session is equal */
245 struct ATS_Address *exact_address;
246 /* exact_address != NULL if address and session is 0 */
247 struct ATS_Address *base_address;
252 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
254 struct CompareAddressContext *cac = cls;
255 struct ATS_Address *aa = value;
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing peer %4s: address length %u session %u <-> address length %u session %u\n",
259 aa->addr_len, aa->session_id,
260 cac->search->addr_len, cac->search->session_id);
262 /* Find an matching exact address:
265 * aa->addr_len == cac->search->addr_len
266 * aa->plugin == cac->search->plugin
267 * aa->addr == cac->search->addr
268 * aa->session == cac->search->session
270 * return as exact address
272 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
274 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
275 cac->exact_address = aa;
278 /* Find an matching base address:
282 * aa->session_id == 0
285 * aa->addr_len == cac->search->addr_len
286 * aa->plugin == cac->search->plugin
287 * aa->addr == cac->search->addr
289 * return as base address
291 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
293 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
294 cac->base_address = aa;
297 /* Find an matching exact address based on session:
301 * cac->search->addr_len == 0
304 * aa->plugin == cac->search->plugin
305 * aa->session_id == cac->search->session_id
307 * return as exact address
309 if (0 == cac->search->addr_len)
311 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
312 cac->exact_address = aa;
315 if (cac->exact_address == NULL)
316 return GNUNET_YES; /* Continue iteration to find exact address */
318 return GNUNET_NO; /* Stop iteration since we have an exact address */
323 * Find an existing equivalent address record.
324 * Compares by peer identity and network address OR by session ID
325 * (one of the two must match).
327 * @param peer peer to lookup addresses for
328 * @param addr existing address record
329 * @return existing address record, NULL for none
332 find_address (const struct GNUNET_PeerIdentity *peer,
333 const struct ATS_Address *addr)
335 struct CompareAddressContext cac;
337 cac.exact_address = NULL;
338 cac.base_address = NULL;
340 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
341 &compare_address_it, &cac);
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Found exact address: %s base address: %s\n",
346 (cac.exact_address != NULL) ? "YES" : "NO",
347 (cac.base_address != NULL) ? "YES" : "NO");
349 if (cac.exact_address == NULL)
350 return cac.base_address;
351 return cac.exact_address;
355 static struct ATS_Address *
356 lookup_address (const struct GNUNET_PeerIdentity *peer,
357 const char *plugin_name, const void *plugin_addr,
358 size_t plugin_addr_len, uint32_t session_id,
359 const struct GNUNET_ATS_Information *atsi,
362 struct ATS_Address *aa;
363 struct ATS_Address *old;
365 aa = create_address (peer,
367 plugin_addr, plugin_addr_len,
370 aa->mlp_information = NULL;
371 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
372 aa->ats_count = atsi_count;
373 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
375 /* Get existing address or address with session == 0 */
376 old = find_address (peer, aa);
382 else if (old->session_id != session_id)
392 compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
394 struct CompareAddressContext *cac = cls;
395 struct ATS_Address *aa = value;
397 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
399 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
401 cac->exact_address = aa;
410 * Find an existing equivalent address record.
411 * Compares by peer identity and network address AND by session ID
412 * (one of the two must match).
414 * @param peer peer to lookup addresses for
415 * @param addr existing address record
416 * @return existing address record, NULL for none
418 static struct ATS_Address *
419 find_exact_address (const struct GNUNET_PeerIdentity *peer,
420 const struct ATS_Address *addr)
422 struct CompareAddressContext cac;
424 cac.exact_address = NULL;
426 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
427 &compare_address_session_it, &cac);
428 return cac.exact_address;
434 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
435 const char *plugin_name, const void *plugin_addr,
436 size_t plugin_addr_len, uint32_t session_id,
437 const struct GNUNET_ATS_Information *atsi,
440 struct ATS_Address *aa;
441 struct ATS_Address *old;
443 if (GNUNET_NO == running)
446 GNUNET_assert (NULL != addresses);
448 aa = create_address (peer,
450 plugin_addr, plugin_addr_len,
453 aa->mlp_information = NULL;
454 aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
455 aa->ats_count = atsi_count;
456 memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
458 /* Get existing address or address with session == 0 */
459 old = find_address (peer, aa);
462 /* We have a new address */
463 GNUNET_assert (GNUNET_OK ==
464 GNUNET_CONTAINER_multihashmap_put (addresses,
465 &peer->hashPubKey, aa,
466 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
468 GNUNET_i2s (peer), session_id, aa);
472 if (old->session_id == 0)
474 /* We have a base address with out an session, update this address */
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Updated existing address for peer `%s' %p with new session %u\n",
477 GNUNET_i2s (peer), old, session_id);
478 GNUNET_free_non_null (old->ats);
479 old->session_id = session_id;
483 old->ats_count = aa->ats_count;
484 GNUNET_free (aa->plugin);
489 /* This address and session is already existing */
490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
492 GNUNET_i2s (peer), plugin_name, session_id);
498 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
499 const char *plugin_name, const void *plugin_addr,
500 size_t plugin_addr_len, uint32_t session_id,
501 const struct GNUNET_ATS_Information *atsi,
504 struct ATS_Address *old;
507 if (GNUNET_NO == running)
510 GNUNET_assert (NULL != addresses);
512 /* Get existing address */
513 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
514 session_id, atsi, atsi_count);
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
518 GNUNET_i2s (peer), plugin_name, session_id);
523 for (i = 0; i < atsi_count; i++)
524 switch (ntohl (atsi[i].type))
526 case GNUNET_ATS_UTILIZATION_UP:
527 old->atsp_utilization_out.value__ = atsi[i].value;
529 case GNUNET_ATS_UTILIZATION_DOWN:
530 old->atsp_utilization_in.value__ = atsi[i].value;
532 case GNUNET_ATS_QUALITY_NET_DELAY:
533 old->atsp_latency.rel_value = ntohl (atsi[i].value);
535 case GNUNET_ATS_QUALITY_NET_DISTANCE:
536 old->atsp_distance = ntohl (atsi[i].value);
538 case GNUNET_ATS_COST_WAN:
539 old->atsp_cost_wan = ntohl (atsi[i].value);
541 case GNUNET_ATS_COST_LAN:
542 old->atsp_cost_lan = ntohl (atsi[i].value);
544 case GNUNET_ATS_COST_WLAN:
545 old->atsp_cost_wlan = ntohl (atsi[i].value);
547 case GNUNET_ATS_NETWORK_TYPE:
548 old->atsp_network_type = ntohl (atsi[i].value);
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 "Received unsupported ATS type %u\n", ntohl (atsi[i].type));
559 GAS_mlp_address_update (mlp, addresses, old);
567 * If session != 0, just the session is deleted, the address itself still exists
568 * If session == 0, remove full address
569 * If session == 0 and addrlen == 0, destroy inbound address
573 * @param value the 'struct ATS_Address'
574 * @return GNUNET_OK (continue to iterate)
577 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
579 const struct ATS_Address *info = cls;
580 struct ATS_Address *aa = value;
583 memcmp (&aa->peer, &info->peer,
584 sizeof (struct GNUNET_PeerIdentity)));
585 /* session == 0, remove full address */
586 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
587 (aa->addr_len == info->addr_len) &&
588 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Deleting address for peer `%s': `%s' %u\n",
593 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
595 if (GNUNET_YES == destroy_address (aa))
596 recalculate_assigned_bw ();
599 /* session != 0, just remove session */
600 if (aa->session_id != info->session_id)
601 return GNUNET_OK; /* irrelevant */
602 if (aa->session_id != 0)
603 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
606 "Deleting session for peer `%s': `%s' %u\n",
607 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
610 if (GNUNET_YES == aa->active)
612 aa->active = GNUNET_NO;
614 recalculate_assigned_bw ();
617 /* session == 0 and addrlen == 0 : destroy address */
618 if (aa->addr_len == 0)
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Deleting session and address for peer `%s': `%s' %u\n",
622 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
623 (void) destroy_address (aa);
627 /* session was set to 0, update address */
630 GAS_mlp_address_update (mlp, addresses, aa);
639 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
640 const char *plugin_name, const void *plugin_addr,
641 size_t plugin_addr_len, uint32_t session_id)
643 struct ATS_Address *aa;
644 struct ATS_Address *old;
646 if (GNUNET_NO == running)
649 /* Get existing address */
650 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
651 session_id, NULL, 0);
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
655 GNUNET_i2s (peer), plugin_name, session_id);
660 GNUNET_break (0 < strlen (plugin_name));
661 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
663 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
664 &destroy_by_session_id, aa);
671 * Find a "good" address to use for a peer. If we already have an existing
672 * address, we stick to it. Otherwise, we pick by lowest distance and then
675 * @param cls the 'struct ATS_Address**' where we store the result
677 * @param value another 'struct ATS_Address*' to consider using
678 * @return GNUNET_OK (continue to iterate)
681 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
683 struct ATS_Address **ap = cls;
684 struct ATS_Address *aa = (struct ATS_Address *) value;
685 struct ATS_Address *ab = *ap;
686 struct GNUNET_TIME_Absolute now;
688 now = GNUNET_TIME_absolute_get();
690 if (aa->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, aa->blocked_until).abs_value)
692 /* This address is blocked for suggestion */
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Address %p blocked for suggestion for %llu ms \n",
696 GNUNET_TIME_absolute_get_difference(now, aa->blocked_until).rel_value);
700 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
701 aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
706 /* FIXME this is a hack */
711 if ((0 == strcmp (ab->plugin, "tcp")) &&
712 (0 == strcmp (aa->plugin, "tcp")))
714 if ((0 != ab->addr_len) &&
717 /* saved address was an outbound address, but we have an inbound address */
721 if (0 == ab->addr_len)
723 /* saved address was an inbound address, so do not overwrite */
728 /* FIXME end of hack */
735 if ((ntohl (ab->assigned_bw_in.value__) == 0) &&
736 (ntohl (aa->assigned_bw_in.value__) > 0))
738 /* stick to existing connection */
742 if (ab->atsp_distance > aa->atsp_distance)
744 /* user shorter distance */
748 if (ab->atsp_latency.rel_value > aa->atsp_latency.rel_value)
750 /* user lower latency */
760 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
761 const char *plugin_name, const void *plugin_addr,
762 size_t plugin_addr_len, uint32_t session_id, int in_use)
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Received `%s' message for peer `%s': %i\n", "ADDRESS_IN_USE",
767 GNUNET_i2s (peer), in_use);
770 struct ATS_Address *old;
772 if (GNUNET_NO == running)
773 return GNUNET_SYSERR;
775 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
779 "Trying to set unknown address `%s', %s %u %s \n",
781 plugin_name, session_id,
782 (GNUNET_NO == in_use) ? "NO" : "YES");
784 return GNUNET_SYSERR;
786 if (old->used == in_use)
789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790 "Address in use called multiple times for peer `%s': %s -> %s \n",
792 (GNUNET_NO == old->used) ? "NO" : "YES",
793 (GNUNET_NO == in_use) ? "NO" : "YES");
794 return GNUNET_SYSERR;
799 GAS_mlp_address_update (mlp, addresses, old);
806 request_address_mlp (const struct GNUNET_PeerIdentity *peer)
808 struct ATS_Address *aa;
812 /* Get preferred address from MLP */
813 struct ATS_PreferedAddress * paddr = NULL;
814 paddr = GAS_mlp_get_preferred_address (mlp, addresses, peer);
816 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(paddr->bandwidth_out);
817 /* FIXME use bw in value */
818 paddr->bandwidth_in = paddr->bandwidth_out;
819 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init (paddr->bandwidth_in);
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
826 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
829 if (aa->active == GNUNET_NO)
831 aa->active = GNUNET_YES;
834 send_bw_notification (aa);
838 /* just to be sure... */
839 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
840 aa->addr_len, aa->session_id,
841 aa->ats, aa->ats_count,
850 request_address_simple (const struct GNUNET_PeerIdentity *peer)
852 struct ATS_Address *aa;
855 /* Get address with: stick to current address, lower distance, lower latency */
856 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
857 &find_address_it, &aa);
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
861 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
866 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
868 if (aa->active == GNUNET_NO)
870 aa->active = GNUNET_YES;
872 if (ats_mode == SIMPLE)
874 recalculate_assigned_bw ();
879 /* just to be sure... */
880 GAS_scheduling_transmit_address_suggestion (peer, aa->plugin, aa->addr,
881 aa->addr_len, aa->session_id,
882 aa->ats, aa->ats_count,
890 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
892 if (GNUNET_NO == running)
895 if (ats_mode == SIMPLE)
897 request_address_simple (peer);
901 request_address_mlp(peer);
907 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
909 struct ATS_Address *aa = value;
911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
912 "Resetting interval for peer `%s' address %p from %llu to 0\n", GNUNET_i2s (&aa->peer), aa, aa->block_interval);
914 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
915 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
921 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
923 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (addresses,
931 // FIXME: this function should likely end up in the LP-subsystem and
932 // not with 'addresses' in the future...
934 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
935 enum GNUNET_ATS_PreferenceKind kind,
938 if (GNUNET_NO == running)
942 GAS_mlp_address_change_preference (mlp, peer, kind, score);
949 * Initialize address subsystem.
951 * @param cfg configuration to use
952 * @param stats the statistics handle to use
955 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
956 const struct GNUNET_STATISTICS_Handle *stats)
960 char *quota_wan_in_str;
961 char *quota_wan_out_str;
965 addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
966 GNUNET_assert (NULL != addresses);
968 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_wan_in_str))
970 if (0 == strcmp(quota_wan_in_str, "unlimited") ||
971 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_in_str, &wan_quota_in)))
972 wan_quota_in = (UINT32_MAX) /10;
974 GNUNET_free (quota_wan_in_str);
975 quota_wan_in_str = NULL;
979 wan_quota_in = (UINT32_MAX) /10;
982 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_wan_out_str))
984 if (0 == strcmp(quota_wan_out_str, "unlimited") ||
985 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_wan_out_str, &wan_quota_out)))
986 wan_quota_out = (UINT32_MAX) /10;
988 GNUNET_free (quota_wan_out_str);
989 quota_wan_out_str = NULL;
993 wan_quota_out = (UINT32_MAX) /10;
996 mode = GNUNET_CONFIGURATION_get_value_yesno (cfg, "ats", "MLP");
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MLP mode %u", mode);
1004 /* Init the MLP solver with default values */
1005 mlp = GAS_mlp_init (cfg, stats, MLP_MAX_EXEC_DURATION, MLP_MAX_ITERATIONS);
1008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode\n");
1009 GNUNET_STATISTICS_update (GSA_stats, "MLP mode enabled", 0, GNUNET_NO);
1014 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 1, GNUNET_NO);
1018 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "MLP mode was configured, but libglpk is not installed, switching to simple mode");
1019 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1025 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1028 /* No configuration value */
1030 GNUNET_STATISTICS_update (GSA_stats, "MLP enabled", 0, GNUNET_NO);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started with %s mode\n", (SIMPLE == ats_mode) ? "SIMPLE" : "MLP");
1037 running = GNUNET_YES;
1042 * Free memory of address.
1045 * @param key peer identity (unused)
1046 * @param value the 'struct ATS_Address' to free
1047 * @return GNUNET_OK (continue to iterate)
1050 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1052 struct ATS_Address *aa = value;
1054 destroy_address (aa);
1060 GAS_addresses_destroy_all ()
1062 if (GNUNET_NO == running)
1065 if (addresses != NULL)
1066 GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
1067 GNUNET_assert (active_addr_count == 0);
1072 * Shutdown address subsystem.
1075 GAS_addresses_done ()
1077 GAS_addresses_destroy_all ();
1078 running = GNUNET_NO;
1079 GNUNET_CONTAINER_multihashmap_destroy (addresses);
1082 if (ats_mode == MLP)
1089 struct PeerIteratorContext
1091 GNUNET_ATS_Peer_Iterator it;
1093 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1098 const struct GNUNET_HashCode * key,
1101 struct PeerIteratorContext *ip_ctx = cls;
1102 struct GNUNET_PeerIdentity tmp;
1104 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1106 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1107 tmp.hashPubKey = (*key);
1108 ip_ctx->it (ip_ctx->it_cls, &tmp);
1115 * Return all peers currently known to ATS
1117 * @param p_it the iterator to call for every peer
1118 * @param p_it_cls the closure for the iterator
1121 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1123 struct PeerIteratorContext ip_ctx;
1128 GNUNET_assert (NULL != addresses);
1130 size = GNUNET_CONTAINER_multihashmap_size(addresses);
1134 ip_ctx.it_cls = p_it_cls;
1135 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1136 GNUNET_CONTAINER_multihashmap_iterate (addresses, &peer_it, &ip_ctx);
1137 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1139 p_it (p_it_cls, NULL);
1142 struct PeerInfoIteratorContext
1144 GNUNET_ATS_PeerInfo_Iterator it;
1150 peerinfo_it (void *cls,
1151 const struct GNUNET_HashCode * key,
1154 struct PeerInfoIteratorContext *pi_ctx = cls;
1155 struct ATS_Address *addr = (struct ATS_Address *) value;
1156 struct GNUNET_ATS_Information *ats;
1159 if (NULL != pi_ctx->it)
1161 ats_count = assemble_ats_information (addr, &ats);
1163 pi_ctx->it (pi_ctx->it_cls,
1166 addr->addr, addr->addr_len,
1169 addr->assigned_bw_out,
1170 addr->assigned_bw_in);
1178 * Return all peers currently known to ATS
1180 * @param peer the respective peer
1181 * @param pi_it the iterator to call for every peer
1182 * @param pi_it_cls the closure for the iterator
1185 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1187 struct PeerInfoIteratorContext pi_ctx;
1188 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1189 GNUNET_assert (NULL != peer);
1190 GNUNET_assert (NULL != addresses);
1192 return; /* does not make sense without callback */
1194 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1196 pi_ctx.it_cls = pi_it_cls;
1198 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1201 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1206 /* end of gnunet-service-ats_addresses.c */