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
60 * A handle for the simplistic solver
62 struct GAS_SIMPLISTIC_Handle
68 struct GNUNET_STATISTICS_Handle *stats;
71 * Total number of addresses for solver
73 unsigned int total_addresses;
76 * Number of active addresses for solver
78 unsigned int active_addresses;
83 struct Network *network_entries;
88 unsigned int networks;
93 GAS_bandwidth_changed_cb bw_changed;
109 * Network description
114 * Total inbound quota
117 unsigned long long total_quota_in;
120 * Total outbound quota
123 unsigned long long total_quota_out;
126 * Number of active addresses for this network
128 unsigned int active_addresses;
131 * Number of total addresses for this network
133 unsigned int total_addresses;
136 * String for statistics total addresses
141 * String for statistics active addresses
145 struct AddressWrapper *head;
146 struct AddressWrapper *tail;
149 struct AddressWrapper
151 struct AddressWrapper *next;
152 struct AddressWrapper *prev;
154 struct ATS_Address *addr;
158 * Init the simplistic problem solving component
161 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
162 * out_quota[i] contains outbound quota for network type i
163 * in_quota[i] contains inbound quota for network type i
166 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
167 * network[2] == GNUNET_ATS_NET_LAN
168 * out_quota[2] == 65353
169 * in_quota[2] == 65353
171 * @param cfg configuration handle
172 * @param stats the GNUNET_STATISTICS handle
173 * @param network array of GNUNET_ATS_NetworkType with length dest_length
174 * @param out_quota array of outbound quotas
175 * @param in_quota array of outbound quota
176 * @param dest_length array length for quota arrays
177 * @param bw_changed_cb callback for changed bandwidth amounts
178 * @param bw_changed_cb_cls cls for callback
179 * @return handle for the solver on success, NULL on fail
182 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
183 const struct GNUNET_STATISTICS_Handle *stats,
185 unsigned long long *out_quota,
186 unsigned long long *in_quota,
188 GAS_bandwidth_changed_cb bw_changed_cb,
189 void *bw_changed_cb_cls)
192 struct GAS_SIMPLISTIC_Handle *s = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
193 struct Network * cur;
194 char * net_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
197 s->stats = (struct GNUNET_STATISTICS_Handle *) stats;
198 s->bw_changed = bw_changed_cb;
199 s->bw_changed_cls = bw_changed_cb_cls;
200 s->networks = dest_length;
201 s->network_entries = GNUNET_malloc (dest_length * sizeof (struct Network));
202 s->active_addresses = 0;
203 s->total_addresses = 0;
205 for (c = 0; c < dest_length; c++)
207 cur = &s->network_entries[c];
208 cur->total_addresses = 0;
209 cur->active_addresses = 0;
210 cur->type = network[c];
211 cur->total_quota_in = in_quota[c];
212 cur->total_quota_out = out_quota[c];
213 cur->desc = net_str[c];
214 GNUNET_asprintf (&cur->stat_total, "# ATS addresses %s total", cur->desc);
215 GNUNET_asprintf (&cur->stat_active, "# ATS active addresses %s total", cur->desc);
222 * Shutdown the simplistic problem solving component
224 * @param solver the respective handle to shutdown
227 GAS_simplistic_done (void *solver)
229 struct GAS_SIMPLISTIC_Handle *s = solver;
230 struct AddressWrapper *cur;
231 struct AddressWrapper *next;
233 GNUNET_assert (s != NULL);
235 for (c = 0; c < s->networks; c++)
237 if (s->network_entries[c].total_addresses > 0)
239 LOG (GNUNET_ERROR_TYPE_ERROR,
240 "Had %u addresses for network `%s' not deleted during shutdown\n",
241 s->network_entries[c].total_addresses,
242 s->network_entries[c].desc);
246 if (s->network_entries[c].active_addresses > 0)
248 LOG (GNUNET_ERROR_TYPE_ERROR,
249 "Had %u active addresses for network `%s' not deleted during shutdown\n",
250 s->network_entries[c].active_addresses,
251 s->network_entries[c].desc);
255 next = s->network_entries[c].head;
256 while (NULL != (cur = next))
259 GNUNET_CONTAINER_DLL_remove (s->network_entries[c].head,
260 s->network_entries[c].tail,
264 GNUNET_free (s->network_entries[c].stat_total);
265 GNUNET_free (s->network_entries[c].stat_active);
267 if (s->total_addresses > 0)
269 LOG (GNUNET_ERROR_TYPE_ERROR,
270 "Had %u addresses not deleted during shutdown\n",
274 if (s->active_addresses > 0)
276 LOG (GNUNET_ERROR_TYPE_ERROR,
277 "Had %u active addresses not deleted during shutdown\n",
278 s->active_addresses);
282 GNUNET_free (s->network_entries);
287 * Update the quotas for a network type
289 * @param s the solver handle
290 * @param net the network type to update
291 * @param address_except address excluded from notifcation, since we suggest
296 update_quota_per_network (struct GAS_SIMPLISTIC_Handle *s,
298 struct ATS_Address *address_except)
300 unsigned long long quota_in = 0;
301 unsigned long long quota_out = 0;
302 struct AddressWrapper *cur;
304 LOG (GNUNET_ERROR_TYPE_DEBUG,
305 "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
306 net->desc, net->active_addresses, quota_in, quota_out);
308 if (net->active_addresses == 0)
309 return; /* no addresses to update */
311 quota_in = net->total_quota_in / net->active_addresses;
312 quota_out = net->total_quota_out / net->active_addresses;
314 LOG (GNUNET_ERROR_TYPE_DEBUG,
315 "New per address quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
316 net->desc, net->active_addresses, quota_in, quota_out);
321 /* Compare to current bandwidth assigned */
322 if ((quota_in != ntohl(cur->addr->assigned_bw_in.value__)) ||
323 (quota_out != ntohl(cur->addr->assigned_bw_out.value__)))
325 cur->addr->assigned_bw_in.value__ = htonl (quota_in);
326 cur->addr->assigned_bw_out.value__ = htonl (quota_out);
327 /* Notify on change */
328 if ((GNUNET_YES == cur->addr->active) && (cur->addr != address_except))
329 s->bw_changed (s->bw_changed_cls, cur->addr);
336 addresse_increment (struct GAS_SIMPLISTIC_Handle *s,
341 if (GNUNET_YES == total)
343 s->total_addresses ++;
344 net->total_addresses ++;
345 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", 1, GNUNET_NO);
346 GNUNET_STATISTICS_update (s->stats, net->stat_total, 1, GNUNET_NO);
348 if (GNUNET_YES == active)
350 net->active_addresses ++;
351 s->active_addresses ++;
352 GNUNET_STATISTICS_update (s->stats, "# ATS active addresses total", 1, GNUNET_NO);
353 GNUNET_STATISTICS_update (s->stats, net->stat_active, 1, GNUNET_NO);
359 addresse_decrement (struct GAS_SIMPLISTIC_Handle *s,
365 if (GNUNET_YES == total)
367 if (s->total_addresses < 1)
374 s->total_addresses --;
375 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
377 if (net->total_addresses < 1)
384 net->total_addresses --;
385 GNUNET_STATISTICS_update (s->stats, net->stat_total, -1, GNUNET_NO);
389 if (GNUNET_YES == active)
391 if (net->active_addresses < 1)
398 net->active_addresses --;
399 GNUNET_STATISTICS_update (s->stats, net->stat_active, -1, GNUNET_NO);
401 if (s->active_addresses < 1)
408 s->active_addresses --;
409 GNUNET_STATISTICS_update (s->stats, "# ATS addresses total", -1, GNUNET_NO);
417 * Add a single address to the solve
419 * @param solver the solver Handle
420 * @param addresses the address hashmap containing all addresses
421 * @param address the address to add
424 GAS_simplistic_address_add (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
426 struct GAS_SIMPLISTIC_Handle *s = solver;
427 struct Network *net = NULL;
428 struct AddressWrapper *aw = NULL;
429 GNUNET_assert (NULL != s);
431 for (c = 0; c < s->networks; c++)
433 net = &s->network_entries[c];
434 if (address->atsp_network_type == net->type)
443 aw = GNUNET_malloc (sizeof (struct AddressWrapper));
445 GNUNET_CONTAINER_DLL_insert (net->head, net->tail, aw);
446 addresse_increment (s, net, GNUNET_YES, GNUNET_NO);
447 aw->addr->solver_information = net;
450 LOG (GNUNET_ERROR_TYPE_DEBUG, "After adding address now total %u and active %u addresses in network `%s'\n",
451 net->total_addresses,
452 net->active_addresses,
457 * Remove an address from the solver
459 * @param solver the solver handle
460 * @param addresses the address hashmap containing all addresses
461 * @param address the address to remove
462 * @param session_only delete only session not whole address
465 GAS_simplistic_address_delete (void *solver,
466 struct GNUNET_CONTAINER_MultiHashMap * addresses,
467 struct ATS_Address *address, int session_only)
469 struct GAS_SIMPLISTIC_Handle *s = solver;
471 struct AddressWrapper *aw;
473 /* Remove an adress completely, we have to:
474 * - Remove from specific network
475 * - Decrease number of total addresses
477 * - decrease number of active addreses
481 net = (struct Network *) address->solver_information;
483 if (GNUNET_NO == session_only)
485 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
486 (GNUNET_NO == address->active) ? "inactive" : "active",
487 address, GNUNET_i2s (&address->peer),
488 net->desc, net->total_addresses, net->active_addresses);
491 addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
492 for (aw = net->head; NULL != aw; aw = aw->next)
494 if (aw->addr == address)
502 GNUNET_CONTAINER_DLL_remove (net->head, net->tail, aw);
507 /* Remove session only: remove if active and update */
508 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
509 (GNUNET_NO == address->active) ? "inactive" : "active",
510 address, GNUNET_i2s (&address->peer),
511 net->desc, net->total_addresses, net->active_addresses);
515 if (GNUNET_YES == address->active)
517 /* Address was active, remove from network and update quotas*/
518 address->active = GNUNET_NO;
519 if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
521 update_quota_per_network (s, net, NULL);
523 LOG (GNUNET_ERROR_TYPE_DEBUG, "After deleting address now total %u and active %u addresses in network `%s'\n",
524 net->total_addresses,
525 net->active_addresses,
530 static struct Network *
531 find_network (struct GAS_SIMPLISTIC_Handle *s, uint32_t type)
534 for (c = 0 ; c < s->networks; c++)
536 if (s->network_entries[c].type == type)
537 return &s->network_entries[c];
543 * Updates a single address in the solve
545 * @param solver the solver Handle
546 * @param addresses the address hashmap containing all addresses
547 * @param address the update address
548 * @param session the new session (if changed otherwise current)
549 * @param in_use the new address in use state (if changed otherwise current)
550 * @param atsi the latest ATS information
551 * @param atsi_count the atsi count
554 GAS_simplistic_address_update (void *solver,
555 struct GNUNET_CONTAINER_MultiHashMap *addresses,
556 struct ATS_Address *address,
559 const struct GNUNET_ATS_Information *atsi,
562 struct GAS_SIMPLISTIC_Handle *s = (struct GAS_SIMPLISTIC_Handle *) solver;
566 int save_active = GNUNET_NO;
567 struct Network *new_net = NULL;
568 for (i = 0; i < atsi_count; i++)
570 type = ntohl (atsi[i].type);
571 value = ntohl (atsi[i].value);
574 case GNUNET_ATS_UTILIZATION_UP:
575 //if (address->atsp_utilization_out.value__ != atsi[i].value)
578 case GNUNET_ATS_UTILIZATION_DOWN:
579 //if (address->atsp_utilization_in.value__ != atsi[i].value)
582 case GNUNET_ATS_QUALITY_NET_DELAY:
583 //if (address->atsp_latency.rel_value != value)
586 case GNUNET_ATS_QUALITY_NET_DISTANCE:
587 //if (address->atsp_distance != value)
590 case GNUNET_ATS_COST_WAN:
591 //if (address->atsp_cost_wan != value)
594 case GNUNET_ATS_COST_LAN:
595 //if (address->atsp_cost_lan != value)
598 case GNUNET_ATS_COST_WLAN:
599 //if (address->atsp_cost_wlan != value)
602 case GNUNET_ATS_NETWORK_TYPE:
603 if (address->atsp_network_type != value)
606 LOG (GNUNET_ERROR_TYPE_DEBUG, "Network type changed, moving %s address from `%s' to `%s'\n",
607 (GNUNET_YES == address->active) ? "active" : "inactive",
608 GNUNET_ATS_print_network_type(address->atsp_network_type),
609 GNUNET_ATS_print_network_type(value));
611 save_active = address->active;
612 /* remove from old network */
613 GAS_simplistic_address_delete (solver, addresses, address, GNUNET_NO);
615 /* set new network type */
616 address->atsp_network_type = value;
617 new_net = find_network (solver, value);
618 address->solver_information = new_net;
619 if (address->solver_information == NULL)
622 address->atsp_network_type = GNUNET_ATS_NET_UNSPECIFIED;
626 /* restore active state, add to new network and update*/
627 address->active = save_active;
628 GAS_simplistic_address_add (solver, addresses, address);
629 if (GNUNET_YES == save_active)
631 addresse_increment (s, new_net, GNUNET_NO, GNUNET_YES);
632 update_quota_per_network (solver, new_net, NULL);
636 case GNUNET_ATS_ARRAY_TERMINATOR:
639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640 "Received unsupported ATS type %u\n", type);
647 if (address->session_id != session)
649 LOG (GNUNET_ERROR_TYPE_DEBUG,
650 "Session changed from %u to %u\n", address->session_id, session);
651 address->session_id = session;
653 if (address->used != in_use)
655 LOG (GNUNET_ERROR_TYPE_DEBUG,
656 "Usage changed from %u to %u\n", address->used, in_use);
657 address->used = in_use;
665 * Find a "good" address to use for a peer. If we already have an existing
666 * address, we stick to it. Otherwise, we pick by lowest distance and then
669 * @param cls the 'struct ATS_Address**' where we store the result
671 * @param value another 'struct ATS_Address*' to consider using
672 * @return GNUNET_OK (continue to iterate)
675 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
677 struct ATS_Address **previous_p = cls;
678 struct ATS_Address *current = (struct ATS_Address *) value;
679 struct ATS_Address *previous = *previous_p;
680 struct GNUNET_TIME_Absolute now;
682 now = GNUNET_TIME_absolute_get();
684 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
686 /* This address is blocked for suggestion */
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Address %p blocked for suggestion for %llu ms \n",
690 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
694 if (NULL != previous)
696 if ((0 == strcmp (previous->plugin, "tcp")) &&
697 (0 == strcmp (current->plugin, "tcp")))
699 if ((0 != previous->addr_len) &&
700 (0 == current->addr_len))
702 /* saved address was an outbound address, but we have an inbound address */
703 *previous_p = current;
706 if (0 == previous->addr_len)
708 /* saved address was an inbound address, so do not overwrite */
714 if (NULL == previous)
716 *previous_p = current;
719 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
720 (ntohl (current->assigned_bw_in.value__) > 0))
722 /* stick to existing connection */
723 *previous_p = current;
726 if (previous->atsp_distance > current->atsp_distance)
728 /* user shorter distance */
729 *previous_p = current;
732 if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
734 /* user lower latency */
735 *previous_p = current;
743 find_active_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
745 struct ATS_Address * dest = (struct ATS_Address *) (*(struct ATS_Address **)cls);
746 struct ATS_Address * aa = (struct ATS_Address *) value;
748 if (GNUNET_YES == aa->active)
752 /* should never happen */
753 LOG (GNUNET_ERROR_TYPE_ERROR, "Multiple active addresses for peer `%s'\n", GNUNET_i2s (&aa->peer));
762 static struct ATS_Address *
763 find_active_address (void *solver,
764 struct GNUNET_CONTAINER_MultiHashMap * addresses,
765 const struct GNUNET_PeerIdentity *peer)
767 struct ATS_Address * dest = NULL;
769 GNUNET_CONTAINER_multihashmap_get_multiple(addresses,
771 &find_active_address_it, &dest);
776 * Get the prefered address for a specific peer
778 * @param solver the solver handle
779 * @param addresses the address hashmap containing all addresses
780 * @param peer the identity of the peer
782 const struct ATS_Address *
783 GAS_simplistic_get_preferred_address (void *solver,
784 struct GNUNET_CONTAINER_MultiHashMap * addresses,
785 const struct GNUNET_PeerIdentity *peer)
787 struct GAS_SIMPLISTIC_Handle *s = solver;
788 struct Network *net_prev;
789 struct Network *net_cur;
790 struct ATS_Address *cur;
791 struct ATS_Address *prev;
793 GNUNET_assert (s != NULL);
795 /* Get address with: stick to current address, lower distance, lower latency */
796 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
797 &find_address_it, &cur);
800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
804 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting %s address %p for peer `%s'\n",
805 (GNUNET_NO == cur->active) ? "inactive" : "active",
806 cur, GNUNET_i2s (peer));
807 net_cur = (struct Network *) cur->solver_information;
808 if (GNUNET_YES == cur->active)
810 /* This address was selected previously, so no need to update quotas */
814 /* This address was not active, so we have to:
816 * - mark previous active address as not active
817 * - update quota for previous address network
818 * - update quota for this address network
821 prev = find_active_address (s, addresses, peer);
824 net_prev = (struct Network *) prev->solver_information;
825 prev->active = GNUNET_NO; /* No active any longer */
826 prev->assigned_bw_in = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
827 prev->assigned_bw_out = GNUNET_BANDWIDTH_value_init (0); /* no bw assigned */
828 s->bw_changed (s->bw_changed_cls, prev); /* notify about bw change, REQUIRED? */
829 if (GNUNET_SYSERR == addresse_decrement (s, net_prev, GNUNET_NO, GNUNET_YES))
831 update_quota_per_network (s, net_prev, NULL);
834 cur->active = GNUNET_YES;
835 addresse_increment(s, net_cur, GNUNET_NO, GNUNET_YES);
836 update_quota_per_network (s, net_cur, cur);
843 * Changes the preferences for a peer in the problem
845 * @param solver the solver handle
846 * @param client the client with this preference
847 * @param peer the peer to change the preference for
848 * @param kind the kind to change the preference
849 * @param score the score
852 GAS_simplistic_address_change_preference (void *solver,
854 const struct GNUNET_PeerIdentity *peer,
855 enum GNUNET_ATS_PreferenceKind kind,
861 /* end of gnunet-service-ats_addresses_simplistic.c */