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_enc.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
59 * Which test are we running?
64 * String with test name
69 * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
71 static int test_backwards = GNUNET_NO;
74 * How many events have happened
79 * Each peer is supposed to generate the following callbacks:
80 * 1 incoming channel (@dest)
81 * 1 connected peer (@orig)
82 * 1 received data packet (@dest)
83 * 1 received data packet (@orig)
84 * 1 received channel destroy (@dest)
85 * _________________________________
86 * 5 x ok expected per peer
92 * Size of each test packet
94 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
97 * Operation to get peer ids.
99 struct GNUNET_TESTBED_Operation *t_op[2];
104 struct GNUNET_PeerIdentity *p_id[2];
112 * Is the setup initialized?
114 static int initialized;
117 * Number of payload packes sent
119 static int data_sent;
122 * Number of payload packets received
124 static int data_received;
127 * Number of payload packed explicitly (app level) acknowledged
132 * Total number of currently running peers.
134 static unsigned long long peers_running;
137 * Test context (to shut down).
139 struct GNUNET_MESH_TEST_Context *test_ctx;
142 * Task called to disconnect peers.
144 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
147 * Task To perform tests
149 static GNUNET_SCHEDULER_TaskIdentifier test_task;
152 * Task called to shutdown test.
154 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
157 * Mesh handle for the root peer
159 static struct GNUNET_MESH_Handle *h1;
162 * Mesh handle for the first leaf peer
164 static struct GNUNET_MESH_Handle *h2;
167 * Channel handle for the root peer
169 static struct GNUNET_MESH_Channel *ch;
172 * Channel handle for the dest peer
174 static struct GNUNET_MESH_Channel *incoming_ch;
177 * Time we started the data transmission (after channel has been established
180 static struct GNUNET_TIME_Absolute start_time;
184 * Show the results of the test (banwidth acheived) and log them to GAUGER
189 static struct GNUNET_TIME_Absolute end_time;
190 static struct GNUNET_TIME_Relative total_time;
192 end_time = GNUNET_TIME_absolute_get();
193 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
194 FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
195 FPRINTF (stderr, "Test time %s\n",
196 GNUNET_STRINGS_relative_time_to_string (total_time,
198 FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
199 4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
200 FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
201 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
202 GAUGER ("MESH", test_name,
203 TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
209 * Shut down peergroup, clean up.
211 * @param cls Closure (unused).
212 * @param tc Task Context.
215 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
218 shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
223 * Disconnect from mesh services af all peers, call shutdown.
225 * @param cls Closure (unused).
226 * @param tc Task Context.
229 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 long line = (long) cls;
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "disconnecting mesh service of peers, called from line %ld\n",
237 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
238 for (i = 0; i < 2; i++)
240 GNUNET_TESTBED_operation_done (t_op[i]);
244 GNUNET_MESH_channel_destroy (ch);
247 if (NULL != incoming_ch)
249 GNUNET_MESH_channel_destroy (incoming_ch);
252 GNUNET_MESH_TEST_cleanup (test_ctx);
253 if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
255 GNUNET_SCHEDULER_cancel (shutdown_handle);
257 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
262 * Abort test: schedule disconnect and shutdown immediately
264 * @param line Line in the code the abort is requested from (__LINE__).
267 abort_test (long line)
269 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
271 GNUNET_SCHEDULER_cancel (disconnect_task);
272 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
278 * Transmit ready callback.
280 * @param cls Closure (message type).
281 * @param size Size of the tranmist buffer.
282 * @param buf Pointer to the beginning of the buffer.
284 * @return Number of bytes written to buf.
287 tmt_rdy (void *cls, size_t size, void *buf);
291 * Task to schedule a new data transmission.
293 * @param cls Closure (peer #).
294 * @param tc Task Context.
297 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
299 struct GNUNET_MESH_TransmitHandle *th;
300 struct GNUNET_MESH_Channel *channel;
302 if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
306 if (GNUNET_YES == test_backwards)
308 channel = incoming_ch;
314 th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
315 GNUNET_TIME_UNIT_FOREVER_REL,
316 size_payload, &tmt_rdy, (void *) 1L);
319 unsigned long i = (unsigned long) cls;
321 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
324 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " in 1 ms\n");
325 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
326 &data_task, (void *)1UL);
331 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
332 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
333 GNUNET_TIME_UNIT_MILLISECONDS,
335 &data_task, (void *)i);
342 * Transmit ready callback
344 * @param cls Closure (message type).
345 * @param size Size of the buffer we have.
346 * @param buf Buffer to copy data to.
349 tmt_rdy (void *cls, size_t size, void *buf)
351 struct GNUNET_MessageHeader *msg = buf;
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 " tmt_rdy called\n");
356 if (size < size_payload || NULL == buf)
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 "size %u, buf %p, data_sent %u, data_received %u\n",
367 msg->size = htons (size);
368 msg->type = htons ((long) cls);
369 data = (uint32_t *) &msg[1];
370 *data = htonl (data_sent);
371 if (SPEED == test && GNUNET_YES == initialized)
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 " Sent packet %d\n", data_sent);
376 if (data_sent < TOTAL_PACKETS)
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 " Scheduling packet %d\n", data_sent + 1);
380 GNUNET_SCHEDULER_add_now(&data_task, NULL);
388 * Function is called whenever a message is received.
390 * @param cls closure (set from GNUNET_MESH_connect)
391 * @param channel connection to the other end
392 * @param channel_ctx place to store local state associated with the channel
393 * @param message the actual message
394 * @return GNUNET_OK to keep the connection open,
395 * GNUNET_SYSERR to close it (signal serious error)
398 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
400 const struct GNUNET_MessageHeader *message)
402 long client = (long) cls;
403 long expected_target_client;
408 GNUNET_MESH_receive_done (channel);
412 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
414 GNUNET_SCHEDULER_cancel (disconnect_task);
415 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
416 &disconnect_mesh_peers,
424 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
427 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
428 "Leaf client %li got a message.\n",
435 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
436 data = (uint32_t *) &message[1];
437 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
438 if (SPEED == test && GNUNET_YES == test_backwards)
440 expected_target_client = 0L;
444 expected_target_client = 4L;
447 if (GNUNET_NO == initialized)
449 initialized = GNUNET_YES;
450 start_time = GNUNET_TIME_absolute_get ();
453 GNUNET_assert (4L == client);
454 GNUNET_SCHEDULER_add_now (&data_task, NULL);
459 if (client == expected_target_client) // Normally 4
462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
463 " received data %u\n", data_received);
464 if (SPEED != test || (ok_goal - 2) == ok)
466 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
467 GNUNET_TIME_UNIT_FOREVER_REL,
468 size_payload, &tmt_rdy, (void *) 1L);
473 if (data_received < TOTAL_PACKETS)
479 if (test == SPEED_ACK || test == SPEED)
482 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
483 " received ack %u\n", data_ack);
484 GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
485 GNUNET_TIME_UNIT_FOREVER_REL,
486 size_payload, &tmt_rdy, (void *) 1L);
487 if (data_ack < TOTAL_PACKETS && SPEED != test)
489 if (ok == 2 && SPEED == test)
493 if (test == P2P_SIGNAL)
495 GNUNET_MESH_channel_destroy (incoming_ch);
500 GNUNET_MESH_channel_destroy (ch);
505 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
507 GNUNET_SCHEDULER_cancel (disconnect_task);
508 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
509 &disconnect_mesh_peers,
518 * Handlers, for diverse services
520 static struct GNUNET_MESH_MessageHandler handlers[] = {
521 {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
527 * Method called whenever another peer has added us to a channel
528 * the other peer initiated.
530 * @param cls Closure.
531 * @param channel New handle to the channel.
532 * @param initiator Peer that started the channel.
533 * @param port Port this channel is connected to.
534 * @return Initial channel context for the channel
535 * (can be NULL -- that's not an error).
538 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
539 const struct GNUNET_PeerIdentity *initiator,
542 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
543 "Incoming channel from %s to peer %d\n",
544 GNUNET_i2s (initiator), (long) cls);
546 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
547 if ((long) cls == 4L)
548 incoming_ch = channel;
551 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
552 "Incoming channel for unknown client %lu\n", (long) cls);
555 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
557 GNUNET_SCHEDULER_cancel (disconnect_task);
558 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
559 &disconnect_mesh_peers,
567 * Function called whenever an inbound channel is destroyed. Should clean up
568 * any associated state.
570 * @param cls closure (set from GNUNET_MESH_connect)
571 * @param channel connection to the other end (henceforth invalid)
572 * @param channel_ctx place where local state associated
573 * with the channel is stored
576 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
581 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
582 "Incoming channel disconnected at peer %d\n",
589 else if (0L == i && P2P_SIGNAL == test)
595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
596 "Unknown peer! %d\n", i);
597 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
599 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
601 GNUNET_SCHEDULER_cancel (disconnect_task);
602 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
611 * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
613 * Testcase continues when the root receives confirmation of connected peers,
614 * on callback funtion ch.
616 * @param cls Closure (unsued).
617 * @param tc Task Context.
620 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "schedule timeout in TIMEOUT\n");
630 if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
632 GNUNET_SCHEDULER_cancel (disconnect_task);
634 if (SPEED_NOBUF == test)
642 if (SPEED_REL == test)
649 ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, nobuf, rel);
651 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
652 &disconnect_mesh_peers,
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Sending data initializer...\n");
659 GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
660 GNUNET_TIME_UNIT_FOREVER_REL,
661 size_payload, &tmt_rdy, (void *) 1L);
665 * Callback to be called when the requested peer information is available
667 * @param cls the closure from GNUNET_TESTBED_peer_get_information()
668 * @param op the operation this callback corresponds to
669 * @param pinfo the result; will be NULL if the operation has failed
670 * @param emsg error message if the operation has failed;
671 * NULL if the operation is successfull
675 struct GNUNET_TESTBED_Operation *op,
676 const struct GNUNET_TESTBED_PeerInformation *pinfo,
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
683 if (NULL == pinfo || NULL != emsg)
685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
686 abort_test (__LINE__);
689 p_id[i] = pinfo->result.id;
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " id: %s\n", GNUNET_i2s (p_id[i]));
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
695 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
700 * test main: start test when all peers are connected
702 * @param cls Closure.
703 * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
704 * @param num_peers Number of peers that are running.
705 * @param peers Array of peers.
706 * @param meshes Handle to each of the MESHs of the peers.
710 struct GNUNET_MESH_TEST_Context *ctx,
711 unsigned int num_peers,
712 struct GNUNET_TESTBED_Peer **peers,
713 struct GNUNET_MESH_Handle **meshes)
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
718 peers_running = num_peers;
720 h2 = meshes[num_peers - 1];
721 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
722 &disconnect_mesh_peers,
724 shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
725 &shutdown_task, NULL);
726 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
727 GNUNET_TESTBED_PIT_IDENTITY,
728 &pi_cb, (void *) 0L);
729 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
730 GNUNET_TESTBED_PIT_IDENTITY,
731 &pi_cb, (void *) 1L);
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
740 main (int argc, char *argv[])
742 initialized = GNUNET_NO;
744 const char *config_file;
746 GNUNET_log_setup ("test", "DEBUG", NULL);
747 config_file = "test_mesh.conf";
749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
750 if (strstr (argv[0], "_small_forward") != NULL)
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
754 test_name = "unicast";
757 else if (strstr (argv[0], "_small_signal") != NULL)
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
761 test_name = "signal";
764 else if (strstr (argv[0], "_small_speed_ack") != NULL)
766 /* Each peer is supposed to generate the following callbacks:
767 * 1 incoming channel (@dest)
768 * TOTAL_PACKETS received data packet (@dest)
769 * TOTAL_PACKETS received data packet (@orig)
770 * 1 received channel destroy (@dest)
771 * _________________________________
772 * 5 x ok expected per peer
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
776 test_name = "speed ack";
777 ok_goal = TOTAL_PACKETS * 2 + 2;
779 else if (strstr (argv[0], "_small_speed") != NULL)
781 /* Each peer is supposed to generate the following callbacks:
782 * 1 incoming channel (@dest)
783 * 1 initial packet (@dest)
784 * TOTAL_PACKETS received data packet (@dest)
785 * 1 received data packet (@orig)
786 * 1 received channel destroy (@dest)
787 * _________________________________
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
790 ok_goal = TOTAL_PACKETS + 4;
791 if (strstr (argv[0], "_nobuf") != NULL)
794 test_name = "speed nobuf";
796 else if (strstr (argv[0], "_reliable") != NULL)
799 test_name = "speed reliable";
800 config_file = "test_mesh_drop.conf";
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
815 if (strstr (argv[0], "backwards") != NULL)
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
820 test_backwards = GNUNET_YES;
821 aux = GNUNET_malloc (32);
822 sprintf (aux, "backwards %s", test_name);
829 GNUNET_MESH_TEST_run ("test_mesh_small",
833 NULL, /* tmain cls */
841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
842 "FAILED! (%d/%d)\n", ok, ok_goal);
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
849 /* end of test_mesh_small.c */