2 This file is part of GNUnet.
3 (C) 2010,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.
21 * @file ats/test_ats_mlp.c
22 * @brief test for the MLP solver
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 * @file ats/test_ats_api_scheduling_add_address.c
29 * @brief test for ats proportional solver preference aging:
30 * Add 2 addresses and set high preference for one. Expect higher bw for this
31 * address, wait. Preferences should age and so bw assigned should decrease.
32 * @author Christian Grothoff
33 * @author Matthias Wachs
36 #include "gnunet_ats_service.h"
37 #include "gnunet_testing_lib.h"
39 #include "test_ats_api_common.h"
41 #define DEBUG_ATS_INFO GNUNET_NO
43 static GNUNET_SCHEDULER_TaskIdentifier die_task;
48 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
53 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
63 static struct Test_Address test_addr[2];
68 static struct PeerContext p[2];
74 struct GNUNET_HELLO_Address test_hello_address[2];
79 static void *test_session[2];
84 struct GNUNET_ATS_Information test_ats_info[2];
89 uint32_t test_ats_count;
92 * Configured WAN out quota
94 unsigned long long wan_quota_out;
97 * Configured WAN in quota
99 unsigned long long wan_quota_in;
103 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 die_task = GNUNET_SCHEDULER_NO_TASK;
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error! Shutting down\n");
107 if (sched_ats != NULL)
109 GNUNET_ATS_scheduling_done (sched_ats);
112 if (perf_ats != NULL)
114 GNUNET_ATS_performance_done (perf_ats);
117 free_test_address (&test_addr[0]);
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
126 if (die_task != GNUNET_SCHEDULER_NO_TASK)
128 GNUNET_SCHEDULER_cancel (die_task);
129 die_task = GNUNET_SCHEDULER_NO_TASK;
132 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
133 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
135 if (NULL != sched_ats)
136 GNUNET_ATS_scheduling_done (sched_ats);
137 if (NULL != perf_ats)
138 GNUNET_ATS_performance_done (perf_ats);
141 free_test_address (&test_addr[0]);
142 free_test_address (&test_addr[1]);
147 address_suggest_cb (void *cls,
148 const struct GNUNET_PeerIdentity *peer,
149 const struct GNUNET_HELLO_Address *address,
150 struct Session *session,
151 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
152 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
153 const struct GNUNET_ATS_Information *atsi,
156 static int stage = 0;
157 static int sug_p0 = GNUNET_NO;
158 static int sug_p1 = GNUNET_NO;
160 static uint32_t p0_last_bandwidth_out;
161 static uint32_t p0_last_bandwidth_in;
163 static uint32_t p1_last_bandwidth_out;
164 static uint32_t p1_last_bandwidth_in;
166 uint32_t cur_bandwidth_out = ntohl (bandwidth_out.value__);
167 uint32_t cur_bandwidth_in = ntohl (bandwidth_in.value__);
171 /* Callback for initial suggestion */
172 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
175 "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
177 GNUNET_i2s (&address->peer),
178 (unsigned int) ntohl (bandwidth_in.value__),
179 (unsigned int) ntohl (bandwidth_out.value__));
181 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
182 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
184 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
186 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
187 "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
189 GNUNET_i2s (&address->peer),
190 (unsigned int) ntohl (bandwidth_in.value__),
191 (unsigned int) ntohl (bandwidth_out.value__));
193 p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
194 p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
196 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
198 /* Changing preference for peer 0 */
200 GNUNET_ATS_performance_change_preference (perf_ats, &p[0].id, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
208 /* Callback due to preference change */
209 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
211 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
212 "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
214 GNUNET_i2s (&address->peer),
215 (unsigned int) ntohl (bandwidth_in.value__),
216 (unsigned int) ntohl (bandwidth_out.value__));
219 /* Peer 0 should get more bandwidth */
220 if (cur_bandwidth_out <= p0_last_bandwidth_out)
222 if (cur_bandwidth_in <= p0_last_bandwidth_in)
224 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
225 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
227 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
229 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
230 "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
232 GNUNET_i2s (&address->peer),
233 (unsigned int) ntohl (bandwidth_in.value__),
234 (unsigned int) ntohl (bandwidth_out.value__));
237 /* Peer 1 should get less bandwidth */
238 if (cur_bandwidth_out >= p1_last_bandwidth_out)
243 if (cur_bandwidth_in >= p1_last_bandwidth_in)
248 p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
249 p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
251 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
261 /* Callback due to preference aging */
262 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
265 "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
267 GNUNET_i2s (&address->peer),
268 (unsigned int) ntohl (bandwidth_in.value__),
269 (unsigned int) ntohl (bandwidth_out.value__));
272 /* Peer 0 should get less bandwidth */
273 if (cur_bandwidth_out <= p0_last_bandwidth_out)
275 if (cur_bandwidth_in <= p0_last_bandwidth_in)
277 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
278 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
280 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283 "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
285 GNUNET_i2s (&address->peer),
286 (unsigned int) ntohl (bandwidth_in.value__),
287 (unsigned int) ntohl (bandwidth_out.value__));
289 /* Peer 1 should get more bandwidth */
290 if (cur_bandwidth_out <= p1_last_bandwidth_out)
295 if (cur_bandwidth_in <= p1_last_bandwidth_in)
300 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
301 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
304 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
307 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
310 GNUNET_SCHEDULER_add_now (&end,NULL);
319 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Error!\n");
320 GNUNET_SCHEDULER_add_now (&end,NULL);
326 const struct GNUNET_CONFIGURATION_Handle *cfg,
327 struct GNUNET_TESTING_Peer *peer)
331 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
333 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
337 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
339 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
341 GNUNET_free (quota_str);
344 GNUNET_free (quota_str);
347 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
349 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
353 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
355 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
356 GNUNET_free (quota_str);
360 GNUNET_free (quota_str);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
366 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
368 /* Connect to ATS scheduling */
369 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
370 if (sched_ats == NULL)
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
378 /* Connect to ATS performance */
379 perf_ats = GNUNET_ATS_performance_init(cfg, NULL, NULL);
380 if (sched_ats == NULL)
382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
390 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
398 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
401 GNUNET_i2s(&p[0].id));
404 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
412 GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
415 GNUNET_i2s(&p[1].id));
417 /* Prepare ATS Information */
418 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
419 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
420 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
421 test_ats_info[1].value = htonl(1);
424 /* Peer 0: Adding address with session */
425 test_session[0] = &test_addr[0];
426 create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
427 test_hello_address[0].peer = p[0].id;
428 test_hello_address[0].transport_name = test_addr[0].plugin;
429 test_hello_address[0].address = test_addr[0].addr;
430 test_hello_address[0].address_length = test_addr[0].addr_len;
431 GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
433 /* Peer 1: Adding address with session */
434 test_session[1] = &test_addr[1];
435 create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
436 test_hello_address[1].peer = p[1].id;
437 test_hello_address[1].transport_name = test_addr[1].plugin;
438 test_hello_address[1].address = test_addr[1].addr;
439 test_hello_address[1].address_length = test_addr[1].addr_len;
440 GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
442 GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
443 GNUNET_ATS_suggest_address (sched_ats, &p[1].id);
448 main (int argc, char *argv[])
450 if (0 != GNUNET_TESTING_peer_run ("test_ats_proportional_pref_aging",
458 /* end of file test_ats_simplistic_pref_aging.c */