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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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->core_connect_ops) &&
92 (NULL != p->core_connect_ops[c_op].connect_op) )
94 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
95 "Failed to connect peer 0 and %u\n",
97 GNUNET_TESTBED_operation_done (p->core_connect_ops[c_op].connect_op);
98 p->core_connect_ops[c_op].connect_op = NULL;
102 if (NULL != p->ats_perf_op)
104 GNUNET_TESTBED_operation_done (p->ats_perf_op);
105 p->ats_perf_op = NULL;
108 if (NULL != p->comm_op)
110 GNUNET_TESTBED_operation_done (p->comm_op);
113 GNUNET_free_non_null (p->core_connect_ops);
114 GNUNET_free(p->partners);
118 for (c_s = 0; c_s < top->num_slaves; c_s++)
121 if (NULL != p->peer_id_op)
123 GNUNET_TESTBED_operation_done (p->peer_id_op);
124 p->peer_id_op = NULL;
126 if (NULL != p->ats_perf_op)
128 GNUNET_TESTBED_operation_done (p->ats_perf_op);
129 p->ats_perf_op = NULL;
131 if (NULL != p->comm_op)
133 GNUNET_TESTBED_operation_done (p->comm_op);
136 GNUNET_free(p->partners);
139 GNUNET_SCHEDULER_shutdown ();
145 static struct BenchmarkPartner *
146 find_partner (struct BenchmarkPeer *me,
147 const struct GNUNET_PeerIdentity *peer)
151 for (c_m = 0; c_m < me->num_partners; c_m++)
153 /* Find a partner with other as destination */
154 if (0 == GNUNET_memcmp (peer,
155 &me->partners[c_m].dest->id))
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 == GNUNET_memcmp (&top->mps[c_p].id,
174 return &top->mps[c_p];
177 for (c_p = 0; c_p < top->num_slaves; c_p++)
179 if (0 == GNUNET_memcmp (&top->sps[c_p].id,
181 return &top->sps[c_p];
188 * Method called whenever a given peer connects.
191 * @param peer peer identity this notification is about
192 * @param mq queue to use to send messages to @a peer
193 * @return the `struct BenchmarkPartner` of @a peer
196 comm_connect_cb (void *cls,
197 const struct GNUNET_PeerIdentity *peer,
198 struct GNUNET_MQ_Handle *mq)
200 struct BenchmarkPeer *me = cls;
201 struct BenchmarkPeer *remote;
202 struct BenchmarkPartner *p;
207 remote = find_peer (peer);
210 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
211 "Unknown peer connected: `%s'\n",
217 id = GNUNET_strdup (GNUNET_i2s (&me->id));
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "%s [%u] `%s' connected to %s [%u] %s\n",
220 (me->master == GNUNET_YES) ? "Master": "Slave",
223 (remote->master == GNUNET_YES) ? "Master": "Slave",
227 me->core_connections++;
228 if ((GNUNET_YES == me->master) &&
229 (GNUNET_NO == remote->master) &&
230 (GNUNET_NO == top->state.connected_CORE))
232 me->core_slave_connections++;
234 if (me->core_slave_connections == top->num_slaves)
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "Master [%u] connected all slaves\n",
240 completed = GNUNET_YES;
241 for (c = 0; c < top->num_masters; c++)
243 if (top->mps[c].core_slave_connections != top->num_slaves)
244 completed = GNUNET_NO;
246 if (GNUNET_YES == completed)
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 "All master peers connected all slave peers\n");
250 top->state.connected_CORE = GNUNET_YES;
251 /* Notify about setup done */
252 if (NULL != top->done_cb)
253 top->done_cb (top->done_cb_cls,
259 p = find_partner (me,
268 * @param cls this peer
269 * @param peer id of disconnecting peer
270 * @param internal_cls the `struct BenchmarkPartner` of @a peer
273 comm_disconnect_cb (void *cls,
274 const struct GNUNET_PeerIdentity *peer,
277 struct BenchmarkPeer *me = cls;
278 struct BenchmarkPartner *p = internal_cls;
284 id = GNUNET_strdup (GNUNET_i2s (&me->id));
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "%s disconnected from %s\n",
289 GNUNET_assert (me->core_connections > 0);
290 me->core_connections--;
292 if ( (GNUNET_YES == top->state.benchmarking) &&
293 ( (GNUNET_YES == me->master) ||
294 (GNUNET_YES == p->dest->master) ) )
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "%s disconnected from %s while benchmarking\n",
306 handle_pong (void *cls,
307 const struct TestMessage *message)
309 struct BenchmarkPartner *p = cls;
311 GNUNET_ATS_TEST_traffic_handle_pong (p);
316 handle_ping (void *cls,
317 const struct TestMessage *message)
319 struct BenchmarkPartner *p = cls;
321 GNUNET_ATS_TEST_traffic_handle_ping (p);
326 transport_connect_adapter (void *cls,
327 const struct GNUNET_CONFIGURATION_Handle *cfg)
329 struct BenchmarkPeer *me = cls;
330 struct GNUNET_MQ_MessageHandler handlers[] = {
331 GNUNET_MQ_hd_fixed_size (ping,
332 TEST_MESSAGE_TYPE_PING,
335 GNUNET_MQ_hd_fixed_size (pong,
336 TEST_MESSAGE_TYPE_PONG,
339 GNUNET_MQ_handler_end ()
342 me->th = GNUNET_TRANSPORT_core_connect (cfg,
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
351 "Failed to create transport connection \n");
357 transport_disconnect_adapter (void *cls,
360 struct BenchmarkPeer *me = cls;
362 GNUNET_TRANSPORT_core_disconnect (me->th);
368 core_connect_adapter (void *cls,
369 const struct GNUNET_CONFIGURATION_Handle *cfg)
371 struct BenchmarkPeer *me = cls;
372 struct GNUNET_MQ_MessageHandler handlers[] = {
373 GNUNET_MQ_hd_fixed_size (ping,
374 TEST_MESSAGE_TYPE_PING,
377 GNUNET_MQ_hd_fixed_size (pong,
378 TEST_MESSAGE_TYPE_PONG,
381 GNUNET_MQ_handler_end ()
384 me->ch = GNUNET_CORE_connect (cfg,
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 "Failed to create core connection \n");
398 core_disconnect_adapter (void *cls,
401 struct BenchmarkPeer *me = cls;
403 GNUNET_CORE_disconnect (me->ch);
409 connect_completion_callback (void *cls,
410 struct GNUNET_TESTBED_Operation *op,
413 struct TestbedConnectOperation *cop = cls;
418 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
419 _("Connected master [%u] with slave [%u]\n"),
425 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
426 _("Failed to connect master peer [%u] with slave [%u]\n"),
427 cop->master->no, cop->slave->no);
429 GNUNET_SCHEDULER_shutdown ();
431 GNUNET_TESTBED_operation_done (op);
433 for (c = 0; c < top->num_slaves; c++)
435 if (cop == &cop->master->core_connect_ops[c])
436 cop->master->core_connect_ops[c].connect_op = NULL;
438 if (ops == top->num_masters * top->num_slaves)
440 top->state.connected_PEERS = GNUNET_YES;
446 do_connect_peers (void *cls)
450 struct BenchmarkPeer *p;
452 if ((top->state.connected_ATS_service == GNUNET_NO) ||
453 (top->state.connected_COMM_service == GNUNET_NO))
456 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
457 "Connecting peers on CORE level\n");
458 for (c_m = 0; c_m < top->num_masters; c_m++)
461 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
462 sizeof (struct TestbedConnectOperation));
464 for (c_s = 0; c_s < top->num_slaves; c_s++)
466 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
467 "Connecting master [%u] with slave [%u]\n",
470 p->core_connect_ops[c_s].master = p;
471 p->core_connect_ops[c_s].slave = &top->sps[c_s];
472 p->core_connect_ops[c_s].connect_op
473 = GNUNET_TESTBED_overlay_connect (NULL,
474 &connect_completion_callback,
475 &p->core_connect_ops[c_s],
478 if (NULL == p->core_connect_ops[c_s].connect_op)
480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481 "Could not connect master [%u] and slave [%u]\n",
485 GNUNET_SCHEDULER_shutdown ();
494 comm_connect_completion_cb (void *cls,
495 struct GNUNET_TESTBED_Operation *op,
499 static int comm_done = 0;
501 if ((NULL != emsg) || (NULL == ca_result))
503 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
504 "Initialization failed, shutdown\n");
506 GNUNET_SCHEDULER_shutdown ();
511 if (comm_done == top->num_slaves + top->num_masters)
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
514 "Connected to all %s services\n",
515 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
516 top->state.connected_COMM_service = GNUNET_YES;
517 GNUNET_SCHEDULER_add_now (&do_connect_peers,
524 do_comm_connect (void *cls)
528 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
529 "Connecting to all %s services\n",
530 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
531 for (c_m = 0; c_m < top->num_masters; c_m++)
533 if (GNUNET_YES == top->test_core)
534 top->mps[c_m].comm_op
535 = GNUNET_TESTBED_service_connect (NULL,
538 &comm_connect_completion_cb,
540 &core_connect_adapter,
541 &core_disconnect_adapter,
545 top->mps[c_m].comm_op
546 = GNUNET_TESTBED_service_connect (NULL,
549 &comm_connect_completion_cb,
551 &transport_connect_adapter,
552 &transport_disconnect_adapter,
557 for (c_s = 0; c_s < top->num_slaves; c_s++)
559 if (GNUNET_YES == top->test_core)
560 top->sps[c_s].comm_op
561 = GNUNET_TESTBED_service_connect (NULL,
564 &comm_connect_completion_cb,
566 &core_connect_adapter,
567 &core_disconnect_adapter,
571 top->sps[c_s].comm_op
572 = GNUNET_TESTBED_service_connect (NULL,
575 &comm_connect_completion_cb,
577 &transport_connect_adapter,
578 &transport_disconnect_adapter,
586 ats_performance_info_cb (void *cls,
587 const struct GNUNET_HELLO_Address *address,
589 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
590 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
591 const struct GNUNET_ATS_Properties *ats_prop)
593 struct BenchmarkPeer *me = cls;
594 struct BenchmarkPartner *p;
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 "Peer %u: ATS Service disconnected!\n",
606 p = find_partner (me,
610 /* This is not one of my partners
611 * Will happen since the peers will connect to each other due to gossiping
615 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
618 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
619 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
621 p->bandwidth_in = ntohl (bandwidth_in.value__);
622 p->bandwidth_out = ntohl (bandwidth_out.value__);
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "%s [%u] received ATS information: %s\n",
626 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
628 GNUNET_i2s (&p->dest->id));
630 p->props.utilization_out = ats_prop->utilization_out;
631 p->props.utilization_in = ats_prop->utilization_in;
632 p->props.scope = ats_prop->scope;
633 p->props.delay = ats_prop->delay;
634 p->props.distance = ats_prop->distance;
636 if (GNUNET_YES == log)
637 top->ats_perf_cb (cls, address,
642 GNUNET_free(peer_id);
647 ats_perf_connect_adapter (void *cls,
648 const struct GNUNET_CONFIGURATION_Handle *cfg)
650 struct BenchmarkPeer *me = cls;
653 = GNUNET_ATS_performance_init (cfg,
654 &ats_performance_info_cb,
656 if (NULL == me->ats_perf_handle)
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 "Failed to create ATS performance handle \n");
659 return me->ats_perf_handle;
664 ats_perf_disconnect_adapter (void *cls,
667 struct BenchmarkPeer *me = cls;
669 GNUNET_ATS_performance_done (me->ats_perf_handle);
670 me->ats_perf_handle = NULL;
675 ats_connect_completion_cb (void *cls,
676 struct GNUNET_TESTBED_Operation *op,
680 static int op_done = 0;
682 if ((NULL != emsg) || (NULL == ca_result))
684 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
685 "Initialization failed, shutdown\n");
687 GNUNET_SCHEDULER_shutdown ();
691 if (op_done == (top->num_masters + top->num_slaves))
693 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
694 "Connected to all ATS services\n");
695 top->state.connected_ATS_service = GNUNET_YES;
696 GNUNET_SCHEDULER_add_now (&do_comm_connect,
703 do_connect_ats (void *cls)
708 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
709 "Connecting to all ATS services\n");
710 for (c_m = 0; c_m < top->num_masters; c_m++)
712 top->mps[c_m].ats_perf_op
713 = GNUNET_TESTBED_service_connect (NULL,
716 &ats_connect_completion_cb,
718 &ats_perf_connect_adapter,
719 &ats_perf_disconnect_adapter,
723 for (c_s = 0; c_s < top->num_slaves; c_s++)
725 top->sps[c_s].ats_perf_op
726 = GNUNET_TESTBED_service_connect (NULL,
729 &ats_connect_completion_cb,
731 &ats_perf_connect_adapter,
732 &ats_perf_disconnect_adapter,
740 peerinformation_cb (void *cb_cls,
741 struct GNUNET_TESTBED_Operation *op,
742 const struct GNUNET_TESTBED_PeerInformation *pinfo,
745 struct BenchmarkPeer *p = cb_cls;
748 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
750 p->id = *pinfo->result.id;
751 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
752 "%s [%u] has peer id `%s'\n",
753 (p->master == GNUNET_YES) ? "Master" : "Slave",
755 GNUNET_i2s (&p->id));
757 GNUNET_TESTBED_operation_done (op);
758 p->peer_id_op = NULL;
761 if (done == top->num_slaves + top->num_masters)
763 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
764 "Retrieved all peer ID, connect to ATS\n");
765 GNUNET_SCHEDULER_add_now (&do_connect_ats,
772 * Signature of a main function for a testcase.
775 * @param h testbed handle
776 * @param num_peers number of peers in 'peers'
777 * @param peers_ handle to peers run in the testbed
778 * @param links_succeeded the number of overlay link connection attempts that
780 * @param links_failed the number of overlay link connection attempts that
785 struct GNUNET_TESTBED_RunHandle *h,
786 unsigned int num_peers,
787 struct GNUNET_TESTBED_Peer **peers_,
788 unsigned int links_succeeded,
789 unsigned int links_failed)
794 GNUNET_assert (NULL == cls);
795 GNUNET_assert (top->num_masters + top->num_slaves == num_peers);
796 GNUNET_assert (NULL != peers_);
798 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
801 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
802 "Setting up %u masters and %u slaves\n",
806 /* Setup master peers */
807 for (c_m = 0; c_m < top->num_masters; c_m++)
809 GNUNET_assert(NULL != peers_[c_m]);
810 top->mps[c_m].peer = peers_[c_m];
811 top->mps[c_m].no = c_m;
812 top->mps[c_m].master = GNUNET_YES;
813 top->mps[c_m].pref_partner = &top->sps[c_m];
814 top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
815 top->mps[c_m].partners =
816 GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
817 top->mps[c_m].num_partners = top->num_slaves;
818 /* Initialize partners */
819 for (c_s = 0; c_s < top->num_slaves; c_s++)
821 top->mps[c_m].partners[c_s].me = &top->mps[c_m];
822 top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
824 /* Get configuration */
825 top->mps[c_m].peer_id_op
826 = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
827 GNUNET_TESTBED_PIT_IDENTITY,
832 /* Setup slave peers */
833 for (c_s = 0; c_s < top->num_slaves; c_s++)
835 GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
836 top->sps[c_s].peer = peers_[c_s + top->num_masters];
837 top->sps[c_s].no = c_s + top->num_masters;
838 top->sps[c_s].master = GNUNET_NO;
839 top->sps[c_s].partners =
840 GNUNET_new_array (top->num_masters,
841 struct BenchmarkPartner);
842 top->sps[c_s].num_partners = top->num_masters;
843 /* Initialize partners */
844 for (c_m = 0; c_m < top->num_masters; c_m++)
846 top->sps[c_s].partners[c_m].me = &top->sps[c_s];
847 top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
849 /* Initialize properties */
850 top->sps[c_s].partners[c_m].props.delay = GNUNET_TIME_UNIT_ZERO;
851 top->sps[c_s].partners[c_m].props.distance = 0;
852 top->sps[c_s].partners[c_m].props.scope = GNUNET_NT_UNSPECIFIED;
853 top->sps[c_s].partners[c_m].props.utilization_in = 0;
854 top->sps[c_s].partners[c_m].props.utilization_out = 0;
856 /* Get configuration */
857 top->sps[c_s].peer_id_op
858 = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
859 GNUNET_TESTBED_PIT_IDENTITY,
867 * Controller event callback
870 * @param event the controller event
873 controller_event_cb (void *cls,
874 const struct GNUNET_TESTBED_EventInformation *event)
878 case GNUNET_TESTBED_ET_CONNECT:
880 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
884 GNUNET_SCHEDULER_shutdown ();
889 struct BenchmarkPeer *
890 GNUNET_ATS_TEST_get_peer (int src)
892 if (src > top->num_masters)
894 return &top->mps[src];
898 struct BenchmarkPartner *
899 GNUNET_ATS_TEST_get_partner (int src,
902 if (src > top->num_masters)
904 if (dest > top->num_slaves)
906 return &top->mps[src].partners[dest];
911 * Create a topology for ats testing
913 * @param name test name
914 * @param cfg_file configuration file to use for the peers
915 * @param num_slaves number of slaves
916 * @param num_masters number of masters
917 * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
918 * @param done_cb function to call when topology is setup
919 * @param done_cb_cls cls for callback
920 * @param log_request_cb callback to call when logging is required
923 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
924 unsigned int num_slaves,
925 unsigned int num_masters,
927 GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
929 GNUNET_ATS_AddressInformationCallback log_request_cb)
931 top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
932 top->num_masters = num_masters;
933 top->num_slaves = num_slaves;
934 top->done_cb = done_cb;
935 top->done_cb_cls = done_cb_cls;
936 top->test_core = test_core;
937 top->ats_perf_cb = log_request_cb;
938 top->mps = GNUNET_new_array (num_masters,
939 struct BenchmarkPeer);
940 top->sps = GNUNET_new_array (num_slaves,
941 struct BenchmarkPeer);
946 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
947 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
948 (void) GNUNET_TESTBED_test_run (name,
950 num_slaves + num_masters,
952 &controller_event_cb, NULL,
961 GNUNET_ATS_TEST_shutdown_topology (void)
965 GNUNET_SCHEDULER_shutdown();
969 /* end of file ats-testing.c */