separate update and full results
[oweals/gnunet.git] / src / ats / test_ats_simplistic_pref_aging.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file ats/test_ats_mlp.c
22  * @brief test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27 /**
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
34  */
35 #include "platform.h"
36 #include "gnunet_ats_service.h"
37 #include "gnunet_testing_lib.h"
38 #include "ats.h"
39 #include "test_ats_api_common.h"
40
41 #define DEBUG_ATS_INFO GNUNET_NO
42
43 static GNUNET_SCHEDULER_TaskIdentifier die_task;
44
45 /**
46  * Scheduling handle
47  */
48 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
49
50 /**
51  * Performance handle
52  */
53 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
54
55 /**
56  * Return value
57  */
58 static int ret;
59
60 /**
61  * Test address
62  */
63 static struct Test_Address test_addr[2];
64
65 /**
66  * Test peer
67  */
68 static struct PeerContext p[2];
69
70
71 /**
72  * HELLO address
73  */
74 struct GNUNET_HELLO_Address test_hello_address[2];
75
76 /**
77  * Session
78  */
79 static void *test_session[2];
80
81 /**
82  * Test ats info
83  */
84 struct GNUNET_ATS_Information test_ats_info[2];
85
86 /**
87  * Test ats count
88  */
89 uint32_t test_ats_count;
90
91 /**
92  * Configured WAN out quota
93  */
94 unsigned long long wan_quota_out;
95
96 /**
97  * Configured WAN in quota
98  */
99 unsigned long long wan_quota_in;
100
101
102 static void
103 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 {
105   die_task = GNUNET_SCHEDULER_NO_TASK;
106   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error! Shutting down\n");
107   if (sched_ats != NULL)
108   {
109     GNUNET_ATS_scheduling_done (sched_ats);
110     sched_ats = NULL;
111   }
112   if (perf_ats != NULL)
113   {
114     GNUNET_ATS_performance_done (perf_ats);
115     perf_ats = NULL;
116   }
117   free_test_address (&test_addr[0]);
118   ret = GNUNET_SYSERR;
119 }
120
121
122 static void
123 end ()
124 {
125   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
126   if (die_task != GNUNET_SCHEDULER_NO_TASK)
127   {
128     GNUNET_SCHEDULER_cancel (die_task);
129     die_task = GNUNET_SCHEDULER_NO_TASK;
130   }
131
132   GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
133   GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
134
135   if (NULL != sched_ats)
136         GNUNET_ATS_scheduling_done (sched_ats);
137   if (NULL != perf_ats)
138   GNUNET_ATS_performance_done (perf_ats);
139   sched_ats = NULL;
140   perf_ats = NULL;
141   free_test_address (&test_addr[0]);
142   free_test_address (&test_addr[1]);
143 }
144
145
146 static void
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,
154                     uint32_t ats_count)
155 {
156   static int stage = 0;
157   static int sug_p0 = GNUNET_NO;
158   static int sug_p1 = GNUNET_NO;
159
160   static uint32_t p0_last_bandwidth_out;
161   static uint32_t p0_last_bandwidth_in;
162
163   static uint32_t p1_last_bandwidth_out;
164   static uint32_t p1_last_bandwidth_in;
165
166   uint32_t cur_bandwidth_out = ntohl (bandwidth_out.value__);
167   uint32_t cur_bandwidth_in = ntohl (bandwidth_in.value__);
168
169   if (0 == stage)
170   {
171     /* Callback for initial suggestion */
172     if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
173     {
174       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
175                   "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
176                   stage,
177                   GNUNET_i2s (&address->peer),
178                   (unsigned int) ntohl (bandwidth_in.value__),
179                   (unsigned int) ntohl (bandwidth_out.value__));
180       sug_p0 = GNUNET_YES;
181       p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
182       p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
183     }
184     if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
185     {
186       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
187                   "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
188                   stage,
189                   GNUNET_i2s (&address->peer),
190                   (unsigned int) ntohl (bandwidth_in.value__),
191                   (unsigned int) ntohl (bandwidth_out.value__));
192       sug_p1 = GNUNET_YES;
193       p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
194       p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
195     }
196     if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
197     {
198       /* Changing preference for peer 0 */
199       stage ++;
200       GNUNET_ATS_performance_change_preference (perf_ats, &p[0].id, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
201       sug_p0 = GNUNET_NO;
202       sug_p1 = GNUNET_NO;
203       return;
204     }
205   }
206   if (1 == stage)
207   {
208     /* Callback due to preference change */
209     if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
210     {
211       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
212                   "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
213                   stage,
214                   GNUNET_i2s (&address->peer),
215                   (unsigned int) ntohl (bandwidth_in.value__),
216                   (unsigned int) ntohl (bandwidth_out.value__));
217       sug_p0 = GNUNET_YES;
218
219       /* Peer 0 should get more bandwidth */
220       if (cur_bandwidth_out <= p0_last_bandwidth_out)
221         GNUNET_break (0);
222       if (cur_bandwidth_in <= p0_last_bandwidth_in)
223         GNUNET_break (0);
224       p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
225       p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
226     }
227     if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
228     {
229       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
230                   "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
231                   stage,
232                   GNUNET_i2s (&address->peer),
233                   (unsigned int) ntohl (bandwidth_in.value__),
234                   (unsigned int) ntohl (bandwidth_out.value__));
235       sug_p1 = GNUNET_YES;
236
237       /* Peer 1 should get less bandwidth */
238       if (cur_bandwidth_out >= p1_last_bandwidth_out)
239       {
240         GNUNET_break (0);
241         goto error;
242       }
243       if (cur_bandwidth_in >= p1_last_bandwidth_in)
244       {
245         GNUNET_break (0);
246         goto error;
247       }
248       p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
249       p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
250     }
251     if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
252     {
253       stage ++;
254       sug_p0 = GNUNET_NO;
255       sug_p1 = GNUNET_NO;
256       return;
257     }
258   }
259   if (2 == stage)
260   {
261     /* Callback due to preference aging */
262     if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
263     {
264       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
265                   "Stage %u: Callback for peer 0 `%s': (in/out) %u/%u\n",
266                   stage,
267                   GNUNET_i2s (&address->peer),
268                   (unsigned int) ntohl (bandwidth_in.value__),
269                   (unsigned int) ntohl (bandwidth_out.value__));
270       sug_p0 = GNUNET_YES;
271
272       /* Peer 0 should get less bandwidth */
273       if (cur_bandwidth_out <= p0_last_bandwidth_out)
274         GNUNET_break (0);
275       if (cur_bandwidth_in <= p0_last_bandwidth_in)
276         GNUNET_break (0);
277       p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
278       p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
279     }
280     if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
281     {
282       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283                   "Stage %u: Callback for peer 1 `%s': (in/out) %u/%u\n",
284                   stage,
285                   GNUNET_i2s (&address->peer),
286                   (unsigned int) ntohl (bandwidth_in.value__),
287                   (unsigned int) ntohl (bandwidth_out.value__));
288       sug_p1 = GNUNET_YES;
289       /* Peer 1 should get more bandwidth */
290       if (cur_bandwidth_out <= p1_last_bandwidth_out)
291       {
292         GNUNET_break (0);
293         goto error;
294       }
295       if (cur_bandwidth_in <= p1_last_bandwidth_in)
296       {
297         GNUNET_break (0);
298         goto error;
299       }
300       p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
301       p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
302     }
303
304     if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
305     {
306       /* Done ! */
307       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
308       stage ++;
309       ret = 0;
310       GNUNET_SCHEDULER_add_now (&end,NULL);
311       return;
312     }
313   }
314   return;
315
316 error:
317   /* Error ! */
318   ret = 1;
319   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Error!\n");
320   GNUNET_SCHEDULER_add_now (&end,NULL);
321 }
322
323
324 static void
325 run (void *cls,
326      const struct GNUNET_CONFIGURATION_Handle *cfg,
327      struct GNUNET_TESTING_Peer *peer)
328 {
329   char *quota_str;
330
331   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
332   {
333       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
334       ret = 1;
335       return;
336   }
337   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
338   {
339       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
340       ret = 1;
341       GNUNET_free (quota_str);
342       return;
343   }
344   GNUNET_free (quota_str);
345   quota_str = NULL;
346
347   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
348   {
349       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
350       ret = 1;
351       return;
352   }
353   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
354   {
355       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
356       GNUNET_free (quota_str);
357       ret = 1;
358       return;
359   }
360   GNUNET_free (quota_str);
361   quota_str = NULL;
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);
364
365
366   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
367
368   /* Connect to ATS scheduling */
369   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
370   if (sched_ats == NULL)
371   {
372     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
373     ret = 1;
374     end ();
375     return;
376   }
377
378   /* Connect to ATS performance */
379   perf_ats = GNUNET_ATS_performance_init(cfg, NULL, NULL);
380   if (sched_ats == NULL)
381   {
382     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
383     ret = 1;
384     end ();
385     return;
386   }
387
388
389   /* Set up peer 0 */
390   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
391   {
392       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
393       ret = GNUNET_SYSERR;
394       end ();
395       return;
396   }
397
398   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
399
400   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
401               GNUNET_i2s(&p[0].id));
402
403   /* Set up peer 1*/
404   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
405   {
406       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
407       ret = GNUNET_SYSERR;
408       end ();
409       return;
410   }
411
412   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
413
414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
415               GNUNET_i2s(&p[1].id));
416
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);
422   test_ats_count = 2;
423
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);
432
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);
441
442   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
443   GNUNET_ATS_suggest_address (sched_ats, &p[1].id);
444 }
445
446
447 int
448 main (int argc, char *argv[])
449 {
450   if (0 != GNUNET_TESTING_peer_run ("test_ats_proportional_pref_aging",
451                                     "test_ats_api.conf",
452                                     &run, NULL))
453     return 1;
454   return ret;
455 }
456
457
458 /* end of file test_ats_simplistic_pref_aging.c */