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_simplistic.h
23 * @brief ats simplistic ressource assignment
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet_statistics_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "ats-simplistic",__VA_ARGS__)
35 * ATS simplistic solver
37 * Assigns in and outbound bandwidth equally for all addresses in specific
38 * network type (WAN, LAN) based on configured in and outbound quota for this
41 * For each peer only a single is selected and marked as "active" in the address
46 * You have the networks WAN and LAN and quotas
47 * WAN_TOTAL_IN, WAN_TOTAL_OUT
48 * LAN_TOTAL_IN, LAN_TOTAL_OUT
50 * If you have x addresses in the network segment LAN, the quotas are
51 * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
53 * Quotas are automatically recalculated and reported back when addresses are
58 #define DEFAULT_PREFERENCE 1.0
59 #define MIN_UPDATE_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
62 * A handle for the simplistic solver
64 struct GAS_SIMPLISTIC_Handle
70 struct GNUNET_STATISTICS_Handle *stats;
73 * Total number of addresses for solver
75 unsigned int total_addresses;
78 * Number of active addresses for solver
80 unsigned int active_addresses;
85 struct Network *network_entries;
90 unsigned int networks;
95 GAS_bandwidth_changed_cb bw_changed;
100 void *bw_changed_cls;
102 struct GNUNET_CONTAINER_MultiHashMap *prefs;
104 struct PreferenceClient *pc_head;
105 struct PreferenceClient *pc_tail;
116 * Network description
121 * Total inbound quota
124 unsigned long long total_quota_in;
127 * Total outbound quota
130 unsigned long long total_quota_out;
133 * Number of active addresses for this network
135 unsigned int active_addresses;
138 * Number of total addresses for this network
140 unsigned int total_addresses;
143 * String for statistics total addresses
148 * String for statistics active addresses
152 struct AddressWrapper *head;
153 struct AddressWrapper *tail;
156 struct AddressWrapper
158 struct AddressWrapper *next;
159 struct AddressWrapper *prev;
161 struct ATS_Address *addr;
165 struct PreferencePeer
167 struct PreferencePeer *next;
168 struct PreferencePeer *prev;
169 struct GNUNET_PeerIdentity id;
171 float f[GNUNET_ATS_PreferenceCount];
172 float f_rel[GNUNET_ATS_PreferenceCount];
176 struct PreferenceClient
178 struct PreferenceClient *prev;
179 struct PreferenceClient *next;
182 float f_total[GNUNET_ATS_PreferenceCount];
184 struct PreferencePeer *p_head;
185 struct PreferencePeer *p_tail;
190 * Get the prefered address for a specific peer
192 * @param solver the solver handle
193 * @param addresses the address hashmap containing all addresses
194 * @param peer the identity of the peer
196 const struct ATS_Address *
197 GAS_simplistic_get_preferred_address (void *solver,
198 struct GNUNET_CONTAINER_MultiHashMap * addresses,
199 const struct GNUNET_PeerIdentity *peer);
202 * Init the simplistic problem solving component
205 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
206 * out_quota[i] contains outbound quota for network type i
207 * in_quota[i] contains inbound quota for network type i
210 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
211 * network[2] == GNUNET_ATS_NET_LAN
212 * out_quota[2] == 65353
213 * in_quota[2] == 65353
215 * @param cfg configuration handle
216 * @param stats the GNUNET_STATISTICS handle
217 * @param network array of GNUNET_ATS_NetworkType with length dest_length
218 * @param out_quota array of outbound quotas
219 * @param in_quota array of outbound quota
220 * @param dest_length array length for quota arrays
221 * @param bw_changed_cb callback for changed bandwidth amounts
222 * @param bw_changed_cb_cls cls for callback
223 * @return handle for the solver on success, NULL on fail
226 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
227 const struct GNUNET_STATISTICS_Handle *stats,
229 unsigned long long *out_quota,
230 unsigned long long *in_quota,
232 GAS_bandwidth_changed_cb bw_changed_cb,
233 void *bw_changed_cb_cls)
236 struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
237 struct Network * cur;
238 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
241 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
242 s->bw_changed = bw_changed_cb;
243 s->bw_changed_cls = bw_changed_cb_cls;
244 s->networks = dest_length;
245 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
246 s->active_addresses = 0;
247 s->total_addresses = 0;
248 s->prefs = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
250 for (c = 0; c < dest_length; c++)
252 cur = &s->network_entries[c];
253 cur->total_addresses = 0;
254 cur->active_addresses = 0;
255 cur->type = network[c];
256 cur->total_quota_in = in_quota[c];
257 cur->total_quota_out = out_quota[c];
258 cur->desc = net_str[c];
259 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
260 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
266 free_pref (void *cls,
267 const struct GNUNET_HashCode * key,
276 * Shutdown the simplistic problem solving component
278 * @param solver the respective handle to shutdown
281 GAS_simplistic_done (void *solver)
283 struct GAS_SIMPLISTIC_Handle *s = solver;
284 struct PreferenceClient *pc;
285 struct PreferenceClient *next_pc;
286 struct PreferencePeer *p;
287 struct PreferencePeer *next_p;
288 struct AddressWrapper *cur;
289 struct AddressWrapper *next;
291 GNUNET_assert (s != NULL);
293 for (c = 0; c < s->networks; c++)
295 if (s->network_entries[c].total_addresses > 0)
297 LOG (GNUNET_ERROR_TYPE_ERROR,
298 "Had %u addresses for network `%s' not deleted during shutdown\n",
299 s->network_entries[c].total_addresses,
300 s->network_entries[c].desc);
304 if (s->network_entries[c].active_addresses > 0)
306 LOG (GNUNET_ERROR_TYPE_ERROR,
307 "Had %u active addresses for network `%s' not deleted during shutdown\n",
308 s->network_entries[c].active_addresses,
309 s->network_entries[c].desc);
313 next = s->network_entries[c].head;
314 while (NULL != (cur = next))
317 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
318 s->network_entries[c].tail,
322 GNUNET_free (s->network_entries[c].stat_total);
323 GNUNET_free (s->network_entries[c].stat_active);
325 if (s->total_addresses > 0)
327 LOG (GNUNET_ERROR_TYPE_ERROR,
328 "Had %u addresses not deleted during shutdown\n",
332 if (s->active_addresses > 0)
334 LOG (GNUNET_ERROR_TYPE_ERROR,
335 "Had %u active addresses not deleted during shutdown\n",
336 s->active_addresses);
339 GNUNET_free (s->network_entries);
341 next_pc = s->pc_head;
342 while (NULL != (pc = next_pc))
345 GNUNET_CONTAINER_DLL_remove (s->pc_head, s->pc_tail, pc);
347 while (NULL != (p = next_p))
350 GNUNET_CONTAINER_DLL_remove (pc->p_head, pc->p_tail, p);
356 GNUNET_CONTAINER_multihashmap_iterate (s->prefs, &free_pref, NULL);
357 GNUNET_CONTAINER_multihashmap_destroy (s->prefs);
363 * Test if bandwidth is available in this network
365 * @param s the solver handle
366 * @param net the network type to update
367 * @return GNUNET_YES or GNUNET_NO
371 bw_available_in_network (struct Network *net)
373 unsigned int na = net->active_addresses + 1;
374 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
375 if (((net->total_quota_in / na) > min_bw) &&
376 ((net->total_quota_out / na) > min_bw))
378 LOG (GNUNET_ERROR_TYPE_DEBUG,
379 "Enough bandwidth available for %u active addresses in network `%s'\n",
385 LOG (GNUNET_ERROR_TYPE_DEBUG,
386 "Not enough bandwidth available for %u active addresses in network `%s'\n",
393 * Update the quotas for a network type
395 * @param s the solver handle
396 * @param net the network type to update
397 * @param address_except address excluded from notifcation, since we suggest
401 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
403 struct ATS_Address *address_except)
405 unsigned long long remaining_quota_in = 0;
406 unsigned long long quota_out_used = 0;
408 unsigned long long remaining_quota_out = 0;
409 unsigned long long quota_in_used = 0;
410 uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
411 double total_prefs; /* Important: has to be double not float due to precision */
412 double cur_pref; /* Important: has to be double not float due to precision */
413 double *t = NULL; /* Important: has to be double not float due to precision */
415 unsigned long long assigned_quota_in = 0;
416 unsigned long long assigned_quota_out = 0;
417 struct AddressWrapper *cur;
419 LOG (GNUNET_ERROR_TYPE_DEBUG,
420 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
421 net->desc, net->active_addresses, net->total_quota_in, net->total_quota_in);
423 if (net->active_addresses == 0)
424 return; /* no addresses to update */
428 * Assign every peer in network minimum Bandwidth
429 * Distribute bandwidth left according to preference
432 if ((net->active_addresses * min_bw) > net->total_quota_in)
437 if ((net->active_addresses * min_bw) > net->total_quota_out)
443 remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
444 remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
445 LOG (GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
446 remaining_quota_in, remaining_quota_out);
448 for (cur = net->head; NULL != cur; cur = cur->next)
450 if (GNUNET_YES == cur->addr->active)
452 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
454 total_prefs += DEFAULT_PREFERENCE;
461 for (cur = net->head; NULL != cur; cur = cur->next)
463 if (GNUNET_YES == cur->addr->active)
466 t = GNUNET_CONTAINER_multihashmap_get (s->prefs, &cur->addr->peer.hashPubKey);
468 cur_pref = DEFAULT_PREFERENCE;
471 assigned_quota_in = min_bw + ((cur_pref / total_prefs) * remaining_quota_in);
472 assigned_quota_out = min_bw + ((cur_pref / total_prefs) * remaining_quota_out);
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 "New quota for peer `%s' with preference (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
476 GNUNET_i2s (&cur->addr->peer),
477 cur_pref, total_prefs,
478 assigned_quota_in, assigned_quota_out);
482 assigned_quota_in = 0;
483 assigned_quota_out = 0;
486 quota_in_used += assigned_quota_in;
487 quota_out_used += assigned_quota_out;
488 /* Prevent overflow due to rounding errors */
489 if (assigned_quota_in > UINT32_MAX)
490 assigned_quota_in = UINT32_MAX;
491 if (assigned_quota_out > UINT32_MAX)
492 assigned_quota_out = UINT32_MAX;
494 /* Compare to current bandwidth assigned */
495 if ((assigned_quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
496 (assigned_quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
498 cur->addr->assigned_bw_in.value__ = htonl (assigned_quota_in);
499 cur->addr->assigned_bw_out.value__ = htonl (assigned_quota_out);
500 /* Notify on change */
501 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
502 s->bw_changed (s->bw_changed_cls, cur->addr);
506 LOG (GNUNET_ERROR_TYPE_DEBUG,
507 "Total bandwidth assigned is (in/out): %llu /%llu\n",
510 if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
512 LOG (GNUNET_ERROR_TYPE_ERROR,
513 "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
514 net->active_addresses,
516 net->total_quota_out);
518 if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
520 LOG (GNUNET_ERROR_TYPE_ERROR,
521 "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
522 net->active_addresses,
524 net->total_quota_in);
529 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
534 if (GNUNET_YES == total)
536 s->total_addresses ++;
537 net->total_addresses ++;
538 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
539 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
541 if (GNUNET_YES == active)
543 net->active_addresses ++;
544 s->active_addresses ++;
545 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
546 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
552 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
558 if (GNUNET_YES == total)
560 if (s->total_addresses < 1)
567 s->total_addresses --;
568 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
570 if (net->total_addresses < 1)
577 net->total_addresses --;
578 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
582 if (GNUNET_YES == active)
584 if (net->active_addresses < 1)
591 net->active_addresses --;
592 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
594 if (s->active_addresses < 1)
601 s->active_addresses --;
602 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
610 * Add a single address to the solve
612 * @param solver the solver Handle
613 * @param addresses the address hashmap containing all addresses
614 * @param address the address to add
617 GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
619 struct GAS_SIMPLISTIC_Handle *s = solver;
620 struct Network *net = NULL;
621 struct AddressWrapper *aw = NULL;
623 GNUNET_assert (NULL != s);
625 for (c = 0; c < s->networks; c++)
627 net = &s->network_entries[c];
628 if (address->atsp_network_type == net->type)
637 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
639 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
640 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
641 aw->addr->solver_information = net;
644 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
645 net->total_addresses,
646 net->active_addresses,
651 * Remove an address from the solver
653 * @param solver the solver handle
654 * @param addresses the address hashmap containing all addresses
655 * @param address the address to remove
656 * @param session_only delete only session not whole address
659 GAS_simplistic_address_delete (void *solver,
660 struct GNUNET_CONTAINER_MultiHashMap * addresses,
661 struct ATS_Address *address, int session_only)
663 struct GAS_SIMPLISTIC_Handle *s = solver;
665 struct AddressWrapper *aw;
667 /* Remove an adress completely, we have to:
668 * - Remove from specific network
669 * - Decrease number of total addresses
671 * - decrease number of active addreses
675 net = (struct Network *) address->solver_information;
677 if (GNUNET_NO == session_only)
679 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
680 (GNUNET_NO == address->active) ? "inactive" : "active",
681 address, GNUNET_i2s (&address->peer),
682 net->desc, net->total_addresses, net->active_addresses);
685 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
686 for (aw = net->head; NULL != aw; aw = aw->next)
688 if (aw->addr == address)
696 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
701 /* Remove session only: remove if active and update */
702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
703 (GNUNET_NO == address->active) ? "inactive" : "active",
704 address, GNUNET_i2s (&address->peer),
705 net->desc, net->total_addresses, net->active_addresses);
708 if (GNUNET_YES == address->active)
710 /* Address was active, remove from network and update quotas*/
711 address->active = GNUNET_NO;
712 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
714 update_quota_per_network (s, net, NULL);
716 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
717 net->total_addresses,
718 net->active_addresses,
723 static struct Network *
724 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
727 for (c = 0 ; c < s->networks; c++)
729 if (s->network_entries[c].type == type)
730 return &s->network_entries[c];
736 * Updates a single address in the solve
738 * @param solver the solver Handle
739 * @param addresses the address hashmap containing all addresses
740 * @param address the update address
741 * @param session the new session (if changed otherwise current)
742 * @param in_use the new address in use state (if changed otherwise current)
743 * @param atsi the latest ATS information
744 * @param atsi_count the atsi count
747 GAS_simplistic_address_update (void *solver,
748 struct GNUNET_CONTAINER_MultiHashMap *addresses,
749 struct ATS_Address *address,
752 const struct GNUNET_ATS_Information *atsi,
755 struct ATS_Address *new;
756 struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
760 int save_active = GNUNET_NO;
761 struct Network *new_net = NULL;
762 for (i = 0; i < atsi_count; i++)
764 type = ntohl (atsi[i].type);
765 value = ntohl (atsi[i].value);
768 case GNUNET_ATS_UTILIZATION_UP:
769 //if (address->atsp_utilization_out.value__ != atsi[i].value)
772 case GNUNET_ATS_UTILIZATION_DOWN:
773 //if (address->atsp_utilization_in.value__ != atsi[i].value)
776 case GNUNET_ATS_QUALITY_NET_DELAY:
777 //if (address->atsp_latency.rel_value != value)
780 case GNUNET_ATS_QUALITY_NET_DISTANCE:
781 //if (address->atsp_distance != value)
784 case GNUNET_ATS_COST_WAN:
785 //if (address->atsp_cost_wan != value)
788 case GNUNET_ATS_COST_LAN:
789 //if (address->atsp_cost_lan != value)
792 case GNUNET_ATS_COST_WLAN:
793 //if (address->atsp_cost_wlan != value)
796 case GNUNET_ATS_NETWORK_TYPE:
797 if (address->atsp_network_type != value)
800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
801 (GNUNET_YES == address->active) ? "active" : "inactive",
802 GNUNET_ATS_print_network_type(address->atsp_network_type),
803 GNUNET_ATS_print_network_type(value));
805 save_active = address->active;
806 /* remove from old network */
807 GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
809 /* set new network type */
810 address->atsp_network_type = value;
811 new_net = find_network (solver, value);
812 address->solver_information = new_net;
813 if (address->solver_information == NULL)
816 address->atsp_network_type = GNUNET_ATS_NET_UNSPECIFIED;
820 /* Add to new network and update*/
821 GAS_simplistic_address_add (solver, addresses, address);
822 if (GNUNET_YES == save_active)
824 /* check if bandwidth available in new network */
825 if (GNUNET_YES == (bw_available_in_network (new_net)))
827 /* Suggest updated address */
828 address->active = GNUNET_YES;
829 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
830 update_quota_per_network (solver, new_net, NULL);
834 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not enough bandwidth in new network, suggesting alternative address ..\n");
836 /* Set old address to zero bw */
837 address->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0);
838 address->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0);
839 s->bw_changed (s->bw_changed_cls, address);
841 /* Find new address to suggest since no bandwidth in network*/
842 new = (struct ATS_Address *) GAS_simplistic_get_preferred_address (s, addresses, &address->peer);
845 /* Have an alternative address to suggest */
846 s->bw_changed (s->bw_changed_cls, new);
853 case GNUNET_ATS_ARRAY_TERMINATOR:
856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
857 "Received unsupported ATS type %u\n", type);
864 if (address->session_id != session)
866 LOG (GNUNET_ERROR_TYPE_DEBUG,
867 "Session changed from %u to %u\n", address->session_id, session);
868 address->session_id = session;
870 if (address->used != in_use)
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "Usage changed from %u to %u\n", address->used, in_use);
874 address->used = in_use;
882 * Find a "good" address to use for a peer. If we already have an existing
883 * address, we stick to it. Otherwise, we pick by lowest distance and then
886 * @param cls the 'struct ATS_Address**' where we store the result
888 * @param value another 'struct ATS_Address*' to consider using
889 * @return GNUNET_OK (continue to iterate)
892 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
894 struct ATS_Address **previous_p = cls;
895 struct ATS_Address *current = (struct ATS_Address *) value;
896 struct ATS_Address *previous = *previous_p;
897 struct GNUNET_TIME_Absolute now;
898 struct Network *net = (struct Network *) current->solver_information;
900 now = GNUNET_TIME_absolute_get();
902 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
904 /* This address is blocked for suggestion */
905 LOG (GNUNET_ERROR_TYPE_DEBUG,
906 "Address %p blocked for suggestion for %llu ms \n",
908 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
912 if (GNUNET_NO == bw_available_in_network (net))
913 return GNUNET_OK; /* There's no bandwidth available in this network */
915 if (NULL != previous)
917 if ((0 == strcmp (previous->plugin, "tcp")) &&
918 (0 == strcmp (current->plugin, "tcp")))
920 if ((0 != previous->addr_len) &&
921 (0 == current->addr_len))
923 /* saved address was an outbound address, but we have an inbound address */
924 *previous_p = current;
927 if (0 == previous->addr_len)
929 /* saved address was an inbound address, so do not overwrite */
935 if (NULL == previous)
937 *previous_p = current;
940 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
941 (ntohl (current->assigned_bw_in.value__) > 0))
943 /* stick to existing connection */
944 *previous_p = current;
947 if (previous->atsp_distance > current->atsp_distance)
949 /* user shorter distance */
950 *previous_p = current;
953 if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
955 /* user lower latency */
956 *previous_p = current;
964 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
966 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
967 struct ATS_Address * aa = (struct ATS_Address *) value;
969 if (GNUNET_YES == aa->active)
973 /* should never happen */
974 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
983 static struct ATS_Address *
984 find_active_address (void *solver,
985 struct GNUNET_CONTAINER_MultiHashMap * addresses,
986 const struct GNUNET_PeerIdentity *peer)
988 struct ATS_Address * dest = NULL;
990 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
992 &find_active_address_it, &dest);
997 * Get the prefered address for a specific peer
999 * @param solver the solver handle
1000 * @param addresses the address hashmap containing all addresses
1001 * @param peer the identity of the peer
1003 const struct ATS_Address *
1004 GAS_simplistic_get_preferred_address (void *solver,
1005 struct GNUNET_CONTAINER_MultiHashMap * addresses,
1006 const struct GNUNET_PeerIdentity *peer)
1008 struct GAS_SIMPLISTIC_Handle *s = solver;
1009 struct Network *net_prev;
1010 struct Network *net_cur;
1011 struct ATS_Address *cur;
1012 struct ATS_Address *prev;
1014 GNUNET_assert (s != NULL);
1016 /* Get address with: stick to current address, lower distance, lower latency */
1017 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
1018 &find_address_it, &cur);
1021 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
1025 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
1026 (GNUNET_NO == cur->active) ? "inactive" : "active",
1027 cur, GNUNET_i2s (peer));
1028 net_cur = (struct Network *) cur->solver_information;
1029 if (GNUNET_YES == cur->active)
1031 /* This address was selected previously, so no need to update quotas */
1035 /* This address was not active, so we have to:
1037 * - mark previous active address as not active
1038 * - update quota for previous address network
1039 * - update quota for this address network
1042 prev = find_active_address (s, addresses, peer);
1045 net_prev = (struct Network *) prev->solver_information;
1046 prev->active = GNUNET_NO; /* No active any longer */
1047 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1048 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
1049 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
1050 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
1052 update_quota_per_network (s, net_prev, NULL);
1055 if (GNUNET_NO == (bw_available_in_network (cur->solver_information)))
1057 GNUNET_break (0); /* This should never happen*/
1061 cur->active = GNUNET_YES;
1062 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
1063 update_quota_per_network (s, net_cur, cur);
1069 * Changes the preferences for a peer in the problem
1071 * @param solver the solver handle
1072 * @param client the client with this preference
1073 * @param peer the peer to change the preference for
1074 * @param kind the kind to change the preference
1075 * @param score the score
1078 GAS_simplistic_address_change_preference (void *solver,
1080 const struct GNUNET_PeerIdentity *peer,
1081 enum GNUNET_ATS_PreferenceKind kind,
1084 static struct GNUNET_TIME_Absolute next_update;
1085 struct GAS_SIMPLISTIC_Handle *s = solver;
1086 struct PreferenceClient *cur;
1087 struct PreferencePeer *p;
1094 GNUNET_assert (NULL != solver);
1095 GNUNET_assert (NULL != client);
1096 GNUNET_assert (NULL != peer);
1098 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' %s %f\n",
1101 GNUNET_ATS_print_preference_type (kind),
1104 if (kind >= GNUNET_ATS_PreferenceCount)
1115 * Set of peers p_i in P
1116 * Set of preference kinds k
1117 * A preference value f_k_p_i with an unknown range
1120 * A client specific relative preference f_p_i_rel [1..2] for all peers
1122 * For every client c
1124 * For every preference kind k:
1126 * We remember for the preference f_p_i for each peer p_i.
1127 * We have a default preference value f_p_i = 0
1128 * We have a sum of all preferences f_t = sum (f_p_i)
1129 * So we can calculate a relative preference value fr_p_i:
1131 * f_k_p_i_rel = (f_t + f_p_i) / f_t
1132 * f_k_p_i_rel = [1..2], default 1.0
1134 * f_p_i_rel = sum (f_k_p_i_rel) / #k
1139 /* Find preference client */
1140 for (cur = s->pc_head; NULL != cur; cur = cur->next)
1142 if (client == cur->client)
1145 /* Not found: create new preference client */
1148 cur = GNUNET_malloc (sizeof (struct PreferenceClient));
1149 cur->client = client;
1150 GNUNET_CONTAINER_DLL_insert (s->pc_head, s->pc_tail, cur);
1153 /* Find entry for peer */
1154 for (p = cur->p_head; NULL != p; p = p->next)
1155 if (0 == memcmp (&p->id, peer, sizeof (p->id)))
1158 /* Not found: create new peer entry */
1161 p = GNUNET_malloc (sizeof (struct PreferencePeer));
1163 for (i = 0; i < GNUNET_ATS_PreferenceCount; i++)
1165 /* Default value per peer absolut preference for a quality:
1166 * No value set, so absolute preference 0 */
1168 /* Default value per peer relative preference for a quality: 1.0 */
1169 p->f_rel[i] = DEFAULT_PREFERENCE;
1171 GNUNET_CONTAINER_DLL_insert (cur->p_head, cur->p_tail, p);
1174 /* Update preference value according to type */
1176 case GNUNET_ATS_PREFERENCE_BANDWIDTH:
1177 case GNUNET_ATS_PREFERENCE_LATENCY:
1178 p->f[kind] = (p->f[kind] + score) / 2;
1180 case GNUNET_ATS_PREFERENCE_END:
1186 /* Recalcalculate total preference for this quality kind over all peers*/
1187 cur->f_total[kind] = 0;
1188 for (p = cur->p_head; NULL != p; p = p->next)
1189 cur->f_total[kind] += p->f[kind];
1191 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n",
1193 GNUNET_ATS_print_preference_type (kind),
1194 cur->f_total[kind]);
1196 /* Recalcalculate relative preference for all peers */
1197 for (p = cur->p_head; NULL != p; p = p->next)
1199 /* Calculate relative preference for specific kind */
1200 p->f_rel[kind] = (cur->f_total[kind] + p->f[kind]) / cur->f_total[kind];
1201 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n",
1203 GNUNET_i2s (&p->id),
1204 GNUNET_ATS_print_preference_type (kind),
1207 /* Calculate peer relative preference
1208 * Start with i = 1 to exclude terminator */
1210 for (i = 1; i < GNUNET_ATS_PreferenceCount; i ++)
1212 p->f_rel_total += p->f_rel[i];
1214 p->f_rel_total /= (GNUNET_ATS_PreferenceCount - 1.0); /* -1 due to terminator */
1215 LOG (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has total relative preference of %.3f\n",
1217 GNUNET_i2s (&p->id),
1221 /* Calculcate global total relative peer preference over all clients */
1224 for (cur = s->pc_head; NULL != cur; cur = cur->next)
1226 for (p = cur->p_head; NULL != p; p = p->next)
1227 if (0 == memcmp (&p->id, peer, sizeof (p->id)))
1232 p_rel_global += p->f_rel_total;
1235 p_rel_global /= clients;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG, "Global preference value for peer `%s': %.3f\n",
1237 GNUNET_i2s (peer), p_rel_global);
1239 /* Update global map */
1240 /* FIXME: We should update all peers since they have influence on each other */
1241 if (NULL != (dest = GNUNET_CONTAINER_multihashmap_get(s->prefs, &peer->hashPubKey)))
1242 (*dest) = p_rel_global;
1245 dest = GNUNET_malloc (sizeof (float));
1246 (*dest) = p_rel_global;
1247 GNUNET_CONTAINER_multihashmap_put(s->prefs,
1250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1253 /* FIXME: We should update quotas if UPDATE_INTERVAL is reached */
1254 if (GNUNET_TIME_absolute_get().abs_value > next_update.abs_value)
1257 next_update = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1258 MIN_UPDATE_INTERVAL);
1263 /* end of gnunet-service-ats_addresses_simplistic.c */