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/>.
19 * @file cadet/test_cadet_flow.c
21 * @author Christian Grothoff
22 * @brief Test for flow control of CADET service
26 #include "cadet_test_lib.h"
27 #include "gnunet_cadet_service.h"
28 #include "gnunet_statistics_service.h"
33 * Ugly workaround to unify data handlers on incoming and outgoing channels.
35 struct CadetTestChannelWrapper
40 struct GNUNET_CADET_Channel *ch;
44 * How many messages to send by default.
46 #define TOTAL_PACKETS_DEFAULT 500
49 * How long until we give up on connecting the peers?
51 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
54 * Time to wait by default for stuff that should be rather fast.
56 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
59 * How fast do we send messages?
61 #define SEND_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10)
65 * How many packets to send.
67 static unsigned int total_packets = TOTAL_PACKETS_DEFAULT;
70 * Time to wait for fast operations.
72 static struct GNUNET_TIME_Relative short_time;
75 * Size of each test packet's payload
77 static size_t size_payload = sizeof (uint32_t);
80 * Operation to get peer ids.
82 static struct GNUNET_TESTBED_Operation *t_op[2];
87 static struct GNUNET_PeerIdentity *p_id[2];
92 static struct GNUNET_HashCode port;
97 static unsigned int p_ids;
100 * Is the setup initialized?
102 static int initialized;
105 * Number of payload packes sent.
107 static int data_sent;
110 * Number of payload packets received.
112 static int data_received;
115 * Number of payload packed acknowledgements sent.
120 * Number of payload packed explicitly (app level) acknowledged.
122 static int ack_received;
125 * Total number of peers asked to run.
127 static unsigned int peers_requested = 2;
130 * Number of currently running peers (should be same as @c peers_requested).
132 static unsigned int peers_running;
135 * Test context (to shut down).
137 struct GNUNET_CADET_TEST_Context *test_ctx;
140 * Task called to disconnect peers.
142 static struct GNUNET_SCHEDULER_Task *disconnect_task;
145 * Task To perform tests
147 static struct GNUNET_SCHEDULER_Task *test_task;
150 * Task runnining #send_next_msg().
152 static struct GNUNET_SCHEDULER_Task *send_next_msg_task;
155 * Cadet handle for the root peer
157 static struct GNUNET_CADET_Handle *h1;
160 * Cadet handle for the first leaf peer
162 static struct GNUNET_CADET_Handle *h2;
165 * Channel handle for the root peer
167 static struct GNUNET_CADET_Channel *outgoing_ch;
170 * Channel handle for the dest peer
172 static struct GNUNET_CADET_Channel *incoming_ch;
175 * Time we started the data transmission (after channel has been established
178 static struct GNUNET_TIME_Absolute start_time;
183 static struct GNUNET_TESTBED_Peer **testbed_peers;
186 * Statistics operation handle.
188 static struct GNUNET_TESTBED_Operation *stats_op;
193 static unsigned int ka_sent;
196 * Keepalives received.
198 static unsigned int ka_received;
201 * How many messages were dropped by CADET because of full buffers?
203 static unsigned int msg_dropped;
207 * Show the results of the test (banwidth acheived) and log them to GAUGER
212 static struct GNUNET_TIME_Absolute end_time;
213 static struct GNUNET_TIME_Relative total_time;
215 end_time = GNUNET_TIME_absolute_get ();
216 total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time);
218 "\nResults of test \"%s\"\n",
222 GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES));
224 "Test bandwidth: %f kb/s\n",
225 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
227 "Test throughput: %f packets/s\n\n",
228 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
231 total_packets * 1000.0 / (total_time.rel_value_us / 1000),
237 * Shut down peergroup, clean up.
239 * @param cls Closure (unused).
240 * @param tc Task Context.
243 shutdown_task (void *cls)
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 if (NULL != send_next_msg_task)
249 GNUNET_SCHEDULER_cancel (send_next_msg_task);
250 send_next_msg_task = NULL;
252 if (NULL != test_task)
254 GNUNET_SCHEDULER_cancel (test_task);
257 for (unsigned int i = 0; i < 2; i++)
258 GNUNET_TESTBED_operation_done (t_op[i]);
259 if (NULL != outgoing_ch)
261 GNUNET_CADET_channel_destroy (outgoing_ch);
264 if (NULL != incoming_ch)
266 GNUNET_CADET_channel_destroy (incoming_ch);
269 GNUNET_CADET_TEST_cleanup (test_ctx);
274 * Stats callback. Finish the stats testbed operation and when all stats have
275 * been iterated, shutdown the test.
277 * @param cls Closure (line number from which termination was requested).
278 * @param op the operation that has been finished
279 * @param emsg error message in case the operation has failed; will be NULL if
280 * operation has executed successfully.
283 stats_cont (void *cls,
284 struct GNUNET_TESTBED_Operation *op,
287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
288 "KA sent: %u, KA received: %u\n",
291 if ((KEEPALIVE == test) && ((ka_sent < 2) || (ka_sent > ka_received + 1)))
296 GNUNET_TESTBED_operation_done (stats_op);
298 if (NULL != disconnect_task)
299 GNUNET_SCHEDULER_cancel (disconnect_task);
300 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
306 * Process statistic values.
308 * @param cls closure (line number, unused)
309 * @param peer the peer the statistic belong to
310 * @param subsystem name of subsystem that created the statistic
311 * @param name the name of the datum
312 * @param value the current value
313 * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
314 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
317 stats_iterator (void *cls,
318 const struct GNUNET_TESTBED_Peer *peer,
319 const char *subsystem,
324 static const char *s_sent = "# keepalives sent";
325 static const char *s_recv = "# keepalives received";
326 static const char *rdrops = "# messages dropped due to full buffer";
327 static const char *cdrops = "# messages dropped due to slow client";
330 i = GNUNET_TESTBED_get_index (peer);
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i,
332 subsystem, name, (unsigned long long) value);
333 if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
335 if (0 == strncmp (s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
337 if (0 == strncmp (rdrops, name, strlen (rdrops)))
338 msg_dropped += value;
339 if (0 == strncmp (cdrops, name, strlen (cdrops)))
340 msg_dropped += value;
347 * Task to gather all statistics.
349 * @param cls Closure (line from which the task was scheduled).
352 gather_stats_and_exit (void *cls)
356 disconnect_task = NULL;
357 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
358 "gathering statistics from line %ld\n",
360 if (NULL != outgoing_ch)
362 GNUNET_CADET_channel_destroy (outgoing_ch);
365 stats_op = GNUNET_TESTBED_get_statistics (peers_running,
376 * Abort test: schedule disconnect and shutdown immediately
378 * @param line Line in the code the abort is requested from (__LINE__).
381 abort_test (long line)
383 if (NULL != disconnect_task)
385 GNUNET_SCHEDULER_cancel (disconnect_task);
386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387 "Aborting test from %ld\n",
390 GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
397 * Send a message on the channel with the appropriate size and payload.
399 * Update the appropriate *_sent counter.
401 * @param channel Channel to send the message on.
404 send_test_message (struct GNUNET_CADET_Channel *channel)
406 struct GNUNET_MQ_Envelope *env;
407 struct GNUNET_MessageHeader *msg;
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Sending test message on channel %p\n",
416 if (GNUNET_NO == initialized)
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n");
421 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer
424 else if (SPEED == test || SPEED_ACK == test)
426 if (get_target_channel() == channel)
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Sending ACK %u [%d bytes]\n",
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Sending DATA %u [%d bytes]\n",
445 else if (FORWARD == test)
449 else if (P2P_SIGNAL == test)
457 env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY);
459 data = (uint32_t *) &msg[1];
460 *data = htonl (payload);
461 GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env);
466 * Task to request a new data transmission in a SPEED test, without waiting
467 * for previous messages to be sent/arrrive.
469 * @param cls Closure (unused).
472 send_next_msg (void *cls)
474 struct GNUNET_CADET_Channel *channel;
476 send_next_msg_task = NULL;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "Sending next message: %d\n",
481 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch;
482 GNUNET_assert (NULL != channel);
483 GNUNET_assert (SPEED == test);
484 send_test_message (channel);
485 if (data_sent < total_packets)
487 /* SPEED test: Send all messages as soon as possible */
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Scheduling message %d\n",
492 GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL,
500 * Check if payload is sane (size contains payload).
502 * @param cls should match #ch
503 * @param message The actual message.
504 * @return #GNUNET_OK to keep the channel open,
505 * #GNUNET_SYSERR to close it (signal serious error).
508 check_data (void *cls,
509 const struct GNUNET_MessageHeader *message)
511 return GNUNET_OK; /* all is well-formed */
516 * Function is called whenever a message is received.
518 * @param cls closure (set from GNUNET_CADET_connect(), peer number)
519 * @param message the actual message
522 handle_data (void *cls,
523 const struct GNUNET_MessageHeader *message)
525 struct CadetTestChannelWrapper *ch = cls;
526 struct GNUNET_CADET_Channel *channel = ch->ch;
531 GNUNET_CADET_receive_done (channel);
532 counter = get_target_channel () == channel ? &data_received : &ack_received;
533 if (channel == outgoing_ch)
535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
536 "Root client got a message.\n");
538 else if (channel == incoming_ch)
540 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
541 "Leaf client got a message.\n");
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 "Unknown channel %p.\n",
551 data = (uint32_t *) &message[1];
552 payload = ntohl (*data);
553 if (payload == *counter)
555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
556 "Payload as expected: %u\n",
561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
562 "Received payload %u, expected: %u\n",
566 if (get_target_channel () == channel) /* Got "data" */
568 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
569 " received data %u\n",
571 if (data_received < total_packets)
576 if (SPEED_ACK == test || SPEED == test)
578 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
580 send_test_message (channel);
581 if (ack_received < total_packets && SPEED != test)
583 if (ok == 2 && SPEED == test)
587 if (test == P2P_SIGNAL)
589 GNUNET_CADET_channel_destroy (incoming_ch);
594 GNUNET_CADET_channel_destroy (outgoing_ch);
602 * Method called whenever a peer connects to a port in MQ-based CADET.
604 * @param cls Closure from #GNUNET_CADET_open_port (peer # as long).
605 * @param channel New handle to the channel.
606 * @param source Peer that started this channel.
607 * @return Closure for the incoming @a channel. It's given to:
608 * - The #GNUNET_CADET_DisconnectEventHandler (given to
609 * #GNUNET_CADET_open_port) when the channel dies.
610 * - Each the #GNUNET_MQ_MessageCallback handlers for each message
611 * received on the @a channel.
614 connect_handler (void *cls,
615 struct GNUNET_CADET_Channel *channel,
616 const struct GNUNET_PeerIdentity *source)
618 struct CadetTestChannelWrapper *ch;
619 long peer = (long) cls;
621 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
622 "Incoming channel from %s to %ld: %p\n",
626 if (peer == peers_requested - 1)
628 if (NULL != incoming_ch)
630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631 "Duplicate incoming channel for client %lu\n",
635 incoming_ch = channel;
639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
640 "Incoming channel for unexpected peer #%lu\n",
644 ch = GNUNET_new (struct CadetTestChannelWrapper);
652 * Function called whenever an MQ-channel is destroyed, even if the destruction
653 * was requested by #GNUNET_CADET_channel_destroy.
654 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
656 * It should clean up any associated state, including cancelling any pending
657 * transmission on this channel.
659 * @param cls Channel closure (channel wrapper).
660 * @param channel Connection to the other end (henceforth invalid).
663 disconnect_handler (void *cls,
664 const struct GNUNET_CADET_Channel *channel)
666 struct CadetTestChannelWrapper *ch_w = cls;
668 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
669 "Channel disconnected at %d\n",
671 GNUNET_assert (ch_w->ch == channel);
672 if (channel == incoming_ch)
674 else if (outgoing_ch == channel)
677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
678 "Disconnect on unknown channel %p\n",
680 if (NULL != disconnect_task)
681 GNUNET_SCHEDULER_cancel (disconnect_task);
682 disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit,
689 * Start the testcase, we know the peers and have handles to CADET.
691 * Testcase continues when the root receives confirmation of connected peers,
692 * on callback function ch.
694 * @param cls Closure (unused).
697 start_test (void *cls)
699 struct GNUNET_MQ_MessageHandler handlers[] = {
700 GNUNET_MQ_hd_var_size (data,
701 GNUNET_MESSAGE_TYPE_DUMMY,
702 struct GNUNET_MessageHeader,
704 GNUNET_MQ_handler_end ()
706 struct CadetTestChannelWrapper *ch;
707 enum GNUNET_CADET_ChannelOption flags;
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 start_time = GNUNET_TIME_absolute_get ();
713 ch = GNUNET_new (struct CadetTestChannelWrapper);
714 outgoing_ch = GNUNET_CADET_channel_create (h1,
722 ch->ch = outgoing_ch;
723 GNUNET_assert (NULL == disconnect_task);
725 = GNUNET_SCHEDULER_add_delayed (short_time,
726 &gather_stats_and_exit,
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Sending data initializer on channel %p...\n",
731 send_test_message (outgoing_ch);
736 * Callback to be called when the requested peer information is available
738 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
739 * @param op the operation this callback corresponds to
740 * @param pinfo the result; will be NULL if the operation has failed
741 * @param emsg error message if the operation has failed;
742 * NULL if the operation is successfull
746 struct GNUNET_TESTBED_Operation *op,
747 const struct GNUNET_TESTBED_PeerInformation *pinfo,
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "ID callback for %ld\n",
755 if ( (NULL == pinfo) ||
758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
761 abort_test (__LINE__);
764 p_id[i] = pinfo->result.id;
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 GNUNET_i2s (p_id[i]));
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Got all IDs, starting test\n");
773 test_task = GNUNET_SCHEDULER_add_now (&start_test,
779 * test main: start test when all peers are connected
781 * @param cls Closure.
782 * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
783 * @param num_peers Number of peers that are running.
784 * @param peers Array of peers.
785 * @param cadets Handle to each of the CADETs of the peers.
789 struct GNUNET_CADET_TEST_Context *ctx,
790 unsigned int num_peers,
791 struct GNUNET_TESTBED_Peer **peers,
792 struct GNUNET_CADET_Handle **cadets)
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 peers_running = num_peers;
798 GNUNET_assert (peers_running == peers_requested);
799 testbed_peers = peers;
801 h2 = cadets[num_peers - 1];
802 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
805 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
806 GNUNET_TESTBED_PIT_IDENTITY,
809 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
810 GNUNET_TESTBED_PIT_IDENTITY,
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "requested peer ids\n");
825 static const struct GNUNET_HashCode *ports[2];
826 struct GNUNET_MQ_MessageHandler handlers[] = {
827 GNUNET_MQ_hd_var_size (data,
828 GNUNET_MESSAGE_TYPE_DUMMY,
829 struct GNUNET_MessageHeader,
831 GNUNET_MQ_handler_end ()
833 const char *config_file = "test_cadet.conf";
834 char port_id[] = "test port";
835 struct GNUNET_GETOPT_CommandLineOption options[] = {
836 GNUNET_GETOPT_option_relative_time ('t',
839 gettext_noop ("set short timeout"),
841 GNUNET_GETOPT_option_uint ('m',
844 gettext_noop ("set number of messages to send"),
846 GNUNET_GETOPT_option_uint ('p',
849 gettext_noop ("number of peers to launch"),
851 GNUNET_GETOPT_OPTION_END
854 GNUNET_log_setup ("test-cadet-flow",
857 total_packets = TOTAL_PACKETS;
858 short_time = SHORT_TIME;
859 if (-1 == GNUNET_GETOPT_run (argv[0],
865 "test failed: problem with CLI parameters\n");
868 GNUNET_CRYPTO_hash (port_id,
873 GNUNET_CADET_TEST_ruN ("test_cadet_flow",
877 NULL, /* tmain cls */
886 /* end of test_cadet_flow.c */