fix eligibility traces
[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, 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,
152                     uint32_t ats_count)
153 {
154   static int stage = 0;
155   static int sug_p0 = GNUNET_NO;
156   static int sug_p1 = GNUNET_NO;
157
158   static uint32_t p0_last_bandwidth_out;
159   static uint32_t p0_last_bandwidth_in;
160
161   static uint32_t p1_last_bandwidth_out;
162   static uint32_t p1_last_bandwidth_in;
163
164   uint32_t cur_bandwidth_out = ntohl (bandwidth_out.value__);
165   uint32_t cur_bandwidth_in = ntohl (bandwidth_in.value__);
166
167   if (0 == stage)
168   {
169                 /* Callback for initial suggestion */
170                 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
171                 {
172                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 0 `%s': (in/out) %llu/%llu\n",
173                                                 stage,
174                                                 GNUNET_i2s (&address->peer),
175                                                 ntohl (bandwidth_in.value__),
176                                                 ntohl (bandwidth_out.value__));
177                                 sug_p0 = GNUNET_YES;
178                                 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
179                                 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
180                 }
181                 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
182                 {
183                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 1 `%s': (in/out) %llu/%llu\n",
184                                                 stage,
185                                                 GNUNET_i2s (&address->peer),
186                                                 ntohl (bandwidth_in.value__),
187                                                 ntohl (bandwidth_out.value__));
188                                 sug_p1 = GNUNET_YES;
189                                 p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
190                                 p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
191                 }
192                 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
193                 {
194                   /* Changing preference for peer 0 */
195                         stage ++;
196                         GNUNET_ATS_performance_change_preference (perf_ats, &p[0].id, GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
197                                 sug_p0 = GNUNET_NO;
198                                 sug_p1 = GNUNET_NO;
199                         return;
200                 }
201
202   }
203   if (1 == stage)
204   {
205                 /* Callback due to preference change */
206                 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
207                 {
208                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 0 `%s': (in/out) %llu/%llu\n",
209                                                 stage,
210                                                 GNUNET_i2s (&address->peer),
211                                                 ntohl (bandwidth_in.value__),
212                                                 ntohl (bandwidth_out.value__));
213                                 sug_p0 = GNUNET_YES;
214
215                                 /* Peer 0 should get more bandwidth */
216                                 if (cur_bandwidth_out <= p0_last_bandwidth_out)
217                                         GNUNET_break (0);
218                                 if (cur_bandwidth_in <= p0_last_bandwidth_in)
219                                         GNUNET_break (0);
220                                 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
221                                 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
222                 }
223                 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
224                 {
225                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 1 `%s': (in/out) %llu/%llu\n",
226                                                 stage,
227                                                 GNUNET_i2s (&address->peer),
228                                                 ntohl (bandwidth_in.value__),
229                                                 ntohl (bandwidth_out.value__));
230                                 sug_p1 = GNUNET_YES;
231
232                                 /* Peer 1 should get less bandwidth */
233                                 if (cur_bandwidth_out >= p1_last_bandwidth_out)
234                                 {
235                                         GNUNET_break (0);
236                                         goto error;
237                                 }
238                                 if (cur_bandwidth_in >= p1_last_bandwidth_in)
239                                 {
240                                         GNUNET_break (0);
241                                         goto error;
242                                 }
243                                 p1_last_bandwidth_out = ntohl(bandwidth_out.value__);
244                                 p1_last_bandwidth_in = ntohl(bandwidth_in.value__);
245                 }
246                 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
247                 {
248                         stage ++;
249                                 sug_p0 = GNUNET_NO;
250                                 sug_p1 = GNUNET_NO;
251                         return;
252                 }
253   }
254   if (2 == stage)
255   {
256                 /* Callback due to preference aging */
257                 if (0 == memcmp (&address->peer, &p[0].id, sizeof (p[0].id)))
258                 {
259                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 0 `%s': (in/out) %llu/%llu\n",
260                                                 stage,
261                                                 GNUNET_i2s (&address->peer),
262                                                 ntohl (bandwidth_in.value__),
263                                                 ntohl (bandwidth_out.value__));
264                                 sug_p0 = GNUNET_YES;
265
266                                 /* Peer 0 should get less bandwidth */
267                                 if (cur_bandwidth_out <= p0_last_bandwidth_out)
268                                         GNUNET_break (0);
269                                 if (cur_bandwidth_in <= p0_last_bandwidth_in)
270                                         GNUNET_break (0);
271                                 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
272                                 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
273                 }
274                 if (0 == memcmp (&address->peer, &p[1].id, sizeof (p[1].id)))
275                 {
276                                 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %u: Callback for peer 1 `%s': (in/out) %llu/%llu\n",
277                                                 stage,
278                                                 GNUNET_i2s (&address->peer),
279                                                 ntohl (bandwidth_in.value__),
280                                                 ntohl (bandwidth_out.value__));
281                                 sug_p1 = GNUNET_YES;
282                                 /* Peer 1 should get more bandwidth */
283                                 if (cur_bandwidth_out <= p1_last_bandwidth_out)
284                                 {
285                                         GNUNET_break (0);
286                                         goto error;
287                                 }
288                                 if (cur_bandwidth_in <= p1_last_bandwidth_in)
289                                 {
290                                         GNUNET_break (0);
291                                         goto error;
292                                 }
293                                 p0_last_bandwidth_out = ntohl(bandwidth_out.value__);
294                                 p0_last_bandwidth_in = ntohl(bandwidth_in.value__);
295                 }
296
297                 if ((GNUNET_YES == sug_p0) && (GNUNET_YES == sug_p1))
298                 {
299                         /* Done ! */
300                         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done!\n");
301                         stage ++;
302                         ret = 0;
303         GNUNET_SCHEDULER_add_now (&end,NULL);
304                         return;
305                 }
306   }
307   return;
308
309 error:
310         /* Error ! */
311         ret = 1;
312         GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Error!\n");
313         GNUNET_SCHEDULER_add_now (&end,NULL);
314 }
315
316 static void
317 run (void *cls,
318      const struct GNUNET_CONFIGURATION_Handle *cfg,
319      struct GNUNET_TESTING_Peer *peer)
320 {
321   char *quota_str;
322
323   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", &quota_str))
324   {
325       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
326       ret = 1;
327       return;
328   }
329   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
330   {
331       fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
332       ret = 1;
333       GNUNET_free (quota_str);
334       return;
335   }
336   GNUNET_free (quota_str);
337   quota_str = NULL;
338
339   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", &quota_str))
340   {
341       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
342       ret = 1;
343       return;
344   }
345   if  (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
346   {
347       fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
348       GNUNET_free (quota_str);
349       ret = 1;
350       return;
351   }
352   GNUNET_free (quota_str);
353   quota_str = NULL;
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
355   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
356
357
358   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
359
360   /* Connect to ATS scheduling */
361   sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
362   if (sched_ats == NULL)
363   {
364     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
365     ret = 1;
366     end ();
367     return;
368   }
369
370   /* Connect to ATS performance */
371   perf_ats = GNUNET_ATS_performance_init(cfg, NULL, NULL);
372   if (sched_ats == NULL)
373   {
374     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
375     ret = 1;
376     end ();
377     return;
378   }
379
380
381   /* Set up peer 0 */
382   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
383   {
384       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
385       ret = GNUNET_SYSERR;
386       end ();
387       return;
388   }
389
390   GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
391
392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
393               GNUNET_i2s(&p[0].id));
394
395   /* Set up peer 1*/
396   if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
397   {
398       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
399       ret = GNUNET_SYSERR;
400       end ();
401       return;
402   }
403
404   GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
405
406   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
407               GNUNET_i2s(&p[1].id));
408
409   /* Prepare ATS Information */
410   test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
411   test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
412   test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
413   test_ats_info[1].value = htonl(1);
414   test_ats_count = 2;
415
416   /* Peer 0: Adding address with session */
417   test_session[0] = &test_addr[0];
418   create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
419   test_hello_address[0].peer = p[0].id;
420   test_hello_address[0].transport_name = test_addr[0].plugin;
421   test_hello_address[0].address = test_addr[0].addr;
422   test_hello_address[0].address_length = test_addr[0].addr_len;
423   GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
424
425   /* Peer 1: Adding address with session */
426   test_session[1] = &test_addr[1];
427   create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
428   test_hello_address[1].peer = p[1].id;
429   test_hello_address[1].transport_name = test_addr[1].plugin;
430   test_hello_address[1].address = test_addr[1].addr;
431   test_hello_address[1].address_length = test_addr[1].addr_len;
432   GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
433
434   GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
435   GNUNET_ATS_suggest_address (sched_ats, &p[1].id);
436 }
437
438
439 int
440 main (int argc, char *argv[])
441 {
442   if (0 != GNUNET_TESTING_peer_run ("test_ats_proportional_pref_aging",
443                                     "test_ats_api.conf",
444                                     &run, NULL))
445     return 1;
446   return ret;
447 }
448
449
450 /* end of file test_ats_simplistic_pref_aging.c */