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 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.
16 * @file ats-tests/ats-testing.c
17 * @brief ats testing library: setup topology
19 * @author Christian Grothoff
20 * @author Matthias Wachs
22 #include "ats-testing.h"
26 * Connect peers with testbed
28 struct TestbedConnectOperation
31 * The benchmarking master initiating this connection
33 struct BenchmarkPeer *master;
36 * The benchmarking slave to connect to
38 struct BenchmarkPeer *slave;
41 * Testbed operation to connect peers
43 struct GNUNET_TESTBED_Operation *connect_op;
46 struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_ATS_TEST_Topology *top;
57 do_shutdown (void *cls)
62 struct BenchmarkPeer *p;
64 top->state.benchmarking = GNUNET_NO;
66 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
67 "Benchmarking done\n");
69 GNUNET_ATS_TEST_generate_traffic_stop_all ();
71 for (c_m = 0; c_m < top->num_masters; c_m++)
74 if (NULL != top->mps[c_m].peer_id_op)
76 GNUNET_TESTBED_operation_done (p->peer_id_op);
80 if (NULL != p->ats_task)
81 GNUNET_SCHEDULER_cancel (p->ats_task);
84 for (c_op = 0; c_op < p->num_partners; c_op++)
86 if ( (NULL != p->core_connect_ops) &&
87 (NULL != p->core_connect_ops[c_op].connect_op) )
89 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
90 "Failed to connect peer 0 and %u\n",
92 GNUNET_TESTBED_operation_done (p->core_connect_ops[c_op].connect_op);
93 p->core_connect_ops[c_op].connect_op = NULL;
97 if (NULL != p->ats_perf_op)
99 GNUNET_TESTBED_operation_done (p->ats_perf_op);
100 p->ats_perf_op = NULL;
103 if (NULL != p->comm_op)
105 GNUNET_TESTBED_operation_done (p->comm_op);
108 GNUNET_free_non_null (p->core_connect_ops);
109 GNUNET_free(p->partners);
113 for (c_s = 0; c_s < top->num_slaves; c_s++)
116 if (NULL != p->peer_id_op)
118 GNUNET_TESTBED_operation_done (p->peer_id_op);
119 p->peer_id_op = NULL;
121 if (NULL != p->ats_perf_op)
123 GNUNET_TESTBED_operation_done (p->ats_perf_op);
124 p->ats_perf_op = NULL;
126 if (NULL != p->comm_op)
128 GNUNET_TESTBED_operation_done (p->comm_op);
131 GNUNET_free(p->partners);
134 GNUNET_SCHEDULER_shutdown ();
140 static struct BenchmarkPartner *
141 find_partner (struct BenchmarkPeer *me,
142 const struct GNUNET_PeerIdentity *peer)
146 for (c_m = 0; c_m < me->num_partners; c_m++)
148 /* Find a partner with other as destination */
149 if (0 == memcmp (peer,
150 &me->partners[c_m].dest->id,
151 sizeof (struct GNUNET_PeerIdentity)))
153 return &me->partners[c_m];
161 static struct BenchmarkPeer *
162 find_peer (const struct GNUNET_PeerIdentity * peer)
166 for (c_p = 0; c_p < top->num_masters; c_p++)
168 if (0 == memcmp (&top->mps[c_p].id,
170 sizeof(struct GNUNET_PeerIdentity)))
171 return &top->mps[c_p];
174 for (c_p = 0; c_p < top->num_slaves; c_p++)
176 if (0 == memcmp (&top->sps[c_p].id,
178 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
190 * @param mq queue to use to send messages to @a peer
191 * @return the `struct BenchmarkPartner` of @a peer
194 comm_connect_cb (void *cls,
195 const struct GNUNET_PeerIdentity *peer,
196 struct GNUNET_MQ_Handle *mq)
198 struct BenchmarkPeer *me = cls;
199 struct BenchmarkPeer *remote;
200 struct BenchmarkPartner *p;
205 remote = find_peer (peer);
208 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
209 "Unknown peer connected: `%s'\n",
215 id = GNUNET_strdup (GNUNET_i2s (&me->id));
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "%s [%u] `%s' connected to %s [%u] %s\n",
218 (me->master == GNUNET_YES) ? "Master": "Slave",
221 (remote->master == GNUNET_YES) ? "Master": "Slave",
225 me->core_connections++;
226 if ((GNUNET_YES == me->master) &&
227 (GNUNET_NO == remote->master) &&
228 (GNUNET_NO == top->state.connected_CORE))
230 me->core_slave_connections++;
232 if (me->core_slave_connections == top->num_slaves)
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "Master [%u] connected all slaves\n",
238 completed = GNUNET_YES;
239 for (c = 0; c < top->num_masters; c++)
241 if (top->mps[c].core_slave_connections != top->num_slaves)
242 completed = GNUNET_NO;
244 if (GNUNET_YES == completed)
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
247 "All master peers connected all slave peers\n");
248 top->state.connected_CORE = GNUNET_YES;
249 /* Notify about setup done */
250 if (NULL != top->done_cb)
251 top->done_cb (top->done_cb_cls,
257 p = find_partner (me,
266 * @param cls this peer
267 * @param peer id of disconnecting peer
268 * @param internal_cls the `struct BenchmarkPartner` of @a peer
271 comm_disconnect_cb (void *cls,
272 const struct GNUNET_PeerIdentity *peer,
275 struct BenchmarkPeer *me = cls;
276 struct BenchmarkPartner *p = internal_cls;
282 id = GNUNET_strdup (GNUNET_i2s (&me->id));
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "%s disconnected from %s\n",
287 GNUNET_assert (me->core_connections > 0);
288 me->core_connections--;
290 if ( (GNUNET_YES == top->state.benchmarking) &&
291 ( (GNUNET_YES == me->master) ||
292 (GNUNET_YES == p->dest->master) ) )
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
295 "%s disconnected from %s while benchmarking\n",
304 handle_pong (void *cls,
305 const struct TestMessage *message)
307 struct BenchmarkPartner *p = cls;
309 GNUNET_ATS_TEST_traffic_handle_pong (p);
314 handle_ping (void *cls,
315 const struct TestMessage *message)
317 struct BenchmarkPartner *p = cls;
319 GNUNET_ATS_TEST_traffic_handle_ping (p);
324 transport_connect_adapter (void *cls,
325 const struct GNUNET_CONFIGURATION_Handle *cfg)
327 struct BenchmarkPeer *me = cls;
328 struct GNUNET_MQ_MessageHandler handlers[] = {
329 GNUNET_MQ_hd_fixed_size (ping,
330 TEST_MESSAGE_TYPE_PING,
333 GNUNET_MQ_hd_fixed_size (pong,
334 TEST_MESSAGE_TYPE_PONG,
337 GNUNET_MQ_handler_end ()
340 me->th = GNUNET_TRANSPORT_core_connect (cfg,
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 "Failed to create transport connection \n");
355 transport_disconnect_adapter (void *cls,
358 struct BenchmarkPeer *me = cls;
360 GNUNET_TRANSPORT_core_disconnect (me->th);
366 core_connect_adapter (void *cls,
367 const struct GNUNET_CONFIGURATION_Handle *cfg)
369 struct BenchmarkPeer *me = cls;
370 struct GNUNET_MQ_MessageHandler handlers[] = {
371 GNUNET_MQ_hd_fixed_size (ping,
372 TEST_MESSAGE_TYPE_PING,
375 GNUNET_MQ_hd_fixed_size (pong,
376 TEST_MESSAGE_TYPE_PONG,
379 GNUNET_MQ_handler_end ()
382 me->ch = GNUNET_CORE_connect (cfg,
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390 "Failed to create core connection \n");
396 core_disconnect_adapter (void *cls,
399 struct BenchmarkPeer *me = cls;
401 GNUNET_CORE_disconnect (me->ch);
407 connect_completion_callback (void *cls,
408 struct GNUNET_TESTBED_Operation *op,
411 struct TestbedConnectOperation *cop = cls;
416 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
417 _("Connected master [%u] with slave [%u]\n"),
423 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
424 _("Failed to connect master peer [%u] with slave [%u]\n"),
425 cop->master->no, cop->slave->no);
427 GNUNET_SCHEDULER_shutdown ();
429 GNUNET_TESTBED_operation_done (op);
431 for (c = 0; c < top->num_slaves; c++)
433 if (cop == &cop->master->core_connect_ops[c])
434 cop->master->core_connect_ops[c].connect_op = NULL;
436 if (ops == top->num_masters * top->num_slaves)
438 top->state.connected_PEERS = GNUNET_YES;
444 do_connect_peers (void *cls)
448 struct BenchmarkPeer *p;
450 if ((top->state.connected_ATS_service == GNUNET_NO) ||
451 (top->state.connected_COMM_service == GNUNET_NO))
454 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
455 "Connecting peers on CORE level\n");
456 for (c_m = 0; c_m < top->num_masters; c_m++)
459 p->core_connect_ops = GNUNET_malloc (top->num_slaves *
460 sizeof (struct TestbedConnectOperation));
462 for (c_s = 0; c_s < top->num_slaves; c_s++)
464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
465 "Connecting master [%u] with slave [%u]\n",
468 p->core_connect_ops[c_s].master = p;
469 p->core_connect_ops[c_s].slave = &top->sps[c_s];
470 p->core_connect_ops[c_s].connect_op
471 = GNUNET_TESTBED_overlay_connect (NULL,
472 &connect_completion_callback,
473 &p->core_connect_ops[c_s],
476 if (NULL == p->core_connect_ops[c_s].connect_op)
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
479 "Could not connect master [%u] and slave [%u]\n",
483 GNUNET_SCHEDULER_shutdown ();
492 comm_connect_completion_cb (void *cls,
493 struct GNUNET_TESTBED_Operation *op,
497 static int comm_done = 0;
499 if ((NULL != emsg) || (NULL == ca_result))
501 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
502 "Initialization failed, shutdown\n");
504 GNUNET_SCHEDULER_shutdown ();
509 if (comm_done == top->num_slaves + top->num_masters)
511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
512 "Connected to all %s services\n",
513 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
514 top->state.connected_COMM_service = GNUNET_YES;
515 GNUNET_SCHEDULER_add_now (&do_connect_peers,
522 do_comm_connect (void *cls)
526 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
527 "Connecting to all %s services\n",
528 (GNUNET_YES == top->test_core) ? "CORE" : "TRANSPORT");
529 for (c_m = 0; c_m < top->num_masters; c_m++)
531 if (GNUNET_YES == top->test_core)
532 top->mps[c_m].comm_op
533 = GNUNET_TESTBED_service_connect (NULL,
536 &comm_connect_completion_cb,
538 &core_connect_adapter,
539 &core_disconnect_adapter,
543 top->mps[c_m].comm_op
544 = GNUNET_TESTBED_service_connect (NULL,
547 &comm_connect_completion_cb,
549 &transport_connect_adapter,
550 &transport_disconnect_adapter,
555 for (c_s = 0; c_s < top->num_slaves; c_s++)
557 if (GNUNET_YES == top->test_core)
558 top->sps[c_s].comm_op
559 = GNUNET_TESTBED_service_connect (NULL,
562 &comm_connect_completion_cb,
564 &core_connect_adapter,
565 &core_disconnect_adapter,
569 top->sps[c_s].comm_op
570 = GNUNET_TESTBED_service_connect (NULL,
573 &comm_connect_completion_cb,
575 &transport_connect_adapter,
576 &transport_disconnect_adapter,
584 ats_performance_info_cb (void *cls,
585 const struct GNUNET_HELLO_Address *address,
587 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
588 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
589 const struct GNUNET_ATS_Properties *ats_prop)
591 struct BenchmarkPeer *me = cls;
592 struct BenchmarkPartner *p;
598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599 "Peer %u: ATS Service disconnected!\n",
604 p = find_partner (me,
608 /* This is not one of my partners
609 * Will happen since the peers will connect to each other due to gossiping
613 peer_id = GNUNET_strdup (GNUNET_i2s (&me->id));
616 if ((p->bandwidth_in != ntohl (bandwidth_in.value__)) ||
617 (p->bandwidth_out != ntohl (bandwidth_out.value__)))
619 p->bandwidth_in = ntohl (bandwidth_in.value__);
620 p->bandwidth_out = ntohl (bandwidth_out.value__);
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "%s [%u] received ATS information: %s\n",
624 (GNUNET_YES == p->me->master) ? "Master" : "Slave",
626 GNUNET_i2s (&p->dest->id));
628 p->props.utilization_out = ats_prop->utilization_out;
629 p->props.utilization_in = ats_prop->utilization_in;
630 p->props.scope = ats_prop->scope;
631 p->props.delay = ats_prop->delay;
632 p->props.distance = ats_prop->distance;
634 if (GNUNET_YES == log)
635 top->ats_perf_cb (cls, address,
640 GNUNET_free(peer_id);
645 ats_perf_connect_adapter (void *cls,
646 const struct GNUNET_CONFIGURATION_Handle *cfg)
648 struct BenchmarkPeer *me = cls;
651 = GNUNET_ATS_performance_init (cfg,
652 &ats_performance_info_cb,
654 if (NULL == me->ats_perf_handle)
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 "Failed to create ATS performance handle \n");
657 return me->ats_perf_handle;
662 ats_perf_disconnect_adapter (void *cls,
665 struct BenchmarkPeer *me = cls;
667 GNUNET_ATS_performance_done (me->ats_perf_handle);
668 me->ats_perf_handle = NULL;
673 ats_connect_completion_cb (void *cls,
674 struct GNUNET_TESTBED_Operation *op,
678 static int op_done = 0;
680 if ((NULL != emsg) || (NULL == ca_result))
682 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
683 "Initialization failed, shutdown\n");
685 GNUNET_SCHEDULER_shutdown ();
689 if (op_done == (top->num_masters + top->num_slaves))
691 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
692 "Connected to all ATS services\n");
693 top->state.connected_ATS_service = GNUNET_YES;
694 GNUNET_SCHEDULER_add_now (&do_comm_connect,
701 do_connect_ats (void *cls)
706 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
707 "Connecting to all ATS services\n");
708 for (c_m = 0; c_m < top->num_masters; c_m++)
710 top->mps[c_m].ats_perf_op
711 = GNUNET_TESTBED_service_connect (NULL,
714 &ats_connect_completion_cb,
716 &ats_perf_connect_adapter,
717 &ats_perf_disconnect_adapter,
721 for (c_s = 0; c_s < top->num_slaves; c_s++)
723 top->sps[c_s].ats_perf_op
724 = GNUNET_TESTBED_service_connect (NULL,
727 &ats_connect_completion_cb,
729 &ats_perf_connect_adapter,
730 &ats_perf_disconnect_adapter,
738 peerinformation_cb (void *cb_cls,
739 struct GNUNET_TESTBED_Operation *op,
740 const struct GNUNET_TESTBED_PeerInformation *pinfo,
743 struct BenchmarkPeer *p = cb_cls;
746 GNUNET_assert(pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY);
748 p->id = *pinfo->result.id;
749 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
750 "%s [%u] has peer id `%s'\n",
751 (p->master == GNUNET_YES) ? "Master" : "Slave",
753 GNUNET_i2s (&p->id));
755 GNUNET_TESTBED_operation_done (op);
756 p->peer_id_op = NULL;
759 if (done == top->num_slaves + top->num_masters)
761 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
762 "Retrieved all peer ID, connect to ATS\n");
763 GNUNET_SCHEDULER_add_now (&do_connect_ats,
770 * Signature of a main function for a testcase.
773 * @param h testbed handle
774 * @param num_peers number of peers in 'peers'
775 * @param peers_ handle to peers run in the testbed
776 * @param links_succeeded the number of overlay link connection attempts that
778 * @param links_failed the number of overlay link connection attempts that
783 struct GNUNET_TESTBED_RunHandle *h,
784 unsigned int num_peers,
785 struct GNUNET_TESTBED_Peer **peers_,
786 unsigned int links_succeeded,
787 unsigned int links_failed)
792 GNUNET_assert (NULL == cls);
793 GNUNET_assert (top->num_masters + top->num_slaves == num_peers);
794 GNUNET_assert (NULL != peers_);
796 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
799 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
800 "Setting up %u masters and %u slaves\n",
804 /* Setup master peers */
805 for (c_m = 0; c_m < top->num_masters; c_m++)
807 GNUNET_assert(NULL != peers_[c_m]);
808 top->mps[c_m].peer = peers_[c_m];
809 top->mps[c_m].no = c_m;
810 top->mps[c_m].master = GNUNET_YES;
811 top->mps[c_m].pref_partner = &top->sps[c_m];
812 top->mps[c_m].pref_value = TEST_ATS_PREFERENCE_DEFAULT;
813 top->mps[c_m].partners =
814 GNUNET_malloc (top->num_slaves * sizeof (struct BenchmarkPartner));
815 top->mps[c_m].num_partners = top->num_slaves;
816 /* Initialize partners */
817 for (c_s = 0; c_s < top->num_slaves; c_s++)
819 top->mps[c_m].partners[c_s].me = &top->mps[c_m];
820 top->mps[c_m].partners[c_s].dest = &top->sps[c_s];
822 /* Get configuration */
823 top->mps[c_m].peer_id_op
824 = GNUNET_TESTBED_peer_get_information (top->mps[c_m].peer,
825 GNUNET_TESTBED_PIT_IDENTITY,
830 /* Setup slave peers */
831 for (c_s = 0; c_s < top->num_slaves; c_s++)
833 GNUNET_assert(NULL != peers_[c_s + top->num_masters]);
834 top->sps[c_s].peer = peers_[c_s + top->num_masters];
835 top->sps[c_s].no = c_s + top->num_masters;
836 top->sps[c_s].master = GNUNET_NO;
837 top->sps[c_s].partners =
838 GNUNET_new_array (top->num_masters,
839 struct BenchmarkPartner);
840 top->sps[c_s].num_partners = top->num_masters;
841 /* Initialize partners */
842 for (c_m = 0; c_m < top->num_masters; c_m++)
844 top->sps[c_s].partners[c_m].me = &top->sps[c_s];
845 top->sps[c_s].partners[c_m].dest = &top->mps[c_m];
847 /* Initialize properties */
848 top->sps[c_s].partners[c_m].props.delay = GNUNET_TIME_UNIT_ZERO;
849 top->sps[c_s].partners[c_m].props.distance = 0;
850 top->sps[c_s].partners[c_m].props.scope = GNUNET_ATS_NET_UNSPECIFIED;
851 top->sps[c_s].partners[c_m].props.utilization_in = 0;
852 top->sps[c_s].partners[c_m].props.utilization_out = 0;
854 /* Get configuration */
855 top->sps[c_s].peer_id_op
856 = GNUNET_TESTBED_peer_get_information (top->sps[c_s].peer,
857 GNUNET_TESTBED_PIT_IDENTITY,
865 * Controller event callback
868 * @param event the controller event
871 controller_event_cb (void *cls,
872 const struct GNUNET_TESTBED_EventInformation *event)
876 case GNUNET_TESTBED_ET_CONNECT:
878 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
882 GNUNET_SCHEDULER_shutdown ();
887 struct BenchmarkPeer *
888 GNUNET_ATS_TEST_get_peer (int src)
890 if (src > top->num_masters)
892 return &top->mps[src];
896 struct BenchmarkPartner *
897 GNUNET_ATS_TEST_get_partner (int src,
900 if (src > top->num_masters)
902 if (dest > top->num_slaves)
904 return &top->mps[src].partners[dest];
909 * Create a topology for ats testing
911 * @param name test name
912 * @param cfg_file configuration file to use for the peers
913 * @param num_slaves number of slaves
914 * @param num_masters number of masters
915 * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
916 * @param done_cb function to call when topology is setup
917 * @param done_cb_cls cls for callback
918 * @param log_request_cb callback to call when logging is required
921 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
922 unsigned int num_slaves,
923 unsigned int num_masters,
925 GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
927 GNUNET_ATS_AddressInformationCallback log_request_cb)
929 top = GNUNET_new (struct GNUNET_ATS_TEST_Topology);
930 top->num_masters = num_masters;
931 top->num_slaves = num_slaves;
932 top->done_cb = done_cb;
933 top->done_cb_cls = done_cb_cls;
934 top->test_core = test_core;
935 top->ats_perf_cb = log_request_cb;
936 top->mps = GNUNET_new_array (num_masters,
937 struct BenchmarkPeer);
938 top->sps = GNUNET_new_array (num_slaves,
939 struct BenchmarkPeer);
944 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
945 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
946 (void) GNUNET_TESTBED_test_run (name,
948 num_slaves + num_masters,
950 &controller_event_cb, NULL,
959 GNUNET_ATS_TEST_shutdown_topology (void)
963 GNUNET_SCHEDULER_shutdown();
967 /* end of file ats-testing.c */