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
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->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 == memcmp (peer,
155 &me->partners[c_m].dest->id,
156 sizeof (struct GNUNET_PeerIdentity)))
158 return &me->partners[c_m];
166 static struct BenchmarkPeer *
167 find_peer (const struct GNUNET_PeerIdentity * peer)
171 for (c_p = 0; c_p < top->num_masters; c_p++)
173 if (0 == memcmp (&top->mps[c_p].id,
175 sizeof(struct GNUNET_PeerIdentity)))
176 return &top->mps[c_p];
179 for (c_p = 0; c_p < top->num_slaves; c_p++)
181 if (0 == memcmp (&top->sps[c_p].id,
183 sizeof(struct GNUNET_PeerIdentity)))
184 return &top->sps[c_p];
191 * Method called whenever a given peer connects.
194 * @param peer peer identity this notification is about
195 * @param mq queue to use to send messages to @a peer
196 * @return the `struct BenchmarkPartner` of @a peer
199 comm_connect_cb (void *cls,
200 const struct GNUNET_PeerIdentity *peer,
201 struct GNUNET_MQ_Handle *mq)
203 struct BenchmarkPeer *me = cls;
204 struct BenchmarkPeer *remote;
205 struct BenchmarkPartner *p;
210 remote = find_peer (peer);
213 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
214 "Unknown peer connected: `%s'\n",
220 id = GNUNET_strdup (GNUNET_i2s (&me->id));
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "%s [%u] `%s' connected to %s [%u] %s\n",
223 (me->master == GNUNET_YES) ? "Master": "Slave",
226 (remote->master == GNUNET_YES) ? "Master": "Slave",
230 me->core_connections++;
231 if ((GNUNET_YES == me->master) &&
232 (GNUNET_NO == remote->master) &&
233 (GNUNET_NO == top->state.connected_CORE))
235 me->core_slave_connections++;
237 if (me->core_slave_connections == top->num_slaves)
239 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
240 "Master [%u] connected all slaves\n",
243 completed = GNUNET_YES;
244 for (c = 0; c < top->num_masters; c++)
246 if (top->mps[c].core_slave_connections != top->num_slaves)
247 completed = GNUNET_NO;
249 if (GNUNET_YES == completed)
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
252 "All master peers connected all slave peers\n");
253 top->state.connected_CORE = GNUNET_YES;
254 /* Notify about setup done */
255 if (NULL != top->done_cb)
256 top->done_cb (top->done_cb_cls,
262 p = find_partner (me,
271 * @param cls this peer
272 * @param peer id of disconnecting peer
273 * @param internal_cls the `struct BenchmarkPartner` of @a peer
276 comm_disconnect_cb (void *cls,
277 const struct GNUNET_PeerIdentity *peer,
280 struct BenchmarkPeer *me = cls;
281 struct BenchmarkPartner *p = internal_cls;
287 id = GNUNET_strdup (GNUNET_i2s (&me->id));
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "%s disconnected from %s\n",
292 GNUNET_assert (me->core_connections > 0);
293 me->core_connections--;
295 if ( (GNUNET_YES == top->state.benchmarking) &&
296 ( (GNUNET_YES == me->master) ||
297 (GNUNET_YES == p->dest->master) ) )
299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
300 "%s disconnected from %s while benchmarking\n",
309 handle_pong (void *cls,
310 const struct TestMessage *message)
312 struct BenchmarkPartner *p = cls;
314 GNUNET_ATS_TEST_traffic_handle_pong (p);
319 handle_ping (void *cls,
320 const struct TestMessage *message)
322 struct BenchmarkPartner *p = cls;
324 GNUNET_ATS_TEST_traffic_handle_ping (p);
329 transport_connect_adapter (void *cls,
330 const struct GNUNET_CONFIGURATION_Handle *cfg)
332 struct BenchmarkPeer *me = cls;
333 struct GNUNET_MQ_MessageHandler handlers[] = {
334 GNUNET_MQ_hd_fixed_size (ping,
335 TEST_MESSAGE_TYPE_PING,
338 GNUNET_MQ_hd_fixed_size (pong,
339 TEST_MESSAGE_TYPE_PONG,
342 GNUNET_MQ_handler_end ()
345 me->th = GNUNET_TRANSPORT_core_connect (cfg,
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354 "Failed to create transport connection \n");
360 transport_disconnect_adapter (void *cls,
363 struct BenchmarkPeer *me = cls;
365 GNUNET_TRANSPORT_core_disconnect (me->th);
371 core_connect_adapter (void *cls,
372 const struct GNUNET_CONFIGURATION_Handle *cfg)
374 struct BenchmarkPeer *me = cls;
375 struct GNUNET_MQ_MessageHandler handlers[] = {
376 GNUNET_MQ_hd_fixed_size (ping,
377 TEST_MESSAGE_TYPE_PING,
380 GNUNET_MQ_hd_fixed_size (pong,
381 TEST_MESSAGE_TYPE_PONG,
384 GNUNET_MQ_handler_end ()
387 me->ch = GNUNET_CORE_connect (cfg,
394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
395 "Failed to create core connection \n");
401 core_disconnect_adapter (void *cls,
404 struct BenchmarkPeer *me = cls;
406 GNUNET_CORE_disconnect (me->ch);
412 connect_completion_callback (void *cls,
413 struct GNUNET_TESTBED_Operation *op,
416 struct TestbedConnectOperation *cop = cls;
421 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
422 _("Connected master [%u] with slave [%u]\n"),
428 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
429 _("Failed to connect master peer [%u] with slave [%u]\n"),
430 cop->master->no, cop->slave->no);
432 GNUNET_SCHEDULER_shutdown ();
434 GNUNET_TESTBED_operation_done (op);
436 for (c = 0; c < top->num_slaves; c++)
438 if (cop == &cop->master->core_connect_ops[c])
439 cop->master->core_connect_ops[c].connect_op = NULL;
441 if (ops == top->num_masters * top->num_slaves)
443 top->state.connected_PEERS = GNUNET_YES;
449 do_connect_peers (void *cls)
453 struct BenchmarkPeer *p;
455 if ((top->state.connected_ATS_service == GNUNET_NO) ||
456 (top->state.connected_COMM_service == GNUNET_NO))
459 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
460 "Connecting peers on CORE level\n");
461 for (c_m = 0; c_m < top->num_masters; c_m++)
464 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
465 sizeof (struct TestbedConnectOperation));
467 for (c_s = 0; c_s < top->num_slaves; c_s++)
469 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
470 "Connecting master [%u] with slave [%u]\n",
473 p->core_connect_ops[c_s].master = p;
474 p->core_connect_ops[c_s].slave = &top->sps[c_s];
475 p->core_connect_ops[c_s].connect_op
476 = GNUNET_TESTBED_overlay_connect (NULL,
477 &connect_completion_callback,
478 &p->core_connect_ops[c_s],
481 if (NULL == p->core_connect_ops[c_s].connect_op)
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
484 "Could not connect master [%u] and slave [%u]\n",
488 GNUNET_SCHEDULER_shutdown ();
497 comm_connect_completion_cb (void *cls,
498 struct GNUNET_TESTBED_Operation *op,
502 static int comm_done = 0;
504 if ((NULL != emsg) || (NULL == ca_result))
506 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
507 "Initialization failed, shutdown\n");
509 GNUNET_SCHEDULER_shutdown ();
514 if (comm_done == top->num_slaves + top->num_masters)
516 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
517 "Connected to all %s services\n",
518 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
519 top->state.connected_COMM_service = GNUNET_YES;
520 GNUNET_SCHEDULER_add_now (&do_connect_peers,
527 do_comm_connect (void *cls)
531 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
532 "Connecting to all %s services\n",
533 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
534 for (c_m = 0; c_m < top->num_masters; c_m++)
536 if (GNUNET_YES == top->test_core)
537 top->mps[c_m].comm_op
538 = GNUNET_TESTBED_service_connect (NULL,
541 &comm_connect_completion_cb,
543 &core_connect_adapter,
544 &core_disconnect_adapter,
548 top->mps[c_m].comm_op
549 = GNUNET_TESTBED_service_connect (NULL,
552 &comm_connect_completion_cb,
554 &transport_connect_adapter,
555 &transport_disconnect_adapter,
560 for (c_s = 0; c_s < top->num_slaves; c_s++)
562 if (GNUNET_YES == top->test_core)
563 top->sps[c_s].comm_op
564 = GNUNET_TESTBED_service_connect (NULL,
567 &comm_connect_completion_cb,
569 &core_connect_adapter,
570 &core_disconnect_adapter,
574 top->sps[c_s].comm_op
575 = GNUNET_TESTBED_service_connect (NULL,
578 &comm_connect_completion_cb,
580 &transport_connect_adapter,
581 &transport_disconnect_adapter,
589 ats_performance_info_cb (void *cls,
590 const struct GNUNET_HELLO_Address *address,
592 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
593 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
594 const struct GNUNET_ATS_Properties *ats_prop)
596 struct BenchmarkPeer *me = cls;
597 struct BenchmarkPartner *p;
603 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
604 "Peer %u: ATS Service disconnected!\n",
609 p = find_partner (me,
613 /* This is not one of my partners
614 * Will happen since the peers will connect to each other due to gossiping
618 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
621 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
622 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
624 p->bandwidth_in = ntohl (bandwidth_in.value__);
625 p->bandwidth_out = ntohl (bandwidth_out.value__);
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "%s [%u] received ATS information: %s\n",
629 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
631 GNUNET_i2s (&p->dest->id));
633 p->props.utilization_out = ats_prop->utilization_out;
634 p->props.utilization_in = ats_prop->utilization_in;
635 p->props.scope = ats_prop->scope;
636 p->props.delay = ats_prop->delay;
637 p->props.distance = ats_prop->distance;
639 if (GNUNET_YES == log)
640 top->ats_perf_cb (cls, address,
645 GNUNET_free(peer_id);
650 ats_perf_connect_adapter (void *cls,
651 const struct GNUNET_CONFIGURATION_Handle *cfg)
653 struct BenchmarkPeer *me = cls;
656 = GNUNET_ATS_performance_init (cfg,
657 &ats_performance_info_cb,
659 if (NULL == me->ats_perf_handle)
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661 "Failed to create ATS performance handle \n");
662 return me->ats_perf_handle;
667 ats_perf_disconnect_adapter (void *cls,
670 struct BenchmarkPeer *me = cls;
672 GNUNET_ATS_performance_done (me->ats_perf_handle);
673 me->ats_perf_handle = NULL;
678 ats_connect_completion_cb (void *cls,
679 struct GNUNET_TESTBED_Operation *op,
683 static int op_done = 0;
685 if ((NULL != emsg) || (NULL == ca_result))
687 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
688 "Initialization failed, shutdown\n");
690 GNUNET_SCHEDULER_shutdown ();
694 if (op_done == (top->num_masters + top->num_slaves))
696 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
697 "Connected to all ATS services\n");
698 top->state.connected_ATS_service = GNUNET_YES;
699 GNUNET_SCHEDULER_add_now (&do_comm_connect,
706 do_connect_ats (void *cls)
711 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
712 "Connecting to all ATS services\n");
713 for (c_m = 0; c_m < top->num_masters; c_m++)
715 top->mps[c_m].ats_perf_op
716 = GNUNET_TESTBED_service_connect (NULL,
719 &ats_connect_completion_cb,
721 &ats_perf_connect_adapter,
722 &ats_perf_disconnect_adapter,
726 for (c_s = 0; c_s < top->num_slaves; c_s++)
728 top->sps[c_s].ats_perf_op
729 = GNUNET_TESTBED_service_connect (NULL,
732 &ats_connect_completion_cb,
734 &ats_perf_connect_adapter,
735 &ats_perf_disconnect_adapter,
743 peerinformation_cb (void *cb_cls,
744 struct GNUNET_TESTBED_Operation *op,
745 const struct GNUNET_TESTBED_PeerInformation *pinfo,
748 struct BenchmarkPeer *p = cb_cls;
751 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
753 p->id = *pinfo->result.id;
754 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
755 "%s [%u] has peer id `%s'\n",
756 (p->master == GNUNET_YES) ? "Master" : "Slave",
758 GNUNET_i2s (&p->id));
760 GNUNET_TESTBED_operation_done (op);
761 p->peer_id_op = NULL;
764 if (done == top->num_slaves + top->num_masters)
766 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
767 "Retrieved all peer ID, connect to ATS\n");
768 GNUNET_SCHEDULER_add_now (&do_connect_ats,
775 * Signature of a main function for a testcase.
778 * @param h testbed handle
779 * @param num_peers number of peers in 'peers'
780 * @param peers_ handle to peers run in the testbed
781 * @param links_succeeded the number of overlay link connection attempts that
783 * @param links_failed the number of overlay link connection attempts that
788 struct GNUNET_TESTBED_RunHandle *h,
789 unsigned int num_peers,
790 struct GNUNET_TESTBED_Peer **peers_,
791 unsigned int links_succeeded,
792 unsigned int links_failed)
797 GNUNET_assert (NULL == cls);
798 GNUNET_assert (top->num_masters + top->num_slaves == num_peers);
799 GNUNET_assert (NULL != peers_);
801 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
805 "Setting up %u masters and %u slaves\n",
809 /* Setup master peers */
810 for (c_m = 0; c_m < top->num_masters; c_m++)
812 GNUNET_assert(NULL != peers_[c_m]);
813 top->mps[c_m].peer = peers_[c_m];
814 top->mps[c_m].no = c_m;
815 top->mps[c_m].master = GNUNET_YES;
816 top->mps[c_m].pref_partner = &top->sps[c_m];
817 top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
818 top->mps[c_m].partners =
819 GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
820 top->mps[c_m].num_partners = top->num_slaves;
821 /* Initialize partners */
822 for (c_s = 0; c_s < top->num_slaves; c_s++)
824 top->mps[c_m].partners[c_s].me = &top->mps[c_m];
825 top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
827 /* Get configuration */
828 top->mps[c_m].peer_id_op
829 = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
830 GNUNET_TESTBED_PIT_IDENTITY,
835 /* Setup slave peers */
836 for (c_s = 0; c_s < top->num_slaves; c_s++)
838 GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
839 top->sps[c_s].peer = peers_[c_s + top->num_masters];
840 top->sps[c_s].no = c_s + top->num_masters;
841 top->sps[c_s].master = GNUNET_NO;
842 top->sps[c_s].partners =
843 GNUNET_new_array (top->num_masters,
844 struct BenchmarkPartner);
845 top->sps[c_s].num_partners = top->num_masters;
846 /* Initialize partners */
847 for (c_m = 0; c_m < top->num_masters; c_m++)
849 top->sps[c_s].partners[c_m].me = &top->sps[c_s];
850 top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
852 /* Initialize properties */
853 top->sps[c_s].partners[c_m].props.delay = GNUNET_TIME_UNIT_ZERO;
854 top->sps[c_s].partners[c_m].props.distance = 0;
855 top->sps[c_s].partners[c_m].props.scope = GNUNET_ATS_NET_UNSPECIFIED;
856 top->sps[c_s].partners[c_m].props.utilization_in = 0;
857 top->sps[c_s].partners[c_m].props.utilization_out = 0;
859 /* Get configuration */
860 top->sps[c_s].peer_id_op
861 = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
862 GNUNET_TESTBED_PIT_IDENTITY,
870 * Controller event callback
873 * @param event the controller event
876 controller_event_cb (void *cls,
877 const struct GNUNET_TESTBED_EventInformation *event)
881 case GNUNET_TESTBED_ET_CONNECT:
883 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
887 GNUNET_SCHEDULER_shutdown ();
892 struct BenchmarkPeer *
893 GNUNET_ATS_TEST_get_peer (int src)
895 if (src > top->num_masters)
897 return &top->mps[src];
901 struct BenchmarkPartner *
902 GNUNET_ATS_TEST_get_partner (int src,
905 if (src > top->num_masters)
907 if (dest > top->num_slaves)
909 return &top->mps[src].partners[dest];
914 * Create a topology for ats testing
916 * @param name test name
917 * @param cfg_file configuration file to use for the peers
918 * @param num_slaves number of slaves
919 * @param num_masters number of masters
920 * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
921 * @param done_cb function to call when topology is setup
922 * @param done_cb_cls cls for callback
923 * @param log_request_cb callback to call when logging is required
926 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
927 unsigned int num_slaves,
928 unsigned int num_masters,
930 GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
932 GNUNET_ATS_AddressInformationCallback log_request_cb)
934 top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
935 top->num_masters = num_masters;
936 top->num_slaves = num_slaves;
937 top->done_cb = done_cb;
938 top->done_cb_cls = done_cb_cls;
939 top->test_core = test_core;
940 top->ats_perf_cb = log_request_cb;
941 top->mps = GNUNET_new_array (num_masters,
942 struct BenchmarkPeer);
943 top->sps = GNUNET_new_array (num_slaves,
944 struct BenchmarkPeer);
949 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
950 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
951 (void) GNUNET_TESTBED_test_run (name,
953 num_slaves + num_masters,
955 &controller_event_cb, NULL,
964 GNUNET_ATS_TEST_shutdown_topology (void)
968 GNUNET_SCHEDULER_shutdown();
972 /* end of file ats-testing.c */