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 * Handle for ATS address component
91 struct GAS_Addresses_Suggestion_Requests
93 struct GAS_Addresses_Suggestion_Requests *next;
94 struct GAS_Addresses_Suggestion_Requests *prev;
96 struct GNUNET_PeerIdentity id;
100 * Handle for ATS address component
102 struct GAS_Addresses_Handle
105 * A multihashmap to store all addresses
107 struct GNUNET_CONTAINER_MultiHashMap *addresses;
110 * Configure WAN quota in
112 unsigned long long wan_quota_in;
115 * Configure WAN quota out
117 unsigned long long wan_quota_out;
120 * Is ATS addresses running
125 * Configured ATS solver
135 * Address suggestion requests DLL head
137 struct GAS_Addresses_Suggestion_Requests *r_head;
140 * Address suggestion requests DLL tail
142 struct GAS_Addresses_Suggestion_Requests *r_tail;
144 /* Solver functions */
149 GAS_solver_init s_init;
152 * Add an address to the solver
154 GAS_solver_address_add s_add;
157 * Update address in solver
159 GAS_solver_address_update s_update;
162 * Get address from solver
164 GAS_solver_get_preferred_address s_get;
167 * Delete address in solver
169 GAS_solver_address_delete s_del;
172 * Change preference for quality in solver
174 GAS_solver_address_change_preference s_pref;
179 GAS_solver_done s_done;
186 struct GAS_Addresses_Handle *handle;
190 assemble_ats_information (const struct ATS_Address *aa, struct GNUNET_ATS_Information **dest)
192 unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
193 struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
196 ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
197 ats[0].value = aa->atsp_utilization_out.value__;
198 ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
199 ats[1].value = aa->atsp_utilization_in.value__;
200 ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
201 ats[2].value = ntohl(aa->atsp_network_type);
202 ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
203 ats[3].value = ntohl(aa->atsp_latency.rel_value);
204 ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
205 ats[4].value = ntohl(aa->atsp_distance);
206 ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
207 ats[5].value = ntohl (aa->atsp_cost_wan);
208 ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
209 ats[6].value = ntohl (aa->atsp_cost_lan);
210 ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
211 ats[7].value = ntohl (aa->atsp_cost_wlan);
216 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
218 struct ATS_Address *dest)
222 for (i = 0; i < ats_count; i++)
223 switch (ntohl (src[i].type))
225 case GNUNET_ATS_UTILIZATION_UP:
226 dest->atsp_utilization_out.value__ = src[i].value;
229 case GNUNET_ATS_UTILIZATION_DOWN:
230 dest->atsp_utilization_in.value__ = src[i].value;
233 case GNUNET_ATS_QUALITY_NET_DELAY:
234 dest->atsp_latency.rel_value = ntohl (src[i].value);
237 case GNUNET_ATS_QUALITY_NET_DISTANCE:
238 dest->atsp_distance = ntohl (src[i].value);
241 case GNUNET_ATS_COST_WAN:
242 dest->atsp_cost_wan = ntohl (src[i].value);
245 case GNUNET_ATS_COST_LAN:
246 dest->atsp_cost_lan = ntohl (src[i].value);
249 case GNUNET_ATS_COST_WLAN:
250 dest->atsp_cost_wlan = ntohl (src[i].value);
253 case GNUNET_ATS_NETWORK_TYPE:
254 dest->atsp_network_type = ntohl (src[i].value);
257 case GNUNET_ATS_ARRAY_TERMINATOR:
260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
261 "Received unsupported ATS type %u\n", ntohl (src[i].type));
269 * Free the given address
270 * @param addr address to destroy
273 free_address (struct ATS_Address *addr)
275 GNUNET_free (addr->plugin);
280 * Create a ATS_address with the given information
282 * @param plugin_name plugin
283 * @param plugin_addr address
284 * @param plugin_addr_len address length
285 * @param session_id session
286 * @return the ATS_Address
288 static struct ATS_Address *
289 create_address (const struct GNUNET_PeerIdentity *peer,
290 const char *plugin_name,
291 const void *plugin_addr, size_t plugin_addr_len,
294 struct ATS_Address *aa = NULL;
296 aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
298 aa->addr_len = plugin_addr_len;
300 memcpy (&aa[1], plugin_addr, plugin_addr_len);
301 aa->plugin = GNUNET_strdup (plugin_name);
302 aa->session_id = session_id;
303 aa->active = GNUNET_NO;
304 aa->solver_information = NULL;
305 aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
306 aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
312 * Destroy the given address.
314 * @param addr address to destroy
315 * @return GNUNET_YES if bandwidth allocations should be recalcualted
318 destroy_address (struct ATS_Address *addr)
323 GNUNET_assert (GNUNET_YES ==
324 GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
325 &addr->peer.hashPubKey,
332 struct CompareAddressContext
334 const struct ATS_Address *search;
336 /* exact_address != NULL if address and session is equal */
337 struct ATS_Address *exact_address;
338 /* exact_address != NULL if address and session is 0 */
339 struct ATS_Address *base_address;
344 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
346 struct CompareAddressContext *cac = cls;
347 struct ATS_Address *aa = value;
349 /* Find an matching exact address:
352 * aa->addr_len == cac->search->addr_len
353 * aa->plugin == cac->search->plugin
354 * aa->addr == cac->search->addr
355 * aa->session == cac->search->session
357 * return as exact address
359 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
361 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
362 cac->exact_address = aa;
365 /* Find an matching base address:
369 * aa->session_id == 0
372 * aa->addr_len == cac->search->addr_len
373 * aa->plugin == cac->search->plugin
374 * aa->addr == cac->search->addr
376 * return as base address
378 if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
380 if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
381 cac->base_address = aa;
384 /* Find an matching exact address based on session:
388 * cac->search->addr_len == 0
391 * aa->plugin == cac->search->plugin
392 * aa->session_id == cac->search->session_id
394 * return as exact address
396 if (0 == cac->search->addr_len)
398 if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
399 cac->exact_address = aa;
402 if (cac->exact_address == NULL)
403 return GNUNET_YES; /* Continue iteration to find exact address */
405 return GNUNET_NO; /* Stop iteration since we have an exact address */
410 * Find an existing equivalent address record.
411 * Compares by peer identity and network address OR 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
419 find_equivalent_address (const struct GNUNET_PeerIdentity *peer,
420 const struct ATS_Address *addr)
422 struct CompareAddressContext cac;
424 cac.exact_address = NULL;
425 cac.base_address = NULL;
427 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
428 &compare_address_it, &cac);
430 if (cac.exact_address == NULL)
431 return cac.base_address;
432 return cac.exact_address;
436 static struct ATS_Address *
437 lookup_address (const struct GNUNET_PeerIdentity *peer,
438 const char *plugin_name,
439 const void *plugin_addr,
440 size_t plugin_addr_len,
442 const struct GNUNET_ATS_Information *atsi,
445 struct ATS_Address *aa;
446 struct ATS_Address *ea;
448 aa = create_address (peer,
450 plugin_addr, plugin_addr_len,
453 /* Get existing address or address with session == 0 */
454 ea = find_equivalent_address (peer, aa);
460 else if (ea->session_id != session_id)
469 GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
470 const char *plugin_name, const void *plugin_addr,
471 size_t plugin_addr_len, uint32_t session_id,
472 const struct GNUNET_ATS_Information *atsi,
475 struct ATS_Address *aa;
476 struct ATS_Address *ea;
477 unsigned int ats_res;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Received `%s' for peer `%s'\n",
484 if (GNUNET_NO == handle->running)
487 GNUNET_assert (NULL != handle->addresses);
489 aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
491 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "While adding address: had %u ATS elements to add, could only add %u\n",
495 atsi_count, ats_res);
498 /* Get existing address or address with session == 0 */
499 ea = find_equivalent_address (peer, aa);
502 /* We have a new address */
503 GNUNET_assert (GNUNET_OK ==
504 GNUNET_CONTAINER_multihashmap_put (handle->addresses,
505 &peer->hashPubKey, aa,
506 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
508 GNUNET_i2s (peer), session_id, aa);
509 /* Tell solver about new address */
510 handle->s_add (handle->solver, handle->addresses, aa);
513 GNUNET_free (aa->plugin);
516 if (ea->session_id != 0)
518 /* This addresswith the same session is already existing */
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
520 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
521 GNUNET_i2s (peer), plugin_name, session_id);
526 /* We have an address without an session, update this address */
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Updated existing address for peer `%s' %p with new session %u\n",
529 GNUNET_i2s (peer), ea, session_id);
530 ea->session_id = session_id;
531 if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "While updating address: had %u ATS elements to add, could only add %u\n",
535 atsi_count, ats_res);
538 handle->s_update (handle->solver, handle->addresses, ea);
543 GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
544 const char *plugin_name, const void *plugin_addr,
545 size_t plugin_addr_len, uint32_t session_id,
546 const struct GNUNET_ATS_Information *atsi,
549 struct ATS_Address *aa;
552 if (GNUNET_NO == handle->running)
555 GNUNET_assert (NULL != handle->addresses);
557 /* Get existing address */
558 aa = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
559 session_id, atsi, atsi_count);
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
563 GNUNET_i2s (peer), plugin_name, session_id);
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Received `%s' for peer `%s' address \n",
571 GNUNET_i2s (peer), aa);
573 if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "While adding address: had %u ATS elements to add, could only add %u\n",
577 atsi_count, ats_res);
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Updated %u ATS elements for address %p\n",
583 /* Tell solver about update */
584 handle->s_update (handle->solver, handle->addresses, aa);
588 struct DestroyContext
590 struct ATS_Address *aa;
592 struct GAS_Addresses_Handle *handle;
595 * GNUNET_NO : full address
596 * GNUNET_YES : just session
605 * If session != 0, just the session is deleted, the address itself still exists
606 * If session == 0, remove full address
607 * If session == 0 and addrlen == 0, destroy inbound address
611 * @param value the 'struct ATS_Address'
612 * @return GNUNET_OK (continue to iterate)
615 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
617 struct DestroyContext *dc = cls;
618 struct GAS_Addresses_Handle *handle = dc->handle;
619 const struct ATS_Address *des = dc->aa;
620 struct ATS_Address *aa = value;
622 GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
623 sizeof (struct GNUNET_PeerIdentity)));
626 if (des->session_id == 0)
628 /* Session == 0, remove full address */
629 if ((0 == strcmp (des->plugin, aa->plugin)) &&
630 (aa->addr_len == des->addr_len) &&
631 (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Deleting full address for peer `%s' session %u %p\n",
636 GNUNET_i2s (&aa->peer), aa->session_id, aa);
638 /* Notify solver about deletion */
639 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
640 destroy_address (aa);
641 dc->result = GNUNET_NO;
642 return GNUNET_OK; /* Continue iteration */
647 /* Session != 0, just remove session */
648 if (aa->session_id != des->session_id)
649 return GNUNET_OK; /* irrelevant */
651 if ((aa->session_id != 0) &&
652 (0 != strcmp (des->plugin, aa->plugin)))
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Different plugins during removal: `%s' vs `%s' \n",
656 des->plugin, aa->plugin);
661 if (aa->addr_len == 0)
663 /* Inbound connection died, delete full address */
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "Deleting inbound address for peer `%s': `%s' session %u\n",
666 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
668 /* Notify solver about deletion */
669 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
670 destroy_address (aa);
671 dc->result = GNUNET_NO;
672 return GNUNET_OK; /* Continue iteration */
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "Deleting session for peer `%s': `%s' %u\n",
679 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
680 /* Notify solver to delete session */
681 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
690 GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
691 const char *plugin_name, const void *plugin_addr,
692 size_t plugin_addr_len, uint32_t session_id)
694 struct ATS_Address *ea;
695 struct DestroyContext dc;
697 if (GNUNET_NO == handle->running)
700 /* Get existing address */
701 ea = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
702 session_id, NULL, 0);
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Received `%s' for peer `%s' address %p session %u\n",
707 GNUNET_i2s (peer), ea, session_id);
711 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
712 GNUNET_i2s (peer), plugin_name, session_id);
716 GNUNET_break (0 < strlen (plugin_name));
718 dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
720 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
721 &destroy_by_session_id, &dc);
722 free_address (dc.aa);
727 GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
728 const char *plugin_name, const void *plugin_addr,
729 size_t plugin_addr_len, uint32_t session_id, int in_use)
731 struct ATS_Address *old;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "Received `%s' for peer `%s'\n",
738 if (GNUNET_NO == handle->running)
739 return GNUNET_SYSERR;
741 old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
745 "Trying to set unknown address `%s', %s %u %s \n",
747 plugin_name, session_id,
748 (GNUNET_NO == in_use) ? "NO" : "YES");
750 return GNUNET_SYSERR;
752 if (old->used == in_use)
755 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
756 "Address in use called multiple times for peer `%s': %s -> %s \n",
758 (GNUNET_NO == old->used) ? "NO" : "YES",
759 (GNUNET_NO == in_use) ? "NO" : "YES");
760 return GNUNET_SYSERR;
764 /* Tell solver about update */
765 handle->s_update (handle->solver, handle->addresses, old);
772 * Cancel address suggestions for a peer
774 * @param peer the respective peer
777 GAS_addresses_request_address_cancel (const struct GNUNET_PeerIdentity *peer)
779 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
786 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
794 "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
797 GAS_addresses_handle_backoff_reset (peer);
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
800 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
806 * Add an address suggestions for a peer
808 * @param peer the respective peer
811 GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
813 struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
814 struct ATS_Address *aa;
815 struct GNUNET_ATS_Information *ats;
816 unsigned int ats_count;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Received `%s' for peer `%s'\n",
823 if (GNUNET_NO == handle->running)
827 if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
828 break; /* already suggesting */
833 cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
835 GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
838 /* Get prefered address from solver */
839 aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
850 ats_count = assemble_ats_information (aa, &ats);
851 GAS_scheduling_transmit_address_suggestion (peer,
853 aa->addr, aa->addr_len,
859 aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
860 aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Address %p ready for suggestion, block interval now %llu \n",
864 aa, aa->block_interval);
871 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
873 struct ATS_Address *aa = value;
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Resetting interval for peer `%s' address %p from %llu to 0\n",
877 GNUNET_i2s (&aa->peer), aa, aa->block_interval);
879 aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
880 aa->block_interval = GNUNET_TIME_UNIT_ZERO;
886 GAS_addresses_handle_backoff_reset (const struct GNUNET_PeerIdentity *peer)
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Received `%s' for peer `%s'\n",
893 GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
901 GAS_addresses_change_preference (const struct GNUNET_PeerIdentity *peer,
902 enum GNUNET_ATS_PreferenceKind kind,
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Received `%s' for peer `%s'\n",
910 if (GNUNET_NO == handle->running)
913 /* Tell solver about update */
914 handle->s_pref (handle->solver, peer, kind, score);
918 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
920 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
921 char * entry_in = NULL;
922 char * entry_out = NULL;
923 char * quota_out_str;
927 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
932 case GNUNET_ATS_NET_UNSPECIFIED:
933 entry_out = "UNSPECIFIED_QUOTA_OUT";
934 entry_in = "UNSPECIFIED_QUOTA_IN";
936 case GNUNET_ATS_NET_LOOPBACK:
937 entry_out = "LOOPBACK_QUOTA_OUT";
938 entry_in = "LOOPBACK_QUOTA_IN";
940 case GNUNET_ATS_NET_LAN:
941 entry_out = "LAN_QUOTA_OUT";
942 entry_in = "LAN_QUOTA_IN";
944 case GNUNET_ATS_NET_WAN:
945 entry_out = "WAN_QUOTA_OUT";
946 entry_in = "WAN_QUOTA_IN";
948 case GNUNET_ATS_NET_WLAN:
949 entry_out = "WLAN_QUOTA_OUT";
950 entry_in = "WLAN_QUOTA_IN";
956 if ((entry_in == NULL) || (entry_out == NULL))
960 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
962 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
963 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
964 out_dest[c] = UINT32_MAX;
966 GNUNET_free (quota_out_str);
967 quota_out_str = NULL;
969 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
970 out_dest[c] = UINT32_MAX;
972 out_dest[c] = UINT32_MAX;
975 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
977 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
978 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
979 in_dest[c] = UINT32_MAX;
981 GNUNET_free (quota_in_str);
984 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
986 in_dest[c] = UINT32_MAX;
990 in_dest[c] = UINT32_MAX;
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
995 return GNUNET_ATS_NetworkTypeCount;
1000 bandwidth_changed_cb (struct ATS_Address *address)
1002 struct GAS_Addresses_Suggestion_Requests *cur;
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1004 struct GNUNET_ATS_Information *ats;
1005 unsigned int ats_count;
1007 cur = handle->r_head;
1010 if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1011 break; /* we have an address request pending*/
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1024 ats_count = assemble_ats_information (address, &ats);
1025 GAS_scheduling_transmit_address_suggestion (&address->peer,
1027 address->addr, address->addr_len,
1028 address->session_id,
1030 address->assigned_bw_out,
1031 address->assigned_bw_in);
1037 * Initialize address subsystem.
1039 * @param cfg configuration to use
1040 * @param stats the statistics handle to use
1042 struct GAS_Addresses_Handle *
1043 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1044 const struct GNUNET_STATISTICS_Handle *stats)
1046 struct GAS_Addresses_Handle *ah;
1047 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1048 unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
1049 unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
1054 ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1056 handle->running = GNUNET_NO;
1058 /* Initialize the addresses database */
1059 ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1060 GNUNET_assert (NULL != ah->addresses);
1062 /* Figure out configured solution method */
1063 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1066 ah->ats_mode = MODE_SIMPLISTIC;
1070 for (c = 0; c < strlen (mode_str); c++)
1071 mode_str[c] = toupper (mode_str[c]);
1072 if (0 == strcmp (mode_str, "SIMPLISTIC"))
1074 ah->ats_mode = MODE_SIMPLISTIC;
1076 else if (0 == strcmp (mode_str, "MLP"))
1078 ah->ats_mode = MODE_MLP;
1080 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1081 ah->ats_mode = MODE_SIMPLISTIC;
1086 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1087 ah->ats_mode = MODE_SIMPLISTIC;
1089 GNUNET_free (mode_str);
1091 /* Start configured solution method */
1092 switch (ah->ats_mode)
1095 /* Init the MLP solver with default values */
1097 ah->ats_mode = MODE_MLP;
1098 ah->s_init = &GAS_mlp_init;
1099 ah->s_add = &GAS_mlp_address_add;
1100 ah->s_update = &GAS_mlp_address_update;
1101 ah->s_get = &GAS_mlp_get_preferred_address;
1102 ah->s_pref = &GAS_mlp_address_change_preference;
1103 ah->s_del = &GAS_mlp_address_delete;
1104 ah->s_done = &GAS_mlp_done;
1110 case MODE_SIMPLISTIC:
1111 /* Init the simplistic solver with default values */
1112 ah->ats_mode = MODE_SIMPLISTIC;
1113 ah->s_init = &GAS_simplistic_init;
1114 ah->s_add = &GAS_simplistic_address_add;
1115 ah->s_update = &GAS_simplistic_address_update;
1116 ah->s_get = &GAS_simplistic_get_preferred_address;
1117 ah->s_pref = &GAS_simplistic_address_change_preference;
1118 ah->s_del = &GAS_simplistic_address_delete;
1119 ah->s_done = &GAS_simplistic_done;
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1127 GNUNET_assert (NULL != ah->s_init);
1128 GNUNET_assert (NULL != ah->s_add);
1129 GNUNET_assert (NULL != ah->s_update);
1130 GNUNET_assert (NULL != ah->s_get);
1131 GNUNET_assert (NULL != ah->s_pref);
1132 GNUNET_assert (NULL != ah->s_del);
1133 GNUNET_assert (NULL != ah->s_done);
1135 quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1137 ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb);
1138 if (NULL == ah->solver)
1140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1145 /* up and running */
1146 ah->running = GNUNET_YES;
1152 * Free memory of address.
1155 * @param key peer identity (unused)
1156 * @param value the 'struct ATS_Address' to free
1157 * @return GNUNET_OK (continue to iterate)
1160 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1162 struct GAS_Addresses_Handle *handle = cls;
1163 struct ATS_Address *aa = value;
1164 handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1165 destroy_address (aa);
1171 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1173 if (GNUNET_NO == handle->running)
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 if (handle->addresses != NULL)
1181 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1186 * Shutdown address subsystem.
1189 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1191 struct GAS_Addresses_Suggestion_Requests *cur;
1193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1194 "Shutting down addresses\n");
1195 GNUNET_assert (NULL != handle);
1196 GAS_addresses_destroy_all (handle);
1197 handle->running = GNUNET_NO;
1198 GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1199 handle->addresses = NULL;
1200 while (NULL != (cur = handle->r_head))
1202 GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1205 handle->s_done (handle->solver);
1206 GNUNET_free (handle);
1207 /* Stop configured solution method */
1211 struct PeerIteratorContext
1213 GNUNET_ATS_Peer_Iterator it;
1215 struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1220 const struct GNUNET_HashCode * key,
1223 struct PeerIteratorContext *ip_ctx = cls;
1224 struct GNUNET_PeerIdentity tmp;
1226 if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1228 GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1229 tmp.hashPubKey = (*key);
1230 ip_ctx->it (ip_ctx->it_cls, &tmp);
1237 * Return all peers currently known to ATS
1239 * @param p_it the iterator to call for every peer, callbach with id == NULL
1241 * @param p_it_cls the closure for the iterator
1244 GAS_addresses_iterate_peers (GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1246 struct PeerIteratorContext ip_ctx;
1251 GNUNET_assert (NULL != handle->addresses);
1253 size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1257 ip_ctx.it_cls = p_it_cls;
1258 ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1259 GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1260 GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1262 p_it (p_it_cls, NULL);
1265 struct PeerInfoIteratorContext
1267 GNUNET_ATS_PeerInfo_Iterator it;
1273 peerinfo_it (void *cls,
1274 const struct GNUNET_HashCode * key,
1277 struct PeerInfoIteratorContext *pi_ctx = cls;
1278 struct ATS_Address *addr = (struct ATS_Address *) value;
1279 struct GNUNET_ATS_Information *ats;
1282 if (NULL != pi_ctx->it)
1284 ats_count = assemble_ats_information (addr, &ats);
1286 pi_ctx->it (pi_ctx->it_cls,
1289 addr->addr, addr->addr_len,
1292 addr->assigned_bw_out,
1293 addr->assigned_bw_in);
1301 * Return all peers currently known to ATS
1303 * @param peer the respective peer
1304 * @param pi_it the iterator to call for every peer
1305 * @param pi_it_cls the closure for the iterator
1308 GAS_addresses_get_peer_info (const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it, void *pi_it_cls)
1310 struct PeerInfoIteratorContext pi_ctx;
1311 struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1312 GNUNET_assert (NULL != peer);
1313 GNUNET_assert (NULL != handle->addresses);
1315 return; /* does not make sense without callback */
1317 zero_bw = GNUNET_BANDWIDTH_value_init (0);
1319 pi_ctx.it_cls = pi_it_cls;
1321 GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1324 pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1329 /* end of gnunet-service-ats_addresses.c */