2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016, 2017 GNUnet e.V.
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.
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.
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/>.
19 * @file ats-tests/ats-testing.c
20 * @brief ats testing library: setup topology
22 * @author Christian Grothoff
23 * @author Matthias Wachs
25 #include "ats-testing.h"
29 * Connect peers with testbed
31 struct TestbedConnectOperation
34 * The benchmarking master initiating this connection
36 struct BenchmarkPeer *master;
39 * The benchmarking slave to connect to
41 struct BenchmarkPeer *slave;
44 * Testbed operation to connect peers
46 struct GNUNET_TESTBED_Operation *connect_op;
49 struct GNUNET_CONFIGURATION_Handle *cfg;
51 struct GNUNET_ATS_TEST_Topology *top;
60 do_shutdown (void *cls)
65 struct BenchmarkPeer *p;
67 top->state.benchmarking = GNUNET_NO;
69 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
70 "Benchmarking done\n");
72 GNUNET_ATS_TEST_generate_traffic_stop_all ();
74 for (c_m = 0; c_m < top->num_masters; c_m++)
77 if (NULL != top->mps[c_m].peer_id_op)
79 GNUNET_TESTBED_operation_done (p->peer_id_op);
83 if (NULL != p->ats_task)
84 GNUNET_SCHEDULER_cancel (p->ats_task);
87 for (c_op = 0; c_op < p->num_partners; c_op++)
89 if ( (NULL != p->core_connect_ops) &&
90 (NULL != p->core_connect_ops[c_op].connect_op) )
92 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
93 "Failed to connect peer 0 and %u\n",
95 GNUNET_TESTBED_operation_done (p->core_connect_ops[c_op].connect_op);
96 p->core_connect_ops[c_op].connect_op = NULL;
100 if (NULL != p->ats_perf_op)
102 GNUNET_TESTBED_operation_done (p->ats_perf_op);
103 p->ats_perf_op = NULL;
106 if (NULL != p->comm_op)
108 GNUNET_TESTBED_operation_done (p->comm_op);
111 GNUNET_free_non_null (p->core_connect_ops);
112 GNUNET_free(p->partners);
116 for (c_s = 0; c_s < top->num_slaves; c_s++)
119 if (NULL != p->peer_id_op)
121 GNUNET_TESTBED_operation_done (p->peer_id_op);
122 p->peer_id_op = NULL;
124 if (NULL != p->ats_perf_op)
126 GNUNET_TESTBED_operation_done (p->ats_perf_op);
127 p->ats_perf_op = NULL;
129 if (NULL != p->comm_op)
131 GNUNET_TESTBED_operation_done (p->comm_op);
134 GNUNET_free(p->partners);
137 GNUNET_SCHEDULER_shutdown ();
143 static struct BenchmarkPartner *
144 find_partner (struct BenchmarkPeer *me,
145 const struct GNUNET_PeerIdentity *peer)
149 for (c_m = 0; c_m < me->num_partners; c_m++)
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)))
156 return &me->partners[c_m];
164 static struct BenchmarkPeer *
165 find_peer (const struct GNUNET_PeerIdentity * peer)
169 for (c_p = 0; c_p < top->num_masters; c_p++)
171 if (0 == memcmp (&top->mps[c_p].id,
173 sizeof(struct GNUNET_PeerIdentity)))
174 return &top->mps[c_p];
177 for (c_p = 0; c_p < top->num_slaves; c_p++)
179 if (0 == memcmp (&top->sps[c_p].id,
181 sizeof(struct GNUNET_PeerIdentity)))
182 return &top->sps[c_p];
189 * Method called whenever a given peer connects.
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
197 comm_connect_cb (void *cls,
198 const struct GNUNET_PeerIdentity *peer,
199 struct GNUNET_MQ_Handle *mq)
201 struct BenchmarkPeer *me = cls;
202 struct BenchmarkPeer *remote;
203 struct BenchmarkPartner *p;
208 remote = find_peer (peer);
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212 "Unknown peer connected: `%s'\n",
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",
224 (remote->master == GNUNET_YES) ? "Master": "Slave",
228 me->core_connections++;
229 if ((GNUNET_YES == me->master) &&
230 (GNUNET_NO == remote->master) &&
231 (GNUNET_NO == top->state.connected_CORE))
233 me->core_slave_connections++;
235 if (me->core_slave_connections == top->num_slaves)
237 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
238 "Master [%u] connected all slaves\n",
241 completed = GNUNET_YES;
242 for (c = 0; c < top->num_masters; c++)
244 if (top->mps[c].core_slave_connections != top->num_slaves)
245 completed = GNUNET_NO;
247 if (GNUNET_YES == completed)
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,
260 p = find_partner (me,
269 * @param cls this peer
270 * @param peer id of disconnecting peer
271 * @param internal_cls the `struct BenchmarkPartner` of @a peer
274 comm_disconnect_cb (void *cls,
275 const struct GNUNET_PeerIdentity *peer,
278 struct BenchmarkPeer *me = cls;
279 struct BenchmarkPartner *p = internal_cls;
285 id = GNUNET_strdup (GNUNET_i2s (&me->id));
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "%s disconnected from %s\n",
290 GNUNET_assert (me->core_connections > 0);
291 me->core_connections--;
293 if ( (GNUNET_YES == top->state.benchmarking) &&
294 ( (GNUNET_YES == me->master) ||
295 (GNUNET_YES == p->dest->master) ) )
297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
298 "%s disconnected from %s while benchmarking\n",
307 handle_pong (void *cls,
308 const struct TestMessage *message)
310 struct BenchmarkPartner *p = cls;
312 GNUNET_ATS_TEST_traffic_handle_pong (p);
317 handle_ping (void *cls,
318 const struct TestMessage *message)
320 struct BenchmarkPartner *p = cls;
322 GNUNET_ATS_TEST_traffic_handle_ping (p);
327 transport_connect_adapter (void *cls,
328 const struct GNUNET_CONFIGURATION_Handle *cfg)
330 struct BenchmarkPeer *me = cls;
331 struct GNUNET_MQ_MessageHandler handlers[] = {
332 GNUNET_MQ_hd_fixed_size (ping,
333 TEST_MESSAGE_TYPE_PING,
336 GNUNET_MQ_hd_fixed_size (pong,
337 TEST_MESSAGE_TYPE_PONG,
340 GNUNET_MQ_handler_end ()
343 me->th = GNUNET_TRANSPORT_core_connect (cfg,
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 "Failed to create transport connection \n");
358 transport_disconnect_adapter (void *cls,
361 struct BenchmarkPeer *me = cls;
363 GNUNET_TRANSPORT_core_disconnect (me->th);
369 core_connect_adapter (void *cls,
370 const struct GNUNET_CONFIGURATION_Handle *cfg)
372 struct BenchmarkPeer *me = cls;
373 struct GNUNET_MQ_MessageHandler handlers[] = {
374 GNUNET_MQ_hd_fixed_size (ping,
375 TEST_MESSAGE_TYPE_PING,
378 GNUNET_MQ_hd_fixed_size (pong,
379 TEST_MESSAGE_TYPE_PONG,
382 GNUNET_MQ_handler_end ()
385 me->ch = GNUNET_CORE_connect (cfg,
392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
393 "Failed to create core connection \n");
399 core_disconnect_adapter (void *cls,
402 struct BenchmarkPeer *me = cls;
404 GNUNET_CORE_disconnect (me->ch);
410 connect_completion_callback (void *cls,
411 struct GNUNET_TESTBED_Operation *op,
414 struct TestbedConnectOperation *cop = cls;
419 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
420 _("Connected master [%u] with slave [%u]\n"),
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);
430 GNUNET_SCHEDULER_shutdown ();
432 GNUNET_TESTBED_operation_done (op);
434 for (c = 0; c < top->num_slaves; c++)
436 if (cop == &cop->master->core_connect_ops[c])
437 cop->master->core_connect_ops[c].connect_op = NULL;
439 if (ops == top->num_masters * top->num_slaves)
441 top->state.connected_PEERS = GNUNET_YES;
447 do_connect_peers (void *cls)
451 struct BenchmarkPeer *p;
453 if ((top->state.connected_ATS_service == GNUNET_NO) ||
454 (top->state.connected_COMM_service == GNUNET_NO))
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++)
462 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
463 sizeof (struct TestbedConnectOperation));
465 for (c_s = 0; c_s < top->num_slaves; c_s++)
467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468 "Connecting master [%u] with slave [%u]\n",
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],
479 if (NULL == p->core_connect_ops[c_s].connect_op)
481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
482 "Could not connect master [%u] and slave [%u]\n",
486 GNUNET_SCHEDULER_shutdown ();
495 comm_connect_completion_cb (void *cls,
496 struct GNUNET_TESTBED_Operation *op,
500 static int comm_done = 0;
502 if ((NULL != emsg) || (NULL == ca_result))
504 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
505 "Initialization failed, shutdown\n");
507 GNUNET_SCHEDULER_shutdown ();
512 if (comm_done == top->num_slaves + top->num_masters)
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,
525 do_comm_connect (void *cls)
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++)
534 if (GNUNET_YES == top->test_core)
535 top->mps[c_m].comm_op
536 = GNUNET_TESTBED_service_connect (NULL,
539 &comm_connect_completion_cb,
541 &core_connect_adapter,
542 &core_disconnect_adapter,
546 top->mps[c_m].comm_op
547 = GNUNET_TESTBED_service_connect (NULL,
550 &comm_connect_completion_cb,
552 &transport_connect_adapter,
553 &transport_disconnect_adapter,
558 for (c_s = 0; c_s < top->num_slaves; c_s++)
560 if (GNUNET_YES == top->test_core)
561 top->sps[c_s].comm_op
562 = GNUNET_TESTBED_service_connect (NULL,
565 &comm_connect_completion_cb,
567 &core_connect_adapter,
568 &core_disconnect_adapter,
572 top->sps[c_s].comm_op
573 = GNUNET_TESTBED_service_connect (NULL,
576 &comm_connect_completion_cb,
578 &transport_connect_adapter,
579 &transport_disconnect_adapter,
587 ats_performance_info_cb (void *cls,
588 const struct GNUNET_HELLO_Address *address,
590 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
591 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
592 const struct GNUNET_ATS_Properties *ats_prop)
594 struct BenchmarkPeer *me = cls;
595 struct BenchmarkPartner *p;
601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
602 "Peer %u: ATS Service disconnected!\n",
607 p = find_partner (me,
611 /* This is not one of my partners
612 * Will happen since the peers will connect to each other due to gossiping
616 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
619 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
620 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
622 p->bandwidth_in = ntohl (bandwidth_in.value__);
623 p->bandwidth_out = ntohl (bandwidth_out.value__);
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "%s [%u] received ATS information: %s\n",
627 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
629 GNUNET_i2s (&p->dest->id));
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;
637 if (GNUNET_YES == log)
638 top->ats_perf_cb (cls, address,
643 GNUNET_free(peer_id);
648 ats_perf_connect_adapter (void *cls,
649 const struct GNUNET_CONFIGURATION_Handle *cfg)
651 struct BenchmarkPeer *me = cls;
654 = GNUNET_ATS_performance_init (cfg,
655 &ats_performance_info_cb,
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;
665 ats_perf_disconnect_adapter (void *cls,
668 struct BenchmarkPeer *me = cls;
670 GNUNET_ATS_performance_done (me->ats_perf_handle);
671 me->ats_perf_handle = NULL;
676 ats_connect_completion_cb (void *cls,
677 struct GNUNET_TESTBED_Operation *op,
681 static int op_done = 0;
683 if ((NULL != emsg) || (NULL == ca_result))
685 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
686 "Initialization failed, shutdown\n");
688 GNUNET_SCHEDULER_shutdown ();
692 if (op_done == (top->num_masters + top->num_slaves))
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,
704 do_connect_ats (void *cls)
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++)
713 top->mps[c_m].ats_perf_op
714 = GNUNET_TESTBED_service_connect (NULL,
717 &ats_connect_completion_cb,
719 &ats_perf_connect_adapter,
720 &ats_perf_disconnect_adapter,
724 for (c_s = 0; c_s < top->num_slaves; c_s++)
726 top->sps[c_s].ats_perf_op
727 = GNUNET_TESTBED_service_connect (NULL,
730 &ats_connect_completion_cb,
732 &ats_perf_connect_adapter,
733 &ats_perf_disconnect_adapter,
741 peerinformation_cb (void *cb_cls,
742 struct GNUNET_TESTBED_Operation *op,
743 const struct GNUNET_TESTBED_PeerInformation *pinfo,
746 struct BenchmarkPeer *p = cb_cls;
749 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
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",
756 GNUNET_i2s (&p->id));
758 GNUNET_TESTBED_operation_done (op);
759 p->peer_id_op = NULL;
762 if (done == top->num_slaves + top->num_masters)
764 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
765 "Retrieved all peer ID, connect to ATS\n");
766 GNUNET_SCHEDULER_add_now (&do_connect_ats,
773 * Signature of a main function for a testcase.
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
781 * @param links_failed the number of overlay link connection attempts that
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)
795 GNUNET_assert (NULL == cls);
796 GNUNET_assert (top->num_masters + top->num_slaves == num_peers);
797 GNUNET_assert (NULL != peers_);
799 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
803 "Setting up %u masters and %u slaves\n",
807 /* Setup master peers */
808 for (c_m = 0; c_m < top->num_masters; c_m++)
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++)
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];
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,
833 /* Setup slave peers */
834 for (c_s = 0; c_s < top->num_slaves; c_s++)
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++)
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];
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;
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,
868 * Controller event callback
871 * @param event the controller event
874 controller_event_cb (void *cls,
875 const struct GNUNET_TESTBED_EventInformation *event)
879 case GNUNET_TESTBED_ET_CONNECT:
881 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
885 GNUNET_SCHEDULER_shutdown ();
890 struct BenchmarkPeer *
891 GNUNET_ATS_TEST_get_peer (int src)
893 if (src > top->num_masters)
895 return &top->mps[src];
899 struct BenchmarkPartner *
900 GNUNET_ATS_TEST_get_partner (int src,
903 if (src > top->num_masters)
905 if (dest > top->num_slaves)
907 return &top->mps[src].partners[dest];
912 * Create a topology for ats testing
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
924 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
925 unsigned int num_slaves,
926 unsigned int num_masters,
928 GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
930 GNUNET_ATS_AddressInformationCallback log_request_cb)
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);
947 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
948 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
949 (void) GNUNET_TESTBED_test_run (name,
951 num_slaves + num_masters,
953 &controller_event_cb, NULL,
962 GNUNET_ATS_TEST_shutdown_topology (void)
966 GNUNET_SCHEDULER_shutdown();
970 /* end of file ats-testing.c */