2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file ats-tests/ats-testing.c
22 * @brief ats testing library: setup topology
24 * @author Christian Grothoff
25 * @author Matthias Wachs
27 #include "ats-testing.h"
31 * Connect peers with testbed
33 struct TestbedConnectOperation
36 * The benchmarking master initiating this connection
38 struct BenchmarkPeer *master;
41 * The benchmarking slave to connect to
43 struct BenchmarkPeer *slave;
46 * Testbed operation to connect peers
48 struct GNUNET_TESTBED_Operation *connect_op;
51 struct GNUNET_CONFIGURATION_Handle *cfg;
53 struct GNUNET_ATS_TEST_Topology *top;
62 do_shutdown (void *cls)
67 struct BenchmarkPeer *p;
69 top->state.benchmarking = GNUNET_NO;
71 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
72 "Benchmarking done\n");
74 GNUNET_ATS_TEST_generate_traffic_stop_all ();
76 for (c_m = 0; c_m < top->num_masters; c_m++)
79 if (NULL != top->mps[c_m].peer_id_op)
81 GNUNET_TESTBED_operation_done (p->peer_id_op);
85 if (NULL != p->ats_task)
86 GNUNET_SCHEDULER_cancel (p->ats_task);
89 for (c_op = 0; c_op < p->num_partners; c_op++)
91 if (NULL != p->partners[c_op].cth)
93 p->partners[c_op].cth = NULL;
95 if ( (NULL != p->core_connect_ops) &&
96 (NULL != p->core_connect_ops[c_op].connect_op) )
98 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
99 "Failed to connect peer 0 and %u\n",
101 GNUNET_TESTBED_operation_done (p->core_connect_ops[c_op].connect_op);
102 p->core_connect_ops[c_op].connect_op = NULL;
106 if (NULL != p->ats_perf_op)
108 GNUNET_TESTBED_operation_done (p->ats_perf_op);
109 p->ats_perf_op = NULL;
112 if (NULL != p->comm_op)
114 GNUNET_TESTBED_operation_done (p->comm_op);
117 GNUNET_free_non_null (p->core_connect_ops);
118 GNUNET_free(p->partners);
122 for (c_s = 0; c_s < top->num_slaves; c_s++)
125 if (NULL != p->peer_id_op)
127 GNUNET_TESTBED_operation_done (p->peer_id_op);
128 p->peer_id_op = NULL;
131 for (c_op = 0; c_op < p->num_partners; c_op++)
133 if (NULL != p->partners[c_op].cth)
135 p->partners[c_op].cth = NULL;
138 if (NULL != p->ats_perf_op)
140 GNUNET_TESTBED_operation_done (p->ats_perf_op);
141 p->ats_perf_op = NULL;
143 if (NULL != p->comm_op)
145 GNUNET_TESTBED_operation_done (p->comm_op);
148 GNUNET_free(p->partners);
151 GNUNET_SCHEDULER_shutdown ();
157 static struct BenchmarkPartner *
158 find_partner (struct BenchmarkPeer *me,
159 const struct GNUNET_PeerIdentity *peer)
163 for (c_m = 0; c_m < me->num_partners; c_m++)
165 /* Find a partner with other as destination */
166 if (0 == memcmp (peer,
167 &me->partners[c_m].dest->id,
168 sizeof (struct GNUNET_PeerIdentity)))
170 return &me->partners[c_m];
178 static struct BenchmarkPeer *
179 find_peer (const struct GNUNET_PeerIdentity * peer)
183 for (c_p = 0; c_p < top->num_masters; c_p++)
185 if (0 == memcmp (&top->mps[c_p].id,
187 sizeof(struct GNUNET_PeerIdentity)))
188 return &top->mps[c_p];
191 for (c_p = 0; c_p < top->num_slaves; c_p++)
193 if (0 == memcmp (&top->sps[c_p].id,
195 sizeof(struct GNUNET_PeerIdentity)))
196 return &top->sps[c_p];
203 * Method called whenever a given peer connects.
206 * @param peer peer identity this notification is about
207 * @param mq queue to use to send messages to @a peer
208 * @return the `struct BenchmarkPartner` of @a peer
211 comm_connect_cb (void *cls,
212 const struct GNUNET_PeerIdentity *peer,
213 struct GNUNET_MQ_Handle *mq)
215 struct BenchmarkPeer *me = cls;
216 struct BenchmarkPeer *remote;
217 struct BenchmarkPartner *p;
222 remote = find_peer (peer);
225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
226 "Unknown peer connected: `%s'\n",
232 id = GNUNET_strdup (GNUNET_i2s (&me->id));
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "%s [%u] `%s' connected to %s [%u] %s\n",
235 (me->master == GNUNET_YES) ? "Master": "Slave",
238 (remote->master == GNUNET_YES) ? "Master": "Slave",
242 me->core_connections++;
243 if ((GNUNET_YES == me->master) &&
244 (GNUNET_NO == remote->master) &&
245 (GNUNET_NO == top->state.connected_CORE))
247 me->core_slave_connections++;
249 if (me->core_slave_connections == top->num_slaves)
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 "Master [%u] connected all slaves\n",
255 completed = GNUNET_YES;
256 for (c = 0; c < top->num_masters; c++)
258 if (top->mps[c].core_slave_connections != top->num_slaves)
259 completed = GNUNET_NO;
261 if (GNUNET_YES == completed)
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 "All master peers connected all slave peers\n");
265 top->state.connected_CORE = GNUNET_YES;
266 /* Notify about setup done */
267 if (NULL != top->done_cb)
268 top->done_cb (top->done_cb_cls,
274 p = find_partner (me,
283 * @param cls this peer
284 * @param peer id of disconnecting peer
285 * @param internal_cls the `struct BenchmarkPartner` of @a peer
288 comm_disconnect_cb (void *cls,
289 const struct GNUNET_PeerIdentity *peer,
292 struct BenchmarkPeer *me = cls;
293 struct BenchmarkPartner *p = internal_cls;
299 id = GNUNET_strdup (GNUNET_i2s (&me->id));
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "%s disconnected from %s\n",
304 GNUNET_assert (me->core_connections > 0);
305 me->core_connections--;
307 if ( (GNUNET_YES == top->state.benchmarking) &&
308 ( (GNUNET_YES == me->master) ||
309 (GNUNET_YES == p->dest->master) ) )
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 "%s disconnected from %s while benchmarking\n",
325 handle_pong (void *cls,
326 const struct TestMessage *message)
328 struct BenchmarkPartner *p = cls;
330 GNUNET_ATS_TEST_traffic_handle_pong (p);
335 handle_ping (void *cls,
336 const struct TestMessage *message)
338 struct BenchmarkPartner *p = cls;
340 GNUNET_ATS_TEST_traffic_handle_ping (p);
345 transport_connect_adapter (void *cls,
346 const struct GNUNET_CONFIGURATION_Handle *cfg)
348 struct BenchmarkPeer *me = cls;
349 struct GNUNET_MQ_MessageHandler handlers[] = {
350 GNUNET_MQ_hd_fixed_size (ping,
351 TEST_MESSAGE_TYPE_PING,
354 GNUNET_MQ_hd_fixed_size (pong,
355 TEST_MESSAGE_TYPE_PONG,
358 GNUNET_MQ_handler_end ()
361 me->th = GNUNET_TRANSPORT_core_connect (cfg,
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
370 "Failed to create transport connection \n");
376 transport_disconnect_adapter (void *cls,
379 struct BenchmarkPeer *me = cls;
381 GNUNET_TRANSPORT_core_disconnect (me->th);
387 core_connect_adapter (void *cls,
388 const struct GNUNET_CONFIGURATION_Handle *cfg)
390 struct BenchmarkPeer *me = cls;
391 struct GNUNET_MQ_MessageHandler handlers[] = {
392 GNUNET_MQ_hd_fixed_size (ping,
393 TEST_MESSAGE_TYPE_PING,
396 GNUNET_MQ_hd_fixed_size (pong,
397 TEST_MESSAGE_TYPE_PONG,
400 GNUNET_MQ_handler_end ()
403 me->ch = GNUNET_CORE_connecT (cfg,
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 "Failed to create core connection \n");
417 core_disconnect_adapter (void *cls,
420 struct BenchmarkPeer *me = cls;
422 GNUNET_CORE_disconnecT (me->ch);
428 connect_completion_callback (void *cls,
429 struct GNUNET_TESTBED_Operation *op,
432 struct TestbedConnectOperation *cop = cls;
437 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
438 _("Connected master [%u] with slave [%u]\n"),
444 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
445 _("Failed to connect master peer [%u] with slave [%u]\n"),
446 cop->master->no, cop->slave->no);
448 GNUNET_SCHEDULER_shutdown ();
450 GNUNET_TESTBED_operation_done (op);
452 for (c = 0; c < top->num_slaves; c++)
454 if (cop == &cop->master->core_connect_ops[c])
455 cop->master->core_connect_ops[c].connect_op = NULL;
457 if (ops == top->num_masters * top->num_slaves)
459 top->state.connected_PEERS = GNUNET_YES;
465 do_connect_peers (void *cls)
469 struct BenchmarkPeer *p;
471 if ((top->state.connected_ATS_service == GNUNET_NO) ||
472 (top->state.connected_COMM_service == GNUNET_NO))
475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
476 "Connecting peers on CORE level\n");
477 for (c_m = 0; c_m < top->num_masters; c_m++)
480 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
481 sizeof (struct TestbedConnectOperation));
483 for (c_s = 0; c_s < top->num_slaves; c_s++)
485 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
486 "Connecting master [%u] with slave [%u]\n",
489 p->core_connect_ops[c_s].master = p;
490 p->core_connect_ops[c_s].slave = &top->sps[c_s];
491 p->core_connect_ops[c_s].connect_op
492 = GNUNET_TESTBED_overlay_connect (NULL,
493 &connect_completion_callback,
494 &p->core_connect_ops[c_s],
497 if (NULL == p->core_connect_ops[c_s].connect_op)
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 "Could not connect master [%u] and slave [%u]\n",
504 GNUNET_SCHEDULER_shutdown ();
513 comm_connect_completion_cb (void *cls,
514 struct GNUNET_TESTBED_Operation *op,
518 static int comm_done = 0;
520 if ((NULL != emsg) || (NULL == ca_result))
522 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
523 "Initialization failed, shutdown\n");
525 GNUNET_SCHEDULER_shutdown ();
530 if (comm_done == top->num_slaves + top->num_masters)
532 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
533 "Connected to all %s services\n",
534 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
535 top->state.connected_COMM_service = GNUNET_YES;
536 GNUNET_SCHEDULER_add_now (&do_connect_peers,
543 do_comm_connect (void *cls)
547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548 "Connecting to all %s services\n",
549 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
550 for (c_m = 0; c_m < top->num_masters; c_m++)
552 if (GNUNET_YES == top->test_core)
553 top->mps[c_m].comm_op
554 = GNUNET_TESTBED_service_connect (NULL,
557 &comm_connect_completion_cb,
559 &core_connect_adapter,
560 &core_disconnect_adapter,
564 top->mps[c_m].comm_op
565 = GNUNET_TESTBED_service_connect (NULL,
568 &comm_connect_completion_cb,
570 &transport_connect_adapter,
571 &transport_disconnect_adapter,
576 for (c_s = 0; c_s < top->num_slaves; c_s++)
578 if (GNUNET_YES == top->test_core)
579 top->sps[c_s].comm_op
580 = GNUNET_TESTBED_service_connect (NULL,
583 &comm_connect_completion_cb,
585 &core_connect_adapter,
586 &core_disconnect_adapter,
590 top->sps[c_s].comm_op
591 = GNUNET_TESTBED_service_connect (NULL,
594 &comm_connect_completion_cb,
596 &transport_connect_adapter,
597 &transport_disconnect_adapter,
605 ats_performance_info_cb (void *cls,
606 const struct GNUNET_HELLO_Address *address,
608 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
609 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
610 const struct GNUNET_ATS_Properties *ats_prop)
612 struct BenchmarkPeer *me = cls;
613 struct BenchmarkPartner *p;
619 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
620 "Peer %u: ATS Service disconnected!\n",
625 p = find_partner (me,
629 /* This is not one of my partners
630 * Will happen since the peers will connect to each other due to gossiping
634 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
637 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
638 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
640 p->bandwidth_in = ntohl (bandwidth_in.value__);
641 p->bandwidth_out = ntohl (bandwidth_out.value__);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "%s [%u] received ATS information: %s\n",
645 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
647 GNUNET_i2s (&p->dest->id));
649 p->props.utilization_out = ats_prop->utilization_out;
650 p->props.utilization_in = ats_prop->utilization_in;
651 p->props.scope = ats_prop->scope;
652 p->props.delay = ats_prop->delay;
653 p->props.distance = ats_prop->distance;
655 if (GNUNET_YES == log)
656 top->ats_perf_cb (cls, address,
661 GNUNET_free(peer_id);
666 ats_perf_connect_adapter (void *cls,
667 const struct GNUNET_CONFIGURATION_Handle *cfg)
669 struct BenchmarkPeer *me = cls;
672 = GNUNET_ATS_performance_init (cfg,
673 &ats_performance_info_cb,
675 if (NULL == me->ats_perf_handle)
676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677 "Failed to create ATS performance handle \n");
678 return me->ats_perf_handle;
683 ats_perf_disconnect_adapter (void *cls,
686 struct BenchmarkPeer *me = cls;
688 GNUNET_ATS_performance_done (me->ats_perf_handle);
689 me->ats_perf_handle = NULL;
694 ats_connect_completion_cb (void *cls,
695 struct GNUNET_TESTBED_Operation *op,
699 static int op_done = 0;
701 if ((NULL != emsg) || (NULL == ca_result))
703 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
704 "Initialization failed, shutdown\n");
706 GNUNET_SCHEDULER_shutdown ();
710 if (op_done == (top->num_masters + top->num_slaves))
712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
713 "Connected to all ATS services\n");
714 top->state.connected_ATS_service = GNUNET_YES;
715 GNUNET_SCHEDULER_add_now (&do_comm_connect,
722 do_connect_ats (void *cls)
727 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
728 "Connecting to all ATS services\n");
729 for (c_m = 0; c_m < top->num_masters; c_m++)
731 top->mps[c_m].ats_perf_op
732 = GNUNET_TESTBED_service_connect (NULL,
735 &ats_connect_completion_cb,
737 &ats_perf_connect_adapter,
738 &ats_perf_disconnect_adapter,
742 for (c_s = 0; c_s < top->num_slaves; c_s++)
744 top->sps[c_s].ats_perf_op
745 = GNUNET_TESTBED_service_connect (NULL,
748 &ats_connect_completion_cb,
750 &ats_perf_connect_adapter,
751 &ats_perf_disconnect_adapter,
759 peerinformation_cb (void *cb_cls,
760 struct GNUNET_TESTBED_Operation *op,
761 const struct GNUNET_TESTBED_PeerInformation *pinfo,
764 struct BenchmarkPeer *p = cb_cls;
767 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
769 p->id = *pinfo->result.id;
770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
771 "%s [%u] has peer id `%s'\n",
772 (p->master == GNUNET_YES) ? "Master" : "Slave",
774 GNUNET_i2s (&p->id));
776 GNUNET_TESTBED_operation_done (op);
777 p->peer_id_op = NULL;
780 if (done == top->num_slaves + top->num_masters)
782 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
783 "Retrieved all peer ID, connect to ATS\n");
784 GNUNET_SCHEDULER_add_now (&do_connect_ats,
791 * Signature of a main function for a testcase.
794 * @param h testbed handle
795 * @param num_peers number of peers in 'peers'
796 * @param peers_ handle to peers run in the testbed
797 * @param links_succeeded the number of overlay link connection attempts that
799 * @param links_failed the number of overlay link connection attempts that
804 struct GNUNET_TESTBED_RunHandle *h,
805 unsigned int num_peers,
806 struct GNUNET_TESTBED_Peer **peers_,
807 unsigned int links_succeeded,
808 unsigned int links_failed)
813 GNUNET_assert (NULL == cls);
814 GNUNET_assert (top->num_masters + top->num_slaves == num_peers);
815 GNUNET_assert (NULL != peers_);
817 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
820 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
821 "Setting up %u masters and %u slaves\n",
825 /* Setup master peers */
826 for (c_m = 0; c_m < top->num_masters; c_m++)
828 GNUNET_assert(NULL != peers_[c_m]);
829 top->mps[c_m].peer = peers_[c_m];
830 top->mps[c_m].no = c_m;
831 top->mps[c_m].master = GNUNET_YES;
832 top->mps[c_m].pref_partner = &top->sps[c_m];
833 top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
834 top->mps[c_m].partners =
835 GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
836 top->mps[c_m].num_partners = top->num_slaves;
837 /* Initialize partners */
838 for (c_s = 0; c_s < top->num_slaves; c_s++)
840 top->mps[c_m].partners[c_s].me = &top->mps[c_m];
841 top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
843 /* Get configuration */
844 top->mps[c_m].peer_id_op
845 = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
846 GNUNET_TESTBED_PIT_IDENTITY,
851 /* Setup slave peers */
852 for (c_s = 0; c_s < top->num_slaves; c_s++)
854 GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
855 top->sps[c_s].peer = peers_[c_s + top->num_masters];
856 top->sps[c_s].no = c_s + top->num_masters;
857 top->sps[c_s].master = GNUNET_NO;
858 top->sps[c_s].partners =
859 GNUNET_new_array (top->num_masters,
860 struct BenchmarkPartner);
861 top->sps[c_s].num_partners = top->num_masters;
862 /* Initialize partners */
863 for (c_m = 0; c_m < top->num_masters; c_m++)
865 top->sps[c_s].partners[c_m].me = &top->sps[c_s];
866 top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
868 /* Initialize properties */
869 top->sps[c_s].partners[c_m].props.delay = GNUNET_TIME_UNIT_ZERO;
870 top->sps[c_s].partners[c_m].props.distance = 0;
871 top->sps[c_s].partners[c_m].props.scope = GNUNET_ATS_NET_UNSPECIFIED;
872 top->sps[c_s].partners[c_m].props.utilization_in = 0;
873 top->sps[c_s].partners[c_m].props.utilization_out = 0;
875 /* Get configuration */
876 top->sps[c_s].peer_id_op
877 = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
878 GNUNET_TESTBED_PIT_IDENTITY,
886 * Controller event callback
889 * @param event the controller event
892 controller_event_cb (void *cls,
893 const struct GNUNET_TESTBED_EventInformation *event)
897 case GNUNET_TESTBED_ET_CONNECT:
899 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
903 GNUNET_SCHEDULER_shutdown ();
908 struct BenchmarkPeer *
909 GNUNET_ATS_TEST_get_peer (int src)
911 if (src > top->num_masters)
913 return &top->mps[src];
917 struct BenchmarkPartner *
918 GNUNET_ATS_TEST_get_partner (int src,
921 if (src > top->num_masters)
923 if (dest > top->num_slaves)
925 return &top->mps[src].partners[dest];
930 * Create a topology for ats testing
932 * @param name test name
933 * @param cfg_file configuration file to use for the peers
934 * @param num_slaves number of slaves
935 * @param num_masters number of masters
936 * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
937 * @param done_cb function to call when topology is setup
938 * @param done_cb_cls cls for callback
939 * @param log_request_cb callback to call when logging is required
942 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
943 unsigned int num_slaves,
944 unsigned int num_masters,
946 GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
948 GNUNET_ATS_AddressInformationCallback log_request_cb)
950 top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
951 top->num_masters = num_masters;
952 top->num_slaves = num_slaves;
953 top->done_cb = done_cb;
954 top->done_cb_cls = done_cb_cls;
955 top->test_core = test_core;
956 top->ats_perf_cb = log_request_cb;
957 top->mps = GNUNET_new_array (num_masters,
958 struct BenchmarkPeer);
959 top->sps = GNUNET_new_array (num_slaves,
960 struct BenchmarkPeer);
965 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
966 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
967 (void) GNUNET_TESTBED_test_run (name,
969 num_slaves + num_masters,
971 &controller_event_cb, NULL,
980 GNUNET_ATS_TEST_shutdown_topology (void)
984 GNUNET_SCHEDULER_shutdown();
988 /* end of file ats-testing.c */