2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file ats-tests/ats-testing.c
22 * @brief ats testing library: setup topology and provide logging to test ats
24 * @author Christian Grothoff
25 * @author Matthias Wachs
27 #include "ats-testing.h"
29 static struct GNUNET_ATS_TEST_Topology *top;
35 * @param tc the task context
38 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
43 struct BenchmarkPeer *p;
45 if (GNUNET_YES == top->logging)
46 GNUNET_ATS_TEST_logging_stop ();
48 top->shutdown_task = GNUNET_SCHEDULER_NO_TASK;
50 top->state.benchmarking = GNUNET_NO;
51 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
53 for (c_m = 0; c_m < top->num_masters; c_m++)
56 if (NULL != top->mps[c_m].peer_id_op)
58 GNUNET_TESTBED_operation_done (p->peer_id_op);
62 if (GNUNET_SCHEDULER_NO_TASK != p->ats_task)
63 GNUNET_SCHEDULER_cancel (p->ats_task);
64 p->ats_task = GNUNET_SCHEDULER_NO_TASK;
66 for (c_op = 0; c_op < p->num_partners; c_op++)
68 if (NULL != p->partners[c_op].cth)
70 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
71 p->partners[c_op].cth = NULL;
73 if (NULL != p->partners[c_op].tth)
75 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
76 p->partners[c_op].tth = NULL;
78 if ( (NULL != p->core_connect_ops) &&
79 (NULL != p->core_connect_ops[c_op].connect_op) )
81 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
82 _("Failed to connect peer 0 and %u\n"), c_op);
83 GNUNET_TESTBED_operation_done (
84 p->core_connect_ops[c_op].connect_op);
85 p->core_connect_ops[c_op].connect_op = NULL;
89 if (NULL != p->ats_perf_op)
91 GNUNET_TESTBED_operation_done (p->ats_perf_op);
92 p->ats_perf_op = NULL;
95 if (NULL != p->comm_op)
97 GNUNET_TESTBED_operation_done (p->comm_op);
100 GNUNET_free_non_null (p->core_connect_ops);
101 GNUNET_free(p->partners);
105 for (c_s = 0; c_s < top->num_slaves; c_s++)
108 if (NULL != p->peer_id_op)
110 GNUNET_TESTBED_operation_done (p->peer_id_op);
111 p->peer_id_op = NULL;
114 for (c_op = 0; c_op < p->num_partners; c_op++)
116 if (NULL != p->partners[c_op].cth)
118 GNUNET_CORE_notify_transmit_ready_cancel (p->partners[c_op].cth);
119 p->partners[c_op].cth = NULL;
121 if (NULL != p->partners[c_op].tth)
123 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->partners[c_op].tth);
124 p->partners[c_op].tth = NULL;
127 if (NULL != p->ats_perf_op)
129 GNUNET_TESTBED_operation_done (p->ats_perf_op);
130 p->ats_perf_op = NULL;
132 if (NULL != p->comm_op)
134 GNUNET_TESTBED_operation_done (p->comm_op);
137 GNUNET_free(p->partners);
141 GNUNET_SCHEDULER_shutdown ();
144 static struct BenchmarkPartner *
145 find_partner (struct BenchmarkPeer *me, const struct GNUNET_PeerIdentity * peer)
148 GNUNET_assert (NULL != me);
149 GNUNET_assert (NULL != peer);
151 for (c_m = 0; c_m < me->num_partners; c_m++)
153 /* Find a partner with other as destination */
154 if (0 == memcmp (peer, &me->partners[c_m].dest->id,
155 sizeof(struct GNUNET_PeerIdentity)))
157 return &me->partners[c_m];
165 static struct BenchmarkPeer *
166 find_peer (const struct GNUNET_PeerIdentity * peer)
170 for (c_p = 0; c_p < top->num_masters; c_p++)
172 if (0 == memcmp (&top->mps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
173 return &top->mps[c_p];
176 for (c_p = 0; c_p < top->num_slaves; c_p++)
178 if (0 == memcmp (&top->sps[c_p].id, peer, sizeof(struct GNUNET_PeerIdentity)))
179 return &top->sps[c_p];
186 * Method called whenever a given peer connects.
189 * @param peer peer identity this notification is about
192 comm_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
194 struct BenchmarkPeer *me = cls;
195 struct BenchmarkPeer *remote;
200 remote = find_peer (peer);
203 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unknown peer connected: `%s'\n", GNUNET_i2s (peer));
208 id = GNUNET_strdup (GNUNET_i2s (&me->id));
209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s [%u] `%s' connected to %s [%u] %s\n",
210 (me->master == GNUNET_YES) ? "Master": "Slave", me->no, id,
211 (remote->master == GNUNET_YES) ? "Master": "Slave", remote->no,
214 me->core_connections++;
215 if ((GNUNET_YES == me->master) && (GNUNET_NO == remote->master)
216 && (GNUNET_NO == top->state.connected_CORE))
218 me->core_slave_connections++;
220 if (me->core_slave_connections == top->num_slaves)
222 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Master [%u] connected all slaves\n",
225 completed = GNUNET_YES;
226 for (c = 0; c < top->num_masters; c++)
228 if (top->mps[c].core_slave_connections != top->num_slaves)
229 completed = GNUNET_NO;
231 if (GNUNET_YES == completed)
233 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
234 "All master peers connected all slave peers\n", id,
236 top->state.connected_CORE = GNUNET_YES;
243 comm_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
245 struct BenchmarkPeer *me = cls;
246 struct BenchmarkPartner *p;
249 if (NULL == (p = find_partner (me, peer)))
252 id = GNUNET_strdup (GNUNET_i2s (&me->id));
253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "%s disconnected from %s \n", id,
255 GNUNET_assert(me->core_connections > 0);
256 me->core_connections--;
258 if ((GNUNET_YES == top->state.benchmarking)
259 && ((GNUNET_YES == me->master) || (GNUNET_YES == p->dest->master)))
261 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
262 "%s disconnected from %s while benchmarking \n", id, GNUNET_i2s (peer));
265 GNUNET_TRANSPORT_notify_transmit_ready_cancel (p->tth);
270 GNUNET_CORE_notify_transmit_ready_cancel (p->cth);
279 core_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
281 struct BenchmarkPeer *me = cls;
283 me->ch = GNUNET_CORE_connect (cfg, me, NULL, comm_connect_cb,
284 comm_disconnect_cb, NULL, GNUNET_NO, NULL, GNUNET_NO, top->handlers);
286 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create core connection \n");
291 core_disconnect_adapter (void *cls, void *op_result)
293 struct BenchmarkPeer *me = cls;
295 GNUNET_CORE_disconnect (me->ch);
301 transport_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
303 struct BenchmarkPeer *me = cls;
305 me->th = GNUNET_TRANSPORT_connect (cfg, &me->id, me, top->transport_recv_cb,
306 &comm_connect_cb, &comm_disconnect_cb);
308 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to create transport connection \n");
313 transport_disconnect_adapter (void *cls, void *op_result)
315 struct BenchmarkPeer *me = cls;
317 GNUNET_TRANSPORT_disconnect (me->th);
323 connect_completion_callback (void *cls, struct GNUNET_TESTBED_Operation *op,
326 struct TestbedConnectOperation *cop = cls;
331 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
332 _("Connected master [%u] with slave [%u]\n"), cop->master->no,
337 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
338 _("Failed to connect master peer [%u] with slave [%u]\n"),
339 cop->master->no, cop->slave->no);
341 if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
342 GNUNET_SCHEDULER_cancel (top->shutdown_task);
343 top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
345 GNUNET_TESTBED_operation_done (op);
347 for (c = 0; c < top->num_slaves; c++)
349 if (cop == &cop->master->core_connect_ops[c])
350 cop->master->core_connect_ops[c].connect_op = NULL;
352 if (ops == top->num_masters * top->num_slaves)
354 top->state.connected_PEERS = GNUNET_YES;
355 /* Notify about setup done */
356 if (NULL != top->done_cb)
357 top->done_cb (top->done_cb_cls, top->mps, top->sps);
362 do_connect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
366 struct BenchmarkPeer *p;
368 if ((top->state.connected_ATS_service == GNUNET_NO) ||
369 (top->state.connected_COMM_service == GNUNET_NO))
372 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Connecting peers on CORE level\n"));
374 for (c_m = 0; c_m < top->num_masters; c_m++)
377 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
378 sizeof (struct TestbedConnectOperation));
380 for (c_s = 0; c_s < top->num_slaves; c_s++)
382 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
383 _("Connecting master [%u] with slave [%u]\n"), p->no, top->sps[c_s].no);
384 p->core_connect_ops[c_s].master = p;
385 p->core_connect_ops[c_s].slave = &top->sps[c_s];
386 p->core_connect_ops[c_s].connect_op = GNUNET_TESTBED_overlay_connect (
387 NULL, &connect_completion_callback, &p->core_connect_ops[c_s],
388 top->sps[c_s].peer, p->peer);
389 if (NULL == p->core_connect_ops[c_s].connect_op)
391 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
392 _("Could not connect master [%u] and slave [%u]\n"), p->no,
395 if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
396 GNUNET_SCHEDULER_cancel (top->shutdown_task);
397 top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
406 comm_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
407 void *ca_result, const char *emsg)
409 static int comm_done = 0;
410 if ((NULL != emsg) || (NULL == ca_result))
412 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
414 if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
415 GNUNET_SCHEDULER_cancel (top->shutdown_task);
416 top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
421 if (comm_done == top->num_slaves + top->num_masters)
423 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all %s services\n",
424 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
425 top->state.connected_COMM_service = GNUNET_YES;
426 GNUNET_SCHEDULER_add_now (&do_connect_peers, NULL );
431 do_comm_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
435 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all %s services\n",
436 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
437 for (c_m = 0; c_m < top->num_masters; c_m++)
439 if (GNUNET_YES == top->test_core)
440 top->mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, top->mps[c_m].peer,
441 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
442 &core_disconnect_adapter, &top->mps[c_m]);
445 top->mps[c_m].comm_op = GNUNET_TESTBED_service_connect (NULL, top->mps[c_m].peer,
446 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
447 &transport_disconnect_adapter, &top->mps[c_m]);
451 for (c_s = 0; c_s < top->num_slaves; c_s++)
453 if (GNUNET_YES == top->test_core)
454 top->sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
455 "core", &comm_connect_completion_cb, NULL, &core_connect_adapter,
456 &core_disconnect_adapter, &top->sps[c_s]);
459 top->sps[c_s].comm_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
460 "transport", &comm_connect_completion_cb, NULL, &transport_connect_adapter,
461 &transport_disconnect_adapter, &top->sps[c_s]);
468 ats_perf_connect_adapter (void *cls,
469 const struct GNUNET_CONFIGURATION_Handle *cfg)
471 struct BenchmarkPeer *me = cls;
473 me->ats_perf_handle = GNUNET_ATS_performance_init (cfg, top->ats_perf_cb, me);
474 if (NULL == me->ats_perf_handle)
475 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
476 "Failed to create ATS performance handle \n");
477 return me->ats_perf_handle;
481 ats_perf_disconnect_adapter (void *cls, void *op_result)
483 struct BenchmarkPeer *me = cls;
485 GNUNET_ATS_performance_done (me->ats_perf_handle);
486 me->ats_perf_handle = NULL;
490 ats_connect_completion_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
491 void *ca_result, const char *emsg)
493 static int op_done = 0;
495 if ((NULL != emsg) || (NULL == ca_result))
497 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Initialization failed, shutdown\n"));
499 if (GNUNET_SCHEDULER_NO_TASK != top->shutdown_task)
500 GNUNET_SCHEDULER_cancel (top->shutdown_task);
501 top->shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL );
505 if (op_done == (top->num_masters + top->num_slaves))
507 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connected to all ATS services\n");
508 top->state.connected_ATS_service = GNUNET_YES;
509 GNUNET_SCHEDULER_add_now (&do_comm_connect, NULL );
515 do_connect_ats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
520 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Connecting to all ATS services\n");
521 for (c_m = 0; c_m < top->num_masters; c_m++)
523 top->mps[c_m].ats_perf_op = GNUNET_TESTBED_service_connect (NULL,
525 "ats", ats_connect_completion_cb, NULL,
526 &ats_perf_connect_adapter,
527 &ats_perf_disconnect_adapter, &top->mps[c_m]);
530 for (c_s = 0; c_s < top->num_slaves; c_s++)
532 top->sps[c_s].ats_perf_op = GNUNET_TESTBED_service_connect (NULL, top->sps[c_s].peer,
533 "ats", ats_connect_completion_cb, NULL, &ats_perf_connect_adapter,
534 &ats_perf_disconnect_adapter, &top->sps[c_s]);
541 peerinformation_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
542 const struct GNUNET_TESTBED_PeerInformation*pinfo, const char *emsg)
544 struct BenchmarkPeer *p = cb_cls;
547 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
549 p->id = *pinfo->result.id;
550 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%s [%u] has peer id `%s'\n",
551 (p->master == GNUNET_YES) ? "Master" : "Slave", p->no,
552 GNUNET_i2s (&p->id));
554 GNUNET_TESTBED_operation_done (op);
555 p->peer_id_op = NULL;
558 if (done == top->num_slaves + top->num_masters)
560 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
561 "Retrieved all peer ID, connect to ATS\n");
562 GNUNET_SCHEDULER_add_now (&do_connect_ats, NULL );
567 * Signature of a main function for a testcase.
570 * @param num_peers number of peers in 'peers'
571 * @param peers_ handle to peers run in the testbed
572 * @param links_succeeded the number of overlay link connection attempts that
574 * @param links_failed the number of overlay link connection attempts that
578 main_run (void *cls, struct GNUNET_TESTBED_RunHandle *h,
579 unsigned int num_peers,
580 struct GNUNET_TESTBED_Peer **peers_,
581 unsigned int links_succeeded,
582 unsigned int links_failed)
586 GNUNET_assert(NULL == cls);
587 GNUNET_assert(top->num_masters + top->num_slaves == num_peers);
588 GNUNET_assert(NULL != peers_);
590 top->shutdown_task = GNUNET_SCHEDULER_add_delayed (
591 GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown, top);
593 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Setting up %u masters and %u slaves\n",
594 top->num_masters, top->num_slaves);
596 /* Setup master peers */
597 for (c_m = 0; c_m < top->num_masters; c_m++)
599 GNUNET_assert(NULL != peers_[c_m]);
600 top->mps[c_m].peer = peers_[c_m];
601 top->mps[c_m].no = c_m;
602 top->mps[c_m].master = GNUNET_YES;
603 top->mps[c_m].pref_partner = &top->sps[c_m];
604 top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
605 top->mps[c_m].partners =
606 GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
607 top->mps[c_m].num_partners = top->num_slaves;
608 /* Initialize partners */
609 for (c_s = 0; c_s < top->num_slaves; c_s++)
611 top->mps[c_m].partners[c_s].me = &top->mps[c_m];
612 top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
614 /* Get configuration */
615 top->mps[c_m].peer_id_op = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
616 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &top->mps[c_m]);
619 /* Setup slave peers */
620 for (c_s = 0; c_s < top->num_slaves; c_s++)
622 GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
623 top->sps[c_s].peer = peers_[c_s + top->num_masters];
624 top->sps[c_s].no = c_s + top->num_masters;
625 top->sps[c_s].master = GNUNET_NO;
626 top->sps[c_s].partners =
627 GNUNET_malloc (top->num_masters * sizeof (struct BenchmarkPartner));
628 top->sps[c_s].num_partners = top->num_masters;
629 /* Initialize partners */
630 for (c_m = 0; c_m < top->num_masters; c_m++)
632 top->sps[c_s].partners[c_m].me = &top->sps[c_s];
633 top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
635 /* Get configuration */
636 top->sps[c_s].peer_id_op = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
637 GNUNET_TESTBED_PIT_IDENTITY, &peerinformation_cb, &top->sps[c_s]);
642 * Controller event callback
645 * @param event the controller event
648 controller_event_cb (void *cls,
649 const struct GNUNET_TESTBED_EventInformation *event)
651 struct GNUNET_ATS_TEST_Topology *top = cls;
654 case GNUNET_TESTBED_ET_CONNECT:
656 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
660 GNUNET_SCHEDULER_cancel (top->shutdown_task);
661 top->shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL );
666 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
667 unsigned int num_slaves,
668 unsigned int num_masters,
670 GNUNET_ATS_TESTING_TopologySetupDoneCallback done_cb,
672 struct GNUNET_CORE_MessageHandler *handlers,
673 GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb,
674 GNUNET_ATS_AddressInformationCallback ats_perf_cb)
677 top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
678 top->num_masters = num_masters;
679 top->num_slaves = num_slaves;
680 top->handlers = handlers;
681 top->done_cb = done_cb;
682 top->done_cb_cls = done_cb_cls;
683 top->test_core = test_core;
684 top->transport_recv_cb = transport_recv_cb;
685 top->ats_perf_cb = ats_perf_cb;
687 top->mps = GNUNET_malloc (num_masters * sizeof (struct BenchmarkPeer));
688 top->sps = GNUNET_malloc (num_slaves * sizeof (struct BenchmarkPeer));
693 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
694 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
695 (void) GNUNET_TESTBED_test_run (name, cfg_file,
696 num_slaves + num_masters, event_mask, &controller_event_cb, NULL,
701 GNUNET_ATS_TEST_shutdown_topology (void)
703 GNUNET_SCHEDULER_shutdown();
706 /* end of file ats-testing.c */