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, const struct GNUNET_HELLO_Address *address,
148 struct Session *session,
149 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
150 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
151 const struct GNUNET_ATS_Information *atsi,
154 static int stage = 0;
155 static int sug_p0 = GNUNET_NO;
156 static int sug_p1 = GNUNET_NO;
158 static uint32_t p0_last_bandwidth_out;
159 static uint32_t p0_last_bandwidth_in;
161 static uint32_t p1_last_bandwidth_out;
162 static uint32_t p1_last_bandwidth_in;
164 uint32_t cur_bandwidth_out = ntohl (bandwidth_out.value__);
165 uint32_t cur_bandwidth_in = ntohl (bandwidth_in.value__);
169 /* Callback for initial suggestion */
170 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
172 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
173 "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
175 GNUNET_i2s (&address->peer),
176 (unsigned int) ntohl (bandwidth_in.value__),
177 (unsigned int) ntohl (bandwidth_out.value__));
179 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
180 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
182 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
185 "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
187 GNUNET_i2s (&address->peer),
188 (unsigned int) ntohl (bandwidth_in.value__),
189 (unsigned int) ntohl (bandwidth_out.value__));
191 p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
192 p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
194 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
196 /* Changing preference for peer 0 */
198 GNUNET_ATS_performance_change_preference (perf_ats, &p[0].id, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
206 /* Callback due to preference change */
207 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
210 "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
212 GNUNET_i2s (&address->peer),
213 (unsigned int) ntohl (bandwidth_in.value__),
214 (unsigned int) ntohl (bandwidth_out.value__));
217 /* Peer 0 should get more bandwidth */
218 if (cur_bandwidth_out <= p0_last_bandwidth_out)
220 if (cur_bandwidth_in <= p0_last_bandwidth_in)
222 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
223 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
225 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
228 "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
230 GNUNET_i2s (&address->peer),
231 (unsigned int) ntohl (bandwidth_in.value__),
232 (unsigned int) ntohl (bandwidth_out.value__));
235 /* Peer 1 should get less bandwidth */
236 if (cur_bandwidth_out >= p1_last_bandwidth_out)
241 if (cur_bandwidth_in >= p1_last_bandwidth_in)
246 p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
247 p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
249 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
259 /* Callback due to preference aging */
260 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
263 "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
265 GNUNET_i2s (&address->peer),
266 (unsigned int) ntohl (bandwidth_in.value__),
267 (unsigned int) ntohl (bandwidth_out.value__));
270 /* Peer 0 should get less bandwidth */
271 if (cur_bandwidth_out <= p0_last_bandwidth_out)
273 if (cur_bandwidth_in <= p0_last_bandwidth_in)
275 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
276 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
278 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
280 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
281 "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
283 GNUNET_i2s (&address->peer),
284 (unsigned int) ntohl (bandwidth_in.value__),
285 (unsigned int) ntohl (bandwidth_out.value__));
287 /* Peer 1 should get more bandwidth */
288 if (cur_bandwidth_out <= p1_last_bandwidth_out)
293 if (cur_bandwidth_in <= p1_last_bandwidth_in)
298 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
299 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
302 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
305 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
308 GNUNET_SCHEDULER_add_now (&end,NULL);
317 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Error!\n");
318 GNUNET_SCHEDULER_add_now (&end,NULL);
324 const struct GNUNET_CONFIGURATION_Handle *cfg,
325 struct GNUNET_TESTING_Peer *peer)
329 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
331 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
335 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
337 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
339 GNUNET_free (quota_str);
342 GNUNET_free (quota_str);
345 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
347 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
351 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
353 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
354 GNUNET_free (quota_str);
358 GNUNET_free (quota_str);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
364 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
366 /* Connect to ATS scheduling */
367 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
368 if (sched_ats == NULL)
370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
376 /* Connect to ATS performance */
377 perf_ats = GNUNET_ATS_performance_init(cfg, NULL, NULL);
378 if (sched_ats == NULL)
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
388 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
396 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
399 GNUNET_i2s(&p[0].id));
402 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
410 GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
413 GNUNET_i2s(&p[1].id));
415 /* Prepare ATS Information */
416 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
417 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
418 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
419 test_ats_info[1].value = htonl(1);
422 /* Peer 0: Adding address with session */
423 test_session[0] = &test_addr[0];
424 create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
425 test_hello_address[0].peer = p[0].id;
426 test_hello_address[0].transport_name = test_addr[0].plugin;
427 test_hello_address[0].address = test_addr[0].addr;
428 test_hello_address[0].address_length = test_addr[0].addr_len;
429 GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
431 /* Peer 1: Adding address with session */
432 test_session[1] = &test_addr[1];
433 create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
434 test_hello_address[1].peer = p[1].id;
435 test_hello_address[1].transport_name = test_addr[1].plugin;
436 test_hello_address[1].address = test_addr[1].addr;
437 test_hello_address[1].address_length = test_addr[1].addr_len;
438 GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
440 GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
441 GNUNET_ATS_suggest_address (sched_ats, &p[1].id);
446 main (int argc, char *argv[])
448 if (0 != GNUNET_TESTING_peer_run ("test_ats_proportional_pref_aging",
456 /* end of file test_ats_simplistic_pref_aging.c */