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"
40 * ATS address management
44 * - If you add a new address without a session, a new address will be added
45 * - If you add this address again now with a session a, the existing address
46 * will be updated with this session
47 * - If you add this address again now with a session b, a new address object
48 * with this session will be added
50 * Destroying addresses:
52 * - If you destroy an address without a session, the address itself and all
53 * address instances with an session will be removed
54 * - If you destroy an address with a session, the session for this address
58 * Addresses without a session will be updated with a new session and if the
59 * the session is destroyed the session is removed and address itself still
60 * exists for suggestion
66 * Available ressource assignment modes
73 * Assign each peer an equal amount of bandwidth (bw)
75 * bw_per_peer = bw_total / #active addresses
82 * Solve ressource assignment as an optimization problem
83 * Uses an mixed integer programming solver
89 * Pending Address suggestion requests
91 struct GAS_Addresses_Suggestion_Requests
96 struct GAS_Addresses_Suggestion_Requests *next;
101 struct GAS_Addresses_Suggestion_Requests *prev;
106 struct GNUNET_PeerIdentity id;
110 * Handle for ATS address component
112 struct GAS_Addresses_Handle
117 struct GNUNET_STATISTICS_Handle *stat;
120 * A multihashmap to store all addresses
122 struct GNUNET_CONTAINER_MultiHashMap *addresses;
125 * Configure WAN quota in
127 unsigned long long wan_quota_in;
130 * Configure WAN quota out
132 unsigned long long wan_quota_out;
135 * Is ATS addresses running
140 * Configured ATS solver
150 * Address suggestion requests DLL head
152 struct GAS_Addresses_Suggestion_Requests *r_head;
155 * Address suggestion requests DLL tail
157 struct GAS_Addresses_Suggestion_Requests *r_tail;
159 /* Solver functions */
164 GAS_solver_init s_init;
167 * Add an address to the solver
169 GAS_solver_address_add s_add;
172 * Update address in solver
174 GAS_solver_address_update s_update;
177 * Get address from solver
179 GAS_solver_get_preferred_address s_get;
182 * Delete address in solver
184 GAS_solver_address_delete s_del;
187 * Change preference for quality in solver
189 GAS_solver_address_change_preference s_pref;
194 GAS_solver_done s_done;
199 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
201 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
202 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
205 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
206 ats[0].value = aa->atsp_utilization_out.value__;
207 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
208 ats[1].value = aa->atsp_utilization_in.value__;
209 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
210 ats[2].value = ntohl(aa->atsp_network_type);
211 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
212 ats[3].value = ntohl(aa->atsp_latency.rel_value);
213 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
214 ats[4].value = ntohl(aa->atsp_distance);
215 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
216 ats[5].value = ntohl (aa->atsp_cost_wan);
217 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
218 ats[6].value = ntohl (aa->atsp_cost_lan);
219 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
220 ats[7].value = ntohl (aa->atsp_cost_wlan);
225 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
227 struct ATS_Address *dest)
231 for (i = 0; i < ats_count; i++)
232 switch (ntohl (src[i].type))
234 case GNUNET_ATS_UTILIZATION_UP:
235 dest->atsp_utilization_out.value__ = src[i].value;
238 case GNUNET_ATS_UTILIZATION_DOWN:
239 dest->atsp_utilization_in.value__ = src[i].value;
242 case GNUNET_ATS_QUALITY_NET_DELAY:
243 dest->atsp_latency.rel_value = ntohl (src[i].value);
246 case GNUNET_ATS_QUALITY_NET_DISTANCE:
247 dest->atsp_distance = ntohl (src[i].value);
250 case GNUNET_ATS_COST_WAN:
251 dest->atsp_cost_wan = ntohl (src[i].value);
254 case GNUNET_ATS_COST_LAN:
255 dest->atsp_cost_lan = ntohl (src[i].value);
258 case GNUNET_ATS_COST_WLAN:
259 dest->atsp_cost_wlan = ntohl (src[i].value);
262 case GNUNET_ATS_NETWORK_TYPE:
263 dest->atsp_network_type = ntohl (src[i].value);
266 case GNUNET_ATS_ARRAY_TERMINATOR:
269 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
270 "Received unsupported ATS type %u\n", ntohl (src[i].type));
278 * Free the given address
279 * @param addr address to destroy
282 free_address (struct ATS_Address *addr)
284 GNUNET_free (addr->plugin);
289 * Create a ATS_address with the given information
291 * @param plugin_name plugin
292 * @param plugin_addr address
293 * @param plugin_addr_len address length
294 * @param session_id session
295 * @return the ATS_Address
297 static struct ATS_Address *
298 create_address (const struct GNUNET_PeerIdentity *peer,
299 const char *plugin_name,
300 const void *plugin_addr, size_t plugin_addr_len,
303 struct ATS_Address *aa = NULL;
305 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
307 aa->addr_len = plugin_addr_len;
309 memcpy (&aa[1], plugin_addr, plugin_addr_len);
310 aa->plugin = GNUNET_strdup (plugin_name);
311 aa->session_id = session_id;
312 aa->active = GNUNET_NO;
313 aa->used = GNUNET_NO;
314 aa->solver_information = NULL;
315 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
316 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
322 * Destroy the given address.
324 * @param handle the address handle
325 * @param addr address to destroy
326 * @return GNUNET_YES if bandwidth allocations should be recalcualted
329 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
334 GNUNET_assert (GNUNET_YES ==
335 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
336 &addr->peer.hashPubKey,
343 struct CompareAddressContext
345 const struct ATS_Address *search;
347 /* exact_address != NULL if address and session is equal */
348 struct ATS_Address *exact_address;
349 /* exact_address != NULL if address and session is 0 */
350 struct ATS_Address *base_address;
355 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
357 struct CompareAddressContext *cac = cls;
358 struct ATS_Address *aa = value;
360 /* Find an matching exact address:
363 * aa->addr_len == cac->search->addr_len
364 * aa->plugin == cac->search->plugin
365 * aa->addr == cac->search->addr
366 * aa->session == cac->search->session
368 * return as exact address
370 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
372 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
373 cac->exact_address = aa;
376 /* Find an matching base address:
380 * aa->session_id == 0
383 * aa->addr_len == cac->search->addr_len
384 * aa->plugin == cac->search->plugin
385 * aa->addr == cac->search->addr
387 * return as base address
389 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
391 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
392 cac->base_address = aa;
395 /* Find an matching exact address based on session:
399 * cac->search->addr_len == 0
402 * aa->plugin == cac->search->plugin
403 * aa->session_id == cac->search->session_id
405 * return as exact address
407 if (0 == cac->search->addr_len)
409 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
410 cac->exact_address = aa;
413 if (cac->exact_address == NULL)
414 return GNUNET_YES; /* Continue iteration to find exact address */
416 return GNUNET_NO; /* Stop iteration since we have an exact address */
421 * Find an existing equivalent address record.
422 * Compares by peer identity and network address OR by session ID
423 * (one of the two must match).
425 * @param handle the address handle
426 * @param peer peer to lookup addresses for
427 * @param addr existing address record
428 * @return existing address record, NULL for none
431 find_equivalent_address (struct GAS_Addresses_Handle *handle,
432 const struct GNUNET_PeerIdentity *peer,
433 const struct ATS_Address *addr)
435 struct CompareAddressContext cac;
437 cac.exact_address = NULL;
438 cac.base_address = NULL;
440 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
441 &compare_address_it, &cac);
443 if (cac.exact_address == NULL)
444 return cac.base_address;
445 return cac.exact_address;
449 static struct ATS_Address *
450 lookup_address (struct GAS_Addresses_Handle *handle,
451 const struct GNUNET_PeerIdentity *peer,
452 const char *plugin_name,
453 const void *plugin_addr,
454 size_t plugin_addr_len,
456 const struct GNUNET_ATS_Information *atsi,
459 struct ATS_Address *aa;
460 struct ATS_Address *ea;
462 aa = create_address (peer,
464 plugin_addr, plugin_addr_len,
467 /* Get existing address or address with session == 0 */
468 ea = find_equivalent_address (handle, peer, aa);
474 else if (ea->session_id != session_id)
483 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
484 const struct GNUNET_PeerIdentity *peer,
485 const char *plugin_name, const void *plugin_addr,
486 size_t plugin_addr_len, uint32_t session_id,
487 const struct GNUNET_ATS_Information *atsi,
490 struct ATS_Address *aa;
491 struct ATS_Address *ea;
492 unsigned int ats_res;
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "Received `%s' for peer `%s'\n",
499 if (GNUNET_NO == handle->running)
502 GNUNET_assert (NULL != handle->addresses);
504 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
506 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "While adding address: had %u ATS elements to add, could only add %u\n",
510 atsi_count, ats_res);
513 /* Get existing address or address with session == 0 */
514 ea = find_equivalent_address (handle, peer, aa);
517 /* We have a new address */
518 GNUNET_assert (GNUNET_OK ==
519 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
520 &peer->hashPubKey, aa,
521 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
523 GNUNET_i2s (peer), session_id, aa);
524 /* Tell solver about new address */
525 handle->s_add (handle->solver, handle->addresses, aa);
528 GNUNET_free (aa->plugin);
531 if (ea->session_id != 0)
533 /* This address with the same session is already existing
534 * Should not happen */
535 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
536 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
537 GNUNET_i2s (peer), plugin_name, session_id);
542 /* We have an address without an session, update this address */
544 /* Notify solver about update with atsi information and session */
545 handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
548 ea->session_id = session_id;
549 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "While updating address: had %u ATS elements to add, could only add %u\n",
553 atsi_count, ats_res);
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Updated existing address for peer `%s' %p with new session %u\n",
557 GNUNET_i2s (peer), ea, session_id);
562 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
563 const struct GNUNET_PeerIdentity *peer,
564 const char *plugin_name, const void *plugin_addr,
565 size_t plugin_addr_len, uint32_t session_id,
566 const struct GNUNET_ATS_Information *atsi,
569 struct ATS_Address *aa;
572 if (GNUNET_NO == handle->running)
575 GNUNET_assert (NULL != handle->addresses);
577 /* Get existing address */
578 aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
579 session_id, atsi, atsi_count);
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
583 GNUNET_i2s (peer), plugin_name, session_id);
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Received `%s' for peer `%s' address \n",
591 GNUNET_i2s (peer), aa);
593 /* Tell solver about update */
594 handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
597 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "While adding address: had %u ATS elements to add, could only add %u\n",
601 atsi_count, ats_res);
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Updated %u ATS elements for address %p\n",
609 struct DestroyContext
611 struct ATS_Address *aa;
613 struct GAS_Addresses_Handle *handle;
616 * GNUNET_NO : full address
617 * GNUNET_YES : just session
626 * If session != 0, just the session is deleted, the address itself still exists
627 * If session == 0, remove full address
628 * If session == 0 and addrlen == 0, destroy inbound address
632 * @param value the 'struct ATS_Address'
633 * @return GNUNET_OK (continue to iterate)
636 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
638 struct DestroyContext *dc = cls;
639 struct GAS_Addresses_Handle *handle = dc->handle;
640 const struct ATS_Address *des = dc->aa;
641 struct ATS_Address *aa = value;
643 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
644 sizeof (struct GNUNET_PeerIdentity)));
647 if (des->session_id == 0)
649 /* Session == 0, remove full address */
650 if ((0 == strcmp (des->plugin, aa->plugin)) &&
651 (aa->addr_len == des->addr_len) &&
652 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656 "Deleting full address for peer `%s' session %u %p\n",
657 GNUNET_i2s (&aa->peer), aa->session_id, aa);
659 /* Notify solver about deletion */
660 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
661 destroy_address (handle, aa);
662 dc->result = GNUNET_NO;
663 return GNUNET_OK; /* Continue iteration */
668 /* Session != 0, just remove session */
669 if (aa->session_id != des->session_id)
670 return GNUNET_OK; /* irrelevant */
672 if ((aa->session_id != 0) &&
673 (0 != strcmp (des->plugin, aa->plugin)))
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Different plugins during removal: `%s' vs `%s' \n",
677 des->plugin, aa->plugin);
682 if (aa->addr_len == 0)
684 /* Inbound connection died, delete full address */
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Deleting inbound address for peer `%s': `%s' session %u\n",
687 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
689 /* Notify solver about deletion */
690 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
691 destroy_address (handle, aa);
692 dc->result = GNUNET_NO;
693 return GNUNET_OK; /* Continue iteration */
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Deleting session for peer `%s': `%s' %u\n",
700 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
701 /* Notify solver to delete session */
702 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
711 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
712 const struct GNUNET_PeerIdentity *peer,
713 const char *plugin_name, const void *plugin_addr,
714 size_t plugin_addr_len, uint32_t session_id)
716 struct ATS_Address *ea;
717 struct DestroyContext dc;
719 if (GNUNET_NO == handle->running)
722 /* Get existing address */
723 ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
724 session_id, NULL, 0);
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Received `%s' for peer `%s' address %p session %u\n",
729 GNUNET_i2s (peer), ea, session_id);
733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
734 GNUNET_i2s (peer), plugin_name, session_id);
738 GNUNET_break (0 < strlen (plugin_name));
740 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
742 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
743 &destroy_by_session_id, &dc);
744 free_address (dc.aa);
749 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
750 const struct GNUNET_PeerIdentity *peer,
751 const char *plugin_name, const void *plugin_addr,
752 size_t plugin_addr_len, uint32_t session_id, int in_use)
754 struct ATS_Address *ea;
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Received `%s' for peer `%s'\n",
761 if (GNUNET_NO == handle->running)
762 return GNUNET_SYSERR;
764 ea = lookup_address (handle, peer, plugin_name,
765 plugin_addr, plugin_addr_len,
766 session_id, NULL, 0);
769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
770 "Trying to set unknown address `%s', %s %u %s \n",
772 plugin_name, session_id,
773 (GNUNET_NO == in_use) ? "NO" : "YES");
775 return GNUNET_SYSERR;
777 if (ea->used == in_use)
780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
781 "Address in use called multiple times for peer `%s': %s -> %s \n",
783 (GNUNET_NO == ea->used) ? "NO" : "YES",
784 (GNUNET_NO == in_use) ? "NO" : "YES");
785 return GNUNET_SYSERR;
788 /* Tell solver about update */
789 handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
797 * Cancel address suggestions for a peer
799 * @param handle the address handle
800 * @param peer the respective peer
803 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
804 const struct GNUNET_PeerIdentity *peer)
806 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
813 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
821 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
824 GAS_addresses_handle_backoff_reset (handle, peer);
825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
826 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
827 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
833 * Add an address suggestions for a peer
835 * @param handle the address handle
836 * @param peer the respective peer
839 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
840 const struct GNUNET_PeerIdentity *peer)
842 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
843 struct ATS_Address *aa;
844 struct GNUNET_ATS_Information *ats;
845 unsigned int ats_count;
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Received `%s' for peer `%s'\n",
852 if (GNUNET_NO == handle->running)
856 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
857 break; /* already suggesting */
862 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
864 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
867 /* Get prefered address from solver */
868 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
879 ats_count = assemble_ats_information (aa, &ats);
880 GAS_scheduling_transmit_address_suggestion (peer,
882 aa->addr, aa->addr_len,
888 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
889 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
892 "Address %p ready for suggestion, block interval now %llu \n",
893 aa, aa->block_interval);
900 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
902 struct ATS_Address *aa = value;
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "Resetting interval for peer `%s' address %p from %llu to 0\n",
906 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
908 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
909 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
915 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
916 const struct GNUNET_PeerIdentity *peer)
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "Received `%s' for peer `%s'\n",
923 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
931 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
932 const struct GNUNET_PeerIdentity *peer,
933 enum GNUNET_ATS_PreferenceKind kind,
936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
937 "Received `%s' for peer `%s'\n",
941 if (GNUNET_NO == handle->running)
944 /* Tell solver about update */
945 handle->s_pref (handle->solver, peer, kind, score);
949 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
951 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
952 char * entry_in = NULL;
953 char * entry_out = NULL;
954 char * quota_out_str;
958 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
963 case GNUNET_ATS_NET_UNSPECIFIED:
964 entry_out = "UNSPECIFIED_QUOTA_OUT";
965 entry_in = "UNSPECIFIED_QUOTA_IN";
967 case GNUNET_ATS_NET_LOOPBACK:
968 entry_out = "LOOPBACK_QUOTA_OUT";
969 entry_in = "LOOPBACK_QUOTA_IN";
971 case GNUNET_ATS_NET_LAN:
972 entry_out = "LAN_QUOTA_OUT";
973 entry_in = "LAN_QUOTA_IN";
975 case GNUNET_ATS_NET_WAN:
976 entry_out = "WAN_QUOTA_OUT";
977 entry_in = "WAN_QUOTA_IN";
979 case GNUNET_ATS_NET_WLAN:
980 entry_out = "WLAN_QUOTA_OUT";
981 entry_in = "WLAN_QUOTA_IN";
987 if ((entry_in == NULL) || (entry_out == NULL))
991 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
993 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
994 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
995 out_dest[c] = UINT32_MAX;
997 GNUNET_free (quota_out_str);
998 quota_out_str = NULL;
1000 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
1001 out_dest[c] = UINT32_MAX;
1003 out_dest[c] = UINT32_MAX;
1006 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
1008 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
1009 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1010 in_dest[c] = UINT32_MAX;
1012 GNUNET_free (quota_in_str);
1013 quota_in_str = NULL;
1015 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
1017 in_dest[c] = UINT32_MAX;
1021 in_dest[c] = UINT32_MAX;
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
1026 return GNUNET_ATS_NetworkTypeCount;
1031 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1033 struct GAS_Addresses_Handle *handle = cls;
1034 struct GAS_Addresses_Suggestion_Requests *cur;
1036 GNUNET_assert (handle != NULL);
1037 GNUNET_assert (address != NULL);
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1040 struct GNUNET_ATS_Information *ats;
1041 unsigned int ats_count;
1043 cur = handle->r_head;
1046 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1047 break; /* we have an address request pending*/
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1053 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1060 ats_count = assemble_ats_information (address, &ats);
1061 GAS_scheduling_transmit_address_suggestion (&address->peer,
1063 address->addr, address->addr_len,
1064 address->session_id,
1066 address->assigned_bw_out,
1067 address->assigned_bw_in);
1073 * Initialize address subsystem.
1075 * @param cfg configuration to use
1076 * @param stats the statistics handle to use
1078 struct GAS_Addresses_Handle *
1079 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1080 const struct GNUNET_STATISTICS_Handle *stats)
1082 struct GAS_Addresses_Handle *ah;
1083 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1084 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1085 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1090 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1091 ah->running = GNUNET_NO;
1093 ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1094 /* Initialize the addresses database */
1095 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1096 GNUNET_assert (NULL != ah->addresses);
1098 /* Figure out configured solution method */
1099 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1102 ah->ats_mode = MODE_SIMPLISTIC;
1106 for (c = 0; c < strlen (mode_str); c++)
1107 mode_str[c] = toupper (mode_str[c]);
1108 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1110 ah->ats_mode = MODE_SIMPLISTIC;
1112 else if (0 == strcmp (mode_str, "MLP"))
1114 ah->ats_mode = MODE_MLP;
1116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1117 ah->ats_mode = MODE_SIMPLISTIC;
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1123 ah->ats_mode = MODE_SIMPLISTIC;
1125 GNUNET_free (mode_str);
1127 /* Start configured solution method */
1128 switch (ah->ats_mode)
1131 /* Init the MLP solver with default values */
1133 ah->ats_mode = MODE_MLP;
1134 ah->s_init = &GAS_mlp_init;
1135 ah->s_add = &GAS_mlp_address_add;
1136 ah->s_update = &GAS_mlp_address_update;
1137 ah->s_get = &GAS_mlp_get_preferred_address;
1138 ah->s_pref = &GAS_mlp_address_change_preference;
1139 ah->s_del = &GAS_mlp_address_delete;
1140 ah->s_done = &GAS_mlp_done;
1146 case MODE_SIMPLISTIC:
1147 /* Init the simplistic solver with default values */
1148 ah->ats_mode = MODE_SIMPLISTIC;
1149 ah->s_init = &GAS_simplistic_init;
1150 ah->s_add = &GAS_simplistic_address_add;
1151 ah->s_update = &GAS_simplistic_address_update;
1152 ah->s_get = &GAS_simplistic_get_preferred_address;
1153 ah->s_pref = &GAS_simplistic_address_change_preference;
1154 ah->s_del = &GAS_simplistic_address_delete;
1155 ah->s_done = &GAS_simplistic_done;
1156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1163 GNUNET_assert (NULL != ah->s_init);
1164 GNUNET_assert (NULL != ah->s_add);
1165 GNUNET_assert (NULL != ah->s_update);
1166 GNUNET_assert (NULL != ah->s_get);
1167 GNUNET_assert (NULL != ah->s_pref);
1168 GNUNET_assert (NULL != ah->s_del);
1169 GNUNET_assert (NULL != ah->s_done);
1171 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1173 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1174 if (NULL == ah->solver)
1176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1181 /* up and running */
1182 ah->running = GNUNET_YES;
1188 * Free memory of address.
1191 * @param key peer identity (unused)
1192 * @param value the 'struct ATS_Address' to free
1193 * @return GNUNET_OK (continue to iterate)
1196 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1198 struct GAS_Addresses_Handle *handle = cls;
1199 struct ATS_Address *aa = value;
1200 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1201 destroy_address (handle, aa);
1207 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1209 if (GNUNET_NO == handle->running)
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 if (handle->addresses != NULL)
1217 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1222 * Shutdown address subsystem.
1225 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1227 struct GAS_Addresses_Suggestion_Requests *cur;
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Shutting down addresses\n");
1231 GNUNET_assert (NULL != handle);
1232 GAS_addresses_destroy_all (handle);
1233 handle->running = GNUNET_NO;
1234 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1235 handle->addresses = NULL;
1236 while (NULL != (cur = handle->r_head))
1238 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1241 handle->s_done (handle->solver);
1242 GNUNET_free (handle);
1243 /* Stop configured solution method */
1247 struct PeerIteratorContext
1249 GNUNET_ATS_Peer_Iterator it;
1251 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1256 const struct GNUNET_HashCode * key,
1259 struct PeerIteratorContext *ip_ctx = cls;
1260 struct GNUNET_PeerIdentity tmp;
1262 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1264 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1265 tmp.hashPubKey = (*key);
1266 ip_ctx->it (ip_ctx->it_cls, &tmp);
1273 * Return all peers currently known to ATS
1275 * @param handle the address handle
1276 * @param p_it the iterator to call for every peer, callbach with id == NULL
1278 * @param p_it_cls the closure for the iterator
1281 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1283 struct PeerIteratorContext ip_ctx;
1288 GNUNET_assert (NULL != handle->addresses);
1290 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1294 ip_ctx.it_cls = p_it_cls;
1295 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1296 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1297 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1299 p_it (p_it_cls, NULL);
1302 struct PeerInfoIteratorContext
1304 GNUNET_ATS_PeerInfo_Iterator it;
1310 peerinfo_it (void *cls,
1311 const struct GNUNET_HashCode * key,
1314 struct PeerInfoIteratorContext *pi_ctx = cls;
1315 struct ATS_Address *addr = (struct ATS_Address *) value;
1316 struct GNUNET_ATS_Information *ats;
1319 if (NULL != pi_ctx->it)
1321 ats_count = assemble_ats_information (addr, &ats);
1323 pi_ctx->it (pi_ctx->it_cls,
1326 addr->addr, addr->addr_len,
1329 addr->assigned_bw_out,
1330 addr->assigned_bw_in);
1338 * Return all peers currently known to ATS
1340 * @param handle the address handle
1341 * @param peer the respective peer
1342 * @param pi_it the iterator to call for every peer
1343 * @param pi_it_cls the closure for the iterator
1346 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1347 const struct GNUNET_PeerIdentity *peer,
1348 GNUNET_ATS_PeerInfo_Iterator pi_it,
1351 struct PeerInfoIteratorContext pi_ctx;
1352 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1353 GNUNET_assert (NULL != peer);
1354 GNUNET_assert (NULL != handle->addresses);
1356 return; /* does not make sense without callback */
1358 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1360 pi_ctx.it_cls = pi_it_cls;
1362 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1365 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1370 /* end of gnunet-service-ats_addresses.c */