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"
41 * Available ressource assignment modes
48 * Assign each peer an equal amount of bandwidth (bw)
50 * bw_per_peer = bw_total / #active addresses
57 * Solve ressource assignment as an optimization problem
58 * Uses an mixed integer programming solver
64 * Handle for ATS address component
66 struct GAS_Addresses_Suggestion_Requests
68 struct GAS_Addresses_Suggestion_Requests *next;
69 struct GAS_Addresses_Suggestion_Requests *prev;
71 struct GNUNET_PeerIdentity id;
75 * Handle for ATS address component
77 struct GAS_Addresses_Handle
80 * A multihashmap to store all addresses
82 struct GNUNET_CONTAINER_MultiHashMap *addresses;
85 * Configure WAN quota in
87 unsigned long long wan_quota_in;
90 * Configure WAN quota out
92 unsigned long long wan_quota_out;
95 * Is ATS addresses running
100 * Configured ATS solver
110 * Address suggestion requests DLL head
112 struct GAS_Addresses_Suggestion_Requests *r_head;
115 * Address suggestion requests DLL tail
117 struct GAS_Addresses_Suggestion_Requests *r_tail;
119 /* Solver functions */
124 GAS_solver_init s_init;
127 * Add an address to the solver
129 GAS_solver_address_add s_add;
132 * Update address in solver
134 GAS_solver_address_update s_update;
137 * Get address from solver
139 GAS_solver_get_preferred_address s_get;
142 * Delete address in solver
144 GAS_solver_address_delete s_del;
147 * Change preference for quality in solver
149 GAS_solver_address_change_preference s_pref;
154 GAS_solver_done s_done;
161 struct GAS_Addresses_Handle *handle;
165 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
167 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
168 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
171 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
172 ats[0].value = aa->atsp_utilization_out.value__;
173 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
174 ats[1].value = aa->atsp_utilization_in.value__;
175 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
176 ats[2].value = ntohl(aa->atsp_network_type);
177 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
178 ats[3].value = ntohl(aa->atsp_latency.rel_value);
179 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
180 ats[4].value = ntohl(aa->atsp_distance);
181 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
182 ats[5].value = ntohl (aa->atsp_cost_wan);
183 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
184 ats[6].value = ntohl (aa->atsp_cost_lan);
185 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
186 ats[7].value = ntohl (aa->atsp_cost_wlan);
191 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
193 struct ATS_Address *dest)
197 for (i = 0; i < ats_count; i++)
198 switch (ntohl (src[i].type))
200 case GNUNET_ATS_UTILIZATION_UP:
201 dest->atsp_utilization_out.value__ = src[i].value;
204 case GNUNET_ATS_UTILIZATION_DOWN:
205 dest->atsp_utilization_in.value__ = src[i].value;
208 case GNUNET_ATS_QUALITY_NET_DELAY:
209 dest->atsp_latency.rel_value = ntohl (src[i].value);
212 case GNUNET_ATS_QUALITY_NET_DISTANCE:
213 dest->atsp_distance = ntohl (src[i].value);
216 case GNUNET_ATS_COST_WAN:
217 dest->atsp_cost_wan = ntohl (src[i].value);
220 case GNUNET_ATS_COST_LAN:
221 dest->atsp_cost_lan = ntohl (src[i].value);
224 case GNUNET_ATS_COST_WLAN:
225 dest->atsp_cost_wlan = ntohl (src[i].value);
228 case GNUNET_ATS_NETWORK_TYPE:
229 dest->atsp_network_type = ntohl (src[i].value);
232 case GNUNET_ATS_ARRAY_TERMINATOR:
235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
236 "Received unsupported ATS type %u\n", ntohl (src[i].type));
244 * Free the given address
245 * @param addr address to destroy
248 free_address (struct ATS_Address *addr)
250 GNUNET_free (addr->plugin);
255 * Create a ATS_address with the given information
257 * @param plugin_name plugin
258 * @param plugin_addr address
259 * @param plugin_addr_len address length
260 * @param session_id session
261 * @return the ATS_Address
263 static struct ATS_Address *
264 create_address (const struct GNUNET_PeerIdentity *peer,
265 const char *plugin_name,
266 const void *plugin_addr, size_t plugin_addr_len,
269 struct ATS_Address *aa = NULL;
271 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
273 aa->addr_len = plugin_addr_len;
275 memcpy (&aa[1], plugin_addr, plugin_addr_len);
276 aa->plugin = GNUNET_strdup (plugin_name);
277 aa->session_id = session_id;
278 aa->active = GNUNET_NO;
279 aa->solver_information = NULL;
280 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
281 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
287 * Destroy the given address.
289 * @param addr address to destroy
290 * @return GNUNET_YES if bandwidth allocations should be recalcualted
293 destroy_address (struct ATS_Address *addr)
298 GNUNET_assert (GNUNET_YES ==
299 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
300 &addr->peer.hashPubKey,
303 handle->s_del (handle->solver, handle->addresses, addr);
309 struct CompareAddressContext
311 const struct ATS_Address *search;
313 /* exact_address != NULL if address and session is equal */
314 struct ATS_Address *exact_address;
315 /* exact_address != NULL if address and session is 0 */
316 struct ATS_Address *base_address;
321 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
323 struct CompareAddressContext *cac = cls;
324 struct ATS_Address *aa = value;
326 /* Find an matching exact address:
329 * aa->addr_len == cac->search->addr_len
330 * aa->plugin == cac->search->plugin
331 * aa->addr == cac->search->addr
332 * aa->session == cac->search->session
334 * return as exact address
336 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
338 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
339 cac->exact_address = aa;
342 /* Find an matching base address:
346 * aa->session_id == 0
349 * aa->addr_len == cac->search->addr_len
350 * aa->plugin == cac->search->plugin
351 * aa->addr == cac->search->addr
353 * return as base address
355 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
357 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
358 cac->base_address = aa;
361 /* Find an matching exact address based on session:
365 * cac->search->addr_len == 0
368 * aa->plugin == cac->search->plugin
369 * aa->session_id == cac->search->session_id
371 * return as exact address
373 if (0 == cac->search->addr_len)
375 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
376 cac->exact_address = aa;
379 if (cac->exact_address == NULL)
380 return GNUNET_YES; /* Continue iteration to find exact address */
382 return GNUNET_NO; /* Stop iteration since we have an exact address */
387 * Find an existing equivalent address record.
388 * Compares by peer identity and network address OR by session ID
389 * (one of the two must match).
391 * @param peer peer to lookup addresses for
392 * @param addr existing address record
393 * @return existing address record, NULL for none
396 find_address (const struct GNUNET_PeerIdentity *peer,
397 const struct ATS_Address *addr)
399 struct CompareAddressContext cac;
401 cac.exact_address = NULL;
402 cac.base_address = NULL;
404 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
405 &compare_address_it, &cac);
407 if (cac.exact_address == NULL)
408 return cac.base_address;
409 return cac.exact_address;
413 static struct ATS_Address *
414 lookup_address (const struct GNUNET_PeerIdentity *peer,
415 const char *plugin_name, const void *plugin_addr,
416 size_t plugin_addr_len, uint32_t session_id,
417 const struct GNUNET_ATS_Information *atsi,
420 struct ATS_Address *aa;
421 struct ATS_Address *old;
423 aa = create_address (peer,
425 plugin_addr, plugin_addr_len,
428 /* Get existing address or address with session == 0 */
429 old = find_address (peer, aa);
435 else if (old->session_id != session_id)
444 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
445 const char *plugin_name, const void *plugin_addr,
446 size_t plugin_addr_len, uint32_t session_id,
447 const struct GNUNET_ATS_Information *atsi,
450 struct ATS_Address *aa;
451 struct ATS_Address *old;
452 unsigned int ats_res;
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Received `%s' for peer `%s'\n",
459 if (GNUNET_NO == handle->running)
462 GNUNET_assert (NULL != handle->addresses);
464 aa = create_address (peer,
466 plugin_addr, plugin_addr_len,
468 aa->solver_information = NULL;
469 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "While adding address: had %u ATS elements to add, could only add %u\n",
473 atsi_count, ats_res);
476 /* Get existing address or address with session == 0 */
477 old = find_address (peer, aa);
480 /* We have a new address */
481 GNUNET_assert (GNUNET_OK ==
482 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
483 &peer->hashPubKey, aa,
484 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
486 GNUNET_i2s (peer), session_id, aa);
487 /* Tell solver about update */
488 handle->s_add (handle->solver, handle->addresses, aa);
492 if (old->session_id != 0)
494 /* This address and session is already existing */
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
496 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
497 GNUNET_i2s (peer), plugin_name, session_id);
502 /* We have an address without an session, update this address */
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Updated existing address for peer `%s' %p with new session %u\n",
505 GNUNET_i2s (peer), old, session_id);
506 old->session_id = session_id;
507 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, old)))
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "While updating address: had %u ATS elements to add, could only add %u\n",
511 atsi_count, ats_res);
513 GNUNET_free (aa->plugin);
515 handle->s_add (handle->solver, handle->addresses, old);
520 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
521 const char *plugin_name, const void *plugin_addr,
522 size_t plugin_addr_len, uint32_t session_id,
523 const struct GNUNET_ATS_Information *atsi,
526 struct ATS_Address *old;
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "Received `%s' for peer `%s'\n",
534 if (GNUNET_NO == handle->running)
537 GNUNET_assert (NULL != handle->addresses);
539 /* Get existing address */
540 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
541 session_id, atsi, atsi_count);
544 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
545 GNUNET_i2s (peer), plugin_name, session_id);
550 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, old)))
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "While adding address: had %u ATS elements to add, could only add %u\n",
554 atsi_count, ats_res);
557 /* Tell solver about update */
558 handle->s_update (handle->solver, handle->addresses, old);
565 * If session != 0, just the session is deleted, the address itself still exists
566 * If session == 0, remove full address
567 * If session == 0 and addrlen == 0, destroy inbound address
571 * @param value the 'struct ATS_Address'
572 * @return GNUNET_OK (continue to iterate)
575 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
577 const struct ATS_Address *info = cls;
578 struct ATS_Address *aa = value;
581 memcmp (&aa->peer, &info->peer,
582 sizeof (struct GNUNET_PeerIdentity)));
583 /* session == 0, remove full address */
584 if ((info->session_id == 0) && (0 == strcmp (info->plugin, aa->plugin)) &&
585 (aa->addr_len == info->addr_len) &&
586 (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Deleting address for peer `%s': `%s' %u\n",
591 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
593 destroy_address (aa);
594 // FIXME if (GNUNET_YES == destroy_address (aa))recalculate_assigned_bw ();
597 /* session != 0, just remove session */
598 if (aa->session_id != info->session_id)
599 return GNUNET_OK; /* irrelevant */
600 if (aa->session_id != 0)
601 GNUNET_break (0 == strcmp (info->plugin, aa->plugin));
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Deleting session for peer `%s': `%s' %u\n",
605 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
608 if (GNUNET_YES == aa->active)
610 aa->active = GNUNET_NO;
611 //FIXME recalculate_assigned_bw ();
614 /* session == 0 and addrlen == 0 : destroy address */
615 if (aa->addr_len == 0)
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Deleting session and address for peer `%s': `%s' %u\n",
619 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
620 (void) destroy_address (aa);
624 /* session was set to 0, update address */
626 if (handle->ats_mode == MODE_MLP)
627 GAS_mlp_address_update (handle->solver, handle->addresses, aa);
636 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
637 const char *plugin_name, const void *plugin_addr,
638 size_t plugin_addr_len, uint32_t session_id)
640 struct ATS_Address *aa;
641 struct ATS_Address *old;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Received `%s' for peer `%s'\n",
648 if (GNUNET_NO == handle->running)
651 /* Get existing address */
652 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
653 session_id, NULL, 0);
656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
657 GNUNET_i2s (peer), plugin_name, session_id);
661 GNUNET_break (0 < strlen (plugin_name));
662 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
663 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
664 &destroy_by_session_id, aa);
670 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
671 const char *plugin_name, const void *plugin_addr,
672 size_t plugin_addr_len, uint32_t session_id, int in_use)
674 struct ATS_Address *old;
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "Received `%s' for peer `%s'\n",
681 if (GNUNET_NO == handle->running)
682 return GNUNET_SYSERR;
684 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
687 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
688 "Trying to set unknown address `%s', %s %u %s \n",
690 plugin_name, session_id,
691 (GNUNET_NO == in_use) ? "NO" : "YES");
693 return GNUNET_SYSERR;
695 if (old->used == in_use)
698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
699 "Address in use called multiple times for peer `%s': %s -> %s \n",
701 (GNUNET_NO == old->used) ? "NO" : "YES",
702 (GNUNET_NO == in_use) ? "NO" : "YES");
703 return GNUNET_SYSERR;
707 /* Tell solver about update */
708 handle->s_update (handle->solver, handle->addresses, old);
715 * Cancel address suggestions for a peer
717 * @param peer the respective peer
720 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
722 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
725 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
729 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
737 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
740 GAS_addresses_handle_backoff_reset (peer);
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
743 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
749 * Add an address suggestions for a peer
751 * @param peer the respective peer
754 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
756 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
757 struct ATS_Address *aa;
758 struct GNUNET_ATS_Information *ats;
759 unsigned int ats_count;
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Received `%s' for peer `%s'\n",
766 if (GNUNET_NO == handle->running)
770 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
771 break; /* already suggesting */
776 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
778 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
781 /* Get prefered address from solver */
782 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
793 ats_count = assemble_ats_information (aa, &ats);
794 GAS_scheduling_transmit_address_suggestion (peer,
796 aa->addr, aa->addr_len,
802 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
803 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
806 "Address %p ready for suggestion, block interval now %llu \n",
807 aa, aa->block_interval);
814 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
816 struct ATS_Address *aa = value;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Resetting interval for peer `%s' address %p from %llu to 0\n",
820 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
822 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
823 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
829 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Received `%s' for peer `%s'\n",
836 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
844 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
845 enum GNUNET_ATS_PreferenceKind kind,
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Received `%s' for peer `%s'\n",
853 if (GNUNET_NO == handle->running)
856 /* Tell solver about update */
857 handle->s_pref (handle->solver, peer, kind, score);
861 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
863 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
864 char * entry_in = NULL;
865 char * entry_out = NULL;
866 char * quota_out_str;
870 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
875 case GNUNET_ATS_NET_UNSPECIFIED:
876 entry_out = "UNSPECIFIED_QUOTA_OUT";
877 entry_in = "UNSPECIFIED_QUOTA_IN";
879 case GNUNET_ATS_NET_LOOPBACK:
880 entry_out = "LOOPBACK_QUOTA_OUT";
881 entry_in = "LOOPBACK_QUOTA_IN";
883 case GNUNET_ATS_NET_LAN:
884 entry_out = "LAN_QUOTA_OUT";
885 entry_in = "LAN_QUOTA_IN";
887 case GNUNET_ATS_NET_WAN:
888 entry_out = "WAN_QUOTA_OUT";
889 entry_in = "WAN_QUOTA_IN";
891 case GNUNET_ATS_NET_WLAN:
892 entry_out = "WLAN_QUOTA_OUT";
893 entry_in = "WLAN_QUOTA_IN";
899 if ((entry_in == NULL) || (entry_out == NULL))
903 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
905 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
906 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
907 out_dest[c] = UINT32_MAX;
909 GNUNET_free (quota_out_str);
910 quota_out_str = NULL;
912 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
913 out_dest[c] = UINT32_MAX;
915 out_dest[c] = UINT32_MAX;
918 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
920 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
921 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
922 in_dest[c] = UINT32_MAX;
924 GNUNET_free (quota_in_str);
927 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
929 in_dest[c] = UINT32_MAX;
933 in_dest[c] = UINT32_MAX;
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
938 return GNUNET_ATS_NetworkTypeCount;
943 bandwidth_changed_cb (struct ATS_Address *address)
945 struct GAS_Addresses_Suggestion_Requests *cur;
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
947 struct GNUNET_ATS_Information *ats;
948 unsigned int ats_count;
950 cur = handle->r_head;
953 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
954 break; /* we have an address request pending*/
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
967 ats_count = assemble_ats_information (address, &ats);
968 GAS_scheduling_transmit_address_suggestion (&address->peer,
970 address->addr, address->addr_len,
973 address->assigned_bw_out,
974 address->assigned_bw_in);
980 * Initialize address subsystem.
982 * @param cfg configuration to use
983 * @param stats the statistics handle to use
985 struct GAS_Addresses_Handle *
986 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
987 const struct GNUNET_STATISTICS_Handle *stats)
989 struct GAS_Addresses_Handle *ah;
990 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
991 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
992 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
997 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
999 handle->running = GNUNET_NO;
1001 /* Initialize the addresses database */
1002 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1003 GNUNET_assert (NULL != ah->addresses);
1005 /* Figure out configured solution method */
1006 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1009 ah->ats_mode = MODE_SIMPLISTIC;
1013 for (c = 0; c < strlen (mode_str); c++)
1014 mode_str[c] = toupper (mode_str[c]);
1015 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1017 ah->ats_mode = MODE_SIMPLISTIC;
1019 else if (0 == strcmp (mode_str, "MLP"))
1021 ah->ats_mode = MODE_MLP;
1023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1024 ah->ats_mode = MODE_SIMPLISTIC;
1029 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1030 ah->ats_mode = MODE_SIMPLISTIC;
1032 GNUNET_free (mode_str);
1034 /* Start configured solution method */
1035 switch (ah->ats_mode)
1038 /* Init the MLP solver with default values */
1040 ah->ats_mode = MODE_MLP;
1041 ah->s_init = &GAS_mlp_init;
1042 ah->s_add = &GAS_mlp_address_add;
1043 ah->s_update = &GAS_mlp_address_update;
1044 ah->s_get = &GAS_mlp_get_preferred_address;
1045 ah->s_pref = &GAS_mlp_address_change_preference;
1046 ah->s_del = &GAS_mlp_address_delete;
1047 ah->s_done = &GAS_mlp_done;
1053 case MODE_SIMPLISTIC:
1054 /* Init the simplistic solver with default values */
1055 ah->ats_mode = MODE_SIMPLISTIC;
1056 ah->s_init = &GAS_simplistic_init;
1057 ah->s_add = &GAS_simplistic_address_add;
1058 ah->s_update = &GAS_simplistic_address_update;
1059 ah->s_get = &GAS_simplistic_get_preferred_address;
1060 ah->s_pref = &GAS_simplistic_address_change_preference;
1061 ah->s_del = &GAS_simplistic_address_delete;
1062 ah->s_done = &GAS_simplistic_done;
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1070 GNUNET_assert (NULL != ah->s_init);
1071 GNUNET_assert (NULL != ah->s_add);
1072 GNUNET_assert (NULL != ah->s_update);
1073 GNUNET_assert (NULL != ah->s_get);
1074 GNUNET_assert (NULL != ah->s_pref);
1075 GNUNET_assert (NULL != ah->s_del);
1076 GNUNET_assert (NULL != ah->s_done);
1078 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1080 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb);
1081 if (NULL == ah->solver)
1083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1088 /* up and running */
1089 ah->running = GNUNET_YES;
1095 * Free memory of address.
1098 * @param key peer identity (unused)
1099 * @param value the 'struct ATS_Address' to free
1100 * @return GNUNET_OK (continue to iterate)
1103 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1105 struct ATS_Address *aa = value;
1107 destroy_address (aa);
1113 GAS_addresses_destroy_all ()
1115 if (GNUNET_NO == handle->running)
1118 if (handle->addresses != NULL)
1119 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, NULL);
1124 * Shutdown address subsystem.
1127 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1129 struct GAS_Addresses_Suggestion_Requests *cur;
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Shutting down addresses\n");
1133 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 while (NULL != (cur = handle->r_head))
1140 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1143 handle->s_done (handle->solver);
1144 GNUNET_free (handle);
1145 /* Stop configured solution method */
1149 struct PeerIteratorContext
1151 GNUNET_ATS_Peer_Iterator it;
1153 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1158 const struct GNUNET_HashCode * key,
1161 struct PeerIteratorContext *ip_ctx = cls;
1162 struct GNUNET_PeerIdentity tmp;
1164 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1166 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1167 tmp.hashPubKey = (*key);
1168 ip_ctx->it (ip_ctx->it_cls, &tmp);
1175 * Return all peers currently known to ATS
1177 * @param p_it the iterator to call for every peer, callbach with id == NULL
1179 * @param p_it_cls the closure for the iterator
1182 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1184 struct PeerIteratorContext ip_ctx;
1189 GNUNET_assert (NULL != handle->addresses);
1191 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1195 ip_ctx.it_cls = p_it_cls;
1196 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1197 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1198 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1200 p_it (p_it_cls, NULL);
1203 struct PeerInfoIteratorContext
1205 GNUNET_ATS_PeerInfo_Iterator it;
1211 peerinfo_it (void *cls,
1212 const struct GNUNET_HashCode * key,
1215 struct PeerInfoIteratorContext *pi_ctx = cls;
1216 struct ATS_Address *addr = (struct ATS_Address *) value;
1217 struct GNUNET_ATS_Information *ats;
1220 if (NULL != pi_ctx->it)
1222 ats_count = assemble_ats_information (addr, &ats);
1224 pi_ctx->it (pi_ctx->it_cls,
1227 addr->addr, addr->addr_len,
1230 addr->assigned_bw_out,
1231 addr->assigned_bw_in);
1239 * Return all peers currently known to ATS
1241 * @param peer the respective peer
1242 * @param pi_it the iterator to call for every peer
1243 * @param pi_it_cls the closure for the iterator
1246 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1248 struct PeerInfoIteratorContext pi_ctx;
1249 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1250 GNUNET_assert (NULL != peer);
1251 GNUNET_assert (NULL != handle->addresses);
1253 return; /* does not make sense without callback */
1255 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1257 pi_ctx.it_cls = pi_it_cls;
1259 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1262 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1267 /* end of gnunet-service-ats_addresses.c */