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 (GNUNET_TIME_UNIT_MILLISECONDS, 10)
67 * How many packets to send.
69 static unsigned int total_packets = TOTAL_PACKETS_DEFAULT;
72 * Time to wait for fast operations.
74 static struct GNUNET_TIME_Relative short_time;
77 * Size of each test packet's payload
79 static size_t size_payload = sizeof (uint32_t);
82 * Operation to get peer ids.
84 static struct GNUNET_TESTBED_Operation *t_op[2];
89 static struct GNUNET_PeerIdentity *p_id[2];
94 static struct GNUNET_HashCode port;
99 static unsigned int p_ids;
102 * Is the setup initialized?
104 static int initialized;
107 * Number of payload packes sent.
109 static int data_sent;
112 * Number of payload packets received.
114 static int data_received;
117 * Number of payload packed acknowledgements sent.
122 * Number of payload packed explicitly (app level) acknowledged.
124 static int ack_received;
127 * Total number of peers asked to run.
129 static unsigned int peers_requested = 2;
132 * Number of currently running peers (should be same as @c peers_requested).
134 static unsigned int peers_running;
137 * Test context (to shut down).
139 struct GNUNET_CADET_TEST_Context *test_ctx;
142 * Task called to disconnect peers.
144 static struct GNUNET_SCHEDULER_Task *disconnect_task;
147 * Task To perform tests
149 static struct GNUNET_SCHEDULER_Task *test_task;
152 * Task runnining #send_next_msg().
154 static struct GNUNET_SCHEDULER_Task *send_next_msg_task;
157 * Cadet handle for the root peer
159 static struct GNUNET_CADET_Handle *h1;
162 * Cadet handle for the first leaf peer
164 static struct GNUNET_CADET_Handle *h2;
167 * Channel handle for the root peer
169 static struct GNUNET_CADET_Channel *outgoing_ch;
172 * Channel handle for the dest peer
174 static struct GNUNET_CADET_Channel *incoming_ch;
177 * Time we started the data transmission (after channel has been established
180 static struct GNUNET_TIME_Absolute start_time;
185 static struct GNUNET_TESTBED_Peer **testbed_peers;
188 * Statistics operation handle.
190 static struct GNUNET_TESTBED_Operation *stats_op;
195 static unsigned int ka_sent;
198 * Keepalives received.
200 static unsigned int ka_received;
203 * How many messages were dropped by CADET because of full buffers?
205 static unsigned int msg_dropped;
209 * Show the results of the test (banwidth acheived) and log them to GAUGER
214 static struct GNUNET_TIME_Absolute end_time;
215 static struct GNUNET_TIME_Relative total_time;
217 end_time = GNUNET_TIME_absolute_get ();
218 total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time);
220 "\nResults of test \"%s\"\n",
224 GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES));
226 "Test bandwidth: %f kb/s\n",
227 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
229 "Test throughput: %f packets/s\n\n",
230 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
233 total_packets * 1000.0 / (total_time.rel_value_us / 1000),
239 * Shut down peergroup, clean up.
241 * @param cls Closure (unused).
242 * @param tc Task Context.
245 shutdown_task (void *cls)
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 if (NULL != send_next_msg_task)
251 GNUNET_SCHEDULER_cancel (send_next_msg_task);
252 send_next_msg_task = NULL;
254 if (NULL != test_task)
256 GNUNET_SCHEDULER_cancel (test_task);
259 for (unsigned int i = 0; i < 2; i++)
260 GNUNET_TESTBED_operation_done (t_op[i]);
261 if (NULL != outgoing_ch)
263 GNUNET_CADET_channel_destroy (outgoing_ch);
266 if (NULL != incoming_ch)
268 GNUNET_CADET_channel_destroy (incoming_ch);
271 GNUNET_CADET_TEST_cleanup (test_ctx);
276 * Stats callback. Finish the stats testbed operation and when all stats have
277 * been iterated, shutdown the test.
279 * @param cls Closure (line number from which termination was requested).
280 * @param op the operation that has been finished
281 * @param emsg error message in case the operation has failed; will be NULL if
282 * operation has executed successfully.
285 stats_cont (void *cls,
286 struct GNUNET_TESTBED_Operation *op,
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
290 "KA sent: %u, KA received: %u\n",
293 if ((KEEPALIVE == test) && ((ka_sent < 2) || (ka_sent > ka_received + 1)))
298 GNUNET_TESTBED_operation_done (stats_op);
300 if (NULL != disconnect_task)
301 GNUNET_SCHEDULER_cancel (disconnect_task);
302 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
308 * Process statistic values.
310 * @param cls closure (line number, unused)
311 * @param peer the peer the statistic belong to
312 * @param subsystem name of subsystem that created the statistic
313 * @param name the name of the datum
314 * @param value the current value
315 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
316 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
319 stats_iterator (void *cls,
320 const struct GNUNET_TESTBED_Peer *peer,
321 const char *subsystem,
326 static const char *s_sent = "# keepalives sent";
327 static const char *s_recv = "# keepalives received";
328 static const char *rdrops = "# messages dropped due to full buffer";
329 static const char *cdrops = "# messages dropped due to slow client";
332 i = GNUNET_TESTBED_get_index (peer);
333 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i,
334 subsystem, name, (unsigned long long) value);
335 if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
337 if (0 == strncmp (s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
339 if (0 == strncmp (rdrops, name, strlen (rdrops)))
340 msg_dropped += value;
341 if (0 == strncmp (cdrops, name, strlen (cdrops)))
342 msg_dropped += value;
349 * Task to gather all statistics.
351 * @param cls Closure (line from which the task was scheduled).
354 gather_stats_and_exit (void *cls)
358 disconnect_task = NULL;
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
360 "gathering statistics from line %ld\n",
362 if (NULL != outgoing_ch)
364 GNUNET_CADET_channel_destroy (outgoing_ch);
367 stats_op = GNUNET_TESTBED_get_statistics (peers_running,
378 * Abort test: schedule disconnect and shutdown immediately
380 * @param line Line in the code the abort is requested from (__LINE__).
383 abort_test (long line)
385 if (NULL != disconnect_task)
387 GNUNET_SCHEDULER_cancel (disconnect_task);
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
389 "Aborting test from %ld\n",
392 GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
399 * Send a message on the channel with the appropriate size and payload.
401 * Update the appropriate *_sent counter.
403 * @param channel Channel to send the message on.
406 send_test_message (struct GNUNET_CADET_Channel *channel)
408 struct GNUNET_MQ_Envelope *env;
409 struct GNUNET_MessageHeader *msg;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Sending test message on channel %p\n",
418 if (GNUNET_NO == initialized)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n");
423 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer
426 else if (SPEED == test || SPEED_ACK == test)
428 if (get_target_channel() == channel)
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Sending ACK %u [%d bytes]\n",
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Sending DATA %u [%d bytes]\n",
447 else if (FORWARD == test)
451 else if (P2P_SIGNAL == test)
459 env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY);
461 data = (uint32_t *) &msg[1];
462 *data = htonl (payload);
463 GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env);
468 * Task to request a new data transmission in a SPEED test, without waiting
469 * for previous messages to be sent/arrrive.
471 * @param cls Closure (unused).
474 send_next_msg (void *cls)
476 struct GNUNET_CADET_Channel *channel;
478 send_next_msg_task = NULL;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Sending next message: %d\n",
483 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch;
484 GNUNET_assert (NULL != channel);
485 GNUNET_assert (SPEED == test);
486 send_test_message (channel);
487 if (data_sent < total_packets)
489 /* SPEED test: Send all messages as soon as possible */
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Scheduling message %d\n",
494 GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL,
502 * Check if payload is sane (size contains payload).
504 * @param cls should match #ch
505 * @param message The actual message.
506 * @return #GNUNET_OK to keep the channel open,
507 * #GNUNET_SYSERR to close it (signal serious error).
510 check_data (void *cls,
511 const struct GNUNET_MessageHeader *message)
513 return GNUNET_OK; /* all is well-formed */
518 * Function is called whenever a message is received.
520 * @param cls closure (set from GNUNET_CADET_connect(), peer number)
521 * @param message the actual message
524 handle_data (void *cls,
525 const struct GNUNET_MessageHeader *message)
527 struct CadetTestChannelWrapper *ch = cls;
528 struct GNUNET_CADET_Channel *channel = ch->ch;
533 GNUNET_CADET_receive_done (channel);
534 counter = get_target_channel () == channel ? &data_received : &ack_received;
535 if (channel == outgoing_ch)
537 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
538 "Root client got a message.\n");
540 else if (channel == incoming_ch)
542 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
543 "Leaf client got a message.\n");
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
548 "Unknown channel %p.\n",
553 data = (uint32_t *) &message[1];
554 payload = ntohl (*data);
555 if (payload == *counter)
557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
558 "Payload as expected: %u\n",
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "Received payload %u, expected: %u\n",
568 if (get_target_channel () == channel) /* Got "data" */
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
571 " received data %u\n",
573 if (data_received < total_packets)
578 if (SPEED_ACK == test || SPEED == test)
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
582 send_test_message (channel);
583 if (ack_received < total_packets && SPEED != test)
585 if (ok == 2 && SPEED == test)
589 if (test == P2P_SIGNAL)
591 GNUNET_CADET_channel_destroy (incoming_ch);
596 GNUNET_CADET_channel_destroy (outgoing_ch);
604 * Method called whenever a peer connects to a port in MQ-based CADET.
606 * @param cls Closure from #GNUNET_CADET_open_port (peer # as long).
607 * @param channel New handle to the channel.
608 * @param source Peer that started this channel.
609 * @return Closure for the incoming @a channel. It's given to:
610 * - The #GNUNET_CADET_DisconnectEventHandler (given to
611 * #GNUNET_CADET_open_port) when the channel dies.
612 * - Each the #GNUNET_MQ_MessageCallback handlers for each message
613 * received on the @a channel.
616 connect_handler (void *cls,
617 struct GNUNET_CADET_Channel *channel,
618 const struct GNUNET_PeerIdentity *source)
620 struct CadetTestChannelWrapper *ch;
621 long peer = (long) cls;
623 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
624 "Incoming channel from %s to %ld: %p\n",
628 if (peer == peers_requested - 1)
630 if (NULL != incoming_ch)
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
633 "Duplicate incoming channel for client %lu\n",
637 incoming_ch = channel;
641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
642 "Incoming channel for unexpected peer #%lu\n",
646 ch = GNUNET_new (struct CadetTestChannelWrapper);
654 * Function called whenever an MQ-channel is destroyed, even if the destruction
655 * was requested by #GNUNET_CADET_channel_destroy.
656 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
658 * It should clean up any associated state, including cancelling any pending
659 * transmission on this channel.
661 * @param cls Channel closure (channel wrapper).
662 * @param channel Connection to the other end (henceforth invalid).
665 disconnect_handler (void *cls,
666 const struct GNUNET_CADET_Channel *channel)
668 struct CadetTestChannelWrapper *ch_w = cls;
670 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
671 "Channel disconnected at %d\n",
673 GNUNET_assert (ch_w->ch == channel);
674 if (channel == incoming_ch)
676 else if (outgoing_ch == channel)
679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
680 "Disconnect on unknown channel %p\n",
682 if (NULL != disconnect_task)
683 GNUNET_SCHEDULER_cancel (disconnect_task);
684 disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit,
691 * Start the testcase, we know the peers and have handles to CADET.
693 * Testcase continues when the root receives confirmation of connected peers,
694 * on callback function ch.
696 * @param cls Closure (unused).
699 start_test (void *cls)
701 struct GNUNET_MQ_MessageHandler handlers[] = {
702 GNUNET_MQ_hd_var_size (data,
703 GNUNET_MESSAGE_TYPE_DUMMY,
704 struct GNUNET_MessageHeader,
706 GNUNET_MQ_handler_end ()
708 struct CadetTestChannelWrapper *ch;
709 enum GNUNET_CADET_ChannelOption flags;
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 start_time = GNUNET_TIME_absolute_get ();
715 ch = GNUNET_new (struct CadetTestChannelWrapper);
716 outgoing_ch = GNUNET_CADET_channel_create (h1,
724 ch->ch = outgoing_ch;
725 GNUNET_assert (NULL == disconnect_task);
727 = GNUNET_SCHEDULER_add_delayed (short_time,
728 &gather_stats_and_exit,
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Sending data initializer on channel %p...\n",
733 send_test_message (outgoing_ch);
738 * Callback to be called when the requested peer information is available
740 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
741 * @param op the operation this callback corresponds to
742 * @param pinfo the result; will be NULL if the operation has failed
743 * @param emsg error message if the operation has failed;
744 * NULL if the operation is successfull
748 struct GNUNET_TESTBED_Operation *op,
749 const struct GNUNET_TESTBED_PeerInformation *pinfo,
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
755 "ID callback for %ld\n",
757 if ( (NULL == pinfo) ||
760 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
763 abort_test (__LINE__);
766 p_id[i] = pinfo->result.id;
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 GNUNET_i2s (p_id[i]));
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Got all IDs, starting test\n");
775 test_task = GNUNET_SCHEDULER_add_now (&start_test,
781 * test main: start test when all peers are connected
783 * @param cls Closure.
784 * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
785 * @param num_peers Number of peers that are running.
786 * @param peers Array of peers.
787 * @param cadets Handle to each of the CADETs of the peers.
791 struct GNUNET_CADET_TEST_Context *ctx,
792 unsigned int num_peers,
793 struct GNUNET_TESTBED_Peer **peers,
794 struct GNUNET_CADET_Handle **cadets)
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 peers_running = num_peers;
800 GNUNET_assert (peers_running == peers_requested);
801 testbed_peers = peers;
803 h2 = cadets[num_peers - 1];
804 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
807 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
808 GNUNET_TESTBED_PIT_IDENTITY,
811 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
812 GNUNET_TESTBED_PIT_IDENTITY,
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "requested peer ids\n");
827 static const struct GNUNET_HashCode *ports[2];
828 struct GNUNET_MQ_MessageHandler handlers[] = {
829 GNUNET_MQ_hd_var_size (data,
830 GNUNET_MESSAGE_TYPE_DUMMY,
831 struct GNUNET_MessageHeader,
833 GNUNET_MQ_handler_end ()
835 const char *config_file = "test_cadet.conf";
836 char port_id[] = "test port";
837 struct GNUNET_GETOPT_CommandLineOption options[] = {
838 GNUNET_GETOPT_option_relative_time ('t',
841 gettext_noop ("set short timeout"),
843 GNUNET_GETOPT_option_uint ('m',
846 gettext_noop ("set number of messages to send"),
848 GNUNET_GETOPT_option_uint ('p',
851 gettext_noop ("number of peers to launch"),
853 GNUNET_GETOPT_OPTION_END
856 GNUNET_log_setup ("test-cadet-flow",
859 total_packets = TOTAL_PACKETS;
860 short_time = SHORT_TIME;
861 if (-1 == GNUNET_GETOPT_run (argv[0],
867 "test failed: problem with CLI parameters\n");
870 GNUNET_CRYPTO_hash (port_id,
875 GNUNET_CADET_TEST_ruN ("test_cadet_flow",
879 NULL, /* tmain cls */
888 /* end of test_cadet_flow.c */