2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file mesh/test_mesh_small.c
23 * @brief Test for the mesh service: retransmission of traffic.
27 #include "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
33 * How namy messages to send
35 #define TOTAL_PACKETS 1000
38 * How long until we give up on connecting the peers?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
43 * Time to wait for stuff that should be rather fast
45 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
48 * DIFFERENT TESTS TO RUN
58 * Which test are we running?
63 * String with test name
68 * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
70 static int test_backwards = GNUNET_NO;
73 * How many events have happened
78 * Each peer is supposed to generate the following callbacks:
79 * 1 incoming channel (@dest)
80 * 1 connected peer (@orig)
81 * 1 received data packet (@dest)
82 * 1 received data packet (@orig)
83 * 1 received channel destroy (@dest)
84 * _________________________________
85 * 5 x ok expected per peer
91 * Size of each test packet
93 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
96 * Operation to get peer ids.
98 struct GNUNET_TESTBED_Operation *t_op[2];
103 struct GNUNET_PeerIdentity *p_id[2];
111 * Is the setup initialized?
113 static int initialized;
116 * Number of payload packes sent
118 static int data_sent;
121 * Number of payload packets received
123 static int data_received;
126 * Number of payload packed explicitly (app level) acknowledged
131 * Total number of currently running peers.
133 static unsigned long long peers_running;
136 * Test context (to shut down).
138 struct GNUNET_MESH_TEST_Context *test_ctx;
141 * Task called to disconnect peers.
143 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
146 * Task To perform tests
148 static GNUNET_SCHEDULER_TaskIdentifier test_task;
151 * Task called to shutdown test.
153 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
156 * Mesh handle for the root peer
158 static struct GNUNET_MESH_Handle *h1;
161 * Mesh handle for the first leaf peer
163 static struct GNUNET_MESH_Handle *h2;
166 * Channel handle for the root peer
168 static struct GNUNET_MESH_Channel *ch;
171 * Channel handle for the dest peer
173 static struct GNUNET_MESH_Channel *incoming_ch;
176 * Time we started the data transmission (after channel has been established
179 static struct GNUNET_TIME_Absolute start_time;
183 * Show the results of the test (banwidth acheived) and log them to GAUGER
188 static struct GNUNET_TIME_Absolute end_time;
189 static struct GNUNET_TIME_Relative total_time;
191 end_time = GNUNET_TIME_absolute_get();
192 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
193 FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
194 FPRINTF (stderr, "Test time %s\n",
195 GNUNET_STRINGS_relative_time_to_string (total_time,
197 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
198 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
199 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
200 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
201 GAUGER ("MESH", test_name,
202 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
208 * Shut down peergroup, clean up.
210 * @param cls Closure (unused).
211 * @param tc Task Context.
214 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
217 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
222 * Disconnect from mesh services af all peers, call shutdown.
224 * @param cls Closure (unused).
225 * @param tc Task Context.
228 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
230 long line = (long) cls;
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "disconnecting mesh service of peers, called from line %ld\n",
236 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
237 for (i = 0; i < 2; i++)
239 GNUNET_TESTBED_operation_done (t_op[i]);
243 GNUNET_MESH_channel_destroy (ch);
246 if (NULL != incoming_ch)
248 GNUNET_MESH_channel_destroy (incoming_ch);
251 GNUNET_MESH_TEST_cleanup (test_ctx);
252 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
254 GNUNET_SCHEDULER_cancel (shutdown_handle);
256 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
261 * Abort test: schedule disconnect and shutdown immediately
263 * @param line Line in the code the abort is requested from (__LINE__).
266 abort_test (long line)
268 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
270 GNUNET_SCHEDULER_cancel (disconnect_task);
271 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
277 * Transmit ready callback.
279 * @param cls Closure (message type).
280 * @param size Size of the tranmist buffer.
281 * @param buf Pointer to the beginning of the buffer.
283 * @return Number of bytes written to buf.
286 tmt_rdy (void *cls, size_t size, void *buf);
290 * Task to schedule a new data transmission.
292 * @param cls Closure (peer #).
293 * @param tc Task Context.
296 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 struct GNUNET_MESH_TransmitHandle *th;
299 struct GNUNET_MESH_Channel *channel;
301 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
305 if (GNUNET_YES == test_backwards)
307 channel = incoming_ch;
313 th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
314 GNUNET_TIME_UNIT_FOREVER_REL,
315 size_payload, &tmt_rdy, (void *) 1L);
318 unsigned long i = (unsigned long) cls;
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
323 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n");
324 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
325 &data_task, (void *)1UL);
330 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
331 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
332 GNUNET_TIME_UNIT_MILLISECONDS,
334 &data_task, (void *)i);
341 * Transmit ready callback
343 * @param cls Closure (message type).
344 * @param size Size of the buffer we have.
345 * @param buf Buffer to copy data to.
348 tmt_rdy (void *cls, size_t size, void *buf)
350 struct GNUNET_MessageHeader *msg = buf;
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "tmt_rdy called, filling buffer\n");
355 if (size < size_payload || NULL == buf)
357 GNUNET_break (ok >= ok_goal - 2);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "size %u, buf %p, data_sent %u, data_received %u\n",
366 msg->size = htons (size);
367 msg->type = htons ((long) cls);
368 data = (uint32_t *) &msg[1];
369 *data = htonl (data_sent);
370 if (GNUNET_NO == initialized)
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "sending initializer\n");
375 else if (SPEED == test)
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 " Sent packet %d\n", data_sent);
380 if (data_sent < TOTAL_PACKETS)
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 " Scheduling packet %d\n", data_sent + 1);
384 GNUNET_SCHEDULER_add_now (&data_task, NULL);
393 * Function is called whenever a message is received.
395 * @param cls closure (set from GNUNET_MESH_connect)
396 * @param channel connection to the other end
397 * @param channel_ctx place to store local state associated with the channel
398 * @param message the actual message
399 * @return GNUNET_OK to keep the connection open,
400 * GNUNET_SYSERR to close it (signal serious error)
403 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
405 const struct GNUNET_MessageHeader *message)
407 long client = (long) cls;
408 long expected_target_client;
413 GNUNET_MESH_receive_done (channel);
417 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
419 GNUNET_SCHEDULER_cancel (disconnect_task);
420 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
421 &disconnect_mesh_peers,
429 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
432 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433 "Leaf client %li got a message.\n",
440 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
441 data = (uint32_t *) &message[1];
442 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
443 if (SPEED == test && GNUNET_YES == test_backwards)
445 expected_target_client = 0L;
449 expected_target_client = 4L;
452 if (GNUNET_NO == initialized)
454 initialized = GNUNET_YES;
455 start_time = GNUNET_TIME_absolute_get ();
458 GNUNET_assert (4L == client);
459 GNUNET_SCHEDULER_add_now (&data_task, NULL);
464 if (client == expected_target_client) // Normally 4
467 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
468 " received data %u\n", data_received);
469 if (SPEED != test || (ok_goal - 2) == ok)
471 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
472 GNUNET_TIME_UNIT_FOREVER_REL,
473 size_payload, &tmt_rdy, (void *) 1L);
478 if (data_received < TOTAL_PACKETS)
484 if (test == SPEED_ACK || test == SPEED)
487 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
488 " received ack %u\n", data_ack);
489 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
490 GNUNET_TIME_UNIT_FOREVER_REL,
491 size_payload, &tmt_rdy, (void *) 1L);
492 if (data_ack < TOTAL_PACKETS && SPEED != test)
494 if (ok == 2 && SPEED == test)
498 if (test == P2P_SIGNAL)
500 GNUNET_MESH_channel_destroy (incoming_ch);
505 GNUNET_MESH_channel_destroy (ch);
510 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
512 GNUNET_SCHEDULER_cancel (disconnect_task);
513 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
514 &disconnect_mesh_peers,
523 * Handlers, for diverse services
525 static struct GNUNET_MESH_MessageHandler handlers[] = {
526 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
532 * Method called whenever another peer has added us to a channel
533 * the other peer initiated.
535 * @param cls Closure.
536 * @param channel New handle to the channel.
537 * @param initiator Peer that started the channel.
538 * @param port Port this channel is connected to.
539 * @param options channel option flags
540 * @return Initial channel context for the channel
541 * (can be NULL -- that's not an error).
544 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
545 const struct GNUNET_PeerIdentity *initiator,
546 uint32_t port, enum GNUNET_MESH_ChannelOption options)
548 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
549 "Incoming channel from %s to peer %d\n",
550 GNUNET_i2s (initiator), (long) cls);
552 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
553 if ((long) cls == 4L)
554 incoming_ch = channel;
557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
558 "Incoming channel for unknown client %lu\n", (long) cls);
561 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
563 GNUNET_SCHEDULER_cancel (disconnect_task);
564 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
565 &disconnect_mesh_peers,
573 * Function called whenever an inbound channel is destroyed. Should clean up
574 * any associated state.
576 * @param cls closure (set from GNUNET_MESH_connect)
577 * @param channel connection to the other end (henceforth invalid)
578 * @param channel_ctx place where local state associated
579 * with the channel is stored
582 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
587 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
588 "Incoming channel disconnected at peer %d\n",
593 GNUNET_break (channel == incoming_ch);
598 if (P2P_SIGNAL == test)
602 GNUNET_break (channel == ch);
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
607 "Unknown peer! %d\n", i);
608 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
610 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
612 GNUNET_SCHEDULER_cancel (disconnect_task);
613 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
622 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
624 * Testcase continues when the root receives confirmation of connected peers,
625 * on callback funtion ch.
627 * @param cls Closure (unsued).
628 * @param tc Task Context.
631 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
633 enum GNUNET_MESH_ChannelOption flags;
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639 "schedule timeout in TIMEOUT\n");
640 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
642 GNUNET_SCHEDULER_cancel (disconnect_task);
645 flags = GNUNET_MESH_OPTION_DEFAULT;
646 if (SPEED_REL == test)
649 flags |= GNUNET_MESH_OPTION_RELIABLE;
651 ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags);
653 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
654 &disconnect_mesh_peers,
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Sending data initializer...\n");
661 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
662 GNUNET_TIME_UNIT_FOREVER_REL,
663 size_payload, &tmt_rdy, (void *) 1L);
667 * Callback to be called when the requested peer information is available
669 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
670 * @param op the operation this callback corresponds to
671 * @param pinfo the result; will be NULL if the operation has failed
672 * @param emsg error message if the operation has failed;
673 * NULL if the operation is successfull
677 struct GNUNET_TESTBED_Operation *op,
678 const struct GNUNET_TESTBED_PeerInformation *pinfo,
683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
685 if (NULL == pinfo || NULL != emsg)
687 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
688 abort_test (__LINE__);
691 p_id[i] = pinfo->result.id;
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id: %s\n", GNUNET_i2s (p_id[i]));
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
697 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
702 * test main: start test when all peers are connected
704 * @param cls Closure.
705 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
706 * @param num_peers Number of peers that are running.
707 * @param peers Array of peers.
708 * @param meshes Handle to each of the MESHs of the peers.
712 struct GNUNET_MESH_TEST_Context *ctx,
713 unsigned int num_peers,
714 struct GNUNET_TESTBED_Peer **peers,
715 struct GNUNET_MESH_Handle **meshes)
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
720 peers_running = num_peers;
722 h2 = meshes[num_peers - 1];
723 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
724 &disconnect_mesh_peers,
726 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
727 &shutdown_task, NULL);
728 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
729 GNUNET_TESTBED_PIT_IDENTITY,
730 &pi_cb, (void *) 0L);
731 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
732 GNUNET_TESTBED_PIT_IDENTITY,
733 &pi_cb, (void *) 1L);
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
742 main (int argc, char *argv[])
744 initialized = GNUNET_NO;
746 const char *config_file;
748 GNUNET_log_setup ("test", "DEBUG", NULL);
749 config_file = "test_mesh.conf";
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
752 if (strstr (argv[0], "_small_forward") != NULL)
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
756 test_name = "unicast";
759 else if (strstr (argv[0], "_small_signal") != NULL)
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
763 test_name = "signal";
766 else if (strstr (argv[0], "_small_speed_ack") != NULL)
768 /* Each peer is supposed to generate the following callbacks:
769 * 1 incoming channel (@dest)
770 * TOTAL_PACKETS received data packet (@dest)
771 * TOTAL_PACKETS received data packet (@orig)
772 * 1 received channel destroy (@dest)
773 * _________________________________
774 * 5 x ok expected per peer
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
778 test_name = "speed ack";
779 ok_goal = TOTAL_PACKETS * 2 + 2;
781 else if (strstr (argv[0], "_small_speed") != NULL)
783 /* Each peer is supposed to generate the following callbacks:
784 * 1 incoming channel (@dest)
785 * 1 initial packet (@dest)
786 * TOTAL_PACKETS received data packet (@dest)
787 * 1 received data packet (@orig)
788 * 1 received channel destroy (@dest)
789 * _________________________________
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
792 ok_goal = TOTAL_PACKETS + 4;
793 if (strstr (argv[0], "_reliable") != NULL)
796 test_name = "speed reliable";
797 config_file = "test_mesh_drop.conf";
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
812 if (strstr (argv[0], "backwards") != NULL)
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
817 test_backwards = GNUNET_YES;
818 aux = GNUNET_malloc (32);
819 sprintf (aux, "backwards %s", test_name);
826 GNUNET_MESH_TEST_run ("test_mesh_small",
830 NULL, /* tmain cls */
838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
839 "FAILED! (%d/%d)\n", ok, ok_goal);
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
846 /* end of test_mesh_small.c */