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 * A handle for the simplistic solver
37 struct GAS_SIMPLISTIC_Handle
40 unsigned int active_addresses;
45 * quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
51 * Array of inbound quotas
54 unsigned long long *quota_in;
57 * Array of outbound quotas
60 unsigned long long *quota_out;
63 * Active addresses per network type
65 unsigned int *active_addresses_per_net;
69 * Init the simplistic problem solving component
72 * network[i] contains the network type as type GNUNET_ATS_NetworkType[i]
73 * out_quota[i] contains outbound quota for network type i
74 * in_quota[i] contains inbound quota for network type i
77 * network = {GNUNET_ATS_NET_UNSPECIFIED, GNUNET_ATS_NET_LOOPBACK, GNUNET_ATS_NET_LAN, GNUNET_ATS_NET_WAN, GNUNET_ATS_NET_WLAN}
78 * network[2] == GNUNET_ATS_NET_LAN
79 * out_quota[2] == 65353
80 * in_quota[2] == 65353
82 * @param cfg configuration handle
83 * @param stats the GNUNET_STATISTICS handle
84 * @param network array of GNUNET_ATS_NetworkType with length dest_length
85 * @param out_quota array of outbound quotas
86 * param in_quota array of outbound quota
87 * @return handle for the solver on success, NULL on fail
90 GAS_simplistic_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
91 const struct GNUNET_STATISTICS_Handle *stats,
93 unsigned long long *out_quota,
94 unsigned long long *in_quota,
97 struct GAS_SIMPLISTIC_Handle *solver = GNUNET_malloc (sizeof (struct GAS_SIMPLISTIC_Handle));
99 solver->quota_net = GNUNET_malloc (dest_length * sizeof (int));
100 memcpy (solver->quota_net, network, dest_length * sizeof (int));
102 solver->quota_in = GNUNET_malloc (dest_length * sizeof (unsigned long long));
103 memcpy (solver->quota_in, in_quota, dest_length * sizeof (int));
105 solver->quota_out = GNUNET_malloc (dest_length * sizeof (unsigned long long));
106 memcpy (solver->quota_out, out_quota, dest_length * sizeof (unsigned long long));
108 solver->active_addresses_per_net = GNUNET_malloc (dest_length * sizeof (unsigned int));
115 * Shutdown the simplistic problem solving component
117 * @param solver the respective handle to shutdown
120 GAS_simplistic_done (void *solver)
122 struct GAS_SIMPLISTIC_Handle *s = solver;
123 GNUNET_assert (s != NULL);
124 GNUNET_free (s->quota_net);
125 GNUNET_free (s->quota_in);
126 GNUNET_free (s->quota_out);
127 GNUNET_free (s->active_addresses_per_net);
132 * Updates a single address in the solve
134 * @param solver the solver Handle
135 * @param addresses the address hashmap containing all addresses
136 * @param address the update address
139 GAS_simplistic_address_update (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
146 * Remove an address from the solver
148 * @param solver the solver handle
149 * @param addresses the address hashmap containing all addresses
150 * @param address the address to remove
153 GAS_simplistic_address_delete (void *solver, struct GNUNET_CONTAINER_MultiHashMap * addresses, struct ATS_Address *address)
161 * Find a "good" address to use for a peer. If we already have an existing
162 * address, we stick to it. Otherwise, we pick by lowest distance and then
165 * @param cls the 'struct ATS_Address**' where we store the result
167 * @param value another 'struct ATS_Address*' to consider using
168 * @return GNUNET_OK (continue to iterate)
171 find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
173 struct ATS_Address **previous_p = cls;
174 struct ATS_Address *current = (struct ATS_Address *) value;
175 struct ATS_Address *previous = *previous_p;
176 struct GNUNET_TIME_Absolute now;
178 now = GNUNET_TIME_absolute_get();
180 if (current->blocked_until.abs_value == GNUNET_TIME_absolute_max (now, current->blocked_until).abs_value)
182 /* This address is blocked for suggestion */
183 LOG (GNUNET_ERROR_TYPE_DEBUG,
184 "Address %p blocked for suggestion for %llu ms \n",
186 GNUNET_TIME_absolute_get_difference(now, current->blocked_until).rel_value);
190 if (NULL != previous)
192 if ((0 == strcmp (previous->plugin, "tcp")) &&
193 (0 == strcmp (current->plugin, "tcp")))
195 if ((0 != previous->addr_len) &&
196 (0 == current->addr_len))
198 /* saved address was an outbound address, but we have an inbound address */
199 *previous_p = current;
202 if (0 == previous->addr_len)
204 /* saved address was an inbound address, so do not overwrite */
210 if (NULL == previous)
212 *previous_p = current;
215 if ((ntohl (previous->assigned_bw_in.value__) == 0) &&
216 (ntohl (current->assigned_bw_in.value__) > 0))
218 /* stick to existing connection */
219 *previous_p = current;
222 if (previous->atsp_distance > current->atsp_distance)
224 /* user shorter distance */
225 *previous_p = current;
228 if (previous->atsp_latency.rel_value > current->atsp_latency.rel_value)
230 /* user lower latency */
231 *previous_p = current;
239 update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
241 struct GAS_SIMPLISTIC_Handle *s = cls;
242 struct ATS_Address *aa = value;
244 if (GNUNET_YES != aa->active)
246 GNUNET_assert (s->active_addresses > 0);
251 aa->assigned_bw_in.value__ = htonl (UINT32_MAX / s->active_addresses);
252 aa->assigned_bw_out.value__ = htonl (UINT32_MAX / s->active_addresses);
258 * Some (significant) input changed, recalculate bandwidth assignment
262 recalculate_assigned_bw (void *solver,
263 struct GNUNET_CONTAINER_MultiHashMap * addresses)
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Recalculating bandwidth for all active connections\n");
267 GNUNET_CONTAINER_multihashmap_iterate (addresses, &update_bw_simple_it, solver);
273 * Get the prefered address for a specific peer
275 * @param solver the solver handle
276 * @param addresses the address hashmap containing all addresses
277 * @param peer the identity of the peer
279 const struct ATS_Address *
280 GAS_simplistic_get_preferred_address (void *solver,
281 struct GNUNET_CONTAINER_MultiHashMap * addresses,
282 const struct GNUNET_PeerIdentity *peer)
284 struct GAS_SIMPLISTIC_Handle *s = solver;
285 struct ATS_Address *aa;
287 GNUNET_assert (s != NULL);
289 /* Get address with: stick to current address, lower distance, lower latency */
290 GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
291 &find_address_it, &aa);
293 LOG (GNUNET_ERROR_TYPE_DEBUG, "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
296 LOG (GNUNET_ERROR_TYPE_DEBUG, "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
298 if (GNUNET_NO == aa->active)
300 aa->active = GNUNET_YES;
301 s->active_addresses++;
302 recalculate_assigned_bw (s, addresses);
311 * Changes the preferences for a peer in the problem
313 * @param solver the solver handle
314 * @param peer the peer to change the preference for
315 * @param kind the kind to change the preference
316 * @param score the score
319 GAS_simplistic_address_change_preference (void *solver,
320 const struct GNUNET_PeerIdentity *peer,
321 enum GNUNET_ATS_PreferenceKind kind,
324 /* FIXME : implement this */
327 /* end of gnunet-service-ats_addresses_simplistic.c */