2 This file is part of GNUnet.
3 Copyright (C) 2011, 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 cadet/test_cadet_flow.c
23 * @author Christian Grothoff
24 * @brief Test for flow control of CADET service
28 #include "cadet_test_lib.h"
29 #include "gnunet_cadet_service.h"
30 #include "gnunet_statistics_service.h"
35 * Ugly workaround to unify data handlers on incoming and outgoing channels.
37 struct CadetTestChannelWrapper
42 struct GNUNET_CADET_Channel *ch;
46 * How many messages to send by default.
48 #define TOTAL_PACKETS_DEFAULT 500
51 * How long until we give up on connecting the peers?
53 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
56 * Time to wait by default for stuff that should be rather fast.
58 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
61 * How fast do we send messages?
63 #define SEND_INTERVAL GNUNET_TIME_relative_multiply ( \
64 GNUNET_TIME_UNIT_MILLISECONDS, 10)
68 * How many packets to send.
70 static unsigned int total_packets = TOTAL_PACKETS_DEFAULT;
73 * Time to wait for fast operations.
75 static struct GNUNET_TIME_Relative short_time;
78 * Size of each test packet's payload
80 static size_t size_payload = sizeof(uint32_t);
83 * Operation to get peer ids.
85 static struct GNUNET_TESTBED_Operation *t_op[2];
90 static struct GNUNET_PeerIdentity *p_id[2];
95 static struct GNUNET_HashCode port;
100 static unsigned int p_ids;
103 * Is the setup initialized?
105 static int initialized;
108 * Number of payload packes sent.
110 static int data_sent;
113 * Number of payload packets received.
115 static int data_received;
118 * Number of payload packed acknowledgements sent.
123 * Number of payload packed explicitly (app level) acknowledged.
125 static int ack_received;
128 * Total number of peers asked to run.
130 static unsigned int peers_requested = 2;
133 * Number of currently running peers (should be same as @c peers_requested).
135 static unsigned int peers_running;
138 * Test context (to shut down).
140 struct GNUNET_CADET_TEST_Context *test_ctx;
143 * Task called to disconnect peers.
145 static struct GNUNET_SCHEDULER_Task *disconnect_task;
148 * Task To perform tests
150 static struct GNUNET_SCHEDULER_Task *test_task;
153 * Task runnining #send_next_msg().
155 static struct GNUNET_SCHEDULER_Task *send_next_msg_task;
158 * Cadet handle for the root peer
160 static struct GNUNET_CADET_Handle *h1;
163 * Cadet handle for the first leaf peer
165 static struct GNUNET_CADET_Handle *h2;
168 * Channel handle for the root peer
170 static struct GNUNET_CADET_Channel *outgoing_ch;
173 * Channel handle for the dest peer
175 static struct GNUNET_CADET_Channel *incoming_ch;
178 * Time we started the data transmission (after channel has been established
181 static struct GNUNET_TIME_Absolute start_time;
186 static struct GNUNET_TESTBED_Peer **testbed_peers;
189 * Statistics operation handle.
191 static struct GNUNET_TESTBED_Operation *stats_op;
196 static unsigned int ka_sent;
199 * Keepalives received.
201 static unsigned int ka_received;
204 * How many messages were dropped by CADET because of full buffers?
206 static unsigned int msg_dropped;
210 * Show the results of the test (banwidth acheived) and log them to GAUGER
215 static struct GNUNET_TIME_Absolute end_time;
216 static struct GNUNET_TIME_Relative total_time;
218 end_time = GNUNET_TIME_absolute_get ();
219 total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time);
221 "\nResults of test \"%s\"\n",
225 GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES));
227 "Test bandwidth: %f kb/s\n",
228 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
230 "Test throughput: %f packets/s\n\n",
231 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
234 total_packets * 1000.0 / (total_time.rel_value_us / 1000),
240 * Shut down peergroup, clean up.
242 * @param cls Closure (unused).
243 * @param tc Task Context.
246 shutdown_task (void *cls)
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250 if (NULL != send_next_msg_task)
252 GNUNET_SCHEDULER_cancel (send_next_msg_task);
253 send_next_msg_task = NULL;
255 if (NULL != test_task)
257 GNUNET_SCHEDULER_cancel (test_task);
260 for (unsigned int i = 0; i < 2; i++)
261 GNUNET_TESTBED_operation_done (t_op[i]);
262 if (NULL != outgoing_ch)
264 GNUNET_CADET_channel_destroy (outgoing_ch);
267 if (NULL != incoming_ch)
269 GNUNET_CADET_channel_destroy (incoming_ch);
272 GNUNET_CADET_TEST_cleanup (test_ctx);
277 * Stats callback. Finish the stats testbed operation and when all stats have
278 * been iterated, shutdown the test.
280 * @param cls Closure (line number from which termination was requested).
281 * @param op the operation that has been finished
282 * @param emsg error message in case the operation has failed; will be NULL if
283 * operation has executed successfully.
286 stats_cont (void *cls,
287 struct GNUNET_TESTBED_Operation *op,
290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291 "KA sent: %u, KA received: %u\n",
294 if ((KEEPALIVE == test) && ((ka_sent < 2) || (ka_sent > ka_received + 1)))
299 GNUNET_TESTBED_operation_done (stats_op);
301 if (NULL != disconnect_task)
302 GNUNET_SCHEDULER_cancel (disconnect_task);
303 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
309 * Process statistic values.
311 * @param cls closure (line number, unused)
312 * @param peer the peer the statistic belong to
313 * @param subsystem name of subsystem that created the statistic
314 * @param name the name of the datum
315 * @param value the current value
316 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
317 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
320 stats_iterator (void *cls,
321 const struct GNUNET_TESTBED_Peer *peer,
322 const char *subsystem,
327 static const char *s_sent = "# keepalives sent";
328 static const char *s_recv = "# keepalives received";
329 static const char *rdrops = "# messages dropped due to full buffer";
330 static const char *cdrops = "# messages dropped due to slow client";
333 i = GNUNET_TESTBED_get_index (peer);
334 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i,
335 subsystem, name, (unsigned long long) value);
336 if ((0 == strncmp (s_sent, name, strlen (s_sent))) && (0 == i))
338 if ((0 == strncmp (s_recv, name, strlen (s_recv))) && (peers_requested - 1 ==
341 if (0 == strncmp (rdrops, name, strlen (rdrops)))
342 msg_dropped += value;
343 if (0 == strncmp (cdrops, name, strlen (cdrops)))
344 msg_dropped += value;
351 * Task to gather all statistics.
353 * @param cls Closure (line from which the task was scheduled).
356 gather_stats_and_exit (void *cls)
360 disconnect_task = NULL;
361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
362 "gathering statistics from line %ld\n",
364 if (NULL != outgoing_ch)
366 GNUNET_CADET_channel_destroy (outgoing_ch);
369 stats_op = GNUNET_TESTBED_get_statistics (peers_running,
380 * Abort test: schedule disconnect and shutdown immediately
382 * @param line Line in the code the abort is requested from (__LINE__).
385 abort_test (long line)
387 if (NULL != disconnect_task)
389 GNUNET_SCHEDULER_cancel (disconnect_task);
390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
391 "Aborting test from %ld\n",
394 GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
401 * Send a message on the channel with the appropriate size and payload.
403 * Update the appropriate *_sent counter.
405 * @param channel Channel to send the message on.
408 send_test_message (struct GNUNET_CADET_Channel *channel)
410 struct GNUNET_MQ_Envelope *env;
411 struct GNUNET_MessageHeader *msg;
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Sending test message on channel %p\n",
420 if (GNUNET_NO == initialized)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n");
425 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer
428 else if ((SPEED == test) || (SPEED_ACK == test))
430 if (get_target_channel () == channel)
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Sending ACK %u [%d bytes]\n",
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
445 "Sending DATA %u [%d bytes]\n",
449 else if (FORWARD == test)
453 else if (P2P_SIGNAL == test)
461 env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY);
463 data = (uint32_t *) &msg[1];
464 *data = htonl (payload);
465 GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env);
470 * Task to request a new data transmission in a SPEED test, without waiting
471 * for previous messages to be sent/arrrive.
473 * @param cls Closure (unused).
476 send_next_msg (void *cls)
478 struct GNUNET_CADET_Channel *channel;
480 send_next_msg_task = NULL;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Sending next message: %d\n",
485 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch;
486 GNUNET_assert (NULL != channel);
487 GNUNET_assert (SPEED == test);
488 send_test_message (channel);
489 if (data_sent < total_packets)
491 /* SPEED test: Send all messages as soon as possible */
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Scheduling message %d\n",
496 GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL,
504 * Check if payload is sane (size contains payload).
506 * @param cls should match #ch
507 * @param message The actual message.
508 * @return #GNUNET_OK to keep the channel open,
509 * #GNUNET_SYSERR to close it (signal serious error).
512 check_data (void *cls,
513 const struct GNUNET_MessageHeader *message)
515 return GNUNET_OK; /* all is well-formed */
520 * Function is called whenever a message is received.
522 * @param cls closure (set from GNUNET_CADET_connect(), peer number)
523 * @param message the actual message
526 handle_data (void *cls,
527 const struct GNUNET_MessageHeader *message)
529 struct CadetTestChannelWrapper *ch = cls;
530 struct GNUNET_CADET_Channel *channel = ch->ch;
535 GNUNET_CADET_receive_done (channel);
536 counter = get_target_channel () == channel ? &data_received : &ack_received;
537 if (channel == outgoing_ch)
539 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
540 "Root client got a message.\n");
542 else if (channel == incoming_ch)
544 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
545 "Leaf client got a message.\n");
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 "Unknown channel %p.\n",
555 data = (uint32_t *) &message[1];
556 payload = ntohl (*data);
557 if (payload == *counter)
559 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
560 "Payload as expected: %u\n",
565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
566 "Received payload %u, expected: %u\n",
570 if (get_target_channel () == channel) /* Got "data" */
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573 " received data %u\n",
575 if (data_received < total_packets)
580 if ((SPEED_ACK == test) || (SPEED == test) )
582 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
584 send_test_message (channel);
585 if ((ack_received < total_packets) && (SPEED != test) )
587 if ((ok == 2) && (SPEED == test) )
591 if (test == P2P_SIGNAL)
593 GNUNET_CADET_channel_destroy (incoming_ch);
598 GNUNET_CADET_channel_destroy (outgoing_ch);
606 * Method called whenever a peer connects to a port in MQ-based CADET.
608 * @param cls Closure from #GNUNET_CADET_open_port (peer # as long).
609 * @param channel New handle to the channel.
610 * @param source Peer that started this channel.
611 * @return Closure for the incoming @a channel. It's given to:
612 * - The #GNUNET_CADET_DisconnectEventHandler (given to
613 * #GNUNET_CADET_open_port) when the channel dies.
614 * - Each the #GNUNET_MQ_MessageCallback handlers for each message
615 * received on the @a channel.
618 connect_handler (void *cls,
619 struct GNUNET_CADET_Channel *channel,
620 const struct GNUNET_PeerIdentity *source)
622 struct CadetTestChannelWrapper *ch;
623 long peer = (long) cls;
625 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
626 "Incoming channel from %s to %ld: %p\n",
630 if (peer == peers_requested - 1)
632 if (NULL != incoming_ch)
634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
635 "Duplicate incoming channel for client %lu\n",
639 incoming_ch = channel;
643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
644 "Incoming channel for unexpected peer #%lu\n",
648 ch = GNUNET_new (struct CadetTestChannelWrapper);
656 * Function called whenever an MQ-channel is destroyed, even if the destruction
657 * was requested by #GNUNET_CADET_channel_destroy.
658 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
660 * It should clean up any associated state, including cancelling any pending
661 * transmission on this channel.
663 * @param cls Channel closure (channel wrapper).
664 * @param channel Connection to the other end (henceforth invalid).
667 disconnect_handler (void *cls,
668 const struct GNUNET_CADET_Channel *channel)
670 struct CadetTestChannelWrapper *ch_w = cls;
672 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
673 "Channel disconnected at %d\n",
675 GNUNET_assert (ch_w->ch == channel);
676 if (channel == incoming_ch)
678 else if (outgoing_ch == channel)
681 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
682 "Disconnect on unknown channel %p\n",
684 if (NULL != disconnect_task)
685 GNUNET_SCHEDULER_cancel (disconnect_task);
686 disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit,
693 * Start the testcase, we know the peers and have handles to CADET.
695 * Testcase continues when the root receives confirmation of connected peers,
696 * on callback function ch.
698 * @param cls Closure (unused).
701 start_test (void *cls)
703 struct GNUNET_MQ_MessageHandler handlers[] = {
704 GNUNET_MQ_hd_var_size (data,
705 GNUNET_MESSAGE_TYPE_DUMMY,
706 struct GNUNET_MessageHeader,
708 GNUNET_MQ_handler_end ()
710 struct CadetTestChannelWrapper *ch;
711 enum GNUNET_CADET_ChannelOption flags;
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 start_time = GNUNET_TIME_absolute_get ();
717 ch = GNUNET_new (struct CadetTestChannelWrapper);
718 outgoing_ch = GNUNET_CADET_channel_create (h1,
726 ch->ch = outgoing_ch;
727 GNUNET_assert (NULL == disconnect_task);
729 = GNUNET_SCHEDULER_add_delayed (short_time,
730 &gather_stats_and_exit,
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Sending data initializer on channel %p...\n",
735 send_test_message (outgoing_ch);
740 * Callback to be called when the requested peer information is available
742 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
743 * @param op the operation this callback corresponds to
744 * @param pinfo the result; will be NULL if the operation has failed
745 * @param emsg error message if the operation has failed;
746 * NULL if the operation is successfull
750 struct GNUNET_TESTBED_Operation *op,
751 const struct GNUNET_TESTBED_PeerInformation *pinfo,
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "ID callback for %ld\n",
759 if ((NULL == pinfo) ||
762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 abort_test (__LINE__);
768 p_id[i] = pinfo->result.id;
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 GNUNET_i2s (p_id[i]));
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Got all IDs, starting test\n");
777 test_task = GNUNET_SCHEDULER_add_now (&start_test,
783 * test main: start test when all peers are connected
785 * @param cls Closure.
786 * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
787 * @param num_peers Number of peers that are running.
788 * @param peers Array of peers.
789 * @param cadets Handle to each of the CADETs of the peers.
793 struct GNUNET_CADET_TEST_Context *ctx,
794 unsigned int num_peers,
795 struct GNUNET_TESTBED_Peer **peers,
796 struct GNUNET_CADET_Handle **cadets)
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 peers_running = num_peers;
802 GNUNET_assert (peers_running == peers_requested);
803 testbed_peers = peers;
805 h2 = cadets[num_peers - 1];
806 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
809 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
810 GNUNET_TESTBED_PIT_IDENTITY,
813 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
814 GNUNET_TESTBED_PIT_IDENTITY,
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "requested peer ids\n");
829 static const struct GNUNET_HashCode *ports[2];
830 struct GNUNET_MQ_MessageHandler handlers[] = {
831 GNUNET_MQ_hd_var_size (data,
832 GNUNET_MESSAGE_TYPE_DUMMY,
833 struct GNUNET_MessageHeader,
835 GNUNET_MQ_handler_end ()
837 const char *config_file = "test_cadet.conf";
838 char port_id[] = "test port";
839 struct GNUNET_GETOPT_CommandLineOption options[] = {
840 GNUNET_GETOPT_option_relative_time ('t',
843 gettext_noop ("set short timeout"),
845 GNUNET_GETOPT_option_uint ('m',
848 gettext_noop ("set number of messages to send"),
850 GNUNET_GETOPT_option_uint ('p',
853 gettext_noop ("number of peers to launch"),
855 GNUNET_GETOPT_OPTION_END
858 GNUNET_log_setup ("test-cadet-flow",
861 total_packets = TOTAL_PACKETS;
862 short_time = SHORT_TIME;
863 if (-1 == GNUNET_GETOPT_run (argv[0],
869 "test failed: problem with CLI parameters\n");
872 GNUNET_CRYPTO_hash (port_id,
877 GNUNET_CADET_TEST_ruN ("test_cadet_flow",
881 NULL, /* tmain cls */
891 /* end of test_cadet_flow.c */