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