Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / ats-tests / ats-testing.c
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2013, 2016, 2017 GNUnet e.V.
4
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14
15  You should have received a copy of the GNU Affero General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /**
19  * @file ats-tests/ats-testing.c
20  * @brief ats testing library: setup topology
21  * solvers
22  * @author Christian Grothoff
23  * @author Matthias Wachs
24  */
25 #include "ats-testing.h"
26
27
28 /**
29  * Connect peers with testbed
30  */
31 struct TestbedConnectOperation
32 {
33   /**
34    * The benchmarking master initiating this connection
35    */
36   struct BenchmarkPeer *master;
37
38   /**
39    * The benchmarking slave to connect to
40    */
41   struct BenchmarkPeer *slave;
42
43   /**
44    * Testbed operation to connect peers
45    */
46   struct GNUNET_TESTBED_Operation *connect_op;
47 };
48
49 struct GNUNET_CONFIGURATION_Handle *cfg;
50
51 struct GNUNET_ATS_TEST_Topology *top;
52
53
54 /**
55  * Shutdown nicely
56  *
57  * @param cls NULL
58  */
59 static void
60 do_shutdown (void *cls)
61 {
62   int c_m;
63   int c_s;
64   int c_op;
65   struct BenchmarkPeer *p;
66
67   top->state.benchmarking = GNUNET_NO;
68
69   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
70               "Benchmarking done\n");
71
72   GNUNET_ATS_TEST_generate_traffic_stop_all ();
73
74   for (c_m = 0; c_m < top->num_masters; c_m++)
75   {
76     p = &top->mps[c_m];
77     if (NULL != top->mps[c_m].peer_id_op)
78     {
79       GNUNET_TESTBED_operation_done (p->peer_id_op);
80       p->peer_id_op = NULL;
81     }
82
83     if (NULL != p->ats_task)
84       GNUNET_SCHEDULER_cancel (p->ats_task);
85     p->ats_task = NULL;
86
87     for (c_op = 0; c_op < p->num_partners; c_op++)
88     {
89       if ( (NULL != p->core_connect_ops) &&
90            (NULL != p->core_connect_ops[c_op].connect_op) )
91       {
92         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
93                     "Failed to connect peer 0 and %u\n",
94                     c_op);
95         GNUNET_TESTBED_operation_done (p->core_connect_ops[c_op].connect_op);
96         p->core_connect_ops[c_op].connect_op = NULL;
97       }
98     }
99
100     if (NULL != p->ats_perf_op)
101     {
102       GNUNET_TESTBED_operation_done (p->ats_perf_op);
103       p->ats_perf_op = NULL;
104     }
105
106     if (NULL != p->comm_op)
107     {
108       GNUNET_TESTBED_operation_done (p->comm_op);
109       p->comm_op = NULL;
110     }
111     GNUNET_free_non_null (p->core_connect_ops);
112     GNUNET_free(p->partners);
113     p->partners = NULL;
114   }
115
116   for (c_s = 0; c_s < top->num_slaves; c_s++)
117   {
118     p = &top->sps[c_s];
119     if (NULL != p->peer_id_op)
120     {
121       GNUNET_TESTBED_operation_done (p->peer_id_op);
122       p->peer_id_op = NULL;
123     }
124     if (NULL != p->ats_perf_op)
125     {
126       GNUNET_TESTBED_operation_done (p->ats_perf_op);
127       p->ats_perf_op = NULL;
128     }
129     if (NULL != p->comm_op)
130     {
131       GNUNET_TESTBED_operation_done (p->comm_op);
132       p->comm_op = NULL;
133     }
134     GNUNET_free(p->partners);
135     p->partners = NULL;
136   }
137   GNUNET_SCHEDULER_shutdown ();
138   GNUNET_free (top);
139   top = NULL;
140 }
141
142
143 static struct BenchmarkPartner *
144 find_partner (struct BenchmarkPeer *me,
145               const struct GNUNET_PeerIdentity *peer)
146 {
147   int c_m;
148
149   for (c_m = 0; c_m < me->num_partners; c_m++)
150   {
151     /* Find a partner with other as destination */
152     if (0 == memcmp (peer,
153                      &me->partners[c_m].dest->id,
154                      sizeof (struct GNUNET_PeerIdentity)))
155     {
156       return &me->partners[c_m];
157     }
158   }
159
160   return NULL;
161 }
162
163
164 static struct BenchmarkPeer *
165 find_peer (const struct GNUNET_PeerIdentity * peer)
166 {
167   int c_p;
168
169   for (c_p = 0; c_p < top->num_masters; c_p++)
170   {
171     if (0 == memcmp (&top->mps[c_p].id,
172                      peer,
173                      sizeof(struct GNUNET_PeerIdentity)))
174       return &top->mps[c_p];
175   }
176
177   for (c_p = 0; c_p < top->num_slaves; c_p++)
178   {
179     if (0 == memcmp (&top->sps[c_p].id,
180                      peer,
181                      sizeof(struct GNUNET_PeerIdentity)))
182       return &top->sps[c_p];
183   }
184   return NULL ;
185 }
186
187
188 /**
189  * Method called whenever a given peer connects.
190  *
191  * @param cls closure
192  * @param peer peer identity this notification is about
193  * @param mq queue to use to send messages to @a peer
194  * @return the `struct BenchmarkPartner` of @a peer
195  */
196 static void *
197 comm_connect_cb (void *cls,
198                  const struct GNUNET_PeerIdentity *peer,
199                  struct GNUNET_MQ_Handle *mq)
200 {
201   struct BenchmarkPeer *me = cls;
202   struct BenchmarkPeer *remote;
203   struct BenchmarkPartner *p;
204   char *id;
205   int c;
206   int completed;
207
208   remote = find_peer (peer);
209   if (NULL == remote)
210   {
211     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212                 "Unknown peer connected: `%s'\n",
213                 GNUNET_i2s (peer));
214     GNUNET_break (0);
215     return NULL;
216   }
217
218   id = GNUNET_strdup (GNUNET_i2s (&me->id));
219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220               "%s [%u] `%s' connected to %s [%u] %s\n",
221               (me->master == GNUNET_YES) ? "Master": "Slave",
222               me->no,
223               id,
224               (remote->master == GNUNET_YES) ? "Master": "Slave",
225               remote->no,
226               GNUNET_i2s (peer));
227
228   me->core_connections++;
229   if ((GNUNET_YES == me->master) &&
230       (GNUNET_NO == remote->master) &&
231       (GNUNET_NO == top->state.connected_CORE))
232   {
233     me->core_slave_connections++;
234
235     if (me->core_slave_connections == top->num_slaves)
236     {
237       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
238                   "Master [%u] connected all slaves\n",
239                   me->no);
240     }
241     completed = GNUNET_YES;
242     for (c = 0; c < top->num_masters; c++)
243     {
244       if (top->mps[c].core_slave_connections != top->num_slaves)
245         completed = GNUNET_NO;
246     }
247     if (GNUNET_YES == completed)
248     {
249       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
250                   "All master peers connected all slave peers\n");
251       top->state.connected_CORE = GNUNET_YES;
252       /* Notify about setup done */
253       if (NULL != top->done_cb)
254         top->done_cb (top->done_cb_cls,
255                       top->mps,
256                       top->sps);
257     }
258   }
259   GNUNET_free (id);
260   p = find_partner (me,
261                     peer);
262   if (NULL != p)
263     p->mq = mq;
264   return p;
265 }
266
267
268 /**
269  * @param cls this peer
270  * @param peer id of disconnecting peer
271  * @param internal_cls the `struct BenchmarkPartner` of @a peer
272  */
273 static void
274 comm_disconnect_cb (void *cls,
275                     const struct GNUNET_PeerIdentity *peer,
276                     void *internal_cls)
277 {
278   struct BenchmarkPeer *me = cls;
279   struct BenchmarkPartner *p = internal_cls;
280   char *id;
281
282   if (NULL == p)
283     return;
284
285   id = GNUNET_strdup (GNUNET_i2s (&me->id));
286   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287               "%s disconnected from %s\n",
288               id,
289               GNUNET_i2s (peer));
290   GNUNET_assert (me->core_connections > 0);
291   me->core_connections--;
292
293   if ( (GNUNET_YES == top->state.benchmarking) &&
294        ( (GNUNET_YES == me->master) ||
295          (GNUNET_YES == p->dest->master) ) )
296   {
297     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298                 "%s disconnected from %s while benchmarking\n",
299                 id,
300                 GNUNET_i2s (peer));
301   }
302   GNUNET_free(id);
303 }
304
305
306 static void
307 handle_pong (void *cls,
308              const struct TestMessage *message)
309 {
310   struct BenchmarkPartner *p = cls;
311
312   GNUNET_ATS_TEST_traffic_handle_pong (p);
313 }
314
315
316 static void
317 handle_ping (void *cls,
318              const struct TestMessage *message)
319 {
320   struct BenchmarkPartner *p = cls;
321
322   GNUNET_ATS_TEST_traffic_handle_ping (p);
323 }
324
325
326 static void *
327 transport_connect_adapter (void *cls,
328                            const struct GNUNET_CONFIGURATION_Handle *cfg)
329 {
330   struct BenchmarkPeer *me = cls;
331   struct GNUNET_MQ_MessageHandler handlers[] = {
332     GNUNET_MQ_hd_fixed_size (ping,
333                              TEST_MESSAGE_TYPE_PING,
334                              struct TestMessage,
335                              me),
336     GNUNET_MQ_hd_fixed_size (pong,
337                              TEST_MESSAGE_TYPE_PONG,
338                              struct TestMessage,
339                              me),
340     GNUNET_MQ_handler_end ()
341   };
342
343   me->th = GNUNET_TRANSPORT_core_connect (cfg,
344                                           &me->id,
345                                           handlers,
346                                           me,
347                                           &comm_connect_cb,
348                                           &comm_disconnect_cb,
349                                           NULL);
350   if (NULL == me->th)
351     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352                 "Failed to create transport connection \n");
353   return me->th;
354 }
355
356
357 static void
358 transport_disconnect_adapter (void *cls,
359                               void *op_result)
360 {
361   struct BenchmarkPeer *me = cls;
362
363   GNUNET_TRANSPORT_core_disconnect (me->th);
364   me->th = NULL;
365 }
366
367
368 static void *
369 core_connect_adapter (void *cls,
370                       const struct GNUNET_CONFIGURATION_Handle *cfg)
371 {
372   struct BenchmarkPeer *me = cls;
373   struct GNUNET_MQ_MessageHandler handlers[] = {
374     GNUNET_MQ_hd_fixed_size (ping,
375                              TEST_MESSAGE_TYPE_PING,
376                              struct TestMessage,
377                              me),
378     GNUNET_MQ_hd_fixed_size (pong,
379                              TEST_MESSAGE_TYPE_PONG,
380                              struct TestMessage,
381                              me),
382     GNUNET_MQ_handler_end ()
383   };
384
385   me->ch = GNUNET_CORE_connect (cfg,
386                                 me,
387                                 NULL,
388                                 &comm_connect_cb,
389                                 &comm_disconnect_cb,
390                                 handlers);
391   if (NULL == me->ch)
392     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393                 "Failed to create core connection \n");
394   return me->ch;
395 }
396
397
398 static void
399 core_disconnect_adapter (void *cls,
400                          void *op_result)
401 {
402   struct BenchmarkPeer *me = cls;
403
404   GNUNET_CORE_disconnect (me->ch);
405   me->ch = NULL;
406 }
407
408
409 static void
410 connect_completion_callback (void *cls,
411                              struct GNUNET_TESTBED_Operation *op,
412                              const char *emsg)
413 {
414   struct TestbedConnectOperation *cop = cls;
415   static int ops = 0;
416   int c;
417   if (NULL == emsg)
418   {
419     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
420                _("Connected master [%u] with slave [%u]\n"),
421                cop->master->no,
422                cop->slave->no);
423   }
424   else
425   {
426     GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
427         _("Failed to connect master peer [%u] with slave [%u]\n"),
428         cop->master->no, cop->slave->no);
429     GNUNET_break(0);
430     GNUNET_SCHEDULER_shutdown ();
431   }
432   GNUNET_TESTBED_operation_done (op);
433   ops++;
434   for (c = 0; c < top->num_slaves; c++)
435   {
436     if (cop == &cop->master->core_connect_ops[c])
437       cop->master->core_connect_ops[c].connect_op = NULL;
438   }
439   if (ops == top->num_masters * top->num_slaves)
440   {
441     top->state.connected_PEERS = GNUNET_YES;
442   }
443 }
444
445
446 static void
447 do_connect_peers (void *cls)
448 {
449   int c_m;
450   int c_s;
451   struct BenchmarkPeer *p;
452
453   if ((top->state.connected_ATS_service == GNUNET_NO) ||
454       (top->state.connected_COMM_service == GNUNET_NO))
455     return;
456
457   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
458               "Connecting peers on CORE level\n");
459   for (c_m = 0; c_m < top->num_masters; c_m++)
460   {
461     p = &top->mps[c_m];
462     p->core_connect_ops = GNUNET_malloc (top->num_slaves *
463         sizeof (struct TestbedConnectOperation));
464
465     for (c_s = 0; c_s < top->num_slaves; c_s++)
466     {
467       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468                  "Connecting master [%u] with slave [%u]\n",
469                   p->no,
470                   top->sps[c_s].no);
471       p->core_connect_ops[c_s].master = p;
472       p->core_connect_ops[c_s].slave = &top->sps[c_s];
473       p->core_connect_ops[c_s].connect_op
474         = GNUNET_TESTBED_overlay_connect (NULL,
475                                           &connect_completion_callback,
476                                           &p->core_connect_ops[c_s],
477                                           top->sps[c_s].peer,
478                                           p->peer);
479       if (NULL == p->core_connect_ops[c_s].connect_op)
480        {
481         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
482                     "Could not connect master [%u] and slave [%u]\n",
483                     p->no,
484                     top->sps[c_s].no);
485         GNUNET_break(0);
486         GNUNET_SCHEDULER_shutdown ();
487         return;
488       }
489     }
490   }
491 }
492
493
494 static void
495 comm_connect_completion_cb (void *cls,
496                             struct GNUNET_TESTBED_Operation *op,
497                             void *ca_result,
498                             const char *emsg)
499 {
500   static int comm_done = 0;
501
502   if ((NULL != emsg) || (NULL == ca_result))
503   {
504     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
505                "Initialization failed, shutdown\n");
506     GNUNET_break(0);
507     GNUNET_SCHEDULER_shutdown ();
508     return;
509   }
510   comm_done++;
511
512   if (comm_done == top->num_slaves + top->num_masters)
513   {
514     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
515                 "Connected to all %s services\n",
516                 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
517     top->state.connected_COMM_service = GNUNET_YES;
518     GNUNET_SCHEDULER_add_now (&do_connect_peers,
519                               NULL);
520   }
521 }
522
523
524 static void
525 do_comm_connect (void *cls)
526 {
527   int c_s;
528   int c_m;
529   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
530               "Connecting to all %s services\n",
531               (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
532   for (c_m = 0; c_m < top->num_masters; c_m++)
533   {
534     if (GNUNET_YES == top->test_core)
535       top->mps[c_m].comm_op
536         = GNUNET_TESTBED_service_connect (NULL,
537                                           top->mps[c_m].peer,
538                                           "core",
539                                           &comm_connect_completion_cb,
540                                           NULL,
541                                           &core_connect_adapter,
542                                           &core_disconnect_adapter,
543                                           &top->mps[c_m]);
544     else
545     {
546       top->mps[c_m].comm_op
547         = GNUNET_TESTBED_service_connect (NULL,
548                                           top->mps[c_m].peer,
549                                           "transport",
550                                           &comm_connect_completion_cb,
551                                           NULL,
552                                           &transport_connect_adapter,
553                                           &transport_disconnect_adapter,
554                                           &top->mps[c_m]);
555     }
556   }
557
558   for (c_s = 0; c_s < top->num_slaves; c_s++)
559   {
560     if (GNUNET_YES == top->test_core)
561       top->sps[c_s].comm_op
562         = GNUNET_TESTBED_service_connect (NULL,
563                                           top->sps[c_s].peer,
564                                           "core",
565                                           &comm_connect_completion_cb,
566                                           NULL,
567                                           &core_connect_adapter,
568                                           &core_disconnect_adapter,
569                                           &top->sps[c_s]);
570     else
571     {
572       top->sps[c_s].comm_op
573         = GNUNET_TESTBED_service_connect (NULL,
574                                           top->sps[c_s].peer,
575                                           "transport",
576                                           &comm_connect_completion_cb,
577                                           NULL,
578                                           &transport_connect_adapter,
579                                           &transport_disconnect_adapter,
580                                           &top->sps[c_s]);
581     }
582   }
583 }
584
585
586 static void
587 ats_performance_info_cb (void *cls,
588                          const struct GNUNET_HELLO_Address *address,
589                          int address_active,
590                          struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
591                          struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
592                          const struct GNUNET_ATS_Properties *ats_prop)
593 {
594   struct BenchmarkPeer *me = cls;
595   struct BenchmarkPartner *p;
596   int log;
597   char *peer_id;
598
599   if (NULL == address)
600   {
601     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
602                 "Peer %u: ATS Service disconnected!\n",
603                 me->no);
604     return;
605   }
606
607   p = find_partner (me,
608                     &address->peer);
609   if (NULL == p)
610   {
611     /* This is not one of my partners
612      * Will happen since the peers will connect to each other due to gossiping
613      */
614     return;
615   }
616   peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
617
618   log = GNUNET_NO;
619   if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
620       (p->bandwidth_out != ntohl (bandwidth_out.value__)))
621       log = GNUNET_YES;
622   p->bandwidth_in = ntohl (bandwidth_in.value__);
623   p->bandwidth_out = ntohl (bandwidth_out.value__);
624
625   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626               "%s [%u] received ATS information: %s\n",
627               (GNUNET_YES == p->me->master) ? "Master" : "Slave",
628               p->me->no,
629               GNUNET_i2s (&p->dest->id));
630
631   p->props.utilization_out = ats_prop->utilization_out;
632   p->props.utilization_in = ats_prop->utilization_in;
633   p->props.scope = ats_prop->scope;
634   p->props.delay = ats_prop->delay;
635   p->props.distance = ats_prop->distance;
636
637   if (GNUNET_YES == log)
638     top->ats_perf_cb (cls, address,
639                       address_active,
640                       bandwidth_out,
641                       bandwidth_in,
642                       ats_prop);
643   GNUNET_free(peer_id);
644 }
645
646
647 static void *
648 ats_perf_connect_adapter (void *cls,
649                           const struct GNUNET_CONFIGURATION_Handle *cfg)
650 {
651   struct BenchmarkPeer *me = cls;
652
653   me->ats_perf_handle
654     = GNUNET_ATS_performance_init (cfg,
655                                    &ats_performance_info_cb,
656                                    me);
657   if (NULL == me->ats_perf_handle)
658     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
659                 "Failed to create ATS performance handle \n");
660   return me->ats_perf_handle;
661 }
662
663
664 static void
665 ats_perf_disconnect_adapter (void *cls,
666                              void *op_result)
667 {
668   struct BenchmarkPeer *me = cls;
669
670   GNUNET_ATS_performance_done (me->ats_perf_handle);
671   me->ats_perf_handle = NULL;
672 }
673
674
675 static void
676 ats_connect_completion_cb (void *cls,
677                            struct GNUNET_TESTBED_Operation *op,
678                            void *ca_result,
679                            const char *emsg)
680 {
681   static int op_done = 0;
682
683   if ((NULL != emsg) || (NULL == ca_result))
684   {
685     GNUNET_log(GNUNET_ERROR_TYPE_INFO,
686                "Initialization failed, shutdown\n");
687     GNUNET_break(0);
688     GNUNET_SCHEDULER_shutdown ();
689     return;
690   }
691   op_done++;
692   if (op_done == (top->num_masters + top->num_slaves))
693   {
694     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
695                 "Connected to all ATS services\n");
696     top->state.connected_ATS_service = GNUNET_YES;
697     GNUNET_SCHEDULER_add_now (&do_comm_connect,
698                               NULL);
699   }
700 }
701
702
703 static void
704 do_connect_ats (void *cls)
705 {
706   int c_m;
707   int c_s;
708
709   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
710               "Connecting to all ATS services\n");
711   for (c_m = 0; c_m < top->num_masters; c_m++)
712   {
713     top->mps[c_m].ats_perf_op
714       = GNUNET_TESTBED_service_connect (NULL,
715                                         top->mps[c_m].peer,
716                                         "ats",
717                                         &ats_connect_completion_cb,
718                                         NULL,
719                                         &ats_perf_connect_adapter,
720                                         &ats_perf_disconnect_adapter,
721                                         &top->mps[c_m]);
722   }
723
724   for (c_s = 0; c_s < top->num_slaves; c_s++)
725   {
726     top->sps[c_s].ats_perf_op
727       = GNUNET_TESTBED_service_connect (NULL,
728                                         top->sps[c_s].peer,
729                                         "ats",
730                                         &ats_connect_completion_cb,
731                                         NULL,
732                                         &ats_perf_connect_adapter,
733                                         &ats_perf_disconnect_adapter,
734                                         &top->sps[c_s]);
735   }
736 }
737
738
739
740 static void
741 peerinformation_cb (void *cb_cls,
742                     struct GNUNET_TESTBED_Operation *op,
743                     const struct GNUNET_TESTBED_PeerInformation *pinfo,
744                     const char *emsg)
745 {
746   struct BenchmarkPeer *p = cb_cls;
747   static int done = 0;
748
749   GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
750
751   p->id = *pinfo->result.id;
752   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
753               "%s [%u] has peer id `%s'\n",
754               (p->master == GNUNET_YES) ? "Master" : "Slave",
755               p->no,
756               GNUNET_i2s (&p->id));
757
758   GNUNET_TESTBED_operation_done (op);
759   p->peer_id_op = NULL;
760   done++;
761
762   if (done == top->num_slaves + top->num_masters)
763   {
764     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
765                 "Retrieved all peer ID, connect to ATS\n");
766     GNUNET_SCHEDULER_add_now (&do_connect_ats,
767                               NULL);
768   }
769 }
770
771
772 /**
773  * Signature of a main function for a testcase.
774  *
775  * @param cls closure
776  * @param h testbed handle
777  * @param num_peers number of peers in 'peers'
778  * @param peers_ handle to peers run in the testbed
779  * @param links_succeeded the number of overlay link connection attempts that
780  *          succeeded
781  * @param links_failed the number of overlay link connection attempts that
782  *          failed
783  */
784 static void
785 main_run (void *cls,
786           struct GNUNET_TESTBED_RunHandle *h,
787           unsigned int num_peers,
788           struct GNUNET_TESTBED_Peer **peers_,
789           unsigned int links_succeeded,
790           unsigned int links_failed)
791 {
792   int c_m;
793   int c_s;
794
795   GNUNET_assert (NULL == cls);
796   GNUNET_assert (top->num_masters + top->num_slaves == num_peers);
797   GNUNET_assert (NULL != peers_);
798
799   GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
800                                  top);
801
802   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
803               "Setting up %u masters and %u slaves\n",
804               top->num_masters,
805               top->num_slaves);
806
807   /* Setup master peers */
808   for (c_m = 0; c_m < top->num_masters; c_m++)
809   {
810     GNUNET_assert(NULL != peers_[c_m]);
811     top->mps[c_m].peer = peers_[c_m];
812     top->mps[c_m].no = c_m;
813     top->mps[c_m].master = GNUNET_YES;
814     top->mps[c_m].pref_partner = &top->sps[c_m];
815     top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
816     top->mps[c_m].partners =
817         GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
818     top->mps[c_m].num_partners = top->num_slaves;
819     /* Initialize partners */
820     for (c_s = 0; c_s < top->num_slaves; c_s++)
821     {
822       top->mps[c_m].partners[c_s].me = &top->mps[c_m];
823       top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
824     }
825     /* Get configuration */
826     top->mps[c_m].peer_id_op
827       = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
828                                              GNUNET_TESTBED_PIT_IDENTITY,
829                                              &peerinformation_cb,
830                                              &top->mps[c_m]);
831   }
832
833   /* Setup slave peers */
834   for (c_s = 0; c_s < top->num_slaves; c_s++)
835   {
836     GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
837     top->sps[c_s].peer = peers_[c_s + top->num_masters];
838     top->sps[c_s].no = c_s + top->num_masters;
839     top->sps[c_s].master = GNUNET_NO;
840     top->sps[c_s].partners =
841       GNUNET_new_array (top->num_masters,
842                         struct BenchmarkPartner);
843     top->sps[c_s].num_partners = top->num_masters;
844     /* Initialize partners */
845     for (c_m = 0; c_m < top->num_masters; c_m++)
846     {
847       top->sps[c_s].partners[c_m].me = &top->sps[c_s];
848       top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
849
850       /* Initialize properties */
851       top->sps[c_s].partners[c_m].props.delay = GNUNET_TIME_UNIT_ZERO;
852       top->sps[c_s].partners[c_m].props.distance = 0;
853       top->sps[c_s].partners[c_m].props.scope = GNUNET_ATS_NET_UNSPECIFIED;
854       top->sps[c_s].partners[c_m].props.utilization_in = 0;
855       top->sps[c_s].partners[c_m].props.utilization_out = 0;
856     }
857     /* Get configuration */
858     top->sps[c_s].peer_id_op
859       = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
860                                              GNUNET_TESTBED_PIT_IDENTITY,
861                                              &peerinformation_cb,
862                                              &top->sps[c_s]);
863   }
864 }
865
866
867 /**
868  * Controller event callback
869  *
870  * @param cls NULL
871  * @param event the controller event
872  */
873 static void
874 controller_event_cb (void *cls,
875                      const struct GNUNET_TESTBED_EventInformation *event)
876 {
877   switch (event->type)
878   {
879   case GNUNET_TESTBED_ET_CONNECT:
880     break;
881   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
882     break;
883   default:
884     GNUNET_break(0);
885     GNUNET_SCHEDULER_shutdown ();
886   }
887 }
888
889
890 struct BenchmarkPeer *
891 GNUNET_ATS_TEST_get_peer (int src)
892 {
893   if (src > top->num_masters)
894     return NULL;
895   return &top->mps[src];
896 }
897
898
899 struct BenchmarkPartner *
900 GNUNET_ATS_TEST_get_partner (int src,
901                              int dest)
902 {
903   if (src > top->num_masters)
904     return NULL;
905   if (dest > top->num_slaves)
906     return NULL;
907   return &top->mps[src].partners[dest];
908 }
909
910
911 /**
912  * Create a topology for ats testing
913  *
914  * @param name test name
915  * @param cfg_file configuration file to use for the peers
916  * @param num_slaves number of slaves
917  * @param num_masters number of masters
918  * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
919  * @param done_cb function to call when topology is setup
920  * @param done_cb_cls cls for callback
921  * @param log_request_cb callback to call when logging is required
922  */
923 void
924 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
925                                  unsigned int num_slaves,
926                                  unsigned int num_masters,
927                                  int test_core,
928                                  GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
929                                  void *done_cb_cls,
930                                  GNUNET_ATS_AddressInformationCallback log_request_cb)
931 {
932   top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
933   top->num_masters = num_masters;
934   top->num_slaves = num_slaves;
935   top->done_cb = done_cb;
936   top->done_cb_cls = done_cb_cls;
937   top->test_core = test_core;
938   top->ats_perf_cb = log_request_cb;
939   top->mps = GNUNET_new_array (num_masters,
940                                struct BenchmarkPeer);
941   top->sps = GNUNET_new_array (num_slaves,
942                                struct BenchmarkPeer);
943
944   /* Start topology */
945   uint64_t event_mask;
946   event_mask = 0;
947   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
948   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
949   (void) GNUNET_TESTBED_test_run (name,
950                                   cfg_file,
951                                   num_slaves + num_masters,
952                                   event_mask,
953                                   &controller_event_cb, NULL,
954                                   &main_run, NULL);
955 }
956
957
958 /**
959  * Shutdown topology
960  */
961 void
962 GNUNET_ATS_TEST_shutdown_topology (void)
963 {
964   if (NULL == top)
965     return;
966   GNUNET_SCHEDULER_shutdown();
967 }
968
969
970 /* end of file ats-testing.c */